1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
5 #include <linux/kernel.h>
6 #include <linux/sched/signal.h>
7 #include <linux/slab.h>
8 #include <linux/vmalloc.h>
9 #include <linux/wait.h>
10 #include <linux/writeback.h>
13 #include "mds_client.h"
15 #include <linux/ceph/decode.h>
16 #include <linux/ceph/messenger.h>
19 * Capability management
21 * The Ceph metadata servers control client access to inode metadata
22 * and file data by issuing capabilities, granting clients permission
23 * to read and/or write both inode field and file data to OSDs
24 * (storage nodes). Each capability consists of a set of bits
25 * indicating which operations are allowed.
27 * If the client holds a *_SHARED cap, the client has a coherent value
28 * that can be safely read from the cached inode.
30 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
31 * client is allowed to change inode attributes (e.g., file size,
32 * mtime), note its dirty state in the ceph_cap, and asynchronously
33 * flush that metadata change to the MDS.
35 * In the event of a conflicting operation (perhaps by another
36 * client), the MDS will revoke the conflicting client capabilities.
38 * In order for a client to cache an inode, it must hold a capability
39 * with at least one MDS server. When inodes are released, release
40 * notifications are batched and periodically sent en masse to the MDS
41 * cluster to release server state.
44 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc);
45 static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
46 struct ceph_mds_session *session,
47 struct ceph_inode_info *ci,
48 u64 oldest_flush_tid);
51 * Generate readable cap strings for debugging output.
53 #define MAX_CAP_STR 20
54 static char cap_str[MAX_CAP_STR][40];
55 static DEFINE_SPINLOCK(cap_str_lock);
56 static int last_cap_str;
58 static char *gcap_string(char *s, int c)
60 if (c & CEPH_CAP_GSHARED)
62 if (c & CEPH_CAP_GEXCL)
64 if (c & CEPH_CAP_GCACHE)
70 if (c & CEPH_CAP_GBUFFER)
72 if (c & CEPH_CAP_GLAZYIO)
77 const char *ceph_cap_string(int caps)
83 spin_lock(&cap_str_lock);
85 if (last_cap_str == MAX_CAP_STR)
87 spin_unlock(&cap_str_lock);
91 if (caps & CEPH_CAP_PIN)
94 c = (caps >> CEPH_CAP_SAUTH) & 3;
97 s = gcap_string(s, c);
100 c = (caps >> CEPH_CAP_SLINK) & 3;
103 s = gcap_string(s, c);
106 c = (caps >> CEPH_CAP_SXATTR) & 3;
109 s = gcap_string(s, c);
112 c = caps >> CEPH_CAP_SFILE;
115 s = gcap_string(s, c);
124 void ceph_caps_init(struct ceph_mds_client *mdsc)
126 INIT_LIST_HEAD(&mdsc->caps_list);
127 spin_lock_init(&mdsc->caps_list_lock);
130 void ceph_caps_finalize(struct ceph_mds_client *mdsc)
132 struct ceph_cap *cap;
134 spin_lock(&mdsc->caps_list_lock);
135 while (!list_empty(&mdsc->caps_list)) {
136 cap = list_first_entry(&mdsc->caps_list,
137 struct ceph_cap, caps_item);
138 list_del(&cap->caps_item);
139 kmem_cache_free(ceph_cap_cachep, cap);
141 mdsc->caps_total_count = 0;
142 mdsc->caps_avail_count = 0;
143 mdsc->caps_use_count = 0;
144 mdsc->caps_reserve_count = 0;
145 mdsc->caps_min_count = 0;
146 spin_unlock(&mdsc->caps_list_lock);
149 void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta)
151 spin_lock(&mdsc->caps_list_lock);
152 mdsc->caps_min_count += delta;
153 BUG_ON(mdsc->caps_min_count < 0);
154 spin_unlock(&mdsc->caps_list_lock);
157 void ceph_reserve_caps(struct ceph_mds_client *mdsc,
158 struct ceph_cap_reservation *ctx, int need)
161 struct ceph_cap *cap;
166 dout("reserve caps ctx=%p need=%d\n", ctx, need);
168 /* first reserve any caps that are already allocated */
169 spin_lock(&mdsc->caps_list_lock);
170 if (mdsc->caps_avail_count >= need)
173 have = mdsc->caps_avail_count;
174 mdsc->caps_avail_count -= have;
175 mdsc->caps_reserve_count += have;
176 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
177 mdsc->caps_reserve_count +
178 mdsc->caps_avail_count);
179 spin_unlock(&mdsc->caps_list_lock);
181 for (i = have; i < need; i++) {
182 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
185 list_add(&cap->caps_item, &newcaps);
188 /* we didn't manage to reserve as much as we needed */
189 if (have + alloc != need)
190 pr_warn("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
191 ctx, need, have + alloc);
193 spin_lock(&mdsc->caps_list_lock);
194 mdsc->caps_total_count += alloc;
195 mdsc->caps_reserve_count += alloc;
196 list_splice(&newcaps, &mdsc->caps_list);
198 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
199 mdsc->caps_reserve_count +
200 mdsc->caps_avail_count);
201 spin_unlock(&mdsc->caps_list_lock);
204 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
205 ctx, mdsc->caps_total_count, mdsc->caps_use_count,
206 mdsc->caps_reserve_count, mdsc->caps_avail_count);
209 int ceph_unreserve_caps(struct ceph_mds_client *mdsc,
210 struct ceph_cap_reservation *ctx)
212 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
214 spin_lock(&mdsc->caps_list_lock);
215 BUG_ON(mdsc->caps_reserve_count < ctx->count);
216 mdsc->caps_reserve_count -= ctx->count;
217 mdsc->caps_avail_count += ctx->count;
219 dout("unreserve caps %d = %d used + %d resv + %d avail\n",
220 mdsc->caps_total_count, mdsc->caps_use_count,
221 mdsc->caps_reserve_count, mdsc->caps_avail_count);
222 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
223 mdsc->caps_reserve_count +
224 mdsc->caps_avail_count);
225 spin_unlock(&mdsc->caps_list_lock);
230 struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc,
231 struct ceph_cap_reservation *ctx)
233 struct ceph_cap *cap = NULL;
235 /* temporary, until we do something about cap import/export */
237 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
239 spin_lock(&mdsc->caps_list_lock);
240 mdsc->caps_use_count++;
241 mdsc->caps_total_count++;
242 spin_unlock(&mdsc->caps_list_lock);
247 spin_lock(&mdsc->caps_list_lock);
248 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
249 ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
250 mdsc->caps_reserve_count, mdsc->caps_avail_count);
252 BUG_ON(ctx->count > mdsc->caps_reserve_count);
253 BUG_ON(list_empty(&mdsc->caps_list));
256 mdsc->caps_reserve_count--;
257 mdsc->caps_use_count++;
259 cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
260 list_del(&cap->caps_item);
262 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
263 mdsc->caps_reserve_count + mdsc->caps_avail_count);
264 spin_unlock(&mdsc->caps_list_lock);
268 void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
270 spin_lock(&mdsc->caps_list_lock);
271 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
272 cap, mdsc->caps_total_count, mdsc->caps_use_count,
273 mdsc->caps_reserve_count, mdsc->caps_avail_count);
274 mdsc->caps_use_count--;
276 * Keep some preallocated caps around (ceph_min_count), to
277 * avoid lots of free/alloc churn.
279 if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
280 mdsc->caps_min_count) {
281 mdsc->caps_total_count--;
282 kmem_cache_free(ceph_cap_cachep, cap);
284 mdsc->caps_avail_count++;
285 list_add(&cap->caps_item, &mdsc->caps_list);
288 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
289 mdsc->caps_reserve_count + mdsc->caps_avail_count);
290 spin_unlock(&mdsc->caps_list_lock);
293 void ceph_reservation_status(struct ceph_fs_client *fsc,
294 int *total, int *avail, int *used, int *reserved,
297 struct ceph_mds_client *mdsc = fsc->mdsc;
300 *total = mdsc->caps_total_count;
302 *avail = mdsc->caps_avail_count;
304 *used = mdsc->caps_use_count;
306 *reserved = mdsc->caps_reserve_count;
308 *min = mdsc->caps_min_count;
312 * Find ceph_cap for given mds, if any.
314 * Called with i_ceph_lock held.
316 static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
318 struct ceph_cap *cap;
319 struct rb_node *n = ci->i_caps.rb_node;
322 cap = rb_entry(n, struct ceph_cap, ci_node);
325 else if (mds > cap->mds)
333 struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
335 struct ceph_cap *cap;
337 spin_lock(&ci->i_ceph_lock);
338 cap = __get_cap_for_mds(ci, mds);
339 spin_unlock(&ci->i_ceph_lock);
344 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
346 static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
348 struct ceph_cap *cap;
352 /* prefer mds with WR|BUFFER|EXCL caps */
353 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
354 cap = rb_entry(p, struct ceph_cap, ci_node);
356 if (cap->issued & (CEPH_CAP_FILE_WR |
357 CEPH_CAP_FILE_BUFFER |
364 int ceph_get_cap_mds(struct inode *inode)
366 struct ceph_inode_info *ci = ceph_inode(inode);
368 spin_lock(&ci->i_ceph_lock);
369 mds = __ceph_get_cap_mds(ceph_inode(inode));
370 spin_unlock(&ci->i_ceph_lock);
375 * Called under i_ceph_lock.
377 static void __insert_cap_node(struct ceph_inode_info *ci,
378 struct ceph_cap *new)
380 struct rb_node **p = &ci->i_caps.rb_node;
381 struct rb_node *parent = NULL;
382 struct ceph_cap *cap = NULL;
386 cap = rb_entry(parent, struct ceph_cap, ci_node);
387 if (new->mds < cap->mds)
389 else if (new->mds > cap->mds)
395 rb_link_node(&new->ci_node, parent, p);
396 rb_insert_color(&new->ci_node, &ci->i_caps);
400 * (re)set cap hold timeouts, which control the delayed release
401 * of unused caps back to the MDS. Should be called on cap use.
403 static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
404 struct ceph_inode_info *ci)
406 struct ceph_mount_options *ma = mdsc->fsc->mount_options;
408 ci->i_hold_caps_min = round_jiffies(jiffies +
409 ma->caps_wanted_delay_min * HZ);
410 ci->i_hold_caps_max = round_jiffies(jiffies +
411 ma->caps_wanted_delay_max * HZ);
412 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
413 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
417 * (Re)queue cap at the end of the delayed cap release list.
419 * If I_FLUSH is set, leave the inode at the front of the list.
421 * Caller holds i_ceph_lock
422 * -> we take mdsc->cap_delay_lock
424 static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
425 struct ceph_inode_info *ci)
427 __cap_set_timeouts(mdsc, ci);
428 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
429 ci->i_ceph_flags, ci->i_hold_caps_max);
430 if (!mdsc->stopping) {
431 spin_lock(&mdsc->cap_delay_lock);
432 if (!list_empty(&ci->i_cap_delay_list)) {
433 if (ci->i_ceph_flags & CEPH_I_FLUSH)
435 list_del_init(&ci->i_cap_delay_list);
437 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
439 spin_unlock(&mdsc->cap_delay_lock);
444 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
445 * indicating we should send a cap message to flush dirty metadata
446 * asap, and move to the front of the delayed cap list.
448 static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
449 struct ceph_inode_info *ci)
451 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
452 spin_lock(&mdsc->cap_delay_lock);
453 ci->i_ceph_flags |= CEPH_I_FLUSH;
454 if (!list_empty(&ci->i_cap_delay_list))
455 list_del_init(&ci->i_cap_delay_list);
456 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
457 spin_unlock(&mdsc->cap_delay_lock);
461 * Cancel delayed work on cap.
463 * Caller must hold i_ceph_lock.
465 static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
466 struct ceph_inode_info *ci)
468 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
469 if (list_empty(&ci->i_cap_delay_list))
471 spin_lock(&mdsc->cap_delay_lock);
472 list_del_init(&ci->i_cap_delay_list);
473 spin_unlock(&mdsc->cap_delay_lock);
477 * Common issue checks for add_cap, handle_cap_grant.
479 static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
482 unsigned had = __ceph_caps_issued(ci, NULL);
485 * Each time we receive FILE_CACHE anew, we increment
488 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
489 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) {
494 * If FILE_SHARED is newly issued, mark dir not complete. We don't
495 * know what happened to this directory while we didn't have the cap.
496 * If FILE_SHARED is being revoked, also mark dir not complete. It
497 * stops on-going cached readdir.
499 if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) {
500 if (issued & CEPH_CAP_FILE_SHARED)
502 if (S_ISDIR(ci->vfs_inode.i_mode)) {
503 dout(" marking %p NOT complete\n", &ci->vfs_inode);
504 __ceph_dir_clear_complete(ci);
510 * Add a capability under the given MDS session.
512 * Caller should hold session snap_rwsem (read) and s_mutex.
514 * @fmode is the open file mode, if we are opening a file, otherwise
515 * it is < 0. (This is so we can atomically add the cap and add an
516 * open file reference to it.)
518 void ceph_add_cap(struct inode *inode,
519 struct ceph_mds_session *session, u64 cap_id,
520 int fmode, unsigned issued, unsigned wanted,
521 unsigned seq, unsigned mseq, u64 realmino, int flags,
522 struct ceph_cap **new_cap)
524 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
525 struct ceph_inode_info *ci = ceph_inode(inode);
526 struct ceph_cap *cap;
527 int mds = session->s_mds;
530 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
531 session->s_mds, cap_id, ceph_cap_string(issued), seq);
534 * If we are opening the file, include file mode wanted bits
538 wanted |= ceph_caps_for_mode(fmode);
540 cap = __get_cap_for_mds(ci, mds);
546 cap->implemented = 0;
552 __insert_cap_node(ci, cap);
554 /* add to session cap list */
555 cap->session = session;
556 spin_lock(&session->s_cap_lock);
557 list_add_tail(&cap->session_caps, &session->s_caps);
558 session->s_nr_caps++;
559 spin_unlock(&session->s_cap_lock);
562 * auth mds of the inode changed. we received the cap export
563 * message, but still haven't received the cap import message.
564 * handle_cap_export() updated the new auth MDS' cap.
566 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
567 * a message that was send before the cap import message. So
570 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
571 WARN_ON(cap != ci->i_auth_cap);
572 WARN_ON(cap->cap_id != cap_id);
575 issued |= cap->issued;
576 flags |= CEPH_CAP_FLAG_AUTH;
580 if (!ci->i_snap_realm) {
582 * add this inode to the appropriate snap realm
584 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
587 spin_lock(&realm->inodes_with_caps_lock);
588 ci->i_snap_realm = realm;
589 list_add(&ci->i_snap_realm_item,
590 &realm->inodes_with_caps);
591 spin_unlock(&realm->inodes_with_caps_lock);
593 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
599 __check_cap_issue(ci, cap, issued);
602 * If we are issued caps we don't want, or the mds' wanted
603 * value appears to be off, queue a check so we'll release
604 * later and/or update the mds wanted value.
606 actual_wanted = __ceph_caps_wanted(ci);
607 if ((wanted & ~actual_wanted) ||
608 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
609 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
610 ceph_cap_string(issued), ceph_cap_string(wanted),
611 ceph_cap_string(actual_wanted));
612 __cap_delay_requeue(mdsc, ci);
615 if (flags & CEPH_CAP_FLAG_AUTH) {
616 if (!ci->i_auth_cap ||
617 ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
618 ci->i_auth_cap = cap;
619 cap->mds_wanted = wanted;
622 WARN_ON(ci->i_auth_cap == cap);
625 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
626 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
627 ceph_cap_string(issued|cap->issued), seq, mds);
628 cap->cap_id = cap_id;
629 cap->issued = issued;
630 cap->implemented |= issued;
631 if (ceph_seq_cmp(mseq, cap->mseq) > 0)
632 cap->mds_wanted = wanted;
634 cap->mds_wanted |= wanted;
636 cap->issue_seq = seq;
638 cap->cap_gen = session->s_cap_gen;
641 __ceph_get_fmode(ci, fmode);
645 * Return true if cap has not timed out and belongs to the current
646 * generation of the MDS session (i.e. has not gone 'stale' due to
647 * us losing touch with the mds).
649 static int __cap_is_valid(struct ceph_cap *cap)
654 spin_lock(&cap->session->s_gen_ttl_lock);
655 gen = cap->session->s_cap_gen;
656 ttl = cap->session->s_cap_ttl;
657 spin_unlock(&cap->session->s_gen_ttl_lock);
659 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
660 dout("__cap_is_valid %p cap %p issued %s "
661 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
662 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
670 * Return set of valid cap bits issued to us. Note that caps time
671 * out, and may be invalidated in bulk if the client session times out
672 * and session->s_cap_gen is bumped.
674 int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
676 int have = ci->i_snap_caps;
677 struct ceph_cap *cap;
682 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
683 cap = rb_entry(p, struct ceph_cap, ci_node);
684 if (!__cap_is_valid(cap))
686 dout("__ceph_caps_issued %p cap %p issued %s\n",
687 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
690 *implemented |= cap->implemented;
693 * exclude caps issued by non-auth MDS, but are been revoking
694 * by the auth MDS. The non-auth MDS should be revoking/exporting
695 * these caps, but the message is delayed.
697 if (ci->i_auth_cap) {
698 cap = ci->i_auth_cap;
699 have &= ~cap->implemented | cap->issued;
705 * Get cap bits issued by caps other than @ocap
707 int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
709 int have = ci->i_snap_caps;
710 struct ceph_cap *cap;
713 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
714 cap = rb_entry(p, struct ceph_cap, ci_node);
717 if (!__cap_is_valid(cap))
725 * Move a cap to the end of the LRU (oldest caps at list head, newest
728 static void __touch_cap(struct ceph_cap *cap)
730 struct ceph_mds_session *s = cap->session;
732 spin_lock(&s->s_cap_lock);
733 if (!s->s_cap_iterator) {
734 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
736 list_move_tail(&cap->session_caps, &s->s_caps);
738 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
739 &cap->ci->vfs_inode, cap, s->s_mds);
741 spin_unlock(&s->s_cap_lock);
745 * Check if we hold the given mask. If so, move the cap(s) to the
746 * front of their respective LRUs. (This is the preferred way for
747 * callers to check for caps they want.)
749 int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
751 struct ceph_cap *cap;
753 int have = ci->i_snap_caps;
755 if ((have & mask) == mask) {
756 dout("__ceph_caps_issued_mask %p snap issued %s"
757 " (mask %s)\n", &ci->vfs_inode,
758 ceph_cap_string(have),
759 ceph_cap_string(mask));
763 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
764 cap = rb_entry(p, struct ceph_cap, ci_node);
765 if (!__cap_is_valid(cap))
767 if ((cap->issued & mask) == mask) {
768 dout("__ceph_caps_issued_mask %p cap %p issued %s"
769 " (mask %s)\n", &ci->vfs_inode, cap,
770 ceph_cap_string(cap->issued),
771 ceph_cap_string(mask));
777 /* does a combination of caps satisfy mask? */
779 if ((have & mask) == mask) {
780 dout("__ceph_caps_issued_mask %p combo issued %s"
781 " (mask %s)\n", &ci->vfs_inode,
782 ceph_cap_string(cap->issued),
783 ceph_cap_string(mask));
787 /* touch this + preceding caps */
789 for (q = rb_first(&ci->i_caps); q != p;
791 cap = rb_entry(q, struct ceph_cap,
793 if (!__cap_is_valid(cap))
806 * Return true if mask caps are currently being revoked by an MDS.
808 int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
809 struct ceph_cap *ocap, int mask)
811 struct ceph_cap *cap;
814 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
815 cap = rb_entry(p, struct ceph_cap, ci_node);
817 (cap->implemented & ~cap->issued & mask))
823 int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
825 struct inode *inode = &ci->vfs_inode;
828 spin_lock(&ci->i_ceph_lock);
829 ret = __ceph_caps_revoking_other(ci, NULL, mask);
830 spin_unlock(&ci->i_ceph_lock);
831 dout("ceph_caps_revoking %p %s = %d\n", inode,
832 ceph_cap_string(mask), ret);
836 int __ceph_caps_used(struct ceph_inode_info *ci)
840 used |= CEPH_CAP_PIN;
842 used |= CEPH_CAP_FILE_RD;
843 if (ci->i_rdcache_ref ||
844 (!S_ISDIR(ci->vfs_inode.i_mode) && /* ignore readdir cache */
845 ci->vfs_inode.i_data.nrpages))
846 used |= CEPH_CAP_FILE_CACHE;
848 used |= CEPH_CAP_FILE_WR;
849 if (ci->i_wb_ref || ci->i_wrbuffer_ref)
850 used |= CEPH_CAP_FILE_BUFFER;
855 * wanted, by virtue of open file modes
857 int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
860 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
861 if (ci->i_nr_by_mode[i])
866 return ceph_caps_for_mode(bits >> 1);
870 * Return caps we have registered with the MDS(s) as 'wanted'.
872 int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check)
874 struct ceph_cap *cap;
878 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
879 cap = rb_entry(p, struct ceph_cap, ci_node);
880 if (check && !__cap_is_valid(cap))
882 if (cap == ci->i_auth_cap)
883 mds_wanted |= cap->mds_wanted;
885 mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
891 * called under i_ceph_lock
893 static int __ceph_is_any_caps(struct ceph_inode_info *ci)
895 return !RB_EMPTY_ROOT(&ci->i_caps);
898 int ceph_is_any_caps(struct inode *inode)
900 struct ceph_inode_info *ci = ceph_inode(inode);
903 spin_lock(&ci->i_ceph_lock);
904 ret = __ceph_is_any_caps(ci);
905 spin_unlock(&ci->i_ceph_lock);
910 static void drop_inode_snap_realm(struct ceph_inode_info *ci)
912 struct ceph_snap_realm *realm = ci->i_snap_realm;
913 spin_lock(&realm->inodes_with_caps_lock);
914 list_del_init(&ci->i_snap_realm_item);
915 ci->i_snap_realm_counter++;
916 ci->i_snap_realm = NULL;
917 spin_unlock(&realm->inodes_with_caps_lock);
918 ceph_put_snap_realm(ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc,
923 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
925 * caller should hold i_ceph_lock.
926 * caller will not hold session s_mutex if called from destroy_inode.
928 void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
930 struct ceph_mds_session *session = cap->session;
931 struct ceph_inode_info *ci = cap->ci;
932 struct ceph_mds_client *mdsc;
935 /* 'ci' being NULL means the remove have already occurred */
937 dout("%s: cap inode is NULL\n", __func__);
941 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
943 mdsc = ceph_inode_to_client(&ci->vfs_inode)->mdsc;
945 /* remove from inode's cap rbtree, and clear auth cap */
946 rb_erase(&cap->ci_node, &ci->i_caps);
947 if (ci->i_auth_cap == cap)
948 ci->i_auth_cap = NULL;
950 /* remove from session list */
951 spin_lock(&session->s_cap_lock);
952 if (session->s_cap_iterator == cap) {
953 /* not yet, we are iterating over this very cap */
954 dout("__ceph_remove_cap delaying %p removal from session %p\n",
957 list_del_init(&cap->session_caps);
958 session->s_nr_caps--;
962 /* protect backpointer with s_cap_lock: see iterate_session_caps */
966 * s_cap_reconnect is protected by s_cap_lock. no one changes
967 * s_cap_gen while session is in the reconnect state.
970 (!session->s_cap_reconnect || cap->cap_gen == session->s_cap_gen)) {
971 cap->queue_release = 1;
973 list_add_tail(&cap->session_caps,
974 &session->s_cap_releases);
975 session->s_num_cap_releases++;
979 cap->queue_release = 0;
981 cap->cap_ino = ci->i_vino.ino;
983 spin_unlock(&session->s_cap_lock);
986 ceph_put_cap(mdsc, cap);
988 /* when reconnect denied, we remove session caps forcibly,
989 * i_wr_ref can be non-zero. If there are ongoing write,
992 if (!__ceph_is_any_caps(ci) && ci->i_wr_ref == 0 && ci->i_snap_realm)
993 drop_inode_snap_realm(ci);
995 if (!__ceph_is_any_real_caps(ci))
996 __cap_delay_cancel(mdsc, ci);
999 struct cap_msg_args {
1000 struct ceph_mds_session *session;
1001 u64 ino, cid, follows;
1002 u64 flush_tid, oldest_flush_tid, size, max_size;
1004 struct ceph_buffer *xattr_buf;
1005 struct timespec atime, mtime, ctime;
1006 int op, caps, wanted, dirty;
1007 u32 seq, issue_seq, mseq, time_warp_seq;
1016 * Build and send a cap message to the given MDS.
1018 * Caller should be holding s_mutex.
1020 static int send_cap_msg(struct cap_msg_args *arg)
1022 struct ceph_mds_caps *fc;
1023 struct ceph_msg *msg;
1026 struct timespec zerotime = {0};
1027 struct ceph_osd_client *osdc = &arg->session->s_mdsc->fsc->client->osdc;
1029 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
1030 " seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu"
1031 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(arg->op),
1032 arg->cid, arg->ino, ceph_cap_string(arg->caps),
1033 ceph_cap_string(arg->wanted), ceph_cap_string(arg->dirty),
1034 arg->seq, arg->issue_seq, arg->flush_tid, arg->oldest_flush_tid,
1035 arg->mseq, arg->follows, arg->size, arg->max_size,
1037 arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0);
1039 /* flock buffer size + inline version + inline data size +
1040 * osd_epoch_barrier + oldest_flush_tid */
1041 extra_len = 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4;
1042 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc) + extra_len,
1047 msg->hdr.version = cpu_to_le16(10);
1048 msg->hdr.tid = cpu_to_le64(arg->flush_tid);
1050 fc = msg->front.iov_base;
1051 memset(fc, 0, sizeof(*fc));
1053 fc->cap_id = cpu_to_le64(arg->cid);
1054 fc->op = cpu_to_le32(arg->op);
1055 fc->seq = cpu_to_le32(arg->seq);
1056 fc->issue_seq = cpu_to_le32(arg->issue_seq);
1057 fc->migrate_seq = cpu_to_le32(arg->mseq);
1058 fc->caps = cpu_to_le32(arg->caps);
1059 fc->wanted = cpu_to_le32(arg->wanted);
1060 fc->dirty = cpu_to_le32(arg->dirty);
1061 fc->ino = cpu_to_le64(arg->ino);
1062 fc->snap_follows = cpu_to_le64(arg->follows);
1064 fc->size = cpu_to_le64(arg->size);
1065 fc->max_size = cpu_to_le64(arg->max_size);
1066 ceph_encode_timespec(&fc->mtime, &arg->mtime);
1067 ceph_encode_timespec(&fc->atime, &arg->atime);
1068 ceph_encode_timespec(&fc->ctime, &arg->ctime);
1069 fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq);
1071 fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid));
1072 fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid));
1073 fc->mode = cpu_to_le32(arg->mode);
1075 fc->xattr_version = cpu_to_le64(arg->xattr_version);
1076 if (arg->xattr_buf) {
1077 msg->middle = ceph_buffer_get(arg->xattr_buf);
1078 fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1079 msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1083 /* flock buffer size (version 2) */
1084 ceph_encode_32(&p, 0);
1085 /* inline version (version 4) */
1086 ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE);
1087 /* inline data size */
1088 ceph_encode_32(&p, 0);
1090 * osd_epoch_barrier (version 5)
1091 * The epoch_barrier is protected osdc->lock, so READ_ONCE here in
1092 * case it was recently changed
1094 ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier));
1095 /* oldest_flush_tid (version 6) */
1096 ceph_encode_64(&p, arg->oldest_flush_tid);
1099 * caller_uid/caller_gid (version 7)
1101 * Currently, we don't properly track which caller dirtied the caps
1102 * last, and force a flush of them when there is a conflict. For now,
1103 * just set this to 0:0, to emulate how the MDS has worked up to now.
1105 ceph_encode_32(&p, 0);
1106 ceph_encode_32(&p, 0);
1108 /* pool namespace (version 8) (mds always ignores this) */
1109 ceph_encode_32(&p, 0);
1112 * btime and change_attr (version 9)
1114 * We just zero these out for now, as the MDS ignores them unless
1115 * the requisite feature flags are set (which we don't do yet).
1117 ceph_encode_timespec(p, &zerotime);
1118 p += sizeof(struct ceph_timespec);
1119 ceph_encode_64(&p, 0);
1121 /* Advisory flags (version 10) */
1122 ceph_encode_32(&p, arg->flags);
1124 ceph_con_send(&arg->session->s_con, msg);
1129 * Queue cap releases when an inode is dropped from our cache.
1131 void ceph_queue_caps_release(struct inode *inode)
1133 struct ceph_inode_info *ci = ceph_inode(inode);
1136 /* lock i_ceph_lock, because ceph_d_revalidate(..., LOOKUP_RCU)
1137 * may call __ceph_caps_issued_mask() on a freeing inode. */
1138 spin_lock(&ci->i_ceph_lock);
1139 p = rb_first(&ci->i_caps);
1141 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1143 __ceph_remove_cap(cap, true);
1145 spin_unlock(&ci->i_ceph_lock);
1149 * Send a cap msg on the given inode. Update our caps state, then
1150 * drop i_ceph_lock and send the message.
1152 * Make note of max_size reported/requested from mds, revoked caps
1153 * that have now been implemented.
1155 * Make half-hearted attempt ot to invalidate page cache if we are
1156 * dropping RDCACHE. Note that this will leave behind locked pages
1157 * that we'll then need to deal with elsewhere.
1159 * Return non-zero if delayed release, or we experienced an error
1160 * such that the caller should requeue + retry later.
1162 * called with i_ceph_lock, then drops it.
1163 * caller should hold snap_rwsem (read), s_mutex.
1165 static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1166 int op, bool sync, int used, int want, int retain,
1167 int flushing, u64 flush_tid, u64 oldest_flush_tid)
1168 __releases(cap->ci->i_ceph_lock)
1170 struct ceph_inode_info *ci = cap->ci;
1171 struct inode *inode = &ci->vfs_inode;
1172 struct ceph_buffer *old_blob = NULL;
1173 struct cap_msg_args arg;
1174 int held, revoking, dropping;
1179 held = cap->issued | cap->implemented;
1180 revoking = cap->implemented & ~cap->issued;
1181 retain &= ~revoking;
1182 dropping = cap->issued & ~retain;
1184 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1185 inode, cap, cap->session,
1186 ceph_cap_string(held), ceph_cap_string(held & retain),
1187 ceph_cap_string(revoking));
1188 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1190 arg.session = cap->session;
1192 /* don't release wanted unless we've waited a bit. */
1193 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1194 time_before(jiffies, ci->i_hold_caps_min)) {
1195 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1196 ceph_cap_string(cap->issued),
1197 ceph_cap_string(cap->issued & retain),
1198 ceph_cap_string(cap->mds_wanted),
1199 ceph_cap_string(want));
1200 want |= cap->mds_wanted;
1201 retain |= cap->issued;
1204 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1205 if (want & ~cap->mds_wanted) {
1206 /* user space may open/close single file frequently.
1207 * This avoids droping mds_wanted immediately after
1208 * requesting new mds_wanted.
1210 __cap_set_timeouts(mdsc, ci);
1213 cap->issued &= retain; /* drop bits we don't want */
1214 if (cap->implemented & ~cap->issued) {
1216 * Wake up any waiters on wanted -> needed transition.
1217 * This is due to the weird transition from buffered
1218 * to sync IO... we need to flush dirty pages _before_
1219 * allowing sync writes to avoid reordering.
1223 cap->implemented &= cap->issued | used;
1224 cap->mds_wanted = want;
1226 arg.ino = ceph_vino(inode).ino;
1227 arg.cid = cap->cap_id;
1228 arg.follows = flushing ? ci->i_head_snapc->seq : 0;
1229 arg.flush_tid = flush_tid;
1230 arg.oldest_flush_tid = oldest_flush_tid;
1232 arg.size = inode->i_size;
1233 ci->i_reported_size = arg.size;
1234 arg.max_size = ci->i_wanted_max_size;
1235 ci->i_requested_max_size = arg.max_size;
1237 if (flushing & CEPH_CAP_XATTR_EXCL) {
1238 old_blob = __ceph_build_xattrs_blob(ci);
1239 arg.xattr_version = ci->i_xattrs.version;
1240 arg.xattr_buf = ci->i_xattrs.blob;
1242 arg.xattr_buf = NULL;
1245 arg.mtime = inode->i_mtime;
1246 arg.atime = inode->i_atime;
1247 arg.ctime = inode->i_ctime;
1250 arg.caps = cap->implemented;
1252 arg.dirty = flushing;
1255 arg.issue_seq = cap->issue_seq;
1256 arg.mseq = cap->mseq;
1257 arg.time_warp_seq = ci->i_time_warp_seq;
1259 arg.uid = inode->i_uid;
1260 arg.gid = inode->i_gid;
1261 arg.mode = inode->i_mode;
1263 arg.inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
1264 if (list_empty(&ci->i_cap_snaps))
1265 arg.flags = CEPH_CLIENT_CAPS_NO_CAPSNAP;
1267 arg.flags = CEPH_CLIENT_CAPS_PENDING_CAPSNAP;
1269 arg.flags |= CEPH_CLIENT_CAPS_SYNC;
1271 spin_unlock(&ci->i_ceph_lock);
1273 ceph_buffer_put(old_blob);
1275 ret = send_cap_msg(&arg);
1277 dout("error sending cap msg, must requeue %p\n", inode);
1282 wake_up_all(&ci->i_cap_wq);
1287 static inline int __send_flush_snap(struct inode *inode,
1288 struct ceph_mds_session *session,
1289 struct ceph_cap_snap *capsnap,
1290 u32 mseq, u64 oldest_flush_tid)
1292 struct cap_msg_args arg;
1294 arg.session = session;
1295 arg.ino = ceph_vino(inode).ino;
1297 arg.follows = capsnap->follows;
1298 arg.flush_tid = capsnap->cap_flush.tid;
1299 arg.oldest_flush_tid = oldest_flush_tid;
1301 arg.size = capsnap->size;
1303 arg.xattr_version = capsnap->xattr_version;
1304 arg.xattr_buf = capsnap->xattr_blob;
1306 arg.atime = capsnap->atime;
1307 arg.mtime = capsnap->mtime;
1308 arg.ctime = capsnap->ctime;
1310 arg.op = CEPH_CAP_OP_FLUSHSNAP;
1311 arg.caps = capsnap->issued;
1313 arg.dirty = capsnap->dirty;
1318 arg.time_warp_seq = capsnap->time_warp_seq;
1320 arg.uid = capsnap->uid;
1321 arg.gid = capsnap->gid;
1322 arg.mode = capsnap->mode;
1324 arg.inline_data = capsnap->inline_data;
1327 return send_cap_msg(&arg);
1331 * When a snapshot is taken, clients accumulate dirty metadata on
1332 * inodes with capabilities in ceph_cap_snaps to describe the file
1333 * state at the time the snapshot was taken. This must be flushed
1334 * asynchronously back to the MDS once sync writes complete and dirty
1335 * data is written out.
1337 * Called under i_ceph_lock. Takes s_mutex as needed.
1339 static void __ceph_flush_snaps(struct ceph_inode_info *ci,
1340 struct ceph_mds_session *session)
1341 __releases(ci->i_ceph_lock)
1342 __acquires(ci->i_ceph_lock)
1344 struct inode *inode = &ci->vfs_inode;
1345 struct ceph_mds_client *mdsc = session->s_mdsc;
1346 struct ceph_cap_snap *capsnap;
1347 u64 oldest_flush_tid = 0;
1348 u64 first_tid = 1, last_tid = 0;
1350 dout("__flush_snaps %p session %p\n", inode, session);
1352 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1354 * we need to wait for sync writes to complete and for dirty
1355 * pages to be written out.
1357 if (capsnap->dirty_pages || capsnap->writing)
1360 /* should be removed by ceph_try_drop_cap_snap() */
1361 BUG_ON(!capsnap->need_flush);
1363 /* only flush each capsnap once */
1364 if (capsnap->cap_flush.tid > 0) {
1365 dout(" already flushed %p, skipping\n", capsnap);
1369 spin_lock(&mdsc->cap_dirty_lock);
1370 capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
1371 list_add_tail(&capsnap->cap_flush.g_list,
1372 &mdsc->cap_flush_list);
1373 if (oldest_flush_tid == 0)
1374 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1375 if (list_empty(&ci->i_flushing_item)) {
1376 list_add_tail(&ci->i_flushing_item,
1377 &session->s_cap_flushing);
1379 spin_unlock(&mdsc->cap_dirty_lock);
1381 list_add_tail(&capsnap->cap_flush.i_list,
1382 &ci->i_cap_flush_list);
1385 first_tid = capsnap->cap_flush.tid;
1386 last_tid = capsnap->cap_flush.tid;
1389 ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS;
1391 while (first_tid <= last_tid) {
1392 struct ceph_cap *cap = ci->i_auth_cap;
1393 struct ceph_cap_flush *cf;
1396 if (!(cap && cap->session == session)) {
1397 dout("__flush_snaps %p auth cap %p not mds%d, "
1398 "stop\n", inode, cap, session->s_mds);
1403 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
1404 if (cf->tid >= first_tid) {
1412 first_tid = cf->tid + 1;
1414 capsnap = container_of(cf, struct ceph_cap_snap, cap_flush);
1415 refcount_inc(&capsnap->nref);
1416 spin_unlock(&ci->i_ceph_lock);
1418 dout("__flush_snaps %p capsnap %p tid %llu %s\n",
1419 inode, capsnap, cf->tid, ceph_cap_string(capsnap->dirty));
1421 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
1424 pr_err("__flush_snaps: error sending cap flushsnap, "
1425 "ino (%llx.%llx) tid %llu follows %llu\n",
1426 ceph_vinop(inode), cf->tid, capsnap->follows);
1429 ceph_put_cap_snap(capsnap);
1430 spin_lock(&ci->i_ceph_lock);
1434 void ceph_flush_snaps(struct ceph_inode_info *ci,
1435 struct ceph_mds_session **psession)
1437 struct inode *inode = &ci->vfs_inode;
1438 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
1439 struct ceph_mds_session *session = NULL;
1442 dout("ceph_flush_snaps %p\n", inode);
1444 session = *psession;
1446 spin_lock(&ci->i_ceph_lock);
1447 if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) {
1448 dout(" no capsnap needs flush, doing nothing\n");
1451 if (!ci->i_auth_cap) {
1452 dout(" no auth cap (migrating?), doing nothing\n");
1456 mds = ci->i_auth_cap->session->s_mds;
1457 if (session && session->s_mds != mds) {
1458 dout(" oops, wrong session %p mutex\n", session);
1459 mutex_unlock(&session->s_mutex);
1460 ceph_put_mds_session(session);
1464 spin_unlock(&ci->i_ceph_lock);
1465 mutex_lock(&mdsc->mutex);
1466 session = __ceph_lookup_mds_session(mdsc, mds);
1467 mutex_unlock(&mdsc->mutex);
1469 dout(" inverting session/ino locks on %p\n", session);
1470 mutex_lock(&session->s_mutex);
1475 // make sure flushsnap messages are sent in proper order.
1476 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
1477 __kick_flushing_caps(mdsc, session, ci, 0);
1478 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
1481 __ceph_flush_snaps(ci, session);
1483 spin_unlock(&ci->i_ceph_lock);
1486 *psession = session;
1487 } else if (session) {
1488 mutex_unlock(&session->s_mutex);
1489 ceph_put_mds_session(session);
1491 /* we flushed them all; remove this inode from the queue */
1492 spin_lock(&mdsc->snap_flush_lock);
1493 list_del_init(&ci->i_snap_flush_item);
1494 spin_unlock(&mdsc->snap_flush_lock);
1498 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1499 * Caller is then responsible for calling __mark_inode_dirty with the
1500 * returned flags value.
1502 int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
1503 struct ceph_cap_flush **pcf)
1505 struct ceph_mds_client *mdsc =
1506 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
1507 struct inode *inode = &ci->vfs_inode;
1508 int was = ci->i_dirty_caps;
1511 if (!ci->i_auth_cap) {
1512 pr_warn("__mark_dirty_caps %p %llx mask %s, "
1513 "but no auth cap (session was closed?)\n",
1514 inode, ceph_ino(inode), ceph_cap_string(mask));
1518 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1519 ceph_cap_string(mask), ceph_cap_string(was),
1520 ceph_cap_string(was | mask));
1521 ci->i_dirty_caps |= mask;
1523 WARN_ON_ONCE(ci->i_prealloc_cap_flush);
1524 swap(ci->i_prealloc_cap_flush, *pcf);
1526 if (!ci->i_head_snapc) {
1527 WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
1528 ci->i_head_snapc = ceph_get_snap_context(
1529 ci->i_snap_realm->cached_context);
1531 dout(" inode %p now dirty snapc %p auth cap %p\n",
1532 &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
1533 BUG_ON(!list_empty(&ci->i_dirty_item));
1534 spin_lock(&mdsc->cap_dirty_lock);
1535 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
1536 spin_unlock(&mdsc->cap_dirty_lock);
1537 if (ci->i_flushing_caps == 0) {
1539 dirty |= I_DIRTY_SYNC;
1542 WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
1544 BUG_ON(list_empty(&ci->i_dirty_item));
1545 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1546 (mask & CEPH_CAP_FILE_BUFFER))
1547 dirty |= I_DIRTY_DATASYNC;
1548 __cap_delay_requeue(mdsc, ci);
1552 struct ceph_cap_flush *ceph_alloc_cap_flush(void)
1554 return kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
1557 void ceph_free_cap_flush(struct ceph_cap_flush *cf)
1560 kmem_cache_free(ceph_cap_flush_cachep, cf);
1563 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
1565 if (!list_empty(&mdsc->cap_flush_list)) {
1566 struct ceph_cap_flush *cf =
1567 list_first_entry(&mdsc->cap_flush_list,
1568 struct ceph_cap_flush, g_list);
1575 * Remove cap_flush from the mdsc's or inode's flushing cap list.
1576 * Return true if caller needs to wake up flush waiters.
1578 static bool __finish_cap_flush(struct ceph_mds_client *mdsc,
1579 struct ceph_inode_info *ci,
1580 struct ceph_cap_flush *cf)
1582 struct ceph_cap_flush *prev;
1583 bool wake = cf->wake;
1585 /* are there older pending cap flushes? */
1586 if (wake && cf->g_list.prev != &mdsc->cap_flush_list) {
1587 prev = list_prev_entry(cf, g_list);
1591 list_del(&cf->g_list);
1593 if (wake && cf->i_list.prev != &ci->i_cap_flush_list) {
1594 prev = list_prev_entry(cf, i_list);
1598 list_del(&cf->i_list);
1606 * Add dirty inode to the flushing list. Assigned a seq number so we
1607 * can wait for caps to flush without starving.
1609 * Called under i_ceph_lock.
1611 static int __mark_caps_flushing(struct inode *inode,
1612 struct ceph_mds_session *session, bool wake,
1613 u64 *flush_tid, u64 *oldest_flush_tid)
1615 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1616 struct ceph_inode_info *ci = ceph_inode(inode);
1617 struct ceph_cap_flush *cf = NULL;
1620 BUG_ON(ci->i_dirty_caps == 0);
1621 BUG_ON(list_empty(&ci->i_dirty_item));
1622 BUG_ON(!ci->i_prealloc_cap_flush);
1624 flushing = ci->i_dirty_caps;
1625 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1626 ceph_cap_string(flushing),
1627 ceph_cap_string(ci->i_flushing_caps),
1628 ceph_cap_string(ci->i_flushing_caps | flushing));
1629 ci->i_flushing_caps |= flushing;
1630 ci->i_dirty_caps = 0;
1631 dout(" inode %p now !dirty\n", inode);
1633 swap(cf, ci->i_prealloc_cap_flush);
1634 cf->caps = flushing;
1637 spin_lock(&mdsc->cap_dirty_lock);
1638 list_del_init(&ci->i_dirty_item);
1640 cf->tid = ++mdsc->last_cap_flush_tid;
1641 list_add_tail(&cf->g_list, &mdsc->cap_flush_list);
1642 *oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1644 if (list_empty(&ci->i_flushing_item)) {
1645 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1646 mdsc->num_cap_flushing++;
1648 spin_unlock(&mdsc->cap_dirty_lock);
1650 list_add_tail(&cf->i_list, &ci->i_cap_flush_list);
1652 *flush_tid = cf->tid;
1657 * try to invalidate mapping pages without blocking.
1659 static int try_nonblocking_invalidate(struct inode *inode)
1660 __releases(ci->i_ceph_lock)
1661 __acquires(ci->i_ceph_lock)
1663 struct ceph_inode_info *ci = ceph_inode(inode);
1664 u32 invalidating_gen = ci->i_rdcache_gen;
1666 spin_unlock(&ci->i_ceph_lock);
1667 ceph_fscache_invalidate(inode);
1668 invalidate_mapping_pages(&inode->i_data, 0, -1);
1669 spin_lock(&ci->i_ceph_lock);
1671 if (inode->i_data.nrpages == 0 &&
1672 invalidating_gen == ci->i_rdcache_gen) {
1674 dout("try_nonblocking_invalidate %p success\n", inode);
1675 /* save any racing async invalidate some trouble */
1676 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
1679 dout("try_nonblocking_invalidate %p failed\n", inode);
1683 bool __ceph_should_report_size(struct ceph_inode_info *ci)
1685 loff_t size = ci->vfs_inode.i_size;
1686 /* mds will adjust max size according to the reported size */
1687 if (ci->i_flushing_caps & CEPH_CAP_FILE_WR)
1689 if (size >= ci->i_max_size)
1691 /* half of previous max_size increment has been used */
1692 if (ci->i_max_size > ci->i_reported_size &&
1693 (size << 1) >= ci->i_max_size + ci->i_reported_size)
1699 * Swiss army knife function to examine currently used and wanted
1700 * versus held caps. Release, flush, ack revoked caps to mds as
1703 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1704 * cap release further.
1705 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1706 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1709 void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1710 struct ceph_mds_session *session)
1712 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1713 struct ceph_mds_client *mdsc = fsc->mdsc;
1714 struct inode *inode = &ci->vfs_inode;
1715 struct ceph_cap *cap;
1716 u64 flush_tid, oldest_flush_tid;
1717 int file_wanted, used, cap_used;
1718 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
1719 int issued, implemented, want, retain, revoking, flushing = 0;
1720 int mds = -1; /* keep track of how far we've gone through i_caps list
1721 to avoid an infinite loop on retry */
1723 int delayed = 0, sent = 0, num;
1724 bool is_delayed = flags & CHECK_CAPS_NODELAY;
1725 bool queue_invalidate = false;
1726 bool force_requeue = false;
1727 bool tried_invalidate = false;
1729 /* if we are unmounting, flush any unused caps immediately. */
1733 spin_lock(&ci->i_ceph_lock);
1735 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1736 flags |= CHECK_CAPS_FLUSH;
1740 spin_lock(&ci->i_ceph_lock);
1742 file_wanted = __ceph_caps_file_wanted(ci);
1743 used = __ceph_caps_used(ci);
1744 issued = __ceph_caps_issued(ci, &implemented);
1745 revoking = implemented & ~issued;
1748 retain = file_wanted | used | CEPH_CAP_PIN;
1749 if (!mdsc->stopping && inode->i_nlink > 0) {
1751 retain |= CEPH_CAP_ANY; /* be greedy */
1752 } else if (S_ISDIR(inode->i_mode) &&
1753 (issued & CEPH_CAP_FILE_SHARED) &&
1754 __ceph_dir_is_complete(ci)) {
1756 * If a directory is complete, we want to keep
1757 * the exclusive cap. So that MDS does not end up
1758 * revoking the shared cap on every create/unlink
1761 want = CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
1765 retain |= CEPH_CAP_ANY_SHARED;
1767 * keep RD only if we didn't have the file open RW,
1768 * because then the mds would revoke it anyway to
1769 * journal max_size=0.
1771 if (ci->i_max_size == 0)
1772 retain |= CEPH_CAP_ANY_RD;
1776 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1777 " issued %s revoking %s retain %s %s%s%s\n", inode,
1778 ceph_cap_string(file_wanted),
1779 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1780 ceph_cap_string(ci->i_flushing_caps),
1781 ceph_cap_string(issued), ceph_cap_string(revoking),
1782 ceph_cap_string(retain),
1783 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1784 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1785 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1788 * If we no longer need to hold onto old our caps, and we may
1789 * have cached pages, but don't want them, then try to invalidate.
1790 * If we fail, it's because pages are locked.... try again later.
1792 if ((!is_delayed || mdsc->stopping) &&
1793 !S_ISDIR(inode->i_mode) && /* ignore readdir cache */
1794 !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */
1795 inode->i_data.nrpages && /* have cached pages */
1796 (revoking & (CEPH_CAP_FILE_CACHE|
1797 CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */
1798 !tried_invalidate) {
1799 dout("check_caps trying to invalidate on %p\n", inode);
1800 if (try_nonblocking_invalidate(inode) < 0) {
1801 if (revoking & (CEPH_CAP_FILE_CACHE|
1802 CEPH_CAP_FILE_LAZYIO)) {
1803 dout("check_caps queuing invalidate\n");
1804 queue_invalidate = true;
1805 ci->i_rdcache_revoking = ci->i_rdcache_gen;
1807 dout("check_caps failed to invalidate pages\n");
1808 /* we failed to invalidate pages. check these
1809 caps again later. */
1810 force_requeue = true;
1811 __cap_set_timeouts(mdsc, ci);
1814 tried_invalidate = true;
1819 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1820 cap = rb_entry(p, struct ceph_cap, ci_node);
1823 /* avoid looping forever */
1824 if (mds >= cap->mds ||
1825 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1828 /* NOTE: no side-effects allowed, until we take s_mutex */
1831 if (ci->i_auth_cap && cap != ci->i_auth_cap)
1832 cap_used &= ~ci->i_auth_cap->issued;
1834 revoking = cap->implemented & ~cap->issued;
1835 dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
1836 cap->mds, cap, ceph_cap_string(cap_used),
1837 ceph_cap_string(cap->issued),
1838 ceph_cap_string(cap->implemented),
1839 ceph_cap_string(revoking));
1841 if (cap == ci->i_auth_cap &&
1842 (cap->issued & CEPH_CAP_FILE_WR)) {
1843 /* request larger max_size from MDS? */
1844 if (ci->i_wanted_max_size > ci->i_max_size &&
1845 ci->i_wanted_max_size > ci->i_requested_max_size) {
1846 dout("requesting new max_size\n");
1850 /* approaching file_max? */
1851 if (__ceph_should_report_size(ci)) {
1852 dout("i_size approaching max_size\n");
1856 /* flush anything dirty? */
1857 if (cap == ci->i_auth_cap) {
1858 if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) {
1859 dout("flushing dirty caps\n");
1862 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) {
1863 dout("flushing snap caps\n");
1868 /* completed revocation? going down and there are no caps? */
1869 if (revoking && (revoking & cap_used) == 0) {
1870 dout("completed revocation of %s\n",
1871 ceph_cap_string(cap->implemented & ~cap->issued));
1875 /* want more caps from mds? */
1876 if (want & ~cap->mds_wanted) {
1877 if (want & ~(cap->mds_wanted | cap->issued))
1879 if (!__cap_is_valid(cap))
1883 /* things we might delay */
1884 if ((cap->issued & ~retain) == 0 &&
1885 cap->mds_wanted == want)
1886 continue; /* nope, all good */
1892 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1893 time_before(jiffies, ci->i_hold_caps_max)) {
1894 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1895 ceph_cap_string(cap->issued),
1896 ceph_cap_string(cap->issued & retain),
1897 ceph_cap_string(cap->mds_wanted),
1898 ceph_cap_string(want));
1904 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1905 dout(" skipping %p I_NOFLUSH set\n", inode);
1909 if (session && session != cap->session) {
1910 dout("oops, wrong session %p mutex\n", session);
1911 mutex_unlock(&session->s_mutex);
1915 session = cap->session;
1916 if (mutex_trylock(&session->s_mutex) == 0) {
1917 dout("inverting session/ino locks on %p\n",
1919 session = ceph_get_mds_session(session);
1920 spin_unlock(&ci->i_ceph_lock);
1921 if (took_snap_rwsem) {
1922 up_read(&mdsc->snap_rwsem);
1923 took_snap_rwsem = 0;
1926 mutex_lock(&session->s_mutex);
1927 ceph_put_mds_session(session);
1930 * Because we take the reference while
1931 * holding the i_ceph_lock, it should
1932 * never be NULL. Throw a warning if it
1941 /* kick flushing and flush snaps before sending normal
1943 if (cap == ci->i_auth_cap &&
1945 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) {
1946 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
1947 __kick_flushing_caps(mdsc, session, ci, 0);
1948 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
1950 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
1951 __ceph_flush_snaps(ci, session);
1956 /* take snap_rwsem after session mutex */
1957 if (!took_snap_rwsem) {
1958 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
1959 dout("inverting snap/in locks on %p\n",
1961 spin_unlock(&ci->i_ceph_lock);
1962 down_read(&mdsc->snap_rwsem);
1963 took_snap_rwsem = 1;
1966 took_snap_rwsem = 1;
1969 if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
1970 flushing = __mark_caps_flushing(inode, session, false,
1976 spin_lock(&mdsc->cap_dirty_lock);
1977 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1978 spin_unlock(&mdsc->cap_dirty_lock);
1981 mds = cap->mds; /* remember mds, so we don't repeat */
1984 /* __send_cap drops i_ceph_lock */
1985 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, false,
1986 cap_used, want, retain, flushing,
1987 flush_tid, oldest_flush_tid);
1988 goto retry; /* retake i_ceph_lock and restart our cap scan. */
1992 * Reschedule delayed caps release if we delayed anything,
1995 if (delayed && is_delayed)
1996 force_requeue = true; /* __send_cap delayed release; requeue */
1997 if (!delayed && !is_delayed)
1998 __cap_delay_cancel(mdsc, ci);
1999 else if (!is_delayed || force_requeue)
2000 __cap_delay_requeue(mdsc, ci);
2002 spin_unlock(&ci->i_ceph_lock);
2004 if (queue_invalidate)
2005 ceph_queue_invalidate(inode);
2008 mutex_unlock(&session->s_mutex);
2009 if (took_snap_rwsem)
2010 up_read(&mdsc->snap_rwsem);
2014 * Try to flush dirty caps back to the auth mds.
2016 static int try_flush_caps(struct inode *inode, u64 *ptid)
2018 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2019 struct ceph_inode_info *ci = ceph_inode(inode);
2020 struct ceph_mds_session *session = NULL;
2022 u64 flush_tid = 0, oldest_flush_tid = 0;
2025 spin_lock(&ci->i_ceph_lock);
2026 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
2027 spin_unlock(&ci->i_ceph_lock);
2028 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
2031 if (ci->i_dirty_caps && ci->i_auth_cap) {
2032 struct ceph_cap *cap = ci->i_auth_cap;
2033 int used = __ceph_caps_used(ci);
2034 int want = __ceph_caps_wanted(ci);
2037 if (!session || session != cap->session) {
2038 spin_unlock(&ci->i_ceph_lock);
2040 mutex_unlock(&session->s_mutex);
2041 session = cap->session;
2042 mutex_lock(&session->s_mutex);
2045 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) {
2046 spin_unlock(&ci->i_ceph_lock);
2050 flushing = __mark_caps_flushing(inode, session, true,
2051 &flush_tid, &oldest_flush_tid);
2053 /* __send_cap drops i_ceph_lock */
2054 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, true,
2055 used, want, (cap->issued | cap->implemented),
2056 flushing, flush_tid, oldest_flush_tid);
2059 spin_lock(&ci->i_ceph_lock);
2060 __cap_delay_requeue(mdsc, ci);
2061 spin_unlock(&ci->i_ceph_lock);
2064 if (!list_empty(&ci->i_cap_flush_list)) {
2065 struct ceph_cap_flush *cf =
2066 list_last_entry(&ci->i_cap_flush_list,
2067 struct ceph_cap_flush, i_list);
2069 flush_tid = cf->tid;
2071 flushing = ci->i_flushing_caps;
2072 spin_unlock(&ci->i_ceph_lock);
2076 mutex_unlock(&session->s_mutex);
2083 * Return true if we've flushed caps through the given flush_tid.
2085 static int caps_are_flushed(struct inode *inode, u64 flush_tid)
2087 struct ceph_inode_info *ci = ceph_inode(inode);
2090 spin_lock(&ci->i_ceph_lock);
2091 if (!list_empty(&ci->i_cap_flush_list)) {
2092 struct ceph_cap_flush * cf =
2093 list_first_entry(&ci->i_cap_flush_list,
2094 struct ceph_cap_flush, i_list);
2095 if (cf->tid <= flush_tid)
2098 spin_unlock(&ci->i_ceph_lock);
2103 * wait for any unsafe requests to complete.
2105 static int unsafe_request_wait(struct inode *inode)
2107 struct ceph_inode_info *ci = ceph_inode(inode);
2108 struct ceph_mds_request *req1 = NULL, *req2 = NULL;
2111 spin_lock(&ci->i_unsafe_lock);
2112 if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
2113 req1 = list_last_entry(&ci->i_unsafe_dirops,
2114 struct ceph_mds_request,
2116 ceph_mdsc_get_request(req1);
2118 if (!list_empty(&ci->i_unsafe_iops)) {
2119 req2 = list_last_entry(&ci->i_unsafe_iops,
2120 struct ceph_mds_request,
2121 r_unsafe_target_item);
2122 ceph_mdsc_get_request(req2);
2124 spin_unlock(&ci->i_unsafe_lock);
2126 dout("unsafe_request_wait %p wait on tid %llu %llu\n",
2127 inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL);
2129 ret = !wait_for_completion_timeout(&req1->r_safe_completion,
2130 ceph_timeout_jiffies(req1->r_timeout));
2133 ceph_mdsc_put_request(req1);
2136 ret = !wait_for_completion_timeout(&req2->r_safe_completion,
2137 ceph_timeout_jiffies(req2->r_timeout));
2140 ceph_mdsc_put_request(req2);
2145 int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2147 struct inode *inode = file->f_mapping->host;
2148 struct ceph_inode_info *ci = ceph_inode(inode);
2153 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
2155 ret = file_write_and_wait_range(file, start, end);
2164 dirty = try_flush_caps(inode, &flush_tid);
2165 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
2167 ret = unsafe_request_wait(inode);
2170 * only wait on non-file metadata writeback (the mds
2171 * can recover size and mtime, so we don't need to
2174 if (!ret && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
2175 ret = wait_event_interruptible(ci->i_cap_wq,
2176 caps_are_flushed(inode, flush_tid));
2178 inode_unlock(inode);
2180 dout("fsync %p%s result=%d\n", inode, datasync ? " datasync" : "", ret);
2185 * Flush any dirty caps back to the mds. If we aren't asked to wait,
2186 * queue inode for flush but don't do so immediately, because we can
2187 * get by with fewer MDS messages if we wait for data writeback to
2190 int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
2192 struct ceph_inode_info *ci = ceph_inode(inode);
2196 int wait = wbc->sync_mode == WB_SYNC_ALL;
2198 dout("write_inode %p wait=%d\n", inode, wait);
2200 dirty = try_flush_caps(inode, &flush_tid);
2202 err = wait_event_interruptible(ci->i_cap_wq,
2203 caps_are_flushed(inode, flush_tid));
2205 struct ceph_mds_client *mdsc =
2206 ceph_sb_to_client(inode->i_sb)->mdsc;
2208 spin_lock(&ci->i_ceph_lock);
2209 if (__ceph_caps_dirty(ci))
2210 __cap_delay_requeue_front(mdsc, ci);
2211 spin_unlock(&ci->i_ceph_lock);
2216 static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
2217 struct ceph_mds_session *session,
2218 struct ceph_inode_info *ci,
2219 u64 oldest_flush_tid)
2220 __releases(ci->i_ceph_lock)
2221 __acquires(ci->i_ceph_lock)
2223 struct inode *inode = &ci->vfs_inode;
2224 struct ceph_cap *cap;
2225 struct ceph_cap_flush *cf;
2229 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
2230 if (cf->tid < first_tid)
2233 cap = ci->i_auth_cap;
2234 if (!(cap && cap->session == session)) {
2235 pr_err("%p auth cap %p not mds%d ???\n",
2236 inode, cap, session->s_mds);
2240 first_tid = cf->tid + 1;
2243 dout("kick_flushing_caps %p cap %p tid %llu %s\n",
2244 inode, cap, cf->tid, ceph_cap_string(cf->caps));
2245 ci->i_ceph_flags |= CEPH_I_NODELAY;
2246 ret = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
2247 false, __ceph_caps_used(ci),
2248 __ceph_caps_wanted(ci),
2249 cap->issued | cap->implemented,
2250 cf->caps, cf->tid, oldest_flush_tid);
2252 pr_err("kick_flushing_caps: error sending "
2253 "cap flush, ino (%llx.%llx) "
2254 "tid %llu flushing %s\n",
2255 ceph_vinop(inode), cf->tid,
2256 ceph_cap_string(cf->caps));
2259 struct ceph_cap_snap *capsnap =
2260 container_of(cf, struct ceph_cap_snap,
2262 dout("kick_flushing_caps %p capsnap %p tid %llu %s\n",
2263 inode, capsnap, cf->tid,
2264 ceph_cap_string(capsnap->dirty));
2266 refcount_inc(&capsnap->nref);
2267 spin_unlock(&ci->i_ceph_lock);
2269 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
2272 pr_err("kick_flushing_caps: error sending "
2273 "cap flushsnap, ino (%llx.%llx) "
2274 "tid %llu follows %llu\n",
2275 ceph_vinop(inode), cf->tid,
2279 ceph_put_cap_snap(capsnap);
2282 spin_lock(&ci->i_ceph_lock);
2286 void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
2287 struct ceph_mds_session *session)
2289 struct ceph_inode_info *ci;
2290 struct ceph_cap *cap;
2291 u64 oldest_flush_tid;
2293 dout("early_kick_flushing_caps mds%d\n", session->s_mds);
2295 spin_lock(&mdsc->cap_dirty_lock);
2296 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2297 spin_unlock(&mdsc->cap_dirty_lock);
2299 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2300 spin_lock(&ci->i_ceph_lock);
2301 cap = ci->i_auth_cap;
2302 if (!(cap && cap->session == session)) {
2303 pr_err("%p auth cap %p not mds%d ???\n",
2304 &ci->vfs_inode, cap, session->s_mds);
2305 spin_unlock(&ci->i_ceph_lock);
2311 * if flushing caps were revoked, we re-send the cap flush
2312 * in client reconnect stage. This guarantees MDS * processes
2313 * the cap flush message before issuing the flushing caps to
2316 if ((cap->issued & ci->i_flushing_caps) !=
2317 ci->i_flushing_caps) {
2318 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2319 __kick_flushing_caps(mdsc, session, ci,
2322 ci->i_ceph_flags |= CEPH_I_KICK_FLUSH;
2325 spin_unlock(&ci->i_ceph_lock);
2329 void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
2330 struct ceph_mds_session *session)
2332 struct ceph_inode_info *ci;
2333 struct ceph_cap *cap;
2334 u64 oldest_flush_tid;
2336 dout("kick_flushing_caps mds%d\n", session->s_mds);
2338 spin_lock(&mdsc->cap_dirty_lock);
2339 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2340 spin_unlock(&mdsc->cap_dirty_lock);
2342 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2343 spin_lock(&ci->i_ceph_lock);
2344 cap = ci->i_auth_cap;
2345 if (!(cap && cap->session == session)) {
2346 pr_err("%p auth cap %p not mds%d ???\n",
2347 &ci->vfs_inode, cap, session->s_mds);
2348 spin_unlock(&ci->i_ceph_lock);
2351 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
2352 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2353 __kick_flushing_caps(mdsc, session, ci,
2356 spin_unlock(&ci->i_ceph_lock);
2360 static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
2361 struct ceph_mds_session *session,
2362 struct inode *inode)
2363 __releases(ci->i_ceph_lock)
2365 struct ceph_inode_info *ci = ceph_inode(inode);
2366 struct ceph_cap *cap;
2368 cap = ci->i_auth_cap;
2369 dout("kick_flushing_inode_caps %p flushing %s\n", inode,
2370 ceph_cap_string(ci->i_flushing_caps));
2372 if (!list_empty(&ci->i_cap_flush_list)) {
2373 u64 oldest_flush_tid;
2374 spin_lock(&mdsc->cap_dirty_lock);
2375 list_move_tail(&ci->i_flushing_item,
2376 &cap->session->s_cap_flushing);
2377 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2378 spin_unlock(&mdsc->cap_dirty_lock);
2380 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2381 __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid);
2382 spin_unlock(&ci->i_ceph_lock);
2384 spin_unlock(&ci->i_ceph_lock);
2390 * Take references to capabilities we hold, so that we don't release
2391 * them to the MDS prematurely.
2393 * Protected by i_ceph_lock.
2395 static void __take_cap_refs(struct ceph_inode_info *ci, int got,
2396 bool snap_rwsem_locked)
2398 if (got & CEPH_CAP_PIN)
2400 if (got & CEPH_CAP_FILE_RD)
2402 if (got & CEPH_CAP_FILE_CACHE)
2403 ci->i_rdcache_ref++;
2404 if (got & CEPH_CAP_FILE_WR) {
2405 if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
2406 BUG_ON(!snap_rwsem_locked);
2407 ci->i_head_snapc = ceph_get_snap_context(
2408 ci->i_snap_realm->cached_context);
2412 if (got & CEPH_CAP_FILE_BUFFER) {
2413 if (ci->i_wb_ref == 0)
2414 ihold(&ci->vfs_inode);
2416 dout("__take_cap_refs %p wb %d -> %d (?)\n",
2417 &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
2422 * Try to grab cap references. Specify those refs we @want, and the
2423 * minimal set we @need. Also include the larger offset we are writing
2424 * to (when applicable), and check against max_size here as well.
2425 * Note that caller is responsible for ensuring max_size increases are
2426 * requested from the MDS.
2428 static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
2429 loff_t endoff, bool nonblock, int *got, int *err)
2431 struct inode *inode = &ci->vfs_inode;
2432 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
2434 int have, implemented;
2436 bool snap_rwsem_locked = false;
2438 dout("get_cap_refs %p need %s want %s\n", inode,
2439 ceph_cap_string(need), ceph_cap_string(want));
2442 spin_lock(&ci->i_ceph_lock);
2444 /* make sure file is actually open */
2445 file_wanted = __ceph_caps_file_wanted(ci);
2446 if ((file_wanted & need) != need) {
2447 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2448 ceph_cap_string(need), ceph_cap_string(file_wanted));
2454 /* finish pending truncate */
2455 while (ci->i_truncate_pending) {
2456 spin_unlock(&ci->i_ceph_lock);
2457 if (snap_rwsem_locked) {
2458 up_read(&mdsc->snap_rwsem);
2459 snap_rwsem_locked = false;
2461 __ceph_do_pending_vmtruncate(inode);
2462 spin_lock(&ci->i_ceph_lock);
2465 have = __ceph_caps_issued(ci, &implemented);
2467 if (have & need & CEPH_CAP_FILE_WR) {
2468 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2469 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2470 inode, endoff, ci->i_max_size);
2471 if (endoff > ci->i_requested_max_size) {
2478 * If a sync write is in progress, we must wait, so that we
2479 * can get a final snapshot value for size+mtime.
2481 if (__ceph_have_pending_cap_snap(ci)) {
2482 dout("get_cap_refs %p cap_snap_pending\n", inode);
2487 if ((have & need) == need) {
2489 * Look at (implemented & ~have & not) so that we keep waiting
2490 * on transition from wanted -> needed caps. This is needed
2491 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2492 * going before a prior buffered writeback happens.
2494 int not = want & ~(have & need);
2495 int revoking = implemented & ~have;
2496 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2497 inode, ceph_cap_string(have), ceph_cap_string(not),
2498 ceph_cap_string(revoking));
2499 if ((revoking & not) == 0) {
2500 if (!snap_rwsem_locked &&
2501 !ci->i_head_snapc &&
2502 (need & CEPH_CAP_FILE_WR)) {
2503 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2505 * we can not call down_read() when
2506 * task isn't in TASK_RUNNING state
2514 spin_unlock(&ci->i_ceph_lock);
2515 down_read(&mdsc->snap_rwsem);
2516 snap_rwsem_locked = true;
2519 snap_rwsem_locked = true;
2521 *got = need | (have & want);
2522 if ((need & CEPH_CAP_FILE_RD) &&
2523 !(*got & CEPH_CAP_FILE_CACHE))
2524 ceph_disable_fscache_readpage(ci);
2525 __take_cap_refs(ci, *got, true);
2529 int session_readonly = false;
2530 if ((need & CEPH_CAP_FILE_WR) && ci->i_auth_cap) {
2531 struct ceph_mds_session *s = ci->i_auth_cap->session;
2532 spin_lock(&s->s_cap_lock);
2533 session_readonly = s->s_readonly;
2534 spin_unlock(&s->s_cap_lock);
2536 if (session_readonly) {
2537 dout("get_cap_refs %p needed %s but mds%d readonly\n",
2538 inode, ceph_cap_string(need), ci->i_auth_cap->mds);
2544 if (ci->i_ceph_flags & CEPH_I_CAP_DROPPED) {
2546 if (READ_ONCE(mdsc->fsc->mount_state) ==
2547 CEPH_MOUNT_SHUTDOWN) {
2548 dout("get_cap_refs %p forced umount\n", inode);
2553 mds_wanted = __ceph_caps_mds_wanted(ci, false);
2554 if (need & ~(mds_wanted & need)) {
2555 dout("get_cap_refs %p caps were dropped"
2556 " (session killed?)\n", inode);
2561 if (!(file_wanted & ~mds_wanted))
2562 ci->i_ceph_flags &= ~CEPH_I_CAP_DROPPED;
2565 dout("get_cap_refs %p have %s needed %s\n", inode,
2566 ceph_cap_string(have), ceph_cap_string(need));
2569 spin_unlock(&ci->i_ceph_lock);
2570 if (snap_rwsem_locked)
2571 up_read(&mdsc->snap_rwsem);
2573 dout("get_cap_refs %p ret %d got %s\n", inode,
2574 ret, ceph_cap_string(*got));
2579 * Check the offset we are writing up to against our current
2580 * max_size. If necessary, tell the MDS we want to write to
2583 static void check_max_size(struct inode *inode, loff_t endoff)
2585 struct ceph_inode_info *ci = ceph_inode(inode);
2588 /* do we need to explicitly request a larger max_size? */
2589 spin_lock(&ci->i_ceph_lock);
2590 if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
2591 dout("write %p at large endoff %llu, req max_size\n",
2593 ci->i_wanted_max_size = endoff;
2595 /* duplicate ceph_check_caps()'s logic */
2596 if (ci->i_auth_cap &&
2597 (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
2598 ci->i_wanted_max_size > ci->i_max_size &&
2599 ci->i_wanted_max_size > ci->i_requested_max_size)
2601 spin_unlock(&ci->i_ceph_lock);
2603 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2606 int ceph_try_get_caps(struct ceph_inode_info *ci, int need, int want, int *got)
2610 BUG_ON(need & ~CEPH_CAP_FILE_RD);
2611 BUG_ON(want & ~(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
2612 ret = ceph_pool_perm_check(ci, need);
2616 ret = try_get_cap_refs(ci, need, want, 0, true, got, &err);
2618 if (err == -EAGAIN) {
2620 } else if (err < 0) {
2628 * Wait for caps, and take cap references. If we can't get a WR cap
2629 * due to a small max_size, make sure we check_max_size (and possibly
2630 * ask the mds) so we don't get hung up indefinitely.
2632 int ceph_get_caps(struct ceph_inode_info *ci, int need, int want,
2633 loff_t endoff, int *got, struct page **pinned_page)
2635 int _got, ret, err = 0;
2637 ret = ceph_pool_perm_check(ci, need);
2643 check_max_size(&ci->vfs_inode, endoff);
2647 ret = try_get_cap_refs(ci, need, want, endoff,
2648 false, &_got, &err);
2655 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2656 add_wait_queue(&ci->i_cap_wq, &wait);
2658 while (!try_get_cap_refs(ci, need, want, endoff,
2659 true, &_got, &err)) {
2660 if (signal_pending(current)) {
2664 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
2667 remove_wait_queue(&ci->i_cap_wq, &wait);
2675 if (err == -ESTALE) {
2676 /* session was killed, try renew caps */
2677 ret = ceph_renew_caps(&ci->vfs_inode);
2684 if (ci->i_inline_version != CEPH_INLINE_NONE &&
2685 (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
2686 i_size_read(&ci->vfs_inode) > 0) {
2688 find_get_page(ci->vfs_inode.i_mapping, 0);
2690 if (PageUptodate(page)) {
2691 *pinned_page = page;
2697 * drop cap refs first because getattr while
2698 * holding * caps refs can cause deadlock.
2700 ceph_put_cap_refs(ci, _got);
2704 * getattr request will bring inline data into
2707 ret = __ceph_do_getattr(&ci->vfs_inode, NULL,
2708 CEPH_STAT_CAP_INLINE_DATA,
2717 if ((_got & CEPH_CAP_FILE_RD) && (_got & CEPH_CAP_FILE_CACHE))
2718 ceph_fscache_revalidate_cookie(ci);
2725 * Take cap refs. Caller must already know we hold at least one ref
2726 * on the caps in question or we don't know this is safe.
2728 void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2730 spin_lock(&ci->i_ceph_lock);
2731 __take_cap_refs(ci, caps, false);
2732 spin_unlock(&ci->i_ceph_lock);
2737 * drop cap_snap that is not associated with any snapshot.
2738 * we don't need to send FLUSHSNAP message for it.
2740 static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci,
2741 struct ceph_cap_snap *capsnap)
2743 if (!capsnap->need_flush &&
2744 !capsnap->writing && !capsnap->dirty_pages) {
2745 dout("dropping cap_snap %p follows %llu\n",
2746 capsnap, capsnap->follows);
2747 BUG_ON(capsnap->cap_flush.tid > 0);
2748 ceph_put_snap_context(capsnap->context);
2749 if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps))
2750 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2752 list_del(&capsnap->ci_item);
2753 ceph_put_cap_snap(capsnap);
2762 * If we released the last ref on any given cap, call ceph_check_caps
2763 * to release (or schedule a release).
2765 * If we are releasing a WR cap (from a sync write), finalize any affected
2766 * cap_snap, and wake up any waiters.
2768 void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2770 struct inode *inode = &ci->vfs_inode;
2771 int last = 0, put = 0, flushsnaps = 0, wake = 0;
2773 spin_lock(&ci->i_ceph_lock);
2774 if (had & CEPH_CAP_PIN)
2776 if (had & CEPH_CAP_FILE_RD)
2777 if (--ci->i_rd_ref == 0)
2779 if (had & CEPH_CAP_FILE_CACHE)
2780 if (--ci->i_rdcache_ref == 0)
2782 if (had & CEPH_CAP_FILE_BUFFER) {
2783 if (--ci->i_wb_ref == 0) {
2787 dout("put_cap_refs %p wb %d -> %d (?)\n",
2788 inode, ci->i_wb_ref+1, ci->i_wb_ref);
2790 if (had & CEPH_CAP_FILE_WR)
2791 if (--ci->i_wr_ref == 0) {
2793 if (__ceph_have_pending_cap_snap(ci)) {
2794 struct ceph_cap_snap *capsnap =
2795 list_last_entry(&ci->i_cap_snaps,
2796 struct ceph_cap_snap,
2798 capsnap->writing = 0;
2799 if (ceph_try_drop_cap_snap(ci, capsnap))
2801 else if (__ceph_finish_cap_snap(ci, capsnap))
2805 if (ci->i_wrbuffer_ref_head == 0 &&
2806 ci->i_dirty_caps == 0 &&
2807 ci->i_flushing_caps == 0) {
2808 BUG_ON(!ci->i_head_snapc);
2809 ceph_put_snap_context(ci->i_head_snapc);
2810 ci->i_head_snapc = NULL;
2812 /* see comment in __ceph_remove_cap() */
2813 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm)
2814 drop_inode_snap_realm(ci);
2816 spin_unlock(&ci->i_ceph_lock);
2818 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2819 last ? " last" : "", put ? " put" : "");
2821 if (last && !flushsnaps)
2822 ceph_check_caps(ci, 0, NULL);
2823 else if (flushsnaps)
2824 ceph_flush_snaps(ci, NULL);
2826 wake_up_all(&ci->i_cap_wq);
2832 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2833 * context. Adjust per-snap dirty page accounting as appropriate.
2834 * Once all dirty data for a cap_snap is flushed, flush snapped file
2835 * metadata back to the MDS. If we dropped the last ref, call
2838 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2839 struct ceph_snap_context *snapc)
2841 struct inode *inode = &ci->vfs_inode;
2842 struct ceph_cap_snap *capsnap = NULL;
2846 bool flush_snaps = false;
2847 bool complete_capsnap = false;
2849 spin_lock(&ci->i_ceph_lock);
2850 ci->i_wrbuffer_ref -= nr;
2851 if (ci->i_wrbuffer_ref == 0) {
2856 if (ci->i_head_snapc == snapc) {
2857 ci->i_wrbuffer_ref_head -= nr;
2858 if (ci->i_wrbuffer_ref_head == 0 &&
2859 ci->i_wr_ref == 0 &&
2860 ci->i_dirty_caps == 0 &&
2861 ci->i_flushing_caps == 0) {
2862 BUG_ON(!ci->i_head_snapc);
2863 ceph_put_snap_context(ci->i_head_snapc);
2864 ci->i_head_snapc = NULL;
2866 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2868 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2869 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2870 last ? " LAST" : "");
2872 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2873 if (capsnap->context == snapc) {
2879 capsnap->dirty_pages -= nr;
2880 if (capsnap->dirty_pages == 0) {
2881 complete_capsnap = true;
2882 if (!capsnap->writing) {
2883 if (ceph_try_drop_cap_snap(ci, capsnap)) {
2886 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2891 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2892 " snap %lld %d/%d -> %d/%d %s%s\n",
2893 inode, capsnap, capsnap->context->seq,
2894 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2895 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2896 last ? " (wrbuffer last)" : "",
2897 complete_capsnap ? " (complete capsnap)" : "");
2900 spin_unlock(&ci->i_ceph_lock);
2903 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2904 } else if (flush_snaps) {
2905 ceph_flush_snaps(ci, NULL);
2907 if (complete_capsnap)
2908 wake_up_all(&ci->i_cap_wq);
2914 * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
2916 static void invalidate_aliases(struct inode *inode)
2918 struct dentry *dn, *prev = NULL;
2920 dout("invalidate_aliases inode %p\n", inode);
2921 d_prune_aliases(inode);
2923 * For non-directory inode, d_find_alias() only returns
2924 * hashed dentry. After calling d_invalidate(), the
2925 * dentry becomes unhashed.
2927 * For directory inode, d_find_alias() can return
2928 * unhashed dentry. But directory inode should have
2929 * one alias at most.
2931 while ((dn = d_find_alias(inode))) {
2946 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2947 * actually be a revocation if it specifies a smaller cap set.)
2949 * caller holds s_mutex and i_ceph_lock, we drop both.
2951 static void handle_cap_grant(struct ceph_mds_client *mdsc,
2952 struct inode *inode, struct ceph_mds_caps *grant,
2953 struct ceph_string **pns, u64 inline_version,
2954 void *inline_data, u32 inline_len,
2955 struct ceph_buffer *xattr_buf,
2956 struct ceph_mds_session *session,
2957 struct ceph_cap *cap, int issued)
2958 __releases(ci->i_ceph_lock)
2959 __releases(mdsc->snap_rwsem)
2961 struct ceph_inode_info *ci = ceph_inode(inode);
2962 int mds = session->s_mds;
2963 int seq = le32_to_cpu(grant->seq);
2964 int newcaps = le32_to_cpu(grant->caps);
2965 int used, wanted, dirty;
2966 u64 size = le64_to_cpu(grant->size);
2967 u64 max_size = le64_to_cpu(grant->max_size);
2968 struct timespec mtime, atime, ctime;
2971 bool writeback = false;
2972 bool queue_trunc = false;
2973 bool queue_invalidate = false;
2974 bool deleted_inode = false;
2975 bool fill_inline = false;
2977 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2978 inode, cap, mds, seq, ceph_cap_string(newcaps));
2979 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
2984 * auth mds of the inode changed. we received the cap export message,
2985 * but still haven't received the cap import message. handle_cap_export
2986 * updated the new auth MDS' cap.
2988 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
2989 * that was sent before the cap import message. So don't remove caps.
2991 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
2992 WARN_ON(cap != ci->i_auth_cap);
2993 WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
2995 newcaps |= cap->issued;
2999 * If CACHE is being revoked, and we have no dirty buffers,
3000 * try to invalidate (once). (If there are dirty buffers, we
3001 * will invalidate _after_ writeback.)
3003 if (!S_ISDIR(inode->i_mode) && /* don't invalidate readdir cache */
3004 ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
3005 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3006 !(ci->i_wrbuffer_ref || ci->i_wb_ref)) {
3007 if (try_nonblocking_invalidate(inode)) {
3008 /* there were locked pages.. invalidate later
3009 in a separate thread. */
3010 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
3011 queue_invalidate = true;
3012 ci->i_rdcache_revoking = ci->i_rdcache_gen;
3017 /* side effects now are allowed */
3018 cap->cap_gen = session->s_cap_gen;
3021 __check_cap_issue(ci, cap, newcaps);
3023 if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
3024 (issued & CEPH_CAP_AUTH_EXCL) == 0) {
3025 inode->i_mode = le32_to_cpu(grant->mode);
3026 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
3027 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
3028 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
3029 from_kuid(&init_user_ns, inode->i_uid),
3030 from_kgid(&init_user_ns, inode->i_gid));
3033 if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
3034 (issued & CEPH_CAP_LINK_EXCL) == 0) {
3035 set_nlink(inode, le32_to_cpu(grant->nlink));
3036 if (inode->i_nlink == 0 &&
3037 (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
3038 deleted_inode = true;
3041 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
3042 int len = le32_to_cpu(grant->xattr_len);
3043 u64 version = le64_to_cpu(grant->xattr_version);
3045 if (version > ci->i_xattrs.version) {
3046 dout(" got new xattrs v%llu on %p len %d\n",
3047 version, inode, len);
3048 if (ci->i_xattrs.blob)
3049 ceph_buffer_put(ci->i_xattrs.blob);
3050 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
3051 ci->i_xattrs.version = version;
3052 ceph_forget_all_cached_acls(inode);
3056 if (newcaps & CEPH_CAP_ANY_RD) {
3057 /* ctime/mtime/atime? */
3058 ceph_decode_timespec(&mtime, &grant->mtime);
3059 ceph_decode_timespec(&atime, &grant->atime);
3060 ceph_decode_timespec(&ctime, &grant->ctime);
3061 ceph_fill_file_time(inode, issued,
3062 le32_to_cpu(grant->time_warp_seq),
3063 &ctime, &mtime, &atime);
3066 if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
3067 /* file layout may have changed */
3068 s64 old_pool = ci->i_layout.pool_id;
3069 struct ceph_string *old_ns;
3071 ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout);
3072 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
3073 lockdep_is_held(&ci->i_ceph_lock));
3074 rcu_assign_pointer(ci->i_layout.pool_ns, *pns);
3076 if (ci->i_layout.pool_id != old_pool || *pns != old_ns)
3077 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
3081 /* size/truncate_seq? */
3082 queue_trunc = ceph_fill_file_size(inode, issued,
3083 le32_to_cpu(grant->truncate_seq),
3084 le64_to_cpu(grant->truncate_size),
3088 if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) {
3089 if (max_size != ci->i_max_size) {
3090 dout("max_size %lld -> %llu\n",
3091 ci->i_max_size, max_size);
3092 ci->i_max_size = max_size;
3093 if (max_size >= ci->i_wanted_max_size) {
3094 ci->i_wanted_max_size = 0; /* reset */
3095 ci->i_requested_max_size = 0;
3098 } else if (ci->i_wanted_max_size > ci->i_max_size &&
3099 ci->i_wanted_max_size > ci->i_requested_max_size) {
3100 /* CEPH_CAP_OP_IMPORT */
3105 /* check cap bits */
3106 wanted = __ceph_caps_wanted(ci);
3107 used = __ceph_caps_used(ci);
3108 dirty = __ceph_caps_dirty(ci);
3109 dout(" my wanted = %s, used = %s, dirty %s\n",
3110 ceph_cap_string(wanted),
3111 ceph_cap_string(used),
3112 ceph_cap_string(dirty));
3113 if (wanted != le32_to_cpu(grant->wanted)) {
3114 dout("mds wanted %s -> %s\n",
3115 ceph_cap_string(le32_to_cpu(grant->wanted)),
3116 ceph_cap_string(wanted));
3117 /* imported cap may not have correct mds_wanted */
3118 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT)
3122 /* revocation, grant, or no-op? */
3123 if (cap->issued & ~newcaps) {
3124 int revoking = cap->issued & ~newcaps;
3126 dout("revocation: %s -> %s (revoking %s)\n",
3127 ceph_cap_string(cap->issued),
3128 ceph_cap_string(newcaps),
3129 ceph_cap_string(revoking));
3130 if (revoking & used & CEPH_CAP_FILE_BUFFER)
3131 writeback = true; /* initiate writeback; will delay ack */
3132 else if (revoking == CEPH_CAP_FILE_CACHE &&
3133 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3135 ; /* do nothing yet, invalidation will be queued */
3136 else if (cap == ci->i_auth_cap)
3137 check_caps = 1; /* check auth cap only */
3139 check_caps = 2; /* check all caps */
3140 cap->issued = newcaps;
3141 cap->implemented |= newcaps;
3142 } else if (cap->issued == newcaps) {
3143 dout("caps unchanged: %s -> %s\n",
3144 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
3146 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
3147 ceph_cap_string(newcaps));
3148 /* non-auth MDS is revoking the newly grant caps ? */
3149 if (cap == ci->i_auth_cap &&
3150 __ceph_caps_revoking_other(ci, cap, newcaps))
3153 cap->issued = newcaps;
3154 cap->implemented |= newcaps; /* add bits only, to
3155 * avoid stepping on a
3156 * pending revocation */
3159 BUG_ON(cap->issued & ~cap->implemented);
3161 if (inline_version > 0 && inline_version >= ci->i_inline_version) {
3162 ci->i_inline_version = inline_version;
3163 if (ci->i_inline_version != CEPH_INLINE_NONE &&
3164 (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
3168 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
3169 if (newcaps & ~issued)
3171 kick_flushing_inode_caps(mdsc, session, inode);
3172 up_read(&mdsc->snap_rwsem);
3174 spin_unlock(&ci->i_ceph_lock);
3178 ceph_fill_inline_data(inode, NULL, inline_data, inline_len);
3181 ceph_queue_vmtruncate(inode);
3185 * queue inode for writeback: we can't actually call
3186 * filemap_write_and_wait, etc. from message handler
3189 ceph_queue_writeback(inode);
3190 if (queue_invalidate)
3191 ceph_queue_invalidate(inode);
3193 invalidate_aliases(inode);
3195 wake_up_all(&ci->i_cap_wq);
3197 if (check_caps == 1)
3198 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
3200 else if (check_caps == 2)
3201 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
3203 mutex_unlock(&session->s_mutex);
3207 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
3208 * MDS has been safely committed.
3210 static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
3211 struct ceph_mds_caps *m,
3212 struct ceph_mds_session *session,
3213 struct ceph_cap *cap)
3214 __releases(ci->i_ceph_lock)
3216 struct ceph_inode_info *ci = ceph_inode(inode);
3217 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3218 struct ceph_cap_flush *cf, *tmp_cf;
3219 LIST_HEAD(to_remove);
3220 unsigned seq = le32_to_cpu(m->seq);
3221 int dirty = le32_to_cpu(m->dirty);
3227 list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
3228 if (cf->tid == flush_tid)
3230 if (cf->caps == 0) /* capsnap */
3232 if (cf->tid <= flush_tid) {
3233 if (__finish_cap_flush(NULL, ci, cf))
3235 list_add_tail(&cf->i_list, &to_remove);
3237 cleaned &= ~cf->caps;
3243 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
3244 " flushing %s -> %s\n",
3245 inode, session->s_mds, seq, ceph_cap_string(dirty),
3246 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
3247 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
3249 if (list_empty(&to_remove) && !cleaned)
3252 ci->i_flushing_caps &= ~cleaned;
3254 spin_lock(&mdsc->cap_dirty_lock);
3256 list_for_each_entry(cf, &to_remove, i_list) {
3257 if (__finish_cap_flush(mdsc, NULL, cf))
3261 if (ci->i_flushing_caps == 0) {
3262 if (list_empty(&ci->i_cap_flush_list)) {
3263 list_del_init(&ci->i_flushing_item);
3264 if (!list_empty(&session->s_cap_flushing)) {
3265 dout(" mds%d still flushing cap on %p\n",
3267 &list_first_entry(&session->s_cap_flushing,
3268 struct ceph_inode_info,
3269 i_flushing_item)->vfs_inode);
3272 mdsc->num_cap_flushing--;
3273 dout(" inode %p now !flushing\n", inode);
3275 if (ci->i_dirty_caps == 0) {
3276 dout(" inode %p now clean\n", inode);
3277 BUG_ON(!list_empty(&ci->i_dirty_item));
3279 if (ci->i_wr_ref == 0 &&
3280 ci->i_wrbuffer_ref_head == 0) {
3281 BUG_ON(!ci->i_head_snapc);
3282 ceph_put_snap_context(ci->i_head_snapc);
3283 ci->i_head_snapc = NULL;
3286 BUG_ON(list_empty(&ci->i_dirty_item));
3289 spin_unlock(&mdsc->cap_dirty_lock);
3292 spin_unlock(&ci->i_ceph_lock);
3294 while (!list_empty(&to_remove)) {
3295 cf = list_first_entry(&to_remove,
3296 struct ceph_cap_flush, i_list);
3297 list_del(&cf->i_list);
3298 ceph_free_cap_flush(cf);
3302 wake_up_all(&ci->i_cap_wq);
3304 wake_up_all(&mdsc->cap_flushing_wq);
3310 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
3311 * throw away our cap_snap.
3313 * Caller hold s_mutex.
3315 static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
3316 struct ceph_mds_caps *m,
3317 struct ceph_mds_session *session)
3319 struct ceph_inode_info *ci = ceph_inode(inode);
3320 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3321 u64 follows = le64_to_cpu(m->snap_follows);
3322 struct ceph_cap_snap *capsnap;
3323 bool flushed = false;
3324 bool wake_ci = false;
3325 bool wake_mdsc = false;
3327 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
3328 inode, ci, session->s_mds, follows);
3330 spin_lock(&ci->i_ceph_lock);
3331 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
3332 if (capsnap->follows == follows) {
3333 if (capsnap->cap_flush.tid != flush_tid) {
3334 dout(" cap_snap %p follows %lld tid %lld !="
3335 " %lld\n", capsnap, follows,
3336 flush_tid, capsnap->cap_flush.tid);
3342 dout(" skipping cap_snap %p follows %lld\n",
3343 capsnap, capsnap->follows);
3347 WARN_ON(capsnap->dirty_pages || capsnap->writing);
3348 dout(" removing %p cap_snap %p follows %lld\n",
3349 inode, capsnap, follows);
3350 list_del(&capsnap->ci_item);
3351 if (__finish_cap_flush(NULL, ci, &capsnap->cap_flush))
3354 spin_lock(&mdsc->cap_dirty_lock);
3356 if (list_empty(&ci->i_cap_flush_list))
3357 list_del_init(&ci->i_flushing_item);
3359 if (__finish_cap_flush(mdsc, NULL, &capsnap->cap_flush))
3362 spin_unlock(&mdsc->cap_dirty_lock);
3364 spin_unlock(&ci->i_ceph_lock);
3366 ceph_put_snap_context(capsnap->context);
3367 ceph_put_cap_snap(capsnap);
3369 wake_up_all(&ci->i_cap_wq);
3371 wake_up_all(&mdsc->cap_flushing_wq);
3377 * Handle TRUNC from MDS, indicating file truncation.
3379 * caller hold s_mutex.
3381 static void handle_cap_trunc(struct inode *inode,
3382 struct ceph_mds_caps *trunc,
3383 struct ceph_mds_session *session)
3384 __releases(ci->i_ceph_lock)
3386 struct ceph_inode_info *ci = ceph_inode(inode);
3387 int mds = session->s_mds;
3388 int seq = le32_to_cpu(trunc->seq);
3389 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
3390 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
3391 u64 size = le64_to_cpu(trunc->size);
3392 int implemented = 0;
3393 int dirty = __ceph_caps_dirty(ci);
3394 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
3395 int queue_trunc = 0;
3397 issued |= implemented | dirty;
3399 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
3400 inode, mds, seq, truncate_size, truncate_seq);
3401 queue_trunc = ceph_fill_file_size(inode, issued,
3402 truncate_seq, truncate_size, size);
3403 spin_unlock(&ci->i_ceph_lock);
3406 ceph_queue_vmtruncate(inode);
3410 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
3411 * different one. If we are the most recent migration we've seen (as
3412 * indicated by mseq), make note of the migrating cap bits for the
3413 * duration (until we see the corresponding IMPORT).
3415 * caller holds s_mutex
3417 static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
3418 struct ceph_mds_cap_peer *ph,
3419 struct ceph_mds_session *session)
3421 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
3422 struct ceph_mds_session *tsession = NULL;
3423 struct ceph_cap *cap, *tcap, *new_cap = NULL;
3424 struct ceph_inode_info *ci = ceph_inode(inode);
3426 unsigned mseq = le32_to_cpu(ex->migrate_seq);
3427 unsigned t_seq, t_mseq;
3429 int mds = session->s_mds;
3432 t_cap_id = le64_to_cpu(ph->cap_id);
3433 t_seq = le32_to_cpu(ph->seq);
3434 t_mseq = le32_to_cpu(ph->mseq);
3435 target = le32_to_cpu(ph->mds);
3437 t_cap_id = t_seq = t_mseq = 0;
3441 dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n",
3442 inode, ci, mds, mseq, target);
3444 spin_lock(&ci->i_ceph_lock);
3445 cap = __get_cap_for_mds(ci, mds);
3446 if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
3450 __ceph_remove_cap(cap, false);
3451 if (!ci->i_auth_cap)
3452 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
3457 * now we know we haven't received the cap import message yet
3458 * because the exported cap still exist.
3461 issued = cap->issued;
3462 WARN_ON(issued != cap->implemented);
3464 tcap = __get_cap_for_mds(ci, target);
3466 /* already have caps from the target */
3467 if (tcap->cap_id == t_cap_id &&
3468 ceph_seq_cmp(tcap->seq, t_seq) < 0) {
3469 dout(" updating import cap %p mds%d\n", tcap, target);
3470 tcap->cap_id = t_cap_id;
3471 tcap->seq = t_seq - 1;
3472 tcap->issue_seq = t_seq - 1;
3473 tcap->issued |= issued;
3474 tcap->implemented |= issued;
3475 if (cap == ci->i_auth_cap)
3476 ci->i_auth_cap = tcap;
3478 if (!list_empty(&ci->i_cap_flush_list) &&
3479 ci->i_auth_cap == tcap) {
3480 spin_lock(&mdsc->cap_dirty_lock);
3481 list_move_tail(&ci->i_flushing_item,
3482 &tcap->session->s_cap_flushing);
3483 spin_unlock(&mdsc->cap_dirty_lock);
3486 __ceph_remove_cap(cap, false);
3488 } else if (tsession) {
3489 /* add placeholder for the export tagert */
3490 int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
3492 ceph_add_cap(inode, tsession, t_cap_id, -1, issued, 0,
3493 t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
3495 if (!list_empty(&ci->i_cap_flush_list) &&
3496 ci->i_auth_cap == tcap) {
3497 spin_lock(&mdsc->cap_dirty_lock);
3498 list_move_tail(&ci->i_flushing_item,
3499 &tcap->session->s_cap_flushing);
3500 spin_unlock(&mdsc->cap_dirty_lock);
3503 __ceph_remove_cap(cap, false);
3507 spin_unlock(&ci->i_ceph_lock);
3508 mutex_unlock(&session->s_mutex);
3510 /* open target session */
3511 tsession = ceph_mdsc_open_export_target_session(mdsc, target);
3512 if (!IS_ERR(tsession)) {
3514 mutex_lock(&session->s_mutex);
3515 mutex_lock_nested(&tsession->s_mutex,
3516 SINGLE_DEPTH_NESTING);
3518 mutex_lock(&tsession->s_mutex);
3519 mutex_lock_nested(&session->s_mutex,
3520 SINGLE_DEPTH_NESTING);
3522 new_cap = ceph_get_cap(mdsc, NULL);
3527 mutex_lock(&session->s_mutex);
3532 spin_unlock(&ci->i_ceph_lock);
3533 mutex_unlock(&session->s_mutex);
3535 mutex_unlock(&tsession->s_mutex);
3536 ceph_put_mds_session(tsession);
3539 ceph_put_cap(mdsc, new_cap);
3543 * Handle cap IMPORT.
3545 * caller holds s_mutex. acquires i_ceph_lock
3547 static void handle_cap_import(struct ceph_mds_client *mdsc,
3548 struct inode *inode, struct ceph_mds_caps *im,
3549 struct ceph_mds_cap_peer *ph,
3550 struct ceph_mds_session *session,
3551 struct ceph_cap **target_cap, int *old_issued)
3552 __acquires(ci->i_ceph_lock)
3554 struct ceph_inode_info *ci = ceph_inode(inode);
3555 struct ceph_cap *cap, *ocap, *new_cap = NULL;
3556 int mds = session->s_mds;
3558 unsigned caps = le32_to_cpu(im->caps);
3559 unsigned wanted = le32_to_cpu(im->wanted);
3560 unsigned seq = le32_to_cpu(im->seq);
3561 unsigned mseq = le32_to_cpu(im->migrate_seq);
3562 u64 realmino = le64_to_cpu(im->realm);
3563 u64 cap_id = le64_to_cpu(im->cap_id);
3568 p_cap_id = le64_to_cpu(ph->cap_id);
3569 peer = le32_to_cpu(ph->mds);
3575 dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n",
3576 inode, ci, mds, mseq, peer);
3579 spin_lock(&ci->i_ceph_lock);
3580 cap = __get_cap_for_mds(ci, mds);
3583 spin_unlock(&ci->i_ceph_lock);
3584 new_cap = ceph_get_cap(mdsc, NULL);
3590 ceph_put_cap(mdsc, new_cap);
3595 __ceph_caps_issued(ci, &issued);
3596 issued |= __ceph_caps_dirty(ci);
3598 ceph_add_cap(inode, session, cap_id, -1, caps, wanted, seq, mseq,
3599 realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
3601 ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
3602 if (ocap && ocap->cap_id == p_cap_id) {
3603 dout(" remove export cap %p mds%d flags %d\n",
3604 ocap, peer, ph->flags);
3605 if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
3606 (ocap->seq != le32_to_cpu(ph->seq) ||
3607 ocap->mseq != le32_to_cpu(ph->mseq))) {
3608 pr_err("handle_cap_import: mismatched seq/mseq: "
3609 "ino (%llx.%llx) mds%d seq %d mseq %d "
3610 "importer mds%d has peer seq %d mseq %d\n",
3611 ceph_vinop(inode), peer, ocap->seq,
3612 ocap->mseq, mds, le32_to_cpu(ph->seq),
3613 le32_to_cpu(ph->mseq));
3615 __ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
3618 /* make sure we re-request max_size, if necessary */
3619 ci->i_requested_max_size = 0;
3621 *old_issued = issued;
3626 * Handle a caps message from the MDS.
3628 * Identify the appropriate session, inode, and call the right handler
3629 * based on the cap op.
3631 void ceph_handle_caps(struct ceph_mds_session *session,
3632 struct ceph_msg *msg)
3634 struct ceph_mds_client *mdsc = session->s_mdsc;
3635 struct super_block *sb = mdsc->fsc->sb;
3636 struct inode *inode;
3637 struct ceph_inode_info *ci;
3638 struct ceph_cap *cap;
3639 struct ceph_mds_caps *h;
3640 struct ceph_mds_cap_peer *peer = NULL;
3641 struct ceph_snap_realm *realm = NULL;
3642 struct ceph_string *pool_ns = NULL;
3643 int mds = session->s_mds;
3646 struct ceph_vino vino;
3648 u64 inline_version = 0;
3649 void *inline_data = NULL;
3652 size_t snaptrace_len;
3655 dout("handle_caps from mds%d\n", mds);
3658 end = msg->front.iov_base + msg->front.iov_len;
3659 tid = le64_to_cpu(msg->hdr.tid);
3660 if (msg->front.iov_len < sizeof(*h))
3662 h = msg->front.iov_base;
3663 op = le32_to_cpu(h->op);
3664 vino.ino = le64_to_cpu(h->ino);
3665 vino.snap = CEPH_NOSNAP;
3666 seq = le32_to_cpu(h->seq);
3667 mseq = le32_to_cpu(h->migrate_seq);
3670 snaptrace_len = le32_to_cpu(h->snap_trace_len);
3671 p = snaptrace + snaptrace_len;
3673 if (le16_to_cpu(msg->hdr.version) >= 2) {
3675 ceph_decode_32_safe(&p, end, flock_len, bad);
3676 if (p + flock_len > end)
3681 if (le16_to_cpu(msg->hdr.version) >= 3) {
3682 if (op == CEPH_CAP_OP_IMPORT) {
3683 if (p + sizeof(*peer) > end)
3687 } else if (op == CEPH_CAP_OP_EXPORT) {
3688 /* recorded in unused fields */
3689 peer = (void *)&h->size;
3693 if (le16_to_cpu(msg->hdr.version) >= 4) {
3694 ceph_decode_64_safe(&p, end, inline_version, bad);
3695 ceph_decode_32_safe(&p, end, inline_len, bad);
3696 if (p + inline_len > end)
3702 if (le16_to_cpu(msg->hdr.version) >= 5) {
3703 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
3706 ceph_decode_32_safe(&p, end, epoch_barrier, bad);
3707 ceph_osdc_update_epoch_barrier(osdc, epoch_barrier);
3710 if (le16_to_cpu(msg->hdr.version) >= 8) {
3712 u32 caller_uid, caller_gid;
3716 ceph_decode_64_safe(&p, end, flush_tid, bad);
3718 ceph_decode_32_safe(&p, end, caller_uid, bad);
3719 ceph_decode_32_safe(&p, end, caller_gid, bad);
3721 ceph_decode_32_safe(&p, end, pool_ns_len, bad);
3722 if (pool_ns_len > 0) {
3723 ceph_decode_need(&p, end, pool_ns_len, bad);
3724 pool_ns = ceph_find_or_create_string(p, pool_ns_len);
3730 inode = ceph_find_inode(sb, vino);
3731 ci = ceph_inode(inode);
3732 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
3735 mutex_lock(&session->s_mutex);
3737 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
3741 dout(" i don't have ino %llx\n", vino.ino);
3743 if (op == CEPH_CAP_OP_IMPORT) {
3744 cap = ceph_get_cap(mdsc, NULL);
3745 cap->cap_ino = vino.ino;
3746 cap->queue_release = 1;
3747 cap->cap_id = le64_to_cpu(h->cap_id);
3750 cap->issue_seq = seq;
3751 spin_lock(&session->s_cap_lock);
3752 list_add_tail(&cap->session_caps,
3753 &session->s_cap_releases);
3754 session->s_num_cap_releases++;
3755 spin_unlock(&session->s_cap_lock);
3757 goto flush_cap_releases;
3760 /* these will work even if we don't have a cap yet */
3762 case CEPH_CAP_OP_FLUSHSNAP_ACK:
3763 handle_cap_flushsnap_ack(inode, tid, h, session);
3766 case CEPH_CAP_OP_EXPORT:
3767 handle_cap_export(inode, h, peer, session);
3770 case CEPH_CAP_OP_IMPORT:
3772 if (snaptrace_len) {
3773 down_write(&mdsc->snap_rwsem);
3774 ceph_update_snap_trace(mdsc, snaptrace,
3775 snaptrace + snaptrace_len,
3777 downgrade_write(&mdsc->snap_rwsem);
3779 down_read(&mdsc->snap_rwsem);
3781 handle_cap_import(mdsc, inode, h, peer, session,
3783 handle_cap_grant(mdsc, inode, h, &pool_ns,
3784 inline_version, inline_data, inline_len,
3785 msg->middle, session, cap, issued);
3787 ceph_put_snap_realm(mdsc, realm);
3791 /* the rest require a cap */
3792 spin_lock(&ci->i_ceph_lock);
3793 cap = __get_cap_for_mds(ceph_inode(inode), mds);
3795 dout(" no cap on %p ino %llx.%llx from mds%d\n",
3796 inode, ceph_ino(inode), ceph_snap(inode), mds);
3797 spin_unlock(&ci->i_ceph_lock);
3798 goto flush_cap_releases;
3801 /* note that each of these drops i_ceph_lock for us */
3803 case CEPH_CAP_OP_REVOKE:
3804 case CEPH_CAP_OP_GRANT:
3805 __ceph_caps_issued(ci, &issued);
3806 issued |= __ceph_caps_dirty(ci);
3807 handle_cap_grant(mdsc, inode, h, &pool_ns,
3808 inline_version, inline_data, inline_len,
3809 msg->middle, session, cap, issued);
3812 case CEPH_CAP_OP_FLUSH_ACK:
3813 handle_cap_flush_ack(inode, tid, h, session, cap);
3816 case CEPH_CAP_OP_TRUNC:
3817 handle_cap_trunc(inode, h, session);
3821 spin_unlock(&ci->i_ceph_lock);
3822 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
3823 ceph_cap_op_name(op));
3830 * send any cap release message to try to move things
3831 * along for the mds (who clearly thinks we still have this
3834 ceph_send_cap_releases(mdsc, session);
3837 mutex_unlock(&session->s_mutex);
3840 ceph_put_string(pool_ns);
3844 pr_err("ceph_handle_caps: corrupt message\n");
3850 * Delayed work handler to process end of delayed cap release LRU list.
3852 void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
3854 struct inode *inode;
3855 struct ceph_inode_info *ci;
3856 int flags = CHECK_CAPS_NODELAY;
3858 dout("check_delayed_caps\n");
3860 spin_lock(&mdsc->cap_delay_lock);
3861 if (list_empty(&mdsc->cap_delay_list))
3863 ci = list_first_entry(&mdsc->cap_delay_list,
3864 struct ceph_inode_info,
3866 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
3867 time_before(jiffies, ci->i_hold_caps_max))
3869 list_del_init(&ci->i_cap_delay_list);
3871 inode = igrab(&ci->vfs_inode);
3872 spin_unlock(&mdsc->cap_delay_lock);
3875 dout("check_delayed_caps on %p\n", inode);
3876 ceph_check_caps(ci, flags, NULL);
3880 spin_unlock(&mdsc->cap_delay_lock);
3884 * Flush all dirty caps to the mds
3886 void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
3888 struct ceph_inode_info *ci;
3889 struct inode *inode;
3891 dout("flush_dirty_caps\n");
3892 spin_lock(&mdsc->cap_dirty_lock);
3893 while (!list_empty(&mdsc->cap_dirty)) {
3894 ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
3896 inode = &ci->vfs_inode;
3898 dout("flush_dirty_caps %p\n", inode);
3899 spin_unlock(&mdsc->cap_dirty_lock);
3900 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
3902 spin_lock(&mdsc->cap_dirty_lock);
3904 spin_unlock(&mdsc->cap_dirty_lock);
3905 dout("flush_dirty_caps done\n");
3908 void __ceph_get_fmode(struct ceph_inode_info *ci, int fmode)
3911 int bits = (fmode << 1) | 1;
3912 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
3913 if (bits & (1 << i))
3914 ci->i_nr_by_mode[i]++;
3919 * Drop open file reference. If we were the last open file,
3920 * we may need to release capabilities to the MDS (or schedule
3921 * their delayed release).
3923 void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
3926 int bits = (fmode << 1) | 1;
3927 spin_lock(&ci->i_ceph_lock);
3928 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
3929 if (bits & (1 << i)) {
3930 BUG_ON(ci->i_nr_by_mode[i] == 0);
3931 if (--ci->i_nr_by_mode[i] == 0)
3935 dout("put_fmode %p fmode %d {%d,%d,%d,%d}\n",
3936 &ci->vfs_inode, fmode,
3937 ci->i_nr_by_mode[0], ci->i_nr_by_mode[1],
3938 ci->i_nr_by_mode[2], ci->i_nr_by_mode[3]);
3939 spin_unlock(&ci->i_ceph_lock);
3941 if (last && ci->i_vino.snap == CEPH_NOSNAP)
3942 ceph_check_caps(ci, 0, NULL);
3946 * Helpers for embedding cap and dentry lease releases into mds
3949 * @force is used by dentry_release (below) to force inclusion of a
3950 * record for the directory inode, even when there aren't any caps to
3953 int ceph_encode_inode_release(void **p, struct inode *inode,
3954 int mds, int drop, int unless, int force)
3956 struct ceph_inode_info *ci = ceph_inode(inode);
3957 struct ceph_cap *cap;
3958 struct ceph_mds_request_release *rel = *p;
3962 spin_lock(&ci->i_ceph_lock);
3963 used = __ceph_caps_used(ci);
3964 dirty = __ceph_caps_dirty(ci);
3966 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
3967 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
3968 ceph_cap_string(unless));
3970 /* only drop unused, clean caps */
3971 drop &= ~(used | dirty);
3973 cap = __get_cap_for_mds(ci, mds);
3974 if (cap && __cap_is_valid(cap)) {
3976 ((cap->issued & drop) &&
3977 (cap->issued & unless) == 0)) {
3978 if ((cap->issued & drop) &&
3979 (cap->issued & unless) == 0) {
3980 int wanted = __ceph_caps_wanted(ci);
3981 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0)
3982 wanted |= cap->mds_wanted;
3983 dout("encode_inode_release %p cap %p "
3984 "%s -> %s, wanted %s -> %s\n", inode, cap,
3985 ceph_cap_string(cap->issued),
3986 ceph_cap_string(cap->issued & ~drop),
3987 ceph_cap_string(cap->mds_wanted),
3988 ceph_cap_string(wanted));
3990 cap->issued &= ~drop;
3991 cap->implemented &= ~drop;
3992 cap->mds_wanted = wanted;
3994 dout("encode_inode_release %p cap %p %s"
3995 " (force)\n", inode, cap,
3996 ceph_cap_string(cap->issued));
3999 rel->ino = cpu_to_le64(ceph_ino(inode));
4000 rel->cap_id = cpu_to_le64(cap->cap_id);
4001 rel->seq = cpu_to_le32(cap->seq);
4002 rel->issue_seq = cpu_to_le32(cap->issue_seq);
4003 rel->mseq = cpu_to_le32(cap->mseq);
4004 rel->caps = cpu_to_le32(cap->implemented);
4005 rel->wanted = cpu_to_le32(cap->mds_wanted);
4011 dout("encode_inode_release %p cap %p %s\n",
4012 inode, cap, ceph_cap_string(cap->issued));
4015 spin_unlock(&ci->i_ceph_lock);
4019 int ceph_encode_dentry_release(void **p, struct dentry *dentry,
4021 int mds, int drop, int unless)
4023 struct dentry *parent = NULL;
4024 struct ceph_mds_request_release *rel = *p;
4025 struct ceph_dentry_info *di = ceph_dentry(dentry);
4030 * force an record for the directory caps if we have a dentry lease.
4031 * this is racy (can't take i_ceph_lock and d_lock together), but it
4032 * doesn't have to be perfect; the mds will revoke anything we don't
4035 spin_lock(&dentry->d_lock);
4036 if (di->lease_session && di->lease_session->s_mds == mds)
4039 parent = dget(dentry->d_parent);
4040 dir = d_inode(parent);
4042 spin_unlock(&dentry->d_lock);
4044 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
4047 spin_lock(&dentry->d_lock);
4048 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
4049 dout("encode_dentry_release %p mds%d seq %d\n",
4050 dentry, mds, (int)di->lease_seq);
4051 rel->dname_len = cpu_to_le32(dentry->d_name.len);
4052 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
4053 *p += dentry->d_name.len;
4054 rel->dname_seq = cpu_to_le32(di->lease_seq);
4055 __ceph_mdsc_drop_dentry_lease(dentry);
4057 spin_unlock(&dentry->d_lock);