1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/wait.h>
4 #include <linux/slab.h>
5 #include <linux/sched.h>
6 #include <linux/smp_lock.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);
532 struct ceph_inode_info *ci = ceph_inode(dir);
534 spin_lock(&ci->i_unsafe_lock);
535 req->r_unsafe_dir = dir;
536 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
537 spin_unlock(&ci->i_unsafe_lock);
541 static void __unregister_request(struct ceph_mds_client *mdsc,
542 struct ceph_mds_request *req)
544 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
545 rb_erase(&req->r_node, &mdsc->request_tree);
546 RB_CLEAR_NODE(&req->r_node);
548 if (req->r_unsafe_dir) {
549 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
551 spin_lock(&ci->i_unsafe_lock);
552 list_del_init(&req->r_unsafe_dir_item);
553 spin_unlock(&ci->i_unsafe_lock);
556 ceph_mdsc_put_request(req);
560 * Choose mds to send request to next. If there is a hint set in the
561 * request (e.g., due to a prior forward hint from the mds), use that.
562 * Otherwise, consult frag tree and/or caps to identify the
563 * appropriate mds. If all else fails, choose randomly.
565 * Called under mdsc->mutex.
567 struct dentry *get_nonsnap_parent(struct dentry *dentry)
569 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
570 dentry = dentry->d_parent;
574 static int __choose_mds(struct ceph_mds_client *mdsc,
575 struct ceph_mds_request *req)
578 struct ceph_inode_info *ci;
579 struct ceph_cap *cap;
580 int mode = req->r_direct_mode;
582 u32 hash = req->r_direct_hash;
583 bool is_hash = req->r_direct_is_hash;
586 * is there a specific mds we should try? ignore hint if we have
587 * no session and the mds is not up (active or recovering).
589 if (req->r_resend_mds >= 0 &&
590 (__have_session(mdsc, req->r_resend_mds) ||
591 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
592 dout("choose_mds using resend_mds mds%d\n",
594 return req->r_resend_mds;
597 if (mode == USE_RANDOM_MDS)
602 inode = req->r_inode;
603 } else if (req->r_dentry) {
604 struct inode *dir = req->r_dentry->d_parent->d_inode;
606 if (dir->i_sb != mdsc->fsc->sb) {
608 inode = req->r_dentry->d_inode;
609 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
610 /* direct snapped/virtual snapdir requests
611 * based on parent dir inode */
613 get_nonsnap_parent(req->r_dentry->d_parent);
615 dout("__choose_mds using nonsnap parent %p\n", inode);
616 } else if (req->r_dentry->d_inode) {
618 inode = req->r_dentry->d_inode;
622 hash = req->r_dentry->d_name.hash;
627 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
631 ci = ceph_inode(inode);
633 if (is_hash && S_ISDIR(inode->i_mode)) {
634 struct ceph_inode_frag frag;
637 ceph_choose_frag(ci, hash, &frag, &found);
639 if (mode == USE_ANY_MDS && frag.ndist > 0) {
642 /* choose a random replica */
643 get_random_bytes(&r, 1);
646 dout("choose_mds %p %llx.%llx "
647 "frag %u mds%d (%d/%d)\n",
648 inode, ceph_vinop(inode),
654 /* since this file/dir wasn't known to be
655 * replicated, then we want to look for the
656 * authoritative mds. */
659 /* choose auth mds */
661 dout("choose_mds %p %llx.%llx "
662 "frag %u mds%d (auth)\n",
663 inode, ceph_vinop(inode), frag.frag, mds);
669 spin_lock(&inode->i_lock);
671 if (mode == USE_AUTH_MDS)
672 cap = ci->i_auth_cap;
673 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
674 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
676 spin_unlock(&inode->i_lock);
679 mds = cap->session->s_mds;
680 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
681 inode, ceph_vinop(inode), mds,
682 cap == ci->i_auth_cap ? "auth " : "", cap);
683 spin_unlock(&inode->i_lock);
687 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
688 dout("choose_mds chose random mds%d\n", mds);
696 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
698 struct ceph_msg *msg;
699 struct ceph_mds_session_head *h;
701 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS);
703 pr_err("create_session_msg ENOMEM creating msg\n");
706 h = msg->front.iov_base;
707 h->op = cpu_to_le32(op);
708 h->seq = cpu_to_le64(seq);
713 * send session open request.
715 * called under mdsc->mutex
717 static int __open_session(struct ceph_mds_client *mdsc,
718 struct ceph_mds_session *session)
720 struct ceph_msg *msg;
722 int mds = session->s_mds;
724 /* wait for mds to go active? */
725 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
726 dout("open_session to mds%d (%s)\n", mds,
727 ceph_mds_state_name(mstate));
728 session->s_state = CEPH_MDS_SESSION_OPENING;
729 session->s_renew_requested = jiffies;
731 /* send connect message */
732 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
735 ceph_con_send(&session->s_con, msg);
740 * open sessions for any export targets for the given mds
742 * called under mdsc->mutex
744 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
745 struct ceph_mds_session *session)
747 struct ceph_mds_info *mi;
748 struct ceph_mds_session *ts;
749 int i, mds = session->s_mds;
752 if (mds >= mdsc->mdsmap->m_max_mds)
754 mi = &mdsc->mdsmap->m_info[mds];
755 dout("open_export_target_sessions for mds%d (%d targets)\n",
756 session->s_mds, mi->num_export_targets);
758 for (i = 0; i < mi->num_export_targets; i++) {
759 target = mi->export_targets[i];
760 ts = __ceph_lookup_mds_session(mdsc, target);
762 ts = register_session(mdsc, target);
766 if (session->s_state == CEPH_MDS_SESSION_NEW ||
767 session->s_state == CEPH_MDS_SESSION_CLOSING)
768 __open_session(mdsc, session);
770 dout(" mds%d target mds%d %p is %s\n", session->s_mds,
771 i, ts, session_state_name(ts->s_state));
772 ceph_put_mds_session(ts);
776 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
777 struct ceph_mds_session *session)
779 mutex_lock(&mdsc->mutex);
780 __open_export_target_sessions(mdsc, session);
781 mutex_unlock(&mdsc->mutex);
789 * Free preallocated cap messages assigned to this session
791 static void cleanup_cap_releases(struct ceph_mds_session *session)
793 struct ceph_msg *msg;
795 spin_lock(&session->s_cap_lock);
796 while (!list_empty(&session->s_cap_releases)) {
797 msg = list_first_entry(&session->s_cap_releases,
798 struct ceph_msg, list_head);
799 list_del_init(&msg->list_head);
802 while (!list_empty(&session->s_cap_releases_done)) {
803 msg = list_first_entry(&session->s_cap_releases_done,
804 struct ceph_msg, list_head);
805 list_del_init(&msg->list_head);
808 spin_unlock(&session->s_cap_lock);
812 * Helper to safely iterate over all caps associated with a session, with
813 * special care taken to handle a racing __ceph_remove_cap().
815 * Caller must hold session s_mutex.
817 static int iterate_session_caps(struct ceph_mds_session *session,
818 int (*cb)(struct inode *, struct ceph_cap *,
822 struct ceph_cap *cap;
823 struct inode *inode, *last_inode = NULL;
824 struct ceph_cap *old_cap = NULL;
827 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
828 spin_lock(&session->s_cap_lock);
829 p = session->s_caps.next;
830 while (p != &session->s_caps) {
831 cap = list_entry(p, struct ceph_cap, session_caps);
832 inode = igrab(&cap->ci->vfs_inode);
837 session->s_cap_iterator = cap;
838 spin_unlock(&session->s_cap_lock);
845 ceph_put_cap(session->s_mdsc, old_cap);
849 ret = cb(inode, cap, arg);
852 spin_lock(&session->s_cap_lock);
854 if (cap->ci == NULL) {
855 dout("iterate_session_caps finishing cap %p removal\n",
857 BUG_ON(cap->session != session);
858 list_del_init(&cap->session_caps);
859 session->s_nr_caps--;
861 old_cap = cap; /* put_cap it w/o locks held */
868 session->s_cap_iterator = NULL;
869 spin_unlock(&session->s_cap_lock);
874 ceph_put_cap(session->s_mdsc, old_cap);
879 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
882 struct ceph_inode_info *ci = ceph_inode(inode);
885 dout("removing cap %p, ci is %p, inode is %p\n",
886 cap, ci, &ci->vfs_inode);
887 spin_lock(&inode->i_lock);
888 __ceph_remove_cap(cap);
889 if (!__ceph_is_any_real_caps(ci)) {
890 struct ceph_mds_client *mdsc =
891 ceph_sb_to_client(inode->i_sb)->mdsc;
893 spin_lock(&mdsc->cap_dirty_lock);
894 if (!list_empty(&ci->i_dirty_item)) {
895 pr_info(" dropping dirty %s state for %p %lld\n",
896 ceph_cap_string(ci->i_dirty_caps),
897 inode, ceph_ino(inode));
898 ci->i_dirty_caps = 0;
899 list_del_init(&ci->i_dirty_item);
902 if (!list_empty(&ci->i_flushing_item)) {
903 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
904 ceph_cap_string(ci->i_flushing_caps),
905 inode, ceph_ino(inode));
906 ci->i_flushing_caps = 0;
907 list_del_init(&ci->i_flushing_item);
908 mdsc->num_cap_flushing--;
911 if (drop && ci->i_wrbuffer_ref) {
912 pr_info(" dropping dirty data for %p %lld\n",
913 inode, ceph_ino(inode));
914 ci->i_wrbuffer_ref = 0;
915 ci->i_wrbuffer_ref_head = 0;
918 spin_unlock(&mdsc->cap_dirty_lock);
920 spin_unlock(&inode->i_lock);
927 * caller must hold session s_mutex
929 static void remove_session_caps(struct ceph_mds_session *session)
931 dout("remove_session_caps on %p\n", session);
932 iterate_session_caps(session, remove_session_caps_cb, NULL);
933 BUG_ON(session->s_nr_caps > 0);
934 BUG_ON(!list_empty(&session->s_cap_flushing));
935 cleanup_cap_releases(session);
939 * wake up any threads waiting on this session's caps. if the cap is
940 * old (didn't get renewed on the client reconnect), remove it now.
942 * caller must hold s_mutex.
944 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
947 struct ceph_inode_info *ci = ceph_inode(inode);
949 wake_up_all(&ci->i_cap_wq);
951 spin_lock(&inode->i_lock);
952 ci->i_wanted_max_size = 0;
953 ci->i_requested_max_size = 0;
954 spin_unlock(&inode->i_lock);
959 static void wake_up_session_caps(struct ceph_mds_session *session,
962 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
963 iterate_session_caps(session, wake_up_session_cb,
964 (void *)(unsigned long)reconnect);
968 * Send periodic message to MDS renewing all currently held caps. The
969 * ack will reset the expiration for all caps from this session.
971 * caller holds s_mutex
973 static int send_renew_caps(struct ceph_mds_client *mdsc,
974 struct ceph_mds_session *session)
976 struct ceph_msg *msg;
979 if (time_after_eq(jiffies, session->s_cap_ttl) &&
980 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
981 pr_info("mds%d caps stale\n", session->s_mds);
982 session->s_renew_requested = jiffies;
984 /* do not try to renew caps until a recovering mds has reconnected
985 * with its clients. */
986 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
987 if (state < CEPH_MDS_STATE_RECONNECT) {
988 dout("send_renew_caps ignoring mds%d (%s)\n",
989 session->s_mds, ceph_mds_state_name(state));
993 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
994 ceph_mds_state_name(state));
995 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
996 ++session->s_renew_seq);
999 ceph_con_send(&session->s_con, msg);
1004 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1006 * Called under session->s_mutex
1008 static void renewed_caps(struct ceph_mds_client *mdsc,
1009 struct ceph_mds_session *session, int is_renew)
1014 spin_lock(&session->s_cap_lock);
1015 was_stale = is_renew && (session->s_cap_ttl == 0 ||
1016 time_after_eq(jiffies, session->s_cap_ttl));
1018 session->s_cap_ttl = session->s_renew_requested +
1019 mdsc->mdsmap->m_session_timeout*HZ;
1022 if (time_before(jiffies, session->s_cap_ttl)) {
1023 pr_info("mds%d caps renewed\n", session->s_mds);
1026 pr_info("mds%d caps still stale\n", session->s_mds);
1029 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1030 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1031 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1032 spin_unlock(&session->s_cap_lock);
1035 wake_up_session_caps(session, 0);
1039 * send a session close request
1041 static int request_close_session(struct ceph_mds_client *mdsc,
1042 struct ceph_mds_session *session)
1044 struct ceph_msg *msg;
1046 dout("request_close_session mds%d state %s seq %lld\n",
1047 session->s_mds, session_state_name(session->s_state),
1049 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1052 ceph_con_send(&session->s_con, msg);
1057 * Called with s_mutex held.
1059 static int __close_session(struct ceph_mds_client *mdsc,
1060 struct ceph_mds_session *session)
1062 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1064 session->s_state = CEPH_MDS_SESSION_CLOSING;
1065 return request_close_session(mdsc, session);
1069 * Trim old(er) caps.
1071 * Because we can't cache an inode without one or more caps, we do
1072 * this indirectly: if a cap is unused, we prune its aliases, at which
1073 * point the inode will hopefully get dropped to.
1075 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1076 * memory pressure from the MDS, though, so it needn't be perfect.
1078 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1080 struct ceph_mds_session *session = arg;
1081 struct ceph_inode_info *ci = ceph_inode(inode);
1082 int used, oissued, mine;
1084 if (session->s_trim_caps <= 0)
1087 spin_lock(&inode->i_lock);
1088 mine = cap->issued | cap->implemented;
1089 used = __ceph_caps_used(ci);
1090 oissued = __ceph_caps_issued_other(ci, cap);
1092 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1093 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1094 ceph_cap_string(used));
1095 if (ci->i_dirty_caps)
1096 goto out; /* dirty caps */
1097 if ((used & ~oissued) & mine)
1098 goto out; /* we need these caps */
1100 session->s_trim_caps--;
1102 /* we aren't the only cap.. just remove us */
1103 __ceph_remove_cap(cap);
1105 /* try to drop referring dentries */
1106 spin_unlock(&inode->i_lock);
1107 d_prune_aliases(inode);
1108 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1109 inode, cap, atomic_read(&inode->i_count));
1114 spin_unlock(&inode->i_lock);
1119 * Trim session cap count down to some max number.
1121 static int trim_caps(struct ceph_mds_client *mdsc,
1122 struct ceph_mds_session *session,
1125 int trim_caps = session->s_nr_caps - max_caps;
1127 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1128 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1129 if (trim_caps > 0) {
1130 session->s_trim_caps = trim_caps;
1131 iterate_session_caps(session, trim_caps_cb, session);
1132 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1133 session->s_mds, session->s_nr_caps, max_caps,
1134 trim_caps - session->s_trim_caps);
1135 session->s_trim_caps = 0;
1141 * Allocate cap_release messages. If there is a partially full message
1142 * in the queue, try to allocate enough to cover it's remainder, so that
1143 * we can send it immediately.
1145 * Called under s_mutex.
1147 int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
1148 struct ceph_mds_session *session)
1150 struct ceph_msg *msg, *partial = NULL;
1151 struct ceph_mds_cap_release *head;
1153 int extra = mdsc->fsc->mount_options->cap_release_safety;
1156 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1159 spin_lock(&session->s_cap_lock);
1161 if (!list_empty(&session->s_cap_releases)) {
1162 msg = list_first_entry(&session->s_cap_releases,
1165 head = msg->front.iov_base;
1166 num = le32_to_cpu(head->num);
1168 dout(" partial %p with (%d/%d)\n", msg, num,
1169 (int)CEPH_CAPS_PER_RELEASE);
1170 extra += CEPH_CAPS_PER_RELEASE - num;
1174 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1175 spin_unlock(&session->s_cap_lock);
1176 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1180 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1181 (int)msg->front.iov_len);
1182 head = msg->front.iov_base;
1183 head->num = cpu_to_le32(0);
1184 msg->front.iov_len = sizeof(*head);
1185 spin_lock(&session->s_cap_lock);
1186 list_add(&msg->list_head, &session->s_cap_releases);
1187 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1191 head = partial->front.iov_base;
1192 num = le32_to_cpu(head->num);
1193 dout(" queueing partial %p with %d/%d\n", partial, num,
1194 (int)CEPH_CAPS_PER_RELEASE);
1195 list_move_tail(&partial->list_head,
1196 &session->s_cap_releases_done);
1197 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
1200 spin_unlock(&session->s_cap_lock);
1206 * flush all dirty inode data to disk.
1208 * returns true if we've flushed through want_flush_seq
1210 static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1214 dout("check_cap_flush want %lld\n", want_flush_seq);
1215 mutex_lock(&mdsc->mutex);
1216 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1217 struct ceph_mds_session *session = mdsc->sessions[mds];
1221 get_session(session);
1222 mutex_unlock(&mdsc->mutex);
1224 mutex_lock(&session->s_mutex);
1225 if (!list_empty(&session->s_cap_flushing)) {
1226 struct ceph_inode_info *ci =
1227 list_entry(session->s_cap_flushing.next,
1228 struct ceph_inode_info,
1230 struct inode *inode = &ci->vfs_inode;
1232 spin_lock(&inode->i_lock);
1233 if (ci->i_cap_flush_seq <= want_flush_seq) {
1234 dout("check_cap_flush still flushing %p "
1235 "seq %lld <= %lld to mds%d\n", inode,
1236 ci->i_cap_flush_seq, want_flush_seq,
1240 spin_unlock(&inode->i_lock);
1242 mutex_unlock(&session->s_mutex);
1243 ceph_put_mds_session(session);
1247 mutex_lock(&mdsc->mutex);
1250 mutex_unlock(&mdsc->mutex);
1251 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1256 * called under s_mutex
1258 void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1259 struct ceph_mds_session *session)
1261 struct ceph_msg *msg;
1263 dout("send_cap_releases mds%d\n", session->s_mds);
1264 spin_lock(&session->s_cap_lock);
1265 while (!list_empty(&session->s_cap_releases_done)) {
1266 msg = list_first_entry(&session->s_cap_releases_done,
1267 struct ceph_msg, list_head);
1268 list_del_init(&msg->list_head);
1269 spin_unlock(&session->s_cap_lock);
1270 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1271 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1272 ceph_con_send(&session->s_con, msg);
1273 spin_lock(&session->s_cap_lock);
1275 spin_unlock(&session->s_cap_lock);
1278 static void discard_cap_releases(struct ceph_mds_client *mdsc,
1279 struct ceph_mds_session *session)
1281 struct ceph_msg *msg;
1282 struct ceph_mds_cap_release *head;
1285 dout("discard_cap_releases mds%d\n", session->s_mds);
1286 spin_lock(&session->s_cap_lock);
1288 /* zero out the in-progress message */
1289 msg = list_first_entry(&session->s_cap_releases,
1290 struct ceph_msg, list_head);
1291 head = msg->front.iov_base;
1292 num = le32_to_cpu(head->num);
1293 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1294 head->num = cpu_to_le32(0);
1295 session->s_num_cap_releases += num;
1297 /* requeue completed messages */
1298 while (!list_empty(&session->s_cap_releases_done)) {
1299 msg = list_first_entry(&session->s_cap_releases_done,
1300 struct ceph_msg, list_head);
1301 list_del_init(&msg->list_head);
1303 head = msg->front.iov_base;
1304 num = le32_to_cpu(head->num);
1305 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1307 session->s_num_cap_releases += num;
1308 head->num = cpu_to_le32(0);
1309 msg->front.iov_len = sizeof(*head);
1310 list_add(&msg->list_head, &session->s_cap_releases);
1313 spin_unlock(&session->s_cap_lock);
1321 * Create an mds request.
1323 struct ceph_mds_request *
1324 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1326 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1329 return ERR_PTR(-ENOMEM);
1331 mutex_init(&req->r_fill_mutex);
1333 req->r_started = jiffies;
1334 req->r_resend_mds = -1;
1335 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1337 kref_init(&req->r_kref);
1338 INIT_LIST_HEAD(&req->r_wait);
1339 init_completion(&req->r_completion);
1340 init_completion(&req->r_safe_completion);
1341 INIT_LIST_HEAD(&req->r_unsafe_item);
1344 req->r_direct_mode = mode;
1349 * return oldest (lowest) request, tid in request tree, 0 if none.
1351 * called under mdsc->mutex.
1353 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1355 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1357 return rb_entry(rb_first(&mdsc->request_tree),
1358 struct ceph_mds_request, r_node);
1361 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1363 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1371 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1372 * on build_path_from_dentry in fs/cifs/dir.c.
1374 * If @stop_on_nosnap, generate path relative to the first non-snapped
1377 * Encode hidden .snap dirs as a double /, i.e.
1378 * foo/.snap/bar -> foo//bar
1380 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1383 struct dentry *temp;
1388 return ERR_PTR(-EINVAL);
1392 for (temp = dentry; !IS_ROOT(temp);) {
1393 struct inode *inode = temp->d_inode;
1394 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1395 len++; /* slash only */
1396 else if (stop_on_nosnap && inode &&
1397 ceph_snap(inode) == CEPH_NOSNAP)
1400 len += 1 + temp->d_name.len;
1401 temp = temp->d_parent;
1403 pr_err("build_path corrupt dentry %p\n", dentry);
1404 return ERR_PTR(-EINVAL);
1408 len--; /* no leading '/' */
1410 path = kmalloc(len+1, GFP_NOFS);
1412 return ERR_PTR(-ENOMEM);
1414 path[pos] = 0; /* trailing null */
1415 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1416 struct inode *inode = temp->d_inode;
1418 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1419 dout("build_path path+%d: %p SNAPDIR\n",
1421 } else if (stop_on_nosnap && inode &&
1422 ceph_snap(inode) == CEPH_NOSNAP) {
1425 pos -= temp->d_name.len;
1428 strncpy(path + pos, temp->d_name.name,
1433 temp = temp->d_parent;
1435 pr_err("build_path corrupt dentry\n");
1437 return ERR_PTR(-EINVAL);
1441 pr_err("build_path did not end path lookup where "
1442 "expected, namelen is %d, pos is %d\n", len, pos);
1443 /* presumably this is only possible if racing with a
1444 rename of one of the parent directories (we can not
1445 lock the dentries above us to prevent this, but
1446 retrying should be harmless) */
1451 *base = ceph_ino(temp->d_inode);
1453 dout("build_path on %p %d built %llx '%.*s'\n",
1454 dentry, atomic_read(&dentry->d_count), *base, len, path);
1458 static int build_dentry_path(struct dentry *dentry,
1459 const char **ppath, int *ppathlen, u64 *pino,
1464 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1465 *pino = ceph_ino(dentry->d_parent->d_inode);
1466 *ppath = dentry->d_name.name;
1467 *ppathlen = dentry->d_name.len;
1470 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1472 return PTR_ERR(path);
1478 static int build_inode_path(struct inode *inode,
1479 const char **ppath, int *ppathlen, u64 *pino,
1482 struct dentry *dentry;
1485 if (ceph_snap(inode) == CEPH_NOSNAP) {
1486 *pino = ceph_ino(inode);
1490 dentry = d_find_alias(inode);
1491 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1494 return PTR_ERR(path);
1501 * request arguments may be specified via an inode *, a dentry *, or
1502 * an explicit ino+path.
1504 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1505 const char *rpath, u64 rino,
1506 const char **ppath, int *pathlen,
1507 u64 *ino, int *freepath)
1512 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1513 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1515 } else if (rdentry) {
1516 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1517 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1522 *pathlen = strlen(rpath);
1523 dout(" path %.*s\n", *pathlen, rpath);
1530 * called under mdsc->mutex
1532 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1533 struct ceph_mds_request *req,
1536 struct ceph_msg *msg;
1537 struct ceph_mds_request_head *head;
1538 const char *path1 = NULL;
1539 const char *path2 = NULL;
1540 u64 ino1 = 0, ino2 = 0;
1541 int pathlen1 = 0, pathlen2 = 0;
1542 int freepath1 = 0, freepath2 = 0;
1548 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1549 req->r_path1, req->r_ino1.ino,
1550 &path1, &pathlen1, &ino1, &freepath1);
1556 ret = set_request_path_attr(NULL, req->r_old_dentry,
1557 req->r_path2, req->r_ino2.ino,
1558 &path2, &pathlen2, &ino2, &freepath2);
1564 len = sizeof(*head) +
1565 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
1567 /* calculate (max) length for cap releases */
1568 len += sizeof(struct ceph_mds_request_release) *
1569 (!!req->r_inode_drop + !!req->r_dentry_drop +
1570 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1571 if (req->r_dentry_drop)
1572 len += req->r_dentry->d_name.len;
1573 if (req->r_old_dentry_drop)
1574 len += req->r_old_dentry->d_name.len;
1576 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS);
1578 msg = ERR_PTR(-ENOMEM);
1582 msg->hdr.tid = cpu_to_le64(req->r_tid);
1584 head = msg->front.iov_base;
1585 p = msg->front.iov_base + sizeof(*head);
1586 end = msg->front.iov_base + msg->front.iov_len;
1588 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1589 head->op = cpu_to_le32(req->r_op);
1590 head->caller_uid = cpu_to_le32(current_fsuid());
1591 head->caller_gid = cpu_to_le32(current_fsgid());
1592 head->args = req->r_args;
1594 ceph_encode_filepath(&p, end, ino1, path1);
1595 ceph_encode_filepath(&p, end, ino2, path2);
1597 /* make note of release offset, in case we need to replay */
1598 req->r_request_release_offset = p - msg->front.iov_base;
1602 if (req->r_inode_drop)
1603 releases += ceph_encode_inode_release(&p,
1604 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1605 mds, req->r_inode_drop, req->r_inode_unless, 0);
1606 if (req->r_dentry_drop)
1607 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1608 mds, req->r_dentry_drop, req->r_dentry_unless);
1609 if (req->r_old_dentry_drop)
1610 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1611 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1612 if (req->r_old_inode_drop)
1613 releases += ceph_encode_inode_release(&p,
1614 req->r_old_dentry->d_inode,
1615 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1616 head->num_releases = cpu_to_le16(releases);
1619 msg->front.iov_len = p - msg->front.iov_base;
1620 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1622 msg->pages = req->r_pages;
1623 msg->nr_pages = req->r_num_pages;
1624 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1625 msg->hdr.data_off = cpu_to_le16(0);
1629 kfree((char *)path2);
1632 kfree((char *)path1);
1638 * called under mdsc->mutex if error, under no mutex if
1641 static void complete_request(struct ceph_mds_client *mdsc,
1642 struct ceph_mds_request *req)
1644 if (req->r_callback)
1645 req->r_callback(mdsc, req);
1647 complete_all(&req->r_completion);
1651 * called under mdsc->mutex
1653 static int __prepare_send_request(struct ceph_mds_client *mdsc,
1654 struct ceph_mds_request *req,
1657 struct ceph_mds_request_head *rhead;
1658 struct ceph_msg *msg;
1664 struct ceph_cap *cap =
1665 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1668 req->r_sent_on_mseq = cap->mseq;
1670 req->r_sent_on_mseq = -1;
1672 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1673 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1675 if (req->r_got_unsafe) {
1677 * Replay. Do not regenerate message (and rebuild
1678 * paths, etc.); just use the original message.
1679 * Rebuilding paths will break for renames because
1680 * d_move mangles the src name.
1682 msg = req->r_request;
1683 rhead = msg->front.iov_base;
1685 flags = le32_to_cpu(rhead->flags);
1686 flags |= CEPH_MDS_FLAG_REPLAY;
1687 rhead->flags = cpu_to_le32(flags);
1689 if (req->r_target_inode)
1690 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1692 rhead->num_retry = req->r_attempts - 1;
1694 /* remove cap/dentry releases from message */
1695 rhead->num_releases = 0;
1696 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1697 msg->front.iov_len = req->r_request_release_offset;
1701 if (req->r_request) {
1702 ceph_msg_put(req->r_request);
1703 req->r_request = NULL;
1705 msg = create_request_message(mdsc, req, mds);
1707 req->r_err = PTR_ERR(msg);
1708 complete_request(mdsc, req);
1709 return PTR_ERR(msg);
1711 req->r_request = msg;
1713 rhead = msg->front.iov_base;
1714 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1715 if (req->r_got_unsafe)
1716 flags |= CEPH_MDS_FLAG_REPLAY;
1717 if (req->r_locked_dir)
1718 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1719 rhead->flags = cpu_to_le32(flags);
1720 rhead->num_fwd = req->r_num_fwd;
1721 rhead->num_retry = req->r_attempts - 1;
1724 dout(" r_locked_dir = %p\n", req->r_locked_dir);
1729 * send request, or put it on the appropriate wait list.
1731 static int __do_request(struct ceph_mds_client *mdsc,
1732 struct ceph_mds_request *req)
1734 struct ceph_mds_session *session = NULL;
1738 if (req->r_err || req->r_got_result)
1741 if (req->r_timeout &&
1742 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1743 dout("do_request timed out\n");
1748 mds = __choose_mds(mdsc, req);
1750 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1751 dout("do_request no mds or not active, waiting for map\n");
1752 list_add(&req->r_wait, &mdsc->waiting_for_map);
1756 /* get, open session */
1757 session = __ceph_lookup_mds_session(mdsc, mds);
1759 session = register_session(mdsc, mds);
1760 if (IS_ERR(session)) {
1761 err = PTR_ERR(session);
1765 dout("do_request mds%d session %p state %s\n", mds, session,
1766 session_state_name(session->s_state));
1767 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1768 session->s_state != CEPH_MDS_SESSION_HUNG) {
1769 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1770 session->s_state == CEPH_MDS_SESSION_CLOSING)
1771 __open_session(mdsc, session);
1772 list_add(&req->r_wait, &session->s_waiting);
1777 req->r_session = get_session(session);
1778 req->r_resend_mds = -1; /* forget any previous mds hint */
1780 if (req->r_request_started == 0) /* note request start time */
1781 req->r_request_started = jiffies;
1783 err = __prepare_send_request(mdsc, req, mds);
1785 ceph_msg_get(req->r_request);
1786 ceph_con_send(&session->s_con, req->r_request);
1790 ceph_put_mds_session(session);
1796 complete_request(mdsc, req);
1801 * called under mdsc->mutex
1803 static void __wake_requests(struct ceph_mds_client *mdsc,
1804 struct list_head *head)
1806 struct ceph_mds_request *req, *nreq;
1808 list_for_each_entry_safe(req, nreq, head, r_wait) {
1809 list_del_init(&req->r_wait);
1810 __do_request(mdsc, req);
1815 * Wake up threads with requests pending for @mds, so that they can
1816 * resubmit their requests to a possibly different mds.
1818 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
1820 struct ceph_mds_request *req;
1823 dout("kick_requests mds%d\n", mds);
1824 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1825 req = rb_entry(p, struct ceph_mds_request, r_node);
1826 if (req->r_got_unsafe)
1828 if (req->r_session &&
1829 req->r_session->s_mds == mds) {
1830 dout(" kicking tid %llu\n", req->r_tid);
1831 put_request_session(req);
1832 __do_request(mdsc, req);
1837 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1838 struct ceph_mds_request *req)
1840 dout("submit_request on %p\n", req);
1841 mutex_lock(&mdsc->mutex);
1842 __register_request(mdsc, req, NULL);
1843 __do_request(mdsc, req);
1844 mutex_unlock(&mdsc->mutex);
1848 * Synchrously perform an mds request. Take care of all of the
1849 * session setup, forwarding, retry details.
1851 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1853 struct ceph_mds_request *req)
1857 dout("do_request on %p\n", req);
1859 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1861 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1862 if (req->r_locked_dir)
1863 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1864 if (req->r_old_dentry)
1866 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1870 mutex_lock(&mdsc->mutex);
1871 __register_request(mdsc, req, dir);
1872 __do_request(mdsc, req);
1876 __unregister_request(mdsc, req);
1877 dout("do_request early error %d\n", err);
1882 mutex_unlock(&mdsc->mutex);
1883 dout("do_request waiting\n");
1884 if (req->r_timeout) {
1885 err = (long)wait_for_completion_killable_timeout(
1886 &req->r_completion, req->r_timeout);
1890 err = wait_for_completion_killable(&req->r_completion);
1892 dout("do_request waited, got %d\n", err);
1893 mutex_lock(&mdsc->mutex);
1895 /* only abort if we didn't race with a real reply */
1896 if (req->r_got_result) {
1897 err = le32_to_cpu(req->r_reply_info.head->result);
1898 } else if (err < 0) {
1899 dout("aborted request %lld with %d\n", req->r_tid, err);
1902 * ensure we aren't running concurrently with
1903 * ceph_fill_trace or ceph_readdir_prepopulate, which
1904 * rely on locks (dir mutex) held by our caller.
1906 mutex_lock(&req->r_fill_mutex);
1908 req->r_aborted = true;
1909 mutex_unlock(&req->r_fill_mutex);
1911 if (req->r_locked_dir &&
1912 (req->r_op & CEPH_MDS_OP_WRITE))
1913 ceph_invalidate_dir_request(req);
1919 mutex_unlock(&mdsc->mutex);
1920 dout("do_request %p done, result %d\n", req, err);
1925 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1926 * namespace request.
1928 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
1930 struct inode *inode = req->r_locked_dir;
1931 struct ceph_inode_info *ci = ceph_inode(inode);
1933 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode);
1934 spin_lock(&inode->i_lock);
1935 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1936 ci->i_release_count++;
1937 spin_unlock(&inode->i_lock);
1940 ceph_invalidate_dentry_lease(req->r_dentry);
1941 if (req->r_old_dentry)
1942 ceph_invalidate_dentry_lease(req->r_old_dentry);
1948 * We take the session mutex and parse and process the reply immediately.
1949 * This preserves the logical ordering of replies, capabilities, etc., sent
1950 * by the MDS as they are applied to our local cache.
1952 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
1954 struct ceph_mds_client *mdsc = session->s_mdsc;
1955 struct ceph_mds_request *req;
1956 struct ceph_mds_reply_head *head = msg->front.iov_base;
1957 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
1960 int mds = session->s_mds;
1962 if (msg->front.iov_len < sizeof(*head)) {
1963 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
1968 /* get request, session */
1969 tid = le64_to_cpu(msg->hdr.tid);
1970 mutex_lock(&mdsc->mutex);
1971 req = __lookup_request(mdsc, tid);
1973 dout("handle_reply on unknown tid %llu\n", tid);
1974 mutex_unlock(&mdsc->mutex);
1977 dout("handle_reply %p\n", req);
1979 /* correct session? */
1980 if (req->r_session != session) {
1981 pr_err("mdsc_handle_reply got %llu on session mds%d"
1982 " not mds%d\n", tid, session->s_mds,
1983 req->r_session ? req->r_session->s_mds : -1);
1984 mutex_unlock(&mdsc->mutex);
1989 if ((req->r_got_unsafe && !head->safe) ||
1990 (req->r_got_safe && head->safe)) {
1991 pr_warning("got a dup %s reply on %llu from mds%d\n",
1992 head->safe ? "safe" : "unsafe", tid, mds);
1993 mutex_unlock(&mdsc->mutex);
1996 if (req->r_got_safe && !head->safe) {
1997 pr_warning("got unsafe after safe on %llu from mds%d\n",
1999 mutex_unlock(&mdsc->mutex);
2003 result = le32_to_cpu(head->result);
2007 * if we're not talking to the authority, send to them
2008 * if the authority has changed while we weren't looking,
2009 * send to new authority
2010 * Otherwise we just have to return an ESTALE
2012 if (result == -ESTALE) {
2013 dout("got ESTALE on request %llu", req->r_tid);
2014 if (!req->r_inode) {
2015 /* do nothing; not an authority problem */
2016 } else if (req->r_direct_mode != USE_AUTH_MDS) {
2017 dout("not using auth, setting for that now");
2018 req->r_direct_mode = USE_AUTH_MDS;
2019 __do_request(mdsc, req);
2020 mutex_unlock(&mdsc->mutex);
2023 struct ceph_inode_info *ci = ceph_inode(req->r_inode);
2024 struct ceph_cap *cap =
2025 ceph_get_cap_for_mds(ci, req->r_mds);;
2027 dout("already using auth");
2028 if ((!cap || cap != ci->i_auth_cap) ||
2029 (cap->mseq != req->r_sent_on_mseq)) {
2030 dout("but cap changed, so resending");
2031 __do_request(mdsc, req);
2032 mutex_unlock(&mdsc->mutex);
2036 dout("have to return ESTALE on request %llu", req->r_tid);
2041 req->r_got_safe = true;
2042 __unregister_request(mdsc, req);
2043 complete_all(&req->r_safe_completion);
2045 if (req->r_got_unsafe) {
2047 * We already handled the unsafe response, now do the
2048 * cleanup. No need to examine the response; the MDS
2049 * doesn't include any result info in the safe
2050 * response. And even if it did, there is nothing
2051 * useful we could do with a revised return value.
2053 dout("got safe reply %llu, mds%d\n", tid, mds);
2054 list_del_init(&req->r_unsafe_item);
2056 /* last unsafe request during umount? */
2057 if (mdsc->stopping && !__get_oldest_req(mdsc))
2058 complete_all(&mdsc->safe_umount_waiters);
2059 mutex_unlock(&mdsc->mutex);
2063 req->r_got_unsafe = true;
2064 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2067 dout("handle_reply tid %lld result %d\n", tid, result);
2068 rinfo = &req->r_reply_info;
2069 err = parse_reply_info(msg, rinfo);
2070 mutex_unlock(&mdsc->mutex);
2072 mutex_lock(&session->s_mutex);
2074 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds);
2080 if (rinfo->snapblob_len) {
2081 down_write(&mdsc->snap_rwsem);
2082 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2083 rinfo->snapblob + rinfo->snapblob_len,
2084 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2085 downgrade_write(&mdsc->snap_rwsem);
2087 down_read(&mdsc->snap_rwsem);
2090 /* insert trace into our cache */
2091 mutex_lock(&req->r_fill_mutex);
2092 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
2094 if (result == 0 && rinfo->dir_nr)
2095 ceph_readdir_prepopulate(req, req->r_session);
2096 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2098 mutex_unlock(&req->r_fill_mutex);
2100 up_read(&mdsc->snap_rwsem);
2102 mutex_lock(&mdsc->mutex);
2103 if (!req->r_aborted) {
2109 req->r_got_result = true;
2112 dout("reply arrived after request %lld was aborted\n", tid);
2114 mutex_unlock(&mdsc->mutex);
2116 ceph_add_cap_releases(mdsc, req->r_session);
2117 mutex_unlock(&session->s_mutex);
2119 /* kick calling process */
2120 complete_request(mdsc, req);
2122 ceph_mdsc_put_request(req);
2129 * handle mds notification that our request has been forwarded.
2131 static void handle_forward(struct ceph_mds_client *mdsc,
2132 struct ceph_mds_session *session,
2133 struct ceph_msg *msg)
2135 struct ceph_mds_request *req;
2136 u64 tid = le64_to_cpu(msg->hdr.tid);
2140 void *p = msg->front.iov_base;
2141 void *end = p + msg->front.iov_len;
2143 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2144 next_mds = ceph_decode_32(&p);
2145 fwd_seq = ceph_decode_32(&p);
2147 mutex_lock(&mdsc->mutex);
2148 req = __lookup_request(mdsc, tid);
2150 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2151 goto out; /* dup reply? */
2154 if (req->r_aborted) {
2155 dout("forward tid %llu aborted, unregistering\n", tid);
2156 __unregister_request(mdsc, req);
2157 } else if (fwd_seq <= req->r_num_fwd) {
2158 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2159 tid, next_mds, req->r_num_fwd, fwd_seq);
2161 /* resend. forward race not possible; mds would drop */
2162 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2164 BUG_ON(req->r_got_result);
2165 req->r_num_fwd = fwd_seq;
2166 req->r_resend_mds = next_mds;
2167 put_request_session(req);
2168 __do_request(mdsc, req);
2170 ceph_mdsc_put_request(req);
2172 mutex_unlock(&mdsc->mutex);
2176 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2180 * handle a mds session control message
2182 static void handle_session(struct ceph_mds_session *session,
2183 struct ceph_msg *msg)
2185 struct ceph_mds_client *mdsc = session->s_mdsc;
2188 int mds = session->s_mds;
2189 struct ceph_mds_session_head *h = msg->front.iov_base;
2193 if (msg->front.iov_len != sizeof(*h))
2195 op = le32_to_cpu(h->op);
2196 seq = le64_to_cpu(h->seq);
2198 mutex_lock(&mdsc->mutex);
2199 if (op == CEPH_SESSION_CLOSE)
2200 __unregister_session(mdsc, session);
2201 /* FIXME: this ttl calculation is generous */
2202 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2203 mutex_unlock(&mdsc->mutex);
2205 mutex_lock(&session->s_mutex);
2207 dout("handle_session mds%d %s %p state %s seq %llu\n",
2208 mds, ceph_session_op_name(op), session,
2209 session_state_name(session->s_state), seq);
2211 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2212 session->s_state = CEPH_MDS_SESSION_OPEN;
2213 pr_info("mds%d came back\n", session->s_mds);
2217 case CEPH_SESSION_OPEN:
2218 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2219 pr_info("mds%d reconnect success\n", session->s_mds);
2220 session->s_state = CEPH_MDS_SESSION_OPEN;
2221 renewed_caps(mdsc, session, 0);
2224 __close_session(mdsc, session);
2227 case CEPH_SESSION_RENEWCAPS:
2228 if (session->s_renew_seq == seq)
2229 renewed_caps(mdsc, session, 1);
2232 case CEPH_SESSION_CLOSE:
2233 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2234 pr_info("mds%d reconnect denied\n", session->s_mds);
2235 remove_session_caps(session);
2236 wake = 1; /* for good measure */
2237 wake_up_all(&mdsc->session_close_wq);
2238 kick_requests(mdsc, mds);
2241 case CEPH_SESSION_STALE:
2242 pr_info("mds%d caps went stale, renewing\n",
2244 spin_lock(&session->s_cap_lock);
2245 session->s_cap_gen++;
2246 session->s_cap_ttl = 0;
2247 spin_unlock(&session->s_cap_lock);
2248 send_renew_caps(mdsc, session);
2251 case CEPH_SESSION_RECALL_STATE:
2252 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2256 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2260 mutex_unlock(&session->s_mutex);
2262 mutex_lock(&mdsc->mutex);
2263 __wake_requests(mdsc, &session->s_waiting);
2264 mutex_unlock(&mdsc->mutex);
2269 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2270 (int)msg->front.iov_len);
2277 * called under session->mutex.
2279 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2280 struct ceph_mds_session *session)
2282 struct ceph_mds_request *req, *nreq;
2285 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2287 mutex_lock(&mdsc->mutex);
2288 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2289 err = __prepare_send_request(mdsc, req, session->s_mds);
2291 ceph_msg_get(req->r_request);
2292 ceph_con_send(&session->s_con, req->r_request);
2295 mutex_unlock(&mdsc->mutex);
2299 * Encode information about a cap for a reconnect with the MDS.
2301 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2305 struct ceph_mds_cap_reconnect v2;
2306 struct ceph_mds_cap_reconnect_v1 v1;
2309 struct ceph_inode_info *ci;
2310 struct ceph_reconnect_state *recon_state = arg;
2311 struct ceph_pagelist *pagelist = recon_state->pagelist;
2315 struct dentry *dentry;
2319 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2320 inode, ceph_vinop(inode), cap, cap->cap_id,
2321 ceph_cap_string(cap->issued));
2322 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2326 dentry = d_find_alias(inode);
2328 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2330 err = PTR_ERR(path);
2337 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2341 spin_lock(&inode->i_lock);
2342 cap->seq = 0; /* reset cap seq */
2343 cap->issue_seq = 0; /* and issue_seq */
2345 if (recon_state->flock) {
2346 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2347 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2348 rec.v2.issued = cpu_to_le32(cap->issued);
2349 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2350 rec.v2.pathbase = cpu_to_le64(pathbase);
2351 rec.v2.flock_len = 0;
2352 reclen = sizeof(rec.v2);
2354 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2355 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2356 rec.v1.issued = cpu_to_le32(cap->issued);
2357 rec.v1.size = cpu_to_le64(inode->i_size);
2358 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2359 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2360 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2361 rec.v1.pathbase = cpu_to_le64(pathbase);
2362 reclen = sizeof(rec.v1);
2364 spin_unlock(&inode->i_lock);
2366 if (recon_state->flock) {
2367 int num_fcntl_locks, num_flock_locks;
2370 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
2371 rec.v2.flock_len = (2*sizeof(u32) +
2372 (num_fcntl_locks+num_flock_locks) *
2373 sizeof(struct ceph_filelock));
2375 err = ceph_pagelist_append(pagelist, &rec, reclen);
2377 err = ceph_encode_locks(inode, pagelist,
2382 err = ceph_pagelist_append(pagelist, &rec, reclen);
2394 * If an MDS fails and recovers, clients need to reconnect in order to
2395 * reestablish shared state. This includes all caps issued through
2396 * this session _and_ the snap_realm hierarchy. Because it's not
2397 * clear which snap realms the mds cares about, we send everything we
2398 * know about.. that ensures we'll then get any new info the
2399 * recovering MDS might have.
2401 * This is a relatively heavyweight operation, but it's rare.
2403 * called with mdsc->mutex held.
2405 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2406 struct ceph_mds_session *session)
2408 struct ceph_msg *reply;
2410 int mds = session->s_mds;
2412 struct ceph_pagelist *pagelist;
2413 struct ceph_reconnect_state recon_state;
2415 pr_info("mds%d reconnect start\n", mds);
2417 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2419 goto fail_nopagelist;
2420 ceph_pagelist_init(pagelist);
2422 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS);
2426 mutex_lock(&session->s_mutex);
2427 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2430 ceph_con_open(&session->s_con,
2431 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2433 /* replay unsafe requests */
2434 replay_unsafe_requests(mdsc, session);
2436 down_read(&mdsc->snap_rwsem);
2438 dout("session %p state %s\n", session,
2439 session_state_name(session->s_state));
2441 /* drop old cap expires; we're about to reestablish that state */
2442 discard_cap_releases(mdsc, session);
2444 /* traverse this session's caps */
2445 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2449 recon_state.pagelist = pagelist;
2450 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2451 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
2456 * snaprealms. we provide mds with the ino, seq (version), and
2457 * parent for all of our realms. If the mds has any newer info,
2460 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2461 struct ceph_snap_realm *realm =
2462 rb_entry(p, struct ceph_snap_realm, node);
2463 struct ceph_mds_snaprealm_reconnect sr_rec;
2465 dout(" adding snap realm %llx seq %lld parent %llx\n",
2466 realm->ino, realm->seq, realm->parent_ino);
2467 sr_rec.ino = cpu_to_le64(realm->ino);
2468 sr_rec.seq = cpu_to_le64(realm->seq);
2469 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2470 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2475 reply->pagelist = pagelist;
2476 if (recon_state.flock)
2477 reply->hdr.version = cpu_to_le16(2);
2478 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2479 reply->nr_pages = calc_pages_for(0, pagelist->length);
2480 ceph_con_send(&session->s_con, reply);
2482 mutex_unlock(&session->s_mutex);
2484 mutex_lock(&mdsc->mutex);
2485 __wake_requests(mdsc, &session->s_waiting);
2486 mutex_unlock(&mdsc->mutex);
2488 up_read(&mdsc->snap_rwsem);
2492 ceph_msg_put(reply);
2493 up_read(&mdsc->snap_rwsem);
2494 mutex_unlock(&session->s_mutex);
2496 ceph_pagelist_release(pagelist);
2499 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
2505 * compare old and new mdsmaps, kicking requests
2506 * and closing out old connections as necessary
2508 * called under mdsc->mutex.
2510 static void check_new_map(struct ceph_mds_client *mdsc,
2511 struct ceph_mdsmap *newmap,
2512 struct ceph_mdsmap *oldmap)
2515 int oldstate, newstate;
2516 struct ceph_mds_session *s;
2518 dout("check_new_map new %u old %u\n",
2519 newmap->m_epoch, oldmap->m_epoch);
2521 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2522 if (mdsc->sessions[i] == NULL)
2524 s = mdsc->sessions[i];
2525 oldstate = ceph_mdsmap_get_state(oldmap, i);
2526 newstate = ceph_mdsmap_get_state(newmap, i);
2528 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2529 i, ceph_mds_state_name(oldstate),
2530 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
2531 ceph_mds_state_name(newstate),
2532 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
2533 session_state_name(s->s_state));
2535 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2536 ceph_mdsmap_get_addr(newmap, i),
2537 sizeof(struct ceph_entity_addr))) {
2538 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2539 /* the session never opened, just close it
2541 __wake_requests(mdsc, &s->s_waiting);
2542 __unregister_session(mdsc, s);
2545 mutex_unlock(&mdsc->mutex);
2546 mutex_lock(&s->s_mutex);
2547 mutex_lock(&mdsc->mutex);
2548 ceph_con_close(&s->s_con);
2549 mutex_unlock(&s->s_mutex);
2550 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2553 /* kick any requests waiting on the recovering mds */
2554 kick_requests(mdsc, i);
2555 } else if (oldstate == newstate) {
2556 continue; /* nothing new with this mds */
2562 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
2563 newstate >= CEPH_MDS_STATE_RECONNECT) {
2564 mutex_unlock(&mdsc->mutex);
2565 send_mds_reconnect(mdsc, s);
2566 mutex_lock(&mdsc->mutex);
2570 * kick request on any mds that has gone active.
2572 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2573 newstate >= CEPH_MDS_STATE_ACTIVE) {
2574 if (oldstate != CEPH_MDS_STATE_CREATING &&
2575 oldstate != CEPH_MDS_STATE_STARTING)
2576 pr_info("mds%d recovery completed\n", s->s_mds);
2577 kick_requests(mdsc, i);
2578 ceph_kick_flushing_caps(mdsc, s);
2579 wake_up_session_caps(s, 1);
2583 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2584 s = mdsc->sessions[i];
2587 if (!ceph_mdsmap_is_laggy(newmap, i))
2589 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2590 s->s_state == CEPH_MDS_SESSION_HUNG ||
2591 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2592 dout(" connecting to export targets of laggy mds%d\n",
2594 __open_export_target_sessions(mdsc, s);
2606 * caller must hold session s_mutex, dentry->d_lock
2608 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2610 struct ceph_dentry_info *di = ceph_dentry(dentry);
2612 ceph_put_mds_session(di->lease_session);
2613 di->lease_session = NULL;
2616 static void handle_lease(struct ceph_mds_client *mdsc,
2617 struct ceph_mds_session *session,
2618 struct ceph_msg *msg)
2620 struct super_block *sb = mdsc->fsc->sb;
2621 struct inode *inode;
2622 struct ceph_inode_info *ci;
2623 struct dentry *parent, *dentry;
2624 struct ceph_dentry_info *di;
2625 int mds = session->s_mds;
2626 struct ceph_mds_lease *h = msg->front.iov_base;
2628 struct ceph_vino vino;
2633 dout("handle_lease from mds%d\n", mds);
2636 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2638 vino.ino = le64_to_cpu(h->ino);
2639 vino.snap = CEPH_NOSNAP;
2640 mask = le16_to_cpu(h->mask);
2641 seq = le32_to_cpu(h->seq);
2642 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2643 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2644 if (dname.len != get_unaligned_le32(h+1))
2647 mutex_lock(&session->s_mutex);
2651 inode = ceph_find_inode(sb, vino);
2652 dout("handle_lease %s, mask %d, ino %llx %p %.*s\n",
2653 ceph_lease_op_name(h->action), mask, vino.ino, inode,
2654 dname.len, dname.name);
2655 if (inode == NULL) {
2656 dout("handle_lease no inode %llx\n", vino.ino);
2659 ci = ceph_inode(inode);
2662 parent = d_find_alias(inode);
2664 dout("no parent dentry on inode %p\n", inode);
2666 goto release; /* hrm... */
2668 dname.hash = full_name_hash(dname.name, dname.len);
2669 dentry = d_lookup(parent, &dname);
2674 spin_lock(&dentry->d_lock);
2675 di = ceph_dentry(dentry);
2676 switch (h->action) {
2677 case CEPH_MDS_LEASE_REVOKE:
2678 if (di && di->lease_session == session) {
2679 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2680 h->seq = cpu_to_le32(di->lease_seq);
2681 __ceph_mdsc_drop_dentry_lease(dentry);
2686 case CEPH_MDS_LEASE_RENEW:
2687 if (di && di->lease_session == session &&
2688 di->lease_gen == session->s_cap_gen &&
2689 di->lease_renew_from &&
2690 di->lease_renew_after == 0) {
2691 unsigned long duration =
2692 le32_to_cpu(h->duration_ms) * HZ / 1000;
2694 di->lease_seq = seq;
2695 dentry->d_time = di->lease_renew_from + duration;
2696 di->lease_renew_after = di->lease_renew_from +
2698 di->lease_renew_from = 0;
2702 spin_unlock(&dentry->d_lock);
2709 /* let's just reuse the same message */
2710 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2712 ceph_con_send(&session->s_con, msg);
2716 mutex_unlock(&session->s_mutex);
2720 pr_err("corrupt lease message\n");
2724 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2725 struct inode *inode,
2726 struct dentry *dentry, char action,
2729 struct ceph_msg *msg;
2730 struct ceph_mds_lease *lease;
2731 int len = sizeof(*lease) + sizeof(u32);
2734 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2735 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2736 dnamelen = dentry->d_name.len;
2739 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS);
2742 lease = msg->front.iov_base;
2743 lease->action = action;
2744 lease->mask = cpu_to_le16(1);
2745 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2746 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2747 lease->seq = cpu_to_le32(seq);
2748 put_unaligned_le32(dnamelen, lease + 1);
2749 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2752 * if this is a preemptive lease RELEASE, no need to
2753 * flush request stream, since the actual request will
2756 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2758 ceph_con_send(&session->s_con, msg);
2762 * Preemptively release a lease we expect to invalidate anyway.
2763 * Pass @inode always, @dentry is optional.
2765 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2766 struct dentry *dentry, int mask)
2768 struct ceph_dentry_info *di;
2769 struct ceph_mds_session *session;
2772 BUG_ON(inode == NULL);
2773 BUG_ON(dentry == NULL);
2776 /* is dentry lease valid? */
2777 spin_lock(&dentry->d_lock);
2778 di = ceph_dentry(dentry);
2779 if (!di || !di->lease_session ||
2780 di->lease_session->s_mds < 0 ||
2781 di->lease_gen != di->lease_session->s_cap_gen ||
2782 !time_before(jiffies, dentry->d_time)) {
2783 dout("lease_release inode %p dentry %p -- "
2785 inode, dentry, mask);
2786 spin_unlock(&dentry->d_lock);
2790 /* we do have a lease on this dentry; note mds and seq */
2791 session = ceph_get_mds_session(di->lease_session);
2792 seq = di->lease_seq;
2793 __ceph_mdsc_drop_dentry_lease(dentry);
2794 spin_unlock(&dentry->d_lock);
2796 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2797 inode, dentry, mask, session->s_mds);
2798 ceph_mdsc_lease_send_msg(session, inode, dentry,
2799 CEPH_MDS_LEASE_RELEASE, seq);
2800 ceph_put_mds_session(session);
2804 * drop all leases (and dentry refs) in preparation for umount
2806 static void drop_leases(struct ceph_mds_client *mdsc)
2810 dout("drop_leases\n");
2811 mutex_lock(&mdsc->mutex);
2812 for (i = 0; i < mdsc->max_sessions; i++) {
2813 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2816 mutex_unlock(&mdsc->mutex);
2817 mutex_lock(&s->s_mutex);
2818 mutex_unlock(&s->s_mutex);
2819 ceph_put_mds_session(s);
2820 mutex_lock(&mdsc->mutex);
2822 mutex_unlock(&mdsc->mutex);
2828 * delayed work -- periodically trim expired leases, renew caps with mds
2830 static void schedule_delayed(struct ceph_mds_client *mdsc)
2833 unsigned hz = round_jiffies_relative(HZ * delay);
2834 schedule_delayed_work(&mdsc->delayed_work, hz);
2837 static void delayed_work(struct work_struct *work)
2840 struct ceph_mds_client *mdsc =
2841 container_of(work, struct ceph_mds_client, delayed_work.work);
2845 dout("mdsc delayed_work\n");
2846 ceph_check_delayed_caps(mdsc);
2848 mutex_lock(&mdsc->mutex);
2849 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2850 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2851 mdsc->last_renew_caps);
2853 mdsc->last_renew_caps = jiffies;
2855 for (i = 0; i < mdsc->max_sessions; i++) {
2856 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2859 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2860 dout("resending session close request for mds%d\n",
2862 request_close_session(mdsc, s);
2863 ceph_put_mds_session(s);
2866 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2867 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2868 s->s_state = CEPH_MDS_SESSION_HUNG;
2869 pr_info("mds%d hung\n", s->s_mds);
2872 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2873 /* this mds is failed or recovering, just wait */
2874 ceph_put_mds_session(s);
2877 mutex_unlock(&mdsc->mutex);
2879 mutex_lock(&s->s_mutex);
2881 send_renew_caps(mdsc, s);
2883 ceph_con_keepalive(&s->s_con);
2884 ceph_add_cap_releases(mdsc, s);
2885 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2886 s->s_state == CEPH_MDS_SESSION_HUNG)
2887 ceph_send_cap_releases(mdsc, s);
2888 mutex_unlock(&s->s_mutex);
2889 ceph_put_mds_session(s);
2891 mutex_lock(&mdsc->mutex);
2893 mutex_unlock(&mdsc->mutex);
2895 schedule_delayed(mdsc);
2898 int ceph_mdsc_init(struct ceph_fs_client *fsc)
2901 struct ceph_mds_client *mdsc;
2903 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
2908 mutex_init(&mdsc->mutex);
2909 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
2910 if (mdsc->mdsmap == NULL)
2913 init_completion(&mdsc->safe_umount_waiters);
2914 init_waitqueue_head(&mdsc->session_close_wq);
2915 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2916 mdsc->sessions = NULL;
2917 mdsc->max_sessions = 0;
2919 init_rwsem(&mdsc->snap_rwsem);
2920 mdsc->snap_realms = RB_ROOT;
2921 INIT_LIST_HEAD(&mdsc->snap_empty);
2922 spin_lock_init(&mdsc->snap_empty_lock);
2924 mdsc->request_tree = RB_ROOT;
2925 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2926 mdsc->last_renew_caps = jiffies;
2927 INIT_LIST_HEAD(&mdsc->cap_delay_list);
2928 spin_lock_init(&mdsc->cap_delay_lock);
2929 INIT_LIST_HEAD(&mdsc->snap_flush_list);
2930 spin_lock_init(&mdsc->snap_flush_lock);
2931 mdsc->cap_flush_seq = 0;
2932 INIT_LIST_HEAD(&mdsc->cap_dirty);
2933 mdsc->num_cap_flushing = 0;
2934 spin_lock_init(&mdsc->cap_dirty_lock);
2935 init_waitqueue_head(&mdsc->cap_flushing_wq);
2936 spin_lock_init(&mdsc->dentry_lru_lock);
2937 INIT_LIST_HEAD(&mdsc->dentry_lru);
2939 ceph_caps_init(mdsc);
2940 ceph_adjust_min_caps(mdsc, fsc->min_caps);
2946 * Wait for safe replies on open mds requests. If we time out, drop
2947 * all requests from the tree to avoid dangling dentry refs.
2949 static void wait_requests(struct ceph_mds_client *mdsc)
2951 struct ceph_mds_request *req;
2952 struct ceph_fs_client *fsc = mdsc->fsc;
2954 mutex_lock(&mdsc->mutex);
2955 if (__get_oldest_req(mdsc)) {
2956 mutex_unlock(&mdsc->mutex);
2958 dout("wait_requests waiting for requests\n");
2959 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
2960 fsc->client->options->mount_timeout * HZ);
2962 /* tear down remaining requests */
2963 mutex_lock(&mdsc->mutex);
2964 while ((req = __get_oldest_req(mdsc))) {
2965 dout("wait_requests timed out on tid %llu\n",
2967 __unregister_request(mdsc, req);
2970 mutex_unlock(&mdsc->mutex);
2971 dout("wait_requests done\n");
2975 * called before mount is ro, and before dentries are torn down.
2976 * (hmm, does this still race with new lookups?)
2978 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
2980 dout("pre_umount\n");
2984 ceph_flush_dirty_caps(mdsc);
2985 wait_requests(mdsc);
2988 * wait for reply handlers to drop their request refs and
2989 * their inode/dcache refs
2995 * wait for all write mds requests to flush.
2997 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
2999 struct ceph_mds_request *req = NULL, *nextreq;
3002 mutex_lock(&mdsc->mutex);
3003 dout("wait_unsafe_requests want %lld\n", want_tid);
3005 req = __get_oldest_req(mdsc);
3006 while (req && req->r_tid <= want_tid) {
3007 /* find next request */
3008 n = rb_next(&req->r_node);
3010 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3013 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3015 ceph_mdsc_get_request(req);
3017 ceph_mdsc_get_request(nextreq);
3018 mutex_unlock(&mdsc->mutex);
3019 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3020 req->r_tid, want_tid);
3021 wait_for_completion(&req->r_safe_completion);
3022 mutex_lock(&mdsc->mutex);
3023 ceph_mdsc_put_request(req);
3025 break; /* next dne before, so we're done! */
3026 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3027 /* next request was removed from tree */
3028 ceph_mdsc_put_request(nextreq);
3031 ceph_mdsc_put_request(nextreq); /* won't go away */
3035 mutex_unlock(&mdsc->mutex);
3036 dout("wait_unsafe_requests done\n");
3039 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3041 u64 want_tid, want_flush;
3043 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3047 mutex_lock(&mdsc->mutex);
3048 want_tid = mdsc->last_tid;
3049 want_flush = mdsc->cap_flush_seq;
3050 mutex_unlock(&mdsc->mutex);
3051 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3053 ceph_flush_dirty_caps(mdsc);
3055 wait_unsafe_requests(mdsc, want_tid);
3056 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3060 * true if all sessions are closed, or we force unmount
3062 bool done_closing_sessions(struct ceph_mds_client *mdsc)
3066 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3069 mutex_lock(&mdsc->mutex);
3070 for (i = 0; i < mdsc->max_sessions; i++)
3071 if (mdsc->sessions[i])
3073 mutex_unlock(&mdsc->mutex);
3078 * called after sb is ro.
3080 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3082 struct ceph_mds_session *session;
3084 struct ceph_fs_client *fsc = mdsc->fsc;
3085 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
3087 dout("close_sessions\n");
3089 /* close sessions */
3090 mutex_lock(&mdsc->mutex);
3091 for (i = 0; i < mdsc->max_sessions; i++) {
3092 session = __ceph_lookup_mds_session(mdsc, i);
3095 mutex_unlock(&mdsc->mutex);
3096 mutex_lock(&session->s_mutex);
3097 __close_session(mdsc, session);
3098 mutex_unlock(&session->s_mutex);
3099 ceph_put_mds_session(session);
3100 mutex_lock(&mdsc->mutex);
3102 mutex_unlock(&mdsc->mutex);
3104 dout("waiting for sessions to close\n");
3105 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3108 /* tear down remaining sessions */
3109 mutex_lock(&mdsc->mutex);
3110 for (i = 0; i < mdsc->max_sessions; i++) {
3111 if (mdsc->sessions[i]) {
3112 session = get_session(mdsc->sessions[i]);
3113 __unregister_session(mdsc, session);
3114 mutex_unlock(&mdsc->mutex);
3115 mutex_lock(&session->s_mutex);
3116 remove_session_caps(session);
3117 mutex_unlock(&session->s_mutex);
3118 ceph_put_mds_session(session);
3119 mutex_lock(&mdsc->mutex);
3122 WARN_ON(!list_empty(&mdsc->cap_delay_list));
3123 mutex_unlock(&mdsc->mutex);
3125 ceph_cleanup_empty_realms(mdsc);
3127 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3132 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
3135 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3137 ceph_mdsmap_destroy(mdsc->mdsmap);
3138 kfree(mdsc->sessions);
3139 ceph_caps_finalize(mdsc);
3142 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3144 struct ceph_mds_client *mdsc = fsc->mdsc;
3146 ceph_mdsc_stop(mdsc);
3153 * handle mds map update.
3155 void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3159 void *p = msg->front.iov_base;
3160 void *end = p + msg->front.iov_len;
3161 struct ceph_mdsmap *newmap, *oldmap;
3162 struct ceph_fsid fsid;
3165 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3166 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3167 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
3169 epoch = ceph_decode_32(&p);
3170 maplen = ceph_decode_32(&p);
3171 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3173 /* do we need it? */
3174 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
3175 mutex_lock(&mdsc->mutex);
3176 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3177 dout("handle_map epoch %u <= our %u\n",
3178 epoch, mdsc->mdsmap->m_epoch);
3179 mutex_unlock(&mdsc->mutex);
3183 newmap = ceph_mdsmap_decode(&p, end);
3184 if (IS_ERR(newmap)) {
3185 err = PTR_ERR(newmap);
3189 /* swap into place */
3191 oldmap = mdsc->mdsmap;
3192 mdsc->mdsmap = newmap;
3193 check_new_map(mdsc, newmap, oldmap);
3194 ceph_mdsmap_destroy(oldmap);
3196 mdsc->mdsmap = newmap; /* first mds map */
3198 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
3200 __wake_requests(mdsc, &mdsc->waiting_for_map);
3202 mutex_unlock(&mdsc->mutex);
3203 schedule_delayed(mdsc);
3207 mutex_unlock(&mdsc->mutex);
3209 pr_err("error decoding mdsmap %d\n", err);
3213 static struct ceph_connection *con_get(struct ceph_connection *con)
3215 struct ceph_mds_session *s = con->private;
3217 if (get_session(s)) {
3218 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
3221 dout("mdsc con_get %p FAIL\n", s);
3225 static void con_put(struct ceph_connection *con)
3227 struct ceph_mds_session *s = con->private;
3229 ceph_put_mds_session(s);
3230 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
3234 * if the client is unresponsive for long enough, the mds will kill
3235 * the session entirely.
3237 static void peer_reset(struct ceph_connection *con)
3239 struct ceph_mds_session *s = con->private;
3240 struct ceph_mds_client *mdsc = s->s_mdsc;
3242 pr_warning("mds%d closed our session\n", s->s_mds);
3243 send_mds_reconnect(mdsc, s);
3246 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3248 struct ceph_mds_session *s = con->private;
3249 struct ceph_mds_client *mdsc = s->s_mdsc;
3250 int type = le16_to_cpu(msg->hdr.type);
3252 mutex_lock(&mdsc->mutex);
3253 if (__verify_registered_session(mdsc, s) < 0) {
3254 mutex_unlock(&mdsc->mutex);
3257 mutex_unlock(&mdsc->mutex);
3260 case CEPH_MSG_MDS_MAP:
3261 ceph_mdsc_handle_map(mdsc, msg);
3263 case CEPH_MSG_CLIENT_SESSION:
3264 handle_session(s, msg);
3266 case CEPH_MSG_CLIENT_REPLY:
3267 handle_reply(s, msg);
3269 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
3270 handle_forward(mdsc, s, msg);
3272 case CEPH_MSG_CLIENT_CAPS:
3273 ceph_handle_caps(s, msg);
3275 case CEPH_MSG_CLIENT_SNAP:
3276 ceph_handle_snap(mdsc, s, msg);
3278 case CEPH_MSG_CLIENT_LEASE:
3279 handle_lease(mdsc, s, msg);
3283 pr_err("received unknown message type %d %s\n", type,
3284 ceph_msg_type_name(type));
3293 static int get_authorizer(struct ceph_connection *con,
3294 void **buf, int *len, int *proto,
3295 void **reply_buf, int *reply_len, int force_new)
3297 struct ceph_mds_session *s = con->private;
3298 struct ceph_mds_client *mdsc = s->s_mdsc;
3299 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3302 if (force_new && s->s_authorizer) {
3303 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3304 s->s_authorizer = NULL;
3306 if (s->s_authorizer == NULL) {
3307 if (ac->ops->create_authorizer) {
3308 ret = ac->ops->create_authorizer(
3309 ac, CEPH_ENTITY_TYPE_MDS,
3311 &s->s_authorizer_buf,
3312 &s->s_authorizer_buf_len,
3313 &s->s_authorizer_reply_buf,
3314 &s->s_authorizer_reply_buf_len);
3320 *proto = ac->protocol;
3321 *buf = s->s_authorizer_buf;
3322 *len = s->s_authorizer_buf_len;
3323 *reply_buf = s->s_authorizer_reply_buf;
3324 *reply_len = s->s_authorizer_reply_buf_len;
3329 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3331 struct ceph_mds_session *s = con->private;
3332 struct ceph_mds_client *mdsc = s->s_mdsc;
3333 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3335 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3338 static int invalidate_authorizer(struct ceph_connection *con)
3340 struct ceph_mds_session *s = con->private;
3341 struct ceph_mds_client *mdsc = s->s_mdsc;
3342 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3344 if (ac->ops->invalidate_authorizer)
3345 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3347 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
3350 static const struct ceph_connection_operations mds_con_ops = {
3353 .dispatch = dispatch,
3354 .get_authorizer = get_authorizer,
3355 .verify_authorizer_reply = verify_authorizer_reply,
3356 .invalidate_authorizer = invalidate_authorizer,
3357 .peer_reset = peer_reset,