1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
5 #include <linux/kernel.h>
6 #include <linux/sched/signal.h>
7 #include <linux/slab.h>
8 #include <linux/vmalloc.h>
9 #include <linux/wait.h>
10 #include <linux/writeback.h>
11 #include <linux/iversion.h>
12 #include <linux/filelock.h>
15 #include "mds_client.h"
18 #include <linux/ceph/decode.h>
19 #include <linux/ceph/messenger.h>
22 * Capability management
24 * The Ceph metadata servers control client access to inode metadata
25 * and file data by issuing capabilities, granting clients permission
26 * to read and/or write both inode field and file data to OSDs
27 * (storage nodes). Each capability consists of a set of bits
28 * indicating which operations are allowed.
30 * If the client holds a *_SHARED cap, the client has a coherent value
31 * that can be safely read from the cached inode.
33 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
34 * client is allowed to change inode attributes (e.g., file size,
35 * mtime), note its dirty state in the ceph_cap, and asynchronously
36 * flush that metadata change to the MDS.
38 * In the event of a conflicting operation (perhaps by another
39 * client), the MDS will revoke the conflicting client capabilities.
41 * In order for a client to cache an inode, it must hold a capability
42 * with at least one MDS server. When inodes are released, release
43 * notifications are batched and periodically sent en masse to the MDS
44 * cluster to release server state.
47 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc);
48 static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
49 struct ceph_mds_session *session,
50 struct ceph_inode_info *ci,
51 u64 oldest_flush_tid);
54 * Generate readable cap strings for debugging output.
56 #define MAX_CAP_STR 20
57 static char cap_str[MAX_CAP_STR][40];
58 static DEFINE_SPINLOCK(cap_str_lock);
59 static int last_cap_str;
61 static char *gcap_string(char *s, int c)
63 if (c & CEPH_CAP_GSHARED)
65 if (c & CEPH_CAP_GEXCL)
67 if (c & CEPH_CAP_GCACHE)
73 if (c & CEPH_CAP_GBUFFER)
75 if (c & CEPH_CAP_GWREXTEND)
77 if (c & CEPH_CAP_GLAZYIO)
82 const char *ceph_cap_string(int caps)
88 spin_lock(&cap_str_lock);
90 if (last_cap_str == MAX_CAP_STR)
92 spin_unlock(&cap_str_lock);
96 if (caps & CEPH_CAP_PIN)
99 c = (caps >> CEPH_CAP_SAUTH) & 3;
102 s = gcap_string(s, c);
105 c = (caps >> CEPH_CAP_SLINK) & 3;
108 s = gcap_string(s, c);
111 c = (caps >> CEPH_CAP_SXATTR) & 3;
114 s = gcap_string(s, c);
117 c = caps >> CEPH_CAP_SFILE;
120 s = gcap_string(s, c);
129 void ceph_caps_init(struct ceph_mds_client *mdsc)
131 INIT_LIST_HEAD(&mdsc->caps_list);
132 spin_lock_init(&mdsc->caps_list_lock);
135 void ceph_caps_finalize(struct ceph_mds_client *mdsc)
137 struct ceph_cap *cap;
139 spin_lock(&mdsc->caps_list_lock);
140 while (!list_empty(&mdsc->caps_list)) {
141 cap = list_first_entry(&mdsc->caps_list,
142 struct ceph_cap, caps_item);
143 list_del(&cap->caps_item);
144 kmem_cache_free(ceph_cap_cachep, cap);
146 mdsc->caps_total_count = 0;
147 mdsc->caps_avail_count = 0;
148 mdsc->caps_use_count = 0;
149 mdsc->caps_reserve_count = 0;
150 mdsc->caps_min_count = 0;
151 spin_unlock(&mdsc->caps_list_lock);
154 void ceph_adjust_caps_max_min(struct ceph_mds_client *mdsc,
155 struct ceph_mount_options *fsopt)
157 spin_lock(&mdsc->caps_list_lock);
158 mdsc->caps_min_count = fsopt->max_readdir;
159 if (mdsc->caps_min_count < 1024)
160 mdsc->caps_min_count = 1024;
161 mdsc->caps_use_max = fsopt->caps_max;
162 if (mdsc->caps_use_max > 0 &&
163 mdsc->caps_use_max < mdsc->caps_min_count)
164 mdsc->caps_use_max = mdsc->caps_min_count;
165 spin_unlock(&mdsc->caps_list_lock);
168 static void __ceph_unreserve_caps(struct ceph_mds_client *mdsc, int nr_caps)
170 struct ceph_cap *cap;
174 BUG_ON(mdsc->caps_reserve_count < nr_caps);
175 mdsc->caps_reserve_count -= nr_caps;
176 if (mdsc->caps_avail_count >=
177 mdsc->caps_reserve_count + mdsc->caps_min_count) {
178 mdsc->caps_total_count -= nr_caps;
179 for (i = 0; i < nr_caps; i++) {
180 cap = list_first_entry(&mdsc->caps_list,
181 struct ceph_cap, caps_item);
182 list_del(&cap->caps_item);
183 kmem_cache_free(ceph_cap_cachep, cap);
186 mdsc->caps_avail_count += nr_caps;
189 dout("%s: caps %d = %d used + %d resv + %d avail\n",
191 mdsc->caps_total_count, mdsc->caps_use_count,
192 mdsc->caps_reserve_count, mdsc->caps_avail_count);
193 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
194 mdsc->caps_reserve_count +
195 mdsc->caps_avail_count);
200 * Called under mdsc->mutex.
202 int ceph_reserve_caps(struct ceph_mds_client *mdsc,
203 struct ceph_cap_reservation *ctx, int need)
206 struct ceph_cap *cap;
211 bool trimmed = false;
212 struct ceph_mds_session *s;
215 dout("reserve caps ctx=%p need=%d\n", ctx, need);
217 /* first reserve any caps that are already allocated */
218 spin_lock(&mdsc->caps_list_lock);
219 if (mdsc->caps_avail_count >= need)
222 have = mdsc->caps_avail_count;
223 mdsc->caps_avail_count -= have;
224 mdsc->caps_reserve_count += have;
225 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
226 mdsc->caps_reserve_count +
227 mdsc->caps_avail_count);
228 spin_unlock(&mdsc->caps_list_lock);
230 for (i = have; i < need; ) {
231 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
233 list_add(&cap->caps_item, &newcaps);
240 for (j = 0; j < mdsc->max_sessions; j++) {
241 s = __ceph_lookup_mds_session(mdsc, j);
244 mutex_unlock(&mdsc->mutex);
246 mutex_lock(&s->s_mutex);
247 max_caps = s->s_nr_caps - (need - i);
248 ceph_trim_caps(mdsc, s, max_caps);
249 mutex_unlock(&s->s_mutex);
251 ceph_put_mds_session(s);
252 mutex_lock(&mdsc->mutex);
256 spin_lock(&mdsc->caps_list_lock);
257 if (mdsc->caps_avail_count) {
259 if (mdsc->caps_avail_count >= need - i)
260 more_have = need - i;
262 more_have = mdsc->caps_avail_count;
266 mdsc->caps_avail_count -= more_have;
267 mdsc->caps_reserve_count += more_have;
270 spin_unlock(&mdsc->caps_list_lock);
275 pr_warn("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
276 ctx, need, have + alloc);
282 BUG_ON(have + alloc != need);
287 spin_lock(&mdsc->caps_list_lock);
288 mdsc->caps_total_count += alloc;
289 mdsc->caps_reserve_count += alloc;
290 list_splice(&newcaps, &mdsc->caps_list);
292 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
293 mdsc->caps_reserve_count +
294 mdsc->caps_avail_count);
297 __ceph_unreserve_caps(mdsc, have + alloc);
299 spin_unlock(&mdsc->caps_list_lock);
301 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
302 ctx, mdsc->caps_total_count, mdsc->caps_use_count,
303 mdsc->caps_reserve_count, mdsc->caps_avail_count);
307 void ceph_unreserve_caps(struct ceph_mds_client *mdsc,
308 struct ceph_cap_reservation *ctx)
310 bool reclaim = false;
314 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
315 spin_lock(&mdsc->caps_list_lock);
316 __ceph_unreserve_caps(mdsc, ctx->count);
319 if (mdsc->caps_use_max > 0 &&
320 mdsc->caps_use_count > mdsc->caps_use_max)
322 spin_unlock(&mdsc->caps_list_lock);
325 ceph_reclaim_caps_nr(mdsc, ctx->used);
328 struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc,
329 struct ceph_cap_reservation *ctx)
331 struct ceph_cap *cap = NULL;
333 /* temporary, until we do something about cap import/export */
335 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
337 spin_lock(&mdsc->caps_list_lock);
338 mdsc->caps_use_count++;
339 mdsc->caps_total_count++;
340 spin_unlock(&mdsc->caps_list_lock);
342 spin_lock(&mdsc->caps_list_lock);
343 if (mdsc->caps_avail_count) {
344 BUG_ON(list_empty(&mdsc->caps_list));
346 mdsc->caps_avail_count--;
347 mdsc->caps_use_count++;
348 cap = list_first_entry(&mdsc->caps_list,
349 struct ceph_cap, caps_item);
350 list_del(&cap->caps_item);
352 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
353 mdsc->caps_reserve_count + mdsc->caps_avail_count);
355 spin_unlock(&mdsc->caps_list_lock);
361 spin_lock(&mdsc->caps_list_lock);
362 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
363 ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
364 mdsc->caps_reserve_count, mdsc->caps_avail_count);
366 BUG_ON(ctx->count > mdsc->caps_reserve_count);
367 BUG_ON(list_empty(&mdsc->caps_list));
371 mdsc->caps_reserve_count--;
372 mdsc->caps_use_count++;
374 cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
375 list_del(&cap->caps_item);
377 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
378 mdsc->caps_reserve_count + mdsc->caps_avail_count);
379 spin_unlock(&mdsc->caps_list_lock);
383 void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
385 spin_lock(&mdsc->caps_list_lock);
386 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
387 cap, mdsc->caps_total_count, mdsc->caps_use_count,
388 mdsc->caps_reserve_count, mdsc->caps_avail_count);
389 mdsc->caps_use_count--;
391 * Keep some preallocated caps around (ceph_min_count), to
392 * avoid lots of free/alloc churn.
394 if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
395 mdsc->caps_min_count) {
396 mdsc->caps_total_count--;
397 kmem_cache_free(ceph_cap_cachep, cap);
399 mdsc->caps_avail_count++;
400 list_add(&cap->caps_item, &mdsc->caps_list);
403 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
404 mdsc->caps_reserve_count + mdsc->caps_avail_count);
405 spin_unlock(&mdsc->caps_list_lock);
408 void ceph_reservation_status(struct ceph_fs_client *fsc,
409 int *total, int *avail, int *used, int *reserved,
412 struct ceph_mds_client *mdsc = fsc->mdsc;
414 spin_lock(&mdsc->caps_list_lock);
417 *total = mdsc->caps_total_count;
419 *avail = mdsc->caps_avail_count;
421 *used = mdsc->caps_use_count;
423 *reserved = mdsc->caps_reserve_count;
425 *min = mdsc->caps_min_count;
427 spin_unlock(&mdsc->caps_list_lock);
431 * Find ceph_cap for given mds, if any.
433 * Called with i_ceph_lock held.
435 struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
437 struct ceph_cap *cap;
438 struct rb_node *n = ci->i_caps.rb_node;
441 cap = rb_entry(n, struct ceph_cap, ci_node);
444 else if (mds > cap->mds)
452 struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
454 struct ceph_cap *cap;
456 spin_lock(&ci->i_ceph_lock);
457 cap = __get_cap_for_mds(ci, mds);
458 spin_unlock(&ci->i_ceph_lock);
463 * Called under i_ceph_lock.
465 static void __insert_cap_node(struct ceph_inode_info *ci,
466 struct ceph_cap *new)
468 struct rb_node **p = &ci->i_caps.rb_node;
469 struct rb_node *parent = NULL;
470 struct ceph_cap *cap = NULL;
474 cap = rb_entry(parent, struct ceph_cap, ci_node);
475 if (new->mds < cap->mds)
477 else if (new->mds > cap->mds)
483 rb_link_node(&new->ci_node, parent, p);
484 rb_insert_color(&new->ci_node, &ci->i_caps);
488 * (re)set cap hold timeouts, which control the delayed release
489 * of unused caps back to the MDS. Should be called on cap use.
491 static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
492 struct ceph_inode_info *ci)
494 struct ceph_mount_options *opt = mdsc->fsc->mount_options;
495 ci->i_hold_caps_max = round_jiffies(jiffies +
496 opt->caps_wanted_delay_max * HZ);
497 dout("__cap_set_timeouts %p %lu\n", &ci->netfs.inode,
498 ci->i_hold_caps_max - jiffies);
502 * (Re)queue cap at the end of the delayed cap release list.
504 * If I_FLUSH is set, leave the inode at the front of the list.
506 * Caller holds i_ceph_lock
507 * -> we take mdsc->cap_delay_lock
509 static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
510 struct ceph_inode_info *ci)
512 dout("__cap_delay_requeue %p flags 0x%lx at %lu\n", &ci->netfs.inode,
513 ci->i_ceph_flags, ci->i_hold_caps_max);
514 if (!mdsc->stopping) {
515 spin_lock(&mdsc->cap_delay_lock);
516 if (!list_empty(&ci->i_cap_delay_list)) {
517 if (ci->i_ceph_flags & CEPH_I_FLUSH)
519 list_del_init(&ci->i_cap_delay_list);
521 __cap_set_timeouts(mdsc, ci);
522 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
524 spin_unlock(&mdsc->cap_delay_lock);
529 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
530 * indicating we should send a cap message to flush dirty metadata
531 * asap, and move to the front of the delayed cap list.
533 static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
534 struct ceph_inode_info *ci)
536 dout("__cap_delay_requeue_front %p\n", &ci->netfs.inode);
537 spin_lock(&mdsc->cap_delay_lock);
538 ci->i_ceph_flags |= CEPH_I_FLUSH;
539 if (!list_empty(&ci->i_cap_delay_list))
540 list_del_init(&ci->i_cap_delay_list);
541 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
542 spin_unlock(&mdsc->cap_delay_lock);
546 * Cancel delayed work on cap.
548 * Caller must hold i_ceph_lock.
550 static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
551 struct ceph_inode_info *ci)
553 dout("__cap_delay_cancel %p\n", &ci->netfs.inode);
554 if (list_empty(&ci->i_cap_delay_list))
556 spin_lock(&mdsc->cap_delay_lock);
557 list_del_init(&ci->i_cap_delay_list);
558 spin_unlock(&mdsc->cap_delay_lock);
561 /* Common issue checks for add_cap, handle_cap_grant. */
562 static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
565 unsigned had = __ceph_caps_issued(ci, NULL);
567 lockdep_assert_held(&ci->i_ceph_lock);
570 * Each time we receive FILE_CACHE anew, we increment
573 if (S_ISREG(ci->netfs.inode.i_mode) &&
574 (issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
575 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) {
580 * If FILE_SHARED is newly issued, mark dir not complete. We don't
581 * know what happened to this directory while we didn't have the cap.
582 * If FILE_SHARED is being revoked, also mark dir not complete. It
583 * stops on-going cached readdir.
585 if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) {
586 if (issued & CEPH_CAP_FILE_SHARED)
587 atomic_inc(&ci->i_shared_gen);
588 if (S_ISDIR(ci->netfs.inode.i_mode)) {
589 dout(" marking %p NOT complete\n", &ci->netfs.inode);
590 __ceph_dir_clear_complete(ci);
594 /* Wipe saved layout if we're losing DIR_CREATE caps */
595 if (S_ISDIR(ci->netfs.inode.i_mode) && (had & CEPH_CAP_DIR_CREATE) &&
596 !(issued & CEPH_CAP_DIR_CREATE)) {
597 ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
598 memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
603 * change_auth_cap_ses - move inode to appropriate lists when auth caps change
604 * @ci: inode to be moved
605 * @session: new auth caps session
607 void change_auth_cap_ses(struct ceph_inode_info *ci,
608 struct ceph_mds_session *session)
610 lockdep_assert_held(&ci->i_ceph_lock);
612 if (list_empty(&ci->i_dirty_item) && list_empty(&ci->i_flushing_item))
615 spin_lock(&session->s_mdsc->cap_dirty_lock);
616 if (!list_empty(&ci->i_dirty_item))
617 list_move(&ci->i_dirty_item, &session->s_cap_dirty);
618 if (!list_empty(&ci->i_flushing_item))
619 list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
620 spin_unlock(&session->s_mdsc->cap_dirty_lock);
624 * Add a capability under the given MDS session.
626 * Caller should hold session snap_rwsem (read) and ci->i_ceph_lock
628 * @fmode is the open file mode, if we are opening a file, otherwise
629 * it is < 0. (This is so we can atomically add the cap and add an
630 * open file reference to it.)
632 void ceph_add_cap(struct inode *inode,
633 struct ceph_mds_session *session, u64 cap_id,
634 unsigned issued, unsigned wanted,
635 unsigned seq, unsigned mseq, u64 realmino, int flags,
636 struct ceph_cap **new_cap)
638 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
639 struct ceph_inode_info *ci = ceph_inode(inode);
640 struct ceph_cap *cap;
641 int mds = session->s_mds;
645 lockdep_assert_held(&ci->i_ceph_lock);
647 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
648 session->s_mds, cap_id, ceph_cap_string(issued), seq);
650 gen = atomic_read(&session->s_cap_gen);
652 cap = __get_cap_for_mds(ci, mds);
658 cap->implemented = 0;
664 __insert_cap_node(ci, cap);
666 /* add to session cap list */
667 cap->session = session;
668 spin_lock(&session->s_cap_lock);
669 list_add_tail(&cap->session_caps, &session->s_caps);
670 session->s_nr_caps++;
671 atomic64_inc(&mdsc->metric.total_caps);
672 spin_unlock(&session->s_cap_lock);
674 spin_lock(&session->s_cap_lock);
675 list_move_tail(&cap->session_caps, &session->s_caps);
676 spin_unlock(&session->s_cap_lock);
678 if (cap->cap_gen < gen)
679 cap->issued = cap->implemented = CEPH_CAP_PIN;
682 * auth mds of the inode changed. we received the cap export
683 * message, but still haven't received the cap import message.
684 * handle_cap_export() updated the new auth MDS' cap.
686 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
687 * a message that was send before the cap import message. So
690 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
691 WARN_ON(cap != ci->i_auth_cap);
692 WARN_ON(cap->cap_id != cap_id);
695 issued |= cap->issued;
696 flags |= CEPH_CAP_FLAG_AUTH;
700 if (!ci->i_snap_realm ||
701 ((flags & CEPH_CAP_FLAG_AUTH) &&
702 realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) {
704 * add this inode to the appropriate snap realm
706 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
709 ceph_change_snap_realm(inode, realm);
711 WARN(1, "%s: couldn't find snap realm 0x%llx (ino 0x%llx oldrealm 0x%llx)\n",
712 __func__, realmino, ci->i_vino.ino,
713 ci->i_snap_realm ? ci->i_snap_realm->ino : 0);
716 __check_cap_issue(ci, cap, issued);
719 * If we are issued caps we don't want, or the mds' wanted
720 * value appears to be off, queue a check so we'll release
721 * later and/or update the mds wanted value.
723 actual_wanted = __ceph_caps_wanted(ci);
724 if ((wanted & ~actual_wanted) ||
725 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
726 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
727 ceph_cap_string(issued), ceph_cap_string(wanted),
728 ceph_cap_string(actual_wanted));
729 __cap_delay_requeue(mdsc, ci);
732 if (flags & CEPH_CAP_FLAG_AUTH) {
733 if (!ci->i_auth_cap ||
734 ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
735 if (ci->i_auth_cap &&
736 ci->i_auth_cap->session != cap->session)
737 change_auth_cap_ses(ci, cap->session);
738 ci->i_auth_cap = cap;
739 cap->mds_wanted = wanted;
742 WARN_ON(ci->i_auth_cap == cap);
745 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
746 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
747 ceph_cap_string(issued|cap->issued), seq, mds);
748 cap->cap_id = cap_id;
749 cap->issued = issued;
750 cap->implemented |= issued;
751 if (ceph_seq_cmp(mseq, cap->mseq) > 0)
752 cap->mds_wanted = wanted;
754 cap->mds_wanted |= wanted;
756 cap->issue_seq = seq;
759 wake_up_all(&ci->i_cap_wq);
763 * Return true if cap has not timed out and belongs to the current
764 * generation of the MDS session (i.e. has not gone 'stale' due to
765 * us losing touch with the mds).
767 static int __cap_is_valid(struct ceph_cap *cap)
772 gen = atomic_read(&cap->session->s_cap_gen);
773 ttl = cap->session->s_cap_ttl;
775 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
776 dout("__cap_is_valid %p cap %p issued %s "
777 "but STALE (gen %u vs %u)\n", &cap->ci->netfs.inode,
778 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
786 * Return set of valid cap bits issued to us. Note that caps time
787 * out, and may be invalidated in bulk if the client session times out
788 * and session->s_cap_gen is bumped.
790 int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
792 int have = ci->i_snap_caps;
793 struct ceph_cap *cap;
798 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
799 cap = rb_entry(p, struct ceph_cap, ci_node);
800 if (!__cap_is_valid(cap))
802 dout("__ceph_caps_issued %p cap %p issued %s\n",
803 &ci->netfs.inode, cap, ceph_cap_string(cap->issued));
806 *implemented |= cap->implemented;
809 * exclude caps issued by non-auth MDS, but are been revoking
810 * by the auth MDS. The non-auth MDS should be revoking/exporting
811 * these caps, but the message is delayed.
813 if (ci->i_auth_cap) {
814 cap = ci->i_auth_cap;
815 have &= ~cap->implemented | cap->issued;
821 * Get cap bits issued by caps other than @ocap
823 int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
825 int have = ci->i_snap_caps;
826 struct ceph_cap *cap;
829 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
830 cap = rb_entry(p, struct ceph_cap, ci_node);
833 if (!__cap_is_valid(cap))
841 * Move a cap to the end of the LRU (oldest caps at list head, newest
844 static void __touch_cap(struct ceph_cap *cap)
846 struct ceph_mds_session *s = cap->session;
848 spin_lock(&s->s_cap_lock);
849 if (!s->s_cap_iterator) {
850 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->netfs.inode, cap,
852 list_move_tail(&cap->session_caps, &s->s_caps);
854 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
855 &cap->ci->netfs.inode, cap, s->s_mds);
857 spin_unlock(&s->s_cap_lock);
861 * Check if we hold the given mask. If so, move the cap(s) to the
862 * front of their respective LRUs. (This is the preferred way for
863 * callers to check for caps they want.)
865 int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
867 struct ceph_cap *cap;
869 int have = ci->i_snap_caps;
871 if ((have & mask) == mask) {
872 dout("__ceph_caps_issued_mask ino 0x%llx snap issued %s"
873 " (mask %s)\n", ceph_ino(&ci->netfs.inode),
874 ceph_cap_string(have),
875 ceph_cap_string(mask));
879 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
880 cap = rb_entry(p, struct ceph_cap, ci_node);
881 if (!__cap_is_valid(cap))
883 if ((cap->issued & mask) == mask) {
884 dout("__ceph_caps_issued_mask ino 0x%llx cap %p issued %s"
885 " (mask %s)\n", ceph_ino(&ci->netfs.inode), cap,
886 ceph_cap_string(cap->issued),
887 ceph_cap_string(mask));
893 /* does a combination of caps satisfy mask? */
895 if ((have & mask) == mask) {
896 dout("__ceph_caps_issued_mask ino 0x%llx combo issued %s"
897 " (mask %s)\n", ceph_ino(&ci->netfs.inode),
898 ceph_cap_string(cap->issued),
899 ceph_cap_string(mask));
903 /* touch this + preceding caps */
905 for (q = rb_first(&ci->i_caps); q != p;
907 cap = rb_entry(q, struct ceph_cap,
909 if (!__cap_is_valid(cap))
911 if (cap->issued & mask)
922 int __ceph_caps_issued_mask_metric(struct ceph_inode_info *ci, int mask,
925 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->netfs.inode.i_sb);
928 r = __ceph_caps_issued_mask(ci, mask, touch);
930 ceph_update_cap_hit(&fsc->mdsc->metric);
932 ceph_update_cap_mis(&fsc->mdsc->metric);
937 * Return true if mask caps are currently being revoked by an MDS.
939 int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
940 struct ceph_cap *ocap, int mask)
942 struct ceph_cap *cap;
945 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
946 cap = rb_entry(p, struct ceph_cap, ci_node);
948 (cap->implemented & ~cap->issued & mask))
954 int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
956 struct inode *inode = &ci->netfs.inode;
959 spin_lock(&ci->i_ceph_lock);
960 ret = __ceph_caps_revoking_other(ci, NULL, mask);
961 spin_unlock(&ci->i_ceph_lock);
962 dout("ceph_caps_revoking %p %s = %d\n", inode,
963 ceph_cap_string(mask), ret);
967 int __ceph_caps_used(struct ceph_inode_info *ci)
971 used |= CEPH_CAP_PIN;
973 used |= CEPH_CAP_FILE_RD;
974 if (ci->i_rdcache_ref ||
975 (S_ISREG(ci->netfs.inode.i_mode) &&
976 ci->netfs.inode.i_data.nrpages))
977 used |= CEPH_CAP_FILE_CACHE;
979 used |= CEPH_CAP_FILE_WR;
980 if (ci->i_wb_ref || ci->i_wrbuffer_ref)
981 used |= CEPH_CAP_FILE_BUFFER;
983 used |= CEPH_CAP_FILE_EXCL;
987 #define FMODE_WAIT_BIAS 1000
990 * wanted, by virtue of open file modes
992 int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
994 const int PIN_SHIFT = ffs(CEPH_FILE_MODE_PIN);
995 const int RD_SHIFT = ffs(CEPH_FILE_MODE_RD);
996 const int WR_SHIFT = ffs(CEPH_FILE_MODE_WR);
997 const int LAZY_SHIFT = ffs(CEPH_FILE_MODE_LAZY);
998 struct ceph_mount_options *opt =
999 ceph_inode_to_client(&ci->netfs.inode)->mount_options;
1000 unsigned long used_cutoff = jiffies - opt->caps_wanted_delay_max * HZ;
1001 unsigned long idle_cutoff = jiffies - opt->caps_wanted_delay_min * HZ;
1003 if (S_ISDIR(ci->netfs.inode.i_mode)) {
1006 /* use used_cutoff here, to keep dir's wanted caps longer */
1007 if (ci->i_nr_by_mode[RD_SHIFT] > 0 ||
1008 time_after(ci->i_last_rd, used_cutoff))
1009 want |= CEPH_CAP_ANY_SHARED;
1011 if (ci->i_nr_by_mode[WR_SHIFT] > 0 ||
1012 time_after(ci->i_last_wr, used_cutoff)) {
1013 want |= CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
1014 if (opt->flags & CEPH_MOUNT_OPT_ASYNC_DIROPS)
1015 want |= CEPH_CAP_ANY_DIR_OPS;
1018 if (want || ci->i_nr_by_mode[PIN_SHIFT] > 0)
1019 want |= CEPH_CAP_PIN;
1025 if (ci->i_nr_by_mode[RD_SHIFT] > 0) {
1026 if (ci->i_nr_by_mode[RD_SHIFT] >= FMODE_WAIT_BIAS ||
1027 time_after(ci->i_last_rd, used_cutoff))
1028 bits |= 1 << RD_SHIFT;
1029 } else if (time_after(ci->i_last_rd, idle_cutoff)) {
1030 bits |= 1 << RD_SHIFT;
1033 if (ci->i_nr_by_mode[WR_SHIFT] > 0) {
1034 if (ci->i_nr_by_mode[WR_SHIFT] >= FMODE_WAIT_BIAS ||
1035 time_after(ci->i_last_wr, used_cutoff))
1036 bits |= 1 << WR_SHIFT;
1037 } else if (time_after(ci->i_last_wr, idle_cutoff)) {
1038 bits |= 1 << WR_SHIFT;
1041 /* check lazyio only when read/write is wanted */
1042 if ((bits & (CEPH_FILE_MODE_RDWR << 1)) &&
1043 ci->i_nr_by_mode[LAZY_SHIFT] > 0)
1044 bits |= 1 << LAZY_SHIFT;
1046 return bits ? ceph_caps_for_mode(bits >> 1) : 0;
1051 * wanted, by virtue of open file modes AND cap refs (buffered/cached data)
1053 int __ceph_caps_wanted(struct ceph_inode_info *ci)
1055 int w = __ceph_caps_file_wanted(ci) | __ceph_caps_used(ci);
1056 if (S_ISDIR(ci->netfs.inode.i_mode)) {
1057 /* we want EXCL if holding caps of dir ops */
1058 if (w & CEPH_CAP_ANY_DIR_OPS)
1059 w |= CEPH_CAP_FILE_EXCL;
1061 /* we want EXCL if dirty data */
1062 if (w & CEPH_CAP_FILE_BUFFER)
1063 w |= CEPH_CAP_FILE_EXCL;
1069 * Return caps we have registered with the MDS(s) as 'wanted'.
1071 int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check)
1073 struct ceph_cap *cap;
1077 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1078 cap = rb_entry(p, struct ceph_cap, ci_node);
1079 if (check && !__cap_is_valid(cap))
1081 if (cap == ci->i_auth_cap)
1082 mds_wanted |= cap->mds_wanted;
1084 mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
1089 int ceph_is_any_caps(struct inode *inode)
1091 struct ceph_inode_info *ci = ceph_inode(inode);
1094 spin_lock(&ci->i_ceph_lock);
1095 ret = __ceph_is_any_real_caps(ci);
1096 spin_unlock(&ci->i_ceph_lock);
1102 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
1104 * caller should hold i_ceph_lock.
1105 * caller will not hold session s_mutex if called from destroy_inode.
1107 void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
1109 struct ceph_mds_session *session = cap->session;
1110 struct ceph_inode_info *ci = cap->ci;
1111 struct ceph_mds_client *mdsc;
1114 /* 'ci' being NULL means the remove have already occurred */
1116 dout("%s: cap inode is NULL\n", __func__);
1120 lockdep_assert_held(&ci->i_ceph_lock);
1122 dout("__ceph_remove_cap %p from %p\n", cap, &ci->netfs.inode);
1124 mdsc = ceph_inode_to_client(&ci->netfs.inode)->mdsc;
1126 /* remove from inode's cap rbtree, and clear auth cap */
1127 rb_erase(&cap->ci_node, &ci->i_caps);
1128 if (ci->i_auth_cap == cap)
1129 ci->i_auth_cap = NULL;
1131 /* remove from session list */
1132 spin_lock(&session->s_cap_lock);
1133 if (session->s_cap_iterator == cap) {
1134 /* not yet, we are iterating over this very cap */
1135 dout("__ceph_remove_cap delaying %p removal from session %p\n",
1138 list_del_init(&cap->session_caps);
1139 session->s_nr_caps--;
1140 atomic64_dec(&mdsc->metric.total_caps);
1141 cap->session = NULL;
1144 /* protect backpointer with s_cap_lock: see iterate_session_caps */
1148 * s_cap_reconnect is protected by s_cap_lock. no one changes
1149 * s_cap_gen while session is in the reconnect state.
1151 if (queue_release &&
1152 (!session->s_cap_reconnect ||
1153 cap->cap_gen == atomic_read(&session->s_cap_gen))) {
1154 cap->queue_release = 1;
1156 __ceph_queue_cap_release(session, cap);
1160 cap->queue_release = 0;
1162 cap->cap_ino = ci->i_vino.ino;
1164 spin_unlock(&session->s_cap_lock);
1167 ceph_put_cap(mdsc, cap);
1169 if (!__ceph_is_any_real_caps(ci)) {
1170 /* when reconnect denied, we remove session caps forcibly,
1171 * i_wr_ref can be non-zero. If there are ongoing write,
1172 * keep i_snap_realm.
1174 if (ci->i_wr_ref == 0 && ci->i_snap_realm)
1175 ceph_change_snap_realm(&ci->netfs.inode, NULL);
1177 __cap_delay_cancel(mdsc, ci);
1181 void ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
1183 struct ceph_inode_info *ci = cap->ci;
1184 struct ceph_fs_client *fsc;
1186 /* 'ci' being NULL means the remove have already occurred */
1188 dout("%s: cap inode is NULL\n", __func__);
1192 lockdep_assert_held(&ci->i_ceph_lock);
1194 fsc = ceph_inode_to_client(&ci->netfs.inode);
1195 WARN_ON_ONCE(ci->i_auth_cap == cap &&
1196 !list_empty(&ci->i_dirty_item) &&
1197 !fsc->blocklisted &&
1198 !ceph_inode_is_shutdown(&ci->netfs.inode));
1200 __ceph_remove_cap(cap, queue_release);
1203 struct cap_msg_args {
1204 struct ceph_mds_session *session;
1205 u64 ino, cid, follows;
1206 u64 flush_tid, oldest_flush_tid, size, max_size;
1209 struct ceph_buffer *xattr_buf;
1210 struct ceph_buffer *old_xattr_buf;
1211 struct timespec64 atime, mtime, ctime, btime;
1212 int op, caps, wanted, dirty;
1213 u32 seq, issue_seq, mseq, time_warp_seq;
1221 u32 fscrypt_auth_len;
1222 u8 fscrypt_auth[sizeof(struct ceph_fscrypt_auth)]; // for context
1225 /* Marshal up the cap msg to the MDS */
1226 static void encode_cap_msg(struct ceph_msg *msg, struct cap_msg_args *arg)
1228 struct ceph_mds_caps *fc;
1230 struct ceph_osd_client *osdc = &arg->session->s_mdsc->fsc->client->osdc;
1232 dout("%s %s %llx %llx caps %s wanted %s dirty %s seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu xattr_ver %llu xattr_len %d\n",
1233 __func__, ceph_cap_op_name(arg->op), arg->cid, arg->ino,
1234 ceph_cap_string(arg->caps), ceph_cap_string(arg->wanted),
1235 ceph_cap_string(arg->dirty), arg->seq, arg->issue_seq,
1236 arg->flush_tid, arg->oldest_flush_tid, arg->mseq, arg->follows,
1237 arg->size, arg->max_size, arg->xattr_version,
1238 arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0);
1240 msg->hdr.version = cpu_to_le16(12);
1241 msg->hdr.tid = cpu_to_le64(arg->flush_tid);
1243 fc = msg->front.iov_base;
1244 memset(fc, 0, sizeof(*fc));
1246 fc->cap_id = cpu_to_le64(arg->cid);
1247 fc->op = cpu_to_le32(arg->op);
1248 fc->seq = cpu_to_le32(arg->seq);
1249 fc->issue_seq = cpu_to_le32(arg->issue_seq);
1250 fc->migrate_seq = cpu_to_le32(arg->mseq);
1251 fc->caps = cpu_to_le32(arg->caps);
1252 fc->wanted = cpu_to_le32(arg->wanted);
1253 fc->dirty = cpu_to_le32(arg->dirty);
1254 fc->ino = cpu_to_le64(arg->ino);
1255 fc->snap_follows = cpu_to_le64(arg->follows);
1257 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
1259 fc->size = cpu_to_le64(round_up(arg->size,
1260 CEPH_FSCRYPT_BLOCK_SIZE));
1263 fc->size = cpu_to_le64(arg->size);
1264 fc->max_size = cpu_to_le64(arg->max_size);
1265 ceph_encode_timespec64(&fc->mtime, &arg->mtime);
1266 ceph_encode_timespec64(&fc->atime, &arg->atime);
1267 ceph_encode_timespec64(&fc->ctime, &arg->ctime);
1268 fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq);
1270 fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid));
1271 fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid));
1272 fc->mode = cpu_to_le32(arg->mode);
1274 fc->xattr_version = cpu_to_le64(arg->xattr_version);
1275 if (arg->xattr_buf) {
1276 msg->middle = ceph_buffer_get(arg->xattr_buf);
1277 fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1278 msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1282 /* flock buffer size (version 2) */
1283 ceph_encode_32(&p, 0);
1284 /* inline version (version 4) */
1285 ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE);
1286 /* inline data size */
1287 ceph_encode_32(&p, 0);
1289 * osd_epoch_barrier (version 5)
1290 * The epoch_barrier is protected osdc->lock, so READ_ONCE here in
1291 * case it was recently changed
1293 ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier));
1294 /* oldest_flush_tid (version 6) */
1295 ceph_encode_64(&p, arg->oldest_flush_tid);
1298 * caller_uid/caller_gid (version 7)
1300 * Currently, we don't properly track which caller dirtied the caps
1301 * last, and force a flush of them when there is a conflict. For now,
1302 * just set this to 0:0, to emulate how the MDS has worked up to now.
1304 ceph_encode_32(&p, 0);
1305 ceph_encode_32(&p, 0);
1307 /* pool namespace (version 8) (mds always ignores this) */
1308 ceph_encode_32(&p, 0);
1310 /* btime and change_attr (version 9) */
1311 ceph_encode_timespec64(p, &arg->btime);
1312 p += sizeof(struct ceph_timespec);
1313 ceph_encode_64(&p, arg->change_attr);
1315 /* Advisory flags (version 10) */
1316 ceph_encode_32(&p, arg->flags);
1318 /* dirstats (version 11) - these are r/o on the client */
1319 ceph_encode_64(&p, 0);
1320 ceph_encode_64(&p, 0);
1322 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
1324 * fscrypt_auth and fscrypt_file (version 12)
1326 * fscrypt_auth holds the crypto context (if any). fscrypt_file
1327 * tracks the real i_size as an __le64 field (and we use a rounded-up
1328 * i_size in the traditional size field).
1330 ceph_encode_32(&p, arg->fscrypt_auth_len);
1331 ceph_encode_copy(&p, arg->fscrypt_auth, arg->fscrypt_auth_len);
1332 ceph_encode_32(&p, sizeof(__le64));
1333 ceph_encode_64(&p, arg->size);
1334 #else /* CONFIG_FS_ENCRYPTION */
1335 ceph_encode_32(&p, 0);
1336 ceph_encode_32(&p, 0);
1337 #endif /* CONFIG_FS_ENCRYPTION */
1341 * Queue cap releases when an inode is dropped from our cache.
1343 void __ceph_remove_caps(struct ceph_inode_info *ci)
1347 /* lock i_ceph_lock, because ceph_d_revalidate(..., LOOKUP_RCU)
1348 * may call __ceph_caps_issued_mask() on a freeing inode. */
1349 spin_lock(&ci->i_ceph_lock);
1350 p = rb_first(&ci->i_caps);
1352 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1354 ceph_remove_cap(cap, true);
1356 spin_unlock(&ci->i_ceph_lock);
1360 * Prepare to send a cap message to an MDS. Update the cap state, and populate
1361 * the arg struct with the parameters that will need to be sent. This should
1362 * be done under the i_ceph_lock to guard against changes to cap state.
1364 * Make note of max_size reported/requested from mds, revoked caps
1365 * that have now been implemented.
1367 static void __prep_cap(struct cap_msg_args *arg, struct ceph_cap *cap,
1368 int op, int flags, int used, int want, int retain,
1369 int flushing, u64 flush_tid, u64 oldest_flush_tid)
1371 struct ceph_inode_info *ci = cap->ci;
1372 struct inode *inode = &ci->netfs.inode;
1375 lockdep_assert_held(&ci->i_ceph_lock);
1377 held = cap->issued | cap->implemented;
1378 revoking = cap->implemented & ~cap->issued;
1379 retain &= ~revoking;
1381 dout("%s %p cap %p session %p %s -> %s (revoking %s)\n",
1382 __func__, inode, cap, cap->session,
1383 ceph_cap_string(held), ceph_cap_string(held & retain),
1384 ceph_cap_string(revoking));
1385 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1387 ci->i_ceph_flags &= ~CEPH_I_FLUSH;
1389 cap->issued &= retain; /* drop bits we don't want */
1391 * Wake up any waiters on wanted -> needed transition. This is due to
1392 * the weird transition from buffered to sync IO... we need to flush
1393 * dirty pages _before_ allowing sync writes to avoid reordering.
1395 arg->wake = cap->implemented & ~cap->issued;
1396 cap->implemented &= cap->issued | used;
1397 cap->mds_wanted = want;
1399 arg->session = cap->session;
1400 arg->ino = ceph_vino(inode).ino;
1401 arg->cid = cap->cap_id;
1402 arg->follows = flushing ? ci->i_head_snapc->seq : 0;
1403 arg->flush_tid = flush_tid;
1404 arg->oldest_flush_tid = oldest_flush_tid;
1405 arg->size = i_size_read(inode);
1406 ci->i_reported_size = arg->size;
1407 arg->max_size = ci->i_wanted_max_size;
1408 if (cap == ci->i_auth_cap) {
1409 if (want & CEPH_CAP_ANY_FILE_WR)
1410 ci->i_requested_max_size = arg->max_size;
1412 ci->i_requested_max_size = 0;
1415 if (flushing & CEPH_CAP_XATTR_EXCL) {
1416 arg->old_xattr_buf = __ceph_build_xattrs_blob(ci);
1417 arg->xattr_version = ci->i_xattrs.version;
1418 arg->xattr_buf = ci->i_xattrs.blob;
1420 arg->xattr_buf = NULL;
1421 arg->old_xattr_buf = NULL;
1424 arg->mtime = inode_get_mtime(inode);
1425 arg->atime = inode_get_atime(inode);
1426 arg->ctime = inode_get_ctime(inode);
1427 arg->btime = ci->i_btime;
1428 arg->change_attr = inode_peek_iversion_raw(inode);
1431 arg->caps = cap->implemented;
1433 arg->dirty = flushing;
1435 arg->seq = cap->seq;
1436 arg->issue_seq = cap->issue_seq;
1437 arg->mseq = cap->mseq;
1438 arg->time_warp_seq = ci->i_time_warp_seq;
1440 arg->uid = inode->i_uid;
1441 arg->gid = inode->i_gid;
1442 arg->mode = inode->i_mode;
1444 arg->inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
1445 if (!(flags & CEPH_CLIENT_CAPS_PENDING_CAPSNAP) &&
1446 !list_empty(&ci->i_cap_snaps)) {
1447 struct ceph_cap_snap *capsnap;
1448 list_for_each_entry_reverse(capsnap, &ci->i_cap_snaps, ci_item) {
1449 if (capsnap->cap_flush.tid)
1451 if (capsnap->need_flush) {
1452 flags |= CEPH_CLIENT_CAPS_PENDING_CAPSNAP;
1458 arg->encrypted = IS_ENCRYPTED(inode);
1459 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
1460 if (ci->fscrypt_auth_len &&
1461 WARN_ON_ONCE(ci->fscrypt_auth_len > sizeof(struct ceph_fscrypt_auth))) {
1462 /* Don't set this if it's too big */
1463 arg->fscrypt_auth_len = 0;
1465 arg->fscrypt_auth_len = ci->fscrypt_auth_len;
1466 memcpy(arg->fscrypt_auth, ci->fscrypt_auth,
1467 min_t(size_t, ci->fscrypt_auth_len,
1468 sizeof(arg->fscrypt_auth)));
1470 #endif /* CONFIG_FS_ENCRYPTION */
1473 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
1474 #define CAP_MSG_FIXED_FIELDS (sizeof(struct ceph_mds_caps) + \
1475 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + 8 + 4 + 4 + 8)
1477 static inline int cap_msg_size(struct cap_msg_args *arg)
1479 return CAP_MSG_FIXED_FIELDS + arg->fscrypt_auth_len;
1482 #define CAP_MSG_FIXED_FIELDS (sizeof(struct ceph_mds_caps) + \
1483 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + 8 + 4 + 4)
1485 static inline int cap_msg_size(struct cap_msg_args *arg)
1487 return CAP_MSG_FIXED_FIELDS;
1489 #endif /* CONFIG_FS_ENCRYPTION */
1492 * Send a cap msg on the given inode.
1494 * Caller should hold snap_rwsem (read), s_mutex.
1496 static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci)
1498 struct ceph_msg *msg;
1499 struct inode *inode = &ci->netfs.inode;
1501 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, cap_msg_size(arg), GFP_NOFS,
1504 pr_err("error allocating cap msg: ino (%llx.%llx) flushing %s tid %llu, requeuing cap.\n",
1505 ceph_vinop(inode), ceph_cap_string(arg->dirty),
1507 spin_lock(&ci->i_ceph_lock);
1508 __cap_delay_requeue(arg->session->s_mdsc, ci);
1509 spin_unlock(&ci->i_ceph_lock);
1513 encode_cap_msg(msg, arg);
1514 ceph_con_send(&arg->session->s_con, msg);
1515 ceph_buffer_put(arg->old_xattr_buf);
1517 wake_up_all(&ci->i_cap_wq);
1520 static inline int __send_flush_snap(struct inode *inode,
1521 struct ceph_mds_session *session,
1522 struct ceph_cap_snap *capsnap,
1523 u32 mseq, u64 oldest_flush_tid)
1525 struct cap_msg_args arg;
1526 struct ceph_msg *msg;
1528 arg.session = session;
1529 arg.ino = ceph_vino(inode).ino;
1531 arg.follows = capsnap->follows;
1532 arg.flush_tid = capsnap->cap_flush.tid;
1533 arg.oldest_flush_tid = oldest_flush_tid;
1535 arg.size = capsnap->size;
1537 arg.xattr_version = capsnap->xattr_version;
1538 arg.xattr_buf = capsnap->xattr_blob;
1539 arg.old_xattr_buf = NULL;
1541 arg.atime = capsnap->atime;
1542 arg.mtime = capsnap->mtime;
1543 arg.ctime = capsnap->ctime;
1544 arg.btime = capsnap->btime;
1545 arg.change_attr = capsnap->change_attr;
1547 arg.op = CEPH_CAP_OP_FLUSHSNAP;
1548 arg.caps = capsnap->issued;
1550 arg.dirty = capsnap->dirty;
1555 arg.time_warp_seq = capsnap->time_warp_seq;
1557 arg.uid = capsnap->uid;
1558 arg.gid = capsnap->gid;
1559 arg.mode = capsnap->mode;
1561 arg.inline_data = capsnap->inline_data;
1564 arg.encrypted = IS_ENCRYPTED(inode);
1566 /* No fscrypt_auth changes from a capsnap.*/
1567 arg.fscrypt_auth_len = 0;
1569 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, cap_msg_size(&arg),
1574 encode_cap_msg(msg, &arg);
1575 ceph_con_send(&arg.session->s_con, msg);
1580 * When a snapshot is taken, clients accumulate dirty metadata on
1581 * inodes with capabilities in ceph_cap_snaps to describe the file
1582 * state at the time the snapshot was taken. This must be flushed
1583 * asynchronously back to the MDS once sync writes complete and dirty
1584 * data is written out.
1586 * Called under i_ceph_lock.
1588 static void __ceph_flush_snaps(struct ceph_inode_info *ci,
1589 struct ceph_mds_session *session)
1590 __releases(ci->i_ceph_lock)
1591 __acquires(ci->i_ceph_lock)
1593 struct inode *inode = &ci->netfs.inode;
1594 struct ceph_mds_client *mdsc = session->s_mdsc;
1595 struct ceph_cap_snap *capsnap;
1596 u64 oldest_flush_tid = 0;
1597 u64 first_tid = 1, last_tid = 0;
1599 dout("__flush_snaps %p session %p\n", inode, session);
1601 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1603 * we need to wait for sync writes to complete and for dirty
1604 * pages to be written out.
1606 if (capsnap->dirty_pages || capsnap->writing)
1609 /* should be removed by ceph_try_drop_cap_snap() */
1610 BUG_ON(!capsnap->need_flush);
1612 /* only flush each capsnap once */
1613 if (capsnap->cap_flush.tid > 0) {
1614 dout(" already flushed %p, skipping\n", capsnap);
1618 spin_lock(&mdsc->cap_dirty_lock);
1619 capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
1620 list_add_tail(&capsnap->cap_flush.g_list,
1621 &mdsc->cap_flush_list);
1622 if (oldest_flush_tid == 0)
1623 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1624 if (list_empty(&ci->i_flushing_item)) {
1625 list_add_tail(&ci->i_flushing_item,
1626 &session->s_cap_flushing);
1628 spin_unlock(&mdsc->cap_dirty_lock);
1630 list_add_tail(&capsnap->cap_flush.i_list,
1631 &ci->i_cap_flush_list);
1634 first_tid = capsnap->cap_flush.tid;
1635 last_tid = capsnap->cap_flush.tid;
1638 ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS;
1640 while (first_tid <= last_tid) {
1641 struct ceph_cap *cap = ci->i_auth_cap;
1642 struct ceph_cap_flush *cf = NULL, *iter;
1645 if (!(cap && cap->session == session)) {
1646 dout("__flush_snaps %p auth cap %p not mds%d, "
1647 "stop\n", inode, cap, session->s_mds);
1652 list_for_each_entry(iter, &ci->i_cap_flush_list, i_list) {
1653 if (iter->tid >= first_tid) {
1662 first_tid = cf->tid + 1;
1664 capsnap = container_of(cf, struct ceph_cap_snap, cap_flush);
1665 refcount_inc(&capsnap->nref);
1666 spin_unlock(&ci->i_ceph_lock);
1668 dout("__flush_snaps %p capsnap %p tid %llu %s\n",
1669 inode, capsnap, cf->tid, ceph_cap_string(capsnap->dirty));
1671 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
1674 pr_err("__flush_snaps: error sending cap flushsnap, "
1675 "ino (%llx.%llx) tid %llu follows %llu\n",
1676 ceph_vinop(inode), cf->tid, capsnap->follows);
1679 ceph_put_cap_snap(capsnap);
1680 spin_lock(&ci->i_ceph_lock);
1684 void ceph_flush_snaps(struct ceph_inode_info *ci,
1685 struct ceph_mds_session **psession)
1687 struct inode *inode = &ci->netfs.inode;
1688 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
1689 struct ceph_mds_session *session = NULL;
1690 bool need_put = false;
1693 dout("ceph_flush_snaps %p\n", inode);
1695 session = *psession;
1697 spin_lock(&ci->i_ceph_lock);
1698 if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) {
1699 dout(" no capsnap needs flush, doing nothing\n");
1702 if (!ci->i_auth_cap) {
1703 dout(" no auth cap (migrating?), doing nothing\n");
1707 mds = ci->i_auth_cap->session->s_mds;
1708 if (session && session->s_mds != mds) {
1709 dout(" oops, wrong session %p mutex\n", session);
1710 ceph_put_mds_session(session);
1714 spin_unlock(&ci->i_ceph_lock);
1715 mutex_lock(&mdsc->mutex);
1716 session = __ceph_lookup_mds_session(mdsc, mds);
1717 mutex_unlock(&mdsc->mutex);
1721 // make sure flushsnap messages are sent in proper order.
1722 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
1723 __kick_flushing_caps(mdsc, session, ci, 0);
1725 __ceph_flush_snaps(ci, session);
1727 spin_unlock(&ci->i_ceph_lock);
1730 *psession = session;
1732 ceph_put_mds_session(session);
1733 /* we flushed them all; remove this inode from the queue */
1734 spin_lock(&mdsc->snap_flush_lock);
1735 if (!list_empty(&ci->i_snap_flush_item))
1737 list_del_init(&ci->i_snap_flush_item);
1738 spin_unlock(&mdsc->snap_flush_lock);
1745 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1746 * Caller is then responsible for calling __mark_inode_dirty with the
1747 * returned flags value.
1749 int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
1750 struct ceph_cap_flush **pcf)
1752 struct ceph_mds_client *mdsc =
1753 ceph_sb_to_client(ci->netfs.inode.i_sb)->mdsc;
1754 struct inode *inode = &ci->netfs.inode;
1755 int was = ci->i_dirty_caps;
1758 lockdep_assert_held(&ci->i_ceph_lock);
1760 if (!ci->i_auth_cap) {
1761 pr_warn("__mark_dirty_caps %p %llx mask %s, "
1762 "but no auth cap (session was closed?)\n",
1763 inode, ceph_ino(inode), ceph_cap_string(mask));
1767 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->netfs.inode,
1768 ceph_cap_string(mask), ceph_cap_string(was),
1769 ceph_cap_string(was | mask));
1770 ci->i_dirty_caps |= mask;
1772 struct ceph_mds_session *session = ci->i_auth_cap->session;
1774 WARN_ON_ONCE(ci->i_prealloc_cap_flush);
1775 swap(ci->i_prealloc_cap_flush, *pcf);
1777 if (!ci->i_head_snapc) {
1778 WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
1779 ci->i_head_snapc = ceph_get_snap_context(
1780 ci->i_snap_realm->cached_context);
1782 dout(" inode %p now dirty snapc %p auth cap %p\n",
1783 &ci->netfs.inode, ci->i_head_snapc, ci->i_auth_cap);
1784 BUG_ON(!list_empty(&ci->i_dirty_item));
1785 spin_lock(&mdsc->cap_dirty_lock);
1786 list_add(&ci->i_dirty_item, &session->s_cap_dirty);
1787 spin_unlock(&mdsc->cap_dirty_lock);
1788 if (ci->i_flushing_caps == 0) {
1790 dirty |= I_DIRTY_SYNC;
1793 WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
1795 BUG_ON(list_empty(&ci->i_dirty_item));
1796 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1797 (mask & CEPH_CAP_FILE_BUFFER))
1798 dirty |= I_DIRTY_DATASYNC;
1799 __cap_delay_requeue(mdsc, ci);
1803 struct ceph_cap_flush *ceph_alloc_cap_flush(void)
1805 struct ceph_cap_flush *cf;
1807 cf = kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
1811 cf->is_capsnap = false;
1815 void ceph_free_cap_flush(struct ceph_cap_flush *cf)
1818 kmem_cache_free(ceph_cap_flush_cachep, cf);
1821 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
1823 if (!list_empty(&mdsc->cap_flush_list)) {
1824 struct ceph_cap_flush *cf =
1825 list_first_entry(&mdsc->cap_flush_list,
1826 struct ceph_cap_flush, g_list);
1833 * Remove cap_flush from the mdsc's or inode's flushing cap list.
1834 * Return true if caller needs to wake up flush waiters.
1836 static bool __detach_cap_flush_from_mdsc(struct ceph_mds_client *mdsc,
1837 struct ceph_cap_flush *cf)
1839 struct ceph_cap_flush *prev;
1840 bool wake = cf->wake;
1842 if (wake && cf->g_list.prev != &mdsc->cap_flush_list) {
1843 prev = list_prev_entry(cf, g_list);
1847 list_del_init(&cf->g_list);
1851 static bool __detach_cap_flush_from_ci(struct ceph_inode_info *ci,
1852 struct ceph_cap_flush *cf)
1854 struct ceph_cap_flush *prev;
1855 bool wake = cf->wake;
1857 if (wake && cf->i_list.prev != &ci->i_cap_flush_list) {
1858 prev = list_prev_entry(cf, i_list);
1862 list_del_init(&cf->i_list);
1867 * Add dirty inode to the flushing list. Assigned a seq number so we
1868 * can wait for caps to flush without starving.
1870 * Called under i_ceph_lock. Returns the flush tid.
1872 static u64 __mark_caps_flushing(struct inode *inode,
1873 struct ceph_mds_session *session, bool wake,
1874 u64 *oldest_flush_tid)
1876 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1877 struct ceph_inode_info *ci = ceph_inode(inode);
1878 struct ceph_cap_flush *cf = NULL;
1881 lockdep_assert_held(&ci->i_ceph_lock);
1882 BUG_ON(ci->i_dirty_caps == 0);
1883 BUG_ON(list_empty(&ci->i_dirty_item));
1884 BUG_ON(!ci->i_prealloc_cap_flush);
1886 flushing = ci->i_dirty_caps;
1887 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1888 ceph_cap_string(flushing),
1889 ceph_cap_string(ci->i_flushing_caps),
1890 ceph_cap_string(ci->i_flushing_caps | flushing));
1891 ci->i_flushing_caps |= flushing;
1892 ci->i_dirty_caps = 0;
1893 dout(" inode %p now !dirty\n", inode);
1895 swap(cf, ci->i_prealloc_cap_flush);
1896 cf->caps = flushing;
1899 spin_lock(&mdsc->cap_dirty_lock);
1900 list_del_init(&ci->i_dirty_item);
1902 cf->tid = ++mdsc->last_cap_flush_tid;
1903 list_add_tail(&cf->g_list, &mdsc->cap_flush_list);
1904 *oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1906 if (list_empty(&ci->i_flushing_item)) {
1907 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1908 mdsc->num_cap_flushing++;
1910 spin_unlock(&mdsc->cap_dirty_lock);
1912 list_add_tail(&cf->i_list, &ci->i_cap_flush_list);
1918 * try to invalidate mapping pages without blocking.
1920 static int try_nonblocking_invalidate(struct inode *inode)
1921 __releases(ci->i_ceph_lock)
1922 __acquires(ci->i_ceph_lock)
1924 struct ceph_inode_info *ci = ceph_inode(inode);
1925 u32 invalidating_gen = ci->i_rdcache_gen;
1927 spin_unlock(&ci->i_ceph_lock);
1928 ceph_fscache_invalidate(inode, false);
1929 invalidate_mapping_pages(&inode->i_data, 0, -1);
1930 spin_lock(&ci->i_ceph_lock);
1932 if (inode->i_data.nrpages == 0 &&
1933 invalidating_gen == ci->i_rdcache_gen) {
1935 dout("try_nonblocking_invalidate %p success\n", inode);
1936 /* save any racing async invalidate some trouble */
1937 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
1940 dout("try_nonblocking_invalidate %p failed\n", inode);
1944 bool __ceph_should_report_size(struct ceph_inode_info *ci)
1946 loff_t size = i_size_read(&ci->netfs.inode);
1947 /* mds will adjust max size according to the reported size */
1948 if (ci->i_flushing_caps & CEPH_CAP_FILE_WR)
1950 if (size >= ci->i_max_size)
1952 /* half of previous max_size increment has been used */
1953 if (ci->i_max_size > ci->i_reported_size &&
1954 (size << 1) >= ci->i_max_size + ci->i_reported_size)
1960 * Swiss army knife function to examine currently used and wanted
1961 * versus held caps. Release, flush, ack revoked caps to mds as
1964 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1965 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1968 void ceph_check_caps(struct ceph_inode_info *ci, int flags)
1970 struct inode *inode = &ci->netfs.inode;
1971 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
1972 struct ceph_cap *cap;
1973 u64 flush_tid, oldest_flush_tid;
1974 int file_wanted, used, cap_used;
1975 int issued, implemented, want, retain, revoking, flushing = 0;
1976 int mds = -1; /* keep track of how far we've gone through i_caps list
1977 to avoid an infinite loop on retry */
1979 bool queue_invalidate = false;
1980 bool tried_invalidate = false;
1981 bool queue_writeback = false;
1982 struct ceph_mds_session *session = NULL;
1984 spin_lock(&ci->i_ceph_lock);
1985 if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE) {
1986 ci->i_ceph_flags |= CEPH_I_ASYNC_CHECK_CAPS;
1988 /* Don't send messages until we get async create reply */
1989 spin_unlock(&ci->i_ceph_lock);
1993 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1994 flags |= CHECK_CAPS_FLUSH;
1996 /* Caps wanted by virtue of active open files. */
1997 file_wanted = __ceph_caps_file_wanted(ci);
1999 /* Caps which have active references against them */
2000 used = __ceph_caps_used(ci);
2003 * "issued" represents the current caps that the MDS wants us to have.
2004 * "implemented" is the set that we have been granted, and includes the
2005 * ones that have not yet been returned to the MDS (the "revoking" set,
2006 * usually because they have outstanding references).
2008 issued = __ceph_caps_issued(ci, &implemented);
2009 revoking = implemented & ~issued;
2013 /* The ones we currently want to retain (may be adjusted below) */
2014 retain = file_wanted | used | CEPH_CAP_PIN;
2015 if (!mdsc->stopping && inode->i_nlink > 0) {
2017 retain |= CEPH_CAP_ANY; /* be greedy */
2018 } else if (S_ISDIR(inode->i_mode) &&
2019 (issued & CEPH_CAP_FILE_SHARED) &&
2020 __ceph_dir_is_complete(ci)) {
2022 * If a directory is complete, we want to keep
2023 * the exclusive cap. So that MDS does not end up
2024 * revoking the shared cap on every create/unlink
2027 if (IS_RDONLY(inode)) {
2028 want = CEPH_CAP_ANY_SHARED;
2030 want |= CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
2035 retain |= CEPH_CAP_ANY_SHARED;
2037 * keep RD only if we didn't have the file open RW,
2038 * because then the mds would revoke it anyway to
2039 * journal max_size=0.
2041 if (ci->i_max_size == 0)
2042 retain |= CEPH_CAP_ANY_RD;
2046 dout("check_caps %llx.%llx file_want %s used %s dirty %s flushing %s"
2047 " issued %s revoking %s retain %s %s%s%s\n", ceph_vinop(inode),
2048 ceph_cap_string(file_wanted),
2049 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
2050 ceph_cap_string(ci->i_flushing_caps),
2051 ceph_cap_string(issued), ceph_cap_string(revoking),
2052 ceph_cap_string(retain),
2053 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
2054 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "",
2055 (flags & CHECK_CAPS_NOINVAL) ? " NOINVAL" : "");
2058 * If we no longer need to hold onto old our caps, and we may
2059 * have cached pages, but don't want them, then try to invalidate.
2060 * If we fail, it's because pages are locked.... try again later.
2062 if ((!(flags & CHECK_CAPS_NOINVAL) || mdsc->stopping) &&
2063 S_ISREG(inode->i_mode) &&
2064 !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */
2065 inode->i_data.nrpages && /* have cached pages */
2066 (revoking & (CEPH_CAP_FILE_CACHE|
2067 CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */
2068 !tried_invalidate) {
2069 dout("check_caps trying to invalidate on %llx.%llx\n",
2071 if (try_nonblocking_invalidate(inode) < 0) {
2072 dout("check_caps queuing invalidate\n");
2073 queue_invalidate = true;
2074 ci->i_rdcache_revoking = ci->i_rdcache_gen;
2076 tried_invalidate = true;
2080 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
2082 struct cap_msg_args arg;
2084 cap = rb_entry(p, struct ceph_cap, ci_node);
2086 /* avoid looping forever */
2087 if (mds >= cap->mds ||
2088 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
2092 * If we have an auth cap, we don't need to consider any
2093 * overlapping caps as used.
2096 if (ci->i_auth_cap && cap != ci->i_auth_cap)
2097 cap_used &= ~ci->i_auth_cap->issued;
2099 revoking = cap->implemented & ~cap->issued;
2100 dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
2101 cap->mds, cap, ceph_cap_string(cap_used),
2102 ceph_cap_string(cap->issued),
2103 ceph_cap_string(cap->implemented),
2104 ceph_cap_string(revoking));
2106 if (cap == ci->i_auth_cap &&
2107 (cap->issued & CEPH_CAP_FILE_WR)) {
2108 /* request larger max_size from MDS? */
2109 if (ci->i_wanted_max_size > ci->i_max_size &&
2110 ci->i_wanted_max_size > ci->i_requested_max_size) {
2111 dout("requesting new max_size\n");
2115 /* approaching file_max? */
2116 if (__ceph_should_report_size(ci)) {
2117 dout("i_size approaching max_size\n");
2121 /* flush anything dirty? */
2122 if (cap == ci->i_auth_cap) {
2123 if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) {
2124 dout("flushing dirty caps\n");
2127 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) {
2128 dout("flushing snap caps\n");
2133 /* completed revocation? going down and there are no caps? */
2135 if ((revoking & cap_used) == 0) {
2136 dout("completed revocation of %s\n",
2137 ceph_cap_string(cap->implemented & ~cap->issued));
2142 * If the "i_wrbuffer_ref" was increased by mmap or generic
2143 * cache write just before the ceph_check_caps() is called,
2144 * the Fb capability revoking will fail this time. Then we
2145 * must wait for the BDI's delayed work to flush the dirty
2146 * pages and to release the "i_wrbuffer_ref", which will cost
2147 * at most 5 seconds. That means the MDS needs to wait at
2148 * most 5 seconds to finished the Fb capability's revocation.
2150 * Let's queue a writeback for it.
2152 if (S_ISREG(inode->i_mode) && ci->i_wrbuffer_ref &&
2153 (revoking & CEPH_CAP_FILE_BUFFER))
2154 queue_writeback = true;
2157 /* want more caps from mds? */
2158 if (want & ~cap->mds_wanted) {
2159 if (want & ~(cap->mds_wanted | cap->issued))
2161 if (!__cap_is_valid(cap))
2165 /* things we might delay */
2166 if ((cap->issued & ~retain) == 0)
2167 continue; /* nope, all good */
2170 ceph_put_mds_session(session);
2171 session = ceph_get_mds_session(cap->session);
2173 /* kick flushing and flush snaps before sending normal
2175 if (cap == ci->i_auth_cap &&
2177 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) {
2178 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
2179 __kick_flushing_caps(mdsc, session, ci, 0);
2180 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
2181 __ceph_flush_snaps(ci, session);
2186 if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
2187 flushing = ci->i_dirty_caps;
2188 flush_tid = __mark_caps_flushing(inode, session, false,
2190 if (flags & CHECK_CAPS_FLUSH &&
2191 list_empty(&session->s_cap_dirty))
2192 mflags |= CEPH_CLIENT_CAPS_SYNC;
2196 spin_lock(&mdsc->cap_dirty_lock);
2197 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2198 spin_unlock(&mdsc->cap_dirty_lock);
2201 mds = cap->mds; /* remember mds, so we don't repeat */
2203 __prep_cap(&arg, cap, CEPH_CAP_OP_UPDATE, mflags, cap_used,
2204 want, retain, flushing, flush_tid, oldest_flush_tid);
2206 spin_unlock(&ci->i_ceph_lock);
2207 __send_cap(&arg, ci);
2208 spin_lock(&ci->i_ceph_lock);
2210 goto retry; /* retake i_ceph_lock and restart our cap scan. */
2213 /* periodically re-calculate caps wanted by open files */
2214 if (__ceph_is_any_real_caps(ci) &&
2215 list_empty(&ci->i_cap_delay_list) &&
2216 (file_wanted & ~CEPH_CAP_PIN) &&
2217 !(used & (CEPH_CAP_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
2218 __cap_delay_requeue(mdsc, ci);
2221 spin_unlock(&ci->i_ceph_lock);
2223 ceph_put_mds_session(session);
2224 if (queue_writeback)
2225 ceph_queue_writeback(inode);
2226 if (queue_invalidate)
2227 ceph_queue_invalidate(inode);
2231 * Try to flush dirty caps back to the auth mds.
2233 static int try_flush_caps(struct inode *inode, u64 *ptid)
2235 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2236 struct ceph_inode_info *ci = ceph_inode(inode);
2238 u64 flush_tid = 0, oldest_flush_tid = 0;
2240 spin_lock(&ci->i_ceph_lock);
2242 if (ci->i_dirty_caps && ci->i_auth_cap) {
2243 struct ceph_cap *cap = ci->i_auth_cap;
2244 struct cap_msg_args arg;
2245 struct ceph_mds_session *session = cap->session;
2247 if (session->s_state < CEPH_MDS_SESSION_OPEN) {
2248 spin_unlock(&ci->i_ceph_lock);
2252 if (ci->i_ceph_flags &
2253 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS)) {
2254 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
2255 __kick_flushing_caps(mdsc, session, ci, 0);
2256 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
2257 __ceph_flush_snaps(ci, session);
2261 flushing = ci->i_dirty_caps;
2262 flush_tid = __mark_caps_flushing(inode, session, true,
2265 __prep_cap(&arg, cap, CEPH_CAP_OP_FLUSH, CEPH_CLIENT_CAPS_SYNC,
2266 __ceph_caps_used(ci), __ceph_caps_wanted(ci),
2267 (cap->issued | cap->implemented),
2268 flushing, flush_tid, oldest_flush_tid);
2269 spin_unlock(&ci->i_ceph_lock);
2271 __send_cap(&arg, ci);
2273 if (!list_empty(&ci->i_cap_flush_list)) {
2274 struct ceph_cap_flush *cf =
2275 list_last_entry(&ci->i_cap_flush_list,
2276 struct ceph_cap_flush, i_list);
2278 flush_tid = cf->tid;
2280 flushing = ci->i_flushing_caps;
2281 spin_unlock(&ci->i_ceph_lock);
2289 * Return true if we've flushed caps through the given flush_tid.
2291 static int caps_are_flushed(struct inode *inode, u64 flush_tid)
2293 struct ceph_inode_info *ci = ceph_inode(inode);
2296 spin_lock(&ci->i_ceph_lock);
2297 if (!list_empty(&ci->i_cap_flush_list)) {
2298 struct ceph_cap_flush * cf =
2299 list_first_entry(&ci->i_cap_flush_list,
2300 struct ceph_cap_flush, i_list);
2301 if (cf->tid <= flush_tid)
2304 spin_unlock(&ci->i_ceph_lock);
2309 * flush the mdlog and wait for any unsafe requests to complete.
2311 static int flush_mdlog_and_wait_inode_unsafe_requests(struct inode *inode)
2313 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2314 struct ceph_inode_info *ci = ceph_inode(inode);
2315 struct ceph_mds_request *req1 = NULL, *req2 = NULL;
2318 spin_lock(&ci->i_unsafe_lock);
2319 if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
2320 req1 = list_last_entry(&ci->i_unsafe_dirops,
2321 struct ceph_mds_request,
2323 ceph_mdsc_get_request(req1);
2325 if (!list_empty(&ci->i_unsafe_iops)) {
2326 req2 = list_last_entry(&ci->i_unsafe_iops,
2327 struct ceph_mds_request,
2328 r_unsafe_target_item);
2329 ceph_mdsc_get_request(req2);
2331 spin_unlock(&ci->i_unsafe_lock);
2334 * Trigger to flush the journal logs in all the relevant MDSes
2335 * manually, or in the worst case we must wait at most 5 seconds
2336 * to wait the journal logs to be flushed by the MDSes periodically.
2339 struct ceph_mds_request *req;
2340 struct ceph_mds_session **sessions;
2341 struct ceph_mds_session *s;
2342 unsigned int max_sessions;
2345 mutex_lock(&mdsc->mutex);
2346 max_sessions = mdsc->max_sessions;
2348 sessions = kcalloc(max_sessions, sizeof(s), GFP_KERNEL);
2350 mutex_unlock(&mdsc->mutex);
2355 spin_lock(&ci->i_unsafe_lock);
2357 list_for_each_entry(req, &ci->i_unsafe_dirops,
2358 r_unsafe_dir_item) {
2362 if (!sessions[s->s_mds]) {
2363 s = ceph_get_mds_session(s);
2364 sessions[s->s_mds] = s;
2369 list_for_each_entry(req, &ci->i_unsafe_iops,
2370 r_unsafe_target_item) {
2374 if (!sessions[s->s_mds]) {
2375 s = ceph_get_mds_session(s);
2376 sessions[s->s_mds] = s;
2380 spin_unlock(&ci->i_unsafe_lock);
2383 spin_lock(&ci->i_ceph_lock);
2384 if (ci->i_auth_cap) {
2385 s = ci->i_auth_cap->session;
2386 if (!sessions[s->s_mds])
2387 sessions[s->s_mds] = ceph_get_mds_session(s);
2389 spin_unlock(&ci->i_ceph_lock);
2390 mutex_unlock(&mdsc->mutex);
2392 /* send flush mdlog request to MDSes */
2393 for (i = 0; i < max_sessions; i++) {
2396 send_flush_mdlog(s);
2397 ceph_put_mds_session(s);
2403 dout("%s %p wait on tid %llu %llu\n", __func__,
2404 inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL);
2406 ret = !wait_for_completion_timeout(&req1->r_safe_completion,
2407 ceph_timeout_jiffies(req1->r_timeout));
2412 ret = !wait_for_completion_timeout(&req2->r_safe_completion,
2413 ceph_timeout_jiffies(req2->r_timeout));
2420 ceph_mdsc_put_request(req1);
2422 ceph_mdsc_put_request(req2);
2426 int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2428 struct inode *inode = file->f_mapping->host;
2429 struct ceph_inode_info *ci = ceph_inode(inode);
2434 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
2436 ret = file_write_and_wait_range(file, start, end);
2440 ret = ceph_wait_on_async_create(inode);
2444 dirty = try_flush_caps(inode, &flush_tid);
2445 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
2447 err = flush_mdlog_and_wait_inode_unsafe_requests(inode);
2450 * only wait on non-file metadata writeback (the mds
2451 * can recover size and mtime, so we don't need to
2454 if (!err && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
2455 err = wait_event_interruptible(ci->i_cap_wq,
2456 caps_are_flushed(inode, flush_tid));
2462 err = file_check_and_advance_wb_err(file);
2466 dout("fsync %p%s result=%d\n", inode, datasync ? " datasync" : "", ret);
2471 * Flush any dirty caps back to the mds. If we aren't asked to wait,
2472 * queue inode for flush but don't do so immediately, because we can
2473 * get by with fewer MDS messages if we wait for data writeback to
2476 int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
2478 struct ceph_inode_info *ci = ceph_inode(inode);
2482 int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync);
2484 dout("write_inode %p wait=%d\n", inode, wait);
2485 ceph_fscache_unpin_writeback(inode, wbc);
2487 err = ceph_wait_on_async_create(inode);
2490 dirty = try_flush_caps(inode, &flush_tid);
2492 err = wait_event_interruptible(ci->i_cap_wq,
2493 caps_are_flushed(inode, flush_tid));
2495 struct ceph_mds_client *mdsc =
2496 ceph_sb_to_client(inode->i_sb)->mdsc;
2498 spin_lock(&ci->i_ceph_lock);
2499 if (__ceph_caps_dirty(ci))
2500 __cap_delay_requeue_front(mdsc, ci);
2501 spin_unlock(&ci->i_ceph_lock);
2506 static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
2507 struct ceph_mds_session *session,
2508 struct ceph_inode_info *ci,
2509 u64 oldest_flush_tid)
2510 __releases(ci->i_ceph_lock)
2511 __acquires(ci->i_ceph_lock)
2513 struct inode *inode = &ci->netfs.inode;
2514 struct ceph_cap *cap;
2515 struct ceph_cap_flush *cf;
2518 u64 last_snap_flush = 0;
2520 /* Don't do anything until create reply comes in */
2521 if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE)
2524 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2526 list_for_each_entry_reverse(cf, &ci->i_cap_flush_list, i_list) {
2527 if (cf->is_capsnap) {
2528 last_snap_flush = cf->tid;
2533 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
2534 if (cf->tid < first_tid)
2537 cap = ci->i_auth_cap;
2538 if (!(cap && cap->session == session)) {
2539 pr_err("%p auth cap %p not mds%d ???\n",
2540 inode, cap, session->s_mds);
2544 first_tid = cf->tid + 1;
2546 if (!cf->is_capsnap) {
2547 struct cap_msg_args arg;
2549 dout("kick_flushing_caps %p cap %p tid %llu %s\n",
2550 inode, cap, cf->tid, ceph_cap_string(cf->caps));
2551 __prep_cap(&arg, cap, CEPH_CAP_OP_FLUSH,
2552 (cf->tid < last_snap_flush ?
2553 CEPH_CLIENT_CAPS_PENDING_CAPSNAP : 0),
2554 __ceph_caps_used(ci),
2555 __ceph_caps_wanted(ci),
2556 (cap->issued | cap->implemented),
2557 cf->caps, cf->tid, oldest_flush_tid);
2558 spin_unlock(&ci->i_ceph_lock);
2559 __send_cap(&arg, ci);
2561 struct ceph_cap_snap *capsnap =
2562 container_of(cf, struct ceph_cap_snap,
2564 dout("kick_flushing_caps %p capsnap %p tid %llu %s\n",
2565 inode, capsnap, cf->tid,
2566 ceph_cap_string(capsnap->dirty));
2568 refcount_inc(&capsnap->nref);
2569 spin_unlock(&ci->i_ceph_lock);
2571 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
2574 pr_err("kick_flushing_caps: error sending "
2575 "cap flushsnap, ino (%llx.%llx) "
2576 "tid %llu follows %llu\n",
2577 ceph_vinop(inode), cf->tid,
2581 ceph_put_cap_snap(capsnap);
2584 spin_lock(&ci->i_ceph_lock);
2588 void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
2589 struct ceph_mds_session *session)
2591 struct ceph_inode_info *ci;
2592 struct ceph_cap *cap;
2593 u64 oldest_flush_tid;
2595 dout("early_kick_flushing_caps mds%d\n", session->s_mds);
2597 spin_lock(&mdsc->cap_dirty_lock);
2598 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2599 spin_unlock(&mdsc->cap_dirty_lock);
2601 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2602 spin_lock(&ci->i_ceph_lock);
2603 cap = ci->i_auth_cap;
2604 if (!(cap && cap->session == session)) {
2605 pr_err("%p auth cap %p not mds%d ???\n",
2606 &ci->netfs.inode, cap, session->s_mds);
2607 spin_unlock(&ci->i_ceph_lock);
2613 * if flushing caps were revoked, we re-send the cap flush
2614 * in client reconnect stage. This guarantees MDS * processes
2615 * the cap flush message before issuing the flushing caps to
2618 if ((cap->issued & ci->i_flushing_caps) !=
2619 ci->i_flushing_caps) {
2620 /* encode_caps_cb() also will reset these sequence
2621 * numbers. make sure sequence numbers in cap flush
2622 * message match later reconnect message */
2626 __kick_flushing_caps(mdsc, session, ci,
2629 ci->i_ceph_flags |= CEPH_I_KICK_FLUSH;
2632 spin_unlock(&ci->i_ceph_lock);
2636 void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
2637 struct ceph_mds_session *session)
2639 struct ceph_inode_info *ci;
2640 struct ceph_cap *cap;
2641 u64 oldest_flush_tid;
2643 lockdep_assert_held(&session->s_mutex);
2645 dout("kick_flushing_caps mds%d\n", session->s_mds);
2647 spin_lock(&mdsc->cap_dirty_lock);
2648 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2649 spin_unlock(&mdsc->cap_dirty_lock);
2651 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2652 spin_lock(&ci->i_ceph_lock);
2653 cap = ci->i_auth_cap;
2654 if (!(cap && cap->session == session)) {
2655 pr_err("%p auth cap %p not mds%d ???\n",
2656 &ci->netfs.inode, cap, session->s_mds);
2657 spin_unlock(&ci->i_ceph_lock);
2660 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
2661 __kick_flushing_caps(mdsc, session, ci,
2664 spin_unlock(&ci->i_ceph_lock);
2668 void ceph_kick_flushing_inode_caps(struct ceph_mds_session *session,
2669 struct ceph_inode_info *ci)
2671 struct ceph_mds_client *mdsc = session->s_mdsc;
2672 struct ceph_cap *cap = ci->i_auth_cap;
2674 lockdep_assert_held(&ci->i_ceph_lock);
2676 dout("%s %p flushing %s\n", __func__, &ci->netfs.inode,
2677 ceph_cap_string(ci->i_flushing_caps));
2679 if (!list_empty(&ci->i_cap_flush_list)) {
2680 u64 oldest_flush_tid;
2681 spin_lock(&mdsc->cap_dirty_lock);
2682 list_move_tail(&ci->i_flushing_item,
2683 &cap->session->s_cap_flushing);
2684 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2685 spin_unlock(&mdsc->cap_dirty_lock);
2687 __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid);
2693 * Take references to capabilities we hold, so that we don't release
2694 * them to the MDS prematurely.
2696 void ceph_take_cap_refs(struct ceph_inode_info *ci, int got,
2697 bool snap_rwsem_locked)
2699 lockdep_assert_held(&ci->i_ceph_lock);
2701 if (got & CEPH_CAP_PIN)
2703 if (got & CEPH_CAP_FILE_RD)
2705 if (got & CEPH_CAP_FILE_CACHE)
2706 ci->i_rdcache_ref++;
2707 if (got & CEPH_CAP_FILE_EXCL)
2709 if (got & CEPH_CAP_FILE_WR) {
2710 if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
2711 BUG_ON(!snap_rwsem_locked);
2712 ci->i_head_snapc = ceph_get_snap_context(
2713 ci->i_snap_realm->cached_context);
2717 if (got & CEPH_CAP_FILE_BUFFER) {
2718 if (ci->i_wb_ref == 0)
2719 ihold(&ci->netfs.inode);
2721 dout("%s %p wb %d -> %d (?)\n", __func__,
2722 &ci->netfs.inode, ci->i_wb_ref-1, ci->i_wb_ref);
2727 * Try to grab cap references. Specify those refs we @want, and the
2728 * minimal set we @need. Also include the larger offset we are writing
2729 * to (when applicable), and check against max_size here as well.
2730 * Note that caller is responsible for ensuring max_size increases are
2731 * requested from the MDS.
2733 * Returns 0 if caps were not able to be acquired (yet), 1 if succeed,
2734 * or a negative error code. There are 3 speical error codes:
2735 * -EAGAIN: need to sleep but non-blocking is specified
2736 * -EFBIG: ask caller to call check_max_size() and try again.
2737 * -EUCLEAN: ask caller to call ceph_renew_caps() and try again.
2740 /* first 8 bits are reserved for CEPH_FILE_MODE_FOO */
2741 NON_BLOCKING = (1 << 8),
2742 CHECK_FILELOCK = (1 << 9),
2745 static int try_get_cap_refs(struct inode *inode, int need, int want,
2746 loff_t endoff, int flags, int *got)
2748 struct ceph_inode_info *ci = ceph_inode(inode);
2749 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
2751 int have, implemented;
2752 bool snap_rwsem_locked = false;
2754 dout("get_cap_refs %p need %s want %s\n", inode,
2755 ceph_cap_string(need), ceph_cap_string(want));
2758 spin_lock(&ci->i_ceph_lock);
2760 if ((flags & CHECK_FILELOCK) &&
2761 (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK)) {
2762 dout("try_get_cap_refs %p error filelock\n", inode);
2767 /* finish pending truncate */
2768 while (ci->i_truncate_pending) {
2769 spin_unlock(&ci->i_ceph_lock);
2770 if (snap_rwsem_locked) {
2771 up_read(&mdsc->snap_rwsem);
2772 snap_rwsem_locked = false;
2774 __ceph_do_pending_vmtruncate(inode);
2775 spin_lock(&ci->i_ceph_lock);
2778 have = __ceph_caps_issued(ci, &implemented);
2780 if (have & need & CEPH_CAP_FILE_WR) {
2781 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2782 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2783 inode, endoff, ci->i_max_size);
2784 if (endoff > ci->i_requested_max_size)
2785 ret = ci->i_auth_cap ? -EFBIG : -EUCLEAN;
2789 * If a sync write is in progress, we must wait, so that we
2790 * can get a final snapshot value for size+mtime.
2792 if (__ceph_have_pending_cap_snap(ci)) {
2793 dout("get_cap_refs %p cap_snap_pending\n", inode);
2798 if ((have & need) == need) {
2800 * Look at (implemented & ~have & not) so that we keep waiting
2801 * on transition from wanted -> needed caps. This is needed
2802 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2803 * going before a prior buffered writeback happens.
2805 * For RDCACHE|RD -> RD, there is not need to wait and we can
2806 * just exclude the revoking caps and force to sync read.
2808 int not = want & ~(have & need);
2809 int revoking = implemented & ~have;
2810 int exclude = revoking & not;
2811 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2812 inode, ceph_cap_string(have), ceph_cap_string(not),
2813 ceph_cap_string(revoking));
2814 if (!exclude || !(exclude & CEPH_CAP_FILE_BUFFER)) {
2815 if (!snap_rwsem_locked &&
2816 !ci->i_head_snapc &&
2817 (need & CEPH_CAP_FILE_WR)) {
2818 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2820 * we can not call down_read() when
2821 * task isn't in TASK_RUNNING state
2823 if (flags & NON_BLOCKING) {
2828 spin_unlock(&ci->i_ceph_lock);
2829 down_read(&mdsc->snap_rwsem);
2830 snap_rwsem_locked = true;
2833 snap_rwsem_locked = true;
2835 if ((have & want) == want)
2836 *got = need | (want & ~exclude);
2839 ceph_take_cap_refs(ci, *got, true);
2843 int session_readonly = false;
2845 if (ci->i_auth_cap &&
2846 (need & (CEPH_CAP_FILE_WR | CEPH_CAP_FILE_EXCL))) {
2847 struct ceph_mds_session *s = ci->i_auth_cap->session;
2848 spin_lock(&s->s_cap_lock);
2849 session_readonly = s->s_readonly;
2850 spin_unlock(&s->s_cap_lock);
2852 if (session_readonly) {
2853 dout("get_cap_refs %p need %s but mds%d readonly\n",
2854 inode, ceph_cap_string(need), ci->i_auth_cap->mds);
2859 if (ceph_inode_is_shutdown(inode)) {
2860 dout("get_cap_refs %p inode is shutdown\n", inode);
2864 mds_wanted = __ceph_caps_mds_wanted(ci, false);
2865 if (need & ~mds_wanted) {
2866 dout("get_cap_refs %p need %s > mds_wanted %s\n",
2867 inode, ceph_cap_string(need),
2868 ceph_cap_string(mds_wanted));
2873 dout("get_cap_refs %p have %s need %s\n", inode,
2874 ceph_cap_string(have), ceph_cap_string(need));
2878 __ceph_touch_fmode(ci, mdsc, flags);
2880 spin_unlock(&ci->i_ceph_lock);
2881 if (snap_rwsem_locked)
2882 up_read(&mdsc->snap_rwsem);
2885 ceph_update_cap_mis(&mdsc->metric);
2887 ceph_update_cap_hit(&mdsc->metric);
2889 dout("get_cap_refs %p ret %d got %s\n", inode,
2890 ret, ceph_cap_string(*got));
2895 * Check the offset we are writing up to against our current
2896 * max_size. If necessary, tell the MDS we want to write to
2899 static void check_max_size(struct inode *inode, loff_t endoff)
2901 struct ceph_inode_info *ci = ceph_inode(inode);
2904 /* do we need to explicitly request a larger max_size? */
2905 spin_lock(&ci->i_ceph_lock);
2906 if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
2907 dout("write %p at large endoff %llu, req max_size\n",
2909 ci->i_wanted_max_size = endoff;
2911 /* duplicate ceph_check_caps()'s logic */
2912 if (ci->i_auth_cap &&
2913 (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
2914 ci->i_wanted_max_size > ci->i_max_size &&
2915 ci->i_wanted_max_size > ci->i_requested_max_size)
2917 spin_unlock(&ci->i_ceph_lock);
2919 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY);
2922 static inline int get_used_fmode(int caps)
2925 if (caps & CEPH_CAP_FILE_RD)
2926 fmode |= CEPH_FILE_MODE_RD;
2927 if (caps & CEPH_CAP_FILE_WR)
2928 fmode |= CEPH_FILE_MODE_WR;
2932 int ceph_try_get_caps(struct inode *inode, int need, int want,
2933 bool nonblock, int *got)
2937 BUG_ON(need & ~CEPH_CAP_FILE_RD);
2938 BUG_ON(want & ~(CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO |
2939 CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
2940 CEPH_CAP_ANY_DIR_OPS));
2942 ret = ceph_pool_perm_check(inode, need);
2947 flags = get_used_fmode(need | want);
2949 flags |= NON_BLOCKING;
2951 ret = try_get_cap_refs(inode, need, want, 0, flags, got);
2952 /* three special error codes */
2953 if (ret == -EAGAIN || ret == -EFBIG || ret == -EUCLEAN)
2959 * Wait for caps, and take cap references. If we can't get a WR cap
2960 * due to a small max_size, make sure we check_max_size (and possibly
2961 * ask the mds) so we don't get hung up indefinitely.
2963 int __ceph_get_caps(struct inode *inode, struct ceph_file_info *fi, int need,
2964 int want, loff_t endoff, int *got)
2966 struct ceph_inode_info *ci = ceph_inode(inode);
2967 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
2968 int ret, _got, flags;
2970 ret = ceph_pool_perm_check(inode, need);
2974 if (fi && (fi->fmode & CEPH_FILE_MODE_WR) &&
2975 fi->filp_gen != READ_ONCE(fsc->filp_gen))
2978 flags = get_used_fmode(need | want);
2981 flags &= CEPH_FILE_MODE_MASK;
2982 if (vfs_inode_has_locks(inode))
2983 flags |= CHECK_FILELOCK;
2985 ret = try_get_cap_refs(inode, need, want, endoff,
2987 WARN_ON_ONCE(ret == -EAGAIN);
2989 struct ceph_mds_client *mdsc = fsc->mdsc;
2991 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2993 cw.ino = ceph_ino(inode);
2994 cw.tgid = current->tgid;
2998 spin_lock(&mdsc->caps_list_lock);
2999 list_add(&cw.list, &mdsc->cap_wait_list);
3000 spin_unlock(&mdsc->caps_list_lock);
3002 /* make sure used fmode not timeout */
3003 ceph_get_fmode(ci, flags, FMODE_WAIT_BIAS);
3004 add_wait_queue(&ci->i_cap_wq, &wait);
3006 flags |= NON_BLOCKING;
3007 while (!(ret = try_get_cap_refs(inode, need, want,
3008 endoff, flags, &_got))) {
3009 if (signal_pending(current)) {
3013 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
3016 remove_wait_queue(&ci->i_cap_wq, &wait);
3017 ceph_put_fmode(ci, flags, FMODE_WAIT_BIAS);
3019 spin_lock(&mdsc->caps_list_lock);
3021 spin_unlock(&mdsc->caps_list_lock);
3027 if (fi && (fi->fmode & CEPH_FILE_MODE_WR) &&
3028 fi->filp_gen != READ_ONCE(fsc->filp_gen)) {
3029 if (ret >= 0 && _got)
3030 ceph_put_cap_refs(ci, _got);
3035 if (ret == -EFBIG || ret == -EUCLEAN) {
3036 int ret2 = ceph_wait_on_async_create(inode);
3040 if (ret == -EFBIG) {
3041 check_max_size(inode, endoff);
3044 if (ret == -EUCLEAN) {
3045 /* session was killed, try renew caps */
3046 ret = ceph_renew_caps(inode, flags);
3053 if (S_ISREG(ci->netfs.inode.i_mode) &&
3054 ceph_has_inline_data(ci) &&
3055 (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
3056 i_size_read(inode) > 0) {
3058 find_get_page(inode->i_mapping, 0);
3060 bool uptodate = PageUptodate(page);
3067 * drop cap refs first because getattr while
3068 * holding * caps refs can cause deadlock.
3070 ceph_put_cap_refs(ci, _got);
3074 * getattr request will bring inline data into
3077 ret = __ceph_do_getattr(inode, NULL,
3078 CEPH_STAT_CAP_INLINE_DATA,
3090 int ceph_get_caps(struct file *filp, int need, int want, loff_t endoff,
3093 struct ceph_file_info *fi = filp->private_data;
3094 struct inode *inode = file_inode(filp);
3096 return __ceph_get_caps(inode, fi, need, want, endoff, got);
3100 * Take cap refs. Caller must already know we hold at least one ref
3101 * on the caps in question or we don't know this is safe.
3103 void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
3105 spin_lock(&ci->i_ceph_lock);
3106 ceph_take_cap_refs(ci, caps, false);
3107 spin_unlock(&ci->i_ceph_lock);
3112 * drop cap_snap that is not associated with any snapshot.
3113 * we don't need to send FLUSHSNAP message for it.
3115 static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci,
3116 struct ceph_cap_snap *capsnap)
3118 if (!capsnap->need_flush &&
3119 !capsnap->writing && !capsnap->dirty_pages) {
3120 dout("dropping cap_snap %p follows %llu\n",
3121 capsnap, capsnap->follows);
3122 BUG_ON(capsnap->cap_flush.tid > 0);
3123 ceph_put_snap_context(capsnap->context);
3124 if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps))
3125 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
3127 list_del(&capsnap->ci_item);
3128 ceph_put_cap_snap(capsnap);
3134 enum put_cap_refs_mode {
3135 PUT_CAP_REFS_SYNC = 0,
3136 PUT_CAP_REFS_NO_CHECK,
3143 * If we released the last ref on any given cap, call ceph_check_caps
3144 * to release (or schedule a release).
3146 * If we are releasing a WR cap (from a sync write), finalize any affected
3147 * cap_snap, and wake up any waiters.
3149 static void __ceph_put_cap_refs(struct ceph_inode_info *ci, int had,
3150 enum put_cap_refs_mode mode)
3152 struct inode *inode = &ci->netfs.inode;
3153 int last = 0, put = 0, flushsnaps = 0, wake = 0;
3154 bool check_flushsnaps = false;
3156 spin_lock(&ci->i_ceph_lock);
3157 if (had & CEPH_CAP_PIN)
3159 if (had & CEPH_CAP_FILE_RD)
3160 if (--ci->i_rd_ref == 0)
3162 if (had & CEPH_CAP_FILE_CACHE)
3163 if (--ci->i_rdcache_ref == 0)
3165 if (had & CEPH_CAP_FILE_EXCL)
3166 if (--ci->i_fx_ref == 0)
3168 if (had & CEPH_CAP_FILE_BUFFER) {
3169 if (--ci->i_wb_ref == 0) {
3171 /* put the ref held by ceph_take_cap_refs() */
3173 check_flushsnaps = true;
3175 dout("put_cap_refs %p wb %d -> %d (?)\n",
3176 inode, ci->i_wb_ref+1, ci->i_wb_ref);
3178 if (had & CEPH_CAP_FILE_WR) {
3179 if (--ci->i_wr_ref == 0) {
3181 * The Fb caps will always be took and released
3182 * together with the Fw caps.
3184 WARN_ON_ONCE(ci->i_wb_ref);
3187 check_flushsnaps = true;
3188 if (ci->i_wrbuffer_ref_head == 0 &&
3189 ci->i_dirty_caps == 0 &&
3190 ci->i_flushing_caps == 0) {
3191 BUG_ON(!ci->i_head_snapc);
3192 ceph_put_snap_context(ci->i_head_snapc);
3193 ci->i_head_snapc = NULL;
3195 /* see comment in __ceph_remove_cap() */
3196 if (!__ceph_is_any_real_caps(ci) && ci->i_snap_realm)
3197 ceph_change_snap_realm(inode, NULL);
3200 if (check_flushsnaps && __ceph_have_pending_cap_snap(ci)) {
3201 struct ceph_cap_snap *capsnap =
3202 list_last_entry(&ci->i_cap_snaps,
3203 struct ceph_cap_snap,
3206 capsnap->writing = 0;
3207 if (ceph_try_drop_cap_snap(ci, capsnap))
3208 /* put the ref held by ceph_queue_cap_snap() */
3210 else if (__ceph_finish_cap_snap(ci, capsnap))
3214 spin_unlock(&ci->i_ceph_lock);
3216 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
3217 last ? " last" : "", put ? " put" : "");
3220 case PUT_CAP_REFS_SYNC:
3222 ceph_check_caps(ci, 0);
3223 else if (flushsnaps)
3224 ceph_flush_snaps(ci, NULL);
3226 case PUT_CAP_REFS_ASYNC:
3228 ceph_queue_check_caps(inode);
3229 else if (flushsnaps)
3230 ceph_queue_flush_snaps(inode);
3236 wake_up_all(&ci->i_cap_wq);
3241 void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
3243 __ceph_put_cap_refs(ci, had, PUT_CAP_REFS_SYNC);
3246 void ceph_put_cap_refs_async(struct ceph_inode_info *ci, int had)
3248 __ceph_put_cap_refs(ci, had, PUT_CAP_REFS_ASYNC);
3251 void ceph_put_cap_refs_no_check_caps(struct ceph_inode_info *ci, int had)
3253 __ceph_put_cap_refs(ci, had, PUT_CAP_REFS_NO_CHECK);
3257 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
3258 * context. Adjust per-snap dirty page accounting as appropriate.
3259 * Once all dirty data for a cap_snap is flushed, flush snapped file
3260 * metadata back to the MDS. If we dropped the last ref, call
3263 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
3264 struct ceph_snap_context *snapc)
3266 struct inode *inode = &ci->netfs.inode;
3267 struct ceph_cap_snap *capsnap = NULL, *iter;
3270 bool flush_snaps = false;
3271 bool complete_capsnap = false;
3273 spin_lock(&ci->i_ceph_lock);
3274 ci->i_wrbuffer_ref -= nr;
3275 if (ci->i_wrbuffer_ref == 0) {
3280 if (ci->i_head_snapc == snapc) {
3281 ci->i_wrbuffer_ref_head -= nr;
3282 if (ci->i_wrbuffer_ref_head == 0 &&
3283 ci->i_wr_ref == 0 &&
3284 ci->i_dirty_caps == 0 &&
3285 ci->i_flushing_caps == 0) {
3286 BUG_ON(!ci->i_head_snapc);
3287 ceph_put_snap_context(ci->i_head_snapc);
3288 ci->i_head_snapc = NULL;
3290 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
3292 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
3293 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
3294 last ? " LAST" : "");
3296 list_for_each_entry(iter, &ci->i_cap_snaps, ci_item) {
3297 if (iter->context == snapc) {
3305 * The capsnap should already be removed when removing
3306 * auth cap in the case of a forced unmount.
3308 WARN_ON_ONCE(ci->i_auth_cap);
3312 capsnap->dirty_pages -= nr;
3313 if (capsnap->dirty_pages == 0) {
3314 complete_capsnap = true;
3315 if (!capsnap->writing) {
3316 if (ceph_try_drop_cap_snap(ci, capsnap)) {
3319 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
3324 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
3325 " snap %lld %d/%d -> %d/%d %s%s\n",
3326 inode, capsnap, capsnap->context->seq,
3327 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
3328 ci->i_wrbuffer_ref, capsnap->dirty_pages,
3329 last ? " (wrbuffer last)" : "",
3330 complete_capsnap ? " (complete capsnap)" : "");
3334 spin_unlock(&ci->i_ceph_lock);
3337 ceph_check_caps(ci, 0);
3338 } else if (flush_snaps) {
3339 ceph_flush_snaps(ci, NULL);
3341 if (complete_capsnap)
3342 wake_up_all(&ci->i_cap_wq);
3349 * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
3351 static void invalidate_aliases(struct inode *inode)
3353 struct dentry *dn, *prev = NULL;
3355 dout("invalidate_aliases inode %p\n", inode);
3356 d_prune_aliases(inode);
3358 * For non-directory inode, d_find_alias() only returns
3359 * hashed dentry. After calling d_invalidate(), the
3360 * dentry becomes unhashed.
3362 * For directory inode, d_find_alias() can return
3363 * unhashed dentry. But directory inode should have
3364 * one alias at most.
3366 while ((dn = d_find_alias(inode))) {
3380 struct cap_extra_info {
3381 struct ceph_string *pool_ns;
3391 /* currently issued */
3393 struct timespec64 btime;
3395 u32 fscrypt_auth_len;
3396 u64 fscrypt_file_size;
3400 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
3401 * actually be a revocation if it specifies a smaller cap set.)
3403 * caller holds s_mutex and i_ceph_lock, we drop both.
3405 static void handle_cap_grant(struct inode *inode,
3406 struct ceph_mds_session *session,
3407 struct ceph_cap *cap,
3408 struct ceph_mds_caps *grant,
3409 struct ceph_buffer *xattr_buf,
3410 struct cap_extra_info *extra_info)
3411 __releases(ci->i_ceph_lock)
3412 __releases(session->s_mdsc->snap_rwsem)
3414 struct ceph_inode_info *ci = ceph_inode(inode);
3415 int seq = le32_to_cpu(grant->seq);
3416 int newcaps = le32_to_cpu(grant->caps);
3417 int used, wanted, dirty;
3418 u64 size = le64_to_cpu(grant->size);
3419 u64 max_size = le64_to_cpu(grant->max_size);
3420 unsigned char check_caps = 0;
3421 bool was_stale = cap->cap_gen < atomic_read(&session->s_cap_gen);
3423 bool writeback = false;
3424 bool queue_trunc = false;
3425 bool queue_invalidate = false;
3426 bool deleted_inode = false;
3427 bool fill_inline = false;
3430 * If there is at least one crypto block then we'll trust
3431 * fscrypt_file_size. If the real length of the file is 0, then
3432 * ignore it (it has probably been truncated down to 0 by the MDS).
3434 if (IS_ENCRYPTED(inode) && size)
3435 size = extra_info->fscrypt_file_size;
3437 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
3438 inode, cap, session->s_mds, seq, ceph_cap_string(newcaps));
3439 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
3440 i_size_read(inode));
3444 * If CACHE is being revoked, and we have no dirty buffers,
3445 * try to invalidate (once). (If there are dirty buffers, we
3446 * will invalidate _after_ writeback.)
3448 if (S_ISREG(inode->i_mode) && /* don't invalidate readdir cache */
3449 ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
3450 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3451 !(ci->i_wrbuffer_ref || ci->i_wb_ref)) {
3452 if (try_nonblocking_invalidate(inode)) {
3453 /* there were locked pages.. invalidate later
3454 in a separate thread. */
3455 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
3456 queue_invalidate = true;
3457 ci->i_rdcache_revoking = ci->i_rdcache_gen;
3463 cap->issued = cap->implemented = CEPH_CAP_PIN;
3466 * auth mds of the inode changed. we received the cap export message,
3467 * but still haven't received the cap import message. handle_cap_export
3468 * updated the new auth MDS' cap.
3470 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
3471 * that was sent before the cap import message. So don't remove caps.
3473 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
3474 WARN_ON(cap != ci->i_auth_cap);
3475 WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
3477 newcaps |= cap->issued;
3480 /* side effects now are allowed */
3481 cap->cap_gen = atomic_read(&session->s_cap_gen);
3484 __check_cap_issue(ci, cap, newcaps);
3486 inode_set_max_iversion_raw(inode, extra_info->change_attr);
3488 if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
3489 (extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) {
3490 umode_t mode = le32_to_cpu(grant->mode);
3492 if (inode_wrong_type(inode, mode))
3493 pr_warn_once("inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n",
3494 ceph_vinop(inode), inode->i_mode, mode);
3496 inode->i_mode = mode;
3497 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
3498 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
3499 ci->i_btime = extra_info->btime;
3500 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
3501 from_kuid(&init_user_ns, inode->i_uid),
3502 from_kgid(&init_user_ns, inode->i_gid));
3503 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
3504 if (ci->fscrypt_auth_len != extra_info->fscrypt_auth_len ||
3505 memcmp(ci->fscrypt_auth, extra_info->fscrypt_auth,
3506 ci->fscrypt_auth_len))
3507 pr_warn_ratelimited("%s: cap grant attempt to change fscrypt_auth on non-I_NEW inode (old len %d new len %d)\n",
3508 __func__, ci->fscrypt_auth_len,
3509 extra_info->fscrypt_auth_len);
3513 if ((newcaps & CEPH_CAP_LINK_SHARED) &&
3514 (extra_info->issued & CEPH_CAP_LINK_EXCL) == 0) {
3515 set_nlink(inode, le32_to_cpu(grant->nlink));
3516 if (inode->i_nlink == 0)
3517 deleted_inode = true;
3520 if ((extra_info->issued & CEPH_CAP_XATTR_EXCL) == 0 &&
3522 int len = le32_to_cpu(grant->xattr_len);
3523 u64 version = le64_to_cpu(grant->xattr_version);
3525 if (version > ci->i_xattrs.version) {
3526 dout(" got new xattrs v%llu on %p len %d\n",
3527 version, inode, len);
3528 if (ci->i_xattrs.blob)
3529 ceph_buffer_put(ci->i_xattrs.blob);
3530 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
3531 ci->i_xattrs.version = version;
3532 ceph_forget_all_cached_acls(inode);
3533 ceph_security_invalidate_secctx(inode);
3537 if (newcaps & CEPH_CAP_ANY_RD) {
3538 struct timespec64 mtime, atime, ctime;
3539 /* ctime/mtime/atime? */
3540 ceph_decode_timespec64(&mtime, &grant->mtime);
3541 ceph_decode_timespec64(&atime, &grant->atime);
3542 ceph_decode_timespec64(&ctime, &grant->ctime);
3543 ceph_fill_file_time(inode, extra_info->issued,
3544 le32_to_cpu(grant->time_warp_seq),
3545 &ctime, &mtime, &atime);
3548 if ((newcaps & CEPH_CAP_FILE_SHARED) && extra_info->dirstat_valid) {
3549 ci->i_files = extra_info->nfiles;
3550 ci->i_subdirs = extra_info->nsubdirs;
3553 if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
3554 /* file layout may have changed */
3555 s64 old_pool = ci->i_layout.pool_id;
3556 struct ceph_string *old_ns;
3558 ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout);
3559 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
3560 lockdep_is_held(&ci->i_ceph_lock));
3561 rcu_assign_pointer(ci->i_layout.pool_ns, extra_info->pool_ns);
3563 if (ci->i_layout.pool_id != old_pool ||
3564 extra_info->pool_ns != old_ns)
3565 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
3567 extra_info->pool_ns = old_ns;
3569 /* size/truncate_seq? */
3570 queue_trunc = ceph_fill_file_size(inode, extra_info->issued,
3571 le32_to_cpu(grant->truncate_seq),
3572 le64_to_cpu(grant->truncate_size),
3576 if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) {
3577 if (max_size != ci->i_max_size) {
3578 dout("max_size %lld -> %llu\n",
3579 ci->i_max_size, max_size);
3580 ci->i_max_size = max_size;
3581 if (max_size >= ci->i_wanted_max_size) {
3582 ci->i_wanted_max_size = 0; /* reset */
3583 ci->i_requested_max_size = 0;
3589 /* check cap bits */
3590 wanted = __ceph_caps_wanted(ci);
3591 used = __ceph_caps_used(ci);
3592 dirty = __ceph_caps_dirty(ci);
3593 dout(" my wanted = %s, used = %s, dirty %s\n",
3594 ceph_cap_string(wanted),
3595 ceph_cap_string(used),
3596 ceph_cap_string(dirty));
3598 if ((was_stale || le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) &&
3599 (wanted & ~(cap->mds_wanted | newcaps))) {
3601 * If mds is importing cap, prior cap messages that update
3602 * 'wanted' may get dropped by mds (migrate seq mismatch).
3604 * We don't send cap message to update 'wanted' if what we
3605 * want are already issued. If mds revokes caps, cap message
3606 * that releases caps also tells mds what we want. But if
3607 * caps got revoked by mds forcedly (session stale). We may
3608 * haven't told mds what we want.
3613 /* revocation, grant, or no-op? */
3614 if (cap->issued & ~newcaps) {
3615 int revoking = cap->issued & ~newcaps;
3617 dout("revocation: %s -> %s (revoking %s)\n",
3618 ceph_cap_string(cap->issued),
3619 ceph_cap_string(newcaps),
3620 ceph_cap_string(revoking));
3621 if (S_ISREG(inode->i_mode) &&
3622 (revoking & used & CEPH_CAP_FILE_BUFFER))
3623 writeback = true; /* initiate writeback; will delay ack */
3624 else if (queue_invalidate &&
3625 revoking == CEPH_CAP_FILE_CACHE &&
3626 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0)
3627 ; /* do nothing yet, invalidation will be queued */
3628 else if (cap == ci->i_auth_cap)
3629 check_caps = 1; /* check auth cap only */
3631 check_caps = 2; /* check all caps */
3632 /* If there is new caps, try to wake up the waiters */
3633 if (~cap->issued & newcaps)
3635 cap->issued = newcaps;
3636 cap->implemented |= newcaps;
3637 } else if (cap->issued == newcaps) {
3638 dout("caps unchanged: %s -> %s\n",
3639 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
3641 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
3642 ceph_cap_string(newcaps));
3643 /* non-auth MDS is revoking the newly grant caps ? */
3644 if (cap == ci->i_auth_cap &&
3645 __ceph_caps_revoking_other(ci, cap, newcaps))
3648 cap->issued = newcaps;
3649 cap->implemented |= newcaps; /* add bits only, to
3650 * avoid stepping on a
3651 * pending revocation */
3654 BUG_ON(cap->issued & ~cap->implemented);
3656 /* don't let check_caps skip sending a response to MDS for revoke msgs */
3657 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_REVOKE) {
3658 cap->mds_wanted = 0;
3659 if (cap == ci->i_auth_cap)
3660 check_caps = 1; /* check auth cap only */
3662 check_caps = 2; /* check all caps */
3665 if (extra_info->inline_version > 0 &&
3666 extra_info->inline_version >= ci->i_inline_version) {
3667 ci->i_inline_version = extra_info->inline_version;
3668 if (ci->i_inline_version != CEPH_INLINE_NONE &&
3669 (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
3673 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
3674 if (ci->i_auth_cap == cap) {
3675 if (newcaps & ~extra_info->issued)
3678 if (ci->i_requested_max_size > max_size ||
3679 !(le32_to_cpu(grant->wanted) & CEPH_CAP_ANY_FILE_WR)) {
3680 /* re-request max_size if necessary */
3681 ci->i_requested_max_size = 0;
3685 ceph_kick_flushing_inode_caps(session, ci);
3687 up_read(&session->s_mdsc->snap_rwsem);
3689 spin_unlock(&ci->i_ceph_lock);
3692 ceph_fill_inline_data(inode, NULL, extra_info->inline_data,
3693 extra_info->inline_len);
3696 ceph_queue_vmtruncate(inode);
3700 * queue inode for writeback: we can't actually call
3701 * filemap_write_and_wait, etc. from message handler
3704 ceph_queue_writeback(inode);
3705 if (queue_invalidate)
3706 ceph_queue_invalidate(inode);
3708 invalidate_aliases(inode);
3710 wake_up_all(&ci->i_cap_wq);
3712 mutex_unlock(&session->s_mutex);
3713 if (check_caps == 1)
3714 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY | CHECK_CAPS_NOINVAL);
3715 else if (check_caps == 2)
3716 ceph_check_caps(ci, CHECK_CAPS_NOINVAL);
3720 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
3721 * MDS has been safely committed.
3723 static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
3724 struct ceph_mds_caps *m,
3725 struct ceph_mds_session *session,
3726 struct ceph_cap *cap)
3727 __releases(ci->i_ceph_lock)
3729 struct ceph_inode_info *ci = ceph_inode(inode);
3730 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3731 struct ceph_cap_flush *cf, *tmp_cf;
3732 LIST_HEAD(to_remove);
3733 unsigned seq = le32_to_cpu(m->seq);
3734 int dirty = le32_to_cpu(m->dirty);
3737 bool wake_ci = false;
3738 bool wake_mdsc = false;
3740 list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
3741 /* Is this the one that was flushed? */
3742 if (cf->tid == flush_tid)
3745 /* Is this a capsnap? */
3749 if (cf->tid <= flush_tid) {
3751 * An earlier or current tid. The FLUSH_ACK should
3752 * represent a superset of this flush's caps.
3754 wake_ci |= __detach_cap_flush_from_ci(ci, cf);
3755 list_add_tail(&cf->i_list, &to_remove);
3758 * This is a later one. Any caps in it are still dirty
3759 * so don't count them as cleaned.
3761 cleaned &= ~cf->caps;
3767 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
3768 " flushing %s -> %s\n",
3769 inode, session->s_mds, seq, ceph_cap_string(dirty),
3770 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
3771 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
3773 if (list_empty(&to_remove) && !cleaned)
3776 ci->i_flushing_caps &= ~cleaned;
3778 spin_lock(&mdsc->cap_dirty_lock);
3780 list_for_each_entry(cf, &to_remove, i_list)
3781 wake_mdsc |= __detach_cap_flush_from_mdsc(mdsc, cf);
3783 if (ci->i_flushing_caps == 0) {
3784 if (list_empty(&ci->i_cap_flush_list)) {
3785 list_del_init(&ci->i_flushing_item);
3786 if (!list_empty(&session->s_cap_flushing)) {
3787 dout(" mds%d still flushing cap on %p\n",
3789 &list_first_entry(&session->s_cap_flushing,
3790 struct ceph_inode_info,
3791 i_flushing_item)->netfs.inode);
3794 mdsc->num_cap_flushing--;
3795 dout(" inode %p now !flushing\n", inode);
3797 if (ci->i_dirty_caps == 0) {
3798 dout(" inode %p now clean\n", inode);
3799 BUG_ON(!list_empty(&ci->i_dirty_item));
3801 if (ci->i_wr_ref == 0 &&
3802 ci->i_wrbuffer_ref_head == 0) {
3803 BUG_ON(!ci->i_head_snapc);
3804 ceph_put_snap_context(ci->i_head_snapc);
3805 ci->i_head_snapc = NULL;
3808 BUG_ON(list_empty(&ci->i_dirty_item));
3811 spin_unlock(&mdsc->cap_dirty_lock);
3814 spin_unlock(&ci->i_ceph_lock);
3816 while (!list_empty(&to_remove)) {
3817 cf = list_first_entry(&to_remove,
3818 struct ceph_cap_flush, i_list);
3819 list_del_init(&cf->i_list);
3820 if (!cf->is_capsnap)
3821 ceph_free_cap_flush(cf);
3825 wake_up_all(&ci->i_cap_wq);
3827 wake_up_all(&mdsc->cap_flushing_wq);
3832 void __ceph_remove_capsnap(struct inode *inode, struct ceph_cap_snap *capsnap,
3833 bool *wake_ci, bool *wake_mdsc)
3835 struct ceph_inode_info *ci = ceph_inode(inode);
3836 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3839 lockdep_assert_held(&ci->i_ceph_lock);
3841 dout("removing capsnap %p, inode %p ci %p\n", capsnap, inode, ci);
3843 list_del_init(&capsnap->ci_item);
3844 ret = __detach_cap_flush_from_ci(ci, &capsnap->cap_flush);
3848 spin_lock(&mdsc->cap_dirty_lock);
3849 if (list_empty(&ci->i_cap_flush_list))
3850 list_del_init(&ci->i_flushing_item);
3852 ret = __detach_cap_flush_from_mdsc(mdsc, &capsnap->cap_flush);
3855 spin_unlock(&mdsc->cap_dirty_lock);
3858 void ceph_remove_capsnap(struct inode *inode, struct ceph_cap_snap *capsnap,
3859 bool *wake_ci, bool *wake_mdsc)
3861 struct ceph_inode_info *ci = ceph_inode(inode);
3863 lockdep_assert_held(&ci->i_ceph_lock);
3865 WARN_ON_ONCE(capsnap->dirty_pages || capsnap->writing);
3866 __ceph_remove_capsnap(inode, capsnap, wake_ci, wake_mdsc);
3870 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
3871 * throw away our cap_snap.
3873 * Caller hold s_mutex.
3875 static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
3876 struct ceph_mds_caps *m,
3877 struct ceph_mds_session *session)
3879 struct ceph_inode_info *ci = ceph_inode(inode);
3880 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3881 u64 follows = le64_to_cpu(m->snap_follows);
3882 struct ceph_cap_snap *capsnap = NULL, *iter;
3883 bool wake_ci = false;
3884 bool wake_mdsc = false;
3886 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
3887 inode, ci, session->s_mds, follows);
3889 spin_lock(&ci->i_ceph_lock);
3890 list_for_each_entry(iter, &ci->i_cap_snaps, ci_item) {
3891 if (iter->follows == follows) {
3892 if (iter->cap_flush.tid != flush_tid) {
3893 dout(" cap_snap %p follows %lld tid %lld !="
3894 " %lld\n", iter, follows,
3895 flush_tid, iter->cap_flush.tid);
3901 dout(" skipping cap_snap %p follows %lld\n",
3902 iter, iter->follows);
3906 ceph_remove_capsnap(inode, capsnap, &wake_ci, &wake_mdsc);
3907 spin_unlock(&ci->i_ceph_lock);
3910 ceph_put_snap_context(capsnap->context);
3911 ceph_put_cap_snap(capsnap);
3913 wake_up_all(&ci->i_cap_wq);
3915 wake_up_all(&mdsc->cap_flushing_wq);
3921 * Handle TRUNC from MDS, indicating file truncation.
3923 * caller hold s_mutex.
3925 static bool handle_cap_trunc(struct inode *inode,
3926 struct ceph_mds_caps *trunc,
3927 struct ceph_mds_session *session,
3928 struct cap_extra_info *extra_info)
3930 struct ceph_inode_info *ci = ceph_inode(inode);
3931 int mds = session->s_mds;
3932 int seq = le32_to_cpu(trunc->seq);
3933 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
3934 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
3935 u64 size = le64_to_cpu(trunc->size);
3936 int implemented = 0;
3937 int dirty = __ceph_caps_dirty(ci);
3938 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
3939 bool queue_trunc = false;
3941 lockdep_assert_held(&ci->i_ceph_lock);
3943 issued |= implemented | dirty;
3946 * If there is at least one crypto block then we'll trust
3947 * fscrypt_file_size. If the real length of the file is 0, then
3948 * ignore it (it has probably been truncated down to 0 by the MDS).
3950 if (IS_ENCRYPTED(inode) && size)
3951 size = extra_info->fscrypt_file_size;
3953 dout("%s inode %p mds%d seq %d to %lld truncate seq %d\n",
3954 __func__, inode, mds, seq, truncate_size, truncate_seq);
3955 queue_trunc = ceph_fill_file_size(inode, issued,
3956 truncate_seq, truncate_size, size);
3961 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
3962 * different one. If we are the most recent migration we've seen (as
3963 * indicated by mseq), make note of the migrating cap bits for the
3964 * duration (until we see the corresponding IMPORT).
3966 * caller holds s_mutex
3968 static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
3969 struct ceph_mds_cap_peer *ph,
3970 struct ceph_mds_session *session)
3972 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
3973 struct ceph_mds_session *tsession = NULL;
3974 struct ceph_cap *cap, *tcap, *new_cap = NULL;
3975 struct ceph_inode_info *ci = ceph_inode(inode);
3977 unsigned mseq = le32_to_cpu(ex->migrate_seq);
3978 unsigned t_seq, t_mseq;
3980 int mds = session->s_mds;
3983 t_cap_id = le64_to_cpu(ph->cap_id);
3984 t_seq = le32_to_cpu(ph->seq);
3985 t_mseq = le32_to_cpu(ph->mseq);
3986 target = le32_to_cpu(ph->mds);
3988 t_cap_id = t_seq = t_mseq = 0;
3992 dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n",
3993 inode, ci, mds, mseq, target);
3995 down_read(&mdsc->snap_rwsem);
3996 spin_lock(&ci->i_ceph_lock);
3997 cap = __get_cap_for_mds(ci, mds);
3998 if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
4002 ceph_remove_cap(cap, false);
4007 * now we know we haven't received the cap import message yet
4008 * because the exported cap still exist.
4011 issued = cap->issued;
4012 if (issued != cap->implemented)
4013 pr_err_ratelimited("handle_cap_export: issued != implemented: "
4014 "ino (%llx.%llx) mds%d seq %d mseq %d "
4015 "issued %s implemented %s\n",
4016 ceph_vinop(inode), mds, cap->seq, cap->mseq,
4017 ceph_cap_string(issued),
4018 ceph_cap_string(cap->implemented));
4021 tcap = __get_cap_for_mds(ci, target);
4023 /* already have caps from the target */
4024 if (tcap->cap_id == t_cap_id &&
4025 ceph_seq_cmp(tcap->seq, t_seq) < 0) {
4026 dout(" updating import cap %p mds%d\n", tcap, target);
4027 tcap->cap_id = t_cap_id;
4028 tcap->seq = t_seq - 1;
4029 tcap->issue_seq = t_seq - 1;
4030 tcap->issued |= issued;
4031 tcap->implemented |= issued;
4032 if (cap == ci->i_auth_cap) {
4033 ci->i_auth_cap = tcap;
4034 change_auth_cap_ses(ci, tcap->session);
4037 ceph_remove_cap(cap, false);
4039 } else if (tsession) {
4040 /* add placeholder for the export tagert */
4041 int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
4043 ceph_add_cap(inode, tsession, t_cap_id, issued, 0,
4044 t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
4046 if (!list_empty(&ci->i_cap_flush_list) &&
4047 ci->i_auth_cap == tcap) {
4048 spin_lock(&mdsc->cap_dirty_lock);
4049 list_move_tail(&ci->i_flushing_item,
4050 &tcap->session->s_cap_flushing);
4051 spin_unlock(&mdsc->cap_dirty_lock);
4054 ceph_remove_cap(cap, false);
4058 spin_unlock(&ci->i_ceph_lock);
4059 up_read(&mdsc->snap_rwsem);
4060 mutex_unlock(&session->s_mutex);
4062 /* open target session */
4063 tsession = ceph_mdsc_open_export_target_session(mdsc, target);
4064 if (!IS_ERR(tsession)) {
4066 mutex_lock(&session->s_mutex);
4067 mutex_lock_nested(&tsession->s_mutex,
4068 SINGLE_DEPTH_NESTING);
4070 mutex_lock(&tsession->s_mutex);
4071 mutex_lock_nested(&session->s_mutex,
4072 SINGLE_DEPTH_NESTING);
4074 new_cap = ceph_get_cap(mdsc, NULL);
4079 mutex_lock(&session->s_mutex);
4084 spin_unlock(&ci->i_ceph_lock);
4085 up_read(&mdsc->snap_rwsem);
4086 mutex_unlock(&session->s_mutex);
4088 mutex_unlock(&tsession->s_mutex);
4089 ceph_put_mds_session(tsession);
4092 ceph_put_cap(mdsc, new_cap);
4096 * Handle cap IMPORT.
4098 * caller holds s_mutex. acquires i_ceph_lock
4100 static void handle_cap_import(struct ceph_mds_client *mdsc,
4101 struct inode *inode, struct ceph_mds_caps *im,
4102 struct ceph_mds_cap_peer *ph,
4103 struct ceph_mds_session *session,
4104 struct ceph_cap **target_cap, int *old_issued)
4106 struct ceph_inode_info *ci = ceph_inode(inode);
4107 struct ceph_cap *cap, *ocap, *new_cap = NULL;
4108 int mds = session->s_mds;
4110 unsigned caps = le32_to_cpu(im->caps);
4111 unsigned wanted = le32_to_cpu(im->wanted);
4112 unsigned seq = le32_to_cpu(im->seq);
4113 unsigned mseq = le32_to_cpu(im->migrate_seq);
4114 u64 realmino = le64_to_cpu(im->realm);
4115 u64 cap_id = le64_to_cpu(im->cap_id);
4120 p_cap_id = le64_to_cpu(ph->cap_id);
4121 peer = le32_to_cpu(ph->mds);
4127 dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n",
4128 inode, ci, mds, mseq, peer);
4130 cap = __get_cap_for_mds(ci, mds);
4133 spin_unlock(&ci->i_ceph_lock);
4134 new_cap = ceph_get_cap(mdsc, NULL);
4135 spin_lock(&ci->i_ceph_lock);
4141 ceph_put_cap(mdsc, new_cap);
4146 __ceph_caps_issued(ci, &issued);
4147 issued |= __ceph_caps_dirty(ci);
4149 ceph_add_cap(inode, session, cap_id, caps, wanted, seq, mseq,
4150 realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
4152 ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
4153 if (ocap && ocap->cap_id == p_cap_id) {
4154 dout(" remove export cap %p mds%d flags %d\n",
4155 ocap, peer, ph->flags);
4156 if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
4157 (ocap->seq != le32_to_cpu(ph->seq) ||
4158 ocap->mseq != le32_to_cpu(ph->mseq))) {
4159 pr_err_ratelimited("handle_cap_import: "
4160 "mismatched seq/mseq: ino (%llx.%llx) "
4161 "mds%d seq %d mseq %d importer mds%d "
4162 "has peer seq %d mseq %d\n",
4163 ceph_vinop(inode), peer, ocap->seq,
4164 ocap->mseq, mds, le32_to_cpu(ph->seq),
4165 le32_to_cpu(ph->mseq));
4167 ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
4170 *old_issued = issued;
4174 #ifdef CONFIG_FS_ENCRYPTION
4175 static int parse_fscrypt_fields(void **p, void *end,
4176 struct cap_extra_info *extra)
4180 ceph_decode_32_safe(p, end, extra->fscrypt_auth_len, bad);
4181 if (extra->fscrypt_auth_len) {
4182 ceph_decode_need(p, end, extra->fscrypt_auth_len, bad);
4183 extra->fscrypt_auth = kmalloc(extra->fscrypt_auth_len,
4185 if (!extra->fscrypt_auth)
4187 ceph_decode_copy_safe(p, end, extra->fscrypt_auth,
4188 extra->fscrypt_auth_len, bad);
4191 ceph_decode_32_safe(p, end, len, bad);
4192 if (len >= sizeof(u64)) {
4193 ceph_decode_64_safe(p, end, extra->fscrypt_file_size, bad);
4196 ceph_decode_skip_n(p, end, len, bad);
4202 static int parse_fscrypt_fields(void **p, void *end,
4203 struct cap_extra_info *extra)
4207 /* Don't care about these fields unless we're encryption-capable */
4208 ceph_decode_32_safe(p, end, len, bad);
4210 ceph_decode_skip_n(p, end, len, bad);
4211 ceph_decode_32_safe(p, end, len, bad);
4213 ceph_decode_skip_n(p, end, len, bad);
4221 * Handle a caps message from the MDS.
4223 * Identify the appropriate session, inode, and call the right handler
4224 * based on the cap op.
4226 void ceph_handle_caps(struct ceph_mds_session *session,
4227 struct ceph_msg *msg)
4229 struct ceph_mds_client *mdsc = session->s_mdsc;
4230 struct inode *inode;
4231 struct ceph_inode_info *ci;
4232 struct ceph_cap *cap;
4233 struct ceph_mds_caps *h;
4234 struct ceph_mds_cap_peer *peer = NULL;
4235 struct ceph_snap_realm *realm = NULL;
4237 int msg_version = le16_to_cpu(msg->hdr.version);
4239 struct ceph_vino vino;
4241 size_t snaptrace_len;
4243 struct cap_extra_info extra_info = {};
4245 bool close_sessions = false;
4246 bool do_cap_release = false;
4248 dout("handle_caps from mds%d\n", session->s_mds);
4250 if (!ceph_inc_mds_stopping_blocker(mdsc, session))
4254 end = msg->front.iov_base + msg->front.iov_len;
4255 if (msg->front.iov_len < sizeof(*h))
4257 h = msg->front.iov_base;
4258 op = le32_to_cpu(h->op);
4259 vino.ino = le64_to_cpu(h->ino);
4260 vino.snap = CEPH_NOSNAP;
4261 seq = le32_to_cpu(h->seq);
4262 mseq = le32_to_cpu(h->migrate_seq);
4265 snaptrace_len = le32_to_cpu(h->snap_trace_len);
4266 p = snaptrace + snaptrace_len;
4268 if (msg_version >= 2) {
4270 ceph_decode_32_safe(&p, end, flock_len, bad);
4271 if (p + flock_len > end)
4276 if (msg_version >= 3) {
4277 if (op == CEPH_CAP_OP_IMPORT) {
4278 if (p + sizeof(*peer) > end)
4282 } else if (op == CEPH_CAP_OP_EXPORT) {
4283 /* recorded in unused fields */
4284 peer = (void *)&h->size;
4288 if (msg_version >= 4) {
4289 ceph_decode_64_safe(&p, end, extra_info.inline_version, bad);
4290 ceph_decode_32_safe(&p, end, extra_info.inline_len, bad);
4291 if (p + extra_info.inline_len > end)
4293 extra_info.inline_data = p;
4294 p += extra_info.inline_len;
4297 if (msg_version >= 5) {
4298 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
4301 ceph_decode_32_safe(&p, end, epoch_barrier, bad);
4302 ceph_osdc_update_epoch_barrier(osdc, epoch_barrier);
4305 if (msg_version >= 8) {
4309 ceph_decode_skip_64(&p, end, bad); // flush_tid
4311 ceph_decode_skip_32(&p, end, bad); // caller_uid
4312 ceph_decode_skip_32(&p, end, bad); // caller_gid
4314 ceph_decode_32_safe(&p, end, pool_ns_len, bad);
4315 if (pool_ns_len > 0) {
4316 ceph_decode_need(&p, end, pool_ns_len, bad);
4317 extra_info.pool_ns =
4318 ceph_find_or_create_string(p, pool_ns_len);
4323 if (msg_version >= 9) {
4324 struct ceph_timespec *btime;
4326 if (p + sizeof(*btime) > end)
4329 ceph_decode_timespec64(&extra_info.btime, btime);
4330 p += sizeof(*btime);
4331 ceph_decode_64_safe(&p, end, extra_info.change_attr, bad);
4334 if (msg_version >= 11) {
4336 ceph_decode_skip_32(&p, end, bad); // flags
4338 extra_info.dirstat_valid = true;
4339 ceph_decode_64_safe(&p, end, extra_info.nfiles, bad);
4340 ceph_decode_64_safe(&p, end, extra_info.nsubdirs, bad);
4343 if (msg_version >= 12) {
4344 if (parse_fscrypt_fields(&p, end, &extra_info))
4349 inode = ceph_find_inode(mdsc->fsc->sb, vino);
4350 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
4353 mutex_lock(&session->s_mutex);
4354 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
4358 dout(" i don't have ino %llx\n", vino.ino);
4361 case CEPH_CAP_OP_IMPORT:
4362 case CEPH_CAP_OP_REVOKE:
4363 case CEPH_CAP_OP_GRANT:
4364 do_cap_release = true;
4369 goto flush_cap_releases;
4371 ci = ceph_inode(inode);
4373 /* these will work even if we don't have a cap yet */
4375 case CEPH_CAP_OP_FLUSHSNAP_ACK:
4376 handle_cap_flushsnap_ack(inode, le64_to_cpu(msg->hdr.tid),
4380 case CEPH_CAP_OP_EXPORT:
4381 handle_cap_export(inode, h, peer, session);
4384 case CEPH_CAP_OP_IMPORT:
4386 if (snaptrace_len) {
4387 down_write(&mdsc->snap_rwsem);
4388 if (ceph_update_snap_trace(mdsc, snaptrace,
4389 snaptrace + snaptrace_len,
4391 up_write(&mdsc->snap_rwsem);
4392 close_sessions = true;
4395 downgrade_write(&mdsc->snap_rwsem);
4397 down_read(&mdsc->snap_rwsem);
4399 spin_lock(&ci->i_ceph_lock);
4400 handle_cap_import(mdsc, inode, h, peer, session,
4401 &cap, &extra_info.issued);
4402 handle_cap_grant(inode, session, cap,
4403 h, msg->middle, &extra_info);
4405 ceph_put_snap_realm(mdsc, realm);
4409 /* the rest require a cap */
4410 spin_lock(&ci->i_ceph_lock);
4411 cap = __get_cap_for_mds(ceph_inode(inode), session->s_mds);
4413 dout(" no cap on %p ino %llx.%llx from mds%d\n",
4414 inode, ceph_ino(inode), ceph_snap(inode),
4416 spin_unlock(&ci->i_ceph_lock);
4418 case CEPH_CAP_OP_REVOKE:
4419 case CEPH_CAP_OP_GRANT:
4420 do_cap_release = true;
4425 goto flush_cap_releases;
4428 /* note that each of these drops i_ceph_lock for us */
4430 case CEPH_CAP_OP_REVOKE:
4431 case CEPH_CAP_OP_GRANT:
4432 __ceph_caps_issued(ci, &extra_info.issued);
4433 extra_info.issued |= __ceph_caps_dirty(ci);
4434 handle_cap_grant(inode, session, cap,
4435 h, msg->middle, &extra_info);
4438 case CEPH_CAP_OP_FLUSH_ACK:
4439 handle_cap_flush_ack(inode, le64_to_cpu(msg->hdr.tid),
4443 case CEPH_CAP_OP_TRUNC:
4444 queue_trunc = handle_cap_trunc(inode, h, session,
4446 spin_unlock(&ci->i_ceph_lock);
4448 ceph_queue_vmtruncate(inode);
4452 spin_unlock(&ci->i_ceph_lock);
4453 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
4454 ceph_cap_op_name(op));
4458 mutex_unlock(&session->s_mutex);
4462 ceph_dec_mds_stopping_blocker(mdsc);
4464 ceph_put_string(extra_info.pool_ns);
4466 /* Defer closing the sessions after s_mutex lock being released */
4468 ceph_mdsc_close_sessions(mdsc);
4470 kfree(extra_info.fscrypt_auth);
4475 * send any cap release message to try to move things
4476 * along for the mds (who clearly thinks we still have this
4479 if (do_cap_release) {
4480 cap = ceph_get_cap(mdsc, NULL);
4481 cap->cap_ino = vino.ino;
4482 cap->queue_release = 1;
4483 cap->cap_id = le64_to_cpu(h->cap_id);
4486 cap->issue_seq = seq;
4487 spin_lock(&session->s_cap_lock);
4488 __ceph_queue_cap_release(session, cap);
4489 spin_unlock(&session->s_cap_lock);
4491 ceph_flush_cap_releases(mdsc, session);
4495 pr_err("ceph_handle_caps: corrupt message\n");
4501 * Delayed work handler to process end of delayed cap release LRU list.
4503 * If new caps are added to the list while processing it, these won't get
4504 * processed in this run. In this case, the ci->i_hold_caps_max will be
4505 * returned so that the work can be scheduled accordingly.
4507 unsigned long ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
4509 struct inode *inode;
4510 struct ceph_inode_info *ci;
4511 struct ceph_mount_options *opt = mdsc->fsc->mount_options;
4512 unsigned long delay_max = opt->caps_wanted_delay_max * HZ;
4513 unsigned long loop_start = jiffies;
4514 unsigned long delay = 0;
4516 dout("check_delayed_caps\n");
4517 spin_lock(&mdsc->cap_delay_lock);
4518 while (!list_empty(&mdsc->cap_delay_list)) {
4519 ci = list_first_entry(&mdsc->cap_delay_list,
4520 struct ceph_inode_info,
4522 if (time_before(loop_start, ci->i_hold_caps_max - delay_max)) {
4523 dout("%s caps added recently. Exiting loop", __func__);
4524 delay = ci->i_hold_caps_max;
4527 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
4528 time_before(jiffies, ci->i_hold_caps_max))
4530 list_del_init(&ci->i_cap_delay_list);
4532 inode = igrab(&ci->netfs.inode);
4534 spin_unlock(&mdsc->cap_delay_lock);
4535 dout("check_delayed_caps on %p\n", inode);
4536 ceph_check_caps(ci, 0);
4538 spin_lock(&mdsc->cap_delay_lock);
4541 spin_unlock(&mdsc->cap_delay_lock);
4547 * Flush all dirty caps to the mds
4549 static void flush_dirty_session_caps(struct ceph_mds_session *s)
4551 struct ceph_mds_client *mdsc = s->s_mdsc;
4552 struct ceph_inode_info *ci;
4553 struct inode *inode;
4555 dout("flush_dirty_caps\n");
4556 spin_lock(&mdsc->cap_dirty_lock);
4557 while (!list_empty(&s->s_cap_dirty)) {
4558 ci = list_first_entry(&s->s_cap_dirty, struct ceph_inode_info,
4560 inode = &ci->netfs.inode;
4562 dout("flush_dirty_caps %llx.%llx\n", ceph_vinop(inode));
4563 spin_unlock(&mdsc->cap_dirty_lock);
4564 ceph_wait_on_async_create(inode);
4565 ceph_check_caps(ci, CHECK_CAPS_FLUSH);
4567 spin_lock(&mdsc->cap_dirty_lock);
4569 spin_unlock(&mdsc->cap_dirty_lock);
4570 dout("flush_dirty_caps done\n");
4573 void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
4575 ceph_mdsc_iterate_sessions(mdsc, flush_dirty_session_caps, true);
4578 void __ceph_touch_fmode(struct ceph_inode_info *ci,
4579 struct ceph_mds_client *mdsc, int fmode)
4581 unsigned long now = jiffies;
4582 if (fmode & CEPH_FILE_MODE_RD)
4583 ci->i_last_rd = now;
4584 if (fmode & CEPH_FILE_MODE_WR)
4585 ci->i_last_wr = now;
4586 /* queue periodic check */
4588 __ceph_is_any_real_caps(ci) &&
4589 list_empty(&ci->i_cap_delay_list))
4590 __cap_delay_requeue(mdsc, ci);
4593 void ceph_get_fmode(struct ceph_inode_info *ci, int fmode, int count)
4595 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(ci->netfs.inode.i_sb);
4596 int bits = (fmode << 1) | 1;
4597 bool already_opened = false;
4601 atomic64_inc(&mdsc->metric.opened_files);
4603 spin_lock(&ci->i_ceph_lock);
4604 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
4606 * If any of the mode ref is larger than 0,
4607 * that means it has been already opened by
4608 * others. Just skip checking the PIN ref.
4610 if (i && ci->i_nr_by_mode[i])
4611 already_opened = true;
4613 if (bits & (1 << i))
4614 ci->i_nr_by_mode[i] += count;
4617 if (!already_opened)
4618 percpu_counter_inc(&mdsc->metric.opened_inodes);
4619 spin_unlock(&ci->i_ceph_lock);
4623 * Drop open file reference. If we were the last open file,
4624 * we may need to release capabilities to the MDS (or schedule
4625 * their delayed release).
4627 void ceph_put_fmode(struct ceph_inode_info *ci, int fmode, int count)
4629 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(ci->netfs.inode.i_sb);
4630 int bits = (fmode << 1) | 1;
4631 bool is_closed = true;
4635 atomic64_dec(&mdsc->metric.opened_files);
4637 spin_lock(&ci->i_ceph_lock);
4638 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
4639 if (bits & (1 << i)) {
4640 BUG_ON(ci->i_nr_by_mode[i] < count);
4641 ci->i_nr_by_mode[i] -= count;
4645 * If any of the mode ref is not 0 after
4646 * decreased, that means it is still opened
4647 * by others. Just skip checking the PIN ref.
4649 if (i && ci->i_nr_by_mode[i])
4654 percpu_counter_dec(&mdsc->metric.opened_inodes);
4655 spin_unlock(&ci->i_ceph_lock);
4659 * For a soon-to-be unlinked file, drop the LINK caps. If it
4660 * looks like the link count will hit 0, drop any other caps (other
4661 * than PIN) we don't specifically want (due to the file still being
4664 int ceph_drop_caps_for_unlink(struct inode *inode)
4666 struct ceph_inode_info *ci = ceph_inode(inode);
4667 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
4669 spin_lock(&ci->i_ceph_lock);
4670 if (inode->i_nlink == 1) {
4671 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
4673 if (__ceph_caps_dirty(ci)) {
4674 struct ceph_mds_client *mdsc =
4675 ceph_inode_to_client(inode)->mdsc;
4676 __cap_delay_requeue_front(mdsc, ci);
4679 spin_unlock(&ci->i_ceph_lock);
4684 * Helpers for embedding cap and dentry lease releases into mds
4687 * @force is used by dentry_release (below) to force inclusion of a
4688 * record for the directory inode, even when there aren't any caps to
4691 int ceph_encode_inode_release(void **p, struct inode *inode,
4692 int mds, int drop, int unless, int force)
4694 struct ceph_inode_info *ci = ceph_inode(inode);
4695 struct ceph_cap *cap;
4696 struct ceph_mds_request_release *rel = *p;
4700 spin_lock(&ci->i_ceph_lock);
4701 used = __ceph_caps_used(ci);
4702 dirty = __ceph_caps_dirty(ci);
4704 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
4705 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
4706 ceph_cap_string(unless));
4708 /* only drop unused, clean caps */
4709 drop &= ~(used | dirty);
4711 cap = __get_cap_for_mds(ci, mds);
4712 if (cap && __cap_is_valid(cap)) {
4713 unless &= cap->issued;
4715 if (unless & CEPH_CAP_AUTH_EXCL)
4716 drop &= ~CEPH_CAP_AUTH_SHARED;
4717 if (unless & CEPH_CAP_LINK_EXCL)
4718 drop &= ~CEPH_CAP_LINK_SHARED;
4719 if (unless & CEPH_CAP_XATTR_EXCL)
4720 drop &= ~CEPH_CAP_XATTR_SHARED;
4721 if (unless & CEPH_CAP_FILE_EXCL)
4722 drop &= ~CEPH_CAP_FILE_SHARED;
4725 if (force || (cap->issued & drop)) {
4726 if (cap->issued & drop) {
4727 int wanted = __ceph_caps_wanted(ci);
4728 dout("encode_inode_release %p cap %p "
4729 "%s -> %s, wanted %s -> %s\n", inode, cap,
4730 ceph_cap_string(cap->issued),
4731 ceph_cap_string(cap->issued & ~drop),
4732 ceph_cap_string(cap->mds_wanted),
4733 ceph_cap_string(wanted));
4735 cap->issued &= ~drop;
4736 cap->implemented &= ~drop;
4737 cap->mds_wanted = wanted;
4738 if (cap == ci->i_auth_cap &&
4739 !(wanted & CEPH_CAP_ANY_FILE_WR))
4740 ci->i_requested_max_size = 0;
4742 dout("encode_inode_release %p cap %p %s"
4743 " (force)\n", inode, cap,
4744 ceph_cap_string(cap->issued));
4747 rel->ino = cpu_to_le64(ceph_ino(inode));
4748 rel->cap_id = cpu_to_le64(cap->cap_id);
4749 rel->seq = cpu_to_le32(cap->seq);
4750 rel->issue_seq = cpu_to_le32(cap->issue_seq);
4751 rel->mseq = cpu_to_le32(cap->mseq);
4752 rel->caps = cpu_to_le32(cap->implemented);
4753 rel->wanted = cpu_to_le32(cap->mds_wanted);
4759 dout("encode_inode_release %p cap %p %s (noop)\n",
4760 inode, cap, ceph_cap_string(cap->issued));
4763 spin_unlock(&ci->i_ceph_lock);
4768 * ceph_encode_dentry_release - encode a dentry release into an outgoing request
4769 * @p: outgoing request buffer
4770 * @dentry: dentry to release
4771 * @dir: dir to release it from
4772 * @mds: mds that we're speaking to
4773 * @drop: caps being dropped
4774 * @unless: unless we have these caps
4776 * Encode a dentry release into an outgoing request buffer. Returns 1 if the
4777 * thing was released, or a negative error code otherwise.
4779 int ceph_encode_dentry_release(void **p, struct dentry *dentry,
4781 int mds, int drop, int unless)
4783 struct dentry *parent = NULL;
4784 struct ceph_mds_request_release *rel = *p;
4785 struct ceph_dentry_info *di = ceph_dentry(dentry);
4790 * force an record for the directory caps if we have a dentry lease.
4791 * this is racy (can't take i_ceph_lock and d_lock together), but it
4792 * doesn't have to be perfect; the mds will revoke anything we don't
4795 spin_lock(&dentry->d_lock);
4796 if (di->lease_session && di->lease_session->s_mds == mds)
4799 parent = dget(dentry->d_parent);
4800 dir = d_inode(parent);
4802 spin_unlock(&dentry->d_lock);
4804 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
4807 spin_lock(&dentry->d_lock);
4808 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
4809 dout("encode_dentry_release %p mds%d seq %d\n",
4810 dentry, mds, (int)di->lease_seq);
4811 rel->dname_seq = cpu_to_le32(di->lease_seq);
4812 __ceph_mdsc_drop_dentry_lease(dentry);
4813 spin_unlock(&dentry->d_lock);
4814 if (IS_ENCRYPTED(dir) && fscrypt_has_encryption_key(dir)) {
4815 int ret2 = ceph_encode_encrypted_fname(dir, dentry, *p);
4820 rel->dname_len = cpu_to_le32(ret2);
4823 rel->dname_len = cpu_to_le32(dentry->d_name.len);
4824 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
4825 *p += dentry->d_name.len;
4828 spin_unlock(&dentry->d_lock);
4833 static int remove_capsnaps(struct ceph_mds_client *mdsc, struct inode *inode)
4835 struct ceph_inode_info *ci = ceph_inode(inode);
4836 struct ceph_cap_snap *capsnap;
4837 int capsnap_release = 0;
4839 lockdep_assert_held(&ci->i_ceph_lock);
4841 dout("removing capsnaps, ci is %p, inode is %p\n", ci, inode);
4843 while (!list_empty(&ci->i_cap_snaps)) {
4844 capsnap = list_first_entry(&ci->i_cap_snaps,
4845 struct ceph_cap_snap, ci_item);
4846 __ceph_remove_capsnap(inode, capsnap, NULL, NULL);
4847 ceph_put_snap_context(capsnap->context);
4848 ceph_put_cap_snap(capsnap);
4851 wake_up_all(&ci->i_cap_wq);
4852 wake_up_all(&mdsc->cap_flushing_wq);
4853 return capsnap_release;
4856 int ceph_purge_inode_cap(struct inode *inode, struct ceph_cap *cap, bool *invalidate)
4858 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
4859 struct ceph_mds_client *mdsc = fsc->mdsc;
4860 struct ceph_inode_info *ci = ceph_inode(inode);
4862 bool dirty_dropped = false;
4865 lockdep_assert_held(&ci->i_ceph_lock);
4867 dout("removing cap %p, ci is %p, inode is %p\n",
4868 cap, ci, &ci->netfs.inode);
4870 is_auth = (cap == ci->i_auth_cap);
4871 __ceph_remove_cap(cap, false);
4873 struct ceph_cap_flush *cf;
4875 if (ceph_inode_is_shutdown(inode)) {
4876 if (inode->i_data.nrpages > 0)
4878 if (ci->i_wrbuffer_ref > 0)
4879 mapping_set_error(&inode->i_data, -EIO);
4882 spin_lock(&mdsc->cap_dirty_lock);
4884 /* trash all of the cap flushes for this inode */
4885 while (!list_empty(&ci->i_cap_flush_list)) {
4886 cf = list_first_entry(&ci->i_cap_flush_list,
4887 struct ceph_cap_flush, i_list);
4888 list_del_init(&cf->g_list);
4889 list_del_init(&cf->i_list);
4890 if (!cf->is_capsnap)
4891 ceph_free_cap_flush(cf);
4894 if (!list_empty(&ci->i_dirty_item)) {
4895 pr_warn_ratelimited(
4896 " dropping dirty %s state for %p %lld\n",
4897 ceph_cap_string(ci->i_dirty_caps),
4898 inode, ceph_ino(inode));
4899 ci->i_dirty_caps = 0;
4900 list_del_init(&ci->i_dirty_item);
4901 dirty_dropped = true;
4903 if (!list_empty(&ci->i_flushing_item)) {
4904 pr_warn_ratelimited(
4905 " dropping dirty+flushing %s state for %p %lld\n",
4906 ceph_cap_string(ci->i_flushing_caps),
4907 inode, ceph_ino(inode));
4908 ci->i_flushing_caps = 0;
4909 list_del_init(&ci->i_flushing_item);
4910 mdsc->num_cap_flushing--;
4911 dirty_dropped = true;
4913 spin_unlock(&mdsc->cap_dirty_lock);
4915 if (dirty_dropped) {
4916 mapping_set_error(inode->i_mapping, -EIO);
4918 if (ci->i_wrbuffer_ref_head == 0 &&
4919 ci->i_wr_ref == 0 &&
4920 ci->i_dirty_caps == 0 &&
4921 ci->i_flushing_caps == 0) {
4922 ceph_put_snap_context(ci->i_head_snapc);
4923 ci->i_head_snapc = NULL;
4927 if (atomic_read(&ci->i_filelock_ref) > 0) {
4928 /* make further file lock syscall return -EIO */
4929 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
4930 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
4931 inode, ceph_ino(inode));
4934 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
4935 cf = ci->i_prealloc_cap_flush;
4936 ci->i_prealloc_cap_flush = NULL;
4937 if (!cf->is_capsnap)
4938 ceph_free_cap_flush(cf);
4941 if (!list_empty(&ci->i_cap_snaps))
4942 iputs = remove_capsnaps(mdsc, inode);