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);
160 * Called under mdsc->mutex.
162 int ceph_reserve_caps(struct ceph_mds_client *mdsc,
163 struct ceph_cap_reservation *ctx, int need)
166 struct ceph_cap *cap;
170 bool trimmed = false;
171 struct ceph_mds_session *s;
174 dout("reserve caps ctx=%p need=%d\n", ctx, need);
176 /* first reserve any caps that are already allocated */
177 spin_lock(&mdsc->caps_list_lock);
178 if (mdsc->caps_avail_count >= need)
181 have = mdsc->caps_avail_count;
182 mdsc->caps_avail_count -= have;
183 mdsc->caps_reserve_count += have;
184 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
185 mdsc->caps_reserve_count +
186 mdsc->caps_avail_count);
187 spin_unlock(&mdsc->caps_list_lock);
189 for (i = have; i < need; ) {
190 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
192 list_add(&cap->caps_item, &newcaps);
199 for (j = 0; j < mdsc->max_sessions; j++) {
200 s = __ceph_lookup_mds_session(mdsc, j);
203 mutex_unlock(&mdsc->mutex);
205 mutex_lock(&s->s_mutex);
206 max_caps = s->s_nr_caps - (need - i);
207 ceph_trim_caps(mdsc, s, max_caps);
208 mutex_unlock(&s->s_mutex);
210 ceph_put_mds_session(s);
211 mutex_lock(&mdsc->mutex);
215 spin_lock(&mdsc->caps_list_lock);
216 if (mdsc->caps_avail_count) {
218 if (mdsc->caps_avail_count >= need - i)
219 more_have = need - i;
221 more_have = mdsc->caps_avail_count;
225 mdsc->caps_avail_count -= more_have;
226 mdsc->caps_reserve_count += more_have;
229 spin_unlock(&mdsc->caps_list_lock);
234 pr_warn("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
235 ctx, need, have + alloc);
238 BUG_ON(have + alloc != need);
240 spin_lock(&mdsc->caps_list_lock);
241 mdsc->caps_total_count += alloc;
242 mdsc->caps_reserve_count += alloc;
243 list_splice(&newcaps, &mdsc->caps_list);
245 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
246 mdsc->caps_reserve_count +
247 mdsc->caps_avail_count);
248 spin_unlock(&mdsc->caps_list_lock);
251 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
252 ctx, mdsc->caps_total_count, mdsc->caps_use_count,
253 mdsc->caps_reserve_count, mdsc->caps_avail_count);
258 spin_lock(&mdsc->caps_list_lock);
259 mdsc->caps_avail_count += have;
260 mdsc->caps_reserve_count -= have;
262 while (!list_empty(&newcaps)) {
263 cap = list_first_entry(&newcaps,
264 struct ceph_cap, caps_item);
265 list_del(&cap->caps_item);
267 /* Keep some preallocated caps around (ceph_min_count), to
268 * avoid lots of free/alloc churn. */
269 if (mdsc->caps_avail_count >=
270 mdsc->caps_reserve_count + mdsc->caps_min_count) {
271 kmem_cache_free(ceph_cap_cachep, cap);
273 mdsc->caps_avail_count++;
274 mdsc->caps_total_count++;
275 list_add(&cap->caps_item, &mdsc->caps_list);
279 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
280 mdsc->caps_reserve_count +
281 mdsc->caps_avail_count);
282 spin_unlock(&mdsc->caps_list_lock);
286 int ceph_unreserve_caps(struct ceph_mds_client *mdsc,
287 struct ceph_cap_reservation *ctx)
290 struct ceph_cap *cap;
292 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
294 spin_lock(&mdsc->caps_list_lock);
295 BUG_ON(mdsc->caps_reserve_count < ctx->count);
296 mdsc->caps_reserve_count -= ctx->count;
297 if (mdsc->caps_avail_count >=
298 mdsc->caps_reserve_count + mdsc->caps_min_count) {
299 mdsc->caps_total_count -= ctx->count;
300 for (i = 0; i < ctx->count; i++) {
301 cap = list_first_entry(&mdsc->caps_list,
302 struct ceph_cap, caps_item);
303 list_del(&cap->caps_item);
304 kmem_cache_free(ceph_cap_cachep, cap);
307 mdsc->caps_avail_count += ctx->count;
310 dout("unreserve caps %d = %d used + %d resv + %d avail\n",
311 mdsc->caps_total_count, mdsc->caps_use_count,
312 mdsc->caps_reserve_count, mdsc->caps_avail_count);
313 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
314 mdsc->caps_reserve_count +
315 mdsc->caps_avail_count);
316 spin_unlock(&mdsc->caps_list_lock);
321 struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc,
322 struct ceph_cap_reservation *ctx)
324 struct ceph_cap *cap = NULL;
326 /* temporary, until we do something about cap import/export */
328 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
330 spin_lock(&mdsc->caps_list_lock);
331 mdsc->caps_use_count++;
332 mdsc->caps_total_count++;
333 spin_unlock(&mdsc->caps_list_lock);
335 spin_lock(&mdsc->caps_list_lock);
336 if (mdsc->caps_avail_count) {
337 BUG_ON(list_empty(&mdsc->caps_list));
339 mdsc->caps_avail_count--;
340 mdsc->caps_use_count++;
341 cap = list_first_entry(&mdsc->caps_list,
342 struct ceph_cap, caps_item);
343 list_del(&cap->caps_item);
345 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
346 mdsc->caps_reserve_count + mdsc->caps_avail_count);
348 spin_unlock(&mdsc->caps_list_lock);
354 spin_lock(&mdsc->caps_list_lock);
355 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
356 ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
357 mdsc->caps_reserve_count, mdsc->caps_avail_count);
359 BUG_ON(ctx->count > mdsc->caps_reserve_count);
360 BUG_ON(list_empty(&mdsc->caps_list));
363 mdsc->caps_reserve_count--;
364 mdsc->caps_use_count++;
366 cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
367 list_del(&cap->caps_item);
369 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
370 mdsc->caps_reserve_count + mdsc->caps_avail_count);
371 spin_unlock(&mdsc->caps_list_lock);
375 void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
377 spin_lock(&mdsc->caps_list_lock);
378 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
379 cap, mdsc->caps_total_count, mdsc->caps_use_count,
380 mdsc->caps_reserve_count, mdsc->caps_avail_count);
381 mdsc->caps_use_count--;
383 * Keep some preallocated caps around (ceph_min_count), to
384 * avoid lots of free/alloc churn.
386 if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
387 mdsc->caps_min_count) {
388 mdsc->caps_total_count--;
389 kmem_cache_free(ceph_cap_cachep, cap);
391 mdsc->caps_avail_count++;
392 list_add(&cap->caps_item, &mdsc->caps_list);
395 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
396 mdsc->caps_reserve_count + mdsc->caps_avail_count);
397 spin_unlock(&mdsc->caps_list_lock);
400 void ceph_reservation_status(struct ceph_fs_client *fsc,
401 int *total, int *avail, int *used, int *reserved,
404 struct ceph_mds_client *mdsc = fsc->mdsc;
406 spin_lock(&mdsc->caps_list_lock);
409 *total = mdsc->caps_total_count;
411 *avail = mdsc->caps_avail_count;
413 *used = mdsc->caps_use_count;
415 *reserved = mdsc->caps_reserve_count;
417 *min = mdsc->caps_min_count;
419 spin_unlock(&mdsc->caps_list_lock);
423 * Find ceph_cap for given mds, if any.
425 * Called with i_ceph_lock held.
427 static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
429 struct ceph_cap *cap;
430 struct rb_node *n = ci->i_caps.rb_node;
433 cap = rb_entry(n, struct ceph_cap, ci_node);
436 else if (mds > cap->mds)
444 struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
446 struct ceph_cap *cap;
448 spin_lock(&ci->i_ceph_lock);
449 cap = __get_cap_for_mds(ci, mds);
450 spin_unlock(&ci->i_ceph_lock);
455 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
457 static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
459 struct ceph_cap *cap;
463 /* prefer mds with WR|BUFFER|EXCL caps */
464 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
465 cap = rb_entry(p, struct ceph_cap, ci_node);
467 if (cap->issued & (CEPH_CAP_FILE_WR |
468 CEPH_CAP_FILE_BUFFER |
475 int ceph_get_cap_mds(struct inode *inode)
477 struct ceph_inode_info *ci = ceph_inode(inode);
479 spin_lock(&ci->i_ceph_lock);
480 mds = __ceph_get_cap_mds(ceph_inode(inode));
481 spin_unlock(&ci->i_ceph_lock);
486 * Called under i_ceph_lock.
488 static void __insert_cap_node(struct ceph_inode_info *ci,
489 struct ceph_cap *new)
491 struct rb_node **p = &ci->i_caps.rb_node;
492 struct rb_node *parent = NULL;
493 struct ceph_cap *cap = NULL;
497 cap = rb_entry(parent, struct ceph_cap, ci_node);
498 if (new->mds < cap->mds)
500 else if (new->mds > cap->mds)
506 rb_link_node(&new->ci_node, parent, p);
507 rb_insert_color(&new->ci_node, &ci->i_caps);
511 * (re)set cap hold timeouts, which control the delayed release
512 * of unused caps back to the MDS. Should be called on cap use.
514 static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
515 struct ceph_inode_info *ci)
517 struct ceph_mount_options *ma = mdsc->fsc->mount_options;
519 ci->i_hold_caps_min = round_jiffies(jiffies +
520 ma->caps_wanted_delay_min * HZ);
521 ci->i_hold_caps_max = round_jiffies(jiffies +
522 ma->caps_wanted_delay_max * HZ);
523 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
524 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
528 * (Re)queue cap at the end of the delayed cap release list.
530 * If I_FLUSH is set, leave the inode at the front of the list.
532 * Caller holds i_ceph_lock
533 * -> we take mdsc->cap_delay_lock
535 static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
536 struct ceph_inode_info *ci)
538 __cap_set_timeouts(mdsc, ci);
539 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
540 ci->i_ceph_flags, ci->i_hold_caps_max);
541 if (!mdsc->stopping) {
542 spin_lock(&mdsc->cap_delay_lock);
543 if (!list_empty(&ci->i_cap_delay_list)) {
544 if (ci->i_ceph_flags & CEPH_I_FLUSH)
546 list_del_init(&ci->i_cap_delay_list);
548 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
550 spin_unlock(&mdsc->cap_delay_lock);
555 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
556 * indicating we should send a cap message to flush dirty metadata
557 * asap, and move to the front of the delayed cap list.
559 static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
560 struct ceph_inode_info *ci)
562 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
563 spin_lock(&mdsc->cap_delay_lock);
564 ci->i_ceph_flags |= CEPH_I_FLUSH;
565 if (!list_empty(&ci->i_cap_delay_list))
566 list_del_init(&ci->i_cap_delay_list);
567 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
568 spin_unlock(&mdsc->cap_delay_lock);
572 * Cancel delayed work on cap.
574 * Caller must hold i_ceph_lock.
576 static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
577 struct ceph_inode_info *ci)
579 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
580 if (list_empty(&ci->i_cap_delay_list))
582 spin_lock(&mdsc->cap_delay_lock);
583 list_del_init(&ci->i_cap_delay_list);
584 spin_unlock(&mdsc->cap_delay_lock);
588 * Common issue checks for add_cap, handle_cap_grant.
590 static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
593 unsigned had = __ceph_caps_issued(ci, NULL);
596 * Each time we receive FILE_CACHE anew, we increment
599 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
600 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) {
605 * If FILE_SHARED is newly issued, mark dir not complete. We don't
606 * know what happened to this directory while we didn't have the cap.
607 * If FILE_SHARED is being revoked, also mark dir not complete. It
608 * stops on-going cached readdir.
610 if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) {
611 if (issued & CEPH_CAP_FILE_SHARED)
612 atomic_inc(&ci->i_shared_gen);
613 if (S_ISDIR(ci->vfs_inode.i_mode)) {
614 dout(" marking %p NOT complete\n", &ci->vfs_inode);
615 __ceph_dir_clear_complete(ci);
621 * Add a capability under the given MDS session.
623 * Caller should hold session snap_rwsem (read) and s_mutex.
625 * @fmode is the open file mode, if we are opening a file, otherwise
626 * it is < 0. (This is so we can atomically add the cap and add an
627 * open file reference to it.)
629 void ceph_add_cap(struct inode *inode,
630 struct ceph_mds_session *session, u64 cap_id,
631 int fmode, unsigned issued, unsigned wanted,
632 unsigned seq, unsigned mseq, u64 realmino, int flags,
633 struct ceph_cap **new_cap)
635 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
636 struct ceph_inode_info *ci = ceph_inode(inode);
637 struct ceph_cap *cap;
638 int mds = session->s_mds;
641 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
642 session->s_mds, cap_id, ceph_cap_string(issued), seq);
645 * If we are opening the file, include file mode wanted bits
649 wanted |= ceph_caps_for_mode(fmode);
651 cap = __get_cap_for_mds(ci, mds);
657 cap->implemented = 0;
663 __insert_cap_node(ci, cap);
665 /* add to session cap list */
666 cap->session = session;
667 spin_lock(&session->s_cap_lock);
668 list_add_tail(&cap->session_caps, &session->s_caps);
669 session->s_nr_caps++;
670 spin_unlock(&session->s_cap_lock);
673 * auth mds of the inode changed. we received the cap export
674 * message, but still haven't received the cap import message.
675 * handle_cap_export() updated the new auth MDS' cap.
677 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
678 * a message that was send before the cap import message. So
681 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
682 WARN_ON(cap != ci->i_auth_cap);
683 WARN_ON(cap->cap_id != cap_id);
686 issued |= cap->issued;
687 flags |= CEPH_CAP_FLAG_AUTH;
691 if (!ci->i_snap_realm ||
692 ((flags & CEPH_CAP_FLAG_AUTH) &&
693 realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) {
695 * add this inode to the appropriate snap realm
697 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
700 struct ceph_snap_realm *oldrealm = ci->i_snap_realm;
702 spin_lock(&oldrealm->inodes_with_caps_lock);
703 list_del_init(&ci->i_snap_realm_item);
704 spin_unlock(&oldrealm->inodes_with_caps_lock);
707 spin_lock(&realm->inodes_with_caps_lock);
708 list_add(&ci->i_snap_realm_item,
709 &realm->inodes_with_caps);
710 ci->i_snap_realm = realm;
711 if (realm->ino == ci->i_vino.ino)
712 realm->inode = inode;
713 spin_unlock(&realm->inodes_with_caps_lock);
716 ceph_put_snap_realm(mdsc, oldrealm);
718 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
724 __check_cap_issue(ci, cap, issued);
727 * If we are issued caps we don't want, or the mds' wanted
728 * value appears to be off, queue a check so we'll release
729 * later and/or update the mds wanted value.
731 actual_wanted = __ceph_caps_wanted(ci);
732 if ((wanted & ~actual_wanted) ||
733 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
734 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
735 ceph_cap_string(issued), ceph_cap_string(wanted),
736 ceph_cap_string(actual_wanted));
737 __cap_delay_requeue(mdsc, ci);
740 if (flags & CEPH_CAP_FLAG_AUTH) {
741 if (!ci->i_auth_cap ||
742 ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
743 ci->i_auth_cap = cap;
744 cap->mds_wanted = wanted;
747 WARN_ON(ci->i_auth_cap == cap);
750 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
751 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
752 ceph_cap_string(issued|cap->issued), seq, mds);
753 cap->cap_id = cap_id;
754 cap->issued = issued;
755 cap->implemented |= issued;
756 if (ceph_seq_cmp(mseq, cap->mseq) > 0)
757 cap->mds_wanted = wanted;
759 cap->mds_wanted |= wanted;
761 cap->issue_seq = seq;
763 cap->cap_gen = session->s_cap_gen;
766 __ceph_get_fmode(ci, fmode);
770 * Return true if cap has not timed out and belongs to the current
771 * generation of the MDS session (i.e. has not gone 'stale' due to
772 * us losing touch with the mds).
774 static int __cap_is_valid(struct ceph_cap *cap)
779 spin_lock(&cap->session->s_gen_ttl_lock);
780 gen = cap->session->s_cap_gen;
781 ttl = cap->session->s_cap_ttl;
782 spin_unlock(&cap->session->s_gen_ttl_lock);
784 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
785 dout("__cap_is_valid %p cap %p issued %s "
786 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
787 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
795 * Return set of valid cap bits issued to us. Note that caps time
796 * out, and may be invalidated in bulk if the client session times out
797 * and session->s_cap_gen is bumped.
799 int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
801 int have = ci->i_snap_caps;
802 struct ceph_cap *cap;
807 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
808 cap = rb_entry(p, struct ceph_cap, ci_node);
809 if (!__cap_is_valid(cap))
811 dout("__ceph_caps_issued %p cap %p issued %s\n",
812 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
815 *implemented |= cap->implemented;
818 * exclude caps issued by non-auth MDS, but are been revoking
819 * by the auth MDS. The non-auth MDS should be revoking/exporting
820 * these caps, but the message is delayed.
822 if (ci->i_auth_cap) {
823 cap = ci->i_auth_cap;
824 have &= ~cap->implemented | cap->issued;
830 * Get cap bits issued by caps other than @ocap
832 int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
834 int have = ci->i_snap_caps;
835 struct ceph_cap *cap;
838 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
839 cap = rb_entry(p, struct ceph_cap, ci_node);
842 if (!__cap_is_valid(cap))
850 * Move a cap to the end of the LRU (oldest caps at list head, newest
853 static void __touch_cap(struct ceph_cap *cap)
855 struct ceph_mds_session *s = cap->session;
857 spin_lock(&s->s_cap_lock);
858 if (!s->s_cap_iterator) {
859 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
861 list_move_tail(&cap->session_caps, &s->s_caps);
863 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
864 &cap->ci->vfs_inode, cap, s->s_mds);
866 spin_unlock(&s->s_cap_lock);
870 * Check if we hold the given mask. If so, move the cap(s) to the
871 * front of their respective LRUs. (This is the preferred way for
872 * callers to check for caps they want.)
874 int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
876 struct ceph_cap *cap;
878 int have = ci->i_snap_caps;
880 if ((have & mask) == mask) {
881 dout("__ceph_caps_issued_mask %p snap issued %s"
882 " (mask %s)\n", &ci->vfs_inode,
883 ceph_cap_string(have),
884 ceph_cap_string(mask));
888 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
889 cap = rb_entry(p, struct ceph_cap, ci_node);
890 if (!__cap_is_valid(cap))
892 if ((cap->issued & mask) == mask) {
893 dout("__ceph_caps_issued_mask %p cap %p issued %s"
894 " (mask %s)\n", &ci->vfs_inode, cap,
895 ceph_cap_string(cap->issued),
896 ceph_cap_string(mask));
902 /* does a combination of caps satisfy mask? */
904 if ((have & mask) == mask) {
905 dout("__ceph_caps_issued_mask %p combo issued %s"
906 " (mask %s)\n", &ci->vfs_inode,
907 ceph_cap_string(cap->issued),
908 ceph_cap_string(mask));
912 /* touch this + preceding caps */
914 for (q = rb_first(&ci->i_caps); q != p;
916 cap = rb_entry(q, struct ceph_cap,
918 if (!__cap_is_valid(cap))
931 * Return true if mask caps are currently being revoked by an MDS.
933 int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
934 struct ceph_cap *ocap, int mask)
936 struct ceph_cap *cap;
939 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
940 cap = rb_entry(p, struct ceph_cap, ci_node);
942 (cap->implemented & ~cap->issued & mask))
948 int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
950 struct inode *inode = &ci->vfs_inode;
953 spin_lock(&ci->i_ceph_lock);
954 ret = __ceph_caps_revoking_other(ci, NULL, mask);
955 spin_unlock(&ci->i_ceph_lock);
956 dout("ceph_caps_revoking %p %s = %d\n", inode,
957 ceph_cap_string(mask), ret);
961 int __ceph_caps_used(struct ceph_inode_info *ci)
965 used |= CEPH_CAP_PIN;
967 used |= CEPH_CAP_FILE_RD;
968 if (ci->i_rdcache_ref ||
969 (!S_ISDIR(ci->vfs_inode.i_mode) && /* ignore readdir cache */
970 ci->vfs_inode.i_data.nrpages))
971 used |= CEPH_CAP_FILE_CACHE;
973 used |= CEPH_CAP_FILE_WR;
974 if (ci->i_wb_ref || ci->i_wrbuffer_ref)
975 used |= CEPH_CAP_FILE_BUFFER;
980 * wanted, by virtue of open file modes
982 int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
985 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
986 if (ci->i_nr_by_mode[i])
991 return ceph_caps_for_mode(bits >> 1);
995 * Return caps we have registered with the MDS(s) as 'wanted'.
997 int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check)
999 struct ceph_cap *cap;
1003 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1004 cap = rb_entry(p, struct ceph_cap, ci_node);
1005 if (check && !__cap_is_valid(cap))
1007 if (cap == ci->i_auth_cap)
1008 mds_wanted |= cap->mds_wanted;
1010 mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
1016 * called under i_ceph_lock
1018 static int __ceph_is_single_caps(struct ceph_inode_info *ci)
1020 return rb_first(&ci->i_caps) == rb_last(&ci->i_caps);
1023 static int __ceph_is_any_caps(struct ceph_inode_info *ci)
1025 return !RB_EMPTY_ROOT(&ci->i_caps);
1028 int ceph_is_any_caps(struct inode *inode)
1030 struct ceph_inode_info *ci = ceph_inode(inode);
1033 spin_lock(&ci->i_ceph_lock);
1034 ret = __ceph_is_any_caps(ci);
1035 spin_unlock(&ci->i_ceph_lock);
1040 static void drop_inode_snap_realm(struct ceph_inode_info *ci)
1042 struct ceph_snap_realm *realm = ci->i_snap_realm;
1043 spin_lock(&realm->inodes_with_caps_lock);
1044 list_del_init(&ci->i_snap_realm_item);
1045 ci->i_snap_realm_counter++;
1046 ci->i_snap_realm = NULL;
1047 spin_unlock(&realm->inodes_with_caps_lock);
1048 ceph_put_snap_realm(ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc,
1053 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
1055 * caller should hold i_ceph_lock.
1056 * caller will not hold session s_mutex if called from destroy_inode.
1058 void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
1060 struct ceph_mds_session *session = cap->session;
1061 struct ceph_inode_info *ci = cap->ci;
1062 struct ceph_mds_client *mdsc =
1063 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
1066 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
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);
1103 /* remove from inode list */
1104 rb_erase(&cap->ci_node, &ci->i_caps);
1105 if (ci->i_auth_cap == cap)
1106 ci->i_auth_cap = NULL;
1109 ceph_put_cap(mdsc, cap);
1111 /* when reconnect denied, we remove session caps forcibly,
1112 * i_wr_ref can be non-zero. If there are ongoing write,
1113 * keep i_snap_realm.
1115 if (!__ceph_is_any_caps(ci) && ci->i_wr_ref == 0 && ci->i_snap_realm)
1116 drop_inode_snap_realm(ci);
1118 if (!__ceph_is_any_real_caps(ci))
1119 __cap_delay_cancel(mdsc, ci);
1122 struct cap_msg_args {
1123 struct ceph_mds_session *session;
1124 u64 ino, cid, follows;
1125 u64 flush_tid, oldest_flush_tid, size, max_size;
1127 struct ceph_buffer *xattr_buf;
1128 struct timespec atime, mtime, ctime;
1129 int op, caps, wanted, dirty;
1130 u32 seq, issue_seq, mseq, time_warp_seq;
1139 * Build and send a cap message to the given MDS.
1141 * Caller should be holding s_mutex.
1143 static int send_cap_msg(struct cap_msg_args *arg)
1145 struct ceph_mds_caps *fc;
1146 struct ceph_msg *msg;
1149 struct timespec zerotime = {0};
1150 struct ceph_osd_client *osdc = &arg->session->s_mdsc->fsc->client->osdc;
1152 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
1153 " seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu"
1154 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(arg->op),
1155 arg->cid, arg->ino, ceph_cap_string(arg->caps),
1156 ceph_cap_string(arg->wanted), ceph_cap_string(arg->dirty),
1157 arg->seq, arg->issue_seq, arg->flush_tid, arg->oldest_flush_tid,
1158 arg->mseq, arg->follows, arg->size, arg->max_size,
1160 arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0);
1162 /* flock buffer size + inline version + inline data size +
1163 * osd_epoch_barrier + oldest_flush_tid */
1164 extra_len = 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4;
1165 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc) + extra_len,
1170 msg->hdr.version = cpu_to_le16(10);
1171 msg->hdr.tid = cpu_to_le64(arg->flush_tid);
1173 fc = msg->front.iov_base;
1174 memset(fc, 0, sizeof(*fc));
1176 fc->cap_id = cpu_to_le64(arg->cid);
1177 fc->op = cpu_to_le32(arg->op);
1178 fc->seq = cpu_to_le32(arg->seq);
1179 fc->issue_seq = cpu_to_le32(arg->issue_seq);
1180 fc->migrate_seq = cpu_to_le32(arg->mseq);
1181 fc->caps = cpu_to_le32(arg->caps);
1182 fc->wanted = cpu_to_le32(arg->wanted);
1183 fc->dirty = cpu_to_le32(arg->dirty);
1184 fc->ino = cpu_to_le64(arg->ino);
1185 fc->snap_follows = cpu_to_le64(arg->follows);
1187 fc->size = cpu_to_le64(arg->size);
1188 fc->max_size = cpu_to_le64(arg->max_size);
1189 ceph_encode_timespec(&fc->mtime, &arg->mtime);
1190 ceph_encode_timespec(&fc->atime, &arg->atime);
1191 ceph_encode_timespec(&fc->ctime, &arg->ctime);
1192 fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq);
1194 fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid));
1195 fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid));
1196 fc->mode = cpu_to_le32(arg->mode);
1198 fc->xattr_version = cpu_to_le64(arg->xattr_version);
1199 if (arg->xattr_buf) {
1200 msg->middle = ceph_buffer_get(arg->xattr_buf);
1201 fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1202 msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1206 /* flock buffer size (version 2) */
1207 ceph_encode_32(&p, 0);
1208 /* inline version (version 4) */
1209 ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE);
1210 /* inline data size */
1211 ceph_encode_32(&p, 0);
1213 * osd_epoch_barrier (version 5)
1214 * The epoch_barrier is protected osdc->lock, so READ_ONCE here in
1215 * case it was recently changed
1217 ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier));
1218 /* oldest_flush_tid (version 6) */
1219 ceph_encode_64(&p, arg->oldest_flush_tid);
1222 * caller_uid/caller_gid (version 7)
1224 * Currently, we don't properly track which caller dirtied the caps
1225 * last, and force a flush of them when there is a conflict. For now,
1226 * just set this to 0:0, to emulate how the MDS has worked up to now.
1228 ceph_encode_32(&p, 0);
1229 ceph_encode_32(&p, 0);
1231 /* pool namespace (version 8) (mds always ignores this) */
1232 ceph_encode_32(&p, 0);
1235 * btime and change_attr (version 9)
1237 * We just zero these out for now, as the MDS ignores them unless
1238 * the requisite feature flags are set (which we don't do yet).
1240 ceph_encode_timespec(p, &zerotime);
1241 p += sizeof(struct ceph_timespec);
1242 ceph_encode_64(&p, 0);
1244 /* Advisory flags (version 10) */
1245 ceph_encode_32(&p, arg->flags);
1247 ceph_con_send(&arg->session->s_con, msg);
1252 * Queue cap releases when an inode is dropped from our cache. Since
1253 * inode is about to be destroyed, there is no need for i_ceph_lock.
1255 void ceph_queue_caps_release(struct inode *inode)
1257 struct ceph_inode_info *ci = ceph_inode(inode);
1260 p = rb_first(&ci->i_caps);
1262 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1264 __ceph_remove_cap(cap, true);
1269 * Send a cap msg on the given inode. Update our caps state, then
1270 * drop i_ceph_lock and send the message.
1272 * Make note of max_size reported/requested from mds, revoked caps
1273 * that have now been implemented.
1275 * Make half-hearted attempt ot to invalidate page cache if we are
1276 * dropping RDCACHE. Note that this will leave behind locked pages
1277 * that we'll then need to deal with elsewhere.
1279 * Return non-zero if delayed release, or we experienced an error
1280 * such that the caller should requeue + retry later.
1282 * called with i_ceph_lock, then drops it.
1283 * caller should hold snap_rwsem (read), s_mutex.
1285 static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1286 int op, bool sync, int used, int want, int retain,
1287 int flushing, u64 flush_tid, u64 oldest_flush_tid)
1288 __releases(cap->ci->i_ceph_lock)
1290 struct ceph_inode_info *ci = cap->ci;
1291 struct inode *inode = &ci->vfs_inode;
1292 struct cap_msg_args arg;
1298 held = cap->issued | cap->implemented;
1299 revoking = cap->implemented & ~cap->issued;
1300 retain &= ~revoking;
1302 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1303 inode, cap, cap->session,
1304 ceph_cap_string(held), ceph_cap_string(held & retain),
1305 ceph_cap_string(revoking));
1306 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1308 arg.session = cap->session;
1310 /* don't release wanted unless we've waited a bit. */
1311 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1312 time_before(jiffies, ci->i_hold_caps_min)) {
1313 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1314 ceph_cap_string(cap->issued),
1315 ceph_cap_string(cap->issued & retain),
1316 ceph_cap_string(cap->mds_wanted),
1317 ceph_cap_string(want));
1318 want |= cap->mds_wanted;
1319 retain |= cap->issued;
1322 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1323 if (want & ~cap->mds_wanted) {
1324 /* user space may open/close single file frequently.
1325 * This avoids droping mds_wanted immediately after
1326 * requesting new mds_wanted.
1328 __cap_set_timeouts(mdsc, ci);
1331 cap->issued &= retain; /* drop bits we don't want */
1332 if (cap->implemented & ~cap->issued) {
1334 * Wake up any waiters on wanted -> needed transition.
1335 * This is due to the weird transition from buffered
1336 * to sync IO... we need to flush dirty pages _before_
1337 * allowing sync writes to avoid reordering.
1341 cap->implemented &= cap->issued | used;
1342 cap->mds_wanted = want;
1344 arg.ino = ceph_vino(inode).ino;
1345 arg.cid = cap->cap_id;
1346 arg.follows = flushing ? ci->i_head_snapc->seq : 0;
1347 arg.flush_tid = flush_tid;
1348 arg.oldest_flush_tid = oldest_flush_tid;
1350 arg.size = inode->i_size;
1351 ci->i_reported_size = arg.size;
1352 arg.max_size = ci->i_wanted_max_size;
1353 ci->i_requested_max_size = arg.max_size;
1355 if (flushing & CEPH_CAP_XATTR_EXCL) {
1356 __ceph_build_xattrs_blob(ci);
1357 arg.xattr_version = ci->i_xattrs.version;
1358 arg.xattr_buf = ci->i_xattrs.blob;
1360 arg.xattr_buf = NULL;
1363 arg.mtime = timespec64_to_timespec(inode->i_mtime);
1364 arg.atime = timespec64_to_timespec(inode->i_atime);
1365 arg.ctime = timespec64_to_timespec(inode->i_ctime);
1368 arg.caps = cap->implemented;
1370 arg.dirty = flushing;
1373 arg.issue_seq = cap->issue_seq;
1374 arg.mseq = cap->mseq;
1375 arg.time_warp_seq = ci->i_time_warp_seq;
1377 arg.uid = inode->i_uid;
1378 arg.gid = inode->i_gid;
1379 arg.mode = inode->i_mode;
1381 arg.inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
1382 if (list_empty(&ci->i_cap_snaps))
1383 arg.flags = CEPH_CLIENT_CAPS_NO_CAPSNAP;
1385 arg.flags = CEPH_CLIENT_CAPS_PENDING_CAPSNAP;
1387 arg.flags |= CEPH_CLIENT_CAPS_SYNC;
1389 spin_unlock(&ci->i_ceph_lock);
1391 ret = send_cap_msg(&arg);
1393 dout("error sending cap msg, must requeue %p\n", inode);
1398 wake_up_all(&ci->i_cap_wq);
1403 static inline int __send_flush_snap(struct inode *inode,
1404 struct ceph_mds_session *session,
1405 struct ceph_cap_snap *capsnap,
1406 u32 mseq, u64 oldest_flush_tid)
1408 struct cap_msg_args arg;
1410 arg.session = session;
1411 arg.ino = ceph_vino(inode).ino;
1413 arg.follows = capsnap->follows;
1414 arg.flush_tid = capsnap->cap_flush.tid;
1415 arg.oldest_flush_tid = oldest_flush_tid;
1417 arg.size = capsnap->size;
1419 arg.xattr_version = capsnap->xattr_version;
1420 arg.xattr_buf = capsnap->xattr_blob;
1422 arg.atime = capsnap->atime;
1423 arg.mtime = capsnap->mtime;
1424 arg.ctime = capsnap->ctime;
1426 arg.op = CEPH_CAP_OP_FLUSHSNAP;
1427 arg.caps = capsnap->issued;
1429 arg.dirty = capsnap->dirty;
1434 arg.time_warp_seq = capsnap->time_warp_seq;
1436 arg.uid = capsnap->uid;
1437 arg.gid = capsnap->gid;
1438 arg.mode = capsnap->mode;
1440 arg.inline_data = capsnap->inline_data;
1443 return send_cap_msg(&arg);
1447 * When a snapshot is taken, clients accumulate dirty metadata on
1448 * inodes with capabilities in ceph_cap_snaps to describe the file
1449 * state at the time the snapshot was taken. This must be flushed
1450 * asynchronously back to the MDS once sync writes complete and dirty
1451 * data is written out.
1453 * Called under i_ceph_lock. Takes s_mutex as needed.
1455 static void __ceph_flush_snaps(struct ceph_inode_info *ci,
1456 struct ceph_mds_session *session)
1457 __releases(ci->i_ceph_lock)
1458 __acquires(ci->i_ceph_lock)
1460 struct inode *inode = &ci->vfs_inode;
1461 struct ceph_mds_client *mdsc = session->s_mdsc;
1462 struct ceph_cap_snap *capsnap;
1463 u64 oldest_flush_tid = 0;
1464 u64 first_tid = 1, last_tid = 0;
1466 dout("__flush_snaps %p session %p\n", inode, session);
1468 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1470 * we need to wait for sync writes to complete and for dirty
1471 * pages to be written out.
1473 if (capsnap->dirty_pages || capsnap->writing)
1476 /* should be removed by ceph_try_drop_cap_snap() */
1477 BUG_ON(!capsnap->need_flush);
1479 /* only flush each capsnap once */
1480 if (capsnap->cap_flush.tid > 0) {
1481 dout(" already flushed %p, skipping\n", capsnap);
1485 spin_lock(&mdsc->cap_dirty_lock);
1486 capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
1487 list_add_tail(&capsnap->cap_flush.g_list,
1488 &mdsc->cap_flush_list);
1489 if (oldest_flush_tid == 0)
1490 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1491 if (list_empty(&ci->i_flushing_item)) {
1492 list_add_tail(&ci->i_flushing_item,
1493 &session->s_cap_flushing);
1495 spin_unlock(&mdsc->cap_dirty_lock);
1497 list_add_tail(&capsnap->cap_flush.i_list,
1498 &ci->i_cap_flush_list);
1501 first_tid = capsnap->cap_flush.tid;
1502 last_tid = capsnap->cap_flush.tid;
1505 ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS;
1507 while (first_tid <= last_tid) {
1508 struct ceph_cap *cap = ci->i_auth_cap;
1509 struct ceph_cap_flush *cf;
1512 if (!(cap && cap->session == session)) {
1513 dout("__flush_snaps %p auth cap %p not mds%d, "
1514 "stop\n", inode, cap, session->s_mds);
1519 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
1520 if (cf->tid >= first_tid) {
1528 first_tid = cf->tid + 1;
1530 capsnap = container_of(cf, struct ceph_cap_snap, cap_flush);
1531 refcount_inc(&capsnap->nref);
1532 spin_unlock(&ci->i_ceph_lock);
1534 dout("__flush_snaps %p capsnap %p tid %llu %s\n",
1535 inode, capsnap, cf->tid, ceph_cap_string(capsnap->dirty));
1537 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
1540 pr_err("__flush_snaps: error sending cap flushsnap, "
1541 "ino (%llx.%llx) tid %llu follows %llu\n",
1542 ceph_vinop(inode), cf->tid, capsnap->follows);
1545 ceph_put_cap_snap(capsnap);
1546 spin_lock(&ci->i_ceph_lock);
1550 void ceph_flush_snaps(struct ceph_inode_info *ci,
1551 struct ceph_mds_session **psession)
1553 struct inode *inode = &ci->vfs_inode;
1554 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
1555 struct ceph_mds_session *session = NULL;
1558 dout("ceph_flush_snaps %p\n", inode);
1560 session = *psession;
1562 spin_lock(&ci->i_ceph_lock);
1563 if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) {
1564 dout(" no capsnap needs flush, doing nothing\n");
1567 if (!ci->i_auth_cap) {
1568 dout(" no auth cap (migrating?), doing nothing\n");
1572 mds = ci->i_auth_cap->session->s_mds;
1573 if (session && session->s_mds != mds) {
1574 dout(" oops, wrong session %p mutex\n", session);
1575 mutex_unlock(&session->s_mutex);
1576 ceph_put_mds_session(session);
1580 spin_unlock(&ci->i_ceph_lock);
1581 mutex_lock(&mdsc->mutex);
1582 session = __ceph_lookup_mds_session(mdsc, mds);
1583 mutex_unlock(&mdsc->mutex);
1585 dout(" inverting session/ino locks on %p\n", session);
1586 mutex_lock(&session->s_mutex);
1591 // make sure flushsnap messages are sent in proper order.
1592 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
1593 __kick_flushing_caps(mdsc, session, ci, 0);
1594 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
1597 __ceph_flush_snaps(ci, session);
1599 spin_unlock(&ci->i_ceph_lock);
1602 *psession = session;
1603 } else if (session) {
1604 mutex_unlock(&session->s_mutex);
1605 ceph_put_mds_session(session);
1607 /* we flushed them all; remove this inode from the queue */
1608 spin_lock(&mdsc->snap_flush_lock);
1609 list_del_init(&ci->i_snap_flush_item);
1610 spin_unlock(&mdsc->snap_flush_lock);
1614 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1615 * Caller is then responsible for calling __mark_inode_dirty with the
1616 * returned flags value.
1618 int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
1619 struct ceph_cap_flush **pcf)
1621 struct ceph_mds_client *mdsc =
1622 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
1623 struct inode *inode = &ci->vfs_inode;
1624 int was = ci->i_dirty_caps;
1627 if (!ci->i_auth_cap) {
1628 pr_warn("__mark_dirty_caps %p %llx mask %s, "
1629 "but no auth cap (session was closed?)\n",
1630 inode, ceph_ino(inode), ceph_cap_string(mask));
1634 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1635 ceph_cap_string(mask), ceph_cap_string(was),
1636 ceph_cap_string(was | mask));
1637 ci->i_dirty_caps |= mask;
1639 WARN_ON_ONCE(ci->i_prealloc_cap_flush);
1640 swap(ci->i_prealloc_cap_flush, *pcf);
1642 if (!ci->i_head_snapc) {
1643 WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
1644 ci->i_head_snapc = ceph_get_snap_context(
1645 ci->i_snap_realm->cached_context);
1647 dout(" inode %p now dirty snapc %p auth cap %p\n",
1648 &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
1649 BUG_ON(!list_empty(&ci->i_dirty_item));
1650 spin_lock(&mdsc->cap_dirty_lock);
1651 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
1652 spin_unlock(&mdsc->cap_dirty_lock);
1653 if (ci->i_flushing_caps == 0) {
1655 dirty |= I_DIRTY_SYNC;
1658 WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
1660 BUG_ON(list_empty(&ci->i_dirty_item));
1661 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1662 (mask & CEPH_CAP_FILE_BUFFER))
1663 dirty |= I_DIRTY_DATASYNC;
1664 __cap_delay_requeue(mdsc, ci);
1668 struct ceph_cap_flush *ceph_alloc_cap_flush(void)
1670 return kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
1673 void ceph_free_cap_flush(struct ceph_cap_flush *cf)
1676 kmem_cache_free(ceph_cap_flush_cachep, cf);
1679 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
1681 if (!list_empty(&mdsc->cap_flush_list)) {
1682 struct ceph_cap_flush *cf =
1683 list_first_entry(&mdsc->cap_flush_list,
1684 struct ceph_cap_flush, g_list);
1691 * Remove cap_flush from the mdsc's or inode's flushing cap list.
1692 * Return true if caller needs to wake up flush waiters.
1694 static bool __finish_cap_flush(struct ceph_mds_client *mdsc,
1695 struct ceph_inode_info *ci,
1696 struct ceph_cap_flush *cf)
1698 struct ceph_cap_flush *prev;
1699 bool wake = cf->wake;
1701 /* are there older pending cap flushes? */
1702 if (wake && cf->g_list.prev != &mdsc->cap_flush_list) {
1703 prev = list_prev_entry(cf, g_list);
1707 list_del(&cf->g_list);
1709 if (wake && cf->i_list.prev != &ci->i_cap_flush_list) {
1710 prev = list_prev_entry(cf, i_list);
1714 list_del(&cf->i_list);
1722 * Add dirty inode to the flushing list. Assigned a seq number so we
1723 * can wait for caps to flush without starving.
1725 * Called under i_ceph_lock.
1727 static int __mark_caps_flushing(struct inode *inode,
1728 struct ceph_mds_session *session, bool wake,
1729 u64 *flush_tid, u64 *oldest_flush_tid)
1731 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1732 struct ceph_inode_info *ci = ceph_inode(inode);
1733 struct ceph_cap_flush *cf = NULL;
1736 BUG_ON(ci->i_dirty_caps == 0);
1737 BUG_ON(list_empty(&ci->i_dirty_item));
1738 BUG_ON(!ci->i_prealloc_cap_flush);
1740 flushing = ci->i_dirty_caps;
1741 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1742 ceph_cap_string(flushing),
1743 ceph_cap_string(ci->i_flushing_caps),
1744 ceph_cap_string(ci->i_flushing_caps | flushing));
1745 ci->i_flushing_caps |= flushing;
1746 ci->i_dirty_caps = 0;
1747 dout(" inode %p now !dirty\n", inode);
1749 swap(cf, ci->i_prealloc_cap_flush);
1750 cf->caps = flushing;
1753 spin_lock(&mdsc->cap_dirty_lock);
1754 list_del_init(&ci->i_dirty_item);
1756 cf->tid = ++mdsc->last_cap_flush_tid;
1757 list_add_tail(&cf->g_list, &mdsc->cap_flush_list);
1758 *oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1760 if (list_empty(&ci->i_flushing_item)) {
1761 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1762 mdsc->num_cap_flushing++;
1764 spin_unlock(&mdsc->cap_dirty_lock);
1766 list_add_tail(&cf->i_list, &ci->i_cap_flush_list);
1768 *flush_tid = cf->tid;
1773 * try to invalidate mapping pages without blocking.
1775 static int try_nonblocking_invalidate(struct inode *inode)
1777 struct ceph_inode_info *ci = ceph_inode(inode);
1778 u32 invalidating_gen = ci->i_rdcache_gen;
1780 spin_unlock(&ci->i_ceph_lock);
1781 invalidate_mapping_pages(&inode->i_data, 0, -1);
1782 spin_lock(&ci->i_ceph_lock);
1784 if (inode->i_data.nrpages == 0 &&
1785 invalidating_gen == ci->i_rdcache_gen) {
1787 dout("try_nonblocking_invalidate %p success\n", inode);
1788 /* save any racing async invalidate some trouble */
1789 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
1792 dout("try_nonblocking_invalidate %p failed\n", inode);
1796 bool __ceph_should_report_size(struct ceph_inode_info *ci)
1798 loff_t size = ci->vfs_inode.i_size;
1799 /* mds will adjust max size according to the reported size */
1800 if (ci->i_flushing_caps & CEPH_CAP_FILE_WR)
1802 if (size >= ci->i_max_size)
1804 /* half of previous max_size increment has been used */
1805 if (ci->i_max_size > ci->i_reported_size &&
1806 (size << 1) >= ci->i_max_size + ci->i_reported_size)
1812 * Swiss army knife function to examine currently used and wanted
1813 * versus held caps. Release, flush, ack revoked caps to mds as
1816 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1817 * cap release further.
1818 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1819 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1822 void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1823 struct ceph_mds_session *session)
1825 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1826 struct ceph_mds_client *mdsc = fsc->mdsc;
1827 struct inode *inode = &ci->vfs_inode;
1828 struct ceph_cap *cap;
1829 u64 flush_tid, oldest_flush_tid;
1830 int file_wanted, used, cap_used;
1831 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
1832 int issued, implemented, want, retain, revoking, flushing = 0;
1833 int mds = -1; /* keep track of how far we've gone through i_caps list
1834 to avoid an infinite loop on retry */
1836 int delayed = 0, sent = 0;
1837 bool no_delay = flags & CHECK_CAPS_NODELAY;
1838 bool queue_invalidate = false;
1839 bool tried_invalidate = false;
1841 /* if we are unmounting, flush any unused caps immediately. */
1845 spin_lock(&ci->i_ceph_lock);
1847 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1848 flags |= CHECK_CAPS_FLUSH;
1850 if (!(flags & CHECK_CAPS_AUTHONLY) ||
1851 (ci->i_auth_cap && __ceph_is_single_caps(ci)))
1852 __cap_delay_cancel(mdsc, ci);
1856 spin_lock(&ci->i_ceph_lock);
1858 file_wanted = __ceph_caps_file_wanted(ci);
1859 used = __ceph_caps_used(ci);
1860 issued = __ceph_caps_issued(ci, &implemented);
1861 revoking = implemented & ~issued;
1864 retain = file_wanted | used | CEPH_CAP_PIN;
1865 if (!mdsc->stopping && inode->i_nlink > 0) {
1867 retain |= CEPH_CAP_ANY; /* be greedy */
1868 } else if (S_ISDIR(inode->i_mode) &&
1869 (issued & CEPH_CAP_FILE_SHARED) &&
1870 __ceph_dir_is_complete(ci)) {
1872 * If a directory is complete, we want to keep
1873 * the exclusive cap. So that MDS does not end up
1874 * revoking the shared cap on every create/unlink
1877 want = CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
1881 retain |= CEPH_CAP_ANY_SHARED;
1883 * keep RD only if we didn't have the file open RW,
1884 * because then the mds would revoke it anyway to
1885 * journal max_size=0.
1887 if (ci->i_max_size == 0)
1888 retain |= CEPH_CAP_ANY_RD;
1892 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1893 " issued %s revoking %s retain %s %s%s%s\n", inode,
1894 ceph_cap_string(file_wanted),
1895 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1896 ceph_cap_string(ci->i_flushing_caps),
1897 ceph_cap_string(issued), ceph_cap_string(revoking),
1898 ceph_cap_string(retain),
1899 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1900 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1901 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1904 * If we no longer need to hold onto old our caps, and we may
1905 * have cached pages, but don't want them, then try to invalidate.
1906 * If we fail, it's because pages are locked.... try again later.
1908 if ((!no_delay || mdsc->stopping) &&
1909 !S_ISDIR(inode->i_mode) && /* ignore readdir cache */
1910 !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */
1911 inode->i_data.nrpages && /* have cached pages */
1912 (revoking & (CEPH_CAP_FILE_CACHE|
1913 CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */
1914 !tried_invalidate) {
1915 dout("check_caps trying to invalidate on %p\n", inode);
1916 if (try_nonblocking_invalidate(inode) < 0) {
1917 dout("check_caps queuing invalidate\n");
1918 queue_invalidate = true;
1919 ci->i_rdcache_revoking = ci->i_rdcache_gen;
1921 tried_invalidate = true;
1925 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1926 cap = rb_entry(p, struct ceph_cap, ci_node);
1928 /* avoid looping forever */
1929 if (mds >= cap->mds ||
1930 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1933 /* NOTE: no side-effects allowed, until we take s_mutex */
1936 if (ci->i_auth_cap && cap != ci->i_auth_cap)
1937 cap_used &= ~ci->i_auth_cap->issued;
1939 revoking = cap->implemented & ~cap->issued;
1940 dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
1941 cap->mds, cap, ceph_cap_string(cap_used),
1942 ceph_cap_string(cap->issued),
1943 ceph_cap_string(cap->implemented),
1944 ceph_cap_string(revoking));
1946 if (cap == ci->i_auth_cap &&
1947 (cap->issued & CEPH_CAP_FILE_WR)) {
1948 /* request larger max_size from MDS? */
1949 if (ci->i_wanted_max_size > ci->i_max_size &&
1950 ci->i_wanted_max_size > ci->i_requested_max_size) {
1951 dout("requesting new max_size\n");
1955 /* approaching file_max? */
1956 if (__ceph_should_report_size(ci)) {
1957 dout("i_size approaching max_size\n");
1961 /* flush anything dirty? */
1962 if (cap == ci->i_auth_cap) {
1963 if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) {
1964 dout("flushing dirty caps\n");
1967 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) {
1968 dout("flushing snap caps\n");
1973 /* completed revocation? going down and there are no caps? */
1974 if (revoking && (revoking & cap_used) == 0) {
1975 dout("completed revocation of %s\n",
1976 ceph_cap_string(cap->implemented & ~cap->issued));
1980 /* want more caps from mds? */
1981 if (want & ~(cap->mds_wanted | cap->issued))
1984 /* things we might delay */
1985 if ((cap->issued & ~retain) == 0 &&
1986 cap->mds_wanted == want)
1987 continue; /* nope, all good */
1993 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1994 time_before(jiffies, ci->i_hold_caps_max)) {
1995 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1996 ceph_cap_string(cap->issued),
1997 ceph_cap_string(cap->issued & retain),
1998 ceph_cap_string(cap->mds_wanted),
1999 ceph_cap_string(want));
2005 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
2006 dout(" skipping %p I_NOFLUSH set\n", inode);
2010 if (session && session != cap->session) {
2011 dout("oops, wrong session %p mutex\n", session);
2012 mutex_unlock(&session->s_mutex);
2016 session = cap->session;
2017 if (mutex_trylock(&session->s_mutex) == 0) {
2018 dout("inverting session/ino locks on %p\n",
2020 spin_unlock(&ci->i_ceph_lock);
2021 if (took_snap_rwsem) {
2022 up_read(&mdsc->snap_rwsem);
2023 took_snap_rwsem = 0;
2025 mutex_lock(&session->s_mutex);
2030 /* kick flushing and flush snaps before sending normal
2032 if (cap == ci->i_auth_cap &&
2034 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) {
2035 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
2036 __kick_flushing_caps(mdsc, session, ci, 0);
2037 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2039 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
2040 __ceph_flush_snaps(ci, session);
2045 /* take snap_rwsem after session mutex */
2046 if (!took_snap_rwsem) {
2047 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
2048 dout("inverting snap/in locks on %p\n",
2050 spin_unlock(&ci->i_ceph_lock);
2051 down_read(&mdsc->snap_rwsem);
2052 took_snap_rwsem = 1;
2055 took_snap_rwsem = 1;
2058 if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
2059 flushing = __mark_caps_flushing(inode, session, false,
2065 spin_lock(&mdsc->cap_dirty_lock);
2066 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2067 spin_unlock(&mdsc->cap_dirty_lock);
2070 mds = cap->mds; /* remember mds, so we don't repeat */
2073 /* __send_cap drops i_ceph_lock */
2074 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, false,
2075 cap_used, want, retain, flushing,
2076 flush_tid, oldest_flush_tid);
2077 goto retry; /* retake i_ceph_lock and restart our cap scan. */
2080 /* Reschedule delayed caps release if we delayed anything */
2082 __cap_delay_requeue(mdsc, ci);
2084 spin_unlock(&ci->i_ceph_lock);
2086 if (queue_invalidate)
2087 ceph_queue_invalidate(inode);
2090 mutex_unlock(&session->s_mutex);
2091 if (took_snap_rwsem)
2092 up_read(&mdsc->snap_rwsem);
2096 * Try to flush dirty caps back to the auth mds.
2098 static int try_flush_caps(struct inode *inode, u64 *ptid)
2100 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2101 struct ceph_inode_info *ci = ceph_inode(inode);
2102 struct ceph_mds_session *session = NULL;
2104 u64 flush_tid = 0, oldest_flush_tid = 0;
2107 spin_lock(&ci->i_ceph_lock);
2108 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
2109 spin_unlock(&ci->i_ceph_lock);
2110 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
2113 if (ci->i_dirty_caps && ci->i_auth_cap) {
2114 struct ceph_cap *cap = ci->i_auth_cap;
2115 int used = __ceph_caps_used(ci);
2116 int want = __ceph_caps_wanted(ci);
2119 if (!session || session != cap->session) {
2120 spin_unlock(&ci->i_ceph_lock);
2122 mutex_unlock(&session->s_mutex);
2123 session = cap->session;
2124 mutex_lock(&session->s_mutex);
2127 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) {
2128 spin_unlock(&ci->i_ceph_lock);
2132 flushing = __mark_caps_flushing(inode, session, true,
2133 &flush_tid, &oldest_flush_tid);
2135 /* __send_cap drops i_ceph_lock */
2136 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, true,
2137 used, want, (cap->issued | cap->implemented),
2138 flushing, flush_tid, oldest_flush_tid);
2141 spin_lock(&ci->i_ceph_lock);
2142 __cap_delay_requeue(mdsc, ci);
2143 spin_unlock(&ci->i_ceph_lock);
2146 if (!list_empty(&ci->i_cap_flush_list)) {
2147 struct ceph_cap_flush *cf =
2148 list_last_entry(&ci->i_cap_flush_list,
2149 struct ceph_cap_flush, i_list);
2151 flush_tid = cf->tid;
2153 flushing = ci->i_flushing_caps;
2154 spin_unlock(&ci->i_ceph_lock);
2158 mutex_unlock(&session->s_mutex);
2165 * Return true if we've flushed caps through the given flush_tid.
2167 static int caps_are_flushed(struct inode *inode, u64 flush_tid)
2169 struct ceph_inode_info *ci = ceph_inode(inode);
2172 spin_lock(&ci->i_ceph_lock);
2173 if (!list_empty(&ci->i_cap_flush_list)) {
2174 struct ceph_cap_flush * cf =
2175 list_first_entry(&ci->i_cap_flush_list,
2176 struct ceph_cap_flush, i_list);
2177 if (cf->tid <= flush_tid)
2180 spin_unlock(&ci->i_ceph_lock);
2185 * wait for any unsafe requests to complete.
2187 static int unsafe_request_wait(struct inode *inode)
2189 struct ceph_inode_info *ci = ceph_inode(inode);
2190 struct ceph_mds_request *req1 = NULL, *req2 = NULL;
2193 spin_lock(&ci->i_unsafe_lock);
2194 if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
2195 req1 = list_last_entry(&ci->i_unsafe_dirops,
2196 struct ceph_mds_request,
2198 ceph_mdsc_get_request(req1);
2200 if (!list_empty(&ci->i_unsafe_iops)) {
2201 req2 = list_last_entry(&ci->i_unsafe_iops,
2202 struct ceph_mds_request,
2203 r_unsafe_target_item);
2204 ceph_mdsc_get_request(req2);
2206 spin_unlock(&ci->i_unsafe_lock);
2208 dout("unsafe_request_wait %p wait on tid %llu %llu\n",
2209 inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL);
2211 ret = !wait_for_completion_timeout(&req1->r_safe_completion,
2212 ceph_timeout_jiffies(req1->r_timeout));
2215 ceph_mdsc_put_request(req1);
2218 ret = !wait_for_completion_timeout(&req2->r_safe_completion,
2219 ceph_timeout_jiffies(req2->r_timeout));
2222 ceph_mdsc_put_request(req2);
2227 int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2229 struct inode *inode = file->f_mapping->host;
2230 struct ceph_inode_info *ci = ceph_inode(inode);
2235 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
2237 ret = file_write_and_wait_range(file, start, end);
2246 dirty = try_flush_caps(inode, &flush_tid);
2247 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
2249 ret = unsafe_request_wait(inode);
2252 * only wait on non-file metadata writeback (the mds
2253 * can recover size and mtime, so we don't need to
2256 if (!ret && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
2257 ret = wait_event_interruptible(ci->i_cap_wq,
2258 caps_are_flushed(inode, flush_tid));
2260 inode_unlock(inode);
2262 dout("fsync %p%s result=%d\n", inode, datasync ? " datasync" : "", ret);
2267 * Flush any dirty caps back to the mds. If we aren't asked to wait,
2268 * queue inode for flush but don't do so immediately, because we can
2269 * get by with fewer MDS messages if we wait for data writeback to
2272 int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
2274 struct ceph_inode_info *ci = ceph_inode(inode);
2278 int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync);
2280 dout("write_inode %p wait=%d\n", inode, wait);
2282 dirty = try_flush_caps(inode, &flush_tid);
2284 err = wait_event_interruptible(ci->i_cap_wq,
2285 caps_are_flushed(inode, flush_tid));
2287 struct ceph_mds_client *mdsc =
2288 ceph_sb_to_client(inode->i_sb)->mdsc;
2290 spin_lock(&ci->i_ceph_lock);
2291 if (__ceph_caps_dirty(ci))
2292 __cap_delay_requeue_front(mdsc, ci);
2293 spin_unlock(&ci->i_ceph_lock);
2298 static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
2299 struct ceph_mds_session *session,
2300 struct ceph_inode_info *ci,
2301 u64 oldest_flush_tid)
2302 __releases(ci->i_ceph_lock)
2303 __acquires(ci->i_ceph_lock)
2305 struct inode *inode = &ci->vfs_inode;
2306 struct ceph_cap *cap;
2307 struct ceph_cap_flush *cf;
2311 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
2312 if (cf->tid < first_tid)
2315 cap = ci->i_auth_cap;
2316 if (!(cap && cap->session == session)) {
2317 pr_err("%p auth cap %p not mds%d ???\n",
2318 inode, cap, session->s_mds);
2322 first_tid = cf->tid + 1;
2325 dout("kick_flushing_caps %p cap %p tid %llu %s\n",
2326 inode, cap, cf->tid, ceph_cap_string(cf->caps));
2327 ci->i_ceph_flags |= CEPH_I_NODELAY;
2328 ret = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
2329 false, __ceph_caps_used(ci),
2330 __ceph_caps_wanted(ci),
2331 cap->issued | cap->implemented,
2332 cf->caps, cf->tid, oldest_flush_tid);
2334 pr_err("kick_flushing_caps: error sending "
2335 "cap flush, ino (%llx.%llx) "
2336 "tid %llu flushing %s\n",
2337 ceph_vinop(inode), cf->tid,
2338 ceph_cap_string(cf->caps));
2341 struct ceph_cap_snap *capsnap =
2342 container_of(cf, struct ceph_cap_snap,
2344 dout("kick_flushing_caps %p capsnap %p tid %llu %s\n",
2345 inode, capsnap, cf->tid,
2346 ceph_cap_string(capsnap->dirty));
2348 refcount_inc(&capsnap->nref);
2349 spin_unlock(&ci->i_ceph_lock);
2351 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
2354 pr_err("kick_flushing_caps: error sending "
2355 "cap flushsnap, ino (%llx.%llx) "
2356 "tid %llu follows %llu\n",
2357 ceph_vinop(inode), cf->tid,
2361 ceph_put_cap_snap(capsnap);
2364 spin_lock(&ci->i_ceph_lock);
2368 void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
2369 struct ceph_mds_session *session)
2371 struct ceph_inode_info *ci;
2372 struct ceph_cap *cap;
2373 u64 oldest_flush_tid;
2375 dout("early_kick_flushing_caps mds%d\n", session->s_mds);
2377 spin_lock(&mdsc->cap_dirty_lock);
2378 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2379 spin_unlock(&mdsc->cap_dirty_lock);
2381 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2382 spin_lock(&ci->i_ceph_lock);
2383 cap = ci->i_auth_cap;
2384 if (!(cap && cap->session == session)) {
2385 pr_err("%p auth cap %p not mds%d ???\n",
2386 &ci->vfs_inode, cap, session->s_mds);
2387 spin_unlock(&ci->i_ceph_lock);
2393 * if flushing caps were revoked, we re-send the cap flush
2394 * in client reconnect stage. This guarantees MDS * processes
2395 * the cap flush message before issuing the flushing caps to
2398 if ((cap->issued & ci->i_flushing_caps) !=
2399 ci->i_flushing_caps) {
2400 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2401 __kick_flushing_caps(mdsc, session, ci,
2404 ci->i_ceph_flags |= CEPH_I_KICK_FLUSH;
2407 spin_unlock(&ci->i_ceph_lock);
2411 void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
2412 struct ceph_mds_session *session)
2414 struct ceph_inode_info *ci;
2415 struct ceph_cap *cap;
2416 u64 oldest_flush_tid;
2418 dout("kick_flushing_caps mds%d\n", session->s_mds);
2420 spin_lock(&mdsc->cap_dirty_lock);
2421 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2422 spin_unlock(&mdsc->cap_dirty_lock);
2424 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2425 spin_lock(&ci->i_ceph_lock);
2426 cap = ci->i_auth_cap;
2427 if (!(cap && cap->session == session)) {
2428 pr_err("%p auth cap %p not mds%d ???\n",
2429 &ci->vfs_inode, cap, session->s_mds);
2430 spin_unlock(&ci->i_ceph_lock);
2433 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
2434 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2435 __kick_flushing_caps(mdsc, session, ci,
2438 spin_unlock(&ci->i_ceph_lock);
2442 static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
2443 struct ceph_mds_session *session,
2444 struct inode *inode)
2445 __releases(ci->i_ceph_lock)
2447 struct ceph_inode_info *ci = ceph_inode(inode);
2448 struct ceph_cap *cap;
2450 cap = ci->i_auth_cap;
2451 dout("kick_flushing_inode_caps %p flushing %s\n", inode,
2452 ceph_cap_string(ci->i_flushing_caps));
2454 if (!list_empty(&ci->i_cap_flush_list)) {
2455 u64 oldest_flush_tid;
2456 spin_lock(&mdsc->cap_dirty_lock);
2457 list_move_tail(&ci->i_flushing_item,
2458 &cap->session->s_cap_flushing);
2459 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2460 spin_unlock(&mdsc->cap_dirty_lock);
2462 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2463 __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid);
2464 spin_unlock(&ci->i_ceph_lock);
2466 spin_unlock(&ci->i_ceph_lock);
2472 * Take references to capabilities we hold, so that we don't release
2473 * them to the MDS prematurely.
2475 * Protected by i_ceph_lock.
2477 static void __take_cap_refs(struct ceph_inode_info *ci, int got,
2478 bool snap_rwsem_locked)
2480 if (got & CEPH_CAP_PIN)
2482 if (got & CEPH_CAP_FILE_RD)
2484 if (got & CEPH_CAP_FILE_CACHE)
2485 ci->i_rdcache_ref++;
2486 if (got & CEPH_CAP_FILE_WR) {
2487 if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
2488 BUG_ON(!snap_rwsem_locked);
2489 ci->i_head_snapc = ceph_get_snap_context(
2490 ci->i_snap_realm->cached_context);
2494 if (got & CEPH_CAP_FILE_BUFFER) {
2495 if (ci->i_wb_ref == 0)
2496 ihold(&ci->vfs_inode);
2498 dout("__take_cap_refs %p wb %d -> %d (?)\n",
2499 &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
2504 * Try to grab cap references. Specify those refs we @want, and the
2505 * minimal set we @need. Also include the larger offset we are writing
2506 * to (when applicable), and check against max_size here as well.
2507 * Note that caller is responsible for ensuring max_size increases are
2508 * requested from the MDS.
2510 static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
2511 loff_t endoff, bool nonblock, int *got, int *err)
2513 struct inode *inode = &ci->vfs_inode;
2514 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
2516 int have, implemented;
2518 bool snap_rwsem_locked = false;
2520 dout("get_cap_refs %p need %s want %s\n", inode,
2521 ceph_cap_string(need), ceph_cap_string(want));
2524 spin_lock(&ci->i_ceph_lock);
2526 /* make sure file is actually open */
2527 file_wanted = __ceph_caps_file_wanted(ci);
2528 if ((file_wanted & need) != need) {
2529 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2530 ceph_cap_string(need), ceph_cap_string(file_wanted));
2536 /* finish pending truncate */
2537 while (ci->i_truncate_pending) {
2538 spin_unlock(&ci->i_ceph_lock);
2539 if (snap_rwsem_locked) {
2540 up_read(&mdsc->snap_rwsem);
2541 snap_rwsem_locked = false;
2543 __ceph_do_pending_vmtruncate(inode);
2544 spin_lock(&ci->i_ceph_lock);
2547 have = __ceph_caps_issued(ci, &implemented);
2549 if (have & need & CEPH_CAP_FILE_WR) {
2550 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2551 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2552 inode, endoff, ci->i_max_size);
2553 if (endoff > ci->i_requested_max_size) {
2560 * If a sync write is in progress, we must wait, so that we
2561 * can get a final snapshot value for size+mtime.
2563 if (__ceph_have_pending_cap_snap(ci)) {
2564 dout("get_cap_refs %p cap_snap_pending\n", inode);
2569 if ((have & need) == need) {
2571 * Look at (implemented & ~have & not) so that we keep waiting
2572 * on transition from wanted -> needed caps. This is needed
2573 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2574 * going before a prior buffered writeback happens.
2576 int not = want & ~(have & need);
2577 int revoking = implemented & ~have;
2578 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2579 inode, ceph_cap_string(have), ceph_cap_string(not),
2580 ceph_cap_string(revoking));
2581 if ((revoking & not) == 0) {
2582 if (!snap_rwsem_locked &&
2583 !ci->i_head_snapc &&
2584 (need & CEPH_CAP_FILE_WR)) {
2585 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2587 * we can not call down_read() when
2588 * task isn't in TASK_RUNNING state
2596 spin_unlock(&ci->i_ceph_lock);
2597 down_read(&mdsc->snap_rwsem);
2598 snap_rwsem_locked = true;
2601 snap_rwsem_locked = true;
2603 *got = need | (have & want);
2604 if ((need & CEPH_CAP_FILE_RD) &&
2605 !(*got & CEPH_CAP_FILE_CACHE))
2606 ceph_disable_fscache_readpage(ci);
2607 __take_cap_refs(ci, *got, true);
2611 int session_readonly = false;
2612 if ((need & CEPH_CAP_FILE_WR) && ci->i_auth_cap) {
2613 struct ceph_mds_session *s = ci->i_auth_cap->session;
2614 spin_lock(&s->s_cap_lock);
2615 session_readonly = s->s_readonly;
2616 spin_unlock(&s->s_cap_lock);
2618 if (session_readonly) {
2619 dout("get_cap_refs %p needed %s but mds%d readonly\n",
2620 inode, ceph_cap_string(need), ci->i_auth_cap->mds);
2626 if (ci->i_ceph_flags & CEPH_I_CAP_DROPPED) {
2628 if (READ_ONCE(mdsc->fsc->mount_state) ==
2629 CEPH_MOUNT_SHUTDOWN) {
2630 dout("get_cap_refs %p forced umount\n", inode);
2635 mds_wanted = __ceph_caps_mds_wanted(ci, false);
2636 if (need & ~(mds_wanted & need)) {
2637 dout("get_cap_refs %p caps were dropped"
2638 " (session killed?)\n", inode);
2643 if (!(file_wanted & ~mds_wanted))
2644 ci->i_ceph_flags &= ~CEPH_I_CAP_DROPPED;
2647 dout("get_cap_refs %p have %s needed %s\n", inode,
2648 ceph_cap_string(have), ceph_cap_string(need));
2651 spin_unlock(&ci->i_ceph_lock);
2652 if (snap_rwsem_locked)
2653 up_read(&mdsc->snap_rwsem);
2655 dout("get_cap_refs %p ret %d got %s\n", inode,
2656 ret, ceph_cap_string(*got));
2661 * Check the offset we are writing up to against our current
2662 * max_size. If necessary, tell the MDS we want to write to
2665 static void check_max_size(struct inode *inode, loff_t endoff)
2667 struct ceph_inode_info *ci = ceph_inode(inode);
2670 /* do we need to explicitly request a larger max_size? */
2671 spin_lock(&ci->i_ceph_lock);
2672 if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
2673 dout("write %p at large endoff %llu, req max_size\n",
2675 ci->i_wanted_max_size = endoff;
2677 /* duplicate ceph_check_caps()'s logic */
2678 if (ci->i_auth_cap &&
2679 (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
2680 ci->i_wanted_max_size > ci->i_max_size &&
2681 ci->i_wanted_max_size > ci->i_requested_max_size)
2683 spin_unlock(&ci->i_ceph_lock);
2685 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2688 int ceph_try_get_caps(struct ceph_inode_info *ci, int need, int want, int *got)
2692 BUG_ON(need & ~CEPH_CAP_FILE_RD);
2693 BUG_ON(want & ~(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
2694 ret = ceph_pool_perm_check(ci, need);
2698 ret = try_get_cap_refs(ci, need, want, 0, true, got, &err);
2700 if (err == -EAGAIN) {
2702 } else if (err < 0) {
2710 * Wait for caps, and take cap references. If we can't get a WR cap
2711 * due to a small max_size, make sure we check_max_size (and possibly
2712 * ask the mds) so we don't get hung up indefinitely.
2714 int ceph_get_caps(struct ceph_inode_info *ci, int need, int want,
2715 loff_t endoff, int *got, struct page **pinned_page)
2717 int _got, ret, err = 0;
2719 ret = ceph_pool_perm_check(ci, need);
2725 check_max_size(&ci->vfs_inode, endoff);
2729 ret = try_get_cap_refs(ci, need, want, endoff,
2730 false, &_got, &err);
2737 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2738 add_wait_queue(&ci->i_cap_wq, &wait);
2740 while (!try_get_cap_refs(ci, need, want, endoff,
2741 true, &_got, &err)) {
2742 if (signal_pending(current)) {
2746 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
2749 remove_wait_queue(&ci->i_cap_wq, &wait);
2757 if (err == -ESTALE) {
2758 /* session was killed, try renew caps */
2759 ret = ceph_renew_caps(&ci->vfs_inode);
2766 if (ci->i_inline_version != CEPH_INLINE_NONE &&
2767 (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
2768 i_size_read(&ci->vfs_inode) > 0) {
2770 find_get_page(ci->vfs_inode.i_mapping, 0);
2772 if (PageUptodate(page)) {
2773 *pinned_page = page;
2779 * drop cap refs first because getattr while
2780 * holding * caps refs can cause deadlock.
2782 ceph_put_cap_refs(ci, _got);
2786 * getattr request will bring inline data into
2789 ret = __ceph_do_getattr(&ci->vfs_inode, NULL,
2790 CEPH_STAT_CAP_INLINE_DATA,
2799 if ((_got & CEPH_CAP_FILE_RD) && (_got & CEPH_CAP_FILE_CACHE))
2800 ceph_fscache_revalidate_cookie(ci);
2807 * Take cap refs. Caller must already know we hold at least one ref
2808 * on the caps in question or we don't know this is safe.
2810 void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2812 spin_lock(&ci->i_ceph_lock);
2813 __take_cap_refs(ci, caps, false);
2814 spin_unlock(&ci->i_ceph_lock);
2819 * drop cap_snap that is not associated with any snapshot.
2820 * we don't need to send FLUSHSNAP message for it.
2822 static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci,
2823 struct ceph_cap_snap *capsnap)
2825 if (!capsnap->need_flush &&
2826 !capsnap->writing && !capsnap->dirty_pages) {
2827 dout("dropping cap_snap %p follows %llu\n",
2828 capsnap, capsnap->follows);
2829 BUG_ON(capsnap->cap_flush.tid > 0);
2830 ceph_put_snap_context(capsnap->context);
2831 if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps))
2832 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2834 list_del(&capsnap->ci_item);
2835 ceph_put_cap_snap(capsnap);
2844 * If we released the last ref on any given cap, call ceph_check_caps
2845 * to release (or schedule a release).
2847 * If we are releasing a WR cap (from a sync write), finalize any affected
2848 * cap_snap, and wake up any waiters.
2850 void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2852 struct inode *inode = &ci->vfs_inode;
2853 int last = 0, put = 0, flushsnaps = 0, wake = 0;
2855 spin_lock(&ci->i_ceph_lock);
2856 if (had & CEPH_CAP_PIN)
2858 if (had & CEPH_CAP_FILE_RD)
2859 if (--ci->i_rd_ref == 0)
2861 if (had & CEPH_CAP_FILE_CACHE)
2862 if (--ci->i_rdcache_ref == 0)
2864 if (had & CEPH_CAP_FILE_BUFFER) {
2865 if (--ci->i_wb_ref == 0) {
2869 dout("put_cap_refs %p wb %d -> %d (?)\n",
2870 inode, ci->i_wb_ref+1, ci->i_wb_ref);
2872 if (had & CEPH_CAP_FILE_WR)
2873 if (--ci->i_wr_ref == 0) {
2875 if (__ceph_have_pending_cap_snap(ci)) {
2876 struct ceph_cap_snap *capsnap =
2877 list_last_entry(&ci->i_cap_snaps,
2878 struct ceph_cap_snap,
2880 capsnap->writing = 0;
2881 if (ceph_try_drop_cap_snap(ci, capsnap))
2883 else if (__ceph_finish_cap_snap(ci, capsnap))
2887 if (ci->i_wrbuffer_ref_head == 0 &&
2888 ci->i_dirty_caps == 0 &&
2889 ci->i_flushing_caps == 0) {
2890 BUG_ON(!ci->i_head_snapc);
2891 ceph_put_snap_context(ci->i_head_snapc);
2892 ci->i_head_snapc = NULL;
2894 /* see comment in __ceph_remove_cap() */
2895 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm)
2896 drop_inode_snap_realm(ci);
2898 spin_unlock(&ci->i_ceph_lock);
2900 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2901 last ? " last" : "", put ? " put" : "");
2903 if (last && !flushsnaps)
2904 ceph_check_caps(ci, 0, NULL);
2905 else if (flushsnaps)
2906 ceph_flush_snaps(ci, NULL);
2908 wake_up_all(&ci->i_cap_wq);
2914 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2915 * context. Adjust per-snap dirty page accounting as appropriate.
2916 * Once all dirty data for a cap_snap is flushed, flush snapped file
2917 * metadata back to the MDS. If we dropped the last ref, call
2920 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2921 struct ceph_snap_context *snapc)
2923 struct inode *inode = &ci->vfs_inode;
2924 struct ceph_cap_snap *capsnap = NULL;
2928 bool flush_snaps = false;
2929 bool complete_capsnap = false;
2931 spin_lock(&ci->i_ceph_lock);
2932 ci->i_wrbuffer_ref -= nr;
2933 if (ci->i_wrbuffer_ref == 0) {
2938 if (ci->i_head_snapc == snapc) {
2939 ci->i_wrbuffer_ref_head -= nr;
2940 if (ci->i_wrbuffer_ref_head == 0 &&
2941 ci->i_wr_ref == 0 &&
2942 ci->i_dirty_caps == 0 &&
2943 ci->i_flushing_caps == 0) {
2944 BUG_ON(!ci->i_head_snapc);
2945 ceph_put_snap_context(ci->i_head_snapc);
2946 ci->i_head_snapc = NULL;
2948 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2950 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2951 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2952 last ? " LAST" : "");
2954 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2955 if (capsnap->context == snapc) {
2961 capsnap->dirty_pages -= nr;
2962 if (capsnap->dirty_pages == 0) {
2963 complete_capsnap = true;
2964 if (!capsnap->writing) {
2965 if (ceph_try_drop_cap_snap(ci, capsnap)) {
2968 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2973 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2974 " snap %lld %d/%d -> %d/%d %s%s\n",
2975 inode, capsnap, capsnap->context->seq,
2976 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2977 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2978 last ? " (wrbuffer last)" : "",
2979 complete_capsnap ? " (complete capsnap)" : "");
2982 spin_unlock(&ci->i_ceph_lock);
2985 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2986 } else if (flush_snaps) {
2987 ceph_flush_snaps(ci, NULL);
2989 if (complete_capsnap)
2990 wake_up_all(&ci->i_cap_wq);
2996 * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
2998 static void invalidate_aliases(struct inode *inode)
3000 struct dentry *dn, *prev = NULL;
3002 dout("invalidate_aliases inode %p\n", inode);
3003 d_prune_aliases(inode);
3005 * For non-directory inode, d_find_alias() only returns
3006 * hashed dentry. After calling d_invalidate(), the
3007 * dentry becomes unhashed.
3009 * For directory inode, d_find_alias() can return
3010 * unhashed dentry. But directory inode should have
3011 * one alias at most.
3013 while ((dn = d_find_alias(inode))) {
3027 struct cap_extra_info {
3028 struct ceph_string *pool_ns;
3037 /* currently issued */
3042 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
3043 * actually be a revocation if it specifies a smaller cap set.)
3045 * caller holds s_mutex and i_ceph_lock, we drop both.
3047 static void handle_cap_grant(struct inode *inode,
3048 struct ceph_mds_session *session,
3049 struct ceph_cap *cap,
3050 struct ceph_mds_caps *grant,
3051 struct ceph_buffer *xattr_buf,
3052 struct cap_extra_info *extra_info)
3053 __releases(ci->i_ceph_lock)
3054 __releases(session->s_mdsc->snap_rwsem)
3056 struct ceph_inode_info *ci = ceph_inode(inode);
3057 int seq = le32_to_cpu(grant->seq);
3058 int newcaps = le32_to_cpu(grant->caps);
3059 int used, wanted, dirty;
3060 u64 size = le64_to_cpu(grant->size);
3061 u64 max_size = le64_to_cpu(grant->max_size);
3064 bool writeback = false;
3065 bool queue_trunc = false;
3066 bool queue_invalidate = false;
3067 bool deleted_inode = false;
3068 bool fill_inline = false;
3070 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
3071 inode, cap, session->s_mds, seq, ceph_cap_string(newcaps));
3072 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
3077 * auth mds of the inode changed. we received the cap export message,
3078 * but still haven't received the cap import message. handle_cap_export
3079 * updated the new auth MDS' cap.
3081 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
3082 * that was sent before the cap import message. So don't remove caps.
3084 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
3085 WARN_ON(cap != ci->i_auth_cap);
3086 WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
3088 newcaps |= cap->issued;
3092 * If CACHE is being revoked, and we have no dirty buffers,
3093 * try to invalidate (once). (If there are dirty buffers, we
3094 * will invalidate _after_ writeback.)
3096 if (!S_ISDIR(inode->i_mode) && /* don't invalidate readdir cache */
3097 ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
3098 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3099 !(ci->i_wrbuffer_ref || ci->i_wb_ref)) {
3100 if (try_nonblocking_invalidate(inode)) {
3101 /* there were locked pages.. invalidate later
3102 in a separate thread. */
3103 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
3104 queue_invalidate = true;
3105 ci->i_rdcache_revoking = ci->i_rdcache_gen;
3110 /* side effects now are allowed */
3111 cap->cap_gen = session->s_cap_gen;
3114 __check_cap_issue(ci, cap, newcaps);
3116 if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
3117 (extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) {
3118 inode->i_mode = le32_to_cpu(grant->mode);
3119 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
3120 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
3121 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
3122 from_kuid(&init_user_ns, inode->i_uid),
3123 from_kgid(&init_user_ns, inode->i_gid));
3126 if ((newcaps & CEPH_CAP_LINK_SHARED) &&
3127 (extra_info->issued & CEPH_CAP_LINK_EXCL) == 0) {
3128 set_nlink(inode, le32_to_cpu(grant->nlink));
3129 if (inode->i_nlink == 0 &&
3130 (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
3131 deleted_inode = true;
3134 if ((extra_info->issued & CEPH_CAP_XATTR_EXCL) == 0 &&
3136 int len = le32_to_cpu(grant->xattr_len);
3137 u64 version = le64_to_cpu(grant->xattr_version);
3139 if (version > ci->i_xattrs.version) {
3140 dout(" got new xattrs v%llu on %p len %d\n",
3141 version, inode, len);
3142 if (ci->i_xattrs.blob)
3143 ceph_buffer_put(ci->i_xattrs.blob);
3144 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
3145 ci->i_xattrs.version = version;
3146 ceph_forget_all_cached_acls(inode);
3150 if (newcaps & CEPH_CAP_ANY_RD) {
3151 struct timespec mtime, atime, ctime;
3152 /* ctime/mtime/atime? */
3153 ceph_decode_timespec(&mtime, &grant->mtime);
3154 ceph_decode_timespec(&atime, &grant->atime);
3155 ceph_decode_timespec(&ctime, &grant->ctime);
3156 ceph_fill_file_time(inode, extra_info->issued,
3157 le32_to_cpu(grant->time_warp_seq),
3158 &ctime, &mtime, &atime);
3161 if ((newcaps & CEPH_CAP_FILE_SHARED) && extra_info->dirstat_valid) {
3162 ci->i_files = extra_info->nfiles;
3163 ci->i_subdirs = extra_info->nsubdirs;
3166 if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
3167 /* file layout may have changed */
3168 s64 old_pool = ci->i_layout.pool_id;
3169 struct ceph_string *old_ns;
3171 ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout);
3172 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
3173 lockdep_is_held(&ci->i_ceph_lock));
3174 rcu_assign_pointer(ci->i_layout.pool_ns, extra_info->pool_ns);
3176 if (ci->i_layout.pool_id != old_pool ||
3177 extra_info->pool_ns != old_ns)
3178 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
3180 extra_info->pool_ns = old_ns;
3182 /* size/truncate_seq? */
3183 queue_trunc = ceph_fill_file_size(inode, extra_info->issued,
3184 le32_to_cpu(grant->truncate_seq),
3185 le64_to_cpu(grant->truncate_size),
3189 if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) {
3190 if (max_size != ci->i_max_size) {
3191 dout("max_size %lld -> %llu\n",
3192 ci->i_max_size, max_size);
3193 ci->i_max_size = max_size;
3194 if (max_size >= ci->i_wanted_max_size) {
3195 ci->i_wanted_max_size = 0; /* reset */
3196 ci->i_requested_max_size = 0;
3199 } else if (ci->i_wanted_max_size > ci->i_max_size &&
3200 ci->i_wanted_max_size > ci->i_requested_max_size) {
3201 /* CEPH_CAP_OP_IMPORT */
3206 /* check cap bits */
3207 wanted = __ceph_caps_wanted(ci);
3208 used = __ceph_caps_used(ci);
3209 dirty = __ceph_caps_dirty(ci);
3210 dout(" my wanted = %s, used = %s, dirty %s\n",
3211 ceph_cap_string(wanted),
3212 ceph_cap_string(used),
3213 ceph_cap_string(dirty));
3214 if (wanted != le32_to_cpu(grant->wanted)) {
3215 dout("mds wanted %s -> %s\n",
3216 ceph_cap_string(le32_to_cpu(grant->wanted)),
3217 ceph_cap_string(wanted));
3218 /* imported cap may not have correct mds_wanted */
3219 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT)
3223 /* revocation, grant, or no-op? */
3224 if (cap->issued & ~newcaps) {
3225 int revoking = cap->issued & ~newcaps;
3227 dout("revocation: %s -> %s (revoking %s)\n",
3228 ceph_cap_string(cap->issued),
3229 ceph_cap_string(newcaps),
3230 ceph_cap_string(revoking));
3231 if (revoking & used & CEPH_CAP_FILE_BUFFER)
3232 writeback = true; /* initiate writeback; will delay ack */
3233 else if (revoking == CEPH_CAP_FILE_CACHE &&
3234 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3236 ; /* do nothing yet, invalidation will be queued */
3237 else if (cap == ci->i_auth_cap)
3238 check_caps = 1; /* check auth cap only */
3240 check_caps = 2; /* check all caps */
3241 cap->issued = newcaps;
3242 cap->implemented |= newcaps;
3243 } else if (cap->issued == newcaps) {
3244 dout("caps unchanged: %s -> %s\n",
3245 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
3247 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
3248 ceph_cap_string(newcaps));
3249 /* non-auth MDS is revoking the newly grant caps ? */
3250 if (cap == ci->i_auth_cap &&
3251 __ceph_caps_revoking_other(ci, cap, newcaps))
3254 cap->issued = newcaps;
3255 cap->implemented |= newcaps; /* add bits only, to
3256 * avoid stepping on a
3257 * pending revocation */
3260 BUG_ON(cap->issued & ~cap->implemented);
3262 if (extra_info->inline_version > 0 &&
3263 extra_info->inline_version >= ci->i_inline_version) {
3264 ci->i_inline_version = extra_info->inline_version;
3265 if (ci->i_inline_version != CEPH_INLINE_NONE &&
3266 (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
3270 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
3271 if (newcaps & ~extra_info->issued)
3273 kick_flushing_inode_caps(session->s_mdsc, session, inode);
3274 up_read(&session->s_mdsc->snap_rwsem);
3276 spin_unlock(&ci->i_ceph_lock);
3280 ceph_fill_inline_data(inode, NULL, extra_info->inline_data,
3281 extra_info->inline_len);
3284 ceph_queue_vmtruncate(inode);
3288 * queue inode for writeback: we can't actually call
3289 * filemap_write_and_wait, etc. from message handler
3292 ceph_queue_writeback(inode);
3293 if (queue_invalidate)
3294 ceph_queue_invalidate(inode);
3296 invalidate_aliases(inode);
3298 wake_up_all(&ci->i_cap_wq);
3300 if (check_caps == 1)
3301 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
3303 else if (check_caps == 2)
3304 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
3306 mutex_unlock(&session->s_mutex);
3310 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
3311 * MDS has been safely committed.
3313 static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
3314 struct ceph_mds_caps *m,
3315 struct ceph_mds_session *session,
3316 struct ceph_cap *cap)
3317 __releases(ci->i_ceph_lock)
3319 struct ceph_inode_info *ci = ceph_inode(inode);
3320 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3321 struct ceph_cap_flush *cf, *tmp_cf;
3322 LIST_HEAD(to_remove);
3323 unsigned seq = le32_to_cpu(m->seq);
3324 int dirty = le32_to_cpu(m->dirty);
3327 bool wake_ci = false;
3328 bool wake_mdsc = false;
3330 list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
3331 if (cf->tid == flush_tid)
3333 if (cf->caps == 0) /* capsnap */
3335 if (cf->tid <= flush_tid) {
3336 if (__finish_cap_flush(NULL, ci, cf))
3338 list_add_tail(&cf->i_list, &to_remove);
3340 cleaned &= ~cf->caps;
3346 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
3347 " flushing %s -> %s\n",
3348 inode, session->s_mds, seq, ceph_cap_string(dirty),
3349 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
3350 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
3352 if (list_empty(&to_remove) && !cleaned)
3355 ci->i_flushing_caps &= ~cleaned;
3357 spin_lock(&mdsc->cap_dirty_lock);
3359 list_for_each_entry(cf, &to_remove, i_list) {
3360 if (__finish_cap_flush(mdsc, NULL, cf))
3364 if (ci->i_flushing_caps == 0) {
3365 if (list_empty(&ci->i_cap_flush_list)) {
3366 list_del_init(&ci->i_flushing_item);
3367 if (!list_empty(&session->s_cap_flushing)) {
3368 dout(" mds%d still flushing cap on %p\n",
3370 &list_first_entry(&session->s_cap_flushing,
3371 struct ceph_inode_info,
3372 i_flushing_item)->vfs_inode);
3375 mdsc->num_cap_flushing--;
3376 dout(" inode %p now !flushing\n", inode);
3378 if (ci->i_dirty_caps == 0) {
3379 dout(" inode %p now clean\n", inode);
3380 BUG_ON(!list_empty(&ci->i_dirty_item));
3382 if (ci->i_wr_ref == 0 &&
3383 ci->i_wrbuffer_ref_head == 0) {
3384 BUG_ON(!ci->i_head_snapc);
3385 ceph_put_snap_context(ci->i_head_snapc);
3386 ci->i_head_snapc = NULL;
3389 BUG_ON(list_empty(&ci->i_dirty_item));
3392 spin_unlock(&mdsc->cap_dirty_lock);
3395 spin_unlock(&ci->i_ceph_lock);
3397 while (!list_empty(&to_remove)) {
3398 cf = list_first_entry(&to_remove,
3399 struct ceph_cap_flush, i_list);
3400 list_del(&cf->i_list);
3401 ceph_free_cap_flush(cf);
3405 wake_up_all(&ci->i_cap_wq);
3407 wake_up_all(&mdsc->cap_flushing_wq);
3413 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
3414 * throw away our cap_snap.
3416 * Caller hold s_mutex.
3418 static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
3419 struct ceph_mds_caps *m,
3420 struct ceph_mds_session *session)
3422 struct ceph_inode_info *ci = ceph_inode(inode);
3423 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3424 u64 follows = le64_to_cpu(m->snap_follows);
3425 struct ceph_cap_snap *capsnap;
3426 bool flushed = false;
3427 bool wake_ci = false;
3428 bool wake_mdsc = false;
3430 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
3431 inode, ci, session->s_mds, follows);
3433 spin_lock(&ci->i_ceph_lock);
3434 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
3435 if (capsnap->follows == follows) {
3436 if (capsnap->cap_flush.tid != flush_tid) {
3437 dout(" cap_snap %p follows %lld tid %lld !="
3438 " %lld\n", capsnap, follows,
3439 flush_tid, capsnap->cap_flush.tid);
3445 dout(" skipping cap_snap %p follows %lld\n",
3446 capsnap, capsnap->follows);
3450 WARN_ON(capsnap->dirty_pages || capsnap->writing);
3451 dout(" removing %p cap_snap %p follows %lld\n",
3452 inode, capsnap, follows);
3453 list_del(&capsnap->ci_item);
3454 if (__finish_cap_flush(NULL, ci, &capsnap->cap_flush))
3457 spin_lock(&mdsc->cap_dirty_lock);
3459 if (list_empty(&ci->i_cap_flush_list))
3460 list_del_init(&ci->i_flushing_item);
3462 if (__finish_cap_flush(mdsc, NULL, &capsnap->cap_flush))
3465 spin_unlock(&mdsc->cap_dirty_lock);
3467 spin_unlock(&ci->i_ceph_lock);
3469 ceph_put_snap_context(capsnap->context);
3470 ceph_put_cap_snap(capsnap);
3472 wake_up_all(&ci->i_cap_wq);
3474 wake_up_all(&mdsc->cap_flushing_wq);
3480 * Handle TRUNC from MDS, indicating file truncation.
3482 * caller hold s_mutex.
3484 static void handle_cap_trunc(struct inode *inode,
3485 struct ceph_mds_caps *trunc,
3486 struct ceph_mds_session *session)
3487 __releases(ci->i_ceph_lock)
3489 struct ceph_inode_info *ci = ceph_inode(inode);
3490 int mds = session->s_mds;
3491 int seq = le32_to_cpu(trunc->seq);
3492 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
3493 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
3494 u64 size = le64_to_cpu(trunc->size);
3495 int implemented = 0;
3496 int dirty = __ceph_caps_dirty(ci);
3497 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
3498 int queue_trunc = 0;
3500 issued |= implemented | dirty;
3502 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
3503 inode, mds, seq, truncate_size, truncate_seq);
3504 queue_trunc = ceph_fill_file_size(inode, issued,
3505 truncate_seq, truncate_size, size);
3506 spin_unlock(&ci->i_ceph_lock);
3509 ceph_queue_vmtruncate(inode);
3513 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
3514 * different one. If we are the most recent migration we've seen (as
3515 * indicated by mseq), make note of the migrating cap bits for the
3516 * duration (until we see the corresponding IMPORT).
3518 * caller holds s_mutex
3520 static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
3521 struct ceph_mds_cap_peer *ph,
3522 struct ceph_mds_session *session)
3524 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
3525 struct ceph_mds_session *tsession = NULL;
3526 struct ceph_cap *cap, *tcap, *new_cap = NULL;
3527 struct ceph_inode_info *ci = ceph_inode(inode);
3529 unsigned mseq = le32_to_cpu(ex->migrate_seq);
3530 unsigned t_seq, t_mseq;
3532 int mds = session->s_mds;
3535 t_cap_id = le64_to_cpu(ph->cap_id);
3536 t_seq = le32_to_cpu(ph->seq);
3537 t_mseq = le32_to_cpu(ph->mseq);
3538 target = le32_to_cpu(ph->mds);
3540 t_cap_id = t_seq = t_mseq = 0;
3544 dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n",
3545 inode, ci, mds, mseq, target);
3547 spin_lock(&ci->i_ceph_lock);
3548 cap = __get_cap_for_mds(ci, mds);
3549 if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
3553 __ceph_remove_cap(cap, false);
3554 if (!ci->i_auth_cap)
3555 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
3560 * now we know we haven't received the cap import message yet
3561 * because the exported cap still exist.
3564 issued = cap->issued;
3565 if (issued != cap->implemented)
3566 pr_err_ratelimited("handle_cap_export: issued != implemented: "
3567 "ino (%llx.%llx) mds%d seq %d mseq %d "
3568 "issued %s implemented %s\n",
3569 ceph_vinop(inode), mds, cap->seq, cap->mseq,
3570 ceph_cap_string(issued),
3571 ceph_cap_string(cap->implemented));
3574 tcap = __get_cap_for_mds(ci, target);
3576 /* already have caps from the target */
3577 if (tcap->cap_id == t_cap_id &&
3578 ceph_seq_cmp(tcap->seq, t_seq) < 0) {
3579 dout(" updating import cap %p mds%d\n", tcap, target);
3580 tcap->cap_id = t_cap_id;
3581 tcap->seq = t_seq - 1;
3582 tcap->issue_seq = t_seq - 1;
3583 tcap->mseq = t_mseq;
3584 tcap->issued |= issued;
3585 tcap->implemented |= issued;
3586 if (cap == ci->i_auth_cap)
3587 ci->i_auth_cap = tcap;
3589 if (!list_empty(&ci->i_cap_flush_list) &&
3590 ci->i_auth_cap == tcap) {
3591 spin_lock(&mdsc->cap_dirty_lock);
3592 list_move_tail(&ci->i_flushing_item,
3593 &tcap->session->s_cap_flushing);
3594 spin_unlock(&mdsc->cap_dirty_lock);
3597 __ceph_remove_cap(cap, false);
3599 } else if (tsession) {
3600 /* add placeholder for the export tagert */
3601 int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
3603 ceph_add_cap(inode, tsession, t_cap_id, -1, issued, 0,
3604 t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
3606 if (!list_empty(&ci->i_cap_flush_list) &&
3607 ci->i_auth_cap == tcap) {
3608 spin_lock(&mdsc->cap_dirty_lock);
3609 list_move_tail(&ci->i_flushing_item,
3610 &tcap->session->s_cap_flushing);
3611 spin_unlock(&mdsc->cap_dirty_lock);
3614 __ceph_remove_cap(cap, false);
3618 spin_unlock(&ci->i_ceph_lock);
3619 mutex_unlock(&session->s_mutex);
3621 /* open target session */
3622 tsession = ceph_mdsc_open_export_target_session(mdsc, target);
3623 if (!IS_ERR(tsession)) {
3625 mutex_lock(&session->s_mutex);
3626 mutex_lock_nested(&tsession->s_mutex,
3627 SINGLE_DEPTH_NESTING);
3629 mutex_lock(&tsession->s_mutex);
3630 mutex_lock_nested(&session->s_mutex,
3631 SINGLE_DEPTH_NESTING);
3633 new_cap = ceph_get_cap(mdsc, NULL);
3642 spin_unlock(&ci->i_ceph_lock);
3643 mutex_unlock(&session->s_mutex);
3645 mutex_unlock(&tsession->s_mutex);
3646 ceph_put_mds_session(tsession);
3649 ceph_put_cap(mdsc, new_cap);
3653 * Handle cap IMPORT.
3655 * caller holds s_mutex. acquires i_ceph_lock
3657 static void handle_cap_import(struct ceph_mds_client *mdsc,
3658 struct inode *inode, struct ceph_mds_caps *im,
3659 struct ceph_mds_cap_peer *ph,
3660 struct ceph_mds_session *session,
3661 struct ceph_cap **target_cap, int *old_issued)
3662 __acquires(ci->i_ceph_lock)
3664 struct ceph_inode_info *ci = ceph_inode(inode);
3665 struct ceph_cap *cap, *ocap, *new_cap = NULL;
3666 int mds = session->s_mds;
3668 unsigned caps = le32_to_cpu(im->caps);
3669 unsigned wanted = le32_to_cpu(im->wanted);
3670 unsigned seq = le32_to_cpu(im->seq);
3671 unsigned mseq = le32_to_cpu(im->migrate_seq);
3672 u64 realmino = le64_to_cpu(im->realm);
3673 u64 cap_id = le64_to_cpu(im->cap_id);
3678 p_cap_id = le64_to_cpu(ph->cap_id);
3679 peer = le32_to_cpu(ph->mds);
3685 dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n",
3686 inode, ci, mds, mseq, peer);
3689 spin_lock(&ci->i_ceph_lock);
3690 cap = __get_cap_for_mds(ci, mds);
3693 spin_unlock(&ci->i_ceph_lock);
3694 new_cap = ceph_get_cap(mdsc, NULL);
3700 ceph_put_cap(mdsc, new_cap);
3705 __ceph_caps_issued(ci, &issued);
3706 issued |= __ceph_caps_dirty(ci);
3708 ceph_add_cap(inode, session, cap_id, -1, caps, wanted, seq, mseq,
3709 realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
3711 ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
3712 if (ocap && ocap->cap_id == p_cap_id) {
3713 dout(" remove export cap %p mds%d flags %d\n",
3714 ocap, peer, ph->flags);
3715 if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
3716 (ocap->seq != le32_to_cpu(ph->seq) ||
3717 ocap->mseq != le32_to_cpu(ph->mseq))) {
3718 pr_err_ratelimited("handle_cap_import: "
3719 "mismatched seq/mseq: ino (%llx.%llx) "
3720 "mds%d seq %d mseq %d importer mds%d "
3721 "has peer seq %d mseq %d\n",
3722 ceph_vinop(inode), peer, ocap->seq,
3723 ocap->mseq, mds, le32_to_cpu(ph->seq),
3724 le32_to_cpu(ph->mseq));
3726 __ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
3729 /* make sure we re-request max_size, if necessary */
3730 ci->i_requested_max_size = 0;
3732 *old_issued = issued;
3737 * Handle a caps message from the MDS.
3739 * Identify the appropriate session, inode, and call the right handler
3740 * based on the cap op.
3742 void ceph_handle_caps(struct ceph_mds_session *session,
3743 struct ceph_msg *msg)
3745 struct ceph_mds_client *mdsc = session->s_mdsc;
3746 struct inode *inode;
3747 struct ceph_inode_info *ci;
3748 struct ceph_cap *cap;
3749 struct ceph_mds_caps *h;
3750 struct ceph_mds_cap_peer *peer = NULL;
3751 struct ceph_snap_realm *realm = NULL;
3753 int msg_version = le16_to_cpu(msg->hdr.version);
3755 struct ceph_vino vino;
3757 size_t snaptrace_len;
3759 struct cap_extra_info extra_info = {};
3761 dout("handle_caps from mds%d\n", session->s_mds);
3764 end = msg->front.iov_base + msg->front.iov_len;
3765 if (msg->front.iov_len < sizeof(*h))
3767 h = msg->front.iov_base;
3768 op = le32_to_cpu(h->op);
3769 vino.ino = le64_to_cpu(h->ino);
3770 vino.snap = CEPH_NOSNAP;
3771 seq = le32_to_cpu(h->seq);
3772 mseq = le32_to_cpu(h->migrate_seq);
3775 snaptrace_len = le32_to_cpu(h->snap_trace_len);
3776 p = snaptrace + snaptrace_len;
3778 if (msg_version >= 2) {
3780 ceph_decode_32_safe(&p, end, flock_len, bad);
3781 if (p + flock_len > end)
3786 if (msg_version >= 3) {
3787 if (op == CEPH_CAP_OP_IMPORT) {
3788 if (p + sizeof(*peer) > end)
3792 } else if (op == CEPH_CAP_OP_EXPORT) {
3793 /* recorded in unused fields */
3794 peer = (void *)&h->size;
3798 if (msg_version >= 4) {
3799 ceph_decode_64_safe(&p, end, extra_info.inline_version, bad);
3800 ceph_decode_32_safe(&p, end, extra_info.inline_len, bad);
3801 if (p + extra_info.inline_len > end)
3803 extra_info.inline_data = p;
3804 p += extra_info.inline_len;
3807 if (msg_version >= 5) {
3808 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
3811 ceph_decode_32_safe(&p, end, epoch_barrier, bad);
3812 ceph_osdc_update_epoch_barrier(osdc, epoch_barrier);
3815 if (msg_version >= 8) {
3817 u32 caller_uid, caller_gid;
3821 ceph_decode_64_safe(&p, end, flush_tid, bad);
3823 ceph_decode_32_safe(&p, end, caller_uid, bad);
3824 ceph_decode_32_safe(&p, end, caller_gid, bad);
3826 ceph_decode_32_safe(&p, end, pool_ns_len, bad);
3827 if (pool_ns_len > 0) {
3828 ceph_decode_need(&p, end, pool_ns_len, bad);
3829 extra_info.pool_ns =
3830 ceph_find_or_create_string(p, pool_ns_len);
3835 if (msg_version >= 11) {
3836 struct ceph_timespec *btime;
3841 if (p + sizeof(*btime) > end)
3844 p += sizeof(*btime);
3845 ceph_decode_64_safe(&p, end, change_attr, bad);
3847 ceph_decode_32_safe(&p, end, flags, bad);
3849 extra_info.dirstat_valid = true;
3850 ceph_decode_64_safe(&p, end, extra_info.nfiles, bad);
3851 ceph_decode_64_safe(&p, end, extra_info.nsubdirs, bad);
3855 inode = ceph_find_inode(mdsc->fsc->sb, vino);
3856 ci = ceph_inode(inode);
3857 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
3860 mutex_lock(&session->s_mutex);
3862 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
3866 dout(" i don't have ino %llx\n", vino.ino);
3868 if (op == CEPH_CAP_OP_IMPORT) {
3869 cap = ceph_get_cap(mdsc, NULL);
3870 cap->cap_ino = vino.ino;
3871 cap->queue_release = 1;
3872 cap->cap_id = le64_to_cpu(h->cap_id);
3875 cap->issue_seq = seq;
3876 spin_lock(&session->s_cap_lock);
3877 list_add_tail(&cap->session_caps,
3878 &session->s_cap_releases);
3879 session->s_num_cap_releases++;
3880 spin_unlock(&session->s_cap_lock);
3882 goto flush_cap_releases;
3885 /* these will work even if we don't have a cap yet */
3887 case CEPH_CAP_OP_FLUSHSNAP_ACK:
3888 handle_cap_flushsnap_ack(inode, le64_to_cpu(msg->hdr.tid),
3892 case CEPH_CAP_OP_EXPORT:
3893 handle_cap_export(inode, h, peer, session);
3896 case CEPH_CAP_OP_IMPORT:
3898 if (snaptrace_len) {
3899 down_write(&mdsc->snap_rwsem);
3900 ceph_update_snap_trace(mdsc, snaptrace,
3901 snaptrace + snaptrace_len,
3903 downgrade_write(&mdsc->snap_rwsem);
3905 down_read(&mdsc->snap_rwsem);
3907 handle_cap_import(mdsc, inode, h, peer, session,
3908 &cap, &extra_info.issued);
3909 handle_cap_grant(inode, session, cap,
3910 h, msg->middle, &extra_info);
3912 ceph_put_snap_realm(mdsc, realm);
3916 /* the rest require a cap */
3917 spin_lock(&ci->i_ceph_lock);
3918 cap = __get_cap_for_mds(ceph_inode(inode), session->s_mds);
3920 dout(" no cap on %p ino %llx.%llx from mds%d\n",
3921 inode, ceph_ino(inode), ceph_snap(inode),
3923 spin_unlock(&ci->i_ceph_lock);
3924 goto flush_cap_releases;
3927 /* note that each of these drops i_ceph_lock for us */
3929 case CEPH_CAP_OP_REVOKE:
3930 case CEPH_CAP_OP_GRANT:
3931 __ceph_caps_issued(ci, &extra_info.issued);
3932 extra_info.issued |= __ceph_caps_dirty(ci);
3933 handle_cap_grant(inode, session, cap,
3934 h, msg->middle, &extra_info);
3937 case CEPH_CAP_OP_FLUSH_ACK:
3938 handle_cap_flush_ack(inode, le64_to_cpu(msg->hdr.tid),
3942 case CEPH_CAP_OP_TRUNC:
3943 handle_cap_trunc(inode, h, session);
3947 spin_unlock(&ci->i_ceph_lock);
3948 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
3949 ceph_cap_op_name(op));
3956 * send any cap release message to try to move things
3957 * along for the mds (who clearly thinks we still have this
3960 ceph_send_cap_releases(mdsc, session);
3963 mutex_unlock(&session->s_mutex);
3966 ceph_put_string(extra_info.pool_ns);
3970 pr_err("ceph_handle_caps: corrupt message\n");
3976 * Delayed work handler to process end of delayed cap release LRU list.
3978 void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
3980 struct inode *inode;
3981 struct ceph_inode_info *ci;
3982 int flags = CHECK_CAPS_NODELAY;
3984 dout("check_delayed_caps\n");
3986 spin_lock(&mdsc->cap_delay_lock);
3987 if (list_empty(&mdsc->cap_delay_list))
3989 ci = list_first_entry(&mdsc->cap_delay_list,
3990 struct ceph_inode_info,
3992 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
3993 time_before(jiffies, ci->i_hold_caps_max))
3995 list_del_init(&ci->i_cap_delay_list);
3997 inode = igrab(&ci->vfs_inode);
3998 spin_unlock(&mdsc->cap_delay_lock);
4001 dout("check_delayed_caps on %p\n", inode);
4002 ceph_check_caps(ci, flags, NULL);
4006 spin_unlock(&mdsc->cap_delay_lock);
4010 * Flush all dirty caps to the mds
4012 void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
4014 struct ceph_inode_info *ci;
4015 struct inode *inode;
4017 dout("flush_dirty_caps\n");
4018 spin_lock(&mdsc->cap_dirty_lock);
4019 while (!list_empty(&mdsc->cap_dirty)) {
4020 ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
4022 inode = &ci->vfs_inode;
4024 dout("flush_dirty_caps %p\n", inode);
4025 spin_unlock(&mdsc->cap_dirty_lock);
4026 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
4028 spin_lock(&mdsc->cap_dirty_lock);
4030 spin_unlock(&mdsc->cap_dirty_lock);
4031 dout("flush_dirty_caps done\n");
4034 void __ceph_get_fmode(struct ceph_inode_info *ci, int fmode)
4037 int bits = (fmode << 1) | 1;
4038 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
4039 if (bits & (1 << i))
4040 ci->i_nr_by_mode[i]++;
4045 * Drop open file reference. If we were the last open file,
4046 * we may need to release capabilities to the MDS (or schedule
4047 * their delayed release).
4049 void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
4052 int bits = (fmode << 1) | 1;
4053 spin_lock(&ci->i_ceph_lock);
4054 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
4055 if (bits & (1 << i)) {
4056 BUG_ON(ci->i_nr_by_mode[i] == 0);
4057 if (--ci->i_nr_by_mode[i] == 0)
4061 dout("put_fmode %p fmode %d {%d,%d,%d,%d}\n",
4062 &ci->vfs_inode, fmode,
4063 ci->i_nr_by_mode[0], ci->i_nr_by_mode[1],
4064 ci->i_nr_by_mode[2], ci->i_nr_by_mode[3]);
4065 spin_unlock(&ci->i_ceph_lock);
4067 if (last && ci->i_vino.snap == CEPH_NOSNAP)
4068 ceph_check_caps(ci, 0, NULL);
4072 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
4073 * looks like the link count will hit 0, drop any other caps (other
4074 * than PIN) we don't specifically want (due to the file still being
4077 int ceph_drop_caps_for_unlink(struct inode *inode)
4079 struct ceph_inode_info *ci = ceph_inode(inode);
4080 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
4082 spin_lock(&ci->i_ceph_lock);
4083 if (inode->i_nlink == 1) {
4084 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
4086 ci->i_ceph_flags |= CEPH_I_NODELAY;
4087 if (__ceph_caps_dirty(ci)) {
4088 struct ceph_mds_client *mdsc =
4089 ceph_inode_to_client(inode)->mdsc;
4090 __cap_delay_requeue_front(mdsc, ci);
4093 spin_unlock(&ci->i_ceph_lock);
4098 * Helpers for embedding cap and dentry lease releases into mds
4101 * @force is used by dentry_release (below) to force inclusion of a
4102 * record for the directory inode, even when there aren't any caps to
4105 int ceph_encode_inode_release(void **p, struct inode *inode,
4106 int mds, int drop, int unless, int force)
4108 struct ceph_inode_info *ci = ceph_inode(inode);
4109 struct ceph_cap *cap;
4110 struct ceph_mds_request_release *rel = *p;
4114 spin_lock(&ci->i_ceph_lock);
4115 used = __ceph_caps_used(ci);
4116 dirty = __ceph_caps_dirty(ci);
4118 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
4119 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
4120 ceph_cap_string(unless));
4122 /* only drop unused, clean caps */
4123 drop &= ~(used | dirty);
4125 cap = __get_cap_for_mds(ci, mds);
4126 if (cap && __cap_is_valid(cap)) {
4127 unless &= cap->issued;
4129 if (unless & CEPH_CAP_AUTH_EXCL)
4130 drop &= ~CEPH_CAP_AUTH_SHARED;
4131 if (unless & CEPH_CAP_LINK_EXCL)
4132 drop &= ~CEPH_CAP_LINK_SHARED;
4133 if (unless & CEPH_CAP_XATTR_EXCL)
4134 drop &= ~CEPH_CAP_XATTR_SHARED;
4135 if (unless & CEPH_CAP_FILE_EXCL)
4136 drop &= ~CEPH_CAP_FILE_SHARED;
4139 if (force || (cap->issued & drop)) {
4140 if (cap->issued & drop) {
4141 int wanted = __ceph_caps_wanted(ci);
4142 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0)
4143 wanted |= cap->mds_wanted;
4144 dout("encode_inode_release %p cap %p "
4145 "%s -> %s, wanted %s -> %s\n", inode, cap,
4146 ceph_cap_string(cap->issued),
4147 ceph_cap_string(cap->issued & ~drop),
4148 ceph_cap_string(cap->mds_wanted),
4149 ceph_cap_string(wanted));
4151 cap->issued &= ~drop;
4152 cap->implemented &= ~drop;
4153 cap->mds_wanted = wanted;
4155 dout("encode_inode_release %p cap %p %s"
4156 " (force)\n", inode, cap,
4157 ceph_cap_string(cap->issued));
4160 rel->ino = cpu_to_le64(ceph_ino(inode));
4161 rel->cap_id = cpu_to_le64(cap->cap_id);
4162 rel->seq = cpu_to_le32(cap->seq);
4163 rel->issue_seq = cpu_to_le32(cap->issue_seq);
4164 rel->mseq = cpu_to_le32(cap->mseq);
4165 rel->caps = cpu_to_le32(cap->implemented);
4166 rel->wanted = cpu_to_le32(cap->mds_wanted);
4172 dout("encode_inode_release %p cap %p %s (noop)\n",
4173 inode, cap, ceph_cap_string(cap->issued));
4176 spin_unlock(&ci->i_ceph_lock);
4180 int ceph_encode_dentry_release(void **p, struct dentry *dentry,
4182 int mds, int drop, int unless)
4184 struct dentry *parent = NULL;
4185 struct ceph_mds_request_release *rel = *p;
4186 struct ceph_dentry_info *di = ceph_dentry(dentry);
4191 * force an record for the directory caps if we have a dentry lease.
4192 * this is racy (can't take i_ceph_lock and d_lock together), but it
4193 * doesn't have to be perfect; the mds will revoke anything we don't
4196 spin_lock(&dentry->d_lock);
4197 if (di->lease_session && di->lease_session->s_mds == mds)
4200 parent = dget(dentry->d_parent);
4201 dir = d_inode(parent);
4203 spin_unlock(&dentry->d_lock);
4205 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
4208 spin_lock(&dentry->d_lock);
4209 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
4210 dout("encode_dentry_release %p mds%d seq %d\n",
4211 dentry, mds, (int)di->lease_seq);
4212 rel->dname_len = cpu_to_le32(dentry->d_name.len);
4213 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
4214 *p += dentry->d_name.len;
4215 rel->dname_seq = cpu_to_le32(di->lease_seq);
4216 __ceph_mdsc_drop_dentry_lease(dentry);
4218 spin_unlock(&dentry->d_lock);