1 #include <linux/ceph/ceph_debug.h>
4 #include <linux/wait.h>
5 #include <linux/slab.h>
6 #include <linux/sched.h>
7 #include <linux/debugfs.h>
8 #include <linux/seq_file.h>
11 #include "mds_client.h"
13 #include <linux/ceph/ceph_features.h>
14 #include <linux/ceph/messenger.h>
15 #include <linux/ceph/decode.h>
16 #include <linux/ceph/pagelist.h>
17 #include <linux/ceph/auth.h>
18 #include <linux/ceph/debugfs.h>
21 * A cluster of MDS (metadata server) daemons is responsible for
22 * managing the file system namespace (the directory hierarchy and
23 * inodes) and for coordinating shared access to storage. Metadata is
24 * partitioning hierarchically across a number of servers, and that
25 * partition varies over time as the cluster adjusts the distribution
26 * in order to balance load.
28 * The MDS client is primarily responsible to managing synchronous
29 * metadata requests for operations like open, unlink, and so forth.
30 * If there is a MDS failure, we find out about it when we (possibly
31 * request and) receive a new MDS map, and can resubmit affected
34 * For the most part, though, we take advantage of a lossless
35 * communications channel to the MDS, and do not need to worry about
36 * timing out or resubmitting requests.
38 * We maintain a stateful "session" with each MDS we interact with.
39 * Within each session, we sent periodic heartbeat messages to ensure
40 * any capabilities or leases we have been issues remain valid. If
41 * the session times out and goes stale, our leases and capabilities
42 * are no longer valid.
45 struct ceph_reconnect_state {
47 struct ceph_pagelist *pagelist;
51 static void __wake_requests(struct ceph_mds_client *mdsc,
52 struct list_head *head);
54 static const struct ceph_connection_operations mds_con_ops;
62 * parse individual inode info
64 static int parse_reply_info_in(void **p, void *end,
65 struct ceph_mds_reply_info_in *info,
71 *p += sizeof(struct ceph_mds_reply_inode) +
72 sizeof(*info->in->fragtree.splits) *
73 le32_to_cpu(info->in->fragtree.nsplits);
75 ceph_decode_32_safe(p, end, info->symlink_len, bad);
76 ceph_decode_need(p, end, info->symlink_len, bad);
78 *p += info->symlink_len;
80 if (features & CEPH_FEATURE_DIRLAYOUTHASH)
81 ceph_decode_copy_safe(p, end, &info->dir_layout,
82 sizeof(info->dir_layout), bad);
84 memset(&info->dir_layout, 0, sizeof(info->dir_layout));
86 ceph_decode_32_safe(p, end, info->xattr_len, bad);
87 ceph_decode_need(p, end, info->xattr_len, bad);
88 info->xattr_data = *p;
89 *p += info->xattr_len;
96 * parse a normal reply, which may contain a (dir+)dentry and/or a
99 static int parse_reply_info_trace(void **p, void *end,
100 struct ceph_mds_reply_info_parsed *info,
105 if (info->head->is_dentry) {
106 err = parse_reply_info_in(p, end, &info->diri, features);
110 if (unlikely(*p + sizeof(*info->dirfrag) > end))
113 *p += sizeof(*info->dirfrag) +
114 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
115 if (unlikely(*p > end))
118 ceph_decode_32_safe(p, end, info->dname_len, bad);
119 ceph_decode_need(p, end, info->dname_len, bad);
121 *p += info->dname_len;
123 *p += sizeof(*info->dlease);
126 if (info->head->is_target) {
127 err = parse_reply_info_in(p, end, &info->targeti, features);
132 if (unlikely(*p != end))
139 pr_err("problem parsing mds trace %d\n", err);
144 * parse readdir results
146 static int parse_reply_info_dir(void **p, void *end,
147 struct ceph_mds_reply_info_parsed *info,
154 if (*p + sizeof(*info->dir_dir) > end)
156 *p += sizeof(*info->dir_dir) +
157 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
161 ceph_decode_need(p, end, sizeof(num) + 2, bad);
162 num = ceph_decode_32(p);
163 info->dir_end = ceph_decode_8(p);
164 info->dir_complete = ceph_decode_8(p);
168 /* alloc large array */
170 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
171 sizeof(*info->dir_dname) +
172 sizeof(*info->dir_dname_len) +
173 sizeof(*info->dir_dlease),
175 if (info->dir_in == NULL) {
179 info->dir_dname = (void *)(info->dir_in + num);
180 info->dir_dname_len = (void *)(info->dir_dname + num);
181 info->dir_dlease = (void *)(info->dir_dname_len + num);
185 ceph_decode_need(p, end, sizeof(u32)*2, bad);
186 info->dir_dname_len[i] = ceph_decode_32(p);
187 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
188 info->dir_dname[i] = *p;
189 *p += info->dir_dname_len[i];
190 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
192 info->dir_dlease[i] = *p;
193 *p += sizeof(struct ceph_mds_reply_lease);
196 err = parse_reply_info_in(p, end, &info->dir_in[i], features);
211 pr_err("problem parsing dir contents %d\n", err);
216 * parse fcntl F_GETLK results
218 static int parse_reply_info_filelock(void **p, void *end,
219 struct ceph_mds_reply_info_parsed *info,
222 if (*p + sizeof(*info->filelock_reply) > end)
225 info->filelock_reply = *p;
226 *p += sizeof(*info->filelock_reply);
228 if (unlikely(*p != end))
237 * parse create results
239 static int parse_reply_info_create(void **p, void *end,
240 struct ceph_mds_reply_info_parsed *info,
243 if (features & CEPH_FEATURE_REPLY_CREATE_INODE) {
245 info->has_create_ino = false;
247 info->has_create_ino = true;
248 info->ino = ceph_decode_64(p);
252 if (unlikely(*p != end))
261 * parse extra results
263 static int parse_reply_info_extra(void **p, void *end,
264 struct ceph_mds_reply_info_parsed *info,
267 if (info->head->op == CEPH_MDS_OP_GETFILELOCK)
268 return parse_reply_info_filelock(p, end, info, features);
269 else if (info->head->op == CEPH_MDS_OP_READDIR ||
270 info->head->op == CEPH_MDS_OP_LSSNAP)
271 return parse_reply_info_dir(p, end, info, features);
272 else if (info->head->op == CEPH_MDS_OP_CREATE)
273 return parse_reply_info_create(p, end, info, features);
279 * parse entire mds reply
281 static int parse_reply_info(struct ceph_msg *msg,
282 struct ceph_mds_reply_info_parsed *info,
289 info->head = msg->front.iov_base;
290 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
291 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
294 ceph_decode_32_safe(&p, end, len, bad);
296 ceph_decode_need(&p, end, len, bad);
297 err = parse_reply_info_trace(&p, p+len, info, features);
303 ceph_decode_32_safe(&p, end, len, bad);
305 ceph_decode_need(&p, end, len, bad);
306 err = parse_reply_info_extra(&p, p+len, info, features);
312 ceph_decode_32_safe(&p, end, len, bad);
313 info->snapblob_len = len;
324 pr_err("mds parse_reply err %d\n", err);
328 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
337 static const char *session_state_name(int s)
340 case CEPH_MDS_SESSION_NEW: return "new";
341 case CEPH_MDS_SESSION_OPENING: return "opening";
342 case CEPH_MDS_SESSION_OPEN: return "open";
343 case CEPH_MDS_SESSION_HUNG: return "hung";
344 case CEPH_MDS_SESSION_CLOSING: return "closing";
345 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
346 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
347 default: return "???";
351 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
353 if (atomic_inc_not_zero(&s->s_ref)) {
354 dout("mdsc get_session %p %d -> %d\n", s,
355 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
358 dout("mdsc get_session %p 0 -- FAIL", s);
363 void ceph_put_mds_session(struct ceph_mds_session *s)
365 dout("mdsc put_session %p %d -> %d\n", s,
366 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
367 if (atomic_dec_and_test(&s->s_ref)) {
368 if (s->s_auth.authorizer)
369 ceph_auth_destroy_authorizer(
370 s->s_mdsc->fsc->client->monc.auth,
371 s->s_auth.authorizer);
377 * called under mdsc->mutex
379 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
382 struct ceph_mds_session *session;
384 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
386 session = mdsc->sessions[mds];
387 dout("lookup_mds_session %p %d\n", session,
388 atomic_read(&session->s_ref));
389 get_session(session);
393 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
395 if (mds >= mdsc->max_sessions)
397 return mdsc->sessions[mds];
400 static int __verify_registered_session(struct ceph_mds_client *mdsc,
401 struct ceph_mds_session *s)
403 if (s->s_mds >= mdsc->max_sessions ||
404 mdsc->sessions[s->s_mds] != s)
410 * create+register a new session for given mds.
411 * called under mdsc->mutex.
413 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
416 struct ceph_mds_session *s;
418 if (mds >= mdsc->mdsmap->m_max_mds)
419 return ERR_PTR(-EINVAL);
421 s = kzalloc(sizeof(*s), GFP_NOFS);
423 return ERR_PTR(-ENOMEM);
426 s->s_state = CEPH_MDS_SESSION_NEW;
429 mutex_init(&s->s_mutex);
431 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
433 spin_lock_init(&s->s_gen_ttl_lock);
435 s->s_cap_ttl = jiffies - 1;
437 spin_lock_init(&s->s_cap_lock);
438 s->s_renew_requested = 0;
440 INIT_LIST_HEAD(&s->s_caps);
443 atomic_set(&s->s_ref, 1);
444 INIT_LIST_HEAD(&s->s_waiting);
445 INIT_LIST_HEAD(&s->s_unsafe);
446 s->s_num_cap_releases = 0;
447 s->s_cap_reconnect = 0;
448 s->s_cap_iterator = NULL;
449 INIT_LIST_HEAD(&s->s_cap_releases);
450 INIT_LIST_HEAD(&s->s_cap_releases_done);
451 INIT_LIST_HEAD(&s->s_cap_flushing);
452 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
454 dout("register_session mds%d\n", mds);
455 if (mds >= mdsc->max_sessions) {
456 int newmax = 1 << get_count_order(mds+1);
457 struct ceph_mds_session **sa;
459 dout("register_session realloc to %d\n", newmax);
460 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
463 if (mdsc->sessions) {
464 memcpy(sa, mdsc->sessions,
465 mdsc->max_sessions * sizeof(void *));
466 kfree(mdsc->sessions);
469 mdsc->max_sessions = newmax;
471 mdsc->sessions[mds] = s;
472 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
474 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
475 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
481 return ERR_PTR(-ENOMEM);
485 * called under mdsc->mutex
487 static void __unregister_session(struct ceph_mds_client *mdsc,
488 struct ceph_mds_session *s)
490 dout("__unregister_session mds%d %p\n", s->s_mds, s);
491 BUG_ON(mdsc->sessions[s->s_mds] != s);
492 mdsc->sessions[s->s_mds] = NULL;
493 ceph_con_close(&s->s_con);
494 ceph_put_mds_session(s);
498 * drop session refs in request.
500 * should be last request ref, or hold mdsc->mutex
502 static void put_request_session(struct ceph_mds_request *req)
504 if (req->r_session) {
505 ceph_put_mds_session(req->r_session);
506 req->r_session = NULL;
510 void ceph_mdsc_release_request(struct kref *kref)
512 struct ceph_mds_request *req = container_of(kref,
513 struct ceph_mds_request,
516 ceph_msg_put(req->r_request);
518 ceph_msg_put(req->r_reply);
519 destroy_reply_info(&req->r_reply_info);
522 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
525 if (req->r_locked_dir)
526 ceph_put_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
527 if (req->r_target_inode)
528 iput(req->r_target_inode);
531 if (req->r_old_dentry)
532 dput(req->r_old_dentry);
533 if (req->r_old_dentry_dir) {
535 * track (and drop pins for) r_old_dentry_dir
536 * separately, since r_old_dentry's d_parent may have
537 * changed between the dir mutex being dropped and
538 * this request being freed.
540 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
542 iput(req->r_old_dentry_dir);
546 put_request_session(req);
547 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
552 * lookup session, bump ref if found.
554 * called under mdsc->mutex.
556 static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
559 struct ceph_mds_request *req;
560 struct rb_node *n = mdsc->request_tree.rb_node;
563 req = rb_entry(n, struct ceph_mds_request, r_node);
564 if (tid < req->r_tid)
566 else if (tid > req->r_tid)
569 ceph_mdsc_get_request(req);
576 static void __insert_request(struct ceph_mds_client *mdsc,
577 struct ceph_mds_request *new)
579 struct rb_node **p = &mdsc->request_tree.rb_node;
580 struct rb_node *parent = NULL;
581 struct ceph_mds_request *req = NULL;
585 req = rb_entry(parent, struct ceph_mds_request, r_node);
586 if (new->r_tid < req->r_tid)
588 else if (new->r_tid > req->r_tid)
594 rb_link_node(&new->r_node, parent, p);
595 rb_insert_color(&new->r_node, &mdsc->request_tree);
599 * Register an in-flight request, and assign a tid. Link to directory
600 * are modifying (if any).
602 * Called under mdsc->mutex.
604 static void __register_request(struct ceph_mds_client *mdsc,
605 struct ceph_mds_request *req,
608 req->r_tid = ++mdsc->last_tid;
610 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
612 dout("__register_request %p tid %lld\n", req, req->r_tid);
613 ceph_mdsc_get_request(req);
614 __insert_request(mdsc, req);
616 req->r_uid = current_fsuid();
617 req->r_gid = current_fsgid();
620 struct ceph_inode_info *ci = ceph_inode(dir);
623 spin_lock(&ci->i_unsafe_lock);
624 req->r_unsafe_dir = dir;
625 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
626 spin_unlock(&ci->i_unsafe_lock);
630 static void __unregister_request(struct ceph_mds_client *mdsc,
631 struct ceph_mds_request *req)
633 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
634 rb_erase(&req->r_node, &mdsc->request_tree);
635 RB_CLEAR_NODE(&req->r_node);
637 if (req->r_unsafe_dir) {
638 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
640 spin_lock(&ci->i_unsafe_lock);
641 list_del_init(&req->r_unsafe_dir_item);
642 spin_unlock(&ci->i_unsafe_lock);
644 iput(req->r_unsafe_dir);
645 req->r_unsafe_dir = NULL;
648 complete_all(&req->r_safe_completion);
650 ceph_mdsc_put_request(req);
654 * Choose mds to send request to next. If there is a hint set in the
655 * request (e.g., due to a prior forward hint from the mds), use that.
656 * Otherwise, consult frag tree and/or caps to identify the
657 * appropriate mds. If all else fails, choose randomly.
659 * Called under mdsc->mutex.
661 static struct dentry *get_nonsnap_parent(struct dentry *dentry)
664 * we don't need to worry about protecting the d_parent access
665 * here because we never renaming inside the snapped namespace
666 * except to resplice to another snapdir, and either the old or new
667 * result is a valid result.
669 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
670 dentry = dentry->d_parent;
674 static int __choose_mds(struct ceph_mds_client *mdsc,
675 struct ceph_mds_request *req)
678 struct ceph_inode_info *ci;
679 struct ceph_cap *cap;
680 int mode = req->r_direct_mode;
682 u32 hash = req->r_direct_hash;
683 bool is_hash = req->r_direct_is_hash;
686 * is there a specific mds we should try? ignore hint if we have
687 * no session and the mds is not up (active or recovering).
689 if (req->r_resend_mds >= 0 &&
690 (__have_session(mdsc, req->r_resend_mds) ||
691 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
692 dout("choose_mds using resend_mds mds%d\n",
694 return req->r_resend_mds;
697 if (mode == USE_RANDOM_MDS)
702 inode = req->r_inode;
703 } else if (req->r_dentry) {
704 /* ignore race with rename; old or new d_parent is okay */
705 struct dentry *parent = req->r_dentry->d_parent;
706 struct inode *dir = parent->d_inode;
708 if (dir->i_sb != mdsc->fsc->sb) {
710 inode = req->r_dentry->d_inode;
711 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
712 /* direct snapped/virtual snapdir requests
713 * based on parent dir inode */
714 struct dentry *dn = get_nonsnap_parent(parent);
716 dout("__choose_mds using nonsnap parent %p\n", inode);
719 inode = req->r_dentry->d_inode;
720 if (!inode || mode == USE_AUTH_MDS) {
723 hash = ceph_dentry_hash(dir, req->r_dentry);
729 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
733 ci = ceph_inode(inode);
735 if (is_hash && S_ISDIR(inode->i_mode)) {
736 struct ceph_inode_frag frag;
739 ceph_choose_frag(ci, hash, &frag, &found);
741 if (mode == USE_ANY_MDS && frag.ndist > 0) {
744 /* choose a random replica */
745 get_random_bytes(&r, 1);
748 dout("choose_mds %p %llx.%llx "
749 "frag %u mds%d (%d/%d)\n",
750 inode, ceph_vinop(inode),
753 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
754 CEPH_MDS_STATE_ACTIVE)
758 /* since this file/dir wasn't known to be
759 * replicated, then we want to look for the
760 * authoritative mds. */
763 /* choose auth mds */
765 dout("choose_mds %p %llx.%llx "
766 "frag %u mds%d (auth)\n",
767 inode, ceph_vinop(inode), frag.frag, mds);
768 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
769 CEPH_MDS_STATE_ACTIVE)
775 spin_lock(&ci->i_ceph_lock);
777 if (mode == USE_AUTH_MDS)
778 cap = ci->i_auth_cap;
779 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
780 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
782 spin_unlock(&ci->i_ceph_lock);
785 mds = cap->session->s_mds;
786 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
787 inode, ceph_vinop(inode), mds,
788 cap == ci->i_auth_cap ? "auth " : "", cap);
789 spin_unlock(&ci->i_ceph_lock);
793 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
794 dout("choose_mds chose random mds%d\n", mds);
802 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
804 struct ceph_msg *msg;
805 struct ceph_mds_session_head *h;
807 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
810 pr_err("create_session_msg ENOMEM creating msg\n");
813 h = msg->front.iov_base;
814 h->op = cpu_to_le32(op);
815 h->seq = cpu_to_le64(seq);
820 * send session open request.
822 * called under mdsc->mutex
824 static int __open_session(struct ceph_mds_client *mdsc,
825 struct ceph_mds_session *session)
827 struct ceph_msg *msg;
829 int mds = session->s_mds;
831 /* wait for mds to go active? */
832 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
833 dout("open_session to mds%d (%s)\n", mds,
834 ceph_mds_state_name(mstate));
835 session->s_state = CEPH_MDS_SESSION_OPENING;
836 session->s_renew_requested = jiffies;
838 /* send connect message */
839 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
842 ceph_con_send(&session->s_con, msg);
847 * open sessions for any export targets for the given mds
849 * called under mdsc->mutex
851 static struct ceph_mds_session *
852 __open_export_target_session(struct ceph_mds_client *mdsc, int target)
854 struct ceph_mds_session *session;
856 session = __ceph_lookup_mds_session(mdsc, target);
858 session = register_session(mdsc, target);
862 if (session->s_state == CEPH_MDS_SESSION_NEW ||
863 session->s_state == CEPH_MDS_SESSION_CLOSING)
864 __open_session(mdsc, session);
869 struct ceph_mds_session *
870 ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
872 struct ceph_mds_session *session;
874 dout("open_export_target_session to mds%d\n", target);
876 mutex_lock(&mdsc->mutex);
877 session = __open_export_target_session(mdsc, target);
878 mutex_unlock(&mdsc->mutex);
883 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
884 struct ceph_mds_session *session)
886 struct ceph_mds_info *mi;
887 struct ceph_mds_session *ts;
888 int i, mds = session->s_mds;
890 if (mds >= mdsc->mdsmap->m_max_mds)
893 mi = &mdsc->mdsmap->m_info[mds];
894 dout("open_export_target_sessions for mds%d (%d targets)\n",
895 session->s_mds, mi->num_export_targets);
897 for (i = 0; i < mi->num_export_targets; i++) {
898 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
900 ceph_put_mds_session(ts);
904 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
905 struct ceph_mds_session *session)
907 mutex_lock(&mdsc->mutex);
908 __open_export_target_sessions(mdsc, session);
909 mutex_unlock(&mdsc->mutex);
917 * Free preallocated cap messages assigned to this session
919 static void cleanup_cap_releases(struct ceph_mds_session *session)
921 struct ceph_msg *msg;
923 spin_lock(&session->s_cap_lock);
924 while (!list_empty(&session->s_cap_releases)) {
925 msg = list_first_entry(&session->s_cap_releases,
926 struct ceph_msg, list_head);
927 list_del_init(&msg->list_head);
930 while (!list_empty(&session->s_cap_releases_done)) {
931 msg = list_first_entry(&session->s_cap_releases_done,
932 struct ceph_msg, list_head);
933 list_del_init(&msg->list_head);
936 spin_unlock(&session->s_cap_lock);
940 * Helper to safely iterate over all caps associated with a session, with
941 * special care taken to handle a racing __ceph_remove_cap().
943 * Caller must hold session s_mutex.
945 static int iterate_session_caps(struct ceph_mds_session *session,
946 int (*cb)(struct inode *, struct ceph_cap *,
950 struct ceph_cap *cap;
951 struct inode *inode, *last_inode = NULL;
952 struct ceph_cap *old_cap = NULL;
955 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
956 spin_lock(&session->s_cap_lock);
957 p = session->s_caps.next;
958 while (p != &session->s_caps) {
959 cap = list_entry(p, struct ceph_cap, session_caps);
960 inode = igrab(&cap->ci->vfs_inode);
965 session->s_cap_iterator = cap;
966 spin_unlock(&session->s_cap_lock);
973 ceph_put_cap(session->s_mdsc, old_cap);
977 ret = cb(inode, cap, arg);
980 spin_lock(&session->s_cap_lock);
982 if (cap->ci == NULL) {
983 dout("iterate_session_caps finishing cap %p removal\n",
985 BUG_ON(cap->session != session);
986 list_del_init(&cap->session_caps);
987 session->s_nr_caps--;
989 old_cap = cap; /* put_cap it w/o locks held */
996 session->s_cap_iterator = NULL;
997 spin_unlock(&session->s_cap_lock);
1002 ceph_put_cap(session->s_mdsc, old_cap);
1007 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
1010 struct ceph_inode_info *ci = ceph_inode(inode);
1013 dout("removing cap %p, ci is %p, inode is %p\n",
1014 cap, ci, &ci->vfs_inode);
1015 spin_lock(&ci->i_ceph_lock);
1016 __ceph_remove_cap(cap, false);
1017 if (!__ceph_is_any_real_caps(ci)) {
1018 struct ceph_mds_client *mdsc =
1019 ceph_sb_to_client(inode->i_sb)->mdsc;
1021 spin_lock(&mdsc->cap_dirty_lock);
1022 if (!list_empty(&ci->i_dirty_item)) {
1023 pr_info(" dropping dirty %s state for %p %lld\n",
1024 ceph_cap_string(ci->i_dirty_caps),
1025 inode, ceph_ino(inode));
1026 ci->i_dirty_caps = 0;
1027 list_del_init(&ci->i_dirty_item);
1030 if (!list_empty(&ci->i_flushing_item)) {
1031 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
1032 ceph_cap_string(ci->i_flushing_caps),
1033 inode, ceph_ino(inode));
1034 ci->i_flushing_caps = 0;
1035 list_del_init(&ci->i_flushing_item);
1036 mdsc->num_cap_flushing--;
1039 if (drop && ci->i_wrbuffer_ref) {
1040 pr_info(" dropping dirty data for %p %lld\n",
1041 inode, ceph_ino(inode));
1042 ci->i_wrbuffer_ref = 0;
1043 ci->i_wrbuffer_ref_head = 0;
1046 spin_unlock(&mdsc->cap_dirty_lock);
1048 spin_unlock(&ci->i_ceph_lock);
1055 * caller must hold session s_mutex
1057 static void remove_session_caps(struct ceph_mds_session *session)
1059 dout("remove_session_caps on %p\n", session);
1060 iterate_session_caps(session, remove_session_caps_cb, NULL);
1062 spin_lock(&session->s_cap_lock);
1063 if (session->s_nr_caps > 0) {
1064 struct super_block *sb = session->s_mdsc->fsc->sb;
1065 struct inode *inode;
1066 struct ceph_cap *cap, *prev = NULL;
1067 struct ceph_vino vino;
1069 * iterate_session_caps() skips inodes that are being
1070 * deleted, we need to wait until deletions are complete.
1071 * __wait_on_freeing_inode() is designed for the job,
1072 * but it is not exported, so use lookup inode function
1075 while (!list_empty(&session->s_caps)) {
1076 cap = list_entry(session->s_caps.next,
1077 struct ceph_cap, session_caps);
1081 vino = cap->ci->i_vino;
1082 spin_unlock(&session->s_cap_lock);
1084 inode = ceph_find_inode(sb, vino);
1087 spin_lock(&session->s_cap_lock);
1090 spin_unlock(&session->s_cap_lock);
1092 BUG_ON(session->s_nr_caps > 0);
1093 BUG_ON(!list_empty(&session->s_cap_flushing));
1094 cleanup_cap_releases(session);
1098 * wake up any threads waiting on this session's caps. if the cap is
1099 * old (didn't get renewed on the client reconnect), remove it now.
1101 * caller must hold s_mutex.
1103 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1106 struct ceph_inode_info *ci = ceph_inode(inode);
1108 wake_up_all(&ci->i_cap_wq);
1110 spin_lock(&ci->i_ceph_lock);
1111 ci->i_wanted_max_size = 0;
1112 ci->i_requested_max_size = 0;
1113 spin_unlock(&ci->i_ceph_lock);
1118 static void wake_up_session_caps(struct ceph_mds_session *session,
1121 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1122 iterate_session_caps(session, wake_up_session_cb,
1123 (void *)(unsigned long)reconnect);
1127 * Send periodic message to MDS renewing all currently held caps. The
1128 * ack will reset the expiration for all caps from this session.
1130 * caller holds s_mutex
1132 static int send_renew_caps(struct ceph_mds_client *mdsc,
1133 struct ceph_mds_session *session)
1135 struct ceph_msg *msg;
1138 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1139 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1140 pr_info("mds%d caps stale\n", session->s_mds);
1141 session->s_renew_requested = jiffies;
1143 /* do not try to renew caps until a recovering mds has reconnected
1144 * with its clients. */
1145 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1146 if (state < CEPH_MDS_STATE_RECONNECT) {
1147 dout("send_renew_caps ignoring mds%d (%s)\n",
1148 session->s_mds, ceph_mds_state_name(state));
1152 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1153 ceph_mds_state_name(state));
1154 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1155 ++session->s_renew_seq);
1158 ceph_con_send(&session->s_con, msg);
1162 static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1163 struct ceph_mds_session *session, u64 seq)
1165 struct ceph_msg *msg;
1167 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1168 session->s_mds, session_state_name(session->s_state), seq);
1169 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1172 ceph_con_send(&session->s_con, msg);
1178 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1180 * Called under session->s_mutex
1182 static void renewed_caps(struct ceph_mds_client *mdsc,
1183 struct ceph_mds_session *session, int is_renew)
1188 spin_lock(&session->s_cap_lock);
1189 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
1191 session->s_cap_ttl = session->s_renew_requested +
1192 mdsc->mdsmap->m_session_timeout*HZ;
1195 if (time_before(jiffies, session->s_cap_ttl)) {
1196 pr_info("mds%d caps renewed\n", session->s_mds);
1199 pr_info("mds%d caps still stale\n", session->s_mds);
1202 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1203 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1204 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1205 spin_unlock(&session->s_cap_lock);
1208 wake_up_session_caps(session, 0);
1212 * send a session close request
1214 static int request_close_session(struct ceph_mds_client *mdsc,
1215 struct ceph_mds_session *session)
1217 struct ceph_msg *msg;
1219 dout("request_close_session mds%d state %s seq %lld\n",
1220 session->s_mds, session_state_name(session->s_state),
1222 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1225 ceph_con_send(&session->s_con, msg);
1230 * Called with s_mutex held.
1232 static int __close_session(struct ceph_mds_client *mdsc,
1233 struct ceph_mds_session *session)
1235 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1237 session->s_state = CEPH_MDS_SESSION_CLOSING;
1238 return request_close_session(mdsc, session);
1242 * Trim old(er) caps.
1244 * Because we can't cache an inode without one or more caps, we do
1245 * this indirectly: if a cap is unused, we prune its aliases, at which
1246 * point the inode will hopefully get dropped to.
1248 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1249 * memory pressure from the MDS, though, so it needn't be perfect.
1251 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1253 struct ceph_mds_session *session = arg;
1254 struct ceph_inode_info *ci = ceph_inode(inode);
1255 int used, wanted, oissued, mine;
1257 if (session->s_trim_caps <= 0)
1260 spin_lock(&ci->i_ceph_lock);
1261 mine = cap->issued | cap->implemented;
1262 used = __ceph_caps_used(ci);
1263 wanted = __ceph_caps_file_wanted(ci);
1264 oissued = __ceph_caps_issued_other(ci, cap);
1266 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1267 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1268 ceph_cap_string(used), ceph_cap_string(wanted));
1269 if (cap == ci->i_auth_cap) {
1270 if (ci->i_dirty_caps | ci->i_flushing_caps)
1272 if ((used | wanted) & CEPH_CAP_ANY_WR)
1275 if ((used | wanted) & ~oissued & mine)
1276 goto out; /* we need these caps */
1278 session->s_trim_caps--;
1280 /* we aren't the only cap.. just remove us */
1281 __ceph_remove_cap(cap, true);
1283 /* try to drop referring dentries */
1284 spin_unlock(&ci->i_ceph_lock);
1285 d_prune_aliases(inode);
1286 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1287 inode, cap, atomic_read(&inode->i_count));
1292 spin_unlock(&ci->i_ceph_lock);
1297 * Trim session cap count down to some max number.
1299 static int trim_caps(struct ceph_mds_client *mdsc,
1300 struct ceph_mds_session *session,
1303 int trim_caps = session->s_nr_caps - max_caps;
1305 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1306 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1307 if (trim_caps > 0) {
1308 session->s_trim_caps = trim_caps;
1309 iterate_session_caps(session, trim_caps_cb, session);
1310 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1311 session->s_mds, session->s_nr_caps, max_caps,
1312 trim_caps - session->s_trim_caps);
1313 session->s_trim_caps = 0;
1319 * Allocate cap_release messages. If there is a partially full message
1320 * in the queue, try to allocate enough to cover it's remainder, so that
1321 * we can send it immediately.
1323 * Called under s_mutex.
1325 int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
1326 struct ceph_mds_session *session)
1328 struct ceph_msg *msg, *partial = NULL;
1329 struct ceph_mds_cap_release *head;
1331 int extra = mdsc->fsc->mount_options->cap_release_safety;
1334 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1337 spin_lock(&session->s_cap_lock);
1339 if (!list_empty(&session->s_cap_releases)) {
1340 msg = list_first_entry(&session->s_cap_releases,
1343 head = msg->front.iov_base;
1344 num = le32_to_cpu(head->num);
1346 dout(" partial %p with (%d/%d)\n", msg, num,
1347 (int)CEPH_CAPS_PER_RELEASE);
1348 extra += CEPH_CAPS_PER_RELEASE - num;
1352 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1353 spin_unlock(&session->s_cap_lock);
1354 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1358 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1359 (int)msg->front.iov_len);
1360 head = msg->front.iov_base;
1361 head->num = cpu_to_le32(0);
1362 msg->front.iov_len = sizeof(*head);
1363 spin_lock(&session->s_cap_lock);
1364 list_add(&msg->list_head, &session->s_cap_releases);
1365 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1369 head = partial->front.iov_base;
1370 num = le32_to_cpu(head->num);
1371 dout(" queueing partial %p with %d/%d\n", partial, num,
1372 (int)CEPH_CAPS_PER_RELEASE);
1373 list_move_tail(&partial->list_head,
1374 &session->s_cap_releases_done);
1375 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
1378 spin_unlock(&session->s_cap_lock);
1384 * flush all dirty inode data to disk.
1386 * returns true if we've flushed through want_flush_seq
1388 static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1392 dout("check_cap_flush want %lld\n", want_flush_seq);
1393 mutex_lock(&mdsc->mutex);
1394 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1395 struct ceph_mds_session *session = mdsc->sessions[mds];
1399 get_session(session);
1400 mutex_unlock(&mdsc->mutex);
1402 mutex_lock(&session->s_mutex);
1403 if (!list_empty(&session->s_cap_flushing)) {
1404 struct ceph_inode_info *ci =
1405 list_entry(session->s_cap_flushing.next,
1406 struct ceph_inode_info,
1408 struct inode *inode = &ci->vfs_inode;
1410 spin_lock(&ci->i_ceph_lock);
1411 if (ci->i_cap_flush_seq <= want_flush_seq) {
1412 dout("check_cap_flush still flushing %p "
1413 "seq %lld <= %lld to mds%d\n", inode,
1414 ci->i_cap_flush_seq, want_flush_seq,
1418 spin_unlock(&ci->i_ceph_lock);
1420 mutex_unlock(&session->s_mutex);
1421 ceph_put_mds_session(session);
1425 mutex_lock(&mdsc->mutex);
1428 mutex_unlock(&mdsc->mutex);
1429 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1434 * called under s_mutex
1436 void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1437 struct ceph_mds_session *session)
1439 struct ceph_msg *msg;
1441 dout("send_cap_releases mds%d\n", session->s_mds);
1442 spin_lock(&session->s_cap_lock);
1443 while (!list_empty(&session->s_cap_releases_done)) {
1444 msg = list_first_entry(&session->s_cap_releases_done,
1445 struct ceph_msg, list_head);
1446 list_del_init(&msg->list_head);
1447 spin_unlock(&session->s_cap_lock);
1448 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1449 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1450 ceph_con_send(&session->s_con, msg);
1451 spin_lock(&session->s_cap_lock);
1453 spin_unlock(&session->s_cap_lock);
1456 static void discard_cap_releases(struct ceph_mds_client *mdsc,
1457 struct ceph_mds_session *session)
1459 struct ceph_msg *msg;
1460 struct ceph_mds_cap_release *head;
1463 dout("discard_cap_releases mds%d\n", session->s_mds);
1465 /* zero out the in-progress message */
1466 msg = list_first_entry(&session->s_cap_releases,
1467 struct ceph_msg, list_head);
1468 head = msg->front.iov_base;
1469 num = le32_to_cpu(head->num);
1470 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1471 head->num = cpu_to_le32(0);
1472 msg->front.iov_len = sizeof(*head);
1473 session->s_num_cap_releases += num;
1475 /* requeue completed messages */
1476 while (!list_empty(&session->s_cap_releases_done)) {
1477 msg = list_first_entry(&session->s_cap_releases_done,
1478 struct ceph_msg, list_head);
1479 list_del_init(&msg->list_head);
1481 head = msg->front.iov_base;
1482 num = le32_to_cpu(head->num);
1483 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1485 session->s_num_cap_releases += num;
1486 head->num = cpu_to_le32(0);
1487 msg->front.iov_len = sizeof(*head);
1488 list_add(&msg->list_head, &session->s_cap_releases);
1497 * Create an mds request.
1499 struct ceph_mds_request *
1500 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1502 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1505 return ERR_PTR(-ENOMEM);
1507 mutex_init(&req->r_fill_mutex);
1509 req->r_started = jiffies;
1510 req->r_resend_mds = -1;
1511 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1513 kref_init(&req->r_kref);
1514 INIT_LIST_HEAD(&req->r_wait);
1515 init_completion(&req->r_completion);
1516 init_completion(&req->r_safe_completion);
1517 INIT_LIST_HEAD(&req->r_unsafe_item);
1520 req->r_direct_mode = mode;
1525 * return oldest (lowest) request, tid in request tree, 0 if none.
1527 * called under mdsc->mutex.
1529 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1531 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1533 return rb_entry(rb_first(&mdsc->request_tree),
1534 struct ceph_mds_request, r_node);
1537 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1539 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1547 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1548 * on build_path_from_dentry in fs/cifs/dir.c.
1550 * If @stop_on_nosnap, generate path relative to the first non-snapped
1553 * Encode hidden .snap dirs as a double /, i.e.
1554 * foo/.snap/bar -> foo//bar
1556 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1559 struct dentry *temp;
1565 return ERR_PTR(-EINVAL);
1569 seq = read_seqbegin(&rename_lock);
1571 for (temp = dentry; !IS_ROOT(temp);) {
1572 struct inode *inode = temp->d_inode;
1573 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1574 len++; /* slash only */
1575 else if (stop_on_nosnap && inode &&
1576 ceph_snap(inode) == CEPH_NOSNAP)
1579 len += 1 + temp->d_name.len;
1580 temp = temp->d_parent;
1584 len--; /* no leading '/' */
1586 path = kmalloc(len+1, GFP_NOFS);
1588 return ERR_PTR(-ENOMEM);
1590 path[pos] = 0; /* trailing null */
1592 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1593 struct inode *inode;
1595 spin_lock(&temp->d_lock);
1596 inode = temp->d_inode;
1597 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1598 dout("build_path path+%d: %p SNAPDIR\n",
1600 } else if (stop_on_nosnap && inode &&
1601 ceph_snap(inode) == CEPH_NOSNAP) {
1602 spin_unlock(&temp->d_lock);
1605 pos -= temp->d_name.len;
1607 spin_unlock(&temp->d_lock);
1610 strncpy(path + pos, temp->d_name.name,
1613 spin_unlock(&temp->d_lock);
1616 temp = temp->d_parent;
1619 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
1620 pr_err("build_path did not end path lookup where "
1621 "expected, namelen is %d, pos is %d\n", len, pos);
1622 /* presumably this is only possible if racing with a
1623 rename of one of the parent directories (we can not
1624 lock the dentries above us to prevent this, but
1625 retrying should be harmless) */
1630 *base = ceph_ino(temp->d_inode);
1632 dout("build_path on %p %d built %llx '%.*s'\n",
1633 dentry, d_count(dentry), *base, len, path);
1637 static int build_dentry_path(struct dentry *dentry,
1638 const char **ppath, int *ppathlen, u64 *pino,
1643 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1644 *pino = ceph_ino(dentry->d_parent->d_inode);
1645 *ppath = dentry->d_name.name;
1646 *ppathlen = dentry->d_name.len;
1649 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1651 return PTR_ERR(path);
1657 static int build_inode_path(struct inode *inode,
1658 const char **ppath, int *ppathlen, u64 *pino,
1661 struct dentry *dentry;
1664 if (ceph_snap(inode) == CEPH_NOSNAP) {
1665 *pino = ceph_ino(inode);
1669 dentry = d_find_alias(inode);
1670 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1673 return PTR_ERR(path);
1680 * request arguments may be specified via an inode *, a dentry *, or
1681 * an explicit ino+path.
1683 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1684 const char *rpath, u64 rino,
1685 const char **ppath, int *pathlen,
1686 u64 *ino, int *freepath)
1691 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1692 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1694 } else if (rdentry) {
1695 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1696 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1698 } else if (rpath || rino) {
1701 *pathlen = rpath ? strlen(rpath) : 0;
1702 dout(" path %.*s\n", *pathlen, rpath);
1709 * called under mdsc->mutex
1711 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1712 struct ceph_mds_request *req,
1715 struct ceph_msg *msg;
1716 struct ceph_mds_request_head *head;
1717 const char *path1 = NULL;
1718 const char *path2 = NULL;
1719 u64 ino1 = 0, ino2 = 0;
1720 int pathlen1 = 0, pathlen2 = 0;
1721 int freepath1 = 0, freepath2 = 0;
1727 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1728 req->r_path1, req->r_ino1.ino,
1729 &path1, &pathlen1, &ino1, &freepath1);
1735 ret = set_request_path_attr(NULL, req->r_old_dentry,
1736 req->r_path2, req->r_ino2.ino,
1737 &path2, &pathlen2, &ino2, &freepath2);
1743 len = sizeof(*head) +
1744 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
1746 /* calculate (max) length for cap releases */
1747 len += sizeof(struct ceph_mds_request_release) *
1748 (!!req->r_inode_drop + !!req->r_dentry_drop +
1749 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1750 if (req->r_dentry_drop)
1751 len += req->r_dentry->d_name.len;
1752 if (req->r_old_dentry_drop)
1753 len += req->r_old_dentry->d_name.len;
1755 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
1757 msg = ERR_PTR(-ENOMEM);
1761 msg->hdr.tid = cpu_to_le64(req->r_tid);
1763 head = msg->front.iov_base;
1764 p = msg->front.iov_base + sizeof(*head);
1765 end = msg->front.iov_base + msg->front.iov_len;
1767 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1768 head->op = cpu_to_le32(req->r_op);
1769 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
1770 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
1771 head->args = req->r_args;
1773 ceph_encode_filepath(&p, end, ino1, path1);
1774 ceph_encode_filepath(&p, end, ino2, path2);
1776 /* make note of release offset, in case we need to replay */
1777 req->r_request_release_offset = p - msg->front.iov_base;
1781 if (req->r_inode_drop)
1782 releases += ceph_encode_inode_release(&p,
1783 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1784 mds, req->r_inode_drop, req->r_inode_unless, 0);
1785 if (req->r_dentry_drop)
1786 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1787 mds, req->r_dentry_drop, req->r_dentry_unless);
1788 if (req->r_old_dentry_drop)
1789 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1790 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1791 if (req->r_old_inode_drop)
1792 releases += ceph_encode_inode_release(&p,
1793 req->r_old_dentry->d_inode,
1794 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1795 head->num_releases = cpu_to_le16(releases);
1798 msg->front.iov_len = p - msg->front.iov_base;
1799 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1801 if (req->r_data_len) {
1802 /* outbound data set only by ceph_sync_setxattr() */
1803 BUG_ON(!req->r_pages);
1804 ceph_msg_data_add_pages(msg, req->r_pages, req->r_data_len, 0);
1807 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1808 msg->hdr.data_off = cpu_to_le16(0);
1812 kfree((char *)path2);
1815 kfree((char *)path1);
1821 * called under mdsc->mutex if error, under no mutex if
1824 static void complete_request(struct ceph_mds_client *mdsc,
1825 struct ceph_mds_request *req)
1827 if (req->r_callback)
1828 req->r_callback(mdsc, req);
1830 complete_all(&req->r_completion);
1834 * called under mdsc->mutex
1836 static int __prepare_send_request(struct ceph_mds_client *mdsc,
1837 struct ceph_mds_request *req,
1840 struct ceph_mds_request_head *rhead;
1841 struct ceph_msg *msg;
1846 struct ceph_cap *cap =
1847 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1850 req->r_sent_on_mseq = cap->mseq;
1852 req->r_sent_on_mseq = -1;
1854 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1855 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1857 if (req->r_got_unsafe) {
1859 * Replay. Do not regenerate message (and rebuild
1860 * paths, etc.); just use the original message.
1861 * Rebuilding paths will break for renames because
1862 * d_move mangles the src name.
1864 msg = req->r_request;
1865 rhead = msg->front.iov_base;
1867 flags = le32_to_cpu(rhead->flags);
1868 flags |= CEPH_MDS_FLAG_REPLAY;
1869 rhead->flags = cpu_to_le32(flags);
1871 if (req->r_target_inode)
1872 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1874 rhead->num_retry = req->r_attempts - 1;
1876 /* remove cap/dentry releases from message */
1877 rhead->num_releases = 0;
1878 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1879 msg->front.iov_len = req->r_request_release_offset;
1883 if (req->r_request) {
1884 ceph_msg_put(req->r_request);
1885 req->r_request = NULL;
1887 msg = create_request_message(mdsc, req, mds);
1889 req->r_err = PTR_ERR(msg);
1890 complete_request(mdsc, req);
1891 return PTR_ERR(msg);
1893 req->r_request = msg;
1895 rhead = msg->front.iov_base;
1896 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1897 if (req->r_got_unsafe)
1898 flags |= CEPH_MDS_FLAG_REPLAY;
1899 if (req->r_locked_dir)
1900 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1901 rhead->flags = cpu_to_le32(flags);
1902 rhead->num_fwd = req->r_num_fwd;
1903 rhead->num_retry = req->r_attempts - 1;
1906 dout(" r_locked_dir = %p\n", req->r_locked_dir);
1911 * send request, or put it on the appropriate wait list.
1913 static int __do_request(struct ceph_mds_client *mdsc,
1914 struct ceph_mds_request *req)
1916 struct ceph_mds_session *session = NULL;
1920 if (req->r_err || req->r_got_result) {
1922 __unregister_request(mdsc, req);
1926 if (req->r_timeout &&
1927 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1928 dout("do_request timed out\n");
1933 put_request_session(req);
1935 mds = __choose_mds(mdsc, req);
1937 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1938 dout("do_request no mds or not active, waiting for map\n");
1939 list_add(&req->r_wait, &mdsc->waiting_for_map);
1943 /* get, open session */
1944 session = __ceph_lookup_mds_session(mdsc, mds);
1946 session = register_session(mdsc, mds);
1947 if (IS_ERR(session)) {
1948 err = PTR_ERR(session);
1952 req->r_session = get_session(session);
1954 dout("do_request mds%d session %p state %s\n", mds, session,
1955 session_state_name(session->s_state));
1956 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1957 session->s_state != CEPH_MDS_SESSION_HUNG) {
1958 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1959 session->s_state == CEPH_MDS_SESSION_CLOSING)
1960 __open_session(mdsc, session);
1961 list_add(&req->r_wait, &session->s_waiting);
1966 req->r_resend_mds = -1; /* forget any previous mds hint */
1968 if (req->r_request_started == 0) /* note request start time */
1969 req->r_request_started = jiffies;
1971 err = __prepare_send_request(mdsc, req, mds);
1973 ceph_msg_get(req->r_request);
1974 ceph_con_send(&session->s_con, req->r_request);
1978 ceph_put_mds_session(session);
1984 complete_request(mdsc, req);
1989 * called under mdsc->mutex
1991 static void __wake_requests(struct ceph_mds_client *mdsc,
1992 struct list_head *head)
1994 struct ceph_mds_request *req;
1995 LIST_HEAD(tmp_list);
1997 list_splice_init(head, &tmp_list);
1999 while (!list_empty(&tmp_list)) {
2000 req = list_entry(tmp_list.next,
2001 struct ceph_mds_request, r_wait);
2002 list_del_init(&req->r_wait);
2003 dout(" wake request %p tid %llu\n", req, req->r_tid);
2004 __do_request(mdsc, req);
2009 * Wake up threads with requests pending for @mds, so that they can
2010 * resubmit their requests to a possibly different mds.
2012 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
2014 struct ceph_mds_request *req;
2017 dout("kick_requests mds%d\n", mds);
2018 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
2019 req = rb_entry(p, struct ceph_mds_request, r_node);
2020 if (req->r_got_unsafe)
2022 if (req->r_session &&
2023 req->r_session->s_mds == mds) {
2024 dout(" kicking tid %llu\n", req->r_tid);
2025 __do_request(mdsc, req);
2030 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2031 struct ceph_mds_request *req)
2033 dout("submit_request on %p\n", req);
2034 mutex_lock(&mdsc->mutex);
2035 __register_request(mdsc, req, NULL);
2036 __do_request(mdsc, req);
2037 mutex_unlock(&mdsc->mutex);
2041 * Synchrously perform an mds request. Take care of all of the
2042 * session setup, forwarding, retry details.
2044 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2046 struct ceph_mds_request *req)
2050 dout("do_request on %p\n", req);
2052 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
2054 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
2055 if (req->r_locked_dir)
2056 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
2057 if (req->r_old_dentry_dir)
2058 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2062 mutex_lock(&mdsc->mutex);
2063 __register_request(mdsc, req, dir);
2064 __do_request(mdsc, req);
2068 __unregister_request(mdsc, req);
2069 dout("do_request early error %d\n", err);
2074 mutex_unlock(&mdsc->mutex);
2075 dout("do_request waiting\n");
2076 if (req->r_timeout) {
2077 err = (long)wait_for_completion_killable_timeout(
2078 &req->r_completion, req->r_timeout);
2082 err = wait_for_completion_killable(&req->r_completion);
2084 dout("do_request waited, got %d\n", err);
2085 mutex_lock(&mdsc->mutex);
2087 /* only abort if we didn't race with a real reply */
2088 if (req->r_got_result) {
2089 err = le32_to_cpu(req->r_reply_info.head->result);
2090 } else if (err < 0) {
2091 dout("aborted request %lld with %d\n", req->r_tid, err);
2094 * ensure we aren't running concurrently with
2095 * ceph_fill_trace or ceph_readdir_prepopulate, which
2096 * rely on locks (dir mutex) held by our caller.
2098 mutex_lock(&req->r_fill_mutex);
2100 req->r_aborted = true;
2101 mutex_unlock(&req->r_fill_mutex);
2103 if (req->r_locked_dir &&
2104 (req->r_op & CEPH_MDS_OP_WRITE))
2105 ceph_invalidate_dir_request(req);
2111 mutex_unlock(&mdsc->mutex);
2112 dout("do_request %p done, result %d\n", req, err);
2117 * Invalidate dir's completeness, dentry lease state on an aborted MDS
2118 * namespace request.
2120 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2122 struct inode *inode = req->r_locked_dir;
2124 dout("invalidate_dir_request %p (complete, lease(s))\n", inode);
2126 ceph_dir_clear_complete(inode);
2128 ceph_invalidate_dentry_lease(req->r_dentry);
2129 if (req->r_old_dentry)
2130 ceph_invalidate_dentry_lease(req->r_old_dentry);
2136 * We take the session mutex and parse and process the reply immediately.
2137 * This preserves the logical ordering of replies, capabilities, etc., sent
2138 * by the MDS as they are applied to our local cache.
2140 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2142 struct ceph_mds_client *mdsc = session->s_mdsc;
2143 struct ceph_mds_request *req;
2144 struct ceph_mds_reply_head *head = msg->front.iov_base;
2145 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2148 int mds = session->s_mds;
2150 if (msg->front.iov_len < sizeof(*head)) {
2151 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2156 /* get request, session */
2157 tid = le64_to_cpu(msg->hdr.tid);
2158 mutex_lock(&mdsc->mutex);
2159 req = __lookup_request(mdsc, tid);
2161 dout("handle_reply on unknown tid %llu\n", tid);
2162 mutex_unlock(&mdsc->mutex);
2165 dout("handle_reply %p\n", req);
2167 /* correct session? */
2168 if (req->r_session != session) {
2169 pr_err("mdsc_handle_reply got %llu on session mds%d"
2170 " not mds%d\n", tid, session->s_mds,
2171 req->r_session ? req->r_session->s_mds : -1);
2172 mutex_unlock(&mdsc->mutex);
2177 if ((req->r_got_unsafe && !head->safe) ||
2178 (req->r_got_safe && head->safe)) {
2179 pr_warning("got a dup %s reply on %llu from mds%d\n",
2180 head->safe ? "safe" : "unsafe", tid, mds);
2181 mutex_unlock(&mdsc->mutex);
2184 if (req->r_got_safe && !head->safe) {
2185 pr_warning("got unsafe after safe on %llu from mds%d\n",
2187 mutex_unlock(&mdsc->mutex);
2191 result = le32_to_cpu(head->result);
2195 * if we're not talking to the authority, send to them
2196 * if the authority has changed while we weren't looking,
2197 * send to new authority
2198 * Otherwise we just have to return an ESTALE
2200 if (result == -ESTALE) {
2201 dout("got ESTALE on request %llu", req->r_tid);
2202 if (req->r_direct_mode != USE_AUTH_MDS) {
2203 dout("not using auth, setting for that now");
2204 req->r_direct_mode = USE_AUTH_MDS;
2205 __do_request(mdsc, req);
2206 mutex_unlock(&mdsc->mutex);
2209 int mds = __choose_mds(mdsc, req);
2210 if (mds >= 0 && mds != req->r_session->s_mds) {
2211 dout("but auth changed, so resending");
2212 __do_request(mdsc, req);
2213 mutex_unlock(&mdsc->mutex);
2217 dout("have to return ESTALE on request %llu", req->r_tid);
2222 req->r_got_safe = true;
2223 __unregister_request(mdsc, req);
2225 if (req->r_got_unsafe) {
2227 * We already handled the unsafe response, now do the
2228 * cleanup. No need to examine the response; the MDS
2229 * doesn't include any result info in the safe
2230 * response. And even if it did, there is nothing
2231 * useful we could do with a revised return value.
2233 dout("got safe reply %llu, mds%d\n", tid, mds);
2234 list_del_init(&req->r_unsafe_item);
2236 /* last unsafe request during umount? */
2237 if (mdsc->stopping && !__get_oldest_req(mdsc))
2238 complete_all(&mdsc->safe_umount_waiters);
2239 mutex_unlock(&mdsc->mutex);
2243 req->r_got_unsafe = true;
2244 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2247 dout("handle_reply tid %lld result %d\n", tid, result);
2248 rinfo = &req->r_reply_info;
2249 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2250 mutex_unlock(&mdsc->mutex);
2252 mutex_lock(&session->s_mutex);
2254 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
2260 if (rinfo->snapblob_len) {
2261 down_write(&mdsc->snap_rwsem);
2262 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2263 rinfo->snapblob + rinfo->snapblob_len,
2264 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2265 downgrade_write(&mdsc->snap_rwsem);
2267 down_read(&mdsc->snap_rwsem);
2270 /* insert trace into our cache */
2271 mutex_lock(&req->r_fill_mutex);
2272 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
2274 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
2275 req->r_op == CEPH_MDS_OP_LSSNAP))
2276 ceph_readdir_prepopulate(req, req->r_session);
2277 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2279 mutex_unlock(&req->r_fill_mutex);
2281 up_read(&mdsc->snap_rwsem);
2283 mutex_lock(&mdsc->mutex);
2284 if (!req->r_aborted) {
2290 req->r_got_result = true;
2293 dout("reply arrived after request %lld was aborted\n", tid);
2295 mutex_unlock(&mdsc->mutex);
2297 ceph_add_cap_releases(mdsc, req->r_session);
2298 mutex_unlock(&session->s_mutex);
2300 /* kick calling process */
2301 complete_request(mdsc, req);
2303 ceph_mdsc_put_request(req);
2310 * handle mds notification that our request has been forwarded.
2312 static void handle_forward(struct ceph_mds_client *mdsc,
2313 struct ceph_mds_session *session,
2314 struct ceph_msg *msg)
2316 struct ceph_mds_request *req;
2317 u64 tid = le64_to_cpu(msg->hdr.tid);
2321 void *p = msg->front.iov_base;
2322 void *end = p + msg->front.iov_len;
2324 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2325 next_mds = ceph_decode_32(&p);
2326 fwd_seq = ceph_decode_32(&p);
2328 mutex_lock(&mdsc->mutex);
2329 req = __lookup_request(mdsc, tid);
2331 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2332 goto out; /* dup reply? */
2335 if (req->r_aborted) {
2336 dout("forward tid %llu aborted, unregistering\n", tid);
2337 __unregister_request(mdsc, req);
2338 } else if (fwd_seq <= req->r_num_fwd) {
2339 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2340 tid, next_mds, req->r_num_fwd, fwd_seq);
2342 /* resend. forward race not possible; mds would drop */
2343 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2345 BUG_ON(req->r_got_result);
2346 req->r_num_fwd = fwd_seq;
2347 req->r_resend_mds = next_mds;
2348 put_request_session(req);
2349 __do_request(mdsc, req);
2351 ceph_mdsc_put_request(req);
2353 mutex_unlock(&mdsc->mutex);
2357 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2361 * handle a mds session control message
2363 static void handle_session(struct ceph_mds_session *session,
2364 struct ceph_msg *msg)
2366 struct ceph_mds_client *mdsc = session->s_mdsc;
2369 int mds = session->s_mds;
2370 struct ceph_mds_session_head *h = msg->front.iov_base;
2374 if (msg->front.iov_len != sizeof(*h))
2376 op = le32_to_cpu(h->op);
2377 seq = le64_to_cpu(h->seq);
2379 mutex_lock(&mdsc->mutex);
2380 if (op == CEPH_SESSION_CLOSE)
2381 __unregister_session(mdsc, session);
2382 /* FIXME: this ttl calculation is generous */
2383 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2384 mutex_unlock(&mdsc->mutex);
2386 mutex_lock(&session->s_mutex);
2388 dout("handle_session mds%d %s %p state %s seq %llu\n",
2389 mds, ceph_session_op_name(op), session,
2390 session_state_name(session->s_state), seq);
2392 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2393 session->s_state = CEPH_MDS_SESSION_OPEN;
2394 pr_info("mds%d came back\n", session->s_mds);
2398 case CEPH_SESSION_OPEN:
2399 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2400 pr_info("mds%d reconnect success\n", session->s_mds);
2401 session->s_state = CEPH_MDS_SESSION_OPEN;
2402 renewed_caps(mdsc, session, 0);
2405 __close_session(mdsc, session);
2408 case CEPH_SESSION_RENEWCAPS:
2409 if (session->s_renew_seq == seq)
2410 renewed_caps(mdsc, session, 1);
2413 case CEPH_SESSION_CLOSE:
2414 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2415 pr_info("mds%d reconnect denied\n", session->s_mds);
2416 remove_session_caps(session);
2417 wake = 1; /* for good measure */
2418 wake_up_all(&mdsc->session_close_wq);
2419 kick_requests(mdsc, mds);
2422 case CEPH_SESSION_STALE:
2423 pr_info("mds%d caps went stale, renewing\n",
2425 spin_lock(&session->s_gen_ttl_lock);
2426 session->s_cap_gen++;
2427 session->s_cap_ttl = jiffies - 1;
2428 spin_unlock(&session->s_gen_ttl_lock);
2429 send_renew_caps(mdsc, session);
2432 case CEPH_SESSION_RECALL_STATE:
2433 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2436 case CEPH_SESSION_FLUSHMSG:
2437 send_flushmsg_ack(mdsc, session, seq);
2441 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2445 mutex_unlock(&session->s_mutex);
2447 mutex_lock(&mdsc->mutex);
2448 __wake_requests(mdsc, &session->s_waiting);
2449 mutex_unlock(&mdsc->mutex);
2454 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2455 (int)msg->front.iov_len);
2462 * called under session->mutex.
2464 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2465 struct ceph_mds_session *session)
2467 struct ceph_mds_request *req, *nreq;
2470 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2472 mutex_lock(&mdsc->mutex);
2473 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2474 err = __prepare_send_request(mdsc, req, session->s_mds);
2476 ceph_msg_get(req->r_request);
2477 ceph_con_send(&session->s_con, req->r_request);
2480 mutex_unlock(&mdsc->mutex);
2484 * Encode information about a cap for a reconnect with the MDS.
2486 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2490 struct ceph_mds_cap_reconnect v2;
2491 struct ceph_mds_cap_reconnect_v1 v1;
2494 struct ceph_inode_info *ci;
2495 struct ceph_reconnect_state *recon_state = arg;
2496 struct ceph_pagelist *pagelist = recon_state->pagelist;
2500 struct dentry *dentry;
2504 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2505 inode, ceph_vinop(inode), cap, cap->cap_id,
2506 ceph_cap_string(cap->issued));
2507 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2511 dentry = d_find_alias(inode);
2513 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2515 err = PTR_ERR(path);
2522 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2526 spin_lock(&ci->i_ceph_lock);
2527 cap->seq = 0; /* reset cap seq */
2528 cap->issue_seq = 0; /* and issue_seq */
2529 cap->mseq = 0; /* and migrate_seq */
2530 cap->cap_gen = cap->session->s_cap_gen;
2532 if (recon_state->flock) {
2533 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2534 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2535 rec.v2.issued = cpu_to_le32(cap->issued);
2536 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2537 rec.v2.pathbase = cpu_to_le64(pathbase);
2538 rec.v2.flock_len = 0;
2539 reclen = sizeof(rec.v2);
2541 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2542 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2543 rec.v1.issued = cpu_to_le32(cap->issued);
2544 rec.v1.size = cpu_to_le64(inode->i_size);
2545 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2546 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2547 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2548 rec.v1.pathbase = cpu_to_le64(pathbase);
2549 reclen = sizeof(rec.v1);
2551 spin_unlock(&ci->i_ceph_lock);
2553 if (recon_state->flock) {
2554 int num_fcntl_locks, num_flock_locks;
2555 struct ceph_filelock *flocks;
2558 spin_lock(&inode->i_lock);
2559 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
2560 spin_unlock(&inode->i_lock);
2561 flocks = kmalloc((num_fcntl_locks+num_flock_locks) *
2562 sizeof(struct ceph_filelock), GFP_NOFS);
2567 spin_lock(&inode->i_lock);
2568 err = ceph_encode_locks_to_buffer(inode, flocks,
2571 spin_unlock(&inode->i_lock);
2579 * number of encoded locks is stable, so copy to pagelist
2581 rec.v2.flock_len = cpu_to_le32(2*sizeof(u32) +
2582 (num_fcntl_locks+num_flock_locks) *
2583 sizeof(struct ceph_filelock));
2584 err = ceph_pagelist_append(pagelist, &rec, reclen);
2586 err = ceph_locks_to_pagelist(flocks, pagelist,
2591 err = ceph_pagelist_append(pagelist, &rec, reclen);
2594 recon_state->nr_caps++;
2604 * If an MDS fails and recovers, clients need to reconnect in order to
2605 * reestablish shared state. This includes all caps issued through
2606 * this session _and_ the snap_realm hierarchy. Because it's not
2607 * clear which snap realms the mds cares about, we send everything we
2608 * know about.. that ensures we'll then get any new info the
2609 * recovering MDS might have.
2611 * This is a relatively heavyweight operation, but it's rare.
2613 * called with mdsc->mutex held.
2615 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2616 struct ceph_mds_session *session)
2618 struct ceph_msg *reply;
2620 int mds = session->s_mds;
2623 struct ceph_pagelist *pagelist;
2624 struct ceph_reconnect_state recon_state;
2626 pr_info("mds%d reconnect start\n", mds);
2628 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2630 goto fail_nopagelist;
2631 ceph_pagelist_init(pagelist);
2633 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
2637 mutex_lock(&session->s_mutex);
2638 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2641 ceph_con_close(&session->s_con);
2642 ceph_con_open(&session->s_con,
2643 CEPH_ENTITY_TYPE_MDS, mds,
2644 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2646 /* replay unsafe requests */
2647 replay_unsafe_requests(mdsc, session);
2649 down_read(&mdsc->snap_rwsem);
2651 dout("session %p state %s\n", session,
2652 session_state_name(session->s_state));
2654 spin_lock(&session->s_gen_ttl_lock);
2655 session->s_cap_gen++;
2656 spin_unlock(&session->s_gen_ttl_lock);
2658 spin_lock(&session->s_cap_lock);
2660 * notify __ceph_remove_cap() that we are composing cap reconnect.
2661 * If a cap get released before being added to the cap reconnect,
2662 * __ceph_remove_cap() should skip queuing cap release.
2664 session->s_cap_reconnect = 1;
2665 /* drop old cap expires; we're about to reestablish that state */
2666 discard_cap_releases(mdsc, session);
2667 spin_unlock(&session->s_cap_lock);
2669 /* traverse this session's caps */
2670 s_nr_caps = session->s_nr_caps;
2671 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
2675 recon_state.nr_caps = 0;
2676 recon_state.pagelist = pagelist;
2677 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2678 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
2682 spin_lock(&session->s_cap_lock);
2683 session->s_cap_reconnect = 0;
2684 spin_unlock(&session->s_cap_lock);
2687 * snaprealms. we provide mds with the ino, seq (version), and
2688 * parent for all of our realms. If the mds has any newer info,
2691 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2692 struct ceph_snap_realm *realm =
2693 rb_entry(p, struct ceph_snap_realm, node);
2694 struct ceph_mds_snaprealm_reconnect sr_rec;
2696 dout(" adding snap realm %llx seq %lld parent %llx\n",
2697 realm->ino, realm->seq, realm->parent_ino);
2698 sr_rec.ino = cpu_to_le64(realm->ino);
2699 sr_rec.seq = cpu_to_le64(realm->seq);
2700 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2701 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2706 if (recon_state.flock)
2707 reply->hdr.version = cpu_to_le16(2);
2709 /* raced with cap release? */
2710 if (s_nr_caps != recon_state.nr_caps) {
2711 struct page *page = list_first_entry(&pagelist->head,
2713 __le32 *addr = kmap_atomic(page);
2714 *addr = cpu_to_le32(recon_state.nr_caps);
2715 kunmap_atomic(addr);
2718 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2719 ceph_msg_data_add_pagelist(reply, pagelist);
2720 ceph_con_send(&session->s_con, reply);
2722 mutex_unlock(&session->s_mutex);
2724 mutex_lock(&mdsc->mutex);
2725 __wake_requests(mdsc, &session->s_waiting);
2726 mutex_unlock(&mdsc->mutex);
2728 up_read(&mdsc->snap_rwsem);
2732 ceph_msg_put(reply);
2733 up_read(&mdsc->snap_rwsem);
2734 mutex_unlock(&session->s_mutex);
2736 ceph_pagelist_release(pagelist);
2739 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
2745 * compare old and new mdsmaps, kicking requests
2746 * and closing out old connections as necessary
2748 * called under mdsc->mutex.
2750 static void check_new_map(struct ceph_mds_client *mdsc,
2751 struct ceph_mdsmap *newmap,
2752 struct ceph_mdsmap *oldmap)
2755 int oldstate, newstate;
2756 struct ceph_mds_session *s;
2758 dout("check_new_map new %u old %u\n",
2759 newmap->m_epoch, oldmap->m_epoch);
2761 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2762 if (mdsc->sessions[i] == NULL)
2764 s = mdsc->sessions[i];
2765 oldstate = ceph_mdsmap_get_state(oldmap, i);
2766 newstate = ceph_mdsmap_get_state(newmap, i);
2768 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2769 i, ceph_mds_state_name(oldstate),
2770 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
2771 ceph_mds_state_name(newstate),
2772 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
2773 session_state_name(s->s_state));
2775 if (i >= newmap->m_max_mds ||
2776 memcmp(ceph_mdsmap_get_addr(oldmap, i),
2777 ceph_mdsmap_get_addr(newmap, i),
2778 sizeof(struct ceph_entity_addr))) {
2779 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2780 /* the session never opened, just close it
2782 __wake_requests(mdsc, &s->s_waiting);
2783 __unregister_session(mdsc, s);
2786 mutex_unlock(&mdsc->mutex);
2787 mutex_lock(&s->s_mutex);
2788 mutex_lock(&mdsc->mutex);
2789 ceph_con_close(&s->s_con);
2790 mutex_unlock(&s->s_mutex);
2791 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2794 /* kick any requests waiting on the recovering mds */
2795 kick_requests(mdsc, i);
2796 } else if (oldstate == newstate) {
2797 continue; /* nothing new with this mds */
2803 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
2804 newstate >= CEPH_MDS_STATE_RECONNECT) {
2805 mutex_unlock(&mdsc->mutex);
2806 send_mds_reconnect(mdsc, s);
2807 mutex_lock(&mdsc->mutex);
2811 * kick request on any mds that has gone active.
2813 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2814 newstate >= CEPH_MDS_STATE_ACTIVE) {
2815 if (oldstate != CEPH_MDS_STATE_CREATING &&
2816 oldstate != CEPH_MDS_STATE_STARTING)
2817 pr_info("mds%d recovery completed\n", s->s_mds);
2818 kick_requests(mdsc, i);
2819 ceph_kick_flushing_caps(mdsc, s);
2820 wake_up_session_caps(s, 1);
2824 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2825 s = mdsc->sessions[i];
2828 if (!ceph_mdsmap_is_laggy(newmap, i))
2830 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2831 s->s_state == CEPH_MDS_SESSION_HUNG ||
2832 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2833 dout(" connecting to export targets of laggy mds%d\n",
2835 __open_export_target_sessions(mdsc, s);
2847 * caller must hold session s_mutex, dentry->d_lock
2849 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2851 struct ceph_dentry_info *di = ceph_dentry(dentry);
2853 ceph_put_mds_session(di->lease_session);
2854 di->lease_session = NULL;
2857 static void handle_lease(struct ceph_mds_client *mdsc,
2858 struct ceph_mds_session *session,
2859 struct ceph_msg *msg)
2861 struct super_block *sb = mdsc->fsc->sb;
2862 struct inode *inode;
2863 struct dentry *parent, *dentry;
2864 struct ceph_dentry_info *di;
2865 int mds = session->s_mds;
2866 struct ceph_mds_lease *h = msg->front.iov_base;
2868 struct ceph_vino vino;
2872 dout("handle_lease from mds%d\n", mds);
2875 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2877 vino.ino = le64_to_cpu(h->ino);
2878 vino.snap = CEPH_NOSNAP;
2879 seq = le32_to_cpu(h->seq);
2880 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2881 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2882 if (dname.len != get_unaligned_le32(h+1))
2885 mutex_lock(&session->s_mutex);
2889 inode = ceph_find_inode(sb, vino);
2890 dout("handle_lease %s, ino %llx %p %.*s\n",
2891 ceph_lease_op_name(h->action), vino.ino, inode,
2892 dname.len, dname.name);
2893 if (inode == NULL) {
2894 dout("handle_lease no inode %llx\n", vino.ino);
2899 parent = d_find_alias(inode);
2901 dout("no parent dentry on inode %p\n", inode);
2903 goto release; /* hrm... */
2905 dname.hash = full_name_hash(dname.name, dname.len);
2906 dentry = d_lookup(parent, &dname);
2911 spin_lock(&dentry->d_lock);
2912 di = ceph_dentry(dentry);
2913 switch (h->action) {
2914 case CEPH_MDS_LEASE_REVOKE:
2915 if (di->lease_session == session) {
2916 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2917 h->seq = cpu_to_le32(di->lease_seq);
2918 __ceph_mdsc_drop_dentry_lease(dentry);
2923 case CEPH_MDS_LEASE_RENEW:
2924 if (di->lease_session == session &&
2925 di->lease_gen == session->s_cap_gen &&
2926 di->lease_renew_from &&
2927 di->lease_renew_after == 0) {
2928 unsigned long duration =
2929 le32_to_cpu(h->duration_ms) * HZ / 1000;
2931 di->lease_seq = seq;
2932 dentry->d_time = di->lease_renew_from + duration;
2933 di->lease_renew_after = di->lease_renew_from +
2935 di->lease_renew_from = 0;
2939 spin_unlock(&dentry->d_lock);
2946 /* let's just reuse the same message */
2947 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2949 ceph_con_send(&session->s_con, msg);
2953 mutex_unlock(&session->s_mutex);
2957 pr_err("corrupt lease message\n");
2961 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2962 struct inode *inode,
2963 struct dentry *dentry, char action,
2966 struct ceph_msg *msg;
2967 struct ceph_mds_lease *lease;
2968 int len = sizeof(*lease) + sizeof(u32);
2971 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2972 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2973 dnamelen = dentry->d_name.len;
2976 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
2979 lease = msg->front.iov_base;
2980 lease->action = action;
2981 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2982 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2983 lease->seq = cpu_to_le32(seq);
2984 put_unaligned_le32(dnamelen, lease + 1);
2985 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2988 * if this is a preemptive lease RELEASE, no need to
2989 * flush request stream, since the actual request will
2992 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2994 ceph_con_send(&session->s_con, msg);
2998 * Preemptively release a lease we expect to invalidate anyway.
2999 * Pass @inode always, @dentry is optional.
3001 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
3002 struct dentry *dentry)
3004 struct ceph_dentry_info *di;
3005 struct ceph_mds_session *session;
3008 BUG_ON(inode == NULL);
3009 BUG_ON(dentry == NULL);
3011 /* is dentry lease valid? */
3012 spin_lock(&dentry->d_lock);
3013 di = ceph_dentry(dentry);
3014 if (!di || !di->lease_session ||
3015 di->lease_session->s_mds < 0 ||
3016 di->lease_gen != di->lease_session->s_cap_gen ||
3017 !time_before(jiffies, dentry->d_time)) {
3018 dout("lease_release inode %p dentry %p -- "
3021 spin_unlock(&dentry->d_lock);
3025 /* we do have a lease on this dentry; note mds and seq */
3026 session = ceph_get_mds_session(di->lease_session);
3027 seq = di->lease_seq;
3028 __ceph_mdsc_drop_dentry_lease(dentry);
3029 spin_unlock(&dentry->d_lock);
3031 dout("lease_release inode %p dentry %p to mds%d\n",
3032 inode, dentry, session->s_mds);
3033 ceph_mdsc_lease_send_msg(session, inode, dentry,
3034 CEPH_MDS_LEASE_RELEASE, seq);
3035 ceph_put_mds_session(session);
3039 * drop all leases (and dentry refs) in preparation for umount
3041 static void drop_leases(struct ceph_mds_client *mdsc)
3045 dout("drop_leases\n");
3046 mutex_lock(&mdsc->mutex);
3047 for (i = 0; i < mdsc->max_sessions; i++) {
3048 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3051 mutex_unlock(&mdsc->mutex);
3052 mutex_lock(&s->s_mutex);
3053 mutex_unlock(&s->s_mutex);
3054 ceph_put_mds_session(s);
3055 mutex_lock(&mdsc->mutex);
3057 mutex_unlock(&mdsc->mutex);
3063 * delayed work -- periodically trim expired leases, renew caps with mds
3065 static void schedule_delayed(struct ceph_mds_client *mdsc)
3068 unsigned hz = round_jiffies_relative(HZ * delay);
3069 schedule_delayed_work(&mdsc->delayed_work, hz);
3072 static void delayed_work(struct work_struct *work)
3075 struct ceph_mds_client *mdsc =
3076 container_of(work, struct ceph_mds_client, delayed_work.work);
3080 dout("mdsc delayed_work\n");
3081 ceph_check_delayed_caps(mdsc);
3083 mutex_lock(&mdsc->mutex);
3084 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3085 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3086 mdsc->last_renew_caps);
3088 mdsc->last_renew_caps = jiffies;
3090 for (i = 0; i < mdsc->max_sessions; i++) {
3091 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3094 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3095 dout("resending session close request for mds%d\n",
3097 request_close_session(mdsc, s);
3098 ceph_put_mds_session(s);
3101 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3102 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3103 s->s_state = CEPH_MDS_SESSION_HUNG;
3104 pr_info("mds%d hung\n", s->s_mds);
3107 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3108 /* this mds is failed or recovering, just wait */
3109 ceph_put_mds_session(s);
3112 mutex_unlock(&mdsc->mutex);
3114 mutex_lock(&s->s_mutex);
3116 send_renew_caps(mdsc, s);
3118 ceph_con_keepalive(&s->s_con);
3119 ceph_add_cap_releases(mdsc, s);
3120 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3121 s->s_state == CEPH_MDS_SESSION_HUNG)
3122 ceph_send_cap_releases(mdsc, s);
3123 mutex_unlock(&s->s_mutex);
3124 ceph_put_mds_session(s);
3126 mutex_lock(&mdsc->mutex);
3128 mutex_unlock(&mdsc->mutex);
3130 schedule_delayed(mdsc);
3133 int ceph_mdsc_init(struct ceph_fs_client *fsc)
3136 struct ceph_mds_client *mdsc;
3138 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3143 mutex_init(&mdsc->mutex);
3144 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
3145 if (mdsc->mdsmap == NULL) {
3150 init_completion(&mdsc->safe_umount_waiters);
3151 init_waitqueue_head(&mdsc->session_close_wq);
3152 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3153 mdsc->sessions = NULL;
3154 mdsc->max_sessions = 0;
3156 init_rwsem(&mdsc->snap_rwsem);
3157 mdsc->snap_realms = RB_ROOT;
3158 INIT_LIST_HEAD(&mdsc->snap_empty);
3159 spin_lock_init(&mdsc->snap_empty_lock);
3161 mdsc->request_tree = RB_ROOT;
3162 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3163 mdsc->last_renew_caps = jiffies;
3164 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3165 spin_lock_init(&mdsc->cap_delay_lock);
3166 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3167 spin_lock_init(&mdsc->snap_flush_lock);
3168 mdsc->cap_flush_seq = 0;
3169 INIT_LIST_HEAD(&mdsc->cap_dirty);
3170 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
3171 mdsc->num_cap_flushing = 0;
3172 spin_lock_init(&mdsc->cap_dirty_lock);
3173 init_waitqueue_head(&mdsc->cap_flushing_wq);
3174 spin_lock_init(&mdsc->dentry_lru_lock);
3175 INIT_LIST_HEAD(&mdsc->dentry_lru);
3177 ceph_caps_init(mdsc);
3178 ceph_adjust_min_caps(mdsc, fsc->min_caps);
3184 * Wait for safe replies on open mds requests. If we time out, drop
3185 * all requests from the tree to avoid dangling dentry refs.
3187 static void wait_requests(struct ceph_mds_client *mdsc)
3189 struct ceph_mds_request *req;
3190 struct ceph_fs_client *fsc = mdsc->fsc;
3192 mutex_lock(&mdsc->mutex);
3193 if (__get_oldest_req(mdsc)) {
3194 mutex_unlock(&mdsc->mutex);
3196 dout("wait_requests waiting for requests\n");
3197 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
3198 fsc->client->options->mount_timeout * HZ);
3200 /* tear down remaining requests */
3201 mutex_lock(&mdsc->mutex);
3202 while ((req = __get_oldest_req(mdsc))) {
3203 dout("wait_requests timed out on tid %llu\n",
3205 __unregister_request(mdsc, req);
3208 mutex_unlock(&mdsc->mutex);
3209 dout("wait_requests done\n");
3213 * called before mount is ro, and before dentries are torn down.
3214 * (hmm, does this still race with new lookups?)
3216 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3218 dout("pre_umount\n");
3222 ceph_flush_dirty_caps(mdsc);
3223 wait_requests(mdsc);
3226 * wait for reply handlers to drop their request refs and
3227 * their inode/dcache refs
3233 * wait for all write mds requests to flush.
3235 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3237 struct ceph_mds_request *req = NULL, *nextreq;
3240 mutex_lock(&mdsc->mutex);
3241 dout("wait_unsafe_requests want %lld\n", want_tid);
3243 req = __get_oldest_req(mdsc);
3244 while (req && req->r_tid <= want_tid) {
3245 /* find next request */
3246 n = rb_next(&req->r_node);
3248 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3251 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3253 ceph_mdsc_get_request(req);
3255 ceph_mdsc_get_request(nextreq);
3256 mutex_unlock(&mdsc->mutex);
3257 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3258 req->r_tid, want_tid);
3259 wait_for_completion(&req->r_safe_completion);
3260 mutex_lock(&mdsc->mutex);
3261 ceph_mdsc_put_request(req);
3263 break; /* next dne before, so we're done! */
3264 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3265 /* next request was removed from tree */
3266 ceph_mdsc_put_request(nextreq);
3269 ceph_mdsc_put_request(nextreq); /* won't go away */
3273 mutex_unlock(&mdsc->mutex);
3274 dout("wait_unsafe_requests done\n");
3277 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3279 u64 want_tid, want_flush;
3281 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3285 mutex_lock(&mdsc->mutex);
3286 want_tid = mdsc->last_tid;
3287 want_flush = mdsc->cap_flush_seq;
3288 mutex_unlock(&mdsc->mutex);
3289 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3291 ceph_flush_dirty_caps(mdsc);
3293 wait_unsafe_requests(mdsc, want_tid);
3294 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3298 * true if all sessions are closed, or we force unmount
3300 static bool done_closing_sessions(struct ceph_mds_client *mdsc)
3304 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3307 mutex_lock(&mdsc->mutex);
3308 for (i = 0; i < mdsc->max_sessions; i++)
3309 if (mdsc->sessions[i])
3311 mutex_unlock(&mdsc->mutex);
3316 * called after sb is ro.
3318 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3320 struct ceph_mds_session *session;
3322 struct ceph_fs_client *fsc = mdsc->fsc;
3323 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
3325 dout("close_sessions\n");
3327 /* close sessions */
3328 mutex_lock(&mdsc->mutex);
3329 for (i = 0; i < mdsc->max_sessions; i++) {
3330 session = __ceph_lookup_mds_session(mdsc, i);
3333 mutex_unlock(&mdsc->mutex);
3334 mutex_lock(&session->s_mutex);
3335 __close_session(mdsc, session);
3336 mutex_unlock(&session->s_mutex);
3337 ceph_put_mds_session(session);
3338 mutex_lock(&mdsc->mutex);
3340 mutex_unlock(&mdsc->mutex);
3342 dout("waiting for sessions to close\n");
3343 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3346 /* tear down remaining sessions */
3347 mutex_lock(&mdsc->mutex);
3348 for (i = 0; i < mdsc->max_sessions; i++) {
3349 if (mdsc->sessions[i]) {
3350 session = get_session(mdsc->sessions[i]);
3351 __unregister_session(mdsc, session);
3352 mutex_unlock(&mdsc->mutex);
3353 mutex_lock(&session->s_mutex);
3354 remove_session_caps(session);
3355 mutex_unlock(&session->s_mutex);
3356 ceph_put_mds_session(session);
3357 mutex_lock(&mdsc->mutex);
3360 WARN_ON(!list_empty(&mdsc->cap_delay_list));
3361 mutex_unlock(&mdsc->mutex);
3363 ceph_cleanup_empty_realms(mdsc);
3365 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3370 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
3373 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3375 ceph_mdsmap_destroy(mdsc->mdsmap);
3376 kfree(mdsc->sessions);
3377 ceph_caps_finalize(mdsc);
3380 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3382 struct ceph_mds_client *mdsc = fsc->mdsc;
3384 dout("mdsc_destroy %p\n", mdsc);
3385 ceph_mdsc_stop(mdsc);
3387 /* flush out any connection work with references to us */
3392 dout("mdsc_destroy %p done\n", mdsc);
3397 * handle mds map update.
3399 void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3403 void *p = msg->front.iov_base;
3404 void *end = p + msg->front.iov_len;
3405 struct ceph_mdsmap *newmap, *oldmap;
3406 struct ceph_fsid fsid;
3409 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3410 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3411 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
3413 epoch = ceph_decode_32(&p);
3414 maplen = ceph_decode_32(&p);
3415 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3417 /* do we need it? */
3418 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
3419 mutex_lock(&mdsc->mutex);
3420 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3421 dout("handle_map epoch %u <= our %u\n",
3422 epoch, mdsc->mdsmap->m_epoch);
3423 mutex_unlock(&mdsc->mutex);
3427 newmap = ceph_mdsmap_decode(&p, end);
3428 if (IS_ERR(newmap)) {
3429 err = PTR_ERR(newmap);
3433 /* swap into place */
3435 oldmap = mdsc->mdsmap;
3436 mdsc->mdsmap = newmap;
3437 check_new_map(mdsc, newmap, oldmap);
3438 ceph_mdsmap_destroy(oldmap);
3440 mdsc->mdsmap = newmap; /* first mds map */
3442 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
3444 __wake_requests(mdsc, &mdsc->waiting_for_map);
3446 mutex_unlock(&mdsc->mutex);
3447 schedule_delayed(mdsc);
3451 mutex_unlock(&mdsc->mutex);
3453 pr_err("error decoding mdsmap %d\n", err);
3457 static struct ceph_connection *con_get(struct ceph_connection *con)
3459 struct ceph_mds_session *s = con->private;
3461 if (get_session(s)) {
3462 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
3465 dout("mdsc con_get %p FAIL\n", s);
3469 static void con_put(struct ceph_connection *con)
3471 struct ceph_mds_session *s = con->private;
3473 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref) - 1);
3474 ceph_put_mds_session(s);
3478 * if the client is unresponsive for long enough, the mds will kill
3479 * the session entirely.
3481 static void peer_reset(struct ceph_connection *con)
3483 struct ceph_mds_session *s = con->private;
3484 struct ceph_mds_client *mdsc = s->s_mdsc;
3486 pr_warning("mds%d closed our session\n", s->s_mds);
3487 send_mds_reconnect(mdsc, s);
3490 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3492 struct ceph_mds_session *s = con->private;
3493 struct ceph_mds_client *mdsc = s->s_mdsc;
3494 int type = le16_to_cpu(msg->hdr.type);
3496 mutex_lock(&mdsc->mutex);
3497 if (__verify_registered_session(mdsc, s) < 0) {
3498 mutex_unlock(&mdsc->mutex);
3501 mutex_unlock(&mdsc->mutex);
3504 case CEPH_MSG_MDS_MAP:
3505 ceph_mdsc_handle_map(mdsc, msg);
3507 case CEPH_MSG_CLIENT_SESSION:
3508 handle_session(s, msg);
3510 case CEPH_MSG_CLIENT_REPLY:
3511 handle_reply(s, msg);
3513 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
3514 handle_forward(mdsc, s, msg);
3516 case CEPH_MSG_CLIENT_CAPS:
3517 ceph_handle_caps(s, msg);
3519 case CEPH_MSG_CLIENT_SNAP:
3520 ceph_handle_snap(mdsc, s, msg);
3522 case CEPH_MSG_CLIENT_LEASE:
3523 handle_lease(mdsc, s, msg);
3527 pr_err("received unknown message type %d %s\n", type,
3528 ceph_msg_type_name(type));
3539 * Note: returned pointer is the address of a structure that's
3540 * managed separately. Caller must *not* attempt to free it.
3542 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
3543 int *proto, int force_new)
3545 struct ceph_mds_session *s = con->private;
3546 struct ceph_mds_client *mdsc = s->s_mdsc;
3547 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3548 struct ceph_auth_handshake *auth = &s->s_auth;
3550 if (force_new && auth->authorizer) {
3551 ceph_auth_destroy_authorizer(ac, auth->authorizer);
3552 auth->authorizer = NULL;
3554 if (!auth->authorizer) {
3555 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3558 return ERR_PTR(ret);
3560 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3563 return ERR_PTR(ret);
3565 *proto = ac->protocol;
3571 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3573 struct ceph_mds_session *s = con->private;
3574 struct ceph_mds_client *mdsc = s->s_mdsc;
3575 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3577 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer, len);
3580 static int invalidate_authorizer(struct ceph_connection *con)
3582 struct ceph_mds_session *s = con->private;
3583 struct ceph_mds_client *mdsc = s->s_mdsc;
3584 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3586 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3588 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
3591 static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
3592 struct ceph_msg_header *hdr, int *skip)
3594 struct ceph_msg *msg;
3595 int type = (int) le16_to_cpu(hdr->type);
3596 int front_len = (int) le32_to_cpu(hdr->front_len);
3602 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
3604 pr_err("unable to allocate msg type %d len %d\n",
3612 static const struct ceph_connection_operations mds_con_ops = {
3615 .dispatch = dispatch,
3616 .get_authorizer = get_authorizer,
3617 .verify_authorizer_reply = verify_authorizer_reply,
3618 .invalidate_authorizer = invalidate_authorizer,
3619 .peer_reset = peer_reset,
3620 .alloc_msg = mds_alloc_msg,