1 #include "ceph_debug.h"
4 #include <linux/kernel.h>
5 #include <linux/sched.h>
6 #include <linux/vmalloc.h>
7 #include <linux/wait.h>
11 #include "messenger.h"
14 * Capability management
16 * The Ceph metadata servers control client access to inode metadata
17 * and file data by issuing capabilities, granting clients permission
18 * to read and/or write both inode field and file data to OSDs
19 * (storage nodes). Each capability consists of a set of bits
20 * indicating which operations are allowed.
22 * If the client holds a *_SHARED cap, the client has a coherent value
23 * that can be safely read from the cached inode.
25 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
26 * client is allowed to change inode attributes (e.g., file size,
27 * mtime), note its dirty state in the ceph_cap, and asynchronously
28 * flush that metadata change to the MDS.
30 * In the event of a conflicting operation (perhaps by another
31 * client), the MDS will revoke the conflicting client capabilities.
33 * In order for a client to cache an inode, it must hold a capability
34 * with at least one MDS server. When inodes are released, release
35 * notifications are batched and periodically sent en masse to the MDS
36 * cluster to release server state.
41 * Generate readable cap strings for debugging output.
43 #define MAX_CAP_STR 20
44 static char cap_str
[MAX_CAP_STR
][40];
45 static DEFINE_SPINLOCK(cap_str_lock
);
46 static int last_cap_str
;
48 static char *gcap_string(char *s
, int c
)
50 if (c
& CEPH_CAP_GSHARED
)
52 if (c
& CEPH_CAP_GEXCL
)
54 if (c
& CEPH_CAP_GCACHE
)
60 if (c
& CEPH_CAP_GBUFFER
)
62 if (c
& CEPH_CAP_GLAZYIO
)
67 const char *ceph_cap_string(int caps
)
73 spin_lock(&cap_str_lock
);
75 if (last_cap_str
== MAX_CAP_STR
)
77 spin_unlock(&cap_str_lock
);
81 if (caps
& CEPH_CAP_PIN
)
84 c
= (caps
>> CEPH_CAP_SAUTH
) & 3;
87 s
= gcap_string(s
, c
);
90 c
= (caps
>> CEPH_CAP_SLINK
) & 3;
93 s
= gcap_string(s
, c
);
96 c
= (caps
>> CEPH_CAP_SXATTR
) & 3;
99 s
= gcap_string(s
, c
);
102 c
= caps
>> CEPH_CAP_SFILE
;
105 s
= gcap_string(s
, c
);
117 * Maintain a global pool of preallocated struct ceph_caps, referenced
118 * by struct ceph_caps_reservations. This ensures that we preallocate
119 * memory needed to successfully process an MDS response. (If an MDS
120 * sends us cap information and we fail to process it, we will have
121 * problems due to the client and MDS being out of sync.)
123 * Reservations are 'owned' by a ceph_cap_reservation context.
125 static spinlock_t caps_list_lock
;
126 static struct list_head caps_list
; /* unused (reserved or unreserved) */
127 static int caps_total_count
; /* total caps allocated */
128 static int caps_use_count
; /* in use */
129 static int caps_reserve_count
; /* unused, reserved */
130 static int caps_avail_count
; /* unused, unreserved */
132 void __init
ceph_caps_init(void)
134 INIT_LIST_HEAD(&caps_list
);
135 spin_lock_init(&caps_list_lock
);
138 void ceph_caps_finalize(void)
140 struct ceph_cap
*cap
;
142 spin_lock(&caps_list_lock
);
143 while (!list_empty(&caps_list
)) {
144 cap
= list_first_entry(&caps_list
, struct ceph_cap
, caps_item
);
145 list_del(&cap
->caps_item
);
146 kmem_cache_free(ceph_cap_cachep
, cap
);
148 caps_total_count
= 0;
149 caps_avail_count
= 0;
151 caps_reserve_count
= 0;
152 spin_unlock(&caps_list_lock
);
155 int ceph_reserve_caps(struct ceph_cap_reservation
*ctx
, int need
)
158 struct ceph_cap
*cap
;
164 dout("reserve caps ctx=%p need=%d\n", ctx
, need
);
166 /* first reserve any caps that are already allocated */
167 spin_lock(&caps_list_lock
);
168 if (caps_avail_count
>= need
)
171 have
= caps_avail_count
;
172 caps_avail_count
-= have
;
173 caps_reserve_count
+= have
;
174 BUG_ON(caps_total_count
!= caps_use_count
+ caps_reserve_count
+
176 spin_unlock(&caps_list_lock
);
178 for (i
= have
; i
< need
; i
++) {
179 cap
= kmem_cache_alloc(ceph_cap_cachep
, GFP_NOFS
);
182 goto out_alloc_count
;
184 list_add(&cap
->caps_item
, &newcaps
);
187 BUG_ON(have
+ alloc
!= need
);
189 spin_lock(&caps_list_lock
);
190 caps_total_count
+= alloc
;
191 caps_reserve_count
+= alloc
;
192 list_splice(&newcaps
, &caps_list
);
194 BUG_ON(caps_total_count
!= caps_use_count
+ caps_reserve_count
+
196 spin_unlock(&caps_list_lock
);
199 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
200 ctx
, caps_total_count
, caps_use_count
, caps_reserve_count
,
205 /* we didn't manage to reserve as much as we needed */
206 pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
211 int ceph_unreserve_caps(struct ceph_cap_reservation
*ctx
)
213 dout("unreserve caps ctx=%p count=%d\n", ctx
, ctx
->count
);
215 spin_lock(&caps_list_lock
);
216 BUG_ON(caps_reserve_count
< ctx
->count
);
217 caps_reserve_count
-= ctx
->count
;
218 caps_avail_count
+= ctx
->count
;
220 dout("unreserve caps %d = %d used + %d resv + %d avail\n",
221 caps_total_count
, caps_use_count
, caps_reserve_count
,
223 BUG_ON(caps_total_count
!= caps_use_count
+ caps_reserve_count
+
225 spin_unlock(&caps_list_lock
);
230 static struct ceph_cap
*get_cap(struct ceph_cap_reservation
*ctx
)
232 struct ceph_cap
*cap
= NULL
;
234 /* temporary, until we do something about cap import/export */
236 return kmem_cache_alloc(ceph_cap_cachep
, GFP_NOFS
);
238 spin_lock(&caps_list_lock
);
239 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
240 ctx
, ctx
->count
, caps_total_count
, caps_use_count
,
241 caps_reserve_count
, caps_avail_count
);
243 BUG_ON(ctx
->count
> caps_reserve_count
);
244 BUG_ON(list_empty(&caps_list
));
247 caps_reserve_count
--;
250 cap
= list_first_entry(&caps_list
, struct ceph_cap
, caps_item
);
251 list_del(&cap
->caps_item
);
253 BUG_ON(caps_total_count
!= caps_use_count
+ caps_reserve_count
+
255 spin_unlock(&caps_list_lock
);
259 static void put_cap(struct ceph_cap
*cap
,
260 struct ceph_cap_reservation
*ctx
)
262 spin_lock(&caps_list_lock
);
263 dout("put_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
264 ctx
, ctx
? ctx
->count
: 0, caps_total_count
, caps_use_count
,
265 caps_reserve_count
, caps_avail_count
);
268 * Keep some preallocated caps around, at least enough to do a
269 * readdir (which needs to preallocate lots of them), to avoid
270 * lots of free/alloc churn.
272 if (caps_avail_count
>= caps_reserve_count
+
273 ceph_client(cap
->ci
->vfs_inode
.i_sb
)->mount_args
->max_readdir
) {
275 kmem_cache_free(ceph_cap_cachep
, cap
);
279 caps_reserve_count
++;
283 list_add(&cap
->caps_item
, &caps_list
);
286 BUG_ON(caps_total_count
!= caps_use_count
+ caps_reserve_count
+
288 spin_unlock(&caps_list_lock
);
291 void ceph_reservation_status(struct ceph_client
*client
,
292 int *total
, int *avail
, int *used
, int *reserved
)
295 *total
= caps_total_count
;
297 *avail
= caps_avail_count
;
299 *used
= caps_use_count
;
301 *reserved
= caps_reserve_count
;
305 * Find ceph_cap for given mds, if any.
307 * Called with i_lock held.
309 static struct ceph_cap
*__get_cap_for_mds(struct ceph_inode_info
*ci
, int mds
)
311 struct ceph_cap
*cap
;
312 struct rb_node
*n
= ci
->i_caps
.rb_node
;
315 cap
= rb_entry(n
, struct ceph_cap
, ci_node
);
318 else if (mds
> cap
->mds
)
327 * Return id of any MDS with a cap, preferably FILE_WR|WRBUFFER|EXCL, else
330 static int __ceph_get_cap_mds(struct ceph_inode_info
*ci
, u32
*mseq
)
332 struct ceph_cap
*cap
;
336 /* prefer mds with WR|WRBUFFER|EXCL caps */
337 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
338 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
342 if (cap
->issued
& (CEPH_CAP_FILE_WR
|
343 CEPH_CAP_FILE_BUFFER
|
350 int ceph_get_cap_mds(struct inode
*inode
)
353 spin_lock(&inode
->i_lock
);
354 mds
= __ceph_get_cap_mds(ceph_inode(inode
), NULL
);
355 spin_unlock(&inode
->i_lock
);
360 * Called under i_lock.
362 static void __insert_cap_node(struct ceph_inode_info
*ci
,
363 struct ceph_cap
*new)
365 struct rb_node
**p
= &ci
->i_caps
.rb_node
;
366 struct rb_node
*parent
= NULL
;
367 struct ceph_cap
*cap
= NULL
;
371 cap
= rb_entry(parent
, struct ceph_cap
, ci_node
);
372 if (new->mds
< cap
->mds
)
374 else if (new->mds
> cap
->mds
)
380 rb_link_node(&new->ci_node
, parent
, p
);
381 rb_insert_color(&new->ci_node
, &ci
->i_caps
);
385 * (re)set cap hold timeouts, which control the delayed release
386 * of unused caps back to the MDS. Should be called on cap use.
388 static void __cap_set_timeouts(struct ceph_mds_client
*mdsc
,
389 struct ceph_inode_info
*ci
)
391 struct ceph_mount_args
*ma
= mdsc
->client
->mount_args
;
393 ci
->i_hold_caps_min
= round_jiffies(jiffies
+
394 ma
->caps_wanted_delay_min
* HZ
);
395 ci
->i_hold_caps_max
= round_jiffies(jiffies
+
396 ma
->caps_wanted_delay_max
* HZ
);
397 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci
->vfs_inode
,
398 ci
->i_hold_caps_min
- jiffies
, ci
->i_hold_caps_max
- jiffies
);
402 * (Re)queue cap at the end of the delayed cap release list.
404 * If I_FLUSH is set, leave the inode at the front of the list.
406 * Caller holds i_lock
407 * -> we take mdsc->cap_delay_lock
409 static void __cap_delay_requeue(struct ceph_mds_client
*mdsc
,
410 struct ceph_inode_info
*ci
)
412 __cap_set_timeouts(mdsc
, ci
);
413 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci
->vfs_inode
,
414 ci
->i_ceph_flags
, ci
->i_hold_caps_max
);
415 if (!mdsc
->stopping
) {
416 spin_lock(&mdsc
->cap_delay_lock
);
417 if (!list_empty(&ci
->i_cap_delay_list
)) {
418 if (ci
->i_ceph_flags
& CEPH_I_FLUSH
)
420 list_del_init(&ci
->i_cap_delay_list
);
422 list_add_tail(&ci
->i_cap_delay_list
, &mdsc
->cap_delay_list
);
424 spin_unlock(&mdsc
->cap_delay_lock
);
429 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
430 * indicating we should send a cap message to flush dirty metadata
431 * asap, and move to the front of the delayed cap list.
433 static void __cap_delay_requeue_front(struct ceph_mds_client
*mdsc
,
434 struct ceph_inode_info
*ci
)
436 dout("__cap_delay_requeue_front %p\n", &ci
->vfs_inode
);
437 spin_lock(&mdsc
->cap_delay_lock
);
438 ci
->i_ceph_flags
|= CEPH_I_FLUSH
;
439 if (!list_empty(&ci
->i_cap_delay_list
))
440 list_del_init(&ci
->i_cap_delay_list
);
441 list_add(&ci
->i_cap_delay_list
, &mdsc
->cap_delay_list
);
442 spin_unlock(&mdsc
->cap_delay_lock
);
446 * Cancel delayed work on cap.
448 * Caller must hold i_lock.
450 static void __cap_delay_cancel(struct ceph_mds_client
*mdsc
,
451 struct ceph_inode_info
*ci
)
453 dout("__cap_delay_cancel %p\n", &ci
->vfs_inode
);
454 if (list_empty(&ci
->i_cap_delay_list
))
456 spin_lock(&mdsc
->cap_delay_lock
);
457 list_del_init(&ci
->i_cap_delay_list
);
458 spin_unlock(&mdsc
->cap_delay_lock
);
462 * Common issue checks for add_cap, handle_cap_grant.
464 static void __check_cap_issue(struct ceph_inode_info
*ci
, struct ceph_cap
*cap
,
467 unsigned had
= __ceph_caps_issued(ci
, NULL
);
470 * Each time we receive FILE_CACHE anew, we increment
473 if ((issued
& CEPH_CAP_FILE_CACHE
) &&
474 (had
& CEPH_CAP_FILE_CACHE
) == 0)
478 * if we are newly issued FILE_SHARED, clear I_COMPLETE; we
479 * don't know what happened to this directory while we didn't
482 if ((issued
& CEPH_CAP_FILE_SHARED
) &&
483 (had
& CEPH_CAP_FILE_SHARED
) == 0) {
485 if (S_ISDIR(ci
->vfs_inode
.i_mode
)) {
486 dout(" marking %p NOT complete\n", &ci
->vfs_inode
);
487 ci
->i_ceph_flags
&= ~CEPH_I_COMPLETE
;
493 * Add a capability under the given MDS session.
495 * Caller should hold session snap_rwsem (read) and s_mutex.
497 * @fmode is the open file mode, if we are opening a file, otherwise
498 * it is < 0. (This is so we can atomically add the cap and add an
499 * open file reference to it.)
501 int ceph_add_cap(struct inode
*inode
,
502 struct ceph_mds_session
*session
, u64 cap_id
,
503 int fmode
, unsigned issued
, unsigned wanted
,
504 unsigned seq
, unsigned mseq
, u64 realmino
, int flags
,
505 struct ceph_cap_reservation
*caps_reservation
)
507 struct ceph_mds_client
*mdsc
= &ceph_inode_to_client(inode
)->mdsc
;
508 struct ceph_inode_info
*ci
= ceph_inode(inode
);
509 struct ceph_cap
*new_cap
= NULL
;
510 struct ceph_cap
*cap
;
511 int mds
= session
->s_mds
;
514 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode
,
515 session
->s_mds
, cap_id
, ceph_cap_string(issued
), seq
);
518 * If we are opening the file, include file mode wanted bits
522 wanted
|= ceph_caps_for_mode(fmode
);
525 spin_lock(&inode
->i_lock
);
526 cap
= __get_cap_for_mds(ci
, mds
);
532 spin_unlock(&inode
->i_lock
);
533 new_cap
= get_cap(caps_reservation
);
540 cap
->implemented
= 0;
545 __insert_cap_node(ci
, cap
);
547 /* clear out old exporting info? (i.e. on cap import) */
548 if (ci
->i_cap_exporting_mds
== mds
) {
549 ci
->i_cap_exporting_issued
= 0;
550 ci
->i_cap_exporting_mseq
= 0;
551 ci
->i_cap_exporting_mds
= -1;
554 /* add to session cap list */
555 cap
->session
= session
;
556 spin_lock(&session
->s_cap_lock
);
557 list_add_tail(&cap
->session_caps
, &session
->s_caps
);
558 session
->s_nr_caps
++;
559 spin_unlock(&session
->s_cap_lock
);
562 if (!ci
->i_snap_realm
) {
564 * add this inode to the appropriate snap realm
566 struct ceph_snap_realm
*realm
= ceph_lookup_snap_realm(mdsc
,
569 ceph_get_snap_realm(mdsc
, realm
);
570 spin_lock(&realm
->inodes_with_caps_lock
);
571 ci
->i_snap_realm
= realm
;
572 list_add(&ci
->i_snap_realm_item
,
573 &realm
->inodes_with_caps
);
574 spin_unlock(&realm
->inodes_with_caps_lock
);
576 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
581 __check_cap_issue(ci
, cap
, issued
);
584 * If we are issued caps we don't want, or the mds' wanted
585 * value appears to be off, queue a check so we'll release
586 * later and/or update the mds wanted value.
588 actual_wanted
= __ceph_caps_wanted(ci
);
589 if ((wanted
& ~actual_wanted
) ||
590 (issued
& ~actual_wanted
& CEPH_CAP_ANY_WR
)) {
591 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
592 ceph_cap_string(issued
), ceph_cap_string(wanted
),
593 ceph_cap_string(actual_wanted
));
594 __cap_delay_requeue(mdsc
, ci
);
597 if (flags
& CEPH_CAP_FLAG_AUTH
)
598 ci
->i_auth_cap
= cap
;
599 else if (ci
->i_auth_cap
== cap
)
600 ci
->i_auth_cap
= NULL
;
602 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
603 inode
, ceph_vinop(inode
), cap
, ceph_cap_string(issued
),
604 ceph_cap_string(issued
|cap
->issued
), seq
, mds
);
605 cap
->cap_id
= cap_id
;
606 cap
->issued
= issued
;
607 cap
->implemented
|= issued
;
608 cap
->mds_wanted
|= wanted
;
610 cap
->issue_seq
= seq
;
612 cap
->cap_gen
= session
->s_cap_gen
;
615 __ceph_get_fmode(ci
, fmode
);
616 spin_unlock(&inode
->i_lock
);
617 wake_up(&ci
->i_cap_wq
);
622 * Return true if cap has not timed out and belongs to the current
623 * generation of the MDS session (i.e. has not gone 'stale' due to
624 * us losing touch with the mds).
626 static int __cap_is_valid(struct ceph_cap
*cap
)
631 spin_lock(&cap
->session
->s_cap_lock
);
632 gen
= cap
->session
->s_cap_gen
;
633 ttl
= cap
->session
->s_cap_ttl
;
634 spin_unlock(&cap
->session
->s_cap_lock
);
636 if (cap
->cap_gen
< gen
|| time_after_eq(jiffies
, ttl
)) {
637 dout("__cap_is_valid %p cap %p issued %s "
638 "but STALE (gen %u vs %u)\n", &cap
->ci
->vfs_inode
,
639 cap
, ceph_cap_string(cap
->issued
), cap
->cap_gen
, gen
);
647 * Return set of valid cap bits issued to us. Note that caps time
648 * out, and may be invalidated in bulk if the client session times out
649 * and session->s_cap_gen is bumped.
651 int __ceph_caps_issued(struct ceph_inode_info
*ci
, int *implemented
)
653 int have
= ci
->i_snap_caps
;
654 struct ceph_cap
*cap
;
659 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
660 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
661 if (!__cap_is_valid(cap
))
663 dout("__ceph_caps_issued %p cap %p issued %s\n",
664 &ci
->vfs_inode
, cap
, ceph_cap_string(cap
->issued
));
667 *implemented
|= cap
->implemented
;
673 * Get cap bits issued by caps other than @ocap
675 int __ceph_caps_issued_other(struct ceph_inode_info
*ci
, struct ceph_cap
*ocap
)
677 int have
= ci
->i_snap_caps
;
678 struct ceph_cap
*cap
;
681 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
682 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
685 if (!__cap_is_valid(cap
))
693 * Move a cap to the end of the LRU (oldest caps at list head, newest
696 static void __touch_cap(struct ceph_cap
*cap
)
698 struct ceph_mds_session
*s
= cap
->session
;
700 spin_lock(&s
->s_cap_lock
);
701 if (!s
->s_iterating_caps
) {
702 dout("__touch_cap %p cap %p mds%d\n", &cap
->ci
->vfs_inode
, cap
,
704 list_move_tail(&cap
->session_caps
, &s
->s_caps
);
706 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
707 &cap
->ci
->vfs_inode
, cap
, s
->s_mds
);
709 spin_unlock(&s
->s_cap_lock
);
713 * Check if we hold the given mask. If so, move the cap(s) to the
714 * front of their respective LRUs. (This is the preferred way for
715 * callers to check for caps they want.)
717 int __ceph_caps_issued_mask(struct ceph_inode_info
*ci
, int mask
, int touch
)
719 struct ceph_cap
*cap
;
721 int have
= ci
->i_snap_caps
;
723 if ((have
& mask
) == mask
) {
724 dout("__ceph_caps_issued_mask %p snap issued %s"
725 " (mask %s)\n", &ci
->vfs_inode
,
726 ceph_cap_string(have
),
727 ceph_cap_string(mask
));
731 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
732 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
733 if (!__cap_is_valid(cap
))
735 if ((cap
->issued
& mask
) == mask
) {
736 dout("__ceph_caps_issued_mask %p cap %p issued %s"
737 " (mask %s)\n", &ci
->vfs_inode
, cap
,
738 ceph_cap_string(cap
->issued
),
739 ceph_cap_string(mask
));
745 /* does a combination of caps satisfy mask? */
747 if ((have
& mask
) == mask
) {
748 dout("__ceph_caps_issued_mask %p combo issued %s"
749 " (mask %s)\n", &ci
->vfs_inode
,
750 ceph_cap_string(cap
->issued
),
751 ceph_cap_string(mask
));
755 /* touch this + preceeding caps */
757 for (q
= rb_first(&ci
->i_caps
); q
!= p
;
759 cap
= rb_entry(q
, struct ceph_cap
,
761 if (!__cap_is_valid(cap
))
774 * Return true if mask caps are currently being revoked by an MDS.
776 int ceph_caps_revoking(struct ceph_inode_info
*ci
, int mask
)
778 struct inode
*inode
= &ci
->vfs_inode
;
779 struct ceph_cap
*cap
;
783 spin_lock(&inode
->i_lock
);
784 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
785 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
786 if (__cap_is_valid(cap
) &&
787 (cap
->implemented
& ~cap
->issued
& mask
)) {
792 spin_unlock(&inode
->i_lock
);
793 dout("ceph_caps_revoking %p %s = %d\n", inode
,
794 ceph_cap_string(mask
), ret
);
798 int __ceph_caps_used(struct ceph_inode_info
*ci
)
802 used
|= CEPH_CAP_PIN
;
804 used
|= CEPH_CAP_FILE_RD
;
805 if (ci
->i_rdcache_ref
|| ci
->i_rdcache_gen
)
806 used
|= CEPH_CAP_FILE_CACHE
;
808 used
|= CEPH_CAP_FILE_WR
;
809 if (ci
->i_wrbuffer_ref
)
810 used
|= CEPH_CAP_FILE_BUFFER
;
815 * wanted, by virtue of open file modes
817 int __ceph_caps_file_wanted(struct ceph_inode_info
*ci
)
821 for (mode
= 0; mode
< 4; mode
++)
822 if (ci
->i_nr_by_mode
[mode
])
823 want
|= ceph_caps_for_mode(mode
);
828 * Return caps we have registered with the MDS(s) as 'wanted'.
830 int __ceph_caps_mds_wanted(struct ceph_inode_info
*ci
)
832 struct ceph_cap
*cap
;
836 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
837 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
838 if (!__cap_is_valid(cap
))
840 mds_wanted
|= cap
->mds_wanted
;
846 * called under i_lock
848 static int __ceph_is_any_caps(struct ceph_inode_info
*ci
)
850 return !RB_EMPTY_ROOT(&ci
->i_caps
) || ci
->i_cap_exporting_mds
>= 0;
854 * caller should hold i_lock, and session s_mutex.
855 * returns true if this is the last cap. if so, caller should iput.
857 void __ceph_remove_cap(struct ceph_cap
*cap
,
858 struct ceph_cap_reservation
*ctx
)
860 struct ceph_mds_session
*session
= cap
->session
;
861 struct ceph_inode_info
*ci
= cap
->ci
;
862 struct ceph_mds_client
*mdsc
= &ceph_client(ci
->vfs_inode
.i_sb
)->mdsc
;
864 dout("__ceph_remove_cap %p from %p\n", cap
, &ci
->vfs_inode
);
866 /* remove from session list */
867 spin_lock(&session
->s_cap_lock
);
868 list_del_init(&cap
->session_caps
);
869 session
->s_nr_caps
--;
870 spin_unlock(&session
->s_cap_lock
);
872 /* remove from inode list */
873 rb_erase(&cap
->ci_node
, &ci
->i_caps
);
875 if (ci
->i_auth_cap
== cap
)
876 ci
->i_auth_cap
= NULL
;
880 if (!__ceph_is_any_caps(ci
) && ci
->i_snap_realm
) {
881 struct ceph_snap_realm
*realm
= ci
->i_snap_realm
;
882 spin_lock(&realm
->inodes_with_caps_lock
);
883 list_del_init(&ci
->i_snap_realm_item
);
884 ci
->i_snap_realm_counter
++;
885 ci
->i_snap_realm
= NULL
;
886 spin_unlock(&realm
->inodes_with_caps_lock
);
887 ceph_put_snap_realm(mdsc
, realm
);
889 if (!__ceph_is_any_real_caps(ci
))
890 __cap_delay_cancel(mdsc
, ci
);
894 * Build and send a cap message to the given MDS.
896 * Caller should be holding s_mutex.
898 static int send_cap_msg(struct ceph_mds_session
*session
,
899 u64 ino
, u64 cid
, int op
,
900 int caps
, int wanted
, int dirty
,
901 u32 seq
, u64 flush_tid
, u32 issue_seq
, u32 mseq
,
902 u64 size
, u64 max_size
,
903 struct timespec
*mtime
, struct timespec
*atime
,
905 uid_t uid
, gid_t gid
, mode_t mode
,
907 struct ceph_buffer
*xattrs_buf
,
910 struct ceph_mds_caps
*fc
;
911 struct ceph_msg
*msg
;
913 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
914 " seq %u/%u mseq %u follows %lld size %llu/%llu"
915 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op
),
916 cid
, ino
, ceph_cap_string(caps
), ceph_cap_string(wanted
),
917 ceph_cap_string(dirty
),
918 seq
, issue_seq
, mseq
, follows
, size
, max_size
,
919 xattr_version
, xattrs_buf
? (int)xattrs_buf
->vec
.iov_len
: 0);
921 msg
= ceph_msg_new(CEPH_MSG_CLIENT_CAPS
, sizeof(*fc
), 0, 0, NULL
);
925 msg
->hdr
.tid
= cpu_to_le64(flush_tid
);
927 fc
= msg
->front
.iov_base
;
928 memset(fc
, 0, sizeof(*fc
));
930 fc
->cap_id
= cpu_to_le64(cid
);
931 fc
->op
= cpu_to_le32(op
);
932 fc
->seq
= cpu_to_le32(seq
);
933 fc
->issue_seq
= cpu_to_le32(issue_seq
);
934 fc
->migrate_seq
= cpu_to_le32(mseq
);
935 fc
->caps
= cpu_to_le32(caps
);
936 fc
->wanted
= cpu_to_le32(wanted
);
937 fc
->dirty
= cpu_to_le32(dirty
);
938 fc
->ino
= cpu_to_le64(ino
);
939 fc
->snap_follows
= cpu_to_le64(follows
);
941 fc
->size
= cpu_to_le64(size
);
942 fc
->max_size
= cpu_to_le64(max_size
);
944 ceph_encode_timespec(&fc
->mtime
, mtime
);
946 ceph_encode_timespec(&fc
->atime
, atime
);
947 fc
->time_warp_seq
= cpu_to_le32(time_warp_seq
);
949 fc
->uid
= cpu_to_le32(uid
);
950 fc
->gid
= cpu_to_le32(gid
);
951 fc
->mode
= cpu_to_le32(mode
);
953 fc
->xattr_version
= cpu_to_le64(xattr_version
);
955 msg
->middle
= ceph_buffer_get(xattrs_buf
);
956 fc
->xattr_len
= cpu_to_le32(xattrs_buf
->vec
.iov_len
);
957 msg
->hdr
.middle_len
= cpu_to_le32(xattrs_buf
->vec
.iov_len
);
960 ceph_con_send(&session
->s_con
, msg
);
965 * Queue cap releases when an inode is dropped from our
968 void ceph_queue_caps_release(struct inode
*inode
)
970 struct ceph_inode_info
*ci
= ceph_inode(inode
);
973 spin_lock(&inode
->i_lock
);
974 p
= rb_first(&ci
->i_caps
);
976 struct ceph_cap
*cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
977 struct ceph_mds_session
*session
= cap
->session
;
978 struct ceph_msg
*msg
;
979 struct ceph_mds_cap_release
*head
;
980 struct ceph_mds_cap_item
*item
;
982 spin_lock(&session
->s_cap_lock
);
983 BUG_ON(!session
->s_num_cap_releases
);
984 msg
= list_first_entry(&session
->s_cap_releases
,
985 struct ceph_msg
, list_head
);
987 dout(" adding %p release to mds%d msg %p (%d left)\n",
988 inode
, session
->s_mds
, msg
, session
->s_num_cap_releases
);
990 BUG_ON(msg
->front
.iov_len
+ sizeof(*item
) > PAGE_CACHE_SIZE
);
991 head
= msg
->front
.iov_base
;
992 head
->num
= cpu_to_le32(le32_to_cpu(head
->num
) + 1);
993 item
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
994 item
->ino
= cpu_to_le64(ceph_ino(inode
));
995 item
->cap_id
= cpu_to_le64(cap
->cap_id
);
996 item
->migrate_seq
= cpu_to_le32(cap
->mseq
);
997 item
->seq
= cpu_to_le32(cap
->issue_seq
);
999 session
->s_num_cap_releases
--;
1001 msg
->front
.iov_len
+= sizeof(*item
);
1002 if (le32_to_cpu(head
->num
) == CEPH_CAPS_PER_RELEASE
) {
1003 dout(" release msg %p full\n", msg
);
1004 list_move_tail(&msg
->list_head
,
1005 &session
->s_cap_releases_done
);
1007 dout(" release msg %p at %d/%d (%d)\n", msg
,
1008 (int)le32_to_cpu(head
->num
),
1009 (int)CEPH_CAPS_PER_RELEASE
,
1010 (int)msg
->front
.iov_len
);
1012 spin_unlock(&session
->s_cap_lock
);
1014 __ceph_remove_cap(cap
, NULL
);
1017 spin_unlock(&inode
->i_lock
);
1021 * Send a cap msg on the given inode. Update our caps state, then
1022 * drop i_lock and send the message.
1024 * Make note of max_size reported/requested from mds, revoked caps
1025 * that have now been implemented.
1027 * Make half-hearted attempt ot to invalidate page cache if we are
1028 * dropping RDCACHE. Note that this will leave behind locked pages
1029 * that we'll then need to deal with elsewhere.
1031 * Return non-zero if delayed release, or we experienced an error
1032 * such that the caller should requeue + retry later.
1034 * called with i_lock, then drops it.
1035 * caller should hold snap_rwsem (read), s_mutex.
1037 static int __send_cap(struct ceph_mds_client
*mdsc
, struct ceph_cap
*cap
,
1038 int op
, int used
, int want
, int retain
, int flushing
,
1039 unsigned *pflush_tid
)
1040 __releases(cap
->ci
->vfs_inode
->i_lock
)
1042 struct ceph_inode_info
*ci
= cap
->ci
;
1043 struct inode
*inode
= &ci
->vfs_inode
;
1044 u64 cap_id
= cap
->cap_id
;
1045 int held
, revoking
, dropping
, keep
;
1046 u64 seq
, issue_seq
, mseq
, time_warp_seq
, follows
;
1048 struct timespec mtime
, atime
;
1053 struct ceph_mds_session
*session
;
1054 u64 xattr_version
= 0;
1060 held
= cap
->issued
| cap
->implemented
;
1061 revoking
= cap
->implemented
& ~cap
->issued
;
1062 retain
&= ~revoking
;
1063 dropping
= cap
->issued
& ~retain
;
1065 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1066 inode
, cap
, cap
->session
,
1067 ceph_cap_string(held
), ceph_cap_string(held
& retain
),
1068 ceph_cap_string(revoking
));
1069 BUG_ON((retain
& CEPH_CAP_PIN
) == 0);
1071 session
= cap
->session
;
1073 /* don't release wanted unless we've waited a bit. */
1074 if ((ci
->i_ceph_flags
& CEPH_I_NODELAY
) == 0 &&
1075 time_before(jiffies
, ci
->i_hold_caps_min
)) {
1076 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1077 ceph_cap_string(cap
->issued
),
1078 ceph_cap_string(cap
->issued
& retain
),
1079 ceph_cap_string(cap
->mds_wanted
),
1080 ceph_cap_string(want
));
1081 want
|= cap
->mds_wanted
;
1082 retain
|= cap
->issued
;
1085 ci
->i_ceph_flags
&= ~(CEPH_I_NODELAY
| CEPH_I_FLUSH
);
1087 cap
->issued
&= retain
; /* drop bits we don't want */
1088 if (cap
->implemented
& ~cap
->issued
) {
1090 * Wake up any waiters on wanted -> needed transition.
1091 * This is due to the weird transition from buffered
1092 * to sync IO... we need to flush dirty pages _before_
1093 * allowing sync writes to avoid reordering.
1097 cap
->implemented
&= cap
->issued
| used
;
1098 cap
->mds_wanted
= want
;
1102 * assign a tid for flush operations so we can avoid
1103 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1104 * clean type races. track latest tid for every bit
1105 * so we can handle flush AxFw, flush Fw, and have the
1106 * first ack clean Ax.
1108 flush_tid
= ++ci
->i_cap_flush_last_tid
;
1110 *pflush_tid
= flush_tid
;
1111 dout(" cap_flush_tid %d\n", (int)flush_tid
);
1112 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
1113 if (flushing
& (1 << i
))
1114 ci
->i_cap_flush_tid
[i
] = flush_tid
;
1117 keep
= cap
->implemented
;
1119 issue_seq
= cap
->issue_seq
;
1121 size
= inode
->i_size
;
1122 ci
->i_reported_size
= size
;
1123 max_size
= ci
->i_wanted_max_size
;
1124 ci
->i_requested_max_size
= max_size
;
1125 mtime
= inode
->i_mtime
;
1126 atime
= inode
->i_atime
;
1127 time_warp_seq
= ci
->i_time_warp_seq
;
1128 follows
= ci
->i_snap_realm
->cached_context
->seq
;
1131 mode
= inode
->i_mode
;
1133 if (dropping
& CEPH_CAP_XATTR_EXCL
) {
1134 __ceph_build_xattrs_blob(ci
);
1135 xattr_version
= ci
->i_xattrs
.version
+ 1;
1138 spin_unlock(&inode
->i_lock
);
1140 ret
= send_cap_msg(session
, ceph_vino(inode
).ino
, cap_id
,
1141 op
, keep
, want
, flushing
, seq
, flush_tid
, issue_seq
, mseq
,
1142 size
, max_size
, &mtime
, &atime
, time_warp_seq
,
1145 (flushing
& CEPH_CAP_XATTR_EXCL
) ? ci
->i_xattrs
.blob
: NULL
,
1148 dout("error sending cap msg, must requeue %p\n", inode
);
1153 wake_up(&ci
->i_cap_wq
);
1159 * When a snapshot is taken, clients accumulate dirty metadata on
1160 * inodes with capabilities in ceph_cap_snaps to describe the file
1161 * state at the time the snapshot was taken. This must be flushed
1162 * asynchronously back to the MDS once sync writes complete and dirty
1163 * data is written out.
1165 * Called under i_lock. Takes s_mutex as needed.
1167 void __ceph_flush_snaps(struct ceph_inode_info
*ci
,
1168 struct ceph_mds_session
**psession
)
1170 struct inode
*inode
= &ci
->vfs_inode
;
1172 struct ceph_cap_snap
*capsnap
;
1174 struct ceph_mds_client
*mdsc
= &ceph_inode_to_client(inode
)->mdsc
;
1175 struct ceph_mds_session
*session
= NULL
; /* if session != NULL, we hold
1177 u64 next_follows
= 0; /* keep track of how far we've gotten through the
1178 i_cap_snaps list, and skip these entries next time
1179 around to avoid an infinite loop */
1182 session
= *psession
;
1184 dout("__flush_snaps %p\n", inode
);
1186 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
1187 /* avoid an infiniute loop after retry */
1188 if (capsnap
->follows
< next_follows
)
1191 * we need to wait for sync writes to complete and for dirty
1192 * pages to be written out.
1194 if (capsnap
->dirty_pages
|| capsnap
->writing
)
1197 /* pick mds, take s_mutex */
1198 mds
= __ceph_get_cap_mds(ci
, &mseq
);
1199 if (session
&& session
->s_mds
!= mds
) {
1200 dout("oops, wrong session %p mutex\n", session
);
1201 mutex_unlock(&session
->s_mutex
);
1202 ceph_put_mds_session(session
);
1206 spin_unlock(&inode
->i_lock
);
1207 mutex_lock(&mdsc
->mutex
);
1208 session
= __ceph_lookup_mds_session(mdsc
, mds
);
1209 mutex_unlock(&mdsc
->mutex
);
1211 dout("inverting session/ino locks on %p\n",
1213 mutex_lock(&session
->s_mutex
);
1216 * if session == NULL, we raced against a cap
1217 * deletion. retry, and we'll get a better
1218 * @mds value next time.
1220 spin_lock(&inode
->i_lock
);
1224 capsnap
->flush_tid
= ++ci
->i_cap_flush_last_tid
;
1225 atomic_inc(&capsnap
->nref
);
1226 if (!list_empty(&capsnap
->flushing_item
))
1227 list_del_init(&capsnap
->flushing_item
);
1228 list_add_tail(&capsnap
->flushing_item
,
1229 &session
->s_cap_snaps_flushing
);
1230 spin_unlock(&inode
->i_lock
);
1232 dout("flush_snaps %p cap_snap %p follows %lld size %llu\n",
1233 inode
, capsnap
, next_follows
, capsnap
->size
);
1234 send_cap_msg(session
, ceph_vino(inode
).ino
, 0,
1235 CEPH_CAP_OP_FLUSHSNAP
, capsnap
->issued
, 0,
1236 capsnap
->dirty
, 0, capsnap
->flush_tid
, 0, mseq
,
1238 &capsnap
->mtime
, &capsnap
->atime
,
1239 capsnap
->time_warp_seq
,
1240 capsnap
->uid
, capsnap
->gid
, capsnap
->mode
,
1244 next_follows
= capsnap
->follows
+ 1;
1245 ceph_put_cap_snap(capsnap
);
1247 spin_lock(&inode
->i_lock
);
1251 /* we flushed them all; remove this inode from the queue */
1252 spin_lock(&mdsc
->snap_flush_lock
);
1253 list_del_init(&ci
->i_snap_flush_item
);
1254 spin_unlock(&mdsc
->snap_flush_lock
);
1257 *psession
= session
;
1259 mutex_unlock(&session
->s_mutex
);
1260 ceph_put_mds_session(session
);
1264 static void ceph_flush_snaps(struct ceph_inode_info
*ci
)
1266 struct inode
*inode
= &ci
->vfs_inode
;
1268 spin_lock(&inode
->i_lock
);
1269 __ceph_flush_snaps(ci
, NULL
);
1270 spin_unlock(&inode
->i_lock
);
1274 * Mark caps dirty. If inode is newly dirty, add to the global dirty
1277 void __ceph_mark_dirty_caps(struct ceph_inode_info
*ci
, int mask
)
1279 struct ceph_mds_client
*mdsc
= &ceph_client(ci
->vfs_inode
.i_sb
)->mdsc
;
1280 struct inode
*inode
= &ci
->vfs_inode
;
1281 int was
= ci
->i_dirty_caps
;
1284 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci
->vfs_inode
,
1285 ceph_cap_string(mask
), ceph_cap_string(was
),
1286 ceph_cap_string(was
| mask
));
1287 ci
->i_dirty_caps
|= mask
;
1289 dout(" inode %p now dirty\n", &ci
->vfs_inode
);
1290 BUG_ON(!list_empty(&ci
->i_dirty_item
));
1291 spin_lock(&mdsc
->cap_dirty_lock
);
1292 list_add(&ci
->i_dirty_item
, &mdsc
->cap_dirty
);
1293 spin_unlock(&mdsc
->cap_dirty_lock
);
1294 if (ci
->i_flushing_caps
== 0) {
1296 dirty
|= I_DIRTY_SYNC
;
1299 BUG_ON(list_empty(&ci
->i_dirty_item
));
1300 if (((was
| ci
->i_flushing_caps
) & CEPH_CAP_FILE_BUFFER
) &&
1301 (mask
& CEPH_CAP_FILE_BUFFER
))
1302 dirty
|= I_DIRTY_DATASYNC
;
1304 __mark_inode_dirty(inode
, dirty
);
1305 __cap_delay_requeue(mdsc
, ci
);
1309 * Add dirty inode to the flushing list. Assigned a seq number so we
1310 * can wait for caps to flush without starving.
1312 * Called under i_lock.
1314 static int __mark_caps_flushing(struct inode
*inode
,
1315 struct ceph_mds_session
*session
)
1317 struct ceph_mds_client
*mdsc
= &ceph_client(inode
->i_sb
)->mdsc
;
1318 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1321 BUG_ON(ci
->i_dirty_caps
== 0);
1322 BUG_ON(list_empty(&ci
->i_dirty_item
));
1324 flushing
= ci
->i_dirty_caps
;
1325 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1326 ceph_cap_string(flushing
),
1327 ceph_cap_string(ci
->i_flushing_caps
),
1328 ceph_cap_string(ci
->i_flushing_caps
| flushing
));
1329 ci
->i_flushing_caps
|= flushing
;
1330 ci
->i_dirty_caps
= 0;
1331 dout(" inode %p now !dirty\n", inode
);
1333 spin_lock(&mdsc
->cap_dirty_lock
);
1334 list_del_init(&ci
->i_dirty_item
);
1336 ci
->i_cap_flush_seq
= ++mdsc
->cap_flush_seq
;
1337 if (list_empty(&ci
->i_flushing_item
)) {
1338 list_add_tail(&ci
->i_flushing_item
, &session
->s_cap_flushing
);
1339 mdsc
->num_cap_flushing
++;
1340 dout(" inode %p now flushing seq %lld\n", inode
,
1341 ci
->i_cap_flush_seq
);
1343 list_move_tail(&ci
->i_flushing_item
, &session
->s_cap_flushing
);
1344 dout(" inode %p now flushing (more) seq %lld\n", inode
,
1345 ci
->i_cap_flush_seq
);
1347 spin_unlock(&mdsc
->cap_dirty_lock
);
1353 * Swiss army knife function to examine currently used and wanted
1354 * versus held caps. Release, flush, ack revoked caps to mds as
1357 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1358 * cap release further.
1359 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1360 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1363 void ceph_check_caps(struct ceph_inode_info
*ci
, int flags
,
1364 struct ceph_mds_session
*session
)
1366 struct ceph_client
*client
= ceph_inode_to_client(&ci
->vfs_inode
);
1367 struct ceph_mds_client
*mdsc
= &client
->mdsc
;
1368 struct inode
*inode
= &ci
->vfs_inode
;
1369 struct ceph_cap
*cap
;
1370 int file_wanted
, used
;
1371 int took_snap_rwsem
= 0; /* true if mdsc->snap_rwsem held */
1372 int drop_session_lock
= session
? 0 : 1;
1373 int issued
, implemented
, want
, retain
, revoking
, flushing
= 0;
1374 int mds
= -1; /* keep track of how far we've gone through i_caps list
1375 to avoid an infinite loop on retry */
1377 int tried_invalidate
= 0;
1378 int delayed
= 0, sent
= 0, force_requeue
= 0, num
;
1379 int queue_invalidate
= 0;
1380 int is_delayed
= flags
& CHECK_CAPS_NODELAY
;
1382 /* if we are unmounting, flush any unused caps immediately. */
1386 spin_lock(&inode
->i_lock
);
1388 if (ci
->i_ceph_flags
& CEPH_I_FLUSH
)
1389 flags
|= CHECK_CAPS_FLUSH
;
1391 /* flush snaps first time around only */
1392 if (!list_empty(&ci
->i_cap_snaps
))
1393 __ceph_flush_snaps(ci
, &session
);
1396 spin_lock(&inode
->i_lock
);
1398 file_wanted
= __ceph_caps_file_wanted(ci
);
1399 used
= __ceph_caps_used(ci
);
1400 want
= file_wanted
| used
;
1401 issued
= __ceph_caps_issued(ci
, &implemented
);
1402 revoking
= implemented
& ~issued
;
1404 retain
= want
| CEPH_CAP_PIN
;
1405 if (!mdsc
->stopping
&& inode
->i_nlink
> 0) {
1407 retain
|= CEPH_CAP_ANY
; /* be greedy */
1409 retain
|= CEPH_CAP_ANY_SHARED
;
1411 * keep RD only if we didn't have the file open RW,
1412 * because then the mds would revoke it anyway to
1413 * journal max_size=0.
1415 if (ci
->i_max_size
== 0)
1416 retain
|= CEPH_CAP_ANY_RD
;
1420 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1421 " issued %s revoking %s retain %s %s%s%s\n", inode
,
1422 ceph_cap_string(file_wanted
),
1423 ceph_cap_string(used
), ceph_cap_string(ci
->i_dirty_caps
),
1424 ceph_cap_string(ci
->i_flushing_caps
),
1425 ceph_cap_string(issued
), ceph_cap_string(revoking
),
1426 ceph_cap_string(retain
),
1427 (flags
& CHECK_CAPS_AUTHONLY
) ? " AUTHONLY" : "",
1428 (flags
& CHECK_CAPS_NODELAY
) ? " NODELAY" : "",
1429 (flags
& CHECK_CAPS_FLUSH
) ? " FLUSH" : "");
1432 * If we no longer need to hold onto old our caps, and we may
1433 * have cached pages, but don't want them, then try to invalidate.
1434 * If we fail, it's because pages are locked.... try again later.
1436 if ((!is_delayed
|| mdsc
->stopping
) &&
1437 ci
->i_wrbuffer_ref
== 0 && /* no dirty pages... */
1438 ci
->i_rdcache_gen
&& /* may have cached pages */
1439 (file_wanted
== 0 || /* no open files */
1440 (revoking
& CEPH_CAP_FILE_CACHE
)) && /* or revoking cache */
1441 !tried_invalidate
) {
1442 u32 invalidating_gen
= ci
->i_rdcache_gen
;
1445 dout("check_caps trying to invalidate on %p\n", inode
);
1446 spin_unlock(&inode
->i_lock
);
1447 ret
= invalidate_mapping_pages(&inode
->i_data
, 0, -1);
1448 spin_lock(&inode
->i_lock
);
1449 if (ret
== 0 && invalidating_gen
== ci
->i_rdcache_gen
) {
1451 ci
->i_rdcache_gen
= 0;
1452 ci
->i_rdcache_revoking
= 0;
1453 } else if (revoking
& CEPH_CAP_FILE_CACHE
) {
1454 dout("check_caps queuing invalidate\n");
1455 queue_invalidate
= 1;
1456 ci
->i_rdcache_revoking
= ci
->i_rdcache_gen
;
1458 dout("check_caps failed to invalidate pages\n");
1459 /* we failed to invalidate pages. check these
1460 caps again later. */
1462 __cap_set_timeouts(mdsc
, ci
);
1464 tried_invalidate
= 1;
1469 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
1470 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
1473 /* avoid looping forever */
1474 if (mds
>= cap
->mds
||
1475 ((flags
& CHECK_CAPS_AUTHONLY
) && cap
!= ci
->i_auth_cap
))
1478 /* NOTE: no side-effects allowed, until we take s_mutex */
1480 revoking
= cap
->implemented
& ~cap
->issued
;
1482 dout(" mds%d revoking %s\n", cap
->mds
,
1483 ceph_cap_string(revoking
));
1485 if (cap
== ci
->i_auth_cap
&&
1486 (cap
->issued
& CEPH_CAP_FILE_WR
)) {
1487 /* request larger max_size from MDS? */
1488 if (ci
->i_wanted_max_size
> ci
->i_max_size
&&
1489 ci
->i_wanted_max_size
> ci
->i_requested_max_size
) {
1490 dout("requesting new max_size\n");
1494 /* approaching file_max? */
1495 if ((inode
->i_size
<< 1) >= ci
->i_max_size
&&
1496 (ci
->i_reported_size
<< 1) < ci
->i_max_size
) {
1497 dout("i_size approaching max_size\n");
1501 /* flush anything dirty? */
1502 if (cap
== ci
->i_auth_cap
&& (flags
& CHECK_CAPS_FLUSH
) &&
1504 dout("flushing dirty caps\n");
1508 /* completed revocation? going down and there are no caps? */
1509 if (revoking
&& (revoking
& used
) == 0) {
1510 dout("completed revocation of %s\n",
1511 ceph_cap_string(cap
->implemented
& ~cap
->issued
));
1515 /* want more caps from mds? */
1516 if (want
& ~(cap
->mds_wanted
| cap
->issued
))
1519 /* things we might delay */
1520 if ((cap
->issued
& ~retain
) == 0 &&
1521 cap
->mds_wanted
== want
)
1522 continue; /* nope, all good */
1528 if ((ci
->i_ceph_flags
& CEPH_I_NODELAY
) == 0 &&
1529 time_before(jiffies
, ci
->i_hold_caps_max
)) {
1530 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1531 ceph_cap_string(cap
->issued
),
1532 ceph_cap_string(cap
->issued
& retain
),
1533 ceph_cap_string(cap
->mds_wanted
),
1534 ceph_cap_string(want
));
1540 if (session
&& session
!= cap
->session
) {
1541 dout("oops, wrong session %p mutex\n", session
);
1542 mutex_unlock(&session
->s_mutex
);
1546 session
= cap
->session
;
1547 if (mutex_trylock(&session
->s_mutex
) == 0) {
1548 dout("inverting session/ino locks on %p\n",
1550 spin_unlock(&inode
->i_lock
);
1551 if (took_snap_rwsem
) {
1552 up_read(&mdsc
->snap_rwsem
);
1553 took_snap_rwsem
= 0;
1555 mutex_lock(&session
->s_mutex
);
1559 /* take snap_rwsem after session mutex */
1560 if (!took_snap_rwsem
) {
1561 if (down_read_trylock(&mdsc
->snap_rwsem
) == 0) {
1562 dout("inverting snap/in locks on %p\n",
1564 spin_unlock(&inode
->i_lock
);
1565 down_read(&mdsc
->snap_rwsem
);
1566 took_snap_rwsem
= 1;
1569 took_snap_rwsem
= 1;
1572 if (cap
== ci
->i_auth_cap
&& ci
->i_dirty_caps
)
1573 flushing
= __mark_caps_flushing(inode
, session
);
1575 mds
= cap
->mds
; /* remember mds, so we don't repeat */
1578 /* __send_cap drops i_lock */
1579 delayed
+= __send_cap(mdsc
, cap
, CEPH_CAP_OP_UPDATE
, used
, want
,
1580 retain
, flushing
, NULL
);
1581 goto retry
; /* retake i_lock and restart our cap scan. */
1585 * Reschedule delayed caps release if we delayed anything,
1588 if (delayed
&& is_delayed
)
1589 force_requeue
= 1; /* __send_cap delayed release; requeue */
1590 if (!delayed
&& !is_delayed
)
1591 __cap_delay_cancel(mdsc
, ci
);
1592 else if (!is_delayed
|| force_requeue
)
1593 __cap_delay_requeue(mdsc
, ci
);
1595 spin_unlock(&inode
->i_lock
);
1597 if (queue_invalidate
)
1598 ceph_queue_invalidate(inode
);
1600 if (session
&& drop_session_lock
)
1601 mutex_unlock(&session
->s_mutex
);
1602 if (took_snap_rwsem
)
1603 up_read(&mdsc
->snap_rwsem
);
1607 * Try to flush dirty caps back to the auth mds.
1609 static int try_flush_caps(struct inode
*inode
, struct ceph_mds_session
*session
,
1610 unsigned *flush_tid
)
1612 struct ceph_mds_client
*mdsc
= &ceph_client(inode
->i_sb
)->mdsc
;
1613 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1614 int unlock_session
= session
? 0 : 1;
1618 spin_lock(&inode
->i_lock
);
1619 if (ci
->i_dirty_caps
&& ci
->i_auth_cap
) {
1620 struct ceph_cap
*cap
= ci
->i_auth_cap
;
1621 int used
= __ceph_caps_used(ci
);
1622 int want
= __ceph_caps_wanted(ci
);
1626 spin_unlock(&inode
->i_lock
);
1627 session
= cap
->session
;
1628 mutex_lock(&session
->s_mutex
);
1631 BUG_ON(session
!= cap
->session
);
1632 if (cap
->session
->s_state
< CEPH_MDS_SESSION_OPEN
)
1635 flushing
= __mark_caps_flushing(inode
, session
);
1637 /* __send_cap drops i_lock */
1638 delayed
= __send_cap(mdsc
, cap
, CEPH_CAP_OP_FLUSH
, used
, want
,
1639 cap
->issued
| cap
->implemented
, flushing
,
1644 spin_lock(&inode
->i_lock
);
1645 __cap_delay_requeue(mdsc
, ci
);
1648 spin_unlock(&inode
->i_lock
);
1650 if (session
&& unlock_session
)
1651 mutex_unlock(&session
->s_mutex
);
1656 * Return true if we've flushed caps through the given flush_tid.
1658 static int caps_are_flushed(struct inode
*inode
, unsigned tid
)
1660 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1661 int dirty
, i
, ret
= 1;
1663 spin_lock(&inode
->i_lock
);
1664 dirty
= __ceph_caps_dirty(ci
);
1665 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
1666 if ((ci
->i_flushing_caps
& (1 << i
)) &&
1667 ci
->i_cap_flush_tid
[i
] <= tid
) {
1668 /* still flushing this bit */
1672 spin_unlock(&inode
->i_lock
);
1677 * Wait on any unsafe replies for the given inode. First wait on the
1678 * newest request, and make that the upper bound. Then, if there are
1679 * more requests, keep waiting on the oldest as long as it is still older
1680 * than the original request.
1682 static void sync_write_wait(struct inode
*inode
)
1684 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1685 struct list_head
*head
= &ci
->i_unsafe_writes
;
1686 struct ceph_osd_request
*req
;
1689 spin_lock(&ci
->i_unsafe_lock
);
1690 if (list_empty(head
))
1693 /* set upper bound as _last_ entry in chain */
1694 req
= list_entry(head
->prev
, struct ceph_osd_request
,
1696 last_tid
= req
->r_tid
;
1699 ceph_osdc_get_request(req
);
1700 spin_unlock(&ci
->i_unsafe_lock
);
1701 dout("sync_write_wait on tid %llu (until %llu)\n",
1702 req
->r_tid
, last_tid
);
1703 wait_for_completion(&req
->r_safe_completion
);
1704 spin_lock(&ci
->i_unsafe_lock
);
1705 ceph_osdc_put_request(req
);
1708 * from here on look at first entry in chain, since we
1709 * only want to wait for anything older than last_tid
1711 if (list_empty(head
))
1713 req
= list_entry(head
->next
, struct ceph_osd_request
,
1715 } while (req
->r_tid
< last_tid
);
1717 spin_unlock(&ci
->i_unsafe_lock
);
1720 int ceph_fsync(struct file
*file
, struct dentry
*dentry
, int datasync
)
1722 struct inode
*inode
= dentry
->d_inode
;
1723 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1728 dout("fsync %p%s\n", inode
, datasync
? " datasync" : "");
1729 sync_write_wait(inode
);
1731 ret
= filemap_write_and_wait(inode
->i_mapping
);
1735 dirty
= try_flush_caps(inode
, NULL
, &flush_tid
);
1736 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty
));
1739 * only wait on non-file metadata writeback (the mds
1740 * can recover size and mtime, so we don't need to
1743 if (!datasync
&& (dirty
& ~CEPH_CAP_ANY_FILE_WR
)) {
1744 dout("fsync waiting for flush_tid %u\n", flush_tid
);
1745 ret
= wait_event_interruptible(ci
->i_cap_wq
,
1746 caps_are_flushed(inode
, flush_tid
));
1749 dout("fsync %p%s done\n", inode
, datasync
? " datasync" : "");
1754 * Flush any dirty caps back to the mds. If we aren't asked to wait,
1755 * queue inode for flush but don't do so immediately, because we can
1756 * get by with fewer MDS messages if we wait for data writeback to
1759 int ceph_write_inode(struct inode
*inode
, int wait
)
1761 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1766 dout("write_inode %p wait=%d\n", inode
, wait
);
1768 dirty
= try_flush_caps(inode
, NULL
, &flush_tid
);
1770 err
= wait_event_interruptible(ci
->i_cap_wq
,
1771 caps_are_flushed(inode
, flush_tid
));
1773 struct ceph_mds_client
*mdsc
= &ceph_client(inode
->i_sb
)->mdsc
;
1775 spin_lock(&inode
->i_lock
);
1776 if (__ceph_caps_dirty(ci
))
1777 __cap_delay_requeue_front(mdsc
, ci
);
1778 spin_unlock(&inode
->i_lock
);
1784 * After a recovering MDS goes active, we need to resend any caps
1787 * Caller holds session->s_mutex.
1789 static void kick_flushing_capsnaps(struct ceph_mds_client
*mdsc
,
1790 struct ceph_mds_session
*session
)
1792 struct ceph_cap_snap
*capsnap
;
1794 dout("kick_flushing_capsnaps mds%d\n", session
->s_mds
);
1795 list_for_each_entry(capsnap
, &session
->s_cap_snaps_flushing
,
1797 struct ceph_inode_info
*ci
= capsnap
->ci
;
1798 struct inode
*inode
= &ci
->vfs_inode
;
1799 struct ceph_cap
*cap
;
1801 spin_lock(&inode
->i_lock
);
1802 cap
= ci
->i_auth_cap
;
1803 if (cap
&& cap
->session
== session
) {
1804 dout("kick_flushing_caps %p cap %p capsnap %p\n", inode
,
1806 __ceph_flush_snaps(ci
, &session
);
1808 pr_err("%p auth cap %p not mds%d ???\n", inode
,
1809 cap
, session
->s_mds
);
1810 spin_unlock(&inode
->i_lock
);
1815 void ceph_kick_flushing_caps(struct ceph_mds_client
*mdsc
,
1816 struct ceph_mds_session
*session
)
1818 struct ceph_inode_info
*ci
;
1820 kick_flushing_capsnaps(mdsc
, session
);
1822 dout("kick_flushing_caps mds%d\n", session
->s_mds
);
1823 list_for_each_entry(ci
, &session
->s_cap_flushing
, i_flushing_item
) {
1824 struct inode
*inode
= &ci
->vfs_inode
;
1825 struct ceph_cap
*cap
;
1828 spin_lock(&inode
->i_lock
);
1829 cap
= ci
->i_auth_cap
;
1830 if (cap
&& cap
->session
== session
) {
1831 dout("kick_flushing_caps %p cap %p %s\n", inode
,
1832 cap
, ceph_cap_string(ci
->i_flushing_caps
));
1833 delayed
= __send_cap(mdsc
, cap
, CEPH_CAP_OP_FLUSH
,
1834 __ceph_caps_used(ci
),
1835 __ceph_caps_wanted(ci
),
1836 cap
->issued
| cap
->implemented
,
1837 ci
->i_flushing_caps
, NULL
);
1839 spin_lock(&inode
->i_lock
);
1840 __cap_delay_requeue(mdsc
, ci
);
1841 spin_unlock(&inode
->i_lock
);
1844 pr_err("%p auth cap %p not mds%d ???\n", inode
,
1845 cap
, session
->s_mds
);
1846 spin_unlock(&inode
->i_lock
);
1853 * Take references to capabilities we hold, so that we don't release
1854 * them to the MDS prematurely.
1856 * Protected by i_lock.
1858 static void __take_cap_refs(struct ceph_inode_info
*ci
, int got
)
1860 if (got
& CEPH_CAP_PIN
)
1862 if (got
& CEPH_CAP_FILE_RD
)
1864 if (got
& CEPH_CAP_FILE_CACHE
)
1865 ci
->i_rdcache_ref
++;
1866 if (got
& CEPH_CAP_FILE_WR
)
1868 if (got
& CEPH_CAP_FILE_BUFFER
) {
1869 if (ci
->i_wrbuffer_ref
== 0)
1870 igrab(&ci
->vfs_inode
);
1871 ci
->i_wrbuffer_ref
++;
1872 dout("__take_cap_refs %p wrbuffer %d -> %d (?)\n",
1873 &ci
->vfs_inode
, ci
->i_wrbuffer_ref
-1, ci
->i_wrbuffer_ref
);
1878 * Try to grab cap references. Specify those refs we @want, and the
1879 * minimal set we @need. Also include the larger offset we are writing
1880 * to (when applicable), and check against max_size here as well.
1881 * Note that caller is responsible for ensuring max_size increases are
1882 * requested from the MDS.
1884 static int try_get_cap_refs(struct ceph_inode_info
*ci
, int need
, int want
,
1885 int *got
, loff_t endoff
, int *check_max
, int *err
)
1887 struct inode
*inode
= &ci
->vfs_inode
;
1889 int have
, implemented
;
1891 dout("get_cap_refs %p need %s want %s\n", inode
,
1892 ceph_cap_string(need
), ceph_cap_string(want
));
1893 spin_lock(&inode
->i_lock
);
1895 /* make sure we _have_ some caps! */
1896 if (!__ceph_is_any_caps(ci
)) {
1897 dout("get_cap_refs %p no real caps\n", inode
);
1903 if (need
& CEPH_CAP_FILE_WR
) {
1904 if (endoff
>= 0 && endoff
> (loff_t
)ci
->i_max_size
) {
1905 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
1906 inode
, endoff
, ci
->i_max_size
);
1907 if (endoff
> ci
->i_wanted_max_size
) {
1914 * If a sync write is in progress, we must wait, so that we
1915 * can get a final snapshot value for size+mtime.
1917 if (__ceph_have_pending_cap_snap(ci
)) {
1918 dout("get_cap_refs %p cap_snap_pending\n", inode
);
1922 have
= __ceph_caps_issued(ci
, &implemented
);
1925 * disallow writes while a truncate is pending
1927 if (ci
->i_truncate_pending
)
1928 have
&= ~CEPH_CAP_FILE_WR
;
1930 if ((have
& need
) == need
) {
1932 * Look at (implemented & ~have & not) so that we keep waiting
1933 * on transition from wanted -> needed caps. This is needed
1934 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
1935 * going before a prior buffered writeback happens.
1937 int not = want
& ~(have
& need
);
1938 int revoking
= implemented
& ~have
;
1939 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
1940 inode
, ceph_cap_string(have
), ceph_cap_string(not),
1941 ceph_cap_string(revoking
));
1942 if ((revoking
& not) == 0) {
1943 *got
= need
| (have
& want
);
1944 __take_cap_refs(ci
, *got
);
1948 dout("get_cap_refs %p have %s needed %s\n", inode
,
1949 ceph_cap_string(have
), ceph_cap_string(need
));
1952 spin_unlock(&inode
->i_lock
);
1953 dout("get_cap_refs %p ret %d got %s\n", inode
,
1954 ret
, ceph_cap_string(*got
));
1959 * Check the offset we are writing up to against our current
1960 * max_size. If necessary, tell the MDS we want to write to
1963 static void check_max_size(struct inode
*inode
, loff_t endoff
)
1965 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1968 /* do we need to explicitly request a larger max_size? */
1969 spin_lock(&inode
->i_lock
);
1970 if ((endoff
>= ci
->i_max_size
||
1971 endoff
> (inode
->i_size
<< 1)) &&
1972 endoff
> ci
->i_wanted_max_size
) {
1973 dout("write %p at large endoff %llu, req max_size\n",
1975 ci
->i_wanted_max_size
= endoff
;
1978 spin_unlock(&inode
->i_lock
);
1980 ceph_check_caps(ci
, CHECK_CAPS_AUTHONLY
, NULL
);
1984 * Wait for caps, and take cap references. If we can't get a WR cap
1985 * due to a small max_size, make sure we check_max_size (and possibly
1986 * ask the mds) so we don't get hung up indefinitely.
1988 int ceph_get_caps(struct ceph_inode_info
*ci
, int need
, int want
, int *got
,
1991 int check_max
, ret
, err
;
1995 check_max_size(&ci
->vfs_inode
, endoff
);
1998 ret
= wait_event_interruptible(ci
->i_cap_wq
,
1999 try_get_cap_refs(ci
, need
, want
,
2010 * Take cap refs. Caller must already know we hold at least one ref
2011 * on the caps in question or we don't know this is safe.
2013 void ceph_get_cap_refs(struct ceph_inode_info
*ci
, int caps
)
2015 spin_lock(&ci
->vfs_inode
.i_lock
);
2016 __take_cap_refs(ci
, caps
);
2017 spin_unlock(&ci
->vfs_inode
.i_lock
);
2023 * If we released the last ref on any given cap, call ceph_check_caps
2024 * to release (or schedule a release).
2026 * If we are releasing a WR cap (from a sync write), finalize any affected
2027 * cap_snap, and wake up any waiters.
2029 void ceph_put_cap_refs(struct ceph_inode_info
*ci
, int had
)
2031 struct inode
*inode
= &ci
->vfs_inode
;
2032 int last
= 0, put
= 0, flushsnaps
= 0, wake
= 0;
2033 struct ceph_cap_snap
*capsnap
;
2035 spin_lock(&inode
->i_lock
);
2036 if (had
& CEPH_CAP_PIN
)
2038 if (had
& CEPH_CAP_FILE_RD
)
2039 if (--ci
->i_rd_ref
== 0)
2041 if (had
& CEPH_CAP_FILE_CACHE
)
2042 if (--ci
->i_rdcache_ref
== 0)
2044 if (had
& CEPH_CAP_FILE_BUFFER
) {
2045 if (--ci
->i_wrbuffer_ref
== 0) {
2049 dout("put_cap_refs %p wrbuffer %d -> %d (?)\n",
2050 inode
, ci
->i_wrbuffer_ref
+1, ci
->i_wrbuffer_ref
);
2052 if (had
& CEPH_CAP_FILE_WR
)
2053 if (--ci
->i_wr_ref
== 0) {
2055 if (!list_empty(&ci
->i_cap_snaps
)) {
2056 capsnap
= list_first_entry(&ci
->i_cap_snaps
,
2057 struct ceph_cap_snap
,
2059 if (capsnap
->writing
) {
2060 capsnap
->writing
= 0;
2062 __ceph_finish_cap_snap(ci
,
2068 spin_unlock(&inode
->i_lock
);
2070 dout("put_cap_refs %p had %s %s\n", inode
, ceph_cap_string(had
),
2071 last
? "last" : "");
2073 if (last
&& !flushsnaps
)
2074 ceph_check_caps(ci
, 0, NULL
);
2075 else if (flushsnaps
)
2076 ceph_flush_snaps(ci
);
2078 wake_up(&ci
->i_cap_wq
);
2084 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2085 * context. Adjust per-snap dirty page accounting as appropriate.
2086 * Once all dirty data for a cap_snap is flushed, flush snapped file
2087 * metadata back to the MDS. If we dropped the last ref, call
2090 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info
*ci
, int nr
,
2091 struct ceph_snap_context
*snapc
)
2093 struct inode
*inode
= &ci
->vfs_inode
;
2097 struct ceph_cap_snap
*capsnap
= NULL
;
2099 spin_lock(&inode
->i_lock
);
2100 ci
->i_wrbuffer_ref
-= nr
;
2101 last
= !ci
->i_wrbuffer_ref
;
2103 if (ci
->i_head_snapc
== snapc
) {
2104 ci
->i_wrbuffer_ref_head
-= nr
;
2105 if (!ci
->i_wrbuffer_ref_head
) {
2106 ceph_put_snap_context(ci
->i_head_snapc
);
2107 ci
->i_head_snapc
= NULL
;
2109 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2111 ci
->i_wrbuffer_ref
+nr
, ci
->i_wrbuffer_ref_head
+nr
,
2112 ci
->i_wrbuffer_ref
, ci
->i_wrbuffer_ref_head
,
2113 last
? " LAST" : "");
2115 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
2116 if (capsnap
->context
== snapc
) {
2118 capsnap
->dirty_pages
-= nr
;
2119 last_snap
= !capsnap
->dirty_pages
;
2124 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2125 " snap %lld %d/%d -> %d/%d %s%s\n",
2126 inode
, capsnap
, capsnap
->context
->seq
,
2127 ci
->i_wrbuffer_ref
+nr
, capsnap
->dirty_pages
+ nr
,
2128 ci
->i_wrbuffer_ref
, capsnap
->dirty_pages
,
2129 last
? " (wrbuffer last)" : "",
2130 last_snap
? " (capsnap last)" : "");
2133 spin_unlock(&inode
->i_lock
);
2136 ceph_check_caps(ci
, CHECK_CAPS_AUTHONLY
, NULL
);
2138 } else if (last_snap
) {
2139 ceph_flush_snaps(ci
);
2140 wake_up(&ci
->i_cap_wq
);
2145 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2146 * actually be a revocation if it specifies a smaller cap set.)
2148 * caller holds s_mutex.
2151 * 1 - check_caps on auth cap only (writeback)
2152 * 2 - check_caps (ack revoke)
2154 static int handle_cap_grant(struct inode
*inode
, struct ceph_mds_caps
*grant
,
2155 struct ceph_mds_session
*session
,
2156 struct ceph_cap
*cap
,
2157 struct ceph_buffer
*xattr_buf
)
2158 __releases(inode
->i_lock
)
2161 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2162 int mds
= session
->s_mds
;
2163 int seq
= le32_to_cpu(grant
->seq
);
2164 int newcaps
= le32_to_cpu(grant
->caps
);
2165 int issued
, implemented
, used
, wanted
, dirty
;
2166 u64 size
= le64_to_cpu(grant
->size
);
2167 u64 max_size
= le64_to_cpu(grant
->max_size
);
2168 struct timespec mtime
, atime
, ctime
;
2172 int revoked_rdcache
= 0;
2173 int queue_invalidate
= 0;
2174 int tried_invalidate
= 0;
2177 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2178 inode
, cap
, mds
, seq
, ceph_cap_string(newcaps
));
2179 dout(" size %llu max_size %llu, i_size %llu\n", size
, max_size
,
2183 * If CACHE is being revoked, and we have no dirty buffers,
2184 * try to invalidate (once). (If there are dirty buffers, we
2185 * will invalidate _after_ writeback.)
2188 if (((cap
->issued
& ~newcaps
) & CEPH_CAP_FILE_CACHE
) &&
2189 !ci
->i_wrbuffer_ref
&& !tried_invalidate
) {
2190 dout("CACHE invalidation\n");
2191 spin_unlock(&inode
->i_lock
);
2192 tried_invalidate
= 1;
2194 ret
= invalidate_mapping_pages(&inode
->i_data
, 0, -1);
2195 spin_lock(&inode
->i_lock
);
2197 /* there were locked pages.. invalidate later
2198 in a separate thread. */
2199 if (ci
->i_rdcache_revoking
!= ci
->i_rdcache_gen
) {
2200 queue_invalidate
= 1;
2201 ci
->i_rdcache_revoking
= ci
->i_rdcache_gen
;
2204 /* we successfully invalidated those pages */
2205 revoked_rdcache
= 1;
2206 ci
->i_rdcache_gen
= 0;
2207 ci
->i_rdcache_revoking
= 0;
2212 /* side effects now are allowed */
2214 issued
= __ceph_caps_issued(ci
, &implemented
);
2215 issued
|= implemented
| __ceph_caps_dirty(ci
);
2217 cap
->cap_gen
= session
->s_cap_gen
;
2219 __check_cap_issue(ci
, cap
, newcaps
);
2221 if ((issued
& CEPH_CAP_AUTH_EXCL
) == 0) {
2222 inode
->i_mode
= le32_to_cpu(grant
->mode
);
2223 inode
->i_uid
= le32_to_cpu(grant
->uid
);
2224 inode
->i_gid
= le32_to_cpu(grant
->gid
);
2225 dout("%p mode 0%o uid.gid %d.%d\n", inode
, inode
->i_mode
,
2226 inode
->i_uid
, inode
->i_gid
);
2229 if ((issued
& CEPH_CAP_LINK_EXCL
) == 0)
2230 inode
->i_nlink
= le32_to_cpu(grant
->nlink
);
2232 if ((issued
& CEPH_CAP_XATTR_EXCL
) == 0 && grant
->xattr_len
) {
2233 int len
= le32_to_cpu(grant
->xattr_len
);
2234 u64 version
= le64_to_cpu(grant
->xattr_version
);
2236 if (version
> ci
->i_xattrs
.version
) {
2237 dout(" got new xattrs v%llu on %p len %d\n",
2238 version
, inode
, len
);
2239 if (ci
->i_xattrs
.blob
)
2240 ceph_buffer_put(ci
->i_xattrs
.blob
);
2241 ci
->i_xattrs
.blob
= ceph_buffer_get(xattr_buf
);
2242 ci
->i_xattrs
.version
= version
;
2246 /* size/ctime/mtime/atime? */
2247 ceph_fill_file_size(inode
, issued
,
2248 le32_to_cpu(grant
->truncate_seq
),
2249 le64_to_cpu(grant
->truncate_size
), size
);
2250 ceph_decode_timespec(&mtime
, &grant
->mtime
);
2251 ceph_decode_timespec(&atime
, &grant
->atime
);
2252 ceph_decode_timespec(&ctime
, &grant
->ctime
);
2253 ceph_fill_file_time(inode
, issued
,
2254 le32_to_cpu(grant
->time_warp_seq
), &ctime
, &mtime
,
2257 /* max size increase? */
2258 if (max_size
!= ci
->i_max_size
) {
2259 dout("max_size %lld -> %llu\n", ci
->i_max_size
, max_size
);
2260 ci
->i_max_size
= max_size
;
2261 if (max_size
>= ci
->i_wanted_max_size
) {
2262 ci
->i_wanted_max_size
= 0; /* reset */
2263 ci
->i_requested_max_size
= 0;
2268 /* check cap bits */
2269 wanted
= __ceph_caps_wanted(ci
);
2270 used
= __ceph_caps_used(ci
);
2271 dirty
= __ceph_caps_dirty(ci
);
2272 dout(" my wanted = %s, used = %s, dirty %s\n",
2273 ceph_cap_string(wanted
),
2274 ceph_cap_string(used
),
2275 ceph_cap_string(dirty
));
2276 if (wanted
!= le32_to_cpu(grant
->wanted
)) {
2277 dout("mds wanted %s -> %s\n",
2278 ceph_cap_string(le32_to_cpu(grant
->wanted
)),
2279 ceph_cap_string(wanted
));
2280 grant
->wanted
= cpu_to_le32(wanted
);
2285 /* file layout may have changed */
2286 ci
->i_layout
= grant
->layout
;
2288 /* revocation, grant, or no-op? */
2289 if (cap
->issued
& ~newcaps
) {
2290 dout("revocation: %s -> %s\n", ceph_cap_string(cap
->issued
),
2291 ceph_cap_string(newcaps
));
2292 if ((used
& ~newcaps
) & CEPH_CAP_FILE_BUFFER
)
2293 writeback
= 1; /* will delay ack */
2294 else if (dirty
& ~newcaps
)
2295 reply
= 1; /* initiate writeback in check_caps */
2296 else if (((used
& ~newcaps
) & CEPH_CAP_FILE_CACHE
) == 0 ||
2298 reply
= 2; /* send revoke ack in check_caps */
2299 cap
->issued
= newcaps
;
2300 } else if (cap
->issued
== newcaps
) {
2301 dout("caps unchanged: %s -> %s\n",
2302 ceph_cap_string(cap
->issued
), ceph_cap_string(newcaps
));
2304 dout("grant: %s -> %s\n", ceph_cap_string(cap
->issued
),
2305 ceph_cap_string(newcaps
));
2306 cap
->issued
= newcaps
;
2307 cap
->implemented
|= newcaps
; /* add bits only, to
2308 * avoid stepping on a
2309 * pending revocation */
2313 spin_unlock(&inode
->i_lock
);
2316 * queue inode for writeback: we can't actually call
2317 * filemap_write_and_wait, etc. from message handler
2320 ceph_queue_writeback(inode
);
2321 if (queue_invalidate
)
2322 ceph_queue_invalidate(inode
);
2324 wake_up(&ci
->i_cap_wq
);
2329 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2330 * MDS has been safely committed.
2332 static void handle_cap_flush_ack(struct inode
*inode
, u64 flush_tid
,
2333 struct ceph_mds_caps
*m
,
2334 struct ceph_mds_session
*session
,
2335 struct ceph_cap
*cap
)
2336 __releases(inode
->i_lock
)
2338 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2339 struct ceph_mds_client
*mdsc
= &ceph_client(inode
->i_sb
)->mdsc
;
2340 unsigned seq
= le32_to_cpu(m
->seq
);
2341 int dirty
= le32_to_cpu(m
->dirty
);
2346 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
2347 if ((dirty
& (1 << i
)) &&
2348 flush_tid
== ci
->i_cap_flush_tid
[i
])
2351 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2352 " flushing %s -> %s\n",
2353 inode
, session
->s_mds
, seq
, ceph_cap_string(dirty
),
2354 ceph_cap_string(cleaned
), ceph_cap_string(ci
->i_flushing_caps
),
2355 ceph_cap_string(ci
->i_flushing_caps
& ~cleaned
));
2357 if (ci
->i_flushing_caps
== (ci
->i_flushing_caps
& ~cleaned
))
2360 ci
->i_flushing_caps
&= ~cleaned
;
2362 spin_lock(&mdsc
->cap_dirty_lock
);
2363 if (ci
->i_flushing_caps
== 0) {
2364 list_del_init(&ci
->i_flushing_item
);
2365 if (!list_empty(&session
->s_cap_flushing
))
2366 dout(" mds%d still flushing cap on %p\n",
2368 &list_entry(session
->s_cap_flushing
.next
,
2369 struct ceph_inode_info
,
2370 i_flushing_item
)->vfs_inode
);
2371 mdsc
->num_cap_flushing
--;
2372 wake_up(&mdsc
->cap_flushing_wq
);
2373 dout(" inode %p now !flushing\n", inode
);
2375 if (ci
->i_dirty_caps
== 0) {
2376 dout(" inode %p now clean\n", inode
);
2377 BUG_ON(!list_empty(&ci
->i_dirty_item
));
2380 BUG_ON(list_empty(&ci
->i_dirty_item
));
2383 spin_unlock(&mdsc
->cap_dirty_lock
);
2384 wake_up(&ci
->i_cap_wq
);
2387 spin_unlock(&inode
->i_lock
);
2393 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
2394 * throw away our cap_snap.
2396 * Caller hold s_mutex.
2398 static void handle_cap_flushsnap_ack(struct inode
*inode
, u64 flush_tid
,
2399 struct ceph_mds_caps
*m
,
2400 struct ceph_mds_session
*session
)
2402 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2403 u64 follows
= le64_to_cpu(m
->snap_follows
);
2404 struct ceph_cap_snap
*capsnap
;
2407 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2408 inode
, ci
, session
->s_mds
, follows
);
2410 spin_lock(&inode
->i_lock
);
2411 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
2412 if (capsnap
->follows
== follows
) {
2413 if (capsnap
->flush_tid
!= flush_tid
) {
2414 dout(" cap_snap %p follows %lld tid %lld !="
2415 " %lld\n", capsnap
, follows
,
2416 flush_tid
, capsnap
->flush_tid
);
2419 WARN_ON(capsnap
->dirty_pages
|| capsnap
->writing
);
2420 dout(" removing cap_snap %p follows %lld\n",
2422 ceph_put_snap_context(capsnap
->context
);
2423 list_del(&capsnap
->ci_item
);
2424 list_del(&capsnap
->flushing_item
);
2425 ceph_put_cap_snap(capsnap
);
2429 dout(" skipping cap_snap %p follows %lld\n",
2430 capsnap
, capsnap
->follows
);
2433 spin_unlock(&inode
->i_lock
);
2439 * Handle TRUNC from MDS, indicating file truncation.
2441 * caller hold s_mutex.
2443 static void handle_cap_trunc(struct inode
*inode
,
2444 struct ceph_mds_caps
*trunc
,
2445 struct ceph_mds_session
*session
)
2446 __releases(inode
->i_lock
)
2448 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2449 int mds
= session
->s_mds
;
2450 int seq
= le32_to_cpu(trunc
->seq
);
2451 u32 truncate_seq
= le32_to_cpu(trunc
->truncate_seq
);
2452 u64 truncate_size
= le64_to_cpu(trunc
->truncate_size
);
2453 u64 size
= le64_to_cpu(trunc
->size
);
2454 int implemented
= 0;
2455 int dirty
= __ceph_caps_dirty(ci
);
2456 int issued
= __ceph_caps_issued(ceph_inode(inode
), &implemented
);
2457 int queue_trunc
= 0;
2459 issued
|= implemented
| dirty
;
2461 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2462 inode
, mds
, seq
, truncate_size
, truncate_seq
);
2463 queue_trunc
= ceph_fill_file_size(inode
, issued
,
2464 truncate_seq
, truncate_size
, size
);
2465 spin_unlock(&inode
->i_lock
);
2468 ceph_queue_vmtruncate(inode
);
2472 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
2473 * different one. If we are the most recent migration we've seen (as
2474 * indicated by mseq), make note of the migrating cap bits for the
2475 * duration (until we see the corresponding IMPORT).
2477 * caller holds s_mutex
2479 static void handle_cap_export(struct inode
*inode
, struct ceph_mds_caps
*ex
,
2480 struct ceph_mds_session
*session
)
2482 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2483 int mds
= session
->s_mds
;
2484 unsigned mseq
= le32_to_cpu(ex
->migrate_seq
);
2485 struct ceph_cap
*cap
= NULL
, *t
;
2489 dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
2490 inode
, ci
, mds
, mseq
);
2492 spin_lock(&inode
->i_lock
);
2494 /* make sure we haven't seen a higher mseq */
2495 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
2496 t
= rb_entry(p
, struct ceph_cap
, ci_node
);
2497 if (ceph_seq_cmp(t
->mseq
, mseq
) > 0) {
2498 dout(" higher mseq on cap from mds%d\n",
2502 if (t
->session
->s_mds
== mds
)
2509 ci
->i_cap_exporting_mds
= mds
;
2510 ci
->i_cap_exporting_mseq
= mseq
;
2511 ci
->i_cap_exporting_issued
= cap
->issued
;
2513 __ceph_remove_cap(cap
, NULL
);
2518 spin_unlock(&inode
->i_lock
);
2522 * Handle cap IMPORT. If there are temp bits from an older EXPORT,
2525 * caller holds s_mutex.
2527 static void handle_cap_import(struct ceph_mds_client
*mdsc
,
2528 struct inode
*inode
, struct ceph_mds_caps
*im
,
2529 struct ceph_mds_session
*session
,
2530 void *snaptrace
, int snaptrace_len
)
2532 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2533 int mds
= session
->s_mds
;
2534 unsigned issued
= le32_to_cpu(im
->caps
);
2535 unsigned wanted
= le32_to_cpu(im
->wanted
);
2536 unsigned seq
= le32_to_cpu(im
->seq
);
2537 unsigned mseq
= le32_to_cpu(im
->migrate_seq
);
2538 u64 realmino
= le64_to_cpu(im
->realm
);
2539 u64 cap_id
= le64_to_cpu(im
->cap_id
);
2541 if (ci
->i_cap_exporting_mds
>= 0 &&
2542 ceph_seq_cmp(ci
->i_cap_exporting_mseq
, mseq
) < 0) {
2543 dout("handle_cap_import inode %p ci %p mds%d mseq %d"
2544 " - cleared exporting from mds%d\n",
2545 inode
, ci
, mds
, mseq
,
2546 ci
->i_cap_exporting_mds
);
2547 ci
->i_cap_exporting_issued
= 0;
2548 ci
->i_cap_exporting_mseq
= 0;
2549 ci
->i_cap_exporting_mds
= -1;
2551 dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
2552 inode
, ci
, mds
, mseq
);
2555 down_write(&mdsc
->snap_rwsem
);
2556 ceph_update_snap_trace(mdsc
, snaptrace
, snaptrace
+snaptrace_len
,
2558 downgrade_write(&mdsc
->snap_rwsem
);
2559 ceph_add_cap(inode
, session
, cap_id
, -1,
2560 issued
, wanted
, seq
, mseq
, realmino
, CEPH_CAP_FLAG_AUTH
,
2561 NULL
/* no caps context */);
2562 try_flush_caps(inode
, session
, NULL
);
2563 up_read(&mdsc
->snap_rwsem
);
2567 * Handle a caps message from the MDS.
2569 * Identify the appropriate session, inode, and call the right handler
2570 * based on the cap op.
2572 void ceph_handle_caps(struct ceph_mds_session
*session
,
2573 struct ceph_msg
*msg
)
2575 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2576 struct super_block
*sb
= mdsc
->client
->sb
;
2577 struct inode
*inode
;
2578 struct ceph_cap
*cap
;
2579 struct ceph_mds_caps
*h
;
2580 int mds
= le64_to_cpu(msg
->hdr
.src
.name
.num
);
2583 struct ceph_vino vino
;
2590 dout("handle_caps from mds%d\n", mds
);
2593 tid
= le64_to_cpu(msg
->hdr
.tid
);
2594 if (msg
->front
.iov_len
< sizeof(*h
))
2596 h
= msg
->front
.iov_base
;
2597 op
= le32_to_cpu(h
->op
);
2598 vino
.ino
= le64_to_cpu(h
->ino
);
2599 vino
.snap
= CEPH_NOSNAP
;
2600 cap_id
= le64_to_cpu(h
->cap_id
);
2601 seq
= le32_to_cpu(h
->seq
);
2602 size
= le64_to_cpu(h
->size
);
2603 max_size
= le64_to_cpu(h
->max_size
);
2605 mutex_lock(&session
->s_mutex
);
2607 dout(" mds%d seq %lld cap seq %u\n", session
->s_mds
, session
->s_seq
,
2611 inode
= ceph_find_inode(sb
, vino
);
2612 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op
), vino
.ino
,
2615 dout(" i don't have ino %llx\n", vino
.ino
);
2619 /* these will work even if we don't have a cap yet */
2621 case CEPH_CAP_OP_FLUSHSNAP_ACK
:
2622 handle_cap_flushsnap_ack(inode
, tid
, h
, session
);
2625 case CEPH_CAP_OP_EXPORT
:
2626 handle_cap_export(inode
, h
, session
);
2629 case CEPH_CAP_OP_IMPORT
:
2630 handle_cap_import(mdsc
, inode
, h
, session
,
2632 le32_to_cpu(h
->snap_trace_len
));
2633 check_caps
= 1; /* we may have sent a RELEASE to the old auth */
2637 /* the rest require a cap */
2638 spin_lock(&inode
->i_lock
);
2639 cap
= __get_cap_for_mds(ceph_inode(inode
), mds
);
2641 dout("no cap on %p ino %llx.%llx from mds%d, releasing\n",
2642 inode
, ceph_ino(inode
), ceph_snap(inode
), mds
);
2643 spin_unlock(&inode
->i_lock
);
2647 /* note that each of these drops i_lock for us */
2649 case CEPH_CAP_OP_REVOKE
:
2650 case CEPH_CAP_OP_GRANT
:
2651 r
= handle_cap_grant(inode
, h
, session
, cap
, msg
->middle
);
2653 ceph_check_caps(ceph_inode(inode
),
2654 CHECK_CAPS_NODELAY
|CHECK_CAPS_AUTHONLY
,
2657 ceph_check_caps(ceph_inode(inode
),
2662 case CEPH_CAP_OP_FLUSH_ACK
:
2663 handle_cap_flush_ack(inode
, tid
, h
, session
, cap
);
2666 case CEPH_CAP_OP_TRUNC
:
2667 handle_cap_trunc(inode
, h
, session
);
2671 spin_unlock(&inode
->i_lock
);
2672 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op
,
2673 ceph_cap_op_name(op
));
2677 mutex_unlock(&session
->s_mutex
);
2680 ceph_check_caps(ceph_inode(inode
), CHECK_CAPS_NODELAY
, NULL
);
2686 pr_err("ceph_handle_caps: corrupt message\n");
2692 * Delayed work handler to process end of delayed cap release LRU list.
2694 void ceph_check_delayed_caps(struct ceph_mds_client
*mdsc
)
2696 struct ceph_inode_info
*ci
;
2697 int flags
= CHECK_CAPS_NODELAY
;
2699 dout("check_delayed_caps\n");
2701 spin_lock(&mdsc
->cap_delay_lock
);
2702 if (list_empty(&mdsc
->cap_delay_list
))
2704 ci
= list_first_entry(&mdsc
->cap_delay_list
,
2705 struct ceph_inode_info
,
2707 if ((ci
->i_ceph_flags
& CEPH_I_FLUSH
) == 0 &&
2708 time_before(jiffies
, ci
->i_hold_caps_max
))
2710 list_del_init(&ci
->i_cap_delay_list
);
2711 spin_unlock(&mdsc
->cap_delay_lock
);
2712 dout("check_delayed_caps on %p\n", &ci
->vfs_inode
);
2713 ceph_check_caps(ci
, flags
, NULL
);
2715 spin_unlock(&mdsc
->cap_delay_lock
);
2719 * Flush all dirty caps to the mds
2721 void ceph_flush_dirty_caps(struct ceph_mds_client
*mdsc
)
2723 struct ceph_inode_info
*ci
;
2724 struct inode
*inode
;
2726 dout("flush_dirty_caps\n");
2727 spin_lock(&mdsc
->cap_dirty_lock
);
2728 while (!list_empty(&mdsc
->cap_dirty
)) {
2729 ci
= list_first_entry(&mdsc
->cap_dirty
,
2730 struct ceph_inode_info
,
2732 inode
= igrab(&ci
->vfs_inode
);
2733 spin_unlock(&mdsc
->cap_dirty_lock
);
2735 ceph_check_caps(ci
, CHECK_CAPS_NODELAY
|CHECK_CAPS_FLUSH
,
2739 spin_lock(&mdsc
->cap_dirty_lock
);
2741 spin_unlock(&mdsc
->cap_dirty_lock
);
2745 * Drop open file reference. If we were the last open file,
2746 * we may need to release capabilities to the MDS (or schedule
2747 * their delayed release).
2749 void ceph_put_fmode(struct ceph_inode_info
*ci
, int fmode
)
2751 struct inode
*inode
= &ci
->vfs_inode
;
2754 spin_lock(&inode
->i_lock
);
2755 dout("put_fmode %p fmode %d %d -> %d\n", inode
, fmode
,
2756 ci
->i_nr_by_mode
[fmode
], ci
->i_nr_by_mode
[fmode
]-1);
2757 BUG_ON(ci
->i_nr_by_mode
[fmode
] == 0);
2758 if (--ci
->i_nr_by_mode
[fmode
] == 0)
2760 spin_unlock(&inode
->i_lock
);
2762 if (last
&& ci
->i_vino
.snap
== CEPH_NOSNAP
)
2763 ceph_check_caps(ci
, 0, NULL
);
2767 * Helpers for embedding cap and dentry lease releases into mds
2770 * @force is used by dentry_release (below) to force inclusion of a
2771 * record for the directory inode, even when there aren't any caps to
2774 int ceph_encode_inode_release(void **p
, struct inode
*inode
,
2775 int mds
, int drop
, int unless
, int force
)
2777 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2778 struct ceph_cap
*cap
;
2779 struct ceph_mds_request_release
*rel
= *p
;
2782 dout("encode_inode_release %p mds%d drop %s unless %s\n", inode
,
2783 mds
, ceph_cap_string(drop
), ceph_cap_string(unless
));
2785 spin_lock(&inode
->i_lock
);
2786 cap
= __get_cap_for_mds(ci
, mds
);
2787 if (cap
&& __cap_is_valid(cap
)) {
2789 ((cap
->issued
& drop
) &&
2790 (cap
->issued
& unless
) == 0)) {
2791 if ((cap
->issued
& drop
) &&
2792 (cap
->issued
& unless
) == 0) {
2793 dout("encode_inode_release %p cap %p %s -> "
2795 ceph_cap_string(cap
->issued
),
2796 ceph_cap_string(cap
->issued
& ~drop
));
2797 cap
->issued
&= ~drop
;
2798 cap
->implemented
&= ~drop
;
2799 if (ci
->i_ceph_flags
& CEPH_I_NODELAY
) {
2800 int wanted
= __ceph_caps_wanted(ci
);
2801 dout(" wanted %s -> %s (act %s)\n",
2802 ceph_cap_string(cap
->mds_wanted
),
2803 ceph_cap_string(cap
->mds_wanted
&
2805 ceph_cap_string(wanted
));
2806 cap
->mds_wanted
&= wanted
;
2809 dout("encode_inode_release %p cap %p %s"
2810 " (force)\n", inode
, cap
,
2811 ceph_cap_string(cap
->issued
));
2814 rel
->ino
= cpu_to_le64(ceph_ino(inode
));
2815 rel
->cap_id
= cpu_to_le64(cap
->cap_id
);
2816 rel
->seq
= cpu_to_le32(cap
->seq
);
2817 rel
->issue_seq
= cpu_to_le32(cap
->issue_seq
),
2818 rel
->mseq
= cpu_to_le32(cap
->mseq
);
2819 rel
->caps
= cpu_to_le32(cap
->issued
);
2820 rel
->wanted
= cpu_to_le32(cap
->mds_wanted
);
2826 dout("encode_inode_release %p cap %p %s\n",
2827 inode
, cap
, ceph_cap_string(cap
->issued
));
2830 spin_unlock(&inode
->i_lock
);
2834 int ceph_encode_dentry_release(void **p
, struct dentry
*dentry
,
2835 int mds
, int drop
, int unless
)
2837 struct inode
*dir
= dentry
->d_parent
->d_inode
;
2838 struct ceph_mds_request_release
*rel
= *p
;
2839 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
2844 * force an record for the directory caps if we have a dentry lease.
2845 * this is racy (can't take i_lock and d_lock together), but it
2846 * doesn't have to be perfect; the mds will revoke anything we don't
2849 spin_lock(&dentry
->d_lock
);
2850 if (di
->lease_session
&& di
->lease_session
->s_mds
== mds
)
2852 spin_unlock(&dentry
->d_lock
);
2854 ret
= ceph_encode_inode_release(p
, dir
, mds
, drop
, unless
, force
);
2856 spin_lock(&dentry
->d_lock
);
2857 if (ret
&& di
->lease_session
&& di
->lease_session
->s_mds
== mds
) {
2858 dout("encode_dentry_release %p mds%d seq %d\n",
2859 dentry
, mds
, (int)di
->lease_seq
);
2860 rel
->dname_len
= cpu_to_le32(dentry
->d_name
.len
);
2861 memcpy(*p
, dentry
->d_name
.name
, dentry
->d_name
.len
);
2862 *p
+= dentry
->d_name
.len
;
2863 rel
->dname_seq
= cpu_to_le32(di
->lease_seq
);
2865 spin_unlock(&dentry
->d_lock
);