BKL: remove extraneous #include <smp_lock.h>
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ceph / mds_client.c
blob7799cac2b629de0f5b7ef47a911d65801261cab9
1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/fs.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>
10 #include "super.h"
11 #include "mds_client.h"
13 #include <linux/ceph/messenger.h>
14 #include <linux/ceph/decode.h>
15 #include <linux/ceph/pagelist.h>
16 #include <linux/ceph/auth.h>
17 #include <linux/ceph/debugfs.h>
20 * A cluster of MDS (metadata server) daemons is responsible for
21 * managing the file system namespace (the directory hierarchy and
22 * inodes) and for coordinating shared access to storage. Metadata is
23 * partitioning hierarchically across a number of servers, and that
24 * partition varies over time as the cluster adjusts the distribution
25 * in order to balance load.
27 * The MDS client is primarily responsible to managing synchronous
28 * metadata requests for operations like open, unlink, and so forth.
29 * If there is a MDS failure, we find out about it when we (possibly
30 * request and) receive a new MDS map, and can resubmit affected
31 * requests.
33 * For the most part, though, we take advantage of a lossless
34 * communications channel to the MDS, and do not need to worry about
35 * timing out or resubmitting requests.
37 * We maintain a stateful "session" with each MDS we interact with.
38 * Within each session, we sent periodic heartbeat messages to ensure
39 * any capabilities or leases we have been issues remain valid. If
40 * the session times out and goes stale, our leases and capabilities
41 * are no longer valid.
44 struct ceph_reconnect_state {
45 struct ceph_pagelist *pagelist;
46 bool flock;
49 static void __wake_requests(struct ceph_mds_client *mdsc,
50 struct list_head *head);
52 static const struct ceph_connection_operations mds_con_ops;
56 * mds reply parsing
60 * parse individual inode info
62 static int parse_reply_info_in(void **p, void *end,
63 struct ceph_mds_reply_info_in *info)
65 int err = -EIO;
67 info->in = *p;
68 *p += sizeof(struct ceph_mds_reply_inode) +
69 sizeof(*info->in->fragtree.splits) *
70 le32_to_cpu(info->in->fragtree.nsplits);
72 ceph_decode_32_safe(p, end, info->symlink_len, bad);
73 ceph_decode_need(p, end, info->symlink_len, bad);
74 info->symlink = *p;
75 *p += info->symlink_len;
77 ceph_decode_32_safe(p, end, info->xattr_len, bad);
78 ceph_decode_need(p, end, info->xattr_len, bad);
79 info->xattr_data = *p;
80 *p += info->xattr_len;
81 return 0;
82 bad:
83 return err;
87 * parse a normal reply, which may contain a (dir+)dentry and/or a
88 * target inode.
90 static int parse_reply_info_trace(void **p, void *end,
91 struct ceph_mds_reply_info_parsed *info)
93 int err;
95 if (info->head->is_dentry) {
96 err = parse_reply_info_in(p, end, &info->diri);
97 if (err < 0)
98 goto out_bad;
100 if (unlikely(*p + sizeof(*info->dirfrag) > end))
101 goto bad;
102 info->dirfrag = *p;
103 *p += sizeof(*info->dirfrag) +
104 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
105 if (unlikely(*p > end))
106 goto bad;
108 ceph_decode_32_safe(p, end, info->dname_len, bad);
109 ceph_decode_need(p, end, info->dname_len, bad);
110 info->dname = *p;
111 *p += info->dname_len;
112 info->dlease = *p;
113 *p += sizeof(*info->dlease);
116 if (info->head->is_target) {
117 err = parse_reply_info_in(p, end, &info->targeti);
118 if (err < 0)
119 goto out_bad;
122 if (unlikely(*p != end))
123 goto bad;
124 return 0;
126 bad:
127 err = -EIO;
128 out_bad:
129 pr_err("problem parsing mds trace %d\n", err);
130 return err;
134 * parse readdir results
136 static int parse_reply_info_dir(void **p, void *end,
137 struct ceph_mds_reply_info_parsed *info)
139 u32 num, i = 0;
140 int err;
142 info->dir_dir = *p;
143 if (*p + sizeof(*info->dir_dir) > end)
144 goto bad;
145 *p += sizeof(*info->dir_dir) +
146 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
147 if (*p > end)
148 goto bad;
150 ceph_decode_need(p, end, sizeof(num) + 2, bad);
151 num = ceph_decode_32(p);
152 info->dir_end = ceph_decode_8(p);
153 info->dir_complete = ceph_decode_8(p);
154 if (num == 0)
155 goto done;
157 /* alloc large array */
158 info->dir_nr = num;
159 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
160 sizeof(*info->dir_dname) +
161 sizeof(*info->dir_dname_len) +
162 sizeof(*info->dir_dlease),
163 GFP_NOFS);
164 if (info->dir_in == NULL) {
165 err = -ENOMEM;
166 goto out_bad;
168 info->dir_dname = (void *)(info->dir_in + num);
169 info->dir_dname_len = (void *)(info->dir_dname + num);
170 info->dir_dlease = (void *)(info->dir_dname_len + num);
172 while (num) {
173 /* dentry */
174 ceph_decode_need(p, end, sizeof(u32)*2, bad);
175 info->dir_dname_len[i] = ceph_decode_32(p);
176 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
177 info->dir_dname[i] = *p;
178 *p += info->dir_dname_len[i];
179 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
180 info->dir_dname[i]);
181 info->dir_dlease[i] = *p;
182 *p += sizeof(struct ceph_mds_reply_lease);
184 /* inode */
185 err = parse_reply_info_in(p, end, &info->dir_in[i]);
186 if (err < 0)
187 goto out_bad;
188 i++;
189 num--;
192 done:
193 if (*p != end)
194 goto bad;
195 return 0;
197 bad:
198 err = -EIO;
199 out_bad:
200 pr_err("problem parsing dir contents %d\n", err);
201 return err;
205 * parse entire mds reply
207 static int parse_reply_info(struct ceph_msg *msg,
208 struct ceph_mds_reply_info_parsed *info)
210 void *p, *end;
211 u32 len;
212 int err;
214 info->head = msg->front.iov_base;
215 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
216 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
218 /* trace */
219 ceph_decode_32_safe(&p, end, len, bad);
220 if (len > 0) {
221 err = parse_reply_info_trace(&p, p+len, info);
222 if (err < 0)
223 goto out_bad;
226 /* dir content */
227 ceph_decode_32_safe(&p, end, len, bad);
228 if (len > 0) {
229 err = parse_reply_info_dir(&p, p+len, info);
230 if (err < 0)
231 goto out_bad;
234 /* snap blob */
235 ceph_decode_32_safe(&p, end, len, bad);
236 info->snapblob_len = len;
237 info->snapblob = p;
238 p += len;
240 if (p != end)
241 goto bad;
242 return 0;
244 bad:
245 err = -EIO;
246 out_bad:
247 pr_err("mds parse_reply err %d\n", err);
248 return err;
251 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
253 kfree(info->dir_in);
258 * sessions
260 static const char *session_state_name(int s)
262 switch (s) {
263 case CEPH_MDS_SESSION_NEW: return "new";
264 case CEPH_MDS_SESSION_OPENING: return "opening";
265 case CEPH_MDS_SESSION_OPEN: return "open";
266 case CEPH_MDS_SESSION_HUNG: return "hung";
267 case CEPH_MDS_SESSION_CLOSING: return "closing";
268 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
269 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
270 default: return "???";
274 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
276 if (atomic_inc_not_zero(&s->s_ref)) {
277 dout("mdsc get_session %p %d -> %d\n", s,
278 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
279 return s;
280 } else {
281 dout("mdsc get_session %p 0 -- FAIL", s);
282 return NULL;
286 void ceph_put_mds_session(struct ceph_mds_session *s)
288 dout("mdsc put_session %p %d -> %d\n", s,
289 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
290 if (atomic_dec_and_test(&s->s_ref)) {
291 if (s->s_authorizer)
292 s->s_mdsc->fsc->client->monc.auth->ops->destroy_authorizer(
293 s->s_mdsc->fsc->client->monc.auth,
294 s->s_authorizer);
295 kfree(s);
300 * called under mdsc->mutex
302 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
303 int mds)
305 struct ceph_mds_session *session;
307 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
308 return NULL;
309 session = mdsc->sessions[mds];
310 dout("lookup_mds_session %p %d\n", session,
311 atomic_read(&session->s_ref));
312 get_session(session);
313 return session;
316 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
318 if (mds >= mdsc->max_sessions)
319 return false;
320 return mdsc->sessions[mds];
323 static int __verify_registered_session(struct ceph_mds_client *mdsc,
324 struct ceph_mds_session *s)
326 if (s->s_mds >= mdsc->max_sessions ||
327 mdsc->sessions[s->s_mds] != s)
328 return -ENOENT;
329 return 0;
333 * create+register a new session for given mds.
334 * called under mdsc->mutex.
336 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
337 int mds)
339 struct ceph_mds_session *s;
341 s = kzalloc(sizeof(*s), GFP_NOFS);
342 if (!s)
343 return ERR_PTR(-ENOMEM);
344 s->s_mdsc = mdsc;
345 s->s_mds = mds;
346 s->s_state = CEPH_MDS_SESSION_NEW;
347 s->s_ttl = 0;
348 s->s_seq = 0;
349 mutex_init(&s->s_mutex);
351 ceph_con_init(mdsc->fsc->client->msgr, &s->s_con);
352 s->s_con.private = s;
353 s->s_con.ops = &mds_con_ops;
354 s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
355 s->s_con.peer_name.num = cpu_to_le64(mds);
357 spin_lock_init(&s->s_cap_lock);
358 s->s_cap_gen = 0;
359 s->s_cap_ttl = 0;
360 s->s_renew_requested = 0;
361 s->s_renew_seq = 0;
362 INIT_LIST_HEAD(&s->s_caps);
363 s->s_nr_caps = 0;
364 s->s_trim_caps = 0;
365 atomic_set(&s->s_ref, 1);
366 INIT_LIST_HEAD(&s->s_waiting);
367 INIT_LIST_HEAD(&s->s_unsafe);
368 s->s_num_cap_releases = 0;
369 s->s_cap_iterator = NULL;
370 INIT_LIST_HEAD(&s->s_cap_releases);
371 INIT_LIST_HEAD(&s->s_cap_releases_done);
372 INIT_LIST_HEAD(&s->s_cap_flushing);
373 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
375 dout("register_session mds%d\n", mds);
376 if (mds >= mdsc->max_sessions) {
377 int newmax = 1 << get_count_order(mds+1);
378 struct ceph_mds_session **sa;
380 dout("register_session realloc to %d\n", newmax);
381 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
382 if (sa == NULL)
383 goto fail_realloc;
384 if (mdsc->sessions) {
385 memcpy(sa, mdsc->sessions,
386 mdsc->max_sessions * sizeof(void *));
387 kfree(mdsc->sessions);
389 mdsc->sessions = sa;
390 mdsc->max_sessions = newmax;
392 mdsc->sessions[mds] = s;
393 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
395 ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
397 return s;
399 fail_realloc:
400 kfree(s);
401 return ERR_PTR(-ENOMEM);
405 * called under mdsc->mutex
407 static void __unregister_session(struct ceph_mds_client *mdsc,
408 struct ceph_mds_session *s)
410 dout("__unregister_session mds%d %p\n", s->s_mds, s);
411 BUG_ON(mdsc->sessions[s->s_mds] != s);
412 mdsc->sessions[s->s_mds] = NULL;
413 ceph_con_close(&s->s_con);
414 ceph_put_mds_session(s);
418 * drop session refs in request.
420 * should be last request ref, or hold mdsc->mutex
422 static void put_request_session(struct ceph_mds_request *req)
424 if (req->r_session) {
425 ceph_put_mds_session(req->r_session);
426 req->r_session = NULL;
430 void ceph_mdsc_release_request(struct kref *kref)
432 struct ceph_mds_request *req = container_of(kref,
433 struct ceph_mds_request,
434 r_kref);
435 if (req->r_request)
436 ceph_msg_put(req->r_request);
437 if (req->r_reply) {
438 ceph_msg_put(req->r_reply);
439 destroy_reply_info(&req->r_reply_info);
441 if (req->r_inode) {
442 ceph_put_cap_refs(ceph_inode(req->r_inode),
443 CEPH_CAP_PIN);
444 iput(req->r_inode);
446 if (req->r_locked_dir)
447 ceph_put_cap_refs(ceph_inode(req->r_locked_dir),
448 CEPH_CAP_PIN);
449 if (req->r_target_inode)
450 iput(req->r_target_inode);
451 if (req->r_dentry)
452 dput(req->r_dentry);
453 if (req->r_old_dentry) {
454 ceph_put_cap_refs(
455 ceph_inode(req->r_old_dentry->d_parent->d_inode),
456 CEPH_CAP_PIN);
457 dput(req->r_old_dentry);
459 kfree(req->r_path1);
460 kfree(req->r_path2);
461 put_request_session(req);
462 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
463 kfree(req);
467 * lookup session, bump ref if found.
469 * called under mdsc->mutex.
471 static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
472 u64 tid)
474 struct ceph_mds_request *req;
475 struct rb_node *n = mdsc->request_tree.rb_node;
477 while (n) {
478 req = rb_entry(n, struct ceph_mds_request, r_node);
479 if (tid < req->r_tid)
480 n = n->rb_left;
481 else if (tid > req->r_tid)
482 n = n->rb_right;
483 else {
484 ceph_mdsc_get_request(req);
485 return req;
488 return NULL;
491 static void __insert_request(struct ceph_mds_client *mdsc,
492 struct ceph_mds_request *new)
494 struct rb_node **p = &mdsc->request_tree.rb_node;
495 struct rb_node *parent = NULL;
496 struct ceph_mds_request *req = NULL;
498 while (*p) {
499 parent = *p;
500 req = rb_entry(parent, struct ceph_mds_request, r_node);
501 if (new->r_tid < req->r_tid)
502 p = &(*p)->rb_left;
503 else if (new->r_tid > req->r_tid)
504 p = &(*p)->rb_right;
505 else
506 BUG();
509 rb_link_node(&new->r_node, parent, p);
510 rb_insert_color(&new->r_node, &mdsc->request_tree);
514 * Register an in-flight request, and assign a tid. Link to directory
515 * are modifying (if any).
517 * Called under mdsc->mutex.
519 static void __register_request(struct ceph_mds_client *mdsc,
520 struct ceph_mds_request *req,
521 struct inode *dir)
523 req->r_tid = ++mdsc->last_tid;
524 if (req->r_num_caps)
525 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
526 req->r_num_caps);
527 dout("__register_request %p tid %lld\n", req, req->r_tid);
528 ceph_mdsc_get_request(req);
529 __insert_request(mdsc, req);
531 if (dir) {
532 struct ceph_inode_info *ci = ceph_inode(dir);
534 spin_lock(&ci->i_unsafe_lock);
535 req->r_unsafe_dir = dir;
536 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
537 spin_unlock(&ci->i_unsafe_lock);
541 static void __unregister_request(struct ceph_mds_client *mdsc,
542 struct ceph_mds_request *req)
544 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
545 rb_erase(&req->r_node, &mdsc->request_tree);
546 RB_CLEAR_NODE(&req->r_node);
548 if (req->r_unsafe_dir) {
549 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
551 spin_lock(&ci->i_unsafe_lock);
552 list_del_init(&req->r_unsafe_dir_item);
553 spin_unlock(&ci->i_unsafe_lock);
556 ceph_mdsc_put_request(req);
560 * Choose mds to send request to next. If there is a hint set in the
561 * request (e.g., due to a prior forward hint from the mds), use that.
562 * Otherwise, consult frag tree and/or caps to identify the
563 * appropriate mds. If all else fails, choose randomly.
565 * Called under mdsc->mutex.
567 struct dentry *get_nonsnap_parent(struct dentry *dentry)
569 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
570 dentry = dentry->d_parent;
571 return dentry;
574 static int __choose_mds(struct ceph_mds_client *mdsc,
575 struct ceph_mds_request *req)
577 struct inode *inode;
578 struct ceph_inode_info *ci;
579 struct ceph_cap *cap;
580 int mode = req->r_direct_mode;
581 int mds = -1;
582 u32 hash = req->r_direct_hash;
583 bool is_hash = req->r_direct_is_hash;
586 * is there a specific mds we should try? ignore hint if we have
587 * no session and the mds is not up (active or recovering).
589 if (req->r_resend_mds >= 0 &&
590 (__have_session(mdsc, req->r_resend_mds) ||
591 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
592 dout("choose_mds using resend_mds mds%d\n",
593 req->r_resend_mds);
594 return req->r_resend_mds;
597 if (mode == USE_RANDOM_MDS)
598 goto random;
600 inode = NULL;
601 if (req->r_inode) {
602 inode = req->r_inode;
603 } else if (req->r_dentry) {
604 struct inode *dir = req->r_dentry->d_parent->d_inode;
606 if (dir->i_sb != mdsc->fsc->sb) {
607 /* not this fs! */
608 inode = req->r_dentry->d_inode;
609 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
610 /* direct snapped/virtual snapdir requests
611 * based on parent dir inode */
612 struct dentry *dn =
613 get_nonsnap_parent(req->r_dentry->d_parent);
614 inode = dn->d_inode;
615 dout("__choose_mds using nonsnap parent %p\n", inode);
616 } else if (req->r_dentry->d_inode) {
617 /* dentry target */
618 inode = req->r_dentry->d_inode;
619 } else {
620 /* dir + name */
621 inode = dir;
622 hash = req->r_dentry->d_name.hash;
623 is_hash = true;
627 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
628 (int)hash, mode);
629 if (!inode)
630 goto random;
631 ci = ceph_inode(inode);
633 if (is_hash && S_ISDIR(inode->i_mode)) {
634 struct ceph_inode_frag frag;
635 int found;
637 ceph_choose_frag(ci, hash, &frag, &found);
638 if (found) {
639 if (mode == USE_ANY_MDS && frag.ndist > 0) {
640 u8 r;
642 /* choose a random replica */
643 get_random_bytes(&r, 1);
644 r %= frag.ndist;
645 mds = frag.dist[r];
646 dout("choose_mds %p %llx.%llx "
647 "frag %u mds%d (%d/%d)\n",
648 inode, ceph_vinop(inode),
649 frag.frag, frag.mds,
650 (int)r, frag.ndist);
651 return mds;
654 /* since this file/dir wasn't known to be
655 * replicated, then we want to look for the
656 * authoritative mds. */
657 mode = USE_AUTH_MDS;
658 if (frag.mds >= 0) {
659 /* choose auth mds */
660 mds = frag.mds;
661 dout("choose_mds %p %llx.%llx "
662 "frag %u mds%d (auth)\n",
663 inode, ceph_vinop(inode), frag.frag, mds);
664 return mds;
669 spin_lock(&inode->i_lock);
670 cap = NULL;
671 if (mode == USE_AUTH_MDS)
672 cap = ci->i_auth_cap;
673 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
674 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
675 if (!cap) {
676 spin_unlock(&inode->i_lock);
677 goto random;
679 mds = cap->session->s_mds;
680 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
681 inode, ceph_vinop(inode), mds,
682 cap == ci->i_auth_cap ? "auth " : "", cap);
683 spin_unlock(&inode->i_lock);
684 return mds;
686 random:
687 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
688 dout("choose_mds chose random mds%d\n", mds);
689 return mds;
694 * session messages
696 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
698 struct ceph_msg *msg;
699 struct ceph_mds_session_head *h;
701 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS);
702 if (!msg) {
703 pr_err("create_session_msg ENOMEM creating msg\n");
704 return NULL;
706 h = msg->front.iov_base;
707 h->op = cpu_to_le32(op);
708 h->seq = cpu_to_le64(seq);
709 return msg;
713 * send session open request.
715 * called under mdsc->mutex
717 static int __open_session(struct ceph_mds_client *mdsc,
718 struct ceph_mds_session *session)
720 struct ceph_msg *msg;
721 int mstate;
722 int mds = session->s_mds;
724 /* wait for mds to go active? */
725 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
726 dout("open_session to mds%d (%s)\n", mds,
727 ceph_mds_state_name(mstate));
728 session->s_state = CEPH_MDS_SESSION_OPENING;
729 session->s_renew_requested = jiffies;
731 /* send connect message */
732 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
733 if (!msg)
734 return -ENOMEM;
735 ceph_con_send(&session->s_con, msg);
736 return 0;
740 * open sessions for any export targets for the given mds
742 * called under mdsc->mutex
744 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
745 struct ceph_mds_session *session)
747 struct ceph_mds_info *mi;
748 struct ceph_mds_session *ts;
749 int i, mds = session->s_mds;
750 int target;
752 if (mds >= mdsc->mdsmap->m_max_mds)
753 return;
754 mi = &mdsc->mdsmap->m_info[mds];
755 dout("open_export_target_sessions for mds%d (%d targets)\n",
756 session->s_mds, mi->num_export_targets);
758 for (i = 0; i < mi->num_export_targets; i++) {
759 target = mi->export_targets[i];
760 ts = __ceph_lookup_mds_session(mdsc, target);
761 if (!ts) {
762 ts = register_session(mdsc, target);
763 if (IS_ERR(ts))
764 return;
766 if (session->s_state == CEPH_MDS_SESSION_NEW ||
767 session->s_state == CEPH_MDS_SESSION_CLOSING)
768 __open_session(mdsc, session);
769 else
770 dout(" mds%d target mds%d %p is %s\n", session->s_mds,
771 i, ts, session_state_name(ts->s_state));
772 ceph_put_mds_session(ts);
776 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
777 struct ceph_mds_session *session)
779 mutex_lock(&mdsc->mutex);
780 __open_export_target_sessions(mdsc, session);
781 mutex_unlock(&mdsc->mutex);
785 * session caps
789 * Free preallocated cap messages assigned to this session
791 static void cleanup_cap_releases(struct ceph_mds_session *session)
793 struct ceph_msg *msg;
795 spin_lock(&session->s_cap_lock);
796 while (!list_empty(&session->s_cap_releases)) {
797 msg = list_first_entry(&session->s_cap_releases,
798 struct ceph_msg, list_head);
799 list_del_init(&msg->list_head);
800 ceph_msg_put(msg);
802 while (!list_empty(&session->s_cap_releases_done)) {
803 msg = list_first_entry(&session->s_cap_releases_done,
804 struct ceph_msg, list_head);
805 list_del_init(&msg->list_head);
806 ceph_msg_put(msg);
808 spin_unlock(&session->s_cap_lock);
812 * Helper to safely iterate over all caps associated with a session, with
813 * special care taken to handle a racing __ceph_remove_cap().
815 * Caller must hold session s_mutex.
817 static int iterate_session_caps(struct ceph_mds_session *session,
818 int (*cb)(struct inode *, struct ceph_cap *,
819 void *), void *arg)
821 struct list_head *p;
822 struct ceph_cap *cap;
823 struct inode *inode, *last_inode = NULL;
824 struct ceph_cap *old_cap = NULL;
825 int ret;
827 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
828 spin_lock(&session->s_cap_lock);
829 p = session->s_caps.next;
830 while (p != &session->s_caps) {
831 cap = list_entry(p, struct ceph_cap, session_caps);
832 inode = igrab(&cap->ci->vfs_inode);
833 if (!inode) {
834 p = p->next;
835 continue;
837 session->s_cap_iterator = cap;
838 spin_unlock(&session->s_cap_lock);
840 if (last_inode) {
841 iput(last_inode);
842 last_inode = NULL;
844 if (old_cap) {
845 ceph_put_cap(session->s_mdsc, old_cap);
846 old_cap = NULL;
849 ret = cb(inode, cap, arg);
850 last_inode = inode;
852 spin_lock(&session->s_cap_lock);
853 p = p->next;
854 if (cap->ci == NULL) {
855 dout("iterate_session_caps finishing cap %p removal\n",
856 cap);
857 BUG_ON(cap->session != session);
858 list_del_init(&cap->session_caps);
859 session->s_nr_caps--;
860 cap->session = NULL;
861 old_cap = cap; /* put_cap it w/o locks held */
863 if (ret < 0)
864 goto out;
866 ret = 0;
867 out:
868 session->s_cap_iterator = NULL;
869 spin_unlock(&session->s_cap_lock);
871 if (last_inode)
872 iput(last_inode);
873 if (old_cap)
874 ceph_put_cap(session->s_mdsc, old_cap);
876 return ret;
879 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
880 void *arg)
882 struct ceph_inode_info *ci = ceph_inode(inode);
883 int drop = 0;
885 dout("removing cap %p, ci is %p, inode is %p\n",
886 cap, ci, &ci->vfs_inode);
887 spin_lock(&inode->i_lock);
888 __ceph_remove_cap(cap);
889 if (!__ceph_is_any_real_caps(ci)) {
890 struct ceph_mds_client *mdsc =
891 ceph_sb_to_client(inode->i_sb)->mdsc;
893 spin_lock(&mdsc->cap_dirty_lock);
894 if (!list_empty(&ci->i_dirty_item)) {
895 pr_info(" dropping dirty %s state for %p %lld\n",
896 ceph_cap_string(ci->i_dirty_caps),
897 inode, ceph_ino(inode));
898 ci->i_dirty_caps = 0;
899 list_del_init(&ci->i_dirty_item);
900 drop = 1;
902 if (!list_empty(&ci->i_flushing_item)) {
903 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
904 ceph_cap_string(ci->i_flushing_caps),
905 inode, ceph_ino(inode));
906 ci->i_flushing_caps = 0;
907 list_del_init(&ci->i_flushing_item);
908 mdsc->num_cap_flushing--;
909 drop = 1;
911 if (drop && ci->i_wrbuffer_ref) {
912 pr_info(" dropping dirty data for %p %lld\n",
913 inode, ceph_ino(inode));
914 ci->i_wrbuffer_ref = 0;
915 ci->i_wrbuffer_ref_head = 0;
916 drop++;
918 spin_unlock(&mdsc->cap_dirty_lock);
920 spin_unlock(&inode->i_lock);
921 while (drop--)
922 iput(inode);
923 return 0;
927 * caller must hold session s_mutex
929 static void remove_session_caps(struct ceph_mds_session *session)
931 dout("remove_session_caps on %p\n", session);
932 iterate_session_caps(session, remove_session_caps_cb, NULL);
933 BUG_ON(session->s_nr_caps > 0);
934 BUG_ON(!list_empty(&session->s_cap_flushing));
935 cleanup_cap_releases(session);
939 * wake up any threads waiting on this session's caps. if the cap is
940 * old (didn't get renewed on the client reconnect), remove it now.
942 * caller must hold s_mutex.
944 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
945 void *arg)
947 struct ceph_inode_info *ci = ceph_inode(inode);
949 wake_up_all(&ci->i_cap_wq);
950 if (arg) {
951 spin_lock(&inode->i_lock);
952 ci->i_wanted_max_size = 0;
953 ci->i_requested_max_size = 0;
954 spin_unlock(&inode->i_lock);
956 return 0;
959 static void wake_up_session_caps(struct ceph_mds_session *session,
960 int reconnect)
962 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
963 iterate_session_caps(session, wake_up_session_cb,
964 (void *)(unsigned long)reconnect);
968 * Send periodic message to MDS renewing all currently held caps. The
969 * ack will reset the expiration for all caps from this session.
971 * caller holds s_mutex
973 static int send_renew_caps(struct ceph_mds_client *mdsc,
974 struct ceph_mds_session *session)
976 struct ceph_msg *msg;
977 int state;
979 if (time_after_eq(jiffies, session->s_cap_ttl) &&
980 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
981 pr_info("mds%d caps stale\n", session->s_mds);
982 session->s_renew_requested = jiffies;
984 /* do not try to renew caps until a recovering mds has reconnected
985 * with its clients. */
986 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
987 if (state < CEPH_MDS_STATE_RECONNECT) {
988 dout("send_renew_caps ignoring mds%d (%s)\n",
989 session->s_mds, ceph_mds_state_name(state));
990 return 0;
993 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
994 ceph_mds_state_name(state));
995 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
996 ++session->s_renew_seq);
997 if (!msg)
998 return -ENOMEM;
999 ceph_con_send(&session->s_con, msg);
1000 return 0;
1004 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1006 * Called under session->s_mutex
1008 static void renewed_caps(struct ceph_mds_client *mdsc,
1009 struct ceph_mds_session *session, int is_renew)
1011 int was_stale;
1012 int wake = 0;
1014 spin_lock(&session->s_cap_lock);
1015 was_stale = is_renew && (session->s_cap_ttl == 0 ||
1016 time_after_eq(jiffies, session->s_cap_ttl));
1018 session->s_cap_ttl = session->s_renew_requested +
1019 mdsc->mdsmap->m_session_timeout*HZ;
1021 if (was_stale) {
1022 if (time_before(jiffies, session->s_cap_ttl)) {
1023 pr_info("mds%d caps renewed\n", session->s_mds);
1024 wake = 1;
1025 } else {
1026 pr_info("mds%d caps still stale\n", session->s_mds);
1029 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1030 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1031 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1032 spin_unlock(&session->s_cap_lock);
1034 if (wake)
1035 wake_up_session_caps(session, 0);
1039 * send a session close request
1041 static int request_close_session(struct ceph_mds_client *mdsc,
1042 struct ceph_mds_session *session)
1044 struct ceph_msg *msg;
1046 dout("request_close_session mds%d state %s seq %lld\n",
1047 session->s_mds, session_state_name(session->s_state),
1048 session->s_seq);
1049 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1050 if (!msg)
1051 return -ENOMEM;
1052 ceph_con_send(&session->s_con, msg);
1053 return 0;
1057 * Called with s_mutex held.
1059 static int __close_session(struct ceph_mds_client *mdsc,
1060 struct ceph_mds_session *session)
1062 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1063 return 0;
1064 session->s_state = CEPH_MDS_SESSION_CLOSING;
1065 return request_close_session(mdsc, session);
1069 * Trim old(er) caps.
1071 * Because we can't cache an inode without one or more caps, we do
1072 * this indirectly: if a cap is unused, we prune its aliases, at which
1073 * point the inode will hopefully get dropped to.
1075 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1076 * memory pressure from the MDS, though, so it needn't be perfect.
1078 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1080 struct ceph_mds_session *session = arg;
1081 struct ceph_inode_info *ci = ceph_inode(inode);
1082 int used, oissued, mine;
1084 if (session->s_trim_caps <= 0)
1085 return -1;
1087 spin_lock(&inode->i_lock);
1088 mine = cap->issued | cap->implemented;
1089 used = __ceph_caps_used(ci);
1090 oissued = __ceph_caps_issued_other(ci, cap);
1092 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1093 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1094 ceph_cap_string(used));
1095 if (ci->i_dirty_caps)
1096 goto out; /* dirty caps */
1097 if ((used & ~oissued) & mine)
1098 goto out; /* we need these caps */
1100 session->s_trim_caps--;
1101 if (oissued) {
1102 /* we aren't the only cap.. just remove us */
1103 __ceph_remove_cap(cap);
1104 } else {
1105 /* try to drop referring dentries */
1106 spin_unlock(&inode->i_lock);
1107 d_prune_aliases(inode);
1108 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1109 inode, cap, atomic_read(&inode->i_count));
1110 return 0;
1113 out:
1114 spin_unlock(&inode->i_lock);
1115 return 0;
1119 * Trim session cap count down to some max number.
1121 static int trim_caps(struct ceph_mds_client *mdsc,
1122 struct ceph_mds_session *session,
1123 int max_caps)
1125 int trim_caps = session->s_nr_caps - max_caps;
1127 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1128 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1129 if (trim_caps > 0) {
1130 session->s_trim_caps = trim_caps;
1131 iterate_session_caps(session, trim_caps_cb, session);
1132 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1133 session->s_mds, session->s_nr_caps, max_caps,
1134 trim_caps - session->s_trim_caps);
1135 session->s_trim_caps = 0;
1137 return 0;
1141 * Allocate cap_release messages. If there is a partially full message
1142 * in the queue, try to allocate enough to cover it's remainder, so that
1143 * we can send it immediately.
1145 * Called under s_mutex.
1147 int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
1148 struct ceph_mds_session *session)
1150 struct ceph_msg *msg, *partial = NULL;
1151 struct ceph_mds_cap_release *head;
1152 int err = -ENOMEM;
1153 int extra = mdsc->fsc->mount_options->cap_release_safety;
1154 int num;
1156 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1157 extra);
1159 spin_lock(&session->s_cap_lock);
1161 if (!list_empty(&session->s_cap_releases)) {
1162 msg = list_first_entry(&session->s_cap_releases,
1163 struct ceph_msg,
1164 list_head);
1165 head = msg->front.iov_base;
1166 num = le32_to_cpu(head->num);
1167 if (num) {
1168 dout(" partial %p with (%d/%d)\n", msg, num,
1169 (int)CEPH_CAPS_PER_RELEASE);
1170 extra += CEPH_CAPS_PER_RELEASE - num;
1171 partial = msg;
1174 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1175 spin_unlock(&session->s_cap_lock);
1176 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1177 GFP_NOFS);
1178 if (!msg)
1179 goto out_unlocked;
1180 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1181 (int)msg->front.iov_len);
1182 head = msg->front.iov_base;
1183 head->num = cpu_to_le32(0);
1184 msg->front.iov_len = sizeof(*head);
1185 spin_lock(&session->s_cap_lock);
1186 list_add(&msg->list_head, &session->s_cap_releases);
1187 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1190 if (partial) {
1191 head = partial->front.iov_base;
1192 num = le32_to_cpu(head->num);
1193 dout(" queueing partial %p with %d/%d\n", partial, num,
1194 (int)CEPH_CAPS_PER_RELEASE);
1195 list_move_tail(&partial->list_head,
1196 &session->s_cap_releases_done);
1197 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
1199 err = 0;
1200 spin_unlock(&session->s_cap_lock);
1201 out_unlocked:
1202 return err;
1206 * flush all dirty inode data to disk.
1208 * returns true if we've flushed through want_flush_seq
1210 static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1212 int mds, ret = 1;
1214 dout("check_cap_flush want %lld\n", want_flush_seq);
1215 mutex_lock(&mdsc->mutex);
1216 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1217 struct ceph_mds_session *session = mdsc->sessions[mds];
1219 if (!session)
1220 continue;
1221 get_session(session);
1222 mutex_unlock(&mdsc->mutex);
1224 mutex_lock(&session->s_mutex);
1225 if (!list_empty(&session->s_cap_flushing)) {
1226 struct ceph_inode_info *ci =
1227 list_entry(session->s_cap_flushing.next,
1228 struct ceph_inode_info,
1229 i_flushing_item);
1230 struct inode *inode = &ci->vfs_inode;
1232 spin_lock(&inode->i_lock);
1233 if (ci->i_cap_flush_seq <= want_flush_seq) {
1234 dout("check_cap_flush still flushing %p "
1235 "seq %lld <= %lld to mds%d\n", inode,
1236 ci->i_cap_flush_seq, want_flush_seq,
1237 session->s_mds);
1238 ret = 0;
1240 spin_unlock(&inode->i_lock);
1242 mutex_unlock(&session->s_mutex);
1243 ceph_put_mds_session(session);
1245 if (!ret)
1246 return ret;
1247 mutex_lock(&mdsc->mutex);
1250 mutex_unlock(&mdsc->mutex);
1251 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1252 return ret;
1256 * called under s_mutex
1258 void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1259 struct ceph_mds_session *session)
1261 struct ceph_msg *msg;
1263 dout("send_cap_releases mds%d\n", session->s_mds);
1264 spin_lock(&session->s_cap_lock);
1265 while (!list_empty(&session->s_cap_releases_done)) {
1266 msg = list_first_entry(&session->s_cap_releases_done,
1267 struct ceph_msg, list_head);
1268 list_del_init(&msg->list_head);
1269 spin_unlock(&session->s_cap_lock);
1270 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1271 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1272 ceph_con_send(&session->s_con, msg);
1273 spin_lock(&session->s_cap_lock);
1275 spin_unlock(&session->s_cap_lock);
1278 static void discard_cap_releases(struct ceph_mds_client *mdsc,
1279 struct ceph_mds_session *session)
1281 struct ceph_msg *msg;
1282 struct ceph_mds_cap_release *head;
1283 unsigned num;
1285 dout("discard_cap_releases mds%d\n", session->s_mds);
1286 spin_lock(&session->s_cap_lock);
1288 /* zero out the in-progress message */
1289 msg = list_first_entry(&session->s_cap_releases,
1290 struct ceph_msg, list_head);
1291 head = msg->front.iov_base;
1292 num = le32_to_cpu(head->num);
1293 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1294 head->num = cpu_to_le32(0);
1295 session->s_num_cap_releases += num;
1297 /* requeue completed messages */
1298 while (!list_empty(&session->s_cap_releases_done)) {
1299 msg = list_first_entry(&session->s_cap_releases_done,
1300 struct ceph_msg, list_head);
1301 list_del_init(&msg->list_head);
1303 head = msg->front.iov_base;
1304 num = le32_to_cpu(head->num);
1305 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1306 num);
1307 session->s_num_cap_releases += num;
1308 head->num = cpu_to_le32(0);
1309 msg->front.iov_len = sizeof(*head);
1310 list_add(&msg->list_head, &session->s_cap_releases);
1313 spin_unlock(&session->s_cap_lock);
1317 * requests
1321 * Create an mds request.
1323 struct ceph_mds_request *
1324 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1326 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1328 if (!req)
1329 return ERR_PTR(-ENOMEM);
1331 mutex_init(&req->r_fill_mutex);
1332 req->r_mdsc = mdsc;
1333 req->r_started = jiffies;
1334 req->r_resend_mds = -1;
1335 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1336 req->r_fmode = -1;
1337 kref_init(&req->r_kref);
1338 INIT_LIST_HEAD(&req->r_wait);
1339 init_completion(&req->r_completion);
1340 init_completion(&req->r_safe_completion);
1341 INIT_LIST_HEAD(&req->r_unsafe_item);
1343 req->r_op = op;
1344 req->r_direct_mode = mode;
1345 return req;
1349 * return oldest (lowest) request, tid in request tree, 0 if none.
1351 * called under mdsc->mutex.
1353 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1355 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1356 return NULL;
1357 return rb_entry(rb_first(&mdsc->request_tree),
1358 struct ceph_mds_request, r_node);
1361 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1363 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1365 if (req)
1366 return req->r_tid;
1367 return 0;
1371 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1372 * on build_path_from_dentry in fs/cifs/dir.c.
1374 * If @stop_on_nosnap, generate path relative to the first non-snapped
1375 * inode.
1377 * Encode hidden .snap dirs as a double /, i.e.
1378 * foo/.snap/bar -> foo//bar
1380 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1381 int stop_on_nosnap)
1383 struct dentry *temp;
1384 char *path;
1385 int len, pos;
1387 if (dentry == NULL)
1388 return ERR_PTR(-EINVAL);
1390 retry:
1391 len = 0;
1392 for (temp = dentry; !IS_ROOT(temp);) {
1393 struct inode *inode = temp->d_inode;
1394 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1395 len++; /* slash only */
1396 else if (stop_on_nosnap && inode &&
1397 ceph_snap(inode) == CEPH_NOSNAP)
1398 break;
1399 else
1400 len += 1 + temp->d_name.len;
1401 temp = temp->d_parent;
1402 if (temp == NULL) {
1403 pr_err("build_path corrupt dentry %p\n", dentry);
1404 return ERR_PTR(-EINVAL);
1407 if (len)
1408 len--; /* no leading '/' */
1410 path = kmalloc(len+1, GFP_NOFS);
1411 if (path == NULL)
1412 return ERR_PTR(-ENOMEM);
1413 pos = len;
1414 path[pos] = 0; /* trailing null */
1415 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1416 struct inode *inode = temp->d_inode;
1418 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1419 dout("build_path path+%d: %p SNAPDIR\n",
1420 pos, temp);
1421 } else if (stop_on_nosnap && inode &&
1422 ceph_snap(inode) == CEPH_NOSNAP) {
1423 break;
1424 } else {
1425 pos -= temp->d_name.len;
1426 if (pos < 0)
1427 break;
1428 strncpy(path + pos, temp->d_name.name,
1429 temp->d_name.len);
1431 if (pos)
1432 path[--pos] = '/';
1433 temp = temp->d_parent;
1434 if (temp == NULL) {
1435 pr_err("build_path corrupt dentry\n");
1436 kfree(path);
1437 return ERR_PTR(-EINVAL);
1440 if (pos != 0) {
1441 pr_err("build_path did not end path lookup where "
1442 "expected, namelen is %d, pos is %d\n", len, pos);
1443 /* presumably this is only possible if racing with a
1444 rename of one of the parent directories (we can not
1445 lock the dentries above us to prevent this, but
1446 retrying should be harmless) */
1447 kfree(path);
1448 goto retry;
1451 *base = ceph_ino(temp->d_inode);
1452 *plen = len;
1453 dout("build_path on %p %d built %llx '%.*s'\n",
1454 dentry, atomic_read(&dentry->d_count), *base, len, path);
1455 return path;
1458 static int build_dentry_path(struct dentry *dentry,
1459 const char **ppath, int *ppathlen, u64 *pino,
1460 int *pfreepath)
1462 char *path;
1464 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1465 *pino = ceph_ino(dentry->d_parent->d_inode);
1466 *ppath = dentry->d_name.name;
1467 *ppathlen = dentry->d_name.len;
1468 return 0;
1470 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1471 if (IS_ERR(path))
1472 return PTR_ERR(path);
1473 *ppath = path;
1474 *pfreepath = 1;
1475 return 0;
1478 static int build_inode_path(struct inode *inode,
1479 const char **ppath, int *ppathlen, u64 *pino,
1480 int *pfreepath)
1482 struct dentry *dentry;
1483 char *path;
1485 if (ceph_snap(inode) == CEPH_NOSNAP) {
1486 *pino = ceph_ino(inode);
1487 *ppathlen = 0;
1488 return 0;
1490 dentry = d_find_alias(inode);
1491 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1492 dput(dentry);
1493 if (IS_ERR(path))
1494 return PTR_ERR(path);
1495 *ppath = path;
1496 *pfreepath = 1;
1497 return 0;
1501 * request arguments may be specified via an inode *, a dentry *, or
1502 * an explicit ino+path.
1504 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1505 const char *rpath, u64 rino,
1506 const char **ppath, int *pathlen,
1507 u64 *ino, int *freepath)
1509 int r = 0;
1511 if (rinode) {
1512 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1513 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1514 ceph_snap(rinode));
1515 } else if (rdentry) {
1516 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1517 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1518 *ppath);
1519 } else if (rpath) {
1520 *ino = rino;
1521 *ppath = rpath;
1522 *pathlen = strlen(rpath);
1523 dout(" path %.*s\n", *pathlen, rpath);
1526 return r;
1530 * called under mdsc->mutex
1532 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1533 struct ceph_mds_request *req,
1534 int mds)
1536 struct ceph_msg *msg;
1537 struct ceph_mds_request_head *head;
1538 const char *path1 = NULL;
1539 const char *path2 = NULL;
1540 u64 ino1 = 0, ino2 = 0;
1541 int pathlen1 = 0, pathlen2 = 0;
1542 int freepath1 = 0, freepath2 = 0;
1543 int len;
1544 u16 releases;
1545 void *p, *end;
1546 int ret;
1548 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1549 req->r_path1, req->r_ino1.ino,
1550 &path1, &pathlen1, &ino1, &freepath1);
1551 if (ret < 0) {
1552 msg = ERR_PTR(ret);
1553 goto out;
1556 ret = set_request_path_attr(NULL, req->r_old_dentry,
1557 req->r_path2, req->r_ino2.ino,
1558 &path2, &pathlen2, &ino2, &freepath2);
1559 if (ret < 0) {
1560 msg = ERR_PTR(ret);
1561 goto out_free1;
1564 len = sizeof(*head) +
1565 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
1567 /* calculate (max) length for cap releases */
1568 len += sizeof(struct ceph_mds_request_release) *
1569 (!!req->r_inode_drop + !!req->r_dentry_drop +
1570 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1571 if (req->r_dentry_drop)
1572 len += req->r_dentry->d_name.len;
1573 if (req->r_old_dentry_drop)
1574 len += req->r_old_dentry->d_name.len;
1576 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS);
1577 if (!msg) {
1578 msg = ERR_PTR(-ENOMEM);
1579 goto out_free2;
1582 msg->hdr.tid = cpu_to_le64(req->r_tid);
1584 head = msg->front.iov_base;
1585 p = msg->front.iov_base + sizeof(*head);
1586 end = msg->front.iov_base + msg->front.iov_len;
1588 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1589 head->op = cpu_to_le32(req->r_op);
1590 head->caller_uid = cpu_to_le32(current_fsuid());
1591 head->caller_gid = cpu_to_le32(current_fsgid());
1592 head->args = req->r_args;
1594 ceph_encode_filepath(&p, end, ino1, path1);
1595 ceph_encode_filepath(&p, end, ino2, path2);
1597 /* make note of release offset, in case we need to replay */
1598 req->r_request_release_offset = p - msg->front.iov_base;
1600 /* cap releases */
1601 releases = 0;
1602 if (req->r_inode_drop)
1603 releases += ceph_encode_inode_release(&p,
1604 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1605 mds, req->r_inode_drop, req->r_inode_unless, 0);
1606 if (req->r_dentry_drop)
1607 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1608 mds, req->r_dentry_drop, req->r_dentry_unless);
1609 if (req->r_old_dentry_drop)
1610 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1611 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1612 if (req->r_old_inode_drop)
1613 releases += ceph_encode_inode_release(&p,
1614 req->r_old_dentry->d_inode,
1615 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1616 head->num_releases = cpu_to_le16(releases);
1618 BUG_ON(p > end);
1619 msg->front.iov_len = p - msg->front.iov_base;
1620 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1622 msg->pages = req->r_pages;
1623 msg->nr_pages = req->r_num_pages;
1624 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1625 msg->hdr.data_off = cpu_to_le16(0);
1627 out_free2:
1628 if (freepath2)
1629 kfree((char *)path2);
1630 out_free1:
1631 if (freepath1)
1632 kfree((char *)path1);
1633 out:
1634 return msg;
1638 * called under mdsc->mutex if error, under no mutex if
1639 * success.
1641 static void complete_request(struct ceph_mds_client *mdsc,
1642 struct ceph_mds_request *req)
1644 if (req->r_callback)
1645 req->r_callback(mdsc, req);
1646 else
1647 complete_all(&req->r_completion);
1651 * called under mdsc->mutex
1653 static int __prepare_send_request(struct ceph_mds_client *mdsc,
1654 struct ceph_mds_request *req,
1655 int mds)
1657 struct ceph_mds_request_head *rhead;
1658 struct ceph_msg *msg;
1659 int flags = 0;
1661 req->r_mds = mds;
1662 req->r_attempts++;
1663 if (req->r_inode) {
1664 struct ceph_cap *cap =
1665 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1667 if (cap)
1668 req->r_sent_on_mseq = cap->mseq;
1669 else
1670 req->r_sent_on_mseq = -1;
1672 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1673 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1675 if (req->r_got_unsafe) {
1677 * Replay. Do not regenerate message (and rebuild
1678 * paths, etc.); just use the original message.
1679 * Rebuilding paths will break for renames because
1680 * d_move mangles the src name.
1682 msg = req->r_request;
1683 rhead = msg->front.iov_base;
1685 flags = le32_to_cpu(rhead->flags);
1686 flags |= CEPH_MDS_FLAG_REPLAY;
1687 rhead->flags = cpu_to_le32(flags);
1689 if (req->r_target_inode)
1690 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1692 rhead->num_retry = req->r_attempts - 1;
1694 /* remove cap/dentry releases from message */
1695 rhead->num_releases = 0;
1696 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1697 msg->front.iov_len = req->r_request_release_offset;
1698 return 0;
1701 if (req->r_request) {
1702 ceph_msg_put(req->r_request);
1703 req->r_request = NULL;
1705 msg = create_request_message(mdsc, req, mds);
1706 if (IS_ERR(msg)) {
1707 req->r_err = PTR_ERR(msg);
1708 complete_request(mdsc, req);
1709 return PTR_ERR(msg);
1711 req->r_request = msg;
1713 rhead = msg->front.iov_base;
1714 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1715 if (req->r_got_unsafe)
1716 flags |= CEPH_MDS_FLAG_REPLAY;
1717 if (req->r_locked_dir)
1718 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1719 rhead->flags = cpu_to_le32(flags);
1720 rhead->num_fwd = req->r_num_fwd;
1721 rhead->num_retry = req->r_attempts - 1;
1722 rhead->ino = 0;
1724 dout(" r_locked_dir = %p\n", req->r_locked_dir);
1725 return 0;
1729 * send request, or put it on the appropriate wait list.
1731 static int __do_request(struct ceph_mds_client *mdsc,
1732 struct ceph_mds_request *req)
1734 struct ceph_mds_session *session = NULL;
1735 int mds = -1;
1736 int err = -EAGAIN;
1738 if (req->r_err || req->r_got_result)
1739 goto out;
1741 if (req->r_timeout &&
1742 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1743 dout("do_request timed out\n");
1744 err = -EIO;
1745 goto finish;
1748 mds = __choose_mds(mdsc, req);
1749 if (mds < 0 ||
1750 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1751 dout("do_request no mds or not active, waiting for map\n");
1752 list_add(&req->r_wait, &mdsc->waiting_for_map);
1753 goto out;
1756 /* get, open session */
1757 session = __ceph_lookup_mds_session(mdsc, mds);
1758 if (!session) {
1759 session = register_session(mdsc, mds);
1760 if (IS_ERR(session)) {
1761 err = PTR_ERR(session);
1762 goto finish;
1765 dout("do_request mds%d session %p state %s\n", mds, session,
1766 session_state_name(session->s_state));
1767 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1768 session->s_state != CEPH_MDS_SESSION_HUNG) {
1769 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1770 session->s_state == CEPH_MDS_SESSION_CLOSING)
1771 __open_session(mdsc, session);
1772 list_add(&req->r_wait, &session->s_waiting);
1773 goto out_session;
1776 /* send request */
1777 req->r_session = get_session(session);
1778 req->r_resend_mds = -1; /* forget any previous mds hint */
1780 if (req->r_request_started == 0) /* note request start time */
1781 req->r_request_started = jiffies;
1783 err = __prepare_send_request(mdsc, req, mds);
1784 if (!err) {
1785 ceph_msg_get(req->r_request);
1786 ceph_con_send(&session->s_con, req->r_request);
1789 out_session:
1790 ceph_put_mds_session(session);
1791 out:
1792 return err;
1794 finish:
1795 req->r_err = err;
1796 complete_request(mdsc, req);
1797 goto out;
1801 * called under mdsc->mutex
1803 static void __wake_requests(struct ceph_mds_client *mdsc,
1804 struct list_head *head)
1806 struct ceph_mds_request *req, *nreq;
1808 list_for_each_entry_safe(req, nreq, head, r_wait) {
1809 list_del_init(&req->r_wait);
1810 __do_request(mdsc, req);
1815 * Wake up threads with requests pending for @mds, so that they can
1816 * resubmit their requests to a possibly different mds.
1818 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
1820 struct ceph_mds_request *req;
1821 struct rb_node *p;
1823 dout("kick_requests mds%d\n", mds);
1824 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1825 req = rb_entry(p, struct ceph_mds_request, r_node);
1826 if (req->r_got_unsafe)
1827 continue;
1828 if (req->r_session &&
1829 req->r_session->s_mds == mds) {
1830 dout(" kicking tid %llu\n", req->r_tid);
1831 put_request_session(req);
1832 __do_request(mdsc, req);
1837 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1838 struct ceph_mds_request *req)
1840 dout("submit_request on %p\n", req);
1841 mutex_lock(&mdsc->mutex);
1842 __register_request(mdsc, req, NULL);
1843 __do_request(mdsc, req);
1844 mutex_unlock(&mdsc->mutex);
1848 * Synchrously perform an mds request. Take care of all of the
1849 * session setup, forwarding, retry details.
1851 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1852 struct inode *dir,
1853 struct ceph_mds_request *req)
1855 int err;
1857 dout("do_request on %p\n", req);
1859 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1860 if (req->r_inode)
1861 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1862 if (req->r_locked_dir)
1863 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1864 if (req->r_old_dentry)
1865 ceph_get_cap_refs(
1866 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1867 CEPH_CAP_PIN);
1869 /* issue */
1870 mutex_lock(&mdsc->mutex);
1871 __register_request(mdsc, req, dir);
1872 __do_request(mdsc, req);
1874 if (req->r_err) {
1875 err = req->r_err;
1876 __unregister_request(mdsc, req);
1877 dout("do_request early error %d\n", err);
1878 goto out;
1881 /* wait */
1882 mutex_unlock(&mdsc->mutex);
1883 dout("do_request waiting\n");
1884 if (req->r_timeout) {
1885 err = (long)wait_for_completion_killable_timeout(
1886 &req->r_completion, req->r_timeout);
1887 if (err == 0)
1888 err = -EIO;
1889 } else {
1890 err = wait_for_completion_killable(&req->r_completion);
1892 dout("do_request waited, got %d\n", err);
1893 mutex_lock(&mdsc->mutex);
1895 /* only abort if we didn't race with a real reply */
1896 if (req->r_got_result) {
1897 err = le32_to_cpu(req->r_reply_info.head->result);
1898 } else if (err < 0) {
1899 dout("aborted request %lld with %d\n", req->r_tid, err);
1902 * ensure we aren't running concurrently with
1903 * ceph_fill_trace or ceph_readdir_prepopulate, which
1904 * rely on locks (dir mutex) held by our caller.
1906 mutex_lock(&req->r_fill_mutex);
1907 req->r_err = err;
1908 req->r_aborted = true;
1909 mutex_unlock(&req->r_fill_mutex);
1911 if (req->r_locked_dir &&
1912 (req->r_op & CEPH_MDS_OP_WRITE))
1913 ceph_invalidate_dir_request(req);
1914 } else {
1915 err = req->r_err;
1918 out:
1919 mutex_unlock(&mdsc->mutex);
1920 dout("do_request %p done, result %d\n", req, err);
1921 return err;
1925 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1926 * namespace request.
1928 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
1930 struct inode *inode = req->r_locked_dir;
1931 struct ceph_inode_info *ci = ceph_inode(inode);
1933 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode);
1934 spin_lock(&inode->i_lock);
1935 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1936 ci->i_release_count++;
1937 spin_unlock(&inode->i_lock);
1939 if (req->r_dentry)
1940 ceph_invalidate_dentry_lease(req->r_dentry);
1941 if (req->r_old_dentry)
1942 ceph_invalidate_dentry_lease(req->r_old_dentry);
1946 * Handle mds reply.
1948 * We take the session mutex and parse and process the reply immediately.
1949 * This preserves the logical ordering of replies, capabilities, etc., sent
1950 * by the MDS as they are applied to our local cache.
1952 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
1954 struct ceph_mds_client *mdsc = session->s_mdsc;
1955 struct ceph_mds_request *req;
1956 struct ceph_mds_reply_head *head = msg->front.iov_base;
1957 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
1958 u64 tid;
1959 int err, result;
1960 int mds = session->s_mds;
1962 if (msg->front.iov_len < sizeof(*head)) {
1963 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
1964 ceph_msg_dump(msg);
1965 return;
1968 /* get request, session */
1969 tid = le64_to_cpu(msg->hdr.tid);
1970 mutex_lock(&mdsc->mutex);
1971 req = __lookup_request(mdsc, tid);
1972 if (!req) {
1973 dout("handle_reply on unknown tid %llu\n", tid);
1974 mutex_unlock(&mdsc->mutex);
1975 return;
1977 dout("handle_reply %p\n", req);
1979 /* correct session? */
1980 if (req->r_session != session) {
1981 pr_err("mdsc_handle_reply got %llu on session mds%d"
1982 " not mds%d\n", tid, session->s_mds,
1983 req->r_session ? req->r_session->s_mds : -1);
1984 mutex_unlock(&mdsc->mutex);
1985 goto out;
1988 /* dup? */
1989 if ((req->r_got_unsafe && !head->safe) ||
1990 (req->r_got_safe && head->safe)) {
1991 pr_warning("got a dup %s reply on %llu from mds%d\n",
1992 head->safe ? "safe" : "unsafe", tid, mds);
1993 mutex_unlock(&mdsc->mutex);
1994 goto out;
1996 if (req->r_got_safe && !head->safe) {
1997 pr_warning("got unsafe after safe on %llu from mds%d\n",
1998 tid, mds);
1999 mutex_unlock(&mdsc->mutex);
2000 goto out;
2003 result = le32_to_cpu(head->result);
2006 * Handle an ESTALE
2007 * if we're not talking to the authority, send to them
2008 * if the authority has changed while we weren't looking,
2009 * send to new authority
2010 * Otherwise we just have to return an ESTALE
2012 if (result == -ESTALE) {
2013 dout("got ESTALE on request %llu", req->r_tid);
2014 if (!req->r_inode) {
2015 /* do nothing; not an authority problem */
2016 } else if (req->r_direct_mode != USE_AUTH_MDS) {
2017 dout("not using auth, setting for that now");
2018 req->r_direct_mode = USE_AUTH_MDS;
2019 __do_request(mdsc, req);
2020 mutex_unlock(&mdsc->mutex);
2021 goto out;
2022 } else {
2023 struct ceph_inode_info *ci = ceph_inode(req->r_inode);
2024 struct ceph_cap *cap =
2025 ceph_get_cap_for_mds(ci, req->r_mds);;
2027 dout("already using auth");
2028 if ((!cap || cap != ci->i_auth_cap) ||
2029 (cap->mseq != req->r_sent_on_mseq)) {
2030 dout("but cap changed, so resending");
2031 __do_request(mdsc, req);
2032 mutex_unlock(&mdsc->mutex);
2033 goto out;
2036 dout("have to return ESTALE on request %llu", req->r_tid);
2040 if (head->safe) {
2041 req->r_got_safe = true;
2042 __unregister_request(mdsc, req);
2043 complete_all(&req->r_safe_completion);
2045 if (req->r_got_unsafe) {
2047 * We already handled the unsafe response, now do the
2048 * cleanup. No need to examine the response; the MDS
2049 * doesn't include any result info in the safe
2050 * response. And even if it did, there is nothing
2051 * useful we could do with a revised return value.
2053 dout("got safe reply %llu, mds%d\n", tid, mds);
2054 list_del_init(&req->r_unsafe_item);
2056 /* last unsafe request during umount? */
2057 if (mdsc->stopping && !__get_oldest_req(mdsc))
2058 complete_all(&mdsc->safe_umount_waiters);
2059 mutex_unlock(&mdsc->mutex);
2060 goto out;
2062 } else {
2063 req->r_got_unsafe = true;
2064 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2067 dout("handle_reply tid %lld result %d\n", tid, result);
2068 rinfo = &req->r_reply_info;
2069 err = parse_reply_info(msg, rinfo);
2070 mutex_unlock(&mdsc->mutex);
2072 mutex_lock(&session->s_mutex);
2073 if (err < 0) {
2074 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds);
2075 ceph_msg_dump(msg);
2076 goto out_err;
2079 /* snap trace */
2080 if (rinfo->snapblob_len) {
2081 down_write(&mdsc->snap_rwsem);
2082 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2083 rinfo->snapblob + rinfo->snapblob_len,
2084 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2085 downgrade_write(&mdsc->snap_rwsem);
2086 } else {
2087 down_read(&mdsc->snap_rwsem);
2090 /* insert trace into our cache */
2091 mutex_lock(&req->r_fill_mutex);
2092 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
2093 if (err == 0) {
2094 if (result == 0 && rinfo->dir_nr)
2095 ceph_readdir_prepopulate(req, req->r_session);
2096 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2098 mutex_unlock(&req->r_fill_mutex);
2100 up_read(&mdsc->snap_rwsem);
2101 out_err:
2102 mutex_lock(&mdsc->mutex);
2103 if (!req->r_aborted) {
2104 if (err) {
2105 req->r_err = err;
2106 } else {
2107 req->r_reply = msg;
2108 ceph_msg_get(msg);
2109 req->r_got_result = true;
2111 } else {
2112 dout("reply arrived after request %lld was aborted\n", tid);
2114 mutex_unlock(&mdsc->mutex);
2116 ceph_add_cap_releases(mdsc, req->r_session);
2117 mutex_unlock(&session->s_mutex);
2119 /* kick calling process */
2120 complete_request(mdsc, req);
2121 out:
2122 ceph_mdsc_put_request(req);
2123 return;
2129 * handle mds notification that our request has been forwarded.
2131 static void handle_forward(struct ceph_mds_client *mdsc,
2132 struct ceph_mds_session *session,
2133 struct ceph_msg *msg)
2135 struct ceph_mds_request *req;
2136 u64 tid = le64_to_cpu(msg->hdr.tid);
2137 u32 next_mds;
2138 u32 fwd_seq;
2139 int err = -EINVAL;
2140 void *p = msg->front.iov_base;
2141 void *end = p + msg->front.iov_len;
2143 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2144 next_mds = ceph_decode_32(&p);
2145 fwd_seq = ceph_decode_32(&p);
2147 mutex_lock(&mdsc->mutex);
2148 req = __lookup_request(mdsc, tid);
2149 if (!req) {
2150 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2151 goto out; /* dup reply? */
2154 if (req->r_aborted) {
2155 dout("forward tid %llu aborted, unregistering\n", tid);
2156 __unregister_request(mdsc, req);
2157 } else if (fwd_seq <= req->r_num_fwd) {
2158 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2159 tid, next_mds, req->r_num_fwd, fwd_seq);
2160 } else {
2161 /* resend. forward race not possible; mds would drop */
2162 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2163 BUG_ON(req->r_err);
2164 BUG_ON(req->r_got_result);
2165 req->r_num_fwd = fwd_seq;
2166 req->r_resend_mds = next_mds;
2167 put_request_session(req);
2168 __do_request(mdsc, req);
2170 ceph_mdsc_put_request(req);
2171 out:
2172 mutex_unlock(&mdsc->mutex);
2173 return;
2175 bad:
2176 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2180 * handle a mds session control message
2182 static void handle_session(struct ceph_mds_session *session,
2183 struct ceph_msg *msg)
2185 struct ceph_mds_client *mdsc = session->s_mdsc;
2186 u32 op;
2187 u64 seq;
2188 int mds = session->s_mds;
2189 struct ceph_mds_session_head *h = msg->front.iov_base;
2190 int wake = 0;
2192 /* decode */
2193 if (msg->front.iov_len != sizeof(*h))
2194 goto bad;
2195 op = le32_to_cpu(h->op);
2196 seq = le64_to_cpu(h->seq);
2198 mutex_lock(&mdsc->mutex);
2199 if (op == CEPH_SESSION_CLOSE)
2200 __unregister_session(mdsc, session);
2201 /* FIXME: this ttl calculation is generous */
2202 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2203 mutex_unlock(&mdsc->mutex);
2205 mutex_lock(&session->s_mutex);
2207 dout("handle_session mds%d %s %p state %s seq %llu\n",
2208 mds, ceph_session_op_name(op), session,
2209 session_state_name(session->s_state), seq);
2211 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2212 session->s_state = CEPH_MDS_SESSION_OPEN;
2213 pr_info("mds%d came back\n", session->s_mds);
2216 switch (op) {
2217 case CEPH_SESSION_OPEN:
2218 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2219 pr_info("mds%d reconnect success\n", session->s_mds);
2220 session->s_state = CEPH_MDS_SESSION_OPEN;
2221 renewed_caps(mdsc, session, 0);
2222 wake = 1;
2223 if (mdsc->stopping)
2224 __close_session(mdsc, session);
2225 break;
2227 case CEPH_SESSION_RENEWCAPS:
2228 if (session->s_renew_seq == seq)
2229 renewed_caps(mdsc, session, 1);
2230 break;
2232 case CEPH_SESSION_CLOSE:
2233 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2234 pr_info("mds%d reconnect denied\n", session->s_mds);
2235 remove_session_caps(session);
2236 wake = 1; /* for good measure */
2237 wake_up_all(&mdsc->session_close_wq);
2238 kick_requests(mdsc, mds);
2239 break;
2241 case CEPH_SESSION_STALE:
2242 pr_info("mds%d caps went stale, renewing\n",
2243 session->s_mds);
2244 spin_lock(&session->s_cap_lock);
2245 session->s_cap_gen++;
2246 session->s_cap_ttl = 0;
2247 spin_unlock(&session->s_cap_lock);
2248 send_renew_caps(mdsc, session);
2249 break;
2251 case CEPH_SESSION_RECALL_STATE:
2252 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2253 break;
2255 default:
2256 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2257 WARN_ON(1);
2260 mutex_unlock(&session->s_mutex);
2261 if (wake) {
2262 mutex_lock(&mdsc->mutex);
2263 __wake_requests(mdsc, &session->s_waiting);
2264 mutex_unlock(&mdsc->mutex);
2266 return;
2268 bad:
2269 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2270 (int)msg->front.iov_len);
2271 ceph_msg_dump(msg);
2272 return;
2277 * called under session->mutex.
2279 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2280 struct ceph_mds_session *session)
2282 struct ceph_mds_request *req, *nreq;
2283 int err;
2285 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2287 mutex_lock(&mdsc->mutex);
2288 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2289 err = __prepare_send_request(mdsc, req, session->s_mds);
2290 if (!err) {
2291 ceph_msg_get(req->r_request);
2292 ceph_con_send(&session->s_con, req->r_request);
2295 mutex_unlock(&mdsc->mutex);
2299 * Encode information about a cap for a reconnect with the MDS.
2301 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2302 void *arg)
2304 union {
2305 struct ceph_mds_cap_reconnect v2;
2306 struct ceph_mds_cap_reconnect_v1 v1;
2307 } rec;
2308 size_t reclen;
2309 struct ceph_inode_info *ci;
2310 struct ceph_reconnect_state *recon_state = arg;
2311 struct ceph_pagelist *pagelist = recon_state->pagelist;
2312 char *path;
2313 int pathlen, err;
2314 u64 pathbase;
2315 struct dentry *dentry;
2317 ci = cap->ci;
2319 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2320 inode, ceph_vinop(inode), cap, cap->cap_id,
2321 ceph_cap_string(cap->issued));
2322 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2323 if (err)
2324 return err;
2326 dentry = d_find_alias(inode);
2327 if (dentry) {
2328 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2329 if (IS_ERR(path)) {
2330 err = PTR_ERR(path);
2331 goto out_dput;
2333 } else {
2334 path = NULL;
2335 pathlen = 0;
2337 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2338 if (err)
2339 goto out_free;
2341 spin_lock(&inode->i_lock);
2342 cap->seq = 0; /* reset cap seq */
2343 cap->issue_seq = 0; /* and issue_seq */
2345 if (recon_state->flock) {
2346 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2347 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2348 rec.v2.issued = cpu_to_le32(cap->issued);
2349 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2350 rec.v2.pathbase = cpu_to_le64(pathbase);
2351 rec.v2.flock_len = 0;
2352 reclen = sizeof(rec.v2);
2353 } else {
2354 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2355 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2356 rec.v1.issued = cpu_to_le32(cap->issued);
2357 rec.v1.size = cpu_to_le64(inode->i_size);
2358 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2359 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2360 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2361 rec.v1.pathbase = cpu_to_le64(pathbase);
2362 reclen = sizeof(rec.v1);
2364 spin_unlock(&inode->i_lock);
2366 if (recon_state->flock) {
2367 int num_fcntl_locks, num_flock_locks;
2368 struct ceph_pagelist_cursor trunc_point;
2370 ceph_pagelist_set_cursor(pagelist, &trunc_point);
2371 do {
2372 lock_flocks();
2373 ceph_count_locks(inode, &num_fcntl_locks,
2374 &num_flock_locks);
2375 rec.v2.flock_len = (2*sizeof(u32) +
2376 (num_fcntl_locks+num_flock_locks) *
2377 sizeof(struct ceph_filelock));
2378 unlock_flocks();
2380 /* pre-alloc pagelist */
2381 ceph_pagelist_truncate(pagelist, &trunc_point);
2382 err = ceph_pagelist_append(pagelist, &rec, reclen);
2383 if (!err)
2384 err = ceph_pagelist_reserve(pagelist,
2385 rec.v2.flock_len);
2387 /* encode locks */
2388 if (!err) {
2389 lock_flocks();
2390 err = ceph_encode_locks(inode,
2391 pagelist,
2392 num_fcntl_locks,
2393 num_flock_locks);
2394 unlock_flocks();
2396 } while (err == -ENOSPC);
2397 } else {
2398 err = ceph_pagelist_append(pagelist, &rec, reclen);
2401 out_free:
2402 kfree(path);
2403 out_dput:
2404 dput(dentry);
2405 return err;
2410 * If an MDS fails and recovers, clients need to reconnect in order to
2411 * reestablish shared state. This includes all caps issued through
2412 * this session _and_ the snap_realm hierarchy. Because it's not
2413 * clear which snap realms the mds cares about, we send everything we
2414 * know about.. that ensures we'll then get any new info the
2415 * recovering MDS might have.
2417 * This is a relatively heavyweight operation, but it's rare.
2419 * called with mdsc->mutex held.
2421 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2422 struct ceph_mds_session *session)
2424 struct ceph_msg *reply;
2425 struct rb_node *p;
2426 int mds = session->s_mds;
2427 int err = -ENOMEM;
2428 struct ceph_pagelist *pagelist;
2429 struct ceph_reconnect_state recon_state;
2431 pr_info("mds%d reconnect start\n", mds);
2433 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2434 if (!pagelist)
2435 goto fail_nopagelist;
2436 ceph_pagelist_init(pagelist);
2438 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS);
2439 if (!reply)
2440 goto fail_nomsg;
2442 mutex_lock(&session->s_mutex);
2443 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2444 session->s_seq = 0;
2446 ceph_con_open(&session->s_con,
2447 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2449 /* replay unsafe requests */
2450 replay_unsafe_requests(mdsc, session);
2452 down_read(&mdsc->snap_rwsem);
2454 dout("session %p state %s\n", session,
2455 session_state_name(session->s_state));
2457 /* drop old cap expires; we're about to reestablish that state */
2458 discard_cap_releases(mdsc, session);
2460 /* traverse this session's caps */
2461 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2462 if (err)
2463 goto fail;
2465 recon_state.pagelist = pagelist;
2466 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2467 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
2468 if (err < 0)
2469 goto fail;
2472 * snaprealms. we provide mds with the ino, seq (version), and
2473 * parent for all of our realms. If the mds has any newer info,
2474 * it will tell us.
2476 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2477 struct ceph_snap_realm *realm =
2478 rb_entry(p, struct ceph_snap_realm, node);
2479 struct ceph_mds_snaprealm_reconnect sr_rec;
2481 dout(" adding snap realm %llx seq %lld parent %llx\n",
2482 realm->ino, realm->seq, realm->parent_ino);
2483 sr_rec.ino = cpu_to_le64(realm->ino);
2484 sr_rec.seq = cpu_to_le64(realm->seq);
2485 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2486 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2487 if (err)
2488 goto fail;
2491 reply->pagelist = pagelist;
2492 if (recon_state.flock)
2493 reply->hdr.version = cpu_to_le16(2);
2494 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2495 reply->nr_pages = calc_pages_for(0, pagelist->length);
2496 ceph_con_send(&session->s_con, reply);
2498 mutex_unlock(&session->s_mutex);
2500 mutex_lock(&mdsc->mutex);
2501 __wake_requests(mdsc, &session->s_waiting);
2502 mutex_unlock(&mdsc->mutex);
2504 up_read(&mdsc->snap_rwsem);
2505 return;
2507 fail:
2508 ceph_msg_put(reply);
2509 up_read(&mdsc->snap_rwsem);
2510 mutex_unlock(&session->s_mutex);
2511 fail_nomsg:
2512 ceph_pagelist_release(pagelist);
2513 kfree(pagelist);
2514 fail_nopagelist:
2515 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
2516 return;
2521 * compare old and new mdsmaps, kicking requests
2522 * and closing out old connections as necessary
2524 * called under mdsc->mutex.
2526 static void check_new_map(struct ceph_mds_client *mdsc,
2527 struct ceph_mdsmap *newmap,
2528 struct ceph_mdsmap *oldmap)
2530 int i;
2531 int oldstate, newstate;
2532 struct ceph_mds_session *s;
2534 dout("check_new_map new %u old %u\n",
2535 newmap->m_epoch, oldmap->m_epoch);
2537 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2538 if (mdsc->sessions[i] == NULL)
2539 continue;
2540 s = mdsc->sessions[i];
2541 oldstate = ceph_mdsmap_get_state(oldmap, i);
2542 newstate = ceph_mdsmap_get_state(newmap, i);
2544 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2545 i, ceph_mds_state_name(oldstate),
2546 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
2547 ceph_mds_state_name(newstate),
2548 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
2549 session_state_name(s->s_state));
2551 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2552 ceph_mdsmap_get_addr(newmap, i),
2553 sizeof(struct ceph_entity_addr))) {
2554 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2555 /* the session never opened, just close it
2556 * out now */
2557 __wake_requests(mdsc, &s->s_waiting);
2558 __unregister_session(mdsc, s);
2559 } else {
2560 /* just close it */
2561 mutex_unlock(&mdsc->mutex);
2562 mutex_lock(&s->s_mutex);
2563 mutex_lock(&mdsc->mutex);
2564 ceph_con_close(&s->s_con);
2565 mutex_unlock(&s->s_mutex);
2566 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2569 /* kick any requests waiting on the recovering mds */
2570 kick_requests(mdsc, i);
2571 } else if (oldstate == newstate) {
2572 continue; /* nothing new with this mds */
2576 * send reconnect?
2578 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
2579 newstate >= CEPH_MDS_STATE_RECONNECT) {
2580 mutex_unlock(&mdsc->mutex);
2581 send_mds_reconnect(mdsc, s);
2582 mutex_lock(&mdsc->mutex);
2586 * kick request on any mds that has gone active.
2588 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2589 newstate >= CEPH_MDS_STATE_ACTIVE) {
2590 if (oldstate != CEPH_MDS_STATE_CREATING &&
2591 oldstate != CEPH_MDS_STATE_STARTING)
2592 pr_info("mds%d recovery completed\n", s->s_mds);
2593 kick_requests(mdsc, i);
2594 ceph_kick_flushing_caps(mdsc, s);
2595 wake_up_session_caps(s, 1);
2599 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2600 s = mdsc->sessions[i];
2601 if (!s)
2602 continue;
2603 if (!ceph_mdsmap_is_laggy(newmap, i))
2604 continue;
2605 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2606 s->s_state == CEPH_MDS_SESSION_HUNG ||
2607 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2608 dout(" connecting to export targets of laggy mds%d\n",
2610 __open_export_target_sessions(mdsc, s);
2618 * leases
2622 * caller must hold session s_mutex, dentry->d_lock
2624 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2626 struct ceph_dentry_info *di = ceph_dentry(dentry);
2628 ceph_put_mds_session(di->lease_session);
2629 di->lease_session = NULL;
2632 static void handle_lease(struct ceph_mds_client *mdsc,
2633 struct ceph_mds_session *session,
2634 struct ceph_msg *msg)
2636 struct super_block *sb = mdsc->fsc->sb;
2637 struct inode *inode;
2638 struct ceph_inode_info *ci;
2639 struct dentry *parent, *dentry;
2640 struct ceph_dentry_info *di;
2641 int mds = session->s_mds;
2642 struct ceph_mds_lease *h = msg->front.iov_base;
2643 u32 seq;
2644 struct ceph_vino vino;
2645 int mask;
2646 struct qstr dname;
2647 int release = 0;
2649 dout("handle_lease from mds%d\n", mds);
2651 /* decode */
2652 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2653 goto bad;
2654 vino.ino = le64_to_cpu(h->ino);
2655 vino.snap = CEPH_NOSNAP;
2656 mask = le16_to_cpu(h->mask);
2657 seq = le32_to_cpu(h->seq);
2658 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2659 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2660 if (dname.len != get_unaligned_le32(h+1))
2661 goto bad;
2663 mutex_lock(&session->s_mutex);
2664 session->s_seq++;
2666 /* lookup inode */
2667 inode = ceph_find_inode(sb, vino);
2668 dout("handle_lease %s, mask %d, ino %llx %p %.*s\n",
2669 ceph_lease_op_name(h->action), mask, vino.ino, inode,
2670 dname.len, dname.name);
2671 if (inode == NULL) {
2672 dout("handle_lease no inode %llx\n", vino.ino);
2673 goto release;
2675 ci = ceph_inode(inode);
2677 /* dentry */
2678 parent = d_find_alias(inode);
2679 if (!parent) {
2680 dout("no parent dentry on inode %p\n", inode);
2681 WARN_ON(1);
2682 goto release; /* hrm... */
2684 dname.hash = full_name_hash(dname.name, dname.len);
2685 dentry = d_lookup(parent, &dname);
2686 dput(parent);
2687 if (!dentry)
2688 goto release;
2690 spin_lock(&dentry->d_lock);
2691 di = ceph_dentry(dentry);
2692 switch (h->action) {
2693 case CEPH_MDS_LEASE_REVOKE:
2694 if (di && di->lease_session == session) {
2695 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2696 h->seq = cpu_to_le32(di->lease_seq);
2697 __ceph_mdsc_drop_dentry_lease(dentry);
2699 release = 1;
2700 break;
2702 case CEPH_MDS_LEASE_RENEW:
2703 if (di && di->lease_session == session &&
2704 di->lease_gen == session->s_cap_gen &&
2705 di->lease_renew_from &&
2706 di->lease_renew_after == 0) {
2707 unsigned long duration =
2708 le32_to_cpu(h->duration_ms) * HZ / 1000;
2710 di->lease_seq = seq;
2711 dentry->d_time = di->lease_renew_from + duration;
2712 di->lease_renew_after = di->lease_renew_from +
2713 (duration >> 1);
2714 di->lease_renew_from = 0;
2716 break;
2718 spin_unlock(&dentry->d_lock);
2719 dput(dentry);
2721 if (!release)
2722 goto out;
2724 release:
2725 /* let's just reuse the same message */
2726 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2727 ceph_msg_get(msg);
2728 ceph_con_send(&session->s_con, msg);
2730 out:
2731 iput(inode);
2732 mutex_unlock(&session->s_mutex);
2733 return;
2735 bad:
2736 pr_err("corrupt lease message\n");
2737 ceph_msg_dump(msg);
2740 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2741 struct inode *inode,
2742 struct dentry *dentry, char action,
2743 u32 seq)
2745 struct ceph_msg *msg;
2746 struct ceph_mds_lease *lease;
2747 int len = sizeof(*lease) + sizeof(u32);
2748 int dnamelen = 0;
2750 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2751 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2752 dnamelen = dentry->d_name.len;
2753 len += dnamelen;
2755 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS);
2756 if (!msg)
2757 return;
2758 lease = msg->front.iov_base;
2759 lease->action = action;
2760 lease->mask = cpu_to_le16(1);
2761 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2762 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2763 lease->seq = cpu_to_le32(seq);
2764 put_unaligned_le32(dnamelen, lease + 1);
2765 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2768 * if this is a preemptive lease RELEASE, no need to
2769 * flush request stream, since the actual request will
2770 * soon follow.
2772 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2774 ceph_con_send(&session->s_con, msg);
2778 * Preemptively release a lease we expect to invalidate anyway.
2779 * Pass @inode always, @dentry is optional.
2781 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2782 struct dentry *dentry, int mask)
2784 struct ceph_dentry_info *di;
2785 struct ceph_mds_session *session;
2786 u32 seq;
2788 BUG_ON(inode == NULL);
2789 BUG_ON(dentry == NULL);
2790 BUG_ON(mask == 0);
2792 /* is dentry lease valid? */
2793 spin_lock(&dentry->d_lock);
2794 di = ceph_dentry(dentry);
2795 if (!di || !di->lease_session ||
2796 di->lease_session->s_mds < 0 ||
2797 di->lease_gen != di->lease_session->s_cap_gen ||
2798 !time_before(jiffies, dentry->d_time)) {
2799 dout("lease_release inode %p dentry %p -- "
2800 "no lease on %d\n",
2801 inode, dentry, mask);
2802 spin_unlock(&dentry->d_lock);
2803 return;
2806 /* we do have a lease on this dentry; note mds and seq */
2807 session = ceph_get_mds_session(di->lease_session);
2808 seq = di->lease_seq;
2809 __ceph_mdsc_drop_dentry_lease(dentry);
2810 spin_unlock(&dentry->d_lock);
2812 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2813 inode, dentry, mask, session->s_mds);
2814 ceph_mdsc_lease_send_msg(session, inode, dentry,
2815 CEPH_MDS_LEASE_RELEASE, seq);
2816 ceph_put_mds_session(session);
2820 * drop all leases (and dentry refs) in preparation for umount
2822 static void drop_leases(struct ceph_mds_client *mdsc)
2824 int i;
2826 dout("drop_leases\n");
2827 mutex_lock(&mdsc->mutex);
2828 for (i = 0; i < mdsc->max_sessions; i++) {
2829 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2830 if (!s)
2831 continue;
2832 mutex_unlock(&mdsc->mutex);
2833 mutex_lock(&s->s_mutex);
2834 mutex_unlock(&s->s_mutex);
2835 ceph_put_mds_session(s);
2836 mutex_lock(&mdsc->mutex);
2838 mutex_unlock(&mdsc->mutex);
2844 * delayed work -- periodically trim expired leases, renew caps with mds
2846 static void schedule_delayed(struct ceph_mds_client *mdsc)
2848 int delay = 5;
2849 unsigned hz = round_jiffies_relative(HZ * delay);
2850 schedule_delayed_work(&mdsc->delayed_work, hz);
2853 static void delayed_work(struct work_struct *work)
2855 int i;
2856 struct ceph_mds_client *mdsc =
2857 container_of(work, struct ceph_mds_client, delayed_work.work);
2858 int renew_interval;
2859 int renew_caps;
2861 dout("mdsc delayed_work\n");
2862 ceph_check_delayed_caps(mdsc);
2864 mutex_lock(&mdsc->mutex);
2865 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2866 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2867 mdsc->last_renew_caps);
2868 if (renew_caps)
2869 mdsc->last_renew_caps = jiffies;
2871 for (i = 0; i < mdsc->max_sessions; i++) {
2872 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2873 if (s == NULL)
2874 continue;
2875 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2876 dout("resending session close request for mds%d\n",
2877 s->s_mds);
2878 request_close_session(mdsc, s);
2879 ceph_put_mds_session(s);
2880 continue;
2882 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2883 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2884 s->s_state = CEPH_MDS_SESSION_HUNG;
2885 pr_info("mds%d hung\n", s->s_mds);
2888 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2889 /* this mds is failed or recovering, just wait */
2890 ceph_put_mds_session(s);
2891 continue;
2893 mutex_unlock(&mdsc->mutex);
2895 mutex_lock(&s->s_mutex);
2896 if (renew_caps)
2897 send_renew_caps(mdsc, s);
2898 else
2899 ceph_con_keepalive(&s->s_con);
2900 ceph_add_cap_releases(mdsc, s);
2901 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2902 s->s_state == CEPH_MDS_SESSION_HUNG)
2903 ceph_send_cap_releases(mdsc, s);
2904 mutex_unlock(&s->s_mutex);
2905 ceph_put_mds_session(s);
2907 mutex_lock(&mdsc->mutex);
2909 mutex_unlock(&mdsc->mutex);
2911 schedule_delayed(mdsc);
2914 int ceph_mdsc_init(struct ceph_fs_client *fsc)
2917 struct ceph_mds_client *mdsc;
2919 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
2920 if (!mdsc)
2921 return -ENOMEM;
2922 mdsc->fsc = fsc;
2923 fsc->mdsc = mdsc;
2924 mutex_init(&mdsc->mutex);
2925 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
2926 if (mdsc->mdsmap == NULL)
2927 return -ENOMEM;
2929 init_completion(&mdsc->safe_umount_waiters);
2930 init_waitqueue_head(&mdsc->session_close_wq);
2931 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2932 mdsc->sessions = NULL;
2933 mdsc->max_sessions = 0;
2934 mdsc->stopping = 0;
2935 init_rwsem(&mdsc->snap_rwsem);
2936 mdsc->snap_realms = RB_ROOT;
2937 INIT_LIST_HEAD(&mdsc->snap_empty);
2938 spin_lock_init(&mdsc->snap_empty_lock);
2939 mdsc->last_tid = 0;
2940 mdsc->request_tree = RB_ROOT;
2941 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2942 mdsc->last_renew_caps = jiffies;
2943 INIT_LIST_HEAD(&mdsc->cap_delay_list);
2944 spin_lock_init(&mdsc->cap_delay_lock);
2945 INIT_LIST_HEAD(&mdsc->snap_flush_list);
2946 spin_lock_init(&mdsc->snap_flush_lock);
2947 mdsc->cap_flush_seq = 0;
2948 INIT_LIST_HEAD(&mdsc->cap_dirty);
2949 mdsc->num_cap_flushing = 0;
2950 spin_lock_init(&mdsc->cap_dirty_lock);
2951 init_waitqueue_head(&mdsc->cap_flushing_wq);
2952 spin_lock_init(&mdsc->dentry_lru_lock);
2953 INIT_LIST_HEAD(&mdsc->dentry_lru);
2955 ceph_caps_init(mdsc);
2956 ceph_adjust_min_caps(mdsc, fsc->min_caps);
2958 return 0;
2962 * Wait for safe replies on open mds requests. If we time out, drop
2963 * all requests from the tree to avoid dangling dentry refs.
2965 static void wait_requests(struct ceph_mds_client *mdsc)
2967 struct ceph_mds_request *req;
2968 struct ceph_fs_client *fsc = mdsc->fsc;
2970 mutex_lock(&mdsc->mutex);
2971 if (__get_oldest_req(mdsc)) {
2972 mutex_unlock(&mdsc->mutex);
2974 dout("wait_requests waiting for requests\n");
2975 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
2976 fsc->client->options->mount_timeout * HZ);
2978 /* tear down remaining requests */
2979 mutex_lock(&mdsc->mutex);
2980 while ((req = __get_oldest_req(mdsc))) {
2981 dout("wait_requests timed out on tid %llu\n",
2982 req->r_tid);
2983 __unregister_request(mdsc, req);
2986 mutex_unlock(&mdsc->mutex);
2987 dout("wait_requests done\n");
2991 * called before mount is ro, and before dentries are torn down.
2992 * (hmm, does this still race with new lookups?)
2994 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
2996 dout("pre_umount\n");
2997 mdsc->stopping = 1;
2999 drop_leases(mdsc);
3000 ceph_flush_dirty_caps(mdsc);
3001 wait_requests(mdsc);
3004 * wait for reply handlers to drop their request refs and
3005 * their inode/dcache refs
3007 ceph_msgr_flush();
3011 * wait for all write mds requests to flush.
3013 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3015 struct ceph_mds_request *req = NULL, *nextreq;
3016 struct rb_node *n;
3018 mutex_lock(&mdsc->mutex);
3019 dout("wait_unsafe_requests want %lld\n", want_tid);
3020 restart:
3021 req = __get_oldest_req(mdsc);
3022 while (req && req->r_tid <= want_tid) {
3023 /* find next request */
3024 n = rb_next(&req->r_node);
3025 if (n)
3026 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3027 else
3028 nextreq = NULL;
3029 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3030 /* write op */
3031 ceph_mdsc_get_request(req);
3032 if (nextreq)
3033 ceph_mdsc_get_request(nextreq);
3034 mutex_unlock(&mdsc->mutex);
3035 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3036 req->r_tid, want_tid);
3037 wait_for_completion(&req->r_safe_completion);
3038 mutex_lock(&mdsc->mutex);
3039 ceph_mdsc_put_request(req);
3040 if (!nextreq)
3041 break; /* next dne before, so we're done! */
3042 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3043 /* next request was removed from tree */
3044 ceph_mdsc_put_request(nextreq);
3045 goto restart;
3047 ceph_mdsc_put_request(nextreq); /* won't go away */
3049 req = nextreq;
3051 mutex_unlock(&mdsc->mutex);
3052 dout("wait_unsafe_requests done\n");
3055 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3057 u64 want_tid, want_flush;
3059 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3060 return;
3062 dout("sync\n");
3063 mutex_lock(&mdsc->mutex);
3064 want_tid = mdsc->last_tid;
3065 want_flush = mdsc->cap_flush_seq;
3066 mutex_unlock(&mdsc->mutex);
3067 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3069 ceph_flush_dirty_caps(mdsc);
3071 wait_unsafe_requests(mdsc, want_tid);
3072 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3076 * true if all sessions are closed, or we force unmount
3078 bool done_closing_sessions(struct ceph_mds_client *mdsc)
3080 int i, n = 0;
3082 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3083 return true;
3085 mutex_lock(&mdsc->mutex);
3086 for (i = 0; i < mdsc->max_sessions; i++)
3087 if (mdsc->sessions[i])
3088 n++;
3089 mutex_unlock(&mdsc->mutex);
3090 return n == 0;
3094 * called after sb is ro.
3096 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3098 struct ceph_mds_session *session;
3099 int i;
3100 struct ceph_fs_client *fsc = mdsc->fsc;
3101 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
3103 dout("close_sessions\n");
3105 /* close sessions */
3106 mutex_lock(&mdsc->mutex);
3107 for (i = 0; i < mdsc->max_sessions; i++) {
3108 session = __ceph_lookup_mds_session(mdsc, i);
3109 if (!session)
3110 continue;
3111 mutex_unlock(&mdsc->mutex);
3112 mutex_lock(&session->s_mutex);
3113 __close_session(mdsc, session);
3114 mutex_unlock(&session->s_mutex);
3115 ceph_put_mds_session(session);
3116 mutex_lock(&mdsc->mutex);
3118 mutex_unlock(&mdsc->mutex);
3120 dout("waiting for sessions to close\n");
3121 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3122 timeout);
3124 /* tear down remaining sessions */
3125 mutex_lock(&mdsc->mutex);
3126 for (i = 0; i < mdsc->max_sessions; i++) {
3127 if (mdsc->sessions[i]) {
3128 session = get_session(mdsc->sessions[i]);
3129 __unregister_session(mdsc, session);
3130 mutex_unlock(&mdsc->mutex);
3131 mutex_lock(&session->s_mutex);
3132 remove_session_caps(session);
3133 mutex_unlock(&session->s_mutex);
3134 ceph_put_mds_session(session);
3135 mutex_lock(&mdsc->mutex);
3138 WARN_ON(!list_empty(&mdsc->cap_delay_list));
3139 mutex_unlock(&mdsc->mutex);
3141 ceph_cleanup_empty_realms(mdsc);
3143 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3145 dout("stopped\n");
3148 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
3150 dout("stop\n");
3151 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3152 if (mdsc->mdsmap)
3153 ceph_mdsmap_destroy(mdsc->mdsmap);
3154 kfree(mdsc->sessions);
3155 ceph_caps_finalize(mdsc);
3158 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3160 struct ceph_mds_client *mdsc = fsc->mdsc;
3162 ceph_mdsc_stop(mdsc);
3163 fsc->mdsc = NULL;
3164 kfree(mdsc);
3169 * handle mds map update.
3171 void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3173 u32 epoch;
3174 u32 maplen;
3175 void *p = msg->front.iov_base;
3176 void *end = p + msg->front.iov_len;
3177 struct ceph_mdsmap *newmap, *oldmap;
3178 struct ceph_fsid fsid;
3179 int err = -EINVAL;
3181 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3182 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3183 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
3184 return;
3185 epoch = ceph_decode_32(&p);
3186 maplen = ceph_decode_32(&p);
3187 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3189 /* do we need it? */
3190 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
3191 mutex_lock(&mdsc->mutex);
3192 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3193 dout("handle_map epoch %u <= our %u\n",
3194 epoch, mdsc->mdsmap->m_epoch);
3195 mutex_unlock(&mdsc->mutex);
3196 return;
3199 newmap = ceph_mdsmap_decode(&p, end);
3200 if (IS_ERR(newmap)) {
3201 err = PTR_ERR(newmap);
3202 goto bad_unlock;
3205 /* swap into place */
3206 if (mdsc->mdsmap) {
3207 oldmap = mdsc->mdsmap;
3208 mdsc->mdsmap = newmap;
3209 check_new_map(mdsc, newmap, oldmap);
3210 ceph_mdsmap_destroy(oldmap);
3211 } else {
3212 mdsc->mdsmap = newmap; /* first mds map */
3214 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
3216 __wake_requests(mdsc, &mdsc->waiting_for_map);
3218 mutex_unlock(&mdsc->mutex);
3219 schedule_delayed(mdsc);
3220 return;
3222 bad_unlock:
3223 mutex_unlock(&mdsc->mutex);
3224 bad:
3225 pr_err("error decoding mdsmap %d\n", err);
3226 return;
3229 static struct ceph_connection *con_get(struct ceph_connection *con)
3231 struct ceph_mds_session *s = con->private;
3233 if (get_session(s)) {
3234 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
3235 return con;
3237 dout("mdsc con_get %p FAIL\n", s);
3238 return NULL;
3241 static void con_put(struct ceph_connection *con)
3243 struct ceph_mds_session *s = con->private;
3245 ceph_put_mds_session(s);
3246 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
3250 * if the client is unresponsive for long enough, the mds will kill
3251 * the session entirely.
3253 static void peer_reset(struct ceph_connection *con)
3255 struct ceph_mds_session *s = con->private;
3256 struct ceph_mds_client *mdsc = s->s_mdsc;
3258 pr_warning("mds%d closed our session\n", s->s_mds);
3259 send_mds_reconnect(mdsc, s);
3262 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3264 struct ceph_mds_session *s = con->private;
3265 struct ceph_mds_client *mdsc = s->s_mdsc;
3266 int type = le16_to_cpu(msg->hdr.type);
3268 mutex_lock(&mdsc->mutex);
3269 if (__verify_registered_session(mdsc, s) < 0) {
3270 mutex_unlock(&mdsc->mutex);
3271 goto out;
3273 mutex_unlock(&mdsc->mutex);
3275 switch (type) {
3276 case CEPH_MSG_MDS_MAP:
3277 ceph_mdsc_handle_map(mdsc, msg);
3278 break;
3279 case CEPH_MSG_CLIENT_SESSION:
3280 handle_session(s, msg);
3281 break;
3282 case CEPH_MSG_CLIENT_REPLY:
3283 handle_reply(s, msg);
3284 break;
3285 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
3286 handle_forward(mdsc, s, msg);
3287 break;
3288 case CEPH_MSG_CLIENT_CAPS:
3289 ceph_handle_caps(s, msg);
3290 break;
3291 case CEPH_MSG_CLIENT_SNAP:
3292 ceph_handle_snap(mdsc, s, msg);
3293 break;
3294 case CEPH_MSG_CLIENT_LEASE:
3295 handle_lease(mdsc, s, msg);
3296 break;
3298 default:
3299 pr_err("received unknown message type %d %s\n", type,
3300 ceph_msg_type_name(type));
3302 out:
3303 ceph_msg_put(msg);
3307 * authentication
3309 static int get_authorizer(struct ceph_connection *con,
3310 void **buf, int *len, int *proto,
3311 void **reply_buf, int *reply_len, int force_new)
3313 struct ceph_mds_session *s = con->private;
3314 struct ceph_mds_client *mdsc = s->s_mdsc;
3315 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3316 int ret = 0;
3318 if (force_new && s->s_authorizer) {
3319 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3320 s->s_authorizer = NULL;
3322 if (s->s_authorizer == NULL) {
3323 if (ac->ops->create_authorizer) {
3324 ret = ac->ops->create_authorizer(
3325 ac, CEPH_ENTITY_TYPE_MDS,
3326 &s->s_authorizer,
3327 &s->s_authorizer_buf,
3328 &s->s_authorizer_buf_len,
3329 &s->s_authorizer_reply_buf,
3330 &s->s_authorizer_reply_buf_len);
3331 if (ret)
3332 return ret;
3336 *proto = ac->protocol;
3337 *buf = s->s_authorizer_buf;
3338 *len = s->s_authorizer_buf_len;
3339 *reply_buf = s->s_authorizer_reply_buf;
3340 *reply_len = s->s_authorizer_reply_buf_len;
3341 return 0;
3345 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3347 struct ceph_mds_session *s = con->private;
3348 struct ceph_mds_client *mdsc = s->s_mdsc;
3349 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3351 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3354 static int invalidate_authorizer(struct ceph_connection *con)
3356 struct ceph_mds_session *s = con->private;
3357 struct ceph_mds_client *mdsc = s->s_mdsc;
3358 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3360 if (ac->ops->invalidate_authorizer)
3361 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3363 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
3366 static const struct ceph_connection_operations mds_con_ops = {
3367 .get = con_get,
3368 .put = con_put,
3369 .dispatch = dispatch,
3370 .get_authorizer = get_authorizer,
3371 .verify_authorizer_reply = verify_authorizer_reply,
3372 .invalidate_authorizer = invalidate_authorizer,
3373 .peer_reset = peer_reset,
3376 /* eof */