1 #include <linux/ceph/ceph_debug.h>
4 #include <linux/wait.h>
5 #include <linux/slab.h>
7 #include <linux/sched.h>
8 #include <linux/debugfs.h>
9 #include <linux/seq_file.h>
10 #include <linux/utsname.h>
13 #include "mds_client.h"
15 #include <linux/ceph/ceph_features.h>
16 #include <linux/ceph/messenger.h>
17 #include <linux/ceph/decode.h>
18 #include <linux/ceph/pagelist.h>
19 #include <linux/ceph/auth.h>
20 #include <linux/ceph/debugfs.h>
23 * A cluster of MDS (metadata server) daemons is responsible for
24 * managing the file system namespace (the directory hierarchy and
25 * inodes) and for coordinating shared access to storage. Metadata is
26 * partitioning hierarchically across a number of servers, and that
27 * partition varies over time as the cluster adjusts the distribution
28 * in order to balance load.
30 * The MDS client is primarily responsible to managing synchronous
31 * metadata requests for operations like open, unlink, and so forth.
32 * If there is a MDS failure, we find out about it when we (possibly
33 * request and) receive a new MDS map, and can resubmit affected
36 * For the most part, though, we take advantage of a lossless
37 * communications channel to the MDS, and do not need to worry about
38 * timing out or resubmitting requests.
40 * We maintain a stateful "session" with each MDS we interact with.
41 * Within each session, we sent periodic heartbeat messages to ensure
42 * any capabilities or leases we have been issues remain valid. If
43 * the session times out and goes stale, our leases and capabilities
44 * are no longer valid.
47 struct ceph_reconnect_state {
49 struct ceph_pagelist *pagelist;
53 static void __wake_requests(struct ceph_mds_client *mdsc,
54 struct list_head *head);
56 static const struct ceph_connection_operations mds_con_ops;
64 * parse individual inode info
66 static int parse_reply_info_in(void **p, void *end,
67 struct ceph_mds_reply_info_in *info,
73 *p += sizeof(struct ceph_mds_reply_inode) +
74 sizeof(*info->in->fragtree.splits) *
75 le32_to_cpu(info->in->fragtree.nsplits);
77 ceph_decode_32_safe(p, end, info->symlink_len, bad);
78 ceph_decode_need(p, end, info->symlink_len, bad);
80 *p += info->symlink_len;
82 if (features & CEPH_FEATURE_DIRLAYOUTHASH)
83 ceph_decode_copy_safe(p, end, &info->dir_layout,
84 sizeof(info->dir_layout), bad);
86 memset(&info->dir_layout, 0, sizeof(info->dir_layout));
88 ceph_decode_32_safe(p, end, info->xattr_len, bad);
89 ceph_decode_need(p, end, info->xattr_len, bad);
90 info->xattr_data = *p;
91 *p += info->xattr_len;
93 if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
94 ceph_decode_64_safe(p, end, info->inline_version, bad);
95 ceph_decode_32_safe(p, end, info->inline_len, bad);
96 ceph_decode_need(p, end, info->inline_len, bad);
97 info->inline_data = *p;
98 *p += info->inline_len;
100 info->inline_version = CEPH_INLINE_NONE;
108 * parse a normal reply, which may contain a (dir+)dentry and/or a
111 static int parse_reply_info_trace(void **p, void *end,
112 struct ceph_mds_reply_info_parsed *info,
117 if (info->head->is_dentry) {
118 err = parse_reply_info_in(p, end, &info->diri, features);
122 if (unlikely(*p + sizeof(*info->dirfrag) > end))
125 *p += sizeof(*info->dirfrag) +
126 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
127 if (unlikely(*p > end))
130 ceph_decode_32_safe(p, end, info->dname_len, bad);
131 ceph_decode_need(p, end, info->dname_len, bad);
133 *p += info->dname_len;
135 *p += sizeof(*info->dlease);
138 if (info->head->is_target) {
139 err = parse_reply_info_in(p, end, &info->targeti, features);
144 if (unlikely(*p != end))
151 pr_err("problem parsing mds trace %d\n", err);
156 * parse readdir results
158 static int parse_reply_info_dir(void **p, void *end,
159 struct ceph_mds_reply_info_parsed *info,
166 if (*p + sizeof(*info->dir_dir) > end)
168 *p += sizeof(*info->dir_dir) +
169 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
173 ceph_decode_need(p, end, sizeof(num) + 2, bad);
174 num = ceph_decode_32(p);
175 info->dir_end = ceph_decode_8(p);
176 info->dir_complete = ceph_decode_8(p);
180 BUG_ON(!info->dir_in);
181 info->dir_dname = (void *)(info->dir_in + num);
182 info->dir_dname_len = (void *)(info->dir_dname + num);
183 info->dir_dlease = (void *)(info->dir_dname_len + num);
184 if ((unsigned long)(info->dir_dlease + num) >
185 (unsigned long)info->dir_in + info->dir_buf_size) {
186 pr_err("dir contents are larger than expected\n");
194 ceph_decode_need(p, end, sizeof(u32)*2, bad);
195 info->dir_dname_len[i] = ceph_decode_32(p);
196 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
197 info->dir_dname[i] = *p;
198 *p += info->dir_dname_len[i];
199 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
201 info->dir_dlease[i] = *p;
202 *p += sizeof(struct ceph_mds_reply_lease);
205 err = parse_reply_info_in(p, end, &info->dir_in[i], features);
220 pr_err("problem parsing dir contents %d\n", err);
225 * parse fcntl F_GETLK results
227 static int parse_reply_info_filelock(void **p, void *end,
228 struct ceph_mds_reply_info_parsed *info,
231 if (*p + sizeof(*info->filelock_reply) > end)
234 info->filelock_reply = *p;
235 *p += sizeof(*info->filelock_reply);
237 if (unlikely(*p != end))
246 * parse create results
248 static int parse_reply_info_create(void **p, void *end,
249 struct ceph_mds_reply_info_parsed *info,
252 if (features & CEPH_FEATURE_REPLY_CREATE_INODE) {
254 info->has_create_ino = false;
256 info->has_create_ino = true;
257 info->ino = ceph_decode_64(p);
261 if (unlikely(*p != end))
270 * parse extra results
272 static int parse_reply_info_extra(void **p, void *end,
273 struct ceph_mds_reply_info_parsed *info,
276 if (info->head->op == CEPH_MDS_OP_GETFILELOCK)
277 return parse_reply_info_filelock(p, end, info, features);
278 else if (info->head->op == CEPH_MDS_OP_READDIR ||
279 info->head->op == CEPH_MDS_OP_LSSNAP)
280 return parse_reply_info_dir(p, end, info, features);
281 else if (info->head->op == CEPH_MDS_OP_CREATE)
282 return parse_reply_info_create(p, end, info, features);
288 * parse entire mds reply
290 static int parse_reply_info(struct ceph_msg *msg,
291 struct ceph_mds_reply_info_parsed *info,
298 info->head = msg->front.iov_base;
299 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
300 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
303 ceph_decode_32_safe(&p, end, len, bad);
305 ceph_decode_need(&p, end, len, bad);
306 err = parse_reply_info_trace(&p, p+len, info, features);
312 ceph_decode_32_safe(&p, end, len, bad);
314 ceph_decode_need(&p, end, len, bad);
315 err = parse_reply_info_extra(&p, p+len, info, features);
321 ceph_decode_32_safe(&p, end, len, bad);
322 info->snapblob_len = len;
333 pr_err("mds parse_reply err %d\n", err);
337 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
341 free_pages((unsigned long)info->dir_in, get_order(info->dir_buf_size));
348 const char *ceph_session_state_name(int s)
351 case CEPH_MDS_SESSION_NEW: return "new";
352 case CEPH_MDS_SESSION_OPENING: return "opening";
353 case CEPH_MDS_SESSION_OPEN: return "open";
354 case CEPH_MDS_SESSION_HUNG: return "hung";
355 case CEPH_MDS_SESSION_CLOSING: return "closing";
356 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
357 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
358 default: return "???";
362 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
364 if (atomic_inc_not_zero(&s->s_ref)) {
365 dout("mdsc get_session %p %d -> %d\n", s,
366 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
369 dout("mdsc get_session %p 0 -- FAIL", s);
374 void ceph_put_mds_session(struct ceph_mds_session *s)
376 dout("mdsc put_session %p %d -> %d\n", s,
377 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
378 if (atomic_dec_and_test(&s->s_ref)) {
379 if (s->s_auth.authorizer)
380 ceph_auth_destroy_authorizer(
381 s->s_mdsc->fsc->client->monc.auth,
382 s->s_auth.authorizer);
388 * called under mdsc->mutex
390 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
393 struct ceph_mds_session *session;
395 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
397 session = mdsc->sessions[mds];
398 dout("lookup_mds_session %p %d\n", session,
399 atomic_read(&session->s_ref));
400 get_session(session);
404 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
406 if (mds >= mdsc->max_sessions)
408 return mdsc->sessions[mds];
411 static int __verify_registered_session(struct ceph_mds_client *mdsc,
412 struct ceph_mds_session *s)
414 if (s->s_mds >= mdsc->max_sessions ||
415 mdsc->sessions[s->s_mds] != s)
421 * create+register a new session for given mds.
422 * called under mdsc->mutex.
424 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
427 struct ceph_mds_session *s;
429 if (mds >= mdsc->mdsmap->m_max_mds)
430 return ERR_PTR(-EINVAL);
432 s = kzalloc(sizeof(*s), GFP_NOFS);
434 return ERR_PTR(-ENOMEM);
437 s->s_state = CEPH_MDS_SESSION_NEW;
440 mutex_init(&s->s_mutex);
442 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
444 spin_lock_init(&s->s_gen_ttl_lock);
446 s->s_cap_ttl = jiffies - 1;
448 spin_lock_init(&s->s_cap_lock);
449 s->s_renew_requested = 0;
451 INIT_LIST_HEAD(&s->s_caps);
454 atomic_set(&s->s_ref, 1);
455 INIT_LIST_HEAD(&s->s_waiting);
456 INIT_LIST_HEAD(&s->s_unsafe);
457 s->s_num_cap_releases = 0;
458 s->s_cap_reconnect = 0;
459 s->s_cap_iterator = NULL;
460 INIT_LIST_HEAD(&s->s_cap_releases);
461 INIT_LIST_HEAD(&s->s_cap_releases_done);
462 INIT_LIST_HEAD(&s->s_cap_flushing);
463 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
465 dout("register_session mds%d\n", mds);
466 if (mds >= mdsc->max_sessions) {
467 int newmax = 1 << get_count_order(mds+1);
468 struct ceph_mds_session **sa;
470 dout("register_session realloc to %d\n", newmax);
471 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
474 if (mdsc->sessions) {
475 memcpy(sa, mdsc->sessions,
476 mdsc->max_sessions * sizeof(void *));
477 kfree(mdsc->sessions);
480 mdsc->max_sessions = newmax;
482 mdsc->sessions[mds] = s;
483 atomic_inc(&mdsc->num_sessions);
484 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
486 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
487 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
493 return ERR_PTR(-ENOMEM);
497 * called under mdsc->mutex
499 static void __unregister_session(struct ceph_mds_client *mdsc,
500 struct ceph_mds_session *s)
502 dout("__unregister_session mds%d %p\n", s->s_mds, s);
503 BUG_ON(mdsc->sessions[s->s_mds] != s);
504 mdsc->sessions[s->s_mds] = NULL;
505 ceph_con_close(&s->s_con);
506 ceph_put_mds_session(s);
507 atomic_dec(&mdsc->num_sessions);
511 * drop session refs in request.
513 * should be last request ref, or hold mdsc->mutex
515 static void put_request_session(struct ceph_mds_request *req)
517 if (req->r_session) {
518 ceph_put_mds_session(req->r_session);
519 req->r_session = NULL;
523 void ceph_mdsc_release_request(struct kref *kref)
525 struct ceph_mds_request *req = container_of(kref,
526 struct ceph_mds_request,
528 destroy_reply_info(&req->r_reply_info);
530 ceph_msg_put(req->r_request);
532 ceph_msg_put(req->r_reply);
534 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
537 if (req->r_locked_dir)
538 ceph_put_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
539 iput(req->r_target_inode);
542 if (req->r_old_dentry)
543 dput(req->r_old_dentry);
544 if (req->r_old_dentry_dir) {
546 * track (and drop pins for) r_old_dentry_dir
547 * separately, since r_old_dentry's d_parent may have
548 * changed between the dir mutex being dropped and
549 * this request being freed.
551 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
553 iput(req->r_old_dentry_dir);
558 ceph_pagelist_release(req->r_pagelist);
559 put_request_session(req);
560 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
565 * lookup session, bump ref if found.
567 * called under mdsc->mutex.
569 static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
572 struct ceph_mds_request *req;
573 struct rb_node *n = mdsc->request_tree.rb_node;
576 req = rb_entry(n, struct ceph_mds_request, r_node);
577 if (tid < req->r_tid)
579 else if (tid > req->r_tid)
582 ceph_mdsc_get_request(req);
589 static void __insert_request(struct ceph_mds_client *mdsc,
590 struct ceph_mds_request *new)
592 struct rb_node **p = &mdsc->request_tree.rb_node;
593 struct rb_node *parent = NULL;
594 struct ceph_mds_request *req = NULL;
598 req = rb_entry(parent, struct ceph_mds_request, r_node);
599 if (new->r_tid < req->r_tid)
601 else if (new->r_tid > req->r_tid)
607 rb_link_node(&new->r_node, parent, p);
608 rb_insert_color(&new->r_node, &mdsc->request_tree);
612 * Register an in-flight request, and assign a tid. Link to directory
613 * are modifying (if any).
615 * Called under mdsc->mutex.
617 static void __register_request(struct ceph_mds_client *mdsc,
618 struct ceph_mds_request *req,
621 req->r_tid = ++mdsc->last_tid;
623 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
625 dout("__register_request %p tid %lld\n", req, req->r_tid);
626 ceph_mdsc_get_request(req);
627 __insert_request(mdsc, req);
629 req->r_uid = current_fsuid();
630 req->r_gid = current_fsgid();
633 struct ceph_inode_info *ci = ceph_inode(dir);
636 spin_lock(&ci->i_unsafe_lock);
637 req->r_unsafe_dir = dir;
638 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
639 spin_unlock(&ci->i_unsafe_lock);
643 static void __unregister_request(struct ceph_mds_client *mdsc,
644 struct ceph_mds_request *req)
646 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
647 rb_erase(&req->r_node, &mdsc->request_tree);
648 RB_CLEAR_NODE(&req->r_node);
650 if (req->r_unsafe_dir) {
651 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
653 spin_lock(&ci->i_unsafe_lock);
654 list_del_init(&req->r_unsafe_dir_item);
655 spin_unlock(&ci->i_unsafe_lock);
657 iput(req->r_unsafe_dir);
658 req->r_unsafe_dir = NULL;
661 complete_all(&req->r_safe_completion);
663 ceph_mdsc_put_request(req);
667 * Choose mds to send request to next. If there is a hint set in the
668 * request (e.g., due to a prior forward hint from the mds), use that.
669 * Otherwise, consult frag tree and/or caps to identify the
670 * appropriate mds. If all else fails, choose randomly.
672 * Called under mdsc->mutex.
674 static struct dentry *get_nonsnap_parent(struct dentry *dentry)
677 * we don't need to worry about protecting the d_parent access
678 * here because we never renaming inside the snapped namespace
679 * except to resplice to another snapdir, and either the old or new
680 * result is a valid result.
682 while (!IS_ROOT(dentry) && ceph_snap(d_inode(dentry)) != CEPH_NOSNAP)
683 dentry = dentry->d_parent;
687 static int __choose_mds(struct ceph_mds_client *mdsc,
688 struct ceph_mds_request *req)
691 struct ceph_inode_info *ci;
692 struct ceph_cap *cap;
693 int mode = req->r_direct_mode;
695 u32 hash = req->r_direct_hash;
696 bool is_hash = req->r_direct_is_hash;
699 * is there a specific mds we should try? ignore hint if we have
700 * no session and the mds is not up (active or recovering).
702 if (req->r_resend_mds >= 0 &&
703 (__have_session(mdsc, req->r_resend_mds) ||
704 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
705 dout("choose_mds using resend_mds mds%d\n",
707 return req->r_resend_mds;
710 if (mode == USE_RANDOM_MDS)
715 inode = req->r_inode;
716 } else if (req->r_dentry) {
717 /* ignore race with rename; old or new d_parent is okay */
718 struct dentry *parent = req->r_dentry->d_parent;
719 struct inode *dir = d_inode(parent);
721 if (dir->i_sb != mdsc->fsc->sb) {
723 inode = d_inode(req->r_dentry);
724 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
725 /* direct snapped/virtual snapdir requests
726 * based on parent dir inode */
727 struct dentry *dn = get_nonsnap_parent(parent);
729 dout("__choose_mds using nonsnap parent %p\n", inode);
732 inode = d_inode(req->r_dentry);
733 if (!inode || mode == USE_AUTH_MDS) {
736 hash = ceph_dentry_hash(dir, req->r_dentry);
742 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
746 ci = ceph_inode(inode);
748 if (is_hash && S_ISDIR(inode->i_mode)) {
749 struct ceph_inode_frag frag;
752 ceph_choose_frag(ci, hash, &frag, &found);
754 if (mode == USE_ANY_MDS && frag.ndist > 0) {
757 /* choose a random replica */
758 get_random_bytes(&r, 1);
761 dout("choose_mds %p %llx.%llx "
762 "frag %u mds%d (%d/%d)\n",
763 inode, ceph_vinop(inode),
766 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
767 CEPH_MDS_STATE_ACTIVE)
771 /* since this file/dir wasn't known to be
772 * replicated, then we want to look for the
773 * authoritative mds. */
776 /* choose auth mds */
778 dout("choose_mds %p %llx.%llx "
779 "frag %u mds%d (auth)\n",
780 inode, ceph_vinop(inode), frag.frag, mds);
781 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
782 CEPH_MDS_STATE_ACTIVE)
788 spin_lock(&ci->i_ceph_lock);
790 if (mode == USE_AUTH_MDS)
791 cap = ci->i_auth_cap;
792 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
793 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
795 spin_unlock(&ci->i_ceph_lock);
798 mds = cap->session->s_mds;
799 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
800 inode, ceph_vinop(inode), mds,
801 cap == ci->i_auth_cap ? "auth " : "", cap);
802 spin_unlock(&ci->i_ceph_lock);
806 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
807 dout("choose_mds chose random mds%d\n", mds);
815 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
817 struct ceph_msg *msg;
818 struct ceph_mds_session_head *h;
820 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
823 pr_err("create_session_msg ENOMEM creating msg\n");
826 h = msg->front.iov_base;
827 h->op = cpu_to_le32(op);
828 h->seq = cpu_to_le64(seq);
834 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
835 * to include additional client metadata fields.
837 static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
839 struct ceph_msg *msg;
840 struct ceph_mds_session_head *h;
842 int metadata_bytes = 0;
843 int metadata_key_count = 0;
844 struct ceph_options *opt = mdsc->fsc->client->options;
847 const char* metadata[][2] = {
848 {"hostname", utsname()->nodename},
849 {"kernel_version", utsname()->release},
850 {"entity_id", opt->name ? opt->name : ""},
854 /* Calculate serialized length of metadata */
855 metadata_bytes = 4; /* map length */
856 for (i = 0; metadata[i][0] != NULL; ++i) {
857 metadata_bytes += 8 + strlen(metadata[i][0]) +
858 strlen(metadata[i][1]);
859 metadata_key_count++;
862 /* Allocate the message */
863 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + metadata_bytes,
866 pr_err("create_session_msg ENOMEM creating msg\n");
869 h = msg->front.iov_base;
870 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
871 h->seq = cpu_to_le64(seq);
874 * Serialize client metadata into waiting buffer space, using
875 * the format that userspace expects for map<string, string>
877 * ClientSession messages with metadata are v2
879 msg->hdr.version = cpu_to_le16(2);
880 msg->hdr.compat_version = cpu_to_le16(1);
882 /* The write pointer, following the session_head structure */
883 p = msg->front.iov_base + sizeof(*h);
885 /* Number of entries in the map */
886 ceph_encode_32(&p, metadata_key_count);
888 /* Two length-prefixed strings for each entry in the map */
889 for (i = 0; metadata[i][0] != NULL; ++i) {
890 size_t const key_len = strlen(metadata[i][0]);
891 size_t const val_len = strlen(metadata[i][1]);
893 ceph_encode_32(&p, key_len);
894 memcpy(p, metadata[i][0], key_len);
896 ceph_encode_32(&p, val_len);
897 memcpy(p, metadata[i][1], val_len);
905 * send session open request.
907 * called under mdsc->mutex
909 static int __open_session(struct ceph_mds_client *mdsc,
910 struct ceph_mds_session *session)
912 struct ceph_msg *msg;
914 int mds = session->s_mds;
916 /* wait for mds to go active? */
917 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
918 dout("open_session to mds%d (%s)\n", mds,
919 ceph_mds_state_name(mstate));
920 session->s_state = CEPH_MDS_SESSION_OPENING;
921 session->s_renew_requested = jiffies;
923 /* send connect message */
924 msg = create_session_open_msg(mdsc, session->s_seq);
927 ceph_con_send(&session->s_con, msg);
932 * open sessions for any export targets for the given mds
934 * called under mdsc->mutex
936 static struct ceph_mds_session *
937 __open_export_target_session(struct ceph_mds_client *mdsc, int target)
939 struct ceph_mds_session *session;
941 session = __ceph_lookup_mds_session(mdsc, target);
943 session = register_session(mdsc, target);
947 if (session->s_state == CEPH_MDS_SESSION_NEW ||
948 session->s_state == CEPH_MDS_SESSION_CLOSING)
949 __open_session(mdsc, session);
954 struct ceph_mds_session *
955 ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
957 struct ceph_mds_session *session;
959 dout("open_export_target_session to mds%d\n", target);
961 mutex_lock(&mdsc->mutex);
962 session = __open_export_target_session(mdsc, target);
963 mutex_unlock(&mdsc->mutex);
968 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
969 struct ceph_mds_session *session)
971 struct ceph_mds_info *mi;
972 struct ceph_mds_session *ts;
973 int i, mds = session->s_mds;
975 if (mds >= mdsc->mdsmap->m_max_mds)
978 mi = &mdsc->mdsmap->m_info[mds];
979 dout("open_export_target_sessions for mds%d (%d targets)\n",
980 session->s_mds, mi->num_export_targets);
982 for (i = 0; i < mi->num_export_targets; i++) {
983 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
985 ceph_put_mds_session(ts);
989 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
990 struct ceph_mds_session *session)
992 mutex_lock(&mdsc->mutex);
993 __open_export_target_sessions(mdsc, session);
994 mutex_unlock(&mdsc->mutex);
1002 * Free preallocated cap messages assigned to this session
1004 static void cleanup_cap_releases(struct ceph_mds_session *session)
1006 struct ceph_msg *msg;
1008 spin_lock(&session->s_cap_lock);
1009 while (!list_empty(&session->s_cap_releases)) {
1010 msg = list_first_entry(&session->s_cap_releases,
1011 struct ceph_msg, list_head);
1012 list_del_init(&msg->list_head);
1015 while (!list_empty(&session->s_cap_releases_done)) {
1016 msg = list_first_entry(&session->s_cap_releases_done,
1017 struct ceph_msg, list_head);
1018 list_del_init(&msg->list_head);
1021 spin_unlock(&session->s_cap_lock);
1024 static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1025 struct ceph_mds_session *session)
1027 struct ceph_mds_request *req;
1030 dout("cleanup_session_requests mds%d\n", session->s_mds);
1031 mutex_lock(&mdsc->mutex);
1032 while (!list_empty(&session->s_unsafe)) {
1033 req = list_first_entry(&session->s_unsafe,
1034 struct ceph_mds_request, r_unsafe_item);
1035 list_del_init(&req->r_unsafe_item);
1036 pr_info(" dropping unsafe request %llu\n", req->r_tid);
1037 __unregister_request(mdsc, req);
1039 /* zero r_attempts, so kick_requests() will re-send requests */
1040 p = rb_first(&mdsc->request_tree);
1042 req = rb_entry(p, struct ceph_mds_request, r_node);
1044 if (req->r_session &&
1045 req->r_session->s_mds == session->s_mds)
1046 req->r_attempts = 0;
1048 mutex_unlock(&mdsc->mutex);
1052 * Helper to safely iterate over all caps associated with a session, with
1053 * special care taken to handle a racing __ceph_remove_cap().
1055 * Caller must hold session s_mutex.
1057 static int iterate_session_caps(struct ceph_mds_session *session,
1058 int (*cb)(struct inode *, struct ceph_cap *,
1061 struct list_head *p;
1062 struct ceph_cap *cap;
1063 struct inode *inode, *last_inode = NULL;
1064 struct ceph_cap *old_cap = NULL;
1067 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1068 spin_lock(&session->s_cap_lock);
1069 p = session->s_caps.next;
1070 while (p != &session->s_caps) {
1071 cap = list_entry(p, struct ceph_cap, session_caps);
1072 inode = igrab(&cap->ci->vfs_inode);
1077 session->s_cap_iterator = cap;
1078 spin_unlock(&session->s_cap_lock);
1085 ceph_put_cap(session->s_mdsc, old_cap);
1089 ret = cb(inode, cap, arg);
1092 spin_lock(&session->s_cap_lock);
1094 if (cap->ci == NULL) {
1095 dout("iterate_session_caps finishing cap %p removal\n",
1097 BUG_ON(cap->session != session);
1098 list_del_init(&cap->session_caps);
1099 session->s_nr_caps--;
1100 cap->session = NULL;
1101 old_cap = cap; /* put_cap it w/o locks held */
1108 session->s_cap_iterator = NULL;
1109 spin_unlock(&session->s_cap_lock);
1113 ceph_put_cap(session->s_mdsc, old_cap);
1118 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
1121 struct ceph_inode_info *ci = ceph_inode(inode);
1124 dout("removing cap %p, ci is %p, inode is %p\n",
1125 cap, ci, &ci->vfs_inode);
1126 spin_lock(&ci->i_ceph_lock);
1127 __ceph_remove_cap(cap, false);
1128 if (!ci->i_auth_cap) {
1129 struct ceph_mds_client *mdsc =
1130 ceph_sb_to_client(inode->i_sb)->mdsc;
1132 spin_lock(&mdsc->cap_dirty_lock);
1133 if (!list_empty(&ci->i_dirty_item)) {
1134 pr_info(" dropping dirty %s state for %p %lld\n",
1135 ceph_cap_string(ci->i_dirty_caps),
1136 inode, ceph_ino(inode));
1137 ci->i_dirty_caps = 0;
1138 list_del_init(&ci->i_dirty_item);
1141 if (!list_empty(&ci->i_flushing_item)) {
1142 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
1143 ceph_cap_string(ci->i_flushing_caps),
1144 inode, ceph_ino(inode));
1145 ci->i_flushing_caps = 0;
1146 list_del_init(&ci->i_flushing_item);
1147 mdsc->num_cap_flushing--;
1150 spin_unlock(&mdsc->cap_dirty_lock);
1152 spin_unlock(&ci->i_ceph_lock);
1159 * caller must hold session s_mutex
1161 static void remove_session_caps(struct ceph_mds_session *session)
1163 dout("remove_session_caps on %p\n", session);
1164 iterate_session_caps(session, remove_session_caps_cb, NULL);
1166 spin_lock(&session->s_cap_lock);
1167 if (session->s_nr_caps > 0) {
1168 struct super_block *sb = session->s_mdsc->fsc->sb;
1169 struct inode *inode;
1170 struct ceph_cap *cap, *prev = NULL;
1171 struct ceph_vino vino;
1173 * iterate_session_caps() skips inodes that are being
1174 * deleted, we need to wait until deletions are complete.
1175 * __wait_on_freeing_inode() is designed for the job,
1176 * but it is not exported, so use lookup inode function
1179 while (!list_empty(&session->s_caps)) {
1180 cap = list_entry(session->s_caps.next,
1181 struct ceph_cap, session_caps);
1185 vino = cap->ci->i_vino;
1186 spin_unlock(&session->s_cap_lock);
1188 inode = ceph_find_inode(sb, vino);
1191 spin_lock(&session->s_cap_lock);
1194 spin_unlock(&session->s_cap_lock);
1196 BUG_ON(session->s_nr_caps > 0);
1197 BUG_ON(!list_empty(&session->s_cap_flushing));
1198 cleanup_cap_releases(session);
1202 * wake up any threads waiting on this session's caps. if the cap is
1203 * old (didn't get renewed on the client reconnect), remove it now.
1205 * caller must hold s_mutex.
1207 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1210 struct ceph_inode_info *ci = ceph_inode(inode);
1212 wake_up_all(&ci->i_cap_wq);
1214 spin_lock(&ci->i_ceph_lock);
1215 ci->i_wanted_max_size = 0;
1216 ci->i_requested_max_size = 0;
1217 spin_unlock(&ci->i_ceph_lock);
1222 static void wake_up_session_caps(struct ceph_mds_session *session,
1225 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1226 iterate_session_caps(session, wake_up_session_cb,
1227 (void *)(unsigned long)reconnect);
1231 * Send periodic message to MDS renewing all currently held caps. The
1232 * ack will reset the expiration for all caps from this session.
1234 * caller holds s_mutex
1236 static int send_renew_caps(struct ceph_mds_client *mdsc,
1237 struct ceph_mds_session *session)
1239 struct ceph_msg *msg;
1242 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1243 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1244 pr_info("mds%d caps stale\n", session->s_mds);
1245 session->s_renew_requested = jiffies;
1247 /* do not try to renew caps until a recovering mds has reconnected
1248 * with its clients. */
1249 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1250 if (state < CEPH_MDS_STATE_RECONNECT) {
1251 dout("send_renew_caps ignoring mds%d (%s)\n",
1252 session->s_mds, ceph_mds_state_name(state));
1256 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1257 ceph_mds_state_name(state));
1258 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1259 ++session->s_renew_seq);
1262 ceph_con_send(&session->s_con, msg);
1266 static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1267 struct ceph_mds_session *session, u64 seq)
1269 struct ceph_msg *msg;
1271 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1272 session->s_mds, ceph_session_state_name(session->s_state), seq);
1273 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1276 ceph_con_send(&session->s_con, msg);
1282 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1284 * Called under session->s_mutex
1286 static void renewed_caps(struct ceph_mds_client *mdsc,
1287 struct ceph_mds_session *session, int is_renew)
1292 spin_lock(&session->s_cap_lock);
1293 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
1295 session->s_cap_ttl = session->s_renew_requested +
1296 mdsc->mdsmap->m_session_timeout*HZ;
1299 if (time_before(jiffies, session->s_cap_ttl)) {
1300 pr_info("mds%d caps renewed\n", session->s_mds);
1303 pr_info("mds%d caps still stale\n", session->s_mds);
1306 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1307 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1308 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1309 spin_unlock(&session->s_cap_lock);
1312 wake_up_session_caps(session, 0);
1316 * send a session close request
1318 static int request_close_session(struct ceph_mds_client *mdsc,
1319 struct ceph_mds_session *session)
1321 struct ceph_msg *msg;
1323 dout("request_close_session mds%d state %s seq %lld\n",
1324 session->s_mds, ceph_session_state_name(session->s_state),
1326 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1329 ceph_con_send(&session->s_con, msg);
1334 * Called with s_mutex held.
1336 static int __close_session(struct ceph_mds_client *mdsc,
1337 struct ceph_mds_session *session)
1339 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1341 session->s_state = CEPH_MDS_SESSION_CLOSING;
1342 return request_close_session(mdsc, session);
1346 * Trim old(er) caps.
1348 * Because we can't cache an inode without one or more caps, we do
1349 * this indirectly: if a cap is unused, we prune its aliases, at which
1350 * point the inode will hopefully get dropped to.
1352 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1353 * memory pressure from the MDS, though, so it needn't be perfect.
1355 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1357 struct ceph_mds_session *session = arg;
1358 struct ceph_inode_info *ci = ceph_inode(inode);
1359 int used, wanted, oissued, mine;
1361 if (session->s_trim_caps <= 0)
1364 spin_lock(&ci->i_ceph_lock);
1365 mine = cap->issued | cap->implemented;
1366 used = __ceph_caps_used(ci);
1367 wanted = __ceph_caps_file_wanted(ci);
1368 oissued = __ceph_caps_issued_other(ci, cap);
1370 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1371 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1372 ceph_cap_string(used), ceph_cap_string(wanted));
1373 if (cap == ci->i_auth_cap) {
1374 if (ci->i_dirty_caps | ci->i_flushing_caps)
1376 if ((used | wanted) & CEPH_CAP_ANY_WR)
1379 if ((used | wanted) & ~oissued & mine)
1380 goto out; /* we need these caps */
1382 session->s_trim_caps--;
1384 /* we aren't the only cap.. just remove us */
1385 __ceph_remove_cap(cap, true);
1387 /* try to drop referring dentries */
1388 spin_unlock(&ci->i_ceph_lock);
1389 d_prune_aliases(inode);
1390 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1391 inode, cap, atomic_read(&inode->i_count));
1396 spin_unlock(&ci->i_ceph_lock);
1401 * Trim session cap count down to some max number.
1403 static int trim_caps(struct ceph_mds_client *mdsc,
1404 struct ceph_mds_session *session,
1407 int trim_caps = session->s_nr_caps - max_caps;
1409 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1410 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1411 if (trim_caps > 0) {
1412 session->s_trim_caps = trim_caps;
1413 iterate_session_caps(session, trim_caps_cb, session);
1414 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1415 session->s_mds, session->s_nr_caps, max_caps,
1416 trim_caps - session->s_trim_caps);
1417 session->s_trim_caps = 0;
1420 ceph_add_cap_releases(mdsc, session);
1421 ceph_send_cap_releases(mdsc, session);
1426 * Allocate cap_release messages. If there is a partially full message
1427 * in the queue, try to allocate enough to cover it's remainder, so that
1428 * we can send it immediately.
1430 * Called under s_mutex.
1432 int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
1433 struct ceph_mds_session *session)
1435 struct ceph_msg *msg, *partial = NULL;
1436 struct ceph_mds_cap_release *head;
1438 int extra = mdsc->fsc->mount_options->cap_release_safety;
1441 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1444 spin_lock(&session->s_cap_lock);
1446 if (!list_empty(&session->s_cap_releases)) {
1447 msg = list_first_entry(&session->s_cap_releases,
1450 head = msg->front.iov_base;
1451 num = le32_to_cpu(head->num);
1453 dout(" partial %p with (%d/%d)\n", msg, num,
1454 (int)CEPH_CAPS_PER_RELEASE);
1455 extra += CEPH_CAPS_PER_RELEASE - num;
1459 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1460 spin_unlock(&session->s_cap_lock);
1461 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1465 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1466 (int)msg->front.iov_len);
1467 head = msg->front.iov_base;
1468 head->num = cpu_to_le32(0);
1469 msg->front.iov_len = sizeof(*head);
1470 spin_lock(&session->s_cap_lock);
1471 list_add(&msg->list_head, &session->s_cap_releases);
1472 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1476 head = partial->front.iov_base;
1477 num = le32_to_cpu(head->num);
1478 dout(" queueing partial %p with %d/%d\n", partial, num,
1479 (int)CEPH_CAPS_PER_RELEASE);
1480 list_move_tail(&partial->list_head,
1481 &session->s_cap_releases_done);
1482 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
1485 spin_unlock(&session->s_cap_lock);
1490 static int check_cap_flush(struct inode *inode, u64 want_flush_seq)
1492 struct ceph_inode_info *ci = ceph_inode(inode);
1494 spin_lock(&ci->i_ceph_lock);
1495 if (ci->i_flushing_caps)
1496 ret = ci->i_cap_flush_seq >= want_flush_seq;
1499 spin_unlock(&ci->i_ceph_lock);
1504 * flush all dirty inode data to disk.
1506 * returns true if we've flushed through want_flush_seq
1508 static void wait_caps_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1512 dout("check_cap_flush want %lld\n", want_flush_seq);
1513 mutex_lock(&mdsc->mutex);
1514 for (mds = 0; mds < mdsc->max_sessions; mds++) {
1515 struct ceph_mds_session *session = mdsc->sessions[mds];
1516 struct inode *inode = NULL;
1520 get_session(session);
1521 mutex_unlock(&mdsc->mutex);
1523 mutex_lock(&session->s_mutex);
1524 if (!list_empty(&session->s_cap_flushing)) {
1525 struct ceph_inode_info *ci =
1526 list_entry(session->s_cap_flushing.next,
1527 struct ceph_inode_info,
1530 if (!check_cap_flush(&ci->vfs_inode, want_flush_seq)) {
1531 dout("check_cap_flush still flushing %p "
1532 "seq %lld <= %lld to mds%d\n",
1533 &ci->vfs_inode, ci->i_cap_flush_seq,
1534 want_flush_seq, session->s_mds);
1535 inode = igrab(&ci->vfs_inode);
1538 mutex_unlock(&session->s_mutex);
1539 ceph_put_mds_session(session);
1542 wait_event(mdsc->cap_flushing_wq,
1543 check_cap_flush(inode, want_flush_seq));
1547 mutex_lock(&mdsc->mutex);
1550 mutex_unlock(&mdsc->mutex);
1551 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1555 * called under s_mutex
1557 void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1558 struct ceph_mds_session *session)
1560 struct ceph_msg *msg;
1562 dout("send_cap_releases mds%d\n", session->s_mds);
1563 spin_lock(&session->s_cap_lock);
1564 while (!list_empty(&session->s_cap_releases_done)) {
1565 msg = list_first_entry(&session->s_cap_releases_done,
1566 struct ceph_msg, list_head);
1567 list_del_init(&msg->list_head);
1568 spin_unlock(&session->s_cap_lock);
1569 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1570 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1571 ceph_con_send(&session->s_con, msg);
1572 spin_lock(&session->s_cap_lock);
1574 spin_unlock(&session->s_cap_lock);
1577 static void discard_cap_releases(struct ceph_mds_client *mdsc,
1578 struct ceph_mds_session *session)
1580 struct ceph_msg *msg;
1581 struct ceph_mds_cap_release *head;
1584 dout("discard_cap_releases mds%d\n", session->s_mds);
1586 if (!list_empty(&session->s_cap_releases)) {
1587 /* zero out the in-progress message */
1588 msg = list_first_entry(&session->s_cap_releases,
1589 struct ceph_msg, list_head);
1590 head = msg->front.iov_base;
1591 num = le32_to_cpu(head->num);
1592 dout("discard_cap_releases mds%d %p %u\n",
1593 session->s_mds, msg, num);
1594 head->num = cpu_to_le32(0);
1595 msg->front.iov_len = sizeof(*head);
1596 session->s_num_cap_releases += num;
1599 /* requeue completed messages */
1600 while (!list_empty(&session->s_cap_releases_done)) {
1601 msg = list_first_entry(&session->s_cap_releases_done,
1602 struct ceph_msg, list_head);
1603 list_del_init(&msg->list_head);
1605 head = msg->front.iov_base;
1606 num = le32_to_cpu(head->num);
1607 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1609 session->s_num_cap_releases += num;
1610 head->num = cpu_to_le32(0);
1611 msg->front.iov_len = sizeof(*head);
1612 list_add(&msg->list_head, &session->s_cap_releases);
1620 int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1623 struct ceph_inode_info *ci = ceph_inode(dir);
1624 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1625 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
1626 size_t size = sizeof(*rinfo->dir_in) + sizeof(*rinfo->dir_dname_len) +
1627 sizeof(*rinfo->dir_dname) + sizeof(*rinfo->dir_dlease);
1628 int order, num_entries;
1630 spin_lock(&ci->i_ceph_lock);
1631 num_entries = ci->i_files + ci->i_subdirs;
1632 spin_unlock(&ci->i_ceph_lock);
1633 num_entries = max(num_entries, 1);
1634 num_entries = min(num_entries, opt->max_readdir);
1636 order = get_order(size * num_entries);
1637 while (order >= 0) {
1638 rinfo->dir_in = (void*)__get_free_pages(GFP_NOFS | __GFP_NOWARN,
1647 num_entries = (PAGE_SIZE << order) / size;
1648 num_entries = min(num_entries, opt->max_readdir);
1650 rinfo->dir_buf_size = PAGE_SIZE << order;
1651 req->r_num_caps = num_entries + 1;
1652 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1653 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1658 * Create an mds request.
1660 struct ceph_mds_request *
1661 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1663 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1666 return ERR_PTR(-ENOMEM);
1668 mutex_init(&req->r_fill_mutex);
1670 req->r_started = jiffies;
1671 req->r_resend_mds = -1;
1672 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1674 kref_init(&req->r_kref);
1675 INIT_LIST_HEAD(&req->r_wait);
1676 init_completion(&req->r_completion);
1677 init_completion(&req->r_safe_completion);
1678 INIT_LIST_HEAD(&req->r_unsafe_item);
1680 req->r_stamp = CURRENT_TIME;
1683 req->r_direct_mode = mode;
1688 * return oldest (lowest) request, tid in request tree, 0 if none.
1690 * called under mdsc->mutex.
1692 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1694 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1696 return rb_entry(rb_first(&mdsc->request_tree),
1697 struct ceph_mds_request, r_node);
1700 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1702 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1710 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1711 * on build_path_from_dentry in fs/cifs/dir.c.
1713 * If @stop_on_nosnap, generate path relative to the first non-snapped
1716 * Encode hidden .snap dirs as a double /, i.e.
1717 * foo/.snap/bar -> foo//bar
1719 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1722 struct dentry *temp;
1728 return ERR_PTR(-EINVAL);
1732 seq = read_seqbegin(&rename_lock);
1734 for (temp = dentry; !IS_ROOT(temp);) {
1735 struct inode *inode = d_inode(temp);
1736 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1737 len++; /* slash only */
1738 else if (stop_on_nosnap && inode &&
1739 ceph_snap(inode) == CEPH_NOSNAP)
1742 len += 1 + temp->d_name.len;
1743 temp = temp->d_parent;
1747 len--; /* no leading '/' */
1749 path = kmalloc(len+1, GFP_NOFS);
1751 return ERR_PTR(-ENOMEM);
1753 path[pos] = 0; /* trailing null */
1755 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1756 struct inode *inode;
1758 spin_lock(&temp->d_lock);
1759 inode = d_inode(temp);
1760 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1761 dout("build_path path+%d: %p SNAPDIR\n",
1763 } else if (stop_on_nosnap && inode &&
1764 ceph_snap(inode) == CEPH_NOSNAP) {
1765 spin_unlock(&temp->d_lock);
1768 pos -= temp->d_name.len;
1770 spin_unlock(&temp->d_lock);
1773 strncpy(path + pos, temp->d_name.name,
1776 spin_unlock(&temp->d_lock);
1779 temp = temp->d_parent;
1782 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
1783 pr_err("build_path did not end path lookup where "
1784 "expected, namelen is %d, pos is %d\n", len, pos);
1785 /* presumably this is only possible if racing with a
1786 rename of one of the parent directories (we can not
1787 lock the dentries above us to prevent this, but
1788 retrying should be harmless) */
1793 *base = ceph_ino(d_inode(temp));
1795 dout("build_path on %p %d built %llx '%.*s'\n",
1796 dentry, d_count(dentry), *base, len, path);
1800 static int build_dentry_path(struct dentry *dentry,
1801 const char **ppath, int *ppathlen, u64 *pino,
1806 if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_NOSNAP) {
1807 *pino = ceph_ino(d_inode(dentry->d_parent));
1808 *ppath = dentry->d_name.name;
1809 *ppathlen = dentry->d_name.len;
1812 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1814 return PTR_ERR(path);
1820 static int build_inode_path(struct inode *inode,
1821 const char **ppath, int *ppathlen, u64 *pino,
1824 struct dentry *dentry;
1827 if (ceph_snap(inode) == CEPH_NOSNAP) {
1828 *pino = ceph_ino(inode);
1832 dentry = d_find_alias(inode);
1833 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1836 return PTR_ERR(path);
1843 * request arguments may be specified via an inode *, a dentry *, or
1844 * an explicit ino+path.
1846 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1847 const char *rpath, u64 rino,
1848 const char **ppath, int *pathlen,
1849 u64 *ino, int *freepath)
1854 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1855 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1857 } else if (rdentry) {
1858 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1859 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1861 } else if (rpath || rino) {
1864 *pathlen = rpath ? strlen(rpath) : 0;
1865 dout(" path %.*s\n", *pathlen, rpath);
1872 * called under mdsc->mutex
1874 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1875 struct ceph_mds_request *req,
1876 int mds, bool drop_cap_releases)
1878 struct ceph_msg *msg;
1879 struct ceph_mds_request_head *head;
1880 const char *path1 = NULL;
1881 const char *path2 = NULL;
1882 u64 ino1 = 0, ino2 = 0;
1883 int pathlen1 = 0, pathlen2 = 0;
1884 int freepath1 = 0, freepath2 = 0;
1890 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1891 req->r_path1, req->r_ino1.ino,
1892 &path1, &pathlen1, &ino1, &freepath1);
1898 ret = set_request_path_attr(NULL, req->r_old_dentry,
1899 req->r_path2, req->r_ino2.ino,
1900 &path2, &pathlen2, &ino2, &freepath2);
1906 len = sizeof(*head) +
1907 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
1908 sizeof(struct timespec);
1910 /* calculate (max) length for cap releases */
1911 len += sizeof(struct ceph_mds_request_release) *
1912 (!!req->r_inode_drop + !!req->r_dentry_drop +
1913 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1914 if (req->r_dentry_drop)
1915 len += req->r_dentry->d_name.len;
1916 if (req->r_old_dentry_drop)
1917 len += req->r_old_dentry->d_name.len;
1919 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
1921 msg = ERR_PTR(-ENOMEM);
1925 msg->hdr.version = cpu_to_le16(2);
1926 msg->hdr.tid = cpu_to_le64(req->r_tid);
1928 head = msg->front.iov_base;
1929 p = msg->front.iov_base + sizeof(*head);
1930 end = msg->front.iov_base + msg->front.iov_len;
1932 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1933 head->op = cpu_to_le32(req->r_op);
1934 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
1935 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
1936 head->args = req->r_args;
1938 ceph_encode_filepath(&p, end, ino1, path1);
1939 ceph_encode_filepath(&p, end, ino2, path2);
1941 /* make note of release offset, in case we need to replay */
1942 req->r_request_release_offset = p - msg->front.iov_base;
1946 if (req->r_inode_drop)
1947 releases += ceph_encode_inode_release(&p,
1948 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
1949 mds, req->r_inode_drop, req->r_inode_unless, 0);
1950 if (req->r_dentry_drop)
1951 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1952 mds, req->r_dentry_drop, req->r_dentry_unless);
1953 if (req->r_old_dentry_drop)
1954 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1955 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1956 if (req->r_old_inode_drop)
1957 releases += ceph_encode_inode_release(&p,
1958 d_inode(req->r_old_dentry),
1959 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1961 if (drop_cap_releases) {
1963 p = msg->front.iov_base + req->r_request_release_offset;
1966 head->num_releases = cpu_to_le16(releases);
1970 struct ceph_timespec ts;
1971 ceph_encode_timespec(&ts, &req->r_stamp);
1972 ceph_encode_copy(&p, &ts, sizeof(ts));
1976 msg->front.iov_len = p - msg->front.iov_base;
1977 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1979 if (req->r_pagelist) {
1980 struct ceph_pagelist *pagelist = req->r_pagelist;
1981 atomic_inc(&pagelist->refcnt);
1982 ceph_msg_data_add_pagelist(msg, pagelist);
1983 msg->hdr.data_len = cpu_to_le32(pagelist->length);
1985 msg->hdr.data_len = 0;
1988 msg->hdr.data_off = cpu_to_le16(0);
1992 kfree((char *)path2);
1995 kfree((char *)path1);
2001 * called under mdsc->mutex if error, under no mutex if
2004 static void complete_request(struct ceph_mds_client *mdsc,
2005 struct ceph_mds_request *req)
2007 if (req->r_callback)
2008 req->r_callback(mdsc, req);
2010 complete_all(&req->r_completion);
2014 * called under mdsc->mutex
2016 static int __prepare_send_request(struct ceph_mds_client *mdsc,
2017 struct ceph_mds_request *req,
2018 int mds, bool drop_cap_releases)
2020 struct ceph_mds_request_head *rhead;
2021 struct ceph_msg *msg;
2026 struct ceph_cap *cap =
2027 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2030 req->r_sent_on_mseq = cap->mseq;
2032 req->r_sent_on_mseq = -1;
2034 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2035 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2037 if (req->r_got_unsafe) {
2040 * Replay. Do not regenerate message (and rebuild
2041 * paths, etc.); just use the original message.
2042 * Rebuilding paths will break for renames because
2043 * d_move mangles the src name.
2045 msg = req->r_request;
2046 rhead = msg->front.iov_base;
2048 flags = le32_to_cpu(rhead->flags);
2049 flags |= CEPH_MDS_FLAG_REPLAY;
2050 rhead->flags = cpu_to_le32(flags);
2052 if (req->r_target_inode)
2053 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2055 rhead->num_retry = req->r_attempts - 1;
2057 /* remove cap/dentry releases from message */
2058 rhead->num_releases = 0;
2061 p = msg->front.iov_base + req->r_request_release_offset;
2063 struct ceph_timespec ts;
2064 ceph_encode_timespec(&ts, &req->r_stamp);
2065 ceph_encode_copy(&p, &ts, sizeof(ts));
2068 msg->front.iov_len = p - msg->front.iov_base;
2069 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2073 if (req->r_request) {
2074 ceph_msg_put(req->r_request);
2075 req->r_request = NULL;
2077 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
2079 req->r_err = PTR_ERR(msg);
2080 complete_request(mdsc, req);
2081 return PTR_ERR(msg);
2083 req->r_request = msg;
2085 rhead = msg->front.iov_base;
2086 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
2087 if (req->r_got_unsafe)
2088 flags |= CEPH_MDS_FLAG_REPLAY;
2089 if (req->r_locked_dir)
2090 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2091 rhead->flags = cpu_to_le32(flags);
2092 rhead->num_fwd = req->r_num_fwd;
2093 rhead->num_retry = req->r_attempts - 1;
2096 dout(" r_locked_dir = %p\n", req->r_locked_dir);
2101 * send request, or put it on the appropriate wait list.
2103 static int __do_request(struct ceph_mds_client *mdsc,
2104 struct ceph_mds_request *req)
2106 struct ceph_mds_session *session = NULL;
2110 if (req->r_err || req->r_got_result) {
2112 __unregister_request(mdsc, req);
2116 if (req->r_timeout &&
2117 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2118 dout("do_request timed out\n");
2123 put_request_session(req);
2125 mds = __choose_mds(mdsc, req);
2127 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2128 dout("do_request no mds or not active, waiting for map\n");
2129 list_add(&req->r_wait, &mdsc->waiting_for_map);
2133 /* get, open session */
2134 session = __ceph_lookup_mds_session(mdsc, mds);
2136 session = register_session(mdsc, mds);
2137 if (IS_ERR(session)) {
2138 err = PTR_ERR(session);
2142 req->r_session = get_session(session);
2144 dout("do_request mds%d session %p state %s\n", mds, session,
2145 ceph_session_state_name(session->s_state));
2146 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2147 session->s_state != CEPH_MDS_SESSION_HUNG) {
2148 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2149 session->s_state == CEPH_MDS_SESSION_CLOSING)
2150 __open_session(mdsc, session);
2151 list_add(&req->r_wait, &session->s_waiting);
2156 req->r_resend_mds = -1; /* forget any previous mds hint */
2158 if (req->r_request_started == 0) /* note request start time */
2159 req->r_request_started = jiffies;
2161 err = __prepare_send_request(mdsc, req, mds, false);
2163 ceph_msg_get(req->r_request);
2164 ceph_con_send(&session->s_con, req->r_request);
2168 ceph_put_mds_session(session);
2174 complete_request(mdsc, req);
2179 * called under mdsc->mutex
2181 static void __wake_requests(struct ceph_mds_client *mdsc,
2182 struct list_head *head)
2184 struct ceph_mds_request *req;
2185 LIST_HEAD(tmp_list);
2187 list_splice_init(head, &tmp_list);
2189 while (!list_empty(&tmp_list)) {
2190 req = list_entry(tmp_list.next,
2191 struct ceph_mds_request, r_wait);
2192 list_del_init(&req->r_wait);
2193 dout(" wake request %p tid %llu\n", req, req->r_tid);
2194 __do_request(mdsc, req);
2199 * Wake up threads with requests pending for @mds, so that they can
2200 * resubmit their requests to a possibly different mds.
2202 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
2204 struct ceph_mds_request *req;
2205 struct rb_node *p = rb_first(&mdsc->request_tree);
2207 dout("kick_requests mds%d\n", mds);
2209 req = rb_entry(p, struct ceph_mds_request, r_node);
2211 if (req->r_got_unsafe)
2213 if (req->r_attempts > 0)
2214 continue; /* only new requests */
2215 if (req->r_session &&
2216 req->r_session->s_mds == mds) {
2217 dout(" kicking tid %llu\n", req->r_tid);
2218 list_del_init(&req->r_wait);
2219 __do_request(mdsc, req);
2224 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2225 struct ceph_mds_request *req)
2227 dout("submit_request on %p\n", req);
2228 mutex_lock(&mdsc->mutex);
2229 __register_request(mdsc, req, NULL);
2230 __do_request(mdsc, req);
2231 mutex_unlock(&mdsc->mutex);
2235 * Synchrously perform an mds request. Take care of all of the
2236 * session setup, forwarding, retry details.
2238 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2240 struct ceph_mds_request *req)
2244 dout("do_request on %p\n", req);
2246 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
2248 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
2249 if (req->r_locked_dir)
2250 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
2251 if (req->r_old_dentry_dir)
2252 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2256 mutex_lock(&mdsc->mutex);
2257 __register_request(mdsc, req, dir);
2258 __do_request(mdsc, req);
2262 __unregister_request(mdsc, req);
2263 dout("do_request early error %d\n", err);
2268 mutex_unlock(&mdsc->mutex);
2269 dout("do_request waiting\n");
2270 if (req->r_timeout) {
2271 err = (long)wait_for_completion_killable_timeout(
2272 &req->r_completion, req->r_timeout);
2275 } else if (req->r_wait_for_completion) {
2276 err = req->r_wait_for_completion(mdsc, req);
2278 err = wait_for_completion_killable(&req->r_completion);
2280 dout("do_request waited, got %d\n", err);
2281 mutex_lock(&mdsc->mutex);
2283 /* only abort if we didn't race with a real reply */
2284 if (req->r_got_result) {
2285 err = le32_to_cpu(req->r_reply_info.head->result);
2286 } else if (err < 0) {
2287 dout("aborted request %lld with %d\n", req->r_tid, err);
2290 * ensure we aren't running concurrently with
2291 * ceph_fill_trace or ceph_readdir_prepopulate, which
2292 * rely on locks (dir mutex) held by our caller.
2294 mutex_lock(&req->r_fill_mutex);
2296 req->r_aborted = true;
2297 mutex_unlock(&req->r_fill_mutex);
2299 if (req->r_locked_dir &&
2300 (req->r_op & CEPH_MDS_OP_WRITE))
2301 ceph_invalidate_dir_request(req);
2307 mutex_unlock(&mdsc->mutex);
2308 dout("do_request %p done, result %d\n", req, err);
2313 * Invalidate dir's completeness, dentry lease state on an aborted MDS
2314 * namespace request.
2316 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2318 struct inode *inode = req->r_locked_dir;
2320 dout("invalidate_dir_request %p (complete, lease(s))\n", inode);
2322 ceph_dir_clear_complete(inode);
2324 ceph_invalidate_dentry_lease(req->r_dentry);
2325 if (req->r_old_dentry)
2326 ceph_invalidate_dentry_lease(req->r_old_dentry);
2332 * We take the session mutex and parse and process the reply immediately.
2333 * This preserves the logical ordering of replies, capabilities, etc., sent
2334 * by the MDS as they are applied to our local cache.
2336 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2338 struct ceph_mds_client *mdsc = session->s_mdsc;
2339 struct ceph_mds_request *req;
2340 struct ceph_mds_reply_head *head = msg->front.iov_base;
2341 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2342 struct ceph_snap_realm *realm;
2345 int mds = session->s_mds;
2347 if (msg->front.iov_len < sizeof(*head)) {
2348 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2353 /* get request, session */
2354 tid = le64_to_cpu(msg->hdr.tid);
2355 mutex_lock(&mdsc->mutex);
2356 req = __lookup_request(mdsc, tid);
2358 dout("handle_reply on unknown tid %llu\n", tid);
2359 mutex_unlock(&mdsc->mutex);
2362 dout("handle_reply %p\n", req);
2364 /* correct session? */
2365 if (req->r_session != session) {
2366 pr_err("mdsc_handle_reply got %llu on session mds%d"
2367 " not mds%d\n", tid, session->s_mds,
2368 req->r_session ? req->r_session->s_mds : -1);
2369 mutex_unlock(&mdsc->mutex);
2374 if ((req->r_got_unsafe && !head->safe) ||
2375 (req->r_got_safe && head->safe)) {
2376 pr_warn("got a dup %s reply on %llu from mds%d\n",
2377 head->safe ? "safe" : "unsafe", tid, mds);
2378 mutex_unlock(&mdsc->mutex);
2381 if (req->r_got_safe && !head->safe) {
2382 pr_warn("got unsafe after safe on %llu from mds%d\n",
2384 mutex_unlock(&mdsc->mutex);
2388 result = le32_to_cpu(head->result);
2392 * if we're not talking to the authority, send to them
2393 * if the authority has changed while we weren't looking,
2394 * send to new authority
2395 * Otherwise we just have to return an ESTALE
2397 if (result == -ESTALE) {
2398 dout("got ESTALE on request %llu", req->r_tid);
2399 req->r_resend_mds = -1;
2400 if (req->r_direct_mode != USE_AUTH_MDS) {
2401 dout("not using auth, setting for that now");
2402 req->r_direct_mode = USE_AUTH_MDS;
2403 __do_request(mdsc, req);
2404 mutex_unlock(&mdsc->mutex);
2407 int mds = __choose_mds(mdsc, req);
2408 if (mds >= 0 && mds != req->r_session->s_mds) {
2409 dout("but auth changed, so resending");
2410 __do_request(mdsc, req);
2411 mutex_unlock(&mdsc->mutex);
2415 dout("have to return ESTALE on request %llu", req->r_tid);
2420 req->r_got_safe = true;
2421 __unregister_request(mdsc, req);
2423 if (req->r_got_unsafe) {
2425 * We already handled the unsafe response, now do the
2426 * cleanup. No need to examine the response; the MDS
2427 * doesn't include any result info in the safe
2428 * response. And even if it did, there is nothing
2429 * useful we could do with a revised return value.
2431 dout("got safe reply %llu, mds%d\n", tid, mds);
2432 list_del_init(&req->r_unsafe_item);
2434 /* last unsafe request during umount? */
2435 if (mdsc->stopping && !__get_oldest_req(mdsc))
2436 complete_all(&mdsc->safe_umount_waiters);
2437 mutex_unlock(&mdsc->mutex);
2441 req->r_got_unsafe = true;
2442 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2445 dout("handle_reply tid %lld result %d\n", tid, result);
2446 rinfo = &req->r_reply_info;
2447 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2448 mutex_unlock(&mdsc->mutex);
2450 mutex_lock(&session->s_mutex);
2452 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
2459 if (rinfo->snapblob_len) {
2460 down_write(&mdsc->snap_rwsem);
2461 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2462 rinfo->snapblob + rinfo->snapblob_len,
2463 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2465 downgrade_write(&mdsc->snap_rwsem);
2467 down_read(&mdsc->snap_rwsem);
2470 /* insert trace into our cache */
2471 mutex_lock(&req->r_fill_mutex);
2472 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
2474 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
2475 req->r_op == CEPH_MDS_OP_LSSNAP))
2476 ceph_readdir_prepopulate(req, req->r_session);
2477 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2479 mutex_unlock(&req->r_fill_mutex);
2481 up_read(&mdsc->snap_rwsem);
2483 ceph_put_snap_realm(mdsc, realm);
2485 mutex_lock(&mdsc->mutex);
2486 if (!req->r_aborted) {
2492 req->r_got_result = true;
2495 dout("reply arrived after request %lld was aborted\n", tid);
2497 mutex_unlock(&mdsc->mutex);
2499 ceph_add_cap_releases(mdsc, req->r_session);
2500 mutex_unlock(&session->s_mutex);
2502 /* kick calling process */
2503 complete_request(mdsc, req);
2505 ceph_mdsc_put_request(req);
2512 * handle mds notification that our request has been forwarded.
2514 static void handle_forward(struct ceph_mds_client *mdsc,
2515 struct ceph_mds_session *session,
2516 struct ceph_msg *msg)
2518 struct ceph_mds_request *req;
2519 u64 tid = le64_to_cpu(msg->hdr.tid);
2523 void *p = msg->front.iov_base;
2524 void *end = p + msg->front.iov_len;
2526 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2527 next_mds = ceph_decode_32(&p);
2528 fwd_seq = ceph_decode_32(&p);
2530 mutex_lock(&mdsc->mutex);
2531 req = __lookup_request(mdsc, tid);
2533 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2534 goto out; /* dup reply? */
2537 if (req->r_aborted) {
2538 dout("forward tid %llu aborted, unregistering\n", tid);
2539 __unregister_request(mdsc, req);
2540 } else if (fwd_seq <= req->r_num_fwd) {
2541 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2542 tid, next_mds, req->r_num_fwd, fwd_seq);
2544 /* resend. forward race not possible; mds would drop */
2545 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2547 BUG_ON(req->r_got_result);
2548 req->r_attempts = 0;
2549 req->r_num_fwd = fwd_seq;
2550 req->r_resend_mds = next_mds;
2551 put_request_session(req);
2552 __do_request(mdsc, req);
2554 ceph_mdsc_put_request(req);
2556 mutex_unlock(&mdsc->mutex);
2560 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2564 * handle a mds session control message
2566 static void handle_session(struct ceph_mds_session *session,
2567 struct ceph_msg *msg)
2569 struct ceph_mds_client *mdsc = session->s_mdsc;
2572 int mds = session->s_mds;
2573 struct ceph_mds_session_head *h = msg->front.iov_base;
2577 if (msg->front.iov_len != sizeof(*h))
2579 op = le32_to_cpu(h->op);
2580 seq = le64_to_cpu(h->seq);
2582 mutex_lock(&mdsc->mutex);
2583 if (op == CEPH_SESSION_CLOSE)
2584 __unregister_session(mdsc, session);
2585 /* FIXME: this ttl calculation is generous */
2586 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2587 mutex_unlock(&mdsc->mutex);
2589 mutex_lock(&session->s_mutex);
2591 dout("handle_session mds%d %s %p state %s seq %llu\n",
2592 mds, ceph_session_op_name(op), session,
2593 ceph_session_state_name(session->s_state), seq);
2595 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2596 session->s_state = CEPH_MDS_SESSION_OPEN;
2597 pr_info("mds%d came back\n", session->s_mds);
2601 case CEPH_SESSION_OPEN:
2602 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2603 pr_info("mds%d reconnect success\n", session->s_mds);
2604 session->s_state = CEPH_MDS_SESSION_OPEN;
2605 renewed_caps(mdsc, session, 0);
2608 __close_session(mdsc, session);
2611 case CEPH_SESSION_RENEWCAPS:
2612 if (session->s_renew_seq == seq)
2613 renewed_caps(mdsc, session, 1);
2616 case CEPH_SESSION_CLOSE:
2617 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2618 pr_info("mds%d reconnect denied\n", session->s_mds);
2619 cleanup_session_requests(mdsc, session);
2620 remove_session_caps(session);
2621 wake = 2; /* for good measure */
2622 wake_up_all(&mdsc->session_close_wq);
2625 case CEPH_SESSION_STALE:
2626 pr_info("mds%d caps went stale, renewing\n",
2628 spin_lock(&session->s_gen_ttl_lock);
2629 session->s_cap_gen++;
2630 session->s_cap_ttl = jiffies - 1;
2631 spin_unlock(&session->s_gen_ttl_lock);
2632 send_renew_caps(mdsc, session);
2635 case CEPH_SESSION_RECALL_STATE:
2636 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2639 case CEPH_SESSION_FLUSHMSG:
2640 send_flushmsg_ack(mdsc, session, seq);
2643 case CEPH_SESSION_FORCE_RO:
2644 dout("force_session_readonly %p\n", session);
2645 spin_lock(&session->s_cap_lock);
2646 session->s_readonly = true;
2647 spin_unlock(&session->s_cap_lock);
2648 wake_up_session_caps(session, 0);
2652 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2656 mutex_unlock(&session->s_mutex);
2658 mutex_lock(&mdsc->mutex);
2659 __wake_requests(mdsc, &session->s_waiting);
2661 kick_requests(mdsc, mds);
2662 mutex_unlock(&mdsc->mutex);
2667 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2668 (int)msg->front.iov_len);
2675 * called under session->mutex.
2677 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2678 struct ceph_mds_session *session)
2680 struct ceph_mds_request *req, *nreq;
2684 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2686 mutex_lock(&mdsc->mutex);
2687 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2688 err = __prepare_send_request(mdsc, req, session->s_mds, true);
2690 ceph_msg_get(req->r_request);
2691 ceph_con_send(&session->s_con, req->r_request);
2696 * also re-send old requests when MDS enters reconnect stage. So that MDS
2697 * can process completed request in clientreplay stage.
2699 p = rb_first(&mdsc->request_tree);
2701 req = rb_entry(p, struct ceph_mds_request, r_node);
2703 if (req->r_got_unsafe)
2705 if (req->r_attempts == 0)
2706 continue; /* only old requests */
2707 if (req->r_session &&
2708 req->r_session->s_mds == session->s_mds) {
2709 err = __prepare_send_request(mdsc, req,
2710 session->s_mds, true);
2712 ceph_msg_get(req->r_request);
2713 ceph_con_send(&session->s_con, req->r_request);
2717 mutex_unlock(&mdsc->mutex);
2721 * Encode information about a cap for a reconnect with the MDS.
2723 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2727 struct ceph_mds_cap_reconnect v2;
2728 struct ceph_mds_cap_reconnect_v1 v1;
2731 struct ceph_inode_info *ci;
2732 struct ceph_reconnect_state *recon_state = arg;
2733 struct ceph_pagelist *pagelist = recon_state->pagelist;
2737 struct dentry *dentry;
2741 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2742 inode, ceph_vinop(inode), cap, cap->cap_id,
2743 ceph_cap_string(cap->issued));
2744 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2748 dentry = d_find_alias(inode);
2750 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2752 err = PTR_ERR(path);
2759 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2763 spin_lock(&ci->i_ceph_lock);
2764 cap->seq = 0; /* reset cap seq */
2765 cap->issue_seq = 0; /* and issue_seq */
2766 cap->mseq = 0; /* and migrate_seq */
2767 cap->cap_gen = cap->session->s_cap_gen;
2769 if (recon_state->flock) {
2770 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2771 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2772 rec.v2.issued = cpu_to_le32(cap->issued);
2773 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2774 rec.v2.pathbase = cpu_to_le64(pathbase);
2775 rec.v2.flock_len = 0;
2776 reclen = sizeof(rec.v2);
2778 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2779 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2780 rec.v1.issued = cpu_to_le32(cap->issued);
2781 rec.v1.size = cpu_to_le64(inode->i_size);
2782 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2783 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2784 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2785 rec.v1.pathbase = cpu_to_le64(pathbase);
2786 reclen = sizeof(rec.v1);
2788 spin_unlock(&ci->i_ceph_lock);
2790 if (recon_state->flock) {
2791 int num_fcntl_locks, num_flock_locks;
2792 struct ceph_filelock *flocks;
2795 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
2796 flocks = kmalloc((num_fcntl_locks+num_flock_locks) *
2797 sizeof(struct ceph_filelock), GFP_NOFS);
2802 err = ceph_encode_locks_to_buffer(inode, flocks,
2812 * number of encoded locks is stable, so copy to pagelist
2814 rec.v2.flock_len = cpu_to_le32(2*sizeof(u32) +
2815 (num_fcntl_locks+num_flock_locks) *
2816 sizeof(struct ceph_filelock));
2817 err = ceph_pagelist_append(pagelist, &rec, reclen);
2819 err = ceph_locks_to_pagelist(flocks, pagelist,
2824 err = ceph_pagelist_append(pagelist, &rec, reclen);
2827 recon_state->nr_caps++;
2837 * If an MDS fails and recovers, clients need to reconnect in order to
2838 * reestablish shared state. This includes all caps issued through
2839 * this session _and_ the snap_realm hierarchy. Because it's not
2840 * clear which snap realms the mds cares about, we send everything we
2841 * know about.. that ensures we'll then get any new info the
2842 * recovering MDS might have.
2844 * This is a relatively heavyweight operation, but it's rare.
2846 * called with mdsc->mutex held.
2848 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2849 struct ceph_mds_session *session)
2851 struct ceph_msg *reply;
2853 int mds = session->s_mds;
2856 struct ceph_pagelist *pagelist;
2857 struct ceph_reconnect_state recon_state;
2859 pr_info("mds%d reconnect start\n", mds);
2861 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2863 goto fail_nopagelist;
2864 ceph_pagelist_init(pagelist);
2866 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
2870 mutex_lock(&session->s_mutex);
2871 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2874 dout("session %p state %s\n", session,
2875 ceph_session_state_name(session->s_state));
2877 spin_lock(&session->s_gen_ttl_lock);
2878 session->s_cap_gen++;
2879 spin_unlock(&session->s_gen_ttl_lock);
2881 spin_lock(&session->s_cap_lock);
2882 /* don't know if session is readonly */
2883 session->s_readonly = 0;
2885 * notify __ceph_remove_cap() that we are composing cap reconnect.
2886 * If a cap get released before being added to the cap reconnect,
2887 * __ceph_remove_cap() should skip queuing cap release.
2889 session->s_cap_reconnect = 1;
2890 /* drop old cap expires; we're about to reestablish that state */
2891 discard_cap_releases(mdsc, session);
2892 spin_unlock(&session->s_cap_lock);
2894 /* trim unused caps to reduce MDS's cache rejoin time */
2895 if (mdsc->fsc->sb->s_root)
2896 shrink_dcache_parent(mdsc->fsc->sb->s_root);
2898 ceph_con_close(&session->s_con);
2899 ceph_con_open(&session->s_con,
2900 CEPH_ENTITY_TYPE_MDS, mds,
2901 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2903 /* replay unsafe requests */
2904 replay_unsafe_requests(mdsc, session);
2906 down_read(&mdsc->snap_rwsem);
2908 /* traverse this session's caps */
2909 s_nr_caps = session->s_nr_caps;
2910 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
2914 recon_state.nr_caps = 0;
2915 recon_state.pagelist = pagelist;
2916 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2917 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
2921 spin_lock(&session->s_cap_lock);
2922 session->s_cap_reconnect = 0;
2923 spin_unlock(&session->s_cap_lock);
2926 * snaprealms. we provide mds with the ino, seq (version), and
2927 * parent for all of our realms. If the mds has any newer info,
2930 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2931 struct ceph_snap_realm *realm =
2932 rb_entry(p, struct ceph_snap_realm, node);
2933 struct ceph_mds_snaprealm_reconnect sr_rec;
2935 dout(" adding snap realm %llx seq %lld parent %llx\n",
2936 realm->ino, realm->seq, realm->parent_ino);
2937 sr_rec.ino = cpu_to_le64(realm->ino);
2938 sr_rec.seq = cpu_to_le64(realm->seq);
2939 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2940 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2945 if (recon_state.flock)
2946 reply->hdr.version = cpu_to_le16(2);
2948 /* raced with cap release? */
2949 if (s_nr_caps != recon_state.nr_caps) {
2950 struct page *page = list_first_entry(&pagelist->head,
2952 __le32 *addr = kmap_atomic(page);
2953 *addr = cpu_to_le32(recon_state.nr_caps);
2954 kunmap_atomic(addr);
2957 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2958 ceph_msg_data_add_pagelist(reply, pagelist);
2959 ceph_con_send(&session->s_con, reply);
2961 mutex_unlock(&session->s_mutex);
2963 mutex_lock(&mdsc->mutex);
2964 __wake_requests(mdsc, &session->s_waiting);
2965 mutex_unlock(&mdsc->mutex);
2967 up_read(&mdsc->snap_rwsem);
2971 ceph_msg_put(reply);
2972 up_read(&mdsc->snap_rwsem);
2973 mutex_unlock(&session->s_mutex);
2975 ceph_pagelist_release(pagelist);
2977 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
2983 * compare old and new mdsmaps, kicking requests
2984 * and closing out old connections as necessary
2986 * called under mdsc->mutex.
2988 static void check_new_map(struct ceph_mds_client *mdsc,
2989 struct ceph_mdsmap *newmap,
2990 struct ceph_mdsmap *oldmap)
2993 int oldstate, newstate;
2994 struct ceph_mds_session *s;
2996 dout("check_new_map new %u old %u\n",
2997 newmap->m_epoch, oldmap->m_epoch);
2999 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
3000 if (mdsc->sessions[i] == NULL)
3002 s = mdsc->sessions[i];
3003 oldstate = ceph_mdsmap_get_state(oldmap, i);
3004 newstate = ceph_mdsmap_get_state(newmap, i);
3006 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
3007 i, ceph_mds_state_name(oldstate),
3008 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
3009 ceph_mds_state_name(newstate),
3010 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
3011 ceph_session_state_name(s->s_state));
3013 if (i >= newmap->m_max_mds ||
3014 memcmp(ceph_mdsmap_get_addr(oldmap, i),
3015 ceph_mdsmap_get_addr(newmap, i),
3016 sizeof(struct ceph_entity_addr))) {
3017 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
3018 /* the session never opened, just close it
3020 __wake_requests(mdsc, &s->s_waiting);
3021 __unregister_session(mdsc, s);
3024 mutex_unlock(&mdsc->mutex);
3025 mutex_lock(&s->s_mutex);
3026 mutex_lock(&mdsc->mutex);
3027 ceph_con_close(&s->s_con);
3028 mutex_unlock(&s->s_mutex);
3029 s->s_state = CEPH_MDS_SESSION_RESTARTING;
3031 } else if (oldstate == newstate) {
3032 continue; /* nothing new with this mds */
3038 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
3039 newstate >= CEPH_MDS_STATE_RECONNECT) {
3040 mutex_unlock(&mdsc->mutex);
3041 send_mds_reconnect(mdsc, s);
3042 mutex_lock(&mdsc->mutex);
3046 * kick request on any mds that has gone active.
3048 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3049 newstate >= CEPH_MDS_STATE_ACTIVE) {
3050 if (oldstate != CEPH_MDS_STATE_CREATING &&
3051 oldstate != CEPH_MDS_STATE_STARTING)
3052 pr_info("mds%d recovery completed\n", s->s_mds);
3053 kick_requests(mdsc, i);
3054 ceph_kick_flushing_caps(mdsc, s);
3055 wake_up_session_caps(s, 1);
3059 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
3060 s = mdsc->sessions[i];
3063 if (!ceph_mdsmap_is_laggy(newmap, i))
3065 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3066 s->s_state == CEPH_MDS_SESSION_HUNG ||
3067 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3068 dout(" connecting to export targets of laggy mds%d\n",
3070 __open_export_target_sessions(mdsc, s);
3082 * caller must hold session s_mutex, dentry->d_lock
3084 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3086 struct ceph_dentry_info *di = ceph_dentry(dentry);
3088 ceph_put_mds_session(di->lease_session);
3089 di->lease_session = NULL;
3092 static void handle_lease(struct ceph_mds_client *mdsc,
3093 struct ceph_mds_session *session,
3094 struct ceph_msg *msg)
3096 struct super_block *sb = mdsc->fsc->sb;
3097 struct inode *inode;
3098 struct dentry *parent, *dentry;
3099 struct ceph_dentry_info *di;
3100 int mds = session->s_mds;
3101 struct ceph_mds_lease *h = msg->front.iov_base;
3103 struct ceph_vino vino;
3107 dout("handle_lease from mds%d\n", mds);
3110 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3112 vino.ino = le64_to_cpu(h->ino);
3113 vino.snap = CEPH_NOSNAP;
3114 seq = le32_to_cpu(h->seq);
3115 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
3116 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
3117 if (dname.len != get_unaligned_le32(h+1))
3121 inode = ceph_find_inode(sb, vino);
3122 dout("handle_lease %s, ino %llx %p %.*s\n",
3123 ceph_lease_op_name(h->action), vino.ino, inode,
3124 dname.len, dname.name);
3126 mutex_lock(&session->s_mutex);
3129 if (inode == NULL) {
3130 dout("handle_lease no inode %llx\n", vino.ino);
3135 parent = d_find_alias(inode);
3137 dout("no parent dentry on inode %p\n", inode);
3139 goto release; /* hrm... */
3141 dname.hash = full_name_hash(dname.name, dname.len);
3142 dentry = d_lookup(parent, &dname);
3147 spin_lock(&dentry->d_lock);
3148 di = ceph_dentry(dentry);
3149 switch (h->action) {
3150 case CEPH_MDS_LEASE_REVOKE:
3151 if (di->lease_session == session) {
3152 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3153 h->seq = cpu_to_le32(di->lease_seq);
3154 __ceph_mdsc_drop_dentry_lease(dentry);
3159 case CEPH_MDS_LEASE_RENEW:
3160 if (di->lease_session == session &&
3161 di->lease_gen == session->s_cap_gen &&
3162 di->lease_renew_from &&
3163 di->lease_renew_after == 0) {
3164 unsigned long duration =
3165 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
3167 di->lease_seq = seq;
3168 dentry->d_time = di->lease_renew_from + duration;
3169 di->lease_renew_after = di->lease_renew_from +
3171 di->lease_renew_from = 0;
3175 spin_unlock(&dentry->d_lock);
3182 /* let's just reuse the same message */
3183 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3185 ceph_con_send(&session->s_con, msg);
3189 mutex_unlock(&session->s_mutex);
3193 pr_err("corrupt lease message\n");
3197 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3198 struct inode *inode,
3199 struct dentry *dentry, char action,
3202 struct ceph_msg *msg;
3203 struct ceph_mds_lease *lease;
3204 int len = sizeof(*lease) + sizeof(u32);
3207 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3208 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3209 dnamelen = dentry->d_name.len;
3212 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
3215 lease = msg->front.iov_base;
3216 lease->action = action;
3217 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3218 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3219 lease->seq = cpu_to_le32(seq);
3220 put_unaligned_le32(dnamelen, lease + 1);
3221 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3224 * if this is a preemptive lease RELEASE, no need to
3225 * flush request stream, since the actual request will
3228 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3230 ceph_con_send(&session->s_con, msg);
3234 * Preemptively release a lease we expect to invalidate anyway.
3235 * Pass @inode always, @dentry is optional.
3237 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
3238 struct dentry *dentry)
3240 struct ceph_dentry_info *di;
3241 struct ceph_mds_session *session;
3244 BUG_ON(inode == NULL);
3245 BUG_ON(dentry == NULL);
3247 /* is dentry lease valid? */
3248 spin_lock(&dentry->d_lock);
3249 di = ceph_dentry(dentry);
3250 if (!di || !di->lease_session ||
3251 di->lease_session->s_mds < 0 ||
3252 di->lease_gen != di->lease_session->s_cap_gen ||
3253 !time_before(jiffies, dentry->d_time)) {
3254 dout("lease_release inode %p dentry %p -- "
3257 spin_unlock(&dentry->d_lock);
3261 /* we do have a lease on this dentry; note mds and seq */
3262 session = ceph_get_mds_session(di->lease_session);
3263 seq = di->lease_seq;
3264 __ceph_mdsc_drop_dentry_lease(dentry);
3265 spin_unlock(&dentry->d_lock);
3267 dout("lease_release inode %p dentry %p to mds%d\n",
3268 inode, dentry, session->s_mds);
3269 ceph_mdsc_lease_send_msg(session, inode, dentry,
3270 CEPH_MDS_LEASE_RELEASE, seq);
3271 ceph_put_mds_session(session);
3275 * drop all leases (and dentry refs) in preparation for umount
3277 static void drop_leases(struct ceph_mds_client *mdsc)
3281 dout("drop_leases\n");
3282 mutex_lock(&mdsc->mutex);
3283 for (i = 0; i < mdsc->max_sessions; i++) {
3284 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3287 mutex_unlock(&mdsc->mutex);
3288 mutex_lock(&s->s_mutex);
3289 mutex_unlock(&s->s_mutex);
3290 ceph_put_mds_session(s);
3291 mutex_lock(&mdsc->mutex);
3293 mutex_unlock(&mdsc->mutex);
3299 * delayed work -- periodically trim expired leases, renew caps with mds
3301 static void schedule_delayed(struct ceph_mds_client *mdsc)
3304 unsigned hz = round_jiffies_relative(HZ * delay);
3305 schedule_delayed_work(&mdsc->delayed_work, hz);
3308 static void delayed_work(struct work_struct *work)
3311 struct ceph_mds_client *mdsc =
3312 container_of(work, struct ceph_mds_client, delayed_work.work);
3316 dout("mdsc delayed_work\n");
3317 ceph_check_delayed_caps(mdsc);
3319 mutex_lock(&mdsc->mutex);
3320 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3321 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3322 mdsc->last_renew_caps);
3324 mdsc->last_renew_caps = jiffies;
3326 for (i = 0; i < mdsc->max_sessions; i++) {
3327 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3330 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3331 dout("resending session close request for mds%d\n",
3333 request_close_session(mdsc, s);
3334 ceph_put_mds_session(s);
3337 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3338 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3339 s->s_state = CEPH_MDS_SESSION_HUNG;
3340 pr_info("mds%d hung\n", s->s_mds);
3343 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3344 /* this mds is failed or recovering, just wait */
3345 ceph_put_mds_session(s);
3348 mutex_unlock(&mdsc->mutex);
3350 mutex_lock(&s->s_mutex);
3352 send_renew_caps(mdsc, s);
3354 ceph_con_keepalive(&s->s_con);
3355 ceph_add_cap_releases(mdsc, s);
3356 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3357 s->s_state == CEPH_MDS_SESSION_HUNG)
3358 ceph_send_cap_releases(mdsc, s);
3359 mutex_unlock(&s->s_mutex);
3360 ceph_put_mds_session(s);
3362 mutex_lock(&mdsc->mutex);
3364 mutex_unlock(&mdsc->mutex);
3366 schedule_delayed(mdsc);
3369 int ceph_mdsc_init(struct ceph_fs_client *fsc)
3372 struct ceph_mds_client *mdsc;
3374 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3379 mutex_init(&mdsc->mutex);
3380 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
3381 if (mdsc->mdsmap == NULL) {
3386 init_completion(&mdsc->safe_umount_waiters);
3387 init_waitqueue_head(&mdsc->session_close_wq);
3388 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3389 mdsc->sessions = NULL;
3390 atomic_set(&mdsc->num_sessions, 0);
3391 mdsc->max_sessions = 0;
3393 init_rwsem(&mdsc->snap_rwsem);
3394 mdsc->snap_realms = RB_ROOT;
3395 INIT_LIST_HEAD(&mdsc->snap_empty);
3396 spin_lock_init(&mdsc->snap_empty_lock);
3398 mdsc->request_tree = RB_ROOT;
3399 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3400 mdsc->last_renew_caps = jiffies;
3401 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3402 spin_lock_init(&mdsc->cap_delay_lock);
3403 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3404 spin_lock_init(&mdsc->snap_flush_lock);
3405 mdsc->cap_flush_seq = 0;
3406 INIT_LIST_HEAD(&mdsc->cap_dirty);
3407 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
3408 mdsc->num_cap_flushing = 0;
3409 spin_lock_init(&mdsc->cap_dirty_lock);
3410 init_waitqueue_head(&mdsc->cap_flushing_wq);
3411 spin_lock_init(&mdsc->dentry_lru_lock);
3412 INIT_LIST_HEAD(&mdsc->dentry_lru);
3414 ceph_caps_init(mdsc);
3415 ceph_adjust_min_caps(mdsc, fsc->min_caps);
3421 * Wait for safe replies on open mds requests. If we time out, drop
3422 * all requests from the tree to avoid dangling dentry refs.
3424 static void wait_requests(struct ceph_mds_client *mdsc)
3426 struct ceph_mds_request *req;
3427 struct ceph_fs_client *fsc = mdsc->fsc;
3429 mutex_lock(&mdsc->mutex);
3430 if (__get_oldest_req(mdsc)) {
3431 mutex_unlock(&mdsc->mutex);
3433 dout("wait_requests waiting for requests\n");
3434 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
3435 fsc->client->options->mount_timeout * HZ);
3437 /* tear down remaining requests */
3438 mutex_lock(&mdsc->mutex);
3439 while ((req = __get_oldest_req(mdsc))) {
3440 dout("wait_requests timed out on tid %llu\n",
3442 __unregister_request(mdsc, req);
3445 mutex_unlock(&mdsc->mutex);
3446 dout("wait_requests done\n");
3450 * called before mount is ro, and before dentries are torn down.
3451 * (hmm, does this still race with new lookups?)
3453 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3455 dout("pre_umount\n");
3459 ceph_flush_dirty_caps(mdsc);
3460 wait_requests(mdsc);
3463 * wait for reply handlers to drop their request refs and
3464 * their inode/dcache refs
3470 * wait for all write mds requests to flush.
3472 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3474 struct ceph_mds_request *req = NULL, *nextreq;
3477 mutex_lock(&mdsc->mutex);
3478 dout("wait_unsafe_requests want %lld\n", want_tid);
3480 req = __get_oldest_req(mdsc);
3481 while (req && req->r_tid <= want_tid) {
3482 /* find next request */
3483 n = rb_next(&req->r_node);
3485 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3488 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3490 ceph_mdsc_get_request(req);
3492 ceph_mdsc_get_request(nextreq);
3493 mutex_unlock(&mdsc->mutex);
3494 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3495 req->r_tid, want_tid);
3496 wait_for_completion(&req->r_safe_completion);
3497 mutex_lock(&mdsc->mutex);
3498 ceph_mdsc_put_request(req);
3500 break; /* next dne before, so we're done! */
3501 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3502 /* next request was removed from tree */
3503 ceph_mdsc_put_request(nextreq);
3506 ceph_mdsc_put_request(nextreq); /* won't go away */
3510 mutex_unlock(&mdsc->mutex);
3511 dout("wait_unsafe_requests done\n");
3514 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3516 u64 want_tid, want_flush;
3518 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3522 mutex_lock(&mdsc->mutex);
3523 want_tid = mdsc->last_tid;
3524 mutex_unlock(&mdsc->mutex);
3526 ceph_flush_dirty_caps(mdsc);
3527 spin_lock(&mdsc->cap_dirty_lock);
3528 want_flush = mdsc->cap_flush_seq;
3529 spin_unlock(&mdsc->cap_dirty_lock);
3531 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3533 wait_unsafe_requests(mdsc, want_tid);
3534 wait_caps_flush(mdsc, want_flush);
3538 * true if all sessions are closed, or we force unmount
3540 static bool done_closing_sessions(struct ceph_mds_client *mdsc)
3542 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3544 return atomic_read(&mdsc->num_sessions) == 0;
3548 * called after sb is ro.
3550 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3552 struct ceph_mds_session *session;
3554 struct ceph_fs_client *fsc = mdsc->fsc;
3555 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
3557 dout("close_sessions\n");
3559 /* close sessions */
3560 mutex_lock(&mdsc->mutex);
3561 for (i = 0; i < mdsc->max_sessions; i++) {
3562 session = __ceph_lookup_mds_session(mdsc, i);
3565 mutex_unlock(&mdsc->mutex);
3566 mutex_lock(&session->s_mutex);
3567 __close_session(mdsc, session);
3568 mutex_unlock(&session->s_mutex);
3569 ceph_put_mds_session(session);
3570 mutex_lock(&mdsc->mutex);
3572 mutex_unlock(&mdsc->mutex);
3574 dout("waiting for sessions to close\n");
3575 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3578 /* tear down remaining sessions */
3579 mutex_lock(&mdsc->mutex);
3580 for (i = 0; i < mdsc->max_sessions; i++) {
3581 if (mdsc->sessions[i]) {
3582 session = get_session(mdsc->sessions[i]);
3583 __unregister_session(mdsc, session);
3584 mutex_unlock(&mdsc->mutex);
3585 mutex_lock(&session->s_mutex);
3586 remove_session_caps(session);
3587 mutex_unlock(&session->s_mutex);
3588 ceph_put_mds_session(session);
3589 mutex_lock(&mdsc->mutex);
3592 WARN_ON(!list_empty(&mdsc->cap_delay_list));
3593 mutex_unlock(&mdsc->mutex);
3595 ceph_cleanup_empty_realms(mdsc);
3597 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3602 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
3605 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3607 ceph_mdsmap_destroy(mdsc->mdsmap);
3608 kfree(mdsc->sessions);
3609 ceph_caps_finalize(mdsc);
3612 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3614 struct ceph_mds_client *mdsc = fsc->mdsc;
3616 dout("mdsc_destroy %p\n", mdsc);
3617 ceph_mdsc_stop(mdsc);
3619 /* flush out any connection work with references to us */
3624 dout("mdsc_destroy %p done\n", mdsc);
3629 * handle mds map update.
3631 void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3635 void *p = msg->front.iov_base;
3636 void *end = p + msg->front.iov_len;
3637 struct ceph_mdsmap *newmap, *oldmap;
3638 struct ceph_fsid fsid;
3641 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3642 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3643 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
3645 epoch = ceph_decode_32(&p);
3646 maplen = ceph_decode_32(&p);
3647 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3649 /* do we need it? */
3650 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
3651 mutex_lock(&mdsc->mutex);
3652 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3653 dout("handle_map epoch %u <= our %u\n",
3654 epoch, mdsc->mdsmap->m_epoch);
3655 mutex_unlock(&mdsc->mutex);
3659 newmap = ceph_mdsmap_decode(&p, end);
3660 if (IS_ERR(newmap)) {
3661 err = PTR_ERR(newmap);
3665 /* swap into place */
3667 oldmap = mdsc->mdsmap;
3668 mdsc->mdsmap = newmap;
3669 check_new_map(mdsc, newmap, oldmap);
3670 ceph_mdsmap_destroy(oldmap);
3672 mdsc->mdsmap = newmap; /* first mds map */
3674 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
3676 __wake_requests(mdsc, &mdsc->waiting_for_map);
3678 mutex_unlock(&mdsc->mutex);
3679 schedule_delayed(mdsc);
3683 mutex_unlock(&mdsc->mutex);
3685 pr_err("error decoding mdsmap %d\n", err);
3689 static struct ceph_connection *con_get(struct ceph_connection *con)
3691 struct ceph_mds_session *s = con->private;
3693 if (get_session(s)) {
3694 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
3697 dout("mdsc con_get %p FAIL\n", s);
3701 static void con_put(struct ceph_connection *con)
3703 struct ceph_mds_session *s = con->private;
3705 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref) - 1);
3706 ceph_put_mds_session(s);
3710 * if the client is unresponsive for long enough, the mds will kill
3711 * the session entirely.
3713 static void peer_reset(struct ceph_connection *con)
3715 struct ceph_mds_session *s = con->private;
3716 struct ceph_mds_client *mdsc = s->s_mdsc;
3718 pr_warn("mds%d closed our session\n", s->s_mds);
3719 send_mds_reconnect(mdsc, s);
3722 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3724 struct ceph_mds_session *s = con->private;
3725 struct ceph_mds_client *mdsc = s->s_mdsc;
3726 int type = le16_to_cpu(msg->hdr.type);
3728 mutex_lock(&mdsc->mutex);
3729 if (__verify_registered_session(mdsc, s) < 0) {
3730 mutex_unlock(&mdsc->mutex);
3733 mutex_unlock(&mdsc->mutex);
3736 case CEPH_MSG_MDS_MAP:
3737 ceph_mdsc_handle_map(mdsc, msg);
3739 case CEPH_MSG_CLIENT_SESSION:
3740 handle_session(s, msg);
3742 case CEPH_MSG_CLIENT_REPLY:
3743 handle_reply(s, msg);
3745 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
3746 handle_forward(mdsc, s, msg);
3748 case CEPH_MSG_CLIENT_CAPS:
3749 ceph_handle_caps(s, msg);
3751 case CEPH_MSG_CLIENT_SNAP:
3752 ceph_handle_snap(mdsc, s, msg);
3754 case CEPH_MSG_CLIENT_LEASE:
3755 handle_lease(mdsc, s, msg);
3759 pr_err("received unknown message type %d %s\n", type,
3760 ceph_msg_type_name(type));
3771 * Note: returned pointer is the address of a structure that's
3772 * managed separately. Caller must *not* attempt to free it.
3774 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
3775 int *proto, int force_new)
3777 struct ceph_mds_session *s = con->private;
3778 struct ceph_mds_client *mdsc = s->s_mdsc;
3779 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3780 struct ceph_auth_handshake *auth = &s->s_auth;
3782 if (force_new && auth->authorizer) {
3783 ceph_auth_destroy_authorizer(ac, auth->authorizer);
3784 auth->authorizer = NULL;
3786 if (!auth->authorizer) {
3787 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3790 return ERR_PTR(ret);
3792 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3795 return ERR_PTR(ret);
3797 *proto = ac->protocol;
3803 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3805 struct ceph_mds_session *s = con->private;
3806 struct ceph_mds_client *mdsc = s->s_mdsc;
3807 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3809 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer, len);
3812 static int invalidate_authorizer(struct ceph_connection *con)
3814 struct ceph_mds_session *s = con->private;
3815 struct ceph_mds_client *mdsc = s->s_mdsc;
3816 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3818 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3820 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
3823 static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
3824 struct ceph_msg_header *hdr, int *skip)
3826 struct ceph_msg *msg;
3827 int type = (int) le16_to_cpu(hdr->type);
3828 int front_len = (int) le32_to_cpu(hdr->front_len);
3834 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
3836 pr_err("unable to allocate msg type %d len %d\n",
3844 static int sign_message(struct ceph_connection *con, struct ceph_msg *msg)
3846 struct ceph_mds_session *s = con->private;
3847 struct ceph_auth_handshake *auth = &s->s_auth;
3848 return ceph_auth_sign_message(auth, msg);
3851 static int check_message_signature(struct ceph_connection *con, struct ceph_msg *msg)
3853 struct ceph_mds_session *s = con->private;
3854 struct ceph_auth_handshake *auth = &s->s_auth;
3855 return ceph_auth_check_message_signature(auth, msg);
3858 static const struct ceph_connection_operations mds_con_ops = {
3861 .dispatch = dispatch,
3862 .get_authorizer = get_authorizer,
3863 .verify_authorizer_reply = verify_authorizer_reply,
3864 .invalidate_authorizer = invalidate_authorizer,
3865 .peer_reset = peer_reset,
3866 .alloc_msg = mds_alloc_msg,
3867 .sign_message = sign_message,
3868 .check_message_signature = check_message_signature,