1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
5 #include <linux/wait.h>
6 #include <linux/slab.h>
8 #include <linux/sched.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.h>
11 #include <linux/ratelimit.h>
12 #include <linux/bits.h>
13 #include <linux/ktime.h>
14 #include <linux/bitmap.h>
17 #include "mds_client.h"
19 #include <linux/ceph/ceph_features.h>
20 #include <linux/ceph/messenger.h>
21 #include <linux/ceph/decode.h>
22 #include <linux/ceph/pagelist.h>
23 #include <linux/ceph/auth.h>
24 #include <linux/ceph/debugfs.h>
26 #define RECONNECT_MAX_SIZE (INT_MAX - PAGE_SIZE)
29 * A cluster of MDS (metadata server) daemons is responsible for
30 * managing the file system namespace (the directory hierarchy and
31 * inodes) and for coordinating shared access to storage. Metadata is
32 * partitioning hierarchically across a number of servers, and that
33 * partition varies over time as the cluster adjusts the distribution
34 * in order to balance load.
36 * The MDS client is primarily responsible to managing synchronous
37 * metadata requests for operations like open, unlink, and so forth.
38 * If there is a MDS failure, we find out about it when we (possibly
39 * request and) receive a new MDS map, and can resubmit affected
42 * For the most part, though, we take advantage of a lossless
43 * communications channel to the MDS, and do not need to worry about
44 * timing out or resubmitting requests.
46 * We maintain a stateful "session" with each MDS we interact with.
47 * Within each session, we sent periodic heartbeat messages to ensure
48 * any capabilities or leases we have been issues remain valid. If
49 * the session times out and goes stale, our leases and capabilities
50 * are no longer valid.
53 struct ceph_reconnect_state {
54 struct ceph_mds_session *session;
55 int nr_caps, nr_realms;
56 struct ceph_pagelist *pagelist;
61 static void __wake_requests(struct ceph_mds_client *mdsc,
62 struct list_head *head);
63 static void ceph_cap_release_work(struct work_struct *work);
64 static void ceph_cap_reclaim_work(struct work_struct *work);
66 static const struct ceph_connection_operations mds_con_ops;
73 static int parse_reply_info_quota(void **p, void *end,
74 struct ceph_mds_reply_info_in *info)
76 u8 struct_v, struct_compat;
79 ceph_decode_8_safe(p, end, struct_v, bad);
80 ceph_decode_8_safe(p, end, struct_compat, bad);
81 /* struct_v is expected to be >= 1. we only
82 * understand encoding with struct_compat == 1. */
83 if (!struct_v || struct_compat != 1)
85 ceph_decode_32_safe(p, end, struct_len, bad);
86 ceph_decode_need(p, end, struct_len, bad);
87 end = *p + struct_len;
88 ceph_decode_64_safe(p, end, info->max_bytes, bad);
89 ceph_decode_64_safe(p, end, info->max_files, bad);
97 * parse individual inode info
99 static int parse_reply_info_in(void **p, void *end,
100 struct ceph_mds_reply_info_in *info,
106 if (features == (u64)-1) {
109 ceph_decode_8_safe(p, end, struct_v, bad);
110 ceph_decode_8_safe(p, end, struct_compat, bad);
111 /* struct_v is expected to be >= 1. we only understand
112 * encoding with struct_compat == 1. */
113 if (!struct_v || struct_compat != 1)
115 ceph_decode_32_safe(p, end, struct_len, bad);
116 ceph_decode_need(p, end, struct_len, bad);
117 end = *p + struct_len;
120 ceph_decode_need(p, end, sizeof(struct ceph_mds_reply_inode), bad);
122 *p += sizeof(struct ceph_mds_reply_inode) +
123 sizeof(*info->in->fragtree.splits) *
124 le32_to_cpu(info->in->fragtree.nsplits);
126 ceph_decode_32_safe(p, end, info->symlink_len, bad);
127 ceph_decode_need(p, end, info->symlink_len, bad);
129 *p += info->symlink_len;
131 ceph_decode_copy_safe(p, end, &info->dir_layout,
132 sizeof(info->dir_layout), bad);
133 ceph_decode_32_safe(p, end, info->xattr_len, bad);
134 ceph_decode_need(p, end, info->xattr_len, bad);
135 info->xattr_data = *p;
136 *p += info->xattr_len;
138 if (features == (u64)-1) {
140 ceph_decode_64_safe(p, end, info->inline_version, bad);
141 ceph_decode_32_safe(p, end, info->inline_len, bad);
142 ceph_decode_need(p, end, info->inline_len, bad);
143 info->inline_data = *p;
144 *p += info->inline_len;
146 err = parse_reply_info_quota(p, end, info);
150 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
151 if (info->pool_ns_len > 0) {
152 ceph_decode_need(p, end, info->pool_ns_len, bad);
153 info->pool_ns_data = *p;
154 *p += info->pool_ns_len;
158 ceph_decode_need(p, end, sizeof(info->btime), bad);
159 ceph_decode_copy(p, &info->btime, sizeof(info->btime));
161 /* change attribute */
162 ceph_decode_64_safe(p, end, info->change_attr, bad);
166 ceph_decode_32_safe(p, end, info->dir_pin, bad);
168 info->dir_pin = -ENODATA;
171 /* snapshot birth time, remains zero for v<=2 */
173 ceph_decode_need(p, end, sizeof(info->snap_btime), bad);
174 ceph_decode_copy(p, &info->snap_btime,
175 sizeof(info->snap_btime));
177 memset(&info->snap_btime, 0, sizeof(info->snap_btime));
180 /* snapshot count, remains zero for v<=3 */
182 ceph_decode_64_safe(p, end, info->rsnaps, bad);
189 if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
190 ceph_decode_64_safe(p, end, info->inline_version, bad);
191 ceph_decode_32_safe(p, end, info->inline_len, bad);
192 ceph_decode_need(p, end, info->inline_len, bad);
193 info->inline_data = *p;
194 *p += info->inline_len;
196 info->inline_version = CEPH_INLINE_NONE;
198 if (features & CEPH_FEATURE_MDS_QUOTA) {
199 err = parse_reply_info_quota(p, end, info);
207 info->pool_ns_len = 0;
208 info->pool_ns_data = NULL;
209 if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) {
210 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
211 if (info->pool_ns_len > 0) {
212 ceph_decode_need(p, end, info->pool_ns_len, bad);
213 info->pool_ns_data = *p;
214 *p += info->pool_ns_len;
218 if (features & CEPH_FEATURE_FS_BTIME) {
219 ceph_decode_need(p, end, sizeof(info->btime), bad);
220 ceph_decode_copy(p, &info->btime, sizeof(info->btime));
221 ceph_decode_64_safe(p, end, info->change_attr, bad);
224 info->dir_pin = -ENODATA;
225 /* info->snap_btime and info->rsnaps remain zero */
234 static int parse_reply_info_dir(void **p, void *end,
235 struct ceph_mds_reply_dirfrag **dirfrag,
238 if (features == (u64)-1) {
239 u8 struct_v, struct_compat;
241 ceph_decode_8_safe(p, end, struct_v, bad);
242 ceph_decode_8_safe(p, end, struct_compat, bad);
243 /* struct_v is expected to be >= 1. we only understand
244 * encoding whose struct_compat == 1. */
245 if (!struct_v || struct_compat != 1)
247 ceph_decode_32_safe(p, end, struct_len, bad);
248 ceph_decode_need(p, end, struct_len, bad);
249 end = *p + struct_len;
252 ceph_decode_need(p, end, sizeof(**dirfrag), bad);
254 *p += sizeof(**dirfrag) + sizeof(u32) * le32_to_cpu((*dirfrag)->ndist);
255 if (unlikely(*p > end))
257 if (features == (u64)-1)
264 static int parse_reply_info_lease(void **p, void *end,
265 struct ceph_mds_reply_lease **lease,
268 if (features == (u64)-1) {
269 u8 struct_v, struct_compat;
271 ceph_decode_8_safe(p, end, struct_v, bad);
272 ceph_decode_8_safe(p, end, struct_compat, bad);
273 /* struct_v is expected to be >= 1. we only understand
274 * encoding whose struct_compat == 1. */
275 if (!struct_v || struct_compat != 1)
277 ceph_decode_32_safe(p, end, struct_len, bad);
278 ceph_decode_need(p, end, struct_len, bad);
279 end = *p + struct_len;
282 ceph_decode_need(p, end, sizeof(**lease), bad);
284 *p += sizeof(**lease);
285 if (features == (u64)-1)
293 * parse a normal reply, which may contain a (dir+)dentry and/or a
296 static int parse_reply_info_trace(void **p, void *end,
297 struct ceph_mds_reply_info_parsed *info,
302 if (info->head->is_dentry) {
303 err = parse_reply_info_in(p, end, &info->diri, features);
307 err = parse_reply_info_dir(p, end, &info->dirfrag, features);
311 ceph_decode_32_safe(p, end, info->dname_len, bad);
312 ceph_decode_need(p, end, info->dname_len, bad);
314 *p += info->dname_len;
316 err = parse_reply_info_lease(p, end, &info->dlease, features);
321 if (info->head->is_target) {
322 err = parse_reply_info_in(p, end, &info->targeti, features);
327 if (unlikely(*p != end))
334 pr_err("problem parsing mds trace %d\n", err);
339 * parse readdir results
341 static int parse_reply_info_readdir(void **p, void *end,
342 struct ceph_mds_reply_info_parsed *info,
348 err = parse_reply_info_dir(p, end, &info->dir_dir, features);
352 ceph_decode_need(p, end, sizeof(num) + 2, bad);
353 num = ceph_decode_32(p);
355 u16 flags = ceph_decode_16(p);
356 info->dir_end = !!(flags & CEPH_READDIR_FRAG_END);
357 info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE);
358 info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER);
359 info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH);
364 BUG_ON(!info->dir_entries);
365 if ((unsigned long)(info->dir_entries + num) >
366 (unsigned long)info->dir_entries + info->dir_buf_size) {
367 pr_err("dir contents are larger than expected\n");
374 struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i;
376 ceph_decode_32_safe(p, end, rde->name_len, bad);
377 ceph_decode_need(p, end, rde->name_len, bad);
380 dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name);
383 err = parse_reply_info_lease(p, end, &rde->lease, features);
387 err = parse_reply_info_in(p, end, &rde->inode, features);
390 /* ceph_readdir_prepopulate() will update it */
397 /* Skip over any unrecognized fields */
404 pr_err("problem parsing dir contents %d\n", err);
409 * parse fcntl F_GETLK results
411 static int parse_reply_info_filelock(void **p, void *end,
412 struct ceph_mds_reply_info_parsed *info,
415 if (*p + sizeof(*info->filelock_reply) > end)
418 info->filelock_reply = *p;
420 /* Skip over any unrecognized fields */
428 #if BITS_PER_LONG == 64
430 #define DELEGATED_INO_AVAILABLE xa_mk_value(1)
432 static int ceph_parse_deleg_inos(void **p, void *end,
433 struct ceph_mds_session *s)
437 ceph_decode_32_safe(p, end, sets, bad);
438 dout("got %u sets of delegated inodes\n", sets);
442 ceph_decode_64_safe(p, end, start, bad);
443 ceph_decode_64_safe(p, end, len, bad);
445 /* Don't accept a delegation of system inodes */
446 if (start < CEPH_INO_SYSTEM_BASE) {
447 pr_warn_ratelimited("ceph: ignoring reserved inode range delegation (start=0x%llx len=0x%llx)\n",
452 int err = xa_insert(&s->s_delegated_inos, start++,
453 DELEGATED_INO_AVAILABLE,
456 dout("added delegated inode 0x%llx\n",
458 } else if (err == -EBUSY) {
459 pr_warn("MDS delegated inode 0x%llx more than once.\n",
471 u64 ceph_get_deleg_ino(struct ceph_mds_session *s)
476 xa_for_each(&s->s_delegated_inos, ino, val) {
477 val = xa_erase(&s->s_delegated_inos, ino);
478 if (val == DELEGATED_INO_AVAILABLE)
484 int ceph_restore_deleg_ino(struct ceph_mds_session *s, u64 ino)
486 return xa_insert(&s->s_delegated_inos, ino, DELEGATED_INO_AVAILABLE,
489 #else /* BITS_PER_LONG == 64 */
491 * FIXME: xarrays can't handle 64-bit indexes on a 32-bit arch. For now, just
492 * ignore delegated_inos on 32 bit arch. Maybe eventually add xarrays for top
495 static int ceph_parse_deleg_inos(void **p, void *end,
496 struct ceph_mds_session *s)
500 ceph_decode_32_safe(p, end, sets, bad);
502 ceph_decode_skip_n(p, end, sets * 2 * sizeof(__le64), bad);
508 u64 ceph_get_deleg_ino(struct ceph_mds_session *s)
513 int ceph_restore_deleg_ino(struct ceph_mds_session *s, u64 ino)
517 #endif /* BITS_PER_LONG == 64 */
520 * parse create results
522 static int parse_reply_info_create(void **p, void *end,
523 struct ceph_mds_reply_info_parsed *info,
524 u64 features, struct ceph_mds_session *s)
528 if (features == (u64)-1 ||
529 (features & CEPH_FEATURE_REPLY_CREATE_INODE)) {
531 /* Malformed reply? */
532 info->has_create_ino = false;
533 } else if (test_bit(CEPHFS_FEATURE_DELEG_INO, &s->s_features)) {
534 info->has_create_ino = true;
535 /* struct_v, struct_compat, and len */
536 ceph_decode_skip_n(p, end, 2 + sizeof(u32), bad);
537 ceph_decode_64_safe(p, end, info->ino, bad);
538 ret = ceph_parse_deleg_inos(p, end, s);
543 ceph_decode_64_safe(p, end, info->ino, bad);
544 info->has_create_ino = true;
551 /* Skip over any unrecognized fields */
558 static int parse_reply_info_getvxattr(void **p, void *end,
559 struct ceph_mds_reply_info_parsed *info,
564 ceph_decode_skip_8(p, end, bad); /* skip current version: 1 */
565 ceph_decode_skip_8(p, end, bad); /* skip first version: 1 */
566 ceph_decode_skip_32(p, end, bad); /* skip payload length */
568 ceph_decode_32_safe(p, end, value_len, bad);
570 if (value_len == end - *p) {
571 info->xattr_info.xattr_value = *p;
572 info->xattr_info.xattr_value_len = value_len;
581 * parse extra results
583 static int parse_reply_info_extra(void **p, void *end,
584 struct ceph_mds_reply_info_parsed *info,
585 u64 features, struct ceph_mds_session *s)
587 u32 op = le32_to_cpu(info->head->op);
589 if (op == CEPH_MDS_OP_GETFILELOCK)
590 return parse_reply_info_filelock(p, end, info, features);
591 else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
592 return parse_reply_info_readdir(p, end, info, features);
593 else if (op == CEPH_MDS_OP_CREATE)
594 return parse_reply_info_create(p, end, info, features, s);
595 else if (op == CEPH_MDS_OP_GETVXATTR)
596 return parse_reply_info_getvxattr(p, end, info, features);
602 * parse entire mds reply
604 static int parse_reply_info(struct ceph_mds_session *s, struct ceph_msg *msg,
605 struct ceph_mds_reply_info_parsed *info,
612 info->head = msg->front.iov_base;
613 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
614 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
617 ceph_decode_32_safe(&p, end, len, bad);
619 ceph_decode_need(&p, end, len, bad);
620 err = parse_reply_info_trace(&p, p+len, info, features);
626 ceph_decode_32_safe(&p, end, len, bad);
628 ceph_decode_need(&p, end, len, bad);
629 err = parse_reply_info_extra(&p, p+len, info, features, s);
635 ceph_decode_32_safe(&p, end, len, bad);
636 info->snapblob_len = len;
647 pr_err("mds parse_reply err %d\n", err);
651 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
653 if (!info->dir_entries)
655 free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
659 * In async unlink case the kclient won't wait for the first reply
660 * from MDS and just drop all the links and unhash the dentry and then
661 * succeeds immediately.
663 * For any new create/link/rename,etc requests followed by using the
664 * same file names we must wait for the first reply of the inflight
665 * unlink request, or the MDS possibly will fail these following
666 * requests with -EEXIST if the inflight async unlink request was
667 * delayed for some reasons.
669 * And the worst case is that for the none async openc request it will
670 * successfully open the file if the CDentry hasn't been unlinked yet,
671 * but later the previous delayed async unlink request will remove the
672 * CDenty. That means the just created file is possiblly deleted later
675 * We need to wait for the inflight async unlink requests to finish
676 * when creating new files/directories by using the same file names.
678 int ceph_wait_on_conflict_unlink(struct dentry *dentry)
680 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
681 struct dentry *pdentry = dentry->d_parent;
682 struct dentry *udentry, *found = NULL;
683 struct ceph_dentry_info *di;
685 u32 hash = dentry->d_name.hash;
688 dname.name = dentry->d_name.name;
689 dname.len = dentry->d_name.len;
692 hash_for_each_possible_rcu(fsc->async_unlink_conflict, di,
694 udentry = di->dentry;
696 spin_lock(&udentry->d_lock);
697 if (udentry->d_name.hash != hash)
699 if (unlikely(udentry->d_parent != pdentry))
701 if (!hash_hashed(&di->hnode))
704 if (!test_bit(CEPH_DENTRY_ASYNC_UNLINK_BIT, &di->flags))
705 pr_warn("%s dentry %p:%pd async unlink bit is not set\n",
706 __func__, dentry, dentry);
708 if (!d_same_name(udentry, pdentry, &dname))
711 spin_unlock(&udentry->d_lock);
712 found = dget(udentry);
715 spin_unlock(&udentry->d_lock);
722 dout("%s dentry %p:%pd conflict with old %p:%pd\n", __func__,
723 dentry, dentry, found, found);
725 err = wait_on_bit(&di->flags, CEPH_DENTRY_ASYNC_UNLINK_BIT,
735 const char *ceph_session_state_name(int s)
738 case CEPH_MDS_SESSION_NEW: return "new";
739 case CEPH_MDS_SESSION_OPENING: return "opening";
740 case CEPH_MDS_SESSION_OPEN: return "open";
741 case CEPH_MDS_SESSION_HUNG: return "hung";
742 case CEPH_MDS_SESSION_CLOSING: return "closing";
743 case CEPH_MDS_SESSION_CLOSED: return "closed";
744 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
745 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
746 case CEPH_MDS_SESSION_REJECTED: return "rejected";
747 default: return "???";
751 struct ceph_mds_session *ceph_get_mds_session(struct ceph_mds_session *s)
753 if (refcount_inc_not_zero(&s->s_ref))
758 void ceph_put_mds_session(struct ceph_mds_session *s)
760 if (IS_ERR_OR_NULL(s))
763 if (refcount_dec_and_test(&s->s_ref)) {
764 if (s->s_auth.authorizer)
765 ceph_auth_destroy_authorizer(s->s_auth.authorizer);
766 WARN_ON(mutex_is_locked(&s->s_mutex));
767 xa_destroy(&s->s_delegated_inos);
773 * called under mdsc->mutex
775 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
778 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
780 return ceph_get_mds_session(mdsc->sessions[mds]);
783 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
785 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
791 static int __verify_registered_session(struct ceph_mds_client *mdsc,
792 struct ceph_mds_session *s)
794 if (s->s_mds >= mdsc->max_sessions ||
795 mdsc->sessions[s->s_mds] != s)
801 * create+register a new session for given mds.
802 * called under mdsc->mutex.
804 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
807 struct ceph_mds_session *s;
809 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_FENCE_IO)
810 return ERR_PTR(-EIO);
812 if (mds >= mdsc->mdsmap->possible_max_rank)
813 return ERR_PTR(-EINVAL);
815 s = kzalloc(sizeof(*s), GFP_NOFS);
817 return ERR_PTR(-ENOMEM);
819 if (mds >= mdsc->max_sessions) {
820 int newmax = 1 << get_count_order(mds + 1);
821 struct ceph_mds_session **sa;
823 dout("%s: realloc to %d\n", __func__, newmax);
824 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
827 if (mdsc->sessions) {
828 memcpy(sa, mdsc->sessions,
829 mdsc->max_sessions * sizeof(void *));
830 kfree(mdsc->sessions);
833 mdsc->max_sessions = newmax;
836 dout("%s: mds%d\n", __func__, mds);
839 s->s_state = CEPH_MDS_SESSION_NEW;
840 mutex_init(&s->s_mutex);
842 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
844 atomic_set(&s->s_cap_gen, 1);
845 s->s_cap_ttl = jiffies - 1;
847 spin_lock_init(&s->s_cap_lock);
848 INIT_LIST_HEAD(&s->s_caps);
849 refcount_set(&s->s_ref, 1);
850 INIT_LIST_HEAD(&s->s_waiting);
851 INIT_LIST_HEAD(&s->s_unsafe);
852 xa_init(&s->s_delegated_inos);
853 INIT_LIST_HEAD(&s->s_cap_releases);
854 INIT_WORK(&s->s_cap_release_work, ceph_cap_release_work);
856 INIT_LIST_HEAD(&s->s_cap_dirty);
857 INIT_LIST_HEAD(&s->s_cap_flushing);
859 mdsc->sessions[mds] = s;
860 atomic_inc(&mdsc->num_sessions);
861 refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
863 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
864 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
870 return ERR_PTR(-ENOMEM);
874 * called under mdsc->mutex
876 static void __unregister_session(struct ceph_mds_client *mdsc,
877 struct ceph_mds_session *s)
879 dout("__unregister_session mds%d %p\n", s->s_mds, s);
880 BUG_ON(mdsc->sessions[s->s_mds] != s);
881 mdsc->sessions[s->s_mds] = NULL;
882 ceph_con_close(&s->s_con);
883 ceph_put_mds_session(s);
884 atomic_dec(&mdsc->num_sessions);
888 * drop session refs in request.
890 * should be last request ref, or hold mdsc->mutex
892 static void put_request_session(struct ceph_mds_request *req)
894 if (req->r_session) {
895 ceph_put_mds_session(req->r_session);
896 req->r_session = NULL;
900 void ceph_mdsc_iterate_sessions(struct ceph_mds_client *mdsc,
901 void (*cb)(struct ceph_mds_session *),
906 mutex_lock(&mdsc->mutex);
907 for (mds = 0; mds < mdsc->max_sessions; ++mds) {
908 struct ceph_mds_session *s;
910 s = __ceph_lookup_mds_session(mdsc, mds);
914 if (check_state && !check_session_state(s)) {
915 ceph_put_mds_session(s);
919 mutex_unlock(&mdsc->mutex);
921 ceph_put_mds_session(s);
922 mutex_lock(&mdsc->mutex);
924 mutex_unlock(&mdsc->mutex);
927 void ceph_mdsc_release_request(struct kref *kref)
929 struct ceph_mds_request *req = container_of(kref,
930 struct ceph_mds_request,
932 ceph_mdsc_release_dir_caps_no_check(req);
933 destroy_reply_info(&req->r_reply_info);
935 ceph_msg_put(req->r_request);
937 ceph_msg_put(req->r_reply);
939 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
943 ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
946 iput(req->r_target_inode);
949 if (req->r_old_dentry)
950 dput(req->r_old_dentry);
951 if (req->r_old_dentry_dir) {
953 * track (and drop pins for) r_old_dentry_dir
954 * separately, since r_old_dentry's d_parent may have
955 * changed between the dir mutex being dropped and
956 * this request being freed.
958 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
960 iput(req->r_old_dentry_dir);
964 put_cred(req->r_cred);
966 ceph_pagelist_release(req->r_pagelist);
967 put_request_session(req);
968 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
969 WARN_ON_ONCE(!list_empty(&req->r_wait));
970 kmem_cache_free(ceph_mds_request_cachep, req);
973 DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
976 * lookup session, bump ref if found.
978 * called under mdsc->mutex.
980 static struct ceph_mds_request *
981 lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
983 struct ceph_mds_request *req;
985 req = lookup_request(&mdsc->request_tree, tid);
987 ceph_mdsc_get_request(req);
993 * Register an in-flight request, and assign a tid. Link to directory
994 * are modifying (if any).
996 * Called under mdsc->mutex.
998 static void __register_request(struct ceph_mds_client *mdsc,
999 struct ceph_mds_request *req,
1004 req->r_tid = ++mdsc->last_tid;
1005 if (req->r_num_caps) {
1006 ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
1009 pr_err("__register_request %p "
1010 "failed to reserve caps: %d\n", req, ret);
1011 /* set req->r_err to fail early from __do_request */
1016 dout("__register_request %p tid %lld\n", req, req->r_tid);
1017 ceph_mdsc_get_request(req);
1018 insert_request(&mdsc->request_tree, req);
1020 req->r_cred = get_current_cred();
1022 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
1023 mdsc->oldest_tid = req->r_tid;
1026 struct ceph_inode_info *ci = ceph_inode(dir);
1029 req->r_unsafe_dir = dir;
1030 spin_lock(&ci->i_unsafe_lock);
1031 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
1032 spin_unlock(&ci->i_unsafe_lock);
1036 static void __unregister_request(struct ceph_mds_client *mdsc,
1037 struct ceph_mds_request *req)
1039 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1041 /* Never leave an unregistered request on an unsafe list! */
1042 list_del_init(&req->r_unsafe_item);
1044 if (req->r_tid == mdsc->oldest_tid) {
1045 struct rb_node *p = rb_next(&req->r_node);
1046 mdsc->oldest_tid = 0;
1048 struct ceph_mds_request *next_req =
1049 rb_entry(p, struct ceph_mds_request, r_node);
1050 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
1051 mdsc->oldest_tid = next_req->r_tid;
1058 erase_request(&mdsc->request_tree, req);
1060 if (req->r_unsafe_dir) {
1061 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
1062 spin_lock(&ci->i_unsafe_lock);
1063 list_del_init(&req->r_unsafe_dir_item);
1064 spin_unlock(&ci->i_unsafe_lock);
1066 if (req->r_target_inode &&
1067 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
1068 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
1069 spin_lock(&ci->i_unsafe_lock);
1070 list_del_init(&req->r_unsafe_target_item);
1071 spin_unlock(&ci->i_unsafe_lock);
1074 if (req->r_unsafe_dir) {
1075 iput(req->r_unsafe_dir);
1076 req->r_unsafe_dir = NULL;
1079 complete_all(&req->r_safe_completion);
1081 ceph_mdsc_put_request(req);
1085 * Walk back up the dentry tree until we hit a dentry representing a
1086 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
1087 * when calling this) to ensure that the objects won't disappear while we're
1088 * working with them. Once we hit a candidate dentry, we attempt to take a
1089 * reference to it, and return that as the result.
1091 static struct inode *get_nonsnap_parent(struct dentry *dentry)
1093 struct inode *inode = NULL;
1095 while (dentry && !IS_ROOT(dentry)) {
1096 inode = d_inode_rcu(dentry);
1097 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
1099 dentry = dentry->d_parent;
1102 inode = igrab(inode);
1107 * Choose mds to send request to next. If there is a hint set in the
1108 * request (e.g., due to a prior forward hint from the mds), use that.
1109 * Otherwise, consult frag tree and/or caps to identify the
1110 * appropriate mds. If all else fails, choose randomly.
1112 * Called under mdsc->mutex.
1114 static int __choose_mds(struct ceph_mds_client *mdsc,
1115 struct ceph_mds_request *req,
1118 struct inode *inode;
1119 struct ceph_inode_info *ci;
1120 struct ceph_cap *cap;
1121 int mode = req->r_direct_mode;
1123 u32 hash = req->r_direct_hash;
1124 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
1130 * is there a specific mds we should try? ignore hint if we have
1131 * no session and the mds is not up (active or recovering).
1133 if (req->r_resend_mds >= 0 &&
1134 (__have_session(mdsc, req->r_resend_mds) ||
1135 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
1136 dout("%s using resend_mds mds%d\n", __func__,
1138 return req->r_resend_mds;
1141 if (mode == USE_RANDOM_MDS)
1146 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
1147 inode = req->r_inode;
1150 /* req->r_dentry is non-null for LSSNAP request */
1152 inode = get_nonsnap_parent(req->r_dentry);
1154 dout("%s using snapdir's parent %p\n", __func__, inode);
1156 } else if (req->r_dentry) {
1157 /* ignore race with rename; old or new d_parent is okay */
1158 struct dentry *parent;
1162 parent = READ_ONCE(req->r_dentry->d_parent);
1163 dir = req->r_parent ? : d_inode_rcu(parent);
1165 if (!dir || dir->i_sb != mdsc->fsc->sb) {
1166 /* not this fs or parent went negative */
1167 inode = d_inode(req->r_dentry);
1170 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
1171 /* direct snapped/virtual snapdir requests
1172 * based on parent dir inode */
1173 inode = get_nonsnap_parent(parent);
1174 dout("%s using nonsnap parent %p\n", __func__, inode);
1177 inode = d_inode(req->r_dentry);
1178 if (!inode || mode == USE_AUTH_MDS) {
1181 hash = ceph_dentry_hash(dir, req->r_dentry);
1190 dout("%s %p is_hash=%d (0x%x) mode %d\n", __func__, inode, (int)is_hash,
1194 ci = ceph_inode(inode);
1196 if (is_hash && S_ISDIR(inode->i_mode)) {
1197 struct ceph_inode_frag frag;
1200 ceph_choose_frag(ci, hash, &frag, &found);
1202 if (mode == USE_ANY_MDS && frag.ndist > 0) {
1205 /* choose a random replica */
1206 get_random_bytes(&r, 1);
1209 dout("%s %p %llx.%llx frag %u mds%d (%d/%d)\n",
1210 __func__, inode, ceph_vinop(inode),
1211 frag.frag, mds, (int)r, frag.ndist);
1212 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
1213 CEPH_MDS_STATE_ACTIVE &&
1214 !ceph_mdsmap_is_laggy(mdsc->mdsmap, mds))
1218 /* since this file/dir wasn't known to be
1219 * replicated, then we want to look for the
1220 * authoritative mds. */
1221 if (frag.mds >= 0) {
1222 /* choose auth mds */
1224 dout("%s %p %llx.%llx frag %u mds%d (auth)\n",
1225 __func__, inode, ceph_vinop(inode),
1227 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
1228 CEPH_MDS_STATE_ACTIVE) {
1229 if (!ceph_mdsmap_is_laggy(mdsc->mdsmap,
1234 mode = USE_AUTH_MDS;
1238 spin_lock(&ci->i_ceph_lock);
1240 if (mode == USE_AUTH_MDS)
1241 cap = ci->i_auth_cap;
1242 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
1243 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
1245 spin_unlock(&ci->i_ceph_lock);
1249 mds = cap->session->s_mds;
1250 dout("%s %p %llx.%llx mds%d (%scap %p)\n", __func__,
1251 inode, ceph_vinop(inode), mds,
1252 cap == ci->i_auth_cap ? "auth " : "", cap);
1253 spin_unlock(&ci->i_ceph_lock);
1262 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
1263 dout("%s chose random mds%d\n", __func__, mds);
1271 struct ceph_msg *ceph_create_session_msg(u32 op, u64 seq)
1273 struct ceph_msg *msg;
1274 struct ceph_mds_session_head *h;
1276 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
1279 pr_err("ENOMEM creating session %s msg\n",
1280 ceph_session_op_name(op));
1283 h = msg->front.iov_base;
1284 h->op = cpu_to_le32(op);
1285 h->seq = cpu_to_le64(seq);
1290 static const unsigned char feature_bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
1291 #define FEATURE_BYTES(c) (DIV_ROUND_UP((size_t)feature_bits[c - 1] + 1, 64) * 8)
1292 static int encode_supported_features(void **p, void *end)
1294 static const size_t count = ARRAY_SIZE(feature_bits);
1298 size_t size = FEATURE_BYTES(count);
1301 if (WARN_ON_ONCE(*p + 4 + size > end))
1304 ceph_encode_32(p, size);
1305 memset(*p, 0, size);
1306 for (i = 0; i < count; i++) {
1307 bit = feature_bits[i];
1308 ((unsigned char *)(*p))[bit / 8] |= BIT(bit % 8);
1312 if (WARN_ON_ONCE(*p + 4 > end))
1315 ceph_encode_32(p, 0);
1321 static const unsigned char metric_bits[] = CEPHFS_METRIC_SPEC_CLIENT_SUPPORTED;
1322 #define METRIC_BYTES(cnt) (DIV_ROUND_UP((size_t)metric_bits[cnt - 1] + 1, 64) * 8)
1323 static int encode_metric_spec(void **p, void *end)
1325 static const size_t count = ARRAY_SIZE(metric_bits);
1328 if (WARN_ON_ONCE(*p + 2 > end))
1331 ceph_encode_8(p, 1); /* version */
1332 ceph_encode_8(p, 1); /* compat */
1336 size_t size = METRIC_BYTES(count);
1338 if (WARN_ON_ONCE(*p + 4 + 4 + size > end))
1341 /* metric spec info length */
1342 ceph_encode_32(p, 4 + size);
1345 ceph_encode_32(p, size);
1346 memset(*p, 0, size);
1347 for (i = 0; i < count; i++)
1348 ((unsigned char *)(*p))[i / 8] |= BIT(metric_bits[i] % 8);
1351 if (WARN_ON_ONCE(*p + 4 + 4 > end))
1354 /* metric spec info length */
1355 ceph_encode_32(p, 4);
1357 ceph_encode_32(p, 0);
1364 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
1365 * to include additional client metadata fields.
1367 static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
1369 struct ceph_msg *msg;
1370 struct ceph_mds_session_head *h;
1372 int extra_bytes = 0;
1373 int metadata_key_count = 0;
1374 struct ceph_options *opt = mdsc->fsc->client->options;
1375 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
1380 const char* metadata[][2] = {
1381 {"hostname", mdsc->nodename},
1382 {"kernel_version", init_utsname()->release},
1383 {"entity_id", opt->name ? : ""},
1384 {"root", fsopt->server_path ? : "/"},
1388 /* Calculate serialized length of metadata */
1389 extra_bytes = 4; /* map length */
1390 for (i = 0; metadata[i][0]; ++i) {
1391 extra_bytes += 8 + strlen(metadata[i][0]) +
1392 strlen(metadata[i][1]);
1393 metadata_key_count++;
1396 /* supported feature */
1398 count = ARRAY_SIZE(feature_bits);
1400 size = FEATURE_BYTES(count);
1401 extra_bytes += 4 + size;
1405 count = ARRAY_SIZE(metric_bits);
1407 size = METRIC_BYTES(count);
1408 extra_bytes += 2 + 4 + 4 + size;
1410 /* Allocate the message */
1411 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
1414 pr_err("ENOMEM creating session open msg\n");
1415 return ERR_PTR(-ENOMEM);
1417 p = msg->front.iov_base;
1418 end = p + msg->front.iov_len;
1421 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
1422 h->seq = cpu_to_le64(seq);
1425 * Serialize client metadata into waiting buffer space, using
1426 * the format that userspace expects for map<string, string>
1428 * ClientSession messages with metadata are v4
1430 msg->hdr.version = cpu_to_le16(4);
1431 msg->hdr.compat_version = cpu_to_le16(1);
1433 /* The write pointer, following the session_head structure */
1436 /* Number of entries in the map */
1437 ceph_encode_32(&p, metadata_key_count);
1439 /* Two length-prefixed strings for each entry in the map */
1440 for (i = 0; metadata[i][0]; ++i) {
1441 size_t const key_len = strlen(metadata[i][0]);
1442 size_t const val_len = strlen(metadata[i][1]);
1444 ceph_encode_32(&p, key_len);
1445 memcpy(p, metadata[i][0], key_len);
1447 ceph_encode_32(&p, val_len);
1448 memcpy(p, metadata[i][1], val_len);
1452 ret = encode_supported_features(&p, end);
1454 pr_err("encode_supported_features failed!\n");
1456 return ERR_PTR(ret);
1459 ret = encode_metric_spec(&p, end);
1461 pr_err("encode_metric_spec failed!\n");
1463 return ERR_PTR(ret);
1466 msg->front.iov_len = p - msg->front.iov_base;
1467 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1473 * send session open request.
1475 * called under mdsc->mutex
1477 static int __open_session(struct ceph_mds_client *mdsc,
1478 struct ceph_mds_session *session)
1480 struct ceph_msg *msg;
1482 int mds = session->s_mds;
1484 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_FENCE_IO)
1487 /* wait for mds to go active? */
1488 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
1489 dout("open_session to mds%d (%s)\n", mds,
1490 ceph_mds_state_name(mstate));
1491 session->s_state = CEPH_MDS_SESSION_OPENING;
1492 session->s_renew_requested = jiffies;
1494 /* send connect message */
1495 msg = create_session_open_msg(mdsc, session->s_seq);
1497 return PTR_ERR(msg);
1498 ceph_con_send(&session->s_con, msg);
1503 * open sessions for any export targets for the given mds
1505 * called under mdsc->mutex
1507 static struct ceph_mds_session *
1508 __open_export_target_session(struct ceph_mds_client *mdsc, int target)
1510 struct ceph_mds_session *session;
1513 session = __ceph_lookup_mds_session(mdsc, target);
1515 session = register_session(mdsc, target);
1516 if (IS_ERR(session))
1519 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1520 session->s_state == CEPH_MDS_SESSION_CLOSING) {
1521 ret = __open_session(mdsc, session);
1523 return ERR_PTR(ret);
1529 struct ceph_mds_session *
1530 ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1532 struct ceph_mds_session *session;
1534 dout("open_export_target_session to mds%d\n", target);
1536 mutex_lock(&mdsc->mutex);
1537 session = __open_export_target_session(mdsc, target);
1538 mutex_unlock(&mdsc->mutex);
1543 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1544 struct ceph_mds_session *session)
1546 struct ceph_mds_info *mi;
1547 struct ceph_mds_session *ts;
1548 int i, mds = session->s_mds;
1550 if (mds >= mdsc->mdsmap->possible_max_rank)
1553 mi = &mdsc->mdsmap->m_info[mds];
1554 dout("open_export_target_sessions for mds%d (%d targets)\n",
1555 session->s_mds, mi->num_export_targets);
1557 for (i = 0; i < mi->num_export_targets; i++) {
1558 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1559 ceph_put_mds_session(ts);
1563 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1564 struct ceph_mds_session *session)
1566 mutex_lock(&mdsc->mutex);
1567 __open_export_target_sessions(mdsc, session);
1568 mutex_unlock(&mdsc->mutex);
1575 static void detach_cap_releases(struct ceph_mds_session *session,
1576 struct list_head *target)
1578 lockdep_assert_held(&session->s_cap_lock);
1580 list_splice_init(&session->s_cap_releases, target);
1581 session->s_num_cap_releases = 0;
1582 dout("dispose_cap_releases mds%d\n", session->s_mds);
1585 static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1586 struct list_head *dispose)
1588 while (!list_empty(dispose)) {
1589 struct ceph_cap *cap;
1590 /* zero out the in-progress message */
1591 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
1592 list_del(&cap->session_caps);
1593 ceph_put_cap(mdsc, cap);
1597 static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1598 struct ceph_mds_session *session)
1600 struct ceph_mds_request *req;
1603 dout("cleanup_session_requests mds%d\n", session->s_mds);
1604 mutex_lock(&mdsc->mutex);
1605 while (!list_empty(&session->s_unsafe)) {
1606 req = list_first_entry(&session->s_unsafe,
1607 struct ceph_mds_request, r_unsafe_item);
1608 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1610 if (req->r_target_inode)
1611 mapping_set_error(req->r_target_inode->i_mapping, -EIO);
1612 if (req->r_unsafe_dir)
1613 mapping_set_error(req->r_unsafe_dir->i_mapping, -EIO);
1614 __unregister_request(mdsc, req);
1616 /* zero r_attempts, so kick_requests() will re-send requests */
1617 p = rb_first(&mdsc->request_tree);
1619 req = rb_entry(p, struct ceph_mds_request, r_node);
1621 if (req->r_session &&
1622 req->r_session->s_mds == session->s_mds)
1623 req->r_attempts = 0;
1625 mutex_unlock(&mdsc->mutex);
1629 * Helper to safely iterate over all caps associated with a session, with
1630 * special care taken to handle a racing __ceph_remove_cap().
1632 * Caller must hold session s_mutex.
1634 int ceph_iterate_session_caps(struct ceph_mds_session *session,
1635 int (*cb)(struct inode *, int mds, void *),
1638 struct list_head *p;
1639 struct ceph_cap *cap;
1640 struct inode *inode, *last_inode = NULL;
1641 struct ceph_cap *old_cap = NULL;
1644 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1645 spin_lock(&session->s_cap_lock);
1646 p = session->s_caps.next;
1647 while (p != &session->s_caps) {
1650 cap = list_entry(p, struct ceph_cap, session_caps);
1651 inode = igrab(&cap->ci->netfs.inode);
1656 session->s_cap_iterator = cap;
1658 spin_unlock(&session->s_cap_lock);
1665 ceph_put_cap(session->s_mdsc, old_cap);
1669 ret = cb(inode, mds, arg);
1672 spin_lock(&session->s_cap_lock);
1675 dout("iterate_session_caps finishing cap %p removal\n",
1677 BUG_ON(cap->session != session);
1678 cap->session = NULL;
1679 list_del_init(&cap->session_caps);
1680 session->s_nr_caps--;
1681 atomic64_dec(&session->s_mdsc->metric.total_caps);
1682 if (cap->queue_release)
1683 __ceph_queue_cap_release(session, cap);
1685 old_cap = cap; /* put_cap it w/o locks held */
1692 session->s_cap_iterator = NULL;
1693 spin_unlock(&session->s_cap_lock);
1697 ceph_put_cap(session->s_mdsc, old_cap);
1702 static int remove_session_caps_cb(struct inode *inode, int mds, void *arg)
1704 struct ceph_inode_info *ci = ceph_inode(inode);
1705 bool invalidate = false;
1706 struct ceph_cap *cap;
1709 spin_lock(&ci->i_ceph_lock);
1710 cap = __get_cap_for_mds(ci, mds);
1712 dout(" removing cap %p, ci is %p, inode is %p\n",
1713 cap, ci, &ci->netfs.inode);
1715 iputs = ceph_purge_inode_cap(inode, cap, &invalidate);
1717 spin_unlock(&ci->i_ceph_lock);
1720 wake_up_all(&ci->i_cap_wq);
1722 ceph_queue_invalidate(inode);
1729 * caller must hold session s_mutex
1731 static void remove_session_caps(struct ceph_mds_session *session)
1733 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1734 struct super_block *sb = fsc->sb;
1737 dout("remove_session_caps on %p\n", session);
1738 ceph_iterate_session_caps(session, remove_session_caps_cb, fsc);
1740 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1742 spin_lock(&session->s_cap_lock);
1743 if (session->s_nr_caps > 0) {
1744 struct inode *inode;
1745 struct ceph_cap *cap, *prev = NULL;
1746 struct ceph_vino vino;
1748 * iterate_session_caps() skips inodes that are being
1749 * deleted, we need to wait until deletions are complete.
1750 * __wait_on_freeing_inode() is designed for the job,
1751 * but it is not exported, so use lookup inode function
1754 while (!list_empty(&session->s_caps)) {
1755 cap = list_entry(session->s_caps.next,
1756 struct ceph_cap, session_caps);
1760 vino = cap->ci->i_vino;
1761 spin_unlock(&session->s_cap_lock);
1763 inode = ceph_find_inode(sb, vino);
1766 spin_lock(&session->s_cap_lock);
1770 // drop cap expires and unlock s_cap_lock
1771 detach_cap_releases(session, &dispose);
1773 BUG_ON(session->s_nr_caps > 0);
1774 BUG_ON(!list_empty(&session->s_cap_flushing));
1775 spin_unlock(&session->s_cap_lock);
1776 dispose_cap_releases(session->s_mdsc, &dispose);
1786 * wake up any threads waiting on this session's caps. if the cap is
1787 * old (didn't get renewed on the client reconnect), remove it now.
1789 * caller must hold s_mutex.
1791 static int wake_up_session_cb(struct inode *inode, int mds, void *arg)
1793 struct ceph_inode_info *ci = ceph_inode(inode);
1794 unsigned long ev = (unsigned long)arg;
1796 if (ev == RECONNECT) {
1797 spin_lock(&ci->i_ceph_lock);
1798 ci->i_wanted_max_size = 0;
1799 ci->i_requested_max_size = 0;
1800 spin_unlock(&ci->i_ceph_lock);
1801 } else if (ev == RENEWCAPS) {
1802 struct ceph_cap *cap;
1804 spin_lock(&ci->i_ceph_lock);
1805 cap = __get_cap_for_mds(ci, mds);
1806 /* mds did not re-issue stale cap */
1807 if (cap && cap->cap_gen < atomic_read(&cap->session->s_cap_gen))
1808 cap->issued = cap->implemented = CEPH_CAP_PIN;
1809 spin_unlock(&ci->i_ceph_lock);
1810 } else if (ev == FORCE_RO) {
1812 wake_up_all(&ci->i_cap_wq);
1816 static void wake_up_session_caps(struct ceph_mds_session *session, int ev)
1818 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1819 ceph_iterate_session_caps(session, wake_up_session_cb,
1820 (void *)(unsigned long)ev);
1824 * Send periodic message to MDS renewing all currently held caps. The
1825 * ack will reset the expiration for all caps from this session.
1827 * caller holds s_mutex
1829 static int send_renew_caps(struct ceph_mds_client *mdsc,
1830 struct ceph_mds_session *session)
1832 struct ceph_msg *msg;
1835 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1836 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1837 pr_info("mds%d caps stale\n", session->s_mds);
1838 session->s_renew_requested = jiffies;
1840 /* do not try to renew caps until a recovering mds has reconnected
1841 * with its clients. */
1842 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1843 if (state < CEPH_MDS_STATE_RECONNECT) {
1844 dout("send_renew_caps ignoring mds%d (%s)\n",
1845 session->s_mds, ceph_mds_state_name(state));
1849 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1850 ceph_mds_state_name(state));
1851 msg = ceph_create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1852 ++session->s_renew_seq);
1855 ceph_con_send(&session->s_con, msg);
1859 static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1860 struct ceph_mds_session *session, u64 seq)
1862 struct ceph_msg *msg;
1864 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1865 session->s_mds, ceph_session_state_name(session->s_state), seq);
1866 msg = ceph_create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1869 ceph_con_send(&session->s_con, msg);
1875 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1877 * Called under session->s_mutex
1879 static void renewed_caps(struct ceph_mds_client *mdsc,
1880 struct ceph_mds_session *session, int is_renew)
1885 spin_lock(&session->s_cap_lock);
1886 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
1888 session->s_cap_ttl = session->s_renew_requested +
1889 mdsc->mdsmap->m_session_timeout*HZ;
1892 if (time_before(jiffies, session->s_cap_ttl)) {
1893 pr_info("mds%d caps renewed\n", session->s_mds);
1896 pr_info("mds%d caps still stale\n", session->s_mds);
1899 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1900 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1901 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1902 spin_unlock(&session->s_cap_lock);
1905 wake_up_session_caps(session, RENEWCAPS);
1909 * send a session close request
1911 static int request_close_session(struct ceph_mds_session *session)
1913 struct ceph_msg *msg;
1915 dout("request_close_session mds%d state %s seq %lld\n",
1916 session->s_mds, ceph_session_state_name(session->s_state),
1918 msg = ceph_create_session_msg(CEPH_SESSION_REQUEST_CLOSE,
1922 ceph_con_send(&session->s_con, msg);
1927 * Called with s_mutex held.
1929 static int __close_session(struct ceph_mds_client *mdsc,
1930 struct ceph_mds_session *session)
1932 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1934 session->s_state = CEPH_MDS_SESSION_CLOSING;
1935 return request_close_session(session);
1938 static bool drop_negative_children(struct dentry *dentry)
1940 struct dentry *child;
1941 bool all_negative = true;
1943 if (!d_is_dir(dentry))
1946 spin_lock(&dentry->d_lock);
1947 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1948 if (d_really_is_positive(child)) {
1949 all_negative = false;
1953 spin_unlock(&dentry->d_lock);
1956 shrink_dcache_parent(dentry);
1958 return all_negative;
1962 * Trim old(er) caps.
1964 * Because we can't cache an inode without one or more caps, we do
1965 * this indirectly: if a cap is unused, we prune its aliases, at which
1966 * point the inode will hopefully get dropped to.
1968 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1969 * memory pressure from the MDS, though, so it needn't be perfect.
1971 static int trim_caps_cb(struct inode *inode, int mds, void *arg)
1973 int *remaining = arg;
1974 struct ceph_inode_info *ci = ceph_inode(inode);
1975 int used, wanted, oissued, mine;
1976 struct ceph_cap *cap;
1978 if (*remaining <= 0)
1981 spin_lock(&ci->i_ceph_lock);
1982 cap = __get_cap_for_mds(ci, mds);
1984 spin_unlock(&ci->i_ceph_lock);
1987 mine = cap->issued | cap->implemented;
1988 used = __ceph_caps_used(ci);
1989 wanted = __ceph_caps_file_wanted(ci);
1990 oissued = __ceph_caps_issued_other(ci, cap);
1992 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1993 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1994 ceph_cap_string(used), ceph_cap_string(wanted));
1995 if (cap == ci->i_auth_cap) {
1996 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1997 !list_empty(&ci->i_cap_snaps))
1999 if ((used | wanted) & CEPH_CAP_ANY_WR)
2001 /* Note: it's possible that i_filelock_ref becomes non-zero
2002 * after dropping auth caps. It doesn't hurt because reply
2003 * of lock mds request will re-add auth caps. */
2004 if (atomic_read(&ci->i_filelock_ref) > 0)
2007 /* The inode has cached pages, but it's no longer used.
2008 * we can safely drop it */
2009 if (S_ISREG(inode->i_mode) &&
2010 wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
2011 !(oissued & CEPH_CAP_FILE_CACHE)) {
2015 if ((used | wanted) & ~oissued & mine)
2016 goto out; /* we need these caps */
2019 /* we aren't the only cap.. just remove us */
2020 ceph_remove_cap(cap, true);
2023 struct dentry *dentry;
2024 /* try dropping referring dentries */
2025 spin_unlock(&ci->i_ceph_lock);
2026 dentry = d_find_any_alias(inode);
2027 if (dentry && drop_negative_children(dentry)) {
2030 d_prune_aliases(inode);
2031 count = atomic_read(&inode->i_count);
2034 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
2043 spin_unlock(&ci->i_ceph_lock);
2048 * Trim session cap count down to some max number.
2050 int ceph_trim_caps(struct ceph_mds_client *mdsc,
2051 struct ceph_mds_session *session,
2054 int trim_caps = session->s_nr_caps - max_caps;
2056 dout("trim_caps mds%d start: %d / %d, trim %d\n",
2057 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
2058 if (trim_caps > 0) {
2059 int remaining = trim_caps;
2061 ceph_iterate_session_caps(session, trim_caps_cb, &remaining);
2062 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
2063 session->s_mds, session->s_nr_caps, max_caps,
2064 trim_caps - remaining);
2067 ceph_flush_cap_releases(mdsc, session);
2071 static int check_caps_flush(struct ceph_mds_client *mdsc,
2076 spin_lock(&mdsc->cap_dirty_lock);
2077 if (!list_empty(&mdsc->cap_flush_list)) {
2078 struct ceph_cap_flush *cf =
2079 list_first_entry(&mdsc->cap_flush_list,
2080 struct ceph_cap_flush, g_list);
2081 if (cf->tid <= want_flush_tid) {
2082 dout("check_caps_flush still flushing tid "
2083 "%llu <= %llu\n", cf->tid, want_flush_tid);
2087 spin_unlock(&mdsc->cap_dirty_lock);
2092 * flush all dirty inode data to disk.
2094 * returns true if we've flushed through want_flush_tid
2096 static void wait_caps_flush(struct ceph_mds_client *mdsc,
2099 dout("check_caps_flush want %llu\n", want_flush_tid);
2101 wait_event(mdsc->cap_flushing_wq,
2102 check_caps_flush(mdsc, want_flush_tid));
2104 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
2108 * called under s_mutex
2110 static void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
2111 struct ceph_mds_session *session)
2113 struct ceph_msg *msg = NULL;
2114 struct ceph_mds_cap_release *head;
2115 struct ceph_mds_cap_item *item;
2116 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
2117 struct ceph_cap *cap;
2118 LIST_HEAD(tmp_list);
2119 int num_cap_releases;
2120 __le32 barrier, *cap_barrier;
2122 down_read(&osdc->lock);
2123 barrier = cpu_to_le32(osdc->epoch_barrier);
2124 up_read(&osdc->lock);
2126 spin_lock(&session->s_cap_lock);
2128 list_splice_init(&session->s_cap_releases, &tmp_list);
2129 num_cap_releases = session->s_num_cap_releases;
2130 session->s_num_cap_releases = 0;
2131 spin_unlock(&session->s_cap_lock);
2133 while (!list_empty(&tmp_list)) {
2135 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
2136 PAGE_SIZE, GFP_NOFS, false);
2139 head = msg->front.iov_base;
2140 head->num = cpu_to_le32(0);
2141 msg->front.iov_len = sizeof(*head);
2143 msg->hdr.version = cpu_to_le16(2);
2144 msg->hdr.compat_version = cpu_to_le16(1);
2147 cap = list_first_entry(&tmp_list, struct ceph_cap,
2149 list_del(&cap->session_caps);
2152 head = msg->front.iov_base;
2153 put_unaligned_le32(get_unaligned_le32(&head->num) + 1,
2155 item = msg->front.iov_base + msg->front.iov_len;
2156 item->ino = cpu_to_le64(cap->cap_ino);
2157 item->cap_id = cpu_to_le64(cap->cap_id);
2158 item->migrate_seq = cpu_to_le32(cap->mseq);
2159 item->seq = cpu_to_le32(cap->issue_seq);
2160 msg->front.iov_len += sizeof(*item);
2162 ceph_put_cap(mdsc, cap);
2164 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
2165 // Append cap_barrier field
2166 cap_barrier = msg->front.iov_base + msg->front.iov_len;
2167 *cap_barrier = barrier;
2168 msg->front.iov_len += sizeof(*cap_barrier);
2170 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2171 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
2172 ceph_con_send(&session->s_con, msg);
2177 BUG_ON(num_cap_releases != 0);
2179 spin_lock(&session->s_cap_lock);
2180 if (!list_empty(&session->s_cap_releases))
2182 spin_unlock(&session->s_cap_lock);
2185 // Append cap_barrier field
2186 cap_barrier = msg->front.iov_base + msg->front.iov_len;
2187 *cap_barrier = barrier;
2188 msg->front.iov_len += sizeof(*cap_barrier);
2190 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2191 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
2192 ceph_con_send(&session->s_con, msg);
2196 pr_err("send_cap_releases mds%d, failed to allocate message\n",
2198 spin_lock(&session->s_cap_lock);
2199 list_splice(&tmp_list, &session->s_cap_releases);
2200 session->s_num_cap_releases += num_cap_releases;
2201 spin_unlock(&session->s_cap_lock);
2204 static void ceph_cap_release_work(struct work_struct *work)
2206 struct ceph_mds_session *session =
2207 container_of(work, struct ceph_mds_session, s_cap_release_work);
2209 mutex_lock(&session->s_mutex);
2210 if (session->s_state == CEPH_MDS_SESSION_OPEN ||
2211 session->s_state == CEPH_MDS_SESSION_HUNG)
2212 ceph_send_cap_releases(session->s_mdsc, session);
2213 mutex_unlock(&session->s_mutex);
2214 ceph_put_mds_session(session);
2217 void ceph_flush_cap_releases(struct ceph_mds_client *mdsc,
2218 struct ceph_mds_session *session)
2223 ceph_get_mds_session(session);
2224 if (queue_work(mdsc->fsc->cap_wq,
2225 &session->s_cap_release_work)) {
2226 dout("cap release work queued\n");
2228 ceph_put_mds_session(session);
2229 dout("failed to queue cap release work\n");
2234 * caller holds session->s_cap_lock
2236 void __ceph_queue_cap_release(struct ceph_mds_session *session,
2237 struct ceph_cap *cap)
2239 list_add_tail(&cap->session_caps, &session->s_cap_releases);
2240 session->s_num_cap_releases++;
2242 if (!(session->s_num_cap_releases % CEPH_CAPS_PER_RELEASE))
2243 ceph_flush_cap_releases(session->s_mdsc, session);
2246 static void ceph_cap_reclaim_work(struct work_struct *work)
2248 struct ceph_mds_client *mdsc =
2249 container_of(work, struct ceph_mds_client, cap_reclaim_work);
2250 int ret = ceph_trim_dentries(mdsc);
2252 ceph_queue_cap_reclaim_work(mdsc);
2255 void ceph_queue_cap_reclaim_work(struct ceph_mds_client *mdsc)
2260 if (queue_work(mdsc->fsc->cap_wq, &mdsc->cap_reclaim_work)) {
2261 dout("caps reclaim work queued\n");
2263 dout("failed to queue caps release work\n");
2267 void ceph_reclaim_caps_nr(struct ceph_mds_client *mdsc, int nr)
2272 val = atomic_add_return(nr, &mdsc->cap_reclaim_pending);
2273 if ((val % CEPH_CAPS_PER_RELEASE) < nr) {
2274 atomic_set(&mdsc->cap_reclaim_pending, 0);
2275 ceph_queue_cap_reclaim_work(mdsc);
2283 int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
2286 struct ceph_inode_info *ci = ceph_inode(dir);
2287 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
2288 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
2289 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
2290 unsigned int num_entries;
2293 spin_lock(&ci->i_ceph_lock);
2294 num_entries = ci->i_files + ci->i_subdirs;
2295 spin_unlock(&ci->i_ceph_lock);
2296 num_entries = max(num_entries, 1U);
2297 num_entries = min(num_entries, opt->max_readdir);
2299 order = get_order(size * num_entries);
2300 while (order >= 0) {
2301 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
2305 if (rinfo->dir_entries)
2309 if (!rinfo->dir_entries)
2312 num_entries = (PAGE_SIZE << order) / size;
2313 num_entries = min(num_entries, opt->max_readdir);
2315 rinfo->dir_buf_size = PAGE_SIZE << order;
2316 req->r_num_caps = num_entries + 1;
2317 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
2318 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
2323 * Create an mds request.
2325 struct ceph_mds_request *
2326 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
2328 struct ceph_mds_request *req;
2330 req = kmem_cache_zalloc(ceph_mds_request_cachep, GFP_NOFS);
2332 return ERR_PTR(-ENOMEM);
2334 mutex_init(&req->r_fill_mutex);
2336 req->r_started = jiffies;
2337 req->r_start_latency = ktime_get();
2338 req->r_resend_mds = -1;
2339 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
2340 INIT_LIST_HEAD(&req->r_unsafe_target_item);
2342 req->r_feature_needed = -1;
2343 kref_init(&req->r_kref);
2344 RB_CLEAR_NODE(&req->r_node);
2345 INIT_LIST_HEAD(&req->r_wait);
2346 init_completion(&req->r_completion);
2347 init_completion(&req->r_safe_completion);
2348 INIT_LIST_HEAD(&req->r_unsafe_item);
2350 ktime_get_coarse_real_ts64(&req->r_stamp);
2353 req->r_direct_mode = mode;
2358 * return oldest (lowest) request, tid in request tree, 0 if none.
2360 * called under mdsc->mutex.
2362 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
2364 if (RB_EMPTY_ROOT(&mdsc->request_tree))
2366 return rb_entry(rb_first(&mdsc->request_tree),
2367 struct ceph_mds_request, r_node);
2370 static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
2372 return mdsc->oldest_tid;
2376 * Build a dentry's path. Allocate on heap; caller must kfree. Based
2377 * on build_path_from_dentry in fs/cifs/dir.c.
2379 * If @stop_on_nosnap, generate path relative to the first non-snapped
2382 * Encode hidden .snap dirs as a double /, i.e.
2383 * foo/.snap/bar -> foo//bar
2385 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase,
2388 struct dentry *temp;
2395 return ERR_PTR(-EINVAL);
2399 return ERR_PTR(-ENOMEM);
2404 seq = read_seqbegin(&rename_lock);
2408 struct inode *inode;
2410 spin_lock(&temp->d_lock);
2411 inode = d_inode(temp);
2412 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
2413 dout("build_path path+%d: %p SNAPDIR\n",
2415 } else if (stop_on_nosnap && inode && dentry != temp &&
2416 ceph_snap(inode) == CEPH_NOSNAP) {
2417 spin_unlock(&temp->d_lock);
2418 pos++; /* get rid of any prepended '/' */
2421 pos -= temp->d_name.len;
2423 spin_unlock(&temp->d_lock);
2426 memcpy(path + pos, temp->d_name.name, temp->d_name.len);
2428 spin_unlock(&temp->d_lock);
2429 temp = READ_ONCE(temp->d_parent);
2431 /* Are we at the root? */
2435 /* Are we out of buffer? */
2441 base = ceph_ino(d_inode(temp));
2444 if (read_seqretry(&rename_lock, seq))
2449 * A rename didn't occur, but somehow we didn't end up where
2450 * we thought we would. Throw a warning and try again.
2452 pr_warn("build_path did not end path lookup where "
2453 "expected, pos is %d\n", pos);
2458 *plen = PATH_MAX - 1 - pos;
2459 dout("build_path on %p %d built %llx '%.*s'\n",
2460 dentry, d_count(dentry), base, *plen, path + pos);
2464 static int build_dentry_path(struct dentry *dentry, struct inode *dir,
2465 const char **ppath, int *ppathlen, u64 *pino,
2466 bool *pfreepath, bool parent_locked)
2472 dir = d_inode_rcu(dentry->d_parent);
2473 if (dir && parent_locked && ceph_snap(dir) == CEPH_NOSNAP) {
2474 *pino = ceph_ino(dir);
2476 *ppath = dentry->d_name.name;
2477 *ppathlen = dentry->d_name.len;
2481 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2483 return PTR_ERR(path);
2489 static int build_inode_path(struct inode *inode,
2490 const char **ppath, int *ppathlen, u64 *pino,
2493 struct dentry *dentry;
2496 if (ceph_snap(inode) == CEPH_NOSNAP) {
2497 *pino = ceph_ino(inode);
2501 dentry = d_find_alias(inode);
2502 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2505 return PTR_ERR(path);
2512 * request arguments may be specified via an inode *, a dentry *, or
2513 * an explicit ino+path.
2515 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
2516 struct inode *rdiri, const char *rpath,
2517 u64 rino, const char **ppath, int *pathlen,
2518 u64 *ino, bool *freepath, bool parent_locked)
2523 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
2524 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
2526 } else if (rdentry) {
2527 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
2528 freepath, parent_locked);
2529 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
2531 } else if (rpath || rino) {
2534 *pathlen = rpath ? strlen(rpath) : 0;
2535 dout(" path %.*s\n", *pathlen, rpath);
2541 static void encode_timestamp_and_gids(void **p,
2542 const struct ceph_mds_request *req)
2544 struct ceph_timespec ts;
2547 ceph_encode_timespec64(&ts, &req->r_stamp);
2548 ceph_encode_copy(p, &ts, sizeof(ts));
2551 ceph_encode_32(p, req->r_cred->group_info->ngroups);
2552 for (i = 0; i < req->r_cred->group_info->ngroups; i++)
2553 ceph_encode_64(p, from_kgid(&init_user_ns,
2554 req->r_cred->group_info->gid[i]));
2558 * called under mdsc->mutex
2560 static struct ceph_msg *create_request_message(struct ceph_mds_session *session,
2561 struct ceph_mds_request *req,
2562 bool drop_cap_releases)
2564 int mds = session->s_mds;
2565 struct ceph_mds_client *mdsc = session->s_mdsc;
2566 struct ceph_msg *msg;
2567 struct ceph_mds_request_head_old *head;
2568 const char *path1 = NULL;
2569 const char *path2 = NULL;
2570 u64 ino1 = 0, ino2 = 0;
2571 int pathlen1 = 0, pathlen2 = 0;
2572 bool freepath1 = false, freepath2 = false;
2573 struct dentry *old_dentry = NULL;
2578 bool legacy = !(session->s_con.peer_features & CEPH_FEATURE_FS_BTIME);
2580 ret = set_request_path_attr(req->r_inode, req->r_dentry,
2581 req->r_parent, req->r_path1, req->r_ino1.ino,
2582 &path1, &pathlen1, &ino1, &freepath1,
2583 test_bit(CEPH_MDS_R_PARENT_LOCKED,
2584 &req->r_req_flags));
2590 /* If r_old_dentry is set, then assume that its parent is locked */
2591 if (req->r_old_dentry &&
2592 !(req->r_old_dentry->d_flags & DCACHE_DISCONNECTED))
2593 old_dentry = req->r_old_dentry;
2594 ret = set_request_path_attr(NULL, old_dentry,
2595 req->r_old_dentry_dir,
2596 req->r_path2, req->r_ino2.ino,
2597 &path2, &pathlen2, &ino2, &freepath2, true);
2603 len = legacy ? sizeof(*head) : sizeof(struct ceph_mds_request_head);
2604 len += pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
2605 sizeof(struct ceph_timespec);
2606 len += sizeof(u32) + (sizeof(u64) * req->r_cred->group_info->ngroups);
2608 /* calculate (max) length for cap releases */
2609 len += sizeof(struct ceph_mds_request_release) *
2610 (!!req->r_inode_drop + !!req->r_dentry_drop +
2611 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2613 if (req->r_dentry_drop)
2615 if (req->r_old_dentry_drop)
2618 msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
2620 msg = ERR_PTR(-ENOMEM);
2624 msg->hdr.tid = cpu_to_le64(req->r_tid);
2627 * The old ceph_mds_request_head didn't contain a version field, and
2628 * one was added when we moved the message version from 3->4.
2631 msg->hdr.version = cpu_to_le16(3);
2632 head = msg->front.iov_base;
2633 p = msg->front.iov_base + sizeof(*head);
2635 struct ceph_mds_request_head *new_head = msg->front.iov_base;
2637 msg->hdr.version = cpu_to_le16(4);
2638 new_head->version = cpu_to_le16(CEPH_MDS_REQUEST_HEAD_VERSION);
2639 head = (struct ceph_mds_request_head_old *)&new_head->oldest_client_tid;
2640 p = msg->front.iov_base + sizeof(*new_head);
2643 end = msg->front.iov_base + msg->front.iov_len;
2645 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2646 head->op = cpu_to_le32(req->r_op);
2647 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns,
2648 req->r_cred->fsuid));
2649 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns,
2650 req->r_cred->fsgid));
2651 head->ino = cpu_to_le64(req->r_deleg_ino);
2652 head->args = req->r_args;
2654 ceph_encode_filepath(&p, end, ino1, path1);
2655 ceph_encode_filepath(&p, end, ino2, path2);
2657 /* make note of release offset, in case we need to replay */
2658 req->r_request_release_offset = p - msg->front.iov_base;
2662 if (req->r_inode_drop)
2663 releases += ceph_encode_inode_release(&p,
2664 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
2665 mds, req->r_inode_drop, req->r_inode_unless,
2666 req->r_op == CEPH_MDS_OP_READDIR);
2667 if (req->r_dentry_drop)
2668 releases += ceph_encode_dentry_release(&p, req->r_dentry,
2669 req->r_parent, mds, req->r_dentry_drop,
2670 req->r_dentry_unless);
2671 if (req->r_old_dentry_drop)
2672 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
2673 req->r_old_dentry_dir, mds,
2674 req->r_old_dentry_drop,
2675 req->r_old_dentry_unless);
2676 if (req->r_old_inode_drop)
2677 releases += ceph_encode_inode_release(&p,
2678 d_inode(req->r_old_dentry),
2679 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
2681 if (drop_cap_releases) {
2683 p = msg->front.iov_base + req->r_request_release_offset;
2686 head->num_releases = cpu_to_le16(releases);
2688 encode_timestamp_and_gids(&p, req);
2690 if (WARN_ON_ONCE(p > end)) {
2692 msg = ERR_PTR(-ERANGE);
2696 msg->front.iov_len = p - msg->front.iov_base;
2697 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2699 if (req->r_pagelist) {
2700 struct ceph_pagelist *pagelist = req->r_pagelist;
2701 ceph_msg_data_add_pagelist(msg, pagelist);
2702 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2704 msg->hdr.data_len = 0;
2707 msg->hdr.data_off = cpu_to_le16(0);
2711 ceph_mdsc_free_path((char *)path2, pathlen2);
2714 ceph_mdsc_free_path((char *)path1, pathlen1);
2720 * called under mdsc->mutex if error, under no mutex if
2723 static void complete_request(struct ceph_mds_client *mdsc,
2724 struct ceph_mds_request *req)
2726 req->r_end_latency = ktime_get();
2728 if (req->r_callback)
2729 req->r_callback(mdsc, req);
2730 complete_all(&req->r_completion);
2733 static struct ceph_mds_request_head_old *
2734 find_old_request_head(void *p, u64 features)
2736 bool legacy = !(features & CEPH_FEATURE_FS_BTIME);
2737 struct ceph_mds_request_head *new_head;
2740 return (struct ceph_mds_request_head_old *)p;
2741 new_head = (struct ceph_mds_request_head *)p;
2742 return (struct ceph_mds_request_head_old *)&new_head->oldest_client_tid;
2746 * called under mdsc->mutex
2748 static int __prepare_send_request(struct ceph_mds_session *session,
2749 struct ceph_mds_request *req,
2750 bool drop_cap_releases)
2752 int mds = session->s_mds;
2753 struct ceph_mds_client *mdsc = session->s_mdsc;
2754 struct ceph_mds_request_head_old *rhead;
2755 struct ceph_msg *msg;
2756 int flags = 0, max_retry;
2759 * The type of 'r_attempts' in kernel 'ceph_mds_request'
2760 * is 'int', while in 'ceph_mds_request_head' the type of
2761 * 'num_retry' is '__u8'. So in case the request retries
2762 * exceeding 256 times, the MDS will receive a incorrect
2765 * In this case it's ususally a bug in MDS and continue
2766 * retrying the request makes no sense.
2768 * In future this could be fixed in ceph code, so avoid
2769 * using the hardcode here.
2771 max_retry = sizeof_field(struct ceph_mds_request_head, num_retry);
2772 max_retry = 1 << (max_retry * BITS_PER_BYTE);
2773 if (req->r_attempts >= max_retry) {
2774 pr_warn_ratelimited("%s request tid %llu seq overflow\n",
2775 __func__, req->r_tid);
2781 struct ceph_cap *cap =
2782 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2785 req->r_sent_on_mseq = cap->mseq;
2787 req->r_sent_on_mseq = -1;
2789 dout("%s %p tid %lld %s (attempt %d)\n", __func__, req,
2790 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2792 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2796 * Replay. Do not regenerate message (and rebuild
2797 * paths, etc.); just use the original message.
2798 * Rebuilding paths will break for renames because
2799 * d_move mangles the src name.
2801 msg = req->r_request;
2802 rhead = find_old_request_head(msg->front.iov_base,
2803 session->s_con.peer_features);
2805 flags = le32_to_cpu(rhead->flags);
2806 flags |= CEPH_MDS_FLAG_REPLAY;
2807 rhead->flags = cpu_to_le32(flags);
2809 if (req->r_target_inode)
2810 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2812 rhead->num_retry = req->r_attempts - 1;
2814 /* remove cap/dentry releases from message */
2815 rhead->num_releases = 0;
2817 p = msg->front.iov_base + req->r_request_release_offset;
2818 encode_timestamp_and_gids(&p, req);
2820 msg->front.iov_len = p - msg->front.iov_base;
2821 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2825 if (req->r_request) {
2826 ceph_msg_put(req->r_request);
2827 req->r_request = NULL;
2829 msg = create_request_message(session, req, drop_cap_releases);
2831 req->r_err = PTR_ERR(msg);
2832 return PTR_ERR(msg);
2834 req->r_request = msg;
2836 rhead = find_old_request_head(msg->front.iov_base,
2837 session->s_con.peer_features);
2838 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
2839 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2840 flags |= CEPH_MDS_FLAG_REPLAY;
2841 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags))
2842 flags |= CEPH_MDS_FLAG_ASYNC;
2844 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2845 rhead->flags = cpu_to_le32(flags);
2846 rhead->num_fwd = req->r_num_fwd;
2847 rhead->num_retry = req->r_attempts - 1;
2849 dout(" r_parent = %p\n", req->r_parent);
2854 * called under mdsc->mutex
2856 static int __send_request(struct ceph_mds_session *session,
2857 struct ceph_mds_request *req,
2858 bool drop_cap_releases)
2862 err = __prepare_send_request(session, req, drop_cap_releases);
2864 ceph_msg_get(req->r_request);
2865 ceph_con_send(&session->s_con, req->r_request);
2872 * send request, or put it on the appropriate wait list.
2874 static void __do_request(struct ceph_mds_client *mdsc,
2875 struct ceph_mds_request *req)
2877 struct ceph_mds_session *session = NULL;
2882 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2883 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
2884 __unregister_request(mdsc, req);
2888 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_FENCE_IO) {
2889 dout("do_request metadata corrupted\n");
2893 if (req->r_timeout &&
2894 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2895 dout("do_request timed out\n");
2899 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
2900 dout("do_request forced umount\n");
2904 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
2905 if (mdsc->mdsmap_err) {
2906 err = mdsc->mdsmap_err;
2907 dout("do_request mdsmap err %d\n", err);
2910 if (mdsc->mdsmap->m_epoch == 0) {
2911 dout("do_request no mdsmap, waiting for map\n");
2912 list_add(&req->r_wait, &mdsc->waiting_for_map);
2915 if (!(mdsc->fsc->mount_options->flags &
2916 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2917 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
2918 err = -EHOSTUNREACH;
2923 put_request_session(req);
2925 mds = __choose_mds(mdsc, req, &random);
2927 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2928 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags)) {
2932 dout("do_request no mds or not active, waiting for map\n");
2933 list_add(&req->r_wait, &mdsc->waiting_for_map);
2937 /* get, open session */
2938 session = __ceph_lookup_mds_session(mdsc, mds);
2940 session = register_session(mdsc, mds);
2941 if (IS_ERR(session)) {
2942 err = PTR_ERR(session);
2946 req->r_session = ceph_get_mds_session(session);
2948 dout("do_request mds%d session %p state %s\n", mds, session,
2949 ceph_session_state_name(session->s_state));
2952 * The old ceph will crash the MDSs when see unknown OPs
2954 if (req->r_feature_needed > 0 &&
2955 !test_bit(req->r_feature_needed, &session->s_features)) {
2960 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2961 session->s_state != CEPH_MDS_SESSION_HUNG) {
2963 * We cannot queue async requests since the caps and delegated
2964 * inodes are bound to the session. Just return -EJUKEBOX and
2965 * let the caller retry a sync request in that case.
2967 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags)) {
2973 * If the session has been REJECTED, then return a hard error,
2974 * unless it's a CLEANRECOVER mount, in which case we'll queue
2975 * it to the mdsc queue.
2977 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2978 if (ceph_test_mount_opt(mdsc->fsc, CLEANRECOVER))
2979 list_add(&req->r_wait, &mdsc->waiting_for_map);
2985 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2986 session->s_state == CEPH_MDS_SESSION_CLOSING) {
2987 err = __open_session(mdsc, session);
2990 /* retry the same mds later */
2992 req->r_resend_mds = mds;
2994 list_add(&req->r_wait, &session->s_waiting);
2999 req->r_resend_mds = -1; /* forget any previous mds hint */
3001 if (req->r_request_started == 0) /* note request start time */
3002 req->r_request_started = jiffies;
3005 * For async create we will choose the auth MDS of frag in parent
3006 * directory to send the request and ususally this works fine, but
3007 * if the migrated the dirtory to another MDS before it could handle
3008 * it the request will be forwarded.
3010 * And then the auth cap will be changed.
3012 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags) && req->r_num_fwd) {
3013 struct ceph_dentry_info *di = ceph_dentry(req->r_dentry);
3014 struct ceph_inode_info *ci;
3015 struct ceph_cap *cap;
3018 * The request maybe handled very fast and the new inode
3019 * hasn't been linked to the dentry yet. We need to wait
3020 * for the ceph_finish_async_create(), which shouldn't be
3021 * stuck too long or fail in thoery, to finish when forwarding
3024 if (!d_inode(req->r_dentry)) {
3025 err = wait_on_bit(&di->flags, CEPH_DENTRY_ASYNC_CREATE_BIT,
3028 mutex_lock(&req->r_fill_mutex);
3029 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
3030 mutex_unlock(&req->r_fill_mutex);
3035 ci = ceph_inode(d_inode(req->r_dentry));
3037 spin_lock(&ci->i_ceph_lock);
3038 cap = ci->i_auth_cap;
3039 if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE && mds != cap->mds) {
3040 dout("do_request session changed for auth cap %d -> %d\n",
3041 cap->session->s_mds, session->s_mds);
3043 /* Remove the auth cap from old session */
3044 spin_lock(&cap->session->s_cap_lock);
3045 cap->session->s_nr_caps--;
3046 list_del_init(&cap->session_caps);
3047 spin_unlock(&cap->session->s_cap_lock);
3049 /* Add the auth cap to the new session */
3051 cap->session = session;
3052 spin_lock(&session->s_cap_lock);
3053 session->s_nr_caps++;
3054 list_add_tail(&cap->session_caps, &session->s_caps);
3055 spin_unlock(&session->s_cap_lock);
3057 change_auth_cap_ses(ci, session);
3059 spin_unlock(&ci->i_ceph_lock);
3062 err = __send_request(session, req, false);
3065 ceph_put_mds_session(session);
3068 dout("__do_request early error %d\n", err);
3070 complete_request(mdsc, req);
3071 __unregister_request(mdsc, req);
3077 * called under mdsc->mutex
3079 static void __wake_requests(struct ceph_mds_client *mdsc,
3080 struct list_head *head)
3082 struct ceph_mds_request *req;
3083 LIST_HEAD(tmp_list);
3085 list_splice_init(head, &tmp_list);
3087 while (!list_empty(&tmp_list)) {
3088 req = list_entry(tmp_list.next,
3089 struct ceph_mds_request, r_wait);
3090 list_del_init(&req->r_wait);
3091 dout(" wake request %p tid %llu\n", req, req->r_tid);
3092 __do_request(mdsc, req);
3097 * Wake up threads with requests pending for @mds, so that they can
3098 * resubmit their requests to a possibly different mds.
3100 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
3102 struct ceph_mds_request *req;
3103 struct rb_node *p = rb_first(&mdsc->request_tree);
3105 dout("kick_requests mds%d\n", mds);
3107 req = rb_entry(p, struct ceph_mds_request, r_node);
3109 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
3111 if (req->r_attempts > 0)
3112 continue; /* only new requests */
3113 if (req->r_session &&
3114 req->r_session->s_mds == mds) {
3115 dout(" kicking tid %llu\n", req->r_tid);
3116 list_del_init(&req->r_wait);
3117 __do_request(mdsc, req);
3122 int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
3123 struct ceph_mds_request *req)
3127 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
3129 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
3130 if (req->r_parent) {
3131 struct ceph_inode_info *ci = ceph_inode(req->r_parent);
3132 int fmode = (req->r_op & CEPH_MDS_OP_WRITE) ?
3133 CEPH_FILE_MODE_WR : CEPH_FILE_MODE_RD;
3134 spin_lock(&ci->i_ceph_lock);
3135 ceph_take_cap_refs(ci, CEPH_CAP_PIN, false);
3136 __ceph_touch_fmode(ci, mdsc, fmode);
3137 spin_unlock(&ci->i_ceph_lock);
3139 if (req->r_old_dentry_dir)
3140 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
3144 err = ceph_wait_on_async_create(req->r_inode);
3146 dout("%s: wait for async create returned: %d\n",
3152 if (!err && req->r_old_inode) {
3153 err = ceph_wait_on_async_create(req->r_old_inode);
3155 dout("%s: wait for async create returned: %d\n",
3161 dout("submit_request on %p for inode %p\n", req, dir);
3162 mutex_lock(&mdsc->mutex);
3163 __register_request(mdsc, req, dir);
3164 __do_request(mdsc, req);
3166 mutex_unlock(&mdsc->mutex);
3170 int ceph_mdsc_wait_request(struct ceph_mds_client *mdsc,
3171 struct ceph_mds_request *req,
3172 ceph_mds_request_wait_callback_t wait_func)
3177 dout("do_request waiting\n");
3179 err = wait_func(mdsc, req);
3181 long timeleft = wait_for_completion_killable_timeout(
3183 ceph_timeout_jiffies(req->r_timeout));
3187 err = -ETIMEDOUT; /* timed out */
3189 err = timeleft; /* killed */
3191 dout("do_request waited, got %d\n", err);
3192 mutex_lock(&mdsc->mutex);
3194 /* only abort if we didn't race with a real reply */
3195 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
3196 err = le32_to_cpu(req->r_reply_info.head->result);
3197 } else if (err < 0) {
3198 dout("aborted request %lld with %d\n", req->r_tid, err);
3201 * ensure we aren't running concurrently with
3202 * ceph_fill_trace or ceph_readdir_prepopulate, which
3203 * rely on locks (dir mutex) held by our caller.
3205 mutex_lock(&req->r_fill_mutex);
3207 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
3208 mutex_unlock(&req->r_fill_mutex);
3210 if (req->r_parent &&
3211 (req->r_op & CEPH_MDS_OP_WRITE))
3212 ceph_invalidate_dir_request(req);
3217 mutex_unlock(&mdsc->mutex);
3222 * Synchrously perform an mds request. Take care of all of the
3223 * session setup, forwarding, retry details.
3225 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
3227 struct ceph_mds_request *req)
3231 dout("do_request on %p\n", req);
3234 err = ceph_mdsc_submit_request(mdsc, dir, req);
3236 err = ceph_mdsc_wait_request(mdsc, req, NULL);
3237 dout("do_request %p done, result %d\n", req, err);
3242 * Invalidate dir's completeness, dentry lease state on an aborted MDS
3243 * namespace request.
3245 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
3247 struct inode *dir = req->r_parent;
3248 struct inode *old_dir = req->r_old_dentry_dir;
3250 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
3252 ceph_dir_clear_complete(dir);
3254 ceph_dir_clear_complete(old_dir);
3256 ceph_invalidate_dentry_lease(req->r_dentry);
3257 if (req->r_old_dentry)
3258 ceph_invalidate_dentry_lease(req->r_old_dentry);
3264 * We take the session mutex and parse and process the reply immediately.
3265 * This preserves the logical ordering of replies, capabilities, etc., sent
3266 * by the MDS as they are applied to our local cache.
3268 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
3270 struct ceph_mds_client *mdsc = session->s_mdsc;
3271 struct ceph_mds_request *req;
3272 struct ceph_mds_reply_head *head = msg->front.iov_base;
3273 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
3274 struct ceph_snap_realm *realm;
3277 int mds = session->s_mds;
3278 bool close_sessions = false;
3280 if (msg->front.iov_len < sizeof(*head)) {
3281 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
3286 /* get request, session */
3287 tid = le64_to_cpu(msg->hdr.tid);
3288 mutex_lock(&mdsc->mutex);
3289 req = lookup_get_request(mdsc, tid);
3291 dout("handle_reply on unknown tid %llu\n", tid);
3292 mutex_unlock(&mdsc->mutex);
3295 dout("handle_reply %p\n", req);
3297 /* correct session? */
3298 if (req->r_session != session) {
3299 pr_err("mdsc_handle_reply got %llu on session mds%d"
3300 " not mds%d\n", tid, session->s_mds,
3301 req->r_session ? req->r_session->s_mds : -1);
3302 mutex_unlock(&mdsc->mutex);
3307 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
3308 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
3309 pr_warn("got a dup %s reply on %llu from mds%d\n",
3310 head->safe ? "safe" : "unsafe", tid, mds);
3311 mutex_unlock(&mdsc->mutex);
3314 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
3315 pr_warn("got unsafe after safe on %llu from mds%d\n",
3317 mutex_unlock(&mdsc->mutex);
3321 result = le32_to_cpu(head->result);
3324 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
3325 __unregister_request(mdsc, req);
3327 /* last request during umount? */
3328 if (mdsc->stopping && !__get_oldest_req(mdsc))
3329 complete_all(&mdsc->safe_umount_waiters);
3331 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
3333 * We already handled the unsafe response, now do the
3334 * cleanup. No need to examine the response; the MDS
3335 * doesn't include any result info in the safe
3336 * response. And even if it did, there is nothing
3337 * useful we could do with a revised return value.
3339 dout("got safe reply %llu, mds%d\n", tid, mds);
3341 mutex_unlock(&mdsc->mutex);
3345 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
3346 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
3349 dout("handle_reply tid %lld result %d\n", tid, result);
3350 rinfo = &req->r_reply_info;
3351 if (test_bit(CEPHFS_FEATURE_REPLY_ENCODING, &session->s_features))
3352 err = parse_reply_info(session, msg, rinfo, (u64)-1);
3354 err = parse_reply_info(session, msg, rinfo, session->s_con.peer_features);
3355 mutex_unlock(&mdsc->mutex);
3357 /* Must find target inode outside of mutexes to avoid deadlocks */
3358 if ((err >= 0) && rinfo->head->is_target) {
3360 struct ceph_vino tvino = {
3361 .ino = le64_to_cpu(rinfo->targeti.in->ino),
3362 .snap = le64_to_cpu(rinfo->targeti.in->snapid)
3365 in = ceph_get_inode(mdsc->fsc->sb, tvino);
3368 mutex_lock(&session->s_mutex);
3371 req->r_target_inode = in;
3374 mutex_lock(&session->s_mutex);
3376 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
3383 if (rinfo->snapblob_len) {
3384 down_write(&mdsc->snap_rwsem);
3385 err = ceph_update_snap_trace(mdsc, rinfo->snapblob,
3386 rinfo->snapblob + rinfo->snapblob_len,
3387 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
3390 up_write(&mdsc->snap_rwsem);
3391 close_sessions = true;
3396 downgrade_write(&mdsc->snap_rwsem);
3398 down_read(&mdsc->snap_rwsem);
3401 /* insert trace into our cache */
3402 mutex_lock(&req->r_fill_mutex);
3403 current->journal_info = req;
3404 err = ceph_fill_trace(mdsc->fsc->sb, req);
3406 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
3407 req->r_op == CEPH_MDS_OP_LSSNAP))
3408 ceph_readdir_prepopulate(req, req->r_session);
3410 current->journal_info = NULL;
3411 mutex_unlock(&req->r_fill_mutex);
3413 up_read(&mdsc->snap_rwsem);
3415 ceph_put_snap_realm(mdsc, realm);
3418 if (req->r_target_inode &&
3419 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
3420 struct ceph_inode_info *ci =
3421 ceph_inode(req->r_target_inode);
3422 spin_lock(&ci->i_unsafe_lock);
3423 list_add_tail(&req->r_unsafe_target_item,
3424 &ci->i_unsafe_iops);
3425 spin_unlock(&ci->i_unsafe_lock);
3428 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
3431 mutex_lock(&mdsc->mutex);
3432 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
3436 req->r_reply = ceph_msg_get(msg);
3437 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
3440 dout("reply arrived after request %lld was aborted\n", tid);
3442 mutex_unlock(&mdsc->mutex);
3444 mutex_unlock(&session->s_mutex);
3446 /* kick calling process */
3447 complete_request(mdsc, req);
3449 ceph_update_metadata_metrics(&mdsc->metric, req->r_start_latency,
3450 req->r_end_latency, err);
3452 ceph_mdsc_put_request(req);
3454 /* Defer closing the sessions after s_mutex lock being released */
3456 ceph_mdsc_close_sessions(mdsc);
3463 * handle mds notification that our request has been forwarded.
3465 static void handle_forward(struct ceph_mds_client *mdsc,
3466 struct ceph_mds_session *session,
3467 struct ceph_msg *msg)
3469 struct ceph_mds_request *req;
3470 u64 tid = le64_to_cpu(msg->hdr.tid);
3474 void *p = msg->front.iov_base;
3475 void *end = p + msg->front.iov_len;
3476 bool aborted = false;
3478 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3479 next_mds = ceph_decode_32(&p);
3480 fwd_seq = ceph_decode_32(&p);
3482 mutex_lock(&mdsc->mutex);
3483 req = lookup_get_request(mdsc, tid);
3485 mutex_unlock(&mdsc->mutex);
3486 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
3487 return; /* dup reply? */
3490 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
3491 dout("forward tid %llu aborted, unregistering\n", tid);
3492 __unregister_request(mdsc, req);
3493 } else if (fwd_seq <= req->r_num_fwd) {
3495 * The type of 'num_fwd' in ceph 'MClientRequestForward'
3496 * is 'int32_t', while in 'ceph_mds_request_head' the
3497 * type is '__u8'. So in case the request bounces between
3498 * MDSes exceeding 256 times, the client will get stuck.
3500 * In this case it's ususally a bug in MDS and continue
3501 * bouncing the request makes no sense.
3503 * In future this could be fixed in ceph code, so avoid
3504 * using the hardcode here.
3506 int max = sizeof_field(struct ceph_mds_request_head, num_fwd);
3507 max = 1 << (max * BITS_PER_BYTE);
3508 if (req->r_num_fwd >= max) {
3509 mutex_lock(&req->r_fill_mutex);
3510 req->r_err = -EMULTIHOP;
3511 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
3512 mutex_unlock(&req->r_fill_mutex);
3514 pr_warn_ratelimited("forward tid %llu seq overflow\n",
3517 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
3518 tid, next_mds, req->r_num_fwd, fwd_seq);
3521 /* resend. forward race not possible; mds would drop */
3522 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
3524 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
3525 req->r_attempts = 0;
3526 req->r_num_fwd = fwd_seq;
3527 req->r_resend_mds = next_mds;
3528 put_request_session(req);
3529 __do_request(mdsc, req);
3531 mutex_unlock(&mdsc->mutex);
3533 /* kick calling process */
3535 complete_request(mdsc, req);
3536 ceph_mdsc_put_request(req);
3540 pr_err("mdsc_handle_forward decode error err=%d\n", err);
3543 static int __decode_session_metadata(void **p, void *end,
3546 /* map<string,string> */
3549 ceph_decode_32_safe(p, end, n, bad);
3552 ceph_decode_32_safe(p, end, len, bad);
3553 ceph_decode_need(p, end, len, bad);
3554 err_str = !strncmp(*p, "error_string", len);
3556 ceph_decode_32_safe(p, end, len, bad);
3557 ceph_decode_need(p, end, len, bad);
3559 * Match "blocklisted (blacklisted)" from newer MDSes,
3560 * or "blacklisted" from older MDSes.
3562 if (err_str && strnstr(*p, "blacklisted", len))
3563 *blocklisted = true;
3572 * handle a mds session control message
3574 static void handle_session(struct ceph_mds_session *session,
3575 struct ceph_msg *msg)
3577 struct ceph_mds_client *mdsc = session->s_mdsc;
3578 int mds = session->s_mds;
3579 int msg_version = le16_to_cpu(msg->hdr.version);
3580 void *p = msg->front.iov_base;
3581 void *end = p + msg->front.iov_len;
3582 struct ceph_mds_session_head *h;
3584 u64 seq, features = 0;
3586 bool blocklisted = false;
3589 ceph_decode_need(&p, end, sizeof(*h), bad);
3593 op = le32_to_cpu(h->op);
3594 seq = le64_to_cpu(h->seq);
3596 if (msg_version >= 3) {
3598 /* version >= 2 and < 5, decode metadata, skip otherwise
3599 * as it's handled via flags.
3601 if (msg_version >= 5)
3602 ceph_decode_skip_map(&p, end, string, string, bad);
3603 else if (__decode_session_metadata(&p, end, &blocklisted) < 0)
3606 /* version >= 3, feature bits */
3607 ceph_decode_32_safe(&p, end, len, bad);
3609 ceph_decode_64_safe(&p, end, features, bad);
3610 p += len - sizeof(features);
3614 if (msg_version >= 5) {
3618 ceph_decode_skip_16(&p, end, bad); /* struct_v, struct_cv */
3619 ceph_decode_32_safe(&p, end, len, bad); /* len */
3620 ceph_decode_skip_n(&p, end, len, bad); /* metric_spec */
3622 /* version >= 5, flags */
3623 ceph_decode_32_safe(&p, end, flags, bad);
3624 if (flags & CEPH_SESSION_BLOCKLISTED) {
3625 pr_warn("mds%d session blocklisted\n", session->s_mds);
3630 mutex_lock(&mdsc->mutex);
3631 if (op == CEPH_SESSION_CLOSE) {
3632 ceph_get_mds_session(session);
3633 __unregister_session(mdsc, session);
3635 /* FIXME: this ttl calculation is generous */
3636 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
3637 mutex_unlock(&mdsc->mutex);
3639 mutex_lock(&session->s_mutex);
3641 dout("handle_session mds%d %s %p state %s seq %llu\n",
3642 mds, ceph_session_op_name(op), session,
3643 ceph_session_state_name(session->s_state), seq);
3645 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
3646 session->s_state = CEPH_MDS_SESSION_OPEN;
3647 pr_info("mds%d came back\n", session->s_mds);
3651 case CEPH_SESSION_OPEN:
3652 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3653 pr_info("mds%d reconnect success\n", session->s_mds);
3655 if (session->s_state == CEPH_MDS_SESSION_OPEN) {
3656 pr_notice("mds%d is already opened\n", session->s_mds);
3658 session->s_state = CEPH_MDS_SESSION_OPEN;
3659 session->s_features = features;
3660 renewed_caps(mdsc, session, 0);
3661 if (test_bit(CEPHFS_FEATURE_METRIC_COLLECT,
3662 &session->s_features))
3663 metric_schedule_delayed(&mdsc->metric);
3667 * The connection maybe broken and the session in client
3668 * side has been reinitialized, need to update the seq
3671 if (!session->s_seq && seq)
3672 session->s_seq = seq;
3676 __close_session(mdsc, session);
3679 case CEPH_SESSION_RENEWCAPS:
3680 if (session->s_renew_seq == seq)
3681 renewed_caps(mdsc, session, 1);
3684 case CEPH_SESSION_CLOSE:
3685 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3686 pr_info("mds%d reconnect denied\n", session->s_mds);
3687 session->s_state = CEPH_MDS_SESSION_CLOSED;
3688 cleanup_session_requests(mdsc, session);
3689 remove_session_caps(session);
3690 wake = 2; /* for good measure */
3691 wake_up_all(&mdsc->session_close_wq);
3694 case CEPH_SESSION_STALE:
3695 pr_info("mds%d caps went stale, renewing\n",
3697 atomic_inc(&session->s_cap_gen);
3698 session->s_cap_ttl = jiffies - 1;
3699 send_renew_caps(mdsc, session);
3702 case CEPH_SESSION_RECALL_STATE:
3703 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
3706 case CEPH_SESSION_FLUSHMSG:
3707 /* flush cap releases */
3708 spin_lock(&session->s_cap_lock);
3709 if (session->s_num_cap_releases)
3710 ceph_flush_cap_releases(mdsc, session);
3711 spin_unlock(&session->s_cap_lock);
3713 send_flushmsg_ack(mdsc, session, seq);
3716 case CEPH_SESSION_FORCE_RO:
3717 dout("force_session_readonly %p\n", session);
3718 spin_lock(&session->s_cap_lock);
3719 session->s_readonly = true;
3720 spin_unlock(&session->s_cap_lock);
3721 wake_up_session_caps(session, FORCE_RO);
3724 case CEPH_SESSION_REJECT:
3725 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
3726 pr_info("mds%d rejected session\n", session->s_mds);
3727 session->s_state = CEPH_MDS_SESSION_REJECTED;
3728 cleanup_session_requests(mdsc, session);
3729 remove_session_caps(session);
3731 mdsc->fsc->blocklisted = true;
3732 wake = 2; /* for good measure */
3736 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
3740 mutex_unlock(&session->s_mutex);
3742 mutex_lock(&mdsc->mutex);
3743 __wake_requests(mdsc, &session->s_waiting);
3745 kick_requests(mdsc, mds);
3746 mutex_unlock(&mdsc->mutex);
3748 if (op == CEPH_SESSION_CLOSE)
3749 ceph_put_mds_session(session);
3753 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
3754 (int)msg->front.iov_len);
3759 void ceph_mdsc_release_dir_caps(struct ceph_mds_request *req)
3763 dcaps = xchg(&req->r_dir_caps, 0);
3765 dout("releasing r_dir_caps=%s\n", ceph_cap_string(dcaps));
3766 ceph_put_cap_refs(ceph_inode(req->r_parent), dcaps);
3770 void ceph_mdsc_release_dir_caps_no_check(struct ceph_mds_request *req)
3774 dcaps = xchg(&req->r_dir_caps, 0);
3776 dout("releasing r_dir_caps=%s\n", ceph_cap_string(dcaps));
3777 ceph_put_cap_refs_no_check_caps(ceph_inode(req->r_parent),
3783 * called under session->mutex.
3785 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
3786 struct ceph_mds_session *session)
3788 struct ceph_mds_request *req, *nreq;
3791 dout("replay_unsafe_requests mds%d\n", session->s_mds);
3793 mutex_lock(&mdsc->mutex);
3794 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item)
3795 __send_request(session, req, true);
3798 * also re-send old requests when MDS enters reconnect stage. So that MDS
3799 * can process completed request in clientreplay stage.
3801 p = rb_first(&mdsc->request_tree);
3803 req = rb_entry(p, struct ceph_mds_request, r_node);
3805 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
3807 if (req->r_attempts == 0)
3808 continue; /* only old requests */
3809 if (!req->r_session)
3811 if (req->r_session->s_mds != session->s_mds)
3814 ceph_mdsc_release_dir_caps_no_check(req);
3816 __send_request(session, req, true);
3818 mutex_unlock(&mdsc->mutex);
3821 static int send_reconnect_partial(struct ceph_reconnect_state *recon_state)
3823 struct ceph_msg *reply;
3824 struct ceph_pagelist *_pagelist;
3829 if (!recon_state->allow_multi)
3832 /* can't handle message that contains both caps and realm */
3833 BUG_ON(!recon_state->nr_caps == !recon_state->nr_realms);
3835 /* pre-allocate new pagelist */
3836 _pagelist = ceph_pagelist_alloc(GFP_NOFS);
3840 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3844 /* placeholder for nr_caps */
3845 err = ceph_pagelist_encode_32(_pagelist, 0);
3849 if (recon_state->nr_caps) {
3850 /* currently encoding caps */
3851 err = ceph_pagelist_encode_32(recon_state->pagelist, 0);
3855 /* placeholder for nr_realms (currently encoding relams) */
3856 err = ceph_pagelist_encode_32(_pagelist, 0);
3861 err = ceph_pagelist_encode_8(recon_state->pagelist, 1);
3865 page = list_first_entry(&recon_state->pagelist->head, struct page, lru);
3866 addr = kmap_atomic(page);
3867 if (recon_state->nr_caps) {
3868 /* currently encoding caps */
3869 *addr = cpu_to_le32(recon_state->nr_caps);
3871 /* currently encoding relams */
3872 *(addr + 1) = cpu_to_le32(recon_state->nr_realms);
3874 kunmap_atomic(addr);
3876 reply->hdr.version = cpu_to_le16(5);
3877 reply->hdr.compat_version = cpu_to_le16(4);
3879 reply->hdr.data_len = cpu_to_le32(recon_state->pagelist->length);
3880 ceph_msg_data_add_pagelist(reply, recon_state->pagelist);
3882 ceph_con_send(&recon_state->session->s_con, reply);
3883 ceph_pagelist_release(recon_state->pagelist);
3885 recon_state->pagelist = _pagelist;
3886 recon_state->nr_caps = 0;
3887 recon_state->nr_realms = 0;
3888 recon_state->msg_version = 5;
3891 ceph_msg_put(reply);
3893 ceph_pagelist_release(_pagelist);
3897 static struct dentry* d_find_primary(struct inode *inode)
3899 struct dentry *alias, *dn = NULL;
3901 if (hlist_empty(&inode->i_dentry))
3904 spin_lock(&inode->i_lock);
3905 if (hlist_empty(&inode->i_dentry))
3908 if (S_ISDIR(inode->i_mode)) {
3909 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
3910 if (!IS_ROOT(alias))
3915 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
3916 spin_lock(&alias->d_lock);
3917 if (!d_unhashed(alias) &&
3918 (ceph_dentry(alias)->flags & CEPH_DENTRY_PRIMARY_LINK)) {
3919 dn = dget_dlock(alias);
3921 spin_unlock(&alias->d_lock);
3926 spin_unlock(&inode->i_lock);
3931 * Encode information about a cap for a reconnect with the MDS.
3933 static int reconnect_caps_cb(struct inode *inode, int mds, void *arg)
3936 struct ceph_mds_cap_reconnect v2;
3937 struct ceph_mds_cap_reconnect_v1 v1;
3939 struct ceph_inode_info *ci = ceph_inode(inode);
3940 struct ceph_reconnect_state *recon_state = arg;
3941 struct ceph_pagelist *pagelist = recon_state->pagelist;
3942 struct dentry *dentry;
3943 struct ceph_cap *cap;
3945 int pathlen = 0, err;
3949 dentry = d_find_primary(inode);
3951 /* set pathbase to parent dir when msg_version >= 2 */
3952 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase,
3953 recon_state->msg_version >= 2);
3956 err = PTR_ERR(path);
3964 spin_lock(&ci->i_ceph_lock);
3965 cap = __get_cap_for_mds(ci, mds);
3967 spin_unlock(&ci->i_ceph_lock);
3971 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
3972 inode, ceph_vinop(inode), cap, cap->cap_id,
3973 ceph_cap_string(cap->issued));
3975 cap->seq = 0; /* reset cap seq */
3976 cap->issue_seq = 0; /* and issue_seq */
3977 cap->mseq = 0; /* and migrate_seq */
3978 cap->cap_gen = atomic_read(&cap->session->s_cap_gen);
3980 /* These are lost when the session goes away */
3981 if (S_ISDIR(inode->i_mode)) {
3982 if (cap->issued & CEPH_CAP_DIR_CREATE) {
3983 ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
3984 memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
3986 cap->issued &= ~CEPH_CAP_ANY_DIR_OPS;
3989 if (recon_state->msg_version >= 2) {
3990 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
3991 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3992 rec.v2.issued = cpu_to_le32(cap->issued);
3993 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
3994 rec.v2.pathbase = cpu_to_le64(pathbase);
3995 rec.v2.flock_len = (__force __le32)
3996 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
3998 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
3999 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
4000 rec.v1.issued = cpu_to_le32(cap->issued);
4001 rec.v1.size = cpu_to_le64(i_size_read(inode));
4002 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
4003 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
4004 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
4005 rec.v1.pathbase = cpu_to_le64(pathbase);
4008 if (list_empty(&ci->i_cap_snaps)) {
4009 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
4011 struct ceph_cap_snap *capsnap =
4012 list_first_entry(&ci->i_cap_snaps,
4013 struct ceph_cap_snap, ci_item);
4014 snap_follows = capsnap->follows;
4016 spin_unlock(&ci->i_ceph_lock);
4018 if (recon_state->msg_version >= 2) {
4019 int num_fcntl_locks, num_flock_locks;
4020 struct ceph_filelock *flocks = NULL;
4021 size_t struct_len, total_len = sizeof(u64);
4025 if (rec.v2.flock_len) {
4026 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
4028 num_fcntl_locks = 0;
4029 num_flock_locks = 0;
4031 if (num_fcntl_locks + num_flock_locks > 0) {
4032 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
4033 sizeof(struct ceph_filelock),
4039 err = ceph_encode_locks_to_buffer(inode, flocks,
4054 if (recon_state->msg_version >= 3) {
4055 /* version, compat_version and struct_len */
4056 total_len += 2 * sizeof(u8) + sizeof(u32);
4060 * number of encoded locks is stable, so copy to pagelist
4062 struct_len = 2 * sizeof(u32) +
4063 (num_fcntl_locks + num_flock_locks) *
4064 sizeof(struct ceph_filelock);
4065 rec.v2.flock_len = cpu_to_le32(struct_len);
4067 struct_len += sizeof(u32) + pathlen + sizeof(rec.v2);
4070 struct_len += sizeof(u64); /* snap_follows */
4072 total_len += struct_len;
4074 if (pagelist->length + total_len > RECONNECT_MAX_SIZE) {
4075 err = send_reconnect_partial(recon_state);
4077 goto out_freeflocks;
4078 pagelist = recon_state->pagelist;
4081 err = ceph_pagelist_reserve(pagelist, total_len);
4083 goto out_freeflocks;
4085 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
4086 if (recon_state->msg_version >= 3) {
4087 ceph_pagelist_encode_8(pagelist, struct_v);
4088 ceph_pagelist_encode_8(pagelist, 1);
4089 ceph_pagelist_encode_32(pagelist, struct_len);
4091 ceph_pagelist_encode_string(pagelist, path, pathlen);
4092 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
4093 ceph_locks_to_pagelist(flocks, pagelist,
4094 num_fcntl_locks, num_flock_locks);
4096 ceph_pagelist_encode_64(pagelist, snap_follows);
4100 err = ceph_pagelist_reserve(pagelist,
4101 sizeof(u64) + sizeof(u32) +
4102 pathlen + sizeof(rec.v1));
4106 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
4107 ceph_pagelist_encode_string(pagelist, path, pathlen);
4108 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
4112 ceph_mdsc_free_path(path, pathlen);
4114 recon_state->nr_caps++;
4118 static int encode_snap_realms(struct ceph_mds_client *mdsc,
4119 struct ceph_reconnect_state *recon_state)
4122 struct ceph_pagelist *pagelist = recon_state->pagelist;
4125 if (recon_state->msg_version >= 4) {
4126 err = ceph_pagelist_encode_32(pagelist, mdsc->num_snap_realms);
4132 * snaprealms. we provide mds with the ino, seq (version), and
4133 * parent for all of our realms. If the mds has any newer info,
4136 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
4137 struct ceph_snap_realm *realm =
4138 rb_entry(p, struct ceph_snap_realm, node);
4139 struct ceph_mds_snaprealm_reconnect sr_rec;
4141 if (recon_state->msg_version >= 4) {
4142 size_t need = sizeof(u8) * 2 + sizeof(u32) +
4145 if (pagelist->length + need > RECONNECT_MAX_SIZE) {
4146 err = send_reconnect_partial(recon_state);
4149 pagelist = recon_state->pagelist;
4152 err = ceph_pagelist_reserve(pagelist, need);
4156 ceph_pagelist_encode_8(pagelist, 1);
4157 ceph_pagelist_encode_8(pagelist, 1);
4158 ceph_pagelist_encode_32(pagelist, sizeof(sr_rec));
4161 dout(" adding snap realm %llx seq %lld parent %llx\n",
4162 realm->ino, realm->seq, realm->parent_ino);
4163 sr_rec.ino = cpu_to_le64(realm->ino);
4164 sr_rec.seq = cpu_to_le64(realm->seq);
4165 sr_rec.parent = cpu_to_le64(realm->parent_ino);
4167 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
4171 recon_state->nr_realms++;
4179 * If an MDS fails and recovers, clients need to reconnect in order to
4180 * reestablish shared state. This includes all caps issued through
4181 * this session _and_ the snap_realm hierarchy. Because it's not
4182 * clear which snap realms the mds cares about, we send everything we
4183 * know about.. that ensures we'll then get any new info the
4184 * recovering MDS might have.
4186 * This is a relatively heavyweight operation, but it's rare.
4188 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
4189 struct ceph_mds_session *session)
4191 struct ceph_msg *reply;
4192 int mds = session->s_mds;
4194 struct ceph_reconnect_state recon_state = {
4199 pr_info("mds%d reconnect start\n", mds);
4201 recon_state.pagelist = ceph_pagelist_alloc(GFP_NOFS);
4202 if (!recon_state.pagelist)
4203 goto fail_nopagelist;
4205 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
4209 xa_destroy(&session->s_delegated_inos);
4211 mutex_lock(&session->s_mutex);
4212 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
4215 dout("session %p state %s\n", session,
4216 ceph_session_state_name(session->s_state));
4218 atomic_inc(&session->s_cap_gen);
4220 spin_lock(&session->s_cap_lock);
4221 /* don't know if session is readonly */
4222 session->s_readonly = 0;
4224 * notify __ceph_remove_cap() that we are composing cap reconnect.
4225 * If a cap get released before being added to the cap reconnect,
4226 * __ceph_remove_cap() should skip queuing cap release.
4228 session->s_cap_reconnect = 1;
4229 /* drop old cap expires; we're about to reestablish that state */
4230 detach_cap_releases(session, &dispose);
4231 spin_unlock(&session->s_cap_lock);
4232 dispose_cap_releases(mdsc, &dispose);
4234 /* trim unused caps to reduce MDS's cache rejoin time */
4235 if (mdsc->fsc->sb->s_root)
4236 shrink_dcache_parent(mdsc->fsc->sb->s_root);
4238 ceph_con_close(&session->s_con);
4239 ceph_con_open(&session->s_con,
4240 CEPH_ENTITY_TYPE_MDS, mds,
4241 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
4243 /* replay unsafe requests */
4244 replay_unsafe_requests(mdsc, session);
4246 ceph_early_kick_flushing_caps(mdsc, session);
4248 down_read(&mdsc->snap_rwsem);
4250 /* placeholder for nr_caps */
4251 err = ceph_pagelist_encode_32(recon_state.pagelist, 0);
4255 if (test_bit(CEPHFS_FEATURE_MULTI_RECONNECT, &session->s_features)) {
4256 recon_state.msg_version = 3;
4257 recon_state.allow_multi = true;
4258 } else if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) {
4259 recon_state.msg_version = 3;
4261 recon_state.msg_version = 2;
4263 /* trsaverse this session's caps */
4264 err = ceph_iterate_session_caps(session, reconnect_caps_cb, &recon_state);
4266 spin_lock(&session->s_cap_lock);
4267 session->s_cap_reconnect = 0;
4268 spin_unlock(&session->s_cap_lock);
4273 /* check if all realms can be encoded into current message */
4274 if (mdsc->num_snap_realms) {
4276 recon_state.pagelist->length +
4277 mdsc->num_snap_realms *
4278 sizeof(struct ceph_mds_snaprealm_reconnect);
4279 if (recon_state.msg_version >= 4) {
4280 /* number of realms */
4281 total_len += sizeof(u32);
4282 /* version, compat_version and struct_len */
4283 total_len += mdsc->num_snap_realms *
4284 (2 * sizeof(u8) + sizeof(u32));
4286 if (total_len > RECONNECT_MAX_SIZE) {
4287 if (!recon_state.allow_multi) {
4291 if (recon_state.nr_caps) {
4292 err = send_reconnect_partial(&recon_state);
4296 recon_state.msg_version = 5;
4300 err = encode_snap_realms(mdsc, &recon_state);
4304 if (recon_state.msg_version >= 5) {
4305 err = ceph_pagelist_encode_8(recon_state.pagelist, 0);
4310 if (recon_state.nr_caps || recon_state.nr_realms) {
4312 list_first_entry(&recon_state.pagelist->head,
4314 __le32 *addr = kmap_atomic(page);
4315 if (recon_state.nr_caps) {
4316 WARN_ON(recon_state.nr_realms != mdsc->num_snap_realms);
4317 *addr = cpu_to_le32(recon_state.nr_caps);
4318 } else if (recon_state.msg_version >= 4) {
4319 *(addr + 1) = cpu_to_le32(recon_state.nr_realms);
4321 kunmap_atomic(addr);
4324 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
4325 if (recon_state.msg_version >= 4)
4326 reply->hdr.compat_version = cpu_to_le16(4);
4328 reply->hdr.data_len = cpu_to_le32(recon_state.pagelist->length);
4329 ceph_msg_data_add_pagelist(reply, recon_state.pagelist);
4331 ceph_con_send(&session->s_con, reply);
4333 mutex_unlock(&session->s_mutex);
4335 mutex_lock(&mdsc->mutex);
4336 __wake_requests(mdsc, &session->s_waiting);
4337 mutex_unlock(&mdsc->mutex);
4339 up_read(&mdsc->snap_rwsem);
4340 ceph_pagelist_release(recon_state.pagelist);
4344 ceph_msg_put(reply);
4345 up_read(&mdsc->snap_rwsem);
4346 mutex_unlock(&session->s_mutex);
4348 ceph_pagelist_release(recon_state.pagelist);
4350 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
4356 * compare old and new mdsmaps, kicking requests
4357 * and closing out old connections as necessary
4359 * called under mdsc->mutex.
4361 static void check_new_map(struct ceph_mds_client *mdsc,
4362 struct ceph_mdsmap *newmap,
4363 struct ceph_mdsmap *oldmap)
4366 int oldstate, newstate;
4367 struct ceph_mds_session *s;
4368 unsigned long targets[DIV_ROUND_UP(CEPH_MAX_MDS, sizeof(unsigned long))] = {0};
4370 dout("check_new_map new %u old %u\n",
4371 newmap->m_epoch, oldmap->m_epoch);
4373 if (newmap->m_info) {
4374 for (i = 0; i < newmap->possible_max_rank; i++) {
4375 for (j = 0; j < newmap->m_info[i].num_export_targets; j++)
4376 set_bit(newmap->m_info[i].export_targets[j], targets);
4380 for (i = 0; i < oldmap->possible_max_rank && i < mdsc->max_sessions; i++) {
4381 if (!mdsc->sessions[i])
4383 s = mdsc->sessions[i];
4384 oldstate = ceph_mdsmap_get_state(oldmap, i);
4385 newstate = ceph_mdsmap_get_state(newmap, i);
4387 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
4388 i, ceph_mds_state_name(oldstate),
4389 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
4390 ceph_mds_state_name(newstate),
4391 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
4392 ceph_session_state_name(s->s_state));
4394 if (i >= newmap->possible_max_rank) {
4395 /* force close session for stopped mds */
4396 ceph_get_mds_session(s);
4397 __unregister_session(mdsc, s);
4398 __wake_requests(mdsc, &s->s_waiting);
4399 mutex_unlock(&mdsc->mutex);
4401 mutex_lock(&s->s_mutex);
4402 cleanup_session_requests(mdsc, s);
4403 remove_session_caps(s);
4404 mutex_unlock(&s->s_mutex);
4406 ceph_put_mds_session(s);
4408 mutex_lock(&mdsc->mutex);
4409 kick_requests(mdsc, i);
4413 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
4414 ceph_mdsmap_get_addr(newmap, i),
4415 sizeof(struct ceph_entity_addr))) {
4417 mutex_unlock(&mdsc->mutex);
4418 mutex_lock(&s->s_mutex);
4419 mutex_lock(&mdsc->mutex);
4420 ceph_con_close(&s->s_con);
4421 mutex_unlock(&s->s_mutex);
4422 s->s_state = CEPH_MDS_SESSION_RESTARTING;
4423 } else if (oldstate == newstate) {
4424 continue; /* nothing new with this mds */
4430 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
4431 newstate >= CEPH_MDS_STATE_RECONNECT) {
4432 mutex_unlock(&mdsc->mutex);
4433 clear_bit(i, targets);
4434 send_mds_reconnect(mdsc, s);
4435 mutex_lock(&mdsc->mutex);
4439 * kick request on any mds that has gone active.
4441 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
4442 newstate >= CEPH_MDS_STATE_ACTIVE) {
4443 if (oldstate != CEPH_MDS_STATE_CREATING &&
4444 oldstate != CEPH_MDS_STATE_STARTING)
4445 pr_info("mds%d recovery completed\n", s->s_mds);
4446 kick_requests(mdsc, i);
4447 mutex_unlock(&mdsc->mutex);
4448 mutex_lock(&s->s_mutex);
4449 mutex_lock(&mdsc->mutex);
4450 ceph_kick_flushing_caps(mdsc, s);
4451 mutex_unlock(&s->s_mutex);
4452 wake_up_session_caps(s, RECONNECT);
4457 * Only open and reconnect sessions that don't exist yet.
4459 for (i = 0; i < newmap->possible_max_rank; i++) {
4461 * In case the import MDS is crashed just after
4462 * the EImportStart journal is flushed, so when
4463 * a standby MDS takes over it and is replaying
4464 * the EImportStart journal the new MDS daemon
4465 * will wait the client to reconnect it, but the
4466 * client may never register/open the session yet.
4468 * Will try to reconnect that MDS daemon if the
4469 * rank number is in the export targets array and
4470 * is the up:reconnect state.
4472 newstate = ceph_mdsmap_get_state(newmap, i);
4473 if (!test_bit(i, targets) || newstate != CEPH_MDS_STATE_RECONNECT)
4477 * The session maybe registered and opened by some
4478 * requests which were choosing random MDSes during
4479 * the mdsc->mutex's unlock/lock gap below in rare
4480 * case. But the related MDS daemon will just queue
4481 * that requests and be still waiting for the client's
4482 * reconnection request in up:reconnect state.
4484 s = __ceph_lookup_mds_session(mdsc, i);
4486 s = __open_export_target_session(mdsc, i);
4489 pr_err("failed to open export target session, err %d\n",
4494 dout("send reconnect to export target mds.%d\n", i);
4495 mutex_unlock(&mdsc->mutex);
4496 send_mds_reconnect(mdsc, s);
4497 ceph_put_mds_session(s);
4498 mutex_lock(&mdsc->mutex);
4501 for (i = 0; i < newmap->possible_max_rank && i < mdsc->max_sessions; i++) {
4502 s = mdsc->sessions[i];
4505 if (!ceph_mdsmap_is_laggy(newmap, i))
4507 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4508 s->s_state == CEPH_MDS_SESSION_HUNG ||
4509 s->s_state == CEPH_MDS_SESSION_CLOSING) {
4510 dout(" connecting to export targets of laggy mds%d\n",
4512 __open_export_target_sessions(mdsc, s);
4524 * caller must hold session s_mutex, dentry->d_lock
4526 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
4528 struct ceph_dentry_info *di = ceph_dentry(dentry);
4530 ceph_put_mds_session(di->lease_session);
4531 di->lease_session = NULL;
4534 static void handle_lease(struct ceph_mds_client *mdsc,
4535 struct ceph_mds_session *session,
4536 struct ceph_msg *msg)
4538 struct super_block *sb = mdsc->fsc->sb;
4539 struct inode *inode;
4540 struct dentry *parent, *dentry;
4541 struct ceph_dentry_info *di;
4542 int mds = session->s_mds;
4543 struct ceph_mds_lease *h = msg->front.iov_base;
4545 struct ceph_vino vino;
4549 dout("handle_lease from mds%d\n", mds);
4552 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
4554 vino.ino = le64_to_cpu(h->ino);
4555 vino.snap = CEPH_NOSNAP;
4556 seq = le32_to_cpu(h->seq);
4557 dname.len = get_unaligned_le32(h + 1);
4558 if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
4560 dname.name = (void *)(h + 1) + sizeof(u32);
4563 inode = ceph_find_inode(sb, vino);
4564 dout("handle_lease %s, ino %llx %p %.*s\n",
4565 ceph_lease_op_name(h->action), vino.ino, inode,
4566 dname.len, dname.name);
4568 mutex_lock(&session->s_mutex);
4569 inc_session_sequence(session);
4572 dout("handle_lease no inode %llx\n", vino.ino);
4577 parent = d_find_alias(inode);
4579 dout("no parent dentry on inode %p\n", inode);
4581 goto release; /* hrm... */
4583 dname.hash = full_name_hash(parent, dname.name, dname.len);
4584 dentry = d_lookup(parent, &dname);
4589 spin_lock(&dentry->d_lock);
4590 di = ceph_dentry(dentry);
4591 switch (h->action) {
4592 case CEPH_MDS_LEASE_REVOKE:
4593 if (di->lease_session == session) {
4594 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
4595 h->seq = cpu_to_le32(di->lease_seq);
4596 __ceph_mdsc_drop_dentry_lease(dentry);
4601 case CEPH_MDS_LEASE_RENEW:
4602 if (di->lease_session == session &&
4603 di->lease_gen == atomic_read(&session->s_cap_gen) &&
4604 di->lease_renew_from &&
4605 di->lease_renew_after == 0) {
4606 unsigned long duration =
4607 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
4609 di->lease_seq = seq;
4610 di->time = di->lease_renew_from + duration;
4611 di->lease_renew_after = di->lease_renew_from +
4613 di->lease_renew_from = 0;
4617 spin_unlock(&dentry->d_lock);
4624 /* let's just reuse the same message */
4625 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
4627 ceph_con_send(&session->s_con, msg);
4630 mutex_unlock(&session->s_mutex);
4635 pr_err("corrupt lease message\n");
4639 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
4640 struct dentry *dentry, char action,
4643 struct ceph_msg *msg;
4644 struct ceph_mds_lease *lease;
4646 int len = sizeof(*lease) + sizeof(u32) + NAME_MAX;
4648 dout("lease_send_msg identry %p %s to mds%d\n",
4649 dentry, ceph_lease_op_name(action), session->s_mds);
4651 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
4654 lease = msg->front.iov_base;
4655 lease->action = action;
4656 lease->seq = cpu_to_le32(seq);
4658 spin_lock(&dentry->d_lock);
4659 dir = d_inode(dentry->d_parent);
4660 lease->ino = cpu_to_le64(ceph_ino(dir));
4661 lease->first = lease->last = cpu_to_le64(ceph_snap(dir));
4663 put_unaligned_le32(dentry->d_name.len, lease + 1);
4664 memcpy((void *)(lease + 1) + 4,
4665 dentry->d_name.name, dentry->d_name.len);
4666 spin_unlock(&dentry->d_lock);
4668 ceph_con_send(&session->s_con, msg);
4672 * lock unlock the session, to wait ongoing session activities
4674 static void lock_unlock_session(struct ceph_mds_session *s)
4676 mutex_lock(&s->s_mutex);
4677 mutex_unlock(&s->s_mutex);
4680 static void maybe_recover_session(struct ceph_mds_client *mdsc)
4682 struct ceph_fs_client *fsc = mdsc->fsc;
4684 if (!ceph_test_mount_opt(fsc, CLEANRECOVER))
4687 if (READ_ONCE(fsc->mount_state) != CEPH_MOUNT_MOUNTED)
4690 if (!READ_ONCE(fsc->blocklisted))
4693 pr_info("auto reconnect after blocklisted\n");
4694 ceph_force_reconnect(fsc->sb);
4697 bool check_session_state(struct ceph_mds_session *s)
4699 switch (s->s_state) {
4700 case CEPH_MDS_SESSION_OPEN:
4701 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
4702 s->s_state = CEPH_MDS_SESSION_HUNG;
4703 pr_info("mds%d hung\n", s->s_mds);
4706 case CEPH_MDS_SESSION_CLOSING:
4707 case CEPH_MDS_SESSION_NEW:
4708 case CEPH_MDS_SESSION_RESTARTING:
4709 case CEPH_MDS_SESSION_CLOSED:
4710 case CEPH_MDS_SESSION_REJECTED:
4718 * If the sequence is incremented while we're waiting on a REQUEST_CLOSE reply,
4719 * then we need to retransmit that request.
4721 void inc_session_sequence(struct ceph_mds_session *s)
4723 lockdep_assert_held(&s->s_mutex);
4727 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
4730 dout("resending session close request for mds%d\n", s->s_mds);
4731 ret = request_close_session(s);
4733 pr_err("unable to close session to mds%d: %d\n",
4739 * delayed work -- periodically trim expired leases, renew caps with mds. If
4740 * the @delay parameter is set to 0 or if it's more than 5 secs, the default
4741 * workqueue delay value of 5 secs will be used.
4743 static void schedule_delayed(struct ceph_mds_client *mdsc, unsigned long delay)
4745 unsigned long max_delay = HZ * 5;
4747 /* 5 secs default delay */
4748 if (!delay || (delay > max_delay))
4750 schedule_delayed_work(&mdsc->delayed_work,
4751 round_jiffies_relative(delay));
4754 static void delayed_work(struct work_struct *work)
4756 struct ceph_mds_client *mdsc =
4757 container_of(work, struct ceph_mds_client, delayed_work.work);
4758 unsigned long delay;
4763 dout("mdsc delayed_work\n");
4768 mutex_lock(&mdsc->mutex);
4769 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
4770 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
4771 mdsc->last_renew_caps);
4773 mdsc->last_renew_caps = jiffies;
4775 for (i = 0; i < mdsc->max_sessions; i++) {
4776 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
4780 if (!check_session_state(s)) {
4781 ceph_put_mds_session(s);
4784 mutex_unlock(&mdsc->mutex);
4786 mutex_lock(&s->s_mutex);
4788 send_renew_caps(mdsc, s);
4790 ceph_con_keepalive(&s->s_con);
4791 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4792 s->s_state == CEPH_MDS_SESSION_HUNG)
4793 ceph_send_cap_releases(mdsc, s);
4794 mutex_unlock(&s->s_mutex);
4795 ceph_put_mds_session(s);
4797 mutex_lock(&mdsc->mutex);
4799 mutex_unlock(&mdsc->mutex);
4801 delay = ceph_check_delayed_caps(mdsc);
4803 ceph_queue_cap_reclaim_work(mdsc);
4805 ceph_trim_snapid_map(mdsc);
4807 maybe_recover_session(mdsc);
4809 schedule_delayed(mdsc, delay);
4812 int ceph_mdsc_init(struct ceph_fs_client *fsc)
4815 struct ceph_mds_client *mdsc;
4818 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
4822 mutex_init(&mdsc->mutex);
4823 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
4824 if (!mdsc->mdsmap) {
4829 init_completion(&mdsc->safe_umount_waiters);
4830 init_waitqueue_head(&mdsc->session_close_wq);
4831 INIT_LIST_HEAD(&mdsc->waiting_for_map);
4832 mdsc->quotarealms_inodes = RB_ROOT;
4833 mutex_init(&mdsc->quotarealms_inodes_mutex);
4834 init_rwsem(&mdsc->snap_rwsem);
4835 mdsc->snap_realms = RB_ROOT;
4836 INIT_LIST_HEAD(&mdsc->snap_empty);
4837 spin_lock_init(&mdsc->snap_empty_lock);
4838 mdsc->request_tree = RB_ROOT;
4839 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
4840 mdsc->last_renew_caps = jiffies;
4841 INIT_LIST_HEAD(&mdsc->cap_delay_list);
4842 INIT_LIST_HEAD(&mdsc->cap_wait_list);
4843 spin_lock_init(&mdsc->cap_delay_lock);
4844 INIT_LIST_HEAD(&mdsc->snap_flush_list);
4845 spin_lock_init(&mdsc->snap_flush_lock);
4846 mdsc->last_cap_flush_tid = 1;
4847 INIT_LIST_HEAD(&mdsc->cap_flush_list);
4848 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
4849 spin_lock_init(&mdsc->cap_dirty_lock);
4850 init_waitqueue_head(&mdsc->cap_flushing_wq);
4851 INIT_WORK(&mdsc->cap_reclaim_work, ceph_cap_reclaim_work);
4852 err = ceph_metric_init(&mdsc->metric);
4856 spin_lock_init(&mdsc->dentry_list_lock);
4857 INIT_LIST_HEAD(&mdsc->dentry_leases);
4858 INIT_LIST_HEAD(&mdsc->dentry_dir_leases);
4860 ceph_caps_init(mdsc);
4861 ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
4863 spin_lock_init(&mdsc->snapid_map_lock);
4864 mdsc->snapid_map_tree = RB_ROOT;
4865 INIT_LIST_HEAD(&mdsc->snapid_map_lru);
4867 init_rwsem(&mdsc->pool_perm_rwsem);
4868 mdsc->pool_perm_tree = RB_ROOT;
4870 strscpy(mdsc->nodename, utsname()->nodename,
4871 sizeof(mdsc->nodename));
4877 kfree(mdsc->mdsmap);
4884 * Wait for safe replies on open mds requests. If we time out, drop
4885 * all requests from the tree to avoid dangling dentry refs.
4887 static void wait_requests(struct ceph_mds_client *mdsc)
4889 struct ceph_options *opts = mdsc->fsc->client->options;
4890 struct ceph_mds_request *req;
4892 mutex_lock(&mdsc->mutex);
4893 if (__get_oldest_req(mdsc)) {
4894 mutex_unlock(&mdsc->mutex);
4896 dout("wait_requests waiting for requests\n");
4897 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
4898 ceph_timeout_jiffies(opts->mount_timeout));
4900 /* tear down remaining requests */
4901 mutex_lock(&mdsc->mutex);
4902 while ((req = __get_oldest_req(mdsc))) {
4903 dout("wait_requests timed out on tid %llu\n",
4905 list_del_init(&req->r_wait);
4906 __unregister_request(mdsc, req);
4909 mutex_unlock(&mdsc->mutex);
4910 dout("wait_requests done\n");
4913 void send_flush_mdlog(struct ceph_mds_session *s)
4915 struct ceph_msg *msg;
4918 * Pre-luminous MDS crashes when it sees an unknown session request
4920 if (!CEPH_HAVE_FEATURE(s->s_con.peer_features, SERVER_LUMINOUS))
4923 mutex_lock(&s->s_mutex);
4924 dout("request mdlog flush to mds%d (%s)s seq %lld\n", s->s_mds,
4925 ceph_session_state_name(s->s_state), s->s_seq);
4926 msg = ceph_create_session_msg(CEPH_SESSION_REQUEST_FLUSH_MDLOG,
4929 pr_err("failed to request mdlog flush to mds%d (%s) seq %lld\n",
4930 s->s_mds, ceph_session_state_name(s->s_state), s->s_seq);
4932 ceph_con_send(&s->s_con, msg);
4934 mutex_unlock(&s->s_mutex);
4938 * called before mount is ro, and before dentries are torn down.
4939 * (hmm, does this still race with new lookups?)
4941 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
4943 dout("pre_umount\n");
4946 ceph_mdsc_iterate_sessions(mdsc, send_flush_mdlog, true);
4947 ceph_mdsc_iterate_sessions(mdsc, lock_unlock_session, false);
4948 ceph_flush_dirty_caps(mdsc);
4949 wait_requests(mdsc);
4952 * wait for reply handlers to drop their request refs and
4953 * their inode/dcache refs
4957 ceph_cleanup_quotarealms_inodes(mdsc);
4961 * flush the mdlog and wait for all write mds requests to flush.
4963 static void flush_mdlog_and_wait_mdsc_unsafe_requests(struct ceph_mds_client *mdsc,
4966 struct ceph_mds_request *req = NULL, *nextreq;
4967 struct ceph_mds_session *last_session = NULL;
4970 mutex_lock(&mdsc->mutex);
4971 dout("%s want %lld\n", __func__, want_tid);
4973 req = __get_oldest_req(mdsc);
4974 while (req && req->r_tid <= want_tid) {
4975 /* find next request */
4976 n = rb_next(&req->r_node);
4978 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
4981 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
4982 (req->r_op & CEPH_MDS_OP_WRITE)) {
4983 struct ceph_mds_session *s = req->r_session;
4991 ceph_mdsc_get_request(req);
4993 ceph_mdsc_get_request(nextreq);
4994 s = ceph_get_mds_session(s);
4995 mutex_unlock(&mdsc->mutex);
4997 /* send flush mdlog request to MDS */
4998 if (last_session != s) {
4999 send_flush_mdlog(s);
5000 ceph_put_mds_session(last_session);
5003 ceph_put_mds_session(s);
5005 dout("%s wait on %llu (want %llu)\n", __func__,
5006 req->r_tid, want_tid);
5007 wait_for_completion(&req->r_safe_completion);
5009 mutex_lock(&mdsc->mutex);
5010 ceph_mdsc_put_request(req);
5012 break; /* next dne before, so we're done! */
5013 if (RB_EMPTY_NODE(&nextreq->r_node)) {
5014 /* next request was removed from tree */
5015 ceph_mdsc_put_request(nextreq);
5018 ceph_mdsc_put_request(nextreq); /* won't go away */
5022 mutex_unlock(&mdsc->mutex);
5023 ceph_put_mds_session(last_session);
5024 dout("%s done\n", __func__);
5027 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
5029 u64 want_tid, want_flush;
5031 if (READ_ONCE(mdsc->fsc->mount_state) >= CEPH_MOUNT_SHUTDOWN)
5035 mutex_lock(&mdsc->mutex);
5036 want_tid = mdsc->last_tid;
5037 mutex_unlock(&mdsc->mutex);
5039 ceph_flush_dirty_caps(mdsc);
5040 spin_lock(&mdsc->cap_dirty_lock);
5041 want_flush = mdsc->last_cap_flush_tid;
5042 if (!list_empty(&mdsc->cap_flush_list)) {
5043 struct ceph_cap_flush *cf =
5044 list_last_entry(&mdsc->cap_flush_list,
5045 struct ceph_cap_flush, g_list);
5048 spin_unlock(&mdsc->cap_dirty_lock);
5050 dout("sync want tid %lld flush_seq %lld\n",
5051 want_tid, want_flush);
5053 flush_mdlog_and_wait_mdsc_unsafe_requests(mdsc, want_tid);
5054 wait_caps_flush(mdsc, want_flush);
5058 * true if all sessions are closed, or we force unmount
5060 static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
5062 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
5064 return atomic_read(&mdsc->num_sessions) <= skipped;
5068 * called after sb is ro or when metadata corrupted.
5070 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
5072 struct ceph_options *opts = mdsc->fsc->client->options;
5073 struct ceph_mds_session *session;
5077 dout("close_sessions\n");
5079 /* close sessions */
5080 mutex_lock(&mdsc->mutex);
5081 for (i = 0; i < mdsc->max_sessions; i++) {
5082 session = __ceph_lookup_mds_session(mdsc, i);
5085 mutex_unlock(&mdsc->mutex);
5086 mutex_lock(&session->s_mutex);
5087 if (__close_session(mdsc, session) <= 0)
5089 mutex_unlock(&session->s_mutex);
5090 ceph_put_mds_session(session);
5091 mutex_lock(&mdsc->mutex);
5093 mutex_unlock(&mdsc->mutex);
5095 dout("waiting for sessions to close\n");
5096 wait_event_timeout(mdsc->session_close_wq,
5097 done_closing_sessions(mdsc, skipped),
5098 ceph_timeout_jiffies(opts->mount_timeout));
5100 /* tear down remaining sessions */
5101 mutex_lock(&mdsc->mutex);
5102 for (i = 0; i < mdsc->max_sessions; i++) {
5103 if (mdsc->sessions[i]) {
5104 session = ceph_get_mds_session(mdsc->sessions[i]);
5105 __unregister_session(mdsc, session);
5106 mutex_unlock(&mdsc->mutex);
5107 mutex_lock(&session->s_mutex);
5108 remove_session_caps(session);
5109 mutex_unlock(&session->s_mutex);
5110 ceph_put_mds_session(session);
5111 mutex_lock(&mdsc->mutex);
5114 WARN_ON(!list_empty(&mdsc->cap_delay_list));
5115 mutex_unlock(&mdsc->mutex);
5117 ceph_cleanup_snapid_map(mdsc);
5118 ceph_cleanup_global_and_empty_realms(mdsc);
5120 cancel_work_sync(&mdsc->cap_reclaim_work);
5121 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
5126 void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
5128 struct ceph_mds_session *session;
5131 dout("force umount\n");
5133 mutex_lock(&mdsc->mutex);
5134 for (mds = 0; mds < mdsc->max_sessions; mds++) {
5135 session = __ceph_lookup_mds_session(mdsc, mds);
5139 if (session->s_state == CEPH_MDS_SESSION_REJECTED)
5140 __unregister_session(mdsc, session);
5141 __wake_requests(mdsc, &session->s_waiting);
5142 mutex_unlock(&mdsc->mutex);
5144 mutex_lock(&session->s_mutex);
5145 __close_session(mdsc, session);
5146 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
5147 cleanup_session_requests(mdsc, session);
5148 remove_session_caps(session);
5150 mutex_unlock(&session->s_mutex);
5151 ceph_put_mds_session(session);
5153 mutex_lock(&mdsc->mutex);
5154 kick_requests(mdsc, mds);
5156 __wake_requests(mdsc, &mdsc->waiting_for_map);
5157 mutex_unlock(&mdsc->mutex);
5160 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
5164 * Make sure the delayed work stopped before releasing
5167 * Because the cancel_delayed_work_sync() will only
5168 * guarantee that the work finishes executing. But the
5169 * delayed work will re-arm itself again after that.
5171 flush_delayed_work(&mdsc->delayed_work);
5174 ceph_mdsmap_destroy(mdsc->mdsmap);
5175 kfree(mdsc->sessions);
5176 ceph_caps_finalize(mdsc);
5177 ceph_pool_perm_destroy(mdsc);
5180 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
5182 struct ceph_mds_client *mdsc = fsc->mdsc;
5183 dout("mdsc_destroy %p\n", mdsc);
5188 /* flush out any connection work with references to us */
5191 ceph_mdsc_stop(mdsc);
5193 ceph_metric_destroy(&mdsc->metric);
5197 dout("mdsc_destroy %p done\n", mdsc);
5200 void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
5202 struct ceph_fs_client *fsc = mdsc->fsc;
5203 const char *mds_namespace = fsc->mount_options->mds_namespace;
5204 void *p = msg->front.iov_base;
5205 void *end = p + msg->front.iov_len;
5208 u32 mount_fscid = (u32)-1;
5211 ceph_decode_need(&p, end, sizeof(u32), bad);
5212 epoch = ceph_decode_32(&p);
5214 dout("handle_fsmap epoch %u\n", epoch);
5216 /* struct_v, struct_cv, map_len, epoch, legacy_client_fscid */
5217 ceph_decode_skip_n(&p, end, 2 + sizeof(u32) * 3, bad);
5219 ceph_decode_32_safe(&p, end, num_fs, bad);
5220 while (num_fs-- > 0) {
5221 void *info_p, *info_end;
5225 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
5226 p += 2; // info_v, info_cv
5227 info_len = ceph_decode_32(&p);
5228 ceph_decode_need(&p, end, info_len, bad);
5230 info_end = p + info_len;
5233 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
5234 fscid = ceph_decode_32(&info_p);
5235 namelen = ceph_decode_32(&info_p);
5236 ceph_decode_need(&info_p, info_end, namelen, bad);
5238 if (mds_namespace &&
5239 strlen(mds_namespace) == namelen &&
5240 !strncmp(mds_namespace, (char *)info_p, namelen)) {
5241 mount_fscid = fscid;
5246 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
5247 if (mount_fscid != (u32)-1) {
5248 fsc->client->monc.fs_cluster_id = mount_fscid;
5249 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
5251 ceph_monc_renew_subs(&fsc->client->monc);
5259 pr_err("error decoding fsmap %d. Shutting down mount.\n", err);
5260 ceph_umount_begin(mdsc->fsc->sb);
5262 mutex_lock(&mdsc->mutex);
5263 mdsc->mdsmap_err = err;
5264 __wake_requests(mdsc, &mdsc->waiting_for_map);
5265 mutex_unlock(&mdsc->mutex);
5269 * handle mds map update.
5271 void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
5275 void *p = msg->front.iov_base;
5276 void *end = p + msg->front.iov_len;
5277 struct ceph_mdsmap *newmap, *oldmap;
5278 struct ceph_fsid fsid;
5281 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
5282 ceph_decode_copy(&p, &fsid, sizeof(fsid));
5283 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
5285 epoch = ceph_decode_32(&p);
5286 maplen = ceph_decode_32(&p);
5287 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
5289 /* do we need it? */
5290 mutex_lock(&mdsc->mutex);
5291 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
5292 dout("handle_map epoch %u <= our %u\n",
5293 epoch, mdsc->mdsmap->m_epoch);
5294 mutex_unlock(&mdsc->mutex);
5298 newmap = ceph_mdsmap_decode(&p, end, ceph_msgr2(mdsc->fsc->client));
5299 if (IS_ERR(newmap)) {
5300 err = PTR_ERR(newmap);
5304 /* swap into place */
5306 oldmap = mdsc->mdsmap;
5307 mdsc->mdsmap = newmap;
5308 check_new_map(mdsc, newmap, oldmap);
5309 ceph_mdsmap_destroy(oldmap);
5311 mdsc->mdsmap = newmap; /* first mds map */
5313 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
5316 __wake_requests(mdsc, &mdsc->waiting_for_map);
5317 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
5318 mdsc->mdsmap->m_epoch);
5320 mutex_unlock(&mdsc->mutex);
5321 schedule_delayed(mdsc, 0);
5325 mutex_unlock(&mdsc->mutex);
5327 pr_err("error decoding mdsmap %d. Shutting down mount.\n", err);
5328 ceph_umount_begin(mdsc->fsc->sb);
5332 static struct ceph_connection *mds_get_con(struct ceph_connection *con)
5334 struct ceph_mds_session *s = con->private;
5336 if (ceph_get_mds_session(s))
5341 static void mds_put_con(struct ceph_connection *con)
5343 struct ceph_mds_session *s = con->private;
5345 ceph_put_mds_session(s);
5349 * if the client is unresponsive for long enough, the mds will kill
5350 * the session entirely.
5352 static void mds_peer_reset(struct ceph_connection *con)
5354 struct ceph_mds_session *s = con->private;
5355 struct ceph_mds_client *mdsc = s->s_mdsc;
5357 pr_warn("mds%d closed our session\n", s->s_mds);
5358 if (READ_ONCE(mdsc->fsc->mount_state) != CEPH_MOUNT_FENCE_IO)
5359 send_mds_reconnect(mdsc, s);
5362 static void mds_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
5364 struct ceph_mds_session *s = con->private;
5365 struct ceph_mds_client *mdsc = s->s_mdsc;
5366 int type = le16_to_cpu(msg->hdr.type);
5368 mutex_lock(&mdsc->mutex);
5369 if (__verify_registered_session(mdsc, s) < 0) {
5370 mutex_unlock(&mdsc->mutex);
5373 mutex_unlock(&mdsc->mutex);
5376 case CEPH_MSG_MDS_MAP:
5377 ceph_mdsc_handle_mdsmap(mdsc, msg);
5379 case CEPH_MSG_FS_MAP_USER:
5380 ceph_mdsc_handle_fsmap(mdsc, msg);
5382 case CEPH_MSG_CLIENT_SESSION:
5383 handle_session(s, msg);
5385 case CEPH_MSG_CLIENT_REPLY:
5386 handle_reply(s, msg);
5388 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
5389 handle_forward(mdsc, s, msg);
5391 case CEPH_MSG_CLIENT_CAPS:
5392 ceph_handle_caps(s, msg);
5394 case CEPH_MSG_CLIENT_SNAP:
5395 ceph_handle_snap(mdsc, s, msg);
5397 case CEPH_MSG_CLIENT_LEASE:
5398 handle_lease(mdsc, s, msg);
5400 case CEPH_MSG_CLIENT_QUOTA:
5401 ceph_handle_quota(mdsc, s, msg);
5405 pr_err("received unknown message type %d %s\n", type,
5406 ceph_msg_type_name(type));
5417 * Note: returned pointer is the address of a structure that's
5418 * managed separately. Caller must *not* attempt to free it.
5420 static struct ceph_auth_handshake *
5421 mds_get_authorizer(struct ceph_connection *con, int *proto, int force_new)
5423 struct ceph_mds_session *s = con->private;
5424 struct ceph_mds_client *mdsc = s->s_mdsc;
5425 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
5426 struct ceph_auth_handshake *auth = &s->s_auth;
5429 ret = __ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_MDS,
5430 force_new, proto, NULL, NULL);
5432 return ERR_PTR(ret);
5437 static int mds_add_authorizer_challenge(struct ceph_connection *con,
5438 void *challenge_buf, int challenge_buf_len)
5440 struct ceph_mds_session *s = con->private;
5441 struct ceph_mds_client *mdsc = s->s_mdsc;
5442 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
5444 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
5445 challenge_buf, challenge_buf_len);
5448 static int mds_verify_authorizer_reply(struct ceph_connection *con)
5450 struct ceph_mds_session *s = con->private;
5451 struct ceph_mds_client *mdsc = s->s_mdsc;
5452 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
5453 struct ceph_auth_handshake *auth = &s->s_auth;
5455 return ceph_auth_verify_authorizer_reply(ac, auth->authorizer,
5456 auth->authorizer_reply_buf, auth->authorizer_reply_buf_len,
5457 NULL, NULL, NULL, NULL);
5460 static int mds_invalidate_authorizer(struct ceph_connection *con)
5462 struct ceph_mds_session *s = con->private;
5463 struct ceph_mds_client *mdsc = s->s_mdsc;
5464 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
5466 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
5468 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
5471 static int mds_get_auth_request(struct ceph_connection *con,
5472 void *buf, int *buf_len,
5473 void **authorizer, int *authorizer_len)
5475 struct ceph_mds_session *s = con->private;
5476 struct ceph_auth_client *ac = s->s_mdsc->fsc->client->monc.auth;
5477 struct ceph_auth_handshake *auth = &s->s_auth;
5480 ret = ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_MDS,
5485 *authorizer = auth->authorizer_buf;
5486 *authorizer_len = auth->authorizer_buf_len;
5490 static int mds_handle_auth_reply_more(struct ceph_connection *con,
5491 void *reply, int reply_len,
5492 void *buf, int *buf_len,
5493 void **authorizer, int *authorizer_len)
5495 struct ceph_mds_session *s = con->private;
5496 struct ceph_auth_client *ac = s->s_mdsc->fsc->client->monc.auth;
5497 struct ceph_auth_handshake *auth = &s->s_auth;
5500 ret = ceph_auth_handle_svc_reply_more(ac, auth, reply, reply_len,
5505 *authorizer = auth->authorizer_buf;
5506 *authorizer_len = auth->authorizer_buf_len;
5510 static int mds_handle_auth_done(struct ceph_connection *con,
5511 u64 global_id, void *reply, int reply_len,
5512 u8 *session_key, int *session_key_len,
5513 u8 *con_secret, int *con_secret_len)
5515 struct ceph_mds_session *s = con->private;
5516 struct ceph_auth_client *ac = s->s_mdsc->fsc->client->monc.auth;
5517 struct ceph_auth_handshake *auth = &s->s_auth;
5519 return ceph_auth_handle_svc_reply_done(ac, auth, reply, reply_len,
5520 session_key, session_key_len,
5521 con_secret, con_secret_len);
5524 static int mds_handle_auth_bad_method(struct ceph_connection *con,
5525 int used_proto, int result,
5526 const int *allowed_protos, int proto_cnt,
5527 const int *allowed_modes, int mode_cnt)
5529 struct ceph_mds_session *s = con->private;
5530 struct ceph_mon_client *monc = &s->s_mdsc->fsc->client->monc;
5533 if (ceph_auth_handle_bad_authorizer(monc->auth, CEPH_ENTITY_TYPE_MDS,
5535 allowed_protos, proto_cnt,
5536 allowed_modes, mode_cnt)) {
5537 ret = ceph_monc_validate_auth(monc);
5545 static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
5546 struct ceph_msg_header *hdr, int *skip)
5548 struct ceph_msg *msg;
5549 int type = (int) le16_to_cpu(hdr->type);
5550 int front_len = (int) le32_to_cpu(hdr->front_len);
5556 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
5558 pr_err("unable to allocate msg type %d len %d\n",
5566 static int mds_sign_message(struct ceph_msg *msg)
5568 struct ceph_mds_session *s = msg->con->private;
5569 struct ceph_auth_handshake *auth = &s->s_auth;
5571 return ceph_auth_sign_message(auth, msg);
5574 static int mds_check_message_signature(struct ceph_msg *msg)
5576 struct ceph_mds_session *s = msg->con->private;
5577 struct ceph_auth_handshake *auth = &s->s_auth;
5579 return ceph_auth_check_message_signature(auth, msg);
5582 static const struct ceph_connection_operations mds_con_ops = {
5585 .alloc_msg = mds_alloc_msg,
5586 .dispatch = mds_dispatch,
5587 .peer_reset = mds_peer_reset,
5588 .get_authorizer = mds_get_authorizer,
5589 .add_authorizer_challenge = mds_add_authorizer_challenge,
5590 .verify_authorizer_reply = mds_verify_authorizer_reply,
5591 .invalidate_authorizer = mds_invalidate_authorizer,
5592 .sign_message = mds_sign_message,
5593 .check_message_signature = mds_check_message_signature,
5594 .get_auth_request = mds_get_auth_request,
5595 .handle_auth_reply_more = mds_handle_auth_reply_more,
5596 .handle_auth_done = mds_handle_auth_done,
5597 .handle_auth_bad_method = mds_handle_auth_bad_method,