1 #include <linux/ceph/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>
12 #include "mds_client.h"
13 #include <linux/ceph/decode.h>
14 #include <linux/ceph/messenger.h>
17 * Capability management
19 * The Ceph metadata servers control client access to inode metadata
20 * and file data by issuing capabilities, granting clients permission
21 * to read and/or write both inode field and file data to OSDs
22 * (storage nodes). Each capability consists of a set of bits
23 * indicating which operations are allowed.
25 * If the client holds a *_SHARED cap, the client has a coherent value
26 * that can be safely read from the cached inode.
28 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
29 * client is allowed to change inode attributes (e.g., file size,
30 * mtime), note its dirty state in the ceph_cap, and asynchronously
31 * flush that metadata change to the MDS.
33 * In the event of a conflicting operation (perhaps by another
34 * client), the MDS will revoke the conflicting client capabilities.
36 * In order for a client to cache an inode, it must hold a capability
37 * with at least one MDS server. When inodes are released, release
38 * notifications are batched and periodically sent en masse to the MDS
39 * cluster to release server state.
44 * Generate readable cap strings for debugging output.
46 #define MAX_CAP_STR 20
47 static char cap_str
[MAX_CAP_STR
][40];
48 static DEFINE_SPINLOCK(cap_str_lock
);
49 static int last_cap_str
;
51 static char *gcap_string(char *s
, int c
)
53 if (c
& CEPH_CAP_GSHARED
)
55 if (c
& CEPH_CAP_GEXCL
)
57 if (c
& CEPH_CAP_GCACHE
)
63 if (c
& CEPH_CAP_GBUFFER
)
65 if (c
& CEPH_CAP_GLAZYIO
)
70 const char *ceph_cap_string(int caps
)
76 spin_lock(&cap_str_lock
);
78 if (last_cap_str
== MAX_CAP_STR
)
80 spin_unlock(&cap_str_lock
);
84 if (caps
& CEPH_CAP_PIN
)
87 c
= (caps
>> CEPH_CAP_SAUTH
) & 3;
90 s
= gcap_string(s
, c
);
93 c
= (caps
>> CEPH_CAP_SLINK
) & 3;
96 s
= gcap_string(s
, c
);
99 c
= (caps
>> CEPH_CAP_SXATTR
) & 3;
102 s
= gcap_string(s
, c
);
105 c
= caps
>> CEPH_CAP_SFILE
;
108 s
= gcap_string(s
, c
);
117 void ceph_caps_init(struct ceph_mds_client
*mdsc
)
119 INIT_LIST_HEAD(&mdsc
->caps_list
);
120 spin_lock_init(&mdsc
->caps_list_lock
);
123 void ceph_caps_finalize(struct ceph_mds_client
*mdsc
)
125 struct ceph_cap
*cap
;
127 spin_lock(&mdsc
->caps_list_lock
);
128 while (!list_empty(&mdsc
->caps_list
)) {
129 cap
= list_first_entry(&mdsc
->caps_list
,
130 struct ceph_cap
, caps_item
);
131 list_del(&cap
->caps_item
);
132 kmem_cache_free(ceph_cap_cachep
, cap
);
134 mdsc
->caps_total_count
= 0;
135 mdsc
->caps_avail_count
= 0;
136 mdsc
->caps_use_count
= 0;
137 mdsc
->caps_reserve_count
= 0;
138 mdsc
->caps_min_count
= 0;
139 spin_unlock(&mdsc
->caps_list_lock
);
142 void ceph_adjust_min_caps(struct ceph_mds_client
*mdsc
, int delta
)
144 spin_lock(&mdsc
->caps_list_lock
);
145 mdsc
->caps_min_count
+= delta
;
146 BUG_ON(mdsc
->caps_min_count
< 0);
147 spin_unlock(&mdsc
->caps_list_lock
);
150 int ceph_reserve_caps(struct ceph_mds_client
*mdsc
,
151 struct ceph_cap_reservation
*ctx
, int need
)
154 struct ceph_cap
*cap
;
160 dout("reserve caps ctx=%p need=%d\n", ctx
, need
);
162 /* first reserve any caps that are already allocated */
163 spin_lock(&mdsc
->caps_list_lock
);
164 if (mdsc
->caps_avail_count
>= need
)
167 have
= mdsc
->caps_avail_count
;
168 mdsc
->caps_avail_count
-= have
;
169 mdsc
->caps_reserve_count
+= have
;
170 BUG_ON(mdsc
->caps_total_count
!= mdsc
->caps_use_count
+
171 mdsc
->caps_reserve_count
+
172 mdsc
->caps_avail_count
);
173 spin_unlock(&mdsc
->caps_list_lock
);
175 for (i
= have
; i
< need
; i
++) {
176 cap
= kmem_cache_alloc(ceph_cap_cachep
, GFP_NOFS
);
179 goto out_alloc_count
;
181 list_add(&cap
->caps_item
, &newcaps
);
184 BUG_ON(have
+ alloc
!= need
);
186 spin_lock(&mdsc
->caps_list_lock
);
187 mdsc
->caps_total_count
+= alloc
;
188 mdsc
->caps_reserve_count
+= alloc
;
189 list_splice(&newcaps
, &mdsc
->caps_list
);
191 BUG_ON(mdsc
->caps_total_count
!= mdsc
->caps_use_count
+
192 mdsc
->caps_reserve_count
+
193 mdsc
->caps_avail_count
);
194 spin_unlock(&mdsc
->caps_list_lock
);
197 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
198 ctx
, mdsc
->caps_total_count
, mdsc
->caps_use_count
,
199 mdsc
->caps_reserve_count
, mdsc
->caps_avail_count
);
203 /* we didn't manage to reserve as much as we needed */
204 pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
209 int ceph_unreserve_caps(struct ceph_mds_client
*mdsc
,
210 struct ceph_cap_reservation
*ctx
)
212 dout("unreserve caps ctx=%p count=%d\n", ctx
, ctx
->count
);
214 spin_lock(&mdsc
->caps_list_lock
);
215 BUG_ON(mdsc
->caps_reserve_count
< ctx
->count
);
216 mdsc
->caps_reserve_count
-= ctx
->count
;
217 mdsc
->caps_avail_count
+= ctx
->count
;
219 dout("unreserve caps %d = %d used + %d resv + %d avail\n",
220 mdsc
->caps_total_count
, mdsc
->caps_use_count
,
221 mdsc
->caps_reserve_count
, mdsc
->caps_avail_count
);
222 BUG_ON(mdsc
->caps_total_count
!= mdsc
->caps_use_count
+
223 mdsc
->caps_reserve_count
+
224 mdsc
->caps_avail_count
);
225 spin_unlock(&mdsc
->caps_list_lock
);
230 static struct ceph_cap
*get_cap(struct ceph_mds_client
*mdsc
,
231 struct ceph_cap_reservation
*ctx
)
233 struct ceph_cap
*cap
= NULL
;
235 /* temporary, until we do something about cap import/export */
237 cap
= kmem_cache_alloc(ceph_cap_cachep
, GFP_NOFS
);
239 spin_lock(&mdsc
->caps_list_lock
);
240 mdsc
->caps_use_count
++;
241 mdsc
->caps_total_count
++;
242 spin_unlock(&mdsc
->caps_list_lock
);
247 spin_lock(&mdsc
->caps_list_lock
);
248 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
249 ctx
, ctx
->count
, mdsc
->caps_total_count
, mdsc
->caps_use_count
,
250 mdsc
->caps_reserve_count
, mdsc
->caps_avail_count
);
252 BUG_ON(ctx
->count
> mdsc
->caps_reserve_count
);
253 BUG_ON(list_empty(&mdsc
->caps_list
));
256 mdsc
->caps_reserve_count
--;
257 mdsc
->caps_use_count
++;
259 cap
= list_first_entry(&mdsc
->caps_list
, struct ceph_cap
, caps_item
);
260 list_del(&cap
->caps_item
);
262 BUG_ON(mdsc
->caps_total_count
!= mdsc
->caps_use_count
+
263 mdsc
->caps_reserve_count
+ mdsc
->caps_avail_count
);
264 spin_unlock(&mdsc
->caps_list_lock
);
268 void ceph_put_cap(struct ceph_mds_client
*mdsc
, struct ceph_cap
*cap
)
270 spin_lock(&mdsc
->caps_list_lock
);
271 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
272 cap
, mdsc
->caps_total_count
, mdsc
->caps_use_count
,
273 mdsc
->caps_reserve_count
, mdsc
->caps_avail_count
);
274 mdsc
->caps_use_count
--;
276 * Keep some preallocated caps around (ceph_min_count), to
277 * avoid lots of free/alloc churn.
279 if (mdsc
->caps_avail_count
>= mdsc
->caps_reserve_count
+
280 mdsc
->caps_min_count
) {
281 mdsc
->caps_total_count
--;
282 kmem_cache_free(ceph_cap_cachep
, cap
);
284 mdsc
->caps_avail_count
++;
285 list_add(&cap
->caps_item
, &mdsc
->caps_list
);
288 BUG_ON(mdsc
->caps_total_count
!= mdsc
->caps_use_count
+
289 mdsc
->caps_reserve_count
+ mdsc
->caps_avail_count
);
290 spin_unlock(&mdsc
->caps_list_lock
);
293 void ceph_reservation_status(struct ceph_fs_client
*fsc
,
294 int *total
, int *avail
, int *used
, int *reserved
,
297 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
300 *total
= mdsc
->caps_total_count
;
302 *avail
= mdsc
->caps_avail_count
;
304 *used
= mdsc
->caps_use_count
;
306 *reserved
= mdsc
->caps_reserve_count
;
308 *min
= mdsc
->caps_min_count
;
312 * Find ceph_cap for given mds, if any.
314 * Called with i_ceph_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
)
333 struct ceph_cap
*ceph_get_cap_for_mds(struct ceph_inode_info
*ci
, int mds
)
335 struct ceph_cap
*cap
;
337 spin_lock(&ci
->i_ceph_lock
);
338 cap
= __get_cap_for_mds(ci
, mds
);
339 spin_unlock(&ci
->i_ceph_lock
);
344 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
346 static int __ceph_get_cap_mds(struct ceph_inode_info
*ci
)
348 struct ceph_cap
*cap
;
352 /* prefer mds with WR|BUFFER|EXCL caps */
353 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
354 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
356 if (cap
->issued
& (CEPH_CAP_FILE_WR
|
357 CEPH_CAP_FILE_BUFFER
|
364 int ceph_get_cap_mds(struct inode
*inode
)
366 struct ceph_inode_info
*ci
= ceph_inode(inode
);
368 spin_lock(&ci
->i_ceph_lock
);
369 mds
= __ceph_get_cap_mds(ceph_inode(inode
));
370 spin_unlock(&ci
->i_ceph_lock
);
375 * Called under i_ceph_lock.
377 static void __insert_cap_node(struct ceph_inode_info
*ci
,
378 struct ceph_cap
*new)
380 struct rb_node
**p
= &ci
->i_caps
.rb_node
;
381 struct rb_node
*parent
= NULL
;
382 struct ceph_cap
*cap
= NULL
;
386 cap
= rb_entry(parent
, struct ceph_cap
, ci_node
);
387 if (new->mds
< cap
->mds
)
389 else if (new->mds
> cap
->mds
)
395 rb_link_node(&new->ci_node
, parent
, p
);
396 rb_insert_color(&new->ci_node
, &ci
->i_caps
);
400 * (re)set cap hold timeouts, which control the delayed release
401 * of unused caps back to the MDS. Should be called on cap use.
403 static void __cap_set_timeouts(struct ceph_mds_client
*mdsc
,
404 struct ceph_inode_info
*ci
)
406 struct ceph_mount_options
*ma
= mdsc
->fsc
->mount_options
;
408 ci
->i_hold_caps_min
= round_jiffies(jiffies
+
409 ma
->caps_wanted_delay_min
* HZ
);
410 ci
->i_hold_caps_max
= round_jiffies(jiffies
+
411 ma
->caps_wanted_delay_max
* HZ
);
412 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci
->vfs_inode
,
413 ci
->i_hold_caps_min
- jiffies
, ci
->i_hold_caps_max
- jiffies
);
417 * (Re)queue cap at the end of the delayed cap release list.
419 * If I_FLUSH is set, leave the inode at the front of the list.
421 * Caller holds i_ceph_lock
422 * -> we take mdsc->cap_delay_lock
424 static void __cap_delay_requeue(struct ceph_mds_client
*mdsc
,
425 struct ceph_inode_info
*ci
)
427 __cap_set_timeouts(mdsc
, ci
);
428 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci
->vfs_inode
,
429 ci
->i_ceph_flags
, ci
->i_hold_caps_max
);
430 if (!mdsc
->stopping
) {
431 spin_lock(&mdsc
->cap_delay_lock
);
432 if (!list_empty(&ci
->i_cap_delay_list
)) {
433 if (ci
->i_ceph_flags
& CEPH_I_FLUSH
)
435 list_del_init(&ci
->i_cap_delay_list
);
437 list_add_tail(&ci
->i_cap_delay_list
, &mdsc
->cap_delay_list
);
439 spin_unlock(&mdsc
->cap_delay_lock
);
444 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
445 * indicating we should send a cap message to flush dirty metadata
446 * asap, and move to the front of the delayed cap list.
448 static void __cap_delay_requeue_front(struct ceph_mds_client
*mdsc
,
449 struct ceph_inode_info
*ci
)
451 dout("__cap_delay_requeue_front %p\n", &ci
->vfs_inode
);
452 spin_lock(&mdsc
->cap_delay_lock
);
453 ci
->i_ceph_flags
|= CEPH_I_FLUSH
;
454 if (!list_empty(&ci
->i_cap_delay_list
))
455 list_del_init(&ci
->i_cap_delay_list
);
456 list_add(&ci
->i_cap_delay_list
, &mdsc
->cap_delay_list
);
457 spin_unlock(&mdsc
->cap_delay_lock
);
461 * Cancel delayed work on cap.
463 * Caller must hold i_ceph_lock.
465 static void __cap_delay_cancel(struct ceph_mds_client
*mdsc
,
466 struct ceph_inode_info
*ci
)
468 dout("__cap_delay_cancel %p\n", &ci
->vfs_inode
);
469 if (list_empty(&ci
->i_cap_delay_list
))
471 spin_lock(&mdsc
->cap_delay_lock
);
472 list_del_init(&ci
->i_cap_delay_list
);
473 spin_unlock(&mdsc
->cap_delay_lock
);
477 * Common issue checks for add_cap, handle_cap_grant.
479 static void __check_cap_issue(struct ceph_inode_info
*ci
, struct ceph_cap
*cap
,
482 unsigned had
= __ceph_caps_issued(ci
, NULL
);
485 * Each time we receive FILE_CACHE anew, we increment
488 if ((issued
& (CEPH_CAP_FILE_CACHE
|CEPH_CAP_FILE_LAZYIO
)) &&
489 (had
& (CEPH_CAP_FILE_CACHE
|CEPH_CAP_FILE_LAZYIO
)) == 0)
493 * if we are newly issued FILE_SHARED, mark dir not complete; we
494 * don't know what happened to this directory while we didn't
497 if ((issued
& CEPH_CAP_FILE_SHARED
) &&
498 (had
& CEPH_CAP_FILE_SHARED
) == 0) {
500 if (S_ISDIR(ci
->vfs_inode
.i_mode
)) {
501 dout(" marking %p NOT complete\n", &ci
->vfs_inode
);
502 __ceph_dir_clear_complete(ci
);
508 * Add a capability under the given MDS session.
510 * Caller should hold session snap_rwsem (read) and s_mutex.
512 * @fmode is the open file mode, if we are opening a file, otherwise
513 * it is < 0. (This is so we can atomically add the cap and add an
514 * open file reference to it.)
516 int ceph_add_cap(struct inode
*inode
,
517 struct ceph_mds_session
*session
, u64 cap_id
,
518 int fmode
, unsigned issued
, unsigned wanted
,
519 unsigned seq
, unsigned mseq
, u64 realmino
, int flags
,
520 struct ceph_cap_reservation
*caps_reservation
)
522 struct ceph_mds_client
*mdsc
= ceph_inode_to_client(inode
)->mdsc
;
523 struct ceph_inode_info
*ci
= ceph_inode(inode
);
524 struct ceph_cap
*new_cap
= NULL
;
525 struct ceph_cap
*cap
;
526 int mds
= session
->s_mds
;
529 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode
,
530 session
->s_mds
, cap_id
, ceph_cap_string(issued
), seq
);
533 * If we are opening the file, include file mode wanted bits
537 wanted
|= ceph_caps_for_mode(fmode
);
540 spin_lock(&ci
->i_ceph_lock
);
541 cap
= __get_cap_for_mds(ci
, mds
);
547 spin_unlock(&ci
->i_ceph_lock
);
548 new_cap
= get_cap(mdsc
, caps_reservation
);
555 cap
->implemented
= 0;
561 __insert_cap_node(ci
, cap
);
563 /* clear out old exporting info? (i.e. on cap import) */
564 if (ci
->i_cap_exporting_mds
== mds
) {
565 ci
->i_cap_exporting_issued
= 0;
566 ci
->i_cap_exporting_mseq
= 0;
567 ci
->i_cap_exporting_mds
= -1;
570 /* add to session cap list */
571 cap
->session
= session
;
572 spin_lock(&session
->s_cap_lock
);
573 list_add_tail(&cap
->session_caps
, &session
->s_caps
);
574 session
->s_nr_caps
++;
575 spin_unlock(&session
->s_cap_lock
);
577 ceph_put_cap(mdsc
, new_cap
);
579 if (!ci
->i_snap_realm
) {
581 * add this inode to the appropriate snap realm
583 struct ceph_snap_realm
*realm
= ceph_lookup_snap_realm(mdsc
,
586 ceph_get_snap_realm(mdsc
, realm
);
587 spin_lock(&realm
->inodes_with_caps_lock
);
588 ci
->i_snap_realm
= realm
;
589 list_add(&ci
->i_snap_realm_item
,
590 &realm
->inodes_with_caps
);
591 spin_unlock(&realm
->inodes_with_caps_lock
);
593 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
599 __check_cap_issue(ci
, cap
, issued
);
602 * If we are issued caps we don't want, or the mds' wanted
603 * value appears to be off, queue a check so we'll release
604 * later and/or update the mds wanted value.
606 actual_wanted
= __ceph_caps_wanted(ci
);
607 if ((wanted
& ~actual_wanted
) ||
608 (issued
& ~actual_wanted
& CEPH_CAP_ANY_WR
)) {
609 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
610 ceph_cap_string(issued
), ceph_cap_string(wanted
),
611 ceph_cap_string(actual_wanted
));
612 __cap_delay_requeue(mdsc
, ci
);
615 if (flags
& CEPH_CAP_FLAG_AUTH
) {
616 if (ci
->i_auth_cap
== NULL
||
617 ceph_seq_cmp(ci
->i_auth_cap
->mseq
, mseq
) < 0)
618 ci
->i_auth_cap
= cap
;
619 } else if (ci
->i_auth_cap
== cap
) {
620 ci
->i_auth_cap
= NULL
;
621 spin_lock(&mdsc
->cap_dirty_lock
);
622 if (!list_empty(&ci
->i_dirty_item
)) {
623 dout(" moving %p to cap_dirty_migrating\n", inode
);
624 list_move(&ci
->i_dirty_item
,
625 &mdsc
->cap_dirty_migrating
);
627 spin_unlock(&mdsc
->cap_dirty_lock
);
630 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
631 inode
, ceph_vinop(inode
), cap
, ceph_cap_string(issued
),
632 ceph_cap_string(issued
|cap
->issued
), seq
, mds
);
633 cap
->cap_id
= cap_id
;
634 cap
->issued
= issued
;
635 cap
->implemented
|= issued
;
636 if (mseq
> cap
->mseq
)
637 cap
->mds_wanted
= wanted
;
639 cap
->mds_wanted
|= wanted
;
641 cap
->issue_seq
= seq
;
643 cap
->cap_gen
= session
->s_cap_gen
;
646 __ceph_get_fmode(ci
, fmode
);
647 spin_unlock(&ci
->i_ceph_lock
);
648 wake_up_all(&ci
->i_cap_wq
);
653 * Return true if cap has not timed out and belongs to the current
654 * generation of the MDS session (i.e. has not gone 'stale' due to
655 * us losing touch with the mds).
657 static int __cap_is_valid(struct ceph_cap
*cap
)
662 spin_lock(&cap
->session
->s_gen_ttl_lock
);
663 gen
= cap
->session
->s_cap_gen
;
664 ttl
= cap
->session
->s_cap_ttl
;
665 spin_unlock(&cap
->session
->s_gen_ttl_lock
);
667 if (cap
->cap_gen
< gen
|| time_after_eq(jiffies
, ttl
)) {
668 dout("__cap_is_valid %p cap %p issued %s "
669 "but STALE (gen %u vs %u)\n", &cap
->ci
->vfs_inode
,
670 cap
, ceph_cap_string(cap
->issued
), cap
->cap_gen
, gen
);
678 * Return set of valid cap bits issued to us. Note that caps time
679 * out, and may be invalidated in bulk if the client session times out
680 * and session->s_cap_gen is bumped.
682 int __ceph_caps_issued(struct ceph_inode_info
*ci
, int *implemented
)
684 int have
= ci
->i_snap_caps
| ci
->i_cap_exporting_issued
;
685 struct ceph_cap
*cap
;
690 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
691 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
692 if (!__cap_is_valid(cap
))
694 dout("__ceph_caps_issued %p cap %p issued %s\n",
695 &ci
->vfs_inode
, cap
, ceph_cap_string(cap
->issued
));
698 *implemented
|= cap
->implemented
;
704 * Get cap bits issued by caps other than @ocap
706 int __ceph_caps_issued_other(struct ceph_inode_info
*ci
, struct ceph_cap
*ocap
)
708 int have
= ci
->i_snap_caps
;
709 struct ceph_cap
*cap
;
712 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
713 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
716 if (!__cap_is_valid(cap
))
724 * Move a cap to the end of the LRU (oldest caps at list head, newest
727 static void __touch_cap(struct ceph_cap
*cap
)
729 struct ceph_mds_session
*s
= cap
->session
;
731 spin_lock(&s
->s_cap_lock
);
732 if (s
->s_cap_iterator
== NULL
) {
733 dout("__touch_cap %p cap %p mds%d\n", &cap
->ci
->vfs_inode
, cap
,
735 list_move_tail(&cap
->session_caps
, &s
->s_caps
);
737 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
738 &cap
->ci
->vfs_inode
, cap
, s
->s_mds
);
740 spin_unlock(&s
->s_cap_lock
);
744 * Check if we hold the given mask. If so, move the cap(s) to the
745 * front of their respective LRUs. (This is the preferred way for
746 * callers to check for caps they want.)
748 int __ceph_caps_issued_mask(struct ceph_inode_info
*ci
, int mask
, int touch
)
750 struct ceph_cap
*cap
;
752 int have
= ci
->i_snap_caps
;
754 if ((have
& mask
) == mask
) {
755 dout("__ceph_caps_issued_mask %p snap issued %s"
756 " (mask %s)\n", &ci
->vfs_inode
,
757 ceph_cap_string(have
),
758 ceph_cap_string(mask
));
762 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
763 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
764 if (!__cap_is_valid(cap
))
766 if ((cap
->issued
& mask
) == mask
) {
767 dout("__ceph_caps_issued_mask %p cap %p issued %s"
768 " (mask %s)\n", &ci
->vfs_inode
, cap
,
769 ceph_cap_string(cap
->issued
),
770 ceph_cap_string(mask
));
776 /* does a combination of caps satisfy mask? */
778 if ((have
& mask
) == mask
) {
779 dout("__ceph_caps_issued_mask %p combo issued %s"
780 " (mask %s)\n", &ci
->vfs_inode
,
781 ceph_cap_string(cap
->issued
),
782 ceph_cap_string(mask
));
786 /* touch this + preceding caps */
788 for (q
= rb_first(&ci
->i_caps
); q
!= p
;
790 cap
= rb_entry(q
, struct ceph_cap
,
792 if (!__cap_is_valid(cap
))
805 * Return true if mask caps are currently being revoked by an MDS.
807 int ceph_caps_revoking(struct ceph_inode_info
*ci
, int mask
)
809 struct inode
*inode
= &ci
->vfs_inode
;
810 struct ceph_cap
*cap
;
814 spin_lock(&ci
->i_ceph_lock
);
815 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
816 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
817 if (__cap_is_valid(cap
) &&
818 (cap
->implemented
& ~cap
->issued
& mask
)) {
823 spin_unlock(&ci
->i_ceph_lock
);
824 dout("ceph_caps_revoking %p %s = %d\n", inode
,
825 ceph_cap_string(mask
), ret
);
829 int __ceph_caps_used(struct ceph_inode_info
*ci
)
833 used
|= CEPH_CAP_PIN
;
835 used
|= CEPH_CAP_FILE_RD
;
836 if (ci
->i_rdcache_ref
|| ci
->vfs_inode
.i_data
.nrpages
)
837 used
|= CEPH_CAP_FILE_CACHE
;
839 used
|= CEPH_CAP_FILE_WR
;
840 if (ci
->i_wb_ref
|| ci
->i_wrbuffer_ref
)
841 used
|= CEPH_CAP_FILE_BUFFER
;
846 * wanted, by virtue of open file modes
848 int __ceph_caps_file_wanted(struct ceph_inode_info
*ci
)
852 for (mode
= 0; mode
< CEPH_FILE_MODE_NUM
; mode
++)
853 if (ci
->i_nr_by_mode
[mode
])
854 want
|= ceph_caps_for_mode(mode
);
859 * Return caps we have registered with the MDS(s) as 'wanted'.
861 int __ceph_caps_mds_wanted(struct ceph_inode_info
*ci
)
863 struct ceph_cap
*cap
;
867 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
868 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
869 if (!__cap_is_valid(cap
))
871 mds_wanted
|= cap
->mds_wanted
;
877 * called under i_ceph_lock
879 static int __ceph_is_any_caps(struct ceph_inode_info
*ci
)
881 return !RB_EMPTY_ROOT(&ci
->i_caps
) || ci
->i_cap_exporting_mds
>= 0;
885 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
887 * caller should hold i_ceph_lock.
888 * caller will not hold session s_mutex if called from destroy_inode.
890 void __ceph_remove_cap(struct ceph_cap
*cap
)
892 struct ceph_mds_session
*session
= cap
->session
;
893 struct ceph_inode_info
*ci
= cap
->ci
;
894 struct ceph_mds_client
*mdsc
=
895 ceph_sb_to_client(ci
->vfs_inode
.i_sb
)->mdsc
;
898 dout("__ceph_remove_cap %p from %p\n", cap
, &ci
->vfs_inode
);
900 /* remove from session list */
901 spin_lock(&session
->s_cap_lock
);
902 if (session
->s_cap_iterator
== cap
) {
903 /* not yet, we are iterating over this very cap */
904 dout("__ceph_remove_cap delaying %p removal from session %p\n",
907 list_del_init(&cap
->session_caps
);
908 session
->s_nr_caps
--;
912 /* protect backpointer with s_cap_lock: see iterate_session_caps */
914 spin_unlock(&session
->s_cap_lock
);
916 /* remove from inode list */
917 rb_erase(&cap
->ci_node
, &ci
->i_caps
);
918 if (ci
->i_auth_cap
== cap
)
919 ci
->i_auth_cap
= NULL
;
922 ceph_put_cap(mdsc
, cap
);
924 if (!__ceph_is_any_caps(ci
) && ci
->i_snap_realm
) {
925 struct ceph_snap_realm
*realm
= ci
->i_snap_realm
;
926 spin_lock(&realm
->inodes_with_caps_lock
);
927 list_del_init(&ci
->i_snap_realm_item
);
928 ci
->i_snap_realm_counter
++;
929 ci
->i_snap_realm
= NULL
;
930 spin_unlock(&realm
->inodes_with_caps_lock
);
931 ceph_put_snap_realm(mdsc
, realm
);
933 if (!__ceph_is_any_real_caps(ci
))
934 __cap_delay_cancel(mdsc
, ci
);
938 * Build and send a cap message to the given MDS.
940 * Caller should be holding s_mutex.
942 static int send_cap_msg(struct ceph_mds_session
*session
,
943 u64 ino
, u64 cid
, int op
,
944 int caps
, int wanted
, int dirty
,
945 u32 seq
, u64 flush_tid
, u32 issue_seq
, u32 mseq
,
946 u64 size
, u64 max_size
,
947 struct timespec
*mtime
, struct timespec
*atime
,
949 kuid_t uid
, kgid_t gid
, umode_t mode
,
951 struct ceph_buffer
*xattrs_buf
,
954 struct ceph_mds_caps
*fc
;
955 struct ceph_msg
*msg
;
957 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
958 " seq %u/%u mseq %u follows %lld size %llu/%llu"
959 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op
),
960 cid
, ino
, ceph_cap_string(caps
), ceph_cap_string(wanted
),
961 ceph_cap_string(dirty
),
962 seq
, issue_seq
, mseq
, follows
, size
, max_size
,
963 xattr_version
, xattrs_buf
? (int)xattrs_buf
->vec
.iov_len
: 0);
965 msg
= ceph_msg_new(CEPH_MSG_CLIENT_CAPS
, sizeof(*fc
), GFP_NOFS
, false);
969 msg
->hdr
.tid
= cpu_to_le64(flush_tid
);
971 fc
= msg
->front
.iov_base
;
972 memset(fc
, 0, sizeof(*fc
));
974 fc
->cap_id
= cpu_to_le64(cid
);
975 fc
->op
= cpu_to_le32(op
);
976 fc
->seq
= cpu_to_le32(seq
);
977 fc
->issue_seq
= cpu_to_le32(issue_seq
);
978 fc
->migrate_seq
= cpu_to_le32(mseq
);
979 fc
->caps
= cpu_to_le32(caps
);
980 fc
->wanted
= cpu_to_le32(wanted
);
981 fc
->dirty
= cpu_to_le32(dirty
);
982 fc
->ino
= cpu_to_le64(ino
);
983 fc
->snap_follows
= cpu_to_le64(follows
);
985 fc
->size
= cpu_to_le64(size
);
986 fc
->max_size
= cpu_to_le64(max_size
);
988 ceph_encode_timespec(&fc
->mtime
, mtime
);
990 ceph_encode_timespec(&fc
->atime
, atime
);
991 fc
->time_warp_seq
= cpu_to_le32(time_warp_seq
);
993 fc
->uid
= cpu_to_le32(from_kuid(&init_user_ns
, uid
));
994 fc
->gid
= cpu_to_le32(from_kgid(&init_user_ns
, gid
));
995 fc
->mode
= cpu_to_le32(mode
);
997 fc
->xattr_version
= cpu_to_le64(xattr_version
);
999 msg
->middle
= ceph_buffer_get(xattrs_buf
);
1000 fc
->xattr_len
= cpu_to_le32(xattrs_buf
->vec
.iov_len
);
1001 msg
->hdr
.middle_len
= cpu_to_le32(xattrs_buf
->vec
.iov_len
);
1004 ceph_con_send(&session
->s_con
, msg
);
1008 void __queue_cap_release(struct ceph_mds_session
*session
,
1009 u64 ino
, u64 cap_id
, u32 migrate_seq
,
1012 struct ceph_msg
*msg
;
1013 struct ceph_mds_cap_release
*head
;
1014 struct ceph_mds_cap_item
*item
;
1016 spin_lock(&session
->s_cap_lock
);
1017 BUG_ON(!session
->s_num_cap_releases
);
1018 msg
= list_first_entry(&session
->s_cap_releases
,
1019 struct ceph_msg
, list_head
);
1021 dout(" adding %llx release to mds%d msg %p (%d left)\n",
1022 ino
, session
->s_mds
, msg
, session
->s_num_cap_releases
);
1024 BUG_ON(msg
->front
.iov_len
+ sizeof(*item
) > PAGE_CACHE_SIZE
);
1025 head
= msg
->front
.iov_base
;
1026 le32_add_cpu(&head
->num
, 1);
1027 item
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
1028 item
->ino
= cpu_to_le64(ino
);
1029 item
->cap_id
= cpu_to_le64(cap_id
);
1030 item
->migrate_seq
= cpu_to_le32(migrate_seq
);
1031 item
->seq
= cpu_to_le32(issue_seq
);
1033 session
->s_num_cap_releases
--;
1035 msg
->front
.iov_len
+= sizeof(*item
);
1036 if (le32_to_cpu(head
->num
) == CEPH_CAPS_PER_RELEASE
) {
1037 dout(" release msg %p full\n", msg
);
1038 list_move_tail(&msg
->list_head
, &session
->s_cap_releases_done
);
1040 dout(" release msg %p at %d/%d (%d)\n", msg
,
1041 (int)le32_to_cpu(head
->num
),
1042 (int)CEPH_CAPS_PER_RELEASE
,
1043 (int)msg
->front
.iov_len
);
1045 spin_unlock(&session
->s_cap_lock
);
1049 * Queue cap releases when an inode is dropped from our cache. Since
1050 * inode is about to be destroyed, there is no need for i_ceph_lock.
1052 void ceph_queue_caps_release(struct inode
*inode
)
1054 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1057 p
= rb_first(&ci
->i_caps
);
1059 struct ceph_cap
*cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
1060 struct ceph_mds_session
*session
= cap
->session
;
1062 __queue_cap_release(session
, ceph_ino(inode
), cap
->cap_id
,
1063 cap
->mseq
, cap
->issue_seq
);
1065 __ceph_remove_cap(cap
);
1070 * Send a cap msg on the given inode. Update our caps state, then
1071 * drop i_ceph_lock and send the message.
1073 * Make note of max_size reported/requested from mds, revoked caps
1074 * that have now been implemented.
1076 * Make half-hearted attempt ot to invalidate page cache if we are
1077 * dropping RDCACHE. Note that this will leave behind locked pages
1078 * that we'll then need to deal with elsewhere.
1080 * Return non-zero if delayed release, or we experienced an error
1081 * such that the caller should requeue + retry later.
1083 * called with i_ceph_lock, then drops it.
1084 * caller should hold snap_rwsem (read), s_mutex.
1086 static int __send_cap(struct ceph_mds_client
*mdsc
, struct ceph_cap
*cap
,
1087 int op
, int used
, int want
, int retain
, int flushing
,
1088 unsigned *pflush_tid
)
1089 __releases(cap
->ci
->i_ceph_lock
)
1091 struct ceph_inode_info
*ci
= cap
->ci
;
1092 struct inode
*inode
= &ci
->vfs_inode
;
1093 u64 cap_id
= cap
->cap_id
;
1094 int held
, revoking
, dropping
, keep
;
1095 u64 seq
, issue_seq
, mseq
, time_warp_seq
, follows
;
1097 struct timespec mtime
, atime
;
1102 struct ceph_mds_session
*session
;
1103 u64 xattr_version
= 0;
1104 struct ceph_buffer
*xattr_blob
= NULL
;
1110 held
= cap
->issued
| cap
->implemented
;
1111 revoking
= cap
->implemented
& ~cap
->issued
;
1112 retain
&= ~revoking
;
1113 dropping
= cap
->issued
& ~retain
;
1115 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1116 inode
, cap
, cap
->session
,
1117 ceph_cap_string(held
), ceph_cap_string(held
& retain
),
1118 ceph_cap_string(revoking
));
1119 BUG_ON((retain
& CEPH_CAP_PIN
) == 0);
1121 session
= cap
->session
;
1123 /* don't release wanted unless we've waited a bit. */
1124 if ((ci
->i_ceph_flags
& CEPH_I_NODELAY
) == 0 &&
1125 time_before(jiffies
, ci
->i_hold_caps_min
)) {
1126 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1127 ceph_cap_string(cap
->issued
),
1128 ceph_cap_string(cap
->issued
& retain
),
1129 ceph_cap_string(cap
->mds_wanted
),
1130 ceph_cap_string(want
));
1131 want
|= cap
->mds_wanted
;
1132 retain
|= cap
->issued
;
1135 ci
->i_ceph_flags
&= ~(CEPH_I_NODELAY
| CEPH_I_FLUSH
);
1137 cap
->issued
&= retain
; /* drop bits we don't want */
1138 if (cap
->implemented
& ~cap
->issued
) {
1140 * Wake up any waiters on wanted -> needed transition.
1141 * This is due to the weird transition from buffered
1142 * to sync IO... we need to flush dirty pages _before_
1143 * allowing sync writes to avoid reordering.
1147 cap
->implemented
&= cap
->issued
| used
;
1148 cap
->mds_wanted
= want
;
1152 * assign a tid for flush operations so we can avoid
1153 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1154 * clean type races. track latest tid for every bit
1155 * so we can handle flush AxFw, flush Fw, and have the
1156 * first ack clean Ax.
1158 flush_tid
= ++ci
->i_cap_flush_last_tid
;
1160 *pflush_tid
= flush_tid
;
1161 dout(" cap_flush_tid %d\n", (int)flush_tid
);
1162 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
1163 if (flushing
& (1 << i
))
1164 ci
->i_cap_flush_tid
[i
] = flush_tid
;
1166 follows
= ci
->i_head_snapc
->seq
;
1171 keep
= cap
->implemented
;
1173 issue_seq
= cap
->issue_seq
;
1175 size
= inode
->i_size
;
1176 ci
->i_reported_size
= size
;
1177 max_size
= ci
->i_wanted_max_size
;
1178 ci
->i_requested_max_size
= max_size
;
1179 mtime
= inode
->i_mtime
;
1180 atime
= inode
->i_atime
;
1181 time_warp_seq
= ci
->i_time_warp_seq
;
1184 mode
= inode
->i_mode
;
1186 if (flushing
& CEPH_CAP_XATTR_EXCL
) {
1187 __ceph_build_xattrs_blob(ci
);
1188 xattr_blob
= ci
->i_xattrs
.blob
;
1189 xattr_version
= ci
->i_xattrs
.version
;
1192 spin_unlock(&ci
->i_ceph_lock
);
1194 ret
= send_cap_msg(session
, ceph_vino(inode
).ino
, cap_id
,
1195 op
, keep
, want
, flushing
, seq
, flush_tid
, issue_seq
, mseq
,
1196 size
, max_size
, &mtime
, &atime
, time_warp_seq
,
1197 uid
, gid
, mode
, xattr_version
, xattr_blob
,
1200 dout("error sending cap msg, must requeue %p\n", inode
);
1205 wake_up_all(&ci
->i_cap_wq
);
1211 * When a snapshot is taken, clients accumulate dirty metadata on
1212 * inodes with capabilities in ceph_cap_snaps to describe the file
1213 * state at the time the snapshot was taken. This must be flushed
1214 * asynchronously back to the MDS once sync writes complete and dirty
1215 * data is written out.
1217 * Unless @again is true, skip cap_snaps that were already sent to
1218 * the MDS (i.e., during this session).
1220 * Called under i_ceph_lock. Takes s_mutex as needed.
1222 void __ceph_flush_snaps(struct ceph_inode_info
*ci
,
1223 struct ceph_mds_session
**psession
,
1225 __releases(ci
->i_ceph_lock
)
1226 __acquires(ci
->i_ceph_lock
)
1228 struct inode
*inode
= &ci
->vfs_inode
;
1230 struct ceph_cap_snap
*capsnap
;
1232 struct ceph_mds_client
*mdsc
= ceph_inode_to_client(inode
)->mdsc
;
1233 struct ceph_mds_session
*session
= NULL
; /* if session != NULL, we hold
1235 u64 next_follows
= 0; /* keep track of how far we've gotten through the
1236 i_cap_snaps list, and skip these entries next time
1237 around to avoid an infinite loop */
1240 session
= *psession
;
1242 dout("__flush_snaps %p\n", inode
);
1244 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
1245 /* avoid an infiniute loop after retry */
1246 if (capsnap
->follows
< next_follows
)
1249 * we need to wait for sync writes to complete and for dirty
1250 * pages to be written out.
1252 if (capsnap
->dirty_pages
|| capsnap
->writing
)
1256 * if cap writeback already occurred, we should have dropped
1257 * the capsnap in ceph_put_wrbuffer_cap_refs.
1259 BUG_ON(capsnap
->dirty
== 0);
1261 /* pick mds, take s_mutex */
1262 if (ci
->i_auth_cap
== NULL
) {
1263 dout("no auth cap (migrating?), doing nothing\n");
1267 /* only flush each capsnap once */
1268 if (!again
&& !list_empty(&capsnap
->flushing_item
)) {
1269 dout("already flushed %p, skipping\n", capsnap
);
1273 mds
= ci
->i_auth_cap
->session
->s_mds
;
1274 mseq
= ci
->i_auth_cap
->mseq
;
1276 if (session
&& session
->s_mds
!= mds
) {
1277 dout("oops, wrong session %p mutex\n", session
);
1278 mutex_unlock(&session
->s_mutex
);
1279 ceph_put_mds_session(session
);
1283 spin_unlock(&ci
->i_ceph_lock
);
1284 mutex_lock(&mdsc
->mutex
);
1285 session
= __ceph_lookup_mds_session(mdsc
, mds
);
1286 mutex_unlock(&mdsc
->mutex
);
1288 dout("inverting session/ino locks on %p\n",
1290 mutex_lock(&session
->s_mutex
);
1293 * if session == NULL, we raced against a cap
1294 * deletion or migration. retry, and we'll
1295 * get a better @mds value next time.
1297 spin_lock(&ci
->i_ceph_lock
);
1301 capsnap
->flush_tid
= ++ci
->i_cap_flush_last_tid
;
1302 atomic_inc(&capsnap
->nref
);
1303 if (!list_empty(&capsnap
->flushing_item
))
1304 list_del_init(&capsnap
->flushing_item
);
1305 list_add_tail(&capsnap
->flushing_item
,
1306 &session
->s_cap_snaps_flushing
);
1307 spin_unlock(&ci
->i_ceph_lock
);
1309 dout("flush_snaps %p cap_snap %p follows %lld tid %llu\n",
1310 inode
, capsnap
, capsnap
->follows
, capsnap
->flush_tid
);
1311 send_cap_msg(session
, ceph_vino(inode
).ino
, 0,
1312 CEPH_CAP_OP_FLUSHSNAP
, capsnap
->issued
, 0,
1313 capsnap
->dirty
, 0, capsnap
->flush_tid
, 0, mseq
,
1315 &capsnap
->mtime
, &capsnap
->atime
,
1316 capsnap
->time_warp_seq
,
1317 capsnap
->uid
, capsnap
->gid
, capsnap
->mode
,
1318 capsnap
->xattr_version
, capsnap
->xattr_blob
,
1321 next_follows
= capsnap
->follows
+ 1;
1322 ceph_put_cap_snap(capsnap
);
1324 spin_lock(&ci
->i_ceph_lock
);
1328 /* we flushed them all; remove this inode from the queue */
1329 spin_lock(&mdsc
->snap_flush_lock
);
1330 list_del_init(&ci
->i_snap_flush_item
);
1331 spin_unlock(&mdsc
->snap_flush_lock
);
1335 *psession
= session
;
1337 mutex_unlock(&session
->s_mutex
);
1338 ceph_put_mds_session(session
);
1342 static void ceph_flush_snaps(struct ceph_inode_info
*ci
)
1344 spin_lock(&ci
->i_ceph_lock
);
1345 __ceph_flush_snaps(ci
, NULL
, 0);
1346 spin_unlock(&ci
->i_ceph_lock
);
1350 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1351 * Caller is then responsible for calling __mark_inode_dirty with the
1352 * returned flags value.
1354 int __ceph_mark_dirty_caps(struct ceph_inode_info
*ci
, int mask
)
1356 struct ceph_mds_client
*mdsc
=
1357 ceph_sb_to_client(ci
->vfs_inode
.i_sb
)->mdsc
;
1358 struct inode
*inode
= &ci
->vfs_inode
;
1359 int was
= ci
->i_dirty_caps
;
1362 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci
->vfs_inode
,
1363 ceph_cap_string(mask
), ceph_cap_string(was
),
1364 ceph_cap_string(was
| mask
));
1365 ci
->i_dirty_caps
|= mask
;
1367 if (!ci
->i_head_snapc
)
1368 ci
->i_head_snapc
= ceph_get_snap_context(
1369 ci
->i_snap_realm
->cached_context
);
1370 dout(" inode %p now dirty snapc %p auth cap %p\n",
1371 &ci
->vfs_inode
, ci
->i_head_snapc
, ci
->i_auth_cap
);
1372 BUG_ON(!list_empty(&ci
->i_dirty_item
));
1373 spin_lock(&mdsc
->cap_dirty_lock
);
1375 list_add(&ci
->i_dirty_item
, &mdsc
->cap_dirty
);
1377 list_add(&ci
->i_dirty_item
,
1378 &mdsc
->cap_dirty_migrating
);
1379 spin_unlock(&mdsc
->cap_dirty_lock
);
1380 if (ci
->i_flushing_caps
== 0) {
1382 dirty
|= I_DIRTY_SYNC
;
1385 BUG_ON(list_empty(&ci
->i_dirty_item
));
1386 if (((was
| ci
->i_flushing_caps
) & CEPH_CAP_FILE_BUFFER
) &&
1387 (mask
& CEPH_CAP_FILE_BUFFER
))
1388 dirty
|= I_DIRTY_DATASYNC
;
1389 __cap_delay_requeue(mdsc
, ci
);
1394 * Add dirty inode to the flushing list. Assigned a seq number so we
1395 * can wait for caps to flush without starving.
1397 * Called under i_ceph_lock.
1399 static int __mark_caps_flushing(struct inode
*inode
,
1400 struct ceph_mds_session
*session
)
1402 struct ceph_mds_client
*mdsc
= ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1403 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1406 BUG_ON(ci
->i_dirty_caps
== 0);
1407 BUG_ON(list_empty(&ci
->i_dirty_item
));
1409 flushing
= ci
->i_dirty_caps
;
1410 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1411 ceph_cap_string(flushing
),
1412 ceph_cap_string(ci
->i_flushing_caps
),
1413 ceph_cap_string(ci
->i_flushing_caps
| flushing
));
1414 ci
->i_flushing_caps
|= flushing
;
1415 ci
->i_dirty_caps
= 0;
1416 dout(" inode %p now !dirty\n", inode
);
1418 spin_lock(&mdsc
->cap_dirty_lock
);
1419 list_del_init(&ci
->i_dirty_item
);
1421 ci
->i_cap_flush_seq
= ++mdsc
->cap_flush_seq
;
1422 if (list_empty(&ci
->i_flushing_item
)) {
1423 list_add_tail(&ci
->i_flushing_item
, &session
->s_cap_flushing
);
1424 mdsc
->num_cap_flushing
++;
1425 dout(" inode %p now flushing seq %lld\n", inode
,
1426 ci
->i_cap_flush_seq
);
1428 list_move_tail(&ci
->i_flushing_item
, &session
->s_cap_flushing
);
1429 dout(" inode %p now flushing (more) seq %lld\n", inode
,
1430 ci
->i_cap_flush_seq
);
1432 spin_unlock(&mdsc
->cap_dirty_lock
);
1438 * try to invalidate mapping pages without blocking.
1440 static int try_nonblocking_invalidate(struct inode
*inode
)
1442 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1443 u32 invalidating_gen
= ci
->i_rdcache_gen
;
1445 spin_unlock(&ci
->i_ceph_lock
);
1446 invalidate_mapping_pages(&inode
->i_data
, 0, -1);
1447 spin_lock(&ci
->i_ceph_lock
);
1449 if (inode
->i_data
.nrpages
== 0 &&
1450 invalidating_gen
== ci
->i_rdcache_gen
) {
1452 dout("try_nonblocking_invalidate %p success\n", inode
);
1453 /* save any racing async invalidate some trouble */
1454 ci
->i_rdcache_revoking
= ci
->i_rdcache_gen
- 1;
1457 dout("try_nonblocking_invalidate %p failed\n", inode
);
1462 * Swiss army knife function to examine currently used and wanted
1463 * versus held caps. Release, flush, ack revoked caps to mds as
1466 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1467 * cap release further.
1468 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1469 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1472 void ceph_check_caps(struct ceph_inode_info
*ci
, int flags
,
1473 struct ceph_mds_session
*session
)
1475 struct ceph_fs_client
*fsc
= ceph_inode_to_client(&ci
->vfs_inode
);
1476 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
1477 struct inode
*inode
= &ci
->vfs_inode
;
1478 struct ceph_cap
*cap
;
1479 int file_wanted
, used
, cap_used
;
1480 int took_snap_rwsem
= 0; /* true if mdsc->snap_rwsem held */
1481 int issued
, implemented
, want
, retain
, revoking
, flushing
= 0;
1482 int mds
= -1; /* keep track of how far we've gone through i_caps list
1483 to avoid an infinite loop on retry */
1485 int tried_invalidate
= 0;
1486 int delayed
= 0, sent
= 0, force_requeue
= 0, num
;
1487 int queue_invalidate
= 0;
1488 int is_delayed
= flags
& CHECK_CAPS_NODELAY
;
1490 /* if we are unmounting, flush any unused caps immediately. */
1494 spin_lock(&ci
->i_ceph_lock
);
1496 if (ci
->i_ceph_flags
& CEPH_I_FLUSH
)
1497 flags
|= CHECK_CAPS_FLUSH
;
1499 /* flush snaps first time around only */
1500 if (!list_empty(&ci
->i_cap_snaps
))
1501 __ceph_flush_snaps(ci
, &session
, 0);
1504 spin_lock(&ci
->i_ceph_lock
);
1506 file_wanted
= __ceph_caps_file_wanted(ci
);
1507 used
= __ceph_caps_used(ci
);
1508 want
= file_wanted
| used
;
1509 issued
= __ceph_caps_issued(ci
, &implemented
);
1510 revoking
= implemented
& ~issued
;
1512 retain
= want
| CEPH_CAP_PIN
;
1513 if (!mdsc
->stopping
&& inode
->i_nlink
> 0) {
1515 retain
|= CEPH_CAP_ANY
; /* be greedy */
1517 retain
|= CEPH_CAP_ANY_SHARED
;
1519 * keep RD only if we didn't have the file open RW,
1520 * because then the mds would revoke it anyway to
1521 * journal max_size=0.
1523 if (ci
->i_max_size
== 0)
1524 retain
|= CEPH_CAP_ANY_RD
;
1528 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1529 " issued %s revoking %s retain %s %s%s%s\n", inode
,
1530 ceph_cap_string(file_wanted
),
1531 ceph_cap_string(used
), ceph_cap_string(ci
->i_dirty_caps
),
1532 ceph_cap_string(ci
->i_flushing_caps
),
1533 ceph_cap_string(issued
), ceph_cap_string(revoking
),
1534 ceph_cap_string(retain
),
1535 (flags
& CHECK_CAPS_AUTHONLY
) ? " AUTHONLY" : "",
1536 (flags
& CHECK_CAPS_NODELAY
) ? " NODELAY" : "",
1537 (flags
& CHECK_CAPS_FLUSH
) ? " FLUSH" : "");
1540 * If we no longer need to hold onto old our caps, and we may
1541 * have cached pages, but don't want them, then try to invalidate.
1542 * If we fail, it's because pages are locked.... try again later.
1544 if ((!is_delayed
|| mdsc
->stopping
) &&
1545 ci
->i_wrbuffer_ref
== 0 && /* no dirty pages... */
1546 inode
->i_data
.nrpages
&& /* have cached pages */
1547 (file_wanted
== 0 || /* no open files */
1548 (revoking
& (CEPH_CAP_FILE_CACHE
|
1549 CEPH_CAP_FILE_LAZYIO
))) && /* or revoking cache */
1550 !tried_invalidate
) {
1551 dout("check_caps trying to invalidate on %p\n", inode
);
1552 if (try_nonblocking_invalidate(inode
) < 0) {
1553 if (revoking
& (CEPH_CAP_FILE_CACHE
|
1554 CEPH_CAP_FILE_LAZYIO
)) {
1555 dout("check_caps queuing invalidate\n");
1556 queue_invalidate
= 1;
1557 ci
->i_rdcache_revoking
= ci
->i_rdcache_gen
;
1559 dout("check_caps failed to invalidate pages\n");
1560 /* we failed to invalidate pages. check these
1561 caps again later. */
1563 __cap_set_timeouts(mdsc
, ci
);
1566 tried_invalidate
= 1;
1571 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
1572 cap
= rb_entry(p
, struct ceph_cap
, ci_node
);
1575 /* avoid looping forever */
1576 if (mds
>= cap
->mds
||
1577 ((flags
& CHECK_CAPS_AUTHONLY
) && cap
!= ci
->i_auth_cap
))
1580 /* NOTE: no side-effects allowed, until we take s_mutex */
1583 if (ci
->i_auth_cap
&& cap
!= ci
->i_auth_cap
)
1584 cap_used
&= ~ci
->i_auth_cap
->issued
;
1586 revoking
= cap
->implemented
& ~cap
->issued
;
1587 dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
1588 cap
->mds
, cap
, ceph_cap_string(cap
->issued
),
1589 ceph_cap_string(cap_used
),
1590 ceph_cap_string(cap
->implemented
),
1591 ceph_cap_string(revoking
));
1593 if (cap
== ci
->i_auth_cap
&&
1594 (cap
->issued
& CEPH_CAP_FILE_WR
)) {
1595 /* request larger max_size from MDS? */
1596 if (ci
->i_wanted_max_size
> ci
->i_max_size
&&
1597 ci
->i_wanted_max_size
> ci
->i_requested_max_size
) {
1598 dout("requesting new max_size\n");
1602 /* approaching file_max? */
1603 if ((inode
->i_size
<< 1) >= ci
->i_max_size
&&
1604 (ci
->i_reported_size
<< 1) < ci
->i_max_size
) {
1605 dout("i_size approaching max_size\n");
1609 /* flush anything dirty? */
1610 if (cap
== ci
->i_auth_cap
&& (flags
& CHECK_CAPS_FLUSH
) &&
1612 dout("flushing dirty caps\n");
1616 /* completed revocation? going down and there are no caps? */
1617 if (revoking
&& (revoking
& cap_used
) == 0) {
1618 dout("completed revocation of %s\n",
1619 ceph_cap_string(cap
->implemented
& ~cap
->issued
));
1623 /* want more caps from mds? */
1624 if (want
& ~(cap
->mds_wanted
| cap
->issued
))
1627 /* things we might delay */
1628 if ((cap
->issued
& ~retain
) == 0 &&
1629 cap
->mds_wanted
== want
)
1630 continue; /* nope, all good */
1636 if ((ci
->i_ceph_flags
& CEPH_I_NODELAY
) == 0 &&
1637 time_before(jiffies
, ci
->i_hold_caps_max
)) {
1638 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1639 ceph_cap_string(cap
->issued
),
1640 ceph_cap_string(cap
->issued
& retain
),
1641 ceph_cap_string(cap
->mds_wanted
),
1642 ceph_cap_string(want
));
1648 if (ci
->i_ceph_flags
& CEPH_I_NOFLUSH
) {
1649 dout(" skipping %p I_NOFLUSH set\n", inode
);
1653 if (session
&& session
!= cap
->session
) {
1654 dout("oops, wrong session %p mutex\n", session
);
1655 mutex_unlock(&session
->s_mutex
);
1659 session
= cap
->session
;
1660 if (mutex_trylock(&session
->s_mutex
) == 0) {
1661 dout("inverting session/ino locks on %p\n",
1663 spin_unlock(&ci
->i_ceph_lock
);
1664 if (took_snap_rwsem
) {
1665 up_read(&mdsc
->snap_rwsem
);
1666 took_snap_rwsem
= 0;
1668 mutex_lock(&session
->s_mutex
);
1672 /* take snap_rwsem after session mutex */
1673 if (!took_snap_rwsem
) {
1674 if (down_read_trylock(&mdsc
->snap_rwsem
) == 0) {
1675 dout("inverting snap/in locks on %p\n",
1677 spin_unlock(&ci
->i_ceph_lock
);
1678 down_read(&mdsc
->snap_rwsem
);
1679 took_snap_rwsem
= 1;
1682 took_snap_rwsem
= 1;
1685 if (cap
== ci
->i_auth_cap
&& ci
->i_dirty_caps
)
1686 flushing
= __mark_caps_flushing(inode
, session
);
1690 mds
= cap
->mds
; /* remember mds, so we don't repeat */
1693 /* __send_cap drops i_ceph_lock */
1694 delayed
+= __send_cap(mdsc
, cap
, CEPH_CAP_OP_UPDATE
, cap_used
,
1695 want
, retain
, flushing
, NULL
);
1696 goto retry
; /* retake i_ceph_lock and restart our cap scan. */
1700 * Reschedule delayed caps release if we delayed anything,
1703 if (delayed
&& is_delayed
)
1704 force_requeue
= 1; /* __send_cap delayed release; requeue */
1705 if (!delayed
&& !is_delayed
)
1706 __cap_delay_cancel(mdsc
, ci
);
1707 else if (!is_delayed
|| force_requeue
)
1708 __cap_delay_requeue(mdsc
, ci
);
1710 spin_unlock(&ci
->i_ceph_lock
);
1712 if (queue_invalidate
)
1713 ceph_queue_invalidate(inode
);
1716 mutex_unlock(&session
->s_mutex
);
1717 if (took_snap_rwsem
)
1718 up_read(&mdsc
->snap_rwsem
);
1722 * Try to flush dirty caps back to the auth mds.
1724 static int try_flush_caps(struct inode
*inode
, struct ceph_mds_session
*session
,
1725 unsigned *flush_tid
)
1727 struct ceph_mds_client
*mdsc
= ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1728 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1729 int unlock_session
= session
? 0 : 1;
1733 spin_lock(&ci
->i_ceph_lock
);
1734 if (ci
->i_ceph_flags
& CEPH_I_NOFLUSH
) {
1735 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode
);
1738 if (ci
->i_dirty_caps
&& ci
->i_auth_cap
) {
1739 struct ceph_cap
*cap
= ci
->i_auth_cap
;
1740 int used
= __ceph_caps_used(ci
);
1741 int want
= __ceph_caps_wanted(ci
);
1745 spin_unlock(&ci
->i_ceph_lock
);
1746 session
= cap
->session
;
1747 mutex_lock(&session
->s_mutex
);
1750 BUG_ON(session
!= cap
->session
);
1751 if (cap
->session
->s_state
< CEPH_MDS_SESSION_OPEN
)
1754 flushing
= __mark_caps_flushing(inode
, session
);
1756 /* __send_cap drops i_ceph_lock */
1757 delayed
= __send_cap(mdsc
, cap
, CEPH_CAP_OP_FLUSH
, used
, want
,
1758 cap
->issued
| cap
->implemented
, flushing
,
1763 spin_lock(&ci
->i_ceph_lock
);
1764 __cap_delay_requeue(mdsc
, ci
);
1767 spin_unlock(&ci
->i_ceph_lock
);
1769 if (session
&& unlock_session
)
1770 mutex_unlock(&session
->s_mutex
);
1775 * Return true if we've flushed caps through the given flush_tid.
1777 static int caps_are_flushed(struct inode
*inode
, unsigned tid
)
1779 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1782 spin_lock(&ci
->i_ceph_lock
);
1783 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
1784 if ((ci
->i_flushing_caps
& (1 << i
)) &&
1785 ci
->i_cap_flush_tid
[i
] <= tid
) {
1786 /* still flushing this bit */
1790 spin_unlock(&ci
->i_ceph_lock
);
1795 * Wait on any unsafe replies for the given inode. First wait on the
1796 * newest request, and make that the upper bound. Then, if there are
1797 * more requests, keep waiting on the oldest as long as it is still older
1798 * than the original request.
1800 static void sync_write_wait(struct inode
*inode
)
1802 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1803 struct list_head
*head
= &ci
->i_unsafe_writes
;
1804 struct ceph_osd_request
*req
;
1807 spin_lock(&ci
->i_unsafe_lock
);
1808 if (list_empty(head
))
1811 /* set upper bound as _last_ entry in chain */
1812 req
= list_entry(head
->prev
, struct ceph_osd_request
,
1814 last_tid
= req
->r_tid
;
1817 ceph_osdc_get_request(req
);
1818 spin_unlock(&ci
->i_unsafe_lock
);
1819 dout("sync_write_wait on tid %llu (until %llu)\n",
1820 req
->r_tid
, last_tid
);
1821 wait_for_completion(&req
->r_safe_completion
);
1822 spin_lock(&ci
->i_unsafe_lock
);
1823 ceph_osdc_put_request(req
);
1826 * from here on look at first entry in chain, since we
1827 * only want to wait for anything older than last_tid
1829 if (list_empty(head
))
1831 req
= list_entry(head
->next
, struct ceph_osd_request
,
1833 } while (req
->r_tid
< last_tid
);
1835 spin_unlock(&ci
->i_unsafe_lock
);
1838 int ceph_fsync(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
1840 struct inode
*inode
= file
->f_mapping
->host
;
1841 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1846 dout("fsync %p%s\n", inode
, datasync
? " datasync" : "");
1847 sync_write_wait(inode
);
1849 ret
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
1852 mutex_lock(&inode
->i_mutex
);
1854 dirty
= try_flush_caps(inode
, NULL
, &flush_tid
);
1855 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty
));
1858 * only wait on non-file metadata writeback (the mds
1859 * can recover size and mtime, so we don't need to
1862 if (!datasync
&& (dirty
& ~CEPH_CAP_ANY_FILE_WR
)) {
1863 dout("fsync waiting for flush_tid %u\n", flush_tid
);
1864 ret
= wait_event_interruptible(ci
->i_cap_wq
,
1865 caps_are_flushed(inode
, flush_tid
));
1868 dout("fsync %p%s done\n", inode
, datasync
? " datasync" : "");
1869 mutex_unlock(&inode
->i_mutex
);
1874 * Flush any dirty caps back to the mds. If we aren't asked to wait,
1875 * queue inode for flush but don't do so immediately, because we can
1876 * get by with fewer MDS messages if we wait for data writeback to
1879 int ceph_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1881 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1885 int wait
= wbc
->sync_mode
== WB_SYNC_ALL
;
1887 dout("write_inode %p wait=%d\n", inode
, wait
);
1889 dirty
= try_flush_caps(inode
, NULL
, &flush_tid
);
1891 err
= wait_event_interruptible(ci
->i_cap_wq
,
1892 caps_are_flushed(inode
, flush_tid
));
1894 struct ceph_mds_client
*mdsc
=
1895 ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1897 spin_lock(&ci
->i_ceph_lock
);
1898 if (__ceph_caps_dirty(ci
))
1899 __cap_delay_requeue_front(mdsc
, ci
);
1900 spin_unlock(&ci
->i_ceph_lock
);
1906 * After a recovering MDS goes active, we need to resend any caps
1909 * Caller holds session->s_mutex.
1911 static void kick_flushing_capsnaps(struct ceph_mds_client
*mdsc
,
1912 struct ceph_mds_session
*session
)
1914 struct ceph_cap_snap
*capsnap
;
1916 dout("kick_flushing_capsnaps mds%d\n", session
->s_mds
);
1917 list_for_each_entry(capsnap
, &session
->s_cap_snaps_flushing
,
1919 struct ceph_inode_info
*ci
= capsnap
->ci
;
1920 struct inode
*inode
= &ci
->vfs_inode
;
1921 struct ceph_cap
*cap
;
1923 spin_lock(&ci
->i_ceph_lock
);
1924 cap
= ci
->i_auth_cap
;
1925 if (cap
&& cap
->session
== session
) {
1926 dout("kick_flushing_caps %p cap %p capsnap %p\n", inode
,
1928 __ceph_flush_snaps(ci
, &session
, 1);
1930 pr_err("%p auth cap %p not mds%d ???\n", inode
,
1931 cap
, session
->s_mds
);
1933 spin_unlock(&ci
->i_ceph_lock
);
1937 void ceph_kick_flushing_caps(struct ceph_mds_client
*mdsc
,
1938 struct ceph_mds_session
*session
)
1940 struct ceph_inode_info
*ci
;
1942 kick_flushing_capsnaps(mdsc
, session
);
1944 dout("kick_flushing_caps mds%d\n", session
->s_mds
);
1945 list_for_each_entry(ci
, &session
->s_cap_flushing
, i_flushing_item
) {
1946 struct inode
*inode
= &ci
->vfs_inode
;
1947 struct ceph_cap
*cap
;
1950 spin_lock(&ci
->i_ceph_lock
);
1951 cap
= ci
->i_auth_cap
;
1952 if (cap
&& cap
->session
== session
) {
1953 dout("kick_flushing_caps %p cap %p %s\n", inode
,
1954 cap
, ceph_cap_string(ci
->i_flushing_caps
));
1955 delayed
= __send_cap(mdsc
, cap
, CEPH_CAP_OP_FLUSH
,
1956 __ceph_caps_used(ci
),
1957 __ceph_caps_wanted(ci
),
1958 cap
->issued
| cap
->implemented
,
1959 ci
->i_flushing_caps
, NULL
);
1961 spin_lock(&ci
->i_ceph_lock
);
1962 __cap_delay_requeue(mdsc
, ci
);
1963 spin_unlock(&ci
->i_ceph_lock
);
1966 pr_err("%p auth cap %p not mds%d ???\n", inode
,
1967 cap
, session
->s_mds
);
1968 spin_unlock(&ci
->i_ceph_lock
);
1973 static void kick_flushing_inode_caps(struct ceph_mds_client
*mdsc
,
1974 struct ceph_mds_session
*session
,
1975 struct inode
*inode
)
1977 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1978 struct ceph_cap
*cap
;
1981 spin_lock(&ci
->i_ceph_lock
);
1982 cap
= ci
->i_auth_cap
;
1983 dout("kick_flushing_inode_caps %p flushing %s flush_seq %lld\n", inode
,
1984 ceph_cap_string(ci
->i_flushing_caps
), ci
->i_cap_flush_seq
);
1986 __ceph_flush_snaps(ci
, &session
, 1);
1988 if (ci
->i_flushing_caps
) {
1989 spin_lock(&mdsc
->cap_dirty_lock
);
1990 list_move_tail(&ci
->i_flushing_item
,
1991 &cap
->session
->s_cap_flushing
);
1992 spin_unlock(&mdsc
->cap_dirty_lock
);
1994 delayed
= __send_cap(mdsc
, cap
, CEPH_CAP_OP_FLUSH
,
1995 __ceph_caps_used(ci
),
1996 __ceph_caps_wanted(ci
),
1997 cap
->issued
| cap
->implemented
,
1998 ci
->i_flushing_caps
, NULL
);
2000 spin_lock(&ci
->i_ceph_lock
);
2001 __cap_delay_requeue(mdsc
, ci
);
2002 spin_unlock(&ci
->i_ceph_lock
);
2005 spin_unlock(&ci
->i_ceph_lock
);
2011 * Take references to capabilities we hold, so that we don't release
2012 * them to the MDS prematurely.
2014 * Protected by i_ceph_lock.
2016 static void __take_cap_refs(struct ceph_inode_info
*ci
, int got
)
2018 if (got
& CEPH_CAP_PIN
)
2020 if (got
& CEPH_CAP_FILE_RD
)
2022 if (got
& CEPH_CAP_FILE_CACHE
)
2023 ci
->i_rdcache_ref
++;
2024 if (got
& CEPH_CAP_FILE_WR
)
2026 if (got
& CEPH_CAP_FILE_BUFFER
) {
2027 if (ci
->i_wb_ref
== 0)
2028 ihold(&ci
->vfs_inode
);
2030 dout("__take_cap_refs %p wb %d -> %d (?)\n",
2031 &ci
->vfs_inode
, ci
->i_wb_ref
-1, ci
->i_wb_ref
);
2036 * Try to grab cap references. Specify those refs we @want, and the
2037 * minimal set we @need. Also include the larger offset we are writing
2038 * to (when applicable), and check against max_size here as well.
2039 * Note that caller is responsible for ensuring max_size increases are
2040 * requested from the MDS.
2042 static int try_get_cap_refs(struct ceph_inode_info
*ci
, int need
, int want
,
2043 int *got
, loff_t endoff
, int *check_max
, int *err
)
2045 struct inode
*inode
= &ci
->vfs_inode
;
2047 int have
, implemented
;
2050 dout("get_cap_refs %p need %s want %s\n", inode
,
2051 ceph_cap_string(need
), ceph_cap_string(want
));
2052 spin_lock(&ci
->i_ceph_lock
);
2054 /* make sure file is actually open */
2055 file_wanted
= __ceph_caps_file_wanted(ci
);
2056 if ((file_wanted
& need
) == 0) {
2057 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2058 ceph_cap_string(need
), ceph_cap_string(file_wanted
));
2064 /* finish pending truncate */
2065 while (ci
->i_truncate_pending
) {
2066 spin_unlock(&ci
->i_ceph_lock
);
2067 __ceph_do_pending_vmtruncate(inode
, !(need
& CEPH_CAP_FILE_WR
));
2068 spin_lock(&ci
->i_ceph_lock
);
2071 if (need
& CEPH_CAP_FILE_WR
) {
2072 if (endoff
>= 0 && endoff
> (loff_t
)ci
->i_max_size
) {
2073 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2074 inode
, endoff
, ci
->i_max_size
);
2075 if (endoff
> ci
->i_wanted_max_size
) {
2082 * If a sync write is in progress, we must wait, so that we
2083 * can get a final snapshot value for size+mtime.
2085 if (__ceph_have_pending_cap_snap(ci
)) {
2086 dout("get_cap_refs %p cap_snap_pending\n", inode
);
2090 have
= __ceph_caps_issued(ci
, &implemented
);
2092 if ((have
& need
) == need
) {
2094 * Look at (implemented & ~have & not) so that we keep waiting
2095 * on transition from wanted -> needed caps. This is needed
2096 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2097 * going before a prior buffered writeback happens.
2099 int not = want
& ~(have
& need
);
2100 int revoking
= implemented
& ~have
;
2101 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2102 inode
, ceph_cap_string(have
), ceph_cap_string(not),
2103 ceph_cap_string(revoking
));
2104 if ((revoking
& not) == 0) {
2105 *got
= need
| (have
& want
);
2106 __take_cap_refs(ci
, *got
);
2110 dout("get_cap_refs %p have %s needed %s\n", inode
,
2111 ceph_cap_string(have
), ceph_cap_string(need
));
2114 spin_unlock(&ci
->i_ceph_lock
);
2115 dout("get_cap_refs %p ret %d got %s\n", inode
,
2116 ret
, ceph_cap_string(*got
));
2121 * Check the offset we are writing up to against our current
2122 * max_size. If necessary, tell the MDS we want to write to
2125 static void check_max_size(struct inode
*inode
, loff_t endoff
)
2127 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2130 /* do we need to explicitly request a larger max_size? */
2131 spin_lock(&ci
->i_ceph_lock
);
2132 if ((endoff
>= ci
->i_max_size
||
2133 endoff
> (inode
->i_size
<< 1)) &&
2134 endoff
> ci
->i_wanted_max_size
) {
2135 dout("write %p at large endoff %llu, req max_size\n",
2137 ci
->i_wanted_max_size
= endoff
;
2140 spin_unlock(&ci
->i_ceph_lock
);
2142 ceph_check_caps(ci
, CHECK_CAPS_AUTHONLY
, NULL
);
2146 * Wait for caps, and take cap references. If we can't get a WR cap
2147 * due to a small max_size, make sure we check_max_size (and possibly
2148 * ask the mds) so we don't get hung up indefinitely.
2150 int ceph_get_caps(struct ceph_inode_info
*ci
, int need
, int want
, int *got
,
2153 int check_max
, ret
, err
;
2157 check_max_size(&ci
->vfs_inode
, endoff
);
2160 ret
= wait_event_interruptible(ci
->i_cap_wq
,
2161 try_get_cap_refs(ci
, need
, want
,
2172 * Take cap refs. Caller must already know we hold at least one ref
2173 * on the caps in question or we don't know this is safe.
2175 void ceph_get_cap_refs(struct ceph_inode_info
*ci
, int caps
)
2177 spin_lock(&ci
->i_ceph_lock
);
2178 __take_cap_refs(ci
, caps
);
2179 spin_unlock(&ci
->i_ceph_lock
);
2185 * If we released the last ref on any given cap, call ceph_check_caps
2186 * to release (or schedule a release).
2188 * If we are releasing a WR cap (from a sync write), finalize any affected
2189 * cap_snap, and wake up any waiters.
2191 void ceph_put_cap_refs(struct ceph_inode_info
*ci
, int had
)
2193 struct inode
*inode
= &ci
->vfs_inode
;
2194 int last
= 0, put
= 0, flushsnaps
= 0, wake
= 0;
2195 struct ceph_cap_snap
*capsnap
;
2197 spin_lock(&ci
->i_ceph_lock
);
2198 if (had
& CEPH_CAP_PIN
)
2200 if (had
& CEPH_CAP_FILE_RD
)
2201 if (--ci
->i_rd_ref
== 0)
2203 if (had
& CEPH_CAP_FILE_CACHE
)
2204 if (--ci
->i_rdcache_ref
== 0)
2206 if (had
& CEPH_CAP_FILE_BUFFER
) {
2207 if (--ci
->i_wb_ref
== 0) {
2211 dout("put_cap_refs %p wb %d -> %d (?)\n",
2212 inode
, ci
->i_wb_ref
+1, ci
->i_wb_ref
);
2214 if (had
& CEPH_CAP_FILE_WR
)
2215 if (--ci
->i_wr_ref
== 0) {
2217 if (!list_empty(&ci
->i_cap_snaps
)) {
2218 capsnap
= list_first_entry(&ci
->i_cap_snaps
,
2219 struct ceph_cap_snap
,
2221 if (capsnap
->writing
) {
2222 capsnap
->writing
= 0;
2224 __ceph_finish_cap_snap(ci
,
2230 spin_unlock(&ci
->i_ceph_lock
);
2232 dout("put_cap_refs %p had %s%s%s\n", inode
, ceph_cap_string(had
),
2233 last
? " last" : "", put
? " put" : "");
2235 if (last
&& !flushsnaps
)
2236 ceph_check_caps(ci
, 0, NULL
);
2237 else if (flushsnaps
)
2238 ceph_flush_snaps(ci
);
2240 wake_up_all(&ci
->i_cap_wq
);
2246 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2247 * context. Adjust per-snap dirty page accounting as appropriate.
2248 * Once all dirty data for a cap_snap is flushed, flush snapped file
2249 * metadata back to the MDS. If we dropped the last ref, call
2252 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info
*ci
, int nr
,
2253 struct ceph_snap_context
*snapc
)
2255 struct inode
*inode
= &ci
->vfs_inode
;
2257 int complete_capsnap
= 0;
2258 int drop_capsnap
= 0;
2260 struct ceph_cap_snap
*capsnap
= NULL
;
2262 spin_lock(&ci
->i_ceph_lock
);
2263 ci
->i_wrbuffer_ref
-= nr
;
2264 last
= !ci
->i_wrbuffer_ref
;
2266 if (ci
->i_head_snapc
== snapc
) {
2267 ci
->i_wrbuffer_ref_head
-= nr
;
2268 if (ci
->i_wrbuffer_ref_head
== 0 &&
2269 ci
->i_dirty_caps
== 0 && ci
->i_flushing_caps
== 0) {
2270 BUG_ON(!ci
->i_head_snapc
);
2271 ceph_put_snap_context(ci
->i_head_snapc
);
2272 ci
->i_head_snapc
= NULL
;
2274 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2276 ci
->i_wrbuffer_ref
+nr
, ci
->i_wrbuffer_ref_head
+nr
,
2277 ci
->i_wrbuffer_ref
, ci
->i_wrbuffer_ref_head
,
2278 last
? " LAST" : "");
2280 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
2281 if (capsnap
->context
== snapc
) {
2287 capsnap
->dirty_pages
-= nr
;
2288 if (capsnap
->dirty_pages
== 0) {
2289 complete_capsnap
= 1;
2290 if (capsnap
->dirty
== 0)
2291 /* cap writeback completed before we created
2292 * the cap_snap; no FLUSHSNAP is needed */
2295 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2296 " snap %lld %d/%d -> %d/%d %s%s%s\n",
2297 inode
, capsnap
, capsnap
->context
->seq
,
2298 ci
->i_wrbuffer_ref
+nr
, capsnap
->dirty_pages
+ nr
,
2299 ci
->i_wrbuffer_ref
, capsnap
->dirty_pages
,
2300 last
? " (wrbuffer last)" : "",
2301 complete_capsnap
? " (complete capsnap)" : "",
2302 drop_capsnap
? " (drop capsnap)" : "");
2304 ceph_put_snap_context(capsnap
->context
);
2305 list_del(&capsnap
->ci_item
);
2306 list_del(&capsnap
->flushing_item
);
2307 ceph_put_cap_snap(capsnap
);
2311 spin_unlock(&ci
->i_ceph_lock
);
2314 ceph_check_caps(ci
, CHECK_CAPS_AUTHONLY
, NULL
);
2316 } else if (complete_capsnap
) {
2317 ceph_flush_snaps(ci
);
2318 wake_up_all(&ci
->i_cap_wq
);
2325 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2326 * actually be a revocation if it specifies a smaller cap set.)
2328 * caller holds s_mutex and i_ceph_lock, we drop both.
2332 * 1 - check_caps on auth cap only (writeback)
2333 * 2 - check_caps (ack revoke)
2335 static void handle_cap_grant(struct inode
*inode
, struct ceph_mds_caps
*grant
,
2336 struct ceph_mds_session
*session
,
2337 struct ceph_cap
*cap
,
2338 struct ceph_buffer
*xattr_buf
)
2339 __releases(ci
->i_ceph_lock
)
2341 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2342 int mds
= session
->s_mds
;
2343 int seq
= le32_to_cpu(grant
->seq
);
2344 int newcaps
= le32_to_cpu(grant
->caps
);
2345 int issued
, implemented
, used
, wanted
, dirty
;
2346 u64 size
= le64_to_cpu(grant
->size
);
2347 u64 max_size
= le64_to_cpu(grant
->max_size
);
2348 struct timespec mtime
, atime
, ctime
;
2352 int revoked_rdcache
= 0;
2353 int queue_invalidate
= 0;
2355 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2356 inode
, cap
, mds
, seq
, ceph_cap_string(newcaps
));
2357 dout(" size %llu max_size %llu, i_size %llu\n", size
, max_size
,
2361 * If CACHE is being revoked, and we have no dirty buffers,
2362 * try to invalidate (once). (If there are dirty buffers, we
2363 * will invalidate _after_ writeback.)
2365 if (((cap
->issued
& ~newcaps
) & CEPH_CAP_FILE_CACHE
) &&
2366 (newcaps
& CEPH_CAP_FILE_LAZYIO
) == 0 &&
2367 !ci
->i_wrbuffer_ref
) {
2368 if (try_nonblocking_invalidate(inode
) == 0) {
2369 revoked_rdcache
= 1;
2371 /* there were locked pages.. invalidate later
2372 in a separate thread. */
2373 if (ci
->i_rdcache_revoking
!= ci
->i_rdcache_gen
) {
2374 queue_invalidate
= 1;
2375 ci
->i_rdcache_revoking
= ci
->i_rdcache_gen
;
2380 /* side effects now are allowed */
2382 issued
= __ceph_caps_issued(ci
, &implemented
);
2383 issued
|= implemented
| __ceph_caps_dirty(ci
);
2385 cap
->cap_gen
= session
->s_cap_gen
;
2387 __check_cap_issue(ci
, cap
, newcaps
);
2389 if ((issued
& CEPH_CAP_AUTH_EXCL
) == 0) {
2390 inode
->i_mode
= le32_to_cpu(grant
->mode
);
2391 inode
->i_uid
= make_kuid(&init_user_ns
, le32_to_cpu(grant
->uid
));
2392 inode
->i_gid
= make_kgid(&init_user_ns
, le32_to_cpu(grant
->gid
));
2393 dout("%p mode 0%o uid.gid %d.%d\n", inode
, inode
->i_mode
,
2394 from_kuid(&init_user_ns
, inode
->i_uid
),
2395 from_kgid(&init_user_ns
, inode
->i_gid
));
2398 if ((issued
& CEPH_CAP_LINK_EXCL
) == 0)
2399 set_nlink(inode
, le32_to_cpu(grant
->nlink
));
2401 if ((issued
& CEPH_CAP_XATTR_EXCL
) == 0 && grant
->xattr_len
) {
2402 int len
= le32_to_cpu(grant
->xattr_len
);
2403 u64 version
= le64_to_cpu(grant
->xattr_version
);
2405 if (version
> ci
->i_xattrs
.version
) {
2406 dout(" got new xattrs v%llu on %p len %d\n",
2407 version
, inode
, len
);
2408 if (ci
->i_xattrs
.blob
)
2409 ceph_buffer_put(ci
->i_xattrs
.blob
);
2410 ci
->i_xattrs
.blob
= ceph_buffer_get(xattr_buf
);
2411 ci
->i_xattrs
.version
= version
;
2415 /* size/ctime/mtime/atime? */
2416 ceph_fill_file_size(inode
, issued
,
2417 le32_to_cpu(grant
->truncate_seq
),
2418 le64_to_cpu(grant
->truncate_size
), size
);
2419 ceph_decode_timespec(&mtime
, &grant
->mtime
);
2420 ceph_decode_timespec(&atime
, &grant
->atime
);
2421 ceph_decode_timespec(&ctime
, &grant
->ctime
);
2422 ceph_fill_file_time(inode
, issued
,
2423 le32_to_cpu(grant
->time_warp_seq
), &ctime
, &mtime
,
2426 /* max size increase? */
2427 if (ci
->i_auth_cap
== cap
&& max_size
!= ci
->i_max_size
) {
2428 dout("max_size %lld -> %llu\n", ci
->i_max_size
, max_size
);
2429 ci
->i_max_size
= max_size
;
2430 if (max_size
>= ci
->i_wanted_max_size
) {
2431 ci
->i_wanted_max_size
= 0; /* reset */
2432 ci
->i_requested_max_size
= 0;
2437 /* check cap bits */
2438 wanted
= __ceph_caps_wanted(ci
);
2439 used
= __ceph_caps_used(ci
);
2440 dirty
= __ceph_caps_dirty(ci
);
2441 dout(" my wanted = %s, used = %s, dirty %s\n",
2442 ceph_cap_string(wanted
),
2443 ceph_cap_string(used
),
2444 ceph_cap_string(dirty
));
2445 if (wanted
!= le32_to_cpu(grant
->wanted
)) {
2446 dout("mds wanted %s -> %s\n",
2447 ceph_cap_string(le32_to_cpu(grant
->wanted
)),
2448 ceph_cap_string(wanted
));
2449 /* imported cap may not have correct mds_wanted */
2450 if (le32_to_cpu(grant
->op
) == CEPH_CAP_OP_IMPORT
)
2456 /* file layout may have changed */
2457 ci
->i_layout
= grant
->layout
;
2459 /* revocation, grant, or no-op? */
2460 if (cap
->issued
& ~newcaps
) {
2461 int revoking
= cap
->issued
& ~newcaps
;
2463 dout("revocation: %s -> %s (revoking %s)\n",
2464 ceph_cap_string(cap
->issued
),
2465 ceph_cap_string(newcaps
),
2466 ceph_cap_string(revoking
));
2467 if (revoking
& used
& CEPH_CAP_FILE_BUFFER
)
2468 writeback
= 1; /* initiate writeback; will delay ack */
2469 else if (revoking
== CEPH_CAP_FILE_CACHE
&&
2470 (newcaps
& CEPH_CAP_FILE_LAZYIO
) == 0 &&
2472 ; /* do nothing yet, invalidation will be queued */
2473 else if (cap
== ci
->i_auth_cap
)
2474 check_caps
= 1; /* check auth cap only */
2476 check_caps
= 2; /* check all caps */
2477 cap
->issued
= newcaps
;
2478 cap
->implemented
|= newcaps
;
2479 } else if (cap
->issued
== newcaps
) {
2480 dout("caps unchanged: %s -> %s\n",
2481 ceph_cap_string(cap
->issued
), ceph_cap_string(newcaps
));
2483 dout("grant: %s -> %s\n", ceph_cap_string(cap
->issued
),
2484 ceph_cap_string(newcaps
));
2485 cap
->issued
= newcaps
;
2486 cap
->implemented
|= newcaps
; /* add bits only, to
2487 * avoid stepping on a
2488 * pending revocation */
2491 BUG_ON(cap
->issued
& ~cap
->implemented
);
2493 spin_unlock(&ci
->i_ceph_lock
);
2496 * queue inode for writeback: we can't actually call
2497 * filemap_write_and_wait, etc. from message handler
2500 ceph_queue_writeback(inode
);
2501 if (queue_invalidate
)
2502 ceph_queue_invalidate(inode
);
2504 wake_up_all(&ci
->i_cap_wq
);
2506 if (check_caps
== 1)
2507 ceph_check_caps(ci
, CHECK_CAPS_NODELAY
|CHECK_CAPS_AUTHONLY
,
2509 else if (check_caps
== 2)
2510 ceph_check_caps(ci
, CHECK_CAPS_NODELAY
, session
);
2512 mutex_unlock(&session
->s_mutex
);
2516 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2517 * MDS has been safely committed.
2519 static void handle_cap_flush_ack(struct inode
*inode
, u64 flush_tid
,
2520 struct ceph_mds_caps
*m
,
2521 struct ceph_mds_session
*session
,
2522 struct ceph_cap
*cap
)
2523 __releases(ci
->i_ceph_lock
)
2525 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2526 struct ceph_mds_client
*mdsc
= ceph_sb_to_client(inode
->i_sb
)->mdsc
;
2527 unsigned seq
= le32_to_cpu(m
->seq
);
2528 int dirty
= le32_to_cpu(m
->dirty
);
2533 for (i
= 0; i
< CEPH_CAP_BITS
; i
++)
2534 if ((dirty
& (1 << i
)) &&
2535 flush_tid
== ci
->i_cap_flush_tid
[i
])
2538 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2539 " flushing %s -> %s\n",
2540 inode
, session
->s_mds
, seq
, ceph_cap_string(dirty
),
2541 ceph_cap_string(cleaned
), ceph_cap_string(ci
->i_flushing_caps
),
2542 ceph_cap_string(ci
->i_flushing_caps
& ~cleaned
));
2544 if (ci
->i_flushing_caps
== (ci
->i_flushing_caps
& ~cleaned
))
2547 ci
->i_flushing_caps
&= ~cleaned
;
2549 spin_lock(&mdsc
->cap_dirty_lock
);
2550 if (ci
->i_flushing_caps
== 0) {
2551 list_del_init(&ci
->i_flushing_item
);
2552 if (!list_empty(&session
->s_cap_flushing
))
2553 dout(" mds%d still flushing cap on %p\n",
2555 &list_entry(session
->s_cap_flushing
.next
,
2556 struct ceph_inode_info
,
2557 i_flushing_item
)->vfs_inode
);
2558 mdsc
->num_cap_flushing
--;
2559 wake_up_all(&mdsc
->cap_flushing_wq
);
2560 dout(" inode %p now !flushing\n", inode
);
2562 if (ci
->i_dirty_caps
== 0) {
2563 dout(" inode %p now clean\n", inode
);
2564 BUG_ON(!list_empty(&ci
->i_dirty_item
));
2566 if (ci
->i_wrbuffer_ref_head
== 0) {
2567 BUG_ON(!ci
->i_head_snapc
);
2568 ceph_put_snap_context(ci
->i_head_snapc
);
2569 ci
->i_head_snapc
= NULL
;
2572 BUG_ON(list_empty(&ci
->i_dirty_item
));
2575 spin_unlock(&mdsc
->cap_dirty_lock
);
2576 wake_up_all(&ci
->i_cap_wq
);
2579 spin_unlock(&ci
->i_ceph_lock
);
2585 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
2586 * throw away our cap_snap.
2588 * Caller hold s_mutex.
2590 static void handle_cap_flushsnap_ack(struct inode
*inode
, u64 flush_tid
,
2591 struct ceph_mds_caps
*m
,
2592 struct ceph_mds_session
*session
)
2594 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2595 u64 follows
= le64_to_cpu(m
->snap_follows
);
2596 struct ceph_cap_snap
*capsnap
;
2599 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2600 inode
, ci
, session
->s_mds
, follows
);
2602 spin_lock(&ci
->i_ceph_lock
);
2603 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
2604 if (capsnap
->follows
== follows
) {
2605 if (capsnap
->flush_tid
!= flush_tid
) {
2606 dout(" cap_snap %p follows %lld tid %lld !="
2607 " %lld\n", capsnap
, follows
,
2608 flush_tid
, capsnap
->flush_tid
);
2611 WARN_ON(capsnap
->dirty_pages
|| capsnap
->writing
);
2612 dout(" removing %p cap_snap %p follows %lld\n",
2613 inode
, capsnap
, follows
);
2614 ceph_put_snap_context(capsnap
->context
);
2615 list_del(&capsnap
->ci_item
);
2616 list_del(&capsnap
->flushing_item
);
2617 ceph_put_cap_snap(capsnap
);
2621 dout(" skipping cap_snap %p follows %lld\n",
2622 capsnap
, capsnap
->follows
);
2625 spin_unlock(&ci
->i_ceph_lock
);
2631 * Handle TRUNC from MDS, indicating file truncation.
2633 * caller hold s_mutex.
2635 static void handle_cap_trunc(struct inode
*inode
,
2636 struct ceph_mds_caps
*trunc
,
2637 struct ceph_mds_session
*session
)
2638 __releases(ci
->i_ceph_lock
)
2640 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2641 int mds
= session
->s_mds
;
2642 int seq
= le32_to_cpu(trunc
->seq
);
2643 u32 truncate_seq
= le32_to_cpu(trunc
->truncate_seq
);
2644 u64 truncate_size
= le64_to_cpu(trunc
->truncate_size
);
2645 u64 size
= le64_to_cpu(trunc
->size
);
2646 int implemented
= 0;
2647 int dirty
= __ceph_caps_dirty(ci
);
2648 int issued
= __ceph_caps_issued(ceph_inode(inode
), &implemented
);
2649 int queue_trunc
= 0;
2651 issued
|= implemented
| dirty
;
2653 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2654 inode
, mds
, seq
, truncate_size
, truncate_seq
);
2655 queue_trunc
= ceph_fill_file_size(inode
, issued
,
2656 truncate_seq
, truncate_size
, size
);
2657 spin_unlock(&ci
->i_ceph_lock
);
2660 ceph_queue_vmtruncate(inode
);
2664 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
2665 * different one. If we are the most recent migration we've seen (as
2666 * indicated by mseq), make note of the migrating cap bits for the
2667 * duration (until we see the corresponding IMPORT).
2669 * caller holds s_mutex
2671 static void handle_cap_export(struct inode
*inode
, struct ceph_mds_caps
*ex
,
2672 struct ceph_mds_session
*session
,
2673 int *open_target_sessions
)
2675 struct ceph_mds_client
*mdsc
= ceph_inode_to_client(inode
)->mdsc
;
2676 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2677 int mds
= session
->s_mds
;
2678 unsigned mseq
= le32_to_cpu(ex
->migrate_seq
);
2679 struct ceph_cap
*cap
= NULL
, *t
;
2683 dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
2684 inode
, ci
, mds
, mseq
);
2686 spin_lock(&ci
->i_ceph_lock
);
2688 /* make sure we haven't seen a higher mseq */
2689 for (p
= rb_first(&ci
->i_caps
); p
; p
= rb_next(p
)) {
2690 t
= rb_entry(p
, struct ceph_cap
, ci_node
);
2691 if (ceph_seq_cmp(t
->mseq
, mseq
) > 0) {
2692 dout(" higher mseq on cap from mds%d\n",
2696 if (t
->session
->s_mds
== mds
)
2703 ci
->i_cap_exporting_mds
= mds
;
2704 ci
->i_cap_exporting_mseq
= mseq
;
2705 ci
->i_cap_exporting_issued
= cap
->issued
;
2708 * make sure we have open sessions with all possible
2709 * export targets, so that we get the matching IMPORT
2711 *open_target_sessions
= 1;
2714 * we can't flush dirty caps that we've seen the
2715 * EXPORT but no IMPORT for
2717 spin_lock(&mdsc
->cap_dirty_lock
);
2718 if (!list_empty(&ci
->i_dirty_item
)) {
2719 dout(" moving %p to cap_dirty_migrating\n",
2721 list_move(&ci
->i_dirty_item
,
2722 &mdsc
->cap_dirty_migrating
);
2724 spin_unlock(&mdsc
->cap_dirty_lock
);
2726 __ceph_remove_cap(cap
);
2728 /* else, we already released it */
2730 spin_unlock(&ci
->i_ceph_lock
);
2734 * Handle cap IMPORT. If there are temp bits from an older EXPORT,
2737 * caller holds s_mutex.
2739 static void handle_cap_import(struct ceph_mds_client
*mdsc
,
2740 struct inode
*inode
, struct ceph_mds_caps
*im
,
2741 struct ceph_mds_session
*session
,
2742 void *snaptrace
, int snaptrace_len
)
2744 struct ceph_inode_info
*ci
= ceph_inode(inode
);
2745 int mds
= session
->s_mds
;
2746 unsigned issued
= le32_to_cpu(im
->caps
);
2747 unsigned wanted
= le32_to_cpu(im
->wanted
);
2748 unsigned seq
= le32_to_cpu(im
->seq
);
2749 unsigned mseq
= le32_to_cpu(im
->migrate_seq
);
2750 u64 realmino
= le64_to_cpu(im
->realm
);
2751 u64 cap_id
= le64_to_cpu(im
->cap_id
);
2753 if (ci
->i_cap_exporting_mds
>= 0 &&
2754 ceph_seq_cmp(ci
->i_cap_exporting_mseq
, mseq
) < 0) {
2755 dout("handle_cap_import inode %p ci %p mds%d mseq %d"
2756 " - cleared exporting from mds%d\n",
2757 inode
, ci
, mds
, mseq
,
2758 ci
->i_cap_exporting_mds
);
2759 ci
->i_cap_exporting_issued
= 0;
2760 ci
->i_cap_exporting_mseq
= 0;
2761 ci
->i_cap_exporting_mds
= -1;
2763 spin_lock(&mdsc
->cap_dirty_lock
);
2764 if (!list_empty(&ci
->i_dirty_item
)) {
2765 dout(" moving %p back to cap_dirty\n", inode
);
2766 list_move(&ci
->i_dirty_item
, &mdsc
->cap_dirty
);
2768 spin_unlock(&mdsc
->cap_dirty_lock
);
2770 dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
2771 inode
, ci
, mds
, mseq
);
2774 down_write(&mdsc
->snap_rwsem
);
2775 ceph_update_snap_trace(mdsc
, snaptrace
, snaptrace
+snaptrace_len
,
2777 downgrade_write(&mdsc
->snap_rwsem
);
2778 ceph_add_cap(inode
, session
, cap_id
, -1,
2779 issued
, wanted
, seq
, mseq
, realmino
, CEPH_CAP_FLAG_AUTH
,
2780 NULL
/* no caps context */);
2781 kick_flushing_inode_caps(mdsc
, session
, inode
);
2782 up_read(&mdsc
->snap_rwsem
);
2784 /* make sure we re-request max_size, if necessary */
2785 spin_lock(&ci
->i_ceph_lock
);
2786 ci
->i_wanted_max_size
= 0; /* reset */
2787 ci
->i_requested_max_size
= 0;
2788 spin_unlock(&ci
->i_ceph_lock
);
2792 * Handle a caps message from the MDS.
2794 * Identify the appropriate session, inode, and call the right handler
2795 * based on the cap op.
2797 void ceph_handle_caps(struct ceph_mds_session
*session
,
2798 struct ceph_msg
*msg
)
2800 struct ceph_mds_client
*mdsc
= session
->s_mdsc
;
2801 struct super_block
*sb
= mdsc
->fsc
->sb
;
2802 struct inode
*inode
;
2803 struct ceph_inode_info
*ci
;
2804 struct ceph_cap
*cap
;
2805 struct ceph_mds_caps
*h
;
2806 int mds
= session
->s_mds
;
2809 struct ceph_vino vino
;
2814 size_t snaptrace_len
;
2817 int open_target_sessions
= 0;
2819 dout("handle_caps from mds%d\n", mds
);
2822 tid
= le64_to_cpu(msg
->hdr
.tid
);
2823 if (msg
->front
.iov_len
< sizeof(*h
))
2825 h
= msg
->front
.iov_base
;
2826 op
= le32_to_cpu(h
->op
);
2827 vino
.ino
= le64_to_cpu(h
->ino
);
2828 vino
.snap
= CEPH_NOSNAP
;
2829 cap_id
= le64_to_cpu(h
->cap_id
);
2830 seq
= le32_to_cpu(h
->seq
);
2831 mseq
= le32_to_cpu(h
->migrate_seq
);
2832 size
= le64_to_cpu(h
->size
);
2833 max_size
= le64_to_cpu(h
->max_size
);
2836 snaptrace_len
= le32_to_cpu(h
->snap_trace_len
);
2838 if (le16_to_cpu(msg
->hdr
.version
) >= 2) {
2841 p
= snaptrace
+ snaptrace_len
;
2842 end
= msg
->front
.iov_base
+ msg
->front
.iov_len
;
2843 ceph_decode_32_safe(&p
, end
, flock_len
, bad
);
2850 mutex_lock(&session
->s_mutex
);
2852 dout(" mds%d seq %lld cap seq %u\n", session
->s_mds
, session
->s_seq
,
2855 if (op
== CEPH_CAP_OP_IMPORT
)
2856 ceph_add_cap_releases(mdsc
, session
);
2859 inode
= ceph_find_inode(sb
, vino
);
2860 ci
= ceph_inode(inode
);
2861 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op
), vino
.ino
,
2864 dout(" i don't have ino %llx\n", vino
.ino
);
2866 if (op
== CEPH_CAP_OP_IMPORT
)
2867 __queue_cap_release(session
, vino
.ino
, cap_id
,
2869 goto flush_cap_releases
;
2872 /* these will work even if we don't have a cap yet */
2874 case CEPH_CAP_OP_FLUSHSNAP_ACK
:
2875 handle_cap_flushsnap_ack(inode
, tid
, h
, session
);
2878 case CEPH_CAP_OP_EXPORT
:
2879 handle_cap_export(inode
, h
, session
, &open_target_sessions
);
2882 case CEPH_CAP_OP_IMPORT
:
2883 handle_cap_import(mdsc
, inode
, h
, session
,
2884 snaptrace
, snaptrace_len
);
2887 /* the rest require a cap */
2888 spin_lock(&ci
->i_ceph_lock
);
2889 cap
= __get_cap_for_mds(ceph_inode(inode
), mds
);
2891 dout(" no cap on %p ino %llx.%llx from mds%d\n",
2892 inode
, ceph_ino(inode
), ceph_snap(inode
), mds
);
2893 spin_unlock(&ci
->i_ceph_lock
);
2894 goto flush_cap_releases
;
2897 /* note that each of these drops i_ceph_lock for us */
2899 case CEPH_CAP_OP_REVOKE
:
2900 case CEPH_CAP_OP_GRANT
:
2901 case CEPH_CAP_OP_IMPORT
:
2902 handle_cap_grant(inode
, h
, session
, cap
, msg
->middle
);
2905 case CEPH_CAP_OP_FLUSH_ACK
:
2906 handle_cap_flush_ack(inode
, tid
, h
, session
, cap
);
2909 case CEPH_CAP_OP_TRUNC
:
2910 handle_cap_trunc(inode
, h
, session
);
2914 spin_unlock(&ci
->i_ceph_lock
);
2915 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op
,
2916 ceph_cap_op_name(op
));
2923 * send any full release message to try to move things
2924 * along for the mds (who clearly thinks we still have this
2927 ceph_add_cap_releases(mdsc
, session
);
2928 ceph_send_cap_releases(mdsc
, session
);
2931 mutex_unlock(&session
->s_mutex
);
2935 if (open_target_sessions
)
2936 ceph_mdsc_open_export_target_sessions(mdsc
, session
);
2940 pr_err("ceph_handle_caps: corrupt message\n");
2946 * Delayed work handler to process end of delayed cap release LRU list.
2948 void ceph_check_delayed_caps(struct ceph_mds_client
*mdsc
)
2950 struct ceph_inode_info
*ci
;
2951 int flags
= CHECK_CAPS_NODELAY
;
2953 dout("check_delayed_caps\n");
2955 spin_lock(&mdsc
->cap_delay_lock
);
2956 if (list_empty(&mdsc
->cap_delay_list
))
2958 ci
= list_first_entry(&mdsc
->cap_delay_list
,
2959 struct ceph_inode_info
,
2961 if ((ci
->i_ceph_flags
& CEPH_I_FLUSH
) == 0 &&
2962 time_before(jiffies
, ci
->i_hold_caps_max
))
2964 list_del_init(&ci
->i_cap_delay_list
);
2965 spin_unlock(&mdsc
->cap_delay_lock
);
2966 dout("check_delayed_caps on %p\n", &ci
->vfs_inode
);
2967 ceph_check_caps(ci
, flags
, NULL
);
2969 spin_unlock(&mdsc
->cap_delay_lock
);
2973 * Flush all dirty caps to the mds
2975 void ceph_flush_dirty_caps(struct ceph_mds_client
*mdsc
)
2977 struct ceph_inode_info
*ci
;
2978 struct inode
*inode
;
2980 dout("flush_dirty_caps\n");
2981 spin_lock(&mdsc
->cap_dirty_lock
);
2982 while (!list_empty(&mdsc
->cap_dirty
)) {
2983 ci
= list_first_entry(&mdsc
->cap_dirty
, struct ceph_inode_info
,
2985 inode
= &ci
->vfs_inode
;
2987 dout("flush_dirty_caps %p\n", inode
);
2988 spin_unlock(&mdsc
->cap_dirty_lock
);
2989 ceph_check_caps(ci
, CHECK_CAPS_NODELAY
|CHECK_CAPS_FLUSH
, NULL
);
2991 spin_lock(&mdsc
->cap_dirty_lock
);
2993 spin_unlock(&mdsc
->cap_dirty_lock
);
2994 dout("flush_dirty_caps done\n");
2998 * Drop open file reference. If we were the last open file,
2999 * we may need to release capabilities to the MDS (or schedule
3000 * their delayed release).
3002 void ceph_put_fmode(struct ceph_inode_info
*ci
, int fmode
)
3004 struct inode
*inode
= &ci
->vfs_inode
;
3007 spin_lock(&ci
->i_ceph_lock
);
3008 dout("put_fmode %p fmode %d %d -> %d\n", inode
, fmode
,
3009 ci
->i_nr_by_mode
[fmode
], ci
->i_nr_by_mode
[fmode
]-1);
3010 BUG_ON(ci
->i_nr_by_mode
[fmode
] == 0);
3011 if (--ci
->i_nr_by_mode
[fmode
] == 0)
3013 spin_unlock(&ci
->i_ceph_lock
);
3015 if (last
&& ci
->i_vino
.snap
== CEPH_NOSNAP
)
3016 ceph_check_caps(ci
, 0, NULL
);
3020 * Helpers for embedding cap and dentry lease releases into mds
3023 * @force is used by dentry_release (below) to force inclusion of a
3024 * record for the directory inode, even when there aren't any caps to
3027 int ceph_encode_inode_release(void **p
, struct inode
*inode
,
3028 int mds
, int drop
, int unless
, int force
)
3030 struct ceph_inode_info
*ci
= ceph_inode(inode
);
3031 struct ceph_cap
*cap
;
3032 struct ceph_mds_request_release
*rel
= *p
;
3036 spin_lock(&ci
->i_ceph_lock
);
3037 used
= __ceph_caps_used(ci
);
3038 dirty
= __ceph_caps_dirty(ci
);
3040 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
3041 inode
, mds
, ceph_cap_string(used
|dirty
), ceph_cap_string(drop
),
3042 ceph_cap_string(unless
));
3044 /* only drop unused, clean caps */
3045 drop
&= ~(used
| dirty
);
3047 cap
= __get_cap_for_mds(ci
, mds
);
3048 if (cap
&& __cap_is_valid(cap
)) {
3050 ((cap
->issued
& drop
) &&
3051 (cap
->issued
& unless
) == 0)) {
3052 if ((cap
->issued
& drop
) &&
3053 (cap
->issued
& unless
) == 0) {
3054 int wanted
= __ceph_caps_wanted(ci
);
3055 if ((ci
->i_ceph_flags
& CEPH_I_NODELAY
) == 0)
3056 wanted
|= cap
->mds_wanted
;
3057 dout("encode_inode_release %p cap %p "
3058 "%s -> %s, wanted %s -> %s\n", inode
, cap
,
3059 ceph_cap_string(cap
->issued
),
3060 ceph_cap_string(cap
->issued
& ~drop
),
3061 ceph_cap_string(cap
->mds_wanted
),
3062 ceph_cap_string(wanted
));
3064 cap
->issued
&= ~drop
;
3065 cap
->implemented
&= ~drop
;
3066 cap
->mds_wanted
= wanted
;
3068 dout("encode_inode_release %p cap %p %s"
3069 " (force)\n", inode
, cap
,
3070 ceph_cap_string(cap
->issued
));
3073 rel
->ino
= cpu_to_le64(ceph_ino(inode
));
3074 rel
->cap_id
= cpu_to_le64(cap
->cap_id
);
3075 rel
->seq
= cpu_to_le32(cap
->seq
);
3076 rel
->issue_seq
= cpu_to_le32(cap
->issue_seq
),
3077 rel
->mseq
= cpu_to_le32(cap
->mseq
);
3078 rel
->caps
= cpu_to_le32(cap
->issued
);
3079 rel
->wanted
= cpu_to_le32(cap
->mds_wanted
);
3085 dout("encode_inode_release %p cap %p %s\n",
3086 inode
, cap
, ceph_cap_string(cap
->issued
));
3089 spin_unlock(&ci
->i_ceph_lock
);
3093 int ceph_encode_dentry_release(void **p
, struct dentry
*dentry
,
3094 int mds
, int drop
, int unless
)
3096 struct inode
*dir
= dentry
->d_parent
->d_inode
;
3097 struct ceph_mds_request_release
*rel
= *p
;
3098 struct ceph_dentry_info
*di
= ceph_dentry(dentry
);
3103 * force an record for the directory caps if we have a dentry lease.
3104 * this is racy (can't take i_ceph_lock and d_lock together), but it
3105 * doesn't have to be perfect; the mds will revoke anything we don't
3108 spin_lock(&dentry
->d_lock
);
3109 if (di
->lease_session
&& di
->lease_session
->s_mds
== mds
)
3111 spin_unlock(&dentry
->d_lock
);
3113 ret
= ceph_encode_inode_release(p
, dir
, mds
, drop
, unless
, force
);
3115 spin_lock(&dentry
->d_lock
);
3116 if (ret
&& di
->lease_session
&& di
->lease_session
->s_mds
== mds
) {
3117 dout("encode_dentry_release %p mds%d seq %d\n",
3118 dentry
, mds
, (int)di
->lease_seq
);
3119 rel
->dname_len
= cpu_to_le32(dentry
->d_name
.len
);
3120 memcpy(*p
, dentry
->d_name
.name
, dentry
->d_name
.len
);
3121 *p
+= dentry
->d_name
.len
;
3122 rel
->dname_seq
= cpu_to_le32(di
->lease_seq
);
3123 __ceph_mdsc_drop_dentry_lease(dentry
);
3125 spin_unlock(&dentry
->d_lock
);