1 #include "ceph_debug.h"
3 #include <linux/wait.h>
4 #include <linux/slab.h>
5 #include <linux/sched.h>
7 #include "mds_client.h"
8 #include "mon_client.h"
10 #include "messenger.h"
16 * A cluster of MDS (metadata server) daemons is responsible for
17 * managing the file system namespace (the directory hierarchy and
18 * inodes) and for coordinating shared access to storage. Metadata is
19 * partitioning hierarchically across a number of servers, and that
20 * partition varies over time as the cluster adjusts the distribution
21 * in order to balance load.
23 * The MDS client is primarily responsible to managing synchronous
24 * metadata requests for operations like open, unlink, and so forth.
25 * If there is a MDS failure, we find out about it when we (possibly
26 * request and) receive a new MDS map, and can resubmit affected
29 * For the most part, though, we take advantage of a lossless
30 * communications channel to the MDS, and do not need to worry about
31 * timing out or resubmitting requests.
33 * We maintain a stateful "session" with each MDS we interact with.
34 * Within each session, we sent periodic heartbeat messages to ensure
35 * any capabilities or leases we have been issues remain valid. If
36 * the session times out and goes stale, our leases and capabilities
37 * are no longer valid.
40 struct ceph_reconnect_state {
41 struct ceph_pagelist *pagelist;
45 static void __wake_requests(struct ceph_mds_client *mdsc,
46 struct list_head *head);
48 static const struct ceph_connection_operations mds_con_ops;
56 * parse individual inode info
58 static int parse_reply_info_in(void **p, void *end,
59 struct ceph_mds_reply_info_in *info)
64 *p += sizeof(struct ceph_mds_reply_inode) +
65 sizeof(*info->in->fragtree.splits) *
66 le32_to_cpu(info->in->fragtree.nsplits);
68 ceph_decode_32_safe(p, end, info->symlink_len, bad);
69 ceph_decode_need(p, end, info->symlink_len, bad);
71 *p += info->symlink_len;
73 ceph_decode_32_safe(p, end, info->xattr_len, bad);
74 ceph_decode_need(p, end, info->xattr_len, bad);
75 info->xattr_data = *p;
76 *p += info->xattr_len;
83 * parse a normal reply, which may contain a (dir+)dentry and/or a
86 static int parse_reply_info_trace(void **p, void *end,
87 struct ceph_mds_reply_info_parsed *info)
91 if (info->head->is_dentry) {
92 err = parse_reply_info_in(p, end, &info->diri);
96 if (unlikely(*p + sizeof(*info->dirfrag) > end))
99 *p += sizeof(*info->dirfrag) +
100 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
101 if (unlikely(*p > end))
104 ceph_decode_32_safe(p, end, info->dname_len, bad);
105 ceph_decode_need(p, end, info->dname_len, bad);
107 *p += info->dname_len;
109 *p += sizeof(*info->dlease);
112 if (info->head->is_target) {
113 err = parse_reply_info_in(p, end, &info->targeti);
118 if (unlikely(*p != end))
125 pr_err("problem parsing mds trace %d\n", err);
130 * parse readdir results
132 static int parse_reply_info_dir(void **p, void *end,
133 struct ceph_mds_reply_info_parsed *info)
139 if (*p + sizeof(*info->dir_dir) > end)
141 *p += sizeof(*info->dir_dir) +
142 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
146 ceph_decode_need(p, end, sizeof(num) + 2, bad);
147 num = ceph_decode_32(p);
148 info->dir_end = ceph_decode_8(p);
149 info->dir_complete = ceph_decode_8(p);
153 /* alloc large array */
155 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
156 sizeof(*info->dir_dname) +
157 sizeof(*info->dir_dname_len) +
158 sizeof(*info->dir_dlease),
160 if (info->dir_in == NULL) {
164 info->dir_dname = (void *)(info->dir_in + num);
165 info->dir_dname_len = (void *)(info->dir_dname + num);
166 info->dir_dlease = (void *)(info->dir_dname_len + num);
170 ceph_decode_need(p, end, sizeof(u32)*2, bad);
171 info->dir_dname_len[i] = ceph_decode_32(p);
172 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
173 info->dir_dname[i] = *p;
174 *p += info->dir_dname_len[i];
175 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
177 info->dir_dlease[i] = *p;
178 *p += sizeof(struct ceph_mds_reply_lease);
181 err = parse_reply_info_in(p, end, &info->dir_in[i]);
196 pr_err("problem parsing dir contents %d\n", err);
201 * parse entire mds reply
203 static int parse_reply_info(struct ceph_msg *msg,
204 struct ceph_mds_reply_info_parsed *info)
210 info->head = msg->front.iov_base;
211 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
212 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
215 ceph_decode_32_safe(&p, end, len, bad);
217 err = parse_reply_info_trace(&p, p+len, info);
223 ceph_decode_32_safe(&p, end, len, bad);
225 err = parse_reply_info_dir(&p, p+len, info);
231 ceph_decode_32_safe(&p, end, len, bad);
232 info->snapblob_len = len;
243 pr_err("mds parse_reply err %d\n", err);
247 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
256 static const char *session_state_name(int s)
259 case CEPH_MDS_SESSION_NEW: return "new";
260 case CEPH_MDS_SESSION_OPENING: return "opening";
261 case CEPH_MDS_SESSION_OPEN: return "open";
262 case CEPH_MDS_SESSION_HUNG: return "hung";
263 case CEPH_MDS_SESSION_CLOSING: return "closing";
264 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
265 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
266 default: return "???";
270 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
272 if (atomic_inc_not_zero(&s->s_ref)) {
273 dout("mdsc get_session %p %d -> %d\n", s,
274 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
277 dout("mdsc get_session %p 0 -- FAIL", s);
282 void ceph_put_mds_session(struct ceph_mds_session *s)
284 dout("mdsc put_session %p %d -> %d\n", s,
285 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
286 if (atomic_dec_and_test(&s->s_ref)) {
288 s->s_mdsc->client->monc.auth->ops->destroy_authorizer(
289 s->s_mdsc->client->monc.auth, s->s_authorizer);
295 * called under mdsc->mutex
297 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
300 struct ceph_mds_session *session;
302 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
304 session = mdsc->sessions[mds];
305 dout("lookup_mds_session %p %d\n", session,
306 atomic_read(&session->s_ref));
307 get_session(session);
311 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
313 if (mds >= mdsc->max_sessions)
315 return mdsc->sessions[mds];
318 static int __verify_registered_session(struct ceph_mds_client *mdsc,
319 struct ceph_mds_session *s)
321 if (s->s_mds >= mdsc->max_sessions ||
322 mdsc->sessions[s->s_mds] != s)
328 * create+register a new session for given mds.
329 * called under mdsc->mutex.
331 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
334 struct ceph_mds_session *s;
336 s = kzalloc(sizeof(*s), GFP_NOFS);
338 return ERR_PTR(-ENOMEM);
341 s->s_state = CEPH_MDS_SESSION_NEW;
344 mutex_init(&s->s_mutex);
346 ceph_con_init(mdsc->client->msgr, &s->s_con);
347 s->s_con.private = s;
348 s->s_con.ops = &mds_con_ops;
349 s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
350 s->s_con.peer_name.num = cpu_to_le64(mds);
352 spin_lock_init(&s->s_cap_lock);
355 s->s_renew_requested = 0;
357 INIT_LIST_HEAD(&s->s_caps);
360 atomic_set(&s->s_ref, 1);
361 INIT_LIST_HEAD(&s->s_waiting);
362 INIT_LIST_HEAD(&s->s_unsafe);
363 s->s_num_cap_releases = 0;
364 s->s_cap_iterator = NULL;
365 INIT_LIST_HEAD(&s->s_cap_releases);
366 INIT_LIST_HEAD(&s->s_cap_releases_done);
367 INIT_LIST_HEAD(&s->s_cap_flushing);
368 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
370 dout("register_session mds%d\n", mds);
371 if (mds >= mdsc->max_sessions) {
372 int newmax = 1 << get_count_order(mds+1);
373 struct ceph_mds_session **sa;
375 dout("register_session realloc to %d\n", newmax);
376 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
379 if (mdsc->sessions) {
380 memcpy(sa, mdsc->sessions,
381 mdsc->max_sessions * sizeof(void *));
382 kfree(mdsc->sessions);
385 mdsc->max_sessions = newmax;
387 mdsc->sessions[mds] = s;
388 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
390 ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
396 return ERR_PTR(-ENOMEM);
400 * called under mdsc->mutex
402 static void __unregister_session(struct ceph_mds_client *mdsc,
403 struct ceph_mds_session *s)
405 dout("__unregister_session mds%d %p\n", s->s_mds, s);
406 BUG_ON(mdsc->sessions[s->s_mds] != s);
407 mdsc->sessions[s->s_mds] = NULL;
408 ceph_con_close(&s->s_con);
409 ceph_put_mds_session(s);
413 * drop session refs in request.
415 * should be last request ref, or hold mdsc->mutex
417 static void put_request_session(struct ceph_mds_request *req)
419 if (req->r_session) {
420 ceph_put_mds_session(req->r_session);
421 req->r_session = NULL;
425 void ceph_mdsc_release_request(struct kref *kref)
427 struct ceph_mds_request *req = container_of(kref,
428 struct ceph_mds_request,
431 ceph_msg_put(req->r_request);
433 ceph_msg_put(req->r_reply);
434 destroy_reply_info(&req->r_reply_info);
437 ceph_put_cap_refs(ceph_inode(req->r_inode),
441 if (req->r_locked_dir)
442 ceph_put_cap_refs(ceph_inode(req->r_locked_dir),
444 if (req->r_target_inode)
445 iput(req->r_target_inode);
448 if (req->r_old_dentry) {
450 ceph_inode(req->r_old_dentry->d_parent->d_inode),
452 dput(req->r_old_dentry);
456 put_request_session(req);
457 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
462 * lookup session, bump ref if found.
464 * called under mdsc->mutex.
466 static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
469 struct ceph_mds_request *req;
470 struct rb_node *n = mdsc->request_tree.rb_node;
473 req = rb_entry(n, struct ceph_mds_request, r_node);
474 if (tid < req->r_tid)
476 else if (tid > req->r_tid)
479 ceph_mdsc_get_request(req);
486 static void __insert_request(struct ceph_mds_client *mdsc,
487 struct ceph_mds_request *new)
489 struct rb_node **p = &mdsc->request_tree.rb_node;
490 struct rb_node *parent = NULL;
491 struct ceph_mds_request *req = NULL;
495 req = rb_entry(parent, struct ceph_mds_request, r_node);
496 if (new->r_tid < req->r_tid)
498 else if (new->r_tid > req->r_tid)
504 rb_link_node(&new->r_node, parent, p);
505 rb_insert_color(&new->r_node, &mdsc->request_tree);
509 * Register an in-flight request, and assign a tid. Link to directory
510 * are modifying (if any).
512 * Called under mdsc->mutex.
514 static void __register_request(struct ceph_mds_client *mdsc,
515 struct ceph_mds_request *req,
518 req->r_tid = ++mdsc->last_tid;
520 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
522 dout("__register_request %p tid %lld\n", req, req->r_tid);
523 ceph_mdsc_get_request(req);
524 __insert_request(mdsc, req);
527 struct ceph_inode_info *ci = ceph_inode(dir);
529 spin_lock(&ci->i_unsafe_lock);
530 req->r_unsafe_dir = dir;
531 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
532 spin_unlock(&ci->i_unsafe_lock);
536 static void __unregister_request(struct ceph_mds_client *mdsc,
537 struct ceph_mds_request *req)
539 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
540 rb_erase(&req->r_node, &mdsc->request_tree);
541 RB_CLEAR_NODE(&req->r_node);
543 if (req->r_unsafe_dir) {
544 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
546 spin_lock(&ci->i_unsafe_lock);
547 list_del_init(&req->r_unsafe_dir_item);
548 spin_unlock(&ci->i_unsafe_lock);
551 ceph_mdsc_put_request(req);
555 * Choose mds to send request to next. If there is a hint set in the
556 * request (e.g., due to a prior forward hint from the mds), use that.
557 * Otherwise, consult frag tree and/or caps to identify the
558 * appropriate mds. If all else fails, choose randomly.
560 * Called under mdsc->mutex.
562 static int __choose_mds(struct ceph_mds_client *mdsc,
563 struct ceph_mds_request *req)
566 struct ceph_inode_info *ci;
567 struct ceph_cap *cap;
568 int mode = req->r_direct_mode;
570 u32 hash = req->r_direct_hash;
571 bool is_hash = req->r_direct_is_hash;
574 * is there a specific mds we should try? ignore hint if we have
575 * no session and the mds is not up (active or recovering).
577 if (req->r_resend_mds >= 0 &&
578 (__have_session(mdsc, req->r_resend_mds) ||
579 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
580 dout("choose_mds using resend_mds mds%d\n",
582 return req->r_resend_mds;
585 if (mode == USE_RANDOM_MDS)
590 inode = req->r_inode;
591 } else if (req->r_dentry) {
592 if (req->r_dentry->d_inode) {
593 inode = req->r_dentry->d_inode;
595 inode = req->r_dentry->d_parent->d_inode;
596 hash = req->r_dentry->d_name.hash;
600 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
604 ci = ceph_inode(inode);
606 if (is_hash && S_ISDIR(inode->i_mode)) {
607 struct ceph_inode_frag frag;
610 ceph_choose_frag(ci, hash, &frag, &found);
612 if (mode == USE_ANY_MDS && frag.ndist > 0) {
615 /* choose a random replica */
616 get_random_bytes(&r, 1);
619 dout("choose_mds %p %llx.%llx "
620 "frag %u mds%d (%d/%d)\n",
621 inode, ceph_vinop(inode),
627 /* since this file/dir wasn't known to be
628 * replicated, then we want to look for the
629 * authoritative mds. */
632 /* choose auth mds */
634 dout("choose_mds %p %llx.%llx "
635 "frag %u mds%d (auth)\n",
636 inode, ceph_vinop(inode), frag.frag, mds);
642 spin_lock(&inode->i_lock);
644 if (mode == USE_AUTH_MDS)
645 cap = ci->i_auth_cap;
646 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
647 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
649 spin_unlock(&inode->i_lock);
652 mds = cap->session->s_mds;
653 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
654 inode, ceph_vinop(inode), mds,
655 cap == ci->i_auth_cap ? "auth " : "", cap);
656 spin_unlock(&inode->i_lock);
660 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
661 dout("choose_mds chose random mds%d\n", mds);
669 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
671 struct ceph_msg *msg;
672 struct ceph_mds_session_head *h;
674 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS);
676 pr_err("create_session_msg ENOMEM creating msg\n");
679 h = msg->front.iov_base;
680 h->op = cpu_to_le32(op);
681 h->seq = cpu_to_le64(seq);
686 * send session open request.
688 * called under mdsc->mutex
690 static int __open_session(struct ceph_mds_client *mdsc,
691 struct ceph_mds_session *session)
693 struct ceph_msg *msg;
695 int mds = session->s_mds;
697 /* wait for mds to go active? */
698 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
699 dout("open_session to mds%d (%s)\n", mds,
700 ceph_mds_state_name(mstate));
701 session->s_state = CEPH_MDS_SESSION_OPENING;
702 session->s_renew_requested = jiffies;
704 /* send connect message */
705 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
708 ceph_con_send(&session->s_con, msg);
713 * open sessions for any export targets for the given mds
715 * called under mdsc->mutex
717 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
718 struct ceph_mds_session *session)
720 struct ceph_mds_info *mi;
721 struct ceph_mds_session *ts;
722 int i, mds = session->s_mds;
725 if (mds >= mdsc->mdsmap->m_max_mds)
727 mi = &mdsc->mdsmap->m_info[mds];
728 dout("open_export_target_sessions for mds%d (%d targets)\n",
729 session->s_mds, mi->num_export_targets);
731 for (i = 0; i < mi->num_export_targets; i++) {
732 target = mi->export_targets[i];
733 ts = __ceph_lookup_mds_session(mdsc, target);
735 ts = register_session(mdsc, target);
739 if (session->s_state == CEPH_MDS_SESSION_NEW ||
740 session->s_state == CEPH_MDS_SESSION_CLOSING)
741 __open_session(mdsc, session);
743 dout(" mds%d target mds%d %p is %s\n", session->s_mds,
744 i, ts, session_state_name(ts->s_state));
745 ceph_put_mds_session(ts);
749 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
750 struct ceph_mds_session *session)
752 mutex_lock(&mdsc->mutex);
753 __open_export_target_sessions(mdsc, session);
754 mutex_unlock(&mdsc->mutex);
762 * Free preallocated cap messages assigned to this session
764 static void cleanup_cap_releases(struct ceph_mds_session *session)
766 struct ceph_msg *msg;
768 spin_lock(&session->s_cap_lock);
769 while (!list_empty(&session->s_cap_releases)) {
770 msg = list_first_entry(&session->s_cap_releases,
771 struct ceph_msg, list_head);
772 list_del_init(&msg->list_head);
775 while (!list_empty(&session->s_cap_releases_done)) {
776 msg = list_first_entry(&session->s_cap_releases_done,
777 struct ceph_msg, list_head);
778 list_del_init(&msg->list_head);
781 spin_unlock(&session->s_cap_lock);
785 * Helper to safely iterate over all caps associated with a session, with
786 * special care taken to handle a racing __ceph_remove_cap().
788 * Caller must hold session s_mutex.
790 static int iterate_session_caps(struct ceph_mds_session *session,
791 int (*cb)(struct inode *, struct ceph_cap *,
795 struct ceph_cap *cap;
796 struct inode *inode, *last_inode = NULL;
797 struct ceph_cap *old_cap = NULL;
800 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
801 spin_lock(&session->s_cap_lock);
802 p = session->s_caps.next;
803 while (p != &session->s_caps) {
804 cap = list_entry(p, struct ceph_cap, session_caps);
805 inode = igrab(&cap->ci->vfs_inode);
810 session->s_cap_iterator = cap;
811 spin_unlock(&session->s_cap_lock);
818 ceph_put_cap(session->s_mdsc, old_cap);
822 ret = cb(inode, cap, arg);
825 spin_lock(&session->s_cap_lock);
827 if (cap->ci == NULL) {
828 dout("iterate_session_caps finishing cap %p removal\n",
830 BUG_ON(cap->session != session);
831 list_del_init(&cap->session_caps);
832 session->s_nr_caps--;
834 old_cap = cap; /* put_cap it w/o locks held */
841 session->s_cap_iterator = NULL;
842 spin_unlock(&session->s_cap_lock);
847 ceph_put_cap(session->s_mdsc, old_cap);
852 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
855 struct ceph_inode_info *ci = ceph_inode(inode);
858 dout("removing cap %p, ci is %p, inode is %p\n",
859 cap, ci, &ci->vfs_inode);
860 spin_lock(&inode->i_lock);
861 __ceph_remove_cap(cap);
862 if (!__ceph_is_any_real_caps(ci)) {
863 struct ceph_mds_client *mdsc =
864 &ceph_sb_to_client(inode->i_sb)->mdsc;
866 spin_lock(&mdsc->cap_dirty_lock);
867 if (!list_empty(&ci->i_dirty_item)) {
868 pr_info(" dropping dirty %s state for %p %lld\n",
869 ceph_cap_string(ci->i_dirty_caps),
870 inode, ceph_ino(inode));
871 ci->i_dirty_caps = 0;
872 list_del_init(&ci->i_dirty_item);
875 if (!list_empty(&ci->i_flushing_item)) {
876 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
877 ceph_cap_string(ci->i_flushing_caps),
878 inode, ceph_ino(inode));
879 ci->i_flushing_caps = 0;
880 list_del_init(&ci->i_flushing_item);
881 mdsc->num_cap_flushing--;
884 if (drop && ci->i_wrbuffer_ref) {
885 pr_info(" dropping dirty data for %p %lld\n",
886 inode, ceph_ino(inode));
887 ci->i_wrbuffer_ref = 0;
888 ci->i_wrbuffer_ref_head = 0;
891 spin_unlock(&mdsc->cap_dirty_lock);
893 spin_unlock(&inode->i_lock);
900 * caller must hold session s_mutex
902 static void remove_session_caps(struct ceph_mds_session *session)
904 dout("remove_session_caps on %p\n", session);
905 iterate_session_caps(session, remove_session_caps_cb, NULL);
906 BUG_ON(session->s_nr_caps > 0);
907 BUG_ON(!list_empty(&session->s_cap_flushing));
908 cleanup_cap_releases(session);
912 * wake up any threads waiting on this session's caps. if the cap is
913 * old (didn't get renewed on the client reconnect), remove it now.
915 * caller must hold s_mutex.
917 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
920 struct ceph_inode_info *ci = ceph_inode(inode);
922 wake_up_all(&ci->i_cap_wq);
924 spin_lock(&inode->i_lock);
925 ci->i_wanted_max_size = 0;
926 ci->i_requested_max_size = 0;
927 spin_unlock(&inode->i_lock);
932 static void wake_up_session_caps(struct ceph_mds_session *session,
935 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
936 iterate_session_caps(session, wake_up_session_cb,
937 (void *)(unsigned long)reconnect);
941 * Send periodic message to MDS renewing all currently held caps. The
942 * ack will reset the expiration for all caps from this session.
944 * caller holds s_mutex
946 static int send_renew_caps(struct ceph_mds_client *mdsc,
947 struct ceph_mds_session *session)
949 struct ceph_msg *msg;
952 if (time_after_eq(jiffies, session->s_cap_ttl) &&
953 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
954 pr_info("mds%d caps stale\n", session->s_mds);
955 session->s_renew_requested = jiffies;
957 /* do not try to renew caps until a recovering mds has reconnected
958 * with its clients. */
959 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
960 if (state < CEPH_MDS_STATE_RECONNECT) {
961 dout("send_renew_caps ignoring mds%d (%s)\n",
962 session->s_mds, ceph_mds_state_name(state));
966 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
967 ceph_mds_state_name(state));
968 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
969 ++session->s_renew_seq);
972 ceph_con_send(&session->s_con, msg);
977 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
979 * Called under session->s_mutex
981 static void renewed_caps(struct ceph_mds_client *mdsc,
982 struct ceph_mds_session *session, int is_renew)
987 spin_lock(&session->s_cap_lock);
988 was_stale = is_renew && (session->s_cap_ttl == 0 ||
989 time_after_eq(jiffies, session->s_cap_ttl));
991 session->s_cap_ttl = session->s_renew_requested +
992 mdsc->mdsmap->m_session_timeout*HZ;
995 if (time_before(jiffies, session->s_cap_ttl)) {
996 pr_info("mds%d caps renewed\n", session->s_mds);
999 pr_info("mds%d caps still stale\n", session->s_mds);
1002 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1003 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1004 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1005 spin_unlock(&session->s_cap_lock);
1008 wake_up_session_caps(session, 0);
1012 * send a session close request
1014 static int request_close_session(struct ceph_mds_client *mdsc,
1015 struct ceph_mds_session *session)
1017 struct ceph_msg *msg;
1019 dout("request_close_session mds%d state %s seq %lld\n",
1020 session->s_mds, session_state_name(session->s_state),
1022 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1025 ceph_con_send(&session->s_con, msg);
1030 * Called with s_mutex held.
1032 static int __close_session(struct ceph_mds_client *mdsc,
1033 struct ceph_mds_session *session)
1035 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1037 session->s_state = CEPH_MDS_SESSION_CLOSING;
1038 return request_close_session(mdsc, session);
1042 * Trim old(er) caps.
1044 * Because we can't cache an inode without one or more caps, we do
1045 * this indirectly: if a cap is unused, we prune its aliases, at which
1046 * point the inode will hopefully get dropped to.
1048 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1049 * memory pressure from the MDS, though, so it needn't be perfect.
1051 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1053 struct ceph_mds_session *session = arg;
1054 struct ceph_inode_info *ci = ceph_inode(inode);
1055 int used, oissued, mine;
1057 if (session->s_trim_caps <= 0)
1060 spin_lock(&inode->i_lock);
1061 mine = cap->issued | cap->implemented;
1062 used = __ceph_caps_used(ci);
1063 oissued = __ceph_caps_issued_other(ci, cap);
1065 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1066 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1067 ceph_cap_string(used));
1068 if (ci->i_dirty_caps)
1069 goto out; /* dirty caps */
1070 if ((used & ~oissued) & mine)
1071 goto out; /* we need these caps */
1073 session->s_trim_caps--;
1075 /* we aren't the only cap.. just remove us */
1076 __ceph_remove_cap(cap);
1078 /* try to drop referring dentries */
1079 spin_unlock(&inode->i_lock);
1080 d_prune_aliases(inode);
1081 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1082 inode, cap, atomic_read(&inode->i_count));
1087 spin_unlock(&inode->i_lock);
1092 * Trim session cap count down to some max number.
1094 static int trim_caps(struct ceph_mds_client *mdsc,
1095 struct ceph_mds_session *session,
1098 int trim_caps = session->s_nr_caps - max_caps;
1100 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1101 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1102 if (trim_caps > 0) {
1103 session->s_trim_caps = trim_caps;
1104 iterate_session_caps(session, trim_caps_cb, session);
1105 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1106 session->s_mds, session->s_nr_caps, max_caps,
1107 trim_caps - session->s_trim_caps);
1108 session->s_trim_caps = 0;
1114 * Allocate cap_release messages. If there is a partially full message
1115 * in the queue, try to allocate enough to cover it's remainder, so that
1116 * we can send it immediately.
1118 * Called under s_mutex.
1120 int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
1121 struct ceph_mds_session *session)
1123 struct ceph_msg *msg, *partial = NULL;
1124 struct ceph_mds_cap_release *head;
1126 int extra = mdsc->client->mount_args->cap_release_safety;
1129 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1132 spin_lock(&session->s_cap_lock);
1134 if (!list_empty(&session->s_cap_releases)) {
1135 msg = list_first_entry(&session->s_cap_releases,
1138 head = msg->front.iov_base;
1139 num = le32_to_cpu(head->num);
1141 dout(" partial %p with (%d/%d)\n", msg, num,
1142 (int)CEPH_CAPS_PER_RELEASE);
1143 extra += CEPH_CAPS_PER_RELEASE - num;
1147 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1148 spin_unlock(&session->s_cap_lock);
1149 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1153 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1154 (int)msg->front.iov_len);
1155 head = msg->front.iov_base;
1156 head->num = cpu_to_le32(0);
1157 msg->front.iov_len = sizeof(*head);
1158 spin_lock(&session->s_cap_lock);
1159 list_add(&msg->list_head, &session->s_cap_releases);
1160 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1164 head = partial->front.iov_base;
1165 num = le32_to_cpu(head->num);
1166 dout(" queueing partial %p with %d/%d\n", partial, num,
1167 (int)CEPH_CAPS_PER_RELEASE);
1168 list_move_tail(&partial->list_head,
1169 &session->s_cap_releases_done);
1170 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
1173 spin_unlock(&session->s_cap_lock);
1179 * flush all dirty inode data to disk.
1181 * returns true if we've flushed through want_flush_seq
1183 static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1187 dout("check_cap_flush want %lld\n", want_flush_seq);
1188 mutex_lock(&mdsc->mutex);
1189 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1190 struct ceph_mds_session *session = mdsc->sessions[mds];
1194 get_session(session);
1195 mutex_unlock(&mdsc->mutex);
1197 mutex_lock(&session->s_mutex);
1198 if (!list_empty(&session->s_cap_flushing)) {
1199 struct ceph_inode_info *ci =
1200 list_entry(session->s_cap_flushing.next,
1201 struct ceph_inode_info,
1203 struct inode *inode = &ci->vfs_inode;
1205 spin_lock(&inode->i_lock);
1206 if (ci->i_cap_flush_seq <= want_flush_seq) {
1207 dout("check_cap_flush still flushing %p "
1208 "seq %lld <= %lld to mds%d\n", inode,
1209 ci->i_cap_flush_seq, want_flush_seq,
1213 spin_unlock(&inode->i_lock);
1215 mutex_unlock(&session->s_mutex);
1216 ceph_put_mds_session(session);
1220 mutex_lock(&mdsc->mutex);
1223 mutex_unlock(&mdsc->mutex);
1224 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1229 * called under s_mutex
1231 void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1232 struct ceph_mds_session *session)
1234 struct ceph_msg *msg;
1236 dout("send_cap_releases mds%d\n", session->s_mds);
1237 spin_lock(&session->s_cap_lock);
1238 while (!list_empty(&session->s_cap_releases_done)) {
1239 msg = list_first_entry(&session->s_cap_releases_done,
1240 struct ceph_msg, list_head);
1241 list_del_init(&msg->list_head);
1242 spin_unlock(&session->s_cap_lock);
1243 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1244 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1245 ceph_con_send(&session->s_con, msg);
1246 spin_lock(&session->s_cap_lock);
1248 spin_unlock(&session->s_cap_lock);
1251 static void discard_cap_releases(struct ceph_mds_client *mdsc,
1252 struct ceph_mds_session *session)
1254 struct ceph_msg *msg;
1255 struct ceph_mds_cap_release *head;
1258 dout("discard_cap_releases mds%d\n", session->s_mds);
1259 spin_lock(&session->s_cap_lock);
1261 /* zero out the in-progress message */
1262 msg = list_first_entry(&session->s_cap_releases,
1263 struct ceph_msg, list_head);
1264 head = msg->front.iov_base;
1265 num = le32_to_cpu(head->num);
1266 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1267 head->num = cpu_to_le32(0);
1268 session->s_num_cap_releases += num;
1270 /* requeue completed messages */
1271 while (!list_empty(&session->s_cap_releases_done)) {
1272 msg = list_first_entry(&session->s_cap_releases_done,
1273 struct ceph_msg, list_head);
1274 list_del_init(&msg->list_head);
1276 head = msg->front.iov_base;
1277 num = le32_to_cpu(head->num);
1278 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1280 session->s_num_cap_releases += num;
1281 head->num = cpu_to_le32(0);
1282 msg->front.iov_len = sizeof(*head);
1283 list_add(&msg->list_head, &session->s_cap_releases);
1286 spin_unlock(&session->s_cap_lock);
1294 * Create an mds request.
1296 struct ceph_mds_request *
1297 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1299 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1302 return ERR_PTR(-ENOMEM);
1304 mutex_init(&req->r_fill_mutex);
1306 req->r_started = jiffies;
1307 req->r_resend_mds = -1;
1308 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1310 kref_init(&req->r_kref);
1311 INIT_LIST_HEAD(&req->r_wait);
1312 init_completion(&req->r_completion);
1313 init_completion(&req->r_safe_completion);
1314 INIT_LIST_HEAD(&req->r_unsafe_item);
1317 req->r_direct_mode = mode;
1322 * return oldest (lowest) request, tid in request tree, 0 if none.
1324 * called under mdsc->mutex.
1326 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1328 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1330 return rb_entry(rb_first(&mdsc->request_tree),
1331 struct ceph_mds_request, r_node);
1334 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1336 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1344 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1345 * on build_path_from_dentry in fs/cifs/dir.c.
1347 * If @stop_on_nosnap, generate path relative to the first non-snapped
1350 * Encode hidden .snap dirs as a double /, i.e.
1351 * foo/.snap/bar -> foo//bar
1353 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1356 struct dentry *temp;
1361 return ERR_PTR(-EINVAL);
1365 for (temp = dentry; !IS_ROOT(temp);) {
1366 struct inode *inode = temp->d_inode;
1367 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1368 len++; /* slash only */
1369 else if (stop_on_nosnap && inode &&
1370 ceph_snap(inode) == CEPH_NOSNAP)
1373 len += 1 + temp->d_name.len;
1374 temp = temp->d_parent;
1376 pr_err("build_path corrupt dentry %p\n", dentry);
1377 return ERR_PTR(-EINVAL);
1381 len--; /* no leading '/' */
1383 path = kmalloc(len+1, GFP_NOFS);
1385 return ERR_PTR(-ENOMEM);
1387 path[pos] = 0; /* trailing null */
1388 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1389 struct inode *inode = temp->d_inode;
1391 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1392 dout("build_path path+%d: %p SNAPDIR\n",
1394 } else if (stop_on_nosnap && inode &&
1395 ceph_snap(inode) == CEPH_NOSNAP) {
1398 pos -= temp->d_name.len;
1401 strncpy(path + pos, temp->d_name.name,
1406 temp = temp->d_parent;
1408 pr_err("build_path corrupt dentry\n");
1410 return ERR_PTR(-EINVAL);
1414 pr_err("build_path did not end path lookup where "
1415 "expected, namelen is %d, pos is %d\n", len, pos);
1416 /* presumably this is only possible if racing with a
1417 rename of one of the parent directories (we can not
1418 lock the dentries above us to prevent this, but
1419 retrying should be harmless) */
1424 *base = ceph_ino(temp->d_inode);
1426 dout("build_path on %p %d built %llx '%.*s'\n",
1427 dentry, atomic_read(&dentry->d_count), *base, len, path);
1431 static int build_dentry_path(struct dentry *dentry,
1432 const char **ppath, int *ppathlen, u64 *pino,
1437 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1438 *pino = ceph_ino(dentry->d_parent->d_inode);
1439 *ppath = dentry->d_name.name;
1440 *ppathlen = dentry->d_name.len;
1443 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1445 return PTR_ERR(path);
1451 static int build_inode_path(struct inode *inode,
1452 const char **ppath, int *ppathlen, u64 *pino,
1455 struct dentry *dentry;
1458 if (ceph_snap(inode) == CEPH_NOSNAP) {
1459 *pino = ceph_ino(inode);
1463 dentry = d_find_alias(inode);
1464 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1467 return PTR_ERR(path);
1474 * request arguments may be specified via an inode *, a dentry *, or
1475 * an explicit ino+path.
1477 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1478 const char *rpath, u64 rino,
1479 const char **ppath, int *pathlen,
1480 u64 *ino, int *freepath)
1485 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1486 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1488 } else if (rdentry) {
1489 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1490 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1495 *pathlen = strlen(rpath);
1496 dout(" path %.*s\n", *pathlen, rpath);
1503 * called under mdsc->mutex
1505 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1506 struct ceph_mds_request *req,
1509 struct ceph_msg *msg;
1510 struct ceph_mds_request_head *head;
1511 const char *path1 = NULL;
1512 const char *path2 = NULL;
1513 u64 ino1 = 0, ino2 = 0;
1514 int pathlen1 = 0, pathlen2 = 0;
1515 int freepath1 = 0, freepath2 = 0;
1521 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1522 req->r_path1, req->r_ino1.ino,
1523 &path1, &pathlen1, &ino1, &freepath1);
1529 ret = set_request_path_attr(NULL, req->r_old_dentry,
1530 req->r_path2, req->r_ino2.ino,
1531 &path2, &pathlen2, &ino2, &freepath2);
1537 len = sizeof(*head) +
1538 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
1540 /* calculate (max) length for cap releases */
1541 len += sizeof(struct ceph_mds_request_release) *
1542 (!!req->r_inode_drop + !!req->r_dentry_drop +
1543 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1544 if (req->r_dentry_drop)
1545 len += req->r_dentry->d_name.len;
1546 if (req->r_old_dentry_drop)
1547 len += req->r_old_dentry->d_name.len;
1549 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS);
1551 msg = ERR_PTR(-ENOMEM);
1555 msg->hdr.tid = cpu_to_le64(req->r_tid);
1557 head = msg->front.iov_base;
1558 p = msg->front.iov_base + sizeof(*head);
1559 end = msg->front.iov_base + msg->front.iov_len;
1561 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1562 head->op = cpu_to_le32(req->r_op);
1563 head->caller_uid = cpu_to_le32(current_fsuid());
1564 head->caller_gid = cpu_to_le32(current_fsgid());
1565 head->args = req->r_args;
1567 ceph_encode_filepath(&p, end, ino1, path1);
1568 ceph_encode_filepath(&p, end, ino2, path2);
1570 /* make note of release offset, in case we need to replay */
1571 req->r_request_release_offset = p - msg->front.iov_base;
1575 if (req->r_inode_drop)
1576 releases += ceph_encode_inode_release(&p,
1577 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1578 mds, req->r_inode_drop, req->r_inode_unless, 0);
1579 if (req->r_dentry_drop)
1580 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1581 mds, req->r_dentry_drop, req->r_dentry_unless);
1582 if (req->r_old_dentry_drop)
1583 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1584 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1585 if (req->r_old_inode_drop)
1586 releases += ceph_encode_inode_release(&p,
1587 req->r_old_dentry->d_inode,
1588 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1589 head->num_releases = cpu_to_le16(releases);
1592 msg->front.iov_len = p - msg->front.iov_base;
1593 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1595 msg->pages = req->r_pages;
1596 msg->nr_pages = req->r_num_pages;
1597 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1598 msg->hdr.data_off = cpu_to_le16(0);
1602 kfree((char *)path2);
1605 kfree((char *)path1);
1611 * called under mdsc->mutex if error, under no mutex if
1614 static void complete_request(struct ceph_mds_client *mdsc,
1615 struct ceph_mds_request *req)
1617 if (req->r_callback)
1618 req->r_callback(mdsc, req);
1620 complete_all(&req->r_completion);
1624 * called under mdsc->mutex
1626 static int __prepare_send_request(struct ceph_mds_client *mdsc,
1627 struct ceph_mds_request *req,
1630 struct ceph_mds_request_head *rhead;
1631 struct ceph_msg *msg;
1637 struct ceph_cap *cap =
1638 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1641 req->r_sent_on_mseq = cap->mseq;
1643 req->r_sent_on_mseq = -1;
1645 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1646 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1648 if (req->r_got_unsafe) {
1650 * Replay. Do not regenerate message (and rebuild
1651 * paths, etc.); just use the original message.
1652 * Rebuilding paths will break for renames because
1653 * d_move mangles the src name.
1655 msg = req->r_request;
1656 rhead = msg->front.iov_base;
1658 flags = le32_to_cpu(rhead->flags);
1659 flags |= CEPH_MDS_FLAG_REPLAY;
1660 rhead->flags = cpu_to_le32(flags);
1662 if (req->r_target_inode)
1663 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1665 rhead->num_retry = req->r_attempts - 1;
1667 /* remove cap/dentry releases from message */
1668 rhead->num_releases = 0;
1669 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1670 msg->front.iov_len = req->r_request_release_offset;
1674 if (req->r_request) {
1675 ceph_msg_put(req->r_request);
1676 req->r_request = NULL;
1678 msg = create_request_message(mdsc, req, mds);
1680 req->r_err = PTR_ERR(msg);
1681 complete_request(mdsc, req);
1682 return PTR_ERR(msg);
1684 req->r_request = msg;
1686 rhead = msg->front.iov_base;
1687 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1688 if (req->r_got_unsafe)
1689 flags |= CEPH_MDS_FLAG_REPLAY;
1690 if (req->r_locked_dir)
1691 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1692 rhead->flags = cpu_to_le32(flags);
1693 rhead->num_fwd = req->r_num_fwd;
1694 rhead->num_retry = req->r_attempts - 1;
1697 dout(" r_locked_dir = %p\n", req->r_locked_dir);
1702 * send request, or put it on the appropriate wait list.
1704 static int __do_request(struct ceph_mds_client *mdsc,
1705 struct ceph_mds_request *req)
1707 struct ceph_mds_session *session = NULL;
1711 if (req->r_err || req->r_got_result)
1714 if (req->r_timeout &&
1715 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1716 dout("do_request timed out\n");
1721 mds = __choose_mds(mdsc, req);
1723 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1724 dout("do_request no mds or not active, waiting for map\n");
1725 list_add(&req->r_wait, &mdsc->waiting_for_map);
1729 /* get, open session */
1730 session = __ceph_lookup_mds_session(mdsc, mds);
1732 session = register_session(mdsc, mds);
1733 if (IS_ERR(session)) {
1734 err = PTR_ERR(session);
1738 dout("do_request mds%d session %p state %s\n", mds, session,
1739 session_state_name(session->s_state));
1740 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1741 session->s_state != CEPH_MDS_SESSION_HUNG) {
1742 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1743 session->s_state == CEPH_MDS_SESSION_CLOSING)
1744 __open_session(mdsc, session);
1745 list_add(&req->r_wait, &session->s_waiting);
1750 req->r_session = get_session(session);
1751 req->r_resend_mds = -1; /* forget any previous mds hint */
1753 if (req->r_request_started == 0) /* note request start time */
1754 req->r_request_started = jiffies;
1756 err = __prepare_send_request(mdsc, req, mds);
1758 ceph_msg_get(req->r_request);
1759 ceph_con_send(&session->s_con, req->r_request);
1763 ceph_put_mds_session(session);
1769 complete_request(mdsc, req);
1774 * called under mdsc->mutex
1776 static void __wake_requests(struct ceph_mds_client *mdsc,
1777 struct list_head *head)
1779 struct ceph_mds_request *req, *nreq;
1781 list_for_each_entry_safe(req, nreq, head, r_wait) {
1782 list_del_init(&req->r_wait);
1783 __do_request(mdsc, req);
1788 * Wake up threads with requests pending for @mds, so that they can
1789 * resubmit their requests to a possibly different mds.
1791 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
1793 struct ceph_mds_request *req;
1796 dout("kick_requests mds%d\n", mds);
1797 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1798 req = rb_entry(p, struct ceph_mds_request, r_node);
1799 if (req->r_got_unsafe)
1801 if (req->r_session &&
1802 req->r_session->s_mds == mds) {
1803 dout(" kicking tid %llu\n", req->r_tid);
1804 put_request_session(req);
1805 __do_request(mdsc, req);
1810 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1811 struct ceph_mds_request *req)
1813 dout("submit_request on %p\n", req);
1814 mutex_lock(&mdsc->mutex);
1815 __register_request(mdsc, req, NULL);
1816 __do_request(mdsc, req);
1817 mutex_unlock(&mdsc->mutex);
1821 * Synchrously perform an mds request. Take care of all of the
1822 * session setup, forwarding, retry details.
1824 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1826 struct ceph_mds_request *req)
1830 dout("do_request on %p\n", req);
1832 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1834 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1835 if (req->r_locked_dir)
1836 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1837 if (req->r_old_dentry)
1839 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1843 mutex_lock(&mdsc->mutex);
1844 __register_request(mdsc, req, dir);
1845 __do_request(mdsc, req);
1849 __unregister_request(mdsc, req);
1850 dout("do_request early error %d\n", err);
1855 mutex_unlock(&mdsc->mutex);
1856 dout("do_request waiting\n");
1857 if (req->r_timeout) {
1858 err = (long)wait_for_completion_killable_timeout(
1859 &req->r_completion, req->r_timeout);
1863 err = wait_for_completion_killable(&req->r_completion);
1865 dout("do_request waited, got %d\n", err);
1866 mutex_lock(&mdsc->mutex);
1868 /* only abort if we didn't race with a real reply */
1869 if (req->r_got_result) {
1870 err = le32_to_cpu(req->r_reply_info.head->result);
1871 } else if (err < 0) {
1872 dout("aborted request %lld with %d\n", req->r_tid, err);
1875 * ensure we aren't running concurrently with
1876 * ceph_fill_trace or ceph_readdir_prepopulate, which
1877 * rely on locks (dir mutex) held by our caller.
1879 mutex_lock(&req->r_fill_mutex);
1881 req->r_aborted = true;
1882 mutex_unlock(&req->r_fill_mutex);
1884 if (req->r_locked_dir &&
1885 (req->r_op & CEPH_MDS_OP_WRITE))
1886 ceph_invalidate_dir_request(req);
1892 mutex_unlock(&mdsc->mutex);
1893 dout("do_request %p done, result %d\n", req, err);
1898 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1899 * namespace request.
1901 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
1903 struct inode *inode = req->r_locked_dir;
1904 struct ceph_inode_info *ci = ceph_inode(inode);
1906 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode);
1907 spin_lock(&inode->i_lock);
1908 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1909 ci->i_release_count++;
1910 spin_unlock(&inode->i_lock);
1913 ceph_invalidate_dentry_lease(req->r_dentry);
1914 if (req->r_old_dentry)
1915 ceph_invalidate_dentry_lease(req->r_old_dentry);
1921 * We take the session mutex and parse and process the reply immediately.
1922 * This preserves the logical ordering of replies, capabilities, etc., sent
1923 * by the MDS as they are applied to our local cache.
1925 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
1927 struct ceph_mds_client *mdsc = session->s_mdsc;
1928 struct ceph_mds_request *req;
1929 struct ceph_mds_reply_head *head = msg->front.iov_base;
1930 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
1933 int mds = session->s_mds;
1935 if (msg->front.iov_len < sizeof(*head)) {
1936 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
1941 /* get request, session */
1942 tid = le64_to_cpu(msg->hdr.tid);
1943 mutex_lock(&mdsc->mutex);
1944 req = __lookup_request(mdsc, tid);
1946 dout("handle_reply on unknown tid %llu\n", tid);
1947 mutex_unlock(&mdsc->mutex);
1950 dout("handle_reply %p\n", req);
1952 /* correct session? */
1953 if (req->r_session != session) {
1954 pr_err("mdsc_handle_reply got %llu on session mds%d"
1955 " not mds%d\n", tid, session->s_mds,
1956 req->r_session ? req->r_session->s_mds : -1);
1957 mutex_unlock(&mdsc->mutex);
1962 if ((req->r_got_unsafe && !head->safe) ||
1963 (req->r_got_safe && head->safe)) {
1964 pr_warning("got a dup %s reply on %llu from mds%d\n",
1965 head->safe ? "safe" : "unsafe", tid, mds);
1966 mutex_unlock(&mdsc->mutex);
1969 if (req->r_got_safe && !head->safe) {
1970 pr_warning("got unsafe after safe on %llu from mds%d\n",
1972 mutex_unlock(&mdsc->mutex);
1976 result = le32_to_cpu(head->result);
1980 * if we're not talking to the authority, send to them
1981 * if the authority has changed while we weren't looking,
1982 * send to new authority
1983 * Otherwise we just have to return an ESTALE
1985 if (result == -ESTALE) {
1986 dout("got ESTALE on request %llu", req->r_tid);
1987 if (!req->r_inode) ; //do nothing; not an authority problem
1988 else if (req->r_direct_mode != USE_AUTH_MDS) {
1989 dout("not using auth, setting for that now");
1990 req->r_direct_mode = USE_AUTH_MDS;
1991 __do_request(mdsc, req);
1992 mutex_unlock(&mdsc->mutex);
1995 struct ceph_inode_info *ci = ceph_inode(req->r_inode);
1996 struct ceph_cap *cap =
1997 ceph_get_cap_for_mds(ci, req->r_mds);;
1999 dout("already using auth");
2000 if ((!cap || cap != ci->i_auth_cap) ||
2001 (cap->mseq != req->r_sent_on_mseq)) {
2002 dout("but cap changed, so resending");
2003 __do_request(mdsc, req);
2004 mutex_unlock(&mdsc->mutex);
2008 dout("have to return ESTALE on request %llu", req->r_tid);
2013 req->r_got_safe = true;
2014 __unregister_request(mdsc, req);
2015 complete_all(&req->r_safe_completion);
2017 if (req->r_got_unsafe) {
2019 * We already handled the unsafe response, now do the
2020 * cleanup. No need to examine the response; the MDS
2021 * doesn't include any result info in the safe
2022 * response. And even if it did, there is nothing
2023 * useful we could do with a revised return value.
2025 dout("got safe reply %llu, mds%d\n", tid, mds);
2026 list_del_init(&req->r_unsafe_item);
2028 /* last unsafe request during umount? */
2029 if (mdsc->stopping && !__get_oldest_req(mdsc))
2030 complete_all(&mdsc->safe_umount_waiters);
2031 mutex_unlock(&mdsc->mutex);
2035 req->r_got_unsafe = true;
2036 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2039 dout("handle_reply tid %lld result %d\n", tid, result);
2040 rinfo = &req->r_reply_info;
2041 err = parse_reply_info(msg, rinfo);
2042 mutex_unlock(&mdsc->mutex);
2044 mutex_lock(&session->s_mutex);
2046 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds);
2052 if (rinfo->snapblob_len) {
2053 down_write(&mdsc->snap_rwsem);
2054 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2055 rinfo->snapblob + rinfo->snapblob_len,
2056 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2057 downgrade_write(&mdsc->snap_rwsem);
2059 down_read(&mdsc->snap_rwsem);
2062 /* insert trace into our cache */
2063 mutex_lock(&req->r_fill_mutex);
2064 err = ceph_fill_trace(mdsc->client->sb, req, req->r_session);
2066 if (result == 0 && rinfo->dir_nr)
2067 ceph_readdir_prepopulate(req, req->r_session);
2068 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2070 mutex_unlock(&req->r_fill_mutex);
2072 up_read(&mdsc->snap_rwsem);
2074 mutex_lock(&mdsc->mutex);
2075 if (!req->r_aborted) {
2081 req->r_got_result = true;
2084 dout("reply arrived after request %lld was aborted\n", tid);
2086 mutex_unlock(&mdsc->mutex);
2088 ceph_add_cap_releases(mdsc, req->r_session);
2089 mutex_unlock(&session->s_mutex);
2091 /* kick calling process */
2092 complete_request(mdsc, req);
2094 ceph_mdsc_put_request(req);
2101 * handle mds notification that our request has been forwarded.
2103 static void handle_forward(struct ceph_mds_client *mdsc,
2104 struct ceph_mds_session *session,
2105 struct ceph_msg *msg)
2107 struct ceph_mds_request *req;
2108 u64 tid = le64_to_cpu(msg->hdr.tid);
2112 void *p = msg->front.iov_base;
2113 void *end = p + msg->front.iov_len;
2115 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2116 next_mds = ceph_decode_32(&p);
2117 fwd_seq = ceph_decode_32(&p);
2119 mutex_lock(&mdsc->mutex);
2120 req = __lookup_request(mdsc, tid);
2122 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2123 goto out; /* dup reply? */
2126 if (req->r_aborted) {
2127 dout("forward tid %llu aborted, unregistering\n", tid);
2128 __unregister_request(mdsc, req);
2129 } else if (fwd_seq <= req->r_num_fwd) {
2130 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2131 tid, next_mds, req->r_num_fwd, fwd_seq);
2133 /* resend. forward race not possible; mds would drop */
2134 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2136 BUG_ON(req->r_got_result);
2137 req->r_num_fwd = fwd_seq;
2138 req->r_resend_mds = next_mds;
2139 put_request_session(req);
2140 __do_request(mdsc, req);
2142 ceph_mdsc_put_request(req);
2144 mutex_unlock(&mdsc->mutex);
2148 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2152 * handle a mds session control message
2154 static void handle_session(struct ceph_mds_session *session,
2155 struct ceph_msg *msg)
2157 struct ceph_mds_client *mdsc = session->s_mdsc;
2160 int mds = session->s_mds;
2161 struct ceph_mds_session_head *h = msg->front.iov_base;
2165 if (msg->front.iov_len != sizeof(*h))
2167 op = le32_to_cpu(h->op);
2168 seq = le64_to_cpu(h->seq);
2170 mutex_lock(&mdsc->mutex);
2171 if (op == CEPH_SESSION_CLOSE)
2172 __unregister_session(mdsc, session);
2173 /* FIXME: this ttl calculation is generous */
2174 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2175 mutex_unlock(&mdsc->mutex);
2177 mutex_lock(&session->s_mutex);
2179 dout("handle_session mds%d %s %p state %s seq %llu\n",
2180 mds, ceph_session_op_name(op), session,
2181 session_state_name(session->s_state), seq);
2183 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2184 session->s_state = CEPH_MDS_SESSION_OPEN;
2185 pr_info("mds%d came back\n", session->s_mds);
2189 case CEPH_SESSION_OPEN:
2190 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2191 pr_info("mds%d reconnect success\n", session->s_mds);
2192 session->s_state = CEPH_MDS_SESSION_OPEN;
2193 renewed_caps(mdsc, session, 0);
2196 __close_session(mdsc, session);
2199 case CEPH_SESSION_RENEWCAPS:
2200 if (session->s_renew_seq == seq)
2201 renewed_caps(mdsc, session, 1);
2204 case CEPH_SESSION_CLOSE:
2205 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2206 pr_info("mds%d reconnect denied\n", session->s_mds);
2207 remove_session_caps(session);
2208 wake = 1; /* for good measure */
2209 complete_all(&mdsc->session_close_waiters);
2210 kick_requests(mdsc, mds);
2213 case CEPH_SESSION_STALE:
2214 pr_info("mds%d caps went stale, renewing\n",
2216 spin_lock(&session->s_cap_lock);
2217 session->s_cap_gen++;
2218 session->s_cap_ttl = 0;
2219 spin_unlock(&session->s_cap_lock);
2220 send_renew_caps(mdsc, session);
2223 case CEPH_SESSION_RECALL_STATE:
2224 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2228 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2232 mutex_unlock(&session->s_mutex);
2234 mutex_lock(&mdsc->mutex);
2235 __wake_requests(mdsc, &session->s_waiting);
2236 mutex_unlock(&mdsc->mutex);
2241 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2242 (int)msg->front.iov_len);
2249 * called under session->mutex.
2251 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2252 struct ceph_mds_session *session)
2254 struct ceph_mds_request *req, *nreq;
2257 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2259 mutex_lock(&mdsc->mutex);
2260 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2261 err = __prepare_send_request(mdsc, req, session->s_mds);
2263 ceph_msg_get(req->r_request);
2264 ceph_con_send(&session->s_con, req->r_request);
2267 mutex_unlock(&mdsc->mutex);
2271 * Encode information about a cap for a reconnect with the MDS.
2273 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2277 struct ceph_mds_cap_reconnect v2;
2278 struct ceph_mds_cap_reconnect_v1 v1;
2281 struct ceph_inode_info *ci;
2282 struct ceph_reconnect_state *recon_state = arg;
2283 struct ceph_pagelist *pagelist = recon_state->pagelist;
2287 struct dentry *dentry;
2291 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2292 inode, ceph_vinop(inode), cap, cap->cap_id,
2293 ceph_cap_string(cap->issued));
2294 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2298 dentry = d_find_alias(inode);
2300 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2302 err = PTR_ERR(path);
2309 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2313 spin_lock(&inode->i_lock);
2314 cap->seq = 0; /* reset cap seq */
2315 cap->issue_seq = 0; /* and issue_seq */
2317 if (recon_state->flock) {
2318 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2319 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2320 rec.v2.issued = cpu_to_le32(cap->issued);
2321 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2322 rec.v2.pathbase = cpu_to_le64(pathbase);
2323 rec.v2.flock_len = 0;
2324 reclen = sizeof(rec.v2);
2326 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2327 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2328 rec.v1.issued = cpu_to_le32(cap->issued);
2329 rec.v1.size = cpu_to_le64(inode->i_size);
2330 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2331 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2332 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2333 rec.v1.pathbase = cpu_to_le64(pathbase);
2334 reclen = sizeof(rec.v1);
2336 spin_unlock(&inode->i_lock);
2338 err = ceph_pagelist_append(pagelist, &rec, reclen);
2348 * If an MDS fails and recovers, clients need to reconnect in order to
2349 * reestablish shared state. This includes all caps issued through
2350 * this session _and_ the snap_realm hierarchy. Because it's not
2351 * clear which snap realms the mds cares about, we send everything we
2352 * know about.. that ensures we'll then get any new info the
2353 * recovering MDS might have.
2355 * This is a relatively heavyweight operation, but it's rare.
2357 * called with mdsc->mutex held.
2359 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2360 struct ceph_mds_session *session)
2362 struct ceph_msg *reply;
2364 int mds = session->s_mds;
2366 struct ceph_pagelist *pagelist;
2367 struct ceph_reconnect_state recon_state;
2369 pr_info("mds%d reconnect start\n", mds);
2371 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2373 goto fail_nopagelist;
2374 ceph_pagelist_init(pagelist);
2376 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS);
2380 mutex_lock(&session->s_mutex);
2381 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2384 ceph_con_open(&session->s_con,
2385 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2387 /* replay unsafe requests */
2388 replay_unsafe_requests(mdsc, session);
2390 down_read(&mdsc->snap_rwsem);
2392 dout("session %p state %s\n", session,
2393 session_state_name(session->s_state));
2395 /* drop old cap expires; we're about to reestablish that state */
2396 discard_cap_releases(mdsc, session);
2398 /* traverse this session's caps */
2399 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2403 recon_state.pagelist = pagelist;
2404 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2405 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
2410 * snaprealms. we provide mds with the ino, seq (version), and
2411 * parent for all of our realms. If the mds has any newer info,
2414 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2415 struct ceph_snap_realm *realm =
2416 rb_entry(p, struct ceph_snap_realm, node);
2417 struct ceph_mds_snaprealm_reconnect sr_rec;
2419 dout(" adding snap realm %llx seq %lld parent %llx\n",
2420 realm->ino, realm->seq, realm->parent_ino);
2421 sr_rec.ino = cpu_to_le64(realm->ino);
2422 sr_rec.seq = cpu_to_le64(realm->seq);
2423 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2424 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2429 reply->pagelist = pagelist;
2430 if (recon_state.flock)
2431 reply->hdr.version = cpu_to_le16(2);
2432 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2433 reply->nr_pages = calc_pages_for(0, pagelist->length);
2434 ceph_con_send(&session->s_con, reply);
2436 mutex_unlock(&session->s_mutex);
2438 mutex_lock(&mdsc->mutex);
2439 __wake_requests(mdsc, &session->s_waiting);
2440 mutex_unlock(&mdsc->mutex);
2442 up_read(&mdsc->snap_rwsem);
2446 ceph_msg_put(reply);
2447 up_read(&mdsc->snap_rwsem);
2448 mutex_unlock(&session->s_mutex);
2450 ceph_pagelist_release(pagelist);
2453 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
2459 * compare old and new mdsmaps, kicking requests
2460 * and closing out old connections as necessary
2462 * called under mdsc->mutex.
2464 static void check_new_map(struct ceph_mds_client *mdsc,
2465 struct ceph_mdsmap *newmap,
2466 struct ceph_mdsmap *oldmap)
2469 int oldstate, newstate;
2470 struct ceph_mds_session *s;
2472 dout("check_new_map new %u old %u\n",
2473 newmap->m_epoch, oldmap->m_epoch);
2475 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2476 if (mdsc->sessions[i] == NULL)
2478 s = mdsc->sessions[i];
2479 oldstate = ceph_mdsmap_get_state(oldmap, i);
2480 newstate = ceph_mdsmap_get_state(newmap, i);
2482 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2483 i, ceph_mds_state_name(oldstate),
2484 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
2485 ceph_mds_state_name(newstate),
2486 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
2487 session_state_name(s->s_state));
2489 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2490 ceph_mdsmap_get_addr(newmap, i),
2491 sizeof(struct ceph_entity_addr))) {
2492 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2493 /* the session never opened, just close it
2495 __wake_requests(mdsc, &s->s_waiting);
2496 __unregister_session(mdsc, s);
2499 mutex_unlock(&mdsc->mutex);
2500 mutex_lock(&s->s_mutex);
2501 mutex_lock(&mdsc->mutex);
2502 ceph_con_close(&s->s_con);
2503 mutex_unlock(&s->s_mutex);
2504 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2507 /* kick any requests waiting on the recovering mds */
2508 kick_requests(mdsc, i);
2509 } else if (oldstate == newstate) {
2510 continue; /* nothing new with this mds */
2516 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
2517 newstate >= CEPH_MDS_STATE_RECONNECT) {
2518 mutex_unlock(&mdsc->mutex);
2519 send_mds_reconnect(mdsc, s);
2520 mutex_lock(&mdsc->mutex);
2524 * kick request on any mds that has gone active.
2526 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2527 newstate >= CEPH_MDS_STATE_ACTIVE) {
2528 if (oldstate != CEPH_MDS_STATE_CREATING &&
2529 oldstate != CEPH_MDS_STATE_STARTING)
2530 pr_info("mds%d recovery completed\n", s->s_mds);
2531 kick_requests(mdsc, i);
2532 ceph_kick_flushing_caps(mdsc, s);
2533 wake_up_session_caps(s, 1);
2537 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2538 s = mdsc->sessions[i];
2541 if (!ceph_mdsmap_is_laggy(newmap, i))
2543 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2544 s->s_state == CEPH_MDS_SESSION_HUNG ||
2545 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2546 dout(" connecting to export targets of laggy mds%d\n",
2548 __open_export_target_sessions(mdsc, s);
2560 * caller must hold session s_mutex, dentry->d_lock
2562 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2564 struct ceph_dentry_info *di = ceph_dentry(dentry);
2566 ceph_put_mds_session(di->lease_session);
2567 di->lease_session = NULL;
2570 static void handle_lease(struct ceph_mds_client *mdsc,
2571 struct ceph_mds_session *session,
2572 struct ceph_msg *msg)
2574 struct super_block *sb = mdsc->client->sb;
2575 struct inode *inode;
2576 struct ceph_inode_info *ci;
2577 struct dentry *parent, *dentry;
2578 struct ceph_dentry_info *di;
2579 int mds = session->s_mds;
2580 struct ceph_mds_lease *h = msg->front.iov_base;
2582 struct ceph_vino vino;
2587 dout("handle_lease from mds%d\n", mds);
2590 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2592 vino.ino = le64_to_cpu(h->ino);
2593 vino.snap = CEPH_NOSNAP;
2594 mask = le16_to_cpu(h->mask);
2595 seq = le32_to_cpu(h->seq);
2596 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2597 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2598 if (dname.len != get_unaligned_le32(h+1))
2601 mutex_lock(&session->s_mutex);
2605 inode = ceph_find_inode(sb, vino);
2606 dout("handle_lease %s, mask %d, ino %llx %p %.*s\n",
2607 ceph_lease_op_name(h->action), mask, vino.ino, inode,
2608 dname.len, dname.name);
2609 if (inode == NULL) {
2610 dout("handle_lease no inode %llx\n", vino.ino);
2613 ci = ceph_inode(inode);
2616 parent = d_find_alias(inode);
2618 dout("no parent dentry on inode %p\n", inode);
2620 goto release; /* hrm... */
2622 dname.hash = full_name_hash(dname.name, dname.len);
2623 dentry = d_lookup(parent, &dname);
2628 spin_lock(&dentry->d_lock);
2629 di = ceph_dentry(dentry);
2630 switch (h->action) {
2631 case CEPH_MDS_LEASE_REVOKE:
2632 if (di && di->lease_session == session) {
2633 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2634 h->seq = cpu_to_le32(di->lease_seq);
2635 __ceph_mdsc_drop_dentry_lease(dentry);
2640 case CEPH_MDS_LEASE_RENEW:
2641 if (di && di->lease_session == session &&
2642 di->lease_gen == session->s_cap_gen &&
2643 di->lease_renew_from &&
2644 di->lease_renew_after == 0) {
2645 unsigned long duration =
2646 le32_to_cpu(h->duration_ms) * HZ / 1000;
2648 di->lease_seq = seq;
2649 dentry->d_time = di->lease_renew_from + duration;
2650 di->lease_renew_after = di->lease_renew_from +
2652 di->lease_renew_from = 0;
2656 spin_unlock(&dentry->d_lock);
2663 /* let's just reuse the same message */
2664 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2666 ceph_con_send(&session->s_con, msg);
2670 mutex_unlock(&session->s_mutex);
2674 pr_err("corrupt lease message\n");
2678 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2679 struct inode *inode,
2680 struct dentry *dentry, char action,
2683 struct ceph_msg *msg;
2684 struct ceph_mds_lease *lease;
2685 int len = sizeof(*lease) + sizeof(u32);
2688 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2689 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2690 dnamelen = dentry->d_name.len;
2693 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS);
2696 lease = msg->front.iov_base;
2697 lease->action = action;
2698 lease->mask = cpu_to_le16(1);
2699 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2700 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2701 lease->seq = cpu_to_le32(seq);
2702 put_unaligned_le32(dnamelen, lease + 1);
2703 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2706 * if this is a preemptive lease RELEASE, no need to
2707 * flush request stream, since the actual request will
2710 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2712 ceph_con_send(&session->s_con, msg);
2716 * Preemptively release a lease we expect to invalidate anyway.
2717 * Pass @inode always, @dentry is optional.
2719 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2720 struct dentry *dentry, int mask)
2722 struct ceph_dentry_info *di;
2723 struct ceph_mds_session *session;
2726 BUG_ON(inode == NULL);
2727 BUG_ON(dentry == NULL);
2730 /* is dentry lease valid? */
2731 spin_lock(&dentry->d_lock);
2732 di = ceph_dentry(dentry);
2733 if (!di || !di->lease_session ||
2734 di->lease_session->s_mds < 0 ||
2735 di->lease_gen != di->lease_session->s_cap_gen ||
2736 !time_before(jiffies, dentry->d_time)) {
2737 dout("lease_release inode %p dentry %p -- "
2739 inode, dentry, mask);
2740 spin_unlock(&dentry->d_lock);
2744 /* we do have a lease on this dentry; note mds and seq */
2745 session = ceph_get_mds_session(di->lease_session);
2746 seq = di->lease_seq;
2747 __ceph_mdsc_drop_dentry_lease(dentry);
2748 spin_unlock(&dentry->d_lock);
2750 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2751 inode, dentry, mask, session->s_mds);
2752 ceph_mdsc_lease_send_msg(session, inode, dentry,
2753 CEPH_MDS_LEASE_RELEASE, seq);
2754 ceph_put_mds_session(session);
2758 * drop all leases (and dentry refs) in preparation for umount
2760 static void drop_leases(struct ceph_mds_client *mdsc)
2764 dout("drop_leases\n");
2765 mutex_lock(&mdsc->mutex);
2766 for (i = 0; i < mdsc->max_sessions; i++) {
2767 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2770 mutex_unlock(&mdsc->mutex);
2771 mutex_lock(&s->s_mutex);
2772 mutex_unlock(&s->s_mutex);
2773 ceph_put_mds_session(s);
2774 mutex_lock(&mdsc->mutex);
2776 mutex_unlock(&mdsc->mutex);
2782 * delayed work -- periodically trim expired leases, renew caps with mds
2784 static void schedule_delayed(struct ceph_mds_client *mdsc)
2787 unsigned hz = round_jiffies_relative(HZ * delay);
2788 schedule_delayed_work(&mdsc->delayed_work, hz);
2791 static void delayed_work(struct work_struct *work)
2794 struct ceph_mds_client *mdsc =
2795 container_of(work, struct ceph_mds_client, delayed_work.work);
2799 dout("mdsc delayed_work\n");
2800 ceph_check_delayed_caps(mdsc);
2802 mutex_lock(&mdsc->mutex);
2803 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2804 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2805 mdsc->last_renew_caps);
2807 mdsc->last_renew_caps = jiffies;
2809 for (i = 0; i < mdsc->max_sessions; i++) {
2810 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2813 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2814 dout("resending session close request for mds%d\n",
2816 request_close_session(mdsc, s);
2817 ceph_put_mds_session(s);
2820 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2821 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2822 s->s_state = CEPH_MDS_SESSION_HUNG;
2823 pr_info("mds%d hung\n", s->s_mds);
2826 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2827 /* this mds is failed or recovering, just wait */
2828 ceph_put_mds_session(s);
2831 mutex_unlock(&mdsc->mutex);
2833 mutex_lock(&s->s_mutex);
2835 send_renew_caps(mdsc, s);
2837 ceph_con_keepalive(&s->s_con);
2838 ceph_add_cap_releases(mdsc, s);
2839 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2840 s->s_state == CEPH_MDS_SESSION_HUNG)
2841 ceph_send_cap_releases(mdsc, s);
2842 mutex_unlock(&s->s_mutex);
2843 ceph_put_mds_session(s);
2845 mutex_lock(&mdsc->mutex);
2847 mutex_unlock(&mdsc->mutex);
2849 schedule_delayed(mdsc);
2853 int ceph_mdsc_init(struct ceph_mds_client *mdsc, struct ceph_client *client)
2855 mdsc->client = client;
2856 mutex_init(&mdsc->mutex);
2857 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
2858 if (mdsc->mdsmap == NULL)
2861 init_completion(&mdsc->safe_umount_waiters);
2862 init_completion(&mdsc->session_close_waiters);
2863 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2864 mdsc->sessions = NULL;
2865 mdsc->max_sessions = 0;
2867 init_rwsem(&mdsc->snap_rwsem);
2868 mdsc->snap_realms = RB_ROOT;
2869 INIT_LIST_HEAD(&mdsc->snap_empty);
2870 spin_lock_init(&mdsc->snap_empty_lock);
2872 mdsc->request_tree = RB_ROOT;
2873 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2874 mdsc->last_renew_caps = jiffies;
2875 INIT_LIST_HEAD(&mdsc->cap_delay_list);
2876 spin_lock_init(&mdsc->cap_delay_lock);
2877 INIT_LIST_HEAD(&mdsc->snap_flush_list);
2878 spin_lock_init(&mdsc->snap_flush_lock);
2879 mdsc->cap_flush_seq = 0;
2880 INIT_LIST_HEAD(&mdsc->cap_dirty);
2881 mdsc->num_cap_flushing = 0;
2882 spin_lock_init(&mdsc->cap_dirty_lock);
2883 init_waitqueue_head(&mdsc->cap_flushing_wq);
2884 spin_lock_init(&mdsc->dentry_lru_lock);
2885 INIT_LIST_HEAD(&mdsc->dentry_lru);
2887 ceph_caps_init(mdsc);
2888 ceph_adjust_min_caps(mdsc, client->min_caps);
2894 * Wait for safe replies on open mds requests. If we time out, drop
2895 * all requests from the tree to avoid dangling dentry refs.
2897 static void wait_requests(struct ceph_mds_client *mdsc)
2899 struct ceph_mds_request *req;
2900 struct ceph_client *client = mdsc->client;
2902 mutex_lock(&mdsc->mutex);
2903 if (__get_oldest_req(mdsc)) {
2904 mutex_unlock(&mdsc->mutex);
2906 dout("wait_requests waiting for requests\n");
2907 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
2908 client->mount_args->mount_timeout * HZ);
2910 /* tear down remaining requests */
2911 mutex_lock(&mdsc->mutex);
2912 while ((req = __get_oldest_req(mdsc))) {
2913 dout("wait_requests timed out on tid %llu\n",
2915 __unregister_request(mdsc, req);
2918 mutex_unlock(&mdsc->mutex);
2919 dout("wait_requests done\n");
2923 * called before mount is ro, and before dentries are torn down.
2924 * (hmm, does this still race with new lookups?)
2926 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
2928 dout("pre_umount\n");
2932 ceph_flush_dirty_caps(mdsc);
2933 wait_requests(mdsc);
2936 * wait for reply handlers to drop their request refs and
2937 * their inode/dcache refs
2943 * wait for all write mds requests to flush.
2945 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
2947 struct ceph_mds_request *req = NULL, *nextreq;
2950 mutex_lock(&mdsc->mutex);
2951 dout("wait_unsafe_requests want %lld\n", want_tid);
2953 req = __get_oldest_req(mdsc);
2954 while (req && req->r_tid <= want_tid) {
2955 /* find next request */
2956 n = rb_next(&req->r_node);
2958 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
2961 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
2963 ceph_mdsc_get_request(req);
2965 ceph_mdsc_get_request(nextreq);
2966 mutex_unlock(&mdsc->mutex);
2967 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
2968 req->r_tid, want_tid);
2969 wait_for_completion(&req->r_safe_completion);
2970 mutex_lock(&mdsc->mutex);
2971 ceph_mdsc_put_request(req);
2973 break; /* next dne before, so we're done! */
2974 if (RB_EMPTY_NODE(&nextreq->r_node)) {
2975 /* next request was removed from tree */
2976 ceph_mdsc_put_request(nextreq);
2979 ceph_mdsc_put_request(nextreq); /* won't go away */
2983 mutex_unlock(&mdsc->mutex);
2984 dout("wait_unsafe_requests done\n");
2987 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
2989 u64 want_tid, want_flush;
2991 if (mdsc->client->mount_state == CEPH_MOUNT_SHUTDOWN)
2995 mutex_lock(&mdsc->mutex);
2996 want_tid = mdsc->last_tid;
2997 want_flush = mdsc->cap_flush_seq;
2998 mutex_unlock(&mdsc->mutex);
2999 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3001 ceph_flush_dirty_caps(mdsc);
3003 wait_unsafe_requests(mdsc, want_tid);
3004 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3009 * called after sb is ro.
3011 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3013 struct ceph_mds_session *session;
3016 struct ceph_client *client = mdsc->client;
3017 unsigned long started, timeout = client->mount_args->mount_timeout * HZ;
3019 dout("close_sessions\n");
3021 mutex_lock(&mdsc->mutex);
3023 /* close sessions */
3025 while (time_before(jiffies, started + timeout)) {
3026 dout("closing sessions\n");
3028 for (i = 0; i < mdsc->max_sessions; i++) {
3029 session = __ceph_lookup_mds_session(mdsc, i);
3032 mutex_unlock(&mdsc->mutex);
3033 mutex_lock(&session->s_mutex);
3034 __close_session(mdsc, session);
3035 mutex_unlock(&session->s_mutex);
3036 ceph_put_mds_session(session);
3037 mutex_lock(&mdsc->mutex);
3043 if (client->mount_state == CEPH_MOUNT_SHUTDOWN)
3046 dout("waiting for sessions to close\n");
3047 mutex_unlock(&mdsc->mutex);
3048 wait_for_completion_timeout(&mdsc->session_close_waiters,
3050 mutex_lock(&mdsc->mutex);
3053 /* tear down remaining sessions */
3054 for (i = 0; i < mdsc->max_sessions; i++) {
3055 if (mdsc->sessions[i]) {
3056 session = get_session(mdsc->sessions[i]);
3057 __unregister_session(mdsc, session);
3058 mutex_unlock(&mdsc->mutex);
3059 mutex_lock(&session->s_mutex);
3060 remove_session_caps(session);
3061 mutex_unlock(&session->s_mutex);
3062 ceph_put_mds_session(session);
3063 mutex_lock(&mdsc->mutex);
3067 WARN_ON(!list_empty(&mdsc->cap_delay_list));
3069 mutex_unlock(&mdsc->mutex);
3071 ceph_cleanup_empty_realms(mdsc);
3073 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3078 void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
3081 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3083 ceph_mdsmap_destroy(mdsc->mdsmap);
3084 kfree(mdsc->sessions);
3085 ceph_caps_finalize(mdsc);
3090 * handle mds map update.
3092 void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3096 void *p = msg->front.iov_base;
3097 void *end = p + msg->front.iov_len;
3098 struct ceph_mdsmap *newmap, *oldmap;
3099 struct ceph_fsid fsid;
3102 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3103 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3104 if (ceph_check_fsid(mdsc->client, &fsid) < 0)
3106 epoch = ceph_decode_32(&p);
3107 maplen = ceph_decode_32(&p);
3108 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3110 /* do we need it? */
3111 ceph_monc_got_mdsmap(&mdsc->client->monc, epoch);
3112 mutex_lock(&mdsc->mutex);
3113 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3114 dout("handle_map epoch %u <= our %u\n",
3115 epoch, mdsc->mdsmap->m_epoch);
3116 mutex_unlock(&mdsc->mutex);
3120 newmap = ceph_mdsmap_decode(&p, end);
3121 if (IS_ERR(newmap)) {
3122 err = PTR_ERR(newmap);
3126 /* swap into place */
3128 oldmap = mdsc->mdsmap;
3129 mdsc->mdsmap = newmap;
3130 check_new_map(mdsc, newmap, oldmap);
3131 ceph_mdsmap_destroy(oldmap);
3133 mdsc->mdsmap = newmap; /* first mds map */
3135 mdsc->client->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
3137 __wake_requests(mdsc, &mdsc->waiting_for_map);
3139 mutex_unlock(&mdsc->mutex);
3140 schedule_delayed(mdsc);
3144 mutex_unlock(&mdsc->mutex);
3146 pr_err("error decoding mdsmap %d\n", err);
3150 static struct ceph_connection *con_get(struct ceph_connection *con)
3152 struct ceph_mds_session *s = con->private;
3154 if (get_session(s)) {
3155 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
3158 dout("mdsc con_get %p FAIL\n", s);
3162 static void con_put(struct ceph_connection *con)
3164 struct ceph_mds_session *s = con->private;
3166 ceph_put_mds_session(s);
3167 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
3171 * if the client is unresponsive for long enough, the mds will kill
3172 * the session entirely.
3174 static void peer_reset(struct ceph_connection *con)
3176 struct ceph_mds_session *s = con->private;
3177 struct ceph_mds_client *mdsc = s->s_mdsc;
3179 pr_warning("mds%d closed our session\n", s->s_mds);
3180 send_mds_reconnect(mdsc, s);
3183 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3185 struct ceph_mds_session *s = con->private;
3186 struct ceph_mds_client *mdsc = s->s_mdsc;
3187 int type = le16_to_cpu(msg->hdr.type);
3189 mutex_lock(&mdsc->mutex);
3190 if (__verify_registered_session(mdsc, s) < 0) {
3191 mutex_unlock(&mdsc->mutex);
3194 mutex_unlock(&mdsc->mutex);
3197 case CEPH_MSG_MDS_MAP:
3198 ceph_mdsc_handle_map(mdsc, msg);
3200 case CEPH_MSG_CLIENT_SESSION:
3201 handle_session(s, msg);
3203 case CEPH_MSG_CLIENT_REPLY:
3204 handle_reply(s, msg);
3206 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
3207 handle_forward(mdsc, s, msg);
3209 case CEPH_MSG_CLIENT_CAPS:
3210 ceph_handle_caps(s, msg);
3212 case CEPH_MSG_CLIENT_SNAP:
3213 ceph_handle_snap(mdsc, s, msg);
3215 case CEPH_MSG_CLIENT_LEASE:
3216 handle_lease(mdsc, s, msg);
3220 pr_err("received unknown message type %d %s\n", type,
3221 ceph_msg_type_name(type));
3230 static int get_authorizer(struct ceph_connection *con,
3231 void **buf, int *len, int *proto,
3232 void **reply_buf, int *reply_len, int force_new)
3234 struct ceph_mds_session *s = con->private;
3235 struct ceph_mds_client *mdsc = s->s_mdsc;
3236 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3239 if (force_new && s->s_authorizer) {
3240 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3241 s->s_authorizer = NULL;
3243 if (s->s_authorizer == NULL) {
3244 if (ac->ops->create_authorizer) {
3245 ret = ac->ops->create_authorizer(
3246 ac, CEPH_ENTITY_TYPE_MDS,
3248 &s->s_authorizer_buf,
3249 &s->s_authorizer_buf_len,
3250 &s->s_authorizer_reply_buf,
3251 &s->s_authorizer_reply_buf_len);
3257 *proto = ac->protocol;
3258 *buf = s->s_authorizer_buf;
3259 *len = s->s_authorizer_buf_len;
3260 *reply_buf = s->s_authorizer_reply_buf;
3261 *reply_len = s->s_authorizer_reply_buf_len;
3266 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3268 struct ceph_mds_session *s = con->private;
3269 struct ceph_mds_client *mdsc = s->s_mdsc;
3270 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3272 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3275 static int invalidate_authorizer(struct ceph_connection *con)
3277 struct ceph_mds_session *s = con->private;
3278 struct ceph_mds_client *mdsc = s->s_mdsc;
3279 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3281 if (ac->ops->invalidate_authorizer)
3282 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3284 return ceph_monc_validate_auth(&mdsc->client->monc);
3287 static const struct ceph_connection_operations mds_con_ops = {
3290 .dispatch = dispatch,
3291 .get_authorizer = get_authorizer,
3292 .verify_authorizer_reply = verify_authorizer_reply,
3293 .invalidate_authorizer = invalidate_authorizer,
3294 .peer_reset = peer_reset,