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 return kmem_cache_alloc(ceph_cap_cachep
, GFP_NOFS
);
250 spin_lock(&caps_list_lock
);
251 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
252 ctx
, ctx
->count
, caps_total_count
, caps_use_count
,
253 caps_reserve_count
, caps_avail_count
);
255 BUG_ON(ctx
->count
> caps_reserve_count
);
256 BUG_ON(list_empty(&caps_list
));
259 caps_reserve_count
--;
262 cap
= list_first_entry(&caps_list
, struct ceph_cap
, caps_item
);
263 list_del(&cap
->caps_item
);
265 BUG_ON(caps_total_count
!= caps_use_count
+ caps_reserve_count
+
267 spin_unlock(&caps_list_lock
);
271 void ceph_put_cap(struct ceph_cap
*cap
)
273 spin_lock(&caps_list_lock
);
274 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
275 cap
, caps_total_count
, caps_use_count
,
276 caps_reserve_count
, caps_avail_count
);
279 * Keep some preallocated caps around (ceph_min_count), to
280 * avoid lots of free/alloc churn.
282 if (caps_avail_count
>= caps_reserve_count
+ caps_min_count
) {
284 kmem_cache_free(ceph_cap_cachep
, cap
);
287 list_add(&cap
->caps_item
, &caps_list
);
290 BUG_ON(caps_total_count
!= caps_use_count
+ caps_reserve_count
+
292 spin_unlock(&caps_list_lock
);
295 void ceph_reservation_status(struct ceph_client
*client
,
296 int *total
, int *avail
, int *used
, int *reserved
,
300 *total
= caps_total_count
;
302 *avail
= caps_avail_count
;
304 *used
= caps_use_count
;
306 *reserved
= caps_reserve_count
;
308 *min
= caps_min_count
;
312 * Find ceph_cap for given mds, if any.
314 * Called with i_lock held.
316 static struct ceph_cap
*__get_cap_for_mds(struct ceph_inode_info
*ci
, int mds
)
318 struct ceph_cap
*cap
;
319 struct rb_node
*n
= ci
->i_caps
.rb_node
;
322 cap
= rb_entry(n
, struct ceph_cap
, ci_node
);
325 else if (mds
> cap
->mds
)
334 * Return id of any MDS with a cap, preferably FILE_WR|WRBUFFER|EXCL, else
337 static int __ceph_get_cap_mds(struct ceph_inode_info
*ci
, u32
*mseq
)
339 struct ceph_cap
*cap
;
343 /* prefer mds with WR|WRBUFFER|EXCL caps */
344 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
345 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
349 if (cap
->issued
& (CEPH_CAP_FILE_WR
|
350 CEPH_CAP_FILE_BUFFER
|
357 int ceph_get_cap_mds(struct inode
*inode
)
360 spin_lock(&inode
->i_lock
);
361 mds
= __ceph_get_cap_mds(ceph_inode(inode
), NULL
);
362 spin_unlock(&inode
->i_lock
);
367 * Called under i_lock.
369 static void __insert_cap_node(struct ceph_inode_info
*ci
,
370 struct ceph_cap
*new)
372 struct rb_node
**p
= &ci
->i_caps
.rb_node
;
373 struct rb_node
*parent
= NULL
;
374 struct ceph_cap
*cap
= NULL
;
378 cap
= rb_entry(parent
, struct ceph_cap
, ci_node
);
379 if (new->mds
< cap
->mds
)
381 else if (new->mds
> cap
->mds
)
387 rb_link_node(&new->ci_node
, parent
, p
);
388 rb_insert_color(&new->ci_node
, &ci
->i_caps
);
392 * (re)set cap hold timeouts, which control the delayed release
393 * of unused caps back to the MDS. Should be called on cap use.
395 static void __cap_set_timeouts(struct ceph_mds_client
*mdsc
,
396 struct ceph_inode_info
*ci
)
398 struct ceph_mount_args
*ma
= mdsc
->client
->mount_args
;
400 ci
->i_hold_caps_min
= round_jiffies(jiffies
+
401 ma
->caps_wanted_delay_min
* HZ
);
402 ci
->i_hold_caps_max
= round_jiffies(jiffies
+
403 ma
->caps_wanted_delay_max
* HZ
);
404 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci
->vfs_inode
,
405 ci
->i_hold_caps_min
- jiffies
, ci
->i_hold_caps_max
- jiffies
);
409 * (Re)queue cap at the end of the delayed cap release list.
411 * If I_FLUSH is set, leave the inode at the front of the list.
413 * Caller holds i_lock
414 * -> we take mdsc->cap_delay_lock
416 static void __cap_delay_requeue(struct ceph_mds_client
*mdsc
,
417 struct ceph_inode_info
*ci
)
419 __cap_set_timeouts(mdsc
, ci
);
420 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci
->vfs_inode
,
421 ci
->i_ceph_flags
, ci
->i_hold_caps_max
);
422 if (!mdsc
->stopping
) {
423 spin_lock(&mdsc
->cap_delay_lock
);
424 if (!list_empty(&ci
->i_cap_delay_list
)) {
425 if (ci
->i_ceph_flags
& CEPH_I_FLUSH
)
427 list_del_init(&ci
->i_cap_delay_list
);
429 list_add_tail(&ci
->i_cap_delay_list
, &mdsc
->cap_delay_list
);
431 spin_unlock(&mdsc
->cap_delay_lock
);
436 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
437 * indicating we should send a cap message to flush dirty metadata
438 * asap, and move to the front of the delayed cap list.
440 static void __cap_delay_requeue_front(struct ceph_mds_client
*mdsc
,
441 struct ceph_inode_info
*ci
)
443 dout("__cap_delay_requeue_front %p\n", &ci
->vfs_inode
);
444 spin_lock(&mdsc
->cap_delay_lock
);
445 ci
->i_ceph_flags
|= CEPH_I_FLUSH
;
446 if (!list_empty(&ci
->i_cap_delay_list
))
447 list_del_init(&ci
->i_cap_delay_list
);
448 list_add(&ci
->i_cap_delay_list
, &mdsc
->cap_delay_list
);
449 spin_unlock(&mdsc
->cap_delay_lock
);
453 * Cancel delayed work on cap.
455 * Caller must hold i_lock.
457 static void __cap_delay_cancel(struct ceph_mds_client
*mdsc
,
458 struct ceph_inode_info
*ci
)
460 dout("__cap_delay_cancel %p\n", &ci
->vfs_inode
);
461 if (list_empty(&ci
->i_cap_delay_list
))
463 spin_lock(&mdsc
->cap_delay_lock
);
464 list_del_init(&ci
->i_cap_delay_list
);
465 spin_unlock(&mdsc
->cap_delay_lock
);
469 * Common issue checks for add_cap, handle_cap_grant.
471 static void __check_cap_issue(struct ceph_inode_info
*ci
, struct ceph_cap
*cap
,
474 unsigned had
= __ceph_caps_issued(ci
, NULL
);
477 * Each time we receive FILE_CACHE anew, we increment
480 if ((issued
& CEPH_CAP_FILE_CACHE
) &&
481 (had
& CEPH_CAP_FILE_CACHE
) == 0)
485 * if we are newly issued FILE_SHARED, clear I_COMPLETE; we
486 * don't know what happened to this directory while we didn't
489 if ((issued
& CEPH_CAP_FILE_SHARED
) &&
490 (had
& CEPH_CAP_FILE_SHARED
) == 0) {
492 if (S_ISDIR(ci
->vfs_inode
.i_mode
)) {
493 dout(" marking %p NOT complete\n", &ci
->vfs_inode
);
494 ci
->i_ceph_flags
&= ~CEPH_I_COMPLETE
;
500 * Add a capability under the given MDS session.
502 * Caller should hold session snap_rwsem (read) and s_mutex.
504 * @fmode is the open file mode, if we are opening a file, otherwise
505 * it is < 0. (This is so we can atomically add the cap and add an
506 * open file reference to it.)
508 int ceph_add_cap(struct inode
*inode
,
509 struct ceph_mds_session
*session
, u64 cap_id
,
510 int fmode
, unsigned issued
, unsigned wanted
,
511 unsigned seq
, unsigned mseq
, u64 realmino
, int flags
,
512 struct ceph_cap_reservation
*caps_reservation
)
514 struct ceph_mds_client
*mdsc
= &ceph_inode_to_client(inode
)->mdsc
;
515 struct ceph_inode_info
*ci
= ceph_inode(inode
);
516 struct ceph_cap
*new_cap
= NULL
;
517 struct ceph_cap
*cap
;
518 int mds
= session
->s_mds
;
521 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode
,
522 session
->s_mds
, cap_id
, ceph_cap_string(issued
), seq
);
525 * If we are opening the file, include file mode wanted bits
529 wanted
|= ceph_caps_for_mode(fmode
);
532 spin_lock(&inode
->i_lock
);
533 cap
= __get_cap_for_mds(ci
, mds
);
539 spin_unlock(&inode
->i_lock
);
540 new_cap
= get_cap(caps_reservation
);
547 cap
->implemented
= 0;
552 __insert_cap_node(ci
, cap
);
554 /* clear out old exporting info? (i.e. on cap import) */
555 if (ci
->i_cap_exporting_mds
== mds
) {
556 ci
->i_cap_exporting_issued
= 0;
557 ci
->i_cap_exporting_mseq
= 0;
558 ci
->i_cap_exporting_mds
= -1;
561 /* add to session cap list */
562 cap
->session
= session
;
563 spin_lock(&session
->s_cap_lock
);
564 list_add_tail(&cap
->session_caps
, &session
->s_caps
);
565 session
->s_nr_caps
++;
566 spin_unlock(&session
->s_cap_lock
);
569 if (!ci
->i_snap_realm
) {
571 * add this inode to the appropriate snap realm
573 struct ceph_snap_realm
*realm
= ceph_lookup_snap_realm(mdsc
,
576 ceph_get_snap_realm(mdsc
, realm
);
577 spin_lock(&realm
->inodes_with_caps_lock
);
578 ci
->i_snap_realm
= realm
;
579 list_add(&ci
->i_snap_realm_item
,
580 &realm
->inodes_with_caps
);
581 spin_unlock(&realm
->inodes_with_caps_lock
);
583 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
588 __check_cap_issue(ci
, cap
, issued
);
591 * If we are issued caps we don't want, or the mds' wanted
592 * value appears to be off, queue a check so we'll release
593 * later and/or update the mds wanted value.
595 actual_wanted
= __ceph_caps_wanted(ci
);
596 if ((wanted
& ~actual_wanted
) ||
597 (issued
& ~actual_wanted
& CEPH_CAP_ANY_WR
)) {
598 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
599 ceph_cap_string(issued
), ceph_cap_string(wanted
),
600 ceph_cap_string(actual_wanted
));
601 __cap_delay_requeue(mdsc
, ci
);
604 if (flags
& CEPH_CAP_FLAG_AUTH
)
605 ci
->i_auth_cap
= cap
;
606 else if (ci
->i_auth_cap
== cap
)
607 ci
->i_auth_cap
= NULL
;
609 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
610 inode
, ceph_vinop(inode
), cap
, ceph_cap_string(issued
),
611 ceph_cap_string(issued
|cap
->issued
), seq
, mds
);
612 cap
->cap_id
= cap_id
;
613 cap
->issued
= issued
;
614 cap
->implemented
|= issued
;
615 cap
->mds_wanted
|= wanted
;
617 cap
->issue_seq
= seq
;
619 cap
->cap_gen
= session
->s_cap_gen
;
622 __ceph_get_fmode(ci
, fmode
);
623 spin_unlock(&inode
->i_lock
);
624 wake_up(&ci
->i_cap_wq
);
629 * Return true if cap has not timed out and belongs to the current
630 * generation of the MDS session (i.e. has not gone 'stale' due to
631 * us losing touch with the mds).
633 static int __cap_is_valid(struct ceph_cap
*cap
)
638 spin_lock(&cap
->session
->s_cap_lock
);
639 gen
= cap
->session
->s_cap_gen
;
640 ttl
= cap
->session
->s_cap_ttl
;
641 spin_unlock(&cap
->session
->s_cap_lock
);
643 if (cap
->cap_gen
< gen
|| time_after_eq(jiffies
, ttl
)) {
644 dout("__cap_is_valid %p cap %p issued %s "
645 "but STALE (gen %u vs %u)\n", &cap
->ci
->vfs_inode
,
646 cap
, ceph_cap_string(cap
->issued
), cap
->cap_gen
, gen
);
654 * Return set of valid cap bits issued to us. Note that caps time
655 * out, and may be invalidated in bulk if the client session times out
656 * and session->s_cap_gen is bumped.
658 int __ceph_caps_issued(struct ceph_inode_info
*ci
, int *implemented
)
660 int have
= ci
->i_snap_caps
| ci
->i_cap_exporting_issued
;
661 struct ceph_cap
*cap
;
666 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
667 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
668 if (!__cap_is_valid(cap
))
670 dout("__ceph_caps_issued %p cap %p issued %s\n",
671 &ci
->vfs_inode
, cap
, ceph_cap_string(cap
->issued
));
674 *implemented
|= cap
->implemented
;
680 * Get cap bits issued by caps other than @ocap
682 int __ceph_caps_issued_other(struct ceph_inode_info
*ci
, struct ceph_cap
*ocap
)
684 int have
= ci
->i_snap_caps
;
685 struct ceph_cap
*cap
;
688 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
689 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
692 if (!__cap_is_valid(cap
))
700 * Move a cap to the end of the LRU (oldest caps at list head, newest
703 static void __touch_cap(struct ceph_cap
*cap
)
705 struct ceph_mds_session
*s
= cap
->session
;
707 spin_lock(&s
->s_cap_lock
);
708 if (s
->s_cap_iterator
== NULL
) {
709 dout("__touch_cap %p cap %p mds%d\n", &cap
->ci
->vfs_inode
, cap
,
711 list_move_tail(&cap
->session_caps
, &s
->s_caps
);
713 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
714 &cap
->ci
->vfs_inode
, cap
, s
->s_mds
);
716 spin_unlock(&s
->s_cap_lock
);
720 * Check if we hold the given mask. If so, move the cap(s) to the
721 * front of their respective LRUs. (This is the preferred way for
722 * callers to check for caps they want.)
724 int __ceph_caps_issued_mask(struct ceph_inode_info
*ci
, int mask
, int touch
)
726 struct ceph_cap
*cap
;
728 int have
= ci
->i_snap_caps
;
730 if ((have
& mask
) == mask
) {
731 dout("__ceph_caps_issued_mask %p snap issued %s"
732 " (mask %s)\n", &ci
->vfs_inode
,
733 ceph_cap_string(have
),
734 ceph_cap_string(mask
));
738 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
739 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
740 if (!__cap_is_valid(cap
))
742 if ((cap
->issued
& mask
) == mask
) {
743 dout("__ceph_caps_issued_mask %p cap %p issued %s"
744 " (mask %s)\n", &ci
->vfs_inode
, cap
,
745 ceph_cap_string(cap
->issued
),
746 ceph_cap_string(mask
));
752 /* does a combination of caps satisfy mask? */
754 if ((have
& mask
) == mask
) {
755 dout("__ceph_caps_issued_mask %p combo issued %s"
756 " (mask %s)\n", &ci
->vfs_inode
,
757 ceph_cap_string(cap
->issued
),
758 ceph_cap_string(mask
));
762 /* touch this + preceeding caps */
764 for (q
= rb_first(&ci
->i_caps
); q
!= p
;
766 cap
= rb_entry(q
, struct ceph_cap
,
768 if (!__cap_is_valid(cap
))
781 * Return true if mask caps are currently being revoked by an MDS.
783 int ceph_caps_revoking(struct ceph_inode_info
*ci
, int mask
)
785 struct inode
*inode
= &ci
->vfs_inode
;
786 struct ceph_cap
*cap
;
790 spin_lock(&inode
->i_lock
);
791 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
792 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
793 if (__cap_is_valid(cap
) &&
794 (cap
->implemented
& ~cap
->issued
& mask
)) {
799 spin_unlock(&inode
->i_lock
);
800 dout("ceph_caps_revoking %p %s = %d\n", inode
,
801 ceph_cap_string(mask
), ret
);
805 int __ceph_caps_used(struct ceph_inode_info
*ci
)
809 used
|= CEPH_CAP_PIN
;
811 used
|= CEPH_CAP_FILE_RD
;
812 if (ci
->i_rdcache_ref
|| ci
->i_rdcache_gen
)
813 used
|= CEPH_CAP_FILE_CACHE
;
815 used
|= CEPH_CAP_FILE_WR
;
816 if (ci
->i_wrbuffer_ref
)
817 used
|= CEPH_CAP_FILE_BUFFER
;
822 * wanted, by virtue of open file modes
824 int __ceph_caps_file_wanted(struct ceph_inode_info
*ci
)
828 for (mode
= 0; mode
< 4; mode
++)
829 if (ci
->i_nr_by_mode
[mode
])
830 want
|= ceph_caps_for_mode(mode
);
835 * Return caps we have registered with the MDS(s) as 'wanted'.
837 int __ceph_caps_mds_wanted(struct ceph_inode_info
*ci
)
839 struct ceph_cap
*cap
;
843 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
844 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
845 if (!__cap_is_valid(cap
))
847 mds_wanted
|= cap
->mds_wanted
;
853 * called under i_lock
855 static int __ceph_is_any_caps(struct ceph_inode_info
*ci
)
857 return !RB_EMPTY_ROOT(&ci
->i_caps
) || ci
->i_cap_exporting_mds
>= 0;
861 * caller should hold i_lock.
862 * caller will not hold session s_mutex if called from destroy_inode.
864 void __ceph_remove_cap(struct ceph_cap
*cap
)
866 struct ceph_mds_session
*session
= cap
->session
;
867 struct ceph_inode_info
*ci
= cap
->ci
;
868 struct ceph_mds_client
*mdsc
= &ceph_client(ci
->vfs_inode
.i_sb
)->mdsc
;
870 dout("__ceph_remove_cap %p from %p\n", cap
, &ci
->vfs_inode
);
872 /* remove from inode list */
873 rb_erase(&cap
->ci_node
, &ci
->i_caps
);
875 if (ci
->i_auth_cap
== cap
)
876 ci
->i_auth_cap
= NULL
;
878 /* remove from session list */
879 spin_lock(&session
->s_cap_lock
);
880 if (session
->s_cap_iterator
== cap
) {
881 /* not yet, we are iterating over this very cap */
882 dout("__ceph_remove_cap delaying %p removal from session %p\n",
885 list_del_init(&cap
->session_caps
);
886 session
->s_nr_caps
--;
889 spin_unlock(&session
->s_cap_lock
);
891 if (cap
->session
== NULL
)
894 if (!__ceph_is_any_caps(ci
) && ci
->i_snap_realm
) {
895 struct ceph_snap_realm
*realm
= ci
->i_snap_realm
;
896 spin_lock(&realm
->inodes_with_caps_lock
);
897 list_del_init(&ci
->i_snap_realm_item
);
898 ci
->i_snap_realm_counter
++;
899 ci
->i_snap_realm
= NULL
;
900 spin_unlock(&realm
->inodes_with_caps_lock
);
901 ceph_put_snap_realm(mdsc
, realm
);
903 if (!__ceph_is_any_real_caps(ci
))
904 __cap_delay_cancel(mdsc
, ci
);
908 * Build and send a cap message to the given MDS.
910 * Caller should be holding s_mutex.
912 static int send_cap_msg(struct ceph_mds_session
*session
,
913 u64 ino
, u64 cid
, int op
,
914 int caps
, int wanted
, int dirty
,
915 u32 seq
, u64 flush_tid
, u32 issue_seq
, u32 mseq
,
916 u64 size
, u64 max_size
,
917 struct timespec
*mtime
, struct timespec
*atime
,
919 uid_t uid
, gid_t gid
, mode_t mode
,
921 struct ceph_buffer
*xattrs_buf
,
924 struct ceph_mds_caps
*fc
;
925 struct ceph_msg
*msg
;
927 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
928 " seq %u/%u mseq %u follows %lld size %llu/%llu"
929 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op
),
930 cid
, ino
, ceph_cap_string(caps
), ceph_cap_string(wanted
),
931 ceph_cap_string(dirty
),
932 seq
, issue_seq
, mseq
, follows
, size
, max_size
,
933 xattr_version
, xattrs_buf
? (int)xattrs_buf
->vec
.iov_len
: 0);
935 msg
= ceph_msg_new(CEPH_MSG_CLIENT_CAPS
, sizeof(*fc
), 0, 0, NULL
);
939 msg
->hdr
.tid
= cpu_to_le64(flush_tid
);
941 fc
= msg
->front
.iov_base
;
942 memset(fc
, 0, sizeof(*fc
));
944 fc
->cap_id
= cpu_to_le64(cid
);
945 fc
->op
= cpu_to_le32(op
);
946 fc
->seq
= cpu_to_le32(seq
);
947 fc
->issue_seq
= cpu_to_le32(issue_seq
);
948 fc
->migrate_seq
= cpu_to_le32(mseq
);
949 fc
->caps
= cpu_to_le32(caps
);
950 fc
->wanted
= cpu_to_le32(wanted
);
951 fc
->dirty
= cpu_to_le32(dirty
);
952 fc
->ino
= cpu_to_le64(ino
);
953 fc
->snap_follows
= cpu_to_le64(follows
);
955 fc
->size
= cpu_to_le64(size
);
956 fc
->max_size
= cpu_to_le64(max_size
);
958 ceph_encode_timespec(&fc
->mtime
, mtime
);
960 ceph_encode_timespec(&fc
->atime
, atime
);
961 fc
->time_warp_seq
= cpu_to_le32(time_warp_seq
);
963 fc
->uid
= cpu_to_le32(uid
);
964 fc
->gid
= cpu_to_le32(gid
);
965 fc
->mode
= cpu_to_le32(mode
);
967 fc
->xattr_version
= cpu_to_le64(xattr_version
);
969 msg
->middle
= ceph_buffer_get(xattrs_buf
);
970 fc
->xattr_len
= cpu_to_le32(xattrs_buf
->vec
.iov_len
);
971 msg
->hdr
.middle_len
= cpu_to_le32(xattrs_buf
->vec
.iov_len
);
974 ceph_con_send(&session
->s_con
, msg
);
979 * Queue cap releases when an inode is dropped from our cache. Since
980 * inode is about to be destroyed, there is no need for i_lock.
982 void ceph_queue_caps_release(struct inode
*inode
)
984 struct ceph_inode_info
*ci
= ceph_inode(inode
);
987 p
= rb_first(&ci
->i_caps
);
989 struct ceph_cap
*cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
990 struct ceph_mds_session
*session
= cap
->session
;
991 struct ceph_msg
*msg
;
992 struct ceph_mds_cap_release
*head
;
993 struct ceph_mds_cap_item
*item
;
995 spin_lock(&session
->s_cap_lock
);
996 BUG_ON(!session
->s_num_cap_releases
);
997 msg
= list_first_entry(&session
->s_cap_releases
,
998 struct ceph_msg
, list_head
);
1000 dout(" adding %p release to mds%d msg %p (%d left)\n",
1001 inode
, session
->s_mds
, msg
, session
->s_num_cap_releases
);
1003 BUG_ON(msg
->front
.iov_len
+ sizeof(*item
) > PAGE_CACHE_SIZE
);
1004 head
= msg
->front
.iov_base
;
1005 head
->num
= cpu_to_le32(le32_to_cpu(head
->num
) + 1);
1006 item
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1007 item
->ino
= cpu_to_le64(ceph_ino(inode
));
1008 item
->cap_id
= cpu_to_le64(cap
->cap_id
);
1009 item
->migrate_seq
= cpu_to_le32(cap
->mseq
);
1010 item
->seq
= cpu_to_le32(cap
->issue_seq
);
1012 session
->s_num_cap_releases
--;
1014 msg
->front
.iov_len
+= sizeof(*item
);
1015 if (le32_to_cpu(head
->num
) == CEPH_CAPS_PER_RELEASE
) {
1016 dout(" release msg %p full\n", msg
);
1017 list_move_tail(&msg
->list_head
,
1018 &session
->s_cap_releases_done
);
1020 dout(" release msg %p at %d/%d (%d)\n", msg
,
1021 (int)le32_to_cpu(head
->num
),
1022 (int)CEPH_CAPS_PER_RELEASE
,
1023 (int)msg
->front
.iov_len
);
1025 spin_unlock(&session
->s_cap_lock
);
1027 __ceph_remove_cap(cap
);
1032 * Send a cap msg on the given inode. Update our caps state, then
1033 * drop i_lock and send the message.
1035 * Make note of max_size reported/requested from mds, revoked caps
1036 * that have now been implemented.
1038 * Make half-hearted attempt ot to invalidate page cache if we are
1039 * dropping RDCACHE. Note that this will leave behind locked pages
1040 * that we'll then need to deal with elsewhere.
1042 * Return non-zero if delayed release, or we experienced an error
1043 * such that the caller should requeue + retry later.
1045 * called with i_lock, then drops it.
1046 * caller should hold snap_rwsem (read), s_mutex.
1048 static int __send_cap(struct ceph_mds_client
*mdsc
, struct ceph_cap
*cap
,
1049 int op
, int used
, int want
, int retain
, int flushing
,
1050 unsigned *pflush_tid
)
1051 __releases(cap
->ci
->vfs_inode
->i_lock
)
1053 struct ceph_inode_info
*ci
= cap
->ci
;
1054 struct inode
*inode
= &ci
->vfs_inode
;
1055 u64 cap_id
= cap
->cap_id
;
1056 int held
, revoking
, dropping
, keep
;
1057 u64 seq
, issue_seq
, mseq
, time_warp_seq
, follows
;
1059 struct timespec mtime
, atime
;
1064 struct ceph_mds_session
*session
;
1065 u64 xattr_version
= 0;
1071 held
= cap
->issued
| cap
->implemented
;
1072 revoking
= cap
->implemented
& ~cap
->issued
;
1073 retain
&= ~revoking
;
1074 dropping
= cap
->issued
& ~retain
;
1076 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1077 inode
, cap
, cap
->session
,
1078 ceph_cap_string(held
), ceph_cap_string(held
& retain
),
1079 ceph_cap_string(revoking
));
1080 BUG_ON((retain
& CEPH_CAP_PIN
) == 0);
1082 session
= cap
->session
;
1084 /* don't release wanted unless we've waited a bit. */
1085 if ((ci
->i_ceph_flags
& CEPH_I_NODELAY
) == 0 &&
1086 time_before(jiffies
, ci
->i_hold_caps_min
)) {
1087 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1088 ceph_cap_string(cap
->issued
),
1089 ceph_cap_string(cap
->issued
& retain
),
1090 ceph_cap_string(cap
->mds_wanted
),
1091 ceph_cap_string(want
));
1092 want
|= cap
->mds_wanted
;
1093 retain
|= cap
->issued
;
1096 ci
->i_ceph_flags
&= ~(CEPH_I_NODELAY
| CEPH_I_FLUSH
);
1098 cap
->issued
&= retain
; /* drop bits we don't want */
1099 if (cap
->implemented
& ~cap
->issued
) {
1101 * Wake up any waiters on wanted -> needed transition.
1102 * This is due to the weird transition from buffered
1103 * to sync IO... we need to flush dirty pages _before_
1104 * allowing sync writes to avoid reordering.
1108 cap
->implemented
&= cap
->issued
| used
;
1109 cap
->mds_wanted
= want
;
1113 * assign a tid for flush operations so we can avoid
1114 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1115 * clean type races. track latest tid for every bit
1116 * so we can handle flush AxFw, flush Fw, and have the
1117 * first ack clean Ax.
1119 flush_tid
= ++ci
->i_cap_flush_last_tid
;
1121 *pflush_tid
= flush_tid
;
1122 dout(" cap_flush_tid %d\n", (int)flush_tid
);
1123 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
1124 if (flushing
& (1 << i
))
1125 ci
->i_cap_flush_tid
[i
] = flush_tid
;
1128 keep
= cap
->implemented
;
1130 issue_seq
= cap
->issue_seq
;
1132 size
= inode
->i_size
;
1133 ci
->i_reported_size
= size
;
1134 max_size
= ci
->i_wanted_max_size
;
1135 ci
->i_requested_max_size
= max_size
;
1136 mtime
= inode
->i_mtime
;
1137 atime
= inode
->i_atime
;
1138 time_warp_seq
= ci
->i_time_warp_seq
;
1139 follows
= ci
->i_snap_realm
->cached_context
->seq
;
1142 mode
= inode
->i_mode
;
1144 if (dropping
& CEPH_CAP_XATTR_EXCL
) {
1145 __ceph_build_xattrs_blob(ci
);
1146 xattr_version
= ci
->i_xattrs
.version
+ 1;
1149 spin_unlock(&inode
->i_lock
);
1151 ret
= send_cap_msg(session
, ceph_vino(inode
).ino
, cap_id
,
1152 op
, keep
, want
, flushing
, seq
, flush_tid
, issue_seq
, mseq
,
1153 size
, max_size
, &mtime
, &atime
, time_warp_seq
,
1156 (flushing
& CEPH_CAP_XATTR_EXCL
) ? ci
->i_xattrs
.blob
: NULL
,
1159 dout("error sending cap msg, must requeue %p\n", inode
);
1164 wake_up(&ci
->i_cap_wq
);
1170 * When a snapshot is taken, clients accumulate dirty metadata on
1171 * inodes with capabilities in ceph_cap_snaps to describe the file
1172 * state at the time the snapshot was taken. This must be flushed
1173 * asynchronously back to the MDS once sync writes complete and dirty
1174 * data is written out.
1176 * Called under i_lock. Takes s_mutex as needed.
1178 void __ceph_flush_snaps(struct ceph_inode_info
*ci
,
1179 struct ceph_mds_session
**psession
)
1181 struct inode
*inode
= &ci
->vfs_inode
;
1183 struct ceph_cap_snap
*capsnap
;
1185 struct ceph_mds_client
*mdsc
= &ceph_inode_to_client(inode
)->mdsc
;
1186 struct ceph_mds_session
*session
= NULL
; /* if session != NULL, we hold
1188 u64 next_follows
= 0; /* keep track of how far we've gotten through the
1189 i_cap_snaps list, and skip these entries next time
1190 around to avoid an infinite loop */
1193 session
= *psession
;
1195 dout("__flush_snaps %p\n", inode
);
1197 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
1198 /* avoid an infiniute loop after retry */
1199 if (capsnap
->follows
< next_follows
)
1202 * we need to wait for sync writes to complete and for dirty
1203 * pages to be written out.
1205 if (capsnap
->dirty_pages
|| capsnap
->writing
)
1209 * if cap writeback already occurred, we should have dropped
1210 * the capsnap in ceph_put_wrbuffer_cap_refs.
1212 BUG_ON(capsnap
->dirty
== 0);
1214 /* pick mds, take s_mutex */
1215 mds
= __ceph_get_cap_mds(ci
, &mseq
);
1216 if (session
&& session
->s_mds
!= mds
) {
1217 dout("oops, wrong session %p mutex\n", session
);
1218 mutex_unlock(&session
->s_mutex
);
1219 ceph_put_mds_session(session
);
1223 spin_unlock(&inode
->i_lock
);
1224 mutex_lock(&mdsc
->mutex
);
1225 session
= __ceph_lookup_mds_session(mdsc
, mds
);
1226 mutex_unlock(&mdsc
->mutex
);
1228 dout("inverting session/ino locks on %p\n",
1230 mutex_lock(&session
->s_mutex
);
1233 * if session == NULL, we raced against a cap
1234 * deletion. retry, and we'll get a better
1235 * @mds value next time.
1237 spin_lock(&inode
->i_lock
);
1241 capsnap
->flush_tid
= ++ci
->i_cap_flush_last_tid
;
1242 atomic_inc(&capsnap
->nref
);
1243 if (!list_empty(&capsnap
->flushing_item
))
1244 list_del_init(&capsnap
->flushing_item
);
1245 list_add_tail(&capsnap
->flushing_item
,
1246 &session
->s_cap_snaps_flushing
);
1247 spin_unlock(&inode
->i_lock
);
1249 dout("flush_snaps %p cap_snap %p follows %lld size %llu\n",
1250 inode
, capsnap
, next_follows
, capsnap
->size
);
1251 send_cap_msg(session
, ceph_vino(inode
).ino
, 0,
1252 CEPH_CAP_OP_FLUSHSNAP
, capsnap
->issued
, 0,
1253 capsnap
->dirty
, 0, capsnap
->flush_tid
, 0, mseq
,
1255 &capsnap
->mtime
, &capsnap
->atime
,
1256 capsnap
->time_warp_seq
,
1257 capsnap
->uid
, capsnap
->gid
, capsnap
->mode
,
1261 next_follows
= capsnap
->follows
+ 1;
1262 ceph_put_cap_snap(capsnap
);
1264 spin_lock(&inode
->i_lock
);
1268 /* we flushed them all; remove this inode from the queue */
1269 spin_lock(&mdsc
->snap_flush_lock
);
1270 list_del_init(&ci
->i_snap_flush_item
);
1271 spin_unlock(&mdsc
->snap_flush_lock
);
1274 *psession
= session
;
1276 mutex_unlock(&session
->s_mutex
);
1277 ceph_put_mds_session(session
);
1281 static void ceph_flush_snaps(struct ceph_inode_info
*ci
)
1283 struct inode
*inode
= &ci
->vfs_inode
;
1285 spin_lock(&inode
->i_lock
);
1286 __ceph_flush_snaps(ci
, NULL
);
1287 spin_unlock(&inode
->i_lock
);
1291 * Mark caps dirty. If inode is newly dirty, add to the global dirty
1294 void __ceph_mark_dirty_caps(struct ceph_inode_info
*ci
, int mask
)
1296 struct ceph_mds_client
*mdsc
= &ceph_client(ci
->vfs_inode
.i_sb
)->mdsc
;
1297 struct inode
*inode
= &ci
->vfs_inode
;
1298 int was
= ci
->i_dirty_caps
;
1301 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci
->vfs_inode
,
1302 ceph_cap_string(mask
), ceph_cap_string(was
),
1303 ceph_cap_string(was
| mask
));
1304 ci
->i_dirty_caps
|= mask
;
1306 dout(" inode %p now dirty\n", &ci
->vfs_inode
);
1307 BUG_ON(!list_empty(&ci
->i_dirty_item
));
1308 spin_lock(&mdsc
->cap_dirty_lock
);
1309 list_add(&ci
->i_dirty_item
, &mdsc
->cap_dirty
);
1310 spin_unlock(&mdsc
->cap_dirty_lock
);
1311 if (ci
->i_flushing_caps
== 0) {
1313 dirty
|= I_DIRTY_SYNC
;
1316 BUG_ON(list_empty(&ci
->i_dirty_item
));
1317 if (((was
| ci
->i_flushing_caps
) & CEPH_CAP_FILE_BUFFER
) &&
1318 (mask
& CEPH_CAP_FILE_BUFFER
))
1319 dirty
|= I_DIRTY_DATASYNC
;
1321 __mark_inode_dirty(inode
, dirty
);
1322 __cap_delay_requeue(mdsc
, ci
);
1326 * Add dirty inode to the flushing list. Assigned a seq number so we
1327 * can wait for caps to flush without starving.
1329 * Called under i_lock.
1331 static int __mark_caps_flushing(struct inode
*inode
,
1332 struct ceph_mds_session
*session
)
1334 struct ceph_mds_client
*mdsc
= &ceph_client(inode
->i_sb
)->mdsc
;
1335 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1338 BUG_ON(ci
->i_dirty_caps
== 0);
1339 BUG_ON(list_empty(&ci
->i_dirty_item
));
1341 flushing
= ci
->i_dirty_caps
;
1342 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1343 ceph_cap_string(flushing
),
1344 ceph_cap_string(ci
->i_flushing_caps
),
1345 ceph_cap_string(ci
->i_flushing_caps
| flushing
));
1346 ci
->i_flushing_caps
|= flushing
;
1347 ci
->i_dirty_caps
= 0;
1348 dout(" inode %p now !dirty\n", inode
);
1350 spin_lock(&mdsc
->cap_dirty_lock
);
1351 list_del_init(&ci
->i_dirty_item
);
1353 ci
->i_cap_flush_seq
= ++mdsc
->cap_flush_seq
;
1354 if (list_empty(&ci
->i_flushing_item
)) {
1355 list_add_tail(&ci
->i_flushing_item
, &session
->s_cap_flushing
);
1356 mdsc
->num_cap_flushing
++;
1357 dout(" inode %p now flushing seq %lld\n", inode
,
1358 ci
->i_cap_flush_seq
);
1360 list_move_tail(&ci
->i_flushing_item
, &session
->s_cap_flushing
);
1361 dout(" inode %p now flushing (more) seq %lld\n", inode
,
1362 ci
->i_cap_flush_seq
);
1364 spin_unlock(&mdsc
->cap_dirty_lock
);
1370 * try to invalidate mapping pages without blocking.
1372 static int mapping_is_empty(struct address_space
*mapping
)
1374 struct page
*page
= find_get_page(mapping
, 0);
1383 static int try_nonblocking_invalidate(struct inode
*inode
)
1385 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1386 u32 invalidating_gen
= ci
->i_rdcache_gen
;
1388 spin_unlock(&inode
->i_lock
);
1389 invalidate_mapping_pages(&inode
->i_data
, 0, -1);
1390 spin_lock(&inode
->i_lock
);
1392 if (mapping_is_empty(&inode
->i_data
) &&
1393 invalidating_gen
== ci
->i_rdcache_gen
) {
1395 dout("try_nonblocking_invalidate %p success\n", inode
);
1396 ci
->i_rdcache_gen
= 0;
1397 ci
->i_rdcache_revoking
= 0;
1400 dout("try_nonblocking_invalidate %p failed\n", inode
);
1405 * Swiss army knife function to examine currently used and wanted
1406 * versus held caps. Release, flush, ack revoked caps to mds as
1409 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1410 * cap release further.
1411 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1412 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1415 void ceph_check_caps(struct ceph_inode_info
*ci
, int flags
,
1416 struct ceph_mds_session
*session
)
1417 __releases(session
->s_mutex
)
1419 struct ceph_client
*client
= ceph_inode_to_client(&ci
->vfs_inode
);
1420 struct ceph_mds_client
*mdsc
= &client
->mdsc
;
1421 struct inode
*inode
= &ci
->vfs_inode
;
1422 struct ceph_cap
*cap
;
1423 int file_wanted
, used
;
1424 int took_snap_rwsem
= 0; /* true if mdsc->snap_rwsem held */
1425 int issued
, implemented
, want
, retain
, revoking
, flushing
= 0;
1426 int mds
= -1; /* keep track of how far we've gone through i_caps list
1427 to avoid an infinite loop on retry */
1429 int tried_invalidate
= 0;
1430 int delayed
= 0, sent
= 0, force_requeue
= 0, num
;
1431 int queue_invalidate
= 0;
1432 int is_delayed
= flags
& CHECK_CAPS_NODELAY
;
1434 /* if we are unmounting, flush any unused caps immediately. */
1438 spin_lock(&inode
->i_lock
);
1440 if (ci
->i_ceph_flags
& CEPH_I_FLUSH
)
1441 flags
|= CHECK_CAPS_FLUSH
;
1443 /* flush snaps first time around only */
1444 if (!list_empty(&ci
->i_cap_snaps
))
1445 __ceph_flush_snaps(ci
, &session
);
1448 spin_lock(&inode
->i_lock
);
1450 file_wanted
= __ceph_caps_file_wanted(ci
);
1451 used
= __ceph_caps_used(ci
);
1452 want
= file_wanted
| used
;
1453 issued
= __ceph_caps_issued(ci
, &implemented
);
1454 revoking
= implemented
& ~issued
;
1456 retain
= want
| CEPH_CAP_PIN
;
1457 if (!mdsc
->stopping
&& inode
->i_nlink
> 0) {
1459 retain
|= CEPH_CAP_ANY
; /* be greedy */
1461 retain
|= CEPH_CAP_ANY_SHARED
;
1463 * keep RD only if we didn't have the file open RW,
1464 * because then the mds would revoke it anyway to
1465 * journal max_size=0.
1467 if (ci
->i_max_size
== 0)
1468 retain
|= CEPH_CAP_ANY_RD
;
1472 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1473 " issued %s revoking %s retain %s %s%s%s\n", inode
,
1474 ceph_cap_string(file_wanted
),
1475 ceph_cap_string(used
), ceph_cap_string(ci
->i_dirty_caps
),
1476 ceph_cap_string(ci
->i_flushing_caps
),
1477 ceph_cap_string(issued
), ceph_cap_string(revoking
),
1478 ceph_cap_string(retain
),
1479 (flags
& CHECK_CAPS_AUTHONLY
) ? " AUTHONLY" : "",
1480 (flags
& CHECK_CAPS_NODELAY
) ? " NODELAY" : "",
1481 (flags
& CHECK_CAPS_FLUSH
) ? " FLUSH" : "");
1484 * If we no longer need to hold onto old our caps, and we may
1485 * have cached pages, but don't want them, then try to invalidate.
1486 * If we fail, it's because pages are locked.... try again later.
1488 if ((!is_delayed
|| mdsc
->stopping
) &&
1489 ci
->i_wrbuffer_ref
== 0 && /* no dirty pages... */
1490 ci
->i_rdcache_gen
&& /* may have cached pages */
1491 (file_wanted
== 0 || /* no open files */
1492 (revoking
& CEPH_CAP_FILE_CACHE
)) && /* or revoking cache */
1493 !tried_invalidate
) {
1494 dout("check_caps trying to invalidate on %p\n", inode
);
1495 if (try_nonblocking_invalidate(inode
) < 0) {
1496 if (revoking
& CEPH_CAP_FILE_CACHE
) {
1497 dout("check_caps queuing invalidate\n");
1498 queue_invalidate
= 1;
1499 ci
->i_rdcache_revoking
= ci
->i_rdcache_gen
;
1501 dout("check_caps failed to invalidate pages\n");
1502 /* we failed to invalidate pages. check these
1503 caps again later. */
1505 __cap_set_timeouts(mdsc
, ci
);
1508 tried_invalidate
= 1;
1513 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
1514 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
1517 /* avoid looping forever */
1518 if (mds
>= cap
->mds
||
1519 ((flags
& CHECK_CAPS_AUTHONLY
) && cap
!= ci
->i_auth_cap
))
1522 /* NOTE: no side-effects allowed, until we take s_mutex */
1524 revoking
= cap
->implemented
& ~cap
->issued
;
1526 dout(" mds%d revoking %s\n", cap
->mds
,
1527 ceph_cap_string(revoking
));
1529 if (cap
== ci
->i_auth_cap
&&
1530 (cap
->issued
& CEPH_CAP_FILE_WR
)) {
1531 /* request larger max_size from MDS? */
1532 if (ci
->i_wanted_max_size
> ci
->i_max_size
&&
1533 ci
->i_wanted_max_size
> ci
->i_requested_max_size
) {
1534 dout("requesting new max_size\n");
1538 /* approaching file_max? */
1539 if ((inode
->i_size
<< 1) >= ci
->i_max_size
&&
1540 (ci
->i_reported_size
<< 1) < ci
->i_max_size
) {
1541 dout("i_size approaching max_size\n");
1545 /* flush anything dirty? */
1546 if (cap
== ci
->i_auth_cap
&& (flags
& CHECK_CAPS_FLUSH
) &&
1548 dout("flushing dirty caps\n");
1552 /* completed revocation? going down and there are no caps? */
1553 if (revoking
&& (revoking
& used
) == 0) {
1554 dout("completed revocation of %s\n",
1555 ceph_cap_string(cap
->implemented
& ~cap
->issued
));
1559 /* want more caps from mds? */
1560 if (want
& ~(cap
->mds_wanted
| cap
->issued
))
1563 /* things we might delay */
1564 if ((cap
->issued
& ~retain
) == 0 &&
1565 cap
->mds_wanted
== want
)
1566 continue; /* nope, all good */
1572 if ((ci
->i_ceph_flags
& CEPH_I_NODELAY
) == 0 &&
1573 time_before(jiffies
, ci
->i_hold_caps_max
)) {
1574 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1575 ceph_cap_string(cap
->issued
),
1576 ceph_cap_string(cap
->issued
& retain
),
1577 ceph_cap_string(cap
->mds_wanted
),
1578 ceph_cap_string(want
));
1584 if (ci
->i_ceph_flags
& CEPH_I_NOFLUSH
) {
1585 dout(" skipping %p I_NOFLUSH set\n", inode
);
1589 if (session
&& session
!= cap
->session
) {
1590 dout("oops, wrong session %p mutex\n", session
);
1591 mutex_unlock(&session
->s_mutex
);
1595 session
= cap
->session
;
1596 if (mutex_trylock(&session
->s_mutex
) == 0) {
1597 dout("inverting session/ino locks on %p\n",
1599 spin_unlock(&inode
->i_lock
);
1600 if (took_snap_rwsem
) {
1601 up_read(&mdsc
->snap_rwsem
);
1602 took_snap_rwsem
= 0;
1604 mutex_lock(&session
->s_mutex
);
1608 /* take snap_rwsem after session mutex */
1609 if (!took_snap_rwsem
) {
1610 if (down_read_trylock(&mdsc
->snap_rwsem
) == 0) {
1611 dout("inverting snap/in locks on %p\n",
1613 spin_unlock(&inode
->i_lock
);
1614 down_read(&mdsc
->snap_rwsem
);
1615 took_snap_rwsem
= 1;
1618 took_snap_rwsem
= 1;
1621 if (cap
== ci
->i_auth_cap
&& ci
->i_dirty_caps
)
1622 flushing
= __mark_caps_flushing(inode
, session
);
1624 mds
= cap
->mds
; /* remember mds, so we don't repeat */
1627 /* __send_cap drops i_lock */
1628 delayed
+= __send_cap(mdsc
, cap
, CEPH_CAP_OP_UPDATE
, used
, want
,
1629 retain
, flushing
, NULL
);
1630 goto retry
; /* retake i_lock and restart our cap scan. */
1634 * Reschedule delayed caps release if we delayed anything,
1637 if (delayed
&& is_delayed
)
1638 force_requeue
= 1; /* __send_cap delayed release; requeue */
1639 if (!delayed
&& !is_delayed
)
1640 __cap_delay_cancel(mdsc
, ci
);
1641 else if (!is_delayed
|| force_requeue
)
1642 __cap_delay_requeue(mdsc
, ci
);
1644 spin_unlock(&inode
->i_lock
);
1646 if (queue_invalidate
)
1647 ceph_queue_invalidate(inode
);
1650 mutex_unlock(&session
->s_mutex
);
1651 if (took_snap_rwsem
)
1652 up_read(&mdsc
->snap_rwsem
);
1656 * Try to flush dirty caps back to the auth mds.
1658 static int try_flush_caps(struct inode
*inode
, struct ceph_mds_session
*session
,
1659 unsigned *flush_tid
)
1661 struct ceph_mds_client
*mdsc
= &ceph_client(inode
->i_sb
)->mdsc
;
1662 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1663 int unlock_session
= session
? 0 : 1;
1667 spin_lock(&inode
->i_lock
);
1668 if (ci
->i_ceph_flags
& CEPH_I_NOFLUSH
) {
1669 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode
);
1672 if (ci
->i_dirty_caps
&& ci
->i_auth_cap
) {
1673 struct ceph_cap
*cap
= ci
->i_auth_cap
;
1674 int used
= __ceph_caps_used(ci
);
1675 int want
= __ceph_caps_wanted(ci
);
1679 spin_unlock(&inode
->i_lock
);
1680 session
= cap
->session
;
1681 mutex_lock(&session
->s_mutex
);
1684 BUG_ON(session
!= cap
->session
);
1685 if (cap
->session
->s_state
< CEPH_MDS_SESSION_OPEN
)
1688 flushing
= __mark_caps_flushing(inode
, session
);
1690 /* __send_cap drops i_lock */
1691 delayed
= __send_cap(mdsc
, cap
, CEPH_CAP_OP_FLUSH
, used
, want
,
1692 cap
->issued
| cap
->implemented
, flushing
,
1697 spin_lock(&inode
->i_lock
);
1698 __cap_delay_requeue(mdsc
, ci
);
1701 spin_unlock(&inode
->i_lock
);
1703 if (session
&& unlock_session
)
1704 mutex_unlock(&session
->s_mutex
);
1709 * Return true if we've flushed caps through the given flush_tid.
1711 static int caps_are_flushed(struct inode
*inode
, unsigned tid
)
1713 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1714 int dirty
, i
, ret
= 1;
1716 spin_lock(&inode
->i_lock
);
1717 dirty
= __ceph_caps_dirty(ci
);
1718 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
1719 if ((ci
->i_flushing_caps
& (1 << i
)) &&
1720 ci
->i_cap_flush_tid
[i
] <= tid
) {
1721 /* still flushing this bit */
1725 spin_unlock(&inode
->i_lock
);
1730 * Wait on any unsafe replies for the given inode. First wait on the
1731 * newest request, and make that the upper bound. Then, if there are
1732 * more requests, keep waiting on the oldest as long as it is still older
1733 * than the original request.
1735 static void sync_write_wait(struct inode
*inode
)
1737 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1738 struct list_head
*head
= &ci
->i_unsafe_writes
;
1739 struct ceph_osd_request
*req
;
1742 spin_lock(&ci
->i_unsafe_lock
);
1743 if (list_empty(head
))
1746 /* set upper bound as _last_ entry in chain */
1747 req
= list_entry(head
->prev
, struct ceph_osd_request
,
1749 last_tid
= req
->r_tid
;
1752 ceph_osdc_get_request(req
);
1753 spin_unlock(&ci
->i_unsafe_lock
);
1754 dout("sync_write_wait on tid %llu (until %llu)\n",
1755 req
->r_tid
, last_tid
);
1756 wait_for_completion(&req
->r_safe_completion
);
1757 spin_lock(&ci
->i_unsafe_lock
);
1758 ceph_osdc_put_request(req
);
1761 * from here on look at first entry in chain, since we
1762 * only want to wait for anything older than last_tid
1764 if (list_empty(head
))
1766 req
= list_entry(head
->next
, struct ceph_osd_request
,
1768 } while (req
->r_tid
< last_tid
);
1770 spin_unlock(&ci
->i_unsafe_lock
);
1773 int ceph_fsync(struct file
*file
, struct dentry
*dentry
, int datasync
)
1775 struct inode
*inode
= dentry
->d_inode
;
1776 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1781 dout("fsync %p%s\n", inode
, datasync
? " datasync" : "");
1782 sync_write_wait(inode
);
1784 ret
= filemap_write_and_wait(inode
->i_mapping
);
1788 dirty
= try_flush_caps(inode
, NULL
, &flush_tid
);
1789 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty
));
1792 * only wait on non-file metadata writeback (the mds
1793 * can recover size and mtime, so we don't need to
1796 if (!datasync
&& (dirty
& ~CEPH_CAP_ANY_FILE_WR
)) {
1797 dout("fsync waiting for flush_tid %u\n", flush_tid
);
1798 ret
= wait_event_interruptible(ci
->i_cap_wq
,
1799 caps_are_flushed(inode
, flush_tid
));
1802 dout("fsync %p%s done\n", inode
, datasync
? " datasync" : "");
1807 * Flush any dirty caps back to the mds. If we aren't asked to wait,
1808 * queue inode for flush but don't do so immediately, because we can
1809 * get by with fewer MDS messages if we wait for data writeback to
1812 int ceph_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1814 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1818 int wait
= wbc
->sync_mode
== WB_SYNC_ALL
;
1820 dout("write_inode %p wait=%d\n", inode
, wait
);
1822 dirty
= try_flush_caps(inode
, NULL
, &flush_tid
);
1824 err
= wait_event_interruptible(ci
->i_cap_wq
,
1825 caps_are_flushed(inode
, flush_tid
));
1827 struct ceph_mds_client
*mdsc
= &ceph_client(inode
->i_sb
)->mdsc
;
1829 spin_lock(&inode
->i_lock
);
1830 if (__ceph_caps_dirty(ci
))
1831 __cap_delay_requeue_front(mdsc
, ci
);
1832 spin_unlock(&inode
->i_lock
);
1838 * After a recovering MDS goes active, we need to resend any caps
1841 * Caller holds session->s_mutex.
1843 static void kick_flushing_capsnaps(struct ceph_mds_client
*mdsc
,
1844 struct ceph_mds_session
*session
)
1846 struct ceph_cap_snap
*capsnap
;
1848 dout("kick_flushing_capsnaps mds%d\n", session
->s_mds
);
1849 list_for_each_entry(capsnap
, &session
->s_cap_snaps_flushing
,
1851 struct ceph_inode_info
*ci
= capsnap
->ci
;
1852 struct inode
*inode
= &ci
->vfs_inode
;
1853 struct ceph_cap
*cap
;
1855 spin_lock(&inode
->i_lock
);
1856 cap
= ci
->i_auth_cap
;
1857 if (cap
&& cap
->session
== session
) {
1858 dout("kick_flushing_caps %p cap %p capsnap %p\n", inode
,
1860 __ceph_flush_snaps(ci
, &session
);
1862 pr_err("%p auth cap %p not mds%d ???\n", inode
,
1863 cap
, session
->s_mds
);
1864 spin_unlock(&inode
->i_lock
);
1869 void ceph_kick_flushing_caps(struct ceph_mds_client
*mdsc
,
1870 struct ceph_mds_session
*session
)
1872 struct ceph_inode_info
*ci
;
1874 kick_flushing_capsnaps(mdsc
, session
);
1876 dout("kick_flushing_caps mds%d\n", session
->s_mds
);
1877 list_for_each_entry(ci
, &session
->s_cap_flushing
, i_flushing_item
) {
1878 struct inode
*inode
= &ci
->vfs_inode
;
1879 struct ceph_cap
*cap
;
1882 spin_lock(&inode
->i_lock
);
1883 cap
= ci
->i_auth_cap
;
1884 if (cap
&& cap
->session
== session
) {
1885 dout("kick_flushing_caps %p cap %p %s\n", inode
,
1886 cap
, ceph_cap_string(ci
->i_flushing_caps
));
1887 delayed
= __send_cap(mdsc
, cap
, CEPH_CAP_OP_FLUSH
,
1888 __ceph_caps_used(ci
),
1889 __ceph_caps_wanted(ci
),
1890 cap
->issued
| cap
->implemented
,
1891 ci
->i_flushing_caps
, NULL
);
1893 spin_lock(&inode
->i_lock
);
1894 __cap_delay_requeue(mdsc
, ci
);
1895 spin_unlock(&inode
->i_lock
);
1898 pr_err("%p auth cap %p not mds%d ???\n", inode
,
1899 cap
, session
->s_mds
);
1900 spin_unlock(&inode
->i_lock
);
1907 * Take references to capabilities we hold, so that we don't release
1908 * them to the MDS prematurely.
1910 * Protected by i_lock.
1912 static void __take_cap_refs(struct ceph_inode_info
*ci
, int got
)
1914 if (got
& CEPH_CAP_PIN
)
1916 if (got
& CEPH_CAP_FILE_RD
)
1918 if (got
& CEPH_CAP_FILE_CACHE
)
1919 ci
->i_rdcache_ref
++;
1920 if (got
& CEPH_CAP_FILE_WR
)
1922 if (got
& CEPH_CAP_FILE_BUFFER
) {
1923 if (ci
->i_wrbuffer_ref
== 0)
1924 igrab(&ci
->vfs_inode
);
1925 ci
->i_wrbuffer_ref
++;
1926 dout("__take_cap_refs %p wrbuffer %d -> %d (?)\n",
1927 &ci
->vfs_inode
, ci
->i_wrbuffer_ref
-1, ci
->i_wrbuffer_ref
);
1932 * Try to grab cap references. Specify those refs we @want, and the
1933 * minimal set we @need. Also include the larger offset we are writing
1934 * to (when applicable), and check against max_size here as well.
1935 * Note that caller is responsible for ensuring max_size increases are
1936 * requested from the MDS.
1938 static int try_get_cap_refs(struct ceph_inode_info
*ci
, int need
, int want
,
1939 int *got
, loff_t endoff
, int *check_max
, int *err
)
1941 struct inode
*inode
= &ci
->vfs_inode
;
1943 int have
, implemented
;
1946 dout("get_cap_refs %p need %s want %s\n", inode
,
1947 ceph_cap_string(need
), ceph_cap_string(want
));
1948 spin_lock(&inode
->i_lock
);
1950 /* make sure file is actually open */
1951 file_wanted
= __ceph_caps_file_wanted(ci
);
1952 if ((file_wanted
& need
) == 0) {
1953 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
1954 ceph_cap_string(need
), ceph_cap_string(file_wanted
));
1960 if (need
& CEPH_CAP_FILE_WR
) {
1961 if (endoff
>= 0 && endoff
> (loff_t
)ci
->i_max_size
) {
1962 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
1963 inode
, endoff
, ci
->i_max_size
);
1964 if (endoff
> ci
->i_wanted_max_size
) {
1971 * If a sync write is in progress, we must wait, so that we
1972 * can get a final snapshot value for size+mtime.
1974 if (__ceph_have_pending_cap_snap(ci
)) {
1975 dout("get_cap_refs %p cap_snap_pending\n", inode
);
1979 have
= __ceph_caps_issued(ci
, &implemented
);
1982 * disallow writes while a truncate is pending
1984 if (ci
->i_truncate_pending
)
1985 have
&= ~CEPH_CAP_FILE_WR
;
1987 if ((have
& need
) == need
) {
1989 * Look at (implemented & ~have & not) so that we keep waiting
1990 * on transition from wanted -> needed caps. This is needed
1991 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
1992 * going before a prior buffered writeback happens.
1994 int not = want
& ~(have
& need
);
1995 int revoking
= implemented
& ~have
;
1996 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
1997 inode
, ceph_cap_string(have
), ceph_cap_string(not),
1998 ceph_cap_string(revoking
));
1999 if ((revoking
& not) == 0) {
2000 *got
= need
| (have
& want
);
2001 __take_cap_refs(ci
, *got
);
2005 dout("get_cap_refs %p have %s needed %s\n", inode
,
2006 ceph_cap_string(have
), ceph_cap_string(need
));
2009 spin_unlock(&inode
->i_lock
);
2010 dout("get_cap_refs %p ret %d got %s\n", inode
,
2011 ret
, ceph_cap_string(*got
));
2016 * Check the offset we are writing up to against our current
2017 * max_size. If necessary, tell the MDS we want to write to
2020 static void check_max_size(struct inode
*inode
, loff_t endoff
)
2022 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2025 /* do we need to explicitly request a larger max_size? */
2026 spin_lock(&inode
->i_lock
);
2027 if ((endoff
>= ci
->i_max_size
||
2028 endoff
> (inode
->i_size
<< 1)) &&
2029 endoff
> ci
->i_wanted_max_size
) {
2030 dout("write %p at large endoff %llu, req max_size\n",
2032 ci
->i_wanted_max_size
= endoff
;
2035 spin_unlock(&inode
->i_lock
);
2037 ceph_check_caps(ci
, CHECK_CAPS_AUTHONLY
, NULL
);
2041 * Wait for caps, and take cap references. If we can't get a WR cap
2042 * due to a small max_size, make sure we check_max_size (and possibly
2043 * ask the mds) so we don't get hung up indefinitely.
2045 int ceph_get_caps(struct ceph_inode_info
*ci
, int need
, int want
, int *got
,
2048 int check_max
, ret
, err
;
2052 check_max_size(&ci
->vfs_inode
, endoff
);
2055 ret
= wait_event_interruptible(ci
->i_cap_wq
,
2056 try_get_cap_refs(ci
, need
, want
,
2067 * Take cap refs. Caller must already know we hold at least one ref
2068 * on the caps in question or we don't know this is safe.
2070 void ceph_get_cap_refs(struct ceph_inode_info
*ci
, int caps
)
2072 spin_lock(&ci
->vfs_inode
.i_lock
);
2073 __take_cap_refs(ci
, caps
);
2074 spin_unlock(&ci
->vfs_inode
.i_lock
);
2080 * If we released the last ref on any given cap, call ceph_check_caps
2081 * to release (or schedule a release).
2083 * If we are releasing a WR cap (from a sync write), finalize any affected
2084 * cap_snap, and wake up any waiters.
2086 void ceph_put_cap_refs(struct ceph_inode_info
*ci
, int had
)
2088 struct inode
*inode
= &ci
->vfs_inode
;
2089 int last
= 0, put
= 0, flushsnaps
= 0, wake
= 0;
2090 struct ceph_cap_snap
*capsnap
;
2092 spin_lock(&inode
->i_lock
);
2093 if (had
& CEPH_CAP_PIN
)
2095 if (had
& CEPH_CAP_FILE_RD
)
2096 if (--ci
->i_rd_ref
== 0)
2098 if (had
& CEPH_CAP_FILE_CACHE
)
2099 if (--ci
->i_rdcache_ref
== 0)
2101 if (had
& CEPH_CAP_FILE_BUFFER
) {
2102 if (--ci
->i_wrbuffer_ref
== 0) {
2106 dout("put_cap_refs %p wrbuffer %d -> %d (?)\n",
2107 inode
, ci
->i_wrbuffer_ref
+1, ci
->i_wrbuffer_ref
);
2109 if (had
& CEPH_CAP_FILE_WR
)
2110 if (--ci
->i_wr_ref
== 0) {
2112 if (!list_empty(&ci
->i_cap_snaps
)) {
2113 capsnap
= list_first_entry(&ci
->i_cap_snaps
,
2114 struct ceph_cap_snap
,
2116 if (capsnap
->writing
) {
2117 capsnap
->writing
= 0;
2119 __ceph_finish_cap_snap(ci
,
2125 spin_unlock(&inode
->i_lock
);
2127 dout("put_cap_refs %p had %s%s%s\n", inode
, ceph_cap_string(had
),
2128 last
? " last" : "", put
? " put" : "");
2130 if (last
&& !flushsnaps
)
2131 ceph_check_caps(ci
, 0, NULL
);
2132 else if (flushsnaps
)
2133 ceph_flush_snaps(ci
);
2135 wake_up(&ci
->i_cap_wq
);
2141 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2142 * context. Adjust per-snap dirty page accounting as appropriate.
2143 * Once all dirty data for a cap_snap is flushed, flush snapped file
2144 * metadata back to the MDS. If we dropped the last ref, call
2147 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info
*ci
, int nr
,
2148 struct ceph_snap_context
*snapc
)
2150 struct inode
*inode
= &ci
->vfs_inode
;
2152 int complete_capsnap
= 0;
2153 int drop_capsnap
= 0;
2155 struct ceph_cap_snap
*capsnap
= NULL
;
2157 spin_lock(&inode
->i_lock
);
2158 ci
->i_wrbuffer_ref
-= nr
;
2159 last
= !ci
->i_wrbuffer_ref
;
2161 if (ci
->i_head_snapc
== snapc
) {
2162 ci
->i_wrbuffer_ref_head
-= nr
;
2163 if (!ci
->i_wrbuffer_ref_head
) {
2164 ceph_put_snap_context(ci
->i_head_snapc
);
2165 ci
->i_head_snapc
= NULL
;
2167 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2169 ci
->i_wrbuffer_ref
+nr
, ci
->i_wrbuffer_ref_head
+nr
,
2170 ci
->i_wrbuffer_ref
, ci
->i_wrbuffer_ref_head
,
2171 last
? " LAST" : "");
2173 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
2174 if (capsnap
->context
== snapc
) {
2180 capsnap
->dirty_pages
-= nr
;
2181 if (capsnap
->dirty_pages
== 0) {
2182 complete_capsnap
= 1;
2183 if (capsnap
->dirty
== 0)
2184 /* cap writeback completed before we created
2185 * the cap_snap; no FLUSHSNAP is needed */
2188 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2189 " snap %lld %d/%d -> %d/%d %s%s%s\n",
2190 inode
, capsnap
, capsnap
->context
->seq
,
2191 ci
->i_wrbuffer_ref
+nr
, capsnap
->dirty_pages
+ nr
,
2192 ci
->i_wrbuffer_ref
, capsnap
->dirty_pages
,
2193 last
? " (wrbuffer last)" : "",
2194 complete_capsnap
? " (complete capsnap)" : "",
2195 drop_capsnap
? " (drop capsnap)" : "");
2197 ceph_put_snap_context(capsnap
->context
);
2198 list_del(&capsnap
->ci_item
);
2199 list_del(&capsnap
->flushing_item
);
2200 ceph_put_cap_snap(capsnap
);
2204 spin_unlock(&inode
->i_lock
);
2207 ceph_check_caps(ci
, CHECK_CAPS_AUTHONLY
, NULL
);
2209 } else if (complete_capsnap
) {
2210 ceph_flush_snaps(ci
);
2211 wake_up(&ci
->i_cap_wq
);
2218 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2219 * actually be a revocation if it specifies a smaller cap set.)
2221 * caller holds s_mutex and i_lock, we drop both.
2225 * 1 - check_caps on auth cap only (writeback)
2226 * 2 - check_caps (ack revoke)
2228 static void handle_cap_grant(struct inode
*inode
, struct ceph_mds_caps
*grant
,
2229 struct ceph_mds_session
*session
,
2230 struct ceph_cap
*cap
,
2231 struct ceph_buffer
*xattr_buf
)
2232 __releases(inode
->i_lock
)
2233 __releases(session
->s_mutex
)
2235 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2236 int mds
= session
->s_mds
;
2237 int seq
= le32_to_cpu(grant
->seq
);
2238 int newcaps
= le32_to_cpu(grant
->caps
);
2239 int issued
, implemented
, used
, wanted
, dirty
;
2240 u64 size
= le64_to_cpu(grant
->size
);
2241 u64 max_size
= le64_to_cpu(grant
->max_size
);
2242 struct timespec mtime
, atime
, ctime
;
2246 int revoked_rdcache
= 0;
2247 int queue_invalidate
= 0;
2249 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2250 inode
, cap
, mds
, seq
, ceph_cap_string(newcaps
));
2251 dout(" size %llu max_size %llu, i_size %llu\n", size
, max_size
,
2255 * If CACHE is being revoked, and we have no dirty buffers,
2256 * try to invalidate (once). (If there are dirty buffers, we
2257 * will invalidate _after_ writeback.)
2259 if (((cap
->issued
& ~newcaps
) & CEPH_CAP_FILE_CACHE
) &&
2260 !ci
->i_wrbuffer_ref
) {
2261 if (try_nonblocking_invalidate(inode
) == 0) {
2262 revoked_rdcache
= 1;
2264 /* there were locked pages.. invalidate later
2265 in a separate thread. */
2266 if (ci
->i_rdcache_revoking
!= ci
->i_rdcache_gen
) {
2267 queue_invalidate
= 1;
2268 ci
->i_rdcache_revoking
= ci
->i_rdcache_gen
;
2273 /* side effects now are allowed */
2275 issued
= __ceph_caps_issued(ci
, &implemented
);
2276 issued
|= implemented
| __ceph_caps_dirty(ci
);
2278 cap
->cap_gen
= session
->s_cap_gen
;
2280 __check_cap_issue(ci
, cap
, newcaps
);
2282 if ((issued
& CEPH_CAP_AUTH_EXCL
) == 0) {
2283 inode
->i_mode
= le32_to_cpu(grant
->mode
);
2284 inode
->i_uid
= le32_to_cpu(grant
->uid
);
2285 inode
->i_gid
= le32_to_cpu(grant
->gid
);
2286 dout("%p mode 0%o uid.gid %d.%d\n", inode
, inode
->i_mode
,
2287 inode
->i_uid
, inode
->i_gid
);
2290 if ((issued
& CEPH_CAP_LINK_EXCL
) == 0)
2291 inode
->i_nlink
= le32_to_cpu(grant
->nlink
);
2293 if ((issued
& CEPH_CAP_XATTR_EXCL
) == 0 && grant
->xattr_len
) {
2294 int len
= le32_to_cpu(grant
->xattr_len
);
2295 u64 version
= le64_to_cpu(grant
->xattr_version
);
2297 if (version
> ci
->i_xattrs
.version
) {
2298 dout(" got new xattrs v%llu on %p len %d\n",
2299 version
, inode
, len
);
2300 if (ci
->i_xattrs
.blob
)
2301 ceph_buffer_put(ci
->i_xattrs
.blob
);
2302 ci
->i_xattrs
.blob
= ceph_buffer_get(xattr_buf
);
2303 ci
->i_xattrs
.version
= version
;
2307 /* size/ctime/mtime/atime? */
2308 ceph_fill_file_size(inode
, issued
,
2309 le32_to_cpu(grant
->truncate_seq
),
2310 le64_to_cpu(grant
->truncate_size
), size
);
2311 ceph_decode_timespec(&mtime
, &grant
->mtime
);
2312 ceph_decode_timespec(&atime
, &grant
->atime
);
2313 ceph_decode_timespec(&ctime
, &grant
->ctime
);
2314 ceph_fill_file_time(inode
, issued
,
2315 le32_to_cpu(grant
->time_warp_seq
), &ctime
, &mtime
,
2318 /* max size increase? */
2319 if (max_size
!= ci
->i_max_size
) {
2320 dout("max_size %lld -> %llu\n", ci
->i_max_size
, max_size
);
2321 ci
->i_max_size
= max_size
;
2322 if (max_size
>= ci
->i_wanted_max_size
) {
2323 ci
->i_wanted_max_size
= 0; /* reset */
2324 ci
->i_requested_max_size
= 0;
2329 /* check cap bits */
2330 wanted
= __ceph_caps_wanted(ci
);
2331 used
= __ceph_caps_used(ci
);
2332 dirty
= __ceph_caps_dirty(ci
);
2333 dout(" my wanted = %s, used = %s, dirty %s\n",
2334 ceph_cap_string(wanted
),
2335 ceph_cap_string(used
),
2336 ceph_cap_string(dirty
));
2337 if (wanted
!= le32_to_cpu(grant
->wanted
)) {
2338 dout("mds wanted %s -> %s\n",
2339 ceph_cap_string(le32_to_cpu(grant
->wanted
)),
2340 ceph_cap_string(wanted
));
2341 grant
->wanted
= cpu_to_le32(wanted
);
2346 /* file layout may have changed */
2347 ci
->i_layout
= grant
->layout
;
2349 /* revocation, grant, or no-op? */
2350 if (cap
->issued
& ~newcaps
) {
2351 dout("revocation: %s -> %s\n", ceph_cap_string(cap
->issued
),
2352 ceph_cap_string(newcaps
));
2353 if ((used
& ~newcaps
) & CEPH_CAP_FILE_BUFFER
)
2354 writeback
= 1; /* will delay ack */
2355 else if (dirty
& ~newcaps
)
2356 check_caps
= 1; /* initiate writeback in check_caps */
2357 else if (((used
& ~newcaps
) & CEPH_CAP_FILE_CACHE
) == 0 ||
2359 check_caps
= 2; /* send revoke ack in check_caps */
2360 cap
->issued
= newcaps
;
2361 cap
->implemented
|= newcaps
;
2362 } else if (cap
->issued
== newcaps
) {
2363 dout("caps unchanged: %s -> %s\n",
2364 ceph_cap_string(cap
->issued
), ceph_cap_string(newcaps
));
2366 dout("grant: %s -> %s\n", ceph_cap_string(cap
->issued
),
2367 ceph_cap_string(newcaps
));
2368 cap
->issued
= newcaps
;
2369 cap
->implemented
|= newcaps
; /* add bits only, to
2370 * avoid stepping on a
2371 * pending revocation */
2374 BUG_ON(cap
->issued
& ~cap
->implemented
);
2376 spin_unlock(&inode
->i_lock
);
2379 * queue inode for writeback: we can't actually call
2380 * filemap_write_and_wait, etc. from message handler
2383 ceph_queue_writeback(inode
);
2384 if (queue_invalidate
)
2385 ceph_queue_invalidate(inode
);
2387 wake_up(&ci
->i_cap_wq
);
2389 if (check_caps
== 1)
2390 ceph_check_caps(ci
, CHECK_CAPS_NODELAY
|CHECK_CAPS_AUTHONLY
,
2392 else if (check_caps
== 2)
2393 ceph_check_caps(ci
, CHECK_CAPS_NODELAY
, session
);
2395 mutex_unlock(&session
->s_mutex
);
2399 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2400 * MDS has been safely committed.
2402 static void handle_cap_flush_ack(struct inode
*inode
, u64 flush_tid
,
2403 struct ceph_mds_caps
*m
,
2404 struct ceph_mds_session
*session
,
2405 struct ceph_cap
*cap
)
2406 __releases(inode
->i_lock
)
2408 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2409 struct ceph_mds_client
*mdsc
= &ceph_client(inode
->i_sb
)->mdsc
;
2410 unsigned seq
= le32_to_cpu(m
->seq
);
2411 int dirty
= le32_to_cpu(m
->dirty
);
2416 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
2417 if ((dirty
& (1 << i
)) &&
2418 flush_tid
== ci
->i_cap_flush_tid
[i
])
2421 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2422 " flushing %s -> %s\n",
2423 inode
, session
->s_mds
, seq
, ceph_cap_string(dirty
),
2424 ceph_cap_string(cleaned
), ceph_cap_string(ci
->i_flushing_caps
),
2425 ceph_cap_string(ci
->i_flushing_caps
& ~cleaned
));
2427 if (ci
->i_flushing_caps
== (ci
->i_flushing_caps
& ~cleaned
))
2430 ci
->i_flushing_caps
&= ~cleaned
;
2432 spin_lock(&mdsc
->cap_dirty_lock
);
2433 if (ci
->i_flushing_caps
== 0) {
2434 list_del_init(&ci
->i_flushing_item
);
2435 if (!list_empty(&session
->s_cap_flushing
))
2436 dout(" mds%d still flushing cap on %p\n",
2438 &list_entry(session
->s_cap_flushing
.next
,
2439 struct ceph_inode_info
,
2440 i_flushing_item
)->vfs_inode
);
2441 mdsc
->num_cap_flushing
--;
2442 wake_up(&mdsc
->cap_flushing_wq
);
2443 dout(" inode %p now !flushing\n", inode
);
2445 if (ci
->i_dirty_caps
== 0) {
2446 dout(" inode %p now clean\n", inode
);
2447 BUG_ON(!list_empty(&ci
->i_dirty_item
));
2450 BUG_ON(list_empty(&ci
->i_dirty_item
));
2453 spin_unlock(&mdsc
->cap_dirty_lock
);
2454 wake_up(&ci
->i_cap_wq
);
2457 spin_unlock(&inode
->i_lock
);
2463 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
2464 * throw away our cap_snap.
2466 * Caller hold s_mutex.
2468 static void handle_cap_flushsnap_ack(struct inode
*inode
, u64 flush_tid
,
2469 struct ceph_mds_caps
*m
,
2470 struct ceph_mds_session
*session
)
2472 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2473 u64 follows
= le64_to_cpu(m
->snap_follows
);
2474 struct ceph_cap_snap
*capsnap
;
2477 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2478 inode
, ci
, session
->s_mds
, follows
);
2480 spin_lock(&inode
->i_lock
);
2481 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
2482 if (capsnap
->follows
== follows
) {
2483 if (capsnap
->flush_tid
!= flush_tid
) {
2484 dout(" cap_snap %p follows %lld tid %lld !="
2485 " %lld\n", capsnap
, follows
,
2486 flush_tid
, capsnap
->flush_tid
);
2489 WARN_ON(capsnap
->dirty_pages
|| capsnap
->writing
);
2490 dout(" removing %p cap_snap %p follows %lld\n",
2491 inode
, capsnap
, follows
);
2492 ceph_put_snap_context(capsnap
->context
);
2493 list_del(&capsnap
->ci_item
);
2494 list_del(&capsnap
->flushing_item
);
2495 ceph_put_cap_snap(capsnap
);
2499 dout(" skipping cap_snap %p follows %lld\n",
2500 capsnap
, capsnap
->follows
);
2503 spin_unlock(&inode
->i_lock
);
2509 * Handle TRUNC from MDS, indicating file truncation.
2511 * caller hold s_mutex.
2513 static void handle_cap_trunc(struct inode
*inode
,
2514 struct ceph_mds_caps
*trunc
,
2515 struct ceph_mds_session
*session
)
2516 __releases(inode
->i_lock
)
2518 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2519 int mds
= session
->s_mds
;
2520 int seq
= le32_to_cpu(trunc
->seq
);
2521 u32 truncate_seq
= le32_to_cpu(trunc
->truncate_seq
);
2522 u64 truncate_size
= le64_to_cpu(trunc
->truncate_size
);
2523 u64 size
= le64_to_cpu(trunc
->size
);
2524 int implemented
= 0;
2525 int dirty
= __ceph_caps_dirty(ci
);
2526 int issued
= __ceph_caps_issued(ceph_inode(inode
), &implemented
);
2527 int queue_trunc
= 0;
2529 issued
|= implemented
| dirty
;
2531 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2532 inode
, mds
, seq
, truncate_size
, truncate_seq
);
2533 queue_trunc
= ceph_fill_file_size(inode
, issued
,
2534 truncate_seq
, truncate_size
, size
);
2535 spin_unlock(&inode
->i_lock
);
2538 ceph_queue_vmtruncate(inode
);
2542 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
2543 * different one. If we are the most recent migration we've seen (as
2544 * indicated by mseq), make note of the migrating cap bits for the
2545 * duration (until we see the corresponding IMPORT).
2547 * caller holds s_mutex
2549 static void handle_cap_export(struct inode
*inode
, struct ceph_mds_caps
*ex
,
2550 struct ceph_mds_session
*session
)
2552 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2553 int mds
= session
->s_mds
;
2554 unsigned mseq
= le32_to_cpu(ex
->migrate_seq
);
2555 struct ceph_cap
*cap
= NULL
, *t
;
2559 dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
2560 inode
, ci
, mds
, mseq
);
2562 spin_lock(&inode
->i_lock
);
2564 /* make sure we haven't seen a higher mseq */
2565 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
2566 t
= rb_entry(p
, struct ceph_cap
, ci_node
);
2567 if (ceph_seq_cmp(t
->mseq
, mseq
) > 0) {
2568 dout(" higher mseq on cap from mds%d\n",
2572 if (t
->session
->s_mds
== mds
)
2579 ci
->i_cap_exporting_mds
= mds
;
2580 ci
->i_cap_exporting_mseq
= mseq
;
2581 ci
->i_cap_exporting_issued
= cap
->issued
;
2583 __ceph_remove_cap(cap
);
2585 /* else, we already released it */
2587 spin_unlock(&inode
->i_lock
);
2591 * Handle cap IMPORT. If there are temp bits from an older EXPORT,
2594 * caller holds s_mutex.
2596 static void handle_cap_import(struct ceph_mds_client
*mdsc
,
2597 struct inode
*inode
, struct ceph_mds_caps
*im
,
2598 struct ceph_mds_session
*session
,
2599 void *snaptrace
, int snaptrace_len
)
2601 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2602 int mds
= session
->s_mds
;
2603 unsigned issued
= le32_to_cpu(im
->caps
);
2604 unsigned wanted
= le32_to_cpu(im
->wanted
);
2605 unsigned seq
= le32_to_cpu(im
->seq
);
2606 unsigned mseq
= le32_to_cpu(im
->migrate_seq
);
2607 u64 realmino
= le64_to_cpu(im
->realm
);
2608 u64 cap_id
= le64_to_cpu(im
->cap_id
);
2610 if (ci
->i_cap_exporting_mds
>= 0 &&
2611 ceph_seq_cmp(ci
->i_cap_exporting_mseq
, mseq
) < 0) {
2612 dout("handle_cap_import inode %p ci %p mds%d mseq %d"
2613 " - cleared exporting from mds%d\n",
2614 inode
, ci
, mds
, mseq
,
2615 ci
->i_cap_exporting_mds
);
2616 ci
->i_cap_exporting_issued
= 0;
2617 ci
->i_cap_exporting_mseq
= 0;
2618 ci
->i_cap_exporting_mds
= -1;
2620 dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
2621 inode
, ci
, mds
, mseq
);
2624 down_write(&mdsc
->snap_rwsem
);
2625 ceph_update_snap_trace(mdsc
, snaptrace
, snaptrace
+snaptrace_len
,
2627 downgrade_write(&mdsc
->snap_rwsem
);
2628 ceph_add_cap(inode
, session
, cap_id
, -1,
2629 issued
, wanted
, seq
, mseq
, realmino
, CEPH_CAP_FLAG_AUTH
,
2630 NULL
/* no caps context */);
2631 try_flush_caps(inode
, session
, NULL
);
2632 up_read(&mdsc
->snap_rwsem
);
2636 * Handle a caps message from the MDS.
2638 * Identify the appropriate session, inode, and call the right handler
2639 * based on the cap op.
2641 void ceph_handle_caps(struct ceph_mds_session
*session
,
2642 struct ceph_msg
*msg
)
2644 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2645 struct super_block
*sb
= mdsc
->client
->sb
;
2646 struct inode
*inode
;
2647 struct ceph_cap
*cap
;
2648 struct ceph_mds_caps
*h
;
2649 int mds
= session
->s_mds
;
2652 struct ceph_vino vino
;
2658 dout("handle_caps from mds%d\n", mds
);
2661 tid
= le64_to_cpu(msg
->hdr
.tid
);
2662 if (msg
->front
.iov_len
< sizeof(*h
))
2664 h
= msg
->front
.iov_base
;
2666 op
= le32_to_cpu(h
->op
);
2667 vino
.ino
= le64_to_cpu(h
->ino
);
2668 vino
.snap
= CEPH_NOSNAP
;
2669 cap_id
= le64_to_cpu(h
->cap_id
);
2670 seq
= le32_to_cpu(h
->seq
);
2671 size
= le64_to_cpu(h
->size
);
2672 max_size
= le64_to_cpu(h
->max_size
);
2674 mutex_lock(&session
->s_mutex
);
2676 dout(" mds%d seq %lld cap seq %u\n", session
->s_mds
, session
->s_seq
,
2680 inode
= ceph_find_inode(sb
, vino
);
2681 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op
), vino
.ino
,
2684 dout(" i don't have ino %llx\n", vino
.ino
);
2688 /* these will work even if we don't have a cap yet */
2690 case CEPH_CAP_OP_FLUSHSNAP_ACK
:
2691 handle_cap_flushsnap_ack(inode
, tid
, h
, session
);
2694 case CEPH_CAP_OP_EXPORT
:
2695 handle_cap_export(inode
, h
, session
);
2698 case CEPH_CAP_OP_IMPORT
:
2699 handle_cap_import(mdsc
, inode
, h
, session
,
2700 snaptrace
, le32_to_cpu(h
->snap_trace_len
));
2701 ceph_check_caps(ceph_inode(inode
), CHECK_CAPS_NODELAY
,
2706 /* the rest require a cap */
2707 spin_lock(&inode
->i_lock
);
2708 cap
= __get_cap_for_mds(ceph_inode(inode
), mds
);
2710 dout("no cap on %p ino %llx.%llx from mds%d, releasing\n",
2711 inode
, ceph_ino(inode
), ceph_snap(inode
), mds
);
2712 spin_unlock(&inode
->i_lock
);
2716 /* note that each of these drops i_lock for us */
2718 case CEPH_CAP_OP_REVOKE
:
2719 case CEPH_CAP_OP_GRANT
:
2720 handle_cap_grant(inode
, h
, session
, cap
, msg
->middle
);
2723 case CEPH_CAP_OP_FLUSH_ACK
:
2724 handle_cap_flush_ack(inode
, tid
, h
, session
, cap
);
2727 case CEPH_CAP_OP_TRUNC
:
2728 handle_cap_trunc(inode
, h
, session
);
2732 spin_unlock(&inode
->i_lock
);
2733 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op
,
2734 ceph_cap_op_name(op
));
2738 mutex_unlock(&session
->s_mutex
);
2745 pr_err("ceph_handle_caps: corrupt message\n");
2751 * Delayed work handler to process end of delayed cap release LRU list.
2753 void ceph_check_delayed_caps(struct ceph_mds_client
*mdsc
)
2755 struct ceph_inode_info
*ci
;
2756 int flags
= CHECK_CAPS_NODELAY
;
2758 dout("check_delayed_caps\n");
2760 spin_lock(&mdsc
->cap_delay_lock
);
2761 if (list_empty(&mdsc
->cap_delay_list
))
2763 ci
= list_first_entry(&mdsc
->cap_delay_list
,
2764 struct ceph_inode_info
,
2766 if ((ci
->i_ceph_flags
& CEPH_I_FLUSH
) == 0 &&
2767 time_before(jiffies
, ci
->i_hold_caps_max
))
2769 list_del_init(&ci
->i_cap_delay_list
);
2770 spin_unlock(&mdsc
->cap_delay_lock
);
2771 dout("check_delayed_caps on %p\n", &ci
->vfs_inode
);
2772 ceph_check_caps(ci
, flags
, NULL
);
2774 spin_unlock(&mdsc
->cap_delay_lock
);
2778 * Flush all dirty caps to the mds
2780 void ceph_flush_dirty_caps(struct ceph_mds_client
*mdsc
)
2782 struct ceph_inode_info
*ci
, *nci
= NULL
;
2783 struct inode
*inode
, *ninode
= NULL
;
2784 struct list_head
*p
, *n
;
2786 dout("flush_dirty_caps\n");
2787 spin_lock(&mdsc
->cap_dirty_lock
);
2788 list_for_each_safe(p
, n
, &mdsc
->cap_dirty
) {
2792 ci
->i_ceph_flags
&= ~CEPH_I_NOFLUSH
;
2793 dout("flush_dirty_caps inode %p (was next inode)\n",
2796 ci
= list_entry(p
, struct ceph_inode_info
,
2798 inode
= igrab(&ci
->vfs_inode
);
2800 dout("flush_dirty_caps inode %p\n", inode
);
2802 if (n
!= &mdsc
->cap_dirty
) {
2803 nci
= list_entry(n
, struct ceph_inode_info
,
2805 ninode
= igrab(&nci
->vfs_inode
);
2807 nci
->i_ceph_flags
|= CEPH_I_NOFLUSH
;
2808 dout("flush_dirty_caps next inode %p, noflush\n",
2814 spin_unlock(&mdsc
->cap_dirty_lock
);
2816 ceph_check_caps(ci
, CHECK_CAPS_NODELAY
|CHECK_CAPS_FLUSH
,
2820 spin_lock(&mdsc
->cap_dirty_lock
);
2822 spin_unlock(&mdsc
->cap_dirty_lock
);
2826 * Drop open file reference. If we were the last open file,
2827 * we may need to release capabilities to the MDS (or schedule
2828 * their delayed release).
2830 void ceph_put_fmode(struct ceph_inode_info
*ci
, int fmode
)
2832 struct inode
*inode
= &ci
->vfs_inode
;
2835 spin_lock(&inode
->i_lock
);
2836 dout("put_fmode %p fmode %d %d -> %d\n", inode
, fmode
,
2837 ci
->i_nr_by_mode
[fmode
], ci
->i_nr_by_mode
[fmode
]-1);
2838 BUG_ON(ci
->i_nr_by_mode
[fmode
] == 0);
2839 if (--ci
->i_nr_by_mode
[fmode
] == 0)
2841 spin_unlock(&inode
->i_lock
);
2843 if (last
&& ci
->i_vino
.snap
== CEPH_NOSNAP
)
2844 ceph_check_caps(ci
, 0, NULL
);
2848 * Helpers for embedding cap and dentry lease releases into mds
2851 * @force is used by dentry_release (below) to force inclusion of a
2852 * record for the directory inode, even when there aren't any caps to
2855 int ceph_encode_inode_release(void **p
, struct inode
*inode
,
2856 int mds
, int drop
, int unless
, int force
)
2858 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2859 struct ceph_cap
*cap
;
2860 struct ceph_mds_request_release
*rel
= *p
;
2864 spin_lock(&inode
->i_lock
);
2865 used
= __ceph_caps_used(ci
);
2867 dout("encode_inode_release %p mds%d used %s drop %s unless %s\n", inode
,
2868 mds
, ceph_cap_string(used
), ceph_cap_string(drop
),
2869 ceph_cap_string(unless
));
2871 /* only drop unused caps */
2874 cap
= __get_cap_for_mds(ci
, mds
);
2875 if (cap
&& __cap_is_valid(cap
)) {
2877 ((cap
->issued
& drop
) &&
2878 (cap
->issued
& unless
) == 0)) {
2879 if ((cap
->issued
& drop
) &&
2880 (cap
->issued
& unless
) == 0) {
2881 dout("encode_inode_release %p cap %p %s -> "
2883 ceph_cap_string(cap
->issued
),
2884 ceph_cap_string(cap
->issued
& ~drop
));
2885 cap
->issued
&= ~drop
;
2886 cap
->implemented
&= ~drop
;
2887 if (ci
->i_ceph_flags
& CEPH_I_NODELAY
) {
2888 int wanted
= __ceph_caps_wanted(ci
);
2889 dout(" wanted %s -> %s (act %s)\n",
2890 ceph_cap_string(cap
->mds_wanted
),
2891 ceph_cap_string(cap
->mds_wanted
&
2893 ceph_cap_string(wanted
));
2894 cap
->mds_wanted
&= wanted
;
2897 dout("encode_inode_release %p cap %p %s"
2898 " (force)\n", inode
, cap
,
2899 ceph_cap_string(cap
->issued
));
2902 rel
->ino
= cpu_to_le64(ceph_ino(inode
));
2903 rel
->cap_id
= cpu_to_le64(cap
->cap_id
);
2904 rel
->seq
= cpu_to_le32(cap
->seq
);
2905 rel
->issue_seq
= cpu_to_le32(cap
->issue_seq
),
2906 rel
->mseq
= cpu_to_le32(cap
->mseq
);
2907 rel
->caps
= cpu_to_le32(cap
->issued
);
2908 rel
->wanted
= cpu_to_le32(cap
->mds_wanted
);
2914 dout("encode_inode_release %p cap %p %s\n",
2915 inode
, cap
, ceph_cap_string(cap
->issued
));
2918 spin_unlock(&inode
->i_lock
);
2922 int ceph_encode_dentry_release(void **p
, struct dentry
*dentry
,
2923 int mds
, int drop
, int unless
)
2925 struct inode
*dir
= dentry
->d_parent
->d_inode
;
2926 struct ceph_mds_request_release
*rel
= *p
;
2927 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
2932 * force an record for the directory caps if we have a dentry lease.
2933 * this is racy (can't take i_lock and d_lock together), but it
2934 * doesn't have to be perfect; the mds will revoke anything we don't
2937 spin_lock(&dentry
->d_lock
);
2938 if (di
->lease_session
&& di
->lease_session
->s_mds
== mds
)
2940 spin_unlock(&dentry
->d_lock
);
2942 ret
= ceph_encode_inode_release(p
, dir
, mds
, drop
, unless
, force
);
2944 spin_lock(&dentry
->d_lock
);
2945 if (ret
&& di
->lease_session
&& di
->lease_session
->s_mds
== mds
) {
2946 dout("encode_dentry_release %p mds%d seq %d\n",
2947 dentry
, mds
, (int)di
->lease_seq
);
2948 rel
->dname_len
= cpu_to_le32(dentry
->d_name
.len
);
2949 memcpy(*p
, dentry
->d_name
.name
, dentry
->d_name
.len
);
2950 *p
+= dentry
->d_name
.len
;
2951 rel
->dname_seq
= cpu_to_le32(di
->lease_seq
);
2953 spin_unlock(&dentry
->d_lock
);