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)
68 *p += sizeof(struct ceph_mds_reply_inode) +
69 sizeof(*info->in->fragtree.splits) *
70 le32_to_cpu(info->in->fragtree.nsplits);
72 ceph_decode_32_safe(p, end, info->symlink_len, bad);
73 ceph_decode_need(p, end, info->symlink_len, bad);
75 *p += info->symlink_len;
77 ceph_decode_32_safe(p, end, info->xattr_len, bad);
78 ceph_decode_need(p, end, info->xattr_len, bad);
79 info->xattr_data = *p;
80 *p += info->xattr_len;
87 * parse a normal reply, which may contain a (dir+)dentry and/or a
90 static int parse_reply_info_trace(void **p, void *end,
91 struct ceph_mds_reply_info_parsed *info)
95 if (info->head->is_dentry) {
96 err = parse_reply_info_in(p, end, &info->diri);
100 if (unlikely(*p + sizeof(*info->dirfrag) > end))
103 *p += sizeof(*info->dirfrag) +
104 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
105 if (unlikely(*p > end))
108 ceph_decode_32_safe(p, end, info->dname_len, bad);
109 ceph_decode_need(p, end, info->dname_len, bad);
111 *p += info->dname_len;
113 *p += sizeof(*info->dlease);
116 if (info->head->is_target) {
117 err = parse_reply_info_in(p, end, &info->targeti);
122 if (unlikely(*p != end))
129 pr_err("problem parsing mds trace %d\n", err);
134 * parse readdir results
136 static int parse_reply_info_dir(void **p, void *end,
137 struct ceph_mds_reply_info_parsed *info)
143 if (*p + sizeof(*info->dir_dir) > end)
145 *p += sizeof(*info->dir_dir) +
146 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
150 ceph_decode_need(p, end, sizeof(num) + 2, bad);
151 num = ceph_decode_32(p);
152 info->dir_end = ceph_decode_8(p);
153 info->dir_complete = ceph_decode_8(p);
157 /* alloc large array */
159 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
160 sizeof(*info->dir_dname) +
161 sizeof(*info->dir_dname_len) +
162 sizeof(*info->dir_dlease),
164 if (info->dir_in == NULL) {
168 info->dir_dname = (void *)(info->dir_in + num);
169 info->dir_dname_len = (void *)(info->dir_dname + num);
170 info->dir_dlease = (void *)(info->dir_dname_len + num);
174 ceph_decode_need(p, end, sizeof(u32)*2, bad);
175 info->dir_dname_len[i] = ceph_decode_32(p);
176 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
177 info->dir_dname[i] = *p;
178 *p += info->dir_dname_len[i];
179 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
181 info->dir_dlease[i] = *p;
182 *p += sizeof(struct ceph_mds_reply_lease);
185 err = parse_reply_info_in(p, end, &info->dir_in[i]);
200 pr_err("problem parsing dir contents %d\n", err);
205 * parse entire mds reply
207 static int parse_reply_info(struct ceph_msg *msg,
208 struct ceph_mds_reply_info_parsed *info)
214 info->head = msg->front.iov_base;
215 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
216 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
219 ceph_decode_32_safe(&p, end, len, bad);
221 err = parse_reply_info_trace(&p, p+len, info);
227 ceph_decode_32_safe(&p, end, len, bad);
229 err = parse_reply_info_dir(&p, p+len, info);
235 ceph_decode_32_safe(&p, end, len, bad);
236 info->snapblob_len = len;
247 pr_err("mds parse_reply err %d\n", err);
251 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
260 static const char *session_state_name(int s)
263 case CEPH_MDS_SESSION_NEW: return "new";
264 case CEPH_MDS_SESSION_OPENING: return "opening";
265 case CEPH_MDS_SESSION_OPEN: return "open";
266 case CEPH_MDS_SESSION_HUNG: return "hung";
267 case CEPH_MDS_SESSION_CLOSING: return "closing";
268 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
269 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
270 default: return "???";
274 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
276 if (atomic_inc_not_zero(&s->s_ref)) {
277 dout("mdsc get_session %p %d -> %d\n", s,
278 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
281 dout("mdsc get_session %p 0 -- FAIL", s);
286 void ceph_put_mds_session(struct ceph_mds_session *s)
288 dout("mdsc put_session %p %d -> %d\n", s,
289 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
290 if (atomic_dec_and_test(&s->s_ref)) {
292 s->s_mdsc->fsc->client->monc.auth->ops->destroy_authorizer(
293 s->s_mdsc->fsc->client->monc.auth,
300 * called under mdsc->mutex
302 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
305 struct ceph_mds_session *session;
307 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
309 session = mdsc->sessions[mds];
310 dout("lookup_mds_session %p %d\n", session,
311 atomic_read(&session->s_ref));
312 get_session(session);
316 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
318 if (mds >= mdsc->max_sessions)
320 return mdsc->sessions[mds];
323 static int __verify_registered_session(struct ceph_mds_client *mdsc,
324 struct ceph_mds_session *s)
326 if (s->s_mds >= mdsc->max_sessions ||
327 mdsc->sessions[s->s_mds] != s)
333 * create+register a new session for given mds.
334 * called under mdsc->mutex.
336 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
339 struct ceph_mds_session *s;
341 s = kzalloc(sizeof(*s), GFP_NOFS);
343 return ERR_PTR(-ENOMEM);
346 s->s_state = CEPH_MDS_SESSION_NEW;
349 mutex_init(&s->s_mutex);
351 ceph_con_init(mdsc->fsc->client->msgr, &s->s_con);
352 s->s_con.private = s;
353 s->s_con.ops = &mds_con_ops;
354 s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
355 s->s_con.peer_name.num = cpu_to_le64(mds);
357 spin_lock_init(&s->s_cap_lock);
360 s->s_renew_requested = 0;
362 INIT_LIST_HEAD(&s->s_caps);
365 atomic_set(&s->s_ref, 1);
366 INIT_LIST_HEAD(&s->s_waiting);
367 INIT_LIST_HEAD(&s->s_unsafe);
368 s->s_num_cap_releases = 0;
369 s->s_cap_iterator = NULL;
370 INIT_LIST_HEAD(&s->s_cap_releases);
371 INIT_LIST_HEAD(&s->s_cap_releases_done);
372 INIT_LIST_HEAD(&s->s_cap_flushing);
373 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
375 dout("register_session mds%d\n", mds);
376 if (mds >= mdsc->max_sessions) {
377 int newmax = 1 << get_count_order(mds+1);
378 struct ceph_mds_session **sa;
380 dout("register_session realloc to %d\n", newmax);
381 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
384 if (mdsc->sessions) {
385 memcpy(sa, mdsc->sessions,
386 mdsc->max_sessions * sizeof(void *));
387 kfree(mdsc->sessions);
390 mdsc->max_sessions = newmax;
392 mdsc->sessions[mds] = s;
393 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
395 ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
401 return ERR_PTR(-ENOMEM);
405 * called under mdsc->mutex
407 static void __unregister_session(struct ceph_mds_client *mdsc,
408 struct ceph_mds_session *s)
410 dout("__unregister_session mds%d %p\n", s->s_mds, s);
411 BUG_ON(mdsc->sessions[s->s_mds] != s);
412 mdsc->sessions[s->s_mds] = NULL;
413 ceph_con_close(&s->s_con);
414 ceph_put_mds_session(s);
418 * drop session refs in request.
420 * should be last request ref, or hold mdsc->mutex
422 static void put_request_session(struct ceph_mds_request *req)
424 if (req->r_session) {
425 ceph_put_mds_session(req->r_session);
426 req->r_session = NULL;
430 void ceph_mdsc_release_request(struct kref *kref)
432 struct ceph_mds_request *req = container_of(kref,
433 struct ceph_mds_request,
436 ceph_msg_put(req->r_request);
438 ceph_msg_put(req->r_reply);
439 destroy_reply_info(&req->r_reply_info);
442 ceph_put_cap_refs(ceph_inode(req->r_inode),
446 if (req->r_locked_dir)
447 ceph_put_cap_refs(ceph_inode(req->r_locked_dir),
449 if (req->r_target_inode)
450 iput(req->r_target_inode);
453 if (req->r_old_dentry) {
455 ceph_inode(req->r_old_dentry->d_parent->d_inode),
457 dput(req->r_old_dentry);
461 put_request_session(req);
462 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
467 * lookup session, bump ref if found.
469 * called under mdsc->mutex.
471 static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
474 struct ceph_mds_request *req;
475 struct rb_node *n = mdsc->request_tree.rb_node;
478 req = rb_entry(n, struct ceph_mds_request, r_node);
479 if (tid < req->r_tid)
481 else if (tid > req->r_tid)
484 ceph_mdsc_get_request(req);
491 static void __insert_request(struct ceph_mds_client *mdsc,
492 struct ceph_mds_request *new)
494 struct rb_node **p = &mdsc->request_tree.rb_node;
495 struct rb_node *parent = NULL;
496 struct ceph_mds_request *req = NULL;
500 req = rb_entry(parent, struct ceph_mds_request, r_node);
501 if (new->r_tid < req->r_tid)
503 else if (new->r_tid > req->r_tid)
509 rb_link_node(&new->r_node, parent, p);
510 rb_insert_color(&new->r_node, &mdsc->request_tree);
514 * Register an in-flight request, and assign a tid. Link to directory
515 * are modifying (if any).
517 * Called under mdsc->mutex.
519 static void __register_request(struct ceph_mds_client *mdsc,
520 struct ceph_mds_request *req,
523 req->r_tid = ++mdsc->last_tid;
525 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
527 dout("__register_request %p tid %lld\n", req, req->r_tid);
528 ceph_mdsc_get_request(req);
529 __insert_request(mdsc, req);
531 req->r_uid = current_fsuid();
532 req->r_gid = current_fsgid();
535 struct ceph_inode_info *ci = ceph_inode(dir);
537 spin_lock(&ci->i_unsafe_lock);
538 req->r_unsafe_dir = dir;
539 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
540 spin_unlock(&ci->i_unsafe_lock);
544 static void __unregister_request(struct ceph_mds_client *mdsc,
545 struct ceph_mds_request *req)
547 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
548 rb_erase(&req->r_node, &mdsc->request_tree);
549 RB_CLEAR_NODE(&req->r_node);
551 if (req->r_unsafe_dir) {
552 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
554 spin_lock(&ci->i_unsafe_lock);
555 list_del_init(&req->r_unsafe_dir_item);
556 spin_unlock(&ci->i_unsafe_lock);
559 ceph_mdsc_put_request(req);
563 * Choose mds to send request to next. If there is a hint set in the
564 * request (e.g., due to a prior forward hint from the mds), use that.
565 * Otherwise, consult frag tree and/or caps to identify the
566 * appropriate mds. If all else fails, choose randomly.
568 * Called under mdsc->mutex.
570 struct dentry *get_nonsnap_parent(struct dentry *dentry)
572 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
573 dentry = dentry->d_parent;
577 static int __choose_mds(struct ceph_mds_client *mdsc,
578 struct ceph_mds_request *req)
581 struct ceph_inode_info *ci;
582 struct ceph_cap *cap;
583 int mode = req->r_direct_mode;
585 u32 hash = req->r_direct_hash;
586 bool is_hash = req->r_direct_is_hash;
589 * is there a specific mds we should try? ignore hint if we have
590 * no session and the mds is not up (active or recovering).
592 if (req->r_resend_mds >= 0 &&
593 (__have_session(mdsc, req->r_resend_mds) ||
594 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
595 dout("choose_mds using resend_mds mds%d\n",
597 return req->r_resend_mds;
600 if (mode == USE_RANDOM_MDS)
605 inode = req->r_inode;
606 } else if (req->r_dentry) {
607 struct inode *dir = req->r_dentry->d_parent->d_inode;
609 if (dir->i_sb != mdsc->fsc->sb) {
611 inode = req->r_dentry->d_inode;
612 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
613 /* direct snapped/virtual snapdir requests
614 * based on parent dir inode */
616 get_nonsnap_parent(req->r_dentry->d_parent);
618 dout("__choose_mds using nonsnap parent %p\n", inode);
619 } else if (req->r_dentry->d_inode) {
621 inode = req->r_dentry->d_inode;
625 hash = req->r_dentry->d_name.hash;
630 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
634 ci = ceph_inode(inode);
636 if (is_hash && S_ISDIR(inode->i_mode)) {
637 struct ceph_inode_frag frag;
640 ceph_choose_frag(ci, hash, &frag, &found);
642 if (mode == USE_ANY_MDS && frag.ndist > 0) {
645 /* choose a random replica */
646 get_random_bytes(&r, 1);
649 dout("choose_mds %p %llx.%llx "
650 "frag %u mds%d (%d/%d)\n",
651 inode, ceph_vinop(inode),
657 /* since this file/dir wasn't known to be
658 * replicated, then we want to look for the
659 * authoritative mds. */
662 /* choose auth mds */
664 dout("choose_mds %p %llx.%llx "
665 "frag %u mds%d (auth)\n",
666 inode, ceph_vinop(inode), frag.frag, mds);
672 spin_lock(&inode->i_lock);
674 if (mode == USE_AUTH_MDS)
675 cap = ci->i_auth_cap;
676 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
677 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
679 spin_unlock(&inode->i_lock);
682 mds = cap->session->s_mds;
683 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
684 inode, ceph_vinop(inode), mds,
685 cap == ci->i_auth_cap ? "auth " : "", cap);
686 spin_unlock(&inode->i_lock);
690 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
691 dout("choose_mds chose random mds%d\n", mds);
699 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
701 struct ceph_msg *msg;
702 struct ceph_mds_session_head *h;
704 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS);
706 pr_err("create_session_msg ENOMEM creating msg\n");
709 h = msg->front.iov_base;
710 h->op = cpu_to_le32(op);
711 h->seq = cpu_to_le64(seq);
716 * send session open request.
718 * called under mdsc->mutex
720 static int __open_session(struct ceph_mds_client *mdsc,
721 struct ceph_mds_session *session)
723 struct ceph_msg *msg;
725 int mds = session->s_mds;
727 /* wait for mds to go active? */
728 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
729 dout("open_session to mds%d (%s)\n", mds,
730 ceph_mds_state_name(mstate));
731 session->s_state = CEPH_MDS_SESSION_OPENING;
732 session->s_renew_requested = jiffies;
734 /* send connect message */
735 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
738 ceph_con_send(&session->s_con, msg);
743 * open sessions for any export targets for the given mds
745 * called under mdsc->mutex
747 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
748 struct ceph_mds_session *session)
750 struct ceph_mds_info *mi;
751 struct ceph_mds_session *ts;
752 int i, mds = session->s_mds;
755 if (mds >= mdsc->mdsmap->m_max_mds)
757 mi = &mdsc->mdsmap->m_info[mds];
758 dout("open_export_target_sessions for mds%d (%d targets)\n",
759 session->s_mds, mi->num_export_targets);
761 for (i = 0; i < mi->num_export_targets; i++) {
762 target = mi->export_targets[i];
763 ts = __ceph_lookup_mds_session(mdsc, target);
765 ts = register_session(mdsc, target);
769 if (session->s_state == CEPH_MDS_SESSION_NEW ||
770 session->s_state == CEPH_MDS_SESSION_CLOSING)
771 __open_session(mdsc, session);
773 dout(" mds%d target mds%d %p is %s\n", session->s_mds,
774 i, ts, session_state_name(ts->s_state));
775 ceph_put_mds_session(ts);
779 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
780 struct ceph_mds_session *session)
782 mutex_lock(&mdsc->mutex);
783 __open_export_target_sessions(mdsc, session);
784 mutex_unlock(&mdsc->mutex);
792 * Free preallocated cap messages assigned to this session
794 static void cleanup_cap_releases(struct ceph_mds_session *session)
796 struct ceph_msg *msg;
798 spin_lock(&session->s_cap_lock);
799 while (!list_empty(&session->s_cap_releases)) {
800 msg = list_first_entry(&session->s_cap_releases,
801 struct ceph_msg, list_head);
802 list_del_init(&msg->list_head);
805 while (!list_empty(&session->s_cap_releases_done)) {
806 msg = list_first_entry(&session->s_cap_releases_done,
807 struct ceph_msg, list_head);
808 list_del_init(&msg->list_head);
811 spin_unlock(&session->s_cap_lock);
815 * Helper to safely iterate over all caps associated with a session, with
816 * special care taken to handle a racing __ceph_remove_cap().
818 * Caller must hold session s_mutex.
820 static int iterate_session_caps(struct ceph_mds_session *session,
821 int (*cb)(struct inode *, struct ceph_cap *,
825 struct ceph_cap *cap;
826 struct inode *inode, *last_inode = NULL;
827 struct ceph_cap *old_cap = NULL;
830 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
831 spin_lock(&session->s_cap_lock);
832 p = session->s_caps.next;
833 while (p != &session->s_caps) {
834 cap = list_entry(p, struct ceph_cap, session_caps);
835 inode = igrab(&cap->ci->vfs_inode);
840 session->s_cap_iterator = cap;
841 spin_unlock(&session->s_cap_lock);
848 ceph_put_cap(session->s_mdsc, old_cap);
852 ret = cb(inode, cap, arg);
855 spin_lock(&session->s_cap_lock);
857 if (cap->ci == NULL) {
858 dout("iterate_session_caps finishing cap %p removal\n",
860 BUG_ON(cap->session != session);
861 list_del_init(&cap->session_caps);
862 session->s_nr_caps--;
864 old_cap = cap; /* put_cap it w/o locks held */
871 session->s_cap_iterator = NULL;
872 spin_unlock(&session->s_cap_lock);
877 ceph_put_cap(session->s_mdsc, old_cap);
882 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
885 struct ceph_inode_info *ci = ceph_inode(inode);
888 dout("removing cap %p, ci is %p, inode is %p\n",
889 cap, ci, &ci->vfs_inode);
890 spin_lock(&inode->i_lock);
891 __ceph_remove_cap(cap);
892 if (!__ceph_is_any_real_caps(ci)) {
893 struct ceph_mds_client *mdsc =
894 ceph_sb_to_client(inode->i_sb)->mdsc;
896 spin_lock(&mdsc->cap_dirty_lock);
897 if (!list_empty(&ci->i_dirty_item)) {
898 pr_info(" dropping dirty %s state for %p %lld\n",
899 ceph_cap_string(ci->i_dirty_caps),
900 inode, ceph_ino(inode));
901 ci->i_dirty_caps = 0;
902 list_del_init(&ci->i_dirty_item);
905 if (!list_empty(&ci->i_flushing_item)) {
906 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
907 ceph_cap_string(ci->i_flushing_caps),
908 inode, ceph_ino(inode));
909 ci->i_flushing_caps = 0;
910 list_del_init(&ci->i_flushing_item);
911 mdsc->num_cap_flushing--;
914 if (drop && ci->i_wrbuffer_ref) {
915 pr_info(" dropping dirty data for %p %lld\n",
916 inode, ceph_ino(inode));
917 ci->i_wrbuffer_ref = 0;
918 ci->i_wrbuffer_ref_head = 0;
921 spin_unlock(&mdsc->cap_dirty_lock);
923 spin_unlock(&inode->i_lock);
930 * caller must hold session s_mutex
932 static void remove_session_caps(struct ceph_mds_session *session)
934 dout("remove_session_caps on %p\n", session);
935 iterate_session_caps(session, remove_session_caps_cb, NULL);
936 BUG_ON(session->s_nr_caps > 0);
937 BUG_ON(!list_empty(&session->s_cap_flushing));
938 cleanup_cap_releases(session);
942 * wake up any threads waiting on this session's caps. if the cap is
943 * old (didn't get renewed on the client reconnect), remove it now.
945 * caller must hold s_mutex.
947 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
950 struct ceph_inode_info *ci = ceph_inode(inode);
952 wake_up_all(&ci->i_cap_wq);
954 spin_lock(&inode->i_lock);
955 ci->i_wanted_max_size = 0;
956 ci->i_requested_max_size = 0;
957 spin_unlock(&inode->i_lock);
962 static void wake_up_session_caps(struct ceph_mds_session *session,
965 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
966 iterate_session_caps(session, wake_up_session_cb,
967 (void *)(unsigned long)reconnect);
971 * Send periodic message to MDS renewing all currently held caps. The
972 * ack will reset the expiration for all caps from this session.
974 * caller holds s_mutex
976 static int send_renew_caps(struct ceph_mds_client *mdsc,
977 struct ceph_mds_session *session)
979 struct ceph_msg *msg;
982 if (time_after_eq(jiffies, session->s_cap_ttl) &&
983 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
984 pr_info("mds%d caps stale\n", session->s_mds);
985 session->s_renew_requested = jiffies;
987 /* do not try to renew caps until a recovering mds has reconnected
988 * with its clients. */
989 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
990 if (state < CEPH_MDS_STATE_RECONNECT) {
991 dout("send_renew_caps ignoring mds%d (%s)\n",
992 session->s_mds, ceph_mds_state_name(state));
996 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
997 ceph_mds_state_name(state));
998 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
999 ++session->s_renew_seq);
1002 ceph_con_send(&session->s_con, msg);
1007 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1009 * Called under session->s_mutex
1011 static void renewed_caps(struct ceph_mds_client *mdsc,
1012 struct ceph_mds_session *session, int is_renew)
1017 spin_lock(&session->s_cap_lock);
1018 was_stale = is_renew && (session->s_cap_ttl == 0 ||
1019 time_after_eq(jiffies, session->s_cap_ttl));
1021 session->s_cap_ttl = session->s_renew_requested +
1022 mdsc->mdsmap->m_session_timeout*HZ;
1025 if (time_before(jiffies, session->s_cap_ttl)) {
1026 pr_info("mds%d caps renewed\n", session->s_mds);
1029 pr_info("mds%d caps still stale\n", session->s_mds);
1032 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1033 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1034 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1035 spin_unlock(&session->s_cap_lock);
1038 wake_up_session_caps(session, 0);
1042 * send a session close request
1044 static int request_close_session(struct ceph_mds_client *mdsc,
1045 struct ceph_mds_session *session)
1047 struct ceph_msg *msg;
1049 dout("request_close_session mds%d state %s seq %lld\n",
1050 session->s_mds, session_state_name(session->s_state),
1052 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1055 ceph_con_send(&session->s_con, msg);
1060 * Called with s_mutex held.
1062 static int __close_session(struct ceph_mds_client *mdsc,
1063 struct ceph_mds_session *session)
1065 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1067 session->s_state = CEPH_MDS_SESSION_CLOSING;
1068 return request_close_session(mdsc, session);
1072 * Trim old(er) caps.
1074 * Because we can't cache an inode without one or more caps, we do
1075 * this indirectly: if a cap is unused, we prune its aliases, at which
1076 * point the inode will hopefully get dropped to.
1078 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1079 * memory pressure from the MDS, though, so it needn't be perfect.
1081 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1083 struct ceph_mds_session *session = arg;
1084 struct ceph_inode_info *ci = ceph_inode(inode);
1085 int used, oissued, mine;
1087 if (session->s_trim_caps <= 0)
1090 spin_lock(&inode->i_lock);
1091 mine = cap->issued | cap->implemented;
1092 used = __ceph_caps_used(ci);
1093 oissued = __ceph_caps_issued_other(ci, cap);
1095 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1096 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1097 ceph_cap_string(used));
1098 if (ci->i_dirty_caps)
1099 goto out; /* dirty caps */
1100 if ((used & ~oissued) & mine)
1101 goto out; /* we need these caps */
1103 session->s_trim_caps--;
1105 /* we aren't the only cap.. just remove us */
1106 __ceph_remove_cap(cap);
1108 /* try to drop referring dentries */
1109 spin_unlock(&inode->i_lock);
1110 d_prune_aliases(inode);
1111 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1112 inode, cap, atomic_read(&inode->i_count));
1117 spin_unlock(&inode->i_lock);
1122 * Trim session cap count down to some max number.
1124 static int trim_caps(struct ceph_mds_client *mdsc,
1125 struct ceph_mds_session *session,
1128 int trim_caps = session->s_nr_caps - max_caps;
1130 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1131 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1132 if (trim_caps > 0) {
1133 session->s_trim_caps = trim_caps;
1134 iterate_session_caps(session, trim_caps_cb, session);
1135 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1136 session->s_mds, session->s_nr_caps, max_caps,
1137 trim_caps - session->s_trim_caps);
1138 session->s_trim_caps = 0;
1144 * Allocate cap_release messages. If there is a partially full message
1145 * in the queue, try to allocate enough to cover it's remainder, so that
1146 * we can send it immediately.
1148 * Called under s_mutex.
1150 int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
1151 struct ceph_mds_session *session)
1153 struct ceph_msg *msg, *partial = NULL;
1154 struct ceph_mds_cap_release *head;
1156 int extra = mdsc->fsc->mount_options->cap_release_safety;
1159 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1162 spin_lock(&session->s_cap_lock);
1164 if (!list_empty(&session->s_cap_releases)) {
1165 msg = list_first_entry(&session->s_cap_releases,
1168 head = msg->front.iov_base;
1169 num = le32_to_cpu(head->num);
1171 dout(" partial %p with (%d/%d)\n", msg, num,
1172 (int)CEPH_CAPS_PER_RELEASE);
1173 extra += CEPH_CAPS_PER_RELEASE - num;
1177 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1178 spin_unlock(&session->s_cap_lock);
1179 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1183 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1184 (int)msg->front.iov_len);
1185 head = msg->front.iov_base;
1186 head->num = cpu_to_le32(0);
1187 msg->front.iov_len = sizeof(*head);
1188 spin_lock(&session->s_cap_lock);
1189 list_add(&msg->list_head, &session->s_cap_releases);
1190 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1194 head = partial->front.iov_base;
1195 num = le32_to_cpu(head->num);
1196 dout(" queueing partial %p with %d/%d\n", partial, num,
1197 (int)CEPH_CAPS_PER_RELEASE);
1198 list_move_tail(&partial->list_head,
1199 &session->s_cap_releases_done);
1200 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
1203 spin_unlock(&session->s_cap_lock);
1209 * flush all dirty inode data to disk.
1211 * returns true if we've flushed through want_flush_seq
1213 static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1217 dout("check_cap_flush want %lld\n", want_flush_seq);
1218 mutex_lock(&mdsc->mutex);
1219 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1220 struct ceph_mds_session *session = mdsc->sessions[mds];
1224 get_session(session);
1225 mutex_unlock(&mdsc->mutex);
1227 mutex_lock(&session->s_mutex);
1228 if (!list_empty(&session->s_cap_flushing)) {
1229 struct ceph_inode_info *ci =
1230 list_entry(session->s_cap_flushing.next,
1231 struct ceph_inode_info,
1233 struct inode *inode = &ci->vfs_inode;
1235 spin_lock(&inode->i_lock);
1236 if (ci->i_cap_flush_seq <= want_flush_seq) {
1237 dout("check_cap_flush still flushing %p "
1238 "seq %lld <= %lld to mds%d\n", inode,
1239 ci->i_cap_flush_seq, want_flush_seq,
1243 spin_unlock(&inode->i_lock);
1245 mutex_unlock(&session->s_mutex);
1246 ceph_put_mds_session(session);
1250 mutex_lock(&mdsc->mutex);
1253 mutex_unlock(&mdsc->mutex);
1254 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1259 * called under s_mutex
1261 void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1262 struct ceph_mds_session *session)
1264 struct ceph_msg *msg;
1266 dout("send_cap_releases mds%d\n", session->s_mds);
1267 spin_lock(&session->s_cap_lock);
1268 while (!list_empty(&session->s_cap_releases_done)) {
1269 msg = list_first_entry(&session->s_cap_releases_done,
1270 struct ceph_msg, list_head);
1271 list_del_init(&msg->list_head);
1272 spin_unlock(&session->s_cap_lock);
1273 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1274 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1275 ceph_con_send(&session->s_con, msg);
1276 spin_lock(&session->s_cap_lock);
1278 spin_unlock(&session->s_cap_lock);
1281 static void discard_cap_releases(struct ceph_mds_client *mdsc,
1282 struct ceph_mds_session *session)
1284 struct ceph_msg *msg;
1285 struct ceph_mds_cap_release *head;
1288 dout("discard_cap_releases mds%d\n", session->s_mds);
1289 spin_lock(&session->s_cap_lock);
1291 /* zero out the in-progress message */
1292 msg = list_first_entry(&session->s_cap_releases,
1293 struct ceph_msg, list_head);
1294 head = msg->front.iov_base;
1295 num = le32_to_cpu(head->num);
1296 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1297 head->num = cpu_to_le32(0);
1298 session->s_num_cap_releases += num;
1300 /* requeue completed messages */
1301 while (!list_empty(&session->s_cap_releases_done)) {
1302 msg = list_first_entry(&session->s_cap_releases_done,
1303 struct ceph_msg, list_head);
1304 list_del_init(&msg->list_head);
1306 head = msg->front.iov_base;
1307 num = le32_to_cpu(head->num);
1308 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1310 session->s_num_cap_releases += num;
1311 head->num = cpu_to_le32(0);
1312 msg->front.iov_len = sizeof(*head);
1313 list_add(&msg->list_head, &session->s_cap_releases);
1316 spin_unlock(&session->s_cap_lock);
1324 * Create an mds request.
1326 struct ceph_mds_request *
1327 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1329 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1332 return ERR_PTR(-ENOMEM);
1334 mutex_init(&req->r_fill_mutex);
1336 req->r_started = jiffies;
1337 req->r_resend_mds = -1;
1338 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1340 kref_init(&req->r_kref);
1341 INIT_LIST_HEAD(&req->r_wait);
1342 init_completion(&req->r_completion);
1343 init_completion(&req->r_safe_completion);
1344 INIT_LIST_HEAD(&req->r_unsafe_item);
1347 req->r_direct_mode = mode;
1352 * return oldest (lowest) request, tid in request tree, 0 if none.
1354 * called under mdsc->mutex.
1356 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1358 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1360 return rb_entry(rb_first(&mdsc->request_tree),
1361 struct ceph_mds_request, r_node);
1364 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1366 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1374 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1375 * on build_path_from_dentry in fs/cifs/dir.c.
1377 * If @stop_on_nosnap, generate path relative to the first non-snapped
1380 * Encode hidden .snap dirs as a double /, i.e.
1381 * foo/.snap/bar -> foo//bar
1383 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1386 struct dentry *temp;
1391 return ERR_PTR(-EINVAL);
1395 for (temp = dentry; !IS_ROOT(temp);) {
1396 struct inode *inode = temp->d_inode;
1397 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1398 len++; /* slash only */
1399 else if (stop_on_nosnap && inode &&
1400 ceph_snap(inode) == CEPH_NOSNAP)
1403 len += 1 + temp->d_name.len;
1404 temp = temp->d_parent;
1406 pr_err("build_path corrupt dentry %p\n", dentry);
1407 return ERR_PTR(-EINVAL);
1411 len--; /* no leading '/' */
1413 path = kmalloc(len+1, GFP_NOFS);
1415 return ERR_PTR(-ENOMEM);
1417 path[pos] = 0; /* trailing null */
1418 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1419 struct inode *inode = temp->d_inode;
1421 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1422 dout("build_path path+%d: %p SNAPDIR\n",
1424 } else if (stop_on_nosnap && inode &&
1425 ceph_snap(inode) == CEPH_NOSNAP) {
1428 pos -= temp->d_name.len;
1431 strncpy(path + pos, temp->d_name.name,
1436 temp = temp->d_parent;
1438 pr_err("build_path corrupt dentry\n");
1440 return ERR_PTR(-EINVAL);
1444 pr_err("build_path did not end path lookup where "
1445 "expected, namelen is %d, pos is %d\n", len, pos);
1446 /* presumably this is only possible if racing with a
1447 rename of one of the parent directories (we can not
1448 lock the dentries above us to prevent this, but
1449 retrying should be harmless) */
1454 *base = ceph_ino(temp->d_inode);
1456 dout("build_path on %p %d built %llx '%.*s'\n",
1457 dentry, atomic_read(&dentry->d_count), *base, len, path);
1461 static int build_dentry_path(struct dentry *dentry,
1462 const char **ppath, int *ppathlen, u64 *pino,
1467 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1468 *pino = ceph_ino(dentry->d_parent->d_inode);
1469 *ppath = dentry->d_name.name;
1470 *ppathlen = dentry->d_name.len;
1473 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1475 return PTR_ERR(path);
1481 static int build_inode_path(struct inode *inode,
1482 const char **ppath, int *ppathlen, u64 *pino,
1485 struct dentry *dentry;
1488 if (ceph_snap(inode) == CEPH_NOSNAP) {
1489 *pino = ceph_ino(inode);
1493 dentry = d_find_alias(inode);
1494 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1497 return PTR_ERR(path);
1504 * request arguments may be specified via an inode *, a dentry *, or
1505 * an explicit ino+path.
1507 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1508 const char *rpath, u64 rino,
1509 const char **ppath, int *pathlen,
1510 u64 *ino, int *freepath)
1515 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1516 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1518 } else if (rdentry) {
1519 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1520 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1525 *pathlen = strlen(rpath);
1526 dout(" path %.*s\n", *pathlen, rpath);
1533 * called under mdsc->mutex
1535 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1536 struct ceph_mds_request *req,
1539 struct ceph_msg *msg;
1540 struct ceph_mds_request_head *head;
1541 const char *path1 = NULL;
1542 const char *path2 = NULL;
1543 u64 ino1 = 0, ino2 = 0;
1544 int pathlen1 = 0, pathlen2 = 0;
1545 int freepath1 = 0, freepath2 = 0;
1551 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1552 req->r_path1, req->r_ino1.ino,
1553 &path1, &pathlen1, &ino1, &freepath1);
1559 ret = set_request_path_attr(NULL, req->r_old_dentry,
1560 req->r_path2, req->r_ino2.ino,
1561 &path2, &pathlen2, &ino2, &freepath2);
1567 len = sizeof(*head) +
1568 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
1570 /* calculate (max) length for cap releases */
1571 len += sizeof(struct ceph_mds_request_release) *
1572 (!!req->r_inode_drop + !!req->r_dentry_drop +
1573 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1574 if (req->r_dentry_drop)
1575 len += req->r_dentry->d_name.len;
1576 if (req->r_old_dentry_drop)
1577 len += req->r_old_dentry->d_name.len;
1579 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS);
1581 msg = ERR_PTR(-ENOMEM);
1585 msg->hdr.tid = cpu_to_le64(req->r_tid);
1587 head = msg->front.iov_base;
1588 p = msg->front.iov_base + sizeof(*head);
1589 end = msg->front.iov_base + msg->front.iov_len;
1591 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1592 head->op = cpu_to_le32(req->r_op);
1593 head->caller_uid = cpu_to_le32(req->r_uid);
1594 head->caller_gid = cpu_to_le32(req->r_gid);
1595 head->args = req->r_args;
1597 ceph_encode_filepath(&p, end, ino1, path1);
1598 ceph_encode_filepath(&p, end, ino2, path2);
1600 /* make note of release offset, in case we need to replay */
1601 req->r_request_release_offset = p - msg->front.iov_base;
1605 if (req->r_inode_drop)
1606 releases += ceph_encode_inode_release(&p,
1607 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1608 mds, req->r_inode_drop, req->r_inode_unless, 0);
1609 if (req->r_dentry_drop)
1610 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1611 mds, req->r_dentry_drop, req->r_dentry_unless);
1612 if (req->r_old_dentry_drop)
1613 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1614 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1615 if (req->r_old_inode_drop)
1616 releases += ceph_encode_inode_release(&p,
1617 req->r_old_dentry->d_inode,
1618 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1619 head->num_releases = cpu_to_le16(releases);
1622 msg->front.iov_len = p - msg->front.iov_base;
1623 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1625 msg->pages = req->r_pages;
1626 msg->nr_pages = req->r_num_pages;
1627 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1628 msg->hdr.data_off = cpu_to_le16(0);
1632 kfree((char *)path2);
1635 kfree((char *)path1);
1641 * called under mdsc->mutex if error, under no mutex if
1644 static void complete_request(struct ceph_mds_client *mdsc,
1645 struct ceph_mds_request *req)
1647 if (req->r_callback)
1648 req->r_callback(mdsc, req);
1650 complete_all(&req->r_completion);
1654 * called under mdsc->mutex
1656 static int __prepare_send_request(struct ceph_mds_client *mdsc,
1657 struct ceph_mds_request *req,
1660 struct ceph_mds_request_head *rhead;
1661 struct ceph_msg *msg;
1667 struct ceph_cap *cap =
1668 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1671 req->r_sent_on_mseq = cap->mseq;
1673 req->r_sent_on_mseq = -1;
1675 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1676 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1678 if (req->r_got_unsafe) {
1680 * Replay. Do not regenerate message (and rebuild
1681 * paths, etc.); just use the original message.
1682 * Rebuilding paths will break for renames because
1683 * d_move mangles the src name.
1685 msg = req->r_request;
1686 rhead = msg->front.iov_base;
1688 flags = le32_to_cpu(rhead->flags);
1689 flags |= CEPH_MDS_FLAG_REPLAY;
1690 rhead->flags = cpu_to_le32(flags);
1692 if (req->r_target_inode)
1693 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1695 rhead->num_retry = req->r_attempts - 1;
1697 /* remove cap/dentry releases from message */
1698 rhead->num_releases = 0;
1699 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1700 msg->front.iov_len = req->r_request_release_offset;
1704 if (req->r_request) {
1705 ceph_msg_put(req->r_request);
1706 req->r_request = NULL;
1708 msg = create_request_message(mdsc, req, mds);
1710 req->r_err = PTR_ERR(msg);
1711 complete_request(mdsc, req);
1712 return PTR_ERR(msg);
1714 req->r_request = msg;
1716 rhead = msg->front.iov_base;
1717 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1718 if (req->r_got_unsafe)
1719 flags |= CEPH_MDS_FLAG_REPLAY;
1720 if (req->r_locked_dir)
1721 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1722 rhead->flags = cpu_to_le32(flags);
1723 rhead->num_fwd = req->r_num_fwd;
1724 rhead->num_retry = req->r_attempts - 1;
1727 dout(" r_locked_dir = %p\n", req->r_locked_dir);
1732 * send request, or put it on the appropriate wait list.
1734 static int __do_request(struct ceph_mds_client *mdsc,
1735 struct ceph_mds_request *req)
1737 struct ceph_mds_session *session = NULL;
1741 if (req->r_err || req->r_got_result)
1744 if (req->r_timeout &&
1745 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1746 dout("do_request timed out\n");
1751 mds = __choose_mds(mdsc, req);
1753 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1754 dout("do_request no mds or not active, waiting for map\n");
1755 list_add(&req->r_wait, &mdsc->waiting_for_map);
1759 /* get, open session */
1760 session = __ceph_lookup_mds_session(mdsc, mds);
1762 session = register_session(mdsc, mds);
1763 if (IS_ERR(session)) {
1764 err = PTR_ERR(session);
1768 dout("do_request mds%d session %p state %s\n", mds, session,
1769 session_state_name(session->s_state));
1770 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1771 session->s_state != CEPH_MDS_SESSION_HUNG) {
1772 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1773 session->s_state == CEPH_MDS_SESSION_CLOSING)
1774 __open_session(mdsc, session);
1775 list_add(&req->r_wait, &session->s_waiting);
1780 req->r_session = get_session(session);
1781 req->r_resend_mds = -1; /* forget any previous mds hint */
1783 if (req->r_request_started == 0) /* note request start time */
1784 req->r_request_started = jiffies;
1786 err = __prepare_send_request(mdsc, req, mds);
1788 ceph_msg_get(req->r_request);
1789 ceph_con_send(&session->s_con, req->r_request);
1793 ceph_put_mds_session(session);
1799 complete_request(mdsc, req);
1804 * called under mdsc->mutex
1806 static void __wake_requests(struct ceph_mds_client *mdsc,
1807 struct list_head *head)
1809 struct ceph_mds_request *req, *nreq;
1811 list_for_each_entry_safe(req, nreq, head, r_wait) {
1812 list_del_init(&req->r_wait);
1813 __do_request(mdsc, req);
1818 * Wake up threads with requests pending for @mds, so that they can
1819 * resubmit their requests to a possibly different mds.
1821 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
1823 struct ceph_mds_request *req;
1826 dout("kick_requests mds%d\n", mds);
1827 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1828 req = rb_entry(p, struct ceph_mds_request, r_node);
1829 if (req->r_got_unsafe)
1831 if (req->r_session &&
1832 req->r_session->s_mds == mds) {
1833 dout(" kicking tid %llu\n", req->r_tid);
1834 put_request_session(req);
1835 __do_request(mdsc, req);
1840 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1841 struct ceph_mds_request *req)
1843 dout("submit_request on %p\n", req);
1844 mutex_lock(&mdsc->mutex);
1845 __register_request(mdsc, req, NULL);
1846 __do_request(mdsc, req);
1847 mutex_unlock(&mdsc->mutex);
1851 * Synchrously perform an mds request. Take care of all of the
1852 * session setup, forwarding, retry details.
1854 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1856 struct ceph_mds_request *req)
1860 dout("do_request on %p\n", req);
1862 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1864 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1865 if (req->r_locked_dir)
1866 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1867 if (req->r_old_dentry)
1869 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1873 mutex_lock(&mdsc->mutex);
1874 __register_request(mdsc, req, dir);
1875 __do_request(mdsc, req);
1879 __unregister_request(mdsc, req);
1880 dout("do_request early error %d\n", err);
1885 mutex_unlock(&mdsc->mutex);
1886 dout("do_request waiting\n");
1887 if (req->r_timeout) {
1888 err = (long)wait_for_completion_killable_timeout(
1889 &req->r_completion, req->r_timeout);
1893 err = wait_for_completion_killable(&req->r_completion);
1895 dout("do_request waited, got %d\n", err);
1896 mutex_lock(&mdsc->mutex);
1898 /* only abort if we didn't race with a real reply */
1899 if (req->r_got_result) {
1900 err = le32_to_cpu(req->r_reply_info.head->result);
1901 } else if (err < 0) {
1902 dout("aborted request %lld with %d\n", req->r_tid, err);
1905 * ensure we aren't running concurrently with
1906 * ceph_fill_trace or ceph_readdir_prepopulate, which
1907 * rely on locks (dir mutex) held by our caller.
1909 mutex_lock(&req->r_fill_mutex);
1911 req->r_aborted = true;
1912 mutex_unlock(&req->r_fill_mutex);
1914 if (req->r_locked_dir &&
1915 (req->r_op & CEPH_MDS_OP_WRITE))
1916 ceph_invalidate_dir_request(req);
1922 mutex_unlock(&mdsc->mutex);
1923 dout("do_request %p done, result %d\n", req, err);
1928 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1929 * namespace request.
1931 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
1933 struct inode *inode = req->r_locked_dir;
1934 struct ceph_inode_info *ci = ceph_inode(inode);
1936 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode);
1937 spin_lock(&inode->i_lock);
1938 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1939 ci->i_release_count++;
1940 spin_unlock(&inode->i_lock);
1943 ceph_invalidate_dentry_lease(req->r_dentry);
1944 if (req->r_old_dentry)
1945 ceph_invalidate_dentry_lease(req->r_old_dentry);
1951 * We take the session mutex and parse and process the reply immediately.
1952 * This preserves the logical ordering of replies, capabilities, etc., sent
1953 * by the MDS as they are applied to our local cache.
1955 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
1957 struct ceph_mds_client *mdsc = session->s_mdsc;
1958 struct ceph_mds_request *req;
1959 struct ceph_mds_reply_head *head = msg->front.iov_base;
1960 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
1963 int mds = session->s_mds;
1965 if (msg->front.iov_len < sizeof(*head)) {
1966 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
1971 /* get request, session */
1972 tid = le64_to_cpu(msg->hdr.tid);
1973 mutex_lock(&mdsc->mutex);
1974 req = __lookup_request(mdsc, tid);
1976 dout("handle_reply on unknown tid %llu\n", tid);
1977 mutex_unlock(&mdsc->mutex);
1980 dout("handle_reply %p\n", req);
1982 /* correct session? */
1983 if (req->r_session != session) {
1984 pr_err("mdsc_handle_reply got %llu on session mds%d"
1985 " not mds%d\n", tid, session->s_mds,
1986 req->r_session ? req->r_session->s_mds : -1);
1987 mutex_unlock(&mdsc->mutex);
1992 if ((req->r_got_unsafe && !head->safe) ||
1993 (req->r_got_safe && head->safe)) {
1994 pr_warning("got a dup %s reply on %llu from mds%d\n",
1995 head->safe ? "safe" : "unsafe", tid, mds);
1996 mutex_unlock(&mdsc->mutex);
1999 if (req->r_got_safe && !head->safe) {
2000 pr_warning("got unsafe after safe on %llu from mds%d\n",
2002 mutex_unlock(&mdsc->mutex);
2006 result = le32_to_cpu(head->result);
2010 * if we're not talking to the authority, send to them
2011 * if the authority has changed while we weren't looking,
2012 * send to new authority
2013 * Otherwise we just have to return an ESTALE
2015 if (result == -ESTALE) {
2016 dout("got ESTALE on request %llu", req->r_tid);
2017 if (!req->r_inode) {
2018 /* do nothing; not an authority problem */
2019 } else if (req->r_direct_mode != USE_AUTH_MDS) {
2020 dout("not using auth, setting for that now");
2021 req->r_direct_mode = USE_AUTH_MDS;
2022 __do_request(mdsc, req);
2023 mutex_unlock(&mdsc->mutex);
2026 struct ceph_inode_info *ci = ceph_inode(req->r_inode);
2027 struct ceph_cap *cap =
2028 ceph_get_cap_for_mds(ci, req->r_mds);;
2030 dout("already using auth");
2031 if ((!cap || cap != ci->i_auth_cap) ||
2032 (cap->mseq != req->r_sent_on_mseq)) {
2033 dout("but cap changed, so resending");
2034 __do_request(mdsc, req);
2035 mutex_unlock(&mdsc->mutex);
2039 dout("have to return ESTALE on request %llu", req->r_tid);
2044 req->r_got_safe = true;
2045 __unregister_request(mdsc, req);
2046 complete_all(&req->r_safe_completion);
2048 if (req->r_got_unsafe) {
2050 * We already handled the unsafe response, now do the
2051 * cleanup. No need to examine the response; the MDS
2052 * doesn't include any result info in the safe
2053 * response. And even if it did, there is nothing
2054 * useful we could do with a revised return value.
2056 dout("got safe reply %llu, mds%d\n", tid, mds);
2057 list_del_init(&req->r_unsafe_item);
2059 /* last unsafe request during umount? */
2060 if (mdsc->stopping && !__get_oldest_req(mdsc))
2061 complete_all(&mdsc->safe_umount_waiters);
2062 mutex_unlock(&mdsc->mutex);
2066 req->r_got_unsafe = true;
2067 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2070 dout("handle_reply tid %lld result %d\n", tid, result);
2071 rinfo = &req->r_reply_info;
2072 err = parse_reply_info(msg, rinfo);
2073 mutex_unlock(&mdsc->mutex);
2075 mutex_lock(&session->s_mutex);
2077 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds);
2083 if (rinfo->snapblob_len) {
2084 down_write(&mdsc->snap_rwsem);
2085 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2086 rinfo->snapblob + rinfo->snapblob_len,
2087 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2088 downgrade_write(&mdsc->snap_rwsem);
2090 down_read(&mdsc->snap_rwsem);
2093 /* insert trace into our cache */
2094 mutex_lock(&req->r_fill_mutex);
2095 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
2097 if (result == 0 && rinfo->dir_nr)
2098 ceph_readdir_prepopulate(req, req->r_session);
2099 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2101 mutex_unlock(&req->r_fill_mutex);
2103 up_read(&mdsc->snap_rwsem);
2105 mutex_lock(&mdsc->mutex);
2106 if (!req->r_aborted) {
2112 req->r_got_result = true;
2115 dout("reply arrived after request %lld was aborted\n", tid);
2117 mutex_unlock(&mdsc->mutex);
2119 ceph_add_cap_releases(mdsc, req->r_session);
2120 mutex_unlock(&session->s_mutex);
2122 /* kick calling process */
2123 complete_request(mdsc, req);
2125 ceph_mdsc_put_request(req);
2132 * handle mds notification that our request has been forwarded.
2134 static void handle_forward(struct ceph_mds_client *mdsc,
2135 struct ceph_mds_session *session,
2136 struct ceph_msg *msg)
2138 struct ceph_mds_request *req;
2139 u64 tid = le64_to_cpu(msg->hdr.tid);
2143 void *p = msg->front.iov_base;
2144 void *end = p + msg->front.iov_len;
2146 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2147 next_mds = ceph_decode_32(&p);
2148 fwd_seq = ceph_decode_32(&p);
2150 mutex_lock(&mdsc->mutex);
2151 req = __lookup_request(mdsc, tid);
2153 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2154 goto out; /* dup reply? */
2157 if (req->r_aborted) {
2158 dout("forward tid %llu aborted, unregistering\n", tid);
2159 __unregister_request(mdsc, req);
2160 } else if (fwd_seq <= req->r_num_fwd) {
2161 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2162 tid, next_mds, req->r_num_fwd, fwd_seq);
2164 /* resend. forward race not possible; mds would drop */
2165 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2167 BUG_ON(req->r_got_result);
2168 req->r_num_fwd = fwd_seq;
2169 req->r_resend_mds = next_mds;
2170 put_request_session(req);
2171 __do_request(mdsc, req);
2173 ceph_mdsc_put_request(req);
2175 mutex_unlock(&mdsc->mutex);
2179 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2183 * handle a mds session control message
2185 static void handle_session(struct ceph_mds_session *session,
2186 struct ceph_msg *msg)
2188 struct ceph_mds_client *mdsc = session->s_mdsc;
2191 int mds = session->s_mds;
2192 struct ceph_mds_session_head *h = msg->front.iov_base;
2196 if (msg->front.iov_len != sizeof(*h))
2198 op = le32_to_cpu(h->op);
2199 seq = le64_to_cpu(h->seq);
2201 mutex_lock(&mdsc->mutex);
2202 if (op == CEPH_SESSION_CLOSE)
2203 __unregister_session(mdsc, session);
2204 /* FIXME: this ttl calculation is generous */
2205 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2206 mutex_unlock(&mdsc->mutex);
2208 mutex_lock(&session->s_mutex);
2210 dout("handle_session mds%d %s %p state %s seq %llu\n",
2211 mds, ceph_session_op_name(op), session,
2212 session_state_name(session->s_state), seq);
2214 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2215 session->s_state = CEPH_MDS_SESSION_OPEN;
2216 pr_info("mds%d came back\n", session->s_mds);
2220 case CEPH_SESSION_OPEN:
2221 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2222 pr_info("mds%d reconnect success\n", session->s_mds);
2223 session->s_state = CEPH_MDS_SESSION_OPEN;
2224 renewed_caps(mdsc, session, 0);
2227 __close_session(mdsc, session);
2230 case CEPH_SESSION_RENEWCAPS:
2231 if (session->s_renew_seq == seq)
2232 renewed_caps(mdsc, session, 1);
2235 case CEPH_SESSION_CLOSE:
2236 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2237 pr_info("mds%d reconnect denied\n", session->s_mds);
2238 remove_session_caps(session);
2239 wake = 1; /* for good measure */
2240 wake_up_all(&mdsc->session_close_wq);
2241 kick_requests(mdsc, mds);
2244 case CEPH_SESSION_STALE:
2245 pr_info("mds%d caps went stale, renewing\n",
2247 spin_lock(&session->s_cap_lock);
2248 session->s_cap_gen++;
2249 session->s_cap_ttl = 0;
2250 spin_unlock(&session->s_cap_lock);
2251 send_renew_caps(mdsc, session);
2254 case CEPH_SESSION_RECALL_STATE:
2255 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2259 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2263 mutex_unlock(&session->s_mutex);
2265 mutex_lock(&mdsc->mutex);
2266 __wake_requests(mdsc, &session->s_waiting);
2267 mutex_unlock(&mdsc->mutex);
2272 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2273 (int)msg->front.iov_len);
2280 * called under session->mutex.
2282 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2283 struct ceph_mds_session *session)
2285 struct ceph_mds_request *req, *nreq;
2288 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2290 mutex_lock(&mdsc->mutex);
2291 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2292 err = __prepare_send_request(mdsc, req, session->s_mds);
2294 ceph_msg_get(req->r_request);
2295 ceph_con_send(&session->s_con, req->r_request);
2298 mutex_unlock(&mdsc->mutex);
2302 * Encode information about a cap for a reconnect with the MDS.
2304 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2308 struct ceph_mds_cap_reconnect v2;
2309 struct ceph_mds_cap_reconnect_v1 v1;
2312 struct ceph_inode_info *ci;
2313 struct ceph_reconnect_state *recon_state = arg;
2314 struct ceph_pagelist *pagelist = recon_state->pagelist;
2318 struct dentry *dentry;
2322 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2323 inode, ceph_vinop(inode), cap, cap->cap_id,
2324 ceph_cap_string(cap->issued));
2325 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2329 dentry = d_find_alias(inode);
2331 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2333 err = PTR_ERR(path);
2340 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2344 spin_lock(&inode->i_lock);
2345 cap->seq = 0; /* reset cap seq */
2346 cap->issue_seq = 0; /* and issue_seq */
2348 if (recon_state->flock) {
2349 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2350 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2351 rec.v2.issued = cpu_to_le32(cap->issued);
2352 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2353 rec.v2.pathbase = cpu_to_le64(pathbase);
2354 rec.v2.flock_len = 0;
2355 reclen = sizeof(rec.v2);
2357 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2358 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2359 rec.v1.issued = cpu_to_le32(cap->issued);
2360 rec.v1.size = cpu_to_le64(inode->i_size);
2361 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2362 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2363 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2364 rec.v1.pathbase = cpu_to_le64(pathbase);
2365 reclen = sizeof(rec.v1);
2367 spin_unlock(&inode->i_lock);
2369 if (recon_state->flock) {
2370 int num_fcntl_locks, num_flock_locks;
2371 struct ceph_pagelist_cursor trunc_point;
2373 ceph_pagelist_set_cursor(pagelist, &trunc_point);
2376 ceph_count_locks(inode, &num_fcntl_locks,
2378 rec.v2.flock_len = (2*sizeof(u32) +
2379 (num_fcntl_locks+num_flock_locks) *
2380 sizeof(struct ceph_filelock));
2383 /* pre-alloc pagelist */
2384 ceph_pagelist_truncate(pagelist, &trunc_point);
2385 err = ceph_pagelist_append(pagelist, &rec, reclen);
2387 err = ceph_pagelist_reserve(pagelist,
2393 err = ceph_encode_locks(inode,
2399 } while (err == -ENOSPC);
2401 err = ceph_pagelist_append(pagelist, &rec, reclen);
2413 * If an MDS fails and recovers, clients need to reconnect in order to
2414 * reestablish shared state. This includes all caps issued through
2415 * this session _and_ the snap_realm hierarchy. Because it's not
2416 * clear which snap realms the mds cares about, we send everything we
2417 * know about.. that ensures we'll then get any new info the
2418 * recovering MDS might have.
2420 * This is a relatively heavyweight operation, but it's rare.
2422 * called with mdsc->mutex held.
2424 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2425 struct ceph_mds_session *session)
2427 struct ceph_msg *reply;
2429 int mds = session->s_mds;
2431 struct ceph_pagelist *pagelist;
2432 struct ceph_reconnect_state recon_state;
2434 pr_info("mds%d reconnect start\n", mds);
2436 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2438 goto fail_nopagelist;
2439 ceph_pagelist_init(pagelist);
2441 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS);
2445 mutex_lock(&session->s_mutex);
2446 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2449 ceph_con_open(&session->s_con,
2450 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2452 /* replay unsafe requests */
2453 replay_unsafe_requests(mdsc, session);
2455 down_read(&mdsc->snap_rwsem);
2457 dout("session %p state %s\n", session,
2458 session_state_name(session->s_state));
2460 /* drop old cap expires; we're about to reestablish that state */
2461 discard_cap_releases(mdsc, session);
2463 /* traverse this session's caps */
2464 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2468 recon_state.pagelist = pagelist;
2469 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2470 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
2475 * snaprealms. we provide mds with the ino, seq (version), and
2476 * parent for all of our realms. If the mds has any newer info,
2479 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2480 struct ceph_snap_realm *realm =
2481 rb_entry(p, struct ceph_snap_realm, node);
2482 struct ceph_mds_snaprealm_reconnect sr_rec;
2484 dout(" adding snap realm %llx seq %lld parent %llx\n",
2485 realm->ino, realm->seq, realm->parent_ino);
2486 sr_rec.ino = cpu_to_le64(realm->ino);
2487 sr_rec.seq = cpu_to_le64(realm->seq);
2488 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2489 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2494 reply->pagelist = pagelist;
2495 if (recon_state.flock)
2496 reply->hdr.version = cpu_to_le16(2);
2497 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2498 reply->nr_pages = calc_pages_for(0, pagelist->length);
2499 ceph_con_send(&session->s_con, reply);
2501 mutex_unlock(&session->s_mutex);
2503 mutex_lock(&mdsc->mutex);
2504 __wake_requests(mdsc, &session->s_waiting);
2505 mutex_unlock(&mdsc->mutex);
2507 up_read(&mdsc->snap_rwsem);
2511 ceph_msg_put(reply);
2512 up_read(&mdsc->snap_rwsem);
2513 mutex_unlock(&session->s_mutex);
2515 ceph_pagelist_release(pagelist);
2518 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
2524 * compare old and new mdsmaps, kicking requests
2525 * and closing out old connections as necessary
2527 * called under mdsc->mutex.
2529 static void check_new_map(struct ceph_mds_client *mdsc,
2530 struct ceph_mdsmap *newmap,
2531 struct ceph_mdsmap *oldmap)
2534 int oldstate, newstate;
2535 struct ceph_mds_session *s;
2537 dout("check_new_map new %u old %u\n",
2538 newmap->m_epoch, oldmap->m_epoch);
2540 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2541 if (mdsc->sessions[i] == NULL)
2543 s = mdsc->sessions[i];
2544 oldstate = ceph_mdsmap_get_state(oldmap, i);
2545 newstate = ceph_mdsmap_get_state(newmap, i);
2547 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2548 i, ceph_mds_state_name(oldstate),
2549 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
2550 ceph_mds_state_name(newstate),
2551 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
2552 session_state_name(s->s_state));
2554 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2555 ceph_mdsmap_get_addr(newmap, i),
2556 sizeof(struct ceph_entity_addr))) {
2557 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2558 /* the session never opened, just close it
2560 __wake_requests(mdsc, &s->s_waiting);
2561 __unregister_session(mdsc, s);
2564 mutex_unlock(&mdsc->mutex);
2565 mutex_lock(&s->s_mutex);
2566 mutex_lock(&mdsc->mutex);
2567 ceph_con_close(&s->s_con);
2568 mutex_unlock(&s->s_mutex);
2569 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2572 /* kick any requests waiting on the recovering mds */
2573 kick_requests(mdsc, i);
2574 } else if (oldstate == newstate) {
2575 continue; /* nothing new with this mds */
2581 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
2582 newstate >= CEPH_MDS_STATE_RECONNECT) {
2583 mutex_unlock(&mdsc->mutex);
2584 send_mds_reconnect(mdsc, s);
2585 mutex_lock(&mdsc->mutex);
2589 * kick request on any mds that has gone active.
2591 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2592 newstate >= CEPH_MDS_STATE_ACTIVE) {
2593 if (oldstate != CEPH_MDS_STATE_CREATING &&
2594 oldstate != CEPH_MDS_STATE_STARTING)
2595 pr_info("mds%d recovery completed\n", s->s_mds);
2596 kick_requests(mdsc, i);
2597 ceph_kick_flushing_caps(mdsc, s);
2598 wake_up_session_caps(s, 1);
2602 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2603 s = mdsc->sessions[i];
2606 if (!ceph_mdsmap_is_laggy(newmap, i))
2608 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2609 s->s_state == CEPH_MDS_SESSION_HUNG ||
2610 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2611 dout(" connecting to export targets of laggy mds%d\n",
2613 __open_export_target_sessions(mdsc, s);
2625 * caller must hold session s_mutex, dentry->d_lock
2627 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2629 struct ceph_dentry_info *di = ceph_dentry(dentry);
2631 ceph_put_mds_session(di->lease_session);
2632 di->lease_session = NULL;
2635 static void handle_lease(struct ceph_mds_client *mdsc,
2636 struct ceph_mds_session *session,
2637 struct ceph_msg *msg)
2639 struct super_block *sb = mdsc->fsc->sb;
2640 struct inode *inode;
2641 struct ceph_inode_info *ci;
2642 struct dentry *parent, *dentry;
2643 struct ceph_dentry_info *di;
2644 int mds = session->s_mds;
2645 struct ceph_mds_lease *h = msg->front.iov_base;
2647 struct ceph_vino vino;
2652 dout("handle_lease from mds%d\n", mds);
2655 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2657 vino.ino = le64_to_cpu(h->ino);
2658 vino.snap = CEPH_NOSNAP;
2659 mask = le16_to_cpu(h->mask);
2660 seq = le32_to_cpu(h->seq);
2661 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2662 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2663 if (dname.len != get_unaligned_le32(h+1))
2666 mutex_lock(&session->s_mutex);
2670 inode = ceph_find_inode(sb, vino);
2671 dout("handle_lease %s, mask %d, ino %llx %p %.*s\n",
2672 ceph_lease_op_name(h->action), mask, vino.ino, inode,
2673 dname.len, dname.name);
2674 if (inode == NULL) {
2675 dout("handle_lease no inode %llx\n", vino.ino);
2678 ci = ceph_inode(inode);
2681 parent = d_find_alias(inode);
2683 dout("no parent dentry on inode %p\n", inode);
2685 goto release; /* hrm... */
2687 dname.hash = full_name_hash(dname.name, dname.len);
2688 dentry = d_lookup(parent, &dname);
2693 spin_lock(&dentry->d_lock);
2694 di = ceph_dentry(dentry);
2695 switch (h->action) {
2696 case CEPH_MDS_LEASE_REVOKE:
2697 if (di && di->lease_session == session) {
2698 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2699 h->seq = cpu_to_le32(di->lease_seq);
2700 __ceph_mdsc_drop_dentry_lease(dentry);
2705 case CEPH_MDS_LEASE_RENEW:
2706 if (di && di->lease_session == session &&
2707 di->lease_gen == session->s_cap_gen &&
2708 di->lease_renew_from &&
2709 di->lease_renew_after == 0) {
2710 unsigned long duration =
2711 le32_to_cpu(h->duration_ms) * HZ / 1000;
2713 di->lease_seq = seq;
2714 dentry->d_time = di->lease_renew_from + duration;
2715 di->lease_renew_after = di->lease_renew_from +
2717 di->lease_renew_from = 0;
2721 spin_unlock(&dentry->d_lock);
2728 /* let's just reuse the same message */
2729 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2731 ceph_con_send(&session->s_con, msg);
2735 mutex_unlock(&session->s_mutex);
2739 pr_err("corrupt lease message\n");
2743 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2744 struct inode *inode,
2745 struct dentry *dentry, char action,
2748 struct ceph_msg *msg;
2749 struct ceph_mds_lease *lease;
2750 int len = sizeof(*lease) + sizeof(u32);
2753 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2754 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2755 dnamelen = dentry->d_name.len;
2758 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS);
2761 lease = msg->front.iov_base;
2762 lease->action = action;
2763 lease->mask = cpu_to_le16(1);
2764 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2765 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2766 lease->seq = cpu_to_le32(seq);
2767 put_unaligned_le32(dnamelen, lease + 1);
2768 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2771 * if this is a preemptive lease RELEASE, no need to
2772 * flush request stream, since the actual request will
2775 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2777 ceph_con_send(&session->s_con, msg);
2781 * Preemptively release a lease we expect to invalidate anyway.
2782 * Pass @inode always, @dentry is optional.
2784 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2785 struct dentry *dentry, int mask)
2787 struct ceph_dentry_info *di;
2788 struct ceph_mds_session *session;
2791 BUG_ON(inode == NULL);
2792 BUG_ON(dentry == NULL);
2795 /* is dentry lease valid? */
2796 spin_lock(&dentry->d_lock);
2797 di = ceph_dentry(dentry);
2798 if (!di || !di->lease_session ||
2799 di->lease_session->s_mds < 0 ||
2800 di->lease_gen != di->lease_session->s_cap_gen ||
2801 !time_before(jiffies, dentry->d_time)) {
2802 dout("lease_release inode %p dentry %p -- "
2804 inode, dentry, mask);
2805 spin_unlock(&dentry->d_lock);
2809 /* we do have a lease on this dentry; note mds and seq */
2810 session = ceph_get_mds_session(di->lease_session);
2811 seq = di->lease_seq;
2812 __ceph_mdsc_drop_dentry_lease(dentry);
2813 spin_unlock(&dentry->d_lock);
2815 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2816 inode, dentry, mask, session->s_mds);
2817 ceph_mdsc_lease_send_msg(session, inode, dentry,
2818 CEPH_MDS_LEASE_RELEASE, seq);
2819 ceph_put_mds_session(session);
2823 * drop all leases (and dentry refs) in preparation for umount
2825 static void drop_leases(struct ceph_mds_client *mdsc)
2829 dout("drop_leases\n");
2830 mutex_lock(&mdsc->mutex);
2831 for (i = 0; i < mdsc->max_sessions; i++) {
2832 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2835 mutex_unlock(&mdsc->mutex);
2836 mutex_lock(&s->s_mutex);
2837 mutex_unlock(&s->s_mutex);
2838 ceph_put_mds_session(s);
2839 mutex_lock(&mdsc->mutex);
2841 mutex_unlock(&mdsc->mutex);
2847 * delayed work -- periodically trim expired leases, renew caps with mds
2849 static void schedule_delayed(struct ceph_mds_client *mdsc)
2852 unsigned hz = round_jiffies_relative(HZ * delay);
2853 schedule_delayed_work(&mdsc->delayed_work, hz);
2856 static void delayed_work(struct work_struct *work)
2859 struct ceph_mds_client *mdsc =
2860 container_of(work, struct ceph_mds_client, delayed_work.work);
2864 dout("mdsc delayed_work\n");
2865 ceph_check_delayed_caps(mdsc);
2867 mutex_lock(&mdsc->mutex);
2868 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2869 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2870 mdsc->last_renew_caps);
2872 mdsc->last_renew_caps = jiffies;
2874 for (i = 0; i < mdsc->max_sessions; i++) {
2875 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2878 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2879 dout("resending session close request for mds%d\n",
2881 request_close_session(mdsc, s);
2882 ceph_put_mds_session(s);
2885 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2886 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2887 s->s_state = CEPH_MDS_SESSION_HUNG;
2888 pr_info("mds%d hung\n", s->s_mds);
2891 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2892 /* this mds is failed or recovering, just wait */
2893 ceph_put_mds_session(s);
2896 mutex_unlock(&mdsc->mutex);
2898 mutex_lock(&s->s_mutex);
2900 send_renew_caps(mdsc, s);
2902 ceph_con_keepalive(&s->s_con);
2903 ceph_add_cap_releases(mdsc, s);
2904 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2905 s->s_state == CEPH_MDS_SESSION_HUNG)
2906 ceph_send_cap_releases(mdsc, s);
2907 mutex_unlock(&s->s_mutex);
2908 ceph_put_mds_session(s);
2910 mutex_lock(&mdsc->mutex);
2912 mutex_unlock(&mdsc->mutex);
2914 schedule_delayed(mdsc);
2917 int ceph_mdsc_init(struct ceph_fs_client *fsc)
2920 struct ceph_mds_client *mdsc;
2922 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
2927 mutex_init(&mdsc->mutex);
2928 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
2929 if (mdsc->mdsmap == NULL)
2932 init_completion(&mdsc->safe_umount_waiters);
2933 init_waitqueue_head(&mdsc->session_close_wq);
2934 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2935 mdsc->sessions = NULL;
2936 mdsc->max_sessions = 0;
2938 init_rwsem(&mdsc->snap_rwsem);
2939 mdsc->snap_realms = RB_ROOT;
2940 INIT_LIST_HEAD(&mdsc->snap_empty);
2941 spin_lock_init(&mdsc->snap_empty_lock);
2943 mdsc->request_tree = RB_ROOT;
2944 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2945 mdsc->last_renew_caps = jiffies;
2946 INIT_LIST_HEAD(&mdsc->cap_delay_list);
2947 spin_lock_init(&mdsc->cap_delay_lock);
2948 INIT_LIST_HEAD(&mdsc->snap_flush_list);
2949 spin_lock_init(&mdsc->snap_flush_lock);
2950 mdsc->cap_flush_seq = 0;
2951 INIT_LIST_HEAD(&mdsc->cap_dirty);
2952 mdsc->num_cap_flushing = 0;
2953 spin_lock_init(&mdsc->cap_dirty_lock);
2954 init_waitqueue_head(&mdsc->cap_flushing_wq);
2955 spin_lock_init(&mdsc->dentry_lru_lock);
2956 INIT_LIST_HEAD(&mdsc->dentry_lru);
2958 ceph_caps_init(mdsc);
2959 ceph_adjust_min_caps(mdsc, fsc->min_caps);
2965 * Wait for safe replies on open mds requests. If we time out, drop
2966 * all requests from the tree to avoid dangling dentry refs.
2968 static void wait_requests(struct ceph_mds_client *mdsc)
2970 struct ceph_mds_request *req;
2971 struct ceph_fs_client *fsc = mdsc->fsc;
2973 mutex_lock(&mdsc->mutex);
2974 if (__get_oldest_req(mdsc)) {
2975 mutex_unlock(&mdsc->mutex);
2977 dout("wait_requests waiting for requests\n");
2978 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
2979 fsc->client->options->mount_timeout * HZ);
2981 /* tear down remaining requests */
2982 mutex_lock(&mdsc->mutex);
2983 while ((req = __get_oldest_req(mdsc))) {
2984 dout("wait_requests timed out on tid %llu\n",
2986 __unregister_request(mdsc, req);
2989 mutex_unlock(&mdsc->mutex);
2990 dout("wait_requests done\n");
2994 * called before mount is ro, and before dentries are torn down.
2995 * (hmm, does this still race with new lookups?)
2997 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
2999 dout("pre_umount\n");
3003 ceph_flush_dirty_caps(mdsc);
3004 wait_requests(mdsc);
3007 * wait for reply handlers to drop their request refs and
3008 * their inode/dcache refs
3014 * wait for all write mds requests to flush.
3016 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3018 struct ceph_mds_request *req = NULL, *nextreq;
3021 mutex_lock(&mdsc->mutex);
3022 dout("wait_unsafe_requests want %lld\n", want_tid);
3024 req = __get_oldest_req(mdsc);
3025 while (req && req->r_tid <= want_tid) {
3026 /* find next request */
3027 n = rb_next(&req->r_node);
3029 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3032 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3034 ceph_mdsc_get_request(req);
3036 ceph_mdsc_get_request(nextreq);
3037 mutex_unlock(&mdsc->mutex);
3038 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3039 req->r_tid, want_tid);
3040 wait_for_completion(&req->r_safe_completion);
3041 mutex_lock(&mdsc->mutex);
3042 ceph_mdsc_put_request(req);
3044 break; /* next dne before, so we're done! */
3045 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3046 /* next request was removed from tree */
3047 ceph_mdsc_put_request(nextreq);
3050 ceph_mdsc_put_request(nextreq); /* won't go away */
3054 mutex_unlock(&mdsc->mutex);
3055 dout("wait_unsafe_requests done\n");
3058 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3060 u64 want_tid, want_flush;
3062 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3066 mutex_lock(&mdsc->mutex);
3067 want_tid = mdsc->last_tid;
3068 want_flush = mdsc->cap_flush_seq;
3069 mutex_unlock(&mdsc->mutex);
3070 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3072 ceph_flush_dirty_caps(mdsc);
3074 wait_unsafe_requests(mdsc, want_tid);
3075 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3079 * true if all sessions are closed, or we force unmount
3081 bool done_closing_sessions(struct ceph_mds_client *mdsc)
3085 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3088 mutex_lock(&mdsc->mutex);
3089 for (i = 0; i < mdsc->max_sessions; i++)
3090 if (mdsc->sessions[i])
3092 mutex_unlock(&mdsc->mutex);
3097 * called after sb is ro.
3099 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3101 struct ceph_mds_session *session;
3103 struct ceph_fs_client *fsc = mdsc->fsc;
3104 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
3106 dout("close_sessions\n");
3108 /* close sessions */
3109 mutex_lock(&mdsc->mutex);
3110 for (i = 0; i < mdsc->max_sessions; i++) {
3111 session = __ceph_lookup_mds_session(mdsc, i);
3114 mutex_unlock(&mdsc->mutex);
3115 mutex_lock(&session->s_mutex);
3116 __close_session(mdsc, session);
3117 mutex_unlock(&session->s_mutex);
3118 ceph_put_mds_session(session);
3119 mutex_lock(&mdsc->mutex);
3121 mutex_unlock(&mdsc->mutex);
3123 dout("waiting for sessions to close\n");
3124 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3127 /* tear down remaining sessions */
3128 mutex_lock(&mdsc->mutex);
3129 for (i = 0; i < mdsc->max_sessions; i++) {
3130 if (mdsc->sessions[i]) {
3131 session = get_session(mdsc->sessions[i]);
3132 __unregister_session(mdsc, session);
3133 mutex_unlock(&mdsc->mutex);
3134 mutex_lock(&session->s_mutex);
3135 remove_session_caps(session);
3136 mutex_unlock(&session->s_mutex);
3137 ceph_put_mds_session(session);
3138 mutex_lock(&mdsc->mutex);
3141 WARN_ON(!list_empty(&mdsc->cap_delay_list));
3142 mutex_unlock(&mdsc->mutex);
3144 ceph_cleanup_empty_realms(mdsc);
3146 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3151 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
3154 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3156 ceph_mdsmap_destroy(mdsc->mdsmap);
3157 kfree(mdsc->sessions);
3158 ceph_caps_finalize(mdsc);
3161 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3163 struct ceph_mds_client *mdsc = fsc->mdsc;
3165 ceph_mdsc_stop(mdsc);
3172 * handle mds map update.
3174 void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3178 void *p = msg->front.iov_base;
3179 void *end = p + msg->front.iov_len;
3180 struct ceph_mdsmap *newmap, *oldmap;
3181 struct ceph_fsid fsid;
3184 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3185 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3186 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
3188 epoch = ceph_decode_32(&p);
3189 maplen = ceph_decode_32(&p);
3190 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3192 /* do we need it? */
3193 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
3194 mutex_lock(&mdsc->mutex);
3195 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3196 dout("handle_map epoch %u <= our %u\n",
3197 epoch, mdsc->mdsmap->m_epoch);
3198 mutex_unlock(&mdsc->mutex);
3202 newmap = ceph_mdsmap_decode(&p, end);
3203 if (IS_ERR(newmap)) {
3204 err = PTR_ERR(newmap);
3208 /* swap into place */
3210 oldmap = mdsc->mdsmap;
3211 mdsc->mdsmap = newmap;
3212 check_new_map(mdsc, newmap, oldmap);
3213 ceph_mdsmap_destroy(oldmap);
3215 mdsc->mdsmap = newmap; /* first mds map */
3217 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
3219 __wake_requests(mdsc, &mdsc->waiting_for_map);
3221 mutex_unlock(&mdsc->mutex);
3222 schedule_delayed(mdsc);
3226 mutex_unlock(&mdsc->mutex);
3228 pr_err("error decoding mdsmap %d\n", err);
3232 static struct ceph_connection *con_get(struct ceph_connection *con)
3234 struct ceph_mds_session *s = con->private;
3236 if (get_session(s)) {
3237 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
3240 dout("mdsc con_get %p FAIL\n", s);
3244 static void con_put(struct ceph_connection *con)
3246 struct ceph_mds_session *s = con->private;
3248 ceph_put_mds_session(s);
3249 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
3253 * if the client is unresponsive for long enough, the mds will kill
3254 * the session entirely.
3256 static void peer_reset(struct ceph_connection *con)
3258 struct ceph_mds_session *s = con->private;
3259 struct ceph_mds_client *mdsc = s->s_mdsc;
3261 pr_warning("mds%d closed our session\n", s->s_mds);
3262 send_mds_reconnect(mdsc, s);
3265 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3267 struct ceph_mds_session *s = con->private;
3268 struct ceph_mds_client *mdsc = s->s_mdsc;
3269 int type = le16_to_cpu(msg->hdr.type);
3271 mutex_lock(&mdsc->mutex);
3272 if (__verify_registered_session(mdsc, s) < 0) {
3273 mutex_unlock(&mdsc->mutex);
3276 mutex_unlock(&mdsc->mutex);
3279 case CEPH_MSG_MDS_MAP:
3280 ceph_mdsc_handle_map(mdsc, msg);
3282 case CEPH_MSG_CLIENT_SESSION:
3283 handle_session(s, msg);
3285 case CEPH_MSG_CLIENT_REPLY:
3286 handle_reply(s, msg);
3288 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
3289 handle_forward(mdsc, s, msg);
3291 case CEPH_MSG_CLIENT_CAPS:
3292 ceph_handle_caps(s, msg);
3294 case CEPH_MSG_CLIENT_SNAP:
3295 ceph_handle_snap(mdsc, s, msg);
3297 case CEPH_MSG_CLIENT_LEASE:
3298 handle_lease(mdsc, s, msg);
3302 pr_err("received unknown message type %d %s\n", type,
3303 ceph_msg_type_name(type));
3312 static int get_authorizer(struct ceph_connection *con,
3313 void **buf, int *len, int *proto,
3314 void **reply_buf, int *reply_len, int force_new)
3316 struct ceph_mds_session *s = con->private;
3317 struct ceph_mds_client *mdsc = s->s_mdsc;
3318 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3321 if (force_new && s->s_authorizer) {
3322 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3323 s->s_authorizer = NULL;
3325 if (s->s_authorizer == NULL) {
3326 if (ac->ops->create_authorizer) {
3327 ret = ac->ops->create_authorizer(
3328 ac, CEPH_ENTITY_TYPE_MDS,
3330 &s->s_authorizer_buf,
3331 &s->s_authorizer_buf_len,
3332 &s->s_authorizer_reply_buf,
3333 &s->s_authorizer_reply_buf_len);
3339 *proto = ac->protocol;
3340 *buf = s->s_authorizer_buf;
3341 *len = s->s_authorizer_buf_len;
3342 *reply_buf = s->s_authorizer_reply_buf;
3343 *reply_len = s->s_authorizer_reply_buf_len;
3348 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3350 struct ceph_mds_session *s = con->private;
3351 struct ceph_mds_client *mdsc = s->s_mdsc;
3352 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3354 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3357 static int invalidate_authorizer(struct ceph_connection *con)
3359 struct ceph_mds_session *s = con->private;
3360 struct ceph_mds_client *mdsc = s->s_mdsc;
3361 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3363 if (ac->ops->invalidate_authorizer)
3364 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3366 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
3369 static const struct ceph_connection_operations mds_con_ops = {
3372 .dispatch = dispatch,
3373 .get_authorizer = get_authorizer,
3374 .verify_authorizer_reply = verify_authorizer_reply,
3375 .invalidate_authorizer = invalidate_authorizer,
3376 .peer_reset = peer_reset,