ceph: Storage class should be before const qualifier
[zen-stable.git] / fs / ceph / mds_client.c
blob885aa5710cfd541997fa9764dffba33b0e8ad644
1 #include "ceph_debug.h"
3 #include <linux/wait.h>
4 #include <linux/slab.h>
5 #include <linux/sched.h>
7 #include "mds_client.h"
8 #include "mon_client.h"
9 #include "super.h"
10 #include "messenger.h"
11 #include "decode.h"
12 #include "auth.h"
13 #include "pagelist.h"
16 * A cluster of MDS (metadata server) daemons is responsible for
17 * managing the file system namespace (the directory hierarchy and
18 * inodes) and for coordinating shared access to storage. Metadata is
19 * partitioning hierarchically across a number of servers, and that
20 * partition varies over time as the cluster adjusts the distribution
21 * in order to balance load.
23 * The MDS client is primarily responsible to managing synchronous
24 * metadata requests for operations like open, unlink, and so forth.
25 * If there is a MDS failure, we find out about it when we (possibly
26 * request and) receive a new MDS map, and can resubmit affected
27 * requests.
29 * For the most part, though, we take advantage of a lossless
30 * communications channel to the MDS, and do not need to worry about
31 * timing out or resubmitting requests.
33 * We maintain a stateful "session" with each MDS we interact with.
34 * Within each session, we sent periodic heartbeat messages to ensure
35 * any capabilities or leases we have been issues remain valid. If
36 * the session times out and goes stale, our leases and capabilities
37 * are no longer valid.
40 static void __wake_requests(struct ceph_mds_client *mdsc,
41 struct list_head *head);
43 static const struct ceph_connection_operations mds_con_ops;
47 * mds reply parsing
51 * parse individual inode info
53 static int parse_reply_info_in(void **p, void *end,
54 struct ceph_mds_reply_info_in *info)
56 int err = -EIO;
58 info->in = *p;
59 *p += sizeof(struct ceph_mds_reply_inode) +
60 sizeof(*info->in->fragtree.splits) *
61 le32_to_cpu(info->in->fragtree.nsplits);
63 ceph_decode_32_safe(p, end, info->symlink_len, bad);
64 ceph_decode_need(p, end, info->symlink_len, bad);
65 info->symlink = *p;
66 *p += info->symlink_len;
68 ceph_decode_32_safe(p, end, info->xattr_len, bad);
69 ceph_decode_need(p, end, info->xattr_len, bad);
70 info->xattr_data = *p;
71 *p += info->xattr_len;
72 return 0;
73 bad:
74 return err;
78 * parse a normal reply, which may contain a (dir+)dentry and/or a
79 * target inode.
81 static int parse_reply_info_trace(void **p, void *end,
82 struct ceph_mds_reply_info_parsed *info)
84 int err;
86 if (info->head->is_dentry) {
87 err = parse_reply_info_in(p, end, &info->diri);
88 if (err < 0)
89 goto out_bad;
91 if (unlikely(*p + sizeof(*info->dirfrag) > end))
92 goto bad;
93 info->dirfrag = *p;
94 *p += sizeof(*info->dirfrag) +
95 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
96 if (unlikely(*p > end))
97 goto bad;
99 ceph_decode_32_safe(p, end, info->dname_len, bad);
100 ceph_decode_need(p, end, info->dname_len, bad);
101 info->dname = *p;
102 *p += info->dname_len;
103 info->dlease = *p;
104 *p += sizeof(*info->dlease);
107 if (info->head->is_target) {
108 err = parse_reply_info_in(p, end, &info->targeti);
109 if (err < 0)
110 goto out_bad;
113 if (unlikely(*p != end))
114 goto bad;
115 return 0;
117 bad:
118 err = -EIO;
119 out_bad:
120 pr_err("problem parsing mds trace %d\n", err);
121 return err;
125 * parse readdir results
127 static int parse_reply_info_dir(void **p, void *end,
128 struct ceph_mds_reply_info_parsed *info)
130 u32 num, i = 0;
131 int err;
133 info->dir_dir = *p;
134 if (*p + sizeof(*info->dir_dir) > end)
135 goto bad;
136 *p += sizeof(*info->dir_dir) +
137 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
138 if (*p > end)
139 goto bad;
141 ceph_decode_need(p, end, sizeof(num) + 2, bad);
142 num = ceph_decode_32(p);
143 info->dir_end = ceph_decode_8(p);
144 info->dir_complete = ceph_decode_8(p);
145 if (num == 0)
146 goto done;
148 /* alloc large array */
149 info->dir_nr = num;
150 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
151 sizeof(*info->dir_dname) +
152 sizeof(*info->dir_dname_len) +
153 sizeof(*info->dir_dlease),
154 GFP_NOFS);
155 if (info->dir_in == NULL) {
156 err = -ENOMEM;
157 goto out_bad;
159 info->dir_dname = (void *)(info->dir_in + num);
160 info->dir_dname_len = (void *)(info->dir_dname + num);
161 info->dir_dlease = (void *)(info->dir_dname_len + num);
163 while (num) {
164 /* dentry */
165 ceph_decode_need(p, end, sizeof(u32)*2, bad);
166 info->dir_dname_len[i] = ceph_decode_32(p);
167 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
168 info->dir_dname[i] = *p;
169 *p += info->dir_dname_len[i];
170 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
171 info->dir_dname[i]);
172 info->dir_dlease[i] = *p;
173 *p += sizeof(struct ceph_mds_reply_lease);
175 /* inode */
176 err = parse_reply_info_in(p, end, &info->dir_in[i]);
177 if (err < 0)
178 goto out_bad;
179 i++;
180 num--;
183 done:
184 if (*p != end)
185 goto bad;
186 return 0;
188 bad:
189 err = -EIO;
190 out_bad:
191 pr_err("problem parsing dir contents %d\n", err);
192 return err;
196 * parse entire mds reply
198 static int parse_reply_info(struct ceph_msg *msg,
199 struct ceph_mds_reply_info_parsed *info)
201 void *p, *end;
202 u32 len;
203 int err;
205 info->head = msg->front.iov_base;
206 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
207 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
209 /* trace */
210 ceph_decode_32_safe(&p, end, len, bad);
211 if (len > 0) {
212 err = parse_reply_info_trace(&p, p+len, info);
213 if (err < 0)
214 goto out_bad;
217 /* dir content */
218 ceph_decode_32_safe(&p, end, len, bad);
219 if (len > 0) {
220 err = parse_reply_info_dir(&p, p+len, info);
221 if (err < 0)
222 goto out_bad;
225 /* snap blob */
226 ceph_decode_32_safe(&p, end, len, bad);
227 info->snapblob_len = len;
228 info->snapblob = p;
229 p += len;
231 if (p != end)
232 goto bad;
233 return 0;
235 bad:
236 err = -EIO;
237 out_bad:
238 pr_err("mds parse_reply err %d\n", err);
239 return err;
242 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
244 kfree(info->dir_in);
249 * sessions
251 static const char *session_state_name(int s)
253 switch (s) {
254 case CEPH_MDS_SESSION_NEW: return "new";
255 case CEPH_MDS_SESSION_OPENING: return "opening";
256 case CEPH_MDS_SESSION_OPEN: return "open";
257 case CEPH_MDS_SESSION_HUNG: return "hung";
258 case CEPH_MDS_SESSION_CLOSING: return "closing";
259 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
260 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
261 default: return "???";
265 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
267 if (atomic_inc_not_zero(&s->s_ref)) {
268 dout("mdsc get_session %p %d -> %d\n", s,
269 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
270 return s;
271 } else {
272 dout("mdsc get_session %p 0 -- FAIL", s);
273 return NULL;
277 void ceph_put_mds_session(struct ceph_mds_session *s)
279 dout("mdsc put_session %p %d -> %d\n", s,
280 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
281 if (atomic_dec_and_test(&s->s_ref)) {
282 if (s->s_authorizer)
283 s->s_mdsc->client->monc.auth->ops->destroy_authorizer(
284 s->s_mdsc->client->monc.auth, s->s_authorizer);
285 kfree(s);
290 * called under mdsc->mutex
292 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
293 int mds)
295 struct ceph_mds_session *session;
297 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
298 return NULL;
299 session = mdsc->sessions[mds];
300 dout("lookup_mds_session %p %d\n", session,
301 atomic_read(&session->s_ref));
302 get_session(session);
303 return session;
306 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
308 if (mds >= mdsc->max_sessions)
309 return false;
310 return mdsc->sessions[mds];
313 static int __verify_registered_session(struct ceph_mds_client *mdsc,
314 struct ceph_mds_session *s)
316 if (s->s_mds >= mdsc->max_sessions ||
317 mdsc->sessions[s->s_mds] != s)
318 return -ENOENT;
319 return 0;
323 * create+register a new session for given mds.
324 * called under mdsc->mutex.
326 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
327 int mds)
329 struct ceph_mds_session *s;
331 s = kzalloc(sizeof(*s), GFP_NOFS);
332 if (!s)
333 return ERR_PTR(-ENOMEM);
334 s->s_mdsc = mdsc;
335 s->s_mds = mds;
336 s->s_state = CEPH_MDS_SESSION_NEW;
337 s->s_ttl = 0;
338 s->s_seq = 0;
339 mutex_init(&s->s_mutex);
341 ceph_con_init(mdsc->client->msgr, &s->s_con);
342 s->s_con.private = s;
343 s->s_con.ops = &mds_con_ops;
344 s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
345 s->s_con.peer_name.num = cpu_to_le64(mds);
347 spin_lock_init(&s->s_cap_lock);
348 s->s_cap_gen = 0;
349 s->s_cap_ttl = 0;
350 s->s_renew_requested = 0;
351 s->s_renew_seq = 0;
352 INIT_LIST_HEAD(&s->s_caps);
353 s->s_nr_caps = 0;
354 s->s_trim_caps = 0;
355 atomic_set(&s->s_ref, 1);
356 INIT_LIST_HEAD(&s->s_waiting);
357 INIT_LIST_HEAD(&s->s_unsafe);
358 s->s_num_cap_releases = 0;
359 s->s_cap_iterator = NULL;
360 INIT_LIST_HEAD(&s->s_cap_releases);
361 INIT_LIST_HEAD(&s->s_cap_releases_done);
362 INIT_LIST_HEAD(&s->s_cap_flushing);
363 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
365 dout("register_session mds%d\n", mds);
366 if (mds >= mdsc->max_sessions) {
367 int newmax = 1 << get_count_order(mds+1);
368 struct ceph_mds_session **sa;
370 dout("register_session realloc to %d\n", newmax);
371 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
372 if (sa == NULL)
373 goto fail_realloc;
374 if (mdsc->sessions) {
375 memcpy(sa, mdsc->sessions,
376 mdsc->max_sessions * sizeof(void *));
377 kfree(mdsc->sessions);
379 mdsc->sessions = sa;
380 mdsc->max_sessions = newmax;
382 mdsc->sessions[mds] = s;
383 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
385 ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
387 return s;
389 fail_realloc:
390 kfree(s);
391 return ERR_PTR(-ENOMEM);
395 * called under mdsc->mutex
397 static void __unregister_session(struct ceph_mds_client *mdsc,
398 struct ceph_mds_session *s)
400 dout("__unregister_session mds%d %p\n", s->s_mds, s);
401 BUG_ON(mdsc->sessions[s->s_mds] != s);
402 mdsc->sessions[s->s_mds] = NULL;
403 ceph_con_close(&s->s_con);
404 ceph_put_mds_session(s);
408 * drop session refs in request.
410 * should be last request ref, or hold mdsc->mutex
412 static void put_request_session(struct ceph_mds_request *req)
414 if (req->r_session) {
415 ceph_put_mds_session(req->r_session);
416 req->r_session = NULL;
420 void ceph_mdsc_release_request(struct kref *kref)
422 struct ceph_mds_request *req = container_of(kref,
423 struct ceph_mds_request,
424 r_kref);
425 if (req->r_request)
426 ceph_msg_put(req->r_request);
427 if (req->r_reply) {
428 ceph_msg_put(req->r_reply);
429 destroy_reply_info(&req->r_reply_info);
431 if (req->r_inode) {
432 ceph_put_cap_refs(ceph_inode(req->r_inode),
433 CEPH_CAP_PIN);
434 iput(req->r_inode);
436 if (req->r_locked_dir)
437 ceph_put_cap_refs(ceph_inode(req->r_locked_dir),
438 CEPH_CAP_PIN);
439 if (req->r_target_inode)
440 iput(req->r_target_inode);
441 if (req->r_dentry)
442 dput(req->r_dentry);
443 if (req->r_old_dentry) {
444 ceph_put_cap_refs(
445 ceph_inode(req->r_old_dentry->d_parent->d_inode),
446 CEPH_CAP_PIN);
447 dput(req->r_old_dentry);
449 kfree(req->r_path1);
450 kfree(req->r_path2);
451 put_request_session(req);
452 ceph_unreserve_caps(&req->r_caps_reservation);
453 kfree(req);
457 * lookup session, bump ref if found.
459 * called under mdsc->mutex.
461 static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
462 u64 tid)
464 struct ceph_mds_request *req;
465 struct rb_node *n = mdsc->request_tree.rb_node;
467 while (n) {
468 req = rb_entry(n, struct ceph_mds_request, r_node);
469 if (tid < req->r_tid)
470 n = n->rb_left;
471 else if (tid > req->r_tid)
472 n = n->rb_right;
473 else {
474 ceph_mdsc_get_request(req);
475 return req;
478 return NULL;
481 static void __insert_request(struct ceph_mds_client *mdsc,
482 struct ceph_mds_request *new)
484 struct rb_node **p = &mdsc->request_tree.rb_node;
485 struct rb_node *parent = NULL;
486 struct ceph_mds_request *req = NULL;
488 while (*p) {
489 parent = *p;
490 req = rb_entry(parent, struct ceph_mds_request, r_node);
491 if (new->r_tid < req->r_tid)
492 p = &(*p)->rb_left;
493 else if (new->r_tid > req->r_tid)
494 p = &(*p)->rb_right;
495 else
496 BUG();
499 rb_link_node(&new->r_node, parent, p);
500 rb_insert_color(&new->r_node, &mdsc->request_tree);
504 * Register an in-flight request, and assign a tid. Link to directory
505 * are modifying (if any).
507 * Called under mdsc->mutex.
509 static void __register_request(struct ceph_mds_client *mdsc,
510 struct ceph_mds_request *req,
511 struct inode *dir)
513 req->r_tid = ++mdsc->last_tid;
514 if (req->r_num_caps)
515 ceph_reserve_caps(&req->r_caps_reservation, req->r_num_caps);
516 dout("__register_request %p tid %lld\n", req, req->r_tid);
517 ceph_mdsc_get_request(req);
518 __insert_request(mdsc, req);
520 if (dir) {
521 struct ceph_inode_info *ci = ceph_inode(dir);
523 spin_lock(&ci->i_unsafe_lock);
524 req->r_unsafe_dir = dir;
525 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
526 spin_unlock(&ci->i_unsafe_lock);
530 static void __unregister_request(struct ceph_mds_client *mdsc,
531 struct ceph_mds_request *req)
533 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
534 rb_erase(&req->r_node, &mdsc->request_tree);
535 RB_CLEAR_NODE(&req->r_node);
537 if (req->r_unsafe_dir) {
538 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
540 spin_lock(&ci->i_unsafe_lock);
541 list_del_init(&req->r_unsafe_dir_item);
542 spin_unlock(&ci->i_unsafe_lock);
545 ceph_mdsc_put_request(req);
549 * Choose mds to send request to next. If there is a hint set in the
550 * request (e.g., due to a prior forward hint from the mds), use that.
551 * Otherwise, consult frag tree and/or caps to identify the
552 * appropriate mds. If all else fails, choose randomly.
554 * Called under mdsc->mutex.
556 static int __choose_mds(struct ceph_mds_client *mdsc,
557 struct ceph_mds_request *req)
559 struct inode *inode;
560 struct ceph_inode_info *ci;
561 struct ceph_cap *cap;
562 int mode = req->r_direct_mode;
563 int mds = -1;
564 u32 hash = req->r_direct_hash;
565 bool is_hash = req->r_direct_is_hash;
568 * is there a specific mds we should try? ignore hint if we have
569 * no session and the mds is not up (active or recovering).
571 if (req->r_resend_mds >= 0 &&
572 (__have_session(mdsc, req->r_resend_mds) ||
573 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
574 dout("choose_mds using resend_mds mds%d\n",
575 req->r_resend_mds);
576 return req->r_resend_mds;
579 if (mode == USE_RANDOM_MDS)
580 goto random;
582 inode = NULL;
583 if (req->r_inode) {
584 inode = req->r_inode;
585 } else if (req->r_dentry) {
586 if (req->r_dentry->d_inode) {
587 inode = req->r_dentry->d_inode;
588 } else {
589 inode = req->r_dentry->d_parent->d_inode;
590 hash = req->r_dentry->d_name.hash;
591 is_hash = true;
594 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
595 (int)hash, mode);
596 if (!inode)
597 goto random;
598 ci = ceph_inode(inode);
600 if (is_hash && S_ISDIR(inode->i_mode)) {
601 struct ceph_inode_frag frag;
602 int found;
604 ceph_choose_frag(ci, hash, &frag, &found);
605 if (found) {
606 if (mode == USE_ANY_MDS && frag.ndist > 0) {
607 u8 r;
609 /* choose a random replica */
610 get_random_bytes(&r, 1);
611 r %= frag.ndist;
612 mds = frag.dist[r];
613 dout("choose_mds %p %llx.%llx "
614 "frag %u mds%d (%d/%d)\n",
615 inode, ceph_vinop(inode),
616 frag.frag, frag.mds,
617 (int)r, frag.ndist);
618 return mds;
621 /* since this file/dir wasn't known to be
622 * replicated, then we want to look for the
623 * authoritative mds. */
624 mode = USE_AUTH_MDS;
625 if (frag.mds >= 0) {
626 /* choose auth mds */
627 mds = frag.mds;
628 dout("choose_mds %p %llx.%llx "
629 "frag %u mds%d (auth)\n",
630 inode, ceph_vinop(inode), frag.frag, mds);
631 return mds;
636 spin_lock(&inode->i_lock);
637 cap = NULL;
638 if (mode == USE_AUTH_MDS)
639 cap = ci->i_auth_cap;
640 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
641 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
642 if (!cap) {
643 spin_unlock(&inode->i_lock);
644 goto random;
646 mds = cap->session->s_mds;
647 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
648 inode, ceph_vinop(inode), mds,
649 cap == ci->i_auth_cap ? "auth " : "", cap);
650 spin_unlock(&inode->i_lock);
651 return mds;
653 random:
654 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
655 dout("choose_mds chose random mds%d\n", mds);
656 return mds;
661 * session messages
663 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
665 struct ceph_msg *msg;
666 struct ceph_mds_session_head *h;
668 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS);
669 if (!msg) {
670 pr_err("create_session_msg ENOMEM creating msg\n");
671 return NULL;
673 h = msg->front.iov_base;
674 h->op = cpu_to_le32(op);
675 h->seq = cpu_to_le64(seq);
676 return msg;
680 * send session open request.
682 * called under mdsc->mutex
684 static int __open_session(struct ceph_mds_client *mdsc,
685 struct ceph_mds_session *session)
687 struct ceph_msg *msg;
688 int mstate;
689 int mds = session->s_mds;
691 /* wait for mds to go active? */
692 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
693 dout("open_session to mds%d (%s)\n", mds,
694 ceph_mds_state_name(mstate));
695 session->s_state = CEPH_MDS_SESSION_OPENING;
696 session->s_renew_requested = jiffies;
698 /* send connect message */
699 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
700 if (!msg)
701 return -ENOMEM;
702 ceph_con_send(&session->s_con, msg);
703 return 0;
707 * session caps
711 * Free preallocated cap messages assigned to this session
713 static void cleanup_cap_releases(struct ceph_mds_session *session)
715 struct ceph_msg *msg;
717 spin_lock(&session->s_cap_lock);
718 while (!list_empty(&session->s_cap_releases)) {
719 msg = list_first_entry(&session->s_cap_releases,
720 struct ceph_msg, list_head);
721 list_del_init(&msg->list_head);
722 ceph_msg_put(msg);
724 while (!list_empty(&session->s_cap_releases_done)) {
725 msg = list_first_entry(&session->s_cap_releases_done,
726 struct ceph_msg, list_head);
727 list_del_init(&msg->list_head);
728 ceph_msg_put(msg);
730 spin_unlock(&session->s_cap_lock);
734 * Helper to safely iterate over all caps associated with a session, with
735 * special care taken to handle a racing __ceph_remove_cap().
737 * Caller must hold session s_mutex.
739 static int iterate_session_caps(struct ceph_mds_session *session,
740 int (*cb)(struct inode *, struct ceph_cap *,
741 void *), void *arg)
743 struct list_head *p;
744 struct ceph_cap *cap;
745 struct inode *inode, *last_inode = NULL;
746 struct ceph_cap *old_cap = NULL;
747 int ret;
749 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
750 spin_lock(&session->s_cap_lock);
751 p = session->s_caps.next;
752 while (p != &session->s_caps) {
753 cap = list_entry(p, struct ceph_cap, session_caps);
754 inode = igrab(&cap->ci->vfs_inode);
755 if (!inode) {
756 p = p->next;
757 continue;
759 session->s_cap_iterator = cap;
760 spin_unlock(&session->s_cap_lock);
762 if (last_inode) {
763 iput(last_inode);
764 last_inode = NULL;
766 if (old_cap) {
767 ceph_put_cap(old_cap);
768 old_cap = NULL;
771 ret = cb(inode, cap, arg);
772 last_inode = inode;
774 spin_lock(&session->s_cap_lock);
775 p = p->next;
776 if (cap->ci == NULL) {
777 dout("iterate_session_caps finishing cap %p removal\n",
778 cap);
779 BUG_ON(cap->session != session);
780 list_del_init(&cap->session_caps);
781 session->s_nr_caps--;
782 cap->session = NULL;
783 old_cap = cap; /* put_cap it w/o locks held */
785 if (ret < 0)
786 goto out;
788 ret = 0;
789 out:
790 session->s_cap_iterator = NULL;
791 spin_unlock(&session->s_cap_lock);
793 if (last_inode)
794 iput(last_inode);
795 if (old_cap)
796 ceph_put_cap(old_cap);
798 return ret;
801 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
802 void *arg)
804 struct ceph_inode_info *ci = ceph_inode(inode);
805 int drop = 0;
807 dout("removing cap %p, ci is %p, inode is %p\n",
808 cap, ci, &ci->vfs_inode);
809 spin_lock(&inode->i_lock);
810 __ceph_remove_cap(cap);
811 if (!__ceph_is_any_real_caps(ci)) {
812 struct ceph_mds_client *mdsc =
813 &ceph_sb_to_client(inode->i_sb)->mdsc;
815 spin_lock(&mdsc->cap_dirty_lock);
816 if (!list_empty(&ci->i_dirty_item)) {
817 pr_info(" dropping dirty %s state for %p %lld\n",
818 ceph_cap_string(ci->i_dirty_caps),
819 inode, ceph_ino(inode));
820 ci->i_dirty_caps = 0;
821 list_del_init(&ci->i_dirty_item);
822 drop = 1;
824 if (!list_empty(&ci->i_flushing_item)) {
825 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
826 ceph_cap_string(ci->i_flushing_caps),
827 inode, ceph_ino(inode));
828 ci->i_flushing_caps = 0;
829 list_del_init(&ci->i_flushing_item);
830 mdsc->num_cap_flushing--;
831 drop = 1;
833 if (drop && ci->i_wrbuffer_ref) {
834 pr_info(" dropping dirty data for %p %lld\n",
835 inode, ceph_ino(inode));
836 ci->i_wrbuffer_ref = 0;
837 ci->i_wrbuffer_ref_head = 0;
838 drop++;
840 spin_unlock(&mdsc->cap_dirty_lock);
842 spin_unlock(&inode->i_lock);
843 while (drop--)
844 iput(inode);
845 return 0;
849 * caller must hold session s_mutex
851 static void remove_session_caps(struct ceph_mds_session *session)
853 dout("remove_session_caps on %p\n", session);
854 iterate_session_caps(session, remove_session_caps_cb, NULL);
855 BUG_ON(session->s_nr_caps > 0);
856 BUG_ON(!list_empty(&session->s_cap_flushing));
857 cleanup_cap_releases(session);
861 * wake up any threads waiting on this session's caps. if the cap is
862 * old (didn't get renewed on the client reconnect), remove it now.
864 * caller must hold s_mutex.
866 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
867 void *arg)
869 struct ceph_inode_info *ci = ceph_inode(inode);
871 wake_up(&ci->i_cap_wq);
872 if (arg) {
873 spin_lock(&inode->i_lock);
874 ci->i_wanted_max_size = 0;
875 ci->i_requested_max_size = 0;
876 spin_unlock(&inode->i_lock);
878 return 0;
881 static void wake_up_session_caps(struct ceph_mds_session *session,
882 int reconnect)
884 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
885 iterate_session_caps(session, wake_up_session_cb,
886 (void *)(unsigned long)reconnect);
890 * Send periodic message to MDS renewing all currently held caps. The
891 * ack will reset the expiration for all caps from this session.
893 * caller holds s_mutex
895 static int send_renew_caps(struct ceph_mds_client *mdsc,
896 struct ceph_mds_session *session)
898 struct ceph_msg *msg;
899 int state;
901 if (time_after_eq(jiffies, session->s_cap_ttl) &&
902 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
903 pr_info("mds%d caps stale\n", session->s_mds);
904 session->s_renew_requested = jiffies;
906 /* do not try to renew caps until a recovering mds has reconnected
907 * with its clients. */
908 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
909 if (state < CEPH_MDS_STATE_RECONNECT) {
910 dout("send_renew_caps ignoring mds%d (%s)\n",
911 session->s_mds, ceph_mds_state_name(state));
912 return 0;
915 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
916 ceph_mds_state_name(state));
917 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
918 ++session->s_renew_seq);
919 if (!msg)
920 return -ENOMEM;
921 ceph_con_send(&session->s_con, msg);
922 return 0;
926 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
928 * Called under session->s_mutex
930 static void renewed_caps(struct ceph_mds_client *mdsc,
931 struct ceph_mds_session *session, int is_renew)
933 int was_stale;
934 int wake = 0;
936 spin_lock(&session->s_cap_lock);
937 was_stale = is_renew && (session->s_cap_ttl == 0 ||
938 time_after_eq(jiffies, session->s_cap_ttl));
940 session->s_cap_ttl = session->s_renew_requested +
941 mdsc->mdsmap->m_session_timeout*HZ;
943 if (was_stale) {
944 if (time_before(jiffies, session->s_cap_ttl)) {
945 pr_info("mds%d caps renewed\n", session->s_mds);
946 wake = 1;
947 } else {
948 pr_info("mds%d caps still stale\n", session->s_mds);
951 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
952 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
953 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
954 spin_unlock(&session->s_cap_lock);
956 if (wake)
957 wake_up_session_caps(session, 0);
961 * send a session close request
963 static int request_close_session(struct ceph_mds_client *mdsc,
964 struct ceph_mds_session *session)
966 struct ceph_msg *msg;
968 dout("request_close_session mds%d state %s seq %lld\n",
969 session->s_mds, session_state_name(session->s_state),
970 session->s_seq);
971 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
972 if (!msg)
973 return -ENOMEM;
974 ceph_con_send(&session->s_con, msg);
975 return 0;
979 * Called with s_mutex held.
981 static int __close_session(struct ceph_mds_client *mdsc,
982 struct ceph_mds_session *session)
984 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
985 return 0;
986 session->s_state = CEPH_MDS_SESSION_CLOSING;
987 return request_close_session(mdsc, session);
991 * Trim old(er) caps.
993 * Because we can't cache an inode without one or more caps, we do
994 * this indirectly: if a cap is unused, we prune its aliases, at which
995 * point the inode will hopefully get dropped to.
997 * Yes, this is a bit sloppy. Our only real goal here is to respond to
998 * memory pressure from the MDS, though, so it needn't be perfect.
1000 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1002 struct ceph_mds_session *session = arg;
1003 struct ceph_inode_info *ci = ceph_inode(inode);
1004 int used, oissued, mine;
1006 if (session->s_trim_caps <= 0)
1007 return -1;
1009 spin_lock(&inode->i_lock);
1010 mine = cap->issued | cap->implemented;
1011 used = __ceph_caps_used(ci);
1012 oissued = __ceph_caps_issued_other(ci, cap);
1014 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1015 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1016 ceph_cap_string(used));
1017 if (ci->i_dirty_caps)
1018 goto out; /* dirty caps */
1019 if ((used & ~oissued) & mine)
1020 goto out; /* we need these caps */
1022 session->s_trim_caps--;
1023 if (oissued) {
1024 /* we aren't the only cap.. just remove us */
1025 __ceph_remove_cap(cap);
1026 } else {
1027 /* try to drop referring dentries */
1028 spin_unlock(&inode->i_lock);
1029 d_prune_aliases(inode);
1030 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1031 inode, cap, atomic_read(&inode->i_count));
1032 return 0;
1035 out:
1036 spin_unlock(&inode->i_lock);
1037 return 0;
1041 * Trim session cap count down to some max number.
1043 static int trim_caps(struct ceph_mds_client *mdsc,
1044 struct ceph_mds_session *session,
1045 int max_caps)
1047 int trim_caps = session->s_nr_caps - max_caps;
1049 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1050 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1051 if (trim_caps > 0) {
1052 session->s_trim_caps = trim_caps;
1053 iterate_session_caps(session, trim_caps_cb, session);
1054 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1055 session->s_mds, session->s_nr_caps, max_caps,
1056 trim_caps - session->s_trim_caps);
1057 session->s_trim_caps = 0;
1059 return 0;
1063 * Allocate cap_release messages. If there is a partially full message
1064 * in the queue, try to allocate enough to cover it's remainder, so that
1065 * we can send it immediately.
1067 * Called under s_mutex.
1069 static int add_cap_releases(struct ceph_mds_client *mdsc,
1070 struct ceph_mds_session *session,
1071 int extra)
1073 struct ceph_msg *msg;
1074 struct ceph_mds_cap_release *head;
1075 int err = -ENOMEM;
1077 if (extra < 0)
1078 extra = mdsc->client->mount_args->cap_release_safety;
1080 spin_lock(&session->s_cap_lock);
1082 if (!list_empty(&session->s_cap_releases)) {
1083 msg = list_first_entry(&session->s_cap_releases,
1084 struct ceph_msg,
1085 list_head);
1086 head = msg->front.iov_base;
1087 extra += CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num);
1090 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1091 spin_unlock(&session->s_cap_lock);
1092 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1093 GFP_NOFS);
1094 if (!msg)
1095 goto out_unlocked;
1096 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1097 (int)msg->front.iov_len);
1098 head = msg->front.iov_base;
1099 head->num = cpu_to_le32(0);
1100 msg->front.iov_len = sizeof(*head);
1101 spin_lock(&session->s_cap_lock);
1102 list_add(&msg->list_head, &session->s_cap_releases);
1103 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1106 if (!list_empty(&session->s_cap_releases)) {
1107 msg = list_first_entry(&session->s_cap_releases,
1108 struct ceph_msg,
1109 list_head);
1110 head = msg->front.iov_base;
1111 if (head->num) {
1112 dout(" queueing non-full %p (%d)\n", msg,
1113 le32_to_cpu(head->num));
1114 list_move_tail(&msg->list_head,
1115 &session->s_cap_releases_done);
1116 session->s_num_cap_releases -=
1117 CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num);
1120 err = 0;
1121 spin_unlock(&session->s_cap_lock);
1122 out_unlocked:
1123 return err;
1127 * flush all dirty inode data to disk.
1129 * returns true if we've flushed through want_flush_seq
1131 static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1133 int mds, ret = 1;
1135 dout("check_cap_flush want %lld\n", want_flush_seq);
1136 mutex_lock(&mdsc->mutex);
1137 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1138 struct ceph_mds_session *session = mdsc->sessions[mds];
1140 if (!session)
1141 continue;
1142 get_session(session);
1143 mutex_unlock(&mdsc->mutex);
1145 mutex_lock(&session->s_mutex);
1146 if (!list_empty(&session->s_cap_flushing)) {
1147 struct ceph_inode_info *ci =
1148 list_entry(session->s_cap_flushing.next,
1149 struct ceph_inode_info,
1150 i_flushing_item);
1151 struct inode *inode = &ci->vfs_inode;
1153 spin_lock(&inode->i_lock);
1154 if (ci->i_cap_flush_seq <= want_flush_seq) {
1155 dout("check_cap_flush still flushing %p "
1156 "seq %lld <= %lld to mds%d\n", inode,
1157 ci->i_cap_flush_seq, want_flush_seq,
1158 session->s_mds);
1159 ret = 0;
1161 spin_unlock(&inode->i_lock);
1163 mutex_unlock(&session->s_mutex);
1164 ceph_put_mds_session(session);
1166 if (!ret)
1167 return ret;
1168 mutex_lock(&mdsc->mutex);
1171 mutex_unlock(&mdsc->mutex);
1172 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1173 return ret;
1177 * called under s_mutex
1179 static void send_cap_releases(struct ceph_mds_client *mdsc,
1180 struct ceph_mds_session *session)
1182 struct ceph_msg *msg;
1184 dout("send_cap_releases mds%d\n", session->s_mds);
1185 spin_lock(&session->s_cap_lock);
1186 while (!list_empty(&session->s_cap_releases_done)) {
1187 msg = list_first_entry(&session->s_cap_releases_done,
1188 struct ceph_msg, list_head);
1189 list_del_init(&msg->list_head);
1190 spin_unlock(&session->s_cap_lock);
1191 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1192 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1193 ceph_con_send(&session->s_con, msg);
1194 spin_lock(&session->s_cap_lock);
1196 spin_unlock(&session->s_cap_lock);
1199 static void discard_cap_releases(struct ceph_mds_client *mdsc,
1200 struct ceph_mds_session *session)
1202 struct ceph_msg *msg;
1203 struct ceph_mds_cap_release *head;
1204 unsigned num;
1206 dout("discard_cap_releases mds%d\n", session->s_mds);
1207 spin_lock(&session->s_cap_lock);
1209 /* zero out the in-progress message */
1210 msg = list_first_entry(&session->s_cap_releases,
1211 struct ceph_msg, list_head);
1212 head = msg->front.iov_base;
1213 num = le32_to_cpu(head->num);
1214 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1215 head->num = cpu_to_le32(0);
1216 session->s_num_cap_releases += num;
1218 /* requeue completed messages */
1219 while (!list_empty(&session->s_cap_releases_done)) {
1220 msg = list_first_entry(&session->s_cap_releases_done,
1221 struct ceph_msg, list_head);
1222 list_del_init(&msg->list_head);
1224 head = msg->front.iov_base;
1225 num = le32_to_cpu(head->num);
1226 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1227 num);
1228 session->s_num_cap_releases += num;
1229 head->num = cpu_to_le32(0);
1230 msg->front.iov_len = sizeof(*head);
1231 list_add(&msg->list_head, &session->s_cap_releases);
1234 spin_unlock(&session->s_cap_lock);
1238 * requests
1242 * Create an mds request.
1244 struct ceph_mds_request *
1245 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1247 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1249 if (!req)
1250 return ERR_PTR(-ENOMEM);
1252 mutex_init(&req->r_fill_mutex);
1253 req->r_started = jiffies;
1254 req->r_resend_mds = -1;
1255 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1256 req->r_fmode = -1;
1257 kref_init(&req->r_kref);
1258 INIT_LIST_HEAD(&req->r_wait);
1259 init_completion(&req->r_completion);
1260 init_completion(&req->r_safe_completion);
1261 INIT_LIST_HEAD(&req->r_unsafe_item);
1263 req->r_op = op;
1264 req->r_direct_mode = mode;
1265 return req;
1269 * return oldest (lowest) request, tid in request tree, 0 if none.
1271 * called under mdsc->mutex.
1273 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1275 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1276 return NULL;
1277 return rb_entry(rb_first(&mdsc->request_tree),
1278 struct ceph_mds_request, r_node);
1281 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1283 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1285 if (req)
1286 return req->r_tid;
1287 return 0;
1291 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1292 * on build_path_from_dentry in fs/cifs/dir.c.
1294 * If @stop_on_nosnap, generate path relative to the first non-snapped
1295 * inode.
1297 * Encode hidden .snap dirs as a double /, i.e.
1298 * foo/.snap/bar -> foo//bar
1300 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1301 int stop_on_nosnap)
1303 struct dentry *temp;
1304 char *path;
1305 int len, pos;
1307 if (dentry == NULL)
1308 return ERR_PTR(-EINVAL);
1310 retry:
1311 len = 0;
1312 for (temp = dentry; !IS_ROOT(temp);) {
1313 struct inode *inode = temp->d_inode;
1314 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1315 len++; /* slash only */
1316 else if (stop_on_nosnap && inode &&
1317 ceph_snap(inode) == CEPH_NOSNAP)
1318 break;
1319 else
1320 len += 1 + temp->d_name.len;
1321 temp = temp->d_parent;
1322 if (temp == NULL) {
1323 pr_err("build_path corrupt dentry %p\n", dentry);
1324 return ERR_PTR(-EINVAL);
1327 if (len)
1328 len--; /* no leading '/' */
1330 path = kmalloc(len+1, GFP_NOFS);
1331 if (path == NULL)
1332 return ERR_PTR(-ENOMEM);
1333 pos = len;
1334 path[pos] = 0; /* trailing null */
1335 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1336 struct inode *inode = temp->d_inode;
1338 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1339 dout("build_path path+%d: %p SNAPDIR\n",
1340 pos, temp);
1341 } else if (stop_on_nosnap && inode &&
1342 ceph_snap(inode) == CEPH_NOSNAP) {
1343 break;
1344 } else {
1345 pos -= temp->d_name.len;
1346 if (pos < 0)
1347 break;
1348 strncpy(path + pos, temp->d_name.name,
1349 temp->d_name.len);
1351 if (pos)
1352 path[--pos] = '/';
1353 temp = temp->d_parent;
1354 if (temp == NULL) {
1355 pr_err("build_path corrupt dentry\n");
1356 kfree(path);
1357 return ERR_PTR(-EINVAL);
1360 if (pos != 0) {
1361 pr_err("build_path did not end path lookup where "
1362 "expected, namelen is %d, pos is %d\n", len, pos);
1363 /* presumably this is only possible if racing with a
1364 rename of one of the parent directories (we can not
1365 lock the dentries above us to prevent this, but
1366 retrying should be harmless) */
1367 kfree(path);
1368 goto retry;
1371 *base = ceph_ino(temp->d_inode);
1372 *plen = len;
1373 dout("build_path on %p %d built %llx '%.*s'\n",
1374 dentry, atomic_read(&dentry->d_count), *base, len, path);
1375 return path;
1378 static int build_dentry_path(struct dentry *dentry,
1379 const char **ppath, int *ppathlen, u64 *pino,
1380 int *pfreepath)
1382 char *path;
1384 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1385 *pino = ceph_ino(dentry->d_parent->d_inode);
1386 *ppath = dentry->d_name.name;
1387 *ppathlen = dentry->d_name.len;
1388 return 0;
1390 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1391 if (IS_ERR(path))
1392 return PTR_ERR(path);
1393 *ppath = path;
1394 *pfreepath = 1;
1395 return 0;
1398 static int build_inode_path(struct inode *inode,
1399 const char **ppath, int *ppathlen, u64 *pino,
1400 int *pfreepath)
1402 struct dentry *dentry;
1403 char *path;
1405 if (ceph_snap(inode) == CEPH_NOSNAP) {
1406 *pino = ceph_ino(inode);
1407 *ppathlen = 0;
1408 return 0;
1410 dentry = d_find_alias(inode);
1411 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1412 dput(dentry);
1413 if (IS_ERR(path))
1414 return PTR_ERR(path);
1415 *ppath = path;
1416 *pfreepath = 1;
1417 return 0;
1421 * request arguments may be specified via an inode *, a dentry *, or
1422 * an explicit ino+path.
1424 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1425 const char *rpath, u64 rino,
1426 const char **ppath, int *pathlen,
1427 u64 *ino, int *freepath)
1429 int r = 0;
1431 if (rinode) {
1432 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1433 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1434 ceph_snap(rinode));
1435 } else if (rdentry) {
1436 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1437 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1438 *ppath);
1439 } else if (rpath) {
1440 *ino = rino;
1441 *ppath = rpath;
1442 *pathlen = strlen(rpath);
1443 dout(" path %.*s\n", *pathlen, rpath);
1446 return r;
1450 * called under mdsc->mutex
1452 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1453 struct ceph_mds_request *req,
1454 int mds)
1456 struct ceph_msg *msg;
1457 struct ceph_mds_request_head *head;
1458 const char *path1 = NULL;
1459 const char *path2 = NULL;
1460 u64 ino1 = 0, ino2 = 0;
1461 int pathlen1 = 0, pathlen2 = 0;
1462 int freepath1 = 0, freepath2 = 0;
1463 int len;
1464 u16 releases;
1465 void *p, *end;
1466 int ret;
1468 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1469 req->r_path1, req->r_ino1.ino,
1470 &path1, &pathlen1, &ino1, &freepath1);
1471 if (ret < 0) {
1472 msg = ERR_PTR(ret);
1473 goto out;
1476 ret = set_request_path_attr(NULL, req->r_old_dentry,
1477 req->r_path2, req->r_ino2.ino,
1478 &path2, &pathlen2, &ino2, &freepath2);
1479 if (ret < 0) {
1480 msg = ERR_PTR(ret);
1481 goto out_free1;
1484 len = sizeof(*head) +
1485 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
1487 /* calculate (max) length for cap releases */
1488 len += sizeof(struct ceph_mds_request_release) *
1489 (!!req->r_inode_drop + !!req->r_dentry_drop +
1490 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1491 if (req->r_dentry_drop)
1492 len += req->r_dentry->d_name.len;
1493 if (req->r_old_dentry_drop)
1494 len += req->r_old_dentry->d_name.len;
1496 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS);
1497 if (!msg) {
1498 msg = ERR_PTR(-ENOMEM);
1499 goto out_free2;
1502 msg->hdr.tid = cpu_to_le64(req->r_tid);
1504 head = msg->front.iov_base;
1505 p = msg->front.iov_base + sizeof(*head);
1506 end = msg->front.iov_base + msg->front.iov_len;
1508 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1509 head->op = cpu_to_le32(req->r_op);
1510 head->caller_uid = cpu_to_le32(current_fsuid());
1511 head->caller_gid = cpu_to_le32(current_fsgid());
1512 head->args = req->r_args;
1514 ceph_encode_filepath(&p, end, ino1, path1);
1515 ceph_encode_filepath(&p, end, ino2, path2);
1517 /* cap releases */
1518 releases = 0;
1519 if (req->r_inode_drop)
1520 releases += ceph_encode_inode_release(&p,
1521 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1522 mds, req->r_inode_drop, req->r_inode_unless, 0);
1523 if (req->r_dentry_drop)
1524 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1525 mds, req->r_dentry_drop, req->r_dentry_unless);
1526 if (req->r_old_dentry_drop)
1527 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1528 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1529 if (req->r_old_inode_drop)
1530 releases += ceph_encode_inode_release(&p,
1531 req->r_old_dentry->d_inode,
1532 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1533 head->num_releases = cpu_to_le16(releases);
1535 BUG_ON(p > end);
1536 msg->front.iov_len = p - msg->front.iov_base;
1537 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1539 msg->pages = req->r_pages;
1540 msg->nr_pages = req->r_num_pages;
1541 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1542 msg->hdr.data_off = cpu_to_le16(0);
1544 out_free2:
1545 if (freepath2)
1546 kfree((char *)path2);
1547 out_free1:
1548 if (freepath1)
1549 kfree((char *)path1);
1550 out:
1551 return msg;
1555 * called under mdsc->mutex if error, under no mutex if
1556 * success.
1558 static void complete_request(struct ceph_mds_client *mdsc,
1559 struct ceph_mds_request *req)
1561 if (req->r_callback)
1562 req->r_callback(mdsc, req);
1563 else
1564 complete(&req->r_completion);
1568 * called under mdsc->mutex
1570 static int __prepare_send_request(struct ceph_mds_client *mdsc,
1571 struct ceph_mds_request *req,
1572 int mds)
1574 struct ceph_mds_request_head *rhead;
1575 struct ceph_msg *msg;
1576 int flags = 0;
1578 req->r_mds = mds;
1579 req->r_attempts++;
1580 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1581 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1583 if (req->r_request) {
1584 ceph_msg_put(req->r_request);
1585 req->r_request = NULL;
1587 msg = create_request_message(mdsc, req, mds);
1588 if (IS_ERR(msg)) {
1589 req->r_err = PTR_ERR(msg);
1590 complete_request(mdsc, req);
1591 return PTR_ERR(msg);
1593 req->r_request = msg;
1595 rhead = msg->front.iov_base;
1596 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1597 if (req->r_got_unsafe)
1598 flags |= CEPH_MDS_FLAG_REPLAY;
1599 if (req->r_locked_dir)
1600 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1601 rhead->flags = cpu_to_le32(flags);
1602 rhead->num_fwd = req->r_num_fwd;
1603 rhead->num_retry = req->r_attempts - 1;
1605 dout(" r_locked_dir = %p\n", req->r_locked_dir);
1607 if (req->r_target_inode && req->r_got_unsafe)
1608 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1609 else
1610 rhead->ino = 0;
1611 return 0;
1615 * send request, or put it on the appropriate wait list.
1617 static int __do_request(struct ceph_mds_client *mdsc,
1618 struct ceph_mds_request *req)
1620 struct ceph_mds_session *session = NULL;
1621 int mds = -1;
1622 int err = -EAGAIN;
1624 if (req->r_err || req->r_got_result)
1625 goto out;
1627 if (req->r_timeout &&
1628 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1629 dout("do_request timed out\n");
1630 err = -EIO;
1631 goto finish;
1634 mds = __choose_mds(mdsc, req);
1635 if (mds < 0 ||
1636 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1637 dout("do_request no mds or not active, waiting for map\n");
1638 list_add(&req->r_wait, &mdsc->waiting_for_map);
1639 goto out;
1642 /* get, open session */
1643 session = __ceph_lookup_mds_session(mdsc, mds);
1644 if (!session) {
1645 session = register_session(mdsc, mds);
1646 if (IS_ERR(session)) {
1647 err = PTR_ERR(session);
1648 goto finish;
1651 dout("do_request mds%d session %p state %s\n", mds, session,
1652 session_state_name(session->s_state));
1653 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1654 session->s_state != CEPH_MDS_SESSION_HUNG) {
1655 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1656 session->s_state == CEPH_MDS_SESSION_CLOSING)
1657 __open_session(mdsc, session);
1658 list_add(&req->r_wait, &session->s_waiting);
1659 goto out_session;
1662 /* send request */
1663 req->r_session = get_session(session);
1664 req->r_resend_mds = -1; /* forget any previous mds hint */
1666 if (req->r_request_started == 0) /* note request start time */
1667 req->r_request_started = jiffies;
1669 err = __prepare_send_request(mdsc, req, mds);
1670 if (!err) {
1671 ceph_msg_get(req->r_request);
1672 ceph_con_send(&session->s_con, req->r_request);
1675 out_session:
1676 ceph_put_mds_session(session);
1677 out:
1678 return err;
1680 finish:
1681 req->r_err = err;
1682 complete_request(mdsc, req);
1683 goto out;
1687 * called under mdsc->mutex
1689 static void __wake_requests(struct ceph_mds_client *mdsc,
1690 struct list_head *head)
1692 struct ceph_mds_request *req, *nreq;
1694 list_for_each_entry_safe(req, nreq, head, r_wait) {
1695 list_del_init(&req->r_wait);
1696 __do_request(mdsc, req);
1701 * Wake up threads with requests pending for @mds, so that they can
1702 * resubmit their requests to a possibly different mds.
1704 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
1706 struct ceph_mds_request *req;
1707 struct rb_node *p;
1709 dout("kick_requests mds%d\n", mds);
1710 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1711 req = rb_entry(p, struct ceph_mds_request, r_node);
1712 if (req->r_got_unsafe)
1713 continue;
1714 if (req->r_session &&
1715 req->r_session->s_mds == mds) {
1716 dout(" kicking tid %llu\n", req->r_tid);
1717 put_request_session(req);
1718 __do_request(mdsc, req);
1723 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1724 struct ceph_mds_request *req)
1726 dout("submit_request on %p\n", req);
1727 mutex_lock(&mdsc->mutex);
1728 __register_request(mdsc, req, NULL);
1729 __do_request(mdsc, req);
1730 mutex_unlock(&mdsc->mutex);
1734 * Synchrously perform an mds request. Take care of all of the
1735 * session setup, forwarding, retry details.
1737 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1738 struct inode *dir,
1739 struct ceph_mds_request *req)
1741 int err;
1743 dout("do_request on %p\n", req);
1745 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1746 if (req->r_inode)
1747 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1748 if (req->r_locked_dir)
1749 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1750 if (req->r_old_dentry)
1751 ceph_get_cap_refs(
1752 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1753 CEPH_CAP_PIN);
1755 /* issue */
1756 mutex_lock(&mdsc->mutex);
1757 __register_request(mdsc, req, dir);
1758 __do_request(mdsc, req);
1760 if (req->r_err) {
1761 err = req->r_err;
1762 __unregister_request(mdsc, req);
1763 dout("do_request early error %d\n", err);
1764 goto out;
1767 /* wait */
1768 mutex_unlock(&mdsc->mutex);
1769 dout("do_request waiting\n");
1770 if (req->r_timeout) {
1771 err = (long)wait_for_completion_interruptible_timeout(
1772 &req->r_completion, req->r_timeout);
1773 if (err == 0)
1774 err = -EIO;
1775 } else {
1776 err = wait_for_completion_interruptible(&req->r_completion);
1778 dout("do_request waited, got %d\n", err);
1779 mutex_lock(&mdsc->mutex);
1781 /* only abort if we didn't race with a real reply */
1782 if (req->r_got_result) {
1783 err = le32_to_cpu(req->r_reply_info.head->result);
1784 } else if (err < 0) {
1785 dout("aborted request %lld with %d\n", req->r_tid, err);
1788 * ensure we aren't running concurrently with
1789 * ceph_fill_trace or ceph_readdir_prepopulate, which
1790 * rely on locks (dir mutex) held by our caller.
1792 mutex_lock(&req->r_fill_mutex);
1793 req->r_err = err;
1794 req->r_aborted = true;
1795 mutex_unlock(&req->r_fill_mutex);
1797 if (req->r_locked_dir &&
1798 (req->r_op & CEPH_MDS_OP_WRITE))
1799 ceph_invalidate_dir_request(req);
1800 } else {
1801 err = req->r_err;
1804 out:
1805 mutex_unlock(&mdsc->mutex);
1806 dout("do_request %p done, result %d\n", req, err);
1807 return err;
1811 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1812 * namespace request.
1814 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
1816 struct inode *inode = req->r_locked_dir;
1817 struct ceph_inode_info *ci = ceph_inode(inode);
1819 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode);
1820 spin_lock(&inode->i_lock);
1821 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1822 ci->i_release_count++;
1823 spin_unlock(&inode->i_lock);
1825 if (req->r_dentry)
1826 ceph_invalidate_dentry_lease(req->r_dentry);
1827 if (req->r_old_dentry)
1828 ceph_invalidate_dentry_lease(req->r_old_dentry);
1832 * Handle mds reply.
1834 * We take the session mutex and parse and process the reply immediately.
1835 * This preserves the logical ordering of replies, capabilities, etc., sent
1836 * by the MDS as they are applied to our local cache.
1838 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
1840 struct ceph_mds_client *mdsc = session->s_mdsc;
1841 struct ceph_mds_request *req;
1842 struct ceph_mds_reply_head *head = msg->front.iov_base;
1843 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
1844 u64 tid;
1845 int err, result;
1846 int mds = session->s_mds;
1848 if (msg->front.iov_len < sizeof(*head)) {
1849 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
1850 ceph_msg_dump(msg);
1851 return;
1854 /* get request, session */
1855 tid = le64_to_cpu(msg->hdr.tid);
1856 mutex_lock(&mdsc->mutex);
1857 req = __lookup_request(mdsc, tid);
1858 if (!req) {
1859 dout("handle_reply on unknown tid %llu\n", tid);
1860 mutex_unlock(&mdsc->mutex);
1861 return;
1863 dout("handle_reply %p\n", req);
1865 /* correct session? */
1866 if (req->r_session != session) {
1867 pr_err("mdsc_handle_reply got %llu on session mds%d"
1868 " not mds%d\n", tid, session->s_mds,
1869 req->r_session ? req->r_session->s_mds : -1);
1870 mutex_unlock(&mdsc->mutex);
1871 goto out;
1874 /* dup? */
1875 if ((req->r_got_unsafe && !head->safe) ||
1876 (req->r_got_safe && head->safe)) {
1877 pr_warning("got a dup %s reply on %llu from mds%d\n",
1878 head->safe ? "safe" : "unsafe", tid, mds);
1879 mutex_unlock(&mdsc->mutex);
1880 goto out;
1882 if (req->r_got_safe && !head->safe) {
1883 pr_warning("got unsafe after safe on %llu from mds%d\n",
1884 tid, mds);
1885 mutex_unlock(&mdsc->mutex);
1886 goto out;
1889 result = le32_to_cpu(head->result);
1892 * Tolerate 2 consecutive ESTALEs from the same mds.
1893 * FIXME: we should be looking at the cap migrate_seq.
1895 if (result == -ESTALE) {
1896 req->r_direct_mode = USE_AUTH_MDS;
1897 req->r_num_stale++;
1898 if (req->r_num_stale <= 2) {
1899 __do_request(mdsc, req);
1900 mutex_unlock(&mdsc->mutex);
1901 goto out;
1903 } else {
1904 req->r_num_stale = 0;
1907 if (head->safe) {
1908 req->r_got_safe = true;
1909 __unregister_request(mdsc, req);
1910 complete(&req->r_safe_completion);
1912 if (req->r_got_unsafe) {
1914 * We already handled the unsafe response, now do the
1915 * cleanup. No need to examine the response; the MDS
1916 * doesn't include any result info in the safe
1917 * response. And even if it did, there is nothing
1918 * useful we could do with a revised return value.
1920 dout("got safe reply %llu, mds%d\n", tid, mds);
1921 list_del_init(&req->r_unsafe_item);
1923 /* last unsafe request during umount? */
1924 if (mdsc->stopping && !__get_oldest_req(mdsc))
1925 complete(&mdsc->safe_umount_waiters);
1926 mutex_unlock(&mdsc->mutex);
1927 goto out;
1929 } else {
1930 req->r_got_unsafe = true;
1931 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
1934 dout("handle_reply tid %lld result %d\n", tid, result);
1935 rinfo = &req->r_reply_info;
1936 err = parse_reply_info(msg, rinfo);
1937 mutex_unlock(&mdsc->mutex);
1939 mutex_lock(&session->s_mutex);
1940 if (err < 0) {
1941 pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds);
1942 ceph_msg_dump(msg);
1943 goto out_err;
1946 /* snap trace */
1947 if (rinfo->snapblob_len) {
1948 down_write(&mdsc->snap_rwsem);
1949 ceph_update_snap_trace(mdsc, rinfo->snapblob,
1950 rinfo->snapblob + rinfo->snapblob_len,
1951 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
1952 downgrade_write(&mdsc->snap_rwsem);
1953 } else {
1954 down_read(&mdsc->snap_rwsem);
1957 /* insert trace into our cache */
1958 mutex_lock(&req->r_fill_mutex);
1959 err = ceph_fill_trace(mdsc->client->sb, req, req->r_session);
1960 if (err == 0) {
1961 if (result == 0 && rinfo->dir_nr)
1962 ceph_readdir_prepopulate(req, req->r_session);
1963 ceph_unreserve_caps(&req->r_caps_reservation);
1965 mutex_unlock(&req->r_fill_mutex);
1967 up_read(&mdsc->snap_rwsem);
1968 out_err:
1969 mutex_lock(&mdsc->mutex);
1970 if (!req->r_aborted) {
1971 if (err) {
1972 req->r_err = err;
1973 } else {
1974 req->r_reply = msg;
1975 ceph_msg_get(msg);
1976 req->r_got_result = true;
1978 } else {
1979 dout("reply arrived after request %lld was aborted\n", tid);
1981 mutex_unlock(&mdsc->mutex);
1983 add_cap_releases(mdsc, req->r_session, -1);
1984 mutex_unlock(&session->s_mutex);
1986 /* kick calling process */
1987 complete_request(mdsc, req);
1988 out:
1989 ceph_mdsc_put_request(req);
1990 return;
1996 * handle mds notification that our request has been forwarded.
1998 static void handle_forward(struct ceph_mds_client *mdsc,
1999 struct ceph_mds_session *session,
2000 struct ceph_msg *msg)
2002 struct ceph_mds_request *req;
2003 u64 tid = le64_to_cpu(msg->hdr.tid);
2004 u32 next_mds;
2005 u32 fwd_seq;
2006 int err = -EINVAL;
2007 void *p = msg->front.iov_base;
2008 void *end = p + msg->front.iov_len;
2010 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2011 next_mds = ceph_decode_32(&p);
2012 fwd_seq = ceph_decode_32(&p);
2014 mutex_lock(&mdsc->mutex);
2015 req = __lookup_request(mdsc, tid);
2016 if (!req) {
2017 dout("forward %llu to mds%d - req dne\n", tid, next_mds);
2018 goto out; /* dup reply? */
2021 if (fwd_seq <= req->r_num_fwd) {
2022 dout("forward %llu to mds%d - old seq %d <= %d\n",
2023 tid, next_mds, req->r_num_fwd, fwd_seq);
2024 } else {
2025 /* resend. forward race not possible; mds would drop */
2026 dout("forward %llu to mds%d (we resend)\n", tid, next_mds);
2027 req->r_num_fwd = fwd_seq;
2028 req->r_resend_mds = next_mds;
2029 put_request_session(req);
2030 __do_request(mdsc, req);
2032 ceph_mdsc_put_request(req);
2033 out:
2034 mutex_unlock(&mdsc->mutex);
2035 return;
2037 bad:
2038 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2042 * handle a mds session control message
2044 static void handle_session(struct ceph_mds_session *session,
2045 struct ceph_msg *msg)
2047 struct ceph_mds_client *mdsc = session->s_mdsc;
2048 u32 op;
2049 u64 seq;
2050 int mds = session->s_mds;
2051 struct ceph_mds_session_head *h = msg->front.iov_base;
2052 int wake = 0;
2054 /* decode */
2055 if (msg->front.iov_len != sizeof(*h))
2056 goto bad;
2057 op = le32_to_cpu(h->op);
2058 seq = le64_to_cpu(h->seq);
2060 mutex_lock(&mdsc->mutex);
2061 if (op == CEPH_SESSION_CLOSE)
2062 __unregister_session(mdsc, session);
2063 /* FIXME: this ttl calculation is generous */
2064 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2065 mutex_unlock(&mdsc->mutex);
2067 mutex_lock(&session->s_mutex);
2069 dout("handle_session mds%d %s %p state %s seq %llu\n",
2070 mds, ceph_session_op_name(op), session,
2071 session_state_name(session->s_state), seq);
2073 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2074 session->s_state = CEPH_MDS_SESSION_OPEN;
2075 pr_info("mds%d came back\n", session->s_mds);
2078 switch (op) {
2079 case CEPH_SESSION_OPEN:
2080 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2081 pr_info("mds%d reconnect success\n", session->s_mds);
2082 session->s_state = CEPH_MDS_SESSION_OPEN;
2083 renewed_caps(mdsc, session, 0);
2084 wake = 1;
2085 if (mdsc->stopping)
2086 __close_session(mdsc, session);
2087 break;
2089 case CEPH_SESSION_RENEWCAPS:
2090 if (session->s_renew_seq == seq)
2091 renewed_caps(mdsc, session, 1);
2092 break;
2094 case CEPH_SESSION_CLOSE:
2095 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2096 pr_info("mds%d reconnect denied\n", session->s_mds);
2097 remove_session_caps(session);
2098 wake = 1; /* for good measure */
2099 complete(&mdsc->session_close_waiters);
2100 kick_requests(mdsc, mds);
2101 break;
2103 case CEPH_SESSION_STALE:
2104 pr_info("mds%d caps went stale, renewing\n",
2105 session->s_mds);
2106 spin_lock(&session->s_cap_lock);
2107 session->s_cap_gen++;
2108 session->s_cap_ttl = 0;
2109 spin_unlock(&session->s_cap_lock);
2110 send_renew_caps(mdsc, session);
2111 break;
2113 case CEPH_SESSION_RECALL_STATE:
2114 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2115 break;
2117 default:
2118 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2119 WARN_ON(1);
2122 mutex_unlock(&session->s_mutex);
2123 if (wake) {
2124 mutex_lock(&mdsc->mutex);
2125 __wake_requests(mdsc, &session->s_waiting);
2126 mutex_unlock(&mdsc->mutex);
2128 return;
2130 bad:
2131 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2132 (int)msg->front.iov_len);
2133 ceph_msg_dump(msg);
2134 return;
2139 * called under session->mutex.
2141 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2142 struct ceph_mds_session *session)
2144 struct ceph_mds_request *req, *nreq;
2145 int err;
2147 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2149 mutex_lock(&mdsc->mutex);
2150 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2151 err = __prepare_send_request(mdsc, req, session->s_mds);
2152 if (!err) {
2153 ceph_msg_get(req->r_request);
2154 ceph_con_send(&session->s_con, req->r_request);
2157 mutex_unlock(&mdsc->mutex);
2161 * Encode information about a cap for a reconnect with the MDS.
2163 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2164 void *arg)
2166 struct ceph_mds_cap_reconnect rec;
2167 struct ceph_inode_info *ci;
2168 struct ceph_pagelist *pagelist = arg;
2169 char *path;
2170 int pathlen, err;
2171 u64 pathbase;
2172 struct dentry *dentry;
2174 ci = cap->ci;
2176 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2177 inode, ceph_vinop(inode), cap, cap->cap_id,
2178 ceph_cap_string(cap->issued));
2179 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2180 if (err)
2181 return err;
2183 dentry = d_find_alias(inode);
2184 if (dentry) {
2185 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2186 if (IS_ERR(path)) {
2187 err = PTR_ERR(path);
2188 BUG_ON(err);
2190 } else {
2191 path = NULL;
2192 pathlen = 0;
2194 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2195 if (err)
2196 goto out;
2198 spin_lock(&inode->i_lock);
2199 cap->seq = 0; /* reset cap seq */
2200 cap->issue_seq = 0; /* and issue_seq */
2201 rec.cap_id = cpu_to_le64(cap->cap_id);
2202 rec.pathbase = cpu_to_le64(pathbase);
2203 rec.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2204 rec.issued = cpu_to_le32(cap->issued);
2205 rec.size = cpu_to_le64(inode->i_size);
2206 ceph_encode_timespec(&rec.mtime, &inode->i_mtime);
2207 ceph_encode_timespec(&rec.atime, &inode->i_atime);
2208 rec.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2209 spin_unlock(&inode->i_lock);
2211 err = ceph_pagelist_append(pagelist, &rec, sizeof(rec));
2213 out:
2214 kfree(path);
2215 dput(dentry);
2216 return err;
2221 * If an MDS fails and recovers, clients need to reconnect in order to
2222 * reestablish shared state. This includes all caps issued through
2223 * this session _and_ the snap_realm hierarchy. Because it's not
2224 * clear which snap realms the mds cares about, we send everything we
2225 * know about.. that ensures we'll then get any new info the
2226 * recovering MDS might have.
2228 * This is a relatively heavyweight operation, but it's rare.
2230 * called with mdsc->mutex held.
2232 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2233 struct ceph_mds_session *session)
2235 struct ceph_msg *reply;
2236 struct rb_node *p;
2237 int mds = session->s_mds;
2238 int err = -ENOMEM;
2239 struct ceph_pagelist *pagelist;
2241 pr_info("mds%d reconnect start\n", mds);
2243 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2244 if (!pagelist)
2245 goto fail_nopagelist;
2246 ceph_pagelist_init(pagelist);
2248 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS);
2249 if (!reply)
2250 goto fail_nomsg;
2252 mutex_lock(&session->s_mutex);
2253 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2254 session->s_seq = 0;
2256 ceph_con_open(&session->s_con,
2257 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2259 /* replay unsafe requests */
2260 replay_unsafe_requests(mdsc, session);
2262 down_read(&mdsc->snap_rwsem);
2264 dout("session %p state %s\n", session,
2265 session_state_name(session->s_state));
2267 /* drop old cap expires; we're about to reestablish that state */
2268 discard_cap_releases(mdsc, session);
2270 /* traverse this session's caps */
2271 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2272 if (err)
2273 goto fail;
2274 err = iterate_session_caps(session, encode_caps_cb, pagelist);
2275 if (err < 0)
2276 goto fail;
2279 * snaprealms. we provide mds with the ino, seq (version), and
2280 * parent for all of our realms. If the mds has any newer info,
2281 * it will tell us.
2283 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2284 struct ceph_snap_realm *realm =
2285 rb_entry(p, struct ceph_snap_realm, node);
2286 struct ceph_mds_snaprealm_reconnect sr_rec;
2288 dout(" adding snap realm %llx seq %lld parent %llx\n",
2289 realm->ino, realm->seq, realm->parent_ino);
2290 sr_rec.ino = cpu_to_le64(realm->ino);
2291 sr_rec.seq = cpu_to_le64(realm->seq);
2292 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2293 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2294 if (err)
2295 goto fail;
2298 reply->pagelist = pagelist;
2299 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2300 reply->nr_pages = calc_pages_for(0, pagelist->length);
2301 ceph_con_send(&session->s_con, reply);
2303 mutex_unlock(&session->s_mutex);
2305 mutex_lock(&mdsc->mutex);
2306 __wake_requests(mdsc, &session->s_waiting);
2307 mutex_unlock(&mdsc->mutex);
2309 up_read(&mdsc->snap_rwsem);
2310 return;
2312 fail:
2313 ceph_msg_put(reply);
2314 up_read(&mdsc->snap_rwsem);
2315 mutex_unlock(&session->s_mutex);
2316 fail_nomsg:
2317 ceph_pagelist_release(pagelist);
2318 kfree(pagelist);
2319 fail_nopagelist:
2320 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
2321 return;
2326 * compare old and new mdsmaps, kicking requests
2327 * and closing out old connections as necessary
2329 * called under mdsc->mutex.
2331 static void check_new_map(struct ceph_mds_client *mdsc,
2332 struct ceph_mdsmap *newmap,
2333 struct ceph_mdsmap *oldmap)
2335 int i;
2336 int oldstate, newstate;
2337 struct ceph_mds_session *s;
2339 dout("check_new_map new %u old %u\n",
2340 newmap->m_epoch, oldmap->m_epoch);
2342 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2343 if (mdsc->sessions[i] == NULL)
2344 continue;
2345 s = mdsc->sessions[i];
2346 oldstate = ceph_mdsmap_get_state(oldmap, i);
2347 newstate = ceph_mdsmap_get_state(newmap, i);
2349 dout("check_new_map mds%d state %s -> %s (session %s)\n",
2350 i, ceph_mds_state_name(oldstate),
2351 ceph_mds_state_name(newstate),
2352 session_state_name(s->s_state));
2354 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2355 ceph_mdsmap_get_addr(newmap, i),
2356 sizeof(struct ceph_entity_addr))) {
2357 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2358 /* the session never opened, just close it
2359 * out now */
2360 __wake_requests(mdsc, &s->s_waiting);
2361 __unregister_session(mdsc, s);
2362 } else {
2363 /* just close it */
2364 mutex_unlock(&mdsc->mutex);
2365 mutex_lock(&s->s_mutex);
2366 mutex_lock(&mdsc->mutex);
2367 ceph_con_close(&s->s_con);
2368 mutex_unlock(&s->s_mutex);
2369 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2372 /* kick any requests waiting on the recovering mds */
2373 kick_requests(mdsc, i);
2374 } else if (oldstate == newstate) {
2375 continue; /* nothing new with this mds */
2379 * send reconnect?
2381 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
2382 newstate >= CEPH_MDS_STATE_RECONNECT) {
2383 mutex_unlock(&mdsc->mutex);
2384 send_mds_reconnect(mdsc, s);
2385 mutex_lock(&mdsc->mutex);
2389 * kick request on any mds that has gone active.
2391 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2392 newstate >= CEPH_MDS_STATE_ACTIVE) {
2393 if (oldstate != CEPH_MDS_STATE_CREATING &&
2394 oldstate != CEPH_MDS_STATE_STARTING)
2395 pr_info("mds%d recovery completed\n", s->s_mds);
2396 kick_requests(mdsc, i);
2397 ceph_kick_flushing_caps(mdsc, s);
2398 wake_up_session_caps(s, 1);
2406 * leases
2410 * caller must hold session s_mutex, dentry->d_lock
2412 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2414 struct ceph_dentry_info *di = ceph_dentry(dentry);
2416 ceph_put_mds_session(di->lease_session);
2417 di->lease_session = NULL;
2420 static void handle_lease(struct ceph_mds_client *mdsc,
2421 struct ceph_mds_session *session,
2422 struct ceph_msg *msg)
2424 struct super_block *sb = mdsc->client->sb;
2425 struct inode *inode;
2426 struct ceph_inode_info *ci;
2427 struct dentry *parent, *dentry;
2428 struct ceph_dentry_info *di;
2429 int mds = session->s_mds;
2430 struct ceph_mds_lease *h = msg->front.iov_base;
2431 struct ceph_vino vino;
2432 int mask;
2433 struct qstr dname;
2434 int release = 0;
2436 dout("handle_lease from mds%d\n", mds);
2438 /* decode */
2439 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2440 goto bad;
2441 vino.ino = le64_to_cpu(h->ino);
2442 vino.snap = CEPH_NOSNAP;
2443 mask = le16_to_cpu(h->mask);
2444 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2445 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2446 if (dname.len != get_unaligned_le32(h+1))
2447 goto bad;
2449 mutex_lock(&session->s_mutex);
2450 session->s_seq++;
2452 /* lookup inode */
2453 inode = ceph_find_inode(sb, vino);
2454 dout("handle_lease '%s', mask %d, ino %llx %p\n",
2455 ceph_lease_op_name(h->action), mask, vino.ino, inode);
2456 if (inode == NULL) {
2457 dout("handle_lease no inode %llx\n", vino.ino);
2458 goto release;
2460 ci = ceph_inode(inode);
2462 /* dentry */
2463 parent = d_find_alias(inode);
2464 if (!parent) {
2465 dout("no parent dentry on inode %p\n", inode);
2466 WARN_ON(1);
2467 goto release; /* hrm... */
2469 dname.hash = full_name_hash(dname.name, dname.len);
2470 dentry = d_lookup(parent, &dname);
2471 dput(parent);
2472 if (!dentry)
2473 goto release;
2475 spin_lock(&dentry->d_lock);
2476 di = ceph_dentry(dentry);
2477 switch (h->action) {
2478 case CEPH_MDS_LEASE_REVOKE:
2479 if (di && di->lease_session == session) {
2480 h->seq = cpu_to_le32(di->lease_seq);
2481 __ceph_mdsc_drop_dentry_lease(dentry);
2483 release = 1;
2484 break;
2486 case CEPH_MDS_LEASE_RENEW:
2487 if (di && di->lease_session == session &&
2488 di->lease_gen == session->s_cap_gen &&
2489 di->lease_renew_from &&
2490 di->lease_renew_after == 0) {
2491 unsigned long duration =
2492 le32_to_cpu(h->duration_ms) * HZ / 1000;
2494 di->lease_seq = le32_to_cpu(h->seq);
2495 dentry->d_time = di->lease_renew_from + duration;
2496 di->lease_renew_after = di->lease_renew_from +
2497 (duration >> 1);
2498 di->lease_renew_from = 0;
2500 break;
2502 spin_unlock(&dentry->d_lock);
2503 dput(dentry);
2505 if (!release)
2506 goto out;
2508 release:
2509 /* let's just reuse the same message */
2510 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2511 ceph_msg_get(msg);
2512 ceph_con_send(&session->s_con, msg);
2514 out:
2515 iput(inode);
2516 mutex_unlock(&session->s_mutex);
2517 return;
2519 bad:
2520 pr_err("corrupt lease message\n");
2521 ceph_msg_dump(msg);
2524 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2525 struct inode *inode,
2526 struct dentry *dentry, char action,
2527 u32 seq)
2529 struct ceph_msg *msg;
2530 struct ceph_mds_lease *lease;
2531 int len = sizeof(*lease) + sizeof(u32);
2532 int dnamelen = 0;
2534 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2535 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2536 dnamelen = dentry->d_name.len;
2537 len += dnamelen;
2539 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS);
2540 if (!msg)
2541 return;
2542 lease = msg->front.iov_base;
2543 lease->action = action;
2544 lease->mask = cpu_to_le16(CEPH_LOCK_DN);
2545 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2546 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2547 lease->seq = cpu_to_le32(seq);
2548 put_unaligned_le32(dnamelen, lease + 1);
2549 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2552 * if this is a preemptive lease RELEASE, no need to
2553 * flush request stream, since the actual request will
2554 * soon follow.
2556 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2558 ceph_con_send(&session->s_con, msg);
2562 * Preemptively release a lease we expect to invalidate anyway.
2563 * Pass @inode always, @dentry is optional.
2565 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2566 struct dentry *dentry, int mask)
2568 struct ceph_dentry_info *di;
2569 struct ceph_mds_session *session;
2570 u32 seq;
2572 BUG_ON(inode == NULL);
2573 BUG_ON(dentry == NULL);
2574 BUG_ON(mask != CEPH_LOCK_DN);
2576 /* is dentry lease valid? */
2577 spin_lock(&dentry->d_lock);
2578 di = ceph_dentry(dentry);
2579 if (!di || !di->lease_session ||
2580 di->lease_session->s_mds < 0 ||
2581 di->lease_gen != di->lease_session->s_cap_gen ||
2582 !time_before(jiffies, dentry->d_time)) {
2583 dout("lease_release inode %p dentry %p -- "
2584 "no lease on %d\n",
2585 inode, dentry, mask);
2586 spin_unlock(&dentry->d_lock);
2587 return;
2590 /* we do have a lease on this dentry; note mds and seq */
2591 session = ceph_get_mds_session(di->lease_session);
2592 seq = di->lease_seq;
2593 __ceph_mdsc_drop_dentry_lease(dentry);
2594 spin_unlock(&dentry->d_lock);
2596 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2597 inode, dentry, mask, session->s_mds);
2598 ceph_mdsc_lease_send_msg(session, inode, dentry,
2599 CEPH_MDS_LEASE_RELEASE, seq);
2600 ceph_put_mds_session(session);
2604 * drop all leases (and dentry refs) in preparation for umount
2606 static void drop_leases(struct ceph_mds_client *mdsc)
2608 int i;
2610 dout("drop_leases\n");
2611 mutex_lock(&mdsc->mutex);
2612 for (i = 0; i < mdsc->max_sessions; i++) {
2613 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2614 if (!s)
2615 continue;
2616 mutex_unlock(&mdsc->mutex);
2617 mutex_lock(&s->s_mutex);
2618 mutex_unlock(&s->s_mutex);
2619 ceph_put_mds_session(s);
2620 mutex_lock(&mdsc->mutex);
2622 mutex_unlock(&mdsc->mutex);
2628 * delayed work -- periodically trim expired leases, renew caps with mds
2630 static void schedule_delayed(struct ceph_mds_client *mdsc)
2632 int delay = 5;
2633 unsigned hz = round_jiffies_relative(HZ * delay);
2634 schedule_delayed_work(&mdsc->delayed_work, hz);
2637 static void delayed_work(struct work_struct *work)
2639 int i;
2640 struct ceph_mds_client *mdsc =
2641 container_of(work, struct ceph_mds_client, delayed_work.work);
2642 int renew_interval;
2643 int renew_caps;
2645 dout("mdsc delayed_work\n");
2646 ceph_check_delayed_caps(mdsc);
2648 mutex_lock(&mdsc->mutex);
2649 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2650 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2651 mdsc->last_renew_caps);
2652 if (renew_caps)
2653 mdsc->last_renew_caps = jiffies;
2655 for (i = 0; i < mdsc->max_sessions; i++) {
2656 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2657 if (s == NULL)
2658 continue;
2659 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2660 dout("resending session close request for mds%d\n",
2661 s->s_mds);
2662 request_close_session(mdsc, s);
2663 ceph_put_mds_session(s);
2664 continue;
2666 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2667 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2668 s->s_state = CEPH_MDS_SESSION_HUNG;
2669 pr_info("mds%d hung\n", s->s_mds);
2672 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2673 /* this mds is failed or recovering, just wait */
2674 ceph_put_mds_session(s);
2675 continue;
2677 mutex_unlock(&mdsc->mutex);
2679 mutex_lock(&s->s_mutex);
2680 if (renew_caps)
2681 send_renew_caps(mdsc, s);
2682 else
2683 ceph_con_keepalive(&s->s_con);
2684 add_cap_releases(mdsc, s, -1);
2685 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2686 s->s_state == CEPH_MDS_SESSION_HUNG)
2687 send_cap_releases(mdsc, s);
2688 mutex_unlock(&s->s_mutex);
2689 ceph_put_mds_session(s);
2691 mutex_lock(&mdsc->mutex);
2693 mutex_unlock(&mdsc->mutex);
2695 schedule_delayed(mdsc);
2699 int ceph_mdsc_init(struct ceph_mds_client *mdsc, struct ceph_client *client)
2701 mdsc->client = client;
2702 mutex_init(&mdsc->mutex);
2703 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
2704 if (mdsc->mdsmap == NULL)
2705 return -ENOMEM;
2707 init_completion(&mdsc->safe_umount_waiters);
2708 init_completion(&mdsc->session_close_waiters);
2709 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2710 mdsc->sessions = NULL;
2711 mdsc->max_sessions = 0;
2712 mdsc->stopping = 0;
2713 init_rwsem(&mdsc->snap_rwsem);
2714 mdsc->snap_realms = RB_ROOT;
2715 INIT_LIST_HEAD(&mdsc->snap_empty);
2716 spin_lock_init(&mdsc->snap_empty_lock);
2717 mdsc->last_tid = 0;
2718 mdsc->request_tree = RB_ROOT;
2719 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2720 mdsc->last_renew_caps = jiffies;
2721 INIT_LIST_HEAD(&mdsc->cap_delay_list);
2722 spin_lock_init(&mdsc->cap_delay_lock);
2723 INIT_LIST_HEAD(&mdsc->snap_flush_list);
2724 spin_lock_init(&mdsc->snap_flush_lock);
2725 mdsc->cap_flush_seq = 0;
2726 INIT_LIST_HEAD(&mdsc->cap_dirty);
2727 mdsc->num_cap_flushing = 0;
2728 spin_lock_init(&mdsc->cap_dirty_lock);
2729 init_waitqueue_head(&mdsc->cap_flushing_wq);
2730 spin_lock_init(&mdsc->dentry_lru_lock);
2731 INIT_LIST_HEAD(&mdsc->dentry_lru);
2733 return 0;
2737 * Wait for safe replies on open mds requests. If we time out, drop
2738 * all requests from the tree to avoid dangling dentry refs.
2740 static void wait_requests(struct ceph_mds_client *mdsc)
2742 struct ceph_mds_request *req;
2743 struct ceph_client *client = mdsc->client;
2745 mutex_lock(&mdsc->mutex);
2746 if (__get_oldest_req(mdsc)) {
2747 mutex_unlock(&mdsc->mutex);
2749 dout("wait_requests waiting for requests\n");
2750 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
2751 client->mount_args->mount_timeout * HZ);
2753 /* tear down remaining requests */
2754 mutex_lock(&mdsc->mutex);
2755 while ((req = __get_oldest_req(mdsc))) {
2756 dout("wait_requests timed out on tid %llu\n",
2757 req->r_tid);
2758 __unregister_request(mdsc, req);
2761 mutex_unlock(&mdsc->mutex);
2762 dout("wait_requests done\n");
2766 * called before mount is ro, and before dentries are torn down.
2767 * (hmm, does this still race with new lookups?)
2769 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
2771 dout("pre_umount\n");
2772 mdsc->stopping = 1;
2774 drop_leases(mdsc);
2775 ceph_flush_dirty_caps(mdsc);
2776 wait_requests(mdsc);
2780 * wait for all write mds requests to flush.
2782 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
2784 struct ceph_mds_request *req = NULL, *nextreq;
2785 struct rb_node *n;
2787 mutex_lock(&mdsc->mutex);
2788 dout("wait_unsafe_requests want %lld\n", want_tid);
2789 restart:
2790 req = __get_oldest_req(mdsc);
2791 while (req && req->r_tid <= want_tid) {
2792 /* find next request */
2793 n = rb_next(&req->r_node);
2794 if (n)
2795 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
2796 else
2797 nextreq = NULL;
2798 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
2799 /* write op */
2800 ceph_mdsc_get_request(req);
2801 if (nextreq)
2802 ceph_mdsc_get_request(nextreq);
2803 mutex_unlock(&mdsc->mutex);
2804 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
2805 req->r_tid, want_tid);
2806 wait_for_completion(&req->r_safe_completion);
2807 mutex_lock(&mdsc->mutex);
2808 ceph_mdsc_put_request(req);
2809 if (!nextreq)
2810 break; /* next dne before, so we're done! */
2811 if (RB_EMPTY_NODE(&nextreq->r_node)) {
2812 /* next request was removed from tree */
2813 ceph_mdsc_put_request(nextreq);
2814 goto restart;
2816 ceph_mdsc_put_request(nextreq); /* won't go away */
2818 req = nextreq;
2820 mutex_unlock(&mdsc->mutex);
2821 dout("wait_unsafe_requests done\n");
2824 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
2826 u64 want_tid, want_flush;
2828 if (mdsc->client->mount_state == CEPH_MOUNT_SHUTDOWN)
2829 return;
2831 dout("sync\n");
2832 mutex_lock(&mdsc->mutex);
2833 want_tid = mdsc->last_tid;
2834 want_flush = mdsc->cap_flush_seq;
2835 mutex_unlock(&mdsc->mutex);
2836 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
2838 ceph_flush_dirty_caps(mdsc);
2840 wait_unsafe_requests(mdsc, want_tid);
2841 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
2846 * called after sb is ro.
2848 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
2850 struct ceph_mds_session *session;
2851 int i;
2852 int n;
2853 struct ceph_client *client = mdsc->client;
2854 unsigned long started, timeout = client->mount_args->mount_timeout * HZ;
2856 dout("close_sessions\n");
2858 mutex_lock(&mdsc->mutex);
2860 /* close sessions */
2861 started = jiffies;
2862 while (time_before(jiffies, started + timeout)) {
2863 dout("closing sessions\n");
2864 n = 0;
2865 for (i = 0; i < mdsc->max_sessions; i++) {
2866 session = __ceph_lookup_mds_session(mdsc, i);
2867 if (!session)
2868 continue;
2869 mutex_unlock(&mdsc->mutex);
2870 mutex_lock(&session->s_mutex);
2871 __close_session(mdsc, session);
2872 mutex_unlock(&session->s_mutex);
2873 ceph_put_mds_session(session);
2874 mutex_lock(&mdsc->mutex);
2875 n++;
2877 if (n == 0)
2878 break;
2880 if (client->mount_state == CEPH_MOUNT_SHUTDOWN)
2881 break;
2883 dout("waiting for sessions to close\n");
2884 mutex_unlock(&mdsc->mutex);
2885 wait_for_completion_timeout(&mdsc->session_close_waiters,
2886 timeout);
2887 mutex_lock(&mdsc->mutex);
2890 /* tear down remaining sessions */
2891 for (i = 0; i < mdsc->max_sessions; i++) {
2892 if (mdsc->sessions[i]) {
2893 session = get_session(mdsc->sessions[i]);
2894 __unregister_session(mdsc, session);
2895 mutex_unlock(&mdsc->mutex);
2896 mutex_lock(&session->s_mutex);
2897 remove_session_caps(session);
2898 mutex_unlock(&session->s_mutex);
2899 ceph_put_mds_session(session);
2900 mutex_lock(&mdsc->mutex);
2904 WARN_ON(!list_empty(&mdsc->cap_delay_list));
2906 mutex_unlock(&mdsc->mutex);
2908 ceph_cleanup_empty_realms(mdsc);
2910 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
2912 dout("stopped\n");
2915 void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
2917 dout("stop\n");
2918 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
2919 if (mdsc->mdsmap)
2920 ceph_mdsmap_destroy(mdsc->mdsmap);
2921 kfree(mdsc->sessions);
2926 * handle mds map update.
2928 void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
2930 u32 epoch;
2931 u32 maplen;
2932 void *p = msg->front.iov_base;
2933 void *end = p + msg->front.iov_len;
2934 struct ceph_mdsmap *newmap, *oldmap;
2935 struct ceph_fsid fsid;
2936 int err = -EINVAL;
2938 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
2939 ceph_decode_copy(&p, &fsid, sizeof(fsid));
2940 if (ceph_check_fsid(mdsc->client, &fsid) < 0)
2941 return;
2942 epoch = ceph_decode_32(&p);
2943 maplen = ceph_decode_32(&p);
2944 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
2946 /* do we need it? */
2947 ceph_monc_got_mdsmap(&mdsc->client->monc, epoch);
2948 mutex_lock(&mdsc->mutex);
2949 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
2950 dout("handle_map epoch %u <= our %u\n",
2951 epoch, mdsc->mdsmap->m_epoch);
2952 mutex_unlock(&mdsc->mutex);
2953 return;
2956 newmap = ceph_mdsmap_decode(&p, end);
2957 if (IS_ERR(newmap)) {
2958 err = PTR_ERR(newmap);
2959 goto bad_unlock;
2962 /* swap into place */
2963 if (mdsc->mdsmap) {
2964 oldmap = mdsc->mdsmap;
2965 mdsc->mdsmap = newmap;
2966 check_new_map(mdsc, newmap, oldmap);
2967 ceph_mdsmap_destroy(oldmap);
2968 } else {
2969 mdsc->mdsmap = newmap; /* first mds map */
2971 mdsc->client->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
2973 __wake_requests(mdsc, &mdsc->waiting_for_map);
2975 mutex_unlock(&mdsc->mutex);
2976 schedule_delayed(mdsc);
2977 return;
2979 bad_unlock:
2980 mutex_unlock(&mdsc->mutex);
2981 bad:
2982 pr_err("error decoding mdsmap %d\n", err);
2983 return;
2986 static struct ceph_connection *con_get(struct ceph_connection *con)
2988 struct ceph_mds_session *s = con->private;
2990 if (get_session(s)) {
2991 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
2992 return con;
2994 dout("mdsc con_get %p FAIL\n", s);
2995 return NULL;
2998 static void con_put(struct ceph_connection *con)
3000 struct ceph_mds_session *s = con->private;
3002 ceph_put_mds_session(s);
3003 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
3007 * if the client is unresponsive for long enough, the mds will kill
3008 * the session entirely.
3010 static void peer_reset(struct ceph_connection *con)
3012 struct ceph_mds_session *s = con->private;
3013 struct ceph_mds_client *mdsc = s->s_mdsc;
3015 pr_warning("mds%d closed our session\n", s->s_mds);
3016 send_mds_reconnect(mdsc, s);
3019 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3021 struct ceph_mds_session *s = con->private;
3022 struct ceph_mds_client *mdsc = s->s_mdsc;
3023 int type = le16_to_cpu(msg->hdr.type);
3025 mutex_lock(&mdsc->mutex);
3026 if (__verify_registered_session(mdsc, s) < 0) {
3027 mutex_unlock(&mdsc->mutex);
3028 goto out;
3030 mutex_unlock(&mdsc->mutex);
3032 switch (type) {
3033 case CEPH_MSG_MDS_MAP:
3034 ceph_mdsc_handle_map(mdsc, msg);
3035 break;
3036 case CEPH_MSG_CLIENT_SESSION:
3037 handle_session(s, msg);
3038 break;
3039 case CEPH_MSG_CLIENT_REPLY:
3040 handle_reply(s, msg);
3041 break;
3042 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
3043 handle_forward(mdsc, s, msg);
3044 break;
3045 case CEPH_MSG_CLIENT_CAPS:
3046 ceph_handle_caps(s, msg);
3047 break;
3048 case CEPH_MSG_CLIENT_SNAP:
3049 ceph_handle_snap(mdsc, s, msg);
3050 break;
3051 case CEPH_MSG_CLIENT_LEASE:
3052 handle_lease(mdsc, s, msg);
3053 break;
3055 default:
3056 pr_err("received unknown message type %d %s\n", type,
3057 ceph_msg_type_name(type));
3059 out:
3060 ceph_msg_put(msg);
3064 * authentication
3066 static int get_authorizer(struct ceph_connection *con,
3067 void **buf, int *len, int *proto,
3068 void **reply_buf, int *reply_len, int force_new)
3070 struct ceph_mds_session *s = con->private;
3071 struct ceph_mds_client *mdsc = s->s_mdsc;
3072 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3073 int ret = 0;
3075 if (force_new && s->s_authorizer) {
3076 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3077 s->s_authorizer = NULL;
3079 if (s->s_authorizer == NULL) {
3080 if (ac->ops->create_authorizer) {
3081 ret = ac->ops->create_authorizer(
3082 ac, CEPH_ENTITY_TYPE_MDS,
3083 &s->s_authorizer,
3084 &s->s_authorizer_buf,
3085 &s->s_authorizer_buf_len,
3086 &s->s_authorizer_reply_buf,
3087 &s->s_authorizer_reply_buf_len);
3088 if (ret)
3089 return ret;
3093 *proto = ac->protocol;
3094 *buf = s->s_authorizer_buf;
3095 *len = s->s_authorizer_buf_len;
3096 *reply_buf = s->s_authorizer_reply_buf;
3097 *reply_len = s->s_authorizer_reply_buf_len;
3098 return 0;
3102 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3104 struct ceph_mds_session *s = con->private;
3105 struct ceph_mds_client *mdsc = s->s_mdsc;
3106 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3108 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3111 static int invalidate_authorizer(struct ceph_connection *con)
3113 struct ceph_mds_session *s = con->private;
3114 struct ceph_mds_client *mdsc = s->s_mdsc;
3115 struct ceph_auth_client *ac = mdsc->client->monc.auth;
3117 if (ac->ops->invalidate_authorizer)
3118 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3120 return ceph_monc_validate_auth(&mdsc->client->monc);
3123 static const struct ceph_connection_operations mds_con_ops = {
3124 .get = con_get,
3125 .put = con_put,
3126 .dispatch = dispatch,
3127 .get_authorizer = get_authorizer,
3128 .verify_authorizer_reply = verify_authorizer_reply,
3129 .invalidate_authorizer = invalidate_authorizer,
3130 .peer_reset = peer_reset,
3136 /* eof */