1 #include <linux/ceph/ceph_debug.h>
4 #include <linux/wait.h>
5 #include <linux/slab.h>
7 #include <linux/sched.h>
8 #include <linux/debugfs.h>
9 #include <linux/seq_file.h>
10 #include <linux/utsname.h>
13 #include "mds_client.h"
15 #include <linux/ceph/ceph_features.h>
16 #include <linux/ceph/messenger.h>
17 #include <linux/ceph/decode.h>
18 #include <linux/ceph/pagelist.h>
19 #include <linux/ceph/auth.h>
20 #include <linux/ceph/debugfs.h>
23 * A cluster of MDS (metadata server) daemons is responsible for
24 * managing the file system namespace (the directory hierarchy and
25 * inodes) and for coordinating shared access to storage. Metadata is
26 * partitioning hierarchically across a number of servers, and that
27 * partition varies over time as the cluster adjusts the distribution
28 * in order to balance load.
30 * The MDS client is primarily responsible to managing synchronous
31 * metadata requests for operations like open, unlink, and so forth.
32 * If there is a MDS failure, we find out about it when we (possibly
33 * request and) receive a new MDS map, and can resubmit affected
36 * For the most part, though, we take advantage of a lossless
37 * communications channel to the MDS, and do not need to worry about
38 * timing out or resubmitting requests.
40 * We maintain a stateful "session" with each MDS we interact with.
41 * Within each session, we sent periodic heartbeat messages to ensure
42 * any capabilities or leases we have been issues remain valid. If
43 * the session times out and goes stale, our leases and capabilities
44 * are no longer valid.
47 struct ceph_reconnect_state {
49 struct ceph_pagelist *pagelist;
53 static void __wake_requests(struct ceph_mds_client *mdsc,
54 struct list_head *head);
56 static const struct ceph_connection_operations mds_con_ops;
64 * parse individual inode info
66 static int parse_reply_info_in(void **p, void *end,
67 struct ceph_mds_reply_info_in *info,
73 *p += sizeof(struct ceph_mds_reply_inode) +
74 sizeof(*info->in->fragtree.splits) *
75 le32_to_cpu(info->in->fragtree.nsplits);
77 ceph_decode_32_safe(p, end, info->symlink_len, bad);
78 ceph_decode_need(p, end, info->symlink_len, bad);
80 *p += info->symlink_len;
82 if (features & CEPH_FEATURE_DIRLAYOUTHASH)
83 ceph_decode_copy_safe(p, end, &info->dir_layout,
84 sizeof(info->dir_layout), bad);
86 memset(&info->dir_layout, 0, sizeof(info->dir_layout));
88 ceph_decode_32_safe(p, end, info->xattr_len, bad);
89 ceph_decode_need(p, end, info->xattr_len, bad);
90 info->xattr_data = *p;
91 *p += info->xattr_len;
98 * parse a normal reply, which may contain a (dir+)dentry and/or a
101 static int parse_reply_info_trace(void **p, void *end,
102 struct ceph_mds_reply_info_parsed *info,
107 if (info->head->is_dentry) {
108 err = parse_reply_info_in(p, end, &info->diri, features);
112 if (unlikely(*p + sizeof(*info->dirfrag) > end))
115 *p += sizeof(*info->dirfrag) +
116 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
117 if (unlikely(*p > end))
120 ceph_decode_32_safe(p, end, info->dname_len, bad);
121 ceph_decode_need(p, end, info->dname_len, bad);
123 *p += info->dname_len;
125 *p += sizeof(*info->dlease);
128 if (info->head->is_target) {
129 err = parse_reply_info_in(p, end, &info->targeti, features);
134 if (unlikely(*p != end))
141 pr_err("problem parsing mds trace %d\n", err);
146 * parse readdir results
148 static int parse_reply_info_dir(void **p, void *end,
149 struct ceph_mds_reply_info_parsed *info,
156 if (*p + sizeof(*info->dir_dir) > end)
158 *p += sizeof(*info->dir_dir) +
159 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
163 ceph_decode_need(p, end, sizeof(num) + 2, bad);
164 num = ceph_decode_32(p);
165 info->dir_end = ceph_decode_8(p);
166 info->dir_complete = ceph_decode_8(p);
170 BUG_ON(!info->dir_in);
171 info->dir_dname = (void *)(info->dir_in + num);
172 info->dir_dname_len = (void *)(info->dir_dname + num);
173 info->dir_dlease = (void *)(info->dir_dname_len + num);
174 if ((unsigned long)(info->dir_dlease + num) >
175 (unsigned long)info->dir_in + info->dir_buf_size) {
176 pr_err("dir contents are larger than expected\n");
184 ceph_decode_need(p, end, sizeof(u32)*2, bad);
185 info->dir_dname_len[i] = ceph_decode_32(p);
186 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
187 info->dir_dname[i] = *p;
188 *p += info->dir_dname_len[i];
189 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
191 info->dir_dlease[i] = *p;
192 *p += sizeof(struct ceph_mds_reply_lease);
195 err = parse_reply_info_in(p, end, &info->dir_in[i], features);
210 pr_err("problem parsing dir contents %d\n", err);
215 * parse fcntl F_GETLK results
217 static int parse_reply_info_filelock(void **p, void *end,
218 struct ceph_mds_reply_info_parsed *info,
221 if (*p + sizeof(*info->filelock_reply) > end)
224 info->filelock_reply = *p;
225 *p += sizeof(*info->filelock_reply);
227 if (unlikely(*p != end))
236 * parse create results
238 static int parse_reply_info_create(void **p, void *end,
239 struct ceph_mds_reply_info_parsed *info,
242 if (features & CEPH_FEATURE_REPLY_CREATE_INODE) {
244 info->has_create_ino = false;
246 info->has_create_ino = true;
247 info->ino = ceph_decode_64(p);
251 if (unlikely(*p != end))
260 * parse extra results
262 static int parse_reply_info_extra(void **p, void *end,
263 struct ceph_mds_reply_info_parsed *info,
266 if (info->head->op == CEPH_MDS_OP_GETFILELOCK)
267 return parse_reply_info_filelock(p, end, info, features);
268 else if (info->head->op == CEPH_MDS_OP_READDIR ||
269 info->head->op == CEPH_MDS_OP_LSSNAP)
270 return parse_reply_info_dir(p, end, info, features);
271 else if (info->head->op == CEPH_MDS_OP_CREATE)
272 return parse_reply_info_create(p, end, info, features);
278 * parse entire mds reply
280 static int parse_reply_info(struct ceph_msg *msg,
281 struct ceph_mds_reply_info_parsed *info,
288 info->head = msg->front.iov_base;
289 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
290 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
293 ceph_decode_32_safe(&p, end, len, bad);
295 ceph_decode_need(&p, end, len, bad);
296 err = parse_reply_info_trace(&p, p+len, info, features);
302 ceph_decode_32_safe(&p, end, len, bad);
304 ceph_decode_need(&p, end, len, bad);
305 err = parse_reply_info_extra(&p, p+len, info, features);
311 ceph_decode_32_safe(&p, end, len, bad);
312 info->snapblob_len = len;
323 pr_err("mds parse_reply err %d\n", err);
327 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
331 free_pages((unsigned long)info->dir_in, get_order(info->dir_buf_size));
338 const char *ceph_session_state_name(int s)
341 case CEPH_MDS_SESSION_NEW: return "new";
342 case CEPH_MDS_SESSION_OPENING: return "opening";
343 case CEPH_MDS_SESSION_OPEN: return "open";
344 case CEPH_MDS_SESSION_HUNG: return "hung";
345 case CEPH_MDS_SESSION_CLOSING: return "closing";
346 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
347 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
348 default: return "???";
352 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
354 if (atomic_inc_not_zero(&s->s_ref)) {
355 dout("mdsc get_session %p %d -> %d\n", s,
356 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
359 dout("mdsc get_session %p 0 -- FAIL", s);
364 void ceph_put_mds_session(struct ceph_mds_session *s)
366 dout("mdsc put_session %p %d -> %d\n", s,
367 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
368 if (atomic_dec_and_test(&s->s_ref)) {
369 if (s->s_auth.authorizer)
370 ceph_auth_destroy_authorizer(
371 s->s_mdsc->fsc->client->monc.auth,
372 s->s_auth.authorizer);
378 * called under mdsc->mutex
380 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
383 struct ceph_mds_session *session;
385 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
387 session = mdsc->sessions[mds];
388 dout("lookup_mds_session %p %d\n", session,
389 atomic_read(&session->s_ref));
390 get_session(session);
394 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
396 if (mds >= mdsc->max_sessions)
398 return mdsc->sessions[mds];
401 static int __verify_registered_session(struct ceph_mds_client *mdsc,
402 struct ceph_mds_session *s)
404 if (s->s_mds >= mdsc->max_sessions ||
405 mdsc->sessions[s->s_mds] != s)
411 * create+register a new session for given mds.
412 * called under mdsc->mutex.
414 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
417 struct ceph_mds_session *s;
419 if (mds >= mdsc->mdsmap->m_max_mds)
420 return ERR_PTR(-EINVAL);
422 s = kzalloc(sizeof(*s), GFP_NOFS);
424 return ERR_PTR(-ENOMEM);
427 s->s_state = CEPH_MDS_SESSION_NEW;
430 mutex_init(&s->s_mutex);
432 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
434 spin_lock_init(&s->s_gen_ttl_lock);
436 s->s_cap_ttl = jiffies - 1;
438 spin_lock_init(&s->s_cap_lock);
439 s->s_renew_requested = 0;
441 INIT_LIST_HEAD(&s->s_caps);
444 atomic_set(&s->s_ref, 1);
445 INIT_LIST_HEAD(&s->s_waiting);
446 INIT_LIST_HEAD(&s->s_unsafe);
447 s->s_num_cap_releases = 0;
448 s->s_cap_reconnect = 0;
449 s->s_cap_iterator = NULL;
450 INIT_LIST_HEAD(&s->s_cap_releases);
451 INIT_LIST_HEAD(&s->s_cap_releases_done);
452 INIT_LIST_HEAD(&s->s_cap_flushing);
453 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
455 dout("register_session mds%d\n", mds);
456 if (mds >= mdsc->max_sessions) {
457 int newmax = 1 << get_count_order(mds+1);
458 struct ceph_mds_session **sa;
460 dout("register_session realloc to %d\n", newmax);
461 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
464 if (mdsc->sessions) {
465 memcpy(sa, mdsc->sessions,
466 mdsc->max_sessions * sizeof(void *));
467 kfree(mdsc->sessions);
470 mdsc->max_sessions = newmax;
472 mdsc->sessions[mds] = s;
473 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
475 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
476 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
482 return ERR_PTR(-ENOMEM);
486 * called under mdsc->mutex
488 static void __unregister_session(struct ceph_mds_client *mdsc,
489 struct ceph_mds_session *s)
491 dout("__unregister_session mds%d %p\n", s->s_mds, s);
492 BUG_ON(mdsc->sessions[s->s_mds] != s);
493 mdsc->sessions[s->s_mds] = NULL;
494 ceph_con_close(&s->s_con);
495 ceph_put_mds_session(s);
499 * drop session refs in request.
501 * should be last request ref, or hold mdsc->mutex
503 static void put_request_session(struct ceph_mds_request *req)
505 if (req->r_session) {
506 ceph_put_mds_session(req->r_session);
507 req->r_session = NULL;
511 void ceph_mdsc_release_request(struct kref *kref)
513 struct ceph_mds_request *req = container_of(kref,
514 struct ceph_mds_request,
516 destroy_reply_info(&req->r_reply_info);
518 ceph_msg_put(req->r_request);
520 ceph_msg_put(req->r_reply);
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);
547 ceph_pagelist_release(req->r_pagelist);
548 put_request_session(req);
549 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
554 * lookup session, bump ref if found.
556 * called under mdsc->mutex.
558 static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
561 struct ceph_mds_request *req;
562 struct rb_node *n = mdsc->request_tree.rb_node;
565 req = rb_entry(n, struct ceph_mds_request, r_node);
566 if (tid < req->r_tid)
568 else if (tid > req->r_tid)
571 ceph_mdsc_get_request(req);
578 static void __insert_request(struct ceph_mds_client *mdsc,
579 struct ceph_mds_request *new)
581 struct rb_node **p = &mdsc->request_tree.rb_node;
582 struct rb_node *parent = NULL;
583 struct ceph_mds_request *req = NULL;
587 req = rb_entry(parent, struct ceph_mds_request, r_node);
588 if (new->r_tid < req->r_tid)
590 else if (new->r_tid > req->r_tid)
596 rb_link_node(&new->r_node, parent, p);
597 rb_insert_color(&new->r_node, &mdsc->request_tree);
601 * Register an in-flight request, and assign a tid. Link to directory
602 * are modifying (if any).
604 * Called under mdsc->mutex.
606 static void __register_request(struct ceph_mds_client *mdsc,
607 struct ceph_mds_request *req,
610 req->r_tid = ++mdsc->last_tid;
612 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
614 dout("__register_request %p tid %lld\n", req, req->r_tid);
615 ceph_mdsc_get_request(req);
616 __insert_request(mdsc, req);
618 req->r_uid = current_fsuid();
619 req->r_gid = current_fsgid();
622 struct ceph_inode_info *ci = ceph_inode(dir);
625 spin_lock(&ci->i_unsafe_lock);
626 req->r_unsafe_dir = dir;
627 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
628 spin_unlock(&ci->i_unsafe_lock);
632 static void __unregister_request(struct ceph_mds_client *mdsc,
633 struct ceph_mds_request *req)
635 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
636 rb_erase(&req->r_node, &mdsc->request_tree);
637 RB_CLEAR_NODE(&req->r_node);
639 if (req->r_unsafe_dir) {
640 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
642 spin_lock(&ci->i_unsafe_lock);
643 list_del_init(&req->r_unsafe_dir_item);
644 spin_unlock(&ci->i_unsafe_lock);
646 iput(req->r_unsafe_dir);
647 req->r_unsafe_dir = NULL;
650 complete_all(&req->r_safe_completion);
652 ceph_mdsc_put_request(req);
656 * Choose mds to send request to next. If there is a hint set in the
657 * request (e.g., due to a prior forward hint from the mds), use that.
658 * Otherwise, consult frag tree and/or caps to identify the
659 * appropriate mds. If all else fails, choose randomly.
661 * Called under mdsc->mutex.
663 static struct dentry *get_nonsnap_parent(struct dentry *dentry)
666 * we don't need to worry about protecting the d_parent access
667 * here because we never renaming inside the snapped namespace
668 * except to resplice to another snapdir, and either the old or new
669 * result is a valid result.
671 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
672 dentry = dentry->d_parent;
676 static int __choose_mds(struct ceph_mds_client *mdsc,
677 struct ceph_mds_request *req)
680 struct ceph_inode_info *ci;
681 struct ceph_cap *cap;
682 int mode = req->r_direct_mode;
684 u32 hash = req->r_direct_hash;
685 bool is_hash = req->r_direct_is_hash;
688 * is there a specific mds we should try? ignore hint if we have
689 * no session and the mds is not up (active or recovering).
691 if (req->r_resend_mds >= 0 &&
692 (__have_session(mdsc, req->r_resend_mds) ||
693 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
694 dout("choose_mds using resend_mds mds%d\n",
696 return req->r_resend_mds;
699 if (mode == USE_RANDOM_MDS)
704 inode = req->r_inode;
705 } else if (req->r_dentry) {
706 /* ignore race with rename; old or new d_parent is okay */
707 struct dentry *parent = req->r_dentry->d_parent;
708 struct inode *dir = parent->d_inode;
710 if (dir->i_sb != mdsc->fsc->sb) {
712 inode = req->r_dentry->d_inode;
713 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
714 /* direct snapped/virtual snapdir requests
715 * based on parent dir inode */
716 struct dentry *dn = get_nonsnap_parent(parent);
718 dout("__choose_mds using nonsnap parent %p\n", inode);
721 inode = req->r_dentry->d_inode;
722 if (!inode || mode == USE_AUTH_MDS) {
725 hash = ceph_dentry_hash(dir, req->r_dentry);
731 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
735 ci = ceph_inode(inode);
737 if (is_hash && S_ISDIR(inode->i_mode)) {
738 struct ceph_inode_frag frag;
741 ceph_choose_frag(ci, hash, &frag, &found);
743 if (mode == USE_ANY_MDS && frag.ndist > 0) {
746 /* choose a random replica */
747 get_random_bytes(&r, 1);
750 dout("choose_mds %p %llx.%llx "
751 "frag %u mds%d (%d/%d)\n",
752 inode, ceph_vinop(inode),
755 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
756 CEPH_MDS_STATE_ACTIVE)
760 /* since this file/dir wasn't known to be
761 * replicated, then we want to look for the
762 * authoritative mds. */
765 /* choose auth mds */
767 dout("choose_mds %p %llx.%llx "
768 "frag %u mds%d (auth)\n",
769 inode, ceph_vinop(inode), frag.frag, mds);
770 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
771 CEPH_MDS_STATE_ACTIVE)
777 spin_lock(&ci->i_ceph_lock);
779 if (mode == USE_AUTH_MDS)
780 cap = ci->i_auth_cap;
781 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
782 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
784 spin_unlock(&ci->i_ceph_lock);
787 mds = cap->session->s_mds;
788 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
789 inode, ceph_vinop(inode), mds,
790 cap == ci->i_auth_cap ? "auth " : "", cap);
791 spin_unlock(&ci->i_ceph_lock);
795 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
796 dout("choose_mds chose random mds%d\n", mds);
804 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
806 struct ceph_msg *msg;
807 struct ceph_mds_session_head *h;
809 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
812 pr_err("create_session_msg ENOMEM creating msg\n");
815 h = msg->front.iov_base;
816 h->op = cpu_to_le32(op);
817 h->seq = cpu_to_le64(seq);
823 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
824 * to include additional client metadata fields.
826 static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
828 struct ceph_msg *msg;
829 struct ceph_mds_session_head *h;
831 int metadata_bytes = 0;
832 int metadata_key_count = 0;
833 struct ceph_options *opt = mdsc->fsc->client->options;
836 const char* metadata[3][2] = {
837 {"hostname", utsname()->nodename},
838 {"entity_id", opt->name ? opt->name : ""},
842 /* Calculate serialized length of metadata */
843 metadata_bytes = 4; /* map length */
844 for (i = 0; metadata[i][0] != NULL; ++i) {
845 metadata_bytes += 8 + strlen(metadata[i][0]) +
846 strlen(metadata[i][1]);
847 metadata_key_count++;
850 /* Allocate the message */
851 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + metadata_bytes,
854 pr_err("create_session_msg ENOMEM creating msg\n");
857 h = msg->front.iov_base;
858 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
859 h->seq = cpu_to_le64(seq);
862 * Serialize client metadata into waiting buffer space, using
863 * the format that userspace expects for map<string, string>
865 msg->hdr.version = 2; /* ClientSession messages with metadata are v2 */
867 /* The write pointer, following the session_head structure */
868 p = msg->front.iov_base + sizeof(*h);
870 /* Number of entries in the map */
871 ceph_encode_32(&p, metadata_key_count);
873 /* Two length-prefixed strings for each entry in the map */
874 for (i = 0; metadata[i][0] != NULL; ++i) {
875 size_t const key_len = strlen(metadata[i][0]);
876 size_t const val_len = strlen(metadata[i][1]);
878 ceph_encode_32(&p, key_len);
879 memcpy(p, metadata[i][0], key_len);
881 ceph_encode_32(&p, val_len);
882 memcpy(p, metadata[i][1], val_len);
890 * send session open request.
892 * called under mdsc->mutex
894 static int __open_session(struct ceph_mds_client *mdsc,
895 struct ceph_mds_session *session)
897 struct ceph_msg *msg;
899 int mds = session->s_mds;
901 /* wait for mds to go active? */
902 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
903 dout("open_session to mds%d (%s)\n", mds,
904 ceph_mds_state_name(mstate));
905 session->s_state = CEPH_MDS_SESSION_OPENING;
906 session->s_renew_requested = jiffies;
908 /* send connect message */
909 msg = create_session_open_msg(mdsc, session->s_seq);
912 ceph_con_send(&session->s_con, msg);
917 * open sessions for any export targets for the given mds
919 * called under mdsc->mutex
921 static struct ceph_mds_session *
922 __open_export_target_session(struct ceph_mds_client *mdsc, int target)
924 struct ceph_mds_session *session;
926 session = __ceph_lookup_mds_session(mdsc, target);
928 session = register_session(mdsc, target);
932 if (session->s_state == CEPH_MDS_SESSION_NEW ||
933 session->s_state == CEPH_MDS_SESSION_CLOSING)
934 __open_session(mdsc, session);
939 struct ceph_mds_session *
940 ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
942 struct ceph_mds_session *session;
944 dout("open_export_target_session to mds%d\n", target);
946 mutex_lock(&mdsc->mutex);
947 session = __open_export_target_session(mdsc, target);
948 mutex_unlock(&mdsc->mutex);
953 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
954 struct ceph_mds_session *session)
956 struct ceph_mds_info *mi;
957 struct ceph_mds_session *ts;
958 int i, mds = session->s_mds;
960 if (mds >= mdsc->mdsmap->m_max_mds)
963 mi = &mdsc->mdsmap->m_info[mds];
964 dout("open_export_target_sessions for mds%d (%d targets)\n",
965 session->s_mds, mi->num_export_targets);
967 for (i = 0; i < mi->num_export_targets; i++) {
968 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
970 ceph_put_mds_session(ts);
974 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
975 struct ceph_mds_session *session)
977 mutex_lock(&mdsc->mutex);
978 __open_export_target_sessions(mdsc, session);
979 mutex_unlock(&mdsc->mutex);
987 * Free preallocated cap messages assigned to this session
989 static void cleanup_cap_releases(struct ceph_mds_session *session)
991 struct ceph_msg *msg;
993 spin_lock(&session->s_cap_lock);
994 while (!list_empty(&session->s_cap_releases)) {
995 msg = list_first_entry(&session->s_cap_releases,
996 struct ceph_msg, list_head);
997 list_del_init(&msg->list_head);
1000 while (!list_empty(&session->s_cap_releases_done)) {
1001 msg = list_first_entry(&session->s_cap_releases_done,
1002 struct ceph_msg, list_head);
1003 list_del_init(&msg->list_head);
1006 spin_unlock(&session->s_cap_lock);
1010 * Helper to safely iterate over all caps associated with a session, with
1011 * special care taken to handle a racing __ceph_remove_cap().
1013 * Caller must hold session s_mutex.
1015 static int iterate_session_caps(struct ceph_mds_session *session,
1016 int (*cb)(struct inode *, struct ceph_cap *,
1019 struct list_head *p;
1020 struct ceph_cap *cap;
1021 struct inode *inode, *last_inode = NULL;
1022 struct ceph_cap *old_cap = NULL;
1025 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1026 spin_lock(&session->s_cap_lock);
1027 p = session->s_caps.next;
1028 while (p != &session->s_caps) {
1029 cap = list_entry(p, struct ceph_cap, session_caps);
1030 inode = igrab(&cap->ci->vfs_inode);
1035 session->s_cap_iterator = cap;
1036 spin_unlock(&session->s_cap_lock);
1043 ceph_put_cap(session->s_mdsc, old_cap);
1047 ret = cb(inode, cap, arg);
1050 spin_lock(&session->s_cap_lock);
1052 if (cap->ci == NULL) {
1053 dout("iterate_session_caps finishing cap %p removal\n",
1055 BUG_ON(cap->session != session);
1056 list_del_init(&cap->session_caps);
1057 session->s_nr_caps--;
1058 cap->session = NULL;
1059 old_cap = cap; /* put_cap it w/o locks held */
1066 session->s_cap_iterator = NULL;
1067 spin_unlock(&session->s_cap_lock);
1072 ceph_put_cap(session->s_mdsc, old_cap);
1077 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
1080 struct ceph_inode_info *ci = ceph_inode(inode);
1083 dout("removing cap %p, ci is %p, inode is %p\n",
1084 cap, ci, &ci->vfs_inode);
1085 spin_lock(&ci->i_ceph_lock);
1086 __ceph_remove_cap(cap, false);
1087 if (!__ceph_is_any_real_caps(ci)) {
1088 struct ceph_mds_client *mdsc =
1089 ceph_sb_to_client(inode->i_sb)->mdsc;
1091 spin_lock(&mdsc->cap_dirty_lock);
1092 if (!list_empty(&ci->i_dirty_item)) {
1093 pr_info(" dropping dirty %s state for %p %lld\n",
1094 ceph_cap_string(ci->i_dirty_caps),
1095 inode, ceph_ino(inode));
1096 ci->i_dirty_caps = 0;
1097 list_del_init(&ci->i_dirty_item);
1100 if (!list_empty(&ci->i_flushing_item)) {
1101 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
1102 ceph_cap_string(ci->i_flushing_caps),
1103 inode, ceph_ino(inode));
1104 ci->i_flushing_caps = 0;
1105 list_del_init(&ci->i_flushing_item);
1106 mdsc->num_cap_flushing--;
1109 if (drop && ci->i_wrbuffer_ref) {
1110 pr_info(" dropping dirty data for %p %lld\n",
1111 inode, ceph_ino(inode));
1112 ci->i_wrbuffer_ref = 0;
1113 ci->i_wrbuffer_ref_head = 0;
1116 spin_unlock(&mdsc->cap_dirty_lock);
1118 spin_unlock(&ci->i_ceph_lock);
1125 * caller must hold session s_mutex
1127 static void remove_session_caps(struct ceph_mds_session *session)
1129 dout("remove_session_caps on %p\n", session);
1130 iterate_session_caps(session, remove_session_caps_cb, NULL);
1132 spin_lock(&session->s_cap_lock);
1133 if (session->s_nr_caps > 0) {
1134 struct super_block *sb = session->s_mdsc->fsc->sb;
1135 struct inode *inode;
1136 struct ceph_cap *cap, *prev = NULL;
1137 struct ceph_vino vino;
1139 * iterate_session_caps() skips inodes that are being
1140 * deleted, we need to wait until deletions are complete.
1141 * __wait_on_freeing_inode() is designed for the job,
1142 * but it is not exported, so use lookup inode function
1145 while (!list_empty(&session->s_caps)) {
1146 cap = list_entry(session->s_caps.next,
1147 struct ceph_cap, session_caps);
1151 vino = cap->ci->i_vino;
1152 spin_unlock(&session->s_cap_lock);
1154 inode = ceph_find_inode(sb, vino);
1157 spin_lock(&session->s_cap_lock);
1160 spin_unlock(&session->s_cap_lock);
1162 BUG_ON(session->s_nr_caps > 0);
1163 BUG_ON(!list_empty(&session->s_cap_flushing));
1164 cleanup_cap_releases(session);
1168 * wake up any threads waiting on this session's caps. if the cap is
1169 * old (didn't get renewed on the client reconnect), remove it now.
1171 * caller must hold s_mutex.
1173 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1176 struct ceph_inode_info *ci = ceph_inode(inode);
1178 wake_up_all(&ci->i_cap_wq);
1180 spin_lock(&ci->i_ceph_lock);
1181 ci->i_wanted_max_size = 0;
1182 ci->i_requested_max_size = 0;
1183 spin_unlock(&ci->i_ceph_lock);
1188 static void wake_up_session_caps(struct ceph_mds_session *session,
1191 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1192 iterate_session_caps(session, wake_up_session_cb,
1193 (void *)(unsigned long)reconnect);
1197 * Send periodic message to MDS renewing all currently held caps. The
1198 * ack will reset the expiration for all caps from this session.
1200 * caller holds s_mutex
1202 static int send_renew_caps(struct ceph_mds_client *mdsc,
1203 struct ceph_mds_session *session)
1205 struct ceph_msg *msg;
1208 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1209 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1210 pr_info("mds%d caps stale\n", session->s_mds);
1211 session->s_renew_requested = jiffies;
1213 /* do not try to renew caps until a recovering mds has reconnected
1214 * with its clients. */
1215 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1216 if (state < CEPH_MDS_STATE_RECONNECT) {
1217 dout("send_renew_caps ignoring mds%d (%s)\n",
1218 session->s_mds, ceph_mds_state_name(state));
1222 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1223 ceph_mds_state_name(state));
1224 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1225 ++session->s_renew_seq);
1228 ceph_con_send(&session->s_con, msg);
1232 static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1233 struct ceph_mds_session *session, u64 seq)
1235 struct ceph_msg *msg;
1237 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1238 session->s_mds, ceph_session_state_name(session->s_state), seq);
1239 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1242 ceph_con_send(&session->s_con, msg);
1248 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1250 * Called under session->s_mutex
1252 static void renewed_caps(struct ceph_mds_client *mdsc,
1253 struct ceph_mds_session *session, int is_renew)
1258 spin_lock(&session->s_cap_lock);
1259 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
1261 session->s_cap_ttl = session->s_renew_requested +
1262 mdsc->mdsmap->m_session_timeout*HZ;
1265 if (time_before(jiffies, session->s_cap_ttl)) {
1266 pr_info("mds%d caps renewed\n", session->s_mds);
1269 pr_info("mds%d caps still stale\n", session->s_mds);
1272 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1273 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1274 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1275 spin_unlock(&session->s_cap_lock);
1278 wake_up_session_caps(session, 0);
1282 * send a session close request
1284 static int request_close_session(struct ceph_mds_client *mdsc,
1285 struct ceph_mds_session *session)
1287 struct ceph_msg *msg;
1289 dout("request_close_session mds%d state %s seq %lld\n",
1290 session->s_mds, ceph_session_state_name(session->s_state),
1292 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1295 ceph_con_send(&session->s_con, msg);
1300 * Called with s_mutex held.
1302 static int __close_session(struct ceph_mds_client *mdsc,
1303 struct ceph_mds_session *session)
1305 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1307 session->s_state = CEPH_MDS_SESSION_CLOSING;
1308 return request_close_session(mdsc, session);
1312 * Trim old(er) caps.
1314 * Because we can't cache an inode without one or more caps, we do
1315 * this indirectly: if a cap is unused, we prune its aliases, at which
1316 * point the inode will hopefully get dropped to.
1318 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1319 * memory pressure from the MDS, though, so it needn't be perfect.
1321 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1323 struct ceph_mds_session *session = arg;
1324 struct ceph_inode_info *ci = ceph_inode(inode);
1325 int used, wanted, oissued, mine;
1327 if (session->s_trim_caps <= 0)
1330 spin_lock(&ci->i_ceph_lock);
1331 mine = cap->issued | cap->implemented;
1332 used = __ceph_caps_used(ci);
1333 wanted = __ceph_caps_file_wanted(ci);
1334 oissued = __ceph_caps_issued_other(ci, cap);
1336 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1337 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1338 ceph_cap_string(used), ceph_cap_string(wanted));
1339 if (cap == ci->i_auth_cap) {
1340 if (ci->i_dirty_caps | ci->i_flushing_caps)
1342 if ((used | wanted) & CEPH_CAP_ANY_WR)
1345 if ((used | wanted) & ~oissued & mine)
1346 goto out; /* we need these caps */
1348 session->s_trim_caps--;
1350 /* we aren't the only cap.. just remove us */
1351 __ceph_remove_cap(cap, true);
1353 /* try to drop referring dentries */
1354 spin_unlock(&ci->i_ceph_lock);
1355 d_prune_aliases(inode);
1356 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1357 inode, cap, atomic_read(&inode->i_count));
1362 spin_unlock(&ci->i_ceph_lock);
1367 * Trim session cap count down to some max number.
1369 static int trim_caps(struct ceph_mds_client *mdsc,
1370 struct ceph_mds_session *session,
1373 int trim_caps = session->s_nr_caps - max_caps;
1375 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1376 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1377 if (trim_caps > 0) {
1378 session->s_trim_caps = trim_caps;
1379 iterate_session_caps(session, trim_caps_cb, session);
1380 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1381 session->s_mds, session->s_nr_caps, max_caps,
1382 trim_caps - session->s_trim_caps);
1383 session->s_trim_caps = 0;
1386 ceph_add_cap_releases(mdsc, session);
1387 ceph_send_cap_releases(mdsc, session);
1392 * Allocate cap_release messages. If there is a partially full message
1393 * in the queue, try to allocate enough to cover it's remainder, so that
1394 * we can send it immediately.
1396 * Called under s_mutex.
1398 int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
1399 struct ceph_mds_session *session)
1401 struct ceph_msg *msg, *partial = NULL;
1402 struct ceph_mds_cap_release *head;
1404 int extra = mdsc->fsc->mount_options->cap_release_safety;
1407 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1410 spin_lock(&session->s_cap_lock);
1412 if (!list_empty(&session->s_cap_releases)) {
1413 msg = list_first_entry(&session->s_cap_releases,
1416 head = msg->front.iov_base;
1417 num = le32_to_cpu(head->num);
1419 dout(" partial %p with (%d/%d)\n", msg, num,
1420 (int)CEPH_CAPS_PER_RELEASE);
1421 extra += CEPH_CAPS_PER_RELEASE - num;
1425 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1426 spin_unlock(&session->s_cap_lock);
1427 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1431 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1432 (int)msg->front.iov_len);
1433 head = msg->front.iov_base;
1434 head->num = cpu_to_le32(0);
1435 msg->front.iov_len = sizeof(*head);
1436 spin_lock(&session->s_cap_lock);
1437 list_add(&msg->list_head, &session->s_cap_releases);
1438 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1442 head = partial->front.iov_base;
1443 num = le32_to_cpu(head->num);
1444 dout(" queueing partial %p with %d/%d\n", partial, num,
1445 (int)CEPH_CAPS_PER_RELEASE);
1446 list_move_tail(&partial->list_head,
1447 &session->s_cap_releases_done);
1448 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
1451 spin_unlock(&session->s_cap_lock);
1457 * flush all dirty inode data to disk.
1459 * returns true if we've flushed through want_flush_seq
1461 static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1465 dout("check_cap_flush want %lld\n", want_flush_seq);
1466 mutex_lock(&mdsc->mutex);
1467 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1468 struct ceph_mds_session *session = mdsc->sessions[mds];
1472 get_session(session);
1473 mutex_unlock(&mdsc->mutex);
1475 mutex_lock(&session->s_mutex);
1476 if (!list_empty(&session->s_cap_flushing)) {
1477 struct ceph_inode_info *ci =
1478 list_entry(session->s_cap_flushing.next,
1479 struct ceph_inode_info,
1481 struct inode *inode = &ci->vfs_inode;
1483 spin_lock(&ci->i_ceph_lock);
1484 if (ci->i_cap_flush_seq <= want_flush_seq) {
1485 dout("check_cap_flush still flushing %p "
1486 "seq %lld <= %lld to mds%d\n", inode,
1487 ci->i_cap_flush_seq, want_flush_seq,
1491 spin_unlock(&ci->i_ceph_lock);
1493 mutex_unlock(&session->s_mutex);
1494 ceph_put_mds_session(session);
1498 mutex_lock(&mdsc->mutex);
1501 mutex_unlock(&mdsc->mutex);
1502 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1507 * called under s_mutex
1509 void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1510 struct ceph_mds_session *session)
1512 struct ceph_msg *msg;
1514 dout("send_cap_releases mds%d\n", session->s_mds);
1515 spin_lock(&session->s_cap_lock);
1516 while (!list_empty(&session->s_cap_releases_done)) {
1517 msg = list_first_entry(&session->s_cap_releases_done,
1518 struct ceph_msg, list_head);
1519 list_del_init(&msg->list_head);
1520 spin_unlock(&session->s_cap_lock);
1521 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1522 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1523 ceph_con_send(&session->s_con, msg);
1524 spin_lock(&session->s_cap_lock);
1526 spin_unlock(&session->s_cap_lock);
1529 static void discard_cap_releases(struct ceph_mds_client *mdsc,
1530 struct ceph_mds_session *session)
1532 struct ceph_msg *msg;
1533 struct ceph_mds_cap_release *head;
1536 dout("discard_cap_releases mds%d\n", session->s_mds);
1538 if (!list_empty(&session->s_cap_releases)) {
1539 /* zero out the in-progress message */
1540 msg = list_first_entry(&session->s_cap_releases,
1541 struct ceph_msg, list_head);
1542 head = msg->front.iov_base;
1543 num = le32_to_cpu(head->num);
1544 dout("discard_cap_releases mds%d %p %u\n",
1545 session->s_mds, msg, num);
1546 head->num = cpu_to_le32(0);
1547 msg->front.iov_len = sizeof(*head);
1548 session->s_num_cap_releases += num;
1551 /* requeue completed messages */
1552 while (!list_empty(&session->s_cap_releases_done)) {
1553 msg = list_first_entry(&session->s_cap_releases_done,
1554 struct ceph_msg, list_head);
1555 list_del_init(&msg->list_head);
1557 head = msg->front.iov_base;
1558 num = le32_to_cpu(head->num);
1559 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1561 session->s_num_cap_releases += num;
1562 head->num = cpu_to_le32(0);
1563 msg->front.iov_len = sizeof(*head);
1564 list_add(&msg->list_head, &session->s_cap_releases);
1572 int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1575 struct ceph_inode_info *ci = ceph_inode(dir);
1576 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1577 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
1578 size_t size = sizeof(*rinfo->dir_in) + sizeof(*rinfo->dir_dname_len) +
1579 sizeof(*rinfo->dir_dname) + sizeof(*rinfo->dir_dlease);
1580 int order, num_entries;
1582 spin_lock(&ci->i_ceph_lock);
1583 num_entries = ci->i_files + ci->i_subdirs;
1584 spin_unlock(&ci->i_ceph_lock);
1585 num_entries = max(num_entries, 1);
1586 num_entries = min(num_entries, opt->max_readdir);
1588 order = get_order(size * num_entries);
1589 while (order >= 0) {
1590 rinfo->dir_in = (void*)__get_free_pages(GFP_NOFS | __GFP_NOWARN,
1599 num_entries = (PAGE_SIZE << order) / size;
1600 num_entries = min(num_entries, opt->max_readdir);
1602 rinfo->dir_buf_size = PAGE_SIZE << order;
1603 req->r_num_caps = num_entries + 1;
1604 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1605 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1610 * Create an mds request.
1612 struct ceph_mds_request *
1613 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1615 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1618 return ERR_PTR(-ENOMEM);
1620 mutex_init(&req->r_fill_mutex);
1622 req->r_started = jiffies;
1623 req->r_resend_mds = -1;
1624 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1626 kref_init(&req->r_kref);
1627 INIT_LIST_HEAD(&req->r_wait);
1628 init_completion(&req->r_completion);
1629 init_completion(&req->r_safe_completion);
1630 INIT_LIST_HEAD(&req->r_unsafe_item);
1632 req->r_stamp = CURRENT_TIME;
1635 req->r_direct_mode = mode;
1640 * return oldest (lowest) request, tid in request tree, 0 if none.
1642 * called under mdsc->mutex.
1644 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1646 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1648 return rb_entry(rb_first(&mdsc->request_tree),
1649 struct ceph_mds_request, r_node);
1652 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1654 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1662 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1663 * on build_path_from_dentry in fs/cifs/dir.c.
1665 * If @stop_on_nosnap, generate path relative to the first non-snapped
1668 * Encode hidden .snap dirs as a double /, i.e.
1669 * foo/.snap/bar -> foo//bar
1671 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1674 struct dentry *temp;
1680 return ERR_PTR(-EINVAL);
1684 seq = read_seqbegin(&rename_lock);
1686 for (temp = dentry; !IS_ROOT(temp);) {
1687 struct inode *inode = temp->d_inode;
1688 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1689 len++; /* slash only */
1690 else if (stop_on_nosnap && inode &&
1691 ceph_snap(inode) == CEPH_NOSNAP)
1694 len += 1 + temp->d_name.len;
1695 temp = temp->d_parent;
1699 len--; /* no leading '/' */
1701 path = kmalloc(len+1, GFP_NOFS);
1703 return ERR_PTR(-ENOMEM);
1705 path[pos] = 0; /* trailing null */
1707 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1708 struct inode *inode;
1710 spin_lock(&temp->d_lock);
1711 inode = temp->d_inode;
1712 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1713 dout("build_path path+%d: %p SNAPDIR\n",
1715 } else if (stop_on_nosnap && inode &&
1716 ceph_snap(inode) == CEPH_NOSNAP) {
1717 spin_unlock(&temp->d_lock);
1720 pos -= temp->d_name.len;
1722 spin_unlock(&temp->d_lock);
1725 strncpy(path + pos, temp->d_name.name,
1728 spin_unlock(&temp->d_lock);
1731 temp = temp->d_parent;
1734 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
1735 pr_err("build_path did not end path lookup where "
1736 "expected, namelen is %d, pos is %d\n", len, pos);
1737 /* presumably this is only possible if racing with a
1738 rename of one of the parent directories (we can not
1739 lock the dentries above us to prevent this, but
1740 retrying should be harmless) */
1745 *base = ceph_ino(temp->d_inode);
1747 dout("build_path on %p %d built %llx '%.*s'\n",
1748 dentry, d_count(dentry), *base, len, path);
1752 static int build_dentry_path(struct dentry *dentry,
1753 const char **ppath, int *ppathlen, u64 *pino,
1758 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1759 *pino = ceph_ino(dentry->d_parent->d_inode);
1760 *ppath = dentry->d_name.name;
1761 *ppathlen = dentry->d_name.len;
1764 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1766 return PTR_ERR(path);
1772 static int build_inode_path(struct inode *inode,
1773 const char **ppath, int *ppathlen, u64 *pino,
1776 struct dentry *dentry;
1779 if (ceph_snap(inode) == CEPH_NOSNAP) {
1780 *pino = ceph_ino(inode);
1784 dentry = d_find_alias(inode);
1785 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1788 return PTR_ERR(path);
1795 * request arguments may be specified via an inode *, a dentry *, or
1796 * an explicit ino+path.
1798 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1799 const char *rpath, u64 rino,
1800 const char **ppath, int *pathlen,
1801 u64 *ino, int *freepath)
1806 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1807 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1809 } else if (rdentry) {
1810 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1811 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1813 } else if (rpath || rino) {
1816 *pathlen = rpath ? strlen(rpath) : 0;
1817 dout(" path %.*s\n", *pathlen, rpath);
1824 * called under mdsc->mutex
1826 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1827 struct ceph_mds_request *req,
1830 struct ceph_msg *msg;
1831 struct ceph_mds_request_head *head;
1832 const char *path1 = NULL;
1833 const char *path2 = NULL;
1834 u64 ino1 = 0, ino2 = 0;
1835 int pathlen1 = 0, pathlen2 = 0;
1836 int freepath1 = 0, freepath2 = 0;
1842 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1843 req->r_path1, req->r_ino1.ino,
1844 &path1, &pathlen1, &ino1, &freepath1);
1850 ret = set_request_path_attr(NULL, req->r_old_dentry,
1851 req->r_path2, req->r_ino2.ino,
1852 &path2, &pathlen2, &ino2, &freepath2);
1858 len = sizeof(*head) +
1859 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
1860 sizeof(struct timespec);
1862 /* calculate (max) length for cap releases */
1863 len += sizeof(struct ceph_mds_request_release) *
1864 (!!req->r_inode_drop + !!req->r_dentry_drop +
1865 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1866 if (req->r_dentry_drop)
1867 len += req->r_dentry->d_name.len;
1868 if (req->r_old_dentry_drop)
1869 len += req->r_old_dentry->d_name.len;
1871 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
1873 msg = ERR_PTR(-ENOMEM);
1877 msg->hdr.version = 2;
1878 msg->hdr.tid = cpu_to_le64(req->r_tid);
1880 head = msg->front.iov_base;
1881 p = msg->front.iov_base + sizeof(*head);
1882 end = msg->front.iov_base + msg->front.iov_len;
1884 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1885 head->op = cpu_to_le32(req->r_op);
1886 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
1887 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
1888 head->args = req->r_args;
1890 ceph_encode_filepath(&p, end, ino1, path1);
1891 ceph_encode_filepath(&p, end, ino2, path2);
1893 /* make note of release offset, in case we need to replay */
1894 req->r_request_release_offset = p - msg->front.iov_base;
1898 if (req->r_inode_drop)
1899 releases += ceph_encode_inode_release(&p,
1900 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1901 mds, req->r_inode_drop, req->r_inode_unless, 0);
1902 if (req->r_dentry_drop)
1903 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1904 mds, req->r_dentry_drop, req->r_dentry_unless);
1905 if (req->r_old_dentry_drop)
1906 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1907 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1908 if (req->r_old_inode_drop)
1909 releases += ceph_encode_inode_release(&p,
1910 req->r_old_dentry->d_inode,
1911 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1912 head->num_releases = cpu_to_le16(releases);
1915 ceph_encode_copy(&p, &req->r_stamp, sizeof(req->r_stamp));
1918 msg->front.iov_len = p - msg->front.iov_base;
1919 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1921 if (req->r_pagelist) {
1922 struct ceph_pagelist *pagelist = req->r_pagelist;
1923 atomic_inc(&pagelist->refcnt);
1924 ceph_msg_data_add_pagelist(msg, pagelist);
1925 msg->hdr.data_len = cpu_to_le32(pagelist->length);
1927 msg->hdr.data_len = 0;
1930 msg->hdr.data_off = cpu_to_le16(0);
1934 kfree((char *)path2);
1937 kfree((char *)path1);
1943 * called under mdsc->mutex if error, under no mutex if
1946 static void complete_request(struct ceph_mds_client *mdsc,
1947 struct ceph_mds_request *req)
1949 if (req->r_callback)
1950 req->r_callback(mdsc, req);
1952 complete_all(&req->r_completion);
1956 * called under mdsc->mutex
1958 static int __prepare_send_request(struct ceph_mds_client *mdsc,
1959 struct ceph_mds_request *req,
1962 struct ceph_mds_request_head *rhead;
1963 struct ceph_msg *msg;
1968 struct ceph_cap *cap =
1969 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1972 req->r_sent_on_mseq = cap->mseq;
1974 req->r_sent_on_mseq = -1;
1976 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1977 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1979 if (req->r_got_unsafe) {
1982 * Replay. Do not regenerate message (and rebuild
1983 * paths, etc.); just use the original message.
1984 * Rebuilding paths will break for renames because
1985 * d_move mangles the src name.
1987 msg = req->r_request;
1988 rhead = msg->front.iov_base;
1990 flags = le32_to_cpu(rhead->flags);
1991 flags |= CEPH_MDS_FLAG_REPLAY;
1992 rhead->flags = cpu_to_le32(flags);
1994 if (req->r_target_inode)
1995 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1997 rhead->num_retry = req->r_attempts - 1;
1999 /* remove cap/dentry releases from message */
2000 rhead->num_releases = 0;
2003 p = msg->front.iov_base + req->r_request_release_offset;
2004 ceph_encode_copy(&p, &req->r_stamp, sizeof(req->r_stamp));
2006 msg->front.iov_len = p - msg->front.iov_base;
2007 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2011 if (req->r_request) {
2012 ceph_msg_put(req->r_request);
2013 req->r_request = NULL;
2015 msg = create_request_message(mdsc, req, mds);
2017 req->r_err = PTR_ERR(msg);
2018 complete_request(mdsc, req);
2019 return PTR_ERR(msg);
2021 req->r_request = msg;
2023 rhead = msg->front.iov_base;
2024 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
2025 if (req->r_got_unsafe)
2026 flags |= CEPH_MDS_FLAG_REPLAY;
2027 if (req->r_locked_dir)
2028 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2029 rhead->flags = cpu_to_le32(flags);
2030 rhead->num_fwd = req->r_num_fwd;
2031 rhead->num_retry = req->r_attempts - 1;
2034 dout(" r_locked_dir = %p\n", req->r_locked_dir);
2039 * send request, or put it on the appropriate wait list.
2041 static int __do_request(struct ceph_mds_client *mdsc,
2042 struct ceph_mds_request *req)
2044 struct ceph_mds_session *session = NULL;
2048 if (req->r_err || req->r_got_result) {
2050 __unregister_request(mdsc, req);
2054 if (req->r_timeout &&
2055 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2056 dout("do_request timed out\n");
2061 put_request_session(req);
2063 mds = __choose_mds(mdsc, req);
2065 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2066 dout("do_request no mds or not active, waiting for map\n");
2067 list_add(&req->r_wait, &mdsc->waiting_for_map);
2071 /* get, open session */
2072 session = __ceph_lookup_mds_session(mdsc, mds);
2074 session = register_session(mdsc, mds);
2075 if (IS_ERR(session)) {
2076 err = PTR_ERR(session);
2080 req->r_session = get_session(session);
2082 dout("do_request mds%d session %p state %s\n", mds, session,
2083 ceph_session_state_name(session->s_state));
2084 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2085 session->s_state != CEPH_MDS_SESSION_HUNG) {
2086 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2087 session->s_state == CEPH_MDS_SESSION_CLOSING)
2088 __open_session(mdsc, session);
2089 list_add(&req->r_wait, &session->s_waiting);
2094 req->r_resend_mds = -1; /* forget any previous mds hint */
2096 if (req->r_request_started == 0) /* note request start time */
2097 req->r_request_started = jiffies;
2099 err = __prepare_send_request(mdsc, req, mds);
2101 ceph_msg_get(req->r_request);
2102 ceph_con_send(&session->s_con, req->r_request);
2106 ceph_put_mds_session(session);
2112 complete_request(mdsc, req);
2117 * called under mdsc->mutex
2119 static void __wake_requests(struct ceph_mds_client *mdsc,
2120 struct list_head *head)
2122 struct ceph_mds_request *req;
2123 LIST_HEAD(tmp_list);
2125 list_splice_init(head, &tmp_list);
2127 while (!list_empty(&tmp_list)) {
2128 req = list_entry(tmp_list.next,
2129 struct ceph_mds_request, r_wait);
2130 list_del_init(&req->r_wait);
2131 dout(" wake request %p tid %llu\n", req, req->r_tid);
2132 __do_request(mdsc, req);
2137 * Wake up threads with requests pending for @mds, so that they can
2138 * resubmit their requests to a possibly different mds.
2140 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
2142 struct ceph_mds_request *req;
2143 struct rb_node *p = rb_first(&mdsc->request_tree);
2145 dout("kick_requests mds%d\n", mds);
2147 req = rb_entry(p, struct ceph_mds_request, r_node);
2149 if (req->r_got_unsafe)
2151 if (req->r_session &&
2152 req->r_session->s_mds == mds) {
2153 dout(" kicking tid %llu\n", req->r_tid);
2154 list_del_init(&req->r_wait);
2155 __do_request(mdsc, req);
2160 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2161 struct ceph_mds_request *req)
2163 dout("submit_request on %p\n", req);
2164 mutex_lock(&mdsc->mutex);
2165 __register_request(mdsc, req, NULL);
2166 __do_request(mdsc, req);
2167 mutex_unlock(&mdsc->mutex);
2171 * Synchrously perform an mds request. Take care of all of the
2172 * session setup, forwarding, retry details.
2174 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2176 struct ceph_mds_request *req)
2180 dout("do_request on %p\n", req);
2182 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
2184 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
2185 if (req->r_locked_dir)
2186 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
2187 if (req->r_old_dentry_dir)
2188 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2192 mutex_lock(&mdsc->mutex);
2193 __register_request(mdsc, req, dir);
2194 __do_request(mdsc, req);
2198 __unregister_request(mdsc, req);
2199 dout("do_request early error %d\n", err);
2204 mutex_unlock(&mdsc->mutex);
2205 dout("do_request waiting\n");
2206 if (req->r_timeout) {
2207 err = (long)wait_for_completion_killable_timeout(
2208 &req->r_completion, req->r_timeout);
2212 err = wait_for_completion_killable(&req->r_completion);
2214 dout("do_request waited, got %d\n", err);
2215 mutex_lock(&mdsc->mutex);
2217 /* only abort if we didn't race with a real reply */
2218 if (req->r_got_result) {
2219 err = le32_to_cpu(req->r_reply_info.head->result);
2220 } else if (err < 0) {
2221 dout("aborted request %lld with %d\n", req->r_tid, err);
2224 * ensure we aren't running concurrently with
2225 * ceph_fill_trace or ceph_readdir_prepopulate, which
2226 * rely on locks (dir mutex) held by our caller.
2228 mutex_lock(&req->r_fill_mutex);
2230 req->r_aborted = true;
2231 mutex_unlock(&req->r_fill_mutex);
2233 if (req->r_locked_dir &&
2234 (req->r_op & CEPH_MDS_OP_WRITE))
2235 ceph_invalidate_dir_request(req);
2241 mutex_unlock(&mdsc->mutex);
2242 dout("do_request %p done, result %d\n", req, err);
2247 * Invalidate dir's completeness, dentry lease state on an aborted MDS
2248 * namespace request.
2250 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2252 struct inode *inode = req->r_locked_dir;
2254 dout("invalidate_dir_request %p (complete, lease(s))\n", inode);
2256 ceph_dir_clear_complete(inode);
2258 ceph_invalidate_dentry_lease(req->r_dentry);
2259 if (req->r_old_dentry)
2260 ceph_invalidate_dentry_lease(req->r_old_dentry);
2266 * We take the session mutex and parse and process the reply immediately.
2267 * This preserves the logical ordering of replies, capabilities, etc., sent
2268 * by the MDS as they are applied to our local cache.
2270 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2272 struct ceph_mds_client *mdsc = session->s_mdsc;
2273 struct ceph_mds_request *req;
2274 struct ceph_mds_reply_head *head = msg->front.iov_base;
2275 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2278 int mds = session->s_mds;
2280 if (msg->front.iov_len < sizeof(*head)) {
2281 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2286 /* get request, session */
2287 tid = le64_to_cpu(msg->hdr.tid);
2288 mutex_lock(&mdsc->mutex);
2289 req = __lookup_request(mdsc, tid);
2291 dout("handle_reply on unknown tid %llu\n", tid);
2292 mutex_unlock(&mdsc->mutex);
2295 dout("handle_reply %p\n", req);
2297 /* correct session? */
2298 if (req->r_session != session) {
2299 pr_err("mdsc_handle_reply got %llu on session mds%d"
2300 " not mds%d\n", tid, session->s_mds,
2301 req->r_session ? req->r_session->s_mds : -1);
2302 mutex_unlock(&mdsc->mutex);
2307 if ((req->r_got_unsafe && !head->safe) ||
2308 (req->r_got_safe && head->safe)) {
2309 pr_warn("got a dup %s reply on %llu from mds%d\n",
2310 head->safe ? "safe" : "unsafe", tid, mds);
2311 mutex_unlock(&mdsc->mutex);
2314 if (req->r_got_safe && !head->safe) {
2315 pr_warn("got unsafe after safe on %llu from mds%d\n",
2317 mutex_unlock(&mdsc->mutex);
2321 result = le32_to_cpu(head->result);
2325 * if we're not talking to the authority, send to them
2326 * if the authority has changed while we weren't looking,
2327 * send to new authority
2328 * Otherwise we just have to return an ESTALE
2330 if (result == -ESTALE) {
2331 dout("got ESTALE on request %llu", req->r_tid);
2332 req->r_resend_mds = -1;
2333 if (req->r_direct_mode != USE_AUTH_MDS) {
2334 dout("not using auth, setting for that now");
2335 req->r_direct_mode = USE_AUTH_MDS;
2336 __do_request(mdsc, req);
2337 mutex_unlock(&mdsc->mutex);
2340 int mds = __choose_mds(mdsc, req);
2341 if (mds >= 0 && mds != req->r_session->s_mds) {
2342 dout("but auth changed, so resending");
2343 __do_request(mdsc, req);
2344 mutex_unlock(&mdsc->mutex);
2348 dout("have to return ESTALE on request %llu", req->r_tid);
2353 req->r_got_safe = true;
2354 __unregister_request(mdsc, req);
2356 if (req->r_got_unsafe) {
2358 * We already handled the unsafe response, now do the
2359 * cleanup. No need to examine the response; the MDS
2360 * doesn't include any result info in the safe
2361 * response. And even if it did, there is nothing
2362 * useful we could do with a revised return value.
2364 dout("got safe reply %llu, mds%d\n", tid, mds);
2365 list_del_init(&req->r_unsafe_item);
2367 /* last unsafe request during umount? */
2368 if (mdsc->stopping && !__get_oldest_req(mdsc))
2369 complete_all(&mdsc->safe_umount_waiters);
2370 mutex_unlock(&mdsc->mutex);
2374 req->r_got_unsafe = true;
2375 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2378 dout("handle_reply tid %lld result %d\n", tid, result);
2379 rinfo = &req->r_reply_info;
2380 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2381 mutex_unlock(&mdsc->mutex);
2383 mutex_lock(&session->s_mutex);
2385 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
2391 if (rinfo->snapblob_len) {
2392 down_write(&mdsc->snap_rwsem);
2393 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2394 rinfo->snapblob + rinfo->snapblob_len,
2395 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2396 downgrade_write(&mdsc->snap_rwsem);
2398 down_read(&mdsc->snap_rwsem);
2401 /* insert trace into our cache */
2402 mutex_lock(&req->r_fill_mutex);
2403 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
2405 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
2406 req->r_op == CEPH_MDS_OP_LSSNAP))
2407 ceph_readdir_prepopulate(req, req->r_session);
2408 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2410 mutex_unlock(&req->r_fill_mutex);
2412 up_read(&mdsc->snap_rwsem);
2414 mutex_lock(&mdsc->mutex);
2415 if (!req->r_aborted) {
2421 req->r_got_result = true;
2424 dout("reply arrived after request %lld was aborted\n", tid);
2426 mutex_unlock(&mdsc->mutex);
2428 ceph_add_cap_releases(mdsc, req->r_session);
2429 mutex_unlock(&session->s_mutex);
2431 /* kick calling process */
2432 complete_request(mdsc, req);
2434 ceph_mdsc_put_request(req);
2441 * handle mds notification that our request has been forwarded.
2443 static void handle_forward(struct ceph_mds_client *mdsc,
2444 struct ceph_mds_session *session,
2445 struct ceph_msg *msg)
2447 struct ceph_mds_request *req;
2448 u64 tid = le64_to_cpu(msg->hdr.tid);
2452 void *p = msg->front.iov_base;
2453 void *end = p + msg->front.iov_len;
2455 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2456 next_mds = ceph_decode_32(&p);
2457 fwd_seq = ceph_decode_32(&p);
2459 mutex_lock(&mdsc->mutex);
2460 req = __lookup_request(mdsc, tid);
2462 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2463 goto out; /* dup reply? */
2466 if (req->r_aborted) {
2467 dout("forward tid %llu aborted, unregistering\n", tid);
2468 __unregister_request(mdsc, req);
2469 } else if (fwd_seq <= req->r_num_fwd) {
2470 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2471 tid, next_mds, req->r_num_fwd, fwd_seq);
2473 /* resend. forward race not possible; mds would drop */
2474 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2476 BUG_ON(req->r_got_result);
2477 req->r_num_fwd = fwd_seq;
2478 req->r_resend_mds = next_mds;
2479 put_request_session(req);
2480 __do_request(mdsc, req);
2482 ceph_mdsc_put_request(req);
2484 mutex_unlock(&mdsc->mutex);
2488 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2492 * handle a mds session control message
2494 static void handle_session(struct ceph_mds_session *session,
2495 struct ceph_msg *msg)
2497 struct ceph_mds_client *mdsc = session->s_mdsc;
2500 int mds = session->s_mds;
2501 struct ceph_mds_session_head *h = msg->front.iov_base;
2505 if (msg->front.iov_len != sizeof(*h))
2507 op = le32_to_cpu(h->op);
2508 seq = le64_to_cpu(h->seq);
2510 mutex_lock(&mdsc->mutex);
2511 if (op == CEPH_SESSION_CLOSE)
2512 __unregister_session(mdsc, session);
2513 /* FIXME: this ttl calculation is generous */
2514 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2515 mutex_unlock(&mdsc->mutex);
2517 mutex_lock(&session->s_mutex);
2519 dout("handle_session mds%d %s %p state %s seq %llu\n",
2520 mds, ceph_session_op_name(op), session,
2521 ceph_session_state_name(session->s_state), seq);
2523 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2524 session->s_state = CEPH_MDS_SESSION_OPEN;
2525 pr_info("mds%d came back\n", session->s_mds);
2529 case CEPH_SESSION_OPEN:
2530 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2531 pr_info("mds%d reconnect success\n", session->s_mds);
2532 session->s_state = CEPH_MDS_SESSION_OPEN;
2533 renewed_caps(mdsc, session, 0);
2536 __close_session(mdsc, session);
2539 case CEPH_SESSION_RENEWCAPS:
2540 if (session->s_renew_seq == seq)
2541 renewed_caps(mdsc, session, 1);
2544 case CEPH_SESSION_CLOSE:
2545 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2546 pr_info("mds%d reconnect denied\n", session->s_mds);
2547 remove_session_caps(session);
2548 wake = 2; /* for good measure */
2549 wake_up_all(&mdsc->session_close_wq);
2552 case CEPH_SESSION_STALE:
2553 pr_info("mds%d caps went stale, renewing\n",
2555 spin_lock(&session->s_gen_ttl_lock);
2556 session->s_cap_gen++;
2557 session->s_cap_ttl = jiffies - 1;
2558 spin_unlock(&session->s_gen_ttl_lock);
2559 send_renew_caps(mdsc, session);
2562 case CEPH_SESSION_RECALL_STATE:
2563 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2566 case CEPH_SESSION_FLUSHMSG:
2567 send_flushmsg_ack(mdsc, session, seq);
2571 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2575 mutex_unlock(&session->s_mutex);
2577 mutex_lock(&mdsc->mutex);
2578 __wake_requests(mdsc, &session->s_waiting);
2580 kick_requests(mdsc, mds);
2581 mutex_unlock(&mdsc->mutex);
2586 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2587 (int)msg->front.iov_len);
2594 * called under session->mutex.
2596 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2597 struct ceph_mds_session *session)
2599 struct ceph_mds_request *req, *nreq;
2602 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2604 mutex_lock(&mdsc->mutex);
2605 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2606 err = __prepare_send_request(mdsc, req, session->s_mds);
2608 ceph_msg_get(req->r_request);
2609 ceph_con_send(&session->s_con, req->r_request);
2612 mutex_unlock(&mdsc->mutex);
2616 * Encode information about a cap for a reconnect with the MDS.
2618 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2622 struct ceph_mds_cap_reconnect v2;
2623 struct ceph_mds_cap_reconnect_v1 v1;
2626 struct ceph_inode_info *ci;
2627 struct ceph_reconnect_state *recon_state = arg;
2628 struct ceph_pagelist *pagelist = recon_state->pagelist;
2632 struct dentry *dentry;
2636 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2637 inode, ceph_vinop(inode), cap, cap->cap_id,
2638 ceph_cap_string(cap->issued));
2639 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2643 dentry = d_find_alias(inode);
2645 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2647 err = PTR_ERR(path);
2654 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2658 spin_lock(&ci->i_ceph_lock);
2659 cap->seq = 0; /* reset cap seq */
2660 cap->issue_seq = 0; /* and issue_seq */
2661 cap->mseq = 0; /* and migrate_seq */
2662 cap->cap_gen = cap->session->s_cap_gen;
2664 if (recon_state->flock) {
2665 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2666 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2667 rec.v2.issued = cpu_to_le32(cap->issued);
2668 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2669 rec.v2.pathbase = cpu_to_le64(pathbase);
2670 rec.v2.flock_len = 0;
2671 reclen = sizeof(rec.v2);
2673 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2674 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2675 rec.v1.issued = cpu_to_le32(cap->issued);
2676 rec.v1.size = cpu_to_le64(inode->i_size);
2677 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2678 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2679 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2680 rec.v1.pathbase = cpu_to_le64(pathbase);
2681 reclen = sizeof(rec.v1);
2683 spin_unlock(&ci->i_ceph_lock);
2685 if (recon_state->flock) {
2686 int num_fcntl_locks, num_flock_locks;
2687 struct ceph_filelock *flocks;
2690 spin_lock(&inode->i_lock);
2691 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
2692 spin_unlock(&inode->i_lock);
2693 flocks = kmalloc((num_fcntl_locks+num_flock_locks) *
2694 sizeof(struct ceph_filelock), GFP_NOFS);
2699 spin_lock(&inode->i_lock);
2700 err = ceph_encode_locks_to_buffer(inode, flocks,
2703 spin_unlock(&inode->i_lock);
2711 * number of encoded locks is stable, so copy to pagelist
2713 rec.v2.flock_len = cpu_to_le32(2*sizeof(u32) +
2714 (num_fcntl_locks+num_flock_locks) *
2715 sizeof(struct ceph_filelock));
2716 err = ceph_pagelist_append(pagelist, &rec, reclen);
2718 err = ceph_locks_to_pagelist(flocks, pagelist,
2723 err = ceph_pagelist_append(pagelist, &rec, reclen);
2726 recon_state->nr_caps++;
2736 * If an MDS fails and recovers, clients need to reconnect in order to
2737 * reestablish shared state. This includes all caps issued through
2738 * this session _and_ the snap_realm hierarchy. Because it's not
2739 * clear which snap realms the mds cares about, we send everything we
2740 * know about.. that ensures we'll then get any new info the
2741 * recovering MDS might have.
2743 * This is a relatively heavyweight operation, but it's rare.
2745 * called with mdsc->mutex held.
2747 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2748 struct ceph_mds_session *session)
2750 struct ceph_msg *reply;
2752 int mds = session->s_mds;
2755 struct ceph_pagelist *pagelist;
2756 struct ceph_reconnect_state recon_state;
2758 pr_info("mds%d reconnect start\n", mds);
2760 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2762 goto fail_nopagelist;
2763 ceph_pagelist_init(pagelist);
2765 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
2769 mutex_lock(&session->s_mutex);
2770 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2773 dout("session %p state %s\n", session,
2774 ceph_session_state_name(session->s_state));
2776 spin_lock(&session->s_gen_ttl_lock);
2777 session->s_cap_gen++;
2778 spin_unlock(&session->s_gen_ttl_lock);
2780 spin_lock(&session->s_cap_lock);
2782 * notify __ceph_remove_cap() that we are composing cap reconnect.
2783 * If a cap get released before being added to the cap reconnect,
2784 * __ceph_remove_cap() should skip queuing cap release.
2786 session->s_cap_reconnect = 1;
2787 /* drop old cap expires; we're about to reestablish that state */
2788 discard_cap_releases(mdsc, session);
2789 spin_unlock(&session->s_cap_lock);
2791 /* trim unused caps to reduce MDS's cache rejoin time */
2792 shrink_dcache_parent(mdsc->fsc->sb->s_root);
2794 ceph_con_close(&session->s_con);
2795 ceph_con_open(&session->s_con,
2796 CEPH_ENTITY_TYPE_MDS, mds,
2797 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2799 /* replay unsafe requests */
2800 replay_unsafe_requests(mdsc, session);
2802 down_read(&mdsc->snap_rwsem);
2804 /* traverse this session's caps */
2805 s_nr_caps = session->s_nr_caps;
2806 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
2810 recon_state.nr_caps = 0;
2811 recon_state.pagelist = pagelist;
2812 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2813 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
2817 spin_lock(&session->s_cap_lock);
2818 session->s_cap_reconnect = 0;
2819 spin_unlock(&session->s_cap_lock);
2822 * snaprealms. we provide mds with the ino, seq (version), and
2823 * parent for all of our realms. If the mds has any newer info,
2826 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2827 struct ceph_snap_realm *realm =
2828 rb_entry(p, struct ceph_snap_realm, node);
2829 struct ceph_mds_snaprealm_reconnect sr_rec;
2831 dout(" adding snap realm %llx seq %lld parent %llx\n",
2832 realm->ino, realm->seq, realm->parent_ino);
2833 sr_rec.ino = cpu_to_le64(realm->ino);
2834 sr_rec.seq = cpu_to_le64(realm->seq);
2835 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2836 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2841 if (recon_state.flock)
2842 reply->hdr.version = cpu_to_le16(2);
2844 /* raced with cap release? */
2845 if (s_nr_caps != recon_state.nr_caps) {
2846 struct page *page = list_first_entry(&pagelist->head,
2848 __le32 *addr = kmap_atomic(page);
2849 *addr = cpu_to_le32(recon_state.nr_caps);
2850 kunmap_atomic(addr);
2853 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2854 ceph_msg_data_add_pagelist(reply, pagelist);
2855 ceph_con_send(&session->s_con, reply);
2857 mutex_unlock(&session->s_mutex);
2859 mutex_lock(&mdsc->mutex);
2860 __wake_requests(mdsc, &session->s_waiting);
2861 mutex_unlock(&mdsc->mutex);
2863 up_read(&mdsc->snap_rwsem);
2867 ceph_msg_put(reply);
2868 up_read(&mdsc->snap_rwsem);
2869 mutex_unlock(&session->s_mutex);
2871 ceph_pagelist_release(pagelist);
2873 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
2879 * compare old and new mdsmaps, kicking requests
2880 * and closing out old connections as necessary
2882 * called under mdsc->mutex.
2884 static void check_new_map(struct ceph_mds_client *mdsc,
2885 struct ceph_mdsmap *newmap,
2886 struct ceph_mdsmap *oldmap)
2889 int oldstate, newstate;
2890 struct ceph_mds_session *s;
2892 dout("check_new_map new %u old %u\n",
2893 newmap->m_epoch, oldmap->m_epoch);
2895 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2896 if (mdsc->sessions[i] == NULL)
2898 s = mdsc->sessions[i];
2899 oldstate = ceph_mdsmap_get_state(oldmap, i);
2900 newstate = ceph_mdsmap_get_state(newmap, i);
2902 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2903 i, ceph_mds_state_name(oldstate),
2904 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
2905 ceph_mds_state_name(newstate),
2906 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
2907 ceph_session_state_name(s->s_state));
2909 if (i >= newmap->m_max_mds ||
2910 memcmp(ceph_mdsmap_get_addr(oldmap, i),
2911 ceph_mdsmap_get_addr(newmap, i),
2912 sizeof(struct ceph_entity_addr))) {
2913 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2914 /* the session never opened, just close it
2916 __wake_requests(mdsc, &s->s_waiting);
2917 __unregister_session(mdsc, s);
2920 mutex_unlock(&mdsc->mutex);
2921 mutex_lock(&s->s_mutex);
2922 mutex_lock(&mdsc->mutex);
2923 ceph_con_close(&s->s_con);
2924 mutex_unlock(&s->s_mutex);
2925 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2928 /* kick any requests waiting on the recovering mds */
2929 kick_requests(mdsc, i);
2930 } else if (oldstate == newstate) {
2931 continue; /* nothing new with this mds */
2937 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
2938 newstate >= CEPH_MDS_STATE_RECONNECT) {
2939 mutex_unlock(&mdsc->mutex);
2940 send_mds_reconnect(mdsc, s);
2941 mutex_lock(&mdsc->mutex);
2945 * kick request on any mds that has gone active.
2947 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2948 newstate >= CEPH_MDS_STATE_ACTIVE) {
2949 if (oldstate != CEPH_MDS_STATE_CREATING &&
2950 oldstate != CEPH_MDS_STATE_STARTING)
2951 pr_info("mds%d recovery completed\n", s->s_mds);
2952 kick_requests(mdsc, i);
2953 ceph_kick_flushing_caps(mdsc, s);
2954 wake_up_session_caps(s, 1);
2958 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2959 s = mdsc->sessions[i];
2962 if (!ceph_mdsmap_is_laggy(newmap, i))
2964 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2965 s->s_state == CEPH_MDS_SESSION_HUNG ||
2966 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2967 dout(" connecting to export targets of laggy mds%d\n",
2969 __open_export_target_sessions(mdsc, s);
2981 * caller must hold session s_mutex, dentry->d_lock
2983 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2985 struct ceph_dentry_info *di = ceph_dentry(dentry);
2987 ceph_put_mds_session(di->lease_session);
2988 di->lease_session = NULL;
2991 static void handle_lease(struct ceph_mds_client *mdsc,
2992 struct ceph_mds_session *session,
2993 struct ceph_msg *msg)
2995 struct super_block *sb = mdsc->fsc->sb;
2996 struct inode *inode;
2997 struct dentry *parent, *dentry;
2998 struct ceph_dentry_info *di;
2999 int mds = session->s_mds;
3000 struct ceph_mds_lease *h = msg->front.iov_base;
3002 struct ceph_vino vino;
3006 dout("handle_lease from mds%d\n", mds);
3009 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3011 vino.ino = le64_to_cpu(h->ino);
3012 vino.snap = CEPH_NOSNAP;
3013 seq = le32_to_cpu(h->seq);
3014 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
3015 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
3016 if (dname.len != get_unaligned_le32(h+1))
3020 inode = ceph_find_inode(sb, vino);
3021 dout("handle_lease %s, ino %llx %p %.*s\n",
3022 ceph_lease_op_name(h->action), vino.ino, inode,
3023 dname.len, dname.name);
3025 mutex_lock(&session->s_mutex);
3028 if (inode == NULL) {
3029 dout("handle_lease no inode %llx\n", vino.ino);
3034 parent = d_find_alias(inode);
3036 dout("no parent dentry on inode %p\n", inode);
3038 goto release; /* hrm... */
3040 dname.hash = full_name_hash(dname.name, dname.len);
3041 dentry = d_lookup(parent, &dname);
3046 spin_lock(&dentry->d_lock);
3047 di = ceph_dentry(dentry);
3048 switch (h->action) {
3049 case CEPH_MDS_LEASE_REVOKE:
3050 if (di->lease_session == session) {
3051 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3052 h->seq = cpu_to_le32(di->lease_seq);
3053 __ceph_mdsc_drop_dentry_lease(dentry);
3058 case CEPH_MDS_LEASE_RENEW:
3059 if (di->lease_session == session &&
3060 di->lease_gen == session->s_cap_gen &&
3061 di->lease_renew_from &&
3062 di->lease_renew_after == 0) {
3063 unsigned long duration =
3064 le32_to_cpu(h->duration_ms) * HZ / 1000;
3066 di->lease_seq = seq;
3067 dentry->d_time = di->lease_renew_from + duration;
3068 di->lease_renew_after = di->lease_renew_from +
3070 di->lease_renew_from = 0;
3074 spin_unlock(&dentry->d_lock);
3081 /* let's just reuse the same message */
3082 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3084 ceph_con_send(&session->s_con, msg);
3088 mutex_unlock(&session->s_mutex);
3092 pr_err("corrupt lease message\n");
3096 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3097 struct inode *inode,
3098 struct dentry *dentry, char action,
3101 struct ceph_msg *msg;
3102 struct ceph_mds_lease *lease;
3103 int len = sizeof(*lease) + sizeof(u32);
3106 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3107 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3108 dnamelen = dentry->d_name.len;
3111 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
3114 lease = msg->front.iov_base;
3115 lease->action = action;
3116 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3117 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3118 lease->seq = cpu_to_le32(seq);
3119 put_unaligned_le32(dnamelen, lease + 1);
3120 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3123 * if this is a preemptive lease RELEASE, no need to
3124 * flush request stream, since the actual request will
3127 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3129 ceph_con_send(&session->s_con, msg);
3133 * Preemptively release a lease we expect to invalidate anyway.
3134 * Pass @inode always, @dentry is optional.
3136 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
3137 struct dentry *dentry)
3139 struct ceph_dentry_info *di;
3140 struct ceph_mds_session *session;
3143 BUG_ON(inode == NULL);
3144 BUG_ON(dentry == NULL);
3146 /* is dentry lease valid? */
3147 spin_lock(&dentry->d_lock);
3148 di = ceph_dentry(dentry);
3149 if (!di || !di->lease_session ||
3150 di->lease_session->s_mds < 0 ||
3151 di->lease_gen != di->lease_session->s_cap_gen ||
3152 !time_before(jiffies, dentry->d_time)) {
3153 dout("lease_release inode %p dentry %p -- "
3156 spin_unlock(&dentry->d_lock);
3160 /* we do have a lease on this dentry; note mds and seq */
3161 session = ceph_get_mds_session(di->lease_session);
3162 seq = di->lease_seq;
3163 __ceph_mdsc_drop_dentry_lease(dentry);
3164 spin_unlock(&dentry->d_lock);
3166 dout("lease_release inode %p dentry %p to mds%d\n",
3167 inode, dentry, session->s_mds);
3168 ceph_mdsc_lease_send_msg(session, inode, dentry,
3169 CEPH_MDS_LEASE_RELEASE, seq);
3170 ceph_put_mds_session(session);
3174 * drop all leases (and dentry refs) in preparation for umount
3176 static void drop_leases(struct ceph_mds_client *mdsc)
3180 dout("drop_leases\n");
3181 mutex_lock(&mdsc->mutex);
3182 for (i = 0; i < mdsc->max_sessions; i++) {
3183 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3186 mutex_unlock(&mdsc->mutex);
3187 mutex_lock(&s->s_mutex);
3188 mutex_unlock(&s->s_mutex);
3189 ceph_put_mds_session(s);
3190 mutex_lock(&mdsc->mutex);
3192 mutex_unlock(&mdsc->mutex);
3198 * delayed work -- periodically trim expired leases, renew caps with mds
3200 static void schedule_delayed(struct ceph_mds_client *mdsc)
3203 unsigned hz = round_jiffies_relative(HZ * delay);
3204 schedule_delayed_work(&mdsc->delayed_work, hz);
3207 static void delayed_work(struct work_struct *work)
3210 struct ceph_mds_client *mdsc =
3211 container_of(work, struct ceph_mds_client, delayed_work.work);
3215 dout("mdsc delayed_work\n");
3216 ceph_check_delayed_caps(mdsc);
3218 mutex_lock(&mdsc->mutex);
3219 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3220 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3221 mdsc->last_renew_caps);
3223 mdsc->last_renew_caps = jiffies;
3225 for (i = 0; i < mdsc->max_sessions; i++) {
3226 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3229 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3230 dout("resending session close request for mds%d\n",
3232 request_close_session(mdsc, s);
3233 ceph_put_mds_session(s);
3236 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3237 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3238 s->s_state = CEPH_MDS_SESSION_HUNG;
3239 pr_info("mds%d hung\n", s->s_mds);