1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
5 #include <linux/wait.h>
6 #include <linux/slab.h>
8 #include <linux/sched.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.h>
11 #include <linux/ratelimit.h>
12 #include <linux/bits.h>
13 #include <linux/ktime.h>
16 #include "mds_client.h"
18 #include <linux/ceph/ceph_features.h>
19 #include <linux/ceph/messenger.h>
20 #include <linux/ceph/decode.h>
21 #include <linux/ceph/pagelist.h>
22 #include <linux/ceph/auth.h>
23 #include <linux/ceph/debugfs.h>
25 #define RECONNECT_MAX_SIZE (INT_MAX - PAGE_SIZE)
28 * A cluster of MDS (metadata server) daemons is responsible for
29 * managing the file system namespace (the directory hierarchy and
30 * inodes) and for coordinating shared access to storage. Metadata is
31 * partitioning hierarchically across a number of servers, and that
32 * partition varies over time as the cluster adjusts the distribution
33 * in order to balance load.
35 * The MDS client is primarily responsible to managing synchronous
36 * metadata requests for operations like open, unlink, and so forth.
37 * If there is a MDS failure, we find out about it when we (possibly
38 * request and) receive a new MDS map, and can resubmit affected
41 * For the most part, though, we take advantage of a lossless
42 * communications channel to the MDS, and do not need to worry about
43 * timing out or resubmitting requests.
45 * We maintain a stateful "session" with each MDS we interact with.
46 * Within each session, we sent periodic heartbeat messages to ensure
47 * any capabilities or leases we have been issues remain valid. If
48 * the session times out and goes stale, our leases and capabilities
49 * are no longer valid.
52 struct ceph_reconnect_state {
53 struct ceph_mds_session *session;
54 int nr_caps, nr_realms;
55 struct ceph_pagelist *pagelist;
60 static void __wake_requests(struct ceph_mds_client *mdsc,
61 struct list_head *head);
62 static void ceph_cap_release_work(struct work_struct *work);
63 static void ceph_cap_reclaim_work(struct work_struct *work);
65 static const struct ceph_connection_operations mds_con_ops;
72 static int parse_reply_info_quota(void **p, void *end,
73 struct ceph_mds_reply_info_in *info)
75 u8 struct_v, struct_compat;
78 ceph_decode_8_safe(p, end, struct_v, bad);
79 ceph_decode_8_safe(p, end, struct_compat, bad);
80 /* struct_v is expected to be >= 1. we only
81 * understand encoding with struct_compat == 1. */
82 if (!struct_v || struct_compat != 1)
84 ceph_decode_32_safe(p, end, struct_len, bad);
85 ceph_decode_need(p, end, struct_len, bad);
86 end = *p + struct_len;
87 ceph_decode_64_safe(p, end, info->max_bytes, bad);
88 ceph_decode_64_safe(p, end, info->max_files, bad);
96 * parse individual inode info
98 static int parse_reply_info_in(void **p, void *end,
99 struct ceph_mds_reply_info_in *info,
105 if (features == (u64)-1) {
108 ceph_decode_8_safe(p, end, struct_v, bad);
109 ceph_decode_8_safe(p, end, struct_compat, bad);
110 /* struct_v is expected to be >= 1. we only understand
111 * encoding with struct_compat == 1. */
112 if (!struct_v || struct_compat != 1)
114 ceph_decode_32_safe(p, end, struct_len, bad);
115 ceph_decode_need(p, end, struct_len, bad);
116 end = *p + struct_len;
119 ceph_decode_need(p, end, sizeof(struct ceph_mds_reply_inode), bad);
121 *p += sizeof(struct ceph_mds_reply_inode) +
122 sizeof(*info->in->fragtree.splits) *
123 le32_to_cpu(info->in->fragtree.nsplits);
125 ceph_decode_32_safe(p, end, info->symlink_len, bad);
126 ceph_decode_need(p, end, info->symlink_len, bad);
128 *p += info->symlink_len;
130 ceph_decode_copy_safe(p, end, &info->dir_layout,
131 sizeof(info->dir_layout), bad);
132 ceph_decode_32_safe(p, end, info->xattr_len, bad);
133 ceph_decode_need(p, end, info->xattr_len, bad);
134 info->xattr_data = *p;
135 *p += info->xattr_len;
137 if (features == (u64)-1) {
139 ceph_decode_64_safe(p, end, info->inline_version, bad);
140 ceph_decode_32_safe(p, end, info->inline_len, bad);
141 ceph_decode_need(p, end, info->inline_len, bad);
142 info->inline_data = *p;
143 *p += info->inline_len;
145 err = parse_reply_info_quota(p, end, info);
149 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
150 if (info->pool_ns_len > 0) {
151 ceph_decode_need(p, end, info->pool_ns_len, bad);
152 info->pool_ns_data = *p;
153 *p += info->pool_ns_len;
157 ceph_decode_need(p, end, sizeof(info->btime), bad);
158 ceph_decode_copy(p, &info->btime, sizeof(info->btime));
160 /* change attribute */
161 ceph_decode_64_safe(p, end, info->change_attr, bad);
165 ceph_decode_32_safe(p, end, info->dir_pin, bad);
167 info->dir_pin = -ENODATA;
170 /* snapshot birth time, remains zero for v<=2 */
172 ceph_decode_need(p, end, sizeof(info->snap_btime), bad);
173 ceph_decode_copy(p, &info->snap_btime,
174 sizeof(info->snap_btime));
176 memset(&info->snap_btime, 0, sizeof(info->snap_btime));
179 /* snapshot count, remains zero for v<=3 */
181 ceph_decode_64_safe(p, end, info->rsnaps, bad);
188 if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
189 ceph_decode_64_safe(p, end, info->inline_version, bad);
190 ceph_decode_32_safe(p, end, info->inline_len, bad);
191 ceph_decode_need(p, end, info->inline_len, bad);
192 info->inline_data = *p;
193 *p += info->inline_len;
195 info->inline_version = CEPH_INLINE_NONE;
197 if (features & CEPH_FEATURE_MDS_QUOTA) {
198 err = parse_reply_info_quota(p, end, info);
206 info->pool_ns_len = 0;
207 info->pool_ns_data = NULL;
208 if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) {
209 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
210 if (info->pool_ns_len > 0) {
211 ceph_decode_need(p, end, info->pool_ns_len, bad);
212 info->pool_ns_data = *p;
213 *p += info->pool_ns_len;
217 if (features & CEPH_FEATURE_FS_BTIME) {
218 ceph_decode_need(p, end, sizeof(info->btime), bad);
219 ceph_decode_copy(p, &info->btime, sizeof(info->btime));
220 ceph_decode_64_safe(p, end, info->change_attr, bad);
223 info->dir_pin = -ENODATA;
224 /* info->snap_btime and info->rsnaps remain zero */
233 static int parse_reply_info_dir(void **p, void *end,
234 struct ceph_mds_reply_dirfrag **dirfrag,
237 if (features == (u64)-1) {
238 u8 struct_v, struct_compat;
240 ceph_decode_8_safe(p, end, struct_v, bad);
241 ceph_decode_8_safe(p, end, struct_compat, bad);
242 /* struct_v is expected to be >= 1. we only understand
243 * encoding whose struct_compat == 1. */
244 if (!struct_v || struct_compat != 1)
246 ceph_decode_32_safe(p, end, struct_len, bad);
247 ceph_decode_need(p, end, struct_len, bad);
248 end = *p + struct_len;
251 ceph_decode_need(p, end, sizeof(**dirfrag), bad);
253 *p += sizeof(**dirfrag) + sizeof(u32) * le32_to_cpu((*dirfrag)->ndist);
254 if (unlikely(*p > end))
256 if (features == (u64)-1)
263 static int parse_reply_info_lease(void **p, void *end,
264 struct ceph_mds_reply_lease **lease,
267 if (features == (u64)-1) {
268 u8 struct_v, struct_compat;
270 ceph_decode_8_safe(p, end, struct_v, bad);
271 ceph_decode_8_safe(p, end, struct_compat, bad);
272 /* struct_v is expected to be >= 1. we only understand
273 * encoding whose struct_compat == 1. */
274 if (!struct_v || struct_compat != 1)
276 ceph_decode_32_safe(p, end, struct_len, bad);
277 ceph_decode_need(p, end, struct_len, bad);
278 end = *p + struct_len;
281 ceph_decode_need(p, end, sizeof(**lease), bad);
283 *p += sizeof(**lease);
284 if (features == (u64)-1)
292 * parse a normal reply, which may contain a (dir+)dentry and/or a
295 static int parse_reply_info_trace(void **p, void *end,
296 struct ceph_mds_reply_info_parsed *info,
301 if (info->head->is_dentry) {
302 err = parse_reply_info_in(p, end, &info->diri, features);
306 err = parse_reply_info_dir(p, end, &info->dirfrag, features);
310 ceph_decode_32_safe(p, end, info->dname_len, bad);
311 ceph_decode_need(p, end, info->dname_len, bad);
313 *p += info->dname_len;
315 err = parse_reply_info_lease(p, end, &info->dlease, features);
320 if (info->head->is_target) {
321 err = parse_reply_info_in(p, end, &info->targeti, features);
326 if (unlikely(*p != end))
333 pr_err("problem parsing mds trace %d\n", err);
338 * parse readdir results
340 static int parse_reply_info_readdir(void **p, void *end,
341 struct ceph_mds_reply_info_parsed *info,
347 err = parse_reply_info_dir(p, end, &info->dir_dir, features);
351 ceph_decode_need(p, end, sizeof(num) + 2, bad);
352 num = ceph_decode_32(p);
354 u16 flags = ceph_decode_16(p);
355 info->dir_end = !!(flags & CEPH_READDIR_FRAG_END);
356 info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE);
357 info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER);
358 info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH);
363 BUG_ON(!info->dir_entries);
364 if ((unsigned long)(info->dir_entries + num) >
365 (unsigned long)info->dir_entries + info->dir_buf_size) {
366 pr_err("dir contents are larger than expected\n");
373 struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i;
375 ceph_decode_32_safe(p, end, rde->name_len, bad);
376 ceph_decode_need(p, end, rde->name_len, bad);
379 dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name);
382 err = parse_reply_info_lease(p, end, &rde->lease, features);
386 err = parse_reply_info_in(p, end, &rde->inode, features);
389 /* ceph_readdir_prepopulate() will update it */
396 /* Skip over any unrecognized fields */
403 pr_err("problem parsing dir contents %d\n", err);
408 * parse fcntl F_GETLK results
410 static int parse_reply_info_filelock(void **p, void *end,
411 struct ceph_mds_reply_info_parsed *info,
414 if (*p + sizeof(*info->filelock_reply) > end)
417 info->filelock_reply = *p;
419 /* Skip over any unrecognized fields */
427 #if BITS_PER_LONG == 64
429 #define DELEGATED_INO_AVAILABLE xa_mk_value(1)
431 static int ceph_parse_deleg_inos(void **p, void *end,
432 struct ceph_mds_session *s)
436 ceph_decode_32_safe(p, end, sets, bad);
437 dout("got %u sets of delegated inodes\n", sets);
441 ceph_decode_64_safe(p, end, start, bad);
442 ceph_decode_64_safe(p, end, len, bad);
444 /* Don't accept a delegation of system inodes */
445 if (start < CEPH_INO_SYSTEM_BASE) {
446 pr_warn_ratelimited("ceph: ignoring reserved inode range delegation (start=0x%llx len=0x%llx)\n",
451 int err = xa_insert(&s->s_delegated_inos, ino = start++,
452 DELEGATED_INO_AVAILABLE,
455 dout("added delegated inode 0x%llx\n",
457 } else if (err == -EBUSY) {
458 pr_warn("ceph: MDS delegated inode 0x%llx more than once.\n",
470 u64 ceph_get_deleg_ino(struct ceph_mds_session *s)
475 xa_for_each(&s->s_delegated_inos, ino, val) {
476 val = xa_erase(&s->s_delegated_inos, ino);
477 if (val == DELEGATED_INO_AVAILABLE)
483 int ceph_restore_deleg_ino(struct ceph_mds_session *s, u64 ino)
485 return xa_insert(&s->s_delegated_inos, ino, DELEGATED_INO_AVAILABLE,
488 #else /* BITS_PER_LONG == 64 */
490 * FIXME: xarrays can't handle 64-bit indexes on a 32-bit arch. For now, just
491 * ignore delegated_inos on 32 bit arch. Maybe eventually add xarrays for top
494 static int ceph_parse_deleg_inos(void **p, void *end,
495 struct ceph_mds_session *s)
499 ceph_decode_32_safe(p, end, sets, bad);
501 ceph_decode_skip_n(p, end, sets * 2 * sizeof(__le64), bad);
507 u64 ceph_get_deleg_ino(struct ceph_mds_session *s)
512 int ceph_restore_deleg_ino(struct ceph_mds_session *s, u64 ino)
516 #endif /* BITS_PER_LONG == 64 */
519 * parse create results
521 static int parse_reply_info_create(void **p, void *end,
522 struct ceph_mds_reply_info_parsed *info,
523 u64 features, struct ceph_mds_session *s)
527 if (features == (u64)-1 ||
528 (features & CEPH_FEATURE_REPLY_CREATE_INODE)) {
530 /* Malformed reply? */
531 info->has_create_ino = false;
532 } else if (test_bit(CEPHFS_FEATURE_DELEG_INO, &s->s_features)) {
533 info->has_create_ino = true;
534 /* struct_v, struct_compat, and len */
535 ceph_decode_skip_n(p, end, 2 + sizeof(u32), bad);
536 ceph_decode_64_safe(p, end, info->ino, bad);
537 ret = ceph_parse_deleg_inos(p, end, s);
542 ceph_decode_64_safe(p, end, info->ino, bad);
543 info->has_create_ino = true;
550 /* Skip over any unrecognized fields */
558 * parse extra results
560 static int parse_reply_info_extra(void **p, void *end,
561 struct ceph_mds_reply_info_parsed *info,
562 u64 features, struct ceph_mds_session *s)
564 u32 op = le32_to_cpu(info->head->op);
566 if (op == CEPH_MDS_OP_GETFILELOCK)
567 return parse_reply_info_filelock(p, end, info, features);
568 else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
569 return parse_reply_info_readdir(p, end, info, features);
570 else if (op == CEPH_MDS_OP_CREATE)
571 return parse_reply_info_create(p, end, info, features, s);
577 * parse entire mds reply
579 static int parse_reply_info(struct ceph_mds_session *s, struct ceph_msg *msg,
580 struct ceph_mds_reply_info_parsed *info,
587 info->head = msg->front.iov_base;
588 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
589 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
592 ceph_decode_32_safe(&p, end, len, bad);
594 ceph_decode_need(&p, end, len, bad);
595 err = parse_reply_info_trace(&p, p+len, info, features);
601 ceph_decode_32_safe(&p, end, len, bad);
603 ceph_decode_need(&p, end, len, bad);
604 err = parse_reply_info_extra(&p, p+len, info, features, s);
610 ceph_decode_32_safe(&p, end, len, bad);
611 info->snapblob_len = len;
622 pr_err("mds parse_reply err %d\n", err);
626 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
628 if (!info->dir_entries)
630 free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
637 const char *ceph_session_state_name(int s)
640 case CEPH_MDS_SESSION_NEW: return "new";
641 case CEPH_MDS_SESSION_OPENING: return "opening";
642 case CEPH_MDS_SESSION_OPEN: return "open";
643 case CEPH_MDS_SESSION_HUNG: return "hung";
644 case CEPH_MDS_SESSION_CLOSING: return "closing";
645 case CEPH_MDS_SESSION_CLOSED: return "closed";
646 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
647 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
648 case CEPH_MDS_SESSION_REJECTED: return "rejected";
649 default: return "???";
653 struct ceph_mds_session *ceph_get_mds_session(struct ceph_mds_session *s)
655 if (refcount_inc_not_zero(&s->s_ref)) {
656 dout("mdsc get_session %p %d -> %d\n", s,
657 refcount_read(&s->s_ref)-1, refcount_read(&s->s_ref));
660 dout("mdsc get_session %p 0 -- FAIL\n", s);
665 void ceph_put_mds_session(struct ceph_mds_session *s)
667 dout("mdsc put_session %p %d -> %d\n", s,
668 refcount_read(&s->s_ref), refcount_read(&s->s_ref)-1);
669 if (refcount_dec_and_test(&s->s_ref)) {
670 if (s->s_auth.authorizer)
671 ceph_auth_destroy_authorizer(s->s_auth.authorizer);
672 WARN_ON(mutex_is_locked(&s->s_mutex));
673 xa_destroy(&s->s_delegated_inos);
679 * called under mdsc->mutex
681 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
684 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
686 return ceph_get_mds_session(mdsc->sessions[mds]);
689 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
691 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
697 static int __verify_registered_session(struct ceph_mds_client *mdsc,
698 struct ceph_mds_session *s)
700 if (s->s_mds >= mdsc->max_sessions ||
701 mdsc->sessions[s->s_mds] != s)
707 * create+register a new session for given mds.
708 * called under mdsc->mutex.
710 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
713 struct ceph_mds_session *s;
715 if (mds >= mdsc->mdsmap->possible_max_rank)
716 return ERR_PTR(-EINVAL);
718 s = kzalloc(sizeof(*s), GFP_NOFS);
720 return ERR_PTR(-ENOMEM);
722 if (mds >= mdsc->max_sessions) {
723 int newmax = 1 << get_count_order(mds + 1);
724 struct ceph_mds_session **sa;
726 dout("%s: realloc to %d\n", __func__, newmax);
727 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
730 if (mdsc->sessions) {
731 memcpy(sa, mdsc->sessions,
732 mdsc->max_sessions * sizeof(void *));
733 kfree(mdsc->sessions);
736 mdsc->max_sessions = newmax;
739 dout("%s: mds%d\n", __func__, mds);
742 s->s_state = CEPH_MDS_SESSION_NEW;
745 mutex_init(&s->s_mutex);
747 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
749 spin_lock_init(&s->s_gen_ttl_lock);
751 s->s_cap_ttl = jiffies - 1;
753 spin_lock_init(&s->s_cap_lock);
754 s->s_renew_requested = 0;
756 INIT_LIST_HEAD(&s->s_caps);
758 refcount_set(&s->s_ref, 1);
759 INIT_LIST_HEAD(&s->s_waiting);
760 INIT_LIST_HEAD(&s->s_unsafe);
761 xa_init(&s->s_delegated_inos);
762 s->s_num_cap_releases = 0;
763 s->s_cap_reconnect = 0;
764 s->s_cap_iterator = NULL;
765 INIT_LIST_HEAD(&s->s_cap_releases);
766 INIT_WORK(&s->s_cap_release_work, ceph_cap_release_work);
768 INIT_LIST_HEAD(&s->s_cap_dirty);
769 INIT_LIST_HEAD(&s->s_cap_flushing);
771 mdsc->sessions[mds] = s;
772 atomic_inc(&mdsc->num_sessions);
773 refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
775 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
776 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
782 return ERR_PTR(-ENOMEM);
786 * called under mdsc->mutex
788 static void __unregister_session(struct ceph_mds_client *mdsc,
789 struct ceph_mds_session *s)
791 dout("__unregister_session mds%d %p\n", s->s_mds, s);
792 BUG_ON(mdsc->sessions[s->s_mds] != s);
793 mdsc->sessions[s->s_mds] = NULL;
794 ceph_con_close(&s->s_con);
795 ceph_put_mds_session(s);
796 atomic_dec(&mdsc->num_sessions);
800 * drop session refs in request.
802 * should be last request ref, or hold mdsc->mutex
804 static void put_request_session(struct ceph_mds_request *req)
806 if (req->r_session) {
807 ceph_put_mds_session(req->r_session);
808 req->r_session = NULL;
812 void ceph_mdsc_release_request(struct kref *kref)
814 struct ceph_mds_request *req = container_of(kref,
815 struct ceph_mds_request,
817 ceph_mdsc_release_dir_caps_no_check(req);
818 destroy_reply_info(&req->r_reply_info);
820 ceph_msg_put(req->r_request);
822 ceph_msg_put(req->r_reply);
824 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
825 /* avoid calling iput_final() in mds dispatch threads */
826 ceph_async_iput(req->r_inode);
829 ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
830 ceph_async_iput(req->r_parent);
832 ceph_async_iput(req->r_target_inode);
835 if (req->r_old_dentry)
836 dput(req->r_old_dentry);
837 if (req->r_old_dentry_dir) {
839 * track (and drop pins for) r_old_dentry_dir
840 * separately, since r_old_dentry's d_parent may have
841 * changed between the dir mutex being dropped and
842 * this request being freed.
844 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
846 ceph_async_iput(req->r_old_dentry_dir);
850 put_cred(req->r_cred);
852 ceph_pagelist_release(req->r_pagelist);
853 put_request_session(req);
854 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
855 WARN_ON_ONCE(!list_empty(&req->r_wait));
856 kmem_cache_free(ceph_mds_request_cachep, req);
859 DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
862 * lookup session, bump ref if found.
864 * called under mdsc->mutex.
866 static struct ceph_mds_request *
867 lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
869 struct ceph_mds_request *req;
871 req = lookup_request(&mdsc->request_tree, tid);
873 ceph_mdsc_get_request(req);
879 * Register an in-flight request, and assign a tid. Link to directory
880 * are modifying (if any).
882 * Called under mdsc->mutex.
884 static void __register_request(struct ceph_mds_client *mdsc,
885 struct ceph_mds_request *req,
890 req->r_tid = ++mdsc->last_tid;
891 if (req->r_num_caps) {
892 ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
895 pr_err("__register_request %p "
896 "failed to reserve caps: %d\n", req, ret);
897 /* set req->r_err to fail early from __do_request */
902 dout("__register_request %p tid %lld\n", req, req->r_tid);
903 ceph_mdsc_get_request(req);
904 insert_request(&mdsc->request_tree, req);
906 req->r_cred = get_current_cred();
908 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
909 mdsc->oldest_tid = req->r_tid;
912 struct ceph_inode_info *ci = ceph_inode(dir);
915 req->r_unsafe_dir = dir;
916 spin_lock(&ci->i_unsafe_lock);
917 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
918 spin_unlock(&ci->i_unsafe_lock);
922 static void __unregister_request(struct ceph_mds_client *mdsc,
923 struct ceph_mds_request *req)
925 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
927 /* Never leave an unregistered request on an unsafe list! */
928 list_del_init(&req->r_unsafe_item);
930 if (req->r_tid == mdsc->oldest_tid) {
931 struct rb_node *p = rb_next(&req->r_node);
932 mdsc->oldest_tid = 0;
934 struct ceph_mds_request *next_req =
935 rb_entry(p, struct ceph_mds_request, r_node);
936 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
937 mdsc->oldest_tid = next_req->r_tid;
944 erase_request(&mdsc->request_tree, req);
946 if (req->r_unsafe_dir) {
947 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
948 spin_lock(&ci->i_unsafe_lock);
949 list_del_init(&req->r_unsafe_dir_item);
950 spin_unlock(&ci->i_unsafe_lock);
952 if (req->r_target_inode &&
953 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
954 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
955 spin_lock(&ci->i_unsafe_lock);
956 list_del_init(&req->r_unsafe_target_item);
957 spin_unlock(&ci->i_unsafe_lock);
960 if (req->r_unsafe_dir) {
961 /* avoid calling iput_final() in mds dispatch threads */
962 ceph_async_iput(req->r_unsafe_dir);
963 req->r_unsafe_dir = NULL;
966 complete_all(&req->r_safe_completion);
968 ceph_mdsc_put_request(req);
972 * Walk back up the dentry tree until we hit a dentry representing a
973 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
974 * when calling this) to ensure that the objects won't disappear while we're
975 * working with them. Once we hit a candidate dentry, we attempt to take a
976 * reference to it, and return that as the result.
978 static struct inode *get_nonsnap_parent(struct dentry *dentry)
980 struct inode *inode = NULL;
982 while (dentry && !IS_ROOT(dentry)) {
983 inode = d_inode_rcu(dentry);
984 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
986 dentry = dentry->d_parent;
989 inode = igrab(inode);
994 * Choose mds to send request to next. If there is a hint set in the
995 * request (e.g., due to a prior forward hint from the mds), use that.
996 * Otherwise, consult frag tree and/or caps to identify the
997 * appropriate mds. If all else fails, choose randomly.
999 * Called under mdsc->mutex.
1001 static int __choose_mds(struct ceph_mds_client *mdsc,
1002 struct ceph_mds_request *req,
1005 struct inode *inode;
1006 struct ceph_inode_info *ci;
1007 struct ceph_cap *cap;
1008 int mode = req->r_direct_mode;
1010 u32 hash = req->r_direct_hash;
1011 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
1017 * is there a specific mds we should try? ignore hint if we have
1018 * no session and the mds is not up (active or recovering).
1020 if (req->r_resend_mds >= 0 &&
1021 (__have_session(mdsc, req->r_resend_mds) ||
1022 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
1023 dout("%s using resend_mds mds%d\n", __func__,
1025 return req->r_resend_mds;
1028 if (mode == USE_RANDOM_MDS)
1033 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
1034 inode = req->r_inode;
1037 /* req->r_dentry is non-null for LSSNAP request */
1039 inode = get_nonsnap_parent(req->r_dentry);
1041 dout("%s using snapdir's parent %p\n", __func__, inode);
1043 } else if (req->r_dentry) {
1044 /* ignore race with rename; old or new d_parent is okay */
1045 struct dentry *parent;
1049 parent = READ_ONCE(req->r_dentry->d_parent);
1050 dir = req->r_parent ? : d_inode_rcu(parent);
1052 if (!dir || dir->i_sb != mdsc->fsc->sb) {
1053 /* not this fs or parent went negative */
1054 inode = d_inode(req->r_dentry);
1057 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
1058 /* direct snapped/virtual snapdir requests
1059 * based on parent dir inode */
1060 inode = get_nonsnap_parent(parent);
1061 dout("%s using nonsnap parent %p\n", __func__, inode);
1064 inode = d_inode(req->r_dentry);
1065 if (!inode || mode == USE_AUTH_MDS) {
1068 hash = ceph_dentry_hash(dir, req->r_dentry);
1077 dout("%s %p is_hash=%d (0x%x) mode %d\n", __func__, inode, (int)is_hash,
1081 ci = ceph_inode(inode);
1083 if (is_hash && S_ISDIR(inode->i_mode)) {
1084 struct ceph_inode_frag frag;
1087 ceph_choose_frag(ci, hash, &frag, &found);
1089 if (mode == USE_ANY_MDS && frag.ndist > 0) {
1092 /* choose a random replica */
1093 get_random_bytes(&r, 1);
1096 dout("%s %p %llx.%llx frag %u mds%d (%d/%d)\n",
1097 __func__, inode, ceph_vinop(inode),
1098 frag.frag, mds, (int)r, frag.ndist);
1099 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
1100 CEPH_MDS_STATE_ACTIVE &&
1101 !ceph_mdsmap_is_laggy(mdsc->mdsmap, mds))
1105 /* since this file/dir wasn't known to be
1106 * replicated, then we want to look for the
1107 * authoritative mds. */
1108 if (frag.mds >= 0) {
1109 /* choose auth mds */
1111 dout("%s %p %llx.%llx frag %u mds%d (auth)\n",
1112 __func__, inode, ceph_vinop(inode),
1114 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
1115 CEPH_MDS_STATE_ACTIVE) {
1116 if (!ceph_mdsmap_is_laggy(mdsc->mdsmap,
1121 mode = USE_AUTH_MDS;
1125 spin_lock(&ci->i_ceph_lock);
1127 if (mode == USE_AUTH_MDS)
1128 cap = ci->i_auth_cap;
1129 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
1130 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
1132 spin_unlock(&ci->i_ceph_lock);
1133 ceph_async_iput(inode);
1136 mds = cap->session->s_mds;
1137 dout("%s %p %llx.%llx mds%d (%scap %p)\n", __func__,
1138 inode, ceph_vinop(inode), mds,
1139 cap == ci->i_auth_cap ? "auth " : "", cap);
1140 spin_unlock(&ci->i_ceph_lock);
1142 /* avoid calling iput_final() while holding mdsc->mutex or
1143 * in mds dispatch threads */
1144 ceph_async_iput(inode);
1151 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
1152 dout("%s chose random mds%d\n", __func__, mds);
1160 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
1162 struct ceph_msg *msg;
1163 struct ceph_mds_session_head *h;
1165 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
1168 pr_err("create_session_msg ENOMEM creating msg\n");
1171 h = msg->front.iov_base;
1172 h->op = cpu_to_le32(op);
1173 h->seq = cpu_to_le64(seq);
1178 static const unsigned char feature_bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
1179 #define FEATURE_BYTES(c) (DIV_ROUND_UP((size_t)feature_bits[c - 1] + 1, 64) * 8)
1180 static int encode_supported_features(void **p, void *end)
1182 static const size_t count = ARRAY_SIZE(feature_bits);
1186 size_t size = FEATURE_BYTES(count);
1188 if (WARN_ON_ONCE(*p + 4 + size > end))
1191 ceph_encode_32(p, size);
1192 memset(*p, 0, size);
1193 for (i = 0; i < count; i++)
1194 ((unsigned char*)(*p))[i / 8] |= BIT(feature_bits[i] % 8);
1197 if (WARN_ON_ONCE(*p + 4 > end))
1200 ceph_encode_32(p, 0);
1206 static const unsigned char metric_bits[] = CEPHFS_METRIC_SPEC_CLIENT_SUPPORTED;
1207 #define METRIC_BYTES(cnt) (DIV_ROUND_UP((size_t)metric_bits[cnt - 1] + 1, 64) * 8)
1208 static int encode_metric_spec(void **p, void *end)
1210 static const size_t count = ARRAY_SIZE(metric_bits);
1213 if (WARN_ON_ONCE(*p + 2 > end))
1216 ceph_encode_8(p, 1); /* version */
1217 ceph_encode_8(p, 1); /* compat */
1221 size_t size = METRIC_BYTES(count);
1223 if (WARN_ON_ONCE(*p + 4 + 4 + size > end))
1226 /* metric spec info length */
1227 ceph_encode_32(p, 4 + size);
1230 ceph_encode_32(p, size);
1231 memset(*p, 0, size);
1232 for (i = 0; i < count; i++)
1233 ((unsigned char *)(*p))[i / 8] |= BIT(metric_bits[i] % 8);
1236 if (WARN_ON_ONCE(*p + 4 + 4 > end))
1239 /* metric spec info length */
1240 ceph_encode_32(p, 4);
1242 ceph_encode_32(p, 0);
1249 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
1250 * to include additional client metadata fields.
1252 static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
1254 struct ceph_msg *msg;
1255 struct ceph_mds_session_head *h;
1257 int extra_bytes = 0;
1258 int metadata_key_count = 0;
1259 struct ceph_options *opt = mdsc->fsc->client->options;
1260 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
1265 const char* metadata[][2] = {
1266 {"hostname", mdsc->nodename},
1267 {"kernel_version", init_utsname()->release},
1268 {"entity_id", opt->name ? : ""},
1269 {"root", fsopt->server_path ? : "/"},
1273 /* Calculate serialized length of metadata */
1274 extra_bytes = 4; /* map length */
1275 for (i = 0; metadata[i][0]; ++i) {
1276 extra_bytes += 8 + strlen(metadata[i][0]) +
1277 strlen(metadata[i][1]);
1278 metadata_key_count++;
1281 /* supported feature */
1283 count = ARRAY_SIZE(feature_bits);
1285 size = FEATURE_BYTES(count);
1286 extra_bytes += 4 + size;
1290 count = ARRAY_SIZE(metric_bits);
1292 size = METRIC_BYTES(count);
1293 extra_bytes += 2 + 4 + 4 + size;
1295 /* Allocate the message */
1296 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
1299 pr_err("create_session_msg ENOMEM creating msg\n");
1300 return ERR_PTR(-ENOMEM);
1302 p = msg->front.iov_base;
1303 end = p + msg->front.iov_len;
1306 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
1307 h->seq = cpu_to_le64(seq);
1310 * Serialize client metadata into waiting buffer space, using
1311 * the format that userspace expects for map<string, string>
1313 * ClientSession messages with metadata are v4
1315 msg->hdr.version = cpu_to_le16(4);
1316 msg->hdr.compat_version = cpu_to_le16(1);
1318 /* The write pointer, following the session_head structure */
1321 /* Number of entries in the map */
1322 ceph_encode_32(&p, metadata_key_count);
1324 /* Two length-prefixed strings for each entry in the map */
1325 for (i = 0; metadata[i][0]; ++i) {
1326 size_t const key_len = strlen(metadata[i][0]);
1327 size_t const val_len = strlen(metadata[i][1]);
1329 ceph_encode_32(&p, key_len);
1330 memcpy(p, metadata[i][0], key_len);
1332 ceph_encode_32(&p, val_len);
1333 memcpy(p, metadata[i][1], val_len);
1337 ret = encode_supported_features(&p, end);
1339 pr_err("encode_supported_features failed!\n");
1341 return ERR_PTR(ret);
1344 ret = encode_metric_spec(&p, end);
1346 pr_err("encode_metric_spec failed!\n");
1348 return ERR_PTR(ret);
1351 msg->front.iov_len = p - msg->front.iov_base;
1352 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1358 * send session open request.
1360 * called under mdsc->mutex
1362 static int __open_session(struct ceph_mds_client *mdsc,
1363 struct ceph_mds_session *session)
1365 struct ceph_msg *msg;
1367 int mds = session->s_mds;
1369 /* wait for mds to go active? */
1370 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
1371 dout("open_session to mds%d (%s)\n", mds,
1372 ceph_mds_state_name(mstate));
1373 session->s_state = CEPH_MDS_SESSION_OPENING;
1374 session->s_renew_requested = jiffies;
1376 /* send connect message */
1377 msg = create_session_open_msg(mdsc, session->s_seq);
1379 return PTR_ERR(msg);
1380 ceph_con_send(&session->s_con, msg);
1385 * open sessions for any export targets for the given mds
1387 * called under mdsc->mutex
1389 static struct ceph_mds_session *
1390 __open_export_target_session(struct ceph_mds_client *mdsc, int target)
1392 struct ceph_mds_session *session;
1395 session = __ceph_lookup_mds_session(mdsc, target);
1397 session = register_session(mdsc, target);
1398 if (IS_ERR(session))
1401 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1402 session->s_state == CEPH_MDS_SESSION_CLOSING) {
1403 ret = __open_session(mdsc, session);
1405 return ERR_PTR(ret);
1411 struct ceph_mds_session *
1412 ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1414 struct ceph_mds_session *session;
1416 dout("open_export_target_session to mds%d\n", target);
1418 mutex_lock(&mdsc->mutex);
1419 session = __open_export_target_session(mdsc, target);
1420 mutex_unlock(&mdsc->mutex);
1425 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1426 struct ceph_mds_session *session)
1428 struct ceph_mds_info *mi;
1429 struct ceph_mds_session *ts;
1430 int i, mds = session->s_mds;
1432 if (mds >= mdsc->mdsmap->possible_max_rank)
1435 mi = &mdsc->mdsmap->m_info[mds];
1436 dout("open_export_target_sessions for mds%d (%d targets)\n",
1437 session->s_mds, mi->num_export_targets);
1439 for (i = 0; i < mi->num_export_targets; i++) {
1440 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1442 ceph_put_mds_session(ts);
1446 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1447 struct ceph_mds_session *session)
1449 mutex_lock(&mdsc->mutex);
1450 __open_export_target_sessions(mdsc, session);
1451 mutex_unlock(&mdsc->mutex);
1458 static void detach_cap_releases(struct ceph_mds_session *session,
1459 struct list_head *target)
1461 lockdep_assert_held(&session->s_cap_lock);
1463 list_splice_init(&session->s_cap_releases, target);
1464 session->s_num_cap_releases = 0;
1465 dout("dispose_cap_releases mds%d\n", session->s_mds);
1468 static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1469 struct list_head *dispose)
1471 while (!list_empty(dispose)) {
1472 struct ceph_cap *cap;
1473 /* zero out the in-progress message */
1474 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
1475 list_del(&cap->session_caps);
1476 ceph_put_cap(mdsc, cap);
1480 static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1481 struct ceph_mds_session *session)
1483 struct ceph_mds_request *req;
1485 struct ceph_inode_info *ci;
1487 dout("cleanup_session_requests mds%d\n", session->s_mds);
1488 mutex_lock(&mdsc->mutex);
1489 while (!list_empty(&session->s_unsafe)) {
1490 req = list_first_entry(&session->s_unsafe,
1491 struct ceph_mds_request, r_unsafe_item);
1492 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1494 if (req->r_target_inode) {
1495 /* dropping unsafe change of inode's attributes */
1496 ci = ceph_inode(req->r_target_inode);
1497 errseq_set(&ci->i_meta_err, -EIO);
1499 if (req->r_unsafe_dir) {
1500 /* dropping unsafe directory operation */
1501 ci = ceph_inode(req->r_unsafe_dir);
1502 errseq_set(&ci->i_meta_err, -EIO);
1504 __unregister_request(mdsc, req);
1506 /* zero r_attempts, so kick_requests() will re-send requests */
1507 p = rb_first(&mdsc->request_tree);
1509 req = rb_entry(p, struct ceph_mds_request, r_node);
1511 if (req->r_session &&
1512 req->r_session->s_mds == session->s_mds)
1513 req->r_attempts = 0;
1515 mutex_unlock(&mdsc->mutex);
1519 * Helper to safely iterate over all caps associated with a session, with
1520 * special care taken to handle a racing __ceph_remove_cap().
1522 * Caller must hold session s_mutex.
1524 int ceph_iterate_session_caps(struct ceph_mds_session *session,
1525 int (*cb)(struct inode *, struct ceph_cap *,
1528 struct list_head *p;
1529 struct ceph_cap *cap;
1530 struct inode *inode, *last_inode = NULL;
1531 struct ceph_cap *old_cap = NULL;
1534 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1535 spin_lock(&session->s_cap_lock);
1536 p = session->s_caps.next;
1537 while (p != &session->s_caps) {
1538 cap = list_entry(p, struct ceph_cap, session_caps);
1539 inode = igrab(&cap->ci->vfs_inode);
1544 session->s_cap_iterator = cap;
1545 spin_unlock(&session->s_cap_lock);
1548 /* avoid calling iput_final() while holding
1549 * s_mutex or in mds dispatch threads */
1550 ceph_async_iput(last_inode);
1554 ceph_put_cap(session->s_mdsc, old_cap);
1558 ret = cb(inode, cap, arg);
1561 spin_lock(&session->s_cap_lock);
1564 dout("iterate_session_caps finishing cap %p removal\n",
1566 BUG_ON(cap->session != session);
1567 cap->session = NULL;
1568 list_del_init(&cap->session_caps);
1569 session->s_nr_caps--;
1570 atomic64_dec(&session->s_mdsc->metric.total_caps);
1571 if (cap->queue_release)
1572 __ceph_queue_cap_release(session, cap);
1574 old_cap = cap; /* put_cap it w/o locks held */
1581 session->s_cap_iterator = NULL;
1582 spin_unlock(&session->s_cap_lock);
1584 ceph_async_iput(last_inode);
1586 ceph_put_cap(session->s_mdsc, old_cap);
1591 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
1594 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
1595 struct ceph_inode_info *ci = ceph_inode(inode);
1596 LIST_HEAD(to_remove);
1597 bool dirty_dropped = false;
1598 bool invalidate = false;
1600 dout("removing cap %p, ci is %p, inode is %p\n",
1601 cap, ci, &ci->vfs_inode);
1602 spin_lock(&ci->i_ceph_lock);
1603 __ceph_remove_cap(cap, false);
1604 if (!ci->i_auth_cap) {
1605 struct ceph_cap_flush *cf;
1606 struct ceph_mds_client *mdsc = fsc->mdsc;
1608 if (READ_ONCE(fsc->mount_state) >= CEPH_MOUNT_SHUTDOWN) {
1609 if (inode->i_data.nrpages > 0)
1611 if (ci->i_wrbuffer_ref > 0)
1612 mapping_set_error(&inode->i_data, -EIO);
1615 while (!list_empty(&ci->i_cap_flush_list)) {
1616 cf = list_first_entry(&ci->i_cap_flush_list,
1617 struct ceph_cap_flush, i_list);
1618 list_move(&cf->i_list, &to_remove);
1621 spin_lock(&mdsc->cap_dirty_lock);
1623 list_for_each_entry(cf, &to_remove, i_list)
1624 list_del(&cf->g_list);
1626 if (!list_empty(&ci->i_dirty_item)) {
1627 pr_warn_ratelimited(
1628 " dropping dirty %s state for %p %lld\n",
1629 ceph_cap_string(ci->i_dirty_caps),
1630 inode, ceph_ino(inode));
1631 ci->i_dirty_caps = 0;
1632 list_del_init(&ci->i_dirty_item);
1633 dirty_dropped = true;
1635 if (!list_empty(&ci->i_flushing_item)) {
1636 pr_warn_ratelimited(
1637 " dropping dirty+flushing %s state for %p %lld\n",
1638 ceph_cap_string(ci->i_flushing_caps),
1639 inode, ceph_ino(inode));
1640 ci->i_flushing_caps = 0;
1641 list_del_init(&ci->i_flushing_item);
1642 mdsc->num_cap_flushing--;
1643 dirty_dropped = true;
1645 spin_unlock(&mdsc->cap_dirty_lock);
1647 if (dirty_dropped) {
1648 errseq_set(&ci->i_meta_err, -EIO);
1650 if (ci->i_wrbuffer_ref_head == 0 &&
1651 ci->i_wr_ref == 0 &&
1652 ci->i_dirty_caps == 0 &&
1653 ci->i_flushing_caps == 0) {
1654 ceph_put_snap_context(ci->i_head_snapc);
1655 ci->i_head_snapc = NULL;
1659 if (atomic_read(&ci->i_filelock_ref) > 0) {
1660 /* make further file lock syscall return -EIO */
1661 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1662 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1663 inode, ceph_ino(inode));
1666 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
1667 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
1668 ci->i_prealloc_cap_flush = NULL;
1671 spin_unlock(&ci->i_ceph_lock);
1672 while (!list_empty(&to_remove)) {
1673 struct ceph_cap_flush *cf;
1674 cf = list_first_entry(&to_remove,
1675 struct ceph_cap_flush, i_list);
1676 list_del(&cf->i_list);
1677 ceph_free_cap_flush(cf);
1680 wake_up_all(&ci->i_cap_wq);
1682 ceph_queue_invalidate(inode);
1689 * caller must hold session s_mutex
1691 static void remove_session_caps(struct ceph_mds_session *session)
1693 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1694 struct super_block *sb = fsc->sb;
1697 dout("remove_session_caps on %p\n", session);
1698 ceph_iterate_session_caps(session, remove_session_caps_cb, fsc);
1700 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1702 spin_lock(&session->s_cap_lock);
1703 if (session->s_nr_caps > 0) {
1704 struct inode *inode;
1705 struct ceph_cap *cap, *prev = NULL;
1706 struct ceph_vino vino;
1708 * iterate_session_caps() skips inodes that are being
1709 * deleted, we need to wait until deletions are complete.
1710 * __wait_on_freeing_inode() is designed for the job,
1711 * but it is not exported, so use lookup inode function
1714 while (!list_empty(&session->s_caps)) {
1715 cap = list_entry(session->s_caps.next,
1716 struct ceph_cap, session_caps);
1720 vino = cap->ci->i_vino;
1721 spin_unlock(&session->s_cap_lock);
1723 inode = ceph_find_inode(sb, vino);
1724 /* avoid calling iput_final() while holding s_mutex */
1725 ceph_async_iput(inode);
1727 spin_lock(&session->s_cap_lock);
1731 // drop cap expires and unlock s_cap_lock
1732 detach_cap_releases(session, &dispose);
1734 BUG_ON(session->s_nr_caps > 0);
1735 BUG_ON(!list_empty(&session->s_cap_flushing));
1736 spin_unlock(&session->s_cap_lock);
1737 dispose_cap_releases(session->s_mdsc, &dispose);
1747 * wake up any threads waiting on this session's caps. if the cap is
1748 * old (didn't get renewed on the client reconnect), remove it now.
1750 * caller must hold s_mutex.
1752 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1755 struct ceph_inode_info *ci = ceph_inode(inode);
1756 unsigned long ev = (unsigned long)arg;
1758 if (ev == RECONNECT) {
1759 spin_lock(&ci->i_ceph_lock);
1760 ci->i_wanted_max_size = 0;
1761 ci->i_requested_max_size = 0;
1762 spin_unlock(&ci->i_ceph_lock);
1763 } else if (ev == RENEWCAPS) {
1764 if (cap->cap_gen < cap->session->s_cap_gen) {
1765 /* mds did not re-issue stale cap */
1766 spin_lock(&ci->i_ceph_lock);
1767 cap->issued = cap->implemented = CEPH_CAP_PIN;
1768 spin_unlock(&ci->i_ceph_lock);
1770 } else if (ev == FORCE_RO) {
1772 wake_up_all(&ci->i_cap_wq);
1776 static void wake_up_session_caps(struct ceph_mds_session *session, int ev)
1778 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1779 ceph_iterate_session_caps(session, wake_up_session_cb,
1780 (void *)(unsigned long)ev);
1784 * Send periodic message to MDS renewing all currently held caps. The
1785 * ack will reset the expiration for all caps from this session.
1787 * caller holds s_mutex
1789 static int send_renew_caps(struct ceph_mds_client *mdsc,
1790 struct ceph_mds_session *session)
1792 struct ceph_msg *msg;
1795 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1796 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1797 pr_info("mds%d caps stale\n", session->s_mds);
1798 session->s_renew_requested = jiffies;
1800 /* do not try to renew caps until a recovering mds has reconnected
1801 * with its clients. */
1802 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1803 if (state < CEPH_MDS_STATE_RECONNECT) {
1804 dout("send_renew_caps ignoring mds%d (%s)\n",
1805 session->s_mds, ceph_mds_state_name(state));
1809 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1810 ceph_mds_state_name(state));
1811 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1812 ++session->s_renew_seq);
1815 ceph_con_send(&session->s_con, msg);
1819 static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1820 struct ceph_mds_session *session, u64 seq)
1822 struct ceph_msg *msg;
1824 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1825 session->s_mds, ceph_session_state_name(session->s_state), seq);
1826 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1829 ceph_con_send(&session->s_con, msg);
1835 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1837 * Called under session->s_mutex
1839 static void renewed_caps(struct ceph_mds_client *mdsc,
1840 struct ceph_mds_session *session, int is_renew)
1845 spin_lock(&session->s_cap_lock);
1846 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
1848 session->s_cap_ttl = session->s_renew_requested +
1849 mdsc->mdsmap->m_session_timeout*HZ;
1852 if (time_before(jiffies, session->s_cap_ttl)) {
1853 pr_info("mds%d caps renewed\n", session->s_mds);
1856 pr_info("mds%d caps still stale\n", session->s_mds);
1859 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1860 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1861 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1862 spin_unlock(&session->s_cap_lock);
1865 wake_up_session_caps(session, RENEWCAPS);
1869 * send a session close request
1871 static int request_close_session(struct ceph_mds_session *session)
1873 struct ceph_msg *msg;
1875 dout("request_close_session mds%d state %s seq %lld\n",
1876 session->s_mds, ceph_session_state_name(session->s_state),
1878 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1881 ceph_con_send(&session->s_con, msg);
1886 * Called with s_mutex held.
1888 static int __close_session(struct ceph_mds_client *mdsc,
1889 struct ceph_mds_session *session)
1891 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1893 session->s_state = CEPH_MDS_SESSION_CLOSING;
1894 return request_close_session(session);
1897 static bool drop_negative_children(struct dentry *dentry)
1899 struct dentry *child;
1900 bool all_negative = true;
1902 if (!d_is_dir(dentry))
1905 spin_lock(&dentry->d_lock);
1906 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1907 if (d_really_is_positive(child)) {
1908 all_negative = false;
1912 spin_unlock(&dentry->d_lock);
1915 shrink_dcache_parent(dentry);
1917 return all_negative;
1921 * Trim old(er) caps.
1923 * Because we can't cache an inode without one or more caps, we do
1924 * this indirectly: if a cap is unused, we prune its aliases, at which
1925 * point the inode will hopefully get dropped to.
1927 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1928 * memory pressure from the MDS, though, so it needn't be perfect.
1930 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1932 int *remaining = arg;
1933 struct ceph_inode_info *ci = ceph_inode(inode);
1934 int used, wanted, oissued, mine;
1936 if (*remaining <= 0)
1939 spin_lock(&ci->i_ceph_lock);
1940 mine = cap->issued | cap->implemented;
1941 used = __ceph_caps_used(ci);
1942 wanted = __ceph_caps_file_wanted(ci);
1943 oissued = __ceph_caps_issued_other(ci, cap);
1945 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1946 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1947 ceph_cap_string(used), ceph_cap_string(wanted));
1948 if (cap == ci->i_auth_cap) {
1949 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1950 !list_empty(&ci->i_cap_snaps))
1952 if ((used | wanted) & CEPH_CAP_ANY_WR)
1954 /* Note: it's possible that i_filelock_ref becomes non-zero
1955 * after dropping auth caps. It doesn't hurt because reply
1956 * of lock mds request will re-add auth caps. */
1957 if (atomic_read(&ci->i_filelock_ref) > 0)
1960 /* The inode has cached pages, but it's no longer used.
1961 * we can safely drop it */
1962 if (S_ISREG(inode->i_mode) &&
1963 wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
1964 !(oissued & CEPH_CAP_FILE_CACHE)) {
1968 if ((used | wanted) & ~oissued & mine)
1969 goto out; /* we need these caps */
1972 /* we aren't the only cap.. just remove us */
1973 __ceph_remove_cap(cap, true);
1976 struct dentry *dentry;
1977 /* try dropping referring dentries */
1978 spin_unlock(&ci->i_ceph_lock);
1979 dentry = d_find_any_alias(inode);
1980 if (dentry && drop_negative_children(dentry)) {
1983 d_prune_aliases(inode);
1984 count = atomic_read(&inode->i_count);
1987 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1996 spin_unlock(&ci->i_ceph_lock);
2001 * Trim session cap count down to some max number.
2003 int ceph_trim_caps(struct ceph_mds_client *mdsc,
2004 struct ceph_mds_session *session,
2007 int trim_caps = session->s_nr_caps - max_caps;
2009 dout("trim_caps mds%d start: %d / %d, trim %d\n",
2010 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
2011 if (trim_caps > 0) {
2012 int remaining = trim_caps;
2014 ceph_iterate_session_caps(session, trim_caps_cb, &remaining);
2015 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
2016 session->s_mds, session->s_nr_caps, max_caps,
2017 trim_caps - remaining);
2020 ceph_flush_cap_releases(mdsc, session);
2024 static int check_caps_flush(struct ceph_mds_client *mdsc,
2029 spin_lock(&mdsc->cap_dirty_lock);
2030 if (!list_empty(&mdsc->cap_flush_list)) {
2031 struct ceph_cap_flush *cf =
2032 list_first_entry(&mdsc->cap_flush_list,
2033 struct ceph_cap_flush, g_list);
2034 if (cf->tid <= want_flush_tid) {
2035 dout("check_caps_flush still flushing tid "
2036 "%llu <= %llu\n", cf->tid, want_flush_tid);
2040 spin_unlock(&mdsc->cap_dirty_lock);
2045 * flush all dirty inode data to disk.
2047 * returns true if we've flushed through want_flush_tid
2049 static void wait_caps_flush(struct ceph_mds_client *mdsc,
2052 dout("check_caps_flush want %llu\n", want_flush_tid);
2054 wait_event(mdsc->cap_flushing_wq,
2055 check_caps_flush(mdsc, want_flush_tid));
2057 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
2061 * called under s_mutex
2063 static void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
2064 struct ceph_mds_session *session)
2066 struct ceph_msg *msg = NULL;
2067 struct ceph_mds_cap_release *head;
2068 struct ceph_mds_cap_item *item;
2069 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
2070 struct ceph_cap *cap;
2071 LIST_HEAD(tmp_list);
2072 int num_cap_releases;
2073 __le32 barrier, *cap_barrier;
2075 down_read(&osdc->lock);
2076 barrier = cpu_to_le32(osdc->epoch_barrier);
2077 up_read(&osdc->lock);
2079 spin_lock(&session->s_cap_lock);
2081 list_splice_init(&session->s_cap_releases, &tmp_list);
2082 num_cap_releases = session->s_num_cap_releases;
2083 session->s_num_cap_releases = 0;
2084 spin_unlock(&session->s_cap_lock);
2086 while (!list_empty(&tmp_list)) {
2088 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
2089 PAGE_SIZE, GFP_NOFS, false);
2092 head = msg->front.iov_base;
2093 head->num = cpu_to_le32(0);
2094 msg->front.iov_len = sizeof(*head);
2096 msg->hdr.version = cpu_to_le16(2);
2097 msg->hdr.compat_version = cpu_to_le16(1);
2100 cap = list_first_entry(&tmp_list, struct ceph_cap,
2102 list_del(&cap->session_caps);
2105 head = msg->front.iov_base;
2106 put_unaligned_le32(get_unaligned_le32(&head->num) + 1,
2108 item = msg->front.iov_base + msg->front.iov_len;
2109 item->ino = cpu_to_le64(cap->cap_ino);
2110 item->cap_id = cpu_to_le64(cap->cap_id);
2111 item->migrate_seq = cpu_to_le32(cap->mseq);
2112 item->seq = cpu_to_le32(cap->issue_seq);
2113 msg->front.iov_len += sizeof(*item);
2115 ceph_put_cap(mdsc, cap);
2117 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
2118 // Append cap_barrier field
2119 cap_barrier = msg->front.iov_base + msg->front.iov_len;
2120 *cap_barrier = barrier;
2121 msg->front.iov_len += sizeof(*cap_barrier);
2123 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2124 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
2125 ceph_con_send(&session->s_con, msg);
2130 BUG_ON(num_cap_releases != 0);
2132 spin_lock(&session->s_cap_lock);
2133 if (!list_empty(&session->s_cap_releases))
2135 spin_unlock(&session->s_cap_lock);
2138 // Append cap_barrier field
2139 cap_barrier = msg->front.iov_base + msg->front.iov_len;
2140 *cap_barrier = barrier;
2141 msg->front.iov_len += sizeof(*cap_barrier);
2143 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2144 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
2145 ceph_con_send(&session->s_con, msg);
2149 pr_err("send_cap_releases mds%d, failed to allocate message\n",
2151 spin_lock(&session->s_cap_lock);
2152 list_splice(&tmp_list, &session->s_cap_releases);
2153 session->s_num_cap_releases += num_cap_releases;
2154 spin_unlock(&session->s_cap_lock);
2157 static void ceph_cap_release_work(struct work_struct *work)
2159 struct ceph_mds_session *session =
2160 container_of(work, struct ceph_mds_session, s_cap_release_work);
2162 mutex_lock(&session->s_mutex);
2163 if (session->s_state == CEPH_MDS_SESSION_OPEN ||
2164 session->s_state == CEPH_MDS_SESSION_HUNG)
2165 ceph_send_cap_releases(session->s_mdsc, session);
2166 mutex_unlock(&session->s_mutex);
2167 ceph_put_mds_session(session);
2170 void ceph_flush_cap_releases(struct ceph_mds_client *mdsc,
2171 struct ceph_mds_session *session)
2176 ceph_get_mds_session(session);
2177 if (queue_work(mdsc->fsc->cap_wq,
2178 &session->s_cap_release_work)) {
2179 dout("cap release work queued\n");
2181 ceph_put_mds_session(session);
2182 dout("failed to queue cap release work\n");
2187 * caller holds session->s_cap_lock
2189 void __ceph_queue_cap_release(struct ceph_mds_session *session,
2190 struct ceph_cap *cap)
2192 list_add_tail(&cap->session_caps, &session->s_cap_releases);
2193 session->s_num_cap_releases++;
2195 if (!(session->s_num_cap_releases % CEPH_CAPS_PER_RELEASE))
2196 ceph_flush_cap_releases(session->s_mdsc, session);
2199 static void ceph_cap_reclaim_work(struct work_struct *work)
2201 struct ceph_mds_client *mdsc =
2202 container_of(work, struct ceph_mds_client, cap_reclaim_work);
2203 int ret = ceph_trim_dentries(mdsc);
2205 ceph_queue_cap_reclaim_work(mdsc);
2208 void ceph_queue_cap_reclaim_work(struct ceph_mds_client *mdsc)
2213 if (queue_work(mdsc->fsc->cap_wq, &mdsc->cap_reclaim_work)) {
2214 dout("caps reclaim work queued\n");
2216 dout("failed to queue caps release work\n");
2220 void ceph_reclaim_caps_nr(struct ceph_mds_client *mdsc, int nr)
2225 val = atomic_add_return(nr, &mdsc->cap_reclaim_pending);
2226 if ((val % CEPH_CAPS_PER_RELEASE) < nr) {
2227 atomic_set(&mdsc->cap_reclaim_pending, 0);
2228 ceph_queue_cap_reclaim_work(mdsc);
2236 int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
2239 struct ceph_inode_info *ci = ceph_inode(dir);
2240 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
2241 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
2242 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
2243 unsigned int num_entries;
2246 spin_lock(&ci->i_ceph_lock);
2247 num_entries = ci->i_files + ci->i_subdirs;
2248 spin_unlock(&ci->i_ceph_lock);
2249 num_entries = max(num_entries, 1U);
2250 num_entries = min(num_entries, opt->max_readdir);
2252 order = get_order(size * num_entries);
2253 while (order >= 0) {
2254 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
2257 if (rinfo->dir_entries)
2261 if (!rinfo->dir_entries)
2264 num_entries = (PAGE_SIZE << order) / size;
2265 num_entries = min(num_entries, opt->max_readdir);
2267 rinfo->dir_buf_size = PAGE_SIZE << order;
2268 req->r_num_caps = num_entries + 1;
2269 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
2270 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
2275 * Create an mds request.
2277 struct ceph_mds_request *
2278 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
2280 struct ceph_mds_request *req;
2282 req = kmem_cache_zalloc(ceph_mds_request_cachep, GFP_NOFS);
2284 return ERR_PTR(-ENOMEM);
2286 mutex_init(&req->r_fill_mutex);
2288 req->r_started = jiffies;
2289 req->r_start_latency = ktime_get();
2290 req->r_resend_mds = -1;
2291 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
2292 INIT_LIST_HEAD(&req->r_unsafe_target_item);
2294 kref_init(&req->r_kref);
2295 RB_CLEAR_NODE(&req->r_node);
2296 INIT_LIST_HEAD(&req->r_wait);
2297 init_completion(&req->r_completion);
2298 init_completion(&req->r_safe_completion);
2299 INIT_LIST_HEAD(&req->r_unsafe_item);
2301 ktime_get_coarse_real_ts64(&req->r_stamp);
2304 req->r_direct_mode = mode;
2309 * return oldest (lowest) request, tid in request tree, 0 if none.
2311 * called under mdsc->mutex.
2313 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
2315 if (RB_EMPTY_ROOT(&mdsc->request_tree))
2317 return rb_entry(rb_first(&mdsc->request_tree),
2318 struct ceph_mds_request, r_node);
2321 static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
2323 return mdsc->oldest_tid;
2327 * Build a dentry's path. Allocate on heap; caller must kfree. Based
2328 * on build_path_from_dentry in fs/cifs/dir.c.
2330 * If @stop_on_nosnap, generate path relative to the first non-snapped
2333 * Encode hidden .snap dirs as a double /, i.e.
2334 * foo/.snap/bar -> foo//bar
2336 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase,
2339 struct dentry *temp;
2346 return ERR_PTR(-EINVAL);
2350 return ERR_PTR(-ENOMEM);
2355 seq = read_seqbegin(&rename_lock);
2359 struct inode *inode;
2361 spin_lock(&temp->d_lock);
2362 inode = d_inode(temp);
2363 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
2364 dout("build_path path+%d: %p SNAPDIR\n",
2366 } else if (stop_on_nosnap && inode && dentry != temp &&
2367 ceph_snap(inode) == CEPH_NOSNAP) {
2368 spin_unlock(&temp->d_lock);
2369 pos++; /* get rid of any prepended '/' */
2372 pos -= temp->d_name.len;
2374 spin_unlock(&temp->d_lock);
2377 memcpy(path + pos, temp->d_name.name, temp->d_name.len);
2379 spin_unlock(&temp->d_lock);
2380 temp = READ_ONCE(temp->d_parent);
2382 /* Are we at the root? */
2386 /* Are we out of buffer? */
2392 base = ceph_ino(d_inode(temp));
2395 if (read_seqretry(&rename_lock, seq))
2400 * A rename didn't occur, but somehow we didn't end up where
2401 * we thought we would. Throw a warning and try again.
2403 pr_warn("build_path did not end path lookup where "
2404 "expected, pos is %d\n", pos);
2409 *plen = PATH_MAX - 1 - pos;
2410 dout("build_path on %p %d built %llx '%.*s'\n",
2411 dentry, d_count(dentry), base, *plen, path + pos);
2415 static int build_dentry_path(struct dentry *dentry, struct inode *dir,
2416 const char **ppath, int *ppathlen, u64 *pino,
2417 bool *pfreepath, bool parent_locked)
2423 dir = d_inode_rcu(dentry->d_parent);
2424 if (dir && parent_locked && ceph_snap(dir) == CEPH_NOSNAP) {
2425 *pino = ceph_ino(dir);
2427 *ppath = dentry->d_name.name;
2428 *ppathlen = dentry->d_name.len;
2432 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2434 return PTR_ERR(path);
2440 static int build_inode_path(struct inode *inode,
2441 const char **ppath, int *ppathlen, u64 *pino,
2444 struct dentry *dentry;
2447 if (ceph_snap(inode) == CEPH_NOSNAP) {
2448 *pino = ceph_ino(inode);
2452 dentry = d_find_alias(inode);
2453 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
2456 return PTR_ERR(path);
2463 * request arguments may be specified via an inode *, a dentry *, or
2464 * an explicit ino+path.
2466 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
2467 struct inode *rdiri, const char *rpath,
2468 u64 rino, const char **ppath, int *pathlen,
2469 u64 *ino, bool *freepath, bool parent_locked)
2474 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
2475 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
2477 } else if (rdentry) {
2478 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
2479 freepath, parent_locked);
2480 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
2482 } else if (rpath || rino) {
2485 *pathlen = rpath ? strlen(rpath) : 0;
2486 dout(" path %.*s\n", *pathlen, rpath);
2492 static void encode_timestamp_and_gids(void **p,
2493 const struct ceph_mds_request *req)
2495 struct ceph_timespec ts;
2498 ceph_encode_timespec64(&ts, &req->r_stamp);
2499 ceph_encode_copy(p, &ts, sizeof(ts));
2502 ceph_encode_32(p, req->r_cred->group_info->ngroups);
2503 for (i = 0; i < req->r_cred->group_info->ngroups; i++)
2504 ceph_encode_64(p, from_kgid(&init_user_ns,
2505 req->r_cred->group_info->gid[i]));
2509 * called under mdsc->mutex
2511 static struct ceph_msg *create_request_message(struct ceph_mds_session *session,
2512 struct ceph_mds_request *req,
2513 bool drop_cap_releases)
2515 int mds = session->s_mds;
2516 struct ceph_mds_client *mdsc = session->s_mdsc;
2517 struct ceph_msg *msg;
2518 struct ceph_mds_request_head_old *head;
2519 const char *path1 = NULL;
2520 const char *path2 = NULL;
2521 u64 ino1 = 0, ino2 = 0;
2522 int pathlen1 = 0, pathlen2 = 0;
2523 bool freepath1 = false, freepath2 = false;
2528 bool legacy = !(session->s_con.peer_features & CEPH_FEATURE_FS_BTIME);
2530 ret = set_request_path_attr(req->r_inode, req->r_dentry,
2531 req->r_parent, req->r_path1, req->r_ino1.ino,
2532 &path1, &pathlen1, &ino1, &freepath1,
2533 test_bit(CEPH_MDS_R_PARENT_LOCKED,
2534 &req->r_req_flags));
2540 /* If r_old_dentry is set, then assume that its parent is locked */
2541 ret = set_request_path_attr(NULL, req->r_old_dentry,
2542 req->r_old_dentry_dir,
2543 req->r_path2, req->r_ino2.ino,
2544 &path2, &pathlen2, &ino2, &freepath2, true);
2550 len = legacy ? sizeof(*head) : sizeof(struct ceph_mds_request_head);
2551 len += pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
2552 sizeof(struct ceph_timespec);
2553 len += sizeof(u32) + (sizeof(u64) * req->r_cred->group_info->ngroups);
2555 /* calculate (max) length for cap releases */
2556 len += sizeof(struct ceph_mds_request_release) *
2557 (!!req->r_inode_drop + !!req->r_dentry_drop +
2558 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2560 if (req->r_dentry_drop)
2562 if (req->r_old_dentry_drop)
2565 msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
2567 msg = ERR_PTR(-ENOMEM);
2571 msg->hdr.tid = cpu_to_le64(req->r_tid);
2574 * The old ceph_mds_request_head didn't contain a version field, and
2575 * one was added when we moved the message version from 3->4.
2578 msg->hdr.version = cpu_to_le16(3);
2579 head = msg->front.iov_base;
2580 p = msg->front.iov_base + sizeof(*head);
2582 struct ceph_mds_request_head *new_head = msg->front.iov_base;
2584 msg->hdr.version = cpu_to_le16(4);
2585 new_head->version = cpu_to_le16(CEPH_MDS_REQUEST_HEAD_VERSION);
2586 head = (struct ceph_mds_request_head_old *)&new_head->oldest_client_tid;
2587 p = msg->front.iov_base + sizeof(*new_head);
2590 end = msg->front.iov_base + msg->front.iov_len;
2592 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2593 head->op = cpu_to_le32(req->r_op);
2594 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns,
2595 req->r_cred->fsuid));
2596 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns,
2597 req->r_cred->fsgid));
2598 head->ino = cpu_to_le64(req->r_deleg_ino);
2599 head->args = req->r_args;
2601 ceph_encode_filepath(&p, end, ino1, path1);
2602 ceph_encode_filepath(&p, end, ino2, path2);
2604 /* make note of release offset, in case we need to replay */
2605 req->r_request_release_offset = p - msg->front.iov_base;
2609 if (req->r_inode_drop)
2610 releases += ceph_encode_inode_release(&p,
2611 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
2612 mds, req->r_inode_drop, req->r_inode_unless,
2613 req->r_op == CEPH_MDS_OP_READDIR);
2614 if (req->r_dentry_drop)
2615 releases += ceph_encode_dentry_release(&p, req->r_dentry,
2616 req->r_parent, mds, req->r_dentry_drop,
2617 req->r_dentry_unless);
2618 if (req->r_old_dentry_drop)
2619 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
2620 req->r_old_dentry_dir, mds,
2621 req->r_old_dentry_drop,
2622 req->r_old_dentry_unless);
2623 if (req->r_old_inode_drop)
2624 releases += ceph_encode_inode_release(&p,
2625 d_inode(req->r_old_dentry),
2626 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
2628 if (drop_cap_releases) {
2630 p = msg->front.iov_base + req->r_request_release_offset;
2633 head->num_releases = cpu_to_le16(releases);
2635 encode_timestamp_and_gids(&p, req);
2637 if (WARN_ON_ONCE(p > end)) {
2639 msg = ERR_PTR(-ERANGE);
2643 msg->front.iov_len = p - msg->front.iov_base;
2644 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2646 if (req->r_pagelist) {
2647 struct ceph_pagelist *pagelist = req->r_pagelist;
2648 ceph_msg_data_add_pagelist(msg, pagelist);
2649 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2651 msg->hdr.data_len = 0;
2654 msg->hdr.data_off = cpu_to_le16(0);
2658 ceph_mdsc_free_path((char *)path2, pathlen2);
2661 ceph_mdsc_free_path((char *)path1, pathlen1);
2667 * called under mdsc->mutex if error, under no mutex if
2670 static void complete_request(struct ceph_mds_client *mdsc,
2671 struct ceph_mds_request *req)
2673 req->r_end_latency = ktime_get();
2675 if (req->r_callback)
2676 req->r_callback(mdsc, req);
2677 complete_all(&req->r_completion);
2680 static struct ceph_mds_request_head_old *
2681 find_old_request_head(void *p, u64 features)
2683 bool legacy = !(features & CEPH_FEATURE_FS_BTIME);
2684 struct ceph_mds_request_head *new_head;
2687 return (struct ceph_mds_request_head_old *)p;
2688 new_head = (struct ceph_mds_request_head *)p;
2689 return (struct ceph_mds_request_head_old *)&new_head->oldest_client_tid;
2693 * called under mdsc->mutex
2695 static int __prepare_send_request(struct ceph_mds_session *session,
2696 struct ceph_mds_request *req,
2697 bool drop_cap_releases)
2699 int mds = session->s_mds;
2700 struct ceph_mds_client *mdsc = session->s_mdsc;
2701 struct ceph_mds_request_head_old *rhead;
2702 struct ceph_msg *msg;
2707 struct ceph_cap *cap =
2708 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2711 req->r_sent_on_mseq = cap->mseq;
2713 req->r_sent_on_mseq = -1;
2715 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2716 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2718 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2722 * Replay. Do not regenerate message (and rebuild
2723 * paths, etc.); just use the original message.
2724 * Rebuilding paths will break for renames because
2725 * d_move mangles the src name.
2727 msg = req->r_request;
2728 rhead = find_old_request_head(msg->front.iov_base,
2729 session->s_con.peer_features);
2731 flags = le32_to_cpu(rhead->flags);
2732 flags |= CEPH_MDS_FLAG_REPLAY;
2733 rhead->flags = cpu_to_le32(flags);
2735 if (req->r_target_inode)
2736 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2738 rhead->num_retry = req->r_attempts - 1;
2740 /* remove cap/dentry releases from message */
2741 rhead->num_releases = 0;
2743 p = msg->front.iov_base + req->r_request_release_offset;
2744 encode_timestamp_and_gids(&p, req);
2746 msg->front.iov_len = p - msg->front.iov_base;
2747 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2751 if (req->r_request) {
2752 ceph_msg_put(req->r_request);
2753 req->r_request = NULL;
2755 msg = create_request_message(session, req, drop_cap_releases);
2757 req->r_err = PTR_ERR(msg);
2758 return PTR_ERR(msg);
2760 req->r_request = msg;
2762 rhead = find_old_request_head(msg->front.iov_base,
2763 session->s_con.peer_features);
2764 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
2765 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2766 flags |= CEPH_MDS_FLAG_REPLAY;
2767 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags))
2768 flags |= CEPH_MDS_FLAG_ASYNC;
2770 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2771 rhead->flags = cpu_to_le32(flags);
2772 rhead->num_fwd = req->r_num_fwd;
2773 rhead->num_retry = req->r_attempts - 1;
2775 dout(" r_parent = %p\n", req->r_parent);
2780 * called under mdsc->mutex
2782 static int __send_request(struct ceph_mds_session *session,
2783 struct ceph_mds_request *req,
2784 bool drop_cap_releases)
2788 err = __prepare_send_request(session, req, drop_cap_releases);
2790 ceph_msg_get(req->r_request);
2791 ceph_con_send(&session->s_con, req->r_request);
2798 * send request, or put it on the appropriate wait list.
2800 static void __do_request(struct ceph_mds_client *mdsc,
2801 struct ceph_mds_request *req)
2803 struct ceph_mds_session *session = NULL;
2808 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2809 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
2810 __unregister_request(mdsc, req);
2814 if (req->r_timeout &&
2815 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2816 dout("do_request timed out\n");
2820 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
2821 dout("do_request forced umount\n");
2825 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
2826 if (mdsc->mdsmap_err) {
2827 err = mdsc->mdsmap_err;
2828 dout("do_request mdsmap err %d\n", err);
2831 if (mdsc->mdsmap->m_epoch == 0) {
2832 dout("do_request no mdsmap, waiting for map\n");
2833 list_add(&req->r_wait, &mdsc->waiting_for_map);
2836 if (!(mdsc->fsc->mount_options->flags &
2837 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2838 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
2839 err = -EHOSTUNREACH;
2844 put_request_session(req);
2846 mds = __choose_mds(mdsc, req, &random);
2848 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2849 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags)) {
2853 dout("do_request no mds or not active, waiting for map\n");
2854 list_add(&req->r_wait, &mdsc->waiting_for_map);
2858 /* get, open session */
2859 session = __ceph_lookup_mds_session(mdsc, mds);
2861 session = register_session(mdsc, mds);
2862 if (IS_ERR(session)) {
2863 err = PTR_ERR(session);
2867 req->r_session = ceph_get_mds_session(session);
2869 dout("do_request mds%d session %p state %s\n", mds, session,
2870 ceph_session_state_name(session->s_state));
2871 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2872 session->s_state != CEPH_MDS_SESSION_HUNG) {
2874 * We cannot queue async requests since the caps and delegated
2875 * inodes are bound to the session. Just return -EJUKEBOX and
2876 * let the caller retry a sync request in that case.
2878 if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags)) {
2884 * If the session has been REJECTED, then return a hard error,
2885 * unless it's a CLEANRECOVER mount, in which case we'll queue
2886 * it to the mdsc queue.
2888 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2889 if (ceph_test_mount_opt(mdsc->fsc, CLEANRECOVER))
2890 list_add(&req->r_wait, &mdsc->waiting_for_map);
2896 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2897 session->s_state == CEPH_MDS_SESSION_CLOSING) {
2898 err = __open_session(mdsc, session);
2901 /* retry the same mds later */
2903 req->r_resend_mds = mds;
2905 list_add(&req->r_wait, &session->s_waiting);
2910 req->r_resend_mds = -1; /* forget any previous mds hint */
2912 if (req->r_request_started == 0) /* note request start time */
2913 req->r_request_started = jiffies;
2915 err = __send_request(session, req, false);
2918 ceph_put_mds_session(session);
2921 dout("__do_request early error %d\n", err);
2923 complete_request(mdsc, req);
2924 __unregister_request(mdsc, req);
2930 * called under mdsc->mutex
2932 static void __wake_requests(struct ceph_mds_client *mdsc,
2933 struct list_head *head)
2935 struct ceph_mds_request *req;
2936 LIST_HEAD(tmp_list);
2938 list_splice_init(head, &tmp_list);
2940 while (!list_empty(&tmp_list)) {
2941 req = list_entry(tmp_list.next,
2942 struct ceph_mds_request, r_wait);
2943 list_del_init(&req->r_wait);
2944 dout(" wake request %p tid %llu\n", req, req->r_tid);
2945 __do_request(mdsc, req);
2950 * Wake up threads with requests pending for @mds, so that they can
2951 * resubmit their requests to a possibly different mds.
2953 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
2955 struct ceph_mds_request *req;
2956 struct rb_node *p = rb_first(&mdsc->request_tree);
2958 dout("kick_requests mds%d\n", mds);
2960 req = rb_entry(p, struct ceph_mds_request, r_node);
2962 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2964 if (req->r_attempts > 0)
2965 continue; /* only new requests */
2966 if (req->r_session &&
2967 req->r_session->s_mds == mds) {
2968 dout(" kicking tid %llu\n", req->r_tid);
2969 list_del_init(&req->r_wait);
2970 __do_request(mdsc, req);
2975 int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
2976 struct ceph_mds_request *req)
2980 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
2982 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
2983 if (req->r_parent) {
2984 struct ceph_inode_info *ci = ceph_inode(req->r_parent);
2985 int fmode = (req->r_op & CEPH_MDS_OP_WRITE) ?
2986 CEPH_FILE_MODE_WR : CEPH_FILE_MODE_RD;
2987 spin_lock(&ci->i_ceph_lock);
2988 ceph_take_cap_refs(ci, CEPH_CAP_PIN, false);
2989 __ceph_touch_fmode(ci, mdsc, fmode);
2990 spin_unlock(&ci->i_ceph_lock);
2991 ihold(req->r_parent);
2993 if (req->r_old_dentry_dir)
2994 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2998 err = ceph_wait_on_async_create(req->r_inode);
3000 dout("%s: wait for async create returned: %d\n",
3006 if (!err && req->r_old_inode) {
3007 err = ceph_wait_on_async_create(req->r_old_inode);
3009 dout("%s: wait for async create returned: %d\n",
3015 dout("submit_request on %p for inode %p\n", req, dir);
3016 mutex_lock(&mdsc->mutex);
3017 __register_request(mdsc, req, dir);
3018 __do_request(mdsc, req);
3020 mutex_unlock(&mdsc->mutex);
3024 static int ceph_mdsc_wait_request(struct ceph_mds_client *mdsc,
3025 struct ceph_mds_request *req)
3030 dout("do_request waiting\n");
3031 if (!req->r_timeout && req->r_wait_for_completion) {
3032 err = req->r_wait_for_completion(mdsc, req);
3034 long timeleft = wait_for_completion_killable_timeout(
3036 ceph_timeout_jiffies(req->r_timeout));
3040 err = -ETIMEDOUT; /* timed out */
3042 err = timeleft; /* killed */
3044 dout("do_request waited, got %d\n", err);
3045 mutex_lock(&mdsc->mutex);
3047 /* only abort if we didn't race with a real reply */
3048 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
3049 err = le32_to_cpu(req->r_reply_info.head->result);
3050 } else if (err < 0) {
3051 dout("aborted request %lld with %d\n", req->r_tid, err);
3054 * ensure we aren't running concurrently with
3055 * ceph_fill_trace or ceph_readdir_prepopulate, which
3056 * rely on locks (dir mutex) held by our caller.
3058 mutex_lock(&req->r_fill_mutex);
3060 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
3061 mutex_unlock(&req->r_fill_mutex);
3063 if (req->r_parent &&
3064 (req->r_op & CEPH_MDS_OP_WRITE))
3065 ceph_invalidate_dir_request(req);
3070 mutex_unlock(&mdsc->mutex);
3075 * Synchrously perform an mds request. Take care of all of the
3076 * session setup, forwarding, retry details.
3078 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
3080 struct ceph_mds_request *req)
3084 dout("do_request on %p\n", req);
3087 err = ceph_mdsc_submit_request(mdsc, dir, req);
3089 err = ceph_mdsc_wait_request(mdsc, req);
3090 dout("do_request %p done, result %d\n", req, err);
3095 * Invalidate dir's completeness, dentry lease state on an aborted MDS
3096 * namespace request.
3098 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
3100 struct inode *dir = req->r_parent;
3101 struct inode *old_dir = req->r_old_dentry_dir;
3103 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
3105 ceph_dir_clear_complete(dir);
3107 ceph_dir_clear_complete(old_dir);
3109 ceph_invalidate_dentry_lease(req->r_dentry);
3110 if (req->r_old_dentry)
3111 ceph_invalidate_dentry_lease(req->r_old_dentry);
3117 * We take the session mutex and parse and process the reply immediately.
3118 * This preserves the logical ordering of replies, capabilities, etc., sent
3119 * by the MDS as they are applied to our local cache.
3121 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
3123 struct ceph_mds_client *mdsc = session->s_mdsc;
3124 struct ceph_mds_request *req;
3125 struct ceph_mds_reply_head *head = msg->front.iov_base;
3126 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
3127 struct ceph_snap_realm *realm;
3130 int mds = session->s_mds;
3132 if (msg->front.iov_len < sizeof(*head)) {
3133 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
3138 /* get request, session */
3139 tid = le64_to_cpu(msg->hdr.tid);
3140 mutex_lock(&mdsc->mutex);
3141 req = lookup_get_request(mdsc, tid);
3143 dout("handle_reply on unknown tid %llu\n", tid);
3144 mutex_unlock(&mdsc->mutex);
3147 dout("handle_reply %p\n", req);
3149 /* correct session? */
3150 if (req->r_session != session) {
3151 pr_err("mdsc_handle_reply got %llu on session mds%d"
3152 " not mds%d\n", tid, session->s_mds,
3153 req->r_session ? req->r_session->s_mds : -1);
3154 mutex_unlock(&mdsc->mutex);
3159 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
3160 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
3161 pr_warn("got a dup %s reply on %llu from mds%d\n",
3162 head->safe ? "safe" : "unsafe", tid, mds);
3163 mutex_unlock(&mdsc->mutex);
3166 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
3167 pr_warn("got unsafe after safe on %llu from mds%d\n",
3169 mutex_unlock(&mdsc->mutex);
3173 result = le32_to_cpu(head->result);
3177 * if we're not talking to the authority, send to them
3178 * if the authority has changed while we weren't looking,
3179 * send to new authority
3180 * Otherwise we just have to return an ESTALE
3182 if (result == -ESTALE) {
3183 dout("got ESTALE on request %llu\n", req->r_tid);
3184 req->r_resend_mds = -1;
3185 if (req->r_direct_mode != USE_AUTH_MDS) {
3186 dout("not using auth, setting for that now\n");
3187 req->r_direct_mode = USE_AUTH_MDS;
3188 __do_request(mdsc, req);
3189 mutex_unlock(&mdsc->mutex);
3192 int mds = __choose_mds(mdsc, req, NULL);
3193 if (mds >= 0 && mds != req->r_session->s_mds) {
3194 dout("but auth changed, so resending\n");
3195 __do_request(mdsc, req);
3196 mutex_unlock(&mdsc->mutex);
3200 dout("have to return ESTALE on request %llu\n", req->r_tid);
3205 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
3206 __unregister_request(mdsc, req);
3208 /* last request during umount? */
3209 if (mdsc->stopping && !__get_oldest_req(mdsc))
3210 complete_all(&mdsc->safe_umount_waiters);
3212 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
3214 * We already handled the unsafe response, now do the
3215 * cleanup. No need to examine the response; the MDS
3216 * doesn't include any result info in the safe
3217 * response. And even if it did, there is nothing
3218 * useful we could do with a revised return value.
3220 dout("got safe reply %llu, mds%d\n", tid, mds);
3222 mutex_unlock(&mdsc->mutex);
3226 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
3227 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
3230 dout("handle_reply tid %lld result %d\n", tid, result);
3231 rinfo = &req->r_reply_info;
3232 if (test_bit(CEPHFS_FEATURE_REPLY_ENCODING, &session->s_features))
3233 err = parse_reply_info(session, msg, rinfo, (u64)-1);
3235 err = parse_reply_info(session, msg, rinfo, session->s_con.peer_features);
3236 mutex_unlock(&mdsc->mutex);
3238 /* Must find target inode outside of mutexes to avoid deadlocks */
3239 if ((err >= 0) && rinfo->head->is_target) {
3241 struct ceph_vino tvino = {
3242 .ino = le64_to_cpu(rinfo->targeti.in->ino),
3243 .snap = le64_to_cpu(rinfo->targeti.in->snapid)
3246 in = ceph_get_inode(mdsc->fsc->sb, tvino);
3249 mutex_lock(&session->s_mutex);
3252 req->r_target_inode = in;
3255 mutex_lock(&session->s_mutex);
3257 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
3264 if (rinfo->snapblob_len) {
3265 down_write(&mdsc->snap_rwsem);
3266 ceph_update_snap_trace(mdsc, rinfo->snapblob,
3267 rinfo->snapblob + rinfo->snapblob_len,
3268 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
3270 downgrade_write(&mdsc->snap_rwsem);
3272 down_read(&mdsc->snap_rwsem);
3275 /* insert trace into our cache */
3276 mutex_lock(&req->r_fill_mutex);
3277 current->journal_info = req;
3278 err = ceph_fill_trace(mdsc->fsc->sb, req);
3280 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
3281 req->r_op == CEPH_MDS_OP_LSSNAP))
3282 ceph_readdir_prepopulate(req, req->r_session);
3284 current->journal_info = NULL;
3285 mutex_unlock(&req->r_fill_mutex);
3287 up_read(&mdsc->snap_rwsem);
3289 ceph_put_snap_realm(mdsc, realm);
3292 if (req->r_target_inode &&
3293 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
3294 struct ceph_inode_info *ci =
3295 ceph_inode(req->r_target_inode);
3296 spin_lock(&ci->i_unsafe_lock);
3297 list_add_tail(&req->r_unsafe_target_item,
3298 &ci->i_unsafe_iops);
3299 spin_unlock(&ci->i_unsafe_lock);
3302 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
3305 mutex_lock(&mdsc->mutex);
3306 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
3310 req->r_reply = ceph_msg_get(msg);
3311 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
3314 dout("reply arrived after request %lld was aborted\n", tid);
3316 mutex_unlock(&mdsc->mutex);
3318 mutex_unlock(&session->s_mutex);
3320 /* kick calling process */
3321 complete_request(mdsc, req);
3323 ceph_update_metadata_metrics(&mdsc->metric, req->r_start_latency,
3324 req->r_end_latency, err);
3326 ceph_mdsc_put_request(req);
3333 * handle mds notification that our request has been forwarded.
3335 static void handle_forward(struct ceph_mds_client *mdsc,
3336 struct ceph_mds_session *session,
3337 struct ceph_msg *msg)
3339 struct ceph_mds_request *req;
3340 u64 tid = le64_to_cpu(msg->hdr.tid);
3344 void *p = msg->front.iov_base;
3345 void *end = p + msg->front.iov_len;
3347 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3348 next_mds = ceph_decode_32(&p);
3349 fwd_seq = ceph_decode_32(&p);
3351 mutex_lock(&mdsc->mutex);
3352 req = lookup_get_request(mdsc, tid);
3354 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
3355 goto out; /* dup reply? */
3358 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
3359 dout("forward tid %llu aborted, unregistering\n", tid);
3360 __unregister_request(mdsc, req);
3361 } else if (fwd_seq <= req->r_num_fwd) {
3362 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
3363 tid, next_mds, req->r_num_fwd, fwd_seq);
3365 /* resend. forward race not possible; mds would drop */
3366 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
3368 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
3369 req->r_attempts = 0;
3370 req->r_num_fwd = fwd_seq;
3371 req->r_resend_mds = next_mds;
3372 put_request_session(req);
3373 __do_request(mdsc, req);
3375 ceph_mdsc_put_request(req);
3377 mutex_unlock(&mdsc->mutex);
3381 pr_err("mdsc_handle_forward decode error err=%d\n", err);
3384 static int __decode_session_metadata(void **p, void *end,
3387 /* map<string,string> */
3390 ceph_decode_32_safe(p, end, n, bad);
3393 ceph_decode_32_safe(p, end, len, bad);
3394 ceph_decode_need(p, end, len, bad);
3395 err_str = !strncmp(*p, "error_string", len);
3397 ceph_decode_32_safe(p, end, len, bad);
3398 ceph_decode_need(p, end, len, bad);
3400 * Match "blocklisted (blacklisted)" from newer MDSes,
3401 * or "blacklisted" from older MDSes.
3403 if (err_str && strnstr(*p, "blacklisted", len))
3404 *blocklisted = true;
3413 * handle a mds session control message
3415 static void handle_session(struct ceph_mds_session *session,
3416 struct ceph_msg *msg)
3418 struct ceph_mds_client *mdsc = session->s_mdsc;
3419 int mds = session->s_mds;
3420 int msg_version = le16_to_cpu(msg->hdr.version);
3421 void *p = msg->front.iov_base;
3422 void *end = p + msg->front.iov_len;
3423 struct ceph_mds_session_head *h;
3425 u64 seq, features = 0;
3427 bool blocklisted = false;
3430 ceph_decode_need(&p, end, sizeof(*h), bad);
3434 op = le32_to_cpu(h->op);
3435 seq = le64_to_cpu(h->seq);
3437 if (msg_version >= 3) {
3439 /* version >= 2, metadata */
3440 if (__decode_session_metadata(&p, end, &blocklisted) < 0)
3442 /* version >= 3, feature bits */
3443 ceph_decode_32_safe(&p, end, len, bad);
3445 ceph_decode_64_safe(&p, end, features, bad);
3446 p += len - sizeof(features);
3450 mutex_lock(&mdsc->mutex);
3451 if (op == CEPH_SESSION_CLOSE) {
3452 ceph_get_mds_session(session);
3453 __unregister_session(mdsc, session);
3455 /* FIXME: this ttl calculation is generous */
3456 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
3457 mutex_unlock(&mdsc->mutex);
3459 mutex_lock(&session->s_mutex);
3461 dout("handle_session mds%d %s %p state %s seq %llu\n",
3462 mds, ceph_session_op_name(op), session,
3463 ceph_session_state_name(session->s_state), seq);
3465 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
3466 session->s_state = CEPH_MDS_SESSION_OPEN;
3467 pr_info("mds%d came back\n", session->s_mds);
3471 case CEPH_SESSION_OPEN:
3472 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3473 pr_info("mds%d reconnect success\n", session->s_mds);
3474 session->s_state = CEPH_MDS_SESSION_OPEN;
3475 session->s_features = features;
3476 renewed_caps(mdsc, session, 0);
3477 if (test_bit(CEPHFS_FEATURE_METRIC_COLLECT, &session->s_features))
3478 metric_schedule_delayed(&mdsc->metric);
3481 __close_session(mdsc, session);
3484 case CEPH_SESSION_RENEWCAPS:
3485 if (session->s_renew_seq == seq)
3486 renewed_caps(mdsc, session, 1);
3489 case CEPH_SESSION_CLOSE:
3490 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
3491 pr_info("mds%d reconnect denied\n", session->s_mds);
3492 session->s_state = CEPH_MDS_SESSION_CLOSED;
3493 cleanup_session_requests(mdsc, session);
3494 remove_session_caps(session);
3495 wake = 2; /* for good measure */
3496 wake_up_all(&mdsc->session_close_wq);
3499 case CEPH_SESSION_STALE:
3500 pr_info("mds%d caps went stale, renewing\n",
3502 spin_lock(&session->s_gen_ttl_lock);
3503 session->s_cap_gen++;
3504 session->s_cap_ttl = jiffies - 1;
3505 spin_unlock(&session->s_gen_ttl_lock);
3506 send_renew_caps(mdsc, session);
3509 case CEPH_SESSION_RECALL_STATE:
3510 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
3513 case CEPH_SESSION_FLUSHMSG:
3514 send_flushmsg_ack(mdsc, session, seq);
3517 case CEPH_SESSION_FORCE_RO:
3518 dout("force_session_readonly %p\n", session);
3519 spin_lock(&session->s_cap_lock);
3520 session->s_readonly = true;
3521 spin_unlock(&session->s_cap_lock);
3522 wake_up_session_caps(session, FORCE_RO);
3525 case CEPH_SESSION_REJECT:
3526 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
3527 pr_info("mds%d rejected session\n", session->s_mds);
3528 session->s_state = CEPH_MDS_SESSION_REJECTED;
3529 cleanup_session_requests(mdsc, session);
3530 remove_session_caps(session);
3532 mdsc->fsc->blocklisted = true;
3533 wake = 2; /* for good measure */
3537 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
3541 mutex_unlock(&session->s_mutex);
3543 mutex_lock(&mdsc->mutex);
3544 __wake_requests(mdsc, &session->s_waiting);
3546 kick_requests(mdsc, mds);
3547 mutex_unlock(&mdsc->mutex);
3549 if (op == CEPH_SESSION_CLOSE)
3550 ceph_put_mds_session(session);
3554 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
3555 (int)msg->front.iov_len);
3560 void ceph_mdsc_release_dir_caps(struct ceph_mds_request *req)
3564 dcaps = xchg(&req->r_dir_caps, 0);
3566 dout("releasing r_dir_caps=%s\n", ceph_cap_string(dcaps));
3567 ceph_put_cap_refs(ceph_inode(req->r_parent), dcaps);
3571 void ceph_mdsc_release_dir_caps_no_check(struct ceph_mds_request *req)
3575 dcaps = xchg(&req->r_dir_caps, 0);
3577 dout("releasing r_dir_caps=%s\n", ceph_cap_string(dcaps));
3578 ceph_put_cap_refs_no_check_caps(ceph_inode(req->r_parent),
3584 * called under session->mutex.
3586 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
3587 struct ceph_mds_session *session)
3589 struct ceph_mds_request *req, *nreq;
3592 dout("replay_unsafe_requests mds%d\n", session->s_mds);
3594 mutex_lock(&mdsc->mutex);
3595 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item)
3596 __send_request(session, req, true);
3599 * also re-send old requests when MDS enters reconnect stage. So that MDS
3600 * can process completed request in clientreplay stage.
3602 p = rb_first(&mdsc->request_tree);
3604 req = rb_entry(p, struct ceph_mds_request, r_node);
3606 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
3608 if (req->r_attempts == 0)
3609 continue; /* only old requests */
3610 if (!req->r_session)
3612 if (req->r_session->s_mds != session->s_mds)
3615 ceph_mdsc_release_dir_caps_no_check(req);
3617 __send_request(session, req, true);
3619 mutex_unlock(&mdsc->mutex);
3622 static int send_reconnect_partial(struct ceph_reconnect_state *recon_state)
3624 struct ceph_msg *reply;
3625 struct ceph_pagelist *_pagelist;
3630 if (!recon_state->allow_multi)
3633 /* can't handle message that contains both caps and realm */
3634 BUG_ON(!recon_state->nr_caps == !recon_state->nr_realms);
3636 /* pre-allocate new pagelist */
3637 _pagelist = ceph_pagelist_alloc(GFP_NOFS);
3641 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3645 /* placeholder for nr_caps */
3646 err = ceph_pagelist_encode_32(_pagelist, 0);
3650 if (recon_state->nr_caps) {
3651 /* currently encoding caps */
3652 err = ceph_pagelist_encode_32(recon_state->pagelist, 0);
3656 /* placeholder for nr_realms (currently encoding relams) */
3657 err = ceph_pagelist_encode_32(_pagelist, 0);
3662 err = ceph_pagelist_encode_8(recon_state->pagelist, 1);
3666 page = list_first_entry(&recon_state->pagelist->head, struct page, lru);
3667 addr = kmap_atomic(page);
3668 if (recon_state->nr_caps) {
3669 /* currently encoding caps */
3670 *addr = cpu_to_le32(recon_state->nr_caps);
3672 /* currently encoding relams */
3673 *(addr + 1) = cpu_to_le32(recon_state->nr_realms);
3675 kunmap_atomic(addr);
3677 reply->hdr.version = cpu_to_le16(5);
3678 reply->hdr.compat_version = cpu_to_le16(4);
3680 reply->hdr.data_len = cpu_to_le32(recon_state->pagelist->length);
3681 ceph_msg_data_add_pagelist(reply, recon_state->pagelist);
3683 ceph_con_send(&recon_state->session->s_con, reply);
3684 ceph_pagelist_release(recon_state->pagelist);
3686 recon_state->pagelist = _pagelist;
3687 recon_state->nr_caps = 0;
3688 recon_state->nr_realms = 0;
3689 recon_state->msg_version = 5;
3692 ceph_msg_put(reply);
3694 ceph_pagelist_release(_pagelist);
3698 static struct dentry* d_find_primary(struct inode *inode)
3700 struct dentry *alias, *dn = NULL;
3702 if (hlist_empty(&inode->i_dentry))
3705 spin_lock(&inode->i_lock);
3706 if (hlist_empty(&inode->i_dentry))
3709 if (S_ISDIR(inode->i_mode)) {
3710 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
3711 if (!IS_ROOT(alias))
3716 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
3717 spin_lock(&alias->d_lock);
3718 if (!d_unhashed(alias) &&
3719 (ceph_dentry(alias)->flags & CEPH_DENTRY_PRIMARY_LINK)) {
3720 dn = dget_dlock(alias);
3722 spin_unlock(&alias->d_lock);
3727 spin_unlock(&inode->i_lock);
3732 * Encode information about a cap for a reconnect with the MDS.
3734 static int reconnect_caps_cb(struct inode *inode, struct ceph_cap *cap,
3738 struct ceph_mds_cap_reconnect v2;
3739 struct ceph_mds_cap_reconnect_v1 v1;
3741 struct ceph_inode_info *ci = cap->ci;
3742 struct ceph_reconnect_state *recon_state = arg;
3743 struct ceph_pagelist *pagelist = recon_state->pagelist;
3744 struct dentry *dentry;
3750 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
3751 inode, ceph_vinop(inode), cap, cap->cap_id,
3752 ceph_cap_string(cap->issued));
3754 dentry = d_find_primary(inode);
3756 /* set pathbase to parent dir when msg_version >= 2 */
3757 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase,
3758 recon_state->msg_version >= 2);
3761 err = PTR_ERR(path);
3770 spin_lock(&ci->i_ceph_lock);
3771 cap->seq = 0; /* reset cap seq */
3772 cap->issue_seq = 0; /* and issue_seq */
3773 cap->mseq = 0; /* and migrate_seq */
3774 cap->cap_gen = cap->session->s_cap_gen;
3776 /* These are lost when the session goes away */
3777 if (S_ISDIR(inode->i_mode)) {
3778 if (cap->issued & CEPH_CAP_DIR_CREATE) {
3779 ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
3780 memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
3782 cap->issued &= ~CEPH_CAP_ANY_DIR_OPS;
3785 if (recon_state->msg_version >= 2) {
3786 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
3787 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3788 rec.v2.issued = cpu_to_le32(cap->issued);
3789 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
3790 rec.v2.pathbase = cpu_to_le64(pathbase);
3791 rec.v2.flock_len = (__force __le32)
3792 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
3794 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
3795 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
3796 rec.v1.issued = cpu_to_le32(cap->issued);
3797 rec.v1.size = cpu_to_le64(i_size_read(inode));
3798 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
3799 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
3800 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
3801 rec.v1.pathbase = cpu_to_le64(pathbase);
3804 if (list_empty(&ci->i_cap_snaps)) {
3805 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
3807 struct ceph_cap_snap *capsnap =
3808 list_first_entry(&ci->i_cap_snaps,
3809 struct ceph_cap_snap, ci_item);
3810 snap_follows = capsnap->follows;
3812 spin_unlock(&ci->i_ceph_lock);
3814 if (recon_state->msg_version >= 2) {
3815 int num_fcntl_locks, num_flock_locks;
3816 struct ceph_filelock *flocks = NULL;
3817 size_t struct_len, total_len = sizeof(u64);
3821 if (rec.v2.flock_len) {
3822 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
3824 num_fcntl_locks = 0;
3825 num_flock_locks = 0;
3827 if (num_fcntl_locks + num_flock_locks > 0) {
3828 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
3829 sizeof(struct ceph_filelock),
3835 err = ceph_encode_locks_to_buffer(inode, flocks,
3850 if (recon_state->msg_version >= 3) {
3851 /* version, compat_version and struct_len */
3852 total_len += 2 * sizeof(u8) + sizeof(u32);
3856 * number of encoded locks is stable, so copy to pagelist
3858 struct_len = 2 * sizeof(u32) +
3859 (num_fcntl_locks + num_flock_locks) *
3860 sizeof(struct ceph_filelock);
3861 rec.v2.flock_len = cpu_to_le32(struct_len);
3863 struct_len += sizeof(u32) + pathlen + sizeof(rec.v2);
3866 struct_len += sizeof(u64); /* snap_follows */
3868 total_len += struct_len;
3870 if (pagelist->length + total_len > RECONNECT_MAX_SIZE) {
3871 err = send_reconnect_partial(recon_state);
3873 goto out_freeflocks;
3874 pagelist = recon_state->pagelist;
3877 err = ceph_pagelist_reserve(pagelist, total_len);
3879 goto out_freeflocks;
3881 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
3882 if (recon_state->msg_version >= 3) {
3883 ceph_pagelist_encode_8(pagelist, struct_v);
3884 ceph_pagelist_encode_8(pagelist, 1);
3885 ceph_pagelist_encode_32(pagelist, struct_len);
3887 ceph_pagelist_encode_string(pagelist, path, pathlen);
3888 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3889 ceph_locks_to_pagelist(flocks, pagelist,
3890 num_fcntl_locks, num_flock_locks);
3892 ceph_pagelist_encode_64(pagelist, snap_follows);
3896 err = ceph_pagelist_reserve(pagelist,
3897 sizeof(u64) + sizeof(u32) +
3898 pathlen + sizeof(rec.v1));
3902 ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
3903 ceph_pagelist_encode_string(pagelist, path, pathlen);
3904 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
3908 ceph_mdsc_free_path(path, pathlen);
3910 recon_state->nr_caps++;
3914 static int encode_snap_realms(struct ceph_mds_client *mdsc,
3915 struct ceph_reconnect_state *recon_state)
3918 struct ceph_pagelist *pagelist = recon_state->pagelist;
3921 if (recon_state->msg_version >= 4) {
3922 err = ceph_pagelist_encode_32(pagelist, mdsc->num_snap_realms);
3928 * snaprealms. we provide mds with the ino, seq (version), and
3929 * parent for all of our realms. If the mds has any newer info,
3932 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3933 struct ceph_snap_realm *realm =
3934 rb_entry(p, struct ceph_snap_realm, node);
3935 struct ceph_mds_snaprealm_reconnect sr_rec;
3937 if (recon_state->msg_version >= 4) {
3938 size_t need = sizeof(u8) * 2 + sizeof(u32) +
3941 if (pagelist->length + need > RECONNECT_MAX_SIZE) {
3942 err = send_reconnect_partial(recon_state);
3945 pagelist = recon_state->pagelist;
3948 err = ceph_pagelist_reserve(pagelist, need);
3952 ceph_pagelist_encode_8(pagelist, 1);
3953 ceph_pagelist_encode_8(pagelist, 1);
3954 ceph_pagelist_encode_32(pagelist, sizeof(sr_rec));
3957 dout(" adding snap realm %llx seq %lld parent %llx\n",
3958 realm->ino, realm->seq, realm->parent_ino);
3959 sr_rec.ino = cpu_to_le64(realm->ino);
3960 sr_rec.seq = cpu_to_le64(realm->seq);
3961 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3963 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3967 recon_state->nr_realms++;
3975 * If an MDS fails and recovers, clients need to reconnect in order to
3976 * reestablish shared state. This includes all caps issued through
3977 * this session _and_ the snap_realm hierarchy. Because it's not
3978 * clear which snap realms the mds cares about, we send everything we
3979 * know about.. that ensures we'll then get any new info the
3980 * recovering MDS might have.
3982 * This is a relatively heavyweight operation, but it's rare.
3984 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3985 struct ceph_mds_session *session)
3987 struct ceph_msg *reply;
3988 int mds = session->s_mds;
3990 struct ceph_reconnect_state recon_state = {
3995 pr_info("mds%d reconnect start\n", mds);
3997 recon_state.pagelist = ceph_pagelist_alloc(GFP_NOFS);
3998 if (!recon_state.pagelist)
3999 goto fail_nopagelist;
4001 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
4005 xa_destroy(&session->s_delegated_inos);
4007 mutex_lock(&session->s_mutex);
4008 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
4011 dout("session %p state %s\n", session,
4012 ceph_session_state_name(session->s_state));
4014 spin_lock(&session->s_gen_ttl_lock);
4015 session->s_cap_gen++;
4016 spin_unlock(&session->s_gen_ttl_lock);
4018 spin_lock(&session->s_cap_lock);
4019 /* don't know if session is readonly */
4020 session->s_readonly = 0;
4022 * notify __ceph_remove_cap() that we are composing cap reconnect.
4023 * If a cap get released before being added to the cap reconnect,
4024 * __ceph_remove_cap() should skip queuing cap release.
4026 session->s_cap_reconnect = 1;
4027 /* drop old cap expires; we're about to reestablish that state */
4028 detach_cap_releases(session, &dispose);
4029 spin_unlock(&session->s_cap_lock);
4030 dispose_cap_releases(mdsc, &dispose);
4032 /* trim unused caps to reduce MDS's cache rejoin time */
4033 if (mdsc->fsc->sb->s_root)
4034 shrink_dcache_parent(mdsc->fsc->sb->s_root);
4036 ceph_con_close(&session->s_con);
4037 ceph_con_open(&session->s_con,
4038 CEPH_ENTITY_TYPE_MDS, mds,
4039 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
4041 /* replay unsafe requests */
4042 replay_unsafe_requests(mdsc, session);
4044 ceph_early_kick_flushing_caps(mdsc, session);
4046 down_read(&mdsc->snap_rwsem);
4048 /* placeholder for nr_caps */
4049 err = ceph_pagelist_encode_32(recon_state.pagelist, 0);
4053 if (test_bit(CEPHFS_FEATURE_MULTI_RECONNECT, &session->s_features)) {
4054 recon_state.msg_version = 3;
4055 recon_state.allow_multi = true;
4056 } else if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) {
4057 recon_state.msg_version = 3;
4059 recon_state.msg_version = 2;
4061 /* trsaverse this session's caps */
4062 err = ceph_iterate_session_caps(session, reconnect_caps_cb, &recon_state);
4064 spin_lock(&session->s_cap_lock);
4065 session->s_cap_reconnect = 0;
4066 spin_unlock(&session->s_cap_lock);
4071 /* check if all realms can be encoded into current message */
4072 if (mdsc->num_snap_realms) {
4074 recon_state.pagelist->length +
4075 mdsc->num_snap_realms *
4076 sizeof(struct ceph_mds_snaprealm_reconnect);
4077 if (recon_state.msg_version >= 4) {
4078 /* number of realms */
4079 total_len += sizeof(u32);
4080 /* version, compat_version and struct_len */
4081 total_len += mdsc->num_snap_realms *
4082 (2 * sizeof(u8) + sizeof(u32));
4084 if (total_len > RECONNECT_MAX_SIZE) {
4085 if (!recon_state.allow_multi) {
4089 if (recon_state.nr_caps) {
4090 err = send_reconnect_partial(&recon_state);
4094 recon_state.msg_version = 5;
4098 err = encode_snap_realms(mdsc, &recon_state);
4102 if (recon_state.msg_version >= 5) {
4103 err = ceph_pagelist_encode_8(recon_state.pagelist, 0);
4108 if (recon_state.nr_caps || recon_state.nr_realms) {
4110 list_first_entry(&recon_state.pagelist->head,
4112 __le32 *addr = kmap_atomic(page);
4113 if (recon_state.nr_caps) {
4114 WARN_ON(recon_state.nr_realms != mdsc->num_snap_realms);
4115 *addr = cpu_to_le32(recon_state.nr_caps);
4116 } else if (recon_state.msg_version >= 4) {
4117 *(addr + 1) = cpu_to_le32(recon_state.nr_realms);
4119 kunmap_atomic(addr);
4122 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
4123 if (recon_state.msg_version >= 4)
4124 reply->hdr.compat_version = cpu_to_le16(4);
4126 reply->hdr.data_len = cpu_to_le32(recon_state.pagelist->length);
4127 ceph_msg_data_add_pagelist(reply, recon_state.pagelist);
4129 ceph_con_send(&session->s_con, reply);
4131 mutex_unlock(&session->s_mutex);
4133 mutex_lock(&mdsc->mutex);
4134 __wake_requests(mdsc, &session->s_waiting);
4135 mutex_unlock(&mdsc->mutex);
4137 up_read(&mdsc->snap_rwsem);
4138 ceph_pagelist_release(recon_state.pagelist);
4142 ceph_msg_put(reply);
4143 up_read(&mdsc->snap_rwsem);
4144 mutex_unlock(&session->s_mutex);
4146 ceph_pagelist_release(recon_state.pagelist);
4148 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
4154 * compare old and new mdsmaps, kicking requests
4155 * and closing out old connections as necessary
4157 * called under mdsc->mutex.
4159 static void check_new_map(struct ceph_mds_client *mdsc,
4160 struct ceph_mdsmap *newmap,
4161 struct ceph_mdsmap *oldmap)
4164 int oldstate, newstate;
4165 struct ceph_mds_session *s;
4167 dout("check_new_map new %u old %u\n",
4168 newmap->m_epoch, oldmap->m_epoch);
4170 for (i = 0; i < oldmap->possible_max_rank && i < mdsc->max_sessions; i++) {
4171 if (!mdsc->sessions[i])
4173 s = mdsc->sessions[i];
4174 oldstate = ceph_mdsmap_get_state(oldmap, i);
4175 newstate = ceph_mdsmap_get_state(newmap, i);
4177 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
4178 i, ceph_mds_state_name(oldstate),
4179 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
4180 ceph_mds_state_name(newstate),
4181 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
4182 ceph_session_state_name(s->s_state));
4184 if (i >= newmap->possible_max_rank) {
4185 /* force close session for stopped mds */
4186 ceph_get_mds_session(s);
4187 __unregister_session(mdsc, s);
4188 __wake_requests(mdsc, &s->s_waiting);
4189 mutex_unlock(&mdsc->mutex);
4191 mutex_lock(&s->s_mutex);
4192 cleanup_session_requests(mdsc, s);
4193 remove_session_caps(s);
4194 mutex_unlock(&s->s_mutex);
4196 ceph_put_mds_session(s);
4198 mutex_lock(&mdsc->mutex);
4199 kick_requests(mdsc, i);
4203 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
4204 ceph_mdsmap_get_addr(newmap, i),
4205 sizeof(struct ceph_entity_addr))) {
4207 mutex_unlock(&mdsc->mutex);
4208 mutex_lock(&s->s_mutex);
4209 mutex_lock(&mdsc->mutex);
4210 ceph_con_close(&s->s_con);
4211 mutex_unlock(&s->s_mutex);
4212 s->s_state = CEPH_MDS_SESSION_RESTARTING;
4213 } else if (oldstate == newstate) {
4214 continue; /* nothing new with this mds */
4220 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
4221 newstate >= CEPH_MDS_STATE_RECONNECT) {
4222 mutex_unlock(&mdsc->mutex);
4223 send_mds_reconnect(mdsc, s);
4224 mutex_lock(&mdsc->mutex);
4228 * kick request on any mds that has gone active.
4230 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
4231 newstate >= CEPH_MDS_STATE_ACTIVE) {
4232 if (oldstate != CEPH_MDS_STATE_CREATING &&
4233 oldstate != CEPH_MDS_STATE_STARTING)
4234 pr_info("mds%d recovery completed\n", s->s_mds);
4235 kick_requests(mdsc, i);
4236 mutex_unlock(&mdsc->mutex);
4237 mutex_lock(&s->s_mutex);
4238 mutex_lock(&mdsc->mutex);
4239 ceph_kick_flushing_caps(mdsc, s);
4240 mutex_unlock(&s->s_mutex);
4241 wake_up_session_caps(s, RECONNECT);
4245 for (i = 0; i < newmap->possible_max_rank && i < mdsc->max_sessions; i++) {
4246 s = mdsc->sessions[i];
4249 if (!ceph_mdsmap_is_laggy(newmap, i))
4251 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4252 s->s_state == CEPH_MDS_SESSION_HUNG ||
4253 s->s_state == CEPH_MDS_SESSION_CLOSING) {
4254 dout(" connecting to export targets of laggy mds%d\n",
4256 __open_export_target_sessions(mdsc, s);
4268 * caller must hold session s_mutex, dentry->d_lock
4270 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
4272 struct ceph_dentry_info *di = ceph_dentry(dentry);
4274 ceph_put_mds_session(di->lease_session);
4275 di->lease_session = NULL;
4278 static void handle_lease(struct ceph_mds_client *mdsc,
4279 struct ceph_mds_session *session,
4280 struct ceph_msg *msg)
4282 struct super_block *sb = mdsc->fsc->sb;
4283 struct inode *inode;
4284 struct dentry *parent, *dentry;
4285 struct ceph_dentry_info *di;
4286 int mds = session->s_mds;
4287 struct ceph_mds_lease *h = msg->front.iov_base;
4289 struct ceph_vino vino;
4293 dout("handle_lease from mds%d\n", mds);
4296 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
4298 vino.ino = le64_to_cpu(h->ino);
4299 vino.snap = CEPH_NOSNAP;
4300 seq = le32_to_cpu(h->seq);
4301 dname.len = get_unaligned_le32(h + 1);
4302 if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
4304 dname.name = (void *)(h + 1) + sizeof(u32);
4307 inode = ceph_find_inode(sb, vino);
4308 dout("handle_lease %s, ino %llx %p %.*s\n",
4309 ceph_lease_op_name(h->action), vino.ino, inode,
4310 dname.len, dname.name);
4312 mutex_lock(&session->s_mutex);
4313 inc_session_sequence(session);
4316 dout("handle_lease no inode %llx\n", vino.ino);
4321 parent = d_find_alias(inode);
4323 dout("no parent dentry on inode %p\n", inode);
4325 goto release; /* hrm... */
4327 dname.hash = full_name_hash(parent, dname.name, dname.len);
4328 dentry = d_lookup(parent, &dname);
4333 spin_lock(&dentry->d_lock);
4334 di = ceph_dentry(dentry);
4335 switch (h->action) {
4336 case CEPH_MDS_LEASE_REVOKE:
4337 if (di->lease_session == session) {
4338 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
4339 h->seq = cpu_to_le32(di->lease_seq);
4340 __ceph_mdsc_drop_dentry_lease(dentry);
4345 case CEPH_MDS_LEASE_RENEW:
4346 if (di->lease_session == session &&
4347 di->lease_gen == session->s_cap_gen &&
4348 di->lease_renew_from &&
4349 di->lease_renew_after == 0) {
4350 unsigned long duration =
4351 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
4353 di->lease_seq = seq;
4354 di->time = di->lease_renew_from + duration;
4355 di->lease_renew_after = di->lease_renew_from +
4357 di->lease_renew_from = 0;
4361 spin_unlock(&dentry->d_lock);
4368 /* let's just reuse the same message */
4369 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
4371 ceph_con_send(&session->s_con, msg);
4374 mutex_unlock(&session->s_mutex);
4375 /* avoid calling iput_final() in mds dispatch threads */
4376 ceph_async_iput(inode);
4380 pr_err("corrupt lease message\n");
4384 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
4385 struct dentry *dentry, char action,
4388 struct ceph_msg *msg;
4389 struct ceph_mds_lease *lease;
4391 int len = sizeof(*lease) + sizeof(u32) + NAME_MAX;
4393 dout("lease_send_msg identry %p %s to mds%d\n",
4394 dentry, ceph_lease_op_name(action), session->s_mds);
4396 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
4399 lease = msg->front.iov_base;
4400 lease->action = action;
4401 lease->seq = cpu_to_le32(seq);
4403 spin_lock(&dentry->d_lock);
4404 dir = d_inode(dentry->d_parent);
4405 lease->ino = cpu_to_le64(ceph_ino(dir));
4406 lease->first = lease->last = cpu_to_le64(ceph_snap(dir));
4408 put_unaligned_le32(dentry->d_name.len, lease + 1);
4409 memcpy((void *)(lease + 1) + 4,
4410 dentry->d_name.name, dentry->d_name.len);
4411 spin_unlock(&dentry->d_lock);
4413 * if this is a preemptive lease RELEASE, no need to
4414 * flush request stream, since the actual request will
4417 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
4419 ceph_con_send(&session->s_con, msg);
4423 * lock unlock sessions, to wait ongoing session activities
4425 static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
4429 mutex_lock(&mdsc->mutex);
4430 for (i = 0; i < mdsc->max_sessions; i++) {
4431 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
4434 mutex_unlock(&mdsc->mutex);
4435 mutex_lock(&s->s_mutex);
4436 mutex_unlock(&s->s_mutex);
4437 ceph_put_mds_session(s);
4438 mutex_lock(&mdsc->mutex);
4440 mutex_unlock(&mdsc->mutex);
4443 static void maybe_recover_session(struct ceph_mds_client *mdsc)
4445 struct ceph_fs_client *fsc = mdsc->fsc;
4447 if (!ceph_test_mount_opt(fsc, CLEANRECOVER))
4450 if (READ_ONCE(fsc->mount_state) != CEPH_MOUNT_MOUNTED)
4453 if (!READ_ONCE(fsc->blocklisted))
4456 pr_info("auto reconnect after blocklisted\n");
4457 ceph_force_reconnect(fsc->sb);
4460 bool check_session_state(struct ceph_mds_session *s)
4462 switch (s->s_state) {
4463 case CEPH_MDS_SESSION_OPEN:
4464 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
4465 s->s_state = CEPH_MDS_SESSION_HUNG;
4466 pr_info("mds%d hung\n", s->s_mds);
4469 case CEPH_MDS_SESSION_CLOSING:
4470 /* Should never reach this when we're unmounting */
4473 case CEPH_MDS_SESSION_NEW:
4474 case CEPH_MDS_SESSION_RESTARTING:
4475 case CEPH_MDS_SESSION_CLOSED:
4476 case CEPH_MDS_SESSION_REJECTED:
4484 * If the sequence is incremented while we're waiting on a REQUEST_CLOSE reply,
4485 * then we need to retransmit that request.
4487 void inc_session_sequence(struct ceph_mds_session *s)
4489 lockdep_assert_held(&s->s_mutex);
4493 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
4496 dout("resending session close request for mds%d\n", s->s_mds);
4497 ret = request_close_session(s);
4499 pr_err("unable to close session to mds%d: %d\n",
4505 * delayed work -- periodically trim expired leases, renew caps with mds
4507 static void schedule_delayed(struct ceph_mds_client *mdsc)
4510 unsigned hz = round_jiffies_relative(HZ * delay);
4511 schedule_delayed_work(&mdsc->delayed_work, hz);
4514 static void delayed_work(struct work_struct *work)
4517 struct ceph_mds_client *mdsc =
4518 container_of(work, struct ceph_mds_client, delayed_work.work);
4522 dout("mdsc delayed_work\n");
4527 mutex_lock(&mdsc->mutex);
4528 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
4529 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
4530 mdsc->last_renew_caps);
4532 mdsc->last_renew_caps = jiffies;
4534 for (i = 0; i < mdsc->max_sessions; i++) {
4535 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
4539 if (!check_session_state(s)) {
4540 ceph_put_mds_session(s);
4543 mutex_unlock(&mdsc->mutex);
4545 mutex_lock(&s->s_mutex);
4547 send_renew_caps(mdsc, s);
4549 ceph_con_keepalive(&s->s_con);
4550 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
4551 s->s_state == CEPH_MDS_SESSION_HUNG)
4552 ceph_send_cap_releases(mdsc, s);
4553 mutex_unlock(&s->s_mutex);
4554 ceph_put_mds_session(s);
4556 mutex_lock(&mdsc->mutex);
4558 mutex_unlock(&mdsc->mutex);
4560 ceph_check_delayed_caps(mdsc);
4562 ceph_queue_cap_reclaim_work(mdsc);
4564 ceph_trim_snapid_map(mdsc);
4566 maybe_recover_session(mdsc);
4568 schedule_delayed(mdsc);
4571 int ceph_mdsc_init(struct ceph_fs_client *fsc)
4574 struct ceph_mds_client *mdsc;
4577 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
4581 mutex_init(&mdsc->mutex);
4582 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
4583 if (!mdsc->mdsmap) {
4588 init_completion(&mdsc->safe_umount_waiters);
4589 init_waitqueue_head(&mdsc->session_close_wq);
4590 INIT_LIST_HEAD(&mdsc->waiting_for_map);
4591 mdsc->sessions = NULL;
4592 atomic_set(&mdsc->num_sessions, 0);
4593 mdsc->max_sessions = 0;
4595 atomic64_set(&mdsc->quotarealms_count, 0);
4596 mdsc->quotarealms_inodes = RB_ROOT;
4597 mutex_init(&mdsc->quotarealms_inodes_mutex);
4598 mdsc->last_snap_seq = 0;
4599 init_rwsem(&mdsc->snap_rwsem);
4600 mdsc->snap_realms = RB_ROOT;
4601 INIT_LIST_HEAD(&mdsc->snap_empty);
4602 mdsc->num_snap_realms = 0;
4603 spin_lock_init(&mdsc->snap_empty_lock);
4605 mdsc->oldest_tid = 0;
4606 mdsc->request_tree = RB_ROOT;
4607 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
4608 mdsc->last_renew_caps = jiffies;
4609 INIT_LIST_HEAD(&mdsc->cap_delay_list);
4610 INIT_LIST_HEAD(&mdsc->cap_wait_list);
4611 spin_lock_init(&mdsc->cap_delay_lock);
4612 INIT_LIST_HEAD(&mdsc->snap_flush_list);
4613 spin_lock_init(&mdsc->snap_flush_lock);
4614 mdsc->last_cap_flush_tid = 1;
4615 INIT_LIST_HEAD(&mdsc->cap_flush_list);
4616 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
4617 mdsc->num_cap_flushing = 0;
4618 spin_lock_init(&mdsc->cap_dirty_lock);
4619 init_waitqueue_head(&mdsc->cap_flushing_wq);
4620 INIT_WORK(&mdsc->cap_reclaim_work, ceph_cap_reclaim_work);
4621 atomic_set(&mdsc->cap_reclaim_pending, 0);
4622 err = ceph_metric_init(&mdsc->metric);
4626 spin_lock_init(&mdsc->dentry_list_lock);
4627 INIT_LIST_HEAD(&mdsc->dentry_leases);
4628 INIT_LIST_HEAD(&mdsc->dentry_dir_leases);
4630 ceph_caps_init(mdsc);
4631 ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
4633 spin_lock_init(&mdsc->snapid_map_lock);
4634 mdsc->snapid_map_tree = RB_ROOT;
4635 INIT_LIST_HEAD(&mdsc->snapid_map_lru);
4637 init_rwsem(&mdsc->pool_perm_rwsem);
4638 mdsc->pool_perm_tree = RB_ROOT;
4640 strscpy(mdsc->nodename, utsname()->nodename,
4641 sizeof(mdsc->nodename));
4647 kfree(mdsc->mdsmap);
4654 * Wait for safe replies on open mds requests. If we time out, drop
4655 * all requests from the tree to avoid dangling dentry refs.
4657 static void wait_requests(struct ceph_mds_client *mdsc)
4659 struct ceph_options *opts = mdsc->fsc->client->options;
4660 struct ceph_mds_request *req;
4662 mutex_lock(&mdsc->mutex);
4663 if (__get_oldest_req(mdsc)) {
4664 mutex_unlock(&mdsc->mutex);
4666 dout("wait_requests waiting for requests\n");
4667 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
4668 ceph_timeout_jiffies(opts->mount_timeout));
4670 /* tear down remaining requests */
4671 mutex_lock(&mdsc->mutex);
4672 while ((req = __get_oldest_req(mdsc))) {
4673 dout("wait_requests timed out on tid %llu\n",
4675 list_del_init(&req->r_wait);
4676 __unregister_request(mdsc, req);
4679 mutex_unlock(&mdsc->mutex);
4680 dout("wait_requests done\n");
4684 * called before mount is ro, and before dentries are torn down.
4685 * (hmm, does this still race with new lookups?)
4687 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
4689 dout("pre_umount\n");
4692 lock_unlock_sessions(mdsc);
4693 ceph_flush_dirty_caps(mdsc);
4694 wait_requests(mdsc);
4697 * wait for reply handlers to drop their request refs and
4698 * their inode/dcache refs
4702 ceph_cleanup_quotarealms_inodes(mdsc);
4706 * wait for all write mds requests to flush.
4708 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
4710 struct ceph_mds_request *req = NULL, *nextreq;
4713 mutex_lock(&mdsc->mutex);
4714 dout("wait_unsafe_requests want %lld\n", want_tid);
4716 req = __get_oldest_req(mdsc);
4717 while (req && req->r_tid <= want_tid) {
4718 /* find next request */
4719 n = rb_next(&req->r_node);
4721 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
4724 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
4725 (req->r_op & CEPH_MDS_OP_WRITE)) {
4727 ceph_mdsc_get_request(req);
4729 ceph_mdsc_get_request(nextreq);
4730 mutex_unlock(&mdsc->mutex);
4731 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
4732 req->r_tid, want_tid);
4733 wait_for_completion(&req->r_safe_completion);
4734 mutex_lock(&mdsc->mutex);
4735 ceph_mdsc_put_request(req);
4737 break; /* next dne before, so we're done! */
4738 if (RB_EMPTY_NODE(&nextreq->r_node)) {
4739 /* next request was removed from tree */
4740 ceph_mdsc_put_request(nextreq);
4743 ceph_mdsc_put_request(nextreq); /* won't go away */
4747 mutex_unlock(&mdsc->mutex);
4748 dout("wait_unsafe_requests done\n");
4751 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
4753 u64 want_tid, want_flush;
4755 if (READ_ONCE(mdsc->fsc->mount_state) >= CEPH_MOUNT_SHUTDOWN)
4759 mutex_lock(&mdsc->mutex);
4760 want_tid = mdsc->last_tid;
4761 mutex_unlock(&mdsc->mutex);
4763 ceph_flush_dirty_caps(mdsc);
4764 spin_lock(&mdsc->cap_dirty_lock);
4765 want_flush = mdsc->last_cap_flush_tid;
4766 if (!list_empty(&mdsc->cap_flush_list)) {
4767 struct ceph_cap_flush *cf =
4768 list_last_entry(&mdsc->cap_flush_list,
4769 struct ceph_cap_flush, g_list);
4772 spin_unlock(&mdsc->cap_dirty_lock);
4774 dout("sync want tid %lld flush_seq %lld\n",
4775 want_tid, want_flush);
4777 wait_unsafe_requests(mdsc, want_tid);
4778 wait_caps_flush(mdsc, want_flush);
4782 * true if all sessions are closed, or we force unmount
4784 static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
4786 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
4788 return atomic_read(&mdsc->num_sessions) <= skipped;
4792 * called after sb is ro.
4794 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
4796 struct ceph_options *opts = mdsc->fsc->client->options;
4797 struct ceph_mds_session *session;
4801 dout("close_sessions\n");
4803 /* close sessions */
4804 mutex_lock(&mdsc->mutex);
4805 for (i = 0; i < mdsc->max_sessions; i++) {
4806 session = __ceph_lookup_mds_session(mdsc, i);
4809 mutex_unlock(&mdsc->mutex);
4810 mutex_lock(&session->s_mutex);
4811 if (__close_session(mdsc, session) <= 0)
4813 mutex_unlock(&session->s_mutex);
4814 ceph_put_mds_session(session);
4815 mutex_lock(&mdsc->mutex);
4817 mutex_unlock(&mdsc->mutex);
4819 dout("waiting for sessions to close\n");
4820 wait_event_timeout(mdsc->session_close_wq,
4821 done_closing_sessions(mdsc, skipped),
4822 ceph_timeout_jiffies(opts->mount_timeout));
4824 /* tear down remaining sessions */
4825 mutex_lock(&mdsc->mutex);
4826 for (i = 0; i < mdsc->max_sessions; i++) {
4827 if (mdsc->sessions[i]) {
4828 session = ceph_get_mds_session(mdsc->sessions[i]);
4829 __unregister_session(mdsc, session);
4830 mutex_unlock(&mdsc->mutex);
4831 mutex_lock(&session->s_mutex);
4832 remove_session_caps(session);
4833 mutex_unlock(&session->s_mutex);
4834 ceph_put_mds_session(session);
4835 mutex_lock(&mdsc->mutex);
4838 WARN_ON(!list_empty(&mdsc->cap_delay_list));
4839 mutex_unlock(&mdsc->mutex);
4841 ceph_cleanup_snapid_map(mdsc);
4842 ceph_cleanup_empty_realms(mdsc);
4844 cancel_work_sync(&mdsc->cap_reclaim_work);
4845 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
4850 void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
4852 struct ceph_mds_session *session;
4855 dout("force umount\n");
4857 mutex_lock(&mdsc->mutex);
4858 for (mds = 0; mds < mdsc->max_sessions; mds++) {
4859 session = __ceph_lookup_mds_session(mdsc, mds);
4863 if (session->s_state == CEPH_MDS_SESSION_REJECTED)
4864 __unregister_session(mdsc, session);
4865 __wake_requests(mdsc, &session->s_waiting);
4866 mutex_unlock(&mdsc->mutex);
4868 mutex_lock(&session->s_mutex);
4869 __close_session(mdsc, session);
4870 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
4871 cleanup_session_requests(mdsc, session);
4872 remove_session_caps(session);
4874 mutex_unlock(&session->s_mutex);
4875 ceph_put_mds_session(session);
4877 mutex_lock(&mdsc->mutex);
4878 kick_requests(mdsc, mds);
4880 __wake_requests(mdsc, &mdsc->waiting_for_map);
4881 mutex_unlock(&mdsc->mutex);
4884 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
4888 * Make sure the delayed work stopped before releasing
4891 * Because the cancel_delayed_work_sync() will only
4892 * guarantee that the work finishes executing. But the
4893 * delayed work will re-arm itself again after that.
4895 flush_delayed_work(&mdsc->delayed_work);
4898 ceph_mdsmap_destroy(mdsc->mdsmap);
4899 kfree(mdsc->sessions);
4900 ceph_caps_finalize(mdsc);
4901 ceph_pool_perm_destroy(mdsc);
4904 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
4906 struct ceph_mds_client *mdsc = fsc->mdsc;
4907 dout("mdsc_destroy %p\n", mdsc);
4912 /* flush out any connection work with references to us */
4915 ceph_mdsc_stop(mdsc);
4917 ceph_metric_destroy(&mdsc->metric);
4919 flush_delayed_work(&mdsc->metric.delayed_work);
4922 dout("mdsc_destroy %p done\n", mdsc);
4925 void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
4927 struct ceph_fs_client *fsc = mdsc->fsc;
4928 const char *mds_namespace = fsc->mount_options->mds_namespace;
4929 void *p = msg->front.iov_base;
4930 void *end = p + msg->front.iov_len;
4933 u32 mount_fscid = (u32)-1;
4936 ceph_decode_need(&p, end, sizeof(u32), bad);
4937 epoch = ceph_decode_32(&p);
4939 dout("handle_fsmap epoch %u\n", epoch);
4941 /* struct_v, struct_cv, map_len, epoch, legacy_client_fscid */
4942 ceph_decode_skip_n(&p, end, 2 + sizeof(u32) * 3, bad);
4944 ceph_decode_32_safe(&p, end, num_fs, bad);
4945 while (num_fs-- > 0) {
4946 void *info_p, *info_end;
4950 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
4951 p += 2; // info_v, info_cv
4952 info_len = ceph_decode_32(&p);
4953 ceph_decode_need(&p, end, info_len, bad);
4955 info_end = p + info_len;
4958 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
4959 fscid = ceph_decode_32(&info_p);
4960 namelen = ceph_decode_32(&info_p);
4961 ceph_decode_need(&info_p, info_end, namelen, bad);
4963 if (mds_namespace &&
4964 strlen(mds_namespace) == namelen &&
4965 !strncmp(mds_namespace, (char *)info_p, namelen)) {
4966 mount_fscid = fscid;
4971 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
4972 if (mount_fscid != (u32)-1) {
4973 fsc->client->monc.fs_cluster_id = mount_fscid;
4974 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
4976 ceph_monc_renew_subs(&fsc->client->monc);
4984 pr_err("error decoding fsmap\n");
4986 mutex_lock(&mdsc->mutex);
4987 mdsc->mdsmap_err = err;
4988 __wake_requests(mdsc, &mdsc->waiting_for_map);
4989 mutex_unlock(&mdsc->mutex);
4993 * handle mds map update.
4995 void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
4999 void *p = msg->front.iov_base;
5000 void *end = p + msg->front.iov_len;
5001 struct ceph_mdsmap *newmap, *oldmap;
5002 struct ceph_fsid fsid;
5005 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
5006 ceph_decode_copy(&p, &fsid, sizeof(fsid));
5007 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
5009 epoch = ceph_decode_32(&p);
5010 maplen = ceph_decode_32(&p);
5011 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
5013 /* do we need it? */
5014 mutex_lock(&mdsc->mutex);
5015 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
5016 dout("handle_map epoch %u <= our %u\n",
5017 epoch, mdsc->mdsmap->m_epoch);
5018 mutex_unlock(&mdsc->mutex);
5022 newmap = ceph_mdsmap_decode(&p, end, ceph_msgr2(mdsc->fsc->client));
5023 if (IS_ERR(newmap)) {
5024 err = PTR_ERR(newmap);
5028 /* swap into place */
5030 oldmap = mdsc->mdsmap;
5031 mdsc->mdsmap = newmap;
5032 check_new_map(mdsc, newmap, oldmap);
5033 ceph_mdsmap_destroy(oldmap);
5035 mdsc->mdsmap = newmap; /* first mds map */
5037 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
5040 __wake_requests(mdsc, &mdsc->waiting_for_map);
5041 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
5042 mdsc->mdsmap->m_epoch);
5044 mutex_unlock(&mdsc->mutex);
5045 schedule_delayed(mdsc);
5049 mutex_unlock(&mdsc->mutex);
5051 pr_err("error decoding mdsmap %d\n", err);
5055 static struct ceph_connection *mds_get_con(struct ceph_connection *con)
5057 struct ceph_mds_session *s = con->private;
5059 if (ceph_get_mds_session(s))
5064 static void mds_put_con(struct ceph_connection *con)
5066 struct ceph_mds_session *s = con->private;
5068 ceph_put_mds_session(s);
5072 * if the client is unresponsive for long enough, the mds will kill
5073 * the session entirely.
5075 static void mds_peer_reset(struct ceph_connection *con)
5077 struct ceph_mds_session *s = con->private;
5078 struct ceph_mds_client *mdsc = s->s_mdsc;
5080 pr_warn("mds%d closed our session\n", s->s_mds);
5081 send_mds_reconnect(mdsc, s);
5084 static void mds_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
5086 struct ceph_mds_session *s = con->private;
5087 struct ceph_mds_client *mdsc = s->s_mdsc;
5088 int type = le16_to_cpu(msg->hdr.type);
5090 mutex_lock(&mdsc->mutex);
5091 if (__verify_registered_session(mdsc, s) < 0) {
5092 mutex_unlock(&mdsc->mutex);
5095 mutex_unlock(&mdsc->mutex);
5098 case CEPH_MSG_MDS_MAP:
5099 ceph_mdsc_handle_mdsmap(mdsc, msg);
5101 case CEPH_MSG_FS_MAP_USER:
5102 ceph_mdsc_handle_fsmap(mdsc, msg);
5104 case CEPH_MSG_CLIENT_SESSION:
5105 handle_session(s, msg);
5107 case CEPH_MSG_CLIENT_REPLY:
5108 handle_reply(s, msg);
5110 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
5111 handle_forward(mdsc, s, msg);
5113 case CEPH_MSG_CLIENT_CAPS:
5114 ceph_handle_caps(s, msg);
5116 case CEPH_MSG_CLIENT_SNAP:
5117 ceph_handle_snap(mdsc, s, msg);
5119 case CEPH_MSG_CLIENT_LEASE:
5120 handle_lease(mdsc, s, msg);
5122 case CEPH_MSG_CLIENT_QUOTA:
5123 ceph_handle_quota(mdsc, s, msg);
5127 pr_err("received unknown message type %d %s\n", type,
5128 ceph_msg_type_name(type));
5139 * Note: returned pointer is the address of a structure that's
5140 * managed separately. Caller must *not* attempt to free it.
5142 static struct ceph_auth_handshake *
5143 mds_get_authorizer(struct ceph_connection *con, int *proto, int force_new)
5145 struct ceph_mds_session *s = con->private;
5146 struct ceph_mds_client *mdsc = s->s_mdsc;
5147 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
5148 struct ceph_auth_handshake *auth = &s->s_auth;
5151 ret = __ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_MDS,
5152 force_new, proto, NULL, NULL);
5154 return ERR_PTR(ret);
5159 static int mds_add_authorizer_challenge(struct ceph_connection *con,
5160 void *challenge_buf, int challenge_buf_len)
5162 struct ceph_mds_session *s = con->private;
5163 struct ceph_mds_client *mdsc = s->s_mdsc;
5164 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
5166 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
5167 challenge_buf, challenge_buf_len);
5170 static int mds_verify_authorizer_reply(struct ceph_connection *con)
5172 struct ceph_mds_session *s = con->private;
5173 struct ceph_mds_client *mdsc = s->s_mdsc;
5174 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
5175 struct ceph_auth_handshake *auth = &s->s_auth;
5177 return ceph_auth_verify_authorizer_reply(ac, auth->authorizer,
5178 auth->authorizer_reply_buf, auth->authorizer_reply_buf_len,
5179 NULL, NULL, NULL, NULL);
5182 static int mds_invalidate_authorizer(struct ceph_connection *con)
5184 struct ceph_mds_session *s = con->private;
5185 struct ceph_mds_client *mdsc = s->s_mdsc;
5186 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
5188 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
5190 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
5193 static int mds_get_auth_request(struct ceph_connection *con,
5194 void *buf, int *buf_len,
5195 void **authorizer, int *authorizer_len)
5197 struct ceph_mds_session *s = con->private;
5198 struct ceph_auth_client *ac = s->s_mdsc->fsc->client->monc.auth;
5199 struct ceph_auth_handshake *auth = &s->s_auth;
5202 ret = ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_MDS,
5207 *authorizer = auth->authorizer_buf;
5208 *authorizer_len = auth->authorizer_buf_len;
5212 static int mds_handle_auth_reply_more(struct ceph_connection *con,
5213 void *reply, int reply_len,
5214 void *buf, int *buf_len,
5215 void **authorizer, int *authorizer_len)
5217 struct ceph_mds_session *s = con->private;
5218 struct ceph_auth_client *ac = s->s_mdsc->fsc->client->monc.auth;
5219 struct ceph_auth_handshake *auth = &s->s_auth;
5222 ret = ceph_auth_handle_svc_reply_more(ac, auth, reply, reply_len,
5227 *authorizer = auth->authorizer_buf;
5228 *authorizer_len = auth->authorizer_buf_len;
5232 static int mds_handle_auth_done(struct ceph_connection *con,
5233 u64 global_id, void *reply, int reply_len,
5234 u8 *session_key, int *session_key_len,
5235 u8 *con_secret, int *con_secret_len)
5237 struct ceph_mds_session *s = con->private;
5238 struct ceph_auth_client *ac = s->s_mdsc->fsc->client->monc.auth;
5239 struct ceph_auth_handshake *auth = &s->s_auth;
5241 return ceph_auth_handle_svc_reply_done(ac, auth, reply, reply_len,
5242 session_key, session_key_len,
5243 con_secret, con_secret_len);
5246 static int mds_handle_auth_bad_method(struct ceph_connection *con,
5247 int used_proto, int result,
5248 const int *allowed_protos, int proto_cnt,
5249 const int *allowed_modes, int mode_cnt)
5251 struct ceph_mds_session *s = con->private;
5252 struct ceph_mon_client *monc = &s->s_mdsc->fsc->client->monc;
5255 if (ceph_auth_handle_bad_authorizer(monc->auth, CEPH_ENTITY_TYPE_MDS,
5257 allowed_protos, proto_cnt,
5258 allowed_modes, mode_cnt)) {
5259 ret = ceph_monc_validate_auth(monc);
5267 static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
5268 struct ceph_msg_header *hdr, int *skip)
5270 struct ceph_msg *msg;
5271 int type = (int) le16_to_cpu(hdr->type);
5272 int front_len = (int) le32_to_cpu(hdr->front_len);
5278 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
5280 pr_err("unable to allocate msg type %d len %d\n",
5288 static int mds_sign_message(struct ceph_msg *msg)
5290 struct ceph_mds_session *s = msg->con->private;
5291 struct ceph_auth_handshake *auth = &s->s_auth;
5293 return ceph_auth_sign_message(auth, msg);
5296 static int mds_check_message_signature(struct ceph_msg *msg)
5298 struct ceph_mds_session *s = msg->con->private;
5299 struct ceph_auth_handshake *auth = &s->s_auth;
5301 return ceph_auth_check_message_signature(auth, msg);
5304 static const struct ceph_connection_operations mds_con_ops = {
5307 .alloc_msg = mds_alloc_msg,
5308 .dispatch = mds_dispatch,
5309 .peer_reset = mds_peer_reset,
5310 .get_authorizer = mds_get_authorizer,
5311 .add_authorizer_challenge = mds_add_authorizer_challenge,
5312 .verify_authorizer_reply = mds_verify_authorizer_reply,
5313 .invalidate_authorizer = mds_invalidate_authorizer,
5314 .sign_message = mds_sign_message,
5315 .check_message_signature = mds_check_message_signature,
5316 .get_auth_request = mds_get_auth_request,
5317 .handle_auth_reply_more = mds_handle_auth_reply_more,
5318 .handle_auth_done = mds_handle_auth_done,
5319 .handle_auth_bad_method = mds_handle_auth_bad_method,