1 #include <linux/ceph/ceph_debug.h>
4 #include <linux/wait.h>
5 #include <linux/slab.h>
6 #include <linux/sched.h>
7 #include <linux/debugfs.h>
8 #include <linux/seq_file.h>
11 #include "mds_client.h"
13 #include <linux/ceph/ceph_features.h>
14 #include <linux/ceph/messenger.h>
15 #include <linux/ceph/decode.h>
16 #include <linux/ceph/pagelist.h>
17 #include <linux/ceph/auth.h>
18 #include <linux/ceph/debugfs.h>
21 * A cluster of MDS (metadata server) daemons is responsible for
22 * managing the file system namespace (the directory hierarchy and
23 * inodes) and for coordinating shared access to storage. Metadata is
24 * partitioning hierarchically across a number of servers, and that
25 * partition varies over time as the cluster adjusts the distribution
26 * in order to balance load.
28 * The MDS client is primarily responsible to managing synchronous
29 * metadata requests for operations like open, unlink, and so forth.
30 * If there is a MDS failure, we find out about it when we (possibly
31 * request and) receive a new MDS map, and can resubmit affected
34 * For the most part, though, we take advantage of a lossless
35 * communications channel to the MDS, and do not need to worry about
36 * timing out or resubmitting requests.
38 * We maintain a stateful "session" with each MDS we interact with.
39 * Within each session, we sent periodic heartbeat messages to ensure
40 * any capabilities or leases we have been issues remain valid. If
41 * the session times out and goes stale, our leases and capabilities
42 * are no longer valid.
45 struct ceph_reconnect_state {
46 struct ceph_pagelist *pagelist;
50 static void __wake_requests(struct ceph_mds_client *mdsc,
51 struct list_head *head);
53 static const struct ceph_connection_operations mds_con_ops;
61 * parse individual inode info
63 static int parse_reply_info_in(void **p, void *end,
64 struct ceph_mds_reply_info_in *info,
70 *p += sizeof(struct ceph_mds_reply_inode) +
71 sizeof(*info->in->fragtree.splits) *
72 le32_to_cpu(info->in->fragtree.nsplits);
74 ceph_decode_32_safe(p, end, info->symlink_len, bad);
75 ceph_decode_need(p, end, info->symlink_len, bad);
77 *p += info->symlink_len;
79 if (features & CEPH_FEATURE_DIRLAYOUTHASH)
80 ceph_decode_copy_safe(p, end, &info->dir_layout,
81 sizeof(info->dir_layout), bad);
83 memset(&info->dir_layout, 0, sizeof(info->dir_layout));
85 ceph_decode_32_safe(p, end, info->xattr_len, bad);
86 ceph_decode_need(p, end, info->xattr_len, bad);
87 info->xattr_data = *p;
88 *p += info->xattr_len;
95 * parse a normal reply, which may contain a (dir+)dentry and/or a
98 static int parse_reply_info_trace(void **p, void *end,
99 struct ceph_mds_reply_info_parsed *info,
104 if (info->head->is_dentry) {
105 err = parse_reply_info_in(p, end, &info->diri, features);
109 if (unlikely(*p + sizeof(*info->dirfrag) > end))
112 *p += sizeof(*info->dirfrag) +
113 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
114 if (unlikely(*p > end))
117 ceph_decode_32_safe(p, end, info->dname_len, bad);
118 ceph_decode_need(p, end, info->dname_len, bad);
120 *p += info->dname_len;
122 *p += sizeof(*info->dlease);
125 if (info->head->is_target) {
126 err = parse_reply_info_in(p, end, &info->targeti, features);
131 if (unlikely(*p != end))
138 pr_err("problem parsing mds trace %d\n", err);
143 * parse readdir results
145 static int parse_reply_info_dir(void **p, void *end,
146 struct ceph_mds_reply_info_parsed *info,
153 if (*p + sizeof(*info->dir_dir) > end)
155 *p += sizeof(*info->dir_dir) +
156 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
160 ceph_decode_need(p, end, sizeof(num) + 2, bad);
161 num = ceph_decode_32(p);
162 info->dir_end = ceph_decode_8(p);
163 info->dir_complete = ceph_decode_8(p);
167 /* alloc large array */
169 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
170 sizeof(*info->dir_dname) +
171 sizeof(*info->dir_dname_len) +
172 sizeof(*info->dir_dlease),
174 if (info->dir_in == NULL) {
178 info->dir_dname = (void *)(info->dir_in + num);
179 info->dir_dname_len = (void *)(info->dir_dname + num);
180 info->dir_dlease = (void *)(info->dir_dname_len + num);
184 ceph_decode_need(p, end, sizeof(u32)*2, bad);
185 info->dir_dname_len[i] = ceph_decode_32(p);
186 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
187 info->dir_dname[i] = *p;
188 *p += info->dir_dname_len[i];
189 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
191 info->dir_dlease[i] = *p;
192 *p += sizeof(struct ceph_mds_reply_lease);
195 err = parse_reply_info_in(p, end, &info->dir_in[i], features);
210 pr_err("problem parsing dir contents %d\n", err);
215 * parse fcntl F_GETLK results
217 static int parse_reply_info_filelock(void **p, void *end,
218 struct ceph_mds_reply_info_parsed *info,
221 if (*p + sizeof(*info->filelock_reply) > end)
224 info->filelock_reply = *p;
225 *p += sizeof(*info->filelock_reply);
227 if (unlikely(*p != end))
236 * parse create results
238 static int parse_reply_info_create(void **p, void *end,
239 struct ceph_mds_reply_info_parsed *info,
242 if (features & CEPH_FEATURE_REPLY_CREATE_INODE) {
244 info->has_create_ino = false;
246 info->has_create_ino = true;
247 info->ino = ceph_decode_64(p);
251 if (unlikely(*p != end))
260 * parse extra results
262 static int parse_reply_info_extra(void **p, void *end,
263 struct ceph_mds_reply_info_parsed *info,
266 if (info->head->op == CEPH_MDS_OP_GETFILELOCK)
267 return parse_reply_info_filelock(p, end, info, features);
268 else if (info->head->op == CEPH_MDS_OP_READDIR ||
269 info->head->op == CEPH_MDS_OP_LSSNAP)
270 return parse_reply_info_dir(p, end, info, features);
271 else if (info->head->op == CEPH_MDS_OP_CREATE)
272 return parse_reply_info_create(p, end, info, features);
278 * parse entire mds reply
280 static int parse_reply_info(struct ceph_msg *msg,
281 struct ceph_mds_reply_info_parsed *info,
288 info->head = msg->front.iov_base;
289 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
290 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
293 ceph_decode_32_safe(&p, end, len, bad);
295 ceph_decode_need(&p, end, len, bad);
296 err = parse_reply_info_trace(&p, p+len, info, features);
302 ceph_decode_32_safe(&p, end, len, bad);
304 ceph_decode_need(&p, end, len, bad);
305 err = parse_reply_info_extra(&p, p+len, info, features);
311 ceph_decode_32_safe(&p, end, len, bad);
312 info->snapblob_len = len;
323 pr_err("mds parse_reply err %d\n", err);
327 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
336 static const char *session_state_name(int s)
339 case CEPH_MDS_SESSION_NEW: return "new";
340 case CEPH_MDS_SESSION_OPENING: return "opening";
341 case CEPH_MDS_SESSION_OPEN: return "open";
342 case CEPH_MDS_SESSION_HUNG: return "hung";
343 case CEPH_MDS_SESSION_CLOSING: return "closing";
344 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
345 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
346 default: return "???";
350 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
352 if (atomic_inc_not_zero(&s->s_ref)) {
353 dout("mdsc get_session %p %d -> %d\n", s,
354 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
357 dout("mdsc get_session %p 0 -- FAIL", s);
362 void ceph_put_mds_session(struct ceph_mds_session *s)
364 dout("mdsc put_session %p %d -> %d\n", s,
365 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
366 if (atomic_dec_and_test(&s->s_ref)) {
367 if (s->s_auth.authorizer)
368 ceph_auth_destroy_authorizer(
369 s->s_mdsc->fsc->client->monc.auth,
370 s->s_auth.authorizer);
376 * called under mdsc->mutex
378 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
381 struct ceph_mds_session *session;
383 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
385 session = mdsc->sessions[mds];
386 dout("lookup_mds_session %p %d\n", session,
387 atomic_read(&session->s_ref));
388 get_session(session);
392 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
394 if (mds >= mdsc->max_sessions)
396 return mdsc->sessions[mds];
399 static int __verify_registered_session(struct ceph_mds_client *mdsc,
400 struct ceph_mds_session *s)
402 if (s->s_mds >= mdsc->max_sessions ||
403 mdsc->sessions[s->s_mds] != s)
409 * create+register a new session for given mds.
410 * called under mdsc->mutex.
412 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
415 struct ceph_mds_session *s;
417 s = kzalloc(sizeof(*s), GFP_NOFS);
419 return ERR_PTR(-ENOMEM);
422 s->s_state = CEPH_MDS_SESSION_NEW;
425 mutex_init(&s->s_mutex);
427 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
429 spin_lock_init(&s->s_gen_ttl_lock);
431 s->s_cap_ttl = jiffies - 1;
433 spin_lock_init(&s->s_cap_lock);
434 s->s_renew_requested = 0;
436 INIT_LIST_HEAD(&s->s_caps);
439 atomic_set(&s->s_ref, 1);
440 INIT_LIST_HEAD(&s->s_waiting);
441 INIT_LIST_HEAD(&s->s_unsafe);
442 s->s_num_cap_releases = 0;
443 s->s_cap_iterator = NULL;
444 INIT_LIST_HEAD(&s->s_cap_releases);
445 INIT_LIST_HEAD(&s->s_cap_releases_done);
446 INIT_LIST_HEAD(&s->s_cap_flushing);
447 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
449 dout("register_session mds%d\n", mds);
450 if (mds >= mdsc->max_sessions) {
451 int newmax = 1 << get_count_order(mds+1);
452 struct ceph_mds_session **sa;
454 dout("register_session realloc to %d\n", newmax);
455 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
458 if (mdsc->sessions) {
459 memcpy(sa, mdsc->sessions,
460 mdsc->max_sessions * sizeof(void *));
461 kfree(mdsc->sessions);
464 mdsc->max_sessions = newmax;
466 mdsc->sessions[mds] = s;
467 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
469 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
470 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
476 return ERR_PTR(-ENOMEM);
480 * called under mdsc->mutex
482 static void __unregister_session(struct ceph_mds_client *mdsc,
483 struct ceph_mds_session *s)
485 dout("__unregister_session mds%d %p\n", s->s_mds, s);
486 BUG_ON(mdsc->sessions[s->s_mds] != s);
487 mdsc->sessions[s->s_mds] = NULL;
488 ceph_con_close(&s->s_con);
489 ceph_put_mds_session(s);
493 * drop session refs in request.
495 * should be last request ref, or hold mdsc->mutex
497 static void put_request_session(struct ceph_mds_request *req)
499 if (req->r_session) {
500 ceph_put_mds_session(req->r_session);
501 req->r_session = NULL;
505 void ceph_mdsc_release_request(struct kref *kref)
507 struct ceph_mds_request *req = container_of(kref,
508 struct ceph_mds_request,
511 ceph_msg_put(req->r_request);
513 ceph_msg_put(req->r_reply);
514 destroy_reply_info(&req->r_reply_info);
517 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
520 if (req->r_locked_dir)
521 ceph_put_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
522 if (req->r_target_inode)
523 iput(req->r_target_inode);
526 if (req->r_old_dentry) {
528 * track (and drop pins for) r_old_dentry_dir
529 * separately, since r_old_dentry's d_parent may have
530 * changed between the dir mutex being dropped and
531 * this request being freed.
533 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
535 dput(req->r_old_dentry);
536 iput(req->r_old_dentry_dir);
540 put_request_session(req);
541 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
546 * lookup session, bump ref if found.
548 * called under mdsc->mutex.
550 static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
553 struct ceph_mds_request *req;
554 struct rb_node *n = mdsc->request_tree.rb_node;
557 req = rb_entry(n, struct ceph_mds_request, r_node);
558 if (tid < req->r_tid)
560 else if (tid > req->r_tid)
563 ceph_mdsc_get_request(req);
570 static void __insert_request(struct ceph_mds_client *mdsc,
571 struct ceph_mds_request *new)
573 struct rb_node **p = &mdsc->request_tree.rb_node;
574 struct rb_node *parent = NULL;
575 struct ceph_mds_request *req = NULL;
579 req = rb_entry(parent, struct ceph_mds_request, r_node);
580 if (new->r_tid < req->r_tid)
582 else if (new->r_tid > req->r_tid)
588 rb_link_node(&new->r_node, parent, p);
589 rb_insert_color(&new->r_node, &mdsc->request_tree);
593 * Register an in-flight request, and assign a tid. Link to directory
594 * are modifying (if any).
596 * Called under mdsc->mutex.
598 static void __register_request(struct ceph_mds_client *mdsc,
599 struct ceph_mds_request *req,
602 req->r_tid = ++mdsc->last_tid;
604 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
606 dout("__register_request %p tid %lld\n", req, req->r_tid);
607 ceph_mdsc_get_request(req);
608 __insert_request(mdsc, req);
610 req->r_uid = current_fsuid();
611 req->r_gid = current_fsgid();
614 struct ceph_inode_info *ci = ceph_inode(dir);
617 spin_lock(&ci->i_unsafe_lock);
618 req->r_unsafe_dir = dir;
619 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
620 spin_unlock(&ci->i_unsafe_lock);
624 static void __unregister_request(struct ceph_mds_client *mdsc,
625 struct ceph_mds_request *req)
627 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
628 rb_erase(&req->r_node, &mdsc->request_tree);
629 RB_CLEAR_NODE(&req->r_node);
631 if (req->r_unsafe_dir) {
632 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
634 spin_lock(&ci->i_unsafe_lock);
635 list_del_init(&req->r_unsafe_dir_item);
636 spin_unlock(&ci->i_unsafe_lock);
638 iput(req->r_unsafe_dir);
639 req->r_unsafe_dir = NULL;
642 ceph_mdsc_put_request(req);
646 * Choose mds to send request to next. If there is a hint set in the
647 * request (e.g., due to a prior forward hint from the mds), use that.
648 * Otherwise, consult frag tree and/or caps to identify the
649 * appropriate mds. If all else fails, choose randomly.
651 * Called under mdsc->mutex.
653 static struct dentry *get_nonsnap_parent(struct dentry *dentry)
656 * we don't need to worry about protecting the d_parent access
657 * here because we never renaming inside the snapped namespace
658 * except to resplice to another snapdir, and either the old or new
659 * result is a valid result.
661 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
662 dentry = dentry->d_parent;
666 static int __choose_mds(struct ceph_mds_client *mdsc,
667 struct ceph_mds_request *req)
670 struct ceph_inode_info *ci;
671 struct ceph_cap *cap;
672 int mode = req->r_direct_mode;
674 u32 hash = req->r_direct_hash;
675 bool is_hash = req->r_direct_is_hash;
678 * is there a specific mds we should try? ignore hint if we have
679 * no session and the mds is not up (active or recovering).
681 if (req->r_resend_mds >= 0 &&
682 (__have_session(mdsc, req->r_resend_mds) ||
683 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
684 dout("choose_mds using resend_mds mds%d\n",
686 return req->r_resend_mds;
689 if (mode == USE_RANDOM_MDS)
694 inode = req->r_inode;
695 } else if (req->r_dentry) {
696 /* ignore race with rename; old or new d_parent is okay */
697 struct dentry *parent = req->r_dentry->d_parent;
698 struct inode *dir = parent->d_inode;
700 if (dir->i_sb != mdsc->fsc->sb) {
702 inode = req->r_dentry->d_inode;
703 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
704 /* direct snapped/virtual snapdir requests
705 * based on parent dir inode */
706 struct dentry *dn = get_nonsnap_parent(parent);
708 dout("__choose_mds using nonsnap parent %p\n", inode);
709 } else if (req->r_dentry->d_inode) {
711 inode = req->r_dentry->d_inode;
715 hash = ceph_dentry_hash(dir, req->r_dentry);
720 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
724 ci = ceph_inode(inode);
726 if (is_hash && S_ISDIR(inode->i_mode)) {
727 struct ceph_inode_frag frag;
730 ceph_choose_frag(ci, hash, &frag, &found);
732 if (mode == USE_ANY_MDS && frag.ndist > 0) {
735 /* choose a random replica */
736 get_random_bytes(&r, 1);
739 dout("choose_mds %p %llx.%llx "
740 "frag %u mds%d (%d/%d)\n",
741 inode, ceph_vinop(inode),
744 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
745 CEPH_MDS_STATE_ACTIVE)
749 /* since this file/dir wasn't known to be
750 * replicated, then we want to look for the
751 * authoritative mds. */
754 /* choose auth mds */
756 dout("choose_mds %p %llx.%llx "
757 "frag %u mds%d (auth)\n",
758 inode, ceph_vinop(inode), frag.frag, mds);
759 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
760 CEPH_MDS_STATE_ACTIVE)
766 spin_lock(&ci->i_ceph_lock);
768 if (mode == USE_AUTH_MDS)
769 cap = ci->i_auth_cap;
770 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
771 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
773 spin_unlock(&ci->i_ceph_lock);
776 mds = cap->session->s_mds;
777 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
778 inode, ceph_vinop(inode), mds,
779 cap == ci->i_auth_cap ? "auth " : "", cap);
780 spin_unlock(&ci->i_ceph_lock);
784 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
785 dout("choose_mds chose random mds%d\n", mds);
793 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
795 struct ceph_msg *msg;
796 struct ceph_mds_session_head *h;
798 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
801 pr_err("create_session_msg ENOMEM creating msg\n");
804 h = msg->front.iov_base;
805 h->op = cpu_to_le32(op);
806 h->seq = cpu_to_le64(seq);
811 * send session open request.
813 * called under mdsc->mutex
815 static int __open_session(struct ceph_mds_client *mdsc,
816 struct ceph_mds_session *session)
818 struct ceph_msg *msg;
820 int mds = session->s_mds;
822 /* wait for mds to go active? */
823 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
824 dout("open_session to mds%d (%s)\n", mds,
825 ceph_mds_state_name(mstate));
826 session->s_state = CEPH_MDS_SESSION_OPENING;
827 session->s_renew_requested = jiffies;
829 /* send connect message */
830 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
833 ceph_con_send(&session->s_con, msg);
838 * open sessions for any export targets for the given mds
840 * called under mdsc->mutex
842 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
843 struct ceph_mds_session *session)
845 struct ceph_mds_info *mi;
846 struct ceph_mds_session *ts;
847 int i, mds = session->s_mds;
850 if (mds >= mdsc->mdsmap->m_max_mds)
852 mi = &mdsc->mdsmap->m_info[mds];
853 dout("open_export_target_sessions for mds%d (%d targets)\n",
854 session->s_mds, mi->num_export_targets);
856 for (i = 0; i < mi->num_export_targets; i++) {
857 target = mi->export_targets[i];
858 ts = __ceph_lookup_mds_session(mdsc, target);
860 ts = register_session(mdsc, target);
864 if (session->s_state == CEPH_MDS_SESSION_NEW ||
865 session->s_state == CEPH_MDS_SESSION_CLOSING)
866 __open_session(mdsc, session);
868 dout(" mds%d target mds%d %p is %s\n", session->s_mds,
869 i, ts, session_state_name(ts->s_state));
870 ceph_put_mds_session(ts);
874 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
875 struct ceph_mds_session *session)
877 mutex_lock(&mdsc->mutex);
878 __open_export_target_sessions(mdsc, session);
879 mutex_unlock(&mdsc->mutex);
887 * Free preallocated cap messages assigned to this session
889 static void cleanup_cap_releases(struct ceph_mds_session *session)
891 struct ceph_msg *msg;
893 spin_lock(&session->s_cap_lock);
894 while (!list_empty(&session->s_cap_releases)) {
895 msg = list_first_entry(&session->s_cap_releases,
896 struct ceph_msg, list_head);
897 list_del_init(&msg->list_head);
900 while (!list_empty(&session->s_cap_releases_done)) {
901 msg = list_first_entry(&session->s_cap_releases_done,
902 struct ceph_msg, list_head);
903 list_del_init(&msg->list_head);
906 spin_unlock(&session->s_cap_lock);
910 * Helper to safely iterate over all caps associated with a session, with
911 * special care taken to handle a racing __ceph_remove_cap().
913 * Caller must hold session s_mutex.
915 static int iterate_session_caps(struct ceph_mds_session *session,
916 int (*cb)(struct inode *, struct ceph_cap *,
920 struct ceph_cap *cap;
921 struct inode *inode, *last_inode = NULL;
922 struct ceph_cap *old_cap = NULL;
925 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
926 spin_lock(&session->s_cap_lock);
927 p = session->s_caps.next;
928 while (p != &session->s_caps) {
929 cap = list_entry(p, struct ceph_cap, session_caps);
930 inode = igrab(&cap->ci->vfs_inode);
935 session->s_cap_iterator = cap;
936 spin_unlock(&session->s_cap_lock);
943 ceph_put_cap(session->s_mdsc, old_cap);
947 ret = cb(inode, cap, arg);
950 spin_lock(&session->s_cap_lock);
952 if (cap->ci == NULL) {
953 dout("iterate_session_caps finishing cap %p removal\n",
955 BUG_ON(cap->session != session);
956 list_del_init(&cap->session_caps);
957 session->s_nr_caps--;
959 old_cap = cap; /* put_cap it w/o locks held */
966 session->s_cap_iterator = NULL;
967 spin_unlock(&session->s_cap_lock);
972 ceph_put_cap(session->s_mdsc, old_cap);
977 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
980 struct ceph_inode_info *ci = ceph_inode(inode);
983 dout("removing cap %p, ci is %p, inode is %p\n",
984 cap, ci, &ci->vfs_inode);
985 spin_lock(&ci->i_ceph_lock);
986 __ceph_remove_cap(cap);
987 if (!__ceph_is_any_real_caps(ci)) {
988 struct ceph_mds_client *mdsc =
989 ceph_sb_to_client(inode->i_sb)->mdsc;
991 spin_lock(&mdsc->cap_dirty_lock);
992 if (!list_empty(&ci->i_dirty_item)) {
993 pr_info(" dropping dirty %s state for %p %lld\n",
994 ceph_cap_string(ci->i_dirty_caps),
995 inode, ceph_ino(inode));
996 ci->i_dirty_caps = 0;
997 list_del_init(&ci->i_dirty_item);
1000 if (!list_empty(&ci->i_flushing_item)) {
1001 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
1002 ceph_cap_string(ci->i_flushing_caps),
1003 inode, ceph_ino(inode));
1004 ci->i_flushing_caps = 0;
1005 list_del_init(&ci->i_flushing_item);
1006 mdsc->num_cap_flushing--;
1009 if (drop && ci->i_wrbuffer_ref) {
1010 pr_info(" dropping dirty data for %p %lld\n",
1011 inode, ceph_ino(inode));
1012 ci->i_wrbuffer_ref = 0;
1013 ci->i_wrbuffer_ref_head = 0;
1016 spin_unlock(&mdsc->cap_dirty_lock);
1018 spin_unlock(&ci->i_ceph_lock);
1025 * caller must hold session s_mutex
1027 static void remove_session_caps(struct ceph_mds_session *session)
1029 dout("remove_session_caps on %p\n", session);
1030 iterate_session_caps(session, remove_session_caps_cb, NULL);
1031 BUG_ON(session->s_nr_caps > 0);
1032 BUG_ON(!list_empty(&session->s_cap_flushing));
1033 cleanup_cap_releases(session);
1037 * wake up any threads waiting on this session's caps. if the cap is
1038 * old (didn't get renewed on the client reconnect), remove it now.
1040 * caller must hold s_mutex.
1042 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1045 struct ceph_inode_info *ci = ceph_inode(inode);
1047 wake_up_all(&ci->i_cap_wq);
1049 spin_lock(&ci->i_ceph_lock);
1050 ci->i_wanted_max_size = 0;
1051 ci->i_requested_max_size = 0;
1052 spin_unlock(&ci->i_ceph_lock);
1057 static void wake_up_session_caps(struct ceph_mds_session *session,
1060 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1061 iterate_session_caps(session, wake_up_session_cb,
1062 (void *)(unsigned long)reconnect);
1066 * Send periodic message to MDS renewing all currently held caps. The
1067 * ack will reset the expiration for all caps from this session.
1069 * caller holds s_mutex
1071 static int send_renew_caps(struct ceph_mds_client *mdsc,
1072 struct ceph_mds_session *session)
1074 struct ceph_msg *msg;
1077 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1078 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1079 pr_info("mds%d caps stale\n", session->s_mds);
1080 session->s_renew_requested = jiffies;
1082 /* do not try to renew caps until a recovering mds has reconnected
1083 * with its clients. */
1084 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1085 if (state < CEPH_MDS_STATE_RECONNECT) {
1086 dout("send_renew_caps ignoring mds%d (%s)\n",
1087 session->s_mds, ceph_mds_state_name(state));
1091 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1092 ceph_mds_state_name(state));
1093 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1094 ++session->s_renew_seq);
1097 ceph_con_send(&session->s_con, msg);
1102 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1104 * Called under session->s_mutex
1106 static void renewed_caps(struct ceph_mds_client *mdsc,
1107 struct ceph_mds_session *session, int is_renew)
1112 spin_lock(&session->s_cap_lock);
1113 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
1115 session->s_cap_ttl = session->s_renew_requested +
1116 mdsc->mdsmap->m_session_timeout*HZ;
1119 if (time_before(jiffies, session->s_cap_ttl)) {
1120 pr_info("mds%d caps renewed\n", session->s_mds);
1123 pr_info("mds%d caps still stale\n", session->s_mds);
1126 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1127 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1128 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1129 spin_unlock(&session->s_cap_lock);
1132 wake_up_session_caps(session, 0);
1136 * send a session close request
1138 static int request_close_session(struct ceph_mds_client *mdsc,
1139 struct ceph_mds_session *session)
1141 struct ceph_msg *msg;
1143 dout("request_close_session mds%d state %s seq %lld\n",
1144 session->s_mds, session_state_name(session->s_state),
1146 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1149 ceph_con_send(&session->s_con, msg);
1154 * Called with s_mutex held.
1156 static int __close_session(struct ceph_mds_client *mdsc,
1157 struct ceph_mds_session *session)
1159 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1161 session->s_state = CEPH_MDS_SESSION_CLOSING;
1162 return request_close_session(mdsc, session);
1166 * Trim old(er) caps.
1168 * Because we can't cache an inode without one or more caps, we do
1169 * this indirectly: if a cap is unused, we prune its aliases, at which
1170 * point the inode will hopefully get dropped to.
1172 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1173 * memory pressure from the MDS, though, so it needn't be perfect.
1175 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1177 struct ceph_mds_session *session = arg;
1178 struct ceph_inode_info *ci = ceph_inode(inode);
1179 int used, oissued, mine;
1181 if (session->s_trim_caps <= 0)
1184 spin_lock(&ci->i_ceph_lock);
1185 mine = cap->issued | cap->implemented;
1186 used = __ceph_caps_used(ci);
1187 oissued = __ceph_caps_issued_other(ci, cap);
1189 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1190 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1191 ceph_cap_string(used));
1192 if (ci->i_dirty_caps)
1193 goto out; /* dirty caps */
1194 if ((used & ~oissued) & mine)
1195 goto out; /* we need these caps */
1197 session->s_trim_caps--;
1199 /* we aren't the only cap.. just remove us */
1200 __queue_cap_release(session, ceph_ino(inode), cap->cap_id,
1201 cap->mseq, cap->issue_seq);
1202 __ceph_remove_cap(cap);
1204 /* try to drop referring dentries */
1205 spin_unlock(&ci->i_ceph_lock);
1206 d_prune_aliases(inode);
1207 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1208 inode, cap, atomic_read(&inode->i_count));
1213 spin_unlock(&ci->i_ceph_lock);
1218 * Trim session cap count down to some max number.
1220 static int trim_caps(struct ceph_mds_client *mdsc,
1221 struct ceph_mds_session *session,
1224 int trim_caps = session->s_nr_caps - max_caps;
1226 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1227 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1228 if (trim_caps > 0) {
1229 session->s_trim_caps = trim_caps;
1230 iterate_session_caps(session, trim_caps_cb, session);
1231 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1232 session->s_mds, session->s_nr_caps, max_caps,
1233 trim_caps - session->s_trim_caps);
1234 session->s_trim_caps = 0;
1240 * Allocate cap_release messages. If there is a partially full message
1241 * in the queue, try to allocate enough to cover it's remainder, so that
1242 * we can send it immediately.
1244 * Called under s_mutex.
1246 int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
1247 struct ceph_mds_session *session)
1249 struct ceph_msg *msg, *partial = NULL;
1250 struct ceph_mds_cap_release *head;
1252 int extra = mdsc->fsc->mount_options->cap_release_safety;
1255 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1258 spin_lock(&session->s_cap_lock);
1260 if (!list_empty(&session->s_cap_releases)) {
1261 msg = list_first_entry(&session->s_cap_releases,
1264 head = msg->front.iov_base;
1265 num = le32_to_cpu(head->num);
1267 dout(" partial %p with (%d/%d)\n", msg, num,
1268 (int)CEPH_CAPS_PER_RELEASE);
1269 extra += CEPH_CAPS_PER_RELEASE - num;
1273 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1274 spin_unlock(&session->s_cap_lock);
1275 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1279 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1280 (int)msg->front.iov_len);
1281 head = msg->front.iov_base;
1282 head->num = cpu_to_le32(0);
1283 msg->front.iov_len = sizeof(*head);
1284 spin_lock(&session->s_cap_lock);
1285 list_add(&msg->list_head, &session->s_cap_releases);
1286 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1290 head = partial->front.iov_base;
1291 num = le32_to_cpu(head->num);
1292 dout(" queueing partial %p with %d/%d\n", partial, num,
1293 (int)CEPH_CAPS_PER_RELEASE);
1294 list_move_tail(&partial->list_head,
1295 &session->s_cap_releases_done);
1296 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
1299 spin_unlock(&session->s_cap_lock);
1305 * flush all dirty inode data to disk.
1307 * returns true if we've flushed through want_flush_seq
1309 static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1313 dout("check_cap_flush want %lld\n", want_flush_seq);
1314 mutex_lock(&mdsc->mutex);
1315 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1316 struct ceph_mds_session *session = mdsc->sessions[mds];
1320 get_session(session);
1321 mutex_unlock(&mdsc->mutex);
1323 mutex_lock(&session->s_mutex);
1324 if (!list_empty(&session->s_cap_flushing)) {
1325 struct ceph_inode_info *ci =
1326 list_entry(session->s_cap_flushing.next,
1327 struct ceph_inode_info,
1329 struct inode *inode = &ci->vfs_inode;
1331 spin_lock(&ci->i_ceph_lock);
1332 if (ci->i_cap_flush_seq <= want_flush_seq) {
1333 dout("check_cap_flush still flushing %p "
1334 "seq %lld <= %lld to mds%d\n", inode,
1335 ci->i_cap_flush_seq, want_flush_seq,
1339 spin_unlock(&ci->i_ceph_lock);
1341 mutex_unlock(&session->s_mutex);
1342 ceph_put_mds_session(session);
1346 mutex_lock(&mdsc->mutex);
1349 mutex_unlock(&mdsc->mutex);
1350 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1355 * called under s_mutex
1357 void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1358 struct ceph_mds_session *session)
1360 struct ceph_msg *msg;
1362 dout("send_cap_releases mds%d\n", session->s_mds);
1363 spin_lock(&session->s_cap_lock);
1364 while (!list_empty(&session->s_cap_releases_done)) {
1365 msg = list_first_entry(&session->s_cap_releases_done,
1366 struct ceph_msg, list_head);
1367 list_del_init(&msg->list_head);
1368 spin_unlock(&session->s_cap_lock);
1369 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1370 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1371 ceph_con_send(&session->s_con, msg);
1372 spin_lock(&session->s_cap_lock);
1374 spin_unlock(&session->s_cap_lock);
1377 static void discard_cap_releases(struct ceph_mds_client *mdsc,
1378 struct ceph_mds_session *session)
1380 struct ceph_msg *msg;
1381 struct ceph_mds_cap_release *head;
1384 dout("discard_cap_releases mds%d\n", session->s_mds);
1385 spin_lock(&session->s_cap_lock);
1387 /* zero out the in-progress message */
1388 msg = list_first_entry(&session->s_cap_releases,
1389 struct ceph_msg, list_head);
1390 head = msg->front.iov_base;
1391 num = le32_to_cpu(head->num);
1392 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1393 head->num = cpu_to_le32(0);
1394 session->s_num_cap_releases += num;
1396 /* requeue completed messages */
1397 while (!list_empty(&session->s_cap_releases_done)) {
1398 msg = list_first_entry(&session->s_cap_releases_done,
1399 struct ceph_msg, list_head);
1400 list_del_init(&msg->list_head);
1402 head = msg->front.iov_base;
1403 num = le32_to_cpu(head->num);
1404 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1406 session->s_num_cap_releases += num;
1407 head->num = cpu_to_le32(0);
1408 msg->front.iov_len = sizeof(*head);
1409 list_add(&msg->list_head, &session->s_cap_releases);
1412 spin_unlock(&session->s_cap_lock);
1420 * Create an mds request.
1422 struct ceph_mds_request *
1423 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1425 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1428 return ERR_PTR(-ENOMEM);
1430 mutex_init(&req->r_fill_mutex);
1432 req->r_started = jiffies;
1433 req->r_resend_mds = -1;
1434 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1436 kref_init(&req->r_kref);
1437 INIT_LIST_HEAD(&req->r_wait);
1438 init_completion(&req->r_completion);
1439 init_completion(&req->r_safe_completion);
1440 INIT_LIST_HEAD(&req->r_unsafe_item);
1443 req->r_direct_mode = mode;
1448 * return oldest (lowest) request, tid in request tree, 0 if none.
1450 * called under mdsc->mutex.
1452 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1454 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1456 return rb_entry(rb_first(&mdsc->request_tree),
1457 struct ceph_mds_request, r_node);
1460 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1462 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1470 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1471 * on build_path_from_dentry in fs/cifs/dir.c.
1473 * If @stop_on_nosnap, generate path relative to the first non-snapped
1476 * Encode hidden .snap dirs as a double /, i.e.
1477 * foo/.snap/bar -> foo//bar
1479 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1482 struct dentry *temp;
1488 return ERR_PTR(-EINVAL);
1492 seq = read_seqbegin(&rename_lock);
1494 for (temp = dentry; !IS_ROOT(temp);) {
1495 struct inode *inode = temp->d_inode;
1496 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1497 len++; /* slash only */
1498 else if (stop_on_nosnap && inode &&
1499 ceph_snap(inode) == CEPH_NOSNAP)
1502 len += 1 + temp->d_name.len;
1503 temp = temp->d_parent;
1507 len--; /* no leading '/' */
1509 path = kmalloc(len+1, GFP_NOFS);
1511 return ERR_PTR(-ENOMEM);
1513 path[pos] = 0; /* trailing null */
1515 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1516 struct inode *inode;
1518 spin_lock(&temp->d_lock);
1519 inode = temp->d_inode;
1520 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1521 dout("build_path path+%d: %p SNAPDIR\n",
1523 } else if (stop_on_nosnap && inode &&
1524 ceph_snap(inode) == CEPH_NOSNAP) {
1525 spin_unlock(&temp->d_lock);
1528 pos -= temp->d_name.len;
1530 spin_unlock(&temp->d_lock);
1533 strncpy(path + pos, temp->d_name.name,
1536 spin_unlock(&temp->d_lock);
1539 temp = temp->d_parent;
1542 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
1543 pr_err("build_path did not end path lookup where "
1544 "expected, namelen is %d, pos is %d\n", len, pos);
1545 /* presumably this is only possible if racing with a
1546 rename of one of the parent directories (we can not
1547 lock the dentries above us to prevent this, but
1548 retrying should be harmless) */
1553 *base = ceph_ino(temp->d_inode);
1555 dout("build_path on %p %d built %llx '%.*s'\n",
1556 dentry, dentry->d_count, *base, len, path);
1560 static int build_dentry_path(struct dentry *dentry,
1561 const char **ppath, int *ppathlen, u64 *pino,
1566 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1567 *pino = ceph_ino(dentry->d_parent->d_inode);
1568 *ppath = dentry->d_name.name;
1569 *ppathlen = dentry->d_name.len;
1572 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1574 return PTR_ERR(path);
1580 static int build_inode_path(struct inode *inode,
1581 const char **ppath, int *ppathlen, u64 *pino,
1584 struct dentry *dentry;
1587 if (ceph_snap(inode) == CEPH_NOSNAP) {
1588 *pino = ceph_ino(inode);
1592 dentry = d_find_alias(inode);
1593 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1596 return PTR_ERR(path);
1603 * request arguments may be specified via an inode *, a dentry *, or
1604 * an explicit ino+path.
1606 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1607 const char *rpath, u64 rino,
1608 const char **ppath, int *pathlen,
1609 u64 *ino, int *freepath)
1614 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1615 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1617 } else if (rdentry) {
1618 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1619 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1621 } else if (rpath || rino) {
1624 *pathlen = rpath ? strlen(rpath) : 0;
1625 dout(" path %.*s\n", *pathlen, rpath);
1632 * called under mdsc->mutex
1634 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1635 struct ceph_mds_request *req,
1638 struct ceph_msg *msg;
1639 struct ceph_mds_request_head *head;
1640 const char *path1 = NULL;
1641 const char *path2 = NULL;
1642 u64 ino1 = 0, ino2 = 0;
1643 int pathlen1 = 0, pathlen2 = 0;
1644 int freepath1 = 0, freepath2 = 0;
1650 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1651 req->r_path1, req->r_ino1.ino,
1652 &path1, &pathlen1, &ino1, &freepath1);
1658 ret = set_request_path_attr(NULL, req->r_old_dentry,
1659 req->r_path2, req->r_ino2.ino,
1660 &path2, &pathlen2, &ino2, &freepath2);
1666 len = sizeof(*head) +
1667 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
1669 /* calculate (max) length for cap releases */
1670 len += sizeof(struct ceph_mds_request_release) *
1671 (!!req->r_inode_drop + !!req->r_dentry_drop +
1672 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1673 if (req->r_dentry_drop)
1674 len += req->r_dentry->d_name.len;
1675 if (req->r_old_dentry_drop)
1676 len += req->r_old_dentry->d_name.len;
1678 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
1680 msg = ERR_PTR(-ENOMEM);
1684 msg->hdr.tid = cpu_to_le64(req->r_tid);
1686 head = msg->front.iov_base;
1687 p = msg->front.iov_base + sizeof(*head);
1688 end = msg->front.iov_base + msg->front.iov_len;
1690 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1691 head->op = cpu_to_le32(req->r_op);
1692 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
1693 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
1694 head->args = req->r_args;
1696 ceph_encode_filepath(&p, end, ino1, path1);
1697 ceph_encode_filepath(&p, end, ino2, path2);
1699 /* make note of release offset, in case we need to replay */
1700 req->r_request_release_offset = p - msg->front.iov_base;
1704 if (req->r_inode_drop)
1705 releases += ceph_encode_inode_release(&p,
1706 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1707 mds, req->r_inode_drop, req->r_inode_unless, 0);
1708 if (req->r_dentry_drop)
1709 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1710 mds, req->r_dentry_drop, req->r_dentry_unless);
1711 if (req->r_old_dentry_drop)
1712 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1713 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1714 if (req->r_old_inode_drop)
1715 releases += ceph_encode_inode_release(&p,
1716 req->r_old_dentry->d_inode,
1717 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1718 head->num_releases = cpu_to_le16(releases);
1721 msg->front.iov_len = p - msg->front.iov_base;
1722 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1724 if (req->r_data_len) {
1725 /* outbound data set only by ceph_sync_setxattr() */
1726 BUG_ON(!req->r_pages);
1727 ceph_msg_data_add_pages(msg, req->r_pages, req->r_data_len, 0);
1730 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1731 msg->hdr.data_off = cpu_to_le16(0);
1735 kfree((char *)path2);
1738 kfree((char *)path1);
1744 * called under mdsc->mutex if error, under no mutex if
1747 static void complete_request(struct ceph_mds_client *mdsc,
1748 struct ceph_mds_request *req)
1750 if (req->r_callback)
1751 req->r_callback(mdsc, req);
1753 complete_all(&req->r_completion);
1757 * called under mdsc->mutex
1759 static int __prepare_send_request(struct ceph_mds_client *mdsc,
1760 struct ceph_mds_request *req,
1763 struct ceph_mds_request_head *rhead;
1764 struct ceph_msg *msg;
1769 struct ceph_cap *cap =
1770 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1773 req->r_sent_on_mseq = cap->mseq;
1775 req->r_sent_on_mseq = -1;
1777 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1778 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1780 if (req->r_got_unsafe) {
1782 * Replay. Do not regenerate message (and rebuild
1783 * paths, etc.); just use the original message.
1784 * Rebuilding paths will break for renames because
1785 * d_move mangles the src name.
1787 msg = req->r_request;
1788 rhead = msg->front.iov_base;
1790 flags = le32_to_cpu(rhead->flags);
1791 flags |= CEPH_MDS_FLAG_REPLAY;
1792 rhead->flags = cpu_to_le32(flags);
1794 if (req->r_target_inode)
1795 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1797 rhead->num_retry = req->r_attempts - 1;
1799 /* remove cap/dentry releases from message */
1800 rhead->num_releases = 0;
1801 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1802 msg->front.iov_len = req->r_request_release_offset;
1806 if (req->r_request) {
1807 ceph_msg_put(req->r_request);
1808 req->r_request = NULL;
1810 msg = create_request_message(mdsc, req, mds);
1812 req->r_err = PTR_ERR(msg);
1813 complete_request(mdsc, req);
1814 return PTR_ERR(msg);
1816 req->r_request = msg;
1818 rhead = msg->front.iov_base;
1819 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1820 if (req->r_got_unsafe)
1821 flags |= CEPH_MDS_FLAG_REPLAY;
1822 if (req->r_locked_dir)
1823 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1824 rhead->flags = cpu_to_le32(flags);
1825 rhead->num_fwd = req->r_num_fwd;
1826 rhead->num_retry = req->r_attempts - 1;
1829 dout(" r_locked_dir = %p\n", req->r_locked_dir);
1834 * send request, or put it on the appropriate wait list.
1836 static int __do_request(struct ceph_mds_client *mdsc,
1837 struct ceph_mds_request *req)
1839 struct ceph_mds_session *session = NULL;
1843 if (req->r_err || req->r_got_result) {
1845 __unregister_request(mdsc, req);
1849 if (req->r_timeout &&
1850 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1851 dout("do_request timed out\n");
1856 put_request_session(req);
1858 mds = __choose_mds(mdsc, req);
1860 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1861 dout("do_request no mds or not active, waiting for map\n");
1862 list_add(&req->r_wait, &mdsc->waiting_for_map);
1866 /* get, open session */
1867 session = __ceph_lookup_mds_session(mdsc, mds);
1869 session = register_session(mdsc, mds);
1870 if (IS_ERR(session)) {
1871 err = PTR_ERR(session);
1875 req->r_session = get_session(session);
1877 dout("do_request mds%d session %p state %s\n", mds, session,
1878 session_state_name(session->s_state));
1879 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1880 session->s_state != CEPH_MDS_SESSION_HUNG) {
1881 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1882 session->s_state == CEPH_MDS_SESSION_CLOSING)
1883 __open_session(mdsc, session);
1884 list_add(&req->r_wait, &session->s_waiting);
1889 req->r_resend_mds = -1; /* forget any previous mds hint */
1891 if (req->r_request_started == 0) /* note request start time */
1892 req->r_request_started = jiffies;
1894 err = __prepare_send_request(mdsc, req, mds);
1896 ceph_msg_get(req->r_request);
1897 ceph_con_send(&session->s_con, req->r_request);
1901 ceph_put_mds_session(session);
1907 complete_request(mdsc, req);
1912 * called under mdsc->mutex
1914 static void __wake_requests(struct ceph_mds_client *mdsc,
1915 struct list_head *head)
1917 struct ceph_mds_request *req;
1918 LIST_HEAD(tmp_list);
1920 list_splice_init(head, &tmp_list);
1922 while (!list_empty(&tmp_list)) {
1923 req = list_entry(tmp_list.next,
1924 struct ceph_mds_request, r_wait);
1925 list_del_init(&req->r_wait);
1926 dout(" wake request %p tid %llu\n", req, req->r_tid);
1927 __do_request(mdsc, req);
1932 * Wake up threads with requests pending for @mds, so that they can
1933 * resubmit their requests to a possibly different mds.
1935 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
1937 struct ceph_mds_request *req;
1940 dout("kick_requests mds%d\n", mds);
1941 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1942 req = rb_entry(p, struct ceph_mds_request, r_node);
1943 if (req->r_got_unsafe)
1945 if (req->r_session &&
1946 req->r_session->s_mds == mds) {
1947 dout(" kicking tid %llu\n", req->r_tid);
1948 __do_request(mdsc, req);
1953 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1954 struct ceph_mds_request *req)
1956 dout("submit_request on %p\n", req);
1957 mutex_lock(&mdsc->mutex);
1958 __register_request(mdsc, req, NULL);
1959 __do_request(mdsc, req);
1960 mutex_unlock(&mdsc->mutex);
1964 * Synchrously perform an mds request. Take care of all of the
1965 * session setup, forwarding, retry details.
1967 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1969 struct ceph_mds_request *req)
1973 dout("do_request on %p\n", req);
1975 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1977 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1978 if (req->r_locked_dir)
1979 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1980 if (req->r_old_dentry)
1981 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
1985 mutex_lock(&mdsc->mutex);
1986 __register_request(mdsc, req, dir);
1987 __do_request(mdsc, req);
1991 __unregister_request(mdsc, req);
1992 dout("do_request early error %d\n", err);
1997 mutex_unlock(&mdsc->mutex);
1998 dout("do_request waiting\n");
1999 if (req->r_timeout) {
2000 err = (long)wait_for_completion_killable_timeout(
2001 &req->r_completion, req->r_timeout);
2005 err = wait_for_completion_killable(&req->r_completion);
2007 dout("do_request waited, got %d\n", err);
2008 mutex_lock(&mdsc->mutex);
2010 /* only abort if we didn't race with a real reply */
2011 if (req->r_got_result) {
2012 err = le32_to_cpu(req->r_reply_info.head->result);
2013 } else if (err < 0) {
2014 dout("aborted request %lld with %d\n", req->r_tid, err);
2017 * ensure we aren't running concurrently with
2018 * ceph_fill_trace or ceph_readdir_prepopulate, which
2019 * rely on locks (dir mutex) held by our caller.
2021 mutex_lock(&req->r_fill_mutex);
2023 req->r_aborted = true;
2024 mutex_unlock(&req->r_fill_mutex);
2026 if (req->r_locked_dir &&
2027 (req->r_op & CEPH_MDS_OP_WRITE))
2028 ceph_invalidate_dir_request(req);
2034 mutex_unlock(&mdsc->mutex);
2035 dout("do_request %p done, result %d\n", req, err);
2040 * Invalidate dir's completeness, dentry lease state on an aborted MDS
2041 * namespace request.
2043 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2045 struct inode *inode = req->r_locked_dir;
2047 dout("invalidate_dir_request %p (complete, lease(s))\n", inode);
2049 ceph_dir_clear_complete(inode);
2051 ceph_invalidate_dentry_lease(req->r_dentry);
2052 if (req->r_old_dentry)
2053 ceph_invalidate_dentry_lease(req->r_old_dentry);
2059 * We take the session mutex and parse and process the reply immediately.
2060 * This preserves the logical ordering of replies, capabilities, etc., sent
2061 * by the MDS as they are applied to our local cache.
2063 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2065 struct ceph_mds_client *mdsc = session->s_mdsc;
2066 struct ceph_mds_request *req;
2067 struct ceph_mds_reply_head *head = msg->front.iov_base;
2068 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2071 int mds = session->s_mds;
2073 if (msg->front.iov_len < sizeof(*head)) {
2074 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2079 /* get request, session */
2080 tid = le64_to_cpu(msg->hdr.tid);
2081 mutex_lock(&mdsc->mutex);
2082 req = __lookup_request(mdsc, tid);
2084 dout("handle_reply on unknown tid %llu\n", tid);
2085 mutex_unlock(&mdsc->mutex);
2088 dout("handle_reply %p\n", req);
2090 /* correct session? */
2091 if (req->r_session != session) {
2092 pr_err("mdsc_handle_reply got %llu on session mds%d"
2093 " not mds%d\n", tid, session->s_mds,
2094 req->r_session ? req->r_session->s_mds : -1);
2095 mutex_unlock(&mdsc->mutex);
2100 if ((req->r_got_unsafe && !head->safe) ||
2101 (req->r_got_safe && head->safe)) {
2102 pr_warning("got a dup %s reply on %llu from mds%d\n",
2103 head->safe ? "safe" : "unsafe", tid, mds);
2104 mutex_unlock(&mdsc->mutex);
2107 if (req->r_got_safe && !head->safe) {
2108 pr_warning("got unsafe after safe on %llu from mds%d\n",
2110 mutex_unlock(&mdsc->mutex);
2114 result = le32_to_cpu(head->result);
2118 * if we're not talking to the authority, send to them
2119 * if the authority has changed while we weren't looking,
2120 * send to new authority
2121 * Otherwise we just have to return an ESTALE
2123 if (result == -ESTALE) {
2124 dout("got ESTALE on request %llu", req->r_tid);
2125 if (!req->r_inode) {
2126 /* do nothing; not an authority problem */
2127 } else if (req->r_direct_mode != USE_AUTH_MDS) {
2128 dout("not using auth, setting for that now");
2129 req->r_direct_mode = USE_AUTH_MDS;
2130 __do_request(mdsc, req);
2131 mutex_unlock(&mdsc->mutex);
2134 struct ceph_inode_info *ci = ceph_inode(req->r_inode);
2135 struct ceph_cap *cap = NULL;
2138 cap = ceph_get_cap_for_mds(ci,
2139 req->r_session->s_mds);
2141 dout("already using auth");
2142 if ((!cap || cap != ci->i_auth_cap) ||
2143 (cap->mseq != req->r_sent_on_mseq)) {
2144 dout("but cap changed, so resending");
2145 __do_request(mdsc, req);
2146 mutex_unlock(&mdsc->mutex);
2150 dout("have to return ESTALE on request %llu", req->r_tid);
2155 req->r_got_safe = true;
2156 __unregister_request(mdsc, req);
2157 complete_all(&req->r_safe_completion);
2159 if (req->r_got_unsafe) {
2161 * We already handled the unsafe response, now do the
2162 * cleanup. No need to examine the response; the MDS
2163 * doesn't include any result info in the safe
2164 * response. And even if it did, there is nothing
2165 * useful we could do with a revised return value.
2167 dout("got safe reply %llu, mds%d\n", tid, mds);
2168 list_del_init(&req->r_unsafe_item);
2170 /* last unsafe request during umount? */
2171 if (mdsc->stopping && !__get_oldest_req(mdsc))
2172 complete_all(&mdsc->safe_umount_waiters);
2173 mutex_unlock(&mdsc->mutex);
2177 req->r_got_unsafe = true;
2178 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2181 dout("handle_reply tid %lld result %d\n", tid, result);
2182 rinfo = &req->r_reply_info;
2183 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2184 mutex_unlock(&mdsc->mutex);
2186 mutex_lock(&session->s_mutex);
2188 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
2194 if (rinfo->snapblob_len) {
2195 down_write(&mdsc->snap_rwsem);
2196 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2197 rinfo->snapblob + rinfo->snapblob_len,
2198 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2199 downgrade_write(&mdsc->snap_rwsem);
2201 down_read(&mdsc->snap_rwsem);
2204 /* insert trace into our cache */
2205 mutex_lock(&req->r_fill_mutex);
2206 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
2208 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
2209 req->r_op == CEPH_MDS_OP_LSSNAP) &&
2211 ceph_readdir_prepopulate(req, req->r_session);
2212 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2214 mutex_unlock(&req->r_fill_mutex);
2216 up_read(&mdsc->snap_rwsem);
2218 mutex_lock(&mdsc->mutex);
2219 if (!req->r_aborted) {
2225 req->r_got_result = true;
2228 dout("reply arrived after request %lld was aborted\n", tid);
2230 mutex_unlock(&mdsc->mutex);
2232 ceph_add_cap_releases(mdsc, req->r_session);
2233 mutex_unlock(&session->s_mutex);
2235 /* kick calling process */
2236 complete_request(mdsc, req);
2238 ceph_mdsc_put_request(req);
2245 * handle mds notification that our request has been forwarded.
2247 static void handle_forward(struct ceph_mds_client *mdsc,
2248 struct ceph_mds_session *session,
2249 struct ceph_msg *msg)
2251 struct ceph_mds_request *req;
2252 u64 tid = le64_to_cpu(msg->hdr.tid);
2256 void *p = msg->front.iov_base;
2257 void *end = p + msg->front.iov_len;
2259 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2260 next_mds = ceph_decode_32(&p);
2261 fwd_seq = ceph_decode_32(&p);
2263 mutex_lock(&mdsc->mutex);
2264 req = __lookup_request(mdsc, tid);
2266 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2267 goto out; /* dup reply? */
2270 if (req->r_aborted) {
2271 dout("forward tid %llu aborted, unregistering\n", tid);
2272 __unregister_request(mdsc, req);
2273 } else if (fwd_seq <= req->r_num_fwd) {
2274 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2275 tid, next_mds, req->r_num_fwd, fwd_seq);
2277 /* resend. forward race not possible; mds would drop */
2278 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2280 BUG_ON(req->r_got_result);
2281 req->r_num_fwd = fwd_seq;
2282 req->r_resend_mds = next_mds;
2283 put_request_session(req);
2284 __do_request(mdsc, req);
2286 ceph_mdsc_put_request(req);
2288 mutex_unlock(&mdsc->mutex);
2292 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2296 * handle a mds session control message
2298 static void handle_session(struct ceph_mds_session *session,
2299 struct ceph_msg *msg)
2301 struct ceph_mds_client *mdsc = session->s_mdsc;
2304 int mds = session->s_mds;
2305 struct ceph_mds_session_head *h = msg->front.iov_base;
2309 if (msg->front.iov_len != sizeof(*h))
2311 op = le32_to_cpu(h->op);
2312 seq = le64_to_cpu(h->seq);
2314 mutex_lock(&mdsc->mutex);
2315 if (op == CEPH_SESSION_CLOSE)
2316 __unregister_session(mdsc, session);
2317 /* FIXME: this ttl calculation is generous */
2318 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2319 mutex_unlock(&mdsc->mutex);
2321 mutex_lock(&session->s_mutex);
2323 dout("handle_session mds%d %s %p state %s seq %llu\n",
2324 mds, ceph_session_op_name(op), session,
2325 session_state_name(session->s_state), seq);
2327 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2328 session->s_state = CEPH_MDS_SESSION_OPEN;
2329 pr_info("mds%d came back\n", session->s_mds);
2333 case CEPH_SESSION_OPEN:
2334 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2335 pr_info("mds%d reconnect success\n", session->s_mds);
2336 session->s_state = CEPH_MDS_SESSION_OPEN;
2337 renewed_caps(mdsc, session, 0);
2340 __close_session(mdsc, session);
2343 case CEPH_SESSION_RENEWCAPS:
2344 if (session->s_renew_seq == seq)
2345 renewed_caps(mdsc, session, 1);
2348 case CEPH_SESSION_CLOSE:
2349 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2350 pr_info("mds%d reconnect denied\n", session->s_mds);
2351 remove_session_caps(session);
2352 wake = 1; /* for good measure */
2353 wake_up_all(&mdsc->session_close_wq);
2354 kick_requests(mdsc, mds);
2357 case CEPH_SESSION_STALE:
2358 pr_info("mds%d caps went stale, renewing\n",
2360 spin_lock(&session->s_gen_ttl_lock);
2361 session->s_cap_gen++;
2362 session->s_cap_ttl = jiffies - 1;
2363 spin_unlock(&session->s_gen_ttl_lock);
2364 send_renew_caps(mdsc, session);
2367 case CEPH_SESSION_RECALL_STATE:
2368 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2372 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2376 mutex_unlock(&session->s_mutex);
2378 mutex_lock(&mdsc->mutex);
2379 __wake_requests(mdsc, &session->s_waiting);
2380 mutex_unlock(&mdsc->mutex);
2385 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2386 (int)msg->front.iov_len);
2393 * called under session->mutex.
2395 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2396 struct ceph_mds_session *session)
2398 struct ceph_mds_request *req, *nreq;
2401 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2403 mutex_lock(&mdsc->mutex);
2404 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2405 err = __prepare_send_request(mdsc, req, session->s_mds);
2407 ceph_msg_get(req->r_request);
2408 ceph_con_send(&session->s_con, req->r_request);
2411 mutex_unlock(&mdsc->mutex);
2415 * Encode information about a cap for a reconnect with the MDS.
2417 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2421 struct ceph_mds_cap_reconnect v2;
2422 struct ceph_mds_cap_reconnect_v1 v1;
2425 struct ceph_inode_info *ci;
2426 struct ceph_reconnect_state *recon_state = arg;
2427 struct ceph_pagelist *pagelist = recon_state->pagelist;
2431 struct dentry *dentry;
2435 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2436 inode, ceph_vinop(inode), cap, cap->cap_id,
2437 ceph_cap_string(cap->issued));
2438 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2442 dentry = d_find_alias(inode);
2444 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2446 err = PTR_ERR(path);
2453 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2457 spin_lock(&ci->i_ceph_lock);
2458 cap->seq = 0; /* reset cap seq */
2459 cap->issue_seq = 0; /* and issue_seq */
2461 if (recon_state->flock) {
2462 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2463 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2464 rec.v2.issued = cpu_to_le32(cap->issued);
2465 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2466 rec.v2.pathbase = cpu_to_le64(pathbase);
2467 rec.v2.flock_len = 0;
2468 reclen = sizeof(rec.v2);
2470 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2471 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2472 rec.v1.issued = cpu_to_le32(cap->issued);
2473 rec.v1.size = cpu_to_le64(inode->i_size);
2474 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2475 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2476 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2477 rec.v1.pathbase = cpu_to_le64(pathbase);
2478 reclen = sizeof(rec.v1);
2480 spin_unlock(&ci->i_ceph_lock);
2482 if (recon_state->flock) {
2483 int num_fcntl_locks, num_flock_locks;
2484 struct ceph_filelock *flocks;
2488 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
2490 flocks = kmalloc((num_fcntl_locks+num_flock_locks) *
2491 sizeof(struct ceph_filelock), GFP_NOFS);
2497 err = ceph_encode_locks_to_buffer(inode, flocks,
2508 * number of encoded locks is stable, so copy to pagelist
2510 rec.v2.flock_len = cpu_to_le32(2*sizeof(u32) +
2511 (num_fcntl_locks+num_flock_locks) *
2512 sizeof(struct ceph_filelock));
2513 err = ceph_pagelist_append(pagelist, &rec, reclen);
2515 err = ceph_locks_to_pagelist(flocks, pagelist,
2520 err = ceph_pagelist_append(pagelist, &rec, reclen);
2531 * If an MDS fails and recovers, clients need to reconnect in order to
2532 * reestablish shared state. This includes all caps issued through
2533 * this session _and_ the snap_realm hierarchy. Because it's not
2534 * clear which snap realms the mds cares about, we send everything we
2535 * know about.. that ensures we'll then get any new info the
2536 * recovering MDS might have.
2538 * This is a relatively heavyweight operation, but it's rare.
2540 * called with mdsc->mutex held.
2542 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2543 struct ceph_mds_session *session)
2545 struct ceph_msg *reply;
2547 int mds = session->s_mds;
2549 struct ceph_pagelist *pagelist;
2550 struct ceph_reconnect_state recon_state;
2552 pr_info("mds%d reconnect start\n", mds);
2554 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2556 goto fail_nopagelist;
2557 ceph_pagelist_init(pagelist);
2559 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
2563 mutex_lock(&session->s_mutex);
2564 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2567 ceph_con_close(&session->s_con);
2568 ceph_con_open(&session->s_con,
2569 CEPH_ENTITY_TYPE_MDS, mds,
2570 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2572 /* replay unsafe requests */
2573 replay_unsafe_requests(mdsc, session);
2575 down_read(&mdsc->snap_rwsem);
2577 dout("session %p state %s\n", session,
2578 session_state_name(session->s_state));
2580 /* drop old cap expires; we're about to reestablish that state */
2581 discard_cap_releases(mdsc, session);
2583 /* traverse this session's caps */
2584 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2588 recon_state.pagelist = pagelist;
2589 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2590 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
2595 * snaprealms. we provide mds with the ino, seq (version), and
2596 * parent for all of our realms. If the mds has any newer info,
2599 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2600 struct ceph_snap_realm *realm =
2601 rb_entry(p, struct ceph_snap_realm, node);
2602 struct ceph_mds_snaprealm_reconnect sr_rec;
2604 dout(" adding snap realm %llx seq %lld parent %llx\n",
2605 realm->ino, realm->seq, realm->parent_ino);
2606 sr_rec.ino = cpu_to_le64(realm->ino);
2607 sr_rec.seq = cpu_to_le64(realm->seq);
2608 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2609 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2614 if (recon_state.flock)
2615 reply->hdr.version = cpu_to_le16(2);
2616 if (pagelist->length) {
2617 /* set up outbound data if we have any */
2618 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2619 ceph_msg_data_add_pagelist(reply, pagelist);
2621 ceph_con_send(&session->s_con, reply);
2623 mutex_unlock(&session->s_mutex);
2625 mutex_lock(&mdsc->mutex);
2626 __wake_requests(mdsc, &session->s_waiting);
2627 mutex_unlock(&mdsc->mutex);
2629 up_read(&mdsc->snap_rwsem);
2633 ceph_msg_put(reply);
2634 up_read(&mdsc->snap_rwsem);
2635 mutex_unlock(&session->s_mutex);
2637 ceph_pagelist_release(pagelist);
2640 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
2646 * compare old and new mdsmaps, kicking requests
2647 * and closing out old connections as necessary
2649 * called under mdsc->mutex.
2651 static void check_new_map(struct ceph_mds_client *mdsc,
2652 struct ceph_mdsmap *newmap,
2653 struct ceph_mdsmap *oldmap)
2656 int oldstate, newstate;
2657 struct ceph_mds_session *s;
2659 dout("check_new_map new %u old %u\n",
2660 newmap->m_epoch, oldmap->m_epoch);
2662 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2663 if (mdsc->sessions[i] == NULL)
2665 s = mdsc->sessions[i];
2666 oldstate = ceph_mdsmap_get_state(oldmap, i);
2667 newstate = ceph_mdsmap_get_state(newmap, i);
2669 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2670 i, ceph_mds_state_name(oldstate),
2671 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
2672 ceph_mds_state_name(newstate),
2673 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
2674 session_state_name(s->s_state));
2676 if (i >= newmap->m_max_mds ||
2677 memcmp(ceph_mdsmap_get_addr(oldmap, i),
2678 ceph_mdsmap_get_addr(newmap, i),
2679 sizeof(struct ceph_entity_addr))) {
2680 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2681 /* the session never opened, just close it
2683 __wake_requests(mdsc, &s->s_waiting);
2684 __unregister_session(mdsc, s);
2687 mutex_unlock(&mdsc->mutex);
2688 mutex_lock(&s->s_mutex);
2689 mutex_lock(&mdsc->mutex);
2690 ceph_con_close(&s->s_con);
2691 mutex_unlock(&s->s_mutex);
2692 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2695 /* kick any requests waiting on the recovering mds */
2696 kick_requests(mdsc, i);
2697 } else if (oldstate == newstate) {
2698 continue; /* nothing new with this mds */
2704 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
2705 newstate >= CEPH_MDS_STATE_RECONNECT) {
2706 mutex_unlock(&mdsc->mutex);
2707 send_mds_reconnect(mdsc, s);
2708 mutex_lock(&mdsc->mutex);
2712 * kick request on any mds that has gone active.
2714 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2715 newstate >= CEPH_MDS_STATE_ACTIVE) {
2716 if (oldstate != CEPH_MDS_STATE_CREATING &&
2717 oldstate != CEPH_MDS_STATE_STARTING)
2718 pr_info("mds%d recovery completed\n", s->s_mds);
2719 kick_requests(mdsc, i);
2720 ceph_kick_flushing_caps(mdsc, s);
2721 wake_up_session_caps(s, 1);
2725 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2726 s = mdsc->sessions[i];
2729 if (!ceph_mdsmap_is_laggy(newmap, i))
2731 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2732 s->s_state == CEPH_MDS_SESSION_HUNG ||
2733 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2734 dout(" connecting to export targets of laggy mds%d\n",
2736 __open_export_target_sessions(mdsc, s);
2748 * caller must hold session s_mutex, dentry->d_lock
2750 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2752 struct ceph_dentry_info *di = ceph_dentry(dentry);
2754 ceph_put_mds_session(di->lease_session);
2755 di->lease_session = NULL;
2758 static void handle_lease(struct ceph_mds_client *mdsc,
2759 struct ceph_mds_session *session,
2760 struct ceph_msg *msg)
2762 struct super_block *sb = mdsc->fsc->sb;
2763 struct inode *inode;
2764 struct dentry *parent, *dentry;
2765 struct ceph_dentry_info *di;
2766 int mds = session->s_mds;
2767 struct ceph_mds_lease *h = msg->front.iov_base;
2769 struct ceph_vino vino;
2773 dout("handle_lease from mds%d\n", mds);
2776 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2778 vino.ino = le64_to_cpu(h->ino);
2779 vino.snap = CEPH_NOSNAP;
2780 seq = le32_to_cpu(h->seq);
2781 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2782 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2783 if (dname.len != get_unaligned_le32(h+1))
2786 mutex_lock(&session->s_mutex);
2790 inode = ceph_find_inode(sb, vino);
2791 dout("handle_lease %s, ino %llx %p %.*s\n",
2792 ceph_lease_op_name(h->action), vino.ino, inode,
2793 dname.len, dname.name);
2794 if (inode == NULL) {
2795 dout("handle_lease no inode %llx\n", vino.ino);
2800 parent = d_find_alias(inode);
2802 dout("no parent dentry on inode %p\n", inode);
2804 goto release; /* hrm... */
2806 dname.hash = full_name_hash(dname.name, dname.len);
2807 dentry = d_lookup(parent, &dname);
2812 spin_lock(&dentry->d_lock);
2813 di = ceph_dentry(dentry);
2814 switch (h->action) {
2815 case CEPH_MDS_LEASE_REVOKE:
2816 if (di->lease_session == session) {
2817 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2818 h->seq = cpu_to_le32(di->lease_seq);
2819 __ceph_mdsc_drop_dentry_lease(dentry);
2824 case CEPH_MDS_LEASE_RENEW:
2825 if (di->lease_session == session &&
2826 di->lease_gen == session->s_cap_gen &&
2827 di->lease_renew_from &&
2828 di->lease_renew_after == 0) {
2829 unsigned long duration =
2830 le32_to_cpu(h->duration_ms) * HZ / 1000;
2832 di->lease_seq = seq;
2833 dentry->d_time = di->lease_renew_from + duration;
2834 di->lease_renew_after = di->lease_renew_from +
2836 di->lease_renew_from = 0;
2840 spin_unlock(&dentry->d_lock);
2847 /* let's just reuse the same message */
2848 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2850 ceph_con_send(&session->s_con, msg);
2854 mutex_unlock(&session->s_mutex);
2858 pr_err("corrupt lease message\n");
2862 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2863 struct inode *inode,
2864 struct dentry *dentry, char action,
2867 struct ceph_msg *msg;
2868 struct ceph_mds_lease *lease;
2869 int len = sizeof(*lease) + sizeof(u32);
2872 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2873 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2874 dnamelen = dentry->d_name.len;
2877 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
2880 lease = msg->front.iov_base;
2881 lease->action = action;
2882 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2883 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2884 lease->seq = cpu_to_le32(seq);
2885 put_unaligned_le32(dnamelen, lease + 1);
2886 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2889 * if this is a preemptive lease RELEASE, no need to
2890 * flush request stream, since the actual request will
2893 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2895 ceph_con_send(&session->s_con, msg);
2899 * Preemptively release a lease we expect to invalidate anyway.
2900 * Pass @inode always, @dentry is optional.
2902 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2903 struct dentry *dentry)
2905 struct ceph_dentry_info *di;
2906 struct ceph_mds_session *session;
2909 BUG_ON(inode == NULL);
2910 BUG_ON(dentry == NULL);
2912 /* is dentry lease valid? */
2913 spin_lock(&dentry->d_lock);
2914 di = ceph_dentry(dentry);
2915 if (!di || !di->lease_session ||
2916 di->lease_session->s_mds < 0 ||
2917 di->lease_gen != di->lease_session->s_cap_gen ||
2918 !time_before(jiffies, dentry->d_time)) {
2919 dout("lease_release inode %p dentry %p -- "
2922 spin_unlock(&dentry->d_lock);
2926 /* we do have a lease on this dentry; note mds and seq */
2927 session = ceph_get_mds_session(di->lease_session);
2928 seq = di->lease_seq;
2929 __ceph_mdsc_drop_dentry_lease(dentry);
2930 spin_unlock(&dentry->d_lock);
2932 dout("lease_release inode %p dentry %p to mds%d\n",
2933 inode, dentry, session->s_mds);
2934 ceph_mdsc_lease_send_msg(session, inode, dentry,
2935 CEPH_MDS_LEASE_RELEASE, seq);
2936 ceph_put_mds_session(session);
2940 * drop all leases (and dentry refs) in preparation for umount
2942 static void drop_leases(struct ceph_mds_client *mdsc)
2946 dout("drop_leases\n");
2947 mutex_lock(&mdsc->mutex);
2948 for (i = 0; i < mdsc->max_sessions; i++) {
2949 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2952 mutex_unlock(&mdsc->mutex);
2953 mutex_lock(&s->s_mutex);
2954 mutex_unlock(&s->s_mutex);
2955 ceph_put_mds_session(s);
2956 mutex_lock(&mdsc->mutex);
2958 mutex_unlock(&mdsc->mutex);
2964 * delayed work -- periodically trim expired leases, renew caps with mds
2966 static void schedule_delayed(struct ceph_mds_client *mdsc)
2969 unsigned hz = round_jiffies_relative(HZ * delay);
2970 schedule_delayed_work(&mdsc->delayed_work, hz);
2973 static void delayed_work(struct work_struct *work)
2976 struct ceph_mds_client *mdsc =
2977 container_of(work, struct ceph_mds_client, delayed_work.work);
2981 dout("mdsc delayed_work\n");
2982 ceph_check_delayed_caps(mdsc);
2984 mutex_lock(&mdsc->mutex);
2985 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2986 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2987 mdsc->last_renew_caps);
2989 mdsc->last_renew_caps = jiffies;
2991 for (i = 0; i < mdsc->max_sessions; i++) {
2992 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2995 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2996 dout("resending session close request for mds%d\n",
2998 request_close_session(mdsc, s);
2999 ceph_put_mds_session(s);
3002 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3003 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3004 s->s_state = CEPH_MDS_SESSION_HUNG;
3005 pr_info("mds%d hung\n", s->s_mds);
3008 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3009 /* this mds is failed or recovering, just wait */
3010 ceph_put_mds_session(s);
3013 mutex_unlock(&mdsc->mutex);
3015 mutex_lock(&s->s_mutex);
3017 send_renew_caps(mdsc, s);
3019 ceph_con_keepalive(&s->s_con);
3020 ceph_add_cap_releases(mdsc, s);
3021 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3022 s->s_state == CEPH_MDS_SESSION_HUNG)
3023 ceph_send_cap_releases(mdsc, s);
3024 mutex_unlock(&s->s_mutex);
3025 ceph_put_mds_session(s);
3027 mutex_lock(&mdsc->mutex);
3029 mutex_unlock(&mdsc->mutex);
3031 schedule_delayed(mdsc);
3034 int ceph_mdsc_init(struct ceph_fs_client *fsc)
3037 struct ceph_mds_client *mdsc;
3039 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3044 mutex_init(&mdsc->mutex);
3045 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
3046 if (mdsc->mdsmap == NULL)
3049 init_completion(&mdsc->safe_umount_waiters);
3050 init_waitqueue_head(&mdsc->session_close_wq);
3051 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3052 mdsc->sessions = NULL;
3053 mdsc->max_sessions = 0;
3055 init_rwsem(&mdsc->snap_rwsem);
3056 mdsc->snap_realms = RB_ROOT;
3057 INIT_LIST_HEAD(&mdsc->snap_empty);
3058 spin_lock_init(&mdsc->snap_empty_lock);
3060 mdsc->request_tree = RB_ROOT;
3061 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3062 mdsc->last_renew_caps = jiffies;
3063 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3064 spin_lock_init(&mdsc->cap_delay_lock);
3065 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3066 spin_lock_init(&mdsc->snap_flush_lock);
3067 mdsc->cap_flush_seq = 0;
3068 INIT_LIST_HEAD(&mdsc->cap_dirty);
3069 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
3070 mdsc->num_cap_flushing = 0;
3071 spin_lock_init(&mdsc->cap_dirty_lock);
3072 init_waitqueue_head(&mdsc->cap_flushing_wq);
3073 spin_lock_init(&mdsc->dentry_lru_lock);
3074 INIT_LIST_HEAD(&mdsc->dentry_lru);
3076 ceph_caps_init(mdsc);
3077 ceph_adjust_min_caps(mdsc, fsc->min_caps);
3083 * Wait for safe replies on open mds requests. If we time out, drop
3084 * all requests from the tree to avoid dangling dentry refs.
3086 static void wait_requests(struct ceph_mds_client *mdsc)
3088 struct ceph_mds_request *req;
3089 struct ceph_fs_client *fsc = mdsc->fsc;
3091 mutex_lock(&mdsc->mutex);
3092 if (__get_oldest_req(mdsc)) {
3093 mutex_unlock(&mdsc->mutex);
3095 dout("wait_requests waiting for requests\n");
3096 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
3097 fsc->client->options->mount_timeout * HZ);
3099 /* tear down remaining requests */
3100 mutex_lock(&mdsc->mutex);
3101 while ((req = __get_oldest_req(mdsc))) {
3102 dout("wait_requests timed out on tid %llu\n",
3104 __unregister_request(mdsc, req);
3107 mutex_unlock(&mdsc->mutex);
3108 dout("wait_requests done\n");
3112 * called before mount is ro, and before dentries are torn down.
3113 * (hmm, does this still race with new lookups?)
3115 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3117 dout("pre_umount\n");
3121 ceph_flush_dirty_caps(mdsc);
3122 wait_requests(mdsc);
3125 * wait for reply handlers to drop their request refs and
3126 * their inode/dcache refs
3132 * wait for all write mds requests to flush.
3134 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3136 struct ceph_mds_request *req = NULL, *nextreq;
3139 mutex_lock(&mdsc->mutex);
3140 dout("wait_unsafe_requests want %lld\n", want_tid);
3142 req = __get_oldest_req(mdsc);
3143 while (req && req->r_tid <= want_tid) {
3144 /* find next request */
3145 n = rb_next(&req->r_node);
3147 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3150 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3152 ceph_mdsc_get_request(req);
3154 ceph_mdsc_get_request(nextreq);
3155 mutex_unlock(&mdsc->mutex);
3156 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3157 req->r_tid, want_tid);
3158 wait_for_completion(&req->r_safe_completion);
3159 mutex_lock(&mdsc->mutex);
3160 ceph_mdsc_put_request(req);
3162 break; /* next dne before, so we're done! */
3163 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3164 /* next request was removed from tree */
3165 ceph_mdsc_put_request(nextreq);
3168 ceph_mdsc_put_request(nextreq); /* won't go away */
3172 mutex_unlock(&mdsc->mutex);
3173 dout("wait_unsafe_requests done\n");
3176 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3178 u64 want_tid, want_flush;
3180 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3184 mutex_lock(&mdsc->mutex);
3185 want_tid = mdsc->last_tid;
3186 want_flush = mdsc->cap_flush_seq;
3187 mutex_unlock(&mdsc->mutex);
3188 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3190 ceph_flush_dirty_caps(mdsc);
3192 wait_unsafe_requests(mdsc, want_tid);
3193 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3197 * true if all sessions are closed, or we force unmount
3199 static bool done_closing_sessions(struct ceph_mds_client *mdsc)
3203 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3206 mutex_lock(&mdsc->mutex);
3207 for (i = 0; i < mdsc->max_sessions; i++)
3208 if (mdsc->sessions[i])
3210 mutex_unlock(&mdsc->mutex);
3215 * called after sb is ro.
3217 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3219 struct ceph_mds_session *session;
3221 struct ceph_fs_client *fsc = mdsc->fsc;
3222 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
3224 dout("close_sessions\n");
3226 /* close sessions */
3227 mutex_lock(&mdsc->mutex);
3228 for (i = 0; i < mdsc->max_sessions; i++) {
3229 session = __ceph_lookup_mds_session(mdsc, i);
3232 mutex_unlock(&mdsc->mutex);
3233 mutex_lock(&session->s_mutex);
3234 __close_session(mdsc, session);
3235 mutex_unlock(&session->s_mutex);
3236 ceph_put_mds_session(session);
3237 mutex_lock(&mdsc->mutex);
3239 mutex_unlock(&mdsc->mutex);
3241 dout("waiting for sessions to close\n");
3242 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3245 /* tear down remaining sessions */
3246 mutex_lock(&mdsc->mutex);
3247 for (i = 0; i < mdsc->max_sessions; i++) {
3248 if (mdsc->sessions[i]) {
3249 session = get_session(mdsc->sessions[i]);
3250 __unregister_session(mdsc, session);
3251 mutex_unlock(&mdsc->mutex);
3252 mutex_lock(&session->s_mutex);
3253 remove_session_caps(session);
3254 mutex_unlock(&session->s_mutex);
3255 ceph_put_mds_session(session);
3256 mutex_lock(&mdsc->mutex);
3259 WARN_ON(!list_empty(&mdsc->cap_delay_list));
3260 mutex_unlock(&mdsc->mutex);
3262 ceph_cleanup_empty_realms(mdsc);
3264 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3269 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
3272 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3274 ceph_mdsmap_destroy(mdsc->mdsmap);
3275 kfree(mdsc->sessions);
3276 ceph_caps_finalize(mdsc);
3279 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3281 struct ceph_mds_client *mdsc = fsc->mdsc;
3283 dout("mdsc_destroy %p\n", mdsc);
3284 ceph_mdsc_stop(mdsc);
3286 /* flush out any connection work with references to us */
3291 dout("mdsc_destroy %p done\n", mdsc);
3296 * handle mds map update.
3298 void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3302 void *p = msg->front.iov_base;
3303 void *end = p + msg->front.iov_len;
3304 struct ceph_mdsmap *newmap, *oldmap;
3305 struct ceph_fsid fsid;
3308 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3309 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3310 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
3312 epoch = ceph_decode_32(&p);
3313 maplen = ceph_decode_32(&p);
3314 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3316 /* do we need it? */
3317 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
3318 mutex_lock(&mdsc->mutex);
3319 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3320 dout("handle_map epoch %u <= our %u\n",
3321 epoch, mdsc->mdsmap->m_epoch);
3322 mutex_unlock(&mdsc->mutex);
3326 newmap = ceph_mdsmap_decode(&p, end);
3327 if (IS_ERR(newmap)) {
3328 err = PTR_ERR(newmap);
3332 /* swap into place */
3334 oldmap = mdsc->mdsmap;
3335 mdsc->mdsmap = newmap;
3336 check_new_map(mdsc, newmap, oldmap);
3337 ceph_mdsmap_destroy(oldmap);
3339 mdsc->mdsmap = newmap; /* first mds map */
3341 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
3343 __wake_requests(mdsc, &mdsc->waiting_for_map);
3345 mutex_unlock(&mdsc->mutex);
3346 schedule_delayed(mdsc);
3350 mutex_unlock(&mdsc->mutex);
3352 pr_err("error decoding mdsmap %d\n", err);
3356 static struct ceph_connection *con_get(struct ceph_connection *con)
3358 struct ceph_mds_session *s = con->private;
3360 if (get_session(s)) {
3361 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
3364 dout("mdsc con_get %p FAIL\n", s);
3368 static void con_put(struct ceph_connection *con)
3370 struct ceph_mds_session *s = con->private;
3372 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref) - 1);
3373 ceph_put_mds_session(s);
3377 * if the client is unresponsive for long enough, the mds will kill
3378 * the session entirely.
3380 static void peer_reset(struct ceph_connection *con)
3382 struct ceph_mds_session *s = con->private;
3383 struct ceph_mds_client *mdsc = s->s_mdsc;
3385 pr_warning("mds%d closed our session\n", s->s_mds);
3386 send_mds_reconnect(mdsc, s);
3389 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3391 struct ceph_mds_session *s = con->private;
3392 struct ceph_mds_client *mdsc = s->s_mdsc;
3393 int type = le16_to_cpu(msg->hdr.type);
3395 mutex_lock(&mdsc->mutex);
3396 if (__verify_registered_session(mdsc, s) < 0) {
3397 mutex_unlock(&mdsc->mutex);
3400 mutex_unlock(&mdsc->mutex);
3403 case CEPH_MSG_MDS_MAP:
3404 ceph_mdsc_handle_map(mdsc, msg);
3406 case CEPH_MSG_CLIENT_SESSION:
3407 handle_session(s, msg);
3409 case CEPH_MSG_CLIENT_REPLY:
3410 handle_reply(s, msg);
3412 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
3413 handle_forward(mdsc, s, msg);
3415 case CEPH_MSG_CLIENT_CAPS:
3416 ceph_handle_caps(s, msg);
3418 case CEPH_MSG_CLIENT_SNAP:
3419 ceph_handle_snap(mdsc, s, msg);
3421 case CEPH_MSG_CLIENT_LEASE:
3422 handle_lease(mdsc, s, msg);
3426 pr_err("received unknown message type %d %s\n", type,
3427 ceph_msg_type_name(type));
3438 * Note: returned pointer is the address of a structure that's
3439 * managed separately. Caller must *not* attempt to free it.
3441 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
3442 int *proto, int force_new)
3444 struct ceph_mds_session *s = con->private;
3445 struct ceph_mds_client *mdsc = s->s_mdsc;
3446 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3447 struct ceph_auth_handshake *auth = &s->s_auth;
3449 if (force_new && auth->authorizer) {
3450 ceph_auth_destroy_authorizer(ac, auth->authorizer);
3451 auth->authorizer = NULL;
3453 if (!auth->authorizer) {
3454 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3457 return ERR_PTR(ret);
3459 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3462 return ERR_PTR(ret);
3464 *proto = ac->protocol;
3470 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3472 struct ceph_mds_session *s = con->private;
3473 struct ceph_mds_client *mdsc = s->s_mdsc;
3474 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3476 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer, len);
3479 static int invalidate_authorizer(struct ceph_connection *con)
3481 struct ceph_mds_session *s = con->private;
3482 struct ceph_mds_client *mdsc = s->s_mdsc;
3483 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3485 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3487 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
3490 static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
3491 struct ceph_msg_header *hdr, int *skip)
3493 struct ceph_msg *msg;
3494 int type = (int) le16_to_cpu(hdr->type);
3495 int front_len = (int) le32_to_cpu(hdr->front_len);
3501 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
3503 pr_err("unable to allocate msg type %d len %d\n",
3511 static const struct ceph_connection_operations mds_con_ops = {
3514 .dispatch = dispatch,
3515 .get_authorizer = get_authorizer,
3516 .verify_authorizer_reply = verify_authorizer_reply,
3517 .invalidate_authorizer = invalidate_authorizer,
3518 .peer_reset = peer_reset,
3519 .alloc_msg = mds_alloc_msg,