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);
652 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
654 if (!info->dir_entries)
656 free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
660 * In async unlink case the kclient won't wait for the first reply
661 * from MDS and just drop all the links and unhash the dentry and then
662 * succeeds immediately.
664 * For any new create/link/rename,etc requests followed by using the
665 * same file names we must wait for the first reply of the inflight
666 * unlink request, or the MDS possibly will fail these following
667 * requests with -EEXIST if the inflight async unlink request was
668 * delayed for some reasons.
670 * And the worst case is that for the none async openc request it will
671 * successfully open the file if the CDentry hasn't been unlinked yet,
672 * but later the previous delayed async unlink request will remove the
673 * CDenty. That means the just created file is possiblly deleted later
676 * We need to wait for the inflight async unlink requests to finish
677 * when creating new files/directories by using the same file names.
679 int ceph_wait_on_conflict_unlink(struct dentry *dentry)
681 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
682 struct dentry *pdentry = dentry->d_parent;
683 struct dentry *udentry, *found = NULL;
684 struct ceph_dentry_info *di;
686 u32 hash = dentry->d_name.hash;
689 dname.name = dentry->d_name.name;
690 dname.len = dentry->d_name.len;
693 hash_for_each_possible_rcu(fsc->async_unlink_conflict, di,
695 udentry = di->dentry;
697 spin_lock(&udentry->d_lock);
698 if (udentry->d_name.hash != hash)
700 if (unlikely(udentry->d_parent != pdentry))
702 if (!hash_hashed(&di->hnode))
705 if (!test_bit(CEPH_DENTRY_ASYNC_UNLINK_BIT, &di->flags))
706 pr_warn("%s dentry %p:%pd async unlink bit is not set\n",
707 __func__, dentry, dentry);
709 if (!d_same_name(udentry, pdentry, &dname))
712 spin_unlock(&udentry->d_lock);
713 found = dget(udentry);
716 spin_unlock(&udentry->d_lock);
723 dout("%s dentry %p:%pd conflict with old %p:%pd\n", __func__,
724 dentry, dentry, found, found);
726 err = wait_on_bit(&di->flags, CEPH_DENTRY_ASYNC_UNLINK_BIT,
736 const char *ceph_session_state_name(int s)
739 case CEPH_MDS_SESSION_NEW: return "new";
740 case CEPH_MDS_SESSION_OPENING: return "opening";
741 case CEPH_MDS_SESSION_OPEN: return "open";
742 case CEPH_MDS_SESSION_HUNG: return "hung";
743 case CEPH_MDS_SESSION_CLOSING: return "closing";
744 case CEPH_MDS_SESSION_CLOSED: return "closed";
745 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
746 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
747 case CEPH_MDS_SESSION_REJECTED: return "rejected";
748 default: return "???";
752 struct ceph_mds_session *ceph_get_mds_session(struct ceph_mds_session *s)
754 if (refcount_inc_not_zero(&s->s_ref))
759 void ceph_put_mds_session(struct ceph_mds_session *s)
761 if (IS_ERR_OR_NULL(s))
764 if (refcount_dec_and_test(&s->s_ref)) {
765 if (s->s_auth.authorizer)
766 ceph_auth_destroy_authorizer(s->s_auth.authorizer);
767 WARN_ON(mutex_is_locked(&s->s_mutex));
768 xa_destroy(&s->s_delegated_inos);
774 * called under mdsc->mutex
776 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
779 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
781 return ceph_get_mds_session(mdsc->sessions[mds]);
784 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
786 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
792 static int __verify_registered_session(struct ceph_mds_client *mdsc,
793 struct ceph_mds_session *s)
795 if (s->s_mds >= mdsc->max_sessions ||
796 mdsc->sessions[s->s_mds] != s)
802 * create+register a new session for given mds.
803 * called under mdsc->mutex.
805 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
808 struct ceph_mds_session *s;
810 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_FENCE_IO)
811 return ERR_PTR(-EIO);
813 if (mds >= mdsc->mdsmap->possible_max_rank)
814 return ERR_PTR(-EINVAL);
816 s = kzalloc(sizeof(*s), GFP_NOFS);
818 return ERR_PTR(-ENOMEM);
820 if (mds >= mdsc->max_sessions) {
821 int newmax = 1 << get_count_order(mds + 1);
822 struct ceph_mds_session **sa;
824 dout("%s: realloc to %d\n", __func__, newmax);
825 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
828 if (mdsc->sessions) {
829 memcpy(sa, mdsc->sessions,
830 mdsc->max_sessions * sizeof(void *));
831 kfree(mdsc->sessions);
834 mdsc->max_sessions = newmax;
837 dout("%s: mds%d\n", __func__, mds);
840 s->s_state = CEPH_MDS_SESSION_NEW;
841 mutex_init(&s->s_mutex);
843 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
845 atomic_set(&s->s_cap_gen, 1);
846 s->s_cap_ttl = jiffies - 1;
848 spin_lock_init(&s->s_cap_lock);
849 INIT_LIST_HEAD(&s->s_caps);
850 refcount_set(&s->s_ref, 1);
851 INIT_LIST_HEAD(&s->s_waiting);
852 INIT_LIST_HEAD(&s->s_unsafe);
853 xa_init(&s->s_delegated_inos);
854 INIT_LIST_HEAD(&s->s_cap_releases);
855 INIT_WORK(&s->s_cap_release_work, ceph_cap_release_work);
857 INIT_LIST_HEAD(&s->s_cap_dirty);
858 INIT_LIST_HEAD(&s->s_cap_flushing);
860 mdsc->sessions[mds] = s;
861 atomic_inc(&mdsc->num_sessions);
862 refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
864 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
865 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
871 return ERR_PTR(-ENOMEM);
875 * called under mdsc->mutex
877 static void __unregister_session(struct ceph_mds_client *mdsc,
878 struct ceph_mds_session *s)
880 dout("__unregister_session mds%d %p\n", s->s_mds, s);
881 BUG_ON(mdsc->sessions[s->s_mds] != s);
882 mdsc->sessions[s->s_mds] = NULL;
883 ceph_con_close(&s->s_con);
884 ceph_put_mds_session(s);
885 atomic_dec(&mdsc->num_sessions);
889 * drop session refs in request.
891 * should be last request ref, or hold mdsc->mutex
893 static void put_request_session(struct ceph_mds_request *req)
895 if (req->r_session) {
896 ceph_put_mds_session(req->r_session);
897 req->r_session = NULL;
901 void ceph_mdsc_iterate_sessions(struct ceph_mds_client *mdsc,
902 void (*cb)(struct ceph_mds_session *),
907 mutex_lock(&mdsc->mutex);
908 for (mds = 0; mds < mdsc->max_sessions; ++mds) {
909 struct ceph_mds_session *s;
911 s = __ceph_lookup_mds_session(mdsc, mds);
915 if (check_state && !check_session_state(s)) {
916 ceph_put_mds_session(s);
920 mutex_unlock(&mdsc->mutex);
922 ceph_put_mds_session(s);
923 mutex_lock(&mdsc->mutex);
925 mutex_unlock(&mdsc->mutex);
928 void ceph_mdsc_release_request(struct kref *kref)
930 struct ceph_mds_request *req = container_of(kref,
931 struct ceph_mds_request,
933 ceph_mdsc_release_dir_caps_no_check(req);
934 destroy_reply_info(&req->r_reply_info);
936 ceph_msg_put(req->r_request);
938 ceph_msg_put(req->r_reply);
940 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
944 ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
947 iput(req->r_target_inode);
950 if (req->r_old_dentry)
951 dput(req->r_old_dentry);
952 if (req->r_old_dentry_dir) {
954 * track (and drop pins for) r_old_dentry_dir
955 * separately, since r_old_dentry's d_parent may have
956 * changed between the dir mutex being dropped and
957 * this request being freed.
959 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
961 iput(req->r_old_dentry_dir);
965 put_cred(req->r_cred);
967 ceph_pagelist_release(req->r_pagelist);
968 put_request_session(req);
969 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
970 WARN_ON_ONCE(!list_empty(&req->r_wait));
971 kmem_cache_free(ceph_mds_request_cachep, req);
974 DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
977 * lookup session, bump ref if found.
979 * called under mdsc->mutex.
981 static struct ceph_mds_request *
982 lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
984 struct ceph_mds_request *req;
986 req = lookup_request(&mdsc->request_tree, tid);
988 ceph_mdsc_get_request(req);
994 * Register an in-flight request, and assign a tid. Link to directory
995 * are modifying (if any).
997 * Called under mdsc->mutex.
999 static void __register_request(struct ceph_mds_client *mdsc,
1000 struct ceph_mds_request *req,
1005 req->r_tid = ++mdsc->last_tid;
1006 if (req->r_num_caps) {
1007 ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
1010 pr_err("__register_request %p "
1011 "failed to reserve caps: %d\n", req, ret);
1012 /* set req->r_err to fail early from __do_request */
1017 dout("__register_request %p tid %lld\n", req, req->r_tid);
1018 ceph_mdsc_get_request(req);
1019 insert_request(&mdsc->request_tree, req);
1021 req->r_cred = get_current_cred();
1023 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
1024 mdsc->oldest_tid = req->r_tid;
1027 struct ceph_inode_info *ci = ceph_inode(dir);
1030 req->r_unsafe_dir = dir;
1031 spin_lock(&ci->i_unsafe_lock);
1032 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
1033 spin_unlock(&ci->i_unsafe_lock);
1037 static void __unregister_request(struct ceph_mds_client *mdsc,
1038 struct ceph_mds_request *req)
1040 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1042 /* Never leave an unregistered request on an unsafe list! */
1043 list_del_init(&req->r_unsafe_item);
1045 if (req->r_tid == mdsc->oldest_tid) {
1046 struct rb_node *p = rb_next(&req->r_node);
1047 mdsc->oldest_tid = 0;
1049 struct ceph_mds_request *next_req =
1050 rb_entry(p, struct ceph_mds_request, r_node);
1051 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
1052 mdsc->oldest_tid = next_req->r_tid;
1059 erase_request(&mdsc->request_tree, req);
1061 if (req->r_unsafe_dir) {
1062 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
1063 spin_lock(&ci->i_unsafe_lock);
1064 list_del_init(&req->r_unsafe_dir_item);
1065 spin_unlock(&ci->i_unsafe_lock);
1067 if (req->r_target_inode &&
1068 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
1069 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
1070 spin_lock(&ci->i_unsafe_lock);
1071 list_del_init(&req->r_unsafe_target_item);
1072 spin_unlock(&ci->i_unsafe_lock);
1075 if (req->r_unsafe_dir) {
1076 iput(req->r_unsafe_dir);
1077 req->r_unsafe_dir = NULL;
1080 complete_all(&req->r_safe_completion);
1082 ceph_mdsc_put_request(req);
1086 * Walk back up the dentry tree until we hit a dentry representing a
1087 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
1088 * when calling this) to ensure that the objects won't disappear while we're
1089 * working with them. Once we hit a candidate dentry, we attempt to take a
1090 * reference to it, and return that as the result.
1092 static struct inode *get_nonsnap_parent(struct dentry *dentry)
1094 struct inode *inode = NULL;
1096 while (dentry && !IS_ROOT(dentry)) {
1097 inode = d_inode_rcu(dentry);
1098 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
1100 dentry = dentry->d_parent;
1103 inode = igrab(inode);
1108 * Choose mds to send request to next. If there is a hint set in the
1109 * request (e.g., due to a prior forward hint from the mds), use that.
1110 * Otherwise, consult frag tree and/or caps to identify the
1111 * appropriate mds. If all else fails, choose randomly.
1113 * Called under mdsc->mutex.
1115 static int __choose_mds(struct ceph_mds_client *mdsc,
1116 struct ceph_mds_request *req,
1119 struct inode *inode;
1120 struct ceph_inode_info *ci;
1121 struct ceph_cap *cap;
1122 int mode = req->r_direct_mode;
1124 u32 hash = req->r_direct_hash;
1125 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
1131 * is there a specific mds we should try? ignore hint if we have
1132 * no session and the mds is not up (active or recovering).
1134 if (req->r_resend_mds >= 0 &&
1135 (__have_session(mdsc, req->r_resend_mds) ||
1136 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
1137 dout("%s using resend_mds mds%d\n", __func__,
1139 return req->r_resend_mds;
1142 if (mode == USE_RANDOM_MDS)
1147 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
1148 inode = req->r_inode;
1151 /* req->r_dentry is non-null for LSSNAP request */
1153 inode = get_nonsnap_parent(req->r_dentry);
1155 dout("%s using snapdir's parent %p\n", __func__, inode);
1157 } else if (req->r_dentry) {
1158 /* ignore race with rename; old or new d_parent is okay */
1159 struct dentry *parent;
1163 parent = READ_ONCE(req->r_dentry->d_parent);
1164 dir = req->r_parent ? : d_inode_rcu(parent);
1166 if (!dir || dir->i_sb != mdsc->fsc->sb) {
1167 /* not this fs or parent went negative */
1168 inode = d_inode(req->r_dentry);
1171 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
1172 /* direct snapped/virtual snapdir requests
1173 * based on parent dir inode */
1174 inode = get_nonsnap_parent(parent);
1175 dout("%s using nonsnap parent %p\n", __func__, inode);
1178 inode = d_inode(req->r_dentry);
1179 if (!inode || mode == USE_AUTH_MDS) {
1182 hash = ceph_dentry_hash(dir, req->r_dentry);
1191 dout("%s %p is_hash=%d (0x%x) mode %d\n", __func__, inode, (int)is_hash,
1195 ci = ceph_inode(inode);
1197 if (is_hash && S_ISDIR(inode->i_mode)) {
1198 struct ceph_inode_frag frag;
1201 ceph_choose_frag(ci, hash, &frag, &found);
1203 if (mode == USE_ANY_MDS && frag.ndist > 0) {
1206 /* choose a random replica */
1207 get_random_bytes(&r, 1);
1210 dout("%s %p %llx.%llx frag %u mds%d (%d/%d)\n",
1211 __func__, inode, ceph_vinop(inode),
1212 frag.frag, mds, (int)r, frag.ndist);
1213 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
1214 CEPH_MDS_STATE_ACTIVE &&
1215 !ceph_mdsmap_is_laggy(mdsc->mdsmap, mds))
1219 /* since this file/dir wasn't known to be
1220 * replicated, then we want to look for the
1221 * authoritative mds. */
1222 if (frag.mds >= 0) {
1223 /* choose auth mds */
1225 dout("%s %p %llx.%llx frag %u mds%d (auth)\n",
1226 __func__, inode, ceph_vinop(inode),
1228 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
1229 CEPH_MDS_STATE_ACTIVE) {
1230 if (!ceph_mdsmap_is_laggy(mdsc->mdsmap,
1235 mode = USE_AUTH_MDS;
1239 spin_lock(&ci->i_ceph_lock);
1241 if (mode == USE_AUTH_MDS)
1242 cap = ci->i_auth_cap;
1243 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
1244 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
1246 spin_unlock(&ci->i_ceph_lock);
1250 mds = cap->session->s_mds;
1251 dout("%s %p %llx.%llx mds%d (%scap %p)\n", __func__,
1252 inode, ceph_vinop(inode), mds,
1253 cap == ci->i_auth_cap ? "auth " : "", cap);
1254 spin_unlock(&ci->i_ceph_lock);
1263 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
1264 dout("%s chose random mds%d\n", __func__, mds);
1272 struct ceph_msg *ceph_create_session_msg(u32 op, u64 seq)
1274 struct ceph_msg *msg;
1275 struct ceph_mds_session_head *h;
1277 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
1280 pr_err("ENOMEM creating session %s msg\n",
1281 ceph_session_op_name(op));
1284 h = msg->front.iov_base;
1285 h->op = cpu_to_le32(op);
1286 h->seq = cpu_to_le64(seq);
1291 static const unsigned char feature_bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
1292 #define FEATURE_BYTES(c) (DIV_ROUND_UP((size_t)feature_bits[c - 1] + 1, 64) * 8)
1293 static int encode_supported_features(void **p, void *end)
1295 static const size_t count = ARRAY_SIZE(feature_bits);
1299 size_t size = FEATURE_BYTES(count);
1302 if (WARN_ON_ONCE(*p + 4 + size > end))
1305 ceph_encode_32(p, size);
1306 memset(*p, 0, size);
1307 for (i = 0; i < count; i++) {
1308 bit = feature_bits[i];
1309 ((unsigned char *)(*p))[bit / 8] |= BIT(bit % 8);
1313 if (WARN_ON_ONCE(*p + 4 > end))
1316 ceph_encode_32(p, 0);
1322 static const unsigned char metric_bits[] = CEPHFS_METRIC_SPEC_CLIENT_SUPPORTED;
1323 #define METRIC_BYTES(cnt) (DIV_ROUND_UP((size_t)metric_bits[cnt - 1] + 1, 64) * 8)
1324 static int encode_metric_spec(void **p, void *end)
1326 static const size_t count = ARRAY_SIZE(metric_bits);
1329 if (WARN_ON_ONCE(*p + 2 > end))
1332 ceph_encode_8(p, 1); /* version */
1333 ceph_encode_8(p, 1); /* compat */
1337 size_t size = METRIC_BYTES(count);
1339 if (WARN_ON_ONCE(*p + 4 + 4 + size > end))
1342 /* metric spec info length */
1343 ceph_encode_32(p, 4 + size);
1346 ceph_encode_32(p, size);
1347 memset(*p, 0, size);
1348 for (i = 0; i < count; i++)
1349 ((unsigned char *)(*p))[i / 8] |= BIT(metric_bits[i] % 8);
1352 if (WARN_ON_ONCE(*p + 4 + 4 > end))
1355 /* metric spec info length */
1356 ceph_encode_32(p, 4);
1358 ceph_encode_32(p, 0);
1365 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
1366 * to include additional client metadata fields.
1368 static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
1370 struct ceph_msg *msg;
1371 struct ceph_mds_session_head *h;
1373 int extra_bytes = 0;
1374 int metadata_key_count = 0;
1375 struct ceph_options *opt = mdsc->fsc->client->options;
1376 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
1381 const char* metadata[][2] = {
1382 {"hostname", mdsc->nodename},
1383 {"kernel_version", init_utsname()->release},
1384 {"entity_id", opt->name ? : ""},
1385 {"root", fsopt->server_path ? : "/"},
1389 /* Calculate serialized length of metadata */
1390 extra_bytes = 4; /* map length */
1391 for (i = 0; metadata[i][0]; ++i) {
1392 extra_bytes += 8 + strlen(metadata[i][0]) +
1393 strlen(metadata[i][1]);
1394 metadata_key_count++;
1397 /* supported feature */
1399 count = ARRAY_SIZE(feature_bits);
1401 size = FEATURE_BYTES(count);
1402 extra_bytes += 4 + size;
1406 count = ARRAY_SIZE(metric_bits);
1408 size = METRIC_BYTES(count);
1409 extra_bytes += 2 + 4 + 4 + size;
1411 /* Allocate the message */
1412 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
1415 pr_err("ENOMEM creating session open msg\n");
1416 return ERR_PTR(-ENOMEM);
1418 p = msg->front.iov_base;
1419 end = p + msg->front.iov_len;
1422 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
1423 h->seq = cpu_to_le64(seq);
1426 * Serialize client metadata into waiting buffer space, using
1427 * the format that userspace expects for map<string, string>
1429 * ClientSession messages with metadata are v4
1431 msg->hdr.version = cpu_to_le16(4);
1432 msg->hdr.compat_version = cpu_to_le16(1);
1434 /* The write pointer, following the session_head structure */
1437 /* Number of entries in the map */
1438 ceph_encode_32(&p, metadata_key_count);
1440 /* Two length-prefixed strings for each entry in the map */
1441 for (i = 0; metadata[i][0]; ++i) {
1442 size_t const key_len = strlen(metadata[i][0]);
1443 size_t const val_len = strlen(metadata[i][1]);
1445 ceph_encode_32(&p, key_len);
1446 memcpy(p, metadata[i][0], key_len);
1448 ceph_encode_32(&p, val_len);
1449 memcpy(p, metadata[i][1], val_len);
1453 ret = encode_supported_features(&p, end);
1455 pr_err("encode_supported_features failed!\n");
1457 return ERR_PTR(ret);
1460 ret = encode_metric_spec(&p, end);
1462 pr_err("encode_metric_spec failed!\n");
1464 return ERR_PTR(ret);
1467 msg->front.iov_len = p - msg->front.iov_base;
1468 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1474 * send session open request.
1476 * called under mdsc->mutex
1478 static int __open_session(struct ceph_mds_client *mdsc,
1479 struct ceph_mds_session *session)
1481 struct ceph_msg *msg;
1483 int mds = session->s_mds;
1485 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_FENCE_IO)
1488 /* wait for mds to go active? */
1489 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
1490 dout("open_session to mds%d (%s)\n", mds,
1491 ceph_mds_state_name(mstate));
1492 session->s_state = CEPH_MDS_SESSION_OPENING;
1493 session->s_renew_requested = jiffies;
1495 /* send connect message */
1496 msg = create_session_open_msg(mdsc, session->s_seq);
1498 return PTR_ERR(msg);
1499 ceph_con_send(&session->s_con, msg);
1504 * open sessions for any export targets for the given mds
1506 * called under mdsc->mutex
1508 static struct ceph_mds_session *
1509 __open_export_target_session(struct ceph_mds_client *mdsc, int target)
1511 struct ceph_mds_session *session;
1514 session = __ceph_lookup_mds_session(mdsc, target);
1516 session = register_session(mdsc, target);
1517 if (IS_ERR(session))
1520 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1521 session->s_state == CEPH_MDS_SESSION_CLOSING) {
1522 ret = __open_session(mdsc, session);
1524 return ERR_PTR(ret);
1530 struct ceph_mds_session *
1531 ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1533 struct ceph_mds_session *session;
1535 dout("open_export_target_session to mds%d\n", target);
1537 mutex_lock(&mdsc->mutex);
1538 session = __open_export_target_session(mdsc, target);
1539 mutex_unlock(&mdsc->mutex);
1544 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1545 struct ceph_mds_session *session)
1547 struct ceph_mds_info *mi;
1548 struct ceph_mds_session *ts;
1549 int i, mds = session->s_mds;
1551 if (mds >= mdsc->mdsmap->possible_max_rank)
1554 mi = &mdsc->mdsmap->m_info[mds];
1555 dout("open_export_target_sessions for mds%d (%d targets)\n",
1556 session->s_mds, mi->num_export_targets);
1558 for (i = 0; i < mi->num_export_targets; i++) {
1559 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1560 ceph_put_mds_session(ts);
1564 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1565 struct ceph_mds_session *session)
1567 mutex_lock(&mdsc->mutex);
1568 __open_export_target_sessions(mdsc, session);
1569 mutex_unlock(&mdsc->mutex);
1576 static void detach_cap_releases(struct ceph_mds_session *session,
1577 struct list_head *target)
1579 lockdep_assert_held(&session->s_cap_lock);
1581 list_splice_init(&session->s_cap_releases, target);
1582 session->s_num_cap_releases = 0;
1583 dout("dispose_cap_releases mds%d\n", session->s_mds);
1586 static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1587 struct list_head *dispose)
1589 while (!list_empty(dispose)) {
1590 struct ceph_cap *cap;
1591 /* zero out the in-progress message */
1592 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
1593 list_del(&cap->session_caps);
1594 ceph_put_cap(mdsc, cap);
1598 static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1599 struct ceph_mds_session *session)
1601 struct ceph_mds_request *req;
1604 dout("cleanup_session_requests mds%d\n", session->s_mds);
1605 mutex_lock(&mdsc->mutex);
1606 while (!list_empty(&session->s_unsafe)) {
1607 req = list_first_entry(&session->s_unsafe,
1608 struct ceph_mds_request, r_unsafe_item);
1609 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1611 if (req->r_target_inode)
1612 mapping_set_error(req->r_target_inode->i_mapping, -EIO);
1613 if (req->r_unsafe_dir)
1614 mapping_set_error(req->r_unsafe_dir->i_mapping, -EIO);
1615 __unregister_request(mdsc, req);
1617 /* zero r_attempts, so kick_requests() will re-send requests */
1618 p = rb_first(&mdsc->request_tree);
1620 req = rb_entry(p, struct ceph_mds_request, r_node);
1622 if (req->r_session &&
1623 req->r_session->s_mds == session->s_mds)
1624 req->r_attempts = 0;
1626 mutex_unlock(&mdsc->mutex);
1630 * Helper to safely iterate over all caps associated with a session, with
1631 * special care taken to handle a racing __ceph_remove_cap().
1633 * Caller must hold session s_mutex.
1635 int ceph_iterate_session_caps(struct ceph_mds_session *session,
1636 int (*cb)(struct inode *, int mds, void *),
1639 struct list_head *p;
1640 struct ceph_cap *cap;
1641 struct inode *inode, *last_inode = NULL;
1642 struct ceph_cap *old_cap = NULL;
1645 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1646 spin_lock(&session->s_cap_lock);
1647 p = session->s_caps.next;
1648 while (p != &session->s_caps) {
1651 cap = list_entry(p, struct ceph_cap, session_caps);
1652 inode = igrab(&cap->ci->netfs.inode);
1657 session->s_cap_iterator = cap;
1659 spin_unlock(&session->s_cap_lock);
1666 ceph_put_cap(session->s_mdsc, old_cap);
1670 ret = cb(inode, mds, arg);
1673 spin_lock(&session->s_cap_lock);
1676 dout("iterate_session_caps finishing cap %p removal\n",
1678 BUG_ON(cap->session != session);
1679 cap->session = NULL;
1680 list_del_init(&cap->session_caps);
1681 session->s_nr_caps--;
1682 atomic64_dec(&session->s_mdsc->metric.total_caps);
1683 if (cap->queue_release)
1684 __ceph_queue_cap_release(session, cap);
1686 old_cap = cap; /* put_cap it w/o locks held */
1693 session->s_cap_iterator = NULL;
1694 spin_unlock(&session->s_cap_lock);
1698 ceph_put_cap(session->s_mdsc, old_cap);
1703 static int remove_session_caps_cb(struct inode *inode, int mds, void *arg)
1705 struct ceph_inode_info *ci = ceph_inode(inode);
1706 bool invalidate = false;
1707 struct ceph_cap *cap;
1710 spin_lock(&ci->i_ceph_lock);
1711 cap = __get_cap_for_mds(ci, mds);
1713 dout(" removing cap %p, ci is %p, inode is %p\n",
1714 cap, ci, &ci->netfs.inode);
1716 iputs = ceph_purge_inode_cap(inode, cap, &invalidate);
1718 spin_unlock(&ci->i_ceph_lock);
1721 wake_up_all(&ci->i_cap_wq);
1723 ceph_queue_invalidate(inode);
1730 * caller must hold session s_mutex
1732 static void remove_session_caps(struct ceph_mds_session *session)
1734 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1735 struct super_block *sb = fsc->sb;
1738 dout("remove_session_caps on %p\n", session);
1739 ceph_iterate_session_caps(session, remove_session_caps_cb, fsc);
1741 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1743 spin_lock(&session->s_cap_lock);
1744 if (session->s_nr_caps > 0) {
1745 struct inode *inode;
1746 struct ceph_cap *cap, *prev = NULL;
1747 struct ceph_vino vino;
1749 * iterate_session_caps() skips inodes that are being
1750 * deleted, we need to wait until deletions are complete.
1751 * __wait_on_freeing_inode() is designed for the job,
1752 * but it is not exported, so use lookup inode function
1755 while (!list_empty(&session->s_caps)) {
1756 cap = list_entry(session->s_caps.next,
1757 struct ceph_cap, session_caps);
1761 vino = cap->ci->i_vino;
1762 spin_unlock(&session->s_cap_lock);
1764 inode = ceph_find_inode(sb, vino);
1767 spin_lock(&session->s_cap_lock);
1771 // drop cap expires and unlock s_cap_lock
1772 detach_cap_releases(session, &dispose);
1774 BUG_ON(session->s_nr_caps > 0);
1775 BUG_ON(!list_empty(&session->s_cap_flushing));
1776 spin_unlock(&session->s_cap_lock);
1777 dispose_cap_releases(session->s_mdsc, &dispose);
1787 * wake up any threads waiting on this session's caps. if the cap is
1788 * old (didn't get renewed on the client reconnect), remove it now.
1790 * caller must hold s_mutex.
1792 static int wake_up_session_cb(struct inode *inode, int mds, void *arg)
1794 struct ceph_inode_info *ci = ceph_inode(inode);
1795 unsigned long ev = (unsigned long)arg;
1797 if (ev == RECONNECT) {
1798 spin_lock(&ci->i_ceph_lock);
1799 ci->i_wanted_max_size = 0;
1800 ci->i_requested_max_size = 0;
1801 spin_unlock(&ci->i_ceph_lock);
1802 } else if (ev == RENEWCAPS) {
1803 struct ceph_cap *cap;
1805 spin_lock(&ci->i_ceph_lock);
1806 cap = __get_cap_for_mds(ci, mds);
1807 /* mds did not re-issue stale cap */
1808 if (cap && cap->cap_gen < atomic_read(&cap->session->s_cap_gen))
1809 cap->issued = cap->implemented = CEPH_CAP_PIN;
1810 spin_unlock(&ci->i_ceph_lock);
1811 } else if (ev == FORCE_RO) {
1813 wake_up_all(&ci->i_cap_wq);
1817 static void wake_up_session_caps(struct ceph_mds_session *session, int ev)
1819 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1820 ceph_iterate_session_caps(session, wake_up_session_cb,
1821 (void *)(unsigned long)ev);
1825 * Send periodic message to MDS renewing all currently held caps. The
1826 * ack will reset the expiration for all caps from this session.
1828 * caller holds s_mutex
1830 static int send_renew_caps(struct ceph_mds_client *mdsc,
1831 struct ceph_mds_session *session)
1833 struct ceph_msg *msg;
1836 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1837 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1838 pr_info("mds%d caps stale\n", session->s_mds);
1839 session->s_renew_requested = jiffies;
1841 /* do not try to renew caps until a recovering mds has reconnected
1842 * with its clients. */
1843 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1844 if (state < CEPH_MDS_STATE_RECONNECT) {
1845 dout("send_renew_caps ignoring mds%d (%s)\n",
1846 session->s_mds, ceph_mds_state_name(state));
1850 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1851 ceph_mds_state_name(state));
1852 msg = ceph_create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1853 ++session->s_renew_seq);
1856 ceph_con_send(&session->s_con, msg);
1860 static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1861 struct ceph_mds_session *session, u64 seq)
1863 struct ceph_msg *msg;
1865 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1866 session->s_mds, ceph_session_state_name(session->s_state), seq);
1867 msg = ceph_create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1870 ceph_con_send(&session->s_con, msg);
1876 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1878 * Called under session->s_mutex
1880 static void renewed_caps(struct ceph_mds_client *mdsc,
1881 struct ceph_mds_session *session, int is_renew)
1886 spin_lock(&session->s_cap_lock);
1887 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
1889 session->s_cap_ttl = session->s_renew_requested +
1890 mdsc->mdsmap->m_session_timeout*HZ;
1893 if (time_before(jiffies, session->s_cap_ttl)) {
1894 pr_info("mds%d caps renewed\n", session->s_mds);
1897 pr_info("mds%d caps still stale\n", session->s_mds);
1900 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1901 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1902 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1903 spin_unlock(&session->s_cap_lock);
1906 wake_up_session_caps(session, RENEWCAPS);
1910 * send a session close request
1912 static int request_close_session(struct ceph_mds_session *session)
1914 struct ceph_msg *msg;
1916 dout("request_close_session mds%d state %s seq %lld\n",
1917 session->s_mds, ceph_session_state_name(session->s_state),
1919 msg = ceph_create_session_msg(CEPH_SESSION_REQUEST_CLOSE,
1923 ceph_con_send(&session->s_con, msg);
1928 * Called with s_mutex held.
1930 static int __close_session(struct ceph_mds_client *mdsc,
1931 struct ceph_mds_session *session)
1933 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1935 session->s_state = CEPH_MDS_SESSION_CLOSING;
1936 return request_close_session(session);
1939 static bool drop_negative_children(struct dentry *dentry)
1941 struct dentry *child;
1942 bool all_negative = true;
1944 if (!d_is_dir(dentry))
1947 spin_lock(&dentry->d_lock);
1948 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1949 if (d_really_is_positive(child)) {
1950 all_negative = false;
1954 spin_unlock(&dentry->d_lock);
1957 shrink_dcache_parent(dentry);
1959 return all_negative;
1963 * Trim old(er) caps.
1965 * Because we can't cache an inode without one or more caps, we do
1966 * this indirectly: if a cap is unused, we prune its aliases, at which
1967 * point the inode will hopefully get dropped to.
1969 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1970 * memory pressure from the MDS, though, so it needn't be perfect.
1972 static int trim_caps_cb(struct inode *inode, int mds, void *arg)
1974 int *remaining = arg;
1975 struct ceph_inode_info *ci = ceph_inode(inode);
1976 int used, wanted, oissued, mine;
1977 struct ceph_cap *cap;
1979 if (*remaining <= 0)
1982 spin_lock(&ci->i_ceph_lock);
1983 cap = __get_cap_for_mds(ci, mds);
1985 spin_unlock(&ci->i_ceph_lock);
1988 mine = cap->issued | cap->implemented;
1989 used = __ceph_caps_used(ci);
1990 wanted = __ceph_caps_file_wanted(ci);
1991 oissued = __ceph_caps_issued_other(ci, cap);
1993 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1994 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1995 ceph_cap_string(used), ceph_cap_string(wanted));
1996 if (cap == ci->i_auth_cap) {
1997 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1998 !list_empty(&ci->i_cap_snaps))
2000 if ((used | wanted) & CEPH_CAP_ANY_WR)
2002 /* Note: it's possible that i_filelock_ref becomes non-zero
2003 * after dropping auth caps. It doesn't hurt because reply
2004 * of lock mds request will re-add auth caps. */
2005 if (atomic_read(&ci->i_filelock_ref) > 0)
2008 /* The inode has cached pages, but it's no longer used.
2009 * we can safely drop it */
2010 if (S_ISREG(inode->i_mode) &&
2011 wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
2012 !(oissued & CEPH_CAP_FILE_CACHE)) {
2016 if ((used | wanted) & ~oissued & mine)
2017 goto out; /* we need these caps */
2020 /* we aren't the only cap.. just remove us */
2021 ceph_remove_cap(cap, true);
2024 struct dentry *dentry;
2025 /* try dropping referring dentries */
2026 spin_unlock(&ci->i_ceph_lock);
2027 dentry = d_find_any_alias(inode);
2028 if (dentry && drop_negative_children(dentry)) {
2031 d_prune_aliases(inode);
2032 count = atomic_read(&inode->i_count);
2035 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
2044 spin_unlock(&ci->i_ceph_lock);
2049 * Trim session cap count down to some max number.
2051 int ceph_trim_caps(struct ceph_mds_client *mdsc,
2052 struct ceph_mds_session *session,
2055 int trim_caps = session->s_nr_caps - max_caps;
2057 dout("trim_caps mds%d start: %d / %d, trim %d\n",
2058 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
2059 if (trim_caps > 0) {
2060 int remaining = trim_caps;
2062 ceph_iterate_session_caps(session, trim_caps_cb, &remaining);
2063 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
2064 session->s_mds, session->s_nr_caps, max_caps,
2065 trim_caps - remaining);
2068 ceph_flush_cap_releases(mdsc, session);
2072 static int check_caps_flush(struct ceph_mds_client *mdsc,
2077 spin_lock(&mdsc->cap_dirty_lock);
2078 if (!list_empty(&mdsc->cap_flush_list)) {
2079 struct ceph_cap_flush *cf =
2080 list_first_entry(&mdsc->cap_flush_list,
2081 struct ceph_cap_flush, g_list);
2082 if (cf->tid <= want_flush_tid) {
2083 dout("check_caps_flush still flushing tid "
2084 "%llu <= %llu\n", cf->tid, want_flush_tid);
2088 spin_unlock(&mdsc->cap_dirty_lock);
2093 * flush all dirty inode data to disk.
2095 * returns true if we've flushed through want_flush_tid
2097 static void wait_caps_flush(struct ceph_mds_client *mdsc,
2100 dout("check_caps_flush want %llu\n", want_flush_tid);
2102 wait_event(mdsc->cap_flushing_wq,
2103 check_caps_flush(mdsc, want_flush_tid));
2105 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
2109 * called under s_mutex
2111 static void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
2112 struct ceph_mds_session *session)
2114 struct ceph_msg *msg = NULL;
2115 struct ceph_mds_cap_release *head;
2116 struct ceph_mds_cap_item *item;
2117 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
2118 struct ceph_cap *cap;
2119 LIST_HEAD(tmp_list);
2120 int num_cap_releases;
2121 __le32 barrier, *cap_barrier;
2123 down_read(&osdc->lock);
2124 barrier = cpu_to_le32(osdc->epoch_barrier);
2125 up_read(&osdc->lock);
2127 spin_lock(&session->s_cap_lock);
2129 list_splice_init(&session->s_cap_releases, &tmp_list);
2130 num_cap_releases = session->s_num_cap_releases;
2131 session->s_num_cap_releases = 0;
2132 spin_unlock(&session->s_cap_lock);
2134 while (!list_empty(&tmp_list)) {
2136 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
2137 PAGE_SIZE, GFP_NOFS, false);
2140 head = msg->front.iov_base;
2141 head->num = cpu_to_le32(0);
2142 msg->front.iov_len = sizeof(*head);
2144 msg->hdr.version = cpu_to_le16(2);
2145 msg->hdr.compat_version = cpu_to_le16(1);
2148 cap = list_first_entry(&tmp_list, struct ceph_cap,
2150 list_del(&cap->session_caps);
2153 head = msg->front.iov_base;
2154 put_unaligned_le32(get_unaligned_le32(&head->num) + 1,
2156 item = msg->front.iov_base + msg->front.iov_len;
2157 item->ino = cpu_to_le64(cap->cap_ino);
2158 item->cap_id = cpu_to_le64(cap->cap_id);
2159 item->migrate_seq = cpu_to_le32(cap->mseq);
2160 item->seq = cpu_to_le32(cap->issue_seq);
2161 msg->front.iov_len += sizeof(*item);
2163 ceph_put_cap(mdsc, cap);
2165 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
2166 // Append cap_barrier field
2167 cap_barrier = msg->front.iov_base + msg->front.iov_len;
2168 *cap_barrier = barrier;
2169 msg->front.iov_len += sizeof(*cap_barrier);
2171 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2172 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
2173 ceph_con_send(&session->s_con, msg);
2178 BUG_ON(num_cap_releases != 0);
2180 spin_lock(&session->s_cap_lock);
2181 if (!list_empty(&session->s_cap_releases))
2183 spin_unlock(&session->s_cap_lock);
2186 // Append cap_barrier field
2187 cap_barrier = msg->front.iov_base + msg->front.iov_len;
2188 *cap_barrier = barrier;
2189 msg->front.iov_len += sizeof(*cap_barrier);
2191 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2192 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
2193 ceph_con_send(&session->s_con, msg);
2197 pr_err("send_cap_releases mds%d, failed to allocate message\n",
2199 spin_lock(&session->s_cap_lock);
2200 list_splice(&tmp_list, &session->s_cap_releases);
2201 session->s_num_cap_releases += num_cap_releases;
2202 spin_unlock(&session->s_cap_lock);
2205 static void ceph_cap_release_work(struct work_struct *work)
2207 struct ceph_mds_session *session =
2208 container_of(work, struct ceph_mds_session, s_cap_release_work);
2210 mutex_lock(&session->s_mutex);
2211 if (session->s_state == CEPH_MDS_SESSION_OPEN ||
2212 session->s_state == CEPH_MDS_SESSION_HUNG)
2213 ceph_send_cap_releases(session->s_mdsc, session);
2214 mutex_unlock(&session->s_mutex);
2215 ceph_put_mds_session(session);
2218 void ceph_flush_cap_releases(struct ceph_mds_client *mdsc,
2219 struct ceph_mds_session *session)
2224 ceph_get_mds_session(session);
2225 if (queue_work(mdsc->fsc->cap_wq,
2226 &session->s_cap_release_work)) {
2227 dout("cap release work queued\n");
2229 ceph_put_mds_session(session);
2230 dout("failed to queue cap release work\n");
2235 * caller holds session->s_cap_lock
2237 void __ceph_queue_cap_release(struct ceph_mds_session *session,
2238 struct ceph_cap *cap)
2240 list_add_tail(&cap->session_caps, &session->s_cap_releases);
2241 session->s_num_cap_releases++;
2243 if (!(session->s_num_cap_releases % CEPH_CAPS_PER_RELEASE))
2244 ceph_flush_cap_releases(session->s_mdsc, session);
2247 static void ceph_cap_reclaim_work(struct work_struct *work)
2249 struct ceph_mds_client *mdsc =
2250 container_of(work, struct ceph_mds_client, cap_reclaim_work);
2251 int ret = ceph_trim_dentries(mdsc);
2253 ceph_queue_cap_reclaim_work(mdsc);
2256 void ceph_queue_cap_reclaim_work(struct ceph_mds_client *mdsc)
2261 if (queue_work(mdsc->fsc->cap_wq, &mdsc->cap_reclaim_work)) {
2262 dout("caps reclaim work queued\n");
2264 dout("failed to queue caps release work\n");
2268 void ceph_reclaim_caps_nr(struct ceph_mds_client *mdsc, int nr)
2273 val = atomic_add_return(nr, &mdsc->cap_reclaim_pending);
2274 if ((val % CEPH_CAPS_PER_RELEASE) < nr) {
2275 atomic_set(&mdsc->cap_reclaim_pending, 0);
2276 ceph_queue_cap_reclaim_work(mdsc);
2284 int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
2287 struct ceph_inode_info *ci = ceph_inode(dir);
2288 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
2289 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
2290 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
2291 unsigned int num_entries;
2294 spin_lock(&ci->i_ceph_lock);
2295 num_entries = ci->i_files + ci->i_subdirs;
2296 spin_unlock(&ci->i_ceph_lock);
2297 num_entries = max(num_entries, 1U);
2298 num_entries = min(num_entries, opt->max_readdir);
2300 order = get_order(size * num_entries);
2301 while (order >= 0) {
2302 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
2306 if (rinfo->dir_entries)
2310 if (!rinfo->dir_entries)
2313 num_entries = (PAGE_SIZE << order) / size;
2314 num_entries = min(num_entries, opt->max_readdir);
2316 rinfo->dir_buf_size = PAGE_SIZE << order;
2317 req->r_num_caps = num_entries + 1;
2318 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
2319 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
2324 * Create an mds request.
2326 struct ceph_mds_request *
2327 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
2329 struct ceph_mds_request *req;
2331 req = kmem_cache_zalloc(ceph_mds_request_cachep, GFP_NOFS);
2333 return ERR_PTR(-ENOMEM);
2335 mutex_init(&req->r_fill_mutex);
2337 req->r_started = jiffies;
2338 req->r_start_latency = ktime_get();
2339 req->r_resend_mds = -1;
2340 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
2341 INIT_LIST_HEAD(&req->r_unsafe_target_item);
2343 req->r_feature_needed = -1;
2344 kref_init(&req->r_kref);
2345 RB_CLEAR_NODE(&req->r_node);
2346 INIT_LIST_HEAD(&req->r_wait);
2347 init_completion(&req->r_completion);
2348 init_completion(&req->r_safe_completion);
2349 INIT_LIST_HEAD(&req->r_unsafe_item);
2351 ktime_get_coarse_real_ts64(&req->r_stamp);
2354 req->r_direct_mode = mode;
2359 * return oldest (lowest) request, tid in request tree, 0 if none.
2361 * called under mdsc->mutex.
2363 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
2365 if (RB_EMPTY_ROOT(&mdsc->request_tree))
2367 return rb_entry(rb_first(&mdsc->request_tree),
2368 struct ceph_mds_request, r_node);
2371 static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
2373 return mdsc->oldest_tid;
2377 * Build a dentry's path. Allocate on heap; caller must kfree. Based
2378 * on build_path_from_dentry in fs/cifs/dir.c.
2380 * If @stop_on_nosnap, generate path relative to the first non-snapped
2383 * Encode hidden .snap dirs as a double /, i.e.
2384 * foo/.snap/bar -> foo//bar
2386 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase,
2389 struct dentry *temp;
2396 return ERR_PTR(-EINVAL);
2400 return ERR_PTR(-ENOMEM);
2405 seq = read_seqbegin(&rename_lock);
2409 struct inode *inode;
2411 spin_lock(&temp->d_lock);
2412 inode = d_inode(temp);
2413 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
2414 dout("build_path path+%d: %p SNAPDIR\n",
2416 } else if (stop_on_nosnap && inode && dentry != temp &&
2417 ceph_snap(inode) == CEPH_NOSNAP) {
2418 spin_unlock(&temp->d_lock);
2419 pos++; /* get rid of any prepended '/' */
2422 pos -= temp->d_name.len;
2424 spin_unlock(&temp->d_lock);
2427 memcpy(path + pos, temp->d_name.name, temp->d_name.len);
2429 spin_unlock(&temp->d_lock);
2430 temp = READ_ONCE(temp->d_parent);
2432 /* Are we at the root? */
2436 /* Are we out of buffer? */
2442 base = ceph_ino(d_inode(temp));
2445 if (read_seqretry(&rename_lock, seq))
2450 * A rename didn't occur, but somehow we didn't end up where
2451 * we thought we would. Throw a warning and try again.
2453 pr_warn("build_path did not end path lookup where "
2454 "expected, pos is %d\n", pos);
2459 *plen = PATH_MAX - 1 - pos;
2460 dout("build_path on %p %d built %llx '%.*s'\n",
2461 dentry, d_count(dentry), base, *plen, path + pos);
2465 static int build_dentry_path(struct dentry *dentry, struct inode *dir,
2466 const char **ppath, int *ppathlen, u64 *pino,
2467 bool *pfreepath, bool parent_locked)
2473 dir = d_inode_rcu(dentry->d_parent);
2474 if (dir && parent_locked && ceph_snap(dir) == CEPH_NOSNAP) {
2475 *pino = ceph_ino(dir);
2477 *ppath = dentry->d_name.name;
2478 *ppathlen = dentry->d_name.len;
2482 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2484 return PTR_ERR(path);
2490 static int build_inode_path(struct inode *inode,
2491 const char **ppath, int *ppathlen, u64 *pino,
2494 struct dentry *dentry;
2497 if (ceph_snap(inode) == CEPH_NOSNAP) {
2498 *pino = ceph_ino(inode);
2502 dentry = d_find_alias(inode);
2503 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2506 return PTR_ERR(path);
2513 * request arguments may be specified via an inode *, a dentry *, or
2514 * an explicit ino+path.
2516 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
2517 struct inode *rdiri, const char *rpath,
2518 u64 rino, const char **ppath, int *pathlen,
2519 u64 *ino, bool *freepath, bool parent_locked)
2524 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
2525 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
2527 } else if (rdentry) {
2528 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
2529 freepath, parent_locked);
2530 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
2532 } else if (rpath || rino) {
2535 *pathlen = rpath ? strlen(rpath) : 0;
2536 dout(" path %.*s\n", *pathlen, rpath);
2542 static void encode_timestamp_and_gids(void **p,
2543 const struct ceph_mds_request *req)
2545 struct ceph_timespec ts;
2548 ceph_encode_timespec64(&ts, &req->r_stamp);
2549 ceph_encode_copy(p, &ts, sizeof(ts));
2552 ceph_encode_32(p, req->r_cred->group_info->ngroups);
2553 for (i = 0; i < req->r_cred->group_info->ngroups; i++)
2554 ceph_encode_64(p, from_kgid(&init_user_ns,
2555 req->r_cred->group_info->gid[i]));
2559 * called under mdsc->mutex
2561 static struct ceph_msg *create_request_message(struct ceph_mds_session *session,
2562 struct ceph_mds_request *req,
2563 bool drop_cap_releases)
2565 int mds = session->s_mds;
2566 struct ceph_mds_client *mdsc = session->s_mdsc;
2567 struct ceph_msg *msg;
2568 struct ceph_mds_request_head_old *head;
2569 const char *path1 = NULL;
2570 const char *path2 = NULL;
2571 u64 ino1 = 0, ino2 = 0;
2572 int pathlen1 = 0, pathlen2 = 0;
2573 bool freepath1 = false, freepath2 = false;
2574 struct dentry *old_dentry = NULL;
2579 bool legacy = !(session->s_con.peer_features & CEPH_FEATURE_FS_BTIME);
2581 ret = set_request_path_attr(req->r_inode, req->r_dentry,
2582 req->r_parent, req->r_path1, req->r_ino1.ino,
2583 &path1, &pathlen1, &ino1, &freepath1,
2584 test_bit(CEPH_MDS_R_PARENT_LOCKED,
2585 &req->r_req_flags));
2591 /* If r_old_dentry is set, then assume that its parent is locked */
2592 if (req->r_old_dentry &&
2593 !(req->r_old_dentry->d_flags & DCACHE_DISCONNECTED))
2594 old_dentry = req->r_old_dentry;
2595 ret = set_request_path_attr(NULL, old_dentry,
2596 req->r_old_dentry_dir,
2597 req->r_path2, req->r_ino2.ino,
2598 &path2, &pathlen2, &ino2, &freepath2, true);
2604 len = legacy ? sizeof(*head) : sizeof(struct ceph_mds_request_head);
2605 len += pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
2606 sizeof(struct ceph_timespec);
2607 len += sizeof(u32) + (sizeof(u64) * req->r_cred->group_info->ngroups);
2609 /* calculate (max) length for cap releases */
2610 len += sizeof(struct ceph_mds_request_release) *
2611 (!!req->r_inode_drop + !!req->r_dentry_drop +
2612 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2614 if (req->r_dentry_drop)
2616 if (req->r_old_dentry_drop)
2619 msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
2621 msg = ERR_PTR(-ENOMEM);
2625 msg->hdr.tid = cpu_to_le64(req->r_tid);
2628 * The old ceph_mds_request_head didn't contain a version field, and
2629 * one was added when we moved the message version from 3->4.
2632 msg->hdr.version = cpu_to_le16(3);
2633 head = msg->front.iov_base;
2634 p = msg->front.iov_base + sizeof(*head);
2636 struct ceph_mds_request_head *new_head = msg->front.iov_base;
2638 msg->hdr.version = cpu_to_le16(4);
2639 new_head->version = cpu_to_le16(CEPH_MDS_REQUEST_HEAD_VERSION);
2640 head = (struct ceph_mds_request_head_old *)&new_head->oldest_client_tid;
2641 p = msg->front.iov_base + sizeof(*new_head);
2644 end = msg->front.iov_base + msg->front.iov_len;
2646 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2647 head->op = cpu_to_le32(req->r_op);
2648 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns,
2649 req->r_cred->fsuid));
2650 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns,
2651 req->r_cred->fsgid));
2652 head->ino = cpu_to_le64(req->r_deleg_ino);
2653 head->args = req->r_args;
2655 ceph_encode_filepath(&p, end, ino1, path1);
2656 ceph_encode_filepath(&p, end, ino2, path2);
2658 /* make note of release offset, in case we need to replay */
2659 req->r_request_release_offset = p - msg->front.iov_base;
2663 if (req->r_inode_drop)
2664 releases += ceph_encode_inode_release(&p,
2665 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
2666 mds, req->r_inode_drop, req->r_inode_unless,
2667 req->r_op == CEPH_MDS_OP_READDIR);
2668 if (req->r_dentry_drop)
2669 releases += ceph_encode_dentry_release(&p, req->r_dentry,
2670 req->r_parent, mds, req->r_dentry_drop,
2671 req->r_dentry_unless);
2672 if (req->r_old_dentry_drop)
2673 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
2674 req->r_old_dentry_dir, mds,
2675 req->r_old_dentry_drop,
2676 req->r_old_dentry_unless);
2677 if (req->r_old_inode_drop)
2678 releases += ceph_encode_inode_release(&p,
2679 d_inode(req->r_old_dentry),
2680 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
2682 if (drop_cap_releases) {
2684 p = msg->front.iov_base + req->r_request_release_offset;
2687 head->num_releases = cpu_to_le16(releases);
2689 encode_timestamp_and_gids(&p, req);
2691 if (WARN_ON_ONCE(p > end)) {
2693 msg = ERR_PTR(-ERANGE);
2697 msg->front.iov_len = p - msg->front.iov_base;
2698 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2700 if (req->r_pagelist) {
2701 struct ceph_pagelist *pagelist = req->r_pagelist;
2702 ceph_msg_data_add_pagelist(msg, pagelist);
2703 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2705 msg->hdr.data_len = 0;
2708 msg->hdr.data_off = cpu_to_le16(0);
2712 ceph_mdsc_free_path((char *)path2, pathlen2);
2715 ceph_mdsc_free_path((char *)path1, pathlen1);
2721 * called under mdsc->mutex if error, under no mutex if
2724 static void complete_request(struct ceph_mds_client *mdsc,
2725 struct ceph_mds_request *req)
2727 req->r_end_latency = ktime_get();
2729 if (req->r_callback)
2730 req->r_callback(mdsc, req);
2731 complete_all(&req->r_completion);
2734 static struct ceph_mds_request_head_old *
2735 find_old_request_head(void *p, u64 features)
2737 bool legacy = !(features & CEPH_FEATURE_FS_BTIME);
2738 struct ceph_mds_request_head *new_head;
2741 return (struct ceph_mds_request_head_old *)p;
2742 new_head = (struct ceph_mds_request_head *)p;
2743 return (struct ceph_mds_request_head_old *)&new_head->oldest_client_tid;
2747 * called under mdsc->mutex
2749 static int __prepare_send_request(struct ceph_mds_session *session,
2750 struct ceph_mds_request *req,
2751 bool drop_cap_releases)
2753 int mds = session->s_mds;
2754 struct ceph_mds_client *mdsc = session->s_mdsc;
2755 struct ceph_mds_request_head_old *rhead;
2756 struct ceph_msg *msg;
2757 int flags = 0, max_retry;
2760 * The type of 'r_attempts' in kernel 'ceph_mds_request'
2761 * is 'int', while in 'ceph_mds_request_head' the type of
2762 * 'num_retry' is '__u8'. So in case the request retries
2763 * exceeding 256 times, the MDS will receive a incorrect
2766 * In this case it's ususally a bug in MDS and continue
2767 * retrying the request makes no sense.
2769 * In future this could be fixed in ceph code, so avoid
2770 * using the hardcode here.
2772 max_retry = sizeof_field(struct ceph_mds_request_head, num_retry);
2773 max_retry = 1 << (max_retry * BITS_PER_BYTE);
2774 if (req->r_attempts >= max_retry) {
2775 pr_warn_ratelimited("%s request tid %llu seq overflow\n",
2776 __func__, req->r_tid);
2782 struct ceph_cap *cap =
2783 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2786 req->r_sent_on_mseq = cap->mseq;
2788 req->r_sent_on_mseq = -1;
2790 dout("%s %p tid %lld %s (attempt %d)\n", __func__, req,
2791 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2793 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2797 * Replay. Do not regenerate message (and rebuild
2798 * paths, etc.); just use the original message.
2799 * Rebuilding paths will break for renames because
2800 * d_move mangles the src name.
2802 msg = req->r_request;
2803 rhead = find_old_request_head(msg->front.iov_base,
2804 session->s_con.peer_features);
2806 flags = le32_to_cpu(rhead->flags);
2807 flags |= CEPH_MDS_FLAG_REPLAY;
2808 rhead->flags = cpu_to_le32(flags);
2810 if (req->r_target_inode)
2811 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2813 rhead->num_retry = req->r_attempts - 1;
2815 /* remove cap/dentry releases from message */
2816 rhead->num_releases = 0;
2818 p = msg->front.iov_base + req->r_request_release_offset;
2819 encode_timestamp_and_gids(&p, req);
2821 msg->front.iov_len = p - msg->front.iov_base;
2822 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2826 if (req->r_request) {
2827 ceph_msg_put(req->r_request);
2828 req->r_request = NULL;
2830 msg = create_request_message(session, req, drop_cap_releases);
2832 req->r_err = PTR_ERR(msg);
2833 return PTR_ERR(msg);
2835 req->r_request = msg;
2837 rhead = find_old_request_head(msg->front.iov_base,
2838 session->s_con.peer_features);
2839 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
2840 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2841 flags |= CEPH_MDS_FLAG_REPLAY;
2842 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags))
2843 flags |= CEPH_MDS_FLAG_ASYNC;
2845 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2846 rhead->flags = cpu_to_le32(flags);
2847 rhead->num_fwd = req->r_num_fwd;
2848 rhead->num_retry = req->r_attempts - 1;
2850 dout(" r_parent = %p\n", req->r_parent);
2855 * called under mdsc->mutex
2857 static int __send_request(struct ceph_mds_session *session,
2858 struct ceph_mds_request *req,
2859 bool drop_cap_releases)
2863 err = __prepare_send_request(session, req, drop_cap_releases);
2865 ceph_msg_get(req->r_request);
2866 ceph_con_send(&session->s_con, req->r_request);
2873 * send request, or put it on the appropriate wait list.
2875 static void __do_request(struct ceph_mds_client *mdsc,
2876 struct ceph_mds_request *req)
2878 struct ceph_mds_session *session = NULL;
2883 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2884 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
2885 __unregister_request(mdsc, req);
2889 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_FENCE_IO) {
2890 dout("do_request metadata corrupted\n");
2894 if (req->r_timeout &&
2895 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2896 dout("do_request timed out\n");
2900 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
2901 dout("do_request forced umount\n");
2905 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
2906 if (mdsc->mdsmap_err) {
2907 err = mdsc->mdsmap_err;
2908 dout("do_request mdsmap err %d\n", err);
2911 if (mdsc->mdsmap->m_epoch == 0) {
2912 dout("do_request no mdsmap, waiting for map\n");
2913 list_add(&req->r_wait, &mdsc->waiting_for_map);
2916 if (!(mdsc->fsc->mount_options->flags &
2917 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2918 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
2919 err = -EHOSTUNREACH;
2924 put_request_session(req);
2926 mds = __choose_mds(mdsc, req, &random);
2928 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2929 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags)) {
2933 dout("do_request no mds or not active, waiting for map\n");
2934 list_add(&req->r_wait, &mdsc->waiting_for_map);
2938 /* get, open session */
2939 session = __ceph_lookup_mds_session(mdsc, mds);
2941 session = register_session(mdsc, mds);
2942 if (IS_ERR(session)) {
2943 err = PTR_ERR(session);
2947 req->r_session = ceph_get_mds_session(session);
2949 dout("do_request mds%d session %p state %s\n", mds, session,
2950 ceph_session_state_name(session->s_state));
2953 * The old ceph will crash the MDSs when see unknown OPs
2955 if (req->r_feature_needed > 0 &&
2956 !test_bit(req->r_feature_needed, &session->s_features)) {
2961 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2962 session->s_state != CEPH_MDS_SESSION_HUNG) {
2964 * We cannot queue async requests since the caps and delegated
2965 * inodes are bound to the session. Just return -EJUKEBOX and
2966 * let the caller retry a sync request in that case.
2968 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags)) {
2974 * If the session has been REJECTED, then return a hard error,
2975 * unless it's a CLEANRECOVER mount, in which case we'll queue
2976 * it to the mdsc queue.
2978 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2979 if (ceph_test_mount_opt(mdsc->fsc, CLEANRECOVER))
2980 list_add(&req->r_wait, &mdsc->waiting_for_map);
2986 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2987 session->s_state == CEPH_MDS_SESSION_CLOSING) {
2988 err = __open_session(mdsc, session);
2991 /* retry the same mds later */
2993 req->r_resend_mds = mds;
2995 list_add(&req->r_wait, &session->s_waiting);
3000 req->r_resend_mds = -1; /* forget any previous mds hint */
3002 if (req->r_request_started == 0) /* note request start time */
3003 req->r_request_started = jiffies;
3006 * For async create we will choose the auth MDS of frag in parent
3007 * directory to send the request and ususally this works fine, but
3008 * if the migrated the dirtory to another MDS before it could handle
3009 * it the request will be forwarded.
3011 * And then the auth cap will be changed.
3013 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags) && req->r_num_fwd) {
3014 struct ceph_dentry_info *di = ceph_dentry(req->r_dentry);
3015 struct ceph_inode_info *ci;
3016 struct ceph_cap *cap;
3019 * The request maybe handled very fast and the new inode
3020 * hasn't been linked to the dentry yet. We need to wait
3021 * for the ceph_finish_async_create(), which shouldn't be
3022 * stuck too long or fail in thoery, to finish when forwarding
3025 if (!d_inode(req->r_dentry)) {
3026 err = wait_on_bit(&di->flags, CEPH_DENTRY_ASYNC_CREATE_BIT,
3029 mutex_lock(&req->r_fill_mutex);
3030 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
3031 mutex_unlock(&req->r_fill_mutex);
3036 ci = ceph_inode(d_inode(req->r_dentry));
3038 spin_lock(&ci->i_ceph_lock);
3039 cap = ci->i_auth_cap;
3040 if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE && mds != cap->mds) {
3041 dout("do_request session changed for auth cap %d -> %d\n",
3042 cap->session->s_mds, session->s_mds);
3044 /* Remove the auth cap from old session */
3045 spin_lock(&cap->session->s_cap_lock);
3046 cap->session->s_nr_caps--;
3047 list_del_init(&cap->session_caps);
3048 spin_unlock(&cap->session->s_cap_lock);
3050 /* Add the auth cap to the new session */
3052 cap->session = session;
3053 spin_lock(&session->s_cap_lock);
3054 session->s_nr_caps++;
3055 list_add_tail(&cap->session_caps, &session->s_caps);
3056 spin_unlock(&session->s_cap_lock);
3058 change_auth_cap_ses(ci, session);
3060 spin_unlock(&ci->i_ceph_lock);
3063 err = __send_request(session, req, false);
3066 ceph_put_mds_session(session);
3069 dout("__do_request early error %d\n", err);
3071 complete_request(mdsc, req);
3072 __unregister_request(mdsc, req);
3078 * called under mdsc->mutex
3080 static void __wake_requests(struct ceph_mds_client *mdsc,
3081 struct list_head *head)
3083 struct ceph_mds_request *req;
3084 LIST_HEAD(tmp_list);
3086 list_splice_init(head, &tmp_list);
3088 while (!list_empty(&tmp_list)) {
3089 req = list_entry(tmp_list.next,
3090 struct ceph_mds_request, r_wait);
3091 list_del_init(&req->r_wait);
3092 dout(" wake request %p tid %llu\n", req, req->r_tid);
3093 __do_request(mdsc, req);
3098 * Wake up threads with requests pending for @mds, so that they can
3099 * resubmit their requests to a possibly different mds.
3101 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
3103 struct ceph_mds_request *req;
3104 struct rb_node *p = rb_first(&mdsc->request_tree);
3106 dout("kick_requests mds%d\n", mds);
3108 req = rb_entry(p, struct ceph_mds_request, r_node);
3110 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
3112 if (req->r_attempts > 0)
3113 continue; /* only new requests */
3114 if (req->r_session &&
3115 req->r_session->s_mds == mds) {
3116 dout(" kicking tid %llu\n", req->r_tid);
3117 list_del_init(&req->r_wait);
3118 __do_request(mdsc, req);
3123 int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
3124 struct ceph_mds_request *req)
3128 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
3130 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
3131 if (req->r_parent) {
3132 struct ceph_inode_info *ci = ceph_inode(req->r_parent);
3133 int fmode = (req->r_op & CEPH_MDS_OP_WRITE) ?
3134 CEPH_FILE_MODE_WR : CEPH_FILE_MODE_RD;
3135 spin_lock(&ci->i_ceph_lock);
3136 ceph_take_cap_refs(ci, CEPH_CAP_PIN, false);
3137 __ceph_touch_fmode(ci, mdsc, fmode);
3138 spin_unlock(&ci->i_ceph_lock);
3140 if (req->r_old_dentry_dir)
3141 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
3145 err = ceph_wait_on_async_create(req->r_inode);
3147 dout("%s: wait for async create returned: %d\n",
3153 if (!err && req->r_old_inode) {
3154 err = ceph_wait_on_async_create(req->r_old_inode);
3156 dout("%s: wait for async create returned: %d\n",
3162 dout("submit_request on %p for inode %p\n", req, dir);
3163 mutex_lock(&mdsc->mutex);
3164 __register_request(mdsc, req, dir);
3165 __do_request(mdsc, req);
3167 mutex_unlock(&mdsc->mutex);
3171 int ceph_mdsc_wait_request(struct ceph_mds_client *mdsc,
3172 struct ceph_mds_request *req,
3173 ceph_mds_request_wait_callback_t wait_func)
3178 dout("do_request waiting\n");
3180 err = wait_func(mdsc, req);
3182 long timeleft = wait_for_completion_killable_timeout(
3184 ceph_timeout_jiffies(req->r_timeout));
3188 err = -ETIMEDOUT; /* timed out */
3190 err = timeleft; /* killed */
3192 dout("do_request waited, got %d\n", err);
3193 mutex_lock(&mdsc->mutex);
3195 /* only abort if we didn't race with a real reply */
3196 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
3197 err = le32_to_cpu(req->r_reply_info.head->result);
3198 } else if (err < 0) {
3199 dout("aborted request %lld with %d\n", req->r_tid, err);
3202 * ensure we aren't running concurrently with
3203 * ceph_fill_trace or ceph_readdir_prepopulate, which
3204 * rely on locks (dir mutex) held by our caller.
3206 mutex_lock(&req->r_fill_mutex);
3208 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
3209 mutex_unlock(&req->r_fill_mutex);
3211 if (req->r_parent &&
3212 (req->r_op & CEPH_MDS_OP_WRITE))
3213 ceph_invalidate_dir_request(req);
3218 mutex_unlock(&mdsc->mutex);
3223 * Synchrously perform an mds request. Take care of all of the
3224 * session setup, forwarding, retry details.
3226 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
3228 struct ceph_mds_request *req)
3232 dout("do_request on %p\n", req);
3235 err = ceph_mdsc_submit_request(mdsc, dir, req);
3237 err = ceph_mdsc_wait_request(mdsc, req, NULL);
3238 dout("do_request %p done, result %d\n", req, err);
3243 * Invalidate dir's completeness, dentry lease state on an aborted MDS
3244 * namespace request.
3246 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
3248 struct inode *dir = req->r_parent;
3249 struct inode *old_dir = req->r_old_dentry_dir;
3251 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
3253 ceph_dir_clear_complete(dir);
3255 ceph_dir_clear_complete(old_dir);
3257 ceph_invalidate_dentry_lease(req->r_dentry);
3258 if (req->r_old_dentry)
3259 ceph_invalidate_dentry_lease(req->r_old_dentry);
3265 * We take the session mutex and parse and process the reply immediately.
3266 * This preserves the logical ordering of replies, capabilities, etc., sent
3267 * by the MDS as they are applied to our local cache.
3269 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
3271 struct ceph_mds_client *mdsc = session->s_mdsc;
3272 struct ceph_mds_request *req;
3273 struct ceph_mds_reply_head *head = msg->front.iov_base;
3274 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
3275 struct ceph_snap_realm *realm;
3278 int mds = session->s_mds;
3279 bool close_sessions = false;
3281 if (msg->front.iov_len < sizeof(*head)) {
3282 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
3287 /* get request, session */
3288 tid = le64_to_cpu(msg->hdr.tid);
3289 mutex_lock(&mdsc->mutex);
3290 req = lookup_get_request(mdsc, tid);
3292 dout("handle_reply on unknown tid %llu\n", tid);
3293 mutex_unlock(&mdsc->mutex);
3296 dout("handle_reply %p\n", req);
3298 /* correct session? */
3299 if (req->r_session != session) {
3300 pr_err("mdsc_handle_reply got %llu on session mds%d"
3301 " not mds%d\n", tid, session->s_mds,
3302 req->r_session ? req->r_session->s_mds : -1);
3303 mutex_unlock(&mdsc->mutex);
3308 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
3309 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
3310 pr_warn("got a dup %s reply on %llu from mds%d\n",
3311 head->safe ? "safe" : "unsafe", tid, mds);
3312 mutex_unlock(&mdsc->mutex);
3315 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
3316 pr_warn("got unsafe after safe on %llu from mds%d\n",
3318 mutex_unlock(&mdsc->mutex);
3322 result = le32_to_cpu(head->result);
3325 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
3326 __unregister_request(mdsc, req);
3328 /* last request during umount? */
3329 if (mdsc->stopping && !__get_oldest_req(mdsc))
3330 complete_all(&mdsc->safe_umount_waiters);
3332 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
3334 * We already handled the unsafe response, now do the
3335 * cleanup. No need to examine the response; the MDS
3336 * doesn't include any result info in the safe
3337 * response. And even if it did, there is nothing
3338 * useful we could do with a revised return value.
3340 dout("got safe reply %llu, mds%d\n", tid, mds);
3342 mutex_unlock(&mdsc->mutex);
3346 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
3347 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
3350 dout("handle_reply tid %lld result %d\n", tid, result);
3351 rinfo = &req->r_reply_info;
3352 if (test_bit(CEPHFS_FEATURE_REPLY_ENCODING, &session->s_features))
3353 err = parse_reply_info(session, msg, rinfo, (u64)-1);
3355 err = parse_reply_info(session, msg, rinfo, session->s_con.peer_features);
3356 mutex_unlock(&mdsc->mutex);
3358 /* Must find target inode outside of mutexes to avoid deadlocks */
3359 if ((err >= 0) && rinfo->head->is_target) {
3361 struct ceph_vino tvino = {
3362 .ino = le64_to_cpu(rinfo->targeti.in->ino),
3363 .snap = le64_to_cpu(rinfo->targeti.in->snapid)
3366 in = ceph_get_inode(mdsc->fsc->sb, tvino);
3369 mutex_lock(&session->s_mutex);
3372 req->r_target_inode = in;
3375 mutex_lock(&session->s_mutex);
3377 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
3384 if (rinfo->snapblob_len) {
3385 down_write(&mdsc->snap_rwsem);
3386 err = ceph_update_snap_trace(mdsc, rinfo->snapblob,
3387 rinfo->snapblob + rinfo->snapblob_len,
3388 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
3391 up_write(&mdsc->snap_rwsem);
3392 close_sessions = true;
3397 downgrade_write(&mdsc->snap_rwsem);
3399 down_read(&mdsc->snap_rwsem);
3402 /* insert trace into our cache */
3403 mutex_lock(&req->r_fill_mutex);
3404 current->journal_info = req;
3405 err = ceph_fill_trace(mdsc->fsc->sb, req);
3407 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
3408 req->r_op == CEPH_MDS_OP_LSSNAP))
3409 ceph_readdir_prepopulate(req, req->r_session);
3411 current->journal_info = NULL;
3412 mutex_unlock(&req->r_fill_mutex);
3414 up_read(&mdsc->snap_rwsem);
3416 ceph_put_snap_realm(mdsc, realm);
3419 if (req->r_target_inode &&
3420 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
3421 struct ceph_inode_info *ci =
3422 ceph_inode(req->r_target_inode);
3423 spin_lock(&ci->i_unsafe_lock);
3424 list_add_tail(&req->r_unsafe_target_item,
3425 &ci->i_unsafe_iops);
3426 spin_unlock(&ci->i_unsafe_lock);
3429 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
3432 mutex_lock(&mdsc->mutex);
3433 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
3437 req->r_reply = ceph_msg_get(msg);
3438 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
3441 dout("reply arrived after request %lld was aborted\n", tid);
3443 mutex_unlock(&mdsc->mutex);
3445 mutex_unlock(&session->s_mutex);
3447 /* kick calling process */
3448 complete_request(mdsc, req);
3450 ceph_update_metadata_metrics(&mdsc->metric, req->r_start_latency,
3451 req->r_end_latency, err);
3453 ceph_mdsc_put_request(req);
3455 /* Defer closing the sessions after s_mutex lock being released */
3457 ceph_mdsc_close_sessions(mdsc);
3464 * handle mds notification that our request has been forwarded.
3466 static void handle_forward(struct ceph_mds_client *mdsc,
3467 struct ceph_mds_session *session,
3468 struct ceph_msg *msg)
3470 struct ceph_mds_request *req;
3471 u64 tid = le64_to_cpu(msg->hdr.tid);
3475 void *p = msg->front.iov_base;
3476 void *end = p + msg->front.iov_len;
3477 bool aborted = false;
3479 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3480 next_mds = ceph_decode_32(&p);
3481 fwd_seq = ceph_decode_32(&p);
3483 mutex_lock(&mdsc->mutex);
3484 req = lookup_get_request(mdsc, tid);
3486 mutex_unlock(&mdsc->mutex);
3487 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
3488 return; /* dup reply? */
3491 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
3492 dout("forward tid %llu aborted, unregistering\n", tid);
3493 __unregister_request(mdsc, req);
3494 } else if (fwd_seq <= req->r_num_fwd) {
3496 * The type of 'num_fwd' in ceph 'MClientRequestForward'
3497 * is 'int32_t', while in 'ceph_mds_request_head' the
3498 * type is '__u8'. So in case the request bounces between
3499 * MDSes exceeding 256 times, the client will get stuck.
3501 * In this case it's ususally a bug in MDS and continue
3502 * bouncing the request makes no sense.
3504 * In future this could be fixed in ceph code, so avoid
3505 * using the hardcode here.
3507 int max = sizeof_field(struct ceph_mds_request_head, num_fwd);
3508 max = 1 << (max * BITS_PER_BYTE);
3509 if (req->r_num_fwd >= max) {
3510 mutex_lock(&req->r_fill_mutex);
3511 req->r_err = -EMULTIHOP;
3512 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
3513 mutex_unlock(&req->r_fill_mutex);
3515 pr_warn_ratelimited("forward tid %llu seq overflow\n",
3518 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
3519 tid, next_mds, req->r_num_fwd, fwd_seq);
3522 /* resend. forward race not possible; mds would drop */
3523 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
3525 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
3526 req->r_attempts = 0;
3527 req->r_num_fwd = fwd_seq;
3528 req->r_resend_mds = next_mds;
3529 put_request_session(req);
3530 __do_request(mdsc, req);
3532 mutex_unlock(&mdsc->mutex);
3534 /* kick calling process */
3536 complete_request(mdsc, req);
3537 ceph_mdsc_put_request(req);
3541 pr_err("mdsc_handle_forward decode error err=%d\n", err);
3545 static int __decode_session_metadata(void **p, void *end,
3548 /* map<string,string> */
3551 ceph_decode_32_safe(p, end, n, bad);
3554 ceph_decode_32_safe(p, end, len, bad);
3555 ceph_decode_need(p, end, len, bad);
3556 err_str = !strncmp(*p, "error_string", len);
3558 ceph_decode_32_safe(p, end, len, bad);
3559 ceph_decode_need(p, end, len, bad);
3561 * Match "blocklisted (blacklisted)" from newer MDSes,
3562 * or "blacklisted" from older MDSes.
3564 if (err_str && strnstr(*p, "blacklisted", len))
3565 *blocklisted = true;
3574 * handle a mds session control message
3576 static void handle_session(struct ceph_mds_session *session,
3577 struct ceph_msg *msg)
3579 struct ceph_mds_client *mdsc = session->s_mdsc;
3580 int mds = session->s_mds;
3581 int msg_version = le16_to_cpu(msg->hdr.version);
3582 void *p = msg->front.iov_base;
3583 void *end = p + msg->front.iov_len;
3584 struct ceph_mds_session_head *h;
3586 u64 seq, features = 0;
3588 bool blocklisted = false;
3591 ceph_decode_need(&p, end, sizeof(*h), bad);
3595 op = le32_to_cpu(h->op);
3596 seq = le64_to_cpu(h->seq);
3598 if (msg_version >= 3) {
3600 /* version >= 2 and < 5, decode metadata, skip otherwise
3601 * as it's handled via flags.
3603 if (msg_version >= 5)
3604 ceph_decode_skip_map(&p, end, string, string, bad);
3605 else if (__decode_session_metadata(&p, end, &blocklisted) < 0)
3608 /* version >= 3, feature bits */
3609 ceph_decode_32_safe(&p, end, len, bad);
3611 ceph_decode_64_safe(&p, end, features, bad);
3612 p += len - sizeof(features);
3616 if (msg_version >= 5) {
3620 ceph_decode_skip_16(&p, end, bad); /* struct_v, struct_cv */
3621 ceph_decode_32_safe(&p, end, len, bad); /* len */
3622 ceph_decode_skip_n(&p, end, len, bad); /* metric_spec */
3624 /* version >= 5, flags */
3625 ceph_decode_32_safe(&p, end, flags, bad);
3626 if (flags & CEPH_SESSION_BLOCKLISTED) {
3627 pr_warn("mds%d session blocklisted\n", session->s_mds);
3632 mutex_lock(&mdsc->mutex);
3633 if (op == CEPH_SESSION_CLOSE) {
3634 ceph_get_mds_session(session);
3635 __unregister_session(mdsc, session);
3637 /* FIXME: this ttl calculation is generous */
3638 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
3639 mutex_unlock(&mdsc->mutex);
3641 mutex_lock(&session->s_mutex);
3643 dout("handle_session mds%d %s %p state %s seq %llu\n",
3644 mds, ceph_session_op_name(op), session,
3645 ceph_session_state_name(session->s_state), seq);
3647 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
3648 session->s_state = CEPH_MDS_SESSION_OPEN;
3649 pr_info("mds%d came back\n", session->s_mds);
3653 case CEPH_SESSION_OPEN:
3654 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3655 pr_info("mds%d reconnect success\n", session->s_mds);
3657 if (session->s_state == CEPH_MDS_SESSION_OPEN) {
3658 pr_notice("mds%d is already opened\n", session->s_mds);
3660 session->s_state = CEPH_MDS_SESSION_OPEN;
3661 session->s_features = features;
3662 renewed_caps(mdsc, session, 0);
3663 if (test_bit(CEPHFS_FEATURE_METRIC_COLLECT,
3664 &session->s_features))
3665 metric_schedule_delayed(&mdsc->metric);
3669 * The connection maybe broken and the session in client
3670 * side has been reinitialized, need to update the seq
3673 if (!session->s_seq && seq)
3674 session->s_seq = seq;
3678 __close_session(mdsc, session);
3681 case CEPH_SESSION_RENEWCAPS:
3682 if (session->s_renew_seq == seq)
3683 renewed_caps(mdsc, session, 1);
3686 case CEPH_SESSION_CLOSE:
3687 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3688 pr_info("mds%d reconnect denied\n", session->s_mds);
3689 session->s_state = CEPH_MDS_SESSION_CLOSED;
3690 cleanup_session_requests(mdsc, session);
3691 remove_session_caps(session);
3692 wake = 2; /* for good measure */
3693 wake_up_all(&mdsc->session_close_wq);
3696 case CEPH_SESSION_STALE:
3697 pr_info("mds%d caps went stale, renewing\n",
3699 atomic_inc(&session->s_cap_gen);
3700 session->s_cap_ttl = jiffies - 1;
3701 send_renew_caps(mdsc, session);
3704 case CEPH_SESSION_RECALL_STATE:
3705 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
3708 case CEPH_SESSION_FLUSHMSG:
3709 /* flush cap releases */
3710 spin_lock(&session->s_cap_lock);
3711 if (session->s_num_cap_releases)
3712 ceph_flush_cap_releases(mdsc, session);
3713 spin_unlock(&session->s_cap_lock);
3715 send_flushmsg_ack(mdsc, session, seq);
3718 case CEPH_SESSION_FORCE_RO:
3719 dout("force_session_readonly %p\n", session);
3720 spin_lock(&session->s_cap_lock);
3721 session->s_readonly = true;
3722 spin_unlock(&session->s_cap_lock);
3723 wake_up_session_caps(session, FORCE_RO);
3726 case CEPH_SESSION_REJECT:
3727 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
3728 pr_info("mds%d rejected session\n", session->s_mds);
3729 session->s_state = CEPH_MDS_SESSION_REJECTED;
3730 cleanup_session_requests(mdsc, session);
3731 remove_session_caps(session);
3733 mdsc->fsc->blocklisted = true;
3734 wake = 2; /* for good measure */
3738 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
3742 mutex_unlock(&session->s_mutex);
3744 mutex_lock(&mdsc->mutex);
3745 __wake_requests(mdsc, &session->s_waiting);
3747 kick_requests(mdsc, mds);
3748 mutex_unlock(&mdsc->mutex);
3750 if (op == CEPH_SESSION_CLOSE)
3751 ceph_put_mds_session(session);
3755 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
3756 (int)msg->front.iov_len);
3761 void ceph_mdsc_release_dir_caps(struct ceph_mds_request *req)
3765 dcaps = xchg(&req->r_dir_caps, 0);
3767 dout("releasing r_dir_caps=%s\n", ceph_cap_string(dcaps));
3768 ceph_put_cap_refs(ceph_inode(req->r_parent), dcaps);
3772 void ceph_mdsc_release_dir_caps_no_check(struct ceph_mds_request *req)
3776 dcaps = xchg(&req->r_dir_caps, 0);
3778 dout("releasing r_dir_caps=%s\n", ceph_cap_string(dcaps));
3779 ceph_put_cap_refs_no_check_caps(ceph_inode(req->r_parent),
3785 * called under session->mutex.
3787 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
3788 struct ceph_mds_session *session)
3790 struct ceph_mds_request *req, *nreq;
3793 dout("replay_unsafe_requests mds%d\n", session->s_mds);
3795 mutex_lock(&mdsc->mutex);
3796 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item)
3797 __send_request(session, req, true);
3800 * also re-send old requests when MDS enters reconnect stage. So that MDS
3801 * can process completed request in clientreplay stage.
3803 p = rb_first(&mdsc->request_tree);
3805 req = rb_entry(p, struct ceph_mds_request, r_node);
3807 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
3809 if (req->r_attempts == 0)
3810 continue; /* only old requests */
3811 if (!req->r_session)
3813 if (req->r_session->s_mds != session->s_mds)
3816 ceph_mdsc_release_dir_caps_no_check(req);
3818 __send_request(session, req, true);
3820 mutex_unlock(&mdsc->mutex);
3823 static int send_reconnect_partial(struct ceph_reconnect_state *recon_state)
3825 struct ceph_msg *reply;
3826 struct ceph_pagelist *_pagelist;
3831 if (!recon_state->allow_multi)
3834 /* can't handle message that contains both caps and realm */
3835 BUG_ON(!recon_state->nr_caps == !recon_state->nr_realms);
3837 /* pre-allocate new pagelist */
3838 _pagelist = ceph_pagelist_alloc(GFP_NOFS);
3842 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3846 /* placeholder for nr_caps */
3847 err = ceph_pagelist_encode_32(_pagelist, 0);
3851 if (recon_state->nr_caps) {
3852 /* currently encoding caps */
3853 err = ceph_pagelist_encode_32(recon_state->pagelist, 0);
3857 /* placeholder for nr_realms (currently encoding relams) */
3858 err = ceph_pagelist_encode_32(_pagelist, 0);
3863 err = ceph_pagelist_encode_8(recon_state->pagelist, 1);
3867 page = list_first_entry(&recon_state->pagelist->head, struct page, lru);
3868 addr = kmap_atomic(page);
3869 if (recon_state->nr_caps) {
3870 /* currently encoding caps */
3871 *addr = cpu_to_le32(recon_state->nr_caps);
3873 /* currently encoding relams */
3874 *(addr + 1) = cpu_to_le32(recon_state->nr_realms);
3876 kunmap_atomic(addr);
3878 reply->hdr.version = cpu_to_le16(5);
3879 reply->hdr.compat_version = cpu_to_le16(4);
3881 reply->hdr.data_len = cpu_to_le32(recon_state->pagelist->length);
3882 ceph_msg_data_add_pagelist(reply, recon_state->pagelist);
3884 ceph_con_send(&recon_state->session->s_con, reply);
3885 ceph_pagelist_release(recon_state->pagelist);
3887 recon_state->pagelist = _pagelist;
3888 recon_state->nr_caps = 0;
3889 recon_state->nr_realms = 0;
3890 recon_state->msg_version = 5;
3893 ceph_msg_put(reply);
3895 ceph_pagelist_release(_pagelist);
3899 static struct dentry* d_find_primary(struct inode *inode)
3901 struct dentry *alias, *dn = NULL;
3903 if (hlist_empty(&inode->i_dentry))
3906 spin_lock(&inode->i_lock);
3907 if (hlist_empty(&inode->i_dentry))
3910 if (S_ISDIR(inode->i_mode)) {
3911 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
3912 if (!IS_ROOT(alias))
3917 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
3918 spin_lock(&alias->d_lock);
3919 if (!d_unhashed(alias) &&
3920 (ceph_dentry(alias)->flags & CEPH_DENTRY_PRIMARY_LINK)) {
3921 dn = dget_dlock(alias);
3923 spin_unlock(&alias->d_lock);
3928 spin_unlock(&inode->i_lock);
3933 * Encode information about a cap for a reconnect with the MDS.
3935 static int reconnect_caps_cb(struct inode *inode, int mds, void *arg)
3938 struct ceph_mds_cap_reconnect v2;
3939 struct ceph_mds_cap_reconnect_v1 v1;
3941 struct ceph_inode_info *ci = ceph_inode(inode);
3942 struct ceph_reconnect_state *recon_state = arg;
3943 struct ceph_pagelist *pagelist = recon_state->pagelist;
3944 struct dentry *dentry;
3945 struct ceph_cap *cap;
3947 int pathlen = 0, err;
3951 dentry = d_find_primary(inode);
3953 /* set pathbase to parent dir when msg_version >= 2 */
3954 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase,
3955 recon_state->msg_version >= 2);
3958 err = PTR_ERR(path);
3966 spin_lock(&ci->i_ceph_lock);
3967 cap = __get_cap_for_mds(ci, mds);
3969 spin_unlock(&ci->i_ceph_lock);
3973 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
3974 inode, ceph_vinop(inode), cap, cap->cap_id,
3975 ceph_cap_string(cap->issued));
3977 cap->seq = 0; /* reset cap seq */
3978 cap->issue_seq = 0; /* and issue_seq */
3979 cap->mseq = 0; /* and migrate_seq */
3980 cap->cap_gen = atomic_read(&cap->session->s_cap_gen);
3982 /* These are lost when the session goes away */
3983 if (S_ISDIR(inode->i_mode)) {
3984 if (cap->issued & CEPH_CAP_DIR_CREATE) {
3985 ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
3986 memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
3988 cap->issued &= ~CEPH_CAP_ANY_DIR_OPS;
3991 if (recon_state->msg_version >= 2) {
3992 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
3993 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3994 rec.v2.issued = cpu_to_le32(cap->issued);
3995 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
3996 rec.v2.pathbase = cpu_to_le64(pathbase);
3997 rec.v2.flock_len = (__force __le32)
3998 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
4000 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
4001 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
4002 rec.v1.issued = cpu_to_le32(cap->issued);
4003 rec.v1.size = cpu_to_le64(i_size_read(inode));
4004 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
4005 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
4006 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
4007 rec.v1.pathbase = cpu_to_le64(pathbase);
4010 if (list_empty(&ci->i_cap_snaps)) {
4011 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
4013 struct ceph_cap_snap *capsnap =
4014 list_first_entry(&ci->i_cap_snaps,
4015 struct ceph_cap_snap, ci_item);
4016 snap_follows = capsnap->follows;
4018 spin_unlock(&ci->i_ceph_lock);
4020 if (recon_state->msg_version >= 2) {
4021 int num_fcntl_locks, num_flock_locks;
4022 struct ceph_filelock *flocks = NULL;
4023 size_t struct_len, total_len = sizeof(u64);
4027 if (rec.v2.flock_len) {
4028 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
4030 num_fcntl_locks = 0;
4031 num_flock_locks = 0;
4033 if (num_fcntl_locks + num_flock_locks > 0) {
4034 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
4035 sizeof(struct ceph_filelock),
4041 err = ceph_encode_locks_to_buffer(inode, flocks,
4056 if (recon_state->msg_version >= 3) {
4057 /* version, compat_version and struct_len */
4058 total_len += 2 * sizeof(u8) + sizeof(u32);
4062 * number of encoded locks is stable, so copy to pagelist
4064 struct_len = 2 * sizeof(u32) +
4065 (num_fcntl_locks + num_flock_locks) *
4066 sizeof(struct ceph_filelock);
4067 rec.v2.flock_len = cpu_to_le32(struct_len);
4069 struct_len += sizeof(u32) + pathlen + sizeof(rec.v2);
4072 struct_len += sizeof(u64); /* snap_follows */
4074 total_len += struct_len;
4076 if (pagelist->length + total_len > RECONNECT_MAX_SIZE) {
4077 err = send_reconnect_partial(recon_state);
4079 goto out_freeflocks;
4080 pagelist = recon_state->pagelist;
4083 err = ceph_pagelist_reserve(pagelist, total_len);
4085 goto out_freeflocks;
4087 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
4088 if (recon_state->msg_version >= 3) {
4089 ceph_pagelist_encode_8(pagelist, struct_v);
4090 ceph_pagelist_encode_8(pagelist, 1);
4091 ceph_pagelist_encode_32(pagelist, struct_len);
4093 ceph_pagelist_encode_string(pagelist, path, pathlen);
4094 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
4095 ceph_locks_to_pagelist(flocks, pagelist,
4096 num_fcntl_locks, num_flock_locks);
4098 ceph_pagelist_encode_64(pagelist, snap_follows);
4102 err = ceph_pagelist_reserve(pagelist,
4103 sizeof(u64) + sizeof(u32) +
4104 pathlen + sizeof(rec.v1));
4108 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
4109 ceph_pagelist_encode_string(pagelist, path, pathlen);
4110 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
4114 ceph_mdsc_free_path(path, pathlen);
4116 recon_state->nr_caps++;
4120 static int encode_snap_realms(struct ceph_mds_client *mdsc,
4121 struct ceph_reconnect_state *recon_state)
4124 struct ceph_pagelist *pagelist = recon_state->pagelist;
4127 if (recon_state->msg_version >= 4) {
4128 err = ceph_pagelist_encode_32(pagelist, mdsc->num_snap_realms);
4134 * snaprealms. we provide mds with the ino, seq (version), and
4135 * parent for all of our realms. If the mds has any newer info,
4138 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
4139 struct ceph_snap_realm *realm =
4140 rb_entry(p, struct ceph_snap_realm, node);
4141 struct ceph_mds_snaprealm_reconnect sr_rec;
4143 if (recon_state->msg_version >= 4) {
4144 size_t need = sizeof(u8) * 2 + sizeof(u32) +
4147 if (pagelist->length + need > RECONNECT_MAX_SIZE) {
4148 err = send_reconnect_partial(recon_state);
4151 pagelist = recon_state->pagelist;
4154 err = ceph_pagelist_reserve(pagelist, need);
4158 ceph_pagelist_encode_8(pagelist, 1);
4159 ceph_pagelist_encode_8(pagelist, 1);
4160 ceph_pagelist_encode_32(pagelist, sizeof(sr_rec));
4163 dout(" adding snap realm %llx seq %lld parent %llx\n",
4164 realm->ino, realm->seq, realm->parent_ino);
4165 sr_rec.ino = cpu_to_le64(realm->ino);
4166 sr_rec.seq = cpu_to_le64(realm->seq);
4167 sr_rec.parent = cpu_to_le64(realm->parent_ino);
4169 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
4173 recon_state->nr_realms++;
4181 * If an MDS fails and recovers, clients need to reconnect in order to
4182 * reestablish shared state. This includes all caps issued through
4183 * this session _and_ the snap_realm hierarchy. Because it's not
4184 * clear which snap realms the mds cares about, we send everything we
4185 * know about.. that ensures we'll then get any new info the
4186 * recovering MDS might have.
4188 * This is a relatively heavyweight operation, but it's rare.
4190 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
4191 struct ceph_mds_session *session)
4193 struct ceph_msg *reply;
4194 int mds = session->s_mds;
4196 struct ceph_reconnect_state recon_state = {
4201 pr_info("mds%d reconnect start\n", mds);
4203 recon_state.pagelist = ceph_pagelist_alloc(GFP_NOFS);
4204 if (!recon_state.pagelist)
4205 goto fail_nopagelist;
4207 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
4211 xa_destroy(&session->s_delegated_inos);
4213 mutex_lock(&session->s_mutex);
4214 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
4217 dout("session %p state %s\n", session,
4218 ceph_session_state_name(session->s_state));
4220 atomic_inc(&session->s_cap_gen);
4222 spin_lock(&session->s_cap_lock);
4223 /* don't know if session is readonly */
4224 session->s_readonly = 0;
4226 * notify __ceph_remove_cap() that we are composing cap reconnect.
4227 * If a cap get released before being added to the cap reconnect,
4228 * __ceph_remove_cap() should skip queuing cap release.
4230 session->s_cap_reconnect = 1;
4231 /* drop old cap expires; we're about to reestablish that state */
4232 detach_cap_releases(session, &dispose);
4233 spin_unlock(&session->s_cap_lock);
4234 dispose_cap_releases(mdsc, &dispose);
4236 /* trim unused caps to reduce MDS's cache rejoin time */
4237 if (mdsc->fsc->sb->s_root)
4238 shrink_dcache_parent(mdsc->fsc->sb->s_root);
4240 ceph_con_close(&session->s_con);
4241 ceph_con_open(&session->s_con,
4242 CEPH_ENTITY_TYPE_MDS, mds,
4243 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
4245 /* replay unsafe requests */
4246 replay_unsafe_requests(mdsc, session);
4248 ceph_early_kick_flushing_caps(mdsc, session);
4250 down_read(&mdsc->snap_rwsem);
4252 /* placeholder for nr_caps */
4253 err = ceph_pagelist_encode_32(recon_state.pagelist, 0);
4257 if (test_bit(CEPHFS_FEATURE_MULTI_RECONNECT, &session->s_features)) {
4258 recon_state.msg_version = 3;
4259 recon_state.allow_multi = true;
4260 } else if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) {
4261 recon_state.msg_version = 3;
4263 recon_state.msg_version = 2;
4265 /* trsaverse this session's caps */
4266 err = ceph_iterate_session_caps(session, reconnect_caps_cb, &recon_state);
4268 spin_lock(&session->s_cap_lock);
4269 session->s_cap_reconnect = 0;
4270 spin_unlock(&session->s_cap_lock);
4275 /* check if all realms can be encoded into current message */
4276 if (mdsc->num_snap_realms) {
4278 recon_state.pagelist->length +
4279 mdsc->num_snap_realms *
4280 sizeof(struct ceph_mds_snaprealm_reconnect);
4281 if (recon_state.msg_version >= 4) {
4282 /* number of realms */
4283 total_len += sizeof(u32);
4284 /* version, compat_version and struct_len */
4285 total_len += mdsc->num_snap_realms *
4286 (2 * sizeof(u8) + sizeof(u32));
4288 if (total_len > RECONNECT_MAX_SIZE) {
4289 if (!recon_state.allow_multi) {
4293 if (recon_state.nr_caps) {
4294 err = send_reconnect_partial(&recon_state);
4298 recon_state.msg_version = 5;
4302 err = encode_snap_realms(mdsc, &recon_state);
4306 if (recon_state.msg_version >= 5) {
4307 err = ceph_pagelist_encode_8(recon_state.pagelist, 0);
4312 if (recon_state.nr_caps || recon_state.nr_realms) {
4314 list_first_entry(&recon_state.pagelist->head,
4316 __le32 *addr = kmap_atomic(page);
4317 if (recon_state.nr_caps) {
4318 WARN_ON(recon_state.nr_realms != mdsc->num_snap_realms);
4319 *addr = cpu_to_le32(recon_state.nr_caps);
4320 } else if (recon_state.msg_version >= 4) {
4321 *(addr + 1) = cpu_to_le32(recon_state.nr_realms);
4323 kunmap_atomic(addr);
4326 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
4327 if (recon_state.msg_version >= 4)
4328 reply->hdr.compat_version = cpu_to_le16(4);
4330 reply->hdr.data_len = cpu_to_le32(recon_state.pagelist->length);
4331 ceph_msg_data_add_pagelist(reply, recon_state.pagelist);
4333 ceph_con_send(&session->s_con, reply);
4335 mutex_unlock(&session->s_mutex);
4337 mutex_lock(&mdsc->mutex);
4338 __wake_requests(mdsc, &session->s_waiting);
4339 mutex_unlock(&mdsc->mutex);
4341 up_read(&mdsc->snap_rwsem);
4342 ceph_pagelist_release(recon_state.pagelist);
4346 ceph_msg_put(reply);
4347 up_read(&mdsc->snap_rwsem);
4348 mutex_unlock(&session->s_mutex);
4350 ceph_pagelist_release(recon_state.pagelist);
4352 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
4358 * compare old and new mdsmaps, kicking requests
4359 * and closing out old connections as necessary
4361 * called under mdsc->mutex.
4363 static void check_new_map(struct ceph_mds_client *mdsc,
4364 struct ceph_mdsmap *newmap,
4365 struct ceph_mdsmap *oldmap)
4368 int oldstate, newstate;
4369 struct ceph_mds_session *s;
4370 unsigned long targets[DIV_ROUND_UP(CEPH_MAX_MDS, sizeof(unsigned long))] = {0};
4372 dout("check_new_map new %u old %u\n",
4373 newmap->m_epoch, oldmap->m_epoch);
4375 if (newmap->m_info) {
4376 for (i = 0; i < newmap->possible_max_rank; i++) {
4377 for (j = 0; j < newmap->m_info[i].num_export_targets; j++)
4378 set_bit(newmap->m_info[i].export_targets[j], targets);
4382 for (i = 0; i < oldmap->possible_max_rank && i < mdsc->max_sessions; i++) {
4383 if (!mdsc->sessions[i])
4385 s = mdsc->sessions[i];
4386 oldstate = ceph_mdsmap_get_state(oldmap, i);
4387 newstate = ceph_mdsmap_get_state(newmap, i);
4389 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
4390 i, ceph_mds_state_name(oldstate),
4391 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
4392 ceph_mds_state_name(newstate),
4393 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
4394 ceph_session_state_name(s->s_state));
4396 if (i >= newmap->possible_max_rank) {
4397 /* force close session for stopped mds */
4398 ceph_get_mds_session(s);
4399 __unregister_session(mdsc, s);
4400 __wake_requests(mdsc, &s->s_waiting);
4401 mutex_unlock(&mdsc->mutex);
4403 mutex_lock(&s->s_mutex);
4404 cleanup_session_requests(mdsc, s);
4405 remove_session_caps(s);
4406 mutex_unlock(&s->s_mutex);
4408 ceph_put_mds_session(s);
4410 mutex_lock(&mdsc->mutex);
4411 kick_requests(mdsc, i);
4415 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
4416 ceph_mdsmap_get_addr(newmap, i),
4417 sizeof(struct ceph_entity_addr))) {
4419 mutex_unlock(&mdsc->mutex);
4420 mutex_lock(&s->s_mutex);
4421 mutex_lock(&mdsc->mutex);
4422 ceph_con_close(&s->s_con);
4423 mutex_unlock(&s->s_mutex);
4424 s->s_state = CEPH_MDS_SESSION_RESTARTING;
4425 } else if (oldstate == newstate) {
4426 continue; /* nothing new with this mds */
4432 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
4433 newstate >= CEPH_MDS_STATE_RECONNECT) {
4434 mutex_unlock(&mdsc->mutex);
4435 clear_bit(i, targets);
4436 send_mds_reconnect(mdsc, s);
4437 mutex_lock(&mdsc->mutex);
4441 * kick request on any mds that has gone active.
4443 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
4444 newstate >= CEPH_MDS_STATE_ACTIVE) {
4445 if (oldstate != CEPH_MDS_STATE_CREATING &&
4446 oldstate != CEPH_MDS_STATE_STARTING)
4447 pr_info("mds%d recovery completed\n", s->s_mds);
4448 kick_requests(mdsc, i);
4449 mutex_unlock(&mdsc->mutex);
4450 mutex_lock(&s->s_mutex);
4451 mutex_lock(&mdsc->mutex);
4452 ceph_kick_flushing_caps(mdsc, s);
4453 mutex_unlock(&s->s_mutex);
4454 wake_up_session_caps(s, RECONNECT);
4459 * Only open and reconnect sessions that don't exist yet.
4461 for (i = 0; i < newmap->possible_max_rank; i++) {
4463 * In case the import MDS is crashed just after
4464 * the EImportStart journal is flushed, so when
4465 * a standby MDS takes over it and is replaying
4466 * the EImportStart journal the new MDS daemon
4467 * will wait the client to reconnect it, but the
4468 * client may never register/open the session yet.
4470 * Will try to reconnect that MDS daemon if the
4471 * rank number is in the export targets array and
4472 * is the up:reconnect state.
4474 newstate = ceph_mdsmap_get_state(newmap, i);
4475 if (!test_bit(i, targets) || newstate != CEPH_MDS_STATE_RECONNECT)
4479 * The session maybe registered and opened by some
4480 * requests which were choosing random MDSes during
4481 * the mdsc->mutex's unlock/lock gap below in rare
4482 * case. But the related MDS daemon will just queue
4483 * that requests and be still waiting for the client's
4484 * reconnection request in up:reconnect state.
4486 s = __ceph_lookup_mds_session(mdsc, i);
4488 s = __open_export_target_session(mdsc, i);
4491 pr_err("failed to open export target session, err %d\n",
4496 dout("send reconnect to export target mds.%d\n", i);
4497 mutex_unlock(&mdsc->mutex);
4498 send_mds_reconnect(mdsc, s);
4499 ceph_put_mds_session(s);
4500 mutex_lock(&mdsc->mutex);
4503 for (i = 0; i < newmap->possible_max_rank && i < mdsc->max_sessions; i++) {
4504 s = mdsc->sessions[i];
4507 if (!ceph_mdsmap_is_laggy(newmap, i))
4509 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4510 s->s_state == CEPH_MDS_SESSION_HUNG ||
4511 s->s_state == CEPH_MDS_SESSION_CLOSING) {
4512 dout(" connecting to export targets of laggy mds%d\n",
4514 __open_export_target_sessions(mdsc, s);
4526 * caller must hold session s_mutex, dentry->d_lock
4528 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
4530 struct ceph_dentry_info *di = ceph_dentry(dentry);
4532 ceph_put_mds_session(di->lease_session);
4533 di->lease_session = NULL;
4536 static void handle_lease(struct ceph_mds_client *mdsc,
4537 struct ceph_mds_session *session,
4538 struct ceph_msg *msg)
4540 struct super_block *sb = mdsc->fsc->sb;
4541 struct inode *inode;
4542 struct dentry *parent, *dentry;
4543 struct ceph_dentry_info *di;
4544 int mds = session->s_mds;
4545 struct ceph_mds_lease *h = msg->front.iov_base;
4547 struct ceph_vino vino;
4551 dout("handle_lease from mds%d\n", mds);
4554 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
4556 vino.ino = le64_to_cpu(h->ino);
4557 vino.snap = CEPH_NOSNAP;
4558 seq = le32_to_cpu(h->seq);
4559 dname.len = get_unaligned_le32(h + 1);
4560 if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
4562 dname.name = (void *)(h + 1) + sizeof(u32);
4565 inode = ceph_find_inode(sb, vino);
4566 dout("handle_lease %s, ino %llx %p %.*s\n",
4567 ceph_lease_op_name(h->action), vino.ino, inode,
4568 dname.len, dname.name);
4570 mutex_lock(&session->s_mutex);
4571 inc_session_sequence(session);
4574 dout("handle_lease no inode %llx\n", vino.ino);
4579 parent = d_find_alias(inode);
4581 dout("no parent dentry on inode %p\n", inode);
4583 goto release; /* hrm... */
4585 dname.hash = full_name_hash(parent, dname.name, dname.len);
4586 dentry = d_lookup(parent, &dname);
4591 spin_lock(&dentry->d_lock);
4592 di = ceph_dentry(dentry);
4593 switch (h->action) {
4594 case CEPH_MDS_LEASE_REVOKE:
4595 if (di->lease_session == session) {
4596 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
4597 h->seq = cpu_to_le32(di->lease_seq);
4598 __ceph_mdsc_drop_dentry_lease(dentry);
4603 case CEPH_MDS_LEASE_RENEW:
4604 if (di->lease_session == session &&
4605 di->lease_gen == atomic_read(&session->s_cap_gen) &&
4606 di->lease_renew_from &&
4607 di->lease_renew_after == 0) {
4608 unsigned long duration =
4609 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
4611 di->lease_seq = seq;
4612 di->time = di->lease_renew_from + duration;
4613 di->lease_renew_after = di->lease_renew_from +
4615 di->lease_renew_from = 0;
4619 spin_unlock(&dentry->d_lock);
4626 /* let's just reuse the same message */
4627 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
4629 ceph_con_send(&session->s_con, msg);
4632 mutex_unlock(&session->s_mutex);
4637 pr_err("corrupt lease message\n");
4641 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
4642 struct dentry *dentry, char action,
4645 struct ceph_msg *msg;
4646 struct ceph_mds_lease *lease;
4648 int len = sizeof(*lease) + sizeof(u32) + NAME_MAX;
4650 dout("lease_send_msg identry %p %s to mds%d\n",
4651 dentry, ceph_lease_op_name(action), session->s_mds);
4653 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
4656 lease = msg->front.iov_base;
4657 lease->action = action;
4658 lease->seq = cpu_to_le32(seq);
4660 spin_lock(&dentry->d_lock);
4661 dir = d_inode(dentry->d_parent);
4662 lease->ino = cpu_to_le64(ceph_ino(dir));
4663 lease->first = lease->last = cpu_to_le64(ceph_snap(dir));
4665 put_unaligned_le32(dentry->d_name.len, lease + 1);
4666 memcpy((void *)(lease + 1) + 4,
4667 dentry->d_name.name, dentry->d_name.len);
4668 spin_unlock(&dentry->d_lock);
4670 ceph_con_send(&session->s_con, msg);
4674 * lock unlock the session, to wait ongoing session activities
4676 static void lock_unlock_session(struct ceph_mds_session *s)
4678 mutex_lock(&s->s_mutex);
4679 mutex_unlock(&s->s_mutex);
4682 static void maybe_recover_session(struct ceph_mds_client *mdsc)
4684 struct ceph_fs_client *fsc = mdsc->fsc;
4686 if (!ceph_test_mount_opt(fsc, CLEANRECOVER))
4689 if (READ_ONCE(fsc->mount_state) != CEPH_MOUNT_MOUNTED)
4692 if (!READ_ONCE(fsc->blocklisted))
4695 pr_info("auto reconnect after blocklisted\n");
4696 ceph_force_reconnect(fsc->sb);
4699 bool check_session_state(struct ceph_mds_session *s)
4701 switch (s->s_state) {
4702 case CEPH_MDS_SESSION_OPEN:
4703 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
4704 s->s_state = CEPH_MDS_SESSION_HUNG;
4705 pr_info("mds%d hung\n", s->s_mds);
4708 case CEPH_MDS_SESSION_CLOSING:
4709 case CEPH_MDS_SESSION_NEW:
4710 case CEPH_MDS_SESSION_RESTARTING:
4711 case CEPH_MDS_SESSION_CLOSED:
4712 case CEPH_MDS_SESSION_REJECTED:
4720 * If the sequence is incremented while we're waiting on a REQUEST_CLOSE reply,
4721 * then we need to retransmit that request.
4723 void inc_session_sequence(struct ceph_mds_session *s)
4725 lockdep_assert_held(&s->s_mutex);
4729 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
4732 dout("resending session close request for mds%d\n", s->s_mds);
4733 ret = request_close_session(s);
4735 pr_err("unable to close session to mds%d: %d\n",
4741 * delayed work -- periodically trim expired leases, renew caps with mds. If
4742 * the @delay parameter is set to 0 or if it's more than 5 secs, the default
4743 * workqueue delay value of 5 secs will be used.
4745 static void schedule_delayed(struct ceph_mds_client *mdsc, unsigned long delay)
4747 unsigned long max_delay = HZ * 5;
4749 /* 5 secs default delay */
4750 if (!delay || (delay > max_delay))
4752 schedule_delayed_work(&mdsc->delayed_work,
4753 round_jiffies_relative(delay));
4756 static void delayed_work(struct work_struct *work)
4758 struct ceph_mds_client *mdsc =
4759 container_of(work, struct ceph_mds_client, delayed_work.work);
4760 unsigned long delay;
4765 dout("mdsc delayed_work\n");
4767 if (mdsc->stopping >= CEPH_MDSC_STOPPING_FLUSHED)
4770 mutex_lock(&mdsc->mutex);
4771 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
4772 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
4773 mdsc->last_renew_caps);
4775 mdsc->last_renew_caps = jiffies;
4777 for (i = 0; i < mdsc->max_sessions; i++) {
4778 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
4782 if (!check_session_state(s)) {
4783 ceph_put_mds_session(s);
4786 mutex_unlock(&mdsc->mutex);
4788 mutex_lock(&s->s_mutex);
4790 send_renew_caps(mdsc, s);
4792 ceph_con_keepalive(&s->s_con);
4793 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4794 s->s_state == CEPH_MDS_SESSION_HUNG)
4795 ceph_send_cap_releases(mdsc, s);
4796 mutex_unlock(&s->s_mutex);
4797 ceph_put_mds_session(s);
4799 mutex_lock(&mdsc->mutex);
4801 mutex_unlock(&mdsc->mutex);
4803 delay = ceph_check_delayed_caps(mdsc);
4805 ceph_queue_cap_reclaim_work(mdsc);
4807 ceph_trim_snapid_map(mdsc);
4809 maybe_recover_session(mdsc);
4811 schedule_delayed(mdsc, delay);
4814 int ceph_mdsc_init(struct ceph_fs_client *fsc)
4817 struct ceph_mds_client *mdsc;
4820 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
4824 mutex_init(&mdsc->mutex);
4825 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
4826 if (!mdsc->mdsmap) {
4831 init_completion(&mdsc->safe_umount_waiters);
4832 init_waitqueue_head(&mdsc->session_close_wq);
4833 INIT_LIST_HEAD(&mdsc->waiting_for_map);
4834 mdsc->quotarealms_inodes = RB_ROOT;
4835 mutex_init(&mdsc->quotarealms_inodes_mutex);
4836 init_rwsem(&mdsc->snap_rwsem);
4837 mdsc->snap_realms = RB_ROOT;
4838 INIT_LIST_HEAD(&mdsc->snap_empty);
4839 spin_lock_init(&mdsc->snap_empty_lock);
4840 mdsc->request_tree = RB_ROOT;
4841 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
4842 mdsc->last_renew_caps = jiffies;
4843 INIT_LIST_HEAD(&mdsc->cap_delay_list);
4844 INIT_LIST_HEAD(&mdsc->cap_wait_list);
4845 spin_lock_init(&mdsc->cap_delay_lock);
4846 INIT_LIST_HEAD(&mdsc->snap_flush_list);
4847 spin_lock_init(&mdsc->snap_flush_lock);
4848 mdsc->last_cap_flush_tid = 1;
4849 INIT_LIST_HEAD(&mdsc->cap_flush_list);
4850 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
4851 spin_lock_init(&mdsc->cap_dirty_lock);
4852 init_waitqueue_head(&mdsc->cap_flushing_wq);
4853 INIT_WORK(&mdsc->cap_reclaim_work, ceph_cap_reclaim_work);
4854 err = ceph_metric_init(&mdsc->metric);
4858 spin_lock_init(&mdsc->dentry_list_lock);
4859 INIT_LIST_HEAD(&mdsc->dentry_leases);
4860 INIT_LIST_HEAD(&mdsc->dentry_dir_leases);
4862 ceph_caps_init(mdsc);
4863 ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
4865 spin_lock_init(&mdsc->snapid_map_lock);
4866 mdsc->snapid_map_tree = RB_ROOT;
4867 INIT_LIST_HEAD(&mdsc->snapid_map_lru);
4869 init_rwsem(&mdsc->pool_perm_rwsem);
4870 mdsc->pool_perm_tree = RB_ROOT;
4872 strscpy(mdsc->nodename, utsname()->nodename,
4873 sizeof(mdsc->nodename));
4879 kfree(mdsc->mdsmap);
4886 * Wait for safe replies on open mds requests. If we time out, drop
4887 * all requests from the tree to avoid dangling dentry refs.
4889 static void wait_requests(struct ceph_mds_client *mdsc)
4891 struct ceph_options *opts = mdsc->fsc->client->options;
4892 struct ceph_mds_request *req;
4894 mutex_lock(&mdsc->mutex);
4895 if (__get_oldest_req(mdsc)) {
4896 mutex_unlock(&mdsc->mutex);
4898 dout("wait_requests waiting for requests\n");
4899 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
4900 ceph_timeout_jiffies(opts->mount_timeout));
4902 /* tear down remaining requests */
4903 mutex_lock(&mdsc->mutex);
4904 while ((req = __get_oldest_req(mdsc))) {
4905 dout("wait_requests timed out on tid %llu\n",
4907 list_del_init(&req->r_wait);
4908 __unregister_request(mdsc, req);
4911 mutex_unlock(&mdsc->mutex);
4912 dout("wait_requests done\n");
4915 void send_flush_mdlog(struct ceph_mds_session *s)
4917 struct ceph_msg *msg;
4920 * Pre-luminous MDS crashes when it sees an unknown session request
4922 if (!CEPH_HAVE_FEATURE(s->s_con.peer_features, SERVER_LUMINOUS))
4925 mutex_lock(&s->s_mutex);
4926 dout("request mdlog flush to mds%d (%s)s seq %lld\n", s->s_mds,
4927 ceph_session_state_name(s->s_state), s->s_seq);
4928 msg = ceph_create_session_msg(CEPH_SESSION_REQUEST_FLUSH_MDLOG,
4931 pr_err("failed to request mdlog flush to mds%d (%s) seq %lld\n",
4932 s->s_mds, ceph_session_state_name(s->s_state), s->s_seq);
4934 ceph_con_send(&s->s_con, msg);
4936 mutex_unlock(&s->s_mutex);
4940 * called before mount is ro, and before dentries are torn down.
4941 * (hmm, does this still race with new lookups?)
4943 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
4945 dout("pre_umount\n");
4946 mdsc->stopping = CEPH_MDSC_STOPPING_BEGIN;
4948 ceph_mdsc_iterate_sessions(mdsc, send_flush_mdlog, true);
4949 ceph_mdsc_iterate_sessions(mdsc, lock_unlock_session, false);
4950 ceph_flush_dirty_caps(mdsc);
4951 wait_requests(mdsc);
4954 * wait for reply handlers to drop their request refs and
4955 * their inode/dcache refs
4959 ceph_cleanup_quotarealms_inodes(mdsc);
4963 * flush the mdlog and wait for all write mds requests to flush.
4965 static void flush_mdlog_and_wait_mdsc_unsafe_requests(struct ceph_mds_client *mdsc,
4968 struct ceph_mds_request *req = NULL, *nextreq;
4969 struct ceph_mds_session *last_session = NULL;
4972 mutex_lock(&mdsc->mutex);
4973 dout("%s want %lld\n", __func__, want_tid);
4975 req = __get_oldest_req(mdsc);
4976 while (req && req->r_tid <= want_tid) {
4977 /* find next request */
4978 n = rb_next(&req->r_node);
4980 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
4983 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
4984 (req->r_op & CEPH_MDS_OP_WRITE)) {
4985 struct ceph_mds_session *s = req->r_session;
4993 ceph_mdsc_get_request(req);
4995 ceph_mdsc_get_request(nextreq);
4996 s = ceph_get_mds_session(s);
4997 mutex_unlock(&mdsc->mutex);
4999 /* send flush mdlog request to MDS */
5000 if (last_session != s) {
5001 send_flush_mdlog(s);
5002 ceph_put_mds_session(last_session);
5005 ceph_put_mds_session(s);
5007 dout("%s wait on %llu (want %llu)\n", __func__,
5008 req->r_tid, want_tid);
5009 wait_for_completion(&req->r_safe_completion);
5011 mutex_lock(&mdsc->mutex);
5012 ceph_mdsc_put_request(req);
5014 break; /* next dne before, so we're done! */
5015 if (RB_EMPTY_NODE(&nextreq->r_node)) {
5016 /* next request was removed from tree */
5017 ceph_mdsc_put_request(nextreq);
5020 ceph_mdsc_put_request(nextreq); /* won't go away */
5024 mutex_unlock(&mdsc->mutex);
5025 ceph_put_mds_session(last_session);
5026 dout("%s done\n", __func__);
5029 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
5031 u64 want_tid, want_flush;
5033 if (READ_ONCE(mdsc->fsc->mount_state) >= CEPH_MOUNT_SHUTDOWN)
5037 mutex_lock(&mdsc->mutex);
5038 want_tid = mdsc->last_tid;
5039 mutex_unlock(&mdsc->mutex);
5041 ceph_flush_dirty_caps(mdsc);
5042 spin_lock(&mdsc->cap_dirty_lock);
5043 want_flush = mdsc->last_cap_flush_tid;
5044 if (!list_empty(&mdsc->cap_flush_list)) {
5045 struct ceph_cap_flush *cf =
5046 list_last_entry(&mdsc->cap_flush_list,
5047 struct ceph_cap_flush, g_list);
5050 spin_unlock(&mdsc->cap_dirty_lock);
5052 dout("sync want tid %lld flush_seq %lld\n",
5053 want_tid, want_flush);
5055 flush_mdlog_and_wait_mdsc_unsafe_requests(mdsc, want_tid);
5056 wait_caps_flush(mdsc, want_flush);
5060 * true if all sessions are closed, or we force unmount
5062 static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
5064 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
5066 return atomic_read(&mdsc->num_sessions) <= skipped;
5070 * called after sb is ro or when metadata corrupted.
5072 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
5074 struct ceph_options *opts = mdsc->fsc->client->options;
5075 struct ceph_mds_session *session;
5079 dout("close_sessions\n");
5081 /* close sessions */
5082 mutex_lock(&mdsc->mutex);
5083 for (i = 0; i < mdsc->max_sessions; i++) {
5084 session = __ceph_lookup_mds_session(mdsc, i);
5087 mutex_unlock(&mdsc->mutex);
5088 mutex_lock(&session->s_mutex);
5089 if (__close_session(mdsc, session) <= 0)
5091 mutex_unlock(&session->s_mutex);
5092 ceph_put_mds_session(session);
5093 mutex_lock(&mdsc->mutex);
5095 mutex_unlock(&mdsc->mutex);
5097 dout("waiting for sessions to close\n");
5098 wait_event_timeout(mdsc->session_close_wq,
5099 done_closing_sessions(mdsc, skipped),
5100 ceph_timeout_jiffies(opts->mount_timeout));
5102 /* tear down remaining sessions */
5103 mutex_lock(&mdsc->mutex);
5104 for (i = 0; i < mdsc->max_sessions; i++) {
5105 if (mdsc->sessions[i]) {
5106 session = ceph_get_mds_session(mdsc->sessions[i]);
5107 __unregister_session(mdsc, session);
5108 mutex_unlock(&mdsc->mutex);
5109 mutex_lock(&session->s_mutex);
5110 remove_session_caps(session);
5111 mutex_unlock(&session->s_mutex);
5112 ceph_put_mds_session(session);
5113 mutex_lock(&mdsc->mutex);
5116 WARN_ON(!list_empty(&mdsc->cap_delay_list));
5117 mutex_unlock(&mdsc->mutex);
5119 ceph_cleanup_snapid_map(mdsc);
5120 ceph_cleanup_global_and_empty_realms(mdsc);
5122 cancel_work_sync(&mdsc->cap_reclaim_work);
5123 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
5128 void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
5130 struct ceph_mds_session *session;
5133 dout("force umount\n");
5135 mutex_lock(&mdsc->mutex);
5136 for (mds = 0; mds < mdsc->max_sessions; mds++) {
5137 session = __ceph_lookup_mds_session(mdsc, mds);
5141 if (session->s_state == CEPH_MDS_SESSION_REJECTED)
5142 __unregister_session(mdsc, session);
5143 __wake_requests(mdsc, &session->s_waiting);
5144 mutex_unlock(&mdsc->mutex);
5146 mutex_lock(&session->s_mutex);
5147 __close_session(mdsc, session);
5148 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
5149 cleanup_session_requests(mdsc, session);
5150 remove_session_caps(session);
5152 mutex_unlock(&session->s_mutex);
5153 ceph_put_mds_session(session);
5155 mutex_lock(&mdsc->mutex);
5156 kick_requests(mdsc, mds);
5158 __wake_requests(mdsc, &mdsc->waiting_for_map);
5159 mutex_unlock(&mdsc->mutex);
5162 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
5166 * Make sure the delayed work stopped before releasing
5169 * Because the cancel_delayed_work_sync() will only
5170 * guarantee that the work finishes executing. But the
5171 * delayed work will re-arm itself again after that.
5173 flush_delayed_work(&mdsc->delayed_work);
5176 ceph_mdsmap_destroy(mdsc->mdsmap);
5177 kfree(mdsc->sessions);
5178 ceph_caps_finalize(mdsc);
5179 ceph_pool_perm_destroy(mdsc);
5182 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
5184 struct ceph_mds_client *mdsc = fsc->mdsc;
5185 dout("mdsc_destroy %p\n", mdsc);
5190 /* flush out any connection work with references to us */
5193 ceph_mdsc_stop(mdsc);
5195 ceph_metric_destroy(&mdsc->metric);
5199 dout("mdsc_destroy %p done\n", mdsc);
5202 void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
5204 struct ceph_fs_client *fsc = mdsc->fsc;
5205 const char *mds_namespace = fsc->mount_options->mds_namespace;
5206 void *p = msg->front.iov_base;
5207 void *end = p + msg->front.iov_len;
5210 u32 mount_fscid = (u32)-1;
5213 ceph_decode_need(&p, end, sizeof(u32), bad);
5214 epoch = ceph_decode_32(&p);
5216 dout("handle_fsmap epoch %u\n", epoch);
5218 /* struct_v, struct_cv, map_len, epoch, legacy_client_fscid */
5219 ceph_decode_skip_n(&p, end, 2 + sizeof(u32) * 3, bad);
5221 ceph_decode_32_safe(&p, end, num_fs, bad);
5222 while (num_fs-- > 0) {
5223 void *info_p, *info_end;
5227 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
5228 p += 2; // info_v, info_cv
5229 info_len = ceph_decode_32(&p);
5230 ceph_decode_need(&p, end, info_len, bad);
5232 info_end = p + info_len;
5235 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
5236 fscid = ceph_decode_32(&info_p);
5237 namelen = ceph_decode_32(&info_p);
5238 ceph_decode_need(&info_p, info_end, namelen, bad);
5240 if (mds_namespace &&
5241 strlen(mds_namespace) == namelen &&
5242 !strncmp(mds_namespace, (char *)info_p, namelen)) {
5243 mount_fscid = fscid;
5248 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
5249 if (mount_fscid != (u32)-1) {
5250 fsc->client->monc.fs_cluster_id = mount_fscid;
5251 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
5253 ceph_monc_renew_subs(&fsc->client->monc);
5261 pr_err("error decoding fsmap %d. Shutting down mount.\n", err);
5262 ceph_umount_begin(mdsc->fsc->sb);
5265 mutex_lock(&mdsc->mutex);
5266 mdsc->mdsmap_err = err;
5267 __wake_requests(mdsc, &mdsc->waiting_for_map);
5268 mutex_unlock(&mdsc->mutex);
5272 * handle mds map update.
5274 void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
5278 void *p = msg->front.iov_base;
5279 void *end = p + msg->front.iov_len;
5280 struct ceph_mdsmap *newmap, *oldmap;
5281 struct ceph_fsid fsid;
5284 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
5285 ceph_decode_copy(&p, &fsid, sizeof(fsid));
5286 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
5288 epoch = ceph_decode_32(&p);
5289 maplen = ceph_decode_32(&p);
5290 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
5292 /* do we need it? */
5293 mutex_lock(&mdsc->mutex);
5294 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
5295 dout("handle_map epoch %u <= our %u\n",
5296 epoch, mdsc->mdsmap->m_epoch);
5297 mutex_unlock(&mdsc->mutex);
5301 newmap = ceph_mdsmap_decode(&p, end, ceph_msgr2(mdsc->fsc->client));
5302 if (IS_ERR(newmap)) {
5303 err = PTR_ERR(newmap);
5307 /* swap into place */
5309 oldmap = mdsc->mdsmap;
5310 mdsc->mdsmap = newmap;
5311 check_new_map(mdsc, newmap, oldmap);
5312 ceph_mdsmap_destroy(oldmap);
5314 mdsc->mdsmap = newmap; /* first mds map */
5316 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
5319 __wake_requests(mdsc, &mdsc->waiting_for_map);
5320 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
5321 mdsc->mdsmap->m_epoch);
5323 mutex_unlock(&mdsc->mutex);
5324 schedule_delayed(mdsc, 0);
5328 mutex_unlock(&mdsc->mutex);
5330 pr_err("error decoding mdsmap %d. Shutting down mount.\n", err);
5331 ceph_umount_begin(mdsc->fsc->sb);
5336 static struct ceph_connection *mds_get_con(struct ceph_connection *con)
5338 struct ceph_mds_session *s = con->private;
5340 if (ceph_get_mds_session(s))
5345 static void mds_put_con(struct ceph_connection *con)
5347 struct ceph_mds_session *s = con->private;
5349 ceph_put_mds_session(s);
5353 * if the client is unresponsive for long enough, the mds will kill
5354 * the session entirely.
5356 static void mds_peer_reset(struct ceph_connection *con)
5358 struct ceph_mds_session *s = con->private;
5359 struct ceph_mds_client *mdsc = s->s_mdsc;
5361 pr_warn("mds%d closed our session\n", s->s_mds);
5362 if (READ_ONCE(mdsc->fsc->mount_state) != CEPH_MOUNT_FENCE_IO)
5363 send_mds_reconnect(mdsc, s);
5366 static void mds_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
5368 struct ceph_mds_session *s = con->private;
5369 struct ceph_mds_client *mdsc = s->s_mdsc;
5370 int type = le16_to_cpu(msg->hdr.type);
5372 mutex_lock(&mdsc->mutex);
5373 if (__verify_registered_session(mdsc, s) < 0) {
5374 mutex_unlock(&mdsc->mutex);
5377 mutex_unlock(&mdsc->mutex);
5380 case CEPH_MSG_MDS_MAP:
5381 ceph_mdsc_handle_mdsmap(mdsc, msg);
5383 case CEPH_MSG_FS_MAP_USER:
5384 ceph_mdsc_handle_fsmap(mdsc, msg);
5386 case CEPH_MSG_CLIENT_SESSION:
5387 handle_session(s, msg);
5389 case CEPH_MSG_CLIENT_REPLY:
5390 handle_reply(s, msg);
5392 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
5393 handle_forward(mdsc, s, msg);
5395 case CEPH_MSG_CLIENT_CAPS:
5396 ceph_handle_caps(s, msg);
5398 case CEPH_MSG_CLIENT_SNAP:
5399 ceph_handle_snap(mdsc, s, msg);
5401 case CEPH_MSG_CLIENT_LEASE:
5402 handle_lease(mdsc, s, msg);
5404 case CEPH_MSG_CLIENT_QUOTA:
5405 ceph_handle_quota(mdsc, s, msg);
5409 pr_err("received unknown message type %d %s\n", type,
5410 ceph_msg_type_name(type));
5421 * Note: returned pointer is the address of a structure that's
5422 * managed separately. Caller must *not* attempt to free it.
5424 static struct ceph_auth_handshake *
5425 mds_get_authorizer(struct ceph_connection *con, int *proto, int force_new)
5427 struct ceph_mds_session *s = con->private;
5428 struct ceph_mds_client *mdsc = s->s_mdsc;
5429 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
5430 struct ceph_auth_handshake *auth = &s->s_auth;
5433 ret = __ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_MDS,
5434 force_new, proto, NULL, NULL);
5436 return ERR_PTR(ret);
5441 static int mds_add_authorizer_challenge(struct ceph_connection *con,
5442 void *challenge_buf, int challenge_buf_len)
5444 struct ceph_mds_session *s = con->private;
5445 struct ceph_mds_client *mdsc = s->s_mdsc;
5446 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
5448 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
5449 challenge_buf, challenge_buf_len);
5452 static int mds_verify_authorizer_reply(struct ceph_connection *con)
5454 struct ceph_mds_session *s = con->private;
5455 struct ceph_mds_client *mdsc = s->s_mdsc;
5456 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
5457 struct ceph_auth_handshake *auth = &s->s_auth;
5459 return ceph_auth_verify_authorizer_reply(ac, auth->authorizer,
5460 auth->authorizer_reply_buf, auth->authorizer_reply_buf_len,
5461 NULL, NULL, NULL, NULL);
5464 static int mds_invalidate_authorizer(struct ceph_connection *con)
5466 struct ceph_mds_session *s = con->private;
5467 struct ceph_mds_client *mdsc = s->s_mdsc;
5468 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
5470 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
5472 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
5475 static int mds_get_auth_request(struct ceph_connection *con,
5476 void *buf, int *buf_len,
5477 void **authorizer, int *authorizer_len)
5479 struct ceph_mds_session *s = con->private;
5480 struct ceph_auth_client *ac = s->s_mdsc->fsc->client->monc.auth;
5481 struct ceph_auth_handshake *auth = &s->s_auth;
5484 ret = ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_MDS,
5489 *authorizer = auth->authorizer_buf;
5490 *authorizer_len = auth->authorizer_buf_len;
5494 static int mds_handle_auth_reply_more(struct ceph_connection *con,
5495 void *reply, int reply_len,
5496 void *buf, int *buf_len,
5497 void **authorizer, int *authorizer_len)
5499 struct ceph_mds_session *s = con->private;
5500 struct ceph_auth_client *ac = s->s_mdsc->fsc->client->monc.auth;
5501 struct ceph_auth_handshake *auth = &s->s_auth;
5504 ret = ceph_auth_handle_svc_reply_more(ac, auth, reply, reply_len,
5509 *authorizer = auth->authorizer_buf;
5510 *authorizer_len = auth->authorizer_buf_len;
5514 static int mds_handle_auth_done(struct ceph_connection *con,
5515 u64 global_id, void *reply, int reply_len,
5516 u8 *session_key, int *session_key_len,
5517 u8 *con_secret, int *con_secret_len)
5519 struct ceph_mds_session *s = con->private;
5520 struct ceph_auth_client *ac = s->s_mdsc->fsc->client->monc.auth;
5521 struct ceph_auth_handshake *auth = &s->s_auth;
5523 return ceph_auth_handle_svc_reply_done(ac, auth, reply, reply_len,
5524 session_key, session_key_len,
5525 con_secret, con_secret_len);
5528 static int mds_handle_auth_bad_method(struct ceph_connection *con,
5529 int used_proto, int result,
5530 const int *allowed_protos, int proto_cnt,
5531 const int *allowed_modes, int mode_cnt)
5533 struct ceph_mds_session *s = con->private;
5534 struct ceph_mon_client *monc = &s->s_mdsc->fsc->client->monc;
5537 if (ceph_auth_handle_bad_authorizer(monc->auth, CEPH_ENTITY_TYPE_MDS,
5539 allowed_protos, proto_cnt,
5540 allowed_modes, mode_cnt)) {
5541 ret = ceph_monc_validate_auth(monc);
5549 static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
5550 struct ceph_msg_header *hdr, int *skip)
5552 struct ceph_msg *msg;
5553 int type = (int) le16_to_cpu(hdr->type);
5554 int front_len = (int) le32_to_cpu(hdr->front_len);
5560 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
5562 pr_err("unable to allocate msg type %d len %d\n",
5570 static int mds_sign_message(struct ceph_msg *msg)
5572 struct ceph_mds_session *s = msg->con->private;
5573 struct ceph_auth_handshake *auth = &s->s_auth;
5575 return ceph_auth_sign_message(auth, msg);
5578 static int mds_check_message_signature(struct ceph_msg *msg)
5580 struct ceph_mds_session *s = msg->con->private;
5581 struct ceph_auth_handshake *auth = &s->s_auth;
5583 return ceph_auth_check_message_signature(auth, msg);
5586 static const struct ceph_connection_operations mds_con_ops = {
5589 .alloc_msg = mds_alloc_msg,
5590 .dispatch = mds_dispatch,
5591 .peer_reset = mds_peer_reset,
5592 .get_authorizer = mds_get_authorizer,
5593 .add_authorizer_challenge = mds_add_authorizer_challenge,
5594 .verify_authorizer_reply = mds_verify_authorizer_reply,
5595 .invalidate_authorizer = mds_invalidate_authorizer,
5596 .sign_message = mds_sign_message,
5597 .check_message_signature = mds_check_message_signature,
5598 .get_auth_request = mds_get_auth_request,
5599 .handle_auth_reply_more = mds_handle_auth_reply_more,
5600 .handle_auth_done = mds_handle_auth_done,
5601 .handle_auth_bad_method = mds_handle_auth_bad_method,