1 #include "ceph_debug.h"
4 #include <linux/kernel.h>
5 #include <linux/sched.h>
6 #include <linux/vmalloc.h>
7 #include <linux/wait.h>
8 #include <linux/writeback.h>
12 #include "messenger.h"
15 * Capability management
17 * The Ceph metadata servers control client access to inode metadata
18 * and file data by issuing capabilities, granting clients permission
19 * to read and/or write both inode field and file data to OSDs
20 * (storage nodes). Each capability consists of a set of bits
21 * indicating which operations are allowed.
23 * If the client holds a *_SHARED cap, the client has a coherent value
24 * that can be safely read from the cached inode.
26 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
27 * client is allowed to change inode attributes (e.g., file size,
28 * mtime), note its dirty state in the ceph_cap, and asynchronously
29 * flush that metadata change to the MDS.
31 * In the event of a conflicting operation (perhaps by another
32 * client), the MDS will revoke the conflicting client capabilities.
34 * In order for a client to cache an inode, it must hold a capability
35 * with at least one MDS server. When inodes are released, release
36 * notifications are batched and periodically sent en masse to the MDS
37 * cluster to release server state.
42 * Generate readable cap strings for debugging output.
44 #define MAX_CAP_STR 20
45 static char cap_str[MAX_CAP_STR][40];
46 static DEFINE_SPINLOCK(cap_str_lock);
47 static int last_cap_str;
49 static char *gcap_string(char *s, int c)
51 if (c & CEPH_CAP_GSHARED)
53 if (c & CEPH_CAP_GEXCL)
55 if (c & CEPH_CAP_GCACHE)
61 if (c & CEPH_CAP_GBUFFER)
63 if (c & CEPH_CAP_GLAZYIO)
68 const char *ceph_cap_string(int caps)
74 spin_lock(&cap_str_lock);
76 if (last_cap_str == MAX_CAP_STR)
78 spin_unlock(&cap_str_lock);
82 if (caps & CEPH_CAP_PIN)
85 c = (caps >> CEPH_CAP_SAUTH) & 3;
88 s = gcap_string(s, c);
91 c = (caps >> CEPH_CAP_SLINK) & 3;
94 s = gcap_string(s, c);
97 c = (caps >> CEPH_CAP_SXATTR) & 3;
100 s = gcap_string(s, c);
103 c = caps >> CEPH_CAP_SFILE;
106 s = gcap_string(s, c);
118 * Maintain a global pool of preallocated struct ceph_caps, referenced
119 * by struct ceph_caps_reservations. This ensures that we preallocate
120 * memory needed to successfully process an MDS response. (If an MDS
121 * sends us cap information and we fail to process it, we will have
122 * problems due to the client and MDS being out of sync.)
124 * Reservations are 'owned' by a ceph_cap_reservation context.
126 static spinlock_t caps_list_lock;
127 static struct list_head caps_list; /* unused (reserved or unreserved) */
128 static int caps_total_count; /* total caps allocated */
129 static int caps_use_count; /* in use */
130 static int caps_reserve_count; /* unused, reserved */
131 static int caps_avail_count; /* unused, unreserved */
132 static int caps_min_count; /* keep at least this many (unreserved) */
134 void __init ceph_caps_init(void)
136 INIT_LIST_HEAD(&caps_list);
137 spin_lock_init(&caps_list_lock);
140 void ceph_caps_finalize(void)
142 struct ceph_cap *cap;
144 spin_lock(&caps_list_lock);
145 while (!list_empty(&caps_list)) {
146 cap = list_first_entry(&caps_list, struct ceph_cap, caps_item);
147 list_del(&cap->caps_item);
148 kmem_cache_free(ceph_cap_cachep, cap);
150 caps_total_count = 0;
151 caps_avail_count = 0;
153 caps_reserve_count = 0;
155 spin_unlock(&caps_list_lock);
158 void ceph_adjust_min_caps(int delta)
160 spin_lock(&caps_list_lock);
161 caps_min_count += delta;
162 BUG_ON(caps_min_count < 0);
163 spin_unlock(&caps_list_lock);
166 int ceph_reserve_caps(struct ceph_cap_reservation *ctx, int need)
169 struct ceph_cap *cap;
175 dout("reserve caps ctx=%p need=%d\n", ctx, need);
177 /* first reserve any caps that are already allocated */
178 spin_lock(&caps_list_lock);
179 if (caps_avail_count >= need)
182 have = caps_avail_count;
183 caps_avail_count -= have;
184 caps_reserve_count += have;
185 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count +
187 spin_unlock(&caps_list_lock);
189 for (i = have; i < need; i++) {
190 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
193 goto out_alloc_count;
195 list_add(&cap->caps_item, &newcaps);
198 BUG_ON(have + alloc != need);
200 spin_lock(&caps_list_lock);
201 caps_total_count += alloc;
202 caps_reserve_count += alloc;
203 list_splice(&newcaps, &caps_list);
205 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count +
207 spin_unlock(&caps_list_lock);
210 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
211 ctx, caps_total_count, caps_use_count, caps_reserve_count,
216 /* we didn't manage to reserve as much as we needed */
217 pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
222 int ceph_unreserve_caps(struct ceph_cap_reservation *ctx)
224 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
226 spin_lock(&caps_list_lock);
227 BUG_ON(caps_reserve_count < ctx->count);
228 caps_reserve_count -= ctx->count;
229 caps_avail_count += ctx->count;
231 dout("unreserve caps %d = %d used + %d resv + %d avail\n",
232 caps_total_count, caps_use_count, caps_reserve_count,
234 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count +
236 spin_unlock(&caps_list_lock);
241 static struct ceph_cap *get_cap(struct ceph_cap_reservation *ctx)
243 struct ceph_cap *cap = NULL;
245 /* temporary, until we do something about cap import/export */
247 return kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
249 spin_lock(&caps_list_lock);
250 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
251 ctx, ctx->count, caps_total_count, caps_use_count,
252 caps_reserve_count, caps_avail_count);
254 BUG_ON(ctx->count > caps_reserve_count);
255 BUG_ON(list_empty(&caps_list));
258 caps_reserve_count--;
261 cap = list_first_entry(&caps_list, struct ceph_cap, caps_item);
262 list_del(&cap->caps_item);
264 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count +
266 spin_unlock(&caps_list_lock);
270 void ceph_put_cap(struct ceph_cap *cap)
272 spin_lock(&caps_list_lock);
273 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
274 cap, caps_total_count, caps_use_count,
275 caps_reserve_count, caps_avail_count);
278 * Keep some preallocated caps around (ceph_min_count), to
279 * avoid lots of free/alloc churn.
281 if (caps_avail_count >= caps_reserve_count + caps_min_count) {
283 kmem_cache_free(ceph_cap_cachep, cap);
286 list_add(&cap->caps_item, &caps_list);
289 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count +
291 spin_unlock(&caps_list_lock);
294 void ceph_reservation_status(struct ceph_client *client,
295 int *total, int *avail, int *used, int *reserved,
299 *total = caps_total_count;
301 *avail = caps_avail_count;
303 *used = caps_use_count;
305 *reserved = caps_reserve_count;
307 *min = caps_min_count;
311 * Find ceph_cap for given mds, if any.
313 * Called with i_lock held.
315 static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
317 struct ceph_cap *cap;
318 struct rb_node *n = ci->i_caps.rb_node;
321 cap = rb_entry(n, struct ceph_cap, ci_node);
324 else if (mds > cap->mds)
333 * Return id of any MDS with a cap, preferably FILE_WR|WRBUFFER|EXCL, else
336 static int __ceph_get_cap_mds(struct ceph_inode_info *ci, u32 *mseq)
338 struct ceph_cap *cap;
342 /* prefer mds with WR|WRBUFFER|EXCL caps */
343 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
344 cap = rb_entry(p, struct ceph_cap, ci_node);
348 if (cap->issued & (CEPH_CAP_FILE_WR |
349 CEPH_CAP_FILE_BUFFER |
356 int ceph_get_cap_mds(struct inode *inode)
359 spin_lock(&inode->i_lock);
360 mds = __ceph_get_cap_mds(ceph_inode(inode), NULL);
361 spin_unlock(&inode->i_lock);
366 * Called under i_lock.
368 static void __insert_cap_node(struct ceph_inode_info *ci,
369 struct ceph_cap *new)
371 struct rb_node **p = &ci->i_caps.rb_node;
372 struct rb_node *parent = NULL;
373 struct ceph_cap *cap = NULL;
377 cap = rb_entry(parent, struct ceph_cap, ci_node);
378 if (new->mds < cap->mds)
380 else if (new->mds > cap->mds)
386 rb_link_node(&new->ci_node, parent, p);
387 rb_insert_color(&new->ci_node, &ci->i_caps);
391 * (re)set cap hold timeouts, which control the delayed release
392 * of unused caps back to the MDS. Should be called on cap use.
394 static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
395 struct ceph_inode_info *ci)
397 struct ceph_mount_args *ma = mdsc->client->mount_args;
399 ci->i_hold_caps_min = round_jiffies(jiffies +
400 ma->caps_wanted_delay_min * HZ);
401 ci->i_hold_caps_max = round_jiffies(jiffies +
402 ma->caps_wanted_delay_max * HZ);
403 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
404 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
408 * (Re)queue cap at the end of the delayed cap release list.
410 * If I_FLUSH is set, leave the inode at the front of the list.
412 * Caller holds i_lock
413 * -> we take mdsc->cap_delay_lock
415 static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
416 struct ceph_inode_info *ci)
418 __cap_set_timeouts(mdsc, ci);
419 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
420 ci->i_ceph_flags, ci->i_hold_caps_max);
421 if (!mdsc->stopping) {
422 spin_lock(&mdsc->cap_delay_lock);
423 if (!list_empty(&ci->i_cap_delay_list)) {
424 if (ci->i_ceph_flags & CEPH_I_FLUSH)
426 list_del_init(&ci->i_cap_delay_list);
428 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
430 spin_unlock(&mdsc->cap_delay_lock);
435 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
436 * indicating we should send a cap message to flush dirty metadata
437 * asap, and move to the front of the delayed cap list.
439 static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
440 struct ceph_inode_info *ci)
442 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
443 spin_lock(&mdsc->cap_delay_lock);
444 ci->i_ceph_flags |= CEPH_I_FLUSH;
445 if (!list_empty(&ci->i_cap_delay_list))
446 list_del_init(&ci->i_cap_delay_list);
447 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
448 spin_unlock(&mdsc->cap_delay_lock);
452 * Cancel delayed work on cap.
454 * Caller must hold i_lock.
456 static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
457 struct ceph_inode_info *ci)
459 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
460 if (list_empty(&ci->i_cap_delay_list))
462 spin_lock(&mdsc->cap_delay_lock);
463 list_del_init(&ci->i_cap_delay_list);
464 spin_unlock(&mdsc->cap_delay_lock);
468 * Common issue checks for add_cap, handle_cap_grant.
470 static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
473 unsigned had = __ceph_caps_issued(ci, NULL);
476 * Each time we receive FILE_CACHE anew, we increment
479 if ((issued & CEPH_CAP_FILE_CACHE) &&
480 (had & CEPH_CAP_FILE_CACHE) == 0)
484 * if we are newly issued FILE_SHARED, clear I_COMPLETE; we
485 * don't know what happened to this directory while we didn't
488 if ((issued & CEPH_CAP_FILE_SHARED) &&
489 (had & CEPH_CAP_FILE_SHARED) == 0) {
491 if (S_ISDIR(ci->vfs_inode.i_mode)) {
492 dout(" marking %p NOT complete\n", &ci->vfs_inode);
493 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
499 * Add a capability under the given MDS session.
501 * Caller should hold session snap_rwsem (read) and s_mutex.
503 * @fmode is the open file mode, if we are opening a file, otherwise
504 * it is < 0. (This is so we can atomically add the cap and add an
505 * open file reference to it.)
507 int ceph_add_cap(struct inode *inode,
508 struct ceph_mds_session *session, u64 cap_id,
509 int fmode, unsigned issued, unsigned wanted,
510 unsigned seq, unsigned mseq, u64 realmino, int flags,
511 struct ceph_cap_reservation *caps_reservation)
513 struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc;
514 struct ceph_inode_info *ci = ceph_inode(inode);
515 struct ceph_cap *new_cap = NULL;
516 struct ceph_cap *cap;
517 int mds = session->s_mds;
520 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
521 session->s_mds, cap_id, ceph_cap_string(issued), seq);
524 * If we are opening the file, include file mode wanted bits
528 wanted |= ceph_caps_for_mode(fmode);
531 spin_lock(&inode->i_lock);
532 cap = __get_cap_for_mds(ci, mds);
538 spin_unlock(&inode->i_lock);
539 new_cap = get_cap(caps_reservation);
546 cap->implemented = 0;
551 __insert_cap_node(ci, cap);
553 /* clear out old exporting info? (i.e. on cap import) */
554 if (ci->i_cap_exporting_mds == mds) {
555 ci->i_cap_exporting_issued = 0;
556 ci->i_cap_exporting_mseq = 0;
557 ci->i_cap_exporting_mds = -1;
560 /* add to session cap list */
561 cap->session = session;
562 spin_lock(&session->s_cap_lock);
563 list_add_tail(&cap->session_caps, &session->s_caps);
564 session->s_nr_caps++;
565 spin_unlock(&session->s_cap_lock);
568 if (!ci->i_snap_realm) {
570 * add this inode to the appropriate snap realm
572 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
575 ceph_get_snap_realm(mdsc, realm);
576 spin_lock(&realm->inodes_with_caps_lock);
577 ci->i_snap_realm = realm;
578 list_add(&ci->i_snap_realm_item,
579 &realm->inodes_with_caps);
580 spin_unlock(&realm->inodes_with_caps_lock);
582 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
587 __check_cap_issue(ci, cap, issued);
590 * If we are issued caps we don't want, or the mds' wanted
591 * value appears to be off, queue a check so we'll release
592 * later and/or update the mds wanted value.
594 actual_wanted = __ceph_caps_wanted(ci);
595 if ((wanted & ~actual_wanted) ||
596 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
597 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
598 ceph_cap_string(issued), ceph_cap_string(wanted),
599 ceph_cap_string(actual_wanted));
600 __cap_delay_requeue(mdsc, ci);
603 if (flags & CEPH_CAP_FLAG_AUTH)
604 ci->i_auth_cap = cap;
605 else if (ci->i_auth_cap == cap)
606 ci->i_auth_cap = NULL;
608 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
609 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
610 ceph_cap_string(issued|cap->issued), seq, mds);
611 cap->cap_id = cap_id;
612 cap->issued = issued;
613 cap->implemented |= issued;
614 cap->mds_wanted |= wanted;
616 cap->issue_seq = seq;
618 cap->cap_gen = session->s_cap_gen;
621 __ceph_get_fmode(ci, fmode);
622 spin_unlock(&inode->i_lock);
623 wake_up(&ci->i_cap_wq);
628 * Return true if cap has not timed out and belongs to the current
629 * generation of the MDS session (i.e. has not gone 'stale' due to
630 * us losing touch with the mds).
632 static int __cap_is_valid(struct ceph_cap *cap)
637 spin_lock(&cap->session->s_cap_lock);
638 gen = cap->session->s_cap_gen;
639 ttl = cap->session->s_cap_ttl;
640 spin_unlock(&cap->session->s_cap_lock);
642 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
643 dout("__cap_is_valid %p cap %p issued %s "
644 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
645 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
653 * Return set of valid cap bits issued to us. Note that caps time
654 * out, and may be invalidated in bulk if the client session times out
655 * and session->s_cap_gen is bumped.
657 int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
659 int have = ci->i_snap_caps | ci->i_cap_exporting_issued;
660 struct ceph_cap *cap;
665 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
666 cap = rb_entry(p, struct ceph_cap, ci_node);
667 if (!__cap_is_valid(cap))
669 dout("__ceph_caps_issued %p cap %p issued %s\n",
670 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
673 *implemented |= cap->implemented;
679 * Get cap bits issued by caps other than @ocap
681 int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
683 int have = ci->i_snap_caps;
684 struct ceph_cap *cap;
687 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
688 cap = rb_entry(p, struct ceph_cap, ci_node);
691 if (!__cap_is_valid(cap))
699 * Move a cap to the end of the LRU (oldest caps at list head, newest
702 static void __touch_cap(struct ceph_cap *cap)
704 struct ceph_mds_session *s = cap->session;
706 spin_lock(&s->s_cap_lock);
707 if (s->s_cap_iterator == NULL) {
708 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
710 list_move_tail(&cap->session_caps, &s->s_caps);
712 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
713 &cap->ci->vfs_inode, cap, s->s_mds);
715 spin_unlock(&s->s_cap_lock);
719 * Check if we hold the given mask. If so, move the cap(s) to the
720 * front of their respective LRUs. (This is the preferred way for
721 * callers to check for caps they want.)
723 int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
725 struct ceph_cap *cap;
727 int have = ci->i_snap_caps;
729 if ((have & mask) == mask) {
730 dout("__ceph_caps_issued_mask %p snap issued %s"
731 " (mask %s)\n", &ci->vfs_inode,
732 ceph_cap_string(have),
733 ceph_cap_string(mask));
737 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
738 cap = rb_entry(p, struct ceph_cap, ci_node);
739 if (!__cap_is_valid(cap))
741 if ((cap->issued & mask) == mask) {
742 dout("__ceph_caps_issued_mask %p cap %p issued %s"
743 " (mask %s)\n", &ci->vfs_inode, cap,
744 ceph_cap_string(cap->issued),
745 ceph_cap_string(mask));
751 /* does a combination of caps satisfy mask? */
753 if ((have & mask) == mask) {
754 dout("__ceph_caps_issued_mask %p combo issued %s"
755 " (mask %s)\n", &ci->vfs_inode,
756 ceph_cap_string(cap->issued),
757 ceph_cap_string(mask));
761 /* touch this + preceeding caps */
763 for (q = rb_first(&ci->i_caps); q != p;
765 cap = rb_entry(q, struct ceph_cap,
767 if (!__cap_is_valid(cap))
780 * Return true if mask caps are currently being revoked by an MDS.
782 int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
784 struct inode *inode = &ci->vfs_inode;
785 struct ceph_cap *cap;
789 spin_lock(&inode->i_lock);
790 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
791 cap = rb_entry(p, struct ceph_cap, ci_node);
792 if (__cap_is_valid(cap) &&
793 (cap->implemented & ~cap->issued & mask)) {
798 spin_unlock(&inode->i_lock);
799 dout("ceph_caps_revoking %p %s = %d\n", inode,
800 ceph_cap_string(mask), ret);
804 int __ceph_caps_used(struct ceph_inode_info *ci)
808 used |= CEPH_CAP_PIN;
810 used |= CEPH_CAP_FILE_RD;
811 if (ci->i_rdcache_ref || ci->i_rdcache_gen)
812 used |= CEPH_CAP_FILE_CACHE;
814 used |= CEPH_CAP_FILE_WR;
815 if (ci->i_wrbuffer_ref)
816 used |= CEPH_CAP_FILE_BUFFER;
821 * wanted, by virtue of open file modes
823 int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
827 for (mode = 0; mode < 4; mode++)
828 if (ci->i_nr_by_mode[mode])
829 want |= ceph_caps_for_mode(mode);
834 * Return caps we have registered with the MDS(s) as 'wanted'.
836 int __ceph_caps_mds_wanted(struct ceph_inode_info *ci)
838 struct ceph_cap *cap;
842 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
843 cap = rb_entry(p, struct ceph_cap, ci_node);
844 if (!__cap_is_valid(cap))
846 mds_wanted |= cap->mds_wanted;
852 * called under i_lock
854 static int __ceph_is_any_caps(struct ceph_inode_info *ci)
856 return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0;
860 * caller should hold i_lock.
861 * caller will not hold session s_mutex if called from destroy_inode.
863 void __ceph_remove_cap(struct ceph_cap *cap)
865 struct ceph_mds_session *session = cap->session;
866 struct ceph_inode_info *ci = cap->ci;
867 struct ceph_mds_client *mdsc = &ceph_client(ci->vfs_inode.i_sb)->mdsc;
869 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
871 /* remove from inode list */
872 rb_erase(&cap->ci_node, &ci->i_caps);
874 if (ci->i_auth_cap == cap)
875 ci->i_auth_cap = NULL;
877 /* remove from session list */
878 spin_lock(&session->s_cap_lock);
879 if (session->s_cap_iterator == cap) {
880 /* not yet, we are iterating over this very cap */
881 dout("__ceph_remove_cap delaying %p removal from session %p\n",
884 list_del_init(&cap->session_caps);
885 session->s_nr_caps--;
888 spin_unlock(&session->s_cap_lock);
890 if (cap->session == NULL)
893 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) {
894 struct ceph_snap_realm *realm = ci->i_snap_realm;
895 spin_lock(&realm->inodes_with_caps_lock);
896 list_del_init(&ci->i_snap_realm_item);
897 ci->i_snap_realm_counter++;
898 ci->i_snap_realm = NULL;
899 spin_unlock(&realm->inodes_with_caps_lock);
900 ceph_put_snap_realm(mdsc, realm);
902 if (!__ceph_is_any_real_caps(ci))
903 __cap_delay_cancel(mdsc, ci);
907 * Build and send a cap message to the given MDS.
909 * Caller should be holding s_mutex.
911 static int send_cap_msg(struct ceph_mds_session *session,
912 u64 ino, u64 cid, int op,
913 int caps, int wanted, int dirty,
914 u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq,
915 u64 size, u64 max_size,
916 struct timespec *mtime, struct timespec *atime,
918 uid_t uid, gid_t gid, mode_t mode,
920 struct ceph_buffer *xattrs_buf,
923 struct ceph_mds_caps *fc;
924 struct ceph_msg *msg;
926 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
927 " seq %u/%u mseq %u follows %lld size %llu/%llu"
928 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op),
929 cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted),
930 ceph_cap_string(dirty),
931 seq, issue_seq, mseq, follows, size, max_size,
932 xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0);
934 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), 0, 0, NULL);
938 msg->hdr.tid = cpu_to_le64(flush_tid);
940 fc = msg->front.iov_base;
941 memset(fc, 0, sizeof(*fc));
943 fc->cap_id = cpu_to_le64(cid);
944 fc->op = cpu_to_le32(op);
945 fc->seq = cpu_to_le32(seq);
946 fc->issue_seq = cpu_to_le32(issue_seq);
947 fc->migrate_seq = cpu_to_le32(mseq);
948 fc->caps = cpu_to_le32(caps);
949 fc->wanted = cpu_to_le32(wanted);
950 fc->dirty = cpu_to_le32(dirty);
951 fc->ino = cpu_to_le64(ino);
952 fc->snap_follows = cpu_to_le64(follows);
954 fc->size = cpu_to_le64(size);
955 fc->max_size = cpu_to_le64(max_size);
957 ceph_encode_timespec(&fc->mtime, mtime);
959 ceph_encode_timespec(&fc->atime, atime);
960 fc->time_warp_seq = cpu_to_le32(time_warp_seq);
962 fc->uid = cpu_to_le32(uid);
963 fc->gid = cpu_to_le32(gid);
964 fc->mode = cpu_to_le32(mode);
966 fc->xattr_version = cpu_to_le64(xattr_version);
968 msg->middle = ceph_buffer_get(xattrs_buf);
969 fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len);
970 msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len);
973 ceph_con_send(&session->s_con, msg);
978 * Queue cap releases when an inode is dropped from our cache. Since
979 * inode is about to be destroyed, there is no need for i_lock.
981 void ceph_queue_caps_release(struct inode *inode)
983 struct ceph_inode_info *ci = ceph_inode(inode);
986 p = rb_first(&ci->i_caps);
988 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
989 struct ceph_mds_session *session = cap->session;
990 struct ceph_msg *msg;
991 struct ceph_mds_cap_release *head;
992 struct ceph_mds_cap_item *item;
994 spin_lock(&session->s_cap_lock);
995 BUG_ON(!session->s_num_cap_releases);
996 msg = list_first_entry(&session->s_cap_releases,
997 struct ceph_msg, list_head);
999 dout(" adding %p release to mds%d msg %p (%d left)\n",
1000 inode, session->s_mds, msg, session->s_num_cap_releases);
1002 BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE);
1003 head = msg->front.iov_base;
1004 head->num = cpu_to_le32(le32_to_cpu(head->num) + 1);
1005 item = msg->front.iov_base + msg->front.iov_len;
1006 item->ino = cpu_to_le64(ceph_ino(inode));
1007 item->cap_id = cpu_to_le64(cap->cap_id);
1008 item->migrate_seq = cpu_to_le32(cap->mseq);
1009 item->seq = cpu_to_le32(cap->issue_seq);
1011 session->s_num_cap_releases--;
1013 msg->front.iov_len += sizeof(*item);
1014 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
1015 dout(" release msg %p full\n", msg);
1016 list_move_tail(&msg->list_head,
1017 &session->s_cap_releases_done);
1019 dout(" release msg %p at %d/%d (%d)\n", msg,
1020 (int)le32_to_cpu(head->num),
1021 (int)CEPH_CAPS_PER_RELEASE,
1022 (int)msg->front.iov_len);
1024 spin_unlock(&session->s_cap_lock);
1026 __ceph_remove_cap(cap);
1031 * Send a cap msg on the given inode. Update our caps state, then
1032 * drop i_lock and send the message.
1034 * Make note of max_size reported/requested from mds, revoked caps
1035 * that have now been implemented.
1037 * Make half-hearted attempt ot to invalidate page cache if we are
1038 * dropping RDCACHE. Note that this will leave behind locked pages
1039 * that we'll then need to deal with elsewhere.
1041 * Return non-zero if delayed release, or we experienced an error
1042 * such that the caller should requeue + retry later.
1044 * called with i_lock, then drops it.
1045 * caller should hold snap_rwsem (read), s_mutex.
1047 static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1048 int op, int used, int want, int retain, int flushing,
1049 unsigned *pflush_tid)
1050 __releases(cap->ci->vfs_inode->i_lock)
1052 struct ceph_inode_info *ci = cap->ci;
1053 struct inode *inode = &ci->vfs_inode;
1054 u64 cap_id = cap->cap_id;
1055 int held, revoking, dropping, keep;
1056 u64 seq, issue_seq, mseq, time_warp_seq, follows;
1058 struct timespec mtime, atime;
1063 struct ceph_mds_session *session;
1064 u64 xattr_version = 0;
1070 held = cap->issued | cap->implemented;
1071 revoking = cap->implemented & ~cap->issued;
1072 retain &= ~revoking;
1073 dropping = cap->issued & ~retain;
1075 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1076 inode, cap, cap->session,
1077 ceph_cap_string(held), ceph_cap_string(held & retain),
1078 ceph_cap_string(revoking));
1079 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1081 session = cap->session;
1083 /* don't release wanted unless we've waited a bit. */
1084 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1085 time_before(jiffies, ci->i_hold_caps_min)) {
1086 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1087 ceph_cap_string(cap->issued),
1088 ceph_cap_string(cap->issued & retain),
1089 ceph_cap_string(cap->mds_wanted),
1090 ceph_cap_string(want));
1091 want |= cap->mds_wanted;
1092 retain |= cap->issued;
1095 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1097 cap->issued &= retain; /* drop bits we don't want */
1098 if (cap->implemented & ~cap->issued) {
1100 * Wake up any waiters on wanted -> needed transition.
1101 * This is due to the weird transition from buffered
1102 * to sync IO... we need to flush dirty pages _before_
1103 * allowing sync writes to avoid reordering.
1107 cap->implemented &= cap->issued | used;
1108 cap->mds_wanted = want;
1112 * assign a tid for flush operations so we can avoid
1113 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1114 * clean type races. track latest tid for every bit
1115 * so we can handle flush AxFw, flush Fw, and have the
1116 * first ack clean Ax.
1118 flush_tid = ++ci->i_cap_flush_last_tid;
1120 *pflush_tid = flush_tid;
1121 dout(" cap_flush_tid %d\n", (int)flush_tid);
1122 for (i = 0; i < CEPH_CAP_BITS; i++)
1123 if (flushing & (1 << i))
1124 ci->i_cap_flush_tid[i] = flush_tid;
1127 keep = cap->implemented;
1129 issue_seq = cap->issue_seq;
1131 size = inode->i_size;
1132 ci->i_reported_size = size;
1133 max_size = ci->i_wanted_max_size;
1134 ci->i_requested_max_size = max_size;
1135 mtime = inode->i_mtime;
1136 atime = inode->i_atime;
1137 time_warp_seq = ci->i_time_warp_seq;
1138 follows = ci->i_snap_realm->cached_context->seq;
1141 mode = inode->i_mode;
1143 if (dropping & CEPH_CAP_XATTR_EXCL) {
1144 __ceph_build_xattrs_blob(ci);
1145 xattr_version = ci->i_xattrs.version + 1;
1148 spin_unlock(&inode->i_lock);
1150 ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id,
1151 op, keep, want, flushing, seq, flush_tid, issue_seq, mseq,
1152 size, max_size, &mtime, &atime, time_warp_seq,
1155 (flushing & CEPH_CAP_XATTR_EXCL) ? ci->i_xattrs.blob : NULL,
1158 dout("error sending cap msg, must requeue %p\n", inode);
1163 wake_up(&ci->i_cap_wq);
1169 * When a snapshot is taken, clients accumulate dirty metadata on
1170 * inodes with capabilities in ceph_cap_snaps to describe the file
1171 * state at the time the snapshot was taken. This must be flushed
1172 * asynchronously back to the MDS once sync writes complete and dirty
1173 * data is written out.
1175 * Called under i_lock. Takes s_mutex as needed.
1177 void __ceph_flush_snaps(struct ceph_inode_info *ci,
1178 struct ceph_mds_session **psession)
1180 struct inode *inode = &ci->vfs_inode;
1182 struct ceph_cap_snap *capsnap;
1184 struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc;
1185 struct ceph_mds_session *session = NULL; /* if session != NULL, we hold
1187 u64 next_follows = 0; /* keep track of how far we've gotten through the
1188 i_cap_snaps list, and skip these entries next time
1189 around to avoid an infinite loop */
1192 session = *psession;
1194 dout("__flush_snaps %p\n", inode);
1196 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1197 /* avoid an infiniute loop after retry */
1198 if (capsnap->follows < next_follows)
1201 * we need to wait for sync writes to complete and for dirty
1202 * pages to be written out.
1204 if (capsnap->dirty_pages || capsnap->writing)
1208 * if cap writeback already occurred, we should have dropped
1209 * the capsnap in ceph_put_wrbuffer_cap_refs.
1211 BUG_ON(capsnap->dirty == 0);
1213 /* pick mds, take s_mutex */
1214 mds = __ceph_get_cap_mds(ci, &mseq);
1215 if (session && session->s_mds != mds) {
1216 dout("oops, wrong session %p mutex\n", session);
1217 mutex_unlock(&session->s_mutex);
1218 ceph_put_mds_session(session);
1222 spin_unlock(&inode->i_lock);
1223 mutex_lock(&mdsc->mutex);
1224 session = __ceph_lookup_mds_session(mdsc, mds);
1225 mutex_unlock(&mdsc->mutex);
1227 dout("inverting session/ino locks on %p\n",
1229 mutex_lock(&session->s_mutex);
1232 * if session == NULL, we raced against a cap
1233 * deletion. retry, and we'll get a better
1234 * @mds value next time.
1236 spin_lock(&inode->i_lock);
1240 capsnap->flush_tid = ++ci->i_cap_flush_last_tid;
1241 atomic_inc(&capsnap->nref);
1242 if (!list_empty(&capsnap->flushing_item))
1243 list_del_init(&capsnap->flushing_item);
1244 list_add_tail(&capsnap->flushing_item,
1245 &session->s_cap_snaps_flushing);
1246 spin_unlock(&inode->i_lock);
1248 dout("flush_snaps %p cap_snap %p follows %lld size %llu\n",
1249 inode, capsnap, next_follows, capsnap->size);
1250 send_cap_msg(session, ceph_vino(inode).ino, 0,
1251 CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0,
1252 capsnap->dirty, 0, capsnap->flush_tid, 0, mseq,
1254 &capsnap->mtime, &capsnap->atime,
1255 capsnap->time_warp_seq,
1256 capsnap->uid, capsnap->gid, capsnap->mode,
1260 next_follows = capsnap->follows + 1;
1261 ceph_put_cap_snap(capsnap);
1263 spin_lock(&inode->i_lock);
1267 /* we flushed them all; remove this inode from the queue */
1268 spin_lock(&mdsc->snap_flush_lock);
1269 list_del_init(&ci->i_snap_flush_item);
1270 spin_unlock(&mdsc->snap_flush_lock);
1273 *psession = session;
1275 mutex_unlock(&session->s_mutex);
1276 ceph_put_mds_session(session);
1280 static void ceph_flush_snaps(struct ceph_inode_info *ci)
1282 struct inode *inode = &ci->vfs_inode;
1284 spin_lock(&inode->i_lock);
1285 __ceph_flush_snaps(ci, NULL);
1286 spin_unlock(&inode->i_lock);
1290 * Mark caps dirty. If inode is newly dirty, add to the global dirty
1293 void __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask)
1295 struct ceph_mds_client *mdsc = &ceph_client(ci->vfs_inode.i_sb)->mdsc;
1296 struct inode *inode = &ci->vfs_inode;
1297 int was = ci->i_dirty_caps;
1300 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1301 ceph_cap_string(mask), ceph_cap_string(was),
1302 ceph_cap_string(was | mask));
1303 ci->i_dirty_caps |= mask;
1305 dout(" inode %p now dirty\n", &ci->vfs_inode);
1306 BUG_ON(!list_empty(&ci->i_dirty_item));
1307 spin_lock(&mdsc->cap_dirty_lock);
1308 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
1309 spin_unlock(&mdsc->cap_dirty_lock);
1310 if (ci->i_flushing_caps == 0) {
1312 dirty |= I_DIRTY_SYNC;
1315 BUG_ON(list_empty(&ci->i_dirty_item));
1316 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1317 (mask & CEPH_CAP_FILE_BUFFER))
1318 dirty |= I_DIRTY_DATASYNC;
1320 __mark_inode_dirty(inode, dirty);
1321 __cap_delay_requeue(mdsc, ci);
1325 * Add dirty inode to the flushing list. Assigned a seq number so we
1326 * can wait for caps to flush without starving.
1328 * Called under i_lock.
1330 static int __mark_caps_flushing(struct inode *inode,
1331 struct ceph_mds_session *session)
1333 struct ceph_mds_client *mdsc = &ceph_client(inode->i_sb)->mdsc;
1334 struct ceph_inode_info *ci = ceph_inode(inode);
1337 BUG_ON(ci->i_dirty_caps == 0);
1338 BUG_ON(list_empty(&ci->i_dirty_item));
1340 flushing = ci->i_dirty_caps;
1341 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1342 ceph_cap_string(flushing),
1343 ceph_cap_string(ci->i_flushing_caps),
1344 ceph_cap_string(ci->i_flushing_caps | flushing));
1345 ci->i_flushing_caps |= flushing;
1346 ci->i_dirty_caps = 0;
1347 dout(" inode %p now !dirty\n", inode);
1349 spin_lock(&mdsc->cap_dirty_lock);
1350 list_del_init(&ci->i_dirty_item);
1352 ci->i_cap_flush_seq = ++mdsc->cap_flush_seq;
1353 if (list_empty(&ci->i_flushing_item)) {
1354 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1355 mdsc->num_cap_flushing++;
1356 dout(" inode %p now flushing seq %lld\n", inode,
1357 ci->i_cap_flush_seq);
1359 list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1360 dout(" inode %p now flushing (more) seq %lld\n", inode,
1361 ci->i_cap_flush_seq);
1363 spin_unlock(&mdsc->cap_dirty_lock);
1369 * try to invalidate mapping pages without blocking.
1371 static int mapping_is_empty(struct address_space *mapping)
1373 struct page *page = find_get_page(mapping, 0);
1382 static int try_nonblocking_invalidate(struct inode *inode)
1384 struct ceph_inode_info *ci = ceph_inode(inode);
1385 u32 invalidating_gen = ci->i_rdcache_gen;
1387 spin_unlock(&inode->i_lock);
1388 invalidate_mapping_pages(&inode->i_data, 0, -1);
1389 spin_lock(&inode->i_lock);
1391 if (mapping_is_empty(&inode->i_data) &&
1392 invalidating_gen == ci->i_rdcache_gen) {
1394 dout("try_nonblocking_invalidate %p success\n", inode);
1395 ci->i_rdcache_gen = 0;
1396 ci->i_rdcache_revoking = 0;
1399 dout("try_nonblocking_invalidate %p failed\n", inode);
1404 * Swiss army knife function to examine currently used and wanted
1405 * versus held caps. Release, flush, ack revoked caps to mds as
1408 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1409 * cap release further.
1410 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1411 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1414 void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1415 struct ceph_mds_session *session)
1416 __releases(session->s_mutex)
1418 struct ceph_client *client = ceph_inode_to_client(&ci->vfs_inode);
1419 struct ceph_mds_client *mdsc = &client->mdsc;
1420 struct inode *inode = &ci->vfs_inode;
1421 struct ceph_cap *cap;
1422 int file_wanted, used;
1423 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
1424 int issued, implemented, want, retain, revoking, flushing = 0;
1425 int mds = -1; /* keep track of how far we've gone through i_caps list
1426 to avoid an infinite loop on retry */
1428 int tried_invalidate = 0;
1429 int delayed = 0, sent = 0, force_requeue = 0, num;
1430 int queue_invalidate = 0;
1431 int is_delayed = flags & CHECK_CAPS_NODELAY;
1433 /* if we are unmounting, flush any unused caps immediately. */
1437 spin_lock(&inode->i_lock);
1439 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1440 flags |= CHECK_CAPS_FLUSH;
1442 /* flush snaps first time around only */
1443 if (!list_empty(&ci->i_cap_snaps))
1444 __ceph_flush_snaps(ci, &session);
1447 spin_lock(&inode->i_lock);
1449 file_wanted = __ceph_caps_file_wanted(ci);
1450 used = __ceph_caps_used(ci);
1451 want = file_wanted | used;
1452 issued = __ceph_caps_issued(ci, &implemented);
1453 revoking = implemented & ~issued;
1455 retain = want | CEPH_CAP_PIN;
1456 if (!mdsc->stopping && inode->i_nlink > 0) {
1458 retain |= CEPH_CAP_ANY; /* be greedy */
1460 retain |= CEPH_CAP_ANY_SHARED;
1462 * keep RD only if we didn't have the file open RW,
1463 * because then the mds would revoke it anyway to
1464 * journal max_size=0.
1466 if (ci->i_max_size == 0)
1467 retain |= CEPH_CAP_ANY_RD;
1471 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1472 " issued %s revoking %s retain %s %s%s%s\n", inode,
1473 ceph_cap_string(file_wanted),
1474 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1475 ceph_cap_string(ci->i_flushing_caps),
1476 ceph_cap_string(issued), ceph_cap_string(revoking),
1477 ceph_cap_string(retain),
1478 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1479 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1480 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1483 * If we no longer need to hold onto old our caps, and we may
1484 * have cached pages, but don't want them, then try to invalidate.
1485 * If we fail, it's because pages are locked.... try again later.
1487 if ((!is_delayed || mdsc->stopping) &&
1488 ci->i_wrbuffer_ref == 0 && /* no dirty pages... */
1489 ci->i_rdcache_gen && /* may have cached pages */
1490 (file_wanted == 0 || /* no open files */
1491 (revoking & CEPH_CAP_FILE_CACHE)) && /* or revoking cache */
1492 !tried_invalidate) {
1493 dout("check_caps trying to invalidate on %p\n", inode);
1494 if (try_nonblocking_invalidate(inode) < 0) {
1495 if (revoking & CEPH_CAP_FILE_CACHE) {
1496 dout("check_caps queuing invalidate\n");
1497 queue_invalidate = 1;
1498 ci->i_rdcache_revoking = ci->i_rdcache_gen;
1500 dout("check_caps failed to invalidate pages\n");
1501 /* we failed to invalidate pages. check these
1502 caps again later. */
1504 __cap_set_timeouts(mdsc, ci);
1507 tried_invalidate = 1;
1512 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1513 cap = rb_entry(p, struct ceph_cap, ci_node);
1516 /* avoid looping forever */
1517 if (mds >= cap->mds ||
1518 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1521 /* NOTE: no side-effects allowed, until we take s_mutex */
1523 revoking = cap->implemented & ~cap->issued;
1525 dout(" mds%d revoking %s\n", cap->mds,
1526 ceph_cap_string(revoking));
1528 if (cap == ci->i_auth_cap &&
1529 (cap->issued & CEPH_CAP_FILE_WR)) {
1530 /* request larger max_size from MDS? */
1531 if (ci->i_wanted_max_size > ci->i_max_size &&
1532 ci->i_wanted_max_size > ci->i_requested_max_size) {
1533 dout("requesting new max_size\n");
1537 /* approaching file_max? */
1538 if ((inode->i_size << 1) >= ci->i_max_size &&
1539 (ci->i_reported_size << 1) < ci->i_max_size) {
1540 dout("i_size approaching max_size\n");
1544 /* flush anything dirty? */
1545 if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) &&
1547 dout("flushing dirty caps\n");
1551 /* completed revocation? going down and there are no caps? */
1552 if (revoking && (revoking & used) == 0) {
1553 dout("completed revocation of %s\n",
1554 ceph_cap_string(cap->implemented & ~cap->issued));
1558 /* want more caps from mds? */
1559 if (want & ~(cap->mds_wanted | cap->issued))
1562 /* things we might delay */
1563 if ((cap->issued & ~retain) == 0 &&
1564 cap->mds_wanted == want)
1565 continue; /* nope, all good */
1571 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1572 time_before(jiffies, ci->i_hold_caps_max)) {
1573 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1574 ceph_cap_string(cap->issued),
1575 ceph_cap_string(cap->issued & retain),
1576 ceph_cap_string(cap->mds_wanted),
1577 ceph_cap_string(want));
1583 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1584 dout(" skipping %p I_NOFLUSH set\n", inode);
1588 if (session && session != cap->session) {
1589 dout("oops, wrong session %p mutex\n", session);
1590 mutex_unlock(&session->s_mutex);
1594 session = cap->session;
1595 if (mutex_trylock(&session->s_mutex) == 0) {
1596 dout("inverting session/ino locks on %p\n",
1598 spin_unlock(&inode->i_lock);
1599 if (took_snap_rwsem) {
1600 up_read(&mdsc->snap_rwsem);
1601 took_snap_rwsem = 0;
1603 mutex_lock(&session->s_mutex);
1607 /* take snap_rwsem after session mutex */
1608 if (!took_snap_rwsem) {
1609 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
1610 dout("inverting snap/in locks on %p\n",
1612 spin_unlock(&inode->i_lock);
1613 down_read(&mdsc->snap_rwsem);
1614 took_snap_rwsem = 1;
1617 took_snap_rwsem = 1;
1620 if (cap == ci->i_auth_cap && ci->i_dirty_caps)
1621 flushing = __mark_caps_flushing(inode, session);
1623 mds = cap->mds; /* remember mds, so we don't repeat */
1626 /* __send_cap drops i_lock */
1627 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, used, want,
1628 retain, flushing, NULL);
1629 goto retry; /* retake i_lock and restart our cap scan. */
1633 * Reschedule delayed caps release if we delayed anything,
1636 if (delayed && is_delayed)
1637 force_requeue = 1; /* __send_cap delayed release; requeue */
1638 if (!delayed && !is_delayed)
1639 __cap_delay_cancel(mdsc, ci);
1640 else if (!is_delayed || force_requeue)
1641 __cap_delay_requeue(mdsc, ci);
1643 spin_unlock(&inode->i_lock);
1645 if (queue_invalidate)
1646 ceph_queue_invalidate(inode);
1649 mutex_unlock(&session->s_mutex);
1650 if (took_snap_rwsem)
1651 up_read(&mdsc->snap_rwsem);
1655 * Try to flush dirty caps back to the auth mds.
1657 static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session,
1658 unsigned *flush_tid)
1660 struct ceph_mds_client *mdsc = &ceph_client(inode->i_sb)->mdsc;
1661 struct ceph_inode_info *ci = ceph_inode(inode);
1662 int unlock_session = session ? 0 : 1;
1666 spin_lock(&inode->i_lock);
1667 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1668 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
1671 if (ci->i_dirty_caps && ci->i_auth_cap) {
1672 struct ceph_cap *cap = ci->i_auth_cap;
1673 int used = __ceph_caps_used(ci);
1674 int want = __ceph_caps_wanted(ci);
1678 spin_unlock(&inode->i_lock);
1679 session = cap->session;
1680 mutex_lock(&session->s_mutex);
1683 BUG_ON(session != cap->session);
1684 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN)
1687 flushing = __mark_caps_flushing(inode, session);
1689 /* __send_cap drops i_lock */
1690 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want,
1691 cap->issued | cap->implemented, flushing,
1696 spin_lock(&inode->i_lock);
1697 __cap_delay_requeue(mdsc, ci);
1700 spin_unlock(&inode->i_lock);
1702 if (session && unlock_session)
1703 mutex_unlock(&session->s_mutex);
1708 * Return true if we've flushed caps through the given flush_tid.
1710 static int caps_are_flushed(struct inode *inode, unsigned tid)
1712 struct ceph_inode_info *ci = ceph_inode(inode);
1713 int dirty, i, ret = 1;
1715 spin_lock(&inode->i_lock);
1716 dirty = __ceph_caps_dirty(ci);
1717 for (i = 0; i < CEPH_CAP_BITS; i++)
1718 if ((ci->i_flushing_caps & (1 << i)) &&
1719 ci->i_cap_flush_tid[i] <= tid) {
1720 /* still flushing this bit */
1724 spin_unlock(&inode->i_lock);
1729 * Wait on any unsafe replies for the given inode. First wait on the
1730 * newest request, and make that the upper bound. Then, if there are
1731 * more requests, keep waiting on the oldest as long as it is still older
1732 * than the original request.
1734 static void sync_write_wait(struct inode *inode)
1736 struct ceph_inode_info *ci = ceph_inode(inode);
1737 struct list_head *head = &ci->i_unsafe_writes;
1738 struct ceph_osd_request *req;
1741 spin_lock(&ci->i_unsafe_lock);
1742 if (list_empty(head))
1745 /* set upper bound as _last_ entry in chain */
1746 req = list_entry(head->prev, struct ceph_osd_request,
1748 last_tid = req->r_tid;
1751 ceph_osdc_get_request(req);
1752 spin_unlock(&ci->i_unsafe_lock);
1753 dout("sync_write_wait on tid %llu (until %llu)\n",
1754 req->r_tid, last_tid);
1755 wait_for_completion(&req->r_safe_completion);
1756 spin_lock(&ci->i_unsafe_lock);
1757 ceph_osdc_put_request(req);
1760 * from here on look at first entry in chain, since we
1761 * only want to wait for anything older than last_tid
1763 if (list_empty(head))
1765 req = list_entry(head->next, struct ceph_osd_request,
1767 } while (req->r_tid < last_tid);
1769 spin_unlock(&ci->i_unsafe_lock);
1772 int ceph_fsync(struct file *file, struct dentry *dentry, int datasync)
1774 struct inode *inode = dentry->d_inode;
1775 struct ceph_inode_info *ci = ceph_inode(inode);
1780 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
1781 sync_write_wait(inode);
1783 ret = filemap_write_and_wait(inode->i_mapping);
1787 dirty = try_flush_caps(inode, NULL, &flush_tid);
1788 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
1791 * only wait on non-file metadata writeback (the mds
1792 * can recover size and mtime, so we don't need to
1795 if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
1796 dout("fsync waiting for flush_tid %u\n", flush_tid);
1797 ret = wait_event_interruptible(ci->i_cap_wq,
1798 caps_are_flushed(inode, flush_tid));
1801 dout("fsync %p%s done\n", inode, datasync ? " datasync" : "");
1806 * Flush any dirty caps back to the mds. If we aren't asked to wait,
1807 * queue inode for flush but don't do so immediately, because we can
1808 * get by with fewer MDS messages if we wait for data writeback to
1811 int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
1813 struct ceph_inode_info *ci = ceph_inode(inode);
1817 int wait = wbc->sync_mode == WB_SYNC_ALL;
1819 dout("write_inode %p wait=%d\n", inode, wait);
1821 dirty = try_flush_caps(inode, NULL, &flush_tid);
1823 err = wait_event_interruptible(ci->i_cap_wq,
1824 caps_are_flushed(inode, flush_tid));
1826 struct ceph_mds_client *mdsc = &ceph_client(inode->i_sb)->mdsc;
1828 spin_lock(&inode->i_lock);
1829 if (__ceph_caps_dirty(ci))
1830 __cap_delay_requeue_front(mdsc, ci);
1831 spin_unlock(&inode->i_lock);
1837 * After a recovering MDS goes active, we need to resend any caps
1840 * Caller holds session->s_mutex.
1842 static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc,
1843 struct ceph_mds_session *session)
1845 struct ceph_cap_snap *capsnap;
1847 dout("kick_flushing_capsnaps mds%d\n", session->s_mds);
1848 list_for_each_entry(capsnap, &session->s_cap_snaps_flushing,
1850 struct ceph_inode_info *ci = capsnap->ci;
1851 struct inode *inode = &ci->vfs_inode;
1852 struct ceph_cap *cap;
1854 spin_lock(&inode->i_lock);
1855 cap = ci->i_auth_cap;
1856 if (cap && cap->session == session) {
1857 dout("kick_flushing_caps %p cap %p capsnap %p\n", inode,
1859 __ceph_flush_snaps(ci, &session);
1861 pr_err("%p auth cap %p not mds%d ???\n", inode,
1862 cap, session->s_mds);
1863 spin_unlock(&inode->i_lock);
1868 void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
1869 struct ceph_mds_session *session)
1871 struct ceph_inode_info *ci;
1873 kick_flushing_capsnaps(mdsc, session);
1875 dout("kick_flushing_caps mds%d\n", session->s_mds);
1876 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
1877 struct inode *inode = &ci->vfs_inode;
1878 struct ceph_cap *cap;
1881 spin_lock(&inode->i_lock);
1882 cap = ci->i_auth_cap;
1883 if (cap && cap->session == session) {
1884 dout("kick_flushing_caps %p cap %p %s\n", inode,
1885 cap, ceph_cap_string(ci->i_flushing_caps));
1886 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
1887 __ceph_caps_used(ci),
1888 __ceph_caps_wanted(ci),
1889 cap->issued | cap->implemented,
1890 ci->i_flushing_caps, NULL);
1892 spin_lock(&inode->i_lock);
1893 __cap_delay_requeue(mdsc, ci);
1894 spin_unlock(&inode->i_lock);
1897 pr_err("%p auth cap %p not mds%d ???\n", inode,
1898 cap, session->s_mds);
1899 spin_unlock(&inode->i_lock);
1906 * Take references to capabilities we hold, so that we don't release
1907 * them to the MDS prematurely.
1909 * Protected by i_lock.
1911 static void __take_cap_refs(struct ceph_inode_info *ci, int got)
1913 if (got & CEPH_CAP_PIN)
1915 if (got & CEPH_CAP_FILE_RD)
1917 if (got & CEPH_CAP_FILE_CACHE)
1918 ci->i_rdcache_ref++;
1919 if (got & CEPH_CAP_FILE_WR)
1921 if (got & CEPH_CAP_FILE_BUFFER) {
1922 if (ci->i_wrbuffer_ref == 0)
1923 igrab(&ci->vfs_inode);
1924 ci->i_wrbuffer_ref++;
1925 dout("__take_cap_refs %p wrbuffer %d -> %d (?)\n",
1926 &ci->vfs_inode, ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref);
1931 * Try to grab cap references. Specify those refs we @want, and the
1932 * minimal set we @need. Also include the larger offset we are writing
1933 * to (when applicable), and check against max_size here as well.
1934 * Note that caller is responsible for ensuring max_size increases are
1935 * requested from the MDS.
1937 static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
1938 int *got, loff_t endoff, int *check_max, int *err)
1940 struct inode *inode = &ci->vfs_inode;
1942 int have, implemented;
1945 dout("get_cap_refs %p need %s want %s\n", inode,
1946 ceph_cap_string(need), ceph_cap_string(want));
1947 spin_lock(&inode->i_lock);
1949 /* make sure file is actually open */
1950 file_wanted = __ceph_caps_file_wanted(ci);
1951 if ((file_wanted & need) == 0) {
1952 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
1953 ceph_cap_string(need), ceph_cap_string(file_wanted));
1959 if (need & CEPH_CAP_FILE_WR) {
1960 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
1961 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
1962 inode, endoff, ci->i_max_size);
1963 if (endoff > ci->i_wanted_max_size) {
1970 * If a sync write is in progress, we must wait, so that we
1971 * can get a final snapshot value for size+mtime.
1973 if (__ceph_have_pending_cap_snap(ci)) {
1974 dout("get_cap_refs %p cap_snap_pending\n", inode);
1978 have = __ceph_caps_issued(ci, &implemented);
1981 * disallow writes while a truncate is pending
1983 if (ci->i_truncate_pending)
1984 have &= ~CEPH_CAP_FILE_WR;
1986 if ((have & need) == need) {
1988 * Look at (implemented & ~have & not) so that we keep waiting
1989 * on transition from wanted -> needed caps. This is needed
1990 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
1991 * going before a prior buffered writeback happens.
1993 int not = want & ~(have & need);
1994 int revoking = implemented & ~have;
1995 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
1996 inode, ceph_cap_string(have), ceph_cap_string(not),
1997 ceph_cap_string(revoking));
1998 if ((revoking & not) == 0) {
1999 *got = need | (have & want);
2000 __take_cap_refs(ci, *got);
2004 dout("get_cap_refs %p have %s needed %s\n", inode,
2005 ceph_cap_string(have), ceph_cap_string(need));
2008 spin_unlock(&inode->i_lock);
2009 dout("get_cap_refs %p ret %d got %s\n", inode,
2010 ret, ceph_cap_string(*got));
2015 * Check the offset we are writing up to against our current
2016 * max_size. If necessary, tell the MDS we want to write to
2019 static void check_max_size(struct inode *inode, loff_t endoff)
2021 struct ceph_inode_info *ci = ceph_inode(inode);
2024 /* do we need to explicitly request a larger max_size? */
2025 spin_lock(&inode->i_lock);
2026 if ((endoff >= ci->i_max_size ||
2027 endoff > (inode->i_size << 1)) &&
2028 endoff > ci->i_wanted_max_size) {
2029 dout("write %p at large endoff %llu, req max_size\n",
2031 ci->i_wanted_max_size = endoff;
2034 spin_unlock(&inode->i_lock);
2036 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2040 * Wait for caps, and take cap references. If we can't get a WR cap
2041 * due to a small max_size, make sure we check_max_size (and possibly
2042 * ask the mds) so we don't get hung up indefinitely.
2044 int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got,
2047 int check_max, ret, err;
2051 check_max_size(&ci->vfs_inode, endoff);
2054 ret = wait_event_interruptible(ci->i_cap_wq,
2055 try_get_cap_refs(ci, need, want,
2066 * Take cap refs. Caller must already know we hold at least one ref
2067 * on the caps in question or we don't know this is safe.
2069 void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2071 spin_lock(&ci->vfs_inode.i_lock);
2072 __take_cap_refs(ci, caps);
2073 spin_unlock(&ci->vfs_inode.i_lock);
2079 * If we released the last ref on any given cap, call ceph_check_caps
2080 * to release (or schedule a release).
2082 * If we are releasing a WR cap (from a sync write), finalize any affected
2083 * cap_snap, and wake up any waiters.
2085 void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2087 struct inode *inode = &ci->vfs_inode;
2088 int last = 0, put = 0, flushsnaps = 0, wake = 0;
2089 struct ceph_cap_snap *capsnap;
2091 spin_lock(&inode->i_lock);
2092 if (had & CEPH_CAP_PIN)
2094 if (had & CEPH_CAP_FILE_RD)
2095 if (--ci->i_rd_ref == 0)
2097 if (had & CEPH_CAP_FILE_CACHE)
2098 if (--ci->i_rdcache_ref == 0)
2100 if (had & CEPH_CAP_FILE_BUFFER) {
2101 if (--ci->i_wrbuffer_ref == 0) {
2105 dout("put_cap_refs %p wrbuffer %d -> %d (?)\n",
2106 inode, ci->i_wrbuffer_ref+1, ci->i_wrbuffer_ref);
2108 if (had & CEPH_CAP_FILE_WR)
2109 if (--ci->i_wr_ref == 0) {
2111 if (!list_empty(&ci->i_cap_snaps)) {
2112 capsnap = list_first_entry(&ci->i_cap_snaps,
2113 struct ceph_cap_snap,
2115 if (capsnap->writing) {
2116 capsnap->writing = 0;
2118 __ceph_finish_cap_snap(ci,
2124 spin_unlock(&inode->i_lock);
2126 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2127 last ? " last" : "", put ? " put" : "");
2129 if (last && !flushsnaps)
2130 ceph_check_caps(ci, 0, NULL);
2131 else if (flushsnaps)
2132 ceph_flush_snaps(ci);
2134 wake_up(&ci->i_cap_wq);
2140 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2141 * context. Adjust per-snap dirty page accounting as appropriate.
2142 * Once all dirty data for a cap_snap is flushed, flush snapped file
2143 * metadata back to the MDS. If we dropped the last ref, call
2146 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2147 struct ceph_snap_context *snapc)
2149 struct inode *inode = &ci->vfs_inode;
2151 int complete_capsnap = 0;
2152 int drop_capsnap = 0;
2154 struct ceph_cap_snap *capsnap = NULL;
2156 spin_lock(&inode->i_lock);
2157 ci->i_wrbuffer_ref -= nr;
2158 last = !ci->i_wrbuffer_ref;
2160 if (ci->i_head_snapc == snapc) {
2161 ci->i_wrbuffer_ref_head -= nr;
2162 if (!ci->i_wrbuffer_ref_head) {
2163 ceph_put_snap_context(ci->i_head_snapc);
2164 ci->i_head_snapc = NULL;
2166 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2168 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2169 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2170 last ? " LAST" : "");
2172 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2173 if (capsnap->context == snapc) {
2179 capsnap->dirty_pages -= nr;
2180 if (capsnap->dirty_pages == 0) {
2181 complete_capsnap = 1;
2182 if (capsnap->dirty == 0)
2183 /* cap writeback completed before we created
2184 * the cap_snap; no FLUSHSNAP is needed */
2187 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2188 " snap %lld %d/%d -> %d/%d %s%s%s\n",
2189 inode, capsnap, capsnap->context->seq,
2190 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2191 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2192 last ? " (wrbuffer last)" : "",
2193 complete_capsnap ? " (complete capsnap)" : "",
2194 drop_capsnap ? " (drop capsnap)" : "");
2196 ceph_put_snap_context(capsnap->context);
2197 list_del(&capsnap->ci_item);
2198 list_del(&capsnap->flushing_item);
2199 ceph_put_cap_snap(capsnap);
2203 spin_unlock(&inode->i_lock);
2206 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2208 } else if (complete_capsnap) {
2209 ceph_flush_snaps(ci);
2210 wake_up(&ci->i_cap_wq);
2217 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2218 * actually be a revocation if it specifies a smaller cap set.)
2220 * caller holds s_mutex and i_lock, we drop both.
2224 * 1 - check_caps on auth cap only (writeback)
2225 * 2 - check_caps (ack revoke)
2227 static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant,
2228 struct ceph_mds_session *session,
2229 struct ceph_cap *cap,
2230 struct ceph_buffer *xattr_buf)
2231 __releases(inode->i_lock)
2232 __releases(session->s_mutex)
2234 struct ceph_inode_info *ci = ceph_inode(inode);
2235 int mds = session->s_mds;
2236 int seq = le32_to_cpu(grant->seq);
2237 int newcaps = le32_to_cpu(grant->caps);
2238 int issued, implemented, used, wanted, dirty;
2239 u64 size = le64_to_cpu(grant->size);
2240 u64 max_size = le64_to_cpu(grant->max_size);
2241 struct timespec mtime, atime, ctime;
2245 int revoked_rdcache = 0;
2246 int queue_invalidate = 0;
2248 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2249 inode, cap, mds, seq, ceph_cap_string(newcaps));
2250 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
2254 * If CACHE is being revoked, and we have no dirty buffers,
2255 * try to invalidate (once). (If there are dirty buffers, we
2256 * will invalidate _after_ writeback.)
2258 if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
2259 !ci->i_wrbuffer_ref) {
2260 if (try_nonblocking_invalidate(inode) == 0) {
2261 revoked_rdcache = 1;
2263 /* there were locked pages.. invalidate later
2264 in a separate thread. */
2265 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
2266 queue_invalidate = 1;
2267 ci->i_rdcache_revoking = ci->i_rdcache_gen;
2272 /* side effects now are allowed */
2274 issued = __ceph_caps_issued(ci, &implemented);
2275 issued |= implemented | __ceph_caps_dirty(ci);
2277 cap->cap_gen = session->s_cap_gen;
2279 __check_cap_issue(ci, cap, newcaps);
2281 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
2282 inode->i_mode = le32_to_cpu(grant->mode);
2283 inode->i_uid = le32_to_cpu(grant->uid);
2284 inode->i_gid = le32_to_cpu(grant->gid);
2285 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
2286 inode->i_uid, inode->i_gid);
2289 if ((issued & CEPH_CAP_LINK_EXCL) == 0)
2290 inode->i_nlink = le32_to_cpu(grant->nlink);
2292 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
2293 int len = le32_to_cpu(grant->xattr_len);
2294 u64 version = le64_to_cpu(grant->xattr_version);
2296 if (version > ci->i_xattrs.version) {
2297 dout(" got new xattrs v%llu on %p len %d\n",
2298 version, inode, len);
2299 if (ci->i_xattrs.blob)
2300 ceph_buffer_put(ci->i_xattrs.blob);
2301 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
2302 ci->i_xattrs.version = version;
2306 /* size/ctime/mtime/atime? */
2307 ceph_fill_file_size(inode, issued,
2308 le32_to_cpu(grant->truncate_seq),
2309 le64_to_cpu(grant->truncate_size), size);
2310 ceph_decode_timespec(&mtime, &grant->mtime);
2311 ceph_decode_timespec(&atime, &grant->atime);
2312 ceph_decode_timespec(&ctime, &grant->ctime);
2313 ceph_fill_file_time(inode, issued,
2314 le32_to_cpu(grant->time_warp_seq), &ctime, &mtime,
2317 /* max size increase? */
2318 if (max_size != ci->i_max_size) {
2319 dout("max_size %lld -> %llu\n", ci->i_max_size, max_size);
2320 ci->i_max_size = max_size;
2321 if (max_size >= ci->i_wanted_max_size) {
2322 ci->i_wanted_max_size = 0; /* reset */
2323 ci->i_requested_max_size = 0;
2328 /* check cap bits */
2329 wanted = __ceph_caps_wanted(ci);
2330 used = __ceph_caps_used(ci);
2331 dirty = __ceph_caps_dirty(ci);
2332 dout(" my wanted = %s, used = %s, dirty %s\n",
2333 ceph_cap_string(wanted),
2334 ceph_cap_string(used),
2335 ceph_cap_string(dirty));
2336 if (wanted != le32_to_cpu(grant->wanted)) {
2337 dout("mds wanted %s -> %s\n",
2338 ceph_cap_string(le32_to_cpu(grant->wanted)),
2339 ceph_cap_string(wanted));
2340 grant->wanted = cpu_to_le32(wanted);
2345 /* file layout may have changed */
2346 ci->i_layout = grant->layout;
2348 /* revocation, grant, or no-op? */
2349 if (cap->issued & ~newcaps) {
2350 dout("revocation: %s -> %s\n", ceph_cap_string(cap->issued),
2351 ceph_cap_string(newcaps));
2352 if ((used & ~newcaps) & CEPH_CAP_FILE_BUFFER)
2353 writeback = 1; /* will delay ack */
2354 else if (dirty & ~newcaps)
2355 check_caps = 1; /* initiate writeback in check_caps */
2356 else if (((used & ~newcaps) & CEPH_CAP_FILE_CACHE) == 0 ||
2358 check_caps = 2; /* send revoke ack in check_caps */
2359 cap->issued = newcaps;
2360 cap->implemented |= newcaps;
2361 } else if (cap->issued == newcaps) {
2362 dout("caps unchanged: %s -> %s\n",
2363 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
2365 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
2366 ceph_cap_string(newcaps));
2367 cap->issued = newcaps;
2368 cap->implemented |= newcaps; /* add bits only, to
2369 * avoid stepping on a
2370 * pending revocation */
2373 BUG_ON(cap->issued & ~cap->implemented);
2375 spin_unlock(&inode->i_lock);
2378 * queue inode for writeback: we can't actually call
2379 * filemap_write_and_wait, etc. from message handler
2382 ceph_queue_writeback(inode);
2383 if (queue_invalidate)
2384 ceph_queue_invalidate(inode);
2386 wake_up(&ci->i_cap_wq);
2388 if (check_caps == 1)
2389 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
2391 else if (check_caps == 2)
2392 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
2394 mutex_unlock(&session->s_mutex);
2398 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2399 * MDS has been safely committed.
2401 static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
2402 struct ceph_mds_caps *m,
2403 struct ceph_mds_session *session,
2404 struct ceph_cap *cap)
2405 __releases(inode->i_lock)
2407 struct ceph_inode_info *ci = ceph_inode(inode);
2408 struct ceph_mds_client *mdsc = &ceph_client(inode->i_sb)->mdsc;
2409 unsigned seq = le32_to_cpu(m->seq);
2410 int dirty = le32_to_cpu(m->dirty);
2415 for (i = 0; i < CEPH_CAP_BITS; i++)
2416 if ((dirty & (1 << i)) &&
2417 flush_tid == ci->i_cap_flush_tid[i])
2420 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2421 " flushing %s -> %s\n",
2422 inode, session->s_mds, seq, ceph_cap_string(dirty),
2423 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
2424 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
2426 if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned))
2429 ci->i_flushing_caps &= ~cleaned;
2431 spin_lock(&mdsc->cap_dirty_lock);
2432 if (ci->i_flushing_caps == 0) {
2433 list_del_init(&ci->i_flushing_item);
2434 if (!list_empty(&session->s_cap_flushing))
2435 dout(" mds%d still flushing cap on %p\n",
2437 &list_entry(session->s_cap_flushing.next,
2438 struct ceph_inode_info,
2439 i_flushing_item)->vfs_inode);
2440 mdsc->num_cap_flushing--;
2441 wake_up(&mdsc->cap_flushing_wq);
2442 dout(" inode %p now !flushing\n", inode);
2444 if (ci->i_dirty_caps == 0) {
2445 dout(" inode %p now clean\n", inode);
2446 BUG_ON(!list_empty(&ci->i_dirty_item));
2449 BUG_ON(list_empty(&ci->i_dirty_item));
2452 spin_unlock(&mdsc->cap_dirty_lock);
2453 wake_up(&ci->i_cap_wq);
2456 spin_unlock(&inode->i_lock);
2462 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
2463 * throw away our cap_snap.
2465 * Caller hold s_mutex.
2467 static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
2468 struct ceph_mds_caps *m,
2469 struct ceph_mds_session *session)
2471 struct ceph_inode_info *ci = ceph_inode(inode);
2472 u64 follows = le64_to_cpu(m->snap_follows);
2473 struct ceph_cap_snap *capsnap;
2476 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2477 inode, ci, session->s_mds, follows);
2479 spin_lock(&inode->i_lock);
2480 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2481 if (capsnap->follows == follows) {
2482 if (capsnap->flush_tid != flush_tid) {
2483 dout(" cap_snap %p follows %lld tid %lld !="
2484 " %lld\n", capsnap, follows,
2485 flush_tid, capsnap->flush_tid);
2488 WARN_ON(capsnap->dirty_pages || capsnap->writing);
2489 dout(" removing %p cap_snap %p follows %lld\n",
2490 inode, capsnap, follows);
2491 ceph_put_snap_context(capsnap->context);
2492 list_del(&capsnap->ci_item);
2493 list_del(&capsnap->flushing_item);
2494 ceph_put_cap_snap(capsnap);
2498 dout(" skipping cap_snap %p follows %lld\n",
2499 capsnap, capsnap->follows);
2502 spin_unlock(&inode->i_lock);
2508 * Handle TRUNC from MDS, indicating file truncation.
2510 * caller hold s_mutex.
2512 static void handle_cap_trunc(struct inode *inode,
2513 struct ceph_mds_caps *trunc,
2514 struct ceph_mds_session *session)
2515 __releases(inode->i_lock)
2517 struct ceph_inode_info *ci = ceph_inode(inode);
2518 int mds = session->s_mds;
2519 int seq = le32_to_cpu(trunc->seq);
2520 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
2521 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
2522 u64 size = le64_to_cpu(trunc->size);
2523 int implemented = 0;
2524 int dirty = __ceph_caps_dirty(ci);
2525 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
2526 int queue_trunc = 0;
2528 issued |= implemented | dirty;
2530 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2531 inode, mds, seq, truncate_size, truncate_seq);
2532 queue_trunc = ceph_fill_file_size(inode, issued,
2533 truncate_seq, truncate_size, size);
2534 spin_unlock(&inode->i_lock);
2537 ceph_queue_vmtruncate(inode);
2541 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
2542 * different one. If we are the most recent migration we've seen (as
2543 * indicated by mseq), make note of the migrating cap bits for the
2544 * duration (until we see the corresponding IMPORT).
2546 * caller holds s_mutex
2548 static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
2549 struct ceph_mds_session *session)
2551 struct ceph_inode_info *ci = ceph_inode(inode);
2552 int mds = session->s_mds;
2553 unsigned mseq = le32_to_cpu(ex->migrate_seq);
2554 struct ceph_cap *cap = NULL, *t;
2558 dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
2559 inode, ci, mds, mseq);
2561 spin_lock(&inode->i_lock);
2563 /* make sure we haven't seen a higher mseq */
2564 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
2565 t = rb_entry(p, struct ceph_cap, ci_node);
2566 if (ceph_seq_cmp(t->mseq, mseq) > 0) {
2567 dout(" higher mseq on cap from mds%d\n",
2571 if (t->session->s_mds == mds)
2578 ci->i_cap_exporting_mds = mds;
2579 ci->i_cap_exporting_mseq = mseq;
2580 ci->i_cap_exporting_issued = cap->issued;
2582 __ceph_remove_cap(cap);
2584 /* else, we already released it */
2586 spin_unlock(&inode->i_lock);
2590 * Handle cap IMPORT. If there are temp bits from an older EXPORT,
2593 * caller holds s_mutex.
2595 static void handle_cap_import(struct ceph_mds_client *mdsc,
2596 struct inode *inode, struct ceph_mds_caps *im,
2597 struct ceph_mds_session *session,
2598 void *snaptrace, int snaptrace_len)
2600 struct ceph_inode_info *ci = ceph_inode(inode);
2601 int mds = session->s_mds;
2602 unsigned issued = le32_to_cpu(im->caps);
2603 unsigned wanted = le32_to_cpu(im->wanted);
2604 unsigned seq = le32_to_cpu(im->seq);
2605 unsigned mseq = le32_to_cpu(im->migrate_seq);
2606 u64 realmino = le64_to_cpu(im->realm);
2607 u64 cap_id = le64_to_cpu(im->cap_id);
2609 if (ci->i_cap_exporting_mds >= 0 &&
2610 ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) {
2611 dout("handle_cap_import inode %p ci %p mds%d mseq %d"
2612 " - cleared exporting from mds%d\n",
2613 inode, ci, mds, mseq,
2614 ci->i_cap_exporting_mds);
2615 ci->i_cap_exporting_issued = 0;
2616 ci->i_cap_exporting_mseq = 0;
2617 ci->i_cap_exporting_mds = -1;
2619 dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
2620 inode, ci, mds, mseq);
2623 down_write(&mdsc->snap_rwsem);
2624 ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len,
2626 downgrade_write(&mdsc->snap_rwsem);
2627 ceph_add_cap(inode, session, cap_id, -1,
2628 issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH,
2629 NULL /* no caps context */);
2630 try_flush_caps(inode, session, NULL);
2631 up_read(&mdsc->snap_rwsem);
2635 * Handle a caps message from the MDS.
2637 * Identify the appropriate session, inode, and call the right handler
2638 * based on the cap op.
2640 void ceph_handle_caps(struct ceph_mds_session *session,
2641 struct ceph_msg *msg)
2643 struct ceph_mds_client *mdsc = session->s_mdsc;
2644 struct super_block *sb = mdsc->client->sb;
2645 struct inode *inode;
2646 struct ceph_cap *cap;
2647 struct ceph_mds_caps *h;
2648 int mds = session->s_mds;
2651 struct ceph_vino vino;
2657 dout("handle_caps from mds%d\n", mds);
2660 tid = le64_to_cpu(msg->hdr.tid);
2661 if (msg->front.iov_len < sizeof(*h))
2663 h = msg->front.iov_base;
2665 op = le32_to_cpu(h->op);
2666 vino.ino = le64_to_cpu(h->ino);
2667 vino.snap = CEPH_NOSNAP;
2668 cap_id = le64_to_cpu(h->cap_id);
2669 seq = le32_to_cpu(h->seq);
2670 size = le64_to_cpu(h->size);
2671 max_size = le64_to_cpu(h->max_size);
2673 mutex_lock(&session->s_mutex);
2675 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
2679 inode = ceph_find_inode(sb, vino);
2680 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
2683 dout(" i don't have ino %llx\n", vino.ino);
2687 /* these will work even if we don't have a cap yet */
2689 case CEPH_CAP_OP_FLUSHSNAP_ACK:
2690 handle_cap_flushsnap_ack(inode, tid, h, session);
2693 case CEPH_CAP_OP_EXPORT:
2694 handle_cap_export(inode, h, session);
2697 case CEPH_CAP_OP_IMPORT:
2698 handle_cap_import(mdsc, inode, h, session,
2699 snaptrace, le32_to_cpu(h->snap_trace_len));
2700 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_NODELAY,
2705 /* the rest require a cap */
2706 spin_lock(&inode->i_lock);
2707 cap = __get_cap_for_mds(ceph_inode(inode), mds);
2709 dout("no cap on %p ino %llx.%llx from mds%d, releasing\n",
2710 inode, ceph_ino(inode), ceph_snap(inode), mds);
2711 spin_unlock(&inode->i_lock);
2715 /* note that each of these drops i_lock for us */
2717 case CEPH_CAP_OP_REVOKE:
2718 case CEPH_CAP_OP_GRANT:
2719 handle_cap_grant(inode, h, session, cap, msg->middle);
2722 case CEPH_CAP_OP_FLUSH_ACK:
2723 handle_cap_flush_ack(inode, tid, h, session, cap);
2726 case CEPH_CAP_OP_TRUNC:
2727 handle_cap_trunc(inode, h, session);
2731 spin_unlock(&inode->i_lock);
2732 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
2733 ceph_cap_op_name(op));
2737 mutex_unlock(&session->s_mutex);
2744 pr_err("ceph_handle_caps: corrupt message\n");
2750 * Delayed work handler to process end of delayed cap release LRU list.
2752 void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
2754 struct ceph_inode_info *ci;
2755 int flags = CHECK_CAPS_NODELAY;
2757 dout("check_delayed_caps\n");
2759 spin_lock(&mdsc->cap_delay_lock);
2760 if (list_empty(&mdsc->cap_delay_list))
2762 ci = list_first_entry(&mdsc->cap_delay_list,
2763 struct ceph_inode_info,
2765 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
2766 time_before(jiffies, ci->i_hold_caps_max))
2768 list_del_init(&ci->i_cap_delay_list);
2769 spin_unlock(&mdsc->cap_delay_lock);
2770 dout("check_delayed_caps on %p\n", &ci->vfs_inode);
2771 ceph_check_caps(ci, flags, NULL);
2773 spin_unlock(&mdsc->cap_delay_lock);
2777 * Flush all dirty caps to the mds
2779 void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
2781 struct ceph_inode_info *ci, *nci = NULL;
2782 struct inode *inode, *ninode = NULL;
2783 struct list_head *p, *n;
2785 dout("flush_dirty_caps\n");
2786 spin_lock(&mdsc->cap_dirty_lock);
2787 list_for_each_safe(p, n, &mdsc->cap_dirty) {
2791 ci->i_ceph_flags &= ~CEPH_I_NOFLUSH;
2792 dout("flush_dirty_caps inode %p (was next inode)\n",
2795 ci = list_entry(p, struct ceph_inode_info,
2797 inode = igrab(&ci->vfs_inode);
2799 dout("flush_dirty_caps inode %p\n", inode);
2801 if (n != &mdsc->cap_dirty) {
2802 nci = list_entry(n, struct ceph_inode_info,
2804 ninode = igrab(&nci->vfs_inode);
2806 nci->i_ceph_flags |= CEPH_I_NOFLUSH;
2807 dout("flush_dirty_caps next inode %p, noflush\n",
2813 spin_unlock(&mdsc->cap_dirty_lock);
2815 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH,
2819 spin_lock(&mdsc->cap_dirty_lock);
2821 spin_unlock(&mdsc->cap_dirty_lock);
2825 * Drop open file reference. If we were the last open file,
2826 * we may need to release capabilities to the MDS (or schedule
2827 * their delayed release).
2829 void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
2831 struct inode *inode = &ci->vfs_inode;
2834 spin_lock(&inode->i_lock);
2835 dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode,
2836 ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1);
2837 BUG_ON(ci->i_nr_by_mode[fmode] == 0);
2838 if (--ci->i_nr_by_mode[fmode] == 0)
2840 spin_unlock(&inode->i_lock);
2842 if (last && ci->i_vino.snap == CEPH_NOSNAP)
2843 ceph_check_caps(ci, 0, NULL);
2847 * Helpers for embedding cap and dentry lease releases into mds
2850 * @force is used by dentry_release (below) to force inclusion of a
2851 * record for the directory inode, even when there aren't any caps to
2854 int ceph_encode_inode_release(void **p, struct inode *inode,
2855 int mds, int drop, int unless, int force)
2857 struct ceph_inode_info *ci = ceph_inode(inode);
2858 struct ceph_cap *cap;
2859 struct ceph_mds_request_release *rel = *p;
2863 spin_lock(&inode->i_lock);
2864 used = __ceph_caps_used(ci);
2866 dout("encode_inode_release %p mds%d used %s drop %s unless %s\n", inode,
2867 mds, ceph_cap_string(used), ceph_cap_string(drop),
2868 ceph_cap_string(unless));
2870 /* only drop unused caps */
2873 cap = __get_cap_for_mds(ci, mds);
2874 if (cap && __cap_is_valid(cap)) {
2876 ((cap->issued & drop) &&
2877 (cap->issued & unless) == 0)) {
2878 if ((cap->issued & drop) &&
2879 (cap->issued & unless) == 0) {
2880 dout("encode_inode_release %p cap %p %s -> "
2882 ceph_cap_string(cap->issued),
2883 ceph_cap_string(cap->issued & ~drop));
2884 cap->issued &= ~drop;
2885 cap->implemented &= ~drop;
2886 if (ci->i_ceph_flags & CEPH_I_NODELAY) {
2887 int wanted = __ceph_caps_wanted(ci);
2888 dout(" wanted %s -> %s (act %s)\n",
2889 ceph_cap_string(cap->mds_wanted),
2890 ceph_cap_string(cap->mds_wanted &
2892 ceph_cap_string(wanted));
2893 cap->mds_wanted &= wanted;
2896 dout("encode_inode_release %p cap %p %s"
2897 " (force)\n", inode, cap,
2898 ceph_cap_string(cap->issued));
2901 rel->ino = cpu_to_le64(ceph_ino(inode));
2902 rel->cap_id = cpu_to_le64(cap->cap_id);
2903 rel->seq = cpu_to_le32(cap->seq);
2904 rel->issue_seq = cpu_to_le32(cap->issue_seq),
2905 rel->mseq = cpu_to_le32(cap->mseq);
2906 rel->caps = cpu_to_le32(cap->issued);
2907 rel->wanted = cpu_to_le32(cap->mds_wanted);
2913 dout("encode_inode_release %p cap %p %s\n",
2914 inode, cap, ceph_cap_string(cap->issued));
2917 spin_unlock(&inode->i_lock);
2921 int ceph_encode_dentry_release(void **p, struct dentry *dentry,
2922 int mds, int drop, int unless)
2924 struct inode *dir = dentry->d_parent->d_inode;
2925 struct ceph_mds_request_release *rel = *p;
2926 struct ceph_dentry_info *di = ceph_dentry(dentry);
2931 * force an record for the directory caps if we have a dentry lease.
2932 * this is racy (can't take i_lock and d_lock together), but it
2933 * doesn't have to be perfect; the mds will revoke anything we don't
2936 spin_lock(&dentry->d_lock);
2937 if (di->lease_session && di->lease_session->s_mds == mds)
2939 spin_unlock(&dentry->d_lock);
2941 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
2943 spin_lock(&dentry->d_lock);
2944 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
2945 dout("encode_dentry_release %p mds%d seq %d\n",
2946 dentry, mds, (int)di->lease_seq);
2947 rel->dname_len = cpu_to_le32(dentry->d_name.len);
2948 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
2949 *p += dentry->d_name.len;
2950 rel->dname_seq = cpu_to_le32(di->lease_seq);
2952 spin_unlock(&dentry->d_lock);