1 #include "ceph_debug.h"
4 #include <linux/kernel.h>
5 #include <linux/sched.h>
6 #include <linux/slab.h>
7 #include <linux/vmalloc.h>
8 #include <linux/wait.h>
9 #include <linux/writeback.h>
13 #include "messenger.h"
16 * Capability management
18 * The Ceph metadata servers control client access to inode metadata
19 * and file data by issuing capabilities, granting clients permission
20 * to read and/or write both inode field and file data to OSDs
21 * (storage nodes). Each capability consists of a set of bits
22 * indicating which operations are allowed.
24 * If the client holds a *_SHARED cap, the client has a coherent value
25 * that can be safely read from the cached inode.
27 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
28 * client is allowed to change inode attributes (e.g., file size,
29 * mtime), note its dirty state in the ceph_cap, and asynchronously
30 * flush that metadata change to the MDS.
32 * In the event of a conflicting operation (perhaps by another
33 * client), the MDS will revoke the conflicting client capabilities.
35 * In order for a client to cache an inode, it must hold a capability
36 * with at least one MDS server. When inodes are released, release
37 * notifications are batched and periodically sent en masse to the MDS
38 * cluster to release server state.
43 * Generate readable cap strings for debugging output.
45 #define MAX_CAP_STR 20
46 static char cap_str[MAX_CAP_STR][40];
47 static DEFINE_SPINLOCK(cap_str_lock);
48 static int last_cap_str;
50 static char *gcap_string(char *s, int c)
52 if (c & CEPH_CAP_GSHARED)
54 if (c & CEPH_CAP_GEXCL)
56 if (c & CEPH_CAP_GCACHE)
62 if (c & CEPH_CAP_GBUFFER)
64 if (c & CEPH_CAP_GLAZYIO)
69 const char *ceph_cap_string(int caps)
75 spin_lock(&cap_str_lock);
77 if (last_cap_str == MAX_CAP_STR)
79 spin_unlock(&cap_str_lock);
83 if (caps & CEPH_CAP_PIN)
86 c = (caps >> CEPH_CAP_SAUTH) & 3;
89 s = gcap_string(s, c);
92 c = (caps >> CEPH_CAP_SLINK) & 3;
95 s = gcap_string(s, c);
98 c = (caps >> CEPH_CAP_SXATTR) & 3;
101 s = gcap_string(s, c);
104 c = caps >> CEPH_CAP_SFILE;
107 s = gcap_string(s, c);
119 * Maintain a global pool of preallocated struct ceph_caps, referenced
120 * by struct ceph_caps_reservations. This ensures that we preallocate
121 * memory needed to successfully process an MDS response. (If an MDS
122 * sends us cap information and we fail to process it, we will have
123 * problems due to the client and MDS being out of sync.)
125 * Reservations are 'owned' by a ceph_cap_reservation context.
127 static spinlock_t caps_list_lock;
128 static struct list_head caps_list; /* unused (reserved or unreserved) */
129 static int caps_total_count; /* total caps allocated */
130 static int caps_use_count; /* in use */
131 static int caps_reserve_count; /* unused, reserved */
132 static int caps_avail_count; /* unused, unreserved */
133 static int caps_min_count; /* keep at least this many (unreserved) */
135 void __init ceph_caps_init(void)
137 INIT_LIST_HEAD(&caps_list);
138 spin_lock_init(&caps_list_lock);
141 void ceph_caps_finalize(void)
143 struct ceph_cap *cap;
145 spin_lock(&caps_list_lock);
146 while (!list_empty(&caps_list)) {
147 cap = list_first_entry(&caps_list, struct ceph_cap, caps_item);
148 list_del(&cap->caps_item);
149 kmem_cache_free(ceph_cap_cachep, cap);
151 caps_total_count = 0;
152 caps_avail_count = 0;
154 caps_reserve_count = 0;
156 spin_unlock(&caps_list_lock);
159 void ceph_adjust_min_caps(int delta)
161 spin_lock(&caps_list_lock);
162 caps_min_count += delta;
163 BUG_ON(caps_min_count < 0);
164 spin_unlock(&caps_list_lock);
167 int ceph_reserve_caps(struct ceph_cap_reservation *ctx, int need)
170 struct ceph_cap *cap;
176 dout("reserve caps ctx=%p need=%d\n", ctx, need);
178 /* first reserve any caps that are already allocated */
179 spin_lock(&caps_list_lock);
180 if (caps_avail_count >= need)
183 have = caps_avail_count;
184 caps_avail_count -= have;
185 caps_reserve_count += have;
186 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count +
188 spin_unlock(&caps_list_lock);
190 for (i = have; i < need; i++) {
191 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
194 goto out_alloc_count;
196 list_add(&cap->caps_item, &newcaps);
199 BUG_ON(have + alloc != need);
201 spin_lock(&caps_list_lock);
202 caps_total_count += alloc;
203 caps_reserve_count += alloc;
204 list_splice(&newcaps, &caps_list);
206 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count +
208 spin_unlock(&caps_list_lock);
211 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
212 ctx, caps_total_count, caps_use_count, caps_reserve_count,
217 /* we didn't manage to reserve as much as we needed */
218 pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
223 int ceph_unreserve_caps(struct ceph_cap_reservation *ctx)
225 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
227 spin_lock(&caps_list_lock);
228 BUG_ON(caps_reserve_count < ctx->count);
229 caps_reserve_count -= ctx->count;
230 caps_avail_count += ctx->count;
232 dout("unreserve caps %d = %d used + %d resv + %d avail\n",
233 caps_total_count, caps_use_count, caps_reserve_count,
235 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count +
237 spin_unlock(&caps_list_lock);
242 static struct ceph_cap *get_cap(struct ceph_cap_reservation *ctx)
244 struct ceph_cap *cap = NULL;
246 /* temporary, until we do something about cap import/export */
248 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
256 spin_lock(&caps_list_lock);
257 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
258 ctx, ctx->count, caps_total_count, caps_use_count,
259 caps_reserve_count, caps_avail_count);
261 BUG_ON(ctx->count > caps_reserve_count);
262 BUG_ON(list_empty(&caps_list));
265 caps_reserve_count--;
268 cap = list_first_entry(&caps_list, struct ceph_cap, caps_item);
269 list_del(&cap->caps_item);
271 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count +
273 spin_unlock(&caps_list_lock);
277 void ceph_put_cap(struct ceph_cap *cap)
279 spin_lock(&caps_list_lock);
280 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
281 cap, caps_total_count, caps_use_count,
282 caps_reserve_count, caps_avail_count);
285 * Keep some preallocated caps around (ceph_min_count), to
286 * avoid lots of free/alloc churn.
288 if (caps_avail_count >= caps_reserve_count + caps_min_count) {
290 kmem_cache_free(ceph_cap_cachep, cap);
293 list_add(&cap->caps_item, &caps_list);
296 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count +
298 spin_unlock(&caps_list_lock);
301 void ceph_reservation_status(struct ceph_client *client,
302 int *total, int *avail, int *used, int *reserved,
306 *total = caps_total_count;
308 *avail = caps_avail_count;
310 *used = caps_use_count;
312 *reserved = caps_reserve_count;
314 *min = caps_min_count;
318 * Find ceph_cap for given mds, if any.
320 * Called with i_lock held.
322 static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
324 struct ceph_cap *cap;
325 struct rb_node *n = ci->i_caps.rb_node;
328 cap = rb_entry(n, struct ceph_cap, ci_node);
331 else if (mds > cap->mds)
340 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
342 static int __ceph_get_cap_mds(struct ceph_inode_info *ci, u32 *mseq)
344 struct ceph_cap *cap;
348 /* prefer mds with WR|BUFFER|EXCL caps */
349 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
350 cap = rb_entry(p, struct ceph_cap, ci_node);
354 if (cap->issued & (CEPH_CAP_FILE_WR |
355 CEPH_CAP_FILE_BUFFER |
362 int ceph_get_cap_mds(struct inode *inode)
365 spin_lock(&inode->i_lock);
366 mds = __ceph_get_cap_mds(ceph_inode(inode), NULL);
367 spin_unlock(&inode->i_lock);
372 * Called under i_lock.
374 static void __insert_cap_node(struct ceph_inode_info *ci,
375 struct ceph_cap *new)
377 struct rb_node **p = &ci->i_caps.rb_node;
378 struct rb_node *parent = NULL;
379 struct ceph_cap *cap = NULL;
383 cap = rb_entry(parent, struct ceph_cap, ci_node);
384 if (new->mds < cap->mds)
386 else if (new->mds > cap->mds)
392 rb_link_node(&new->ci_node, parent, p);
393 rb_insert_color(&new->ci_node, &ci->i_caps);
397 * (re)set cap hold timeouts, which control the delayed release
398 * of unused caps back to the MDS. Should be called on cap use.
400 static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
401 struct ceph_inode_info *ci)
403 struct ceph_mount_args *ma = mdsc->client->mount_args;
405 ci->i_hold_caps_min = round_jiffies(jiffies +
406 ma->caps_wanted_delay_min * HZ);
407 ci->i_hold_caps_max = round_jiffies(jiffies +
408 ma->caps_wanted_delay_max * HZ);
409 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
410 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
414 * (Re)queue cap at the end of the delayed cap release list.
416 * If I_FLUSH is set, leave the inode at the front of the list.
418 * Caller holds i_lock
419 * -> we take mdsc->cap_delay_lock
421 static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
422 struct ceph_inode_info *ci)
424 __cap_set_timeouts(mdsc, ci);
425 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
426 ci->i_ceph_flags, ci->i_hold_caps_max);
427 if (!mdsc->stopping) {
428 spin_lock(&mdsc->cap_delay_lock);
429 if (!list_empty(&ci->i_cap_delay_list)) {
430 if (ci->i_ceph_flags & CEPH_I_FLUSH)
432 list_del_init(&ci->i_cap_delay_list);
434 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
436 spin_unlock(&mdsc->cap_delay_lock);
441 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
442 * indicating we should send a cap message to flush dirty metadata
443 * asap, and move to the front of the delayed cap list.
445 static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
446 struct ceph_inode_info *ci)
448 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
449 spin_lock(&mdsc->cap_delay_lock);
450 ci->i_ceph_flags |= CEPH_I_FLUSH;
451 if (!list_empty(&ci->i_cap_delay_list))
452 list_del_init(&ci->i_cap_delay_list);
453 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
454 spin_unlock(&mdsc->cap_delay_lock);
458 * Cancel delayed work on cap.
460 * Caller must hold i_lock.
462 static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
463 struct ceph_inode_info *ci)
465 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
466 if (list_empty(&ci->i_cap_delay_list))
468 spin_lock(&mdsc->cap_delay_lock);
469 list_del_init(&ci->i_cap_delay_list);
470 spin_unlock(&mdsc->cap_delay_lock);
474 * Common issue checks for add_cap, handle_cap_grant.
476 static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
479 unsigned had = __ceph_caps_issued(ci, NULL);
482 * Each time we receive FILE_CACHE anew, we increment
485 if ((issued & CEPH_CAP_FILE_CACHE) &&
486 (had & CEPH_CAP_FILE_CACHE) == 0)
490 * if we are newly issued FILE_SHARED, clear I_COMPLETE; we
491 * don't know what happened to this directory while we didn't
494 if ((issued & CEPH_CAP_FILE_SHARED) &&
495 (had & CEPH_CAP_FILE_SHARED) == 0) {
497 if (S_ISDIR(ci->vfs_inode.i_mode)) {
498 dout(" marking %p NOT complete\n", &ci->vfs_inode);
499 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
505 * Add a capability under the given MDS session.
507 * Caller should hold session snap_rwsem (read) and s_mutex.
509 * @fmode is the open file mode, if we are opening a file, otherwise
510 * it is < 0. (This is so we can atomically add the cap and add an
511 * open file reference to it.)
513 int ceph_add_cap(struct inode *inode,
514 struct ceph_mds_session *session, u64 cap_id,
515 int fmode, unsigned issued, unsigned wanted,
516 unsigned seq, unsigned mseq, u64 realmino, int flags,
517 struct ceph_cap_reservation *caps_reservation)
519 struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc;
520 struct ceph_inode_info *ci = ceph_inode(inode);
521 struct ceph_cap *new_cap = NULL;
522 struct ceph_cap *cap;
523 int mds = session->s_mds;
526 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
527 session->s_mds, cap_id, ceph_cap_string(issued), seq);
530 * If we are opening the file, include file mode wanted bits
534 wanted |= ceph_caps_for_mode(fmode);
537 spin_lock(&inode->i_lock);
538 cap = __get_cap_for_mds(ci, mds);
544 spin_unlock(&inode->i_lock);
545 new_cap = get_cap(caps_reservation);
552 cap->implemented = 0;
557 __insert_cap_node(ci, cap);
559 /* clear out old exporting info? (i.e. on cap import) */
560 if (ci->i_cap_exporting_mds == mds) {
561 ci->i_cap_exporting_issued = 0;
562 ci->i_cap_exporting_mseq = 0;
563 ci->i_cap_exporting_mds = -1;
566 /* add to session cap list */
567 cap->session = session;
568 spin_lock(&session->s_cap_lock);
569 list_add_tail(&cap->session_caps, &session->s_caps);
570 session->s_nr_caps++;
571 spin_unlock(&session->s_cap_lock);
574 if (!ci->i_snap_realm) {
576 * add this inode to the appropriate snap realm
578 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
581 ceph_get_snap_realm(mdsc, realm);
582 spin_lock(&realm->inodes_with_caps_lock);
583 ci->i_snap_realm = realm;
584 list_add(&ci->i_snap_realm_item,
585 &realm->inodes_with_caps);
586 spin_unlock(&realm->inodes_with_caps_lock);
588 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
593 __check_cap_issue(ci, cap, issued);
596 * If we are issued caps we don't want, or the mds' wanted
597 * value appears to be off, queue a check so we'll release
598 * later and/or update the mds wanted value.
600 actual_wanted = __ceph_caps_wanted(ci);
601 if ((wanted & ~actual_wanted) ||
602 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
603 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
604 ceph_cap_string(issued), ceph_cap_string(wanted),
605 ceph_cap_string(actual_wanted));
606 __cap_delay_requeue(mdsc, ci);
609 if (flags & CEPH_CAP_FLAG_AUTH)
610 ci->i_auth_cap = cap;
611 else if (ci->i_auth_cap == cap)
612 ci->i_auth_cap = NULL;
614 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
615 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
616 ceph_cap_string(issued|cap->issued), seq, mds);
617 cap->cap_id = cap_id;
618 cap->issued = issued;
619 cap->implemented |= issued;
620 cap->mds_wanted |= wanted;
622 cap->issue_seq = seq;
624 cap->cap_gen = session->s_cap_gen;
627 __ceph_get_fmode(ci, fmode);
628 spin_unlock(&inode->i_lock);
629 wake_up_all(&ci->i_cap_wq);
634 * Return true if cap has not timed out and belongs to the current
635 * generation of the MDS session (i.e. has not gone 'stale' due to
636 * us losing touch with the mds).
638 static int __cap_is_valid(struct ceph_cap *cap)
643 spin_lock(&cap->session->s_cap_lock);
644 gen = cap->session->s_cap_gen;
645 ttl = cap->session->s_cap_ttl;
646 spin_unlock(&cap->session->s_cap_lock);
648 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
649 dout("__cap_is_valid %p cap %p issued %s "
650 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
651 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
659 * Return set of valid cap bits issued to us. Note that caps time
660 * out, and may be invalidated in bulk if the client session times out
661 * and session->s_cap_gen is bumped.
663 int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
665 int have = ci->i_snap_caps | ci->i_cap_exporting_issued;
666 struct ceph_cap *cap;
671 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
672 cap = rb_entry(p, struct ceph_cap, ci_node);
673 if (!__cap_is_valid(cap))
675 dout("__ceph_caps_issued %p cap %p issued %s\n",
676 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
679 *implemented |= cap->implemented;
685 * Get cap bits issued by caps other than @ocap
687 int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
689 int have = ci->i_snap_caps;
690 struct ceph_cap *cap;
693 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
694 cap = rb_entry(p, struct ceph_cap, ci_node);
697 if (!__cap_is_valid(cap))
705 * Move a cap to the end of the LRU (oldest caps at list head, newest
708 static void __touch_cap(struct ceph_cap *cap)
710 struct ceph_mds_session *s = cap->session;
712 spin_lock(&s->s_cap_lock);
713 if (s->s_cap_iterator == NULL) {
714 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
716 list_move_tail(&cap->session_caps, &s->s_caps);
718 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
719 &cap->ci->vfs_inode, cap, s->s_mds);
721 spin_unlock(&s->s_cap_lock);
725 * Check if we hold the given mask. If so, move the cap(s) to the
726 * front of their respective LRUs. (This is the preferred way for
727 * callers to check for caps they want.)
729 int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
731 struct ceph_cap *cap;
733 int have = ci->i_snap_caps;
735 if ((have & mask) == mask) {
736 dout("__ceph_caps_issued_mask %p snap issued %s"
737 " (mask %s)\n", &ci->vfs_inode,
738 ceph_cap_string(have),
739 ceph_cap_string(mask));
743 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
744 cap = rb_entry(p, struct ceph_cap, ci_node);
745 if (!__cap_is_valid(cap))
747 if ((cap->issued & mask) == mask) {
748 dout("__ceph_caps_issued_mask %p cap %p issued %s"
749 " (mask %s)\n", &ci->vfs_inode, cap,
750 ceph_cap_string(cap->issued),
751 ceph_cap_string(mask));
757 /* does a combination of caps satisfy mask? */
759 if ((have & mask) == mask) {
760 dout("__ceph_caps_issued_mask %p combo issued %s"
761 " (mask %s)\n", &ci->vfs_inode,
762 ceph_cap_string(cap->issued),
763 ceph_cap_string(mask));
767 /* touch this + preceeding caps */
769 for (q = rb_first(&ci->i_caps); q != p;
771 cap = rb_entry(q, struct ceph_cap,
773 if (!__cap_is_valid(cap))
786 * Return true if mask caps are currently being revoked by an MDS.
788 int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
790 struct inode *inode = &ci->vfs_inode;
791 struct ceph_cap *cap;
795 spin_lock(&inode->i_lock);
796 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
797 cap = rb_entry(p, struct ceph_cap, ci_node);
798 if (__cap_is_valid(cap) &&
799 (cap->implemented & ~cap->issued & mask)) {
804 spin_unlock(&inode->i_lock);
805 dout("ceph_caps_revoking %p %s = %d\n", inode,
806 ceph_cap_string(mask), ret);
810 int __ceph_caps_used(struct ceph_inode_info *ci)
814 used |= CEPH_CAP_PIN;
816 used |= CEPH_CAP_FILE_RD;
817 if (ci->i_rdcache_ref || ci->i_rdcache_gen)
818 used |= CEPH_CAP_FILE_CACHE;
820 used |= CEPH_CAP_FILE_WR;
821 if (ci->i_wrbuffer_ref)
822 used |= CEPH_CAP_FILE_BUFFER;
827 * wanted, by virtue of open file modes
829 int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
833 for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++)
834 if (ci->i_nr_by_mode[mode])
835 want |= ceph_caps_for_mode(mode);
840 * Return caps we have registered with the MDS(s) as 'wanted'.
842 int __ceph_caps_mds_wanted(struct ceph_inode_info *ci)
844 struct ceph_cap *cap;
848 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
849 cap = rb_entry(p, struct ceph_cap, ci_node);
850 if (!__cap_is_valid(cap))
852 mds_wanted |= cap->mds_wanted;
858 * called under i_lock
860 static int __ceph_is_any_caps(struct ceph_inode_info *ci)
862 return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0;
866 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
868 * caller should hold i_lock.
869 * caller will not hold session s_mutex if called from destroy_inode.
871 void __ceph_remove_cap(struct ceph_cap *cap)
873 struct ceph_mds_session *session = cap->session;
874 struct ceph_inode_info *ci = cap->ci;
875 struct ceph_mds_client *mdsc =
876 &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
879 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
881 /* remove from session list */
882 spin_lock(&session->s_cap_lock);
883 if (session->s_cap_iterator == cap) {
884 /* not yet, we are iterating over this very cap */
885 dout("__ceph_remove_cap delaying %p removal from session %p\n",
888 list_del_init(&cap->session_caps);
889 session->s_nr_caps--;
893 /* protect backpointer with s_cap_lock: see iterate_session_caps */
895 spin_unlock(&session->s_cap_lock);
897 /* remove from inode list */
898 rb_erase(&cap->ci_node, &ci->i_caps);
899 if (ci->i_auth_cap == cap)
900 ci->i_auth_cap = NULL;
905 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) {
906 struct ceph_snap_realm *realm = ci->i_snap_realm;
907 spin_lock(&realm->inodes_with_caps_lock);
908 list_del_init(&ci->i_snap_realm_item);
909 ci->i_snap_realm_counter++;
910 ci->i_snap_realm = NULL;
911 spin_unlock(&realm->inodes_with_caps_lock);
912 ceph_put_snap_realm(mdsc, realm);
914 if (!__ceph_is_any_real_caps(ci))
915 __cap_delay_cancel(mdsc, ci);
919 * Build and send a cap message to the given MDS.
921 * Caller should be holding s_mutex.
923 static int send_cap_msg(struct ceph_mds_session *session,
924 u64 ino, u64 cid, int op,
925 int caps, int wanted, int dirty,
926 u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq,
927 u64 size, u64 max_size,
928 struct timespec *mtime, struct timespec *atime,
930 uid_t uid, gid_t gid, mode_t mode,
932 struct ceph_buffer *xattrs_buf,
935 struct ceph_mds_caps *fc;
936 struct ceph_msg *msg;
938 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
939 " seq %u/%u mseq %u follows %lld size %llu/%llu"
940 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op),
941 cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted),
942 ceph_cap_string(dirty),
943 seq, issue_seq, mseq, follows, size, max_size,
944 xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0);
946 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS);
950 msg->hdr.tid = cpu_to_le64(flush_tid);
952 fc = msg->front.iov_base;
953 memset(fc, 0, sizeof(*fc));
955 fc->cap_id = cpu_to_le64(cid);
956 fc->op = cpu_to_le32(op);
957 fc->seq = cpu_to_le32(seq);
958 fc->issue_seq = cpu_to_le32(issue_seq);
959 fc->migrate_seq = cpu_to_le32(mseq);
960 fc->caps = cpu_to_le32(caps);
961 fc->wanted = cpu_to_le32(wanted);
962 fc->dirty = cpu_to_le32(dirty);
963 fc->ino = cpu_to_le64(ino);
964 fc->snap_follows = cpu_to_le64(follows);
966 fc->size = cpu_to_le64(size);
967 fc->max_size = cpu_to_le64(max_size);
969 ceph_encode_timespec(&fc->mtime, mtime);
971 ceph_encode_timespec(&fc->atime, atime);
972 fc->time_warp_seq = cpu_to_le32(time_warp_seq);
974 fc->uid = cpu_to_le32(uid);
975 fc->gid = cpu_to_le32(gid);
976 fc->mode = cpu_to_le32(mode);
978 fc->xattr_version = cpu_to_le64(xattr_version);
980 msg->middle = ceph_buffer_get(xattrs_buf);
981 fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len);
982 msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len);
985 ceph_con_send(&session->s_con, msg);
989 static void __queue_cap_release(struct ceph_mds_session *session,
990 u64 ino, u64 cap_id, u32 migrate_seq,
993 struct ceph_msg *msg;
994 struct ceph_mds_cap_release *head;
995 struct ceph_mds_cap_item *item;
997 spin_lock(&session->s_cap_lock);
998 BUG_ON(!session->s_num_cap_releases);
999 msg = list_first_entry(&session->s_cap_releases,
1000 struct ceph_msg, list_head);
1002 dout(" adding %llx release to mds%d msg %p (%d left)\n",
1003 ino, session->s_mds, msg, session->s_num_cap_releases);
1005 BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE);
1006 head = msg->front.iov_base;
1007 head->num = cpu_to_le32(le32_to_cpu(head->num) + 1);
1008 item = msg->front.iov_base + msg->front.iov_len;
1009 item->ino = cpu_to_le64(ino);
1010 item->cap_id = cpu_to_le64(cap_id);
1011 item->migrate_seq = cpu_to_le32(migrate_seq);
1012 item->seq = cpu_to_le32(issue_seq);
1014 session->s_num_cap_releases--;
1016 msg->front.iov_len += sizeof(*item);
1017 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
1018 dout(" release msg %p full\n", msg);
1019 list_move_tail(&msg->list_head, &session->s_cap_releases_done);
1021 dout(" release msg %p at %d/%d (%d)\n", msg,
1022 (int)le32_to_cpu(head->num),
1023 (int)CEPH_CAPS_PER_RELEASE,
1024 (int)msg->front.iov_len);
1026 spin_unlock(&session->s_cap_lock);
1030 * Queue cap releases when an inode is dropped from our cache. Since
1031 * inode is about to be destroyed, there is no need for i_lock.
1033 void ceph_queue_caps_release(struct inode *inode)
1035 struct ceph_inode_info *ci = ceph_inode(inode);
1038 p = rb_first(&ci->i_caps);
1040 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1041 struct ceph_mds_session *session = cap->session;
1043 __queue_cap_release(session, ceph_ino(inode), cap->cap_id,
1044 cap->mseq, cap->issue_seq);
1046 __ceph_remove_cap(cap);
1051 * Send a cap msg on the given inode. Update our caps state, then
1052 * drop i_lock and send the message.
1054 * Make note of max_size reported/requested from mds, revoked caps
1055 * that have now been implemented.
1057 * Make half-hearted attempt ot to invalidate page cache if we are
1058 * dropping RDCACHE. Note that this will leave behind locked pages
1059 * that we'll then need to deal with elsewhere.
1061 * Return non-zero if delayed release, or we experienced an error
1062 * such that the caller should requeue + retry later.
1064 * called with i_lock, then drops it.
1065 * caller should hold snap_rwsem (read), s_mutex.
1067 static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1068 int op, int used, int want, int retain, int flushing,
1069 unsigned *pflush_tid)
1070 __releases(cap->ci->vfs_inode->i_lock)
1072 struct ceph_inode_info *ci = cap->ci;
1073 struct inode *inode = &ci->vfs_inode;
1074 u64 cap_id = cap->cap_id;
1075 int held, revoking, dropping, keep;
1076 u64 seq, issue_seq, mseq, time_warp_seq, follows;
1078 struct timespec mtime, atime;
1083 struct ceph_mds_session *session;
1084 u64 xattr_version = 0;
1090 held = cap->issued | cap->implemented;
1091 revoking = cap->implemented & ~cap->issued;
1092 retain &= ~revoking;
1093 dropping = cap->issued & ~retain;
1095 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1096 inode, cap, cap->session,
1097 ceph_cap_string(held), ceph_cap_string(held & retain),
1098 ceph_cap_string(revoking));
1099 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1101 session = cap->session;
1103 /* don't release wanted unless we've waited a bit. */
1104 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1105 time_before(jiffies, ci->i_hold_caps_min)) {
1106 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1107 ceph_cap_string(cap->issued),
1108 ceph_cap_string(cap->issued & retain),
1109 ceph_cap_string(cap->mds_wanted),
1110 ceph_cap_string(want));
1111 want |= cap->mds_wanted;
1112 retain |= cap->issued;
1115 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1117 cap->issued &= retain; /* drop bits we don't want */
1118 if (cap->implemented & ~cap->issued) {
1120 * Wake up any waiters on wanted -> needed transition.
1121 * This is due to the weird transition from buffered
1122 * to sync IO... we need to flush dirty pages _before_
1123 * allowing sync writes to avoid reordering.
1127 cap->implemented &= cap->issued | used;
1128 cap->mds_wanted = want;
1132 * assign a tid for flush operations so we can avoid
1133 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1134 * clean type races. track latest tid for every bit
1135 * so we can handle flush AxFw, flush Fw, and have the
1136 * first ack clean Ax.
1138 flush_tid = ++ci->i_cap_flush_last_tid;
1140 *pflush_tid = flush_tid;
1141 dout(" cap_flush_tid %d\n", (int)flush_tid);
1142 for (i = 0; i < CEPH_CAP_BITS; i++)
1143 if (flushing & (1 << i))
1144 ci->i_cap_flush_tid[i] = flush_tid;
1147 keep = cap->implemented;
1149 issue_seq = cap->issue_seq;
1151 size = inode->i_size;
1152 ci->i_reported_size = size;
1153 max_size = ci->i_wanted_max_size;
1154 ci->i_requested_max_size = max_size;
1155 mtime = inode->i_mtime;
1156 atime = inode->i_atime;
1157 time_warp_seq = ci->i_time_warp_seq;
1158 follows = ci->i_snap_realm->cached_context->seq;
1161 mode = inode->i_mode;
1163 if (dropping & CEPH_CAP_XATTR_EXCL) {
1164 __ceph_build_xattrs_blob(ci);
1165 xattr_version = ci->i_xattrs.version + 1;
1168 spin_unlock(&inode->i_lock);
1170 ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id,
1171 op, keep, want, flushing, seq, flush_tid, issue_seq, mseq,
1172 size, max_size, &mtime, &atime, time_warp_seq,
1175 (flushing & CEPH_CAP_XATTR_EXCL) ? ci->i_xattrs.blob : NULL,
1178 dout("error sending cap msg, must requeue %p\n", inode);
1183 wake_up_all(&ci->i_cap_wq);
1189 * When a snapshot is taken, clients accumulate dirty metadata on
1190 * inodes with capabilities in ceph_cap_snaps to describe the file
1191 * state at the time the snapshot was taken. This must be flushed
1192 * asynchronously back to the MDS once sync writes complete and dirty
1193 * data is written out.
1195 * Called under i_lock. Takes s_mutex as needed.
1197 void __ceph_flush_snaps(struct ceph_inode_info *ci,
1198 struct ceph_mds_session **psession)
1200 struct inode *inode = &ci->vfs_inode;
1202 struct ceph_cap_snap *capsnap;
1204 struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc;
1205 struct ceph_mds_session *session = NULL; /* if session != NULL, we hold
1207 u64 next_follows = 0; /* keep track of how far we've gotten through the
1208 i_cap_snaps list, and skip these entries next time
1209 around to avoid an infinite loop */
1212 session = *psession;
1214 dout("__flush_snaps %p\n", inode);
1216 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1217 /* avoid an infiniute loop after retry */
1218 if (capsnap->follows < next_follows)
1221 * we need to wait for sync writes to complete and for dirty
1222 * pages to be written out.
1224 if (capsnap->dirty_pages || capsnap->writing)
1228 * if cap writeback already occurred, we should have dropped
1229 * the capsnap in ceph_put_wrbuffer_cap_refs.
1231 BUG_ON(capsnap->dirty == 0);
1233 /* pick mds, take s_mutex */
1234 mds = __ceph_get_cap_mds(ci, &mseq);
1235 if (session && session->s_mds != mds) {
1236 dout("oops, wrong session %p mutex\n", session);
1237 mutex_unlock(&session->s_mutex);
1238 ceph_put_mds_session(session);
1242 spin_unlock(&inode->i_lock);
1243 mutex_lock(&mdsc->mutex);
1244 session = __ceph_lookup_mds_session(mdsc, mds);
1245 mutex_unlock(&mdsc->mutex);
1247 dout("inverting session/ino locks on %p\n",
1249 mutex_lock(&session->s_mutex);
1252 * if session == NULL, we raced against a cap
1253 * deletion. retry, and we'll get a better
1254 * @mds value next time.
1256 spin_lock(&inode->i_lock);
1260 capsnap->flush_tid = ++ci->i_cap_flush_last_tid;
1261 atomic_inc(&capsnap->nref);
1262 if (!list_empty(&capsnap->flushing_item))
1263 list_del_init(&capsnap->flushing_item);
1264 list_add_tail(&capsnap->flushing_item,
1265 &session->s_cap_snaps_flushing);
1266 spin_unlock(&inode->i_lock);
1268 dout("flush_snaps %p cap_snap %p follows %lld size %llu\n",
1269 inode, capsnap, next_follows, capsnap->size);
1270 send_cap_msg(session, ceph_vino(inode).ino, 0,
1271 CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0,
1272 capsnap->dirty, 0, capsnap->flush_tid, 0, mseq,
1274 &capsnap->mtime, &capsnap->atime,
1275 capsnap->time_warp_seq,
1276 capsnap->uid, capsnap->gid, capsnap->mode,
1280 next_follows = capsnap->follows + 1;
1281 ceph_put_cap_snap(capsnap);
1283 spin_lock(&inode->i_lock);
1287 /* we flushed them all; remove this inode from the queue */
1288 spin_lock(&mdsc->snap_flush_lock);
1289 list_del_init(&ci->i_snap_flush_item);
1290 spin_unlock(&mdsc->snap_flush_lock);
1293 *psession = session;
1295 mutex_unlock(&session->s_mutex);
1296 ceph_put_mds_session(session);
1300 static void ceph_flush_snaps(struct ceph_inode_info *ci)
1302 struct inode *inode = &ci->vfs_inode;
1304 spin_lock(&inode->i_lock);
1305 __ceph_flush_snaps(ci, NULL);
1306 spin_unlock(&inode->i_lock);
1310 * Mark caps dirty. If inode is newly dirty, add to the global dirty
1313 void __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask)
1315 struct ceph_mds_client *mdsc =
1316 &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
1317 struct inode *inode = &ci->vfs_inode;
1318 int was = ci->i_dirty_caps;
1321 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1322 ceph_cap_string(mask), ceph_cap_string(was),
1323 ceph_cap_string(was | mask));
1324 ci->i_dirty_caps |= mask;
1326 dout(" inode %p now dirty\n", &ci->vfs_inode);
1327 BUG_ON(!list_empty(&ci->i_dirty_item));
1328 spin_lock(&mdsc->cap_dirty_lock);
1329 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
1330 spin_unlock(&mdsc->cap_dirty_lock);
1331 if (ci->i_flushing_caps == 0) {
1333 dirty |= I_DIRTY_SYNC;
1336 BUG_ON(list_empty(&ci->i_dirty_item));
1337 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1338 (mask & CEPH_CAP_FILE_BUFFER))
1339 dirty |= I_DIRTY_DATASYNC;
1341 __mark_inode_dirty(inode, dirty);
1342 __cap_delay_requeue(mdsc, ci);
1346 * Add dirty inode to the flushing list. Assigned a seq number so we
1347 * can wait for caps to flush without starving.
1349 * Called under i_lock.
1351 static int __mark_caps_flushing(struct inode *inode,
1352 struct ceph_mds_session *session)
1354 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
1355 struct ceph_inode_info *ci = ceph_inode(inode);
1358 BUG_ON(ci->i_dirty_caps == 0);
1359 BUG_ON(list_empty(&ci->i_dirty_item));
1361 flushing = ci->i_dirty_caps;
1362 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1363 ceph_cap_string(flushing),
1364 ceph_cap_string(ci->i_flushing_caps),
1365 ceph_cap_string(ci->i_flushing_caps | flushing));
1366 ci->i_flushing_caps |= flushing;
1367 ci->i_dirty_caps = 0;
1368 dout(" inode %p now !dirty\n", inode);
1370 spin_lock(&mdsc->cap_dirty_lock);
1371 list_del_init(&ci->i_dirty_item);
1373 ci->i_cap_flush_seq = ++mdsc->cap_flush_seq;
1374 if (list_empty(&ci->i_flushing_item)) {
1375 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1376 mdsc->num_cap_flushing++;
1377 dout(" inode %p now flushing seq %lld\n", inode,
1378 ci->i_cap_flush_seq);
1380 list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1381 dout(" inode %p now flushing (more) seq %lld\n", inode,
1382 ci->i_cap_flush_seq);
1384 spin_unlock(&mdsc->cap_dirty_lock);
1390 * try to invalidate mapping pages without blocking.
1392 static int mapping_is_empty(struct address_space *mapping)
1394 struct page *page = find_get_page(mapping, 0);
1403 static int try_nonblocking_invalidate(struct inode *inode)
1405 struct ceph_inode_info *ci = ceph_inode(inode);
1406 u32 invalidating_gen = ci->i_rdcache_gen;
1408 spin_unlock(&inode->i_lock);
1409 invalidate_mapping_pages(&inode->i_data, 0, -1);
1410 spin_lock(&inode->i_lock);
1412 if (mapping_is_empty(&inode->i_data) &&
1413 invalidating_gen == ci->i_rdcache_gen) {
1415 dout("try_nonblocking_invalidate %p success\n", inode);
1416 ci->i_rdcache_gen = 0;
1417 ci->i_rdcache_revoking = 0;
1420 dout("try_nonblocking_invalidate %p failed\n", inode);
1425 * Swiss army knife function to examine currently used and wanted
1426 * versus held caps. Release, flush, ack revoked caps to mds as
1429 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1430 * cap release further.
1431 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1432 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1435 void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1436 struct ceph_mds_session *session)
1437 __releases(session->s_mutex)
1439 struct ceph_client *client = ceph_inode_to_client(&ci->vfs_inode);
1440 struct ceph_mds_client *mdsc = &client->mdsc;
1441 struct inode *inode = &ci->vfs_inode;
1442 struct ceph_cap *cap;
1443 int file_wanted, used;
1444 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
1445 int issued, implemented, want, retain, revoking, flushing = 0;
1446 int mds = -1; /* keep track of how far we've gone through i_caps list
1447 to avoid an infinite loop on retry */
1449 int tried_invalidate = 0;
1450 int delayed = 0, sent = 0, force_requeue = 0, num;
1451 int queue_invalidate = 0;
1452 int is_delayed = flags & CHECK_CAPS_NODELAY;
1454 /* if we are unmounting, flush any unused caps immediately. */
1458 spin_lock(&inode->i_lock);
1460 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1461 flags |= CHECK_CAPS_FLUSH;
1463 /* flush snaps first time around only */
1464 if (!list_empty(&ci->i_cap_snaps))
1465 __ceph_flush_snaps(ci, &session);
1468 spin_lock(&inode->i_lock);
1470 file_wanted = __ceph_caps_file_wanted(ci);
1471 used = __ceph_caps_used(ci);
1472 want = file_wanted | used;
1473 issued = __ceph_caps_issued(ci, &implemented);
1474 revoking = implemented & ~issued;
1476 retain = want | CEPH_CAP_PIN;
1477 if (!mdsc->stopping && inode->i_nlink > 0) {
1479 retain |= CEPH_CAP_ANY; /* be greedy */
1481 retain |= CEPH_CAP_ANY_SHARED;
1483 * keep RD only if we didn't have the file open RW,
1484 * because then the mds would revoke it anyway to
1485 * journal max_size=0.
1487 if (ci->i_max_size == 0)
1488 retain |= CEPH_CAP_ANY_RD;
1492 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1493 " issued %s revoking %s retain %s %s%s%s\n", inode,
1494 ceph_cap_string(file_wanted),
1495 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1496 ceph_cap_string(ci->i_flushing_caps),
1497 ceph_cap_string(issued), ceph_cap_string(revoking),
1498 ceph_cap_string(retain),
1499 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1500 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1501 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1504 * If we no longer need to hold onto old our caps, and we may
1505 * have cached pages, but don't want them, then try to invalidate.
1506 * If we fail, it's because pages are locked.... try again later.
1508 if ((!is_delayed || mdsc->stopping) &&
1509 ci->i_wrbuffer_ref == 0 && /* no dirty pages... */
1510 ci->i_rdcache_gen && /* may have cached pages */
1511 (file_wanted == 0 || /* no open files */
1512 (revoking & CEPH_CAP_FILE_CACHE)) && /* or revoking cache */
1513 !tried_invalidate) {
1514 dout("check_caps trying to invalidate on %p\n", inode);
1515 if (try_nonblocking_invalidate(inode) < 0) {
1516 if (revoking & CEPH_CAP_FILE_CACHE) {
1517 dout("check_caps queuing invalidate\n");
1518 queue_invalidate = 1;
1519 ci->i_rdcache_revoking = ci->i_rdcache_gen;
1521 dout("check_caps failed to invalidate pages\n");
1522 /* we failed to invalidate pages. check these
1523 caps again later. */
1525 __cap_set_timeouts(mdsc, ci);
1528 tried_invalidate = 1;
1533 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1534 cap = rb_entry(p, struct ceph_cap, ci_node);
1537 /* avoid looping forever */
1538 if (mds >= cap->mds ||
1539 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1542 /* NOTE: no side-effects allowed, until we take s_mutex */
1544 revoking = cap->implemented & ~cap->issued;
1546 dout(" mds%d revoking %s\n", cap->mds,
1547 ceph_cap_string(revoking));
1549 if (cap == ci->i_auth_cap &&
1550 (cap->issued & CEPH_CAP_FILE_WR)) {
1551 /* request larger max_size from MDS? */
1552 if (ci->i_wanted_max_size > ci->i_max_size &&
1553 ci->i_wanted_max_size > ci->i_requested_max_size) {
1554 dout("requesting new max_size\n");
1558 /* approaching file_max? */
1559 if ((inode->i_size << 1) >= ci->i_max_size &&
1560 (ci->i_reported_size << 1) < ci->i_max_size) {
1561 dout("i_size approaching max_size\n");
1565 /* flush anything dirty? */
1566 if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) &&
1568 dout("flushing dirty caps\n");
1572 /* completed revocation? going down and there are no caps? */
1573 if (revoking && (revoking & used) == 0) {
1574 dout("completed revocation of %s\n",
1575 ceph_cap_string(cap->implemented & ~cap->issued));
1579 /* want more caps from mds? */
1580 if (want & ~(cap->mds_wanted | cap->issued))
1583 /* things we might delay */
1584 if ((cap->issued & ~retain) == 0 &&
1585 cap->mds_wanted == want)
1586 continue; /* nope, all good */
1592 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1593 time_before(jiffies, ci->i_hold_caps_max)) {
1594 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1595 ceph_cap_string(cap->issued),
1596 ceph_cap_string(cap->issued & retain),
1597 ceph_cap_string(cap->mds_wanted),
1598 ceph_cap_string(want));
1604 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1605 dout(" skipping %p I_NOFLUSH set\n", inode);
1609 if (session && session != cap->session) {
1610 dout("oops, wrong session %p mutex\n", session);
1611 mutex_unlock(&session->s_mutex);
1615 session = cap->session;
1616 if (mutex_trylock(&session->s_mutex) == 0) {
1617 dout("inverting session/ino locks on %p\n",
1619 spin_unlock(&inode->i_lock);
1620 if (took_snap_rwsem) {
1621 up_read(&mdsc->snap_rwsem);
1622 took_snap_rwsem = 0;
1624 mutex_lock(&session->s_mutex);
1628 /* take snap_rwsem after session mutex */
1629 if (!took_snap_rwsem) {
1630 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
1631 dout("inverting snap/in locks on %p\n",
1633 spin_unlock(&inode->i_lock);
1634 down_read(&mdsc->snap_rwsem);
1635 took_snap_rwsem = 1;
1638 took_snap_rwsem = 1;
1641 if (cap == ci->i_auth_cap && ci->i_dirty_caps)
1642 flushing = __mark_caps_flushing(inode, session);
1644 mds = cap->mds; /* remember mds, so we don't repeat */
1647 /* __send_cap drops i_lock */
1648 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, used, want,
1649 retain, flushing, NULL);
1650 goto retry; /* retake i_lock and restart our cap scan. */
1654 * Reschedule delayed caps release if we delayed anything,
1657 if (delayed && is_delayed)
1658 force_requeue = 1; /* __send_cap delayed release; requeue */
1659 if (!delayed && !is_delayed)
1660 __cap_delay_cancel(mdsc, ci);
1661 else if (!is_delayed || force_requeue)
1662 __cap_delay_requeue(mdsc, ci);
1664 spin_unlock(&inode->i_lock);
1666 if (queue_invalidate)
1667 ceph_queue_invalidate(inode);
1670 mutex_unlock(&session->s_mutex);
1671 if (took_snap_rwsem)
1672 up_read(&mdsc->snap_rwsem);
1676 * Try to flush dirty caps back to the auth mds.
1678 static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session,
1679 unsigned *flush_tid)
1681 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
1682 struct ceph_inode_info *ci = ceph_inode(inode);
1683 int unlock_session = session ? 0 : 1;
1687 spin_lock(&inode->i_lock);
1688 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1689 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
1692 if (ci->i_dirty_caps && ci->i_auth_cap) {
1693 struct ceph_cap *cap = ci->i_auth_cap;
1694 int used = __ceph_caps_used(ci);
1695 int want = __ceph_caps_wanted(ci);
1699 spin_unlock(&inode->i_lock);
1700 session = cap->session;
1701 mutex_lock(&session->s_mutex);
1704 BUG_ON(session != cap->session);
1705 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN)
1708 flushing = __mark_caps_flushing(inode, session);
1710 /* __send_cap drops i_lock */
1711 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want,
1712 cap->issued | cap->implemented, flushing,
1717 spin_lock(&inode->i_lock);
1718 __cap_delay_requeue(mdsc, ci);
1721 spin_unlock(&inode->i_lock);
1723 if (session && unlock_session)
1724 mutex_unlock(&session->s_mutex);
1729 * Return true if we've flushed caps through the given flush_tid.
1731 static int caps_are_flushed(struct inode *inode, unsigned tid)
1733 struct ceph_inode_info *ci = ceph_inode(inode);
1736 spin_lock(&inode->i_lock);
1737 for (i = 0; i < CEPH_CAP_BITS; i++)
1738 if ((ci->i_flushing_caps & (1 << i)) &&
1739 ci->i_cap_flush_tid[i] <= tid) {
1740 /* still flushing this bit */
1744 spin_unlock(&inode->i_lock);
1749 * Wait on any unsafe replies for the given inode. First wait on the
1750 * newest request, and make that the upper bound. Then, if there are
1751 * more requests, keep waiting on the oldest as long as it is still older
1752 * than the original request.
1754 static void sync_write_wait(struct inode *inode)
1756 struct ceph_inode_info *ci = ceph_inode(inode);
1757 struct list_head *head = &ci->i_unsafe_writes;
1758 struct ceph_osd_request *req;
1761 spin_lock(&ci->i_unsafe_lock);
1762 if (list_empty(head))
1765 /* set upper bound as _last_ entry in chain */
1766 req = list_entry(head->prev, struct ceph_osd_request,
1768 last_tid = req->r_tid;
1771 ceph_osdc_get_request(req);
1772 spin_unlock(&ci->i_unsafe_lock);
1773 dout("sync_write_wait on tid %llu (until %llu)\n",
1774 req->r_tid, last_tid);
1775 wait_for_completion(&req->r_safe_completion);
1776 spin_lock(&ci->i_unsafe_lock);
1777 ceph_osdc_put_request(req);
1780 * from here on look at first entry in chain, since we
1781 * only want to wait for anything older than last_tid
1783 if (list_empty(head))
1785 req = list_entry(head->next, struct ceph_osd_request,
1787 } while (req->r_tid < last_tid);
1789 spin_unlock(&ci->i_unsafe_lock);
1792 int ceph_fsync(struct file *file, int datasync)
1794 struct inode *inode = file->f_mapping->host;
1795 struct ceph_inode_info *ci = ceph_inode(inode);
1800 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
1801 sync_write_wait(inode);
1803 ret = filemap_write_and_wait(inode->i_mapping);
1807 dirty = try_flush_caps(inode, NULL, &flush_tid);
1808 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
1811 * only wait on non-file metadata writeback (the mds
1812 * can recover size and mtime, so we don't need to
1815 if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
1816 dout("fsync waiting for flush_tid %u\n", flush_tid);
1817 ret = wait_event_interruptible(ci->i_cap_wq,
1818 caps_are_flushed(inode, flush_tid));
1821 dout("fsync %p%s done\n", inode, datasync ? " datasync" : "");
1826 * Flush any dirty caps back to the mds. If we aren't asked to wait,
1827 * queue inode for flush but don't do so immediately, because we can
1828 * get by with fewer MDS messages if we wait for data writeback to
1831 int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
1833 struct ceph_inode_info *ci = ceph_inode(inode);
1837 int wait = wbc->sync_mode == WB_SYNC_ALL;
1839 dout("write_inode %p wait=%d\n", inode, wait);
1841 dirty = try_flush_caps(inode, NULL, &flush_tid);
1843 err = wait_event_interruptible(ci->i_cap_wq,
1844 caps_are_flushed(inode, flush_tid));
1846 struct ceph_mds_client *mdsc =
1847 &ceph_sb_to_client(inode->i_sb)->mdsc;
1849 spin_lock(&inode->i_lock);
1850 if (__ceph_caps_dirty(ci))
1851 __cap_delay_requeue_front(mdsc, ci);
1852 spin_unlock(&inode->i_lock);
1858 * After a recovering MDS goes active, we need to resend any caps
1861 * Caller holds session->s_mutex.
1863 static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc,
1864 struct ceph_mds_session *session)
1866 struct ceph_cap_snap *capsnap;
1868 dout("kick_flushing_capsnaps mds%d\n", session->s_mds);
1869 list_for_each_entry(capsnap, &session->s_cap_snaps_flushing,
1871 struct ceph_inode_info *ci = capsnap->ci;
1872 struct inode *inode = &ci->vfs_inode;
1873 struct ceph_cap *cap;
1875 spin_lock(&inode->i_lock);
1876 cap = ci->i_auth_cap;
1877 if (cap && cap->session == session) {
1878 dout("kick_flushing_caps %p cap %p capsnap %p\n", inode,
1880 __ceph_flush_snaps(ci, &session);
1882 pr_err("%p auth cap %p not mds%d ???\n", inode,
1883 cap, session->s_mds);
1885 spin_unlock(&inode->i_lock);
1889 void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
1890 struct ceph_mds_session *session)
1892 struct ceph_inode_info *ci;
1894 kick_flushing_capsnaps(mdsc, session);
1896 dout("kick_flushing_caps mds%d\n", session->s_mds);
1897 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
1898 struct inode *inode = &ci->vfs_inode;
1899 struct ceph_cap *cap;
1902 spin_lock(&inode->i_lock);
1903 cap = ci->i_auth_cap;
1904 if (cap && cap->session == session) {
1905 dout("kick_flushing_caps %p cap %p %s\n", inode,
1906 cap, ceph_cap_string(ci->i_flushing_caps));
1907 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
1908 __ceph_caps_used(ci),
1909 __ceph_caps_wanted(ci),
1910 cap->issued | cap->implemented,
1911 ci->i_flushing_caps, NULL);
1913 spin_lock(&inode->i_lock);
1914 __cap_delay_requeue(mdsc, ci);
1915 spin_unlock(&inode->i_lock);
1918 pr_err("%p auth cap %p not mds%d ???\n", inode,
1919 cap, session->s_mds);
1920 spin_unlock(&inode->i_lock);
1927 * Take references to capabilities we hold, so that we don't release
1928 * them to the MDS prematurely.
1930 * Protected by i_lock.
1932 static void __take_cap_refs(struct ceph_inode_info *ci, int got)
1934 if (got & CEPH_CAP_PIN)
1936 if (got & CEPH_CAP_FILE_RD)
1938 if (got & CEPH_CAP_FILE_CACHE)
1939 ci->i_rdcache_ref++;
1940 if (got & CEPH_CAP_FILE_WR)
1942 if (got & CEPH_CAP_FILE_BUFFER) {
1943 if (ci->i_wrbuffer_ref == 0)
1944 igrab(&ci->vfs_inode);
1945 ci->i_wrbuffer_ref++;
1946 dout("__take_cap_refs %p wrbuffer %d -> %d (?)\n",
1947 &ci->vfs_inode, ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref);
1952 * Try to grab cap references. Specify those refs we @want, and the
1953 * minimal set we @need. Also include the larger offset we are writing
1954 * to (when applicable), and check against max_size here as well.
1955 * Note that caller is responsible for ensuring max_size increases are
1956 * requested from the MDS.
1958 static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
1959 int *got, loff_t endoff, int *check_max, int *err)
1961 struct inode *inode = &ci->vfs_inode;
1963 int have, implemented;
1966 dout("get_cap_refs %p need %s want %s\n", inode,
1967 ceph_cap_string(need), ceph_cap_string(want));
1968 spin_lock(&inode->i_lock);
1970 /* make sure file is actually open */
1971 file_wanted = __ceph_caps_file_wanted(ci);
1972 if ((file_wanted & need) == 0) {
1973 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
1974 ceph_cap_string(need), ceph_cap_string(file_wanted));
1980 if (need & CEPH_CAP_FILE_WR) {
1981 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
1982 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
1983 inode, endoff, ci->i_max_size);
1984 if (endoff > ci->i_wanted_max_size) {
1991 * If a sync write is in progress, we must wait, so that we
1992 * can get a final snapshot value for size+mtime.
1994 if (__ceph_have_pending_cap_snap(ci)) {
1995 dout("get_cap_refs %p cap_snap_pending\n", inode);
1999 have = __ceph_caps_issued(ci, &implemented);
2002 * disallow writes while a truncate is pending
2004 if (ci->i_truncate_pending)
2005 have &= ~CEPH_CAP_FILE_WR;
2007 if ((have & need) == need) {
2009 * Look at (implemented & ~have & not) so that we keep waiting
2010 * on transition from wanted -> needed caps. This is needed
2011 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2012 * going before a prior buffered writeback happens.
2014 int not = want & ~(have & need);
2015 int revoking = implemented & ~have;
2016 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2017 inode, ceph_cap_string(have), ceph_cap_string(not),
2018 ceph_cap_string(revoking));
2019 if ((revoking & not) == 0) {
2020 *got = need | (have & want);
2021 __take_cap_refs(ci, *got);
2025 dout("get_cap_refs %p have %s needed %s\n", inode,
2026 ceph_cap_string(have), ceph_cap_string(need));
2029 spin_unlock(&inode->i_lock);
2030 dout("get_cap_refs %p ret %d got %s\n", inode,
2031 ret, ceph_cap_string(*got));
2036 * Check the offset we are writing up to against our current
2037 * max_size. If necessary, tell the MDS we want to write to
2040 static void check_max_size(struct inode *inode, loff_t endoff)
2042 struct ceph_inode_info *ci = ceph_inode(inode);
2045 /* do we need to explicitly request a larger max_size? */
2046 spin_lock(&inode->i_lock);
2047 if ((endoff >= ci->i_max_size ||
2048 endoff > (inode->i_size << 1)) &&
2049 endoff > ci->i_wanted_max_size) {
2050 dout("write %p at large endoff %llu, req max_size\n",
2052 ci->i_wanted_max_size = endoff;
2055 spin_unlock(&inode->i_lock);
2057 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2061 * Wait for caps, and take cap references. If we can't get a WR cap
2062 * due to a small max_size, make sure we check_max_size (and possibly
2063 * ask the mds) so we don't get hung up indefinitely.
2065 int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got,
2068 int check_max, ret, err;
2072 check_max_size(&ci->vfs_inode, endoff);
2075 ret = wait_event_interruptible(ci->i_cap_wq,
2076 try_get_cap_refs(ci, need, want,
2087 * Take cap refs. Caller must already know we hold at least one ref
2088 * on the caps in question or we don't know this is safe.
2090 void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2092 spin_lock(&ci->vfs_inode.i_lock);
2093 __take_cap_refs(ci, caps);
2094 spin_unlock(&ci->vfs_inode.i_lock);
2100 * If we released the last ref on any given cap, call ceph_check_caps
2101 * to release (or schedule a release).
2103 * If we are releasing a WR cap (from a sync write), finalize any affected
2104 * cap_snap, and wake up any waiters.
2106 void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2108 struct inode *inode = &ci->vfs_inode;
2109 int last = 0, put = 0, flushsnaps = 0, wake = 0;
2110 struct ceph_cap_snap *capsnap;
2112 spin_lock(&inode->i_lock);
2113 if (had & CEPH_CAP_PIN)
2115 if (had & CEPH_CAP_FILE_RD)
2116 if (--ci->i_rd_ref == 0)
2118 if (had & CEPH_CAP_FILE_CACHE)
2119 if (--ci->i_rdcache_ref == 0)
2121 if (had & CEPH_CAP_FILE_BUFFER) {
2122 if (--ci->i_wrbuffer_ref == 0) {
2126 dout("put_cap_refs %p wrbuffer %d -> %d (?)\n",
2127 inode, ci->i_wrbuffer_ref+1, ci->i_wrbuffer_ref);
2129 if (had & CEPH_CAP_FILE_WR)
2130 if (--ci->i_wr_ref == 0) {
2132 if (!list_empty(&ci->i_cap_snaps)) {
2133 capsnap = list_first_entry(&ci->i_cap_snaps,
2134 struct ceph_cap_snap,
2136 if (capsnap->writing) {
2137 capsnap->writing = 0;
2139 __ceph_finish_cap_snap(ci,
2145 spin_unlock(&inode->i_lock);
2147 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2148 last ? " last" : "", put ? " put" : "");
2150 if (last && !flushsnaps)
2151 ceph_check_caps(ci, 0, NULL);
2152 else if (flushsnaps)
2153 ceph_flush_snaps(ci);
2155 wake_up_all(&ci->i_cap_wq);
2161 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2162 * context. Adjust per-snap dirty page accounting as appropriate.
2163 * Once all dirty data for a cap_snap is flushed, flush snapped file
2164 * metadata back to the MDS. If we dropped the last ref, call
2167 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2168 struct ceph_snap_context *snapc)
2170 struct inode *inode = &ci->vfs_inode;
2172 int complete_capsnap = 0;
2173 int drop_capsnap = 0;
2175 struct ceph_cap_snap *capsnap = NULL;
2177 spin_lock(&inode->i_lock);
2178 ci->i_wrbuffer_ref -= nr;
2179 last = !ci->i_wrbuffer_ref;
2181 if (ci->i_head_snapc == snapc) {
2182 ci->i_wrbuffer_ref_head -= nr;
2183 if (!ci->i_wrbuffer_ref_head) {
2184 ceph_put_snap_context(ci->i_head_snapc);
2185 ci->i_head_snapc = NULL;
2187 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2189 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2190 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2191 last ? " LAST" : "");
2193 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2194 if (capsnap->context == snapc) {
2200 capsnap->dirty_pages -= nr;
2201 if (capsnap->dirty_pages == 0) {
2202 complete_capsnap = 1;
2203 if (capsnap->dirty == 0)
2204 /* cap writeback completed before we created
2205 * the cap_snap; no FLUSHSNAP is needed */
2208 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2209 " snap %lld %d/%d -> %d/%d %s%s%s\n",
2210 inode, capsnap, capsnap->context->seq,
2211 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2212 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2213 last ? " (wrbuffer last)" : "",
2214 complete_capsnap ? " (complete capsnap)" : "",
2215 drop_capsnap ? " (drop capsnap)" : "");
2217 ceph_put_snap_context(capsnap->context);
2218 list_del(&capsnap->ci_item);
2219 list_del(&capsnap->flushing_item);
2220 ceph_put_cap_snap(capsnap);
2224 spin_unlock(&inode->i_lock);
2227 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2229 } else if (complete_capsnap) {
2230 ceph_flush_snaps(ci);
2231 wake_up_all(&ci->i_cap_wq);
2238 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2239 * actually be a revocation if it specifies a smaller cap set.)
2241 * caller holds s_mutex and i_lock, we drop both.
2245 * 1 - check_caps on auth cap only (writeback)
2246 * 2 - check_caps (ack revoke)
2248 static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant,
2249 struct ceph_mds_session *session,
2250 struct ceph_cap *cap,
2251 struct ceph_buffer *xattr_buf)
2252 __releases(inode->i_lock)
2253 __releases(session->s_mutex)
2255 struct ceph_inode_info *ci = ceph_inode(inode);
2256 int mds = session->s_mds;
2257 int seq = le32_to_cpu(grant->seq);
2258 int newcaps = le32_to_cpu(grant->caps);
2259 int issued, implemented, used, wanted, dirty;
2260 u64 size = le64_to_cpu(grant->size);
2261 u64 max_size = le64_to_cpu(grant->max_size);
2262 struct timespec mtime, atime, ctime;
2266 int revoked_rdcache = 0;
2267 int queue_invalidate = 0;
2269 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2270 inode, cap, mds, seq, ceph_cap_string(newcaps));
2271 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
2275 * If CACHE is being revoked, and we have no dirty buffers,
2276 * try to invalidate (once). (If there are dirty buffers, we
2277 * will invalidate _after_ writeback.)
2279 if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
2280 !ci->i_wrbuffer_ref) {
2281 if (try_nonblocking_invalidate(inode) == 0) {
2282 revoked_rdcache = 1;
2284 /* there were locked pages.. invalidate later
2285 in a separate thread. */
2286 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
2287 queue_invalidate = 1;
2288 ci->i_rdcache_revoking = ci->i_rdcache_gen;
2293 /* side effects now are allowed */
2295 issued = __ceph_caps_issued(ci, &implemented);
2296 issued |= implemented | __ceph_caps_dirty(ci);
2298 cap->cap_gen = session->s_cap_gen;
2300 __check_cap_issue(ci, cap, newcaps);
2302 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
2303 inode->i_mode = le32_to_cpu(grant->mode);
2304 inode->i_uid = le32_to_cpu(grant->uid);
2305 inode->i_gid = le32_to_cpu(grant->gid);
2306 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
2307 inode->i_uid, inode->i_gid);
2310 if ((issued & CEPH_CAP_LINK_EXCL) == 0)
2311 inode->i_nlink = le32_to_cpu(grant->nlink);
2313 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
2314 int len = le32_to_cpu(grant->xattr_len);
2315 u64 version = le64_to_cpu(grant->xattr_version);
2317 if (version > ci->i_xattrs.version) {
2318 dout(" got new xattrs v%llu on %p len %d\n",
2319 version, inode, len);
2320 if (ci->i_xattrs.blob)
2321 ceph_buffer_put(ci->i_xattrs.blob);
2322 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
2323 ci->i_xattrs.version = version;
2327 /* size/ctime/mtime/atime? */
2328 ceph_fill_file_size(inode, issued,
2329 le32_to_cpu(grant->truncate_seq),
2330 le64_to_cpu(grant->truncate_size), size);
2331 ceph_decode_timespec(&mtime, &grant->mtime);
2332 ceph_decode_timespec(&atime, &grant->atime);
2333 ceph_decode_timespec(&ctime, &grant->ctime);
2334 ceph_fill_file_time(inode, issued,
2335 le32_to_cpu(grant->time_warp_seq), &ctime, &mtime,
2338 /* max size increase? */
2339 if (max_size != ci->i_max_size) {
2340 dout("max_size %lld -> %llu\n", ci->i_max_size, max_size);
2341 ci->i_max_size = max_size;
2342 if (max_size >= ci->i_wanted_max_size) {
2343 ci->i_wanted_max_size = 0; /* reset */
2344 ci->i_requested_max_size = 0;
2349 /* check cap bits */
2350 wanted = __ceph_caps_wanted(ci);
2351 used = __ceph_caps_used(ci);
2352 dirty = __ceph_caps_dirty(ci);
2353 dout(" my wanted = %s, used = %s, dirty %s\n",
2354 ceph_cap_string(wanted),
2355 ceph_cap_string(used),
2356 ceph_cap_string(dirty));
2357 if (wanted != le32_to_cpu(grant->wanted)) {
2358 dout("mds wanted %s -> %s\n",
2359 ceph_cap_string(le32_to_cpu(grant->wanted)),
2360 ceph_cap_string(wanted));
2361 grant->wanted = cpu_to_le32(wanted);
2366 /* file layout may have changed */
2367 ci->i_layout = grant->layout;
2369 /* revocation, grant, or no-op? */
2370 if (cap->issued & ~newcaps) {
2371 dout("revocation: %s -> %s\n", ceph_cap_string(cap->issued),
2372 ceph_cap_string(newcaps));
2373 if ((used & ~newcaps) & CEPH_CAP_FILE_BUFFER)
2374 writeback = 1; /* will delay ack */
2375 else if (dirty & ~newcaps)
2376 check_caps = 1; /* initiate writeback in check_caps */
2377 else if (((used & ~newcaps) & CEPH_CAP_FILE_CACHE) == 0 ||
2379 check_caps = 2; /* send revoke ack in check_caps */
2380 cap->issued = newcaps;
2381 cap->implemented |= newcaps;
2382 } else if (cap->issued == newcaps) {
2383 dout("caps unchanged: %s -> %s\n",
2384 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
2386 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
2387 ceph_cap_string(newcaps));
2388 cap->issued = newcaps;
2389 cap->implemented |= newcaps; /* add bits only, to
2390 * avoid stepping on a
2391 * pending revocation */
2394 BUG_ON(cap->issued & ~cap->implemented);
2396 spin_unlock(&inode->i_lock);
2399 * queue inode for writeback: we can't actually call
2400 * filemap_write_and_wait, etc. from message handler
2403 ceph_queue_writeback(inode);
2404 if (queue_invalidate)
2405 ceph_queue_invalidate(inode);
2407 wake_up_all(&ci->i_cap_wq);
2409 if (check_caps == 1)
2410 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
2412 else if (check_caps == 2)
2413 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
2415 mutex_unlock(&session->s_mutex);
2419 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2420 * MDS has been safely committed.
2422 static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
2423 struct ceph_mds_caps *m,
2424 struct ceph_mds_session *session,
2425 struct ceph_cap *cap)
2426 __releases(inode->i_lock)
2428 struct ceph_inode_info *ci = ceph_inode(inode);
2429 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
2430 unsigned seq = le32_to_cpu(m->seq);
2431 int dirty = le32_to_cpu(m->dirty);
2436 for (i = 0; i < CEPH_CAP_BITS; i++)
2437 if ((dirty & (1 << i)) &&
2438 flush_tid == ci->i_cap_flush_tid[i])
2441 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2442 " flushing %s -> %s\n",
2443 inode, session->s_mds, seq, ceph_cap_string(dirty),
2444 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
2445 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
2447 if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned))
2450 ci->i_flushing_caps &= ~cleaned;
2452 spin_lock(&mdsc->cap_dirty_lock);
2453 if (ci->i_flushing_caps == 0) {
2454 list_del_init(&ci->i_flushing_item);
2455 if (!list_empty(&session->s_cap_flushing))
2456 dout(" mds%d still flushing cap on %p\n",
2458 &list_entry(session->s_cap_flushing.next,
2459 struct ceph_inode_info,
2460 i_flushing_item)->vfs_inode);
2461 mdsc->num_cap_flushing--;
2462 wake_up_all(&mdsc->cap_flushing_wq);
2463 dout(" inode %p now !flushing\n", inode);
2465 if (ci->i_dirty_caps == 0) {
2466 dout(" inode %p now clean\n", inode);
2467 BUG_ON(!list_empty(&ci->i_dirty_item));
2470 BUG_ON(list_empty(&ci->i_dirty_item));
2473 spin_unlock(&mdsc->cap_dirty_lock);
2474 wake_up_all(&ci->i_cap_wq);
2477 spin_unlock(&inode->i_lock);
2483 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
2484 * throw away our cap_snap.
2486 * Caller hold s_mutex.
2488 static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
2489 struct ceph_mds_caps *m,
2490 struct ceph_mds_session *session)
2492 struct ceph_inode_info *ci = ceph_inode(inode);
2493 u64 follows = le64_to_cpu(m->snap_follows);
2494 struct ceph_cap_snap *capsnap;
2497 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2498 inode, ci, session->s_mds, follows);
2500 spin_lock(&inode->i_lock);
2501 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2502 if (capsnap->follows == follows) {
2503 if (capsnap->flush_tid != flush_tid) {
2504 dout(" cap_snap %p follows %lld tid %lld !="
2505 " %lld\n", capsnap, follows,
2506 flush_tid, capsnap->flush_tid);
2509 WARN_ON(capsnap->dirty_pages || capsnap->writing);
2510 dout(" removing %p cap_snap %p follows %lld\n",
2511 inode, capsnap, follows);
2512 ceph_put_snap_context(capsnap->context);
2513 list_del(&capsnap->ci_item);
2514 list_del(&capsnap->flushing_item);
2515 ceph_put_cap_snap(capsnap);
2519 dout(" skipping cap_snap %p follows %lld\n",
2520 capsnap, capsnap->follows);
2523 spin_unlock(&inode->i_lock);
2529 * Handle TRUNC from MDS, indicating file truncation.
2531 * caller hold s_mutex.
2533 static void handle_cap_trunc(struct inode *inode,
2534 struct ceph_mds_caps *trunc,
2535 struct ceph_mds_session *session)
2536 __releases(inode->i_lock)
2538 struct ceph_inode_info *ci = ceph_inode(inode);
2539 int mds = session->s_mds;
2540 int seq = le32_to_cpu(trunc->seq);
2541 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
2542 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
2543 u64 size = le64_to_cpu(trunc->size);
2544 int implemented = 0;
2545 int dirty = __ceph_caps_dirty(ci);
2546 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
2547 int queue_trunc = 0;
2549 issued |= implemented | dirty;
2551 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2552 inode, mds, seq, truncate_size, truncate_seq);
2553 queue_trunc = ceph_fill_file_size(inode, issued,
2554 truncate_seq, truncate_size, size);
2555 spin_unlock(&inode->i_lock);
2558 ceph_queue_vmtruncate(inode);
2562 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
2563 * different one. If we are the most recent migration we've seen (as
2564 * indicated by mseq), make note of the migrating cap bits for the
2565 * duration (until we see the corresponding IMPORT).
2567 * caller holds s_mutex
2569 static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
2570 struct ceph_mds_session *session)
2572 struct ceph_inode_info *ci = ceph_inode(inode);
2573 int mds = session->s_mds;
2574 unsigned mseq = le32_to_cpu(ex->migrate_seq);
2575 struct ceph_cap *cap = NULL, *t;
2579 dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
2580 inode, ci, mds, mseq);
2582 spin_lock(&inode->i_lock);
2584 /* make sure we haven't seen a higher mseq */
2585 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
2586 t = rb_entry(p, struct ceph_cap, ci_node);
2587 if (ceph_seq_cmp(t->mseq, mseq) > 0) {
2588 dout(" higher mseq on cap from mds%d\n",
2592 if (t->session->s_mds == mds)
2599 ci->i_cap_exporting_mds = mds;
2600 ci->i_cap_exporting_mseq = mseq;
2601 ci->i_cap_exporting_issued = cap->issued;
2603 __ceph_remove_cap(cap);
2605 /* else, we already released it */
2607 spin_unlock(&inode->i_lock);
2611 * Handle cap IMPORT. If there are temp bits from an older EXPORT,
2614 * caller holds s_mutex.
2616 static void handle_cap_import(struct ceph_mds_client *mdsc,
2617 struct inode *inode, struct ceph_mds_caps *im,
2618 struct ceph_mds_session *session,
2619 void *snaptrace, int snaptrace_len)
2621 struct ceph_inode_info *ci = ceph_inode(inode);
2622 int mds = session->s_mds;
2623 unsigned issued = le32_to_cpu(im->caps);
2624 unsigned wanted = le32_to_cpu(im->wanted);
2625 unsigned seq = le32_to_cpu(im->seq);
2626 unsigned mseq = le32_to_cpu(im->migrate_seq);
2627 u64 realmino = le64_to_cpu(im->realm);
2628 u64 cap_id = le64_to_cpu(im->cap_id);
2630 if (ci->i_cap_exporting_mds >= 0 &&
2631 ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) {
2632 dout("handle_cap_import inode %p ci %p mds%d mseq %d"
2633 " - cleared exporting from mds%d\n",
2634 inode, ci, mds, mseq,
2635 ci->i_cap_exporting_mds);
2636 ci->i_cap_exporting_issued = 0;
2637 ci->i_cap_exporting_mseq = 0;
2638 ci->i_cap_exporting_mds = -1;
2640 dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
2641 inode, ci, mds, mseq);
2644 down_write(&mdsc->snap_rwsem);
2645 ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len,
2647 downgrade_write(&mdsc->snap_rwsem);
2648 ceph_add_cap(inode, session, cap_id, -1,
2649 issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH,
2650 NULL /* no caps context */);
2651 try_flush_caps(inode, session, NULL);
2652 up_read(&mdsc->snap_rwsem);
2656 * Handle a caps message from the MDS.
2658 * Identify the appropriate session, inode, and call the right handler
2659 * based on the cap op.
2661 void ceph_handle_caps(struct ceph_mds_session *session,
2662 struct ceph_msg *msg)
2664 struct ceph_mds_client *mdsc = session->s_mdsc;
2665 struct super_block *sb = mdsc->client->sb;
2666 struct inode *inode;
2667 struct ceph_cap *cap;
2668 struct ceph_mds_caps *h;
2669 int mds = session->s_mds;
2672 struct ceph_vino vino;
2678 dout("handle_caps from mds%d\n", mds);
2681 tid = le64_to_cpu(msg->hdr.tid);
2682 if (msg->front.iov_len < sizeof(*h))
2684 h = msg->front.iov_base;
2686 op = le32_to_cpu(h->op);
2687 vino.ino = le64_to_cpu(h->ino);
2688 vino.snap = CEPH_NOSNAP;
2689 cap_id = le64_to_cpu(h->cap_id);
2690 seq = le32_to_cpu(h->seq);
2691 mseq = le32_to_cpu(h->migrate_seq);
2692 size = le64_to_cpu(h->size);
2693 max_size = le64_to_cpu(h->max_size);
2695 mutex_lock(&session->s_mutex);
2697 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
2701 inode = ceph_find_inode(sb, vino);
2702 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
2705 dout(" i don't have ino %llx\n", vino.ino);
2707 if (op == CEPH_CAP_OP_IMPORT)
2708 __queue_cap_release(session, vino.ino, cap_id,
2712 * send any full release message to try to move things
2713 * along for the mds (who clearly thinks we still have this
2716 ceph_add_cap_releases(mdsc, session, -1);
2717 ceph_send_cap_releases(mdsc, session);
2721 /* these will work even if we don't have a cap yet */
2723 case CEPH_CAP_OP_FLUSHSNAP_ACK:
2724 handle_cap_flushsnap_ack(inode, tid, h, session);
2727 case CEPH_CAP_OP_EXPORT:
2728 handle_cap_export(inode, h, session);
2731 case CEPH_CAP_OP_IMPORT:
2732 handle_cap_import(mdsc, inode, h, session,
2733 snaptrace, le32_to_cpu(h->snap_trace_len));
2734 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_NODELAY,
2739 /* the rest require a cap */
2740 spin_lock(&inode->i_lock);
2741 cap = __get_cap_for_mds(ceph_inode(inode), mds);
2743 dout(" no cap on %p ino %llx.%llx from mds%d\n",
2744 inode, ceph_ino(inode), ceph_snap(inode), mds);
2745 spin_unlock(&inode->i_lock);
2749 /* note that each of these drops i_lock for us */
2751 case CEPH_CAP_OP_REVOKE:
2752 case CEPH_CAP_OP_GRANT:
2753 handle_cap_grant(inode, h, session, cap, msg->middle);
2756 case CEPH_CAP_OP_FLUSH_ACK:
2757 handle_cap_flush_ack(inode, tid, h, session, cap);
2760 case CEPH_CAP_OP_TRUNC:
2761 handle_cap_trunc(inode, h, session);
2765 spin_unlock(&inode->i_lock);
2766 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
2767 ceph_cap_op_name(op));
2771 mutex_unlock(&session->s_mutex);
2778 pr_err("ceph_handle_caps: corrupt message\n");
2784 * Delayed work handler to process end of delayed cap release LRU list.
2786 void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
2788 struct ceph_inode_info *ci;
2789 int flags = CHECK_CAPS_NODELAY;
2791 dout("check_delayed_caps\n");
2793 spin_lock(&mdsc->cap_delay_lock);
2794 if (list_empty(&mdsc->cap_delay_list))
2796 ci = list_first_entry(&mdsc->cap_delay_list,
2797 struct ceph_inode_info,
2799 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
2800 time_before(jiffies, ci->i_hold_caps_max))
2802 list_del_init(&ci->i_cap_delay_list);
2803 spin_unlock(&mdsc->cap_delay_lock);
2804 dout("check_delayed_caps on %p\n", &ci->vfs_inode);
2805 ceph_check_caps(ci, flags, NULL);
2807 spin_unlock(&mdsc->cap_delay_lock);
2811 * Flush all dirty caps to the mds
2813 void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
2815 struct ceph_inode_info *ci, *nci = NULL;
2816 struct inode *inode, *ninode = NULL;
2817 struct list_head *p, *n;
2819 dout("flush_dirty_caps\n");
2820 spin_lock(&mdsc->cap_dirty_lock);
2821 list_for_each_safe(p, n, &mdsc->cap_dirty) {
2825 ci->i_ceph_flags &= ~CEPH_I_NOFLUSH;
2826 dout("flush_dirty_caps inode %p (was next inode)\n",
2829 ci = list_entry(p, struct ceph_inode_info,
2831 inode = igrab(&ci->vfs_inode);
2833 dout("flush_dirty_caps inode %p\n", inode);
2835 if (n != &mdsc->cap_dirty) {
2836 nci = list_entry(n, struct ceph_inode_info,
2838 ninode = igrab(&nci->vfs_inode);
2840 nci->i_ceph_flags |= CEPH_I_NOFLUSH;
2841 dout("flush_dirty_caps next inode %p, noflush\n",
2847 spin_unlock(&mdsc->cap_dirty_lock);
2849 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH,
2853 spin_lock(&mdsc->cap_dirty_lock);
2855 spin_unlock(&mdsc->cap_dirty_lock);
2859 * Drop open file reference. If we were the last open file,
2860 * we may need to release capabilities to the MDS (or schedule
2861 * their delayed release).
2863 void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
2865 struct inode *inode = &ci->vfs_inode;
2868 spin_lock(&inode->i_lock);
2869 dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode,
2870 ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1);
2871 BUG_ON(ci->i_nr_by_mode[fmode] == 0);
2872 if (--ci->i_nr_by_mode[fmode] == 0)
2874 spin_unlock(&inode->i_lock);
2876 if (last && ci->i_vino.snap == CEPH_NOSNAP)
2877 ceph_check_caps(ci, 0, NULL);
2881 * Helpers for embedding cap and dentry lease releases into mds
2884 * @force is used by dentry_release (below) to force inclusion of a
2885 * record for the directory inode, even when there aren't any caps to
2888 int ceph_encode_inode_release(void **p, struct inode *inode,
2889 int mds, int drop, int unless, int force)
2891 struct ceph_inode_info *ci = ceph_inode(inode);
2892 struct ceph_cap *cap;
2893 struct ceph_mds_request_release *rel = *p;
2897 spin_lock(&inode->i_lock);
2898 used = __ceph_caps_used(ci);
2899 dirty = __ceph_caps_dirty(ci);
2901 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
2902 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
2903 ceph_cap_string(unless));
2905 /* only drop unused, clean caps */
2906 drop &= ~(used | dirty);
2908 cap = __get_cap_for_mds(ci, mds);
2909 if (cap && __cap_is_valid(cap)) {
2911 ((cap->issued & drop) &&
2912 (cap->issued & unless) == 0)) {
2913 if ((cap->issued & drop) &&
2914 (cap->issued & unless) == 0) {
2915 dout("encode_inode_release %p cap %p %s -> "
2917 ceph_cap_string(cap->issued),
2918 ceph_cap_string(cap->issued & ~drop));
2919 cap->issued &= ~drop;
2920 cap->implemented &= ~drop;
2921 if (ci->i_ceph_flags & CEPH_I_NODELAY) {
2922 int wanted = __ceph_caps_wanted(ci);
2923 dout(" wanted %s -> %s (act %s)\n",
2924 ceph_cap_string(cap->mds_wanted),
2925 ceph_cap_string(cap->mds_wanted &
2927 ceph_cap_string(wanted));
2928 cap->mds_wanted &= wanted;
2931 dout("encode_inode_release %p cap %p %s"
2932 " (force)\n", inode, cap,
2933 ceph_cap_string(cap->issued));
2936 rel->ino = cpu_to_le64(ceph_ino(inode));
2937 rel->cap_id = cpu_to_le64(cap->cap_id);
2938 rel->seq = cpu_to_le32(cap->seq);
2939 rel->issue_seq = cpu_to_le32(cap->issue_seq),
2940 rel->mseq = cpu_to_le32(cap->mseq);
2941 rel->caps = cpu_to_le32(cap->issued);
2942 rel->wanted = cpu_to_le32(cap->mds_wanted);
2948 dout("encode_inode_release %p cap %p %s\n",
2949 inode, cap, ceph_cap_string(cap->issued));
2952 spin_unlock(&inode->i_lock);
2956 int ceph_encode_dentry_release(void **p, struct dentry *dentry,
2957 int mds, int drop, int unless)
2959 struct inode *dir = dentry->d_parent->d_inode;
2960 struct ceph_mds_request_release *rel = *p;
2961 struct ceph_dentry_info *di = ceph_dentry(dentry);
2966 * force an record for the directory caps if we have a dentry lease.
2967 * this is racy (can't take i_lock and d_lock together), but it
2968 * doesn't have to be perfect; the mds will revoke anything we don't
2971 spin_lock(&dentry->d_lock);
2972 if (di->lease_session && di->lease_session->s_mds == mds)
2974 spin_unlock(&dentry->d_lock);
2976 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
2978 spin_lock(&dentry->d_lock);
2979 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
2980 dout("encode_dentry_release %p mds%d seq %d\n",
2981 dentry, mds, (int)di->lease_seq);
2982 rel->dname_len = cpu_to_le32(dentry->d_name.len);
2983 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
2984 *p += dentry->d_name.len;
2985 rel->dname_seq = cpu_to_le32(di->lease_seq);
2986 __ceph_mdsc_drop_dentry_lease(dentry);
2988 spin_unlock(&dentry->d_lock);