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 static void __wake_requests(struct ceph_mds_client *mdsc,
41 struct list_head *head);
43 const static struct ceph_connection_operations mds_con_ops;
51 * parse individual inode info
53 static int parse_reply_info_in(void **p, void *end,
54 struct ceph_mds_reply_info_in *info)
59 *p += sizeof(struct ceph_mds_reply_inode) +
60 sizeof(*info->in->fragtree.splits) *
61 le32_to_cpu(info->in->fragtree.nsplits);
63 ceph_decode_32_safe(p, end, info->symlink_len, bad);
64 ceph_decode_need(p, end, info->symlink_len, bad);
66 *p += info->symlink_len;
68 ceph_decode_32_safe(p, end, info->xattr_len, bad);
69 ceph_decode_need(p, end, info->xattr_len, bad);
70 info->xattr_data = *p;
71 *p += info->xattr_len;
78 * parse a normal reply, which may contain a (dir+)dentry and/or a
81 static int parse_reply_info_trace(void **p, void *end,
82 struct ceph_mds_reply_info_parsed *info)
86 if (info->head->is_dentry) {
87 err = parse_reply_info_in(p, end, &info->diri);
91 if (unlikely(*p + sizeof(*info->dirfrag) > end))
94 *p += sizeof(*info->dirfrag) +
95 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
96 if (unlikely(*p > end))
99 ceph_decode_32_safe(p, end, info->dname_len, bad);
100 ceph_decode_need(p, end, info->dname_len, bad);
102 *p += info->dname_len;
104 *p += sizeof(*info->dlease);
107 if (info->head->is_target) {
108 err = parse_reply_info_in(p, end, &info->targeti);
113 if (unlikely(*p != end))
120 pr_err("problem parsing mds trace %d\n", err);
125 * parse readdir results
127 static int parse_reply_info_dir(void **p, void *end,
128 struct ceph_mds_reply_info_parsed *info)
134 if (*p + sizeof(*info->dir_dir) > end)
136 *p += sizeof(*info->dir_dir) +
137 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
141 ceph_decode_need(p, end, sizeof(num) + 2, bad);
142 num = ceph_decode_32(p);
143 info->dir_end = ceph_decode_8(p);
144 info->dir_complete = ceph_decode_8(p);
148 /* alloc large array */
150 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
151 sizeof(*info->dir_dname) +
152 sizeof(*info->dir_dname_len) +
153 sizeof(*info->dir_dlease),
155 if (info->dir_in == NULL) {
159 info->dir_dname = (void *)(info->dir_in + num);
160 info->dir_dname_len = (void *)(info->dir_dname + num);
161 info->dir_dlease = (void *)(info->dir_dname_len + num);
165 ceph_decode_need(p, end, sizeof(u32)*2, bad);
166 info->dir_dname_len[i] = ceph_decode_32(p);
167 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
168 info->dir_dname[i] = *p;
169 *p += info->dir_dname_len[i];
170 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
172 info->dir_dlease[i] = *p;
173 *p += sizeof(struct ceph_mds_reply_lease);
176 err = parse_reply_info_in(p, end, &info->dir_in[i]);
191 pr_err("problem parsing dir contents %d\n", err);
196 * parse entire mds reply
198 static int parse_reply_info(struct ceph_msg *msg,
199 struct ceph_mds_reply_info_parsed *info)
205 info->head = msg->front.iov_base;
206 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
207 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
210 ceph_decode_32_safe(&p, end, len, bad);
212 err = parse_reply_info_trace(&p, p+len, info);
218 ceph_decode_32_safe(&p, end, len, bad);
220 err = parse_reply_info_dir(&p, p+len, info);
226 ceph_decode_32_safe(&p, end, len, bad);
227 info->snapblob_len = len;
238 pr_err("mds parse_reply err %d\n", err);
242 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
251 static const char *session_state_name(int s)
254 case CEPH_MDS_SESSION_NEW: return "new";
255 case CEPH_MDS_SESSION_OPENING: return "opening";
256 case CEPH_MDS_SESSION_OPEN: return "open";
257 case CEPH_MDS_SESSION_HUNG: return "hung";
258 case CEPH_MDS_SESSION_CLOSING: return "closing";
259 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
260 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
261 default: return "???";
265 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
267 if (atomic_inc_not_zero(&s->s_ref)) {
268 dout("mdsc get_session %p %d -> %d\n", s,
269 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
272 dout("mdsc get_session %p 0 -- FAIL", s);
277 void ceph_put_mds_session(struct ceph_mds_session *s)
279 dout("mdsc put_session %p %d -> %d\n", s,
280 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
281 if (atomic_dec_and_test(&s->s_ref)) {
283 s->s_mdsc->client->monc.auth->ops->destroy_authorizer(
284 s->s_mdsc->client->monc.auth, s->s_authorizer);
290 * called under mdsc->mutex
292 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
295 struct ceph_mds_session *session;
297 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
299 session = mdsc->sessions[mds];
300 dout("lookup_mds_session %p %d\n", session,
301 atomic_read(&session->s_ref));
302 get_session(session);
306 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
308 if (mds >= mdsc->max_sessions)
310 return mdsc->sessions[mds];
313 static int __verify_registered_session(struct ceph_mds_client *mdsc,
314 struct ceph_mds_session *s)
316 if (s->s_mds >= mdsc->max_sessions ||
317 mdsc->sessions[s->s_mds] != s)
323 * create+register a new session for given mds.
324 * called under mdsc->mutex.
326 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
329 struct ceph_mds_session *s;
331 s = kzalloc(sizeof(*s), GFP_NOFS);
333 return ERR_PTR(-ENOMEM);
336 s->s_state = CEPH_MDS_SESSION_NEW;
339 mutex_init(&s->s_mutex);
341 ceph_con_init(mdsc->client->msgr, &s->s_con);
342 s->s_con.private = s;
343 s->s_con.ops = &mds_con_ops;
344 s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
345 s->s_con.peer_name.num = cpu_to_le64(mds);
347 spin_lock_init(&s->s_cap_lock);
350 s->s_renew_requested = 0;
352 INIT_LIST_HEAD(&s->s_caps);
355 atomic_set(&s->s_ref, 1);
356 INIT_LIST_HEAD(&s->s_waiting);
357 INIT_LIST_HEAD(&s->s_unsafe);
358 s->s_num_cap_releases = 0;
359 s->s_cap_iterator = NULL;
360 INIT_LIST_HEAD(&s->s_cap_releases);
361 INIT_LIST_HEAD(&s->s_cap_releases_done);
362 INIT_LIST_HEAD(&s->s_cap_flushing);
363 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
365 dout("register_session mds%d\n", mds);
366 if (mds >= mdsc->max_sessions) {
367 int newmax = 1 << get_count_order(mds+1);
368 struct ceph_mds_session **sa;
370 dout("register_session realloc to %d\n", newmax);
371 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
374 if (mdsc->sessions) {
375 memcpy(sa, mdsc->sessions,
376 mdsc->max_sessions * sizeof(void *));
377 kfree(mdsc->sessions);
380 mdsc->max_sessions = newmax;
382 mdsc->sessions[mds] = s;
383 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
385 ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
391 return ERR_PTR(-ENOMEM);
395 * called under mdsc->mutex
397 static void __unregister_session(struct ceph_mds_client *mdsc,
398 struct ceph_mds_session *s)
400 dout("__unregister_session mds%d %p\n", s->s_mds, s);
401 BUG_ON(mdsc->sessions[s->s_mds] != s);
402 mdsc->sessions[s->s_mds] = NULL;
403 ceph_con_close(&s->s_con);
404 ceph_put_mds_session(s);
408 * drop session refs in request.
410 * should be last request ref, or hold mdsc->mutex
412 static void put_request_session(struct ceph_mds_request *req)
414 if (req->r_session) {
415 ceph_put_mds_session(req->r_session);
416 req->r_session = NULL;
420 void ceph_mdsc_release_request(struct kref *kref)
422 struct ceph_mds_request *req = container_of(kref,
423 struct ceph_mds_request,
426 ceph_msg_put(req->r_request);
428 ceph_msg_put(req->r_reply);
429 destroy_reply_info(&req->r_reply_info);
432 ceph_put_cap_refs(ceph_inode(req->r_inode),
436 if (req->r_locked_dir)
437 ceph_put_cap_refs(ceph_inode(req->r_locked_dir),
439 if (req->r_target_inode)
440 iput(req->r_target_inode);
443 if (req->r_old_dentry) {
445 ceph_inode(req->r_old_dentry->d_parent->d_inode),
447 dput(req->r_old_dentry);
451 put_request_session(req);
452 ceph_unreserve_caps(&req->r_caps_reservation);
457 * lookup session, bump ref if found.
459 * called under mdsc->mutex.
461 static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
464 struct ceph_mds_request *req;
465 struct rb_node *n = mdsc->request_tree.rb_node;
468 req = rb_entry(n, struct ceph_mds_request, r_node);
469 if (tid < req->r_tid)
471 else if (tid > req->r_tid)
474 ceph_mdsc_get_request(req);
481 static void __insert_request(struct ceph_mds_client *mdsc,
482 struct ceph_mds_request *new)
484 struct rb_node **p = &mdsc->request_tree.rb_node;
485 struct rb_node *parent = NULL;
486 struct ceph_mds_request *req = NULL;
490 req = rb_entry(parent, struct ceph_mds_request, r_node);
491 if (new->r_tid < req->r_tid)
493 else if (new->r_tid > req->r_tid)
499 rb_link_node(&new->r_node, parent, p);
500 rb_insert_color(&new->r_node, &mdsc->request_tree);
504 * Register an in-flight request, and assign a tid. Link to directory
505 * are modifying (if any).
507 * Called under mdsc->mutex.
509 static void __register_request(struct ceph_mds_client *mdsc,
510 struct ceph_mds_request *req,
513 req->r_tid = ++mdsc->last_tid;
515 ceph_reserve_caps(&req->r_caps_reservation, req->r_num_caps);
516 dout("__register_request %p tid %lld\n", req, req->r_tid);
517 ceph_mdsc_get_request(req);
518 __insert_request(mdsc, req);
521 struct ceph_inode_info *ci = ceph_inode(dir);
523 spin_lock(&ci->i_unsafe_lock);
524 req->r_unsafe_dir = dir;
525 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
526 spin_unlock(&ci->i_unsafe_lock);
530 static void __unregister_request(struct ceph_mds_client *mdsc,
531 struct ceph_mds_request *req)
533 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
534 rb_erase(&req->r_node, &mdsc->request_tree);
535 RB_CLEAR_NODE(&req->r_node);
537 if (req->r_unsafe_dir) {
538 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
540 spin_lock(&ci->i_unsafe_lock);
541 list_del_init(&req->r_unsafe_dir_item);
542 spin_unlock(&ci->i_unsafe_lock);
545 ceph_mdsc_put_request(req);
549 * Choose mds to send request to next. If there is a hint set in the
550 * request (e.g., due to a prior forward hint from the mds), use that.
551 * Otherwise, consult frag tree and/or caps to identify the
552 * appropriate mds. If all else fails, choose randomly.
554 * Called under mdsc->mutex.
556 static int __choose_mds(struct ceph_mds_client *mdsc,
557 struct ceph_mds_request *req)
560 struct ceph_inode_info *ci;
561 struct ceph_cap *cap;
562 int mode = req->r_direct_mode;
564 u32 hash = req->r_direct_hash;
565 bool is_hash = req->r_direct_is_hash;
568 * is there a specific mds we should try? ignore hint if we have
569 * no session and the mds is not up (active or recovering).
571 if (req->r_resend_mds >= 0 &&
572 (__have_session(mdsc, req->r_resend_mds) ||
573 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
574 dout("choose_mds using resend_mds mds%d\n",
576 return req->r_resend_mds;
579 if (mode == USE_RANDOM_MDS)
584 inode = req->r_inode;
585 } else if (req->r_dentry) {
586 if (req->r_dentry->d_inode) {
587 inode = req->r_dentry->d_inode;
589 inode = req->r_dentry->d_parent->d_inode;
590 hash = req->r_dentry->d_name.hash;
594 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
598 ci = ceph_inode(inode);
600 if (is_hash && S_ISDIR(inode->i_mode)) {
601 struct ceph_inode_frag frag;
604 ceph_choose_frag(ci, hash, &frag, &found);
606 if (mode == USE_ANY_MDS && frag.ndist > 0) {
609 /* choose a random replica */
610 get_random_bytes(&r, 1);
613 dout("choose_mds %p %llx.%llx "
614 "frag %u mds%d (%d/%d)\n",
615 inode, ceph_vinop(inode),
621 /* since this file/dir wasn't known to be
622 * replicated, then we want to look for the
623 * authoritative mds. */
626 /* choose auth mds */
628 dout("choose_mds %p %llx.%llx "
629 "frag %u mds%d (auth)\n",
630 inode, ceph_vinop(inode), frag.frag, mds);
636 spin_lock(&inode->i_lock);
638 if (mode == USE_AUTH_MDS)
639 cap = ci->i_auth_cap;
640 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
641 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
643 spin_unlock(&inode->i_lock);
646 mds = cap->session->s_mds;
647 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
648 inode, ceph_vinop(inode), mds,
649 cap == ci->i_auth_cap ? "auth " : "", cap);
650 spin_unlock(&inode->i_lock);
654 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
655 dout("choose_mds chose random mds%d\n", mds);
663 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
665 struct ceph_msg *msg;
666 struct ceph_mds_session_head *h;
668 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h));
670 pr_err("create_session_msg ENOMEM creating msg\n");
673 h = msg->front.iov_base;
674 h->op = cpu_to_le32(op);
675 h->seq = cpu_to_le64(seq);
680 * send session open request.
682 * called under mdsc->mutex
684 static int __open_session(struct ceph_mds_client *mdsc,
685 struct ceph_mds_session *session)
687 struct ceph_msg *msg;
689 int mds = session->s_mds;
691 /* wait for mds to go active? */
692 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
693 dout("open_session to mds%d (%s)\n", mds,
694 ceph_mds_state_name(mstate));
695 session->s_state = CEPH_MDS_SESSION_OPENING;
696 session->s_renew_requested = jiffies;
698 /* send connect message */
699 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
702 ceph_con_send(&session->s_con, msg);
711 * Free preallocated cap messages assigned to this session
713 static void cleanup_cap_releases(struct ceph_mds_session *session)
715 struct ceph_msg *msg;
717 spin_lock(&session->s_cap_lock);
718 while (!list_empty(&session->s_cap_releases)) {
719 msg = list_first_entry(&session->s_cap_releases,
720 struct ceph_msg, list_head);
721 list_del_init(&msg->list_head);
724 while (!list_empty(&session->s_cap_releases_done)) {
725 msg = list_first_entry(&session->s_cap_releases_done,
726 struct ceph_msg, list_head);
727 list_del_init(&msg->list_head);
730 spin_unlock(&session->s_cap_lock);
734 * Helper to safely iterate over all caps associated with a session, with
735 * special care taken to handle a racing __ceph_remove_cap().
737 * Caller must hold session s_mutex.
739 static int iterate_session_caps(struct ceph_mds_session *session,
740 int (*cb)(struct inode *, struct ceph_cap *,
744 struct ceph_cap *cap;
745 struct inode *inode, *last_inode = NULL;
746 struct ceph_cap *old_cap = NULL;
749 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
750 spin_lock(&session->s_cap_lock);
751 p = session->s_caps.next;
752 while (p != &session->s_caps) {
753 cap = list_entry(p, struct ceph_cap, session_caps);
754 inode = igrab(&cap->ci->vfs_inode);
759 session->s_cap_iterator = cap;
760 spin_unlock(&session->s_cap_lock);
767 ceph_put_cap(old_cap);
771 ret = cb(inode, cap, arg);
774 spin_lock(&session->s_cap_lock);
776 if (cap->ci == NULL) {
777 dout("iterate_session_caps finishing cap %p removal\n",
779 BUG_ON(cap->session != session);
780 list_del_init(&cap->session_caps);
781 session->s_nr_caps--;
783 old_cap = cap; /* put_cap it w/o locks held */
790 session->s_cap_iterator = NULL;
791 spin_unlock(&session->s_cap_lock);
796 ceph_put_cap(old_cap);
801 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
804 struct ceph_inode_info *ci = ceph_inode(inode);
805 dout("removing cap %p, ci is %p, inode is %p\n",
806 cap, ci, &ci->vfs_inode);
807 ceph_remove_cap(cap);
812 * caller must hold session s_mutex
814 static void remove_session_caps(struct ceph_mds_session *session)
816 dout("remove_session_caps on %p\n", session);
817 iterate_session_caps(session, remove_session_caps_cb, NULL);
818 BUG_ON(session->s_nr_caps > 0);
819 cleanup_cap_releases(session);
823 * wake up any threads waiting on this session's caps. if the cap is
824 * old (didn't get renewed on the client reconnect), remove it now.
826 * caller must hold s_mutex.
828 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
831 struct ceph_inode_info *ci = ceph_inode(inode);
833 wake_up(&ci->i_cap_wq);
835 spin_lock(&inode->i_lock);
836 ci->i_wanted_max_size = 0;
837 ci->i_requested_max_size = 0;
838 spin_unlock(&inode->i_lock);
843 static void wake_up_session_caps(struct ceph_mds_session *session,
846 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
847 iterate_session_caps(session, wake_up_session_cb,
848 (void *)(unsigned long)reconnect);
852 * Send periodic message to MDS renewing all currently held caps. The
853 * ack will reset the expiration for all caps from this session.
855 * caller holds s_mutex
857 static int send_renew_caps(struct ceph_mds_client *mdsc,
858 struct ceph_mds_session *session)
860 struct ceph_msg *msg;
863 if (time_after_eq(jiffies, session->s_cap_ttl) &&
864 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
865 pr_info("mds%d caps stale\n", session->s_mds);
866 session->s_renew_requested = jiffies;
868 /* do not try to renew caps until a recovering mds has reconnected
869 * with its clients. */
870 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
871 if (state < CEPH_MDS_STATE_RECONNECT) {
872 dout("send_renew_caps ignoring mds%d (%s)\n",
873 session->s_mds, ceph_mds_state_name(state));
877 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
878 ceph_mds_state_name(state));
879 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
880 ++session->s_renew_seq);
883 ceph_con_send(&session->s_con, msg);
888 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
890 * Called under session->s_mutex
892 static void renewed_caps(struct ceph_mds_client *mdsc,
893 struct ceph_mds_session *session, int is_renew)
898 spin_lock(&session->s_cap_lock);
899 was_stale = is_renew && (session->s_cap_ttl == 0 ||
900 time_after_eq(jiffies, session->s_cap_ttl));
902 session->s_cap_ttl = session->s_renew_requested +
903 mdsc->mdsmap->m_session_timeout*HZ;
906 if (time_before(jiffies, session->s_cap_ttl)) {
907 pr_info("mds%d caps renewed\n", session->s_mds);
910 pr_info("mds%d caps still stale\n", session->s_mds);
913 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
914 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
915 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
916 spin_unlock(&session->s_cap_lock);
919 wake_up_session_caps(session, 0);
923 * send a session close request
925 static int request_close_session(struct ceph_mds_client *mdsc,
926 struct ceph_mds_session *session)
928 struct ceph_msg *msg;
930 dout("request_close_session mds%d state %s seq %lld\n",
931 session->s_mds, session_state_name(session->s_state),
933 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
936 ceph_con_send(&session->s_con, msg);
941 * Called with s_mutex held.
943 static int __close_session(struct ceph_mds_client *mdsc,
944 struct ceph_mds_session *session)
946 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
948 session->s_state = CEPH_MDS_SESSION_CLOSING;
949 return request_close_session(mdsc, session);
955 * Because we can't cache an inode without one or more caps, we do
956 * this indirectly: if a cap is unused, we prune its aliases, at which
957 * point the inode will hopefully get dropped to.
959 * Yes, this is a bit sloppy. Our only real goal here is to respond to
960 * memory pressure from the MDS, though, so it needn't be perfect.
962 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
964 struct ceph_mds_session *session = arg;
965 struct ceph_inode_info *ci = ceph_inode(inode);
966 int used, oissued, mine;
968 if (session->s_trim_caps <= 0)
971 spin_lock(&inode->i_lock);
972 mine = cap->issued | cap->implemented;
973 used = __ceph_caps_used(ci);
974 oissued = __ceph_caps_issued_other(ci, cap);
976 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
977 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
978 ceph_cap_string(used));
979 if (ci->i_dirty_caps)
980 goto out; /* dirty caps */
981 if ((used & ~oissued) & mine)
982 goto out; /* we need these caps */
984 session->s_trim_caps--;
986 /* we aren't the only cap.. just remove us */
987 __ceph_remove_cap(cap);
989 /* try to drop referring dentries */
990 spin_unlock(&inode->i_lock);
991 d_prune_aliases(inode);
992 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
993 inode, cap, atomic_read(&inode->i_count));
998 spin_unlock(&inode->i_lock);
1003 * Trim session cap count down to some max number.
1005 static int trim_caps(struct ceph_mds_client *mdsc,
1006 struct ceph_mds_session *session,
1009 int trim_caps = session->s_nr_caps - max_caps;
1011 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1012 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1013 if (trim_caps > 0) {
1014 session->s_trim_caps = trim_caps;
1015 iterate_session_caps(session, trim_caps_cb, session);
1016 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1017 session->s_mds, session->s_nr_caps, max_caps,
1018 trim_caps - session->s_trim_caps);
1019 session->s_trim_caps = 0;
1025 * Allocate cap_release messages. If there is a partially full message
1026 * in the queue, try to allocate enough to cover it's remainder, so that
1027 * we can send it immediately.
1029 * Called under s_mutex.
1031 static int add_cap_releases(struct ceph_mds_client *mdsc,
1032 struct ceph_mds_session *session,
1035 struct ceph_msg *msg;
1036 struct ceph_mds_cap_release *head;
1040 extra = mdsc->client->mount_args->cap_release_safety;
1042 spin_lock(&session->s_cap_lock);
1044 if (!list_empty(&session->s_cap_releases)) {
1045 msg = list_first_entry(&session->s_cap_releases,
1048 head = msg->front.iov_base;
1049 extra += CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num);
1052 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1053 spin_unlock(&session->s_cap_lock);
1054 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE);
1057 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1058 (int)msg->front.iov_len);
1059 head = msg->front.iov_base;
1060 head->num = cpu_to_le32(0);
1061 msg->front.iov_len = sizeof(*head);
1062 spin_lock(&session->s_cap_lock);
1063 list_add(&msg->list_head, &session->s_cap_releases);
1064 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1067 if (!list_empty(&session->s_cap_releases)) {
1068 msg = list_first_entry(&session->s_cap_releases,
1071 head = msg->front.iov_base;
1073 dout(" queueing non-full %p (%d)\n", msg,
1074 le32_to_cpu(head->num));
1075 list_move_tail(&msg->list_head,
1076 &session->s_cap_releases_done);
1077 session->s_num_cap_releases -=
1078 CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num);
1082 spin_unlock(&session->s_cap_lock);
1088 * flush all dirty inode data to disk.
1090 * returns true if we've flushed through want_flush_seq
1092 static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1096 dout("check_cap_flush want %lld\n", want_flush_seq);
1097 mutex_lock(&mdsc->mutex);
1098 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1099 struct ceph_mds_session *session = mdsc->sessions[mds];
1103 get_session(session);
1104 mutex_unlock(&mdsc->mutex);
1106 mutex_lock(&session->s_mutex);
1107 if (!list_empty(&session->s_cap_flushing)) {
1108 struct ceph_inode_info *ci =
1109 list_entry(session->s_cap_flushing.next,
1110 struct ceph_inode_info,
1112 struct inode *inode = &ci->vfs_inode;
1114 spin_lock(&inode->i_lock);
1115 if (ci->i_cap_flush_seq <= want_flush_seq) {
1116 dout("check_cap_flush still flushing %p "
1117 "seq %lld <= %lld to mds%d\n", inode,
1118 ci->i_cap_flush_seq, want_flush_seq,
1122 spin_unlock(&inode->i_lock);
1124 mutex_unlock(&session->s_mutex);
1125 ceph_put_mds_session(session);
1129 mutex_lock(&mdsc->mutex);
1132 mutex_unlock(&mdsc->mutex);
1133 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1138 * called under s_mutex
1140 static void send_cap_releases(struct ceph_mds_client *mdsc,
1141 struct ceph_mds_session *session)
1143 struct ceph_msg *msg;
1145 dout("send_cap_releases mds%d\n", session->s_mds);
1146 spin_lock(&session->s_cap_lock);
1147 while (!list_empty(&session->s_cap_releases_done)) {
1148 msg = list_first_entry(&session->s_cap_releases_done,
1149 struct ceph_msg, list_head);
1150 list_del_init(&msg->list_head);
1151 spin_unlock(&session->s_cap_lock);
1152 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1153 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1154 ceph_con_send(&session->s_con, msg);
1155 spin_lock(&session->s_cap_lock);
1157 spin_unlock(&session->s_cap_lock);
1160 static void discard_cap_releases(struct ceph_mds_client *mdsc,
1161 struct ceph_mds_session *session)
1163 struct ceph_msg *msg;
1164 struct ceph_mds_cap_release *head;
1167 dout("discard_cap_releases mds%d\n", session->s_mds);
1168 spin_lock(&session->s_cap_lock);
1170 /* zero out the in-progress message */
1171 msg = list_first_entry(&session->s_cap_releases,
1172 struct ceph_msg, list_head);
1173 head = msg->front.iov_base;
1174 num = le32_to_cpu(head->num);
1175 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1176 head->num = cpu_to_le32(0);
1177 session->s_num_cap_releases += num;
1179 /* requeue completed messages */
1180 while (!list_empty(&session->s_cap_releases_done)) {
1181 msg = list_first_entry(&session->s_cap_releases_done,
1182 struct ceph_msg, list_head);
1183 list_del_init(&msg->list_head);
1185 head = msg->front.iov_base;
1186 num = le32_to_cpu(head->num);
1187 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1189 session->s_num_cap_releases += num;
1190 head->num = cpu_to_le32(0);
1191 msg->front.iov_len = sizeof(*head);
1192 list_add(&msg->list_head, &session->s_cap_releases);
1195 spin_unlock(&session->s_cap_lock);
1203 * Create an mds request.
1205 struct ceph_mds_request *
1206 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1208 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1211 return ERR_PTR(-ENOMEM);
1213 mutex_init(&req->r_fill_mutex);
1214 req->r_started = jiffies;
1215 req->r_resend_mds = -1;
1216 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1218 kref_init(&req->r_kref);
1219 INIT_LIST_HEAD(&req->r_wait);
1220 init_completion(&req->r_completion);
1221 init_completion(&req->r_safe_completion);
1222 INIT_LIST_HEAD(&req->r_unsafe_item);
1225 req->r_direct_mode = mode;
1230 * return oldest (lowest) request, tid in request tree, 0 if none.
1232 * called under mdsc->mutex.
1234 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1236 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1238 return rb_entry(rb_first(&mdsc->request_tree),
1239 struct ceph_mds_request, r_node);
1242 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1244 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1252 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1253 * on build_path_from_dentry in fs/cifs/dir.c.
1255 * If @stop_on_nosnap, generate path relative to the first non-snapped
1258 * Encode hidden .snap dirs as a double /, i.e.
1259 * foo/.snap/bar -> foo//bar
1261 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1264 struct dentry *temp;
1269 return ERR_PTR(-EINVAL);
1273 for (temp = dentry; !IS_ROOT(temp);) {
1274 struct inode *inode = temp->d_inode;
1275 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1276 len++; /* slash only */
1277 else if (stop_on_nosnap && inode &&
1278 ceph_snap(inode) == CEPH_NOSNAP)
1281 len += 1 + temp->d_name.len;
1282 temp = temp->d_parent;
1284 pr_err("build_path_dentry corrupt dentry %p\n", dentry);
1285 return ERR_PTR(-EINVAL);
1289 len--; /* no leading '/' */
1291 path = kmalloc(len+1, GFP_NOFS);
1293 return ERR_PTR(-ENOMEM);
1295 path[pos] = 0; /* trailing null */
1296 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1297 struct inode *inode = temp->d_inode;
1299 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1300 dout("build_path path+%d: %p SNAPDIR\n",
1302 } else if (stop_on_nosnap && inode &&
1303 ceph_snap(inode) == CEPH_NOSNAP) {
1306 pos -= temp->d_name.len;
1309 strncpy(path + pos, temp->d_name.name,
1314 temp = temp->d_parent;
1316 pr_err("build_path corrupt dentry\n");
1318 return ERR_PTR(-EINVAL);
1322 pr_err("build_path did not end path lookup where "
1323 "expected, namelen is %d, pos is %d\n", len, pos);
1324 /* presumably this is only possible if racing with a
1325 rename of one of the parent directories (we can not
1326 lock the dentries above us to prevent this, but
1327 retrying should be harmless) */
1332 *base = ceph_ino(temp->d_inode);
1334 dout("build_path on %p %d built %llx '%.*s'\n",
1335 dentry, atomic_read(&dentry->d_count), *base, len, path);
1339 static int build_dentry_path(struct dentry *dentry,
1340 const char **ppath, int *ppathlen, u64 *pino,
1345 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1346 *pino = ceph_ino(dentry->d_parent->d_inode);
1347 *ppath = dentry->d_name.name;
1348 *ppathlen = dentry->d_name.len;
1351 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1353 return PTR_ERR(path);
1359 static int build_inode_path(struct inode *inode,
1360 const char **ppath, int *ppathlen, u64 *pino,
1363 struct dentry *dentry;
1366 if (ceph_snap(inode) == CEPH_NOSNAP) {
1367 *pino = ceph_ino(inode);
1371 dentry = d_find_alias(inode);
1372 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1375 return PTR_ERR(path);
1382 * request arguments may be specified via an inode *, a dentry *, or
1383 * an explicit ino+path.
1385 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1386 const char *rpath, u64 rino,
1387 const char **ppath, int *pathlen,
1388 u64 *ino, int *freepath)
1393 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1394 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1396 } else if (rdentry) {
1397 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1398 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1403 *pathlen = strlen(rpath);
1404 dout(" path %.*s\n", *pathlen, rpath);
1411 * called under mdsc->mutex
1413 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1414 struct ceph_mds_request *req,
1417 struct ceph_msg *msg;
1418 struct ceph_mds_request_head *head;
1419 const char *path1 = NULL;
1420 const char *path2 = NULL;
1421 u64 ino1 = 0, ino2 = 0;
1422 int pathlen1 = 0, pathlen2 = 0;
1423 int freepath1 = 0, freepath2 = 0;
1429 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1430 req->r_path1, req->r_ino1.ino,
1431 &path1, &pathlen1, &ino1, &freepath1);
1437 ret = set_request_path_attr(NULL, req->r_old_dentry,
1438 req->r_path2, req->r_ino2.ino,
1439 &path2, &pathlen2, &ino2, &freepath2);
1445 len = sizeof(*head) +
1446 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
1448 /* calculate (max) length for cap releases */
1449 len += sizeof(struct ceph_mds_request_release) *
1450 (!!req->r_inode_drop + !!req->r_dentry_drop +
1451 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1452 if (req->r_dentry_drop)
1453 len += req->r_dentry->d_name.len;
1454 if (req->r_old_dentry_drop)
1455 len += req->r_old_dentry->d_name.len;
1457 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len);
1459 msg = ERR_PTR(-ENOMEM);
1463 msg->hdr.tid = cpu_to_le64(req->r_tid);
1465 head = msg->front.iov_base;
1466 p = msg->front.iov_base + sizeof(*head);
1467 end = msg->front.iov_base + msg->front.iov_len;
1469 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1470 head->op = cpu_to_le32(req->r_op);
1471 head->caller_uid = cpu_to_le32(current_fsuid());
1472 head->caller_gid = cpu_to_le32(current_fsgid());
1473 head->args = req->r_args;
1475 ceph_encode_filepath(&p, end, ino1, path1);
1476 ceph_encode_filepath(&p, end, ino2, path2);
1480 if (req->r_inode_drop)
1481 releases += ceph_encode_inode_release(&p,
1482 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1483 mds, req->r_inode_drop, req->r_inode_unless, 0);
1484 if (req->r_dentry_drop)
1485 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1486 mds, req->r_dentry_drop, req->r_dentry_unless);
1487 if (req->r_old_dentry_drop)
1488 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1489 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1490 if (req->r_old_inode_drop)
1491 releases += ceph_encode_inode_release(&p,
1492 req->r_old_dentry->d_inode,
1493 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1494 head->num_releases = cpu_to_le16(releases);
1497 msg->front.iov_len = p - msg->front.iov_base;
1498 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1500 msg->pages = req->r_pages;
1501 msg->nr_pages = req->r_num_pages;
1502 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1503 msg->hdr.data_off = cpu_to_le16(0);
1507 kfree((char *)path2);
1510 kfree((char *)path1);
1516 * called under mdsc->mutex if error, under no mutex if
1519 static void complete_request(struct ceph_mds_client *mdsc,
1520 struct ceph_mds_request *req)
1522 if (req->r_callback)
1523 req->r_callback(mdsc, req);
1525 complete(&req->r_completion);
1529 * called under mdsc->mutex
1531 static int __prepare_send_request(struct ceph_mds_client *mdsc,
1532 struct ceph_mds_request *req,
1535 struct ceph_mds_request_head *rhead;
1536 struct ceph_msg *msg;
1541 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1542 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1544 if (req->r_request) {
1545 ceph_msg_put(req->r_request);
1546 req->r_request = NULL;
1548 msg = create_request_message(mdsc, req, mds);
1550 req->r_err = PTR_ERR(msg);
1551 complete_request(mdsc, req);
1552 return PTR_ERR(msg);
1554 req->r_request = msg;
1556 rhead = msg->front.iov_base;
1557 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1558 if (req->r_got_unsafe)
1559 flags |= CEPH_MDS_FLAG_REPLAY;
1560 if (req->r_locked_dir)
1561 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1562 rhead->flags = cpu_to_le32(flags);
1563 rhead->num_fwd = req->r_num_fwd;
1564 rhead->num_retry = req->r_attempts - 1;
1566 dout(" r_locked_dir = %p\n", req->r_locked_dir);
1568 if (req->r_target_inode && req->r_got_unsafe)
1569 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1576 * send request, or put it on the appropriate wait list.
1578 static int __do_request(struct ceph_mds_client *mdsc,
1579 struct ceph_mds_request *req)
1581 struct ceph_mds_session *session = NULL;
1585 if (req->r_err || req->r_got_result)
1588 if (req->r_timeout &&
1589 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1590 dout("do_request timed out\n");
1595 mds = __choose_mds(mdsc, req);
1597 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1598 dout("do_request no mds or not active, waiting for map\n");
1599 list_add(&req->r_wait, &mdsc->waiting_for_map);
1603 /* get, open session */
1604 session = __ceph_lookup_mds_session(mdsc, mds);
1606 session = register_session(mdsc, mds);
1607 if (IS_ERR(session)) {
1608 err = PTR_ERR(session);
1612 dout("do_request mds%d session %p state %s\n", mds, session,
1613 session_state_name(session->s_state));
1614 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1615 session->s_state != CEPH_MDS_SESSION_HUNG) {
1616 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1617 session->s_state == CEPH_MDS_SESSION_CLOSING)
1618 __open_session(mdsc, session);
1619 list_add(&req->r_wait, &session->s_waiting);
1624 req->r_session = get_session(session);
1625 req->r_resend_mds = -1; /* forget any previous mds hint */
1627 if (req->r_request_started == 0) /* note request start time */
1628 req->r_request_started = jiffies;
1630 err = __prepare_send_request(mdsc, req, mds);
1632 ceph_msg_get(req->r_request);
1633 ceph_con_send(&session->s_con, req->r_request);
1637 ceph_put_mds_session(session);
1643 complete_request(mdsc, req);
1648 * called under mdsc->mutex
1650 static void __wake_requests(struct ceph_mds_client *mdsc,
1651 struct list_head *head)
1653 struct ceph_mds_request *req, *nreq;
1655 list_for_each_entry_safe(req, nreq, head, r_wait) {
1656 list_del_init(&req->r_wait);
1657 __do_request(mdsc, req);
1662 * Wake up threads with requests pending for @mds, so that they can
1663 * resubmit their requests to a possibly different mds.
1665 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
1667 struct ceph_mds_request *req;
1670 dout("kick_requests mds%d\n", mds);
1671 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1672 req = rb_entry(p, struct ceph_mds_request, r_node);
1673 if (req->r_got_unsafe)
1675 if (req->r_session &&
1676 req->r_session->s_mds == mds) {
1677 dout(" kicking tid %llu\n", req->r_tid);
1678 put_request_session(req);
1679 __do_request(mdsc, req);
1684 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1685 struct ceph_mds_request *req)
1687 dout("submit_request on %p\n", req);
1688 mutex_lock(&mdsc->mutex);
1689 __register_request(mdsc, req, NULL);
1690 __do_request(mdsc, req);
1691 mutex_unlock(&mdsc->mutex);
1695 * Synchrously perform an mds request. Take care of all of the
1696 * session setup, forwarding, retry details.
1698 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1700 struct ceph_mds_request *req)
1704 dout("do_request on %p\n", req);
1706 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1708 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1709 if (req->r_locked_dir)
1710 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1711 if (req->r_old_dentry)
1713 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1717 mutex_lock(&mdsc->mutex);
1718 __register_request(mdsc, req, dir);
1719 __do_request(mdsc, req);
1723 __unregister_request(mdsc, req);
1724 dout("do_request early error %d\n", err);
1729 mutex_unlock(&mdsc->mutex);
1730 dout("do_request waiting\n");
1731 if (req->r_timeout) {
1732 err = (long)wait_for_completion_interruptible_timeout(
1733 &req->r_completion, req->r_timeout);
1737 err = wait_for_completion_interruptible(&req->r_completion);
1739 dout("do_request waited, got %d\n", err);
1740 mutex_lock(&mdsc->mutex);
1742 /* only abort if we didn't race with a real reply */
1743 if (req->r_got_result) {
1744 err = le32_to_cpu(req->r_reply_info.head->result);
1745 } else if (err < 0) {
1746 dout("aborted request %lld with %d\n", req->r_tid, err);
1749 * ensure we aren't running concurrently with
1750 * ceph_fill_trace or ceph_readdir_prepopulate, which
1751 * rely on locks (dir mutex) held by our caller.
1753 mutex_lock(&req->r_fill_mutex);
1755 req->r_aborted = true;
1756 mutex_unlock(&req->r_fill_mutex);
1758 if (req->r_locked_dir &&
1759 (req->r_op & CEPH_MDS_OP_WRITE)) {
1760 struct ceph_inode_info *ci =
1761 ceph_inode(req->r_locked_dir);
1763 dout("aborted, clearing I_COMPLETE on %p, leases\n",
1765 spin_lock(&req->r_locked_dir->i_lock);
1766 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1767 ci->i_release_count++;
1768 spin_unlock(&req->r_locked_dir->i_lock);
1771 ceph_invalidate_dentry_lease(req->r_dentry);
1772 if (req->r_old_dentry)
1773 ceph_invalidate_dentry_lease(req->r_old_dentry);
1780 mutex_unlock(&mdsc->mutex);
1781 dout("do_request %p done, result %d\n", req, err);
1788 * We take the session mutex and parse and process the reply immediately.
1789 * This preserves the logical ordering of replies, capabilities, etc., sent
1790 * by the MDS as they are applied to our local cache.
1792 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
1794 struct ceph_mds_client *mdsc = session->s_mdsc;
1795 struct ceph_mds_request *req;
1796 struct ceph_mds_reply_head *head = msg->front.iov_base;
1797 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
1800 int mds = session->s_mds;
1802 if (msg->front.iov_len < sizeof(*head)) {
1803 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
1808 /* get request, session */
1809 tid = le64_to_cpu(msg->hdr.tid);
1810 mutex_lock(&mdsc->mutex);
1811 req = __lookup_request(mdsc, tid);
1813 dout("handle_reply on unknown tid %llu\n", tid);
1814 mutex_unlock(&mdsc->mutex);
1817 dout("handle_reply %p\n", req);
1819 /* correct session? */
1820 if (req->r_session != session) {
1821 pr_err("mdsc_handle_reply got %llu on session mds%d"
1822 " not mds%d\n", tid, session->s_mds,
1823 req->r_session ? req->r_session->s_mds : -1);
1824 mutex_unlock(&mdsc->mutex);
1829 if ((req->r_got_unsafe && !head->safe) ||
1830 (req->r_got_safe && head->safe)) {
1831 pr_warning("got a dup %s reply on %llu from mds%d\n",
1832 head->safe ? "safe" : "unsafe", tid, mds);
1833 mutex_unlock(&mdsc->mutex);
1837 result = le32_to_cpu(head->result);
1840 * Tolerate 2 consecutive ESTALEs from the same mds.
1841 * FIXME: we should be looking at the cap migrate_seq.
1843 if (result == -ESTALE) {
1844 req->r_direct_mode = USE_AUTH_MDS;
1846 if (req->r_num_stale <= 2) {
1847 __do_request(mdsc, req);
1848 mutex_unlock(&mdsc->mutex);
1852 req->r_num_stale = 0;
1856 req->r_got_safe = true;
1857 __unregister_request(mdsc, req);
1858 complete(&req->r_safe_completion);
1860 if (req->r_got_unsafe) {
1862 * We already handled the unsafe response, now do the
1863 * cleanup. No need to examine the response; the MDS
1864 * doesn't include any result info in the safe
1865 * response. And even if it did, there is nothing
1866 * useful we could do with a revised return value.
1868 dout("got safe reply %llu, mds%d\n", tid, mds);
1869 list_del_init(&req->r_unsafe_item);
1871 /* last unsafe request during umount? */
1872 if (mdsc->stopping && !__get_oldest_req(mdsc))
1873 complete(&mdsc->safe_umount_waiters);
1874 mutex_unlock(&mdsc->mutex);
1878 req->r_got_unsafe = true;
1879 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
1882 dout("handle_reply tid %lld result %d\n", tid, result);
1883 rinfo = &req->r_reply_info;
1884 err = parse_reply_info(msg, rinfo);
1885 mutex_unlock(&mdsc->mutex);
1887 mutex_lock(&session->s_mutex);
1889 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds);
1895 if (rinfo->snapblob_len) {
1896 down_write(&mdsc->snap_rwsem);
1897 ceph_update_snap_trace(mdsc, rinfo->snapblob,
1898 rinfo->snapblob + rinfo->snapblob_len,
1899 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
1900 downgrade_write(&mdsc->snap_rwsem);
1902 down_read(&mdsc->snap_rwsem);
1905 /* insert trace into our cache */
1906 mutex_lock(&req->r_fill_mutex);
1907 err = ceph_fill_trace(mdsc->client->sb, req, req->r_session);
1909 if (result == 0 && rinfo->dir_nr)
1910 ceph_readdir_prepopulate(req, req->r_session);
1911 ceph_unreserve_caps(&req->r_caps_reservation);
1913 mutex_unlock(&req->r_fill_mutex);
1915 up_read(&mdsc->snap_rwsem);
1917 mutex_lock(&mdsc->mutex);
1918 if (!req->r_aborted) {
1924 req->r_got_result = true;
1927 dout("reply arrived after request %lld was aborted\n", tid);
1929 mutex_unlock(&mdsc->mutex);
1931 add_cap_releases(mdsc, req->r_session, -1);
1932 mutex_unlock(&session->s_mutex);
1934 /* kick calling process */
1935 complete_request(mdsc, req);
1937 ceph_mdsc_put_request(req);
1944 * handle mds notification that our request has been forwarded.
1946 static void handle_forward(struct ceph_mds_client *mdsc,
1947 struct ceph_mds_session *session,
1948 struct ceph_msg *msg)
1950 struct ceph_mds_request *req;
1951 u64 tid = le64_to_cpu(msg->hdr.tid);
1955 void *p = msg->front.iov_base;
1956 void *end = p + msg->front.iov_len;
1958 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1959 next_mds = ceph_decode_32(&p);
1960 fwd_seq = ceph_decode_32(&p);
1962 mutex_lock(&mdsc->mutex);
1963 req = __lookup_request(mdsc, tid);
1965 dout("forward %llu to mds%d - req dne\n", tid, next_mds);
1966 goto out; /* dup reply? */
1969 if (fwd_seq <= req->r_num_fwd) {
1970 dout("forward %llu to mds%d - old seq %d <= %d\n",
1971 tid, next_mds, req->r_num_fwd, fwd_seq);
1973 /* resend. forward race not possible; mds would drop */
1974 dout("forward %llu to mds%d (we resend)\n", tid, next_mds);
1975 req->r_num_fwd = fwd_seq;
1976 req->r_resend_mds = next_mds;
1977 put_request_session(req);
1978 __do_request(mdsc, req);
1980 ceph_mdsc_put_request(req);
1982 mutex_unlock(&mdsc->mutex);
1986 pr_err("mdsc_handle_forward decode error err=%d\n", err);
1990 * handle a mds session control message
1992 static void handle_session(struct ceph_mds_session *session,
1993 struct ceph_msg *msg)
1995 struct ceph_mds_client *mdsc = session->s_mdsc;
1998 int mds = session->s_mds;
1999 struct ceph_mds_session_head *h = msg->front.iov_base;
2003 if (msg->front.iov_len != sizeof(*h))
2005 op = le32_to_cpu(h->op);
2006 seq = le64_to_cpu(h->seq);
2008 mutex_lock(&mdsc->mutex);
2009 if (op == CEPH_SESSION_CLOSE)
2010 __unregister_session(mdsc, session);
2011 /* FIXME: this ttl calculation is generous */
2012 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2013 mutex_unlock(&mdsc->mutex);
2015 mutex_lock(&session->s_mutex);
2017 dout("handle_session mds%d %s %p state %s seq %llu\n",
2018 mds, ceph_session_op_name(op), session,
2019 session_state_name(session->s_state), seq);
2021 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2022 session->s_state = CEPH_MDS_SESSION_OPEN;
2023 pr_info("mds%d came back\n", session->s_mds);
2027 case CEPH_SESSION_OPEN:
2028 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2029 pr_info("mds%d reconnect success\n", session->s_mds);
2030 session->s_state = CEPH_MDS_SESSION_OPEN;
2031 renewed_caps(mdsc, session, 0);
2034 __close_session(mdsc, session);
2037 case CEPH_SESSION_RENEWCAPS:
2038 if (session->s_renew_seq == seq)
2039 renewed_caps(mdsc, session, 1);
2042 case CEPH_SESSION_CLOSE:
2043 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2044 pr_info("mds%d reconnect denied\n", session->s_mds);
2045 remove_session_caps(session);
2046 wake = 1; /* for good measure */
2047 complete(&mdsc->session_close_waiters);
2048 kick_requests(mdsc, mds);
2051 case CEPH_SESSION_STALE:
2052 pr_info("mds%d caps went stale, renewing\n",
2054 spin_lock(&session->s_cap_lock);
2055 session->s_cap_gen++;
2056 session->s_cap_ttl = 0;
2057 spin_unlock(&session->s_cap_lock);
2058 send_renew_caps(mdsc, session);
2061 case CEPH_SESSION_RECALL_STATE:
2062 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2066 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2070 mutex_unlock(&session->s_mutex);
2072 mutex_lock(&mdsc->mutex);
2073 __wake_requests(mdsc, &session->s_waiting);
2074 mutex_unlock(&mdsc->mutex);
2079 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2080 (int)msg->front.iov_len);
2087 * called under session->mutex.
2089 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2090 struct ceph_mds_session *session)
2092 struct ceph_mds_request *req, *nreq;
2095 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2097 mutex_lock(&mdsc->mutex);
2098 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2099 err = __prepare_send_request(mdsc, req, session->s_mds);
2101 ceph_msg_get(req->r_request);
2102 ceph_con_send(&session->s_con, req->r_request);
2105 mutex_unlock(&mdsc->mutex);
2109 * Encode information about a cap for a reconnect with the MDS.
2111 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2114 struct ceph_mds_cap_reconnect rec;
2115 struct ceph_inode_info *ci;
2116 struct ceph_pagelist *pagelist = arg;
2120 struct dentry *dentry;
2124 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2125 inode, ceph_vinop(inode), cap, cap->cap_id,
2126 ceph_cap_string(cap->issued));
2127 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2131 dentry = d_find_alias(inode);
2133 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2135 err = PTR_ERR(path);
2142 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2146 spin_lock(&inode->i_lock);
2147 cap->seq = 0; /* reset cap seq */
2148 cap->issue_seq = 0; /* and issue_seq */
2149 rec.cap_id = cpu_to_le64(cap->cap_id);
2150 rec.pathbase = cpu_to_le64(pathbase);
2151 rec.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2152 rec.issued = cpu_to_le32(cap->issued);
2153 rec.size = cpu_to_le64(inode->i_size);
2154 ceph_encode_timespec(&rec.mtime, &inode->i_mtime);
2155 ceph_encode_timespec(&rec.atime, &inode->i_atime);
2156 rec.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2157 spin_unlock(&inode->i_lock);
2159 err = ceph_pagelist_append(pagelist, &rec, sizeof(rec));
2169 * If an MDS fails and recovers, clients need to reconnect in order to
2170 * reestablish shared state. This includes all caps issued through
2171 * this session _and_ the snap_realm hierarchy. Because it's not
2172 * clear which snap realms the mds cares about, we send everything we
2173 * know about.. that ensures we'll then get any new info the
2174 * recovering MDS might have.
2176 * This is a relatively heavyweight operation, but it's rare.
2178 * called with mdsc->mutex held.
2180 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2181 struct ceph_mds_session *session)
2183 struct ceph_msg *reply;
2185 int mds = session->s_mds;
2187 struct ceph_pagelist *pagelist;
2189 pr_info("mds%d reconnect start\n", mds);
2191 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2193 goto fail_nopagelist;
2194 ceph_pagelist_init(pagelist);
2196 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0);
2200 mutex_lock(&session->s_mutex);
2201 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2204 ceph_con_open(&session->s_con,
2205 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2207 /* replay unsafe requests */
2208 replay_unsafe_requests(mdsc, session);
2210 down_read(&mdsc->snap_rwsem);
2212 dout("session %p state %s\n", session,
2213 session_state_name(session->s_state));
2215 /* drop old cap expires; we're about to reestablish that state */
2216 discard_cap_releases(mdsc, session);
2218 /* traverse this session's caps */
2219 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2222 err = iterate_session_caps(session, encode_caps_cb, pagelist);
2227 * snaprealms. we provide mds with the ino, seq (version), and
2228 * parent for all of our realms. If the mds has any newer info,
2231 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2232 struct ceph_snap_realm *realm =
2233 rb_entry(p, struct ceph_snap_realm, node);
2234 struct ceph_mds_snaprealm_reconnect sr_rec;
2236 dout(" adding snap realm %llx seq %lld parent %llx\n",
2237 realm->ino, realm->seq, realm->parent_ino);
2238 sr_rec.ino = cpu_to_le64(realm->ino);
2239 sr_rec.seq = cpu_to_le64(realm->seq);
2240 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2241 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2246 reply->pagelist = pagelist;
2247 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2248 reply->nr_pages = calc_pages_for(0, pagelist->length);
2249 ceph_con_send(&session->s_con, reply);
2251 mutex_unlock(&session->s_mutex);
2253 mutex_lock(&mdsc->mutex);
2254 __wake_requests(mdsc, &session->s_waiting);
2255 mutex_unlock(&mdsc->mutex);
2257 up_read(&mdsc->snap_rwsem);
2261 ceph_msg_put(reply);
2262 up_read(&mdsc->snap_rwsem);
2263 mutex_unlock(&session->s_mutex);
2265 ceph_pagelist_release(pagelist);
2268 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
2274 * compare old and new mdsmaps, kicking requests
2275 * and closing out old connections as necessary
2277 * called under mdsc->mutex.
2279 static void check_new_map(struct ceph_mds_client *mdsc,
2280 struct ceph_mdsmap *newmap,
2281 struct ceph_mdsmap *oldmap)
2284 int oldstate, newstate;
2285 struct ceph_mds_session *s;
2287 dout("check_new_map new %u old %u\n",
2288 newmap->m_epoch, oldmap->m_epoch);
2290 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2291 if (mdsc->sessions[i] == NULL)
2293 s = mdsc->sessions[i];
2294 oldstate = ceph_mdsmap_get_state(oldmap, i);
2295 newstate = ceph_mdsmap_get_state(newmap, i);
2297 dout("check_new_map mds%d state %s -> %s (session %s)\n",
2298 i, ceph_mds_state_name(oldstate),
2299 ceph_mds_state_name(newstate),
2300 session_state_name(s->s_state));
2302 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2303 ceph_mdsmap_get_addr(newmap, i),
2304 sizeof(struct ceph_entity_addr))) {
2305 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2306 /* the session never opened, just close it
2308 __wake_requests(mdsc, &s->s_waiting);
2309 __unregister_session(mdsc, s);
2312 mutex_unlock(&mdsc->mutex);
2313 mutex_lock(&s->s_mutex);
2314 mutex_lock(&mdsc->mutex);
2315 ceph_con_close(&s->s_con);
2316 mutex_unlock(&s->s_mutex);
2317 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2320 /* kick any requests waiting on the recovering mds */
2321 kick_requests(mdsc, i);
2322 } else if (oldstate == newstate) {
2323 continue; /* nothing new with this mds */
2329 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
2330 newstate >= CEPH_MDS_STATE_RECONNECT) {
2331 mutex_unlock(&mdsc->mutex);
2332 send_mds_reconnect(mdsc, s);
2333 mutex_lock(&mdsc->mutex);
2337 * kick request on any mds that has gone active.
2339 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2340 newstate >= CEPH_MDS_STATE_ACTIVE) {
2341 if (oldstate != CEPH_MDS_STATE_CREATING &&
2342 oldstate != CEPH_MDS_STATE_STARTING)
2343 pr_info("mds%d recovery completed\n", s->s_mds);
2344 kick_requests(mdsc, i);
2345 ceph_kick_flushing_caps(mdsc, s);
2346 wake_up_session_caps(s, 1);
2358 * caller must hold session s_mutex, dentry->d_lock
2360 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2362 struct ceph_dentry_info *di = ceph_dentry(dentry);
2364 ceph_put_mds_session(di->lease_session);
2365 di->lease_session = NULL;
2368 static void handle_lease(struct ceph_mds_client *mdsc,
2369 struct ceph_mds_session *session,
2370 struct ceph_msg *msg)
2372 struct super_block *sb = mdsc->client->sb;
2373 struct inode *inode;
2374 struct ceph_inode_info *ci;
2375 struct dentry *parent, *dentry;
2376 struct ceph_dentry_info *di;
2377 int mds = session->s_mds;
2378 struct ceph_mds_lease *h = msg->front.iov_base;
2379 struct ceph_vino vino;
2384 dout("handle_lease from mds%d\n", mds);
2387 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2389 vino.ino = le64_to_cpu(h->ino);
2390 vino.snap = CEPH_NOSNAP;
2391 mask = le16_to_cpu(h->mask);
2392 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2393 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2394 if (dname.len != get_unaligned_le32(h+1))
2397 mutex_lock(&session->s_mutex);
2401 inode = ceph_find_inode(sb, vino);
2402 dout("handle_lease '%s', mask %d, ino %llx %p\n",
2403 ceph_lease_op_name(h->action), mask, vino.ino, inode);
2404 if (inode == NULL) {
2405 dout("handle_lease no inode %llx\n", vino.ino);
2408 ci = ceph_inode(inode);
2411 parent = d_find_alias(inode);
2413 dout("no parent dentry on inode %p\n", inode);
2415 goto release; /* hrm... */
2417 dname.hash = full_name_hash(dname.name, dname.len);
2418 dentry = d_lookup(parent, &dname);
2423 spin_lock(&dentry->d_lock);
2424 di = ceph_dentry(dentry);
2425 switch (h->action) {
2426 case CEPH_MDS_LEASE_REVOKE:
2427 if (di && di->lease_session == session) {
2428 h->seq = cpu_to_le32(di->lease_seq);
2429 __ceph_mdsc_drop_dentry_lease(dentry);
2434 case CEPH_MDS_LEASE_RENEW:
2435 if (di && di->lease_session == session &&
2436 di->lease_gen == session->s_cap_gen &&
2437 di->lease_renew_from &&
2438 di->lease_renew_after == 0) {
2439 unsigned long duration =
2440 le32_to_cpu(h->duration_ms) * HZ / 1000;
2442 di->lease_seq = le32_to_cpu(h->seq);
2443 dentry->d_time = di->lease_renew_from + duration;
2444 di->lease_renew_after = di->lease_renew_from +
2446 di->lease_renew_from = 0;
2450 spin_unlock(&dentry->d_lock);
2457 /* let's just reuse the same message */
2458 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2460 ceph_con_send(&session->s_con, msg);
2464 mutex_unlock(&session->s_mutex);
2468 pr_err("corrupt lease message\n");
2472 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2473 struct inode *inode,
2474 struct dentry *dentry, char action,
2477 struct ceph_msg *msg;
2478 struct ceph_mds_lease *lease;
2479 int len = sizeof(*lease) + sizeof(u32);
2482 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2483 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2484 dnamelen = dentry->d_name.len;
2487 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len);
2490 lease = msg->front.iov_base;
2491 lease->action = action;
2492 lease->mask = cpu_to_le16(CEPH_LOCK_DN);
2493 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2494 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2495 lease->seq = cpu_to_le32(seq);
2496 put_unaligned_le32(dnamelen, lease + 1);
2497 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2500 * if this is a preemptive lease RELEASE, no need to
2501 * flush request stream, since the actual request will
2504 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2506 ceph_con_send(&session->s_con, msg);
2510 * Preemptively release a lease we expect to invalidate anyway.
2511 * Pass @inode always, @dentry is optional.
2513 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2514 struct dentry *dentry, int mask)
2516 struct ceph_dentry_info *di;
2517 struct ceph_mds_session *session;
2520 BUG_ON(inode == NULL);
2521 BUG_ON(dentry == NULL);
2522 BUG_ON(mask != CEPH_LOCK_DN);
2524 /* is dentry lease valid? */
2525 spin_lock(&dentry->d_lock);
2526 di = ceph_dentry(dentry);
2527 if (!di || !di->lease_session ||
2528 di->lease_session->s_mds < 0 ||
2529 di->lease_gen != di->lease_session->s_cap_gen ||
2530 !time_before(jiffies, dentry->d_time)) {
2531 dout("lease_release inode %p dentry %p -- "
2533 inode, dentry, mask);
2534 spin_unlock(&dentry->d_lock);
2538 /* we do have a lease on this dentry; note mds and seq */
2539 session = ceph_get_mds_session(di->lease_session);
2540 seq = di->lease_seq;
2541 __ceph_mdsc_drop_dentry_lease(dentry);
2542 spin_unlock(&dentry->d_lock);
2544 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2545 inode, dentry, mask, session->s_mds);
2546 ceph_mdsc_lease_send_msg(session, inode, dentry,
2547 CEPH_MDS_LEASE_RELEASE, seq);
2548 ceph_put_mds_session(session);
2552 * drop all leases (and dentry refs) in preparation for umount
2554 static void drop_leases(struct ceph_mds_client *mdsc)
2558 dout("drop_leases\n");
2559 mutex_lock(&mdsc->mutex);
2560 for (i = 0; i < mdsc->max_sessions; i++) {
2561 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2564 mutex_unlock(&mdsc->mutex);
2565 mutex_lock(&s->s_mutex);
2566 mutex_unlock(&s->s_mutex);
2567 ceph_put_mds_session(s);
2568 mutex_lock(&mdsc->mutex);
2570 mutex_unlock(&mdsc->mutex);
2576 * delayed work -- periodically trim expired leases, renew caps with mds
2578 static void schedule_delayed(struct ceph_mds_client *mdsc)
2581 unsigned hz = round_jiffies_relative(HZ * delay);
2582 schedule_delayed_work(&mdsc->delayed_work, hz);
2585 static void delayed_work(struct work_struct *work)
2588 struct ceph_mds_client *mdsc =
2589 container_of(work, struct ceph_mds_client, delayed_work.work);
2593 dout("mdsc delayed_work\n");
2594 ceph_check_delayed_caps(mdsc);
2596 mutex_lock(&mdsc->mutex);
2597 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2598 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2599 mdsc->last_renew_caps);
2601 mdsc->last_renew_caps = jiffies;
2603 for (i = 0; i < mdsc->max_sessions; i++) {
2604 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2607 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2608 dout("resending session close request for mds%d\n",
2610 request_close_session(mdsc, s);
2611 ceph_put_mds_session(s);
2614 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2615 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2616 s->s_state = CEPH_MDS_SESSION_HUNG;
2617 pr_info("mds%d hung\n", s->s_mds);
2620 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2621 /* this mds is failed or recovering, just wait */
2622 ceph_put_mds_session(s);
2625 mutex_unlock(&mdsc->mutex);
2627 mutex_lock(&s->s_mutex);
2629 send_renew_caps(mdsc, s);
2631 ceph_con_keepalive(&s->s_con);
2632 add_cap_releases(mdsc, s, -1);
2633 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2634 s->s_state == CEPH_MDS_SESSION_HUNG)
2635 send_cap_releases(mdsc, s);
2636 mutex_unlock(&s->s_mutex);
2637 ceph_put_mds_session(s);
2639 mutex_lock(&mdsc->mutex);
2641 mutex_unlock(&mdsc->mutex);
2643 schedule_delayed(mdsc);
2647 int ceph_mdsc_init(struct ceph_mds_client *mdsc, struct ceph_client *client)
2649 mdsc->client = client;
2650 mutex_init(&mdsc->mutex);
2651 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
2652 if (mdsc->mdsmap == NULL)
2655 init_completion(&mdsc->safe_umount_waiters);
2656 init_completion(&mdsc->session_close_waiters);
2657 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2658 mdsc->sessions = NULL;
2659 mdsc->max_sessions = 0;
2661 init_rwsem(&mdsc->snap_rwsem);
2662 mdsc->snap_realms = RB_ROOT;
2663 INIT_LIST_HEAD(&mdsc->snap_empty);
2664 spin_lock_init(&mdsc->snap_empty_lock);
2666 mdsc->request_tree = RB_ROOT;
2667 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2668 mdsc->last_renew_caps = jiffies;
2669 INIT_LIST_HEAD(&mdsc->cap_delay_list);
2670 spin_lock_init(&mdsc->cap_delay_lock);
2671 INIT_LIST_HEAD(&mdsc->snap_flush_list);
2672 spin_lock_init(&mdsc->snap_flush_lock);
2673 mdsc->cap_flush_seq = 0;
2674 INIT_LIST_HEAD(&mdsc->cap_dirty);
2675 mdsc->num_cap_flushing = 0;
2676 spin_lock_init(&mdsc->cap_dirty_lock);
2677 init_waitqueue_head(&mdsc->cap_flushing_wq);
2678 spin_lock_init(&mdsc->dentry_lru_lock);
2679 INIT_LIST_HEAD(&mdsc->dentry_lru);
2685 * Wait for safe replies on open mds requests. If we time out, drop
2686 * all requests from the tree to avoid dangling dentry refs.
2688 static void wait_requests(struct ceph_mds_client *mdsc)
2690 struct ceph_mds_request *req;
2691 struct ceph_client *client = mdsc->client;
2693 mutex_lock(&mdsc->mutex);
2694 if (__get_oldest_req(mdsc)) {
2695 mutex_unlock(&mdsc->mutex);
2697 dout("wait_requests waiting for requests\n");
2698 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
2699 client->mount_args->mount_timeout * HZ);
2701 /* tear down remaining requests */
2702 mutex_lock(&mdsc->mutex);
2703 while ((req = __get_oldest_req(mdsc))) {
2704 dout("wait_requests timed out on tid %llu\n",
2706 __unregister_request(mdsc, req);
2709 mutex_unlock(&mdsc->mutex);
2710 dout("wait_requests done\n");
2714 * called before mount is ro, and before dentries are torn down.
2715 * (hmm, does this still race with new lookups?)
2717 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
2719 dout("pre_umount\n");
2723 ceph_flush_dirty_caps(mdsc);
2724 wait_requests(mdsc);
2728 * wait for all write mds requests to flush.
2730 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
2732 struct ceph_mds_request *req = NULL, *nextreq;
2735 mutex_lock(&mdsc->mutex);
2736 dout("wait_unsafe_requests want %lld\n", want_tid);
2738 req = __get_oldest_req(mdsc);
2739 while (req && req->r_tid <= want_tid) {
2740 /* find next request */
2741 n = rb_next(&req->r_node);
2743 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
2746 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
2748 ceph_mdsc_get_request(req);
2750 ceph_mdsc_get_request(nextreq);
2751 mutex_unlock(&mdsc->mutex);
2752 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
2753 req->r_tid, want_tid);
2754 wait_for_completion(&req->r_safe_completion);
2755 mutex_lock(&mdsc->mutex);
2756 ceph_mdsc_put_request(req);
2758 break; /* next dne before, so we're done! */
2759 if (RB_EMPTY_NODE(&nextreq->r_node)) {
2760 /* next request was removed from tree */
2761 ceph_mdsc_put_request(nextreq);
2764 ceph_mdsc_put_request(nextreq); /* won't go away */
2768 mutex_unlock(&mdsc->mutex);
2769 dout("wait_unsafe_requests done\n");
2772 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
2774 u64 want_tid, want_flush;
2776 if (mdsc->client->mount_state == CEPH_MOUNT_SHUTDOWN)
2780 mutex_lock(&mdsc->mutex);
2781 want_tid = mdsc->last_tid;
2782 want_flush = mdsc->cap_flush_seq;
2783 mutex_unlock(&mdsc->mutex);
2784 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
2786 ceph_flush_dirty_caps(mdsc);
2788 wait_unsafe_requests(mdsc, want_tid);
2789 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
2794 * called after sb is ro.
2796 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
2798 struct ceph_mds_session *session;
2801 struct ceph_client *client = mdsc->client;
2802 unsigned long started, timeout = client->mount_args->mount_timeout * HZ;
2804 dout("close_sessions\n");
2806 mutex_lock(&mdsc->mutex);
2808 /* close sessions */
2810 while (time_before(jiffies, started + timeout)) {
2811 dout("closing sessions\n");
2813 for (i = 0; i < mdsc->max_sessions; i++) {
2814 session = __ceph_lookup_mds_session(mdsc, i);
2817 mutex_unlock(&mdsc->mutex);
2818 mutex_lock(&session->s_mutex);
2819 __close_session(mdsc, session);
2820 mutex_unlock(&session->s_mutex);
2821 ceph_put_mds_session(session);
2822 mutex_lock(&mdsc->mutex);
2828 if (client->mount_state == CEPH_MOUNT_SHUTDOWN)
2831 dout("waiting for sessions to close\n");
2832 mutex_unlock(&mdsc->mutex);
2833 wait_for_completion_timeout(&mdsc->session_close_waiters,
2835 mutex_lock(&mdsc->mutex);
2838 /* tear down remaining sessions */
2839 for (i = 0; i < mdsc->max_sessions; i++) {
2840 if (mdsc->sessions[i]) {
2841 session = get_session(mdsc->sessions[i]);
2842 __unregister_session(mdsc, session);
2843 mutex_unlock(&mdsc->mutex);
2844 mutex_lock(&session->s_mutex);
2845 remove_session_caps(session);
2846 mutex_unlock(&session->s_mutex);
2847 ceph_put_mds_session(session);
2848 mutex_lock(&mdsc->mutex);
2852 WARN_ON(!list_empty(&mdsc->cap_delay_list));
2854 mutex_unlock(&mdsc->mutex);
2856 ceph_cleanup_empty_realms(mdsc);
2858 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
2863 void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
2866 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
2868 ceph_mdsmap_destroy(mdsc->mdsmap);
2869 kfree(mdsc->sessions);
2874 * handle mds map update.
2876 void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
2880 void *p = msg->front.iov_base;
2881 void *end = p + msg->front.iov_len;
2882 struct ceph_mdsmap *newmap, *oldmap;
2883 struct ceph_fsid fsid;
2886 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
2887 ceph_decode_copy(&p, &fsid, sizeof(fsid));
2888 if (ceph_check_fsid(mdsc->client, &fsid) < 0)
2890 epoch = ceph_decode_32(&p);
2891 maplen = ceph_decode_32(&p);
2892 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
2894 /* do we need it? */
2895 ceph_monc_got_mdsmap(&mdsc->client->monc, epoch);
2896 mutex_lock(&mdsc->mutex);
2897 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
2898 dout("handle_map epoch %u <= our %u\n",
2899 epoch, mdsc->mdsmap->m_epoch);
2900 mutex_unlock(&mdsc->mutex);
2904 newmap = ceph_mdsmap_decode(&p, end);
2905 if (IS_ERR(newmap)) {
2906 err = PTR_ERR(newmap);
2910 /* swap into place */
2912 oldmap = mdsc->mdsmap;
2913 mdsc->mdsmap = newmap;
2914 check_new_map(mdsc, newmap, oldmap);
2915 ceph_mdsmap_destroy(oldmap);
2917 mdsc->mdsmap = newmap; /* first mds map */
2919 mdsc->client->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
2921 __wake_requests(mdsc, &mdsc->waiting_for_map);
2923 mutex_unlock(&mdsc->mutex);
2924 schedule_delayed(mdsc);
2928 mutex_unlock(&mdsc->mutex);
2930 pr_err("error decoding mdsmap %d\n", err);
2934 static struct ceph_connection *con_get(struct ceph_connection *con)
2936 struct ceph_mds_session *s = con->private;
2938 if (get_session(s)) {
2939 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
2942 dout("mdsc con_get %p FAIL\n", s);
2946 static void con_put(struct ceph_connection *con)
2948 struct ceph_mds_session *s = con->private;
2950 ceph_put_mds_session(s);
2951 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
2955 * if the client is unresponsive for long enough, the mds will kill
2956 * the session entirely.
2958 static void peer_reset(struct ceph_connection *con)
2960 struct ceph_mds_session *s = con->private;
2962 pr_err("mds%d gave us the boot. IMPLEMENT RECONNECT.\n",
2966 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2968 struct ceph_mds_session *s = con->private;
2969 struct ceph_mds_client *mdsc = s->s_mdsc;
2970 int type = le16_to_cpu(msg->hdr.type);
2972 mutex_lock(&mdsc->mutex);
2973 if (__verify_registered_session(mdsc, s) < 0) {
2974 mutex_unlock(&mdsc->mutex);
2977 mutex_unlock(&mdsc->mutex);
2980 case CEPH_MSG_MDS_MAP:
2981 ceph_mdsc_handle_map(mdsc, msg);
2983 case CEPH_MSG_CLIENT_SESSION:
2984 handle_session(s, msg);
2986 case CEPH_MSG_CLIENT_REPLY:
2987 handle_reply(s, msg);
2989 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
2990 handle_forward(mdsc, s, msg);
2992 case CEPH_MSG_CLIENT_CAPS:
2993 ceph_handle_caps(s, msg);
2995 case CEPH_MSG_CLIENT_SNAP:
2996 ceph_handle_snap(mdsc, s, msg);
2998 case CEPH_MSG_CLIENT_LEASE:
2999 handle_lease(mdsc, s, msg);
3003 pr_err("received unknown message type %d %s\n", type,
3004 ceph_msg_type_name(type));
3013 static int get_authorizer(struct ceph_connection *con,
3014 void **buf, int *len, int *proto,
3015 void **reply_buf, int *reply_len, int force_new)
3017 struct ceph_mds_session *s = con->private;
3018 struct ceph_mds_client *mdsc = s->s_mdsc;
3019 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3022 if (force_new && s->s_authorizer) {
3023 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3024 s->s_authorizer = NULL;
3026 if (s->s_authorizer == NULL) {
3027 if (ac->ops->create_authorizer) {
3028 ret = ac->ops->create_authorizer(
3029 ac, CEPH_ENTITY_TYPE_MDS,
3031 &s->s_authorizer_buf,
3032 &s->s_authorizer_buf_len,
3033 &s->s_authorizer_reply_buf,
3034 &s->s_authorizer_reply_buf_len);
3040 *proto = ac->protocol;
3041 *buf = s->s_authorizer_buf;
3042 *len = s->s_authorizer_buf_len;
3043 *reply_buf = s->s_authorizer_reply_buf;
3044 *reply_len = s->s_authorizer_reply_buf_len;
3049 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3051 struct ceph_mds_session *s = con->private;
3052 struct ceph_mds_client *mdsc = s->s_mdsc;
3053 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3055 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3058 static int invalidate_authorizer(struct ceph_connection *con)
3060 struct ceph_mds_session *s = con->private;
3061 struct ceph_mds_client *mdsc = s->s_mdsc;
3062 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3064 if (ac->ops->invalidate_authorizer)
3065 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3067 return ceph_monc_validate_auth(&mdsc->client->monc);
3070 const static struct ceph_connection_operations mds_con_ops = {
3073 .dispatch = dispatch,
3074 .get_authorizer = get_authorizer,
3075 .verify_authorizer_reply = verify_authorizer_reply,
3076 .invalidate_authorizer = invalidate_authorizer,
3077 .peer_reset = peer_reset,