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(dentry->d_inode) != 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 = parent->d_inode;
721 if (dir->i_sb != mdsc->fsc->sb) {
723 inode = req->r_dentry->d_inode;
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 = req->r_dentry->d_inode;
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[3][2] = {
848 {"hostname", utsname()->nodename},
849 {"entity_id", opt->name ? opt->name : ""},
853 /* Calculate serialized length of metadata */
854 metadata_bytes = 4; /* map length */
855 for (i = 0; metadata[i][0] != NULL; ++i) {
856 metadata_bytes += 8 + strlen(metadata[i][0]) +
857 strlen(metadata[i][1]);
858 metadata_key_count++;
861 /* Allocate the message */
862 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + metadata_bytes,
865 pr_err("create_session_msg ENOMEM creating msg\n");
868 h = msg->front.iov_base;
869 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
870 h->seq = cpu_to_le64(seq);
873 * Serialize client metadata into waiting buffer space, using
874 * the format that userspace expects for map<string, string>
876 * ClientSession messages with metadata are v2
878 msg->hdr.version = cpu_to_le16(2);
879 msg->hdr.compat_version = cpu_to_le16(1);
881 /* The write pointer, following the session_head structure */
882 p = msg->front.iov_base + sizeof(*h);
884 /* Number of entries in the map */
885 ceph_encode_32(&p, metadata_key_count);
887 /* Two length-prefixed strings for each entry in the map */
888 for (i = 0; metadata[i][0] != NULL; ++i) {
889 size_t const key_len = strlen(metadata[i][0]);
890 size_t const val_len = strlen(metadata[i][1]);
892 ceph_encode_32(&p, key_len);
893 memcpy(p, metadata[i][0], key_len);
895 ceph_encode_32(&p, val_len);
896 memcpy(p, metadata[i][1], val_len);
904 * send session open request.
906 * called under mdsc->mutex
908 static int __open_session(struct ceph_mds_client *mdsc,
909 struct ceph_mds_session *session)
911 struct ceph_msg *msg;
913 int mds = session->s_mds;
915 /* wait for mds to go active? */
916 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
917 dout("open_session to mds%d (%s)\n", mds,
918 ceph_mds_state_name(mstate));
919 session->s_state = CEPH_MDS_SESSION_OPENING;
920 session->s_renew_requested = jiffies;
922 /* send connect message */
923 msg = create_session_open_msg(mdsc, session->s_seq);
926 ceph_con_send(&session->s_con, msg);
931 * open sessions for any export targets for the given mds
933 * called under mdsc->mutex
935 static struct ceph_mds_session *
936 __open_export_target_session(struct ceph_mds_client *mdsc, int target)
938 struct ceph_mds_session *session;
940 session = __ceph_lookup_mds_session(mdsc, target);
942 session = register_session(mdsc, target);
946 if (session->s_state == CEPH_MDS_SESSION_NEW ||
947 session->s_state == CEPH_MDS_SESSION_CLOSING)
948 __open_session(mdsc, session);
953 struct ceph_mds_session *
954 ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
956 struct ceph_mds_session *session;
958 dout("open_export_target_session to mds%d\n", target);
960 mutex_lock(&mdsc->mutex);
961 session = __open_export_target_session(mdsc, target);
962 mutex_unlock(&mdsc->mutex);
967 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
968 struct ceph_mds_session *session)
970 struct ceph_mds_info *mi;
971 struct ceph_mds_session *ts;
972 int i, mds = session->s_mds;
974 if (mds >= mdsc->mdsmap->m_max_mds)
977 mi = &mdsc->mdsmap->m_info[mds];
978 dout("open_export_target_sessions for mds%d (%d targets)\n",
979 session->s_mds, mi->num_export_targets);
981 for (i = 0; i < mi->num_export_targets; i++) {
982 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
984 ceph_put_mds_session(ts);
988 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
989 struct ceph_mds_session *session)
991 mutex_lock(&mdsc->mutex);
992 __open_export_target_sessions(mdsc, session);
993 mutex_unlock(&mdsc->mutex);
1001 * Free preallocated cap messages assigned to this session
1003 static void cleanup_cap_releases(struct ceph_mds_session *session)
1005 struct ceph_msg *msg;
1007 spin_lock(&session->s_cap_lock);
1008 while (!list_empty(&session->s_cap_releases)) {
1009 msg = list_first_entry(&session->s_cap_releases,
1010 struct ceph_msg, list_head);
1011 list_del_init(&msg->list_head);
1014 while (!list_empty(&session->s_cap_releases_done)) {
1015 msg = list_first_entry(&session->s_cap_releases_done,
1016 struct ceph_msg, list_head);
1017 list_del_init(&msg->list_head);
1020 spin_unlock(&session->s_cap_lock);
1024 * Helper to safely iterate over all caps associated with a session, with
1025 * special care taken to handle a racing __ceph_remove_cap().
1027 * Caller must hold session s_mutex.
1029 static int iterate_session_caps(struct ceph_mds_session *session,
1030 int (*cb)(struct inode *, struct ceph_cap *,
1033 struct list_head *p;
1034 struct ceph_cap *cap;
1035 struct inode *inode, *last_inode = NULL;
1036 struct ceph_cap *old_cap = NULL;
1039 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1040 spin_lock(&session->s_cap_lock);
1041 p = session->s_caps.next;
1042 while (p != &session->s_caps) {
1043 cap = list_entry(p, struct ceph_cap, session_caps);
1044 inode = igrab(&cap->ci->vfs_inode);
1049 session->s_cap_iterator = cap;
1050 spin_unlock(&session->s_cap_lock);
1057 ceph_put_cap(session->s_mdsc, old_cap);
1061 ret = cb(inode, cap, arg);
1064 spin_lock(&session->s_cap_lock);
1066 if (cap->ci == NULL) {
1067 dout("iterate_session_caps finishing cap %p removal\n",
1069 BUG_ON(cap->session != session);
1070 list_del_init(&cap->session_caps);
1071 session->s_nr_caps--;
1072 cap->session = NULL;
1073 old_cap = cap; /* put_cap it w/o locks held */
1080 session->s_cap_iterator = NULL;
1081 spin_unlock(&session->s_cap_lock);
1085 ceph_put_cap(session->s_mdsc, old_cap);
1090 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
1093 struct ceph_inode_info *ci = ceph_inode(inode);
1096 dout("removing cap %p, ci is %p, inode is %p\n",
1097 cap, ci, &ci->vfs_inode);
1098 spin_lock(&ci->i_ceph_lock);
1099 __ceph_remove_cap(cap, false);
1100 if (!__ceph_is_any_real_caps(ci)) {
1101 struct ceph_mds_client *mdsc =
1102 ceph_sb_to_client(inode->i_sb)->mdsc;
1104 spin_lock(&mdsc->cap_dirty_lock);
1105 if (!list_empty(&ci->i_dirty_item)) {
1106 pr_info(" dropping dirty %s state for %p %lld\n",
1107 ceph_cap_string(ci->i_dirty_caps),
1108 inode, ceph_ino(inode));
1109 ci->i_dirty_caps = 0;
1110 list_del_init(&ci->i_dirty_item);
1113 if (!list_empty(&ci->i_flushing_item)) {
1114 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
1115 ceph_cap_string(ci->i_flushing_caps),
1116 inode, ceph_ino(inode));
1117 ci->i_flushing_caps = 0;
1118 list_del_init(&ci->i_flushing_item);
1119 mdsc->num_cap_flushing--;
1122 if (drop && ci->i_wrbuffer_ref) {
1123 pr_info(" dropping dirty data for %p %lld\n",
1124 inode, ceph_ino(inode));
1125 ci->i_wrbuffer_ref = 0;
1126 ci->i_wrbuffer_ref_head = 0;
1129 spin_unlock(&mdsc->cap_dirty_lock);
1131 spin_unlock(&ci->i_ceph_lock);
1138 * caller must hold session s_mutex
1140 static void remove_session_caps(struct ceph_mds_session *session)
1142 dout("remove_session_caps on %p\n", session);
1143 iterate_session_caps(session, remove_session_caps_cb, NULL);
1145 spin_lock(&session->s_cap_lock);
1146 if (session->s_nr_caps > 0) {
1147 struct super_block *sb = session->s_mdsc->fsc->sb;
1148 struct inode *inode;
1149 struct ceph_cap *cap, *prev = NULL;
1150 struct ceph_vino vino;
1152 * iterate_session_caps() skips inodes that are being
1153 * deleted, we need to wait until deletions are complete.
1154 * __wait_on_freeing_inode() is designed for the job,
1155 * but it is not exported, so use lookup inode function
1158 while (!list_empty(&session->s_caps)) {
1159 cap = list_entry(session->s_caps.next,
1160 struct ceph_cap, session_caps);
1164 vino = cap->ci->i_vino;
1165 spin_unlock(&session->s_cap_lock);
1167 inode = ceph_find_inode(sb, vino);
1170 spin_lock(&session->s_cap_lock);
1173 spin_unlock(&session->s_cap_lock);
1175 BUG_ON(session->s_nr_caps > 0);
1176 BUG_ON(!list_empty(&session->s_cap_flushing));
1177 cleanup_cap_releases(session);
1181 * wake up any threads waiting on this session's caps. if the cap is
1182 * old (didn't get renewed on the client reconnect), remove it now.
1184 * caller must hold s_mutex.
1186 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1189 struct ceph_inode_info *ci = ceph_inode(inode);
1191 wake_up_all(&ci->i_cap_wq);
1193 spin_lock(&ci->i_ceph_lock);
1194 ci->i_wanted_max_size = 0;
1195 ci->i_requested_max_size = 0;
1196 spin_unlock(&ci->i_ceph_lock);
1201 static void wake_up_session_caps(struct ceph_mds_session *session,
1204 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1205 iterate_session_caps(session, wake_up_session_cb,
1206 (void *)(unsigned long)reconnect);
1210 * Send periodic message to MDS renewing all currently held caps. The
1211 * ack will reset the expiration for all caps from this session.
1213 * caller holds s_mutex
1215 static int send_renew_caps(struct ceph_mds_client *mdsc,
1216 struct ceph_mds_session *session)
1218 struct ceph_msg *msg;
1221 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1222 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1223 pr_info("mds%d caps stale\n", session->s_mds);
1224 session->s_renew_requested = jiffies;
1226 /* do not try to renew caps until a recovering mds has reconnected
1227 * with its clients. */
1228 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1229 if (state < CEPH_MDS_STATE_RECONNECT) {
1230 dout("send_renew_caps ignoring mds%d (%s)\n",
1231 session->s_mds, ceph_mds_state_name(state));
1235 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1236 ceph_mds_state_name(state));
1237 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1238 ++session->s_renew_seq);
1241 ceph_con_send(&session->s_con, msg);
1245 static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1246 struct ceph_mds_session *session, u64 seq)
1248 struct ceph_msg *msg;
1250 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1251 session->s_mds, ceph_session_state_name(session->s_state), seq);
1252 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1255 ceph_con_send(&session->s_con, msg);
1261 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1263 * Called under session->s_mutex
1265 static void renewed_caps(struct ceph_mds_client *mdsc,
1266 struct ceph_mds_session *session, int is_renew)
1271 spin_lock(&session->s_cap_lock);
1272 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
1274 session->s_cap_ttl = session->s_renew_requested +
1275 mdsc->mdsmap->m_session_timeout*HZ;
1278 if (time_before(jiffies, session->s_cap_ttl)) {
1279 pr_info("mds%d caps renewed\n", session->s_mds);
1282 pr_info("mds%d caps still stale\n", session->s_mds);
1285 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1286 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1287 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1288 spin_unlock(&session->s_cap_lock);
1291 wake_up_session_caps(session, 0);
1295 * send a session close request
1297 static int request_close_session(struct ceph_mds_client *mdsc,
1298 struct ceph_mds_session *session)
1300 struct ceph_msg *msg;
1302 dout("request_close_session mds%d state %s seq %lld\n",
1303 session->s_mds, ceph_session_state_name(session->s_state),
1305 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1308 ceph_con_send(&session->s_con, msg);
1313 * Called with s_mutex held.
1315 static int __close_session(struct ceph_mds_client *mdsc,
1316 struct ceph_mds_session *session)
1318 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1320 session->s_state = CEPH_MDS_SESSION_CLOSING;
1321 return request_close_session(mdsc, session);
1325 * Trim old(er) caps.
1327 * Because we can't cache an inode without one or more caps, we do
1328 * this indirectly: if a cap is unused, we prune its aliases, at which
1329 * point the inode will hopefully get dropped to.
1331 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1332 * memory pressure from the MDS, though, so it needn't be perfect.
1334 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1336 struct ceph_mds_session *session = arg;
1337 struct ceph_inode_info *ci = ceph_inode(inode);
1338 int used, wanted, oissued, mine;
1340 if (session->s_trim_caps <= 0)
1343 spin_lock(&ci->i_ceph_lock);
1344 mine = cap->issued | cap->implemented;
1345 used = __ceph_caps_used(ci);
1346 wanted = __ceph_caps_file_wanted(ci);
1347 oissued = __ceph_caps_issued_other(ci, cap);
1349 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1350 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1351 ceph_cap_string(used), ceph_cap_string(wanted));
1352 if (cap == ci->i_auth_cap) {
1353 if (ci->i_dirty_caps | ci->i_flushing_caps)
1355 if ((used | wanted) & CEPH_CAP_ANY_WR)
1358 if ((used | wanted) & ~oissued & mine)
1359 goto out; /* we need these caps */
1361 session->s_trim_caps--;
1363 /* we aren't the only cap.. just remove us */
1364 __ceph_remove_cap(cap, true);
1366 /* try to drop referring dentries */
1367 spin_unlock(&ci->i_ceph_lock);
1368 d_prune_aliases(inode);
1369 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1370 inode, cap, atomic_read(&inode->i_count));
1375 spin_unlock(&ci->i_ceph_lock);
1380 * Trim session cap count down to some max number.
1382 static int trim_caps(struct ceph_mds_client *mdsc,
1383 struct ceph_mds_session *session,
1386 int trim_caps = session->s_nr_caps - max_caps;
1388 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1389 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1390 if (trim_caps > 0) {
1391 session->s_trim_caps = trim_caps;
1392 iterate_session_caps(session, trim_caps_cb, session);
1393 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1394 session->s_mds, session->s_nr_caps, max_caps,
1395 trim_caps - session->s_trim_caps);
1396 session->s_trim_caps = 0;
1399 ceph_add_cap_releases(mdsc, session);
1400 ceph_send_cap_releases(mdsc, session);
1405 * Allocate cap_release messages. If there is a partially full message
1406 * in the queue, try to allocate enough to cover it's remainder, so that
1407 * we can send it immediately.
1409 * Called under s_mutex.
1411 int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
1412 struct ceph_mds_session *session)
1414 struct ceph_msg *msg, *partial = NULL;
1415 struct ceph_mds_cap_release *head;
1417 int extra = mdsc->fsc->mount_options->cap_release_safety;
1420 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1423 spin_lock(&session->s_cap_lock);
1425 if (!list_empty(&session->s_cap_releases)) {
1426 msg = list_first_entry(&session->s_cap_releases,
1429 head = msg->front.iov_base;
1430 num = le32_to_cpu(head->num);
1432 dout(" partial %p with (%d/%d)\n", msg, num,
1433 (int)CEPH_CAPS_PER_RELEASE);
1434 extra += CEPH_CAPS_PER_RELEASE - num;
1438 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1439 spin_unlock(&session->s_cap_lock);
1440 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1444 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1445 (int)msg->front.iov_len);
1446 head = msg->front.iov_base;
1447 head->num = cpu_to_le32(0);
1448 msg->front.iov_len = sizeof(*head);
1449 spin_lock(&session->s_cap_lock);
1450 list_add(&msg->list_head, &session->s_cap_releases);
1451 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1455 head = partial->front.iov_base;
1456 num = le32_to_cpu(head->num);
1457 dout(" queueing partial %p with %d/%d\n", partial, num,
1458 (int)CEPH_CAPS_PER_RELEASE);
1459 list_move_tail(&partial->list_head,
1460 &session->s_cap_releases_done);
1461 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
1464 spin_unlock(&session->s_cap_lock);
1469 static int check_cap_flush(struct inode *inode, u64 want_flush_seq)
1471 struct ceph_inode_info *ci = ceph_inode(inode);
1473 spin_lock(&ci->i_ceph_lock);
1474 if (ci->i_flushing_caps)
1475 ret = ci->i_cap_flush_seq >= want_flush_seq;
1478 spin_unlock(&ci->i_ceph_lock);
1483 * flush all dirty inode data to disk.
1485 * returns true if we've flushed through want_flush_seq
1487 static void wait_caps_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1491 dout("check_cap_flush want %lld\n", want_flush_seq);
1492 mutex_lock(&mdsc->mutex);
1493 for (mds = 0; mds < mdsc->max_sessions; mds++) {
1494 struct ceph_mds_session *session = mdsc->sessions[mds];
1495 struct inode *inode = NULL;
1499 get_session(session);
1500 mutex_unlock(&mdsc->mutex);
1502 mutex_lock(&session->s_mutex);
1503 if (!list_empty(&session->s_cap_flushing)) {
1504 struct ceph_inode_info *ci =
1505 list_entry(session->s_cap_flushing.next,
1506 struct ceph_inode_info,
1509 if (!check_cap_flush(&ci->vfs_inode, want_flush_seq)) {
1510 dout("check_cap_flush still flushing %p "
1511 "seq %lld <= %lld to mds%d\n",
1512 &ci->vfs_inode, ci->i_cap_flush_seq,
1513 want_flush_seq, session->s_mds);
1514 inode = igrab(&ci->vfs_inode);
1517 mutex_unlock(&session->s_mutex);
1518 ceph_put_mds_session(session);
1521 wait_event(mdsc->cap_flushing_wq,
1522 check_cap_flush(inode, want_flush_seq));
1526 mutex_lock(&mdsc->mutex);
1529 mutex_unlock(&mdsc->mutex);
1530 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1534 * called under s_mutex
1536 void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1537 struct ceph_mds_session *session)
1539 struct ceph_msg *msg;
1541 dout("send_cap_releases mds%d\n", session->s_mds);
1542 spin_lock(&session->s_cap_lock);
1543 while (!list_empty(&session->s_cap_releases_done)) {
1544 msg = list_first_entry(&session->s_cap_releases_done,
1545 struct ceph_msg, list_head);
1546 list_del_init(&msg->list_head);
1547 spin_unlock(&session->s_cap_lock);
1548 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1549 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1550 ceph_con_send(&session->s_con, msg);
1551 spin_lock(&session->s_cap_lock);
1553 spin_unlock(&session->s_cap_lock);
1556 static void discard_cap_releases(struct ceph_mds_client *mdsc,
1557 struct ceph_mds_session *session)
1559 struct ceph_msg *msg;
1560 struct ceph_mds_cap_release *head;
1563 dout("discard_cap_releases mds%d\n", session->s_mds);
1565 if (!list_empty(&session->s_cap_releases)) {
1566 /* zero out the in-progress message */
1567 msg = list_first_entry(&session->s_cap_releases,
1568 struct ceph_msg, list_head);
1569 head = msg->front.iov_base;
1570 num = le32_to_cpu(head->num);
1571 dout("discard_cap_releases mds%d %p %u\n",
1572 session->s_mds, msg, num);
1573 head->num = cpu_to_le32(0);
1574 msg->front.iov_len = sizeof(*head);
1575 session->s_num_cap_releases += num;
1578 /* requeue completed messages */
1579 while (!list_empty(&session->s_cap_releases_done)) {
1580 msg = list_first_entry(&session->s_cap_releases_done,
1581 struct ceph_msg, list_head);
1582 list_del_init(&msg->list_head);
1584 head = msg->front.iov_base;
1585 num = le32_to_cpu(head->num);
1586 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1588 session->s_num_cap_releases += num;
1589 head->num = cpu_to_le32(0);
1590 msg->front.iov_len = sizeof(*head);
1591 list_add(&msg->list_head, &session->s_cap_releases);
1599 int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1602 struct ceph_inode_info *ci = ceph_inode(dir);
1603 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1604 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
1605 size_t size = sizeof(*rinfo->dir_in) + sizeof(*rinfo->dir_dname_len) +
1606 sizeof(*rinfo->dir_dname) + sizeof(*rinfo->dir_dlease);
1607 int order, num_entries;
1609 spin_lock(&ci->i_ceph_lock);
1610 num_entries = ci->i_files + ci->i_subdirs;
1611 spin_unlock(&ci->i_ceph_lock);
1612 num_entries = max(num_entries, 1);
1613 num_entries = min(num_entries, opt->max_readdir);
1615 order = get_order(size * num_entries);
1616 while (order >= 0) {
1617 rinfo->dir_in = (void*)__get_free_pages(GFP_NOFS | __GFP_NOWARN,
1626 num_entries = (PAGE_SIZE << order) / size;
1627 num_entries = min(num_entries, opt->max_readdir);
1629 rinfo->dir_buf_size = PAGE_SIZE << order;
1630 req->r_num_caps = num_entries + 1;
1631 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1632 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1637 * Create an mds request.
1639 struct ceph_mds_request *
1640 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1642 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1645 return ERR_PTR(-ENOMEM);
1647 mutex_init(&req->r_fill_mutex);
1649 req->r_started = jiffies;
1650 req->r_resend_mds = -1;
1651 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1653 kref_init(&req->r_kref);
1654 INIT_LIST_HEAD(&req->r_wait);
1655 init_completion(&req->r_completion);
1656 init_completion(&req->r_safe_completion);
1657 INIT_LIST_HEAD(&req->r_unsafe_item);
1659 req->r_stamp = CURRENT_TIME;
1662 req->r_direct_mode = mode;
1667 * return oldest (lowest) request, tid in request tree, 0 if none.
1669 * called under mdsc->mutex.
1671 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1673 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1675 return rb_entry(rb_first(&mdsc->request_tree),
1676 struct ceph_mds_request, r_node);
1679 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1681 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1689 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1690 * on build_path_from_dentry in fs/cifs/dir.c.
1692 * If @stop_on_nosnap, generate path relative to the first non-snapped
1695 * Encode hidden .snap dirs as a double /, i.e.
1696 * foo/.snap/bar -> foo//bar
1698 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1701 struct dentry *temp;
1707 return ERR_PTR(-EINVAL);
1711 seq = read_seqbegin(&rename_lock);
1713 for (temp = dentry; !IS_ROOT(temp);) {
1714 struct inode *inode = temp->d_inode;
1715 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1716 len++; /* slash only */
1717 else if (stop_on_nosnap && inode &&
1718 ceph_snap(inode) == CEPH_NOSNAP)
1721 len += 1 + temp->d_name.len;
1722 temp = temp->d_parent;
1726 len--; /* no leading '/' */
1728 path = kmalloc(len+1, GFP_NOFS);
1730 return ERR_PTR(-ENOMEM);
1732 path[pos] = 0; /* trailing null */
1734 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1735 struct inode *inode;
1737 spin_lock(&temp->d_lock);
1738 inode = temp->d_inode;
1739 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1740 dout("build_path path+%d: %p SNAPDIR\n",
1742 } else if (stop_on_nosnap && inode &&
1743 ceph_snap(inode) == CEPH_NOSNAP) {
1744 spin_unlock(&temp->d_lock);
1747 pos -= temp->d_name.len;
1749 spin_unlock(&temp->d_lock);
1752 strncpy(path + pos, temp->d_name.name,
1755 spin_unlock(&temp->d_lock);
1758 temp = temp->d_parent;
1761 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
1762 pr_err("build_path did not end path lookup where "
1763 "expected, namelen is %d, pos is %d\n", len, pos);
1764 /* presumably this is only possible if racing with a
1765 rename of one of the parent directories (we can not
1766 lock the dentries above us to prevent this, but
1767 retrying should be harmless) */
1772 *base = ceph_ino(temp->d_inode);
1774 dout("build_path on %p %d built %llx '%.*s'\n",
1775 dentry, d_count(dentry), *base, len, path);
1779 static int build_dentry_path(struct dentry *dentry,
1780 const char **ppath, int *ppathlen, u64 *pino,
1785 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1786 *pino = ceph_ino(dentry->d_parent->d_inode);
1787 *ppath = dentry->d_name.name;
1788 *ppathlen = dentry->d_name.len;
1791 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1793 return PTR_ERR(path);
1799 static int build_inode_path(struct inode *inode,
1800 const char **ppath, int *ppathlen, u64 *pino,
1803 struct dentry *dentry;
1806 if (ceph_snap(inode) == CEPH_NOSNAP) {
1807 *pino = ceph_ino(inode);
1811 dentry = d_find_alias(inode);
1812 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1815 return PTR_ERR(path);
1822 * request arguments may be specified via an inode *, a dentry *, or
1823 * an explicit ino+path.
1825 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1826 const char *rpath, u64 rino,
1827 const char **ppath, int *pathlen,
1828 u64 *ino, int *freepath)
1833 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1834 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1836 } else if (rdentry) {
1837 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1838 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1840 } else if (rpath || rino) {
1843 *pathlen = rpath ? strlen(rpath) : 0;
1844 dout(" path %.*s\n", *pathlen, rpath);
1851 * called under mdsc->mutex
1853 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1854 struct ceph_mds_request *req,
1857 struct ceph_msg *msg;
1858 struct ceph_mds_request_head *head;
1859 const char *path1 = NULL;
1860 const char *path2 = NULL;
1861 u64 ino1 = 0, ino2 = 0;
1862 int pathlen1 = 0, pathlen2 = 0;
1863 int freepath1 = 0, freepath2 = 0;
1869 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1870 req->r_path1, req->r_ino1.ino,
1871 &path1, &pathlen1, &ino1, &freepath1);
1877 ret = set_request_path_attr(NULL, req->r_old_dentry,
1878 req->r_path2, req->r_ino2.ino,
1879 &path2, &pathlen2, &ino2, &freepath2);
1885 len = sizeof(*head) +
1886 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
1887 sizeof(struct timespec);
1889 /* calculate (max) length for cap releases */
1890 len += sizeof(struct ceph_mds_request_release) *
1891 (!!req->r_inode_drop + !!req->r_dentry_drop +
1892 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1893 if (req->r_dentry_drop)
1894 len += req->r_dentry->d_name.len;
1895 if (req->r_old_dentry_drop)
1896 len += req->r_old_dentry->d_name.len;
1898 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
1900 msg = ERR_PTR(-ENOMEM);
1904 msg->hdr.version = cpu_to_le16(2);
1905 msg->hdr.tid = cpu_to_le64(req->r_tid);
1907 head = msg->front.iov_base;
1908 p = msg->front.iov_base + sizeof(*head);
1909 end = msg->front.iov_base + msg->front.iov_len;
1911 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1912 head->op = cpu_to_le32(req->r_op);
1913 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
1914 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
1915 head->args = req->r_args;
1917 ceph_encode_filepath(&p, end, ino1, path1);
1918 ceph_encode_filepath(&p, end, ino2, path2);
1920 /* make note of release offset, in case we need to replay */
1921 req->r_request_release_offset = p - msg->front.iov_base;
1925 if (req->r_inode_drop)
1926 releases += ceph_encode_inode_release(&p,
1927 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1928 mds, req->r_inode_drop, req->r_inode_unless, 0);
1929 if (req->r_dentry_drop)
1930 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1931 mds, req->r_dentry_drop, req->r_dentry_unless);
1932 if (req->r_old_dentry_drop)
1933 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1934 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1935 if (req->r_old_inode_drop)
1936 releases += ceph_encode_inode_release(&p,
1937 req->r_old_dentry->d_inode,
1938 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1939 head->num_releases = cpu_to_le16(releases);
1942 ceph_encode_copy(&p, &req->r_stamp, sizeof(req->r_stamp));
1945 msg->front.iov_len = p - msg->front.iov_base;
1946 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1948 if (req->r_pagelist) {
1949 struct ceph_pagelist *pagelist = req->r_pagelist;
1950 atomic_inc(&pagelist->refcnt);
1951 ceph_msg_data_add_pagelist(msg, pagelist);
1952 msg->hdr.data_len = cpu_to_le32(pagelist->length);
1954 msg->hdr.data_len = 0;
1957 msg->hdr.data_off = cpu_to_le16(0);
1961 kfree((char *)path2);
1964 kfree((char *)path1);
1970 * called under mdsc->mutex if error, under no mutex if
1973 static void complete_request(struct ceph_mds_client *mdsc,
1974 struct ceph_mds_request *req)
1976 if (req->r_callback)
1977 req->r_callback(mdsc, req);
1979 complete_all(&req->r_completion);
1983 * called under mdsc->mutex
1985 static int __prepare_send_request(struct ceph_mds_client *mdsc,
1986 struct ceph_mds_request *req,
1989 struct ceph_mds_request_head *rhead;
1990 struct ceph_msg *msg;
1995 struct ceph_cap *cap =
1996 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1999 req->r_sent_on_mseq = cap->mseq;
2001 req->r_sent_on_mseq = -1;
2003 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2004 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2006 if (req->r_got_unsafe) {
2009 * Replay. Do not regenerate message (and rebuild
2010 * paths, etc.); just use the original message.
2011 * Rebuilding paths will break for renames because
2012 * d_move mangles the src name.
2014 msg = req->r_request;
2015 rhead = msg->front.iov_base;
2017 flags = le32_to_cpu(rhead->flags);
2018 flags |= CEPH_MDS_FLAG_REPLAY;
2019 rhead->flags = cpu_to_le32(flags);
2021 if (req->r_target_inode)
2022 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2024 rhead->num_retry = req->r_attempts - 1;
2026 /* remove cap/dentry releases from message */
2027 rhead->num_releases = 0;
2030 p = msg->front.iov_base + req->r_request_release_offset;
2031 ceph_encode_copy(&p, &req->r_stamp, sizeof(req->r_stamp));
2033 msg->front.iov_len = p - msg->front.iov_base;
2034 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2038 if (req->r_request) {
2039 ceph_msg_put(req->r_request);
2040 req->r_request = NULL;
2042 msg = create_request_message(mdsc, req, mds);
2044 req->r_err = PTR_ERR(msg);
2045 complete_request(mdsc, req);
2046 return PTR_ERR(msg);
2048 req->r_request = msg;
2050 rhead = msg->front.iov_base;
2051 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
2052 if (req->r_got_unsafe)
2053 flags |= CEPH_MDS_FLAG_REPLAY;
2054 if (req->r_locked_dir)
2055 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2056 rhead->flags = cpu_to_le32(flags);
2057 rhead->num_fwd = req->r_num_fwd;
2058 rhead->num_retry = req->r_attempts - 1;
2061 dout(" r_locked_dir = %p\n", req->r_locked_dir);
2066 * send request, or put it on the appropriate wait list.
2068 static int __do_request(struct ceph_mds_client *mdsc,
2069 struct ceph_mds_request *req)
2071 struct ceph_mds_session *session = NULL;
2075 if (req->r_err || req->r_got_result) {
2077 __unregister_request(mdsc, req);
2081 if (req->r_timeout &&
2082 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2083 dout("do_request timed out\n");
2088 put_request_session(req);
2090 mds = __choose_mds(mdsc, req);
2092 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2093 dout("do_request no mds or not active, waiting for map\n");
2094 list_add(&req->r_wait, &mdsc->waiting_for_map);
2098 /* get, open session */
2099 session = __ceph_lookup_mds_session(mdsc, mds);
2101 session = register_session(mdsc, mds);
2102 if (IS_ERR(session)) {
2103 err = PTR_ERR(session);
2107 req->r_session = get_session(session);
2109 dout("do_request mds%d session %p state %s\n", mds, session,
2110 ceph_session_state_name(session->s_state));
2111 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2112 session->s_state != CEPH_MDS_SESSION_HUNG) {
2113 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2114 session->s_state == CEPH_MDS_SESSION_CLOSING)
2115 __open_session(mdsc, session);
2116 list_add(&req->r_wait, &session->s_waiting);
2121 req->r_resend_mds = -1; /* forget any previous mds hint */
2123 if (req->r_request_started == 0) /* note request start time */
2124 req->r_request_started = jiffies;
2126 err = __prepare_send_request(mdsc, req, mds);
2128 ceph_msg_get(req->r_request);
2129 ceph_con_send(&session->s_con, req->r_request);
2133 ceph_put_mds_session(session);
2139 complete_request(mdsc, req);
2144 * called under mdsc->mutex
2146 static void __wake_requests(struct ceph_mds_client *mdsc,
2147 struct list_head *head)
2149 struct ceph_mds_request *req;
2150 LIST_HEAD(tmp_list);
2152 list_splice_init(head, &tmp_list);
2154 while (!list_empty(&tmp_list)) {
2155 req = list_entry(tmp_list.next,
2156 struct ceph_mds_request, r_wait);
2157 list_del_init(&req->r_wait);
2158 dout(" wake request %p tid %llu\n", req, req->r_tid);
2159 __do_request(mdsc, req);
2164 * Wake up threads with requests pending for @mds, so that they can
2165 * resubmit their requests to a possibly different mds.
2167 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
2169 struct ceph_mds_request *req;
2170 struct rb_node *p = rb_first(&mdsc->request_tree);
2172 dout("kick_requests mds%d\n", mds);
2174 req = rb_entry(p, struct ceph_mds_request, r_node);
2176 if (req->r_got_unsafe)
2178 if (req->r_session &&
2179 req->r_session->s_mds == mds) {
2180 dout(" kicking tid %llu\n", req->r_tid);
2181 list_del_init(&req->r_wait);
2182 __do_request(mdsc, req);
2187 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2188 struct ceph_mds_request *req)
2190 dout("submit_request on %p\n", req);
2191 mutex_lock(&mdsc->mutex);
2192 __register_request(mdsc, req, NULL);
2193 __do_request(mdsc, req);
2194 mutex_unlock(&mdsc->mutex);
2198 * Synchrously perform an mds request. Take care of all of the
2199 * session setup, forwarding, retry details.
2201 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2203 struct ceph_mds_request *req)
2207 dout("do_request on %p\n", req);
2209 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
2211 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
2212 if (req->r_locked_dir)
2213 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
2214 if (req->r_old_dentry_dir)
2215 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2219 mutex_lock(&mdsc->mutex);
2220 __register_request(mdsc, req, dir);
2221 __do_request(mdsc, req);
2225 __unregister_request(mdsc, req);
2226 dout("do_request early error %d\n", err);
2231 mutex_unlock(&mdsc->mutex);
2232 dout("do_request waiting\n");
2233 if (req->r_timeout) {
2234 err = (long)wait_for_completion_killable_timeout(
2235 &req->r_completion, req->r_timeout);
2238 } else if (req->r_wait_for_completion) {
2239 err = req->r_wait_for_completion(mdsc, req);
2241 err = wait_for_completion_killable(&req->r_completion);
2243 dout("do_request waited, got %d\n", err);
2244 mutex_lock(&mdsc->mutex);
2246 /* only abort if we didn't race with a real reply */
2247 if (req->r_got_result) {
2248 err = le32_to_cpu(req->r_reply_info.head->result);
2249 } else if (err < 0) {
2250 dout("aborted request %lld with %d\n", req->r_tid, err);
2253 * ensure we aren't running concurrently with
2254 * ceph_fill_trace or ceph_readdir_prepopulate, which
2255 * rely on locks (dir mutex) held by our caller.
2257 mutex_lock(&req->r_fill_mutex);
2259 req->r_aborted = true;
2260 mutex_unlock(&req->r_fill_mutex);
2262 if (req->r_locked_dir &&
2263 (req->r_op & CEPH_MDS_OP_WRITE))
2264 ceph_invalidate_dir_request(req);
2270 mutex_unlock(&mdsc->mutex);
2271 dout("do_request %p done, result %d\n", req, err);
2276 * Invalidate dir's completeness, dentry lease state on an aborted MDS
2277 * namespace request.
2279 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2281 struct inode *inode = req->r_locked_dir;
2283 dout("invalidate_dir_request %p (complete, lease(s))\n", inode);
2285 ceph_dir_clear_complete(inode);
2287 ceph_invalidate_dentry_lease(req->r_dentry);
2288 if (req->r_old_dentry)
2289 ceph_invalidate_dentry_lease(req->r_old_dentry);
2295 * We take the session mutex and parse and process the reply immediately.
2296 * This preserves the logical ordering of replies, capabilities, etc., sent
2297 * by the MDS as they are applied to our local cache.
2299 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2301 struct ceph_mds_client *mdsc = session->s_mdsc;
2302 struct ceph_mds_request *req;
2303 struct ceph_mds_reply_head *head = msg->front.iov_base;
2304 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2305 struct ceph_snap_realm *realm;
2308 int mds = session->s_mds;
2310 if (msg->front.iov_len < sizeof(*head)) {
2311 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2316 /* get request, session */
2317 tid = le64_to_cpu(msg->hdr.tid);
2318 mutex_lock(&mdsc->mutex);
2319 req = __lookup_request(mdsc, tid);
2321 dout("handle_reply on unknown tid %llu\n", tid);
2322 mutex_unlock(&mdsc->mutex);
2325 dout("handle_reply %p\n", req);
2327 /* correct session? */
2328 if (req->r_session != session) {
2329 pr_err("mdsc_handle_reply got %llu on session mds%d"
2330 " not mds%d\n", tid, session->s_mds,
2331 req->r_session ? req->r_session->s_mds : -1);
2332 mutex_unlock(&mdsc->mutex);
2337 if ((req->r_got_unsafe && !head->safe) ||
2338 (req->r_got_safe && head->safe)) {
2339 pr_warn("got a dup %s reply on %llu from mds%d\n",
2340 head->safe ? "safe" : "unsafe", tid, mds);
2341 mutex_unlock(&mdsc->mutex);
2344 if (req->r_got_safe && !head->safe) {
2345 pr_warn("got unsafe after safe on %llu from mds%d\n",
2347 mutex_unlock(&mdsc->mutex);
2351 result = le32_to_cpu(head->result);
2355 * if we're not talking to the authority, send to them
2356 * if the authority has changed while we weren't looking,
2357 * send to new authority
2358 * Otherwise we just have to return an ESTALE
2360 if (result == -ESTALE) {
2361 dout("got ESTALE on request %llu", req->r_tid);
2362 req->r_resend_mds = -1;
2363 if (req->r_direct_mode != USE_AUTH_MDS) {
2364 dout("not using auth, setting for that now");
2365 req->r_direct_mode = USE_AUTH_MDS;
2366 __do_request(mdsc, req);
2367 mutex_unlock(&mdsc->mutex);
2370 int mds = __choose_mds(mdsc, req);
2371 if (mds >= 0 && mds != req->r_session->s_mds) {
2372 dout("but auth changed, so resending");
2373 __do_request(mdsc, req);
2374 mutex_unlock(&mdsc->mutex);
2378 dout("have to return ESTALE on request %llu", req->r_tid);
2383 req->r_got_safe = true;
2384 __unregister_request(mdsc, req);
2386 if (req->r_got_unsafe) {
2388 * We already handled the unsafe response, now do the
2389 * cleanup. No need to examine the response; the MDS
2390 * doesn't include any result info in the safe
2391 * response. And even if it did, there is nothing
2392 * useful we could do with a revised return value.
2394 dout("got safe reply %llu, mds%d\n", tid, mds);
2395 list_del_init(&req->r_unsafe_item);
2397 /* last unsafe request during umount? */
2398 if (mdsc->stopping && !__get_oldest_req(mdsc))
2399 complete_all(&mdsc->safe_umount_waiters);
2400 mutex_unlock(&mdsc->mutex);
2404 req->r_got_unsafe = true;
2405 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2408 dout("handle_reply tid %lld result %d\n", tid, result);
2409 rinfo = &req->r_reply_info;
2410 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2411 mutex_unlock(&mdsc->mutex);
2413 mutex_lock(&session->s_mutex);
2415 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
2422 if (rinfo->snapblob_len) {
2423 down_write(&mdsc->snap_rwsem);
2424 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2425 rinfo->snapblob + rinfo->snapblob_len,
2426 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2428 downgrade_write(&mdsc->snap_rwsem);
2430 down_read(&mdsc->snap_rwsem);
2433 /* insert trace into our cache */
2434 mutex_lock(&req->r_fill_mutex);
2435 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
2437 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
2438 req->r_op == CEPH_MDS_OP_LSSNAP))
2439 ceph_readdir_prepopulate(req, req->r_session);
2440 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2442 mutex_unlock(&req->r_fill_mutex);
2444 up_read(&mdsc->snap_rwsem);
2446 ceph_put_snap_realm(mdsc, realm);
2448 mutex_lock(&mdsc->mutex);
2449 if (!req->r_aborted) {
2455 req->r_got_result = true;
2458 dout("reply arrived after request %lld was aborted\n", tid);
2460 mutex_unlock(&mdsc->mutex);
2462 ceph_add_cap_releases(mdsc, req->r_session);
2463 mutex_unlock(&session->s_mutex);
2465 /* kick calling process */
2466 complete_request(mdsc, req);
2468 ceph_mdsc_put_request(req);
2475 * handle mds notification that our request has been forwarded.
2477 static void handle_forward(struct ceph_mds_client *mdsc,
2478 struct ceph_mds_session *session,
2479 struct ceph_msg *msg)
2481 struct ceph_mds_request *req;
2482 u64 tid = le64_to_cpu(msg->hdr.tid);
2486 void *p = msg->front.iov_base;
2487 void *end = p + msg->front.iov_len;
2489 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2490 next_mds = ceph_decode_32(&p);
2491 fwd_seq = ceph_decode_32(&p);
2493 mutex_lock(&mdsc->mutex);
2494 req = __lookup_request(mdsc, tid);
2496 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2497 goto out; /* dup reply? */
2500 if (req->r_aborted) {
2501 dout("forward tid %llu aborted, unregistering\n", tid);
2502 __unregister_request(mdsc, req);
2503 } else if (fwd_seq <= req->r_num_fwd) {
2504 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2505 tid, next_mds, req->r_num_fwd, fwd_seq);
2507 /* resend. forward race not possible; mds would drop */
2508 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2510 BUG_ON(req->r_got_result);
2511 req->r_num_fwd = fwd_seq;
2512 req->r_resend_mds = next_mds;
2513 put_request_session(req);
2514 __do_request(mdsc, req);
2516 ceph_mdsc_put_request(req);
2518 mutex_unlock(&mdsc->mutex);
2522 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2526 * handle a mds session control message
2528 static void handle_session(struct ceph_mds_session *session,
2529 struct ceph_msg *msg)
2531 struct ceph_mds_client *mdsc = session->s_mdsc;
2534 int mds = session->s_mds;
2535 struct ceph_mds_session_head *h = msg->front.iov_base;
2539 if (msg->front.iov_len != sizeof(*h))
2541 op = le32_to_cpu(h->op);
2542 seq = le64_to_cpu(h->seq);
2544 mutex_lock(&mdsc->mutex);
2545 if (op == CEPH_SESSION_CLOSE)
2546 __unregister_session(mdsc, session);
2547 /* FIXME: this ttl calculation is generous */
2548 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2549 mutex_unlock(&mdsc->mutex);
2551 mutex_lock(&session->s_mutex);
2553 dout("handle_session mds%d %s %p state %s seq %llu\n",
2554 mds, ceph_session_op_name(op), session,
2555 ceph_session_state_name(session->s_state), seq);
2557 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2558 session->s_state = CEPH_MDS_SESSION_OPEN;
2559 pr_info("mds%d came back\n", session->s_mds);
2563 case CEPH_SESSION_OPEN:
2564 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2565 pr_info("mds%d reconnect success\n", session->s_mds);
2566 session->s_state = CEPH_MDS_SESSION_OPEN;
2567 renewed_caps(mdsc, session, 0);
2570 __close_session(mdsc, session);
2573 case CEPH_SESSION_RENEWCAPS:
2574 if (session->s_renew_seq == seq)
2575 renewed_caps(mdsc, session, 1);
2578 case CEPH_SESSION_CLOSE:
2579 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2580 pr_info("mds%d reconnect denied\n", session->s_mds);
2581 remove_session_caps(session);
2582 wake = 2; /* for good measure */
2583 wake_up_all(&mdsc->session_close_wq);
2586 case CEPH_SESSION_STALE:
2587 pr_info("mds%d caps went stale, renewing\n",
2589 spin_lock(&session->s_gen_ttl_lock);
2590 session->s_cap_gen++;
2591 session->s_cap_ttl = jiffies - 1;
2592 spin_unlock(&session->s_gen_ttl_lock);
2593 send_renew_caps(mdsc, session);
2596 case CEPH_SESSION_RECALL_STATE:
2597 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2600 case CEPH_SESSION_FLUSHMSG:
2601 send_flushmsg_ack(mdsc, session, seq);
2604 case CEPH_SESSION_FORCE_RO:
2605 dout("force_session_readonly %p\n", session);
2606 spin_lock(&session->s_cap_lock);
2607 session->s_readonly = true;
2608 spin_unlock(&session->s_cap_lock);
2609 wake_up_session_caps(session, 0);
2613 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2617 mutex_unlock(&session->s_mutex);
2619 mutex_lock(&mdsc->mutex);
2620 __wake_requests(mdsc, &session->s_waiting);
2622 kick_requests(mdsc, mds);
2623 mutex_unlock(&mdsc->mutex);
2628 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2629 (int)msg->front.iov_len);
2636 * called under session->mutex.
2638 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2639 struct ceph_mds_session *session)
2641 struct ceph_mds_request *req, *nreq;
2644 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2646 mutex_lock(&mdsc->mutex);
2647 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2648 err = __prepare_send_request(mdsc, req, session->s_mds);
2650 ceph_msg_get(req->r_request);
2651 ceph_con_send(&session->s_con, req->r_request);
2654 mutex_unlock(&mdsc->mutex);
2658 * Encode information about a cap for a reconnect with the MDS.
2660 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2664 struct ceph_mds_cap_reconnect v2;
2665 struct ceph_mds_cap_reconnect_v1 v1;
2668 struct ceph_inode_info *ci;
2669 struct ceph_reconnect_state *recon_state = arg;
2670 struct ceph_pagelist *pagelist = recon_state->pagelist;
2674 struct dentry *dentry;
2678 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2679 inode, ceph_vinop(inode), cap, cap->cap_id,
2680 ceph_cap_string(cap->issued));
2681 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2685 dentry = d_find_alias(inode);
2687 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2689 err = PTR_ERR(path);
2696 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2700 spin_lock(&ci->i_ceph_lock);
2701 cap->seq = 0; /* reset cap seq */
2702 cap->issue_seq = 0; /* and issue_seq */
2703 cap->mseq = 0; /* and migrate_seq */
2704 cap->cap_gen = cap->session->s_cap_gen;
2706 if (recon_state->flock) {
2707 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2708 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2709 rec.v2.issued = cpu_to_le32(cap->issued);
2710 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2711 rec.v2.pathbase = cpu_to_le64(pathbase);
2712 rec.v2.flock_len = 0;
2713 reclen = sizeof(rec.v2);
2715 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2716 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2717 rec.v1.issued = cpu_to_le32(cap->issued);
2718 rec.v1.size = cpu_to_le64(inode->i_size);
2719 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2720 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2721 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2722 rec.v1.pathbase = cpu_to_le64(pathbase);
2723 reclen = sizeof(rec.v1);
2725 spin_unlock(&ci->i_ceph_lock);
2727 if (recon_state->flock) {
2728 int num_fcntl_locks, num_flock_locks;
2729 struct ceph_filelock *flocks;
2732 spin_lock(&inode->i_lock);
2733 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
2734 spin_unlock(&inode->i_lock);
2735 flocks = kmalloc((num_fcntl_locks+num_flock_locks) *
2736 sizeof(struct ceph_filelock), GFP_NOFS);
2741 spin_lock(&inode->i_lock);
2742 err = ceph_encode_locks_to_buffer(inode, flocks,
2745 spin_unlock(&inode->i_lock);
2753 * number of encoded locks is stable, so copy to pagelist
2755 rec.v2.flock_len = cpu_to_le32(2*sizeof(u32) +
2756 (num_fcntl_locks+num_flock_locks) *
2757 sizeof(struct ceph_filelock));
2758 err = ceph_pagelist_append(pagelist, &rec, reclen);
2760 err = ceph_locks_to_pagelist(flocks, pagelist,
2765 err = ceph_pagelist_append(pagelist, &rec, reclen);
2768 recon_state->nr_caps++;
2778 * If an MDS fails and recovers, clients need to reconnect in order to
2779 * reestablish shared state. This includes all caps issued through
2780 * this session _and_ the snap_realm hierarchy. Because it's not
2781 * clear which snap realms the mds cares about, we send everything we
2782 * know about.. that ensures we'll then get any new info the
2783 * recovering MDS might have.
2785 * This is a relatively heavyweight operation, but it's rare.
2787 * called with mdsc->mutex held.
2789 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2790 struct ceph_mds_session *session)
2792 struct ceph_msg *reply;
2794 int mds = session->s_mds;
2797 struct ceph_pagelist *pagelist;
2798 struct ceph_reconnect_state recon_state;
2800 pr_info("mds%d reconnect start\n", mds);
2802 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2804 goto fail_nopagelist;
2805 ceph_pagelist_init(pagelist);
2807 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
2811 mutex_lock(&session->s_mutex);
2812 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2815 dout("session %p state %s\n", session,
2816 ceph_session_state_name(session->s_state));
2818 spin_lock(&session->s_gen_ttl_lock);
2819 session->s_cap_gen++;
2820 spin_unlock(&session->s_gen_ttl_lock);
2822 spin_lock(&session->s_cap_lock);
2823 /* don't know if session is readonly */
2824 session->s_readonly = 0;
2826 * notify __ceph_remove_cap() that we are composing cap reconnect.
2827 * If a cap get released before being added to the cap reconnect,
2828 * __ceph_remove_cap() should skip queuing cap release.
2830 session->s_cap_reconnect = 1;
2831 /* drop old cap expires; we're about to reestablish that state */
2832 discard_cap_releases(mdsc, session);
2833 spin_unlock(&session->s_cap_lock);
2835 /* trim unused caps to reduce MDS's cache rejoin time */
2836 shrink_dcache_parent(mdsc->fsc->sb->s_root);
2838 ceph_con_close(&session->s_con);
2839 ceph_con_open(&session->s_con,
2840 CEPH_ENTITY_TYPE_MDS, mds,
2841 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2843 /* replay unsafe requests */
2844 replay_unsafe_requests(mdsc, session);
2846 down_read(&mdsc->snap_rwsem);
2848 /* traverse this session's caps */
2849 s_nr_caps = session->s_nr_caps;
2850 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
2854 recon_state.nr_caps = 0;
2855 recon_state.pagelist = pagelist;
2856 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2857 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
2861 spin_lock(&session->s_cap_lock);
2862 session->s_cap_reconnect = 0;
2863 spin_unlock(&session->s_cap_lock);
2866 * snaprealms. we provide mds with the ino, seq (version), and
2867 * parent for all of our realms. If the mds has any newer info,
2870 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2871 struct ceph_snap_realm *realm =
2872 rb_entry(p, struct ceph_snap_realm, node);
2873 struct ceph_mds_snaprealm_reconnect sr_rec;
2875 dout(" adding snap realm %llx seq %lld parent %llx\n",
2876 realm->ino, realm->seq, realm->parent_ino);
2877 sr_rec.ino = cpu_to_le64(realm->ino);
2878 sr_rec.seq = cpu_to_le64(realm->seq);
2879 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2880 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2885 if (recon_state.flock)
2886 reply->hdr.version = cpu_to_le16(2);
2888 /* raced with cap release? */
2889 if (s_nr_caps != recon_state.nr_caps) {
2890 struct page *page = list_first_entry(&pagelist->head,
2892 __le32 *addr = kmap_atomic(page);
2893 *addr = cpu_to_le32(recon_state.nr_caps);
2894 kunmap_atomic(addr);
2897 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2898 ceph_msg_data_add_pagelist(reply, pagelist);
2899 ceph_con_send(&session->s_con, reply);
2901 mutex_unlock(&session->s_mutex);
2903 mutex_lock(&mdsc->mutex);
2904 __wake_requests(mdsc, &session->s_waiting);
2905 mutex_unlock(&mdsc->mutex);
2907 up_read(&mdsc->snap_rwsem);
2911 ceph_msg_put(reply);
2912 up_read(&mdsc->snap_rwsem);
2913 mutex_unlock(&session->s_mutex);
2915 ceph_pagelist_release(pagelist);
2917 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
2923 * compare old and new mdsmaps, kicking requests
2924 * and closing out old connections as necessary
2926 * called under mdsc->mutex.
2928 static void check_new_map(struct ceph_mds_client *mdsc,
2929 struct ceph_mdsmap *newmap,
2930 struct ceph_mdsmap *oldmap)
2933 int oldstate, newstate;
2934 struct ceph_mds_session *s;
2936 dout("check_new_map new %u old %u\n",
2937 newmap->m_epoch, oldmap->m_epoch);
2939 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2940 if (mdsc->sessions[i] == NULL)
2942 s = mdsc->sessions[i];
2943 oldstate = ceph_mdsmap_get_state(oldmap, i);
2944 newstate = ceph_mdsmap_get_state(newmap, i);
2946 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2947 i, ceph_mds_state_name(oldstate),
2948 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
2949 ceph_mds_state_name(newstate),
2950 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
2951 ceph_session_state_name(s->s_state));
2953 if (i >= newmap->m_max_mds ||
2954 memcmp(ceph_mdsmap_get_addr(oldmap, i),
2955 ceph_mdsmap_get_addr(newmap, i),
2956 sizeof(struct ceph_entity_addr))) {
2957 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2958 /* the session never opened, just close it
2960 __wake_requests(mdsc, &s->s_waiting);
2961 __unregister_session(mdsc, s);
2964 mutex_unlock(&mdsc->mutex);
2965 mutex_lock(&s->s_mutex);
2966 mutex_lock(&mdsc->mutex);
2967 ceph_con_close(&s->s_con);
2968 mutex_unlock(&s->s_mutex);
2969 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2972 /* kick any requests waiting on the recovering mds */
2973 kick_requests(mdsc, i);
2974 } else if (oldstate == newstate) {
2975 continue; /* nothing new with this mds */
2981 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
2982 newstate >= CEPH_MDS_STATE_RECONNECT) {
2983 mutex_unlock(&mdsc->mutex);
2984 send_mds_reconnect(mdsc, s);
2985 mutex_lock(&mdsc->mutex);
2989 * kick request on any mds that has gone active.
2991 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2992 newstate >= CEPH_MDS_STATE_ACTIVE) {
2993 if (oldstate != CEPH_MDS_STATE_CREATING &&
2994 oldstate != CEPH_MDS_STATE_STARTING)
2995 pr_info("mds%d recovery completed\n", s->s_mds);
2996 kick_requests(mdsc, i);
2997 ceph_kick_flushing_caps(mdsc, s);
2998 wake_up_session_caps(s, 1);
3002 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
3003 s = mdsc->sessions[i];
3006 if (!ceph_mdsmap_is_laggy(newmap, i))
3008 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3009 s->s_state == CEPH_MDS_SESSION_HUNG ||
3010 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3011 dout(" connecting to export targets of laggy mds%d\n",
3013 __open_export_target_sessions(mdsc, s);
3025 * caller must hold session s_mutex, dentry->d_lock
3027 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3029 struct ceph_dentry_info *di = ceph_dentry(dentry);
3031 ceph_put_mds_session(di->lease_session);
3032 di->lease_session = NULL;
3035 static void handle_lease(struct ceph_mds_client *mdsc,
3036 struct ceph_mds_session *session,
3037 struct ceph_msg *msg)
3039 struct super_block *sb = mdsc->fsc->sb;
3040 struct inode *inode;
3041 struct dentry *parent, *dentry;
3042 struct ceph_dentry_info *di;
3043 int mds = session->s_mds;
3044 struct ceph_mds_lease *h = msg->front.iov_base;
3046 struct ceph_vino vino;
3050 dout("handle_lease from mds%d\n", mds);
3053 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3055 vino.ino = le64_to_cpu(h->ino);
3056 vino.snap = CEPH_NOSNAP;
3057 seq = le32_to_cpu(h->seq);
3058 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
3059 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
3060 if (dname.len != get_unaligned_le32(h+1))
3064 inode = ceph_find_inode(sb, vino);
3065 dout("handle_lease %s, ino %llx %p %.*s\n",
3066 ceph_lease_op_name(h->action), vino.ino, inode,
3067 dname.len, dname.name);
3069 mutex_lock(&session->s_mutex);
3072 if (inode == NULL) {
3073 dout("handle_lease no inode %llx\n", vino.ino);
3078 parent = d_find_alias(inode);
3080 dout("no parent dentry on inode %p\n", inode);
3082 goto release; /* hrm... */
3084 dname.hash = full_name_hash(dname.name, dname.len);
3085 dentry = d_lookup(parent, &dname);
3090 spin_lock(&dentry->d_lock);
3091 di = ceph_dentry(dentry);
3092 switch (h->action) {
3093 case CEPH_MDS_LEASE_REVOKE:
3094 if (di->lease_session == session) {
3095 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3096 h->seq = cpu_to_le32(di->lease_seq);
3097 __ceph_mdsc_drop_dentry_lease(dentry);
3102 case CEPH_MDS_LEASE_RENEW:
3103 if (di->lease_session == session &&
3104 di->lease_gen == session->s_cap_gen &&
3105 di->lease_renew_from &&
3106 di->lease_renew_after == 0) {
3107 unsigned long duration =
3108 le32_to_cpu(h->duration_ms) * HZ / 1000;
3110 di->lease_seq = seq;
3111 dentry->d_time = di->lease_renew_from + duration;
3112 di->lease_renew_after = di->lease_renew_from +
3114 di->lease_renew_from = 0;
3118 spin_unlock(&dentry->d_lock);
3125 /* let's just reuse the same message */
3126 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3128 ceph_con_send(&session->s_con, msg);
3132 mutex_unlock(&session->s_mutex);
3136 pr_err("corrupt lease message\n");
3140 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3141 struct inode *inode,
3142 struct dentry *dentry, char action,
3145 struct ceph_msg *msg;
3146 struct ceph_mds_lease *lease;
3147 int len = sizeof(*lease) + sizeof(u32);
3150 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3151 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3152 dnamelen = dentry->d_name.len;
3155 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
3158 lease = msg->front.iov_base;
3159 lease->action = action;
3160 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3161 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3162 lease->seq = cpu_to_le32(seq);
3163 put_unaligned_le32(dnamelen, lease + 1);
3164 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3167 * if this is a preemptive lease RELEASE, no need to
3168 * flush request stream, since the actual request will
3171 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3173 ceph_con_send(&session->s_con, msg);
3177 * Preemptively release a lease we expect to invalidate anyway.
3178 * Pass @inode always, @dentry is optional.
3180 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
3181 struct dentry *dentry)
3183 struct ceph_dentry_info *di;
3184 struct ceph_mds_session *session;
3187 BUG_ON(inode == NULL);
3188 BUG_ON(dentry == NULL);
3190 /* is dentry lease valid? */
3191 spin_lock(&dentry->d_lock);
3192 di = ceph_dentry(dentry);
3193 if (!di || !di->lease_session ||
3194 di->lease_session->s_mds < 0 ||
3195 di->lease_gen != di->lease_session->s_cap_gen ||
3196 !time_before(jiffies, dentry->d_time)) {
3197 dout("lease_release inode %p dentry %p -- "
3200 spin_unlock(&dentry->d_lock);
3204 /* we do have a lease on this dentry; note mds and seq */
3205 session = ceph_get_mds_session(di->lease_session);
3206 seq = di->lease_seq;
3207 __ceph_mdsc_drop_dentry_lease(dentry);
3208 spin_unlock(&dentry->d_lock);
3210 dout("lease_release inode %p dentry %p to mds%d\n",
3211 inode, dentry, session->s_mds);
3212 ceph_mdsc_lease_send_msg(session, inode, dentry,
3213 CEPH_MDS_LEASE_RELEASE, seq);
3214 ceph_put_mds_session(session);
3218 * drop all leases (and dentry refs) in preparation for umount
3220 static void drop_leases(struct ceph_mds_client *mdsc)
3224 dout("drop_leases\n");
3225 mutex_lock(&mdsc->mutex);
3226 for (i = 0; i < mdsc->max_sessions; i++) {
3227 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3230 mutex_unlock(&mdsc->mutex);
3231 mutex_lock(&s->s_mutex);
3232 mutex_unlock(&s->s_mutex);
3233 ceph_put_mds_session(s);
3234 mutex_lock(&mdsc->mutex);
3236 mutex_unlock(&mdsc->mutex);
3242 * delayed work -- periodically trim expired leases, renew caps with mds
3244 static void schedule_delayed(struct ceph_mds_client *mdsc)
3247 unsigned hz = round_jiffies_relative(HZ * delay);
3248 schedule_delayed_work(&mdsc->delayed_work, hz);
3251 static void delayed_work(struct work_struct *work)
3254 struct ceph_mds_client *mdsc =
3255 container_of(work, struct ceph_mds_client, delayed_work.work);
3259 dout("mdsc delayed_work\n");
3260 ceph_check_delayed_caps(mdsc);
3262 mutex_lock(&mdsc->mutex);
3263 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3264 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3265 mdsc->last_renew_caps);
3267 mdsc->last_renew_caps = jiffies;
3269 for (i = 0; i < mdsc->max_sessions; i++) {
3270 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3273 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3274 dout("resending session close request for mds%d\n",
3276 request_close_session(mdsc, s);
3277 ceph_put_mds_session(s);
3280 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3281 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3282 s->s_state = CEPH_MDS_SESSION_HUNG;
3283 pr_info("mds%d hung\n", s->s_mds);
3286 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3287 /* this mds is failed or recovering, just wait */
3288 ceph_put_mds_session(s);
3291 mutex_unlock(&mdsc->mutex);
3293 mutex_lock(&s->s_mutex);
3295 send_renew_caps(mdsc, s);
3297 ceph_con_keepalive(&s->s_con);
3298 ceph_add_cap_releases(mdsc, s);
3299 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3300 s->s_state == CEPH_MDS_SESSION_HUNG)
3301 ceph_send_cap_releases(mdsc, s);
3302 mutex_unlock(&s->s_mutex);
3303 ceph_put_mds_session(s);
3305 mutex_lock(&mdsc->mutex);
3307 mutex_unlock(&mdsc->mutex);
3309 schedule_delayed(mdsc);
3312 int ceph_mdsc_init(struct ceph_fs_client *fsc)
3315 struct ceph_mds_client *mdsc;
3317 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3322 mutex_init(&mdsc->mutex);
3323 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
3324 if (mdsc->mdsmap == NULL) {
3329 init_completion(&mdsc->safe_umount_waiters);
3330 init_waitqueue_head(&mdsc->session_close_wq);
3331 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3332 mdsc->sessions = NULL;
3333 atomic_set(&mdsc->num_sessions, 0);
3334 mdsc->max_sessions = 0;
3336 init_rwsem(&mdsc->snap_rwsem);
3337 mdsc->snap_realms = RB_ROOT;
3338 INIT_LIST_HEAD(&mdsc->snap_empty);
3339 spin_lock_init(&mdsc->snap_empty_lock);
3341 mdsc->request_tree = RB_ROOT;
3342 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3343 mdsc->last_renew_caps = jiffies;
3344 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3345 spin_lock_init(&mdsc->cap_delay_lock);
3346 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3347 spin_lock_init(&mdsc->snap_flush_lock);
3348 mdsc->cap_flush_seq = 0;
3349 INIT_LIST_HEAD(&mdsc->cap_dirty);
3350 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
3351 mdsc->num_cap_flushing = 0;
3352 spin_lock_init(&mdsc->cap_dirty_lock);
3353 init_waitqueue_head(&mdsc->cap_flushing_wq);
3354 spin_lock_init(&mdsc->dentry_lru_lock);
3355 INIT_LIST_HEAD(&mdsc->dentry_lru);
3357 ceph_caps_init(mdsc);
3358 ceph_adjust_min_caps(mdsc, fsc->min_caps);
3364 * Wait for safe replies on open mds requests. If we time out, drop
3365 * all requests from the tree to avoid dangling dentry refs.
3367 static void wait_requests(struct ceph_mds_client *mdsc)
3369 struct ceph_mds_request *req;
3370 struct ceph_fs_client *fsc = mdsc->fsc;
3372 mutex_lock(&mdsc->mutex);
3373 if (__get_oldest_req(mdsc)) {
3374 mutex_unlock(&mdsc->mutex);
3376 dout("wait_requests waiting for requests\n");
3377 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
3378 fsc->client->options->mount_timeout * HZ);
3380 /* tear down remaining requests */
3381 mutex_lock(&mdsc->mutex);
3382 while ((req = __get_oldest_req(mdsc))) {
3383 dout("wait_requests timed out on tid %llu\n",
3385 __unregister_request(mdsc, req);
3388 mutex_unlock(&mdsc->mutex);
3389 dout("wait_requests done\n");
3393 * called before mount is ro, and before dentries are torn down.
3394 * (hmm, does this still race with new lookups?)
3396 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3398 dout("pre_umount\n");
3402 ceph_flush_dirty_caps(mdsc);
3403 wait_requests(mdsc);
3406 * wait for reply handlers to drop their request refs and
3407 * their inode/dcache refs
3413 * wait for all write mds requests to flush.
3415 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3417 struct ceph_mds_request *req = NULL, *nextreq;
3420 mutex_lock(&mdsc->mutex);
3421 dout("wait_unsafe_requests want %lld\n", want_tid);
3423 req = __get_oldest_req(mdsc);
3424 while (req && req->r_tid <= want_tid) {
3425 /* find next request */
3426 n = rb_next(&req->r_node);
3428 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3431 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3433 ceph_mdsc_get_request(req);
3435 ceph_mdsc_get_request(nextreq);
3436 mutex_unlock(&mdsc->mutex);
3437 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3438 req->r_tid, want_tid);
3439 wait_for_completion(&req->r_safe_completion);
3440 mutex_lock(&mdsc->mutex);
3441 ceph_mdsc_put_request(req);
3443 break; /* next dne before, so we're done! */
3444 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3445 /* next request was removed from tree */
3446 ceph_mdsc_put_request(nextreq);
3449 ceph_mdsc_put_request(nextreq); /* won't go away */
3453 mutex_unlock(&mdsc->mutex);
3454 dout("wait_unsafe_requests done\n");
3457 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3459 u64 want_tid, want_flush;
3461 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3465 mutex_lock(&mdsc->mutex);
3466 want_tid = mdsc->last_tid;
3467 mutex_unlock(&mdsc->mutex);
3469 ceph_flush_dirty_caps(mdsc);
3470 spin_lock(&mdsc->cap_dirty_lock);
3471 want_flush = mdsc->cap_flush_seq;
3472 spin_unlock(&mdsc->cap_dirty_lock);
3474 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3476 wait_unsafe_requests(mdsc, want_tid);
3477 wait_caps_flush(mdsc, want_flush);
3481 * true if all sessions are closed, or we force unmount
3483 static bool done_closing_sessions(struct ceph_mds_client *mdsc)
3485 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3487 return atomic_read(&mdsc->num_sessions) == 0;
3491 * called after sb is ro.
3493 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3495 struct ceph_mds_session *session;
3497 struct ceph_fs_client *fsc = mdsc->fsc;
3498 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
3500 dout("close_sessions\n");
3502 /* close sessions */
3503 mutex_lock(&mdsc->mutex);
3504 for (i = 0; i < mdsc->max_sessions; i++) {
3505 session = __ceph_lookup_mds_session(mdsc, i);
3508 mutex_unlock(&mdsc->mutex);
3509 mutex_lock(&session->s_mutex);
3510 __close_session(mdsc, session);
3511 mutex_unlock(&session->s_mutex);
3512 ceph_put_mds_session(session);
3513 mutex_lock(&mdsc->mutex);
3515 mutex_unlock(&mdsc->mutex);
3517 dout("waiting for sessions to close\n");
3518 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3521 /* tear down remaining sessions */
3522 mutex_lock(&mdsc->mutex);
3523 for (i = 0; i < mdsc->max_sessions; i++) {
3524 if (mdsc->sessions[i]) {
3525 session = get_session(mdsc->sessions[i]);
3526 __unregister_session(mdsc, session);
3527 mutex_unlock(&mdsc->mutex);
3528 mutex_lock(&session->s_mutex);
3529 remove_session_caps(session);
3530 mutex_unlock(&session->s_mutex);
3531 ceph_put_mds_session(session);
3532 mutex_lock(&mdsc->mutex);
3535 WARN_ON(!list_empty(&mdsc->cap_delay_list));
3536 mutex_unlock(&mdsc->mutex);
3538 ceph_cleanup_empty_realms(mdsc);
3540 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3545 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
3548 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3550 ceph_mdsmap_destroy(mdsc->mdsmap);
3551 kfree(mdsc->sessions);
3552 ceph_caps_finalize(mdsc);
3555 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3557 struct ceph_mds_client *mdsc = fsc->mdsc;
3559 dout("mdsc_destroy %p\n", mdsc);
3560 ceph_mdsc_stop(mdsc);
3562 /* flush out any connection work with references to us */
3567 dout("mdsc_destroy %p done\n", mdsc);
3572 * handle mds map update.
3574 void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3578 void *p = msg->front.iov_base;
3579 void *end = p + msg->front.iov_len;
3580 struct ceph_mdsmap *newmap, *oldmap;
3581 struct ceph_fsid fsid;
3584 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3585 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3586 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
3588 epoch = ceph_decode_32(&p);
3589 maplen = ceph_decode_32(&p);
3590 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3592 /* do we need it? */
3593 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
3594 mutex_lock(&mdsc->mutex);
3595 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3596 dout("handle_map epoch %u <= our %u\n",
3597 epoch, mdsc->mdsmap->m_epoch);
3598 mutex_unlock(&mdsc->mutex);
3602 newmap = ceph_mdsmap_decode(&p, end);
3603 if (IS_ERR(newmap)) {
3604 err = PTR_ERR(newmap);
3608 /* swap into place */
3610 oldmap = mdsc->mdsmap;
3611 mdsc->mdsmap = newmap;
3612 check_new_map(mdsc, newmap, oldmap);
3613 ceph_mdsmap_destroy(oldmap);
3615 mdsc->mdsmap = newmap; /* first mds map */
3617 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
3619 __wake_requests(mdsc, &mdsc->waiting_for_map);
3621 mutex_unlock(&mdsc->mutex);
3622 schedule_delayed(mdsc);
3626 mutex_unlock(&mdsc->mutex);
3628 pr_err("error decoding mdsmap %d\n", err);
3632 static struct ceph_connection *con_get(struct ceph_connection *con)
3634 struct ceph_mds_session *s = con->private;
3636 if (get_session(s)) {
3637 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
3640 dout("mdsc con_get %p FAIL\n", s);
3644 static void con_put(struct ceph_connection *con)
3646 struct ceph_mds_session *s = con->private;
3648 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref) - 1);
3649 ceph_put_mds_session(s);
3653 * if the client is unresponsive for long enough, the mds will kill
3654 * the session entirely.
3656 static void peer_reset(struct ceph_connection *con)
3658 struct ceph_mds_session *s = con->private;
3659 struct ceph_mds_client *mdsc = s->s_mdsc;
3661 pr_warn("mds%d closed our session\n", s->s_mds);
3662 send_mds_reconnect(mdsc, s);
3665 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3667 struct ceph_mds_session *s = con->private;
3668 struct ceph_mds_client *mdsc = s->s_mdsc;
3669 int type = le16_to_cpu(msg->hdr.type);
3671 mutex_lock(&mdsc->mutex);
3672 if (__verify_registered_session(mdsc, s) < 0) {
3673 mutex_unlock(&mdsc->mutex);
3676 mutex_unlock(&mdsc->mutex);
3679 case CEPH_MSG_MDS_MAP:
3680 ceph_mdsc_handle_map(mdsc, msg);
3682 case CEPH_MSG_CLIENT_SESSION:
3683 handle_session(s, msg);
3685 case CEPH_MSG_CLIENT_REPLY:
3686 handle_reply(s, msg);
3688 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
3689 handle_forward(mdsc, s, msg);
3691 case CEPH_MSG_CLIENT_CAPS:
3692 ceph_handle_caps(s, msg);
3694 case CEPH_MSG_CLIENT_SNAP:
3695 ceph_handle_snap(mdsc, s, msg);
3697 case CEPH_MSG_CLIENT_LEASE:
3698 handle_lease(mdsc, s, msg);
3702 pr_err("received unknown message type %d %s\n", type,
3703 ceph_msg_type_name(type));
3714 * Note: returned pointer is the address of a structure that's
3715 * managed separately. Caller must *not* attempt to free it.
3717 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
3718 int *proto, int force_new)
3720 struct ceph_mds_session *s = con->private;
3721 struct ceph_mds_client *mdsc = s->s_mdsc;
3722 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3723 struct ceph_auth_handshake *auth = &s->s_auth;
3725 if (force_new && auth->authorizer) {
3726 ceph_auth_destroy_authorizer(ac, auth->authorizer);
3727 auth->authorizer = NULL;
3729 if (!auth->authorizer) {
3730 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3733 return ERR_PTR(ret);
3735 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3738 return ERR_PTR(ret);
3740 *proto = ac->protocol;
3746 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3748 struct ceph_mds_session *s = con->private;
3749 struct ceph_mds_client *mdsc = s->s_mdsc;
3750 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3752 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer, len);
3755 static int invalidate_authorizer(struct ceph_connection *con)
3757 struct ceph_mds_session *s = con->private;
3758 struct ceph_mds_client *mdsc = s->s_mdsc;
3759 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3761 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3763 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
3766 static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
3767 struct ceph_msg_header *hdr, int *skip)
3769 struct ceph_msg *msg;
3770 int type = (int) le16_to_cpu(hdr->type);
3771 int front_len = (int) le32_to_cpu(hdr->front_len);
3777 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
3779 pr_err("unable to allocate msg type %d len %d\n",
3787 static int sign_message(struct ceph_connection *con, struct ceph_msg *msg)
3789 struct ceph_mds_session *s = con->private;
3790 struct ceph_auth_handshake *auth = &s->s_auth;
3791 return ceph_auth_sign_message(auth, msg);
3794 static int check_message_signature(struct ceph_connection *con, struct ceph_msg *msg)
3796 struct ceph_mds_session *s = con->private;
3797 struct ceph_auth_handshake *auth = &s->s_auth;
3798 return ceph_auth_check_message_signature(auth, msg);
3801 static const struct ceph_connection_operations mds_con_ops = {
3804 .dispatch = dispatch,
3805 .get_authorizer = get_authorizer,
3806 .verify_authorizer_reply = verify_authorizer_reply,
3807 .invalidate_authorizer = invalidate_authorizer,
3808 .peer_reset = peer_reset,
3809 .alloc_msg = mds_alloc_msg,
3810 .sign_message = sign_message,
3811 .check_message_signature = check_message_signature,