1 #include "ceph_debug.h"
3 #include <linux/wait.h>
4 #include <linux/sched.h>
6 #include "mds_client.h"
7 #include "mon_client.h"
15 * A cluster of MDS (metadata server) daemons is responsible for
16 * managing the file system namespace (the directory hierarchy and
17 * inodes) and for coordinating shared access to storage. Metadata is
18 * partitioning hierarchically across a number of servers, and that
19 * partition varies over time as the cluster adjusts the distribution
20 * in order to balance load.
22 * The MDS client is primarily responsible to managing synchronous
23 * metadata requests for operations like open, unlink, and so forth.
24 * If there is a MDS failure, we find out about it when we (possibly
25 * request and) receive a new MDS map, and can resubmit affected
28 * For the most part, though, we take advantage of a lossless
29 * communications channel to the MDS, and do not need to worry about
30 * timing out or resubmitting requests.
32 * We maintain a stateful "session" with each MDS we interact with.
33 * Within each session, we sent periodic heartbeat messages to ensure
34 * any capabilities or leases we have been issues remain valid. If
35 * the session times out and goes stale, our leases and capabilities
36 * are no longer valid.
39 static void __wake_requests(struct ceph_mds_client *mdsc,
40 struct list_head *head);
42 const static struct ceph_connection_operations mds_con_ops;
50 * parse individual inode info
52 static int parse_reply_info_in(void **p, void *end,
53 struct ceph_mds_reply_info_in *info)
58 *p += sizeof(struct ceph_mds_reply_inode) +
59 sizeof(*info->in->fragtree.splits) *
60 le32_to_cpu(info->in->fragtree.nsplits);
62 ceph_decode_32_safe(p, end, info->symlink_len, bad);
63 ceph_decode_need(p, end, info->symlink_len, bad);
65 *p += info->symlink_len;
67 ceph_decode_32_safe(p, end, info->xattr_len, bad);
68 ceph_decode_need(p, end, info->xattr_len, bad);
69 info->xattr_data = *p;
70 *p += info->xattr_len;
77 * parse a normal reply, which may contain a (dir+)dentry and/or a
80 static int parse_reply_info_trace(void **p, void *end,
81 struct ceph_mds_reply_info_parsed *info)
85 if (info->head->is_dentry) {
86 err = parse_reply_info_in(p, end, &info->diri);
90 if (unlikely(*p + sizeof(*info->dirfrag) > end))
93 *p += sizeof(*info->dirfrag) +
94 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
95 if (unlikely(*p > end))
98 ceph_decode_32_safe(p, end, info->dname_len, bad);
99 ceph_decode_need(p, end, info->dname_len, bad);
101 *p += info->dname_len;
103 *p += sizeof(*info->dlease);
106 if (info->head->is_target) {
107 err = parse_reply_info_in(p, end, &info->targeti);
112 if (unlikely(*p != end))
119 pr_err("problem parsing mds trace %d\n", err);
124 * parse readdir results
126 static int parse_reply_info_dir(void **p, void *end,
127 struct ceph_mds_reply_info_parsed *info)
133 if (*p + sizeof(*info->dir_dir) > end)
135 *p += sizeof(*info->dir_dir) +
136 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
140 ceph_decode_need(p, end, sizeof(num) + 2, bad);
141 num = ceph_decode_32(p);
142 info->dir_end = ceph_decode_8(p);
143 info->dir_complete = ceph_decode_8(p);
147 /* alloc large array */
149 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
150 sizeof(*info->dir_dname) +
151 sizeof(*info->dir_dname_len) +
152 sizeof(*info->dir_dlease),
154 if (info->dir_in == NULL) {
158 info->dir_dname = (void *)(info->dir_in + num);
159 info->dir_dname_len = (void *)(info->dir_dname + num);
160 info->dir_dlease = (void *)(info->dir_dname_len + num);
164 ceph_decode_need(p, end, sizeof(u32)*2, bad);
165 info->dir_dname_len[i] = ceph_decode_32(p);
166 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
167 info->dir_dname[i] = *p;
168 *p += info->dir_dname_len[i];
169 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
171 info->dir_dlease[i] = *p;
172 *p += sizeof(struct ceph_mds_reply_lease);
175 err = parse_reply_info_in(p, end, &info->dir_in[i]);
190 pr_err("problem parsing dir contents %d\n", err);
195 * parse entire mds reply
197 static int parse_reply_info(struct ceph_msg *msg,
198 struct ceph_mds_reply_info_parsed *info)
204 info->head = msg->front.iov_base;
205 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
206 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
209 ceph_decode_32_safe(&p, end, len, bad);
211 err = parse_reply_info_trace(&p, p+len, info);
217 ceph_decode_32_safe(&p, end, len, bad);
219 err = parse_reply_info_dir(&p, p+len, info);
225 ceph_decode_32_safe(&p, end, len, bad);
226 info->snapblob_len = len;
237 pr_err("mds parse_reply err %d\n", err);
241 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
250 static const char *session_state_name(int s)
253 case CEPH_MDS_SESSION_NEW: return "new";
254 case CEPH_MDS_SESSION_OPENING: return "opening";
255 case CEPH_MDS_SESSION_OPEN: return "open";
256 case CEPH_MDS_SESSION_HUNG: return "hung";
257 case CEPH_MDS_SESSION_CLOSING: return "closing";
258 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
259 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
260 default: return "???";
264 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
266 if (atomic_inc_not_zero(&s->s_ref)) {
267 dout("mdsc get_session %p %d -> %d\n", s,
268 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
271 dout("mdsc get_session %p 0 -- FAIL", s);
276 void ceph_put_mds_session(struct ceph_mds_session *s)
278 dout("mdsc put_session %p %d -> %d\n", s,
279 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
280 if (atomic_dec_and_test(&s->s_ref)) {
282 s->s_mdsc->client->monc.auth->ops->destroy_authorizer(
283 s->s_mdsc->client->monc.auth, s->s_authorizer);
289 * called under mdsc->mutex
291 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
294 struct ceph_mds_session *session;
296 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
298 session = mdsc->sessions[mds];
299 dout("lookup_mds_session %p %d\n", session,
300 atomic_read(&session->s_ref));
301 get_session(session);
305 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
307 if (mds >= mdsc->max_sessions)
309 return mdsc->sessions[mds];
312 static int __verify_registered_session(struct ceph_mds_client *mdsc,
313 struct ceph_mds_session *s)
315 if (s->s_mds >= mdsc->max_sessions ||
316 mdsc->sessions[s->s_mds] != s)
322 * create+register a new session for given mds.
323 * called under mdsc->mutex.
325 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
328 struct ceph_mds_session *s;
330 s = kzalloc(sizeof(*s), GFP_NOFS);
333 s->s_state = CEPH_MDS_SESSION_NEW;
336 mutex_init(&s->s_mutex);
338 ceph_con_init(mdsc->client->msgr, &s->s_con);
339 s->s_con.private = s;
340 s->s_con.ops = &mds_con_ops;
341 s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
342 s->s_con.peer_name.num = cpu_to_le64(mds);
344 spin_lock_init(&s->s_cap_lock);
347 s->s_renew_requested = 0;
349 INIT_LIST_HEAD(&s->s_caps);
352 atomic_set(&s->s_ref, 1);
353 INIT_LIST_HEAD(&s->s_waiting);
354 INIT_LIST_HEAD(&s->s_unsafe);
355 s->s_num_cap_releases = 0;
356 s->s_cap_iterator = NULL;
357 INIT_LIST_HEAD(&s->s_cap_releases);
358 INIT_LIST_HEAD(&s->s_cap_releases_done);
359 INIT_LIST_HEAD(&s->s_cap_flushing);
360 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
362 dout("register_session mds%d\n", mds);
363 if (mds >= mdsc->max_sessions) {
364 int newmax = 1 << get_count_order(mds+1);
365 struct ceph_mds_session **sa;
367 dout("register_session realloc to %d\n", newmax);
368 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
371 if (mdsc->sessions) {
372 memcpy(sa, mdsc->sessions,
373 mdsc->max_sessions * sizeof(void *));
374 kfree(mdsc->sessions);
377 mdsc->max_sessions = newmax;
379 mdsc->sessions[mds] = s;
380 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
382 ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
388 return ERR_PTR(-ENOMEM);
392 * called under mdsc->mutex
394 static void __unregister_session(struct ceph_mds_client *mdsc,
395 struct ceph_mds_session *s)
397 dout("__unregister_session mds%d %p\n", s->s_mds, s);
398 BUG_ON(mdsc->sessions[s->s_mds] != s);
399 mdsc->sessions[s->s_mds] = NULL;
400 ceph_con_close(&s->s_con);
401 ceph_put_mds_session(s);
405 * drop session refs in request.
407 * should be last request ref, or hold mdsc->mutex
409 static void put_request_session(struct ceph_mds_request *req)
411 if (req->r_session) {
412 ceph_put_mds_session(req->r_session);
413 req->r_session = NULL;
417 void ceph_mdsc_release_request(struct kref *kref)
419 struct ceph_mds_request *req = container_of(kref,
420 struct ceph_mds_request,
423 ceph_msg_put(req->r_request);
425 ceph_msg_put(req->r_reply);
426 destroy_reply_info(&req->r_reply_info);
429 ceph_put_cap_refs(ceph_inode(req->r_inode),
433 if (req->r_locked_dir)
434 ceph_put_cap_refs(ceph_inode(req->r_locked_dir),
436 if (req->r_target_inode)
437 iput(req->r_target_inode);
440 if (req->r_old_dentry) {
442 ceph_inode(req->r_old_dentry->d_parent->d_inode),
444 dput(req->r_old_dentry);
448 put_request_session(req);
449 ceph_unreserve_caps(&req->r_caps_reservation);
454 * lookup session, bump ref if found.
456 * called under mdsc->mutex.
458 static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
461 struct ceph_mds_request *req;
462 struct rb_node *n = mdsc->request_tree.rb_node;
465 req = rb_entry(n, struct ceph_mds_request, r_node);
466 if (tid < req->r_tid)
468 else if (tid > req->r_tid)
471 ceph_mdsc_get_request(req);
478 static void __insert_request(struct ceph_mds_client *mdsc,
479 struct ceph_mds_request *new)
481 struct rb_node **p = &mdsc->request_tree.rb_node;
482 struct rb_node *parent = NULL;
483 struct ceph_mds_request *req = NULL;
487 req = rb_entry(parent, struct ceph_mds_request, r_node);
488 if (new->r_tid < req->r_tid)
490 else if (new->r_tid > req->r_tid)
496 rb_link_node(&new->r_node, parent, p);
497 rb_insert_color(&new->r_node, &mdsc->request_tree);
501 * Register an in-flight request, and assign a tid. Link to directory
502 * are modifying (if any).
504 * Called under mdsc->mutex.
506 static void __register_request(struct ceph_mds_client *mdsc,
507 struct ceph_mds_request *req,
510 req->r_tid = ++mdsc->last_tid;
512 ceph_reserve_caps(&req->r_caps_reservation, req->r_num_caps);
513 dout("__register_request %p tid %lld\n", req, req->r_tid);
514 ceph_mdsc_get_request(req);
515 __insert_request(mdsc, req);
518 struct ceph_inode_info *ci = ceph_inode(dir);
520 spin_lock(&ci->i_unsafe_lock);
521 req->r_unsafe_dir = dir;
522 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
523 spin_unlock(&ci->i_unsafe_lock);
527 static void __unregister_request(struct ceph_mds_client *mdsc,
528 struct ceph_mds_request *req)
530 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
531 rb_erase(&req->r_node, &mdsc->request_tree);
532 RB_CLEAR_NODE(&req->r_node);
533 ceph_mdsc_put_request(req);
535 if (req->r_unsafe_dir) {
536 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
538 spin_lock(&ci->i_unsafe_lock);
539 list_del_init(&req->r_unsafe_dir_item);
540 spin_unlock(&ci->i_unsafe_lock);
545 * Choose mds to send request to next. If there is a hint set in the
546 * request (e.g., due to a prior forward hint from the mds), use that.
547 * Otherwise, consult frag tree and/or caps to identify the
548 * appropriate mds. If all else fails, choose randomly.
550 * Called under mdsc->mutex.
552 static int __choose_mds(struct ceph_mds_client *mdsc,
553 struct ceph_mds_request *req)
556 struct ceph_inode_info *ci;
557 struct ceph_cap *cap;
558 int mode = req->r_direct_mode;
560 u32 hash = req->r_direct_hash;
561 bool is_hash = req->r_direct_is_hash;
564 * is there a specific mds we should try? ignore hint if we have
565 * no session and the mds is not up (active or recovering).
567 if (req->r_resend_mds >= 0 &&
568 (__have_session(mdsc, req->r_resend_mds) ||
569 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
570 dout("choose_mds using resend_mds mds%d\n",
572 return req->r_resend_mds;
575 if (mode == USE_RANDOM_MDS)
580 inode = req->r_inode;
581 } else if (req->r_dentry) {
582 if (req->r_dentry->d_inode) {
583 inode = req->r_dentry->d_inode;
585 inode = req->r_dentry->d_parent->d_inode;
586 hash = req->r_dentry->d_name.hash;
590 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
594 ci = ceph_inode(inode);
596 if (is_hash && S_ISDIR(inode->i_mode)) {
597 struct ceph_inode_frag frag;
600 ceph_choose_frag(ci, hash, &frag, &found);
602 if (mode == USE_ANY_MDS && frag.ndist > 0) {
605 /* choose a random replica */
606 get_random_bytes(&r, 1);
609 dout("choose_mds %p %llx.%llx "
610 "frag %u mds%d (%d/%d)\n",
611 inode, ceph_vinop(inode),
617 /* since this file/dir wasn't known to be
618 * replicated, then we want to look for the
619 * authoritative mds. */
622 /* choose auth mds */
624 dout("choose_mds %p %llx.%llx "
625 "frag %u mds%d (auth)\n",
626 inode, ceph_vinop(inode), frag.frag, mds);
632 spin_lock(&inode->i_lock);
634 if (mode == USE_AUTH_MDS)
635 cap = ci->i_auth_cap;
636 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
637 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
639 spin_unlock(&inode->i_lock);
642 mds = cap->session->s_mds;
643 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
644 inode, ceph_vinop(inode), mds,
645 cap == ci->i_auth_cap ? "auth " : "", cap);
646 spin_unlock(&inode->i_lock);
650 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
651 dout("choose_mds chose random mds%d\n", mds);
659 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
661 struct ceph_msg *msg;
662 struct ceph_mds_session_head *h;
664 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), 0, 0, NULL);
666 pr_err("create_session_msg ENOMEM creating msg\n");
667 return ERR_PTR(PTR_ERR(msg));
669 h = msg->front.iov_base;
670 h->op = cpu_to_le32(op);
671 h->seq = cpu_to_le64(seq);
676 * send session open request.
678 * called under mdsc->mutex
680 static int __open_session(struct ceph_mds_client *mdsc,
681 struct ceph_mds_session *session)
683 struct ceph_msg *msg;
685 int mds = session->s_mds;
688 /* wait for mds to go active? */
689 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
690 dout("open_session to mds%d (%s)\n", mds,
691 ceph_mds_state_name(mstate));
692 session->s_state = CEPH_MDS_SESSION_OPENING;
693 session->s_renew_requested = jiffies;
695 /* send connect message */
696 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
701 ceph_con_send(&session->s_con, msg);
712 * Free preallocated cap messages assigned to this session
714 static void cleanup_cap_releases(struct ceph_mds_session *session)
716 struct ceph_msg *msg;
718 spin_lock(&session->s_cap_lock);
719 while (!list_empty(&session->s_cap_releases)) {
720 msg = list_first_entry(&session->s_cap_releases,
721 struct ceph_msg, list_head);
722 list_del_init(&msg->list_head);
725 while (!list_empty(&session->s_cap_releases_done)) {
726 msg = list_first_entry(&session->s_cap_releases_done,
727 struct ceph_msg, list_head);
728 list_del_init(&msg->list_head);
731 spin_unlock(&session->s_cap_lock);
735 * Helper to safely iterate over all caps associated with a session.
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;
931 dout("request_close_session mds%d state %s seq %lld\n",
932 session->s_mds, session_state_name(session->s_state),
934 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
938 ceph_con_send(&session->s_con, msg);
943 * Called with s_mutex held.
945 static int __close_session(struct ceph_mds_client *mdsc,
946 struct ceph_mds_session *session)
948 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
950 session->s_state = CEPH_MDS_SESSION_CLOSING;
951 return request_close_session(mdsc, session);
957 * Because we can't cache an inode without one or more caps, we do
958 * this indirectly: if a cap is unused, we prune its aliases, at which
959 * point the inode will hopefully get dropped to.
961 * Yes, this is a bit sloppy. Our only real goal here is to respond to
962 * memory pressure from the MDS, though, so it needn't be perfect.
964 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
966 struct ceph_mds_session *session = arg;
967 struct ceph_inode_info *ci = ceph_inode(inode);
968 int used, oissued, mine;
970 if (session->s_trim_caps <= 0)
973 spin_lock(&inode->i_lock);
974 mine = cap->issued | cap->implemented;
975 used = __ceph_caps_used(ci);
976 oissued = __ceph_caps_issued_other(ci, cap);
978 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
979 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
980 ceph_cap_string(used));
981 if (ci->i_dirty_caps)
982 goto out; /* dirty caps */
983 if ((used & ~oissued) & mine)
984 goto out; /* we need these caps */
986 session->s_trim_caps--;
988 /* we aren't the only cap.. just remove us */
989 __ceph_remove_cap(cap);
991 /* try to drop referring dentries */
992 spin_unlock(&inode->i_lock);
993 d_prune_aliases(inode);
994 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
995 inode, cap, atomic_read(&inode->i_count));
1000 spin_unlock(&inode->i_lock);
1005 * Trim session cap count down to some max number.
1007 static int trim_caps(struct ceph_mds_client *mdsc,
1008 struct ceph_mds_session *session,
1011 int trim_caps = session->s_nr_caps - max_caps;
1013 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1014 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1015 if (trim_caps > 0) {
1016 session->s_trim_caps = trim_caps;
1017 iterate_session_caps(session, trim_caps_cb, session);
1018 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1019 session->s_mds, session->s_nr_caps, max_caps,
1020 trim_caps - session->s_trim_caps);
1021 session->s_trim_caps = 0;
1027 * Allocate cap_release messages. If there is a partially full message
1028 * in the queue, try to allocate enough to cover it's remainder, so that
1029 * we can send it immediately.
1031 * Called under s_mutex.
1033 static int add_cap_releases(struct ceph_mds_client *mdsc,
1034 struct ceph_mds_session *session,
1037 struct ceph_msg *msg;
1038 struct ceph_mds_cap_release *head;
1042 extra = mdsc->client->mount_args->cap_release_safety;
1044 spin_lock(&session->s_cap_lock);
1046 if (!list_empty(&session->s_cap_releases)) {
1047 msg = list_first_entry(&session->s_cap_releases,
1050 head = msg->front.iov_base;
1051 extra += CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num);
1054 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1055 spin_unlock(&session->s_cap_lock);
1056 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1060 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1061 (int)msg->front.iov_len);
1062 head = msg->front.iov_base;
1063 head->num = cpu_to_le32(0);
1064 msg->front.iov_len = sizeof(*head);
1065 spin_lock(&session->s_cap_lock);
1066 list_add(&msg->list_head, &session->s_cap_releases);
1067 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1070 if (!list_empty(&session->s_cap_releases)) {
1071 msg = list_first_entry(&session->s_cap_releases,
1074 head = msg->front.iov_base;
1076 dout(" queueing non-full %p (%d)\n", msg,
1077 le32_to_cpu(head->num));
1078 list_move_tail(&msg->list_head,
1079 &session->s_cap_releases_done);
1080 session->s_num_cap_releases -=
1081 CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num);
1085 spin_unlock(&session->s_cap_lock);
1091 * flush all dirty inode data to disk.
1093 * returns true if we've flushed through want_flush_seq
1095 static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1099 dout("check_cap_flush want %lld\n", want_flush_seq);
1100 mutex_lock(&mdsc->mutex);
1101 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1102 struct ceph_mds_session *session = mdsc->sessions[mds];
1106 get_session(session);
1107 mutex_unlock(&mdsc->mutex);
1109 mutex_lock(&session->s_mutex);
1110 if (!list_empty(&session->s_cap_flushing)) {
1111 struct ceph_inode_info *ci =
1112 list_entry(session->s_cap_flushing.next,
1113 struct ceph_inode_info,
1115 struct inode *inode = &ci->vfs_inode;
1117 spin_lock(&inode->i_lock);
1118 if (ci->i_cap_flush_seq <= want_flush_seq) {
1119 dout("check_cap_flush still flushing %p "
1120 "seq %lld <= %lld to mds%d\n", inode,
1121 ci->i_cap_flush_seq, want_flush_seq,
1125 spin_unlock(&inode->i_lock);
1127 mutex_unlock(&session->s_mutex);
1128 ceph_put_mds_session(session);
1132 mutex_lock(&mdsc->mutex);
1135 mutex_unlock(&mdsc->mutex);
1136 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1141 * called under s_mutex
1143 static void send_cap_releases(struct ceph_mds_client *mdsc,
1144 struct ceph_mds_session *session)
1146 struct ceph_msg *msg;
1148 dout("send_cap_releases mds%d\n", session->s_mds);
1150 spin_lock(&session->s_cap_lock);
1151 if (list_empty(&session->s_cap_releases_done))
1153 msg = list_first_entry(&session->s_cap_releases_done,
1154 struct ceph_msg, list_head);
1155 list_del_init(&msg->list_head);
1156 spin_unlock(&session->s_cap_lock);
1157 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1158 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1159 ceph_con_send(&session->s_con, msg);
1161 spin_unlock(&session->s_cap_lock);
1169 * Create an mds request.
1171 struct ceph_mds_request *
1172 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1174 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1177 return ERR_PTR(-ENOMEM);
1179 req->r_started = jiffies;
1180 req->r_resend_mds = -1;
1181 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1183 kref_init(&req->r_kref);
1184 INIT_LIST_HEAD(&req->r_wait);
1185 init_completion(&req->r_completion);
1186 init_completion(&req->r_safe_completion);
1187 INIT_LIST_HEAD(&req->r_unsafe_item);
1190 req->r_direct_mode = mode;
1195 * return oldest (lowest) request, tid in request tree, 0 if none.
1197 * called under mdsc->mutex.
1199 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1201 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1203 return rb_entry(rb_first(&mdsc->request_tree),
1204 struct ceph_mds_request, r_node);
1207 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1209 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1217 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1218 * on build_path_from_dentry in fs/cifs/dir.c.
1220 * If @stop_on_nosnap, generate path relative to the first non-snapped
1223 * Encode hidden .snap dirs as a double /, i.e.
1224 * foo/.snap/bar -> foo//bar
1226 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1229 struct dentry *temp;
1234 return ERR_PTR(-EINVAL);
1238 for (temp = dentry; !IS_ROOT(temp);) {
1239 struct inode *inode = temp->d_inode;
1240 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1241 len++; /* slash only */
1242 else if (stop_on_nosnap && inode &&
1243 ceph_snap(inode) == CEPH_NOSNAP)
1246 len += 1 + temp->d_name.len;
1247 temp = temp->d_parent;
1249 pr_err("build_path_dentry corrupt dentry %p\n", dentry);
1250 return ERR_PTR(-EINVAL);
1254 len--; /* no leading '/' */
1256 path = kmalloc(len+1, GFP_NOFS);
1258 return ERR_PTR(-ENOMEM);
1260 path[pos] = 0; /* trailing null */
1261 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1262 struct inode *inode = temp->d_inode;
1264 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1265 dout("build_path_dentry path+%d: %p SNAPDIR\n",
1267 } else if (stop_on_nosnap && inode &&
1268 ceph_snap(inode) == CEPH_NOSNAP) {
1271 pos -= temp->d_name.len;
1274 strncpy(path + pos, temp->d_name.name,
1276 dout("build_path_dentry path+%d: %p '%.*s'\n",
1277 pos, temp, temp->d_name.len, path + pos);
1281 temp = temp->d_parent;
1283 pr_err("build_path_dentry corrupt dentry\n");
1285 return ERR_PTR(-EINVAL);
1289 pr_err("build_path_dentry did not end path lookup where "
1290 "expected, namelen is %d, pos is %d\n", len, pos);
1291 /* presumably this is only possible if racing with a
1292 rename of one of the parent directories (we can not
1293 lock the dentries above us to prevent this, but
1294 retrying should be harmless) */
1299 *base = ceph_ino(temp->d_inode);
1301 dout("build_path_dentry on %p %d built %llx '%.*s'\n",
1302 dentry, atomic_read(&dentry->d_count), *base, len, path);
1306 static int build_dentry_path(struct dentry *dentry,
1307 const char **ppath, int *ppathlen, u64 *pino,
1312 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1313 *pino = ceph_ino(dentry->d_parent->d_inode);
1314 *ppath = dentry->d_name.name;
1315 *ppathlen = dentry->d_name.len;
1318 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1320 return PTR_ERR(path);
1326 static int build_inode_path(struct inode *inode,
1327 const char **ppath, int *ppathlen, u64 *pino,
1330 struct dentry *dentry;
1333 if (ceph_snap(inode) == CEPH_NOSNAP) {
1334 *pino = ceph_ino(inode);
1338 dentry = d_find_alias(inode);
1339 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1342 return PTR_ERR(path);
1349 * request arguments may be specified via an inode *, a dentry *, or
1350 * an explicit ino+path.
1352 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1353 const char *rpath, u64 rino,
1354 const char **ppath, int *pathlen,
1355 u64 *ino, int *freepath)
1360 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1361 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1363 } else if (rdentry) {
1364 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1365 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1370 *pathlen = strlen(rpath);
1371 dout(" path %.*s\n", *pathlen, rpath);
1378 * called under mdsc->mutex
1380 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1381 struct ceph_mds_request *req,
1384 struct ceph_msg *msg;
1385 struct ceph_mds_request_head *head;
1386 const char *path1 = NULL;
1387 const char *path2 = NULL;
1388 u64 ino1 = 0, ino2 = 0;
1389 int pathlen1 = 0, pathlen2 = 0;
1390 int freepath1 = 0, freepath2 = 0;
1396 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1397 req->r_path1, req->r_ino1.ino,
1398 &path1, &pathlen1, &ino1, &freepath1);
1404 ret = set_request_path_attr(NULL, req->r_old_dentry,
1405 req->r_path2, req->r_ino2.ino,
1406 &path2, &pathlen2, &ino2, &freepath2);
1412 len = sizeof(*head) +
1413 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
1415 /* calculate (max) length for cap releases */
1416 len += sizeof(struct ceph_mds_request_release) *
1417 (!!req->r_inode_drop + !!req->r_dentry_drop +
1418 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1419 if (req->r_dentry_drop)
1420 len += req->r_dentry->d_name.len;
1421 if (req->r_old_dentry_drop)
1422 len += req->r_old_dentry->d_name.len;
1424 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, 0, 0, NULL);
1428 msg->hdr.tid = cpu_to_le64(req->r_tid);
1430 head = msg->front.iov_base;
1431 p = msg->front.iov_base + sizeof(*head);
1432 end = msg->front.iov_base + msg->front.iov_len;
1434 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1435 head->op = cpu_to_le32(req->r_op);
1436 head->caller_uid = cpu_to_le32(current_fsuid());
1437 head->caller_gid = cpu_to_le32(current_fsgid());
1438 head->args = req->r_args;
1440 ceph_encode_filepath(&p, end, ino1, path1);
1441 ceph_encode_filepath(&p, end, ino2, path2);
1445 if (req->r_inode_drop)
1446 releases += ceph_encode_inode_release(&p,
1447 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1448 mds, req->r_inode_drop, req->r_inode_unless, 0);
1449 if (req->r_dentry_drop)
1450 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1451 mds, req->r_dentry_drop, req->r_dentry_unless);
1452 if (req->r_old_dentry_drop)
1453 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1454 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1455 if (req->r_old_inode_drop)
1456 releases += ceph_encode_inode_release(&p,
1457 req->r_old_dentry->d_inode,
1458 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1459 head->num_releases = cpu_to_le16(releases);
1462 msg->front.iov_len = p - msg->front.iov_base;
1463 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1465 msg->pages = req->r_pages;
1466 msg->nr_pages = req->r_num_pages;
1467 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1468 msg->hdr.data_off = cpu_to_le16(0);
1472 kfree((char *)path2);
1475 kfree((char *)path1);
1481 * called under mdsc->mutex if error, under no mutex if
1484 static void complete_request(struct ceph_mds_client *mdsc,
1485 struct ceph_mds_request *req)
1487 if (req->r_callback)
1488 req->r_callback(mdsc, req);
1490 complete(&req->r_completion);
1494 * called under mdsc->mutex
1496 static int __prepare_send_request(struct ceph_mds_client *mdsc,
1497 struct ceph_mds_request *req,
1500 struct ceph_mds_request_head *rhead;
1501 struct ceph_msg *msg;
1506 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1507 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1509 if (req->r_request) {
1510 ceph_msg_put(req->r_request);
1511 req->r_request = NULL;
1513 msg = create_request_message(mdsc, req, mds);
1515 req->r_reply = ERR_PTR(PTR_ERR(msg));
1516 complete_request(mdsc, req);
1517 return -PTR_ERR(msg);
1519 req->r_request = msg;
1521 rhead = msg->front.iov_base;
1522 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1523 if (req->r_got_unsafe)
1524 flags |= CEPH_MDS_FLAG_REPLAY;
1525 if (req->r_locked_dir)
1526 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1527 rhead->flags = cpu_to_le32(flags);
1528 rhead->num_fwd = req->r_num_fwd;
1529 rhead->num_retry = req->r_attempts - 1;
1531 dout(" r_locked_dir = %p\n", req->r_locked_dir);
1533 if (req->r_target_inode && req->r_got_unsafe)
1534 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1541 * send request, or put it on the appropriate wait list.
1543 static int __do_request(struct ceph_mds_client *mdsc,
1544 struct ceph_mds_request *req)
1546 struct ceph_mds_session *session = NULL;
1553 if (req->r_timeout &&
1554 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1555 dout("do_request timed out\n");
1560 mds = __choose_mds(mdsc, req);
1562 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1563 dout("do_request no mds or not active, waiting for map\n");
1564 list_add(&req->r_wait, &mdsc->waiting_for_map);
1568 /* get, open session */
1569 session = __ceph_lookup_mds_session(mdsc, mds);
1571 session = register_session(mdsc, mds);
1572 if (IS_ERR(session)) {
1573 err = PTR_ERR(session);
1577 dout("do_request mds%d session %p state %s\n", mds, session,
1578 session_state_name(session->s_state));
1579 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1580 session->s_state != CEPH_MDS_SESSION_HUNG) {
1581 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1582 session->s_state == CEPH_MDS_SESSION_CLOSING)
1583 __open_session(mdsc, session);
1584 list_add(&req->r_wait, &session->s_waiting);
1589 req->r_session = get_session(session);
1590 req->r_resend_mds = -1; /* forget any previous mds hint */
1592 if (req->r_request_started == 0) /* note request start time */
1593 req->r_request_started = jiffies;
1595 err = __prepare_send_request(mdsc, req, mds);
1597 ceph_msg_get(req->r_request);
1598 ceph_con_send(&session->s_con, req->r_request);
1602 ceph_put_mds_session(session);
1607 req->r_reply = ERR_PTR(err);
1608 complete_request(mdsc, req);
1613 * called under mdsc->mutex
1615 static void __wake_requests(struct ceph_mds_client *mdsc,
1616 struct list_head *head)
1618 struct ceph_mds_request *req, *nreq;
1620 list_for_each_entry_safe(req, nreq, head, r_wait) {
1621 list_del_init(&req->r_wait);
1622 __do_request(mdsc, req);
1627 * Wake up threads with requests pending for @mds, so that they can
1628 * resubmit their requests to a possibly different mds. If @all is set,
1629 * wake up if their requests has been forwarded to @mds, too.
1631 static void kick_requests(struct ceph_mds_client *mdsc, int mds, int all)
1633 struct ceph_mds_request *req;
1636 dout("kick_requests mds%d\n", mds);
1637 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1638 req = rb_entry(p, struct ceph_mds_request, r_node);
1639 if (req->r_got_unsafe)
1641 if (req->r_session &&
1642 req->r_session->s_mds == mds) {
1643 dout(" kicking tid %llu\n", req->r_tid);
1644 put_request_session(req);
1645 __do_request(mdsc, req);
1650 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1651 struct ceph_mds_request *req)
1653 dout("submit_request on %p\n", req);
1654 mutex_lock(&mdsc->mutex);
1655 __register_request(mdsc, req, NULL);
1656 __do_request(mdsc, req);
1657 mutex_unlock(&mdsc->mutex);
1661 * Synchrously perform an mds request. Take care of all of the
1662 * session setup, forwarding, retry details.
1664 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1666 struct ceph_mds_request *req)
1670 dout("do_request on %p\n", req);
1672 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1674 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1675 if (req->r_locked_dir)
1676 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1677 if (req->r_old_dentry)
1679 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1683 mutex_lock(&mdsc->mutex);
1684 __register_request(mdsc, req, dir);
1685 __do_request(mdsc, req);
1688 if (!req->r_reply) {
1689 mutex_unlock(&mdsc->mutex);
1690 if (req->r_timeout) {
1691 err = (long)wait_for_completion_interruptible_timeout(
1692 &req->r_completion, req->r_timeout);
1694 req->r_reply = ERR_PTR(-EIO);
1696 req->r_reply = ERR_PTR(err);
1698 err = wait_for_completion_interruptible(
1699 &req->r_completion);
1701 req->r_reply = ERR_PTR(err);
1703 mutex_lock(&mdsc->mutex);
1706 if (IS_ERR(req->r_reply)) {
1707 err = PTR_ERR(req->r_reply);
1708 req->r_reply = NULL;
1710 if (err == -ERESTARTSYS) {
1712 req->r_aborted = true;
1714 if (req->r_locked_dir &&
1715 (req->r_op & CEPH_MDS_OP_WRITE)) {
1716 struct ceph_inode_info *ci =
1717 ceph_inode(req->r_locked_dir);
1719 dout("aborted, clearing I_COMPLETE on %p\n",
1721 spin_lock(&req->r_locked_dir->i_lock);
1722 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1723 ci->i_release_count++;
1724 spin_unlock(&req->r_locked_dir->i_lock);
1727 /* clean up this request */
1728 __unregister_request(mdsc, req);
1729 if (!list_empty(&req->r_unsafe_item))
1730 list_del_init(&req->r_unsafe_item);
1731 complete(&req->r_safe_completion);
1733 } else if (req->r_err) {
1736 err = le32_to_cpu(req->r_reply_info.head->result);
1738 mutex_unlock(&mdsc->mutex);
1740 dout("do_request %p done, result %d\n", req, err);
1747 * We take the session mutex and parse and process the reply immediately.
1748 * This preserves the logical ordering of replies, capabilities, etc., sent
1749 * by the MDS as they are applied to our local cache.
1751 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
1753 struct ceph_mds_client *mdsc = session->s_mdsc;
1754 struct ceph_mds_request *req;
1755 struct ceph_mds_reply_head *head = msg->front.iov_base;
1756 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
1759 int mds = session->s_mds;
1761 if (msg->front.iov_len < sizeof(*head)) {
1762 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
1767 /* get request, session */
1768 tid = le64_to_cpu(msg->hdr.tid);
1769 mutex_lock(&mdsc->mutex);
1770 req = __lookup_request(mdsc, tid);
1772 dout("handle_reply on unknown tid %llu\n", tid);
1773 mutex_unlock(&mdsc->mutex);
1776 dout("handle_reply %p\n", req);
1778 /* correct session? */
1779 if (!req->r_session && req->r_session != session) {
1780 pr_err("mdsc_handle_reply got %llu on session mds%d"
1781 " not mds%d\n", tid, session->s_mds,
1782 req->r_session ? req->r_session->s_mds : -1);
1783 mutex_unlock(&mdsc->mutex);
1788 if ((req->r_got_unsafe && !head->safe) ||
1789 (req->r_got_safe && head->safe)) {
1790 pr_warning("got a dup %s reply on %llu from mds%d\n",
1791 head->safe ? "safe" : "unsafe", tid, mds);
1792 mutex_unlock(&mdsc->mutex);
1796 result = le32_to_cpu(head->result);
1799 * Tolerate 2 consecutive ESTALEs from the same mds.
1800 * FIXME: we should be looking at the cap migrate_seq.
1802 if (result == -ESTALE) {
1803 req->r_direct_mode = USE_AUTH_MDS;
1805 if (req->r_num_stale <= 2) {
1806 __do_request(mdsc, req);
1807 mutex_unlock(&mdsc->mutex);
1811 req->r_num_stale = 0;
1815 req->r_got_safe = true;
1816 __unregister_request(mdsc, req);
1817 complete(&req->r_safe_completion);
1819 if (req->r_got_unsafe) {
1821 * We already handled the unsafe response, now do the
1822 * cleanup. No need to examine the response; the MDS
1823 * doesn't include any result info in the safe
1824 * response. And even if it did, there is nothing
1825 * useful we could do with a revised return value.
1827 dout("got safe reply %llu, mds%d\n", tid, mds);
1828 list_del_init(&req->r_unsafe_item);
1830 /* last unsafe request during umount? */
1831 if (mdsc->stopping && !__get_oldest_req(mdsc))
1832 complete(&mdsc->safe_umount_waiters);
1833 mutex_unlock(&mdsc->mutex);
1838 BUG_ON(req->r_reply);
1841 req->r_got_unsafe = true;
1842 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
1845 dout("handle_reply tid %lld result %d\n", tid, result);
1846 rinfo = &req->r_reply_info;
1847 err = parse_reply_info(msg, rinfo);
1848 mutex_unlock(&mdsc->mutex);
1850 mutex_lock(&session->s_mutex);
1852 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds);
1858 if (rinfo->snapblob_len) {
1859 down_write(&mdsc->snap_rwsem);
1860 ceph_update_snap_trace(mdsc, rinfo->snapblob,
1861 rinfo->snapblob + rinfo->snapblob_len,
1862 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
1863 downgrade_write(&mdsc->snap_rwsem);
1865 down_read(&mdsc->snap_rwsem);
1868 /* insert trace into our cache */
1869 err = ceph_fill_trace(mdsc->client->sb, req, req->r_session);
1871 if (result == 0 && rinfo->dir_nr)
1872 ceph_readdir_prepopulate(req, req->r_session);
1873 ceph_unreserve_caps(&req->r_caps_reservation);
1876 up_read(&mdsc->snap_rwsem);
1885 add_cap_releases(mdsc, req->r_session, -1);
1886 mutex_unlock(&session->s_mutex);
1888 /* kick calling process */
1889 complete_request(mdsc, req);
1891 ceph_mdsc_put_request(req);
1898 * handle mds notification that our request has been forwarded.
1900 static void handle_forward(struct ceph_mds_client *mdsc,
1901 struct ceph_mds_session *session,
1902 struct ceph_msg *msg)
1904 struct ceph_mds_request *req;
1905 u64 tid = le64_to_cpu(msg->hdr.tid);
1909 void *p = msg->front.iov_base;
1910 void *end = p + msg->front.iov_len;
1912 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
1913 next_mds = ceph_decode_32(&p);
1914 fwd_seq = ceph_decode_32(&p);
1916 mutex_lock(&mdsc->mutex);
1917 req = __lookup_request(mdsc, tid);
1919 dout("forward %llu to mds%d - req dne\n", tid, next_mds);
1920 goto out; /* dup reply? */
1923 if (fwd_seq <= req->r_num_fwd) {
1924 dout("forward %llu to mds%d - old seq %d <= %d\n",
1925 tid, next_mds, req->r_num_fwd, fwd_seq);
1927 /* resend. forward race not possible; mds would drop */
1928 dout("forward %llu to mds%d (we resend)\n", tid, next_mds);
1929 req->r_num_fwd = fwd_seq;
1930 req->r_resend_mds = next_mds;
1931 put_request_session(req);
1932 __do_request(mdsc, req);
1934 ceph_mdsc_put_request(req);
1936 mutex_unlock(&mdsc->mutex);
1940 pr_err("mdsc_handle_forward decode error err=%d\n", err);
1944 * handle a mds session control message
1946 static void handle_session(struct ceph_mds_session *session,
1947 struct ceph_msg *msg)
1949 struct ceph_mds_client *mdsc = session->s_mdsc;
1952 int mds = session->s_mds;
1953 struct ceph_mds_session_head *h = msg->front.iov_base;
1957 if (msg->front.iov_len != sizeof(*h))
1959 op = le32_to_cpu(h->op);
1960 seq = le64_to_cpu(h->seq);
1962 mutex_lock(&mdsc->mutex);
1963 if (op == CEPH_SESSION_CLOSE)
1964 __unregister_session(mdsc, session);
1965 /* FIXME: this ttl calculation is generous */
1966 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
1967 mutex_unlock(&mdsc->mutex);
1969 mutex_lock(&session->s_mutex);
1971 dout("handle_session mds%d %s %p state %s seq %llu\n",
1972 mds, ceph_session_op_name(op), session,
1973 session_state_name(session->s_state), seq);
1975 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
1976 session->s_state = CEPH_MDS_SESSION_OPEN;
1977 pr_info("mds%d came back\n", session->s_mds);
1981 case CEPH_SESSION_OPEN:
1982 session->s_state = CEPH_MDS_SESSION_OPEN;
1983 renewed_caps(mdsc, session, 0);
1986 __close_session(mdsc, session);
1989 case CEPH_SESSION_RENEWCAPS:
1990 if (session->s_renew_seq == seq)
1991 renewed_caps(mdsc, session, 1);
1994 case CEPH_SESSION_CLOSE:
1995 remove_session_caps(session);
1996 wake = 1; /* for good measure */
1997 complete(&mdsc->session_close_waiters);
1998 kick_requests(mdsc, mds, 0); /* cur only */
2001 case CEPH_SESSION_STALE:
2002 pr_info("mds%d caps went stale, renewing\n",
2004 spin_lock(&session->s_cap_lock);
2005 session->s_cap_gen++;
2006 session->s_cap_ttl = 0;
2007 spin_unlock(&session->s_cap_lock);
2008 send_renew_caps(mdsc, session);
2011 case CEPH_SESSION_RECALL_STATE:
2012 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2016 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2020 mutex_unlock(&session->s_mutex);
2022 mutex_lock(&mdsc->mutex);
2023 __wake_requests(mdsc, &session->s_waiting);
2024 mutex_unlock(&mdsc->mutex);
2029 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2030 (int)msg->front.iov_len);
2037 * called under session->mutex.
2039 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2040 struct ceph_mds_session *session)
2042 struct ceph_mds_request *req, *nreq;
2045 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2047 mutex_lock(&mdsc->mutex);
2048 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2049 err = __prepare_send_request(mdsc, req, session->s_mds);
2051 ceph_msg_get(req->r_request);
2052 ceph_con_send(&session->s_con, req->r_request);
2055 mutex_unlock(&mdsc->mutex);
2059 * Encode information about a cap for a reconnect with the MDS.
2061 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2064 struct ceph_mds_cap_reconnect rec;
2065 struct ceph_inode_info *ci;
2066 struct ceph_pagelist *pagelist = arg;
2070 struct dentry *dentry;
2074 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2075 inode, ceph_vinop(inode), cap, cap->cap_id,
2076 ceph_cap_string(cap->issued));
2077 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2081 dentry = d_find_alias(inode);
2083 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2085 err = PTR_ERR(path);
2092 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2096 spin_lock(&inode->i_lock);
2097 cap->seq = 0; /* reset cap seq */
2098 cap->issue_seq = 0; /* and issue_seq */
2099 rec.cap_id = cpu_to_le64(cap->cap_id);
2100 rec.pathbase = cpu_to_le64(pathbase);
2101 rec.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2102 rec.issued = cpu_to_le32(cap->issued);
2103 rec.size = cpu_to_le64(inode->i_size);
2104 ceph_encode_timespec(&rec.mtime, &inode->i_mtime);
2105 ceph_encode_timespec(&rec.atime, &inode->i_atime);
2106 rec.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2107 spin_unlock(&inode->i_lock);
2109 err = ceph_pagelist_append(pagelist, &rec, sizeof(rec));
2119 * If an MDS fails and recovers, clients need to reconnect in order to
2120 * reestablish shared state. This includes all caps issued through
2121 * this session _and_ the snap_realm hierarchy. Because it's not
2122 * clear which snap realms the mds cares about, we send everything we
2123 * know about.. that ensures we'll then get any new info the
2124 * recovering MDS might have.
2126 * This is a relatively heavyweight operation, but it's rare.
2128 * called with mdsc->mutex held.
2130 static void send_mds_reconnect(struct ceph_mds_client *mdsc, int mds)
2132 struct ceph_mds_session *session = NULL;
2133 struct ceph_msg *reply;
2136 struct ceph_pagelist *pagelist;
2138 pr_info("reconnect to recovering mds%d\n", mds);
2140 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2142 goto fail_nopagelist;
2143 ceph_pagelist_init(pagelist);
2145 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, 0, 0, NULL);
2146 if (IS_ERR(reply)) {
2147 err = PTR_ERR(reply);
2152 session = __ceph_lookup_mds_session(mdsc, mds);
2153 mutex_unlock(&mdsc->mutex); /* drop lock for duration */
2156 mutex_lock(&session->s_mutex);
2158 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2161 ceph_con_open(&session->s_con,
2162 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2164 /* replay unsafe requests */
2165 replay_unsafe_requests(mdsc, session);
2167 dout("no session for mds%d, will send short reconnect\n",
2171 down_read(&mdsc->snap_rwsem);
2175 dout("session %p state %s\n", session,
2176 session_state_name(session->s_state));
2178 /* traverse this session's caps */
2179 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2182 err = iterate_session_caps(session, encode_caps_cb, pagelist);
2187 * snaprealms. we provide mds with the ino, seq (version), and
2188 * parent for all of our realms. If the mds has any newer info,
2191 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2192 struct ceph_snap_realm *realm =
2193 rb_entry(p, struct ceph_snap_realm, node);
2194 struct ceph_mds_snaprealm_reconnect sr_rec;
2196 dout(" adding snap realm %llx seq %lld parent %llx\n",
2197 realm->ino, realm->seq, realm->parent_ino);
2198 sr_rec.ino = cpu_to_le64(realm->ino);
2199 sr_rec.seq = cpu_to_le64(realm->seq);
2200 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2201 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2207 reply->pagelist = pagelist;
2208 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2209 reply->nr_pages = calc_pages_for(0, pagelist->length);
2210 ceph_con_send(&session->s_con, reply);
2213 session->s_state = CEPH_MDS_SESSION_OPEN;
2214 __wake_requests(mdsc, &session->s_waiting);
2218 up_read(&mdsc->snap_rwsem);
2220 mutex_unlock(&session->s_mutex);
2221 ceph_put_mds_session(session);
2223 mutex_lock(&mdsc->mutex);
2227 ceph_msg_put(reply);
2229 ceph_pagelist_release(pagelist);
2232 pr_err("ENOMEM preparing reconnect for mds%d\n", mds);
2238 * compare old and new mdsmaps, kicking requests
2239 * and closing out old connections as necessary
2241 * called under mdsc->mutex.
2243 static void check_new_map(struct ceph_mds_client *mdsc,
2244 struct ceph_mdsmap *newmap,
2245 struct ceph_mdsmap *oldmap)
2248 int oldstate, newstate;
2249 struct ceph_mds_session *s;
2251 dout("check_new_map new %u old %u\n",
2252 newmap->m_epoch, oldmap->m_epoch);
2254 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2255 if (mdsc->sessions[i] == NULL)
2257 s = mdsc->sessions[i];
2258 oldstate = ceph_mdsmap_get_state(oldmap, i);
2259 newstate = ceph_mdsmap_get_state(newmap, i);
2261 dout("check_new_map mds%d state %s -> %s (session %s)\n",
2262 i, ceph_mds_state_name(oldstate),
2263 ceph_mds_state_name(newstate),
2264 session_state_name(s->s_state));
2266 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2267 ceph_mdsmap_get_addr(newmap, i),
2268 sizeof(struct ceph_entity_addr))) {
2269 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2270 /* the session never opened, just close it
2272 __wake_requests(mdsc, &s->s_waiting);
2273 __unregister_session(mdsc, s);
2276 mutex_unlock(&mdsc->mutex);
2277 mutex_lock(&s->s_mutex);
2278 mutex_lock(&mdsc->mutex);
2279 ceph_con_close(&s->s_con);
2280 mutex_unlock(&s->s_mutex);
2281 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2284 /* kick any requests waiting on the recovering mds */
2285 kick_requests(mdsc, i, 1);
2286 } else if (oldstate == newstate) {
2287 continue; /* nothing new with this mds */
2293 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
2294 newstate >= CEPH_MDS_STATE_RECONNECT)
2295 send_mds_reconnect(mdsc, i);
2298 * kick requests on any mds that has gone active.
2300 * kick requests on cur or forwarder: we may have sent
2301 * the request to mds1, mds1 told us it forwarded it
2302 * to mds2, but then we learn mds1 failed and can't be
2303 * sure it successfully forwarded our request before
2306 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2307 newstate >= CEPH_MDS_STATE_ACTIVE) {
2308 pr_info("mds%d reconnect completed\n", s->s_mds);
2309 kick_requests(mdsc, i, 1);
2310 ceph_kick_flushing_caps(mdsc, s);
2311 wake_up_session_caps(s, 1);
2323 * caller must hold session s_mutex, dentry->d_lock
2325 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2327 struct ceph_dentry_info *di = ceph_dentry(dentry);
2329 ceph_put_mds_session(di->lease_session);
2330 di->lease_session = NULL;
2333 static void handle_lease(struct ceph_mds_client *mdsc,
2334 struct ceph_mds_session *session,
2335 struct ceph_msg *msg)
2337 struct super_block *sb = mdsc->client->sb;
2338 struct inode *inode;
2339 struct ceph_inode_info *ci;
2340 struct dentry *parent, *dentry;
2341 struct ceph_dentry_info *di;
2342 int mds = session->s_mds;
2343 struct ceph_mds_lease *h = msg->front.iov_base;
2344 struct ceph_vino vino;
2349 dout("handle_lease from mds%d\n", mds);
2352 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2354 vino.ino = le64_to_cpu(h->ino);
2355 vino.snap = CEPH_NOSNAP;
2356 mask = le16_to_cpu(h->mask);
2357 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2358 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2359 if (dname.len != get_unaligned_le32(h+1))
2362 mutex_lock(&session->s_mutex);
2366 inode = ceph_find_inode(sb, vino);
2367 dout("handle_lease '%s', mask %d, ino %llx %p\n",
2368 ceph_lease_op_name(h->action), mask, vino.ino, inode);
2369 if (inode == NULL) {
2370 dout("handle_lease no inode %llx\n", vino.ino);
2373 ci = ceph_inode(inode);
2376 parent = d_find_alias(inode);
2378 dout("no parent dentry on inode %p\n", inode);
2380 goto release; /* hrm... */
2382 dname.hash = full_name_hash(dname.name, dname.len);
2383 dentry = d_lookup(parent, &dname);
2388 spin_lock(&dentry->d_lock);
2389 di = ceph_dentry(dentry);
2390 switch (h->action) {
2391 case CEPH_MDS_LEASE_REVOKE:
2392 if (di && di->lease_session == session) {
2393 h->seq = cpu_to_le32(di->lease_seq);
2394 __ceph_mdsc_drop_dentry_lease(dentry);
2399 case CEPH_MDS_LEASE_RENEW:
2400 if (di && di->lease_session == session &&
2401 di->lease_gen == session->s_cap_gen &&
2402 di->lease_renew_from &&
2403 di->lease_renew_after == 0) {
2404 unsigned long duration =
2405 le32_to_cpu(h->duration_ms) * HZ / 1000;
2407 di->lease_seq = le32_to_cpu(h->seq);
2408 dentry->d_time = di->lease_renew_from + duration;
2409 di->lease_renew_after = di->lease_renew_from +
2411 di->lease_renew_from = 0;
2415 spin_unlock(&dentry->d_lock);
2422 /* let's just reuse the same message */
2423 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2425 ceph_con_send(&session->s_con, msg);
2429 mutex_unlock(&session->s_mutex);
2433 pr_err("corrupt lease message\n");
2437 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2438 struct inode *inode,
2439 struct dentry *dentry, char action,
2442 struct ceph_msg *msg;
2443 struct ceph_mds_lease *lease;
2444 int len = sizeof(*lease) + sizeof(u32);
2447 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2448 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2449 dnamelen = dentry->d_name.len;
2452 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, 0, 0, NULL);
2455 lease = msg->front.iov_base;
2456 lease->action = action;
2457 lease->mask = cpu_to_le16(CEPH_LOCK_DN);
2458 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2459 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2460 lease->seq = cpu_to_le32(seq);
2461 put_unaligned_le32(dnamelen, lease + 1);
2462 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2465 * if this is a preemptive lease RELEASE, no need to
2466 * flush request stream, since the actual request will
2469 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2471 ceph_con_send(&session->s_con, msg);
2475 * Preemptively release a lease we expect to invalidate anyway.
2476 * Pass @inode always, @dentry is optional.
2478 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2479 struct dentry *dentry, int mask)
2481 struct ceph_dentry_info *di;
2482 struct ceph_mds_session *session;
2485 BUG_ON(inode == NULL);
2486 BUG_ON(dentry == NULL);
2487 BUG_ON(mask != CEPH_LOCK_DN);
2489 /* is dentry lease valid? */
2490 spin_lock(&dentry->d_lock);
2491 di = ceph_dentry(dentry);
2492 if (!di || !di->lease_session ||
2493 di->lease_session->s_mds < 0 ||
2494 di->lease_gen != di->lease_session->s_cap_gen ||
2495 !time_before(jiffies, dentry->d_time)) {
2496 dout("lease_release inode %p dentry %p -- "
2498 inode, dentry, mask);
2499 spin_unlock(&dentry->d_lock);
2503 /* we do have a lease on this dentry; note mds and seq */
2504 session = ceph_get_mds_session(di->lease_session);
2505 seq = di->lease_seq;
2506 __ceph_mdsc_drop_dentry_lease(dentry);
2507 spin_unlock(&dentry->d_lock);
2509 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2510 inode, dentry, mask, session->s_mds);
2511 ceph_mdsc_lease_send_msg(session, inode, dentry,
2512 CEPH_MDS_LEASE_RELEASE, seq);
2513 ceph_put_mds_session(session);
2517 * drop all leases (and dentry refs) in preparation for umount
2519 static void drop_leases(struct ceph_mds_client *mdsc)
2523 dout("drop_leases\n");
2524 mutex_lock(&mdsc->mutex);
2525 for (i = 0; i < mdsc->max_sessions; i++) {
2526 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2529 mutex_unlock(&mdsc->mutex);
2530 mutex_lock(&s->s_mutex);
2531 mutex_unlock(&s->s_mutex);
2532 ceph_put_mds_session(s);
2533 mutex_lock(&mdsc->mutex);
2535 mutex_unlock(&mdsc->mutex);
2541 * delayed work -- periodically trim expired leases, renew caps with mds
2543 static void schedule_delayed(struct ceph_mds_client *mdsc)
2546 unsigned hz = round_jiffies_relative(HZ * delay);
2547 schedule_delayed_work(&mdsc->delayed_work, hz);
2550 static void delayed_work(struct work_struct *work)
2553 struct ceph_mds_client *mdsc =
2554 container_of(work, struct ceph_mds_client, delayed_work.work);
2558 dout("mdsc delayed_work\n");
2559 ceph_check_delayed_caps(mdsc);
2561 mutex_lock(&mdsc->mutex);
2562 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2563 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2564 mdsc->last_renew_caps);
2566 mdsc->last_renew_caps = jiffies;
2568 for (i = 0; i < mdsc->max_sessions; i++) {
2569 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2572 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2573 dout("resending session close request for mds%d\n",
2575 request_close_session(mdsc, s);
2576 ceph_put_mds_session(s);
2579 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2580 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2581 s->s_state = CEPH_MDS_SESSION_HUNG;
2582 pr_info("mds%d hung\n", s->s_mds);
2585 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2586 /* this mds is failed or recovering, just wait */
2587 ceph_put_mds_session(s);
2590 mutex_unlock(&mdsc->mutex);
2592 mutex_lock(&s->s_mutex);
2594 send_renew_caps(mdsc, s);
2596 ceph_con_keepalive(&s->s_con);
2597 add_cap_releases(mdsc, s, -1);
2598 send_cap_releases(mdsc, s);
2599 mutex_unlock(&s->s_mutex);
2600 ceph_put_mds_session(s);
2602 mutex_lock(&mdsc->mutex);
2604 mutex_unlock(&mdsc->mutex);
2606 schedule_delayed(mdsc);
2610 int ceph_mdsc_init(struct ceph_mds_client *mdsc, struct ceph_client *client)
2612 mdsc->client = client;
2613 mutex_init(&mdsc->mutex);
2614 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
2615 init_completion(&mdsc->safe_umount_waiters);
2616 init_completion(&mdsc->session_close_waiters);
2617 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2618 mdsc->sessions = NULL;
2619 mdsc->max_sessions = 0;
2621 init_rwsem(&mdsc->snap_rwsem);
2622 mdsc->snap_realms = RB_ROOT;
2623 INIT_LIST_HEAD(&mdsc->snap_empty);
2624 spin_lock_init(&mdsc->snap_empty_lock);
2626 mdsc->request_tree = RB_ROOT;
2627 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2628 mdsc->last_renew_caps = jiffies;
2629 INIT_LIST_HEAD(&mdsc->cap_delay_list);
2630 spin_lock_init(&mdsc->cap_delay_lock);
2631 INIT_LIST_HEAD(&mdsc->snap_flush_list);
2632 spin_lock_init(&mdsc->snap_flush_lock);
2633 mdsc->cap_flush_seq = 0;
2634 INIT_LIST_HEAD(&mdsc->cap_dirty);
2635 mdsc->num_cap_flushing = 0;
2636 spin_lock_init(&mdsc->cap_dirty_lock);
2637 init_waitqueue_head(&mdsc->cap_flushing_wq);
2638 spin_lock_init(&mdsc->dentry_lru_lock);
2639 INIT_LIST_HEAD(&mdsc->dentry_lru);
2644 * Wait for safe replies on open mds requests. If we time out, drop
2645 * all requests from the tree to avoid dangling dentry refs.
2647 static void wait_requests(struct ceph_mds_client *mdsc)
2649 struct ceph_mds_request *req;
2650 struct ceph_client *client = mdsc->client;
2652 mutex_lock(&mdsc->mutex);
2653 if (__get_oldest_req(mdsc)) {
2654 mutex_unlock(&mdsc->mutex);
2656 dout("wait_requests waiting for requests\n");
2657 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
2658 client->mount_args->mount_timeout * HZ);
2660 /* tear down remaining requests */
2661 mutex_lock(&mdsc->mutex);
2662 while ((req = __get_oldest_req(mdsc))) {
2663 dout("wait_requests timed out on tid %llu\n",
2665 __unregister_request(mdsc, req);
2668 mutex_unlock(&mdsc->mutex);
2669 dout("wait_requests done\n");
2673 * called before mount is ro, and before dentries are torn down.
2674 * (hmm, does this still race with new lookups?)
2676 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
2678 dout("pre_umount\n");
2682 ceph_flush_dirty_caps(mdsc);
2683 wait_requests(mdsc);
2687 * wait for all write mds requests to flush.
2689 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
2691 struct ceph_mds_request *req = NULL, *nextreq;
2694 mutex_lock(&mdsc->mutex);
2695 dout("wait_unsafe_requests want %lld\n", want_tid);
2697 req = __get_oldest_req(mdsc);
2698 while (req && req->r_tid <= want_tid) {
2699 /* find next request */
2700 n = rb_next(&req->r_node);
2702 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
2705 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
2707 ceph_mdsc_get_request(req);
2709 ceph_mdsc_get_request(nextreq);
2710 mutex_unlock(&mdsc->mutex);
2711 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
2712 req->r_tid, want_tid);
2713 wait_for_completion(&req->r_safe_completion);
2714 mutex_lock(&mdsc->mutex);
2715 ceph_mdsc_put_request(req);
2717 break; /* next dne before, so we're done! */
2718 if (RB_EMPTY_NODE(&nextreq->r_node)) {
2719 /* next request was removed from tree */
2720 ceph_mdsc_put_request(nextreq);
2723 ceph_mdsc_put_request(nextreq); /* won't go away */
2727 mutex_unlock(&mdsc->mutex);
2728 dout("wait_unsafe_requests done\n");
2731 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
2733 u64 want_tid, want_flush;
2736 mutex_lock(&mdsc->mutex);
2737 want_tid = mdsc->last_tid;
2738 want_flush = mdsc->cap_flush_seq;
2739 mutex_unlock(&mdsc->mutex);
2740 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
2742 ceph_flush_dirty_caps(mdsc);
2744 wait_unsafe_requests(mdsc, want_tid);
2745 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
2750 * called after sb is ro.
2752 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
2754 struct ceph_mds_session *session;
2757 struct ceph_client *client = mdsc->client;
2758 unsigned long started, timeout = client->mount_args->mount_timeout * HZ;
2760 dout("close_sessions\n");
2762 mutex_lock(&mdsc->mutex);
2764 /* close sessions */
2766 while (time_before(jiffies, started + timeout)) {
2767 dout("closing sessions\n");
2769 for (i = 0; i < mdsc->max_sessions; i++) {
2770 session = __ceph_lookup_mds_session(mdsc, i);
2773 mutex_unlock(&mdsc->mutex);
2774 mutex_lock(&session->s_mutex);
2775 __close_session(mdsc, session);
2776 mutex_unlock(&session->s_mutex);
2777 ceph_put_mds_session(session);
2778 mutex_lock(&mdsc->mutex);
2784 if (client->mount_state == CEPH_MOUNT_SHUTDOWN)
2787 dout("waiting for sessions to close\n");
2788 mutex_unlock(&mdsc->mutex);
2789 wait_for_completion_timeout(&mdsc->session_close_waiters,
2791 mutex_lock(&mdsc->mutex);
2794 /* tear down remaining sessions */
2795 for (i = 0; i < mdsc->max_sessions; i++) {
2796 if (mdsc->sessions[i]) {
2797 session = get_session(mdsc->sessions[i]);
2798 __unregister_session(mdsc, session);
2799 mutex_unlock(&mdsc->mutex);
2800 mutex_lock(&session->s_mutex);
2801 remove_session_caps(session);
2802 mutex_unlock(&session->s_mutex);
2803 ceph_put_mds_session(session);
2804 mutex_lock(&mdsc->mutex);
2808 WARN_ON(!list_empty(&mdsc->cap_delay_list));
2810 mutex_unlock(&mdsc->mutex);
2812 ceph_cleanup_empty_realms(mdsc);
2814 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
2819 void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
2822 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
2824 ceph_mdsmap_destroy(mdsc->mdsmap);
2825 kfree(mdsc->sessions);
2830 * handle mds map update.
2832 void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
2836 void *p = msg->front.iov_base;
2837 void *end = p + msg->front.iov_len;
2838 struct ceph_mdsmap *newmap, *oldmap;
2839 struct ceph_fsid fsid;
2842 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
2843 ceph_decode_copy(&p, &fsid, sizeof(fsid));
2844 if (ceph_check_fsid(mdsc->client, &fsid) < 0)
2846 epoch = ceph_decode_32(&p);
2847 maplen = ceph_decode_32(&p);
2848 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
2850 /* do we need it? */
2851 ceph_monc_got_mdsmap(&mdsc->client->monc, epoch);
2852 mutex_lock(&mdsc->mutex);
2853 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
2854 dout("handle_map epoch %u <= our %u\n",
2855 epoch, mdsc->mdsmap->m_epoch);
2856 mutex_unlock(&mdsc->mutex);
2860 newmap = ceph_mdsmap_decode(&p, end);
2861 if (IS_ERR(newmap)) {
2862 err = PTR_ERR(newmap);
2866 /* swap into place */
2868 oldmap = mdsc->mdsmap;
2869 mdsc->mdsmap = newmap;
2870 check_new_map(mdsc, newmap, oldmap);
2871 ceph_mdsmap_destroy(oldmap);
2873 mdsc->mdsmap = newmap; /* first mds map */
2875 mdsc->client->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
2877 __wake_requests(mdsc, &mdsc->waiting_for_map);
2879 mutex_unlock(&mdsc->mutex);
2880 schedule_delayed(mdsc);
2884 mutex_unlock(&mdsc->mutex);
2886 pr_err("error decoding mdsmap %d\n", err);
2890 static struct ceph_connection *con_get(struct ceph_connection *con)
2892 struct ceph_mds_session *s = con->private;
2894 if (get_session(s)) {
2895 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
2898 dout("mdsc con_get %p FAIL\n", s);
2902 static void con_put(struct ceph_connection *con)
2904 struct ceph_mds_session *s = con->private;
2906 ceph_put_mds_session(s);
2907 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
2911 * if the client is unresponsive for long enough, the mds will kill
2912 * the session entirely.
2914 static void peer_reset(struct ceph_connection *con)
2916 struct ceph_mds_session *s = con->private;
2918 pr_err("mds%d gave us the boot. IMPLEMENT RECONNECT.\n",
2922 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2924 struct ceph_mds_session *s = con->private;
2925 struct ceph_mds_client *mdsc = s->s_mdsc;
2926 int type = le16_to_cpu(msg->hdr.type);
2928 mutex_lock(&mdsc->mutex);
2929 if (__verify_registered_session(mdsc, s) < 0) {
2930 mutex_unlock(&mdsc->mutex);
2933 mutex_unlock(&mdsc->mutex);
2936 case CEPH_MSG_MDS_MAP:
2937 ceph_mdsc_handle_map(mdsc, msg);
2939 case CEPH_MSG_CLIENT_SESSION:
2940 handle_session(s, msg);
2942 case CEPH_MSG_CLIENT_REPLY:
2943 handle_reply(s, msg);
2945 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
2946 handle_forward(mdsc, s, msg);
2948 case CEPH_MSG_CLIENT_CAPS:
2949 ceph_handle_caps(s, msg);
2951 case CEPH_MSG_CLIENT_SNAP:
2952 ceph_handle_snap(mdsc, s, msg);
2954 case CEPH_MSG_CLIENT_LEASE:
2955 handle_lease(mdsc, s, msg);
2959 pr_err("received unknown message type %d %s\n", type,
2960 ceph_msg_type_name(type));
2969 static int get_authorizer(struct ceph_connection *con,
2970 void **buf, int *len, int *proto,
2971 void **reply_buf, int *reply_len, int force_new)
2973 struct ceph_mds_session *s = con->private;
2974 struct ceph_mds_client *mdsc = s->s_mdsc;
2975 struct ceph_auth_client *ac = mdsc->client->monc.auth;
2978 if (force_new && s->s_authorizer) {
2979 ac->ops->destroy_authorizer(ac, s->s_authorizer);
2980 s->s_authorizer = NULL;
2982 if (s->s_authorizer == NULL) {
2983 if (ac->ops->create_authorizer) {
2984 ret = ac->ops->create_authorizer(
2985 ac, CEPH_ENTITY_TYPE_MDS,
2987 &s->s_authorizer_buf,
2988 &s->s_authorizer_buf_len,
2989 &s->s_authorizer_reply_buf,
2990 &s->s_authorizer_reply_buf_len);
2996 *proto = ac->protocol;
2997 *buf = s->s_authorizer_buf;
2998 *len = s->s_authorizer_buf_len;
2999 *reply_buf = s->s_authorizer_reply_buf;
3000 *reply_len = s->s_authorizer_reply_buf_len;
3005 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3007 struct ceph_mds_session *s = con->private;
3008 struct ceph_mds_client *mdsc = s->s_mdsc;
3009 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3011 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3014 static int invalidate_authorizer(struct ceph_connection *con)
3016 struct ceph_mds_session *s = con->private;
3017 struct ceph_mds_client *mdsc = s->s_mdsc;
3018 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3020 if (ac->ops->invalidate_authorizer)
3021 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3023 return ceph_monc_validate_auth(&mdsc->client->monc);
3026 const static struct ceph_connection_operations mds_con_ops = {
3029 .dispatch = dispatch,
3030 .get_authorizer = get_authorizer,
3031 .verify_authorizer_reply = verify_authorizer_reply,
3032 .invalidate_authorizer = invalidate_authorizer,
3033 .peer_reset = peer_reset,