1 #include "ceph_debug.h"
3 #include <linux/wait.h>
4 #include <linux/sched.h>
6 #include "mds_client.h"
7 #include "mon_client.h"
13 * A cluster of MDS (metadata server) daemons is responsible for
14 * managing the file system namespace (the directory hierarchy and
15 * inodes) and for coordinating shared access to storage. Metadata is
16 * partitioning hierarchically across a number of servers, and that
17 * partition varies over time as the cluster adjusts the distribution
18 * in order to balance load.
20 * The MDS client is primarily responsible to managing synchronous
21 * metadata requests for operations like open, unlink, and so forth.
22 * If there is a MDS failure, we find out about it when we (possibly
23 * request and) receive a new MDS map, and can resubmit affected
26 * For the most part, though, we take advantage of a lossless
27 * communications channel to the MDS, and do not need to worry about
28 * timing out or resubmitting requests.
30 * We maintain a stateful "session" with each MDS we interact with.
31 * Within each session, we sent periodic heartbeat messages to ensure
32 * any capabilities or leases we have been issues remain valid. If
33 * the session times out and goes stale, our leases and capabilities
34 * are no longer valid.
37 static void __wake_requests(struct ceph_mds_client *mdsc,
38 struct list_head *head);
40 const static struct ceph_connection_operations mds_con_ops;
48 * parse individual inode info
50 static int parse_reply_info_in(void **p, void *end,
51 struct ceph_mds_reply_info_in *info)
56 *p += sizeof(struct ceph_mds_reply_inode) +
57 sizeof(*info->in->fragtree.splits) *
58 le32_to_cpu(info->in->fragtree.nsplits);
60 ceph_decode_32_safe(p, end, info->symlink_len, bad);
61 ceph_decode_need(p, end, info->symlink_len, bad);
63 *p += info->symlink_len;
65 ceph_decode_32_safe(p, end, info->xattr_len, bad);
66 ceph_decode_need(p, end, info->xattr_len, bad);
67 info->xattr_data = *p;
68 *p += info->xattr_len;
75 * parse a normal reply, which may contain a (dir+)dentry and/or a
78 static int parse_reply_info_trace(void **p, void *end,
79 struct ceph_mds_reply_info_parsed *info)
83 if (info->head->is_dentry) {
84 err = parse_reply_info_in(p, end, &info->diri);
88 if (unlikely(*p + sizeof(*info->dirfrag) > end))
91 *p += sizeof(*info->dirfrag) +
92 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
93 if (unlikely(*p > end))
96 ceph_decode_32_safe(p, end, info->dname_len, bad);
97 ceph_decode_need(p, end, info->dname_len, bad);
99 *p += info->dname_len;
101 *p += sizeof(*info->dlease);
104 if (info->head->is_target) {
105 err = parse_reply_info_in(p, end, &info->targeti);
110 if (unlikely(*p != end))
117 pr_err("problem parsing mds trace %d\n", err);
122 * parse readdir results
124 static int parse_reply_info_dir(void **p, void *end,
125 struct ceph_mds_reply_info_parsed *info)
131 if (*p + sizeof(*info->dir_dir) > end)
133 *p += sizeof(*info->dir_dir) +
134 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
138 ceph_decode_need(p, end, sizeof(num) + 2, bad);
139 ceph_decode_32(p, num);
140 ceph_decode_8(p, info->dir_end);
141 ceph_decode_8(p, info->dir_complete);
145 /* alloc large array */
147 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
148 sizeof(*info->dir_dname) +
149 sizeof(*info->dir_dname_len) +
150 sizeof(*info->dir_dlease),
152 if (info->dir_in == NULL) {
156 info->dir_dname = (void *)(info->dir_in + num);
157 info->dir_dname_len = (void *)(info->dir_dname + num);
158 info->dir_dlease = (void *)(info->dir_dname_len + num);
162 ceph_decode_need(p, end, sizeof(u32)*2, bad);
163 ceph_decode_32(p, info->dir_dname_len[i]);
164 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
165 info->dir_dname[i] = *p;
166 *p += info->dir_dname_len[i];
167 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
169 info->dir_dlease[i] = *p;
170 *p += sizeof(struct ceph_mds_reply_lease);
173 err = parse_reply_info_in(p, end, &info->dir_in[i]);
188 pr_err("problem parsing dir contents %d\n", err);
193 * parse entire mds reply
195 static int parse_reply_info(struct ceph_msg *msg,
196 struct ceph_mds_reply_info_parsed *info)
202 info->head = msg->front.iov_base;
203 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
204 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
207 ceph_decode_32_safe(&p, end, len, bad);
209 err = parse_reply_info_trace(&p, p+len, info);
215 ceph_decode_32_safe(&p, end, len, bad);
217 err = parse_reply_info_dir(&p, p+len, info);
223 ceph_decode_32_safe(&p, end, len, bad);
224 info->snapblob_len = len;
235 pr_err("mds parse_reply err %d\n", err);
239 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
248 static const char *session_state_name(int s)
251 case CEPH_MDS_SESSION_NEW: return "new";
252 case CEPH_MDS_SESSION_OPENING: return "opening";
253 case CEPH_MDS_SESSION_OPEN: return "open";
254 case CEPH_MDS_SESSION_HUNG: return "hung";
255 case CEPH_MDS_SESSION_CLOSING: return "closing";
256 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
257 default: return "???";
261 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
263 if (atomic_inc_not_zero(&s->s_ref)) {
264 dout("mdsc get_session %p %d -> %d\n", s,
265 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
268 dout("mdsc get_session %p 0 -- FAIL", s);
273 void ceph_put_mds_session(struct ceph_mds_session *s)
275 dout("mdsc put_session %p %d -> %d\n", s,
276 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
277 if (atomic_dec_and_test(&s->s_ref)) {
278 ceph_con_shutdown(&s->s_con);
284 * called under mdsc->mutex
286 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
289 struct ceph_mds_session *session;
291 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
293 session = mdsc->sessions[mds];
294 dout("lookup_mds_session %p %d\n", session,
295 atomic_read(&session->s_ref));
296 get_session(session);
300 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
302 if (mds >= mdsc->max_sessions)
304 return mdsc->sessions[mds];
308 * create+register a new session for given mds.
309 * called under mdsc->mutex.
311 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
314 struct ceph_mds_session *s;
316 s = kzalloc(sizeof(*s), GFP_NOFS);
319 s->s_state = CEPH_MDS_SESSION_NEW;
322 mutex_init(&s->s_mutex);
324 ceph_con_init(mdsc->client->msgr, &s->s_con);
325 s->s_con.private = s;
326 s->s_con.ops = &mds_con_ops;
327 s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
328 s->s_con.peer_name.num = cpu_to_le64(mds);
329 ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
331 spin_lock_init(&s->s_cap_lock);
334 s->s_renew_requested = 0;
336 INIT_LIST_HEAD(&s->s_caps);
338 atomic_set(&s->s_ref, 1);
339 INIT_LIST_HEAD(&s->s_waiting);
340 INIT_LIST_HEAD(&s->s_unsafe);
341 s->s_num_cap_releases = 0;
342 INIT_LIST_HEAD(&s->s_cap_releases);
343 INIT_LIST_HEAD(&s->s_cap_releases_done);
344 INIT_LIST_HEAD(&s->s_cap_flushing);
345 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
347 dout("register_session mds%d\n", mds);
348 if (mds >= mdsc->max_sessions) {
349 int newmax = 1 << get_count_order(mds+1);
350 struct ceph_mds_session **sa;
352 dout("register_session realloc to %d\n", newmax);
353 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
355 return ERR_PTR(-ENOMEM);
356 if (mdsc->sessions) {
357 memcpy(sa, mdsc->sessions,
358 mdsc->max_sessions * sizeof(void *));
359 kfree(mdsc->sessions);
362 mdsc->max_sessions = newmax;
364 mdsc->sessions[mds] = s;
365 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
370 * called under mdsc->mutex
372 static void unregister_session(struct ceph_mds_client *mdsc, int mds)
374 dout("unregister_session mds%d %p\n", mds, mdsc->sessions[mds]);
375 ceph_put_mds_session(mdsc->sessions[mds]);
376 mdsc->sessions[mds] = NULL;
380 * drop session refs in request.
382 * should be last request ref, or hold mdsc->mutex
384 static void put_request_session(struct ceph_mds_request *req)
386 if (req->r_session) {
387 ceph_put_mds_session(req->r_session);
388 req->r_session = NULL;
392 void ceph_mdsc_put_request(struct ceph_mds_request *req)
394 dout("mdsc put_request %p %d -> %d\n", req,
395 atomic_read(&req->r_ref), atomic_read(&req->r_ref)-1);
396 if (atomic_dec_and_test(&req->r_ref)) {
398 ceph_msg_put(req->r_request);
400 ceph_msg_put(req->r_reply);
401 destroy_reply_info(&req->r_reply_info);
404 ceph_put_cap_refs(ceph_inode(req->r_inode),
408 if (req->r_locked_dir)
409 ceph_put_cap_refs(ceph_inode(req->r_locked_dir),
411 if (req->r_target_inode)
412 iput(req->r_target_inode);
415 if (req->r_old_dentry) {
417 ceph_inode(req->r_old_dentry->d_parent->d_inode),
419 dput(req->r_old_dentry);
423 put_request_session(req);
424 ceph_unreserve_caps(&req->r_caps_reservation);
430 * lookup session, bump ref if found.
432 * called under mdsc->mutex.
434 static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
437 struct ceph_mds_request *req;
438 req = radix_tree_lookup(&mdsc->request_tree, tid);
440 ceph_mdsc_get_request(req);
445 * Register an in-flight request, and assign a tid. Link to directory
446 * are modifying (if any).
448 * Called under mdsc->mutex.
450 static void __register_request(struct ceph_mds_client *mdsc,
451 struct ceph_mds_request *req,
454 req->r_tid = ++mdsc->last_tid;
456 ceph_reserve_caps(&req->r_caps_reservation, req->r_num_caps);
457 dout("__register_request %p tid %lld\n", req, req->r_tid);
458 ceph_mdsc_get_request(req);
459 radix_tree_insert(&mdsc->request_tree, req->r_tid, (void *)req);
462 struct ceph_inode_info *ci = ceph_inode(dir);
464 spin_lock(&ci->i_unsafe_lock);
465 req->r_unsafe_dir = dir;
466 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
467 spin_unlock(&ci->i_unsafe_lock);
471 static void __unregister_request(struct ceph_mds_client *mdsc,
472 struct ceph_mds_request *req)
474 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
475 radix_tree_delete(&mdsc->request_tree, req->r_tid);
476 ceph_mdsc_put_request(req);
478 if (req->r_unsafe_dir) {
479 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
481 spin_lock(&ci->i_unsafe_lock);
482 list_del_init(&req->r_unsafe_dir_item);
483 spin_unlock(&ci->i_unsafe_lock);
488 * Choose mds to send request to next. If there is a hint set in the
489 * request (e.g., due to a prior forward hint from the mds), use that.
490 * Otherwise, consult frag tree and/or caps to identify the
491 * appropriate mds. If all else fails, choose randomly.
493 * Called under mdsc->mutex.
495 static int __choose_mds(struct ceph_mds_client *mdsc,
496 struct ceph_mds_request *req)
499 struct ceph_inode_info *ci;
500 struct ceph_cap *cap;
501 int mode = req->r_direct_mode;
503 u32 hash = req->r_direct_hash;
504 bool is_hash = req->r_direct_is_hash;
507 * is there a specific mds we should try? ignore hint if we have
508 * no session and the mds is not up (active or recovering).
510 if (req->r_resend_mds >= 0 &&
511 (__have_session(mdsc, req->r_resend_mds) ||
512 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
513 dout("choose_mds using resend_mds mds%d\n",
515 return req->r_resend_mds;
518 if (mode == USE_RANDOM_MDS)
523 inode = req->r_inode;
524 } else if (req->r_dentry) {
525 if (req->r_dentry->d_inode) {
526 inode = req->r_dentry->d_inode;
528 inode = req->r_dentry->d_parent->d_inode;
529 hash = req->r_dentry->d_name.hash;
533 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
537 ci = ceph_inode(inode);
539 if (is_hash && S_ISDIR(inode->i_mode)) {
540 struct ceph_inode_frag frag;
543 ceph_choose_frag(ci, hash, &frag, &found);
545 if (mode == USE_ANY_MDS && frag.ndist > 0) {
548 /* choose a random replica */
549 get_random_bytes(&r, 1);
552 dout("choose_mds %p %llx.%llx "
553 "frag %u mds%d (%d/%d)\n",
554 inode, ceph_vinop(inode),
560 /* since this file/dir wasn't known to be
561 * replicated, then we want to look for the
562 * authoritative mds. */
565 /* choose auth mds */
567 dout("choose_mds %p %llx.%llx "
568 "frag %u mds%d (auth)\n",
569 inode, ceph_vinop(inode), frag.frag, mds);
575 spin_lock(&inode->i_lock);
577 if (mode == USE_AUTH_MDS)
578 cap = ci->i_auth_cap;
579 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
580 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
582 spin_unlock(&inode->i_lock);
585 mds = cap->session->s_mds;
586 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
587 inode, ceph_vinop(inode), mds,
588 cap == ci->i_auth_cap ? "auth " : "", cap);
589 spin_unlock(&inode->i_lock);
593 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
594 dout("choose_mds chose random mds%d\n", mds);
602 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
604 struct ceph_msg *msg;
605 struct ceph_mds_session_head *h;
607 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), 0, 0, NULL);
609 pr_err("create_session_msg ENOMEM creating msg\n");
610 return ERR_PTR(PTR_ERR(msg));
612 h = msg->front.iov_base;
613 h->op = cpu_to_le32(op);
614 h->seq = cpu_to_le64(seq);
619 * send session open request.
621 * called under mdsc->mutex
623 static int __open_session(struct ceph_mds_client *mdsc,
624 struct ceph_mds_session *session)
626 struct ceph_msg *msg;
628 int mds = session->s_mds;
631 /* wait for mds to go active? */
632 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
633 dout("open_session to mds%d (%s)\n", mds,
634 ceph_mds_state_name(mstate));
635 session->s_state = CEPH_MDS_SESSION_OPENING;
636 session->s_renew_requested = jiffies;
638 /* send connect message */
639 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
644 ceph_con_send(&session->s_con, msg);
655 * Free preallocated cap messages assigned to this session
657 static void cleanup_cap_releases(struct ceph_mds_session *session)
659 struct ceph_msg *msg;
661 spin_lock(&session->s_cap_lock);
662 while (!list_empty(&session->s_cap_releases)) {
663 msg = list_first_entry(&session->s_cap_releases,
664 struct ceph_msg, list_head);
665 list_del_init(&msg->list_head);
668 while (!list_empty(&session->s_cap_releases_done)) {
669 msg = list_first_entry(&session->s_cap_releases_done,
670 struct ceph_msg, list_head);
671 list_del_init(&msg->list_head);
674 spin_unlock(&session->s_cap_lock);
678 * Helper to safely iterate over all caps associated with a session.
680 * caller must hold session s_mutex
682 static int iterate_session_caps(struct ceph_mds_session *session,
683 int (*cb)(struct inode *, struct ceph_cap *,
686 struct ceph_cap *cap, *ncap;
690 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
691 spin_lock(&session->s_cap_lock);
692 list_for_each_entry_safe(cap, ncap, &session->s_caps, session_caps) {
693 inode = igrab(&cap->ci->vfs_inode);
696 spin_unlock(&session->s_cap_lock);
697 ret = cb(inode, cap, arg);
701 spin_lock(&session->s_cap_lock);
703 spin_unlock(&session->s_cap_lock);
708 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
711 struct ceph_inode_info *ci = ceph_inode(inode);
712 dout("removing cap %p, ci is %p, inode is %p\n",
713 cap, ci, &ci->vfs_inode);
714 ceph_remove_cap(cap);
719 * caller must hold session s_mutex
721 static void remove_session_caps(struct ceph_mds_session *session)
723 dout("remove_session_caps on %p\n", session);
724 iterate_session_caps(session, remove_session_caps_cb, NULL);
725 BUG_ON(session->s_nr_caps > 0);
726 cleanup_cap_releases(session);
730 * wake up any threads waiting on this session's caps. if the cap is
731 * old (didn't get renewed on the client reconnect), remove it now.
733 * caller must hold s_mutex.
735 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
738 struct ceph_mds_session *session = arg;
740 spin_lock(&inode->i_lock);
741 if (cap->gen != session->s_cap_gen) {
742 pr_err("failed reconnect %p %llx.%llx cap %p "
743 "(gen %d < session %d)\n", inode, ceph_vinop(inode),
744 cap, cap->gen, session->s_cap_gen);
745 __ceph_remove_cap(cap, NULL);
747 wake_up(&ceph_inode(inode)->i_cap_wq);
748 spin_unlock(&inode->i_lock);
752 static void wake_up_session_caps(struct ceph_mds_session *session)
754 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
755 iterate_session_caps(session, wake_up_session_cb, session);
759 * Send periodic message to MDS renewing all currently held caps. The
760 * ack will reset the expiration for all caps from this session.
762 * caller holds s_mutex
764 static int send_renew_caps(struct ceph_mds_client *mdsc,
765 struct ceph_mds_session *session)
767 struct ceph_msg *msg;
770 if (time_after_eq(jiffies, session->s_cap_ttl) &&
771 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
772 pr_info("mds%d caps stale\n", session->s_mds);
774 /* do not try to renew caps until a recovering mds has reconnected
775 * with its clients. */
776 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
777 if (state < CEPH_MDS_STATE_RECONNECT) {
778 dout("send_renew_caps ignoring mds%d (%s)\n",
779 session->s_mds, ceph_mds_state_name(state));
783 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
784 ceph_mds_state_name(state));
785 session->s_renew_requested = jiffies;
786 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
787 ++session->s_renew_seq);
790 ceph_con_send(&session->s_con, msg);
795 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
797 static void renewed_caps(struct ceph_mds_client *mdsc,
798 struct ceph_mds_session *session, int is_renew)
803 spin_lock(&session->s_cap_lock);
804 was_stale = is_renew && (session->s_cap_ttl == 0 ||
805 time_after_eq(jiffies, session->s_cap_ttl));
807 session->s_cap_ttl = session->s_renew_requested +
808 mdsc->mdsmap->m_session_timeout*HZ;
811 if (time_before(jiffies, session->s_cap_ttl)) {
812 pr_info("mds%d caps renewed\n", session->s_mds);
815 pr_info("mds%d caps still stale\n", session->s_mds);
818 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
819 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
820 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
821 spin_unlock(&session->s_cap_lock);
824 wake_up_session_caps(session);
828 * send a session close request
830 static int request_close_session(struct ceph_mds_client *mdsc,
831 struct ceph_mds_session *session)
833 struct ceph_msg *msg;
836 dout("request_close_session mds%d state %s seq %lld\n",
837 session->s_mds, session_state_name(session->s_state),
839 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
843 ceph_con_send(&session->s_con, msg);
848 * Called with s_mutex held.
850 static int __close_session(struct ceph_mds_client *mdsc,
851 struct ceph_mds_session *session)
853 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
855 session->s_state = CEPH_MDS_SESSION_CLOSING;
856 return request_close_session(mdsc, session);
862 * Because we can't cache an inode without one or more caps, we do
863 * this indirectly: if a cap is unused, we prune its aliases, at which
864 * point the inode will hopefully get dropped to.
866 * Yes, this is a bit sloppy. Our only real goal here is to respond to
867 * memory pressure from the MDS, though, so it needn't be perfect.
869 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
871 struct ceph_mds_session *session = arg;
872 struct ceph_inode_info *ci = ceph_inode(inode);
873 int used, oissued, mine;
875 if (session->s_trim_caps <= 0)
878 spin_lock(&inode->i_lock);
879 mine = cap->issued | cap->implemented;
880 used = __ceph_caps_used(ci);
881 oissued = __ceph_caps_issued_other(ci, cap);
883 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
884 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
885 ceph_cap_string(used));
886 if (ci->i_dirty_caps)
887 goto out; /* dirty caps */
888 if ((used & ~oissued) & mine)
889 goto out; /* we need these caps */
891 session->s_trim_caps--;
893 /* we aren't the only cap.. just remove us */
894 __ceph_remove_cap(cap, NULL);
896 /* try to drop referring dentries */
897 spin_unlock(&inode->i_lock);
898 d_prune_aliases(inode);
899 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
900 inode, cap, atomic_read(&inode->i_count));
905 spin_unlock(&inode->i_lock);
910 * Trim session cap count down to some max number.
912 static int trim_caps(struct ceph_mds_client *mdsc,
913 struct ceph_mds_session *session,
916 int trim_caps = session->s_nr_caps - max_caps;
918 dout("trim_caps mds%d start: %d / %d, trim %d\n",
919 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
921 session->s_trim_caps = trim_caps;
922 iterate_session_caps(session, trim_caps_cb, session);
923 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
924 session->s_mds, session->s_nr_caps, max_caps,
925 trim_caps - session->s_trim_caps);
931 * Allocate cap_release messages. If there is a partially full message
932 * in the queue, try to allocate enough to cover it's remainder, so that
933 * we can send it immediately.
935 * Called under s_mutex.
937 static int add_cap_releases(struct ceph_mds_client *mdsc,
938 struct ceph_mds_session *session,
941 struct ceph_msg *msg;
942 struct ceph_mds_cap_release *head;
946 extra = mdsc->client->mount_args.cap_release_safety;
948 spin_lock(&session->s_cap_lock);
950 if (!list_empty(&session->s_cap_releases)) {
951 msg = list_first_entry(&session->s_cap_releases,
954 head = msg->front.iov_base;
955 extra += CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num);
958 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
959 spin_unlock(&session->s_cap_lock);
960 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
964 dout("add_cap_releases %p msg %p now %d\n", session, msg,
965 (int)msg->front.iov_len);
966 head = msg->front.iov_base;
967 head->num = cpu_to_le32(0);
968 msg->front.iov_len = sizeof(*head);
969 spin_lock(&session->s_cap_lock);
970 list_add(&msg->list_head, &session->s_cap_releases);
971 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
974 if (!list_empty(&session->s_cap_releases)) {
975 msg = list_first_entry(&session->s_cap_releases,
978 head = msg->front.iov_base;
980 dout(" queueing non-full %p (%d)\n", msg,
981 le32_to_cpu(head->num));
982 list_move_tail(&msg->list_head,
983 &session->s_cap_releases_done);
984 session->s_num_cap_releases -=
985 CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num);
989 spin_unlock(&session->s_cap_lock);
995 * flush all dirty inode data to disk.
997 * returns true if we've flushed through want_flush_seq
999 static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1003 dout("check_cap_flush want %lld\n", want_flush_seq);
1004 mutex_lock(&mdsc->mutex);
1005 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1006 struct ceph_mds_session *session = mdsc->sessions[mds];
1010 get_session(session);
1011 mutex_unlock(&mdsc->mutex);
1013 mutex_lock(&session->s_mutex);
1014 if (!list_empty(&session->s_cap_flushing)) {
1015 struct ceph_inode_info *ci =
1016 list_entry(session->s_cap_flushing.next,
1017 struct ceph_inode_info,
1019 struct inode *inode = &ci->vfs_inode;
1021 spin_lock(&inode->i_lock);
1022 if (ci->i_cap_flush_seq <= want_flush_seq) {
1023 dout("check_cap_flush still flushing %p "
1024 "seq %lld <= %lld to mds%d\n", inode,
1025 ci->i_cap_flush_seq, want_flush_seq,
1029 spin_unlock(&inode->i_lock);
1031 mutex_unlock(&session->s_mutex);
1032 ceph_put_mds_session(session);
1036 mutex_lock(&mdsc->mutex);
1039 mutex_unlock(&mdsc->mutex);
1040 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1045 * called under s_mutex
1047 static void send_cap_releases(struct ceph_mds_client *mdsc,
1048 struct ceph_mds_session *session)
1050 struct ceph_msg *msg;
1052 dout("send_cap_releases mds%d\n", session->s_mds);
1054 spin_lock(&session->s_cap_lock);
1055 if (list_empty(&session->s_cap_releases_done))
1057 msg = list_first_entry(&session->s_cap_releases_done,
1058 struct ceph_msg, list_head);
1059 list_del_init(&msg->list_head);
1060 spin_unlock(&session->s_cap_lock);
1061 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1062 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1063 ceph_con_send(&session->s_con, msg);
1065 spin_unlock(&session->s_cap_lock);
1073 * Create an mds request.
1075 struct ceph_mds_request *
1076 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1078 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1081 return ERR_PTR(-ENOMEM);
1083 req->r_started = jiffies;
1084 req->r_resend_mds = -1;
1085 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1087 atomic_set(&req->r_ref, 1); /* one for request_tree, one for caller */
1088 INIT_LIST_HEAD(&req->r_wait);
1089 init_completion(&req->r_completion);
1090 init_completion(&req->r_safe_completion);
1091 INIT_LIST_HEAD(&req->r_unsafe_item);
1094 req->r_direct_mode = mode;
1099 * return oldest (lowest) tid in request tree, 0 if none.
1101 * called under mdsc->mutex.
1103 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1105 struct ceph_mds_request *first;
1106 if (radix_tree_gang_lookup(&mdsc->request_tree,
1107 (void **)&first, 0, 1) <= 0)
1109 return first->r_tid;
1113 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1114 * on build_path_from_dentry in fs/cifs/dir.c.
1116 * If @stop_on_nosnap, generate path relative to the first non-snapped
1119 * Encode hidden .snap dirs as a double /, i.e.
1120 * foo/.snap/bar -> foo//bar
1122 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1125 struct dentry *temp;
1130 return ERR_PTR(-EINVAL);
1134 for (temp = dentry; !IS_ROOT(temp);) {
1135 struct inode *inode = temp->d_inode;
1136 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1137 len++; /* slash only */
1138 else if (stop_on_nosnap && inode &&
1139 ceph_snap(inode) == CEPH_NOSNAP)
1142 len += 1 + temp->d_name.len;
1143 temp = temp->d_parent;
1145 pr_err("build_path_dentry corrupt dentry %p\n", dentry);
1146 return ERR_PTR(-EINVAL);
1150 len--; /* no leading '/' */
1152 path = kmalloc(len+1, GFP_NOFS);
1154 return ERR_PTR(-ENOMEM);
1156 path[pos] = 0; /* trailing null */
1157 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1158 struct inode *inode = temp->d_inode;
1160 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1161 dout("build_path_dentry path+%d: %p SNAPDIR\n",
1163 } else if (stop_on_nosnap && inode &&
1164 ceph_snap(inode) == CEPH_NOSNAP) {
1167 pos -= temp->d_name.len;
1170 strncpy(path + pos, temp->d_name.name,
1172 dout("build_path_dentry path+%d: %p '%.*s'\n",
1173 pos, temp, temp->d_name.len, path + pos);
1177 temp = temp->d_parent;
1179 pr_err("build_path_dentry corrupt dentry\n");
1181 return ERR_PTR(-EINVAL);
1185 pr_err("build_path_dentry did not end path lookup where "
1186 "expected, namelen is %d, pos is %d\n", len, pos);
1187 /* presumably this is only possible if racing with a
1188 rename of one of the parent directories (we can not
1189 lock the dentries above us to prevent this, but
1190 retrying should be harmless) */
1195 *base = ceph_ino(temp->d_inode);
1197 dout("build_path_dentry on %p %d built %llx '%.*s'\n",
1198 dentry, atomic_read(&dentry->d_count), *base, len, path);
1202 static int build_dentry_path(struct dentry *dentry,
1203 const char **ppath, int *ppathlen, u64 *pino,
1208 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1209 *pino = ceph_ino(dentry->d_parent->d_inode);
1210 *ppath = dentry->d_name.name;
1211 *ppathlen = dentry->d_name.len;
1214 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1216 return PTR_ERR(path);
1222 static int build_inode_path(struct inode *inode,
1223 const char **ppath, int *ppathlen, u64 *pino,
1226 struct dentry *dentry;
1229 if (ceph_snap(inode) == CEPH_NOSNAP) {
1230 *pino = ceph_ino(inode);
1234 dentry = d_find_alias(inode);
1235 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1238 return PTR_ERR(path);
1245 * request arguments may be specified via an inode *, a dentry *, or
1246 * an explicit ino+path.
1248 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1249 const char *rpath, u64 rino,
1250 const char **ppath, int *pathlen,
1251 u64 *ino, int *freepath)
1256 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1257 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1259 } else if (rdentry) {
1260 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1261 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1266 *pathlen = strlen(rpath);
1267 dout(" path %.*s\n", *pathlen, rpath);
1274 * called under mdsc->mutex
1276 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1277 struct ceph_mds_request *req,
1280 struct ceph_msg *msg;
1281 struct ceph_mds_request_head *head;
1282 const char *path1 = NULL;
1283 const char *path2 = NULL;
1284 u64 ino1 = 0, ino2 = 0;
1285 int pathlen1 = 0, pathlen2 = 0;
1286 int freepath1 = 0, freepath2 = 0;
1292 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1293 req->r_path1, req->r_ino1.ino,
1294 &path1, &pathlen1, &ino1, &freepath1);
1300 ret = set_request_path_attr(NULL, req->r_old_dentry,
1301 req->r_path2, req->r_ino2.ino,
1302 &path2, &pathlen2, &ino2, &freepath2);
1308 len = sizeof(*head) +
1309 pathlen1 + pathlen2 + 2*(sizeof(u32) + sizeof(u64));
1311 /* calculate (max) length for cap releases */
1312 len += sizeof(struct ceph_mds_request_release) *
1313 (!!req->r_inode_drop + !!req->r_dentry_drop +
1314 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1315 if (req->r_dentry_drop)
1316 len += req->r_dentry->d_name.len;
1317 if (req->r_old_dentry_drop)
1318 len += req->r_old_dentry->d_name.len;
1320 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, 0, 0, NULL);
1324 head = msg->front.iov_base;
1325 p = msg->front.iov_base + sizeof(*head);
1326 end = msg->front.iov_base + msg->front.iov_len;
1328 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1329 head->op = cpu_to_le32(req->r_op);
1330 head->caller_uid = cpu_to_le32(current_fsuid());
1331 head->caller_gid = cpu_to_le32(current_fsgid());
1332 head->args = req->r_args;
1334 ceph_encode_filepath(&p, end, ino1, path1);
1335 ceph_encode_filepath(&p, end, ino2, path2);
1339 if (req->r_inode_drop)
1340 releases += ceph_encode_inode_release(&p,
1341 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1342 mds, req->r_inode_drop, req->r_inode_unless, 0);
1343 if (req->r_dentry_drop)
1344 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1345 mds, req->r_dentry_drop, req->r_dentry_unless);
1346 if (req->r_old_dentry_drop)
1347 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1348 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1349 if (req->r_old_inode_drop)
1350 releases += ceph_encode_inode_release(&p,
1351 req->r_old_dentry->d_inode,
1352 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1353 head->num_releases = cpu_to_le16(releases);
1356 msg->front.iov_len = p - msg->front.iov_base;
1357 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1359 msg->pages = req->r_pages;
1360 msg->nr_pages = req->r_num_pages;
1361 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1362 msg->hdr.data_off = cpu_to_le16(0);
1366 kfree((char *)path2);
1369 kfree((char *)path1);
1375 * called under mdsc->mutex if error, under no mutex if
1378 static void complete_request(struct ceph_mds_client *mdsc,
1379 struct ceph_mds_request *req)
1381 if (req->r_callback)
1382 req->r_callback(mdsc, req);
1384 complete(&req->r_completion);
1388 * called under mdsc->mutex
1390 static int __prepare_send_request(struct ceph_mds_client *mdsc,
1391 struct ceph_mds_request *req,
1394 struct ceph_mds_request_head *rhead;
1395 struct ceph_msg *msg;
1400 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1401 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1403 if (req->r_request) {
1404 ceph_msg_put(req->r_request);
1405 req->r_request = NULL;
1407 msg = create_request_message(mdsc, req, mds);
1409 req->r_reply = ERR_PTR(PTR_ERR(msg));
1410 complete_request(mdsc, req);
1411 return -PTR_ERR(msg);
1413 req->r_request = msg;
1415 rhead = msg->front.iov_base;
1416 rhead->tid = cpu_to_le64(req->r_tid);
1417 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1418 if (req->r_got_unsafe)
1419 flags |= CEPH_MDS_FLAG_REPLAY;
1420 if (req->r_locked_dir)
1421 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1422 rhead->flags = cpu_to_le32(flags);
1423 rhead->num_fwd = req->r_num_fwd;
1424 rhead->num_retry = req->r_attempts - 1;
1426 dout(" r_locked_dir = %p\n", req->r_locked_dir);
1428 if (req->r_target_inode && req->r_got_unsafe)
1429 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1436 * send request, or put it on the appropriate wait list.
1438 static int __do_request(struct ceph_mds_client *mdsc,
1439 struct ceph_mds_request *req)
1441 struct ceph_mds_session *session = NULL;
1448 if (req->r_timeout &&
1449 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1450 dout("do_request timed out\n");
1455 mds = __choose_mds(mdsc, req);
1457 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1458 dout("do_request no mds or not active, waiting for map\n");
1459 list_add(&req->r_wait, &mdsc->waiting_for_map);
1463 /* get, open session */
1464 session = __ceph_lookup_mds_session(mdsc, mds);
1466 session = register_session(mdsc, mds);
1467 dout("do_request mds%d session %p state %s\n", mds, session,
1468 session_state_name(session->s_state));
1469 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1470 session->s_state != CEPH_MDS_SESSION_HUNG) {
1471 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1472 session->s_state == CEPH_MDS_SESSION_CLOSING)
1473 __open_session(mdsc, session);
1474 list_add(&req->r_wait, &session->s_waiting);
1479 req->r_session = get_session(session);
1480 req->r_resend_mds = -1; /* forget any previous mds hint */
1482 if (req->r_request_started == 0) /* note request start time */
1483 req->r_request_started = jiffies;
1485 err = __prepare_send_request(mdsc, req, mds);
1487 ceph_msg_get(req->r_request);
1488 ceph_con_send(&session->s_con, req->r_request);
1492 ceph_put_mds_session(session);
1497 req->r_reply = ERR_PTR(err);
1498 complete_request(mdsc, req);
1503 * called under mdsc->mutex
1505 static void __wake_requests(struct ceph_mds_client *mdsc,
1506 struct list_head *head)
1508 struct ceph_mds_request *req, *nreq;
1510 list_for_each_entry_safe(req, nreq, head, r_wait) {
1511 list_del_init(&req->r_wait);
1512 __do_request(mdsc, req);
1517 * Wake up threads with requests pending for @mds, so that they can
1518 * resubmit their requests to a possibly different mds. If @all is set,
1519 * wake up if their requests has been forwarded to @mds, too.
1521 static void kick_requests(struct ceph_mds_client *mdsc, int mds, int all)
1523 struct ceph_mds_request *reqs[10];
1527 dout("kick_requests mds%d\n", mds);
1528 while (nexttid <= mdsc->last_tid) {
1529 got = radix_tree_gang_lookup(&mdsc->request_tree,
1530 (void **)&reqs, nexttid, 10);
1533 nexttid = reqs[got-1]->r_tid + 1;
1534 for (i = 0; i < got; i++) {
1535 if (reqs[i]->r_got_unsafe)
1537 if (reqs[i]->r_session &&
1538 reqs[i]->r_session->s_mds == mds) {
1539 dout(" kicking tid %llu\n", reqs[i]->r_tid);
1540 put_request_session(reqs[i]);
1541 __do_request(mdsc, reqs[i]);
1547 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1548 struct ceph_mds_request *req)
1550 dout("submit_request on %p\n", req);
1551 mutex_lock(&mdsc->mutex);
1552 __register_request(mdsc, req, NULL);
1553 __do_request(mdsc, req);
1554 mutex_unlock(&mdsc->mutex);
1558 * Synchrously perform an mds request. Take care of all of the
1559 * session setup, forwarding, retry details.
1561 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1563 struct ceph_mds_request *req)
1567 dout("do_request on %p\n", req);
1569 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1571 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1572 if (req->r_locked_dir)
1573 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1574 if (req->r_old_dentry)
1576 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1580 mutex_lock(&mdsc->mutex);
1581 __register_request(mdsc, req, dir);
1582 __do_request(mdsc, req);
1585 if (!req->r_reply) {
1586 mutex_unlock(&mdsc->mutex);
1587 if (req->r_timeout) {
1588 err = wait_for_completion_timeout(&req->r_completion,
1593 req->r_reply = ERR_PTR(-EIO);
1595 wait_for_completion(&req->r_completion);
1597 mutex_lock(&mdsc->mutex);
1600 if (IS_ERR(req->r_reply)) {
1601 err = PTR_ERR(req->r_reply);
1602 req->r_reply = NULL;
1605 __unregister_request(mdsc, req);
1606 if (!list_empty(&req->r_unsafe_item))
1607 list_del_init(&req->r_unsafe_item);
1608 complete(&req->r_safe_completion);
1609 } else if (req->r_err) {
1612 err = le32_to_cpu(req->r_reply_info.head->result);
1614 mutex_unlock(&mdsc->mutex);
1616 dout("do_request %p done, result %d\n", req, err);
1623 * We take the session mutex and parse and process the reply immediately.
1624 * This preserves the logical ordering of replies, capabilities, etc., sent
1625 * by the MDS as they are applied to our local cache.
1627 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
1629 struct ceph_mds_client *mdsc = session->s_mdsc;
1630 struct ceph_mds_request *req;
1631 struct ceph_mds_reply_head *head = msg->front.iov_base;
1632 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
1637 if (msg->hdr.src.name.type != CEPH_ENTITY_TYPE_MDS)
1639 if (msg->front.iov_len < sizeof(*head)) {
1640 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
1644 /* get request, session */
1645 tid = le64_to_cpu(head->tid);
1646 mutex_lock(&mdsc->mutex);
1647 req = __lookup_request(mdsc, tid);
1649 dout("handle_reply on unknown tid %llu\n", tid);
1650 mutex_unlock(&mdsc->mutex);
1653 dout("handle_reply %p\n", req);
1654 mds = le64_to_cpu(msg->hdr.src.name.num);
1656 /* correct session? */
1657 if (!req->r_session && req->r_session != session) {
1658 pr_err("mdsc_handle_reply got %llu on session mds%d"
1659 " not mds%d\n", tid, session->s_mds,
1660 req->r_session ? req->r_session->s_mds : -1);
1661 mutex_unlock(&mdsc->mutex);
1666 if ((req->r_got_unsafe && !head->safe) ||
1667 (req->r_got_safe && head->safe)) {
1668 pr_warning("got a dup %s reply on %llu from mds%d\n",
1669 head->safe ? "safe" : "unsafe", tid, mds);
1670 mutex_unlock(&mdsc->mutex);
1674 result = le32_to_cpu(head->result);
1677 * Tolerate 2 consecutive ESTALEs from the same mds.
1678 * FIXME: we should be looking at the cap migrate_seq.
1680 if (result == -ESTALE) {
1681 req->r_direct_mode = USE_AUTH_MDS;
1683 if (req->r_num_stale <= 2) {
1684 __do_request(mdsc, req);
1685 mutex_unlock(&mdsc->mutex);
1689 req->r_num_stale = 0;
1693 req->r_got_safe = true;
1694 __unregister_request(mdsc, req);
1695 complete(&req->r_safe_completion);
1697 if (req->r_got_unsafe) {
1699 * We already handled the unsafe response, now do the
1700 * cleanup. No need to examine the response; the MDS
1701 * doesn't include any result info in the safe
1702 * response. And even if it did, there is nothing
1703 * useful we could do with a revised return value.
1705 dout("got safe reply %llu, mds%d\n", tid, mds);
1706 list_del_init(&req->r_unsafe_item);
1708 /* last unsafe request during umount? */
1709 if (mdsc->stopping && !__get_oldest_tid(mdsc))
1710 complete(&mdsc->safe_umount_waiters);
1711 mutex_unlock(&mdsc->mutex);
1716 BUG_ON(req->r_reply);
1719 req->r_got_unsafe = true;
1720 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
1723 dout("handle_reply tid %lld result %d\n", tid, result);
1724 rinfo = &req->r_reply_info;
1725 err = parse_reply_info(msg, rinfo);
1726 mutex_unlock(&mdsc->mutex);
1728 mutex_lock(&session->s_mutex);
1730 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds);
1735 if (rinfo->snapblob_len) {
1736 down_write(&mdsc->snap_rwsem);
1737 ceph_update_snap_trace(mdsc, rinfo->snapblob,
1738 rinfo->snapblob + rinfo->snapblob_len,
1739 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
1740 downgrade_write(&mdsc->snap_rwsem);
1742 down_read(&mdsc->snap_rwsem);
1745 /* insert trace into our cache */
1746 err = ceph_fill_trace(mdsc->client->sb, req, req->r_session);
1748 if (result == 0 && rinfo->dir_nr)
1749 ceph_readdir_prepopulate(req, req->r_session);
1750 ceph_unreserve_caps(&req->r_caps_reservation);
1753 up_read(&mdsc->snap_rwsem);
1762 add_cap_releases(mdsc, req->r_session, -1);
1763 mutex_unlock(&session->s_mutex);
1765 /* kick calling process */
1766 complete_request(mdsc, req);
1768 ceph_mdsc_put_request(req);
1775 * handle mds notification that our request has been forwarded.
1777 static void handle_forward(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
1779 struct ceph_mds_request *req;
1785 void *p = msg->front.iov_base;
1786 void *end = p + msg->front.iov_len;
1787 int from_mds, state;
1789 if (msg->hdr.src.name.type != CEPH_ENTITY_TYPE_MDS)
1791 from_mds = le64_to_cpu(msg->hdr.src.name.num);
1793 ceph_decode_need(&p, end, sizeof(u64)+2*sizeof(u32), bad);
1794 ceph_decode_64(&p, tid);
1795 ceph_decode_32(&p, next_mds);
1796 ceph_decode_32(&p, fwd_seq);
1797 ceph_decode_8(&p, must_resend);
1799 WARN_ON(must_resend); /* shouldn't happen. */
1801 mutex_lock(&mdsc->mutex);
1802 req = __lookup_request(mdsc, tid);
1804 dout("forward %llu dne\n", tid);
1805 goto out; /* dup reply? */
1808 state = mdsc->sessions[next_mds]->s_state;
1809 if (fwd_seq <= req->r_num_fwd) {
1810 dout("forward %llu to mds%d - old seq %d <= %d\n",
1811 tid, next_mds, req->r_num_fwd, fwd_seq);
1813 /* resend. forward race not possible; mds would drop */
1814 dout("forward %llu to mds%d (we resend)\n", tid, next_mds);
1815 req->r_num_fwd = fwd_seq;
1816 req->r_resend_mds = next_mds;
1817 put_request_session(req);
1818 __do_request(mdsc, req);
1820 ceph_mdsc_put_request(req);
1822 mutex_unlock(&mdsc->mutex);
1826 pr_err("mdsc_handle_forward decode error err=%d\n", err);
1830 * handle a mds session control message
1832 static void handle_session(struct ceph_mds_session *session,
1833 struct ceph_msg *msg)
1835 struct ceph_mds_client *mdsc = session->s_mdsc;
1839 struct ceph_mds_session_head *h = msg->front.iov_base;
1842 if (msg->hdr.src.name.type != CEPH_ENTITY_TYPE_MDS)
1844 mds = le64_to_cpu(msg->hdr.src.name.num);
1847 if (msg->front.iov_len != sizeof(*h))
1849 op = le32_to_cpu(h->op);
1850 seq = le64_to_cpu(h->seq);
1852 mutex_lock(&mdsc->mutex);
1853 /* FIXME: this ttl calculation is generous */
1854 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
1855 mutex_unlock(&mdsc->mutex);
1857 mutex_lock(&session->s_mutex);
1859 dout("handle_session mds%d %s %p state %s seq %llu\n",
1860 mds, ceph_session_op_name(op), session,
1861 session_state_name(session->s_state), seq);
1863 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
1864 session->s_state = CEPH_MDS_SESSION_OPEN;
1865 pr_info("mds%d came back\n", session->s_mds);
1869 case CEPH_SESSION_OPEN:
1870 session->s_state = CEPH_MDS_SESSION_OPEN;
1871 renewed_caps(mdsc, session, 0);
1874 __close_session(mdsc, session);
1877 case CEPH_SESSION_RENEWCAPS:
1878 if (session->s_renew_seq == seq)
1879 renewed_caps(mdsc, session, 1);
1882 case CEPH_SESSION_CLOSE:
1883 unregister_session(mdsc, mds);
1884 remove_session_caps(session);
1885 wake = 1; /* for good measure */
1886 complete(&mdsc->session_close_waiters);
1887 kick_requests(mdsc, mds, 0); /* cur only */
1890 case CEPH_SESSION_STALE:
1891 pr_info("mds%d caps went stale, renewing\n",
1893 spin_lock(&session->s_cap_lock);
1894 session->s_cap_gen++;
1895 session->s_cap_ttl = 0;
1896 spin_unlock(&session->s_cap_lock);
1897 send_renew_caps(mdsc, session);
1900 case CEPH_SESSION_RECALL_STATE:
1901 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
1905 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
1909 mutex_unlock(&session->s_mutex);
1911 mutex_lock(&mdsc->mutex);
1912 __wake_requests(mdsc, &session->s_waiting);
1913 mutex_unlock(&mdsc->mutex);
1918 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
1919 (int)msg->front.iov_len);
1925 * called under session->mutex.
1927 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
1928 struct ceph_mds_session *session)
1930 struct ceph_mds_request *req, *nreq;
1933 dout("replay_unsafe_requests mds%d\n", session->s_mds);
1935 mutex_lock(&mdsc->mutex);
1936 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
1937 err = __prepare_send_request(mdsc, req, session->s_mds);
1939 ceph_msg_get(req->r_request);
1940 ceph_con_send(&session->s_con, req->r_request);
1943 mutex_unlock(&mdsc->mutex);
1947 * Encode information about a cap for a reconnect with the MDS.
1949 struct encode_caps_data {
1955 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
1958 struct ceph_mds_cap_reconnect *rec;
1959 struct ceph_inode_info *ci;
1960 struct encode_caps_data *data = (struct encode_caps_data *)arg;
1961 void *p = *(data->pp);
1962 void *end = data->end;
1966 struct dentry *dentry;
1970 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
1971 inode, ceph_vinop(inode), cap, cap->cap_id,
1972 ceph_cap_string(cap->issued));
1973 ceph_decode_need(&p, end, sizeof(u64), needmore);
1974 ceph_encode_64(&p, ceph_ino(inode));
1976 dentry = d_find_alias(inode);
1978 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
1980 err = PTR_ERR(path);
1987 ceph_decode_need(&p, end, pathlen+4, needmore);
1988 ceph_encode_string(&p, end, path, pathlen);
1990 ceph_decode_need(&p, end, sizeof(*rec), needmore);
1994 spin_lock(&inode->i_lock);
1995 cap->seq = 0; /* reset cap seq */
1996 cap->issue_seq = 0; /* and issue_seq */
1997 rec->cap_id = cpu_to_le64(cap->cap_id);
1998 rec->pathbase = cpu_to_le64(pathbase);
1999 rec->wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2000 rec->issued = cpu_to_le32(cap->issued);
2001 rec->size = cpu_to_le64(inode->i_size);
2002 ceph_encode_timespec(&rec->mtime, &inode->i_mtime);
2003 ceph_encode_timespec(&rec->atime, &inode->i_atime);
2004 rec->snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2005 spin_unlock(&inode->i_lock);
2009 (*data->num_caps)++;
2018 * If an MDS fails and recovers, clients need to reconnect in order to
2019 * reestablish shared state. This includes all caps issued through
2020 * this session _and_ the snap_realm hierarchy. Because it's not
2021 * clear which snap realms the mds cares about, we send everything we
2022 * know about.. that ensures we'll then get any new info the
2023 * recovering MDS might have.
2025 * This is a relatively heavyweight operation, but it's rare.
2027 * called with mdsc->mutex held.
2029 static void send_mds_reconnect(struct ceph_mds_client *mdsc, int mds)
2031 struct ceph_mds_session *session;
2032 struct ceph_msg *reply;
2033 int newlen, len = 4 + 1;
2036 int num_caps, num_realms = 0;
2038 u64 next_snap_ino = 0;
2039 __le32 *pnum_caps, *pnum_realms;
2040 struct encode_caps_data iter_args;
2042 pr_info("reconnect to recovering mds%d\n", mds);
2045 session = __ceph_lookup_mds_session(mdsc, mds);
2046 mutex_unlock(&mdsc->mutex); /* drop lock for duration */
2049 mutex_lock(&session->s_mutex);
2051 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2054 ceph_con_open(&session->s_con,
2055 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2057 /* replay unsafe requests */
2058 replay_unsafe_requests(mdsc, session);
2060 /* estimate needed space */
2061 len += session->s_nr_caps *
2062 (100+sizeof(struct ceph_mds_cap_reconnect));
2063 pr_info("estimating i need %d bytes for %d caps\n",
2064 len, session->s_nr_caps);
2066 dout("no session for mds%d, will send short reconnect\n",
2070 down_read(&mdsc->snap_rwsem);
2074 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, len, 0, 0, NULL);
2075 if (IS_ERR(reply)) {
2076 err = PTR_ERR(reply);
2077 pr_err("send_mds_reconnect ENOMEM on %d for mds%d\n",
2081 p = reply->front.iov_base;
2085 ceph_encode_8(&p, 1); /* session was closed */
2086 ceph_encode_32(&p, 0);
2089 dout("session %p state %s\n", session,
2090 session_state_name(session->s_state));
2092 /* traverse this session's caps */
2093 ceph_encode_8(&p, 0);
2095 ceph_encode_32(&p, session->s_nr_caps);
2099 iter_args.end = end;
2100 iter_args.num_caps = &num_caps;
2101 err = iterate_session_caps(session, encode_caps_cb, &iter_args);
2106 *pnum_caps = cpu_to_le32(num_caps);
2109 * snaprealms. we provide mds with the ino, seq (version), and
2110 * parent for all of our realms. If the mds has any newer info,
2114 /* save some space for the snaprealm count */
2116 ceph_decode_need(&p, end, sizeof(*pnum_realms), needmore);
2117 p += sizeof(*pnum_realms);
2120 struct ceph_snap_realm *realm;
2121 struct ceph_mds_snaprealm_reconnect *sr_rec;
2122 got = radix_tree_gang_lookup(&mdsc->snap_realms,
2123 (void **)&realm, next_snap_ino, 1);
2127 dout(" adding snap realm %llx seq %lld parent %llx\n",
2128 realm->ino, realm->seq, realm->parent_ino);
2129 ceph_decode_need(&p, end, sizeof(*sr_rec), needmore);
2131 sr_rec->ino = cpu_to_le64(realm->ino);
2132 sr_rec->seq = cpu_to_le64(realm->seq);
2133 sr_rec->parent = cpu_to_le64(realm->parent_ino);
2134 p += sizeof(*sr_rec);
2136 next_snap_ino = realm->ino + 1;
2138 *pnum_realms = cpu_to_le32(num_realms);
2141 reply->front.iov_len = p - reply->front.iov_base;
2142 reply->hdr.front_len = cpu_to_le32(reply->front.iov_len);
2143 dout("final len was %u (guessed %d)\n",
2144 (unsigned)reply->front.iov_len, len);
2145 ceph_con_send(&session->s_con, reply);
2148 session->s_state = CEPH_MDS_SESSION_OPEN;
2149 __wake_requests(mdsc, &session->s_waiting);
2153 up_read(&mdsc->snap_rwsem);
2155 mutex_unlock(&session->s_mutex);
2156 ceph_put_mds_session(session);
2158 mutex_lock(&mdsc->mutex);
2163 * we need a larger buffer. this doesn't very accurately
2164 * factor in snap realms, but it's safe.
2166 num_caps += num_realms;
2167 newlen = len * ((100 * (session->s_nr_caps+3)) / (num_caps + 1)) / 100;
2168 pr_info("i guessed %d, and did %d of %d caps, retrying with %d\n",
2169 len, num_caps, session->s_nr_caps, newlen);
2171 ceph_msg_put(reply);
2177 * compare old and new mdsmaps, kicking requests
2178 * and closing out old connections as necessary
2180 * called under mdsc->mutex.
2182 static void check_new_map(struct ceph_mds_client *mdsc,
2183 struct ceph_mdsmap *newmap,
2184 struct ceph_mdsmap *oldmap)
2187 int oldstate, newstate;
2188 struct ceph_mds_session *s;
2190 dout("check_new_map new %u old %u\n",
2191 newmap->m_epoch, oldmap->m_epoch);
2193 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2194 if (mdsc->sessions[i] == NULL)
2196 s = mdsc->sessions[i];
2197 oldstate = ceph_mdsmap_get_state(oldmap, i);
2198 newstate = ceph_mdsmap_get_state(newmap, i);
2200 dout("check_new_map mds%d state %s -> %s (session %s)\n",
2201 i, ceph_mds_state_name(oldstate),
2202 ceph_mds_state_name(newstate),
2203 session_state_name(s->s_state));
2205 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2206 ceph_mdsmap_get_addr(newmap, i),
2207 sizeof(struct ceph_entity_addr))) {
2208 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2209 /* the session never opened, just close it
2211 __wake_requests(mdsc, &s->s_waiting);
2212 unregister_session(mdsc, i);
2215 mutex_unlock(&mdsc->mutex);
2216 mutex_lock(&s->s_mutex);
2217 mutex_lock(&mdsc->mutex);
2218 ceph_con_close(&s->s_con);
2219 mutex_unlock(&s->s_mutex);
2220 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2223 /* kick any requests waiting on the recovering mds */
2224 kick_requests(mdsc, i, 1);
2225 } else if (oldstate == newstate) {
2226 continue; /* nothing new with this mds */
2232 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
2233 newstate >= CEPH_MDS_STATE_RECONNECT)
2234 send_mds_reconnect(mdsc, i);
2237 * kick requests on any mds that has gone active.
2239 * kick requests on cur or forwarder: we may have sent
2240 * the request to mds1, mds1 told us it forwarded it
2241 * to mds2, but then we learn mds1 failed and can't be
2242 * sure it successfully forwarded our request before
2245 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2246 newstate >= CEPH_MDS_STATE_ACTIVE) {
2247 kick_requests(mdsc, i, 1);
2248 ceph_kick_flushing_caps(mdsc, s);
2260 * caller must hold session s_mutex, dentry->d_lock
2262 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2264 struct ceph_dentry_info *di = ceph_dentry(dentry);
2266 ceph_put_mds_session(di->lease_session);
2267 di->lease_session = NULL;
2270 static void handle_lease(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
2272 struct super_block *sb = mdsc->client->sb;
2273 struct inode *inode;
2274 struct ceph_mds_session *session;
2275 struct ceph_inode_info *ci;
2276 struct dentry *parent, *dentry;
2277 struct ceph_dentry_info *di;
2279 struct ceph_mds_lease *h = msg->front.iov_base;
2280 struct ceph_vino vino;
2285 if (msg->hdr.src.name.type != CEPH_ENTITY_TYPE_MDS)
2287 mds = le64_to_cpu(msg->hdr.src.name.num);
2288 dout("handle_lease from mds%d\n", mds);
2291 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2293 vino.ino = le64_to_cpu(h->ino);
2294 vino.snap = CEPH_NOSNAP;
2295 mask = le16_to_cpu(h->mask);
2296 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2297 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2298 if (dname.len != get_unaligned_le32(h+1))
2302 mutex_lock(&mdsc->mutex);
2303 session = __ceph_lookup_mds_session(mdsc, mds);
2304 mutex_unlock(&mdsc->mutex);
2306 pr_err("handle_lease got lease but no session mds%d\n", mds);
2310 mutex_lock(&session->s_mutex);
2314 inode = ceph_find_inode(sb, vino);
2315 dout("handle_lease '%s', mask %d, ino %llx %p\n",
2316 ceph_lease_op_name(h->action), mask, vino.ino, inode);
2317 if (inode == NULL) {
2318 dout("handle_lease no inode %llx\n", vino.ino);
2321 ci = ceph_inode(inode);
2324 parent = d_find_alias(inode);
2326 dout("no parent dentry on inode %p\n", inode);
2328 goto release; /* hrm... */
2330 dname.hash = full_name_hash(dname.name, dname.len);
2331 dentry = d_lookup(parent, &dname);
2336 spin_lock(&dentry->d_lock);
2337 di = ceph_dentry(dentry);
2338 switch (h->action) {
2339 case CEPH_MDS_LEASE_REVOKE:
2340 if (di && di->lease_session == session) {
2341 h->seq = cpu_to_le32(di->lease_seq);
2342 __ceph_mdsc_drop_dentry_lease(dentry);
2347 case CEPH_MDS_LEASE_RENEW:
2348 if (di && di->lease_session == session &&
2349 di->lease_gen == session->s_cap_gen &&
2350 di->lease_renew_from &&
2351 di->lease_renew_after == 0) {
2352 unsigned long duration =
2353 le32_to_cpu(h->duration_ms) * HZ / 1000;
2355 di->lease_seq = le32_to_cpu(h->seq);
2356 dentry->d_time = di->lease_renew_from + duration;
2357 di->lease_renew_after = di->lease_renew_from +
2359 di->lease_renew_from = 0;
2363 spin_unlock(&dentry->d_lock);
2370 /* let's just reuse the same message */
2371 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2373 ceph_con_send(&session->s_con, msg);
2377 mutex_unlock(&session->s_mutex);
2378 ceph_put_mds_session(session);
2382 pr_err("corrupt lease message\n");
2385 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2386 struct inode *inode,
2387 struct dentry *dentry, char action,
2390 struct ceph_msg *msg;
2391 struct ceph_mds_lease *lease;
2392 int len = sizeof(*lease) + sizeof(u32);
2395 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2396 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2397 dnamelen = dentry->d_name.len;
2400 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, 0, 0, NULL);
2403 lease = msg->front.iov_base;
2404 lease->action = action;
2405 lease->mask = cpu_to_le16(CEPH_LOCK_DN);
2406 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2407 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2408 lease->seq = cpu_to_le32(seq);
2409 put_unaligned_le32(dnamelen, lease + 1);
2410 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2413 * if this is a preemptive lease RELEASE, no need to
2414 * flush request stream, since the actual request will
2417 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2419 ceph_con_send(&session->s_con, msg);
2423 * Preemptively release a lease we expect to invalidate anyway.
2424 * Pass @inode always, @dentry is optional.
2426 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2427 struct dentry *dentry, int mask)
2429 struct ceph_dentry_info *di;
2430 struct ceph_mds_session *session;
2433 BUG_ON(inode == NULL);
2434 BUG_ON(dentry == NULL);
2435 BUG_ON(mask != CEPH_LOCK_DN);
2437 /* is dentry lease valid? */
2438 spin_lock(&dentry->d_lock);
2439 di = ceph_dentry(dentry);
2440 if (!di || !di->lease_session ||
2441 di->lease_session->s_mds < 0 ||
2442 di->lease_gen != di->lease_session->s_cap_gen ||
2443 !time_before(jiffies, dentry->d_time)) {
2444 dout("lease_release inode %p dentry %p -- "
2446 inode, dentry, mask);
2447 spin_unlock(&dentry->d_lock);
2451 /* we do have a lease on this dentry; note mds and seq */
2452 session = ceph_get_mds_session(di->lease_session);
2453 seq = di->lease_seq;
2454 __ceph_mdsc_drop_dentry_lease(dentry);
2455 spin_unlock(&dentry->d_lock);
2457 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2458 inode, dentry, mask, session->s_mds);
2459 ceph_mdsc_lease_send_msg(session, inode, dentry,
2460 CEPH_MDS_LEASE_RELEASE, seq);
2461 ceph_put_mds_session(session);
2465 * drop all leases (and dentry refs) in preparation for umount
2467 static void drop_leases(struct ceph_mds_client *mdsc)
2471 dout("drop_leases\n");
2472 mutex_lock(&mdsc->mutex);
2473 for (i = 0; i < mdsc->max_sessions; i++) {
2474 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2477 mutex_unlock(&mdsc->mutex);
2478 mutex_lock(&s->s_mutex);
2479 mutex_unlock(&s->s_mutex);
2480 ceph_put_mds_session(s);
2481 mutex_lock(&mdsc->mutex);
2483 mutex_unlock(&mdsc->mutex);
2489 * delayed work -- periodically trim expired leases, renew caps with mds
2491 static void schedule_delayed(struct ceph_mds_client *mdsc)
2494 unsigned hz = round_jiffies_relative(HZ * delay);
2495 schedule_delayed_work(&mdsc->delayed_work, hz);
2498 static void delayed_work(struct work_struct *work)
2501 struct ceph_mds_client *mdsc =
2502 container_of(work, struct ceph_mds_client, delayed_work.work);
2506 dout("mdsc delayed_work\n");
2507 ceph_check_delayed_caps(mdsc, 0);
2509 mutex_lock(&mdsc->mutex);
2510 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2511 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2512 mdsc->last_renew_caps);
2514 mdsc->last_renew_caps = jiffies;
2516 for (i = 0; i < mdsc->max_sessions; i++) {
2517 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2520 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2521 dout("resending session close request for mds%d\n",
2523 request_close_session(mdsc, s);
2524 ceph_put_mds_session(s);
2527 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2528 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2529 s->s_state = CEPH_MDS_SESSION_HUNG;
2530 pr_info("mds%d hung\n", s->s_mds);
2533 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2534 /* this mds is failed or recovering, just wait */
2535 ceph_put_mds_session(s);
2538 mutex_unlock(&mdsc->mutex);
2540 mutex_lock(&s->s_mutex);
2542 send_renew_caps(mdsc, s);
2544 ceph_con_keepalive(&s->s_con);
2545 add_cap_releases(mdsc, s, -1);
2546 send_cap_releases(mdsc, s);
2547 mutex_unlock(&s->s_mutex);
2548 ceph_put_mds_session(s);
2550 mutex_lock(&mdsc->mutex);
2552 mutex_unlock(&mdsc->mutex);
2554 schedule_delayed(mdsc);
2558 void ceph_mdsc_init(struct ceph_mds_client *mdsc, struct ceph_client *client)
2560 mdsc->client = client;
2561 mutex_init(&mdsc->mutex);
2562 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
2563 init_completion(&mdsc->safe_umount_waiters);
2564 init_completion(&mdsc->session_close_waiters);
2565 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2566 mdsc->sessions = NULL;
2567 mdsc->max_sessions = 0;
2569 init_rwsem(&mdsc->snap_rwsem);
2570 INIT_RADIX_TREE(&mdsc->snap_realms, GFP_NOFS);
2571 INIT_LIST_HEAD(&mdsc->snap_empty);
2572 spin_lock_init(&mdsc->snap_empty_lock);
2574 INIT_RADIX_TREE(&mdsc->request_tree, GFP_NOFS);
2575 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2576 mdsc->last_renew_caps = jiffies;
2577 INIT_LIST_HEAD(&mdsc->cap_delay_list);
2578 spin_lock_init(&mdsc->cap_delay_lock);
2579 INIT_LIST_HEAD(&mdsc->snap_flush_list);
2580 spin_lock_init(&mdsc->snap_flush_lock);
2581 mdsc->cap_flush_seq = 0;
2582 INIT_LIST_HEAD(&mdsc->cap_dirty);
2583 mdsc->num_cap_flushing = 0;
2584 spin_lock_init(&mdsc->cap_dirty_lock);
2585 init_waitqueue_head(&mdsc->cap_flushing_wq);
2586 spin_lock_init(&mdsc->dentry_lru_lock);
2587 INIT_LIST_HEAD(&mdsc->dentry_lru);
2591 * Wait for safe replies on open mds requests. If we time out, drop
2592 * all requests from the tree to avoid dangling dentry refs.
2594 static void wait_requests(struct ceph_mds_client *mdsc)
2596 struct ceph_mds_request *req;
2597 struct ceph_client *client = mdsc->client;
2599 mutex_lock(&mdsc->mutex);
2600 if (__get_oldest_tid(mdsc)) {
2601 mutex_unlock(&mdsc->mutex);
2602 dout("wait_requests waiting for requests\n");
2603 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
2604 client->mount_args.mount_timeout * HZ);
2605 mutex_lock(&mdsc->mutex);
2607 /* tear down remaining requests */
2608 while (radix_tree_gang_lookup(&mdsc->request_tree,
2609 (void **)&req, 0, 1)) {
2610 dout("wait_requests timed out on tid %llu\n",
2612 radix_tree_delete(&mdsc->request_tree, req->r_tid);
2613 ceph_mdsc_put_request(req);
2616 mutex_unlock(&mdsc->mutex);
2617 dout("wait_requests done\n");
2621 * called before mount is ro, and before dentries are torn down.
2622 * (hmm, does this still race with new lookups?)
2624 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
2626 dout("pre_umount\n");
2630 ceph_check_delayed_caps(mdsc, 1);
2631 wait_requests(mdsc);
2635 * wait for all write mds requests to flush.
2637 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
2639 struct ceph_mds_request *req;
2643 mutex_lock(&mdsc->mutex);
2644 dout("wait_unsafe_requests want %lld\n", want_tid);
2646 got = radix_tree_gang_lookup(&mdsc->request_tree, (void **)&req,
2650 if (req->r_tid > want_tid)
2653 next_tid = req->r_tid + 1;
2654 if ((req->r_op & CEPH_MDS_OP_WRITE) == 0)
2655 continue; /* not a write op */
2657 ceph_mdsc_get_request(req);
2658 mutex_unlock(&mdsc->mutex);
2659 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
2660 req->r_tid, want_tid);
2661 wait_for_completion(&req->r_safe_completion);
2662 mutex_lock(&mdsc->mutex);
2663 ceph_mdsc_put_request(req);
2665 mutex_unlock(&mdsc->mutex);
2666 dout("wait_unsafe_requests done\n");
2669 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
2671 u64 want_tid, want_flush;
2674 mutex_lock(&mdsc->mutex);
2675 want_tid = mdsc->last_tid;
2676 want_flush = mdsc->cap_flush_seq;
2677 mutex_unlock(&mdsc->mutex);
2678 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
2680 ceph_check_delayed_caps(mdsc, 1);
2682 wait_unsafe_requests(mdsc, want_tid);
2683 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
2688 * called after sb is ro.
2690 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
2692 struct ceph_mds_session *session;
2695 struct ceph_client *client = mdsc->client;
2696 unsigned long started, timeout = client->mount_args.mount_timeout * HZ;
2698 dout("close_sessions\n");
2700 mutex_lock(&mdsc->mutex);
2702 /* close sessions */
2704 while (time_before(jiffies, started + timeout)) {
2705 dout("closing sessions\n");
2707 for (i = 0; i < mdsc->max_sessions; i++) {
2708 session = __ceph_lookup_mds_session(mdsc, i);
2711 mutex_unlock(&mdsc->mutex);
2712 mutex_lock(&session->s_mutex);
2713 __close_session(mdsc, session);
2714 mutex_unlock(&session->s_mutex);
2715 ceph_put_mds_session(session);
2716 mutex_lock(&mdsc->mutex);
2722 if (client->mount_state == CEPH_MOUNT_SHUTDOWN)
2725 dout("waiting for sessions to close\n");
2726 mutex_unlock(&mdsc->mutex);
2727 wait_for_completion_timeout(&mdsc->session_close_waiters,
2729 mutex_lock(&mdsc->mutex);
2732 /* tear down remaining sessions */
2733 for (i = 0; i < mdsc->max_sessions; i++) {
2734 if (mdsc->sessions[i]) {
2735 session = get_session(mdsc->sessions[i]);
2736 unregister_session(mdsc, i);
2737 mutex_unlock(&mdsc->mutex);
2738 mutex_lock(&session->s_mutex);
2739 remove_session_caps(session);
2740 mutex_unlock(&session->s_mutex);
2741 ceph_put_mds_session(session);
2742 mutex_lock(&mdsc->mutex);
2746 WARN_ON(!list_empty(&mdsc->cap_delay_list));
2748 mutex_unlock(&mdsc->mutex);
2750 ceph_cleanup_empty_realms(mdsc);
2752 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
2757 void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
2760 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
2762 ceph_mdsmap_destroy(mdsc->mdsmap);
2763 kfree(mdsc->sessions);
2768 * handle mds map update.
2770 void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
2774 void *p = msg->front.iov_base;
2775 void *end = p + msg->front.iov_len;
2776 struct ceph_mdsmap *newmap, *oldmap;
2777 struct ceph_fsid fsid;
2780 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
2781 ceph_decode_copy(&p, &fsid, sizeof(fsid));
2782 if (ceph_fsid_compare(&fsid, &mdsc->client->monc.monmap->fsid)) {
2783 pr_err("got mdsmap with wrong fsid\n");
2786 ceph_decode_32(&p, epoch);
2787 ceph_decode_32(&p, maplen);
2788 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
2790 /* do we need it? */
2791 ceph_monc_got_mdsmap(&mdsc->client->monc, epoch);
2792 mutex_lock(&mdsc->mutex);
2793 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
2794 dout("handle_map epoch %u <= our %u\n",
2795 epoch, mdsc->mdsmap->m_epoch);
2796 mutex_unlock(&mdsc->mutex);
2800 newmap = ceph_mdsmap_decode(&p, end);
2801 if (IS_ERR(newmap)) {
2802 err = PTR_ERR(newmap);
2806 /* swap into place */
2808 oldmap = mdsc->mdsmap;
2809 mdsc->mdsmap = newmap;
2810 check_new_map(mdsc, newmap, oldmap);
2811 ceph_mdsmap_destroy(oldmap);
2813 mdsc->mdsmap = newmap; /* first mds map */
2815 mdsc->client->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
2817 __wake_requests(mdsc, &mdsc->waiting_for_map);
2819 mutex_unlock(&mdsc->mutex);
2820 schedule_delayed(mdsc);
2824 mutex_unlock(&mdsc->mutex);
2826 pr_err("error decoding mdsmap %d\n", err);
2830 static struct ceph_connection *con_get(struct ceph_connection *con)
2832 struct ceph_mds_session *s = con->private;
2834 if (get_session(s)) {
2835 dout("mdsc con_get %p %d -> %d\n", s,
2836 atomic_read(&s->s_ref) - 1, atomic_read(&s->s_ref));
2839 dout("mdsc con_get %p FAIL\n", s);
2843 static void con_put(struct ceph_connection *con)
2845 struct ceph_mds_session *s = con->private;
2847 dout("mdsc con_put %p %d -> %d\n", s, atomic_read(&s->s_ref),
2848 atomic_read(&s->s_ref) - 1);
2849 ceph_put_mds_session(s);
2853 * if the client is unresponsive for long enough, the mds will kill
2854 * the session entirely.
2856 static void peer_reset(struct ceph_connection *con)
2858 struct ceph_mds_session *s = con->private;
2860 pr_err("mds%d gave us the boot. IMPLEMENT RECONNECT.\n",
2864 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2866 struct ceph_mds_session *s = con->private;
2867 struct ceph_mds_client *mdsc = s->s_mdsc;
2868 int type = le16_to_cpu(msg->hdr.type);
2871 case CEPH_MSG_MDS_MAP:
2872 ceph_mdsc_handle_map(mdsc, msg);
2874 case CEPH_MSG_CLIENT_SESSION:
2875 handle_session(s, msg);
2877 case CEPH_MSG_CLIENT_REPLY:
2878 handle_reply(s, msg);
2880 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
2881 handle_forward(mdsc, msg);
2883 case CEPH_MSG_CLIENT_CAPS:
2884 ceph_handle_caps(s, msg);
2886 case CEPH_MSG_CLIENT_SNAP:
2887 ceph_handle_snap(mdsc, msg);
2889 case CEPH_MSG_CLIENT_LEASE:
2890 handle_lease(mdsc, msg);
2894 pr_err("received unknown message type %d %s\n", type,
2895 ceph_msg_type_name(type));
2900 const static struct ceph_connection_operations mds_con_ops = {
2903 .dispatch = dispatch,
2904 .peer_reset = peer_reset,
2905 .alloc_msg = ceph_alloc_msg,
2906 .alloc_middle = ceph_alloc_middle,