1 #include "ceph_debug.h"
4 #include <linux/kernel.h>
5 #include <linux/sched.h>
6 #include <linux/slab.h>
7 #include <linux/vmalloc.h>
8 #include <linux/wait.h>
9 #include <linux/writeback.h>
13 #include "messenger.h"
16 * Capability management
18 * The Ceph metadata servers control client access to inode metadata
19 * and file data by issuing capabilities, granting clients permission
20 * to read and/or write both inode field and file data to OSDs
21 * (storage nodes). Each capability consists of a set of bits
22 * indicating which operations are allowed.
24 * If the client holds a *_SHARED cap, the client has a coherent value
25 * that can be safely read from the cached inode.
27 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
28 * client is allowed to change inode attributes (e.g., file size,
29 * mtime), note its dirty state in the ceph_cap, and asynchronously
30 * flush that metadata change to the MDS.
32 * In the event of a conflicting operation (perhaps by another
33 * client), the MDS will revoke the conflicting client capabilities.
35 * In order for a client to cache an inode, it must hold a capability
36 * with at least one MDS server. When inodes are released, release
37 * notifications are batched and periodically sent en masse to the MDS
38 * cluster to release server state.
43 * Generate readable cap strings for debugging output.
45 #define MAX_CAP_STR 20
46 static char cap_str
[MAX_CAP_STR
][40];
47 static DEFINE_SPINLOCK(cap_str_lock
);
48 static int last_cap_str
;
50 static char *gcap_string(char *s
, int c
)
52 if (c
& CEPH_CAP_GSHARED
)
54 if (c
& CEPH_CAP_GEXCL
)
56 if (c
& CEPH_CAP_GCACHE
)
62 if (c
& CEPH_CAP_GBUFFER
)
64 if (c
& CEPH_CAP_GLAZYIO
)
69 const char *ceph_cap_string(int caps
)
75 spin_lock(&cap_str_lock
);
77 if (last_cap_str
== MAX_CAP_STR
)
79 spin_unlock(&cap_str_lock
);
83 if (caps
& CEPH_CAP_PIN
)
86 c
= (caps
>> CEPH_CAP_SAUTH
) & 3;
89 s
= gcap_string(s
, c
);
92 c
= (caps
>> CEPH_CAP_SLINK
) & 3;
95 s
= gcap_string(s
, c
);
98 c
= (caps
>> CEPH_CAP_SXATTR
) & 3;
101 s
= gcap_string(s
, c
);
104 c
= caps
>> CEPH_CAP_SFILE
;
107 s
= gcap_string(s
, c
);
119 * Maintain a global pool of preallocated struct ceph_caps, referenced
120 * by struct ceph_caps_reservations. This ensures that we preallocate
121 * memory needed to successfully process an MDS response. (If an MDS
122 * sends us cap information and we fail to process it, we will have
123 * problems due to the client and MDS being out of sync.)
125 * Reservations are 'owned' by a ceph_cap_reservation context.
127 static spinlock_t caps_list_lock
;
128 static struct list_head caps_list
; /* unused (reserved or unreserved) */
129 static int caps_total_count
; /* total caps allocated */
130 static int caps_use_count
; /* in use */
131 static int caps_reserve_count
; /* unused, reserved */
132 static int caps_avail_count
; /* unused, unreserved */
133 static int caps_min_count
; /* keep at least this many (unreserved) */
135 void __init
ceph_caps_init(void)
137 INIT_LIST_HEAD(&caps_list
);
138 spin_lock_init(&caps_list_lock
);
141 void ceph_caps_finalize(void)
143 struct ceph_cap
*cap
;
145 spin_lock(&caps_list_lock
);
146 while (!list_empty(&caps_list
)) {
147 cap
= list_first_entry(&caps_list
, struct ceph_cap
, caps_item
);
148 list_del(&cap
->caps_item
);
149 kmem_cache_free(ceph_cap_cachep
, cap
);
151 caps_total_count
= 0;
152 caps_avail_count
= 0;
154 caps_reserve_count
= 0;
156 spin_unlock(&caps_list_lock
);
159 void ceph_adjust_min_caps(int delta
)
161 spin_lock(&caps_list_lock
);
162 caps_min_count
+= delta
;
163 BUG_ON(caps_min_count
< 0);
164 spin_unlock(&caps_list_lock
);
167 int ceph_reserve_caps(struct ceph_cap_reservation
*ctx
, int need
)
170 struct ceph_cap
*cap
;
176 dout("reserve caps ctx=%p need=%d\n", ctx
, need
);
178 /* first reserve any caps that are already allocated */
179 spin_lock(&caps_list_lock
);
180 if (caps_avail_count
>= need
)
183 have
= caps_avail_count
;
184 caps_avail_count
-= have
;
185 caps_reserve_count
+= have
;
186 BUG_ON(caps_total_count
!= caps_use_count
+ caps_reserve_count
+
188 spin_unlock(&caps_list_lock
);
190 for (i
= have
; i
< need
; i
++) {
191 cap
= kmem_cache_alloc(ceph_cap_cachep
, GFP_NOFS
);
194 goto out_alloc_count
;
196 list_add(&cap
->caps_item
, &newcaps
);
199 BUG_ON(have
+ alloc
!= need
);
201 spin_lock(&caps_list_lock
);
202 caps_total_count
+= alloc
;
203 caps_reserve_count
+= alloc
;
204 list_splice(&newcaps
, &caps_list
);
206 BUG_ON(caps_total_count
!= caps_use_count
+ caps_reserve_count
+
208 spin_unlock(&caps_list_lock
);
211 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
212 ctx
, caps_total_count
, caps_use_count
, caps_reserve_count
,
217 /* we didn't manage to reserve as much as we needed */
218 pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
223 int ceph_unreserve_caps(struct ceph_cap_reservation
*ctx
)
225 dout("unreserve caps ctx=%p count=%d\n", ctx
, ctx
->count
);
227 spin_lock(&caps_list_lock
);
228 BUG_ON(caps_reserve_count
< ctx
->count
);
229 caps_reserve_count
-= ctx
->count
;
230 caps_avail_count
+= ctx
->count
;
232 dout("unreserve caps %d = %d used + %d resv + %d avail\n",
233 caps_total_count
, caps_use_count
, caps_reserve_count
,
235 BUG_ON(caps_total_count
!= caps_use_count
+ caps_reserve_count
+
237 spin_unlock(&caps_list_lock
);
242 static struct ceph_cap
*get_cap(struct ceph_cap_reservation
*ctx
)
244 struct ceph_cap
*cap
= NULL
;
246 /* temporary, until we do something about cap import/export */
248 cap
= kmem_cache_alloc(ceph_cap_cachep
, GFP_NOFS
);
256 spin_lock(&caps_list_lock
);
257 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
258 ctx
, ctx
->count
, caps_total_count
, caps_use_count
,
259 caps_reserve_count
, caps_avail_count
);
261 BUG_ON(ctx
->count
> caps_reserve_count
);
262 BUG_ON(list_empty(&caps_list
));
265 caps_reserve_count
--;
268 cap
= list_first_entry(&caps_list
, struct ceph_cap
, caps_item
);
269 list_del(&cap
->caps_item
);
271 BUG_ON(caps_total_count
!= caps_use_count
+ caps_reserve_count
+
273 spin_unlock(&caps_list_lock
);
277 void ceph_put_cap(struct ceph_cap
*cap
)
279 spin_lock(&caps_list_lock
);
280 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
281 cap
, caps_total_count
, caps_use_count
,
282 caps_reserve_count
, caps_avail_count
);
285 * Keep some preallocated caps around (ceph_min_count), to
286 * avoid lots of free/alloc churn.
288 if (caps_avail_count
>= caps_reserve_count
+ caps_min_count
) {
290 kmem_cache_free(ceph_cap_cachep
, cap
);
293 list_add(&cap
->caps_item
, &caps_list
);
296 BUG_ON(caps_total_count
!= caps_use_count
+ caps_reserve_count
+
298 spin_unlock(&caps_list_lock
);
301 void ceph_reservation_status(struct ceph_client
*client
,
302 int *total
, int *avail
, int *used
, int *reserved
,
306 *total
= caps_total_count
;
308 *avail
= caps_avail_count
;
310 *used
= caps_use_count
;
312 *reserved
= caps_reserve_count
;
314 *min
= caps_min_count
;
318 * Find ceph_cap for given mds, if any.
320 * Called with i_lock held.
322 static struct ceph_cap
*__get_cap_for_mds(struct ceph_inode_info
*ci
, int mds
)
324 struct ceph_cap
*cap
;
325 struct rb_node
*n
= ci
->i_caps
.rb_node
;
328 cap
= rb_entry(n
, struct ceph_cap
, ci_node
);
331 else if (mds
> cap
->mds
)
340 * Return id of any MDS with a cap, preferably FILE_WR|WRBUFFER|EXCL, else
343 static int __ceph_get_cap_mds(struct ceph_inode_info
*ci
, u32
*mseq
)
345 struct ceph_cap
*cap
;
349 /* prefer mds with WR|WRBUFFER|EXCL caps */
350 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
351 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
355 if (cap
->issued
& (CEPH_CAP_FILE_WR
|
356 CEPH_CAP_FILE_BUFFER
|
363 int ceph_get_cap_mds(struct inode
*inode
)
366 spin_lock(&inode
->i_lock
);
367 mds
= __ceph_get_cap_mds(ceph_inode(inode
), NULL
);
368 spin_unlock(&inode
->i_lock
);
373 * Called under i_lock.
375 static void __insert_cap_node(struct ceph_inode_info
*ci
,
376 struct ceph_cap
*new)
378 struct rb_node
**p
= &ci
->i_caps
.rb_node
;
379 struct rb_node
*parent
= NULL
;
380 struct ceph_cap
*cap
= NULL
;
384 cap
= rb_entry(parent
, struct ceph_cap
, ci_node
);
385 if (new->mds
< cap
->mds
)
387 else if (new->mds
> cap
->mds
)
393 rb_link_node(&new->ci_node
, parent
, p
);
394 rb_insert_color(&new->ci_node
, &ci
->i_caps
);
398 * (re)set cap hold timeouts, which control the delayed release
399 * of unused caps back to the MDS. Should be called on cap use.
401 static void __cap_set_timeouts(struct ceph_mds_client
*mdsc
,
402 struct ceph_inode_info
*ci
)
404 struct ceph_mount_args
*ma
= mdsc
->client
->mount_args
;
406 ci
->i_hold_caps_min
= round_jiffies(jiffies
+
407 ma
->caps_wanted_delay_min
* HZ
);
408 ci
->i_hold_caps_max
= round_jiffies(jiffies
+
409 ma
->caps_wanted_delay_max
* HZ
);
410 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci
->vfs_inode
,
411 ci
->i_hold_caps_min
- jiffies
, ci
->i_hold_caps_max
- jiffies
);
415 * (Re)queue cap at the end of the delayed cap release list.
417 * If I_FLUSH is set, leave the inode at the front of the list.
419 * Caller holds i_lock
420 * -> we take mdsc->cap_delay_lock
422 static void __cap_delay_requeue(struct ceph_mds_client
*mdsc
,
423 struct ceph_inode_info
*ci
)
425 __cap_set_timeouts(mdsc
, ci
);
426 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci
->vfs_inode
,
427 ci
->i_ceph_flags
, ci
->i_hold_caps_max
);
428 if (!mdsc
->stopping
) {
429 spin_lock(&mdsc
->cap_delay_lock
);
430 if (!list_empty(&ci
->i_cap_delay_list
)) {
431 if (ci
->i_ceph_flags
& CEPH_I_FLUSH
)
433 list_del_init(&ci
->i_cap_delay_list
);
435 list_add_tail(&ci
->i_cap_delay_list
, &mdsc
->cap_delay_list
);
437 spin_unlock(&mdsc
->cap_delay_lock
);
442 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
443 * indicating we should send a cap message to flush dirty metadata
444 * asap, and move to the front of the delayed cap list.
446 static void __cap_delay_requeue_front(struct ceph_mds_client
*mdsc
,
447 struct ceph_inode_info
*ci
)
449 dout("__cap_delay_requeue_front %p\n", &ci
->vfs_inode
);
450 spin_lock(&mdsc
->cap_delay_lock
);
451 ci
->i_ceph_flags
|= CEPH_I_FLUSH
;
452 if (!list_empty(&ci
->i_cap_delay_list
))
453 list_del_init(&ci
->i_cap_delay_list
);
454 list_add(&ci
->i_cap_delay_list
, &mdsc
->cap_delay_list
);
455 spin_unlock(&mdsc
->cap_delay_lock
);
459 * Cancel delayed work on cap.
461 * Caller must hold i_lock.
463 static void __cap_delay_cancel(struct ceph_mds_client
*mdsc
,
464 struct ceph_inode_info
*ci
)
466 dout("__cap_delay_cancel %p\n", &ci
->vfs_inode
);
467 if (list_empty(&ci
->i_cap_delay_list
))
469 spin_lock(&mdsc
->cap_delay_lock
);
470 list_del_init(&ci
->i_cap_delay_list
);
471 spin_unlock(&mdsc
->cap_delay_lock
);
475 * Common issue checks for add_cap, handle_cap_grant.
477 static void __check_cap_issue(struct ceph_inode_info
*ci
, struct ceph_cap
*cap
,
480 unsigned had
= __ceph_caps_issued(ci
, NULL
);
483 * Each time we receive FILE_CACHE anew, we increment
486 if ((issued
& CEPH_CAP_FILE_CACHE
) &&
487 (had
& CEPH_CAP_FILE_CACHE
) == 0)
491 * if we are newly issued FILE_SHARED, clear I_COMPLETE; we
492 * don't know what happened to this directory while we didn't
495 if ((issued
& CEPH_CAP_FILE_SHARED
) &&
496 (had
& CEPH_CAP_FILE_SHARED
) == 0) {
498 if (S_ISDIR(ci
->vfs_inode
.i_mode
)) {
499 dout(" marking %p NOT complete\n", &ci
->vfs_inode
);
500 ci
->i_ceph_flags
&= ~CEPH_I_COMPLETE
;
506 * Add a capability under the given MDS session.
508 * Caller should hold session snap_rwsem (read) and s_mutex.
510 * @fmode is the open file mode, if we are opening a file, otherwise
511 * it is < 0. (This is so we can atomically add the cap and add an
512 * open file reference to it.)
514 int ceph_add_cap(struct inode
*inode
,
515 struct ceph_mds_session
*session
, u64 cap_id
,
516 int fmode
, unsigned issued
, unsigned wanted
,
517 unsigned seq
, unsigned mseq
, u64 realmino
, int flags
,
518 struct ceph_cap_reservation
*caps_reservation
)
520 struct ceph_mds_client
*mdsc
= &ceph_inode_to_client(inode
)->mdsc
;
521 struct ceph_inode_info
*ci
= ceph_inode(inode
);
522 struct ceph_cap
*new_cap
= NULL
;
523 struct ceph_cap
*cap
;
524 int mds
= session
->s_mds
;
527 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode
,
528 session
->s_mds
, cap_id
, ceph_cap_string(issued
), seq
);
531 * If we are opening the file, include file mode wanted bits
535 wanted
|= ceph_caps_for_mode(fmode
);
538 spin_lock(&inode
->i_lock
);
539 cap
= __get_cap_for_mds(ci
, mds
);
545 spin_unlock(&inode
->i_lock
);
546 new_cap
= get_cap(caps_reservation
);
553 cap
->implemented
= 0;
558 __insert_cap_node(ci
, cap
);
560 /* clear out old exporting info? (i.e. on cap import) */
561 if (ci
->i_cap_exporting_mds
== mds
) {
562 ci
->i_cap_exporting_issued
= 0;
563 ci
->i_cap_exporting_mseq
= 0;
564 ci
->i_cap_exporting_mds
= -1;
567 /* add to session cap list */
568 cap
->session
= session
;
569 spin_lock(&session
->s_cap_lock
);
570 list_add_tail(&cap
->session_caps
, &session
->s_caps
);
571 session
->s_nr_caps
++;
572 spin_unlock(&session
->s_cap_lock
);
575 if (!ci
->i_snap_realm
) {
577 * add this inode to the appropriate snap realm
579 struct ceph_snap_realm
*realm
= ceph_lookup_snap_realm(mdsc
,
582 ceph_get_snap_realm(mdsc
, realm
);
583 spin_lock(&realm
->inodes_with_caps_lock
);
584 ci
->i_snap_realm
= realm
;
585 list_add(&ci
->i_snap_realm_item
,
586 &realm
->inodes_with_caps
);
587 spin_unlock(&realm
->inodes_with_caps_lock
);
589 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
594 __check_cap_issue(ci
, cap
, issued
);
597 * If we are issued caps we don't want, or the mds' wanted
598 * value appears to be off, queue a check so we'll release
599 * later and/or update the mds wanted value.
601 actual_wanted
= __ceph_caps_wanted(ci
);
602 if ((wanted
& ~actual_wanted
) ||
603 (issued
& ~actual_wanted
& CEPH_CAP_ANY_WR
)) {
604 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
605 ceph_cap_string(issued
), ceph_cap_string(wanted
),
606 ceph_cap_string(actual_wanted
));
607 __cap_delay_requeue(mdsc
, ci
);
610 if (flags
& CEPH_CAP_FLAG_AUTH
)
611 ci
->i_auth_cap
= cap
;
612 else if (ci
->i_auth_cap
== cap
)
613 ci
->i_auth_cap
= NULL
;
615 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
616 inode
, ceph_vinop(inode
), cap
, ceph_cap_string(issued
),
617 ceph_cap_string(issued
|cap
->issued
), seq
, mds
);
618 cap
->cap_id
= cap_id
;
619 cap
->issued
= issued
;
620 cap
->implemented
|= issued
;
621 cap
->mds_wanted
|= wanted
;
623 cap
->issue_seq
= seq
;
625 cap
->cap_gen
= session
->s_cap_gen
;
628 __ceph_get_fmode(ci
, fmode
);
629 spin_unlock(&inode
->i_lock
);
630 wake_up_all(&ci
->i_cap_wq
);
635 * Return true if cap has not timed out and belongs to the current
636 * generation of the MDS session (i.e. has not gone 'stale' due to
637 * us losing touch with the mds).
639 static int __cap_is_valid(struct ceph_cap
*cap
)
644 spin_lock(&cap
->session
->s_cap_lock
);
645 gen
= cap
->session
->s_cap_gen
;
646 ttl
= cap
->session
->s_cap_ttl
;
647 spin_unlock(&cap
->session
->s_cap_lock
);
649 if (cap
->cap_gen
< gen
|| time_after_eq(jiffies
, ttl
)) {
650 dout("__cap_is_valid %p cap %p issued %s "
651 "but STALE (gen %u vs %u)\n", &cap
->ci
->vfs_inode
,
652 cap
, ceph_cap_string(cap
->issued
), cap
->cap_gen
, gen
);
660 * Return set of valid cap bits issued to us. Note that caps time
661 * out, and may be invalidated in bulk if the client session times out
662 * and session->s_cap_gen is bumped.
664 int __ceph_caps_issued(struct ceph_inode_info
*ci
, int *implemented
)
666 int have
= ci
->i_snap_caps
| ci
->i_cap_exporting_issued
;
667 struct ceph_cap
*cap
;
672 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
673 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
674 if (!__cap_is_valid(cap
))
676 dout("__ceph_caps_issued %p cap %p issued %s\n",
677 &ci
->vfs_inode
, cap
, ceph_cap_string(cap
->issued
));
680 *implemented
|= cap
->implemented
;
686 * Get cap bits issued by caps other than @ocap
688 int __ceph_caps_issued_other(struct ceph_inode_info
*ci
, struct ceph_cap
*ocap
)
690 int have
= ci
->i_snap_caps
;
691 struct ceph_cap
*cap
;
694 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
695 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
698 if (!__cap_is_valid(cap
))
706 * Move a cap to the end of the LRU (oldest caps at list head, newest
709 static void __touch_cap(struct ceph_cap
*cap
)
711 struct ceph_mds_session
*s
= cap
->session
;
713 spin_lock(&s
->s_cap_lock
);
714 if (s
->s_cap_iterator
== NULL
) {
715 dout("__touch_cap %p cap %p mds%d\n", &cap
->ci
->vfs_inode
, cap
,
717 list_move_tail(&cap
->session_caps
, &s
->s_caps
);
719 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
720 &cap
->ci
->vfs_inode
, cap
, s
->s_mds
);
722 spin_unlock(&s
->s_cap_lock
);
726 * Check if we hold the given mask. If so, move the cap(s) to the
727 * front of their respective LRUs. (This is the preferred way for
728 * callers to check for caps they want.)
730 int __ceph_caps_issued_mask(struct ceph_inode_info
*ci
, int mask
, int touch
)
732 struct ceph_cap
*cap
;
734 int have
= ci
->i_snap_caps
;
736 if ((have
& mask
) == mask
) {
737 dout("__ceph_caps_issued_mask %p snap issued %s"
738 " (mask %s)\n", &ci
->vfs_inode
,
739 ceph_cap_string(have
),
740 ceph_cap_string(mask
));
744 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
745 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
746 if (!__cap_is_valid(cap
))
748 if ((cap
->issued
& mask
) == mask
) {
749 dout("__ceph_caps_issued_mask %p cap %p issued %s"
750 " (mask %s)\n", &ci
->vfs_inode
, cap
,
751 ceph_cap_string(cap
->issued
),
752 ceph_cap_string(mask
));
758 /* does a combination of caps satisfy mask? */
760 if ((have
& mask
) == mask
) {
761 dout("__ceph_caps_issued_mask %p combo issued %s"
762 " (mask %s)\n", &ci
->vfs_inode
,
763 ceph_cap_string(cap
->issued
),
764 ceph_cap_string(mask
));
768 /* touch this + preceeding caps */
770 for (q
= rb_first(&ci
->i_caps
); q
!= p
;
772 cap
= rb_entry(q
, struct ceph_cap
,
774 if (!__cap_is_valid(cap
))
787 * Return true if mask caps are currently being revoked by an MDS.
789 int ceph_caps_revoking(struct ceph_inode_info
*ci
, int mask
)
791 struct inode
*inode
= &ci
->vfs_inode
;
792 struct ceph_cap
*cap
;
796 spin_lock(&inode
->i_lock
);
797 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
798 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
799 if (__cap_is_valid(cap
) &&
800 (cap
->implemented
& ~cap
->issued
& mask
)) {
805 spin_unlock(&inode
->i_lock
);
806 dout("ceph_caps_revoking %p %s = %d\n", inode
,
807 ceph_cap_string(mask
), ret
);
811 int __ceph_caps_used(struct ceph_inode_info
*ci
)
815 used
|= CEPH_CAP_PIN
;
817 used
|= CEPH_CAP_FILE_RD
;
818 if (ci
->i_rdcache_ref
|| ci
->i_rdcache_gen
)
819 used
|= CEPH_CAP_FILE_CACHE
;
821 used
|= CEPH_CAP_FILE_WR
;
822 if (ci
->i_wrbuffer_ref
)
823 used
|= CEPH_CAP_FILE_BUFFER
;
828 * wanted, by virtue of open file modes
830 int __ceph_caps_file_wanted(struct ceph_inode_info
*ci
)
834 for (mode
= 0; mode
< 4; mode
++)
835 if (ci
->i_nr_by_mode
[mode
])
836 want
|= ceph_caps_for_mode(mode
);
841 * Return caps we have registered with the MDS(s) as 'wanted'.
843 int __ceph_caps_mds_wanted(struct ceph_inode_info
*ci
)
845 struct ceph_cap
*cap
;
849 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
850 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
851 if (!__cap_is_valid(cap
))
853 mds_wanted
|= cap
->mds_wanted
;
859 * called under i_lock
861 static int __ceph_is_any_caps(struct ceph_inode_info
*ci
)
863 return !RB_EMPTY_ROOT(&ci
->i_caps
) || ci
->i_cap_exporting_mds
>= 0;
867 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
869 * caller should hold i_lock.
870 * caller will not hold session s_mutex if called from destroy_inode.
872 void __ceph_remove_cap(struct ceph_cap
*cap
)
874 struct ceph_mds_session
*session
= cap
->session
;
875 struct ceph_inode_info
*ci
= cap
->ci
;
876 struct ceph_mds_client
*mdsc
=
877 &ceph_sb_to_client(ci
->vfs_inode
.i_sb
)->mdsc
;
880 dout("__ceph_remove_cap %p from %p\n", cap
, &ci
->vfs_inode
);
882 /* remove from session list */
883 spin_lock(&session
->s_cap_lock
);
884 if (session
->s_cap_iterator
== cap
) {
885 /* not yet, we are iterating over this very cap */
886 dout("__ceph_remove_cap delaying %p removal from session %p\n",
889 list_del_init(&cap
->session_caps
);
890 session
->s_nr_caps
--;
894 /* protect backpointer with s_cap_lock: see iterate_session_caps */
896 spin_unlock(&session
->s_cap_lock
);
898 /* remove from inode list */
899 rb_erase(&cap
->ci_node
, &ci
->i_caps
);
900 if (ci
->i_auth_cap
== cap
)
901 ci
->i_auth_cap
= NULL
;
906 if (!__ceph_is_any_caps(ci
) && ci
->i_snap_realm
) {
907 struct ceph_snap_realm
*realm
= ci
->i_snap_realm
;
908 spin_lock(&realm
->inodes_with_caps_lock
);
909 list_del_init(&ci
->i_snap_realm_item
);
910 ci
->i_snap_realm_counter
++;
911 ci
->i_snap_realm
= NULL
;
912 spin_unlock(&realm
->inodes_with_caps_lock
);
913 ceph_put_snap_realm(mdsc
, realm
);
915 if (!__ceph_is_any_real_caps(ci
))
916 __cap_delay_cancel(mdsc
, ci
);
920 * Build and send a cap message to the given MDS.
922 * Caller should be holding s_mutex.
924 static int send_cap_msg(struct ceph_mds_session
*session
,
925 u64 ino
, u64 cid
, int op
,
926 int caps
, int wanted
, int dirty
,
927 u32 seq
, u64 flush_tid
, u32 issue_seq
, u32 mseq
,
928 u64 size
, u64 max_size
,
929 struct timespec
*mtime
, struct timespec
*atime
,
931 uid_t uid
, gid_t gid
, mode_t mode
,
933 struct ceph_buffer
*xattrs_buf
,
936 struct ceph_mds_caps
*fc
;
937 struct ceph_msg
*msg
;
939 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
940 " seq %u/%u mseq %u follows %lld size %llu/%llu"
941 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op
),
942 cid
, ino
, ceph_cap_string(caps
), ceph_cap_string(wanted
),
943 ceph_cap_string(dirty
),
944 seq
, issue_seq
, mseq
, follows
, size
, max_size
,
945 xattr_version
, xattrs_buf
? (int)xattrs_buf
->vec
.iov_len
: 0);
947 msg
= ceph_msg_new(CEPH_MSG_CLIENT_CAPS
, sizeof(*fc
), GFP_NOFS
);
951 msg
->hdr
.tid
= cpu_to_le64(flush_tid
);
953 fc
= msg
->front
.iov_base
;
954 memset(fc
, 0, sizeof(*fc
));
956 fc
->cap_id
= cpu_to_le64(cid
);
957 fc
->op
= cpu_to_le32(op
);
958 fc
->seq
= cpu_to_le32(seq
);
959 fc
->issue_seq
= cpu_to_le32(issue_seq
);
960 fc
->migrate_seq
= cpu_to_le32(mseq
);
961 fc
->caps
= cpu_to_le32(caps
);
962 fc
->wanted
= cpu_to_le32(wanted
);
963 fc
->dirty
= cpu_to_le32(dirty
);
964 fc
->ino
= cpu_to_le64(ino
);
965 fc
->snap_follows
= cpu_to_le64(follows
);
967 fc
->size
= cpu_to_le64(size
);
968 fc
->max_size
= cpu_to_le64(max_size
);
970 ceph_encode_timespec(&fc
->mtime
, mtime
);
972 ceph_encode_timespec(&fc
->atime
, atime
);
973 fc
->time_warp_seq
= cpu_to_le32(time_warp_seq
);
975 fc
->uid
= cpu_to_le32(uid
);
976 fc
->gid
= cpu_to_le32(gid
);
977 fc
->mode
= cpu_to_le32(mode
);
979 fc
->xattr_version
= cpu_to_le64(xattr_version
);
981 msg
->middle
= ceph_buffer_get(xattrs_buf
);
982 fc
->xattr_len
= cpu_to_le32(xattrs_buf
->vec
.iov_len
);
983 msg
->hdr
.middle_len
= cpu_to_le32(xattrs_buf
->vec
.iov_len
);
986 ceph_con_send(&session
->s_con
, msg
);
990 static void __queue_cap_release(struct ceph_mds_session
*session
,
991 u64 ino
, u64 cap_id
, u32 migrate_seq
,
994 struct ceph_msg
*msg
;
995 struct ceph_mds_cap_release
*head
;
996 struct ceph_mds_cap_item
*item
;
998 spin_lock(&session
->s_cap_lock
);
999 BUG_ON(!session
->s_num_cap_releases
);
1000 msg
= list_first_entry(&session
->s_cap_releases
,
1001 struct ceph_msg
, list_head
);
1003 dout(" adding %llx release to mds%d msg %p (%d left)\n",
1004 ino
, session
->s_mds
, msg
, session
->s_num_cap_releases
);
1006 BUG_ON(msg
->front
.iov_len
+ sizeof(*item
) > PAGE_CACHE_SIZE
);
1007 head
= msg
->front
.iov_base
;
1008 head
->num
= cpu_to_le32(le32_to_cpu(head
->num
) + 1);
1009 item
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1010 item
->ino
= cpu_to_le64(ino
);
1011 item
->cap_id
= cpu_to_le64(cap_id
);
1012 item
->migrate_seq
= cpu_to_le32(migrate_seq
);
1013 item
->seq
= cpu_to_le32(issue_seq
);
1015 session
->s_num_cap_releases
--;
1017 msg
->front
.iov_len
+= sizeof(*item
);
1018 if (le32_to_cpu(head
->num
) == CEPH_CAPS_PER_RELEASE
) {
1019 dout(" release msg %p full\n", msg
);
1020 list_move_tail(&msg
->list_head
, &session
->s_cap_releases_done
);
1022 dout(" release msg %p at %d/%d (%d)\n", msg
,
1023 (int)le32_to_cpu(head
->num
),
1024 (int)CEPH_CAPS_PER_RELEASE
,
1025 (int)msg
->front
.iov_len
);
1027 spin_unlock(&session
->s_cap_lock
);
1031 * Queue cap releases when an inode is dropped from our cache. Since
1032 * inode is about to be destroyed, there is no need for i_lock.
1034 void ceph_queue_caps_release(struct inode
*inode
)
1036 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1039 p
= rb_first(&ci
->i_caps
);
1041 struct ceph_cap
*cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
1042 struct ceph_mds_session
*session
= cap
->session
;
1044 __queue_cap_release(session
, ceph_ino(inode
), cap
->cap_id
,
1045 cap
->mseq
, cap
->issue_seq
);
1047 __ceph_remove_cap(cap
);
1052 * Send a cap msg on the given inode. Update our caps state, then
1053 * drop i_lock and send the message.
1055 * Make note of max_size reported/requested from mds, revoked caps
1056 * that have now been implemented.
1058 * Make half-hearted attempt ot to invalidate page cache if we are
1059 * dropping RDCACHE. Note that this will leave behind locked pages
1060 * that we'll then need to deal with elsewhere.
1062 * Return non-zero if delayed release, or we experienced an error
1063 * such that the caller should requeue + retry later.
1065 * called with i_lock, then drops it.
1066 * caller should hold snap_rwsem (read), s_mutex.
1068 static int __send_cap(struct ceph_mds_client
*mdsc
, struct ceph_cap
*cap
,
1069 int op
, int used
, int want
, int retain
, int flushing
,
1070 unsigned *pflush_tid
)
1071 __releases(cap
->ci
->vfs_inode
->i_lock
)
1073 struct ceph_inode_info
*ci
= cap
->ci
;
1074 struct inode
*inode
= &ci
->vfs_inode
;
1075 u64 cap_id
= cap
->cap_id
;
1076 int held
, revoking
, dropping
, keep
;
1077 u64 seq
, issue_seq
, mseq
, time_warp_seq
, follows
;
1079 struct timespec mtime
, atime
;
1084 struct ceph_mds_session
*session
;
1085 u64 xattr_version
= 0;
1091 held
= cap
->issued
| cap
->implemented
;
1092 revoking
= cap
->implemented
& ~cap
->issued
;
1093 retain
&= ~revoking
;
1094 dropping
= cap
->issued
& ~retain
;
1096 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1097 inode
, cap
, cap
->session
,
1098 ceph_cap_string(held
), ceph_cap_string(held
& retain
),
1099 ceph_cap_string(revoking
));
1100 BUG_ON((retain
& CEPH_CAP_PIN
) == 0);
1102 session
= cap
->session
;
1104 /* don't release wanted unless we've waited a bit. */
1105 if ((ci
->i_ceph_flags
& CEPH_I_NODELAY
) == 0 &&
1106 time_before(jiffies
, ci
->i_hold_caps_min
)) {
1107 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1108 ceph_cap_string(cap
->issued
),
1109 ceph_cap_string(cap
->issued
& retain
),
1110 ceph_cap_string(cap
->mds_wanted
),
1111 ceph_cap_string(want
));
1112 want
|= cap
->mds_wanted
;
1113 retain
|= cap
->issued
;
1116 ci
->i_ceph_flags
&= ~(CEPH_I_NODELAY
| CEPH_I_FLUSH
);
1118 cap
->issued
&= retain
; /* drop bits we don't want */
1119 if (cap
->implemented
& ~cap
->issued
) {
1121 * Wake up any waiters on wanted -> needed transition.
1122 * This is due to the weird transition from buffered
1123 * to sync IO... we need to flush dirty pages _before_
1124 * allowing sync writes to avoid reordering.
1128 cap
->implemented
&= cap
->issued
| used
;
1129 cap
->mds_wanted
= want
;
1133 * assign a tid for flush operations so we can avoid
1134 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1135 * clean type races. track latest tid for every bit
1136 * so we can handle flush AxFw, flush Fw, and have the
1137 * first ack clean Ax.
1139 flush_tid
= ++ci
->i_cap_flush_last_tid
;
1141 *pflush_tid
= flush_tid
;
1142 dout(" cap_flush_tid %d\n", (int)flush_tid
);
1143 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
1144 if (flushing
& (1 << i
))
1145 ci
->i_cap_flush_tid
[i
] = flush_tid
;
1148 keep
= cap
->implemented
;
1150 issue_seq
= cap
->issue_seq
;
1152 size
= inode
->i_size
;
1153 ci
->i_reported_size
= size
;
1154 max_size
= ci
->i_wanted_max_size
;
1155 ci
->i_requested_max_size
= max_size
;
1156 mtime
= inode
->i_mtime
;
1157 atime
= inode
->i_atime
;
1158 time_warp_seq
= ci
->i_time_warp_seq
;
1159 follows
= ci
->i_snap_realm
->cached_context
->seq
;
1162 mode
= inode
->i_mode
;
1164 if (dropping
& CEPH_CAP_XATTR_EXCL
) {
1165 __ceph_build_xattrs_blob(ci
);
1166 xattr_version
= ci
->i_xattrs
.version
+ 1;
1169 spin_unlock(&inode
->i_lock
);
1171 ret
= send_cap_msg(session
, ceph_vino(inode
).ino
, cap_id
,
1172 op
, keep
, want
, flushing
, seq
, flush_tid
, issue_seq
, mseq
,
1173 size
, max_size
, &mtime
, &atime
, time_warp_seq
,
1176 (flushing
& CEPH_CAP_XATTR_EXCL
) ? ci
->i_xattrs
.blob
: NULL
,
1179 dout("error sending cap msg, must requeue %p\n", inode
);
1184 wake_up_all(&ci
->i_cap_wq
);
1190 * When a snapshot is taken, clients accumulate dirty metadata on
1191 * inodes with capabilities in ceph_cap_snaps to describe the file
1192 * state at the time the snapshot was taken. This must be flushed
1193 * asynchronously back to the MDS once sync writes complete and dirty
1194 * data is written out.
1196 * Called under i_lock. Takes s_mutex as needed.
1198 void __ceph_flush_snaps(struct ceph_inode_info
*ci
,
1199 struct ceph_mds_session
**psession
)
1201 struct inode
*inode
= &ci
->vfs_inode
;
1203 struct ceph_cap_snap
*capsnap
;
1205 struct ceph_mds_client
*mdsc
= &ceph_inode_to_client(inode
)->mdsc
;
1206 struct ceph_mds_session
*session
= NULL
; /* if session != NULL, we hold
1208 u64 next_follows
= 0; /* keep track of how far we've gotten through the
1209 i_cap_snaps list, and skip these entries next time
1210 around to avoid an infinite loop */
1213 session
= *psession
;
1215 dout("__flush_snaps %p\n", inode
);
1217 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
1218 /* avoid an infiniute loop after retry */
1219 if (capsnap
->follows
< next_follows
)
1222 * we need to wait for sync writes to complete and for dirty
1223 * pages to be written out.
1225 if (capsnap
->dirty_pages
|| capsnap
->writing
)
1229 * if cap writeback already occurred, we should have dropped
1230 * the capsnap in ceph_put_wrbuffer_cap_refs.
1232 BUG_ON(capsnap
->dirty
== 0);
1234 /* pick mds, take s_mutex */
1235 mds
= __ceph_get_cap_mds(ci
, &mseq
);
1236 if (session
&& session
->s_mds
!= mds
) {
1237 dout("oops, wrong session %p mutex\n", session
);
1238 mutex_unlock(&session
->s_mutex
);
1239 ceph_put_mds_session(session
);
1243 spin_unlock(&inode
->i_lock
);
1244 mutex_lock(&mdsc
->mutex
);
1245 session
= __ceph_lookup_mds_session(mdsc
, mds
);
1246 mutex_unlock(&mdsc
->mutex
);
1248 dout("inverting session/ino locks on %p\n",
1250 mutex_lock(&session
->s_mutex
);
1253 * if session == NULL, we raced against a cap
1254 * deletion. retry, and we'll get a better
1255 * @mds value next time.
1257 spin_lock(&inode
->i_lock
);
1261 capsnap
->flush_tid
= ++ci
->i_cap_flush_last_tid
;
1262 atomic_inc(&capsnap
->nref
);
1263 if (!list_empty(&capsnap
->flushing_item
))
1264 list_del_init(&capsnap
->flushing_item
);
1265 list_add_tail(&capsnap
->flushing_item
,
1266 &session
->s_cap_snaps_flushing
);
1267 spin_unlock(&inode
->i_lock
);
1269 dout("flush_snaps %p cap_snap %p follows %lld size %llu\n",
1270 inode
, capsnap
, next_follows
, capsnap
->size
);
1271 send_cap_msg(session
, ceph_vino(inode
).ino
, 0,
1272 CEPH_CAP_OP_FLUSHSNAP
, capsnap
->issued
, 0,
1273 capsnap
->dirty
, 0, capsnap
->flush_tid
, 0, mseq
,
1275 &capsnap
->mtime
, &capsnap
->atime
,
1276 capsnap
->time_warp_seq
,
1277 capsnap
->uid
, capsnap
->gid
, capsnap
->mode
,
1281 next_follows
= capsnap
->follows
+ 1;
1282 ceph_put_cap_snap(capsnap
);
1284 spin_lock(&inode
->i_lock
);
1288 /* we flushed them all; remove this inode from the queue */
1289 spin_lock(&mdsc
->snap_flush_lock
);
1290 list_del_init(&ci
->i_snap_flush_item
);
1291 spin_unlock(&mdsc
->snap_flush_lock
);
1294 *psession
= session
;
1296 mutex_unlock(&session
->s_mutex
);
1297 ceph_put_mds_session(session
);
1301 static void ceph_flush_snaps(struct ceph_inode_info
*ci
)
1303 struct inode
*inode
= &ci
->vfs_inode
;
1305 spin_lock(&inode
->i_lock
);
1306 __ceph_flush_snaps(ci
, NULL
);
1307 spin_unlock(&inode
->i_lock
);
1311 * Mark caps dirty. If inode is newly dirty, add to the global dirty
1314 void __ceph_mark_dirty_caps(struct ceph_inode_info
*ci
, int mask
)
1316 struct ceph_mds_client
*mdsc
=
1317 &ceph_sb_to_client(ci
->vfs_inode
.i_sb
)->mdsc
;
1318 struct inode
*inode
= &ci
->vfs_inode
;
1319 int was
= ci
->i_dirty_caps
;
1322 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci
->vfs_inode
,
1323 ceph_cap_string(mask
), ceph_cap_string(was
),
1324 ceph_cap_string(was
| mask
));
1325 ci
->i_dirty_caps
|= mask
;
1327 dout(" inode %p now dirty\n", &ci
->vfs_inode
);
1328 BUG_ON(!list_empty(&ci
->i_dirty_item
));
1329 spin_lock(&mdsc
->cap_dirty_lock
);
1330 list_add(&ci
->i_dirty_item
, &mdsc
->cap_dirty
);
1331 spin_unlock(&mdsc
->cap_dirty_lock
);
1332 if (ci
->i_flushing_caps
== 0) {
1334 dirty
|= I_DIRTY_SYNC
;
1337 BUG_ON(list_empty(&ci
->i_dirty_item
));
1338 if (((was
| ci
->i_flushing_caps
) & CEPH_CAP_FILE_BUFFER
) &&
1339 (mask
& CEPH_CAP_FILE_BUFFER
))
1340 dirty
|= I_DIRTY_DATASYNC
;
1342 __mark_inode_dirty(inode
, dirty
);
1343 __cap_delay_requeue(mdsc
, ci
);
1347 * Add dirty inode to the flushing list. Assigned a seq number so we
1348 * can wait for caps to flush without starving.
1350 * Called under i_lock.
1352 static int __mark_caps_flushing(struct inode
*inode
,
1353 struct ceph_mds_session
*session
)
1355 struct ceph_mds_client
*mdsc
= &ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1356 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1359 BUG_ON(ci
->i_dirty_caps
== 0);
1360 BUG_ON(list_empty(&ci
->i_dirty_item
));
1362 flushing
= ci
->i_dirty_caps
;
1363 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1364 ceph_cap_string(flushing
),
1365 ceph_cap_string(ci
->i_flushing_caps
),
1366 ceph_cap_string(ci
->i_flushing_caps
| flushing
));
1367 ci
->i_flushing_caps
|= flushing
;
1368 ci
->i_dirty_caps
= 0;
1369 dout(" inode %p now !dirty\n", inode
);
1371 spin_lock(&mdsc
->cap_dirty_lock
);
1372 list_del_init(&ci
->i_dirty_item
);
1374 ci
->i_cap_flush_seq
= ++mdsc
->cap_flush_seq
;
1375 if (list_empty(&ci
->i_flushing_item
)) {
1376 list_add_tail(&ci
->i_flushing_item
, &session
->s_cap_flushing
);
1377 mdsc
->num_cap_flushing
++;
1378 dout(" inode %p now flushing seq %lld\n", inode
,
1379 ci
->i_cap_flush_seq
);
1381 list_move_tail(&ci
->i_flushing_item
, &session
->s_cap_flushing
);
1382 dout(" inode %p now flushing (more) seq %lld\n", inode
,
1383 ci
->i_cap_flush_seq
);
1385 spin_unlock(&mdsc
->cap_dirty_lock
);
1391 * try to invalidate mapping pages without blocking.
1393 static int mapping_is_empty(struct address_space
*mapping
)
1395 struct page
*page
= find_get_page(mapping
, 0);
1404 static int try_nonblocking_invalidate(struct inode
*inode
)
1406 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1407 u32 invalidating_gen
= ci
->i_rdcache_gen
;
1409 spin_unlock(&inode
->i_lock
);
1410 invalidate_mapping_pages(&inode
->i_data
, 0, -1);
1411 spin_lock(&inode
->i_lock
);
1413 if (mapping_is_empty(&inode
->i_data
) &&
1414 invalidating_gen
== ci
->i_rdcache_gen
) {
1416 dout("try_nonblocking_invalidate %p success\n", inode
);
1417 ci
->i_rdcache_gen
= 0;
1418 ci
->i_rdcache_revoking
= 0;
1421 dout("try_nonblocking_invalidate %p failed\n", inode
);
1426 * Swiss army knife function to examine currently used and wanted
1427 * versus held caps. Release, flush, ack revoked caps to mds as
1430 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1431 * cap release further.
1432 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1433 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1436 void ceph_check_caps(struct ceph_inode_info
*ci
, int flags
,
1437 struct ceph_mds_session
*session
)
1438 __releases(session
->s_mutex
)
1440 struct ceph_client
*client
= ceph_inode_to_client(&ci
->vfs_inode
);
1441 struct ceph_mds_client
*mdsc
= &client
->mdsc
;
1442 struct inode
*inode
= &ci
->vfs_inode
;
1443 struct ceph_cap
*cap
;
1444 int file_wanted
, used
;
1445 int took_snap_rwsem
= 0; /* true if mdsc->snap_rwsem held */
1446 int issued
, implemented
, want
, retain
, revoking
, flushing
= 0;
1447 int mds
= -1; /* keep track of how far we've gone through i_caps list
1448 to avoid an infinite loop on retry */
1450 int tried_invalidate
= 0;
1451 int delayed
= 0, sent
= 0, force_requeue
= 0, num
;
1452 int queue_invalidate
= 0;
1453 int is_delayed
= flags
& CHECK_CAPS_NODELAY
;
1455 /* if we are unmounting, flush any unused caps immediately. */
1459 spin_lock(&inode
->i_lock
);
1461 if (ci
->i_ceph_flags
& CEPH_I_FLUSH
)
1462 flags
|= CHECK_CAPS_FLUSH
;
1464 /* flush snaps first time around only */
1465 if (!list_empty(&ci
->i_cap_snaps
))
1466 __ceph_flush_snaps(ci
, &session
);
1469 spin_lock(&inode
->i_lock
);
1471 file_wanted
= __ceph_caps_file_wanted(ci
);
1472 used
= __ceph_caps_used(ci
);
1473 want
= file_wanted
| used
;
1474 issued
= __ceph_caps_issued(ci
, &implemented
);
1475 revoking
= implemented
& ~issued
;
1477 retain
= want
| CEPH_CAP_PIN
;
1478 if (!mdsc
->stopping
&& inode
->i_nlink
> 0) {
1480 retain
|= CEPH_CAP_ANY
; /* be greedy */
1482 retain
|= CEPH_CAP_ANY_SHARED
;
1484 * keep RD only if we didn't have the file open RW,
1485 * because then the mds would revoke it anyway to
1486 * journal max_size=0.
1488 if (ci
->i_max_size
== 0)
1489 retain
|= CEPH_CAP_ANY_RD
;
1493 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1494 " issued %s revoking %s retain %s %s%s%s\n", inode
,
1495 ceph_cap_string(file_wanted
),
1496 ceph_cap_string(used
), ceph_cap_string(ci
->i_dirty_caps
),
1497 ceph_cap_string(ci
->i_flushing_caps
),
1498 ceph_cap_string(issued
), ceph_cap_string(revoking
),
1499 ceph_cap_string(retain
),
1500 (flags
& CHECK_CAPS_AUTHONLY
) ? " AUTHONLY" : "",
1501 (flags
& CHECK_CAPS_NODELAY
) ? " NODELAY" : "",
1502 (flags
& CHECK_CAPS_FLUSH
) ? " FLUSH" : "");
1505 * If we no longer need to hold onto old our caps, and we may
1506 * have cached pages, but don't want them, then try to invalidate.
1507 * If we fail, it's because pages are locked.... try again later.
1509 if ((!is_delayed
|| mdsc
->stopping
) &&
1510 ci
->i_wrbuffer_ref
== 0 && /* no dirty pages... */
1511 ci
->i_rdcache_gen
&& /* may have cached pages */
1512 (file_wanted
== 0 || /* no open files */
1513 (revoking
& CEPH_CAP_FILE_CACHE
)) && /* or revoking cache */
1514 !tried_invalidate
) {
1515 dout("check_caps trying to invalidate on %p\n", inode
);
1516 if (try_nonblocking_invalidate(inode
) < 0) {
1517 if (revoking
& CEPH_CAP_FILE_CACHE
) {
1518 dout("check_caps queuing invalidate\n");
1519 queue_invalidate
= 1;
1520 ci
->i_rdcache_revoking
= ci
->i_rdcache_gen
;
1522 dout("check_caps failed to invalidate pages\n");
1523 /* we failed to invalidate pages. check these
1524 caps again later. */
1526 __cap_set_timeouts(mdsc
, ci
);
1529 tried_invalidate
= 1;
1534 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
1535 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
1538 /* avoid looping forever */
1539 if (mds
>= cap
->mds
||
1540 ((flags
& CHECK_CAPS_AUTHONLY
) && cap
!= ci
->i_auth_cap
))
1543 /* NOTE: no side-effects allowed, until we take s_mutex */
1545 revoking
= cap
->implemented
& ~cap
->issued
;
1547 dout(" mds%d revoking %s\n", cap
->mds
,
1548 ceph_cap_string(revoking
));
1550 if (cap
== ci
->i_auth_cap
&&
1551 (cap
->issued
& CEPH_CAP_FILE_WR
)) {
1552 /* request larger max_size from MDS? */
1553 if (ci
->i_wanted_max_size
> ci
->i_max_size
&&
1554 ci
->i_wanted_max_size
> ci
->i_requested_max_size
) {
1555 dout("requesting new max_size\n");
1559 /* approaching file_max? */
1560 if ((inode
->i_size
<< 1) >= ci
->i_max_size
&&
1561 (ci
->i_reported_size
<< 1) < ci
->i_max_size
) {
1562 dout("i_size approaching max_size\n");
1566 /* flush anything dirty? */
1567 if (cap
== ci
->i_auth_cap
&& (flags
& CHECK_CAPS_FLUSH
) &&
1569 dout("flushing dirty caps\n");
1573 /* completed revocation? going down and there are no caps? */
1574 if (revoking
&& (revoking
& used
) == 0) {
1575 dout("completed revocation of %s\n",
1576 ceph_cap_string(cap
->implemented
& ~cap
->issued
));
1580 /* want more caps from mds? */
1581 if (want
& ~(cap
->mds_wanted
| cap
->issued
))
1584 /* things we might delay */
1585 if ((cap
->issued
& ~retain
) == 0 &&
1586 cap
->mds_wanted
== want
)
1587 continue; /* nope, all good */
1593 if ((ci
->i_ceph_flags
& CEPH_I_NODELAY
) == 0 &&
1594 time_before(jiffies
, ci
->i_hold_caps_max
)) {
1595 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1596 ceph_cap_string(cap
->issued
),
1597 ceph_cap_string(cap
->issued
& retain
),
1598 ceph_cap_string(cap
->mds_wanted
),
1599 ceph_cap_string(want
));
1605 if (ci
->i_ceph_flags
& CEPH_I_NOFLUSH
) {
1606 dout(" skipping %p I_NOFLUSH set\n", inode
);
1610 if (session
&& session
!= cap
->session
) {
1611 dout("oops, wrong session %p mutex\n", session
);
1612 mutex_unlock(&session
->s_mutex
);
1616 session
= cap
->session
;
1617 if (mutex_trylock(&session
->s_mutex
) == 0) {
1618 dout("inverting session/ino locks on %p\n",
1620 spin_unlock(&inode
->i_lock
);
1621 if (took_snap_rwsem
) {
1622 up_read(&mdsc
->snap_rwsem
);
1623 took_snap_rwsem
= 0;
1625 mutex_lock(&session
->s_mutex
);
1629 /* take snap_rwsem after session mutex */
1630 if (!took_snap_rwsem
) {
1631 if (down_read_trylock(&mdsc
->snap_rwsem
) == 0) {
1632 dout("inverting snap/in locks on %p\n",
1634 spin_unlock(&inode
->i_lock
);
1635 down_read(&mdsc
->snap_rwsem
);
1636 took_snap_rwsem
= 1;
1639 took_snap_rwsem
= 1;
1642 if (cap
== ci
->i_auth_cap
&& ci
->i_dirty_caps
)
1643 flushing
= __mark_caps_flushing(inode
, session
);
1645 mds
= cap
->mds
; /* remember mds, so we don't repeat */
1648 /* __send_cap drops i_lock */
1649 delayed
+= __send_cap(mdsc
, cap
, CEPH_CAP_OP_UPDATE
, used
, want
,
1650 retain
, flushing
, NULL
);
1651 goto retry
; /* retake i_lock and restart our cap scan. */
1655 * Reschedule delayed caps release if we delayed anything,
1658 if (delayed
&& is_delayed
)
1659 force_requeue
= 1; /* __send_cap delayed release; requeue */
1660 if (!delayed
&& !is_delayed
)
1661 __cap_delay_cancel(mdsc
, ci
);
1662 else if (!is_delayed
|| force_requeue
)
1663 __cap_delay_requeue(mdsc
, ci
);
1665 spin_unlock(&inode
->i_lock
);
1667 if (queue_invalidate
)
1668 ceph_queue_invalidate(inode
);
1671 mutex_unlock(&session
->s_mutex
);
1672 if (took_snap_rwsem
)
1673 up_read(&mdsc
->snap_rwsem
);
1677 * Try to flush dirty caps back to the auth mds.
1679 static int try_flush_caps(struct inode
*inode
, struct ceph_mds_session
*session
,
1680 unsigned *flush_tid
)
1682 struct ceph_mds_client
*mdsc
= &ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1683 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1684 int unlock_session
= session
? 0 : 1;
1688 spin_lock(&inode
->i_lock
);
1689 if (ci
->i_ceph_flags
& CEPH_I_NOFLUSH
) {
1690 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode
);
1693 if (ci
->i_dirty_caps
&& ci
->i_auth_cap
) {
1694 struct ceph_cap
*cap
= ci
->i_auth_cap
;
1695 int used
= __ceph_caps_used(ci
);
1696 int want
= __ceph_caps_wanted(ci
);
1700 spin_unlock(&inode
->i_lock
);
1701 session
= cap
->session
;
1702 mutex_lock(&session
->s_mutex
);
1705 BUG_ON(session
!= cap
->session
);
1706 if (cap
->session
->s_state
< CEPH_MDS_SESSION_OPEN
)
1709 flushing
= __mark_caps_flushing(inode
, session
);
1711 /* __send_cap drops i_lock */
1712 delayed
= __send_cap(mdsc
, cap
, CEPH_CAP_OP_FLUSH
, used
, want
,
1713 cap
->issued
| cap
->implemented
, flushing
,
1718 spin_lock(&inode
->i_lock
);
1719 __cap_delay_requeue(mdsc
, ci
);
1722 spin_unlock(&inode
->i_lock
);
1724 if (session
&& unlock_session
)
1725 mutex_unlock(&session
->s_mutex
);
1730 * Return true if we've flushed caps through the given flush_tid.
1732 static int caps_are_flushed(struct inode
*inode
, unsigned tid
)
1734 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1737 spin_lock(&inode
->i_lock
);
1738 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
1739 if ((ci
->i_flushing_caps
& (1 << i
)) &&
1740 ci
->i_cap_flush_tid
[i
] <= tid
) {
1741 /* still flushing this bit */
1745 spin_unlock(&inode
->i_lock
);
1750 * Wait on any unsafe replies for the given inode. First wait on the
1751 * newest request, and make that the upper bound. Then, if there are
1752 * more requests, keep waiting on the oldest as long as it is still older
1753 * than the original request.
1755 static void sync_write_wait(struct inode
*inode
)
1757 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1758 struct list_head
*head
= &ci
->i_unsafe_writes
;
1759 struct ceph_osd_request
*req
;
1762 spin_lock(&ci
->i_unsafe_lock
);
1763 if (list_empty(head
))
1766 /* set upper bound as _last_ entry in chain */
1767 req
= list_entry(head
->prev
, struct ceph_osd_request
,
1769 last_tid
= req
->r_tid
;
1772 ceph_osdc_get_request(req
);
1773 spin_unlock(&ci
->i_unsafe_lock
);
1774 dout("sync_write_wait on tid %llu (until %llu)\n",
1775 req
->r_tid
, last_tid
);
1776 wait_for_completion(&req
->r_safe_completion
);
1777 spin_lock(&ci
->i_unsafe_lock
);
1778 ceph_osdc_put_request(req
);
1781 * from here on look at first entry in chain, since we
1782 * only want to wait for anything older than last_tid
1784 if (list_empty(head
))
1786 req
= list_entry(head
->next
, struct ceph_osd_request
,
1788 } while (req
->r_tid
< last_tid
);
1790 spin_unlock(&ci
->i_unsafe_lock
);
1793 int ceph_fsync(struct file
*file
, int datasync
)
1795 struct inode
*inode
= file
->f_mapping
->host
;
1796 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1801 dout("fsync %p%s\n", inode
, datasync
? " datasync" : "");
1802 sync_write_wait(inode
);
1804 ret
= filemap_write_and_wait(inode
->i_mapping
);
1808 dirty
= try_flush_caps(inode
, NULL
, &flush_tid
);
1809 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty
));
1812 * only wait on non-file metadata writeback (the mds
1813 * can recover size and mtime, so we don't need to
1816 if (!datasync
&& (dirty
& ~CEPH_CAP_ANY_FILE_WR
)) {
1817 dout("fsync waiting for flush_tid %u\n", flush_tid
);
1818 ret
= wait_event_interruptible(ci
->i_cap_wq
,
1819 caps_are_flushed(inode
, flush_tid
));
1822 dout("fsync %p%s done\n", inode
, datasync
? " datasync" : "");
1827 * Flush any dirty caps back to the mds. If we aren't asked to wait,
1828 * queue inode for flush but don't do so immediately, because we can
1829 * get by with fewer MDS messages if we wait for data writeback to
1832 int ceph_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1834 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1838 int wait
= wbc
->sync_mode
== WB_SYNC_ALL
;
1840 dout("write_inode %p wait=%d\n", inode
, wait
);
1842 dirty
= try_flush_caps(inode
, NULL
, &flush_tid
);
1844 err
= wait_event_interruptible(ci
->i_cap_wq
,
1845 caps_are_flushed(inode
, flush_tid
));
1847 struct ceph_mds_client
*mdsc
=
1848 &ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1850 spin_lock(&inode
->i_lock
);
1851 if (__ceph_caps_dirty(ci
))
1852 __cap_delay_requeue_front(mdsc
, ci
);
1853 spin_unlock(&inode
->i_lock
);
1859 * After a recovering MDS goes active, we need to resend any caps
1862 * Caller holds session->s_mutex.
1864 static void kick_flushing_capsnaps(struct ceph_mds_client
*mdsc
,
1865 struct ceph_mds_session
*session
)
1867 struct ceph_cap_snap
*capsnap
;
1869 dout("kick_flushing_capsnaps mds%d\n", session
->s_mds
);
1870 list_for_each_entry(capsnap
, &session
->s_cap_snaps_flushing
,
1872 struct ceph_inode_info
*ci
= capsnap
->ci
;
1873 struct inode
*inode
= &ci
->vfs_inode
;
1874 struct ceph_cap
*cap
;
1876 spin_lock(&inode
->i_lock
);
1877 cap
= ci
->i_auth_cap
;
1878 if (cap
&& cap
->session
== session
) {
1879 dout("kick_flushing_caps %p cap %p capsnap %p\n", inode
,
1881 __ceph_flush_snaps(ci
, &session
);
1883 pr_err("%p auth cap %p not mds%d ???\n", inode
,
1884 cap
, session
->s_mds
);
1886 spin_unlock(&inode
->i_lock
);
1890 void ceph_kick_flushing_caps(struct ceph_mds_client
*mdsc
,
1891 struct ceph_mds_session
*session
)
1893 struct ceph_inode_info
*ci
;
1895 kick_flushing_capsnaps(mdsc
, session
);
1897 dout("kick_flushing_caps mds%d\n", session
->s_mds
);
1898 list_for_each_entry(ci
, &session
->s_cap_flushing
, i_flushing_item
) {
1899 struct inode
*inode
= &ci
->vfs_inode
;
1900 struct ceph_cap
*cap
;
1903 spin_lock(&inode
->i_lock
);
1904 cap
= ci
->i_auth_cap
;
1905 if (cap
&& cap
->session
== session
) {
1906 dout("kick_flushing_caps %p cap %p %s\n", inode
,
1907 cap
, ceph_cap_string(ci
->i_flushing_caps
));
1908 delayed
= __send_cap(mdsc
, cap
, CEPH_CAP_OP_FLUSH
,
1909 __ceph_caps_used(ci
),
1910 __ceph_caps_wanted(ci
),
1911 cap
->issued
| cap
->implemented
,
1912 ci
->i_flushing_caps
, NULL
);
1914 spin_lock(&inode
->i_lock
);
1915 __cap_delay_requeue(mdsc
, ci
);
1916 spin_unlock(&inode
->i_lock
);
1919 pr_err("%p auth cap %p not mds%d ???\n", inode
,
1920 cap
, session
->s_mds
);
1921 spin_unlock(&inode
->i_lock
);
1928 * Take references to capabilities we hold, so that we don't release
1929 * them to the MDS prematurely.
1931 * Protected by i_lock.
1933 static void __take_cap_refs(struct ceph_inode_info
*ci
, int got
)
1935 if (got
& CEPH_CAP_PIN
)
1937 if (got
& CEPH_CAP_FILE_RD
)
1939 if (got
& CEPH_CAP_FILE_CACHE
)
1940 ci
->i_rdcache_ref
++;
1941 if (got
& CEPH_CAP_FILE_WR
)
1943 if (got
& CEPH_CAP_FILE_BUFFER
) {
1944 if (ci
->i_wrbuffer_ref
== 0)
1945 igrab(&ci
->vfs_inode
);
1946 ci
->i_wrbuffer_ref
++;
1947 dout("__take_cap_refs %p wrbuffer %d -> %d (?)\n",
1948 &ci
->vfs_inode
, ci
->i_wrbuffer_ref
-1, ci
->i_wrbuffer_ref
);
1953 * Try to grab cap references. Specify those refs we @want, and the
1954 * minimal set we @need. Also include the larger offset we are writing
1955 * to (when applicable), and check against max_size here as well.
1956 * Note that caller is responsible for ensuring max_size increases are
1957 * requested from the MDS.
1959 static int try_get_cap_refs(struct ceph_inode_info
*ci
, int need
, int want
,
1960 int *got
, loff_t endoff
, int *check_max
, int *err
)
1962 struct inode
*inode
= &ci
->vfs_inode
;
1964 int have
, implemented
;
1967 dout("get_cap_refs %p need %s want %s\n", inode
,
1968 ceph_cap_string(need
), ceph_cap_string(want
));
1969 spin_lock(&inode
->i_lock
);
1971 /* make sure file is actually open */
1972 file_wanted
= __ceph_caps_file_wanted(ci
);
1973 if ((file_wanted
& need
) == 0) {
1974 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
1975 ceph_cap_string(need
), ceph_cap_string(file_wanted
));
1981 if (need
& CEPH_CAP_FILE_WR
) {
1982 if (endoff
>= 0 && endoff
> (loff_t
)ci
->i_max_size
) {
1983 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
1984 inode
, endoff
, ci
->i_max_size
);
1985 if (endoff
> ci
->i_wanted_max_size
) {
1992 * If a sync write is in progress, we must wait, so that we
1993 * can get a final snapshot value for size+mtime.
1995 if (__ceph_have_pending_cap_snap(ci
)) {
1996 dout("get_cap_refs %p cap_snap_pending\n", inode
);
2000 have
= __ceph_caps_issued(ci
, &implemented
);
2003 * disallow writes while a truncate is pending
2005 if (ci
->i_truncate_pending
)
2006 have
&= ~CEPH_CAP_FILE_WR
;
2008 if ((have
& need
) == need
) {
2010 * Look at (implemented & ~have & not) so that we keep waiting
2011 * on transition from wanted -> needed caps. This is needed
2012 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2013 * going before a prior buffered writeback happens.
2015 int not = want
& ~(have
& need
);
2016 int revoking
= implemented
& ~have
;
2017 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2018 inode
, ceph_cap_string(have
), ceph_cap_string(not),
2019 ceph_cap_string(revoking
));
2020 if ((revoking
& not) == 0) {
2021 *got
= need
| (have
& want
);
2022 __take_cap_refs(ci
, *got
);
2026 dout("get_cap_refs %p have %s needed %s\n", inode
,
2027 ceph_cap_string(have
), ceph_cap_string(need
));
2030 spin_unlock(&inode
->i_lock
);
2031 dout("get_cap_refs %p ret %d got %s\n", inode
,
2032 ret
, ceph_cap_string(*got
));
2037 * Check the offset we are writing up to against our current
2038 * max_size. If necessary, tell the MDS we want to write to
2041 static void check_max_size(struct inode
*inode
, loff_t endoff
)
2043 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2046 /* do we need to explicitly request a larger max_size? */
2047 spin_lock(&inode
->i_lock
);
2048 if ((endoff
>= ci
->i_max_size
||
2049 endoff
> (inode
->i_size
<< 1)) &&
2050 endoff
> ci
->i_wanted_max_size
) {
2051 dout("write %p at large endoff %llu, req max_size\n",
2053 ci
->i_wanted_max_size
= endoff
;
2056 spin_unlock(&inode
->i_lock
);
2058 ceph_check_caps(ci
, CHECK_CAPS_AUTHONLY
, NULL
);
2062 * Wait for caps, and take cap references. If we can't get a WR cap
2063 * due to a small max_size, make sure we check_max_size (and possibly
2064 * ask the mds) so we don't get hung up indefinitely.
2066 int ceph_get_caps(struct ceph_inode_info
*ci
, int need
, int want
, int *got
,
2069 int check_max
, ret
, err
;
2073 check_max_size(&ci
->vfs_inode
, endoff
);
2076 ret
= wait_event_interruptible(ci
->i_cap_wq
,
2077 try_get_cap_refs(ci
, need
, want
,
2088 * Take cap refs. Caller must already know we hold at least one ref
2089 * on the caps in question or we don't know this is safe.
2091 void ceph_get_cap_refs(struct ceph_inode_info
*ci
, int caps
)
2093 spin_lock(&ci
->vfs_inode
.i_lock
);
2094 __take_cap_refs(ci
, caps
);
2095 spin_unlock(&ci
->vfs_inode
.i_lock
);
2101 * If we released the last ref on any given cap, call ceph_check_caps
2102 * to release (or schedule a release).
2104 * If we are releasing a WR cap (from a sync write), finalize any affected
2105 * cap_snap, and wake up any waiters.
2107 void ceph_put_cap_refs(struct ceph_inode_info
*ci
, int had
)
2109 struct inode
*inode
= &ci
->vfs_inode
;
2110 int last
= 0, put
= 0, flushsnaps
= 0, wake
= 0;
2111 struct ceph_cap_snap
*capsnap
;
2113 spin_lock(&inode
->i_lock
);
2114 if (had
& CEPH_CAP_PIN
)
2116 if (had
& CEPH_CAP_FILE_RD
)
2117 if (--ci
->i_rd_ref
== 0)
2119 if (had
& CEPH_CAP_FILE_CACHE
)
2120 if (--ci
->i_rdcache_ref
== 0)
2122 if (had
& CEPH_CAP_FILE_BUFFER
) {
2123 if (--ci
->i_wrbuffer_ref
== 0) {
2127 dout("put_cap_refs %p wrbuffer %d -> %d (?)\n",
2128 inode
, ci
->i_wrbuffer_ref
+1, ci
->i_wrbuffer_ref
);
2130 if (had
& CEPH_CAP_FILE_WR
)
2131 if (--ci
->i_wr_ref
== 0) {
2133 if (!list_empty(&ci
->i_cap_snaps
)) {
2134 capsnap
= list_first_entry(&ci
->i_cap_snaps
,
2135 struct ceph_cap_snap
,
2137 if (capsnap
->writing
) {
2138 capsnap
->writing
= 0;
2140 __ceph_finish_cap_snap(ci
,
2146 spin_unlock(&inode
->i_lock
);
2148 dout("put_cap_refs %p had %s%s%s\n", inode
, ceph_cap_string(had
),
2149 last
? " last" : "", put
? " put" : "");
2151 if (last
&& !flushsnaps
)
2152 ceph_check_caps(ci
, 0, NULL
);
2153 else if (flushsnaps
)
2154 ceph_flush_snaps(ci
);
2156 wake_up_all(&ci
->i_cap_wq
);
2162 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2163 * context. Adjust per-snap dirty page accounting as appropriate.
2164 * Once all dirty data for a cap_snap is flushed, flush snapped file
2165 * metadata back to the MDS. If we dropped the last ref, call
2168 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info
*ci
, int nr
,
2169 struct ceph_snap_context
*snapc
)
2171 struct inode
*inode
= &ci
->vfs_inode
;
2173 int complete_capsnap
= 0;
2174 int drop_capsnap
= 0;
2176 struct ceph_cap_snap
*capsnap
= NULL
;
2178 spin_lock(&inode
->i_lock
);
2179 ci
->i_wrbuffer_ref
-= nr
;
2180 last
= !ci
->i_wrbuffer_ref
;
2182 if (ci
->i_head_snapc
== snapc
) {
2183 ci
->i_wrbuffer_ref_head
-= nr
;
2184 if (!ci
->i_wrbuffer_ref_head
) {
2185 ceph_put_snap_context(ci
->i_head_snapc
);
2186 ci
->i_head_snapc
= NULL
;
2188 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2190 ci
->i_wrbuffer_ref
+nr
, ci
->i_wrbuffer_ref_head
+nr
,
2191 ci
->i_wrbuffer_ref
, ci
->i_wrbuffer_ref_head
,
2192 last
? " LAST" : "");
2194 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
2195 if (capsnap
->context
== snapc
) {
2201 capsnap
->dirty_pages
-= nr
;
2202 if (capsnap
->dirty_pages
== 0) {
2203 complete_capsnap
= 1;
2204 if (capsnap
->dirty
== 0)
2205 /* cap writeback completed before we created
2206 * the cap_snap; no FLUSHSNAP is needed */
2209 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2210 " snap %lld %d/%d -> %d/%d %s%s%s\n",
2211 inode
, capsnap
, capsnap
->context
->seq
,
2212 ci
->i_wrbuffer_ref
+nr
, capsnap
->dirty_pages
+ nr
,
2213 ci
->i_wrbuffer_ref
, capsnap
->dirty_pages
,
2214 last
? " (wrbuffer last)" : "",
2215 complete_capsnap
? " (complete capsnap)" : "",
2216 drop_capsnap
? " (drop capsnap)" : "");
2218 ceph_put_snap_context(capsnap
->context
);
2219 list_del(&capsnap
->ci_item
);
2220 list_del(&capsnap
->flushing_item
);
2221 ceph_put_cap_snap(capsnap
);
2225 spin_unlock(&inode
->i_lock
);
2228 ceph_check_caps(ci
, CHECK_CAPS_AUTHONLY
, NULL
);
2230 } else if (complete_capsnap
) {
2231 ceph_flush_snaps(ci
);
2232 wake_up_all(&ci
->i_cap_wq
);
2239 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2240 * actually be a revocation if it specifies a smaller cap set.)
2242 * caller holds s_mutex and i_lock, we drop both.
2246 * 1 - check_caps on auth cap only (writeback)
2247 * 2 - check_caps (ack revoke)
2249 static void handle_cap_grant(struct inode
*inode
, struct ceph_mds_caps
*grant
,
2250 struct ceph_mds_session
*session
,
2251 struct ceph_cap
*cap
,
2252 struct ceph_buffer
*xattr_buf
)
2253 __releases(inode
->i_lock
)
2254 __releases(session
->s_mutex
)
2256 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2257 int mds
= session
->s_mds
;
2258 int seq
= le32_to_cpu(grant
->seq
);
2259 int newcaps
= le32_to_cpu(grant
->caps
);
2260 int issued
, implemented
, used
, wanted
, dirty
;
2261 u64 size
= le64_to_cpu(grant
->size
);
2262 u64 max_size
= le64_to_cpu(grant
->max_size
);
2263 struct timespec mtime
, atime
, ctime
;
2267 int revoked_rdcache
= 0;
2268 int queue_invalidate
= 0;
2270 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2271 inode
, cap
, mds
, seq
, ceph_cap_string(newcaps
));
2272 dout(" size %llu max_size %llu, i_size %llu\n", size
, max_size
,
2276 * If CACHE is being revoked, and we have no dirty buffers,
2277 * try to invalidate (once). (If there are dirty buffers, we
2278 * will invalidate _after_ writeback.)
2280 if (((cap
->issued
& ~newcaps
) & CEPH_CAP_FILE_CACHE
) &&
2281 !ci
->i_wrbuffer_ref
) {
2282 if (try_nonblocking_invalidate(inode
) == 0) {
2283 revoked_rdcache
= 1;
2285 /* there were locked pages.. invalidate later
2286 in a separate thread. */
2287 if (ci
->i_rdcache_revoking
!= ci
->i_rdcache_gen
) {
2288 queue_invalidate
= 1;
2289 ci
->i_rdcache_revoking
= ci
->i_rdcache_gen
;
2294 /* side effects now are allowed */
2296 issued
= __ceph_caps_issued(ci
, &implemented
);
2297 issued
|= implemented
| __ceph_caps_dirty(ci
);
2299 cap
->cap_gen
= session
->s_cap_gen
;
2301 __check_cap_issue(ci
, cap
, newcaps
);
2303 if ((issued
& CEPH_CAP_AUTH_EXCL
) == 0) {
2304 inode
->i_mode
= le32_to_cpu(grant
->mode
);
2305 inode
->i_uid
= le32_to_cpu(grant
->uid
);
2306 inode
->i_gid
= le32_to_cpu(grant
->gid
);
2307 dout("%p mode 0%o uid.gid %d.%d\n", inode
, inode
->i_mode
,
2308 inode
->i_uid
, inode
->i_gid
);
2311 if ((issued
& CEPH_CAP_LINK_EXCL
) == 0)
2312 inode
->i_nlink
= le32_to_cpu(grant
->nlink
);
2314 if ((issued
& CEPH_CAP_XATTR_EXCL
) == 0 && grant
->xattr_len
) {
2315 int len
= le32_to_cpu(grant
->xattr_len
);
2316 u64 version
= le64_to_cpu(grant
->xattr_version
);
2318 if (version
> ci
->i_xattrs
.version
) {
2319 dout(" got new xattrs v%llu on %p len %d\n",
2320 version
, inode
, len
);
2321 if (ci
->i_xattrs
.blob
)
2322 ceph_buffer_put(ci
->i_xattrs
.blob
);
2323 ci
->i_xattrs
.blob
= ceph_buffer_get(xattr_buf
);
2324 ci
->i_xattrs
.version
= version
;
2328 /* size/ctime/mtime/atime? */
2329 ceph_fill_file_size(inode
, issued
,
2330 le32_to_cpu(grant
->truncate_seq
),
2331 le64_to_cpu(grant
->truncate_size
), size
);
2332 ceph_decode_timespec(&mtime
, &grant
->mtime
);
2333 ceph_decode_timespec(&atime
, &grant
->atime
);
2334 ceph_decode_timespec(&ctime
, &grant
->ctime
);
2335 ceph_fill_file_time(inode
, issued
,
2336 le32_to_cpu(grant
->time_warp_seq
), &ctime
, &mtime
,
2339 /* max size increase? */
2340 if (max_size
!= ci
->i_max_size
) {
2341 dout("max_size %lld -> %llu\n", ci
->i_max_size
, max_size
);
2342 ci
->i_max_size
= max_size
;
2343 if (max_size
>= ci
->i_wanted_max_size
) {
2344 ci
->i_wanted_max_size
= 0; /* reset */
2345 ci
->i_requested_max_size
= 0;
2350 /* check cap bits */
2351 wanted
= __ceph_caps_wanted(ci
);
2352 used
= __ceph_caps_used(ci
);
2353 dirty
= __ceph_caps_dirty(ci
);
2354 dout(" my wanted = %s, used = %s, dirty %s\n",
2355 ceph_cap_string(wanted
),
2356 ceph_cap_string(used
),
2357 ceph_cap_string(dirty
));
2358 if (wanted
!= le32_to_cpu(grant
->wanted
)) {
2359 dout("mds wanted %s -> %s\n",
2360 ceph_cap_string(le32_to_cpu(grant
->wanted
)),
2361 ceph_cap_string(wanted
));
2362 grant
->wanted
= cpu_to_le32(wanted
);
2367 /* file layout may have changed */
2368 ci
->i_layout
= grant
->layout
;
2370 /* revocation, grant, or no-op? */
2371 if (cap
->issued
& ~newcaps
) {
2372 dout("revocation: %s -> %s\n", ceph_cap_string(cap
->issued
),
2373 ceph_cap_string(newcaps
));
2374 if ((used
& ~newcaps
) & CEPH_CAP_FILE_BUFFER
)
2375 writeback
= 1; /* will delay ack */
2376 else if (dirty
& ~newcaps
)
2377 check_caps
= 1; /* initiate writeback in check_caps */
2378 else if (((used
& ~newcaps
) & CEPH_CAP_FILE_CACHE
) == 0 ||
2380 check_caps
= 2; /* send revoke ack in check_caps */
2381 cap
->issued
= newcaps
;
2382 cap
->implemented
|= newcaps
;
2383 } else if (cap
->issued
== newcaps
) {
2384 dout("caps unchanged: %s -> %s\n",
2385 ceph_cap_string(cap
->issued
), ceph_cap_string(newcaps
));
2387 dout("grant: %s -> %s\n", ceph_cap_string(cap
->issued
),
2388 ceph_cap_string(newcaps
));
2389 cap
->issued
= newcaps
;
2390 cap
->implemented
|= newcaps
; /* add bits only, to
2391 * avoid stepping on a
2392 * pending revocation */
2395 BUG_ON(cap
->issued
& ~cap
->implemented
);
2397 spin_unlock(&inode
->i_lock
);
2400 * queue inode for writeback: we can't actually call
2401 * filemap_write_and_wait, etc. from message handler
2404 ceph_queue_writeback(inode
);
2405 if (queue_invalidate
)
2406 ceph_queue_invalidate(inode
);
2408 wake_up_all(&ci
->i_cap_wq
);
2410 if (check_caps
== 1)
2411 ceph_check_caps(ci
, CHECK_CAPS_NODELAY
|CHECK_CAPS_AUTHONLY
,
2413 else if (check_caps
== 2)
2414 ceph_check_caps(ci
, CHECK_CAPS_NODELAY
, session
);
2416 mutex_unlock(&session
->s_mutex
);
2420 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2421 * MDS has been safely committed.
2423 static void handle_cap_flush_ack(struct inode
*inode
, u64 flush_tid
,
2424 struct ceph_mds_caps
*m
,
2425 struct ceph_mds_session
*session
,
2426 struct ceph_cap
*cap
)
2427 __releases(inode
->i_lock
)
2429 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2430 struct ceph_mds_client
*mdsc
= &ceph_sb_to_client(inode
->i_sb
)->mdsc
;
2431 unsigned seq
= le32_to_cpu(m
->seq
);
2432 int dirty
= le32_to_cpu(m
->dirty
);
2437 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
2438 if ((dirty
& (1 << i
)) &&
2439 flush_tid
== ci
->i_cap_flush_tid
[i
])
2442 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2443 " flushing %s -> %s\n",
2444 inode
, session
->s_mds
, seq
, ceph_cap_string(dirty
),
2445 ceph_cap_string(cleaned
), ceph_cap_string(ci
->i_flushing_caps
),
2446 ceph_cap_string(ci
->i_flushing_caps
& ~cleaned
));
2448 if (ci
->i_flushing_caps
== (ci
->i_flushing_caps
& ~cleaned
))
2451 ci
->i_flushing_caps
&= ~cleaned
;
2453 spin_lock(&mdsc
->cap_dirty_lock
);
2454 if (ci
->i_flushing_caps
== 0) {
2455 list_del_init(&ci
->i_flushing_item
);
2456 if (!list_empty(&session
->s_cap_flushing
))
2457 dout(" mds%d still flushing cap on %p\n",
2459 &list_entry(session
->s_cap_flushing
.next
,
2460 struct ceph_inode_info
,
2461 i_flushing_item
)->vfs_inode
);
2462 mdsc
->num_cap_flushing
--;
2463 wake_up_all(&mdsc
->cap_flushing_wq
);
2464 dout(" inode %p now !flushing\n", inode
);
2466 if (ci
->i_dirty_caps
== 0) {
2467 dout(" inode %p now clean\n", inode
);
2468 BUG_ON(!list_empty(&ci
->i_dirty_item
));
2471 BUG_ON(list_empty(&ci
->i_dirty_item
));
2474 spin_unlock(&mdsc
->cap_dirty_lock
);
2475 wake_up_all(&ci
->i_cap_wq
);
2478 spin_unlock(&inode
->i_lock
);
2484 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
2485 * throw away our cap_snap.
2487 * Caller hold s_mutex.
2489 static void handle_cap_flushsnap_ack(struct inode
*inode
, u64 flush_tid
,
2490 struct ceph_mds_caps
*m
,
2491 struct ceph_mds_session
*session
)
2493 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2494 u64 follows
= le64_to_cpu(m
->snap_follows
);
2495 struct ceph_cap_snap
*capsnap
;
2498 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2499 inode
, ci
, session
->s_mds
, follows
);
2501 spin_lock(&inode
->i_lock
);
2502 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
2503 if (capsnap
->follows
== follows
) {
2504 if (capsnap
->flush_tid
!= flush_tid
) {
2505 dout(" cap_snap %p follows %lld tid %lld !="
2506 " %lld\n", capsnap
, follows
,
2507 flush_tid
, capsnap
->flush_tid
);
2510 WARN_ON(capsnap
->dirty_pages
|| capsnap
->writing
);
2511 dout(" removing %p cap_snap %p follows %lld\n",
2512 inode
, capsnap
, follows
);
2513 ceph_put_snap_context(capsnap
->context
);
2514 list_del(&capsnap
->ci_item
);
2515 list_del(&capsnap
->flushing_item
);
2516 ceph_put_cap_snap(capsnap
);
2520 dout(" skipping cap_snap %p follows %lld\n",
2521 capsnap
, capsnap
->follows
);
2524 spin_unlock(&inode
->i_lock
);
2530 * Handle TRUNC from MDS, indicating file truncation.
2532 * caller hold s_mutex.
2534 static void handle_cap_trunc(struct inode
*inode
,
2535 struct ceph_mds_caps
*trunc
,
2536 struct ceph_mds_session
*session
)
2537 __releases(inode
->i_lock
)
2539 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2540 int mds
= session
->s_mds
;
2541 int seq
= le32_to_cpu(trunc
->seq
);
2542 u32 truncate_seq
= le32_to_cpu(trunc
->truncate_seq
);
2543 u64 truncate_size
= le64_to_cpu(trunc
->truncate_size
);
2544 u64 size
= le64_to_cpu(trunc
->size
);
2545 int implemented
= 0;
2546 int dirty
= __ceph_caps_dirty(ci
);
2547 int issued
= __ceph_caps_issued(ceph_inode(inode
), &implemented
);
2548 int queue_trunc
= 0;
2550 issued
|= implemented
| dirty
;
2552 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2553 inode
, mds
, seq
, truncate_size
, truncate_seq
);
2554 queue_trunc
= ceph_fill_file_size(inode
, issued
,
2555 truncate_seq
, truncate_size
, size
);
2556 spin_unlock(&inode
->i_lock
);
2559 ceph_queue_vmtruncate(inode
);
2563 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
2564 * different one. If we are the most recent migration we've seen (as
2565 * indicated by mseq), make note of the migrating cap bits for the
2566 * duration (until we see the corresponding IMPORT).
2568 * caller holds s_mutex
2570 static void handle_cap_export(struct inode
*inode
, struct ceph_mds_caps
*ex
,
2571 struct ceph_mds_session
*session
)
2573 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2574 int mds
= session
->s_mds
;
2575 unsigned mseq
= le32_to_cpu(ex
->migrate_seq
);
2576 struct ceph_cap
*cap
= NULL
, *t
;
2580 dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
2581 inode
, ci
, mds
, mseq
);
2583 spin_lock(&inode
->i_lock
);
2585 /* make sure we haven't seen a higher mseq */
2586 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
2587 t
= rb_entry(p
, struct ceph_cap
, ci_node
);
2588 if (ceph_seq_cmp(t
->mseq
, mseq
) > 0) {
2589 dout(" higher mseq on cap from mds%d\n",
2593 if (t
->session
->s_mds
== mds
)
2600 ci
->i_cap_exporting_mds
= mds
;
2601 ci
->i_cap_exporting_mseq
= mseq
;
2602 ci
->i_cap_exporting_issued
= cap
->issued
;
2604 __ceph_remove_cap(cap
);
2606 /* else, we already released it */
2608 spin_unlock(&inode
->i_lock
);
2612 * Handle cap IMPORT. If there are temp bits from an older EXPORT,
2615 * caller holds s_mutex.
2617 static void handle_cap_import(struct ceph_mds_client
*mdsc
,
2618 struct inode
*inode
, struct ceph_mds_caps
*im
,
2619 struct ceph_mds_session
*session
,
2620 void *snaptrace
, int snaptrace_len
)
2622 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2623 int mds
= session
->s_mds
;
2624 unsigned issued
= le32_to_cpu(im
->caps
);
2625 unsigned wanted
= le32_to_cpu(im
->wanted
);
2626 unsigned seq
= le32_to_cpu(im
->seq
);
2627 unsigned mseq
= le32_to_cpu(im
->migrate_seq
);
2628 u64 realmino
= le64_to_cpu(im
->realm
);
2629 u64 cap_id
= le64_to_cpu(im
->cap_id
);
2631 if (ci
->i_cap_exporting_mds
>= 0 &&
2632 ceph_seq_cmp(ci
->i_cap_exporting_mseq
, mseq
) < 0) {
2633 dout("handle_cap_import inode %p ci %p mds%d mseq %d"
2634 " - cleared exporting from mds%d\n",
2635 inode
, ci
, mds
, mseq
,
2636 ci
->i_cap_exporting_mds
);
2637 ci
->i_cap_exporting_issued
= 0;
2638 ci
->i_cap_exporting_mseq
= 0;
2639 ci
->i_cap_exporting_mds
= -1;
2641 dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
2642 inode
, ci
, mds
, mseq
);
2645 down_write(&mdsc
->snap_rwsem
);
2646 ceph_update_snap_trace(mdsc
, snaptrace
, snaptrace
+snaptrace_len
,
2648 downgrade_write(&mdsc
->snap_rwsem
);
2649 ceph_add_cap(inode
, session
, cap_id
, -1,
2650 issued
, wanted
, seq
, mseq
, realmino
, CEPH_CAP_FLAG_AUTH
,
2651 NULL
/* no caps context */);
2652 try_flush_caps(inode
, session
, NULL
);
2653 up_read(&mdsc
->snap_rwsem
);
2657 * Handle a caps message from the MDS.
2659 * Identify the appropriate session, inode, and call the right handler
2660 * based on the cap op.
2662 void ceph_handle_caps(struct ceph_mds_session
*session
,
2663 struct ceph_msg
*msg
)
2665 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2666 struct super_block
*sb
= mdsc
->client
->sb
;
2667 struct inode
*inode
;
2668 struct ceph_cap
*cap
;
2669 struct ceph_mds_caps
*h
;
2670 int mds
= session
->s_mds
;
2673 struct ceph_vino vino
;
2679 dout("handle_caps from mds%d\n", mds
);
2682 tid
= le64_to_cpu(msg
->hdr
.tid
);
2683 if (msg
->front
.iov_len
< sizeof(*h
))
2685 h
= msg
->front
.iov_base
;
2687 op
= le32_to_cpu(h
->op
);
2688 vino
.ino
= le64_to_cpu(h
->ino
);
2689 vino
.snap
= CEPH_NOSNAP
;
2690 cap_id
= le64_to_cpu(h
->cap_id
);
2691 seq
= le32_to_cpu(h
->seq
);
2692 mseq
= le32_to_cpu(h
->migrate_seq
);
2693 size
= le64_to_cpu(h
->size
);
2694 max_size
= le64_to_cpu(h
->max_size
);
2696 mutex_lock(&session
->s_mutex
);
2698 dout(" mds%d seq %lld cap seq %u\n", session
->s_mds
, session
->s_seq
,
2702 inode
= ceph_find_inode(sb
, vino
);
2703 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op
), vino
.ino
,
2706 dout(" i don't have ino %llx\n", vino
.ino
);
2708 if (op
== CEPH_CAP_OP_IMPORT
)
2709 __queue_cap_release(session
, vino
.ino
, cap_id
,
2713 * send any full release message to try to move things
2714 * along for the mds (who clearly thinks we still have this
2717 ceph_add_cap_releases(mdsc
, session
, -1);
2718 ceph_send_cap_releases(mdsc
, session
);
2722 /* these will work even if we don't have a cap yet */
2724 case CEPH_CAP_OP_FLUSHSNAP_ACK
:
2725 handle_cap_flushsnap_ack(inode
, tid
, h
, session
);
2728 case CEPH_CAP_OP_EXPORT
:
2729 handle_cap_export(inode
, h
, session
);
2732 case CEPH_CAP_OP_IMPORT
:
2733 handle_cap_import(mdsc
, inode
, h
, session
,
2734 snaptrace
, le32_to_cpu(h
->snap_trace_len
));
2735 ceph_check_caps(ceph_inode(inode
), CHECK_CAPS_NODELAY
,
2740 /* the rest require a cap */
2741 spin_lock(&inode
->i_lock
);
2742 cap
= __get_cap_for_mds(ceph_inode(inode
), mds
);
2744 dout(" no cap on %p ino %llx.%llx from mds%d\n",
2745 inode
, ceph_ino(inode
), ceph_snap(inode
), mds
);
2746 spin_unlock(&inode
->i_lock
);
2750 /* note that each of these drops i_lock for us */
2752 case CEPH_CAP_OP_REVOKE
:
2753 case CEPH_CAP_OP_GRANT
:
2754 handle_cap_grant(inode
, h
, session
, cap
, msg
->middle
);
2757 case CEPH_CAP_OP_FLUSH_ACK
:
2758 handle_cap_flush_ack(inode
, tid
, h
, session
, cap
);
2761 case CEPH_CAP_OP_TRUNC
:
2762 handle_cap_trunc(inode
, h
, session
);
2766 spin_unlock(&inode
->i_lock
);
2767 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op
,
2768 ceph_cap_op_name(op
));
2772 mutex_unlock(&session
->s_mutex
);
2779 pr_err("ceph_handle_caps: corrupt message\n");
2785 * Delayed work handler to process end of delayed cap release LRU list.
2787 void ceph_check_delayed_caps(struct ceph_mds_client
*mdsc
)
2789 struct ceph_inode_info
*ci
;
2790 int flags
= CHECK_CAPS_NODELAY
;
2792 dout("check_delayed_caps\n");
2794 spin_lock(&mdsc
->cap_delay_lock
);
2795 if (list_empty(&mdsc
->cap_delay_list
))
2797 ci
= list_first_entry(&mdsc
->cap_delay_list
,
2798 struct ceph_inode_info
,
2800 if ((ci
->i_ceph_flags
& CEPH_I_FLUSH
) == 0 &&
2801 time_before(jiffies
, ci
->i_hold_caps_max
))
2803 list_del_init(&ci
->i_cap_delay_list
);
2804 spin_unlock(&mdsc
->cap_delay_lock
);
2805 dout("check_delayed_caps on %p\n", &ci
->vfs_inode
);
2806 ceph_check_caps(ci
, flags
, NULL
);
2808 spin_unlock(&mdsc
->cap_delay_lock
);
2812 * Flush all dirty caps to the mds
2814 void ceph_flush_dirty_caps(struct ceph_mds_client
*mdsc
)
2816 struct ceph_inode_info
*ci
, *nci
= NULL
;
2817 struct inode
*inode
, *ninode
= NULL
;
2818 struct list_head
*p
, *n
;
2820 dout("flush_dirty_caps\n");
2821 spin_lock(&mdsc
->cap_dirty_lock
);
2822 list_for_each_safe(p
, n
, &mdsc
->cap_dirty
) {
2826 ci
->i_ceph_flags
&= ~CEPH_I_NOFLUSH
;
2827 dout("flush_dirty_caps inode %p (was next inode)\n",
2830 ci
= list_entry(p
, struct ceph_inode_info
,
2832 inode
= igrab(&ci
->vfs_inode
);
2834 dout("flush_dirty_caps inode %p\n", inode
);
2836 if (n
!= &mdsc
->cap_dirty
) {
2837 nci
= list_entry(n
, struct ceph_inode_info
,
2839 ninode
= igrab(&nci
->vfs_inode
);
2841 nci
->i_ceph_flags
|= CEPH_I_NOFLUSH
;
2842 dout("flush_dirty_caps next inode %p, noflush\n",
2848 spin_unlock(&mdsc
->cap_dirty_lock
);
2850 ceph_check_caps(ci
, CHECK_CAPS_NODELAY
|CHECK_CAPS_FLUSH
,
2854 spin_lock(&mdsc
->cap_dirty_lock
);
2856 spin_unlock(&mdsc
->cap_dirty_lock
);
2860 * Drop open file reference. If we were the last open file,
2861 * we may need to release capabilities to the MDS (or schedule
2862 * their delayed release).
2864 void ceph_put_fmode(struct ceph_inode_info
*ci
, int fmode
)
2866 struct inode
*inode
= &ci
->vfs_inode
;
2869 spin_lock(&inode
->i_lock
);
2870 dout("put_fmode %p fmode %d %d -> %d\n", inode
, fmode
,
2871 ci
->i_nr_by_mode
[fmode
], ci
->i_nr_by_mode
[fmode
]-1);
2872 BUG_ON(ci
->i_nr_by_mode
[fmode
] == 0);
2873 if (--ci
->i_nr_by_mode
[fmode
] == 0)
2875 spin_unlock(&inode
->i_lock
);
2877 if (last
&& ci
->i_vino
.snap
== CEPH_NOSNAP
)
2878 ceph_check_caps(ci
, 0, NULL
);
2882 * Helpers for embedding cap and dentry lease releases into mds
2885 * @force is used by dentry_release (below) to force inclusion of a
2886 * record for the directory inode, even when there aren't any caps to
2889 int ceph_encode_inode_release(void **p
, struct inode
*inode
,
2890 int mds
, int drop
, int unless
, int force
)
2892 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2893 struct ceph_cap
*cap
;
2894 struct ceph_mds_request_release
*rel
= *p
;
2898 spin_lock(&inode
->i_lock
);
2899 used
= __ceph_caps_used(ci
);
2900 dirty
= __ceph_caps_dirty(ci
);
2902 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
2903 inode
, mds
, ceph_cap_string(used
|dirty
), ceph_cap_string(drop
),
2904 ceph_cap_string(unless
));
2906 /* only drop unused, clean caps */
2907 drop
&= ~(used
| dirty
);
2909 cap
= __get_cap_for_mds(ci
, mds
);
2910 if (cap
&& __cap_is_valid(cap
)) {
2912 ((cap
->issued
& drop
) &&
2913 (cap
->issued
& unless
) == 0)) {
2914 if ((cap
->issued
& drop
) &&
2915 (cap
->issued
& unless
) == 0) {
2916 dout("encode_inode_release %p cap %p %s -> "
2918 ceph_cap_string(cap
->issued
),
2919 ceph_cap_string(cap
->issued
& ~drop
));
2920 cap
->issued
&= ~drop
;
2921 cap
->implemented
&= ~drop
;
2922 if (ci
->i_ceph_flags
& CEPH_I_NODELAY
) {
2923 int wanted
= __ceph_caps_wanted(ci
);
2924 dout(" wanted %s -> %s (act %s)\n",
2925 ceph_cap_string(cap
->mds_wanted
),
2926 ceph_cap_string(cap
->mds_wanted
&
2928 ceph_cap_string(wanted
));
2929 cap
->mds_wanted
&= wanted
;
2932 dout("encode_inode_release %p cap %p %s"
2933 " (force)\n", inode
, cap
,
2934 ceph_cap_string(cap
->issued
));
2937 rel
->ino
= cpu_to_le64(ceph_ino(inode
));
2938 rel
->cap_id
= cpu_to_le64(cap
->cap_id
);
2939 rel
->seq
= cpu_to_le32(cap
->seq
);
2940 rel
->issue_seq
= cpu_to_le32(cap
->issue_seq
),
2941 rel
->mseq
= cpu_to_le32(cap
->mseq
);
2942 rel
->caps
= cpu_to_le32(cap
->issued
);
2943 rel
->wanted
= cpu_to_le32(cap
->mds_wanted
);
2949 dout("encode_inode_release %p cap %p %s\n",
2950 inode
, cap
, ceph_cap_string(cap
->issued
));
2953 spin_unlock(&inode
->i_lock
);
2957 int ceph_encode_dentry_release(void **p
, struct dentry
*dentry
,
2958 int mds
, int drop
, int unless
)
2960 struct inode
*dir
= dentry
->d_parent
->d_inode
;
2961 struct ceph_mds_request_release
*rel
= *p
;
2962 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
2967 * force an record for the directory caps if we have a dentry lease.
2968 * this is racy (can't take i_lock and d_lock together), but it
2969 * doesn't have to be perfect; the mds will revoke anything we don't
2972 spin_lock(&dentry
->d_lock
);
2973 if (di
->lease_session
&& di
->lease_session
->s_mds
== mds
)
2975 spin_unlock(&dentry
->d_lock
);
2977 ret
= ceph_encode_inode_release(p
, dir
, mds
, drop
, unless
, force
);
2979 spin_lock(&dentry
->d_lock
);
2980 if (ret
&& di
->lease_session
&& di
->lease_session
->s_mds
== mds
) {
2981 dout("encode_dentry_release %p mds%d seq %d\n",
2982 dentry
, mds
, (int)di
->lease_seq
);
2983 rel
->dname_len
= cpu_to_le32(dentry
->d_name
.len
);
2984 memcpy(*p
, dentry
->d_name
.name
, dentry
->d_name
.len
);
2985 *p
+= dentry
->d_name
.len
;
2986 rel
->dname_seq
= cpu_to_le32(di
->lease_seq
);
2987 __ceph_mdsc_drop_dentry_lease(dentry
);
2989 spin_unlock(&dentry
->d_lock
);