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/messenger.h>
14 #include <linux/ceph/decode.h>
15 #include <linux/ceph/pagelist.h>
16 #include <linux/ceph/auth.h>
17 #include <linux/ceph/debugfs.h>
20 * A cluster of MDS (metadata server) daemons is responsible for
21 * managing the file system namespace (the directory hierarchy and
22 * inodes) and for coordinating shared access to storage. Metadata is
23 * partitioning hierarchically across a number of servers, and that
24 * partition varies over time as the cluster adjusts the distribution
25 * in order to balance load.
27 * The MDS client is primarily responsible to managing synchronous
28 * metadata requests for operations like open, unlink, and so forth.
29 * If there is a MDS failure, we find out about it when we (possibly
30 * request and) receive a new MDS map, and can resubmit affected
33 * For the most part, though, we take advantage of a lossless
34 * communications channel to the MDS, and do not need to worry about
35 * timing out or resubmitting requests.
37 * We maintain a stateful "session" with each MDS we interact with.
38 * Within each session, we sent periodic heartbeat messages to ensure
39 * any capabilities or leases we have been issues remain valid. If
40 * the session times out and goes stale, our leases and capabilities
41 * are no longer valid.
44 struct ceph_reconnect_state {
45 struct ceph_pagelist *pagelist;
49 static void __wake_requests(struct ceph_mds_client *mdsc,
50 struct list_head *head);
52 static const struct ceph_connection_operations mds_con_ops;
60 * parse individual inode info
62 static int parse_reply_info_in(void **p, void *end,
63 struct ceph_mds_reply_info_in *info,
69 *p += sizeof(struct ceph_mds_reply_inode) +
70 sizeof(*info->in->fragtree.splits) *
71 le32_to_cpu(info->in->fragtree.nsplits);
73 ceph_decode_32_safe(p, end, info->symlink_len, bad);
74 ceph_decode_need(p, end, info->symlink_len, bad);
76 *p += info->symlink_len;
78 if (features & CEPH_FEATURE_DIRLAYOUTHASH)
79 ceph_decode_copy_safe(p, end, &info->dir_layout,
80 sizeof(info->dir_layout), bad);
82 memset(&info->dir_layout, 0, sizeof(info->dir_layout));
84 ceph_decode_32_safe(p, end, info->xattr_len, bad);
85 ceph_decode_need(p, end, info->xattr_len, bad);
86 info->xattr_data = *p;
87 *p += info->xattr_len;
94 * parse a normal reply, which may contain a (dir+)dentry and/or a
97 static int parse_reply_info_trace(void **p, void *end,
98 struct ceph_mds_reply_info_parsed *info,
103 if (info->head->is_dentry) {
104 err = parse_reply_info_in(p, end, &info->diri, features);
108 if (unlikely(*p + sizeof(*info->dirfrag) > end))
111 *p += sizeof(*info->dirfrag) +
112 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
113 if (unlikely(*p > end))
116 ceph_decode_32_safe(p, end, info->dname_len, bad);
117 ceph_decode_need(p, end, info->dname_len, bad);
119 *p += info->dname_len;
121 *p += sizeof(*info->dlease);
124 if (info->head->is_target) {
125 err = parse_reply_info_in(p, end, &info->targeti, features);
130 if (unlikely(*p != end))
137 pr_err("problem parsing mds trace %d\n", err);
142 * parse readdir results
144 static int parse_reply_info_dir(void **p, void *end,
145 struct ceph_mds_reply_info_parsed *info,
152 if (*p + sizeof(*info->dir_dir) > end)
154 *p += sizeof(*info->dir_dir) +
155 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
159 ceph_decode_need(p, end, sizeof(num) + 2, bad);
160 num = ceph_decode_32(p);
161 info->dir_end = ceph_decode_8(p);
162 info->dir_complete = ceph_decode_8(p);
166 /* alloc large array */
168 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
169 sizeof(*info->dir_dname) +
170 sizeof(*info->dir_dname_len) +
171 sizeof(*info->dir_dlease),
173 if (info->dir_in == NULL) {
177 info->dir_dname = (void *)(info->dir_in + num);
178 info->dir_dname_len = (void *)(info->dir_dname + num);
179 info->dir_dlease = (void *)(info->dir_dname_len + num);
183 ceph_decode_need(p, end, sizeof(u32)*2, bad);
184 info->dir_dname_len[i] = ceph_decode_32(p);
185 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
186 info->dir_dname[i] = *p;
187 *p += info->dir_dname_len[i];
188 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
190 info->dir_dlease[i] = *p;
191 *p += sizeof(struct ceph_mds_reply_lease);
194 err = parse_reply_info_in(p, end, &info->dir_in[i], features);
209 pr_err("problem parsing dir contents %d\n", err);
214 * parse fcntl F_GETLK results
216 static int parse_reply_info_filelock(void **p, void *end,
217 struct ceph_mds_reply_info_parsed *info,
220 if (*p + sizeof(*info->filelock_reply) > end)
223 info->filelock_reply = *p;
224 *p += sizeof(*info->filelock_reply);
226 if (unlikely(*p != end))
235 * parse extra results
237 static int parse_reply_info_extra(void **p, void *end,
238 struct ceph_mds_reply_info_parsed *info,
241 if (info->head->op == CEPH_MDS_OP_GETFILELOCK)
242 return parse_reply_info_filelock(p, end, info, features);
244 return parse_reply_info_dir(p, end, info, features);
248 * parse entire mds reply
250 static int parse_reply_info(struct ceph_msg *msg,
251 struct ceph_mds_reply_info_parsed *info,
258 info->head = msg->front.iov_base;
259 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
260 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
263 ceph_decode_32_safe(&p, end, len, bad);
265 err = parse_reply_info_trace(&p, p+len, info, features);
271 ceph_decode_32_safe(&p, end, len, bad);
273 err = parse_reply_info_extra(&p, p+len, info, features);
279 ceph_decode_32_safe(&p, end, len, bad);
280 info->snapblob_len = len;
291 pr_err("mds parse_reply err %d\n", err);
295 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
304 static const char *session_state_name(int s)
307 case CEPH_MDS_SESSION_NEW: return "new";
308 case CEPH_MDS_SESSION_OPENING: return "opening";
309 case CEPH_MDS_SESSION_OPEN: return "open";
310 case CEPH_MDS_SESSION_HUNG: return "hung";
311 case CEPH_MDS_SESSION_CLOSING: return "closing";
312 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
313 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
314 default: return "???";
318 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
320 if (atomic_inc_not_zero(&s->s_ref)) {
321 dout("mdsc get_session %p %d -> %d\n", s,
322 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
325 dout("mdsc get_session %p 0 -- FAIL", s);
330 void ceph_put_mds_session(struct ceph_mds_session *s)
332 dout("mdsc put_session %p %d -> %d\n", s,
333 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
334 if (atomic_dec_and_test(&s->s_ref)) {
336 s->s_mdsc->fsc->client->monc.auth->ops->destroy_authorizer(
337 s->s_mdsc->fsc->client->monc.auth,
344 * called under mdsc->mutex
346 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
349 struct ceph_mds_session *session;
351 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
353 session = mdsc->sessions[mds];
354 dout("lookup_mds_session %p %d\n", session,
355 atomic_read(&session->s_ref));
356 get_session(session);
360 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
362 if (mds >= mdsc->max_sessions)
364 return mdsc->sessions[mds];
367 static int __verify_registered_session(struct ceph_mds_client *mdsc,
368 struct ceph_mds_session *s)
370 if (s->s_mds >= mdsc->max_sessions ||
371 mdsc->sessions[s->s_mds] != s)
377 * create+register a new session for given mds.
378 * called under mdsc->mutex.
380 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
383 struct ceph_mds_session *s;
385 s = kzalloc(sizeof(*s), GFP_NOFS);
387 return ERR_PTR(-ENOMEM);
390 s->s_state = CEPH_MDS_SESSION_NEW;
393 mutex_init(&s->s_mutex);
395 ceph_con_init(mdsc->fsc->client->msgr, &s->s_con);
396 s->s_con.private = s;
397 s->s_con.ops = &mds_con_ops;
398 s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
399 s->s_con.peer_name.num = cpu_to_le64(mds);
401 spin_lock_init(&s->s_cap_lock);
404 s->s_renew_requested = 0;
406 INIT_LIST_HEAD(&s->s_caps);
409 atomic_set(&s->s_ref, 1);
410 INIT_LIST_HEAD(&s->s_waiting);
411 INIT_LIST_HEAD(&s->s_unsafe);
412 s->s_num_cap_releases = 0;
413 s->s_cap_iterator = NULL;
414 INIT_LIST_HEAD(&s->s_cap_releases);
415 INIT_LIST_HEAD(&s->s_cap_releases_done);
416 INIT_LIST_HEAD(&s->s_cap_flushing);
417 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
419 dout("register_session mds%d\n", mds);
420 if (mds >= mdsc->max_sessions) {
421 int newmax = 1 << get_count_order(mds+1);
422 struct ceph_mds_session **sa;
424 dout("register_session realloc to %d\n", newmax);
425 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
428 if (mdsc->sessions) {
429 memcpy(sa, mdsc->sessions,
430 mdsc->max_sessions * sizeof(void *));
431 kfree(mdsc->sessions);
434 mdsc->max_sessions = newmax;
436 mdsc->sessions[mds] = s;
437 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
439 ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
445 return ERR_PTR(-ENOMEM);
449 * called under mdsc->mutex
451 static void __unregister_session(struct ceph_mds_client *mdsc,
452 struct ceph_mds_session *s)
454 dout("__unregister_session mds%d %p\n", s->s_mds, s);
455 BUG_ON(mdsc->sessions[s->s_mds] != s);
456 mdsc->sessions[s->s_mds] = NULL;
457 ceph_con_close(&s->s_con);
458 ceph_put_mds_session(s);
462 * drop session refs in request.
464 * should be last request ref, or hold mdsc->mutex
466 static void put_request_session(struct ceph_mds_request *req)
468 if (req->r_session) {
469 ceph_put_mds_session(req->r_session);
470 req->r_session = NULL;
474 void ceph_mdsc_release_request(struct kref *kref)
476 struct ceph_mds_request *req = container_of(kref,
477 struct ceph_mds_request,
480 ceph_msg_put(req->r_request);
482 ceph_msg_put(req->r_reply);
483 destroy_reply_info(&req->r_reply_info);
486 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
489 if (req->r_locked_dir)
490 ceph_put_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
491 if (req->r_target_inode)
492 iput(req->r_target_inode);
495 if (req->r_old_dentry) {
497 * track (and drop pins for) r_old_dentry_dir
498 * separately, since r_old_dentry's d_parent may have
499 * changed between the dir mutex being dropped and
500 * this request being freed.
502 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
504 dput(req->r_old_dentry);
505 iput(req->r_old_dentry_dir);
509 put_request_session(req);
510 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
515 * lookup session, bump ref if found.
517 * called under mdsc->mutex.
519 static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
522 struct ceph_mds_request *req;
523 struct rb_node *n = mdsc->request_tree.rb_node;
526 req = rb_entry(n, struct ceph_mds_request, r_node);
527 if (tid < req->r_tid)
529 else if (tid > req->r_tid)
532 ceph_mdsc_get_request(req);
539 static void __insert_request(struct ceph_mds_client *mdsc,
540 struct ceph_mds_request *new)
542 struct rb_node **p = &mdsc->request_tree.rb_node;
543 struct rb_node *parent = NULL;
544 struct ceph_mds_request *req = NULL;
548 req = rb_entry(parent, struct ceph_mds_request, r_node);
549 if (new->r_tid < req->r_tid)
551 else if (new->r_tid > req->r_tid)
557 rb_link_node(&new->r_node, parent, p);
558 rb_insert_color(&new->r_node, &mdsc->request_tree);
562 * Register an in-flight request, and assign a tid. Link to directory
563 * are modifying (if any).
565 * Called under mdsc->mutex.
567 static void __register_request(struct ceph_mds_client *mdsc,
568 struct ceph_mds_request *req,
571 req->r_tid = ++mdsc->last_tid;
573 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
575 dout("__register_request %p tid %lld\n", req, req->r_tid);
576 ceph_mdsc_get_request(req);
577 __insert_request(mdsc, req);
579 req->r_uid = current_fsuid();
580 req->r_gid = current_fsgid();
583 struct ceph_inode_info *ci = ceph_inode(dir);
586 spin_lock(&ci->i_unsafe_lock);
587 req->r_unsafe_dir = dir;
588 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
589 spin_unlock(&ci->i_unsafe_lock);
593 static void __unregister_request(struct ceph_mds_client *mdsc,
594 struct ceph_mds_request *req)
596 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
597 rb_erase(&req->r_node, &mdsc->request_tree);
598 RB_CLEAR_NODE(&req->r_node);
600 if (req->r_unsafe_dir) {
601 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
603 spin_lock(&ci->i_unsafe_lock);
604 list_del_init(&req->r_unsafe_dir_item);
605 spin_unlock(&ci->i_unsafe_lock);
607 iput(req->r_unsafe_dir);
608 req->r_unsafe_dir = NULL;
611 complete_all(&req->r_safe_completion);
613 ceph_mdsc_put_request(req);
617 * Choose mds to send request to next. If there is a hint set in the
618 * request (e.g., due to a prior forward hint from the mds), use that.
619 * Otherwise, consult frag tree and/or caps to identify the
620 * appropriate mds. If all else fails, choose randomly.
622 * Called under mdsc->mutex.
624 static struct dentry *get_nonsnap_parent(struct dentry *dentry)
627 * we don't need to worry about protecting the d_parent access
628 * here because we never renaming inside the snapped namespace
629 * except to resplice to another snapdir, and either the old or new
630 * result is a valid result.
632 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
633 dentry = dentry->d_parent;
637 static int __choose_mds(struct ceph_mds_client *mdsc,
638 struct ceph_mds_request *req)
641 struct ceph_inode_info *ci;
642 struct ceph_cap *cap;
643 int mode = req->r_direct_mode;
645 u32 hash = req->r_direct_hash;
646 bool is_hash = req->r_direct_is_hash;
649 * is there a specific mds we should try? ignore hint if we have
650 * no session and the mds is not up (active or recovering).
652 if (req->r_resend_mds >= 0 &&
653 (__have_session(mdsc, req->r_resend_mds) ||
654 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
655 dout("choose_mds using resend_mds mds%d\n",
657 return req->r_resend_mds;
660 if (mode == USE_RANDOM_MDS)
665 inode = req->r_inode;
666 } else if (req->r_dentry) {
667 /* ignore race with rename; old or new d_parent is okay */
668 struct dentry *parent = req->r_dentry->d_parent;
669 struct inode *dir = parent->d_inode;
671 if (dir->i_sb != mdsc->fsc->sb) {
673 inode = req->r_dentry->d_inode;
674 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
675 /* direct snapped/virtual snapdir requests
676 * based on parent dir inode */
677 struct dentry *dn = get_nonsnap_parent(parent);
679 dout("__choose_mds using nonsnap parent %p\n", inode);
680 } else if (req->r_dentry->d_inode) {
682 inode = req->r_dentry->d_inode;
686 hash = ceph_dentry_hash(dir, req->r_dentry);
691 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
695 ci = ceph_inode(inode);
697 if (is_hash && S_ISDIR(inode->i_mode)) {
698 struct ceph_inode_frag frag;
701 ceph_choose_frag(ci, hash, &frag, &found);
703 if (mode == USE_ANY_MDS && frag.ndist > 0) {
706 /* choose a random replica */
707 get_random_bytes(&r, 1);
710 dout("choose_mds %p %llx.%llx "
711 "frag %u mds%d (%d/%d)\n",
712 inode, ceph_vinop(inode),
715 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
716 CEPH_MDS_STATE_ACTIVE)
720 /* since this file/dir wasn't known to be
721 * replicated, then we want to look for the
722 * authoritative mds. */
725 /* choose auth mds */
727 dout("choose_mds %p %llx.%llx "
728 "frag %u mds%d (auth)\n",
729 inode, ceph_vinop(inode), frag.frag, mds);
730 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
731 CEPH_MDS_STATE_ACTIVE)
737 spin_lock(&ci->i_ceph_lock);
739 if (mode == USE_AUTH_MDS)
740 cap = ci->i_auth_cap;
741 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
742 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
744 spin_unlock(&ci->i_ceph_lock);
747 mds = cap->session->s_mds;
748 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
749 inode, ceph_vinop(inode), mds,
750 cap == ci->i_auth_cap ? "auth " : "", cap);
751 spin_unlock(&ci->i_ceph_lock);
755 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
756 dout("choose_mds chose random mds%d\n", mds);
764 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
766 struct ceph_msg *msg;
767 struct ceph_mds_session_head *h;
769 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
772 pr_err("create_session_msg ENOMEM creating msg\n");
775 h = msg->front.iov_base;
776 h->op = cpu_to_le32(op);
777 h->seq = cpu_to_le64(seq);
782 * send session open request.
784 * called under mdsc->mutex
786 static int __open_session(struct ceph_mds_client *mdsc,
787 struct ceph_mds_session *session)
789 struct ceph_msg *msg;
791 int mds = session->s_mds;
793 /* wait for mds to go active? */
794 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
795 dout("open_session to mds%d (%s)\n", mds,
796 ceph_mds_state_name(mstate));
797 session->s_state = CEPH_MDS_SESSION_OPENING;
798 session->s_renew_requested = jiffies;
800 /* send connect message */
801 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
804 ceph_con_send(&session->s_con, msg);
809 * open sessions for any export targets for the given mds
811 * called under mdsc->mutex
813 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
814 struct ceph_mds_session *session)
816 struct ceph_mds_info *mi;
817 struct ceph_mds_session *ts;
818 int i, mds = session->s_mds;
821 if (mds >= mdsc->mdsmap->m_max_mds)
823 mi = &mdsc->mdsmap->m_info[mds];
824 dout("open_export_target_sessions for mds%d (%d targets)\n",
825 session->s_mds, mi->num_export_targets);
827 for (i = 0; i < mi->num_export_targets; i++) {
828 target = mi->export_targets[i];
829 ts = __ceph_lookup_mds_session(mdsc, target);
831 ts = register_session(mdsc, target);
835 if (session->s_state == CEPH_MDS_SESSION_NEW ||
836 session->s_state == CEPH_MDS_SESSION_CLOSING)
837 __open_session(mdsc, session);
839 dout(" mds%d target mds%d %p is %s\n", session->s_mds,
840 i, ts, session_state_name(ts->s_state));
841 ceph_put_mds_session(ts);
845 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
846 struct ceph_mds_session *session)
848 mutex_lock(&mdsc->mutex);
849 __open_export_target_sessions(mdsc, session);
850 mutex_unlock(&mdsc->mutex);
858 * Free preallocated cap messages assigned to this session
860 static void cleanup_cap_releases(struct ceph_mds_session *session)
862 struct ceph_msg *msg;
864 spin_lock(&session->s_cap_lock);
865 while (!list_empty(&session->s_cap_releases)) {
866 msg = list_first_entry(&session->s_cap_releases,
867 struct ceph_msg, list_head);
868 list_del_init(&msg->list_head);
871 while (!list_empty(&session->s_cap_releases_done)) {
872 msg = list_first_entry(&session->s_cap_releases_done,
873 struct ceph_msg, list_head);
874 list_del_init(&msg->list_head);
877 spin_unlock(&session->s_cap_lock);
881 * Helper to safely iterate over all caps associated with a session, with
882 * special care taken to handle a racing __ceph_remove_cap().
884 * Caller must hold session s_mutex.
886 static int iterate_session_caps(struct ceph_mds_session *session,
887 int (*cb)(struct inode *, struct ceph_cap *,
891 struct ceph_cap *cap;
892 struct inode *inode, *last_inode = NULL;
893 struct ceph_cap *old_cap = NULL;
896 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
897 spin_lock(&session->s_cap_lock);
898 p = session->s_caps.next;
899 while (p != &session->s_caps) {
900 cap = list_entry(p, struct ceph_cap, session_caps);
901 inode = igrab(&cap->ci->vfs_inode);
906 session->s_cap_iterator = cap;
907 spin_unlock(&session->s_cap_lock);
914 ceph_put_cap(session->s_mdsc, old_cap);
918 ret = cb(inode, cap, arg);
921 spin_lock(&session->s_cap_lock);
923 if (cap->ci == NULL) {
924 dout("iterate_session_caps finishing cap %p removal\n",
926 BUG_ON(cap->session != session);
927 list_del_init(&cap->session_caps);
928 session->s_nr_caps--;
930 old_cap = cap; /* put_cap it w/o locks held */
937 session->s_cap_iterator = NULL;
938 spin_unlock(&session->s_cap_lock);
943 ceph_put_cap(session->s_mdsc, old_cap);
948 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
951 struct ceph_inode_info *ci = ceph_inode(inode);
954 dout("removing cap %p, ci is %p, inode is %p\n",
955 cap, ci, &ci->vfs_inode);
956 spin_lock(&ci->i_ceph_lock);
957 __ceph_remove_cap(cap);
958 if (!__ceph_is_any_real_caps(ci)) {
959 struct ceph_mds_client *mdsc =
960 ceph_sb_to_client(inode->i_sb)->mdsc;
962 spin_lock(&mdsc->cap_dirty_lock);
963 if (!list_empty(&ci->i_dirty_item)) {
964 pr_info(" dropping dirty %s state for %p %lld\n",
965 ceph_cap_string(ci->i_dirty_caps),
966 inode, ceph_ino(inode));
967 ci->i_dirty_caps = 0;
968 list_del_init(&ci->i_dirty_item);
971 if (!list_empty(&ci->i_flushing_item)) {
972 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
973 ceph_cap_string(ci->i_flushing_caps),
974 inode, ceph_ino(inode));
975 ci->i_flushing_caps = 0;
976 list_del_init(&ci->i_flushing_item);
977 mdsc->num_cap_flushing--;
980 if (drop && ci->i_wrbuffer_ref) {
981 pr_info(" dropping dirty data for %p %lld\n",
982 inode, ceph_ino(inode));
983 ci->i_wrbuffer_ref = 0;
984 ci->i_wrbuffer_ref_head = 0;
987 spin_unlock(&mdsc->cap_dirty_lock);
989 spin_unlock(&ci->i_ceph_lock);
996 * caller must hold session s_mutex
998 static void remove_session_caps(struct ceph_mds_session *session)
1000 dout("remove_session_caps on %p\n", session);
1001 iterate_session_caps(session, remove_session_caps_cb, NULL);
1002 BUG_ON(session->s_nr_caps > 0);
1003 BUG_ON(!list_empty(&session->s_cap_flushing));
1004 cleanup_cap_releases(session);
1008 * wake up any threads waiting on this session's caps. if the cap is
1009 * old (didn't get renewed on the client reconnect), remove it now.
1011 * caller must hold s_mutex.
1013 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1016 struct ceph_inode_info *ci = ceph_inode(inode);
1018 wake_up_all(&ci->i_cap_wq);
1020 spin_lock(&ci->i_ceph_lock);
1021 ci->i_wanted_max_size = 0;
1022 ci->i_requested_max_size = 0;
1023 spin_unlock(&ci->i_ceph_lock);
1028 static void wake_up_session_caps(struct ceph_mds_session *session,
1031 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1032 iterate_session_caps(session, wake_up_session_cb,
1033 (void *)(unsigned long)reconnect);
1037 * Send periodic message to MDS renewing all currently held caps. The
1038 * ack will reset the expiration for all caps from this session.
1040 * caller holds s_mutex
1042 static int send_renew_caps(struct ceph_mds_client *mdsc,
1043 struct ceph_mds_session *session)
1045 struct ceph_msg *msg;
1048 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1049 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1050 pr_info("mds%d caps stale\n", session->s_mds);
1051 session->s_renew_requested = jiffies;
1053 /* do not try to renew caps until a recovering mds has reconnected
1054 * with its clients. */
1055 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1056 if (state < CEPH_MDS_STATE_RECONNECT) {
1057 dout("send_renew_caps ignoring mds%d (%s)\n",
1058 session->s_mds, ceph_mds_state_name(state));
1062 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1063 ceph_mds_state_name(state));
1064 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1065 ++session->s_renew_seq);
1068 ceph_con_send(&session->s_con, msg);
1073 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1075 * Called under session->s_mutex
1077 static void renewed_caps(struct ceph_mds_client *mdsc,
1078 struct ceph_mds_session *session, int is_renew)
1083 spin_lock(&session->s_cap_lock);
1084 was_stale = is_renew && (session->s_cap_ttl == 0 ||
1085 time_after_eq(jiffies, session->s_cap_ttl));
1087 session->s_cap_ttl = session->s_renew_requested +
1088 mdsc->mdsmap->m_session_timeout*HZ;
1091 if (time_before(jiffies, session->s_cap_ttl)) {
1092 pr_info("mds%d caps renewed\n", session->s_mds);
1095 pr_info("mds%d caps still stale\n", session->s_mds);
1098 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1099 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1100 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1101 spin_unlock(&session->s_cap_lock);
1104 wake_up_session_caps(session, 0);
1108 * send a session close request
1110 static int request_close_session(struct ceph_mds_client *mdsc,
1111 struct ceph_mds_session *session)
1113 struct ceph_msg *msg;
1115 dout("request_close_session mds%d state %s seq %lld\n",
1116 session->s_mds, session_state_name(session->s_state),
1118 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1121 ceph_con_send(&session->s_con, msg);
1126 * Called with s_mutex held.
1128 static int __close_session(struct ceph_mds_client *mdsc,
1129 struct ceph_mds_session *session)
1131 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1133 session->s_state = CEPH_MDS_SESSION_CLOSING;
1134 return request_close_session(mdsc, session);
1138 * Trim old(er) caps.
1140 * Because we can't cache an inode without one or more caps, we do
1141 * this indirectly: if a cap is unused, we prune its aliases, at which
1142 * point the inode will hopefully get dropped to.
1144 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1145 * memory pressure from the MDS, though, so it needn't be perfect.
1147 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1149 struct ceph_mds_session *session = arg;
1150 struct ceph_inode_info *ci = ceph_inode(inode);
1151 int used, oissued, mine;
1153 if (session->s_trim_caps <= 0)
1156 spin_lock(&ci->i_ceph_lock);
1157 mine = cap->issued | cap->implemented;
1158 used = __ceph_caps_used(ci);
1159 oissued = __ceph_caps_issued_other(ci, cap);
1161 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1162 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1163 ceph_cap_string(used));
1164 if (ci->i_dirty_caps)
1165 goto out; /* dirty caps */
1166 if ((used & ~oissued) & mine)
1167 goto out; /* we need these caps */
1169 session->s_trim_caps--;
1171 /* we aren't the only cap.. just remove us */
1172 __ceph_remove_cap(cap);
1174 /* try to drop referring dentries */
1175 spin_unlock(&ci->i_ceph_lock);
1176 d_prune_aliases(inode);
1177 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1178 inode, cap, atomic_read(&inode->i_count));
1183 spin_unlock(&ci->i_ceph_lock);
1188 * Trim session cap count down to some max number.
1190 static int trim_caps(struct ceph_mds_client *mdsc,
1191 struct ceph_mds_session *session,
1194 int trim_caps = session->s_nr_caps - max_caps;
1196 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1197 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1198 if (trim_caps > 0) {
1199 session->s_trim_caps = trim_caps;
1200 iterate_session_caps(session, trim_caps_cb, session);
1201 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1202 session->s_mds, session->s_nr_caps, max_caps,
1203 trim_caps - session->s_trim_caps);
1204 session->s_trim_caps = 0;
1210 * Allocate cap_release messages. If there is a partially full message
1211 * in the queue, try to allocate enough to cover it's remainder, so that
1212 * we can send it immediately.
1214 * Called under s_mutex.
1216 int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
1217 struct ceph_mds_session *session)
1219 struct ceph_msg *msg, *partial = NULL;
1220 struct ceph_mds_cap_release *head;
1222 int extra = mdsc->fsc->mount_options->cap_release_safety;
1225 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1228 spin_lock(&session->s_cap_lock);
1230 if (!list_empty(&session->s_cap_releases)) {
1231 msg = list_first_entry(&session->s_cap_releases,
1234 head = msg->front.iov_base;
1235 num = le32_to_cpu(head->num);
1237 dout(" partial %p with (%d/%d)\n", msg, num,
1238 (int)CEPH_CAPS_PER_RELEASE);
1239 extra += CEPH_CAPS_PER_RELEASE - num;
1243 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1244 spin_unlock(&session->s_cap_lock);
1245 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1249 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1250 (int)msg->front.iov_len);
1251 head = msg->front.iov_base;
1252 head->num = cpu_to_le32(0);
1253 msg->front.iov_len = sizeof(*head);
1254 spin_lock(&session->s_cap_lock);
1255 list_add(&msg->list_head, &session->s_cap_releases);
1256 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1260 head = partial->front.iov_base;
1261 num = le32_to_cpu(head->num);
1262 dout(" queueing partial %p with %d/%d\n", partial, num,
1263 (int)CEPH_CAPS_PER_RELEASE);
1264 list_move_tail(&partial->list_head,
1265 &session->s_cap_releases_done);
1266 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
1269 spin_unlock(&session->s_cap_lock);
1275 * flush all dirty inode data to disk.
1277 * returns true if we've flushed through want_flush_seq
1279 static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1283 dout("check_cap_flush want %lld\n", want_flush_seq);
1284 mutex_lock(&mdsc->mutex);
1285 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1286 struct ceph_mds_session *session = mdsc->sessions[mds];
1290 get_session(session);
1291 mutex_unlock(&mdsc->mutex);
1293 mutex_lock(&session->s_mutex);
1294 if (!list_empty(&session->s_cap_flushing)) {
1295 struct ceph_inode_info *ci =
1296 list_entry(session->s_cap_flushing.next,
1297 struct ceph_inode_info,
1299 struct inode *inode = &ci->vfs_inode;
1301 spin_lock(&ci->i_ceph_lock);
1302 if (ci->i_cap_flush_seq <= want_flush_seq) {
1303 dout("check_cap_flush still flushing %p "
1304 "seq %lld <= %lld to mds%d\n", inode,
1305 ci->i_cap_flush_seq, want_flush_seq,
1309 spin_unlock(&ci->i_ceph_lock);
1311 mutex_unlock(&session->s_mutex);
1312 ceph_put_mds_session(session);
1316 mutex_lock(&mdsc->mutex);
1319 mutex_unlock(&mdsc->mutex);
1320 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1325 * called under s_mutex
1327 void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1328 struct ceph_mds_session *session)
1330 struct ceph_msg *msg;
1332 dout("send_cap_releases mds%d\n", session->s_mds);
1333 spin_lock(&session->s_cap_lock);
1334 while (!list_empty(&session->s_cap_releases_done)) {
1335 msg = list_first_entry(&session->s_cap_releases_done,
1336 struct ceph_msg, list_head);
1337 list_del_init(&msg->list_head);
1338 spin_unlock(&session->s_cap_lock);
1339 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1340 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1341 ceph_con_send(&session->s_con, msg);
1342 spin_lock(&session->s_cap_lock);
1344 spin_unlock(&session->s_cap_lock);
1347 static void discard_cap_releases(struct ceph_mds_client *mdsc,
1348 struct ceph_mds_session *session)
1350 struct ceph_msg *msg;
1351 struct ceph_mds_cap_release *head;
1354 dout("discard_cap_releases mds%d\n", session->s_mds);
1355 spin_lock(&session->s_cap_lock);
1357 /* zero out the in-progress message */
1358 msg = list_first_entry(&session->s_cap_releases,
1359 struct ceph_msg, list_head);
1360 head = msg->front.iov_base;
1361 num = le32_to_cpu(head->num);
1362 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1363 head->num = cpu_to_le32(0);
1364 session->s_num_cap_releases += num;
1366 /* requeue completed messages */
1367 while (!list_empty(&session->s_cap_releases_done)) {
1368 msg = list_first_entry(&session->s_cap_releases_done,
1369 struct ceph_msg, list_head);
1370 list_del_init(&msg->list_head);
1372 head = msg->front.iov_base;
1373 num = le32_to_cpu(head->num);
1374 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1376 session->s_num_cap_releases += num;
1377 head->num = cpu_to_le32(0);
1378 msg->front.iov_len = sizeof(*head);
1379 list_add(&msg->list_head, &session->s_cap_releases);
1382 spin_unlock(&session->s_cap_lock);
1390 * Create an mds request.
1392 struct ceph_mds_request *
1393 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1395 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1398 return ERR_PTR(-ENOMEM);
1400 mutex_init(&req->r_fill_mutex);
1402 req->r_started = jiffies;
1403 req->r_resend_mds = -1;
1404 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1406 kref_init(&req->r_kref);
1407 INIT_LIST_HEAD(&req->r_wait);
1408 init_completion(&req->r_completion);
1409 init_completion(&req->r_safe_completion);
1410 INIT_LIST_HEAD(&req->r_unsafe_item);
1413 req->r_direct_mode = mode;
1418 * return oldest (lowest) request, tid in request tree, 0 if none.
1420 * called under mdsc->mutex.
1422 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1424 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1426 return rb_entry(rb_first(&mdsc->request_tree),
1427 struct ceph_mds_request, r_node);
1430 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1432 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1440 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1441 * on build_path_from_dentry in fs/cifs/dir.c.
1443 * If @stop_on_nosnap, generate path relative to the first non-snapped
1446 * Encode hidden .snap dirs as a double /, i.e.
1447 * foo/.snap/bar -> foo//bar
1449 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1452 struct dentry *temp;
1458 return ERR_PTR(-EINVAL);
1462 seq = read_seqbegin(&rename_lock);
1464 for (temp = dentry; !IS_ROOT(temp);) {
1465 struct inode *inode = temp->d_inode;
1466 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1467 len++; /* slash only */
1468 else if (stop_on_nosnap && inode &&
1469 ceph_snap(inode) == CEPH_NOSNAP)
1472 len += 1 + temp->d_name.len;
1473 temp = temp->d_parent;
1476 pr_err("build_path corrupt dentry %p\n", dentry);
1477 return ERR_PTR(-EINVAL);
1482 len--; /* no leading '/' */
1484 path = kmalloc(len+1, GFP_NOFS);
1486 return ERR_PTR(-ENOMEM);
1488 path[pos] = 0; /* trailing null */
1490 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1491 struct inode *inode;
1493 spin_lock(&temp->d_lock);
1494 inode = temp->d_inode;
1495 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1496 dout("build_path path+%d: %p SNAPDIR\n",
1498 } else if (stop_on_nosnap && inode &&
1499 ceph_snap(inode) == CEPH_NOSNAP) {
1500 spin_unlock(&temp->d_lock);
1503 pos -= temp->d_name.len;
1505 spin_unlock(&temp->d_lock);
1508 strncpy(path + pos, temp->d_name.name,
1511 spin_unlock(&temp->d_lock);
1514 temp = temp->d_parent;
1517 pr_err("build_path corrupt dentry\n");
1519 return ERR_PTR(-EINVAL);
1523 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
1524 pr_err("build_path did not end path lookup where "
1525 "expected, namelen is %d, pos is %d\n", len, pos);
1526 /* presumably this is only possible if racing with a
1527 rename of one of the parent directories (we can not
1528 lock the dentries above us to prevent this, but
1529 retrying should be harmless) */
1534 *base = ceph_ino(temp->d_inode);
1536 dout("build_path on %p %d built %llx '%.*s'\n",
1537 dentry, dentry->d_count, *base, len, path);
1541 static int build_dentry_path(struct dentry *dentry,
1542 const char **ppath, int *ppathlen, u64 *pino,
1547 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1548 *pino = ceph_ino(dentry->d_parent->d_inode);
1549 *ppath = dentry->d_name.name;
1550 *ppathlen = dentry->d_name.len;
1553 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1555 return PTR_ERR(path);
1561 static int build_inode_path(struct inode *inode,
1562 const char **ppath, int *ppathlen, u64 *pino,
1565 struct dentry *dentry;
1568 if (ceph_snap(inode) == CEPH_NOSNAP) {
1569 *pino = ceph_ino(inode);
1573 dentry = d_find_alias(inode);
1574 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1577 return PTR_ERR(path);
1584 * request arguments may be specified via an inode *, a dentry *, or
1585 * an explicit ino+path.
1587 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1588 const char *rpath, u64 rino,
1589 const char **ppath, int *pathlen,
1590 u64 *ino, int *freepath)
1595 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1596 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1598 } else if (rdentry) {
1599 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1600 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1602 } else if (rpath || rino) {
1605 *pathlen = strlen(rpath);
1606 dout(" path %.*s\n", *pathlen, rpath);
1613 * called under mdsc->mutex
1615 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1616 struct ceph_mds_request *req,
1619 struct ceph_msg *msg;
1620 struct ceph_mds_request_head *head;
1621 const char *path1 = NULL;
1622 const char *path2 = NULL;
1623 u64 ino1 = 0, ino2 = 0;
1624 int pathlen1 = 0, pathlen2 = 0;
1625 int freepath1 = 0, freepath2 = 0;
1631 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1632 req->r_path1, req->r_ino1.ino,
1633 &path1, &pathlen1, &ino1, &freepath1);
1639 ret = set_request_path_attr(NULL, req->r_old_dentry,
1640 req->r_path2, req->r_ino2.ino,
1641 &path2, &pathlen2, &ino2, &freepath2);
1647 len = sizeof(*head) +
1648 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
1650 /* calculate (max) length for cap releases */
1651 len += sizeof(struct ceph_mds_request_release) *
1652 (!!req->r_inode_drop + !!req->r_dentry_drop +
1653 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1654 if (req->r_dentry_drop)
1655 len += req->r_dentry->d_name.len;
1656 if (req->r_old_dentry_drop)
1657 len += req->r_old_dentry->d_name.len;
1659 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
1661 msg = ERR_PTR(-ENOMEM);
1665 msg->hdr.tid = cpu_to_le64(req->r_tid);
1667 head = msg->front.iov_base;
1668 p = msg->front.iov_base + sizeof(*head);
1669 end = msg->front.iov_base + msg->front.iov_len;
1671 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1672 head->op = cpu_to_le32(req->r_op);
1673 head->caller_uid = cpu_to_le32(req->r_uid);
1674 head->caller_gid = cpu_to_le32(req->r_gid);
1675 head->args = req->r_args;
1677 ceph_encode_filepath(&p, end, ino1, path1);
1678 ceph_encode_filepath(&p, end, ino2, path2);
1680 /* make note of release offset, in case we need to replay */
1681 req->r_request_release_offset = p - msg->front.iov_base;
1685 if (req->r_inode_drop)
1686 releases += ceph_encode_inode_release(&p,
1687 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1688 mds, req->r_inode_drop, req->r_inode_unless, 0);
1689 if (req->r_dentry_drop)
1690 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1691 mds, req->r_dentry_drop, req->r_dentry_unless);
1692 if (req->r_old_dentry_drop)
1693 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1694 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1695 if (req->r_old_inode_drop)
1696 releases += ceph_encode_inode_release(&p,
1697 req->r_old_dentry->d_inode,
1698 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1699 head->num_releases = cpu_to_le16(releases);
1702 msg->front.iov_len = p - msg->front.iov_base;
1703 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1705 msg->pages = req->r_pages;
1706 msg->nr_pages = req->r_num_pages;
1707 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1708 msg->hdr.data_off = cpu_to_le16(0);
1712 kfree((char *)path2);
1715 kfree((char *)path1);
1721 * called under mdsc->mutex if error, under no mutex if
1724 static void complete_request(struct ceph_mds_client *mdsc,
1725 struct ceph_mds_request *req)
1727 if (req->r_callback)
1728 req->r_callback(mdsc, req);
1730 complete_all(&req->r_completion);
1734 * called under mdsc->mutex
1736 static int __prepare_send_request(struct ceph_mds_client *mdsc,
1737 struct ceph_mds_request *req,
1740 struct ceph_mds_request_head *rhead;
1741 struct ceph_msg *msg;
1746 struct ceph_cap *cap =
1747 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1750 req->r_sent_on_mseq = cap->mseq;
1752 req->r_sent_on_mseq = -1;
1754 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1755 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1757 if (req->r_got_unsafe) {
1759 * Replay. Do not regenerate message (and rebuild
1760 * paths, etc.); just use the original message.
1761 * Rebuilding paths will break for renames because
1762 * d_move mangles the src name.
1764 msg = req->r_request;
1765 rhead = msg->front.iov_base;
1767 flags = le32_to_cpu(rhead->flags);
1768 flags |= CEPH_MDS_FLAG_REPLAY;
1769 rhead->flags = cpu_to_le32(flags);
1771 if (req->r_target_inode)
1772 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1774 rhead->num_retry = req->r_attempts - 1;
1776 /* remove cap/dentry releases from message */
1777 rhead->num_releases = 0;
1778 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1779 msg->front.iov_len = req->r_request_release_offset;
1783 if (req->r_request) {
1784 ceph_msg_put(req->r_request);
1785 req->r_request = NULL;
1787 msg = create_request_message(mdsc, req, mds);
1789 req->r_err = PTR_ERR(msg);
1790 complete_request(mdsc, req);
1791 return PTR_ERR(msg);
1793 req->r_request = msg;
1795 rhead = msg->front.iov_base;
1796 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1797 if (req->r_got_unsafe)
1798 flags |= CEPH_MDS_FLAG_REPLAY;
1799 if (req->r_locked_dir)
1800 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1801 rhead->flags = cpu_to_le32(flags);
1802 rhead->num_fwd = req->r_num_fwd;
1803 rhead->num_retry = req->r_attempts - 1;
1806 dout(" r_locked_dir = %p\n", req->r_locked_dir);
1811 * send request, or put it on the appropriate wait list.
1813 static int __do_request(struct ceph_mds_client *mdsc,
1814 struct ceph_mds_request *req)
1816 struct ceph_mds_session *session = NULL;
1820 if (req->r_err || req->r_got_result) {
1822 __unregister_request(mdsc, req);
1826 if (req->r_timeout &&
1827 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1828 dout("do_request timed out\n");
1833 put_request_session(req);
1835 mds = __choose_mds(mdsc, req);
1837 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1838 dout("do_request no mds or not active, waiting for map\n");
1839 list_add(&req->r_wait, &mdsc->waiting_for_map);
1843 /* get, open session */
1844 session = __ceph_lookup_mds_session(mdsc, mds);
1846 session = register_session(mdsc, mds);
1847 if (IS_ERR(session)) {
1848 err = PTR_ERR(session);
1852 req->r_session = get_session(session);
1854 dout("do_request mds%d session %p state %s\n", mds, session,
1855 session_state_name(session->s_state));
1856 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1857 session->s_state != CEPH_MDS_SESSION_HUNG) {
1858 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1859 session->s_state == CEPH_MDS_SESSION_CLOSING)
1860 __open_session(mdsc, session);
1861 list_add(&req->r_wait, &session->s_waiting);
1866 req->r_resend_mds = -1; /* forget any previous mds hint */
1868 if (req->r_request_started == 0) /* note request start time */
1869 req->r_request_started = jiffies;
1871 err = __prepare_send_request(mdsc, req, mds);
1873 ceph_msg_get(req->r_request);
1874 ceph_con_send(&session->s_con, req->r_request);
1878 ceph_put_mds_session(session);
1884 complete_request(mdsc, req);
1889 * called under mdsc->mutex
1891 static void __wake_requests(struct ceph_mds_client *mdsc,
1892 struct list_head *head)
1894 struct ceph_mds_request *req, *nreq;
1896 list_for_each_entry_safe(req, nreq, head, r_wait) {
1897 list_del_init(&req->r_wait);
1898 __do_request(mdsc, req);
1903 * Wake up threads with requests pending for @mds, so that they can
1904 * resubmit their requests to a possibly different mds.
1906 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
1908 struct ceph_mds_request *req;
1911 dout("kick_requests mds%d\n", mds);
1912 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1913 req = rb_entry(p, struct ceph_mds_request, r_node);
1914 if (req->r_got_unsafe)
1916 if (req->r_session &&
1917 req->r_session->s_mds == mds) {
1918 dout(" kicking tid %llu\n", req->r_tid);
1919 __do_request(mdsc, req);
1924 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1925 struct ceph_mds_request *req)
1927 dout("submit_request on %p\n", req);
1928 mutex_lock(&mdsc->mutex);
1929 __register_request(mdsc, req, NULL);
1930 __do_request(mdsc, req);
1931 mutex_unlock(&mdsc->mutex);
1935 * Synchrously perform an mds request. Take care of all of the
1936 * session setup, forwarding, retry details.
1938 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1940 struct ceph_mds_request *req)
1944 dout("do_request on %p\n", req);
1946 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1948 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1949 if (req->r_locked_dir)
1950 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1951 if (req->r_old_dentry)
1952 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
1956 mutex_lock(&mdsc->mutex);
1957 __register_request(mdsc, req, dir);
1958 __do_request(mdsc, req);
1962 __unregister_request(mdsc, req);
1963 dout("do_request early error %d\n", err);
1968 mutex_unlock(&mdsc->mutex);
1969 dout("do_request waiting\n");
1970 if (req->r_timeout) {
1971 err = (long)wait_for_completion_killable_timeout(
1972 &req->r_completion, req->r_timeout);
1976 err = wait_for_completion_killable(&req->r_completion);
1978 dout("do_request waited, got %d\n", err);
1979 mutex_lock(&mdsc->mutex);
1981 /* only abort if we didn't race with a real reply */
1982 if (req->r_got_result) {
1983 err = le32_to_cpu(req->r_reply_info.head->result);
1984 } else if (err < 0) {
1985 dout("aborted request %lld with %d\n", req->r_tid, err);
1988 * ensure we aren't running concurrently with
1989 * ceph_fill_trace or ceph_readdir_prepopulate, which
1990 * rely on locks (dir mutex) held by our caller.
1992 mutex_lock(&req->r_fill_mutex);
1994 req->r_aborted = true;
1995 mutex_unlock(&req->r_fill_mutex);
1997 if (req->r_locked_dir &&
1998 (req->r_op & CEPH_MDS_OP_WRITE))
1999 ceph_invalidate_dir_request(req);
2005 mutex_unlock(&mdsc->mutex);
2006 dout("do_request %p done, result %d\n", req, err);
2011 * Invalidate dir D_COMPLETE, dentry lease state on an aborted MDS
2012 * namespace request.
2014 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2016 struct inode *inode = req->r_locked_dir;
2017 struct ceph_inode_info *ci = ceph_inode(inode);
2019 dout("invalidate_dir_request %p (D_COMPLETE, lease(s))\n", inode);
2020 spin_lock(&ci->i_ceph_lock);
2021 ceph_dir_clear_complete(inode);
2022 ci->i_release_count++;
2023 spin_unlock(&ci->i_ceph_lock);
2026 ceph_invalidate_dentry_lease(req->r_dentry);
2027 if (req->r_old_dentry)
2028 ceph_invalidate_dentry_lease(req->r_old_dentry);
2034 * We take the session mutex and parse and process the reply immediately.
2035 * This preserves the logical ordering of replies, capabilities, etc., sent
2036 * by the MDS as they are applied to our local cache.
2038 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2040 struct ceph_mds_client *mdsc = session->s_mdsc;
2041 struct ceph_mds_request *req;
2042 struct ceph_mds_reply_head *head = msg->front.iov_base;
2043 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2046 int mds = session->s_mds;
2048 if (msg->front.iov_len < sizeof(*head)) {
2049 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2054 /* get request, session */
2055 tid = le64_to_cpu(msg->hdr.tid);
2056 mutex_lock(&mdsc->mutex);
2057 req = __lookup_request(mdsc, tid);
2059 dout("handle_reply on unknown tid %llu\n", tid);
2060 mutex_unlock(&mdsc->mutex);
2063 dout("handle_reply %p\n", req);
2065 /* correct session? */
2066 if (req->r_session != session) {
2067 pr_err("mdsc_handle_reply got %llu on session mds%d"
2068 " not mds%d\n", tid, session->s_mds,
2069 req->r_session ? req->r_session->s_mds : -1);
2070 mutex_unlock(&mdsc->mutex);
2075 if ((req->r_got_unsafe && !head->safe) ||
2076 (req->r_got_safe && head->safe)) {
2077 pr_warning("got a dup %s reply on %llu from mds%d\n",
2078 head->safe ? "safe" : "unsafe", tid, mds);
2079 mutex_unlock(&mdsc->mutex);
2082 if (req->r_got_safe && !head->safe) {
2083 pr_warning("got unsafe after safe on %llu from mds%d\n",
2085 mutex_unlock(&mdsc->mutex);
2089 result = le32_to_cpu(head->result);
2093 * if we're not talking to the authority, send to them
2094 * if the authority has changed while we weren't looking,
2095 * send to new authority
2096 * Otherwise we just have to return an ESTALE
2098 if (result == -ESTALE) {
2099 dout("got ESTALE on request %llu", req->r_tid);
2100 if (!req->r_inode) {
2101 /* do nothing; not an authority problem */
2102 } else if (req->r_direct_mode != USE_AUTH_MDS) {
2103 dout("not using auth, setting for that now");
2104 req->r_direct_mode = USE_AUTH_MDS;
2105 __do_request(mdsc, req);
2106 mutex_unlock(&mdsc->mutex);
2109 struct ceph_inode_info *ci = ceph_inode(req->r_inode);
2110 struct ceph_cap *cap = NULL;
2113 cap = ceph_get_cap_for_mds(ci,
2114 req->r_session->s_mds);
2116 dout("already using auth");
2117 if ((!cap || cap != ci->i_auth_cap) ||
2118 (cap->mseq != req->r_sent_on_mseq)) {
2119 dout("but cap changed, so resending");
2120 __do_request(mdsc, req);
2121 mutex_unlock(&mdsc->mutex);
2125 dout("have to return ESTALE on request %llu", req->r_tid);
2130 req->r_got_safe = true;
2131 __unregister_request(mdsc, req);
2133 if (req->r_got_unsafe) {
2135 * We already handled the unsafe response, now do the
2136 * cleanup. No need to examine the response; the MDS
2137 * doesn't include any result info in the safe
2138 * response. And even if it did, there is nothing
2139 * useful we could do with a revised return value.
2141 dout("got safe reply %llu, mds%d\n", tid, mds);
2142 list_del_init(&req->r_unsafe_item);
2144 /* last unsafe request during umount? */
2145 if (mdsc->stopping && !__get_oldest_req(mdsc))
2146 complete_all(&mdsc->safe_umount_waiters);
2147 mutex_unlock(&mdsc->mutex);
2151 req->r_got_unsafe = true;
2152 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2155 dout("handle_reply tid %lld result %d\n", tid, result);
2156 rinfo = &req->r_reply_info;
2157 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2158 mutex_unlock(&mdsc->mutex);
2160 mutex_lock(&session->s_mutex);
2162 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
2168 if (rinfo->snapblob_len) {
2169 down_write(&mdsc->snap_rwsem);
2170 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2171 rinfo->snapblob + rinfo->snapblob_len,
2172 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2173 downgrade_write(&mdsc->snap_rwsem);
2175 down_read(&mdsc->snap_rwsem);
2178 /* insert trace into our cache */
2179 mutex_lock(&req->r_fill_mutex);
2180 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
2182 if (result == 0 && req->r_op != CEPH_MDS_OP_GETFILELOCK &&
2184 ceph_readdir_prepopulate(req, req->r_session);
2185 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2187 mutex_unlock(&req->r_fill_mutex);
2189 up_read(&mdsc->snap_rwsem);
2191 mutex_lock(&mdsc->mutex);
2192 if (!req->r_aborted) {
2198 req->r_got_result = true;
2201 dout("reply arrived after request %lld was aborted\n", tid);
2203 mutex_unlock(&mdsc->mutex);
2205 ceph_add_cap_releases(mdsc, req->r_session);
2206 mutex_unlock(&session->s_mutex);
2208 /* kick calling process */
2209 complete_request(mdsc, req);
2211 ceph_mdsc_put_request(req);
2218 * handle mds notification that our request has been forwarded.
2220 static void handle_forward(struct ceph_mds_client *mdsc,
2221 struct ceph_mds_session *session,
2222 struct ceph_msg *msg)
2224 struct ceph_mds_request *req;
2225 u64 tid = le64_to_cpu(msg->hdr.tid);
2229 void *p = msg->front.iov_base;
2230 void *end = p + msg->front.iov_len;
2232 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2233 next_mds = ceph_decode_32(&p);
2234 fwd_seq = ceph_decode_32(&p);
2236 mutex_lock(&mdsc->mutex);
2237 req = __lookup_request(mdsc, tid);
2239 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2240 goto out; /* dup reply? */
2243 if (req->r_aborted) {
2244 dout("forward tid %llu aborted, unregistering\n", tid);
2245 __unregister_request(mdsc, req);
2246 } else if (fwd_seq <= req->r_num_fwd) {
2247 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2248 tid, next_mds, req->r_num_fwd, fwd_seq);
2250 /* resend. forward race not possible; mds would drop */
2251 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2253 BUG_ON(req->r_got_result);
2254 req->r_num_fwd = fwd_seq;
2255 req->r_resend_mds = next_mds;
2256 put_request_session(req);
2257 __do_request(mdsc, req);
2259 ceph_mdsc_put_request(req);
2261 mutex_unlock(&mdsc->mutex);
2265 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2269 * handle a mds session control message
2271 static void handle_session(struct ceph_mds_session *session,
2272 struct ceph_msg *msg)
2274 struct ceph_mds_client *mdsc = session->s_mdsc;
2277 int mds = session->s_mds;
2278 struct ceph_mds_session_head *h = msg->front.iov_base;
2282 if (msg->front.iov_len != sizeof(*h))
2284 op = le32_to_cpu(h->op);
2285 seq = le64_to_cpu(h->seq);
2287 mutex_lock(&mdsc->mutex);
2288 if (op == CEPH_SESSION_CLOSE)
2289 __unregister_session(mdsc, session);
2290 /* FIXME: this ttl calculation is generous */
2291 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2292 mutex_unlock(&mdsc->mutex);
2294 mutex_lock(&session->s_mutex);
2296 dout("handle_session mds%d %s %p state %s seq %llu\n",
2297 mds, ceph_session_op_name(op), session,
2298 session_state_name(session->s_state), seq);
2300 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2301 session->s_state = CEPH_MDS_SESSION_OPEN;
2302 pr_info("mds%d came back\n", session->s_mds);
2306 case CEPH_SESSION_OPEN:
2307 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2308 pr_info("mds%d reconnect success\n", session->s_mds);
2309 session->s_state = CEPH_MDS_SESSION_OPEN;
2310 renewed_caps(mdsc, session, 0);
2313 __close_session(mdsc, session);
2316 case CEPH_SESSION_RENEWCAPS:
2317 if (session->s_renew_seq == seq)
2318 renewed_caps(mdsc, session, 1);
2321 case CEPH_SESSION_CLOSE:
2322 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2323 pr_info("mds%d reconnect denied\n", session->s_mds);
2324 remove_session_caps(session);
2325 wake = 1; /* for good measure */
2326 wake_up_all(&mdsc->session_close_wq);
2327 kick_requests(mdsc, mds);
2330 case CEPH_SESSION_STALE:
2331 pr_info("mds%d caps went stale, renewing\n",
2333 spin_lock(&session->s_cap_lock);
2334 session->s_cap_gen++;
2335 session->s_cap_ttl = 0;
2336 spin_unlock(&session->s_cap_lock);
2337 send_renew_caps(mdsc, session);
2340 case CEPH_SESSION_RECALL_STATE:
2341 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2345 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2349 mutex_unlock(&session->s_mutex);
2351 mutex_lock(&mdsc->mutex);
2352 __wake_requests(mdsc, &session->s_waiting);
2353 mutex_unlock(&mdsc->mutex);
2358 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2359 (int)msg->front.iov_len);
2366 * called under session->mutex.
2368 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2369 struct ceph_mds_session *session)
2371 struct ceph_mds_request *req, *nreq;
2374 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2376 mutex_lock(&mdsc->mutex);
2377 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2378 err = __prepare_send_request(mdsc, req, session->s_mds);
2380 ceph_msg_get(req->r_request);
2381 ceph_con_send(&session->s_con, req->r_request);
2384 mutex_unlock(&mdsc->mutex);
2388 * Encode information about a cap for a reconnect with the MDS.
2390 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2394 struct ceph_mds_cap_reconnect v2;
2395 struct ceph_mds_cap_reconnect_v1 v1;
2398 struct ceph_inode_info *ci;
2399 struct ceph_reconnect_state *recon_state = arg;
2400 struct ceph_pagelist *pagelist = recon_state->pagelist;
2404 struct dentry *dentry;
2408 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2409 inode, ceph_vinop(inode), cap, cap->cap_id,
2410 ceph_cap_string(cap->issued));
2411 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2415 dentry = d_find_alias(inode);
2417 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2419 err = PTR_ERR(path);
2426 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2430 spin_lock(&ci->i_ceph_lock);
2431 cap->seq = 0; /* reset cap seq */
2432 cap->issue_seq = 0; /* and issue_seq */
2434 if (recon_state->flock) {
2435 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2436 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2437 rec.v2.issued = cpu_to_le32(cap->issued);
2438 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2439 rec.v2.pathbase = cpu_to_le64(pathbase);
2440 rec.v2.flock_len = 0;
2441 reclen = sizeof(rec.v2);
2443 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2444 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2445 rec.v1.issued = cpu_to_le32(cap->issued);
2446 rec.v1.size = cpu_to_le64(inode->i_size);
2447 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2448 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2449 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2450 rec.v1.pathbase = cpu_to_le64(pathbase);
2451 reclen = sizeof(rec.v1);
2453 spin_unlock(&ci->i_ceph_lock);
2455 if (recon_state->flock) {
2456 int num_fcntl_locks, num_flock_locks;
2457 struct ceph_pagelist_cursor trunc_point;
2459 ceph_pagelist_set_cursor(pagelist, &trunc_point);
2462 ceph_count_locks(inode, &num_fcntl_locks,
2464 rec.v2.flock_len = (2*sizeof(u32) +
2465 (num_fcntl_locks+num_flock_locks) *
2466 sizeof(struct ceph_filelock));
2469 /* pre-alloc pagelist */
2470 ceph_pagelist_truncate(pagelist, &trunc_point);
2471 err = ceph_pagelist_append(pagelist, &rec, reclen);
2473 err = ceph_pagelist_reserve(pagelist,
2479 err = ceph_encode_locks(inode,
2485 } while (err == -ENOSPC);
2487 err = ceph_pagelist_append(pagelist, &rec, reclen);
2499 * If an MDS fails and recovers, clients need to reconnect in order to
2500 * reestablish shared state. This includes all caps issued through
2501 * this session _and_ the snap_realm hierarchy. Because it's not
2502 * clear which snap realms the mds cares about, we send everything we
2503 * know about.. that ensures we'll then get any new info the
2504 * recovering MDS might have.
2506 * This is a relatively heavyweight operation, but it's rare.
2508 * called with mdsc->mutex held.
2510 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2511 struct ceph_mds_session *session)
2513 struct ceph_msg *reply;
2515 int mds = session->s_mds;
2517 struct ceph_pagelist *pagelist;
2518 struct ceph_reconnect_state recon_state;
2520 pr_info("mds%d reconnect start\n", mds);
2522 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2524 goto fail_nopagelist;
2525 ceph_pagelist_init(pagelist);
2527 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
2531 mutex_lock(&session->s_mutex);
2532 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2535 ceph_con_open(&session->s_con,
2536 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2538 /* replay unsafe requests */
2539 replay_unsafe_requests(mdsc, session);
2541 down_read(&mdsc->snap_rwsem);
2543 dout("session %p state %s\n", session,
2544 session_state_name(session->s_state));
2546 /* drop old cap expires; we're about to reestablish that state */
2547 discard_cap_releases(mdsc, session);
2549 /* traverse this session's caps */
2550 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2554 recon_state.pagelist = pagelist;
2555 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2556 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
2561 * snaprealms. we provide mds with the ino, seq (version), and
2562 * parent for all of our realms. If the mds has any newer info,
2565 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2566 struct ceph_snap_realm *realm =
2567 rb_entry(p, struct ceph_snap_realm, node);
2568 struct ceph_mds_snaprealm_reconnect sr_rec;
2570 dout(" adding snap realm %llx seq %lld parent %llx\n",
2571 realm->ino, realm->seq, realm->parent_ino);
2572 sr_rec.ino = cpu_to_le64(realm->ino);
2573 sr_rec.seq = cpu_to_le64(realm->seq);
2574 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2575 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2580 reply->pagelist = pagelist;
2581 if (recon_state.flock)
2582 reply->hdr.version = cpu_to_le16(2);
2583 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2584 reply->nr_pages = calc_pages_for(0, pagelist->length);
2585 ceph_con_send(&session->s_con, reply);
2587 mutex_unlock(&session->s_mutex);
2589 mutex_lock(&mdsc->mutex);
2590 __wake_requests(mdsc, &session->s_waiting);
2591 mutex_unlock(&mdsc->mutex);
2593 up_read(&mdsc->snap_rwsem);
2597 ceph_msg_put(reply);
2598 up_read(&mdsc->snap_rwsem);
2599 mutex_unlock(&session->s_mutex);
2601 ceph_pagelist_release(pagelist);
2604 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
2610 * compare old and new mdsmaps, kicking requests
2611 * and closing out old connections as necessary
2613 * called under mdsc->mutex.
2615 static void check_new_map(struct ceph_mds_client *mdsc,
2616 struct ceph_mdsmap *newmap,
2617 struct ceph_mdsmap *oldmap)
2620 int oldstate, newstate;
2621 struct ceph_mds_session *s;
2623 dout("check_new_map new %u old %u\n",
2624 newmap->m_epoch, oldmap->m_epoch);
2626 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2627 if (mdsc->sessions[i] == NULL)
2629 s = mdsc->sessions[i];
2630 oldstate = ceph_mdsmap_get_state(oldmap, i);
2631 newstate = ceph_mdsmap_get_state(newmap, i);
2633 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2634 i, ceph_mds_state_name(oldstate),
2635 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
2636 ceph_mds_state_name(newstate),
2637 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
2638 session_state_name(s->s_state));
2640 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2641 ceph_mdsmap_get_addr(newmap, i),
2642 sizeof(struct ceph_entity_addr))) {
2643 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2644 /* the session never opened, just close it
2646 __wake_requests(mdsc, &s->s_waiting);
2647 __unregister_session(mdsc, s);
2650 mutex_unlock(&mdsc->mutex);
2651 mutex_lock(&s->s_mutex);
2652 mutex_lock(&mdsc->mutex);
2653 ceph_con_close(&s->s_con);
2654 mutex_unlock(&s->s_mutex);
2655 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2658 /* kick any requests waiting on the recovering mds */
2659 kick_requests(mdsc, i);
2660 } else if (oldstate == newstate) {
2661 continue; /* nothing new with this mds */
2667 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
2668 newstate >= CEPH_MDS_STATE_RECONNECT) {
2669 mutex_unlock(&mdsc->mutex);
2670 send_mds_reconnect(mdsc, s);
2671 mutex_lock(&mdsc->mutex);
2675 * kick request on any mds that has gone active.
2677 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2678 newstate >= CEPH_MDS_STATE_ACTIVE) {
2679 if (oldstate != CEPH_MDS_STATE_CREATING &&
2680 oldstate != CEPH_MDS_STATE_STARTING)
2681 pr_info("mds%d recovery completed\n", s->s_mds);
2682 kick_requests(mdsc, i);
2683 ceph_kick_flushing_caps(mdsc, s);
2684 wake_up_session_caps(s, 1);
2688 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2689 s = mdsc->sessions[i];
2692 if (!ceph_mdsmap_is_laggy(newmap, i))
2694 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2695 s->s_state == CEPH_MDS_SESSION_HUNG ||
2696 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2697 dout(" connecting to export targets of laggy mds%d\n",
2699 __open_export_target_sessions(mdsc, s);
2711 * caller must hold session s_mutex, dentry->d_lock
2713 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2715 struct ceph_dentry_info *di = ceph_dentry(dentry);
2717 ceph_put_mds_session(di->lease_session);
2718 di->lease_session = NULL;
2721 static void handle_lease(struct ceph_mds_client *mdsc,
2722 struct ceph_mds_session *session,
2723 struct ceph_msg *msg)
2725 struct super_block *sb = mdsc->fsc->sb;
2726 struct inode *inode;
2727 struct dentry *parent, *dentry;
2728 struct ceph_dentry_info *di;
2729 int mds = session->s_mds;
2730 struct ceph_mds_lease *h = msg->front.iov_base;
2732 struct ceph_vino vino;
2736 dout("handle_lease from mds%d\n", mds);
2739 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2741 vino.ino = le64_to_cpu(h->ino);
2742 vino.snap = CEPH_NOSNAP;
2743 seq = le32_to_cpu(h->seq);
2744 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2745 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2746 if (dname.len != get_unaligned_le32(h+1))
2749 mutex_lock(&session->s_mutex);
2753 inode = ceph_find_inode(sb, vino);
2754 dout("handle_lease %s, ino %llx %p %.*s\n",
2755 ceph_lease_op_name(h->action), vino.ino, inode,
2756 dname.len, dname.name);
2757 if (inode == NULL) {
2758 dout("handle_lease no inode %llx\n", vino.ino);
2763 parent = d_find_alias(inode);
2765 dout("no parent dentry on inode %p\n", inode);
2767 goto release; /* hrm... */
2769 dname.hash = full_name_hash(dname.name, dname.len);
2770 dentry = d_lookup(parent, &dname);
2775 spin_lock(&dentry->d_lock);
2776 di = ceph_dentry(dentry);
2777 switch (h->action) {
2778 case CEPH_MDS_LEASE_REVOKE:
2779 if (di && di->lease_session == session) {
2780 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2781 h->seq = cpu_to_le32(di->lease_seq);
2782 __ceph_mdsc_drop_dentry_lease(dentry);
2787 case CEPH_MDS_LEASE_RENEW:
2788 if (di && di->lease_session == session &&
2789 di->lease_gen == session->s_cap_gen &&
2790 di->lease_renew_from &&
2791 di->lease_renew_after == 0) {
2792 unsigned long duration =
2793 le32_to_cpu(h->duration_ms) * HZ / 1000;
2795 di->lease_seq = seq;
2796 dentry->d_time = di->lease_renew_from + duration;
2797 di->lease_renew_after = di->lease_renew_from +
2799 di->lease_renew_from = 0;
2803 spin_unlock(&dentry->d_lock);
2810 /* let's just reuse the same message */
2811 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2813 ceph_con_send(&session->s_con, msg);
2817 mutex_unlock(&session->s_mutex);
2821 pr_err("corrupt lease message\n");
2825 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2826 struct inode *inode,
2827 struct dentry *dentry, char action,
2830 struct ceph_msg *msg;
2831 struct ceph_mds_lease *lease;
2832 int len = sizeof(*lease) + sizeof(u32);
2835 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2836 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2837 dnamelen = dentry->d_name.len;
2840 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
2843 lease = msg->front.iov_base;
2844 lease->action = action;
2845 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2846 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2847 lease->seq = cpu_to_le32(seq);
2848 put_unaligned_le32(dnamelen, lease + 1);
2849 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2852 * if this is a preemptive lease RELEASE, no need to
2853 * flush request stream, since the actual request will
2856 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2858 ceph_con_send(&session->s_con, msg);
2862 * Preemptively release a lease we expect to invalidate anyway.
2863 * Pass @inode always, @dentry is optional.
2865 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2866 struct dentry *dentry)
2868 struct ceph_dentry_info *di;
2869 struct ceph_mds_session *session;
2872 BUG_ON(inode == NULL);
2873 BUG_ON(dentry == NULL);
2875 /* is dentry lease valid? */
2876 spin_lock(&dentry->d_lock);
2877 di = ceph_dentry(dentry);
2878 if (!di || !di->lease_session ||
2879 di->lease_session->s_mds < 0 ||
2880 di->lease_gen != di->lease_session->s_cap_gen ||
2881 !time_before(jiffies, dentry->d_time)) {
2882 dout("lease_release inode %p dentry %p -- "
2885 spin_unlock(&dentry->d_lock);
2889 /* we do have a lease on this dentry; note mds and seq */
2890 session = ceph_get_mds_session(di->lease_session);
2891 seq = di->lease_seq;
2892 __ceph_mdsc_drop_dentry_lease(dentry);
2893 spin_unlock(&dentry->d_lock);
2895 dout("lease_release inode %p dentry %p to mds%d\n",
2896 inode, dentry, session->s_mds);
2897 ceph_mdsc_lease_send_msg(session, inode, dentry,
2898 CEPH_MDS_LEASE_RELEASE, seq);
2899 ceph_put_mds_session(session);
2903 * drop all leases (and dentry refs) in preparation for umount
2905 static void drop_leases(struct ceph_mds_client *mdsc)
2909 dout("drop_leases\n");
2910 mutex_lock(&mdsc->mutex);
2911 for (i = 0; i < mdsc->max_sessions; i++) {
2912 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2915 mutex_unlock(&mdsc->mutex);
2916 mutex_lock(&s->s_mutex);
2917 mutex_unlock(&s->s_mutex);
2918 ceph_put_mds_session(s);
2919 mutex_lock(&mdsc->mutex);
2921 mutex_unlock(&mdsc->mutex);
2927 * delayed work -- periodically trim expired leases, renew caps with mds
2929 static void schedule_delayed(struct ceph_mds_client *mdsc)
2932 unsigned hz = round_jiffies_relative(HZ * delay);
2933 schedule_delayed_work(&mdsc->delayed_work, hz);
2936 static void delayed_work(struct work_struct *work)
2939 struct ceph_mds_client *mdsc =
2940 container_of(work, struct ceph_mds_client, delayed_work.work);
2944 dout("mdsc delayed_work\n");
2945 ceph_check_delayed_caps(mdsc);
2947 mutex_lock(&mdsc->mutex);
2948 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2949 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2950 mdsc->last_renew_caps);
2952 mdsc->last_renew_caps = jiffies;
2954 for (i = 0; i < mdsc->max_sessions; i++) {
2955 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2958 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2959 dout("resending session close request for mds%d\n",
2961 request_close_session(mdsc, s);
2962 ceph_put_mds_session(s);
2965 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2966 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2967 s->s_state = CEPH_MDS_SESSION_HUNG;
2968 pr_info("mds%d hung\n", s->s_mds);
2971 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2972 /* this mds is failed or recovering, just wait */
2973 ceph_put_mds_session(s);
2976 mutex_unlock(&mdsc->mutex);
2978 mutex_lock(&s->s_mutex);
2980 send_renew_caps(mdsc, s);
2982 ceph_con_keepalive(&s->s_con);
2983 ceph_add_cap_releases(mdsc, s);
2984 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2985 s->s_state == CEPH_MDS_SESSION_HUNG)
2986 ceph_send_cap_releases(mdsc, s);
2987 mutex_unlock(&s->s_mutex);
2988 ceph_put_mds_session(s);
2990 mutex_lock(&mdsc->mutex);
2992 mutex_unlock(&mdsc->mutex);
2994 schedule_delayed(mdsc);
2997 int ceph_mdsc_init(struct ceph_fs_client *fsc)
3000 struct ceph_mds_client *mdsc;
3002 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3007 mutex_init(&mdsc->mutex);
3008 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
3009 if (mdsc->mdsmap == NULL)
3012 init_completion(&mdsc->safe_umount_waiters);
3013 init_waitqueue_head(&mdsc->session_close_wq);
3014 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3015 mdsc->sessions = NULL;
3016 mdsc->max_sessions = 0;
3018 init_rwsem(&mdsc->snap_rwsem);
3019 mdsc->snap_realms = RB_ROOT;
3020 INIT_LIST_HEAD(&mdsc->snap_empty);
3021 spin_lock_init(&mdsc->snap_empty_lock);
3023 mdsc->request_tree = RB_ROOT;
3024 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3025 mdsc->last_renew_caps = jiffies;
3026 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3027 spin_lock_init(&mdsc->cap_delay_lock);
3028 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3029 spin_lock_init(&mdsc->snap_flush_lock);
3030 mdsc->cap_flush_seq = 0;
3031 INIT_LIST_HEAD(&mdsc->cap_dirty);
3032 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
3033 mdsc->num_cap_flushing = 0;
3034 spin_lock_init(&mdsc->cap_dirty_lock);
3035 init_waitqueue_head(&mdsc->cap_flushing_wq);
3036 spin_lock_init(&mdsc->dentry_lru_lock);
3037 INIT_LIST_HEAD(&mdsc->dentry_lru);
3039 ceph_caps_init(mdsc);
3040 ceph_adjust_min_caps(mdsc, fsc->min_caps);
3046 * Wait for safe replies on open mds requests. If we time out, drop
3047 * all requests from the tree to avoid dangling dentry refs.
3049 static void wait_requests(struct ceph_mds_client *mdsc)
3051 struct ceph_mds_request *req;
3052 struct ceph_fs_client *fsc = mdsc->fsc;
3054 mutex_lock(&mdsc->mutex);
3055 if (__get_oldest_req(mdsc)) {
3056 mutex_unlock(&mdsc->mutex);
3058 dout("wait_requests waiting for requests\n");
3059 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
3060 fsc->client->options->mount_timeout * HZ);
3062 /* tear down remaining requests */
3063 mutex_lock(&mdsc->mutex);
3064 while ((req = __get_oldest_req(mdsc))) {
3065 dout("wait_requests timed out on tid %llu\n",
3067 __unregister_request(mdsc, req);
3070 mutex_unlock(&mdsc->mutex);
3071 dout("wait_requests done\n");
3075 * called before mount is ro, and before dentries are torn down.
3076 * (hmm, does this still race with new lookups?)
3078 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3080 dout("pre_umount\n");
3084 ceph_flush_dirty_caps(mdsc);
3085 wait_requests(mdsc);
3088 * wait for reply handlers to drop their request refs and
3089 * their inode/dcache refs
3095 * wait for all write mds requests to flush.
3097 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3099 struct ceph_mds_request *req = NULL, *nextreq;
3102 mutex_lock(&mdsc->mutex);
3103 dout("wait_unsafe_requests want %lld\n", want_tid);
3105 req = __get_oldest_req(mdsc);
3106 while (req && req->r_tid <= want_tid) {
3107 /* find next request */
3108 n = rb_next(&req->r_node);
3110 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3113 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3115 ceph_mdsc_get_request(req);
3117 ceph_mdsc_get_request(nextreq);
3118 mutex_unlock(&mdsc->mutex);
3119 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3120 req->r_tid, want_tid);
3121 wait_for_completion(&req->r_safe_completion);
3122 mutex_lock(&mdsc->mutex);
3123 ceph_mdsc_put_request(req);
3125 break; /* next dne before, so we're done! */
3126 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3127 /* next request was removed from tree */
3128 ceph_mdsc_put_request(nextreq);
3131 ceph_mdsc_put_request(nextreq); /* won't go away */
3135 mutex_unlock(&mdsc->mutex);
3136 dout("wait_unsafe_requests done\n");
3139 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3141 u64 want_tid, want_flush;
3143 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3147 mutex_lock(&mdsc->mutex);
3148 want_tid = mdsc->last_tid;
3149 want_flush = mdsc->cap_flush_seq;
3150 mutex_unlock(&mdsc->mutex);
3151 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3153 ceph_flush_dirty_caps(mdsc);
3155 wait_unsafe_requests(mdsc, want_tid);
3156 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3160 * true if all sessions are closed, or we force unmount
3162 static bool done_closing_sessions(struct ceph_mds_client *mdsc)
3166 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3169 mutex_lock(&mdsc->mutex);
3170 for (i = 0; i < mdsc->max_sessions; i++)
3171 if (mdsc->sessions[i])
3173 mutex_unlock(&mdsc->mutex);
3178 * called after sb is ro.
3180 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3182 struct ceph_mds_session *session;
3184 struct ceph_fs_client *fsc = mdsc->fsc;
3185 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
3187 dout("close_sessions\n");
3189 /* close sessions */
3190 mutex_lock(&mdsc->mutex);
3191 for (i = 0; i < mdsc->max_sessions; i++) {
3192 session = __ceph_lookup_mds_session(mdsc, i);
3195 mutex_unlock(&mdsc->mutex);
3196 mutex_lock(&session->s_mutex);
3197 __close_session(mdsc, session);
3198 mutex_unlock(&session->s_mutex);
3199 ceph_put_mds_session(session);
3200 mutex_lock(&mdsc->mutex);
3202 mutex_unlock(&mdsc->mutex);
3204 dout("waiting for sessions to close\n");
3205 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3208 /* tear down remaining sessions */
3209 mutex_lock(&mdsc->mutex);
3210 for (i = 0; i < mdsc->max_sessions; i++) {
3211 if (mdsc->sessions[i]) {
3212 session = get_session(mdsc->sessions[i]);
3213 __unregister_session(mdsc, session);
3214 mutex_unlock(&mdsc->mutex);
3215 mutex_lock(&session->s_mutex);
3216 remove_session_caps(session);
3217 mutex_unlock(&session->s_mutex);
3218 ceph_put_mds_session(session);
3219 mutex_lock(&mdsc->mutex);
3222 WARN_ON(!list_empty(&mdsc->cap_delay_list));
3223 mutex_unlock(&mdsc->mutex);
3225 ceph_cleanup_empty_realms(mdsc);
3227 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3232 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
3235 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3237 ceph_mdsmap_destroy(mdsc->mdsmap);
3238 kfree(mdsc->sessions);
3239 ceph_caps_finalize(mdsc);
3242 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3244 struct ceph_mds_client *mdsc = fsc->mdsc;
3246 dout("mdsc_destroy %p\n", mdsc);
3247 ceph_mdsc_stop(mdsc);
3249 /* flush out any connection work with references to us */
3254 dout("mdsc_destroy %p done\n", mdsc);
3259 * handle mds map update.
3261 void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3265 void *p = msg->front.iov_base;
3266 void *end = p + msg->front.iov_len;
3267 struct ceph_mdsmap *newmap, *oldmap;
3268 struct ceph_fsid fsid;
3271 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3272 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3273 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
3275 epoch = ceph_decode_32(&p);
3276 maplen = ceph_decode_32(&p);
3277 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3279 /* do we need it? */
3280 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
3281 mutex_lock(&mdsc->mutex);
3282 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3283 dout("handle_map epoch %u <= our %u\n",
3284 epoch, mdsc->mdsmap->m_epoch);
3285 mutex_unlock(&mdsc->mutex);
3289 newmap = ceph_mdsmap_decode(&p, end);
3290 if (IS_ERR(newmap)) {
3291 err = PTR_ERR(newmap);
3295 /* swap into place */
3297 oldmap = mdsc->mdsmap;
3298 mdsc->mdsmap = newmap;
3299 check_new_map(mdsc, newmap, oldmap);
3300 ceph_mdsmap_destroy(oldmap);
3302 mdsc->mdsmap = newmap; /* first mds map */
3304 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
3306 __wake_requests(mdsc, &mdsc->waiting_for_map);
3308 mutex_unlock(&mdsc->mutex);
3309 schedule_delayed(mdsc);
3313 mutex_unlock(&mdsc->mutex);
3315 pr_err("error decoding mdsmap %d\n", err);
3319 static struct ceph_connection *con_get(struct ceph_connection *con)
3321 struct ceph_mds_session *s = con->private;
3323 if (get_session(s)) {
3324 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
3327 dout("mdsc con_get %p FAIL\n", s);
3331 static void con_put(struct ceph_connection *con)
3333 struct ceph_mds_session *s = con->private;
3335 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref) - 1);
3336 ceph_put_mds_session(s);
3340 * if the client is unresponsive for long enough, the mds will kill
3341 * the session entirely.
3343 static void peer_reset(struct ceph_connection *con)
3345 struct ceph_mds_session *s = con->private;
3346 struct ceph_mds_client *mdsc = s->s_mdsc;
3348 pr_warning("mds%d closed our session\n", s->s_mds);
3349 send_mds_reconnect(mdsc, s);
3352 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3354 struct ceph_mds_session *s = con->private;
3355 struct ceph_mds_client *mdsc = s->s_mdsc;
3356 int type = le16_to_cpu(msg->hdr.type);
3358 mutex_lock(&mdsc->mutex);
3359 if (__verify_registered_session(mdsc, s) < 0) {
3360 mutex_unlock(&mdsc->mutex);
3363 mutex_unlock(&mdsc->mutex);
3366 case CEPH_MSG_MDS_MAP:
3367 ceph_mdsc_handle_map(mdsc, msg);
3369 case CEPH_MSG_CLIENT_SESSION:
3370 handle_session(s, msg);
3372 case CEPH_MSG_CLIENT_REPLY:
3373 handle_reply(s, msg);
3375 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
3376 handle_forward(mdsc, s, msg);
3378 case CEPH_MSG_CLIENT_CAPS:
3379 ceph_handle_caps(s, msg);
3381 case CEPH_MSG_CLIENT_SNAP:
3382 ceph_handle_snap(mdsc, s, msg);
3384 case CEPH_MSG_CLIENT_LEASE:
3385 handle_lease(mdsc, s, msg);
3389 pr_err("received unknown message type %d %s\n", type,
3390 ceph_msg_type_name(type));
3399 static int get_authorizer(struct ceph_connection *con,
3400 void **buf, int *len, int *proto,
3401 void **reply_buf, int *reply_len, int force_new)
3403 struct ceph_mds_session *s = con->private;
3404 struct ceph_mds_client *mdsc = s->s_mdsc;
3405 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3408 if (force_new && s->s_authorizer) {
3409 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3410 s->s_authorizer = NULL;
3412 if (s->s_authorizer == NULL) {
3413 if (ac->ops->create_authorizer) {
3414 ret = ac->ops->create_authorizer(
3415 ac, CEPH_ENTITY_TYPE_MDS,
3417 &s->s_authorizer_buf,
3418 &s->s_authorizer_buf_len,
3419 &s->s_authorizer_reply_buf,
3420 &s->s_authorizer_reply_buf_len);
3426 *proto = ac->protocol;
3427 *buf = s->s_authorizer_buf;
3428 *len = s->s_authorizer_buf_len;
3429 *reply_buf = s->s_authorizer_reply_buf;
3430 *reply_len = s->s_authorizer_reply_buf_len;
3435 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3437 struct ceph_mds_session *s = con->private;
3438 struct ceph_mds_client *mdsc = s->s_mdsc;
3439 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3441 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3444 static int invalidate_authorizer(struct ceph_connection *con)
3446 struct ceph_mds_session *s = con->private;
3447 struct ceph_mds_client *mdsc = s->s_mdsc;
3448 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3450 if (ac->ops->invalidate_authorizer)
3451 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3453 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
3456 static const struct ceph_connection_operations mds_con_ops = {
3459 .dispatch = dispatch,
3460 .get_authorizer = get_authorizer,
3461 .verify_authorizer_reply = verify_authorizer_reply,
3462 .invalidate_authorizer = invalidate_authorizer,
3463 .peer_reset = peer_reset,