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
);
116 void ceph_caps_init(struct ceph_mds_client
*mdsc
)
118 INIT_LIST_HEAD(&mdsc
->caps_list
);
119 spin_lock_init(&mdsc
->caps_list_lock
);
122 void ceph_caps_finalize(struct ceph_mds_client
*mdsc
)
124 struct ceph_cap
*cap
;
126 spin_lock(&mdsc
->caps_list_lock
);
127 while (!list_empty(&mdsc
->caps_list
)) {
128 cap
= list_first_entry(&mdsc
->caps_list
,
129 struct ceph_cap
, caps_item
);
130 list_del(&cap
->caps_item
);
131 kmem_cache_free(ceph_cap_cachep
, cap
);
133 mdsc
->caps_total_count
= 0;
134 mdsc
->caps_avail_count
= 0;
135 mdsc
->caps_use_count
= 0;
136 mdsc
->caps_reserve_count
= 0;
137 mdsc
->caps_min_count
= 0;
138 spin_unlock(&mdsc
->caps_list_lock
);
141 void ceph_adjust_min_caps(struct ceph_mds_client
*mdsc
, int delta
)
143 spin_lock(&mdsc
->caps_list_lock
);
144 mdsc
->caps_min_count
+= delta
;
145 BUG_ON(mdsc
->caps_min_count
< 0);
146 spin_unlock(&mdsc
->caps_list_lock
);
149 int ceph_reserve_caps(struct ceph_mds_client
*mdsc
,
150 struct ceph_cap_reservation
*ctx
, int need
)
153 struct ceph_cap
*cap
;
159 dout("reserve caps ctx=%p need=%d\n", ctx
, need
);
161 /* first reserve any caps that are already allocated */
162 spin_lock(&mdsc
->caps_list_lock
);
163 if (mdsc
->caps_avail_count
>= need
)
166 have
= mdsc
->caps_avail_count
;
167 mdsc
->caps_avail_count
-= have
;
168 mdsc
->caps_reserve_count
+= have
;
169 BUG_ON(mdsc
->caps_total_count
!= mdsc
->caps_use_count
+
170 mdsc
->caps_reserve_count
+
171 mdsc
->caps_avail_count
);
172 spin_unlock(&mdsc
->caps_list_lock
);
174 for (i
= have
; i
< need
; i
++) {
175 cap
= kmem_cache_alloc(ceph_cap_cachep
, GFP_NOFS
);
178 goto out_alloc_count
;
180 list_add(&cap
->caps_item
, &newcaps
);
183 BUG_ON(have
+ alloc
!= need
);
185 spin_lock(&mdsc
->caps_list_lock
);
186 mdsc
->caps_total_count
+= alloc
;
187 mdsc
->caps_reserve_count
+= alloc
;
188 list_splice(&newcaps
, &mdsc
->caps_list
);
190 BUG_ON(mdsc
->caps_total_count
!= mdsc
->caps_use_count
+
191 mdsc
->caps_reserve_count
+
192 mdsc
->caps_avail_count
);
193 spin_unlock(&mdsc
->caps_list_lock
);
196 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
197 ctx
, mdsc
->caps_total_count
, mdsc
->caps_use_count
,
198 mdsc
->caps_reserve_count
, mdsc
->caps_avail_count
);
202 /* we didn't manage to reserve as much as we needed */
203 pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
208 int ceph_unreserve_caps(struct ceph_mds_client
*mdsc
,
209 struct ceph_cap_reservation
*ctx
)
211 dout("unreserve caps ctx=%p count=%d\n", ctx
, ctx
->count
);
213 spin_lock(&mdsc
->caps_list_lock
);
214 BUG_ON(mdsc
->caps_reserve_count
< ctx
->count
);
215 mdsc
->caps_reserve_count
-= ctx
->count
;
216 mdsc
->caps_avail_count
+= ctx
->count
;
218 dout("unreserve caps %d = %d used + %d resv + %d avail\n",
219 mdsc
->caps_total_count
, mdsc
->caps_use_count
,
220 mdsc
->caps_reserve_count
, mdsc
->caps_avail_count
);
221 BUG_ON(mdsc
->caps_total_count
!= mdsc
->caps_use_count
+
222 mdsc
->caps_reserve_count
+
223 mdsc
->caps_avail_count
);
224 spin_unlock(&mdsc
->caps_list_lock
);
229 static struct ceph_cap
*get_cap(struct ceph_mds_client
*mdsc
,
230 struct ceph_cap_reservation
*ctx
)
232 struct ceph_cap
*cap
= NULL
;
234 /* temporary, until we do something about cap import/export */
236 cap
= kmem_cache_alloc(ceph_cap_cachep
, GFP_NOFS
);
238 mdsc
->caps_use_count
++;
239 mdsc
->caps_total_count
++;
244 spin_lock(&mdsc
->caps_list_lock
);
245 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
246 ctx
, ctx
->count
, mdsc
->caps_total_count
, mdsc
->caps_use_count
,
247 mdsc
->caps_reserve_count
, mdsc
->caps_avail_count
);
249 BUG_ON(ctx
->count
> mdsc
->caps_reserve_count
);
250 BUG_ON(list_empty(&mdsc
->caps_list
));
253 mdsc
->caps_reserve_count
--;
254 mdsc
->caps_use_count
++;
256 cap
= list_first_entry(&mdsc
->caps_list
, struct ceph_cap
, caps_item
);
257 list_del(&cap
->caps_item
);
259 BUG_ON(mdsc
->caps_total_count
!= mdsc
->caps_use_count
+
260 mdsc
->caps_reserve_count
+ mdsc
->caps_avail_count
);
261 spin_unlock(&mdsc
->caps_list_lock
);
265 void ceph_put_cap(struct ceph_mds_client
*mdsc
, struct ceph_cap
*cap
)
267 spin_lock(&mdsc
->caps_list_lock
);
268 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
269 cap
, mdsc
->caps_total_count
, mdsc
->caps_use_count
,
270 mdsc
->caps_reserve_count
, mdsc
->caps_avail_count
);
271 mdsc
->caps_use_count
--;
273 * Keep some preallocated caps around (ceph_min_count), to
274 * avoid lots of free/alloc churn.
276 if (mdsc
->caps_avail_count
>= mdsc
->caps_reserve_count
+
277 mdsc
->caps_min_count
) {
278 mdsc
->caps_total_count
--;
279 kmem_cache_free(ceph_cap_cachep
, cap
);
281 mdsc
->caps_avail_count
++;
282 list_add(&cap
->caps_item
, &mdsc
->caps_list
);
285 BUG_ON(mdsc
->caps_total_count
!= mdsc
->caps_use_count
+
286 mdsc
->caps_reserve_count
+ mdsc
->caps_avail_count
);
287 spin_unlock(&mdsc
->caps_list_lock
);
290 void ceph_reservation_status(struct ceph_client
*client
,
291 int *total
, int *avail
, int *used
, int *reserved
,
294 struct ceph_mds_client
*mdsc
= &client
->mdsc
;
297 *total
= mdsc
->caps_total_count
;
299 *avail
= mdsc
->caps_avail_count
;
301 *used
= mdsc
->caps_use_count
;
303 *reserved
= mdsc
->caps_reserve_count
;
305 *min
= mdsc
->caps_min_count
;
309 * Find ceph_cap for given mds, if any.
311 * Called with i_lock held.
313 static struct ceph_cap
*__get_cap_for_mds(struct ceph_inode_info
*ci
, int mds
)
315 struct ceph_cap
*cap
;
316 struct rb_node
*n
= ci
->i_caps
.rb_node
;
319 cap
= rb_entry(n
, struct ceph_cap
, ci_node
);
322 else if (mds
> cap
->mds
)
331 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
333 static int __ceph_get_cap_mds(struct ceph_inode_info
*ci
)
335 struct ceph_cap
*cap
;
339 /* prefer mds with WR|BUFFER|EXCL caps */
340 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
341 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
343 if (cap
->issued
& (CEPH_CAP_FILE_WR
|
344 CEPH_CAP_FILE_BUFFER
|
351 int ceph_get_cap_mds(struct inode
*inode
)
354 spin_lock(&inode
->i_lock
);
355 mds
= __ceph_get_cap_mds(ceph_inode(inode
));
356 spin_unlock(&inode
->i_lock
);
361 * Called under i_lock.
363 static void __insert_cap_node(struct ceph_inode_info
*ci
,
364 struct ceph_cap
*new)
366 struct rb_node
**p
= &ci
->i_caps
.rb_node
;
367 struct rb_node
*parent
= NULL
;
368 struct ceph_cap
*cap
= NULL
;
372 cap
= rb_entry(parent
, struct ceph_cap
, ci_node
);
373 if (new->mds
< cap
->mds
)
375 else if (new->mds
> cap
->mds
)
381 rb_link_node(&new->ci_node
, parent
, p
);
382 rb_insert_color(&new->ci_node
, &ci
->i_caps
);
386 * (re)set cap hold timeouts, which control the delayed release
387 * of unused caps back to the MDS. Should be called on cap use.
389 static void __cap_set_timeouts(struct ceph_mds_client
*mdsc
,
390 struct ceph_inode_info
*ci
)
392 struct ceph_mount_args
*ma
= mdsc
->client
->mount_args
;
394 ci
->i_hold_caps_min
= round_jiffies(jiffies
+
395 ma
->caps_wanted_delay_min
* HZ
);
396 ci
->i_hold_caps_max
= round_jiffies(jiffies
+
397 ma
->caps_wanted_delay_max
* HZ
);
398 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci
->vfs_inode
,
399 ci
->i_hold_caps_min
- jiffies
, ci
->i_hold_caps_max
- jiffies
);
403 * (Re)queue cap at the end of the delayed cap release list.
405 * If I_FLUSH is set, leave the inode at the front of the list.
407 * Caller holds i_lock
408 * -> we take mdsc->cap_delay_lock
410 static void __cap_delay_requeue(struct ceph_mds_client
*mdsc
,
411 struct ceph_inode_info
*ci
)
413 __cap_set_timeouts(mdsc
, ci
);
414 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci
->vfs_inode
,
415 ci
->i_ceph_flags
, ci
->i_hold_caps_max
);
416 if (!mdsc
->stopping
) {
417 spin_lock(&mdsc
->cap_delay_lock
);
418 if (!list_empty(&ci
->i_cap_delay_list
)) {
419 if (ci
->i_ceph_flags
& CEPH_I_FLUSH
)
421 list_del_init(&ci
->i_cap_delay_list
);
423 list_add_tail(&ci
->i_cap_delay_list
, &mdsc
->cap_delay_list
);
425 spin_unlock(&mdsc
->cap_delay_lock
);
430 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
431 * indicating we should send a cap message to flush dirty metadata
432 * asap, and move to the front of the delayed cap list.
434 static void __cap_delay_requeue_front(struct ceph_mds_client
*mdsc
,
435 struct ceph_inode_info
*ci
)
437 dout("__cap_delay_requeue_front %p\n", &ci
->vfs_inode
);
438 spin_lock(&mdsc
->cap_delay_lock
);
439 ci
->i_ceph_flags
|= CEPH_I_FLUSH
;
440 if (!list_empty(&ci
->i_cap_delay_list
))
441 list_del_init(&ci
->i_cap_delay_list
);
442 list_add(&ci
->i_cap_delay_list
, &mdsc
->cap_delay_list
);
443 spin_unlock(&mdsc
->cap_delay_lock
);
447 * Cancel delayed work on cap.
449 * Caller must hold i_lock.
451 static void __cap_delay_cancel(struct ceph_mds_client
*mdsc
,
452 struct ceph_inode_info
*ci
)
454 dout("__cap_delay_cancel %p\n", &ci
->vfs_inode
);
455 if (list_empty(&ci
->i_cap_delay_list
))
457 spin_lock(&mdsc
->cap_delay_lock
);
458 list_del_init(&ci
->i_cap_delay_list
);
459 spin_unlock(&mdsc
->cap_delay_lock
);
463 * Common issue checks for add_cap, handle_cap_grant.
465 static void __check_cap_issue(struct ceph_inode_info
*ci
, struct ceph_cap
*cap
,
468 unsigned had
= __ceph_caps_issued(ci
, NULL
);
471 * Each time we receive FILE_CACHE anew, we increment
474 if ((issued
& (CEPH_CAP_FILE_CACHE
|CEPH_CAP_FILE_LAZYIO
)) &&
475 (had
& (CEPH_CAP_FILE_CACHE
|CEPH_CAP_FILE_LAZYIO
)) == 0)
479 * if we are newly issued FILE_SHARED, clear I_COMPLETE; we
480 * don't know what happened to this directory while we didn't
483 if ((issued
& CEPH_CAP_FILE_SHARED
) &&
484 (had
& CEPH_CAP_FILE_SHARED
) == 0) {
486 if (S_ISDIR(ci
->vfs_inode
.i_mode
)) {
487 dout(" marking %p NOT complete\n", &ci
->vfs_inode
);
488 ci
->i_ceph_flags
&= ~CEPH_I_COMPLETE
;
494 * Add a capability under the given MDS session.
496 * Caller should hold session snap_rwsem (read) and s_mutex.
498 * @fmode is the open file mode, if we are opening a file, otherwise
499 * it is < 0. (This is so we can atomically add the cap and add an
500 * open file reference to it.)
502 int ceph_add_cap(struct inode
*inode
,
503 struct ceph_mds_session
*session
, u64 cap_id
,
504 int fmode
, unsigned issued
, unsigned wanted
,
505 unsigned seq
, unsigned mseq
, u64 realmino
, int flags
,
506 struct ceph_cap_reservation
*caps_reservation
)
508 struct ceph_mds_client
*mdsc
= &ceph_inode_to_client(inode
)->mdsc
;
509 struct ceph_inode_info
*ci
= ceph_inode(inode
);
510 struct ceph_cap
*new_cap
= NULL
;
511 struct ceph_cap
*cap
;
512 int mds
= session
->s_mds
;
515 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode
,
516 session
->s_mds
, cap_id
, ceph_cap_string(issued
), seq
);
519 * If we are opening the file, include file mode wanted bits
523 wanted
|= ceph_caps_for_mode(fmode
);
526 spin_lock(&inode
->i_lock
);
527 cap
= __get_cap_for_mds(ci
, mds
);
533 spin_unlock(&inode
->i_lock
);
534 new_cap
= get_cap(mdsc
, caps_reservation
);
541 cap
->implemented
= 0;
546 __insert_cap_node(ci
, cap
);
548 /* clear out old exporting info? (i.e. on cap import) */
549 if (ci
->i_cap_exporting_mds
== mds
) {
550 ci
->i_cap_exporting_issued
= 0;
551 ci
->i_cap_exporting_mseq
= 0;
552 ci
->i_cap_exporting_mds
= -1;
555 /* add to session cap list */
556 cap
->session
= session
;
557 spin_lock(&session
->s_cap_lock
);
558 list_add_tail(&cap
->session_caps
, &session
->s_caps
);
559 session
->s_nr_caps
++;
560 spin_unlock(&session
->s_cap_lock
);
563 if (!ci
->i_snap_realm
) {
565 * add this inode to the appropriate snap realm
567 struct ceph_snap_realm
*realm
= ceph_lookup_snap_realm(mdsc
,
570 ceph_get_snap_realm(mdsc
, realm
);
571 spin_lock(&realm
->inodes_with_caps_lock
);
572 ci
->i_snap_realm
= realm
;
573 list_add(&ci
->i_snap_realm_item
,
574 &realm
->inodes_with_caps
);
575 spin_unlock(&realm
->inodes_with_caps_lock
);
577 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
582 __check_cap_issue(ci
, cap
, issued
);
585 * If we are issued caps we don't want, or the mds' wanted
586 * value appears to be off, queue a check so we'll release
587 * later and/or update the mds wanted value.
589 actual_wanted
= __ceph_caps_wanted(ci
);
590 if ((wanted
& ~actual_wanted
) ||
591 (issued
& ~actual_wanted
& CEPH_CAP_ANY_WR
)) {
592 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
593 ceph_cap_string(issued
), ceph_cap_string(wanted
),
594 ceph_cap_string(actual_wanted
));
595 __cap_delay_requeue(mdsc
, ci
);
598 if (flags
& CEPH_CAP_FLAG_AUTH
)
599 ci
->i_auth_cap
= cap
;
600 else if (ci
->i_auth_cap
== cap
)
601 ci
->i_auth_cap
= NULL
;
603 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
604 inode
, ceph_vinop(inode
), cap
, ceph_cap_string(issued
),
605 ceph_cap_string(issued
|cap
->issued
), seq
, mds
);
606 cap
->cap_id
= cap_id
;
607 cap
->issued
= issued
;
608 cap
->implemented
|= issued
;
609 cap
->mds_wanted
|= wanted
;
611 cap
->issue_seq
= seq
;
613 cap
->cap_gen
= session
->s_cap_gen
;
616 __ceph_get_fmode(ci
, fmode
);
617 spin_unlock(&inode
->i_lock
);
618 wake_up_all(&ci
->i_cap_wq
);
623 * Return true if cap has not timed out and belongs to the current
624 * generation of the MDS session (i.e. has not gone 'stale' due to
625 * us losing touch with the mds).
627 static int __cap_is_valid(struct ceph_cap
*cap
)
632 spin_lock(&cap
->session
->s_cap_lock
);
633 gen
= cap
->session
->s_cap_gen
;
634 ttl
= cap
->session
->s_cap_ttl
;
635 spin_unlock(&cap
->session
->s_cap_lock
);
637 if (cap
->cap_gen
< gen
|| time_after_eq(jiffies
, ttl
)) {
638 dout("__cap_is_valid %p cap %p issued %s "
639 "but STALE (gen %u vs %u)\n", &cap
->ci
->vfs_inode
,
640 cap
, ceph_cap_string(cap
->issued
), cap
->cap_gen
, gen
);
648 * Return set of valid cap bits issued to us. Note that caps time
649 * out, and may be invalidated in bulk if the client session times out
650 * and session->s_cap_gen is bumped.
652 int __ceph_caps_issued(struct ceph_inode_info
*ci
, int *implemented
)
654 int have
= ci
->i_snap_caps
| ci
->i_cap_exporting_issued
;
655 struct ceph_cap
*cap
;
660 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
661 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
662 if (!__cap_is_valid(cap
))
664 dout("__ceph_caps_issued %p cap %p issued %s\n",
665 &ci
->vfs_inode
, cap
, ceph_cap_string(cap
->issued
));
668 *implemented
|= cap
->implemented
;
674 * Get cap bits issued by caps other than @ocap
676 int __ceph_caps_issued_other(struct ceph_inode_info
*ci
, struct ceph_cap
*ocap
)
678 int have
= ci
->i_snap_caps
;
679 struct ceph_cap
*cap
;
682 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
683 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
686 if (!__cap_is_valid(cap
))
694 * Move a cap to the end of the LRU (oldest caps at list head, newest
697 static void __touch_cap(struct ceph_cap
*cap
)
699 struct ceph_mds_session
*s
= cap
->session
;
701 spin_lock(&s
->s_cap_lock
);
702 if (s
->s_cap_iterator
== NULL
) {
703 dout("__touch_cap %p cap %p mds%d\n", &cap
->ci
->vfs_inode
, cap
,
705 list_move_tail(&cap
->session_caps
, &s
->s_caps
);
707 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
708 &cap
->ci
->vfs_inode
, cap
, s
->s_mds
);
710 spin_unlock(&s
->s_cap_lock
);
714 * Check if we hold the given mask. If so, move the cap(s) to the
715 * front of their respective LRUs. (This is the preferred way for
716 * callers to check for caps they want.)
718 int __ceph_caps_issued_mask(struct ceph_inode_info
*ci
, int mask
, int touch
)
720 struct ceph_cap
*cap
;
722 int have
= ci
->i_snap_caps
;
724 if ((have
& mask
) == mask
) {
725 dout("__ceph_caps_issued_mask %p snap issued %s"
726 " (mask %s)\n", &ci
->vfs_inode
,
727 ceph_cap_string(have
),
728 ceph_cap_string(mask
));
732 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
733 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
734 if (!__cap_is_valid(cap
))
736 if ((cap
->issued
& mask
) == mask
) {
737 dout("__ceph_caps_issued_mask %p cap %p issued %s"
738 " (mask %s)\n", &ci
->vfs_inode
, cap
,
739 ceph_cap_string(cap
->issued
),
740 ceph_cap_string(mask
));
746 /* does a combination of caps satisfy mask? */
748 if ((have
& mask
) == mask
) {
749 dout("__ceph_caps_issued_mask %p combo issued %s"
750 " (mask %s)\n", &ci
->vfs_inode
,
751 ceph_cap_string(cap
->issued
),
752 ceph_cap_string(mask
));
756 /* touch this + preceeding caps */
758 for (q
= rb_first(&ci
->i_caps
); q
!= p
;
760 cap
= rb_entry(q
, struct ceph_cap
,
762 if (!__cap_is_valid(cap
))
775 * Return true if mask caps are currently being revoked by an MDS.
777 int ceph_caps_revoking(struct ceph_inode_info
*ci
, int mask
)
779 struct inode
*inode
= &ci
->vfs_inode
;
780 struct ceph_cap
*cap
;
784 spin_lock(&inode
->i_lock
);
785 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
786 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
787 if (__cap_is_valid(cap
) &&
788 (cap
->implemented
& ~cap
->issued
& mask
)) {
793 spin_unlock(&inode
->i_lock
);
794 dout("ceph_caps_revoking %p %s = %d\n", inode
,
795 ceph_cap_string(mask
), ret
);
799 int __ceph_caps_used(struct ceph_inode_info
*ci
)
803 used
|= CEPH_CAP_PIN
;
805 used
|= CEPH_CAP_FILE_RD
;
806 if (ci
->i_rdcache_ref
|| ci
->i_rdcache_gen
)
807 used
|= CEPH_CAP_FILE_CACHE
;
809 used
|= CEPH_CAP_FILE_WR
;
810 if (ci
->i_wrbuffer_ref
)
811 used
|= CEPH_CAP_FILE_BUFFER
;
816 * wanted, by virtue of open file modes
818 int __ceph_caps_file_wanted(struct ceph_inode_info
*ci
)
822 for (mode
= 0; mode
< CEPH_FILE_MODE_NUM
; mode
++)
823 if (ci
->i_nr_by_mode
[mode
])
824 want
|= ceph_caps_for_mode(mode
);
829 * Return caps we have registered with the MDS(s) as 'wanted'.
831 int __ceph_caps_mds_wanted(struct ceph_inode_info
*ci
)
833 struct ceph_cap
*cap
;
837 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
838 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
839 if (!__cap_is_valid(cap
))
841 mds_wanted
|= cap
->mds_wanted
;
847 * called under i_lock
849 static int __ceph_is_any_caps(struct ceph_inode_info
*ci
)
851 return !RB_EMPTY_ROOT(&ci
->i_caps
) || ci
->i_cap_exporting_mds
>= 0;
855 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
857 * caller should hold i_lock.
858 * caller will not hold session s_mutex if called from destroy_inode.
860 void __ceph_remove_cap(struct ceph_cap
*cap
)
862 struct ceph_mds_session
*session
= cap
->session
;
863 struct ceph_inode_info
*ci
= cap
->ci
;
864 struct ceph_mds_client
*mdsc
=
865 &ceph_sb_to_client(ci
->vfs_inode
.i_sb
)->mdsc
;
868 dout("__ceph_remove_cap %p from %p\n", cap
, &ci
->vfs_inode
);
870 /* remove from session list */
871 spin_lock(&session
->s_cap_lock
);
872 if (session
->s_cap_iterator
== cap
) {
873 /* not yet, we are iterating over this very cap */
874 dout("__ceph_remove_cap delaying %p removal from session %p\n",
877 list_del_init(&cap
->session_caps
);
878 session
->s_nr_caps
--;
882 /* protect backpointer with s_cap_lock: see iterate_session_caps */
884 spin_unlock(&session
->s_cap_lock
);
886 /* remove from inode list */
887 rb_erase(&cap
->ci_node
, &ci
->i_caps
);
888 if (ci
->i_auth_cap
== cap
)
889 ci
->i_auth_cap
= NULL
;
892 ceph_put_cap(mdsc
, cap
);
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
), GFP_NOFS
);
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
);
978 static void __queue_cap_release(struct ceph_mds_session
*session
,
979 u64 ino
, u64 cap_id
, u32 migrate_seq
,
982 struct ceph_msg
*msg
;
983 struct ceph_mds_cap_release
*head
;
984 struct ceph_mds_cap_item
*item
;
986 spin_lock(&session
->s_cap_lock
);
987 BUG_ON(!session
->s_num_cap_releases
);
988 msg
= list_first_entry(&session
->s_cap_releases
,
989 struct ceph_msg
, list_head
);
991 dout(" adding %llx release to mds%d msg %p (%d left)\n",
992 ino
, session
->s_mds
, msg
, session
->s_num_cap_releases
);
994 BUG_ON(msg
->front
.iov_len
+ sizeof(*item
) > PAGE_CACHE_SIZE
);
995 head
= msg
->front
.iov_base
;
996 head
->num
= cpu_to_le32(le32_to_cpu(head
->num
) + 1);
997 item
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
998 item
->ino
= cpu_to_le64(ino
);
999 item
->cap_id
= cpu_to_le64(cap_id
);
1000 item
->migrate_seq
= cpu_to_le32(migrate_seq
);
1001 item
->seq
= cpu_to_le32(issue_seq
);
1003 session
->s_num_cap_releases
--;
1005 msg
->front
.iov_len
+= sizeof(*item
);
1006 if (le32_to_cpu(head
->num
) == CEPH_CAPS_PER_RELEASE
) {
1007 dout(" release msg %p full\n", msg
);
1008 list_move_tail(&msg
->list_head
, &session
->s_cap_releases_done
);
1010 dout(" release msg %p at %d/%d (%d)\n", msg
,
1011 (int)le32_to_cpu(head
->num
),
1012 (int)CEPH_CAPS_PER_RELEASE
,
1013 (int)msg
->front
.iov_len
);
1015 spin_unlock(&session
->s_cap_lock
);
1019 * Queue cap releases when an inode is dropped from our cache. Since
1020 * inode is about to be destroyed, there is no need for i_lock.
1022 void ceph_queue_caps_release(struct inode
*inode
)
1024 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1027 p
= rb_first(&ci
->i_caps
);
1029 struct ceph_cap
*cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
1030 struct ceph_mds_session
*session
= cap
->session
;
1032 __queue_cap_release(session
, ceph_ino(inode
), cap
->cap_id
,
1033 cap
->mseq
, cap
->issue_seq
);
1035 __ceph_remove_cap(cap
);
1040 * Send a cap msg on the given inode. Update our caps state, then
1041 * drop i_lock and send the message.
1043 * Make note of max_size reported/requested from mds, revoked caps
1044 * that have now been implemented.
1046 * Make half-hearted attempt ot to invalidate page cache if we are
1047 * dropping RDCACHE. Note that this will leave behind locked pages
1048 * that we'll then need to deal with elsewhere.
1050 * Return non-zero if delayed release, or we experienced an error
1051 * such that the caller should requeue + retry later.
1053 * called with i_lock, then drops it.
1054 * caller should hold snap_rwsem (read), s_mutex.
1056 static int __send_cap(struct ceph_mds_client
*mdsc
, struct ceph_cap
*cap
,
1057 int op
, int used
, int want
, int retain
, int flushing
,
1058 unsigned *pflush_tid
)
1059 __releases(cap
->ci
->vfs_inode
->i_lock
)
1061 struct ceph_inode_info
*ci
= cap
->ci
;
1062 struct inode
*inode
= &ci
->vfs_inode
;
1063 u64 cap_id
= cap
->cap_id
;
1064 int held
, revoking
, dropping
, keep
;
1065 u64 seq
, issue_seq
, mseq
, time_warp_seq
, follows
;
1067 struct timespec mtime
, atime
;
1072 struct ceph_mds_session
*session
;
1073 u64 xattr_version
= 0;
1079 held
= cap
->issued
| cap
->implemented
;
1080 revoking
= cap
->implemented
& ~cap
->issued
;
1081 retain
&= ~revoking
;
1082 dropping
= cap
->issued
& ~retain
;
1084 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1085 inode
, cap
, cap
->session
,
1086 ceph_cap_string(held
), ceph_cap_string(held
& retain
),
1087 ceph_cap_string(revoking
));
1088 BUG_ON((retain
& CEPH_CAP_PIN
) == 0);
1090 session
= cap
->session
;
1092 /* don't release wanted unless we've waited a bit. */
1093 if ((ci
->i_ceph_flags
& CEPH_I_NODELAY
) == 0 &&
1094 time_before(jiffies
, ci
->i_hold_caps_min
)) {
1095 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1096 ceph_cap_string(cap
->issued
),
1097 ceph_cap_string(cap
->issued
& retain
),
1098 ceph_cap_string(cap
->mds_wanted
),
1099 ceph_cap_string(want
));
1100 want
|= cap
->mds_wanted
;
1101 retain
|= cap
->issued
;
1104 ci
->i_ceph_flags
&= ~(CEPH_I_NODELAY
| CEPH_I_FLUSH
);
1106 cap
->issued
&= retain
; /* drop bits we don't want */
1107 if (cap
->implemented
& ~cap
->issued
) {
1109 * Wake up any waiters on wanted -> needed transition.
1110 * This is due to the weird transition from buffered
1111 * to sync IO... we need to flush dirty pages _before_
1112 * allowing sync writes to avoid reordering.
1116 cap
->implemented
&= cap
->issued
| used
;
1117 cap
->mds_wanted
= want
;
1121 * assign a tid for flush operations so we can avoid
1122 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1123 * clean type races. track latest tid for every bit
1124 * so we can handle flush AxFw, flush Fw, and have the
1125 * first ack clean Ax.
1127 flush_tid
= ++ci
->i_cap_flush_last_tid
;
1129 *pflush_tid
= flush_tid
;
1130 dout(" cap_flush_tid %d\n", (int)flush_tid
);
1131 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
1132 if (flushing
& (1 << i
))
1133 ci
->i_cap_flush_tid
[i
] = flush_tid
;
1136 keep
= cap
->implemented
;
1138 issue_seq
= cap
->issue_seq
;
1140 size
= inode
->i_size
;
1141 ci
->i_reported_size
= size
;
1142 max_size
= ci
->i_wanted_max_size
;
1143 ci
->i_requested_max_size
= max_size
;
1144 mtime
= inode
->i_mtime
;
1145 atime
= inode
->i_atime
;
1146 time_warp_seq
= ci
->i_time_warp_seq
;
1147 follows
= ci
->i_snap_realm
->cached_context
->seq
;
1150 mode
= inode
->i_mode
;
1152 if (dropping
& CEPH_CAP_XATTR_EXCL
) {
1153 __ceph_build_xattrs_blob(ci
);
1154 xattr_version
= ci
->i_xattrs
.version
+ 1;
1157 spin_unlock(&inode
->i_lock
);
1159 ret
= send_cap_msg(session
, ceph_vino(inode
).ino
, cap_id
,
1160 op
, keep
, want
, flushing
, seq
, flush_tid
, issue_seq
, mseq
,
1161 size
, max_size
, &mtime
, &atime
, time_warp_seq
,
1164 (flushing
& CEPH_CAP_XATTR_EXCL
) ? ci
->i_xattrs
.blob
: NULL
,
1167 dout("error sending cap msg, must requeue %p\n", inode
);
1172 wake_up_all(&ci
->i_cap_wq
);
1178 * When a snapshot is taken, clients accumulate dirty metadata on
1179 * inodes with capabilities in ceph_cap_snaps to describe the file
1180 * state at the time the snapshot was taken. This must be flushed
1181 * asynchronously back to the MDS once sync writes complete and dirty
1182 * data is written out.
1184 * Called under i_lock. Takes s_mutex as needed.
1186 void __ceph_flush_snaps(struct ceph_inode_info
*ci
,
1187 struct ceph_mds_session
**psession
)
1188 __releases(ci
->vfs_inode
->i_lock
)
1189 __acquires(ci
->vfs_inode
->i_lock
)
1191 struct inode
*inode
= &ci
->vfs_inode
;
1193 struct ceph_cap_snap
*capsnap
;
1195 struct ceph_mds_client
*mdsc
= &ceph_inode_to_client(inode
)->mdsc
;
1196 struct ceph_mds_session
*session
= NULL
; /* if session != NULL, we hold
1198 u64 next_follows
= 0; /* keep track of how far we've gotten through the
1199 i_cap_snaps list, and skip these entries next time
1200 around to avoid an infinite loop */
1203 session
= *psession
;
1205 dout("__flush_snaps %p\n", inode
);
1207 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
1208 /* avoid an infiniute loop after retry */
1209 if (capsnap
->follows
< next_follows
)
1212 * we need to wait for sync writes to complete and for dirty
1213 * pages to be written out.
1215 if (capsnap
->dirty_pages
|| capsnap
->writing
)
1219 * if cap writeback already occurred, we should have dropped
1220 * the capsnap in ceph_put_wrbuffer_cap_refs.
1222 BUG_ON(capsnap
->dirty
== 0);
1224 /* pick mds, take s_mutex */
1225 if (ci
->i_auth_cap
== NULL
) {
1226 dout("no auth cap (migrating?), doing nothing\n");
1229 mds
= ci
->i_auth_cap
->session
->s_mds
;
1230 mseq
= ci
->i_auth_cap
->mseq
;
1232 if (session
&& session
->s_mds
!= mds
) {
1233 dout("oops, wrong session %p mutex\n", session
);
1234 mutex_unlock(&session
->s_mutex
);
1235 ceph_put_mds_session(session
);
1239 spin_unlock(&inode
->i_lock
);
1240 mutex_lock(&mdsc
->mutex
);
1241 session
= __ceph_lookup_mds_session(mdsc
, mds
);
1242 mutex_unlock(&mdsc
->mutex
);
1244 dout("inverting session/ino locks on %p\n",
1246 mutex_lock(&session
->s_mutex
);
1249 * if session == NULL, we raced against a cap
1250 * deletion or migration. retry, and we'll
1251 * get a better @mds value next time.
1253 spin_lock(&inode
->i_lock
);
1257 capsnap
->flush_tid
= ++ci
->i_cap_flush_last_tid
;
1258 atomic_inc(&capsnap
->nref
);
1259 if (!list_empty(&capsnap
->flushing_item
))
1260 list_del_init(&capsnap
->flushing_item
);
1261 list_add_tail(&capsnap
->flushing_item
,
1262 &session
->s_cap_snaps_flushing
);
1263 spin_unlock(&inode
->i_lock
);
1265 dout("flush_snaps %p cap_snap %p follows %lld size %llu\n",
1266 inode
, capsnap
, next_follows
, capsnap
->size
);
1267 send_cap_msg(session
, ceph_vino(inode
).ino
, 0,
1268 CEPH_CAP_OP_FLUSHSNAP
, capsnap
->issued
, 0,
1269 capsnap
->dirty
, 0, capsnap
->flush_tid
, 0, mseq
,
1271 &capsnap
->mtime
, &capsnap
->atime
,
1272 capsnap
->time_warp_seq
,
1273 capsnap
->uid
, capsnap
->gid
, capsnap
->mode
,
1277 next_follows
= capsnap
->follows
+ 1;
1278 ceph_put_cap_snap(capsnap
);
1280 spin_lock(&inode
->i_lock
);
1284 /* we flushed them all; remove this inode from the queue */
1285 spin_lock(&mdsc
->snap_flush_lock
);
1286 list_del_init(&ci
->i_snap_flush_item
);
1287 spin_unlock(&mdsc
->snap_flush_lock
);
1291 *psession
= session
;
1293 mutex_unlock(&session
->s_mutex
);
1294 ceph_put_mds_session(session
);
1298 static void ceph_flush_snaps(struct ceph_inode_info
*ci
)
1300 struct inode
*inode
= &ci
->vfs_inode
;
1302 spin_lock(&inode
->i_lock
);
1303 __ceph_flush_snaps(ci
, NULL
);
1304 spin_unlock(&inode
->i_lock
);
1308 * Mark caps dirty. If inode is newly dirty, add to the global dirty
1311 void __ceph_mark_dirty_caps(struct ceph_inode_info
*ci
, int mask
)
1313 struct ceph_mds_client
*mdsc
=
1314 &ceph_sb_to_client(ci
->vfs_inode
.i_sb
)->mdsc
;
1315 struct inode
*inode
= &ci
->vfs_inode
;
1316 int was
= ci
->i_dirty_caps
;
1319 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci
->vfs_inode
,
1320 ceph_cap_string(mask
), ceph_cap_string(was
),
1321 ceph_cap_string(was
| mask
));
1322 ci
->i_dirty_caps
|= mask
;
1324 dout(" inode %p now dirty\n", &ci
->vfs_inode
);
1325 BUG_ON(!list_empty(&ci
->i_dirty_item
));
1326 spin_lock(&mdsc
->cap_dirty_lock
);
1327 list_add(&ci
->i_dirty_item
, &mdsc
->cap_dirty
);
1328 spin_unlock(&mdsc
->cap_dirty_lock
);
1329 if (ci
->i_flushing_caps
== 0) {
1331 dirty
|= I_DIRTY_SYNC
;
1334 BUG_ON(list_empty(&ci
->i_dirty_item
));
1335 if (((was
| ci
->i_flushing_caps
) & CEPH_CAP_FILE_BUFFER
) &&
1336 (mask
& CEPH_CAP_FILE_BUFFER
))
1337 dirty
|= I_DIRTY_DATASYNC
;
1339 __mark_inode_dirty(inode
, dirty
);
1340 __cap_delay_requeue(mdsc
, ci
);
1344 * Add dirty inode to the flushing list. Assigned a seq number so we
1345 * can wait for caps to flush without starving.
1347 * Called under i_lock.
1349 static int __mark_caps_flushing(struct inode
*inode
,
1350 struct ceph_mds_session
*session
)
1352 struct ceph_mds_client
*mdsc
= &ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1353 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1356 BUG_ON(ci
->i_dirty_caps
== 0);
1357 BUG_ON(list_empty(&ci
->i_dirty_item
));
1359 flushing
= ci
->i_dirty_caps
;
1360 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1361 ceph_cap_string(flushing
),
1362 ceph_cap_string(ci
->i_flushing_caps
),
1363 ceph_cap_string(ci
->i_flushing_caps
| flushing
));
1364 ci
->i_flushing_caps
|= flushing
;
1365 ci
->i_dirty_caps
= 0;
1366 dout(" inode %p now !dirty\n", inode
);
1368 spin_lock(&mdsc
->cap_dirty_lock
);
1369 list_del_init(&ci
->i_dirty_item
);
1371 ci
->i_cap_flush_seq
= ++mdsc
->cap_flush_seq
;
1372 if (list_empty(&ci
->i_flushing_item
)) {
1373 list_add_tail(&ci
->i_flushing_item
, &session
->s_cap_flushing
);
1374 mdsc
->num_cap_flushing
++;
1375 dout(" inode %p now flushing seq %lld\n", inode
,
1376 ci
->i_cap_flush_seq
);
1378 list_move_tail(&ci
->i_flushing_item
, &session
->s_cap_flushing
);
1379 dout(" inode %p now flushing (more) seq %lld\n", inode
,
1380 ci
->i_cap_flush_seq
);
1382 spin_unlock(&mdsc
->cap_dirty_lock
);
1388 * try to invalidate mapping pages without blocking.
1390 static int mapping_is_empty(struct address_space
*mapping
)
1392 struct page
*page
= find_get_page(mapping
, 0);
1401 static int try_nonblocking_invalidate(struct inode
*inode
)
1403 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1404 u32 invalidating_gen
= ci
->i_rdcache_gen
;
1406 spin_unlock(&inode
->i_lock
);
1407 invalidate_mapping_pages(&inode
->i_data
, 0, -1);
1408 spin_lock(&inode
->i_lock
);
1410 if (mapping_is_empty(&inode
->i_data
) &&
1411 invalidating_gen
== ci
->i_rdcache_gen
) {
1413 dout("try_nonblocking_invalidate %p success\n", inode
);
1414 ci
->i_rdcache_gen
= 0;
1415 ci
->i_rdcache_revoking
= 0;
1418 dout("try_nonblocking_invalidate %p failed\n", inode
);
1423 * Swiss army knife function to examine currently used and wanted
1424 * versus held caps. Release, flush, ack revoked caps to mds as
1427 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1428 * cap release further.
1429 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1430 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1433 void ceph_check_caps(struct ceph_inode_info
*ci
, int flags
,
1434 struct ceph_mds_session
*session
)
1436 struct ceph_client
*client
= ceph_inode_to_client(&ci
->vfs_inode
);
1437 struct ceph_mds_client
*mdsc
= &client
->mdsc
;
1438 struct inode
*inode
= &ci
->vfs_inode
;
1439 struct ceph_cap
*cap
;
1440 int file_wanted
, used
;
1441 int took_snap_rwsem
= 0; /* true if mdsc->snap_rwsem held */
1442 int issued
, implemented
, want
, retain
, revoking
, flushing
= 0;
1443 int mds
= -1; /* keep track of how far we've gone through i_caps list
1444 to avoid an infinite loop on retry */
1446 int tried_invalidate
= 0;
1447 int delayed
= 0, sent
= 0, force_requeue
= 0, num
;
1448 int queue_invalidate
= 0;
1449 int is_delayed
= flags
& CHECK_CAPS_NODELAY
;
1451 /* if we are unmounting, flush any unused caps immediately. */
1455 spin_lock(&inode
->i_lock
);
1457 if (ci
->i_ceph_flags
& CEPH_I_FLUSH
)
1458 flags
|= CHECK_CAPS_FLUSH
;
1460 /* flush snaps first time around only */
1461 if (!list_empty(&ci
->i_cap_snaps
))
1462 __ceph_flush_snaps(ci
, &session
);
1465 spin_lock(&inode
->i_lock
);
1467 file_wanted
= __ceph_caps_file_wanted(ci
);
1468 used
= __ceph_caps_used(ci
);
1469 want
= file_wanted
| used
;
1470 issued
= __ceph_caps_issued(ci
, &implemented
);
1471 revoking
= implemented
& ~issued
;
1473 retain
= want
| CEPH_CAP_PIN
;
1474 if (!mdsc
->stopping
&& inode
->i_nlink
> 0) {
1476 retain
|= CEPH_CAP_ANY
; /* be greedy */
1478 retain
|= CEPH_CAP_ANY_SHARED
;
1480 * keep RD only if we didn't have the file open RW,
1481 * because then the mds would revoke it anyway to
1482 * journal max_size=0.
1484 if (ci
->i_max_size
== 0)
1485 retain
|= CEPH_CAP_ANY_RD
;
1489 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1490 " issued %s revoking %s retain %s %s%s%s\n", inode
,
1491 ceph_cap_string(file_wanted
),
1492 ceph_cap_string(used
), ceph_cap_string(ci
->i_dirty_caps
),
1493 ceph_cap_string(ci
->i_flushing_caps
),
1494 ceph_cap_string(issued
), ceph_cap_string(revoking
),
1495 ceph_cap_string(retain
),
1496 (flags
& CHECK_CAPS_AUTHONLY
) ? " AUTHONLY" : "",
1497 (flags
& CHECK_CAPS_NODELAY
) ? " NODELAY" : "",
1498 (flags
& CHECK_CAPS_FLUSH
) ? " FLUSH" : "");
1501 * If we no longer need to hold onto old our caps, and we may
1502 * have cached pages, but don't want them, then try to invalidate.
1503 * If we fail, it's because pages are locked.... try again later.
1505 if ((!is_delayed
|| mdsc
->stopping
) &&
1506 ci
->i_wrbuffer_ref
== 0 && /* no dirty pages... */
1507 ci
->i_rdcache_gen
&& /* may have cached pages */
1508 (file_wanted
== 0 || /* no open files */
1509 (revoking
& (CEPH_CAP_FILE_CACHE
|
1510 CEPH_CAP_FILE_LAZYIO
))) && /* or revoking cache */
1511 !tried_invalidate
) {
1512 dout("check_caps trying to invalidate on %p\n", inode
);
1513 if (try_nonblocking_invalidate(inode
) < 0) {
1514 if (revoking
& (CEPH_CAP_FILE_CACHE
|
1515 CEPH_CAP_FILE_LAZYIO
)) {
1516 dout("check_caps queuing invalidate\n");
1517 queue_invalidate
= 1;
1518 ci
->i_rdcache_revoking
= ci
->i_rdcache_gen
;
1520 dout("check_caps failed to invalidate pages\n");
1521 /* we failed to invalidate pages. check these
1522 caps again later. */
1524 __cap_set_timeouts(mdsc
, ci
);
1527 tried_invalidate
= 1;
1532 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
1533 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
1536 /* avoid looping forever */
1537 if (mds
>= cap
->mds
||
1538 ((flags
& CHECK_CAPS_AUTHONLY
) && cap
!= ci
->i_auth_cap
))
1541 /* NOTE: no side-effects allowed, until we take s_mutex */
1543 revoking
= cap
->implemented
& ~cap
->issued
;
1545 dout(" mds%d revoking %s\n", cap
->mds
,
1546 ceph_cap_string(revoking
));
1548 if (cap
== ci
->i_auth_cap
&&
1549 (cap
->issued
& CEPH_CAP_FILE_WR
)) {
1550 /* request larger max_size from MDS? */
1551 if (ci
->i_wanted_max_size
> ci
->i_max_size
&&
1552 ci
->i_wanted_max_size
> ci
->i_requested_max_size
) {
1553 dout("requesting new max_size\n");
1557 /* approaching file_max? */
1558 if ((inode
->i_size
<< 1) >= ci
->i_max_size
&&
1559 (ci
->i_reported_size
<< 1) < ci
->i_max_size
) {
1560 dout("i_size approaching max_size\n");
1564 /* flush anything dirty? */
1565 if (cap
== ci
->i_auth_cap
&& (flags
& CHECK_CAPS_FLUSH
) &&
1567 dout("flushing dirty caps\n");
1571 /* completed revocation? going down and there are no caps? */
1572 if (revoking
&& (revoking
& used
) == 0) {
1573 dout("completed revocation of %s\n",
1574 ceph_cap_string(cap
->implemented
& ~cap
->issued
));
1578 /* want more caps from mds? */
1579 if (want
& ~(cap
->mds_wanted
| cap
->issued
))
1582 /* things we might delay */
1583 if ((cap
->issued
& ~retain
) == 0 &&
1584 cap
->mds_wanted
== want
)
1585 continue; /* nope, all good */
1591 if ((ci
->i_ceph_flags
& CEPH_I_NODELAY
) == 0 &&
1592 time_before(jiffies
, ci
->i_hold_caps_max
)) {
1593 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1594 ceph_cap_string(cap
->issued
),
1595 ceph_cap_string(cap
->issued
& retain
),
1596 ceph_cap_string(cap
->mds_wanted
),
1597 ceph_cap_string(want
));
1603 if (ci
->i_ceph_flags
& CEPH_I_NOFLUSH
) {
1604 dout(" skipping %p I_NOFLUSH set\n", inode
);
1608 if (session
&& session
!= cap
->session
) {
1609 dout("oops, wrong session %p mutex\n", session
);
1610 mutex_unlock(&session
->s_mutex
);
1614 session
= cap
->session
;
1615 if (mutex_trylock(&session
->s_mutex
) == 0) {
1616 dout("inverting session/ino locks on %p\n",
1618 spin_unlock(&inode
->i_lock
);
1619 if (took_snap_rwsem
) {
1620 up_read(&mdsc
->snap_rwsem
);
1621 took_snap_rwsem
= 0;
1623 mutex_lock(&session
->s_mutex
);
1627 /* take snap_rwsem after session mutex */
1628 if (!took_snap_rwsem
) {
1629 if (down_read_trylock(&mdsc
->snap_rwsem
) == 0) {
1630 dout("inverting snap/in locks on %p\n",
1632 spin_unlock(&inode
->i_lock
);
1633 down_read(&mdsc
->snap_rwsem
);
1634 took_snap_rwsem
= 1;
1637 took_snap_rwsem
= 1;
1640 if (cap
== ci
->i_auth_cap
&& ci
->i_dirty_caps
)
1641 flushing
= __mark_caps_flushing(inode
, session
);
1643 mds
= cap
->mds
; /* remember mds, so we don't repeat */
1646 /* __send_cap drops i_lock */
1647 delayed
+= __send_cap(mdsc
, cap
, CEPH_CAP_OP_UPDATE
, used
, want
,
1648 retain
, flushing
, NULL
);
1649 goto retry
; /* retake i_lock and restart our cap scan. */
1653 * Reschedule delayed caps release if we delayed anything,
1656 if (delayed
&& is_delayed
)
1657 force_requeue
= 1; /* __send_cap delayed release; requeue */
1658 if (!delayed
&& !is_delayed
)
1659 __cap_delay_cancel(mdsc
, ci
);
1660 else if (!is_delayed
|| force_requeue
)
1661 __cap_delay_requeue(mdsc
, ci
);
1663 spin_unlock(&inode
->i_lock
);
1665 if (queue_invalidate
)
1666 ceph_queue_invalidate(inode
);
1669 mutex_unlock(&session
->s_mutex
);
1670 if (took_snap_rwsem
)
1671 up_read(&mdsc
->snap_rwsem
);
1675 * Try to flush dirty caps back to the auth mds.
1677 static int try_flush_caps(struct inode
*inode
, struct ceph_mds_session
*session
,
1678 unsigned *flush_tid
)
1680 struct ceph_mds_client
*mdsc
= &ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1681 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1682 int unlock_session
= session
? 0 : 1;
1686 spin_lock(&inode
->i_lock
);
1687 if (ci
->i_ceph_flags
& CEPH_I_NOFLUSH
) {
1688 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode
);
1691 if (ci
->i_dirty_caps
&& ci
->i_auth_cap
) {
1692 struct ceph_cap
*cap
= ci
->i_auth_cap
;
1693 int used
= __ceph_caps_used(ci
);
1694 int want
= __ceph_caps_wanted(ci
);
1698 spin_unlock(&inode
->i_lock
);
1699 session
= cap
->session
;
1700 mutex_lock(&session
->s_mutex
);
1703 BUG_ON(session
!= cap
->session
);
1704 if (cap
->session
->s_state
< CEPH_MDS_SESSION_OPEN
)
1707 flushing
= __mark_caps_flushing(inode
, session
);
1709 /* __send_cap drops i_lock */
1710 delayed
= __send_cap(mdsc
, cap
, CEPH_CAP_OP_FLUSH
, used
, want
,
1711 cap
->issued
| cap
->implemented
, flushing
,
1716 spin_lock(&inode
->i_lock
);
1717 __cap_delay_requeue(mdsc
, ci
);
1720 spin_unlock(&inode
->i_lock
);
1722 if (session
&& unlock_session
)
1723 mutex_unlock(&session
->s_mutex
);
1728 * Return true if we've flushed caps through the given flush_tid.
1730 static int caps_are_flushed(struct inode
*inode
, unsigned tid
)
1732 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1735 spin_lock(&inode
->i_lock
);
1736 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
1737 if ((ci
->i_flushing_caps
& (1 << i
)) &&
1738 ci
->i_cap_flush_tid
[i
] <= tid
) {
1739 /* still flushing this bit */
1743 spin_unlock(&inode
->i_lock
);
1748 * Wait on any unsafe replies for the given inode. First wait on the
1749 * newest request, and make that the upper bound. Then, if there are
1750 * more requests, keep waiting on the oldest as long as it is still older
1751 * than the original request.
1753 static void sync_write_wait(struct inode
*inode
)
1755 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1756 struct list_head
*head
= &ci
->i_unsafe_writes
;
1757 struct ceph_osd_request
*req
;
1760 spin_lock(&ci
->i_unsafe_lock
);
1761 if (list_empty(head
))
1764 /* set upper bound as _last_ entry in chain */
1765 req
= list_entry(head
->prev
, struct ceph_osd_request
,
1767 last_tid
= req
->r_tid
;
1770 ceph_osdc_get_request(req
);
1771 spin_unlock(&ci
->i_unsafe_lock
);
1772 dout("sync_write_wait on tid %llu (until %llu)\n",
1773 req
->r_tid
, last_tid
);
1774 wait_for_completion(&req
->r_safe_completion
);
1775 spin_lock(&ci
->i_unsafe_lock
);
1776 ceph_osdc_put_request(req
);
1779 * from here on look at first entry in chain, since we
1780 * only want to wait for anything older than last_tid
1782 if (list_empty(head
))
1784 req
= list_entry(head
->next
, struct ceph_osd_request
,
1786 } while (req
->r_tid
< last_tid
);
1788 spin_unlock(&ci
->i_unsafe_lock
);
1791 int ceph_fsync(struct file
*file
, int datasync
)
1793 struct inode
*inode
= file
->f_mapping
->host
;
1794 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1799 dout("fsync %p%s\n", inode
, datasync
? " datasync" : "");
1800 sync_write_wait(inode
);
1802 ret
= filemap_write_and_wait(inode
->i_mapping
);
1806 dirty
= try_flush_caps(inode
, NULL
, &flush_tid
);
1807 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty
));
1810 * only wait on non-file metadata writeback (the mds
1811 * can recover size and mtime, so we don't need to
1814 if (!datasync
&& (dirty
& ~CEPH_CAP_ANY_FILE_WR
)) {
1815 dout("fsync waiting for flush_tid %u\n", flush_tid
);
1816 ret
= wait_event_interruptible(ci
->i_cap_wq
,
1817 caps_are_flushed(inode
, flush_tid
));
1820 dout("fsync %p%s done\n", inode
, datasync
? " datasync" : "");
1825 * Flush any dirty caps back to the mds. If we aren't asked to wait,
1826 * queue inode for flush but don't do so immediately, because we can
1827 * get by with fewer MDS messages if we wait for data writeback to
1830 int ceph_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1832 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1836 int wait
= wbc
->sync_mode
== WB_SYNC_ALL
;
1838 dout("write_inode %p wait=%d\n", inode
, wait
);
1840 dirty
= try_flush_caps(inode
, NULL
, &flush_tid
);
1842 err
= wait_event_interruptible(ci
->i_cap_wq
,
1843 caps_are_flushed(inode
, flush_tid
));
1845 struct ceph_mds_client
*mdsc
=
1846 &ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1848 spin_lock(&inode
->i_lock
);
1849 if (__ceph_caps_dirty(ci
))
1850 __cap_delay_requeue_front(mdsc
, ci
);
1851 spin_unlock(&inode
->i_lock
);
1857 * After a recovering MDS goes active, we need to resend any caps
1860 * Caller holds session->s_mutex.
1862 static void kick_flushing_capsnaps(struct ceph_mds_client
*mdsc
,
1863 struct ceph_mds_session
*session
)
1865 struct ceph_cap_snap
*capsnap
;
1867 dout("kick_flushing_capsnaps mds%d\n", session
->s_mds
);
1868 list_for_each_entry(capsnap
, &session
->s_cap_snaps_flushing
,
1870 struct ceph_inode_info
*ci
= capsnap
->ci
;
1871 struct inode
*inode
= &ci
->vfs_inode
;
1872 struct ceph_cap
*cap
;
1874 spin_lock(&inode
->i_lock
);
1875 cap
= ci
->i_auth_cap
;
1876 if (cap
&& cap
->session
== session
) {
1877 dout("kick_flushing_caps %p cap %p capsnap %p\n", inode
,
1879 __ceph_flush_snaps(ci
, &session
);
1881 pr_err("%p auth cap %p not mds%d ???\n", inode
,
1882 cap
, session
->s_mds
);
1884 spin_unlock(&inode
->i_lock
);
1888 void ceph_kick_flushing_caps(struct ceph_mds_client
*mdsc
,
1889 struct ceph_mds_session
*session
)
1891 struct ceph_inode_info
*ci
;
1893 kick_flushing_capsnaps(mdsc
, session
);
1895 dout("kick_flushing_caps mds%d\n", session
->s_mds
);
1896 list_for_each_entry(ci
, &session
->s_cap_flushing
, i_flushing_item
) {
1897 struct inode
*inode
= &ci
->vfs_inode
;
1898 struct ceph_cap
*cap
;
1901 spin_lock(&inode
->i_lock
);
1902 cap
= ci
->i_auth_cap
;
1903 if (cap
&& cap
->session
== session
) {
1904 dout("kick_flushing_caps %p cap %p %s\n", inode
,
1905 cap
, ceph_cap_string(ci
->i_flushing_caps
));
1906 delayed
= __send_cap(mdsc
, cap
, CEPH_CAP_OP_FLUSH
,
1907 __ceph_caps_used(ci
),
1908 __ceph_caps_wanted(ci
),
1909 cap
->issued
| cap
->implemented
,
1910 ci
->i_flushing_caps
, NULL
);
1912 spin_lock(&inode
->i_lock
);
1913 __cap_delay_requeue(mdsc
, ci
);
1914 spin_unlock(&inode
->i_lock
);
1917 pr_err("%p auth cap %p not mds%d ???\n", inode
,
1918 cap
, session
->s_mds
);
1919 spin_unlock(&inode
->i_lock
);
1926 * Take references to capabilities we hold, so that we don't release
1927 * them to the MDS prematurely.
1929 * Protected by i_lock.
1931 static void __take_cap_refs(struct ceph_inode_info
*ci
, int got
)
1933 if (got
& CEPH_CAP_PIN
)
1935 if (got
& CEPH_CAP_FILE_RD
)
1937 if (got
& CEPH_CAP_FILE_CACHE
)
1938 ci
->i_rdcache_ref
++;
1939 if (got
& CEPH_CAP_FILE_WR
)
1941 if (got
& CEPH_CAP_FILE_BUFFER
) {
1942 if (ci
->i_wrbuffer_ref
== 0)
1943 igrab(&ci
->vfs_inode
);
1944 ci
->i_wrbuffer_ref
++;
1945 dout("__take_cap_refs %p wrbuffer %d -> %d (?)\n",
1946 &ci
->vfs_inode
, ci
->i_wrbuffer_ref
-1, ci
->i_wrbuffer_ref
);
1951 * Try to grab cap references. Specify those refs we @want, and the
1952 * minimal set we @need. Also include the larger offset we are writing
1953 * to (when applicable), and check against max_size here as well.
1954 * Note that caller is responsible for ensuring max_size increases are
1955 * requested from the MDS.
1957 static int try_get_cap_refs(struct ceph_inode_info
*ci
, int need
, int want
,
1958 int *got
, loff_t endoff
, int *check_max
, int *err
)
1960 struct inode
*inode
= &ci
->vfs_inode
;
1962 int have
, implemented
;
1965 dout("get_cap_refs %p need %s want %s\n", inode
,
1966 ceph_cap_string(need
), ceph_cap_string(want
));
1967 spin_lock(&inode
->i_lock
);
1969 /* make sure file is actually open */
1970 file_wanted
= __ceph_caps_file_wanted(ci
);
1971 if ((file_wanted
& need
) == 0) {
1972 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
1973 ceph_cap_string(need
), ceph_cap_string(file_wanted
));
1979 if (need
& CEPH_CAP_FILE_WR
) {
1980 if (endoff
>= 0 && endoff
> (loff_t
)ci
->i_max_size
) {
1981 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
1982 inode
, endoff
, ci
->i_max_size
);
1983 if (endoff
> ci
->i_wanted_max_size
) {
1990 * If a sync write is in progress, we must wait, so that we
1991 * can get a final snapshot value for size+mtime.
1993 if (__ceph_have_pending_cap_snap(ci
)) {
1994 dout("get_cap_refs %p cap_snap_pending\n", inode
);
1998 have
= __ceph_caps_issued(ci
, &implemented
);
2001 * disallow writes while a truncate is pending
2003 if (ci
->i_truncate_pending
)
2004 have
&= ~CEPH_CAP_FILE_WR
;
2006 if ((have
& need
) == need
) {
2008 * Look at (implemented & ~have & not) so that we keep waiting
2009 * on transition from wanted -> needed caps. This is needed
2010 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2011 * going before a prior buffered writeback happens.
2013 int not = want
& ~(have
& need
);
2014 int revoking
= implemented
& ~have
;
2015 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2016 inode
, ceph_cap_string(have
), ceph_cap_string(not),
2017 ceph_cap_string(revoking
));
2018 if ((revoking
& not) == 0) {
2019 *got
= need
| (have
& want
);
2020 __take_cap_refs(ci
, *got
);
2024 dout("get_cap_refs %p have %s needed %s\n", inode
,
2025 ceph_cap_string(have
), ceph_cap_string(need
));
2028 spin_unlock(&inode
->i_lock
);
2029 dout("get_cap_refs %p ret %d got %s\n", inode
,
2030 ret
, ceph_cap_string(*got
));
2035 * Check the offset we are writing up to against our current
2036 * max_size. If necessary, tell the MDS we want to write to
2039 static void check_max_size(struct inode
*inode
, loff_t endoff
)
2041 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2044 /* do we need to explicitly request a larger max_size? */
2045 spin_lock(&inode
->i_lock
);
2046 if ((endoff
>= ci
->i_max_size
||
2047 endoff
> (inode
->i_size
<< 1)) &&
2048 endoff
> ci
->i_wanted_max_size
) {
2049 dout("write %p at large endoff %llu, req max_size\n",
2051 ci
->i_wanted_max_size
= endoff
;
2054 spin_unlock(&inode
->i_lock
);
2056 ceph_check_caps(ci
, CHECK_CAPS_AUTHONLY
, NULL
);
2060 * Wait for caps, and take cap references. If we can't get a WR cap
2061 * due to a small max_size, make sure we check_max_size (and possibly
2062 * ask the mds) so we don't get hung up indefinitely.
2064 int ceph_get_caps(struct ceph_inode_info
*ci
, int need
, int want
, int *got
,
2067 int check_max
, ret
, err
;
2071 check_max_size(&ci
->vfs_inode
, endoff
);
2074 ret
= wait_event_interruptible(ci
->i_cap_wq
,
2075 try_get_cap_refs(ci
, need
, want
,
2086 * Take cap refs. Caller must already know we hold at least one ref
2087 * on the caps in question or we don't know this is safe.
2089 void ceph_get_cap_refs(struct ceph_inode_info
*ci
, int caps
)
2091 spin_lock(&ci
->vfs_inode
.i_lock
);
2092 __take_cap_refs(ci
, caps
);
2093 spin_unlock(&ci
->vfs_inode
.i_lock
);
2099 * If we released the last ref on any given cap, call ceph_check_caps
2100 * to release (or schedule a release).
2102 * If we are releasing a WR cap (from a sync write), finalize any affected
2103 * cap_snap, and wake up any waiters.
2105 void ceph_put_cap_refs(struct ceph_inode_info
*ci
, int had
)
2107 struct inode
*inode
= &ci
->vfs_inode
;
2108 int last
= 0, put
= 0, flushsnaps
= 0, wake
= 0;
2109 struct ceph_cap_snap
*capsnap
;
2111 spin_lock(&inode
->i_lock
);
2112 if (had
& CEPH_CAP_PIN
)
2114 if (had
& CEPH_CAP_FILE_RD
)
2115 if (--ci
->i_rd_ref
== 0)
2117 if (had
& CEPH_CAP_FILE_CACHE
)
2118 if (--ci
->i_rdcache_ref
== 0)
2120 if (had
& CEPH_CAP_FILE_BUFFER
) {
2121 if (--ci
->i_wrbuffer_ref
== 0) {
2125 dout("put_cap_refs %p wrbuffer %d -> %d (?)\n",
2126 inode
, ci
->i_wrbuffer_ref
+1, ci
->i_wrbuffer_ref
);
2128 if (had
& CEPH_CAP_FILE_WR
)
2129 if (--ci
->i_wr_ref
== 0) {
2131 if (!list_empty(&ci
->i_cap_snaps
)) {
2132 capsnap
= list_first_entry(&ci
->i_cap_snaps
,
2133 struct ceph_cap_snap
,
2135 if (capsnap
->writing
) {
2136 capsnap
->writing
= 0;
2138 __ceph_finish_cap_snap(ci
,
2144 spin_unlock(&inode
->i_lock
);
2146 dout("put_cap_refs %p had %s%s%s\n", inode
, ceph_cap_string(had
),
2147 last
? " last" : "", put
? " put" : "");
2149 if (last
&& !flushsnaps
)
2150 ceph_check_caps(ci
, 0, NULL
);
2151 else if (flushsnaps
)
2152 ceph_flush_snaps(ci
);
2154 wake_up_all(&ci
->i_cap_wq
);
2160 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2161 * context. Adjust per-snap dirty page accounting as appropriate.
2162 * Once all dirty data for a cap_snap is flushed, flush snapped file
2163 * metadata back to the MDS. If we dropped the last ref, call
2166 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info
*ci
, int nr
,
2167 struct ceph_snap_context
*snapc
)
2169 struct inode
*inode
= &ci
->vfs_inode
;
2171 int complete_capsnap
= 0;
2172 int drop_capsnap
= 0;
2174 struct ceph_cap_snap
*capsnap
= NULL
;
2176 spin_lock(&inode
->i_lock
);
2177 ci
->i_wrbuffer_ref
-= nr
;
2178 last
= !ci
->i_wrbuffer_ref
;
2180 if (ci
->i_head_snapc
== snapc
) {
2181 ci
->i_wrbuffer_ref_head
-= nr
;
2182 if (!ci
->i_wrbuffer_ref_head
) {
2183 ceph_put_snap_context(ci
->i_head_snapc
);
2184 ci
->i_head_snapc
= NULL
;
2186 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2188 ci
->i_wrbuffer_ref
+nr
, ci
->i_wrbuffer_ref_head
+nr
,
2189 ci
->i_wrbuffer_ref
, ci
->i_wrbuffer_ref_head
,
2190 last
? " LAST" : "");
2192 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
2193 if (capsnap
->context
== snapc
) {
2199 capsnap
->dirty_pages
-= nr
;
2200 if (capsnap
->dirty_pages
== 0) {
2201 complete_capsnap
= 1;
2202 if (capsnap
->dirty
== 0)
2203 /* cap writeback completed before we created
2204 * the cap_snap; no FLUSHSNAP is needed */
2207 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2208 " snap %lld %d/%d -> %d/%d %s%s%s\n",
2209 inode
, capsnap
, capsnap
->context
->seq
,
2210 ci
->i_wrbuffer_ref
+nr
, capsnap
->dirty_pages
+ nr
,
2211 ci
->i_wrbuffer_ref
, capsnap
->dirty_pages
,
2212 last
? " (wrbuffer last)" : "",
2213 complete_capsnap
? " (complete capsnap)" : "",
2214 drop_capsnap
? " (drop capsnap)" : "");
2216 ceph_put_snap_context(capsnap
->context
);
2217 list_del(&capsnap
->ci_item
);
2218 list_del(&capsnap
->flushing_item
);
2219 ceph_put_cap_snap(capsnap
);
2223 spin_unlock(&inode
->i_lock
);
2226 ceph_check_caps(ci
, CHECK_CAPS_AUTHONLY
, NULL
);
2228 } else if (complete_capsnap
) {
2229 ceph_flush_snaps(ci
);
2230 wake_up_all(&ci
->i_cap_wq
);
2237 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2238 * actually be a revocation if it specifies a smaller cap set.)
2240 * caller holds s_mutex and i_lock, we drop both.
2244 * 1 - check_caps on auth cap only (writeback)
2245 * 2 - check_caps (ack revoke)
2247 static void handle_cap_grant(struct inode
*inode
, struct ceph_mds_caps
*grant
,
2248 struct ceph_mds_session
*session
,
2249 struct ceph_cap
*cap
,
2250 struct ceph_buffer
*xattr_buf
)
2251 __releases(inode
->i_lock
)
2253 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2254 int mds
= session
->s_mds
;
2255 int seq
= le32_to_cpu(grant
->seq
);
2256 int newcaps
= le32_to_cpu(grant
->caps
);
2257 int issued
, implemented
, used
, wanted
, dirty
;
2258 u64 size
= le64_to_cpu(grant
->size
);
2259 u64 max_size
= le64_to_cpu(grant
->max_size
);
2260 struct timespec mtime
, atime
, ctime
;
2264 int revoked_rdcache
= 0;
2265 int queue_invalidate
= 0;
2267 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2268 inode
, cap
, mds
, seq
, ceph_cap_string(newcaps
));
2269 dout(" size %llu max_size %llu, i_size %llu\n", size
, max_size
,
2273 * If CACHE is being revoked, and we have no dirty buffers,
2274 * try to invalidate (once). (If there are dirty buffers, we
2275 * will invalidate _after_ writeback.)
2277 if (((cap
->issued
& ~newcaps
) & CEPH_CAP_FILE_CACHE
) &&
2278 (newcaps
& CEPH_CAP_FILE_LAZYIO
) == 0 &&
2279 !ci
->i_wrbuffer_ref
) {
2280 if (try_nonblocking_invalidate(inode
) == 0) {
2281 revoked_rdcache
= 1;
2283 /* there were locked pages.. invalidate later
2284 in a separate thread. */
2285 if (ci
->i_rdcache_revoking
!= ci
->i_rdcache_gen
) {
2286 queue_invalidate
= 1;
2287 ci
->i_rdcache_revoking
= ci
->i_rdcache_gen
;
2292 /* side effects now are allowed */
2294 issued
= __ceph_caps_issued(ci
, &implemented
);
2295 issued
|= implemented
| __ceph_caps_dirty(ci
);
2297 cap
->cap_gen
= session
->s_cap_gen
;
2299 __check_cap_issue(ci
, cap
, newcaps
);
2301 if ((issued
& CEPH_CAP_AUTH_EXCL
) == 0) {
2302 inode
->i_mode
= le32_to_cpu(grant
->mode
);
2303 inode
->i_uid
= le32_to_cpu(grant
->uid
);
2304 inode
->i_gid
= le32_to_cpu(grant
->gid
);
2305 dout("%p mode 0%o uid.gid %d.%d\n", inode
, inode
->i_mode
,
2306 inode
->i_uid
, inode
->i_gid
);
2309 if ((issued
& CEPH_CAP_LINK_EXCL
) == 0)
2310 inode
->i_nlink
= le32_to_cpu(grant
->nlink
);
2312 if ((issued
& CEPH_CAP_XATTR_EXCL
) == 0 && grant
->xattr_len
) {
2313 int len
= le32_to_cpu(grant
->xattr_len
);
2314 u64 version
= le64_to_cpu(grant
->xattr_version
);
2316 if (version
> ci
->i_xattrs
.version
) {
2317 dout(" got new xattrs v%llu on %p len %d\n",
2318 version
, inode
, len
);
2319 if (ci
->i_xattrs
.blob
)
2320 ceph_buffer_put(ci
->i_xattrs
.blob
);
2321 ci
->i_xattrs
.blob
= ceph_buffer_get(xattr_buf
);
2322 ci
->i_xattrs
.version
= version
;
2326 /* size/ctime/mtime/atime? */
2327 ceph_fill_file_size(inode
, issued
,
2328 le32_to_cpu(grant
->truncate_seq
),
2329 le64_to_cpu(grant
->truncate_size
), size
);
2330 ceph_decode_timespec(&mtime
, &grant
->mtime
);
2331 ceph_decode_timespec(&atime
, &grant
->atime
);
2332 ceph_decode_timespec(&ctime
, &grant
->ctime
);
2333 ceph_fill_file_time(inode
, issued
,
2334 le32_to_cpu(grant
->time_warp_seq
), &ctime
, &mtime
,
2337 /* max size increase? */
2338 if (max_size
!= ci
->i_max_size
) {
2339 dout("max_size %lld -> %llu\n", ci
->i_max_size
, max_size
);
2340 ci
->i_max_size
= max_size
;
2341 if (max_size
>= ci
->i_wanted_max_size
) {
2342 ci
->i_wanted_max_size
= 0; /* reset */
2343 ci
->i_requested_max_size
= 0;
2348 /* check cap bits */
2349 wanted
= __ceph_caps_wanted(ci
);
2350 used
= __ceph_caps_used(ci
);
2351 dirty
= __ceph_caps_dirty(ci
);
2352 dout(" my wanted = %s, used = %s, dirty %s\n",
2353 ceph_cap_string(wanted
),
2354 ceph_cap_string(used
),
2355 ceph_cap_string(dirty
));
2356 if (wanted
!= le32_to_cpu(grant
->wanted
)) {
2357 dout("mds wanted %s -> %s\n",
2358 ceph_cap_string(le32_to_cpu(grant
->wanted
)),
2359 ceph_cap_string(wanted
));
2360 grant
->wanted
= cpu_to_le32(wanted
);
2365 /* file layout may have changed */
2366 ci
->i_layout
= grant
->layout
;
2368 /* revocation, grant, or no-op? */
2369 if (cap
->issued
& ~newcaps
) {
2370 int revoking
= cap
->issued
& ~newcaps
;
2372 dout("revocation: %s -> %s (revoking %s)\n",
2373 ceph_cap_string(cap
->issued
),
2374 ceph_cap_string(newcaps
),
2375 ceph_cap_string(revoking
));
2376 if (revoking
& CEPH_CAP_FILE_BUFFER
)
2377 writeback
= 1; /* initiate writeback; will delay ack */
2378 else if (revoking
== CEPH_CAP_FILE_CACHE
&&
2379 (newcaps
& CEPH_CAP_FILE_LAZYIO
) == 0 &&
2381 ; /* do nothing yet, invalidation will be queued */
2382 else if (cap
== ci
->i_auth_cap
)
2383 check_caps
= 1; /* check auth cap only */
2385 check_caps
= 2; /* check all caps */
2386 cap
->issued
= newcaps
;
2387 cap
->implemented
|= newcaps
;
2388 } else if (cap
->issued
== newcaps
) {
2389 dout("caps unchanged: %s -> %s\n",
2390 ceph_cap_string(cap
->issued
), ceph_cap_string(newcaps
));
2392 dout("grant: %s -> %s\n", ceph_cap_string(cap
->issued
),
2393 ceph_cap_string(newcaps
));
2394 cap
->issued
= newcaps
;
2395 cap
->implemented
|= newcaps
; /* add bits only, to
2396 * avoid stepping on a
2397 * pending revocation */
2400 BUG_ON(cap
->issued
& ~cap
->implemented
);
2402 spin_unlock(&inode
->i_lock
);
2405 * queue inode for writeback: we can't actually call
2406 * filemap_write_and_wait, etc. from message handler
2409 ceph_queue_writeback(inode
);
2410 if (queue_invalidate
)
2411 ceph_queue_invalidate(inode
);
2413 wake_up_all(&ci
->i_cap_wq
);
2415 if (check_caps
== 1)
2416 ceph_check_caps(ci
, CHECK_CAPS_NODELAY
|CHECK_CAPS_AUTHONLY
,
2418 else if (check_caps
== 2)
2419 ceph_check_caps(ci
, CHECK_CAPS_NODELAY
, session
);
2421 mutex_unlock(&session
->s_mutex
);
2425 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2426 * MDS has been safely committed.
2428 static void handle_cap_flush_ack(struct inode
*inode
, u64 flush_tid
,
2429 struct ceph_mds_caps
*m
,
2430 struct ceph_mds_session
*session
,
2431 struct ceph_cap
*cap
)
2432 __releases(inode
->i_lock
)
2434 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2435 struct ceph_mds_client
*mdsc
= &ceph_sb_to_client(inode
->i_sb
)->mdsc
;
2436 unsigned seq
= le32_to_cpu(m
->seq
);
2437 int dirty
= le32_to_cpu(m
->dirty
);
2442 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
2443 if ((dirty
& (1 << i
)) &&
2444 flush_tid
== ci
->i_cap_flush_tid
[i
])
2447 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2448 " flushing %s -> %s\n",
2449 inode
, session
->s_mds
, seq
, ceph_cap_string(dirty
),
2450 ceph_cap_string(cleaned
), ceph_cap_string(ci
->i_flushing_caps
),
2451 ceph_cap_string(ci
->i_flushing_caps
& ~cleaned
));
2453 if (ci
->i_flushing_caps
== (ci
->i_flushing_caps
& ~cleaned
))
2456 ci
->i_flushing_caps
&= ~cleaned
;
2458 spin_lock(&mdsc
->cap_dirty_lock
);
2459 if (ci
->i_flushing_caps
== 0) {
2460 list_del_init(&ci
->i_flushing_item
);
2461 if (!list_empty(&session
->s_cap_flushing
))
2462 dout(" mds%d still flushing cap on %p\n",
2464 &list_entry(session
->s_cap_flushing
.next
,
2465 struct ceph_inode_info
,
2466 i_flushing_item
)->vfs_inode
);
2467 mdsc
->num_cap_flushing
--;
2468 wake_up_all(&mdsc
->cap_flushing_wq
);
2469 dout(" inode %p now !flushing\n", inode
);
2471 if (ci
->i_dirty_caps
== 0) {
2472 dout(" inode %p now clean\n", inode
);
2473 BUG_ON(!list_empty(&ci
->i_dirty_item
));
2476 BUG_ON(list_empty(&ci
->i_dirty_item
));
2479 spin_unlock(&mdsc
->cap_dirty_lock
);
2480 wake_up_all(&ci
->i_cap_wq
);
2483 spin_unlock(&inode
->i_lock
);
2489 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
2490 * throw away our cap_snap.
2492 * Caller hold s_mutex.
2494 static void handle_cap_flushsnap_ack(struct inode
*inode
, u64 flush_tid
,
2495 struct ceph_mds_caps
*m
,
2496 struct ceph_mds_session
*session
)
2498 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2499 u64 follows
= le64_to_cpu(m
->snap_follows
);
2500 struct ceph_cap_snap
*capsnap
;
2503 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2504 inode
, ci
, session
->s_mds
, follows
);
2506 spin_lock(&inode
->i_lock
);
2507 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
2508 if (capsnap
->follows
== follows
) {
2509 if (capsnap
->flush_tid
!= flush_tid
) {
2510 dout(" cap_snap %p follows %lld tid %lld !="
2511 " %lld\n", capsnap
, follows
,
2512 flush_tid
, capsnap
->flush_tid
);
2515 WARN_ON(capsnap
->dirty_pages
|| capsnap
->writing
);
2516 dout(" removing %p cap_snap %p follows %lld\n",
2517 inode
, capsnap
, follows
);
2518 ceph_put_snap_context(capsnap
->context
);
2519 list_del(&capsnap
->ci_item
);
2520 list_del(&capsnap
->flushing_item
);
2521 ceph_put_cap_snap(capsnap
);
2525 dout(" skipping cap_snap %p follows %lld\n",
2526 capsnap
, capsnap
->follows
);
2529 spin_unlock(&inode
->i_lock
);
2535 * Handle TRUNC from MDS, indicating file truncation.
2537 * caller hold s_mutex.
2539 static void handle_cap_trunc(struct inode
*inode
,
2540 struct ceph_mds_caps
*trunc
,
2541 struct ceph_mds_session
*session
)
2542 __releases(inode
->i_lock
)
2544 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2545 int mds
= session
->s_mds
;
2546 int seq
= le32_to_cpu(trunc
->seq
);
2547 u32 truncate_seq
= le32_to_cpu(trunc
->truncate_seq
);
2548 u64 truncate_size
= le64_to_cpu(trunc
->truncate_size
);
2549 u64 size
= le64_to_cpu(trunc
->size
);
2550 int implemented
= 0;
2551 int dirty
= __ceph_caps_dirty(ci
);
2552 int issued
= __ceph_caps_issued(ceph_inode(inode
), &implemented
);
2553 int queue_trunc
= 0;
2555 issued
|= implemented
| dirty
;
2557 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2558 inode
, mds
, seq
, truncate_size
, truncate_seq
);
2559 queue_trunc
= ceph_fill_file_size(inode
, issued
,
2560 truncate_seq
, truncate_size
, size
);
2561 spin_unlock(&inode
->i_lock
);
2564 ceph_queue_vmtruncate(inode
);
2568 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
2569 * different one. If we are the most recent migration we've seen (as
2570 * indicated by mseq), make note of the migrating cap bits for the
2571 * duration (until we see the corresponding IMPORT).
2573 * caller holds s_mutex
2575 static void handle_cap_export(struct inode
*inode
, struct ceph_mds_caps
*ex
,
2576 struct ceph_mds_session
*session
,
2577 int *open_target_sessions
)
2579 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2580 int mds
= session
->s_mds
;
2581 unsigned mseq
= le32_to_cpu(ex
->migrate_seq
);
2582 struct ceph_cap
*cap
= NULL
, *t
;
2586 dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
2587 inode
, ci
, mds
, mseq
);
2589 spin_lock(&inode
->i_lock
);
2591 /* make sure we haven't seen a higher mseq */
2592 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
2593 t
= rb_entry(p
, struct ceph_cap
, ci_node
);
2594 if (ceph_seq_cmp(t
->mseq
, mseq
) > 0) {
2595 dout(" higher mseq on cap from mds%d\n",
2599 if (t
->session
->s_mds
== mds
)
2606 ci
->i_cap_exporting_mds
= mds
;
2607 ci
->i_cap_exporting_mseq
= mseq
;
2608 ci
->i_cap_exporting_issued
= cap
->issued
;
2611 * make sure we have open sessions with all possible
2612 * export targets, so that we get the matching IMPORT
2614 *open_target_sessions
= 1;
2616 __ceph_remove_cap(cap
);
2618 /* else, we already released it */
2620 spin_unlock(&inode
->i_lock
);
2624 * Handle cap IMPORT. If there are temp bits from an older EXPORT,
2627 * caller holds s_mutex.
2629 static void handle_cap_import(struct ceph_mds_client
*mdsc
,
2630 struct inode
*inode
, struct ceph_mds_caps
*im
,
2631 struct ceph_mds_session
*session
,
2632 void *snaptrace
, int snaptrace_len
)
2634 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2635 int mds
= session
->s_mds
;
2636 unsigned issued
= le32_to_cpu(im
->caps
);
2637 unsigned wanted
= le32_to_cpu(im
->wanted
);
2638 unsigned seq
= le32_to_cpu(im
->seq
);
2639 unsigned mseq
= le32_to_cpu(im
->migrate_seq
);
2640 u64 realmino
= le64_to_cpu(im
->realm
);
2641 u64 cap_id
= le64_to_cpu(im
->cap_id
);
2643 if (ci
->i_cap_exporting_mds
>= 0 &&
2644 ceph_seq_cmp(ci
->i_cap_exporting_mseq
, mseq
) < 0) {
2645 dout("handle_cap_import inode %p ci %p mds%d mseq %d"
2646 " - cleared exporting from mds%d\n",
2647 inode
, ci
, mds
, mseq
,
2648 ci
->i_cap_exporting_mds
);
2649 ci
->i_cap_exporting_issued
= 0;
2650 ci
->i_cap_exporting_mseq
= 0;
2651 ci
->i_cap_exporting_mds
= -1;
2653 dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
2654 inode
, ci
, mds
, mseq
);
2657 down_write(&mdsc
->snap_rwsem
);
2658 ceph_update_snap_trace(mdsc
, snaptrace
, snaptrace
+snaptrace_len
,
2660 downgrade_write(&mdsc
->snap_rwsem
);
2661 ceph_add_cap(inode
, session
, cap_id
, -1,
2662 issued
, wanted
, seq
, mseq
, realmino
, CEPH_CAP_FLAG_AUTH
,
2663 NULL
/* no caps context */);
2664 try_flush_caps(inode
, session
, NULL
);
2665 up_read(&mdsc
->snap_rwsem
);
2669 * Handle a caps message from the MDS.
2671 * Identify the appropriate session, inode, and call the right handler
2672 * based on the cap op.
2674 void ceph_handle_caps(struct ceph_mds_session
*session
,
2675 struct ceph_msg
*msg
)
2677 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2678 struct super_block
*sb
= mdsc
->client
->sb
;
2679 struct inode
*inode
;
2680 struct ceph_cap
*cap
;
2681 struct ceph_mds_caps
*h
;
2682 int mds
= session
->s_mds
;
2685 struct ceph_vino vino
;
2690 int open_target_sessions
= 0;
2692 dout("handle_caps from mds%d\n", mds
);
2695 tid
= le64_to_cpu(msg
->hdr
.tid
);
2696 if (msg
->front
.iov_len
< sizeof(*h
))
2698 h
= msg
->front
.iov_base
;
2700 op
= le32_to_cpu(h
->op
);
2701 vino
.ino
= le64_to_cpu(h
->ino
);
2702 vino
.snap
= CEPH_NOSNAP
;
2703 cap_id
= le64_to_cpu(h
->cap_id
);
2704 seq
= le32_to_cpu(h
->seq
);
2705 mseq
= le32_to_cpu(h
->migrate_seq
);
2706 size
= le64_to_cpu(h
->size
);
2707 max_size
= le64_to_cpu(h
->max_size
);
2709 mutex_lock(&session
->s_mutex
);
2711 dout(" mds%d seq %lld cap seq %u\n", session
->s_mds
, session
->s_seq
,
2715 inode
= ceph_find_inode(sb
, vino
);
2716 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op
), vino
.ino
,
2719 dout(" i don't have ino %llx\n", vino
.ino
);
2721 if (op
== CEPH_CAP_OP_IMPORT
)
2722 __queue_cap_release(session
, vino
.ino
, cap_id
,
2726 * send any full release message to try to move things
2727 * along for the mds (who clearly thinks we still have this
2730 ceph_add_cap_releases(mdsc
, session
);
2731 ceph_send_cap_releases(mdsc
, session
);
2735 /* these will work even if we don't have a cap yet */
2737 case CEPH_CAP_OP_FLUSHSNAP_ACK
:
2738 handle_cap_flushsnap_ack(inode
, tid
, h
, session
);
2741 case CEPH_CAP_OP_EXPORT
:
2742 handle_cap_export(inode
, h
, session
, &open_target_sessions
);
2745 case CEPH_CAP_OP_IMPORT
:
2746 handle_cap_import(mdsc
, inode
, h
, session
,
2747 snaptrace
, le32_to_cpu(h
->snap_trace_len
));
2748 ceph_check_caps(ceph_inode(inode
), CHECK_CAPS_NODELAY
,
2753 /* the rest require a cap */
2754 spin_lock(&inode
->i_lock
);
2755 cap
= __get_cap_for_mds(ceph_inode(inode
), mds
);
2757 dout(" no cap on %p ino %llx.%llx from mds%d\n",
2758 inode
, ceph_ino(inode
), ceph_snap(inode
), mds
);
2759 spin_unlock(&inode
->i_lock
);
2763 /* note that each of these drops i_lock for us */
2765 case CEPH_CAP_OP_REVOKE
:
2766 case CEPH_CAP_OP_GRANT
:
2767 handle_cap_grant(inode
, h
, session
, cap
, msg
->middle
);
2770 case CEPH_CAP_OP_FLUSH_ACK
:
2771 handle_cap_flush_ack(inode
, tid
, h
, session
, cap
);
2774 case CEPH_CAP_OP_TRUNC
:
2775 handle_cap_trunc(inode
, h
, session
);
2779 spin_unlock(&inode
->i_lock
);
2780 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op
,
2781 ceph_cap_op_name(op
));
2785 mutex_unlock(&session
->s_mutex
);
2789 if (open_target_sessions
)
2790 ceph_mdsc_open_export_target_sessions(mdsc
, session
);
2794 pr_err("ceph_handle_caps: corrupt message\n");
2800 * Delayed work handler to process end of delayed cap release LRU list.
2802 void ceph_check_delayed_caps(struct ceph_mds_client
*mdsc
)
2804 struct ceph_inode_info
*ci
;
2805 int flags
= CHECK_CAPS_NODELAY
;
2807 dout("check_delayed_caps\n");
2809 spin_lock(&mdsc
->cap_delay_lock
);
2810 if (list_empty(&mdsc
->cap_delay_list
))
2812 ci
= list_first_entry(&mdsc
->cap_delay_list
,
2813 struct ceph_inode_info
,
2815 if ((ci
->i_ceph_flags
& CEPH_I_FLUSH
) == 0 &&
2816 time_before(jiffies
, ci
->i_hold_caps_max
))
2818 list_del_init(&ci
->i_cap_delay_list
);
2819 spin_unlock(&mdsc
->cap_delay_lock
);
2820 dout("check_delayed_caps on %p\n", &ci
->vfs_inode
);
2821 ceph_check_caps(ci
, flags
, NULL
);
2823 spin_unlock(&mdsc
->cap_delay_lock
);
2827 * Flush all dirty caps to the mds
2829 void ceph_flush_dirty_caps(struct ceph_mds_client
*mdsc
)
2831 struct ceph_inode_info
*ci
, *nci
= NULL
;
2832 struct inode
*inode
, *ninode
= NULL
;
2833 struct list_head
*p
, *n
;
2835 dout("flush_dirty_caps\n");
2836 spin_lock(&mdsc
->cap_dirty_lock
);
2837 list_for_each_safe(p
, n
, &mdsc
->cap_dirty
) {
2841 ci
->i_ceph_flags
&= ~CEPH_I_NOFLUSH
;
2842 dout("flush_dirty_caps inode %p (was next inode)\n",
2845 ci
= list_entry(p
, struct ceph_inode_info
,
2847 inode
= igrab(&ci
->vfs_inode
);
2849 dout("flush_dirty_caps inode %p\n", inode
);
2851 if (n
!= &mdsc
->cap_dirty
) {
2852 nci
= list_entry(n
, struct ceph_inode_info
,
2854 ninode
= igrab(&nci
->vfs_inode
);
2856 nci
->i_ceph_flags
|= CEPH_I_NOFLUSH
;
2857 dout("flush_dirty_caps next inode %p, noflush\n",
2863 spin_unlock(&mdsc
->cap_dirty_lock
);
2865 ceph_check_caps(ci
, CHECK_CAPS_NODELAY
|CHECK_CAPS_FLUSH
,
2869 spin_lock(&mdsc
->cap_dirty_lock
);
2871 spin_unlock(&mdsc
->cap_dirty_lock
);
2875 * Drop open file reference. If we were the last open file,
2876 * we may need to release capabilities to the MDS (or schedule
2877 * their delayed release).
2879 void ceph_put_fmode(struct ceph_inode_info
*ci
, int fmode
)
2881 struct inode
*inode
= &ci
->vfs_inode
;
2884 spin_lock(&inode
->i_lock
);
2885 dout("put_fmode %p fmode %d %d -> %d\n", inode
, fmode
,
2886 ci
->i_nr_by_mode
[fmode
], ci
->i_nr_by_mode
[fmode
]-1);
2887 BUG_ON(ci
->i_nr_by_mode
[fmode
] == 0);
2888 if (--ci
->i_nr_by_mode
[fmode
] == 0)
2890 spin_unlock(&inode
->i_lock
);
2892 if (last
&& ci
->i_vino
.snap
== CEPH_NOSNAP
)
2893 ceph_check_caps(ci
, 0, NULL
);
2897 * Helpers for embedding cap and dentry lease releases into mds
2900 * @force is used by dentry_release (below) to force inclusion of a
2901 * record for the directory inode, even when there aren't any caps to
2904 int ceph_encode_inode_release(void **p
, struct inode
*inode
,
2905 int mds
, int drop
, int unless
, int force
)
2907 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2908 struct ceph_cap
*cap
;
2909 struct ceph_mds_request_release
*rel
= *p
;
2913 spin_lock(&inode
->i_lock
);
2914 used
= __ceph_caps_used(ci
);
2915 dirty
= __ceph_caps_dirty(ci
);
2917 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
2918 inode
, mds
, ceph_cap_string(used
|dirty
), ceph_cap_string(drop
),
2919 ceph_cap_string(unless
));
2921 /* only drop unused, clean caps */
2922 drop
&= ~(used
| dirty
);
2924 cap
= __get_cap_for_mds(ci
, mds
);
2925 if (cap
&& __cap_is_valid(cap
)) {
2927 ((cap
->issued
& drop
) &&
2928 (cap
->issued
& unless
) == 0)) {
2929 if ((cap
->issued
& drop
) &&
2930 (cap
->issued
& unless
) == 0) {
2931 dout("encode_inode_release %p cap %p %s -> "
2933 ceph_cap_string(cap
->issued
),
2934 ceph_cap_string(cap
->issued
& ~drop
));
2935 cap
->issued
&= ~drop
;
2936 cap
->implemented
&= ~drop
;
2937 if (ci
->i_ceph_flags
& CEPH_I_NODELAY
) {
2938 int wanted
= __ceph_caps_wanted(ci
);
2939 dout(" wanted %s -> %s (act %s)\n",
2940 ceph_cap_string(cap
->mds_wanted
),
2941 ceph_cap_string(cap
->mds_wanted
&
2943 ceph_cap_string(wanted
));
2944 cap
->mds_wanted
&= wanted
;
2947 dout("encode_inode_release %p cap %p %s"
2948 " (force)\n", inode
, cap
,
2949 ceph_cap_string(cap
->issued
));
2952 rel
->ino
= cpu_to_le64(ceph_ino(inode
));
2953 rel
->cap_id
= cpu_to_le64(cap
->cap_id
);
2954 rel
->seq
= cpu_to_le32(cap
->seq
);
2955 rel
->issue_seq
= cpu_to_le32(cap
->issue_seq
),
2956 rel
->mseq
= cpu_to_le32(cap
->mseq
);
2957 rel
->caps
= cpu_to_le32(cap
->issued
);
2958 rel
->wanted
= cpu_to_le32(cap
->mds_wanted
);
2964 dout("encode_inode_release %p cap %p %s\n",
2965 inode
, cap
, ceph_cap_string(cap
->issued
));
2968 spin_unlock(&inode
->i_lock
);
2972 int ceph_encode_dentry_release(void **p
, struct dentry
*dentry
,
2973 int mds
, int drop
, int unless
)
2975 struct inode
*dir
= dentry
->d_parent
->d_inode
;
2976 struct ceph_mds_request_release
*rel
= *p
;
2977 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
2982 * force an record for the directory caps if we have a dentry lease.
2983 * this is racy (can't take i_lock and d_lock together), but it
2984 * doesn't have to be perfect; the mds will revoke anything we don't
2987 spin_lock(&dentry
->d_lock
);
2988 if (di
->lease_session
&& di
->lease_session
->s_mds
== mds
)
2990 spin_unlock(&dentry
->d_lock
);
2992 ret
= ceph_encode_inode_release(p
, dir
, mds
, drop
, unless
, force
);
2994 spin_lock(&dentry
->d_lock
);
2995 if (ret
&& di
->lease_session
&& di
->lease_session
->s_mds
== mds
) {
2996 dout("encode_dentry_release %p mds%d seq %d\n",
2997 dentry
, mds
, (int)di
->lease_seq
);
2998 rel
->dname_len
= cpu_to_le32(dentry
->d_name
.len
);
2999 memcpy(*p
, dentry
->d_name
.name
, dentry
->d_name
.len
);
3000 *p
+= dentry
->d_name
.len
;
3001 rel
->dname_seq
= cpu_to_le32(di
->lease_seq
);
3002 __ceph_mdsc_drop_dentry_lease(dentry
);
3004 spin_unlock(&dentry
->d_lock
);