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>
11 #include <linux/iversion.h>
14 #include "mds_client.h"
16 #include <linux/ceph/decode.h>
17 #include <linux/ceph/messenger.h>
20 * Capability management
22 * The Ceph metadata servers control client access to inode metadata
23 * and file data by issuing capabilities, granting clients permission
24 * to read and/or write both inode field and file data to OSDs
25 * (storage nodes). Each capability consists of a set of bits
26 * indicating which operations are allowed.
28 * If the client holds a *_SHARED cap, the client has a coherent value
29 * that can be safely read from the cached inode.
31 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
32 * client is allowed to change inode attributes (e.g., file size,
33 * mtime), note its dirty state in the ceph_cap, and asynchronously
34 * flush that metadata change to the MDS.
36 * In the event of a conflicting operation (perhaps by another
37 * client), the MDS will revoke the conflicting client capabilities.
39 * In order for a client to cache an inode, it must hold a capability
40 * with at least one MDS server. When inodes are released, release
41 * notifications are batched and periodically sent en masse to the MDS
42 * cluster to release server state.
45 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc);
46 static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
47 struct ceph_mds_session *session,
48 struct ceph_inode_info *ci,
49 u64 oldest_flush_tid);
52 * Generate readable cap strings for debugging output.
54 #define MAX_CAP_STR 20
55 static char cap_str[MAX_CAP_STR][40];
56 static DEFINE_SPINLOCK(cap_str_lock);
57 static int last_cap_str;
59 static char *gcap_string(char *s, int c)
61 if (c & CEPH_CAP_GSHARED)
63 if (c & CEPH_CAP_GEXCL)
65 if (c & CEPH_CAP_GCACHE)
71 if (c & CEPH_CAP_GBUFFER)
73 if (c & CEPH_CAP_GWREXTEND)
75 if (c & CEPH_CAP_GLAZYIO)
80 const char *ceph_cap_string(int caps)
86 spin_lock(&cap_str_lock);
88 if (last_cap_str == MAX_CAP_STR)
90 spin_unlock(&cap_str_lock);
94 if (caps & CEPH_CAP_PIN)
97 c = (caps >> CEPH_CAP_SAUTH) & 3;
100 s = gcap_string(s, c);
103 c = (caps >> CEPH_CAP_SLINK) & 3;
106 s = gcap_string(s, c);
109 c = (caps >> CEPH_CAP_SXATTR) & 3;
112 s = gcap_string(s, c);
115 c = caps >> CEPH_CAP_SFILE;
118 s = gcap_string(s, c);
127 void ceph_caps_init(struct ceph_mds_client *mdsc)
129 INIT_LIST_HEAD(&mdsc->caps_list);
130 spin_lock_init(&mdsc->caps_list_lock);
133 void ceph_caps_finalize(struct ceph_mds_client *mdsc)
135 struct ceph_cap *cap;
137 spin_lock(&mdsc->caps_list_lock);
138 while (!list_empty(&mdsc->caps_list)) {
139 cap = list_first_entry(&mdsc->caps_list,
140 struct ceph_cap, caps_item);
141 list_del(&cap->caps_item);
142 kmem_cache_free(ceph_cap_cachep, cap);
144 mdsc->caps_total_count = 0;
145 mdsc->caps_avail_count = 0;
146 mdsc->caps_use_count = 0;
147 mdsc->caps_reserve_count = 0;
148 mdsc->caps_min_count = 0;
149 spin_unlock(&mdsc->caps_list_lock);
152 void ceph_adjust_caps_max_min(struct ceph_mds_client *mdsc,
153 struct ceph_mount_options *fsopt)
155 spin_lock(&mdsc->caps_list_lock);
156 mdsc->caps_min_count = fsopt->max_readdir;
157 if (mdsc->caps_min_count < 1024)
158 mdsc->caps_min_count = 1024;
159 mdsc->caps_use_max = fsopt->caps_max;
160 if (mdsc->caps_use_max > 0 &&
161 mdsc->caps_use_max < mdsc->caps_min_count)
162 mdsc->caps_use_max = mdsc->caps_min_count;
163 spin_unlock(&mdsc->caps_list_lock);
166 static void __ceph_unreserve_caps(struct ceph_mds_client *mdsc, int nr_caps)
168 struct ceph_cap *cap;
172 BUG_ON(mdsc->caps_reserve_count < nr_caps);
173 mdsc->caps_reserve_count -= nr_caps;
174 if (mdsc->caps_avail_count >=
175 mdsc->caps_reserve_count + mdsc->caps_min_count) {
176 mdsc->caps_total_count -= nr_caps;
177 for (i = 0; i < nr_caps; i++) {
178 cap = list_first_entry(&mdsc->caps_list,
179 struct ceph_cap, caps_item);
180 list_del(&cap->caps_item);
181 kmem_cache_free(ceph_cap_cachep, cap);
184 mdsc->caps_avail_count += nr_caps;
187 dout("%s: caps %d = %d used + %d resv + %d avail\n",
189 mdsc->caps_total_count, mdsc->caps_use_count,
190 mdsc->caps_reserve_count, mdsc->caps_avail_count);
191 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
192 mdsc->caps_reserve_count +
193 mdsc->caps_avail_count);
198 * Called under mdsc->mutex.
200 int ceph_reserve_caps(struct ceph_mds_client *mdsc,
201 struct ceph_cap_reservation *ctx, int need)
204 struct ceph_cap *cap;
209 bool trimmed = false;
210 struct ceph_mds_session *s;
213 dout("reserve caps ctx=%p need=%d\n", ctx, need);
215 /* first reserve any caps that are already allocated */
216 spin_lock(&mdsc->caps_list_lock);
217 if (mdsc->caps_avail_count >= need)
220 have = mdsc->caps_avail_count;
221 mdsc->caps_avail_count -= have;
222 mdsc->caps_reserve_count += have;
223 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
224 mdsc->caps_reserve_count +
225 mdsc->caps_avail_count);
226 spin_unlock(&mdsc->caps_list_lock);
228 for (i = have; i < need; ) {
229 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
231 list_add(&cap->caps_item, &newcaps);
238 for (j = 0; j < mdsc->max_sessions; j++) {
239 s = __ceph_lookup_mds_session(mdsc, j);
242 mutex_unlock(&mdsc->mutex);
244 mutex_lock(&s->s_mutex);
245 max_caps = s->s_nr_caps - (need - i);
246 ceph_trim_caps(mdsc, s, max_caps);
247 mutex_unlock(&s->s_mutex);
249 ceph_put_mds_session(s);
250 mutex_lock(&mdsc->mutex);
254 spin_lock(&mdsc->caps_list_lock);
255 if (mdsc->caps_avail_count) {
257 if (mdsc->caps_avail_count >= need - i)
258 more_have = need - i;
260 more_have = mdsc->caps_avail_count;
264 mdsc->caps_avail_count -= more_have;
265 mdsc->caps_reserve_count += more_have;
268 spin_unlock(&mdsc->caps_list_lock);
273 pr_warn("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
274 ctx, need, have + alloc);
280 BUG_ON(have + alloc != need);
285 spin_lock(&mdsc->caps_list_lock);
286 mdsc->caps_total_count += alloc;
287 mdsc->caps_reserve_count += alloc;
288 list_splice(&newcaps, &mdsc->caps_list);
290 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
291 mdsc->caps_reserve_count +
292 mdsc->caps_avail_count);
295 __ceph_unreserve_caps(mdsc, have + alloc);
297 spin_unlock(&mdsc->caps_list_lock);
299 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
300 ctx, mdsc->caps_total_count, mdsc->caps_use_count,
301 mdsc->caps_reserve_count, mdsc->caps_avail_count);
305 void ceph_unreserve_caps(struct ceph_mds_client *mdsc,
306 struct ceph_cap_reservation *ctx)
308 bool reclaim = false;
312 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
313 spin_lock(&mdsc->caps_list_lock);
314 __ceph_unreserve_caps(mdsc, ctx->count);
317 if (mdsc->caps_use_max > 0 &&
318 mdsc->caps_use_count > mdsc->caps_use_max)
320 spin_unlock(&mdsc->caps_list_lock);
323 ceph_reclaim_caps_nr(mdsc, ctx->used);
326 struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc,
327 struct ceph_cap_reservation *ctx)
329 struct ceph_cap *cap = NULL;
331 /* temporary, until we do something about cap import/export */
333 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
335 spin_lock(&mdsc->caps_list_lock);
336 mdsc->caps_use_count++;
337 mdsc->caps_total_count++;
338 spin_unlock(&mdsc->caps_list_lock);
340 spin_lock(&mdsc->caps_list_lock);
341 if (mdsc->caps_avail_count) {
342 BUG_ON(list_empty(&mdsc->caps_list));
344 mdsc->caps_avail_count--;
345 mdsc->caps_use_count++;
346 cap = list_first_entry(&mdsc->caps_list,
347 struct ceph_cap, caps_item);
348 list_del(&cap->caps_item);
350 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
351 mdsc->caps_reserve_count + mdsc->caps_avail_count);
353 spin_unlock(&mdsc->caps_list_lock);
359 spin_lock(&mdsc->caps_list_lock);
360 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
361 ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
362 mdsc->caps_reserve_count, mdsc->caps_avail_count);
364 BUG_ON(ctx->count > mdsc->caps_reserve_count);
365 BUG_ON(list_empty(&mdsc->caps_list));
369 mdsc->caps_reserve_count--;
370 mdsc->caps_use_count++;
372 cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
373 list_del(&cap->caps_item);
375 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
376 mdsc->caps_reserve_count + mdsc->caps_avail_count);
377 spin_unlock(&mdsc->caps_list_lock);
381 void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
383 spin_lock(&mdsc->caps_list_lock);
384 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
385 cap, mdsc->caps_total_count, mdsc->caps_use_count,
386 mdsc->caps_reserve_count, mdsc->caps_avail_count);
387 mdsc->caps_use_count--;
389 * Keep some preallocated caps around (ceph_min_count), to
390 * avoid lots of free/alloc churn.
392 if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
393 mdsc->caps_min_count) {
394 mdsc->caps_total_count--;
395 kmem_cache_free(ceph_cap_cachep, cap);
397 mdsc->caps_avail_count++;
398 list_add(&cap->caps_item, &mdsc->caps_list);
401 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
402 mdsc->caps_reserve_count + mdsc->caps_avail_count);
403 spin_unlock(&mdsc->caps_list_lock);
406 void ceph_reservation_status(struct ceph_fs_client *fsc,
407 int *total, int *avail, int *used, int *reserved,
410 struct ceph_mds_client *mdsc = fsc->mdsc;
412 spin_lock(&mdsc->caps_list_lock);
415 *total = mdsc->caps_total_count;
417 *avail = mdsc->caps_avail_count;
419 *used = mdsc->caps_use_count;
421 *reserved = mdsc->caps_reserve_count;
423 *min = mdsc->caps_min_count;
425 spin_unlock(&mdsc->caps_list_lock);
429 * Find ceph_cap for given mds, if any.
431 * Called with i_ceph_lock held.
433 static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
435 struct ceph_cap *cap;
436 struct rb_node *n = ci->i_caps.rb_node;
439 cap = rb_entry(n, struct ceph_cap, ci_node);
442 else if (mds > cap->mds)
450 struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
452 struct ceph_cap *cap;
454 spin_lock(&ci->i_ceph_lock);
455 cap = __get_cap_for_mds(ci, mds);
456 spin_unlock(&ci->i_ceph_lock);
461 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
463 static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
465 struct ceph_cap *cap;
469 /* prefer mds with WR|BUFFER|EXCL caps */
470 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
471 cap = rb_entry(p, struct ceph_cap, ci_node);
473 if (cap->issued & (CEPH_CAP_FILE_WR |
474 CEPH_CAP_FILE_BUFFER |
481 int ceph_get_cap_mds(struct inode *inode)
483 struct ceph_inode_info *ci = ceph_inode(inode);
485 spin_lock(&ci->i_ceph_lock);
486 mds = __ceph_get_cap_mds(ceph_inode(inode));
487 spin_unlock(&ci->i_ceph_lock);
492 * Called under i_ceph_lock.
494 static void __insert_cap_node(struct ceph_inode_info *ci,
495 struct ceph_cap *new)
497 struct rb_node **p = &ci->i_caps.rb_node;
498 struct rb_node *parent = NULL;
499 struct ceph_cap *cap = NULL;
503 cap = rb_entry(parent, struct ceph_cap, ci_node);
504 if (new->mds < cap->mds)
506 else if (new->mds > cap->mds)
512 rb_link_node(&new->ci_node, parent, p);
513 rb_insert_color(&new->ci_node, &ci->i_caps);
517 * (re)set cap hold timeouts, which control the delayed release
518 * of unused caps back to the MDS. Should be called on cap use.
520 static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
521 struct ceph_inode_info *ci)
523 struct ceph_mount_options *opt = mdsc->fsc->mount_options;
525 ci->i_hold_caps_min = round_jiffies(jiffies +
526 opt->caps_wanted_delay_min * HZ);
527 ci->i_hold_caps_max = round_jiffies(jiffies +
528 opt->caps_wanted_delay_max * HZ);
529 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
530 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
534 * (Re)queue cap at the end of the delayed cap release list.
536 * If I_FLUSH is set, leave the inode at the front of the list.
538 * Caller holds i_ceph_lock
539 * -> we take mdsc->cap_delay_lock
541 static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
542 struct ceph_inode_info *ci,
545 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
546 ci->i_ceph_flags, ci->i_hold_caps_max);
547 if (!mdsc->stopping) {
548 spin_lock(&mdsc->cap_delay_lock);
549 if (!list_empty(&ci->i_cap_delay_list)) {
550 if (ci->i_ceph_flags & CEPH_I_FLUSH)
552 list_del_init(&ci->i_cap_delay_list);
555 __cap_set_timeouts(mdsc, ci);
556 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
558 spin_unlock(&mdsc->cap_delay_lock);
563 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
564 * indicating we should send a cap message to flush dirty metadata
565 * asap, and move to the front of the delayed cap list.
567 static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
568 struct ceph_inode_info *ci)
570 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
571 spin_lock(&mdsc->cap_delay_lock);
572 ci->i_ceph_flags |= CEPH_I_FLUSH;
573 if (!list_empty(&ci->i_cap_delay_list))
574 list_del_init(&ci->i_cap_delay_list);
575 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
576 spin_unlock(&mdsc->cap_delay_lock);
580 * Cancel delayed work on cap.
582 * Caller must hold i_ceph_lock.
584 static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
585 struct ceph_inode_info *ci)
587 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
588 if (list_empty(&ci->i_cap_delay_list))
590 spin_lock(&mdsc->cap_delay_lock);
591 list_del_init(&ci->i_cap_delay_list);
592 spin_unlock(&mdsc->cap_delay_lock);
596 * Common issue checks for add_cap, handle_cap_grant.
598 static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
601 unsigned had = __ceph_caps_issued(ci, NULL);
604 * Each time we receive FILE_CACHE anew, we increment
607 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
608 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) {
613 * If FILE_SHARED is newly issued, mark dir not complete. We don't
614 * know what happened to this directory while we didn't have the cap.
615 * If FILE_SHARED is being revoked, also mark dir not complete. It
616 * stops on-going cached readdir.
618 if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) {
619 if (issued & CEPH_CAP_FILE_SHARED)
620 atomic_inc(&ci->i_shared_gen);
621 if (S_ISDIR(ci->vfs_inode.i_mode)) {
622 dout(" marking %p NOT complete\n", &ci->vfs_inode);
623 __ceph_dir_clear_complete(ci);
629 * Add a capability under the given MDS session.
631 * Caller should hold session snap_rwsem (read) and s_mutex.
633 * @fmode is the open file mode, if we are opening a file, otherwise
634 * it is < 0. (This is so we can atomically add the cap and add an
635 * open file reference to it.)
637 void ceph_add_cap(struct inode *inode,
638 struct ceph_mds_session *session, u64 cap_id,
639 int fmode, unsigned issued, unsigned wanted,
640 unsigned seq, unsigned mseq, u64 realmino, int flags,
641 struct ceph_cap **new_cap)
643 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
644 struct ceph_inode_info *ci = ceph_inode(inode);
645 struct ceph_cap *cap;
646 int mds = session->s_mds;
649 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
650 session->s_mds, cap_id, ceph_cap_string(issued), seq);
653 * If we are opening the file, include file mode wanted bits
657 wanted |= ceph_caps_for_mode(fmode);
659 cap = __get_cap_for_mds(ci, mds);
665 cap->implemented = 0;
671 __insert_cap_node(ci, cap);
673 /* add to session cap list */
674 cap->session = session;
675 spin_lock(&session->s_cap_lock);
676 list_add_tail(&cap->session_caps, &session->s_caps);
677 session->s_nr_caps++;
678 spin_unlock(&session->s_cap_lock);
680 spin_lock(&session->s_cap_lock);
681 list_move_tail(&cap->session_caps, &session->s_caps);
682 spin_unlock(&session->s_cap_lock);
684 if (cap->cap_gen < session->s_cap_gen)
685 cap->issued = cap->implemented = CEPH_CAP_PIN;
688 * auth mds of the inode changed. we received the cap export
689 * message, but still haven't received the cap import message.
690 * handle_cap_export() updated the new auth MDS' cap.
692 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
693 * a message that was send before the cap import message. So
696 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
697 WARN_ON(cap != ci->i_auth_cap);
698 WARN_ON(cap->cap_id != cap_id);
701 issued |= cap->issued;
702 flags |= CEPH_CAP_FLAG_AUTH;
706 if (!ci->i_snap_realm ||
707 ((flags & CEPH_CAP_FLAG_AUTH) &&
708 realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) {
710 * add this inode to the appropriate snap realm
712 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
715 struct ceph_snap_realm *oldrealm = ci->i_snap_realm;
717 spin_lock(&oldrealm->inodes_with_caps_lock);
718 list_del_init(&ci->i_snap_realm_item);
719 spin_unlock(&oldrealm->inodes_with_caps_lock);
722 spin_lock(&realm->inodes_with_caps_lock);
723 list_add(&ci->i_snap_realm_item,
724 &realm->inodes_with_caps);
725 ci->i_snap_realm = realm;
726 if (realm->ino == ci->i_vino.ino)
727 realm->inode = inode;
728 spin_unlock(&realm->inodes_with_caps_lock);
731 ceph_put_snap_realm(mdsc, oldrealm);
733 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
739 __check_cap_issue(ci, cap, issued);
742 * If we are issued caps we don't want, or the mds' wanted
743 * value appears to be off, queue a check so we'll release
744 * later and/or update the mds wanted value.
746 actual_wanted = __ceph_caps_wanted(ci);
747 if ((wanted & ~actual_wanted) ||
748 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
749 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
750 ceph_cap_string(issued), ceph_cap_string(wanted),
751 ceph_cap_string(actual_wanted));
752 __cap_delay_requeue(mdsc, ci, true);
755 if (flags & CEPH_CAP_FLAG_AUTH) {
756 if (!ci->i_auth_cap ||
757 ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
758 ci->i_auth_cap = cap;
759 cap->mds_wanted = wanted;
762 WARN_ON(ci->i_auth_cap == cap);
765 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
766 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
767 ceph_cap_string(issued|cap->issued), seq, mds);
768 cap->cap_id = cap_id;
769 cap->issued = issued;
770 cap->implemented |= issued;
771 if (ceph_seq_cmp(mseq, cap->mseq) > 0)
772 cap->mds_wanted = wanted;
774 cap->mds_wanted |= wanted;
776 cap->issue_seq = seq;
778 cap->cap_gen = session->s_cap_gen;
781 __ceph_get_fmode(ci, fmode);
785 * Return true if cap has not timed out and belongs to the current
786 * generation of the MDS session (i.e. has not gone 'stale' due to
787 * us losing touch with the mds).
789 static int __cap_is_valid(struct ceph_cap *cap)
794 spin_lock(&cap->session->s_gen_ttl_lock);
795 gen = cap->session->s_cap_gen;
796 ttl = cap->session->s_cap_ttl;
797 spin_unlock(&cap->session->s_gen_ttl_lock);
799 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
800 dout("__cap_is_valid %p cap %p issued %s "
801 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
802 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
810 * Return set of valid cap bits issued to us. Note that caps time
811 * out, and may be invalidated in bulk if the client session times out
812 * and session->s_cap_gen is bumped.
814 int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
816 int have = ci->i_snap_caps;
817 struct ceph_cap *cap;
822 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
823 cap = rb_entry(p, struct ceph_cap, ci_node);
824 if (!__cap_is_valid(cap))
826 dout("__ceph_caps_issued %p cap %p issued %s\n",
827 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
830 *implemented |= cap->implemented;
833 * exclude caps issued by non-auth MDS, but are been revoking
834 * by the auth MDS. The non-auth MDS should be revoking/exporting
835 * these caps, but the message is delayed.
837 if (ci->i_auth_cap) {
838 cap = ci->i_auth_cap;
839 have &= ~cap->implemented | cap->issued;
845 * Get cap bits issued by caps other than @ocap
847 int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
849 int have = ci->i_snap_caps;
850 struct ceph_cap *cap;
853 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
854 cap = rb_entry(p, struct ceph_cap, ci_node);
857 if (!__cap_is_valid(cap))
865 * Move a cap to the end of the LRU (oldest caps at list head, newest
868 static void __touch_cap(struct ceph_cap *cap)
870 struct ceph_mds_session *s = cap->session;
872 spin_lock(&s->s_cap_lock);
873 if (!s->s_cap_iterator) {
874 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
876 list_move_tail(&cap->session_caps, &s->s_caps);
878 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
879 &cap->ci->vfs_inode, cap, s->s_mds);
881 spin_unlock(&s->s_cap_lock);
885 * Check if we hold the given mask. If so, move the cap(s) to the
886 * front of their respective LRUs. (This is the preferred way for
887 * callers to check for caps they want.)
889 int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
891 struct ceph_cap *cap;
893 int have = ci->i_snap_caps;
895 if ((have & mask) == mask) {
896 dout("__ceph_caps_issued_mask ino 0x%lx snap issued %s"
897 " (mask %s)\n", ci->vfs_inode.i_ino,
898 ceph_cap_string(have),
899 ceph_cap_string(mask));
903 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
904 cap = rb_entry(p, struct ceph_cap, ci_node);
905 if (!__cap_is_valid(cap))
907 if ((cap->issued & mask) == mask) {
908 dout("__ceph_caps_issued_mask ino 0x%lx cap %p issued %s"
909 " (mask %s)\n", ci->vfs_inode.i_ino, cap,
910 ceph_cap_string(cap->issued),
911 ceph_cap_string(mask));
917 /* does a combination of caps satisfy mask? */
919 if ((have & mask) == mask) {
920 dout("__ceph_caps_issued_mask ino 0x%lx combo issued %s"
921 " (mask %s)\n", ci->vfs_inode.i_ino,
922 ceph_cap_string(cap->issued),
923 ceph_cap_string(mask));
927 /* touch this + preceding caps */
929 for (q = rb_first(&ci->i_caps); q != p;
931 cap = rb_entry(q, struct ceph_cap,
933 if (!__cap_is_valid(cap))
946 * Return true if mask caps are currently being revoked by an MDS.
948 int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
949 struct ceph_cap *ocap, int mask)
951 struct ceph_cap *cap;
954 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
955 cap = rb_entry(p, struct ceph_cap, ci_node);
957 (cap->implemented & ~cap->issued & mask))
963 int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
965 struct inode *inode = &ci->vfs_inode;
968 spin_lock(&ci->i_ceph_lock);
969 ret = __ceph_caps_revoking_other(ci, NULL, mask);
970 spin_unlock(&ci->i_ceph_lock);
971 dout("ceph_caps_revoking %p %s = %d\n", inode,
972 ceph_cap_string(mask), ret);
976 int __ceph_caps_used(struct ceph_inode_info *ci)
980 used |= CEPH_CAP_PIN;
982 used |= CEPH_CAP_FILE_RD;
983 if (ci->i_rdcache_ref ||
984 (!S_ISDIR(ci->vfs_inode.i_mode) && /* ignore readdir cache */
985 ci->vfs_inode.i_data.nrpages))
986 used |= CEPH_CAP_FILE_CACHE;
988 used |= CEPH_CAP_FILE_WR;
989 if (ci->i_wb_ref || ci->i_wrbuffer_ref)
990 used |= CEPH_CAP_FILE_BUFFER;
995 * wanted, by virtue of open file modes
997 int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
1000 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
1001 if (ci->i_nr_by_mode[i])
1006 return ceph_caps_for_mode(bits >> 1);
1010 * Return caps we have registered with the MDS(s) as 'wanted'.
1012 int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check)
1014 struct ceph_cap *cap;
1018 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1019 cap = rb_entry(p, struct ceph_cap, ci_node);
1020 if (check && !__cap_is_valid(cap))
1022 if (cap == ci->i_auth_cap)
1023 mds_wanted |= cap->mds_wanted;
1025 mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
1031 * called under i_ceph_lock
1033 static int __ceph_is_single_caps(struct ceph_inode_info *ci)
1035 return rb_first(&ci->i_caps) == rb_last(&ci->i_caps);
1038 static int __ceph_is_any_caps(struct ceph_inode_info *ci)
1040 return !RB_EMPTY_ROOT(&ci->i_caps);
1043 int ceph_is_any_caps(struct inode *inode)
1045 struct ceph_inode_info *ci = ceph_inode(inode);
1048 spin_lock(&ci->i_ceph_lock);
1049 ret = __ceph_is_any_caps(ci);
1050 spin_unlock(&ci->i_ceph_lock);
1055 static void drop_inode_snap_realm(struct ceph_inode_info *ci)
1057 struct ceph_snap_realm *realm = ci->i_snap_realm;
1058 spin_lock(&realm->inodes_with_caps_lock);
1059 list_del_init(&ci->i_snap_realm_item);
1060 ci->i_snap_realm_counter++;
1061 ci->i_snap_realm = NULL;
1062 if (realm->ino == ci->i_vino.ino)
1063 realm->inode = NULL;
1064 spin_unlock(&realm->inodes_with_caps_lock);
1065 ceph_put_snap_realm(ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc,
1070 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
1072 * caller should hold i_ceph_lock.
1073 * caller will not hold session s_mutex if called from destroy_inode.
1075 void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
1077 struct ceph_mds_session *session = cap->session;
1078 struct ceph_inode_info *ci = cap->ci;
1079 struct ceph_mds_client *mdsc =
1080 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
1083 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
1085 /* remove from session list */
1086 spin_lock(&session->s_cap_lock);
1087 if (session->s_cap_iterator == cap) {
1088 /* not yet, we are iterating over this very cap */
1089 dout("__ceph_remove_cap delaying %p removal from session %p\n",
1092 list_del_init(&cap->session_caps);
1093 session->s_nr_caps--;
1094 cap->session = NULL;
1097 /* protect backpointer with s_cap_lock: see iterate_session_caps */
1101 * s_cap_reconnect is protected by s_cap_lock. no one changes
1102 * s_cap_gen while session is in the reconnect state.
1104 if (queue_release &&
1105 (!session->s_cap_reconnect || cap->cap_gen == session->s_cap_gen)) {
1106 cap->queue_release = 1;
1108 __ceph_queue_cap_release(session, cap);
1112 cap->queue_release = 0;
1114 cap->cap_ino = ci->i_vino.ino;
1116 spin_unlock(&session->s_cap_lock);
1118 /* remove from inode list */
1119 rb_erase(&cap->ci_node, &ci->i_caps);
1120 if (ci->i_auth_cap == cap)
1121 ci->i_auth_cap = NULL;
1124 ceph_put_cap(mdsc, cap);
1126 /* when reconnect denied, we remove session caps forcibly,
1127 * i_wr_ref can be non-zero. If there are ongoing write,
1128 * keep i_snap_realm.
1130 if (!__ceph_is_any_caps(ci) && ci->i_wr_ref == 0 && ci->i_snap_realm)
1131 drop_inode_snap_realm(ci);
1133 if (!__ceph_is_any_real_caps(ci))
1134 __cap_delay_cancel(mdsc, ci);
1137 struct cap_msg_args {
1138 struct ceph_mds_session *session;
1139 u64 ino, cid, follows;
1140 u64 flush_tid, oldest_flush_tid, size, max_size;
1143 struct ceph_buffer *xattr_buf;
1144 struct timespec64 atime, mtime, ctime, btime;
1145 int op, caps, wanted, dirty;
1146 u32 seq, issue_seq, mseq, time_warp_seq;
1155 * Build and send a cap message to the given MDS.
1157 * Caller should be holding s_mutex.
1159 static int send_cap_msg(struct cap_msg_args *arg)
1161 struct ceph_mds_caps *fc;
1162 struct ceph_msg *msg;
1165 struct ceph_osd_client *osdc = &arg->session->s_mdsc->fsc->client->osdc;
1167 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
1168 " seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu"
1169 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(arg->op),
1170 arg->cid, arg->ino, ceph_cap_string(arg->caps),
1171 ceph_cap_string(arg->wanted), ceph_cap_string(arg->dirty),
1172 arg->seq, arg->issue_seq, arg->flush_tid, arg->oldest_flush_tid,
1173 arg->mseq, arg->follows, arg->size, arg->max_size,
1175 arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0);
1177 /* flock buffer size + inline version + inline data size +
1178 * osd_epoch_barrier + oldest_flush_tid */
1179 extra_len = 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4;
1180 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc) + extra_len,
1185 msg->hdr.version = cpu_to_le16(10);
1186 msg->hdr.tid = cpu_to_le64(arg->flush_tid);
1188 fc = msg->front.iov_base;
1189 memset(fc, 0, sizeof(*fc));
1191 fc->cap_id = cpu_to_le64(arg->cid);
1192 fc->op = cpu_to_le32(arg->op);
1193 fc->seq = cpu_to_le32(arg->seq);
1194 fc->issue_seq = cpu_to_le32(arg->issue_seq);
1195 fc->migrate_seq = cpu_to_le32(arg->mseq);
1196 fc->caps = cpu_to_le32(arg->caps);
1197 fc->wanted = cpu_to_le32(arg->wanted);
1198 fc->dirty = cpu_to_le32(arg->dirty);
1199 fc->ino = cpu_to_le64(arg->ino);
1200 fc->snap_follows = cpu_to_le64(arg->follows);
1202 fc->size = cpu_to_le64(arg->size);
1203 fc->max_size = cpu_to_le64(arg->max_size);
1204 ceph_encode_timespec64(&fc->mtime, &arg->mtime);
1205 ceph_encode_timespec64(&fc->atime, &arg->atime);
1206 ceph_encode_timespec64(&fc->ctime, &arg->ctime);
1207 fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq);
1209 fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid));
1210 fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid));
1211 fc->mode = cpu_to_le32(arg->mode);
1213 fc->xattr_version = cpu_to_le64(arg->xattr_version);
1214 if (arg->xattr_buf) {
1215 msg->middle = ceph_buffer_get(arg->xattr_buf);
1216 fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1217 msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1221 /* flock buffer size (version 2) */
1222 ceph_encode_32(&p, 0);
1223 /* inline version (version 4) */
1224 ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE);
1225 /* inline data size */
1226 ceph_encode_32(&p, 0);
1228 * osd_epoch_barrier (version 5)
1229 * The epoch_barrier is protected osdc->lock, so READ_ONCE here in
1230 * case it was recently changed
1232 ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier));
1233 /* oldest_flush_tid (version 6) */
1234 ceph_encode_64(&p, arg->oldest_flush_tid);
1237 * caller_uid/caller_gid (version 7)
1239 * Currently, we don't properly track which caller dirtied the caps
1240 * last, and force a flush of them when there is a conflict. For now,
1241 * just set this to 0:0, to emulate how the MDS has worked up to now.
1243 ceph_encode_32(&p, 0);
1244 ceph_encode_32(&p, 0);
1246 /* pool namespace (version 8) (mds always ignores this) */
1247 ceph_encode_32(&p, 0);
1249 /* btime and change_attr (version 9) */
1250 ceph_encode_timespec64(p, &arg->btime);
1251 p += sizeof(struct ceph_timespec);
1252 ceph_encode_64(&p, arg->change_attr);
1254 /* Advisory flags (version 10) */
1255 ceph_encode_32(&p, arg->flags);
1257 ceph_con_send(&arg->session->s_con, msg);
1262 * Queue cap releases when an inode is dropped from our cache.
1264 void __ceph_remove_caps(struct ceph_inode_info *ci)
1268 /* lock i_ceph_lock, because ceph_d_revalidate(..., LOOKUP_RCU)
1269 * may call __ceph_caps_issued_mask() on a freeing inode. */
1270 spin_lock(&ci->i_ceph_lock);
1271 p = rb_first(&ci->i_caps);
1273 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1275 __ceph_remove_cap(cap, true);
1277 spin_unlock(&ci->i_ceph_lock);
1281 * Send a cap msg on the given inode. Update our caps state, then
1282 * drop i_ceph_lock and send the message.
1284 * Make note of max_size reported/requested from mds, revoked caps
1285 * that have now been implemented.
1287 * Make half-hearted attempt ot to invalidate page cache if we are
1288 * dropping RDCACHE. Note that this will leave behind locked pages
1289 * that we'll then need to deal with elsewhere.
1291 * Return non-zero if delayed release, or we experienced an error
1292 * such that the caller should requeue + retry later.
1294 * called with i_ceph_lock, then drops it.
1295 * caller should hold snap_rwsem (read), s_mutex.
1297 static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1298 int op, int flags, int used, int want, int retain,
1299 int flushing, u64 flush_tid, u64 oldest_flush_tid)
1300 __releases(cap->ci->i_ceph_lock)
1302 struct ceph_inode_info *ci = cap->ci;
1303 struct inode *inode = &ci->vfs_inode;
1304 struct ceph_buffer *old_blob = NULL;
1305 struct cap_msg_args arg;
1311 held = cap->issued | cap->implemented;
1312 revoking = cap->implemented & ~cap->issued;
1313 retain &= ~revoking;
1315 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1316 inode, cap, cap->session,
1317 ceph_cap_string(held), ceph_cap_string(held & retain),
1318 ceph_cap_string(revoking));
1319 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1321 arg.session = cap->session;
1323 /* don't release wanted unless we've waited a bit. */
1324 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1325 time_before(jiffies, ci->i_hold_caps_min)) {
1326 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1327 ceph_cap_string(cap->issued),
1328 ceph_cap_string(cap->issued & retain),
1329 ceph_cap_string(cap->mds_wanted),
1330 ceph_cap_string(want));
1331 want |= cap->mds_wanted;
1332 retain |= cap->issued;
1335 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1336 if (want & ~cap->mds_wanted) {
1337 /* user space may open/close single file frequently.
1338 * This avoids droping mds_wanted immediately after
1339 * requesting new mds_wanted.
1341 __cap_set_timeouts(mdsc, ci);
1344 cap->issued &= retain; /* drop bits we don't want */
1345 if (cap->implemented & ~cap->issued) {
1347 * Wake up any waiters on wanted -> needed transition.
1348 * This is due to the weird transition from buffered
1349 * to sync IO... we need to flush dirty pages _before_
1350 * allowing sync writes to avoid reordering.
1354 cap->implemented &= cap->issued | used;
1355 cap->mds_wanted = want;
1357 arg.ino = ceph_vino(inode).ino;
1358 arg.cid = cap->cap_id;
1359 arg.follows = flushing ? ci->i_head_snapc->seq : 0;
1360 arg.flush_tid = flush_tid;
1361 arg.oldest_flush_tid = oldest_flush_tid;
1363 arg.size = inode->i_size;
1364 ci->i_reported_size = arg.size;
1365 arg.max_size = ci->i_wanted_max_size;
1366 ci->i_requested_max_size = arg.max_size;
1368 if (flushing & CEPH_CAP_XATTR_EXCL) {
1369 old_blob = __ceph_build_xattrs_blob(ci);
1370 arg.xattr_version = ci->i_xattrs.version;
1371 arg.xattr_buf = ci->i_xattrs.blob;
1373 arg.xattr_buf = NULL;
1376 arg.mtime = inode->i_mtime;
1377 arg.atime = inode->i_atime;
1378 arg.ctime = inode->i_ctime;
1379 arg.btime = ci->i_btime;
1380 arg.change_attr = inode_peek_iversion_raw(inode);
1383 arg.caps = cap->implemented;
1385 arg.dirty = flushing;
1388 arg.issue_seq = cap->issue_seq;
1389 arg.mseq = cap->mseq;
1390 arg.time_warp_seq = ci->i_time_warp_seq;
1392 arg.uid = inode->i_uid;
1393 arg.gid = inode->i_gid;
1394 arg.mode = inode->i_mode;
1396 arg.inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
1397 if (!(flags & CEPH_CLIENT_CAPS_PENDING_CAPSNAP) &&
1398 !list_empty(&ci->i_cap_snaps)) {
1399 struct ceph_cap_snap *capsnap;
1400 list_for_each_entry_reverse(capsnap, &ci->i_cap_snaps, ci_item) {
1401 if (capsnap->cap_flush.tid)
1403 if (capsnap->need_flush) {
1404 flags |= CEPH_CLIENT_CAPS_PENDING_CAPSNAP;
1411 spin_unlock(&ci->i_ceph_lock);
1413 ceph_buffer_put(old_blob);
1415 ret = send_cap_msg(&arg);
1417 dout("error sending cap msg, must requeue %p\n", inode);
1422 wake_up_all(&ci->i_cap_wq);
1427 static inline int __send_flush_snap(struct inode *inode,
1428 struct ceph_mds_session *session,
1429 struct ceph_cap_snap *capsnap,
1430 u32 mseq, u64 oldest_flush_tid)
1432 struct cap_msg_args arg;
1434 arg.session = session;
1435 arg.ino = ceph_vino(inode).ino;
1437 arg.follows = capsnap->follows;
1438 arg.flush_tid = capsnap->cap_flush.tid;
1439 arg.oldest_flush_tid = oldest_flush_tid;
1441 arg.size = capsnap->size;
1443 arg.xattr_version = capsnap->xattr_version;
1444 arg.xattr_buf = capsnap->xattr_blob;
1446 arg.atime = capsnap->atime;
1447 arg.mtime = capsnap->mtime;
1448 arg.ctime = capsnap->ctime;
1449 arg.btime = capsnap->btime;
1450 arg.change_attr = capsnap->change_attr;
1452 arg.op = CEPH_CAP_OP_FLUSHSNAP;
1453 arg.caps = capsnap->issued;
1455 arg.dirty = capsnap->dirty;
1460 arg.time_warp_seq = capsnap->time_warp_seq;
1462 arg.uid = capsnap->uid;
1463 arg.gid = capsnap->gid;
1464 arg.mode = capsnap->mode;
1466 arg.inline_data = capsnap->inline_data;
1469 return send_cap_msg(&arg);
1473 * When a snapshot is taken, clients accumulate dirty metadata on
1474 * inodes with capabilities in ceph_cap_snaps to describe the file
1475 * state at the time the snapshot was taken. This must be flushed
1476 * asynchronously back to the MDS once sync writes complete and dirty
1477 * data is written out.
1479 * Called under i_ceph_lock. Takes s_mutex as needed.
1481 static void __ceph_flush_snaps(struct ceph_inode_info *ci,
1482 struct ceph_mds_session *session)
1483 __releases(ci->i_ceph_lock)
1484 __acquires(ci->i_ceph_lock)
1486 struct inode *inode = &ci->vfs_inode;
1487 struct ceph_mds_client *mdsc = session->s_mdsc;
1488 struct ceph_cap_snap *capsnap;
1489 u64 oldest_flush_tid = 0;
1490 u64 first_tid = 1, last_tid = 0;
1492 dout("__flush_snaps %p session %p\n", inode, session);
1494 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1496 * we need to wait for sync writes to complete and for dirty
1497 * pages to be written out.
1499 if (capsnap->dirty_pages || capsnap->writing)
1502 /* should be removed by ceph_try_drop_cap_snap() */
1503 BUG_ON(!capsnap->need_flush);
1505 /* only flush each capsnap once */
1506 if (capsnap->cap_flush.tid > 0) {
1507 dout(" already flushed %p, skipping\n", capsnap);
1511 spin_lock(&mdsc->cap_dirty_lock);
1512 capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
1513 list_add_tail(&capsnap->cap_flush.g_list,
1514 &mdsc->cap_flush_list);
1515 if (oldest_flush_tid == 0)
1516 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1517 if (list_empty(&ci->i_flushing_item)) {
1518 list_add_tail(&ci->i_flushing_item,
1519 &session->s_cap_flushing);
1521 spin_unlock(&mdsc->cap_dirty_lock);
1523 list_add_tail(&capsnap->cap_flush.i_list,
1524 &ci->i_cap_flush_list);
1527 first_tid = capsnap->cap_flush.tid;
1528 last_tid = capsnap->cap_flush.tid;
1531 ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS;
1533 while (first_tid <= last_tid) {
1534 struct ceph_cap *cap = ci->i_auth_cap;
1535 struct ceph_cap_flush *cf;
1538 if (!(cap && cap->session == session)) {
1539 dout("__flush_snaps %p auth cap %p not mds%d, "
1540 "stop\n", inode, cap, session->s_mds);
1545 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
1546 if (cf->tid >= first_tid) {
1554 first_tid = cf->tid + 1;
1556 capsnap = container_of(cf, struct ceph_cap_snap, cap_flush);
1557 refcount_inc(&capsnap->nref);
1558 spin_unlock(&ci->i_ceph_lock);
1560 dout("__flush_snaps %p capsnap %p tid %llu %s\n",
1561 inode, capsnap, cf->tid, ceph_cap_string(capsnap->dirty));
1563 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
1566 pr_err("__flush_snaps: error sending cap flushsnap, "
1567 "ino (%llx.%llx) tid %llu follows %llu\n",
1568 ceph_vinop(inode), cf->tid, capsnap->follows);
1571 ceph_put_cap_snap(capsnap);
1572 spin_lock(&ci->i_ceph_lock);
1576 void ceph_flush_snaps(struct ceph_inode_info *ci,
1577 struct ceph_mds_session **psession)
1579 struct inode *inode = &ci->vfs_inode;
1580 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
1581 struct ceph_mds_session *session = NULL;
1584 dout("ceph_flush_snaps %p\n", inode);
1586 session = *psession;
1588 spin_lock(&ci->i_ceph_lock);
1589 if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) {
1590 dout(" no capsnap needs flush, doing nothing\n");
1593 if (!ci->i_auth_cap) {
1594 dout(" no auth cap (migrating?), doing nothing\n");
1598 mds = ci->i_auth_cap->session->s_mds;
1599 if (session && session->s_mds != mds) {
1600 dout(" oops, wrong session %p mutex\n", session);
1601 mutex_unlock(&session->s_mutex);
1602 ceph_put_mds_session(session);
1606 spin_unlock(&ci->i_ceph_lock);
1607 mutex_lock(&mdsc->mutex);
1608 session = __ceph_lookup_mds_session(mdsc, mds);
1609 mutex_unlock(&mdsc->mutex);
1611 dout(" inverting session/ino locks on %p\n", session);
1612 mutex_lock(&session->s_mutex);
1617 // make sure flushsnap messages are sent in proper order.
1618 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
1619 __kick_flushing_caps(mdsc, session, ci, 0);
1621 __ceph_flush_snaps(ci, session);
1623 spin_unlock(&ci->i_ceph_lock);
1626 *psession = session;
1627 } else if (session) {
1628 mutex_unlock(&session->s_mutex);
1629 ceph_put_mds_session(session);
1631 /* we flushed them all; remove this inode from the queue */
1632 spin_lock(&mdsc->snap_flush_lock);
1633 list_del_init(&ci->i_snap_flush_item);
1634 spin_unlock(&mdsc->snap_flush_lock);
1638 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1639 * Caller is then responsible for calling __mark_inode_dirty with the
1640 * returned flags value.
1642 int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
1643 struct ceph_cap_flush **pcf)
1645 struct ceph_mds_client *mdsc =
1646 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
1647 struct inode *inode = &ci->vfs_inode;
1648 int was = ci->i_dirty_caps;
1651 if (!ci->i_auth_cap) {
1652 pr_warn("__mark_dirty_caps %p %llx mask %s, "
1653 "but no auth cap (session was closed?)\n",
1654 inode, ceph_ino(inode), ceph_cap_string(mask));
1658 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1659 ceph_cap_string(mask), ceph_cap_string(was),
1660 ceph_cap_string(was | mask));
1661 ci->i_dirty_caps |= mask;
1663 WARN_ON_ONCE(ci->i_prealloc_cap_flush);
1664 swap(ci->i_prealloc_cap_flush, *pcf);
1666 if (!ci->i_head_snapc) {
1667 WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
1668 ci->i_head_snapc = ceph_get_snap_context(
1669 ci->i_snap_realm->cached_context);
1671 dout(" inode %p now dirty snapc %p auth cap %p\n",
1672 &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
1673 BUG_ON(!list_empty(&ci->i_dirty_item));
1674 spin_lock(&mdsc->cap_dirty_lock);
1675 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
1676 spin_unlock(&mdsc->cap_dirty_lock);
1677 if (ci->i_flushing_caps == 0) {
1679 dirty |= I_DIRTY_SYNC;
1682 WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
1684 BUG_ON(list_empty(&ci->i_dirty_item));
1685 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1686 (mask & CEPH_CAP_FILE_BUFFER))
1687 dirty |= I_DIRTY_DATASYNC;
1688 __cap_delay_requeue(mdsc, ci, true);
1692 struct ceph_cap_flush *ceph_alloc_cap_flush(void)
1694 return kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
1697 void ceph_free_cap_flush(struct ceph_cap_flush *cf)
1700 kmem_cache_free(ceph_cap_flush_cachep, cf);
1703 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
1705 if (!list_empty(&mdsc->cap_flush_list)) {
1706 struct ceph_cap_flush *cf =
1707 list_first_entry(&mdsc->cap_flush_list,
1708 struct ceph_cap_flush, g_list);
1715 * Remove cap_flush from the mdsc's or inode's flushing cap list.
1716 * Return true if caller needs to wake up flush waiters.
1718 static bool __finish_cap_flush(struct ceph_mds_client *mdsc,
1719 struct ceph_inode_info *ci,
1720 struct ceph_cap_flush *cf)
1722 struct ceph_cap_flush *prev;
1723 bool wake = cf->wake;
1725 /* are there older pending cap flushes? */
1726 if (wake && cf->g_list.prev != &mdsc->cap_flush_list) {
1727 prev = list_prev_entry(cf, g_list);
1731 list_del(&cf->g_list);
1733 if (wake && cf->i_list.prev != &ci->i_cap_flush_list) {
1734 prev = list_prev_entry(cf, i_list);
1738 list_del(&cf->i_list);
1746 * Add dirty inode to the flushing list. Assigned a seq number so we
1747 * can wait for caps to flush without starving.
1749 * Called under i_ceph_lock.
1751 static int __mark_caps_flushing(struct inode *inode,
1752 struct ceph_mds_session *session, bool wake,
1753 u64 *flush_tid, u64 *oldest_flush_tid)
1755 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1756 struct ceph_inode_info *ci = ceph_inode(inode);
1757 struct ceph_cap_flush *cf = NULL;
1760 BUG_ON(ci->i_dirty_caps == 0);
1761 BUG_ON(list_empty(&ci->i_dirty_item));
1762 BUG_ON(!ci->i_prealloc_cap_flush);
1764 flushing = ci->i_dirty_caps;
1765 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1766 ceph_cap_string(flushing),
1767 ceph_cap_string(ci->i_flushing_caps),
1768 ceph_cap_string(ci->i_flushing_caps | flushing));
1769 ci->i_flushing_caps |= flushing;
1770 ci->i_dirty_caps = 0;
1771 dout(" inode %p now !dirty\n", inode);
1773 swap(cf, ci->i_prealloc_cap_flush);
1774 cf->caps = flushing;
1777 spin_lock(&mdsc->cap_dirty_lock);
1778 list_del_init(&ci->i_dirty_item);
1780 cf->tid = ++mdsc->last_cap_flush_tid;
1781 list_add_tail(&cf->g_list, &mdsc->cap_flush_list);
1782 *oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1784 if (list_empty(&ci->i_flushing_item)) {
1785 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1786 mdsc->num_cap_flushing++;
1788 spin_unlock(&mdsc->cap_dirty_lock);
1790 list_add_tail(&cf->i_list, &ci->i_cap_flush_list);
1792 *flush_tid = cf->tid;
1797 * try to invalidate mapping pages without blocking.
1799 static int try_nonblocking_invalidate(struct inode *inode)
1801 struct ceph_inode_info *ci = ceph_inode(inode);
1802 u32 invalidating_gen = ci->i_rdcache_gen;
1804 spin_unlock(&ci->i_ceph_lock);
1805 invalidate_mapping_pages(&inode->i_data, 0, -1);
1806 spin_lock(&ci->i_ceph_lock);
1808 if (inode->i_data.nrpages == 0 &&
1809 invalidating_gen == ci->i_rdcache_gen) {
1811 dout("try_nonblocking_invalidate %p success\n", inode);
1812 /* save any racing async invalidate some trouble */
1813 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
1816 dout("try_nonblocking_invalidate %p failed\n", inode);
1820 bool __ceph_should_report_size(struct ceph_inode_info *ci)
1822 loff_t size = ci->vfs_inode.i_size;
1823 /* mds will adjust max size according to the reported size */
1824 if (ci->i_flushing_caps & CEPH_CAP_FILE_WR)
1826 if (size >= ci->i_max_size)
1828 /* half of previous max_size increment has been used */
1829 if (ci->i_max_size > ci->i_reported_size &&
1830 (size << 1) >= ci->i_max_size + ci->i_reported_size)
1836 * Swiss army knife function to examine currently used and wanted
1837 * versus held caps. Release, flush, ack revoked caps to mds as
1840 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1841 * cap release further.
1842 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1843 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1846 void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1847 struct ceph_mds_session *session)
1849 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1850 struct ceph_mds_client *mdsc = fsc->mdsc;
1851 struct inode *inode = &ci->vfs_inode;
1852 struct ceph_cap *cap;
1853 u64 flush_tid, oldest_flush_tid;
1854 int file_wanted, used, cap_used;
1855 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
1856 int issued, implemented, want, retain, revoking, flushing = 0;
1857 int mds = -1; /* keep track of how far we've gone through i_caps list
1858 to avoid an infinite loop on retry */
1860 int delayed = 0, sent = 0;
1861 bool no_delay = flags & CHECK_CAPS_NODELAY;
1862 bool queue_invalidate = false;
1863 bool tried_invalidate = false;
1865 /* if we are unmounting, flush any unused caps immediately. */
1869 spin_lock(&ci->i_ceph_lock);
1871 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1872 flags |= CHECK_CAPS_FLUSH;
1874 if (!(flags & CHECK_CAPS_AUTHONLY) ||
1875 (ci->i_auth_cap && __ceph_is_single_caps(ci)))
1876 __cap_delay_cancel(mdsc, ci);
1880 spin_lock(&ci->i_ceph_lock);
1882 file_wanted = __ceph_caps_file_wanted(ci);
1883 used = __ceph_caps_used(ci);
1884 issued = __ceph_caps_issued(ci, &implemented);
1885 revoking = implemented & ~issued;
1888 retain = file_wanted | used | CEPH_CAP_PIN;
1889 if (!mdsc->stopping && inode->i_nlink > 0) {
1891 retain |= CEPH_CAP_ANY; /* be greedy */
1892 } else if (S_ISDIR(inode->i_mode) &&
1893 (issued & CEPH_CAP_FILE_SHARED) &&
1894 __ceph_dir_is_complete(ci)) {
1896 * If a directory is complete, we want to keep
1897 * the exclusive cap. So that MDS does not end up
1898 * revoking the shared cap on every create/unlink
1901 if (IS_RDONLY(inode))
1902 want = CEPH_CAP_ANY_SHARED;
1904 want = CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
1908 retain |= CEPH_CAP_ANY_SHARED;
1910 * keep RD only if we didn't have the file open RW,
1911 * because then the mds would revoke it anyway to
1912 * journal max_size=0.
1914 if (ci->i_max_size == 0)
1915 retain |= CEPH_CAP_ANY_RD;
1919 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1920 " issued %s revoking %s retain %s %s%s%s\n", inode,
1921 ceph_cap_string(file_wanted),
1922 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1923 ceph_cap_string(ci->i_flushing_caps),
1924 ceph_cap_string(issued), ceph_cap_string(revoking),
1925 ceph_cap_string(retain),
1926 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1927 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1928 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1931 * If we no longer need to hold onto old our caps, and we may
1932 * have cached pages, but don't want them, then try to invalidate.
1933 * If we fail, it's because pages are locked.... try again later.
1935 if ((!no_delay || mdsc->stopping) &&
1936 !S_ISDIR(inode->i_mode) && /* ignore readdir cache */
1937 !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */
1938 inode->i_data.nrpages && /* have cached pages */
1939 (revoking & (CEPH_CAP_FILE_CACHE|
1940 CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */
1941 !tried_invalidate) {
1942 dout("check_caps trying to invalidate on %p\n", inode);
1943 if (try_nonblocking_invalidate(inode) < 0) {
1944 dout("check_caps queuing invalidate\n");
1945 queue_invalidate = true;
1946 ci->i_rdcache_revoking = ci->i_rdcache_gen;
1948 tried_invalidate = true;
1952 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1953 cap = rb_entry(p, struct ceph_cap, ci_node);
1955 /* avoid looping forever */
1956 if (mds >= cap->mds ||
1957 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1960 /* NOTE: no side-effects allowed, until we take s_mutex */
1963 if (ci->i_auth_cap && cap != ci->i_auth_cap)
1964 cap_used &= ~ci->i_auth_cap->issued;
1966 revoking = cap->implemented & ~cap->issued;
1967 dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
1968 cap->mds, cap, ceph_cap_string(cap_used),
1969 ceph_cap_string(cap->issued),
1970 ceph_cap_string(cap->implemented),
1971 ceph_cap_string(revoking));
1973 if (cap == ci->i_auth_cap &&
1974 (cap->issued & CEPH_CAP_FILE_WR)) {
1975 /* request larger max_size from MDS? */
1976 if (ci->i_wanted_max_size > ci->i_max_size &&
1977 ci->i_wanted_max_size > ci->i_requested_max_size) {
1978 dout("requesting new max_size\n");
1982 /* approaching file_max? */
1983 if (__ceph_should_report_size(ci)) {
1984 dout("i_size approaching max_size\n");
1988 /* flush anything dirty? */
1989 if (cap == ci->i_auth_cap) {
1990 if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) {
1991 dout("flushing dirty caps\n");
1994 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) {
1995 dout("flushing snap caps\n");
2000 /* completed revocation? going down and there are no caps? */
2001 if (revoking && (revoking & cap_used) == 0) {
2002 dout("completed revocation of %s\n",
2003 ceph_cap_string(cap->implemented & ~cap->issued));
2007 /* want more caps from mds? */
2008 if (want & ~(cap->mds_wanted | cap->issued))
2011 /* things we might delay */
2012 if ((cap->issued & ~retain) == 0)
2013 continue; /* nope, all good */
2019 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
2020 time_before(jiffies, ci->i_hold_caps_max)) {
2021 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
2022 ceph_cap_string(cap->issued),
2023 ceph_cap_string(cap->issued & retain),
2024 ceph_cap_string(cap->mds_wanted),
2025 ceph_cap_string(want));
2031 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
2032 dout(" skipping %p I_NOFLUSH set\n", inode);
2036 if (session && session != cap->session) {
2037 dout("oops, wrong session %p mutex\n", session);
2038 mutex_unlock(&session->s_mutex);
2042 session = cap->session;
2043 if (mutex_trylock(&session->s_mutex) == 0) {
2044 dout("inverting session/ino locks on %p\n",
2046 spin_unlock(&ci->i_ceph_lock);
2047 if (took_snap_rwsem) {
2048 up_read(&mdsc->snap_rwsem);
2049 took_snap_rwsem = 0;
2051 mutex_lock(&session->s_mutex);
2056 /* kick flushing and flush snaps before sending normal
2058 if (cap == ci->i_auth_cap &&
2060 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) {
2061 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
2062 __kick_flushing_caps(mdsc, session, ci, 0);
2063 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
2064 __ceph_flush_snaps(ci, session);
2069 /* take snap_rwsem after session mutex */
2070 if (!took_snap_rwsem) {
2071 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
2072 dout("inverting snap/in locks on %p\n",
2074 spin_unlock(&ci->i_ceph_lock);
2075 down_read(&mdsc->snap_rwsem);
2076 took_snap_rwsem = 1;
2079 took_snap_rwsem = 1;
2082 if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
2083 flushing = __mark_caps_flushing(inode, session, false,
2089 spin_lock(&mdsc->cap_dirty_lock);
2090 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2091 spin_unlock(&mdsc->cap_dirty_lock);
2094 mds = cap->mds; /* remember mds, so we don't repeat */
2097 /* __send_cap drops i_ceph_lock */
2098 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, 0,
2099 cap_used, want, retain, flushing,
2100 flush_tid, oldest_flush_tid);
2101 goto retry; /* retake i_ceph_lock and restart our cap scan. */
2104 /* Reschedule delayed caps release if we delayed anything */
2106 __cap_delay_requeue(mdsc, ci, false);
2108 spin_unlock(&ci->i_ceph_lock);
2110 if (queue_invalidate)
2111 ceph_queue_invalidate(inode);
2114 mutex_unlock(&session->s_mutex);
2115 if (took_snap_rwsem)
2116 up_read(&mdsc->snap_rwsem);
2120 * Try to flush dirty caps back to the auth mds.
2122 static int try_flush_caps(struct inode *inode, u64 *ptid)
2124 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2125 struct ceph_inode_info *ci = ceph_inode(inode);
2126 struct ceph_mds_session *session = NULL;
2128 u64 flush_tid = 0, oldest_flush_tid = 0;
2131 spin_lock(&ci->i_ceph_lock);
2133 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
2134 spin_unlock(&ci->i_ceph_lock);
2135 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
2138 if (ci->i_dirty_caps && ci->i_auth_cap) {
2139 struct ceph_cap *cap = ci->i_auth_cap;
2142 if (!session || session != cap->session) {
2143 spin_unlock(&ci->i_ceph_lock);
2145 mutex_unlock(&session->s_mutex);
2146 session = cap->session;
2147 mutex_lock(&session->s_mutex);
2150 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) {
2151 spin_unlock(&ci->i_ceph_lock);
2155 if (ci->i_ceph_flags &
2156 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS)) {
2157 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
2158 __kick_flushing_caps(mdsc, session, ci, 0);
2159 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
2160 __ceph_flush_snaps(ci, session);
2164 flushing = __mark_caps_flushing(inode, session, true,
2165 &flush_tid, &oldest_flush_tid);
2167 /* __send_cap drops i_ceph_lock */
2168 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
2169 CEPH_CLIENT_CAPS_SYNC,
2170 __ceph_caps_used(ci),
2171 __ceph_caps_wanted(ci),
2172 (cap->issued | cap->implemented),
2173 flushing, flush_tid, oldest_flush_tid);
2176 spin_lock(&ci->i_ceph_lock);
2177 __cap_delay_requeue(mdsc, ci, true);
2178 spin_unlock(&ci->i_ceph_lock);
2181 if (!list_empty(&ci->i_cap_flush_list)) {
2182 struct ceph_cap_flush *cf =
2183 list_last_entry(&ci->i_cap_flush_list,
2184 struct ceph_cap_flush, i_list);
2186 flush_tid = cf->tid;
2188 flushing = ci->i_flushing_caps;
2189 spin_unlock(&ci->i_ceph_lock);
2193 mutex_unlock(&session->s_mutex);
2200 * Return true if we've flushed caps through the given flush_tid.
2202 static int caps_are_flushed(struct inode *inode, u64 flush_tid)
2204 struct ceph_inode_info *ci = ceph_inode(inode);
2207 spin_lock(&ci->i_ceph_lock);
2208 if (!list_empty(&ci->i_cap_flush_list)) {
2209 struct ceph_cap_flush * cf =
2210 list_first_entry(&ci->i_cap_flush_list,
2211 struct ceph_cap_flush, i_list);
2212 if (cf->tid <= flush_tid)
2215 spin_unlock(&ci->i_ceph_lock);
2220 * wait for any unsafe requests to complete.
2222 static int unsafe_request_wait(struct inode *inode)
2224 struct ceph_inode_info *ci = ceph_inode(inode);
2225 struct ceph_mds_request *req1 = NULL, *req2 = NULL;
2228 spin_lock(&ci->i_unsafe_lock);
2229 if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
2230 req1 = list_last_entry(&ci->i_unsafe_dirops,
2231 struct ceph_mds_request,
2233 ceph_mdsc_get_request(req1);
2235 if (!list_empty(&ci->i_unsafe_iops)) {
2236 req2 = list_last_entry(&ci->i_unsafe_iops,
2237 struct ceph_mds_request,
2238 r_unsafe_target_item);
2239 ceph_mdsc_get_request(req2);
2241 spin_unlock(&ci->i_unsafe_lock);
2243 dout("unsafe_request_wait %p wait on tid %llu %llu\n",
2244 inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL);
2246 ret = !wait_for_completion_timeout(&req1->r_safe_completion,
2247 ceph_timeout_jiffies(req1->r_timeout));
2250 ceph_mdsc_put_request(req1);
2253 ret = !wait_for_completion_timeout(&req2->r_safe_completion,
2254 ceph_timeout_jiffies(req2->r_timeout));
2257 ceph_mdsc_put_request(req2);
2262 int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2264 struct inode *inode = file->f_mapping->host;
2265 struct ceph_inode_info *ci = ceph_inode(inode);
2270 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
2272 ret = file_write_and_wait_range(file, start, end);
2279 dirty = try_flush_caps(inode, &flush_tid);
2280 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
2282 ret = unsafe_request_wait(inode);
2285 * only wait on non-file metadata writeback (the mds
2286 * can recover size and mtime, so we don't need to
2289 if (!ret && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
2290 ret = wait_event_interruptible(ci->i_cap_wq,
2291 caps_are_flushed(inode, flush_tid));
2294 dout("fsync %p%s result=%d\n", inode, datasync ? " datasync" : "", ret);
2299 * Flush any dirty caps back to the mds. If we aren't asked to wait,
2300 * queue inode for flush but don't do so immediately, because we can
2301 * get by with fewer MDS messages if we wait for data writeback to
2304 int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
2306 struct ceph_inode_info *ci = ceph_inode(inode);
2310 int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync);
2312 dout("write_inode %p wait=%d\n", inode, wait);
2314 dirty = try_flush_caps(inode, &flush_tid);
2316 err = wait_event_interruptible(ci->i_cap_wq,
2317 caps_are_flushed(inode, flush_tid));
2319 struct ceph_mds_client *mdsc =
2320 ceph_sb_to_client(inode->i_sb)->mdsc;
2322 spin_lock(&ci->i_ceph_lock);
2323 if (__ceph_caps_dirty(ci))
2324 __cap_delay_requeue_front(mdsc, ci);
2325 spin_unlock(&ci->i_ceph_lock);
2330 static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
2331 struct ceph_mds_session *session,
2332 struct ceph_inode_info *ci,
2333 u64 oldest_flush_tid)
2334 __releases(ci->i_ceph_lock)
2335 __acquires(ci->i_ceph_lock)
2337 struct inode *inode = &ci->vfs_inode;
2338 struct ceph_cap *cap;
2339 struct ceph_cap_flush *cf;
2342 u64 last_snap_flush = 0;
2344 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2346 list_for_each_entry_reverse(cf, &ci->i_cap_flush_list, i_list) {
2348 last_snap_flush = cf->tid;
2353 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
2354 if (cf->tid < first_tid)
2357 cap = ci->i_auth_cap;
2358 if (!(cap && cap->session == session)) {
2359 pr_err("%p auth cap %p not mds%d ???\n",
2360 inode, cap, session->s_mds);
2364 first_tid = cf->tid + 1;
2367 dout("kick_flushing_caps %p cap %p tid %llu %s\n",
2368 inode, cap, cf->tid, ceph_cap_string(cf->caps));
2369 ci->i_ceph_flags |= CEPH_I_NODELAY;
2371 ret = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
2372 (cf->tid < last_snap_flush ?
2373 CEPH_CLIENT_CAPS_PENDING_CAPSNAP : 0),
2374 __ceph_caps_used(ci),
2375 __ceph_caps_wanted(ci),
2376 (cap->issued | cap->implemented),
2377 cf->caps, cf->tid, oldest_flush_tid);
2379 pr_err("kick_flushing_caps: error sending "
2380 "cap flush, ino (%llx.%llx) "
2381 "tid %llu flushing %s\n",
2382 ceph_vinop(inode), cf->tid,
2383 ceph_cap_string(cf->caps));
2386 struct ceph_cap_snap *capsnap =
2387 container_of(cf, struct ceph_cap_snap,
2389 dout("kick_flushing_caps %p capsnap %p tid %llu %s\n",
2390 inode, capsnap, cf->tid,
2391 ceph_cap_string(capsnap->dirty));
2393 refcount_inc(&capsnap->nref);
2394 spin_unlock(&ci->i_ceph_lock);
2396 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
2399 pr_err("kick_flushing_caps: error sending "
2400 "cap flushsnap, ino (%llx.%llx) "
2401 "tid %llu follows %llu\n",
2402 ceph_vinop(inode), cf->tid,
2406 ceph_put_cap_snap(capsnap);
2409 spin_lock(&ci->i_ceph_lock);
2413 void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
2414 struct ceph_mds_session *session)
2416 struct ceph_inode_info *ci;
2417 struct ceph_cap *cap;
2418 u64 oldest_flush_tid;
2420 dout("early_kick_flushing_caps mds%d\n", session->s_mds);
2422 spin_lock(&mdsc->cap_dirty_lock);
2423 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2424 spin_unlock(&mdsc->cap_dirty_lock);
2426 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2427 spin_lock(&ci->i_ceph_lock);
2428 cap = ci->i_auth_cap;
2429 if (!(cap && cap->session == session)) {
2430 pr_err("%p auth cap %p not mds%d ???\n",
2431 &ci->vfs_inode, cap, session->s_mds);
2432 spin_unlock(&ci->i_ceph_lock);
2438 * if flushing caps were revoked, we re-send the cap flush
2439 * in client reconnect stage. This guarantees MDS * processes
2440 * the cap flush message before issuing the flushing caps to
2443 if ((cap->issued & ci->i_flushing_caps) !=
2444 ci->i_flushing_caps) {
2445 /* encode_caps_cb() also will reset these sequence
2446 * numbers. make sure sequence numbers in cap flush
2447 * message match later reconnect message */
2451 __kick_flushing_caps(mdsc, session, ci,
2454 ci->i_ceph_flags |= CEPH_I_KICK_FLUSH;
2457 spin_unlock(&ci->i_ceph_lock);
2461 void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
2462 struct ceph_mds_session *session)
2464 struct ceph_inode_info *ci;
2465 struct ceph_cap *cap;
2466 u64 oldest_flush_tid;
2468 dout("kick_flushing_caps mds%d\n", session->s_mds);
2470 spin_lock(&mdsc->cap_dirty_lock);
2471 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2472 spin_unlock(&mdsc->cap_dirty_lock);
2474 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2475 spin_lock(&ci->i_ceph_lock);
2476 cap = ci->i_auth_cap;
2477 if (!(cap && cap->session == session)) {
2478 pr_err("%p auth cap %p not mds%d ???\n",
2479 &ci->vfs_inode, cap, session->s_mds);
2480 spin_unlock(&ci->i_ceph_lock);
2483 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
2484 __kick_flushing_caps(mdsc, session, ci,
2487 spin_unlock(&ci->i_ceph_lock);
2491 static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
2492 struct ceph_mds_session *session,
2493 struct inode *inode)
2494 __releases(ci->i_ceph_lock)
2496 struct ceph_inode_info *ci = ceph_inode(inode);
2497 struct ceph_cap *cap;
2499 cap = ci->i_auth_cap;
2500 dout("kick_flushing_inode_caps %p flushing %s\n", inode,
2501 ceph_cap_string(ci->i_flushing_caps));
2503 if (!list_empty(&ci->i_cap_flush_list)) {
2504 u64 oldest_flush_tid;
2505 spin_lock(&mdsc->cap_dirty_lock);
2506 list_move_tail(&ci->i_flushing_item,
2507 &cap->session->s_cap_flushing);
2508 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2509 spin_unlock(&mdsc->cap_dirty_lock);
2511 __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid);
2512 spin_unlock(&ci->i_ceph_lock);
2514 spin_unlock(&ci->i_ceph_lock);
2520 * Take references to capabilities we hold, so that we don't release
2521 * them to the MDS prematurely.
2523 * Protected by i_ceph_lock.
2525 static void __take_cap_refs(struct ceph_inode_info *ci, int got,
2526 bool snap_rwsem_locked)
2528 if (got & CEPH_CAP_PIN)
2530 if (got & CEPH_CAP_FILE_RD)
2532 if (got & CEPH_CAP_FILE_CACHE)
2533 ci->i_rdcache_ref++;
2534 if (got & CEPH_CAP_FILE_WR) {
2535 if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
2536 BUG_ON(!snap_rwsem_locked);
2537 ci->i_head_snapc = ceph_get_snap_context(
2538 ci->i_snap_realm->cached_context);
2542 if (got & CEPH_CAP_FILE_BUFFER) {
2543 if (ci->i_wb_ref == 0)
2544 ihold(&ci->vfs_inode);
2546 dout("__take_cap_refs %p wb %d -> %d (?)\n",
2547 &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
2552 * Try to grab cap references. Specify those refs we @want, and the
2553 * minimal set we @need. Also include the larger offset we are writing
2554 * to (when applicable), and check against max_size here as well.
2555 * Note that caller is responsible for ensuring max_size increases are
2556 * requested from the MDS.
2558 * Returns 0 if caps were not able to be acquired (yet), a 1 if they were,
2559 * or a negative error code.
2561 * FIXME: how does a 0 return differ from -EAGAIN?
2563 static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
2564 loff_t endoff, bool nonblock, int *got)
2566 struct inode *inode = &ci->vfs_inode;
2567 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
2569 int have, implemented;
2571 bool snap_rwsem_locked = false;
2573 dout("get_cap_refs %p need %s want %s\n", inode,
2574 ceph_cap_string(need), ceph_cap_string(want));
2577 spin_lock(&ci->i_ceph_lock);
2579 /* make sure file is actually open */
2580 file_wanted = __ceph_caps_file_wanted(ci);
2581 if ((file_wanted & need) != need) {
2582 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2583 ceph_cap_string(need), ceph_cap_string(file_wanted));
2588 /* finish pending truncate */
2589 while (ci->i_truncate_pending) {
2590 spin_unlock(&ci->i_ceph_lock);
2591 if (snap_rwsem_locked) {
2592 up_read(&mdsc->snap_rwsem);
2593 snap_rwsem_locked = false;
2595 __ceph_do_pending_vmtruncate(inode);
2596 spin_lock(&ci->i_ceph_lock);
2599 have = __ceph_caps_issued(ci, &implemented);
2601 if (have & need & CEPH_CAP_FILE_WR) {
2602 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2603 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2604 inode, endoff, ci->i_max_size);
2605 if (endoff > ci->i_requested_max_size)
2610 * If a sync write is in progress, we must wait, so that we
2611 * can get a final snapshot value for size+mtime.
2613 if (__ceph_have_pending_cap_snap(ci)) {
2614 dout("get_cap_refs %p cap_snap_pending\n", inode);
2619 if ((have & need) == need) {
2621 * Look at (implemented & ~have & not) so that we keep waiting
2622 * on transition from wanted -> needed caps. This is needed
2623 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2624 * going before a prior buffered writeback happens.
2626 int not = want & ~(have & need);
2627 int revoking = implemented & ~have;
2628 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2629 inode, ceph_cap_string(have), ceph_cap_string(not),
2630 ceph_cap_string(revoking));
2631 if ((revoking & not) == 0) {
2632 if (!snap_rwsem_locked &&
2633 !ci->i_head_snapc &&
2634 (need & CEPH_CAP_FILE_WR)) {
2635 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2637 * we can not call down_read() when
2638 * task isn't in TASK_RUNNING state
2645 spin_unlock(&ci->i_ceph_lock);
2646 down_read(&mdsc->snap_rwsem);
2647 snap_rwsem_locked = true;
2650 snap_rwsem_locked = true;
2652 *got = need | (have & want);
2653 if ((need & CEPH_CAP_FILE_RD) &&
2654 !(*got & CEPH_CAP_FILE_CACHE))
2655 ceph_disable_fscache_readpage(ci);
2656 __take_cap_refs(ci, *got, true);
2660 int session_readonly = false;
2661 if ((need & CEPH_CAP_FILE_WR) && ci->i_auth_cap) {
2662 struct ceph_mds_session *s = ci->i_auth_cap->session;
2663 spin_lock(&s->s_cap_lock);
2664 session_readonly = s->s_readonly;
2665 spin_unlock(&s->s_cap_lock);
2667 if (session_readonly) {
2668 dout("get_cap_refs %p needed %s but mds%d readonly\n",
2669 inode, ceph_cap_string(need), ci->i_auth_cap->mds);
2674 if (ci->i_ceph_flags & CEPH_I_CAP_DROPPED) {
2676 if (READ_ONCE(mdsc->fsc->mount_state) ==
2677 CEPH_MOUNT_SHUTDOWN) {
2678 dout("get_cap_refs %p forced umount\n", inode);
2682 mds_wanted = __ceph_caps_mds_wanted(ci, false);
2683 if (need & ~(mds_wanted & need)) {
2684 dout("get_cap_refs %p caps were dropped"
2685 " (session killed?)\n", inode);
2689 if (!(file_wanted & ~mds_wanted))
2690 ci->i_ceph_flags &= ~CEPH_I_CAP_DROPPED;
2693 dout("get_cap_refs %p have %s needed %s\n", inode,
2694 ceph_cap_string(have), ceph_cap_string(need));
2697 spin_unlock(&ci->i_ceph_lock);
2698 if (snap_rwsem_locked)
2699 up_read(&mdsc->snap_rwsem);
2701 dout("get_cap_refs %p ret %d got %s\n", inode,
2702 ret, ceph_cap_string(*got));
2707 * Check the offset we are writing up to against our current
2708 * max_size. If necessary, tell the MDS we want to write to
2711 static void check_max_size(struct inode *inode, loff_t endoff)
2713 struct ceph_inode_info *ci = ceph_inode(inode);
2716 /* do we need to explicitly request a larger max_size? */
2717 spin_lock(&ci->i_ceph_lock);
2718 if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
2719 dout("write %p at large endoff %llu, req max_size\n",
2721 ci->i_wanted_max_size = endoff;
2723 /* duplicate ceph_check_caps()'s logic */
2724 if (ci->i_auth_cap &&
2725 (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
2726 ci->i_wanted_max_size > ci->i_max_size &&
2727 ci->i_wanted_max_size > ci->i_requested_max_size)
2729 spin_unlock(&ci->i_ceph_lock);
2731 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2734 int ceph_try_get_caps(struct ceph_inode_info *ci, int need, int want,
2735 bool nonblock, int *got)
2739 BUG_ON(need & ~CEPH_CAP_FILE_RD);
2740 BUG_ON(want & ~(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO|CEPH_CAP_FILE_SHARED));
2741 ret = ceph_pool_perm_check(ci, need);
2745 ret = try_get_cap_refs(ci, need, want, 0, nonblock, got);
2746 return ret == -EAGAIN ? 0 : ret;
2750 * Wait for caps, and take cap references. If we can't get a WR cap
2751 * due to a small max_size, make sure we check_max_size (and possibly
2752 * ask the mds) so we don't get hung up indefinitely.
2754 int ceph_get_caps(struct ceph_inode_info *ci, int need, int want,
2755 loff_t endoff, int *got, struct page **pinned_page)
2759 ret = ceph_pool_perm_check(ci, need);
2765 check_max_size(&ci->vfs_inode, endoff);
2768 ret = try_get_cap_refs(ci, need, want, endoff,
2773 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2774 add_wait_queue(&ci->i_cap_wq, &wait);
2776 while (!(ret = try_get_cap_refs(ci, need, want, endoff,
2778 if (signal_pending(current)) {
2782 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
2785 remove_wait_queue(&ci->i_cap_wq, &wait);
2790 if (ret == -ESTALE) {
2791 /* session was killed, try renew caps */
2792 ret = ceph_renew_caps(&ci->vfs_inode);
2799 if (ci->i_inline_version != CEPH_INLINE_NONE &&
2800 (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
2801 i_size_read(&ci->vfs_inode) > 0) {
2803 find_get_page(ci->vfs_inode.i_mapping, 0);
2805 if (PageUptodate(page)) {
2806 *pinned_page = page;
2812 * drop cap refs first because getattr while
2813 * holding * caps refs can cause deadlock.
2815 ceph_put_cap_refs(ci, _got);
2819 * getattr request will bring inline data into
2822 ret = __ceph_do_getattr(&ci->vfs_inode, NULL,
2823 CEPH_STAT_CAP_INLINE_DATA,
2832 if ((_got & CEPH_CAP_FILE_RD) && (_got & CEPH_CAP_FILE_CACHE))
2833 ceph_fscache_revalidate_cookie(ci);
2840 * Take cap refs. Caller must already know we hold at least one ref
2841 * on the caps in question or we don't know this is safe.
2843 void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2845 spin_lock(&ci->i_ceph_lock);
2846 __take_cap_refs(ci, caps, false);
2847 spin_unlock(&ci->i_ceph_lock);
2852 * drop cap_snap that is not associated with any snapshot.
2853 * we don't need to send FLUSHSNAP message for it.
2855 static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci,
2856 struct ceph_cap_snap *capsnap)
2858 if (!capsnap->need_flush &&
2859 !capsnap->writing && !capsnap->dirty_pages) {
2860 dout("dropping cap_snap %p follows %llu\n",
2861 capsnap, capsnap->follows);
2862 BUG_ON(capsnap->cap_flush.tid > 0);
2863 ceph_put_snap_context(capsnap->context);
2864 if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps))
2865 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2867 list_del(&capsnap->ci_item);
2868 ceph_put_cap_snap(capsnap);
2877 * If we released the last ref on any given cap, call ceph_check_caps
2878 * to release (or schedule a release).
2880 * If we are releasing a WR cap (from a sync write), finalize any affected
2881 * cap_snap, and wake up any waiters.
2883 void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2885 struct inode *inode = &ci->vfs_inode;
2886 int last = 0, put = 0, flushsnaps = 0, wake = 0;
2888 spin_lock(&ci->i_ceph_lock);
2889 if (had & CEPH_CAP_PIN)
2891 if (had & CEPH_CAP_FILE_RD)
2892 if (--ci->i_rd_ref == 0)
2894 if (had & CEPH_CAP_FILE_CACHE)
2895 if (--ci->i_rdcache_ref == 0)
2897 if (had & CEPH_CAP_FILE_BUFFER) {
2898 if (--ci->i_wb_ref == 0) {
2902 dout("put_cap_refs %p wb %d -> %d (?)\n",
2903 inode, ci->i_wb_ref+1, ci->i_wb_ref);
2905 if (had & CEPH_CAP_FILE_WR)
2906 if (--ci->i_wr_ref == 0) {
2908 if (__ceph_have_pending_cap_snap(ci)) {
2909 struct ceph_cap_snap *capsnap =
2910 list_last_entry(&ci->i_cap_snaps,
2911 struct ceph_cap_snap,
2913 capsnap->writing = 0;
2914 if (ceph_try_drop_cap_snap(ci, capsnap))
2916 else if (__ceph_finish_cap_snap(ci, capsnap))
2920 if (ci->i_wrbuffer_ref_head == 0 &&
2921 ci->i_dirty_caps == 0 &&
2922 ci->i_flushing_caps == 0) {
2923 BUG_ON(!ci->i_head_snapc);
2924 ceph_put_snap_context(ci->i_head_snapc);
2925 ci->i_head_snapc = NULL;
2927 /* see comment in __ceph_remove_cap() */
2928 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm)
2929 drop_inode_snap_realm(ci);
2931 spin_unlock(&ci->i_ceph_lock);
2933 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2934 last ? " last" : "", put ? " put" : "");
2936 if (last && !flushsnaps)
2937 ceph_check_caps(ci, 0, NULL);
2938 else if (flushsnaps)
2939 ceph_flush_snaps(ci, NULL);
2941 wake_up_all(&ci->i_cap_wq);
2947 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2948 * context. Adjust per-snap dirty page accounting as appropriate.
2949 * Once all dirty data for a cap_snap is flushed, flush snapped file
2950 * metadata back to the MDS. If we dropped the last ref, call
2953 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2954 struct ceph_snap_context *snapc)
2956 struct inode *inode = &ci->vfs_inode;
2957 struct ceph_cap_snap *capsnap = NULL;
2961 bool flush_snaps = false;
2962 bool complete_capsnap = false;
2964 spin_lock(&ci->i_ceph_lock);
2965 ci->i_wrbuffer_ref -= nr;
2966 if (ci->i_wrbuffer_ref == 0) {
2971 if (ci->i_head_snapc == snapc) {
2972 ci->i_wrbuffer_ref_head -= nr;
2973 if (ci->i_wrbuffer_ref_head == 0 &&
2974 ci->i_wr_ref == 0 &&
2975 ci->i_dirty_caps == 0 &&
2976 ci->i_flushing_caps == 0) {
2977 BUG_ON(!ci->i_head_snapc);
2978 ceph_put_snap_context(ci->i_head_snapc);
2979 ci->i_head_snapc = NULL;
2981 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2983 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2984 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2985 last ? " LAST" : "");
2987 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2988 if (capsnap->context == snapc) {
2994 capsnap->dirty_pages -= nr;
2995 if (capsnap->dirty_pages == 0) {
2996 complete_capsnap = true;
2997 if (!capsnap->writing) {
2998 if (ceph_try_drop_cap_snap(ci, capsnap)) {
3001 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
3006 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
3007 " snap %lld %d/%d -> %d/%d %s%s\n",
3008 inode, capsnap, capsnap->context->seq,
3009 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
3010 ci->i_wrbuffer_ref, capsnap->dirty_pages,
3011 last ? " (wrbuffer last)" : "",
3012 complete_capsnap ? " (complete capsnap)" : "");
3015 spin_unlock(&ci->i_ceph_lock);
3018 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
3019 } else if (flush_snaps) {
3020 ceph_flush_snaps(ci, NULL);
3022 if (complete_capsnap)
3023 wake_up_all(&ci->i_cap_wq);
3025 /* avoid calling iput_final() in osd dispatch threads */
3026 ceph_async_iput(inode);
3031 * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
3033 static void invalidate_aliases(struct inode *inode)
3035 struct dentry *dn, *prev = NULL;
3037 dout("invalidate_aliases inode %p\n", inode);
3038 d_prune_aliases(inode);
3040 * For non-directory inode, d_find_alias() only returns
3041 * hashed dentry. After calling d_invalidate(), the
3042 * dentry becomes unhashed.
3044 * For directory inode, d_find_alias() can return
3045 * unhashed dentry. But directory inode should have
3046 * one alias at most.
3048 while ((dn = d_find_alias(inode))) {
3062 struct cap_extra_info {
3063 struct ceph_string *pool_ns;
3073 /* currently issued */
3075 struct timespec64 btime;
3079 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
3080 * actually be a revocation if it specifies a smaller cap set.)
3082 * caller holds s_mutex and i_ceph_lock, we drop both.
3084 static void handle_cap_grant(struct inode *inode,
3085 struct ceph_mds_session *session,
3086 struct ceph_cap *cap,
3087 struct ceph_mds_caps *grant,
3088 struct ceph_buffer *xattr_buf,
3089 struct cap_extra_info *extra_info)
3090 __releases(ci->i_ceph_lock)
3091 __releases(session->s_mdsc->snap_rwsem)
3093 struct ceph_inode_info *ci = ceph_inode(inode);
3094 int seq = le32_to_cpu(grant->seq);
3095 int newcaps = le32_to_cpu(grant->caps);
3096 int used, wanted, dirty;
3097 u64 size = le64_to_cpu(grant->size);
3098 u64 max_size = le64_to_cpu(grant->max_size);
3099 unsigned char check_caps = 0;
3100 bool was_stale = cap->cap_gen < session->s_cap_gen;
3102 bool writeback = false;
3103 bool queue_trunc = false;
3104 bool queue_invalidate = false;
3105 bool deleted_inode = false;
3106 bool fill_inline = false;
3108 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
3109 inode, cap, session->s_mds, seq, ceph_cap_string(newcaps));
3110 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
3115 * If CACHE is being revoked, and we have no dirty buffers,
3116 * try to invalidate (once). (If there are dirty buffers, we
3117 * will invalidate _after_ writeback.)
3119 if (!S_ISDIR(inode->i_mode) && /* don't invalidate readdir cache */
3120 ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
3121 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3122 !(ci->i_wrbuffer_ref || ci->i_wb_ref)) {
3123 if (try_nonblocking_invalidate(inode)) {
3124 /* there were locked pages.. invalidate later
3125 in a separate thread. */
3126 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
3127 queue_invalidate = true;
3128 ci->i_rdcache_revoking = ci->i_rdcache_gen;
3134 cap->issued = cap->implemented = CEPH_CAP_PIN;
3137 * auth mds of the inode changed. we received the cap export message,
3138 * but still haven't received the cap import message. handle_cap_export
3139 * updated the new auth MDS' cap.
3141 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
3142 * that was sent before the cap import message. So don't remove caps.
3144 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
3145 WARN_ON(cap != ci->i_auth_cap);
3146 WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
3148 newcaps |= cap->issued;
3151 /* side effects now are allowed */
3152 cap->cap_gen = session->s_cap_gen;
3155 __check_cap_issue(ci, cap, newcaps);
3157 inode_set_max_iversion_raw(inode, extra_info->change_attr);
3159 if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
3160 (extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) {
3161 inode->i_mode = le32_to_cpu(grant->mode);
3162 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
3163 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
3164 ci->i_btime = extra_info->btime;
3165 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
3166 from_kuid(&init_user_ns, inode->i_uid),
3167 from_kgid(&init_user_ns, inode->i_gid));
3170 if ((newcaps & CEPH_CAP_LINK_SHARED) &&
3171 (extra_info->issued & CEPH_CAP_LINK_EXCL) == 0) {
3172 set_nlink(inode, le32_to_cpu(grant->nlink));
3173 if (inode->i_nlink == 0 &&
3174 (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
3175 deleted_inode = true;
3178 if ((extra_info->issued & CEPH_CAP_XATTR_EXCL) == 0 &&
3180 int len = le32_to_cpu(grant->xattr_len);
3181 u64 version = le64_to_cpu(grant->xattr_version);
3183 if (version > ci->i_xattrs.version) {
3184 dout(" got new xattrs v%llu on %p len %d\n",
3185 version, inode, len);
3186 if (ci->i_xattrs.blob)
3187 ceph_buffer_put(ci->i_xattrs.blob);
3188 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
3189 ci->i_xattrs.version = version;
3190 ceph_forget_all_cached_acls(inode);
3191 ceph_security_invalidate_secctx(inode);
3195 if (newcaps & CEPH_CAP_ANY_RD) {
3196 struct timespec64 mtime, atime, ctime;
3197 /* ctime/mtime/atime? */
3198 ceph_decode_timespec64(&mtime, &grant->mtime);
3199 ceph_decode_timespec64(&atime, &grant->atime);
3200 ceph_decode_timespec64(&ctime, &grant->ctime);
3201 ceph_fill_file_time(inode, extra_info->issued,
3202 le32_to_cpu(grant->time_warp_seq),
3203 &ctime, &mtime, &atime);
3206 if ((newcaps & CEPH_CAP_FILE_SHARED) && extra_info->dirstat_valid) {
3207 ci->i_files = extra_info->nfiles;
3208 ci->i_subdirs = extra_info->nsubdirs;
3211 if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
3212 /* file layout may have changed */
3213 s64 old_pool = ci->i_layout.pool_id;
3214 struct ceph_string *old_ns;
3216 ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout);
3217 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
3218 lockdep_is_held(&ci->i_ceph_lock));
3219 rcu_assign_pointer(ci->i_layout.pool_ns, extra_info->pool_ns);
3221 if (ci->i_layout.pool_id != old_pool ||
3222 extra_info->pool_ns != old_ns)
3223 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
3225 extra_info->pool_ns = old_ns;
3227 /* size/truncate_seq? */
3228 queue_trunc = ceph_fill_file_size(inode, extra_info->issued,
3229 le32_to_cpu(grant->truncate_seq),
3230 le64_to_cpu(grant->truncate_size),
3234 if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) {
3235 if (max_size != ci->i_max_size) {
3236 dout("max_size %lld -> %llu\n",
3237 ci->i_max_size, max_size);
3238 ci->i_max_size = max_size;
3239 if (max_size >= ci->i_wanted_max_size) {
3240 ci->i_wanted_max_size = 0; /* reset */
3241 ci->i_requested_max_size = 0;
3244 } else if (ci->i_wanted_max_size > ci->i_max_size &&
3245 ci->i_wanted_max_size > ci->i_requested_max_size) {
3246 /* CEPH_CAP_OP_IMPORT */
3251 /* check cap bits */
3252 wanted = __ceph_caps_wanted(ci);
3253 used = __ceph_caps_used(ci);
3254 dirty = __ceph_caps_dirty(ci);
3255 dout(" my wanted = %s, used = %s, dirty %s\n",
3256 ceph_cap_string(wanted),
3257 ceph_cap_string(used),
3258 ceph_cap_string(dirty));
3260 if ((was_stale || le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) &&
3261 (wanted & ~(cap->mds_wanted | newcaps))) {
3263 * If mds is importing cap, prior cap messages that update
3264 * 'wanted' may get dropped by mds (migrate seq mismatch).
3266 * We don't send cap message to update 'wanted' if what we
3267 * want are already issued. If mds revokes caps, cap message
3268 * that releases caps also tells mds what we want. But if
3269 * caps got revoked by mds forcedly (session stale). We may
3270 * haven't told mds what we want.
3275 /* revocation, grant, or no-op? */
3276 if (cap->issued & ~newcaps) {
3277 int revoking = cap->issued & ~newcaps;
3279 dout("revocation: %s -> %s (revoking %s)\n",
3280 ceph_cap_string(cap->issued),
3281 ceph_cap_string(newcaps),
3282 ceph_cap_string(revoking));
3283 if (revoking & used & CEPH_CAP_FILE_BUFFER)
3284 writeback = true; /* initiate writeback; will delay ack */
3285 else if (revoking == CEPH_CAP_FILE_CACHE &&
3286 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3288 ; /* do nothing yet, invalidation will be queued */
3289 else if (cap == ci->i_auth_cap)
3290 check_caps = 1; /* check auth cap only */
3292 check_caps = 2; /* check all caps */
3293 cap->issued = newcaps;
3294 cap->implemented |= newcaps;
3295 } else if (cap->issued == newcaps) {
3296 dout("caps unchanged: %s -> %s\n",
3297 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
3299 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
3300 ceph_cap_string(newcaps));
3301 /* non-auth MDS is revoking the newly grant caps ? */
3302 if (cap == ci->i_auth_cap &&
3303 __ceph_caps_revoking_other(ci, cap, newcaps))
3306 cap->issued = newcaps;
3307 cap->implemented |= newcaps; /* add bits only, to
3308 * avoid stepping on a
3309 * pending revocation */
3312 BUG_ON(cap->issued & ~cap->implemented);
3314 if (extra_info->inline_version > 0 &&
3315 extra_info->inline_version >= ci->i_inline_version) {
3316 ci->i_inline_version = extra_info->inline_version;
3317 if (ci->i_inline_version != CEPH_INLINE_NONE &&
3318 (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
3322 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
3323 if (newcaps & ~extra_info->issued)
3325 kick_flushing_inode_caps(session->s_mdsc, session, inode);
3326 up_read(&session->s_mdsc->snap_rwsem);
3328 spin_unlock(&ci->i_ceph_lock);
3332 ceph_fill_inline_data(inode, NULL, extra_info->inline_data,
3333 extra_info->inline_len);
3336 ceph_queue_vmtruncate(inode);
3340 * queue inode for writeback: we can't actually call
3341 * filemap_write_and_wait, etc. from message handler
3344 ceph_queue_writeback(inode);
3345 if (queue_invalidate)
3346 ceph_queue_invalidate(inode);
3348 invalidate_aliases(inode);
3350 wake_up_all(&ci->i_cap_wq);
3352 if (check_caps == 1)
3353 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
3355 else if (check_caps == 2)
3356 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
3358 mutex_unlock(&session->s_mutex);
3362 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
3363 * MDS has been safely committed.
3365 static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
3366 struct ceph_mds_caps *m,
3367 struct ceph_mds_session *session,
3368 struct ceph_cap *cap)
3369 __releases(ci->i_ceph_lock)
3371 struct ceph_inode_info *ci = ceph_inode(inode);
3372 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3373 struct ceph_cap_flush *cf, *tmp_cf;
3374 LIST_HEAD(to_remove);
3375 unsigned seq = le32_to_cpu(m->seq);
3376 int dirty = le32_to_cpu(m->dirty);
3379 bool wake_ci = false;
3380 bool wake_mdsc = false;
3382 list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
3383 if (cf->tid == flush_tid)
3385 if (cf->caps == 0) /* capsnap */
3387 if (cf->tid <= flush_tid) {
3388 if (__finish_cap_flush(NULL, ci, cf))
3390 list_add_tail(&cf->i_list, &to_remove);
3392 cleaned &= ~cf->caps;
3398 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
3399 " flushing %s -> %s\n",
3400 inode, session->s_mds, seq, ceph_cap_string(dirty),
3401 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
3402 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
3404 if (list_empty(&to_remove) && !cleaned)
3407 ci->i_flushing_caps &= ~cleaned;
3409 spin_lock(&mdsc->cap_dirty_lock);
3411 list_for_each_entry(cf, &to_remove, i_list) {
3412 if (__finish_cap_flush(mdsc, NULL, cf))
3416 if (ci->i_flushing_caps == 0) {
3417 if (list_empty(&ci->i_cap_flush_list)) {
3418 list_del_init(&ci->i_flushing_item);
3419 if (!list_empty(&session->s_cap_flushing)) {
3420 dout(" mds%d still flushing cap on %p\n",
3422 &list_first_entry(&session->s_cap_flushing,
3423 struct ceph_inode_info,
3424 i_flushing_item)->vfs_inode);
3427 mdsc->num_cap_flushing--;
3428 dout(" inode %p now !flushing\n", inode);
3430 if (ci->i_dirty_caps == 0) {
3431 dout(" inode %p now clean\n", inode);
3432 BUG_ON(!list_empty(&ci->i_dirty_item));
3434 if (ci->i_wr_ref == 0 &&
3435 ci->i_wrbuffer_ref_head == 0) {
3436 BUG_ON(!ci->i_head_snapc);
3437 ceph_put_snap_context(ci->i_head_snapc);
3438 ci->i_head_snapc = NULL;
3441 BUG_ON(list_empty(&ci->i_dirty_item));
3444 spin_unlock(&mdsc->cap_dirty_lock);
3447 spin_unlock(&ci->i_ceph_lock);
3449 while (!list_empty(&to_remove)) {
3450 cf = list_first_entry(&to_remove,
3451 struct ceph_cap_flush, i_list);
3452 list_del(&cf->i_list);
3453 ceph_free_cap_flush(cf);
3457 wake_up_all(&ci->i_cap_wq);
3459 wake_up_all(&mdsc->cap_flushing_wq);
3465 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
3466 * throw away our cap_snap.
3468 * Caller hold s_mutex.
3470 static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
3471 struct ceph_mds_caps *m,
3472 struct ceph_mds_session *session)
3474 struct ceph_inode_info *ci = ceph_inode(inode);
3475 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3476 u64 follows = le64_to_cpu(m->snap_follows);
3477 struct ceph_cap_snap *capsnap;
3478 bool flushed = false;
3479 bool wake_ci = false;
3480 bool wake_mdsc = false;
3482 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
3483 inode, ci, session->s_mds, follows);
3485 spin_lock(&ci->i_ceph_lock);
3486 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
3487 if (capsnap->follows == follows) {
3488 if (capsnap->cap_flush.tid != flush_tid) {
3489 dout(" cap_snap %p follows %lld tid %lld !="
3490 " %lld\n", capsnap, follows,
3491 flush_tid, capsnap->cap_flush.tid);
3497 dout(" skipping cap_snap %p follows %lld\n",
3498 capsnap, capsnap->follows);
3502 WARN_ON(capsnap->dirty_pages || capsnap->writing);
3503 dout(" removing %p cap_snap %p follows %lld\n",
3504 inode, capsnap, follows);
3505 list_del(&capsnap->ci_item);
3506 if (__finish_cap_flush(NULL, ci, &capsnap->cap_flush))
3509 spin_lock(&mdsc->cap_dirty_lock);
3511 if (list_empty(&ci->i_cap_flush_list))
3512 list_del_init(&ci->i_flushing_item);
3514 if (__finish_cap_flush(mdsc, NULL, &capsnap->cap_flush))
3517 spin_unlock(&mdsc->cap_dirty_lock);
3519 spin_unlock(&ci->i_ceph_lock);
3521 ceph_put_snap_context(capsnap->context);
3522 ceph_put_cap_snap(capsnap);
3524 wake_up_all(&ci->i_cap_wq);
3526 wake_up_all(&mdsc->cap_flushing_wq);
3532 * Handle TRUNC from MDS, indicating file truncation.
3534 * caller hold s_mutex.
3536 static void handle_cap_trunc(struct inode *inode,
3537 struct ceph_mds_caps *trunc,
3538 struct ceph_mds_session *session)
3539 __releases(ci->i_ceph_lock)
3541 struct ceph_inode_info *ci = ceph_inode(inode);
3542 int mds = session->s_mds;
3543 int seq = le32_to_cpu(trunc->seq);
3544 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
3545 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
3546 u64 size = le64_to_cpu(trunc->size);
3547 int implemented = 0;
3548 int dirty = __ceph_caps_dirty(ci);
3549 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
3550 int queue_trunc = 0;
3552 issued |= implemented | dirty;
3554 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
3555 inode, mds, seq, truncate_size, truncate_seq);
3556 queue_trunc = ceph_fill_file_size(inode, issued,
3557 truncate_seq, truncate_size, size);
3558 spin_unlock(&ci->i_ceph_lock);
3561 ceph_queue_vmtruncate(inode);
3565 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
3566 * different one. If we are the most recent migration we've seen (as
3567 * indicated by mseq), make note of the migrating cap bits for the
3568 * duration (until we see the corresponding IMPORT).
3570 * caller holds s_mutex
3572 static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
3573 struct ceph_mds_cap_peer *ph,
3574 struct ceph_mds_session *session)
3576 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
3577 struct ceph_mds_session *tsession = NULL;
3578 struct ceph_cap *cap, *tcap, *new_cap = NULL;
3579 struct ceph_inode_info *ci = ceph_inode(inode);
3581 unsigned mseq = le32_to_cpu(ex->migrate_seq);
3582 unsigned t_seq, t_mseq;
3584 int mds = session->s_mds;
3587 t_cap_id = le64_to_cpu(ph->cap_id);
3588 t_seq = le32_to_cpu(ph->seq);
3589 t_mseq = le32_to_cpu(ph->mseq);
3590 target = le32_to_cpu(ph->mds);
3592 t_cap_id = t_seq = t_mseq = 0;
3596 dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n",
3597 inode, ci, mds, mseq, target);
3599 spin_lock(&ci->i_ceph_lock);
3600 cap = __get_cap_for_mds(ci, mds);
3601 if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
3605 if (cap->mds_wanted | cap->issued)
3606 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
3607 __ceph_remove_cap(cap, false);
3612 * now we know we haven't received the cap import message yet
3613 * because the exported cap still exist.
3616 issued = cap->issued;
3617 if (issued != cap->implemented)
3618 pr_err_ratelimited("handle_cap_export: issued != implemented: "
3619 "ino (%llx.%llx) mds%d seq %d mseq %d "
3620 "issued %s implemented %s\n",
3621 ceph_vinop(inode), mds, cap->seq, cap->mseq,
3622 ceph_cap_string(issued),
3623 ceph_cap_string(cap->implemented));
3626 tcap = __get_cap_for_mds(ci, target);
3628 /* already have caps from the target */
3629 if (tcap->cap_id == t_cap_id &&
3630 ceph_seq_cmp(tcap->seq, t_seq) < 0) {
3631 dout(" updating import cap %p mds%d\n", tcap, target);
3632 tcap->cap_id = t_cap_id;
3633 tcap->seq = t_seq - 1;
3634 tcap->issue_seq = t_seq - 1;
3635 tcap->issued |= issued;
3636 tcap->implemented |= issued;
3637 if (cap == ci->i_auth_cap)
3638 ci->i_auth_cap = tcap;
3640 if (!list_empty(&ci->i_cap_flush_list) &&
3641 ci->i_auth_cap == tcap) {
3642 spin_lock(&mdsc->cap_dirty_lock);
3643 list_move_tail(&ci->i_flushing_item,
3644 &tcap->session->s_cap_flushing);
3645 spin_unlock(&mdsc->cap_dirty_lock);
3648 __ceph_remove_cap(cap, false);
3650 } else if (tsession) {
3651 /* add placeholder for the export tagert */
3652 int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
3654 ceph_add_cap(inode, tsession, t_cap_id, -1, issued, 0,
3655 t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
3657 if (!list_empty(&ci->i_cap_flush_list) &&
3658 ci->i_auth_cap == tcap) {
3659 spin_lock(&mdsc->cap_dirty_lock);
3660 list_move_tail(&ci->i_flushing_item,
3661 &tcap->session->s_cap_flushing);
3662 spin_unlock(&mdsc->cap_dirty_lock);
3665 __ceph_remove_cap(cap, false);
3669 spin_unlock(&ci->i_ceph_lock);
3670 mutex_unlock(&session->s_mutex);
3672 /* open target session */
3673 tsession = ceph_mdsc_open_export_target_session(mdsc, target);
3674 if (!IS_ERR(tsession)) {
3676 mutex_lock(&session->s_mutex);
3677 mutex_lock_nested(&tsession->s_mutex,
3678 SINGLE_DEPTH_NESTING);
3680 mutex_lock(&tsession->s_mutex);
3681 mutex_lock_nested(&session->s_mutex,
3682 SINGLE_DEPTH_NESTING);
3684 new_cap = ceph_get_cap(mdsc, NULL);
3693 spin_unlock(&ci->i_ceph_lock);
3694 mutex_unlock(&session->s_mutex);
3696 mutex_unlock(&tsession->s_mutex);
3697 ceph_put_mds_session(tsession);
3700 ceph_put_cap(mdsc, new_cap);
3704 * Handle cap IMPORT.
3706 * caller holds s_mutex. acquires i_ceph_lock
3708 static void handle_cap_import(struct ceph_mds_client *mdsc,
3709 struct inode *inode, struct ceph_mds_caps *im,
3710 struct ceph_mds_cap_peer *ph,
3711 struct ceph_mds_session *session,
3712 struct ceph_cap **target_cap, int *old_issued)
3713 __acquires(ci->i_ceph_lock)
3715 struct ceph_inode_info *ci = ceph_inode(inode);
3716 struct ceph_cap *cap, *ocap, *new_cap = NULL;
3717 int mds = session->s_mds;
3719 unsigned caps = le32_to_cpu(im->caps);
3720 unsigned wanted = le32_to_cpu(im->wanted);
3721 unsigned seq = le32_to_cpu(im->seq);
3722 unsigned mseq = le32_to_cpu(im->migrate_seq);
3723 u64 realmino = le64_to_cpu(im->realm);
3724 u64 cap_id = le64_to_cpu(im->cap_id);
3729 p_cap_id = le64_to_cpu(ph->cap_id);
3730 peer = le32_to_cpu(ph->mds);
3736 dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n",
3737 inode, ci, mds, mseq, peer);
3740 spin_lock(&ci->i_ceph_lock);
3741 cap = __get_cap_for_mds(ci, mds);
3744 spin_unlock(&ci->i_ceph_lock);
3745 new_cap = ceph_get_cap(mdsc, NULL);
3751 ceph_put_cap(mdsc, new_cap);
3756 __ceph_caps_issued(ci, &issued);
3757 issued |= __ceph_caps_dirty(ci);
3759 ceph_add_cap(inode, session, cap_id, -1, caps, wanted, seq, mseq,
3760 realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
3762 ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
3763 if (ocap && ocap->cap_id == p_cap_id) {
3764 dout(" remove export cap %p mds%d flags %d\n",
3765 ocap, peer, ph->flags);
3766 if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
3767 (ocap->seq != le32_to_cpu(ph->seq) ||
3768 ocap->mseq != le32_to_cpu(ph->mseq))) {
3769 pr_err_ratelimited("handle_cap_import: "
3770 "mismatched seq/mseq: ino (%llx.%llx) "
3771 "mds%d seq %d mseq %d importer mds%d "
3772 "has peer seq %d mseq %d\n",
3773 ceph_vinop(inode), peer, ocap->seq,
3774 ocap->mseq, mds, le32_to_cpu(ph->seq),
3775 le32_to_cpu(ph->mseq));
3777 __ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
3780 /* make sure we re-request max_size, if necessary */
3781 ci->i_requested_max_size = 0;
3783 *old_issued = issued;
3788 * Handle a caps message from the MDS.
3790 * Identify the appropriate session, inode, and call the right handler
3791 * based on the cap op.
3793 void ceph_handle_caps(struct ceph_mds_session *session,
3794 struct ceph_msg *msg)
3796 struct ceph_mds_client *mdsc = session->s_mdsc;
3797 struct inode *inode;
3798 struct ceph_inode_info *ci;
3799 struct ceph_cap *cap;
3800 struct ceph_mds_caps *h;
3801 struct ceph_mds_cap_peer *peer = NULL;
3802 struct ceph_snap_realm *realm = NULL;
3804 int msg_version = le16_to_cpu(msg->hdr.version);
3806 struct ceph_vino vino;
3808 size_t snaptrace_len;
3810 struct cap_extra_info extra_info = {};
3812 dout("handle_caps from mds%d\n", session->s_mds);
3815 end = msg->front.iov_base + msg->front.iov_len;
3816 if (msg->front.iov_len < sizeof(*h))
3818 h = msg->front.iov_base;
3819 op = le32_to_cpu(h->op);
3820 vino.ino = le64_to_cpu(h->ino);
3821 vino.snap = CEPH_NOSNAP;
3822 seq = le32_to_cpu(h->seq);
3823 mseq = le32_to_cpu(h->migrate_seq);
3826 snaptrace_len = le32_to_cpu(h->snap_trace_len);
3827 p = snaptrace + snaptrace_len;
3829 if (msg_version >= 2) {
3831 ceph_decode_32_safe(&p, end, flock_len, bad);
3832 if (p + flock_len > end)
3837 if (msg_version >= 3) {
3838 if (op == CEPH_CAP_OP_IMPORT) {
3839 if (p + sizeof(*peer) > end)
3843 } else if (op == CEPH_CAP_OP_EXPORT) {
3844 /* recorded in unused fields */
3845 peer = (void *)&h->size;
3849 if (msg_version >= 4) {
3850 ceph_decode_64_safe(&p, end, extra_info.inline_version, bad);
3851 ceph_decode_32_safe(&p, end, extra_info.inline_len, bad);
3852 if (p + extra_info.inline_len > end)
3854 extra_info.inline_data = p;
3855 p += extra_info.inline_len;
3858 if (msg_version >= 5) {
3859 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
3862 ceph_decode_32_safe(&p, end, epoch_barrier, bad);
3863 ceph_osdc_update_epoch_barrier(osdc, epoch_barrier);
3866 if (msg_version >= 8) {
3868 u32 caller_uid, caller_gid;
3872 ceph_decode_64_safe(&p, end, flush_tid, bad);
3874 ceph_decode_32_safe(&p, end, caller_uid, bad);
3875 ceph_decode_32_safe(&p, end, caller_gid, bad);
3877 ceph_decode_32_safe(&p, end, pool_ns_len, bad);
3878 if (pool_ns_len > 0) {
3879 ceph_decode_need(&p, end, pool_ns_len, bad);
3880 extra_info.pool_ns =
3881 ceph_find_or_create_string(p, pool_ns_len);
3886 if (msg_version >= 9) {
3887 struct ceph_timespec *btime;
3889 if (p + sizeof(*btime) > end)
3892 ceph_decode_timespec64(&extra_info.btime, btime);
3893 p += sizeof(*btime);
3894 ceph_decode_64_safe(&p, end, extra_info.change_attr, bad);
3897 if (msg_version >= 11) {
3900 ceph_decode_32_safe(&p, end, flags, bad);
3902 extra_info.dirstat_valid = true;
3903 ceph_decode_64_safe(&p, end, extra_info.nfiles, bad);
3904 ceph_decode_64_safe(&p, end, extra_info.nsubdirs, bad);
3908 inode = ceph_find_inode(mdsc->fsc->sb, vino);
3909 ci = ceph_inode(inode);
3910 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
3913 mutex_lock(&session->s_mutex);
3915 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
3919 dout(" i don't have ino %llx\n", vino.ino);
3921 if (op == CEPH_CAP_OP_IMPORT) {
3922 cap = ceph_get_cap(mdsc, NULL);
3923 cap->cap_ino = vino.ino;
3924 cap->queue_release = 1;
3925 cap->cap_id = le64_to_cpu(h->cap_id);
3928 cap->issue_seq = seq;
3929 spin_lock(&session->s_cap_lock);
3930 __ceph_queue_cap_release(session, cap);
3931 spin_unlock(&session->s_cap_lock);
3936 /* these will work even if we don't have a cap yet */
3938 case CEPH_CAP_OP_FLUSHSNAP_ACK:
3939 handle_cap_flushsnap_ack(inode, le64_to_cpu(msg->hdr.tid),
3943 case CEPH_CAP_OP_EXPORT:
3944 handle_cap_export(inode, h, peer, session);
3947 case CEPH_CAP_OP_IMPORT:
3949 if (snaptrace_len) {
3950 down_write(&mdsc->snap_rwsem);
3951 ceph_update_snap_trace(mdsc, snaptrace,
3952 snaptrace + snaptrace_len,
3954 downgrade_write(&mdsc->snap_rwsem);
3956 down_read(&mdsc->snap_rwsem);
3958 handle_cap_import(mdsc, inode, h, peer, session,
3959 &cap, &extra_info.issued);
3960 handle_cap_grant(inode, session, cap,
3961 h, msg->middle, &extra_info);
3963 ceph_put_snap_realm(mdsc, realm);
3967 /* the rest require a cap */
3968 spin_lock(&ci->i_ceph_lock);
3969 cap = __get_cap_for_mds(ceph_inode(inode), session->s_mds);
3971 dout(" no cap on %p ino %llx.%llx from mds%d\n",
3972 inode, ceph_ino(inode), ceph_snap(inode),
3974 spin_unlock(&ci->i_ceph_lock);
3975 goto flush_cap_releases;
3978 /* note that each of these drops i_ceph_lock for us */
3980 case CEPH_CAP_OP_REVOKE:
3981 case CEPH_CAP_OP_GRANT:
3982 __ceph_caps_issued(ci, &extra_info.issued);
3983 extra_info.issued |= __ceph_caps_dirty(ci);
3984 handle_cap_grant(inode, session, cap,
3985 h, msg->middle, &extra_info);
3988 case CEPH_CAP_OP_FLUSH_ACK:
3989 handle_cap_flush_ack(inode, le64_to_cpu(msg->hdr.tid),
3993 case CEPH_CAP_OP_TRUNC:
3994 handle_cap_trunc(inode, h, session);
3998 spin_unlock(&ci->i_ceph_lock);
3999 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
4000 ceph_cap_op_name(op));
4004 mutex_unlock(&session->s_mutex);
4006 ceph_put_string(extra_info.pool_ns);
4007 /* avoid calling iput_final() in mds dispatch threads */
4008 ceph_async_iput(inode);
4013 * send any cap release message to try to move things
4014 * along for the mds (who clearly thinks we still have this
4017 ceph_flush_cap_releases(mdsc, session);
4021 pr_err("ceph_handle_caps: corrupt message\n");
4027 * Delayed work handler to process end of delayed cap release LRU list.
4029 void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
4031 struct inode *inode;
4032 struct ceph_inode_info *ci;
4033 int flags = CHECK_CAPS_NODELAY;
4035 dout("check_delayed_caps\n");
4037 spin_lock(&mdsc->cap_delay_lock);
4038 if (list_empty(&mdsc->cap_delay_list))
4040 ci = list_first_entry(&mdsc->cap_delay_list,
4041 struct ceph_inode_info,
4043 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
4044 time_before(jiffies, ci->i_hold_caps_max))
4046 list_del_init(&ci->i_cap_delay_list);
4048 inode = igrab(&ci->vfs_inode);
4049 spin_unlock(&mdsc->cap_delay_lock);
4052 dout("check_delayed_caps on %p\n", inode);
4053 ceph_check_caps(ci, flags, NULL);
4054 /* avoid calling iput_final() in tick thread */
4055 ceph_async_iput(inode);
4058 spin_unlock(&mdsc->cap_delay_lock);
4062 * Flush all dirty caps to the mds
4064 void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
4066 struct ceph_inode_info *ci;
4067 struct inode *inode;
4069 dout("flush_dirty_caps\n");
4070 spin_lock(&mdsc->cap_dirty_lock);
4071 while (!list_empty(&mdsc->cap_dirty)) {
4072 ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
4074 inode = &ci->vfs_inode;
4076 dout("flush_dirty_caps %p\n", inode);
4077 spin_unlock(&mdsc->cap_dirty_lock);
4078 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
4080 spin_lock(&mdsc->cap_dirty_lock);
4082 spin_unlock(&mdsc->cap_dirty_lock);
4083 dout("flush_dirty_caps done\n");
4086 void __ceph_get_fmode(struct ceph_inode_info *ci, int fmode)
4089 int bits = (fmode << 1) | 1;
4090 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
4091 if (bits & (1 << i))
4092 ci->i_nr_by_mode[i]++;
4097 * Drop open file reference. If we were the last open file,
4098 * we may need to release capabilities to the MDS (or schedule
4099 * their delayed release).
4101 void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
4104 int bits = (fmode << 1) | 1;
4105 spin_lock(&ci->i_ceph_lock);
4106 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
4107 if (bits & (1 << i)) {
4108 BUG_ON(ci->i_nr_by_mode[i] == 0);
4109 if (--ci->i_nr_by_mode[i] == 0)
4113 dout("put_fmode %p fmode %d {%d,%d,%d,%d}\n",
4114 &ci->vfs_inode, fmode,
4115 ci->i_nr_by_mode[0], ci->i_nr_by_mode[1],
4116 ci->i_nr_by_mode[2], ci->i_nr_by_mode[3]);
4117 spin_unlock(&ci->i_ceph_lock);
4119 if (last && ci->i_vino.snap == CEPH_NOSNAP)
4120 ceph_check_caps(ci, 0, NULL);
4124 * For a soon-to-be unlinked file, drop the LINK caps. If it
4125 * looks like the link count will hit 0, drop any other caps (other
4126 * than PIN) we don't specifically want (due to the file still being
4129 int ceph_drop_caps_for_unlink(struct inode *inode)
4131 struct ceph_inode_info *ci = ceph_inode(inode);
4132 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
4134 spin_lock(&ci->i_ceph_lock);
4135 if (inode->i_nlink == 1) {
4136 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
4138 ci->i_ceph_flags |= CEPH_I_NODELAY;
4139 if (__ceph_caps_dirty(ci)) {
4140 struct ceph_mds_client *mdsc =
4141 ceph_inode_to_client(inode)->mdsc;
4142 __cap_delay_requeue_front(mdsc, ci);
4145 spin_unlock(&ci->i_ceph_lock);
4150 * Helpers for embedding cap and dentry lease releases into mds
4153 * @force is used by dentry_release (below) to force inclusion of a
4154 * record for the directory inode, even when there aren't any caps to
4157 int ceph_encode_inode_release(void **p, struct inode *inode,
4158 int mds, int drop, int unless, int force)
4160 struct ceph_inode_info *ci = ceph_inode(inode);
4161 struct ceph_cap *cap;
4162 struct ceph_mds_request_release *rel = *p;
4166 spin_lock(&ci->i_ceph_lock);
4167 used = __ceph_caps_used(ci);
4168 dirty = __ceph_caps_dirty(ci);
4170 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
4171 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
4172 ceph_cap_string(unless));
4174 /* only drop unused, clean caps */
4175 drop &= ~(used | dirty);
4177 cap = __get_cap_for_mds(ci, mds);
4178 if (cap && __cap_is_valid(cap)) {
4179 unless &= cap->issued;
4181 if (unless & CEPH_CAP_AUTH_EXCL)
4182 drop &= ~CEPH_CAP_AUTH_SHARED;
4183 if (unless & CEPH_CAP_LINK_EXCL)
4184 drop &= ~CEPH_CAP_LINK_SHARED;
4185 if (unless & CEPH_CAP_XATTR_EXCL)
4186 drop &= ~CEPH_CAP_XATTR_SHARED;
4187 if (unless & CEPH_CAP_FILE_EXCL)
4188 drop &= ~CEPH_CAP_FILE_SHARED;
4191 if (force || (cap->issued & drop)) {
4192 if (cap->issued & drop) {
4193 int wanted = __ceph_caps_wanted(ci);
4194 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0)
4195 wanted |= cap->mds_wanted;
4196 dout("encode_inode_release %p cap %p "
4197 "%s -> %s, wanted %s -> %s\n", inode, cap,
4198 ceph_cap_string(cap->issued),
4199 ceph_cap_string(cap->issued & ~drop),
4200 ceph_cap_string(cap->mds_wanted),
4201 ceph_cap_string(wanted));
4203 cap->issued &= ~drop;
4204 cap->implemented &= ~drop;
4205 cap->mds_wanted = wanted;
4207 dout("encode_inode_release %p cap %p %s"
4208 " (force)\n", inode, cap,
4209 ceph_cap_string(cap->issued));
4212 rel->ino = cpu_to_le64(ceph_ino(inode));
4213 rel->cap_id = cpu_to_le64(cap->cap_id);
4214 rel->seq = cpu_to_le32(cap->seq);
4215 rel->issue_seq = cpu_to_le32(cap->issue_seq);
4216 rel->mseq = cpu_to_le32(cap->mseq);
4217 rel->caps = cpu_to_le32(cap->implemented);
4218 rel->wanted = cpu_to_le32(cap->mds_wanted);
4224 dout("encode_inode_release %p cap %p %s (noop)\n",
4225 inode, cap, ceph_cap_string(cap->issued));
4228 spin_unlock(&ci->i_ceph_lock);
4232 int ceph_encode_dentry_release(void **p, struct dentry *dentry,
4234 int mds, int drop, int unless)
4236 struct dentry *parent = NULL;
4237 struct ceph_mds_request_release *rel = *p;
4238 struct ceph_dentry_info *di = ceph_dentry(dentry);
4243 * force an record for the directory caps if we have a dentry lease.
4244 * this is racy (can't take i_ceph_lock and d_lock together), but it
4245 * doesn't have to be perfect; the mds will revoke anything we don't
4248 spin_lock(&dentry->d_lock);
4249 if (di->lease_session && di->lease_session->s_mds == mds)
4252 parent = dget(dentry->d_parent);
4253 dir = d_inode(parent);
4255 spin_unlock(&dentry->d_lock);
4257 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
4260 spin_lock(&dentry->d_lock);
4261 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
4262 dout("encode_dentry_release %p mds%d seq %d\n",
4263 dentry, mds, (int)di->lease_seq);
4264 rel->dname_len = cpu_to_le32(dentry->d_name.len);
4265 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
4266 *p += dentry->d_name.len;
4267 rel->dname_seq = cpu_to_le32(di->lease_seq);
4268 __ceph_mdsc_drop_dentry_lease(dentry);
4270 spin_unlock(&dentry->d_lock);