1 #include <linux/ceph/ceph_debug.h>
4 #include <linux/wait.h>
5 #include <linux/slab.h>
6 #include <linux/sched.h>
7 #include <linux/debugfs.h>
8 #include <linux/seq_file.h>
11 #include "mds_client.h"
13 #include <linux/ceph/ceph_features.h>
14 #include <linux/ceph/messenger.h>
15 #include <linux/ceph/decode.h>
16 #include <linux/ceph/pagelist.h>
17 #include <linux/ceph/auth.h>
18 #include <linux/ceph/debugfs.h>
21 * A cluster of MDS (metadata server) daemons is responsible for
22 * managing the file system namespace (the directory hierarchy and
23 * inodes) and for coordinating shared access to storage. Metadata is
24 * partitioning hierarchically across a number of servers, and that
25 * partition varies over time as the cluster adjusts the distribution
26 * in order to balance load.
28 * The MDS client is primarily responsible to managing synchronous
29 * metadata requests for operations like open, unlink, and so forth.
30 * If there is a MDS failure, we find out about it when we (possibly
31 * request and) receive a new MDS map, and can resubmit affected
34 * For the most part, though, we take advantage of a lossless
35 * communications channel to the MDS, and do not need to worry about
36 * timing out or resubmitting requests.
38 * We maintain a stateful "session" with each MDS we interact with.
39 * Within each session, we sent periodic heartbeat messages to ensure
40 * any capabilities or leases we have been issues remain valid. If
41 * the session times out and goes stale, our leases and capabilities
42 * are no longer valid.
45 struct ceph_reconnect_state {
46 struct ceph_pagelist *pagelist;
50 static void __wake_requests(struct ceph_mds_client *mdsc,
51 struct list_head *head);
53 static const struct ceph_connection_operations mds_con_ops;
61 * parse individual inode info
63 static int parse_reply_info_in(void **p, void *end,
64 struct ceph_mds_reply_info_in *info,
70 *p += sizeof(struct ceph_mds_reply_inode) +
71 sizeof(*info->in->fragtree.splits) *
72 le32_to_cpu(info->in->fragtree.nsplits);
74 ceph_decode_32_safe(p, end, info->symlink_len, bad);
75 ceph_decode_need(p, end, info->symlink_len, bad);
77 *p += info->symlink_len;
79 if (features & CEPH_FEATURE_DIRLAYOUTHASH)
80 ceph_decode_copy_safe(p, end, &info->dir_layout,
81 sizeof(info->dir_layout), bad);
83 memset(&info->dir_layout, 0, sizeof(info->dir_layout));
85 ceph_decode_32_safe(p, end, info->xattr_len, bad);
86 ceph_decode_need(p, end, info->xattr_len, bad);
87 info->xattr_data = *p;
88 *p += info->xattr_len;
95 * parse a normal reply, which may contain a (dir+)dentry and/or a
98 static int parse_reply_info_trace(void **p, void *end,
99 struct ceph_mds_reply_info_parsed *info,
104 if (info->head->is_dentry) {
105 err = parse_reply_info_in(p, end, &info->diri, features);
109 if (unlikely(*p + sizeof(*info->dirfrag) > end))
112 *p += sizeof(*info->dirfrag) +
113 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
114 if (unlikely(*p > end))
117 ceph_decode_32_safe(p, end, info->dname_len, bad);
118 ceph_decode_need(p, end, info->dname_len, bad);
120 *p += info->dname_len;
122 *p += sizeof(*info->dlease);
125 if (info->head->is_target) {
126 err = parse_reply_info_in(p, end, &info->targeti, features);
131 if (unlikely(*p != end))
138 pr_err("problem parsing mds trace %d\n", err);
143 * parse readdir results
145 static int parse_reply_info_dir(void **p, void *end,
146 struct ceph_mds_reply_info_parsed *info,
153 if (*p + sizeof(*info->dir_dir) > end)
155 *p += sizeof(*info->dir_dir) +
156 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
160 ceph_decode_need(p, end, sizeof(num) + 2, bad);
161 num = ceph_decode_32(p);
162 info->dir_end = ceph_decode_8(p);
163 info->dir_complete = ceph_decode_8(p);
167 /* alloc large array */
169 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
170 sizeof(*info->dir_dname) +
171 sizeof(*info->dir_dname_len) +
172 sizeof(*info->dir_dlease),
174 if (info->dir_in == NULL) {
178 info->dir_dname = (void *)(info->dir_in + num);
179 info->dir_dname_len = (void *)(info->dir_dname + num);
180 info->dir_dlease = (void *)(info->dir_dname_len + num);
184 ceph_decode_need(p, end, sizeof(u32)*2, bad);
185 info->dir_dname_len[i] = ceph_decode_32(p);
186 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
187 info->dir_dname[i] = *p;
188 *p += info->dir_dname_len[i];
189 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
191 info->dir_dlease[i] = *p;
192 *p += sizeof(struct ceph_mds_reply_lease);
195 err = parse_reply_info_in(p, end, &info->dir_in[i], features);
210 pr_err("problem parsing dir contents %d\n", err);
215 * parse fcntl F_GETLK results
217 static int parse_reply_info_filelock(void **p, void *end,
218 struct ceph_mds_reply_info_parsed *info,
221 if (*p + sizeof(*info->filelock_reply) > end)
224 info->filelock_reply = *p;
225 *p += sizeof(*info->filelock_reply);
227 if (unlikely(*p != end))
236 * parse create results
238 static int parse_reply_info_create(void **p, void *end,
239 struct ceph_mds_reply_info_parsed *info,
242 if (features & CEPH_FEATURE_REPLY_CREATE_INODE) {
244 info->has_create_ino = false;
246 info->has_create_ino = true;
247 info->ino = ceph_decode_64(p);
251 if (unlikely(*p != end))
260 * parse extra results
262 static int parse_reply_info_extra(void **p, void *end,
263 struct ceph_mds_reply_info_parsed *info,
266 if (info->head->op == CEPH_MDS_OP_GETFILELOCK)
267 return parse_reply_info_filelock(p, end, info, features);
268 else if (info->head->op == CEPH_MDS_OP_READDIR)
269 return parse_reply_info_dir(p, end, info, features);
270 else if (info->head->op == CEPH_MDS_OP_CREATE)
271 return parse_reply_info_create(p, end, info, features);
277 * parse entire mds reply
279 static int parse_reply_info(struct ceph_msg *msg,
280 struct ceph_mds_reply_info_parsed *info,
287 info->head = msg->front.iov_base;
288 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
289 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
292 ceph_decode_32_safe(&p, end, len, bad);
294 ceph_decode_need(&p, end, len, bad);
295 err = parse_reply_info_trace(&p, p+len, info, features);
301 ceph_decode_32_safe(&p, end, len, bad);
303 ceph_decode_need(&p, end, len, bad);
304 err = parse_reply_info_extra(&p, p+len, info, features);
310 ceph_decode_32_safe(&p, end, len, bad);
311 info->snapblob_len = len;
322 pr_err("mds parse_reply err %d\n", err);
326 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
335 static const char *session_state_name(int s)
338 case CEPH_MDS_SESSION_NEW: return "new";
339 case CEPH_MDS_SESSION_OPENING: return "opening";
340 case CEPH_MDS_SESSION_OPEN: return "open";
341 case CEPH_MDS_SESSION_HUNG: return "hung";
342 case CEPH_MDS_SESSION_CLOSING: return "closing";
343 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
344 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
345 default: return "???";
349 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
351 if (atomic_inc_not_zero(&s->s_ref)) {
352 dout("mdsc get_session %p %d -> %d\n", s,
353 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
356 dout("mdsc get_session %p 0 -- FAIL", s);
361 void ceph_put_mds_session(struct ceph_mds_session *s)
363 dout("mdsc put_session %p %d -> %d\n", s,
364 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
365 if (atomic_dec_and_test(&s->s_ref)) {
366 if (s->s_auth.authorizer)
367 s->s_mdsc->fsc->client->monc.auth->ops->destroy_authorizer(
368 s->s_mdsc->fsc->client->monc.auth,
369 s->s_auth.authorizer);
375 * called under mdsc->mutex
377 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
380 struct ceph_mds_session *session;
382 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
384 session = mdsc->sessions[mds];
385 dout("lookup_mds_session %p %d\n", session,
386 atomic_read(&session->s_ref));
387 get_session(session);
391 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
393 if (mds >= mdsc->max_sessions)
395 return mdsc->sessions[mds];
398 static int __verify_registered_session(struct ceph_mds_client *mdsc,
399 struct ceph_mds_session *s)
401 if (s->s_mds >= mdsc->max_sessions ||
402 mdsc->sessions[s->s_mds] != s)
408 * create+register a new session for given mds.
409 * called under mdsc->mutex.
411 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
414 struct ceph_mds_session *s;
416 s = kzalloc(sizeof(*s), GFP_NOFS);
418 return ERR_PTR(-ENOMEM);
421 s->s_state = CEPH_MDS_SESSION_NEW;
424 mutex_init(&s->s_mutex);
426 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
428 spin_lock_init(&s->s_gen_ttl_lock);
430 s->s_cap_ttl = jiffies - 1;
432 spin_lock_init(&s->s_cap_lock);
433 s->s_renew_requested = 0;
435 INIT_LIST_HEAD(&s->s_caps);
438 atomic_set(&s->s_ref, 1);
439 INIT_LIST_HEAD(&s->s_waiting);
440 INIT_LIST_HEAD(&s->s_unsafe);
441 s->s_num_cap_releases = 0;
442 s->s_cap_iterator = NULL;
443 INIT_LIST_HEAD(&s->s_cap_releases);
444 INIT_LIST_HEAD(&s->s_cap_releases_done);
445 INIT_LIST_HEAD(&s->s_cap_flushing);
446 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
448 dout("register_session mds%d\n", mds);
449 if (mds >= mdsc->max_sessions) {
450 int newmax = 1 << get_count_order(mds+1);
451 struct ceph_mds_session **sa;
453 dout("register_session realloc to %d\n", newmax);
454 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
457 if (mdsc->sessions) {
458 memcpy(sa, mdsc->sessions,
459 mdsc->max_sessions * sizeof(void *));
460 kfree(mdsc->sessions);
463 mdsc->max_sessions = newmax;
465 mdsc->sessions[mds] = s;
466 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
468 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
469 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
475 return ERR_PTR(-ENOMEM);
479 * called under mdsc->mutex
481 static void __unregister_session(struct ceph_mds_client *mdsc,
482 struct ceph_mds_session *s)
484 dout("__unregister_session mds%d %p\n", s->s_mds, s);
485 BUG_ON(mdsc->sessions[s->s_mds] != s);
486 mdsc->sessions[s->s_mds] = NULL;
487 ceph_con_close(&s->s_con);
488 ceph_put_mds_session(s);
492 * drop session refs in request.
494 * should be last request ref, or hold mdsc->mutex
496 static void put_request_session(struct ceph_mds_request *req)
498 if (req->r_session) {
499 ceph_put_mds_session(req->r_session);
500 req->r_session = NULL;
504 void ceph_mdsc_release_request(struct kref *kref)
506 struct ceph_mds_request *req = container_of(kref,
507 struct ceph_mds_request,
510 ceph_msg_put(req->r_request);
512 ceph_msg_put(req->r_reply);
513 destroy_reply_info(&req->r_reply_info);
516 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
519 if (req->r_locked_dir)
520 ceph_put_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
521 if (req->r_target_inode)
522 iput(req->r_target_inode);
525 if (req->r_old_dentry) {
527 * track (and drop pins for) r_old_dentry_dir
528 * separately, since r_old_dentry's d_parent may have
529 * changed between the dir mutex being dropped and
530 * this request being freed.
532 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
534 dput(req->r_old_dentry);
535 iput(req->r_old_dentry_dir);
539 put_request_session(req);
540 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
545 * lookup session, bump ref if found.
547 * called under mdsc->mutex.
549 static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
552 struct ceph_mds_request *req;
553 struct rb_node *n = mdsc->request_tree.rb_node;
556 req = rb_entry(n, struct ceph_mds_request, r_node);
557 if (tid < req->r_tid)
559 else if (tid > req->r_tid)
562 ceph_mdsc_get_request(req);
569 static void __insert_request(struct ceph_mds_client *mdsc,
570 struct ceph_mds_request *new)
572 struct rb_node **p = &mdsc->request_tree.rb_node;
573 struct rb_node *parent = NULL;
574 struct ceph_mds_request *req = NULL;
578 req = rb_entry(parent, struct ceph_mds_request, r_node);
579 if (new->r_tid < req->r_tid)
581 else if (new->r_tid > req->r_tid)
587 rb_link_node(&new->r_node, parent, p);
588 rb_insert_color(&new->r_node, &mdsc->request_tree);
592 * Register an in-flight request, and assign a tid. Link to directory
593 * are modifying (if any).
595 * Called under mdsc->mutex.
597 static void __register_request(struct ceph_mds_client *mdsc,
598 struct ceph_mds_request *req,
601 req->r_tid = ++mdsc->last_tid;
603 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
605 dout("__register_request %p tid %lld\n", req, req->r_tid);
606 ceph_mdsc_get_request(req);
607 __insert_request(mdsc, req);
609 req->r_uid = current_fsuid();
610 req->r_gid = current_fsgid();
613 struct ceph_inode_info *ci = ceph_inode(dir);
616 spin_lock(&ci->i_unsafe_lock);
617 req->r_unsafe_dir = dir;
618 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
619 spin_unlock(&ci->i_unsafe_lock);
623 static void __unregister_request(struct ceph_mds_client *mdsc,
624 struct ceph_mds_request *req)
626 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
627 rb_erase(&req->r_node, &mdsc->request_tree);
628 RB_CLEAR_NODE(&req->r_node);
630 if (req->r_unsafe_dir) {
631 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
633 spin_lock(&ci->i_unsafe_lock);
634 list_del_init(&req->r_unsafe_dir_item);
635 spin_unlock(&ci->i_unsafe_lock);
637 iput(req->r_unsafe_dir);
638 req->r_unsafe_dir = NULL;
641 ceph_mdsc_put_request(req);
645 * Choose mds to send request to next. If there is a hint set in the
646 * request (e.g., due to a prior forward hint from the mds), use that.
647 * Otherwise, consult frag tree and/or caps to identify the
648 * appropriate mds. If all else fails, choose randomly.
650 * Called under mdsc->mutex.
652 static struct dentry *get_nonsnap_parent(struct dentry *dentry)
655 * we don't need to worry about protecting the d_parent access
656 * here because we never renaming inside the snapped namespace
657 * except to resplice to another snapdir, and either the old or new
658 * result is a valid result.
660 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
661 dentry = dentry->d_parent;
665 static int __choose_mds(struct ceph_mds_client *mdsc,
666 struct ceph_mds_request *req)
669 struct ceph_inode_info *ci;
670 struct ceph_cap *cap;
671 int mode = req->r_direct_mode;
673 u32 hash = req->r_direct_hash;
674 bool is_hash = req->r_direct_is_hash;
677 * is there a specific mds we should try? ignore hint if we have
678 * no session and the mds is not up (active or recovering).
680 if (req->r_resend_mds >= 0 &&
681 (__have_session(mdsc, req->r_resend_mds) ||
682 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
683 dout("choose_mds using resend_mds mds%d\n",
685 return req->r_resend_mds;
688 if (mode == USE_RANDOM_MDS)
693 inode = req->r_inode;
694 } else if (req->r_dentry) {
695 /* ignore race with rename; old or new d_parent is okay */
696 struct dentry *parent = req->r_dentry->d_parent;
697 struct inode *dir = parent->d_inode;
699 if (dir->i_sb != mdsc->fsc->sb) {
701 inode = req->r_dentry->d_inode;
702 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
703 /* direct snapped/virtual snapdir requests
704 * based on parent dir inode */
705 struct dentry *dn = get_nonsnap_parent(parent);
707 dout("__choose_mds using nonsnap parent %p\n", inode);
708 } else if (req->r_dentry->d_inode) {
710 inode = req->r_dentry->d_inode;
714 hash = ceph_dentry_hash(dir, req->r_dentry);
719 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
723 ci = ceph_inode(inode);
725 if (is_hash && S_ISDIR(inode->i_mode)) {
726 struct ceph_inode_frag frag;
729 ceph_choose_frag(ci, hash, &frag, &found);
731 if (mode == USE_ANY_MDS && frag.ndist > 0) {
734 /* choose a random replica */
735 get_random_bytes(&r, 1);
738 dout("choose_mds %p %llx.%llx "
739 "frag %u mds%d (%d/%d)\n",
740 inode, ceph_vinop(inode),
743 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
744 CEPH_MDS_STATE_ACTIVE)
748 /* since this file/dir wasn't known to be
749 * replicated, then we want to look for the
750 * authoritative mds. */
753 /* choose auth mds */
755 dout("choose_mds %p %llx.%llx "
756 "frag %u mds%d (auth)\n",
757 inode, ceph_vinop(inode), frag.frag, mds);
758 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
759 CEPH_MDS_STATE_ACTIVE)
765 spin_lock(&ci->i_ceph_lock);
767 if (mode == USE_AUTH_MDS)
768 cap = ci->i_auth_cap;
769 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
770 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
772 spin_unlock(&ci->i_ceph_lock);
775 mds = cap->session->s_mds;
776 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
777 inode, ceph_vinop(inode), mds,
778 cap == ci->i_auth_cap ? "auth " : "", cap);
779 spin_unlock(&ci->i_ceph_lock);
783 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
784 dout("choose_mds chose random mds%d\n", mds);
792 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
794 struct ceph_msg *msg;
795 struct ceph_mds_session_head *h;
797 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
800 pr_err("create_session_msg ENOMEM creating msg\n");
803 h = msg->front.iov_base;
804 h->op = cpu_to_le32(op);
805 h->seq = cpu_to_le64(seq);
810 * send session open request.
812 * called under mdsc->mutex
814 static int __open_session(struct ceph_mds_client *mdsc,
815 struct ceph_mds_session *session)
817 struct ceph_msg *msg;
819 int mds = session->s_mds;
821 /* wait for mds to go active? */
822 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
823 dout("open_session to mds%d (%s)\n", mds,
824 ceph_mds_state_name(mstate));
825 session->s_state = CEPH_MDS_SESSION_OPENING;
826 session->s_renew_requested = jiffies;
828 /* send connect message */
829 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
832 ceph_con_send(&session->s_con, msg);
837 * open sessions for any export targets for the given mds
839 * called under mdsc->mutex
841 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
842 struct ceph_mds_session *session)
844 struct ceph_mds_info *mi;
845 struct ceph_mds_session *ts;
846 int i, mds = session->s_mds;
849 if (mds >= mdsc->mdsmap->m_max_mds)
851 mi = &mdsc->mdsmap->m_info[mds];
852 dout("open_export_target_sessions for mds%d (%d targets)\n",
853 session->s_mds, mi->num_export_targets);
855 for (i = 0; i < mi->num_export_targets; i++) {
856 target = mi->export_targets[i];
857 ts = __ceph_lookup_mds_session(mdsc, target);
859 ts = register_session(mdsc, target);
863 if (session->s_state == CEPH_MDS_SESSION_NEW ||
864 session->s_state == CEPH_MDS_SESSION_CLOSING)
865 __open_session(mdsc, session);
867 dout(" mds%d target mds%d %p is %s\n", session->s_mds,
868 i, ts, session_state_name(ts->s_state));
869 ceph_put_mds_session(ts);
873 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
874 struct ceph_mds_session *session)
876 mutex_lock(&mdsc->mutex);
877 __open_export_target_sessions(mdsc, session);
878 mutex_unlock(&mdsc->mutex);
886 * Free preallocated cap messages assigned to this session
888 static void cleanup_cap_releases(struct ceph_mds_session *session)
890 struct ceph_msg *msg;
892 spin_lock(&session->s_cap_lock);
893 while (!list_empty(&session->s_cap_releases)) {
894 msg = list_first_entry(&session->s_cap_releases,
895 struct ceph_msg, list_head);
896 list_del_init(&msg->list_head);
899 while (!list_empty(&session->s_cap_releases_done)) {
900 msg = list_first_entry(&session->s_cap_releases_done,
901 struct ceph_msg, list_head);
902 list_del_init(&msg->list_head);
905 spin_unlock(&session->s_cap_lock);
909 * Helper to safely iterate over all caps associated with a session, with
910 * special care taken to handle a racing __ceph_remove_cap().
912 * Caller must hold session s_mutex.
914 static int iterate_session_caps(struct ceph_mds_session *session,
915 int (*cb)(struct inode *, struct ceph_cap *,
919 struct ceph_cap *cap;
920 struct inode *inode, *last_inode = NULL;
921 struct ceph_cap *old_cap = NULL;
924 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
925 spin_lock(&session->s_cap_lock);
926 p = session->s_caps.next;
927 while (p != &session->s_caps) {
928 cap = list_entry(p, struct ceph_cap, session_caps);
929 inode = igrab(&cap->ci->vfs_inode);
934 session->s_cap_iterator = cap;
935 spin_unlock(&session->s_cap_lock);
942 ceph_put_cap(session->s_mdsc, old_cap);
946 ret = cb(inode, cap, arg);
949 spin_lock(&session->s_cap_lock);
951 if (cap->ci == NULL) {
952 dout("iterate_session_caps finishing cap %p removal\n",
954 BUG_ON(cap->session != session);
955 list_del_init(&cap->session_caps);
956 session->s_nr_caps--;
958 old_cap = cap; /* put_cap it w/o locks held */
965 session->s_cap_iterator = NULL;
966 spin_unlock(&session->s_cap_lock);
971 ceph_put_cap(session->s_mdsc, old_cap);
976 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
979 struct ceph_inode_info *ci = ceph_inode(inode);
982 dout("removing cap %p, ci is %p, inode is %p\n",
983 cap, ci, &ci->vfs_inode);
984 spin_lock(&ci->i_ceph_lock);
985 __ceph_remove_cap(cap);
986 if (!__ceph_is_any_real_caps(ci)) {
987 struct ceph_mds_client *mdsc =
988 ceph_sb_to_client(inode->i_sb)->mdsc;
990 spin_lock(&mdsc->cap_dirty_lock);
991 if (!list_empty(&ci->i_dirty_item)) {
992 pr_info(" dropping dirty %s state for %p %lld\n",
993 ceph_cap_string(ci->i_dirty_caps),
994 inode, ceph_ino(inode));
995 ci->i_dirty_caps = 0;
996 list_del_init(&ci->i_dirty_item);
999 if (!list_empty(&ci->i_flushing_item)) {
1000 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
1001 ceph_cap_string(ci->i_flushing_caps),
1002 inode, ceph_ino(inode));
1003 ci->i_flushing_caps = 0;
1004 list_del_init(&ci->i_flushing_item);
1005 mdsc->num_cap_flushing--;
1008 if (drop && ci->i_wrbuffer_ref) {
1009 pr_info(" dropping dirty data for %p %lld\n",
1010 inode, ceph_ino(inode));
1011 ci->i_wrbuffer_ref = 0;
1012 ci->i_wrbuffer_ref_head = 0;
1015 spin_unlock(&mdsc->cap_dirty_lock);
1017 spin_unlock(&ci->i_ceph_lock);
1024 * caller must hold session s_mutex
1026 static void remove_session_caps(struct ceph_mds_session *session)
1028 dout("remove_session_caps on %p\n", session);
1029 iterate_session_caps(session, remove_session_caps_cb, NULL);
1030 BUG_ON(session->s_nr_caps > 0);
1031 BUG_ON(!list_empty(&session->s_cap_flushing));
1032 cleanup_cap_releases(session);
1036 * wake up any threads waiting on this session's caps. if the cap is
1037 * old (didn't get renewed on the client reconnect), remove it now.
1039 * caller must hold s_mutex.
1041 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1044 struct ceph_inode_info *ci = ceph_inode(inode);
1046 wake_up_all(&ci->i_cap_wq);
1048 spin_lock(&ci->i_ceph_lock);
1049 ci->i_wanted_max_size = 0;
1050 ci->i_requested_max_size = 0;
1051 spin_unlock(&ci->i_ceph_lock);
1056 static void wake_up_session_caps(struct ceph_mds_session *session,
1059 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1060 iterate_session_caps(session, wake_up_session_cb,
1061 (void *)(unsigned long)reconnect);
1065 * Send periodic message to MDS renewing all currently held caps. The
1066 * ack will reset the expiration for all caps from this session.
1068 * caller holds s_mutex
1070 static int send_renew_caps(struct ceph_mds_client *mdsc,
1071 struct ceph_mds_session *session)
1073 struct ceph_msg *msg;
1076 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1077 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1078 pr_info("mds%d caps stale\n", session->s_mds);
1079 session->s_renew_requested = jiffies;
1081 /* do not try to renew caps until a recovering mds has reconnected
1082 * with its clients. */
1083 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1084 if (state < CEPH_MDS_STATE_RECONNECT) {
1085 dout("send_renew_caps ignoring mds%d (%s)\n",
1086 session->s_mds, ceph_mds_state_name(state));
1090 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1091 ceph_mds_state_name(state));
1092 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1093 ++session->s_renew_seq);
1096 ceph_con_send(&session->s_con, msg);
1101 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1103 * Called under session->s_mutex
1105 static void renewed_caps(struct ceph_mds_client *mdsc,
1106 struct ceph_mds_session *session, int is_renew)
1111 spin_lock(&session->s_cap_lock);
1112 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
1114 session->s_cap_ttl = session->s_renew_requested +
1115 mdsc->mdsmap->m_session_timeout*HZ;
1118 if (time_before(jiffies, session->s_cap_ttl)) {
1119 pr_info("mds%d caps renewed\n", session->s_mds);
1122 pr_info("mds%d caps still stale\n", session->s_mds);
1125 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1126 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1127 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1128 spin_unlock(&session->s_cap_lock);
1131 wake_up_session_caps(session, 0);
1135 * send a session close request
1137 static int request_close_session(struct ceph_mds_client *mdsc,
1138 struct ceph_mds_session *session)
1140 struct ceph_msg *msg;
1142 dout("request_close_session mds%d state %s seq %lld\n",
1143 session->s_mds, session_state_name(session->s_state),
1145 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1148 ceph_con_send(&session->s_con, msg);
1153 * Called with s_mutex held.
1155 static int __close_session(struct ceph_mds_client *mdsc,
1156 struct ceph_mds_session *session)
1158 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1160 session->s_state = CEPH_MDS_SESSION_CLOSING;
1161 return request_close_session(mdsc, session);
1165 * Trim old(er) caps.
1167 * Because we can't cache an inode without one or more caps, we do
1168 * this indirectly: if a cap is unused, we prune its aliases, at which
1169 * point the inode will hopefully get dropped to.
1171 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1172 * memory pressure from the MDS, though, so it needn't be perfect.
1174 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1176 struct ceph_mds_session *session = arg;
1177 struct ceph_inode_info *ci = ceph_inode(inode);
1178 int used, oissued, mine;
1180 if (session->s_trim_caps <= 0)
1183 spin_lock(&ci->i_ceph_lock);
1184 mine = cap->issued | cap->implemented;
1185 used = __ceph_caps_used(ci);
1186 oissued = __ceph_caps_issued_other(ci, cap);
1188 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1189 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1190 ceph_cap_string(used));
1191 if (ci->i_dirty_caps)
1192 goto out; /* dirty caps */
1193 if ((used & ~oissued) & mine)
1194 goto out; /* we need these caps */
1196 session->s_trim_caps--;
1198 /* we aren't the only cap.. just remove us */
1199 __ceph_remove_cap(cap);
1201 /* try to drop referring dentries */
1202 spin_unlock(&ci->i_ceph_lock);
1203 d_prune_aliases(inode);
1204 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1205 inode, cap, atomic_read(&inode->i_count));
1210 spin_unlock(&ci->i_ceph_lock);
1215 * Trim session cap count down to some max number.
1217 static int trim_caps(struct ceph_mds_client *mdsc,
1218 struct ceph_mds_session *session,
1221 int trim_caps = session->s_nr_caps - max_caps;
1223 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1224 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1225 if (trim_caps > 0) {
1226 session->s_trim_caps = trim_caps;
1227 iterate_session_caps(session, trim_caps_cb, session);
1228 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1229 session->s_mds, session->s_nr_caps, max_caps,
1230 trim_caps - session->s_trim_caps);
1231 session->s_trim_caps = 0;
1237 * Allocate cap_release messages. If there is a partially full message
1238 * in the queue, try to allocate enough to cover it's remainder, so that
1239 * we can send it immediately.
1241 * Called under s_mutex.
1243 int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
1244 struct ceph_mds_session *session)
1246 struct ceph_msg *msg, *partial = NULL;
1247 struct ceph_mds_cap_release *head;
1249 int extra = mdsc->fsc->mount_options->cap_release_safety;
1252 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1255 spin_lock(&session->s_cap_lock);
1257 if (!list_empty(&session->s_cap_releases)) {
1258 msg = list_first_entry(&session->s_cap_releases,
1261 head = msg->front.iov_base;
1262 num = le32_to_cpu(head->num);
1264 dout(" partial %p with (%d/%d)\n", msg, num,
1265 (int)CEPH_CAPS_PER_RELEASE);
1266 extra += CEPH_CAPS_PER_RELEASE - num;
1270 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1271 spin_unlock(&session->s_cap_lock);
1272 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1276 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1277 (int)msg->front.iov_len);
1278 head = msg->front.iov_base;
1279 head->num = cpu_to_le32(0);
1280 msg->front.iov_len = sizeof(*head);
1281 spin_lock(&session->s_cap_lock);
1282 list_add(&msg->list_head, &session->s_cap_releases);
1283 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1287 head = partial->front.iov_base;
1288 num = le32_to_cpu(head->num);
1289 dout(" queueing partial %p with %d/%d\n", partial, num,
1290 (int)CEPH_CAPS_PER_RELEASE);
1291 list_move_tail(&partial->list_head,
1292 &session->s_cap_releases_done);
1293 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
1296 spin_unlock(&session->s_cap_lock);
1302 * flush all dirty inode data to disk.
1304 * returns true if we've flushed through want_flush_seq
1306 static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1310 dout("check_cap_flush want %lld\n", want_flush_seq);
1311 mutex_lock(&mdsc->mutex);
1312 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1313 struct ceph_mds_session *session = mdsc->sessions[mds];
1317 get_session(session);
1318 mutex_unlock(&mdsc->mutex);
1320 mutex_lock(&session->s_mutex);
1321 if (!list_empty(&session->s_cap_flushing)) {
1322 struct ceph_inode_info *ci =
1323 list_entry(session->s_cap_flushing.next,
1324 struct ceph_inode_info,
1326 struct inode *inode = &ci->vfs_inode;
1328 spin_lock(&ci->i_ceph_lock);
1329 if (ci->i_cap_flush_seq <= want_flush_seq) {
1330 dout("check_cap_flush still flushing %p "
1331 "seq %lld <= %lld to mds%d\n", inode,
1332 ci->i_cap_flush_seq, want_flush_seq,
1336 spin_unlock(&ci->i_ceph_lock);
1338 mutex_unlock(&session->s_mutex);
1339 ceph_put_mds_session(session);
1343 mutex_lock(&mdsc->mutex);
1346 mutex_unlock(&mdsc->mutex);
1347 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1352 * called under s_mutex
1354 void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1355 struct ceph_mds_session *session)
1357 struct ceph_msg *msg;
1359 dout("send_cap_releases mds%d\n", session->s_mds);
1360 spin_lock(&session->s_cap_lock);
1361 while (!list_empty(&session->s_cap_releases_done)) {
1362 msg = list_first_entry(&session->s_cap_releases_done,
1363 struct ceph_msg, list_head);
1364 list_del_init(&msg->list_head);
1365 spin_unlock(&session->s_cap_lock);
1366 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1367 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1368 ceph_con_send(&session->s_con, msg);
1369 spin_lock(&session->s_cap_lock);
1371 spin_unlock(&session->s_cap_lock);
1374 static void discard_cap_releases(struct ceph_mds_client *mdsc,
1375 struct ceph_mds_session *session)
1377 struct ceph_msg *msg;
1378 struct ceph_mds_cap_release *head;
1381 dout("discard_cap_releases mds%d\n", session->s_mds);
1382 spin_lock(&session->s_cap_lock);
1384 /* zero out the in-progress message */
1385 msg = list_first_entry(&session->s_cap_releases,
1386 struct ceph_msg, list_head);
1387 head = msg->front.iov_base;
1388 num = le32_to_cpu(head->num);
1389 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1390 head->num = cpu_to_le32(0);
1391 session->s_num_cap_releases += num;
1393 /* requeue completed messages */
1394 while (!list_empty(&session->s_cap_releases_done)) {
1395 msg = list_first_entry(&session->s_cap_releases_done,
1396 struct ceph_msg, list_head);
1397 list_del_init(&msg->list_head);
1399 head = msg->front.iov_base;
1400 num = le32_to_cpu(head->num);
1401 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1403 session->s_num_cap_releases += num;
1404 head->num = cpu_to_le32(0);
1405 msg->front.iov_len = sizeof(*head);
1406 list_add(&msg->list_head, &session->s_cap_releases);
1409 spin_unlock(&session->s_cap_lock);
1417 * Create an mds request.
1419 struct ceph_mds_request *
1420 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1422 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1425 return ERR_PTR(-ENOMEM);
1427 mutex_init(&req->r_fill_mutex);
1429 req->r_started = jiffies;
1430 req->r_resend_mds = -1;
1431 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1433 kref_init(&req->r_kref);
1434 INIT_LIST_HEAD(&req->r_wait);
1435 init_completion(&req->r_completion);
1436 init_completion(&req->r_safe_completion);
1437 INIT_LIST_HEAD(&req->r_unsafe_item);
1440 req->r_direct_mode = mode;
1445 * return oldest (lowest) request, tid in request tree, 0 if none.
1447 * called under mdsc->mutex.
1449 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1451 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1453 return rb_entry(rb_first(&mdsc->request_tree),
1454 struct ceph_mds_request, r_node);
1457 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1459 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1467 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1468 * on build_path_from_dentry in fs/cifs/dir.c.
1470 * If @stop_on_nosnap, generate path relative to the first non-snapped
1473 * Encode hidden .snap dirs as a double /, i.e.
1474 * foo/.snap/bar -> foo//bar
1476 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1479 struct dentry *temp;
1485 return ERR_PTR(-EINVAL);
1489 seq = read_seqbegin(&rename_lock);
1491 for (temp = dentry; !IS_ROOT(temp);) {
1492 struct inode *inode = temp->d_inode;
1493 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1494 len++; /* slash only */
1495 else if (stop_on_nosnap && inode &&
1496 ceph_snap(inode) == CEPH_NOSNAP)
1499 len += 1 + temp->d_name.len;
1500 temp = temp->d_parent;
1504 len--; /* no leading '/' */
1506 path = kmalloc(len+1, GFP_NOFS);
1508 return ERR_PTR(-ENOMEM);
1510 path[pos] = 0; /* trailing null */
1512 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1513 struct inode *inode;
1515 spin_lock(&temp->d_lock);
1516 inode = temp->d_inode;
1517 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1518 dout("build_path path+%d: %p SNAPDIR\n",
1520 } else if (stop_on_nosnap && inode &&
1521 ceph_snap(inode) == CEPH_NOSNAP) {
1522 spin_unlock(&temp->d_lock);
1525 pos -= temp->d_name.len;
1527 spin_unlock(&temp->d_lock);
1530 strncpy(path + pos, temp->d_name.name,
1533 spin_unlock(&temp->d_lock);
1536 temp = temp->d_parent;
1539 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
1540 pr_err("build_path did not end path lookup where "
1541 "expected, namelen is %d, pos is %d\n", len, pos);
1542 /* presumably this is only possible if racing with a
1543 rename of one of the parent directories (we can not
1544 lock the dentries above us to prevent this, but
1545 retrying should be harmless) */
1550 *base = ceph_ino(temp->d_inode);
1552 dout("build_path on %p %d built %llx '%.*s'\n",
1553 dentry, dentry->d_count, *base, len, path);
1557 static int build_dentry_path(struct dentry *dentry,
1558 const char **ppath, int *ppathlen, u64 *pino,
1563 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1564 *pino = ceph_ino(dentry->d_parent->d_inode);
1565 *ppath = dentry->d_name.name;
1566 *ppathlen = dentry->d_name.len;
1569 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1571 return PTR_ERR(path);
1577 static int build_inode_path(struct inode *inode,
1578 const char **ppath, int *ppathlen, u64 *pino,
1581 struct dentry *dentry;
1584 if (ceph_snap(inode) == CEPH_NOSNAP) {
1585 *pino = ceph_ino(inode);
1589 dentry = d_find_alias(inode);
1590 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1593 return PTR_ERR(path);
1600 * request arguments may be specified via an inode *, a dentry *, or
1601 * an explicit ino+path.
1603 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1604 const char *rpath, u64 rino,
1605 const char **ppath, int *pathlen,
1606 u64 *ino, int *freepath)
1611 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1612 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1614 } else if (rdentry) {
1615 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1616 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1618 } else if (rpath || rino) {
1621 *pathlen = rpath ? strlen(rpath) : 0;
1622 dout(" path %.*s\n", *pathlen, rpath);
1629 * called under mdsc->mutex
1631 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1632 struct ceph_mds_request *req,
1635 struct ceph_msg *msg;
1636 struct ceph_mds_request_head *head;
1637 const char *path1 = NULL;
1638 const char *path2 = NULL;
1639 u64 ino1 = 0, ino2 = 0;
1640 int pathlen1 = 0, pathlen2 = 0;
1641 int freepath1 = 0, freepath2 = 0;
1647 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1648 req->r_path1, req->r_ino1.ino,
1649 &path1, &pathlen1, &ino1, &freepath1);
1655 ret = set_request_path_attr(NULL, req->r_old_dentry,
1656 req->r_path2, req->r_ino2.ino,
1657 &path2, &pathlen2, &ino2, &freepath2);
1663 len = sizeof(*head) +
1664 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
1666 /* calculate (max) length for cap releases */
1667 len += sizeof(struct ceph_mds_request_release) *
1668 (!!req->r_inode_drop + !!req->r_dentry_drop +
1669 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1670 if (req->r_dentry_drop)
1671 len += req->r_dentry->d_name.len;
1672 if (req->r_old_dentry_drop)
1673 len += req->r_old_dentry->d_name.len;
1675 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
1677 msg = ERR_PTR(-ENOMEM);
1681 msg->hdr.tid = cpu_to_le64(req->r_tid);
1683 head = msg->front.iov_base;
1684 p = msg->front.iov_base + sizeof(*head);
1685 end = msg->front.iov_base + msg->front.iov_len;
1687 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1688 head->op = cpu_to_le32(req->r_op);
1689 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
1690 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
1691 head->args = req->r_args;
1693 ceph_encode_filepath(&p, end, ino1, path1);
1694 ceph_encode_filepath(&p, end, ino2, path2);
1696 /* make note of release offset, in case we need to replay */
1697 req->r_request_release_offset = p - msg->front.iov_base;
1701 if (req->r_inode_drop)
1702 releases += ceph_encode_inode_release(&p,
1703 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1704 mds, req->r_inode_drop, req->r_inode_unless, 0);
1705 if (req->r_dentry_drop)
1706 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1707 mds, req->r_dentry_drop, req->r_dentry_unless);
1708 if (req->r_old_dentry_drop)
1709 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1710 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1711 if (req->r_old_inode_drop)
1712 releases += ceph_encode_inode_release(&p,
1713 req->r_old_dentry->d_inode,
1714 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1715 head->num_releases = cpu_to_le16(releases);
1718 msg->front.iov_len = p - msg->front.iov_base;
1719 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1721 msg->pages = req->r_pages;
1722 msg->nr_pages = req->r_num_pages;
1723 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1724 msg->hdr.data_off = cpu_to_le16(0);
1728 kfree((char *)path2);
1731 kfree((char *)path1);
1737 * called under mdsc->mutex if error, under no mutex if
1740 static void complete_request(struct ceph_mds_client *mdsc,
1741 struct ceph_mds_request *req)
1743 if (req->r_callback)
1744 req->r_callback(mdsc, req);
1746 complete_all(&req->r_completion);
1750 * called under mdsc->mutex
1752 static int __prepare_send_request(struct ceph_mds_client *mdsc,
1753 struct ceph_mds_request *req,
1756 struct ceph_mds_request_head *rhead;
1757 struct ceph_msg *msg;
1762 struct ceph_cap *cap =
1763 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1766 req->r_sent_on_mseq = cap->mseq;
1768 req->r_sent_on_mseq = -1;
1770 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1771 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1773 if (req->r_got_unsafe) {
1775 * Replay. Do not regenerate message (and rebuild
1776 * paths, etc.); just use the original message.
1777 * Rebuilding paths will break for renames because
1778 * d_move mangles the src name.
1780 msg = req->r_request;
1781 rhead = msg->front.iov_base;
1783 flags = le32_to_cpu(rhead->flags);
1784 flags |= CEPH_MDS_FLAG_REPLAY;
1785 rhead->flags = cpu_to_le32(flags);
1787 if (req->r_target_inode)
1788 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1790 rhead->num_retry = req->r_attempts - 1;
1792 /* remove cap/dentry releases from message */
1793 rhead->num_releases = 0;
1794 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1795 msg->front.iov_len = req->r_request_release_offset;
1799 if (req->r_request) {
1800 ceph_msg_put(req->r_request);
1801 req->r_request = NULL;
1803 msg = create_request_message(mdsc, req, mds);
1805 req->r_err = PTR_ERR(msg);
1806 complete_request(mdsc, req);
1807 return PTR_ERR(msg);
1809 req->r_request = msg;
1811 rhead = msg->front.iov_base;
1812 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1813 if (req->r_got_unsafe)
1814 flags |= CEPH_MDS_FLAG_REPLAY;
1815 if (req->r_locked_dir)
1816 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1817 rhead->flags = cpu_to_le32(flags);
1818 rhead->num_fwd = req->r_num_fwd;
1819 rhead->num_retry = req->r_attempts - 1;
1822 dout(" r_locked_dir = %p\n", req->r_locked_dir);
1827 * send request, or put it on the appropriate wait list.
1829 static int __do_request(struct ceph_mds_client *mdsc,
1830 struct ceph_mds_request *req)
1832 struct ceph_mds_session *session = NULL;
1836 if (req->r_err || req->r_got_result)
1839 if (req->r_timeout &&
1840 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1841 dout("do_request timed out\n");
1846 put_request_session(req);
1848 mds = __choose_mds(mdsc, req);
1850 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1851 dout("do_request no mds or not active, waiting for map\n");
1852 list_add(&req->r_wait, &mdsc->waiting_for_map);
1856 /* get, open session */
1857 session = __ceph_lookup_mds_session(mdsc, mds);
1859 session = register_session(mdsc, mds);
1860 if (IS_ERR(session)) {
1861 err = PTR_ERR(session);
1865 req->r_session = get_session(session);
1867 dout("do_request mds%d session %p state %s\n", mds, session,
1868 session_state_name(session->s_state));
1869 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1870 session->s_state != CEPH_MDS_SESSION_HUNG) {
1871 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1872 session->s_state == CEPH_MDS_SESSION_CLOSING)
1873 __open_session(mdsc, session);
1874 list_add(&req->r_wait, &session->s_waiting);
1879 req->r_resend_mds = -1; /* forget any previous mds hint */
1881 if (req->r_request_started == 0) /* note request start time */
1882 req->r_request_started = jiffies;
1884 err = __prepare_send_request(mdsc, req, mds);
1886 ceph_msg_get(req->r_request);
1887 ceph_con_send(&session->s_con, req->r_request);
1891 ceph_put_mds_session(session);
1897 complete_request(mdsc, req);
1902 * called under mdsc->mutex
1904 static void __wake_requests(struct ceph_mds_client *mdsc,
1905 struct list_head *head)
1907 struct ceph_mds_request *req;
1908 LIST_HEAD(tmp_list);
1910 list_splice_init(head, &tmp_list);
1912 while (!list_empty(&tmp_list)) {
1913 req = list_entry(tmp_list.next,
1914 struct ceph_mds_request, r_wait);
1915 list_del_init(&req->r_wait);
1916 __do_request(mdsc, req);
1921 * Wake up threads with requests pending for @mds, so that they can
1922 * resubmit their requests to a possibly different mds.
1924 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
1926 struct ceph_mds_request *req;
1929 dout("kick_requests mds%d\n", mds);
1930 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1931 req = rb_entry(p, struct ceph_mds_request, r_node);
1932 if (req->r_got_unsafe)
1934 if (req->r_session &&
1935 req->r_session->s_mds == mds) {
1936 dout(" kicking tid %llu\n", req->r_tid);
1937 __do_request(mdsc, req);
1942 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1943 struct ceph_mds_request *req)
1945 dout("submit_request on %p\n", req);
1946 mutex_lock(&mdsc->mutex);
1947 __register_request(mdsc, req, NULL);
1948 __do_request(mdsc, req);
1949 mutex_unlock(&mdsc->mutex);
1953 * Synchrously perform an mds request. Take care of all of the
1954 * session setup, forwarding, retry details.
1956 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1958 struct ceph_mds_request *req)
1962 dout("do_request on %p\n", req);
1964 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1966 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1967 if (req->r_locked_dir)
1968 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1969 if (req->r_old_dentry)
1970 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
1974 mutex_lock(&mdsc->mutex);
1975 __register_request(mdsc, req, dir);
1976 __do_request(mdsc, req);
1980 __unregister_request(mdsc, req);
1981 dout("do_request early error %d\n", err);
1986 mutex_unlock(&mdsc->mutex);
1987 dout("do_request waiting\n");
1988 if (req->r_timeout) {
1989 err = (long)wait_for_completion_killable_timeout(
1990 &req->r_completion, req->r_timeout);
1994 err = wait_for_completion_killable(&req->r_completion);
1996 dout("do_request waited, got %d\n", err);
1997 mutex_lock(&mdsc->mutex);
1999 /* only abort if we didn't race with a real reply */
2000 if (req->r_got_result) {
2001 err = le32_to_cpu(req->r_reply_info.head->result);
2002 } else if (err < 0) {
2003 dout("aborted request %lld with %d\n", req->r_tid, err);
2006 * ensure we aren't running concurrently with
2007 * ceph_fill_trace or ceph_readdir_prepopulate, which
2008 * rely on locks (dir mutex) held by our caller.
2010 mutex_lock(&req->r_fill_mutex);
2012 req->r_aborted = true;
2013 mutex_unlock(&req->r_fill_mutex);
2015 if (req->r_locked_dir &&
2016 (req->r_op & CEPH_MDS_OP_WRITE))
2017 ceph_invalidate_dir_request(req);
2023 mutex_unlock(&mdsc->mutex);
2024 dout("do_request %p done, result %d\n", req, err);
2029 * Invalidate dir D_COMPLETE, dentry lease state on an aborted MDS
2030 * namespace request.
2032 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2034 struct inode *inode = req->r_locked_dir;
2035 struct ceph_inode_info *ci = ceph_inode(inode);
2037 dout("invalidate_dir_request %p (D_COMPLETE, lease(s))\n", inode);
2038 spin_lock(&ci->i_ceph_lock);
2039 ceph_dir_clear_complete(inode);
2040 ci->i_release_count++;
2041 spin_unlock(&ci->i_ceph_lock);
2044 ceph_invalidate_dentry_lease(req->r_dentry);
2045 if (req->r_old_dentry)
2046 ceph_invalidate_dentry_lease(req->r_old_dentry);
2052 * We take the session mutex and parse and process the reply immediately.
2053 * This preserves the logical ordering of replies, capabilities, etc., sent
2054 * by the MDS as they are applied to our local cache.
2056 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2058 struct ceph_mds_client *mdsc = session->s_mdsc;
2059 struct ceph_mds_request *req;
2060 struct ceph_mds_reply_head *head = msg->front.iov_base;
2061 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2064 int mds = session->s_mds;
2066 if (msg->front.iov_len < sizeof(*head)) {
2067 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2072 /* get request, session */
2073 tid = le64_to_cpu(msg->hdr.tid);
2074 mutex_lock(&mdsc->mutex);
2075 req = __lookup_request(mdsc, tid);
2077 dout("handle_reply on unknown tid %llu\n", tid);
2078 mutex_unlock(&mdsc->mutex);
2081 dout("handle_reply %p\n", req);
2083 /* correct session? */
2084 if (req->r_session != session) {
2085 pr_err("mdsc_handle_reply got %llu on session mds%d"
2086 " not mds%d\n", tid, session->s_mds,
2087 req->r_session ? req->r_session->s_mds : -1);
2088 mutex_unlock(&mdsc->mutex);
2093 if ((req->r_got_unsafe && !head->safe) ||
2094 (req->r_got_safe && head->safe)) {
2095 pr_warning("got a dup %s reply on %llu from mds%d\n",
2096 head->safe ? "safe" : "unsafe", tid, mds);
2097 mutex_unlock(&mdsc->mutex);
2100 if (req->r_got_safe && !head->safe) {
2101 pr_warning("got unsafe after safe on %llu from mds%d\n",
2103 mutex_unlock(&mdsc->mutex);
2107 result = le32_to_cpu(head->result);
2111 * if we're not talking to the authority, send to them
2112 * if the authority has changed while we weren't looking,
2113 * send to new authority
2114 * Otherwise we just have to return an ESTALE
2116 if (result == -ESTALE) {
2117 dout("got ESTALE on request %llu", req->r_tid);
2118 if (!req->r_inode) {
2119 /* do nothing; not an authority problem */
2120 } else if (req->r_direct_mode != USE_AUTH_MDS) {
2121 dout("not using auth, setting for that now");
2122 req->r_direct_mode = USE_AUTH_MDS;
2123 __do_request(mdsc, req);
2124 mutex_unlock(&mdsc->mutex);
2127 struct ceph_inode_info *ci = ceph_inode(req->r_inode);
2128 struct ceph_cap *cap = NULL;
2131 cap = ceph_get_cap_for_mds(ci,
2132 req->r_session->s_mds);
2134 dout("already using auth");
2135 if ((!cap || cap != ci->i_auth_cap) ||
2136 (cap->mseq != req->r_sent_on_mseq)) {
2137 dout("but cap changed, so resending");
2138 __do_request(mdsc, req);
2139 mutex_unlock(&mdsc->mutex);
2143 dout("have to return ESTALE on request %llu", req->r_tid);
2148 req->r_got_safe = true;
2149 __unregister_request(mdsc, req);
2150 complete_all(&req->r_safe_completion);
2152 if (req->r_got_unsafe) {
2154 * We already handled the unsafe response, now do the
2155 * cleanup. No need to examine the response; the MDS
2156 * doesn't include any result info in the safe
2157 * response. And even if it did, there is nothing
2158 * useful we could do with a revised return value.
2160 dout("got safe reply %llu, mds%d\n", tid, mds);
2161 list_del_init(&req->r_unsafe_item);
2163 /* last unsafe request during umount? */
2164 if (mdsc->stopping && !__get_oldest_req(mdsc))
2165 complete_all(&mdsc->safe_umount_waiters);
2166 mutex_unlock(&mdsc->mutex);
2170 req->r_got_unsafe = true;
2171 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2174 dout("handle_reply tid %lld result %d\n", tid, result);
2175 rinfo = &req->r_reply_info;
2176 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2177 mutex_unlock(&mdsc->mutex);
2179 mutex_lock(&session->s_mutex);
2181 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
2187 if (rinfo->snapblob_len) {
2188 down_write(&mdsc->snap_rwsem);
2189 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2190 rinfo->snapblob + rinfo->snapblob_len,
2191 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2192 downgrade_write(&mdsc->snap_rwsem);
2194 down_read(&mdsc->snap_rwsem);
2197 /* insert trace into our cache */
2198 mutex_lock(&req->r_fill_mutex);
2199 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
2201 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
2202 req->r_op == CEPH_MDS_OP_LSSNAP) &&
2204 ceph_readdir_prepopulate(req, req->r_session);
2205 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2207 mutex_unlock(&req->r_fill_mutex);
2209 up_read(&mdsc->snap_rwsem);
2211 mutex_lock(&mdsc->mutex);
2212 if (!req->r_aborted) {
2218 req->r_got_result = true;
2221 dout("reply arrived after request %lld was aborted\n", tid);
2223 mutex_unlock(&mdsc->mutex);
2225 ceph_add_cap_releases(mdsc, req->r_session);
2226 mutex_unlock(&session->s_mutex);
2228 /* kick calling process */
2229 complete_request(mdsc, req);
2231 ceph_mdsc_put_request(req);
2238 * handle mds notification that our request has been forwarded.
2240 static void handle_forward(struct ceph_mds_client *mdsc,
2241 struct ceph_mds_session *session,
2242 struct ceph_msg *msg)
2244 struct ceph_mds_request *req;
2245 u64 tid = le64_to_cpu(msg->hdr.tid);
2249 void *p = msg->front.iov_base;
2250 void *end = p + msg->front.iov_len;
2252 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2253 next_mds = ceph_decode_32(&p);
2254 fwd_seq = ceph_decode_32(&p);
2256 mutex_lock(&mdsc->mutex);
2257 req = __lookup_request(mdsc, tid);
2259 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2260 goto out; /* dup reply? */
2263 if (req->r_aborted) {
2264 dout("forward tid %llu aborted, unregistering\n", tid);
2265 __unregister_request(mdsc, req);
2266 } else if (fwd_seq <= req->r_num_fwd) {
2267 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2268 tid, next_mds, req->r_num_fwd, fwd_seq);
2270 /* resend. forward race not possible; mds would drop */
2271 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2273 BUG_ON(req->r_got_result);
2274 req->r_num_fwd = fwd_seq;
2275 req->r_resend_mds = next_mds;
2276 put_request_session(req);
2277 __do_request(mdsc, req);
2279 ceph_mdsc_put_request(req);
2281 mutex_unlock(&mdsc->mutex);
2285 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2289 * handle a mds session control message
2291 static void handle_session(struct ceph_mds_session *session,
2292 struct ceph_msg *msg)
2294 struct ceph_mds_client *mdsc = session->s_mdsc;
2297 int mds = session->s_mds;
2298 struct ceph_mds_session_head *h = msg->front.iov_base;
2302 if (msg->front.iov_len != sizeof(*h))
2304 op = le32_to_cpu(h->op);
2305 seq = le64_to_cpu(h->seq);
2307 mutex_lock(&mdsc->mutex);
2308 if (op == CEPH_SESSION_CLOSE)
2309 __unregister_session(mdsc, session);
2310 /* FIXME: this ttl calculation is generous */
2311 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2312 mutex_unlock(&mdsc->mutex);
2314 mutex_lock(&session->s_mutex);
2316 dout("handle_session mds%d %s %p state %s seq %llu\n",
2317 mds, ceph_session_op_name(op), session,
2318 session_state_name(session->s_state), seq);
2320 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2321 session->s_state = CEPH_MDS_SESSION_OPEN;
2322 pr_info("mds%d came back\n", session->s_mds);
2326 case CEPH_SESSION_OPEN:
2327 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2328 pr_info("mds%d reconnect success\n", session->s_mds);
2329 session->s_state = CEPH_MDS_SESSION_OPEN;
2330 renewed_caps(mdsc, session, 0);
2333 __close_session(mdsc, session);
2336 case CEPH_SESSION_RENEWCAPS:
2337 if (session->s_renew_seq == seq)
2338 renewed_caps(mdsc, session, 1);
2341 case CEPH_SESSION_CLOSE:
2342 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2343 pr_info("mds%d reconnect denied\n", session->s_mds);
2344 remove_session_caps(session);
2345 wake = 1; /* for good measure */
2346 wake_up_all(&mdsc->session_close_wq);
2347 kick_requests(mdsc, mds);
2350 case CEPH_SESSION_STALE:
2351 pr_info("mds%d caps went stale, renewing\n",
2353 spin_lock(&session->s_gen_ttl_lock);
2354 session->s_cap_gen++;
2355 session->s_cap_ttl = jiffies - 1;
2356 spin_unlock(&session->s_gen_ttl_lock);
2357 send_renew_caps(mdsc, session);
2360 case CEPH_SESSION_RECALL_STATE:
2361 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2365 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2369 mutex_unlock(&session->s_mutex);
2371 mutex_lock(&mdsc->mutex);
2372 __wake_requests(mdsc, &session->s_waiting);
2373 mutex_unlock(&mdsc->mutex);
2378 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2379 (int)msg->front.iov_len);
2386 * called under session->mutex.
2388 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2389 struct ceph_mds_session *session)
2391 struct ceph_mds_request *req, *nreq;
2394 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2396 mutex_lock(&mdsc->mutex);
2397 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2398 err = __prepare_send_request(mdsc, req, session->s_mds);
2400 ceph_msg_get(req->r_request);
2401 ceph_con_send(&session->s_con, req->r_request);
2404 mutex_unlock(&mdsc->mutex);
2408 * Encode information about a cap for a reconnect with the MDS.
2410 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2414 struct ceph_mds_cap_reconnect v2;
2415 struct ceph_mds_cap_reconnect_v1 v1;
2418 struct ceph_inode_info *ci;
2419 struct ceph_reconnect_state *recon_state = arg;
2420 struct ceph_pagelist *pagelist = recon_state->pagelist;
2424 struct dentry *dentry;
2428 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2429 inode, ceph_vinop(inode), cap, cap->cap_id,
2430 ceph_cap_string(cap->issued));
2431 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2435 dentry = d_find_alias(inode);
2437 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2439 err = PTR_ERR(path);
2446 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2450 spin_lock(&ci->i_ceph_lock);
2451 cap->seq = 0; /* reset cap seq */
2452 cap->issue_seq = 0; /* and issue_seq */
2454 if (recon_state->flock) {
2455 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2456 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2457 rec.v2.issued = cpu_to_le32(cap->issued);
2458 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2459 rec.v2.pathbase = cpu_to_le64(pathbase);
2460 rec.v2.flock_len = 0;
2461 reclen = sizeof(rec.v2);
2463 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2464 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2465 rec.v1.issued = cpu_to_le32(cap->issued);
2466 rec.v1.size = cpu_to_le64(inode->i_size);
2467 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2468 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2469 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2470 rec.v1.pathbase = cpu_to_le64(pathbase);
2471 reclen = sizeof(rec.v1);
2473 spin_unlock(&ci->i_ceph_lock);
2475 if (recon_state->flock) {
2476 int num_fcntl_locks, num_flock_locks;
2477 struct ceph_pagelist_cursor trunc_point;
2479 ceph_pagelist_set_cursor(pagelist, &trunc_point);
2482 ceph_count_locks(inode, &num_fcntl_locks,
2484 rec.v2.flock_len = (2*sizeof(u32) +
2485 (num_fcntl_locks+num_flock_locks) *
2486 sizeof(struct ceph_filelock));
2489 /* pre-alloc pagelist */
2490 ceph_pagelist_truncate(pagelist, &trunc_point);
2491 err = ceph_pagelist_append(pagelist, &rec, reclen);
2493 err = ceph_pagelist_reserve(pagelist,
2499 err = ceph_encode_locks(inode,
2505 } while (err == -ENOSPC);
2507 err = ceph_pagelist_append(pagelist, &rec, reclen);
2519 * If an MDS fails and recovers, clients need to reconnect in order to
2520 * reestablish shared state. This includes all caps issued through
2521 * this session _and_ the snap_realm hierarchy. Because it's not
2522 * clear which snap realms the mds cares about, we send everything we
2523 * know about.. that ensures we'll then get any new info the
2524 * recovering MDS might have.
2526 * This is a relatively heavyweight operation, but it's rare.
2528 * called with mdsc->mutex held.
2530 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2531 struct ceph_mds_session *session)
2533 struct ceph_msg *reply;
2535 int mds = session->s_mds;
2537 struct ceph_pagelist *pagelist;
2538 struct ceph_reconnect_state recon_state;
2540 pr_info("mds%d reconnect start\n", mds);
2542 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2544 goto fail_nopagelist;
2545 ceph_pagelist_init(pagelist);
2547 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
2551 mutex_lock(&session->s_mutex);
2552 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2555 ceph_con_close(&session->s_con);
2556 ceph_con_open(&session->s_con,
2557 CEPH_ENTITY_TYPE_MDS, mds,
2558 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2560 /* replay unsafe requests */
2561 replay_unsafe_requests(mdsc, session);
2563 down_read(&mdsc->snap_rwsem);
2565 dout("session %p state %s\n", session,
2566 session_state_name(session->s_state));
2568 /* drop old cap expires; we're about to reestablish that state */
2569 discard_cap_releases(mdsc, session);
2571 /* traverse this session's caps */
2572 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2576 recon_state.pagelist = pagelist;
2577 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2578 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
2583 * snaprealms. we provide mds with the ino, seq (version), and
2584 * parent for all of our realms. If the mds has any newer info,
2587 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2588 struct ceph_snap_realm *realm =
2589 rb_entry(p, struct ceph_snap_realm, node);
2590 struct ceph_mds_snaprealm_reconnect sr_rec;
2592 dout(" adding snap realm %llx seq %lld parent %llx\n",
2593 realm->ino, realm->seq, realm->parent_ino);
2594 sr_rec.ino = cpu_to_le64(realm->ino);
2595 sr_rec.seq = cpu_to_le64(realm->seq);
2596 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2597 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2602 reply->pagelist = pagelist;
2603 if (recon_state.flock)
2604 reply->hdr.version = cpu_to_le16(2);
2605 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2606 reply->nr_pages = calc_pages_for(0, pagelist->length);
2607 ceph_con_send(&session->s_con, reply);
2609 mutex_unlock(&session->s_mutex);
2611 mutex_lock(&mdsc->mutex);
2612 __wake_requests(mdsc, &session->s_waiting);
2613 mutex_unlock(&mdsc->mutex);
2615 up_read(&mdsc->snap_rwsem);
2619 ceph_msg_put(reply);
2620 up_read(&mdsc->snap_rwsem);
2621 mutex_unlock(&session->s_mutex);
2623 ceph_pagelist_release(pagelist);
2626 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
2632 * compare old and new mdsmaps, kicking requests
2633 * and closing out old connections as necessary
2635 * called under mdsc->mutex.
2637 static void check_new_map(struct ceph_mds_client *mdsc,
2638 struct ceph_mdsmap *newmap,
2639 struct ceph_mdsmap *oldmap)
2642 int oldstate, newstate;
2643 struct ceph_mds_session *s;
2645 dout("check_new_map new %u old %u\n",
2646 newmap->m_epoch, oldmap->m_epoch);
2648 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2649 if (mdsc->sessions[i] == NULL)
2651 s = mdsc->sessions[i];
2652 oldstate = ceph_mdsmap_get_state(oldmap, i);
2653 newstate = ceph_mdsmap_get_state(newmap, i);
2655 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2656 i, ceph_mds_state_name(oldstate),
2657 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
2658 ceph_mds_state_name(newstate),
2659 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
2660 session_state_name(s->s_state));
2662 if (i >= newmap->m_max_mds ||
2663 memcmp(ceph_mdsmap_get_addr(oldmap, i),
2664 ceph_mdsmap_get_addr(newmap, i),
2665 sizeof(struct ceph_entity_addr))) {
2666 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2667 /* the session never opened, just close it
2669 __wake_requests(mdsc, &s->s_waiting);
2670 __unregister_session(mdsc, s);
2673 mutex_unlock(&mdsc->mutex);
2674 mutex_lock(&s->s_mutex);
2675 mutex_lock(&mdsc->mutex);
2676 ceph_con_close(&s->s_con);
2677 mutex_unlock(&s->s_mutex);
2678 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2681 /* kick any requests waiting on the recovering mds */
2682 kick_requests(mdsc, i);
2683 } else if (oldstate == newstate) {
2684 continue; /* nothing new with this mds */
2690 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
2691 newstate >= CEPH_MDS_STATE_RECONNECT) {
2692 mutex_unlock(&mdsc->mutex);
2693 send_mds_reconnect(mdsc, s);
2694 mutex_lock(&mdsc->mutex);
2698 * kick request on any mds that has gone active.
2700 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2701 newstate >= CEPH_MDS_STATE_ACTIVE) {
2702 if (oldstate != CEPH_MDS_STATE_CREATING &&
2703 oldstate != CEPH_MDS_STATE_STARTING)
2704 pr_info("mds%d recovery completed\n", s->s_mds);
2705 kick_requests(mdsc, i);
2706 ceph_kick_flushing_caps(mdsc, s);
2707 wake_up_session_caps(s, 1);
2711 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2712 s = mdsc->sessions[i];
2715 if (!ceph_mdsmap_is_laggy(newmap, i))
2717 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2718 s->s_state == CEPH_MDS_SESSION_HUNG ||
2719 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2720 dout(" connecting to export targets of laggy mds%d\n",
2722 __open_export_target_sessions(mdsc, s);
2734 * caller must hold session s_mutex, dentry->d_lock
2736 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2738 struct ceph_dentry_info *di = ceph_dentry(dentry);
2740 ceph_put_mds_session(di->lease_session);
2741 di->lease_session = NULL;
2744 static void handle_lease(struct ceph_mds_client *mdsc,
2745 struct ceph_mds_session *session,
2746 struct ceph_msg *msg)
2748 struct super_block *sb = mdsc->fsc->sb;
2749 struct inode *inode;
2750 struct dentry *parent, *dentry;
2751 struct ceph_dentry_info *di;
2752 int mds = session->s_mds;
2753 struct ceph_mds_lease *h = msg->front.iov_base;
2755 struct ceph_vino vino;
2759 dout("handle_lease from mds%d\n", mds);
2762 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2764 vino.ino = le64_to_cpu(h->ino);
2765 vino.snap = CEPH_NOSNAP;
2766 seq = le32_to_cpu(h->seq);
2767 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2768 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2769 if (dname.len != get_unaligned_le32(h+1))
2772 mutex_lock(&session->s_mutex);
2776 inode = ceph_find_inode(sb, vino);
2777 dout("handle_lease %s, ino %llx %p %.*s\n",
2778 ceph_lease_op_name(h->action), vino.ino, inode,
2779 dname.len, dname.name);
2780 if (inode == NULL) {
2781 dout("handle_lease no inode %llx\n", vino.ino);
2786 parent = d_find_alias(inode);
2788 dout("no parent dentry on inode %p\n", inode);
2790 goto release; /* hrm... */
2792 dname.hash = full_name_hash(dname.name, dname.len);
2793 dentry = d_lookup(parent, &dname);
2798 spin_lock(&dentry->d_lock);
2799 di = ceph_dentry(dentry);
2800 switch (h->action) {
2801 case CEPH_MDS_LEASE_REVOKE:
2802 if (di->lease_session == session) {
2803 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2804 h->seq = cpu_to_le32(di->lease_seq);
2805 __ceph_mdsc_drop_dentry_lease(dentry);
2810 case CEPH_MDS_LEASE_RENEW:
2811 if (di->lease_session == session &&
2812 di->lease_gen == session->s_cap_gen &&
2813 di->lease_renew_from &&
2814 di->lease_renew_after == 0) {
2815 unsigned long duration =
2816 le32_to_cpu(h->duration_ms) * HZ / 1000;
2818 di->lease_seq = seq;
2819 dentry->d_time = di->lease_renew_from + duration;
2820 di->lease_renew_after = di->lease_renew_from +
2822 di->lease_renew_from = 0;
2826 spin_unlock(&dentry->d_lock);
2833 /* let's just reuse the same message */
2834 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2836 ceph_con_send(&session->s_con, msg);
2840 mutex_unlock(&session->s_mutex);
2844 pr_err("corrupt lease message\n");
2848 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2849 struct inode *inode,
2850 struct dentry *dentry, char action,
2853 struct ceph_msg *msg;
2854 struct ceph_mds_lease *lease;
2855 int len = sizeof(*lease) + sizeof(u32);
2858 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2859 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2860 dnamelen = dentry->d_name.len;
2863 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
2866 lease = msg->front.iov_base;
2867 lease->action = action;
2868 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2869 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2870 lease->seq = cpu_to_le32(seq);
2871 put_unaligned_le32(dnamelen, lease + 1);
2872 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2875 * if this is a preemptive lease RELEASE, no need to
2876 * flush request stream, since the actual request will
2879 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2881 ceph_con_send(&session->s_con, msg);
2885 * Preemptively release a lease we expect to invalidate anyway.
2886 * Pass @inode always, @dentry is optional.
2888 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2889 struct dentry *dentry)
2891 struct ceph_dentry_info *di;
2892 struct ceph_mds_session *session;
2895 BUG_ON(inode == NULL);
2896 BUG_ON(dentry == NULL);
2898 /* is dentry lease valid? */
2899 spin_lock(&dentry->d_lock);
2900 di = ceph_dentry(dentry);
2901 if (!di || !di->lease_session ||
2902 di->lease_session->s_mds < 0 ||
2903 di->lease_gen != di->lease_session->s_cap_gen ||
2904 !time_before(jiffies, dentry->d_time)) {
2905 dout("lease_release inode %p dentry %p -- "
2908 spin_unlock(&dentry->d_lock);
2912 /* we do have a lease on this dentry; note mds and seq */
2913 session = ceph_get_mds_session(di->lease_session);
2914 seq = di->lease_seq;
2915 __ceph_mdsc_drop_dentry_lease(dentry);
2916 spin_unlock(&dentry->d_lock);
2918 dout("lease_release inode %p dentry %p to mds%d\n",
2919 inode, dentry, session->s_mds);
2920 ceph_mdsc_lease_send_msg(session, inode, dentry,
2921 CEPH_MDS_LEASE_RELEASE, seq);
2922 ceph_put_mds_session(session);
2926 * drop all leases (and dentry refs) in preparation for umount
2928 static void drop_leases(struct ceph_mds_client *mdsc)
2932 dout("drop_leases\n");
2933 mutex_lock(&mdsc->mutex);
2934 for (i = 0; i < mdsc->max_sessions; i++) {
2935 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2938 mutex_unlock(&mdsc->mutex);
2939 mutex_lock(&s->s_mutex);
2940 mutex_unlock(&s->s_mutex);
2941 ceph_put_mds_session(s);
2942 mutex_lock(&mdsc->mutex);
2944 mutex_unlock(&mdsc->mutex);
2950 * delayed work -- periodically trim expired leases, renew caps with mds
2952 static void schedule_delayed(struct ceph_mds_client *mdsc)
2955 unsigned hz = round_jiffies_relative(HZ * delay);
2956 schedule_delayed_work(&mdsc->delayed_work, hz);
2959 static void delayed_work(struct work_struct *work)
2962 struct ceph_mds_client *mdsc =
2963 container_of(work, struct ceph_mds_client, delayed_work.work);
2967 dout("mdsc delayed_work\n");
2968 ceph_check_delayed_caps(mdsc);
2970 mutex_lock(&mdsc->mutex);
2971 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2972 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2973 mdsc->last_renew_caps);
2975 mdsc->last_renew_caps = jiffies;
2977 for (i = 0; i < mdsc->max_sessions; i++) {
2978 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2981 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2982 dout("resending session close request for mds%d\n",
2984 request_close_session(mdsc, s);
2985 ceph_put_mds_session(s);
2988 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2989 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2990 s->s_state = CEPH_MDS_SESSION_HUNG;
2991 pr_info("mds%d hung\n", s->s_mds);
2994 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2995 /* this mds is failed or recovering, just wait */
2996 ceph_put_mds_session(s);
2999 mutex_unlock(&mdsc->mutex);
3001 mutex_lock(&s->s_mutex);
3003 send_renew_caps(mdsc, s);
3005 ceph_con_keepalive(&s->s_con);
3006 ceph_add_cap_releases(mdsc, s);
3007 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3008 s->s_state == CEPH_MDS_SESSION_HUNG)
3009 ceph_send_cap_releases(mdsc, s);
3010 mutex_unlock(&s->s_mutex);
3011 ceph_put_mds_session(s);
3013 mutex_lock(&mdsc->mutex);
3015 mutex_unlock(&mdsc->mutex);
3017 schedule_delayed(mdsc);
3020 int ceph_mdsc_init(struct ceph_fs_client *fsc)
3023 struct ceph_mds_client *mdsc;
3025 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3030 mutex_init(&mdsc->mutex);
3031 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
3032 if (mdsc->mdsmap == NULL)
3035 init_completion(&mdsc->safe_umount_waiters);
3036 init_waitqueue_head(&mdsc->session_close_wq);
3037 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3038 mdsc->sessions = NULL;
3039 mdsc->max_sessions = 0;
3041 init_rwsem(&mdsc->snap_rwsem);
3042 mdsc->snap_realms = RB_ROOT;
3043 INIT_LIST_HEAD(&mdsc->snap_empty);
3044 spin_lock_init(&mdsc->snap_empty_lock);
3046 mdsc->request_tree = RB_ROOT;
3047 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3048 mdsc->last_renew_caps = jiffies;
3049 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3050 spin_lock_init(&mdsc->cap_delay_lock);
3051 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3052 spin_lock_init(&mdsc->snap_flush_lock);
3053 mdsc->cap_flush_seq = 0;
3054 INIT_LIST_HEAD(&mdsc->cap_dirty);
3055 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
3056 mdsc->num_cap_flushing = 0;
3057 spin_lock_init(&mdsc->cap_dirty_lock);
3058 init_waitqueue_head(&mdsc->cap_flushing_wq);
3059 spin_lock_init(&mdsc->dentry_lru_lock);
3060 INIT_LIST_HEAD(&mdsc->dentry_lru);
3062 ceph_caps_init(mdsc);
3063 ceph_adjust_min_caps(mdsc, fsc->min_caps);
3069 * Wait for safe replies on open mds requests. If we time out, drop
3070 * all requests from the tree to avoid dangling dentry refs.
3072 static void wait_requests(struct ceph_mds_client *mdsc)
3074 struct ceph_mds_request *req;
3075 struct ceph_fs_client *fsc = mdsc->fsc;
3077 mutex_lock(&mdsc->mutex);
3078 if (__get_oldest_req(mdsc)) {
3079 mutex_unlock(&mdsc->mutex);
3081 dout("wait_requests waiting for requests\n");
3082 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
3083 fsc->client->options->mount_timeout * HZ);
3085 /* tear down remaining requests */
3086 mutex_lock(&mdsc->mutex);
3087 while ((req = __get_oldest_req(mdsc))) {
3088 dout("wait_requests timed out on tid %llu\n",
3090 __unregister_request(mdsc, req);
3093 mutex_unlock(&mdsc->mutex);
3094 dout("wait_requests done\n");
3098 * called before mount is ro, and before dentries are torn down.
3099 * (hmm, does this still race with new lookups?)
3101 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3103 dout("pre_umount\n");
3107 ceph_flush_dirty_caps(mdsc);
3108 wait_requests(mdsc);
3111 * wait for reply handlers to drop their request refs and
3112 * their inode/dcache refs
3118 * wait for all write mds requests to flush.
3120 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3122 struct ceph_mds_request *req = NULL, *nextreq;
3125 mutex_lock(&mdsc->mutex);
3126 dout("wait_unsafe_requests want %lld\n", want_tid);
3128 req = __get_oldest_req(mdsc);
3129 while (req && req->r_tid <= want_tid) {
3130 /* find next request */
3131 n = rb_next(&req->r_node);
3133 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3136 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3138 ceph_mdsc_get_request(req);
3140 ceph_mdsc_get_request(nextreq);
3141 mutex_unlock(&mdsc->mutex);
3142 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3143 req->r_tid, want_tid);
3144 wait_for_completion(&req->r_safe_completion);
3145 mutex_lock(&mdsc->mutex);
3146 ceph_mdsc_put_request(req);
3148 break; /* next dne before, so we're done! */
3149 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3150 /* next request was removed from tree */
3151 ceph_mdsc_put_request(nextreq);
3154 ceph_mdsc_put_request(nextreq); /* won't go away */
3158 mutex_unlock(&mdsc->mutex);
3159 dout("wait_unsafe_requests done\n");
3162 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3164 u64 want_tid, want_flush;
3166 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3170 mutex_lock(&mdsc->mutex);
3171 want_tid = mdsc->last_tid;
3172 want_flush = mdsc->cap_flush_seq;
3173 mutex_unlock(&mdsc->mutex);
3174 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3176 ceph_flush_dirty_caps(mdsc);
3178 wait_unsafe_requests(mdsc, want_tid);
3179 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3183 * true if all sessions are closed, or we force unmount
3185 static bool done_closing_sessions(struct ceph_mds_client *mdsc)
3189 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3192 mutex_lock(&mdsc->mutex);
3193 for (i = 0; i < mdsc->max_sessions; i++)
3194 if (mdsc->sessions[i])
3196 mutex_unlock(&mdsc->mutex);
3201 * called after sb is ro.
3203 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3205 struct ceph_mds_session *session;
3207 struct ceph_fs_client *fsc = mdsc->fsc;
3208 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
3210 dout("close_sessions\n");
3212 /* close sessions */
3213 mutex_lock(&mdsc->mutex);
3214 for (i = 0; i < mdsc->max_sessions; i++) {
3215 session = __ceph_lookup_mds_session(mdsc, i);
3218 mutex_unlock(&mdsc->mutex);
3219 mutex_lock(&session->s_mutex);
3220 __close_session(mdsc, session);
3221 mutex_unlock(&session->s_mutex);
3222 ceph_put_mds_session(session);
3223 mutex_lock(&mdsc->mutex);
3225 mutex_unlock(&mdsc->mutex);
3227 dout("waiting for sessions to close\n");
3228 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3231 /* tear down remaining sessions */
3232 mutex_lock(&mdsc->mutex);
3233 for (i = 0; i < mdsc->max_sessions; i++) {
3234 if (mdsc->sessions[i]) {
3235 session = get_session(mdsc->sessions[i]);
3236 __unregister_session(mdsc, session);
3237 mutex_unlock(&mdsc->mutex);
3238 mutex_lock(&session->s_mutex);
3239 remove_session_caps(session);
3240 mutex_unlock(&session->s_mutex);
3241 ceph_put_mds_session(session);
3242 mutex_lock(&mdsc->mutex);
3245 WARN_ON(!list_empty(&mdsc->cap_delay_list));
3246 mutex_unlock(&mdsc->mutex);
3248 ceph_cleanup_empty_realms(mdsc);
3250 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3255 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
3258 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3260 ceph_mdsmap_destroy(mdsc->mdsmap);
3261 kfree(mdsc->sessions);
3262 ceph_caps_finalize(mdsc);
3265 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3267 struct ceph_mds_client *mdsc = fsc->mdsc;
3269 dout("mdsc_destroy %p\n", mdsc);
3270 ceph_mdsc_stop(mdsc);
3272 /* flush out any connection work with references to us */
3277 dout("mdsc_destroy %p done\n", mdsc);
3282 * handle mds map update.
3284 void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3288 void *p = msg->front.iov_base;
3289 void *end = p + msg->front.iov_len;
3290 struct ceph_mdsmap *newmap, *oldmap;
3291 struct ceph_fsid fsid;
3294 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3295 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3296 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
3298 epoch = ceph_decode_32(&p);
3299 maplen = ceph_decode_32(&p);
3300 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3302 /* do we need it? */
3303 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
3304 mutex_lock(&mdsc->mutex);
3305 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3306 dout("handle_map epoch %u <= our %u\n",
3307 epoch, mdsc->mdsmap->m_epoch);
3308 mutex_unlock(&mdsc->mutex);
3312 newmap = ceph_mdsmap_decode(&p, end);
3313 if (IS_ERR(newmap)) {
3314 err = PTR_ERR(newmap);
3318 /* swap into place */
3320 oldmap = mdsc->mdsmap;
3321 mdsc->mdsmap = newmap;
3322 check_new_map(mdsc, newmap, oldmap);
3323 ceph_mdsmap_destroy(oldmap);
3325 mdsc->mdsmap = newmap; /* first mds map */
3327 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
3329 __wake_requests(mdsc, &mdsc->waiting_for_map);
3331 mutex_unlock(&mdsc->mutex);
3332 schedule_delayed(mdsc);
3336 mutex_unlock(&mdsc->mutex);
3338 pr_err("error decoding mdsmap %d\n", err);
3342 static struct ceph_connection *con_get(struct ceph_connection *con)
3344 struct ceph_mds_session *s = con->private;
3346 if (get_session(s)) {
3347 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
3350 dout("mdsc con_get %p FAIL\n", s);
3354 static void con_put(struct ceph_connection *con)
3356 struct ceph_mds_session *s = con->private;
3358 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref) - 1);
3359 ceph_put_mds_session(s);
3363 * if the client is unresponsive for long enough, the mds will kill
3364 * the session entirely.
3366 static void peer_reset(struct ceph_connection *con)
3368 struct ceph_mds_session *s = con->private;
3369 struct ceph_mds_client *mdsc = s->s_mdsc;
3371 pr_warning("mds%d closed our session\n", s->s_mds);
3372 send_mds_reconnect(mdsc, s);
3375 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3377 struct ceph_mds_session *s = con->private;
3378 struct ceph_mds_client *mdsc = s->s_mdsc;
3379 int type = le16_to_cpu(msg->hdr.type);
3381 mutex_lock(&mdsc->mutex);
3382 if (__verify_registered_session(mdsc, s) < 0) {
3383 mutex_unlock(&mdsc->mutex);
3386 mutex_unlock(&mdsc->mutex);
3389 case CEPH_MSG_MDS_MAP:
3390 ceph_mdsc_handle_map(mdsc, msg);
3392 case CEPH_MSG_CLIENT_SESSION:
3393 handle_session(s, msg);
3395 case CEPH_MSG_CLIENT_REPLY:
3396 handle_reply(s, msg);
3398 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
3399 handle_forward(mdsc, s, msg);
3401 case CEPH_MSG_CLIENT_CAPS:
3402 ceph_handle_caps(s, msg);
3404 case CEPH_MSG_CLIENT_SNAP:
3405 ceph_handle_snap(mdsc, s, msg);
3407 case CEPH_MSG_CLIENT_LEASE:
3408 handle_lease(mdsc, s, msg);
3412 pr_err("received unknown message type %d %s\n", type,
3413 ceph_msg_type_name(type));
3424 * Note: returned pointer is the address of a structure that's
3425 * managed separately. Caller must *not* attempt to free it.
3427 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
3428 int *proto, int force_new)
3430 struct ceph_mds_session *s = con->private;
3431 struct ceph_mds_client *mdsc = s->s_mdsc;
3432 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3433 struct ceph_auth_handshake *auth = &s->s_auth;
3435 if (force_new && auth->authorizer) {
3436 if (ac->ops && ac->ops->destroy_authorizer)
3437 ac->ops->destroy_authorizer(ac, auth->authorizer);
3438 auth->authorizer = NULL;
3440 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
3441 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3444 return ERR_PTR(ret);
3446 *proto = ac->protocol;
3452 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3454 struct ceph_mds_session *s = con->private;
3455 struct ceph_mds_client *mdsc = s->s_mdsc;
3456 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3458 return ac->ops->verify_authorizer_reply(ac, s->s_auth.authorizer, len);
3461 static int invalidate_authorizer(struct ceph_connection *con)
3463 struct ceph_mds_session *s = con->private;
3464 struct ceph_mds_client *mdsc = s->s_mdsc;
3465 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3467 if (ac->ops->invalidate_authorizer)
3468 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3470 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
3473 static const struct ceph_connection_operations mds_con_ops = {
3476 .dispatch = dispatch,
3477 .get_authorizer = get_authorizer,
3478 .verify_authorizer_reply = verify_authorizer_reply,
3479 .invalidate_authorizer = invalidate_authorizer,
3480 .peer_reset = peer_reset,