1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
5 #include <linux/wait.h>
6 #include <linux/slab.h>
8 #include <linux/sched.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.h>
11 #include <linux/ratelimit.h>
12 #include <linux/bits.h>
15 #include "mds_client.h"
17 #include <linux/ceph/ceph_features.h>
18 #include <linux/ceph/messenger.h>
19 #include <linux/ceph/decode.h>
20 #include <linux/ceph/pagelist.h>
21 #include <linux/ceph/auth.h>
22 #include <linux/ceph/debugfs.h>
24 #define RECONNECT_MAX_SIZE (INT_MAX - PAGE_SIZE)
27 * A cluster of MDS (metadata server) daemons is responsible for
28 * managing the file system namespace (the directory hierarchy and
29 * inodes) and for coordinating shared access to storage. Metadata is
30 * partitioning hierarchically across a number of servers, and that
31 * partition varies over time as the cluster adjusts the distribution
32 * in order to balance load.
34 * The MDS client is primarily responsible to managing synchronous
35 * metadata requests for operations like open, unlink, and so forth.
36 * If there is a MDS failure, we find out about it when we (possibly
37 * request and) receive a new MDS map, and can resubmit affected
40 * For the most part, though, we take advantage of a lossless
41 * communications channel to the MDS, and do not need to worry about
42 * timing out or resubmitting requests.
44 * We maintain a stateful "session" with each MDS we interact with.
45 * Within each session, we sent periodic heartbeat messages to ensure
46 * any capabilities or leases we have been issues remain valid. If
47 * the session times out and goes stale, our leases and capabilities
48 * are no longer valid.
51 struct ceph_reconnect_state {
52 struct ceph_mds_session *session;
53 int nr_caps, nr_realms;
54 struct ceph_pagelist *pagelist;
59 static void __wake_requests(struct ceph_mds_client *mdsc,
60 struct list_head *head);
61 static void ceph_cap_release_work(struct work_struct *work);
62 static void ceph_cap_reclaim_work(struct work_struct *work);
64 static const struct ceph_connection_operations mds_con_ops;
71 static int parse_reply_info_quota(void **p, void *end,
72 struct ceph_mds_reply_info_in *info)
74 u8 struct_v, struct_compat;
77 ceph_decode_8_safe(p, end, struct_v, bad);
78 ceph_decode_8_safe(p, end, struct_compat, bad);
79 /* struct_v is expected to be >= 1. we only
80 * understand encoding with struct_compat == 1. */
81 if (!struct_v || struct_compat != 1)
83 ceph_decode_32_safe(p, end, struct_len, bad);
84 ceph_decode_need(p, end, struct_len, bad);
85 end = *p + struct_len;
86 ceph_decode_64_safe(p, end, info->max_bytes, bad);
87 ceph_decode_64_safe(p, end, info->max_files, bad);
95 * parse individual inode info
97 static int parse_reply_info_in(void **p, void *end,
98 struct ceph_mds_reply_info_in *info,
104 if (features == (u64)-1) {
107 ceph_decode_8_safe(p, end, struct_v, bad);
108 ceph_decode_8_safe(p, end, struct_compat, bad);
109 /* struct_v is expected to be >= 1. we only understand
110 * encoding with struct_compat == 1. */
111 if (!struct_v || struct_compat != 1)
113 ceph_decode_32_safe(p, end, struct_len, bad);
114 ceph_decode_need(p, end, struct_len, bad);
115 end = *p + struct_len;
118 ceph_decode_need(p, end, sizeof(struct ceph_mds_reply_inode), bad);
120 *p += sizeof(struct ceph_mds_reply_inode) +
121 sizeof(*info->in->fragtree.splits) *
122 le32_to_cpu(info->in->fragtree.nsplits);
124 ceph_decode_32_safe(p, end, info->symlink_len, bad);
125 ceph_decode_need(p, end, info->symlink_len, bad);
127 *p += info->symlink_len;
129 ceph_decode_copy_safe(p, end, &info->dir_layout,
130 sizeof(info->dir_layout), bad);
131 ceph_decode_32_safe(p, end, info->xattr_len, bad);
132 ceph_decode_need(p, end, info->xattr_len, bad);
133 info->xattr_data = *p;
134 *p += info->xattr_len;
136 if (features == (u64)-1) {
138 ceph_decode_64_safe(p, end, info->inline_version, bad);
139 ceph_decode_32_safe(p, end, info->inline_len, bad);
140 ceph_decode_need(p, end, info->inline_len, bad);
141 info->inline_data = *p;
142 *p += info->inline_len;
144 err = parse_reply_info_quota(p, end, info);
148 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
149 if (info->pool_ns_len > 0) {
150 ceph_decode_need(p, end, info->pool_ns_len, bad);
151 info->pool_ns_data = *p;
152 *p += info->pool_ns_len;
156 ceph_decode_need(p, end, sizeof(info->btime), bad);
157 ceph_decode_copy(p, &info->btime, sizeof(info->btime));
159 /* change attribute */
160 ceph_decode_64_safe(p, end, info->change_attr, bad);
164 ceph_decode_32_safe(p, end, info->dir_pin, bad);
166 info->dir_pin = -ENODATA;
169 /* snapshot birth time, remains zero for v<=2 */
171 ceph_decode_need(p, end, sizeof(info->snap_btime), bad);
172 ceph_decode_copy(p, &info->snap_btime,
173 sizeof(info->snap_btime));
175 memset(&info->snap_btime, 0, sizeof(info->snap_btime));
180 if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
181 ceph_decode_64_safe(p, end, info->inline_version, bad);
182 ceph_decode_32_safe(p, end, info->inline_len, bad);
183 ceph_decode_need(p, end, info->inline_len, bad);
184 info->inline_data = *p;
185 *p += info->inline_len;
187 info->inline_version = CEPH_INLINE_NONE;
189 if (features & CEPH_FEATURE_MDS_QUOTA) {
190 err = parse_reply_info_quota(p, end, info);
198 info->pool_ns_len = 0;
199 info->pool_ns_data = NULL;
200 if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) {
201 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
202 if (info->pool_ns_len > 0) {
203 ceph_decode_need(p, end, info->pool_ns_len, bad);
204 info->pool_ns_data = *p;
205 *p += info->pool_ns_len;
209 if (features & CEPH_FEATURE_FS_BTIME) {
210 ceph_decode_need(p, end, sizeof(info->btime), bad);
211 ceph_decode_copy(p, &info->btime, sizeof(info->btime));
212 ceph_decode_64_safe(p, end, info->change_attr, bad);
215 info->dir_pin = -ENODATA;
216 /* info->snap_btime remains zero */
225 static int parse_reply_info_dir(void **p, void *end,
226 struct ceph_mds_reply_dirfrag **dirfrag,
229 if (features == (u64)-1) {
230 u8 struct_v, struct_compat;
232 ceph_decode_8_safe(p, end, struct_v, bad);
233 ceph_decode_8_safe(p, end, struct_compat, bad);
234 /* struct_v is expected to be >= 1. we only understand
235 * encoding whose struct_compat == 1. */
236 if (!struct_v || struct_compat != 1)
238 ceph_decode_32_safe(p, end, struct_len, bad);
239 ceph_decode_need(p, end, struct_len, bad);
240 end = *p + struct_len;
243 ceph_decode_need(p, end, sizeof(**dirfrag), bad);
245 *p += sizeof(**dirfrag) + sizeof(u32) * le32_to_cpu((*dirfrag)->ndist);
246 if (unlikely(*p > end))
248 if (features == (u64)-1)
255 static int parse_reply_info_lease(void **p, void *end,
256 struct ceph_mds_reply_lease **lease,
259 if (features == (u64)-1) {
260 u8 struct_v, struct_compat;
262 ceph_decode_8_safe(p, end, struct_v, bad);
263 ceph_decode_8_safe(p, end, struct_compat, bad);
264 /* struct_v is expected to be >= 1. we only understand
265 * encoding whose struct_compat == 1. */
266 if (!struct_v || struct_compat != 1)
268 ceph_decode_32_safe(p, end, struct_len, bad);
269 ceph_decode_need(p, end, struct_len, bad);
270 end = *p + struct_len;
273 ceph_decode_need(p, end, sizeof(**lease), bad);
275 *p += sizeof(**lease);
276 if (features == (u64)-1)
284 * parse a normal reply, which may contain a (dir+)dentry and/or a
287 static int parse_reply_info_trace(void **p, void *end,
288 struct ceph_mds_reply_info_parsed *info,
293 if (info->head->is_dentry) {
294 err = parse_reply_info_in(p, end, &info->diri, features);
298 err = parse_reply_info_dir(p, end, &info->dirfrag, features);
302 ceph_decode_32_safe(p, end, info->dname_len, bad);
303 ceph_decode_need(p, end, info->dname_len, bad);
305 *p += info->dname_len;
307 err = parse_reply_info_lease(p, end, &info->dlease, features);
312 if (info->head->is_target) {
313 err = parse_reply_info_in(p, end, &info->targeti, features);
318 if (unlikely(*p != end))
325 pr_err("problem parsing mds trace %d\n", err);
330 * parse readdir results
332 static int parse_reply_info_readdir(void **p, void *end,
333 struct ceph_mds_reply_info_parsed *info,
339 err = parse_reply_info_dir(p, end, &info->dir_dir, features);
343 ceph_decode_need(p, end, sizeof(num) + 2, bad);
344 num = ceph_decode_32(p);
346 u16 flags = ceph_decode_16(p);
347 info->dir_end = !!(flags & CEPH_READDIR_FRAG_END);
348 info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE);
349 info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER);
350 info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH);
355 BUG_ON(!info->dir_entries);
356 if ((unsigned long)(info->dir_entries + num) >
357 (unsigned long)info->dir_entries + info->dir_buf_size) {
358 pr_err("dir contents are larger than expected\n");
365 struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i;
367 ceph_decode_32_safe(p, end, rde->name_len, bad);
368 ceph_decode_need(p, end, rde->name_len, bad);
371 dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name);
374 err = parse_reply_info_lease(p, end, &rde->lease, features);
378 err = parse_reply_info_in(p, end, &rde->inode, features);
381 /* ceph_readdir_prepopulate() will update it */
388 /* Skip over any unrecognized fields */
395 pr_err("problem parsing dir contents %d\n", err);
400 * parse fcntl F_GETLK results
402 static int parse_reply_info_filelock(void **p, void *end,
403 struct ceph_mds_reply_info_parsed *info,
406 if (*p + sizeof(*info->filelock_reply) > end)
409 info->filelock_reply = *p;
411 /* Skip over any unrecognized fields */
419 * parse create results
421 static int parse_reply_info_create(void **p, void *end,
422 struct ceph_mds_reply_info_parsed *info,
425 if (features == (u64)-1 ||
426 (features & CEPH_FEATURE_REPLY_CREATE_INODE)) {
427 /* Malformed reply? */
429 info->has_create_ino = false;
431 info->has_create_ino = true;
432 ceph_decode_64_safe(p, end, info->ino, bad);
439 /* Skip over any unrecognized fields */
447 * parse extra results
449 static int parse_reply_info_extra(void **p, void *end,
450 struct ceph_mds_reply_info_parsed *info,
453 u32 op = le32_to_cpu(info->head->op);
455 if (op == CEPH_MDS_OP_GETFILELOCK)
456 return parse_reply_info_filelock(p, end, info, features);
457 else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
458 return parse_reply_info_readdir(p, end, info, features);
459 else if (op == CEPH_MDS_OP_CREATE)
460 return parse_reply_info_create(p, end, info, features);
466 * parse entire mds reply
468 static int parse_reply_info(struct ceph_msg *msg,
469 struct ceph_mds_reply_info_parsed *info,
476 info->head = msg->front.iov_base;
477 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
478 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
481 ceph_decode_32_safe(&p, end, len, bad);
483 ceph_decode_need(&p, end, len, bad);
484 err = parse_reply_info_trace(&p, p+len, info, features);
490 ceph_decode_32_safe(&p, end, len, bad);
492 ceph_decode_need(&p, end, len, bad);
493 err = parse_reply_info_extra(&p, p+len, info, features);
499 ceph_decode_32_safe(&p, end, len, bad);
500 info->snapblob_len = len;
511 pr_err("mds parse_reply err %d\n", err);
515 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
517 if (!info->dir_entries)
519 free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
526 const char *ceph_session_state_name(int s)
529 case CEPH_MDS_SESSION_NEW: return "new";
530 case CEPH_MDS_SESSION_OPENING: return "opening";
531 case CEPH_MDS_SESSION_OPEN: return "open";
532 case CEPH_MDS_SESSION_HUNG: return "hung";
533 case CEPH_MDS_SESSION_CLOSING: return "closing";
534 case CEPH_MDS_SESSION_CLOSED: return "closed";
535 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
536 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
537 case CEPH_MDS_SESSION_REJECTED: return "rejected";
538 default: return "???";
542 struct ceph_mds_session *ceph_get_mds_session(struct ceph_mds_session *s)
544 if (refcount_inc_not_zero(&s->s_ref)) {
545 dout("mdsc get_session %p %d -> %d\n", s,
546 refcount_read(&s->s_ref)-1, refcount_read(&s->s_ref));
549 dout("mdsc get_session %p 0 -- FAIL\n", s);
554 void ceph_put_mds_session(struct ceph_mds_session *s)
556 dout("mdsc put_session %p %d -> %d\n", s,
557 refcount_read(&s->s_ref), refcount_read(&s->s_ref)-1);
558 if (refcount_dec_and_test(&s->s_ref)) {
559 if (s->s_auth.authorizer)
560 ceph_auth_destroy_authorizer(s->s_auth.authorizer);
566 * called under mdsc->mutex
568 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
571 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
573 return ceph_get_mds_session(mdsc->sessions[mds]);
576 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
578 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
584 static int __verify_registered_session(struct ceph_mds_client *mdsc,
585 struct ceph_mds_session *s)
587 if (s->s_mds >= mdsc->max_sessions ||
588 mdsc->sessions[s->s_mds] != s)
594 * create+register a new session for given mds.
595 * called under mdsc->mutex.
597 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
600 struct ceph_mds_session *s;
602 if (mds >= mdsc->mdsmap->possible_max_rank)
603 return ERR_PTR(-EINVAL);
605 s = kzalloc(sizeof(*s), GFP_NOFS);
607 return ERR_PTR(-ENOMEM);
609 if (mds >= mdsc->max_sessions) {
610 int newmax = 1 << get_count_order(mds + 1);
611 struct ceph_mds_session **sa;
613 dout("%s: realloc to %d\n", __func__, newmax);
614 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
617 if (mdsc->sessions) {
618 memcpy(sa, mdsc->sessions,
619 mdsc->max_sessions * sizeof(void *));
620 kfree(mdsc->sessions);
623 mdsc->max_sessions = newmax;
626 dout("%s: mds%d\n", __func__, mds);
629 s->s_state = CEPH_MDS_SESSION_NEW;
632 mutex_init(&s->s_mutex);
634 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
636 spin_lock_init(&s->s_gen_ttl_lock);
638 s->s_cap_ttl = jiffies - 1;
640 spin_lock_init(&s->s_cap_lock);
641 s->s_renew_requested = 0;
643 INIT_LIST_HEAD(&s->s_caps);
645 refcount_set(&s->s_ref, 1);
646 INIT_LIST_HEAD(&s->s_waiting);
647 INIT_LIST_HEAD(&s->s_unsafe);
648 s->s_num_cap_releases = 0;
649 s->s_cap_reconnect = 0;
650 s->s_cap_iterator = NULL;
651 INIT_LIST_HEAD(&s->s_cap_releases);
652 INIT_WORK(&s->s_cap_release_work, ceph_cap_release_work);
654 INIT_LIST_HEAD(&s->s_cap_flushing);
656 mdsc->sessions[mds] = s;
657 atomic_inc(&mdsc->num_sessions);
658 refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
660 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
661 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
667 return ERR_PTR(-ENOMEM);
671 * called under mdsc->mutex
673 static void __unregister_session(struct ceph_mds_client *mdsc,
674 struct ceph_mds_session *s)
676 dout("__unregister_session mds%d %p\n", s->s_mds, s);
677 BUG_ON(mdsc->sessions[s->s_mds] != s);
678 mdsc->sessions[s->s_mds] = NULL;
679 ceph_con_close(&s->s_con);
680 ceph_put_mds_session(s);
681 atomic_dec(&mdsc->num_sessions);
685 * drop session refs in request.
687 * should be last request ref, or hold mdsc->mutex
689 static void put_request_session(struct ceph_mds_request *req)
691 if (req->r_session) {
692 ceph_put_mds_session(req->r_session);
693 req->r_session = NULL;
697 void ceph_mdsc_release_request(struct kref *kref)
699 struct ceph_mds_request *req = container_of(kref,
700 struct ceph_mds_request,
702 destroy_reply_info(&req->r_reply_info);
704 ceph_msg_put(req->r_request);
706 ceph_msg_put(req->r_reply);
708 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
709 /* avoid calling iput_final() in mds dispatch threads */
710 ceph_async_iput(req->r_inode);
713 ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
714 ceph_async_iput(req->r_parent);
716 ceph_async_iput(req->r_target_inode);
719 if (req->r_old_dentry)
720 dput(req->r_old_dentry);
721 if (req->r_old_dentry_dir) {
723 * track (and drop pins for) r_old_dentry_dir
724 * separately, since r_old_dentry's d_parent may have
725 * changed between the dir mutex being dropped and
726 * this request being freed.
728 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
730 ceph_async_iput(req->r_old_dentry_dir);
735 ceph_pagelist_release(req->r_pagelist);
736 put_request_session(req);
737 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
738 WARN_ON_ONCE(!list_empty(&req->r_wait));
739 kmem_cache_free(ceph_mds_request_cachep, req);
742 DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
745 * lookup session, bump ref if found.
747 * called under mdsc->mutex.
749 static struct ceph_mds_request *
750 lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
752 struct ceph_mds_request *req;
754 req = lookup_request(&mdsc->request_tree, tid);
756 ceph_mdsc_get_request(req);
762 * Register an in-flight request, and assign a tid. Link to directory
763 * are modifying (if any).
765 * Called under mdsc->mutex.
767 static void __register_request(struct ceph_mds_client *mdsc,
768 struct ceph_mds_request *req,
773 req->r_tid = ++mdsc->last_tid;
774 if (req->r_num_caps) {
775 ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
778 pr_err("__register_request %p "
779 "failed to reserve caps: %d\n", req, ret);
780 /* set req->r_err to fail early from __do_request */
785 dout("__register_request %p tid %lld\n", req, req->r_tid);
786 ceph_mdsc_get_request(req);
787 insert_request(&mdsc->request_tree, req);
789 req->r_uid = current_fsuid();
790 req->r_gid = current_fsgid();
792 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
793 mdsc->oldest_tid = req->r_tid;
796 struct ceph_inode_info *ci = ceph_inode(dir);
799 req->r_unsafe_dir = dir;
800 spin_lock(&ci->i_unsafe_lock);
801 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
802 spin_unlock(&ci->i_unsafe_lock);
806 static void __unregister_request(struct ceph_mds_client *mdsc,
807 struct ceph_mds_request *req)
809 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
811 /* Never leave an unregistered request on an unsafe list! */
812 list_del_init(&req->r_unsafe_item);
814 if (req->r_tid == mdsc->oldest_tid) {
815 struct rb_node *p = rb_next(&req->r_node);
816 mdsc->oldest_tid = 0;
818 struct ceph_mds_request *next_req =
819 rb_entry(p, struct ceph_mds_request, r_node);
820 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
821 mdsc->oldest_tid = next_req->r_tid;
828 erase_request(&mdsc->request_tree, req);
830 if (req->r_unsafe_dir) {
831 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
832 spin_lock(&ci->i_unsafe_lock);
833 list_del_init(&req->r_unsafe_dir_item);
834 spin_unlock(&ci->i_unsafe_lock);
836 if (req->r_target_inode &&
837 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
838 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
839 spin_lock(&ci->i_unsafe_lock);
840 list_del_init(&req->r_unsafe_target_item);
841 spin_unlock(&ci->i_unsafe_lock);
844 if (req->r_unsafe_dir) {
845 /* avoid calling iput_final() in mds dispatch threads */
846 ceph_async_iput(req->r_unsafe_dir);
847 req->r_unsafe_dir = NULL;
850 complete_all(&req->r_safe_completion);
852 ceph_mdsc_put_request(req);
856 * Walk back up the dentry tree until we hit a dentry representing a
857 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
858 * when calling this) to ensure that the objects won't disappear while we're
859 * working with them. Once we hit a candidate dentry, we attempt to take a
860 * reference to it, and return that as the result.
862 static struct inode *get_nonsnap_parent(struct dentry *dentry)
864 struct inode *inode = NULL;
866 while (dentry && !IS_ROOT(dentry)) {
867 inode = d_inode_rcu(dentry);
868 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
870 dentry = dentry->d_parent;
873 inode = igrab(inode);
878 * Choose mds to send request to next. If there is a hint set in the
879 * request (e.g., due to a prior forward hint from the mds), use that.
880 * Otherwise, consult frag tree and/or caps to identify the
881 * appropriate mds. If all else fails, choose randomly.
883 * Called under mdsc->mutex.
885 static int __choose_mds(struct ceph_mds_client *mdsc,
886 struct ceph_mds_request *req,
890 struct ceph_inode_info *ci;
891 struct ceph_cap *cap;
892 int mode = req->r_direct_mode;
894 u32 hash = req->r_direct_hash;
895 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
901 * is there a specific mds we should try? ignore hint if we have
902 * no session and the mds is not up (active or recovering).
904 if (req->r_resend_mds >= 0 &&
905 (__have_session(mdsc, req->r_resend_mds) ||
906 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
907 dout("%s using resend_mds mds%d\n", __func__,
909 return req->r_resend_mds;
912 if (mode == USE_RANDOM_MDS)
917 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
918 inode = req->r_inode;
921 /* req->r_dentry is non-null for LSSNAP request */
923 inode = get_nonsnap_parent(req->r_dentry);
925 dout("%s using snapdir's parent %p\n", __func__, inode);
927 } else if (req->r_dentry) {
928 /* ignore race with rename; old or new d_parent is okay */
929 struct dentry *parent;
933 parent = READ_ONCE(req->r_dentry->d_parent);
934 dir = req->r_parent ? : d_inode_rcu(parent);
936 if (!dir || dir->i_sb != mdsc->fsc->sb) {
937 /* not this fs or parent went negative */
938 inode = d_inode(req->r_dentry);
941 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
942 /* direct snapped/virtual snapdir requests
943 * based on parent dir inode */
944 inode = get_nonsnap_parent(parent);
945 dout("%s using nonsnap parent %p\n", __func__, inode);
948 inode = d_inode(req->r_dentry);
949 if (!inode || mode == USE_AUTH_MDS) {
952 hash = ceph_dentry_hash(dir, req->r_dentry);
961 dout("%s %p is_hash=%d (0x%x) mode %d\n", __func__, inode, (int)is_hash,
965 ci = ceph_inode(inode);
967 if (is_hash && S_ISDIR(inode->i_mode)) {
968 struct ceph_inode_frag frag;
971 ceph_choose_frag(ci, hash, &frag, &found);
973 if (mode == USE_ANY_MDS && frag.ndist > 0) {
976 /* choose a random replica */
977 get_random_bytes(&r, 1);
980 dout("%s %p %llx.%llx frag %u mds%d (%d/%d)\n",
981 __func__, inode, ceph_vinop(inode),
982 frag.frag, mds, (int)r, frag.ndist);
983 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
984 CEPH_MDS_STATE_ACTIVE &&
985 !ceph_mdsmap_is_laggy(mdsc->mdsmap, mds))
989 /* since this file/dir wasn't known to be
990 * replicated, then we want to look for the
991 * authoritative mds. */
993 /* choose auth mds */
995 dout("%s %p %llx.%llx frag %u mds%d (auth)\n",
996 __func__, inode, ceph_vinop(inode),
998 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
999 CEPH_MDS_STATE_ACTIVE) {
1000 if (mode == USE_ANY_MDS &&
1001 !ceph_mdsmap_is_laggy(mdsc->mdsmap,
1006 mode = USE_AUTH_MDS;
1010 spin_lock(&ci->i_ceph_lock);
1012 if (mode == USE_AUTH_MDS)
1013 cap = ci->i_auth_cap;
1014 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
1015 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
1017 spin_unlock(&ci->i_ceph_lock);
1018 ceph_async_iput(inode);
1021 mds = cap->session->s_mds;
1022 dout("%s %p %llx.%llx mds%d (%scap %p)\n", __func__,
1023 inode, ceph_vinop(inode), mds,
1024 cap == ci->i_auth_cap ? "auth " : "", cap);
1025 spin_unlock(&ci->i_ceph_lock);
1027 /* avoid calling iput_final() while holding mdsc->mutex or
1028 * in mds dispatch threads */
1029 ceph_async_iput(inode);
1036 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
1037 dout("%s chose random mds%d\n", __func__, mds);
1045 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
1047 struct ceph_msg *msg;
1048 struct ceph_mds_session_head *h;
1050 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
1053 pr_err("create_session_msg ENOMEM creating msg\n");
1056 h = msg->front.iov_base;
1057 h->op = cpu_to_le32(op);
1058 h->seq = cpu_to_le64(seq);
1063 static const unsigned char feature_bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
1064 #define FEATURE_BYTES(c) (DIV_ROUND_UP((size_t)feature_bits[c - 1] + 1, 64) * 8)
1065 static void encode_supported_features(void **p, void *end)
1067 static const size_t count = ARRAY_SIZE(feature_bits);
1071 size_t size = FEATURE_BYTES(count);
1073 BUG_ON(*p + 4 + size > end);
1074 ceph_encode_32(p, size);
1075 memset(*p, 0, size);
1076 for (i = 0; i < count; i++)
1077 ((unsigned char*)(*p))[i / 8] |= BIT(feature_bits[i] % 8);
1080 BUG_ON(*p + 4 > end);
1081 ceph_encode_32(p, 0);
1086 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
1087 * to include additional client metadata fields.
1089 static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
1091 struct ceph_msg *msg;
1092 struct ceph_mds_session_head *h;
1094 int extra_bytes = 0;
1095 int metadata_key_count = 0;
1096 struct ceph_options *opt = mdsc->fsc->client->options;
1097 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
1101 const char* metadata[][2] = {
1102 {"hostname", mdsc->nodename},
1103 {"kernel_version", init_utsname()->release},
1104 {"entity_id", opt->name ? : ""},
1105 {"root", fsopt->server_path ? : "/"},
1109 /* Calculate serialized length of metadata */
1110 extra_bytes = 4; /* map length */
1111 for (i = 0; metadata[i][0]; ++i) {
1112 extra_bytes += 8 + strlen(metadata[i][0]) +
1113 strlen(metadata[i][1]);
1114 metadata_key_count++;
1117 /* supported feature */
1119 count = ARRAY_SIZE(feature_bits);
1121 size = FEATURE_BYTES(count);
1122 extra_bytes += 4 + size;
1124 /* Allocate the message */
1125 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
1128 pr_err("create_session_msg ENOMEM creating msg\n");
1131 p = msg->front.iov_base;
1132 end = p + msg->front.iov_len;
1135 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
1136 h->seq = cpu_to_le64(seq);
1139 * Serialize client metadata into waiting buffer space, using
1140 * the format that userspace expects for map<string, string>
1142 * ClientSession messages with metadata are v3
1144 msg->hdr.version = cpu_to_le16(3);
1145 msg->hdr.compat_version = cpu_to_le16(1);
1147 /* The write pointer, following the session_head structure */
1150 /* Number of entries in the map */
1151 ceph_encode_32(&p, metadata_key_count);
1153 /* Two length-prefixed strings for each entry in the map */
1154 for (i = 0; metadata[i][0]; ++i) {
1155 size_t const key_len = strlen(metadata[i][0]);
1156 size_t const val_len = strlen(metadata[i][1]);
1158 ceph_encode_32(&p, key_len);
1159 memcpy(p, metadata[i][0], key_len);
1161 ceph_encode_32(&p, val_len);
1162 memcpy(p, metadata[i][1], val_len);
1166 encode_supported_features(&p, end);
1167 msg->front.iov_len = p - msg->front.iov_base;
1168 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1174 * send session open request.
1176 * called under mdsc->mutex
1178 static int __open_session(struct ceph_mds_client *mdsc,
1179 struct ceph_mds_session *session)
1181 struct ceph_msg *msg;
1183 int mds = session->s_mds;
1185 /* wait for mds to go active? */
1186 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
1187 dout("open_session to mds%d (%s)\n", mds,
1188 ceph_mds_state_name(mstate));
1189 session->s_state = CEPH_MDS_SESSION_OPENING;
1190 session->s_renew_requested = jiffies;
1192 /* send connect message */
1193 msg = create_session_open_msg(mdsc, session->s_seq);
1196 ceph_con_send(&session->s_con, msg);
1201 * open sessions for any export targets for the given mds
1203 * called under mdsc->mutex
1205 static struct ceph_mds_session *
1206 __open_export_target_session(struct ceph_mds_client *mdsc, int target)
1208 struct ceph_mds_session *session;
1210 session = __ceph_lookup_mds_session(mdsc, target);
1212 session = register_session(mdsc, target);
1213 if (IS_ERR(session))
1216 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1217 session->s_state == CEPH_MDS_SESSION_CLOSING)
1218 __open_session(mdsc, session);
1223 struct ceph_mds_session *
1224 ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1226 struct ceph_mds_session *session;
1228 dout("open_export_target_session to mds%d\n", target);
1230 mutex_lock(&mdsc->mutex);
1231 session = __open_export_target_session(mdsc, target);
1232 mutex_unlock(&mdsc->mutex);
1237 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1238 struct ceph_mds_session *session)
1240 struct ceph_mds_info *mi;
1241 struct ceph_mds_session *ts;
1242 int i, mds = session->s_mds;
1244 if (mds >= mdsc->mdsmap->possible_max_rank)
1247 mi = &mdsc->mdsmap->m_info[mds];
1248 dout("open_export_target_sessions for mds%d (%d targets)\n",
1249 session->s_mds, mi->num_export_targets);
1251 for (i = 0; i < mi->num_export_targets; i++) {
1252 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1254 ceph_put_mds_session(ts);
1258 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1259 struct ceph_mds_session *session)
1261 mutex_lock(&mdsc->mutex);
1262 __open_export_target_sessions(mdsc, session);
1263 mutex_unlock(&mdsc->mutex);
1270 static void detach_cap_releases(struct ceph_mds_session *session,
1271 struct list_head *target)
1273 lockdep_assert_held(&session->s_cap_lock);
1275 list_splice_init(&session->s_cap_releases, target);
1276 session->s_num_cap_releases = 0;
1277 dout("dispose_cap_releases mds%d\n", session->s_mds);
1280 static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1281 struct list_head *dispose)
1283 while (!list_empty(dispose)) {
1284 struct ceph_cap *cap;
1285 /* zero out the in-progress message */
1286 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
1287 list_del(&cap->session_caps);
1288 ceph_put_cap(mdsc, cap);
1292 static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1293 struct ceph_mds_session *session)
1295 struct ceph_mds_request *req;
1297 struct ceph_inode_info *ci;
1299 dout("cleanup_session_requests mds%d\n", session->s_mds);
1300 mutex_lock(&mdsc->mutex);
1301 while (!list_empty(&session->s_unsafe)) {
1302 req = list_first_entry(&session->s_unsafe,
1303 struct ceph_mds_request, r_unsafe_item);
1304 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1306 if (req->r_target_inode) {
1307 /* dropping unsafe change of inode's attributes */
1308 ci = ceph_inode(req->r_target_inode);
1309 errseq_set(&ci->i_meta_err, -EIO);
1311 if (req->r_unsafe_dir) {
1312 /* dropping unsafe directory operation */
1313 ci = ceph_inode(req->r_unsafe_dir);
1314 errseq_set(&ci->i_meta_err, -EIO);
1316 __unregister_request(mdsc, req);
1318 /* zero r_attempts, so kick_requests() will re-send requests */
1319 p = rb_first(&mdsc->request_tree);
1321 req = rb_entry(p, struct ceph_mds_request, r_node);
1323 if (req->r_session &&
1324 req->r_session->s_mds == session->s_mds)
1325 req->r_attempts = 0;
1327 mutex_unlock(&mdsc->mutex);
1331 * Helper to safely iterate over all caps associated with a session, with
1332 * special care taken to handle a racing __ceph_remove_cap().
1334 * Caller must hold session s_mutex.
1336 int ceph_iterate_session_caps(struct ceph_mds_session *session,
1337 int (*cb)(struct inode *, struct ceph_cap *,
1340 struct list_head *p;
1341 struct ceph_cap *cap;
1342 struct inode *inode, *last_inode = NULL;
1343 struct ceph_cap *old_cap = NULL;
1346 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1347 spin_lock(&session->s_cap_lock);
1348 p = session->s_caps.next;
1349 while (p != &session->s_caps) {
1350 cap = list_entry(p, struct ceph_cap, session_caps);
1351 inode = igrab(&cap->ci->vfs_inode);
1356 session->s_cap_iterator = cap;
1357 spin_unlock(&session->s_cap_lock);
1360 /* avoid calling iput_final() while holding
1361 * s_mutex or in mds dispatch threads */
1362 ceph_async_iput(last_inode);
1366 ceph_put_cap(session->s_mdsc, old_cap);
1370 ret = cb(inode, cap, arg);
1373 spin_lock(&session->s_cap_lock);
1376 dout("iterate_session_caps finishing cap %p removal\n",
1378 BUG_ON(cap->session != session);
1379 cap->session = NULL;
1380 list_del_init(&cap->session_caps);
1381 session->s_nr_caps--;
1382 if (cap->queue_release)
1383 __ceph_queue_cap_release(session, cap);
1385 old_cap = cap; /* put_cap it w/o locks held */
1392 session->s_cap_iterator = NULL;
1393 spin_unlock(&session->s_cap_lock);
1395 ceph_async_iput(last_inode);
1397 ceph_put_cap(session->s_mdsc, old_cap);
1402 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
1405 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
1406 struct ceph_inode_info *ci = ceph_inode(inode);
1407 LIST_HEAD(to_remove);
1408 bool dirty_dropped = false;
1409 bool invalidate = false;
1411 dout("removing cap %p, ci is %p, inode is %p\n",
1412 cap, ci, &ci->vfs_inode);
1413 spin_lock(&ci->i_ceph_lock);
1414 if (cap->mds_wanted | cap->issued)
1415 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1416 __ceph_remove_cap(cap, false);
1417 if (!ci->i_auth_cap) {
1418 struct ceph_cap_flush *cf;
1419 struct ceph_mds_client *mdsc = fsc->mdsc;
1421 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1422 if (inode->i_data.nrpages > 0)
1424 if (ci->i_wrbuffer_ref > 0)
1425 mapping_set_error(&inode->i_data, -EIO);
1428 while (!list_empty(&ci->i_cap_flush_list)) {
1429 cf = list_first_entry(&ci->i_cap_flush_list,
1430 struct ceph_cap_flush, i_list);
1431 list_move(&cf->i_list, &to_remove);
1434 spin_lock(&mdsc->cap_dirty_lock);
1436 list_for_each_entry(cf, &to_remove, i_list)
1437 list_del(&cf->g_list);
1439 if (!list_empty(&ci->i_dirty_item)) {
1440 pr_warn_ratelimited(
1441 " dropping dirty %s state for %p %lld\n",
1442 ceph_cap_string(ci->i_dirty_caps),
1443 inode, ceph_ino(inode));
1444 ci->i_dirty_caps = 0;
1445 list_del_init(&ci->i_dirty_item);
1446 dirty_dropped = true;
1448 if (!list_empty(&ci->i_flushing_item)) {
1449 pr_warn_ratelimited(
1450 " dropping dirty+flushing %s state for %p %lld\n",
1451 ceph_cap_string(ci->i_flushing_caps),
1452 inode, ceph_ino(inode));
1453 ci->i_flushing_caps = 0;
1454 list_del_init(&ci->i_flushing_item);
1455 mdsc->num_cap_flushing--;
1456 dirty_dropped = true;
1458 spin_unlock(&mdsc->cap_dirty_lock);
1460 if (dirty_dropped) {
1461 errseq_set(&ci->i_meta_err, -EIO);
1463 if (ci->i_wrbuffer_ref_head == 0 &&
1464 ci->i_wr_ref == 0 &&
1465 ci->i_dirty_caps == 0 &&
1466 ci->i_flushing_caps == 0) {
1467 ceph_put_snap_context(ci->i_head_snapc);
1468 ci->i_head_snapc = NULL;
1472 if (atomic_read(&ci->i_filelock_ref) > 0) {
1473 /* make further file lock syscall return -EIO */
1474 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1475 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1476 inode, ceph_ino(inode));
1479 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
1480 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
1481 ci->i_prealloc_cap_flush = NULL;
1484 spin_unlock(&ci->i_ceph_lock);
1485 while (!list_empty(&to_remove)) {
1486 struct ceph_cap_flush *cf;
1487 cf = list_first_entry(&to_remove,
1488 struct ceph_cap_flush, i_list);
1489 list_del(&cf->i_list);
1490 ceph_free_cap_flush(cf);
1493 wake_up_all(&ci->i_cap_wq);
1495 ceph_queue_invalidate(inode);
1502 * caller must hold session s_mutex
1504 static void remove_session_caps(struct ceph_mds_session *session)
1506 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1507 struct super_block *sb = fsc->sb;
1510 dout("remove_session_caps on %p\n", session);
1511 ceph_iterate_session_caps(session, remove_session_caps_cb, fsc);
1513 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1515 spin_lock(&session->s_cap_lock);
1516 if (session->s_nr_caps > 0) {
1517 struct inode *inode;
1518 struct ceph_cap *cap, *prev = NULL;
1519 struct ceph_vino vino;
1521 * iterate_session_caps() skips inodes that are being
1522 * deleted, we need to wait until deletions are complete.
1523 * __wait_on_freeing_inode() is designed for the job,
1524 * but it is not exported, so use lookup inode function
1527 while (!list_empty(&session->s_caps)) {
1528 cap = list_entry(session->s_caps.next,
1529 struct ceph_cap, session_caps);
1533 vino = cap->ci->i_vino;
1534 spin_unlock(&session->s_cap_lock);
1536 inode = ceph_find_inode(sb, vino);
1537 /* avoid calling iput_final() while holding s_mutex */
1538 ceph_async_iput(inode);
1540 spin_lock(&session->s_cap_lock);
1544 // drop cap expires and unlock s_cap_lock
1545 detach_cap_releases(session, &dispose);
1547 BUG_ON(session->s_nr_caps > 0);
1548 BUG_ON(!list_empty(&session->s_cap_flushing));
1549 spin_unlock(&session->s_cap_lock);
1550 dispose_cap_releases(session->s_mdsc, &dispose);
1560 * wake up any threads waiting on this session's caps. if the cap is
1561 * old (didn't get renewed on the client reconnect), remove it now.
1563 * caller must hold s_mutex.
1565 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1568 struct ceph_inode_info *ci = ceph_inode(inode);
1569 unsigned long ev = (unsigned long)arg;
1571 if (ev == RECONNECT) {
1572 spin_lock(&ci->i_ceph_lock);
1573 ci->i_wanted_max_size = 0;
1574 ci->i_requested_max_size = 0;
1575 spin_unlock(&ci->i_ceph_lock);
1576 } else if (ev == RENEWCAPS) {
1577 if (cap->cap_gen < cap->session->s_cap_gen) {
1578 /* mds did not re-issue stale cap */
1579 spin_lock(&ci->i_ceph_lock);
1580 cap->issued = cap->implemented = CEPH_CAP_PIN;
1581 /* make sure mds knows what we want */
1582 if (__ceph_caps_file_wanted(ci) & ~cap->mds_wanted)
1583 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1584 spin_unlock(&ci->i_ceph_lock);
1586 } else if (ev == FORCE_RO) {
1588 wake_up_all(&ci->i_cap_wq);
1592 static void wake_up_session_caps(struct ceph_mds_session *session, int ev)
1594 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1595 ceph_iterate_session_caps(session, wake_up_session_cb,
1596 (void *)(unsigned long)ev);
1600 * Send periodic message to MDS renewing all currently held caps. The
1601 * ack will reset the expiration for all caps from this session.
1603 * caller holds s_mutex
1605 static int send_renew_caps(struct ceph_mds_client *mdsc,
1606 struct ceph_mds_session *session)
1608 struct ceph_msg *msg;
1611 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1612 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1613 pr_info("mds%d caps stale\n", session->s_mds);
1614 session->s_renew_requested = jiffies;
1616 /* do not try to renew caps until a recovering mds has reconnected
1617 * with its clients. */
1618 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1619 if (state < CEPH_MDS_STATE_RECONNECT) {
1620 dout("send_renew_caps ignoring mds%d (%s)\n",
1621 session->s_mds, ceph_mds_state_name(state));
1625 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1626 ceph_mds_state_name(state));
1627 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1628 ++session->s_renew_seq);
1631 ceph_con_send(&session->s_con, msg);
1635 static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1636 struct ceph_mds_session *session, u64 seq)
1638 struct ceph_msg *msg;
1640 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1641 session->s_mds, ceph_session_state_name(session->s_state), seq);
1642 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1645 ceph_con_send(&session->s_con, msg);
1651 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1653 * Called under session->s_mutex
1655 static void renewed_caps(struct ceph_mds_client *mdsc,
1656 struct ceph_mds_session *session, int is_renew)
1661 spin_lock(&session->s_cap_lock);
1662 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
1664 session->s_cap_ttl = session->s_renew_requested +
1665 mdsc->mdsmap->m_session_timeout*HZ;
1668 if (time_before(jiffies, session->s_cap_ttl)) {
1669 pr_info("mds%d caps renewed\n", session->s_mds);
1672 pr_info("mds%d caps still stale\n", session->s_mds);
1675 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1676 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1677 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1678 spin_unlock(&session->s_cap_lock);
1681 wake_up_session_caps(session, RENEWCAPS);
1685 * send a session close request
1687 static int request_close_session(struct ceph_mds_client *mdsc,
1688 struct ceph_mds_session *session)
1690 struct ceph_msg *msg;
1692 dout("request_close_session mds%d state %s seq %lld\n",
1693 session->s_mds, ceph_session_state_name(session->s_state),
1695 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1698 ceph_con_send(&session->s_con, msg);
1703 * Called with s_mutex held.
1705 static int __close_session(struct ceph_mds_client *mdsc,
1706 struct ceph_mds_session *session)
1708 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1710 session->s_state = CEPH_MDS_SESSION_CLOSING;
1711 return request_close_session(mdsc, session);
1714 static bool drop_negative_children(struct dentry *dentry)
1716 struct dentry *child;
1717 bool all_negative = true;
1719 if (!d_is_dir(dentry))
1722 spin_lock(&dentry->d_lock);
1723 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1724 if (d_really_is_positive(child)) {
1725 all_negative = false;
1729 spin_unlock(&dentry->d_lock);
1732 shrink_dcache_parent(dentry);
1734 return all_negative;
1738 * Trim old(er) caps.
1740 * Because we can't cache an inode without one or more caps, we do
1741 * this indirectly: if a cap is unused, we prune its aliases, at which
1742 * point the inode will hopefully get dropped to.
1744 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1745 * memory pressure from the MDS, though, so it needn't be perfect.
1747 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1749 int *remaining = arg;
1750 struct ceph_inode_info *ci = ceph_inode(inode);
1751 int used, wanted, oissued, mine;
1753 if (*remaining <= 0)
1756 spin_lock(&ci->i_ceph_lock);
1757 mine = cap->issued | cap->implemented;
1758 used = __ceph_caps_used(ci);
1759 wanted = __ceph_caps_file_wanted(ci);
1760 oissued = __ceph_caps_issued_other(ci, cap);
1762 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1763 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1764 ceph_cap_string(used), ceph_cap_string(wanted));
1765 if (cap == ci->i_auth_cap) {
1766 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1767 !list_empty(&ci->i_cap_snaps))
1769 if ((used | wanted) & CEPH_CAP_ANY_WR)
1771 /* Note: it's possible that i_filelock_ref becomes non-zero
1772 * after dropping auth caps. It doesn't hurt because reply
1773 * of lock mds request will re-add auth caps. */
1774 if (atomic_read(&ci->i_filelock_ref) > 0)
1777 /* The inode has cached pages, but it's no longer used.
1778 * we can safely drop it */
1779 if (S_ISREG(inode->i_mode) &&
1780 wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
1781 !(oissued & CEPH_CAP_FILE_CACHE)) {
1785 if ((used | wanted) & ~oissued & mine)
1786 goto out; /* we need these caps */
1789 /* we aren't the only cap.. just remove us */
1790 __ceph_remove_cap(cap, true);
1793 struct dentry *dentry;
1794 /* try dropping referring dentries */
1795 spin_unlock(&ci->i_ceph_lock);
1796 dentry = d_find_any_alias(inode);
1797 if (dentry && drop_negative_children(dentry)) {
1800 d_prune_aliases(inode);
1801 count = atomic_read(&inode->i_count);
1804 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1813 spin_unlock(&ci->i_ceph_lock);
1818 * Trim session cap count down to some max number.
1820 int ceph_trim_caps(struct ceph_mds_client *mdsc,
1821 struct ceph_mds_session *session,
1824 int trim_caps = session->s_nr_caps - max_caps;
1826 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1827 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1828 if (trim_caps > 0) {
1829 int remaining = trim_caps;
1831 ceph_iterate_session_caps(session, trim_caps_cb, &remaining);
1832 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1833 session->s_mds, session->s_nr_caps, max_caps,
1834 trim_caps - remaining);
1837 ceph_flush_cap_releases(mdsc, session);
1841 static int check_caps_flush(struct ceph_mds_client *mdsc,
1846 spin_lock(&mdsc->cap_dirty_lock);
1847 if (!list_empty(&mdsc->cap_flush_list)) {
1848 struct ceph_cap_flush *cf =
1849 list_first_entry(&mdsc->cap_flush_list,
1850 struct ceph_cap_flush, g_list);
1851 if (cf->tid <= want_flush_tid) {
1852 dout("check_caps_flush still flushing tid "
1853 "%llu <= %llu\n", cf->tid, want_flush_tid);
1857 spin_unlock(&mdsc->cap_dirty_lock);
1862 * flush all dirty inode data to disk.
1864 * returns true if we've flushed through want_flush_tid
1866 static void wait_caps_flush(struct ceph_mds_client *mdsc,
1869 dout("check_caps_flush want %llu\n", want_flush_tid);
1871 wait_event(mdsc->cap_flushing_wq,
1872 check_caps_flush(mdsc, want_flush_tid));
1874 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
1878 * called under s_mutex
1880 static void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1881 struct ceph_mds_session *session)
1883 struct ceph_msg *msg = NULL;
1884 struct ceph_mds_cap_release *head;
1885 struct ceph_mds_cap_item *item;
1886 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
1887 struct ceph_cap *cap;
1888 LIST_HEAD(tmp_list);
1889 int num_cap_releases;
1890 __le32 barrier, *cap_barrier;
1892 down_read(&osdc->lock);
1893 barrier = cpu_to_le32(osdc->epoch_barrier);
1894 up_read(&osdc->lock);
1896 spin_lock(&session->s_cap_lock);
1898 list_splice_init(&session->s_cap_releases, &tmp_list);
1899 num_cap_releases = session->s_num_cap_releases;
1900 session->s_num_cap_releases = 0;
1901 spin_unlock(&session->s_cap_lock);
1903 while (!list_empty(&tmp_list)) {
1905 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
1906 PAGE_SIZE, GFP_NOFS, false);
1909 head = msg->front.iov_base;
1910 head->num = cpu_to_le32(0);
1911 msg->front.iov_len = sizeof(*head);
1913 msg->hdr.version = cpu_to_le16(2);
1914 msg->hdr.compat_version = cpu_to_le16(1);
1917 cap = list_first_entry(&tmp_list, struct ceph_cap,
1919 list_del(&cap->session_caps);
1922 head = msg->front.iov_base;
1923 put_unaligned_le32(get_unaligned_le32(&head->num) + 1,
1925 item = msg->front.iov_base + msg->front.iov_len;
1926 item->ino = cpu_to_le64(cap->cap_ino);
1927 item->cap_id = cpu_to_le64(cap->cap_id);
1928 item->migrate_seq = cpu_to_le32(cap->mseq);
1929 item->seq = cpu_to_le32(cap->issue_seq);
1930 msg->front.iov_len += sizeof(*item);
1932 ceph_put_cap(mdsc, cap);
1934 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
1935 // Append cap_barrier field
1936 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1937 *cap_barrier = barrier;
1938 msg->front.iov_len += sizeof(*cap_barrier);
1940 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1941 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1942 ceph_con_send(&session->s_con, msg);
1947 BUG_ON(num_cap_releases != 0);
1949 spin_lock(&session->s_cap_lock);
1950 if (!list_empty(&session->s_cap_releases))
1952 spin_unlock(&session->s_cap_lock);
1955 // Append cap_barrier field
1956 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1957 *cap_barrier = barrier;
1958 msg->front.iov_len += sizeof(*cap_barrier);
1960 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1961 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1962 ceph_con_send(&session->s_con, msg);
1966 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1968 spin_lock(&session->s_cap_lock);
1969 list_splice(&tmp_list, &session->s_cap_releases);
1970 session->s_num_cap_releases += num_cap_releases;
1971 spin_unlock(&session->s_cap_lock);
1974 static void ceph_cap_release_work(struct work_struct *work)
1976 struct ceph_mds_session *session =
1977 container_of(work, struct ceph_mds_session, s_cap_release_work);
1979 mutex_lock(&session->s_mutex);
1980 if (session->s_state == CEPH_MDS_SESSION_OPEN ||
1981 session->s_state == CEPH_MDS_SESSION_HUNG)
1982 ceph_send_cap_releases(session->s_mdsc, session);
1983 mutex_unlock(&session->s_mutex);
1984 ceph_put_mds_session(session);
1987 void ceph_flush_cap_releases(struct ceph_mds_client *mdsc,
1988 struct ceph_mds_session *session)
1993 ceph_get_mds_session(session);
1994 if (queue_work(mdsc->fsc->cap_wq,
1995 &session->s_cap_release_work)) {
1996 dout("cap release work queued\n");
1998 ceph_put_mds_session(session);
1999 dout("failed to queue cap release work\n");
2004 * caller holds session->s_cap_lock
2006 void __ceph_queue_cap_release(struct ceph_mds_session *session,
2007 struct ceph_cap *cap)
2009 list_add_tail(&cap->session_caps, &session->s_cap_releases);
2010 session->s_num_cap_releases++;
2012 if (!(session->s_num_cap_releases % CEPH_CAPS_PER_RELEASE))
2013 ceph_flush_cap_releases(session->s_mdsc, session);
2016 static void ceph_cap_reclaim_work(struct work_struct *work)
2018 struct ceph_mds_client *mdsc =
2019 container_of(work, struct ceph_mds_client, cap_reclaim_work);
2020 int ret = ceph_trim_dentries(mdsc);
2022 ceph_queue_cap_reclaim_work(mdsc);
2025 void ceph_queue_cap_reclaim_work(struct ceph_mds_client *mdsc)
2030 if (queue_work(mdsc->fsc->cap_wq, &mdsc->cap_reclaim_work)) {
2031 dout("caps reclaim work queued\n");
2033 dout("failed to queue caps release work\n");
2037 void ceph_reclaim_caps_nr(struct ceph_mds_client *mdsc, int nr)
2042 val = atomic_add_return(nr, &mdsc->cap_reclaim_pending);
2043 if ((val % CEPH_CAPS_PER_RELEASE) < nr) {
2044 atomic_set(&mdsc->cap_reclaim_pending, 0);
2045 ceph_queue_cap_reclaim_work(mdsc);
2053 int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
2056 struct ceph_inode_info *ci = ceph_inode(dir);
2057 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
2058 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
2059 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
2060 unsigned int num_entries;
2063 spin_lock(&ci->i_ceph_lock);
2064 num_entries = ci->i_files + ci->i_subdirs;
2065 spin_unlock(&ci->i_ceph_lock);
2066 num_entries = max(num_entries, 1U);
2067 num_entries = min(num_entries, opt->max_readdir);
2069 order = get_order(size * num_entries);
2070 while (order >= 0) {
2071 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
2074 if (rinfo->dir_entries)
2078 if (!rinfo->dir_entries)
2081 num_entries = (PAGE_SIZE << order) / size;
2082 num_entries = min(num_entries, opt->max_readdir);
2084 rinfo->dir_buf_size = PAGE_SIZE << order;
2085 req->r_num_caps = num_entries + 1;
2086 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
2087 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
2092 * Create an mds request.
2094 struct ceph_mds_request *
2095 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
2097 struct ceph_mds_request *req;
2099 req = kmem_cache_zalloc(ceph_mds_request_cachep, GFP_NOFS);
2101 return ERR_PTR(-ENOMEM);
2103 mutex_init(&req->r_fill_mutex);
2105 req->r_started = jiffies;
2106 req->r_resend_mds = -1;
2107 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
2108 INIT_LIST_HEAD(&req->r_unsafe_target_item);
2110 kref_init(&req->r_kref);
2111 RB_CLEAR_NODE(&req->r_node);
2112 INIT_LIST_HEAD(&req->r_wait);
2113 init_completion(&req->r_completion);
2114 init_completion(&req->r_safe_completion);
2115 INIT_LIST_HEAD(&req->r_unsafe_item);
2117 ktime_get_coarse_real_ts64(&req->r_stamp);
2120 req->r_direct_mode = mode;
2125 * return oldest (lowest) request, tid in request tree, 0 if none.
2127 * called under mdsc->mutex.
2129 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
2131 if (RB_EMPTY_ROOT(&mdsc->request_tree))
2133 return rb_entry(rb_first(&mdsc->request_tree),
2134 struct ceph_mds_request, r_node);
2137 static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
2139 return mdsc->oldest_tid;
2143 * Build a dentry's path. Allocate on heap; caller must kfree. Based
2144 * on build_path_from_dentry in fs/cifs/dir.c.
2146 * If @stop_on_nosnap, generate path relative to the first non-snapped
2149 * Encode hidden .snap dirs as a double /, i.e.
2150 * foo/.snap/bar -> foo//bar
2152 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase,
2155 struct dentry *temp;
2162 return ERR_PTR(-EINVAL);
2166 return ERR_PTR(-ENOMEM);
2171 seq = read_seqbegin(&rename_lock);
2175 struct inode *inode;
2177 spin_lock(&temp->d_lock);
2178 inode = d_inode(temp);
2179 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
2180 dout("build_path path+%d: %p SNAPDIR\n",
2182 } else if (stop_on_nosnap && inode && dentry != temp &&
2183 ceph_snap(inode) == CEPH_NOSNAP) {
2184 spin_unlock(&temp->d_lock);
2185 pos++; /* get rid of any prepended '/' */
2188 pos -= temp->d_name.len;
2190 spin_unlock(&temp->d_lock);
2193 memcpy(path + pos, temp->d_name.name, temp->d_name.len);
2195 spin_unlock(&temp->d_lock);
2196 temp = READ_ONCE(temp->d_parent);
2198 /* Are we at the root? */
2202 /* Are we out of buffer? */
2208 base = ceph_ino(d_inode(temp));
2211 if (read_seqretry(&rename_lock, seq))
2216 * A rename didn't occur, but somehow we didn't end up where
2217 * we thought we would. Throw a warning and try again.
2219 pr_warn("build_path did not end path lookup where "
2220 "expected, pos is %d\n", pos);
2225 *plen = PATH_MAX - 1 - pos;
2226 dout("build_path on %p %d built %llx '%.*s'\n",
2227 dentry, d_count(dentry), base, *plen, path + pos);
2231 static int build_dentry_path(struct dentry *dentry, struct inode *dir,
2232 const char **ppath, int *ppathlen, u64 *pino,
2233 bool *pfreepath, bool parent_locked)
2239 dir = d_inode_rcu(dentry->d_parent);
2240 if (dir && parent_locked && ceph_snap(dir) == CEPH_NOSNAP) {
2241 *pino = ceph_ino(dir);
2243 *ppath = dentry->d_name.name;
2244 *ppathlen = dentry->d_name.len;
2248 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2250 return PTR_ERR(path);
2256 static int build_inode_path(struct inode *inode,
2257 const char **ppath, int *ppathlen, u64 *pino,
2260 struct dentry *dentry;
2263 if (ceph_snap(inode) == CEPH_NOSNAP) {
2264 *pino = ceph_ino(inode);
2268 dentry = d_find_alias(inode);
2269 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2272 return PTR_ERR(path);
2279 * request arguments may be specified via an inode *, a dentry *, or
2280 * an explicit ino+path.
2282 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
2283 struct inode *rdiri, const char *rpath,
2284 u64 rino, const char **ppath, int *pathlen,
2285 u64 *ino, bool *freepath, bool parent_locked)
2290 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
2291 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
2293 } else if (rdentry) {
2294 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
2295 freepath, parent_locked);
2296 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
2298 } else if (rpath || rino) {
2301 *pathlen = rpath ? strlen(rpath) : 0;
2302 dout(" path %.*s\n", *pathlen, rpath);
2309 * called under mdsc->mutex
2311 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
2312 struct ceph_mds_request *req,
2313 int mds, bool drop_cap_releases)
2315 struct ceph_msg *msg;
2316 struct ceph_mds_request_head *head;
2317 const char *path1 = NULL;
2318 const char *path2 = NULL;
2319 u64 ino1 = 0, ino2 = 0;
2320 int pathlen1 = 0, pathlen2 = 0;
2321 bool freepath1 = false, freepath2 = false;
2327 ret = set_request_path_attr(req->r_inode, req->r_dentry,
2328 req->r_parent, req->r_path1, req->r_ino1.ino,
2329 &path1, &pathlen1, &ino1, &freepath1,
2330 test_bit(CEPH_MDS_R_PARENT_LOCKED,
2331 &req->r_req_flags));
2337 /* If r_old_dentry is set, then assume that its parent is locked */
2338 ret = set_request_path_attr(NULL, req->r_old_dentry,
2339 req->r_old_dentry_dir,
2340 req->r_path2, req->r_ino2.ino,
2341 &path2, &pathlen2, &ino2, &freepath2, true);
2347 len = sizeof(*head) +
2348 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
2349 sizeof(struct ceph_timespec);
2351 /* calculate (max) length for cap releases */
2352 len += sizeof(struct ceph_mds_request_release) *
2353 (!!req->r_inode_drop + !!req->r_dentry_drop +
2354 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2355 if (req->r_dentry_drop)
2357 if (req->r_old_dentry_drop)
2360 msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
2362 msg = ERR_PTR(-ENOMEM);
2366 msg->hdr.version = cpu_to_le16(2);
2367 msg->hdr.tid = cpu_to_le64(req->r_tid);
2369 head = msg->front.iov_base;
2370 p = msg->front.iov_base + sizeof(*head);
2371 end = msg->front.iov_base + msg->front.iov_len;
2373 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2374 head->op = cpu_to_le32(req->r_op);
2375 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2376 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
2378 head->args = req->r_args;
2380 ceph_encode_filepath(&p, end, ino1, path1);
2381 ceph_encode_filepath(&p, end, ino2, path2);
2383 /* make note of release offset, in case we need to replay */
2384 req->r_request_release_offset = p - msg->front.iov_base;
2388 if (req->r_inode_drop)
2389 releases += ceph_encode_inode_release(&p,
2390 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
2391 mds, req->r_inode_drop, req->r_inode_unless, 0);
2392 if (req->r_dentry_drop)
2393 releases += ceph_encode_dentry_release(&p, req->r_dentry,
2394 req->r_parent, mds, req->r_dentry_drop,
2395 req->r_dentry_unless);
2396 if (req->r_old_dentry_drop)
2397 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
2398 req->r_old_dentry_dir, mds,
2399 req->r_old_dentry_drop,
2400 req->r_old_dentry_unless);
2401 if (req->r_old_inode_drop)
2402 releases += ceph_encode_inode_release(&p,
2403 d_inode(req->r_old_dentry),
2404 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
2406 if (drop_cap_releases) {
2408 p = msg->front.iov_base + req->r_request_release_offset;
2411 head->num_releases = cpu_to_le16(releases);
2415 struct ceph_timespec ts;
2416 ceph_encode_timespec64(&ts, &req->r_stamp);
2417 ceph_encode_copy(&p, &ts, sizeof(ts));
2421 msg->front.iov_len = p - msg->front.iov_base;
2422 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2424 if (req->r_pagelist) {
2425 struct ceph_pagelist *pagelist = req->r_pagelist;
2426 ceph_msg_data_add_pagelist(msg, pagelist);
2427 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2429 msg->hdr.data_len = 0;
2432 msg->hdr.data_off = cpu_to_le16(0);
2436 ceph_mdsc_free_path((char *)path2, pathlen2);
2439 ceph_mdsc_free_path((char *)path1, pathlen1);
2445 * called under mdsc->mutex if error, under no mutex if
2448 static void complete_request(struct ceph_mds_client *mdsc,
2449 struct ceph_mds_request *req)
2451 if (req->r_callback)
2452 req->r_callback(mdsc, req);
2453 complete_all(&req->r_completion);
2457 * called under mdsc->mutex
2459 static int __prepare_send_request(struct ceph_mds_client *mdsc,
2460 struct ceph_mds_request *req,
2461 int mds, bool drop_cap_releases)
2463 struct ceph_mds_request_head *rhead;
2464 struct ceph_msg *msg;
2469 struct ceph_cap *cap =
2470 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2473 req->r_sent_on_mseq = cap->mseq;
2475 req->r_sent_on_mseq = -1;
2477 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2478 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2480 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2483 * Replay. Do not regenerate message (and rebuild
2484 * paths, etc.); just use the original message.
2485 * Rebuilding paths will break for renames because
2486 * d_move mangles the src name.
2488 msg = req->r_request;
2489 rhead = msg->front.iov_base;
2491 flags = le32_to_cpu(rhead->flags);
2492 flags |= CEPH_MDS_FLAG_REPLAY;
2493 rhead->flags = cpu_to_le32(flags);
2495 if (req->r_target_inode)
2496 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2498 rhead->num_retry = req->r_attempts - 1;
2500 /* remove cap/dentry releases from message */
2501 rhead->num_releases = 0;
2504 p = msg->front.iov_base + req->r_request_release_offset;
2506 struct ceph_timespec ts;
2507 ceph_encode_timespec64(&ts, &req->r_stamp);
2508 ceph_encode_copy(&p, &ts, sizeof(ts));
2511 msg->front.iov_len = p - msg->front.iov_base;
2512 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2516 if (req->r_request) {
2517 ceph_msg_put(req->r_request);
2518 req->r_request = NULL;
2520 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
2522 req->r_err = PTR_ERR(msg);
2523 return PTR_ERR(msg);
2525 req->r_request = msg;
2527 rhead = msg->front.iov_base;
2528 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
2529 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2530 flags |= CEPH_MDS_FLAG_REPLAY;
2531 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags))
2532 flags |= CEPH_MDS_FLAG_ASYNC;
2534 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2535 rhead->flags = cpu_to_le32(flags);
2536 rhead->num_fwd = req->r_num_fwd;
2537 rhead->num_retry = req->r_attempts - 1;
2540 dout(" r_parent = %p\n", req->r_parent);
2545 * called under mdsc->mutex
2547 static int __send_request(struct ceph_mds_client *mdsc,
2548 struct ceph_mds_session *session,
2549 struct ceph_mds_request *req,
2550 bool drop_cap_releases)
2554 err = __prepare_send_request(mdsc, req, session->s_mds,
2557 ceph_msg_get(req->r_request);
2558 ceph_con_send(&session->s_con, req->r_request);
2565 * send request, or put it on the appropriate wait list.
2567 static void __do_request(struct ceph_mds_client *mdsc,
2568 struct ceph_mds_request *req)
2570 struct ceph_mds_session *session = NULL;
2575 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2576 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
2577 __unregister_request(mdsc, req);
2581 if (req->r_timeout &&
2582 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2583 dout("do_request timed out\n");
2587 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
2588 dout("do_request forced umount\n");
2592 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
2593 if (mdsc->mdsmap_err) {
2594 err = mdsc->mdsmap_err;
2595 dout("do_request mdsmap err %d\n", err);
2598 if (mdsc->mdsmap->m_epoch == 0) {
2599 dout("do_request no mdsmap, waiting for map\n");
2600 list_add(&req->r_wait, &mdsc->waiting_for_map);
2603 if (!(mdsc->fsc->mount_options->flags &
2604 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2605 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
2606 err = -EHOSTUNREACH;
2611 put_request_session(req);
2613 mds = __choose_mds(mdsc, req, &random);
2615 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2616 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags)) {
2620 dout("do_request no mds or not active, waiting for map\n");
2621 list_add(&req->r_wait, &mdsc->waiting_for_map);
2625 /* get, open session */
2626 session = __ceph_lookup_mds_session(mdsc, mds);
2628 session = register_session(mdsc, mds);
2629 if (IS_ERR(session)) {
2630 err = PTR_ERR(session);
2634 req->r_session = ceph_get_mds_session(session);
2636 dout("do_request mds%d session %p state %s\n", mds, session,
2637 ceph_session_state_name(session->s_state));
2638 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2639 session->s_state != CEPH_MDS_SESSION_HUNG) {
2640 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2645 * We cannot queue async requests since the caps and delegated
2646 * inodes are bound to the session. Just return -EJUKEBOX and
2647 * let the caller retry a sync request in that case.
2649 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags)) {
2653 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2654 session->s_state == CEPH_MDS_SESSION_CLOSING) {
2655 __open_session(mdsc, session);
2656 /* retry the same mds later */
2658 req->r_resend_mds = mds;
2660 list_add(&req->r_wait, &session->s_waiting);
2665 req->r_resend_mds = -1; /* forget any previous mds hint */
2667 if (req->r_request_started == 0) /* note request start time */
2668 req->r_request_started = jiffies;
2670 err = __send_request(mdsc, session, req, false);
2673 ceph_put_mds_session(session);
2676 dout("__do_request early error %d\n", err);
2678 complete_request(mdsc, req);
2679 __unregister_request(mdsc, req);
2685 * called under mdsc->mutex
2687 static void __wake_requests(struct ceph_mds_client *mdsc,
2688 struct list_head *head)
2690 struct ceph_mds_request *req;
2691 LIST_HEAD(tmp_list);
2693 list_splice_init(head, &tmp_list);
2695 while (!list_empty(&tmp_list)) {
2696 req = list_entry(tmp_list.next,
2697 struct ceph_mds_request, r_wait);
2698 list_del_init(&req->r_wait);
2699 dout(" wake request %p tid %llu\n", req, req->r_tid);
2700 __do_request(mdsc, req);
2705 * Wake up threads with requests pending for @mds, so that they can
2706 * resubmit their requests to a possibly different mds.
2708 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
2710 struct ceph_mds_request *req;
2711 struct rb_node *p = rb_first(&mdsc->request_tree);
2713 dout("kick_requests mds%d\n", mds);
2715 req = rb_entry(p, struct ceph_mds_request, r_node);
2717 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2719 if (req->r_attempts > 0)
2720 continue; /* only new requests */
2721 if (req->r_session &&
2722 req->r_session->s_mds == mds) {
2723 dout(" kicking tid %llu\n", req->r_tid);
2724 list_del_init(&req->r_wait);
2725 __do_request(mdsc, req);
2730 int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
2731 struct ceph_mds_request *req)
2735 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
2737 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
2738 if (req->r_parent) {
2739 ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
2740 ihold(req->r_parent);
2742 if (req->r_old_dentry_dir)
2743 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2747 err = ceph_wait_on_async_create(req->r_inode);
2749 dout("%s: wait for async create returned: %d\n",
2755 if (!err && req->r_old_inode) {
2756 err = ceph_wait_on_async_create(req->r_old_inode);
2758 dout("%s: wait for async create returned: %d\n",
2764 dout("submit_request on %p for inode %p\n", req, dir);
2765 mutex_lock(&mdsc->mutex);
2766 __register_request(mdsc, req, dir);
2767 __do_request(mdsc, req);
2769 mutex_unlock(&mdsc->mutex);
2773 static int ceph_mdsc_wait_request(struct ceph_mds_client *mdsc,
2774 struct ceph_mds_request *req)
2779 dout("do_request waiting\n");
2780 if (!req->r_timeout && req->r_wait_for_completion) {
2781 err = req->r_wait_for_completion(mdsc, req);
2783 long timeleft = wait_for_completion_killable_timeout(
2785 ceph_timeout_jiffies(req->r_timeout));
2789 err = -ETIMEDOUT; /* timed out */
2791 err = timeleft; /* killed */
2793 dout("do_request waited, got %d\n", err);
2794 mutex_lock(&mdsc->mutex);
2796 /* only abort if we didn't race with a real reply */
2797 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2798 err = le32_to_cpu(req->r_reply_info.head->result);
2799 } else if (err < 0) {
2800 dout("aborted request %lld with %d\n", req->r_tid, err);
2803 * ensure we aren't running concurrently with
2804 * ceph_fill_trace or ceph_readdir_prepopulate, which
2805 * rely on locks (dir mutex) held by our caller.
2807 mutex_lock(&req->r_fill_mutex);
2809 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
2810 mutex_unlock(&req->r_fill_mutex);
2812 if (req->r_parent &&
2813 (req->r_op & CEPH_MDS_OP_WRITE))
2814 ceph_invalidate_dir_request(req);
2819 mutex_unlock(&mdsc->mutex);
2824 * Synchrously perform an mds request. Take care of all of the
2825 * session setup, forwarding, retry details.
2827 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2829 struct ceph_mds_request *req)
2833 dout("do_request on %p\n", req);
2836 err = ceph_mdsc_submit_request(mdsc, dir, req);
2838 err = ceph_mdsc_wait_request(mdsc, req);
2839 dout("do_request %p done, result %d\n", req, err);
2844 * Invalidate dir's completeness, dentry lease state on an aborted MDS
2845 * namespace request.
2847 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2849 struct inode *dir = req->r_parent;
2850 struct inode *old_dir = req->r_old_dentry_dir;
2852 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
2854 ceph_dir_clear_complete(dir);
2856 ceph_dir_clear_complete(old_dir);
2858 ceph_invalidate_dentry_lease(req->r_dentry);
2859 if (req->r_old_dentry)
2860 ceph_invalidate_dentry_lease(req->r_old_dentry);
2866 * We take the session mutex and parse and process the reply immediately.
2867 * This preserves the logical ordering of replies, capabilities, etc., sent
2868 * by the MDS as they are applied to our local cache.
2870 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2872 struct ceph_mds_client *mdsc = session->s_mdsc;
2873 struct ceph_mds_request *req;
2874 struct ceph_mds_reply_head *head = msg->front.iov_base;
2875 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2876 struct ceph_snap_realm *realm;
2879 int mds = session->s_mds;
2881 if (msg->front.iov_len < sizeof(*head)) {
2882 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2887 /* get request, session */
2888 tid = le64_to_cpu(msg->hdr.tid);
2889 mutex_lock(&mdsc->mutex);
2890 req = lookup_get_request(mdsc, tid);
2892 dout("handle_reply on unknown tid %llu\n", tid);
2893 mutex_unlock(&mdsc->mutex);
2896 dout("handle_reply %p\n", req);
2898 /* correct session? */
2899 if (req->r_session != session) {
2900 pr_err("mdsc_handle_reply got %llu on session mds%d"
2901 " not mds%d\n", tid, session->s_mds,
2902 req->r_session ? req->r_session->s_mds : -1);
2903 mutex_unlock(&mdsc->mutex);
2908 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
2909 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
2910 pr_warn("got a dup %s reply on %llu from mds%d\n",
2911 head->safe ? "safe" : "unsafe", tid, mds);
2912 mutex_unlock(&mdsc->mutex);
2915 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
2916 pr_warn("got unsafe after safe on %llu from mds%d\n",
2918 mutex_unlock(&mdsc->mutex);
2922 result = le32_to_cpu(head->result);
2926 * if we're not talking to the authority, send to them
2927 * if the authority has changed while we weren't looking,
2928 * send to new authority
2929 * Otherwise we just have to return an ESTALE
2931 if (result == -ESTALE) {
2932 dout("got ESTALE on request %llu\n", req->r_tid);
2933 req->r_resend_mds = -1;
2934 if (req->r_direct_mode != USE_AUTH_MDS) {
2935 dout("not using auth, setting for that now\n");
2936 req->r_direct_mode = USE_AUTH_MDS;
2937 __do_request(mdsc, req);
2938 mutex_unlock(&mdsc->mutex);
2941 int mds = __choose_mds(mdsc, req, NULL);
2942 if (mds >= 0 && mds != req->r_session->s_mds) {
2943 dout("but auth changed, so resending\n");
2944 __do_request(mdsc, req);
2945 mutex_unlock(&mdsc->mutex);
2949 dout("have to return ESTALE on request %llu\n", req->r_tid);
2954 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
2955 __unregister_request(mdsc, req);
2957 /* last request during umount? */
2958 if (mdsc->stopping && !__get_oldest_req(mdsc))
2959 complete_all(&mdsc->safe_umount_waiters);
2961 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2963 * We already handled the unsafe response, now do the
2964 * cleanup. No need to examine the response; the MDS
2965 * doesn't include any result info in the safe
2966 * response. And even if it did, there is nothing
2967 * useful we could do with a revised return value.
2969 dout("got safe reply %llu, mds%d\n", tid, mds);
2971 mutex_unlock(&mdsc->mutex);
2975 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
2976 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2979 dout("handle_reply tid %lld result %d\n", tid, result);
2980 rinfo = &req->r_reply_info;
2981 if (test_bit(CEPHFS_FEATURE_REPLY_ENCODING, &session->s_features))
2982 err = parse_reply_info(msg, rinfo, (u64)-1);
2984 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2985 mutex_unlock(&mdsc->mutex);
2987 mutex_lock(&session->s_mutex);
2989 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
2996 if (rinfo->snapblob_len) {
2997 down_write(&mdsc->snap_rwsem);
2998 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2999 rinfo->snapblob + rinfo->snapblob_len,
3000 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
3002 downgrade_write(&mdsc->snap_rwsem);
3004 down_read(&mdsc->snap_rwsem);
3007 /* insert trace into our cache */
3008 mutex_lock(&req->r_fill_mutex);
3009 current->journal_info = req;
3010 err = ceph_fill_trace(mdsc->fsc->sb, req);
3012 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
3013 req->r_op == CEPH_MDS_OP_LSSNAP))
3014 ceph_readdir_prepopulate(req, req->r_session);
3016 current->journal_info = NULL;
3017 mutex_unlock(&req->r_fill_mutex);
3019 up_read(&mdsc->snap_rwsem);
3021 ceph_put_snap_realm(mdsc, realm);
3024 if (req->r_target_inode &&
3025 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
3026 struct ceph_inode_info *ci =
3027 ceph_inode(req->r_target_inode);
3028 spin_lock(&ci->i_unsafe_lock);
3029 list_add_tail(&req->r_unsafe_target_item,
3030 &ci->i_unsafe_iops);
3031 spin_unlock(&ci->i_unsafe_lock);
3034 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
3037 mutex_lock(&mdsc->mutex);
3038 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
3042 req->r_reply = ceph_msg_get(msg);
3043 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
3046 dout("reply arrived after request %lld was aborted\n", tid);
3048 mutex_unlock(&mdsc->mutex);
3050 mutex_unlock(&session->s_mutex);
3052 /* kick calling process */
3053 complete_request(mdsc, req);
3055 ceph_mdsc_put_request(req);
3062 * handle mds notification that our request has been forwarded.
3064 static void handle_forward(struct ceph_mds_client *mdsc,
3065 struct ceph_mds_session *session,
3066 struct ceph_msg *msg)
3068 struct ceph_mds_request *req;
3069 u64 tid = le64_to_cpu(msg->hdr.tid);
3073 void *p = msg->front.iov_base;
3074 void *end = p + msg->front.iov_len;
3076 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3077 next_mds = ceph_decode_32(&p);
3078 fwd_seq = ceph_decode_32(&p);
3080 mutex_lock(&mdsc->mutex);
3081 req = lookup_get_request(mdsc, tid);
3083 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
3084 goto out; /* dup reply? */
3087 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
3088 dout("forward tid %llu aborted, unregistering\n", tid);
3089 __unregister_request(mdsc, req);
3090 } else if (fwd_seq <= req->r_num_fwd) {
3091 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
3092 tid, next_mds, req->r_num_fwd, fwd_seq);
3094 /* resend. forward race not possible; mds would drop */
3095 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
3097 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
3098 req->r_attempts = 0;
3099 req->r_num_fwd = fwd_seq;
3100 req->r_resend_mds = next_mds;
3101 put_request_session(req);
3102 __do_request(mdsc, req);
3104 ceph_mdsc_put_request(req);
3106 mutex_unlock(&mdsc->mutex);
3110 pr_err("mdsc_handle_forward decode error err=%d\n", err);
3113 static int __decode_session_metadata(void **p, void *end,
3116 /* map<string,string> */
3119 ceph_decode_32_safe(p, end, n, bad);
3122 ceph_decode_32_safe(p, end, len, bad);
3123 ceph_decode_need(p, end, len, bad);
3124 err_str = !strncmp(*p, "error_string", len);
3126 ceph_decode_32_safe(p, end, len, bad);
3127 ceph_decode_need(p, end, len, bad);
3128 if (err_str && strnstr(*p, "blacklisted", len))
3129 *blacklisted = true;
3138 * handle a mds session control message
3140 static void handle_session(struct ceph_mds_session *session,
3141 struct ceph_msg *msg)
3143 struct ceph_mds_client *mdsc = session->s_mdsc;
3144 int mds = session->s_mds;
3145 int msg_version = le16_to_cpu(msg->hdr.version);
3146 void *p = msg->front.iov_base;
3147 void *end = p + msg->front.iov_len;
3148 struct ceph_mds_session_head *h;
3151 unsigned long features = 0;
3153 bool blacklisted = false;
3156 ceph_decode_need(&p, end, sizeof(*h), bad);
3160 op = le32_to_cpu(h->op);
3161 seq = le64_to_cpu(h->seq);
3163 if (msg_version >= 3) {
3165 /* version >= 2, metadata */
3166 if (__decode_session_metadata(&p, end, &blacklisted) < 0)
3168 /* version >= 3, feature bits */
3169 ceph_decode_32_safe(&p, end, len, bad);
3170 ceph_decode_need(&p, end, len, bad);
3171 memcpy(&features, p, min_t(size_t, len, sizeof(features)));
3175 mutex_lock(&mdsc->mutex);
3176 if (op == CEPH_SESSION_CLOSE) {
3177 ceph_get_mds_session(session);
3178 __unregister_session(mdsc, session);
3180 /* FIXME: this ttl calculation is generous */
3181 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
3182 mutex_unlock(&mdsc->mutex);
3184 mutex_lock(&session->s_mutex);
3186 dout("handle_session mds%d %s %p state %s seq %llu\n",
3187 mds, ceph_session_op_name(op), session,
3188 ceph_session_state_name(session->s_state), seq);
3190 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
3191 session->s_state = CEPH_MDS_SESSION_OPEN;
3192 pr_info("mds%d came back\n", session->s_mds);
3196 case CEPH_SESSION_OPEN:
3197 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3198 pr_info("mds%d reconnect success\n", session->s_mds);
3199 session->s_state = CEPH_MDS_SESSION_OPEN;
3200 session->s_features = features;
3201 renewed_caps(mdsc, session, 0);
3204 __close_session(mdsc, session);
3207 case CEPH_SESSION_RENEWCAPS:
3208 if (session->s_renew_seq == seq)
3209 renewed_caps(mdsc, session, 1);
3212 case CEPH_SESSION_CLOSE:
3213 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3214 pr_info("mds%d reconnect denied\n", session->s_mds);
3215 session->s_state = CEPH_MDS_SESSION_CLOSED;
3216 cleanup_session_requests(mdsc, session);
3217 remove_session_caps(session);
3218 wake = 2; /* for good measure */
3219 wake_up_all(&mdsc->session_close_wq);
3222 case CEPH_SESSION_STALE:
3223 pr_info("mds%d caps went stale, renewing\n",
3225 spin_lock(&session->s_gen_ttl_lock);
3226 session->s_cap_gen++;
3227 session->s_cap_ttl = jiffies - 1;
3228 spin_unlock(&session->s_gen_ttl_lock);
3229 send_renew_caps(mdsc, session);
3232 case CEPH_SESSION_RECALL_STATE:
3233 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
3236 case CEPH_SESSION_FLUSHMSG:
3237 send_flushmsg_ack(mdsc, session, seq);
3240 case CEPH_SESSION_FORCE_RO:
3241 dout("force_session_readonly %p\n", session);
3242 spin_lock(&session->s_cap_lock);
3243 session->s_readonly = true;
3244 spin_unlock(&session->s_cap_lock);
3245 wake_up_session_caps(session, FORCE_RO);
3248 case CEPH_SESSION_REJECT:
3249 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
3250 pr_info("mds%d rejected session\n", session->s_mds);
3251 session->s_state = CEPH_MDS_SESSION_REJECTED;
3252 cleanup_session_requests(mdsc, session);
3253 remove_session_caps(session);
3255 mdsc->fsc->blacklisted = true;
3256 wake = 2; /* for good measure */
3260 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
3264 mutex_unlock(&session->s_mutex);
3266 mutex_lock(&mdsc->mutex);
3267 __wake_requests(mdsc, &session->s_waiting);
3269 kick_requests(mdsc, mds);
3270 mutex_unlock(&mdsc->mutex);
3272 if (op == CEPH_SESSION_CLOSE)
3273 ceph_put_mds_session(session);
3277 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
3278 (int)msg->front.iov_len);
3284 * called under session->mutex.
3286 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
3287 struct ceph_mds_session *session)
3289 struct ceph_mds_request *req, *nreq;
3292 dout("replay_unsafe_requests mds%d\n", session->s_mds);
3294 mutex_lock(&mdsc->mutex);
3295 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item)
3296 __send_request(mdsc, session, req, true);
3299 * also re-send old requests when MDS enters reconnect stage. So that MDS
3300 * can process completed request in clientreplay stage.
3302 p = rb_first(&mdsc->request_tree);
3304 req = rb_entry(p, struct ceph_mds_request, r_node);
3306 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
3308 if (req->r_attempts == 0)
3309 continue; /* only old requests */
3310 if (req->r_session &&
3311 req->r_session->s_mds == session->s_mds)
3312 __send_request(mdsc, session, req, true);
3314 mutex_unlock(&mdsc->mutex);
3317 static int send_reconnect_partial(struct ceph_reconnect_state *recon_state)
3319 struct ceph_msg *reply;
3320 struct ceph_pagelist *_pagelist;
3325 if (!recon_state->allow_multi)
3328 /* can't handle message that contains both caps and realm */
3329 BUG_ON(!recon_state->nr_caps == !recon_state->nr_realms);
3331 /* pre-allocate new pagelist */
3332 _pagelist = ceph_pagelist_alloc(GFP_NOFS);
3336 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3340 /* placeholder for nr_caps */
3341 err = ceph_pagelist_encode_32(_pagelist, 0);
3345 if (recon_state->nr_caps) {
3346 /* currently encoding caps */
3347 err = ceph_pagelist_encode_32(recon_state->pagelist, 0);
3351 /* placeholder for nr_realms (currently encoding relams) */
3352 err = ceph_pagelist_encode_32(_pagelist, 0);
3357 err = ceph_pagelist_encode_8(recon_state->pagelist, 1);
3361 page = list_first_entry(&recon_state->pagelist->head, struct page, lru);
3362 addr = kmap_atomic(page);
3363 if (recon_state->nr_caps) {
3364 /* currently encoding caps */
3365 *addr = cpu_to_le32(recon_state->nr_caps);
3367 /* currently encoding relams */
3368 *(addr + 1) = cpu_to_le32(recon_state->nr_realms);
3370 kunmap_atomic(addr);
3372 reply->hdr.version = cpu_to_le16(5);
3373 reply->hdr.compat_version = cpu_to_le16(4);
3375 reply->hdr.data_len = cpu_to_le32(recon_state->pagelist->length);
3376 ceph_msg_data_add_pagelist(reply, recon_state->pagelist);
3378 ceph_con_send(&recon_state->session->s_con, reply);
3379 ceph_pagelist_release(recon_state->pagelist);
3381 recon_state->pagelist = _pagelist;
3382 recon_state->nr_caps = 0;
3383 recon_state->nr_realms = 0;
3384 recon_state->msg_version = 5;
3387 ceph_msg_put(reply);
3389 ceph_pagelist_release(_pagelist);
3394 * Encode information about a cap for a reconnect with the MDS.
3396 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
3400 struct ceph_mds_cap_reconnect v2;
3401 struct ceph_mds_cap_reconnect_v1 v1;
3403 struct ceph_inode_info *ci = cap->ci;
3404 struct ceph_reconnect_state *recon_state = arg;
3405 struct ceph_pagelist *pagelist = recon_state->pagelist;
3409 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
3410 inode, ceph_vinop(inode), cap, cap->cap_id,
3411 ceph_cap_string(cap->issued));
3413 spin_lock(&ci->i_ceph_lock);
3414 cap->seq = 0; /* reset cap seq */
3415 cap->issue_seq = 0; /* and issue_seq */
3416 cap->mseq = 0; /* and migrate_seq */
3417 cap->cap_gen = cap->session->s_cap_gen;
3419 if (recon_state->msg_version >= 2) {
3420 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
3421 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3422 rec.v2.issued = cpu_to_le32(cap->issued);
3423 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
3424 rec.v2.pathbase = 0;
3425 rec.v2.flock_len = (__force __le32)
3426 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
3428 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
3429 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3430 rec.v1.issued = cpu_to_le32(cap->issued);
3431 rec.v1.size = cpu_to_le64(inode->i_size);
3432 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
3433 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
3434 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
3435 rec.v1.pathbase = 0;
3438 if (list_empty(&ci->i_cap_snaps)) {
3439 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
3441 struct ceph_cap_snap *capsnap =
3442 list_first_entry(&ci->i_cap_snaps,
3443 struct ceph_cap_snap, ci_item);
3444 snap_follows = capsnap->follows;
3446 spin_unlock(&ci->i_ceph_lock);
3448 if (recon_state->msg_version >= 2) {
3449 int num_fcntl_locks, num_flock_locks;
3450 struct ceph_filelock *flocks = NULL;
3451 size_t struct_len, total_len = sizeof(u64);
3455 if (rec.v2.flock_len) {
3456 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
3458 num_fcntl_locks = 0;
3459 num_flock_locks = 0;
3461 if (num_fcntl_locks + num_flock_locks > 0) {
3462 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
3463 sizeof(struct ceph_filelock),
3469 err = ceph_encode_locks_to_buffer(inode, flocks,
3484 if (recon_state->msg_version >= 3) {
3485 /* version, compat_version and struct_len */
3486 total_len += 2 * sizeof(u8) + sizeof(u32);
3490 * number of encoded locks is stable, so copy to pagelist
3492 struct_len = 2 * sizeof(u32) +
3493 (num_fcntl_locks + num_flock_locks) *
3494 sizeof(struct ceph_filelock);
3495 rec.v2.flock_len = cpu_to_le32(struct_len);
3497 struct_len += sizeof(u32) + sizeof(rec.v2);
3500 struct_len += sizeof(u64); /* snap_follows */
3502 total_len += struct_len;
3504 if (pagelist->length + total_len > RECONNECT_MAX_SIZE) {
3505 err = send_reconnect_partial(recon_state);
3507 goto out_freeflocks;
3508 pagelist = recon_state->pagelist;
3511 err = ceph_pagelist_reserve(pagelist, total_len);
3513 goto out_freeflocks;
3515 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
3516 if (recon_state->msg_version >= 3) {
3517 ceph_pagelist_encode_8(pagelist, struct_v);
3518 ceph_pagelist_encode_8(pagelist, 1);
3519 ceph_pagelist_encode_32(pagelist, struct_len);
3521 ceph_pagelist_encode_string(pagelist, NULL, 0);
3522 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3523 ceph_locks_to_pagelist(flocks, pagelist,
3524 num_fcntl_locks, num_flock_locks);
3526 ceph_pagelist_encode_64(pagelist, snap_follows);
3533 struct dentry *dentry;
3535 dentry = d_find_alias(inode);
3537 path = ceph_mdsc_build_path(dentry,
3538 &pathlen, &pathbase, 0);
3541 err = PTR_ERR(path);
3544 rec.v1.pathbase = cpu_to_le64(pathbase);
3547 err = ceph_pagelist_reserve(pagelist,
3548 sizeof(u64) + sizeof(u32) +
3549 pathlen + sizeof(rec.v1));
3554 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
3555 ceph_pagelist_encode_string(pagelist, path, pathlen);
3556 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
3558 ceph_mdsc_free_path(path, pathlen);
3563 recon_state->nr_caps++;
3567 static int encode_snap_realms(struct ceph_mds_client *mdsc,
3568 struct ceph_reconnect_state *recon_state)
3571 struct ceph_pagelist *pagelist = recon_state->pagelist;
3574 if (recon_state->msg_version >= 4) {
3575 err = ceph_pagelist_encode_32(pagelist, mdsc->num_snap_realms);
3581 * snaprealms. we provide mds with the ino, seq (version), and
3582 * parent for all of our realms. If the mds has any newer info,
3585 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3586 struct ceph_snap_realm *realm =
3587 rb_entry(p, struct ceph_snap_realm, node);
3588 struct ceph_mds_snaprealm_reconnect sr_rec;
3590 if (recon_state->msg_version >= 4) {
3591 size_t need = sizeof(u8) * 2 + sizeof(u32) +
3594 if (pagelist->length + need > RECONNECT_MAX_SIZE) {
3595 err = send_reconnect_partial(recon_state);
3598 pagelist = recon_state->pagelist;
3601 err = ceph_pagelist_reserve(pagelist, need);
3605 ceph_pagelist_encode_8(pagelist, 1);
3606 ceph_pagelist_encode_8(pagelist, 1);
3607 ceph_pagelist_encode_32(pagelist, sizeof(sr_rec));
3610 dout(" adding snap realm %llx seq %lld parent %llx\n",
3611 realm->ino, realm->seq, realm->parent_ino);
3612 sr_rec.ino = cpu_to_le64(realm->ino);
3613 sr_rec.seq = cpu_to_le64(realm->seq);
3614 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3616 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3620 recon_state->nr_realms++;
3628 * If an MDS fails and recovers, clients need to reconnect in order to
3629 * reestablish shared state. This includes all caps issued through
3630 * this session _and_ the snap_realm hierarchy. Because it's not
3631 * clear which snap realms the mds cares about, we send everything we
3632 * know about.. that ensures we'll then get any new info the
3633 * recovering MDS might have.
3635 * This is a relatively heavyweight operation, but it's rare.
3637 * called with mdsc->mutex held.
3639 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3640 struct ceph_mds_session *session)
3642 struct ceph_msg *reply;
3643 int mds = session->s_mds;
3645 struct ceph_reconnect_state recon_state = {
3650 pr_info("mds%d reconnect start\n", mds);
3652 recon_state.pagelist = ceph_pagelist_alloc(GFP_NOFS);
3653 if (!recon_state.pagelist)
3654 goto fail_nopagelist;
3656 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3660 mutex_lock(&session->s_mutex);
3661 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3664 dout("session %p state %s\n", session,
3665 ceph_session_state_name(session->s_state));
3667 spin_lock(&session->s_gen_ttl_lock);
3668 session->s_cap_gen++;
3669 spin_unlock(&session->s_gen_ttl_lock);
3671 spin_lock(&session->s_cap_lock);
3672 /* don't know if session is readonly */
3673 session->s_readonly = 0;
3675 * notify __ceph_remove_cap() that we are composing cap reconnect.
3676 * If a cap get released before being added to the cap reconnect,
3677 * __ceph_remove_cap() should skip queuing cap release.
3679 session->s_cap_reconnect = 1;
3680 /* drop old cap expires; we're about to reestablish that state */
3681 detach_cap_releases(session, &dispose);
3682 spin_unlock(&session->s_cap_lock);
3683 dispose_cap_releases(mdsc, &dispose);
3685 /* trim unused caps to reduce MDS's cache rejoin time */
3686 if (mdsc->fsc->sb->s_root)
3687 shrink_dcache_parent(mdsc->fsc->sb->s_root);
3689 ceph_con_close(&session->s_con);
3690 ceph_con_open(&session->s_con,
3691 CEPH_ENTITY_TYPE_MDS, mds,
3692 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3694 /* replay unsafe requests */
3695 replay_unsafe_requests(mdsc, session);
3697 ceph_early_kick_flushing_caps(mdsc, session);
3699 down_read(&mdsc->snap_rwsem);
3701 /* placeholder for nr_caps */
3702 err = ceph_pagelist_encode_32(recon_state.pagelist, 0);
3706 if (test_bit(CEPHFS_FEATURE_MULTI_RECONNECT, &session->s_features)) {
3707 recon_state.msg_version = 3;
3708 recon_state.allow_multi = true;
3709 } else if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) {
3710 recon_state.msg_version = 3;
3712 recon_state.msg_version = 2;
3714 /* trsaverse this session's caps */
3715 err = ceph_iterate_session_caps(session, encode_caps_cb, &recon_state);
3717 spin_lock(&session->s_cap_lock);
3718 session->s_cap_reconnect = 0;
3719 spin_unlock(&session->s_cap_lock);
3724 /* check if all realms can be encoded into current message */
3725 if (mdsc->num_snap_realms) {
3727 recon_state.pagelist->length +
3728 mdsc->num_snap_realms *
3729 sizeof(struct ceph_mds_snaprealm_reconnect);
3730 if (recon_state.msg_version >= 4) {
3731 /* number of realms */
3732 total_len += sizeof(u32);
3733 /* version, compat_version and struct_len */
3734 total_len += mdsc->num_snap_realms *
3735 (2 * sizeof(u8) + sizeof(u32));
3737 if (total_len > RECONNECT_MAX_SIZE) {
3738 if (!recon_state.allow_multi) {
3742 if (recon_state.nr_caps) {
3743 err = send_reconnect_partial(&recon_state);
3747 recon_state.msg_version = 5;
3751 err = encode_snap_realms(mdsc, &recon_state);
3755 if (recon_state.msg_version >= 5) {
3756 err = ceph_pagelist_encode_8(recon_state.pagelist, 0);
3761 if (recon_state.nr_caps || recon_state.nr_realms) {
3763 list_first_entry(&recon_state.pagelist->head,
3765 __le32 *addr = kmap_atomic(page);
3766 if (recon_state.nr_caps) {
3767 WARN_ON(recon_state.nr_realms != mdsc->num_snap_realms);
3768 *addr = cpu_to_le32(recon_state.nr_caps);
3769 } else if (recon_state.msg_version >= 4) {
3770 *(addr + 1) = cpu_to_le32(recon_state.nr_realms);
3772 kunmap_atomic(addr);
3775 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
3776 if (recon_state.msg_version >= 4)
3777 reply->hdr.compat_version = cpu_to_le16(4);
3779 reply->hdr.data_len = cpu_to_le32(recon_state.pagelist->length);
3780 ceph_msg_data_add_pagelist(reply, recon_state.pagelist);
3782 ceph_con_send(&session->s_con, reply);
3784 mutex_unlock(&session->s_mutex);
3786 mutex_lock(&mdsc->mutex);
3787 __wake_requests(mdsc, &session->s_waiting);
3788 mutex_unlock(&mdsc->mutex);
3790 up_read(&mdsc->snap_rwsem);
3791 ceph_pagelist_release(recon_state.pagelist);
3795 ceph_msg_put(reply);
3796 up_read(&mdsc->snap_rwsem);
3797 mutex_unlock(&session->s_mutex);
3799 ceph_pagelist_release(recon_state.pagelist);
3801 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
3807 * compare old and new mdsmaps, kicking requests
3808 * and closing out old connections as necessary
3810 * called under mdsc->mutex.
3812 static void check_new_map(struct ceph_mds_client *mdsc,
3813 struct ceph_mdsmap *newmap,
3814 struct ceph_mdsmap *oldmap)
3817 int oldstate, newstate;
3818 struct ceph_mds_session *s;
3820 dout("check_new_map new %u old %u\n",
3821 newmap->m_epoch, oldmap->m_epoch);
3823 for (i = 0; i < oldmap->possible_max_rank && i < mdsc->max_sessions; i++) {
3824 if (!mdsc->sessions[i])
3826 s = mdsc->sessions[i];
3827 oldstate = ceph_mdsmap_get_state(oldmap, i);
3828 newstate = ceph_mdsmap_get_state(newmap, i);
3830 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
3831 i, ceph_mds_state_name(oldstate),
3832 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
3833 ceph_mds_state_name(newstate),
3834 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
3835 ceph_session_state_name(s->s_state));
3837 if (i >= newmap->possible_max_rank) {
3838 /* force close session for stopped mds */
3839 ceph_get_mds_session(s);
3840 __unregister_session(mdsc, s);
3841 __wake_requests(mdsc, &s->s_waiting);
3842 mutex_unlock(&mdsc->mutex);
3844 mutex_lock(&s->s_mutex);
3845 cleanup_session_requests(mdsc, s);
3846 remove_session_caps(s);
3847 mutex_unlock(&s->s_mutex);
3849 ceph_put_mds_session(s);
3851 mutex_lock(&mdsc->mutex);
3852 kick_requests(mdsc, i);
3856 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
3857 ceph_mdsmap_get_addr(newmap, i),
3858 sizeof(struct ceph_entity_addr))) {
3860 mutex_unlock(&mdsc->mutex);
3861 mutex_lock(&s->s_mutex);
3862 mutex_lock(&mdsc->mutex);
3863 ceph_con_close(&s->s_con);
3864 mutex_unlock(&s->s_mutex);
3865 s->s_state = CEPH_MDS_SESSION_RESTARTING;
3866 } else if (oldstate == newstate) {
3867 continue; /* nothing new with this mds */
3873 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
3874 newstate >= CEPH_MDS_STATE_RECONNECT) {
3875 mutex_unlock(&mdsc->mutex);
3876 send_mds_reconnect(mdsc, s);
3877 mutex_lock(&mdsc->mutex);
3881 * kick request on any mds that has gone active.
3883 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3884 newstate >= CEPH_MDS_STATE_ACTIVE) {
3885 if (oldstate != CEPH_MDS_STATE_CREATING &&
3886 oldstate != CEPH_MDS_STATE_STARTING)
3887 pr_info("mds%d recovery completed\n", s->s_mds);
3888 kick_requests(mdsc, i);
3889 ceph_kick_flushing_caps(mdsc, s);
3890 wake_up_session_caps(s, RECONNECT);
3894 for (i = 0; i < newmap->possible_max_rank && i < mdsc->max_sessions; i++) {
3895 s = mdsc->sessions[i];
3898 if (!ceph_mdsmap_is_laggy(newmap, i))
3900 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3901 s->s_state == CEPH_MDS_SESSION_HUNG ||
3902 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3903 dout(" connecting to export targets of laggy mds%d\n",
3905 __open_export_target_sessions(mdsc, s);
3917 * caller must hold session s_mutex, dentry->d_lock
3919 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3921 struct ceph_dentry_info *di = ceph_dentry(dentry);
3923 ceph_put_mds_session(di->lease_session);
3924 di->lease_session = NULL;
3927 static void handle_lease(struct ceph_mds_client *mdsc,
3928 struct ceph_mds_session *session,
3929 struct ceph_msg *msg)
3931 struct super_block *sb = mdsc->fsc->sb;
3932 struct inode *inode;
3933 struct dentry *parent, *dentry;
3934 struct ceph_dentry_info *di;
3935 int mds = session->s_mds;
3936 struct ceph_mds_lease *h = msg->front.iov_base;
3938 struct ceph_vino vino;
3942 dout("handle_lease from mds%d\n", mds);
3945 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3947 vino.ino = le64_to_cpu(h->ino);
3948 vino.snap = CEPH_NOSNAP;
3949 seq = le32_to_cpu(h->seq);
3950 dname.len = get_unaligned_le32(h + 1);
3951 if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
3953 dname.name = (void *)(h + 1) + sizeof(u32);
3956 inode = ceph_find_inode(sb, vino);
3957 dout("handle_lease %s, ino %llx %p %.*s\n",
3958 ceph_lease_op_name(h->action), vino.ino, inode,
3959 dname.len, dname.name);
3961 mutex_lock(&session->s_mutex);
3965 dout("handle_lease no inode %llx\n", vino.ino);
3970 parent = d_find_alias(inode);
3972 dout("no parent dentry on inode %p\n", inode);
3974 goto release; /* hrm... */
3976 dname.hash = full_name_hash(parent, dname.name, dname.len);
3977 dentry = d_lookup(parent, &dname);
3982 spin_lock(&dentry->d_lock);
3983 di = ceph_dentry(dentry);
3984 switch (h->action) {
3985 case CEPH_MDS_LEASE_REVOKE:
3986 if (di->lease_session == session) {
3987 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3988 h->seq = cpu_to_le32(di->lease_seq);
3989 __ceph_mdsc_drop_dentry_lease(dentry);
3994 case CEPH_MDS_LEASE_RENEW:
3995 if (di->lease_session == session &&
3996 di->lease_gen == session->s_cap_gen &&
3997 di->lease_renew_from &&
3998 di->lease_renew_after == 0) {
3999 unsigned long duration =
4000 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
4002 di->lease_seq = seq;
4003 di->time = di->lease_renew_from + duration;
4004 di->lease_renew_after = di->lease_renew_from +
4006 di->lease_renew_from = 0;
4010 spin_unlock(&dentry->d_lock);
4017 /* let's just reuse the same message */
4018 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
4020 ceph_con_send(&session->s_con, msg);
4023 mutex_unlock(&session->s_mutex);
4024 /* avoid calling iput_final() in mds dispatch threads */
4025 ceph_async_iput(inode);
4029 pr_err("corrupt lease message\n");
4033 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
4034 struct dentry *dentry, char action,
4037 struct ceph_msg *msg;
4038 struct ceph_mds_lease *lease;
4040 int len = sizeof(*lease) + sizeof(u32) + NAME_MAX;
4042 dout("lease_send_msg identry %p %s to mds%d\n",
4043 dentry, ceph_lease_op_name(action), session->s_mds);
4045 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
4048 lease = msg->front.iov_base;
4049 lease->action = action;
4050 lease->seq = cpu_to_le32(seq);
4052 spin_lock(&dentry->d_lock);
4053 dir = d_inode(dentry->d_parent);
4054 lease->ino = cpu_to_le64(ceph_ino(dir));
4055 lease->first = lease->last = cpu_to_le64(ceph_snap(dir));
4057 put_unaligned_le32(dentry->d_name.len, lease + 1);
4058 memcpy((void *)(lease + 1) + 4,
4059 dentry->d_name.name, dentry->d_name.len);
4060 spin_unlock(&dentry->d_lock);
4062 * if this is a preemptive lease RELEASE, no need to
4063 * flush request stream, since the actual request will
4066 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
4068 ceph_con_send(&session->s_con, msg);
4072 * lock unlock sessions, to wait ongoing session activities
4074 static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
4078 mutex_lock(&mdsc->mutex);
4079 for (i = 0; i < mdsc->max_sessions; i++) {
4080 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
4083 mutex_unlock(&mdsc->mutex);
4084 mutex_lock(&s->s_mutex);
4085 mutex_unlock(&s->s_mutex);
4086 ceph_put_mds_session(s);
4087 mutex_lock(&mdsc->mutex);
4089 mutex_unlock(&mdsc->mutex);
4092 static void maybe_recover_session(struct ceph_mds_client *mdsc)
4094 struct ceph_fs_client *fsc = mdsc->fsc;
4096 if (!ceph_test_mount_opt(fsc, CLEANRECOVER))
4099 if (READ_ONCE(fsc->mount_state) != CEPH_MOUNT_MOUNTED)
4102 if (!READ_ONCE(fsc->blacklisted))
4105 if (fsc->last_auto_reconnect &&
4106 time_before(jiffies, fsc->last_auto_reconnect + HZ * 60 * 30))
4109 pr_info("auto reconnect after blacklisted\n");
4110 fsc->last_auto_reconnect = jiffies;
4111 ceph_force_reconnect(fsc->sb);
4115 * delayed work -- periodically trim expired leases, renew caps with mds
4117 static void schedule_delayed(struct ceph_mds_client *mdsc)
4120 unsigned hz = round_jiffies_relative(HZ * delay);
4121 schedule_delayed_work(&mdsc->delayed_work, hz);
4124 static void delayed_work(struct work_struct *work)
4127 struct ceph_mds_client *mdsc =
4128 container_of(work, struct ceph_mds_client, delayed_work.work);
4132 dout("mdsc delayed_work\n");
4134 mutex_lock(&mdsc->mutex);
4135 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
4136 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
4137 mdsc->last_renew_caps);
4139 mdsc->last_renew_caps = jiffies;
4141 for (i = 0; i < mdsc->max_sessions; i++) {
4142 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
4145 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
4146 dout("resending session close request for mds%d\n",
4148 request_close_session(mdsc, s);
4149 ceph_put_mds_session(s);
4152 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
4153 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
4154 s->s_state = CEPH_MDS_SESSION_HUNG;
4155 pr_info("mds%d hung\n", s->s_mds);
4158 if (s->s_state == CEPH_MDS_SESSION_NEW ||
4159 s->s_state == CEPH_MDS_SESSION_RESTARTING ||
4160 s->s_state == CEPH_MDS_SESSION_REJECTED) {
4161 /* this mds is failed or recovering, just wait */
4162 ceph_put_mds_session(s);
4165 mutex_unlock(&mdsc->mutex);
4167 mutex_lock(&s->s_mutex);
4169 send_renew_caps(mdsc, s);
4171 ceph_con_keepalive(&s->s_con);
4172 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4173 s->s_state == CEPH_MDS_SESSION_HUNG)
4174 ceph_send_cap_releases(mdsc, s);
4175 mutex_unlock(&s->s_mutex);
4176 ceph_put_mds_session(s);
4178 mutex_lock(&mdsc->mutex);
4180 mutex_unlock(&mdsc->mutex);
4182 ceph_check_delayed_caps(mdsc);
4184 ceph_queue_cap_reclaim_work(mdsc);
4186 ceph_trim_snapid_map(mdsc);
4188 maybe_recover_session(mdsc);
4190 schedule_delayed(mdsc);
4193 int ceph_mdsc_init(struct ceph_fs_client *fsc)
4196 struct ceph_mds_client *mdsc;
4198 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
4202 mutex_init(&mdsc->mutex);
4203 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
4204 if (!mdsc->mdsmap) {
4210 init_completion(&mdsc->safe_umount_waiters);
4211 init_waitqueue_head(&mdsc->session_close_wq);
4212 INIT_LIST_HEAD(&mdsc->waiting_for_map);
4213 mdsc->sessions = NULL;
4214 atomic_set(&mdsc->num_sessions, 0);
4215 mdsc->max_sessions = 0;
4217 atomic64_set(&mdsc->quotarealms_count, 0);
4218 mdsc->quotarealms_inodes = RB_ROOT;
4219 mutex_init(&mdsc->quotarealms_inodes_mutex);
4220 mdsc->last_snap_seq = 0;
4221 init_rwsem(&mdsc->snap_rwsem);
4222 mdsc->snap_realms = RB_ROOT;
4223 INIT_LIST_HEAD(&mdsc->snap_empty);
4224 mdsc->num_snap_realms = 0;
4225 spin_lock_init(&mdsc->snap_empty_lock);
4227 mdsc->oldest_tid = 0;
4228 mdsc->request_tree = RB_ROOT;
4229 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
4230 mdsc->last_renew_caps = jiffies;
4231 INIT_LIST_HEAD(&mdsc->cap_delay_list);
4232 INIT_LIST_HEAD(&mdsc->cap_wait_list);
4233 spin_lock_init(&mdsc->cap_delay_lock);
4234 INIT_LIST_HEAD(&mdsc->snap_flush_list);
4235 spin_lock_init(&mdsc->snap_flush_lock);
4236 mdsc->last_cap_flush_tid = 1;
4237 INIT_LIST_HEAD(&mdsc->cap_flush_list);
4238 INIT_LIST_HEAD(&mdsc->cap_dirty);
4239 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
4240 mdsc->num_cap_flushing = 0;
4241 spin_lock_init(&mdsc->cap_dirty_lock);
4242 init_waitqueue_head(&mdsc->cap_flushing_wq);
4243 INIT_WORK(&mdsc->cap_reclaim_work, ceph_cap_reclaim_work);
4244 atomic_set(&mdsc->cap_reclaim_pending, 0);
4246 spin_lock_init(&mdsc->dentry_list_lock);
4247 INIT_LIST_HEAD(&mdsc->dentry_leases);
4248 INIT_LIST_HEAD(&mdsc->dentry_dir_leases);
4250 ceph_caps_init(mdsc);
4251 ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
4253 spin_lock_init(&mdsc->snapid_map_lock);
4254 mdsc->snapid_map_tree = RB_ROOT;
4255 INIT_LIST_HEAD(&mdsc->snapid_map_lru);
4257 init_rwsem(&mdsc->pool_perm_rwsem);
4258 mdsc->pool_perm_tree = RB_ROOT;
4260 strscpy(mdsc->nodename, utsname()->nodename,
4261 sizeof(mdsc->nodename));
4266 * Wait for safe replies on open mds requests. If we time out, drop
4267 * all requests from the tree to avoid dangling dentry refs.
4269 static void wait_requests(struct ceph_mds_client *mdsc)
4271 struct ceph_options *opts = mdsc->fsc->client->options;
4272 struct ceph_mds_request *req;
4274 mutex_lock(&mdsc->mutex);
4275 if (__get_oldest_req(mdsc)) {
4276 mutex_unlock(&mdsc->mutex);
4278 dout("wait_requests waiting for requests\n");
4279 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
4280 ceph_timeout_jiffies(opts->mount_timeout));
4282 /* tear down remaining requests */
4283 mutex_lock(&mdsc->mutex);
4284 while ((req = __get_oldest_req(mdsc))) {
4285 dout("wait_requests timed out on tid %llu\n",
4287 list_del_init(&req->r_wait);
4288 __unregister_request(mdsc, req);
4291 mutex_unlock(&mdsc->mutex);
4292 dout("wait_requests done\n");
4296 * called before mount is ro, and before dentries are torn down.
4297 * (hmm, does this still race with new lookups?)
4299 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
4301 dout("pre_umount\n");
4304 lock_unlock_sessions(mdsc);
4305 ceph_flush_dirty_caps(mdsc);
4306 wait_requests(mdsc);
4309 * wait for reply handlers to drop their request refs and
4310 * their inode/dcache refs
4314 ceph_cleanup_quotarealms_inodes(mdsc);
4318 * wait for all write mds requests to flush.
4320 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
4322 struct ceph_mds_request *req = NULL, *nextreq;
4325 mutex_lock(&mdsc->mutex);
4326 dout("wait_unsafe_requests want %lld\n", want_tid);
4328 req = __get_oldest_req(mdsc);
4329 while (req && req->r_tid <= want_tid) {
4330 /* find next request */
4331 n = rb_next(&req->r_node);
4333 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
4336 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
4337 (req->r_op & CEPH_MDS_OP_WRITE)) {
4339 ceph_mdsc_get_request(req);
4341 ceph_mdsc_get_request(nextreq);
4342 mutex_unlock(&mdsc->mutex);
4343 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
4344 req->r_tid, want_tid);
4345 wait_for_completion(&req->r_safe_completion);
4346 mutex_lock(&mdsc->mutex);
4347 ceph_mdsc_put_request(req);
4349 break; /* next dne before, so we're done! */
4350 if (RB_EMPTY_NODE(&nextreq->r_node)) {
4351 /* next request was removed from tree */
4352 ceph_mdsc_put_request(nextreq);
4355 ceph_mdsc_put_request(nextreq); /* won't go away */
4359 mutex_unlock(&mdsc->mutex);
4360 dout("wait_unsafe_requests done\n");
4363 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
4365 u64 want_tid, want_flush;
4367 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
4371 mutex_lock(&mdsc->mutex);
4372 want_tid = mdsc->last_tid;
4373 mutex_unlock(&mdsc->mutex);
4375 ceph_flush_dirty_caps(mdsc);
4376 spin_lock(&mdsc->cap_dirty_lock);
4377 want_flush = mdsc->last_cap_flush_tid;
4378 if (!list_empty(&mdsc->cap_flush_list)) {
4379 struct ceph_cap_flush *cf =
4380 list_last_entry(&mdsc->cap_flush_list,
4381 struct ceph_cap_flush, g_list);
4384 spin_unlock(&mdsc->cap_dirty_lock);
4386 dout("sync want tid %lld flush_seq %lld\n",
4387 want_tid, want_flush);
4389 wait_unsafe_requests(mdsc, want_tid);
4390 wait_caps_flush(mdsc, want_flush);
4394 * true if all sessions are closed, or we force unmount
4396 static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
4398 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
4400 return atomic_read(&mdsc->num_sessions) <= skipped;
4404 * called after sb is ro.
4406 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
4408 struct ceph_options *opts = mdsc->fsc->client->options;
4409 struct ceph_mds_session *session;
4413 dout("close_sessions\n");
4415 /* close sessions */
4416 mutex_lock(&mdsc->mutex);
4417 for (i = 0; i < mdsc->max_sessions; i++) {
4418 session = __ceph_lookup_mds_session(mdsc, i);
4421 mutex_unlock(&mdsc->mutex);
4422 mutex_lock(&session->s_mutex);
4423 if (__close_session(mdsc, session) <= 0)
4425 mutex_unlock(&session->s_mutex);
4426 ceph_put_mds_session(session);
4427 mutex_lock(&mdsc->mutex);
4429 mutex_unlock(&mdsc->mutex);
4431 dout("waiting for sessions to close\n");
4432 wait_event_timeout(mdsc->session_close_wq,
4433 done_closing_sessions(mdsc, skipped),
4434 ceph_timeout_jiffies(opts->mount_timeout));
4436 /* tear down remaining sessions */
4437 mutex_lock(&mdsc->mutex);
4438 for (i = 0; i < mdsc->max_sessions; i++) {
4439 if (mdsc->sessions[i]) {
4440 session = ceph_get_mds_session(mdsc->sessions[i]);
4441 __unregister_session(mdsc, session);
4442 mutex_unlock(&mdsc->mutex);
4443 mutex_lock(&session->s_mutex);
4444 remove_session_caps(session);
4445 mutex_unlock(&session->s_mutex);
4446 ceph_put_mds_session(session);
4447 mutex_lock(&mdsc->mutex);
4450 WARN_ON(!list_empty(&mdsc->cap_delay_list));
4451 mutex_unlock(&mdsc->mutex);
4453 ceph_cleanup_snapid_map(mdsc);
4454 ceph_cleanup_empty_realms(mdsc);
4456 cancel_work_sync(&mdsc->cap_reclaim_work);
4457 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4462 void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
4464 struct ceph_mds_session *session;
4467 dout("force umount\n");
4469 mutex_lock(&mdsc->mutex);
4470 for (mds = 0; mds < mdsc->max_sessions; mds++) {
4471 session = __ceph_lookup_mds_session(mdsc, mds);
4475 if (session->s_state == CEPH_MDS_SESSION_REJECTED)
4476 __unregister_session(mdsc, session);
4477 __wake_requests(mdsc, &session->s_waiting);
4478 mutex_unlock(&mdsc->mutex);
4480 mutex_lock(&session->s_mutex);
4481 __close_session(mdsc, session);
4482 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
4483 cleanup_session_requests(mdsc, session);
4484 remove_session_caps(session);
4486 mutex_unlock(&session->s_mutex);
4487 ceph_put_mds_session(session);
4489 mutex_lock(&mdsc->mutex);
4490 kick_requests(mdsc, mds);
4492 __wake_requests(mdsc, &mdsc->waiting_for_map);
4493 mutex_unlock(&mdsc->mutex);
4496 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
4499 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4501 ceph_mdsmap_destroy(mdsc->mdsmap);
4502 kfree(mdsc->sessions);
4503 ceph_caps_finalize(mdsc);
4504 ceph_pool_perm_destroy(mdsc);
4507 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
4509 struct ceph_mds_client *mdsc = fsc->mdsc;
4510 dout("mdsc_destroy %p\n", mdsc);
4515 /* flush out any connection work with references to us */
4518 ceph_mdsc_stop(mdsc);
4522 dout("mdsc_destroy %p done\n", mdsc);
4525 void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
4527 struct ceph_fs_client *fsc = mdsc->fsc;
4528 const char *mds_namespace = fsc->mount_options->mds_namespace;
4529 void *p = msg->front.iov_base;
4530 void *end = p + msg->front.iov_len;
4534 u32 mount_fscid = (u32)-1;
4535 u8 struct_v, struct_cv;
4538 ceph_decode_need(&p, end, sizeof(u32), bad);
4539 epoch = ceph_decode_32(&p);
4541 dout("handle_fsmap epoch %u\n", epoch);
4543 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4544 struct_v = ceph_decode_8(&p);
4545 struct_cv = ceph_decode_8(&p);
4546 map_len = ceph_decode_32(&p);
4548 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
4549 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
4551 num_fs = ceph_decode_32(&p);
4552 while (num_fs-- > 0) {
4553 void *info_p, *info_end;
4558 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4559 info_v = ceph_decode_8(&p);
4560 info_cv = ceph_decode_8(&p);
4561 info_len = ceph_decode_32(&p);
4562 ceph_decode_need(&p, end, info_len, bad);
4564 info_end = p + info_len;
4567 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
4568 fscid = ceph_decode_32(&info_p);
4569 namelen = ceph_decode_32(&info_p);
4570 ceph_decode_need(&info_p, info_end, namelen, bad);
4572 if (mds_namespace &&
4573 strlen(mds_namespace) == namelen &&
4574 !strncmp(mds_namespace, (char *)info_p, namelen)) {
4575 mount_fscid = fscid;
4580 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
4581 if (mount_fscid != (u32)-1) {
4582 fsc->client->monc.fs_cluster_id = mount_fscid;
4583 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
4585 ceph_monc_renew_subs(&fsc->client->monc);
4593 pr_err("error decoding fsmap\n");
4595 mutex_lock(&mdsc->mutex);
4596 mdsc->mdsmap_err = err;
4597 __wake_requests(mdsc, &mdsc->waiting_for_map);
4598 mutex_unlock(&mdsc->mutex);
4602 * handle mds map update.
4604 void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
4608 void *p = msg->front.iov_base;
4609 void *end = p + msg->front.iov_len;
4610 struct ceph_mdsmap *newmap, *oldmap;
4611 struct ceph_fsid fsid;
4614 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
4615 ceph_decode_copy(&p, &fsid, sizeof(fsid));
4616 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
4618 epoch = ceph_decode_32(&p);
4619 maplen = ceph_decode_32(&p);
4620 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
4622 /* do we need it? */
4623 mutex_lock(&mdsc->mutex);
4624 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
4625 dout("handle_map epoch %u <= our %u\n",
4626 epoch, mdsc->mdsmap->m_epoch);
4627 mutex_unlock(&mdsc->mutex);
4631 newmap = ceph_mdsmap_decode(&p, end);
4632 if (IS_ERR(newmap)) {
4633 err = PTR_ERR(newmap);
4637 /* swap into place */
4639 oldmap = mdsc->mdsmap;
4640 mdsc->mdsmap = newmap;
4641 check_new_map(mdsc, newmap, oldmap);
4642 ceph_mdsmap_destroy(oldmap);
4644 mdsc->mdsmap = newmap; /* first mds map */
4646 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
4649 __wake_requests(mdsc, &mdsc->waiting_for_map);
4650 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
4651 mdsc->mdsmap->m_epoch);
4653 mutex_unlock(&mdsc->mutex);
4654 schedule_delayed(mdsc);
4658 mutex_unlock(&mdsc->mutex);
4660 pr_err("error decoding mdsmap %d\n", err);
4664 static struct ceph_connection *con_get(struct ceph_connection *con)
4666 struct ceph_mds_session *s = con->private;
4668 if (ceph_get_mds_session(s))
4673 static void con_put(struct ceph_connection *con)
4675 struct ceph_mds_session *s = con->private;
4677 ceph_put_mds_session(s);
4681 * if the client is unresponsive for long enough, the mds will kill
4682 * the session entirely.
4684 static void peer_reset(struct ceph_connection *con)
4686 struct ceph_mds_session *s = con->private;
4687 struct ceph_mds_client *mdsc = s->s_mdsc;
4689 pr_warn("mds%d closed our session\n", s->s_mds);
4690 send_mds_reconnect(mdsc, s);
4693 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4695 struct ceph_mds_session *s = con->private;
4696 struct ceph_mds_client *mdsc = s->s_mdsc;
4697 int type = le16_to_cpu(msg->hdr.type);
4699 mutex_lock(&mdsc->mutex);
4700 if (__verify_registered_session(mdsc, s) < 0) {
4701 mutex_unlock(&mdsc->mutex);
4704 mutex_unlock(&mdsc->mutex);
4707 case CEPH_MSG_MDS_MAP:
4708 ceph_mdsc_handle_mdsmap(mdsc, msg);
4710 case CEPH_MSG_FS_MAP_USER:
4711 ceph_mdsc_handle_fsmap(mdsc, msg);
4713 case CEPH_MSG_CLIENT_SESSION:
4714 handle_session(s, msg);
4716 case CEPH_MSG_CLIENT_REPLY:
4717 handle_reply(s, msg);
4719 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
4720 handle_forward(mdsc, s, msg);
4722 case CEPH_MSG_CLIENT_CAPS:
4723 ceph_handle_caps(s, msg);
4725 case CEPH_MSG_CLIENT_SNAP:
4726 ceph_handle_snap(mdsc, s, msg);
4728 case CEPH_MSG_CLIENT_LEASE:
4729 handle_lease(mdsc, s, msg);
4731 case CEPH_MSG_CLIENT_QUOTA:
4732 ceph_handle_quota(mdsc, s, msg);
4736 pr_err("received unknown message type %d %s\n", type,
4737 ceph_msg_type_name(type));
4748 * Note: returned pointer is the address of a structure that's
4749 * managed separately. Caller must *not* attempt to free it.
4751 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
4752 int *proto, int force_new)
4754 struct ceph_mds_session *s = con->private;
4755 struct ceph_mds_client *mdsc = s->s_mdsc;
4756 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4757 struct ceph_auth_handshake *auth = &s->s_auth;
4759 if (force_new && auth->authorizer) {
4760 ceph_auth_destroy_authorizer(auth->authorizer);
4761 auth->authorizer = NULL;
4763 if (!auth->authorizer) {
4764 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4767 return ERR_PTR(ret);
4769 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4772 return ERR_PTR(ret);
4774 *proto = ac->protocol;
4779 static int add_authorizer_challenge(struct ceph_connection *con,
4780 void *challenge_buf, int challenge_buf_len)
4782 struct ceph_mds_session *s = con->private;
4783 struct ceph_mds_client *mdsc = s->s_mdsc;
4784 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4786 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
4787 challenge_buf, challenge_buf_len);
4790 static int verify_authorizer_reply(struct ceph_connection *con)
4792 struct ceph_mds_session *s = con->private;
4793 struct ceph_mds_client *mdsc = s->s_mdsc;
4794 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4796 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
4799 static int invalidate_authorizer(struct ceph_connection *con)
4801 struct ceph_mds_session *s = con->private;
4802 struct ceph_mds_client *mdsc = s->s_mdsc;
4803 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4805 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
4807 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
4810 static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
4811 struct ceph_msg_header *hdr, int *skip)
4813 struct ceph_msg *msg;
4814 int type = (int) le16_to_cpu(hdr->type);
4815 int front_len = (int) le32_to_cpu(hdr->front_len);
4821 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
4823 pr_err("unable to allocate msg type %d len %d\n",
4831 static int mds_sign_message(struct ceph_msg *msg)
4833 struct ceph_mds_session *s = msg->con->private;
4834 struct ceph_auth_handshake *auth = &s->s_auth;
4836 return ceph_auth_sign_message(auth, msg);
4839 static int mds_check_message_signature(struct ceph_msg *msg)
4841 struct ceph_mds_session *s = msg->con->private;
4842 struct ceph_auth_handshake *auth = &s->s_auth;
4844 return ceph_auth_check_message_signature(auth, msg);
4847 static const struct ceph_connection_operations mds_con_ops = {
4850 .dispatch = dispatch,
4851 .get_authorizer = get_authorizer,
4852 .add_authorizer_challenge = add_authorizer_challenge,
4853 .verify_authorizer_reply = verify_authorizer_reply,
4854 .invalidate_authorizer = invalidate_authorizer,
4855 .peer_reset = peer_reset,
4856 .alloc_msg = mds_alloc_msg,
4857 .sign_message = mds_sign_message,
4858 .check_message_signature = mds_check_message_signature,