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)
524 __cap_set_timeouts(mdsc, 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);
534 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
536 spin_unlock(&mdsc->cap_delay_lock);
541 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
542 * indicating we should send a cap message to flush dirty metadata
543 * asap, and move to the front of the delayed cap list.
545 static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
546 struct ceph_inode_info *ci)
548 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
549 spin_lock(&mdsc->cap_delay_lock);
550 ci->i_ceph_flags |= CEPH_I_FLUSH;
551 if (!list_empty(&ci->i_cap_delay_list))
552 list_del_init(&ci->i_cap_delay_list);
553 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
554 spin_unlock(&mdsc->cap_delay_lock);
558 * Cancel delayed work on cap.
560 * Caller must hold i_ceph_lock.
562 static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
563 struct ceph_inode_info *ci)
565 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
566 if (list_empty(&ci->i_cap_delay_list))
568 spin_lock(&mdsc->cap_delay_lock);
569 list_del_init(&ci->i_cap_delay_list);
570 spin_unlock(&mdsc->cap_delay_lock);
574 * Common issue checks for add_cap, handle_cap_grant.
576 static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
579 unsigned had = __ceph_caps_issued(ci, NULL);
582 * Each time we receive FILE_CACHE anew, we increment
585 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
586 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) {
591 * If FILE_SHARED is newly issued, mark dir not complete. We don't
592 * know what happened to this directory while we didn't have the cap.
593 * If FILE_SHARED is being revoked, also mark dir not complete. It
594 * stops on-going cached readdir.
596 if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) {
597 if (issued & CEPH_CAP_FILE_SHARED)
598 atomic_inc(&ci->i_shared_gen);
599 if (S_ISDIR(ci->vfs_inode.i_mode)) {
600 dout(" marking %p NOT complete\n", &ci->vfs_inode);
601 __ceph_dir_clear_complete(ci);
607 * Add a capability under the given MDS session.
609 * Caller should hold session snap_rwsem (read) and s_mutex.
611 * @fmode is the open file mode, if we are opening a file, otherwise
612 * it is < 0. (This is so we can atomically add the cap and add an
613 * open file reference to it.)
615 void ceph_add_cap(struct inode *inode,
616 struct ceph_mds_session *session, u64 cap_id,
617 int fmode, unsigned issued, unsigned wanted,
618 unsigned seq, unsigned mseq, u64 realmino, int flags,
619 struct ceph_cap **new_cap)
621 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
622 struct ceph_inode_info *ci = ceph_inode(inode);
623 struct ceph_cap *cap;
624 int mds = session->s_mds;
627 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
628 session->s_mds, cap_id, ceph_cap_string(issued), seq);
631 * If we are opening the file, include file mode wanted bits
635 wanted |= ceph_caps_for_mode(fmode);
637 cap = __get_cap_for_mds(ci, mds);
643 cap->implemented = 0;
649 __insert_cap_node(ci, cap);
651 /* add to session cap list */
652 cap->session = session;
653 spin_lock(&session->s_cap_lock);
654 list_add_tail(&cap->session_caps, &session->s_caps);
655 session->s_nr_caps++;
656 spin_unlock(&session->s_cap_lock);
659 * auth mds of the inode changed. we received the cap export
660 * message, but still haven't received the cap import message.
661 * handle_cap_export() updated the new auth MDS' cap.
663 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
664 * a message that was send before the cap import message. So
667 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
668 WARN_ON(cap != ci->i_auth_cap);
669 WARN_ON(cap->cap_id != cap_id);
672 issued |= cap->issued;
673 flags |= CEPH_CAP_FLAG_AUTH;
677 if (!ci->i_snap_realm ||
678 ((flags & CEPH_CAP_FLAG_AUTH) &&
679 realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) {
681 * add this inode to the appropriate snap realm
683 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
686 struct ceph_snap_realm *oldrealm = ci->i_snap_realm;
688 spin_lock(&oldrealm->inodes_with_caps_lock);
689 list_del_init(&ci->i_snap_realm_item);
690 spin_unlock(&oldrealm->inodes_with_caps_lock);
693 spin_lock(&realm->inodes_with_caps_lock);
694 list_add(&ci->i_snap_realm_item,
695 &realm->inodes_with_caps);
696 ci->i_snap_realm = realm;
697 if (realm->ino == ci->i_vino.ino)
698 realm->inode = inode;
699 spin_unlock(&realm->inodes_with_caps_lock);
702 ceph_put_snap_realm(mdsc, oldrealm);
704 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
710 __check_cap_issue(ci, cap, issued);
713 * If we are issued caps we don't want, or the mds' wanted
714 * value appears to be off, queue a check so we'll release
715 * later and/or update the mds wanted value.
717 actual_wanted = __ceph_caps_wanted(ci);
718 if ((wanted & ~actual_wanted) ||
719 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
720 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
721 ceph_cap_string(issued), ceph_cap_string(wanted),
722 ceph_cap_string(actual_wanted));
723 __cap_delay_requeue(mdsc, ci);
726 if (flags & CEPH_CAP_FLAG_AUTH) {
727 if (!ci->i_auth_cap ||
728 ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
729 ci->i_auth_cap = cap;
730 cap->mds_wanted = wanted;
733 WARN_ON(ci->i_auth_cap == cap);
736 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
737 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
738 ceph_cap_string(issued|cap->issued), seq, mds);
739 cap->cap_id = cap_id;
740 cap->issued = issued;
741 cap->implemented |= issued;
742 if (ceph_seq_cmp(mseq, cap->mseq) > 0)
743 cap->mds_wanted = wanted;
745 cap->mds_wanted |= wanted;
747 cap->issue_seq = seq;
749 cap->cap_gen = session->s_cap_gen;
752 __ceph_get_fmode(ci, fmode);
756 * Return true if cap has not timed out and belongs to the current
757 * generation of the MDS session (i.e. has not gone 'stale' due to
758 * us losing touch with the mds).
760 static int __cap_is_valid(struct ceph_cap *cap)
765 spin_lock(&cap->session->s_gen_ttl_lock);
766 gen = cap->session->s_cap_gen;
767 ttl = cap->session->s_cap_ttl;
768 spin_unlock(&cap->session->s_gen_ttl_lock);
770 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
771 dout("__cap_is_valid %p cap %p issued %s "
772 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
773 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
781 * Return set of valid cap bits issued to us. Note that caps time
782 * out, and may be invalidated in bulk if the client session times out
783 * and session->s_cap_gen is bumped.
785 int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
787 int have = ci->i_snap_caps;
788 struct ceph_cap *cap;
793 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
794 cap = rb_entry(p, struct ceph_cap, ci_node);
795 if (!__cap_is_valid(cap))
797 dout("__ceph_caps_issued %p cap %p issued %s\n",
798 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
801 *implemented |= cap->implemented;
804 * exclude caps issued by non-auth MDS, but are been revoking
805 * by the auth MDS. The non-auth MDS should be revoking/exporting
806 * these caps, but the message is delayed.
808 if (ci->i_auth_cap) {
809 cap = ci->i_auth_cap;
810 have &= ~cap->implemented | cap->issued;
816 * Get cap bits issued by caps other than @ocap
818 int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
820 int have = ci->i_snap_caps;
821 struct ceph_cap *cap;
824 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
825 cap = rb_entry(p, struct ceph_cap, ci_node);
828 if (!__cap_is_valid(cap))
836 * Move a cap to the end of the LRU (oldest caps at list head, newest
839 static void __touch_cap(struct ceph_cap *cap)
841 struct ceph_mds_session *s = cap->session;
843 spin_lock(&s->s_cap_lock);
844 if (!s->s_cap_iterator) {
845 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
847 list_move_tail(&cap->session_caps, &s->s_caps);
849 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
850 &cap->ci->vfs_inode, cap, s->s_mds);
852 spin_unlock(&s->s_cap_lock);
856 * Check if we hold the given mask. If so, move the cap(s) to the
857 * front of their respective LRUs. (This is the preferred way for
858 * callers to check for caps they want.)
860 int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
862 struct ceph_cap *cap;
864 int have = ci->i_snap_caps;
866 if ((have & mask) == mask) {
867 dout("__ceph_caps_issued_mask %p snap issued %s"
868 " (mask %s)\n", &ci->vfs_inode,
869 ceph_cap_string(have),
870 ceph_cap_string(mask));
874 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
875 cap = rb_entry(p, struct ceph_cap, ci_node);
876 if (!__cap_is_valid(cap))
878 if ((cap->issued & mask) == mask) {
879 dout("__ceph_caps_issued_mask %p cap %p issued %s"
880 " (mask %s)\n", &ci->vfs_inode, cap,
881 ceph_cap_string(cap->issued),
882 ceph_cap_string(mask));
888 /* does a combination of caps satisfy mask? */
890 if ((have & mask) == mask) {
891 dout("__ceph_caps_issued_mask %p combo issued %s"
892 " (mask %s)\n", &ci->vfs_inode,
893 ceph_cap_string(cap->issued),
894 ceph_cap_string(mask));
898 /* touch this + preceding caps */
900 for (q = rb_first(&ci->i_caps); q != p;
902 cap = rb_entry(q, struct ceph_cap,
904 if (!__cap_is_valid(cap))
917 * Return true if mask caps are currently being revoked by an MDS.
919 int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
920 struct ceph_cap *ocap, int mask)
922 struct ceph_cap *cap;
925 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
926 cap = rb_entry(p, struct ceph_cap, ci_node);
928 (cap->implemented & ~cap->issued & mask))
934 int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
936 struct inode *inode = &ci->vfs_inode;
939 spin_lock(&ci->i_ceph_lock);
940 ret = __ceph_caps_revoking_other(ci, NULL, mask);
941 spin_unlock(&ci->i_ceph_lock);
942 dout("ceph_caps_revoking %p %s = %d\n", inode,
943 ceph_cap_string(mask), ret);
947 int __ceph_caps_used(struct ceph_inode_info *ci)
951 used |= CEPH_CAP_PIN;
953 used |= CEPH_CAP_FILE_RD;
954 if (ci->i_rdcache_ref ||
955 (!S_ISDIR(ci->vfs_inode.i_mode) && /* ignore readdir cache */
956 ci->vfs_inode.i_data.nrpages))
957 used |= CEPH_CAP_FILE_CACHE;
959 used |= CEPH_CAP_FILE_WR;
960 if (ci->i_wb_ref || ci->i_wrbuffer_ref)
961 used |= CEPH_CAP_FILE_BUFFER;
966 * wanted, by virtue of open file modes
968 int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
971 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
972 if (ci->i_nr_by_mode[i])
977 return ceph_caps_for_mode(bits >> 1);
981 * Return caps we have registered with the MDS(s) as 'wanted'.
983 int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check)
985 struct ceph_cap *cap;
989 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
990 cap = rb_entry(p, struct ceph_cap, ci_node);
991 if (check && !__cap_is_valid(cap))
993 if (cap == ci->i_auth_cap)
994 mds_wanted |= cap->mds_wanted;
996 mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
1002 * called under i_ceph_lock
1004 static int __ceph_is_single_caps(struct ceph_inode_info *ci)
1006 return rb_first(&ci->i_caps) == rb_last(&ci->i_caps);
1009 static int __ceph_is_any_caps(struct ceph_inode_info *ci)
1011 return !RB_EMPTY_ROOT(&ci->i_caps);
1014 int ceph_is_any_caps(struct inode *inode)
1016 struct ceph_inode_info *ci = ceph_inode(inode);
1019 spin_lock(&ci->i_ceph_lock);
1020 ret = __ceph_is_any_caps(ci);
1021 spin_unlock(&ci->i_ceph_lock);
1026 static void drop_inode_snap_realm(struct ceph_inode_info *ci)
1028 struct ceph_snap_realm *realm = ci->i_snap_realm;
1029 spin_lock(&realm->inodes_with_caps_lock);
1030 list_del_init(&ci->i_snap_realm_item);
1031 ci->i_snap_realm_counter++;
1032 ci->i_snap_realm = NULL;
1033 if (realm->ino == ci->i_vino.ino)
1034 realm->inode = NULL;
1035 spin_unlock(&realm->inodes_with_caps_lock);
1036 ceph_put_snap_realm(ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc,
1041 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
1043 * caller should hold i_ceph_lock.
1044 * caller will not hold session s_mutex if called from destroy_inode.
1046 void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
1048 struct ceph_mds_session *session = cap->session;
1049 struct ceph_inode_info *ci = cap->ci;
1050 struct ceph_mds_client *mdsc;
1053 /* 'ci' being NULL means the remove have already occurred */
1055 dout("%s: cap inode is NULL\n", __func__);
1059 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
1061 mdsc = ceph_inode_to_client(&ci->vfs_inode)->mdsc;
1063 /* remove from inode's cap rbtree, and clear auth cap */
1064 rb_erase(&cap->ci_node, &ci->i_caps);
1065 if (ci->i_auth_cap == cap)
1066 ci->i_auth_cap = NULL;
1068 /* remove from session list */
1069 spin_lock(&session->s_cap_lock);
1070 if (session->s_cap_iterator == cap) {
1071 /* not yet, we are iterating over this very cap */
1072 dout("__ceph_remove_cap delaying %p removal from session %p\n",
1075 list_del_init(&cap->session_caps);
1076 session->s_nr_caps--;
1077 cap->session = NULL;
1080 /* protect backpointer with s_cap_lock: see iterate_session_caps */
1084 * s_cap_reconnect is protected by s_cap_lock. no one changes
1085 * s_cap_gen while session is in the reconnect state.
1087 if (queue_release &&
1088 (!session->s_cap_reconnect || cap->cap_gen == session->s_cap_gen)) {
1089 cap->queue_release = 1;
1091 list_add_tail(&cap->session_caps,
1092 &session->s_cap_releases);
1093 session->s_num_cap_releases++;
1097 cap->queue_release = 0;
1099 cap->cap_ino = ci->i_vino.ino;
1101 spin_unlock(&session->s_cap_lock);
1104 ceph_put_cap(mdsc, cap);
1106 /* when reconnect denied, we remove session caps forcibly,
1107 * i_wr_ref can be non-zero. If there are ongoing write,
1108 * keep i_snap_realm.
1110 if (!__ceph_is_any_caps(ci) && ci->i_wr_ref == 0 && ci->i_snap_realm)
1111 drop_inode_snap_realm(ci);
1113 if (!__ceph_is_any_real_caps(ci))
1114 __cap_delay_cancel(mdsc, ci);
1117 struct cap_msg_args {
1118 struct ceph_mds_session *session;
1119 u64 ino, cid, follows;
1120 u64 flush_tid, oldest_flush_tid, size, max_size;
1122 struct ceph_buffer *xattr_buf;
1123 struct timespec64 atime, mtime, ctime;
1124 int op, caps, wanted, dirty;
1125 u32 seq, issue_seq, mseq, time_warp_seq;
1134 * Build and send a cap message to the given MDS.
1136 * Caller should be holding s_mutex.
1138 static int send_cap_msg(struct cap_msg_args *arg)
1140 struct ceph_mds_caps *fc;
1141 struct ceph_msg *msg;
1144 struct timespec64 zerotime = {0};
1145 struct ceph_osd_client *osdc = &arg->session->s_mdsc->fsc->client->osdc;
1147 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
1148 " seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu"
1149 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(arg->op),
1150 arg->cid, arg->ino, ceph_cap_string(arg->caps),
1151 ceph_cap_string(arg->wanted), ceph_cap_string(arg->dirty),
1152 arg->seq, arg->issue_seq, arg->flush_tid, arg->oldest_flush_tid,
1153 arg->mseq, arg->follows, arg->size, arg->max_size,
1155 arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0);
1157 /* flock buffer size + inline version + inline data size +
1158 * osd_epoch_barrier + oldest_flush_tid */
1159 extra_len = 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4;
1160 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc) + extra_len,
1165 msg->hdr.version = cpu_to_le16(10);
1166 msg->hdr.tid = cpu_to_le64(arg->flush_tid);
1168 fc = msg->front.iov_base;
1169 memset(fc, 0, sizeof(*fc));
1171 fc->cap_id = cpu_to_le64(arg->cid);
1172 fc->op = cpu_to_le32(arg->op);
1173 fc->seq = cpu_to_le32(arg->seq);
1174 fc->issue_seq = cpu_to_le32(arg->issue_seq);
1175 fc->migrate_seq = cpu_to_le32(arg->mseq);
1176 fc->caps = cpu_to_le32(arg->caps);
1177 fc->wanted = cpu_to_le32(arg->wanted);
1178 fc->dirty = cpu_to_le32(arg->dirty);
1179 fc->ino = cpu_to_le64(arg->ino);
1180 fc->snap_follows = cpu_to_le64(arg->follows);
1182 fc->size = cpu_to_le64(arg->size);
1183 fc->max_size = cpu_to_le64(arg->max_size);
1184 ceph_encode_timespec64(&fc->mtime, &arg->mtime);
1185 ceph_encode_timespec64(&fc->atime, &arg->atime);
1186 ceph_encode_timespec64(&fc->ctime, &arg->ctime);
1187 fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq);
1189 fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid));
1190 fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid));
1191 fc->mode = cpu_to_le32(arg->mode);
1193 fc->xattr_version = cpu_to_le64(arg->xattr_version);
1194 if (arg->xattr_buf) {
1195 msg->middle = ceph_buffer_get(arg->xattr_buf);
1196 fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1197 msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1201 /* flock buffer size (version 2) */
1202 ceph_encode_32(&p, 0);
1203 /* inline version (version 4) */
1204 ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE);
1205 /* inline data size */
1206 ceph_encode_32(&p, 0);
1208 * osd_epoch_barrier (version 5)
1209 * The epoch_barrier is protected osdc->lock, so READ_ONCE here in
1210 * case it was recently changed
1212 ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier));
1213 /* oldest_flush_tid (version 6) */
1214 ceph_encode_64(&p, arg->oldest_flush_tid);
1217 * caller_uid/caller_gid (version 7)
1219 * Currently, we don't properly track which caller dirtied the caps
1220 * last, and force a flush of them when there is a conflict. For now,
1221 * just set this to 0:0, to emulate how the MDS has worked up to now.
1223 ceph_encode_32(&p, 0);
1224 ceph_encode_32(&p, 0);
1226 /* pool namespace (version 8) (mds always ignores this) */
1227 ceph_encode_32(&p, 0);
1230 * btime and change_attr (version 9)
1232 * We just zero these out for now, as the MDS ignores them unless
1233 * the requisite feature flags are set (which we don't do yet).
1235 ceph_encode_timespec64(p, &zerotime);
1236 p += sizeof(struct ceph_timespec);
1237 ceph_encode_64(&p, 0);
1239 /* Advisory flags (version 10) */
1240 ceph_encode_32(&p, arg->flags);
1242 ceph_con_send(&arg->session->s_con, msg);
1247 * Queue cap releases when an inode is dropped from our cache.
1249 void ceph_queue_caps_release(struct inode *inode)
1251 struct ceph_inode_info *ci = ceph_inode(inode);
1254 /* lock i_ceph_lock, because ceph_d_revalidate(..., LOOKUP_RCU)
1255 * may call __ceph_caps_issued_mask() on a freeing inode. */
1256 spin_lock(&ci->i_ceph_lock);
1257 p = rb_first(&ci->i_caps);
1259 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1261 __ceph_remove_cap(cap, true);
1263 spin_unlock(&ci->i_ceph_lock);
1267 * Send a cap msg on the given inode. Update our caps state, then
1268 * drop i_ceph_lock and send the message.
1270 * Make note of max_size reported/requested from mds, revoked caps
1271 * that have now been implemented.
1273 * Make half-hearted attempt ot to invalidate page cache if we are
1274 * dropping RDCACHE. Note that this will leave behind locked pages
1275 * that we'll then need to deal with elsewhere.
1277 * Return non-zero if delayed release, or we experienced an error
1278 * such that the caller should requeue + retry later.
1280 * called with i_ceph_lock, then drops it.
1281 * caller should hold snap_rwsem (read), s_mutex.
1283 static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1284 int op, bool sync, int used, int want, int retain,
1285 int flushing, u64 flush_tid, u64 oldest_flush_tid)
1286 __releases(cap->ci->i_ceph_lock)
1288 struct ceph_inode_info *ci = cap->ci;
1289 struct inode *inode = &ci->vfs_inode;
1290 struct ceph_buffer *old_blob = NULL;
1291 struct cap_msg_args arg;
1297 held = cap->issued | cap->implemented;
1298 revoking = cap->implemented & ~cap->issued;
1299 retain &= ~revoking;
1301 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1302 inode, cap, cap->session,
1303 ceph_cap_string(held), ceph_cap_string(held & retain),
1304 ceph_cap_string(revoking));
1305 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1307 arg.session = cap->session;
1309 /* don't release wanted unless we've waited a bit. */
1310 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1311 time_before(jiffies, ci->i_hold_caps_min)) {
1312 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1313 ceph_cap_string(cap->issued),
1314 ceph_cap_string(cap->issued & retain),
1315 ceph_cap_string(cap->mds_wanted),
1316 ceph_cap_string(want));
1317 want |= cap->mds_wanted;
1318 retain |= cap->issued;
1321 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1322 if (want & ~cap->mds_wanted) {
1323 /* user space may open/close single file frequently.
1324 * This avoids droping mds_wanted immediately after
1325 * requesting new mds_wanted.
1327 __cap_set_timeouts(mdsc, ci);
1330 cap->issued &= retain; /* drop bits we don't want */
1331 if (cap->implemented & ~cap->issued) {
1333 * Wake up any waiters on wanted -> needed transition.
1334 * This is due to the weird transition from buffered
1335 * to sync IO... we need to flush dirty pages _before_
1336 * allowing sync writes to avoid reordering.
1340 cap->implemented &= cap->issued | used;
1341 cap->mds_wanted = want;
1343 arg.ino = ceph_vino(inode).ino;
1344 arg.cid = cap->cap_id;
1345 arg.follows = flushing ? ci->i_head_snapc->seq : 0;
1346 arg.flush_tid = flush_tid;
1347 arg.oldest_flush_tid = oldest_flush_tid;
1349 arg.size = inode->i_size;
1350 ci->i_reported_size = arg.size;
1351 arg.max_size = ci->i_wanted_max_size;
1352 ci->i_requested_max_size = arg.max_size;
1354 if (flushing & CEPH_CAP_XATTR_EXCL) {
1355 old_blob = __ceph_build_xattrs_blob(ci);
1356 arg.xattr_version = ci->i_xattrs.version;
1357 arg.xattr_buf = ci->i_xattrs.blob;
1359 arg.xattr_buf = NULL;
1362 arg.mtime = inode->i_mtime;
1363 arg.atime = inode->i_atime;
1364 arg.ctime = inode->i_ctime;
1367 arg.caps = cap->implemented;
1369 arg.dirty = flushing;
1372 arg.issue_seq = cap->issue_seq;
1373 arg.mseq = cap->mseq;
1374 arg.time_warp_seq = ci->i_time_warp_seq;
1376 arg.uid = inode->i_uid;
1377 arg.gid = inode->i_gid;
1378 arg.mode = inode->i_mode;
1380 arg.inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
1381 if (list_empty(&ci->i_cap_snaps))
1382 arg.flags = CEPH_CLIENT_CAPS_NO_CAPSNAP;
1384 arg.flags = CEPH_CLIENT_CAPS_PENDING_CAPSNAP;
1386 arg.flags |= CEPH_CLIENT_CAPS_SYNC;
1388 spin_unlock(&ci->i_ceph_lock);
1390 ceph_buffer_put(old_blob);
1392 ret = send_cap_msg(&arg);
1394 dout("error sending cap msg, must requeue %p\n", inode);
1399 wake_up_all(&ci->i_cap_wq);
1404 static inline int __send_flush_snap(struct inode *inode,
1405 struct ceph_mds_session *session,
1406 struct ceph_cap_snap *capsnap,
1407 u32 mseq, u64 oldest_flush_tid)
1409 struct cap_msg_args arg;
1411 arg.session = session;
1412 arg.ino = ceph_vino(inode).ino;
1414 arg.follows = capsnap->follows;
1415 arg.flush_tid = capsnap->cap_flush.tid;
1416 arg.oldest_flush_tid = oldest_flush_tid;
1418 arg.size = capsnap->size;
1420 arg.xattr_version = capsnap->xattr_version;
1421 arg.xattr_buf = capsnap->xattr_blob;
1423 arg.atime = capsnap->atime;
1424 arg.mtime = capsnap->mtime;
1425 arg.ctime = capsnap->ctime;
1427 arg.op = CEPH_CAP_OP_FLUSHSNAP;
1428 arg.caps = capsnap->issued;
1430 arg.dirty = capsnap->dirty;
1435 arg.time_warp_seq = capsnap->time_warp_seq;
1437 arg.uid = capsnap->uid;
1438 arg.gid = capsnap->gid;
1439 arg.mode = capsnap->mode;
1441 arg.inline_data = capsnap->inline_data;
1444 return send_cap_msg(&arg);
1448 * When a snapshot is taken, clients accumulate dirty metadata on
1449 * inodes with capabilities in ceph_cap_snaps to describe the file
1450 * state at the time the snapshot was taken. This must be flushed
1451 * asynchronously back to the MDS once sync writes complete and dirty
1452 * data is written out.
1454 * Called under i_ceph_lock. Takes s_mutex as needed.
1456 static void __ceph_flush_snaps(struct ceph_inode_info *ci,
1457 struct ceph_mds_session *session)
1458 __releases(ci->i_ceph_lock)
1459 __acquires(ci->i_ceph_lock)
1461 struct inode *inode = &ci->vfs_inode;
1462 struct ceph_mds_client *mdsc = session->s_mdsc;
1463 struct ceph_cap_snap *capsnap;
1464 u64 oldest_flush_tid = 0;
1465 u64 first_tid = 1, last_tid = 0;
1467 dout("__flush_snaps %p session %p\n", inode, session);
1469 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1471 * we need to wait for sync writes to complete and for dirty
1472 * pages to be written out.
1474 if (capsnap->dirty_pages || capsnap->writing)
1477 /* should be removed by ceph_try_drop_cap_snap() */
1478 BUG_ON(!capsnap->need_flush);
1480 /* only flush each capsnap once */
1481 if (capsnap->cap_flush.tid > 0) {
1482 dout(" already flushed %p, skipping\n", capsnap);
1486 spin_lock(&mdsc->cap_dirty_lock);
1487 capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
1488 list_add_tail(&capsnap->cap_flush.g_list,
1489 &mdsc->cap_flush_list);
1490 if (oldest_flush_tid == 0)
1491 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1492 if (list_empty(&ci->i_flushing_item)) {
1493 list_add_tail(&ci->i_flushing_item,
1494 &session->s_cap_flushing);
1496 spin_unlock(&mdsc->cap_dirty_lock);
1498 list_add_tail(&capsnap->cap_flush.i_list,
1499 &ci->i_cap_flush_list);
1502 first_tid = capsnap->cap_flush.tid;
1503 last_tid = capsnap->cap_flush.tid;
1506 ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS;
1508 while (first_tid <= last_tid) {
1509 struct ceph_cap *cap = ci->i_auth_cap;
1510 struct ceph_cap_flush *cf;
1513 if (!(cap && cap->session == session)) {
1514 dout("__flush_snaps %p auth cap %p not mds%d, "
1515 "stop\n", inode, cap, session->s_mds);
1520 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
1521 if (cf->tid >= first_tid) {
1529 first_tid = cf->tid + 1;
1531 capsnap = container_of(cf, struct ceph_cap_snap, cap_flush);
1532 refcount_inc(&capsnap->nref);
1533 spin_unlock(&ci->i_ceph_lock);
1535 dout("__flush_snaps %p capsnap %p tid %llu %s\n",
1536 inode, capsnap, cf->tid, ceph_cap_string(capsnap->dirty));
1538 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
1541 pr_err("__flush_snaps: error sending cap flushsnap, "
1542 "ino (%llx.%llx) tid %llu follows %llu\n",
1543 ceph_vinop(inode), cf->tid, capsnap->follows);
1546 ceph_put_cap_snap(capsnap);
1547 spin_lock(&ci->i_ceph_lock);
1551 void ceph_flush_snaps(struct ceph_inode_info *ci,
1552 struct ceph_mds_session **psession)
1554 struct inode *inode = &ci->vfs_inode;
1555 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
1556 struct ceph_mds_session *session = NULL;
1559 dout("ceph_flush_snaps %p\n", inode);
1561 session = *psession;
1563 spin_lock(&ci->i_ceph_lock);
1564 if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) {
1565 dout(" no capsnap needs flush, doing nothing\n");
1568 if (!ci->i_auth_cap) {
1569 dout(" no auth cap (migrating?), doing nothing\n");
1573 mds = ci->i_auth_cap->session->s_mds;
1574 if (session && session->s_mds != mds) {
1575 dout(" oops, wrong session %p mutex\n", session);
1576 mutex_unlock(&session->s_mutex);
1577 ceph_put_mds_session(session);
1581 spin_unlock(&ci->i_ceph_lock);
1582 mutex_lock(&mdsc->mutex);
1583 session = __ceph_lookup_mds_session(mdsc, mds);
1584 mutex_unlock(&mdsc->mutex);
1586 dout(" inverting session/ino locks on %p\n", session);
1587 mutex_lock(&session->s_mutex);
1592 // make sure flushsnap messages are sent in proper order.
1593 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
1594 __kick_flushing_caps(mdsc, session, ci, 0);
1595 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
1598 __ceph_flush_snaps(ci, session);
1600 spin_unlock(&ci->i_ceph_lock);
1603 *psession = session;
1604 } else if (session) {
1605 mutex_unlock(&session->s_mutex);
1606 ceph_put_mds_session(session);
1608 /* we flushed them all; remove this inode from the queue */
1609 spin_lock(&mdsc->snap_flush_lock);
1610 list_del_init(&ci->i_snap_flush_item);
1611 spin_unlock(&mdsc->snap_flush_lock);
1615 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1616 * Caller is then responsible for calling __mark_inode_dirty with the
1617 * returned flags value.
1619 int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
1620 struct ceph_cap_flush **pcf)
1622 struct ceph_mds_client *mdsc =
1623 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
1624 struct inode *inode = &ci->vfs_inode;
1625 int was = ci->i_dirty_caps;
1628 if (!ci->i_auth_cap) {
1629 pr_warn("__mark_dirty_caps %p %llx mask %s, "
1630 "but no auth cap (session was closed?)\n",
1631 inode, ceph_ino(inode), ceph_cap_string(mask));
1635 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1636 ceph_cap_string(mask), ceph_cap_string(was),
1637 ceph_cap_string(was | mask));
1638 ci->i_dirty_caps |= mask;
1640 WARN_ON_ONCE(ci->i_prealloc_cap_flush);
1641 swap(ci->i_prealloc_cap_flush, *pcf);
1643 if (!ci->i_head_snapc) {
1644 WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
1645 ci->i_head_snapc = ceph_get_snap_context(
1646 ci->i_snap_realm->cached_context);
1648 dout(" inode %p now dirty snapc %p auth cap %p\n",
1649 &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
1650 BUG_ON(!list_empty(&ci->i_dirty_item));
1651 spin_lock(&mdsc->cap_dirty_lock);
1652 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
1653 spin_unlock(&mdsc->cap_dirty_lock);
1654 if (ci->i_flushing_caps == 0) {
1656 dirty |= I_DIRTY_SYNC;
1659 WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
1661 BUG_ON(list_empty(&ci->i_dirty_item));
1662 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1663 (mask & CEPH_CAP_FILE_BUFFER))
1664 dirty |= I_DIRTY_DATASYNC;
1665 __cap_delay_requeue(mdsc, ci);
1669 struct ceph_cap_flush *ceph_alloc_cap_flush(void)
1671 return kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
1674 void ceph_free_cap_flush(struct ceph_cap_flush *cf)
1677 kmem_cache_free(ceph_cap_flush_cachep, cf);
1680 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
1682 if (!list_empty(&mdsc->cap_flush_list)) {
1683 struct ceph_cap_flush *cf =
1684 list_first_entry(&mdsc->cap_flush_list,
1685 struct ceph_cap_flush, g_list);
1692 * Remove cap_flush from the mdsc's or inode's flushing cap list.
1693 * Return true if caller needs to wake up flush waiters.
1695 static bool __finish_cap_flush(struct ceph_mds_client *mdsc,
1696 struct ceph_inode_info *ci,
1697 struct ceph_cap_flush *cf)
1699 struct ceph_cap_flush *prev;
1700 bool wake = cf->wake;
1702 /* are there older pending cap flushes? */
1703 if (wake && cf->g_list.prev != &mdsc->cap_flush_list) {
1704 prev = list_prev_entry(cf, g_list);
1708 list_del(&cf->g_list);
1710 if (wake && cf->i_list.prev != &ci->i_cap_flush_list) {
1711 prev = list_prev_entry(cf, i_list);
1715 list_del(&cf->i_list);
1723 * Add dirty inode to the flushing list. Assigned a seq number so we
1724 * can wait for caps to flush without starving.
1726 * Called under i_ceph_lock.
1728 static int __mark_caps_flushing(struct inode *inode,
1729 struct ceph_mds_session *session, bool wake,
1730 u64 *flush_tid, u64 *oldest_flush_tid)
1732 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1733 struct ceph_inode_info *ci = ceph_inode(inode);
1734 struct ceph_cap_flush *cf = NULL;
1737 BUG_ON(ci->i_dirty_caps == 0);
1738 BUG_ON(list_empty(&ci->i_dirty_item));
1739 BUG_ON(!ci->i_prealloc_cap_flush);
1741 flushing = ci->i_dirty_caps;
1742 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1743 ceph_cap_string(flushing),
1744 ceph_cap_string(ci->i_flushing_caps),
1745 ceph_cap_string(ci->i_flushing_caps | flushing));
1746 ci->i_flushing_caps |= flushing;
1747 ci->i_dirty_caps = 0;
1748 dout(" inode %p now !dirty\n", inode);
1750 swap(cf, ci->i_prealloc_cap_flush);
1751 cf->caps = flushing;
1754 spin_lock(&mdsc->cap_dirty_lock);
1755 list_del_init(&ci->i_dirty_item);
1757 cf->tid = ++mdsc->last_cap_flush_tid;
1758 list_add_tail(&cf->g_list, &mdsc->cap_flush_list);
1759 *oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1761 if (list_empty(&ci->i_flushing_item)) {
1762 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1763 mdsc->num_cap_flushing++;
1765 spin_unlock(&mdsc->cap_dirty_lock);
1767 list_add_tail(&cf->i_list, &ci->i_cap_flush_list);
1769 *flush_tid = cf->tid;
1774 * try to invalidate mapping pages without blocking.
1776 static int try_nonblocking_invalidate(struct inode *inode)
1777 __releases(ci->i_ceph_lock)
1778 __acquires(ci->i_ceph_lock)
1780 struct ceph_inode_info *ci = ceph_inode(inode);
1781 u32 invalidating_gen = ci->i_rdcache_gen;
1783 spin_unlock(&ci->i_ceph_lock);
1784 ceph_fscache_invalidate(inode);
1785 invalidate_mapping_pages(&inode->i_data, 0, -1);
1786 spin_lock(&ci->i_ceph_lock);
1788 if (inode->i_data.nrpages == 0 &&
1789 invalidating_gen == ci->i_rdcache_gen) {
1791 dout("try_nonblocking_invalidate %p success\n", inode);
1792 /* save any racing async invalidate some trouble */
1793 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
1796 dout("try_nonblocking_invalidate %p failed\n", inode);
1800 bool __ceph_should_report_size(struct ceph_inode_info *ci)
1802 loff_t size = ci->vfs_inode.i_size;
1803 /* mds will adjust max size according to the reported size */
1804 if (ci->i_flushing_caps & CEPH_CAP_FILE_WR)
1806 if (size >= ci->i_max_size)
1808 /* half of previous max_size increment has been used */
1809 if (ci->i_max_size > ci->i_reported_size &&
1810 (size << 1) >= ci->i_max_size + ci->i_reported_size)
1816 * Swiss army knife function to examine currently used and wanted
1817 * versus held caps. Release, flush, ack revoked caps to mds as
1820 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1821 * cap release further.
1822 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1823 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1826 void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1827 struct ceph_mds_session *session)
1829 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1830 struct ceph_mds_client *mdsc = fsc->mdsc;
1831 struct inode *inode = &ci->vfs_inode;
1832 struct ceph_cap *cap;
1833 u64 flush_tid, oldest_flush_tid;
1834 int file_wanted, used, cap_used;
1835 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
1836 int issued, implemented, want, retain, revoking, flushing = 0;
1837 int mds = -1; /* keep track of how far we've gone through i_caps list
1838 to avoid an infinite loop on retry */
1840 int delayed = 0, sent = 0;
1841 bool no_delay = flags & CHECK_CAPS_NODELAY;
1842 bool queue_invalidate = false;
1843 bool tried_invalidate = false;
1845 /* if we are unmounting, flush any unused caps immediately. */
1849 spin_lock(&ci->i_ceph_lock);
1851 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1852 flags |= CHECK_CAPS_FLUSH;
1854 if (!(flags & CHECK_CAPS_AUTHONLY) ||
1855 (ci->i_auth_cap && __ceph_is_single_caps(ci)))
1856 __cap_delay_cancel(mdsc, ci);
1860 spin_lock(&ci->i_ceph_lock);
1862 file_wanted = __ceph_caps_file_wanted(ci);
1863 used = __ceph_caps_used(ci);
1864 issued = __ceph_caps_issued(ci, &implemented);
1865 revoking = implemented & ~issued;
1868 retain = file_wanted | used | CEPH_CAP_PIN;
1869 if (!mdsc->stopping && inode->i_nlink > 0) {
1871 retain |= CEPH_CAP_ANY; /* be greedy */
1872 } else if (S_ISDIR(inode->i_mode) &&
1873 (issued & CEPH_CAP_FILE_SHARED) &&
1874 __ceph_dir_is_complete(ci)) {
1876 * If a directory is complete, we want to keep
1877 * the exclusive cap. So that MDS does not end up
1878 * revoking the shared cap on every create/unlink
1881 want = CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
1885 retain |= CEPH_CAP_ANY_SHARED;
1887 * keep RD only if we didn't have the file open RW,
1888 * because then the mds would revoke it anyway to
1889 * journal max_size=0.
1891 if (ci->i_max_size == 0)
1892 retain |= CEPH_CAP_ANY_RD;
1896 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1897 " issued %s revoking %s retain %s %s%s%s\n", inode,
1898 ceph_cap_string(file_wanted),
1899 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1900 ceph_cap_string(ci->i_flushing_caps),
1901 ceph_cap_string(issued), ceph_cap_string(revoking),
1902 ceph_cap_string(retain),
1903 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1904 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1905 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1908 * If we no longer need to hold onto old our caps, and we may
1909 * have cached pages, but don't want them, then try to invalidate.
1910 * If we fail, it's because pages are locked.... try again later.
1912 if ((!no_delay || mdsc->stopping) &&
1913 !S_ISDIR(inode->i_mode) && /* ignore readdir cache */
1914 !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */
1915 inode->i_data.nrpages && /* have cached pages */
1916 (revoking & (CEPH_CAP_FILE_CACHE|
1917 CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */
1918 !tried_invalidate) {
1919 dout("check_caps trying to invalidate on %p\n", inode);
1920 if (try_nonblocking_invalidate(inode) < 0) {
1921 dout("check_caps queuing invalidate\n");
1922 queue_invalidate = true;
1923 ci->i_rdcache_revoking = ci->i_rdcache_gen;
1925 tried_invalidate = true;
1929 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1930 cap = rb_entry(p, struct ceph_cap, ci_node);
1932 /* avoid looping forever */
1933 if (mds >= cap->mds ||
1934 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1937 /* NOTE: no side-effects allowed, until we take s_mutex */
1940 if (ci->i_auth_cap && cap != ci->i_auth_cap)
1941 cap_used &= ~ci->i_auth_cap->issued;
1943 revoking = cap->implemented & ~cap->issued;
1944 dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
1945 cap->mds, cap, ceph_cap_string(cap_used),
1946 ceph_cap_string(cap->issued),
1947 ceph_cap_string(cap->implemented),
1948 ceph_cap_string(revoking));
1950 if (cap == ci->i_auth_cap &&
1951 (cap->issued & CEPH_CAP_FILE_WR)) {
1952 /* request larger max_size from MDS? */
1953 if (ci->i_wanted_max_size > ci->i_max_size &&
1954 ci->i_wanted_max_size > ci->i_requested_max_size) {
1955 dout("requesting new max_size\n");
1959 /* approaching file_max? */
1960 if (__ceph_should_report_size(ci)) {
1961 dout("i_size approaching max_size\n");
1965 /* flush anything dirty? */
1966 if (cap == ci->i_auth_cap) {
1967 if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) {
1968 dout("flushing dirty caps\n");
1971 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) {
1972 dout("flushing snap caps\n");
1977 /* completed revocation? going down and there are no caps? */
1978 if (revoking && (revoking & cap_used) == 0) {
1979 dout("completed revocation of %s\n",
1980 ceph_cap_string(cap->implemented & ~cap->issued));
1984 /* want more caps from mds? */
1985 if (want & ~cap->mds_wanted) {
1986 if (want & ~(cap->mds_wanted | cap->issued))
1988 if (!__cap_is_valid(cap))
1992 /* things we might delay */
1993 if ((cap->issued & ~retain) == 0 &&
1994 cap->mds_wanted == want)
1995 continue; /* nope, all good */
2001 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
2002 time_before(jiffies, ci->i_hold_caps_max)) {
2003 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
2004 ceph_cap_string(cap->issued),
2005 ceph_cap_string(cap->issued & retain),
2006 ceph_cap_string(cap->mds_wanted),
2007 ceph_cap_string(want));
2013 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
2014 dout(" skipping %p I_NOFLUSH set\n", inode);
2018 if (session && session != cap->session) {
2019 dout("oops, wrong session %p mutex\n", session);
2020 mutex_unlock(&session->s_mutex);
2024 session = cap->session;
2025 if (mutex_trylock(&session->s_mutex) == 0) {
2026 dout("inverting session/ino locks on %p\n",
2028 session = ceph_get_mds_session(session);
2029 spin_unlock(&ci->i_ceph_lock);
2030 if (took_snap_rwsem) {
2031 up_read(&mdsc->snap_rwsem);
2032 took_snap_rwsem = 0;
2035 mutex_lock(&session->s_mutex);
2036 ceph_put_mds_session(session);
2039 * Because we take the reference while
2040 * holding the i_ceph_lock, it should
2041 * never be NULL. Throw a warning if it
2050 /* kick flushing and flush snaps before sending normal
2052 if (cap == ci->i_auth_cap &&
2054 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) {
2055 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
2056 __kick_flushing_caps(mdsc, session, ci, 0);
2057 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2059 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
2060 __ceph_flush_snaps(ci, session);
2065 /* take snap_rwsem after session mutex */
2066 if (!took_snap_rwsem) {
2067 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
2068 dout("inverting snap/in locks on %p\n",
2070 spin_unlock(&ci->i_ceph_lock);
2071 down_read(&mdsc->snap_rwsem);
2072 took_snap_rwsem = 1;
2075 took_snap_rwsem = 1;
2078 if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
2079 flushing = __mark_caps_flushing(inode, session, false,
2085 spin_lock(&mdsc->cap_dirty_lock);
2086 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2087 spin_unlock(&mdsc->cap_dirty_lock);
2090 mds = cap->mds; /* remember mds, so we don't repeat */
2093 /* __send_cap drops i_ceph_lock */
2094 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, false,
2095 cap_used, want, retain, flushing,
2096 flush_tid, oldest_flush_tid);
2097 goto retry; /* retake i_ceph_lock and restart our cap scan. */
2100 /* Reschedule delayed caps release if we delayed anything */
2102 __cap_delay_requeue(mdsc, ci);
2104 spin_unlock(&ci->i_ceph_lock);
2106 if (queue_invalidate)
2107 ceph_queue_invalidate(inode);
2110 mutex_unlock(&session->s_mutex);
2111 if (took_snap_rwsem)
2112 up_read(&mdsc->snap_rwsem);
2116 * Try to flush dirty caps back to the auth mds.
2118 static int try_flush_caps(struct inode *inode, u64 *ptid)
2120 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2121 struct ceph_inode_info *ci = ceph_inode(inode);
2122 struct ceph_mds_session *session = NULL;
2124 u64 flush_tid = 0, oldest_flush_tid = 0;
2127 spin_lock(&ci->i_ceph_lock);
2128 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
2129 spin_unlock(&ci->i_ceph_lock);
2130 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
2133 if (ci->i_dirty_caps && ci->i_auth_cap) {
2134 struct ceph_cap *cap = ci->i_auth_cap;
2135 int used = __ceph_caps_used(ci);
2136 int want = __ceph_caps_wanted(ci);
2139 if (!session || session != cap->session) {
2140 spin_unlock(&ci->i_ceph_lock);
2142 mutex_unlock(&session->s_mutex);
2143 session = cap->session;
2144 mutex_lock(&session->s_mutex);
2147 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) {
2148 spin_unlock(&ci->i_ceph_lock);
2152 flushing = __mark_caps_flushing(inode, session, true,
2153 &flush_tid, &oldest_flush_tid);
2155 /* __send_cap drops i_ceph_lock */
2156 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, true,
2157 used, want, (cap->issued | cap->implemented),
2158 flushing, flush_tid, oldest_flush_tid);
2161 spin_lock(&ci->i_ceph_lock);
2162 __cap_delay_requeue(mdsc, ci);
2163 spin_unlock(&ci->i_ceph_lock);
2166 if (!list_empty(&ci->i_cap_flush_list)) {
2167 struct ceph_cap_flush *cf =
2168 list_last_entry(&ci->i_cap_flush_list,
2169 struct ceph_cap_flush, i_list);
2171 flush_tid = cf->tid;
2173 flushing = ci->i_flushing_caps;
2174 spin_unlock(&ci->i_ceph_lock);
2178 mutex_unlock(&session->s_mutex);
2185 * Return true if we've flushed caps through the given flush_tid.
2187 static int caps_are_flushed(struct inode *inode, u64 flush_tid)
2189 struct ceph_inode_info *ci = ceph_inode(inode);
2192 spin_lock(&ci->i_ceph_lock);
2193 if (!list_empty(&ci->i_cap_flush_list)) {
2194 struct ceph_cap_flush * cf =
2195 list_first_entry(&ci->i_cap_flush_list,
2196 struct ceph_cap_flush, i_list);
2197 if (cf->tid <= flush_tid)
2200 spin_unlock(&ci->i_ceph_lock);
2205 * wait for any unsafe requests to complete.
2207 static int unsafe_request_wait(struct inode *inode)
2209 struct ceph_inode_info *ci = ceph_inode(inode);
2210 struct ceph_mds_request *req1 = NULL, *req2 = NULL;
2213 spin_lock(&ci->i_unsafe_lock);
2214 if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
2215 req1 = list_last_entry(&ci->i_unsafe_dirops,
2216 struct ceph_mds_request,
2218 ceph_mdsc_get_request(req1);
2220 if (!list_empty(&ci->i_unsafe_iops)) {
2221 req2 = list_last_entry(&ci->i_unsafe_iops,
2222 struct ceph_mds_request,
2223 r_unsafe_target_item);
2224 ceph_mdsc_get_request(req2);
2226 spin_unlock(&ci->i_unsafe_lock);
2228 dout("unsafe_request_wait %p wait on tid %llu %llu\n",
2229 inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL);
2231 ret = !wait_for_completion_timeout(&req1->r_safe_completion,
2232 ceph_timeout_jiffies(req1->r_timeout));
2235 ceph_mdsc_put_request(req1);
2238 ret = !wait_for_completion_timeout(&req2->r_safe_completion,
2239 ceph_timeout_jiffies(req2->r_timeout));
2242 ceph_mdsc_put_request(req2);
2247 int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2249 struct inode *inode = file->f_mapping->host;
2250 struct ceph_inode_info *ci = ceph_inode(inode);
2255 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
2257 ret = file_write_and_wait_range(file, start, end);
2266 dirty = try_flush_caps(inode, &flush_tid);
2267 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
2269 ret = unsafe_request_wait(inode);
2272 * only wait on non-file metadata writeback (the mds
2273 * can recover size and mtime, so we don't need to
2276 if (!ret && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
2277 ret = wait_event_interruptible(ci->i_cap_wq,
2278 caps_are_flushed(inode, flush_tid));
2280 inode_unlock(inode);
2282 dout("fsync %p%s result=%d\n", inode, datasync ? " datasync" : "", ret);
2287 * Flush any dirty caps back to the mds. If we aren't asked to wait,
2288 * queue inode for flush but don't do so immediately, because we can
2289 * get by with fewer MDS messages if we wait for data writeback to
2292 int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
2294 struct ceph_inode_info *ci = ceph_inode(inode);
2298 int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync);
2300 dout("write_inode %p wait=%d\n", inode, wait);
2302 dirty = try_flush_caps(inode, &flush_tid);
2304 err = wait_event_interruptible(ci->i_cap_wq,
2305 caps_are_flushed(inode, flush_tid));
2307 struct ceph_mds_client *mdsc =
2308 ceph_sb_to_client(inode->i_sb)->mdsc;
2310 spin_lock(&ci->i_ceph_lock);
2311 if (__ceph_caps_dirty(ci))
2312 __cap_delay_requeue_front(mdsc, ci);
2313 spin_unlock(&ci->i_ceph_lock);
2318 static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
2319 struct ceph_mds_session *session,
2320 struct ceph_inode_info *ci,
2321 u64 oldest_flush_tid)
2322 __releases(ci->i_ceph_lock)
2323 __acquires(ci->i_ceph_lock)
2325 struct inode *inode = &ci->vfs_inode;
2326 struct ceph_cap *cap;
2327 struct ceph_cap_flush *cf;
2331 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
2332 if (cf->tid < first_tid)
2335 cap = ci->i_auth_cap;
2336 if (!(cap && cap->session == session)) {
2337 pr_err("%p auth cap %p not mds%d ???\n",
2338 inode, cap, session->s_mds);
2342 first_tid = cf->tid + 1;
2345 dout("kick_flushing_caps %p cap %p tid %llu %s\n",
2346 inode, cap, cf->tid, ceph_cap_string(cf->caps));
2347 ci->i_ceph_flags |= CEPH_I_NODELAY;
2348 ret = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
2349 false, __ceph_caps_used(ci),
2350 __ceph_caps_wanted(ci),
2351 cap->issued | cap->implemented,
2352 cf->caps, cf->tid, oldest_flush_tid);
2354 pr_err("kick_flushing_caps: error sending "
2355 "cap flush, ino (%llx.%llx) "
2356 "tid %llu flushing %s\n",
2357 ceph_vinop(inode), cf->tid,
2358 ceph_cap_string(cf->caps));
2361 struct ceph_cap_snap *capsnap =
2362 container_of(cf, struct ceph_cap_snap,
2364 dout("kick_flushing_caps %p capsnap %p tid %llu %s\n",
2365 inode, capsnap, cf->tid,
2366 ceph_cap_string(capsnap->dirty));
2368 refcount_inc(&capsnap->nref);
2369 spin_unlock(&ci->i_ceph_lock);
2371 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
2374 pr_err("kick_flushing_caps: error sending "
2375 "cap flushsnap, ino (%llx.%llx) "
2376 "tid %llu follows %llu\n",
2377 ceph_vinop(inode), cf->tid,
2381 ceph_put_cap_snap(capsnap);
2384 spin_lock(&ci->i_ceph_lock);
2388 void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
2389 struct ceph_mds_session *session)
2391 struct ceph_inode_info *ci;
2392 struct ceph_cap *cap;
2393 u64 oldest_flush_tid;
2395 dout("early_kick_flushing_caps mds%d\n", session->s_mds);
2397 spin_lock(&mdsc->cap_dirty_lock);
2398 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2399 spin_unlock(&mdsc->cap_dirty_lock);
2401 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2402 spin_lock(&ci->i_ceph_lock);
2403 cap = ci->i_auth_cap;
2404 if (!(cap && cap->session == session)) {
2405 pr_err("%p auth cap %p not mds%d ???\n",
2406 &ci->vfs_inode, cap, session->s_mds);
2407 spin_unlock(&ci->i_ceph_lock);
2413 * if flushing caps were revoked, we re-send the cap flush
2414 * in client reconnect stage. This guarantees MDS * processes
2415 * the cap flush message before issuing the flushing caps to
2418 if ((cap->issued & ci->i_flushing_caps) !=
2419 ci->i_flushing_caps) {
2420 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2421 __kick_flushing_caps(mdsc, session, ci,
2424 ci->i_ceph_flags |= CEPH_I_KICK_FLUSH;
2427 spin_unlock(&ci->i_ceph_lock);
2431 void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
2432 struct ceph_mds_session *session)
2434 struct ceph_inode_info *ci;
2435 struct ceph_cap *cap;
2436 u64 oldest_flush_tid;
2438 dout("kick_flushing_caps mds%d\n", session->s_mds);
2440 spin_lock(&mdsc->cap_dirty_lock);
2441 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2442 spin_unlock(&mdsc->cap_dirty_lock);
2444 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2445 spin_lock(&ci->i_ceph_lock);
2446 cap = ci->i_auth_cap;
2447 if (!(cap && cap->session == session)) {
2448 pr_err("%p auth cap %p not mds%d ???\n",
2449 &ci->vfs_inode, cap, session->s_mds);
2450 spin_unlock(&ci->i_ceph_lock);
2453 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
2454 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2455 __kick_flushing_caps(mdsc, session, ci,
2458 spin_unlock(&ci->i_ceph_lock);
2462 static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
2463 struct ceph_mds_session *session,
2464 struct inode *inode)
2465 __releases(ci->i_ceph_lock)
2467 struct ceph_inode_info *ci = ceph_inode(inode);
2468 struct ceph_cap *cap;
2470 cap = ci->i_auth_cap;
2471 dout("kick_flushing_inode_caps %p flushing %s\n", inode,
2472 ceph_cap_string(ci->i_flushing_caps));
2474 if (!list_empty(&ci->i_cap_flush_list)) {
2475 u64 oldest_flush_tid;
2476 spin_lock(&mdsc->cap_dirty_lock);
2477 list_move_tail(&ci->i_flushing_item,
2478 &cap->session->s_cap_flushing);
2479 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2480 spin_unlock(&mdsc->cap_dirty_lock);
2482 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2483 __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid);
2484 spin_unlock(&ci->i_ceph_lock);
2486 spin_unlock(&ci->i_ceph_lock);
2492 * Take references to capabilities we hold, so that we don't release
2493 * them to the MDS prematurely.
2495 * Protected by i_ceph_lock.
2497 static void __take_cap_refs(struct ceph_inode_info *ci, int got,
2498 bool snap_rwsem_locked)
2500 if (got & CEPH_CAP_PIN)
2502 if (got & CEPH_CAP_FILE_RD)
2504 if (got & CEPH_CAP_FILE_CACHE)
2505 ci->i_rdcache_ref++;
2506 if (got & CEPH_CAP_FILE_WR) {
2507 if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
2508 BUG_ON(!snap_rwsem_locked);
2509 ci->i_head_snapc = ceph_get_snap_context(
2510 ci->i_snap_realm->cached_context);
2514 if (got & CEPH_CAP_FILE_BUFFER) {
2515 if (ci->i_wb_ref == 0)
2516 ihold(&ci->vfs_inode);
2518 dout("__take_cap_refs %p wb %d -> %d (?)\n",
2519 &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
2524 * Try to grab cap references. Specify those refs we @want, and the
2525 * minimal set we @need. Also include the larger offset we are writing
2526 * to (when applicable), and check against max_size here as well.
2527 * Note that caller is responsible for ensuring max_size increases are
2528 * requested from the MDS.
2530 static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
2531 loff_t endoff, bool nonblock, int *got, int *err)
2533 struct inode *inode = &ci->vfs_inode;
2534 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
2536 int have, implemented;
2538 bool snap_rwsem_locked = false;
2540 dout("get_cap_refs %p need %s want %s\n", inode,
2541 ceph_cap_string(need), ceph_cap_string(want));
2544 spin_lock(&ci->i_ceph_lock);
2546 /* make sure file is actually open */
2547 file_wanted = __ceph_caps_file_wanted(ci);
2548 if ((file_wanted & need) != need) {
2549 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2550 ceph_cap_string(need), ceph_cap_string(file_wanted));
2556 /* finish pending truncate */
2557 while (ci->i_truncate_pending) {
2558 spin_unlock(&ci->i_ceph_lock);
2559 if (snap_rwsem_locked) {
2560 up_read(&mdsc->snap_rwsem);
2561 snap_rwsem_locked = false;
2563 __ceph_do_pending_vmtruncate(inode);
2564 spin_lock(&ci->i_ceph_lock);
2567 have = __ceph_caps_issued(ci, &implemented);
2569 if (have & need & CEPH_CAP_FILE_WR) {
2570 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2571 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2572 inode, endoff, ci->i_max_size);
2573 if (endoff > ci->i_requested_max_size) {
2580 * If a sync write is in progress, we must wait, so that we
2581 * can get a final snapshot value for size+mtime.
2583 if (__ceph_have_pending_cap_snap(ci)) {
2584 dout("get_cap_refs %p cap_snap_pending\n", inode);
2589 if ((have & need) == need) {
2591 * Look at (implemented & ~have & not) so that we keep waiting
2592 * on transition from wanted -> needed caps. This is needed
2593 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2594 * going before a prior buffered writeback happens.
2596 int not = want & ~(have & need);
2597 int revoking = implemented & ~have;
2598 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2599 inode, ceph_cap_string(have), ceph_cap_string(not),
2600 ceph_cap_string(revoking));
2601 if ((revoking & not) == 0) {
2602 if (!snap_rwsem_locked &&
2603 !ci->i_head_snapc &&
2604 (need & CEPH_CAP_FILE_WR)) {
2605 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2607 * we can not call down_read() when
2608 * task isn't in TASK_RUNNING state
2616 spin_unlock(&ci->i_ceph_lock);
2617 down_read(&mdsc->snap_rwsem);
2618 snap_rwsem_locked = true;
2621 snap_rwsem_locked = true;
2623 *got = need | (have & want);
2624 if ((need & CEPH_CAP_FILE_RD) &&
2625 !(*got & CEPH_CAP_FILE_CACHE))
2626 ceph_disable_fscache_readpage(ci);
2627 __take_cap_refs(ci, *got, true);
2631 int session_readonly = false;
2632 if ((need & CEPH_CAP_FILE_WR) && ci->i_auth_cap) {
2633 struct ceph_mds_session *s = ci->i_auth_cap->session;
2634 spin_lock(&s->s_cap_lock);
2635 session_readonly = s->s_readonly;
2636 spin_unlock(&s->s_cap_lock);
2638 if (session_readonly) {
2639 dout("get_cap_refs %p needed %s but mds%d readonly\n",
2640 inode, ceph_cap_string(need), ci->i_auth_cap->mds);
2646 if (ci->i_ceph_flags & CEPH_I_CAP_DROPPED) {
2648 if (READ_ONCE(mdsc->fsc->mount_state) ==
2649 CEPH_MOUNT_SHUTDOWN) {
2650 dout("get_cap_refs %p forced umount\n", inode);
2655 mds_wanted = __ceph_caps_mds_wanted(ci, false);
2656 if (need & ~(mds_wanted & need)) {
2657 dout("get_cap_refs %p caps were dropped"
2658 " (session killed?)\n", inode);
2663 if (!(file_wanted & ~mds_wanted))
2664 ci->i_ceph_flags &= ~CEPH_I_CAP_DROPPED;
2667 dout("get_cap_refs %p have %s needed %s\n", inode,
2668 ceph_cap_string(have), ceph_cap_string(need));
2671 spin_unlock(&ci->i_ceph_lock);
2672 if (snap_rwsem_locked)
2673 up_read(&mdsc->snap_rwsem);
2675 dout("get_cap_refs %p ret %d got %s\n", inode,
2676 ret, ceph_cap_string(*got));
2681 * Check the offset we are writing up to against our current
2682 * max_size. If necessary, tell the MDS we want to write to
2685 static void check_max_size(struct inode *inode, loff_t endoff)
2687 struct ceph_inode_info *ci = ceph_inode(inode);
2690 /* do we need to explicitly request a larger max_size? */
2691 spin_lock(&ci->i_ceph_lock);
2692 if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
2693 dout("write %p at large endoff %llu, req max_size\n",
2695 ci->i_wanted_max_size = endoff;
2697 /* duplicate ceph_check_caps()'s logic */
2698 if (ci->i_auth_cap &&
2699 (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
2700 ci->i_wanted_max_size > ci->i_max_size &&
2701 ci->i_wanted_max_size > ci->i_requested_max_size)
2703 spin_unlock(&ci->i_ceph_lock);
2705 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2708 int ceph_try_get_caps(struct ceph_inode_info *ci, int need, int want, int *got)
2712 BUG_ON(need & ~CEPH_CAP_FILE_RD);
2713 BUG_ON(want & ~(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
2714 ret = ceph_pool_perm_check(ci, need);
2718 ret = try_get_cap_refs(ci, need, want, 0, true, got, &err);
2720 if (err == -EAGAIN) {
2722 } else if (err < 0) {
2730 * Wait for caps, and take cap references. If we can't get a WR cap
2731 * due to a small max_size, make sure we check_max_size (and possibly
2732 * ask the mds) so we don't get hung up indefinitely.
2734 int ceph_get_caps(struct ceph_inode_info *ci, int need, int want,
2735 loff_t endoff, int *got, struct page **pinned_page)
2737 int _got, ret, err = 0;
2739 ret = ceph_pool_perm_check(ci, need);
2745 check_max_size(&ci->vfs_inode, endoff);
2749 ret = try_get_cap_refs(ci, need, want, endoff,
2750 false, &_got, &err);
2757 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2758 add_wait_queue(&ci->i_cap_wq, &wait);
2760 while (!try_get_cap_refs(ci, need, want, endoff,
2761 true, &_got, &err)) {
2762 if (signal_pending(current)) {
2766 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
2769 remove_wait_queue(&ci->i_cap_wq, &wait);
2777 if (err == -ESTALE) {
2778 /* session was killed, try renew caps */
2779 ret = ceph_renew_caps(&ci->vfs_inode);
2786 if (ci->i_inline_version != CEPH_INLINE_NONE &&
2787 (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
2788 i_size_read(&ci->vfs_inode) > 0) {
2790 find_get_page(ci->vfs_inode.i_mapping, 0);
2792 if (PageUptodate(page)) {
2793 *pinned_page = page;
2799 * drop cap refs first because getattr while
2800 * holding * caps refs can cause deadlock.
2802 ceph_put_cap_refs(ci, _got);
2806 * getattr request will bring inline data into
2809 ret = __ceph_do_getattr(&ci->vfs_inode, NULL,
2810 CEPH_STAT_CAP_INLINE_DATA,
2819 if ((_got & CEPH_CAP_FILE_RD) && (_got & CEPH_CAP_FILE_CACHE))
2820 ceph_fscache_revalidate_cookie(ci);
2827 * Take cap refs. Caller must already know we hold at least one ref
2828 * on the caps in question or we don't know this is safe.
2830 void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2832 spin_lock(&ci->i_ceph_lock);
2833 __take_cap_refs(ci, caps, false);
2834 spin_unlock(&ci->i_ceph_lock);
2839 * drop cap_snap that is not associated with any snapshot.
2840 * we don't need to send FLUSHSNAP message for it.
2842 static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci,
2843 struct ceph_cap_snap *capsnap)
2845 if (!capsnap->need_flush &&
2846 !capsnap->writing && !capsnap->dirty_pages) {
2847 dout("dropping cap_snap %p follows %llu\n",
2848 capsnap, capsnap->follows);
2849 BUG_ON(capsnap->cap_flush.tid > 0);
2850 ceph_put_snap_context(capsnap->context);
2851 if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps))
2852 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2854 list_del(&capsnap->ci_item);
2855 ceph_put_cap_snap(capsnap);
2864 * If we released the last ref on any given cap, call ceph_check_caps
2865 * to release (or schedule a release).
2867 * If we are releasing a WR cap (from a sync write), finalize any affected
2868 * cap_snap, and wake up any waiters.
2870 void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2872 struct inode *inode = &ci->vfs_inode;
2873 int last = 0, put = 0, flushsnaps = 0, wake = 0;
2875 spin_lock(&ci->i_ceph_lock);
2876 if (had & CEPH_CAP_PIN)
2878 if (had & CEPH_CAP_FILE_RD)
2879 if (--ci->i_rd_ref == 0)
2881 if (had & CEPH_CAP_FILE_CACHE)
2882 if (--ci->i_rdcache_ref == 0)
2884 if (had & CEPH_CAP_FILE_BUFFER) {
2885 if (--ci->i_wb_ref == 0) {
2889 dout("put_cap_refs %p wb %d -> %d (?)\n",
2890 inode, ci->i_wb_ref+1, ci->i_wb_ref);
2892 if (had & CEPH_CAP_FILE_WR)
2893 if (--ci->i_wr_ref == 0) {
2895 if (__ceph_have_pending_cap_snap(ci)) {
2896 struct ceph_cap_snap *capsnap =
2897 list_last_entry(&ci->i_cap_snaps,
2898 struct ceph_cap_snap,
2900 capsnap->writing = 0;
2901 if (ceph_try_drop_cap_snap(ci, capsnap))
2903 else if (__ceph_finish_cap_snap(ci, capsnap))
2907 if (ci->i_wrbuffer_ref_head == 0 &&
2908 ci->i_dirty_caps == 0 &&
2909 ci->i_flushing_caps == 0) {
2910 BUG_ON(!ci->i_head_snapc);
2911 ceph_put_snap_context(ci->i_head_snapc);
2912 ci->i_head_snapc = NULL;
2914 /* see comment in __ceph_remove_cap() */
2915 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm)
2916 drop_inode_snap_realm(ci);
2918 spin_unlock(&ci->i_ceph_lock);
2920 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2921 last ? " last" : "", put ? " put" : "");
2923 if (last && !flushsnaps)
2924 ceph_check_caps(ci, 0, NULL);
2925 else if (flushsnaps)
2926 ceph_flush_snaps(ci, NULL);
2928 wake_up_all(&ci->i_cap_wq);
2934 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2935 * context. Adjust per-snap dirty page accounting as appropriate.
2936 * Once all dirty data for a cap_snap is flushed, flush snapped file
2937 * metadata back to the MDS. If we dropped the last ref, call
2940 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2941 struct ceph_snap_context *snapc)
2943 struct inode *inode = &ci->vfs_inode;
2944 struct ceph_cap_snap *capsnap = NULL;
2948 bool flush_snaps = false;
2949 bool complete_capsnap = false;
2951 spin_lock(&ci->i_ceph_lock);
2952 ci->i_wrbuffer_ref -= nr;
2953 if (ci->i_wrbuffer_ref == 0) {
2958 if (ci->i_head_snapc == snapc) {
2959 ci->i_wrbuffer_ref_head -= nr;
2960 if (ci->i_wrbuffer_ref_head == 0 &&
2961 ci->i_wr_ref == 0 &&
2962 ci->i_dirty_caps == 0 &&
2963 ci->i_flushing_caps == 0) {
2964 BUG_ON(!ci->i_head_snapc);
2965 ceph_put_snap_context(ci->i_head_snapc);
2966 ci->i_head_snapc = NULL;
2968 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2970 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2971 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2972 last ? " LAST" : "");
2974 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2975 if (capsnap->context == snapc) {
2981 capsnap->dirty_pages -= nr;
2982 if (capsnap->dirty_pages == 0) {
2983 complete_capsnap = true;
2984 if (!capsnap->writing) {
2985 if (ceph_try_drop_cap_snap(ci, capsnap)) {
2988 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2993 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2994 " snap %lld %d/%d -> %d/%d %s%s\n",
2995 inode, capsnap, capsnap->context->seq,
2996 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2997 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2998 last ? " (wrbuffer last)" : "",
2999 complete_capsnap ? " (complete capsnap)" : "");
3002 spin_unlock(&ci->i_ceph_lock);
3005 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
3006 } else if (flush_snaps) {
3007 ceph_flush_snaps(ci, NULL);
3009 if (complete_capsnap)
3010 wake_up_all(&ci->i_cap_wq);
3016 * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
3018 static void invalidate_aliases(struct inode *inode)
3020 struct dentry *dn, *prev = NULL;
3022 dout("invalidate_aliases inode %p\n", inode);
3023 d_prune_aliases(inode);
3025 * For non-directory inode, d_find_alias() only returns
3026 * hashed dentry. After calling d_invalidate(), the
3027 * dentry becomes unhashed.
3029 * For directory inode, d_find_alias() can return
3030 * unhashed dentry. But directory inode should have
3031 * one alias at most.
3033 while ((dn = d_find_alias(inode))) {
3047 struct cap_extra_info {
3048 struct ceph_string *pool_ns;
3057 /* currently issued */
3062 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
3063 * actually be a revocation if it specifies a smaller cap set.)
3065 * caller holds s_mutex and i_ceph_lock, we drop both.
3067 static void handle_cap_grant(struct inode *inode,
3068 struct ceph_mds_session *session,
3069 struct ceph_cap *cap,
3070 struct ceph_mds_caps *grant,
3071 struct ceph_buffer *xattr_buf,
3072 struct cap_extra_info *extra_info)
3073 __releases(ci->i_ceph_lock)
3074 __releases(session->s_mdsc->snap_rwsem)
3076 struct ceph_inode_info *ci = ceph_inode(inode);
3077 int seq = le32_to_cpu(grant->seq);
3078 int newcaps = le32_to_cpu(grant->caps);
3079 int used, wanted, dirty;
3080 u64 size = le64_to_cpu(grant->size);
3081 u64 max_size = le64_to_cpu(grant->max_size);
3084 bool writeback = false;
3085 bool queue_trunc = false;
3086 bool queue_invalidate = false;
3087 bool deleted_inode = false;
3088 bool fill_inline = false;
3090 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
3091 inode, cap, session->s_mds, seq, ceph_cap_string(newcaps));
3092 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
3097 * auth mds of the inode changed. we received the cap export message,
3098 * but still haven't received the cap import message. handle_cap_export
3099 * updated the new auth MDS' cap.
3101 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
3102 * that was sent before the cap import message. So don't remove caps.
3104 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
3105 WARN_ON(cap != ci->i_auth_cap);
3106 WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
3108 newcaps |= cap->issued;
3112 * If CACHE is being revoked, and we have no dirty buffers,
3113 * try to invalidate (once). (If there are dirty buffers, we
3114 * will invalidate _after_ writeback.)
3116 if (!S_ISDIR(inode->i_mode) && /* don't invalidate readdir cache */
3117 ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
3118 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3119 !(ci->i_wrbuffer_ref || ci->i_wb_ref)) {
3120 if (try_nonblocking_invalidate(inode)) {
3121 /* there were locked pages.. invalidate later
3122 in a separate thread. */
3123 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
3124 queue_invalidate = true;
3125 ci->i_rdcache_revoking = ci->i_rdcache_gen;
3130 /* side effects now are allowed */
3131 cap->cap_gen = session->s_cap_gen;
3134 __check_cap_issue(ci, cap, newcaps);
3136 if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
3137 (extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) {
3138 inode->i_mode = le32_to_cpu(grant->mode);
3139 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
3140 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
3141 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
3142 from_kuid(&init_user_ns, inode->i_uid),
3143 from_kgid(&init_user_ns, inode->i_gid));
3146 if ((newcaps & CEPH_CAP_LINK_SHARED) &&
3147 (extra_info->issued & CEPH_CAP_LINK_EXCL) == 0) {
3148 set_nlink(inode, le32_to_cpu(grant->nlink));
3149 if (inode->i_nlink == 0 &&
3150 (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
3151 deleted_inode = true;
3154 if ((extra_info->issued & CEPH_CAP_XATTR_EXCL) == 0 &&
3156 int len = le32_to_cpu(grant->xattr_len);
3157 u64 version = le64_to_cpu(grant->xattr_version);
3159 if (version > ci->i_xattrs.version) {
3160 dout(" got new xattrs v%llu on %p len %d\n",
3161 version, inode, len);
3162 if (ci->i_xattrs.blob)
3163 ceph_buffer_put(ci->i_xattrs.blob);
3164 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
3165 ci->i_xattrs.version = version;
3166 ceph_forget_all_cached_acls(inode);
3170 if (newcaps & CEPH_CAP_ANY_RD) {
3171 struct timespec64 mtime, atime, ctime;
3172 /* ctime/mtime/atime? */
3173 ceph_decode_timespec64(&mtime, &grant->mtime);
3174 ceph_decode_timespec64(&atime, &grant->atime);
3175 ceph_decode_timespec64(&ctime, &grant->ctime);
3176 ceph_fill_file_time(inode, extra_info->issued,
3177 le32_to_cpu(grant->time_warp_seq),
3178 &ctime, &mtime, &atime);
3181 if ((newcaps & CEPH_CAP_FILE_SHARED) && extra_info->dirstat_valid) {
3182 ci->i_files = extra_info->nfiles;
3183 ci->i_subdirs = extra_info->nsubdirs;
3186 if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
3187 /* file layout may have changed */
3188 s64 old_pool = ci->i_layout.pool_id;
3189 struct ceph_string *old_ns;
3191 ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout);
3192 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
3193 lockdep_is_held(&ci->i_ceph_lock));
3194 rcu_assign_pointer(ci->i_layout.pool_ns, extra_info->pool_ns);
3196 if (ci->i_layout.pool_id != old_pool ||
3197 extra_info->pool_ns != old_ns)
3198 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
3200 extra_info->pool_ns = old_ns;
3202 /* size/truncate_seq? */
3203 queue_trunc = ceph_fill_file_size(inode, extra_info->issued,
3204 le32_to_cpu(grant->truncate_seq),
3205 le64_to_cpu(grant->truncate_size),
3209 if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) {
3210 if (max_size != ci->i_max_size) {
3211 dout("max_size %lld -> %llu\n",
3212 ci->i_max_size, max_size);
3213 ci->i_max_size = max_size;
3214 if (max_size >= ci->i_wanted_max_size) {
3215 ci->i_wanted_max_size = 0; /* reset */
3216 ci->i_requested_max_size = 0;
3219 } else if (ci->i_wanted_max_size > ci->i_max_size &&
3220 ci->i_wanted_max_size > ci->i_requested_max_size) {
3221 /* CEPH_CAP_OP_IMPORT */
3226 /* check cap bits */
3227 wanted = __ceph_caps_wanted(ci);
3228 used = __ceph_caps_used(ci);
3229 dirty = __ceph_caps_dirty(ci);
3230 dout(" my wanted = %s, used = %s, dirty %s\n",
3231 ceph_cap_string(wanted),
3232 ceph_cap_string(used),
3233 ceph_cap_string(dirty));
3234 if (wanted != le32_to_cpu(grant->wanted)) {
3235 dout("mds wanted %s -> %s\n",
3236 ceph_cap_string(le32_to_cpu(grant->wanted)),
3237 ceph_cap_string(wanted));
3238 /* imported cap may not have correct mds_wanted */
3239 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT)
3243 /* revocation, grant, or no-op? */
3244 if (cap->issued & ~newcaps) {
3245 int revoking = cap->issued & ~newcaps;
3247 dout("revocation: %s -> %s (revoking %s)\n",
3248 ceph_cap_string(cap->issued),
3249 ceph_cap_string(newcaps),
3250 ceph_cap_string(revoking));
3251 if (revoking & used & CEPH_CAP_FILE_BUFFER)
3252 writeback = true; /* initiate writeback; will delay ack */
3253 else if (revoking == CEPH_CAP_FILE_CACHE &&
3254 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3256 ; /* do nothing yet, invalidation will be queued */
3257 else if (cap == ci->i_auth_cap)
3258 check_caps = 1; /* check auth cap only */
3260 check_caps = 2; /* check all caps */
3261 cap->issued = newcaps;
3262 cap->implemented |= newcaps;
3263 } else if (cap->issued == newcaps) {
3264 dout("caps unchanged: %s -> %s\n",
3265 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
3267 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
3268 ceph_cap_string(newcaps));
3269 /* non-auth MDS is revoking the newly grant caps ? */
3270 if (cap == ci->i_auth_cap &&
3271 __ceph_caps_revoking_other(ci, cap, newcaps))
3274 cap->issued = newcaps;
3275 cap->implemented |= newcaps; /* add bits only, to
3276 * avoid stepping on a
3277 * pending revocation */
3280 BUG_ON(cap->issued & ~cap->implemented);
3282 if (extra_info->inline_version > 0 &&
3283 extra_info->inline_version >= ci->i_inline_version) {
3284 ci->i_inline_version = extra_info->inline_version;
3285 if (ci->i_inline_version != CEPH_INLINE_NONE &&
3286 (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
3290 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
3291 if (newcaps & ~extra_info->issued)
3293 kick_flushing_inode_caps(session->s_mdsc, session, inode);
3294 up_read(&session->s_mdsc->snap_rwsem);
3296 spin_unlock(&ci->i_ceph_lock);
3300 ceph_fill_inline_data(inode, NULL, extra_info->inline_data,
3301 extra_info->inline_len);
3304 ceph_queue_vmtruncate(inode);
3308 * queue inode for writeback: we can't actually call
3309 * filemap_write_and_wait, etc. from message handler
3312 ceph_queue_writeback(inode);
3313 if (queue_invalidate)
3314 ceph_queue_invalidate(inode);
3316 invalidate_aliases(inode);
3318 wake_up_all(&ci->i_cap_wq);
3320 if (check_caps == 1)
3321 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
3323 else if (check_caps == 2)
3324 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
3326 mutex_unlock(&session->s_mutex);
3330 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
3331 * MDS has been safely committed.
3333 static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
3334 struct ceph_mds_caps *m,
3335 struct ceph_mds_session *session,
3336 struct ceph_cap *cap)
3337 __releases(ci->i_ceph_lock)
3339 struct ceph_inode_info *ci = ceph_inode(inode);
3340 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3341 struct ceph_cap_flush *cf, *tmp_cf;
3342 LIST_HEAD(to_remove);
3343 unsigned seq = le32_to_cpu(m->seq);
3344 int dirty = le32_to_cpu(m->dirty);
3347 bool wake_ci = false;
3348 bool wake_mdsc = false;
3350 list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
3351 if (cf->tid == flush_tid)
3353 if (cf->caps == 0) /* capsnap */
3355 if (cf->tid <= flush_tid) {
3356 if (__finish_cap_flush(NULL, ci, cf))
3358 list_add_tail(&cf->i_list, &to_remove);
3360 cleaned &= ~cf->caps;
3366 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
3367 " flushing %s -> %s\n",
3368 inode, session->s_mds, seq, ceph_cap_string(dirty),
3369 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
3370 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
3372 if (list_empty(&to_remove) && !cleaned)
3375 ci->i_flushing_caps &= ~cleaned;
3377 spin_lock(&mdsc->cap_dirty_lock);
3379 list_for_each_entry(cf, &to_remove, i_list) {
3380 if (__finish_cap_flush(mdsc, NULL, cf))
3384 if (ci->i_flushing_caps == 0) {
3385 if (list_empty(&ci->i_cap_flush_list)) {
3386 list_del_init(&ci->i_flushing_item);
3387 if (!list_empty(&session->s_cap_flushing)) {
3388 dout(" mds%d still flushing cap on %p\n",
3390 &list_first_entry(&session->s_cap_flushing,
3391 struct ceph_inode_info,
3392 i_flushing_item)->vfs_inode);
3395 mdsc->num_cap_flushing--;
3396 dout(" inode %p now !flushing\n", inode);
3398 if (ci->i_dirty_caps == 0) {
3399 dout(" inode %p now clean\n", inode);
3400 BUG_ON(!list_empty(&ci->i_dirty_item));
3402 if (ci->i_wr_ref == 0 &&
3403 ci->i_wrbuffer_ref_head == 0) {
3404 BUG_ON(!ci->i_head_snapc);
3405 ceph_put_snap_context(ci->i_head_snapc);
3406 ci->i_head_snapc = NULL;
3409 BUG_ON(list_empty(&ci->i_dirty_item));
3412 spin_unlock(&mdsc->cap_dirty_lock);
3415 spin_unlock(&ci->i_ceph_lock);
3417 while (!list_empty(&to_remove)) {
3418 cf = list_first_entry(&to_remove,
3419 struct ceph_cap_flush, i_list);
3420 list_del(&cf->i_list);
3421 ceph_free_cap_flush(cf);
3425 wake_up_all(&ci->i_cap_wq);
3427 wake_up_all(&mdsc->cap_flushing_wq);
3433 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
3434 * throw away our cap_snap.
3436 * Caller hold s_mutex.
3438 static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
3439 struct ceph_mds_caps *m,
3440 struct ceph_mds_session *session)
3442 struct ceph_inode_info *ci = ceph_inode(inode);
3443 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3444 u64 follows = le64_to_cpu(m->snap_follows);
3445 struct ceph_cap_snap *capsnap;
3446 bool flushed = false;
3447 bool wake_ci = false;
3448 bool wake_mdsc = false;
3450 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
3451 inode, ci, session->s_mds, follows);
3453 spin_lock(&ci->i_ceph_lock);
3454 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
3455 if (capsnap->follows == follows) {
3456 if (capsnap->cap_flush.tid != flush_tid) {
3457 dout(" cap_snap %p follows %lld tid %lld !="
3458 " %lld\n", capsnap, follows,
3459 flush_tid, capsnap->cap_flush.tid);
3465 dout(" skipping cap_snap %p follows %lld\n",
3466 capsnap, capsnap->follows);
3470 WARN_ON(capsnap->dirty_pages || capsnap->writing);
3471 dout(" removing %p cap_snap %p follows %lld\n",
3472 inode, capsnap, follows);
3473 list_del(&capsnap->ci_item);
3474 if (__finish_cap_flush(NULL, ci, &capsnap->cap_flush))
3477 spin_lock(&mdsc->cap_dirty_lock);
3479 if (list_empty(&ci->i_cap_flush_list))
3480 list_del_init(&ci->i_flushing_item);
3482 if (__finish_cap_flush(mdsc, NULL, &capsnap->cap_flush))
3485 spin_unlock(&mdsc->cap_dirty_lock);
3487 spin_unlock(&ci->i_ceph_lock);
3489 ceph_put_snap_context(capsnap->context);
3490 ceph_put_cap_snap(capsnap);
3492 wake_up_all(&ci->i_cap_wq);
3494 wake_up_all(&mdsc->cap_flushing_wq);
3500 * Handle TRUNC from MDS, indicating file truncation.
3502 * caller hold s_mutex.
3504 static void handle_cap_trunc(struct inode *inode,
3505 struct ceph_mds_caps *trunc,
3506 struct ceph_mds_session *session)
3507 __releases(ci->i_ceph_lock)
3509 struct ceph_inode_info *ci = ceph_inode(inode);
3510 int mds = session->s_mds;
3511 int seq = le32_to_cpu(trunc->seq);
3512 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
3513 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
3514 u64 size = le64_to_cpu(trunc->size);
3515 int implemented = 0;
3516 int dirty = __ceph_caps_dirty(ci);
3517 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
3518 int queue_trunc = 0;
3520 issued |= implemented | dirty;
3522 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
3523 inode, mds, seq, truncate_size, truncate_seq);
3524 queue_trunc = ceph_fill_file_size(inode, issued,
3525 truncate_seq, truncate_size, size);
3526 spin_unlock(&ci->i_ceph_lock);
3529 ceph_queue_vmtruncate(inode);
3533 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
3534 * different one. If we are the most recent migration we've seen (as
3535 * indicated by mseq), make note of the migrating cap bits for the
3536 * duration (until we see the corresponding IMPORT).
3538 * caller holds s_mutex
3540 static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
3541 struct ceph_mds_cap_peer *ph,
3542 struct ceph_mds_session *session)
3544 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
3545 struct ceph_mds_session *tsession = NULL;
3546 struct ceph_cap *cap, *tcap, *new_cap = NULL;
3547 struct ceph_inode_info *ci = ceph_inode(inode);
3549 unsigned mseq = le32_to_cpu(ex->migrate_seq);
3550 unsigned t_seq, t_mseq;
3552 int mds = session->s_mds;
3555 t_cap_id = le64_to_cpu(ph->cap_id);
3556 t_seq = le32_to_cpu(ph->seq);
3557 t_mseq = le32_to_cpu(ph->mseq);
3558 target = le32_to_cpu(ph->mds);
3560 t_cap_id = t_seq = t_mseq = 0;
3564 dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n",
3565 inode, ci, mds, mseq, target);
3567 spin_lock(&ci->i_ceph_lock);
3568 cap = __get_cap_for_mds(ci, mds);
3569 if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
3573 __ceph_remove_cap(cap, false);
3574 if (!ci->i_auth_cap)
3575 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
3580 * now we know we haven't received the cap import message yet
3581 * because the exported cap still exist.
3584 issued = cap->issued;
3585 if (issued != cap->implemented)
3586 pr_err_ratelimited("handle_cap_export: issued != implemented: "
3587 "ino (%llx.%llx) mds%d seq %d mseq %d "
3588 "issued %s implemented %s\n",
3589 ceph_vinop(inode), mds, cap->seq, cap->mseq,
3590 ceph_cap_string(issued),
3591 ceph_cap_string(cap->implemented));
3594 tcap = __get_cap_for_mds(ci, target);
3596 /* already have caps from the target */
3597 if (tcap->cap_id == t_cap_id &&
3598 ceph_seq_cmp(tcap->seq, t_seq) < 0) {
3599 dout(" updating import cap %p mds%d\n", tcap, target);
3600 tcap->cap_id = t_cap_id;
3601 tcap->seq = t_seq - 1;
3602 tcap->issue_seq = t_seq - 1;
3603 tcap->issued |= issued;
3604 tcap->implemented |= issued;
3605 if (cap == ci->i_auth_cap)
3606 ci->i_auth_cap = tcap;
3608 if (!list_empty(&ci->i_cap_flush_list) &&
3609 ci->i_auth_cap == tcap) {
3610 spin_lock(&mdsc->cap_dirty_lock);
3611 list_move_tail(&ci->i_flushing_item,
3612 &tcap->session->s_cap_flushing);
3613 spin_unlock(&mdsc->cap_dirty_lock);
3616 __ceph_remove_cap(cap, false);
3618 } else if (tsession) {
3619 /* add placeholder for the export tagert */
3620 int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
3622 ceph_add_cap(inode, tsession, t_cap_id, -1, issued, 0,
3623 t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
3625 if (!list_empty(&ci->i_cap_flush_list) &&
3626 ci->i_auth_cap == tcap) {
3627 spin_lock(&mdsc->cap_dirty_lock);
3628 list_move_tail(&ci->i_flushing_item,
3629 &tcap->session->s_cap_flushing);
3630 spin_unlock(&mdsc->cap_dirty_lock);
3633 __ceph_remove_cap(cap, false);
3637 spin_unlock(&ci->i_ceph_lock);
3638 mutex_unlock(&session->s_mutex);
3640 /* open target session */
3641 tsession = ceph_mdsc_open_export_target_session(mdsc, target);
3642 if (!IS_ERR(tsession)) {
3644 mutex_lock(&session->s_mutex);
3645 mutex_lock_nested(&tsession->s_mutex,
3646 SINGLE_DEPTH_NESTING);
3648 mutex_lock(&tsession->s_mutex);
3649 mutex_lock_nested(&session->s_mutex,
3650 SINGLE_DEPTH_NESTING);
3652 new_cap = ceph_get_cap(mdsc, NULL);
3657 mutex_lock(&session->s_mutex);
3662 spin_unlock(&ci->i_ceph_lock);
3663 mutex_unlock(&session->s_mutex);
3665 mutex_unlock(&tsession->s_mutex);
3666 ceph_put_mds_session(tsession);
3669 ceph_put_cap(mdsc, new_cap);
3673 * Handle cap IMPORT.
3675 * caller holds s_mutex. acquires i_ceph_lock
3677 static void handle_cap_import(struct ceph_mds_client *mdsc,
3678 struct inode *inode, struct ceph_mds_caps *im,
3679 struct ceph_mds_cap_peer *ph,
3680 struct ceph_mds_session *session,
3681 struct ceph_cap **target_cap, int *old_issued)
3682 __acquires(ci->i_ceph_lock)
3684 struct ceph_inode_info *ci = ceph_inode(inode);
3685 struct ceph_cap *cap, *ocap, *new_cap = NULL;
3686 int mds = session->s_mds;
3688 unsigned caps = le32_to_cpu(im->caps);
3689 unsigned wanted = le32_to_cpu(im->wanted);
3690 unsigned seq = le32_to_cpu(im->seq);
3691 unsigned mseq = le32_to_cpu(im->migrate_seq);
3692 u64 realmino = le64_to_cpu(im->realm);
3693 u64 cap_id = le64_to_cpu(im->cap_id);
3698 p_cap_id = le64_to_cpu(ph->cap_id);
3699 peer = le32_to_cpu(ph->mds);
3705 dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n",
3706 inode, ci, mds, mseq, peer);
3709 spin_lock(&ci->i_ceph_lock);
3710 cap = __get_cap_for_mds(ci, mds);
3713 spin_unlock(&ci->i_ceph_lock);
3714 new_cap = ceph_get_cap(mdsc, NULL);
3720 ceph_put_cap(mdsc, new_cap);
3725 __ceph_caps_issued(ci, &issued);
3726 issued |= __ceph_caps_dirty(ci);
3728 ceph_add_cap(inode, session, cap_id, -1, caps, wanted, seq, mseq,
3729 realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
3731 ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
3732 if (ocap && ocap->cap_id == p_cap_id) {
3733 dout(" remove export cap %p mds%d flags %d\n",
3734 ocap, peer, ph->flags);
3735 if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
3736 (ocap->seq != le32_to_cpu(ph->seq) ||
3737 ocap->mseq != le32_to_cpu(ph->mseq))) {
3738 pr_err_ratelimited("handle_cap_import: "
3739 "mismatched seq/mseq: ino (%llx.%llx) "
3740 "mds%d seq %d mseq %d importer mds%d "
3741 "has peer seq %d mseq %d\n",
3742 ceph_vinop(inode), peer, ocap->seq,
3743 ocap->mseq, mds, le32_to_cpu(ph->seq),
3744 le32_to_cpu(ph->mseq));
3746 __ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
3749 /* make sure we re-request max_size, if necessary */
3750 ci->i_requested_max_size = 0;
3752 *old_issued = issued;
3757 * Handle a caps message from the MDS.
3759 * Identify the appropriate session, inode, and call the right handler
3760 * based on the cap op.
3762 void ceph_handle_caps(struct ceph_mds_session *session,
3763 struct ceph_msg *msg)
3765 struct ceph_mds_client *mdsc = session->s_mdsc;
3766 struct inode *inode;
3767 struct ceph_inode_info *ci;
3768 struct ceph_cap *cap;
3769 struct ceph_mds_caps *h;
3770 struct ceph_mds_cap_peer *peer = NULL;
3771 struct ceph_snap_realm *realm = NULL;
3773 int msg_version = le16_to_cpu(msg->hdr.version);
3775 struct ceph_vino vino;
3777 size_t snaptrace_len;
3779 struct cap_extra_info extra_info = {};
3781 dout("handle_caps from mds%d\n", session->s_mds);
3784 end = msg->front.iov_base + msg->front.iov_len;
3785 if (msg->front.iov_len < sizeof(*h))
3787 h = msg->front.iov_base;
3788 op = le32_to_cpu(h->op);
3789 vino.ino = le64_to_cpu(h->ino);
3790 vino.snap = CEPH_NOSNAP;
3791 seq = le32_to_cpu(h->seq);
3792 mseq = le32_to_cpu(h->migrate_seq);
3795 snaptrace_len = le32_to_cpu(h->snap_trace_len);
3796 p = snaptrace + snaptrace_len;
3798 if (msg_version >= 2) {
3800 ceph_decode_32_safe(&p, end, flock_len, bad);
3801 if (p + flock_len > end)
3806 if (msg_version >= 3) {
3807 if (op == CEPH_CAP_OP_IMPORT) {
3808 if (p + sizeof(*peer) > end)
3812 } else if (op == CEPH_CAP_OP_EXPORT) {
3813 /* recorded in unused fields */
3814 peer = (void *)&h->size;
3818 if (msg_version >= 4) {
3819 ceph_decode_64_safe(&p, end, extra_info.inline_version, bad);
3820 ceph_decode_32_safe(&p, end, extra_info.inline_len, bad);
3821 if (p + extra_info.inline_len > end)
3823 extra_info.inline_data = p;
3824 p += extra_info.inline_len;
3827 if (msg_version >= 5) {
3828 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
3831 ceph_decode_32_safe(&p, end, epoch_barrier, bad);
3832 ceph_osdc_update_epoch_barrier(osdc, epoch_barrier);
3835 if (msg_version >= 8) {
3837 u32 caller_uid, caller_gid;
3841 ceph_decode_64_safe(&p, end, flush_tid, bad);
3843 ceph_decode_32_safe(&p, end, caller_uid, bad);
3844 ceph_decode_32_safe(&p, end, caller_gid, bad);
3846 ceph_decode_32_safe(&p, end, pool_ns_len, bad);
3847 if (pool_ns_len > 0) {
3848 ceph_decode_need(&p, end, pool_ns_len, bad);
3849 extra_info.pool_ns =
3850 ceph_find_or_create_string(p, pool_ns_len);
3855 if (msg_version >= 11) {
3856 struct ceph_timespec *btime;
3861 if (p + sizeof(*btime) > end)
3864 p += sizeof(*btime);
3865 ceph_decode_64_safe(&p, end, change_attr, bad);
3867 ceph_decode_32_safe(&p, end, flags, bad);
3869 extra_info.dirstat_valid = true;
3870 ceph_decode_64_safe(&p, end, extra_info.nfiles, bad);
3871 ceph_decode_64_safe(&p, end, extra_info.nsubdirs, bad);
3875 inode = ceph_find_inode(mdsc->fsc->sb, vino);
3876 ci = ceph_inode(inode);
3877 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
3880 mutex_lock(&session->s_mutex);
3882 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
3886 dout(" i don't have ino %llx\n", vino.ino);
3888 if (op == CEPH_CAP_OP_IMPORT) {
3889 cap = ceph_get_cap(mdsc, NULL);
3890 cap->cap_ino = vino.ino;
3891 cap->queue_release = 1;
3892 cap->cap_id = le64_to_cpu(h->cap_id);
3895 cap->issue_seq = seq;
3896 spin_lock(&session->s_cap_lock);
3897 list_add_tail(&cap->session_caps,
3898 &session->s_cap_releases);
3899 session->s_num_cap_releases++;
3900 spin_unlock(&session->s_cap_lock);
3902 goto flush_cap_releases;
3905 /* these will work even if we don't have a cap yet */
3907 case CEPH_CAP_OP_FLUSHSNAP_ACK:
3908 handle_cap_flushsnap_ack(inode, le64_to_cpu(msg->hdr.tid),
3912 case CEPH_CAP_OP_EXPORT:
3913 handle_cap_export(inode, h, peer, session);
3916 case CEPH_CAP_OP_IMPORT:
3918 if (snaptrace_len) {
3919 down_write(&mdsc->snap_rwsem);
3920 ceph_update_snap_trace(mdsc, snaptrace,
3921 snaptrace + snaptrace_len,
3923 downgrade_write(&mdsc->snap_rwsem);
3925 down_read(&mdsc->snap_rwsem);
3927 handle_cap_import(mdsc, inode, h, peer, session,
3928 &cap, &extra_info.issued);
3929 handle_cap_grant(inode, session, cap,
3930 h, msg->middle, &extra_info);
3932 ceph_put_snap_realm(mdsc, realm);
3936 /* the rest require a cap */
3937 spin_lock(&ci->i_ceph_lock);
3938 cap = __get_cap_for_mds(ceph_inode(inode), session->s_mds);
3940 dout(" no cap on %p ino %llx.%llx from mds%d\n",
3941 inode, ceph_ino(inode), ceph_snap(inode),
3943 spin_unlock(&ci->i_ceph_lock);
3944 goto flush_cap_releases;
3947 /* note that each of these drops i_ceph_lock for us */
3949 case CEPH_CAP_OP_REVOKE:
3950 case CEPH_CAP_OP_GRANT:
3951 __ceph_caps_issued(ci, &extra_info.issued);
3952 extra_info.issued |= __ceph_caps_dirty(ci);
3953 handle_cap_grant(inode, session, cap,
3954 h, msg->middle, &extra_info);
3957 case CEPH_CAP_OP_FLUSH_ACK:
3958 handle_cap_flush_ack(inode, le64_to_cpu(msg->hdr.tid),
3962 case CEPH_CAP_OP_TRUNC:
3963 handle_cap_trunc(inode, h, session);
3967 spin_unlock(&ci->i_ceph_lock);
3968 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
3969 ceph_cap_op_name(op));
3976 * send any cap release message to try to move things
3977 * along for the mds (who clearly thinks we still have this
3980 ceph_send_cap_releases(mdsc, session);
3983 mutex_unlock(&session->s_mutex);
3986 ceph_put_string(extra_info.pool_ns);
3990 pr_err("ceph_handle_caps: corrupt message\n");
3996 * Delayed work handler to process end of delayed cap release LRU list.
3998 void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
4000 struct inode *inode;
4001 struct ceph_inode_info *ci;
4002 int flags = CHECK_CAPS_NODELAY;
4004 dout("check_delayed_caps\n");
4006 spin_lock(&mdsc->cap_delay_lock);
4007 if (list_empty(&mdsc->cap_delay_list))
4009 ci = list_first_entry(&mdsc->cap_delay_list,
4010 struct ceph_inode_info,
4012 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
4013 time_before(jiffies, ci->i_hold_caps_max))
4015 list_del_init(&ci->i_cap_delay_list);
4017 inode = igrab(&ci->vfs_inode);
4018 spin_unlock(&mdsc->cap_delay_lock);
4021 dout("check_delayed_caps on %p\n", inode);
4022 ceph_check_caps(ci, flags, NULL);
4026 spin_unlock(&mdsc->cap_delay_lock);
4030 * Flush all dirty caps to the mds
4032 void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
4034 struct ceph_inode_info *ci;
4035 struct inode *inode;
4037 dout("flush_dirty_caps\n");
4038 spin_lock(&mdsc->cap_dirty_lock);
4039 while (!list_empty(&mdsc->cap_dirty)) {
4040 ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
4042 inode = &ci->vfs_inode;
4044 dout("flush_dirty_caps %p\n", inode);
4045 spin_unlock(&mdsc->cap_dirty_lock);
4046 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
4048 spin_lock(&mdsc->cap_dirty_lock);
4050 spin_unlock(&mdsc->cap_dirty_lock);
4051 dout("flush_dirty_caps done\n");
4054 void __ceph_get_fmode(struct ceph_inode_info *ci, int fmode)
4057 int bits = (fmode << 1) | 1;
4058 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
4059 if (bits & (1 << i))
4060 ci->i_nr_by_mode[i]++;
4065 * Drop open file reference. If we were the last open file,
4066 * we may need to release capabilities to the MDS (or schedule
4067 * their delayed release).
4069 void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
4072 int bits = (fmode << 1) | 1;
4073 spin_lock(&ci->i_ceph_lock);
4074 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
4075 if (bits & (1 << i)) {
4076 BUG_ON(ci->i_nr_by_mode[i] == 0);
4077 if (--ci->i_nr_by_mode[i] == 0)
4081 dout("put_fmode %p fmode %d {%d,%d,%d,%d}\n",
4082 &ci->vfs_inode, fmode,
4083 ci->i_nr_by_mode[0], ci->i_nr_by_mode[1],
4084 ci->i_nr_by_mode[2], ci->i_nr_by_mode[3]);
4085 spin_unlock(&ci->i_ceph_lock);
4087 if (last && ci->i_vino.snap == CEPH_NOSNAP)
4088 ceph_check_caps(ci, 0, NULL);
4092 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
4093 * looks like the link count will hit 0, drop any other caps (other
4094 * than PIN) we don't specifically want (due to the file still being
4097 int ceph_drop_caps_for_unlink(struct inode *inode)
4099 struct ceph_inode_info *ci = ceph_inode(inode);
4100 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
4102 spin_lock(&ci->i_ceph_lock);
4103 if (inode->i_nlink == 1) {
4104 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
4106 ci->i_ceph_flags |= CEPH_I_NODELAY;
4107 if (__ceph_caps_dirty(ci)) {
4108 struct ceph_mds_client *mdsc =
4109 ceph_inode_to_client(inode)->mdsc;
4110 __cap_delay_requeue_front(mdsc, ci);
4113 spin_unlock(&ci->i_ceph_lock);
4118 * Helpers for embedding cap and dentry lease releases into mds
4121 * @force is used by dentry_release (below) to force inclusion of a
4122 * record for the directory inode, even when there aren't any caps to
4125 int ceph_encode_inode_release(void **p, struct inode *inode,
4126 int mds, int drop, int unless, int force)
4128 struct ceph_inode_info *ci = ceph_inode(inode);
4129 struct ceph_cap *cap;
4130 struct ceph_mds_request_release *rel = *p;
4134 spin_lock(&ci->i_ceph_lock);
4135 used = __ceph_caps_used(ci);
4136 dirty = __ceph_caps_dirty(ci);
4138 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
4139 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
4140 ceph_cap_string(unless));
4142 /* only drop unused, clean caps */
4143 drop &= ~(used | dirty);
4145 cap = __get_cap_for_mds(ci, mds);
4146 if (cap && __cap_is_valid(cap)) {
4147 unless &= cap->issued;
4149 if (unless & CEPH_CAP_AUTH_EXCL)
4150 drop &= ~CEPH_CAP_AUTH_SHARED;
4151 if (unless & CEPH_CAP_LINK_EXCL)
4152 drop &= ~CEPH_CAP_LINK_SHARED;
4153 if (unless & CEPH_CAP_XATTR_EXCL)
4154 drop &= ~CEPH_CAP_XATTR_SHARED;
4155 if (unless & CEPH_CAP_FILE_EXCL)
4156 drop &= ~CEPH_CAP_FILE_SHARED;
4159 if (force || (cap->issued & drop)) {
4160 if (cap->issued & drop) {
4161 int wanted = __ceph_caps_wanted(ci);
4162 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0)
4163 wanted |= cap->mds_wanted;
4164 dout("encode_inode_release %p cap %p "
4165 "%s -> %s, wanted %s -> %s\n", inode, cap,
4166 ceph_cap_string(cap->issued),
4167 ceph_cap_string(cap->issued & ~drop),
4168 ceph_cap_string(cap->mds_wanted),
4169 ceph_cap_string(wanted));
4171 cap->issued &= ~drop;
4172 cap->implemented &= ~drop;
4173 cap->mds_wanted = wanted;
4175 dout("encode_inode_release %p cap %p %s"
4176 " (force)\n", inode, cap,
4177 ceph_cap_string(cap->issued));
4180 rel->ino = cpu_to_le64(ceph_ino(inode));
4181 rel->cap_id = cpu_to_le64(cap->cap_id);
4182 rel->seq = cpu_to_le32(cap->seq);
4183 rel->issue_seq = cpu_to_le32(cap->issue_seq);
4184 rel->mseq = cpu_to_le32(cap->mseq);
4185 rel->caps = cpu_to_le32(cap->implemented);
4186 rel->wanted = cpu_to_le32(cap->mds_wanted);
4192 dout("encode_inode_release %p cap %p %s (noop)\n",
4193 inode, cap, ceph_cap_string(cap->issued));
4196 spin_unlock(&ci->i_ceph_lock);
4200 int ceph_encode_dentry_release(void **p, struct dentry *dentry,
4202 int mds, int drop, int unless)
4204 struct dentry *parent = NULL;
4205 struct ceph_mds_request_release *rel = *p;
4206 struct ceph_dentry_info *di = ceph_dentry(dentry);
4211 * force an record for the directory caps if we have a dentry lease.
4212 * this is racy (can't take i_ceph_lock and d_lock together), but it
4213 * doesn't have to be perfect; the mds will revoke anything we don't
4216 spin_lock(&dentry->d_lock);
4217 if (di->lease_session && di->lease_session->s_mds == mds)
4220 parent = dget(dentry->d_parent);
4221 dir = d_inode(parent);
4223 spin_unlock(&dentry->d_lock);
4225 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
4228 spin_lock(&dentry->d_lock);
4229 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
4230 dout("encode_dentry_release %p mds%d seq %d\n",
4231 dentry, mds, (int)di->lease_seq);
4232 rel->dname_len = cpu_to_le32(dentry->d_name.len);
4233 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
4234 *p += dentry->d_name.len;
4235 rel->dname_seq = cpu_to_le32(di->lease_seq);
4236 __ceph_mdsc_drop_dentry_lease(dentry);
4238 spin_unlock(&dentry->d_lock);