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|BUFFER|EXCL, else -1.
342 static int __ceph_get_cap_mds(struct ceph_inode_info
*ci
, u32
*mseq
)
344 struct ceph_cap
*cap
;
348 /* prefer mds with WR|BUFFER|EXCL caps */
349 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
350 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
354 if (cap
->issued
& (CEPH_CAP_FILE_WR
|
355 CEPH_CAP_FILE_BUFFER
|
362 int ceph_get_cap_mds(struct inode
*inode
)
365 spin_lock(&inode
->i_lock
);
366 mds
= __ceph_get_cap_mds(ceph_inode(inode
), NULL
);
367 spin_unlock(&inode
->i_lock
);
372 * Called under i_lock.
374 static void __insert_cap_node(struct ceph_inode_info
*ci
,
375 struct ceph_cap
*new)
377 struct rb_node
**p
= &ci
->i_caps
.rb_node
;
378 struct rb_node
*parent
= NULL
;
379 struct ceph_cap
*cap
= NULL
;
383 cap
= rb_entry(parent
, struct ceph_cap
, ci_node
);
384 if (new->mds
< cap
->mds
)
386 else if (new->mds
> cap
->mds
)
392 rb_link_node(&new->ci_node
, parent
, p
);
393 rb_insert_color(&new->ci_node
, &ci
->i_caps
);
397 * (re)set cap hold timeouts, which control the delayed release
398 * of unused caps back to the MDS. Should be called on cap use.
400 static void __cap_set_timeouts(struct ceph_mds_client
*mdsc
,
401 struct ceph_inode_info
*ci
)
403 struct ceph_mount_args
*ma
= mdsc
->client
->mount_args
;
405 ci
->i_hold_caps_min
= round_jiffies(jiffies
+
406 ma
->caps_wanted_delay_min
* HZ
);
407 ci
->i_hold_caps_max
= round_jiffies(jiffies
+
408 ma
->caps_wanted_delay_max
* HZ
);
409 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci
->vfs_inode
,
410 ci
->i_hold_caps_min
- jiffies
, ci
->i_hold_caps_max
- jiffies
);
414 * (Re)queue cap at the end of the delayed cap release list.
416 * If I_FLUSH is set, leave the inode at the front of the list.
418 * Caller holds i_lock
419 * -> we take mdsc->cap_delay_lock
421 static void __cap_delay_requeue(struct ceph_mds_client
*mdsc
,
422 struct ceph_inode_info
*ci
)
424 __cap_set_timeouts(mdsc
, ci
);
425 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci
->vfs_inode
,
426 ci
->i_ceph_flags
, ci
->i_hold_caps_max
);
427 if (!mdsc
->stopping
) {
428 spin_lock(&mdsc
->cap_delay_lock
);
429 if (!list_empty(&ci
->i_cap_delay_list
)) {
430 if (ci
->i_ceph_flags
& CEPH_I_FLUSH
)
432 list_del_init(&ci
->i_cap_delay_list
);
434 list_add_tail(&ci
->i_cap_delay_list
, &mdsc
->cap_delay_list
);
436 spin_unlock(&mdsc
->cap_delay_lock
);
441 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
442 * indicating we should send a cap message to flush dirty metadata
443 * asap, and move to the front of the delayed cap list.
445 static void __cap_delay_requeue_front(struct ceph_mds_client
*mdsc
,
446 struct ceph_inode_info
*ci
)
448 dout("__cap_delay_requeue_front %p\n", &ci
->vfs_inode
);
449 spin_lock(&mdsc
->cap_delay_lock
);
450 ci
->i_ceph_flags
|= CEPH_I_FLUSH
;
451 if (!list_empty(&ci
->i_cap_delay_list
))
452 list_del_init(&ci
->i_cap_delay_list
);
453 list_add(&ci
->i_cap_delay_list
, &mdsc
->cap_delay_list
);
454 spin_unlock(&mdsc
->cap_delay_lock
);
458 * Cancel delayed work on cap.
460 * Caller must hold i_lock.
462 static void __cap_delay_cancel(struct ceph_mds_client
*mdsc
,
463 struct ceph_inode_info
*ci
)
465 dout("__cap_delay_cancel %p\n", &ci
->vfs_inode
);
466 if (list_empty(&ci
->i_cap_delay_list
))
468 spin_lock(&mdsc
->cap_delay_lock
);
469 list_del_init(&ci
->i_cap_delay_list
);
470 spin_unlock(&mdsc
->cap_delay_lock
);
474 * Common issue checks for add_cap, handle_cap_grant.
476 static void __check_cap_issue(struct ceph_inode_info
*ci
, struct ceph_cap
*cap
,
479 unsigned had
= __ceph_caps_issued(ci
, NULL
);
482 * Each time we receive FILE_CACHE anew, we increment
485 if ((issued
& (CEPH_CAP_FILE_CACHE
|CEPH_CAP_FILE_LAZYIO
)) &&
486 (had
& (CEPH_CAP_FILE_CACHE
|CEPH_CAP_FILE_LAZYIO
)) == 0)
490 * if we are newly issued FILE_SHARED, clear I_COMPLETE; we
491 * don't know what happened to this directory while we didn't
494 if ((issued
& CEPH_CAP_FILE_SHARED
) &&
495 (had
& CEPH_CAP_FILE_SHARED
) == 0) {
497 if (S_ISDIR(ci
->vfs_inode
.i_mode
)) {
498 dout(" marking %p NOT complete\n", &ci
->vfs_inode
);
499 ci
->i_ceph_flags
&= ~CEPH_I_COMPLETE
;
505 * Add a capability under the given MDS session.
507 * Caller should hold session snap_rwsem (read) and s_mutex.
509 * @fmode is the open file mode, if we are opening a file, otherwise
510 * it is < 0. (This is so we can atomically add the cap and add an
511 * open file reference to it.)
513 int ceph_add_cap(struct inode
*inode
,
514 struct ceph_mds_session
*session
, u64 cap_id
,
515 int fmode
, unsigned issued
, unsigned wanted
,
516 unsigned seq
, unsigned mseq
, u64 realmino
, int flags
,
517 struct ceph_cap_reservation
*caps_reservation
)
519 struct ceph_mds_client
*mdsc
= &ceph_inode_to_client(inode
)->mdsc
;
520 struct ceph_inode_info
*ci
= ceph_inode(inode
);
521 struct ceph_cap
*new_cap
= NULL
;
522 struct ceph_cap
*cap
;
523 int mds
= session
->s_mds
;
526 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode
,
527 session
->s_mds
, cap_id
, ceph_cap_string(issued
), seq
);
530 * If we are opening the file, include file mode wanted bits
534 wanted
|= ceph_caps_for_mode(fmode
);
537 spin_lock(&inode
->i_lock
);
538 cap
= __get_cap_for_mds(ci
, mds
);
544 spin_unlock(&inode
->i_lock
);
545 new_cap
= get_cap(caps_reservation
);
552 cap
->implemented
= 0;
557 __insert_cap_node(ci
, cap
);
559 /* clear out old exporting info? (i.e. on cap import) */
560 if (ci
->i_cap_exporting_mds
== mds
) {
561 ci
->i_cap_exporting_issued
= 0;
562 ci
->i_cap_exporting_mseq
= 0;
563 ci
->i_cap_exporting_mds
= -1;
566 /* add to session cap list */
567 cap
->session
= session
;
568 spin_lock(&session
->s_cap_lock
);
569 list_add_tail(&cap
->session_caps
, &session
->s_caps
);
570 session
->s_nr_caps
++;
571 spin_unlock(&session
->s_cap_lock
);
574 if (!ci
->i_snap_realm
) {
576 * add this inode to the appropriate snap realm
578 struct ceph_snap_realm
*realm
= ceph_lookup_snap_realm(mdsc
,
581 ceph_get_snap_realm(mdsc
, realm
);
582 spin_lock(&realm
->inodes_with_caps_lock
);
583 ci
->i_snap_realm
= realm
;
584 list_add(&ci
->i_snap_realm_item
,
585 &realm
->inodes_with_caps
);
586 spin_unlock(&realm
->inodes_with_caps_lock
);
588 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
593 __check_cap_issue(ci
, cap
, issued
);
596 * If we are issued caps we don't want, or the mds' wanted
597 * value appears to be off, queue a check so we'll release
598 * later and/or update the mds wanted value.
600 actual_wanted
= __ceph_caps_wanted(ci
);
601 if ((wanted
& ~actual_wanted
) ||
602 (issued
& ~actual_wanted
& CEPH_CAP_ANY_WR
)) {
603 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
604 ceph_cap_string(issued
), ceph_cap_string(wanted
),
605 ceph_cap_string(actual_wanted
));
606 __cap_delay_requeue(mdsc
, ci
);
609 if (flags
& CEPH_CAP_FLAG_AUTH
)
610 ci
->i_auth_cap
= cap
;
611 else if (ci
->i_auth_cap
== cap
)
612 ci
->i_auth_cap
= NULL
;
614 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
615 inode
, ceph_vinop(inode
), cap
, ceph_cap_string(issued
),
616 ceph_cap_string(issued
|cap
->issued
), seq
, mds
);
617 cap
->cap_id
= cap_id
;
618 cap
->issued
= issued
;
619 cap
->implemented
|= issued
;
620 cap
->mds_wanted
|= wanted
;
622 cap
->issue_seq
= seq
;
624 cap
->cap_gen
= session
->s_cap_gen
;
627 __ceph_get_fmode(ci
, fmode
);
628 spin_unlock(&inode
->i_lock
);
629 wake_up_all(&ci
->i_cap_wq
);
634 * Return true if cap has not timed out and belongs to the current
635 * generation of the MDS session (i.e. has not gone 'stale' due to
636 * us losing touch with the mds).
638 static int __cap_is_valid(struct ceph_cap
*cap
)
643 spin_lock(&cap
->session
->s_cap_lock
);
644 gen
= cap
->session
->s_cap_gen
;
645 ttl
= cap
->session
->s_cap_ttl
;
646 spin_unlock(&cap
->session
->s_cap_lock
);
648 if (cap
->cap_gen
< gen
|| time_after_eq(jiffies
, ttl
)) {
649 dout("__cap_is_valid %p cap %p issued %s "
650 "but STALE (gen %u vs %u)\n", &cap
->ci
->vfs_inode
,
651 cap
, ceph_cap_string(cap
->issued
), cap
->cap_gen
, gen
);
659 * Return set of valid cap bits issued to us. Note that caps time
660 * out, and may be invalidated in bulk if the client session times out
661 * and session->s_cap_gen is bumped.
663 int __ceph_caps_issued(struct ceph_inode_info
*ci
, int *implemented
)
665 int have
= ci
->i_snap_caps
| ci
->i_cap_exporting_issued
;
666 struct ceph_cap
*cap
;
671 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
672 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
673 if (!__cap_is_valid(cap
))
675 dout("__ceph_caps_issued %p cap %p issued %s\n",
676 &ci
->vfs_inode
, cap
, ceph_cap_string(cap
->issued
));
679 *implemented
|= cap
->implemented
;
685 * Get cap bits issued by caps other than @ocap
687 int __ceph_caps_issued_other(struct ceph_inode_info
*ci
, struct ceph_cap
*ocap
)
689 int have
= ci
->i_snap_caps
;
690 struct ceph_cap
*cap
;
693 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
694 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
697 if (!__cap_is_valid(cap
))
705 * Move a cap to the end of the LRU (oldest caps at list head, newest
708 static void __touch_cap(struct ceph_cap
*cap
)
710 struct ceph_mds_session
*s
= cap
->session
;
712 spin_lock(&s
->s_cap_lock
);
713 if (s
->s_cap_iterator
== NULL
) {
714 dout("__touch_cap %p cap %p mds%d\n", &cap
->ci
->vfs_inode
, cap
,
716 list_move_tail(&cap
->session_caps
, &s
->s_caps
);
718 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
719 &cap
->ci
->vfs_inode
, cap
, s
->s_mds
);
721 spin_unlock(&s
->s_cap_lock
);
725 * Check if we hold the given mask. If so, move the cap(s) to the
726 * front of their respective LRUs. (This is the preferred way for
727 * callers to check for caps they want.)
729 int __ceph_caps_issued_mask(struct ceph_inode_info
*ci
, int mask
, int touch
)
731 struct ceph_cap
*cap
;
733 int have
= ci
->i_snap_caps
;
735 if ((have
& mask
) == mask
) {
736 dout("__ceph_caps_issued_mask %p snap issued %s"
737 " (mask %s)\n", &ci
->vfs_inode
,
738 ceph_cap_string(have
),
739 ceph_cap_string(mask
));
743 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
744 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
745 if (!__cap_is_valid(cap
))
747 if ((cap
->issued
& mask
) == mask
) {
748 dout("__ceph_caps_issued_mask %p cap %p issued %s"
749 " (mask %s)\n", &ci
->vfs_inode
, cap
,
750 ceph_cap_string(cap
->issued
),
751 ceph_cap_string(mask
));
757 /* does a combination of caps satisfy mask? */
759 if ((have
& mask
) == mask
) {
760 dout("__ceph_caps_issued_mask %p combo issued %s"
761 " (mask %s)\n", &ci
->vfs_inode
,
762 ceph_cap_string(cap
->issued
),
763 ceph_cap_string(mask
));
767 /* touch this + preceeding caps */
769 for (q
= rb_first(&ci
->i_caps
); q
!= p
;
771 cap
= rb_entry(q
, struct ceph_cap
,
773 if (!__cap_is_valid(cap
))
786 * Return true if mask caps are currently being revoked by an MDS.
788 int ceph_caps_revoking(struct ceph_inode_info
*ci
, int mask
)
790 struct inode
*inode
= &ci
->vfs_inode
;
791 struct ceph_cap
*cap
;
795 spin_lock(&inode
->i_lock
);
796 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
797 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
798 if (__cap_is_valid(cap
) &&
799 (cap
->implemented
& ~cap
->issued
& mask
)) {
804 spin_unlock(&inode
->i_lock
);
805 dout("ceph_caps_revoking %p %s = %d\n", inode
,
806 ceph_cap_string(mask
), ret
);
810 int __ceph_caps_used(struct ceph_inode_info
*ci
)
814 used
|= CEPH_CAP_PIN
;
816 used
|= CEPH_CAP_FILE_RD
;
817 if (ci
->i_rdcache_ref
|| ci
->i_rdcache_gen
)
818 used
|= CEPH_CAP_FILE_CACHE
;
820 used
|= CEPH_CAP_FILE_WR
;
821 if (ci
->i_wrbuffer_ref
)
822 used
|= CEPH_CAP_FILE_BUFFER
;
827 * wanted, by virtue of open file modes
829 int __ceph_caps_file_wanted(struct ceph_inode_info
*ci
)
833 for (mode
= 0; mode
< CEPH_FILE_MODE_NUM
; mode
++)
834 if (ci
->i_nr_by_mode
[mode
])
835 want
|= ceph_caps_for_mode(mode
);
840 * Return caps we have registered with the MDS(s) as 'wanted'.
842 int __ceph_caps_mds_wanted(struct ceph_inode_info
*ci
)
844 struct ceph_cap
*cap
;
848 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
849 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
850 if (!__cap_is_valid(cap
))
852 mds_wanted
|= cap
->mds_wanted
;
858 * called under i_lock
860 static int __ceph_is_any_caps(struct ceph_inode_info
*ci
)
862 return !RB_EMPTY_ROOT(&ci
->i_caps
) || ci
->i_cap_exporting_mds
>= 0;
866 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
868 * caller should hold i_lock.
869 * caller will not hold session s_mutex if called from destroy_inode.
871 void __ceph_remove_cap(struct ceph_cap
*cap
)
873 struct ceph_mds_session
*session
= cap
->session
;
874 struct ceph_inode_info
*ci
= cap
->ci
;
875 struct ceph_mds_client
*mdsc
=
876 &ceph_sb_to_client(ci
->vfs_inode
.i_sb
)->mdsc
;
879 dout("__ceph_remove_cap %p from %p\n", cap
, &ci
->vfs_inode
);
881 /* remove from session list */
882 spin_lock(&session
->s_cap_lock
);
883 if (session
->s_cap_iterator
== cap
) {
884 /* not yet, we are iterating over this very cap */
885 dout("__ceph_remove_cap delaying %p removal from session %p\n",
888 list_del_init(&cap
->session_caps
);
889 session
->s_nr_caps
--;
893 /* protect backpointer with s_cap_lock: see iterate_session_caps */
895 spin_unlock(&session
->s_cap_lock
);
897 /* remove from inode list */
898 rb_erase(&cap
->ci_node
, &ci
->i_caps
);
899 if (ci
->i_auth_cap
== cap
)
900 ci
->i_auth_cap
= NULL
;
905 if (!__ceph_is_any_caps(ci
) && ci
->i_snap_realm
) {
906 struct ceph_snap_realm
*realm
= ci
->i_snap_realm
;
907 spin_lock(&realm
->inodes_with_caps_lock
);
908 list_del_init(&ci
->i_snap_realm_item
);
909 ci
->i_snap_realm_counter
++;
910 ci
->i_snap_realm
= NULL
;
911 spin_unlock(&realm
->inodes_with_caps_lock
);
912 ceph_put_snap_realm(mdsc
, realm
);
914 if (!__ceph_is_any_real_caps(ci
))
915 __cap_delay_cancel(mdsc
, ci
);
919 * Build and send a cap message to the given MDS.
921 * Caller should be holding s_mutex.
923 static int send_cap_msg(struct ceph_mds_session
*session
,
924 u64 ino
, u64 cid
, int op
,
925 int caps
, int wanted
, int dirty
,
926 u32 seq
, u64 flush_tid
, u32 issue_seq
, u32 mseq
,
927 u64 size
, u64 max_size
,
928 struct timespec
*mtime
, struct timespec
*atime
,
930 uid_t uid
, gid_t gid
, mode_t mode
,
932 struct ceph_buffer
*xattrs_buf
,
935 struct ceph_mds_caps
*fc
;
936 struct ceph_msg
*msg
;
938 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
939 " seq %u/%u mseq %u follows %lld size %llu/%llu"
940 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op
),
941 cid
, ino
, ceph_cap_string(caps
), ceph_cap_string(wanted
),
942 ceph_cap_string(dirty
),
943 seq
, issue_seq
, mseq
, follows
, size
, max_size
,
944 xattr_version
, xattrs_buf
? (int)xattrs_buf
->vec
.iov_len
: 0);
946 msg
= ceph_msg_new(CEPH_MSG_CLIENT_CAPS
, sizeof(*fc
), GFP_NOFS
);
950 msg
->hdr
.tid
= cpu_to_le64(flush_tid
);
952 fc
= msg
->front
.iov_base
;
953 memset(fc
, 0, sizeof(*fc
));
955 fc
->cap_id
= cpu_to_le64(cid
);
956 fc
->op
= cpu_to_le32(op
);
957 fc
->seq
= cpu_to_le32(seq
);
958 fc
->issue_seq
= cpu_to_le32(issue_seq
);
959 fc
->migrate_seq
= cpu_to_le32(mseq
);
960 fc
->caps
= cpu_to_le32(caps
);
961 fc
->wanted
= cpu_to_le32(wanted
);
962 fc
->dirty
= cpu_to_le32(dirty
);
963 fc
->ino
= cpu_to_le64(ino
);
964 fc
->snap_follows
= cpu_to_le64(follows
);
966 fc
->size
= cpu_to_le64(size
);
967 fc
->max_size
= cpu_to_le64(max_size
);
969 ceph_encode_timespec(&fc
->mtime
, mtime
);
971 ceph_encode_timespec(&fc
->atime
, atime
);
972 fc
->time_warp_seq
= cpu_to_le32(time_warp_seq
);
974 fc
->uid
= cpu_to_le32(uid
);
975 fc
->gid
= cpu_to_le32(gid
);
976 fc
->mode
= cpu_to_le32(mode
);
978 fc
->xattr_version
= cpu_to_le64(xattr_version
);
980 msg
->middle
= ceph_buffer_get(xattrs_buf
);
981 fc
->xattr_len
= cpu_to_le32(xattrs_buf
->vec
.iov_len
);
982 msg
->hdr
.middle_len
= cpu_to_le32(xattrs_buf
->vec
.iov_len
);
985 ceph_con_send(&session
->s_con
, msg
);
989 static void __queue_cap_release(struct ceph_mds_session
*session
,
990 u64 ino
, u64 cap_id
, u32 migrate_seq
,
993 struct ceph_msg
*msg
;
994 struct ceph_mds_cap_release
*head
;
995 struct ceph_mds_cap_item
*item
;
997 spin_lock(&session
->s_cap_lock
);
998 BUG_ON(!session
->s_num_cap_releases
);
999 msg
= list_first_entry(&session
->s_cap_releases
,
1000 struct ceph_msg
, list_head
);
1002 dout(" adding %llx release to mds%d msg %p (%d left)\n",
1003 ino
, session
->s_mds
, msg
, session
->s_num_cap_releases
);
1005 BUG_ON(msg
->front
.iov_len
+ sizeof(*item
) > PAGE_CACHE_SIZE
);
1006 head
= msg
->front
.iov_base
;
1007 head
->num
= cpu_to_le32(le32_to_cpu(head
->num
) + 1);
1008 item
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1009 item
->ino
= cpu_to_le64(ino
);
1010 item
->cap_id
= cpu_to_le64(cap_id
);
1011 item
->migrate_seq
= cpu_to_le32(migrate_seq
);
1012 item
->seq
= cpu_to_le32(issue_seq
);
1014 session
->s_num_cap_releases
--;
1016 msg
->front
.iov_len
+= sizeof(*item
);
1017 if (le32_to_cpu(head
->num
) == CEPH_CAPS_PER_RELEASE
) {
1018 dout(" release msg %p full\n", msg
);
1019 list_move_tail(&msg
->list_head
, &session
->s_cap_releases_done
);
1021 dout(" release msg %p at %d/%d (%d)\n", msg
,
1022 (int)le32_to_cpu(head
->num
),
1023 (int)CEPH_CAPS_PER_RELEASE
,
1024 (int)msg
->front
.iov_len
);
1026 spin_unlock(&session
->s_cap_lock
);
1030 * Queue cap releases when an inode is dropped from our cache. Since
1031 * inode is about to be destroyed, there is no need for i_lock.
1033 void ceph_queue_caps_release(struct inode
*inode
)
1035 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1038 p
= rb_first(&ci
->i_caps
);
1040 struct ceph_cap
*cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
1041 struct ceph_mds_session
*session
= cap
->session
;
1043 __queue_cap_release(session
, ceph_ino(inode
), cap
->cap_id
,
1044 cap
->mseq
, cap
->issue_seq
);
1046 __ceph_remove_cap(cap
);
1051 * Send a cap msg on the given inode. Update our caps state, then
1052 * drop i_lock and send the message.
1054 * Make note of max_size reported/requested from mds, revoked caps
1055 * that have now been implemented.
1057 * Make half-hearted attempt ot to invalidate page cache if we are
1058 * dropping RDCACHE. Note that this will leave behind locked pages
1059 * that we'll then need to deal with elsewhere.
1061 * Return non-zero if delayed release, or we experienced an error
1062 * such that the caller should requeue + retry later.
1064 * called with i_lock, then drops it.
1065 * caller should hold snap_rwsem (read), s_mutex.
1067 static int __send_cap(struct ceph_mds_client
*mdsc
, struct ceph_cap
*cap
,
1068 int op
, int used
, int want
, int retain
, int flushing
,
1069 unsigned *pflush_tid
)
1070 __releases(cap
->ci
->vfs_inode
->i_lock
)
1072 struct ceph_inode_info
*ci
= cap
->ci
;
1073 struct inode
*inode
= &ci
->vfs_inode
;
1074 u64 cap_id
= cap
->cap_id
;
1075 int held
, revoking
, dropping
, keep
;
1076 u64 seq
, issue_seq
, mseq
, time_warp_seq
, follows
;
1078 struct timespec mtime
, atime
;
1083 struct ceph_mds_session
*session
;
1084 u64 xattr_version
= 0;
1090 held
= cap
->issued
| cap
->implemented
;
1091 revoking
= cap
->implemented
& ~cap
->issued
;
1092 retain
&= ~revoking
;
1093 dropping
= cap
->issued
& ~retain
;
1095 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1096 inode
, cap
, cap
->session
,
1097 ceph_cap_string(held
), ceph_cap_string(held
& retain
),
1098 ceph_cap_string(revoking
));
1099 BUG_ON((retain
& CEPH_CAP_PIN
) == 0);
1101 session
= cap
->session
;
1103 /* don't release wanted unless we've waited a bit. */
1104 if ((ci
->i_ceph_flags
& CEPH_I_NODELAY
) == 0 &&
1105 time_before(jiffies
, ci
->i_hold_caps_min
)) {
1106 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1107 ceph_cap_string(cap
->issued
),
1108 ceph_cap_string(cap
->issued
& retain
),
1109 ceph_cap_string(cap
->mds_wanted
),
1110 ceph_cap_string(want
));
1111 want
|= cap
->mds_wanted
;
1112 retain
|= cap
->issued
;
1115 ci
->i_ceph_flags
&= ~(CEPH_I_NODELAY
| CEPH_I_FLUSH
);
1117 cap
->issued
&= retain
; /* drop bits we don't want */
1118 if (cap
->implemented
& ~cap
->issued
) {
1120 * Wake up any waiters on wanted -> needed transition.
1121 * This is due to the weird transition from buffered
1122 * to sync IO... we need to flush dirty pages _before_
1123 * allowing sync writes to avoid reordering.
1127 cap
->implemented
&= cap
->issued
| used
;
1128 cap
->mds_wanted
= want
;
1132 * assign a tid for flush operations so we can avoid
1133 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1134 * clean type races. track latest tid for every bit
1135 * so we can handle flush AxFw, flush Fw, and have the
1136 * first ack clean Ax.
1138 flush_tid
= ++ci
->i_cap_flush_last_tid
;
1140 *pflush_tid
= flush_tid
;
1141 dout(" cap_flush_tid %d\n", (int)flush_tid
);
1142 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
1143 if (flushing
& (1 << i
))
1144 ci
->i_cap_flush_tid
[i
] = flush_tid
;
1147 keep
= cap
->implemented
;
1149 issue_seq
= cap
->issue_seq
;
1151 size
= inode
->i_size
;
1152 ci
->i_reported_size
= size
;
1153 max_size
= ci
->i_wanted_max_size
;
1154 ci
->i_requested_max_size
= max_size
;
1155 mtime
= inode
->i_mtime
;
1156 atime
= inode
->i_atime
;
1157 time_warp_seq
= ci
->i_time_warp_seq
;
1158 follows
= ci
->i_snap_realm
->cached_context
->seq
;
1161 mode
= inode
->i_mode
;
1163 if (dropping
& CEPH_CAP_XATTR_EXCL
) {
1164 __ceph_build_xattrs_blob(ci
);
1165 xattr_version
= ci
->i_xattrs
.version
+ 1;
1168 spin_unlock(&inode
->i_lock
);
1170 ret
= send_cap_msg(session
, ceph_vino(inode
).ino
, cap_id
,
1171 op
, keep
, want
, flushing
, seq
, flush_tid
, issue_seq
, mseq
,
1172 size
, max_size
, &mtime
, &atime
, time_warp_seq
,
1175 (flushing
& CEPH_CAP_XATTR_EXCL
) ? ci
->i_xattrs
.blob
: NULL
,
1178 dout("error sending cap msg, must requeue %p\n", inode
);
1183 wake_up_all(&ci
->i_cap_wq
);
1189 * When a snapshot is taken, clients accumulate dirty metadata on
1190 * inodes with capabilities in ceph_cap_snaps to describe the file
1191 * state at the time the snapshot was taken. This must be flushed
1192 * asynchronously back to the MDS once sync writes complete and dirty
1193 * data is written out.
1195 * Called under i_lock. Takes s_mutex as needed.
1197 void __ceph_flush_snaps(struct ceph_inode_info
*ci
,
1198 struct ceph_mds_session
**psession
)
1200 struct inode
*inode
= &ci
->vfs_inode
;
1202 struct ceph_cap_snap
*capsnap
;
1204 struct ceph_mds_client
*mdsc
= &ceph_inode_to_client(inode
)->mdsc
;
1205 struct ceph_mds_session
*session
= NULL
; /* if session != NULL, we hold
1207 u64 next_follows
= 0; /* keep track of how far we've gotten through the
1208 i_cap_snaps list, and skip these entries next time
1209 around to avoid an infinite loop */
1212 session
= *psession
;
1214 dout("__flush_snaps %p\n", inode
);
1216 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
1217 /* avoid an infiniute loop after retry */
1218 if (capsnap
->follows
< next_follows
)
1221 * we need to wait for sync writes to complete and for dirty
1222 * pages to be written out.
1224 if (capsnap
->dirty_pages
|| capsnap
->writing
)
1228 * if cap writeback already occurred, we should have dropped
1229 * the capsnap in ceph_put_wrbuffer_cap_refs.
1231 BUG_ON(capsnap
->dirty
== 0);
1233 /* pick mds, take s_mutex */
1234 mds
= __ceph_get_cap_mds(ci
, &mseq
);
1235 if (session
&& session
->s_mds
!= mds
) {
1236 dout("oops, wrong session %p mutex\n", session
);
1237 mutex_unlock(&session
->s_mutex
);
1238 ceph_put_mds_session(session
);
1242 spin_unlock(&inode
->i_lock
);
1243 mutex_lock(&mdsc
->mutex
);
1244 session
= __ceph_lookup_mds_session(mdsc
, mds
);
1245 mutex_unlock(&mdsc
->mutex
);
1247 dout("inverting session/ino locks on %p\n",
1249 mutex_lock(&session
->s_mutex
);
1252 * if session == NULL, we raced against a cap
1253 * deletion. retry, and we'll get a better
1254 * @mds value next time.
1256 spin_lock(&inode
->i_lock
);
1260 capsnap
->flush_tid
= ++ci
->i_cap_flush_last_tid
;
1261 atomic_inc(&capsnap
->nref
);
1262 if (!list_empty(&capsnap
->flushing_item
))
1263 list_del_init(&capsnap
->flushing_item
);
1264 list_add_tail(&capsnap
->flushing_item
,
1265 &session
->s_cap_snaps_flushing
);
1266 spin_unlock(&inode
->i_lock
);
1268 dout("flush_snaps %p cap_snap %p follows %lld size %llu\n",
1269 inode
, capsnap
, next_follows
, capsnap
->size
);
1270 send_cap_msg(session
, ceph_vino(inode
).ino
, 0,
1271 CEPH_CAP_OP_FLUSHSNAP
, capsnap
->issued
, 0,
1272 capsnap
->dirty
, 0, capsnap
->flush_tid
, 0, mseq
,
1274 &capsnap
->mtime
, &capsnap
->atime
,
1275 capsnap
->time_warp_seq
,
1276 capsnap
->uid
, capsnap
->gid
, capsnap
->mode
,
1280 next_follows
= capsnap
->follows
+ 1;
1281 ceph_put_cap_snap(capsnap
);
1283 spin_lock(&inode
->i_lock
);
1287 /* we flushed them all; remove this inode from the queue */
1288 spin_lock(&mdsc
->snap_flush_lock
);
1289 list_del_init(&ci
->i_snap_flush_item
);
1290 spin_unlock(&mdsc
->snap_flush_lock
);
1293 *psession
= session
;
1295 mutex_unlock(&session
->s_mutex
);
1296 ceph_put_mds_session(session
);
1300 static void ceph_flush_snaps(struct ceph_inode_info
*ci
)
1302 struct inode
*inode
= &ci
->vfs_inode
;
1304 spin_lock(&inode
->i_lock
);
1305 __ceph_flush_snaps(ci
, NULL
);
1306 spin_unlock(&inode
->i_lock
);
1310 * Mark caps dirty. If inode is newly dirty, add to the global dirty
1313 void __ceph_mark_dirty_caps(struct ceph_inode_info
*ci
, int mask
)
1315 struct ceph_mds_client
*mdsc
=
1316 &ceph_sb_to_client(ci
->vfs_inode
.i_sb
)->mdsc
;
1317 struct inode
*inode
= &ci
->vfs_inode
;
1318 int was
= ci
->i_dirty_caps
;
1321 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci
->vfs_inode
,
1322 ceph_cap_string(mask
), ceph_cap_string(was
),
1323 ceph_cap_string(was
| mask
));
1324 ci
->i_dirty_caps
|= mask
;
1326 dout(" inode %p now dirty\n", &ci
->vfs_inode
);
1327 BUG_ON(!list_empty(&ci
->i_dirty_item
));
1328 spin_lock(&mdsc
->cap_dirty_lock
);
1329 list_add(&ci
->i_dirty_item
, &mdsc
->cap_dirty
);
1330 spin_unlock(&mdsc
->cap_dirty_lock
);
1331 if (ci
->i_flushing_caps
== 0) {
1333 dirty
|= I_DIRTY_SYNC
;
1336 BUG_ON(list_empty(&ci
->i_dirty_item
));
1337 if (((was
| ci
->i_flushing_caps
) & CEPH_CAP_FILE_BUFFER
) &&
1338 (mask
& CEPH_CAP_FILE_BUFFER
))
1339 dirty
|= I_DIRTY_DATASYNC
;
1341 __mark_inode_dirty(inode
, dirty
);
1342 __cap_delay_requeue(mdsc
, ci
);
1346 * Add dirty inode to the flushing list. Assigned a seq number so we
1347 * can wait for caps to flush without starving.
1349 * Called under i_lock.
1351 static int __mark_caps_flushing(struct inode
*inode
,
1352 struct ceph_mds_session
*session
)
1354 struct ceph_mds_client
*mdsc
= &ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1355 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1358 BUG_ON(ci
->i_dirty_caps
== 0);
1359 BUG_ON(list_empty(&ci
->i_dirty_item
));
1361 flushing
= ci
->i_dirty_caps
;
1362 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1363 ceph_cap_string(flushing
),
1364 ceph_cap_string(ci
->i_flushing_caps
),
1365 ceph_cap_string(ci
->i_flushing_caps
| flushing
));
1366 ci
->i_flushing_caps
|= flushing
;
1367 ci
->i_dirty_caps
= 0;
1368 dout(" inode %p now !dirty\n", inode
);
1370 spin_lock(&mdsc
->cap_dirty_lock
);
1371 list_del_init(&ci
->i_dirty_item
);
1373 ci
->i_cap_flush_seq
= ++mdsc
->cap_flush_seq
;
1374 if (list_empty(&ci
->i_flushing_item
)) {
1375 list_add_tail(&ci
->i_flushing_item
, &session
->s_cap_flushing
);
1376 mdsc
->num_cap_flushing
++;
1377 dout(" inode %p now flushing seq %lld\n", inode
,
1378 ci
->i_cap_flush_seq
);
1380 list_move_tail(&ci
->i_flushing_item
, &session
->s_cap_flushing
);
1381 dout(" inode %p now flushing (more) seq %lld\n", inode
,
1382 ci
->i_cap_flush_seq
);
1384 spin_unlock(&mdsc
->cap_dirty_lock
);
1390 * try to invalidate mapping pages without blocking.
1392 static int mapping_is_empty(struct address_space
*mapping
)
1394 struct page
*page
= find_get_page(mapping
, 0);
1403 static int try_nonblocking_invalidate(struct inode
*inode
)
1405 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1406 u32 invalidating_gen
= ci
->i_rdcache_gen
;
1408 spin_unlock(&inode
->i_lock
);
1409 invalidate_mapping_pages(&inode
->i_data
, 0, -1);
1410 spin_lock(&inode
->i_lock
);
1412 if (mapping_is_empty(&inode
->i_data
) &&
1413 invalidating_gen
== ci
->i_rdcache_gen
) {
1415 dout("try_nonblocking_invalidate %p success\n", inode
);
1416 ci
->i_rdcache_gen
= 0;
1417 ci
->i_rdcache_revoking
= 0;
1420 dout("try_nonblocking_invalidate %p failed\n", inode
);
1425 * Swiss army knife function to examine currently used and wanted
1426 * versus held caps. Release, flush, ack revoked caps to mds as
1429 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1430 * cap release further.
1431 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1432 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1435 void ceph_check_caps(struct ceph_inode_info
*ci
, int flags
,
1436 struct ceph_mds_session
*session
)
1437 __releases(session
->s_mutex
)
1439 struct ceph_client
*client
= ceph_inode_to_client(&ci
->vfs_inode
);
1440 struct ceph_mds_client
*mdsc
= &client
->mdsc
;
1441 struct inode
*inode
= &ci
->vfs_inode
;
1442 struct ceph_cap
*cap
;
1443 int file_wanted
, used
;
1444 int took_snap_rwsem
= 0; /* true if mdsc->snap_rwsem held */
1445 int issued
, implemented
, want
, retain
, revoking
, flushing
= 0;
1446 int mds
= -1; /* keep track of how far we've gone through i_caps list
1447 to avoid an infinite loop on retry */
1449 int tried_invalidate
= 0;
1450 int delayed
= 0, sent
= 0, force_requeue
= 0, num
;
1451 int queue_invalidate
= 0;
1452 int is_delayed
= flags
& CHECK_CAPS_NODELAY
;
1454 /* if we are unmounting, flush any unused caps immediately. */
1458 spin_lock(&inode
->i_lock
);
1460 if (ci
->i_ceph_flags
& CEPH_I_FLUSH
)
1461 flags
|= CHECK_CAPS_FLUSH
;
1463 /* flush snaps first time around only */
1464 if (!list_empty(&ci
->i_cap_snaps
))
1465 __ceph_flush_snaps(ci
, &session
);
1468 spin_lock(&inode
->i_lock
);
1470 file_wanted
= __ceph_caps_file_wanted(ci
);
1471 used
= __ceph_caps_used(ci
);
1472 want
= file_wanted
| used
;
1473 issued
= __ceph_caps_issued(ci
, &implemented
);
1474 revoking
= implemented
& ~issued
;
1476 retain
= want
| CEPH_CAP_PIN
;
1477 if (!mdsc
->stopping
&& inode
->i_nlink
> 0) {
1479 retain
|= CEPH_CAP_ANY
; /* be greedy */
1481 retain
|= CEPH_CAP_ANY_SHARED
;
1483 * keep RD only if we didn't have the file open RW,
1484 * because then the mds would revoke it anyway to
1485 * journal max_size=0.
1487 if (ci
->i_max_size
== 0)
1488 retain
|= CEPH_CAP_ANY_RD
;
1492 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1493 " issued %s revoking %s retain %s %s%s%s\n", inode
,
1494 ceph_cap_string(file_wanted
),
1495 ceph_cap_string(used
), ceph_cap_string(ci
->i_dirty_caps
),
1496 ceph_cap_string(ci
->i_flushing_caps
),
1497 ceph_cap_string(issued
), ceph_cap_string(revoking
),
1498 ceph_cap_string(retain
),
1499 (flags
& CHECK_CAPS_AUTHONLY
) ? " AUTHONLY" : "",
1500 (flags
& CHECK_CAPS_NODELAY
) ? " NODELAY" : "",
1501 (flags
& CHECK_CAPS_FLUSH
) ? " FLUSH" : "");
1504 * If we no longer need to hold onto old our caps, and we may
1505 * have cached pages, but don't want them, then try to invalidate.
1506 * If we fail, it's because pages are locked.... try again later.
1508 if ((!is_delayed
|| mdsc
->stopping
) &&
1509 ci
->i_wrbuffer_ref
== 0 && /* no dirty pages... */
1510 ci
->i_rdcache_gen
&& /* may have cached pages */
1511 (file_wanted
== 0 || /* no open files */
1512 (revoking
& (CEPH_CAP_FILE_CACHE
|
1513 CEPH_CAP_FILE_LAZYIO
))) && /* 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 CEPH_CAP_FILE_LAZYIO
)) {
1519 dout("check_caps queuing invalidate\n");
1520 queue_invalidate
= 1;
1521 ci
->i_rdcache_revoking
= ci
->i_rdcache_gen
;
1523 dout("check_caps failed to invalidate pages\n");
1524 /* we failed to invalidate pages. check these
1525 caps again later. */
1527 __cap_set_timeouts(mdsc
, ci
);
1530 tried_invalidate
= 1;
1535 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
1536 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
1539 /* avoid looping forever */
1540 if (mds
>= cap
->mds
||
1541 ((flags
& CHECK_CAPS_AUTHONLY
) && cap
!= ci
->i_auth_cap
))
1544 /* NOTE: no side-effects allowed, until we take s_mutex */
1546 revoking
= cap
->implemented
& ~cap
->issued
;
1548 dout(" mds%d revoking %s\n", cap
->mds
,
1549 ceph_cap_string(revoking
));
1551 if (cap
== ci
->i_auth_cap
&&
1552 (cap
->issued
& CEPH_CAP_FILE_WR
)) {
1553 /* request larger max_size from MDS? */
1554 if (ci
->i_wanted_max_size
> ci
->i_max_size
&&
1555 ci
->i_wanted_max_size
> ci
->i_requested_max_size
) {
1556 dout("requesting new max_size\n");
1560 /* approaching file_max? */
1561 if ((inode
->i_size
<< 1) >= ci
->i_max_size
&&
1562 (ci
->i_reported_size
<< 1) < ci
->i_max_size
) {
1563 dout("i_size approaching max_size\n");
1567 /* flush anything dirty? */
1568 if (cap
== ci
->i_auth_cap
&& (flags
& CHECK_CAPS_FLUSH
) &&
1570 dout("flushing dirty caps\n");
1574 /* completed revocation? going down and there are no caps? */
1575 if (revoking
&& (revoking
& used
) == 0) {
1576 dout("completed revocation of %s\n",
1577 ceph_cap_string(cap
->implemented
& ~cap
->issued
));
1581 /* want more caps from mds? */
1582 if (want
& ~(cap
->mds_wanted
| cap
->issued
))
1585 /* things we might delay */
1586 if ((cap
->issued
& ~retain
) == 0 &&
1587 cap
->mds_wanted
== want
)
1588 continue; /* nope, all good */
1594 if ((ci
->i_ceph_flags
& CEPH_I_NODELAY
) == 0 &&
1595 time_before(jiffies
, ci
->i_hold_caps_max
)) {
1596 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1597 ceph_cap_string(cap
->issued
),
1598 ceph_cap_string(cap
->issued
& retain
),
1599 ceph_cap_string(cap
->mds_wanted
),
1600 ceph_cap_string(want
));
1606 if (ci
->i_ceph_flags
& CEPH_I_NOFLUSH
) {
1607 dout(" skipping %p I_NOFLUSH set\n", inode
);
1611 if (session
&& session
!= cap
->session
) {
1612 dout("oops, wrong session %p mutex\n", session
);
1613 mutex_unlock(&session
->s_mutex
);
1617 session
= cap
->session
;
1618 if (mutex_trylock(&session
->s_mutex
) == 0) {
1619 dout("inverting session/ino locks on %p\n",
1621 spin_unlock(&inode
->i_lock
);
1622 if (took_snap_rwsem
) {
1623 up_read(&mdsc
->snap_rwsem
);
1624 took_snap_rwsem
= 0;
1626 mutex_lock(&session
->s_mutex
);
1630 /* take snap_rwsem after session mutex */
1631 if (!took_snap_rwsem
) {
1632 if (down_read_trylock(&mdsc
->snap_rwsem
) == 0) {
1633 dout("inverting snap/in locks on %p\n",
1635 spin_unlock(&inode
->i_lock
);
1636 down_read(&mdsc
->snap_rwsem
);
1637 took_snap_rwsem
= 1;
1640 took_snap_rwsem
= 1;
1643 if (cap
== ci
->i_auth_cap
&& ci
->i_dirty_caps
)
1644 flushing
= __mark_caps_flushing(inode
, session
);
1646 mds
= cap
->mds
; /* remember mds, so we don't repeat */
1649 /* __send_cap drops i_lock */
1650 delayed
+= __send_cap(mdsc
, cap
, CEPH_CAP_OP_UPDATE
, used
, want
,
1651 retain
, flushing
, NULL
);
1652 goto retry
; /* retake i_lock and restart our cap scan. */
1656 * Reschedule delayed caps release if we delayed anything,
1659 if (delayed
&& is_delayed
)
1660 force_requeue
= 1; /* __send_cap delayed release; requeue */
1661 if (!delayed
&& !is_delayed
)
1662 __cap_delay_cancel(mdsc
, ci
);
1663 else if (!is_delayed
|| force_requeue
)
1664 __cap_delay_requeue(mdsc
, ci
);
1666 spin_unlock(&inode
->i_lock
);
1668 if (queue_invalidate
)
1669 ceph_queue_invalidate(inode
);
1672 mutex_unlock(&session
->s_mutex
);
1673 if (took_snap_rwsem
)
1674 up_read(&mdsc
->snap_rwsem
);
1678 * Try to flush dirty caps back to the auth mds.
1680 static int try_flush_caps(struct inode
*inode
, struct ceph_mds_session
*session
,
1681 unsigned *flush_tid
)
1683 struct ceph_mds_client
*mdsc
= &ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1684 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1685 int unlock_session
= session
? 0 : 1;
1689 spin_lock(&inode
->i_lock
);
1690 if (ci
->i_ceph_flags
& CEPH_I_NOFLUSH
) {
1691 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode
);
1694 if (ci
->i_dirty_caps
&& ci
->i_auth_cap
) {
1695 struct ceph_cap
*cap
= ci
->i_auth_cap
;
1696 int used
= __ceph_caps_used(ci
);
1697 int want
= __ceph_caps_wanted(ci
);
1701 spin_unlock(&inode
->i_lock
);
1702 session
= cap
->session
;
1703 mutex_lock(&session
->s_mutex
);
1706 BUG_ON(session
!= cap
->session
);
1707 if (cap
->session
->s_state
< CEPH_MDS_SESSION_OPEN
)
1710 flushing
= __mark_caps_flushing(inode
, session
);
1712 /* __send_cap drops i_lock */
1713 delayed
= __send_cap(mdsc
, cap
, CEPH_CAP_OP_FLUSH
, used
, want
,
1714 cap
->issued
| cap
->implemented
, flushing
,
1719 spin_lock(&inode
->i_lock
);
1720 __cap_delay_requeue(mdsc
, ci
);
1723 spin_unlock(&inode
->i_lock
);
1725 if (session
&& unlock_session
)
1726 mutex_unlock(&session
->s_mutex
);
1731 * Return true if we've flushed caps through the given flush_tid.
1733 static int caps_are_flushed(struct inode
*inode
, unsigned tid
)
1735 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1738 spin_lock(&inode
->i_lock
);
1739 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
1740 if ((ci
->i_flushing_caps
& (1 << i
)) &&
1741 ci
->i_cap_flush_tid
[i
] <= tid
) {
1742 /* still flushing this bit */
1746 spin_unlock(&inode
->i_lock
);
1751 * Wait on any unsafe replies for the given inode. First wait on the
1752 * newest request, and make that the upper bound. Then, if there are
1753 * more requests, keep waiting on the oldest as long as it is still older
1754 * than the original request.
1756 static void sync_write_wait(struct inode
*inode
)
1758 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1759 struct list_head
*head
= &ci
->i_unsafe_writes
;
1760 struct ceph_osd_request
*req
;
1763 spin_lock(&ci
->i_unsafe_lock
);
1764 if (list_empty(head
))
1767 /* set upper bound as _last_ entry in chain */
1768 req
= list_entry(head
->prev
, struct ceph_osd_request
,
1770 last_tid
= req
->r_tid
;
1773 ceph_osdc_get_request(req
);
1774 spin_unlock(&ci
->i_unsafe_lock
);
1775 dout("sync_write_wait on tid %llu (until %llu)\n",
1776 req
->r_tid
, last_tid
);
1777 wait_for_completion(&req
->r_safe_completion
);
1778 spin_lock(&ci
->i_unsafe_lock
);
1779 ceph_osdc_put_request(req
);
1782 * from here on look at first entry in chain, since we
1783 * only want to wait for anything older than last_tid
1785 if (list_empty(head
))
1787 req
= list_entry(head
->next
, struct ceph_osd_request
,
1789 } while (req
->r_tid
< last_tid
);
1791 spin_unlock(&ci
->i_unsafe_lock
);
1794 int ceph_fsync(struct file
*file
, int datasync
)
1796 struct inode
*inode
= file
->f_mapping
->host
;
1797 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1802 dout("fsync %p%s\n", inode
, datasync
? " datasync" : "");
1803 sync_write_wait(inode
);
1805 ret
= filemap_write_and_wait(inode
->i_mapping
);
1809 dirty
= try_flush_caps(inode
, NULL
, &flush_tid
);
1810 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty
));
1813 * only wait on non-file metadata writeback (the mds
1814 * can recover size and mtime, so we don't need to
1817 if (!datasync
&& (dirty
& ~CEPH_CAP_ANY_FILE_WR
)) {
1818 dout("fsync waiting for flush_tid %u\n", flush_tid
);
1819 ret
= wait_event_interruptible(ci
->i_cap_wq
,
1820 caps_are_flushed(inode
, flush_tid
));
1823 dout("fsync %p%s done\n", inode
, datasync
? " datasync" : "");
1828 * Flush any dirty caps back to the mds. If we aren't asked to wait,
1829 * queue inode for flush but don't do so immediately, because we can
1830 * get by with fewer MDS messages if we wait for data writeback to
1833 int ceph_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1835 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1839 int wait
= wbc
->sync_mode
== WB_SYNC_ALL
;
1841 dout("write_inode %p wait=%d\n", inode
, wait
);
1843 dirty
= try_flush_caps(inode
, NULL
, &flush_tid
);
1845 err
= wait_event_interruptible(ci
->i_cap_wq
,
1846 caps_are_flushed(inode
, flush_tid
));
1848 struct ceph_mds_client
*mdsc
=
1849 &ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1851 spin_lock(&inode
->i_lock
);
1852 if (__ceph_caps_dirty(ci
))
1853 __cap_delay_requeue_front(mdsc
, ci
);
1854 spin_unlock(&inode
->i_lock
);
1860 * After a recovering MDS goes active, we need to resend any caps
1863 * Caller holds session->s_mutex.
1865 static void kick_flushing_capsnaps(struct ceph_mds_client
*mdsc
,
1866 struct ceph_mds_session
*session
)
1868 struct ceph_cap_snap
*capsnap
;
1870 dout("kick_flushing_capsnaps mds%d\n", session
->s_mds
);
1871 list_for_each_entry(capsnap
, &session
->s_cap_snaps_flushing
,
1873 struct ceph_inode_info
*ci
= capsnap
->ci
;
1874 struct inode
*inode
= &ci
->vfs_inode
;
1875 struct ceph_cap
*cap
;
1877 spin_lock(&inode
->i_lock
);
1878 cap
= ci
->i_auth_cap
;
1879 if (cap
&& cap
->session
== session
) {
1880 dout("kick_flushing_caps %p cap %p capsnap %p\n", inode
,
1882 __ceph_flush_snaps(ci
, &session
);
1884 pr_err("%p auth cap %p not mds%d ???\n", inode
,
1885 cap
, session
->s_mds
);
1887 spin_unlock(&inode
->i_lock
);
1891 void ceph_kick_flushing_caps(struct ceph_mds_client
*mdsc
,
1892 struct ceph_mds_session
*session
)
1894 struct ceph_inode_info
*ci
;
1896 kick_flushing_capsnaps(mdsc
, session
);
1898 dout("kick_flushing_caps mds%d\n", session
->s_mds
);
1899 list_for_each_entry(ci
, &session
->s_cap_flushing
, i_flushing_item
) {
1900 struct inode
*inode
= &ci
->vfs_inode
;
1901 struct ceph_cap
*cap
;
1904 spin_lock(&inode
->i_lock
);
1905 cap
= ci
->i_auth_cap
;
1906 if (cap
&& cap
->session
== session
) {
1907 dout("kick_flushing_caps %p cap %p %s\n", inode
,
1908 cap
, ceph_cap_string(ci
->i_flushing_caps
));
1909 delayed
= __send_cap(mdsc
, cap
, CEPH_CAP_OP_FLUSH
,
1910 __ceph_caps_used(ci
),
1911 __ceph_caps_wanted(ci
),
1912 cap
->issued
| cap
->implemented
,
1913 ci
->i_flushing_caps
, NULL
);
1915 spin_lock(&inode
->i_lock
);
1916 __cap_delay_requeue(mdsc
, ci
);
1917 spin_unlock(&inode
->i_lock
);
1920 pr_err("%p auth cap %p not mds%d ???\n", inode
,
1921 cap
, session
->s_mds
);
1922 spin_unlock(&inode
->i_lock
);
1929 * Take references to capabilities we hold, so that we don't release
1930 * them to the MDS prematurely.
1932 * Protected by i_lock.
1934 static void __take_cap_refs(struct ceph_inode_info
*ci
, int got
)
1936 if (got
& CEPH_CAP_PIN
)
1938 if (got
& CEPH_CAP_FILE_RD
)
1940 if (got
& CEPH_CAP_FILE_CACHE
)
1941 ci
->i_rdcache_ref
++;
1942 if (got
& CEPH_CAP_FILE_WR
)
1944 if (got
& CEPH_CAP_FILE_BUFFER
) {
1945 if (ci
->i_wrbuffer_ref
== 0)
1946 igrab(&ci
->vfs_inode
);
1947 ci
->i_wrbuffer_ref
++;
1948 dout("__take_cap_refs %p wrbuffer %d -> %d (?)\n",
1949 &ci
->vfs_inode
, ci
->i_wrbuffer_ref
-1, ci
->i_wrbuffer_ref
);
1954 * Try to grab cap references. Specify those refs we @want, and the
1955 * minimal set we @need. Also include the larger offset we are writing
1956 * to (when applicable), and check against max_size here as well.
1957 * Note that caller is responsible for ensuring max_size increases are
1958 * requested from the MDS.
1960 static int try_get_cap_refs(struct ceph_inode_info
*ci
, int need
, int want
,
1961 int *got
, loff_t endoff
, int *check_max
, int *err
)
1963 struct inode
*inode
= &ci
->vfs_inode
;
1965 int have
, implemented
;
1968 dout("get_cap_refs %p need %s want %s\n", inode
,
1969 ceph_cap_string(need
), ceph_cap_string(want
));
1970 spin_lock(&inode
->i_lock
);
1972 /* make sure file is actually open */
1973 file_wanted
= __ceph_caps_file_wanted(ci
);
1974 if ((file_wanted
& need
) == 0) {
1975 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
1976 ceph_cap_string(need
), ceph_cap_string(file_wanted
));
1982 if (need
& CEPH_CAP_FILE_WR
) {
1983 if (endoff
>= 0 && endoff
> (loff_t
)ci
->i_max_size
) {
1984 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
1985 inode
, endoff
, ci
->i_max_size
);
1986 if (endoff
> ci
->i_wanted_max_size
) {
1993 * If a sync write is in progress, we must wait, so that we
1994 * can get a final snapshot value for size+mtime.
1996 if (__ceph_have_pending_cap_snap(ci
)) {
1997 dout("get_cap_refs %p cap_snap_pending\n", inode
);
2001 have
= __ceph_caps_issued(ci
, &implemented
);
2004 * disallow writes while a truncate is pending
2006 if (ci
->i_truncate_pending
)
2007 have
&= ~CEPH_CAP_FILE_WR
;
2009 if ((have
& need
) == need
) {
2011 * Look at (implemented & ~have & not) so that we keep waiting
2012 * on transition from wanted -> needed caps. This is needed
2013 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2014 * going before a prior buffered writeback happens.
2016 int not = want
& ~(have
& need
);
2017 int revoking
= implemented
& ~have
;
2018 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2019 inode
, ceph_cap_string(have
), ceph_cap_string(not),
2020 ceph_cap_string(revoking
));
2021 if ((revoking
& not) == 0) {
2022 *got
= need
| (have
& want
);
2023 __take_cap_refs(ci
, *got
);
2027 dout("get_cap_refs %p have %s needed %s\n", inode
,
2028 ceph_cap_string(have
), ceph_cap_string(need
));
2031 spin_unlock(&inode
->i_lock
);
2032 dout("get_cap_refs %p ret %d got %s\n", inode
,
2033 ret
, ceph_cap_string(*got
));
2038 * Check the offset we are writing up to against our current
2039 * max_size. If necessary, tell the MDS we want to write to
2042 static void check_max_size(struct inode
*inode
, loff_t endoff
)
2044 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2047 /* do we need to explicitly request a larger max_size? */
2048 spin_lock(&inode
->i_lock
);
2049 if ((endoff
>= ci
->i_max_size
||
2050 endoff
> (inode
->i_size
<< 1)) &&
2051 endoff
> ci
->i_wanted_max_size
) {
2052 dout("write %p at large endoff %llu, req max_size\n",
2054 ci
->i_wanted_max_size
= endoff
;
2057 spin_unlock(&inode
->i_lock
);
2059 ceph_check_caps(ci
, CHECK_CAPS_AUTHONLY
, NULL
);
2063 * Wait for caps, and take cap references. If we can't get a WR cap
2064 * due to a small max_size, make sure we check_max_size (and possibly
2065 * ask the mds) so we don't get hung up indefinitely.
2067 int ceph_get_caps(struct ceph_inode_info
*ci
, int need
, int want
, int *got
,
2070 int check_max
, ret
, err
;
2074 check_max_size(&ci
->vfs_inode
, endoff
);
2077 ret
= wait_event_interruptible(ci
->i_cap_wq
,
2078 try_get_cap_refs(ci
, need
, want
,
2089 * Take cap refs. Caller must already know we hold at least one ref
2090 * on the caps in question or we don't know this is safe.
2092 void ceph_get_cap_refs(struct ceph_inode_info
*ci
, int caps
)
2094 spin_lock(&ci
->vfs_inode
.i_lock
);
2095 __take_cap_refs(ci
, caps
);
2096 spin_unlock(&ci
->vfs_inode
.i_lock
);
2102 * If we released the last ref on any given cap, call ceph_check_caps
2103 * to release (or schedule a release).
2105 * If we are releasing a WR cap (from a sync write), finalize any affected
2106 * cap_snap, and wake up any waiters.
2108 void ceph_put_cap_refs(struct ceph_inode_info
*ci
, int had
)
2110 struct inode
*inode
= &ci
->vfs_inode
;
2111 int last
= 0, put
= 0, flushsnaps
= 0, wake
= 0;
2112 struct ceph_cap_snap
*capsnap
;
2114 spin_lock(&inode
->i_lock
);
2115 if (had
& CEPH_CAP_PIN
)
2117 if (had
& CEPH_CAP_FILE_RD
)
2118 if (--ci
->i_rd_ref
== 0)
2120 if (had
& CEPH_CAP_FILE_CACHE
)
2121 if (--ci
->i_rdcache_ref
== 0)
2123 if (had
& CEPH_CAP_FILE_BUFFER
) {
2124 if (--ci
->i_wrbuffer_ref
== 0) {
2128 dout("put_cap_refs %p wrbuffer %d -> %d (?)\n",
2129 inode
, ci
->i_wrbuffer_ref
+1, ci
->i_wrbuffer_ref
);
2131 if (had
& CEPH_CAP_FILE_WR
)
2132 if (--ci
->i_wr_ref
== 0) {
2134 if (!list_empty(&ci
->i_cap_snaps
)) {
2135 capsnap
= list_first_entry(&ci
->i_cap_snaps
,
2136 struct ceph_cap_snap
,
2138 if (capsnap
->writing
) {
2139 capsnap
->writing
= 0;
2141 __ceph_finish_cap_snap(ci
,
2147 spin_unlock(&inode
->i_lock
);
2149 dout("put_cap_refs %p had %s%s%s\n", inode
, ceph_cap_string(had
),
2150 last
? " last" : "", put
? " put" : "");
2152 if (last
&& !flushsnaps
)
2153 ceph_check_caps(ci
, 0, NULL
);
2154 else if (flushsnaps
)
2155 ceph_flush_snaps(ci
);
2157 wake_up_all(&ci
->i_cap_wq
);
2163 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2164 * context. Adjust per-snap dirty page accounting as appropriate.
2165 * Once all dirty data for a cap_snap is flushed, flush snapped file
2166 * metadata back to the MDS. If we dropped the last ref, call
2169 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info
*ci
, int nr
,
2170 struct ceph_snap_context
*snapc
)
2172 struct inode
*inode
= &ci
->vfs_inode
;
2174 int complete_capsnap
= 0;
2175 int drop_capsnap
= 0;
2177 struct ceph_cap_snap
*capsnap
= NULL
;
2179 spin_lock(&inode
->i_lock
);
2180 ci
->i_wrbuffer_ref
-= nr
;
2181 last
= !ci
->i_wrbuffer_ref
;
2183 if (ci
->i_head_snapc
== snapc
) {
2184 ci
->i_wrbuffer_ref_head
-= nr
;
2185 if (!ci
->i_wrbuffer_ref_head
) {
2186 ceph_put_snap_context(ci
->i_head_snapc
);
2187 ci
->i_head_snapc
= NULL
;
2189 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2191 ci
->i_wrbuffer_ref
+nr
, ci
->i_wrbuffer_ref_head
+nr
,
2192 ci
->i_wrbuffer_ref
, ci
->i_wrbuffer_ref_head
,
2193 last
? " LAST" : "");
2195 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
2196 if (capsnap
->context
== snapc
) {
2202 capsnap
->dirty_pages
-= nr
;
2203 if (capsnap
->dirty_pages
== 0) {
2204 complete_capsnap
= 1;
2205 if (capsnap
->dirty
== 0)
2206 /* cap writeback completed before we created
2207 * the cap_snap; no FLUSHSNAP is needed */
2210 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2211 " snap %lld %d/%d -> %d/%d %s%s%s\n",
2212 inode
, capsnap
, capsnap
->context
->seq
,
2213 ci
->i_wrbuffer_ref
+nr
, capsnap
->dirty_pages
+ nr
,
2214 ci
->i_wrbuffer_ref
, capsnap
->dirty_pages
,
2215 last
? " (wrbuffer last)" : "",
2216 complete_capsnap
? " (complete capsnap)" : "",
2217 drop_capsnap
? " (drop capsnap)" : "");
2219 ceph_put_snap_context(capsnap
->context
);
2220 list_del(&capsnap
->ci_item
);
2221 list_del(&capsnap
->flushing_item
);
2222 ceph_put_cap_snap(capsnap
);
2226 spin_unlock(&inode
->i_lock
);
2229 ceph_check_caps(ci
, CHECK_CAPS_AUTHONLY
, NULL
);
2231 } else if (complete_capsnap
) {
2232 ceph_flush_snaps(ci
);
2233 wake_up_all(&ci
->i_cap_wq
);
2240 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2241 * actually be a revocation if it specifies a smaller cap set.)
2243 * caller holds s_mutex and i_lock, we drop both.
2247 * 1 - check_caps on auth cap only (writeback)
2248 * 2 - check_caps (ack revoke)
2250 static void handle_cap_grant(struct inode
*inode
, struct ceph_mds_caps
*grant
,
2251 struct ceph_mds_session
*session
,
2252 struct ceph_cap
*cap
,
2253 struct ceph_buffer
*xattr_buf
)
2254 __releases(inode
->i_lock
)
2255 __releases(session
->s_mutex
)
2257 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2258 int mds
= session
->s_mds
;
2259 int seq
= le32_to_cpu(grant
->seq
);
2260 int newcaps
= le32_to_cpu(grant
->caps
);
2261 int issued
, implemented
, used
, wanted
, dirty
;
2262 u64 size
= le64_to_cpu(grant
->size
);
2263 u64 max_size
= le64_to_cpu(grant
->max_size
);
2264 struct timespec mtime
, atime
, ctime
;
2268 int revoked_rdcache
= 0;
2269 int queue_invalidate
= 0;
2271 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2272 inode
, cap
, mds
, seq
, ceph_cap_string(newcaps
));
2273 dout(" size %llu max_size %llu, i_size %llu\n", size
, max_size
,
2277 * If CACHE is being revoked, and we have no dirty buffers,
2278 * try to invalidate (once). (If there are dirty buffers, we
2279 * will invalidate _after_ writeback.)
2281 if (((cap
->issued
& ~newcaps
) & CEPH_CAP_FILE_CACHE
) &&
2282 (newcaps
& CEPH_CAP_FILE_LAZYIO
) == 0 &&
2283 !ci
->i_wrbuffer_ref
) {
2284 if (try_nonblocking_invalidate(inode
) == 0) {
2285 revoked_rdcache
= 1;
2287 /* there were locked pages.. invalidate later
2288 in a separate thread. */
2289 if (ci
->i_rdcache_revoking
!= ci
->i_rdcache_gen
) {
2290 queue_invalidate
= 1;
2291 ci
->i_rdcache_revoking
= ci
->i_rdcache_gen
;
2296 /* side effects now are allowed */
2298 issued
= __ceph_caps_issued(ci
, &implemented
);
2299 issued
|= implemented
| __ceph_caps_dirty(ci
);
2301 cap
->cap_gen
= session
->s_cap_gen
;
2303 __check_cap_issue(ci
, cap
, newcaps
);
2305 if ((issued
& CEPH_CAP_AUTH_EXCL
) == 0) {
2306 inode
->i_mode
= le32_to_cpu(grant
->mode
);
2307 inode
->i_uid
= le32_to_cpu(grant
->uid
);
2308 inode
->i_gid
= le32_to_cpu(grant
->gid
);
2309 dout("%p mode 0%o uid.gid %d.%d\n", inode
, inode
->i_mode
,
2310 inode
->i_uid
, inode
->i_gid
);
2313 if ((issued
& CEPH_CAP_LINK_EXCL
) == 0)
2314 inode
->i_nlink
= le32_to_cpu(grant
->nlink
);
2316 if ((issued
& CEPH_CAP_XATTR_EXCL
) == 0 && grant
->xattr_len
) {
2317 int len
= le32_to_cpu(grant
->xattr_len
);
2318 u64 version
= le64_to_cpu(grant
->xattr_version
);
2320 if (version
> ci
->i_xattrs
.version
) {
2321 dout(" got new xattrs v%llu on %p len %d\n",
2322 version
, inode
, len
);
2323 if (ci
->i_xattrs
.blob
)
2324 ceph_buffer_put(ci
->i_xattrs
.blob
);
2325 ci
->i_xattrs
.blob
= ceph_buffer_get(xattr_buf
);
2326 ci
->i_xattrs
.version
= version
;
2330 /* size/ctime/mtime/atime? */
2331 ceph_fill_file_size(inode
, issued
,
2332 le32_to_cpu(grant
->truncate_seq
),
2333 le64_to_cpu(grant
->truncate_size
), size
);
2334 ceph_decode_timespec(&mtime
, &grant
->mtime
);
2335 ceph_decode_timespec(&atime
, &grant
->atime
);
2336 ceph_decode_timespec(&ctime
, &grant
->ctime
);
2337 ceph_fill_file_time(inode
, issued
,
2338 le32_to_cpu(grant
->time_warp_seq
), &ctime
, &mtime
,
2341 /* max size increase? */
2342 if (max_size
!= ci
->i_max_size
) {
2343 dout("max_size %lld -> %llu\n", ci
->i_max_size
, max_size
);
2344 ci
->i_max_size
= max_size
;
2345 if (max_size
>= ci
->i_wanted_max_size
) {
2346 ci
->i_wanted_max_size
= 0; /* reset */
2347 ci
->i_requested_max_size
= 0;
2352 /* check cap bits */
2353 wanted
= __ceph_caps_wanted(ci
);
2354 used
= __ceph_caps_used(ci
);
2355 dirty
= __ceph_caps_dirty(ci
);
2356 dout(" my wanted = %s, used = %s, dirty %s\n",
2357 ceph_cap_string(wanted
),
2358 ceph_cap_string(used
),
2359 ceph_cap_string(dirty
));
2360 if (wanted
!= le32_to_cpu(grant
->wanted
)) {
2361 dout("mds wanted %s -> %s\n",
2362 ceph_cap_string(le32_to_cpu(grant
->wanted
)),
2363 ceph_cap_string(wanted
));
2364 grant
->wanted
= cpu_to_le32(wanted
);
2369 /* file layout may have changed */
2370 ci
->i_layout
= grant
->layout
;
2372 /* revocation, grant, or no-op? */
2373 if (cap
->issued
& ~newcaps
) {
2374 int revoking
= cap
->issued
& ~newcaps
;
2376 dout("revocation: %s -> %s (revoking %s)\n",
2377 ceph_cap_string(cap
->issued
),
2378 ceph_cap_string(newcaps
),
2379 ceph_cap_string(revoking
));
2380 if (revoking
& CEPH_CAP_FILE_BUFFER
)
2381 writeback
= 1; /* initiate writeback; will delay ack */
2382 else if (revoking
== CEPH_CAP_FILE_CACHE
&&
2383 (newcaps
& CEPH_CAP_FILE_LAZYIO
) == 0 &&
2385 ; /* do nothing yet, invalidation will be queued */
2386 else if (cap
== ci
->i_auth_cap
)
2387 check_caps
= 1; /* check auth cap only */
2389 check_caps
= 2; /* check all caps */
2390 cap
->issued
= newcaps
;
2391 cap
->implemented
|= newcaps
;
2392 } else if (cap
->issued
== newcaps
) {
2393 dout("caps unchanged: %s -> %s\n",
2394 ceph_cap_string(cap
->issued
), ceph_cap_string(newcaps
));
2396 dout("grant: %s -> %s\n", ceph_cap_string(cap
->issued
),
2397 ceph_cap_string(newcaps
));
2398 cap
->issued
= newcaps
;
2399 cap
->implemented
|= newcaps
; /* add bits only, to
2400 * avoid stepping on a
2401 * pending revocation */
2404 BUG_ON(cap
->issued
& ~cap
->implemented
);
2406 spin_unlock(&inode
->i_lock
);
2409 * queue inode for writeback: we can't actually call
2410 * filemap_write_and_wait, etc. from message handler
2413 ceph_queue_writeback(inode
);
2414 if (queue_invalidate
)
2415 ceph_queue_invalidate(inode
);
2417 wake_up_all(&ci
->i_cap_wq
);
2419 if (check_caps
== 1)
2420 ceph_check_caps(ci
, CHECK_CAPS_NODELAY
|CHECK_CAPS_AUTHONLY
,
2422 else if (check_caps
== 2)
2423 ceph_check_caps(ci
, CHECK_CAPS_NODELAY
, session
);
2425 mutex_unlock(&session
->s_mutex
);
2429 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2430 * MDS has been safely committed.
2432 static void handle_cap_flush_ack(struct inode
*inode
, u64 flush_tid
,
2433 struct ceph_mds_caps
*m
,
2434 struct ceph_mds_session
*session
,
2435 struct ceph_cap
*cap
)
2436 __releases(inode
->i_lock
)
2438 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2439 struct ceph_mds_client
*mdsc
= &ceph_sb_to_client(inode
->i_sb
)->mdsc
;
2440 unsigned seq
= le32_to_cpu(m
->seq
);
2441 int dirty
= le32_to_cpu(m
->dirty
);
2446 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
2447 if ((dirty
& (1 << i
)) &&
2448 flush_tid
== ci
->i_cap_flush_tid
[i
])
2451 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2452 " flushing %s -> %s\n",
2453 inode
, session
->s_mds
, seq
, ceph_cap_string(dirty
),
2454 ceph_cap_string(cleaned
), ceph_cap_string(ci
->i_flushing_caps
),
2455 ceph_cap_string(ci
->i_flushing_caps
& ~cleaned
));
2457 if (ci
->i_flushing_caps
== (ci
->i_flushing_caps
& ~cleaned
))
2460 ci
->i_flushing_caps
&= ~cleaned
;
2462 spin_lock(&mdsc
->cap_dirty_lock
);
2463 if (ci
->i_flushing_caps
== 0) {
2464 list_del_init(&ci
->i_flushing_item
);
2465 if (!list_empty(&session
->s_cap_flushing
))
2466 dout(" mds%d still flushing cap on %p\n",
2468 &list_entry(session
->s_cap_flushing
.next
,
2469 struct ceph_inode_info
,
2470 i_flushing_item
)->vfs_inode
);
2471 mdsc
->num_cap_flushing
--;
2472 wake_up_all(&mdsc
->cap_flushing_wq
);
2473 dout(" inode %p now !flushing\n", inode
);
2475 if (ci
->i_dirty_caps
== 0) {
2476 dout(" inode %p now clean\n", inode
);
2477 BUG_ON(!list_empty(&ci
->i_dirty_item
));
2480 BUG_ON(list_empty(&ci
->i_dirty_item
));
2483 spin_unlock(&mdsc
->cap_dirty_lock
);
2484 wake_up_all(&ci
->i_cap_wq
);
2487 spin_unlock(&inode
->i_lock
);
2493 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
2494 * throw away our cap_snap.
2496 * Caller hold s_mutex.
2498 static void handle_cap_flushsnap_ack(struct inode
*inode
, u64 flush_tid
,
2499 struct ceph_mds_caps
*m
,
2500 struct ceph_mds_session
*session
)
2502 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2503 u64 follows
= le64_to_cpu(m
->snap_follows
);
2504 struct ceph_cap_snap
*capsnap
;
2507 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2508 inode
, ci
, session
->s_mds
, follows
);
2510 spin_lock(&inode
->i_lock
);
2511 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
2512 if (capsnap
->follows
== follows
) {
2513 if (capsnap
->flush_tid
!= flush_tid
) {
2514 dout(" cap_snap %p follows %lld tid %lld !="
2515 " %lld\n", capsnap
, follows
,
2516 flush_tid
, capsnap
->flush_tid
);
2519 WARN_ON(capsnap
->dirty_pages
|| capsnap
->writing
);
2520 dout(" removing %p cap_snap %p follows %lld\n",
2521 inode
, capsnap
, follows
);
2522 ceph_put_snap_context(capsnap
->context
);
2523 list_del(&capsnap
->ci_item
);
2524 list_del(&capsnap
->flushing_item
);
2525 ceph_put_cap_snap(capsnap
);
2529 dout(" skipping cap_snap %p follows %lld\n",
2530 capsnap
, capsnap
->follows
);
2533 spin_unlock(&inode
->i_lock
);
2539 * Handle TRUNC from MDS, indicating file truncation.
2541 * caller hold s_mutex.
2543 static void handle_cap_trunc(struct inode
*inode
,
2544 struct ceph_mds_caps
*trunc
,
2545 struct ceph_mds_session
*session
)
2546 __releases(inode
->i_lock
)
2548 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2549 int mds
= session
->s_mds
;
2550 int seq
= le32_to_cpu(trunc
->seq
);
2551 u32 truncate_seq
= le32_to_cpu(trunc
->truncate_seq
);
2552 u64 truncate_size
= le64_to_cpu(trunc
->truncate_size
);
2553 u64 size
= le64_to_cpu(trunc
->size
);
2554 int implemented
= 0;
2555 int dirty
= __ceph_caps_dirty(ci
);
2556 int issued
= __ceph_caps_issued(ceph_inode(inode
), &implemented
);
2557 int queue_trunc
= 0;
2559 issued
|= implemented
| dirty
;
2561 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2562 inode
, mds
, seq
, truncate_size
, truncate_seq
);
2563 queue_trunc
= ceph_fill_file_size(inode
, issued
,
2564 truncate_seq
, truncate_size
, size
);
2565 spin_unlock(&inode
->i_lock
);
2568 ceph_queue_vmtruncate(inode
);
2572 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
2573 * different one. If we are the most recent migration we've seen (as
2574 * indicated by mseq), make note of the migrating cap bits for the
2575 * duration (until we see the corresponding IMPORT).
2577 * caller holds s_mutex
2579 static void handle_cap_export(struct inode
*inode
, struct ceph_mds_caps
*ex
,
2580 struct ceph_mds_session
*session
)
2582 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2583 int mds
= session
->s_mds
;
2584 unsigned mseq
= le32_to_cpu(ex
->migrate_seq
);
2585 struct ceph_cap
*cap
= NULL
, *t
;
2589 dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
2590 inode
, ci
, mds
, mseq
);
2592 spin_lock(&inode
->i_lock
);
2594 /* make sure we haven't seen a higher mseq */
2595 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
2596 t
= rb_entry(p
, struct ceph_cap
, ci_node
);
2597 if (ceph_seq_cmp(t
->mseq
, mseq
) > 0) {
2598 dout(" higher mseq on cap from mds%d\n",
2602 if (t
->session
->s_mds
== mds
)
2609 ci
->i_cap_exporting_mds
= mds
;
2610 ci
->i_cap_exporting_mseq
= mseq
;
2611 ci
->i_cap_exporting_issued
= cap
->issued
;
2613 __ceph_remove_cap(cap
);
2615 /* else, we already released it */
2617 spin_unlock(&inode
->i_lock
);
2621 * Handle cap IMPORT. If there are temp bits from an older EXPORT,
2624 * caller holds s_mutex.
2626 static void handle_cap_import(struct ceph_mds_client
*mdsc
,
2627 struct inode
*inode
, struct ceph_mds_caps
*im
,
2628 struct ceph_mds_session
*session
,
2629 void *snaptrace
, int snaptrace_len
)
2631 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2632 int mds
= session
->s_mds
;
2633 unsigned issued
= le32_to_cpu(im
->caps
);
2634 unsigned wanted
= le32_to_cpu(im
->wanted
);
2635 unsigned seq
= le32_to_cpu(im
->seq
);
2636 unsigned mseq
= le32_to_cpu(im
->migrate_seq
);
2637 u64 realmino
= le64_to_cpu(im
->realm
);
2638 u64 cap_id
= le64_to_cpu(im
->cap_id
);
2640 if (ci
->i_cap_exporting_mds
>= 0 &&
2641 ceph_seq_cmp(ci
->i_cap_exporting_mseq
, mseq
) < 0) {
2642 dout("handle_cap_import inode %p ci %p mds%d mseq %d"
2643 " - cleared exporting from mds%d\n",
2644 inode
, ci
, mds
, mseq
,
2645 ci
->i_cap_exporting_mds
);
2646 ci
->i_cap_exporting_issued
= 0;
2647 ci
->i_cap_exporting_mseq
= 0;
2648 ci
->i_cap_exporting_mds
= -1;
2650 dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
2651 inode
, ci
, mds
, mseq
);
2654 down_write(&mdsc
->snap_rwsem
);
2655 ceph_update_snap_trace(mdsc
, snaptrace
, snaptrace
+snaptrace_len
,
2657 downgrade_write(&mdsc
->snap_rwsem
);
2658 ceph_add_cap(inode
, session
, cap_id
, -1,
2659 issued
, wanted
, seq
, mseq
, realmino
, CEPH_CAP_FLAG_AUTH
,
2660 NULL
/* no caps context */);
2661 try_flush_caps(inode
, session
, NULL
);
2662 up_read(&mdsc
->snap_rwsem
);
2666 * Handle a caps message from the MDS.
2668 * Identify the appropriate session, inode, and call the right handler
2669 * based on the cap op.
2671 void ceph_handle_caps(struct ceph_mds_session
*session
,
2672 struct ceph_msg
*msg
)
2674 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2675 struct super_block
*sb
= mdsc
->client
->sb
;
2676 struct inode
*inode
;
2677 struct ceph_cap
*cap
;
2678 struct ceph_mds_caps
*h
;
2679 int mds
= session
->s_mds
;
2682 struct ceph_vino vino
;
2688 dout("handle_caps from mds%d\n", mds
);
2691 tid
= le64_to_cpu(msg
->hdr
.tid
);
2692 if (msg
->front
.iov_len
< sizeof(*h
))
2694 h
= msg
->front
.iov_base
;
2696 op
= le32_to_cpu(h
->op
);
2697 vino
.ino
= le64_to_cpu(h
->ino
);
2698 vino
.snap
= CEPH_NOSNAP
;
2699 cap_id
= le64_to_cpu(h
->cap_id
);
2700 seq
= le32_to_cpu(h
->seq
);
2701 mseq
= le32_to_cpu(h
->migrate_seq
);
2702 size
= le64_to_cpu(h
->size
);
2703 max_size
= le64_to_cpu(h
->max_size
);
2705 mutex_lock(&session
->s_mutex
);
2707 dout(" mds%d seq %lld cap seq %u\n", session
->s_mds
, session
->s_seq
,
2711 inode
= ceph_find_inode(sb
, vino
);
2712 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op
), vino
.ino
,
2715 dout(" i don't have ino %llx\n", vino
.ino
);
2717 if (op
== CEPH_CAP_OP_IMPORT
)
2718 __queue_cap_release(session
, vino
.ino
, cap_id
,
2722 * send any full release message to try to move things
2723 * along for the mds (who clearly thinks we still have this
2726 ceph_add_cap_releases(mdsc
, session
);
2727 ceph_send_cap_releases(mdsc
, session
);
2731 /* these will work even if we don't have a cap yet */
2733 case CEPH_CAP_OP_FLUSHSNAP_ACK
:
2734 handle_cap_flushsnap_ack(inode
, tid
, h
, session
);
2737 case CEPH_CAP_OP_EXPORT
:
2738 handle_cap_export(inode
, h
, session
);
2741 case CEPH_CAP_OP_IMPORT
:
2742 handle_cap_import(mdsc
, inode
, h
, session
,
2743 snaptrace
, le32_to_cpu(h
->snap_trace_len
));
2744 ceph_check_caps(ceph_inode(inode
), CHECK_CAPS_NODELAY
,
2749 /* the rest require a cap */
2750 spin_lock(&inode
->i_lock
);
2751 cap
= __get_cap_for_mds(ceph_inode(inode
), mds
);
2753 dout(" no cap on %p ino %llx.%llx from mds%d\n",
2754 inode
, ceph_ino(inode
), ceph_snap(inode
), mds
);
2755 spin_unlock(&inode
->i_lock
);
2759 /* note that each of these drops i_lock for us */
2761 case CEPH_CAP_OP_REVOKE
:
2762 case CEPH_CAP_OP_GRANT
:
2763 handle_cap_grant(inode
, h
, session
, cap
, msg
->middle
);
2766 case CEPH_CAP_OP_FLUSH_ACK
:
2767 handle_cap_flush_ack(inode
, tid
, h
, session
, cap
);
2770 case CEPH_CAP_OP_TRUNC
:
2771 handle_cap_trunc(inode
, h
, session
);
2775 spin_unlock(&inode
->i_lock
);
2776 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op
,
2777 ceph_cap_op_name(op
));
2781 mutex_unlock(&session
->s_mutex
);
2788 pr_err("ceph_handle_caps: corrupt message\n");
2794 * Delayed work handler to process end of delayed cap release LRU list.
2796 void ceph_check_delayed_caps(struct ceph_mds_client
*mdsc
)
2798 struct ceph_inode_info
*ci
;
2799 int flags
= CHECK_CAPS_NODELAY
;
2801 dout("check_delayed_caps\n");
2803 spin_lock(&mdsc
->cap_delay_lock
);
2804 if (list_empty(&mdsc
->cap_delay_list
))
2806 ci
= list_first_entry(&mdsc
->cap_delay_list
,
2807 struct ceph_inode_info
,
2809 if ((ci
->i_ceph_flags
& CEPH_I_FLUSH
) == 0 &&
2810 time_before(jiffies
, ci
->i_hold_caps_max
))
2812 list_del_init(&ci
->i_cap_delay_list
);
2813 spin_unlock(&mdsc
->cap_delay_lock
);
2814 dout("check_delayed_caps on %p\n", &ci
->vfs_inode
);
2815 ceph_check_caps(ci
, flags
, NULL
);
2817 spin_unlock(&mdsc
->cap_delay_lock
);
2821 * Flush all dirty caps to the mds
2823 void ceph_flush_dirty_caps(struct ceph_mds_client
*mdsc
)
2825 struct ceph_inode_info
*ci
, *nci
= NULL
;
2826 struct inode
*inode
, *ninode
= NULL
;
2827 struct list_head
*p
, *n
;
2829 dout("flush_dirty_caps\n");
2830 spin_lock(&mdsc
->cap_dirty_lock
);
2831 list_for_each_safe(p
, n
, &mdsc
->cap_dirty
) {
2835 ci
->i_ceph_flags
&= ~CEPH_I_NOFLUSH
;
2836 dout("flush_dirty_caps inode %p (was next inode)\n",
2839 ci
= list_entry(p
, struct ceph_inode_info
,
2841 inode
= igrab(&ci
->vfs_inode
);
2843 dout("flush_dirty_caps inode %p\n", inode
);
2845 if (n
!= &mdsc
->cap_dirty
) {
2846 nci
= list_entry(n
, struct ceph_inode_info
,
2848 ninode
= igrab(&nci
->vfs_inode
);
2850 nci
->i_ceph_flags
|= CEPH_I_NOFLUSH
;
2851 dout("flush_dirty_caps next inode %p, noflush\n",
2857 spin_unlock(&mdsc
->cap_dirty_lock
);
2859 ceph_check_caps(ci
, CHECK_CAPS_NODELAY
|CHECK_CAPS_FLUSH
,
2863 spin_lock(&mdsc
->cap_dirty_lock
);
2865 spin_unlock(&mdsc
->cap_dirty_lock
);
2869 * Drop open file reference. If we were the last open file,
2870 * we may need to release capabilities to the MDS (or schedule
2871 * their delayed release).
2873 void ceph_put_fmode(struct ceph_inode_info
*ci
, int fmode
)
2875 struct inode
*inode
= &ci
->vfs_inode
;
2878 spin_lock(&inode
->i_lock
);
2879 dout("put_fmode %p fmode %d %d -> %d\n", inode
, fmode
,
2880 ci
->i_nr_by_mode
[fmode
], ci
->i_nr_by_mode
[fmode
]-1);
2881 BUG_ON(ci
->i_nr_by_mode
[fmode
] == 0);
2882 if (--ci
->i_nr_by_mode
[fmode
] == 0)
2884 spin_unlock(&inode
->i_lock
);
2886 if (last
&& ci
->i_vino
.snap
== CEPH_NOSNAP
)
2887 ceph_check_caps(ci
, 0, NULL
);
2891 * Helpers for embedding cap and dentry lease releases into mds
2894 * @force is used by dentry_release (below) to force inclusion of a
2895 * record for the directory inode, even when there aren't any caps to
2898 int ceph_encode_inode_release(void **p
, struct inode
*inode
,
2899 int mds
, int drop
, int unless
, int force
)
2901 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2902 struct ceph_cap
*cap
;
2903 struct ceph_mds_request_release
*rel
= *p
;
2907 spin_lock(&inode
->i_lock
);
2908 used
= __ceph_caps_used(ci
);
2909 dirty
= __ceph_caps_dirty(ci
);
2911 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
2912 inode
, mds
, ceph_cap_string(used
|dirty
), ceph_cap_string(drop
),
2913 ceph_cap_string(unless
));
2915 /* only drop unused, clean caps */
2916 drop
&= ~(used
| dirty
);
2918 cap
= __get_cap_for_mds(ci
, mds
);
2919 if (cap
&& __cap_is_valid(cap
)) {
2921 ((cap
->issued
& drop
) &&
2922 (cap
->issued
& unless
) == 0)) {
2923 if ((cap
->issued
& drop
) &&
2924 (cap
->issued
& unless
) == 0) {
2925 dout("encode_inode_release %p cap %p %s -> "
2927 ceph_cap_string(cap
->issued
),
2928 ceph_cap_string(cap
->issued
& ~drop
));
2929 cap
->issued
&= ~drop
;
2930 cap
->implemented
&= ~drop
;
2931 if (ci
->i_ceph_flags
& CEPH_I_NODELAY
) {
2932 int wanted
= __ceph_caps_wanted(ci
);
2933 dout(" wanted %s -> %s (act %s)\n",
2934 ceph_cap_string(cap
->mds_wanted
),
2935 ceph_cap_string(cap
->mds_wanted
&
2937 ceph_cap_string(wanted
));
2938 cap
->mds_wanted
&= wanted
;
2941 dout("encode_inode_release %p cap %p %s"
2942 " (force)\n", inode
, cap
,
2943 ceph_cap_string(cap
->issued
));
2946 rel
->ino
= cpu_to_le64(ceph_ino(inode
));
2947 rel
->cap_id
= cpu_to_le64(cap
->cap_id
);
2948 rel
->seq
= cpu_to_le32(cap
->seq
);
2949 rel
->issue_seq
= cpu_to_le32(cap
->issue_seq
),
2950 rel
->mseq
= cpu_to_le32(cap
->mseq
);
2951 rel
->caps
= cpu_to_le32(cap
->issued
);
2952 rel
->wanted
= cpu_to_le32(cap
->mds_wanted
);
2958 dout("encode_inode_release %p cap %p %s\n",
2959 inode
, cap
, ceph_cap_string(cap
->issued
));
2962 spin_unlock(&inode
->i_lock
);
2966 int ceph_encode_dentry_release(void **p
, struct dentry
*dentry
,
2967 int mds
, int drop
, int unless
)
2969 struct inode
*dir
= dentry
->d_parent
->d_inode
;
2970 struct ceph_mds_request_release
*rel
= *p
;
2971 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
2976 * force an record for the directory caps if we have a dentry lease.
2977 * this is racy (can't take i_lock and d_lock together), but it
2978 * doesn't have to be perfect; the mds will revoke anything we don't
2981 spin_lock(&dentry
->d_lock
);
2982 if (di
->lease_session
&& di
->lease_session
->s_mds
== mds
)
2984 spin_unlock(&dentry
->d_lock
);
2986 ret
= ceph_encode_inode_release(p
, dir
, mds
, drop
, unless
, force
);
2988 spin_lock(&dentry
->d_lock
);
2989 if (ret
&& di
->lease_session
&& di
->lease_session
->s_mds
== mds
) {
2990 dout("encode_dentry_release %p mds%d seq %d\n",
2991 dentry
, mds
, (int)di
->lease_seq
);
2992 rel
->dname_len
= cpu_to_le32(dentry
->d_name
.len
);
2993 memcpy(*p
, dentry
->d_name
.name
, dentry
->d_name
.len
);
2994 *p
+= dentry
->d_name
.len
;
2995 rel
->dname_seq
= cpu_to_le32(di
->lease_seq
);
2996 __ceph_mdsc_drop_dentry_lease(dentry
);
2998 spin_unlock(&dentry
->d_lock
);