1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
5 #include <linux/kernel.h>
6 #include <linux/sched/signal.h>
7 #include <linux/slab.h>
8 #include <linux/vmalloc.h>
9 #include <linux/wait.h>
10 #include <linux/writeback.h>
13 #include "mds_client.h"
15 #include <linux/ceph/decode.h>
16 #include <linux/ceph/messenger.h>
19 * Capability management
21 * The Ceph metadata servers control client access to inode metadata
22 * and file data by issuing capabilities, granting clients permission
23 * to read and/or write both inode field and file data to OSDs
24 * (storage nodes). Each capability consists of a set of bits
25 * indicating which operations are allowed.
27 * If the client holds a *_SHARED cap, the client has a coherent value
28 * that can be safely read from the cached inode.
30 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
31 * client is allowed to change inode attributes (e.g., file size,
32 * mtime), note its dirty state in the ceph_cap, and asynchronously
33 * flush that metadata change to the MDS.
35 * In the event of a conflicting operation (perhaps by another
36 * client), the MDS will revoke the conflicting client capabilities.
38 * In order for a client to cache an inode, it must hold a capability
39 * with at least one MDS server. When inodes are released, release
40 * notifications are batched and periodically sent en masse to the MDS
41 * cluster to release server state.
44 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc);
45 static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
46 struct ceph_mds_session *session,
47 struct ceph_inode_info *ci,
48 u64 oldest_flush_tid);
51 * Generate readable cap strings for debugging output.
53 #define MAX_CAP_STR 20
54 static char cap_str[MAX_CAP_STR][40];
55 static DEFINE_SPINLOCK(cap_str_lock);
56 static int last_cap_str;
58 static char *gcap_string(char *s, int c)
60 if (c & CEPH_CAP_GSHARED)
62 if (c & CEPH_CAP_GEXCL)
64 if (c & CEPH_CAP_GCACHE)
70 if (c & CEPH_CAP_GBUFFER)
72 if (c & CEPH_CAP_GWREXTEND)
74 if (c & CEPH_CAP_GLAZYIO)
79 const char *ceph_cap_string(int caps)
85 spin_lock(&cap_str_lock);
87 if (last_cap_str == MAX_CAP_STR)
89 spin_unlock(&cap_str_lock);
93 if (caps & CEPH_CAP_PIN)
96 c = (caps >> CEPH_CAP_SAUTH) & 3;
99 s = gcap_string(s, c);
102 c = (caps >> CEPH_CAP_SLINK) & 3;
105 s = gcap_string(s, c);
108 c = (caps >> CEPH_CAP_SXATTR) & 3;
111 s = gcap_string(s, c);
114 c = caps >> CEPH_CAP_SFILE;
117 s = gcap_string(s, c);
126 void ceph_caps_init(struct ceph_mds_client *mdsc)
128 INIT_LIST_HEAD(&mdsc->caps_list);
129 spin_lock_init(&mdsc->caps_list_lock);
132 void ceph_caps_finalize(struct ceph_mds_client *mdsc)
134 struct ceph_cap *cap;
136 spin_lock(&mdsc->caps_list_lock);
137 while (!list_empty(&mdsc->caps_list)) {
138 cap = list_first_entry(&mdsc->caps_list,
139 struct ceph_cap, caps_item);
140 list_del(&cap->caps_item);
141 kmem_cache_free(ceph_cap_cachep, cap);
143 mdsc->caps_total_count = 0;
144 mdsc->caps_avail_count = 0;
145 mdsc->caps_use_count = 0;
146 mdsc->caps_reserve_count = 0;
147 mdsc->caps_min_count = 0;
148 spin_unlock(&mdsc->caps_list_lock);
151 void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta)
153 spin_lock(&mdsc->caps_list_lock);
154 mdsc->caps_min_count += delta;
155 BUG_ON(mdsc->caps_min_count < 0);
156 spin_unlock(&mdsc->caps_list_lock);
159 static void __ceph_unreserve_caps(struct ceph_mds_client *mdsc, int nr_caps)
161 struct ceph_cap *cap;
165 BUG_ON(mdsc->caps_reserve_count < nr_caps);
166 mdsc->caps_reserve_count -= nr_caps;
167 if (mdsc->caps_avail_count >=
168 mdsc->caps_reserve_count + mdsc->caps_min_count) {
169 mdsc->caps_total_count -= nr_caps;
170 for (i = 0; i < nr_caps; i++) {
171 cap = list_first_entry(&mdsc->caps_list,
172 struct ceph_cap, caps_item);
173 list_del(&cap->caps_item);
174 kmem_cache_free(ceph_cap_cachep, cap);
177 mdsc->caps_avail_count += nr_caps;
180 dout("%s: caps %d = %d used + %d resv + %d avail\n",
182 mdsc->caps_total_count, mdsc->caps_use_count,
183 mdsc->caps_reserve_count, mdsc->caps_avail_count);
184 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
185 mdsc->caps_reserve_count +
186 mdsc->caps_avail_count);
191 * Called under mdsc->mutex.
193 int ceph_reserve_caps(struct ceph_mds_client *mdsc,
194 struct ceph_cap_reservation *ctx, int need)
197 struct ceph_cap *cap;
202 bool trimmed = false;
203 struct ceph_mds_session *s;
206 dout("reserve caps ctx=%p need=%d\n", ctx, need);
208 /* first reserve any caps that are already allocated */
209 spin_lock(&mdsc->caps_list_lock);
210 if (mdsc->caps_avail_count >= need)
213 have = mdsc->caps_avail_count;
214 mdsc->caps_avail_count -= have;
215 mdsc->caps_reserve_count += have;
216 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
217 mdsc->caps_reserve_count +
218 mdsc->caps_avail_count);
219 spin_unlock(&mdsc->caps_list_lock);
221 for (i = have; i < need; ) {
222 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
224 list_add(&cap->caps_item, &newcaps);
231 for (j = 0; j < mdsc->max_sessions; j++) {
232 s = __ceph_lookup_mds_session(mdsc, j);
235 mutex_unlock(&mdsc->mutex);
237 mutex_lock(&s->s_mutex);
238 max_caps = s->s_nr_caps - (need - i);
239 ceph_trim_caps(mdsc, s, max_caps);
240 mutex_unlock(&s->s_mutex);
242 ceph_put_mds_session(s);
243 mutex_lock(&mdsc->mutex);
247 spin_lock(&mdsc->caps_list_lock);
248 if (mdsc->caps_avail_count) {
250 if (mdsc->caps_avail_count >= need - i)
251 more_have = need - i;
253 more_have = mdsc->caps_avail_count;
257 mdsc->caps_avail_count -= more_have;
258 mdsc->caps_reserve_count += more_have;
261 spin_unlock(&mdsc->caps_list_lock);
266 pr_warn("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
267 ctx, need, have + alloc);
273 BUG_ON(have + alloc != need);
277 spin_lock(&mdsc->caps_list_lock);
278 mdsc->caps_total_count += alloc;
279 mdsc->caps_reserve_count += alloc;
280 list_splice(&newcaps, &mdsc->caps_list);
282 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
283 mdsc->caps_reserve_count +
284 mdsc->caps_avail_count);
287 __ceph_unreserve_caps(mdsc, have + alloc);
289 spin_unlock(&mdsc->caps_list_lock);
291 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
292 ctx, mdsc->caps_total_count, mdsc->caps_use_count,
293 mdsc->caps_reserve_count, mdsc->caps_avail_count);
297 void ceph_unreserve_caps(struct ceph_mds_client *mdsc,
298 struct ceph_cap_reservation *ctx)
300 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
301 spin_lock(&mdsc->caps_list_lock);
302 __ceph_unreserve_caps(mdsc, ctx->count);
304 spin_unlock(&mdsc->caps_list_lock);
307 struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc,
308 struct ceph_cap_reservation *ctx)
310 struct ceph_cap *cap = NULL;
312 /* temporary, until we do something about cap import/export */
314 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
316 spin_lock(&mdsc->caps_list_lock);
317 mdsc->caps_use_count++;
318 mdsc->caps_total_count++;
319 spin_unlock(&mdsc->caps_list_lock);
321 spin_lock(&mdsc->caps_list_lock);
322 if (mdsc->caps_avail_count) {
323 BUG_ON(list_empty(&mdsc->caps_list));
325 mdsc->caps_avail_count--;
326 mdsc->caps_use_count++;
327 cap = list_first_entry(&mdsc->caps_list,
328 struct ceph_cap, caps_item);
329 list_del(&cap->caps_item);
331 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
332 mdsc->caps_reserve_count + mdsc->caps_avail_count);
334 spin_unlock(&mdsc->caps_list_lock);
340 spin_lock(&mdsc->caps_list_lock);
341 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
342 ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
343 mdsc->caps_reserve_count, mdsc->caps_avail_count);
345 BUG_ON(ctx->count > mdsc->caps_reserve_count);
346 BUG_ON(list_empty(&mdsc->caps_list));
349 mdsc->caps_reserve_count--;
350 mdsc->caps_use_count++;
352 cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
353 list_del(&cap->caps_item);
355 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
356 mdsc->caps_reserve_count + mdsc->caps_avail_count);
357 spin_unlock(&mdsc->caps_list_lock);
361 void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
363 spin_lock(&mdsc->caps_list_lock);
364 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
365 cap, mdsc->caps_total_count, mdsc->caps_use_count,
366 mdsc->caps_reserve_count, mdsc->caps_avail_count);
367 mdsc->caps_use_count--;
369 * Keep some preallocated caps around (ceph_min_count), to
370 * avoid lots of free/alloc churn.
372 if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
373 mdsc->caps_min_count) {
374 mdsc->caps_total_count--;
375 kmem_cache_free(ceph_cap_cachep, cap);
377 mdsc->caps_avail_count++;
378 list_add(&cap->caps_item, &mdsc->caps_list);
381 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
382 mdsc->caps_reserve_count + mdsc->caps_avail_count);
383 spin_unlock(&mdsc->caps_list_lock);
386 void ceph_reservation_status(struct ceph_fs_client *fsc,
387 int *total, int *avail, int *used, int *reserved,
390 struct ceph_mds_client *mdsc = fsc->mdsc;
392 spin_lock(&mdsc->caps_list_lock);
395 *total = mdsc->caps_total_count;
397 *avail = mdsc->caps_avail_count;
399 *used = mdsc->caps_use_count;
401 *reserved = mdsc->caps_reserve_count;
403 *min = mdsc->caps_min_count;
405 spin_unlock(&mdsc->caps_list_lock);
409 * Find ceph_cap for given mds, if any.
411 * Called with i_ceph_lock held.
413 static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
415 struct ceph_cap *cap;
416 struct rb_node *n = ci->i_caps.rb_node;
419 cap = rb_entry(n, struct ceph_cap, ci_node);
422 else if (mds > cap->mds)
430 struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
432 struct ceph_cap *cap;
434 spin_lock(&ci->i_ceph_lock);
435 cap = __get_cap_for_mds(ci, mds);
436 spin_unlock(&ci->i_ceph_lock);
441 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
443 static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
445 struct ceph_cap *cap;
449 /* prefer mds with WR|BUFFER|EXCL caps */
450 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
451 cap = rb_entry(p, struct ceph_cap, ci_node);
453 if (cap->issued & (CEPH_CAP_FILE_WR |
454 CEPH_CAP_FILE_BUFFER |
461 int ceph_get_cap_mds(struct inode *inode)
463 struct ceph_inode_info *ci = ceph_inode(inode);
465 spin_lock(&ci->i_ceph_lock);
466 mds = __ceph_get_cap_mds(ceph_inode(inode));
467 spin_unlock(&ci->i_ceph_lock);
472 * Called under i_ceph_lock.
474 static void __insert_cap_node(struct ceph_inode_info *ci,
475 struct ceph_cap *new)
477 struct rb_node **p = &ci->i_caps.rb_node;
478 struct rb_node *parent = NULL;
479 struct ceph_cap *cap = NULL;
483 cap = rb_entry(parent, struct ceph_cap, ci_node);
484 if (new->mds < cap->mds)
486 else if (new->mds > cap->mds)
492 rb_link_node(&new->ci_node, parent, p);
493 rb_insert_color(&new->ci_node, &ci->i_caps);
497 * (re)set cap hold timeouts, which control the delayed release
498 * of unused caps back to the MDS. Should be called on cap use.
500 static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
501 struct ceph_inode_info *ci)
503 struct ceph_mount_options *ma = mdsc->fsc->mount_options;
505 ci->i_hold_caps_min = round_jiffies(jiffies +
506 ma->caps_wanted_delay_min * HZ);
507 ci->i_hold_caps_max = round_jiffies(jiffies +
508 ma->caps_wanted_delay_max * HZ);
509 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
510 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
514 * (Re)queue cap at the end of the delayed cap release list.
516 * If I_FLUSH is set, leave the inode at the front of the list.
518 * Caller holds i_ceph_lock
519 * -> we take mdsc->cap_delay_lock
521 static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
522 struct ceph_inode_info *ci,
525 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
526 ci->i_ceph_flags, ci->i_hold_caps_max);
527 if (!mdsc->stopping) {
528 spin_lock(&mdsc->cap_delay_lock);
529 if (!list_empty(&ci->i_cap_delay_list)) {
530 if (ci->i_ceph_flags & CEPH_I_FLUSH)
532 list_del_init(&ci->i_cap_delay_list);
535 __cap_set_timeouts(mdsc, ci);
536 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
538 spin_unlock(&mdsc->cap_delay_lock);
543 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
544 * indicating we should send a cap message to flush dirty metadata
545 * asap, and move to the front of the delayed cap list.
547 static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
548 struct ceph_inode_info *ci)
550 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
551 spin_lock(&mdsc->cap_delay_lock);
552 ci->i_ceph_flags |= CEPH_I_FLUSH;
553 if (!list_empty(&ci->i_cap_delay_list))
554 list_del_init(&ci->i_cap_delay_list);
555 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
556 spin_unlock(&mdsc->cap_delay_lock);
560 * Cancel delayed work on cap.
562 * Caller must hold i_ceph_lock.
564 static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
565 struct ceph_inode_info *ci)
567 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
568 if (list_empty(&ci->i_cap_delay_list))
570 spin_lock(&mdsc->cap_delay_lock);
571 list_del_init(&ci->i_cap_delay_list);
572 spin_unlock(&mdsc->cap_delay_lock);
576 * Common issue checks for add_cap, handle_cap_grant.
578 static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
581 unsigned had = __ceph_caps_issued(ci, NULL);
584 * Each time we receive FILE_CACHE anew, we increment
587 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
588 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) {
593 * If FILE_SHARED is newly issued, mark dir not complete. We don't
594 * know what happened to this directory while we didn't have the cap.
595 * If FILE_SHARED is being revoked, also mark dir not complete. It
596 * stops on-going cached readdir.
598 if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) {
599 if (issued & CEPH_CAP_FILE_SHARED)
600 atomic_inc(&ci->i_shared_gen);
601 if (S_ISDIR(ci->vfs_inode.i_mode)) {
602 dout(" marking %p NOT complete\n", &ci->vfs_inode);
603 __ceph_dir_clear_complete(ci);
609 * Add a capability under the given MDS session.
611 * Caller should hold session snap_rwsem (read) and s_mutex.
613 * @fmode is the open file mode, if we are opening a file, otherwise
614 * it is < 0. (This is so we can atomically add the cap and add an
615 * open file reference to it.)
617 void ceph_add_cap(struct inode *inode,
618 struct ceph_mds_session *session, u64 cap_id,
619 int fmode, unsigned issued, unsigned wanted,
620 unsigned seq, unsigned mseq, u64 realmino, int flags,
621 struct ceph_cap **new_cap)
623 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
624 struct ceph_inode_info *ci = ceph_inode(inode);
625 struct ceph_cap *cap;
626 int mds = session->s_mds;
629 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
630 session->s_mds, cap_id, ceph_cap_string(issued), seq);
633 * If we are opening the file, include file mode wanted bits
637 wanted |= ceph_caps_for_mode(fmode);
639 cap = __get_cap_for_mds(ci, mds);
645 cap->implemented = 0;
651 __insert_cap_node(ci, cap);
653 /* add to session cap list */
654 cap->session = session;
655 spin_lock(&session->s_cap_lock);
656 list_add_tail(&cap->session_caps, &session->s_caps);
657 session->s_nr_caps++;
658 spin_unlock(&session->s_cap_lock);
660 if (cap->cap_gen < session->s_cap_gen)
661 cap->issued = cap->implemented = CEPH_CAP_PIN;
664 * auth mds of the inode changed. we received the cap export
665 * message, but still haven't received the cap import message.
666 * handle_cap_export() updated the new auth MDS' cap.
668 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
669 * a message that was send before the cap import message. So
672 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
673 WARN_ON(cap != ci->i_auth_cap);
674 WARN_ON(cap->cap_id != cap_id);
677 issued |= cap->issued;
678 flags |= CEPH_CAP_FLAG_AUTH;
682 if (!ci->i_snap_realm ||
683 ((flags & CEPH_CAP_FLAG_AUTH) &&
684 realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) {
686 * add this inode to the appropriate snap realm
688 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
691 struct ceph_snap_realm *oldrealm = ci->i_snap_realm;
693 spin_lock(&oldrealm->inodes_with_caps_lock);
694 list_del_init(&ci->i_snap_realm_item);
695 spin_unlock(&oldrealm->inodes_with_caps_lock);
698 spin_lock(&realm->inodes_with_caps_lock);
699 list_add(&ci->i_snap_realm_item,
700 &realm->inodes_with_caps);
701 ci->i_snap_realm = realm;
702 if (realm->ino == ci->i_vino.ino)
703 realm->inode = inode;
704 spin_unlock(&realm->inodes_with_caps_lock);
707 ceph_put_snap_realm(mdsc, oldrealm);
709 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
715 __check_cap_issue(ci, cap, issued);
718 * If we are issued caps we don't want, or the mds' wanted
719 * value appears to be off, queue a check so we'll release
720 * later and/or update the mds wanted value.
722 actual_wanted = __ceph_caps_wanted(ci);
723 if ((wanted & ~actual_wanted) ||
724 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
725 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
726 ceph_cap_string(issued), ceph_cap_string(wanted),
727 ceph_cap_string(actual_wanted));
728 __cap_delay_requeue(mdsc, ci, true);
731 if (flags & CEPH_CAP_FLAG_AUTH) {
732 if (!ci->i_auth_cap ||
733 ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
734 ci->i_auth_cap = cap;
735 cap->mds_wanted = wanted;
738 WARN_ON(ci->i_auth_cap == cap);
741 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
742 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
743 ceph_cap_string(issued|cap->issued), seq, mds);
744 cap->cap_id = cap_id;
745 cap->issued = issued;
746 cap->implemented |= issued;
747 if (ceph_seq_cmp(mseq, cap->mseq) > 0)
748 cap->mds_wanted = wanted;
750 cap->mds_wanted |= wanted;
752 cap->issue_seq = seq;
754 cap->cap_gen = session->s_cap_gen;
757 __ceph_get_fmode(ci, fmode);
761 * Return true if cap has not timed out and belongs to the current
762 * generation of the MDS session (i.e. has not gone 'stale' due to
763 * us losing touch with the mds).
765 static int __cap_is_valid(struct ceph_cap *cap)
770 spin_lock(&cap->session->s_gen_ttl_lock);
771 gen = cap->session->s_cap_gen;
772 ttl = cap->session->s_cap_ttl;
773 spin_unlock(&cap->session->s_gen_ttl_lock);
775 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
776 dout("__cap_is_valid %p cap %p issued %s "
777 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
778 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
786 * Return set of valid cap bits issued to us. Note that caps time
787 * out, and may be invalidated in bulk if the client session times out
788 * and session->s_cap_gen is bumped.
790 int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
792 int have = ci->i_snap_caps;
793 struct ceph_cap *cap;
798 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
799 cap = rb_entry(p, struct ceph_cap, ci_node);
800 if (!__cap_is_valid(cap))
802 dout("__ceph_caps_issued %p cap %p issued %s\n",
803 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
806 *implemented |= cap->implemented;
809 * exclude caps issued by non-auth MDS, but are been revoking
810 * by the auth MDS. The non-auth MDS should be revoking/exporting
811 * these caps, but the message is delayed.
813 if (ci->i_auth_cap) {
814 cap = ci->i_auth_cap;
815 have &= ~cap->implemented | cap->issued;
821 * Get cap bits issued by caps other than @ocap
823 int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
825 int have = ci->i_snap_caps;
826 struct ceph_cap *cap;
829 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
830 cap = rb_entry(p, struct ceph_cap, ci_node);
833 if (!__cap_is_valid(cap))
841 * Move a cap to the end of the LRU (oldest caps at list head, newest
844 static void __touch_cap(struct ceph_cap *cap)
846 struct ceph_mds_session *s = cap->session;
848 spin_lock(&s->s_cap_lock);
849 if (!s->s_cap_iterator) {
850 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
852 list_move_tail(&cap->session_caps, &s->s_caps);
854 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
855 &cap->ci->vfs_inode, cap, s->s_mds);
857 spin_unlock(&s->s_cap_lock);
861 * Check if we hold the given mask. If so, move the cap(s) to the
862 * front of their respective LRUs. (This is the preferred way for
863 * callers to check for caps they want.)
865 int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
867 struct ceph_cap *cap;
869 int have = ci->i_snap_caps;
871 if ((have & mask) == mask) {
872 dout("__ceph_caps_issued_mask %p snap issued %s"
873 " (mask %s)\n", &ci->vfs_inode,
874 ceph_cap_string(have),
875 ceph_cap_string(mask));
879 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
880 cap = rb_entry(p, struct ceph_cap, ci_node);
881 if (!__cap_is_valid(cap))
883 if ((cap->issued & mask) == mask) {
884 dout("__ceph_caps_issued_mask %p cap %p issued %s"
885 " (mask %s)\n", &ci->vfs_inode, cap,
886 ceph_cap_string(cap->issued),
887 ceph_cap_string(mask));
893 /* does a combination of caps satisfy mask? */
895 if ((have & mask) == mask) {
896 dout("__ceph_caps_issued_mask %p combo issued %s"
897 " (mask %s)\n", &ci->vfs_inode,
898 ceph_cap_string(cap->issued),
899 ceph_cap_string(mask));
903 /* touch this + preceding caps */
905 for (q = rb_first(&ci->i_caps); q != p;
907 cap = rb_entry(q, struct ceph_cap,
909 if (!__cap_is_valid(cap))
922 * Return true if mask caps are currently being revoked by an MDS.
924 int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
925 struct ceph_cap *ocap, int mask)
927 struct ceph_cap *cap;
930 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
931 cap = rb_entry(p, struct ceph_cap, ci_node);
933 (cap->implemented & ~cap->issued & mask))
939 int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
941 struct inode *inode = &ci->vfs_inode;
944 spin_lock(&ci->i_ceph_lock);
945 ret = __ceph_caps_revoking_other(ci, NULL, mask);
946 spin_unlock(&ci->i_ceph_lock);
947 dout("ceph_caps_revoking %p %s = %d\n", inode,
948 ceph_cap_string(mask), ret);
952 int __ceph_caps_used(struct ceph_inode_info *ci)
956 used |= CEPH_CAP_PIN;
958 used |= CEPH_CAP_FILE_RD;
959 if (ci->i_rdcache_ref ||
960 (!S_ISDIR(ci->vfs_inode.i_mode) && /* ignore readdir cache */
961 ci->vfs_inode.i_data.nrpages))
962 used |= CEPH_CAP_FILE_CACHE;
964 used |= CEPH_CAP_FILE_WR;
965 if (ci->i_wb_ref || ci->i_wrbuffer_ref)
966 used |= CEPH_CAP_FILE_BUFFER;
971 * wanted, by virtue of open file modes
973 int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
976 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
977 if (ci->i_nr_by_mode[i])
982 return ceph_caps_for_mode(bits >> 1);
986 * Return caps we have registered with the MDS(s) as 'wanted'.
988 int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check)
990 struct ceph_cap *cap;
994 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
995 cap = rb_entry(p, struct ceph_cap, ci_node);
996 if (check && !__cap_is_valid(cap))
998 if (cap == ci->i_auth_cap)
999 mds_wanted |= cap->mds_wanted;
1001 mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
1007 * called under i_ceph_lock
1009 static int __ceph_is_single_caps(struct ceph_inode_info *ci)
1011 return rb_first(&ci->i_caps) == rb_last(&ci->i_caps);
1014 static int __ceph_is_any_caps(struct ceph_inode_info *ci)
1016 return !RB_EMPTY_ROOT(&ci->i_caps);
1019 int ceph_is_any_caps(struct inode *inode)
1021 struct ceph_inode_info *ci = ceph_inode(inode);
1024 spin_lock(&ci->i_ceph_lock);
1025 ret = __ceph_is_any_caps(ci);
1026 spin_unlock(&ci->i_ceph_lock);
1031 static void drop_inode_snap_realm(struct ceph_inode_info *ci)
1033 struct ceph_snap_realm *realm = ci->i_snap_realm;
1034 spin_lock(&realm->inodes_with_caps_lock);
1035 list_del_init(&ci->i_snap_realm_item);
1036 ci->i_snap_realm_counter++;
1037 ci->i_snap_realm = NULL;
1038 spin_unlock(&realm->inodes_with_caps_lock);
1039 ceph_put_snap_realm(ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc,
1044 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
1046 * caller should hold i_ceph_lock.
1047 * caller will not hold session s_mutex if called from destroy_inode.
1049 void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
1051 struct ceph_mds_session *session = cap->session;
1052 struct ceph_inode_info *ci = cap->ci;
1053 struct ceph_mds_client *mdsc =
1054 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
1057 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
1059 /* remove from session list */
1060 spin_lock(&session->s_cap_lock);
1061 if (session->s_cap_iterator == cap) {
1062 /* not yet, we are iterating over this very cap */
1063 dout("__ceph_remove_cap delaying %p removal from session %p\n",
1066 list_del_init(&cap->session_caps);
1067 session->s_nr_caps--;
1068 cap->session = NULL;
1071 /* protect backpointer with s_cap_lock: see iterate_session_caps */
1075 * s_cap_reconnect is protected by s_cap_lock. no one changes
1076 * s_cap_gen while session is in the reconnect state.
1078 if (queue_release &&
1079 (!session->s_cap_reconnect || cap->cap_gen == session->s_cap_gen)) {
1080 cap->queue_release = 1;
1082 list_add_tail(&cap->session_caps,
1083 &session->s_cap_releases);
1084 session->s_num_cap_releases++;
1088 cap->queue_release = 0;
1090 cap->cap_ino = ci->i_vino.ino;
1092 spin_unlock(&session->s_cap_lock);
1094 /* remove from inode list */
1095 rb_erase(&cap->ci_node, &ci->i_caps);
1096 if (ci->i_auth_cap == cap)
1097 ci->i_auth_cap = NULL;
1100 ceph_put_cap(mdsc, cap);
1102 /* when reconnect denied, we remove session caps forcibly,
1103 * i_wr_ref can be non-zero. If there are ongoing write,
1104 * keep i_snap_realm.
1106 if (!__ceph_is_any_caps(ci) && ci->i_wr_ref == 0 && ci->i_snap_realm)
1107 drop_inode_snap_realm(ci);
1109 if (!__ceph_is_any_real_caps(ci))
1110 __cap_delay_cancel(mdsc, ci);
1113 struct cap_msg_args {
1114 struct ceph_mds_session *session;
1115 u64 ino, cid, follows;
1116 u64 flush_tid, oldest_flush_tid, size, max_size;
1118 struct ceph_buffer *xattr_buf;
1119 struct timespec64 atime, mtime, ctime;
1120 int op, caps, wanted, dirty;
1121 u32 seq, issue_seq, mseq, time_warp_seq;
1130 * Build and send a cap message to the given MDS.
1132 * Caller should be holding s_mutex.
1134 static int send_cap_msg(struct cap_msg_args *arg)
1136 struct ceph_mds_caps *fc;
1137 struct ceph_msg *msg;
1140 struct timespec64 zerotime = {0};
1141 struct ceph_osd_client *osdc = &arg->session->s_mdsc->fsc->client->osdc;
1143 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
1144 " seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu"
1145 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(arg->op),
1146 arg->cid, arg->ino, ceph_cap_string(arg->caps),
1147 ceph_cap_string(arg->wanted), ceph_cap_string(arg->dirty),
1148 arg->seq, arg->issue_seq, arg->flush_tid, arg->oldest_flush_tid,
1149 arg->mseq, arg->follows, arg->size, arg->max_size,
1151 arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0);
1153 /* flock buffer size + inline version + inline data size +
1154 * osd_epoch_barrier + oldest_flush_tid */
1155 extra_len = 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4;
1156 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc) + extra_len,
1161 msg->hdr.version = cpu_to_le16(10);
1162 msg->hdr.tid = cpu_to_le64(arg->flush_tid);
1164 fc = msg->front.iov_base;
1165 memset(fc, 0, sizeof(*fc));
1167 fc->cap_id = cpu_to_le64(arg->cid);
1168 fc->op = cpu_to_le32(arg->op);
1169 fc->seq = cpu_to_le32(arg->seq);
1170 fc->issue_seq = cpu_to_le32(arg->issue_seq);
1171 fc->migrate_seq = cpu_to_le32(arg->mseq);
1172 fc->caps = cpu_to_le32(arg->caps);
1173 fc->wanted = cpu_to_le32(arg->wanted);
1174 fc->dirty = cpu_to_le32(arg->dirty);
1175 fc->ino = cpu_to_le64(arg->ino);
1176 fc->snap_follows = cpu_to_le64(arg->follows);
1178 fc->size = cpu_to_le64(arg->size);
1179 fc->max_size = cpu_to_le64(arg->max_size);
1180 ceph_encode_timespec64(&fc->mtime, &arg->mtime);
1181 ceph_encode_timespec64(&fc->atime, &arg->atime);
1182 ceph_encode_timespec64(&fc->ctime, &arg->ctime);
1183 fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq);
1185 fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid));
1186 fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid));
1187 fc->mode = cpu_to_le32(arg->mode);
1189 fc->xattr_version = cpu_to_le64(arg->xattr_version);
1190 if (arg->xattr_buf) {
1191 msg->middle = ceph_buffer_get(arg->xattr_buf);
1192 fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1193 msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1197 /* flock buffer size (version 2) */
1198 ceph_encode_32(&p, 0);
1199 /* inline version (version 4) */
1200 ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE);
1201 /* inline data size */
1202 ceph_encode_32(&p, 0);
1204 * osd_epoch_barrier (version 5)
1205 * The epoch_barrier is protected osdc->lock, so READ_ONCE here in
1206 * case it was recently changed
1208 ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier));
1209 /* oldest_flush_tid (version 6) */
1210 ceph_encode_64(&p, arg->oldest_flush_tid);
1213 * caller_uid/caller_gid (version 7)
1215 * Currently, we don't properly track which caller dirtied the caps
1216 * last, and force a flush of them when there is a conflict. For now,
1217 * just set this to 0:0, to emulate how the MDS has worked up to now.
1219 ceph_encode_32(&p, 0);
1220 ceph_encode_32(&p, 0);
1222 /* pool namespace (version 8) (mds always ignores this) */
1223 ceph_encode_32(&p, 0);
1226 * btime and change_attr (version 9)
1228 * We just zero these out for now, as the MDS ignores them unless
1229 * the requisite feature flags are set (which we don't do yet).
1231 ceph_encode_timespec64(p, &zerotime);
1232 p += sizeof(struct ceph_timespec);
1233 ceph_encode_64(&p, 0);
1235 /* Advisory flags (version 10) */
1236 ceph_encode_32(&p, arg->flags);
1238 ceph_con_send(&arg->session->s_con, msg);
1243 * Queue cap releases when an inode is dropped from our cache. Since
1244 * inode is about to be destroyed, there is no need for i_ceph_lock.
1246 void ceph_queue_caps_release(struct inode *inode)
1248 struct ceph_inode_info *ci = ceph_inode(inode);
1251 p = rb_first(&ci->i_caps);
1253 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1255 __ceph_remove_cap(cap, true);
1260 * Send a cap msg on the given inode. Update our caps state, then
1261 * drop i_ceph_lock and send the message.
1263 * Make note of max_size reported/requested from mds, revoked caps
1264 * that have now been implemented.
1266 * Make half-hearted attempt ot to invalidate page cache if we are
1267 * dropping RDCACHE. Note that this will leave behind locked pages
1268 * that we'll then need to deal with elsewhere.
1270 * Return non-zero if delayed release, or we experienced an error
1271 * such that the caller should requeue + retry later.
1273 * called with i_ceph_lock, then drops it.
1274 * caller should hold snap_rwsem (read), s_mutex.
1276 static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1277 int op, bool sync, int used, int want, int retain,
1278 int flushing, u64 flush_tid, u64 oldest_flush_tid)
1279 __releases(cap->ci->i_ceph_lock)
1281 struct ceph_inode_info *ci = cap->ci;
1282 struct inode *inode = &ci->vfs_inode;
1283 struct cap_msg_args arg;
1289 held = cap->issued | cap->implemented;
1290 revoking = cap->implemented & ~cap->issued;
1291 retain &= ~revoking;
1293 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1294 inode, cap, cap->session,
1295 ceph_cap_string(held), ceph_cap_string(held & retain),
1296 ceph_cap_string(revoking));
1297 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1299 arg.session = cap->session;
1301 /* don't release wanted unless we've waited a bit. */
1302 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1303 time_before(jiffies, ci->i_hold_caps_min)) {
1304 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1305 ceph_cap_string(cap->issued),
1306 ceph_cap_string(cap->issued & retain),
1307 ceph_cap_string(cap->mds_wanted),
1308 ceph_cap_string(want));
1309 want |= cap->mds_wanted;
1310 retain |= cap->issued;
1313 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1314 if (want & ~cap->mds_wanted) {
1315 /* user space may open/close single file frequently.
1316 * This avoids droping mds_wanted immediately after
1317 * requesting new mds_wanted.
1319 __cap_set_timeouts(mdsc, ci);
1322 cap->issued &= retain; /* drop bits we don't want */
1323 if (cap->implemented & ~cap->issued) {
1325 * Wake up any waiters on wanted -> needed transition.
1326 * This is due to the weird transition from buffered
1327 * to sync IO... we need to flush dirty pages _before_
1328 * allowing sync writes to avoid reordering.
1332 cap->implemented &= cap->issued | used;
1333 cap->mds_wanted = want;
1335 arg.ino = ceph_vino(inode).ino;
1336 arg.cid = cap->cap_id;
1337 arg.follows = flushing ? ci->i_head_snapc->seq : 0;
1338 arg.flush_tid = flush_tid;
1339 arg.oldest_flush_tid = oldest_flush_tid;
1341 arg.size = inode->i_size;
1342 ci->i_reported_size = arg.size;
1343 arg.max_size = ci->i_wanted_max_size;
1344 ci->i_requested_max_size = arg.max_size;
1346 if (flushing & CEPH_CAP_XATTR_EXCL) {
1347 __ceph_build_xattrs_blob(ci);
1348 arg.xattr_version = ci->i_xattrs.version;
1349 arg.xattr_buf = ci->i_xattrs.blob;
1351 arg.xattr_buf = NULL;
1354 arg.mtime = inode->i_mtime;
1355 arg.atime = inode->i_atime;
1356 arg.ctime = inode->i_ctime;
1359 arg.caps = cap->implemented;
1361 arg.dirty = flushing;
1364 arg.issue_seq = cap->issue_seq;
1365 arg.mseq = cap->mseq;
1366 arg.time_warp_seq = ci->i_time_warp_seq;
1368 arg.uid = inode->i_uid;
1369 arg.gid = inode->i_gid;
1370 arg.mode = inode->i_mode;
1372 arg.inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
1373 if (list_empty(&ci->i_cap_snaps))
1374 arg.flags = CEPH_CLIENT_CAPS_NO_CAPSNAP;
1376 arg.flags = CEPH_CLIENT_CAPS_PENDING_CAPSNAP;
1378 arg.flags |= CEPH_CLIENT_CAPS_SYNC;
1380 spin_unlock(&ci->i_ceph_lock);
1382 ret = send_cap_msg(&arg);
1384 dout("error sending cap msg, must requeue %p\n", inode);
1389 wake_up_all(&ci->i_cap_wq);
1394 static inline int __send_flush_snap(struct inode *inode,
1395 struct ceph_mds_session *session,
1396 struct ceph_cap_snap *capsnap,
1397 u32 mseq, u64 oldest_flush_tid)
1399 struct cap_msg_args arg;
1401 arg.session = session;
1402 arg.ino = ceph_vino(inode).ino;
1404 arg.follows = capsnap->follows;
1405 arg.flush_tid = capsnap->cap_flush.tid;
1406 arg.oldest_flush_tid = oldest_flush_tid;
1408 arg.size = capsnap->size;
1410 arg.xattr_version = capsnap->xattr_version;
1411 arg.xattr_buf = capsnap->xattr_blob;
1413 arg.atime = capsnap->atime;
1414 arg.mtime = capsnap->mtime;
1415 arg.ctime = capsnap->ctime;
1417 arg.op = CEPH_CAP_OP_FLUSHSNAP;
1418 arg.caps = capsnap->issued;
1420 arg.dirty = capsnap->dirty;
1425 arg.time_warp_seq = capsnap->time_warp_seq;
1427 arg.uid = capsnap->uid;
1428 arg.gid = capsnap->gid;
1429 arg.mode = capsnap->mode;
1431 arg.inline_data = capsnap->inline_data;
1434 return send_cap_msg(&arg);
1438 * When a snapshot is taken, clients accumulate dirty metadata on
1439 * inodes with capabilities in ceph_cap_snaps to describe the file
1440 * state at the time the snapshot was taken. This must be flushed
1441 * asynchronously back to the MDS once sync writes complete and dirty
1442 * data is written out.
1444 * Called under i_ceph_lock. Takes s_mutex as needed.
1446 static void __ceph_flush_snaps(struct ceph_inode_info *ci,
1447 struct ceph_mds_session *session)
1448 __releases(ci->i_ceph_lock)
1449 __acquires(ci->i_ceph_lock)
1451 struct inode *inode = &ci->vfs_inode;
1452 struct ceph_mds_client *mdsc = session->s_mdsc;
1453 struct ceph_cap_snap *capsnap;
1454 u64 oldest_flush_tid = 0;
1455 u64 first_tid = 1, last_tid = 0;
1457 dout("__flush_snaps %p session %p\n", inode, session);
1459 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1461 * we need to wait for sync writes to complete and for dirty
1462 * pages to be written out.
1464 if (capsnap->dirty_pages || capsnap->writing)
1467 /* should be removed by ceph_try_drop_cap_snap() */
1468 BUG_ON(!capsnap->need_flush);
1470 /* only flush each capsnap once */
1471 if (capsnap->cap_flush.tid > 0) {
1472 dout(" already flushed %p, skipping\n", capsnap);
1476 spin_lock(&mdsc->cap_dirty_lock);
1477 capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
1478 list_add_tail(&capsnap->cap_flush.g_list,
1479 &mdsc->cap_flush_list);
1480 if (oldest_flush_tid == 0)
1481 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1482 if (list_empty(&ci->i_flushing_item)) {
1483 list_add_tail(&ci->i_flushing_item,
1484 &session->s_cap_flushing);
1486 spin_unlock(&mdsc->cap_dirty_lock);
1488 list_add_tail(&capsnap->cap_flush.i_list,
1489 &ci->i_cap_flush_list);
1492 first_tid = capsnap->cap_flush.tid;
1493 last_tid = capsnap->cap_flush.tid;
1496 ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS;
1498 while (first_tid <= last_tid) {
1499 struct ceph_cap *cap = ci->i_auth_cap;
1500 struct ceph_cap_flush *cf;
1503 if (!(cap && cap->session == session)) {
1504 dout("__flush_snaps %p auth cap %p not mds%d, "
1505 "stop\n", inode, cap, session->s_mds);
1510 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
1511 if (cf->tid >= first_tid) {
1519 first_tid = cf->tid + 1;
1521 capsnap = container_of(cf, struct ceph_cap_snap, cap_flush);
1522 refcount_inc(&capsnap->nref);
1523 spin_unlock(&ci->i_ceph_lock);
1525 dout("__flush_snaps %p capsnap %p tid %llu %s\n",
1526 inode, capsnap, cf->tid, ceph_cap_string(capsnap->dirty));
1528 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
1531 pr_err("__flush_snaps: error sending cap flushsnap, "
1532 "ino (%llx.%llx) tid %llu follows %llu\n",
1533 ceph_vinop(inode), cf->tid, capsnap->follows);
1536 ceph_put_cap_snap(capsnap);
1537 spin_lock(&ci->i_ceph_lock);
1541 void ceph_flush_snaps(struct ceph_inode_info *ci,
1542 struct ceph_mds_session **psession)
1544 struct inode *inode = &ci->vfs_inode;
1545 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
1546 struct ceph_mds_session *session = NULL;
1549 dout("ceph_flush_snaps %p\n", inode);
1551 session = *psession;
1553 spin_lock(&ci->i_ceph_lock);
1554 if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) {
1555 dout(" no capsnap needs flush, doing nothing\n");
1558 if (!ci->i_auth_cap) {
1559 dout(" no auth cap (migrating?), doing nothing\n");
1563 mds = ci->i_auth_cap->session->s_mds;
1564 if (session && session->s_mds != mds) {
1565 dout(" oops, wrong session %p mutex\n", session);
1566 mutex_unlock(&session->s_mutex);
1567 ceph_put_mds_session(session);
1571 spin_unlock(&ci->i_ceph_lock);
1572 mutex_lock(&mdsc->mutex);
1573 session = __ceph_lookup_mds_session(mdsc, mds);
1574 mutex_unlock(&mdsc->mutex);
1576 dout(" inverting session/ino locks on %p\n", session);
1577 mutex_lock(&session->s_mutex);
1582 // make sure flushsnap messages are sent in proper order.
1583 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
1584 __kick_flushing_caps(mdsc, session, ci, 0);
1585 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
1588 __ceph_flush_snaps(ci, session);
1590 spin_unlock(&ci->i_ceph_lock);
1593 *psession = session;
1594 } else if (session) {
1595 mutex_unlock(&session->s_mutex);
1596 ceph_put_mds_session(session);
1598 /* we flushed them all; remove this inode from the queue */
1599 spin_lock(&mdsc->snap_flush_lock);
1600 list_del_init(&ci->i_snap_flush_item);
1601 spin_unlock(&mdsc->snap_flush_lock);
1605 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1606 * Caller is then responsible for calling __mark_inode_dirty with the
1607 * returned flags value.
1609 int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
1610 struct ceph_cap_flush **pcf)
1612 struct ceph_mds_client *mdsc =
1613 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
1614 struct inode *inode = &ci->vfs_inode;
1615 int was = ci->i_dirty_caps;
1618 if (!ci->i_auth_cap) {
1619 pr_warn("__mark_dirty_caps %p %llx mask %s, "
1620 "but no auth cap (session was closed?)\n",
1621 inode, ceph_ino(inode), ceph_cap_string(mask));
1625 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1626 ceph_cap_string(mask), ceph_cap_string(was),
1627 ceph_cap_string(was | mask));
1628 ci->i_dirty_caps |= mask;
1630 WARN_ON_ONCE(ci->i_prealloc_cap_flush);
1631 swap(ci->i_prealloc_cap_flush, *pcf);
1633 if (!ci->i_head_snapc) {
1634 WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
1635 ci->i_head_snapc = ceph_get_snap_context(
1636 ci->i_snap_realm->cached_context);
1638 dout(" inode %p now dirty snapc %p auth cap %p\n",
1639 &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
1640 BUG_ON(!list_empty(&ci->i_dirty_item));
1641 spin_lock(&mdsc->cap_dirty_lock);
1642 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
1643 spin_unlock(&mdsc->cap_dirty_lock);
1644 if (ci->i_flushing_caps == 0) {
1646 dirty |= I_DIRTY_SYNC;
1649 WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
1651 BUG_ON(list_empty(&ci->i_dirty_item));
1652 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1653 (mask & CEPH_CAP_FILE_BUFFER))
1654 dirty |= I_DIRTY_DATASYNC;
1655 __cap_delay_requeue(mdsc, ci, true);
1659 struct ceph_cap_flush *ceph_alloc_cap_flush(void)
1661 return kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
1664 void ceph_free_cap_flush(struct ceph_cap_flush *cf)
1667 kmem_cache_free(ceph_cap_flush_cachep, cf);
1670 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
1672 if (!list_empty(&mdsc->cap_flush_list)) {
1673 struct ceph_cap_flush *cf =
1674 list_first_entry(&mdsc->cap_flush_list,
1675 struct ceph_cap_flush, g_list);
1682 * Remove cap_flush from the mdsc's or inode's flushing cap list.
1683 * Return true if caller needs to wake up flush waiters.
1685 static bool __finish_cap_flush(struct ceph_mds_client *mdsc,
1686 struct ceph_inode_info *ci,
1687 struct ceph_cap_flush *cf)
1689 struct ceph_cap_flush *prev;
1690 bool wake = cf->wake;
1692 /* are there older pending cap flushes? */
1693 if (wake && cf->g_list.prev != &mdsc->cap_flush_list) {
1694 prev = list_prev_entry(cf, g_list);
1698 list_del(&cf->g_list);
1700 if (wake && cf->i_list.prev != &ci->i_cap_flush_list) {
1701 prev = list_prev_entry(cf, i_list);
1705 list_del(&cf->i_list);
1713 * Add dirty inode to the flushing list. Assigned a seq number so we
1714 * can wait for caps to flush without starving.
1716 * Called under i_ceph_lock.
1718 static int __mark_caps_flushing(struct inode *inode,
1719 struct ceph_mds_session *session, bool wake,
1720 u64 *flush_tid, u64 *oldest_flush_tid)
1722 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1723 struct ceph_inode_info *ci = ceph_inode(inode);
1724 struct ceph_cap_flush *cf = NULL;
1727 BUG_ON(ci->i_dirty_caps == 0);
1728 BUG_ON(list_empty(&ci->i_dirty_item));
1729 BUG_ON(!ci->i_prealloc_cap_flush);
1731 flushing = ci->i_dirty_caps;
1732 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1733 ceph_cap_string(flushing),
1734 ceph_cap_string(ci->i_flushing_caps),
1735 ceph_cap_string(ci->i_flushing_caps | flushing));
1736 ci->i_flushing_caps |= flushing;
1737 ci->i_dirty_caps = 0;
1738 dout(" inode %p now !dirty\n", inode);
1740 swap(cf, ci->i_prealloc_cap_flush);
1741 cf->caps = flushing;
1744 spin_lock(&mdsc->cap_dirty_lock);
1745 list_del_init(&ci->i_dirty_item);
1747 cf->tid = ++mdsc->last_cap_flush_tid;
1748 list_add_tail(&cf->g_list, &mdsc->cap_flush_list);
1749 *oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1751 if (list_empty(&ci->i_flushing_item)) {
1752 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1753 mdsc->num_cap_flushing++;
1755 spin_unlock(&mdsc->cap_dirty_lock);
1757 list_add_tail(&cf->i_list, &ci->i_cap_flush_list);
1759 *flush_tid = cf->tid;
1764 * try to invalidate mapping pages without blocking.
1766 static int try_nonblocking_invalidate(struct inode *inode)
1768 struct ceph_inode_info *ci = ceph_inode(inode);
1769 u32 invalidating_gen = ci->i_rdcache_gen;
1771 spin_unlock(&ci->i_ceph_lock);
1772 invalidate_mapping_pages(&inode->i_data, 0, -1);
1773 spin_lock(&ci->i_ceph_lock);
1775 if (inode->i_data.nrpages == 0 &&
1776 invalidating_gen == ci->i_rdcache_gen) {
1778 dout("try_nonblocking_invalidate %p success\n", inode);
1779 /* save any racing async invalidate some trouble */
1780 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
1783 dout("try_nonblocking_invalidate %p failed\n", inode);
1787 bool __ceph_should_report_size(struct ceph_inode_info *ci)
1789 loff_t size = ci->vfs_inode.i_size;
1790 /* mds will adjust max size according to the reported size */
1791 if (ci->i_flushing_caps & CEPH_CAP_FILE_WR)
1793 if (size >= ci->i_max_size)
1795 /* half of previous max_size increment has been used */
1796 if (ci->i_max_size > ci->i_reported_size &&
1797 (size << 1) >= ci->i_max_size + ci->i_reported_size)
1803 * Swiss army knife function to examine currently used and wanted
1804 * versus held caps. Release, flush, ack revoked caps to mds as
1807 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1808 * cap release further.
1809 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1810 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1813 void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1814 struct ceph_mds_session *session)
1816 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1817 struct ceph_mds_client *mdsc = fsc->mdsc;
1818 struct inode *inode = &ci->vfs_inode;
1819 struct ceph_cap *cap;
1820 u64 flush_tid, oldest_flush_tid;
1821 int file_wanted, used, cap_used;
1822 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
1823 int issued, implemented, want, retain, revoking, flushing = 0;
1824 int mds = -1; /* keep track of how far we've gone through i_caps list
1825 to avoid an infinite loop on retry */
1827 int delayed = 0, sent = 0;
1828 bool no_delay = flags & CHECK_CAPS_NODELAY;
1829 bool queue_invalidate = false;
1830 bool tried_invalidate = false;
1832 /* if we are unmounting, flush any unused caps immediately. */
1836 spin_lock(&ci->i_ceph_lock);
1838 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1839 flags |= CHECK_CAPS_FLUSH;
1841 if (!(flags & CHECK_CAPS_AUTHONLY) ||
1842 (ci->i_auth_cap && __ceph_is_single_caps(ci)))
1843 __cap_delay_cancel(mdsc, ci);
1847 spin_lock(&ci->i_ceph_lock);
1849 file_wanted = __ceph_caps_file_wanted(ci);
1850 used = __ceph_caps_used(ci);
1851 issued = __ceph_caps_issued(ci, &implemented);
1852 revoking = implemented & ~issued;
1855 retain = file_wanted | used | CEPH_CAP_PIN;
1856 if (!mdsc->stopping && inode->i_nlink > 0) {
1858 retain |= CEPH_CAP_ANY; /* be greedy */
1859 } else if (S_ISDIR(inode->i_mode) &&
1860 (issued & CEPH_CAP_FILE_SHARED) &&
1861 __ceph_dir_is_complete(ci)) {
1863 * If a directory is complete, we want to keep
1864 * the exclusive cap. So that MDS does not end up
1865 * revoking the shared cap on every create/unlink
1868 if (IS_RDONLY(inode))
1869 want = CEPH_CAP_ANY_SHARED;
1871 want = CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
1875 retain |= CEPH_CAP_ANY_SHARED;
1877 * keep RD only if we didn't have the file open RW,
1878 * because then the mds would revoke it anyway to
1879 * journal max_size=0.
1881 if (ci->i_max_size == 0)
1882 retain |= CEPH_CAP_ANY_RD;
1886 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1887 " issued %s revoking %s retain %s %s%s%s\n", inode,
1888 ceph_cap_string(file_wanted),
1889 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1890 ceph_cap_string(ci->i_flushing_caps),
1891 ceph_cap_string(issued), ceph_cap_string(revoking),
1892 ceph_cap_string(retain),
1893 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1894 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1895 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1898 * If we no longer need to hold onto old our caps, and we may
1899 * have cached pages, but don't want them, then try to invalidate.
1900 * If we fail, it's because pages are locked.... try again later.
1902 if ((!no_delay || mdsc->stopping) &&
1903 !S_ISDIR(inode->i_mode) && /* ignore readdir cache */
1904 !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */
1905 inode->i_data.nrpages && /* have cached pages */
1906 (revoking & (CEPH_CAP_FILE_CACHE|
1907 CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */
1908 !tried_invalidate) {
1909 dout("check_caps trying to invalidate on %p\n", inode);
1910 if (try_nonblocking_invalidate(inode) < 0) {
1911 dout("check_caps queuing invalidate\n");
1912 queue_invalidate = true;
1913 ci->i_rdcache_revoking = ci->i_rdcache_gen;
1915 tried_invalidate = true;
1919 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1920 cap = rb_entry(p, struct ceph_cap, ci_node);
1922 /* avoid looping forever */
1923 if (mds >= cap->mds ||
1924 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1927 /* NOTE: no side-effects allowed, until we take s_mutex */
1930 if (ci->i_auth_cap && cap != ci->i_auth_cap)
1931 cap_used &= ~ci->i_auth_cap->issued;
1933 revoking = cap->implemented & ~cap->issued;
1934 dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
1935 cap->mds, cap, ceph_cap_string(cap_used),
1936 ceph_cap_string(cap->issued),
1937 ceph_cap_string(cap->implemented),
1938 ceph_cap_string(revoking));
1940 if (cap == ci->i_auth_cap &&
1941 (cap->issued & CEPH_CAP_FILE_WR)) {
1942 /* request larger max_size from MDS? */
1943 if (ci->i_wanted_max_size > ci->i_max_size &&
1944 ci->i_wanted_max_size > ci->i_requested_max_size) {
1945 dout("requesting new max_size\n");
1949 /* approaching file_max? */
1950 if (__ceph_should_report_size(ci)) {
1951 dout("i_size approaching max_size\n");
1955 /* flush anything dirty? */
1956 if (cap == ci->i_auth_cap) {
1957 if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) {
1958 dout("flushing dirty caps\n");
1961 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) {
1962 dout("flushing snap caps\n");
1967 /* completed revocation? going down and there are no caps? */
1968 if (revoking && (revoking & cap_used) == 0) {
1969 dout("completed revocation of %s\n",
1970 ceph_cap_string(cap->implemented & ~cap->issued));
1974 /* want more caps from mds? */
1975 if (want & ~(cap->mds_wanted | cap->issued))
1978 /* things we might delay */
1979 if ((cap->issued & ~retain) == 0)
1980 continue; /* nope, all good */
1986 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1987 time_before(jiffies, ci->i_hold_caps_max)) {
1988 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1989 ceph_cap_string(cap->issued),
1990 ceph_cap_string(cap->issued & retain),
1991 ceph_cap_string(cap->mds_wanted),
1992 ceph_cap_string(want));
1998 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1999 dout(" skipping %p I_NOFLUSH set\n", inode);
2003 if (session && session != cap->session) {
2004 dout("oops, wrong session %p mutex\n", session);
2005 mutex_unlock(&session->s_mutex);
2009 session = cap->session;
2010 if (mutex_trylock(&session->s_mutex) == 0) {
2011 dout("inverting session/ino locks on %p\n",
2013 spin_unlock(&ci->i_ceph_lock);
2014 if (took_snap_rwsem) {
2015 up_read(&mdsc->snap_rwsem);
2016 took_snap_rwsem = 0;
2018 mutex_lock(&session->s_mutex);
2023 /* kick flushing and flush snaps before sending normal
2025 if (cap == ci->i_auth_cap &&
2027 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) {
2028 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
2029 __kick_flushing_caps(mdsc, session, ci, 0);
2030 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2032 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
2033 __ceph_flush_snaps(ci, session);
2038 /* take snap_rwsem after session mutex */
2039 if (!took_snap_rwsem) {
2040 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
2041 dout("inverting snap/in locks on %p\n",
2043 spin_unlock(&ci->i_ceph_lock);
2044 down_read(&mdsc->snap_rwsem);
2045 took_snap_rwsem = 1;
2048 took_snap_rwsem = 1;
2051 if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
2052 flushing = __mark_caps_flushing(inode, session, false,
2058 spin_lock(&mdsc->cap_dirty_lock);
2059 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2060 spin_unlock(&mdsc->cap_dirty_lock);
2063 mds = cap->mds; /* remember mds, so we don't repeat */
2066 /* __send_cap drops i_ceph_lock */
2067 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, false,
2068 cap_used, want, retain, flushing,
2069 flush_tid, oldest_flush_tid);
2070 goto retry; /* retake i_ceph_lock and restart our cap scan. */
2073 /* Reschedule delayed caps release if we delayed anything */
2075 __cap_delay_requeue(mdsc, ci, false);
2077 spin_unlock(&ci->i_ceph_lock);
2079 if (queue_invalidate)
2080 ceph_queue_invalidate(inode);
2083 mutex_unlock(&session->s_mutex);
2084 if (took_snap_rwsem)
2085 up_read(&mdsc->snap_rwsem);
2089 * Try to flush dirty caps back to the auth mds.
2091 static int try_flush_caps(struct inode *inode, u64 *ptid)
2093 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2094 struct ceph_inode_info *ci = ceph_inode(inode);
2095 struct ceph_mds_session *session = NULL;
2097 u64 flush_tid = 0, oldest_flush_tid = 0;
2100 spin_lock(&ci->i_ceph_lock);
2101 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
2102 spin_unlock(&ci->i_ceph_lock);
2103 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
2106 if (ci->i_dirty_caps && ci->i_auth_cap) {
2107 struct ceph_cap *cap = ci->i_auth_cap;
2108 int used = __ceph_caps_used(ci);
2109 int want = __ceph_caps_wanted(ci);
2112 if (!session || session != cap->session) {
2113 spin_unlock(&ci->i_ceph_lock);
2115 mutex_unlock(&session->s_mutex);
2116 session = cap->session;
2117 mutex_lock(&session->s_mutex);
2120 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) {
2121 spin_unlock(&ci->i_ceph_lock);
2125 flushing = __mark_caps_flushing(inode, session, true,
2126 &flush_tid, &oldest_flush_tid);
2128 /* __send_cap drops i_ceph_lock */
2129 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, true,
2130 used, want, (cap->issued | cap->implemented),
2131 flushing, flush_tid, oldest_flush_tid);
2134 spin_lock(&ci->i_ceph_lock);
2135 __cap_delay_requeue(mdsc, ci, true);
2136 spin_unlock(&ci->i_ceph_lock);
2139 if (!list_empty(&ci->i_cap_flush_list)) {
2140 struct ceph_cap_flush *cf =
2141 list_last_entry(&ci->i_cap_flush_list,
2142 struct ceph_cap_flush, i_list);
2144 flush_tid = cf->tid;
2146 flushing = ci->i_flushing_caps;
2147 spin_unlock(&ci->i_ceph_lock);
2151 mutex_unlock(&session->s_mutex);
2158 * Return true if we've flushed caps through the given flush_tid.
2160 static int caps_are_flushed(struct inode *inode, u64 flush_tid)
2162 struct ceph_inode_info *ci = ceph_inode(inode);
2165 spin_lock(&ci->i_ceph_lock);
2166 if (!list_empty(&ci->i_cap_flush_list)) {
2167 struct ceph_cap_flush * cf =
2168 list_first_entry(&ci->i_cap_flush_list,
2169 struct ceph_cap_flush, i_list);
2170 if (cf->tid <= flush_tid)
2173 spin_unlock(&ci->i_ceph_lock);
2178 * wait for any unsafe requests to complete.
2180 static int unsafe_request_wait(struct inode *inode)
2182 struct ceph_inode_info *ci = ceph_inode(inode);
2183 struct ceph_mds_request *req1 = NULL, *req2 = NULL;
2186 spin_lock(&ci->i_unsafe_lock);
2187 if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
2188 req1 = list_last_entry(&ci->i_unsafe_dirops,
2189 struct ceph_mds_request,
2191 ceph_mdsc_get_request(req1);
2193 if (!list_empty(&ci->i_unsafe_iops)) {
2194 req2 = list_last_entry(&ci->i_unsafe_iops,
2195 struct ceph_mds_request,
2196 r_unsafe_target_item);
2197 ceph_mdsc_get_request(req2);
2199 spin_unlock(&ci->i_unsafe_lock);
2201 dout("unsafe_request_wait %p wait on tid %llu %llu\n",
2202 inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL);
2204 ret = !wait_for_completion_timeout(&req1->r_safe_completion,
2205 ceph_timeout_jiffies(req1->r_timeout));
2208 ceph_mdsc_put_request(req1);
2211 ret = !wait_for_completion_timeout(&req2->r_safe_completion,
2212 ceph_timeout_jiffies(req2->r_timeout));
2215 ceph_mdsc_put_request(req2);
2220 int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2222 struct inode *inode = file->f_mapping->host;
2223 struct ceph_inode_info *ci = ceph_inode(inode);
2228 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
2230 ret = file_write_and_wait_range(file, start, end);
2239 dirty = try_flush_caps(inode, &flush_tid);
2240 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
2242 ret = unsafe_request_wait(inode);
2245 * only wait on non-file metadata writeback (the mds
2246 * can recover size and mtime, so we don't need to
2249 if (!ret && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
2250 ret = wait_event_interruptible(ci->i_cap_wq,
2251 caps_are_flushed(inode, flush_tid));
2253 inode_unlock(inode);
2255 dout("fsync %p%s result=%d\n", inode, datasync ? " datasync" : "", ret);
2260 * Flush any dirty caps back to the mds. If we aren't asked to wait,
2261 * queue inode for flush but don't do so immediately, because we can
2262 * get by with fewer MDS messages if we wait for data writeback to
2265 int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
2267 struct ceph_inode_info *ci = ceph_inode(inode);
2271 int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync);
2273 dout("write_inode %p wait=%d\n", inode, wait);
2275 dirty = try_flush_caps(inode, &flush_tid);
2277 err = wait_event_interruptible(ci->i_cap_wq,
2278 caps_are_flushed(inode, flush_tid));
2280 struct ceph_mds_client *mdsc =
2281 ceph_sb_to_client(inode->i_sb)->mdsc;
2283 spin_lock(&ci->i_ceph_lock);
2284 if (__ceph_caps_dirty(ci))
2285 __cap_delay_requeue_front(mdsc, ci);
2286 spin_unlock(&ci->i_ceph_lock);
2291 static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
2292 struct ceph_mds_session *session,
2293 struct ceph_inode_info *ci,
2294 u64 oldest_flush_tid)
2295 __releases(ci->i_ceph_lock)
2296 __acquires(ci->i_ceph_lock)
2298 struct inode *inode = &ci->vfs_inode;
2299 struct ceph_cap *cap;
2300 struct ceph_cap_flush *cf;
2304 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
2305 if (cf->tid < first_tid)
2308 cap = ci->i_auth_cap;
2309 if (!(cap && cap->session == session)) {
2310 pr_err("%p auth cap %p not mds%d ???\n",
2311 inode, cap, session->s_mds);
2315 first_tid = cf->tid + 1;
2318 dout("kick_flushing_caps %p cap %p tid %llu %s\n",
2319 inode, cap, cf->tid, ceph_cap_string(cf->caps));
2320 ci->i_ceph_flags |= CEPH_I_NODELAY;
2321 ret = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
2322 false, __ceph_caps_used(ci),
2323 __ceph_caps_wanted(ci),
2324 cap->issued | cap->implemented,
2325 cf->caps, cf->tid, oldest_flush_tid);
2327 pr_err("kick_flushing_caps: error sending "
2328 "cap flush, ino (%llx.%llx) "
2329 "tid %llu flushing %s\n",
2330 ceph_vinop(inode), cf->tid,
2331 ceph_cap_string(cf->caps));
2334 struct ceph_cap_snap *capsnap =
2335 container_of(cf, struct ceph_cap_snap,
2337 dout("kick_flushing_caps %p capsnap %p tid %llu %s\n",
2338 inode, capsnap, cf->tid,
2339 ceph_cap_string(capsnap->dirty));
2341 refcount_inc(&capsnap->nref);
2342 spin_unlock(&ci->i_ceph_lock);
2344 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
2347 pr_err("kick_flushing_caps: error sending "
2348 "cap flushsnap, ino (%llx.%llx) "
2349 "tid %llu follows %llu\n",
2350 ceph_vinop(inode), cf->tid,
2354 ceph_put_cap_snap(capsnap);
2357 spin_lock(&ci->i_ceph_lock);
2361 void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
2362 struct ceph_mds_session *session)
2364 struct ceph_inode_info *ci;
2365 struct ceph_cap *cap;
2366 u64 oldest_flush_tid;
2368 dout("early_kick_flushing_caps mds%d\n", session->s_mds);
2370 spin_lock(&mdsc->cap_dirty_lock);
2371 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2372 spin_unlock(&mdsc->cap_dirty_lock);
2374 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2375 spin_lock(&ci->i_ceph_lock);
2376 cap = ci->i_auth_cap;
2377 if (!(cap && cap->session == session)) {
2378 pr_err("%p auth cap %p not mds%d ???\n",
2379 &ci->vfs_inode, cap, session->s_mds);
2380 spin_unlock(&ci->i_ceph_lock);
2386 * if flushing caps were revoked, we re-send the cap flush
2387 * in client reconnect stage. This guarantees MDS * processes
2388 * the cap flush message before issuing the flushing caps to
2391 if ((cap->issued & ci->i_flushing_caps) !=
2392 ci->i_flushing_caps) {
2393 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2394 __kick_flushing_caps(mdsc, session, ci,
2397 ci->i_ceph_flags |= CEPH_I_KICK_FLUSH;
2400 spin_unlock(&ci->i_ceph_lock);
2404 void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
2405 struct ceph_mds_session *session)
2407 struct ceph_inode_info *ci;
2408 struct ceph_cap *cap;
2409 u64 oldest_flush_tid;
2411 dout("kick_flushing_caps mds%d\n", session->s_mds);
2413 spin_lock(&mdsc->cap_dirty_lock);
2414 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2415 spin_unlock(&mdsc->cap_dirty_lock);
2417 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2418 spin_lock(&ci->i_ceph_lock);
2419 cap = ci->i_auth_cap;
2420 if (!(cap && cap->session == session)) {
2421 pr_err("%p auth cap %p not mds%d ???\n",
2422 &ci->vfs_inode, cap, session->s_mds);
2423 spin_unlock(&ci->i_ceph_lock);
2426 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
2427 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2428 __kick_flushing_caps(mdsc, session, ci,
2431 spin_unlock(&ci->i_ceph_lock);
2435 static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
2436 struct ceph_mds_session *session,
2437 struct inode *inode)
2438 __releases(ci->i_ceph_lock)
2440 struct ceph_inode_info *ci = ceph_inode(inode);
2441 struct ceph_cap *cap;
2443 cap = ci->i_auth_cap;
2444 dout("kick_flushing_inode_caps %p flushing %s\n", inode,
2445 ceph_cap_string(ci->i_flushing_caps));
2447 if (!list_empty(&ci->i_cap_flush_list)) {
2448 u64 oldest_flush_tid;
2449 spin_lock(&mdsc->cap_dirty_lock);
2450 list_move_tail(&ci->i_flushing_item,
2451 &cap->session->s_cap_flushing);
2452 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2453 spin_unlock(&mdsc->cap_dirty_lock);
2455 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2456 __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid);
2457 spin_unlock(&ci->i_ceph_lock);
2459 spin_unlock(&ci->i_ceph_lock);
2465 * Take references to capabilities we hold, so that we don't release
2466 * them to the MDS prematurely.
2468 * Protected by i_ceph_lock.
2470 static void __take_cap_refs(struct ceph_inode_info *ci, int got,
2471 bool snap_rwsem_locked)
2473 if (got & CEPH_CAP_PIN)
2475 if (got & CEPH_CAP_FILE_RD)
2477 if (got & CEPH_CAP_FILE_CACHE)
2478 ci->i_rdcache_ref++;
2479 if (got & CEPH_CAP_FILE_WR) {
2480 if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
2481 BUG_ON(!snap_rwsem_locked);
2482 ci->i_head_snapc = ceph_get_snap_context(
2483 ci->i_snap_realm->cached_context);
2487 if (got & CEPH_CAP_FILE_BUFFER) {
2488 if (ci->i_wb_ref == 0)
2489 ihold(&ci->vfs_inode);
2491 dout("__take_cap_refs %p wb %d -> %d (?)\n",
2492 &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
2497 * Try to grab cap references. Specify those refs we @want, and the
2498 * minimal set we @need. Also include the larger offset we are writing
2499 * to (when applicable), and check against max_size here as well.
2500 * Note that caller is responsible for ensuring max_size increases are
2501 * requested from the MDS.
2503 static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
2504 loff_t endoff, bool nonblock, int *got, int *err)
2506 struct inode *inode = &ci->vfs_inode;
2507 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
2509 int have, implemented;
2511 bool snap_rwsem_locked = false;
2513 dout("get_cap_refs %p need %s want %s\n", inode,
2514 ceph_cap_string(need), ceph_cap_string(want));
2517 spin_lock(&ci->i_ceph_lock);
2519 /* make sure file is actually open */
2520 file_wanted = __ceph_caps_file_wanted(ci);
2521 if ((file_wanted & need) != need) {
2522 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2523 ceph_cap_string(need), ceph_cap_string(file_wanted));
2529 /* finish pending truncate */
2530 while (ci->i_truncate_pending) {
2531 spin_unlock(&ci->i_ceph_lock);
2532 if (snap_rwsem_locked) {
2533 up_read(&mdsc->snap_rwsem);
2534 snap_rwsem_locked = false;
2536 __ceph_do_pending_vmtruncate(inode);
2537 spin_lock(&ci->i_ceph_lock);
2540 have = __ceph_caps_issued(ci, &implemented);
2542 if (have & need & CEPH_CAP_FILE_WR) {
2543 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2544 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2545 inode, endoff, ci->i_max_size);
2546 if (endoff > ci->i_requested_max_size) {
2553 * If a sync write is in progress, we must wait, so that we
2554 * can get a final snapshot value for size+mtime.
2556 if (__ceph_have_pending_cap_snap(ci)) {
2557 dout("get_cap_refs %p cap_snap_pending\n", inode);
2562 if ((have & need) == need) {
2564 * Look at (implemented & ~have & not) so that we keep waiting
2565 * on transition from wanted -> needed caps. This is needed
2566 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2567 * going before a prior buffered writeback happens.
2569 int not = want & ~(have & need);
2570 int revoking = implemented & ~have;
2571 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2572 inode, ceph_cap_string(have), ceph_cap_string(not),
2573 ceph_cap_string(revoking));
2574 if ((revoking & not) == 0) {
2575 if (!snap_rwsem_locked &&
2576 !ci->i_head_snapc &&
2577 (need & CEPH_CAP_FILE_WR)) {
2578 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2580 * we can not call down_read() when
2581 * task isn't in TASK_RUNNING state
2589 spin_unlock(&ci->i_ceph_lock);
2590 down_read(&mdsc->snap_rwsem);
2591 snap_rwsem_locked = true;
2594 snap_rwsem_locked = true;
2596 *got = need | (have & want);
2597 if ((need & CEPH_CAP_FILE_RD) &&
2598 !(*got & CEPH_CAP_FILE_CACHE))
2599 ceph_disable_fscache_readpage(ci);
2600 __take_cap_refs(ci, *got, true);
2604 int session_readonly = false;
2605 if ((need & CEPH_CAP_FILE_WR) && ci->i_auth_cap) {
2606 struct ceph_mds_session *s = ci->i_auth_cap->session;
2607 spin_lock(&s->s_cap_lock);
2608 session_readonly = s->s_readonly;
2609 spin_unlock(&s->s_cap_lock);
2611 if (session_readonly) {
2612 dout("get_cap_refs %p needed %s but mds%d readonly\n",
2613 inode, ceph_cap_string(need), ci->i_auth_cap->mds);
2619 if (ci->i_ceph_flags & CEPH_I_CAP_DROPPED) {
2621 if (READ_ONCE(mdsc->fsc->mount_state) ==
2622 CEPH_MOUNT_SHUTDOWN) {
2623 dout("get_cap_refs %p forced umount\n", inode);
2628 mds_wanted = __ceph_caps_mds_wanted(ci, false);
2629 if (need & ~(mds_wanted & need)) {
2630 dout("get_cap_refs %p caps were dropped"
2631 " (session killed?)\n", inode);
2636 if (!(file_wanted & ~mds_wanted))
2637 ci->i_ceph_flags &= ~CEPH_I_CAP_DROPPED;
2640 dout("get_cap_refs %p have %s needed %s\n", inode,
2641 ceph_cap_string(have), ceph_cap_string(need));
2644 spin_unlock(&ci->i_ceph_lock);
2645 if (snap_rwsem_locked)
2646 up_read(&mdsc->snap_rwsem);
2648 dout("get_cap_refs %p ret %d got %s\n", inode,
2649 ret, ceph_cap_string(*got));
2654 * Check the offset we are writing up to against our current
2655 * max_size. If necessary, tell the MDS we want to write to
2658 static void check_max_size(struct inode *inode, loff_t endoff)
2660 struct ceph_inode_info *ci = ceph_inode(inode);
2663 /* do we need to explicitly request a larger max_size? */
2664 spin_lock(&ci->i_ceph_lock);
2665 if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
2666 dout("write %p at large endoff %llu, req max_size\n",
2668 ci->i_wanted_max_size = endoff;
2670 /* duplicate ceph_check_caps()'s logic */
2671 if (ci->i_auth_cap &&
2672 (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
2673 ci->i_wanted_max_size > ci->i_max_size &&
2674 ci->i_wanted_max_size > ci->i_requested_max_size)
2676 spin_unlock(&ci->i_ceph_lock);
2678 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2681 int ceph_try_get_caps(struct ceph_inode_info *ci, int need, int want,
2682 bool nonblock, int *got)
2686 BUG_ON(need & ~CEPH_CAP_FILE_RD);
2687 BUG_ON(want & ~(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO|CEPH_CAP_FILE_SHARED));
2688 ret = ceph_pool_perm_check(ci, need);
2692 ret = try_get_cap_refs(ci, need, want, 0, nonblock, got, &err);
2694 if (err == -EAGAIN) {
2696 } else if (err < 0) {
2704 * Wait for caps, and take cap references. If we can't get a WR cap
2705 * due to a small max_size, make sure we check_max_size (and possibly
2706 * ask the mds) so we don't get hung up indefinitely.
2708 int ceph_get_caps(struct ceph_inode_info *ci, int need, int want,
2709 loff_t endoff, int *got, struct page **pinned_page)
2711 int _got, ret, err = 0;
2713 ret = ceph_pool_perm_check(ci, need);
2719 check_max_size(&ci->vfs_inode, endoff);
2723 ret = try_get_cap_refs(ci, need, want, endoff,
2724 false, &_got, &err);
2731 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2732 add_wait_queue(&ci->i_cap_wq, &wait);
2734 while (!try_get_cap_refs(ci, need, want, endoff,
2735 true, &_got, &err)) {
2736 if (signal_pending(current)) {
2740 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
2743 remove_wait_queue(&ci->i_cap_wq, &wait);
2751 if (err == -ESTALE) {
2752 /* session was killed, try renew caps */
2753 ret = ceph_renew_caps(&ci->vfs_inode);
2760 if (ci->i_inline_version != CEPH_INLINE_NONE &&
2761 (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
2762 i_size_read(&ci->vfs_inode) > 0) {
2764 find_get_page(ci->vfs_inode.i_mapping, 0);
2766 if (PageUptodate(page)) {
2767 *pinned_page = page;
2773 * drop cap refs first because getattr while
2774 * holding * caps refs can cause deadlock.
2776 ceph_put_cap_refs(ci, _got);
2780 * getattr request will bring inline data into
2783 ret = __ceph_do_getattr(&ci->vfs_inode, NULL,
2784 CEPH_STAT_CAP_INLINE_DATA,
2793 if ((_got & CEPH_CAP_FILE_RD) && (_got & CEPH_CAP_FILE_CACHE))
2794 ceph_fscache_revalidate_cookie(ci);
2801 * Take cap refs. Caller must already know we hold at least one ref
2802 * on the caps in question or we don't know this is safe.
2804 void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2806 spin_lock(&ci->i_ceph_lock);
2807 __take_cap_refs(ci, caps, false);
2808 spin_unlock(&ci->i_ceph_lock);
2813 * drop cap_snap that is not associated with any snapshot.
2814 * we don't need to send FLUSHSNAP message for it.
2816 static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci,
2817 struct ceph_cap_snap *capsnap)
2819 if (!capsnap->need_flush &&
2820 !capsnap->writing && !capsnap->dirty_pages) {
2821 dout("dropping cap_snap %p follows %llu\n",
2822 capsnap, capsnap->follows);
2823 BUG_ON(capsnap->cap_flush.tid > 0);
2824 ceph_put_snap_context(capsnap->context);
2825 if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps))
2826 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2828 list_del(&capsnap->ci_item);
2829 ceph_put_cap_snap(capsnap);
2838 * If we released the last ref on any given cap, call ceph_check_caps
2839 * to release (or schedule a release).
2841 * If we are releasing a WR cap (from a sync write), finalize any affected
2842 * cap_snap, and wake up any waiters.
2844 void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2846 struct inode *inode = &ci->vfs_inode;
2847 int last = 0, put = 0, flushsnaps = 0, wake = 0;
2849 spin_lock(&ci->i_ceph_lock);
2850 if (had & CEPH_CAP_PIN)
2852 if (had & CEPH_CAP_FILE_RD)
2853 if (--ci->i_rd_ref == 0)
2855 if (had & CEPH_CAP_FILE_CACHE)
2856 if (--ci->i_rdcache_ref == 0)
2858 if (had & CEPH_CAP_FILE_BUFFER) {
2859 if (--ci->i_wb_ref == 0) {
2863 dout("put_cap_refs %p wb %d -> %d (?)\n",
2864 inode, ci->i_wb_ref+1, ci->i_wb_ref);
2866 if (had & CEPH_CAP_FILE_WR)
2867 if (--ci->i_wr_ref == 0) {
2869 if (__ceph_have_pending_cap_snap(ci)) {
2870 struct ceph_cap_snap *capsnap =
2871 list_last_entry(&ci->i_cap_snaps,
2872 struct ceph_cap_snap,
2874 capsnap->writing = 0;
2875 if (ceph_try_drop_cap_snap(ci, capsnap))
2877 else if (__ceph_finish_cap_snap(ci, capsnap))
2881 if (ci->i_wrbuffer_ref_head == 0 &&
2882 ci->i_dirty_caps == 0 &&
2883 ci->i_flushing_caps == 0) {
2884 BUG_ON(!ci->i_head_snapc);
2885 ceph_put_snap_context(ci->i_head_snapc);
2886 ci->i_head_snapc = NULL;
2888 /* see comment in __ceph_remove_cap() */
2889 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm)
2890 drop_inode_snap_realm(ci);
2892 spin_unlock(&ci->i_ceph_lock);
2894 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2895 last ? " last" : "", put ? " put" : "");
2897 if (last && !flushsnaps)
2898 ceph_check_caps(ci, 0, NULL);
2899 else if (flushsnaps)
2900 ceph_flush_snaps(ci, NULL);
2902 wake_up_all(&ci->i_cap_wq);
2908 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2909 * context. Adjust per-snap dirty page accounting as appropriate.
2910 * Once all dirty data for a cap_snap is flushed, flush snapped file
2911 * metadata back to the MDS. If we dropped the last ref, call
2914 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2915 struct ceph_snap_context *snapc)
2917 struct inode *inode = &ci->vfs_inode;
2918 struct ceph_cap_snap *capsnap = NULL;
2922 bool flush_snaps = false;
2923 bool complete_capsnap = false;
2925 spin_lock(&ci->i_ceph_lock);
2926 ci->i_wrbuffer_ref -= nr;
2927 if (ci->i_wrbuffer_ref == 0) {
2932 if (ci->i_head_snapc == snapc) {
2933 ci->i_wrbuffer_ref_head -= nr;
2934 if (ci->i_wrbuffer_ref_head == 0 &&
2935 ci->i_wr_ref == 0 &&
2936 ci->i_dirty_caps == 0 &&
2937 ci->i_flushing_caps == 0) {
2938 BUG_ON(!ci->i_head_snapc);
2939 ceph_put_snap_context(ci->i_head_snapc);
2940 ci->i_head_snapc = NULL;
2942 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2944 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2945 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2946 last ? " LAST" : "");
2948 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2949 if (capsnap->context == snapc) {
2955 capsnap->dirty_pages -= nr;
2956 if (capsnap->dirty_pages == 0) {
2957 complete_capsnap = true;
2958 if (!capsnap->writing) {
2959 if (ceph_try_drop_cap_snap(ci, capsnap)) {
2962 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2967 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2968 " snap %lld %d/%d -> %d/%d %s%s\n",
2969 inode, capsnap, capsnap->context->seq,
2970 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2971 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2972 last ? " (wrbuffer last)" : "",
2973 complete_capsnap ? " (complete capsnap)" : "");
2976 spin_unlock(&ci->i_ceph_lock);
2979 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2980 } else if (flush_snaps) {
2981 ceph_flush_snaps(ci, NULL);
2983 if (complete_capsnap)
2984 wake_up_all(&ci->i_cap_wq);
2990 * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
2992 static void invalidate_aliases(struct inode *inode)
2994 struct dentry *dn, *prev = NULL;
2996 dout("invalidate_aliases inode %p\n", inode);
2997 d_prune_aliases(inode);
2999 * For non-directory inode, d_find_alias() only returns
3000 * hashed dentry. After calling d_invalidate(), the
3001 * dentry becomes unhashed.
3003 * For directory inode, d_find_alias() can return
3004 * unhashed dentry. But directory inode should have
3005 * one alias at most.
3007 while ((dn = d_find_alias(inode))) {
3021 struct cap_extra_info {
3022 struct ceph_string *pool_ns;
3031 /* currently issued */
3036 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
3037 * actually be a revocation if it specifies a smaller cap set.)
3039 * caller holds s_mutex and i_ceph_lock, we drop both.
3041 static void handle_cap_grant(struct inode *inode,
3042 struct ceph_mds_session *session,
3043 struct ceph_cap *cap,
3044 struct ceph_mds_caps *grant,
3045 struct ceph_buffer *xattr_buf,
3046 struct cap_extra_info *extra_info)
3047 __releases(ci->i_ceph_lock)
3048 __releases(session->s_mdsc->snap_rwsem)
3050 struct ceph_inode_info *ci = ceph_inode(inode);
3051 int seq = le32_to_cpu(grant->seq);
3052 int newcaps = le32_to_cpu(grant->caps);
3053 int used, wanted, dirty;
3054 u64 size = le64_to_cpu(grant->size);
3055 u64 max_size = le64_to_cpu(grant->max_size);
3056 unsigned char check_caps = 0;
3057 bool was_stale = cap->cap_gen < session->s_cap_gen;
3059 bool writeback = false;
3060 bool queue_trunc = false;
3061 bool queue_invalidate = false;
3062 bool deleted_inode = false;
3063 bool fill_inline = false;
3065 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
3066 inode, cap, session->s_mds, seq, ceph_cap_string(newcaps));
3067 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
3072 * If CACHE is being revoked, and we have no dirty buffers,
3073 * try to invalidate (once). (If there are dirty buffers, we
3074 * will invalidate _after_ writeback.)
3076 if (!S_ISDIR(inode->i_mode) && /* don't invalidate readdir cache */
3077 ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
3078 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3079 !(ci->i_wrbuffer_ref || ci->i_wb_ref)) {
3080 if (try_nonblocking_invalidate(inode)) {
3081 /* there were locked pages.. invalidate later
3082 in a separate thread. */
3083 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
3084 queue_invalidate = true;
3085 ci->i_rdcache_revoking = ci->i_rdcache_gen;
3091 cap->issued = cap->implemented = CEPH_CAP_PIN;
3094 * auth mds of the inode changed. we received the cap export message,
3095 * but still haven't received the cap import message. handle_cap_export
3096 * updated the new auth MDS' cap.
3098 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
3099 * that was sent before the cap import message. So don't remove caps.
3101 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
3102 WARN_ON(cap != ci->i_auth_cap);
3103 WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
3105 newcaps |= cap->issued;
3108 /* side effects now are allowed */
3109 cap->cap_gen = session->s_cap_gen;
3112 __check_cap_issue(ci, cap, newcaps);
3114 if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
3115 (extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) {
3116 inode->i_mode = le32_to_cpu(grant->mode);
3117 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
3118 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
3119 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
3120 from_kuid(&init_user_ns, inode->i_uid),
3121 from_kgid(&init_user_ns, inode->i_gid));
3124 if ((newcaps & CEPH_CAP_LINK_SHARED) &&
3125 (extra_info->issued & CEPH_CAP_LINK_EXCL) == 0) {
3126 set_nlink(inode, le32_to_cpu(grant->nlink));
3127 if (inode->i_nlink == 0 &&
3128 (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
3129 deleted_inode = true;
3132 if ((extra_info->issued & CEPH_CAP_XATTR_EXCL) == 0 &&
3134 int len = le32_to_cpu(grant->xattr_len);
3135 u64 version = le64_to_cpu(grant->xattr_version);
3137 if (version > ci->i_xattrs.version) {
3138 dout(" got new xattrs v%llu on %p len %d\n",
3139 version, inode, len);
3140 if (ci->i_xattrs.blob)
3141 ceph_buffer_put(ci->i_xattrs.blob);
3142 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
3143 ci->i_xattrs.version = version;
3144 ceph_forget_all_cached_acls(inode);
3148 if (newcaps & CEPH_CAP_ANY_RD) {
3149 struct timespec64 mtime, atime, ctime;
3150 /* ctime/mtime/atime? */
3151 ceph_decode_timespec64(&mtime, &grant->mtime);
3152 ceph_decode_timespec64(&atime, &grant->atime);
3153 ceph_decode_timespec64(&ctime, &grant->ctime);
3154 ceph_fill_file_time(inode, extra_info->issued,
3155 le32_to_cpu(grant->time_warp_seq),
3156 &ctime, &mtime, &atime);
3159 if ((newcaps & CEPH_CAP_FILE_SHARED) && extra_info->dirstat_valid) {
3160 ci->i_files = extra_info->nfiles;
3161 ci->i_subdirs = extra_info->nsubdirs;
3164 if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
3165 /* file layout may have changed */
3166 s64 old_pool = ci->i_layout.pool_id;
3167 struct ceph_string *old_ns;
3169 ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout);
3170 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
3171 lockdep_is_held(&ci->i_ceph_lock));
3172 rcu_assign_pointer(ci->i_layout.pool_ns, extra_info->pool_ns);
3174 if (ci->i_layout.pool_id != old_pool ||
3175 extra_info->pool_ns != old_ns)
3176 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
3178 extra_info->pool_ns = old_ns;
3180 /* size/truncate_seq? */
3181 queue_trunc = ceph_fill_file_size(inode, extra_info->issued,
3182 le32_to_cpu(grant->truncate_seq),
3183 le64_to_cpu(grant->truncate_size),
3187 if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) {
3188 if (max_size != ci->i_max_size) {
3189 dout("max_size %lld -> %llu\n",
3190 ci->i_max_size, max_size);
3191 ci->i_max_size = max_size;
3192 if (max_size >= ci->i_wanted_max_size) {
3193 ci->i_wanted_max_size = 0; /* reset */
3194 ci->i_requested_max_size = 0;
3197 } else if (ci->i_wanted_max_size > ci->i_max_size &&
3198 ci->i_wanted_max_size > ci->i_requested_max_size) {
3199 /* CEPH_CAP_OP_IMPORT */
3204 /* check cap bits */
3205 wanted = __ceph_caps_wanted(ci);
3206 used = __ceph_caps_used(ci);
3207 dirty = __ceph_caps_dirty(ci);
3208 dout(" my wanted = %s, used = %s, dirty %s\n",
3209 ceph_cap_string(wanted),
3210 ceph_cap_string(used),
3211 ceph_cap_string(dirty));
3213 if ((was_stale || le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) &&
3214 (wanted & ~(cap->mds_wanted | newcaps))) {
3216 * If mds is importing cap, prior cap messages that update
3217 * 'wanted' may get dropped by mds (migrate seq mismatch).
3219 * We don't send cap message to update 'wanted' if what we
3220 * want are already issued. If mds revokes caps, cap message
3221 * that releases caps also tells mds what we want. But if
3222 * caps got revoked by mds forcedly (session stale). We may
3223 * haven't told mds what we want.
3228 /* revocation, grant, or no-op? */
3229 if (cap->issued & ~newcaps) {
3230 int revoking = cap->issued & ~newcaps;
3232 dout("revocation: %s -> %s (revoking %s)\n",
3233 ceph_cap_string(cap->issued),
3234 ceph_cap_string(newcaps),
3235 ceph_cap_string(revoking));
3236 if (revoking & used & CEPH_CAP_FILE_BUFFER)
3237 writeback = true; /* initiate writeback; will delay ack */
3238 else if (revoking == CEPH_CAP_FILE_CACHE &&
3239 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3241 ; /* do nothing yet, invalidation will be queued */
3242 else if (cap == ci->i_auth_cap)
3243 check_caps = 1; /* check auth cap only */
3245 check_caps = 2; /* check all caps */
3246 cap->issued = newcaps;
3247 cap->implemented |= newcaps;
3248 } else if (cap->issued == newcaps) {
3249 dout("caps unchanged: %s -> %s\n",
3250 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
3252 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
3253 ceph_cap_string(newcaps));
3254 /* non-auth MDS is revoking the newly grant caps ? */
3255 if (cap == ci->i_auth_cap &&
3256 __ceph_caps_revoking_other(ci, cap, newcaps))
3259 cap->issued = newcaps;
3260 cap->implemented |= newcaps; /* add bits only, to
3261 * avoid stepping on a
3262 * pending revocation */
3265 BUG_ON(cap->issued & ~cap->implemented);
3267 if (extra_info->inline_version > 0 &&
3268 extra_info->inline_version >= ci->i_inline_version) {
3269 ci->i_inline_version = extra_info->inline_version;
3270 if (ci->i_inline_version != CEPH_INLINE_NONE &&
3271 (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
3275 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
3276 if (newcaps & ~extra_info->issued)
3278 kick_flushing_inode_caps(session->s_mdsc, session, inode);
3279 up_read(&session->s_mdsc->snap_rwsem);
3281 spin_unlock(&ci->i_ceph_lock);
3285 ceph_fill_inline_data(inode, NULL, extra_info->inline_data,
3286 extra_info->inline_len);
3289 ceph_queue_vmtruncate(inode);
3293 * queue inode for writeback: we can't actually call
3294 * filemap_write_and_wait, etc. from message handler
3297 ceph_queue_writeback(inode);
3298 if (queue_invalidate)
3299 ceph_queue_invalidate(inode);
3301 invalidate_aliases(inode);
3303 wake_up_all(&ci->i_cap_wq);
3305 if (check_caps == 1)
3306 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
3308 else if (check_caps == 2)
3309 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
3311 mutex_unlock(&session->s_mutex);
3315 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
3316 * MDS has been safely committed.
3318 static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
3319 struct ceph_mds_caps *m,
3320 struct ceph_mds_session *session,
3321 struct ceph_cap *cap)
3322 __releases(ci->i_ceph_lock)
3324 struct ceph_inode_info *ci = ceph_inode(inode);
3325 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3326 struct ceph_cap_flush *cf, *tmp_cf;
3327 LIST_HEAD(to_remove);
3328 unsigned seq = le32_to_cpu(m->seq);
3329 int dirty = le32_to_cpu(m->dirty);
3332 bool wake_ci = false;
3333 bool wake_mdsc = false;
3335 list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
3336 if (cf->tid == flush_tid)
3338 if (cf->caps == 0) /* capsnap */
3340 if (cf->tid <= flush_tid) {
3341 if (__finish_cap_flush(NULL, ci, cf))
3343 list_add_tail(&cf->i_list, &to_remove);
3345 cleaned &= ~cf->caps;
3351 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
3352 " flushing %s -> %s\n",
3353 inode, session->s_mds, seq, ceph_cap_string(dirty),
3354 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
3355 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
3357 if (list_empty(&to_remove) && !cleaned)
3360 ci->i_flushing_caps &= ~cleaned;
3362 spin_lock(&mdsc->cap_dirty_lock);
3364 list_for_each_entry(cf, &to_remove, i_list) {
3365 if (__finish_cap_flush(mdsc, NULL, cf))
3369 if (ci->i_flushing_caps == 0) {
3370 if (list_empty(&ci->i_cap_flush_list)) {
3371 list_del_init(&ci->i_flushing_item);
3372 if (!list_empty(&session->s_cap_flushing)) {
3373 dout(" mds%d still flushing cap on %p\n",
3375 &list_first_entry(&session->s_cap_flushing,
3376 struct ceph_inode_info,
3377 i_flushing_item)->vfs_inode);
3380 mdsc->num_cap_flushing--;
3381 dout(" inode %p now !flushing\n", inode);
3383 if (ci->i_dirty_caps == 0) {
3384 dout(" inode %p now clean\n", inode);
3385 BUG_ON(!list_empty(&ci->i_dirty_item));
3387 if (ci->i_wr_ref == 0 &&
3388 ci->i_wrbuffer_ref_head == 0) {
3389 BUG_ON(!ci->i_head_snapc);
3390 ceph_put_snap_context(ci->i_head_snapc);
3391 ci->i_head_snapc = NULL;
3394 BUG_ON(list_empty(&ci->i_dirty_item));
3397 spin_unlock(&mdsc->cap_dirty_lock);
3400 spin_unlock(&ci->i_ceph_lock);
3402 while (!list_empty(&to_remove)) {
3403 cf = list_first_entry(&to_remove,
3404 struct ceph_cap_flush, i_list);
3405 list_del(&cf->i_list);
3406 ceph_free_cap_flush(cf);
3410 wake_up_all(&ci->i_cap_wq);
3412 wake_up_all(&mdsc->cap_flushing_wq);
3418 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
3419 * throw away our cap_snap.
3421 * Caller hold s_mutex.
3423 static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
3424 struct ceph_mds_caps *m,
3425 struct ceph_mds_session *session)
3427 struct ceph_inode_info *ci = ceph_inode(inode);
3428 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3429 u64 follows = le64_to_cpu(m->snap_follows);
3430 struct ceph_cap_snap *capsnap;
3431 bool flushed = false;
3432 bool wake_ci = false;
3433 bool wake_mdsc = false;
3435 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
3436 inode, ci, session->s_mds, follows);
3438 spin_lock(&ci->i_ceph_lock);
3439 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
3440 if (capsnap->follows == follows) {
3441 if (capsnap->cap_flush.tid != flush_tid) {
3442 dout(" cap_snap %p follows %lld tid %lld !="
3443 " %lld\n", capsnap, follows,
3444 flush_tid, capsnap->cap_flush.tid);
3450 dout(" skipping cap_snap %p follows %lld\n",
3451 capsnap, capsnap->follows);
3455 WARN_ON(capsnap->dirty_pages || capsnap->writing);
3456 dout(" removing %p cap_snap %p follows %lld\n",
3457 inode, capsnap, follows);
3458 list_del(&capsnap->ci_item);
3459 if (__finish_cap_flush(NULL, ci, &capsnap->cap_flush))
3462 spin_lock(&mdsc->cap_dirty_lock);
3464 if (list_empty(&ci->i_cap_flush_list))
3465 list_del_init(&ci->i_flushing_item);
3467 if (__finish_cap_flush(mdsc, NULL, &capsnap->cap_flush))
3470 spin_unlock(&mdsc->cap_dirty_lock);
3472 spin_unlock(&ci->i_ceph_lock);
3474 ceph_put_snap_context(capsnap->context);
3475 ceph_put_cap_snap(capsnap);
3477 wake_up_all(&ci->i_cap_wq);
3479 wake_up_all(&mdsc->cap_flushing_wq);
3485 * Handle TRUNC from MDS, indicating file truncation.
3487 * caller hold s_mutex.
3489 static void handle_cap_trunc(struct inode *inode,
3490 struct ceph_mds_caps *trunc,
3491 struct ceph_mds_session *session)
3492 __releases(ci->i_ceph_lock)
3494 struct ceph_inode_info *ci = ceph_inode(inode);
3495 int mds = session->s_mds;
3496 int seq = le32_to_cpu(trunc->seq);
3497 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
3498 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
3499 u64 size = le64_to_cpu(trunc->size);
3500 int implemented = 0;
3501 int dirty = __ceph_caps_dirty(ci);
3502 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
3503 int queue_trunc = 0;
3505 issued |= implemented | dirty;
3507 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
3508 inode, mds, seq, truncate_size, truncate_seq);
3509 queue_trunc = ceph_fill_file_size(inode, issued,
3510 truncate_seq, truncate_size, size);
3511 spin_unlock(&ci->i_ceph_lock);
3514 ceph_queue_vmtruncate(inode);
3518 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
3519 * different one. If we are the most recent migration we've seen (as
3520 * indicated by mseq), make note of the migrating cap bits for the
3521 * duration (until we see the corresponding IMPORT).
3523 * caller holds s_mutex
3525 static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
3526 struct ceph_mds_cap_peer *ph,
3527 struct ceph_mds_session *session)
3529 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
3530 struct ceph_mds_session *tsession = NULL;
3531 struct ceph_cap *cap, *tcap, *new_cap = NULL;
3532 struct ceph_inode_info *ci = ceph_inode(inode);
3534 unsigned mseq = le32_to_cpu(ex->migrate_seq);
3535 unsigned t_seq, t_mseq;
3537 int mds = session->s_mds;
3540 t_cap_id = le64_to_cpu(ph->cap_id);
3541 t_seq = le32_to_cpu(ph->seq);
3542 t_mseq = le32_to_cpu(ph->mseq);
3543 target = le32_to_cpu(ph->mds);
3545 t_cap_id = t_seq = t_mseq = 0;
3549 dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n",
3550 inode, ci, mds, mseq, target);
3552 spin_lock(&ci->i_ceph_lock);
3553 cap = __get_cap_for_mds(ci, mds);
3554 if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
3558 if (cap->mds_wanted | cap->issued)
3559 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
3560 __ceph_remove_cap(cap, false);
3565 * now we know we haven't received the cap import message yet
3566 * because the exported cap still exist.
3569 issued = cap->issued;
3570 if (issued != cap->implemented)
3571 pr_err_ratelimited("handle_cap_export: issued != implemented: "
3572 "ino (%llx.%llx) mds%d seq %d mseq %d "
3573 "issued %s implemented %s\n",
3574 ceph_vinop(inode), mds, cap->seq, cap->mseq,
3575 ceph_cap_string(issued),
3576 ceph_cap_string(cap->implemented));
3579 tcap = __get_cap_for_mds(ci, target);
3581 /* already have caps from the target */
3582 if (tcap->cap_id == t_cap_id &&
3583 ceph_seq_cmp(tcap->seq, t_seq) < 0) {
3584 dout(" updating import cap %p mds%d\n", tcap, target);
3585 tcap->cap_id = t_cap_id;
3586 tcap->seq = t_seq - 1;
3587 tcap->issue_seq = t_seq - 1;
3588 tcap->issued |= issued;
3589 tcap->implemented |= issued;
3590 if (cap == ci->i_auth_cap)
3591 ci->i_auth_cap = tcap;
3593 if (!list_empty(&ci->i_cap_flush_list) &&
3594 ci->i_auth_cap == tcap) {
3595 spin_lock(&mdsc->cap_dirty_lock);
3596 list_move_tail(&ci->i_flushing_item,
3597 &tcap->session->s_cap_flushing);
3598 spin_unlock(&mdsc->cap_dirty_lock);
3601 __ceph_remove_cap(cap, false);
3603 } else if (tsession) {
3604 /* add placeholder for the export tagert */
3605 int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
3607 ceph_add_cap(inode, tsession, t_cap_id, -1, issued, 0,
3608 t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
3610 if (!list_empty(&ci->i_cap_flush_list) &&
3611 ci->i_auth_cap == tcap) {
3612 spin_lock(&mdsc->cap_dirty_lock);
3613 list_move_tail(&ci->i_flushing_item,
3614 &tcap->session->s_cap_flushing);
3615 spin_unlock(&mdsc->cap_dirty_lock);
3618 __ceph_remove_cap(cap, false);
3622 spin_unlock(&ci->i_ceph_lock);
3623 mutex_unlock(&session->s_mutex);
3625 /* open target session */
3626 tsession = ceph_mdsc_open_export_target_session(mdsc, target);
3627 if (!IS_ERR(tsession)) {
3629 mutex_lock(&session->s_mutex);
3630 mutex_lock_nested(&tsession->s_mutex,
3631 SINGLE_DEPTH_NESTING);
3633 mutex_lock(&tsession->s_mutex);
3634 mutex_lock_nested(&session->s_mutex,
3635 SINGLE_DEPTH_NESTING);
3637 new_cap = ceph_get_cap(mdsc, NULL);
3646 spin_unlock(&ci->i_ceph_lock);
3647 mutex_unlock(&session->s_mutex);
3649 mutex_unlock(&tsession->s_mutex);
3650 ceph_put_mds_session(tsession);
3653 ceph_put_cap(mdsc, new_cap);
3657 * Handle cap IMPORT.
3659 * caller holds s_mutex. acquires i_ceph_lock
3661 static void handle_cap_import(struct ceph_mds_client *mdsc,
3662 struct inode *inode, struct ceph_mds_caps *im,
3663 struct ceph_mds_cap_peer *ph,
3664 struct ceph_mds_session *session,
3665 struct ceph_cap **target_cap, int *old_issued)
3666 __acquires(ci->i_ceph_lock)
3668 struct ceph_inode_info *ci = ceph_inode(inode);
3669 struct ceph_cap *cap, *ocap, *new_cap = NULL;
3670 int mds = session->s_mds;
3672 unsigned caps = le32_to_cpu(im->caps);
3673 unsigned wanted = le32_to_cpu(im->wanted);
3674 unsigned seq = le32_to_cpu(im->seq);
3675 unsigned mseq = le32_to_cpu(im->migrate_seq);
3676 u64 realmino = le64_to_cpu(im->realm);
3677 u64 cap_id = le64_to_cpu(im->cap_id);
3682 p_cap_id = le64_to_cpu(ph->cap_id);
3683 peer = le32_to_cpu(ph->mds);
3689 dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n",
3690 inode, ci, mds, mseq, peer);
3693 spin_lock(&ci->i_ceph_lock);
3694 cap = __get_cap_for_mds(ci, mds);
3697 spin_unlock(&ci->i_ceph_lock);
3698 new_cap = ceph_get_cap(mdsc, NULL);
3704 ceph_put_cap(mdsc, new_cap);
3709 __ceph_caps_issued(ci, &issued);
3710 issued |= __ceph_caps_dirty(ci);
3712 ceph_add_cap(inode, session, cap_id, -1, caps, wanted, seq, mseq,
3713 realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
3715 ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
3716 if (ocap && ocap->cap_id == p_cap_id) {
3717 dout(" remove export cap %p mds%d flags %d\n",
3718 ocap, peer, ph->flags);
3719 if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
3720 (ocap->seq != le32_to_cpu(ph->seq) ||
3721 ocap->mseq != le32_to_cpu(ph->mseq))) {
3722 pr_err_ratelimited("handle_cap_import: "
3723 "mismatched seq/mseq: ino (%llx.%llx) "
3724 "mds%d seq %d mseq %d importer mds%d "
3725 "has peer seq %d mseq %d\n",
3726 ceph_vinop(inode), peer, ocap->seq,
3727 ocap->mseq, mds, le32_to_cpu(ph->seq),
3728 le32_to_cpu(ph->mseq));
3730 __ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
3733 /* make sure we re-request max_size, if necessary */
3734 ci->i_requested_max_size = 0;
3736 *old_issued = issued;
3741 * Handle a caps message from the MDS.
3743 * Identify the appropriate session, inode, and call the right handler
3744 * based on the cap op.
3746 void ceph_handle_caps(struct ceph_mds_session *session,
3747 struct ceph_msg *msg)
3749 struct ceph_mds_client *mdsc = session->s_mdsc;
3750 struct inode *inode;
3751 struct ceph_inode_info *ci;
3752 struct ceph_cap *cap;
3753 struct ceph_mds_caps *h;
3754 struct ceph_mds_cap_peer *peer = NULL;
3755 struct ceph_snap_realm *realm = NULL;
3757 int msg_version = le16_to_cpu(msg->hdr.version);
3759 struct ceph_vino vino;
3761 size_t snaptrace_len;
3763 struct cap_extra_info extra_info = {};
3765 dout("handle_caps from mds%d\n", session->s_mds);
3768 end = msg->front.iov_base + msg->front.iov_len;
3769 if (msg->front.iov_len < sizeof(*h))
3771 h = msg->front.iov_base;
3772 op = le32_to_cpu(h->op);
3773 vino.ino = le64_to_cpu(h->ino);
3774 vino.snap = CEPH_NOSNAP;
3775 seq = le32_to_cpu(h->seq);
3776 mseq = le32_to_cpu(h->migrate_seq);
3779 snaptrace_len = le32_to_cpu(h->snap_trace_len);
3780 p = snaptrace + snaptrace_len;
3782 if (msg_version >= 2) {
3784 ceph_decode_32_safe(&p, end, flock_len, bad);
3785 if (p + flock_len > end)
3790 if (msg_version >= 3) {
3791 if (op == CEPH_CAP_OP_IMPORT) {
3792 if (p + sizeof(*peer) > end)
3796 } else if (op == CEPH_CAP_OP_EXPORT) {
3797 /* recorded in unused fields */
3798 peer = (void *)&h->size;
3802 if (msg_version >= 4) {
3803 ceph_decode_64_safe(&p, end, extra_info.inline_version, bad);
3804 ceph_decode_32_safe(&p, end, extra_info.inline_len, bad);
3805 if (p + extra_info.inline_len > end)
3807 extra_info.inline_data = p;
3808 p += extra_info.inline_len;
3811 if (msg_version >= 5) {
3812 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
3815 ceph_decode_32_safe(&p, end, epoch_barrier, bad);
3816 ceph_osdc_update_epoch_barrier(osdc, epoch_barrier);
3819 if (msg_version >= 8) {
3821 u32 caller_uid, caller_gid;
3825 ceph_decode_64_safe(&p, end, flush_tid, bad);
3827 ceph_decode_32_safe(&p, end, caller_uid, bad);
3828 ceph_decode_32_safe(&p, end, caller_gid, bad);
3830 ceph_decode_32_safe(&p, end, pool_ns_len, bad);
3831 if (pool_ns_len > 0) {
3832 ceph_decode_need(&p, end, pool_ns_len, bad);
3833 extra_info.pool_ns =
3834 ceph_find_or_create_string(p, pool_ns_len);
3839 if (msg_version >= 11) {
3840 struct ceph_timespec *btime;
3845 if (p + sizeof(*btime) > end)
3848 p += sizeof(*btime);
3849 ceph_decode_64_safe(&p, end, change_attr, bad);
3851 ceph_decode_32_safe(&p, end, flags, bad);
3853 extra_info.dirstat_valid = true;
3854 ceph_decode_64_safe(&p, end, extra_info.nfiles, bad);
3855 ceph_decode_64_safe(&p, end, extra_info.nsubdirs, bad);
3859 inode = ceph_find_inode(mdsc->fsc->sb, vino);
3860 ci = ceph_inode(inode);
3861 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
3864 mutex_lock(&session->s_mutex);
3866 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
3870 dout(" i don't have ino %llx\n", vino.ino);
3872 if (op == CEPH_CAP_OP_IMPORT) {
3873 cap = ceph_get_cap(mdsc, NULL);
3874 cap->cap_ino = vino.ino;
3875 cap->queue_release = 1;
3876 cap->cap_id = le64_to_cpu(h->cap_id);
3879 cap->issue_seq = seq;
3880 spin_lock(&session->s_cap_lock);
3881 list_add_tail(&cap->session_caps,
3882 &session->s_cap_releases);
3883 session->s_num_cap_releases++;
3884 spin_unlock(&session->s_cap_lock);
3886 goto flush_cap_releases;
3889 /* these will work even if we don't have a cap yet */
3891 case CEPH_CAP_OP_FLUSHSNAP_ACK:
3892 handle_cap_flushsnap_ack(inode, le64_to_cpu(msg->hdr.tid),
3896 case CEPH_CAP_OP_EXPORT:
3897 handle_cap_export(inode, h, peer, session);
3900 case CEPH_CAP_OP_IMPORT:
3902 if (snaptrace_len) {
3903 down_write(&mdsc->snap_rwsem);
3904 ceph_update_snap_trace(mdsc, snaptrace,
3905 snaptrace + snaptrace_len,
3907 downgrade_write(&mdsc->snap_rwsem);
3909 down_read(&mdsc->snap_rwsem);
3911 handle_cap_import(mdsc, inode, h, peer, session,
3912 &cap, &extra_info.issued);
3913 handle_cap_grant(inode, session, cap,
3914 h, msg->middle, &extra_info);
3916 ceph_put_snap_realm(mdsc, realm);
3920 /* the rest require a cap */
3921 spin_lock(&ci->i_ceph_lock);
3922 cap = __get_cap_for_mds(ceph_inode(inode), session->s_mds);
3924 dout(" no cap on %p ino %llx.%llx from mds%d\n",
3925 inode, ceph_ino(inode), ceph_snap(inode),
3927 spin_unlock(&ci->i_ceph_lock);
3928 goto flush_cap_releases;
3931 /* note that each of these drops i_ceph_lock for us */
3933 case CEPH_CAP_OP_REVOKE:
3934 case CEPH_CAP_OP_GRANT:
3935 __ceph_caps_issued(ci, &extra_info.issued);
3936 extra_info.issued |= __ceph_caps_dirty(ci);
3937 handle_cap_grant(inode, session, cap,
3938 h, msg->middle, &extra_info);
3941 case CEPH_CAP_OP_FLUSH_ACK:
3942 handle_cap_flush_ack(inode, le64_to_cpu(msg->hdr.tid),
3946 case CEPH_CAP_OP_TRUNC:
3947 handle_cap_trunc(inode, h, session);
3951 spin_unlock(&ci->i_ceph_lock);
3952 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
3953 ceph_cap_op_name(op));
3960 * send any cap release message to try to move things
3961 * along for the mds (who clearly thinks we still have this
3964 ceph_send_cap_releases(mdsc, session);
3967 mutex_unlock(&session->s_mutex);
3970 ceph_put_string(extra_info.pool_ns);
3974 pr_err("ceph_handle_caps: corrupt message\n");
3980 * Delayed work handler to process end of delayed cap release LRU list.
3982 void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
3984 struct inode *inode;
3985 struct ceph_inode_info *ci;
3986 int flags = CHECK_CAPS_NODELAY;
3988 dout("check_delayed_caps\n");
3990 spin_lock(&mdsc->cap_delay_lock);
3991 if (list_empty(&mdsc->cap_delay_list))
3993 ci = list_first_entry(&mdsc->cap_delay_list,
3994 struct ceph_inode_info,
3996 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
3997 time_before(jiffies, ci->i_hold_caps_max))
3999 list_del_init(&ci->i_cap_delay_list);
4001 inode = igrab(&ci->vfs_inode);
4002 spin_unlock(&mdsc->cap_delay_lock);
4005 dout("check_delayed_caps on %p\n", inode);
4006 ceph_check_caps(ci, flags, NULL);
4010 spin_unlock(&mdsc->cap_delay_lock);
4014 * Flush all dirty caps to the mds
4016 void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
4018 struct ceph_inode_info *ci;
4019 struct inode *inode;
4021 dout("flush_dirty_caps\n");
4022 spin_lock(&mdsc->cap_dirty_lock);
4023 while (!list_empty(&mdsc->cap_dirty)) {
4024 ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
4026 inode = &ci->vfs_inode;
4028 dout("flush_dirty_caps %p\n", inode);
4029 spin_unlock(&mdsc->cap_dirty_lock);
4030 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
4032 spin_lock(&mdsc->cap_dirty_lock);
4034 spin_unlock(&mdsc->cap_dirty_lock);
4035 dout("flush_dirty_caps done\n");
4038 void __ceph_get_fmode(struct ceph_inode_info *ci, int fmode)
4041 int bits = (fmode << 1) | 1;
4042 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
4043 if (bits & (1 << i))
4044 ci->i_nr_by_mode[i]++;
4049 * Drop open file reference. If we were the last open file,
4050 * we may need to release capabilities to the MDS (or schedule
4051 * their delayed release).
4053 void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
4056 int bits = (fmode << 1) | 1;
4057 spin_lock(&ci->i_ceph_lock);
4058 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
4059 if (bits & (1 << i)) {
4060 BUG_ON(ci->i_nr_by_mode[i] == 0);
4061 if (--ci->i_nr_by_mode[i] == 0)
4065 dout("put_fmode %p fmode %d {%d,%d,%d,%d}\n",
4066 &ci->vfs_inode, fmode,
4067 ci->i_nr_by_mode[0], ci->i_nr_by_mode[1],
4068 ci->i_nr_by_mode[2], ci->i_nr_by_mode[3]);
4069 spin_unlock(&ci->i_ceph_lock);
4071 if (last && ci->i_vino.snap == CEPH_NOSNAP)
4072 ceph_check_caps(ci, 0, NULL);
4076 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
4077 * looks like the link count will hit 0, drop any other caps (other
4078 * than PIN) we don't specifically want (due to the file still being
4081 int ceph_drop_caps_for_unlink(struct inode *inode)
4083 struct ceph_inode_info *ci = ceph_inode(inode);
4084 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
4086 spin_lock(&ci->i_ceph_lock);
4087 if (inode->i_nlink == 1) {
4088 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
4090 ci->i_ceph_flags |= CEPH_I_NODELAY;
4091 if (__ceph_caps_dirty(ci)) {
4092 struct ceph_mds_client *mdsc =
4093 ceph_inode_to_client(inode)->mdsc;
4094 __cap_delay_requeue_front(mdsc, ci);
4097 spin_unlock(&ci->i_ceph_lock);
4102 * Helpers for embedding cap and dentry lease releases into mds
4105 * @force is used by dentry_release (below) to force inclusion of a
4106 * record for the directory inode, even when there aren't any caps to
4109 int ceph_encode_inode_release(void **p, struct inode *inode,
4110 int mds, int drop, int unless, int force)
4112 struct ceph_inode_info *ci = ceph_inode(inode);
4113 struct ceph_cap *cap;
4114 struct ceph_mds_request_release *rel = *p;
4118 spin_lock(&ci->i_ceph_lock);
4119 used = __ceph_caps_used(ci);
4120 dirty = __ceph_caps_dirty(ci);
4122 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
4123 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
4124 ceph_cap_string(unless));
4126 /* only drop unused, clean caps */
4127 drop &= ~(used | dirty);
4129 cap = __get_cap_for_mds(ci, mds);
4130 if (cap && __cap_is_valid(cap)) {
4131 unless &= cap->issued;
4133 if (unless & CEPH_CAP_AUTH_EXCL)
4134 drop &= ~CEPH_CAP_AUTH_SHARED;
4135 if (unless & CEPH_CAP_LINK_EXCL)
4136 drop &= ~CEPH_CAP_LINK_SHARED;
4137 if (unless & CEPH_CAP_XATTR_EXCL)
4138 drop &= ~CEPH_CAP_XATTR_SHARED;
4139 if (unless & CEPH_CAP_FILE_EXCL)
4140 drop &= ~CEPH_CAP_FILE_SHARED;
4143 if (force || (cap->issued & drop)) {
4144 if (cap->issued & drop) {
4145 int wanted = __ceph_caps_wanted(ci);
4146 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0)
4147 wanted |= cap->mds_wanted;
4148 dout("encode_inode_release %p cap %p "
4149 "%s -> %s, wanted %s -> %s\n", inode, cap,
4150 ceph_cap_string(cap->issued),
4151 ceph_cap_string(cap->issued & ~drop),
4152 ceph_cap_string(cap->mds_wanted),
4153 ceph_cap_string(wanted));
4155 cap->issued &= ~drop;
4156 cap->implemented &= ~drop;
4157 cap->mds_wanted = wanted;
4159 dout("encode_inode_release %p cap %p %s"
4160 " (force)\n", inode, cap,
4161 ceph_cap_string(cap->issued));
4164 rel->ino = cpu_to_le64(ceph_ino(inode));
4165 rel->cap_id = cpu_to_le64(cap->cap_id);
4166 rel->seq = cpu_to_le32(cap->seq);
4167 rel->issue_seq = cpu_to_le32(cap->issue_seq);
4168 rel->mseq = cpu_to_le32(cap->mseq);
4169 rel->caps = cpu_to_le32(cap->implemented);
4170 rel->wanted = cpu_to_le32(cap->mds_wanted);
4176 dout("encode_inode_release %p cap %p %s (noop)\n",
4177 inode, cap, ceph_cap_string(cap->issued));
4180 spin_unlock(&ci->i_ceph_lock);
4184 int ceph_encode_dentry_release(void **p, struct dentry *dentry,
4186 int mds, int drop, int unless)
4188 struct dentry *parent = NULL;
4189 struct ceph_mds_request_release *rel = *p;
4190 struct ceph_dentry_info *di = ceph_dentry(dentry);
4195 * force an record for the directory caps if we have a dentry lease.
4196 * this is racy (can't take i_ceph_lock and d_lock together), but it
4197 * doesn't have to be perfect; the mds will revoke anything we don't
4200 spin_lock(&dentry->d_lock);
4201 if (di->lease_session && di->lease_session->s_mds == mds)
4204 parent = dget(dentry->d_parent);
4205 dir = d_inode(parent);
4207 spin_unlock(&dentry->d_lock);
4209 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
4212 spin_lock(&dentry->d_lock);
4213 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
4214 dout("encode_dentry_release %p mds%d seq %d\n",
4215 dentry, mds, (int)di->lease_seq);
4216 rel->dname_len = cpu_to_le32(dentry->d_name.len);
4217 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
4218 *p += dentry->d_name.len;
4219 rel->dname_seq = cpu_to_le32(di->lease_seq);
4220 __ceph_mdsc_drop_dentry_lease(dentry);
4222 spin_unlock(&dentry->d_lock);