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>
24 * A cluster of MDS (metadata server) daemons is responsible for
25 * managing the file system namespace (the directory hierarchy and
26 * inodes) and for coordinating shared access to storage. Metadata is
27 * partitioning hierarchically across a number of servers, and that
28 * partition varies over time as the cluster adjusts the distribution
29 * in order to balance load.
31 * The MDS client is primarily responsible to managing synchronous
32 * metadata requests for operations like open, unlink, and so forth.
33 * If there is a MDS failure, we find out about it when we (possibly
34 * request and) receive a new MDS map, and can resubmit affected
37 * For the most part, though, we take advantage of a lossless
38 * communications channel to the MDS, and do not need to worry about
39 * timing out or resubmitting requests.
41 * We maintain a stateful "session" with each MDS we interact with.
42 * Within each session, we sent periodic heartbeat messages to ensure
43 * any capabilities or leases we have been issues remain valid. If
44 * the session times out and goes stale, our leases and capabilities
45 * are no longer valid.
48 struct ceph_reconnect_state {
50 struct ceph_pagelist *pagelist;
54 static void __wake_requests(struct ceph_mds_client *mdsc,
55 struct list_head *head);
57 static const struct ceph_connection_operations mds_con_ops;
65 * parse individual inode info
67 static int parse_reply_info_in(void **p, void *end,
68 struct ceph_mds_reply_info_in *info,
74 *p += sizeof(struct ceph_mds_reply_inode) +
75 sizeof(*info->in->fragtree.splits) *
76 le32_to_cpu(info->in->fragtree.nsplits);
78 ceph_decode_32_safe(p, end, info->symlink_len, bad);
79 ceph_decode_need(p, end, info->symlink_len, bad);
81 *p += info->symlink_len;
83 if (features & CEPH_FEATURE_DIRLAYOUTHASH)
84 ceph_decode_copy_safe(p, end, &info->dir_layout,
85 sizeof(info->dir_layout), bad);
87 memset(&info->dir_layout, 0, sizeof(info->dir_layout));
89 ceph_decode_32_safe(p, end, info->xattr_len, bad);
90 ceph_decode_need(p, end, info->xattr_len, bad);
91 info->xattr_data = *p;
92 *p += info->xattr_len;
94 if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
95 ceph_decode_64_safe(p, end, info->inline_version, bad);
96 ceph_decode_32_safe(p, end, info->inline_len, bad);
97 ceph_decode_need(p, end, info->inline_len, bad);
98 info->inline_data = *p;
99 *p += info->inline_len;
101 info->inline_version = CEPH_INLINE_NONE;
103 if (features & CEPH_FEATURE_MDS_QUOTA) {
104 u8 struct_v, struct_compat;
108 * both struct_v and struct_compat are expected to be >= 1
110 ceph_decode_8_safe(p, end, struct_v, bad);
111 ceph_decode_8_safe(p, end, struct_compat, bad);
112 if (!struct_v || !struct_compat)
114 ceph_decode_32_safe(p, end, struct_len, bad);
115 ceph_decode_need(p, end, struct_len, bad);
116 ceph_decode_64_safe(p, end, info->max_bytes, bad);
117 ceph_decode_64_safe(p, end, info->max_files, bad);
123 info->pool_ns_len = 0;
124 info->pool_ns_data = NULL;
125 if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) {
126 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
127 if (info->pool_ns_len > 0) {
128 ceph_decode_need(p, end, info->pool_ns_len, bad);
129 info->pool_ns_data = *p;
130 *p += info->pool_ns_len;
140 * parse a normal reply, which may contain a (dir+)dentry and/or a
143 static int parse_reply_info_trace(void **p, void *end,
144 struct ceph_mds_reply_info_parsed *info,
149 if (info->head->is_dentry) {
150 err = parse_reply_info_in(p, end, &info->diri, features);
154 if (unlikely(*p + sizeof(*info->dirfrag) > end))
157 *p += sizeof(*info->dirfrag) +
158 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
159 if (unlikely(*p > end))
162 ceph_decode_32_safe(p, end, info->dname_len, bad);
163 ceph_decode_need(p, end, info->dname_len, bad);
165 *p += info->dname_len;
167 *p += sizeof(*info->dlease);
170 if (info->head->is_target) {
171 err = parse_reply_info_in(p, end, &info->targeti, features);
176 if (unlikely(*p != end))
183 pr_err("problem parsing mds trace %d\n", err);
188 * parse readdir results
190 static int parse_reply_info_dir(void **p, void *end,
191 struct ceph_mds_reply_info_parsed *info,
198 if (*p + sizeof(*info->dir_dir) > end)
200 *p += sizeof(*info->dir_dir) +
201 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
205 ceph_decode_need(p, end, sizeof(num) + 2, bad);
206 num = ceph_decode_32(p);
208 u16 flags = ceph_decode_16(p);
209 info->dir_end = !!(flags & CEPH_READDIR_FRAG_END);
210 info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE);
211 info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER);
212 info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH);
217 BUG_ON(!info->dir_entries);
218 if ((unsigned long)(info->dir_entries + num) >
219 (unsigned long)info->dir_entries + info->dir_buf_size) {
220 pr_err("dir contents are larger than expected\n");
227 struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i;
229 ceph_decode_need(p, end, sizeof(u32)*2, bad);
230 rde->name_len = ceph_decode_32(p);
231 ceph_decode_need(p, end, rde->name_len, bad);
234 dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name);
236 *p += sizeof(struct ceph_mds_reply_lease);
239 err = parse_reply_info_in(p, end, &rde->inode, features);
242 /* ceph_readdir_prepopulate() will update it */
256 pr_err("problem parsing dir contents %d\n", err);
261 * parse fcntl F_GETLK results
263 static int parse_reply_info_filelock(void **p, void *end,
264 struct ceph_mds_reply_info_parsed *info,
267 if (*p + sizeof(*info->filelock_reply) > end)
270 info->filelock_reply = *p;
271 *p += sizeof(*info->filelock_reply);
273 if (unlikely(*p != end))
282 * parse create results
284 static int parse_reply_info_create(void **p, void *end,
285 struct ceph_mds_reply_info_parsed *info,
288 if (features & CEPH_FEATURE_REPLY_CREATE_INODE) {
290 info->has_create_ino = false;
292 info->has_create_ino = true;
293 info->ino = ceph_decode_64(p);
297 if (unlikely(*p != end))
306 * parse extra results
308 static int parse_reply_info_extra(void **p, void *end,
309 struct ceph_mds_reply_info_parsed *info,
312 u32 op = le32_to_cpu(info->head->op);
314 if (op == CEPH_MDS_OP_GETFILELOCK)
315 return parse_reply_info_filelock(p, end, info, features);
316 else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
317 return parse_reply_info_dir(p, end, info, features);
318 else if (op == CEPH_MDS_OP_CREATE)
319 return parse_reply_info_create(p, end, info, features);
325 * parse entire mds reply
327 static int parse_reply_info(struct ceph_msg *msg,
328 struct ceph_mds_reply_info_parsed *info,
335 info->head = msg->front.iov_base;
336 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
337 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
340 ceph_decode_32_safe(&p, end, len, bad);
342 ceph_decode_need(&p, end, len, bad);
343 err = parse_reply_info_trace(&p, p+len, info, features);
349 ceph_decode_32_safe(&p, end, len, bad);
351 ceph_decode_need(&p, end, len, bad);
352 err = parse_reply_info_extra(&p, p+len, info, features);
358 ceph_decode_32_safe(&p, end, len, bad);
359 info->snapblob_len = len;
370 pr_err("mds parse_reply err %d\n", err);
374 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
376 if (!info->dir_entries)
378 free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
385 const char *ceph_session_state_name(int s)
388 case CEPH_MDS_SESSION_NEW: return "new";
389 case CEPH_MDS_SESSION_OPENING: return "opening";
390 case CEPH_MDS_SESSION_OPEN: return "open";
391 case CEPH_MDS_SESSION_HUNG: return "hung";
392 case CEPH_MDS_SESSION_CLOSING: return "closing";
393 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
394 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
395 case CEPH_MDS_SESSION_REJECTED: return "rejected";
396 default: return "???";
400 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
402 if (refcount_inc_not_zero(&s->s_ref)) {
403 dout("mdsc get_session %p %d -> %d\n", s,
404 refcount_read(&s->s_ref)-1, refcount_read(&s->s_ref));
407 dout("mdsc get_session %p 0 -- FAIL\n", s);
412 void ceph_put_mds_session(struct ceph_mds_session *s)
414 dout("mdsc put_session %p %d -> %d\n", s,
415 refcount_read(&s->s_ref), refcount_read(&s->s_ref)-1);
416 if (refcount_dec_and_test(&s->s_ref)) {
417 if (s->s_auth.authorizer)
418 ceph_auth_destroy_authorizer(s->s_auth.authorizer);
424 * called under mdsc->mutex
426 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
429 struct ceph_mds_session *session;
431 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
433 session = mdsc->sessions[mds];
434 dout("lookup_mds_session %p %d\n", session,
435 refcount_read(&session->s_ref));
436 get_session(session);
440 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
442 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
448 static int __verify_registered_session(struct ceph_mds_client *mdsc,
449 struct ceph_mds_session *s)
451 if (s->s_mds >= mdsc->max_sessions ||
452 mdsc->sessions[s->s_mds] != s)
458 * create+register a new session for given mds.
459 * called under mdsc->mutex.
461 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
464 struct ceph_mds_session *s;
466 if (mds >= mdsc->mdsmap->m_num_mds)
467 return ERR_PTR(-EINVAL);
469 s = kzalloc(sizeof(*s), GFP_NOFS);
471 return ERR_PTR(-ENOMEM);
473 if (mds >= mdsc->max_sessions) {
474 int newmax = 1 << get_count_order(mds + 1);
475 struct ceph_mds_session **sa;
477 dout("%s: realloc to %d\n", __func__, newmax);
478 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
481 if (mdsc->sessions) {
482 memcpy(sa, mdsc->sessions,
483 mdsc->max_sessions * sizeof(void *));
484 kfree(mdsc->sessions);
487 mdsc->max_sessions = newmax;
490 dout("%s: mds%d\n", __func__, mds);
493 s->s_state = CEPH_MDS_SESSION_NEW;
496 mutex_init(&s->s_mutex);
498 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
500 spin_lock_init(&s->s_gen_ttl_lock);
502 s->s_cap_ttl = jiffies - 1;
504 spin_lock_init(&s->s_cap_lock);
505 s->s_renew_requested = 0;
507 INIT_LIST_HEAD(&s->s_caps);
510 refcount_set(&s->s_ref, 1);
511 INIT_LIST_HEAD(&s->s_waiting);
512 INIT_LIST_HEAD(&s->s_unsafe);
513 s->s_num_cap_releases = 0;
514 s->s_cap_reconnect = 0;
515 s->s_cap_iterator = NULL;
516 INIT_LIST_HEAD(&s->s_cap_releases);
517 INIT_LIST_HEAD(&s->s_cap_flushing);
519 mdsc->sessions[mds] = s;
520 atomic_inc(&mdsc->num_sessions);
521 refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
523 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
524 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
530 return ERR_PTR(-ENOMEM);
534 * called under mdsc->mutex
536 static void __unregister_session(struct ceph_mds_client *mdsc,
537 struct ceph_mds_session *s)
539 dout("__unregister_session mds%d %p\n", s->s_mds, s);
540 BUG_ON(mdsc->sessions[s->s_mds] != s);
541 mdsc->sessions[s->s_mds] = NULL;
542 ceph_con_close(&s->s_con);
543 ceph_put_mds_session(s);
544 atomic_dec(&mdsc->num_sessions);
548 * drop session refs in request.
550 * should be last request ref, or hold mdsc->mutex
552 static void put_request_session(struct ceph_mds_request *req)
554 if (req->r_session) {
555 ceph_put_mds_session(req->r_session);
556 req->r_session = NULL;
560 void ceph_mdsc_release_request(struct kref *kref)
562 struct ceph_mds_request *req = container_of(kref,
563 struct ceph_mds_request,
565 destroy_reply_info(&req->r_reply_info);
567 ceph_msg_put(req->r_request);
569 ceph_msg_put(req->r_reply);
571 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
575 ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
576 iput(req->r_target_inode);
579 if (req->r_old_dentry)
580 dput(req->r_old_dentry);
581 if (req->r_old_dentry_dir) {
583 * track (and drop pins for) r_old_dentry_dir
584 * separately, since r_old_dentry's d_parent may have
585 * changed between the dir mutex being dropped and
586 * this request being freed.
588 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
590 iput(req->r_old_dentry_dir);
595 ceph_pagelist_release(req->r_pagelist);
596 put_request_session(req);
597 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
601 DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
604 * lookup session, bump ref if found.
606 * called under mdsc->mutex.
608 static struct ceph_mds_request *
609 lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
611 struct ceph_mds_request *req;
613 req = lookup_request(&mdsc->request_tree, tid);
615 ceph_mdsc_get_request(req);
621 * Register an in-flight request, and assign a tid. Link to directory
622 * are modifying (if any).
624 * Called under mdsc->mutex.
626 static void __register_request(struct ceph_mds_client *mdsc,
627 struct ceph_mds_request *req,
632 req->r_tid = ++mdsc->last_tid;
633 if (req->r_num_caps) {
634 ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
637 pr_err("__register_request %p "
638 "failed to reserve caps: %d\n", req, ret);
639 /* set req->r_err to fail early from __do_request */
644 dout("__register_request %p tid %lld\n", req, req->r_tid);
645 ceph_mdsc_get_request(req);
646 insert_request(&mdsc->request_tree, req);
648 req->r_uid = current_fsuid();
649 req->r_gid = current_fsgid();
651 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
652 mdsc->oldest_tid = req->r_tid;
656 req->r_unsafe_dir = dir;
660 static void __unregister_request(struct ceph_mds_client *mdsc,
661 struct ceph_mds_request *req)
663 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
665 /* Never leave an unregistered request on an unsafe list! */
666 list_del_init(&req->r_unsafe_item);
668 if (req->r_tid == mdsc->oldest_tid) {
669 struct rb_node *p = rb_next(&req->r_node);
670 mdsc->oldest_tid = 0;
672 struct ceph_mds_request *next_req =
673 rb_entry(p, struct ceph_mds_request, r_node);
674 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
675 mdsc->oldest_tid = next_req->r_tid;
682 erase_request(&mdsc->request_tree, req);
684 if (req->r_unsafe_dir &&
685 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
686 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
687 spin_lock(&ci->i_unsafe_lock);
688 list_del_init(&req->r_unsafe_dir_item);
689 spin_unlock(&ci->i_unsafe_lock);
691 if (req->r_target_inode &&
692 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
693 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
694 spin_lock(&ci->i_unsafe_lock);
695 list_del_init(&req->r_unsafe_target_item);
696 spin_unlock(&ci->i_unsafe_lock);
699 if (req->r_unsafe_dir) {
700 iput(req->r_unsafe_dir);
701 req->r_unsafe_dir = NULL;
704 complete_all(&req->r_safe_completion);
706 ceph_mdsc_put_request(req);
710 * Walk back up the dentry tree until we hit a dentry representing a
711 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
712 * when calling this) to ensure that the objects won't disappear while we're
713 * working with them. Once we hit a candidate dentry, we attempt to take a
714 * reference to it, and return that as the result.
716 static struct inode *get_nonsnap_parent(struct dentry *dentry)
718 struct inode *inode = NULL;
720 while (dentry && !IS_ROOT(dentry)) {
721 inode = d_inode_rcu(dentry);
722 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
724 dentry = dentry->d_parent;
727 inode = igrab(inode);
732 * Choose mds to send request to next. If there is a hint set in the
733 * request (e.g., due to a prior forward hint from the mds), use that.
734 * Otherwise, consult frag tree and/or caps to identify the
735 * appropriate mds. If all else fails, choose randomly.
737 * Called under mdsc->mutex.
739 static int __choose_mds(struct ceph_mds_client *mdsc,
740 struct ceph_mds_request *req)
743 struct ceph_inode_info *ci;
744 struct ceph_cap *cap;
745 int mode = req->r_direct_mode;
747 u32 hash = req->r_direct_hash;
748 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
751 * is there a specific mds we should try? ignore hint if we have
752 * no session and the mds is not up (active or recovering).
754 if (req->r_resend_mds >= 0 &&
755 (__have_session(mdsc, req->r_resend_mds) ||
756 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
757 dout("choose_mds using resend_mds mds%d\n",
759 return req->r_resend_mds;
762 if (mode == USE_RANDOM_MDS)
767 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
768 inode = req->r_inode;
771 /* req->r_dentry is non-null for LSSNAP request */
773 inode = get_nonsnap_parent(req->r_dentry);
775 dout("__choose_mds using snapdir's parent %p\n", inode);
777 } else if (req->r_dentry) {
778 /* ignore race with rename; old or new d_parent is okay */
779 struct dentry *parent;
783 parent = req->r_dentry->d_parent;
784 dir = req->r_parent ? : d_inode_rcu(parent);
786 if (!dir || dir->i_sb != mdsc->fsc->sb) {
787 /* not this fs or parent went negative */
788 inode = d_inode(req->r_dentry);
791 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
792 /* direct snapped/virtual snapdir requests
793 * based on parent dir inode */
794 inode = get_nonsnap_parent(parent);
795 dout("__choose_mds using nonsnap parent %p\n", inode);
798 inode = d_inode(req->r_dentry);
799 if (!inode || mode == USE_AUTH_MDS) {
802 hash = ceph_dentry_hash(dir, req->r_dentry);
811 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
815 ci = ceph_inode(inode);
817 if (is_hash && S_ISDIR(inode->i_mode)) {
818 struct ceph_inode_frag frag;
821 ceph_choose_frag(ci, hash, &frag, &found);
823 if (mode == USE_ANY_MDS && frag.ndist > 0) {
826 /* choose a random replica */
827 get_random_bytes(&r, 1);
830 dout("choose_mds %p %llx.%llx "
831 "frag %u mds%d (%d/%d)\n",
832 inode, ceph_vinop(inode),
835 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
836 CEPH_MDS_STATE_ACTIVE)
840 /* since this file/dir wasn't known to be
841 * replicated, then we want to look for the
842 * authoritative mds. */
845 /* choose auth mds */
847 dout("choose_mds %p %llx.%llx "
848 "frag %u mds%d (auth)\n",
849 inode, ceph_vinop(inode), frag.frag, mds);
850 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
851 CEPH_MDS_STATE_ACTIVE)
857 spin_lock(&ci->i_ceph_lock);
859 if (mode == USE_AUTH_MDS)
860 cap = ci->i_auth_cap;
861 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
862 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
864 spin_unlock(&ci->i_ceph_lock);
868 mds = cap->session->s_mds;
869 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
870 inode, ceph_vinop(inode), mds,
871 cap == ci->i_auth_cap ? "auth " : "", cap);
872 spin_unlock(&ci->i_ceph_lock);
878 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
879 dout("choose_mds chose random mds%d\n", mds);
887 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
889 struct ceph_msg *msg;
890 struct ceph_mds_session_head *h;
892 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
895 pr_err("create_session_msg ENOMEM creating msg\n");
898 h = msg->front.iov_base;
899 h->op = cpu_to_le32(op);
900 h->seq = cpu_to_le64(seq);
906 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
907 * to include additional client metadata fields.
909 static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
911 struct ceph_msg *msg;
912 struct ceph_mds_session_head *h;
914 int metadata_bytes = 0;
915 int metadata_key_count = 0;
916 struct ceph_options *opt = mdsc->fsc->client->options;
917 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
920 const char* metadata[][2] = {
921 {"hostname", mdsc->nodename},
922 {"kernel_version", init_utsname()->release},
923 {"entity_id", opt->name ? : ""},
924 {"root", fsopt->server_path ? : "/"},
928 /* Calculate serialized length of metadata */
929 metadata_bytes = 4; /* map length */
930 for (i = 0; metadata[i][0]; ++i) {
931 metadata_bytes += 8 + strlen(metadata[i][0]) +
932 strlen(metadata[i][1]);
933 metadata_key_count++;
936 /* Allocate the message */
937 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + metadata_bytes,
940 pr_err("create_session_msg ENOMEM creating msg\n");
943 h = msg->front.iov_base;
944 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
945 h->seq = cpu_to_le64(seq);
948 * Serialize client metadata into waiting buffer space, using
949 * the format that userspace expects for map<string, string>
951 * ClientSession messages with metadata are v2
953 msg->hdr.version = cpu_to_le16(2);
954 msg->hdr.compat_version = cpu_to_le16(1);
956 /* The write pointer, following the session_head structure */
957 p = msg->front.iov_base + sizeof(*h);
959 /* Number of entries in the map */
960 ceph_encode_32(&p, metadata_key_count);
962 /* Two length-prefixed strings for each entry in the map */
963 for (i = 0; metadata[i][0]; ++i) {
964 size_t const key_len = strlen(metadata[i][0]);
965 size_t const val_len = strlen(metadata[i][1]);
967 ceph_encode_32(&p, key_len);
968 memcpy(p, metadata[i][0], key_len);
970 ceph_encode_32(&p, val_len);
971 memcpy(p, metadata[i][1], val_len);
979 * send session open request.
981 * called under mdsc->mutex
983 static int __open_session(struct ceph_mds_client *mdsc,
984 struct ceph_mds_session *session)
986 struct ceph_msg *msg;
988 int mds = session->s_mds;
990 /* wait for mds to go active? */
991 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
992 dout("open_session to mds%d (%s)\n", mds,
993 ceph_mds_state_name(mstate));
994 session->s_state = CEPH_MDS_SESSION_OPENING;
995 session->s_renew_requested = jiffies;
997 /* send connect message */
998 msg = create_session_open_msg(mdsc, session->s_seq);
1001 ceph_con_send(&session->s_con, msg);
1006 * open sessions for any export targets for the given mds
1008 * called under mdsc->mutex
1010 static struct ceph_mds_session *
1011 __open_export_target_session(struct ceph_mds_client *mdsc, int target)
1013 struct ceph_mds_session *session;
1015 session = __ceph_lookup_mds_session(mdsc, target);
1017 session = register_session(mdsc, target);
1018 if (IS_ERR(session))
1021 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1022 session->s_state == CEPH_MDS_SESSION_CLOSING)
1023 __open_session(mdsc, session);
1028 struct ceph_mds_session *
1029 ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1031 struct ceph_mds_session *session;
1033 dout("open_export_target_session to mds%d\n", target);
1035 mutex_lock(&mdsc->mutex);
1036 session = __open_export_target_session(mdsc, target);
1037 mutex_unlock(&mdsc->mutex);
1042 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1043 struct ceph_mds_session *session)
1045 struct ceph_mds_info *mi;
1046 struct ceph_mds_session *ts;
1047 int i, mds = session->s_mds;
1049 if (mds >= mdsc->mdsmap->m_num_mds)
1052 mi = &mdsc->mdsmap->m_info[mds];
1053 dout("open_export_target_sessions for mds%d (%d targets)\n",
1054 session->s_mds, mi->num_export_targets);
1056 for (i = 0; i < mi->num_export_targets; i++) {
1057 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1059 ceph_put_mds_session(ts);
1063 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1064 struct ceph_mds_session *session)
1066 mutex_lock(&mdsc->mutex);
1067 __open_export_target_sessions(mdsc, session);
1068 mutex_unlock(&mdsc->mutex);
1075 static void detach_cap_releases(struct ceph_mds_session *session,
1076 struct list_head *target)
1078 lockdep_assert_held(&session->s_cap_lock);
1080 list_splice_init(&session->s_cap_releases, target);
1081 session->s_num_cap_releases = 0;
1082 dout("dispose_cap_releases mds%d\n", session->s_mds);
1085 static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1086 struct list_head *dispose)
1088 while (!list_empty(dispose)) {
1089 struct ceph_cap *cap;
1090 /* zero out the in-progress message */
1091 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
1092 list_del(&cap->session_caps);
1093 ceph_put_cap(mdsc, cap);
1097 static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1098 struct ceph_mds_session *session)
1100 struct ceph_mds_request *req;
1103 dout("cleanup_session_requests mds%d\n", session->s_mds);
1104 mutex_lock(&mdsc->mutex);
1105 while (!list_empty(&session->s_unsafe)) {
1106 req = list_first_entry(&session->s_unsafe,
1107 struct ceph_mds_request, r_unsafe_item);
1108 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1110 __unregister_request(mdsc, req);
1112 /* zero r_attempts, so kick_requests() will re-send requests */
1113 p = rb_first(&mdsc->request_tree);
1115 req = rb_entry(p, struct ceph_mds_request, r_node);
1117 if (req->r_session &&
1118 req->r_session->s_mds == session->s_mds)
1119 req->r_attempts = 0;
1121 mutex_unlock(&mdsc->mutex);
1125 * Helper to safely iterate over all caps associated with a session, with
1126 * special care taken to handle a racing __ceph_remove_cap().
1128 * Caller must hold session s_mutex.
1130 static int iterate_session_caps(struct ceph_mds_session *session,
1131 int (*cb)(struct inode *, struct ceph_cap *,
1134 struct list_head *p;
1135 struct ceph_cap *cap;
1136 struct inode *inode, *last_inode = NULL;
1137 struct ceph_cap *old_cap = NULL;
1140 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1141 spin_lock(&session->s_cap_lock);
1142 p = session->s_caps.next;
1143 while (p != &session->s_caps) {
1144 cap = list_entry(p, struct ceph_cap, session_caps);
1145 inode = igrab(&cap->ci->vfs_inode);
1150 session->s_cap_iterator = cap;
1151 spin_unlock(&session->s_cap_lock);
1158 ceph_put_cap(session->s_mdsc, old_cap);
1162 ret = cb(inode, cap, arg);
1165 spin_lock(&session->s_cap_lock);
1168 dout("iterate_session_caps finishing cap %p removal\n",
1170 BUG_ON(cap->session != session);
1171 cap->session = NULL;
1172 list_del_init(&cap->session_caps);
1173 session->s_nr_caps--;
1174 if (cap->queue_release) {
1175 list_add_tail(&cap->session_caps,
1176 &session->s_cap_releases);
1177 session->s_num_cap_releases++;
1179 old_cap = cap; /* put_cap it w/o locks held */
1187 session->s_cap_iterator = NULL;
1188 spin_unlock(&session->s_cap_lock);
1192 ceph_put_cap(session->s_mdsc, old_cap);
1197 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
1200 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
1201 struct ceph_inode_info *ci = ceph_inode(inode);
1202 LIST_HEAD(to_remove);
1204 bool invalidate = false;
1206 dout("removing cap %p, ci is %p, inode is %p\n",
1207 cap, ci, &ci->vfs_inode);
1208 spin_lock(&ci->i_ceph_lock);
1209 __ceph_remove_cap(cap, false);
1210 if (!ci->i_auth_cap) {
1211 struct ceph_cap_flush *cf;
1212 struct ceph_mds_client *mdsc = fsc->mdsc;
1214 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1216 if (ci->i_wrbuffer_ref > 0 &&
1217 READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
1220 while (!list_empty(&ci->i_cap_flush_list)) {
1221 cf = list_first_entry(&ci->i_cap_flush_list,
1222 struct ceph_cap_flush, i_list);
1223 list_move(&cf->i_list, &to_remove);
1226 spin_lock(&mdsc->cap_dirty_lock);
1228 list_for_each_entry(cf, &to_remove, i_list)
1229 list_del(&cf->g_list);
1231 if (!list_empty(&ci->i_dirty_item)) {
1232 pr_warn_ratelimited(
1233 " dropping dirty %s state for %p %lld\n",
1234 ceph_cap_string(ci->i_dirty_caps),
1235 inode, ceph_ino(inode));
1236 ci->i_dirty_caps = 0;
1237 list_del_init(&ci->i_dirty_item);
1240 if (!list_empty(&ci->i_flushing_item)) {
1241 pr_warn_ratelimited(
1242 " dropping dirty+flushing %s state for %p %lld\n",
1243 ceph_cap_string(ci->i_flushing_caps),
1244 inode, ceph_ino(inode));
1245 ci->i_flushing_caps = 0;
1246 list_del_init(&ci->i_flushing_item);
1247 mdsc->num_cap_flushing--;
1250 spin_unlock(&mdsc->cap_dirty_lock);
1252 if (atomic_read(&ci->i_filelock_ref) > 0) {
1253 /* make further file lock syscall return -EIO */
1254 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1255 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1256 inode, ceph_ino(inode));
1259 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
1260 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
1261 ci->i_prealloc_cap_flush = NULL;
1264 spin_unlock(&ci->i_ceph_lock);
1265 while (!list_empty(&to_remove)) {
1266 struct ceph_cap_flush *cf;
1267 cf = list_first_entry(&to_remove,
1268 struct ceph_cap_flush, i_list);
1269 list_del(&cf->i_list);
1270 ceph_free_cap_flush(cf);
1273 wake_up_all(&ci->i_cap_wq);
1275 ceph_queue_invalidate(inode);
1282 * caller must hold session s_mutex
1284 static void remove_session_caps(struct ceph_mds_session *session)
1286 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1287 struct super_block *sb = fsc->sb;
1290 dout("remove_session_caps on %p\n", session);
1291 iterate_session_caps(session, remove_session_caps_cb, fsc);
1293 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1295 spin_lock(&session->s_cap_lock);
1296 if (session->s_nr_caps > 0) {
1297 struct inode *inode;
1298 struct ceph_cap *cap, *prev = NULL;
1299 struct ceph_vino vino;
1301 * iterate_session_caps() skips inodes that are being
1302 * deleted, we need to wait until deletions are complete.
1303 * __wait_on_freeing_inode() is designed for the job,
1304 * but it is not exported, so use lookup inode function
1307 while (!list_empty(&session->s_caps)) {
1308 cap = list_entry(session->s_caps.next,
1309 struct ceph_cap, session_caps);
1313 vino = cap->ci->i_vino;
1314 spin_unlock(&session->s_cap_lock);
1316 inode = ceph_find_inode(sb, vino);
1319 spin_lock(&session->s_cap_lock);
1323 // drop cap expires and unlock s_cap_lock
1324 detach_cap_releases(session, &dispose);
1326 BUG_ON(session->s_nr_caps > 0);
1327 BUG_ON(!list_empty(&session->s_cap_flushing));
1328 spin_unlock(&session->s_cap_lock);
1329 dispose_cap_releases(session->s_mdsc, &dispose);
1333 * wake up any threads waiting on this session's caps. if the cap is
1334 * old (didn't get renewed on the client reconnect), remove it now.
1336 * caller must hold s_mutex.
1338 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1341 struct ceph_inode_info *ci = ceph_inode(inode);
1344 spin_lock(&ci->i_ceph_lock);
1345 ci->i_wanted_max_size = 0;
1346 ci->i_requested_max_size = 0;
1347 spin_unlock(&ci->i_ceph_lock);
1349 wake_up_all(&ci->i_cap_wq);
1353 static void wake_up_session_caps(struct ceph_mds_session *session,
1356 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1357 iterate_session_caps(session, wake_up_session_cb,
1358 (void *)(unsigned long)reconnect);
1362 * Send periodic message to MDS renewing all currently held caps. The
1363 * ack will reset the expiration for all caps from this session.
1365 * caller holds s_mutex
1367 static int send_renew_caps(struct ceph_mds_client *mdsc,
1368 struct ceph_mds_session *session)
1370 struct ceph_msg *msg;
1373 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1374 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1375 pr_info("mds%d caps stale\n", session->s_mds);
1376 session->s_renew_requested = jiffies;
1378 /* do not try to renew caps until a recovering mds has reconnected
1379 * with its clients. */
1380 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1381 if (state < CEPH_MDS_STATE_RECONNECT) {
1382 dout("send_renew_caps ignoring mds%d (%s)\n",
1383 session->s_mds, ceph_mds_state_name(state));
1387 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1388 ceph_mds_state_name(state));
1389 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1390 ++session->s_renew_seq);
1393 ceph_con_send(&session->s_con, msg);
1397 static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1398 struct ceph_mds_session *session, u64 seq)
1400 struct ceph_msg *msg;
1402 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1403 session->s_mds, ceph_session_state_name(session->s_state), seq);
1404 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1407 ceph_con_send(&session->s_con, msg);
1413 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1415 * Called under session->s_mutex
1417 static void renewed_caps(struct ceph_mds_client *mdsc,
1418 struct ceph_mds_session *session, int is_renew)
1423 spin_lock(&session->s_cap_lock);
1424 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
1426 session->s_cap_ttl = session->s_renew_requested +
1427 mdsc->mdsmap->m_session_timeout*HZ;
1430 if (time_before(jiffies, session->s_cap_ttl)) {
1431 pr_info("mds%d caps renewed\n", session->s_mds);
1434 pr_info("mds%d caps still stale\n", session->s_mds);
1437 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1438 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1439 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1440 spin_unlock(&session->s_cap_lock);
1443 wake_up_session_caps(session, 0);
1447 * send a session close request
1449 static int request_close_session(struct ceph_mds_client *mdsc,
1450 struct ceph_mds_session *session)
1452 struct ceph_msg *msg;
1454 dout("request_close_session mds%d state %s seq %lld\n",
1455 session->s_mds, ceph_session_state_name(session->s_state),
1457 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1460 ceph_con_send(&session->s_con, msg);
1465 * Called with s_mutex held.
1467 static int __close_session(struct ceph_mds_client *mdsc,
1468 struct ceph_mds_session *session)
1470 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1472 session->s_state = CEPH_MDS_SESSION_CLOSING;
1473 return request_close_session(mdsc, session);
1476 static bool drop_negative_children(struct dentry *dentry)
1478 struct dentry *child;
1479 bool all_negative = true;
1481 if (!d_is_dir(dentry))
1484 spin_lock(&dentry->d_lock);
1485 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1486 if (d_really_is_positive(child)) {
1487 all_negative = false;
1491 spin_unlock(&dentry->d_lock);
1494 shrink_dcache_parent(dentry);
1496 return all_negative;
1500 * Trim old(er) caps.
1502 * Because we can't cache an inode without one or more caps, we do
1503 * this indirectly: if a cap is unused, we prune its aliases, at which
1504 * point the inode will hopefully get dropped to.
1506 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1507 * memory pressure from the MDS, though, so it needn't be perfect.
1509 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1511 struct ceph_mds_session *session = arg;
1512 struct ceph_inode_info *ci = ceph_inode(inode);
1513 int used, wanted, oissued, mine;
1515 if (session->s_trim_caps <= 0)
1518 spin_lock(&ci->i_ceph_lock);
1519 mine = cap->issued | cap->implemented;
1520 used = __ceph_caps_used(ci);
1521 wanted = __ceph_caps_file_wanted(ci);
1522 oissued = __ceph_caps_issued_other(ci, cap);
1524 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1525 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1526 ceph_cap_string(used), ceph_cap_string(wanted));
1527 if (cap == ci->i_auth_cap) {
1528 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1529 !list_empty(&ci->i_cap_snaps))
1531 if ((used | wanted) & CEPH_CAP_ANY_WR)
1533 /* Note: it's possible that i_filelock_ref becomes non-zero
1534 * after dropping auth caps. It doesn't hurt because reply
1535 * of lock mds request will re-add auth caps. */
1536 if (atomic_read(&ci->i_filelock_ref) > 0)
1539 /* The inode has cached pages, but it's no longer used.
1540 * we can safely drop it */
1541 if (wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
1542 !(oissued & CEPH_CAP_FILE_CACHE)) {
1546 if ((used | wanted) & ~oissued & mine)
1547 goto out; /* we need these caps */
1550 /* we aren't the only cap.. just remove us */
1551 __ceph_remove_cap(cap, true);
1552 session->s_trim_caps--;
1554 struct dentry *dentry;
1555 /* try dropping referring dentries */
1556 spin_unlock(&ci->i_ceph_lock);
1557 dentry = d_find_any_alias(inode);
1558 if (dentry && drop_negative_children(dentry)) {
1561 d_prune_aliases(inode);
1562 count = atomic_read(&inode->i_count);
1564 session->s_trim_caps--;
1565 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1574 spin_unlock(&ci->i_ceph_lock);
1579 * Trim session cap count down to some max number.
1581 int ceph_trim_caps(struct ceph_mds_client *mdsc,
1582 struct ceph_mds_session *session,
1585 int trim_caps = session->s_nr_caps - max_caps;
1587 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1588 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1589 if (trim_caps > 0) {
1590 session->s_trim_caps = trim_caps;
1591 iterate_session_caps(session, trim_caps_cb, session);
1592 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1593 session->s_mds, session->s_nr_caps, max_caps,
1594 trim_caps - session->s_trim_caps);
1595 session->s_trim_caps = 0;
1598 ceph_send_cap_releases(mdsc, session);
1602 static int check_caps_flush(struct ceph_mds_client *mdsc,
1607 spin_lock(&mdsc->cap_dirty_lock);
1608 if (!list_empty(&mdsc->cap_flush_list)) {
1609 struct ceph_cap_flush *cf =
1610 list_first_entry(&mdsc->cap_flush_list,
1611 struct ceph_cap_flush, g_list);
1612 if (cf->tid <= want_flush_tid) {
1613 dout("check_caps_flush still flushing tid "
1614 "%llu <= %llu\n", cf->tid, want_flush_tid);
1618 spin_unlock(&mdsc->cap_dirty_lock);
1623 * flush all dirty inode data to disk.
1625 * returns true if we've flushed through want_flush_tid
1627 static void wait_caps_flush(struct ceph_mds_client *mdsc,
1630 dout("check_caps_flush want %llu\n", want_flush_tid);
1632 wait_event(mdsc->cap_flushing_wq,
1633 check_caps_flush(mdsc, want_flush_tid));
1635 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
1639 * called under s_mutex
1641 void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1642 struct ceph_mds_session *session)
1644 struct ceph_msg *msg = NULL;
1645 struct ceph_mds_cap_release *head;
1646 struct ceph_mds_cap_item *item;
1647 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
1648 struct ceph_cap *cap;
1649 LIST_HEAD(tmp_list);
1650 int num_cap_releases;
1651 __le32 barrier, *cap_barrier;
1653 down_read(&osdc->lock);
1654 barrier = cpu_to_le32(osdc->epoch_barrier);
1655 up_read(&osdc->lock);
1657 spin_lock(&session->s_cap_lock);
1659 list_splice_init(&session->s_cap_releases, &tmp_list);
1660 num_cap_releases = session->s_num_cap_releases;
1661 session->s_num_cap_releases = 0;
1662 spin_unlock(&session->s_cap_lock);
1664 while (!list_empty(&tmp_list)) {
1666 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
1667 PAGE_SIZE, GFP_NOFS, false);
1670 head = msg->front.iov_base;
1671 head->num = cpu_to_le32(0);
1672 msg->front.iov_len = sizeof(*head);
1674 msg->hdr.version = cpu_to_le16(2);
1675 msg->hdr.compat_version = cpu_to_le16(1);
1678 cap = list_first_entry(&tmp_list, struct ceph_cap,
1680 list_del(&cap->session_caps);
1683 head = msg->front.iov_base;
1684 le32_add_cpu(&head->num, 1);
1685 item = msg->front.iov_base + msg->front.iov_len;
1686 item->ino = cpu_to_le64(cap->cap_ino);
1687 item->cap_id = cpu_to_le64(cap->cap_id);
1688 item->migrate_seq = cpu_to_le32(cap->mseq);
1689 item->seq = cpu_to_le32(cap->issue_seq);
1690 msg->front.iov_len += sizeof(*item);
1692 ceph_put_cap(mdsc, cap);
1694 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
1695 // Append cap_barrier field
1696 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1697 *cap_barrier = barrier;
1698 msg->front.iov_len += sizeof(*cap_barrier);
1700 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1701 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1702 ceph_con_send(&session->s_con, msg);
1707 BUG_ON(num_cap_releases != 0);
1709 spin_lock(&session->s_cap_lock);
1710 if (!list_empty(&session->s_cap_releases))
1712 spin_unlock(&session->s_cap_lock);
1715 // Append cap_barrier field
1716 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1717 *cap_barrier = barrier;
1718 msg->front.iov_len += sizeof(*cap_barrier);
1720 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1721 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1722 ceph_con_send(&session->s_con, msg);
1726 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1728 spin_lock(&session->s_cap_lock);
1729 list_splice(&tmp_list, &session->s_cap_releases);
1730 session->s_num_cap_releases += num_cap_releases;
1731 spin_unlock(&session->s_cap_lock);
1738 int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1741 struct ceph_inode_info *ci = ceph_inode(dir);
1742 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1743 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
1744 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
1745 int order, num_entries;
1747 spin_lock(&ci->i_ceph_lock);
1748 num_entries = ci->i_files + ci->i_subdirs;
1749 spin_unlock(&ci->i_ceph_lock);
1750 num_entries = max(num_entries, 1);
1751 num_entries = min(num_entries, opt->max_readdir);
1753 order = get_order(size * num_entries);
1754 while (order >= 0) {
1755 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
1758 if (rinfo->dir_entries)
1762 if (!rinfo->dir_entries)
1765 num_entries = (PAGE_SIZE << order) / size;
1766 num_entries = min(num_entries, opt->max_readdir);
1768 rinfo->dir_buf_size = PAGE_SIZE << order;
1769 req->r_num_caps = num_entries + 1;
1770 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1771 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1776 * Create an mds request.
1778 struct ceph_mds_request *
1779 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1781 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1782 struct timespec64 ts;
1785 return ERR_PTR(-ENOMEM);
1787 mutex_init(&req->r_fill_mutex);
1789 req->r_started = jiffies;
1790 req->r_resend_mds = -1;
1791 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1792 INIT_LIST_HEAD(&req->r_unsafe_target_item);
1794 kref_init(&req->r_kref);
1795 RB_CLEAR_NODE(&req->r_node);
1796 INIT_LIST_HEAD(&req->r_wait);
1797 init_completion(&req->r_completion);
1798 init_completion(&req->r_safe_completion);
1799 INIT_LIST_HEAD(&req->r_unsafe_item);
1801 ktime_get_coarse_real_ts64(&ts);
1802 req->r_stamp = timespec64_trunc(ts, mdsc->fsc->sb->s_time_gran);
1805 req->r_direct_mode = mode;
1810 * return oldest (lowest) request, tid in request tree, 0 if none.
1812 * called under mdsc->mutex.
1814 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1816 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1818 return rb_entry(rb_first(&mdsc->request_tree),
1819 struct ceph_mds_request, r_node);
1822 static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1824 return mdsc->oldest_tid;
1828 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1829 * on build_path_from_dentry in fs/cifs/dir.c.
1831 * If @stop_on_nosnap, generate path relative to the first non-snapped
1834 * Encode hidden .snap dirs as a double /, i.e.
1835 * foo/.snap/bar -> foo//bar
1837 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1840 struct dentry *temp;
1846 return ERR_PTR(-EINVAL);
1850 seq = read_seqbegin(&rename_lock);
1852 for (temp = dentry; !IS_ROOT(temp);) {
1853 struct inode *inode = d_inode(temp);
1854 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1855 len++; /* slash only */
1856 else if (stop_on_nosnap && inode &&
1857 ceph_snap(inode) == CEPH_NOSNAP)
1860 len += 1 + temp->d_name.len;
1861 temp = temp->d_parent;
1865 len--; /* no leading '/' */
1867 path = kmalloc(len+1, GFP_NOFS);
1869 return ERR_PTR(-ENOMEM);
1871 path[pos] = 0; /* trailing null */
1873 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1874 struct inode *inode;
1876 spin_lock(&temp->d_lock);
1877 inode = d_inode(temp);
1878 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1879 dout("build_path path+%d: %p SNAPDIR\n",
1881 } else if (stop_on_nosnap && inode &&
1882 ceph_snap(inode) == CEPH_NOSNAP) {
1883 spin_unlock(&temp->d_lock);
1886 pos -= temp->d_name.len;
1888 spin_unlock(&temp->d_lock);
1891 strncpy(path + pos, temp->d_name.name,
1894 spin_unlock(&temp->d_lock);
1897 temp = temp->d_parent;
1900 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
1901 pr_err("build_path did not end path lookup where "
1902 "expected, namelen is %d, pos is %d\n", len, pos);
1903 /* presumably this is only possible if racing with a
1904 rename of one of the parent directories (we can not
1905 lock the dentries above us to prevent this, but
1906 retrying should be harmless) */
1911 *base = ceph_ino(d_inode(temp));
1913 dout("build_path on %p %d built %llx '%.*s'\n",
1914 dentry, d_count(dentry), *base, len, path);
1918 static int build_dentry_path(struct dentry *dentry, struct inode *dir,
1919 const char **ppath, int *ppathlen, u64 *pino,
1926 dir = d_inode_rcu(dentry->d_parent);
1927 if (dir && ceph_snap(dir) == CEPH_NOSNAP) {
1928 *pino = ceph_ino(dir);
1930 *ppath = dentry->d_name.name;
1931 *ppathlen = dentry->d_name.len;
1935 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1937 return PTR_ERR(path);
1943 static int build_inode_path(struct inode *inode,
1944 const char **ppath, int *ppathlen, u64 *pino,
1947 struct dentry *dentry;
1950 if (ceph_snap(inode) == CEPH_NOSNAP) {
1951 *pino = ceph_ino(inode);
1955 dentry = d_find_alias(inode);
1956 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1959 return PTR_ERR(path);
1966 * request arguments may be specified via an inode *, a dentry *, or
1967 * an explicit ino+path.
1969 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1970 struct inode *rdiri, const char *rpath,
1971 u64 rino, const char **ppath, int *pathlen,
1972 u64 *ino, int *freepath)
1977 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1978 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1980 } else if (rdentry) {
1981 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
1983 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1985 } else if (rpath || rino) {
1988 *pathlen = rpath ? strlen(rpath) : 0;
1989 dout(" path %.*s\n", *pathlen, rpath);
1996 * called under mdsc->mutex
1998 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1999 struct ceph_mds_request *req,
2000 int mds, bool drop_cap_releases)
2002 struct ceph_msg *msg;
2003 struct ceph_mds_request_head *head;
2004 const char *path1 = NULL;
2005 const char *path2 = NULL;
2006 u64 ino1 = 0, ino2 = 0;
2007 int pathlen1 = 0, pathlen2 = 0;
2008 int freepath1 = 0, freepath2 = 0;
2014 ret = set_request_path_attr(req->r_inode, req->r_dentry,
2015 req->r_parent, req->r_path1, req->r_ino1.ino,
2016 &path1, &pathlen1, &ino1, &freepath1);
2022 ret = set_request_path_attr(NULL, req->r_old_dentry,
2023 req->r_old_dentry_dir,
2024 req->r_path2, req->r_ino2.ino,
2025 &path2, &pathlen2, &ino2, &freepath2);
2031 len = sizeof(*head) +
2032 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
2033 sizeof(struct ceph_timespec);
2035 /* calculate (max) length for cap releases */
2036 len += sizeof(struct ceph_mds_request_release) *
2037 (!!req->r_inode_drop + !!req->r_dentry_drop +
2038 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2039 if (req->r_dentry_drop)
2040 len += req->r_dentry->d_name.len;
2041 if (req->r_old_dentry_drop)
2042 len += req->r_old_dentry->d_name.len;
2044 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
2046 msg = ERR_PTR(-ENOMEM);
2050 msg->hdr.version = cpu_to_le16(2);
2051 msg->hdr.tid = cpu_to_le64(req->r_tid);
2053 head = msg->front.iov_base;
2054 p = msg->front.iov_base + sizeof(*head);
2055 end = msg->front.iov_base + msg->front.iov_len;
2057 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2058 head->op = cpu_to_le32(req->r_op);
2059 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2060 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
2061 head->args = req->r_args;
2063 ceph_encode_filepath(&p, end, ino1, path1);
2064 ceph_encode_filepath(&p, end, ino2, path2);
2066 /* make note of release offset, in case we need to replay */
2067 req->r_request_release_offset = p - msg->front.iov_base;
2071 if (req->r_inode_drop)
2072 releases += ceph_encode_inode_release(&p,
2073 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
2074 mds, req->r_inode_drop, req->r_inode_unless, 0);
2075 if (req->r_dentry_drop)
2076 releases += ceph_encode_dentry_release(&p, req->r_dentry,
2077 req->r_parent, mds, req->r_dentry_drop,
2078 req->r_dentry_unless);
2079 if (req->r_old_dentry_drop)
2080 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
2081 req->r_old_dentry_dir, mds,
2082 req->r_old_dentry_drop,
2083 req->r_old_dentry_unless);
2084 if (req->r_old_inode_drop)
2085 releases += ceph_encode_inode_release(&p,
2086 d_inode(req->r_old_dentry),
2087 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
2089 if (drop_cap_releases) {
2091 p = msg->front.iov_base + req->r_request_release_offset;
2094 head->num_releases = cpu_to_le16(releases);
2098 struct ceph_timespec ts;
2099 ceph_encode_timespec64(&ts, &req->r_stamp);
2100 ceph_encode_copy(&p, &ts, sizeof(ts));
2104 msg->front.iov_len = p - msg->front.iov_base;
2105 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2107 if (req->r_pagelist) {
2108 struct ceph_pagelist *pagelist = req->r_pagelist;
2109 refcount_inc(&pagelist->refcnt);
2110 ceph_msg_data_add_pagelist(msg, pagelist);
2111 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2113 msg->hdr.data_len = 0;
2116 msg->hdr.data_off = cpu_to_le16(0);
2120 kfree((char *)path2);
2123 kfree((char *)path1);
2129 * called under mdsc->mutex if error, under no mutex if
2132 static void complete_request(struct ceph_mds_client *mdsc,
2133 struct ceph_mds_request *req)
2135 if (req->r_callback)
2136 req->r_callback(mdsc, req);
2138 complete_all(&req->r_completion);
2142 * called under mdsc->mutex
2144 static int __prepare_send_request(struct ceph_mds_client *mdsc,
2145 struct ceph_mds_request *req,
2146 int mds, bool drop_cap_releases)
2148 struct ceph_mds_request_head *rhead;
2149 struct ceph_msg *msg;
2154 struct ceph_cap *cap =
2155 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2158 req->r_sent_on_mseq = cap->mseq;
2160 req->r_sent_on_mseq = -1;
2162 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2163 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2165 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2168 * Replay. Do not regenerate message (and rebuild
2169 * paths, etc.); just use the original message.
2170 * Rebuilding paths will break for renames because
2171 * d_move mangles the src name.
2173 msg = req->r_request;
2174 rhead = msg->front.iov_base;
2176 flags = le32_to_cpu(rhead->flags);
2177 flags |= CEPH_MDS_FLAG_REPLAY;
2178 rhead->flags = cpu_to_le32(flags);
2180 if (req->r_target_inode)
2181 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2183 rhead->num_retry = req->r_attempts - 1;
2185 /* remove cap/dentry releases from message */
2186 rhead->num_releases = 0;
2189 p = msg->front.iov_base + req->r_request_release_offset;
2191 struct ceph_timespec ts;
2192 ceph_encode_timespec64(&ts, &req->r_stamp);
2193 ceph_encode_copy(&p, &ts, sizeof(ts));
2196 msg->front.iov_len = p - msg->front.iov_base;
2197 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2201 if (req->r_request) {
2202 ceph_msg_put(req->r_request);
2203 req->r_request = NULL;
2205 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
2207 req->r_err = PTR_ERR(msg);
2208 return PTR_ERR(msg);
2210 req->r_request = msg;
2212 rhead = msg->front.iov_base;
2213 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
2214 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2215 flags |= CEPH_MDS_FLAG_REPLAY;
2217 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2218 rhead->flags = cpu_to_le32(flags);
2219 rhead->num_fwd = req->r_num_fwd;
2220 rhead->num_retry = req->r_attempts - 1;
2223 dout(" r_parent = %p\n", req->r_parent);
2228 * send request, or put it on the appropriate wait list.
2230 static void __do_request(struct ceph_mds_client *mdsc,
2231 struct ceph_mds_request *req)
2233 struct ceph_mds_session *session = NULL;
2237 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2238 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
2239 __unregister_request(mdsc, req);
2243 if (req->r_timeout &&
2244 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2245 dout("do_request timed out\n");
2249 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
2250 dout("do_request forced umount\n");
2254 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
2255 if (mdsc->mdsmap_err) {
2256 err = mdsc->mdsmap_err;
2257 dout("do_request mdsmap err %d\n", err);
2260 if (mdsc->mdsmap->m_epoch == 0) {
2261 dout("do_request no mdsmap, waiting for map\n");
2262 list_add(&req->r_wait, &mdsc->waiting_for_map);
2265 if (!(mdsc->fsc->mount_options->flags &
2266 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2267 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
2269 pr_info("probably no mds server is up\n");
2274 put_request_session(req);
2276 mds = __choose_mds(mdsc, req);
2278 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2279 dout("do_request no mds or not active, waiting for map\n");
2280 list_add(&req->r_wait, &mdsc->waiting_for_map);
2284 /* get, open session */
2285 session = __ceph_lookup_mds_session(mdsc, mds);
2287 session = register_session(mdsc, mds);
2288 if (IS_ERR(session)) {
2289 err = PTR_ERR(session);
2293 req->r_session = get_session(session);
2295 dout("do_request mds%d session %p state %s\n", mds, session,
2296 ceph_session_state_name(session->s_state));
2297 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2298 session->s_state != CEPH_MDS_SESSION_HUNG) {
2299 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2303 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2304 session->s_state == CEPH_MDS_SESSION_CLOSING)
2305 __open_session(mdsc, session);
2306 list_add(&req->r_wait, &session->s_waiting);
2311 req->r_resend_mds = -1; /* forget any previous mds hint */
2313 if (req->r_request_started == 0) /* note request start time */
2314 req->r_request_started = jiffies;
2316 err = __prepare_send_request(mdsc, req, mds, false);
2318 ceph_msg_get(req->r_request);
2319 ceph_con_send(&session->s_con, req->r_request);
2323 ceph_put_mds_session(session);
2326 dout("__do_request early error %d\n", err);
2328 complete_request(mdsc, req);
2329 __unregister_request(mdsc, req);
2335 * called under mdsc->mutex
2337 static void __wake_requests(struct ceph_mds_client *mdsc,
2338 struct list_head *head)
2340 struct ceph_mds_request *req;
2341 LIST_HEAD(tmp_list);
2343 list_splice_init(head, &tmp_list);
2345 while (!list_empty(&tmp_list)) {
2346 req = list_entry(tmp_list.next,
2347 struct ceph_mds_request, r_wait);
2348 list_del_init(&req->r_wait);
2349 dout(" wake request %p tid %llu\n", req, req->r_tid);
2350 __do_request(mdsc, req);
2355 * Wake up threads with requests pending for @mds, so that they can
2356 * resubmit their requests to a possibly different mds.
2358 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
2360 struct ceph_mds_request *req;
2361 struct rb_node *p = rb_first(&mdsc->request_tree);
2363 dout("kick_requests mds%d\n", mds);
2365 req = rb_entry(p, struct ceph_mds_request, r_node);
2367 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2369 if (req->r_attempts > 0)
2370 continue; /* only new requests */
2371 if (req->r_session &&
2372 req->r_session->s_mds == mds) {
2373 dout(" kicking tid %llu\n", req->r_tid);
2374 list_del_init(&req->r_wait);
2375 __do_request(mdsc, req);
2380 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2381 struct ceph_mds_request *req)
2383 dout("submit_request on %p\n", req);
2384 mutex_lock(&mdsc->mutex);
2385 __register_request(mdsc, req, NULL);
2386 __do_request(mdsc, req);
2387 mutex_unlock(&mdsc->mutex);
2391 * Synchrously perform an mds request. Take care of all of the
2392 * session setup, forwarding, retry details.
2394 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2396 struct ceph_mds_request *req)
2400 dout("do_request on %p\n", req);
2402 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
2404 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
2406 ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
2407 if (req->r_old_dentry_dir)
2408 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2412 mutex_lock(&mdsc->mutex);
2413 __register_request(mdsc, req, dir);
2414 __do_request(mdsc, req);
2422 mutex_unlock(&mdsc->mutex);
2423 dout("do_request waiting\n");
2424 if (!req->r_timeout && req->r_wait_for_completion) {
2425 err = req->r_wait_for_completion(mdsc, req);
2427 long timeleft = wait_for_completion_killable_timeout(
2429 ceph_timeout_jiffies(req->r_timeout));
2433 err = -EIO; /* timed out */
2435 err = timeleft; /* killed */
2437 dout("do_request waited, got %d\n", err);
2438 mutex_lock(&mdsc->mutex);
2440 /* only abort if we didn't race with a real reply */
2441 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2442 err = le32_to_cpu(req->r_reply_info.head->result);
2443 } else if (err < 0) {
2444 dout("aborted request %lld with %d\n", req->r_tid, err);
2447 * ensure we aren't running concurrently with
2448 * ceph_fill_trace or ceph_readdir_prepopulate, which
2449 * rely on locks (dir mutex) held by our caller.
2451 mutex_lock(&req->r_fill_mutex);
2453 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
2454 mutex_unlock(&req->r_fill_mutex);
2456 if (req->r_parent &&
2457 (req->r_op & CEPH_MDS_OP_WRITE))
2458 ceph_invalidate_dir_request(req);
2464 mutex_unlock(&mdsc->mutex);
2465 dout("do_request %p done, result %d\n", req, err);
2470 * Invalidate dir's completeness, dentry lease state on an aborted MDS
2471 * namespace request.
2473 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2475 struct inode *dir = req->r_parent;
2476 struct inode *old_dir = req->r_old_dentry_dir;
2478 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
2480 ceph_dir_clear_complete(dir);
2482 ceph_dir_clear_complete(old_dir);
2484 ceph_invalidate_dentry_lease(req->r_dentry);
2485 if (req->r_old_dentry)
2486 ceph_invalidate_dentry_lease(req->r_old_dentry);
2492 * We take the session mutex and parse and process the reply immediately.
2493 * This preserves the logical ordering of replies, capabilities, etc., sent
2494 * by the MDS as they are applied to our local cache.
2496 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2498 struct ceph_mds_client *mdsc = session->s_mdsc;
2499 struct ceph_mds_request *req;
2500 struct ceph_mds_reply_head *head = msg->front.iov_base;
2501 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2502 struct ceph_snap_realm *realm;
2505 int mds = session->s_mds;
2507 if (msg->front.iov_len < sizeof(*head)) {
2508 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2513 /* get request, session */
2514 tid = le64_to_cpu(msg->hdr.tid);
2515 mutex_lock(&mdsc->mutex);
2516 req = lookup_get_request(mdsc, tid);
2518 dout("handle_reply on unknown tid %llu\n", tid);
2519 mutex_unlock(&mdsc->mutex);
2522 dout("handle_reply %p\n", req);
2524 /* correct session? */
2525 if (req->r_session != session) {
2526 pr_err("mdsc_handle_reply got %llu on session mds%d"
2527 " not mds%d\n", tid, session->s_mds,
2528 req->r_session ? req->r_session->s_mds : -1);
2529 mutex_unlock(&mdsc->mutex);
2534 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
2535 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
2536 pr_warn("got a dup %s reply on %llu from mds%d\n",
2537 head->safe ? "safe" : "unsafe", tid, mds);
2538 mutex_unlock(&mdsc->mutex);
2541 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
2542 pr_warn("got unsafe after safe on %llu from mds%d\n",
2544 mutex_unlock(&mdsc->mutex);
2548 result = le32_to_cpu(head->result);
2552 * if we're not talking to the authority, send to them
2553 * if the authority has changed while we weren't looking,
2554 * send to new authority
2555 * Otherwise we just have to return an ESTALE
2557 if (result == -ESTALE) {
2558 dout("got ESTALE on request %llu\n", req->r_tid);
2559 req->r_resend_mds = -1;
2560 if (req->r_direct_mode != USE_AUTH_MDS) {
2561 dout("not using auth, setting for that now\n");
2562 req->r_direct_mode = USE_AUTH_MDS;
2563 __do_request(mdsc, req);
2564 mutex_unlock(&mdsc->mutex);
2567 int mds = __choose_mds(mdsc, req);
2568 if (mds >= 0 && mds != req->r_session->s_mds) {
2569 dout("but auth changed, so resending\n");
2570 __do_request(mdsc, req);
2571 mutex_unlock(&mdsc->mutex);
2575 dout("have to return ESTALE on request %llu\n", req->r_tid);
2580 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
2581 __unregister_request(mdsc, req);
2583 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2585 * We already handled the unsafe response, now do the
2586 * cleanup. No need to examine the response; the MDS
2587 * doesn't include any result info in the safe
2588 * response. And even if it did, there is nothing
2589 * useful we could do with a revised return value.
2591 dout("got safe reply %llu, mds%d\n", tid, mds);
2593 /* last unsafe request during umount? */
2594 if (mdsc->stopping && !__get_oldest_req(mdsc))
2595 complete_all(&mdsc->safe_umount_waiters);
2596 mutex_unlock(&mdsc->mutex);
2600 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
2601 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2602 if (req->r_unsafe_dir) {
2603 struct ceph_inode_info *ci =
2604 ceph_inode(req->r_unsafe_dir);
2605 spin_lock(&ci->i_unsafe_lock);
2606 list_add_tail(&req->r_unsafe_dir_item,
2607 &ci->i_unsafe_dirops);
2608 spin_unlock(&ci->i_unsafe_lock);
2612 dout("handle_reply tid %lld result %d\n", tid, result);
2613 rinfo = &req->r_reply_info;
2614 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2615 mutex_unlock(&mdsc->mutex);
2617 mutex_lock(&session->s_mutex);
2619 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
2626 if (rinfo->snapblob_len) {
2627 down_write(&mdsc->snap_rwsem);
2628 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2629 rinfo->snapblob + rinfo->snapblob_len,
2630 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2632 downgrade_write(&mdsc->snap_rwsem);
2634 down_read(&mdsc->snap_rwsem);
2637 /* insert trace into our cache */
2638 mutex_lock(&req->r_fill_mutex);
2639 current->journal_info = req;
2640 err = ceph_fill_trace(mdsc->fsc->sb, req);
2642 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
2643 req->r_op == CEPH_MDS_OP_LSSNAP))
2644 ceph_readdir_prepopulate(req, req->r_session);
2645 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2647 current->journal_info = NULL;
2648 mutex_unlock(&req->r_fill_mutex);
2650 up_read(&mdsc->snap_rwsem);
2652 ceph_put_snap_realm(mdsc, realm);
2654 if (err == 0 && req->r_target_inode &&
2655 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2656 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
2657 spin_lock(&ci->i_unsafe_lock);
2658 list_add_tail(&req->r_unsafe_target_item, &ci->i_unsafe_iops);
2659 spin_unlock(&ci->i_unsafe_lock);
2662 mutex_lock(&mdsc->mutex);
2663 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
2667 req->r_reply = ceph_msg_get(msg);
2668 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
2671 dout("reply arrived after request %lld was aborted\n", tid);
2673 mutex_unlock(&mdsc->mutex);
2675 mutex_unlock(&session->s_mutex);
2677 /* kick calling process */
2678 complete_request(mdsc, req);
2680 ceph_mdsc_put_request(req);
2687 * handle mds notification that our request has been forwarded.
2689 static void handle_forward(struct ceph_mds_client *mdsc,
2690 struct ceph_mds_session *session,
2691 struct ceph_msg *msg)
2693 struct ceph_mds_request *req;
2694 u64 tid = le64_to_cpu(msg->hdr.tid);
2698 void *p = msg->front.iov_base;
2699 void *end = p + msg->front.iov_len;
2701 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2702 next_mds = ceph_decode_32(&p);
2703 fwd_seq = ceph_decode_32(&p);
2705 mutex_lock(&mdsc->mutex);
2706 req = lookup_get_request(mdsc, tid);
2708 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2709 goto out; /* dup reply? */
2712 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
2713 dout("forward tid %llu aborted, unregistering\n", tid);
2714 __unregister_request(mdsc, req);
2715 } else if (fwd_seq <= req->r_num_fwd) {
2716 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2717 tid, next_mds, req->r_num_fwd, fwd_seq);
2719 /* resend. forward race not possible; mds would drop */
2720 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2722 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
2723 req->r_attempts = 0;
2724 req->r_num_fwd = fwd_seq;
2725 req->r_resend_mds = next_mds;
2726 put_request_session(req);
2727 __do_request(mdsc, req);
2729 ceph_mdsc_put_request(req);
2731 mutex_unlock(&mdsc->mutex);
2735 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2739 * handle a mds session control message
2741 static void handle_session(struct ceph_mds_session *session,
2742 struct ceph_msg *msg)
2744 struct ceph_mds_client *mdsc = session->s_mdsc;
2747 int mds = session->s_mds;
2748 struct ceph_mds_session_head *h = msg->front.iov_base;
2752 if (msg->front.iov_len != sizeof(*h))
2754 op = le32_to_cpu(h->op);
2755 seq = le64_to_cpu(h->seq);
2757 mutex_lock(&mdsc->mutex);
2758 if (op == CEPH_SESSION_CLOSE) {
2759 get_session(session);
2760 __unregister_session(mdsc, session);
2762 /* FIXME: this ttl calculation is generous */
2763 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2764 mutex_unlock(&mdsc->mutex);
2766 mutex_lock(&session->s_mutex);
2768 dout("handle_session mds%d %s %p state %s seq %llu\n",
2769 mds, ceph_session_op_name(op), session,
2770 ceph_session_state_name(session->s_state), seq);
2772 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2773 session->s_state = CEPH_MDS_SESSION_OPEN;
2774 pr_info("mds%d came back\n", session->s_mds);
2778 case CEPH_SESSION_OPEN:
2779 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2780 pr_info("mds%d reconnect success\n", session->s_mds);
2781 session->s_state = CEPH_MDS_SESSION_OPEN;
2782 renewed_caps(mdsc, session, 0);
2785 __close_session(mdsc, session);
2788 case CEPH_SESSION_RENEWCAPS:
2789 if (session->s_renew_seq == seq)
2790 renewed_caps(mdsc, session, 1);
2793 case CEPH_SESSION_CLOSE:
2794 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2795 pr_info("mds%d reconnect denied\n", session->s_mds);
2796 cleanup_session_requests(mdsc, session);
2797 remove_session_caps(session);
2798 wake = 2; /* for good measure */
2799 wake_up_all(&mdsc->session_close_wq);
2802 case CEPH_SESSION_STALE:
2803 pr_info("mds%d caps went stale, renewing\n",
2805 spin_lock(&session->s_gen_ttl_lock);
2806 session->s_cap_gen++;
2807 session->s_cap_ttl = jiffies - 1;
2808 spin_unlock(&session->s_gen_ttl_lock);
2809 send_renew_caps(mdsc, session);
2812 case CEPH_SESSION_RECALL_STATE:
2813 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2816 case CEPH_SESSION_FLUSHMSG:
2817 send_flushmsg_ack(mdsc, session, seq);
2820 case CEPH_SESSION_FORCE_RO:
2821 dout("force_session_readonly %p\n", session);
2822 spin_lock(&session->s_cap_lock);
2823 session->s_readonly = true;
2824 spin_unlock(&session->s_cap_lock);
2825 wake_up_session_caps(session, 0);
2828 case CEPH_SESSION_REJECT:
2829 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
2830 pr_info("mds%d rejected session\n", session->s_mds);
2831 session->s_state = CEPH_MDS_SESSION_REJECTED;
2832 cleanup_session_requests(mdsc, session);
2833 remove_session_caps(session);
2834 wake = 2; /* for good measure */
2838 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2842 mutex_unlock(&session->s_mutex);
2844 mutex_lock(&mdsc->mutex);
2845 __wake_requests(mdsc, &session->s_waiting);
2847 kick_requests(mdsc, mds);
2848 mutex_unlock(&mdsc->mutex);
2850 if (op == CEPH_SESSION_CLOSE)
2851 ceph_put_mds_session(session);
2855 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2856 (int)msg->front.iov_len);
2863 * called under session->mutex.
2865 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2866 struct ceph_mds_session *session)
2868 struct ceph_mds_request *req, *nreq;
2872 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2874 mutex_lock(&mdsc->mutex);
2875 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2876 err = __prepare_send_request(mdsc, req, session->s_mds, true);
2878 ceph_msg_get(req->r_request);
2879 ceph_con_send(&session->s_con, req->r_request);
2884 * also re-send old requests when MDS enters reconnect stage. So that MDS
2885 * can process completed request in clientreplay stage.
2887 p = rb_first(&mdsc->request_tree);
2889 req = rb_entry(p, struct ceph_mds_request, r_node);
2891 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2893 if (req->r_attempts == 0)
2894 continue; /* only old requests */
2895 if (req->r_session &&
2896 req->r_session->s_mds == session->s_mds) {
2897 err = __prepare_send_request(mdsc, req,
2898 session->s_mds, true);
2900 ceph_msg_get(req->r_request);
2901 ceph_con_send(&session->s_con, req->r_request);
2905 mutex_unlock(&mdsc->mutex);
2909 * Encode information about a cap for a reconnect with the MDS.
2911 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2915 struct ceph_mds_cap_reconnect v2;
2916 struct ceph_mds_cap_reconnect_v1 v1;
2918 struct ceph_inode_info *ci = cap->ci;
2919 struct ceph_reconnect_state *recon_state = arg;
2920 struct ceph_pagelist *pagelist = recon_state->pagelist;
2925 struct dentry *dentry;
2927 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2928 inode, ceph_vinop(inode), cap, cap->cap_id,
2929 ceph_cap_string(cap->issued));
2930 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2934 dentry = d_find_alias(inode);
2936 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2938 err = PTR_ERR(path);
2947 spin_lock(&ci->i_ceph_lock);
2948 cap->seq = 0; /* reset cap seq */
2949 cap->issue_seq = 0; /* and issue_seq */
2950 cap->mseq = 0; /* and migrate_seq */
2951 cap->cap_gen = cap->session->s_cap_gen;
2953 if (recon_state->msg_version >= 2) {
2954 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2955 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2956 rec.v2.issued = cpu_to_le32(cap->issued);
2957 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2958 rec.v2.pathbase = cpu_to_le64(pathbase);
2959 rec.v2.flock_len = (__force __le32)
2960 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
2962 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2963 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2964 rec.v1.issued = cpu_to_le32(cap->issued);
2965 rec.v1.size = cpu_to_le64(inode->i_size);
2966 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
2967 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
2968 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2969 rec.v1.pathbase = cpu_to_le64(pathbase);
2972 if (list_empty(&ci->i_cap_snaps)) {
2973 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
2975 struct ceph_cap_snap *capsnap =
2976 list_first_entry(&ci->i_cap_snaps,
2977 struct ceph_cap_snap, ci_item);
2978 snap_follows = capsnap->follows;
2980 spin_unlock(&ci->i_ceph_lock);
2982 if (recon_state->msg_version >= 2) {
2983 int num_fcntl_locks, num_flock_locks;
2984 struct ceph_filelock *flocks = NULL;
2985 size_t struct_len, total_len = 0;
2989 if (rec.v2.flock_len) {
2990 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
2992 num_fcntl_locks = 0;
2993 num_flock_locks = 0;
2995 if (num_fcntl_locks + num_flock_locks > 0) {
2996 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
2997 sizeof(struct ceph_filelock),
3003 err = ceph_encode_locks_to_buffer(inode, flocks,
3018 if (recon_state->msg_version >= 3) {
3019 /* version, compat_version and struct_len */
3020 total_len = 2 * sizeof(u8) + sizeof(u32);
3024 * number of encoded locks is stable, so copy to pagelist
3026 struct_len = 2 * sizeof(u32) +
3027 (num_fcntl_locks + num_flock_locks) *
3028 sizeof(struct ceph_filelock);
3029 rec.v2.flock_len = cpu_to_le32(struct_len);
3031 struct_len += sizeof(rec.v2);
3032 struct_len += sizeof(u32) + pathlen;
3035 struct_len += sizeof(u64); /* snap_follows */
3037 total_len += struct_len;
3038 err = ceph_pagelist_reserve(pagelist, total_len);
3041 if (recon_state->msg_version >= 3) {
3042 ceph_pagelist_encode_8(pagelist, struct_v);
3043 ceph_pagelist_encode_8(pagelist, 1);
3044 ceph_pagelist_encode_32(pagelist, struct_len);
3046 ceph_pagelist_encode_string(pagelist, path, pathlen);
3047 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3048 ceph_locks_to_pagelist(flocks, pagelist,
3052 ceph_pagelist_encode_64(pagelist, snap_follows);
3056 size_t size = sizeof(u32) + pathlen + sizeof(rec.v1);
3057 err = ceph_pagelist_reserve(pagelist, size);
3059 ceph_pagelist_encode_string(pagelist, path, pathlen);
3060 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
3064 recon_state->nr_caps++;
3074 * If an MDS fails and recovers, clients need to reconnect in order to
3075 * reestablish shared state. This includes all caps issued through
3076 * this session _and_ the snap_realm hierarchy. Because it's not
3077 * clear which snap realms the mds cares about, we send everything we
3078 * know about.. that ensures we'll then get any new info the
3079 * recovering MDS might have.
3081 * This is a relatively heavyweight operation, but it's rare.
3083 * called with mdsc->mutex held.
3085 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3086 struct ceph_mds_session *session)
3088 struct ceph_msg *reply;
3090 int mds = session->s_mds;
3093 struct ceph_pagelist *pagelist;
3094 struct ceph_reconnect_state recon_state;
3097 pr_info("mds%d reconnect start\n", mds);
3099 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
3101 goto fail_nopagelist;
3102 ceph_pagelist_init(pagelist);
3104 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
3108 mutex_lock(&session->s_mutex);
3109 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3112 dout("session %p state %s\n", session,
3113 ceph_session_state_name(session->s_state));
3115 spin_lock(&session->s_gen_ttl_lock);
3116 session->s_cap_gen++;
3117 spin_unlock(&session->s_gen_ttl_lock);
3119 spin_lock(&session->s_cap_lock);
3120 /* don't know if session is readonly */
3121 session->s_readonly = 0;
3123 * notify __ceph_remove_cap() that we are composing cap reconnect.
3124 * If a cap get released before being added to the cap reconnect,
3125 * __ceph_remove_cap() should skip queuing cap release.
3127 session->s_cap_reconnect = 1;
3128 /* drop old cap expires; we're about to reestablish that state */
3129 detach_cap_releases(session, &dispose);
3130 spin_unlock(&session->s_cap_lock);
3131 dispose_cap_releases(mdsc, &dispose);
3133 /* trim unused caps to reduce MDS's cache rejoin time */
3134 if (mdsc->fsc->sb->s_root)
3135 shrink_dcache_parent(mdsc->fsc->sb->s_root);
3137 ceph_con_close(&session->s_con);
3138 ceph_con_open(&session->s_con,
3139 CEPH_ENTITY_TYPE_MDS, mds,
3140 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3142 /* replay unsafe requests */
3143 replay_unsafe_requests(mdsc, session);
3145 down_read(&mdsc->snap_rwsem);
3147 /* traverse this session's caps */
3148 s_nr_caps = session->s_nr_caps;
3149 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
3153 recon_state.nr_caps = 0;
3154 recon_state.pagelist = pagelist;
3155 if (session->s_con.peer_features & CEPH_FEATURE_MDSENC)
3156 recon_state.msg_version = 3;
3157 else if (session->s_con.peer_features & CEPH_FEATURE_FLOCK)
3158 recon_state.msg_version = 2;
3160 recon_state.msg_version = 1;
3161 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
3165 spin_lock(&session->s_cap_lock);
3166 session->s_cap_reconnect = 0;
3167 spin_unlock(&session->s_cap_lock);
3170 * snaprealms. we provide mds with the ino, seq (version), and
3171 * parent for all of our realms. If the mds has any newer info,
3174 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3175 struct ceph_snap_realm *realm =
3176 rb_entry(p, struct ceph_snap_realm, node);
3177 struct ceph_mds_snaprealm_reconnect sr_rec;
3179 dout(" adding snap realm %llx seq %lld parent %llx\n",
3180 realm->ino, realm->seq, realm->parent_ino);
3181 sr_rec.ino = cpu_to_le64(realm->ino);
3182 sr_rec.seq = cpu_to_le64(realm->seq);
3183 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3184 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3189 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
3191 /* raced with cap release? */
3192 if (s_nr_caps != recon_state.nr_caps) {
3193 struct page *page = list_first_entry(&pagelist->head,
3195 __le32 *addr = kmap_atomic(page);
3196 *addr = cpu_to_le32(recon_state.nr_caps);
3197 kunmap_atomic(addr);
3200 reply->hdr.data_len = cpu_to_le32(pagelist->length);
3201 ceph_msg_data_add_pagelist(reply, pagelist);
3203 ceph_early_kick_flushing_caps(mdsc, session);
3205 ceph_con_send(&session->s_con, reply);
3207 mutex_unlock(&session->s_mutex);
3209 mutex_lock(&mdsc->mutex);
3210 __wake_requests(mdsc, &session->s_waiting);
3211 mutex_unlock(&mdsc->mutex);
3213 up_read(&mdsc->snap_rwsem);
3217 ceph_msg_put(reply);
3218 up_read(&mdsc->snap_rwsem);
3219 mutex_unlock(&session->s_mutex);
3221 ceph_pagelist_release(pagelist);
3223 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
3229 * compare old and new mdsmaps, kicking requests
3230 * and closing out old connections as necessary
3232 * called under mdsc->mutex.
3234 static void check_new_map(struct ceph_mds_client *mdsc,
3235 struct ceph_mdsmap *newmap,
3236 struct ceph_mdsmap *oldmap)
3239 int oldstate, newstate;
3240 struct ceph_mds_session *s;
3242 dout("check_new_map new %u old %u\n",
3243 newmap->m_epoch, oldmap->m_epoch);
3245 for (i = 0; i < oldmap->m_num_mds && i < mdsc->max_sessions; i++) {
3246 if (!mdsc->sessions[i])
3248 s = mdsc->sessions[i];
3249 oldstate = ceph_mdsmap_get_state(oldmap, i);
3250 newstate = ceph_mdsmap_get_state(newmap, i);
3252 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
3253 i, ceph_mds_state_name(oldstate),
3254 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
3255 ceph_mds_state_name(newstate),
3256 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
3257 ceph_session_state_name(s->s_state));
3259 if (i >= newmap->m_num_mds ||
3260 memcmp(ceph_mdsmap_get_addr(oldmap, i),
3261 ceph_mdsmap_get_addr(newmap, i),
3262 sizeof(struct ceph_entity_addr))) {
3263 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
3264 /* the session never opened, just close it
3267 __unregister_session(mdsc, s);
3268 __wake_requests(mdsc, &s->s_waiting);
3269 ceph_put_mds_session(s);
3270 } else if (i >= newmap->m_num_mds) {
3271 /* force close session for stopped mds */
3273 __unregister_session(mdsc, s);
3274 __wake_requests(mdsc, &s->s_waiting);
3275 kick_requests(mdsc, i);
3276 mutex_unlock(&mdsc->mutex);
3278 mutex_lock(&s->s_mutex);
3279 cleanup_session_requests(mdsc, s);
3280 remove_session_caps(s);
3281 mutex_unlock(&s->s_mutex);
3283 ceph_put_mds_session(s);
3285 mutex_lock(&mdsc->mutex);
3288 mutex_unlock(&mdsc->mutex);
3289 mutex_lock(&s->s_mutex);
3290 mutex_lock(&mdsc->mutex);
3291 ceph_con_close(&s->s_con);
3292 mutex_unlock(&s->s_mutex);
3293 s->s_state = CEPH_MDS_SESSION_RESTARTING;
3295 } else if (oldstate == newstate) {
3296 continue; /* nothing new with this mds */
3302 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
3303 newstate >= CEPH_MDS_STATE_RECONNECT) {
3304 mutex_unlock(&mdsc->mutex);
3305 send_mds_reconnect(mdsc, s);
3306 mutex_lock(&mdsc->mutex);
3310 * kick request on any mds that has gone active.
3312 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3313 newstate >= CEPH_MDS_STATE_ACTIVE) {
3314 if (oldstate != CEPH_MDS_STATE_CREATING &&
3315 oldstate != CEPH_MDS_STATE_STARTING)
3316 pr_info("mds%d recovery completed\n", s->s_mds);
3317 kick_requests(mdsc, i);
3318 ceph_kick_flushing_caps(mdsc, s);
3319 wake_up_session_caps(s, 1);
3323 for (i = 0; i < newmap->m_num_mds && i < mdsc->max_sessions; i++) {
3324 s = mdsc->sessions[i];
3327 if (!ceph_mdsmap_is_laggy(newmap, i))
3329 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3330 s->s_state == CEPH_MDS_SESSION_HUNG ||
3331 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3332 dout(" connecting to export targets of laggy mds%d\n",
3334 __open_export_target_sessions(mdsc, s);
3346 * caller must hold session s_mutex, dentry->d_lock
3348 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3350 struct ceph_dentry_info *di = ceph_dentry(dentry);
3352 ceph_put_mds_session(di->lease_session);
3353 di->lease_session = NULL;
3356 static void handle_lease(struct ceph_mds_client *mdsc,
3357 struct ceph_mds_session *session,
3358 struct ceph_msg *msg)
3360 struct super_block *sb = mdsc->fsc->sb;
3361 struct inode *inode;
3362 struct dentry *parent, *dentry;
3363 struct ceph_dentry_info *di;
3364 int mds = session->s_mds;
3365 struct ceph_mds_lease *h = msg->front.iov_base;
3367 struct ceph_vino vino;
3371 dout("handle_lease from mds%d\n", mds);
3374 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3376 vino.ino = le64_to_cpu(h->ino);
3377 vino.snap = CEPH_NOSNAP;
3378 seq = le32_to_cpu(h->seq);
3379 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
3380 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
3381 if (dname.len != get_unaligned_le32(h+1))
3385 inode = ceph_find_inode(sb, vino);
3386 dout("handle_lease %s, ino %llx %p %.*s\n",
3387 ceph_lease_op_name(h->action), vino.ino, inode,
3388 dname.len, dname.name);
3390 mutex_lock(&session->s_mutex);
3394 dout("handle_lease no inode %llx\n", vino.ino);
3399 parent = d_find_alias(inode);
3401 dout("no parent dentry on inode %p\n", inode);
3403 goto release; /* hrm... */
3405 dname.hash = full_name_hash(parent, dname.name, dname.len);
3406 dentry = d_lookup(parent, &dname);
3411 spin_lock(&dentry->d_lock);
3412 di = ceph_dentry(dentry);
3413 switch (h->action) {
3414 case CEPH_MDS_LEASE_REVOKE:
3415 if (di->lease_session == session) {
3416 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3417 h->seq = cpu_to_le32(di->lease_seq);
3418 __ceph_mdsc_drop_dentry_lease(dentry);
3423 case CEPH_MDS_LEASE_RENEW:
3424 if (di->lease_session == session &&
3425 di->lease_gen == session->s_cap_gen &&
3426 di->lease_renew_from &&
3427 di->lease_renew_after == 0) {
3428 unsigned long duration =
3429 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
3431 di->lease_seq = seq;
3432 di->time = di->lease_renew_from + duration;
3433 di->lease_renew_after = di->lease_renew_from +
3435 di->lease_renew_from = 0;
3439 spin_unlock(&dentry->d_lock);
3446 /* let's just reuse the same message */
3447 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3449 ceph_con_send(&session->s_con, msg);
3453 mutex_unlock(&session->s_mutex);
3457 pr_err("corrupt lease message\n");
3461 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3462 struct inode *inode,
3463 struct dentry *dentry, char action,
3466 struct ceph_msg *msg;
3467 struct ceph_mds_lease *lease;
3468 int len = sizeof(*lease) + sizeof(u32);
3471 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3472 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3473 dnamelen = dentry->d_name.len;
3476 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
3479 lease = msg->front.iov_base;
3480 lease->action = action;
3481 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3482 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3483 lease->seq = cpu_to_le32(seq);
3484 put_unaligned_le32(dnamelen, lease + 1);
3485 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3488 * if this is a preemptive lease RELEASE, no need to
3489 * flush request stream, since the actual request will
3492 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3494 ceph_con_send(&session->s_con, msg);
3498 * lock unlock sessions, to wait ongoing session activities
3500 static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
3504 mutex_lock(&mdsc->mutex);
3505 for (i = 0; i < mdsc->max_sessions; i++) {
3506 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3509 mutex_unlock(&mdsc->mutex);
3510 mutex_lock(&s->s_mutex);
3511 mutex_unlock(&s->s_mutex);
3512 ceph_put_mds_session(s);
3513 mutex_lock(&mdsc->mutex);
3515 mutex_unlock(&mdsc->mutex);
3521 * delayed work -- periodically trim expired leases, renew caps with mds
3523 static void schedule_delayed(struct ceph_mds_client *mdsc)
3526 unsigned hz = round_jiffies_relative(HZ * delay);
3527 schedule_delayed_work(&mdsc->delayed_work, hz);
3530 static void delayed_work(struct work_struct *work)
3533 struct ceph_mds_client *mdsc =
3534 container_of(work, struct ceph_mds_client, delayed_work.work);
3538 dout("mdsc delayed_work\n");
3539 ceph_check_delayed_caps(mdsc);
3541 mutex_lock(&mdsc->mutex);
3542 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3543 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3544 mdsc->last_renew_caps);
3546 mdsc->last_renew_caps = jiffies;
3548 for (i = 0; i < mdsc->max_sessions; i++) {
3549 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3552 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3553 dout("resending session close request for mds%d\n",
3555 request_close_session(mdsc, s);
3556 ceph_put_mds_session(s);
3559 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3560 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3561 s->s_state = CEPH_MDS_SESSION_HUNG;
3562 pr_info("mds%d hung\n", s->s_mds);
3565 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3566 /* this mds is failed or recovering, just wait */
3567 ceph_put_mds_session(s);
3570 mutex_unlock(&mdsc->mutex);
3572 mutex_lock(&s->s_mutex);
3574 send_renew_caps(mdsc, s);
3576 ceph_con_keepalive(&s->s_con);
3577 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3578 s->s_state == CEPH_MDS_SESSION_HUNG)
3579 ceph_send_cap_releases(mdsc, s);
3580 mutex_unlock(&s->s_mutex);
3581 ceph_put_mds_session(s);
3583 mutex_lock(&mdsc->mutex);
3585 mutex_unlock(&mdsc->mutex);
3587 schedule_delayed(mdsc);
3590 int ceph_mdsc_init(struct ceph_fs_client *fsc)
3593 struct ceph_mds_client *mdsc;
3595 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3599 mutex_init(&mdsc->mutex);
3600 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
3601 if (!mdsc->mdsmap) {
3607 init_completion(&mdsc->safe_umount_waiters);
3608 init_waitqueue_head(&mdsc->session_close_wq);
3609 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3610 mdsc->sessions = NULL;
3611 atomic_set(&mdsc->num_sessions, 0);
3612 mdsc->max_sessions = 0;
3614 atomic64_set(&mdsc->quotarealms_count, 0);
3615 mdsc->last_snap_seq = 0;
3616 init_rwsem(&mdsc->snap_rwsem);
3617 mdsc->snap_realms = RB_ROOT;
3618 INIT_LIST_HEAD(&mdsc->snap_empty);
3619 spin_lock_init(&mdsc->snap_empty_lock);
3621 mdsc->oldest_tid = 0;
3622 mdsc->request_tree = RB_ROOT;
3623 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3624 mdsc->last_renew_caps = jiffies;
3625 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3626 spin_lock_init(&mdsc->cap_delay_lock);
3627 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3628 spin_lock_init(&mdsc->snap_flush_lock);
3629 mdsc->last_cap_flush_tid = 1;
3630 INIT_LIST_HEAD(&mdsc->cap_flush_list);
3631 INIT_LIST_HEAD(&mdsc->cap_dirty);
3632 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
3633 mdsc->num_cap_flushing = 0;
3634 spin_lock_init(&mdsc->cap_dirty_lock);
3635 init_waitqueue_head(&mdsc->cap_flushing_wq);
3636 spin_lock_init(&mdsc->dentry_lru_lock);
3637 INIT_LIST_HEAD(&mdsc->dentry_lru);
3639 ceph_caps_init(mdsc);
3640 ceph_adjust_min_caps(mdsc, fsc->min_caps);
3642 init_rwsem(&mdsc->pool_perm_rwsem);
3643 mdsc->pool_perm_tree = RB_ROOT;
3645 strscpy(mdsc->nodename, utsname()->nodename,
3646 sizeof(mdsc->nodename));
3651 * Wait for safe replies on open mds requests. If we time out, drop
3652 * all requests from the tree to avoid dangling dentry refs.
3654 static void wait_requests(struct ceph_mds_client *mdsc)
3656 struct ceph_options *opts = mdsc->fsc->client->options;
3657 struct ceph_mds_request *req;
3659 mutex_lock(&mdsc->mutex);
3660 if (__get_oldest_req(mdsc)) {
3661 mutex_unlock(&mdsc->mutex);
3663 dout("wait_requests waiting for requests\n");
3664 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
3665 ceph_timeout_jiffies(opts->mount_timeout));
3667 /* tear down remaining requests */
3668 mutex_lock(&mdsc->mutex);
3669 while ((req = __get_oldest_req(mdsc))) {
3670 dout("wait_requests timed out on tid %llu\n",
3672 __unregister_request(mdsc, req);
3675 mutex_unlock(&mdsc->mutex);
3676 dout("wait_requests done\n");
3680 * called before mount is ro, and before dentries are torn down.
3681 * (hmm, does this still race with new lookups?)
3683 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3685 dout("pre_umount\n");
3688 lock_unlock_sessions(mdsc);
3689 ceph_flush_dirty_caps(mdsc);
3690 wait_requests(mdsc);
3693 * wait for reply handlers to drop their request refs and
3694 * their inode/dcache refs
3700 * wait for all write mds requests to flush.
3702 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3704 struct ceph_mds_request *req = NULL, *nextreq;
3707 mutex_lock(&mdsc->mutex);
3708 dout("wait_unsafe_requests want %lld\n", want_tid);
3710 req = __get_oldest_req(mdsc);
3711 while (req && req->r_tid <= want_tid) {
3712 /* find next request */
3713 n = rb_next(&req->r_node);
3715 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3718 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
3719 (req->r_op & CEPH_MDS_OP_WRITE)) {
3721 ceph_mdsc_get_request(req);
3723 ceph_mdsc_get_request(nextreq);
3724 mutex_unlock(&mdsc->mutex);
3725 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3726 req->r_tid, want_tid);
3727 wait_for_completion(&req->r_safe_completion);
3728 mutex_lock(&mdsc->mutex);
3729 ceph_mdsc_put_request(req);
3731 break; /* next dne before, so we're done! */
3732 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3733 /* next request was removed from tree */
3734 ceph_mdsc_put_request(nextreq);
3737 ceph_mdsc_put_request(nextreq); /* won't go away */
3741 mutex_unlock(&mdsc->mutex);
3742 dout("wait_unsafe_requests done\n");
3745 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3747 u64 want_tid, want_flush;
3749 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
3753 mutex_lock(&mdsc->mutex);
3754 want_tid = mdsc->last_tid;
3755 mutex_unlock(&mdsc->mutex);
3757 ceph_flush_dirty_caps(mdsc);
3758 spin_lock(&mdsc->cap_dirty_lock);
3759 want_flush = mdsc->last_cap_flush_tid;
3760 if (!list_empty(&mdsc->cap_flush_list)) {
3761 struct ceph_cap_flush *cf =
3762 list_last_entry(&mdsc->cap_flush_list,
3763 struct ceph_cap_flush, g_list);
3766 spin_unlock(&mdsc->cap_dirty_lock);
3768 dout("sync want tid %lld flush_seq %lld\n",
3769 want_tid, want_flush);
3771 wait_unsafe_requests(mdsc, want_tid);
3772 wait_caps_flush(mdsc, want_flush);
3776 * true if all sessions are closed, or we force unmount
3778 static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
3780 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
3782 return atomic_read(&mdsc->num_sessions) <= skipped;
3786 * called after sb is ro.
3788 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3790 struct ceph_options *opts = mdsc->fsc->client->options;
3791 struct ceph_mds_session *session;
3795 dout("close_sessions\n");
3797 /* close sessions */
3798 mutex_lock(&mdsc->mutex);
3799 for (i = 0; i < mdsc->max_sessions; i++) {
3800 session = __ceph_lookup_mds_session(mdsc, i);
3803 mutex_unlock(&mdsc->mutex);
3804 mutex_lock(&session->s_mutex);
3805 if (__close_session(mdsc, session) <= 0)
3807 mutex_unlock(&session->s_mutex);
3808 ceph_put_mds_session(session);
3809 mutex_lock(&mdsc->mutex);
3811 mutex_unlock(&mdsc->mutex);
3813 dout("waiting for sessions to close\n");
3814 wait_event_timeout(mdsc->session_close_wq,
3815 done_closing_sessions(mdsc, skipped),
3816 ceph_timeout_jiffies(opts->mount_timeout));
3818 /* tear down remaining sessions */
3819 mutex_lock(&mdsc->mutex);
3820 for (i = 0; i < mdsc->max_sessions; i++) {
3821 if (mdsc->sessions[i]) {
3822 session = get_session(mdsc->sessions[i]);
3823 __unregister_session(mdsc, session);
3824 mutex_unlock(&mdsc->mutex);
3825 mutex_lock(&session->s_mutex);
3826 remove_session_caps(session);
3827 mutex_unlock(&session->s_mutex);
3828 ceph_put_mds_session(session);
3829 mutex_lock(&mdsc->mutex);
3832 WARN_ON(!list_empty(&mdsc->cap_delay_list));
3833 mutex_unlock(&mdsc->mutex);
3835 ceph_cleanup_empty_realms(mdsc);
3837 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3842 void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
3844 struct ceph_mds_session *session;
3847 dout("force umount\n");
3849 mutex_lock(&mdsc->mutex);
3850 for (mds = 0; mds < mdsc->max_sessions; mds++) {
3851 session = __ceph_lookup_mds_session(mdsc, mds);
3854 mutex_unlock(&mdsc->mutex);
3855 mutex_lock(&session->s_mutex);
3856 __close_session(mdsc, session);
3857 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
3858 cleanup_session_requests(mdsc, session);
3859 remove_session_caps(session);
3861 mutex_unlock(&session->s_mutex);
3862 ceph_put_mds_session(session);
3863 mutex_lock(&mdsc->mutex);
3864 kick_requests(mdsc, mds);
3866 __wake_requests(mdsc, &mdsc->waiting_for_map);
3867 mutex_unlock(&mdsc->mutex);
3870 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
3873 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3875 ceph_mdsmap_destroy(mdsc->mdsmap);
3876 kfree(mdsc->sessions);
3877 ceph_caps_finalize(mdsc);
3878 ceph_pool_perm_destroy(mdsc);
3881 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3883 struct ceph_mds_client *mdsc = fsc->mdsc;
3884 dout("mdsc_destroy %p\n", mdsc);
3889 /* flush out any connection work with references to us */
3892 ceph_mdsc_stop(mdsc);
3896 dout("mdsc_destroy %p done\n", mdsc);
3899 void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3901 struct ceph_fs_client *fsc = mdsc->fsc;
3902 const char *mds_namespace = fsc->mount_options->mds_namespace;
3903 void *p = msg->front.iov_base;
3904 void *end = p + msg->front.iov_len;
3908 u32 mount_fscid = (u32)-1;
3909 u8 struct_v, struct_cv;
3912 ceph_decode_need(&p, end, sizeof(u32), bad);
3913 epoch = ceph_decode_32(&p);
3915 dout("handle_fsmap epoch %u\n", epoch);
3917 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
3918 struct_v = ceph_decode_8(&p);
3919 struct_cv = ceph_decode_8(&p);
3920 map_len = ceph_decode_32(&p);
3922 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
3923 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
3925 num_fs = ceph_decode_32(&p);
3926 while (num_fs-- > 0) {
3927 void *info_p, *info_end;
3932 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
3933 info_v = ceph_decode_8(&p);
3934 info_cv = ceph_decode_8(&p);
3935 info_len = ceph_decode_32(&p);
3936 ceph_decode_need(&p, end, info_len, bad);
3938 info_end = p + info_len;
3941 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
3942 fscid = ceph_decode_32(&info_p);
3943 namelen = ceph_decode_32(&info_p);
3944 ceph_decode_need(&info_p, info_end, namelen, bad);
3946 if (mds_namespace &&
3947 strlen(mds_namespace) == namelen &&
3948 !strncmp(mds_namespace, (char *)info_p, namelen)) {
3949 mount_fscid = fscid;
3954 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
3955 if (mount_fscid != (u32)-1) {
3956 fsc->client->monc.fs_cluster_id = mount_fscid;
3957 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
3959 ceph_monc_renew_subs(&fsc->client->monc);
3967 pr_err("error decoding fsmap\n");
3969 mutex_lock(&mdsc->mutex);
3970 mdsc->mdsmap_err = err;
3971 __wake_requests(mdsc, &mdsc->waiting_for_map);
3972 mutex_unlock(&mdsc->mutex);
3976 * handle mds map update.
3978 void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3982 void *p = msg->front.iov_base;
3983 void *end = p + msg->front.iov_len;
3984 struct ceph_mdsmap *newmap, *oldmap;
3985 struct ceph_fsid fsid;
3988 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3989 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3990 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
3992 epoch = ceph_decode_32(&p);
3993 maplen = ceph_decode_32(&p);
3994 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3996 /* do we need it? */
3997 mutex_lock(&mdsc->mutex);
3998 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3999 dout("handle_map epoch %u <= our %u\n",
4000 epoch, mdsc->mdsmap->m_epoch);
4001 mutex_unlock(&mdsc->mutex);
4005 newmap = ceph_mdsmap_decode(&p, end);
4006 if (IS_ERR(newmap)) {
4007 err = PTR_ERR(newmap);
4011 /* swap into place */
4013 oldmap = mdsc->mdsmap;
4014 mdsc->mdsmap = newmap;
4015 check_new_map(mdsc, newmap, oldmap);
4016 ceph_mdsmap_destroy(oldmap);
4018 mdsc->mdsmap = newmap; /* first mds map */
4020 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
4023 __wake_requests(mdsc, &mdsc->waiting_for_map);
4024 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
4025 mdsc->mdsmap->m_epoch);
4027 mutex_unlock(&mdsc->mutex);
4028 schedule_delayed(mdsc);
4032 mutex_unlock(&mdsc->mutex);
4034 pr_err("error decoding mdsmap %d\n", err);
4038 static struct ceph_connection *con_get(struct ceph_connection *con)
4040 struct ceph_mds_session *s = con->private;
4042 if (get_session(s)) {
4043 dout("mdsc con_get %p ok (%d)\n", s, refcount_read(&s->s_ref));
4046 dout("mdsc con_get %p FAIL\n", s);
4050 static void con_put(struct ceph_connection *con)
4052 struct ceph_mds_session *s = con->private;
4054 dout("mdsc con_put %p (%d)\n", s, refcount_read(&s->s_ref) - 1);
4055 ceph_put_mds_session(s);
4059 * if the client is unresponsive for long enough, the mds will kill
4060 * the session entirely.
4062 static void peer_reset(struct ceph_connection *con)
4064 struct ceph_mds_session *s = con->private;
4065 struct ceph_mds_client *mdsc = s->s_mdsc;
4067 pr_warn("mds%d closed our session\n", s->s_mds);
4068 send_mds_reconnect(mdsc, s);
4071 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4073 struct ceph_mds_session *s = con->private;
4074 struct ceph_mds_client *mdsc = s->s_mdsc;
4075 int type = le16_to_cpu(msg->hdr.type);
4077 mutex_lock(&mdsc->mutex);
4078 if (__verify_registered_session(mdsc, s) < 0) {
4079 mutex_unlock(&mdsc->mutex);
4082 mutex_unlock(&mdsc->mutex);
4085 case CEPH_MSG_MDS_MAP:
4086 ceph_mdsc_handle_mdsmap(mdsc, msg);
4088 case CEPH_MSG_FS_MAP_USER:
4089 ceph_mdsc_handle_fsmap(mdsc, msg);
4091 case CEPH_MSG_CLIENT_SESSION:
4092 handle_session(s, msg);
4094 case CEPH_MSG_CLIENT_REPLY:
4095 handle_reply(s, msg);
4097 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
4098 handle_forward(mdsc, s, msg);
4100 case CEPH_MSG_CLIENT_CAPS:
4101 ceph_handle_caps(s, msg);
4103 case CEPH_MSG_CLIENT_SNAP:
4104 ceph_handle_snap(mdsc, s, msg);
4106 case CEPH_MSG_CLIENT_LEASE:
4107 handle_lease(mdsc, s, msg);
4109 case CEPH_MSG_CLIENT_QUOTA:
4110 ceph_handle_quota(mdsc, s, msg);
4114 pr_err("received unknown message type %d %s\n", type,
4115 ceph_msg_type_name(type));
4126 * Note: returned pointer is the address of a structure that's
4127 * managed separately. Caller must *not* attempt to free it.
4129 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
4130 int *proto, int force_new)
4132 struct ceph_mds_session *s = con->private;
4133 struct ceph_mds_client *mdsc = s->s_mdsc;
4134 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4135 struct ceph_auth_handshake *auth = &s->s_auth;
4137 if (force_new && auth->authorizer) {
4138 ceph_auth_destroy_authorizer(auth->authorizer);
4139 auth->authorizer = NULL;
4141 if (!auth->authorizer) {
4142 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4145 return ERR_PTR(ret);
4147 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4150 return ERR_PTR(ret);
4152 *proto = ac->protocol;
4157 static int add_authorizer_challenge(struct ceph_connection *con,
4158 void *challenge_buf, int challenge_buf_len)
4160 struct ceph_mds_session *s = con->private;
4161 struct ceph_mds_client *mdsc = s->s_mdsc;
4162 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4164 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
4165 challenge_buf, challenge_buf_len);
4168 static int verify_authorizer_reply(struct ceph_connection *con)
4170 struct ceph_mds_session *s = con->private;
4171 struct ceph_mds_client *mdsc = s->s_mdsc;
4172 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4174 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
4177 static int invalidate_authorizer(struct ceph_connection *con)
4179 struct ceph_mds_session *s = con->private;
4180 struct ceph_mds_client *mdsc = s->s_mdsc;
4181 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4183 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
4185 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
4188 static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
4189 struct ceph_msg_header *hdr, int *skip)
4191 struct ceph_msg *msg;
4192 int type = (int) le16_to_cpu(hdr->type);
4193 int front_len = (int) le32_to_cpu(hdr->front_len);
4199 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
4201 pr_err("unable to allocate msg type %d len %d\n",
4209 static int mds_sign_message(struct ceph_msg *msg)
4211 struct ceph_mds_session *s = msg->con->private;
4212 struct ceph_auth_handshake *auth = &s->s_auth;
4214 return ceph_auth_sign_message(auth, msg);
4217 static int mds_check_message_signature(struct ceph_msg *msg)
4219 struct ceph_mds_session *s = msg->con->private;
4220 struct ceph_auth_handshake *auth = &s->s_auth;
4222 return ceph_auth_check_message_signature(auth, msg);
4225 static const struct ceph_connection_operations mds_con_ops = {
4228 .dispatch = dispatch,
4229 .get_authorizer = get_authorizer,
4230 .add_authorizer_challenge = add_authorizer_challenge,
4231 .verify_authorizer_reply = verify_authorizer_reply,
4232 .invalidate_authorizer = invalidate_authorizer,
4233 .peer_reset = peer_reset,
4234 .alloc_msg = mds_alloc_msg,
4235 .sign_message = mds_sign_message,
4236 .check_message_signature = mds_check_message_signature,