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, clear D_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 ceph_dir_clear_complete(&ci->vfs_inode);
506 * Add a capability under the given MDS session.
508 * Caller should hold session snap_rwsem (read) and s_mutex.
510 * @fmode is the open file mode, if we are opening a file, otherwise
511 * it is < 0. (This is so we can atomically add the cap and add an
512 * open file reference to it.)
514 int ceph_add_cap(struct inode *inode,
515 struct ceph_mds_session *session, u64 cap_id,
516 int fmode, unsigned issued, unsigned wanted,
517 unsigned seq, unsigned mseq, u64 realmino, int flags,
518 struct ceph_cap_reservation *caps_reservation)
520 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
521 struct ceph_inode_info *ci = ceph_inode(inode);
522 struct ceph_cap *new_cap = NULL;
523 struct ceph_cap *cap;
524 int mds = session->s_mds;
527 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
528 session->s_mds, cap_id, ceph_cap_string(issued), seq);
531 * If we are opening the file, include file mode wanted bits
535 wanted |= ceph_caps_for_mode(fmode);
538 spin_lock(&ci->i_ceph_lock);
539 cap = __get_cap_for_mds(ci, mds);
545 spin_unlock(&ci->i_ceph_lock);
546 new_cap = get_cap(mdsc, caps_reservation);
553 cap->implemented = 0;
559 __insert_cap_node(ci, cap);
561 /* clear out old exporting info? (i.e. on cap import) */
562 if (ci->i_cap_exporting_mds == mds) {
563 ci->i_cap_exporting_issued = 0;
564 ci->i_cap_exporting_mseq = 0;
565 ci->i_cap_exporting_mds = -1;
568 /* add to session cap list */
569 cap->session = session;
570 spin_lock(&session->s_cap_lock);
571 list_add_tail(&cap->session_caps, &session->s_caps);
572 session->s_nr_caps++;
573 spin_unlock(&session->s_cap_lock);
575 ceph_put_cap(mdsc, new_cap);
577 if (!ci->i_snap_realm) {
579 * add this inode to the appropriate snap realm
581 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
584 ceph_get_snap_realm(mdsc, realm);
585 spin_lock(&realm->inodes_with_caps_lock);
586 ci->i_snap_realm = realm;
587 list_add(&ci->i_snap_realm_item,
588 &realm->inodes_with_caps);
589 spin_unlock(&realm->inodes_with_caps_lock);
591 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
597 __check_cap_issue(ci, cap, issued);
600 * If we are issued caps we don't want, or the mds' wanted
601 * value appears to be off, queue a check so we'll release
602 * later and/or update the mds wanted value.
604 actual_wanted = __ceph_caps_wanted(ci);
605 if ((wanted & ~actual_wanted) ||
606 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
607 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
608 ceph_cap_string(issued), ceph_cap_string(wanted),
609 ceph_cap_string(actual_wanted));
610 __cap_delay_requeue(mdsc, ci);
613 if (flags & CEPH_CAP_FLAG_AUTH)
614 ci->i_auth_cap = cap;
615 else if (ci->i_auth_cap == cap) {
616 ci->i_auth_cap = NULL;
617 spin_lock(&mdsc->cap_dirty_lock);
618 if (!list_empty(&ci->i_dirty_item)) {
619 dout(" moving %p to cap_dirty_migrating\n", inode);
620 list_move(&ci->i_dirty_item,
621 &mdsc->cap_dirty_migrating);
623 spin_unlock(&mdsc->cap_dirty_lock);
626 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
627 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
628 ceph_cap_string(issued|cap->issued), seq, mds);
629 cap->cap_id = cap_id;
630 cap->issued = issued;
631 cap->implemented |= issued;
632 if (mseq > cap->mseq)
633 cap->mds_wanted = wanted;
635 cap->mds_wanted |= wanted;
637 cap->issue_seq = seq;
639 cap->cap_gen = session->s_cap_gen;
642 __ceph_get_fmode(ci, fmode);
643 spin_unlock(&ci->i_ceph_lock);
644 wake_up_all(&ci->i_cap_wq);
649 * Return true if cap has not timed out and belongs to the current
650 * generation of the MDS session (i.e. has not gone 'stale' due to
651 * us losing touch with the mds).
653 static int __cap_is_valid(struct ceph_cap *cap)
658 spin_lock(&cap->session->s_gen_ttl_lock);
659 gen = cap->session->s_cap_gen;
660 ttl = cap->session->s_cap_ttl;
661 spin_unlock(&cap->session->s_gen_ttl_lock);
663 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
664 dout("__cap_is_valid %p cap %p issued %s "
665 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
666 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
674 * Return set of valid cap bits issued to us. Note that caps time
675 * out, and may be invalidated in bulk if the client session times out
676 * and session->s_cap_gen is bumped.
678 int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
680 int have = ci->i_snap_caps | ci->i_cap_exporting_issued;
681 struct ceph_cap *cap;
686 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
687 cap = rb_entry(p, struct ceph_cap, ci_node);
688 if (!__cap_is_valid(cap))
690 dout("__ceph_caps_issued %p cap %p issued %s\n",
691 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
694 *implemented |= cap->implemented;
700 * Get cap bits issued by caps other than @ocap
702 int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
704 int have = ci->i_snap_caps;
705 struct ceph_cap *cap;
708 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
709 cap = rb_entry(p, struct ceph_cap, ci_node);
712 if (!__cap_is_valid(cap))
720 * Move a cap to the end of the LRU (oldest caps at list head, newest
723 static void __touch_cap(struct ceph_cap *cap)
725 struct ceph_mds_session *s = cap->session;
727 spin_lock(&s->s_cap_lock);
728 if (s->s_cap_iterator == NULL) {
729 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
731 list_move_tail(&cap->session_caps, &s->s_caps);
733 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
734 &cap->ci->vfs_inode, cap, s->s_mds);
736 spin_unlock(&s->s_cap_lock);
740 * Check if we hold the given mask. If so, move the cap(s) to the
741 * front of their respective LRUs. (This is the preferred way for
742 * callers to check for caps they want.)
744 int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
746 struct ceph_cap *cap;
748 int have = ci->i_snap_caps;
750 if ((have & mask) == mask) {
751 dout("__ceph_caps_issued_mask %p snap issued %s"
752 " (mask %s)\n", &ci->vfs_inode,
753 ceph_cap_string(have),
754 ceph_cap_string(mask));
758 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
759 cap = rb_entry(p, struct ceph_cap, ci_node);
760 if (!__cap_is_valid(cap))
762 if ((cap->issued & mask) == mask) {
763 dout("__ceph_caps_issued_mask %p cap %p issued %s"
764 " (mask %s)\n", &ci->vfs_inode, cap,
765 ceph_cap_string(cap->issued),
766 ceph_cap_string(mask));
772 /* does a combination of caps satisfy mask? */
774 if ((have & mask) == mask) {
775 dout("__ceph_caps_issued_mask %p combo issued %s"
776 " (mask %s)\n", &ci->vfs_inode,
777 ceph_cap_string(cap->issued),
778 ceph_cap_string(mask));
782 /* touch this + preceding caps */
784 for (q = rb_first(&ci->i_caps); q != p;
786 cap = rb_entry(q, struct ceph_cap,
788 if (!__cap_is_valid(cap))
801 * Return true if mask caps are currently being revoked by an MDS.
803 int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
805 struct inode *inode = &ci->vfs_inode;
806 struct ceph_cap *cap;
810 spin_lock(&ci->i_ceph_lock);
811 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
812 cap = rb_entry(p, struct ceph_cap, ci_node);
813 if (__cap_is_valid(cap) &&
814 (cap->implemented & ~cap->issued & mask)) {
819 spin_unlock(&ci->i_ceph_lock);
820 dout("ceph_caps_revoking %p %s = %d\n", inode,
821 ceph_cap_string(mask), ret);
825 int __ceph_caps_used(struct ceph_inode_info *ci)
829 used |= CEPH_CAP_PIN;
831 used |= CEPH_CAP_FILE_RD;
832 if (ci->i_rdcache_ref || ci->vfs_inode.i_data.nrpages)
833 used |= CEPH_CAP_FILE_CACHE;
835 used |= CEPH_CAP_FILE_WR;
836 if (ci->i_wb_ref || ci->i_wrbuffer_ref)
837 used |= CEPH_CAP_FILE_BUFFER;
842 * wanted, by virtue of open file modes
844 int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
848 for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++)
849 if (ci->i_nr_by_mode[mode])
850 want |= ceph_caps_for_mode(mode);
855 * Return caps we have registered with the MDS(s) as 'wanted'.
857 int __ceph_caps_mds_wanted(struct ceph_inode_info *ci)
859 struct ceph_cap *cap;
863 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
864 cap = rb_entry(p, struct ceph_cap, ci_node);
865 if (!__cap_is_valid(cap))
867 mds_wanted |= cap->mds_wanted;
873 * called under i_ceph_lock
875 static int __ceph_is_any_caps(struct ceph_inode_info *ci)
877 return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0;
881 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
883 * caller should hold i_ceph_lock.
884 * caller will not hold session s_mutex if called from destroy_inode.
886 void __ceph_remove_cap(struct ceph_cap *cap)
888 struct ceph_mds_session *session = cap->session;
889 struct ceph_inode_info *ci = cap->ci;
890 struct ceph_mds_client *mdsc =
891 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
894 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
896 /* remove from session list */
897 spin_lock(&session->s_cap_lock);
898 if (session->s_cap_iterator == cap) {
899 /* not yet, we are iterating over this very cap */
900 dout("__ceph_remove_cap delaying %p removal from session %p\n",
903 list_del_init(&cap->session_caps);
904 session->s_nr_caps--;
908 /* protect backpointer with s_cap_lock: see iterate_session_caps */
910 spin_unlock(&session->s_cap_lock);
912 /* remove from inode list */
913 rb_erase(&cap->ci_node, &ci->i_caps);
914 if (ci->i_auth_cap == cap)
915 ci->i_auth_cap = NULL;
918 ceph_put_cap(mdsc, cap);
920 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) {
921 struct ceph_snap_realm *realm = ci->i_snap_realm;
922 spin_lock(&realm->inodes_with_caps_lock);
923 list_del_init(&ci->i_snap_realm_item);
924 ci->i_snap_realm_counter++;
925 ci->i_snap_realm = NULL;
926 spin_unlock(&realm->inodes_with_caps_lock);
927 ceph_put_snap_realm(mdsc, realm);
929 if (!__ceph_is_any_real_caps(ci))
930 __cap_delay_cancel(mdsc, ci);
934 * Build and send a cap message to the given MDS.
936 * Caller should be holding s_mutex.
938 static int send_cap_msg(struct ceph_mds_session *session,
939 u64 ino, u64 cid, int op,
940 int caps, int wanted, int dirty,
941 u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq,
942 u64 size, u64 max_size,
943 struct timespec *mtime, struct timespec *atime,
945 kuid_t uid, kgid_t gid, umode_t mode,
947 struct ceph_buffer *xattrs_buf,
950 struct ceph_mds_caps *fc;
951 struct ceph_msg *msg;
953 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
954 " seq %u/%u mseq %u follows %lld size %llu/%llu"
955 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op),
956 cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted),
957 ceph_cap_string(dirty),
958 seq, issue_seq, mseq, follows, size, max_size,
959 xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0);
961 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS, false);
965 msg->hdr.tid = cpu_to_le64(flush_tid);
967 fc = msg->front.iov_base;
968 memset(fc, 0, sizeof(*fc));
970 fc->cap_id = cpu_to_le64(cid);
971 fc->op = cpu_to_le32(op);
972 fc->seq = cpu_to_le32(seq);
973 fc->issue_seq = cpu_to_le32(issue_seq);
974 fc->migrate_seq = cpu_to_le32(mseq);
975 fc->caps = cpu_to_le32(caps);
976 fc->wanted = cpu_to_le32(wanted);
977 fc->dirty = cpu_to_le32(dirty);
978 fc->ino = cpu_to_le64(ino);
979 fc->snap_follows = cpu_to_le64(follows);
981 fc->size = cpu_to_le64(size);
982 fc->max_size = cpu_to_le64(max_size);
984 ceph_encode_timespec(&fc->mtime, mtime);
986 ceph_encode_timespec(&fc->atime, atime);
987 fc->time_warp_seq = cpu_to_le32(time_warp_seq);
989 fc->uid = cpu_to_le32(from_kuid(&init_user_ns, uid));
990 fc->gid = cpu_to_le32(from_kgid(&init_user_ns, gid));
991 fc->mode = cpu_to_le32(mode);
993 fc->xattr_version = cpu_to_le64(xattr_version);
995 msg->middle = ceph_buffer_get(xattrs_buf);
996 fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len);
997 msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len);
1000 ceph_con_send(&session->s_con, msg);
1004 void __queue_cap_release(struct ceph_mds_session *session,
1005 u64 ino, u64 cap_id, u32 migrate_seq,
1008 struct ceph_msg *msg;
1009 struct ceph_mds_cap_release *head;
1010 struct ceph_mds_cap_item *item;
1012 spin_lock(&session->s_cap_lock);
1013 BUG_ON(!session->s_num_cap_releases);
1014 msg = list_first_entry(&session->s_cap_releases,
1015 struct ceph_msg, list_head);
1017 dout(" adding %llx release to mds%d msg %p (%d left)\n",
1018 ino, session->s_mds, msg, session->s_num_cap_releases);
1020 BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE);
1021 head = msg->front.iov_base;
1022 le32_add_cpu(&head->num, 1);
1023 item = msg->front.iov_base + msg->front.iov_len;
1024 item->ino = cpu_to_le64(ino);
1025 item->cap_id = cpu_to_le64(cap_id);
1026 item->migrate_seq = cpu_to_le32(migrate_seq);
1027 item->seq = cpu_to_le32(issue_seq);
1029 session->s_num_cap_releases--;
1031 msg->front.iov_len += sizeof(*item);
1032 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
1033 dout(" release msg %p full\n", msg);
1034 list_move_tail(&msg->list_head, &session->s_cap_releases_done);
1036 dout(" release msg %p at %d/%d (%d)\n", msg,
1037 (int)le32_to_cpu(head->num),
1038 (int)CEPH_CAPS_PER_RELEASE,
1039 (int)msg->front.iov_len);
1041 spin_unlock(&session->s_cap_lock);
1045 * Queue cap releases when an inode is dropped from our cache. Since
1046 * inode is about to be destroyed, there is no need for i_ceph_lock.
1048 void ceph_queue_caps_release(struct inode *inode)
1050 struct ceph_inode_info *ci = ceph_inode(inode);
1053 p = rb_first(&ci->i_caps);
1055 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1056 struct ceph_mds_session *session = cap->session;
1058 __queue_cap_release(session, ceph_ino(inode), cap->cap_id,
1059 cap->mseq, cap->issue_seq);
1061 __ceph_remove_cap(cap);
1066 * Send a cap msg on the given inode. Update our caps state, then
1067 * drop i_ceph_lock and send the message.
1069 * Make note of max_size reported/requested from mds, revoked caps
1070 * that have now been implemented.
1072 * Make half-hearted attempt ot to invalidate page cache if we are
1073 * dropping RDCACHE. Note that this will leave behind locked pages
1074 * that we'll then need to deal with elsewhere.
1076 * Return non-zero if delayed release, or we experienced an error
1077 * such that the caller should requeue + retry later.
1079 * called with i_ceph_lock, then drops it.
1080 * caller should hold snap_rwsem (read), s_mutex.
1082 static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1083 int op, int used, int want, int retain, int flushing,
1084 unsigned *pflush_tid)
1085 __releases(cap->ci->i_ceph_lock)
1087 struct ceph_inode_info *ci = cap->ci;
1088 struct inode *inode = &ci->vfs_inode;
1089 u64 cap_id = cap->cap_id;
1090 int held, revoking, dropping, keep;
1091 u64 seq, issue_seq, mseq, time_warp_seq, follows;
1093 struct timespec mtime, atime;
1098 struct ceph_mds_session *session;
1099 u64 xattr_version = 0;
1100 struct ceph_buffer *xattr_blob = NULL;
1106 held = cap->issued | cap->implemented;
1107 revoking = cap->implemented & ~cap->issued;
1108 retain &= ~revoking;
1109 dropping = cap->issued & ~retain;
1111 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1112 inode, cap, cap->session,
1113 ceph_cap_string(held), ceph_cap_string(held & retain),
1114 ceph_cap_string(revoking));
1115 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1117 session = cap->session;
1119 /* don't release wanted unless we've waited a bit. */
1120 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1121 time_before(jiffies, ci->i_hold_caps_min)) {
1122 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1123 ceph_cap_string(cap->issued),
1124 ceph_cap_string(cap->issued & retain),
1125 ceph_cap_string(cap->mds_wanted),
1126 ceph_cap_string(want));
1127 want |= cap->mds_wanted;
1128 retain |= cap->issued;
1131 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1133 cap->issued &= retain; /* drop bits we don't want */
1134 if (cap->implemented & ~cap->issued) {
1136 * Wake up any waiters on wanted -> needed transition.
1137 * This is due to the weird transition from buffered
1138 * to sync IO... we need to flush dirty pages _before_
1139 * allowing sync writes to avoid reordering.
1143 cap->implemented &= cap->issued | used;
1144 cap->mds_wanted = want;
1148 * assign a tid for flush operations so we can avoid
1149 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1150 * clean type races. track latest tid for every bit
1151 * so we can handle flush AxFw, flush Fw, and have the
1152 * first ack clean Ax.
1154 flush_tid = ++ci->i_cap_flush_last_tid;
1156 *pflush_tid = flush_tid;
1157 dout(" cap_flush_tid %d\n", (int)flush_tid);
1158 for (i = 0; i < CEPH_CAP_BITS; i++)
1159 if (flushing & (1 << i))
1160 ci->i_cap_flush_tid[i] = flush_tid;
1162 follows = ci->i_head_snapc->seq;
1167 keep = cap->implemented;
1169 issue_seq = cap->issue_seq;
1171 size = inode->i_size;
1172 ci->i_reported_size = size;
1173 max_size = ci->i_wanted_max_size;
1174 ci->i_requested_max_size = max_size;
1175 mtime = inode->i_mtime;
1176 atime = inode->i_atime;
1177 time_warp_seq = ci->i_time_warp_seq;
1180 mode = inode->i_mode;
1182 if (flushing & CEPH_CAP_XATTR_EXCL) {
1183 __ceph_build_xattrs_blob(ci);
1184 xattr_blob = ci->i_xattrs.blob;
1185 xattr_version = ci->i_xattrs.version;
1188 spin_unlock(&ci->i_ceph_lock);
1190 ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id,
1191 op, keep, want, flushing, seq, flush_tid, issue_seq, mseq,
1192 size, max_size, &mtime, &atime, time_warp_seq,
1193 uid, gid, mode, xattr_version, xattr_blob,
1196 dout("error sending cap msg, must requeue %p\n", inode);
1201 wake_up_all(&ci->i_cap_wq);
1207 * When a snapshot is taken, clients accumulate dirty metadata on
1208 * inodes with capabilities in ceph_cap_snaps to describe the file
1209 * state at the time the snapshot was taken. This must be flushed
1210 * asynchronously back to the MDS once sync writes complete and dirty
1211 * data is written out.
1213 * Unless @again is true, skip cap_snaps that were already sent to
1214 * the MDS (i.e., during this session).
1216 * Called under i_ceph_lock. Takes s_mutex as needed.
1218 void __ceph_flush_snaps(struct ceph_inode_info *ci,
1219 struct ceph_mds_session **psession,
1221 __releases(ci->i_ceph_lock)
1222 __acquires(ci->i_ceph_lock)
1224 struct inode *inode = &ci->vfs_inode;
1226 struct ceph_cap_snap *capsnap;
1228 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
1229 struct ceph_mds_session *session = NULL; /* if session != NULL, we hold
1231 u64 next_follows = 0; /* keep track of how far we've gotten through the
1232 i_cap_snaps list, and skip these entries next time
1233 around to avoid an infinite loop */
1236 session = *psession;
1238 dout("__flush_snaps %p\n", inode);
1240 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1241 /* avoid an infiniute loop after retry */
1242 if (capsnap->follows < next_follows)
1245 * we need to wait for sync writes to complete and for dirty
1246 * pages to be written out.
1248 if (capsnap->dirty_pages || capsnap->writing)
1252 * if cap writeback already occurred, we should have dropped
1253 * the capsnap in ceph_put_wrbuffer_cap_refs.
1255 BUG_ON(capsnap->dirty == 0);
1257 /* pick mds, take s_mutex */
1258 if (ci->i_auth_cap == NULL) {
1259 dout("no auth cap (migrating?), doing nothing\n");
1263 /* only flush each capsnap once */
1264 if (!again && !list_empty(&capsnap->flushing_item)) {
1265 dout("already flushed %p, skipping\n", capsnap);
1269 mds = ci->i_auth_cap->session->s_mds;
1270 mseq = ci->i_auth_cap->mseq;
1272 if (session && session->s_mds != mds) {
1273 dout("oops, wrong session %p mutex\n", session);
1274 mutex_unlock(&session->s_mutex);
1275 ceph_put_mds_session(session);
1279 spin_unlock(&ci->i_ceph_lock);
1280 mutex_lock(&mdsc->mutex);
1281 session = __ceph_lookup_mds_session(mdsc, mds);
1282 mutex_unlock(&mdsc->mutex);
1284 dout("inverting session/ino locks on %p\n",
1286 mutex_lock(&session->s_mutex);
1289 * if session == NULL, we raced against a cap
1290 * deletion or migration. retry, and we'll
1291 * get a better @mds value next time.
1293 spin_lock(&ci->i_ceph_lock);
1297 capsnap->flush_tid = ++ci->i_cap_flush_last_tid;
1298 atomic_inc(&capsnap->nref);
1299 if (!list_empty(&capsnap->flushing_item))
1300 list_del_init(&capsnap->flushing_item);
1301 list_add_tail(&capsnap->flushing_item,
1302 &session->s_cap_snaps_flushing);
1303 spin_unlock(&ci->i_ceph_lock);
1305 dout("flush_snaps %p cap_snap %p follows %lld tid %llu\n",
1306 inode, capsnap, capsnap->follows, capsnap->flush_tid);
1307 send_cap_msg(session, ceph_vino(inode).ino, 0,
1308 CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0,
1309 capsnap->dirty, 0, capsnap->flush_tid, 0, mseq,
1311 &capsnap->mtime, &capsnap->atime,
1312 capsnap->time_warp_seq,
1313 capsnap->uid, capsnap->gid, capsnap->mode,
1314 capsnap->xattr_version, capsnap->xattr_blob,
1317 next_follows = capsnap->follows + 1;
1318 ceph_put_cap_snap(capsnap);
1320 spin_lock(&ci->i_ceph_lock);
1324 /* we flushed them all; remove this inode from the queue */
1325 spin_lock(&mdsc->snap_flush_lock);
1326 list_del_init(&ci->i_snap_flush_item);
1327 spin_unlock(&mdsc->snap_flush_lock);
1331 *psession = session;
1333 mutex_unlock(&session->s_mutex);
1334 ceph_put_mds_session(session);
1338 static void ceph_flush_snaps(struct ceph_inode_info *ci)
1340 spin_lock(&ci->i_ceph_lock);
1341 __ceph_flush_snaps(ci, NULL, 0);
1342 spin_unlock(&ci->i_ceph_lock);
1346 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1347 * Caller is then responsible for calling __mark_inode_dirty with the
1348 * returned flags value.
1350 int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask)
1352 struct ceph_mds_client *mdsc =
1353 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
1354 struct inode *inode = &ci->vfs_inode;
1355 int was = ci->i_dirty_caps;
1358 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1359 ceph_cap_string(mask), ceph_cap_string(was),
1360 ceph_cap_string(was | mask));
1361 ci->i_dirty_caps |= mask;
1363 if (!ci->i_head_snapc)
1364 ci->i_head_snapc = ceph_get_snap_context(
1365 ci->i_snap_realm->cached_context);
1366 dout(" inode %p now dirty snapc %p auth cap %p\n",
1367 &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
1368 BUG_ON(!list_empty(&ci->i_dirty_item));
1369 spin_lock(&mdsc->cap_dirty_lock);
1371 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
1373 list_add(&ci->i_dirty_item,
1374 &mdsc->cap_dirty_migrating);
1375 spin_unlock(&mdsc->cap_dirty_lock);
1376 if (ci->i_flushing_caps == 0) {
1378 dirty |= I_DIRTY_SYNC;
1381 BUG_ON(list_empty(&ci->i_dirty_item));
1382 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1383 (mask & CEPH_CAP_FILE_BUFFER))
1384 dirty |= I_DIRTY_DATASYNC;
1385 __cap_delay_requeue(mdsc, ci);
1390 * Add dirty inode to the flushing list. Assigned a seq number so we
1391 * can wait for caps to flush without starving.
1393 * Called under i_ceph_lock.
1395 static int __mark_caps_flushing(struct inode *inode,
1396 struct ceph_mds_session *session)
1398 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1399 struct ceph_inode_info *ci = ceph_inode(inode);
1402 BUG_ON(ci->i_dirty_caps == 0);
1403 BUG_ON(list_empty(&ci->i_dirty_item));
1405 flushing = ci->i_dirty_caps;
1406 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1407 ceph_cap_string(flushing),
1408 ceph_cap_string(ci->i_flushing_caps),
1409 ceph_cap_string(ci->i_flushing_caps | flushing));
1410 ci->i_flushing_caps |= flushing;
1411 ci->i_dirty_caps = 0;
1412 dout(" inode %p now !dirty\n", inode);
1414 spin_lock(&mdsc->cap_dirty_lock);
1415 list_del_init(&ci->i_dirty_item);
1417 ci->i_cap_flush_seq = ++mdsc->cap_flush_seq;
1418 if (list_empty(&ci->i_flushing_item)) {
1419 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1420 mdsc->num_cap_flushing++;
1421 dout(" inode %p now flushing seq %lld\n", inode,
1422 ci->i_cap_flush_seq);
1424 list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1425 dout(" inode %p now flushing (more) seq %lld\n", inode,
1426 ci->i_cap_flush_seq);
1428 spin_unlock(&mdsc->cap_dirty_lock);
1434 * try to invalidate mapping pages without blocking.
1436 static int try_nonblocking_invalidate(struct inode *inode)
1438 struct ceph_inode_info *ci = ceph_inode(inode);
1439 u32 invalidating_gen = ci->i_rdcache_gen;
1441 spin_unlock(&ci->i_ceph_lock);
1442 invalidate_mapping_pages(&inode->i_data, 0, -1);
1443 spin_lock(&ci->i_ceph_lock);
1445 if (inode->i_data.nrpages == 0 &&
1446 invalidating_gen == ci->i_rdcache_gen) {
1448 dout("try_nonblocking_invalidate %p success\n", inode);
1449 /* save any racing async invalidate some trouble */
1450 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
1453 dout("try_nonblocking_invalidate %p failed\n", inode);
1458 * Swiss army knife function to examine currently used and wanted
1459 * versus held caps. Release, flush, ack revoked caps to mds as
1462 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1463 * cap release further.
1464 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1465 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1468 void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1469 struct ceph_mds_session *session)
1471 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1472 struct ceph_mds_client *mdsc = fsc->mdsc;
1473 struct inode *inode = &ci->vfs_inode;
1474 struct ceph_cap *cap;
1475 int file_wanted, used, cap_used;
1476 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
1477 int issued, implemented, want, retain, revoking, flushing = 0;
1478 int mds = -1; /* keep track of how far we've gone through i_caps list
1479 to avoid an infinite loop on retry */
1481 int tried_invalidate = 0;
1482 int delayed = 0, sent = 0, force_requeue = 0, num;
1483 int queue_invalidate = 0;
1484 int is_delayed = flags & CHECK_CAPS_NODELAY;
1486 /* if we are unmounting, flush any unused caps immediately. */
1490 spin_lock(&ci->i_ceph_lock);
1492 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1493 flags |= CHECK_CAPS_FLUSH;
1495 /* flush snaps first time around only */
1496 if (!list_empty(&ci->i_cap_snaps))
1497 __ceph_flush_snaps(ci, &session, 0);
1500 spin_lock(&ci->i_ceph_lock);
1502 file_wanted = __ceph_caps_file_wanted(ci);
1503 used = __ceph_caps_used(ci);
1504 want = file_wanted | used;
1505 issued = __ceph_caps_issued(ci, &implemented);
1506 revoking = implemented & ~issued;
1508 retain = want | CEPH_CAP_PIN;
1509 if (!mdsc->stopping && inode->i_nlink > 0) {
1511 retain |= CEPH_CAP_ANY; /* be greedy */
1513 retain |= CEPH_CAP_ANY_SHARED;
1515 * keep RD only if we didn't have the file open RW,
1516 * because then the mds would revoke it anyway to
1517 * journal max_size=0.
1519 if (ci->i_max_size == 0)
1520 retain |= CEPH_CAP_ANY_RD;
1524 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1525 " issued %s revoking %s retain %s %s%s%s\n", inode,
1526 ceph_cap_string(file_wanted),
1527 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1528 ceph_cap_string(ci->i_flushing_caps),
1529 ceph_cap_string(issued), ceph_cap_string(revoking),
1530 ceph_cap_string(retain),
1531 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1532 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1533 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1536 * If we no longer need to hold onto old our caps, and we may
1537 * have cached pages, but don't want them, then try to invalidate.
1538 * If we fail, it's because pages are locked.... try again later.
1540 if ((!is_delayed || mdsc->stopping) &&
1541 ci->i_wrbuffer_ref == 0 && /* no dirty pages... */
1542 inode->i_data.nrpages && /* have cached pages */
1543 (file_wanted == 0 || /* no open files */
1544 (revoking & (CEPH_CAP_FILE_CACHE|
1545 CEPH_CAP_FILE_LAZYIO))) && /* or revoking cache */
1546 !tried_invalidate) {
1547 dout("check_caps trying to invalidate on %p\n", inode);
1548 if (try_nonblocking_invalidate(inode) < 0) {
1549 if (revoking & (CEPH_CAP_FILE_CACHE|
1550 CEPH_CAP_FILE_LAZYIO)) {
1551 dout("check_caps queuing invalidate\n");
1552 queue_invalidate = 1;
1553 ci->i_rdcache_revoking = ci->i_rdcache_gen;
1555 dout("check_caps failed to invalidate pages\n");
1556 /* we failed to invalidate pages. check these
1557 caps again later. */
1559 __cap_set_timeouts(mdsc, ci);
1562 tried_invalidate = 1;
1567 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1568 cap = rb_entry(p, struct ceph_cap, ci_node);
1571 /* avoid looping forever */
1572 if (mds >= cap->mds ||
1573 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1576 /* NOTE: no side-effects allowed, until we take s_mutex */
1579 if (ci->i_auth_cap && cap != ci->i_auth_cap)
1580 cap_used &= ~ci->i_auth_cap->issued;
1582 revoking = cap->implemented & ~cap->issued;
1583 dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
1584 cap->mds, cap, ceph_cap_string(cap->issued),
1585 ceph_cap_string(cap_used),
1586 ceph_cap_string(cap->implemented),
1587 ceph_cap_string(revoking));
1589 if (cap == ci->i_auth_cap &&
1590 (cap->issued & CEPH_CAP_FILE_WR)) {
1591 /* request larger max_size from MDS? */
1592 if (ci->i_wanted_max_size > ci->i_max_size &&
1593 ci->i_wanted_max_size > ci->i_requested_max_size) {
1594 dout("requesting new max_size\n");
1598 /* approaching file_max? */
1599 if ((inode->i_size << 1) >= ci->i_max_size &&
1600 (ci->i_reported_size << 1) < ci->i_max_size) {
1601 dout("i_size approaching max_size\n");
1605 /* flush anything dirty? */
1606 if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) &&
1608 dout("flushing dirty caps\n");
1612 /* completed revocation? going down and there are no caps? */
1613 if (revoking && (revoking & cap_used) == 0) {
1614 dout("completed revocation of %s\n",
1615 ceph_cap_string(cap->implemented & ~cap->issued));
1619 /* want more caps from mds? */
1620 if (want & ~(cap->mds_wanted | cap->issued))
1623 /* things we might delay */
1624 if ((cap->issued & ~retain) == 0 &&
1625 cap->mds_wanted == want)
1626 continue; /* nope, all good */
1632 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1633 time_before(jiffies, ci->i_hold_caps_max)) {
1634 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1635 ceph_cap_string(cap->issued),
1636 ceph_cap_string(cap->issued & retain),
1637 ceph_cap_string(cap->mds_wanted),
1638 ceph_cap_string(want));
1644 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1645 dout(" skipping %p I_NOFLUSH set\n", inode);
1649 if (session && session != cap->session) {
1650 dout("oops, wrong session %p mutex\n", session);
1651 mutex_unlock(&session->s_mutex);
1655 session = cap->session;
1656 if (mutex_trylock(&session->s_mutex) == 0) {
1657 dout("inverting session/ino locks on %p\n",
1659 spin_unlock(&ci->i_ceph_lock);
1660 if (took_snap_rwsem) {
1661 up_read(&mdsc->snap_rwsem);
1662 took_snap_rwsem = 0;
1664 mutex_lock(&session->s_mutex);
1668 /* take snap_rwsem after session mutex */
1669 if (!took_snap_rwsem) {
1670 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
1671 dout("inverting snap/in locks on %p\n",
1673 spin_unlock(&ci->i_ceph_lock);
1674 down_read(&mdsc->snap_rwsem);
1675 took_snap_rwsem = 1;
1678 took_snap_rwsem = 1;
1681 if (cap == ci->i_auth_cap && ci->i_dirty_caps)
1682 flushing = __mark_caps_flushing(inode, session);
1686 mds = cap->mds; /* remember mds, so we don't repeat */
1689 /* __send_cap drops i_ceph_lock */
1690 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, cap_used,
1691 want, retain, flushing, NULL);
1692 goto retry; /* retake i_ceph_lock and restart our cap scan. */
1696 * Reschedule delayed caps release if we delayed anything,
1699 if (delayed && is_delayed)
1700 force_requeue = 1; /* __send_cap delayed release; requeue */
1701 if (!delayed && !is_delayed)
1702 __cap_delay_cancel(mdsc, ci);
1703 else if (!is_delayed || force_requeue)
1704 __cap_delay_requeue(mdsc, ci);
1706 spin_unlock(&ci->i_ceph_lock);
1708 if (queue_invalidate)
1709 ceph_queue_invalidate(inode);
1712 mutex_unlock(&session->s_mutex);
1713 if (took_snap_rwsem)
1714 up_read(&mdsc->snap_rwsem);
1718 * Try to flush dirty caps back to the auth mds.
1720 static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session,
1721 unsigned *flush_tid)
1723 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1724 struct ceph_inode_info *ci = ceph_inode(inode);
1725 int unlock_session = session ? 0 : 1;
1729 spin_lock(&ci->i_ceph_lock);
1730 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1731 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
1734 if (ci->i_dirty_caps && ci->i_auth_cap) {
1735 struct ceph_cap *cap = ci->i_auth_cap;
1736 int used = __ceph_caps_used(ci);
1737 int want = __ceph_caps_wanted(ci);
1741 spin_unlock(&ci->i_ceph_lock);
1742 session = cap->session;
1743 mutex_lock(&session->s_mutex);
1746 BUG_ON(session != cap->session);
1747 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN)
1750 flushing = __mark_caps_flushing(inode, session);
1752 /* __send_cap drops i_ceph_lock */
1753 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want,
1754 cap->issued | cap->implemented, flushing,
1759 spin_lock(&ci->i_ceph_lock);
1760 __cap_delay_requeue(mdsc, ci);
1763 spin_unlock(&ci->i_ceph_lock);
1765 if (session && unlock_session)
1766 mutex_unlock(&session->s_mutex);
1771 * Return true if we've flushed caps through the given flush_tid.
1773 static int caps_are_flushed(struct inode *inode, unsigned tid)
1775 struct ceph_inode_info *ci = ceph_inode(inode);
1778 spin_lock(&ci->i_ceph_lock);
1779 for (i = 0; i < CEPH_CAP_BITS; i++)
1780 if ((ci->i_flushing_caps & (1 << i)) &&
1781 ci->i_cap_flush_tid[i] <= tid) {
1782 /* still flushing this bit */
1786 spin_unlock(&ci->i_ceph_lock);
1791 * Wait on any unsafe replies for the given inode. First wait on the
1792 * newest request, and make that the upper bound. Then, if there are
1793 * more requests, keep waiting on the oldest as long as it is still older
1794 * than the original request.
1796 static void sync_write_wait(struct inode *inode)
1798 struct ceph_inode_info *ci = ceph_inode(inode);
1799 struct list_head *head = &ci->i_unsafe_writes;
1800 struct ceph_osd_request *req;
1803 spin_lock(&ci->i_unsafe_lock);
1804 if (list_empty(head))
1807 /* set upper bound as _last_ entry in chain */
1808 req = list_entry(head->prev, struct ceph_osd_request,
1810 last_tid = req->r_tid;
1813 ceph_osdc_get_request(req);
1814 spin_unlock(&ci->i_unsafe_lock);
1815 dout("sync_write_wait on tid %llu (until %llu)\n",
1816 req->r_tid, last_tid);
1817 wait_for_completion(&req->r_safe_completion);
1818 spin_lock(&ci->i_unsafe_lock);
1819 ceph_osdc_put_request(req);
1822 * from here on look at first entry in chain, since we
1823 * only want to wait for anything older than last_tid
1825 if (list_empty(head))
1827 req = list_entry(head->next, struct ceph_osd_request,
1829 } while (req->r_tid < last_tid);
1831 spin_unlock(&ci->i_unsafe_lock);
1834 int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1836 struct inode *inode = file->f_mapping->host;
1837 struct ceph_inode_info *ci = ceph_inode(inode);
1842 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
1843 sync_write_wait(inode);
1845 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
1848 mutex_lock(&inode->i_mutex);
1850 dirty = try_flush_caps(inode, NULL, &flush_tid);
1851 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
1854 * only wait on non-file metadata writeback (the mds
1855 * can recover size and mtime, so we don't need to
1858 if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
1859 dout("fsync waiting for flush_tid %u\n", flush_tid);
1860 ret = wait_event_interruptible(ci->i_cap_wq,
1861 caps_are_flushed(inode, flush_tid));
1864 dout("fsync %p%s done\n", inode, datasync ? " datasync" : "");
1865 mutex_unlock(&inode->i_mutex);
1870 * Flush any dirty caps back to the mds. If we aren't asked to wait,
1871 * queue inode for flush but don't do so immediately, because we can
1872 * get by with fewer MDS messages if we wait for data writeback to
1875 int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
1877 struct ceph_inode_info *ci = ceph_inode(inode);
1881 int wait = wbc->sync_mode == WB_SYNC_ALL;
1883 dout("write_inode %p wait=%d\n", inode, wait);
1885 dirty = try_flush_caps(inode, NULL, &flush_tid);
1887 err = wait_event_interruptible(ci->i_cap_wq,
1888 caps_are_flushed(inode, flush_tid));
1890 struct ceph_mds_client *mdsc =
1891 ceph_sb_to_client(inode->i_sb)->mdsc;
1893 spin_lock(&ci->i_ceph_lock);
1894 if (__ceph_caps_dirty(ci))
1895 __cap_delay_requeue_front(mdsc, ci);
1896 spin_unlock(&ci->i_ceph_lock);
1902 * After a recovering MDS goes active, we need to resend any caps
1905 * Caller holds session->s_mutex.
1907 static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc,
1908 struct ceph_mds_session *session)
1910 struct ceph_cap_snap *capsnap;
1912 dout("kick_flushing_capsnaps mds%d\n", session->s_mds);
1913 list_for_each_entry(capsnap, &session->s_cap_snaps_flushing,
1915 struct ceph_inode_info *ci = capsnap->ci;
1916 struct inode *inode = &ci->vfs_inode;
1917 struct ceph_cap *cap;
1919 spin_lock(&ci->i_ceph_lock);
1920 cap = ci->i_auth_cap;
1921 if (cap && cap->session == session) {
1922 dout("kick_flushing_caps %p cap %p capsnap %p\n", inode,
1924 __ceph_flush_snaps(ci, &session, 1);
1926 pr_err("%p auth cap %p not mds%d ???\n", inode,
1927 cap, session->s_mds);
1929 spin_unlock(&ci->i_ceph_lock);
1933 void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
1934 struct ceph_mds_session *session)
1936 struct ceph_inode_info *ci;
1938 kick_flushing_capsnaps(mdsc, session);
1940 dout("kick_flushing_caps mds%d\n", session->s_mds);
1941 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
1942 struct inode *inode = &ci->vfs_inode;
1943 struct ceph_cap *cap;
1946 spin_lock(&ci->i_ceph_lock);
1947 cap = ci->i_auth_cap;
1948 if (cap && cap->session == session) {
1949 dout("kick_flushing_caps %p cap %p %s\n", inode,
1950 cap, ceph_cap_string(ci->i_flushing_caps));
1951 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
1952 __ceph_caps_used(ci),
1953 __ceph_caps_wanted(ci),
1954 cap->issued | cap->implemented,
1955 ci->i_flushing_caps, NULL);
1957 spin_lock(&ci->i_ceph_lock);
1958 __cap_delay_requeue(mdsc, ci);
1959 spin_unlock(&ci->i_ceph_lock);
1962 pr_err("%p auth cap %p not mds%d ???\n", inode,
1963 cap, session->s_mds);
1964 spin_unlock(&ci->i_ceph_lock);
1969 static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
1970 struct ceph_mds_session *session,
1971 struct inode *inode)
1973 struct ceph_inode_info *ci = ceph_inode(inode);
1974 struct ceph_cap *cap;
1977 spin_lock(&ci->i_ceph_lock);
1978 cap = ci->i_auth_cap;
1979 dout("kick_flushing_inode_caps %p flushing %s flush_seq %lld\n", inode,
1980 ceph_cap_string(ci->i_flushing_caps), ci->i_cap_flush_seq);
1981 __ceph_flush_snaps(ci, &session, 1);
1982 if (ci->i_flushing_caps) {
1983 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
1984 __ceph_caps_used(ci),
1985 __ceph_caps_wanted(ci),
1986 cap->issued | cap->implemented,
1987 ci->i_flushing_caps, NULL);
1989 spin_lock(&ci->i_ceph_lock);
1990 __cap_delay_requeue(mdsc, ci);
1991 spin_unlock(&ci->i_ceph_lock);
1994 spin_unlock(&ci->i_ceph_lock);
2000 * Take references to capabilities we hold, so that we don't release
2001 * them to the MDS prematurely.
2003 * Protected by i_ceph_lock.
2005 static void __take_cap_refs(struct ceph_inode_info *ci, int got)
2007 if (got & CEPH_CAP_PIN)
2009 if (got & CEPH_CAP_FILE_RD)
2011 if (got & CEPH_CAP_FILE_CACHE)
2012 ci->i_rdcache_ref++;
2013 if (got & CEPH_CAP_FILE_WR)
2015 if (got & CEPH_CAP_FILE_BUFFER) {
2016 if (ci->i_wb_ref == 0)
2017 ihold(&ci->vfs_inode);
2019 dout("__take_cap_refs %p wb %d -> %d (?)\n",
2020 &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
2025 * Try to grab cap references. Specify those refs we @want, and the
2026 * minimal set we @need. Also include the larger offset we are writing
2027 * to (when applicable), and check against max_size here as well.
2028 * Note that caller is responsible for ensuring max_size increases are
2029 * requested from the MDS.
2031 static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
2032 int *got, loff_t endoff, int *check_max, int *err)
2034 struct inode *inode = &ci->vfs_inode;
2036 int have, implemented;
2039 dout("get_cap_refs %p need %s want %s\n", inode,
2040 ceph_cap_string(need), ceph_cap_string(want));
2041 spin_lock(&ci->i_ceph_lock);
2043 /* make sure file is actually open */
2044 file_wanted = __ceph_caps_file_wanted(ci);
2045 if ((file_wanted & need) == 0) {
2046 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2047 ceph_cap_string(need), ceph_cap_string(file_wanted));
2053 if (need & CEPH_CAP_FILE_WR) {
2054 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2055 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2056 inode, endoff, ci->i_max_size);
2057 if (endoff > ci->i_wanted_max_size) {
2064 * If a sync write is in progress, we must wait, so that we
2065 * can get a final snapshot value for size+mtime.
2067 if (__ceph_have_pending_cap_snap(ci)) {
2068 dout("get_cap_refs %p cap_snap_pending\n", inode);
2072 have = __ceph_caps_issued(ci, &implemented);
2075 * disallow writes while a truncate is pending
2077 if (ci->i_truncate_pending)
2078 have &= ~CEPH_CAP_FILE_WR;
2080 if ((have & need) == need) {
2082 * Look at (implemented & ~have & not) so that we keep waiting
2083 * on transition from wanted -> needed caps. This is needed
2084 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2085 * going before a prior buffered writeback happens.
2087 int not = want & ~(have & need);
2088 int revoking = implemented & ~have;
2089 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2090 inode, ceph_cap_string(have), ceph_cap_string(not),
2091 ceph_cap_string(revoking));
2092 if ((revoking & not) == 0) {
2093 *got = need | (have & want);
2094 __take_cap_refs(ci, *got);
2098 dout("get_cap_refs %p have %s needed %s\n", inode,
2099 ceph_cap_string(have), ceph_cap_string(need));
2102 spin_unlock(&ci->i_ceph_lock);
2103 dout("get_cap_refs %p ret %d got %s\n", inode,
2104 ret, ceph_cap_string(*got));
2109 * Check the offset we are writing up to against our current
2110 * max_size. If necessary, tell the MDS we want to write to
2113 static void check_max_size(struct inode *inode, loff_t endoff)
2115 struct ceph_inode_info *ci = ceph_inode(inode);
2118 /* do we need to explicitly request a larger max_size? */
2119 spin_lock(&ci->i_ceph_lock);
2120 if ((endoff >= ci->i_max_size ||
2121 endoff > (inode->i_size << 1)) &&
2122 endoff > ci->i_wanted_max_size) {
2123 dout("write %p at large endoff %llu, req max_size\n",
2125 ci->i_wanted_max_size = endoff;
2128 spin_unlock(&ci->i_ceph_lock);
2130 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2134 * Wait for caps, and take cap references. If we can't get a WR cap
2135 * due to a small max_size, make sure we check_max_size (and possibly
2136 * ask the mds) so we don't get hung up indefinitely.
2138 int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got,
2141 int check_max, ret, err;
2145 check_max_size(&ci->vfs_inode, endoff);
2148 ret = wait_event_interruptible(ci->i_cap_wq,
2149 try_get_cap_refs(ci, need, want,
2160 * Take cap refs. Caller must already know we hold at least one ref
2161 * on the caps in question or we don't know this is safe.
2163 void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2165 spin_lock(&ci->i_ceph_lock);
2166 __take_cap_refs(ci, caps);
2167 spin_unlock(&ci->i_ceph_lock);
2173 * If we released the last ref on any given cap, call ceph_check_caps
2174 * to release (or schedule a release).
2176 * If we are releasing a WR cap (from a sync write), finalize any affected
2177 * cap_snap, and wake up any waiters.
2179 void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2181 struct inode *inode = &ci->vfs_inode;
2182 int last = 0, put = 0, flushsnaps = 0, wake = 0;
2183 struct ceph_cap_snap *capsnap;
2185 spin_lock(&ci->i_ceph_lock);
2186 if (had & CEPH_CAP_PIN)
2188 if (had & CEPH_CAP_FILE_RD)
2189 if (--ci->i_rd_ref == 0)
2191 if (had & CEPH_CAP_FILE_CACHE)
2192 if (--ci->i_rdcache_ref == 0)
2194 if (had & CEPH_CAP_FILE_BUFFER) {
2195 if (--ci->i_wb_ref == 0) {
2199 dout("put_cap_refs %p wb %d -> %d (?)\n",
2200 inode, ci->i_wb_ref+1, ci->i_wb_ref);
2202 if (had & CEPH_CAP_FILE_WR)
2203 if (--ci->i_wr_ref == 0) {
2205 if (!list_empty(&ci->i_cap_snaps)) {
2206 capsnap = list_first_entry(&ci->i_cap_snaps,
2207 struct ceph_cap_snap,
2209 if (capsnap->writing) {
2210 capsnap->writing = 0;
2212 __ceph_finish_cap_snap(ci,
2218 spin_unlock(&ci->i_ceph_lock);
2220 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2221 last ? " last" : "", put ? " put" : "");
2223 if (last && !flushsnaps)
2224 ceph_check_caps(ci, 0, NULL);
2225 else if (flushsnaps)
2226 ceph_flush_snaps(ci);
2228 wake_up_all(&ci->i_cap_wq);
2234 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2235 * context. Adjust per-snap dirty page accounting as appropriate.
2236 * Once all dirty data for a cap_snap is flushed, flush snapped file
2237 * metadata back to the MDS. If we dropped the last ref, call
2240 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2241 struct ceph_snap_context *snapc)
2243 struct inode *inode = &ci->vfs_inode;
2245 int complete_capsnap = 0;
2246 int drop_capsnap = 0;
2248 struct ceph_cap_snap *capsnap = NULL;
2250 spin_lock(&ci->i_ceph_lock);
2251 ci->i_wrbuffer_ref -= nr;
2252 last = !ci->i_wrbuffer_ref;
2254 if (ci->i_head_snapc == snapc) {
2255 ci->i_wrbuffer_ref_head -= nr;
2256 if (ci->i_wrbuffer_ref_head == 0 &&
2257 ci->i_dirty_caps == 0 && ci->i_flushing_caps == 0) {
2258 BUG_ON(!ci->i_head_snapc);
2259 ceph_put_snap_context(ci->i_head_snapc);
2260 ci->i_head_snapc = NULL;
2262 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2264 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2265 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2266 last ? " LAST" : "");
2268 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2269 if (capsnap->context == snapc) {
2275 capsnap->dirty_pages -= nr;
2276 if (capsnap->dirty_pages == 0) {
2277 complete_capsnap = 1;
2278 if (capsnap->dirty == 0)
2279 /* cap writeback completed before we created
2280 * the cap_snap; no FLUSHSNAP is needed */
2283 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2284 " snap %lld %d/%d -> %d/%d %s%s%s\n",
2285 inode, capsnap, capsnap->context->seq,
2286 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2287 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2288 last ? " (wrbuffer last)" : "",
2289 complete_capsnap ? " (complete capsnap)" : "",
2290 drop_capsnap ? " (drop capsnap)" : "");
2292 ceph_put_snap_context(capsnap->context);
2293 list_del(&capsnap->ci_item);
2294 list_del(&capsnap->flushing_item);
2295 ceph_put_cap_snap(capsnap);
2299 spin_unlock(&ci->i_ceph_lock);
2302 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2304 } else if (complete_capsnap) {
2305 ceph_flush_snaps(ci);
2306 wake_up_all(&ci->i_cap_wq);
2313 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2314 * actually be a revocation if it specifies a smaller cap set.)
2316 * caller holds s_mutex and i_ceph_lock, we drop both.
2320 * 1 - check_caps on auth cap only (writeback)
2321 * 2 - check_caps (ack revoke)
2323 static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant,
2324 struct ceph_mds_session *session,
2325 struct ceph_cap *cap,
2326 struct ceph_buffer *xattr_buf)
2327 __releases(ci->i_ceph_lock)
2329 struct ceph_inode_info *ci = ceph_inode(inode);
2330 int mds = session->s_mds;
2331 int seq = le32_to_cpu(grant->seq);
2332 int newcaps = le32_to_cpu(grant->caps);
2333 int issued, implemented, used, wanted, dirty;
2334 u64 size = le64_to_cpu(grant->size);
2335 u64 max_size = le64_to_cpu(grant->max_size);
2336 struct timespec mtime, atime, ctime;
2340 int revoked_rdcache = 0;
2341 int queue_invalidate = 0;
2343 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2344 inode, cap, mds, seq, ceph_cap_string(newcaps));
2345 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
2349 * If CACHE is being revoked, and we have no dirty buffers,
2350 * try to invalidate (once). (If there are dirty buffers, we
2351 * will invalidate _after_ writeback.)
2353 if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
2354 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
2355 !ci->i_wrbuffer_ref) {
2356 if (try_nonblocking_invalidate(inode) == 0) {
2357 revoked_rdcache = 1;
2359 /* there were locked pages.. invalidate later
2360 in a separate thread. */
2361 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
2362 queue_invalidate = 1;
2363 ci->i_rdcache_revoking = ci->i_rdcache_gen;
2368 /* side effects now are allowed */
2370 issued = __ceph_caps_issued(ci, &implemented);
2371 issued |= implemented | __ceph_caps_dirty(ci);
2373 cap->cap_gen = session->s_cap_gen;
2375 __check_cap_issue(ci, cap, newcaps);
2377 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
2378 inode->i_mode = le32_to_cpu(grant->mode);
2379 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
2380 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
2381 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
2382 from_kuid(&init_user_ns, inode->i_uid),
2383 from_kgid(&init_user_ns, inode->i_gid));
2386 if ((issued & CEPH_CAP_LINK_EXCL) == 0)
2387 set_nlink(inode, le32_to_cpu(grant->nlink));
2389 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
2390 int len = le32_to_cpu(grant->xattr_len);
2391 u64 version = le64_to_cpu(grant->xattr_version);
2393 if (version > ci->i_xattrs.version) {
2394 dout(" got new xattrs v%llu on %p len %d\n",
2395 version, inode, len);
2396 if (ci->i_xattrs.blob)
2397 ceph_buffer_put(ci->i_xattrs.blob);
2398 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
2399 ci->i_xattrs.version = version;
2403 /* size/ctime/mtime/atime? */
2404 ceph_fill_file_size(inode, issued,
2405 le32_to_cpu(grant->truncate_seq),
2406 le64_to_cpu(grant->truncate_size), size);
2407 ceph_decode_timespec(&mtime, &grant->mtime);
2408 ceph_decode_timespec(&atime, &grant->atime);
2409 ceph_decode_timespec(&ctime, &grant->ctime);
2410 ceph_fill_file_time(inode, issued,
2411 le32_to_cpu(grant->time_warp_seq), &ctime, &mtime,
2414 /* max size increase? */
2415 if (ci->i_auth_cap == cap && max_size != ci->i_max_size) {
2416 dout("max_size %lld -> %llu\n", ci->i_max_size, max_size);
2417 ci->i_max_size = max_size;
2418 if (max_size >= ci->i_wanted_max_size) {
2419 ci->i_wanted_max_size = 0; /* reset */
2420 ci->i_requested_max_size = 0;
2425 /* check cap bits */
2426 wanted = __ceph_caps_wanted(ci);
2427 used = __ceph_caps_used(ci);
2428 dirty = __ceph_caps_dirty(ci);
2429 dout(" my wanted = %s, used = %s, dirty %s\n",
2430 ceph_cap_string(wanted),
2431 ceph_cap_string(used),
2432 ceph_cap_string(dirty));
2433 if (wanted != le32_to_cpu(grant->wanted)) {
2434 dout("mds wanted %s -> %s\n",
2435 ceph_cap_string(le32_to_cpu(grant->wanted)),
2436 ceph_cap_string(wanted));
2437 /* imported cap may not have correct mds_wanted */
2438 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT)
2444 /* file layout may have changed */
2445 ci->i_layout = grant->layout;
2447 /* revocation, grant, or no-op? */
2448 if (cap->issued & ~newcaps) {
2449 int revoking = cap->issued & ~newcaps;
2451 dout("revocation: %s -> %s (revoking %s)\n",
2452 ceph_cap_string(cap->issued),
2453 ceph_cap_string(newcaps),
2454 ceph_cap_string(revoking));
2455 if (revoking & used & CEPH_CAP_FILE_BUFFER)
2456 writeback = 1; /* initiate writeback; will delay ack */
2457 else if (revoking == CEPH_CAP_FILE_CACHE &&
2458 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
2460 ; /* do nothing yet, invalidation will be queued */
2461 else if (cap == ci->i_auth_cap)
2462 check_caps = 1; /* check auth cap only */
2464 check_caps = 2; /* check all caps */
2465 cap->issued = newcaps;
2466 cap->implemented |= newcaps;
2467 } else if (cap->issued == newcaps) {
2468 dout("caps unchanged: %s -> %s\n",
2469 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
2471 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
2472 ceph_cap_string(newcaps));
2473 cap->issued = newcaps;
2474 cap->implemented |= newcaps; /* add bits only, to
2475 * avoid stepping on a
2476 * pending revocation */
2479 BUG_ON(cap->issued & ~cap->implemented);
2481 spin_unlock(&ci->i_ceph_lock);
2484 * queue inode for writeback: we can't actually call
2485 * filemap_write_and_wait, etc. from message handler
2488 ceph_queue_writeback(inode);
2489 if (queue_invalidate)
2490 ceph_queue_invalidate(inode);
2492 wake_up_all(&ci->i_cap_wq);
2494 if (check_caps == 1)
2495 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
2497 else if (check_caps == 2)
2498 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
2500 mutex_unlock(&session->s_mutex);
2504 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2505 * MDS has been safely committed.
2507 static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
2508 struct ceph_mds_caps *m,
2509 struct ceph_mds_session *session,
2510 struct ceph_cap *cap)
2511 __releases(ci->i_ceph_lock)
2513 struct ceph_inode_info *ci = ceph_inode(inode);
2514 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2515 unsigned seq = le32_to_cpu(m->seq);
2516 int dirty = le32_to_cpu(m->dirty);
2521 for (i = 0; i < CEPH_CAP_BITS; i++)
2522 if ((dirty & (1 << i)) &&
2523 flush_tid == ci->i_cap_flush_tid[i])
2526 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2527 " flushing %s -> %s\n",
2528 inode, session->s_mds, seq, ceph_cap_string(dirty),
2529 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
2530 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
2532 if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned))
2535 ci->i_flushing_caps &= ~cleaned;
2537 spin_lock(&mdsc->cap_dirty_lock);
2538 if (ci->i_flushing_caps == 0) {
2539 list_del_init(&ci->i_flushing_item);
2540 if (!list_empty(&session->s_cap_flushing))
2541 dout(" mds%d still flushing cap on %p\n",
2543 &list_entry(session->s_cap_flushing.next,
2544 struct ceph_inode_info,
2545 i_flushing_item)->vfs_inode);
2546 mdsc->num_cap_flushing--;
2547 wake_up_all(&mdsc->cap_flushing_wq);
2548 dout(" inode %p now !flushing\n", inode);
2550 if (ci->i_dirty_caps == 0) {
2551 dout(" inode %p now clean\n", inode);
2552 BUG_ON(!list_empty(&ci->i_dirty_item));
2554 if (ci->i_wrbuffer_ref_head == 0) {
2555 BUG_ON(!ci->i_head_snapc);
2556 ceph_put_snap_context(ci->i_head_snapc);
2557 ci->i_head_snapc = NULL;
2560 BUG_ON(list_empty(&ci->i_dirty_item));
2563 spin_unlock(&mdsc->cap_dirty_lock);
2564 wake_up_all(&ci->i_cap_wq);
2567 spin_unlock(&ci->i_ceph_lock);
2573 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
2574 * throw away our cap_snap.
2576 * Caller hold s_mutex.
2578 static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
2579 struct ceph_mds_caps *m,
2580 struct ceph_mds_session *session)
2582 struct ceph_inode_info *ci = ceph_inode(inode);
2583 u64 follows = le64_to_cpu(m->snap_follows);
2584 struct ceph_cap_snap *capsnap;
2587 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2588 inode, ci, session->s_mds, follows);
2590 spin_lock(&ci->i_ceph_lock);
2591 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2592 if (capsnap->follows == follows) {
2593 if (capsnap->flush_tid != flush_tid) {
2594 dout(" cap_snap %p follows %lld tid %lld !="
2595 " %lld\n", capsnap, follows,
2596 flush_tid, capsnap->flush_tid);
2599 WARN_ON(capsnap->dirty_pages || capsnap->writing);
2600 dout(" removing %p cap_snap %p follows %lld\n",
2601 inode, capsnap, follows);
2602 ceph_put_snap_context(capsnap->context);
2603 list_del(&capsnap->ci_item);
2604 list_del(&capsnap->flushing_item);
2605 ceph_put_cap_snap(capsnap);
2609 dout(" skipping cap_snap %p follows %lld\n",
2610 capsnap, capsnap->follows);
2613 spin_unlock(&ci->i_ceph_lock);
2619 * Handle TRUNC from MDS, indicating file truncation.
2621 * caller hold s_mutex.
2623 static void handle_cap_trunc(struct inode *inode,
2624 struct ceph_mds_caps *trunc,
2625 struct ceph_mds_session *session)
2626 __releases(ci->i_ceph_lock)
2628 struct ceph_inode_info *ci = ceph_inode(inode);
2629 int mds = session->s_mds;
2630 int seq = le32_to_cpu(trunc->seq);
2631 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
2632 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
2633 u64 size = le64_to_cpu(trunc->size);
2634 int implemented = 0;
2635 int dirty = __ceph_caps_dirty(ci);
2636 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
2637 int queue_trunc = 0;
2639 issued |= implemented | dirty;
2641 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2642 inode, mds, seq, truncate_size, truncate_seq);
2643 queue_trunc = ceph_fill_file_size(inode, issued,
2644 truncate_seq, truncate_size, size);
2645 spin_unlock(&ci->i_ceph_lock);
2648 ceph_queue_vmtruncate(inode);
2652 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
2653 * different one. If we are the most recent migration we've seen (as
2654 * indicated by mseq), make note of the migrating cap bits for the
2655 * duration (until we see the corresponding IMPORT).
2657 * caller holds s_mutex
2659 static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
2660 struct ceph_mds_session *session,
2661 int *open_target_sessions)
2663 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
2664 struct ceph_inode_info *ci = ceph_inode(inode);
2665 int mds = session->s_mds;
2666 unsigned mseq = le32_to_cpu(ex->migrate_seq);
2667 struct ceph_cap *cap = NULL, *t;
2671 dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
2672 inode, ci, mds, mseq);
2674 spin_lock(&ci->i_ceph_lock);
2676 /* make sure we haven't seen a higher mseq */
2677 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
2678 t = rb_entry(p, struct ceph_cap, ci_node);
2679 if (ceph_seq_cmp(t->mseq, mseq) > 0) {
2680 dout(" higher mseq on cap from mds%d\n",
2684 if (t->session->s_mds == mds)
2691 ci->i_cap_exporting_mds = mds;
2692 ci->i_cap_exporting_mseq = mseq;
2693 ci->i_cap_exporting_issued = cap->issued;
2696 * make sure we have open sessions with all possible
2697 * export targets, so that we get the matching IMPORT
2699 *open_target_sessions = 1;
2702 * we can't flush dirty caps that we've seen the
2703 * EXPORT but no IMPORT for
2705 spin_lock(&mdsc->cap_dirty_lock);
2706 if (!list_empty(&ci->i_dirty_item)) {
2707 dout(" moving %p to cap_dirty_migrating\n",
2709 list_move(&ci->i_dirty_item,
2710 &mdsc->cap_dirty_migrating);
2712 spin_unlock(&mdsc->cap_dirty_lock);
2714 __ceph_remove_cap(cap);
2716 /* else, we already released it */
2718 spin_unlock(&ci->i_ceph_lock);
2722 * Handle cap IMPORT. If there are temp bits from an older EXPORT,
2725 * caller holds s_mutex.
2727 static void handle_cap_import(struct ceph_mds_client *mdsc,
2728 struct inode *inode, struct ceph_mds_caps *im,
2729 struct ceph_mds_session *session,
2730 void *snaptrace, int snaptrace_len)
2732 struct ceph_inode_info *ci = ceph_inode(inode);
2733 int mds = session->s_mds;
2734 unsigned issued = le32_to_cpu(im->caps);
2735 unsigned wanted = le32_to_cpu(im->wanted);
2736 unsigned seq = le32_to_cpu(im->seq);
2737 unsigned mseq = le32_to_cpu(im->migrate_seq);
2738 u64 realmino = le64_to_cpu(im->realm);
2739 u64 cap_id = le64_to_cpu(im->cap_id);
2741 if (ci->i_cap_exporting_mds >= 0 &&
2742 ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) {
2743 dout("handle_cap_import inode %p ci %p mds%d mseq %d"
2744 " - cleared exporting from mds%d\n",
2745 inode, ci, mds, mseq,
2746 ci->i_cap_exporting_mds);
2747 ci->i_cap_exporting_issued = 0;
2748 ci->i_cap_exporting_mseq = 0;
2749 ci->i_cap_exporting_mds = -1;
2751 spin_lock(&mdsc->cap_dirty_lock);
2752 if (!list_empty(&ci->i_dirty_item)) {
2753 dout(" moving %p back to cap_dirty\n", inode);
2754 list_move(&ci->i_dirty_item, &mdsc->cap_dirty);
2756 spin_unlock(&mdsc->cap_dirty_lock);
2758 dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
2759 inode, ci, mds, mseq);
2762 down_write(&mdsc->snap_rwsem);
2763 ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len,
2765 downgrade_write(&mdsc->snap_rwsem);
2766 ceph_add_cap(inode, session, cap_id, -1,
2767 issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH,
2768 NULL /* no caps context */);
2769 kick_flushing_inode_caps(mdsc, session, inode);
2770 up_read(&mdsc->snap_rwsem);
2772 /* make sure we re-request max_size, if necessary */
2773 spin_lock(&ci->i_ceph_lock);
2774 ci->i_wanted_max_size = 0; /* reset */
2775 ci->i_requested_max_size = 0;
2776 spin_unlock(&ci->i_ceph_lock);
2780 * Handle a caps message from the MDS.
2782 * Identify the appropriate session, inode, and call the right handler
2783 * based on the cap op.
2785 void ceph_handle_caps(struct ceph_mds_session *session,
2786 struct ceph_msg *msg)
2788 struct ceph_mds_client *mdsc = session->s_mdsc;
2789 struct super_block *sb = mdsc->fsc->sb;
2790 struct inode *inode;
2791 struct ceph_inode_info *ci;
2792 struct ceph_cap *cap;
2793 struct ceph_mds_caps *h;
2794 int mds = session->s_mds;
2797 struct ceph_vino vino;
2802 size_t snaptrace_len;
2805 int open_target_sessions = 0;
2807 dout("handle_caps from mds%d\n", mds);
2810 tid = le64_to_cpu(msg->hdr.tid);
2811 if (msg->front.iov_len < sizeof(*h))
2813 h = msg->front.iov_base;
2814 op = le32_to_cpu(h->op);
2815 vino.ino = le64_to_cpu(h->ino);
2816 vino.snap = CEPH_NOSNAP;
2817 cap_id = le64_to_cpu(h->cap_id);
2818 seq = le32_to_cpu(h->seq);
2819 mseq = le32_to_cpu(h->migrate_seq);
2820 size = le64_to_cpu(h->size);
2821 max_size = le64_to_cpu(h->max_size);
2824 snaptrace_len = le32_to_cpu(h->snap_trace_len);
2826 if (le16_to_cpu(msg->hdr.version) >= 2) {
2829 p = snaptrace + snaptrace_len;
2830 end = msg->front.iov_base + msg->front.iov_len;
2831 ceph_decode_32_safe(&p, end, flock_len, bad);
2838 mutex_lock(&session->s_mutex);
2840 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
2843 if (op == CEPH_CAP_OP_IMPORT)
2844 ceph_add_cap_releases(mdsc, session);
2847 inode = ceph_find_inode(sb, vino);
2848 ci = ceph_inode(inode);
2849 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
2852 dout(" i don't have ino %llx\n", vino.ino);
2854 if (op == CEPH_CAP_OP_IMPORT)
2855 __queue_cap_release(session, vino.ino, cap_id,
2857 goto flush_cap_releases;
2860 /* these will work even if we don't have a cap yet */
2862 case CEPH_CAP_OP_FLUSHSNAP_ACK:
2863 handle_cap_flushsnap_ack(inode, tid, h, session);
2866 case CEPH_CAP_OP_EXPORT:
2867 handle_cap_export(inode, h, session, &open_target_sessions);
2870 case CEPH_CAP_OP_IMPORT:
2871 handle_cap_import(mdsc, inode, h, session,
2872 snaptrace, snaptrace_len);
2875 /* the rest require a cap */
2876 spin_lock(&ci->i_ceph_lock);
2877 cap = __get_cap_for_mds(ceph_inode(inode), mds);
2879 dout(" no cap on %p ino %llx.%llx from mds%d\n",
2880 inode, ceph_ino(inode), ceph_snap(inode), mds);
2881 spin_unlock(&ci->i_ceph_lock);
2882 goto flush_cap_releases;
2885 /* note that each of these drops i_ceph_lock for us */
2887 case CEPH_CAP_OP_REVOKE:
2888 case CEPH_CAP_OP_GRANT:
2889 case CEPH_CAP_OP_IMPORT:
2890 handle_cap_grant(inode, h, session, cap, msg->middle);
2893 case CEPH_CAP_OP_FLUSH_ACK:
2894 handle_cap_flush_ack(inode, tid, h, session, cap);
2897 case CEPH_CAP_OP_TRUNC:
2898 handle_cap_trunc(inode, h, session);
2902 spin_unlock(&ci->i_ceph_lock);
2903 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
2904 ceph_cap_op_name(op));
2911 * send any full release message to try to move things
2912 * along for the mds (who clearly thinks we still have this
2915 ceph_add_cap_releases(mdsc, session);
2916 ceph_send_cap_releases(mdsc, session);
2919 mutex_unlock(&session->s_mutex);
2923 if (open_target_sessions)
2924 ceph_mdsc_open_export_target_sessions(mdsc, session);
2928 pr_err("ceph_handle_caps: corrupt message\n");
2934 * Delayed work handler to process end of delayed cap release LRU list.
2936 void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
2938 struct ceph_inode_info *ci;
2939 int flags = CHECK_CAPS_NODELAY;
2941 dout("check_delayed_caps\n");
2943 spin_lock(&mdsc->cap_delay_lock);
2944 if (list_empty(&mdsc->cap_delay_list))
2946 ci = list_first_entry(&mdsc->cap_delay_list,
2947 struct ceph_inode_info,
2949 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
2950 time_before(jiffies, ci->i_hold_caps_max))
2952 list_del_init(&ci->i_cap_delay_list);
2953 spin_unlock(&mdsc->cap_delay_lock);
2954 dout("check_delayed_caps on %p\n", &ci->vfs_inode);
2955 ceph_check_caps(ci, flags, NULL);
2957 spin_unlock(&mdsc->cap_delay_lock);
2961 * Flush all dirty caps to the mds
2963 void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
2965 struct ceph_inode_info *ci;
2966 struct inode *inode;
2968 dout("flush_dirty_caps\n");
2969 spin_lock(&mdsc->cap_dirty_lock);
2970 while (!list_empty(&mdsc->cap_dirty)) {
2971 ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
2973 inode = &ci->vfs_inode;
2975 dout("flush_dirty_caps %p\n", inode);
2976 spin_unlock(&mdsc->cap_dirty_lock);
2977 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
2979 spin_lock(&mdsc->cap_dirty_lock);
2981 spin_unlock(&mdsc->cap_dirty_lock);
2982 dout("flush_dirty_caps done\n");
2986 * Drop open file reference. If we were the last open file,
2987 * we may need to release capabilities to the MDS (or schedule
2988 * their delayed release).
2990 void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
2992 struct inode *inode = &ci->vfs_inode;
2995 spin_lock(&ci->i_ceph_lock);
2996 dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode,
2997 ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1);
2998 BUG_ON(ci->i_nr_by_mode[fmode] == 0);
2999 if (--ci->i_nr_by_mode[fmode] == 0)
3001 spin_unlock(&ci->i_ceph_lock);
3003 if (last && ci->i_vino.snap == CEPH_NOSNAP)
3004 ceph_check_caps(ci, 0, NULL);
3008 * Helpers for embedding cap and dentry lease releases into mds
3011 * @force is used by dentry_release (below) to force inclusion of a
3012 * record for the directory inode, even when there aren't any caps to
3015 int ceph_encode_inode_release(void **p, struct inode *inode,
3016 int mds, int drop, int unless, int force)
3018 struct ceph_inode_info *ci = ceph_inode(inode);
3019 struct ceph_cap *cap;
3020 struct ceph_mds_request_release *rel = *p;
3024 spin_lock(&ci->i_ceph_lock);
3025 used = __ceph_caps_used(ci);
3026 dirty = __ceph_caps_dirty(ci);
3028 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
3029 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
3030 ceph_cap_string(unless));
3032 /* only drop unused, clean caps */
3033 drop &= ~(used | dirty);
3035 cap = __get_cap_for_mds(ci, mds);
3036 if (cap && __cap_is_valid(cap)) {
3038 ((cap->issued & drop) &&
3039 (cap->issued & unless) == 0)) {
3040 if ((cap->issued & drop) &&
3041 (cap->issued & unless) == 0) {
3042 dout("encode_inode_release %p cap %p %s -> "
3044 ceph_cap_string(cap->issued),
3045 ceph_cap_string(cap->issued & ~drop));
3046 cap->issued &= ~drop;
3047 cap->implemented &= ~drop;
3048 if (ci->i_ceph_flags & CEPH_I_NODELAY) {
3049 int wanted = __ceph_caps_wanted(ci);
3050 dout(" wanted %s -> %s (act %s)\n",
3051 ceph_cap_string(cap->mds_wanted),
3052 ceph_cap_string(cap->mds_wanted &
3054 ceph_cap_string(wanted));
3055 cap->mds_wanted &= wanted;
3058 dout("encode_inode_release %p cap %p %s"
3059 " (force)\n", inode, cap,
3060 ceph_cap_string(cap->issued));
3063 rel->ino = cpu_to_le64(ceph_ino(inode));
3064 rel->cap_id = cpu_to_le64(cap->cap_id);
3065 rel->seq = cpu_to_le32(cap->seq);
3066 rel->issue_seq = cpu_to_le32(cap->issue_seq),
3067 rel->mseq = cpu_to_le32(cap->mseq);
3068 rel->caps = cpu_to_le32(cap->issued);
3069 rel->wanted = cpu_to_le32(cap->mds_wanted);
3075 dout("encode_inode_release %p cap %p %s\n",
3076 inode, cap, ceph_cap_string(cap->issued));
3079 spin_unlock(&ci->i_ceph_lock);
3083 int ceph_encode_dentry_release(void **p, struct dentry *dentry,
3084 int mds, int drop, int unless)
3086 struct inode *dir = dentry->d_parent->d_inode;
3087 struct ceph_mds_request_release *rel = *p;
3088 struct ceph_dentry_info *di = ceph_dentry(dentry);
3093 * force an record for the directory caps if we have a dentry lease.
3094 * this is racy (can't take i_ceph_lock and d_lock together), but it
3095 * doesn't have to be perfect; the mds will revoke anything we don't
3098 spin_lock(&dentry->d_lock);
3099 if (di->lease_session && di->lease_session->s_mds == mds)
3101 spin_unlock(&dentry->d_lock);
3103 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
3105 spin_lock(&dentry->d_lock);
3106 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
3107 dout("encode_dentry_release %p mds%d seq %d\n",
3108 dentry, mds, (int)di->lease_seq);
3109 rel->dname_len = cpu_to_le32(dentry->d_name.len);
3110 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
3111 *p += dentry->d_name.len;
3112 rel->dname_seq = cpu_to_le32(di->lease_seq);
3113 __ceph_mdsc_drop_dentry_lease(dentry);
3115 spin_unlock(&dentry->d_lock);