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>
14 #include "mds_client.h"
16 #include <linux/ceph/ceph_features.h>
17 #include <linux/ceph/messenger.h>
18 #include <linux/ceph/decode.h>
19 #include <linux/ceph/pagelist.h>
20 #include <linux/ceph/auth.h>
21 #include <linux/ceph/debugfs.h>
23 #define RECONNECT_MAX_SIZE (INT_MAX - PAGE_SIZE)
26 * A cluster of MDS (metadata server) daemons is responsible for
27 * managing the file system namespace (the directory hierarchy and
28 * inodes) and for coordinating shared access to storage. Metadata is
29 * partitioning hierarchically across a number of servers, and that
30 * partition varies over time as the cluster adjusts the distribution
31 * in order to balance load.
33 * The MDS client is primarily responsible to managing synchronous
34 * metadata requests for operations like open, unlink, and so forth.
35 * If there is a MDS failure, we find out about it when we (possibly
36 * request and) receive a new MDS map, and can resubmit affected
39 * For the most part, though, we take advantage of a lossless
40 * communications channel to the MDS, and do not need to worry about
41 * timing out or resubmitting requests.
43 * We maintain a stateful "session" with each MDS we interact with.
44 * Within each session, we sent periodic heartbeat messages to ensure
45 * any capabilities or leases we have been issues remain valid. If
46 * the session times out and goes stale, our leases and capabilities
47 * are no longer valid.
50 struct ceph_reconnect_state {
51 struct ceph_mds_session *session;
52 int nr_caps, nr_realms;
53 struct ceph_pagelist *pagelist;
58 static void __wake_requests(struct ceph_mds_client *mdsc,
59 struct list_head *head);
60 static void ceph_cap_release_work(struct work_struct *work);
61 static void ceph_cap_reclaim_work(struct work_struct *work);
63 static const struct ceph_connection_operations mds_con_ops;
70 static int parse_reply_info_quota(void **p, void *end,
71 struct ceph_mds_reply_info_in *info)
73 u8 struct_v, struct_compat;
76 ceph_decode_8_safe(p, end, struct_v, bad);
77 ceph_decode_8_safe(p, end, struct_compat, bad);
78 /* struct_v is expected to be >= 1. we only
79 * understand encoding with struct_compat == 1. */
80 if (!struct_v || struct_compat != 1)
82 ceph_decode_32_safe(p, end, struct_len, bad);
83 ceph_decode_need(p, end, struct_len, bad);
84 end = *p + struct_len;
85 ceph_decode_64_safe(p, end, info->max_bytes, bad);
86 ceph_decode_64_safe(p, end, info->max_files, bad);
94 * parse individual inode info
96 static int parse_reply_info_in(void **p, void *end,
97 struct ceph_mds_reply_info_in *info,
103 if (features == (u64)-1) {
106 ceph_decode_8_safe(p, end, struct_v, bad);
107 ceph_decode_8_safe(p, end, struct_compat, bad);
108 /* struct_v is expected to be >= 1. we only understand
109 * encoding with struct_compat == 1. */
110 if (!struct_v || struct_compat != 1)
112 ceph_decode_32_safe(p, end, struct_len, bad);
113 ceph_decode_need(p, end, struct_len, bad);
114 end = *p + struct_len;
117 ceph_decode_need(p, end, sizeof(struct ceph_mds_reply_inode), bad);
119 *p += sizeof(struct ceph_mds_reply_inode) +
120 sizeof(*info->in->fragtree.splits) *
121 le32_to_cpu(info->in->fragtree.nsplits);
123 ceph_decode_32_safe(p, end, info->symlink_len, bad);
124 ceph_decode_need(p, end, info->symlink_len, bad);
126 *p += info->symlink_len;
128 ceph_decode_copy_safe(p, end, &info->dir_layout,
129 sizeof(info->dir_layout), bad);
130 ceph_decode_32_safe(p, end, info->xattr_len, bad);
131 ceph_decode_need(p, end, info->xattr_len, bad);
132 info->xattr_data = *p;
133 *p += info->xattr_len;
135 if (features == (u64)-1) {
137 ceph_decode_64_safe(p, end, info->inline_version, bad);
138 ceph_decode_32_safe(p, end, info->inline_len, bad);
139 ceph_decode_need(p, end, info->inline_len, bad);
140 info->inline_data = *p;
141 *p += info->inline_len;
143 err = parse_reply_info_quota(p, end, info);
147 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
148 if (info->pool_ns_len > 0) {
149 ceph_decode_need(p, end, info->pool_ns_len, bad);
150 info->pool_ns_data = *p;
151 *p += info->pool_ns_len;
155 ceph_decode_need(p, end, sizeof(info->btime), bad);
156 ceph_decode_copy(p, &info->btime, sizeof(info->btime));
158 /* change attribute */
159 ceph_decode_64_safe(p, end, info->change_attr, bad);
163 ceph_decode_32_safe(p, end, info->dir_pin, bad);
165 info->dir_pin = -ENODATA;
168 /* snapshot birth time, remains zero for v<=2 */
170 ceph_decode_need(p, end, sizeof(info->snap_btime), bad);
171 ceph_decode_copy(p, &info->snap_btime,
172 sizeof(info->snap_btime));
174 memset(&info->snap_btime, 0, sizeof(info->snap_btime));
179 if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
180 ceph_decode_64_safe(p, end, info->inline_version, bad);
181 ceph_decode_32_safe(p, end, info->inline_len, bad);
182 ceph_decode_need(p, end, info->inline_len, bad);
183 info->inline_data = *p;
184 *p += info->inline_len;
186 info->inline_version = CEPH_INLINE_NONE;
188 if (features & CEPH_FEATURE_MDS_QUOTA) {
189 err = parse_reply_info_quota(p, end, info);
197 info->pool_ns_len = 0;
198 info->pool_ns_data = NULL;
199 if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) {
200 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
201 if (info->pool_ns_len > 0) {
202 ceph_decode_need(p, end, info->pool_ns_len, bad);
203 info->pool_ns_data = *p;
204 *p += info->pool_ns_len;
208 if (features & CEPH_FEATURE_FS_BTIME) {
209 ceph_decode_need(p, end, sizeof(info->btime), bad);
210 ceph_decode_copy(p, &info->btime, sizeof(info->btime));
211 ceph_decode_64_safe(p, end, info->change_attr, bad);
214 info->dir_pin = -ENODATA;
215 /* info->snap_btime remains zero */
224 static int parse_reply_info_dir(void **p, void *end,
225 struct ceph_mds_reply_dirfrag **dirfrag,
228 if (features == (u64)-1) {
229 u8 struct_v, struct_compat;
231 ceph_decode_8_safe(p, end, struct_v, bad);
232 ceph_decode_8_safe(p, end, struct_compat, bad);
233 /* struct_v is expected to be >= 1. we only understand
234 * encoding whose struct_compat == 1. */
235 if (!struct_v || struct_compat != 1)
237 ceph_decode_32_safe(p, end, struct_len, bad);
238 ceph_decode_need(p, end, struct_len, bad);
239 end = *p + struct_len;
242 ceph_decode_need(p, end, sizeof(**dirfrag), bad);
244 *p += sizeof(**dirfrag) + sizeof(u32) * le32_to_cpu((*dirfrag)->ndist);
245 if (unlikely(*p > end))
247 if (features == (u64)-1)
254 static int parse_reply_info_lease(void **p, void *end,
255 struct ceph_mds_reply_lease **lease,
258 if (features == (u64)-1) {
259 u8 struct_v, struct_compat;
261 ceph_decode_8_safe(p, end, struct_v, bad);
262 ceph_decode_8_safe(p, end, struct_compat, bad);
263 /* struct_v is expected to be >= 1. we only understand
264 * encoding whose struct_compat == 1. */
265 if (!struct_v || struct_compat != 1)
267 ceph_decode_32_safe(p, end, struct_len, bad);
268 ceph_decode_need(p, end, struct_len, bad);
269 end = *p + struct_len;
272 ceph_decode_need(p, end, sizeof(**lease), bad);
274 *p += sizeof(**lease);
275 if (features == (u64)-1)
283 * parse a normal reply, which may contain a (dir+)dentry and/or a
286 static int parse_reply_info_trace(void **p, void *end,
287 struct ceph_mds_reply_info_parsed *info,
292 if (info->head->is_dentry) {
293 err = parse_reply_info_in(p, end, &info->diri, features);
297 err = parse_reply_info_dir(p, end, &info->dirfrag, features);
301 ceph_decode_32_safe(p, end, info->dname_len, bad);
302 ceph_decode_need(p, end, info->dname_len, bad);
304 *p += info->dname_len;
306 err = parse_reply_info_lease(p, end, &info->dlease, features);
311 if (info->head->is_target) {
312 err = parse_reply_info_in(p, end, &info->targeti, features);
317 if (unlikely(*p != end))
324 pr_err("problem parsing mds trace %d\n", err);
329 * parse readdir results
331 static int parse_reply_info_readdir(void **p, void *end,
332 struct ceph_mds_reply_info_parsed *info,
338 err = parse_reply_info_dir(p, end, &info->dir_dir, features);
342 ceph_decode_need(p, end, sizeof(num) + 2, bad);
343 num = ceph_decode_32(p);
345 u16 flags = ceph_decode_16(p);
346 info->dir_end = !!(flags & CEPH_READDIR_FRAG_END);
347 info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE);
348 info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER);
349 info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH);
354 BUG_ON(!info->dir_entries);
355 if ((unsigned long)(info->dir_entries + num) >
356 (unsigned long)info->dir_entries + info->dir_buf_size) {
357 pr_err("dir contents are larger than expected\n");
364 struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i;
366 ceph_decode_32_safe(p, end, rde->name_len, bad);
367 ceph_decode_need(p, end, rde->name_len, bad);
370 dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name);
373 err = parse_reply_info_lease(p, end, &rde->lease, features);
377 err = parse_reply_info_in(p, end, &rde->inode, features);
380 /* ceph_readdir_prepopulate() will update it */
387 /* Skip over any unrecognized fields */
394 pr_err("problem parsing dir contents %d\n", err);
399 * parse fcntl F_GETLK results
401 static int parse_reply_info_filelock(void **p, void *end,
402 struct ceph_mds_reply_info_parsed *info,
405 if (*p + sizeof(*info->filelock_reply) > end)
408 info->filelock_reply = *p;
410 /* Skip over any unrecognized fields */
418 * parse create results
420 static int parse_reply_info_create(void **p, void *end,
421 struct ceph_mds_reply_info_parsed *info,
424 if (features == (u64)-1 ||
425 (features & CEPH_FEATURE_REPLY_CREATE_INODE)) {
426 /* Malformed reply? */
428 info->has_create_ino = false;
430 info->has_create_ino = true;
431 ceph_decode_64_safe(p, end, info->ino, bad);
438 /* Skip over any unrecognized fields */
446 * parse extra results
448 static int parse_reply_info_extra(void **p, void *end,
449 struct ceph_mds_reply_info_parsed *info,
452 u32 op = le32_to_cpu(info->head->op);
454 if (op == CEPH_MDS_OP_GETFILELOCK)
455 return parse_reply_info_filelock(p, end, info, features);
456 else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
457 return parse_reply_info_readdir(p, end, info, features);
458 else if (op == CEPH_MDS_OP_CREATE)
459 return parse_reply_info_create(p, end, info, features);
465 * parse entire mds reply
467 static int parse_reply_info(struct ceph_msg *msg,
468 struct ceph_mds_reply_info_parsed *info,
475 info->head = msg->front.iov_base;
476 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
477 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
480 ceph_decode_32_safe(&p, end, len, bad);
482 ceph_decode_need(&p, end, len, bad);
483 err = parse_reply_info_trace(&p, p+len, info, features);
489 ceph_decode_32_safe(&p, end, len, bad);
491 ceph_decode_need(&p, end, len, bad);
492 err = parse_reply_info_extra(&p, p+len, info, features);
498 ceph_decode_32_safe(&p, end, len, bad);
499 info->snapblob_len = len;
510 pr_err("mds parse_reply err %d\n", err);
514 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
516 if (!info->dir_entries)
518 free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
525 const char *ceph_session_state_name(int s)
528 case CEPH_MDS_SESSION_NEW: return "new";
529 case CEPH_MDS_SESSION_OPENING: return "opening";
530 case CEPH_MDS_SESSION_OPEN: return "open";
531 case CEPH_MDS_SESSION_HUNG: return "hung";
532 case CEPH_MDS_SESSION_CLOSING: return "closing";
533 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
534 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
535 case CEPH_MDS_SESSION_REJECTED: return "rejected";
536 default: return "???";
540 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
542 if (refcount_inc_not_zero(&s->s_ref)) {
543 dout("mdsc get_session %p %d -> %d\n", s,
544 refcount_read(&s->s_ref)-1, refcount_read(&s->s_ref));
547 dout("mdsc get_session %p 0 -- FAIL\n", s);
552 void ceph_put_mds_session(struct ceph_mds_session *s)
554 dout("mdsc put_session %p %d -> %d\n", s,
555 refcount_read(&s->s_ref), refcount_read(&s->s_ref)-1);
556 if (refcount_dec_and_test(&s->s_ref)) {
557 if (s->s_auth.authorizer)
558 ceph_auth_destroy_authorizer(s->s_auth.authorizer);
564 * called under mdsc->mutex
566 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
569 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
571 return get_session(mdsc->sessions[mds]);
574 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
576 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
582 static int __verify_registered_session(struct ceph_mds_client *mdsc,
583 struct ceph_mds_session *s)
585 if (s->s_mds >= mdsc->max_sessions ||
586 mdsc->sessions[s->s_mds] != s)
592 * create+register a new session for given mds.
593 * called under mdsc->mutex.
595 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
598 struct ceph_mds_session *s;
600 if (mds >= mdsc->mdsmap->m_num_mds)
601 return ERR_PTR(-EINVAL);
603 s = kzalloc(sizeof(*s), GFP_NOFS);
605 return ERR_PTR(-ENOMEM);
607 if (mds >= mdsc->max_sessions) {
608 int newmax = 1 << get_count_order(mds + 1);
609 struct ceph_mds_session **sa;
611 dout("%s: realloc to %d\n", __func__, newmax);
612 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
615 if (mdsc->sessions) {
616 memcpy(sa, mdsc->sessions,
617 mdsc->max_sessions * sizeof(void *));
618 kfree(mdsc->sessions);
621 mdsc->max_sessions = newmax;
624 dout("%s: mds%d\n", __func__, mds);
627 s->s_state = CEPH_MDS_SESSION_NEW;
630 mutex_init(&s->s_mutex);
632 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
634 spin_lock_init(&s->s_gen_ttl_lock);
636 s->s_cap_ttl = jiffies - 1;
638 spin_lock_init(&s->s_cap_lock);
639 s->s_renew_requested = 0;
641 INIT_LIST_HEAD(&s->s_caps);
643 refcount_set(&s->s_ref, 1);
644 INIT_LIST_HEAD(&s->s_waiting);
645 INIT_LIST_HEAD(&s->s_unsafe);
646 s->s_num_cap_releases = 0;
647 s->s_cap_reconnect = 0;
648 s->s_cap_iterator = NULL;
649 INIT_LIST_HEAD(&s->s_cap_releases);
650 INIT_WORK(&s->s_cap_release_work, ceph_cap_release_work);
652 INIT_LIST_HEAD(&s->s_cap_flushing);
654 mdsc->sessions[mds] = s;
655 atomic_inc(&mdsc->num_sessions);
656 refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
658 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
659 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
665 return ERR_PTR(-ENOMEM);
669 * called under mdsc->mutex
671 static void __unregister_session(struct ceph_mds_client *mdsc,
672 struct ceph_mds_session *s)
674 dout("__unregister_session mds%d %p\n", s->s_mds, s);
675 BUG_ON(mdsc->sessions[s->s_mds] != s);
676 mdsc->sessions[s->s_mds] = NULL;
678 ceph_con_close(&s->s_con);
679 ceph_put_mds_session(s);
680 atomic_dec(&mdsc->num_sessions);
684 * drop session refs in request.
686 * should be last request ref, or hold mdsc->mutex
688 static void put_request_session(struct ceph_mds_request *req)
690 if (req->r_session) {
691 ceph_put_mds_session(req->r_session);
692 req->r_session = NULL;
696 void ceph_mdsc_release_request(struct kref *kref)
698 struct ceph_mds_request *req = container_of(kref,
699 struct ceph_mds_request,
701 destroy_reply_info(&req->r_reply_info);
703 ceph_msg_put(req->r_request);
705 ceph_msg_put(req->r_reply);
707 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
708 /* avoid calling iput_final() in mds dispatch threads */
709 ceph_async_iput(req->r_inode);
712 ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
713 ceph_async_iput(req->r_parent);
715 ceph_async_iput(req->r_target_inode);
718 if (req->r_old_dentry)
719 dput(req->r_old_dentry);
720 if (req->r_old_dentry_dir) {
722 * track (and drop pins for) r_old_dentry_dir
723 * separately, since r_old_dentry's d_parent may have
724 * changed between the dir mutex being dropped and
725 * this request being freed.
727 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
729 ceph_async_iput(req->r_old_dentry_dir);
734 ceph_pagelist_release(req->r_pagelist);
735 put_request_session(req);
736 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
737 WARN_ON_ONCE(!list_empty(&req->r_wait));
741 DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
744 * lookup session, bump ref if found.
746 * called under mdsc->mutex.
748 static struct ceph_mds_request *
749 lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
751 struct ceph_mds_request *req;
753 req = lookup_request(&mdsc->request_tree, tid);
755 ceph_mdsc_get_request(req);
761 * Register an in-flight request, and assign a tid. Link to directory
762 * are modifying (if any).
764 * Called under mdsc->mutex.
766 static void __register_request(struct ceph_mds_client *mdsc,
767 struct ceph_mds_request *req,
772 req->r_tid = ++mdsc->last_tid;
773 if (req->r_num_caps) {
774 ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
777 pr_err("__register_request %p "
778 "failed to reserve caps: %d\n", req, ret);
779 /* set req->r_err to fail early from __do_request */
784 dout("__register_request %p tid %lld\n", req, req->r_tid);
785 ceph_mdsc_get_request(req);
786 insert_request(&mdsc->request_tree, req);
788 req->r_uid = current_fsuid();
789 req->r_gid = current_fsgid();
791 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
792 mdsc->oldest_tid = req->r_tid;
796 req->r_unsafe_dir = dir;
800 static void __unregister_request(struct ceph_mds_client *mdsc,
801 struct ceph_mds_request *req)
803 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
805 /* Never leave an unregistered request on an unsafe list! */
806 list_del_init(&req->r_unsafe_item);
808 if (req->r_tid == mdsc->oldest_tid) {
809 struct rb_node *p = rb_next(&req->r_node);
810 mdsc->oldest_tid = 0;
812 struct ceph_mds_request *next_req =
813 rb_entry(p, struct ceph_mds_request, r_node);
814 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
815 mdsc->oldest_tid = next_req->r_tid;
822 erase_request(&mdsc->request_tree, req);
824 if (req->r_unsafe_dir &&
825 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
826 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
827 spin_lock(&ci->i_unsafe_lock);
828 list_del_init(&req->r_unsafe_dir_item);
829 spin_unlock(&ci->i_unsafe_lock);
831 if (req->r_target_inode &&
832 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
833 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
834 spin_lock(&ci->i_unsafe_lock);
835 list_del_init(&req->r_unsafe_target_item);
836 spin_unlock(&ci->i_unsafe_lock);
839 if (req->r_unsafe_dir) {
840 /* avoid calling iput_final() in mds dispatch threads */
841 ceph_async_iput(req->r_unsafe_dir);
842 req->r_unsafe_dir = NULL;
845 complete_all(&req->r_safe_completion);
847 ceph_mdsc_put_request(req);
851 * Walk back up the dentry tree until we hit a dentry representing a
852 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
853 * when calling this) to ensure that the objects won't disappear while we're
854 * working with them. Once we hit a candidate dentry, we attempt to take a
855 * reference to it, and return that as the result.
857 static struct inode *get_nonsnap_parent(struct dentry *dentry)
859 struct inode *inode = NULL;
861 while (dentry && !IS_ROOT(dentry)) {
862 inode = d_inode_rcu(dentry);
863 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
865 dentry = dentry->d_parent;
868 inode = igrab(inode);
873 * Choose mds to send request to next. If there is a hint set in the
874 * request (e.g., due to a prior forward hint from the mds), use that.
875 * Otherwise, consult frag tree and/or caps to identify the
876 * appropriate mds. If all else fails, choose randomly.
878 * Called under mdsc->mutex.
880 static int __choose_mds(struct ceph_mds_client *mdsc,
881 struct ceph_mds_request *req)
884 struct ceph_inode_info *ci;
885 struct ceph_cap *cap;
886 int mode = req->r_direct_mode;
888 u32 hash = req->r_direct_hash;
889 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
892 * is there a specific mds we should try? ignore hint if we have
893 * no session and the mds is not up (active or recovering).
895 if (req->r_resend_mds >= 0 &&
896 (__have_session(mdsc, req->r_resend_mds) ||
897 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
898 dout("choose_mds using resend_mds mds%d\n",
900 return req->r_resend_mds;
903 if (mode == USE_RANDOM_MDS)
908 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
909 inode = req->r_inode;
912 /* req->r_dentry is non-null for LSSNAP request */
914 inode = get_nonsnap_parent(req->r_dentry);
916 dout("__choose_mds using snapdir's parent %p\n", inode);
918 } else if (req->r_dentry) {
919 /* ignore race with rename; old or new d_parent is okay */
920 struct dentry *parent;
924 parent = READ_ONCE(req->r_dentry->d_parent);
925 dir = req->r_parent ? : d_inode_rcu(parent);
927 if (!dir || dir->i_sb != mdsc->fsc->sb) {
928 /* not this fs or parent went negative */
929 inode = d_inode(req->r_dentry);
932 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
933 /* direct snapped/virtual snapdir requests
934 * based on parent dir inode */
935 inode = get_nonsnap_parent(parent);
936 dout("__choose_mds using nonsnap parent %p\n", inode);
939 inode = d_inode(req->r_dentry);
940 if (!inode || mode == USE_AUTH_MDS) {
943 hash = ceph_dentry_hash(dir, req->r_dentry);
952 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
956 ci = ceph_inode(inode);
958 if (is_hash && S_ISDIR(inode->i_mode)) {
959 struct ceph_inode_frag frag;
962 ceph_choose_frag(ci, hash, &frag, &found);
964 if (mode == USE_ANY_MDS && frag.ndist > 0) {
967 /* choose a random replica */
968 get_random_bytes(&r, 1);
971 dout("choose_mds %p %llx.%llx "
972 "frag %u mds%d (%d/%d)\n",
973 inode, ceph_vinop(inode),
976 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
977 CEPH_MDS_STATE_ACTIVE &&
978 !ceph_mdsmap_is_laggy(mdsc->mdsmap, mds))
982 /* since this file/dir wasn't known to be
983 * replicated, then we want to look for the
984 * authoritative mds. */
986 /* choose auth mds */
988 dout("choose_mds %p %llx.%llx "
989 "frag %u mds%d (auth)\n",
990 inode, ceph_vinop(inode), frag.frag, mds);
991 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
992 CEPH_MDS_STATE_ACTIVE) {
993 if (mode == USE_ANY_MDS &&
994 !ceph_mdsmap_is_laggy(mdsc->mdsmap,
1003 spin_lock(&ci->i_ceph_lock);
1005 if (mode == USE_AUTH_MDS)
1006 cap = ci->i_auth_cap;
1007 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
1008 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
1010 spin_unlock(&ci->i_ceph_lock);
1011 ceph_async_iput(inode);
1014 mds = cap->session->s_mds;
1015 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
1016 inode, ceph_vinop(inode), mds,
1017 cap == ci->i_auth_cap ? "auth " : "", cap);
1018 spin_unlock(&ci->i_ceph_lock);
1020 /* avoid calling iput_final() while holding mdsc->mutex or
1021 * in mds dispatch threads */
1022 ceph_async_iput(inode);
1026 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
1027 dout("choose_mds chose random mds%d\n", mds);
1035 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
1037 struct ceph_msg *msg;
1038 struct ceph_mds_session_head *h;
1040 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
1043 pr_err("create_session_msg ENOMEM creating msg\n");
1046 h = msg->front.iov_base;
1047 h->op = cpu_to_le32(op);
1048 h->seq = cpu_to_le64(seq);
1053 static void encode_supported_features(void **p, void *end)
1055 static const unsigned char bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
1056 static const size_t count = ARRAY_SIZE(bits);
1060 size_t size = ((size_t)bits[count - 1] + 64) / 64 * 8;
1062 BUG_ON(*p + 4 + size > end);
1063 ceph_encode_32(p, size);
1064 memset(*p, 0, size);
1065 for (i = 0; i < count; i++)
1066 ((unsigned char*)(*p))[i / 8] |= 1 << (bits[i] % 8);
1069 BUG_ON(*p + 4 > end);
1070 ceph_encode_32(p, 0);
1075 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
1076 * to include additional client metadata fields.
1078 static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
1080 struct ceph_msg *msg;
1081 struct ceph_mds_session_head *h;
1083 int extra_bytes = 0;
1084 int metadata_key_count = 0;
1085 struct ceph_options *opt = mdsc->fsc->client->options;
1086 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
1089 const char* metadata[][2] = {
1090 {"hostname", mdsc->nodename},
1091 {"kernel_version", init_utsname()->release},
1092 {"entity_id", opt->name ? : ""},
1093 {"root", fsopt->server_path ? : "/"},
1097 /* Calculate serialized length of metadata */
1098 extra_bytes = 4; /* map length */
1099 for (i = 0; metadata[i][0]; ++i) {
1100 extra_bytes += 8 + strlen(metadata[i][0]) +
1101 strlen(metadata[i][1]);
1102 metadata_key_count++;
1104 /* supported feature */
1105 extra_bytes += 4 + 8;
1107 /* Allocate the message */
1108 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
1111 pr_err("create_session_msg ENOMEM creating msg\n");
1114 p = msg->front.iov_base;
1115 end = p + msg->front.iov_len;
1118 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
1119 h->seq = cpu_to_le64(seq);
1122 * Serialize client metadata into waiting buffer space, using
1123 * the format that userspace expects for map<string, string>
1125 * ClientSession messages with metadata are v2
1127 msg->hdr.version = cpu_to_le16(3);
1128 msg->hdr.compat_version = cpu_to_le16(1);
1130 /* The write pointer, following the session_head structure */
1133 /* Number of entries in the map */
1134 ceph_encode_32(&p, metadata_key_count);
1136 /* Two length-prefixed strings for each entry in the map */
1137 for (i = 0; metadata[i][0]; ++i) {
1138 size_t const key_len = strlen(metadata[i][0]);
1139 size_t const val_len = strlen(metadata[i][1]);
1141 ceph_encode_32(&p, key_len);
1142 memcpy(p, metadata[i][0], key_len);
1144 ceph_encode_32(&p, val_len);
1145 memcpy(p, metadata[i][1], val_len);
1149 encode_supported_features(&p, end);
1150 msg->front.iov_len = p - msg->front.iov_base;
1151 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1157 * send session open request.
1159 * called under mdsc->mutex
1161 static int __open_session(struct ceph_mds_client *mdsc,
1162 struct ceph_mds_session *session)
1164 struct ceph_msg *msg;
1166 int mds = session->s_mds;
1168 /* wait for mds to go active? */
1169 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
1170 dout("open_session to mds%d (%s)\n", mds,
1171 ceph_mds_state_name(mstate));
1172 session->s_state = CEPH_MDS_SESSION_OPENING;
1173 session->s_renew_requested = jiffies;
1175 /* send connect message */
1176 msg = create_session_open_msg(mdsc, session->s_seq);
1179 ceph_con_send(&session->s_con, msg);
1184 * open sessions for any export targets for the given mds
1186 * called under mdsc->mutex
1188 static struct ceph_mds_session *
1189 __open_export_target_session(struct ceph_mds_client *mdsc, int target)
1191 struct ceph_mds_session *session;
1193 session = __ceph_lookup_mds_session(mdsc, target);
1195 session = register_session(mdsc, target);
1196 if (IS_ERR(session))
1199 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1200 session->s_state == CEPH_MDS_SESSION_CLOSING)
1201 __open_session(mdsc, session);
1206 struct ceph_mds_session *
1207 ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1209 struct ceph_mds_session *session;
1211 dout("open_export_target_session to mds%d\n", target);
1213 mutex_lock(&mdsc->mutex);
1214 session = __open_export_target_session(mdsc, target);
1215 mutex_unlock(&mdsc->mutex);
1220 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1221 struct ceph_mds_session *session)
1223 struct ceph_mds_info *mi;
1224 struct ceph_mds_session *ts;
1225 int i, mds = session->s_mds;
1227 if (mds >= mdsc->mdsmap->m_num_mds)
1230 mi = &mdsc->mdsmap->m_info[mds];
1231 dout("open_export_target_sessions for mds%d (%d targets)\n",
1232 session->s_mds, mi->num_export_targets);
1234 for (i = 0; i < mi->num_export_targets; i++) {
1235 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1237 ceph_put_mds_session(ts);
1241 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1242 struct ceph_mds_session *session)
1244 mutex_lock(&mdsc->mutex);
1245 __open_export_target_sessions(mdsc, session);
1246 mutex_unlock(&mdsc->mutex);
1253 static void detach_cap_releases(struct ceph_mds_session *session,
1254 struct list_head *target)
1256 lockdep_assert_held(&session->s_cap_lock);
1258 list_splice_init(&session->s_cap_releases, target);
1259 session->s_num_cap_releases = 0;
1260 dout("dispose_cap_releases mds%d\n", session->s_mds);
1263 static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1264 struct list_head *dispose)
1266 while (!list_empty(dispose)) {
1267 struct ceph_cap *cap;
1268 /* zero out the in-progress message */
1269 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
1270 list_del(&cap->session_caps);
1271 ceph_put_cap(mdsc, cap);
1275 static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1276 struct ceph_mds_session *session)
1278 struct ceph_mds_request *req;
1280 struct ceph_inode_info *ci;
1282 dout("cleanup_session_requests mds%d\n", session->s_mds);
1283 mutex_lock(&mdsc->mutex);
1284 while (!list_empty(&session->s_unsafe)) {
1285 req = list_first_entry(&session->s_unsafe,
1286 struct ceph_mds_request, r_unsafe_item);
1287 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1289 if (req->r_target_inode) {
1290 /* dropping unsafe change of inode's attributes */
1291 ci = ceph_inode(req->r_target_inode);
1292 errseq_set(&ci->i_meta_err, -EIO);
1294 if (req->r_unsafe_dir) {
1295 /* dropping unsafe directory operation */
1296 ci = ceph_inode(req->r_unsafe_dir);
1297 errseq_set(&ci->i_meta_err, -EIO);
1299 __unregister_request(mdsc, req);
1301 /* zero r_attempts, so kick_requests() will re-send requests */
1302 p = rb_first(&mdsc->request_tree);
1304 req = rb_entry(p, struct ceph_mds_request, r_node);
1306 if (req->r_session &&
1307 req->r_session->s_mds == session->s_mds)
1308 req->r_attempts = 0;
1310 mutex_unlock(&mdsc->mutex);
1314 * Helper to safely iterate over all caps associated with a session, with
1315 * special care taken to handle a racing __ceph_remove_cap().
1317 * Caller must hold session s_mutex.
1319 int ceph_iterate_session_caps(struct ceph_mds_session *session,
1320 int (*cb)(struct inode *, struct ceph_cap *,
1323 struct list_head *p;
1324 struct ceph_cap *cap;
1325 struct inode *inode, *last_inode = NULL;
1326 struct ceph_cap *old_cap = NULL;
1329 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1330 spin_lock(&session->s_cap_lock);
1331 p = session->s_caps.next;
1332 while (p != &session->s_caps) {
1333 cap = list_entry(p, struct ceph_cap, session_caps);
1334 inode = igrab(&cap->ci->vfs_inode);
1339 session->s_cap_iterator = cap;
1340 spin_unlock(&session->s_cap_lock);
1343 /* avoid calling iput_final() while holding
1344 * s_mutex or in mds dispatch threads */
1345 ceph_async_iput(last_inode);
1349 ceph_put_cap(session->s_mdsc, old_cap);
1353 ret = cb(inode, cap, arg);
1356 spin_lock(&session->s_cap_lock);
1359 dout("iterate_session_caps finishing cap %p removal\n",
1361 BUG_ON(cap->session != session);
1362 cap->session = NULL;
1363 list_del_init(&cap->session_caps);
1364 session->s_nr_caps--;
1365 if (cap->queue_release)
1366 __ceph_queue_cap_release(session, cap);
1368 old_cap = cap; /* put_cap it w/o locks held */
1375 session->s_cap_iterator = NULL;
1376 spin_unlock(&session->s_cap_lock);
1378 ceph_async_iput(last_inode);
1380 ceph_put_cap(session->s_mdsc, old_cap);
1385 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
1388 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
1389 struct ceph_inode_info *ci = ceph_inode(inode);
1390 LIST_HEAD(to_remove);
1391 bool dirty_dropped = false;
1392 bool invalidate = false;
1394 dout("removing cap %p, ci is %p, inode is %p\n",
1395 cap, ci, &ci->vfs_inode);
1396 spin_lock(&ci->i_ceph_lock);
1397 if (cap->mds_wanted | cap->issued)
1398 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1399 __ceph_remove_cap(cap, false);
1400 if (!ci->i_auth_cap) {
1401 struct ceph_cap_flush *cf;
1402 struct ceph_mds_client *mdsc = fsc->mdsc;
1404 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1405 if (inode->i_data.nrpages > 0)
1407 if (ci->i_wrbuffer_ref > 0)
1408 mapping_set_error(&inode->i_data, -EIO);
1411 while (!list_empty(&ci->i_cap_flush_list)) {
1412 cf = list_first_entry(&ci->i_cap_flush_list,
1413 struct ceph_cap_flush, i_list);
1414 list_move(&cf->i_list, &to_remove);
1417 spin_lock(&mdsc->cap_dirty_lock);
1419 list_for_each_entry(cf, &to_remove, i_list)
1420 list_del(&cf->g_list);
1422 if (!list_empty(&ci->i_dirty_item)) {
1423 pr_warn_ratelimited(
1424 " dropping dirty %s state for %p %lld\n",
1425 ceph_cap_string(ci->i_dirty_caps),
1426 inode, ceph_ino(inode));
1427 ci->i_dirty_caps = 0;
1428 list_del_init(&ci->i_dirty_item);
1429 dirty_dropped = true;
1431 if (!list_empty(&ci->i_flushing_item)) {
1432 pr_warn_ratelimited(
1433 " dropping dirty+flushing %s state for %p %lld\n",
1434 ceph_cap_string(ci->i_flushing_caps),
1435 inode, ceph_ino(inode));
1436 ci->i_flushing_caps = 0;
1437 list_del_init(&ci->i_flushing_item);
1438 mdsc->num_cap_flushing--;
1439 dirty_dropped = true;
1441 spin_unlock(&mdsc->cap_dirty_lock);
1443 if (dirty_dropped) {
1444 errseq_set(&ci->i_meta_err, -EIO);
1446 if (ci->i_wrbuffer_ref_head == 0 &&
1447 ci->i_wr_ref == 0 &&
1448 ci->i_dirty_caps == 0 &&
1449 ci->i_flushing_caps == 0) {
1450 ceph_put_snap_context(ci->i_head_snapc);
1451 ci->i_head_snapc = NULL;
1455 if (atomic_read(&ci->i_filelock_ref) > 0) {
1456 /* make further file lock syscall return -EIO */
1457 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1458 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1459 inode, ceph_ino(inode));
1462 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
1463 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
1464 ci->i_prealloc_cap_flush = NULL;
1467 spin_unlock(&ci->i_ceph_lock);
1468 while (!list_empty(&to_remove)) {
1469 struct ceph_cap_flush *cf;
1470 cf = list_first_entry(&to_remove,
1471 struct ceph_cap_flush, i_list);
1472 list_del(&cf->i_list);
1473 ceph_free_cap_flush(cf);
1476 wake_up_all(&ci->i_cap_wq);
1478 ceph_queue_invalidate(inode);
1485 * caller must hold session s_mutex
1487 static void remove_session_caps(struct ceph_mds_session *session)
1489 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1490 struct super_block *sb = fsc->sb;
1493 dout("remove_session_caps on %p\n", session);
1494 ceph_iterate_session_caps(session, remove_session_caps_cb, fsc);
1496 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1498 spin_lock(&session->s_cap_lock);
1499 if (session->s_nr_caps > 0) {
1500 struct inode *inode;
1501 struct ceph_cap *cap, *prev = NULL;
1502 struct ceph_vino vino;
1504 * iterate_session_caps() skips inodes that are being
1505 * deleted, we need to wait until deletions are complete.
1506 * __wait_on_freeing_inode() is designed for the job,
1507 * but it is not exported, so use lookup inode function
1510 while (!list_empty(&session->s_caps)) {
1511 cap = list_entry(session->s_caps.next,
1512 struct ceph_cap, session_caps);
1516 vino = cap->ci->i_vino;
1517 spin_unlock(&session->s_cap_lock);
1519 inode = ceph_find_inode(sb, vino);
1520 /* avoid calling iput_final() while holding s_mutex */
1521 ceph_async_iput(inode);
1523 spin_lock(&session->s_cap_lock);
1527 // drop cap expires and unlock s_cap_lock
1528 detach_cap_releases(session, &dispose);
1530 BUG_ON(session->s_nr_caps > 0);
1531 BUG_ON(!list_empty(&session->s_cap_flushing));
1532 spin_unlock(&session->s_cap_lock);
1533 dispose_cap_releases(session->s_mdsc, &dispose);
1543 * wake up any threads waiting on this session's caps. if the cap is
1544 * old (didn't get renewed on the client reconnect), remove it now.
1546 * caller must hold s_mutex.
1548 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1551 struct ceph_inode_info *ci = ceph_inode(inode);
1552 unsigned long ev = (unsigned long)arg;
1554 if (ev == RECONNECT) {
1555 spin_lock(&ci->i_ceph_lock);
1556 ci->i_wanted_max_size = 0;
1557 ci->i_requested_max_size = 0;
1558 spin_unlock(&ci->i_ceph_lock);
1559 } else if (ev == RENEWCAPS) {
1560 if (cap->cap_gen < cap->session->s_cap_gen) {
1561 /* mds did not re-issue stale cap */
1562 spin_lock(&ci->i_ceph_lock);
1563 cap->issued = cap->implemented = CEPH_CAP_PIN;
1564 /* make sure mds knows what we want */
1565 if (__ceph_caps_file_wanted(ci) & ~cap->mds_wanted)
1566 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1567 spin_unlock(&ci->i_ceph_lock);
1569 } else if (ev == FORCE_RO) {
1571 wake_up_all(&ci->i_cap_wq);
1575 static void wake_up_session_caps(struct ceph_mds_session *session, int ev)
1577 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1578 ceph_iterate_session_caps(session, wake_up_session_cb,
1579 (void *)(unsigned long)ev);
1583 * Send periodic message to MDS renewing all currently held caps. The
1584 * ack will reset the expiration for all caps from this session.
1586 * caller holds s_mutex
1588 static int send_renew_caps(struct ceph_mds_client *mdsc,
1589 struct ceph_mds_session *session)
1591 struct ceph_msg *msg;
1594 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1595 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1596 pr_info("mds%d caps stale\n", session->s_mds);
1597 session->s_renew_requested = jiffies;
1599 /* do not try to renew caps until a recovering mds has reconnected
1600 * with its clients. */
1601 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1602 if (state < CEPH_MDS_STATE_RECONNECT) {
1603 dout("send_renew_caps ignoring mds%d (%s)\n",
1604 session->s_mds, ceph_mds_state_name(state));
1608 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1609 ceph_mds_state_name(state));
1610 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1611 ++session->s_renew_seq);
1614 ceph_con_send(&session->s_con, msg);
1618 static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1619 struct ceph_mds_session *session, u64 seq)
1621 struct ceph_msg *msg;
1623 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1624 session->s_mds, ceph_session_state_name(session->s_state), seq);
1625 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1628 ceph_con_send(&session->s_con, msg);
1634 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1636 * Called under session->s_mutex
1638 static void renewed_caps(struct ceph_mds_client *mdsc,
1639 struct ceph_mds_session *session, int is_renew)
1644 spin_lock(&session->s_cap_lock);
1645 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
1647 session->s_cap_ttl = session->s_renew_requested +
1648 mdsc->mdsmap->m_session_timeout*HZ;
1651 if (time_before(jiffies, session->s_cap_ttl)) {
1652 pr_info("mds%d caps renewed\n", session->s_mds);
1655 pr_info("mds%d caps still stale\n", session->s_mds);
1658 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1659 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1660 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1661 spin_unlock(&session->s_cap_lock);
1664 wake_up_session_caps(session, RENEWCAPS);
1668 * send a session close request
1670 static int request_close_session(struct ceph_mds_client *mdsc,
1671 struct ceph_mds_session *session)
1673 struct ceph_msg *msg;
1675 dout("request_close_session mds%d state %s seq %lld\n",
1676 session->s_mds, ceph_session_state_name(session->s_state),
1678 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1681 ceph_con_send(&session->s_con, msg);
1686 * Called with s_mutex held.
1688 static int __close_session(struct ceph_mds_client *mdsc,
1689 struct ceph_mds_session *session)
1691 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1693 session->s_state = CEPH_MDS_SESSION_CLOSING;
1694 return request_close_session(mdsc, session);
1697 static bool drop_negative_children(struct dentry *dentry)
1699 struct dentry *child;
1700 bool all_negative = true;
1702 if (!d_is_dir(dentry))
1705 spin_lock(&dentry->d_lock);
1706 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1707 if (d_really_is_positive(child)) {
1708 all_negative = false;
1712 spin_unlock(&dentry->d_lock);
1715 shrink_dcache_parent(dentry);
1717 return all_negative;
1721 * Trim old(er) caps.
1723 * Because we can't cache an inode without one or more caps, we do
1724 * this indirectly: if a cap is unused, we prune its aliases, at which
1725 * point the inode will hopefully get dropped to.
1727 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1728 * memory pressure from the MDS, though, so it needn't be perfect.
1730 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1732 int *remaining = arg;
1733 struct ceph_inode_info *ci = ceph_inode(inode);
1734 int used, wanted, oissued, mine;
1736 if (*remaining <= 0)
1739 spin_lock(&ci->i_ceph_lock);
1740 mine = cap->issued | cap->implemented;
1741 used = __ceph_caps_used(ci);
1742 wanted = __ceph_caps_file_wanted(ci);
1743 oissued = __ceph_caps_issued_other(ci, cap);
1745 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1746 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1747 ceph_cap_string(used), ceph_cap_string(wanted));
1748 if (cap == ci->i_auth_cap) {
1749 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1750 !list_empty(&ci->i_cap_snaps))
1752 if ((used | wanted) & CEPH_CAP_ANY_WR)
1754 /* Note: it's possible that i_filelock_ref becomes non-zero
1755 * after dropping auth caps. It doesn't hurt because reply
1756 * of lock mds request will re-add auth caps. */
1757 if (atomic_read(&ci->i_filelock_ref) > 0)
1760 /* The inode has cached pages, but it's no longer used.
1761 * we can safely drop it */
1762 if (wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
1763 !(oissued & CEPH_CAP_FILE_CACHE)) {
1767 if ((used | wanted) & ~oissued & mine)
1768 goto out; /* we need these caps */
1771 /* we aren't the only cap.. just remove us */
1772 __ceph_remove_cap(cap, true);
1775 struct dentry *dentry;
1776 /* try dropping referring dentries */
1777 spin_unlock(&ci->i_ceph_lock);
1778 dentry = d_find_any_alias(inode);
1779 if (dentry && drop_negative_children(dentry)) {
1782 d_prune_aliases(inode);
1783 count = atomic_read(&inode->i_count);
1786 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1795 spin_unlock(&ci->i_ceph_lock);
1800 * Trim session cap count down to some max number.
1802 int ceph_trim_caps(struct ceph_mds_client *mdsc,
1803 struct ceph_mds_session *session,
1806 int trim_caps = session->s_nr_caps - max_caps;
1808 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1809 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1810 if (trim_caps > 0) {
1811 int remaining = trim_caps;
1813 ceph_iterate_session_caps(session, trim_caps_cb, &remaining);
1814 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1815 session->s_mds, session->s_nr_caps, max_caps,
1816 trim_caps - remaining);
1819 ceph_flush_cap_releases(mdsc, session);
1823 static int check_caps_flush(struct ceph_mds_client *mdsc,
1828 spin_lock(&mdsc->cap_dirty_lock);
1829 if (!list_empty(&mdsc->cap_flush_list)) {
1830 struct ceph_cap_flush *cf =
1831 list_first_entry(&mdsc->cap_flush_list,
1832 struct ceph_cap_flush, g_list);
1833 if (cf->tid <= want_flush_tid) {
1834 dout("check_caps_flush still flushing tid "
1835 "%llu <= %llu\n", cf->tid, want_flush_tid);
1839 spin_unlock(&mdsc->cap_dirty_lock);
1844 * flush all dirty inode data to disk.
1846 * returns true if we've flushed through want_flush_tid
1848 static void wait_caps_flush(struct ceph_mds_client *mdsc,
1851 dout("check_caps_flush want %llu\n", want_flush_tid);
1853 wait_event(mdsc->cap_flushing_wq,
1854 check_caps_flush(mdsc, want_flush_tid));
1856 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
1860 * called under s_mutex
1862 static void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1863 struct ceph_mds_session *session)
1865 struct ceph_msg *msg = NULL;
1866 struct ceph_mds_cap_release *head;
1867 struct ceph_mds_cap_item *item;
1868 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
1869 struct ceph_cap *cap;
1870 LIST_HEAD(tmp_list);
1871 int num_cap_releases;
1872 __le32 barrier, *cap_barrier;
1874 down_read(&osdc->lock);
1875 barrier = cpu_to_le32(osdc->epoch_barrier);
1876 up_read(&osdc->lock);
1878 spin_lock(&session->s_cap_lock);
1880 list_splice_init(&session->s_cap_releases, &tmp_list);
1881 num_cap_releases = session->s_num_cap_releases;
1882 session->s_num_cap_releases = 0;
1883 spin_unlock(&session->s_cap_lock);
1885 while (!list_empty(&tmp_list)) {
1887 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
1888 PAGE_SIZE, GFP_NOFS, false);
1891 head = msg->front.iov_base;
1892 head->num = cpu_to_le32(0);
1893 msg->front.iov_len = sizeof(*head);
1895 msg->hdr.version = cpu_to_le16(2);
1896 msg->hdr.compat_version = cpu_to_le16(1);
1899 cap = list_first_entry(&tmp_list, struct ceph_cap,
1901 list_del(&cap->session_caps);
1904 head = msg->front.iov_base;
1905 put_unaligned_le32(get_unaligned_le32(&head->num) + 1,
1907 item = msg->front.iov_base + msg->front.iov_len;
1908 item->ino = cpu_to_le64(cap->cap_ino);
1909 item->cap_id = cpu_to_le64(cap->cap_id);
1910 item->migrate_seq = cpu_to_le32(cap->mseq);
1911 item->seq = cpu_to_le32(cap->issue_seq);
1912 msg->front.iov_len += sizeof(*item);
1914 ceph_put_cap(mdsc, cap);
1916 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
1917 // Append cap_barrier field
1918 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1919 *cap_barrier = barrier;
1920 msg->front.iov_len += sizeof(*cap_barrier);
1922 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1923 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1924 ceph_con_send(&session->s_con, msg);
1929 BUG_ON(num_cap_releases != 0);
1931 spin_lock(&session->s_cap_lock);
1932 if (!list_empty(&session->s_cap_releases))
1934 spin_unlock(&session->s_cap_lock);
1937 // Append cap_barrier field
1938 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1939 *cap_barrier = barrier;
1940 msg->front.iov_len += sizeof(*cap_barrier);
1942 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1943 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1944 ceph_con_send(&session->s_con, msg);
1948 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1950 spin_lock(&session->s_cap_lock);
1951 list_splice(&tmp_list, &session->s_cap_releases);
1952 session->s_num_cap_releases += num_cap_releases;
1953 spin_unlock(&session->s_cap_lock);
1956 static void ceph_cap_release_work(struct work_struct *work)
1958 struct ceph_mds_session *session =
1959 container_of(work, struct ceph_mds_session, s_cap_release_work);
1961 mutex_lock(&session->s_mutex);
1962 if (session->s_state == CEPH_MDS_SESSION_OPEN ||
1963 session->s_state == CEPH_MDS_SESSION_HUNG)
1964 ceph_send_cap_releases(session->s_mdsc, session);
1965 mutex_unlock(&session->s_mutex);
1966 ceph_put_mds_session(session);
1969 void ceph_flush_cap_releases(struct ceph_mds_client *mdsc,
1970 struct ceph_mds_session *session)
1975 get_session(session);
1976 if (queue_work(mdsc->fsc->cap_wq,
1977 &session->s_cap_release_work)) {
1978 dout("cap release work queued\n");
1980 ceph_put_mds_session(session);
1981 dout("failed to queue cap release work\n");
1986 * caller holds session->s_cap_lock
1988 void __ceph_queue_cap_release(struct ceph_mds_session *session,
1989 struct ceph_cap *cap)
1991 list_add_tail(&cap->session_caps, &session->s_cap_releases);
1992 session->s_num_cap_releases++;
1994 if (!(session->s_num_cap_releases % CEPH_CAPS_PER_RELEASE))
1995 ceph_flush_cap_releases(session->s_mdsc, session);
1998 static void ceph_cap_reclaim_work(struct work_struct *work)
2000 struct ceph_mds_client *mdsc =
2001 container_of(work, struct ceph_mds_client, cap_reclaim_work);
2002 int ret = ceph_trim_dentries(mdsc);
2004 ceph_queue_cap_reclaim_work(mdsc);
2007 void ceph_queue_cap_reclaim_work(struct ceph_mds_client *mdsc)
2012 if (queue_work(mdsc->fsc->cap_wq, &mdsc->cap_reclaim_work)) {
2013 dout("caps reclaim work queued\n");
2015 dout("failed to queue caps release work\n");
2019 void ceph_reclaim_caps_nr(struct ceph_mds_client *mdsc, int nr)
2024 val = atomic_add_return(nr, &mdsc->cap_reclaim_pending);
2025 if ((val % CEPH_CAPS_PER_RELEASE) < nr) {
2026 atomic_set(&mdsc->cap_reclaim_pending, 0);
2027 ceph_queue_cap_reclaim_work(mdsc);
2035 int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
2038 struct ceph_inode_info *ci = ceph_inode(dir);
2039 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
2040 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
2041 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
2042 unsigned int num_entries;
2045 spin_lock(&ci->i_ceph_lock);
2046 num_entries = ci->i_files + ci->i_subdirs;
2047 spin_unlock(&ci->i_ceph_lock);
2048 num_entries = max(num_entries, 1U);
2049 num_entries = min(num_entries, opt->max_readdir);
2051 order = get_order(size * num_entries);
2052 while (order >= 0) {
2053 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
2056 if (rinfo->dir_entries)
2060 if (!rinfo->dir_entries)
2063 num_entries = (PAGE_SIZE << order) / size;
2064 num_entries = min(num_entries, opt->max_readdir);
2066 rinfo->dir_buf_size = PAGE_SIZE << order;
2067 req->r_num_caps = num_entries + 1;
2068 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
2069 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
2074 * Create an mds request.
2076 struct ceph_mds_request *
2077 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
2079 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
2080 struct timespec64 ts;
2083 return ERR_PTR(-ENOMEM);
2085 mutex_init(&req->r_fill_mutex);
2087 req->r_started = jiffies;
2088 req->r_resend_mds = -1;
2089 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
2090 INIT_LIST_HEAD(&req->r_unsafe_target_item);
2092 kref_init(&req->r_kref);
2093 RB_CLEAR_NODE(&req->r_node);
2094 INIT_LIST_HEAD(&req->r_wait);
2095 init_completion(&req->r_completion);
2096 init_completion(&req->r_safe_completion);
2097 INIT_LIST_HEAD(&req->r_unsafe_item);
2099 ktime_get_coarse_real_ts64(&ts);
2100 req->r_stamp = timespec64_trunc(ts, mdsc->fsc->sb->s_time_gran);
2103 req->r_direct_mode = mode;
2108 * return oldest (lowest) request, tid in request tree, 0 if none.
2110 * called under mdsc->mutex.
2112 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
2114 if (RB_EMPTY_ROOT(&mdsc->request_tree))
2116 return rb_entry(rb_first(&mdsc->request_tree),
2117 struct ceph_mds_request, r_node);
2120 static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
2122 return mdsc->oldest_tid;
2126 * Build a dentry's path. Allocate on heap; caller must kfree. Based
2127 * on build_path_from_dentry in fs/cifs/dir.c.
2129 * If @stop_on_nosnap, generate path relative to the first non-snapped
2132 * Encode hidden .snap dirs as a double /, i.e.
2133 * foo/.snap/bar -> foo//bar
2135 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase,
2138 struct dentry *temp;
2145 return ERR_PTR(-EINVAL);
2149 return ERR_PTR(-ENOMEM);
2154 seq = read_seqbegin(&rename_lock);
2158 struct inode *inode;
2160 spin_lock(&temp->d_lock);
2161 inode = d_inode(temp);
2162 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
2163 dout("build_path path+%d: %p SNAPDIR\n",
2165 } else if (stop_on_nosnap && inode && dentry != temp &&
2166 ceph_snap(inode) == CEPH_NOSNAP) {
2167 spin_unlock(&temp->d_lock);
2168 pos++; /* get rid of any prepended '/' */
2171 pos -= temp->d_name.len;
2173 spin_unlock(&temp->d_lock);
2176 memcpy(path + pos, temp->d_name.name, temp->d_name.len);
2178 spin_unlock(&temp->d_lock);
2179 temp = READ_ONCE(temp->d_parent);
2181 /* Are we at the root? */
2185 /* Are we out of buffer? */
2191 base = ceph_ino(d_inode(temp));
2194 if (read_seqretry(&rename_lock, seq))
2199 * A rename didn't occur, but somehow we didn't end up where
2200 * we thought we would. Throw a warning and try again.
2202 pr_warn("build_path did not end path lookup where "
2203 "expected, pos is %d\n", pos);
2208 *plen = PATH_MAX - 1 - pos;
2209 dout("build_path on %p %d built %llx '%.*s'\n",
2210 dentry, d_count(dentry), base, *plen, path + pos);
2214 static int build_dentry_path(struct dentry *dentry, struct inode *dir,
2215 const char **ppath, int *ppathlen, u64 *pino,
2216 bool *pfreepath, bool parent_locked)
2222 dir = d_inode_rcu(dentry->d_parent);
2223 if (dir && parent_locked && ceph_snap(dir) == CEPH_NOSNAP) {
2224 *pino = ceph_ino(dir);
2226 *ppath = dentry->d_name.name;
2227 *ppathlen = dentry->d_name.len;
2231 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2233 return PTR_ERR(path);
2239 static int build_inode_path(struct inode *inode,
2240 const char **ppath, int *ppathlen, u64 *pino,
2243 struct dentry *dentry;
2246 if (ceph_snap(inode) == CEPH_NOSNAP) {
2247 *pino = ceph_ino(inode);
2251 dentry = d_find_alias(inode);
2252 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2255 return PTR_ERR(path);
2262 * request arguments may be specified via an inode *, a dentry *, or
2263 * an explicit ino+path.
2265 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
2266 struct inode *rdiri, const char *rpath,
2267 u64 rino, const char **ppath, int *pathlen,
2268 u64 *ino, bool *freepath, bool parent_locked)
2273 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
2274 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
2276 } else if (rdentry) {
2277 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
2278 freepath, parent_locked);
2279 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
2281 } else if (rpath || rino) {
2284 *pathlen = rpath ? strlen(rpath) : 0;
2285 dout(" path %.*s\n", *pathlen, rpath);
2292 * called under mdsc->mutex
2294 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
2295 struct ceph_mds_request *req,
2296 int mds, bool drop_cap_releases)
2298 struct ceph_msg *msg;
2299 struct ceph_mds_request_head *head;
2300 const char *path1 = NULL;
2301 const char *path2 = NULL;
2302 u64 ino1 = 0, ino2 = 0;
2303 int pathlen1 = 0, pathlen2 = 0;
2304 bool freepath1 = false, freepath2 = false;
2310 ret = set_request_path_attr(req->r_inode, req->r_dentry,
2311 req->r_parent, req->r_path1, req->r_ino1.ino,
2312 &path1, &pathlen1, &ino1, &freepath1,
2313 test_bit(CEPH_MDS_R_PARENT_LOCKED,
2314 &req->r_req_flags));
2320 /* If r_old_dentry is set, then assume that its parent is locked */
2321 ret = set_request_path_attr(NULL, req->r_old_dentry,
2322 req->r_old_dentry_dir,
2323 req->r_path2, req->r_ino2.ino,
2324 &path2, &pathlen2, &ino2, &freepath2, true);
2330 len = sizeof(*head) +
2331 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
2332 sizeof(struct ceph_timespec);
2334 /* calculate (max) length for cap releases */
2335 len += sizeof(struct ceph_mds_request_release) *
2336 (!!req->r_inode_drop + !!req->r_dentry_drop +
2337 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2338 if (req->r_dentry_drop)
2340 if (req->r_old_dentry_drop)
2343 msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
2345 msg = ERR_PTR(-ENOMEM);
2349 msg->hdr.version = cpu_to_le16(2);
2350 msg->hdr.tid = cpu_to_le64(req->r_tid);
2352 head = msg->front.iov_base;
2353 p = msg->front.iov_base + sizeof(*head);
2354 end = msg->front.iov_base + msg->front.iov_len;
2356 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2357 head->op = cpu_to_le32(req->r_op);
2358 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2359 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
2361 head->args = req->r_args;
2363 ceph_encode_filepath(&p, end, ino1, path1);
2364 ceph_encode_filepath(&p, end, ino2, path2);
2366 /* make note of release offset, in case we need to replay */
2367 req->r_request_release_offset = p - msg->front.iov_base;
2371 if (req->r_inode_drop)
2372 releases += ceph_encode_inode_release(&p,
2373 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
2374 mds, req->r_inode_drop, req->r_inode_unless, 0);
2375 if (req->r_dentry_drop)
2376 releases += ceph_encode_dentry_release(&p, req->r_dentry,
2377 req->r_parent, mds, req->r_dentry_drop,
2378 req->r_dentry_unless);
2379 if (req->r_old_dentry_drop)
2380 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
2381 req->r_old_dentry_dir, mds,
2382 req->r_old_dentry_drop,
2383 req->r_old_dentry_unless);
2384 if (req->r_old_inode_drop)
2385 releases += ceph_encode_inode_release(&p,
2386 d_inode(req->r_old_dentry),
2387 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
2389 if (drop_cap_releases) {
2391 p = msg->front.iov_base + req->r_request_release_offset;
2394 head->num_releases = cpu_to_le16(releases);
2398 struct ceph_timespec ts;
2399 ceph_encode_timespec64(&ts, &req->r_stamp);
2400 ceph_encode_copy(&p, &ts, sizeof(ts));
2404 msg->front.iov_len = p - msg->front.iov_base;
2405 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2407 if (req->r_pagelist) {
2408 struct ceph_pagelist *pagelist = req->r_pagelist;
2409 ceph_msg_data_add_pagelist(msg, pagelist);
2410 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2412 msg->hdr.data_len = 0;
2415 msg->hdr.data_off = cpu_to_le16(0);
2419 ceph_mdsc_free_path((char *)path2, pathlen2);
2422 ceph_mdsc_free_path((char *)path1, pathlen1);
2428 * called under mdsc->mutex if error, under no mutex if
2431 static void complete_request(struct ceph_mds_client *mdsc,
2432 struct ceph_mds_request *req)
2434 if (req->r_callback)
2435 req->r_callback(mdsc, req);
2436 complete_all(&req->r_completion);
2440 * called under mdsc->mutex
2442 static int __prepare_send_request(struct ceph_mds_client *mdsc,
2443 struct ceph_mds_request *req,
2444 int mds, bool drop_cap_releases)
2446 struct ceph_mds_request_head *rhead;
2447 struct ceph_msg *msg;
2452 struct ceph_cap *cap =
2453 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2456 req->r_sent_on_mseq = cap->mseq;
2458 req->r_sent_on_mseq = -1;
2460 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2461 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2463 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2466 * Replay. Do not regenerate message (and rebuild
2467 * paths, etc.); just use the original message.
2468 * Rebuilding paths will break for renames because
2469 * d_move mangles the src name.
2471 msg = req->r_request;
2472 rhead = msg->front.iov_base;
2474 flags = le32_to_cpu(rhead->flags);
2475 flags |= CEPH_MDS_FLAG_REPLAY;
2476 rhead->flags = cpu_to_le32(flags);
2478 if (req->r_target_inode)
2479 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2481 rhead->num_retry = req->r_attempts - 1;
2483 /* remove cap/dentry releases from message */
2484 rhead->num_releases = 0;
2487 p = msg->front.iov_base + req->r_request_release_offset;
2489 struct ceph_timespec ts;
2490 ceph_encode_timespec64(&ts, &req->r_stamp);
2491 ceph_encode_copy(&p, &ts, sizeof(ts));
2494 msg->front.iov_len = p - msg->front.iov_base;
2495 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2499 if (req->r_request) {
2500 ceph_msg_put(req->r_request);
2501 req->r_request = NULL;
2503 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
2505 req->r_err = PTR_ERR(msg);
2506 return PTR_ERR(msg);
2508 req->r_request = msg;
2510 rhead = msg->front.iov_base;
2511 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
2512 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2513 flags |= CEPH_MDS_FLAG_REPLAY;
2515 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2516 rhead->flags = cpu_to_le32(flags);
2517 rhead->num_fwd = req->r_num_fwd;
2518 rhead->num_retry = req->r_attempts - 1;
2521 dout(" r_parent = %p\n", req->r_parent);
2526 * send request, or put it on the appropriate wait list.
2528 static void __do_request(struct ceph_mds_client *mdsc,
2529 struct ceph_mds_request *req)
2531 struct ceph_mds_session *session = NULL;
2535 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2536 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
2537 __unregister_request(mdsc, req);
2541 if (req->r_timeout &&
2542 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2543 dout("do_request timed out\n");
2547 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
2548 dout("do_request forced umount\n");
2552 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
2553 if (mdsc->mdsmap_err) {
2554 err = mdsc->mdsmap_err;
2555 dout("do_request mdsmap err %d\n", err);
2558 if (mdsc->mdsmap->m_epoch == 0) {
2559 dout("do_request no mdsmap, waiting for map\n");
2560 list_add(&req->r_wait, &mdsc->waiting_for_map);
2563 if (!(mdsc->fsc->mount_options->flags &
2564 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2565 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
2567 pr_info("probably no mds server is up\n");
2572 put_request_session(req);
2574 mds = __choose_mds(mdsc, req);
2576 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2577 dout("do_request no mds or not active, waiting for map\n");
2578 list_add(&req->r_wait, &mdsc->waiting_for_map);
2582 /* get, open session */
2583 session = __ceph_lookup_mds_session(mdsc, mds);
2585 session = register_session(mdsc, mds);
2586 if (IS_ERR(session)) {
2587 err = PTR_ERR(session);
2591 req->r_session = get_session(session);
2593 dout("do_request mds%d session %p state %s\n", mds, session,
2594 ceph_session_state_name(session->s_state));
2595 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2596 session->s_state != CEPH_MDS_SESSION_HUNG) {
2597 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2601 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2602 session->s_state == CEPH_MDS_SESSION_CLOSING)
2603 __open_session(mdsc, session);
2604 list_add(&req->r_wait, &session->s_waiting);
2609 req->r_resend_mds = -1; /* forget any previous mds hint */
2611 if (req->r_request_started == 0) /* note request start time */
2612 req->r_request_started = jiffies;
2614 err = __prepare_send_request(mdsc, req, mds, false);
2616 ceph_msg_get(req->r_request);
2617 ceph_con_send(&session->s_con, req->r_request);
2621 ceph_put_mds_session(session);
2624 dout("__do_request early error %d\n", err);
2626 complete_request(mdsc, req);
2627 __unregister_request(mdsc, req);
2633 * called under mdsc->mutex
2635 static void __wake_requests(struct ceph_mds_client *mdsc,
2636 struct list_head *head)
2638 struct ceph_mds_request *req;
2639 LIST_HEAD(tmp_list);
2641 list_splice_init(head, &tmp_list);
2643 while (!list_empty(&tmp_list)) {
2644 req = list_entry(tmp_list.next,
2645 struct ceph_mds_request, r_wait);
2646 list_del_init(&req->r_wait);
2647 dout(" wake request %p tid %llu\n", req, req->r_tid);
2648 __do_request(mdsc, req);
2653 * Wake up threads with requests pending for @mds, so that they can
2654 * resubmit their requests to a possibly different mds.
2656 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
2658 struct ceph_mds_request *req;
2659 struct rb_node *p = rb_first(&mdsc->request_tree);
2661 dout("kick_requests mds%d\n", mds);
2663 req = rb_entry(p, struct ceph_mds_request, r_node);
2665 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2667 if (req->r_attempts > 0)
2668 continue; /* only new requests */
2669 if (req->r_session &&
2670 req->r_session->s_mds == mds) {
2671 dout(" kicking tid %llu\n", req->r_tid);
2672 list_del_init(&req->r_wait);
2673 __do_request(mdsc, req);
2678 int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
2679 struct ceph_mds_request *req)
2683 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
2685 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
2686 if (req->r_parent) {
2687 ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
2688 ihold(req->r_parent);
2690 if (req->r_old_dentry_dir)
2691 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2694 dout("submit_request on %p for inode %p\n", req, dir);
2695 mutex_lock(&mdsc->mutex);
2696 __register_request(mdsc, req, dir);
2697 __do_request(mdsc, req);
2699 mutex_unlock(&mdsc->mutex);
2703 static int ceph_mdsc_wait_request(struct ceph_mds_client *mdsc,
2704 struct ceph_mds_request *req)
2709 dout("do_request waiting\n");
2710 if (!req->r_timeout && req->r_wait_for_completion) {
2711 err = req->r_wait_for_completion(mdsc, req);
2713 long timeleft = wait_for_completion_killable_timeout(
2715 ceph_timeout_jiffies(req->r_timeout));
2719 err = -EIO; /* timed out */
2721 err = timeleft; /* killed */
2723 dout("do_request waited, got %d\n", err);
2724 mutex_lock(&mdsc->mutex);
2726 /* only abort if we didn't race with a real reply */
2727 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2728 err = le32_to_cpu(req->r_reply_info.head->result);
2729 } else if (err < 0) {
2730 dout("aborted request %lld with %d\n", req->r_tid, err);
2733 * ensure we aren't running concurrently with
2734 * ceph_fill_trace or ceph_readdir_prepopulate, which
2735 * rely on locks (dir mutex) held by our caller.
2737 mutex_lock(&req->r_fill_mutex);
2739 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
2740 mutex_unlock(&req->r_fill_mutex);
2742 if (req->r_parent &&
2743 (req->r_op & CEPH_MDS_OP_WRITE))
2744 ceph_invalidate_dir_request(req);
2749 mutex_unlock(&mdsc->mutex);
2754 * Synchrously perform an mds request. Take care of all of the
2755 * session setup, forwarding, retry details.
2757 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2759 struct ceph_mds_request *req)
2763 dout("do_request on %p\n", req);
2766 err = ceph_mdsc_submit_request(mdsc, dir, req);
2768 err = ceph_mdsc_wait_request(mdsc, req);
2769 dout("do_request %p done, result %d\n", req, err);
2774 * Invalidate dir's completeness, dentry lease state on an aborted MDS
2775 * namespace request.
2777 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2779 struct inode *dir = req->r_parent;
2780 struct inode *old_dir = req->r_old_dentry_dir;
2782 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
2784 ceph_dir_clear_complete(dir);
2786 ceph_dir_clear_complete(old_dir);
2788 ceph_invalidate_dentry_lease(req->r_dentry);
2789 if (req->r_old_dentry)
2790 ceph_invalidate_dentry_lease(req->r_old_dentry);
2796 * We take the session mutex and parse and process the reply immediately.
2797 * This preserves the logical ordering of replies, capabilities, etc., sent
2798 * by the MDS as they are applied to our local cache.
2800 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2802 struct ceph_mds_client *mdsc = session->s_mdsc;
2803 struct ceph_mds_request *req;
2804 struct ceph_mds_reply_head *head = msg->front.iov_base;
2805 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2806 struct ceph_snap_realm *realm;
2809 int mds = session->s_mds;
2811 if (msg->front.iov_len < sizeof(*head)) {
2812 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2817 /* get request, session */
2818 tid = le64_to_cpu(msg->hdr.tid);
2819 mutex_lock(&mdsc->mutex);
2820 req = lookup_get_request(mdsc, tid);
2822 dout("handle_reply on unknown tid %llu\n", tid);
2823 mutex_unlock(&mdsc->mutex);
2826 dout("handle_reply %p\n", req);
2828 /* correct session? */
2829 if (req->r_session != session) {
2830 pr_err("mdsc_handle_reply got %llu on session mds%d"
2831 " not mds%d\n", tid, session->s_mds,
2832 req->r_session ? req->r_session->s_mds : -1);
2833 mutex_unlock(&mdsc->mutex);
2838 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
2839 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
2840 pr_warn("got a dup %s reply on %llu from mds%d\n",
2841 head->safe ? "safe" : "unsafe", tid, mds);
2842 mutex_unlock(&mdsc->mutex);
2845 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
2846 pr_warn("got unsafe after safe on %llu from mds%d\n",
2848 mutex_unlock(&mdsc->mutex);
2852 result = le32_to_cpu(head->result);
2856 * if we're not talking to the authority, send to them
2857 * if the authority has changed while we weren't looking,
2858 * send to new authority
2859 * Otherwise we just have to return an ESTALE
2861 if (result == -ESTALE) {
2862 dout("got ESTALE on request %llu\n", req->r_tid);
2863 req->r_resend_mds = -1;
2864 if (req->r_direct_mode != USE_AUTH_MDS) {
2865 dout("not using auth, setting for that now\n");
2866 req->r_direct_mode = USE_AUTH_MDS;
2867 __do_request(mdsc, req);
2868 mutex_unlock(&mdsc->mutex);
2871 int mds = __choose_mds(mdsc, req);
2872 if (mds >= 0 && mds != req->r_session->s_mds) {
2873 dout("but auth changed, so resending\n");
2874 __do_request(mdsc, req);
2875 mutex_unlock(&mdsc->mutex);
2879 dout("have to return ESTALE on request %llu\n", req->r_tid);
2884 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
2885 __unregister_request(mdsc, req);
2887 /* last request during umount? */
2888 if (mdsc->stopping && !__get_oldest_req(mdsc))
2889 complete_all(&mdsc->safe_umount_waiters);
2891 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2893 * We already handled the unsafe response, now do the
2894 * cleanup. No need to examine the response; the MDS
2895 * doesn't include any result info in the safe
2896 * response. And even if it did, there is nothing
2897 * useful we could do with a revised return value.
2899 dout("got safe reply %llu, mds%d\n", tid, mds);
2901 mutex_unlock(&mdsc->mutex);
2905 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
2906 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2907 if (req->r_unsafe_dir) {
2908 struct ceph_inode_info *ci =
2909 ceph_inode(req->r_unsafe_dir);
2910 spin_lock(&ci->i_unsafe_lock);
2911 list_add_tail(&req->r_unsafe_dir_item,
2912 &ci->i_unsafe_dirops);
2913 spin_unlock(&ci->i_unsafe_lock);
2917 dout("handle_reply tid %lld result %d\n", tid, result);
2918 rinfo = &req->r_reply_info;
2919 if (test_bit(CEPHFS_FEATURE_REPLY_ENCODING, &session->s_features))
2920 err = parse_reply_info(msg, rinfo, (u64)-1);
2922 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2923 mutex_unlock(&mdsc->mutex);
2925 mutex_lock(&session->s_mutex);
2927 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
2934 if (rinfo->snapblob_len) {
2935 down_write(&mdsc->snap_rwsem);
2936 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2937 rinfo->snapblob + rinfo->snapblob_len,
2938 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2940 downgrade_write(&mdsc->snap_rwsem);
2942 down_read(&mdsc->snap_rwsem);
2945 /* insert trace into our cache */
2946 mutex_lock(&req->r_fill_mutex);
2947 current->journal_info = req;
2948 err = ceph_fill_trace(mdsc->fsc->sb, req);
2950 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
2951 req->r_op == CEPH_MDS_OP_LSSNAP))
2952 ceph_readdir_prepopulate(req, req->r_session);
2954 current->journal_info = NULL;
2955 mutex_unlock(&req->r_fill_mutex);
2957 up_read(&mdsc->snap_rwsem);
2959 ceph_put_snap_realm(mdsc, realm);
2962 if (req->r_target_inode &&
2963 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2964 struct ceph_inode_info *ci =
2965 ceph_inode(req->r_target_inode);
2966 spin_lock(&ci->i_unsafe_lock);
2967 list_add_tail(&req->r_unsafe_target_item,
2968 &ci->i_unsafe_iops);
2969 spin_unlock(&ci->i_unsafe_lock);
2972 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2975 mutex_lock(&mdsc->mutex);
2976 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
2980 req->r_reply = ceph_msg_get(msg);
2981 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
2984 dout("reply arrived after request %lld was aborted\n", tid);
2986 mutex_unlock(&mdsc->mutex);
2988 mutex_unlock(&session->s_mutex);
2990 /* kick calling process */
2991 complete_request(mdsc, req);
2993 ceph_mdsc_put_request(req);
3000 * handle mds notification that our request has been forwarded.
3002 static void handle_forward(struct ceph_mds_client *mdsc,
3003 struct ceph_mds_session *session,
3004 struct ceph_msg *msg)
3006 struct ceph_mds_request *req;
3007 u64 tid = le64_to_cpu(msg->hdr.tid);
3011 void *p = msg->front.iov_base;
3012 void *end = p + msg->front.iov_len;
3014 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3015 next_mds = ceph_decode_32(&p);
3016 fwd_seq = ceph_decode_32(&p);
3018 mutex_lock(&mdsc->mutex);
3019 req = lookup_get_request(mdsc, tid);
3021 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
3022 goto out; /* dup reply? */
3025 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
3026 dout("forward tid %llu aborted, unregistering\n", tid);
3027 __unregister_request(mdsc, req);
3028 } else if (fwd_seq <= req->r_num_fwd) {
3029 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
3030 tid, next_mds, req->r_num_fwd, fwd_seq);
3032 /* resend. forward race not possible; mds would drop */
3033 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
3035 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
3036 req->r_attempts = 0;
3037 req->r_num_fwd = fwd_seq;
3038 req->r_resend_mds = next_mds;
3039 put_request_session(req);
3040 __do_request(mdsc, req);
3042 ceph_mdsc_put_request(req);
3044 mutex_unlock(&mdsc->mutex);
3048 pr_err("mdsc_handle_forward decode error err=%d\n", err);
3051 static int __decode_session_metadata(void **p, void *end,
3054 /* map<string,string> */
3057 ceph_decode_32_safe(p, end, n, bad);
3060 ceph_decode_32_safe(p, end, len, bad);
3061 ceph_decode_need(p, end, len, bad);
3062 err_str = !strncmp(*p, "error_string", len);
3064 ceph_decode_32_safe(p, end, len, bad);
3065 ceph_decode_need(p, end, len, bad);
3066 if (err_str && strnstr(*p, "blacklisted", len))
3067 *blacklisted = true;
3076 * handle a mds session control message
3078 static void handle_session(struct ceph_mds_session *session,
3079 struct ceph_msg *msg)
3081 struct ceph_mds_client *mdsc = session->s_mdsc;
3082 int mds = session->s_mds;
3083 int msg_version = le16_to_cpu(msg->hdr.version);
3084 void *p = msg->front.iov_base;
3085 void *end = p + msg->front.iov_len;
3086 struct ceph_mds_session_head *h;
3089 unsigned long features = 0;
3091 bool blacklisted = false;
3094 ceph_decode_need(&p, end, sizeof(*h), bad);
3098 op = le32_to_cpu(h->op);
3099 seq = le64_to_cpu(h->seq);
3101 if (msg_version >= 3) {
3103 /* version >= 2, metadata */
3104 if (__decode_session_metadata(&p, end, &blacklisted) < 0)
3106 /* version >= 3, feature bits */
3107 ceph_decode_32_safe(&p, end, len, bad);
3108 ceph_decode_need(&p, end, len, bad);
3109 memcpy(&features, p, min_t(size_t, len, sizeof(features)));
3113 mutex_lock(&mdsc->mutex);
3114 if (op == CEPH_SESSION_CLOSE) {
3115 get_session(session);
3116 __unregister_session(mdsc, session);
3118 /* FIXME: this ttl calculation is generous */
3119 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
3120 mutex_unlock(&mdsc->mutex);
3122 mutex_lock(&session->s_mutex);
3124 dout("handle_session mds%d %s %p state %s seq %llu\n",
3125 mds, ceph_session_op_name(op), session,
3126 ceph_session_state_name(session->s_state), seq);
3128 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
3129 session->s_state = CEPH_MDS_SESSION_OPEN;
3130 pr_info("mds%d came back\n", session->s_mds);
3134 case CEPH_SESSION_OPEN:
3135 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3136 pr_info("mds%d reconnect success\n", session->s_mds);
3137 session->s_state = CEPH_MDS_SESSION_OPEN;
3138 session->s_features = features;
3139 renewed_caps(mdsc, session, 0);
3142 __close_session(mdsc, session);
3145 case CEPH_SESSION_RENEWCAPS:
3146 if (session->s_renew_seq == seq)
3147 renewed_caps(mdsc, session, 1);
3150 case CEPH_SESSION_CLOSE:
3151 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3152 pr_info("mds%d reconnect denied\n", session->s_mds);
3153 cleanup_session_requests(mdsc, session);
3154 remove_session_caps(session);
3155 wake = 2; /* for good measure */
3156 wake_up_all(&mdsc->session_close_wq);
3159 case CEPH_SESSION_STALE:
3160 pr_info("mds%d caps went stale, renewing\n",
3162 spin_lock(&session->s_gen_ttl_lock);
3163 session->s_cap_gen++;
3164 session->s_cap_ttl = jiffies - 1;
3165 spin_unlock(&session->s_gen_ttl_lock);
3166 send_renew_caps(mdsc, session);
3169 case CEPH_SESSION_RECALL_STATE:
3170 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
3173 case CEPH_SESSION_FLUSHMSG:
3174 send_flushmsg_ack(mdsc, session, seq);
3177 case CEPH_SESSION_FORCE_RO:
3178 dout("force_session_readonly %p\n", session);
3179 spin_lock(&session->s_cap_lock);
3180 session->s_readonly = true;
3181 spin_unlock(&session->s_cap_lock);
3182 wake_up_session_caps(session, FORCE_RO);
3185 case CEPH_SESSION_REJECT:
3186 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
3187 pr_info("mds%d rejected session\n", session->s_mds);
3188 session->s_state = CEPH_MDS_SESSION_REJECTED;
3189 cleanup_session_requests(mdsc, session);
3190 remove_session_caps(session);
3192 mdsc->fsc->blacklisted = true;
3193 wake = 2; /* for good measure */
3197 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
3201 mutex_unlock(&session->s_mutex);
3203 mutex_lock(&mdsc->mutex);
3204 __wake_requests(mdsc, &session->s_waiting);
3206 kick_requests(mdsc, mds);
3207 mutex_unlock(&mdsc->mutex);
3209 if (op == CEPH_SESSION_CLOSE)
3210 ceph_put_mds_session(session);
3214 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
3215 (int)msg->front.iov_len);
3222 * called under session->mutex.
3224 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
3225 struct ceph_mds_session *session)
3227 struct ceph_mds_request *req, *nreq;
3231 dout("replay_unsafe_requests mds%d\n", session->s_mds);
3233 mutex_lock(&mdsc->mutex);
3234 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
3235 err = __prepare_send_request(mdsc, req, session->s_mds, true);
3237 ceph_msg_get(req->r_request);
3238 ceph_con_send(&session->s_con, req->r_request);
3243 * also re-send old requests when MDS enters reconnect stage. So that MDS
3244 * can process completed request in clientreplay stage.
3246 p = rb_first(&mdsc->request_tree);
3248 req = rb_entry(p, struct ceph_mds_request, r_node);
3250 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
3252 if (req->r_attempts == 0)
3253 continue; /* only old requests */
3254 if (req->r_session &&
3255 req->r_session->s_mds == session->s_mds) {
3256 err = __prepare_send_request(mdsc, req,
3257 session->s_mds, true);
3259 ceph_msg_get(req->r_request);
3260 ceph_con_send(&session->s_con, req->r_request);
3264 mutex_unlock(&mdsc->mutex);
3267 static int send_reconnect_partial(struct ceph_reconnect_state *recon_state)
3269 struct ceph_msg *reply;
3270 struct ceph_pagelist *_pagelist;
3275 if (!recon_state->allow_multi)
3278 /* can't handle message that contains both caps and realm */
3279 BUG_ON(!recon_state->nr_caps == !recon_state->nr_realms);
3281 /* pre-allocate new pagelist */
3282 _pagelist = ceph_pagelist_alloc(GFP_NOFS);
3286 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3290 /* placeholder for nr_caps */
3291 err = ceph_pagelist_encode_32(_pagelist, 0);
3295 if (recon_state->nr_caps) {
3296 /* currently encoding caps */
3297 err = ceph_pagelist_encode_32(recon_state->pagelist, 0);
3301 /* placeholder for nr_realms (currently encoding relams) */
3302 err = ceph_pagelist_encode_32(_pagelist, 0);
3307 err = ceph_pagelist_encode_8(recon_state->pagelist, 1);
3311 page = list_first_entry(&recon_state->pagelist->head, struct page, lru);
3312 addr = kmap_atomic(page);
3313 if (recon_state->nr_caps) {
3314 /* currently encoding caps */
3315 *addr = cpu_to_le32(recon_state->nr_caps);
3317 /* currently encoding relams */
3318 *(addr + 1) = cpu_to_le32(recon_state->nr_realms);
3320 kunmap_atomic(addr);
3322 reply->hdr.version = cpu_to_le16(5);
3323 reply->hdr.compat_version = cpu_to_le16(4);
3325 reply->hdr.data_len = cpu_to_le32(recon_state->pagelist->length);
3326 ceph_msg_data_add_pagelist(reply, recon_state->pagelist);
3328 ceph_con_send(&recon_state->session->s_con, reply);
3329 ceph_pagelist_release(recon_state->pagelist);
3331 recon_state->pagelist = _pagelist;
3332 recon_state->nr_caps = 0;
3333 recon_state->nr_realms = 0;
3334 recon_state->msg_version = 5;
3337 ceph_msg_put(reply);
3339 ceph_pagelist_release(_pagelist);
3344 * Encode information about a cap for a reconnect with the MDS.
3346 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
3350 struct ceph_mds_cap_reconnect v2;
3351 struct ceph_mds_cap_reconnect_v1 v1;
3353 struct ceph_inode_info *ci = cap->ci;
3354 struct ceph_reconnect_state *recon_state = arg;
3355 struct ceph_pagelist *pagelist = recon_state->pagelist;
3359 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
3360 inode, ceph_vinop(inode), cap, cap->cap_id,
3361 ceph_cap_string(cap->issued));
3363 spin_lock(&ci->i_ceph_lock);
3364 cap->seq = 0; /* reset cap seq */
3365 cap->issue_seq = 0; /* and issue_seq */
3366 cap->mseq = 0; /* and migrate_seq */
3367 cap->cap_gen = cap->session->s_cap_gen;
3369 if (recon_state->msg_version >= 2) {
3370 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
3371 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3372 rec.v2.issued = cpu_to_le32(cap->issued);
3373 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
3374 rec.v2.pathbase = 0;
3375 rec.v2.flock_len = (__force __le32)
3376 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
3378 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
3379 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3380 rec.v1.issued = cpu_to_le32(cap->issued);
3381 rec.v1.size = cpu_to_le64(inode->i_size);
3382 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
3383 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
3384 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
3385 rec.v1.pathbase = 0;
3388 if (list_empty(&ci->i_cap_snaps)) {
3389 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
3391 struct ceph_cap_snap *capsnap =
3392 list_first_entry(&ci->i_cap_snaps,
3393 struct ceph_cap_snap, ci_item);
3394 snap_follows = capsnap->follows;
3396 spin_unlock(&ci->i_ceph_lock);
3398 if (recon_state->msg_version >= 2) {
3399 int num_fcntl_locks, num_flock_locks;
3400 struct ceph_filelock *flocks = NULL;
3401 size_t struct_len, total_len = sizeof(u64);
3405 if (rec.v2.flock_len) {
3406 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
3408 num_fcntl_locks = 0;
3409 num_flock_locks = 0;
3411 if (num_fcntl_locks + num_flock_locks > 0) {
3412 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
3413 sizeof(struct ceph_filelock),
3419 err = ceph_encode_locks_to_buffer(inode, flocks,
3434 if (recon_state->msg_version >= 3) {
3435 /* version, compat_version and struct_len */
3436 total_len += 2 * sizeof(u8) + sizeof(u32);
3440 * number of encoded locks is stable, so copy to pagelist
3442 struct_len = 2 * sizeof(u32) +
3443 (num_fcntl_locks + num_flock_locks) *
3444 sizeof(struct ceph_filelock);
3445 rec.v2.flock_len = cpu_to_le32(struct_len);
3447 struct_len += sizeof(u32) + sizeof(rec.v2);
3450 struct_len += sizeof(u64); /* snap_follows */
3452 total_len += struct_len;
3454 if (pagelist->length + total_len > RECONNECT_MAX_SIZE) {
3455 err = send_reconnect_partial(recon_state);
3457 goto out_freeflocks;
3458 pagelist = recon_state->pagelist;
3461 err = ceph_pagelist_reserve(pagelist, total_len);
3463 goto out_freeflocks;
3465 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
3466 if (recon_state->msg_version >= 3) {
3467 ceph_pagelist_encode_8(pagelist, struct_v);
3468 ceph_pagelist_encode_8(pagelist, 1);
3469 ceph_pagelist_encode_32(pagelist, struct_len);
3471 ceph_pagelist_encode_string(pagelist, NULL, 0);
3472 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3473 ceph_locks_to_pagelist(flocks, pagelist,
3474 num_fcntl_locks, num_flock_locks);
3476 ceph_pagelist_encode_64(pagelist, snap_follows);
3483 struct dentry *dentry;
3485 dentry = d_find_alias(inode);
3487 path = ceph_mdsc_build_path(dentry,
3488 &pathlen, &pathbase, 0);
3491 err = PTR_ERR(path);
3494 rec.v1.pathbase = cpu_to_le64(pathbase);
3497 err = ceph_pagelist_reserve(pagelist,
3498 sizeof(u64) + sizeof(u32) +
3499 pathlen + sizeof(rec.v1));
3504 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
3505 ceph_pagelist_encode_string(pagelist, path, pathlen);
3506 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
3508 ceph_mdsc_free_path(path, pathlen);
3513 recon_state->nr_caps++;
3517 static int encode_snap_realms(struct ceph_mds_client *mdsc,
3518 struct ceph_reconnect_state *recon_state)
3521 struct ceph_pagelist *pagelist = recon_state->pagelist;
3524 if (recon_state->msg_version >= 4) {
3525 err = ceph_pagelist_encode_32(pagelist, mdsc->num_snap_realms);
3531 * snaprealms. we provide mds with the ino, seq (version), and
3532 * parent for all of our realms. If the mds has any newer info,
3535 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3536 struct ceph_snap_realm *realm =
3537 rb_entry(p, struct ceph_snap_realm, node);
3538 struct ceph_mds_snaprealm_reconnect sr_rec;
3540 if (recon_state->msg_version >= 4) {
3541 size_t need = sizeof(u8) * 2 + sizeof(u32) +
3544 if (pagelist->length + need > RECONNECT_MAX_SIZE) {
3545 err = send_reconnect_partial(recon_state);
3548 pagelist = recon_state->pagelist;
3551 err = ceph_pagelist_reserve(pagelist, need);
3555 ceph_pagelist_encode_8(pagelist, 1);
3556 ceph_pagelist_encode_8(pagelist, 1);
3557 ceph_pagelist_encode_32(pagelist, sizeof(sr_rec));
3560 dout(" adding snap realm %llx seq %lld parent %llx\n",
3561 realm->ino, realm->seq, realm->parent_ino);
3562 sr_rec.ino = cpu_to_le64(realm->ino);
3563 sr_rec.seq = cpu_to_le64(realm->seq);
3564 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3566 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3570 recon_state->nr_realms++;
3578 * If an MDS fails and recovers, clients need to reconnect in order to
3579 * reestablish shared state. This includes all caps issued through
3580 * this session _and_ the snap_realm hierarchy. Because it's not
3581 * clear which snap realms the mds cares about, we send everything we
3582 * know about.. that ensures we'll then get any new info the
3583 * recovering MDS might have.
3585 * This is a relatively heavyweight operation, but it's rare.
3587 * called with mdsc->mutex held.
3589 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3590 struct ceph_mds_session *session)
3592 struct ceph_msg *reply;
3593 int mds = session->s_mds;
3595 struct ceph_reconnect_state recon_state = {
3600 pr_info("mds%d reconnect start\n", mds);
3602 recon_state.pagelist = ceph_pagelist_alloc(GFP_NOFS);
3603 if (!recon_state.pagelist)
3604 goto fail_nopagelist;
3606 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3610 mutex_lock(&session->s_mutex);
3611 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3614 dout("session %p state %s\n", session,
3615 ceph_session_state_name(session->s_state));
3617 spin_lock(&session->s_gen_ttl_lock);
3618 session->s_cap_gen++;
3619 spin_unlock(&session->s_gen_ttl_lock);
3621 spin_lock(&session->s_cap_lock);
3622 /* don't know if session is readonly */
3623 session->s_readonly = 0;
3625 * notify __ceph_remove_cap() that we are composing cap reconnect.
3626 * If a cap get released before being added to the cap reconnect,
3627 * __ceph_remove_cap() should skip queuing cap release.
3629 session->s_cap_reconnect = 1;
3630 /* drop old cap expires; we're about to reestablish that state */
3631 detach_cap_releases(session, &dispose);
3632 spin_unlock(&session->s_cap_lock);
3633 dispose_cap_releases(mdsc, &dispose);
3635 /* trim unused caps to reduce MDS's cache rejoin time */
3636 if (mdsc->fsc->sb->s_root)
3637 shrink_dcache_parent(mdsc->fsc->sb->s_root);
3639 ceph_con_close(&session->s_con);
3640 ceph_con_open(&session->s_con,
3641 CEPH_ENTITY_TYPE_MDS, mds,
3642 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3644 /* replay unsafe requests */
3645 replay_unsafe_requests(mdsc, session);
3647 ceph_early_kick_flushing_caps(mdsc, session);
3649 down_read(&mdsc->snap_rwsem);
3651 /* placeholder for nr_caps */
3652 err = ceph_pagelist_encode_32(recon_state.pagelist, 0);
3656 if (test_bit(CEPHFS_FEATURE_MULTI_RECONNECT, &session->s_features)) {
3657 recon_state.msg_version = 3;
3658 recon_state.allow_multi = true;
3659 } else if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) {
3660 recon_state.msg_version = 3;
3662 recon_state.msg_version = 2;
3664 /* trsaverse this session's caps */
3665 err = ceph_iterate_session_caps(session, encode_caps_cb, &recon_state);
3667 spin_lock(&session->s_cap_lock);
3668 session->s_cap_reconnect = 0;
3669 spin_unlock(&session->s_cap_lock);
3674 /* check if all realms can be encoded into current message */
3675 if (mdsc->num_snap_realms) {
3677 recon_state.pagelist->length +
3678 mdsc->num_snap_realms *
3679 sizeof(struct ceph_mds_snaprealm_reconnect);
3680 if (recon_state.msg_version >= 4) {
3681 /* number of realms */
3682 total_len += sizeof(u32);
3683 /* version, compat_version and struct_len */
3684 total_len += mdsc->num_snap_realms *
3685 (2 * sizeof(u8) + sizeof(u32));
3687 if (total_len > RECONNECT_MAX_SIZE) {
3688 if (!recon_state.allow_multi) {
3692 if (recon_state.nr_caps) {
3693 err = send_reconnect_partial(&recon_state);
3697 recon_state.msg_version = 5;
3701 err = encode_snap_realms(mdsc, &recon_state);
3705 if (recon_state.msg_version >= 5) {
3706 err = ceph_pagelist_encode_8(recon_state.pagelist, 0);
3711 if (recon_state.nr_caps || recon_state.nr_realms) {
3713 list_first_entry(&recon_state.pagelist->head,
3715 __le32 *addr = kmap_atomic(page);
3716 if (recon_state.nr_caps) {
3717 WARN_ON(recon_state.nr_realms != mdsc->num_snap_realms);
3718 *addr = cpu_to_le32(recon_state.nr_caps);
3719 } else if (recon_state.msg_version >= 4) {
3720 *(addr + 1) = cpu_to_le32(recon_state.nr_realms);
3722 kunmap_atomic(addr);
3725 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
3726 if (recon_state.msg_version >= 4)
3727 reply->hdr.compat_version = cpu_to_le16(4);
3729 reply->hdr.data_len = cpu_to_le32(recon_state.pagelist->length);
3730 ceph_msg_data_add_pagelist(reply, recon_state.pagelist);
3732 ceph_con_send(&session->s_con, reply);
3734 mutex_unlock(&session->s_mutex);
3736 mutex_lock(&mdsc->mutex);
3737 __wake_requests(mdsc, &session->s_waiting);
3738 mutex_unlock(&mdsc->mutex);
3740 up_read(&mdsc->snap_rwsem);
3741 ceph_pagelist_release(recon_state.pagelist);
3745 ceph_msg_put(reply);
3746 up_read(&mdsc->snap_rwsem);
3747 mutex_unlock(&session->s_mutex);
3749 ceph_pagelist_release(recon_state.pagelist);
3751 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
3757 * compare old and new mdsmaps, kicking requests
3758 * and closing out old connections as necessary
3760 * called under mdsc->mutex.
3762 static void check_new_map(struct ceph_mds_client *mdsc,
3763 struct ceph_mdsmap *newmap,
3764 struct ceph_mdsmap *oldmap)
3767 int oldstate, newstate;
3768 struct ceph_mds_session *s;
3770 dout("check_new_map new %u old %u\n",
3771 newmap->m_epoch, oldmap->m_epoch);
3773 for (i = 0; i < oldmap->m_num_mds && i < mdsc->max_sessions; i++) {
3774 if (!mdsc->sessions[i])
3776 s = mdsc->sessions[i];
3777 oldstate = ceph_mdsmap_get_state(oldmap, i);
3778 newstate = ceph_mdsmap_get_state(newmap, i);
3780 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
3781 i, ceph_mds_state_name(oldstate),
3782 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
3783 ceph_mds_state_name(newstate),
3784 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
3785 ceph_session_state_name(s->s_state));
3787 if (i >= newmap->m_num_mds) {
3788 /* force close session for stopped mds */
3790 __unregister_session(mdsc, s);
3791 __wake_requests(mdsc, &s->s_waiting);
3792 mutex_unlock(&mdsc->mutex);
3794 mutex_lock(&s->s_mutex);
3795 cleanup_session_requests(mdsc, s);
3796 remove_session_caps(s);
3797 mutex_unlock(&s->s_mutex);
3799 ceph_put_mds_session(s);
3801 mutex_lock(&mdsc->mutex);
3802 kick_requests(mdsc, i);
3806 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
3807 ceph_mdsmap_get_addr(newmap, i),
3808 sizeof(struct ceph_entity_addr))) {
3810 mutex_unlock(&mdsc->mutex);
3811 mutex_lock(&s->s_mutex);
3812 mutex_lock(&mdsc->mutex);
3813 ceph_con_close(&s->s_con);
3814 mutex_unlock(&s->s_mutex);
3815 s->s_state = CEPH_MDS_SESSION_RESTARTING;
3816 } else if (oldstate == newstate) {
3817 continue; /* nothing new with this mds */
3823 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
3824 newstate >= CEPH_MDS_STATE_RECONNECT) {
3825 mutex_unlock(&mdsc->mutex);
3826 send_mds_reconnect(mdsc, s);
3827 mutex_lock(&mdsc->mutex);
3831 * kick request on any mds that has gone active.
3833 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3834 newstate >= CEPH_MDS_STATE_ACTIVE) {
3835 if (oldstate != CEPH_MDS_STATE_CREATING &&
3836 oldstate != CEPH_MDS_STATE_STARTING)
3837 pr_info("mds%d recovery completed\n", s->s_mds);
3838 kick_requests(mdsc, i);
3839 ceph_kick_flushing_caps(mdsc, s);
3840 wake_up_session_caps(s, RECONNECT);
3844 for (i = 0; i < newmap->m_num_mds && i < mdsc->max_sessions; i++) {
3845 s = mdsc->sessions[i];
3848 if (!ceph_mdsmap_is_laggy(newmap, i))
3850 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3851 s->s_state == CEPH_MDS_SESSION_HUNG ||
3852 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3853 dout(" connecting to export targets of laggy mds%d\n",
3855 __open_export_target_sessions(mdsc, s);
3867 * caller must hold session s_mutex, dentry->d_lock
3869 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3871 struct ceph_dentry_info *di = ceph_dentry(dentry);
3873 ceph_put_mds_session(di->lease_session);
3874 di->lease_session = NULL;
3877 static void handle_lease(struct ceph_mds_client *mdsc,
3878 struct ceph_mds_session *session,
3879 struct ceph_msg *msg)
3881 struct super_block *sb = mdsc->fsc->sb;
3882 struct inode *inode;
3883 struct dentry *parent, *dentry;
3884 struct ceph_dentry_info *di;
3885 int mds = session->s_mds;
3886 struct ceph_mds_lease *h = msg->front.iov_base;
3888 struct ceph_vino vino;
3892 dout("handle_lease from mds%d\n", mds);
3895 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3897 vino.ino = le64_to_cpu(h->ino);
3898 vino.snap = CEPH_NOSNAP;
3899 seq = le32_to_cpu(h->seq);
3900 dname.len = get_unaligned_le32(h + 1);
3901 if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
3903 dname.name = (void *)(h + 1) + sizeof(u32);
3906 inode = ceph_find_inode(sb, vino);
3907 dout("handle_lease %s, ino %llx %p %.*s\n",
3908 ceph_lease_op_name(h->action), vino.ino, inode,
3909 dname.len, dname.name);
3911 mutex_lock(&session->s_mutex);
3915 dout("handle_lease no inode %llx\n", vino.ino);
3920 parent = d_find_alias(inode);
3922 dout("no parent dentry on inode %p\n", inode);
3924 goto release; /* hrm... */
3926 dname.hash = full_name_hash(parent, dname.name, dname.len);
3927 dentry = d_lookup(parent, &dname);
3932 spin_lock(&dentry->d_lock);
3933 di = ceph_dentry(dentry);
3934 switch (h->action) {
3935 case CEPH_MDS_LEASE_REVOKE:
3936 if (di->lease_session == session) {
3937 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3938 h->seq = cpu_to_le32(di->lease_seq);
3939 __ceph_mdsc_drop_dentry_lease(dentry);
3944 case CEPH_MDS_LEASE_RENEW:
3945 if (di->lease_session == session &&
3946 di->lease_gen == session->s_cap_gen &&
3947 di->lease_renew_from &&
3948 di->lease_renew_after == 0) {
3949 unsigned long duration =
3950 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
3952 di->lease_seq = seq;
3953 di->time = di->lease_renew_from + duration;
3954 di->lease_renew_after = di->lease_renew_from +
3956 di->lease_renew_from = 0;
3960 spin_unlock(&dentry->d_lock);
3967 /* let's just reuse the same message */
3968 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3970 ceph_con_send(&session->s_con, msg);
3973 mutex_unlock(&session->s_mutex);
3974 /* avoid calling iput_final() in mds dispatch threads */
3975 ceph_async_iput(inode);
3979 pr_err("corrupt lease message\n");
3983 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3984 struct dentry *dentry, char action,
3987 struct ceph_msg *msg;
3988 struct ceph_mds_lease *lease;
3990 int len = sizeof(*lease) + sizeof(u32) + NAME_MAX;
3992 dout("lease_send_msg identry %p %s to mds%d\n",
3993 dentry, ceph_lease_op_name(action), session->s_mds);
3995 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
3998 lease = msg->front.iov_base;
3999 lease->action = action;
4000 lease->seq = cpu_to_le32(seq);
4002 spin_lock(&dentry->d_lock);
4003 dir = d_inode(dentry->d_parent);
4004 lease->ino = cpu_to_le64(ceph_ino(dir));
4005 lease->first = lease->last = cpu_to_le64(ceph_snap(dir));
4007 put_unaligned_le32(dentry->d_name.len, lease + 1);
4008 memcpy((void *)(lease + 1) + 4,
4009 dentry->d_name.name, dentry->d_name.len);
4010 spin_unlock(&dentry->d_lock);
4012 * if this is a preemptive lease RELEASE, no need to
4013 * flush request stream, since the actual request will
4016 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
4018 ceph_con_send(&session->s_con, msg);
4022 * lock unlock sessions, to wait ongoing session activities
4024 static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
4028 mutex_lock(&mdsc->mutex);
4029 for (i = 0; i < mdsc->max_sessions; i++) {
4030 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
4033 mutex_unlock(&mdsc->mutex);
4034 mutex_lock(&s->s_mutex);
4035 mutex_unlock(&s->s_mutex);
4036 ceph_put_mds_session(s);
4037 mutex_lock(&mdsc->mutex);
4039 mutex_unlock(&mdsc->mutex);
4042 static void maybe_recover_session(struct ceph_mds_client *mdsc)
4044 struct ceph_fs_client *fsc = mdsc->fsc;
4046 if (!ceph_test_mount_opt(fsc, CLEANRECOVER))
4049 if (READ_ONCE(fsc->mount_state) != CEPH_MOUNT_MOUNTED)
4052 if (!READ_ONCE(fsc->blacklisted))
4055 if (fsc->last_auto_reconnect &&
4056 time_before(jiffies, fsc->last_auto_reconnect + HZ * 60 * 30))
4059 pr_info("auto reconnect after blacklisted\n");
4060 fsc->last_auto_reconnect = jiffies;
4061 ceph_force_reconnect(fsc->sb);
4065 * delayed work -- periodically trim expired leases, renew caps with mds
4067 static void schedule_delayed(struct ceph_mds_client *mdsc)
4070 unsigned hz = round_jiffies_relative(HZ * delay);
4071 schedule_delayed_work(&mdsc->delayed_work, hz);
4074 static void delayed_work(struct work_struct *work)
4077 struct ceph_mds_client *mdsc =
4078 container_of(work, struct ceph_mds_client, delayed_work.work);
4082 dout("mdsc delayed_work\n");
4084 mutex_lock(&mdsc->mutex);
4085 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
4086 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
4087 mdsc->last_renew_caps);
4089 mdsc->last_renew_caps = jiffies;
4091 for (i = 0; i < mdsc->max_sessions; i++) {
4092 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
4095 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
4096 dout("resending session close request for mds%d\n",
4098 request_close_session(mdsc, s);
4099 ceph_put_mds_session(s);
4102 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
4103 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
4104 s->s_state = CEPH_MDS_SESSION_HUNG;
4105 pr_info("mds%d hung\n", s->s_mds);
4108 if (s->s_state == CEPH_MDS_SESSION_NEW ||
4109 s->s_state == CEPH_MDS_SESSION_RESTARTING ||
4110 s->s_state == CEPH_MDS_SESSION_REJECTED) {
4111 /* this mds is failed or recovering, just wait */
4112 ceph_put_mds_session(s);
4115 mutex_unlock(&mdsc->mutex);
4117 mutex_lock(&s->s_mutex);
4119 send_renew_caps(mdsc, s);
4121 ceph_con_keepalive(&s->s_con);
4122 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4123 s->s_state == CEPH_MDS_SESSION_HUNG)
4124 ceph_send_cap_releases(mdsc, s);
4125 mutex_unlock(&s->s_mutex);
4126 ceph_put_mds_session(s);
4128 mutex_lock(&mdsc->mutex);
4130 mutex_unlock(&mdsc->mutex);
4132 ceph_check_delayed_caps(mdsc);
4134 ceph_queue_cap_reclaim_work(mdsc);
4136 ceph_trim_snapid_map(mdsc);
4138 maybe_recover_session(mdsc);
4140 schedule_delayed(mdsc);
4143 int ceph_mdsc_init(struct ceph_fs_client *fsc)
4146 struct ceph_mds_client *mdsc;
4148 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
4152 mutex_init(&mdsc->mutex);
4153 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
4154 if (!mdsc->mdsmap) {
4160 init_completion(&mdsc->safe_umount_waiters);
4161 init_waitqueue_head(&mdsc->session_close_wq);
4162 INIT_LIST_HEAD(&mdsc->waiting_for_map);
4163 mdsc->sessions = NULL;
4164 atomic_set(&mdsc->num_sessions, 0);
4165 mdsc->max_sessions = 0;
4167 atomic64_set(&mdsc->quotarealms_count, 0);
4168 mdsc->quotarealms_inodes = RB_ROOT;
4169 mutex_init(&mdsc->quotarealms_inodes_mutex);
4170 mdsc->last_snap_seq = 0;
4171 init_rwsem(&mdsc->snap_rwsem);
4172 mdsc->snap_realms = RB_ROOT;
4173 INIT_LIST_HEAD(&mdsc->snap_empty);
4174 mdsc->num_snap_realms = 0;
4175 spin_lock_init(&mdsc->snap_empty_lock);
4177 mdsc->oldest_tid = 0;
4178 mdsc->request_tree = RB_ROOT;
4179 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
4180 mdsc->last_renew_caps = jiffies;
4181 INIT_LIST_HEAD(&mdsc->cap_delay_list);
4182 INIT_LIST_HEAD(&mdsc->cap_wait_list);
4183 spin_lock_init(&mdsc->cap_delay_lock);
4184 INIT_LIST_HEAD(&mdsc->snap_flush_list);
4185 spin_lock_init(&mdsc->snap_flush_lock);
4186 mdsc->last_cap_flush_tid = 1;
4187 INIT_LIST_HEAD(&mdsc->cap_flush_list);
4188 INIT_LIST_HEAD(&mdsc->cap_dirty);
4189 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
4190 mdsc->num_cap_flushing = 0;
4191 spin_lock_init(&mdsc->cap_dirty_lock);
4192 init_waitqueue_head(&mdsc->cap_flushing_wq);
4193 INIT_WORK(&mdsc->cap_reclaim_work, ceph_cap_reclaim_work);
4194 atomic_set(&mdsc->cap_reclaim_pending, 0);
4196 spin_lock_init(&mdsc->dentry_list_lock);
4197 INIT_LIST_HEAD(&mdsc->dentry_leases);
4198 INIT_LIST_HEAD(&mdsc->dentry_dir_leases);
4200 ceph_caps_init(mdsc);
4201 ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
4203 spin_lock_init(&mdsc->snapid_map_lock);
4204 mdsc->snapid_map_tree = RB_ROOT;
4205 INIT_LIST_HEAD(&mdsc->snapid_map_lru);
4207 init_rwsem(&mdsc->pool_perm_rwsem);
4208 mdsc->pool_perm_tree = RB_ROOT;
4210 strscpy(mdsc->nodename, utsname()->nodename,
4211 sizeof(mdsc->nodename));
4216 * Wait for safe replies on open mds requests. If we time out, drop
4217 * all requests from the tree to avoid dangling dentry refs.
4219 static void wait_requests(struct ceph_mds_client *mdsc)
4221 struct ceph_options *opts = mdsc->fsc->client->options;
4222 struct ceph_mds_request *req;
4224 mutex_lock(&mdsc->mutex);
4225 if (__get_oldest_req(mdsc)) {
4226 mutex_unlock(&mdsc->mutex);
4228 dout("wait_requests waiting for requests\n");
4229 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
4230 ceph_timeout_jiffies(opts->mount_timeout));
4232 /* tear down remaining requests */
4233 mutex_lock(&mdsc->mutex);
4234 while ((req = __get_oldest_req(mdsc))) {
4235 dout("wait_requests timed out on tid %llu\n",
4237 list_del_init(&req->r_wait);
4238 __unregister_request(mdsc, req);
4241 mutex_unlock(&mdsc->mutex);
4242 dout("wait_requests done\n");
4246 * called before mount is ro, and before dentries are torn down.
4247 * (hmm, does this still race with new lookups?)
4249 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
4251 dout("pre_umount\n");
4254 lock_unlock_sessions(mdsc);
4255 ceph_flush_dirty_caps(mdsc);
4256 wait_requests(mdsc);
4259 * wait for reply handlers to drop their request refs and
4260 * their inode/dcache refs
4264 ceph_cleanup_quotarealms_inodes(mdsc);
4268 * wait for all write mds requests to flush.
4270 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
4272 struct ceph_mds_request *req = NULL, *nextreq;
4275 mutex_lock(&mdsc->mutex);
4276 dout("wait_unsafe_requests want %lld\n", want_tid);
4278 req = __get_oldest_req(mdsc);
4279 while (req && req->r_tid <= want_tid) {
4280 /* find next request */
4281 n = rb_next(&req->r_node);
4283 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
4286 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
4287 (req->r_op & CEPH_MDS_OP_WRITE)) {
4289 ceph_mdsc_get_request(req);
4291 ceph_mdsc_get_request(nextreq);
4292 mutex_unlock(&mdsc->mutex);
4293 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
4294 req->r_tid, want_tid);
4295 wait_for_completion(&req->r_safe_completion);
4296 mutex_lock(&mdsc->mutex);
4297 ceph_mdsc_put_request(req);
4299 break; /* next dne before, so we're done! */
4300 if (RB_EMPTY_NODE(&nextreq->r_node)) {
4301 /* next request was removed from tree */
4302 ceph_mdsc_put_request(nextreq);
4305 ceph_mdsc_put_request(nextreq); /* won't go away */
4309 mutex_unlock(&mdsc->mutex);
4310 dout("wait_unsafe_requests done\n");
4313 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
4315 u64 want_tid, want_flush;
4317 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
4321 mutex_lock(&mdsc->mutex);
4322 want_tid = mdsc->last_tid;
4323 mutex_unlock(&mdsc->mutex);
4325 ceph_flush_dirty_caps(mdsc);
4326 spin_lock(&mdsc->cap_dirty_lock);
4327 want_flush = mdsc->last_cap_flush_tid;
4328 if (!list_empty(&mdsc->cap_flush_list)) {
4329 struct ceph_cap_flush *cf =
4330 list_last_entry(&mdsc->cap_flush_list,
4331 struct ceph_cap_flush, g_list);
4334 spin_unlock(&mdsc->cap_dirty_lock);
4336 dout("sync want tid %lld flush_seq %lld\n",
4337 want_tid, want_flush);
4339 wait_unsafe_requests(mdsc, want_tid);
4340 wait_caps_flush(mdsc, want_flush);
4344 * true if all sessions are closed, or we force unmount
4346 static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
4348 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
4350 return atomic_read(&mdsc->num_sessions) <= skipped;
4354 * called after sb is ro.
4356 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
4358 struct ceph_options *opts = mdsc->fsc->client->options;
4359 struct ceph_mds_session *session;
4363 dout("close_sessions\n");
4365 /* close sessions */
4366 mutex_lock(&mdsc->mutex);
4367 for (i = 0; i < mdsc->max_sessions; i++) {
4368 session = __ceph_lookup_mds_session(mdsc, i);
4371 mutex_unlock(&mdsc->mutex);
4372 mutex_lock(&session->s_mutex);
4373 if (__close_session(mdsc, session) <= 0)
4375 mutex_unlock(&session->s_mutex);
4376 ceph_put_mds_session(session);
4377 mutex_lock(&mdsc->mutex);
4379 mutex_unlock(&mdsc->mutex);
4381 dout("waiting for sessions to close\n");
4382 wait_event_timeout(mdsc->session_close_wq,
4383 done_closing_sessions(mdsc, skipped),
4384 ceph_timeout_jiffies(opts->mount_timeout));
4386 /* tear down remaining sessions */
4387 mutex_lock(&mdsc->mutex);
4388 for (i = 0; i < mdsc->max_sessions; i++) {
4389 if (mdsc->sessions[i]) {
4390 session = get_session(mdsc->sessions[i]);
4391 __unregister_session(mdsc, session);
4392 mutex_unlock(&mdsc->mutex);
4393 mutex_lock(&session->s_mutex);
4394 remove_session_caps(session);
4395 mutex_unlock(&session->s_mutex);
4396 ceph_put_mds_session(session);
4397 mutex_lock(&mdsc->mutex);
4400 WARN_ON(!list_empty(&mdsc->cap_delay_list));
4401 mutex_unlock(&mdsc->mutex);
4403 ceph_cleanup_snapid_map(mdsc);
4404 ceph_cleanup_empty_realms(mdsc);
4406 cancel_work_sync(&mdsc->cap_reclaim_work);
4407 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4412 void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
4414 struct ceph_mds_session *session;
4417 dout("force umount\n");
4419 mutex_lock(&mdsc->mutex);
4420 for (mds = 0; mds < mdsc->max_sessions; mds++) {
4421 session = __ceph_lookup_mds_session(mdsc, mds);
4425 if (session->s_state == CEPH_MDS_SESSION_REJECTED)
4426 __unregister_session(mdsc, session);
4427 __wake_requests(mdsc, &session->s_waiting);
4428 mutex_unlock(&mdsc->mutex);
4430 mutex_lock(&session->s_mutex);
4431 __close_session(mdsc, session);
4432 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
4433 cleanup_session_requests(mdsc, session);
4434 remove_session_caps(session);
4436 mutex_unlock(&session->s_mutex);
4437 ceph_put_mds_session(session);
4439 mutex_lock(&mdsc->mutex);
4440 kick_requests(mdsc, mds);
4442 __wake_requests(mdsc, &mdsc->waiting_for_map);
4443 mutex_unlock(&mdsc->mutex);
4446 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
4449 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4451 ceph_mdsmap_destroy(mdsc->mdsmap);
4452 kfree(mdsc->sessions);
4453 ceph_caps_finalize(mdsc);
4454 ceph_pool_perm_destroy(mdsc);
4457 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
4459 struct ceph_mds_client *mdsc = fsc->mdsc;
4460 dout("mdsc_destroy %p\n", mdsc);
4465 /* flush out any connection work with references to us */
4468 ceph_mdsc_stop(mdsc);
4472 dout("mdsc_destroy %p done\n", mdsc);
4475 void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
4477 struct ceph_fs_client *fsc = mdsc->fsc;
4478 const char *mds_namespace = fsc->mount_options->mds_namespace;
4479 void *p = msg->front.iov_base;
4480 void *end = p + msg->front.iov_len;
4484 u32 mount_fscid = (u32)-1;
4485 u8 struct_v, struct_cv;
4488 ceph_decode_need(&p, end, sizeof(u32), bad);
4489 epoch = ceph_decode_32(&p);
4491 dout("handle_fsmap epoch %u\n", epoch);
4493 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4494 struct_v = ceph_decode_8(&p);
4495 struct_cv = ceph_decode_8(&p);
4496 map_len = ceph_decode_32(&p);
4498 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
4499 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
4501 num_fs = ceph_decode_32(&p);
4502 while (num_fs-- > 0) {
4503 void *info_p, *info_end;
4508 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4509 info_v = ceph_decode_8(&p);
4510 info_cv = ceph_decode_8(&p);
4511 info_len = ceph_decode_32(&p);
4512 ceph_decode_need(&p, end, info_len, bad);
4514 info_end = p + info_len;
4517 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
4518 fscid = ceph_decode_32(&info_p);
4519 namelen = ceph_decode_32(&info_p);
4520 ceph_decode_need(&info_p, info_end, namelen, bad);
4522 if (mds_namespace &&
4523 strlen(mds_namespace) == namelen &&
4524 !strncmp(mds_namespace, (char *)info_p, namelen)) {
4525 mount_fscid = fscid;
4530 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
4531 if (mount_fscid != (u32)-1) {
4532 fsc->client->monc.fs_cluster_id = mount_fscid;
4533 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
4535 ceph_monc_renew_subs(&fsc->client->monc);
4543 pr_err("error decoding fsmap\n");
4545 mutex_lock(&mdsc->mutex);
4546 mdsc->mdsmap_err = err;
4547 __wake_requests(mdsc, &mdsc->waiting_for_map);
4548 mutex_unlock(&mdsc->mutex);
4552 * handle mds map update.
4554 void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
4558 void *p = msg->front.iov_base;
4559 void *end = p + msg->front.iov_len;
4560 struct ceph_mdsmap *newmap, *oldmap;
4561 struct ceph_fsid fsid;
4564 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
4565 ceph_decode_copy(&p, &fsid, sizeof(fsid));
4566 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
4568 epoch = ceph_decode_32(&p);
4569 maplen = ceph_decode_32(&p);
4570 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
4572 /* do we need it? */
4573 mutex_lock(&mdsc->mutex);
4574 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
4575 dout("handle_map epoch %u <= our %u\n",
4576 epoch, mdsc->mdsmap->m_epoch);
4577 mutex_unlock(&mdsc->mutex);
4581 newmap = ceph_mdsmap_decode(&p, end);
4582 if (IS_ERR(newmap)) {
4583 err = PTR_ERR(newmap);
4587 /* swap into place */
4589 oldmap = mdsc->mdsmap;
4590 mdsc->mdsmap = newmap;
4591 check_new_map(mdsc, newmap, oldmap);
4592 ceph_mdsmap_destroy(oldmap);
4594 mdsc->mdsmap = newmap; /* first mds map */
4596 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
4599 __wake_requests(mdsc, &mdsc->waiting_for_map);
4600 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
4601 mdsc->mdsmap->m_epoch);
4603 mutex_unlock(&mdsc->mutex);
4604 schedule_delayed(mdsc);
4608 mutex_unlock(&mdsc->mutex);
4610 pr_err("error decoding mdsmap %d\n", err);
4614 static struct ceph_connection *con_get(struct ceph_connection *con)
4616 struct ceph_mds_session *s = con->private;
4623 static void con_put(struct ceph_connection *con)
4625 struct ceph_mds_session *s = con->private;
4627 ceph_put_mds_session(s);
4631 * if the client is unresponsive for long enough, the mds will kill
4632 * the session entirely.
4634 static void peer_reset(struct ceph_connection *con)
4636 struct ceph_mds_session *s = con->private;
4637 struct ceph_mds_client *mdsc = s->s_mdsc;
4639 pr_warn("mds%d closed our session\n", s->s_mds);
4640 send_mds_reconnect(mdsc, s);
4643 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4645 struct ceph_mds_session *s = con->private;
4646 struct ceph_mds_client *mdsc = s->s_mdsc;
4647 int type = le16_to_cpu(msg->hdr.type);
4649 mutex_lock(&mdsc->mutex);
4650 if (__verify_registered_session(mdsc, s) < 0) {
4651 mutex_unlock(&mdsc->mutex);
4654 mutex_unlock(&mdsc->mutex);
4657 case CEPH_MSG_MDS_MAP:
4658 ceph_mdsc_handle_mdsmap(mdsc, msg);
4660 case CEPH_MSG_FS_MAP_USER:
4661 ceph_mdsc_handle_fsmap(mdsc, msg);
4663 case CEPH_MSG_CLIENT_SESSION:
4664 handle_session(s, msg);
4666 case CEPH_MSG_CLIENT_REPLY:
4667 handle_reply(s, msg);
4669 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
4670 handle_forward(mdsc, s, msg);
4672 case CEPH_MSG_CLIENT_CAPS:
4673 ceph_handle_caps(s, msg);
4675 case CEPH_MSG_CLIENT_SNAP:
4676 ceph_handle_snap(mdsc, s, msg);
4678 case CEPH_MSG_CLIENT_LEASE:
4679 handle_lease(mdsc, s, msg);
4681 case CEPH_MSG_CLIENT_QUOTA:
4682 ceph_handle_quota(mdsc, s, msg);
4686 pr_err("received unknown message type %d %s\n", type,
4687 ceph_msg_type_name(type));
4698 * Note: returned pointer is the address of a structure that's
4699 * managed separately. Caller must *not* attempt to free it.
4701 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
4702 int *proto, int force_new)
4704 struct ceph_mds_session *s = con->private;
4705 struct ceph_mds_client *mdsc = s->s_mdsc;
4706 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4707 struct ceph_auth_handshake *auth = &s->s_auth;
4709 if (force_new && auth->authorizer) {
4710 ceph_auth_destroy_authorizer(auth->authorizer);
4711 auth->authorizer = NULL;
4713 if (!auth->authorizer) {
4714 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4717 return ERR_PTR(ret);
4719 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4722 return ERR_PTR(ret);
4724 *proto = ac->protocol;
4729 static int add_authorizer_challenge(struct ceph_connection *con,
4730 void *challenge_buf, int challenge_buf_len)
4732 struct ceph_mds_session *s = con->private;
4733 struct ceph_mds_client *mdsc = s->s_mdsc;
4734 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4736 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
4737 challenge_buf, challenge_buf_len);
4740 static int verify_authorizer_reply(struct ceph_connection *con)
4742 struct ceph_mds_session *s = con->private;
4743 struct ceph_mds_client *mdsc = s->s_mdsc;
4744 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4746 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
4749 static int invalidate_authorizer(struct ceph_connection *con)
4751 struct ceph_mds_session *s = con->private;
4752 struct ceph_mds_client *mdsc = s->s_mdsc;
4753 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4755 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
4757 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
4760 static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
4761 struct ceph_msg_header *hdr, int *skip)
4763 struct ceph_msg *msg;
4764 int type = (int) le16_to_cpu(hdr->type);
4765 int front_len = (int) le32_to_cpu(hdr->front_len);
4771 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
4773 pr_err("unable to allocate msg type %d len %d\n",
4781 static int mds_sign_message(struct ceph_msg *msg)
4783 struct ceph_mds_session *s = msg->con->private;
4784 struct ceph_auth_handshake *auth = &s->s_auth;
4786 return ceph_auth_sign_message(auth, msg);
4789 static int mds_check_message_signature(struct ceph_msg *msg)
4791 struct ceph_mds_session *s = msg->con->private;
4792 struct ceph_auth_handshake *auth = &s->s_auth;
4794 return ceph_auth_check_message_signature(auth, msg);
4797 static const struct ceph_connection_operations mds_con_ops = {
4800 .dispatch = dispatch,
4801 .get_authorizer = get_authorizer,
4802 .add_authorizer_challenge = add_authorizer_challenge,
4803 .verify_authorizer_reply = verify_authorizer_reply,
4804 .invalidate_authorizer = invalidate_authorizer,
4805 .peer_reset = peer_reset,
4806 .alloc_msg = mds_alloc_msg,
4807 .sign_message = mds_sign_message,
4808 .check_message_signature = mds_check_message_signature,