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);
905 static void encode_supported_features(void **p, void *end)
907 static const unsigned char bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
908 static const size_t count = ARRAY_SIZE(bits);
912 size_t size = ((size_t)bits[count - 1] + 64) / 64 * 8;
914 BUG_ON(*p + 4 + size > end);
915 ceph_encode_32(p, size);
917 for (i = 0; i < count; i++)
918 ((unsigned char*)(*p))[i / 8] |= 1 << (bits[i] % 8);
921 BUG_ON(*p + 4 > end);
922 ceph_encode_32(p, 0);
927 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
928 * to include additional client metadata fields.
930 static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
932 struct ceph_msg *msg;
933 struct ceph_mds_session_head *h;
936 int metadata_key_count = 0;
937 struct ceph_options *opt = mdsc->fsc->client->options;
938 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
941 const char* metadata[][2] = {
942 {"hostname", mdsc->nodename},
943 {"kernel_version", init_utsname()->release},
944 {"entity_id", opt->name ? : ""},
945 {"root", fsopt->server_path ? : "/"},
949 /* Calculate serialized length of metadata */
950 extra_bytes = 4; /* map length */
951 for (i = 0; metadata[i][0]; ++i) {
952 extra_bytes += 8 + strlen(metadata[i][0]) +
953 strlen(metadata[i][1]);
954 metadata_key_count++;
956 /* supported feature */
957 extra_bytes += 4 + 8;
959 /* Allocate the message */
960 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
963 pr_err("create_session_msg ENOMEM creating msg\n");
966 p = msg->front.iov_base;
967 end = p + msg->front.iov_len;
970 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
971 h->seq = cpu_to_le64(seq);
974 * Serialize client metadata into waiting buffer space, using
975 * the format that userspace expects for map<string, string>
977 * ClientSession messages with metadata are v2
979 msg->hdr.version = cpu_to_le16(3);
980 msg->hdr.compat_version = cpu_to_le16(1);
982 /* The write pointer, following the session_head structure */
985 /* Number of entries in the map */
986 ceph_encode_32(&p, metadata_key_count);
988 /* Two length-prefixed strings for each entry in the map */
989 for (i = 0; metadata[i][0]; ++i) {
990 size_t const key_len = strlen(metadata[i][0]);
991 size_t const val_len = strlen(metadata[i][1]);
993 ceph_encode_32(&p, key_len);
994 memcpy(p, metadata[i][0], key_len);
996 ceph_encode_32(&p, val_len);
997 memcpy(p, metadata[i][1], val_len);
1001 encode_supported_features(&p, end);
1002 msg->front.iov_len = p - msg->front.iov_base;
1003 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1009 * send session open request.
1011 * called under mdsc->mutex
1013 static int __open_session(struct ceph_mds_client *mdsc,
1014 struct ceph_mds_session *session)
1016 struct ceph_msg *msg;
1018 int mds = session->s_mds;
1020 /* wait for mds to go active? */
1021 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
1022 dout("open_session to mds%d (%s)\n", mds,
1023 ceph_mds_state_name(mstate));
1024 session->s_state = CEPH_MDS_SESSION_OPENING;
1025 session->s_renew_requested = jiffies;
1027 /* send connect message */
1028 msg = create_session_open_msg(mdsc, session->s_seq);
1031 ceph_con_send(&session->s_con, msg);
1036 * open sessions for any export targets for the given mds
1038 * called under mdsc->mutex
1040 static struct ceph_mds_session *
1041 __open_export_target_session(struct ceph_mds_client *mdsc, int target)
1043 struct ceph_mds_session *session;
1045 session = __ceph_lookup_mds_session(mdsc, target);
1047 session = register_session(mdsc, target);
1048 if (IS_ERR(session))
1051 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1052 session->s_state == CEPH_MDS_SESSION_CLOSING)
1053 __open_session(mdsc, session);
1058 struct ceph_mds_session *
1059 ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1061 struct ceph_mds_session *session;
1063 dout("open_export_target_session to mds%d\n", target);
1065 mutex_lock(&mdsc->mutex);
1066 session = __open_export_target_session(mdsc, target);
1067 mutex_unlock(&mdsc->mutex);
1072 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1073 struct ceph_mds_session *session)
1075 struct ceph_mds_info *mi;
1076 struct ceph_mds_session *ts;
1077 int i, mds = session->s_mds;
1079 if (mds >= mdsc->mdsmap->m_num_mds)
1082 mi = &mdsc->mdsmap->m_info[mds];
1083 dout("open_export_target_sessions for mds%d (%d targets)\n",
1084 session->s_mds, mi->num_export_targets);
1086 for (i = 0; i < mi->num_export_targets; i++) {
1087 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1089 ceph_put_mds_session(ts);
1093 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1094 struct ceph_mds_session *session)
1096 mutex_lock(&mdsc->mutex);
1097 __open_export_target_sessions(mdsc, session);
1098 mutex_unlock(&mdsc->mutex);
1105 static void detach_cap_releases(struct ceph_mds_session *session,
1106 struct list_head *target)
1108 lockdep_assert_held(&session->s_cap_lock);
1110 list_splice_init(&session->s_cap_releases, target);
1111 session->s_num_cap_releases = 0;
1112 dout("dispose_cap_releases mds%d\n", session->s_mds);
1115 static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1116 struct list_head *dispose)
1118 while (!list_empty(dispose)) {
1119 struct ceph_cap *cap;
1120 /* zero out the in-progress message */
1121 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
1122 list_del(&cap->session_caps);
1123 ceph_put_cap(mdsc, cap);
1127 static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1128 struct ceph_mds_session *session)
1130 struct ceph_mds_request *req;
1133 dout("cleanup_session_requests mds%d\n", session->s_mds);
1134 mutex_lock(&mdsc->mutex);
1135 while (!list_empty(&session->s_unsafe)) {
1136 req = list_first_entry(&session->s_unsafe,
1137 struct ceph_mds_request, r_unsafe_item);
1138 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1140 __unregister_request(mdsc, req);
1142 /* zero r_attempts, so kick_requests() will re-send requests */
1143 p = rb_first(&mdsc->request_tree);
1145 req = rb_entry(p, struct ceph_mds_request, r_node);
1147 if (req->r_session &&
1148 req->r_session->s_mds == session->s_mds)
1149 req->r_attempts = 0;
1151 mutex_unlock(&mdsc->mutex);
1155 * Helper to safely iterate over all caps associated with a session, with
1156 * special care taken to handle a racing __ceph_remove_cap().
1158 * Caller must hold session s_mutex.
1160 static int iterate_session_caps(struct ceph_mds_session *session,
1161 int (*cb)(struct inode *, struct ceph_cap *,
1164 struct list_head *p;
1165 struct ceph_cap *cap;
1166 struct inode *inode, *last_inode = NULL;
1167 struct ceph_cap *old_cap = NULL;
1170 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1171 spin_lock(&session->s_cap_lock);
1172 p = session->s_caps.next;
1173 while (p != &session->s_caps) {
1174 cap = list_entry(p, struct ceph_cap, session_caps);
1175 inode = igrab(&cap->ci->vfs_inode);
1180 session->s_cap_iterator = cap;
1181 spin_unlock(&session->s_cap_lock);
1188 ceph_put_cap(session->s_mdsc, old_cap);
1192 ret = cb(inode, cap, arg);
1195 spin_lock(&session->s_cap_lock);
1198 dout("iterate_session_caps finishing cap %p removal\n",
1200 BUG_ON(cap->session != session);
1201 cap->session = NULL;
1202 list_del_init(&cap->session_caps);
1203 session->s_nr_caps--;
1204 if (cap->queue_release) {
1205 list_add_tail(&cap->session_caps,
1206 &session->s_cap_releases);
1207 session->s_num_cap_releases++;
1209 old_cap = cap; /* put_cap it w/o locks held */
1217 session->s_cap_iterator = NULL;
1218 spin_unlock(&session->s_cap_lock);
1222 ceph_put_cap(session->s_mdsc, old_cap);
1227 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
1230 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
1231 struct ceph_inode_info *ci = ceph_inode(inode);
1232 LIST_HEAD(to_remove);
1234 bool invalidate = false;
1236 dout("removing cap %p, ci is %p, inode is %p\n",
1237 cap, ci, &ci->vfs_inode);
1238 spin_lock(&ci->i_ceph_lock);
1239 __ceph_remove_cap(cap, false);
1240 if (!ci->i_auth_cap) {
1241 struct ceph_cap_flush *cf;
1242 struct ceph_mds_client *mdsc = fsc->mdsc;
1244 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1246 if (ci->i_wrbuffer_ref > 0 &&
1247 READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
1250 while (!list_empty(&ci->i_cap_flush_list)) {
1251 cf = list_first_entry(&ci->i_cap_flush_list,
1252 struct ceph_cap_flush, i_list);
1253 list_move(&cf->i_list, &to_remove);
1256 spin_lock(&mdsc->cap_dirty_lock);
1258 list_for_each_entry(cf, &to_remove, i_list)
1259 list_del(&cf->g_list);
1261 if (!list_empty(&ci->i_dirty_item)) {
1262 pr_warn_ratelimited(
1263 " dropping dirty %s state for %p %lld\n",
1264 ceph_cap_string(ci->i_dirty_caps),
1265 inode, ceph_ino(inode));
1266 ci->i_dirty_caps = 0;
1267 list_del_init(&ci->i_dirty_item);
1270 if (!list_empty(&ci->i_flushing_item)) {
1271 pr_warn_ratelimited(
1272 " dropping dirty+flushing %s state for %p %lld\n",
1273 ceph_cap_string(ci->i_flushing_caps),
1274 inode, ceph_ino(inode));
1275 ci->i_flushing_caps = 0;
1276 list_del_init(&ci->i_flushing_item);
1277 mdsc->num_cap_flushing--;
1280 spin_unlock(&mdsc->cap_dirty_lock);
1282 if (atomic_read(&ci->i_filelock_ref) > 0) {
1283 /* make further file lock syscall return -EIO */
1284 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1285 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1286 inode, ceph_ino(inode));
1289 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
1290 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
1291 ci->i_prealloc_cap_flush = NULL;
1294 spin_unlock(&ci->i_ceph_lock);
1295 while (!list_empty(&to_remove)) {
1296 struct ceph_cap_flush *cf;
1297 cf = list_first_entry(&to_remove,
1298 struct ceph_cap_flush, i_list);
1299 list_del(&cf->i_list);
1300 ceph_free_cap_flush(cf);
1303 wake_up_all(&ci->i_cap_wq);
1305 ceph_queue_invalidate(inode);
1312 * caller must hold session s_mutex
1314 static void remove_session_caps(struct ceph_mds_session *session)
1316 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1317 struct super_block *sb = fsc->sb;
1320 dout("remove_session_caps on %p\n", session);
1321 iterate_session_caps(session, remove_session_caps_cb, fsc);
1323 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1325 spin_lock(&session->s_cap_lock);
1326 if (session->s_nr_caps > 0) {
1327 struct inode *inode;
1328 struct ceph_cap *cap, *prev = NULL;
1329 struct ceph_vino vino;
1331 * iterate_session_caps() skips inodes that are being
1332 * deleted, we need to wait until deletions are complete.
1333 * __wait_on_freeing_inode() is designed for the job,
1334 * but it is not exported, so use lookup inode function
1337 while (!list_empty(&session->s_caps)) {
1338 cap = list_entry(session->s_caps.next,
1339 struct ceph_cap, session_caps);
1343 vino = cap->ci->i_vino;
1344 spin_unlock(&session->s_cap_lock);
1346 inode = ceph_find_inode(sb, vino);
1349 spin_lock(&session->s_cap_lock);
1353 // drop cap expires and unlock s_cap_lock
1354 detach_cap_releases(session, &dispose);
1356 BUG_ON(session->s_nr_caps > 0);
1357 BUG_ON(!list_empty(&session->s_cap_flushing));
1358 spin_unlock(&session->s_cap_lock);
1359 dispose_cap_releases(session->s_mdsc, &dispose);
1363 * wake up any threads waiting on this session's caps. if the cap is
1364 * old (didn't get renewed on the client reconnect), remove it now.
1366 * caller must hold s_mutex.
1368 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1371 struct ceph_inode_info *ci = ceph_inode(inode);
1374 spin_lock(&ci->i_ceph_lock);
1375 ci->i_wanted_max_size = 0;
1376 ci->i_requested_max_size = 0;
1377 spin_unlock(&ci->i_ceph_lock);
1379 wake_up_all(&ci->i_cap_wq);
1383 static void wake_up_session_caps(struct ceph_mds_session *session,
1386 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1387 iterate_session_caps(session, wake_up_session_cb,
1388 (void *)(unsigned long)reconnect);
1392 * Send periodic message to MDS renewing all currently held caps. The
1393 * ack will reset the expiration for all caps from this session.
1395 * caller holds s_mutex
1397 static int send_renew_caps(struct ceph_mds_client *mdsc,
1398 struct ceph_mds_session *session)
1400 struct ceph_msg *msg;
1403 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1404 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1405 pr_info("mds%d caps stale\n", session->s_mds);
1406 session->s_renew_requested = jiffies;
1408 /* do not try to renew caps until a recovering mds has reconnected
1409 * with its clients. */
1410 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1411 if (state < CEPH_MDS_STATE_RECONNECT) {
1412 dout("send_renew_caps ignoring mds%d (%s)\n",
1413 session->s_mds, ceph_mds_state_name(state));
1417 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1418 ceph_mds_state_name(state));
1419 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1420 ++session->s_renew_seq);
1423 ceph_con_send(&session->s_con, msg);
1427 static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1428 struct ceph_mds_session *session, u64 seq)
1430 struct ceph_msg *msg;
1432 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1433 session->s_mds, ceph_session_state_name(session->s_state), seq);
1434 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1437 ceph_con_send(&session->s_con, msg);
1443 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1445 * Called under session->s_mutex
1447 static void renewed_caps(struct ceph_mds_client *mdsc,
1448 struct ceph_mds_session *session, int is_renew)
1453 spin_lock(&session->s_cap_lock);
1454 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
1456 session->s_cap_ttl = session->s_renew_requested +
1457 mdsc->mdsmap->m_session_timeout*HZ;
1460 if (time_before(jiffies, session->s_cap_ttl)) {
1461 pr_info("mds%d caps renewed\n", session->s_mds);
1464 pr_info("mds%d caps still stale\n", session->s_mds);
1467 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1468 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1469 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1470 spin_unlock(&session->s_cap_lock);
1473 wake_up_session_caps(session, 0);
1477 * send a session close request
1479 static int request_close_session(struct ceph_mds_client *mdsc,
1480 struct ceph_mds_session *session)
1482 struct ceph_msg *msg;
1484 dout("request_close_session mds%d state %s seq %lld\n",
1485 session->s_mds, ceph_session_state_name(session->s_state),
1487 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1490 ceph_con_send(&session->s_con, msg);
1495 * Called with s_mutex held.
1497 static int __close_session(struct ceph_mds_client *mdsc,
1498 struct ceph_mds_session *session)
1500 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1502 session->s_state = CEPH_MDS_SESSION_CLOSING;
1503 return request_close_session(mdsc, session);
1506 static bool drop_negative_children(struct dentry *dentry)
1508 struct dentry *child;
1509 bool all_negative = true;
1511 if (!d_is_dir(dentry))
1514 spin_lock(&dentry->d_lock);
1515 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1516 if (d_really_is_positive(child)) {
1517 all_negative = false;
1521 spin_unlock(&dentry->d_lock);
1524 shrink_dcache_parent(dentry);
1526 return all_negative;
1530 * Trim old(er) caps.
1532 * Because we can't cache an inode without one or more caps, we do
1533 * this indirectly: if a cap is unused, we prune its aliases, at which
1534 * point the inode will hopefully get dropped to.
1536 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1537 * memory pressure from the MDS, though, so it needn't be perfect.
1539 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1541 struct ceph_mds_session *session = arg;
1542 struct ceph_inode_info *ci = ceph_inode(inode);
1543 int used, wanted, oissued, mine;
1545 if (session->s_trim_caps <= 0)
1548 spin_lock(&ci->i_ceph_lock);
1549 mine = cap->issued | cap->implemented;
1550 used = __ceph_caps_used(ci);
1551 wanted = __ceph_caps_file_wanted(ci);
1552 oissued = __ceph_caps_issued_other(ci, cap);
1554 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1555 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1556 ceph_cap_string(used), ceph_cap_string(wanted));
1557 if (cap == ci->i_auth_cap) {
1558 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1559 !list_empty(&ci->i_cap_snaps))
1561 if ((used | wanted) & CEPH_CAP_ANY_WR)
1563 /* Note: it's possible that i_filelock_ref becomes non-zero
1564 * after dropping auth caps. It doesn't hurt because reply
1565 * of lock mds request will re-add auth caps. */
1566 if (atomic_read(&ci->i_filelock_ref) > 0)
1569 /* The inode has cached pages, but it's no longer used.
1570 * we can safely drop it */
1571 if (wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
1572 !(oissued & CEPH_CAP_FILE_CACHE)) {
1576 if ((used | wanted) & ~oissued & mine)
1577 goto out; /* we need these caps */
1580 /* we aren't the only cap.. just remove us */
1581 __ceph_remove_cap(cap, true);
1582 session->s_trim_caps--;
1584 struct dentry *dentry;
1585 /* try dropping referring dentries */
1586 spin_unlock(&ci->i_ceph_lock);
1587 dentry = d_find_any_alias(inode);
1588 if (dentry && drop_negative_children(dentry)) {
1591 d_prune_aliases(inode);
1592 count = atomic_read(&inode->i_count);
1594 session->s_trim_caps--;
1595 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1604 spin_unlock(&ci->i_ceph_lock);
1609 * Trim session cap count down to some max number.
1611 int ceph_trim_caps(struct ceph_mds_client *mdsc,
1612 struct ceph_mds_session *session,
1615 int trim_caps = session->s_nr_caps - max_caps;
1617 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1618 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1619 if (trim_caps > 0) {
1620 session->s_trim_caps = trim_caps;
1621 iterate_session_caps(session, trim_caps_cb, session);
1622 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1623 session->s_mds, session->s_nr_caps, max_caps,
1624 trim_caps - session->s_trim_caps);
1625 session->s_trim_caps = 0;
1628 ceph_send_cap_releases(mdsc, session);
1632 static int check_caps_flush(struct ceph_mds_client *mdsc,
1637 spin_lock(&mdsc->cap_dirty_lock);
1638 if (!list_empty(&mdsc->cap_flush_list)) {
1639 struct ceph_cap_flush *cf =
1640 list_first_entry(&mdsc->cap_flush_list,
1641 struct ceph_cap_flush, g_list);
1642 if (cf->tid <= want_flush_tid) {
1643 dout("check_caps_flush still flushing tid "
1644 "%llu <= %llu\n", cf->tid, want_flush_tid);
1648 spin_unlock(&mdsc->cap_dirty_lock);
1653 * flush all dirty inode data to disk.
1655 * returns true if we've flushed through want_flush_tid
1657 static void wait_caps_flush(struct ceph_mds_client *mdsc,
1660 dout("check_caps_flush want %llu\n", want_flush_tid);
1662 wait_event(mdsc->cap_flushing_wq,
1663 check_caps_flush(mdsc, want_flush_tid));
1665 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
1669 * called under s_mutex
1671 void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1672 struct ceph_mds_session *session)
1674 struct ceph_msg *msg = NULL;
1675 struct ceph_mds_cap_release *head;
1676 struct ceph_mds_cap_item *item;
1677 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
1678 struct ceph_cap *cap;
1679 LIST_HEAD(tmp_list);
1680 int num_cap_releases;
1681 __le32 barrier, *cap_barrier;
1683 down_read(&osdc->lock);
1684 barrier = cpu_to_le32(osdc->epoch_barrier);
1685 up_read(&osdc->lock);
1687 spin_lock(&session->s_cap_lock);
1689 list_splice_init(&session->s_cap_releases, &tmp_list);
1690 num_cap_releases = session->s_num_cap_releases;
1691 session->s_num_cap_releases = 0;
1692 spin_unlock(&session->s_cap_lock);
1694 while (!list_empty(&tmp_list)) {
1696 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
1697 PAGE_SIZE, GFP_NOFS, false);
1700 head = msg->front.iov_base;
1701 head->num = cpu_to_le32(0);
1702 msg->front.iov_len = sizeof(*head);
1704 msg->hdr.version = cpu_to_le16(2);
1705 msg->hdr.compat_version = cpu_to_le16(1);
1708 cap = list_first_entry(&tmp_list, struct ceph_cap,
1710 list_del(&cap->session_caps);
1713 head = msg->front.iov_base;
1714 le32_add_cpu(&head->num, 1);
1715 item = msg->front.iov_base + msg->front.iov_len;
1716 item->ino = cpu_to_le64(cap->cap_ino);
1717 item->cap_id = cpu_to_le64(cap->cap_id);
1718 item->migrate_seq = cpu_to_le32(cap->mseq);
1719 item->seq = cpu_to_le32(cap->issue_seq);
1720 msg->front.iov_len += sizeof(*item);
1722 ceph_put_cap(mdsc, cap);
1724 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
1725 // Append cap_barrier field
1726 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1727 *cap_barrier = barrier;
1728 msg->front.iov_len += sizeof(*cap_barrier);
1730 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1731 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1732 ceph_con_send(&session->s_con, msg);
1737 BUG_ON(num_cap_releases != 0);
1739 spin_lock(&session->s_cap_lock);
1740 if (!list_empty(&session->s_cap_releases))
1742 spin_unlock(&session->s_cap_lock);
1745 // Append cap_barrier field
1746 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1747 *cap_barrier = barrier;
1748 msg->front.iov_len += sizeof(*cap_barrier);
1750 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1751 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1752 ceph_con_send(&session->s_con, msg);
1756 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1758 spin_lock(&session->s_cap_lock);
1759 list_splice(&tmp_list, &session->s_cap_releases);
1760 session->s_num_cap_releases += num_cap_releases;
1761 spin_unlock(&session->s_cap_lock);
1768 int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1771 struct ceph_inode_info *ci = ceph_inode(dir);
1772 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1773 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
1774 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
1775 int order, num_entries;
1777 spin_lock(&ci->i_ceph_lock);
1778 num_entries = ci->i_files + ci->i_subdirs;
1779 spin_unlock(&ci->i_ceph_lock);
1780 num_entries = max(num_entries, 1);
1781 num_entries = min(num_entries, opt->max_readdir);
1783 order = get_order(size * num_entries);
1784 while (order >= 0) {
1785 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
1788 if (rinfo->dir_entries)
1792 if (!rinfo->dir_entries)
1795 num_entries = (PAGE_SIZE << order) / size;
1796 num_entries = min(num_entries, opt->max_readdir);
1798 rinfo->dir_buf_size = PAGE_SIZE << order;
1799 req->r_num_caps = num_entries + 1;
1800 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1801 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1806 * Create an mds request.
1808 struct ceph_mds_request *
1809 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1811 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1812 struct timespec64 ts;
1815 return ERR_PTR(-ENOMEM);
1817 mutex_init(&req->r_fill_mutex);
1819 req->r_started = jiffies;
1820 req->r_resend_mds = -1;
1821 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1822 INIT_LIST_HEAD(&req->r_unsafe_target_item);
1824 kref_init(&req->r_kref);
1825 RB_CLEAR_NODE(&req->r_node);
1826 INIT_LIST_HEAD(&req->r_wait);
1827 init_completion(&req->r_completion);
1828 init_completion(&req->r_safe_completion);
1829 INIT_LIST_HEAD(&req->r_unsafe_item);
1831 ktime_get_coarse_real_ts64(&ts);
1832 req->r_stamp = timespec64_trunc(ts, mdsc->fsc->sb->s_time_gran);
1835 req->r_direct_mode = mode;
1840 * return oldest (lowest) request, tid in request tree, 0 if none.
1842 * called under mdsc->mutex.
1844 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1846 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1848 return rb_entry(rb_first(&mdsc->request_tree),
1849 struct ceph_mds_request, r_node);
1852 static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1854 return mdsc->oldest_tid;
1858 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1859 * on build_path_from_dentry in fs/cifs/dir.c.
1861 * If @stop_on_nosnap, generate path relative to the first non-snapped
1864 * Encode hidden .snap dirs as a double /, i.e.
1865 * foo/.snap/bar -> foo//bar
1867 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1870 struct dentry *temp;
1876 return ERR_PTR(-EINVAL);
1880 seq = read_seqbegin(&rename_lock);
1882 for (temp = dentry; !IS_ROOT(temp);) {
1883 struct inode *inode = d_inode(temp);
1884 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1885 len++; /* slash only */
1886 else if (stop_on_nosnap && inode &&
1887 ceph_snap(inode) == CEPH_NOSNAP)
1890 len += 1 + temp->d_name.len;
1891 temp = temp->d_parent;
1895 len--; /* no leading '/' */
1897 path = kmalloc(len+1, GFP_NOFS);
1899 return ERR_PTR(-ENOMEM);
1901 path[pos] = 0; /* trailing null */
1903 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1904 struct inode *inode;
1906 spin_lock(&temp->d_lock);
1907 inode = d_inode(temp);
1908 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1909 dout("build_path path+%d: %p SNAPDIR\n",
1911 } else if (stop_on_nosnap && inode &&
1912 ceph_snap(inode) == CEPH_NOSNAP) {
1913 spin_unlock(&temp->d_lock);
1916 pos -= temp->d_name.len;
1918 spin_unlock(&temp->d_lock);
1921 strncpy(path + pos, temp->d_name.name,
1924 spin_unlock(&temp->d_lock);
1927 temp = temp->d_parent;
1930 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
1931 pr_err("build_path did not end path lookup where "
1932 "expected, namelen is %d, pos is %d\n", len, pos);
1933 /* presumably this is only possible if racing with a
1934 rename of one of the parent directories (we can not
1935 lock the dentries above us to prevent this, but
1936 retrying should be harmless) */
1941 *base = ceph_ino(d_inode(temp));
1943 dout("build_path on %p %d built %llx '%.*s'\n",
1944 dentry, d_count(dentry), *base, len, path);
1948 static int build_dentry_path(struct dentry *dentry, struct inode *dir,
1949 const char **ppath, int *ppathlen, u64 *pino,
1956 dir = d_inode_rcu(dentry->d_parent);
1957 if (dir && ceph_snap(dir) == CEPH_NOSNAP) {
1958 *pino = ceph_ino(dir);
1960 *ppath = dentry->d_name.name;
1961 *ppathlen = dentry->d_name.len;
1965 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1967 return PTR_ERR(path);
1973 static int build_inode_path(struct inode *inode,
1974 const char **ppath, int *ppathlen, u64 *pino,
1977 struct dentry *dentry;
1980 if (ceph_snap(inode) == CEPH_NOSNAP) {
1981 *pino = ceph_ino(inode);
1985 dentry = d_find_alias(inode);
1986 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1989 return PTR_ERR(path);
1996 * request arguments may be specified via an inode *, a dentry *, or
1997 * an explicit ino+path.
1999 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
2000 struct inode *rdiri, const char *rpath,
2001 u64 rino, const char **ppath, int *pathlen,
2002 u64 *ino, int *freepath)
2007 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
2008 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
2010 } else if (rdentry) {
2011 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
2013 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
2015 } else if (rpath || rino) {
2018 *pathlen = rpath ? strlen(rpath) : 0;
2019 dout(" path %.*s\n", *pathlen, rpath);
2026 * called under mdsc->mutex
2028 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
2029 struct ceph_mds_request *req,
2030 int mds, bool drop_cap_releases)
2032 struct ceph_msg *msg;
2033 struct ceph_mds_request_head *head;
2034 const char *path1 = NULL;
2035 const char *path2 = NULL;
2036 u64 ino1 = 0, ino2 = 0;
2037 int pathlen1 = 0, pathlen2 = 0;
2038 int freepath1 = 0, freepath2 = 0;
2044 ret = set_request_path_attr(req->r_inode, req->r_dentry,
2045 req->r_parent, req->r_path1, req->r_ino1.ino,
2046 &path1, &pathlen1, &ino1, &freepath1);
2052 ret = set_request_path_attr(NULL, req->r_old_dentry,
2053 req->r_old_dentry_dir,
2054 req->r_path2, req->r_ino2.ino,
2055 &path2, &pathlen2, &ino2, &freepath2);
2061 len = sizeof(*head) +
2062 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
2063 sizeof(struct ceph_timespec);
2065 /* calculate (max) length for cap releases */
2066 len += sizeof(struct ceph_mds_request_release) *
2067 (!!req->r_inode_drop + !!req->r_dentry_drop +
2068 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2069 if (req->r_dentry_drop)
2070 len += req->r_dentry->d_name.len;
2071 if (req->r_old_dentry_drop)
2072 len += req->r_old_dentry->d_name.len;
2074 msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
2076 msg = ERR_PTR(-ENOMEM);
2080 msg->hdr.version = cpu_to_le16(2);
2081 msg->hdr.tid = cpu_to_le64(req->r_tid);
2083 head = msg->front.iov_base;
2084 p = msg->front.iov_base + sizeof(*head);
2085 end = msg->front.iov_base + msg->front.iov_len;
2087 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2088 head->op = cpu_to_le32(req->r_op);
2089 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2090 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
2091 head->args = req->r_args;
2093 ceph_encode_filepath(&p, end, ino1, path1);
2094 ceph_encode_filepath(&p, end, ino2, path2);
2096 /* make note of release offset, in case we need to replay */
2097 req->r_request_release_offset = p - msg->front.iov_base;
2101 if (req->r_inode_drop)
2102 releases += ceph_encode_inode_release(&p,
2103 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
2104 mds, req->r_inode_drop, req->r_inode_unless, 0);
2105 if (req->r_dentry_drop)
2106 releases += ceph_encode_dentry_release(&p, req->r_dentry,
2107 req->r_parent, mds, req->r_dentry_drop,
2108 req->r_dentry_unless);
2109 if (req->r_old_dentry_drop)
2110 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
2111 req->r_old_dentry_dir, mds,
2112 req->r_old_dentry_drop,
2113 req->r_old_dentry_unless);
2114 if (req->r_old_inode_drop)
2115 releases += ceph_encode_inode_release(&p,
2116 d_inode(req->r_old_dentry),
2117 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
2119 if (drop_cap_releases) {
2121 p = msg->front.iov_base + req->r_request_release_offset;
2124 head->num_releases = cpu_to_le16(releases);
2128 struct ceph_timespec ts;
2129 ceph_encode_timespec64(&ts, &req->r_stamp);
2130 ceph_encode_copy(&p, &ts, sizeof(ts));
2134 msg->front.iov_len = p - msg->front.iov_base;
2135 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2137 if (req->r_pagelist) {
2138 struct ceph_pagelist *pagelist = req->r_pagelist;
2139 ceph_msg_data_add_pagelist(msg, pagelist);
2140 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2142 msg->hdr.data_len = 0;
2145 msg->hdr.data_off = cpu_to_le16(0);
2149 kfree((char *)path2);
2152 kfree((char *)path1);
2158 * called under mdsc->mutex if error, under no mutex if
2161 static void complete_request(struct ceph_mds_client *mdsc,
2162 struct ceph_mds_request *req)
2164 if (req->r_callback)
2165 req->r_callback(mdsc, req);
2167 complete_all(&req->r_completion);
2171 * called under mdsc->mutex
2173 static int __prepare_send_request(struct ceph_mds_client *mdsc,
2174 struct ceph_mds_request *req,
2175 int mds, bool drop_cap_releases)
2177 struct ceph_mds_request_head *rhead;
2178 struct ceph_msg *msg;
2183 struct ceph_cap *cap =
2184 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2187 req->r_sent_on_mseq = cap->mseq;
2189 req->r_sent_on_mseq = -1;
2191 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2192 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2194 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2197 * Replay. Do not regenerate message (and rebuild
2198 * paths, etc.); just use the original message.
2199 * Rebuilding paths will break for renames because
2200 * d_move mangles the src name.
2202 msg = req->r_request;
2203 rhead = msg->front.iov_base;
2205 flags = le32_to_cpu(rhead->flags);
2206 flags |= CEPH_MDS_FLAG_REPLAY;
2207 rhead->flags = cpu_to_le32(flags);
2209 if (req->r_target_inode)
2210 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2212 rhead->num_retry = req->r_attempts - 1;
2214 /* remove cap/dentry releases from message */
2215 rhead->num_releases = 0;
2218 p = msg->front.iov_base + req->r_request_release_offset;
2220 struct ceph_timespec ts;
2221 ceph_encode_timespec64(&ts, &req->r_stamp);
2222 ceph_encode_copy(&p, &ts, sizeof(ts));
2225 msg->front.iov_len = p - msg->front.iov_base;
2226 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2230 if (req->r_request) {
2231 ceph_msg_put(req->r_request);
2232 req->r_request = NULL;
2234 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
2236 req->r_err = PTR_ERR(msg);
2237 return PTR_ERR(msg);
2239 req->r_request = msg;
2241 rhead = msg->front.iov_base;
2242 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
2243 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2244 flags |= CEPH_MDS_FLAG_REPLAY;
2246 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2247 rhead->flags = cpu_to_le32(flags);
2248 rhead->num_fwd = req->r_num_fwd;
2249 rhead->num_retry = req->r_attempts - 1;
2252 dout(" r_parent = %p\n", req->r_parent);
2257 * send request, or put it on the appropriate wait list.
2259 static void __do_request(struct ceph_mds_client *mdsc,
2260 struct ceph_mds_request *req)
2262 struct ceph_mds_session *session = NULL;
2266 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2267 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
2268 __unregister_request(mdsc, req);
2272 if (req->r_timeout &&
2273 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2274 dout("do_request timed out\n");
2278 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
2279 dout("do_request forced umount\n");
2283 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
2284 if (mdsc->mdsmap_err) {
2285 err = mdsc->mdsmap_err;
2286 dout("do_request mdsmap err %d\n", err);
2289 if (mdsc->mdsmap->m_epoch == 0) {
2290 dout("do_request no mdsmap, waiting for map\n");
2291 list_add(&req->r_wait, &mdsc->waiting_for_map);
2294 if (!(mdsc->fsc->mount_options->flags &
2295 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2296 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
2298 pr_info("probably no mds server is up\n");
2303 put_request_session(req);
2305 mds = __choose_mds(mdsc, req);
2307 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2308 dout("do_request no mds or not active, waiting for map\n");
2309 list_add(&req->r_wait, &mdsc->waiting_for_map);
2313 /* get, open session */
2314 session = __ceph_lookup_mds_session(mdsc, mds);
2316 session = register_session(mdsc, mds);
2317 if (IS_ERR(session)) {
2318 err = PTR_ERR(session);
2322 req->r_session = get_session(session);
2324 dout("do_request mds%d session %p state %s\n", mds, session,
2325 ceph_session_state_name(session->s_state));
2326 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2327 session->s_state != CEPH_MDS_SESSION_HUNG) {
2328 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2332 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2333 session->s_state == CEPH_MDS_SESSION_CLOSING)
2334 __open_session(mdsc, session);
2335 list_add(&req->r_wait, &session->s_waiting);
2340 req->r_resend_mds = -1; /* forget any previous mds hint */
2342 if (req->r_request_started == 0) /* note request start time */
2343 req->r_request_started = jiffies;
2345 err = __prepare_send_request(mdsc, req, mds, false);
2347 ceph_msg_get(req->r_request);
2348 ceph_con_send(&session->s_con, req->r_request);
2352 ceph_put_mds_session(session);
2355 dout("__do_request early error %d\n", err);
2357 complete_request(mdsc, req);
2358 __unregister_request(mdsc, req);
2364 * called under mdsc->mutex
2366 static void __wake_requests(struct ceph_mds_client *mdsc,
2367 struct list_head *head)
2369 struct ceph_mds_request *req;
2370 LIST_HEAD(tmp_list);
2372 list_splice_init(head, &tmp_list);
2374 while (!list_empty(&tmp_list)) {
2375 req = list_entry(tmp_list.next,
2376 struct ceph_mds_request, r_wait);
2377 list_del_init(&req->r_wait);
2378 dout(" wake request %p tid %llu\n", req, req->r_tid);
2379 __do_request(mdsc, req);
2384 * Wake up threads with requests pending for @mds, so that they can
2385 * resubmit their requests to a possibly different mds.
2387 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
2389 struct ceph_mds_request *req;
2390 struct rb_node *p = rb_first(&mdsc->request_tree);
2392 dout("kick_requests mds%d\n", mds);
2394 req = rb_entry(p, struct ceph_mds_request, r_node);
2396 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2398 if (req->r_attempts > 0)
2399 continue; /* only new requests */
2400 if (req->r_session &&
2401 req->r_session->s_mds == mds) {
2402 dout(" kicking tid %llu\n", req->r_tid);
2403 list_del_init(&req->r_wait);
2404 __do_request(mdsc, req);
2409 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2410 struct ceph_mds_request *req)
2412 dout("submit_request on %p\n", req);
2413 mutex_lock(&mdsc->mutex);
2414 __register_request(mdsc, req, NULL);
2415 __do_request(mdsc, req);
2416 mutex_unlock(&mdsc->mutex);
2420 * Synchrously perform an mds request. Take care of all of the
2421 * session setup, forwarding, retry details.
2423 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2425 struct ceph_mds_request *req)
2429 dout("do_request on %p\n", req);
2431 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
2433 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
2435 ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
2436 if (req->r_old_dentry_dir)
2437 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2441 mutex_lock(&mdsc->mutex);
2442 __register_request(mdsc, req, dir);
2443 __do_request(mdsc, req);
2451 mutex_unlock(&mdsc->mutex);
2452 dout("do_request waiting\n");
2453 if (!req->r_timeout && req->r_wait_for_completion) {
2454 err = req->r_wait_for_completion(mdsc, req);
2456 long timeleft = wait_for_completion_killable_timeout(
2458 ceph_timeout_jiffies(req->r_timeout));
2462 err = -EIO; /* timed out */
2464 err = timeleft; /* killed */
2466 dout("do_request waited, got %d\n", err);
2467 mutex_lock(&mdsc->mutex);
2469 /* only abort if we didn't race with a real reply */
2470 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2471 err = le32_to_cpu(req->r_reply_info.head->result);
2472 } else if (err < 0) {
2473 dout("aborted request %lld with %d\n", req->r_tid, err);
2476 * ensure we aren't running concurrently with
2477 * ceph_fill_trace or ceph_readdir_prepopulate, which
2478 * rely on locks (dir mutex) held by our caller.
2480 mutex_lock(&req->r_fill_mutex);
2482 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
2483 mutex_unlock(&req->r_fill_mutex);
2485 if (req->r_parent &&
2486 (req->r_op & CEPH_MDS_OP_WRITE))
2487 ceph_invalidate_dir_request(req);
2493 mutex_unlock(&mdsc->mutex);
2494 dout("do_request %p done, result %d\n", req, err);
2499 * Invalidate dir's completeness, dentry lease state on an aborted MDS
2500 * namespace request.
2502 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2504 struct inode *dir = req->r_parent;
2505 struct inode *old_dir = req->r_old_dentry_dir;
2507 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
2509 ceph_dir_clear_complete(dir);
2511 ceph_dir_clear_complete(old_dir);
2513 ceph_invalidate_dentry_lease(req->r_dentry);
2514 if (req->r_old_dentry)
2515 ceph_invalidate_dentry_lease(req->r_old_dentry);
2521 * We take the session mutex and parse and process the reply immediately.
2522 * This preserves the logical ordering of replies, capabilities, etc., sent
2523 * by the MDS as they are applied to our local cache.
2525 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2527 struct ceph_mds_client *mdsc = session->s_mdsc;
2528 struct ceph_mds_request *req;
2529 struct ceph_mds_reply_head *head = msg->front.iov_base;
2530 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2531 struct ceph_snap_realm *realm;
2534 int mds = session->s_mds;
2536 if (msg->front.iov_len < sizeof(*head)) {
2537 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2542 /* get request, session */
2543 tid = le64_to_cpu(msg->hdr.tid);
2544 mutex_lock(&mdsc->mutex);
2545 req = lookup_get_request(mdsc, tid);
2547 dout("handle_reply on unknown tid %llu\n", tid);
2548 mutex_unlock(&mdsc->mutex);
2551 dout("handle_reply %p\n", req);
2553 /* correct session? */
2554 if (req->r_session != session) {
2555 pr_err("mdsc_handle_reply got %llu on session mds%d"
2556 " not mds%d\n", tid, session->s_mds,
2557 req->r_session ? req->r_session->s_mds : -1);
2558 mutex_unlock(&mdsc->mutex);
2563 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
2564 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
2565 pr_warn("got a dup %s reply on %llu from mds%d\n",
2566 head->safe ? "safe" : "unsafe", tid, mds);
2567 mutex_unlock(&mdsc->mutex);
2570 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
2571 pr_warn("got unsafe after safe on %llu from mds%d\n",
2573 mutex_unlock(&mdsc->mutex);
2577 result = le32_to_cpu(head->result);
2581 * if we're not talking to the authority, send to them
2582 * if the authority has changed while we weren't looking,
2583 * send to new authority
2584 * Otherwise we just have to return an ESTALE
2586 if (result == -ESTALE) {
2587 dout("got ESTALE on request %llu\n", req->r_tid);
2588 req->r_resend_mds = -1;
2589 if (req->r_direct_mode != USE_AUTH_MDS) {
2590 dout("not using auth, setting for that now\n");
2591 req->r_direct_mode = USE_AUTH_MDS;
2592 __do_request(mdsc, req);
2593 mutex_unlock(&mdsc->mutex);
2596 int mds = __choose_mds(mdsc, req);
2597 if (mds >= 0 && mds != req->r_session->s_mds) {
2598 dout("but auth changed, so resending\n");
2599 __do_request(mdsc, req);
2600 mutex_unlock(&mdsc->mutex);
2604 dout("have to return ESTALE on request %llu\n", req->r_tid);
2609 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
2610 __unregister_request(mdsc, req);
2612 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2614 * We already handled the unsafe response, now do the
2615 * cleanup. No need to examine the response; the MDS
2616 * doesn't include any result info in the safe
2617 * response. And even if it did, there is nothing
2618 * useful we could do with a revised return value.
2620 dout("got safe reply %llu, mds%d\n", tid, mds);
2622 /* last unsafe request during umount? */
2623 if (mdsc->stopping && !__get_oldest_req(mdsc))
2624 complete_all(&mdsc->safe_umount_waiters);
2625 mutex_unlock(&mdsc->mutex);
2629 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
2630 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2631 if (req->r_unsafe_dir) {
2632 struct ceph_inode_info *ci =
2633 ceph_inode(req->r_unsafe_dir);
2634 spin_lock(&ci->i_unsafe_lock);
2635 list_add_tail(&req->r_unsafe_dir_item,
2636 &ci->i_unsafe_dirops);
2637 spin_unlock(&ci->i_unsafe_lock);
2641 dout("handle_reply tid %lld result %d\n", tid, result);
2642 rinfo = &req->r_reply_info;
2643 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2644 mutex_unlock(&mdsc->mutex);
2646 mutex_lock(&session->s_mutex);
2648 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
2655 if (rinfo->snapblob_len) {
2656 down_write(&mdsc->snap_rwsem);
2657 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2658 rinfo->snapblob + rinfo->snapblob_len,
2659 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2661 downgrade_write(&mdsc->snap_rwsem);
2663 down_read(&mdsc->snap_rwsem);
2666 /* insert trace into our cache */
2667 mutex_lock(&req->r_fill_mutex);
2668 current->journal_info = req;
2669 err = ceph_fill_trace(mdsc->fsc->sb, req);
2671 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
2672 req->r_op == CEPH_MDS_OP_LSSNAP))
2673 ceph_readdir_prepopulate(req, req->r_session);
2674 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2676 current->journal_info = NULL;
2677 mutex_unlock(&req->r_fill_mutex);
2679 up_read(&mdsc->snap_rwsem);
2681 ceph_put_snap_realm(mdsc, realm);
2683 if (err == 0 && req->r_target_inode &&
2684 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2685 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
2686 spin_lock(&ci->i_unsafe_lock);
2687 list_add_tail(&req->r_unsafe_target_item, &ci->i_unsafe_iops);
2688 spin_unlock(&ci->i_unsafe_lock);
2691 mutex_lock(&mdsc->mutex);
2692 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
2696 req->r_reply = ceph_msg_get(msg);
2697 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
2700 dout("reply arrived after request %lld was aborted\n", tid);
2702 mutex_unlock(&mdsc->mutex);
2704 mutex_unlock(&session->s_mutex);
2706 /* kick calling process */
2707 complete_request(mdsc, req);
2709 ceph_mdsc_put_request(req);
2716 * handle mds notification that our request has been forwarded.
2718 static void handle_forward(struct ceph_mds_client *mdsc,
2719 struct ceph_mds_session *session,
2720 struct ceph_msg *msg)
2722 struct ceph_mds_request *req;
2723 u64 tid = le64_to_cpu(msg->hdr.tid);
2727 void *p = msg->front.iov_base;
2728 void *end = p + msg->front.iov_len;
2730 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2731 next_mds = ceph_decode_32(&p);
2732 fwd_seq = ceph_decode_32(&p);
2734 mutex_lock(&mdsc->mutex);
2735 req = lookup_get_request(mdsc, tid);
2737 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2738 goto out; /* dup reply? */
2741 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
2742 dout("forward tid %llu aborted, unregistering\n", tid);
2743 __unregister_request(mdsc, req);
2744 } else if (fwd_seq <= req->r_num_fwd) {
2745 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2746 tid, next_mds, req->r_num_fwd, fwd_seq);
2748 /* resend. forward race not possible; mds would drop */
2749 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2751 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
2752 req->r_attempts = 0;
2753 req->r_num_fwd = fwd_seq;
2754 req->r_resend_mds = next_mds;
2755 put_request_session(req);
2756 __do_request(mdsc, req);
2758 ceph_mdsc_put_request(req);
2760 mutex_unlock(&mdsc->mutex);
2764 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2768 * handle a mds session control message
2770 static void handle_session(struct ceph_mds_session *session,
2771 struct ceph_msg *msg)
2773 struct ceph_mds_client *mdsc = session->s_mdsc;
2776 int mds = session->s_mds;
2777 struct ceph_mds_session_head *h = msg->front.iov_base;
2781 if (msg->front.iov_len < sizeof(*h))
2783 op = le32_to_cpu(h->op);
2784 seq = le64_to_cpu(h->seq);
2786 mutex_lock(&mdsc->mutex);
2787 if (op == CEPH_SESSION_CLOSE) {
2788 get_session(session);
2789 __unregister_session(mdsc, session);
2791 /* FIXME: this ttl calculation is generous */
2792 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2793 mutex_unlock(&mdsc->mutex);
2795 mutex_lock(&session->s_mutex);
2797 dout("handle_session mds%d %s %p state %s seq %llu\n",
2798 mds, ceph_session_op_name(op), session,
2799 ceph_session_state_name(session->s_state), seq);
2801 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2802 session->s_state = CEPH_MDS_SESSION_OPEN;
2803 pr_info("mds%d came back\n", session->s_mds);
2807 case CEPH_SESSION_OPEN:
2808 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2809 pr_info("mds%d reconnect success\n", session->s_mds);
2810 session->s_state = CEPH_MDS_SESSION_OPEN;
2811 renewed_caps(mdsc, session, 0);
2814 __close_session(mdsc, session);
2817 case CEPH_SESSION_RENEWCAPS:
2818 if (session->s_renew_seq == seq)
2819 renewed_caps(mdsc, session, 1);
2822 case CEPH_SESSION_CLOSE:
2823 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2824 pr_info("mds%d reconnect denied\n", session->s_mds);
2825 cleanup_session_requests(mdsc, session);
2826 remove_session_caps(session);
2827 wake = 2; /* for good measure */
2828 wake_up_all(&mdsc->session_close_wq);
2831 case CEPH_SESSION_STALE:
2832 pr_info("mds%d caps went stale, renewing\n",
2834 spin_lock(&session->s_gen_ttl_lock);
2835 session->s_cap_gen++;
2836 session->s_cap_ttl = jiffies - 1;
2837 spin_unlock(&session->s_gen_ttl_lock);
2838 send_renew_caps(mdsc, session);
2841 case CEPH_SESSION_RECALL_STATE:
2842 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2845 case CEPH_SESSION_FLUSHMSG:
2846 send_flushmsg_ack(mdsc, session, seq);
2849 case CEPH_SESSION_FORCE_RO:
2850 dout("force_session_readonly %p\n", session);
2851 spin_lock(&session->s_cap_lock);
2852 session->s_readonly = true;
2853 spin_unlock(&session->s_cap_lock);
2854 wake_up_session_caps(session, 0);
2857 case CEPH_SESSION_REJECT:
2858 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
2859 pr_info("mds%d rejected session\n", session->s_mds);
2860 session->s_state = CEPH_MDS_SESSION_REJECTED;
2861 cleanup_session_requests(mdsc, session);
2862 remove_session_caps(session);
2863 wake = 2; /* for good measure */
2867 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2871 mutex_unlock(&session->s_mutex);
2873 mutex_lock(&mdsc->mutex);
2874 __wake_requests(mdsc, &session->s_waiting);
2876 kick_requests(mdsc, mds);
2877 mutex_unlock(&mdsc->mutex);
2879 if (op == CEPH_SESSION_CLOSE)
2880 ceph_put_mds_session(session);
2884 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2885 (int)msg->front.iov_len);
2892 * called under session->mutex.
2894 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2895 struct ceph_mds_session *session)
2897 struct ceph_mds_request *req, *nreq;
2901 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2903 mutex_lock(&mdsc->mutex);
2904 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2905 err = __prepare_send_request(mdsc, req, session->s_mds, true);
2907 ceph_msg_get(req->r_request);
2908 ceph_con_send(&session->s_con, req->r_request);
2913 * also re-send old requests when MDS enters reconnect stage. So that MDS
2914 * can process completed request in clientreplay stage.
2916 p = rb_first(&mdsc->request_tree);
2918 req = rb_entry(p, struct ceph_mds_request, r_node);
2920 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2922 if (req->r_attempts == 0)
2923 continue; /* only old requests */
2924 if (req->r_session &&
2925 req->r_session->s_mds == session->s_mds) {
2926 err = __prepare_send_request(mdsc, req,
2927 session->s_mds, true);
2929 ceph_msg_get(req->r_request);
2930 ceph_con_send(&session->s_con, req->r_request);
2934 mutex_unlock(&mdsc->mutex);
2938 * Encode information about a cap for a reconnect with the MDS.
2940 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2944 struct ceph_mds_cap_reconnect v2;
2945 struct ceph_mds_cap_reconnect_v1 v1;
2947 struct ceph_inode_info *ci = cap->ci;
2948 struct ceph_reconnect_state *recon_state = arg;
2949 struct ceph_pagelist *pagelist = recon_state->pagelist;
2954 struct dentry *dentry;
2956 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2957 inode, ceph_vinop(inode), cap, cap->cap_id,
2958 ceph_cap_string(cap->issued));
2959 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2963 dentry = d_find_alias(inode);
2965 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2967 err = PTR_ERR(path);
2976 spin_lock(&ci->i_ceph_lock);
2977 cap->seq = 0; /* reset cap seq */
2978 cap->issue_seq = 0; /* and issue_seq */
2979 cap->mseq = 0; /* and migrate_seq */
2980 cap->cap_gen = cap->session->s_cap_gen;
2982 if (recon_state->msg_version >= 2) {
2983 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2984 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2985 rec.v2.issued = cpu_to_le32(cap->issued);
2986 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2987 rec.v2.pathbase = cpu_to_le64(pathbase);
2988 rec.v2.flock_len = (__force __le32)
2989 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
2991 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2992 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2993 rec.v1.issued = cpu_to_le32(cap->issued);
2994 rec.v1.size = cpu_to_le64(inode->i_size);
2995 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
2996 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
2997 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2998 rec.v1.pathbase = cpu_to_le64(pathbase);
3001 if (list_empty(&ci->i_cap_snaps)) {
3002 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
3004 struct ceph_cap_snap *capsnap =
3005 list_first_entry(&ci->i_cap_snaps,
3006 struct ceph_cap_snap, ci_item);
3007 snap_follows = capsnap->follows;
3009 spin_unlock(&ci->i_ceph_lock);
3011 if (recon_state->msg_version >= 2) {
3012 int num_fcntl_locks, num_flock_locks;
3013 struct ceph_filelock *flocks = NULL;
3014 size_t struct_len, total_len = 0;
3018 if (rec.v2.flock_len) {
3019 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
3021 num_fcntl_locks = 0;
3022 num_flock_locks = 0;
3024 if (num_fcntl_locks + num_flock_locks > 0) {
3025 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
3026 sizeof(struct ceph_filelock),
3032 err = ceph_encode_locks_to_buffer(inode, flocks,
3047 if (recon_state->msg_version >= 3) {
3048 /* version, compat_version and struct_len */
3049 total_len = 2 * sizeof(u8) + sizeof(u32);
3053 * number of encoded locks is stable, so copy to pagelist
3055 struct_len = 2 * sizeof(u32) +
3056 (num_fcntl_locks + num_flock_locks) *
3057 sizeof(struct ceph_filelock);
3058 rec.v2.flock_len = cpu_to_le32(struct_len);
3060 struct_len += sizeof(rec.v2);
3061 struct_len += sizeof(u32) + pathlen;
3064 struct_len += sizeof(u64); /* snap_follows */
3066 total_len += struct_len;
3067 err = ceph_pagelist_reserve(pagelist, total_len);
3070 if (recon_state->msg_version >= 3) {
3071 ceph_pagelist_encode_8(pagelist, struct_v);
3072 ceph_pagelist_encode_8(pagelist, 1);
3073 ceph_pagelist_encode_32(pagelist, struct_len);
3075 ceph_pagelist_encode_string(pagelist, path, pathlen);
3076 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3077 ceph_locks_to_pagelist(flocks, pagelist,
3081 ceph_pagelist_encode_64(pagelist, snap_follows);
3085 size_t size = sizeof(u32) + pathlen + sizeof(rec.v1);
3086 err = ceph_pagelist_reserve(pagelist, size);
3088 ceph_pagelist_encode_string(pagelist, path, pathlen);
3089 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
3093 recon_state->nr_caps++;
3103 * If an MDS fails and recovers, clients need to reconnect in order to
3104 * reestablish shared state. This includes all caps issued through
3105 * this session _and_ the snap_realm hierarchy. Because it's not
3106 * clear which snap realms the mds cares about, we send everything we
3107 * know about.. that ensures we'll then get any new info the
3108 * recovering MDS might have.
3110 * This is a relatively heavyweight operation, but it's rare.
3112 * called with mdsc->mutex held.
3114 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3115 struct ceph_mds_session *session)
3117 struct ceph_msg *reply;
3119 int mds = session->s_mds;
3122 struct ceph_pagelist *pagelist;
3123 struct ceph_reconnect_state recon_state;
3126 pr_info("mds%d reconnect start\n", mds);
3128 pagelist = ceph_pagelist_alloc(GFP_NOFS);
3130 goto fail_nopagelist;
3132 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3136 mutex_lock(&session->s_mutex);
3137 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3140 dout("session %p state %s\n", session,
3141 ceph_session_state_name(session->s_state));
3143 spin_lock(&session->s_gen_ttl_lock);
3144 session->s_cap_gen++;
3145 spin_unlock(&session->s_gen_ttl_lock);
3147 spin_lock(&session->s_cap_lock);
3148 /* don't know if session is readonly */
3149 session->s_readonly = 0;
3151 * notify __ceph_remove_cap() that we are composing cap reconnect.
3152 * If a cap get released before being added to the cap reconnect,
3153 * __ceph_remove_cap() should skip queuing cap release.
3155 session->s_cap_reconnect = 1;
3156 /* drop old cap expires; we're about to reestablish that state */
3157 detach_cap_releases(session, &dispose);
3158 spin_unlock(&session->s_cap_lock);
3159 dispose_cap_releases(mdsc, &dispose);
3161 /* trim unused caps to reduce MDS's cache rejoin time */
3162 if (mdsc->fsc->sb->s_root)
3163 shrink_dcache_parent(mdsc->fsc->sb->s_root);
3165 ceph_con_close(&session->s_con);
3166 ceph_con_open(&session->s_con,
3167 CEPH_ENTITY_TYPE_MDS, mds,
3168 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3170 /* replay unsafe requests */
3171 replay_unsafe_requests(mdsc, session);
3173 down_read(&mdsc->snap_rwsem);
3175 /* traverse this session's caps */
3176 s_nr_caps = session->s_nr_caps;
3177 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
3181 recon_state.nr_caps = 0;
3182 recon_state.pagelist = pagelist;
3183 if (session->s_con.peer_features & CEPH_FEATURE_MDSENC)
3184 recon_state.msg_version = 3;
3185 else if (session->s_con.peer_features & CEPH_FEATURE_FLOCK)
3186 recon_state.msg_version = 2;
3188 recon_state.msg_version = 1;
3189 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
3193 spin_lock(&session->s_cap_lock);
3194 session->s_cap_reconnect = 0;
3195 spin_unlock(&session->s_cap_lock);
3198 * snaprealms. we provide mds with the ino, seq (version), and
3199 * parent for all of our realms. If the mds has any newer info,
3202 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3203 struct ceph_snap_realm *realm =
3204 rb_entry(p, struct ceph_snap_realm, node);
3205 struct ceph_mds_snaprealm_reconnect sr_rec;
3207 dout(" adding snap realm %llx seq %lld parent %llx\n",
3208 realm->ino, realm->seq, realm->parent_ino);
3209 sr_rec.ino = cpu_to_le64(realm->ino);
3210 sr_rec.seq = cpu_to_le64(realm->seq);
3211 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3212 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3217 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
3219 /* raced with cap release? */
3220 if (s_nr_caps != recon_state.nr_caps) {
3221 struct page *page = list_first_entry(&pagelist->head,
3223 __le32 *addr = kmap_atomic(page);
3224 *addr = cpu_to_le32(recon_state.nr_caps);
3225 kunmap_atomic(addr);
3228 reply->hdr.data_len = cpu_to_le32(pagelist->length);
3229 ceph_msg_data_add_pagelist(reply, pagelist);
3231 ceph_early_kick_flushing_caps(mdsc, session);
3233 ceph_con_send(&session->s_con, reply);
3235 mutex_unlock(&session->s_mutex);
3237 mutex_lock(&mdsc->mutex);
3238 __wake_requests(mdsc, &session->s_waiting);
3239 mutex_unlock(&mdsc->mutex);
3241 up_read(&mdsc->snap_rwsem);
3242 ceph_pagelist_release(pagelist);
3246 ceph_msg_put(reply);
3247 up_read(&mdsc->snap_rwsem);
3248 mutex_unlock(&session->s_mutex);
3250 ceph_pagelist_release(pagelist);
3252 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
3258 * compare old and new mdsmaps, kicking requests
3259 * and closing out old connections as necessary
3261 * called under mdsc->mutex.
3263 static void check_new_map(struct ceph_mds_client *mdsc,
3264 struct ceph_mdsmap *newmap,
3265 struct ceph_mdsmap *oldmap)
3268 int oldstate, newstate;
3269 struct ceph_mds_session *s;
3271 dout("check_new_map new %u old %u\n",
3272 newmap->m_epoch, oldmap->m_epoch);
3274 for (i = 0; i < oldmap->m_num_mds && i < mdsc->max_sessions; i++) {
3275 if (!mdsc->sessions[i])
3277 s = mdsc->sessions[i];
3278 oldstate = ceph_mdsmap_get_state(oldmap, i);
3279 newstate = ceph_mdsmap_get_state(newmap, i);
3281 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
3282 i, ceph_mds_state_name(oldstate),
3283 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
3284 ceph_mds_state_name(newstate),
3285 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
3286 ceph_session_state_name(s->s_state));
3288 if (i >= newmap->m_num_mds ||
3289 memcmp(ceph_mdsmap_get_addr(oldmap, i),
3290 ceph_mdsmap_get_addr(newmap, i),
3291 sizeof(struct ceph_entity_addr))) {
3292 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
3293 /* the session never opened, just close it
3296 __unregister_session(mdsc, s);
3297 __wake_requests(mdsc, &s->s_waiting);
3298 ceph_put_mds_session(s);
3299 } else if (i >= newmap->m_num_mds) {
3300 /* force close session for stopped mds */
3302 __unregister_session(mdsc, s);
3303 __wake_requests(mdsc, &s->s_waiting);
3304 kick_requests(mdsc, i);
3305 mutex_unlock(&mdsc->mutex);
3307 mutex_lock(&s->s_mutex);
3308 cleanup_session_requests(mdsc, s);
3309 remove_session_caps(s);
3310 mutex_unlock(&s->s_mutex);
3312 ceph_put_mds_session(s);
3314 mutex_lock(&mdsc->mutex);
3317 mutex_unlock(&mdsc->mutex);
3318 mutex_lock(&s->s_mutex);
3319 mutex_lock(&mdsc->mutex);
3320 ceph_con_close(&s->s_con);
3321 mutex_unlock(&s->s_mutex);
3322 s->s_state = CEPH_MDS_SESSION_RESTARTING;
3324 } else if (oldstate == newstate) {
3325 continue; /* nothing new with this mds */
3331 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
3332 newstate >= CEPH_MDS_STATE_RECONNECT) {
3333 mutex_unlock(&mdsc->mutex);
3334 send_mds_reconnect(mdsc, s);
3335 mutex_lock(&mdsc->mutex);
3339 * kick request on any mds that has gone active.
3341 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3342 newstate >= CEPH_MDS_STATE_ACTIVE) {
3343 if (oldstate != CEPH_MDS_STATE_CREATING &&
3344 oldstate != CEPH_MDS_STATE_STARTING)
3345 pr_info("mds%d recovery completed\n", s->s_mds);
3346 kick_requests(mdsc, i);
3347 ceph_kick_flushing_caps(mdsc, s);
3348 wake_up_session_caps(s, 1);
3352 for (i = 0; i < newmap->m_num_mds && i < mdsc->max_sessions; i++) {
3353 s = mdsc->sessions[i];
3356 if (!ceph_mdsmap_is_laggy(newmap, i))
3358 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3359 s->s_state == CEPH_MDS_SESSION_HUNG ||
3360 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3361 dout(" connecting to export targets of laggy mds%d\n",
3363 __open_export_target_sessions(mdsc, s);
3375 * caller must hold session s_mutex, dentry->d_lock
3377 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3379 struct ceph_dentry_info *di = ceph_dentry(dentry);
3381 ceph_put_mds_session(di->lease_session);
3382 di->lease_session = NULL;
3385 static void handle_lease(struct ceph_mds_client *mdsc,
3386 struct ceph_mds_session *session,
3387 struct ceph_msg *msg)
3389 struct super_block *sb = mdsc->fsc->sb;
3390 struct inode *inode;
3391 struct dentry *parent, *dentry;
3392 struct ceph_dentry_info *di;
3393 int mds = session->s_mds;
3394 struct ceph_mds_lease *h = msg->front.iov_base;
3396 struct ceph_vino vino;
3400 dout("handle_lease from mds%d\n", mds);
3403 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3405 vino.ino = le64_to_cpu(h->ino);
3406 vino.snap = CEPH_NOSNAP;
3407 seq = le32_to_cpu(h->seq);
3408 dname.len = get_unaligned_le32(h + 1);
3409 if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
3411 dname.name = (void *)(h + 1) + sizeof(u32);
3414 inode = ceph_find_inode(sb, vino);
3415 dout("handle_lease %s, ino %llx %p %.*s\n",
3416 ceph_lease_op_name(h->action), vino.ino, inode,
3417 dname.len, dname.name);
3419 mutex_lock(&session->s_mutex);
3423 dout("handle_lease no inode %llx\n", vino.ino);
3428 parent = d_find_alias(inode);
3430 dout("no parent dentry on inode %p\n", inode);
3432 goto release; /* hrm... */
3434 dname.hash = full_name_hash(parent, dname.name, dname.len);
3435 dentry = d_lookup(parent, &dname);
3440 spin_lock(&dentry->d_lock);
3441 di = ceph_dentry(dentry);
3442 switch (h->action) {
3443 case CEPH_MDS_LEASE_REVOKE:
3444 if (di->lease_session == session) {
3445 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3446 h->seq = cpu_to_le32(di->lease_seq);
3447 __ceph_mdsc_drop_dentry_lease(dentry);
3452 case CEPH_MDS_LEASE_RENEW:
3453 if (di->lease_session == session &&
3454 di->lease_gen == session->s_cap_gen &&
3455 di->lease_renew_from &&
3456 di->lease_renew_after == 0) {
3457 unsigned long duration =
3458 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
3460 di->lease_seq = seq;
3461 di->time = di->lease_renew_from + duration;
3462 di->lease_renew_after = di->lease_renew_from +
3464 di->lease_renew_from = 0;
3468 spin_unlock(&dentry->d_lock);
3475 /* let's just reuse the same message */
3476 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3478 ceph_con_send(&session->s_con, msg);
3482 mutex_unlock(&session->s_mutex);
3486 pr_err("corrupt lease message\n");
3490 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3491 struct inode *inode,
3492 struct dentry *dentry, char action,
3495 struct ceph_msg *msg;
3496 struct ceph_mds_lease *lease;
3497 int len = sizeof(*lease) + sizeof(u32);
3500 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3501 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3502 dnamelen = dentry->d_name.len;
3505 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
3508 lease = msg->front.iov_base;
3509 lease->action = action;
3510 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3511 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3512 lease->seq = cpu_to_le32(seq);
3513 put_unaligned_le32(dnamelen, lease + 1);
3514 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3517 * if this is a preemptive lease RELEASE, no need to
3518 * flush request stream, since the actual request will
3521 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3523 ceph_con_send(&session->s_con, msg);
3527 * lock unlock sessions, to wait ongoing session activities
3529 static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
3533 mutex_lock(&mdsc->mutex);
3534 for (i = 0; i < mdsc->max_sessions; i++) {
3535 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3538 mutex_unlock(&mdsc->mutex);
3539 mutex_lock(&s->s_mutex);
3540 mutex_unlock(&s->s_mutex);
3541 ceph_put_mds_session(s);
3542 mutex_lock(&mdsc->mutex);
3544 mutex_unlock(&mdsc->mutex);
3550 * delayed work -- periodically trim expired leases, renew caps with mds
3552 static void schedule_delayed(struct ceph_mds_client *mdsc)
3555 unsigned hz = round_jiffies_relative(HZ * delay);
3556 schedule_delayed_work(&mdsc->delayed_work, hz);
3559 static void delayed_work(struct work_struct *work)
3562 struct ceph_mds_client *mdsc =
3563 container_of(work, struct ceph_mds_client, delayed_work.work);
3567 dout("mdsc delayed_work\n");
3568 ceph_check_delayed_caps(mdsc);
3570 mutex_lock(&mdsc->mutex);
3571 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3572 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3573 mdsc->last_renew_caps);
3575 mdsc->last_renew_caps = jiffies;
3577 for (i = 0; i < mdsc->max_sessions; i++) {
3578 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3581 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3582 dout("resending session close request for mds%d\n",
3584 request_close_session(mdsc, s);
3585 ceph_put_mds_session(s);
3588 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3589 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3590 s->s_state = CEPH_MDS_SESSION_HUNG;
3591 pr_info("mds%d hung\n", s->s_mds);
3594 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3595 /* this mds is failed or recovering, just wait */
3596 ceph_put_mds_session(s);
3599 mutex_unlock(&mdsc->mutex);
3601 mutex_lock(&s->s_mutex);
3603 send_renew_caps(mdsc, s);
3605 ceph_con_keepalive(&s->s_con);
3606 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3607 s->s_state == CEPH_MDS_SESSION_HUNG)
3608 ceph_send_cap_releases(mdsc, s);
3609 mutex_unlock(&s->s_mutex);
3610 ceph_put_mds_session(s);
3612 mutex_lock(&mdsc->mutex);
3614 mutex_unlock(&mdsc->mutex);
3616 schedule_delayed(mdsc);
3619 int ceph_mdsc_init(struct ceph_fs_client *fsc)
3622 struct ceph_mds_client *mdsc;
3624 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3628 mutex_init(&mdsc->mutex);
3629 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
3630 if (!mdsc->mdsmap) {
3636 init_completion(&mdsc->safe_umount_waiters);
3637 init_waitqueue_head(&mdsc->session_close_wq);
3638 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3639 mdsc->sessions = NULL;
3640 atomic_set(&mdsc->num_sessions, 0);
3641 mdsc->max_sessions = 0;
3643 atomic64_set(&mdsc->quotarealms_count, 0);
3644 mdsc->last_snap_seq = 0;
3645 init_rwsem(&mdsc->snap_rwsem);
3646 mdsc->snap_realms = RB_ROOT;
3647 INIT_LIST_HEAD(&mdsc->snap_empty);
3648 spin_lock_init(&mdsc->snap_empty_lock);
3650 mdsc->oldest_tid = 0;
3651 mdsc->request_tree = RB_ROOT;
3652 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3653 mdsc->last_renew_caps = jiffies;
3654 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3655 spin_lock_init(&mdsc->cap_delay_lock);
3656 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3657 spin_lock_init(&mdsc->snap_flush_lock);
3658 mdsc->last_cap_flush_tid = 1;
3659 INIT_LIST_HEAD(&mdsc->cap_flush_list);
3660 INIT_LIST_HEAD(&mdsc->cap_dirty);
3661 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
3662 mdsc->num_cap_flushing = 0;
3663 spin_lock_init(&mdsc->cap_dirty_lock);
3664 init_waitqueue_head(&mdsc->cap_flushing_wq);
3665 spin_lock_init(&mdsc->dentry_lru_lock);
3666 INIT_LIST_HEAD(&mdsc->dentry_lru);
3668 ceph_caps_init(mdsc);
3669 ceph_adjust_min_caps(mdsc, fsc->min_caps);
3671 init_rwsem(&mdsc->pool_perm_rwsem);
3672 mdsc->pool_perm_tree = RB_ROOT;
3674 strscpy(mdsc->nodename, utsname()->nodename,
3675 sizeof(mdsc->nodename));
3680 * Wait for safe replies on open mds requests. If we time out, drop
3681 * all requests from the tree to avoid dangling dentry refs.
3683 static void wait_requests(struct ceph_mds_client *mdsc)
3685 struct ceph_options *opts = mdsc->fsc->client->options;
3686 struct ceph_mds_request *req;
3688 mutex_lock(&mdsc->mutex);
3689 if (__get_oldest_req(mdsc)) {
3690 mutex_unlock(&mdsc->mutex);
3692 dout("wait_requests waiting for requests\n");
3693 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
3694 ceph_timeout_jiffies(opts->mount_timeout));
3696 /* tear down remaining requests */
3697 mutex_lock(&mdsc->mutex);
3698 while ((req = __get_oldest_req(mdsc))) {
3699 dout("wait_requests timed out on tid %llu\n",
3701 __unregister_request(mdsc, req);
3704 mutex_unlock(&mdsc->mutex);
3705 dout("wait_requests done\n");
3709 * called before mount is ro, and before dentries are torn down.
3710 * (hmm, does this still race with new lookups?)
3712 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3714 dout("pre_umount\n");
3717 lock_unlock_sessions(mdsc);
3718 ceph_flush_dirty_caps(mdsc);
3719 wait_requests(mdsc);
3722 * wait for reply handlers to drop their request refs and
3723 * their inode/dcache refs
3729 * wait for all write mds requests to flush.
3731 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3733 struct ceph_mds_request *req = NULL, *nextreq;
3736 mutex_lock(&mdsc->mutex);
3737 dout("wait_unsafe_requests want %lld\n", want_tid);
3739 req = __get_oldest_req(mdsc);
3740 while (req && req->r_tid <= want_tid) {
3741 /* find next request */
3742 n = rb_next(&req->r_node);
3744 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3747 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
3748 (req->r_op & CEPH_MDS_OP_WRITE)) {
3750 ceph_mdsc_get_request(req);
3752 ceph_mdsc_get_request(nextreq);
3753 mutex_unlock(&mdsc->mutex);
3754 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3755 req->r_tid, want_tid);
3756 wait_for_completion(&req->r_safe_completion);
3757 mutex_lock(&mdsc->mutex);
3758 ceph_mdsc_put_request(req);
3760 break; /* next dne before, so we're done! */
3761 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3762 /* next request was removed from tree */
3763 ceph_mdsc_put_request(nextreq);
3766 ceph_mdsc_put_request(nextreq); /* won't go away */
3770 mutex_unlock(&mdsc->mutex);
3771 dout("wait_unsafe_requests done\n");
3774 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3776 u64 want_tid, want_flush;
3778 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
3782 mutex_lock(&mdsc->mutex);
3783 want_tid = mdsc->last_tid;
3784 mutex_unlock(&mdsc->mutex);
3786 ceph_flush_dirty_caps(mdsc);
3787 spin_lock(&mdsc->cap_dirty_lock);
3788 want_flush = mdsc->last_cap_flush_tid;
3789 if (!list_empty(&mdsc->cap_flush_list)) {
3790 struct ceph_cap_flush *cf =
3791 list_last_entry(&mdsc->cap_flush_list,
3792 struct ceph_cap_flush, g_list);
3795 spin_unlock(&mdsc->cap_dirty_lock);
3797 dout("sync want tid %lld flush_seq %lld\n",
3798 want_tid, want_flush);
3800 wait_unsafe_requests(mdsc, want_tid);
3801 wait_caps_flush(mdsc, want_flush);
3805 * true if all sessions are closed, or we force unmount
3807 static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
3809 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
3811 return atomic_read(&mdsc->num_sessions) <= skipped;
3815 * called after sb is ro.
3817 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3819 struct ceph_options *opts = mdsc->fsc->client->options;
3820 struct ceph_mds_session *session;
3824 dout("close_sessions\n");
3826 /* close sessions */
3827 mutex_lock(&mdsc->mutex);
3828 for (i = 0; i < mdsc->max_sessions; i++) {
3829 session = __ceph_lookup_mds_session(mdsc, i);
3832 mutex_unlock(&mdsc->mutex);
3833 mutex_lock(&session->s_mutex);
3834 if (__close_session(mdsc, session) <= 0)
3836 mutex_unlock(&session->s_mutex);
3837 ceph_put_mds_session(session);
3838 mutex_lock(&mdsc->mutex);
3840 mutex_unlock(&mdsc->mutex);
3842 dout("waiting for sessions to close\n");
3843 wait_event_timeout(mdsc->session_close_wq,
3844 done_closing_sessions(mdsc, skipped),
3845 ceph_timeout_jiffies(opts->mount_timeout));
3847 /* tear down remaining sessions */
3848 mutex_lock(&mdsc->mutex);
3849 for (i = 0; i < mdsc->max_sessions; i++) {
3850 if (mdsc->sessions[i]) {
3851 session = get_session(mdsc->sessions[i]);
3852 __unregister_session(mdsc, session);
3853 mutex_unlock(&mdsc->mutex);
3854 mutex_lock(&session->s_mutex);
3855 remove_session_caps(session);
3856 mutex_unlock(&session->s_mutex);
3857 ceph_put_mds_session(session);
3858 mutex_lock(&mdsc->mutex);
3861 WARN_ON(!list_empty(&mdsc->cap_delay_list));
3862 mutex_unlock(&mdsc->mutex);
3864 ceph_cleanup_empty_realms(mdsc);
3866 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3871 void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
3873 struct ceph_mds_session *session;
3876 dout("force umount\n");
3878 mutex_lock(&mdsc->mutex);
3879 for (mds = 0; mds < mdsc->max_sessions; mds++) {
3880 session = __ceph_lookup_mds_session(mdsc, mds);
3883 mutex_unlock(&mdsc->mutex);
3884 mutex_lock(&session->s_mutex);
3885 __close_session(mdsc, session);
3886 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
3887 cleanup_session_requests(mdsc, session);
3888 remove_session_caps(session);
3890 mutex_unlock(&session->s_mutex);
3891 ceph_put_mds_session(session);
3892 mutex_lock(&mdsc->mutex);
3893 kick_requests(mdsc, mds);
3895 __wake_requests(mdsc, &mdsc->waiting_for_map);
3896 mutex_unlock(&mdsc->mutex);
3899 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
3902 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3904 ceph_mdsmap_destroy(mdsc->mdsmap);
3905 kfree(mdsc->sessions);
3906 ceph_caps_finalize(mdsc);
3907 ceph_pool_perm_destroy(mdsc);
3910 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3912 struct ceph_mds_client *mdsc = fsc->mdsc;
3913 dout("mdsc_destroy %p\n", mdsc);
3918 /* flush out any connection work with references to us */
3921 ceph_mdsc_stop(mdsc);
3925 dout("mdsc_destroy %p done\n", mdsc);
3928 void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3930 struct ceph_fs_client *fsc = mdsc->fsc;
3931 const char *mds_namespace = fsc->mount_options->mds_namespace;
3932 void *p = msg->front.iov_base;
3933 void *end = p + msg->front.iov_len;
3937 u32 mount_fscid = (u32)-1;
3938 u8 struct_v, struct_cv;
3941 ceph_decode_need(&p, end, sizeof(u32), bad);
3942 epoch = ceph_decode_32(&p);
3944 dout("handle_fsmap epoch %u\n", epoch);
3946 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
3947 struct_v = ceph_decode_8(&p);
3948 struct_cv = ceph_decode_8(&p);
3949 map_len = ceph_decode_32(&p);
3951 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
3952 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
3954 num_fs = ceph_decode_32(&p);
3955 while (num_fs-- > 0) {
3956 void *info_p, *info_end;
3961 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
3962 info_v = ceph_decode_8(&p);
3963 info_cv = ceph_decode_8(&p);
3964 info_len = ceph_decode_32(&p);
3965 ceph_decode_need(&p, end, info_len, bad);
3967 info_end = p + info_len;
3970 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
3971 fscid = ceph_decode_32(&info_p);
3972 namelen = ceph_decode_32(&info_p);
3973 ceph_decode_need(&info_p, info_end, namelen, bad);
3975 if (mds_namespace &&
3976 strlen(mds_namespace) == namelen &&
3977 !strncmp(mds_namespace, (char *)info_p, namelen)) {
3978 mount_fscid = fscid;
3983 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
3984 if (mount_fscid != (u32)-1) {
3985 fsc->client->monc.fs_cluster_id = mount_fscid;
3986 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
3988 ceph_monc_renew_subs(&fsc->client->monc);
3996 pr_err("error decoding fsmap\n");
3998 mutex_lock(&mdsc->mutex);
3999 mdsc->mdsmap_err = err;
4000 __wake_requests(mdsc, &mdsc->waiting_for_map);
4001 mutex_unlock(&mdsc->mutex);
4005 * handle mds map update.
4007 void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
4011 void *p = msg->front.iov_base;
4012 void *end = p + msg->front.iov_len;
4013 struct ceph_mdsmap *newmap, *oldmap;
4014 struct ceph_fsid fsid;
4017 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
4018 ceph_decode_copy(&p, &fsid, sizeof(fsid));
4019 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
4021 epoch = ceph_decode_32(&p);
4022 maplen = ceph_decode_32(&p);
4023 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
4025 /* do we need it? */
4026 mutex_lock(&mdsc->mutex);
4027 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
4028 dout("handle_map epoch %u <= our %u\n",
4029 epoch, mdsc->mdsmap->m_epoch);
4030 mutex_unlock(&mdsc->mutex);
4034 newmap = ceph_mdsmap_decode(&p, end);
4035 if (IS_ERR(newmap)) {
4036 err = PTR_ERR(newmap);
4040 /* swap into place */
4042 oldmap = mdsc->mdsmap;
4043 mdsc->mdsmap = newmap;
4044 check_new_map(mdsc, newmap, oldmap);
4045 ceph_mdsmap_destroy(oldmap);
4047 mdsc->mdsmap = newmap; /* first mds map */
4049 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
4052 __wake_requests(mdsc, &mdsc->waiting_for_map);
4053 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
4054 mdsc->mdsmap->m_epoch);
4056 mutex_unlock(&mdsc->mutex);
4057 schedule_delayed(mdsc);
4061 mutex_unlock(&mdsc->mutex);
4063 pr_err("error decoding mdsmap %d\n", err);
4067 static struct ceph_connection *con_get(struct ceph_connection *con)
4069 struct ceph_mds_session *s = con->private;
4071 if (get_session(s)) {
4072 dout("mdsc con_get %p ok (%d)\n", s, refcount_read(&s->s_ref));
4075 dout("mdsc con_get %p FAIL\n", s);
4079 static void con_put(struct ceph_connection *con)
4081 struct ceph_mds_session *s = con->private;
4083 dout("mdsc con_put %p (%d)\n", s, refcount_read(&s->s_ref) - 1);
4084 ceph_put_mds_session(s);
4088 * if the client is unresponsive for long enough, the mds will kill
4089 * the session entirely.
4091 static void peer_reset(struct ceph_connection *con)
4093 struct ceph_mds_session *s = con->private;
4094 struct ceph_mds_client *mdsc = s->s_mdsc;
4096 pr_warn("mds%d closed our session\n", s->s_mds);
4097 send_mds_reconnect(mdsc, s);
4100 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4102 struct ceph_mds_session *s = con->private;
4103 struct ceph_mds_client *mdsc = s->s_mdsc;
4104 int type = le16_to_cpu(msg->hdr.type);
4106 mutex_lock(&mdsc->mutex);
4107 if (__verify_registered_session(mdsc, s) < 0) {
4108 mutex_unlock(&mdsc->mutex);
4111 mutex_unlock(&mdsc->mutex);
4114 case CEPH_MSG_MDS_MAP:
4115 ceph_mdsc_handle_mdsmap(mdsc, msg);
4117 case CEPH_MSG_FS_MAP_USER:
4118 ceph_mdsc_handle_fsmap(mdsc, msg);
4120 case CEPH_MSG_CLIENT_SESSION:
4121 handle_session(s, msg);
4123 case CEPH_MSG_CLIENT_REPLY:
4124 handle_reply(s, msg);
4126 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
4127 handle_forward(mdsc, s, msg);
4129 case CEPH_MSG_CLIENT_CAPS:
4130 ceph_handle_caps(s, msg);
4132 case CEPH_MSG_CLIENT_SNAP:
4133 ceph_handle_snap(mdsc, s, msg);
4135 case CEPH_MSG_CLIENT_LEASE:
4136 handle_lease(mdsc, s, msg);
4138 case CEPH_MSG_CLIENT_QUOTA:
4139 ceph_handle_quota(mdsc, s, msg);
4143 pr_err("received unknown message type %d %s\n", type,
4144 ceph_msg_type_name(type));
4155 * Note: returned pointer is the address of a structure that's
4156 * managed separately. Caller must *not* attempt to free it.
4158 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
4159 int *proto, int force_new)
4161 struct ceph_mds_session *s = con->private;
4162 struct ceph_mds_client *mdsc = s->s_mdsc;
4163 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4164 struct ceph_auth_handshake *auth = &s->s_auth;
4166 if (force_new && auth->authorizer) {
4167 ceph_auth_destroy_authorizer(auth->authorizer);
4168 auth->authorizer = NULL;
4170 if (!auth->authorizer) {
4171 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4174 return ERR_PTR(ret);
4176 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4179 return ERR_PTR(ret);
4181 *proto = ac->protocol;
4186 static int add_authorizer_challenge(struct ceph_connection *con,
4187 void *challenge_buf, int challenge_buf_len)
4189 struct ceph_mds_session *s = con->private;
4190 struct ceph_mds_client *mdsc = s->s_mdsc;
4191 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4193 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
4194 challenge_buf, challenge_buf_len);
4197 static int verify_authorizer_reply(struct ceph_connection *con)
4199 struct ceph_mds_session *s = con->private;
4200 struct ceph_mds_client *mdsc = s->s_mdsc;
4201 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4203 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
4206 static int invalidate_authorizer(struct ceph_connection *con)
4208 struct ceph_mds_session *s = con->private;
4209 struct ceph_mds_client *mdsc = s->s_mdsc;
4210 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4212 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
4214 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
4217 static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
4218 struct ceph_msg_header *hdr, int *skip)
4220 struct ceph_msg *msg;
4221 int type = (int) le16_to_cpu(hdr->type);
4222 int front_len = (int) le32_to_cpu(hdr->front_len);
4228 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
4230 pr_err("unable to allocate msg type %d len %d\n",
4238 static int mds_sign_message(struct ceph_msg *msg)
4240 struct ceph_mds_session *s = msg->con->private;
4241 struct ceph_auth_handshake *auth = &s->s_auth;
4243 return ceph_auth_sign_message(auth, msg);
4246 static int mds_check_message_signature(struct ceph_msg *msg)
4248 struct ceph_mds_session *s = msg->con->private;
4249 struct ceph_auth_handshake *auth = &s->s_auth;
4251 return ceph_auth_check_message_signature(auth, msg);
4254 static const struct ceph_connection_operations mds_con_ops = {
4257 .dispatch = dispatch,
4258 .get_authorizer = get_authorizer,
4259 .add_authorizer_challenge = add_authorizer_challenge,
4260 .verify_authorizer_reply = verify_authorizer_reply,
4261 .invalidate_authorizer = invalidate_authorizer,
4262 .peer_reset = peer_reset,
4263 .alloc_msg = mds_alloc_msg,
4264 .sign_message = mds_sign_message,
4265 .check_message_signature = mds_check_message_signature,