dma40: combine duplicated code in log_sg_to_dev
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ceph / mds_client.c
blob1e30d194a8e349f4f8a2bc951681a63c9c8d70b3
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,
64 int features)
66 int err = -EIO;
68 info->in = *p;
69 *p += sizeof(struct ceph_mds_reply_inode) +
70 sizeof(*info->in->fragtree.splits) *
71 le32_to_cpu(info->in->fragtree.nsplits);
73 ceph_decode_32_safe(p, end, info->symlink_len, bad);
74 ceph_decode_need(p, end, info->symlink_len, bad);
75 info->symlink = *p;
76 *p += info->symlink_len;
78 if (features & CEPH_FEATURE_DIRLAYOUTHASH)
79 ceph_decode_copy_safe(p, end, &info->dir_layout,
80 sizeof(info->dir_layout), bad);
81 else
82 memset(&info->dir_layout, 0, sizeof(info->dir_layout));
84 ceph_decode_32_safe(p, end, info->xattr_len, bad);
85 ceph_decode_need(p, end, info->xattr_len, bad);
86 info->xattr_data = *p;
87 *p += info->xattr_len;
88 return 0;
89 bad:
90 return err;
94 * parse a normal reply, which may contain a (dir+)dentry and/or a
95 * target inode.
97 static int parse_reply_info_trace(void **p, void *end,
98 struct ceph_mds_reply_info_parsed *info,
99 int features)
101 int err;
103 if (info->head->is_dentry) {
104 err = parse_reply_info_in(p, end, &info->diri, features);
105 if (err < 0)
106 goto out_bad;
108 if (unlikely(*p + sizeof(*info->dirfrag) > end))
109 goto bad;
110 info->dirfrag = *p;
111 *p += sizeof(*info->dirfrag) +
112 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
113 if (unlikely(*p > end))
114 goto bad;
116 ceph_decode_32_safe(p, end, info->dname_len, bad);
117 ceph_decode_need(p, end, info->dname_len, bad);
118 info->dname = *p;
119 *p += info->dname_len;
120 info->dlease = *p;
121 *p += sizeof(*info->dlease);
124 if (info->head->is_target) {
125 err = parse_reply_info_in(p, end, &info->targeti, features);
126 if (err < 0)
127 goto out_bad;
130 if (unlikely(*p != end))
131 goto bad;
132 return 0;
134 bad:
135 err = -EIO;
136 out_bad:
137 pr_err("problem parsing mds trace %d\n", err);
138 return err;
142 * parse readdir results
144 static int parse_reply_info_dir(void **p, void *end,
145 struct ceph_mds_reply_info_parsed *info,
146 int features)
148 u32 num, i = 0;
149 int err;
151 info->dir_dir = *p;
152 if (*p + sizeof(*info->dir_dir) > end)
153 goto bad;
154 *p += sizeof(*info->dir_dir) +
155 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
156 if (*p > end)
157 goto bad;
159 ceph_decode_need(p, end, sizeof(num) + 2, bad);
160 num = ceph_decode_32(p);
161 info->dir_end = ceph_decode_8(p);
162 info->dir_complete = ceph_decode_8(p);
163 if (num == 0)
164 goto done;
166 /* alloc large array */
167 info->dir_nr = num;
168 info->dir_in = kcalloc(num, sizeof(*info->dir_in) +
169 sizeof(*info->dir_dname) +
170 sizeof(*info->dir_dname_len) +
171 sizeof(*info->dir_dlease),
172 GFP_NOFS);
173 if (info->dir_in == NULL) {
174 err = -ENOMEM;
175 goto out_bad;
177 info->dir_dname = (void *)(info->dir_in + num);
178 info->dir_dname_len = (void *)(info->dir_dname + num);
179 info->dir_dlease = (void *)(info->dir_dname_len + num);
181 while (num) {
182 /* dentry */
183 ceph_decode_need(p, end, sizeof(u32)*2, bad);
184 info->dir_dname_len[i] = ceph_decode_32(p);
185 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
186 info->dir_dname[i] = *p;
187 *p += info->dir_dname_len[i];
188 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
189 info->dir_dname[i]);
190 info->dir_dlease[i] = *p;
191 *p += sizeof(struct ceph_mds_reply_lease);
193 /* inode */
194 err = parse_reply_info_in(p, end, &info->dir_in[i], features);
195 if (err < 0)
196 goto out_bad;
197 i++;
198 num--;
201 done:
202 if (*p != end)
203 goto bad;
204 return 0;
206 bad:
207 err = -EIO;
208 out_bad:
209 pr_err("problem parsing dir contents %d\n", err);
210 return err;
214 * parse fcntl F_GETLK results
216 static int parse_reply_info_filelock(void **p, void *end,
217 struct ceph_mds_reply_info_parsed *info,
218 int features)
220 if (*p + sizeof(*info->filelock_reply) > end)
221 goto bad;
223 info->filelock_reply = *p;
224 *p += sizeof(*info->filelock_reply);
226 if (unlikely(*p != end))
227 goto bad;
228 return 0;
230 bad:
231 return -EIO;
235 * parse extra results
237 static int parse_reply_info_extra(void **p, void *end,
238 struct ceph_mds_reply_info_parsed *info,
239 int features)
241 if (info->head->op == CEPH_MDS_OP_GETFILELOCK)
242 return parse_reply_info_filelock(p, end, info, features);
243 else
244 return parse_reply_info_dir(p, end, info, features);
248 * parse entire mds reply
250 static int parse_reply_info(struct ceph_msg *msg,
251 struct ceph_mds_reply_info_parsed *info,
252 int features)
254 void *p, *end;
255 u32 len;
256 int err;
258 info->head = msg->front.iov_base;
259 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
260 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
262 /* trace */
263 ceph_decode_32_safe(&p, end, len, bad);
264 if (len > 0) {
265 err = parse_reply_info_trace(&p, p+len, info, features);
266 if (err < 0)
267 goto out_bad;
270 /* extra */
271 ceph_decode_32_safe(&p, end, len, bad);
272 if (len > 0) {
273 err = parse_reply_info_extra(&p, p+len, info, features);
274 if (err < 0)
275 goto out_bad;
278 /* snap blob */
279 ceph_decode_32_safe(&p, end, len, bad);
280 info->snapblob_len = len;
281 info->snapblob = p;
282 p += len;
284 if (p != end)
285 goto bad;
286 return 0;
288 bad:
289 err = -EIO;
290 out_bad:
291 pr_err("mds parse_reply err %d\n", err);
292 return err;
295 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
297 kfree(info->dir_in);
302 * sessions
304 static const char *session_state_name(int s)
306 switch (s) {
307 case CEPH_MDS_SESSION_NEW: return "new";
308 case CEPH_MDS_SESSION_OPENING: return "opening";
309 case CEPH_MDS_SESSION_OPEN: return "open";
310 case CEPH_MDS_SESSION_HUNG: return "hung";
311 case CEPH_MDS_SESSION_CLOSING: return "closing";
312 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
313 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
314 default: return "???";
318 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
320 if (atomic_inc_not_zero(&s->s_ref)) {
321 dout("mdsc get_session %p %d -> %d\n", s,
322 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
323 return s;
324 } else {
325 dout("mdsc get_session %p 0 -- FAIL", s);
326 return NULL;
330 void ceph_put_mds_session(struct ceph_mds_session *s)
332 dout("mdsc put_session %p %d -> %d\n", s,
333 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
334 if (atomic_dec_and_test(&s->s_ref)) {
335 if (s->s_authorizer)
336 s->s_mdsc->fsc->client->monc.auth->ops->destroy_authorizer(
337 s->s_mdsc->fsc->client->monc.auth,
338 s->s_authorizer);
339 kfree(s);
344 * called under mdsc->mutex
346 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
347 int mds)
349 struct ceph_mds_session *session;
351 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
352 return NULL;
353 session = mdsc->sessions[mds];
354 dout("lookup_mds_session %p %d\n", session,
355 atomic_read(&session->s_ref));
356 get_session(session);
357 return session;
360 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
362 if (mds >= mdsc->max_sessions)
363 return false;
364 return mdsc->sessions[mds];
367 static int __verify_registered_session(struct ceph_mds_client *mdsc,
368 struct ceph_mds_session *s)
370 if (s->s_mds >= mdsc->max_sessions ||
371 mdsc->sessions[s->s_mds] != s)
372 return -ENOENT;
373 return 0;
377 * create+register a new session for given mds.
378 * called under mdsc->mutex.
380 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
381 int mds)
383 struct ceph_mds_session *s;
385 s = kzalloc(sizeof(*s), GFP_NOFS);
386 if (!s)
387 return ERR_PTR(-ENOMEM);
388 s->s_mdsc = mdsc;
389 s->s_mds = mds;
390 s->s_state = CEPH_MDS_SESSION_NEW;
391 s->s_ttl = 0;
392 s->s_seq = 0;
393 mutex_init(&s->s_mutex);
395 ceph_con_init(mdsc->fsc->client->msgr, &s->s_con);
396 s->s_con.private = s;
397 s->s_con.ops = &mds_con_ops;
398 s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS;
399 s->s_con.peer_name.num = cpu_to_le64(mds);
401 spin_lock_init(&s->s_cap_lock);
402 s->s_cap_gen = 0;
403 s->s_cap_ttl = 0;
404 s->s_renew_requested = 0;
405 s->s_renew_seq = 0;
406 INIT_LIST_HEAD(&s->s_caps);
407 s->s_nr_caps = 0;
408 s->s_trim_caps = 0;
409 atomic_set(&s->s_ref, 1);
410 INIT_LIST_HEAD(&s->s_waiting);
411 INIT_LIST_HEAD(&s->s_unsafe);
412 s->s_num_cap_releases = 0;
413 s->s_cap_iterator = NULL;
414 INIT_LIST_HEAD(&s->s_cap_releases);
415 INIT_LIST_HEAD(&s->s_cap_releases_done);
416 INIT_LIST_HEAD(&s->s_cap_flushing);
417 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
419 dout("register_session mds%d\n", mds);
420 if (mds >= mdsc->max_sessions) {
421 int newmax = 1 << get_count_order(mds+1);
422 struct ceph_mds_session **sa;
424 dout("register_session realloc to %d\n", newmax);
425 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
426 if (sa == NULL)
427 goto fail_realloc;
428 if (mdsc->sessions) {
429 memcpy(sa, mdsc->sessions,
430 mdsc->max_sessions * sizeof(void *));
431 kfree(mdsc->sessions);
433 mdsc->sessions = sa;
434 mdsc->max_sessions = newmax;
436 mdsc->sessions[mds] = s;
437 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
439 ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
441 return s;
443 fail_realloc:
444 kfree(s);
445 return ERR_PTR(-ENOMEM);
449 * called under mdsc->mutex
451 static void __unregister_session(struct ceph_mds_client *mdsc,
452 struct ceph_mds_session *s)
454 dout("__unregister_session mds%d %p\n", s->s_mds, s);
455 BUG_ON(mdsc->sessions[s->s_mds] != s);
456 mdsc->sessions[s->s_mds] = NULL;
457 ceph_con_close(&s->s_con);
458 ceph_put_mds_session(s);
462 * drop session refs in request.
464 * should be last request ref, or hold mdsc->mutex
466 static void put_request_session(struct ceph_mds_request *req)
468 if (req->r_session) {
469 ceph_put_mds_session(req->r_session);
470 req->r_session = NULL;
474 void ceph_mdsc_release_request(struct kref *kref)
476 struct ceph_mds_request *req = container_of(kref,
477 struct ceph_mds_request,
478 r_kref);
479 if (req->r_request)
480 ceph_msg_put(req->r_request);
481 if (req->r_reply) {
482 ceph_msg_put(req->r_reply);
483 destroy_reply_info(&req->r_reply_info);
485 if (req->r_inode) {
486 ceph_put_cap_refs(ceph_inode(req->r_inode),
487 CEPH_CAP_PIN);
488 iput(req->r_inode);
490 if (req->r_locked_dir)
491 ceph_put_cap_refs(ceph_inode(req->r_locked_dir),
492 CEPH_CAP_PIN);
493 if (req->r_target_inode)
494 iput(req->r_target_inode);
495 if (req->r_dentry)
496 dput(req->r_dentry);
497 if (req->r_old_dentry) {
498 ceph_put_cap_refs(
499 ceph_inode(req->r_old_dentry->d_parent->d_inode),
500 CEPH_CAP_PIN);
501 dput(req->r_old_dentry);
503 kfree(req->r_path1);
504 kfree(req->r_path2);
505 put_request_session(req);
506 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
507 kfree(req);
511 * lookup session, bump ref if found.
513 * called under mdsc->mutex.
515 static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
516 u64 tid)
518 struct ceph_mds_request *req;
519 struct rb_node *n = mdsc->request_tree.rb_node;
521 while (n) {
522 req = rb_entry(n, struct ceph_mds_request, r_node);
523 if (tid < req->r_tid)
524 n = n->rb_left;
525 else if (tid > req->r_tid)
526 n = n->rb_right;
527 else {
528 ceph_mdsc_get_request(req);
529 return req;
532 return NULL;
535 static void __insert_request(struct ceph_mds_client *mdsc,
536 struct ceph_mds_request *new)
538 struct rb_node **p = &mdsc->request_tree.rb_node;
539 struct rb_node *parent = NULL;
540 struct ceph_mds_request *req = NULL;
542 while (*p) {
543 parent = *p;
544 req = rb_entry(parent, struct ceph_mds_request, r_node);
545 if (new->r_tid < req->r_tid)
546 p = &(*p)->rb_left;
547 else if (new->r_tid > req->r_tid)
548 p = &(*p)->rb_right;
549 else
550 BUG();
553 rb_link_node(&new->r_node, parent, p);
554 rb_insert_color(&new->r_node, &mdsc->request_tree);
558 * Register an in-flight request, and assign a tid. Link to directory
559 * are modifying (if any).
561 * Called under mdsc->mutex.
563 static void __register_request(struct ceph_mds_client *mdsc,
564 struct ceph_mds_request *req,
565 struct inode *dir)
567 req->r_tid = ++mdsc->last_tid;
568 if (req->r_num_caps)
569 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
570 req->r_num_caps);
571 dout("__register_request %p tid %lld\n", req, req->r_tid);
572 ceph_mdsc_get_request(req);
573 __insert_request(mdsc, req);
575 req->r_uid = current_fsuid();
576 req->r_gid = current_fsgid();
578 if (dir) {
579 struct ceph_inode_info *ci = ceph_inode(dir);
581 spin_lock(&ci->i_unsafe_lock);
582 req->r_unsafe_dir = dir;
583 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
584 spin_unlock(&ci->i_unsafe_lock);
588 static void __unregister_request(struct ceph_mds_client *mdsc,
589 struct ceph_mds_request *req)
591 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
592 rb_erase(&req->r_node, &mdsc->request_tree);
593 RB_CLEAR_NODE(&req->r_node);
595 if (req->r_unsafe_dir) {
596 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
598 spin_lock(&ci->i_unsafe_lock);
599 list_del_init(&req->r_unsafe_dir_item);
600 spin_unlock(&ci->i_unsafe_lock);
603 ceph_mdsc_put_request(req);
607 * Choose mds to send request to next. If there is a hint set in the
608 * request (e.g., due to a prior forward hint from the mds), use that.
609 * Otherwise, consult frag tree and/or caps to identify the
610 * appropriate mds. If all else fails, choose randomly.
612 * Called under mdsc->mutex.
614 struct dentry *get_nonsnap_parent(struct dentry *dentry)
616 while (!IS_ROOT(dentry) && ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
617 dentry = dentry->d_parent;
618 return dentry;
621 static int __choose_mds(struct ceph_mds_client *mdsc,
622 struct ceph_mds_request *req)
624 struct inode *inode;
625 struct ceph_inode_info *ci;
626 struct ceph_cap *cap;
627 int mode = req->r_direct_mode;
628 int mds = -1;
629 u32 hash = req->r_direct_hash;
630 bool is_hash = req->r_direct_is_hash;
633 * is there a specific mds we should try? ignore hint if we have
634 * no session and the mds is not up (active or recovering).
636 if (req->r_resend_mds >= 0 &&
637 (__have_session(mdsc, req->r_resend_mds) ||
638 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
639 dout("choose_mds using resend_mds mds%d\n",
640 req->r_resend_mds);
641 return req->r_resend_mds;
644 if (mode == USE_RANDOM_MDS)
645 goto random;
647 inode = NULL;
648 if (req->r_inode) {
649 inode = req->r_inode;
650 } else if (req->r_dentry) {
651 struct inode *dir = req->r_dentry->d_parent->d_inode;
653 if (dir->i_sb != mdsc->fsc->sb) {
654 /* not this fs! */
655 inode = req->r_dentry->d_inode;
656 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
657 /* direct snapped/virtual snapdir requests
658 * based on parent dir inode */
659 struct dentry *dn =
660 get_nonsnap_parent(req->r_dentry->d_parent);
661 inode = dn->d_inode;
662 dout("__choose_mds using nonsnap parent %p\n", inode);
663 } else if (req->r_dentry->d_inode) {
664 /* dentry target */
665 inode = req->r_dentry->d_inode;
666 } else {
667 /* dir + name */
668 inode = dir;
669 hash = ceph_dentry_hash(req->r_dentry);
670 is_hash = true;
674 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
675 (int)hash, mode);
676 if (!inode)
677 goto random;
678 ci = ceph_inode(inode);
680 if (is_hash && S_ISDIR(inode->i_mode)) {
681 struct ceph_inode_frag frag;
682 int found;
684 ceph_choose_frag(ci, hash, &frag, &found);
685 if (found) {
686 if (mode == USE_ANY_MDS && frag.ndist > 0) {
687 u8 r;
689 /* choose a random replica */
690 get_random_bytes(&r, 1);
691 r %= frag.ndist;
692 mds = frag.dist[r];
693 dout("choose_mds %p %llx.%llx "
694 "frag %u mds%d (%d/%d)\n",
695 inode, ceph_vinop(inode),
696 frag.frag, frag.mds,
697 (int)r, frag.ndist);
698 return mds;
701 /* since this file/dir wasn't known to be
702 * replicated, then we want to look for the
703 * authoritative mds. */
704 mode = USE_AUTH_MDS;
705 if (frag.mds >= 0) {
706 /* choose auth mds */
707 mds = frag.mds;
708 dout("choose_mds %p %llx.%llx "
709 "frag %u mds%d (auth)\n",
710 inode, ceph_vinop(inode), frag.frag, mds);
711 return mds;
716 spin_lock(&inode->i_lock);
717 cap = NULL;
718 if (mode == USE_AUTH_MDS)
719 cap = ci->i_auth_cap;
720 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
721 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
722 if (!cap) {
723 spin_unlock(&inode->i_lock);
724 goto random;
726 mds = cap->session->s_mds;
727 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
728 inode, ceph_vinop(inode), mds,
729 cap == ci->i_auth_cap ? "auth " : "", cap);
730 spin_unlock(&inode->i_lock);
731 return mds;
733 random:
734 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
735 dout("choose_mds chose random mds%d\n", mds);
736 return mds;
741 * session messages
743 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
745 struct ceph_msg *msg;
746 struct ceph_mds_session_head *h;
748 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS);
749 if (!msg) {
750 pr_err("create_session_msg ENOMEM creating msg\n");
751 return NULL;
753 h = msg->front.iov_base;
754 h->op = cpu_to_le32(op);
755 h->seq = cpu_to_le64(seq);
756 return msg;
760 * send session open request.
762 * called under mdsc->mutex
764 static int __open_session(struct ceph_mds_client *mdsc,
765 struct ceph_mds_session *session)
767 struct ceph_msg *msg;
768 int mstate;
769 int mds = session->s_mds;
771 /* wait for mds to go active? */
772 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
773 dout("open_session to mds%d (%s)\n", mds,
774 ceph_mds_state_name(mstate));
775 session->s_state = CEPH_MDS_SESSION_OPENING;
776 session->s_renew_requested = jiffies;
778 /* send connect message */
779 msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq);
780 if (!msg)
781 return -ENOMEM;
782 ceph_con_send(&session->s_con, msg);
783 return 0;
787 * open sessions for any export targets for the given mds
789 * called under mdsc->mutex
791 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
792 struct ceph_mds_session *session)
794 struct ceph_mds_info *mi;
795 struct ceph_mds_session *ts;
796 int i, mds = session->s_mds;
797 int target;
799 if (mds >= mdsc->mdsmap->m_max_mds)
800 return;
801 mi = &mdsc->mdsmap->m_info[mds];
802 dout("open_export_target_sessions for mds%d (%d targets)\n",
803 session->s_mds, mi->num_export_targets);
805 for (i = 0; i < mi->num_export_targets; i++) {
806 target = mi->export_targets[i];
807 ts = __ceph_lookup_mds_session(mdsc, target);
808 if (!ts) {
809 ts = register_session(mdsc, target);
810 if (IS_ERR(ts))
811 return;
813 if (session->s_state == CEPH_MDS_SESSION_NEW ||
814 session->s_state == CEPH_MDS_SESSION_CLOSING)
815 __open_session(mdsc, session);
816 else
817 dout(" mds%d target mds%d %p is %s\n", session->s_mds,
818 i, ts, session_state_name(ts->s_state));
819 ceph_put_mds_session(ts);
823 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
824 struct ceph_mds_session *session)
826 mutex_lock(&mdsc->mutex);
827 __open_export_target_sessions(mdsc, session);
828 mutex_unlock(&mdsc->mutex);
832 * session caps
836 * Free preallocated cap messages assigned to this session
838 static void cleanup_cap_releases(struct ceph_mds_session *session)
840 struct ceph_msg *msg;
842 spin_lock(&session->s_cap_lock);
843 while (!list_empty(&session->s_cap_releases)) {
844 msg = list_first_entry(&session->s_cap_releases,
845 struct ceph_msg, list_head);
846 list_del_init(&msg->list_head);
847 ceph_msg_put(msg);
849 while (!list_empty(&session->s_cap_releases_done)) {
850 msg = list_first_entry(&session->s_cap_releases_done,
851 struct ceph_msg, list_head);
852 list_del_init(&msg->list_head);
853 ceph_msg_put(msg);
855 spin_unlock(&session->s_cap_lock);
859 * Helper to safely iterate over all caps associated with a session, with
860 * special care taken to handle a racing __ceph_remove_cap().
862 * Caller must hold session s_mutex.
864 static int iterate_session_caps(struct ceph_mds_session *session,
865 int (*cb)(struct inode *, struct ceph_cap *,
866 void *), void *arg)
868 struct list_head *p;
869 struct ceph_cap *cap;
870 struct inode *inode, *last_inode = NULL;
871 struct ceph_cap *old_cap = NULL;
872 int ret;
874 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
875 spin_lock(&session->s_cap_lock);
876 p = session->s_caps.next;
877 while (p != &session->s_caps) {
878 cap = list_entry(p, struct ceph_cap, session_caps);
879 inode = igrab(&cap->ci->vfs_inode);
880 if (!inode) {
881 p = p->next;
882 continue;
884 session->s_cap_iterator = cap;
885 spin_unlock(&session->s_cap_lock);
887 if (last_inode) {
888 iput(last_inode);
889 last_inode = NULL;
891 if (old_cap) {
892 ceph_put_cap(session->s_mdsc, old_cap);
893 old_cap = NULL;
896 ret = cb(inode, cap, arg);
897 last_inode = inode;
899 spin_lock(&session->s_cap_lock);
900 p = p->next;
901 if (cap->ci == NULL) {
902 dout("iterate_session_caps finishing cap %p removal\n",
903 cap);
904 BUG_ON(cap->session != session);
905 list_del_init(&cap->session_caps);
906 session->s_nr_caps--;
907 cap->session = NULL;
908 old_cap = cap; /* put_cap it w/o locks held */
910 if (ret < 0)
911 goto out;
913 ret = 0;
914 out:
915 session->s_cap_iterator = NULL;
916 spin_unlock(&session->s_cap_lock);
918 if (last_inode)
919 iput(last_inode);
920 if (old_cap)
921 ceph_put_cap(session->s_mdsc, old_cap);
923 return ret;
926 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
927 void *arg)
929 struct ceph_inode_info *ci = ceph_inode(inode);
930 int drop = 0;
932 dout("removing cap %p, ci is %p, inode is %p\n",
933 cap, ci, &ci->vfs_inode);
934 spin_lock(&inode->i_lock);
935 __ceph_remove_cap(cap);
936 if (!__ceph_is_any_real_caps(ci)) {
937 struct ceph_mds_client *mdsc =
938 ceph_sb_to_client(inode->i_sb)->mdsc;
940 spin_lock(&mdsc->cap_dirty_lock);
941 if (!list_empty(&ci->i_dirty_item)) {
942 pr_info(" dropping dirty %s state for %p %lld\n",
943 ceph_cap_string(ci->i_dirty_caps),
944 inode, ceph_ino(inode));
945 ci->i_dirty_caps = 0;
946 list_del_init(&ci->i_dirty_item);
947 drop = 1;
949 if (!list_empty(&ci->i_flushing_item)) {
950 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
951 ceph_cap_string(ci->i_flushing_caps),
952 inode, ceph_ino(inode));
953 ci->i_flushing_caps = 0;
954 list_del_init(&ci->i_flushing_item);
955 mdsc->num_cap_flushing--;
956 drop = 1;
958 if (drop && ci->i_wrbuffer_ref) {
959 pr_info(" dropping dirty data for %p %lld\n",
960 inode, ceph_ino(inode));
961 ci->i_wrbuffer_ref = 0;
962 ci->i_wrbuffer_ref_head = 0;
963 drop++;
965 spin_unlock(&mdsc->cap_dirty_lock);
967 spin_unlock(&inode->i_lock);
968 while (drop--)
969 iput(inode);
970 return 0;
974 * caller must hold session s_mutex
976 static void remove_session_caps(struct ceph_mds_session *session)
978 dout("remove_session_caps on %p\n", session);
979 iterate_session_caps(session, remove_session_caps_cb, NULL);
980 BUG_ON(session->s_nr_caps > 0);
981 BUG_ON(!list_empty(&session->s_cap_flushing));
982 cleanup_cap_releases(session);
986 * wake up any threads waiting on this session's caps. if the cap is
987 * old (didn't get renewed on the client reconnect), remove it now.
989 * caller must hold s_mutex.
991 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
992 void *arg)
994 struct ceph_inode_info *ci = ceph_inode(inode);
996 wake_up_all(&ci->i_cap_wq);
997 if (arg) {
998 spin_lock(&inode->i_lock);
999 ci->i_wanted_max_size = 0;
1000 ci->i_requested_max_size = 0;
1001 spin_unlock(&inode->i_lock);
1003 return 0;
1006 static void wake_up_session_caps(struct ceph_mds_session *session,
1007 int reconnect)
1009 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1010 iterate_session_caps(session, wake_up_session_cb,
1011 (void *)(unsigned long)reconnect);
1015 * Send periodic message to MDS renewing all currently held caps. The
1016 * ack will reset the expiration for all caps from this session.
1018 * caller holds s_mutex
1020 static int send_renew_caps(struct ceph_mds_client *mdsc,
1021 struct ceph_mds_session *session)
1023 struct ceph_msg *msg;
1024 int state;
1026 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1027 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1028 pr_info("mds%d caps stale\n", session->s_mds);
1029 session->s_renew_requested = jiffies;
1031 /* do not try to renew caps until a recovering mds has reconnected
1032 * with its clients. */
1033 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1034 if (state < CEPH_MDS_STATE_RECONNECT) {
1035 dout("send_renew_caps ignoring mds%d (%s)\n",
1036 session->s_mds, ceph_mds_state_name(state));
1037 return 0;
1040 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1041 ceph_mds_state_name(state));
1042 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1043 ++session->s_renew_seq);
1044 if (!msg)
1045 return -ENOMEM;
1046 ceph_con_send(&session->s_con, msg);
1047 return 0;
1051 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1053 * Called under session->s_mutex
1055 static void renewed_caps(struct ceph_mds_client *mdsc,
1056 struct ceph_mds_session *session, int is_renew)
1058 int was_stale;
1059 int wake = 0;
1061 spin_lock(&session->s_cap_lock);
1062 was_stale = is_renew && (session->s_cap_ttl == 0 ||
1063 time_after_eq(jiffies, session->s_cap_ttl));
1065 session->s_cap_ttl = session->s_renew_requested +
1066 mdsc->mdsmap->m_session_timeout*HZ;
1068 if (was_stale) {
1069 if (time_before(jiffies, session->s_cap_ttl)) {
1070 pr_info("mds%d caps renewed\n", session->s_mds);
1071 wake = 1;
1072 } else {
1073 pr_info("mds%d caps still stale\n", session->s_mds);
1076 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1077 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1078 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1079 spin_unlock(&session->s_cap_lock);
1081 if (wake)
1082 wake_up_session_caps(session, 0);
1086 * send a session close request
1088 static int request_close_session(struct ceph_mds_client *mdsc,
1089 struct ceph_mds_session *session)
1091 struct ceph_msg *msg;
1093 dout("request_close_session mds%d state %s seq %lld\n",
1094 session->s_mds, session_state_name(session->s_state),
1095 session->s_seq);
1096 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1097 if (!msg)
1098 return -ENOMEM;
1099 ceph_con_send(&session->s_con, msg);
1100 return 0;
1104 * Called with s_mutex held.
1106 static int __close_session(struct ceph_mds_client *mdsc,
1107 struct ceph_mds_session *session)
1109 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1110 return 0;
1111 session->s_state = CEPH_MDS_SESSION_CLOSING;
1112 return request_close_session(mdsc, session);
1116 * Trim old(er) caps.
1118 * Because we can't cache an inode without one or more caps, we do
1119 * this indirectly: if a cap is unused, we prune its aliases, at which
1120 * point the inode will hopefully get dropped to.
1122 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1123 * memory pressure from the MDS, though, so it needn't be perfect.
1125 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1127 struct ceph_mds_session *session = arg;
1128 struct ceph_inode_info *ci = ceph_inode(inode);
1129 int used, oissued, mine;
1131 if (session->s_trim_caps <= 0)
1132 return -1;
1134 spin_lock(&inode->i_lock);
1135 mine = cap->issued | cap->implemented;
1136 used = __ceph_caps_used(ci);
1137 oissued = __ceph_caps_issued_other(ci, cap);
1139 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n",
1140 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1141 ceph_cap_string(used));
1142 if (ci->i_dirty_caps)
1143 goto out; /* dirty caps */
1144 if ((used & ~oissued) & mine)
1145 goto out; /* we need these caps */
1147 session->s_trim_caps--;
1148 if (oissued) {
1149 /* we aren't the only cap.. just remove us */
1150 __ceph_remove_cap(cap);
1151 } else {
1152 /* try to drop referring dentries */
1153 spin_unlock(&inode->i_lock);
1154 d_prune_aliases(inode);
1155 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1156 inode, cap, atomic_read(&inode->i_count));
1157 return 0;
1160 out:
1161 spin_unlock(&inode->i_lock);
1162 return 0;
1166 * Trim session cap count down to some max number.
1168 static int trim_caps(struct ceph_mds_client *mdsc,
1169 struct ceph_mds_session *session,
1170 int max_caps)
1172 int trim_caps = session->s_nr_caps - max_caps;
1174 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1175 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1176 if (trim_caps > 0) {
1177 session->s_trim_caps = trim_caps;
1178 iterate_session_caps(session, trim_caps_cb, session);
1179 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1180 session->s_mds, session->s_nr_caps, max_caps,
1181 trim_caps - session->s_trim_caps);
1182 session->s_trim_caps = 0;
1184 return 0;
1188 * Allocate cap_release messages. If there is a partially full message
1189 * in the queue, try to allocate enough to cover it's remainder, so that
1190 * we can send it immediately.
1192 * Called under s_mutex.
1194 int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
1195 struct ceph_mds_session *session)
1197 struct ceph_msg *msg, *partial = NULL;
1198 struct ceph_mds_cap_release *head;
1199 int err = -ENOMEM;
1200 int extra = mdsc->fsc->mount_options->cap_release_safety;
1201 int num;
1203 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1204 extra);
1206 spin_lock(&session->s_cap_lock);
1208 if (!list_empty(&session->s_cap_releases)) {
1209 msg = list_first_entry(&session->s_cap_releases,
1210 struct ceph_msg,
1211 list_head);
1212 head = msg->front.iov_base;
1213 num = le32_to_cpu(head->num);
1214 if (num) {
1215 dout(" partial %p with (%d/%d)\n", msg, num,
1216 (int)CEPH_CAPS_PER_RELEASE);
1217 extra += CEPH_CAPS_PER_RELEASE - num;
1218 partial = msg;
1221 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1222 spin_unlock(&session->s_cap_lock);
1223 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1224 GFP_NOFS);
1225 if (!msg)
1226 goto out_unlocked;
1227 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1228 (int)msg->front.iov_len);
1229 head = msg->front.iov_base;
1230 head->num = cpu_to_le32(0);
1231 msg->front.iov_len = sizeof(*head);
1232 spin_lock(&session->s_cap_lock);
1233 list_add(&msg->list_head, &session->s_cap_releases);
1234 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1237 if (partial) {
1238 head = partial->front.iov_base;
1239 num = le32_to_cpu(head->num);
1240 dout(" queueing partial %p with %d/%d\n", partial, num,
1241 (int)CEPH_CAPS_PER_RELEASE);
1242 list_move_tail(&partial->list_head,
1243 &session->s_cap_releases_done);
1244 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
1246 err = 0;
1247 spin_unlock(&session->s_cap_lock);
1248 out_unlocked:
1249 return err;
1253 * flush all dirty inode data to disk.
1255 * returns true if we've flushed through want_flush_seq
1257 static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1259 int mds, ret = 1;
1261 dout("check_cap_flush want %lld\n", want_flush_seq);
1262 mutex_lock(&mdsc->mutex);
1263 for (mds = 0; ret && mds < mdsc->max_sessions; mds++) {
1264 struct ceph_mds_session *session = mdsc->sessions[mds];
1266 if (!session)
1267 continue;
1268 get_session(session);
1269 mutex_unlock(&mdsc->mutex);
1271 mutex_lock(&session->s_mutex);
1272 if (!list_empty(&session->s_cap_flushing)) {
1273 struct ceph_inode_info *ci =
1274 list_entry(session->s_cap_flushing.next,
1275 struct ceph_inode_info,
1276 i_flushing_item);
1277 struct inode *inode = &ci->vfs_inode;
1279 spin_lock(&inode->i_lock);
1280 if (ci->i_cap_flush_seq <= want_flush_seq) {
1281 dout("check_cap_flush still flushing %p "
1282 "seq %lld <= %lld to mds%d\n", inode,
1283 ci->i_cap_flush_seq, want_flush_seq,
1284 session->s_mds);
1285 ret = 0;
1287 spin_unlock(&inode->i_lock);
1289 mutex_unlock(&session->s_mutex);
1290 ceph_put_mds_session(session);
1292 if (!ret)
1293 return ret;
1294 mutex_lock(&mdsc->mutex);
1297 mutex_unlock(&mdsc->mutex);
1298 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1299 return ret;
1303 * called under s_mutex
1305 void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1306 struct ceph_mds_session *session)
1308 struct ceph_msg *msg;
1310 dout("send_cap_releases mds%d\n", session->s_mds);
1311 spin_lock(&session->s_cap_lock);
1312 while (!list_empty(&session->s_cap_releases_done)) {
1313 msg = list_first_entry(&session->s_cap_releases_done,
1314 struct ceph_msg, list_head);
1315 list_del_init(&msg->list_head);
1316 spin_unlock(&session->s_cap_lock);
1317 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1318 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1319 ceph_con_send(&session->s_con, msg);
1320 spin_lock(&session->s_cap_lock);
1322 spin_unlock(&session->s_cap_lock);
1325 static void discard_cap_releases(struct ceph_mds_client *mdsc,
1326 struct ceph_mds_session *session)
1328 struct ceph_msg *msg;
1329 struct ceph_mds_cap_release *head;
1330 unsigned num;
1332 dout("discard_cap_releases mds%d\n", session->s_mds);
1333 spin_lock(&session->s_cap_lock);
1335 /* zero out the in-progress message */
1336 msg = list_first_entry(&session->s_cap_releases,
1337 struct ceph_msg, list_head);
1338 head = msg->front.iov_base;
1339 num = le32_to_cpu(head->num);
1340 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg, num);
1341 head->num = cpu_to_le32(0);
1342 session->s_num_cap_releases += num;
1344 /* requeue completed messages */
1345 while (!list_empty(&session->s_cap_releases_done)) {
1346 msg = list_first_entry(&session->s_cap_releases_done,
1347 struct ceph_msg, list_head);
1348 list_del_init(&msg->list_head);
1350 head = msg->front.iov_base;
1351 num = le32_to_cpu(head->num);
1352 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1353 num);
1354 session->s_num_cap_releases += num;
1355 head->num = cpu_to_le32(0);
1356 msg->front.iov_len = sizeof(*head);
1357 list_add(&msg->list_head, &session->s_cap_releases);
1360 spin_unlock(&session->s_cap_lock);
1364 * requests
1368 * Create an mds request.
1370 struct ceph_mds_request *
1371 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1373 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1375 if (!req)
1376 return ERR_PTR(-ENOMEM);
1378 mutex_init(&req->r_fill_mutex);
1379 req->r_mdsc = mdsc;
1380 req->r_started = jiffies;
1381 req->r_resend_mds = -1;
1382 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1383 req->r_fmode = -1;
1384 kref_init(&req->r_kref);
1385 INIT_LIST_HEAD(&req->r_wait);
1386 init_completion(&req->r_completion);
1387 init_completion(&req->r_safe_completion);
1388 INIT_LIST_HEAD(&req->r_unsafe_item);
1390 req->r_op = op;
1391 req->r_direct_mode = mode;
1392 return req;
1396 * return oldest (lowest) request, tid in request tree, 0 if none.
1398 * called under mdsc->mutex.
1400 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1402 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1403 return NULL;
1404 return rb_entry(rb_first(&mdsc->request_tree),
1405 struct ceph_mds_request, r_node);
1408 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1410 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1412 if (req)
1413 return req->r_tid;
1414 return 0;
1418 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1419 * on build_path_from_dentry in fs/cifs/dir.c.
1421 * If @stop_on_nosnap, generate path relative to the first non-snapped
1422 * inode.
1424 * Encode hidden .snap dirs as a double /, i.e.
1425 * foo/.snap/bar -> foo//bar
1427 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1428 int stop_on_nosnap)
1430 struct dentry *temp;
1431 char *path;
1432 int len, pos;
1434 if (dentry == NULL)
1435 return ERR_PTR(-EINVAL);
1437 retry:
1438 len = 0;
1439 for (temp = dentry; !IS_ROOT(temp);) {
1440 struct inode *inode = temp->d_inode;
1441 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1442 len++; /* slash only */
1443 else if (stop_on_nosnap && inode &&
1444 ceph_snap(inode) == CEPH_NOSNAP)
1445 break;
1446 else
1447 len += 1 + temp->d_name.len;
1448 temp = temp->d_parent;
1449 if (temp == NULL) {
1450 pr_err("build_path corrupt dentry %p\n", dentry);
1451 return ERR_PTR(-EINVAL);
1454 if (len)
1455 len--; /* no leading '/' */
1457 path = kmalloc(len+1, GFP_NOFS);
1458 if (path == NULL)
1459 return ERR_PTR(-ENOMEM);
1460 pos = len;
1461 path[pos] = 0; /* trailing null */
1462 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1463 struct inode *inode = temp->d_inode;
1465 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1466 dout("build_path path+%d: %p SNAPDIR\n",
1467 pos, temp);
1468 } else if (stop_on_nosnap && inode &&
1469 ceph_snap(inode) == CEPH_NOSNAP) {
1470 break;
1471 } else {
1472 pos -= temp->d_name.len;
1473 if (pos < 0)
1474 break;
1475 strncpy(path + pos, temp->d_name.name,
1476 temp->d_name.len);
1478 if (pos)
1479 path[--pos] = '/';
1480 temp = temp->d_parent;
1481 if (temp == NULL) {
1482 pr_err("build_path corrupt dentry\n");
1483 kfree(path);
1484 return ERR_PTR(-EINVAL);
1487 if (pos != 0) {
1488 pr_err("build_path did not end path lookup where "
1489 "expected, namelen is %d, pos is %d\n", len, pos);
1490 /* presumably this is only possible if racing with a
1491 rename of one of the parent directories (we can not
1492 lock the dentries above us to prevent this, but
1493 retrying should be harmless) */
1494 kfree(path);
1495 goto retry;
1498 *base = ceph_ino(temp->d_inode);
1499 *plen = len;
1500 dout("build_path on %p %d built %llx '%.*s'\n",
1501 dentry, dentry->d_count, *base, len, path);
1502 return path;
1505 static int build_dentry_path(struct dentry *dentry,
1506 const char **ppath, int *ppathlen, u64 *pino,
1507 int *pfreepath)
1509 char *path;
1511 if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) {
1512 *pino = ceph_ino(dentry->d_parent->d_inode);
1513 *ppath = dentry->d_name.name;
1514 *ppathlen = dentry->d_name.len;
1515 return 0;
1517 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1518 if (IS_ERR(path))
1519 return PTR_ERR(path);
1520 *ppath = path;
1521 *pfreepath = 1;
1522 return 0;
1525 static int build_inode_path(struct inode *inode,
1526 const char **ppath, int *ppathlen, u64 *pino,
1527 int *pfreepath)
1529 struct dentry *dentry;
1530 char *path;
1532 if (ceph_snap(inode) == CEPH_NOSNAP) {
1533 *pino = ceph_ino(inode);
1534 *ppathlen = 0;
1535 return 0;
1537 dentry = d_find_alias(inode);
1538 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1539 dput(dentry);
1540 if (IS_ERR(path))
1541 return PTR_ERR(path);
1542 *ppath = path;
1543 *pfreepath = 1;
1544 return 0;
1548 * request arguments may be specified via an inode *, a dentry *, or
1549 * an explicit ino+path.
1551 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1552 const char *rpath, u64 rino,
1553 const char **ppath, int *pathlen,
1554 u64 *ino, int *freepath)
1556 int r = 0;
1558 if (rinode) {
1559 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1560 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1561 ceph_snap(rinode));
1562 } else if (rdentry) {
1563 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1564 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1565 *ppath);
1566 } else if (rpath) {
1567 *ino = rino;
1568 *ppath = rpath;
1569 *pathlen = strlen(rpath);
1570 dout(" path %.*s\n", *pathlen, rpath);
1573 return r;
1577 * called under mdsc->mutex
1579 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1580 struct ceph_mds_request *req,
1581 int mds)
1583 struct ceph_msg *msg;
1584 struct ceph_mds_request_head *head;
1585 const char *path1 = NULL;
1586 const char *path2 = NULL;
1587 u64 ino1 = 0, ino2 = 0;
1588 int pathlen1 = 0, pathlen2 = 0;
1589 int freepath1 = 0, freepath2 = 0;
1590 int len;
1591 u16 releases;
1592 void *p, *end;
1593 int ret;
1595 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1596 req->r_path1, req->r_ino1.ino,
1597 &path1, &pathlen1, &ino1, &freepath1);
1598 if (ret < 0) {
1599 msg = ERR_PTR(ret);
1600 goto out;
1603 ret = set_request_path_attr(NULL, req->r_old_dentry,
1604 req->r_path2, req->r_ino2.ino,
1605 &path2, &pathlen2, &ino2, &freepath2);
1606 if (ret < 0) {
1607 msg = ERR_PTR(ret);
1608 goto out_free1;
1611 len = sizeof(*head) +
1612 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64));
1614 /* calculate (max) length for cap releases */
1615 len += sizeof(struct ceph_mds_request_release) *
1616 (!!req->r_inode_drop + !!req->r_dentry_drop +
1617 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1618 if (req->r_dentry_drop)
1619 len += req->r_dentry->d_name.len;
1620 if (req->r_old_dentry_drop)
1621 len += req->r_old_dentry->d_name.len;
1623 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS);
1624 if (!msg) {
1625 msg = ERR_PTR(-ENOMEM);
1626 goto out_free2;
1629 msg->hdr.tid = cpu_to_le64(req->r_tid);
1631 head = msg->front.iov_base;
1632 p = msg->front.iov_base + sizeof(*head);
1633 end = msg->front.iov_base + msg->front.iov_len;
1635 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1636 head->op = cpu_to_le32(req->r_op);
1637 head->caller_uid = cpu_to_le32(req->r_uid);
1638 head->caller_gid = cpu_to_le32(req->r_gid);
1639 head->args = req->r_args;
1641 ceph_encode_filepath(&p, end, ino1, path1);
1642 ceph_encode_filepath(&p, end, ino2, path2);
1644 /* make note of release offset, in case we need to replay */
1645 req->r_request_release_offset = p - msg->front.iov_base;
1647 /* cap releases */
1648 releases = 0;
1649 if (req->r_inode_drop)
1650 releases += ceph_encode_inode_release(&p,
1651 req->r_inode ? req->r_inode : req->r_dentry->d_inode,
1652 mds, req->r_inode_drop, req->r_inode_unless, 0);
1653 if (req->r_dentry_drop)
1654 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1655 mds, req->r_dentry_drop, req->r_dentry_unless);
1656 if (req->r_old_dentry_drop)
1657 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1658 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1659 if (req->r_old_inode_drop)
1660 releases += ceph_encode_inode_release(&p,
1661 req->r_old_dentry->d_inode,
1662 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1663 head->num_releases = cpu_to_le16(releases);
1665 BUG_ON(p > end);
1666 msg->front.iov_len = p - msg->front.iov_base;
1667 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1669 msg->pages = req->r_pages;
1670 msg->nr_pages = req->r_num_pages;
1671 msg->hdr.data_len = cpu_to_le32(req->r_data_len);
1672 msg->hdr.data_off = cpu_to_le16(0);
1674 out_free2:
1675 if (freepath2)
1676 kfree((char *)path2);
1677 out_free1:
1678 if (freepath1)
1679 kfree((char *)path1);
1680 out:
1681 return msg;
1685 * called under mdsc->mutex if error, under no mutex if
1686 * success.
1688 static void complete_request(struct ceph_mds_client *mdsc,
1689 struct ceph_mds_request *req)
1691 if (req->r_callback)
1692 req->r_callback(mdsc, req);
1693 else
1694 complete_all(&req->r_completion);
1698 * called under mdsc->mutex
1700 static int __prepare_send_request(struct ceph_mds_client *mdsc,
1701 struct ceph_mds_request *req,
1702 int mds)
1704 struct ceph_mds_request_head *rhead;
1705 struct ceph_msg *msg;
1706 int flags = 0;
1708 req->r_attempts++;
1709 if (req->r_inode) {
1710 struct ceph_cap *cap =
1711 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
1713 if (cap)
1714 req->r_sent_on_mseq = cap->mseq;
1715 else
1716 req->r_sent_on_mseq = -1;
1718 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
1719 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
1721 if (req->r_got_unsafe) {
1723 * Replay. Do not regenerate message (and rebuild
1724 * paths, etc.); just use the original message.
1725 * Rebuilding paths will break for renames because
1726 * d_move mangles the src name.
1728 msg = req->r_request;
1729 rhead = msg->front.iov_base;
1731 flags = le32_to_cpu(rhead->flags);
1732 flags |= CEPH_MDS_FLAG_REPLAY;
1733 rhead->flags = cpu_to_le32(flags);
1735 if (req->r_target_inode)
1736 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
1738 rhead->num_retry = req->r_attempts - 1;
1740 /* remove cap/dentry releases from message */
1741 rhead->num_releases = 0;
1742 msg->hdr.front_len = cpu_to_le32(req->r_request_release_offset);
1743 msg->front.iov_len = req->r_request_release_offset;
1744 return 0;
1747 if (req->r_request) {
1748 ceph_msg_put(req->r_request);
1749 req->r_request = NULL;
1751 msg = create_request_message(mdsc, req, mds);
1752 if (IS_ERR(msg)) {
1753 req->r_err = PTR_ERR(msg);
1754 complete_request(mdsc, req);
1755 return PTR_ERR(msg);
1757 req->r_request = msg;
1759 rhead = msg->front.iov_base;
1760 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
1761 if (req->r_got_unsafe)
1762 flags |= CEPH_MDS_FLAG_REPLAY;
1763 if (req->r_locked_dir)
1764 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
1765 rhead->flags = cpu_to_le32(flags);
1766 rhead->num_fwd = req->r_num_fwd;
1767 rhead->num_retry = req->r_attempts - 1;
1768 rhead->ino = 0;
1770 dout(" r_locked_dir = %p\n", req->r_locked_dir);
1771 return 0;
1775 * send request, or put it on the appropriate wait list.
1777 static int __do_request(struct ceph_mds_client *mdsc,
1778 struct ceph_mds_request *req)
1780 struct ceph_mds_session *session = NULL;
1781 int mds = -1;
1782 int err = -EAGAIN;
1784 if (req->r_err || req->r_got_result)
1785 goto out;
1787 if (req->r_timeout &&
1788 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
1789 dout("do_request timed out\n");
1790 err = -EIO;
1791 goto finish;
1794 put_request_session(req);
1796 mds = __choose_mds(mdsc, req);
1797 if (mds < 0 ||
1798 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
1799 dout("do_request no mds or not active, waiting for map\n");
1800 list_add(&req->r_wait, &mdsc->waiting_for_map);
1801 goto out;
1804 /* get, open session */
1805 session = __ceph_lookup_mds_session(mdsc, mds);
1806 if (!session) {
1807 session = register_session(mdsc, mds);
1808 if (IS_ERR(session)) {
1809 err = PTR_ERR(session);
1810 goto finish;
1813 req->r_session = get_session(session);
1815 dout("do_request mds%d session %p state %s\n", mds, session,
1816 session_state_name(session->s_state));
1817 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
1818 session->s_state != CEPH_MDS_SESSION_HUNG) {
1819 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1820 session->s_state == CEPH_MDS_SESSION_CLOSING)
1821 __open_session(mdsc, session);
1822 list_add(&req->r_wait, &session->s_waiting);
1823 goto out_session;
1826 /* send request */
1827 req->r_resend_mds = -1; /* forget any previous mds hint */
1829 if (req->r_request_started == 0) /* note request start time */
1830 req->r_request_started = jiffies;
1832 err = __prepare_send_request(mdsc, req, mds);
1833 if (!err) {
1834 ceph_msg_get(req->r_request);
1835 ceph_con_send(&session->s_con, req->r_request);
1838 out_session:
1839 ceph_put_mds_session(session);
1840 out:
1841 return err;
1843 finish:
1844 req->r_err = err;
1845 complete_request(mdsc, req);
1846 goto out;
1850 * called under mdsc->mutex
1852 static void __wake_requests(struct ceph_mds_client *mdsc,
1853 struct list_head *head)
1855 struct ceph_mds_request *req, *nreq;
1857 list_for_each_entry_safe(req, nreq, head, r_wait) {
1858 list_del_init(&req->r_wait);
1859 __do_request(mdsc, req);
1864 * Wake up threads with requests pending for @mds, so that they can
1865 * resubmit their requests to a possibly different mds.
1867 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
1869 struct ceph_mds_request *req;
1870 struct rb_node *p;
1872 dout("kick_requests mds%d\n", mds);
1873 for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) {
1874 req = rb_entry(p, struct ceph_mds_request, r_node);
1875 if (req->r_got_unsafe)
1876 continue;
1877 if (req->r_session &&
1878 req->r_session->s_mds == mds) {
1879 dout(" kicking tid %llu\n", req->r_tid);
1880 __do_request(mdsc, req);
1885 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
1886 struct ceph_mds_request *req)
1888 dout("submit_request on %p\n", req);
1889 mutex_lock(&mdsc->mutex);
1890 __register_request(mdsc, req, NULL);
1891 __do_request(mdsc, req);
1892 mutex_unlock(&mdsc->mutex);
1896 * Synchrously perform an mds request. Take care of all of the
1897 * session setup, forwarding, retry details.
1899 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
1900 struct inode *dir,
1901 struct ceph_mds_request *req)
1903 int err;
1905 dout("do_request on %p\n", req);
1907 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
1908 if (req->r_inode)
1909 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
1910 if (req->r_locked_dir)
1911 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
1912 if (req->r_old_dentry)
1913 ceph_get_cap_refs(
1914 ceph_inode(req->r_old_dentry->d_parent->d_inode),
1915 CEPH_CAP_PIN);
1917 /* issue */
1918 mutex_lock(&mdsc->mutex);
1919 __register_request(mdsc, req, dir);
1920 __do_request(mdsc, req);
1922 if (req->r_err) {
1923 err = req->r_err;
1924 __unregister_request(mdsc, req);
1925 dout("do_request early error %d\n", err);
1926 goto out;
1929 /* wait */
1930 mutex_unlock(&mdsc->mutex);
1931 dout("do_request waiting\n");
1932 if (req->r_timeout) {
1933 err = (long)wait_for_completion_killable_timeout(
1934 &req->r_completion, req->r_timeout);
1935 if (err == 0)
1936 err = -EIO;
1937 } else {
1938 err = wait_for_completion_killable(&req->r_completion);
1940 dout("do_request waited, got %d\n", err);
1941 mutex_lock(&mdsc->mutex);
1943 /* only abort if we didn't race with a real reply */
1944 if (req->r_got_result) {
1945 err = le32_to_cpu(req->r_reply_info.head->result);
1946 } else if (err < 0) {
1947 dout("aborted request %lld with %d\n", req->r_tid, err);
1950 * ensure we aren't running concurrently with
1951 * ceph_fill_trace or ceph_readdir_prepopulate, which
1952 * rely on locks (dir mutex) held by our caller.
1954 mutex_lock(&req->r_fill_mutex);
1955 req->r_err = err;
1956 req->r_aborted = true;
1957 mutex_unlock(&req->r_fill_mutex);
1959 if (req->r_locked_dir &&
1960 (req->r_op & CEPH_MDS_OP_WRITE))
1961 ceph_invalidate_dir_request(req);
1962 } else {
1963 err = req->r_err;
1966 out:
1967 mutex_unlock(&mdsc->mutex);
1968 dout("do_request %p done, result %d\n", req, err);
1969 return err;
1973 * Invalidate dir I_COMPLETE, dentry lease state on an aborted MDS
1974 * namespace request.
1976 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
1978 struct inode *inode = req->r_locked_dir;
1979 struct ceph_inode_info *ci = ceph_inode(inode);
1981 dout("invalidate_dir_request %p (I_COMPLETE, lease(s))\n", inode);
1982 spin_lock(&inode->i_lock);
1983 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
1984 ci->i_release_count++;
1985 spin_unlock(&inode->i_lock);
1987 if (req->r_dentry)
1988 ceph_invalidate_dentry_lease(req->r_dentry);
1989 if (req->r_old_dentry)
1990 ceph_invalidate_dentry_lease(req->r_old_dentry);
1994 * Handle mds reply.
1996 * We take the session mutex and parse and process the reply immediately.
1997 * This preserves the logical ordering of replies, capabilities, etc., sent
1998 * by the MDS as they are applied to our local cache.
2000 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2002 struct ceph_mds_client *mdsc = session->s_mdsc;
2003 struct ceph_mds_request *req;
2004 struct ceph_mds_reply_head *head = msg->front.iov_base;
2005 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2006 u64 tid;
2007 int err, result;
2008 int mds = session->s_mds;
2010 if (msg->front.iov_len < sizeof(*head)) {
2011 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2012 ceph_msg_dump(msg);
2013 return;
2016 /* get request, session */
2017 tid = le64_to_cpu(msg->hdr.tid);
2018 mutex_lock(&mdsc->mutex);
2019 req = __lookup_request(mdsc, tid);
2020 if (!req) {
2021 dout("handle_reply on unknown tid %llu\n", tid);
2022 mutex_unlock(&mdsc->mutex);
2023 return;
2025 dout("handle_reply %p\n", req);
2027 /* correct session? */
2028 if (req->r_session != session) {
2029 pr_err("mdsc_handle_reply got %llu on session mds%d"
2030 " not mds%d\n", tid, session->s_mds,
2031 req->r_session ? req->r_session->s_mds : -1);
2032 mutex_unlock(&mdsc->mutex);
2033 goto out;
2036 /* dup? */
2037 if ((req->r_got_unsafe && !head->safe) ||
2038 (req->r_got_safe && head->safe)) {
2039 pr_warning("got a dup %s reply on %llu from mds%d\n",
2040 head->safe ? "safe" : "unsafe", tid, mds);
2041 mutex_unlock(&mdsc->mutex);
2042 goto out;
2044 if (req->r_got_safe && !head->safe) {
2045 pr_warning("got unsafe after safe on %llu from mds%d\n",
2046 tid, mds);
2047 mutex_unlock(&mdsc->mutex);
2048 goto out;
2051 result = le32_to_cpu(head->result);
2054 * Handle an ESTALE
2055 * if we're not talking to the authority, send to them
2056 * if the authority has changed while we weren't looking,
2057 * send to new authority
2058 * Otherwise we just have to return an ESTALE
2060 if (result == -ESTALE) {
2061 dout("got ESTALE on request %llu", req->r_tid);
2062 if (!req->r_inode) {
2063 /* do nothing; not an authority problem */
2064 } else if (req->r_direct_mode != USE_AUTH_MDS) {
2065 dout("not using auth, setting for that now");
2066 req->r_direct_mode = USE_AUTH_MDS;
2067 __do_request(mdsc, req);
2068 mutex_unlock(&mdsc->mutex);
2069 goto out;
2070 } else {
2071 struct ceph_inode_info *ci = ceph_inode(req->r_inode);
2072 struct ceph_cap *cap = NULL;
2074 if (req->r_session)
2075 cap = ceph_get_cap_for_mds(ci,
2076 req->r_session->s_mds);
2078 dout("already using auth");
2079 if ((!cap || cap != ci->i_auth_cap) ||
2080 (cap->mseq != req->r_sent_on_mseq)) {
2081 dout("but cap changed, so resending");
2082 __do_request(mdsc, req);
2083 mutex_unlock(&mdsc->mutex);
2084 goto out;
2087 dout("have to return ESTALE on request %llu", req->r_tid);
2091 if (head->safe) {
2092 req->r_got_safe = true;
2093 __unregister_request(mdsc, req);
2094 complete_all(&req->r_safe_completion);
2096 if (req->r_got_unsafe) {
2098 * We already handled the unsafe response, now do the
2099 * cleanup. No need to examine the response; the MDS
2100 * doesn't include any result info in the safe
2101 * response. And even if it did, there is nothing
2102 * useful we could do with a revised return value.
2104 dout("got safe reply %llu, mds%d\n", tid, mds);
2105 list_del_init(&req->r_unsafe_item);
2107 /* last unsafe request during umount? */
2108 if (mdsc->stopping && !__get_oldest_req(mdsc))
2109 complete_all(&mdsc->safe_umount_waiters);
2110 mutex_unlock(&mdsc->mutex);
2111 goto out;
2113 } else {
2114 req->r_got_unsafe = true;
2115 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2118 dout("handle_reply tid %lld result %d\n", tid, result);
2119 rinfo = &req->r_reply_info;
2120 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2121 mutex_unlock(&mdsc->mutex);
2123 mutex_lock(&session->s_mutex);
2124 if (err < 0) {
2125 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
2126 ceph_msg_dump(msg);
2127 goto out_err;
2130 /* snap trace */
2131 if (rinfo->snapblob_len) {
2132 down_write(&mdsc->snap_rwsem);
2133 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2134 rinfo->snapblob + rinfo->snapblob_len,
2135 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP);
2136 downgrade_write(&mdsc->snap_rwsem);
2137 } else {
2138 down_read(&mdsc->snap_rwsem);
2141 /* insert trace into our cache */
2142 mutex_lock(&req->r_fill_mutex);
2143 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
2144 if (err == 0) {
2145 if (result == 0 && req->r_op != CEPH_MDS_OP_GETFILELOCK &&
2146 rinfo->dir_nr)
2147 ceph_readdir_prepopulate(req, req->r_session);
2148 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2150 mutex_unlock(&req->r_fill_mutex);
2152 up_read(&mdsc->snap_rwsem);
2153 out_err:
2154 mutex_lock(&mdsc->mutex);
2155 if (!req->r_aborted) {
2156 if (err) {
2157 req->r_err = err;
2158 } else {
2159 req->r_reply = msg;
2160 ceph_msg_get(msg);
2161 req->r_got_result = true;
2163 } else {
2164 dout("reply arrived after request %lld was aborted\n", tid);
2166 mutex_unlock(&mdsc->mutex);
2168 ceph_add_cap_releases(mdsc, req->r_session);
2169 mutex_unlock(&session->s_mutex);
2171 /* kick calling process */
2172 complete_request(mdsc, req);
2173 out:
2174 ceph_mdsc_put_request(req);
2175 return;
2181 * handle mds notification that our request has been forwarded.
2183 static void handle_forward(struct ceph_mds_client *mdsc,
2184 struct ceph_mds_session *session,
2185 struct ceph_msg *msg)
2187 struct ceph_mds_request *req;
2188 u64 tid = le64_to_cpu(msg->hdr.tid);
2189 u32 next_mds;
2190 u32 fwd_seq;
2191 int err = -EINVAL;
2192 void *p = msg->front.iov_base;
2193 void *end = p + msg->front.iov_len;
2195 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2196 next_mds = ceph_decode_32(&p);
2197 fwd_seq = ceph_decode_32(&p);
2199 mutex_lock(&mdsc->mutex);
2200 req = __lookup_request(mdsc, tid);
2201 if (!req) {
2202 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2203 goto out; /* dup reply? */
2206 if (req->r_aborted) {
2207 dout("forward tid %llu aborted, unregistering\n", tid);
2208 __unregister_request(mdsc, req);
2209 } else if (fwd_seq <= req->r_num_fwd) {
2210 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2211 tid, next_mds, req->r_num_fwd, fwd_seq);
2212 } else {
2213 /* resend. forward race not possible; mds would drop */
2214 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2215 BUG_ON(req->r_err);
2216 BUG_ON(req->r_got_result);
2217 req->r_num_fwd = fwd_seq;
2218 req->r_resend_mds = next_mds;
2219 put_request_session(req);
2220 __do_request(mdsc, req);
2222 ceph_mdsc_put_request(req);
2223 out:
2224 mutex_unlock(&mdsc->mutex);
2225 return;
2227 bad:
2228 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2232 * handle a mds session control message
2234 static void handle_session(struct ceph_mds_session *session,
2235 struct ceph_msg *msg)
2237 struct ceph_mds_client *mdsc = session->s_mdsc;
2238 u32 op;
2239 u64 seq;
2240 int mds = session->s_mds;
2241 struct ceph_mds_session_head *h = msg->front.iov_base;
2242 int wake = 0;
2244 /* decode */
2245 if (msg->front.iov_len != sizeof(*h))
2246 goto bad;
2247 op = le32_to_cpu(h->op);
2248 seq = le64_to_cpu(h->seq);
2250 mutex_lock(&mdsc->mutex);
2251 if (op == CEPH_SESSION_CLOSE)
2252 __unregister_session(mdsc, session);
2253 /* FIXME: this ttl calculation is generous */
2254 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2255 mutex_unlock(&mdsc->mutex);
2257 mutex_lock(&session->s_mutex);
2259 dout("handle_session mds%d %s %p state %s seq %llu\n",
2260 mds, ceph_session_op_name(op), session,
2261 session_state_name(session->s_state), seq);
2263 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2264 session->s_state = CEPH_MDS_SESSION_OPEN;
2265 pr_info("mds%d came back\n", session->s_mds);
2268 switch (op) {
2269 case CEPH_SESSION_OPEN:
2270 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2271 pr_info("mds%d reconnect success\n", session->s_mds);
2272 session->s_state = CEPH_MDS_SESSION_OPEN;
2273 renewed_caps(mdsc, session, 0);
2274 wake = 1;
2275 if (mdsc->stopping)
2276 __close_session(mdsc, session);
2277 break;
2279 case CEPH_SESSION_RENEWCAPS:
2280 if (session->s_renew_seq == seq)
2281 renewed_caps(mdsc, session, 1);
2282 break;
2284 case CEPH_SESSION_CLOSE:
2285 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2286 pr_info("mds%d reconnect denied\n", session->s_mds);
2287 remove_session_caps(session);
2288 wake = 1; /* for good measure */
2289 wake_up_all(&mdsc->session_close_wq);
2290 kick_requests(mdsc, mds);
2291 break;
2293 case CEPH_SESSION_STALE:
2294 pr_info("mds%d caps went stale, renewing\n",
2295 session->s_mds);
2296 spin_lock(&session->s_cap_lock);
2297 session->s_cap_gen++;
2298 session->s_cap_ttl = 0;
2299 spin_unlock(&session->s_cap_lock);
2300 send_renew_caps(mdsc, session);
2301 break;
2303 case CEPH_SESSION_RECALL_STATE:
2304 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2305 break;
2307 default:
2308 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2309 WARN_ON(1);
2312 mutex_unlock(&session->s_mutex);
2313 if (wake) {
2314 mutex_lock(&mdsc->mutex);
2315 __wake_requests(mdsc, &session->s_waiting);
2316 mutex_unlock(&mdsc->mutex);
2318 return;
2320 bad:
2321 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2322 (int)msg->front.iov_len);
2323 ceph_msg_dump(msg);
2324 return;
2329 * called under session->mutex.
2331 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2332 struct ceph_mds_session *session)
2334 struct ceph_mds_request *req, *nreq;
2335 int err;
2337 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2339 mutex_lock(&mdsc->mutex);
2340 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2341 err = __prepare_send_request(mdsc, req, session->s_mds);
2342 if (!err) {
2343 ceph_msg_get(req->r_request);
2344 ceph_con_send(&session->s_con, req->r_request);
2347 mutex_unlock(&mdsc->mutex);
2351 * Encode information about a cap for a reconnect with the MDS.
2353 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2354 void *arg)
2356 union {
2357 struct ceph_mds_cap_reconnect v2;
2358 struct ceph_mds_cap_reconnect_v1 v1;
2359 } rec;
2360 size_t reclen;
2361 struct ceph_inode_info *ci;
2362 struct ceph_reconnect_state *recon_state = arg;
2363 struct ceph_pagelist *pagelist = recon_state->pagelist;
2364 char *path;
2365 int pathlen, err;
2366 u64 pathbase;
2367 struct dentry *dentry;
2369 ci = cap->ci;
2371 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2372 inode, ceph_vinop(inode), cap, cap->cap_id,
2373 ceph_cap_string(cap->issued));
2374 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2375 if (err)
2376 return err;
2378 dentry = d_find_alias(inode);
2379 if (dentry) {
2380 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2381 if (IS_ERR(path)) {
2382 err = PTR_ERR(path);
2383 goto out_dput;
2385 } else {
2386 path = NULL;
2387 pathlen = 0;
2389 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2390 if (err)
2391 goto out_free;
2393 spin_lock(&inode->i_lock);
2394 cap->seq = 0; /* reset cap seq */
2395 cap->issue_seq = 0; /* and issue_seq */
2397 if (recon_state->flock) {
2398 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2399 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2400 rec.v2.issued = cpu_to_le32(cap->issued);
2401 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2402 rec.v2.pathbase = cpu_to_le64(pathbase);
2403 rec.v2.flock_len = 0;
2404 reclen = sizeof(rec.v2);
2405 } else {
2406 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2407 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2408 rec.v1.issued = cpu_to_le32(cap->issued);
2409 rec.v1.size = cpu_to_le64(inode->i_size);
2410 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2411 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2412 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2413 rec.v1.pathbase = cpu_to_le64(pathbase);
2414 reclen = sizeof(rec.v1);
2416 spin_unlock(&inode->i_lock);
2418 if (recon_state->flock) {
2419 int num_fcntl_locks, num_flock_locks;
2420 struct ceph_pagelist_cursor trunc_point;
2422 ceph_pagelist_set_cursor(pagelist, &trunc_point);
2423 do {
2424 lock_flocks();
2425 ceph_count_locks(inode, &num_fcntl_locks,
2426 &num_flock_locks);
2427 rec.v2.flock_len = (2*sizeof(u32) +
2428 (num_fcntl_locks+num_flock_locks) *
2429 sizeof(struct ceph_filelock));
2430 unlock_flocks();
2432 /* pre-alloc pagelist */
2433 ceph_pagelist_truncate(pagelist, &trunc_point);
2434 err = ceph_pagelist_append(pagelist, &rec, reclen);
2435 if (!err)
2436 err = ceph_pagelist_reserve(pagelist,
2437 rec.v2.flock_len);
2439 /* encode locks */
2440 if (!err) {
2441 lock_flocks();
2442 err = ceph_encode_locks(inode,
2443 pagelist,
2444 num_fcntl_locks,
2445 num_flock_locks);
2446 unlock_flocks();
2448 } while (err == -ENOSPC);
2449 } else {
2450 err = ceph_pagelist_append(pagelist, &rec, reclen);
2453 out_free:
2454 kfree(path);
2455 out_dput:
2456 dput(dentry);
2457 return err;
2462 * If an MDS fails and recovers, clients need to reconnect in order to
2463 * reestablish shared state. This includes all caps issued through
2464 * this session _and_ the snap_realm hierarchy. Because it's not
2465 * clear which snap realms the mds cares about, we send everything we
2466 * know about.. that ensures we'll then get any new info the
2467 * recovering MDS might have.
2469 * This is a relatively heavyweight operation, but it's rare.
2471 * called with mdsc->mutex held.
2473 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2474 struct ceph_mds_session *session)
2476 struct ceph_msg *reply;
2477 struct rb_node *p;
2478 int mds = session->s_mds;
2479 int err = -ENOMEM;
2480 struct ceph_pagelist *pagelist;
2481 struct ceph_reconnect_state recon_state;
2483 pr_info("mds%d reconnect start\n", mds);
2485 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2486 if (!pagelist)
2487 goto fail_nopagelist;
2488 ceph_pagelist_init(pagelist);
2490 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS);
2491 if (!reply)
2492 goto fail_nomsg;
2494 mutex_lock(&session->s_mutex);
2495 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2496 session->s_seq = 0;
2498 ceph_con_open(&session->s_con,
2499 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2501 /* replay unsafe requests */
2502 replay_unsafe_requests(mdsc, session);
2504 down_read(&mdsc->snap_rwsem);
2506 dout("session %p state %s\n", session,
2507 session_state_name(session->s_state));
2509 /* drop old cap expires; we're about to reestablish that state */
2510 discard_cap_releases(mdsc, session);
2512 /* traverse this session's caps */
2513 err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps);
2514 if (err)
2515 goto fail;
2517 recon_state.pagelist = pagelist;
2518 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2519 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
2520 if (err < 0)
2521 goto fail;
2524 * snaprealms. we provide mds with the ino, seq (version), and
2525 * parent for all of our realms. If the mds has any newer info,
2526 * it will tell us.
2528 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2529 struct ceph_snap_realm *realm =
2530 rb_entry(p, struct ceph_snap_realm, node);
2531 struct ceph_mds_snaprealm_reconnect sr_rec;
2533 dout(" adding snap realm %llx seq %lld parent %llx\n",
2534 realm->ino, realm->seq, realm->parent_ino);
2535 sr_rec.ino = cpu_to_le64(realm->ino);
2536 sr_rec.seq = cpu_to_le64(realm->seq);
2537 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2538 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2539 if (err)
2540 goto fail;
2543 reply->pagelist = pagelist;
2544 if (recon_state.flock)
2545 reply->hdr.version = cpu_to_le16(2);
2546 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2547 reply->nr_pages = calc_pages_for(0, pagelist->length);
2548 ceph_con_send(&session->s_con, reply);
2550 mutex_unlock(&session->s_mutex);
2552 mutex_lock(&mdsc->mutex);
2553 __wake_requests(mdsc, &session->s_waiting);
2554 mutex_unlock(&mdsc->mutex);
2556 up_read(&mdsc->snap_rwsem);
2557 return;
2559 fail:
2560 ceph_msg_put(reply);
2561 up_read(&mdsc->snap_rwsem);
2562 mutex_unlock(&session->s_mutex);
2563 fail_nomsg:
2564 ceph_pagelist_release(pagelist);
2565 kfree(pagelist);
2566 fail_nopagelist:
2567 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
2568 return;
2573 * compare old and new mdsmaps, kicking requests
2574 * and closing out old connections as necessary
2576 * called under mdsc->mutex.
2578 static void check_new_map(struct ceph_mds_client *mdsc,
2579 struct ceph_mdsmap *newmap,
2580 struct ceph_mdsmap *oldmap)
2582 int i;
2583 int oldstate, newstate;
2584 struct ceph_mds_session *s;
2586 dout("check_new_map new %u old %u\n",
2587 newmap->m_epoch, oldmap->m_epoch);
2589 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
2590 if (mdsc->sessions[i] == NULL)
2591 continue;
2592 s = mdsc->sessions[i];
2593 oldstate = ceph_mdsmap_get_state(oldmap, i);
2594 newstate = ceph_mdsmap_get_state(newmap, i);
2596 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2597 i, ceph_mds_state_name(oldstate),
2598 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
2599 ceph_mds_state_name(newstate),
2600 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
2601 session_state_name(s->s_state));
2603 if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
2604 ceph_mdsmap_get_addr(newmap, i),
2605 sizeof(struct ceph_entity_addr))) {
2606 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
2607 /* the session never opened, just close it
2608 * out now */
2609 __wake_requests(mdsc, &s->s_waiting);
2610 __unregister_session(mdsc, s);
2611 } else {
2612 /* just close it */
2613 mutex_unlock(&mdsc->mutex);
2614 mutex_lock(&s->s_mutex);
2615 mutex_lock(&mdsc->mutex);
2616 ceph_con_close(&s->s_con);
2617 mutex_unlock(&s->s_mutex);
2618 s->s_state = CEPH_MDS_SESSION_RESTARTING;
2621 /* kick any requests waiting on the recovering mds */
2622 kick_requests(mdsc, i);
2623 } else if (oldstate == newstate) {
2624 continue; /* nothing new with this mds */
2628 * send reconnect?
2630 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
2631 newstate >= CEPH_MDS_STATE_RECONNECT) {
2632 mutex_unlock(&mdsc->mutex);
2633 send_mds_reconnect(mdsc, s);
2634 mutex_lock(&mdsc->mutex);
2638 * kick request on any mds that has gone active.
2640 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
2641 newstate >= CEPH_MDS_STATE_ACTIVE) {
2642 if (oldstate != CEPH_MDS_STATE_CREATING &&
2643 oldstate != CEPH_MDS_STATE_STARTING)
2644 pr_info("mds%d recovery completed\n", s->s_mds);
2645 kick_requests(mdsc, i);
2646 ceph_kick_flushing_caps(mdsc, s);
2647 wake_up_session_caps(s, 1);
2651 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
2652 s = mdsc->sessions[i];
2653 if (!s)
2654 continue;
2655 if (!ceph_mdsmap_is_laggy(newmap, i))
2656 continue;
2657 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2658 s->s_state == CEPH_MDS_SESSION_HUNG ||
2659 s->s_state == CEPH_MDS_SESSION_CLOSING) {
2660 dout(" connecting to export targets of laggy mds%d\n",
2662 __open_export_target_sessions(mdsc, s);
2670 * leases
2674 * caller must hold session s_mutex, dentry->d_lock
2676 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
2678 struct ceph_dentry_info *di = ceph_dentry(dentry);
2680 ceph_put_mds_session(di->lease_session);
2681 di->lease_session = NULL;
2684 static void handle_lease(struct ceph_mds_client *mdsc,
2685 struct ceph_mds_session *session,
2686 struct ceph_msg *msg)
2688 struct super_block *sb = mdsc->fsc->sb;
2689 struct inode *inode;
2690 struct ceph_inode_info *ci;
2691 struct dentry *parent, *dentry;
2692 struct ceph_dentry_info *di;
2693 int mds = session->s_mds;
2694 struct ceph_mds_lease *h = msg->front.iov_base;
2695 u32 seq;
2696 struct ceph_vino vino;
2697 int mask;
2698 struct qstr dname;
2699 int release = 0;
2701 dout("handle_lease from mds%d\n", mds);
2703 /* decode */
2704 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
2705 goto bad;
2706 vino.ino = le64_to_cpu(h->ino);
2707 vino.snap = CEPH_NOSNAP;
2708 mask = le16_to_cpu(h->mask);
2709 seq = le32_to_cpu(h->seq);
2710 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
2711 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
2712 if (dname.len != get_unaligned_le32(h+1))
2713 goto bad;
2715 mutex_lock(&session->s_mutex);
2716 session->s_seq++;
2718 /* lookup inode */
2719 inode = ceph_find_inode(sb, vino);
2720 dout("handle_lease %s, mask %d, ino %llx %p %.*s\n",
2721 ceph_lease_op_name(h->action), mask, vino.ino, inode,
2722 dname.len, dname.name);
2723 if (inode == NULL) {
2724 dout("handle_lease no inode %llx\n", vino.ino);
2725 goto release;
2727 ci = ceph_inode(inode);
2729 /* dentry */
2730 parent = d_find_alias(inode);
2731 if (!parent) {
2732 dout("no parent dentry on inode %p\n", inode);
2733 WARN_ON(1);
2734 goto release; /* hrm... */
2736 dname.hash = full_name_hash(dname.name, dname.len);
2737 dentry = d_lookup(parent, &dname);
2738 dput(parent);
2739 if (!dentry)
2740 goto release;
2742 spin_lock(&dentry->d_lock);
2743 di = ceph_dentry(dentry);
2744 switch (h->action) {
2745 case CEPH_MDS_LEASE_REVOKE:
2746 if (di && di->lease_session == session) {
2747 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
2748 h->seq = cpu_to_le32(di->lease_seq);
2749 __ceph_mdsc_drop_dentry_lease(dentry);
2751 release = 1;
2752 break;
2754 case CEPH_MDS_LEASE_RENEW:
2755 if (di && di->lease_session == session &&
2756 di->lease_gen == session->s_cap_gen &&
2757 di->lease_renew_from &&
2758 di->lease_renew_after == 0) {
2759 unsigned long duration =
2760 le32_to_cpu(h->duration_ms) * HZ / 1000;
2762 di->lease_seq = seq;
2763 dentry->d_time = di->lease_renew_from + duration;
2764 di->lease_renew_after = di->lease_renew_from +
2765 (duration >> 1);
2766 di->lease_renew_from = 0;
2768 break;
2770 spin_unlock(&dentry->d_lock);
2771 dput(dentry);
2773 if (!release)
2774 goto out;
2776 release:
2777 /* let's just reuse the same message */
2778 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
2779 ceph_msg_get(msg);
2780 ceph_con_send(&session->s_con, msg);
2782 out:
2783 iput(inode);
2784 mutex_unlock(&session->s_mutex);
2785 return;
2787 bad:
2788 pr_err("corrupt lease message\n");
2789 ceph_msg_dump(msg);
2792 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
2793 struct inode *inode,
2794 struct dentry *dentry, char action,
2795 u32 seq)
2797 struct ceph_msg *msg;
2798 struct ceph_mds_lease *lease;
2799 int len = sizeof(*lease) + sizeof(u32);
2800 int dnamelen = 0;
2802 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
2803 inode, dentry, ceph_lease_op_name(action), session->s_mds);
2804 dnamelen = dentry->d_name.len;
2805 len += dnamelen;
2807 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS);
2808 if (!msg)
2809 return;
2810 lease = msg->front.iov_base;
2811 lease->action = action;
2812 lease->mask = cpu_to_le16(1);
2813 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
2814 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
2815 lease->seq = cpu_to_le32(seq);
2816 put_unaligned_le32(dnamelen, lease + 1);
2817 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
2820 * if this is a preemptive lease RELEASE, no need to
2821 * flush request stream, since the actual request will
2822 * soon follow.
2824 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
2826 ceph_con_send(&session->s_con, msg);
2830 * Preemptively release a lease we expect to invalidate anyway.
2831 * Pass @inode always, @dentry is optional.
2833 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
2834 struct dentry *dentry, int mask)
2836 struct ceph_dentry_info *di;
2837 struct ceph_mds_session *session;
2838 u32 seq;
2840 BUG_ON(inode == NULL);
2841 BUG_ON(dentry == NULL);
2842 BUG_ON(mask == 0);
2844 /* is dentry lease valid? */
2845 spin_lock(&dentry->d_lock);
2846 di = ceph_dentry(dentry);
2847 if (!di || !di->lease_session ||
2848 di->lease_session->s_mds < 0 ||
2849 di->lease_gen != di->lease_session->s_cap_gen ||
2850 !time_before(jiffies, dentry->d_time)) {
2851 dout("lease_release inode %p dentry %p -- "
2852 "no lease on %d\n",
2853 inode, dentry, mask);
2854 spin_unlock(&dentry->d_lock);
2855 return;
2858 /* we do have a lease on this dentry; note mds and seq */
2859 session = ceph_get_mds_session(di->lease_session);
2860 seq = di->lease_seq;
2861 __ceph_mdsc_drop_dentry_lease(dentry);
2862 spin_unlock(&dentry->d_lock);
2864 dout("lease_release inode %p dentry %p mask %d to mds%d\n",
2865 inode, dentry, mask, session->s_mds);
2866 ceph_mdsc_lease_send_msg(session, inode, dentry,
2867 CEPH_MDS_LEASE_RELEASE, seq);
2868 ceph_put_mds_session(session);
2872 * drop all leases (and dentry refs) in preparation for umount
2874 static void drop_leases(struct ceph_mds_client *mdsc)
2876 int i;
2878 dout("drop_leases\n");
2879 mutex_lock(&mdsc->mutex);
2880 for (i = 0; i < mdsc->max_sessions; i++) {
2881 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2882 if (!s)
2883 continue;
2884 mutex_unlock(&mdsc->mutex);
2885 mutex_lock(&s->s_mutex);
2886 mutex_unlock(&s->s_mutex);
2887 ceph_put_mds_session(s);
2888 mutex_lock(&mdsc->mutex);
2890 mutex_unlock(&mdsc->mutex);
2896 * delayed work -- periodically trim expired leases, renew caps with mds
2898 static void schedule_delayed(struct ceph_mds_client *mdsc)
2900 int delay = 5;
2901 unsigned hz = round_jiffies_relative(HZ * delay);
2902 schedule_delayed_work(&mdsc->delayed_work, hz);
2905 static void delayed_work(struct work_struct *work)
2907 int i;
2908 struct ceph_mds_client *mdsc =
2909 container_of(work, struct ceph_mds_client, delayed_work.work);
2910 int renew_interval;
2911 int renew_caps;
2913 dout("mdsc delayed_work\n");
2914 ceph_check_delayed_caps(mdsc);
2916 mutex_lock(&mdsc->mutex);
2917 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
2918 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
2919 mdsc->last_renew_caps);
2920 if (renew_caps)
2921 mdsc->last_renew_caps = jiffies;
2923 for (i = 0; i < mdsc->max_sessions; i++) {
2924 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
2925 if (s == NULL)
2926 continue;
2927 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
2928 dout("resending session close request for mds%d\n",
2929 s->s_mds);
2930 request_close_session(mdsc, s);
2931 ceph_put_mds_session(s);
2932 continue;
2934 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
2935 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
2936 s->s_state = CEPH_MDS_SESSION_HUNG;
2937 pr_info("mds%d hung\n", s->s_mds);
2940 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
2941 /* this mds is failed or recovering, just wait */
2942 ceph_put_mds_session(s);
2943 continue;
2945 mutex_unlock(&mdsc->mutex);
2947 mutex_lock(&s->s_mutex);
2948 if (renew_caps)
2949 send_renew_caps(mdsc, s);
2950 else
2951 ceph_con_keepalive(&s->s_con);
2952 ceph_add_cap_releases(mdsc, s);
2953 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
2954 s->s_state == CEPH_MDS_SESSION_HUNG)
2955 ceph_send_cap_releases(mdsc, s);
2956 mutex_unlock(&s->s_mutex);
2957 ceph_put_mds_session(s);
2959 mutex_lock(&mdsc->mutex);
2961 mutex_unlock(&mdsc->mutex);
2963 schedule_delayed(mdsc);
2966 int ceph_mdsc_init(struct ceph_fs_client *fsc)
2969 struct ceph_mds_client *mdsc;
2971 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
2972 if (!mdsc)
2973 return -ENOMEM;
2974 mdsc->fsc = fsc;
2975 fsc->mdsc = mdsc;
2976 mutex_init(&mdsc->mutex);
2977 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
2978 if (mdsc->mdsmap == NULL)
2979 return -ENOMEM;
2981 init_completion(&mdsc->safe_umount_waiters);
2982 init_waitqueue_head(&mdsc->session_close_wq);
2983 INIT_LIST_HEAD(&mdsc->waiting_for_map);
2984 mdsc->sessions = NULL;
2985 mdsc->max_sessions = 0;
2986 mdsc->stopping = 0;
2987 init_rwsem(&mdsc->snap_rwsem);
2988 mdsc->snap_realms = RB_ROOT;
2989 INIT_LIST_HEAD(&mdsc->snap_empty);
2990 spin_lock_init(&mdsc->snap_empty_lock);
2991 mdsc->last_tid = 0;
2992 mdsc->request_tree = RB_ROOT;
2993 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
2994 mdsc->last_renew_caps = jiffies;
2995 INIT_LIST_HEAD(&mdsc->cap_delay_list);
2996 spin_lock_init(&mdsc->cap_delay_lock);
2997 INIT_LIST_HEAD(&mdsc->snap_flush_list);
2998 spin_lock_init(&mdsc->snap_flush_lock);
2999 mdsc->cap_flush_seq = 0;
3000 INIT_LIST_HEAD(&mdsc->cap_dirty);
3001 mdsc->num_cap_flushing = 0;
3002 spin_lock_init(&mdsc->cap_dirty_lock);
3003 init_waitqueue_head(&mdsc->cap_flushing_wq);
3004 spin_lock_init(&mdsc->dentry_lru_lock);
3005 INIT_LIST_HEAD(&mdsc->dentry_lru);
3007 ceph_caps_init(mdsc);
3008 ceph_adjust_min_caps(mdsc, fsc->min_caps);
3010 return 0;
3014 * Wait for safe replies on open mds requests. If we time out, drop
3015 * all requests from the tree to avoid dangling dentry refs.
3017 static void wait_requests(struct ceph_mds_client *mdsc)
3019 struct ceph_mds_request *req;
3020 struct ceph_fs_client *fsc = mdsc->fsc;
3022 mutex_lock(&mdsc->mutex);
3023 if (__get_oldest_req(mdsc)) {
3024 mutex_unlock(&mdsc->mutex);
3026 dout("wait_requests waiting for requests\n");
3027 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
3028 fsc->client->options->mount_timeout * HZ);
3030 /* tear down remaining requests */
3031 mutex_lock(&mdsc->mutex);
3032 while ((req = __get_oldest_req(mdsc))) {
3033 dout("wait_requests timed out on tid %llu\n",
3034 req->r_tid);
3035 __unregister_request(mdsc, req);
3038 mutex_unlock(&mdsc->mutex);
3039 dout("wait_requests done\n");
3043 * called before mount is ro, and before dentries are torn down.
3044 * (hmm, does this still race with new lookups?)
3046 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3048 dout("pre_umount\n");
3049 mdsc->stopping = 1;
3051 drop_leases(mdsc);
3052 ceph_flush_dirty_caps(mdsc);
3053 wait_requests(mdsc);
3056 * wait for reply handlers to drop their request refs and
3057 * their inode/dcache refs
3059 ceph_msgr_flush();
3063 * wait for all write mds requests to flush.
3065 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3067 struct ceph_mds_request *req = NULL, *nextreq;
3068 struct rb_node *n;
3070 mutex_lock(&mdsc->mutex);
3071 dout("wait_unsafe_requests want %lld\n", want_tid);
3072 restart:
3073 req = __get_oldest_req(mdsc);
3074 while (req && req->r_tid <= want_tid) {
3075 /* find next request */
3076 n = rb_next(&req->r_node);
3077 if (n)
3078 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3079 else
3080 nextreq = NULL;
3081 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3082 /* write op */
3083 ceph_mdsc_get_request(req);
3084 if (nextreq)
3085 ceph_mdsc_get_request(nextreq);
3086 mutex_unlock(&mdsc->mutex);
3087 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3088 req->r_tid, want_tid);
3089 wait_for_completion(&req->r_safe_completion);
3090 mutex_lock(&mdsc->mutex);
3091 ceph_mdsc_put_request(req);
3092 if (!nextreq)
3093 break; /* next dne before, so we're done! */
3094 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3095 /* next request was removed from tree */
3096 ceph_mdsc_put_request(nextreq);
3097 goto restart;
3099 ceph_mdsc_put_request(nextreq); /* won't go away */
3101 req = nextreq;
3103 mutex_unlock(&mdsc->mutex);
3104 dout("wait_unsafe_requests done\n");
3107 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3109 u64 want_tid, want_flush;
3111 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3112 return;
3114 dout("sync\n");
3115 mutex_lock(&mdsc->mutex);
3116 want_tid = mdsc->last_tid;
3117 want_flush = mdsc->cap_flush_seq;
3118 mutex_unlock(&mdsc->mutex);
3119 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3121 ceph_flush_dirty_caps(mdsc);
3123 wait_unsafe_requests(mdsc, want_tid);
3124 wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush));
3128 * true if all sessions are closed, or we force unmount
3130 bool done_closing_sessions(struct ceph_mds_client *mdsc)
3132 int i, n = 0;
3134 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3135 return true;
3137 mutex_lock(&mdsc->mutex);
3138 for (i = 0; i < mdsc->max_sessions; i++)
3139 if (mdsc->sessions[i])
3140 n++;
3141 mutex_unlock(&mdsc->mutex);
3142 return n == 0;
3146 * called after sb is ro.
3148 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3150 struct ceph_mds_session *session;
3151 int i;
3152 struct ceph_fs_client *fsc = mdsc->fsc;
3153 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
3155 dout("close_sessions\n");
3157 /* close sessions */
3158 mutex_lock(&mdsc->mutex);
3159 for (i = 0; i < mdsc->max_sessions; i++) {
3160 session = __ceph_lookup_mds_session(mdsc, i);
3161 if (!session)
3162 continue;
3163 mutex_unlock(&mdsc->mutex);
3164 mutex_lock(&session->s_mutex);
3165 __close_session(mdsc, session);
3166 mutex_unlock(&session->s_mutex);
3167 ceph_put_mds_session(session);
3168 mutex_lock(&mdsc->mutex);
3170 mutex_unlock(&mdsc->mutex);
3172 dout("waiting for sessions to close\n");
3173 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3174 timeout);
3176 /* tear down remaining sessions */
3177 mutex_lock(&mdsc->mutex);
3178 for (i = 0; i < mdsc->max_sessions; i++) {
3179 if (mdsc->sessions[i]) {
3180 session = get_session(mdsc->sessions[i]);
3181 __unregister_session(mdsc, session);
3182 mutex_unlock(&mdsc->mutex);
3183 mutex_lock(&session->s_mutex);
3184 remove_session_caps(session);
3185 mutex_unlock(&session->s_mutex);
3186 ceph_put_mds_session(session);
3187 mutex_lock(&mdsc->mutex);
3190 WARN_ON(!list_empty(&mdsc->cap_delay_list));
3191 mutex_unlock(&mdsc->mutex);
3193 ceph_cleanup_empty_realms(mdsc);
3195 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3197 dout("stopped\n");
3200 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
3202 dout("stop\n");
3203 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3204 if (mdsc->mdsmap)
3205 ceph_mdsmap_destroy(mdsc->mdsmap);
3206 kfree(mdsc->sessions);
3207 ceph_caps_finalize(mdsc);
3210 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3212 struct ceph_mds_client *mdsc = fsc->mdsc;
3214 ceph_mdsc_stop(mdsc);
3215 fsc->mdsc = NULL;
3216 kfree(mdsc);
3221 * handle mds map update.
3223 void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3225 u32 epoch;
3226 u32 maplen;
3227 void *p = msg->front.iov_base;
3228 void *end = p + msg->front.iov_len;
3229 struct ceph_mdsmap *newmap, *oldmap;
3230 struct ceph_fsid fsid;
3231 int err = -EINVAL;
3233 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3234 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3235 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
3236 return;
3237 epoch = ceph_decode_32(&p);
3238 maplen = ceph_decode_32(&p);
3239 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3241 /* do we need it? */
3242 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
3243 mutex_lock(&mdsc->mutex);
3244 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3245 dout("handle_map epoch %u <= our %u\n",
3246 epoch, mdsc->mdsmap->m_epoch);
3247 mutex_unlock(&mdsc->mutex);
3248 return;
3251 newmap = ceph_mdsmap_decode(&p, end);
3252 if (IS_ERR(newmap)) {
3253 err = PTR_ERR(newmap);
3254 goto bad_unlock;
3257 /* swap into place */
3258 if (mdsc->mdsmap) {
3259 oldmap = mdsc->mdsmap;
3260 mdsc->mdsmap = newmap;
3261 check_new_map(mdsc, newmap, oldmap);
3262 ceph_mdsmap_destroy(oldmap);
3263 } else {
3264 mdsc->mdsmap = newmap; /* first mds map */
3266 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
3268 __wake_requests(mdsc, &mdsc->waiting_for_map);
3270 mutex_unlock(&mdsc->mutex);
3271 schedule_delayed(mdsc);
3272 return;
3274 bad_unlock:
3275 mutex_unlock(&mdsc->mutex);
3276 bad:
3277 pr_err("error decoding mdsmap %d\n", err);
3278 return;
3281 static struct ceph_connection *con_get(struct ceph_connection *con)
3283 struct ceph_mds_session *s = con->private;
3285 if (get_session(s)) {
3286 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
3287 return con;
3289 dout("mdsc con_get %p FAIL\n", s);
3290 return NULL;
3293 static void con_put(struct ceph_connection *con)
3295 struct ceph_mds_session *s = con->private;
3297 ceph_put_mds_session(s);
3298 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref));
3302 * if the client is unresponsive for long enough, the mds will kill
3303 * the session entirely.
3305 static void peer_reset(struct ceph_connection *con)
3307 struct ceph_mds_session *s = con->private;
3308 struct ceph_mds_client *mdsc = s->s_mdsc;
3310 pr_warning("mds%d closed our session\n", s->s_mds);
3311 send_mds_reconnect(mdsc, s);
3314 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3316 struct ceph_mds_session *s = con->private;
3317 struct ceph_mds_client *mdsc = s->s_mdsc;
3318 int type = le16_to_cpu(msg->hdr.type);
3320 mutex_lock(&mdsc->mutex);
3321 if (__verify_registered_session(mdsc, s) < 0) {
3322 mutex_unlock(&mdsc->mutex);
3323 goto out;
3325 mutex_unlock(&mdsc->mutex);
3327 switch (type) {
3328 case CEPH_MSG_MDS_MAP:
3329 ceph_mdsc_handle_map(mdsc, msg);
3330 break;
3331 case CEPH_MSG_CLIENT_SESSION:
3332 handle_session(s, msg);
3333 break;
3334 case CEPH_MSG_CLIENT_REPLY:
3335 handle_reply(s, msg);
3336 break;
3337 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
3338 handle_forward(mdsc, s, msg);
3339 break;
3340 case CEPH_MSG_CLIENT_CAPS:
3341 ceph_handle_caps(s, msg);
3342 break;
3343 case CEPH_MSG_CLIENT_SNAP:
3344 ceph_handle_snap(mdsc, s, msg);
3345 break;
3346 case CEPH_MSG_CLIENT_LEASE:
3347 handle_lease(mdsc, s, msg);
3348 break;
3350 default:
3351 pr_err("received unknown message type %d %s\n", type,
3352 ceph_msg_type_name(type));
3354 out:
3355 ceph_msg_put(msg);
3359 * authentication
3361 static int get_authorizer(struct ceph_connection *con,
3362 void **buf, int *len, int *proto,
3363 void **reply_buf, int *reply_len, int force_new)
3365 struct ceph_mds_session *s = con->private;
3366 struct ceph_mds_client *mdsc = s->s_mdsc;
3367 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3368 int ret = 0;
3370 if (force_new && s->s_authorizer) {
3371 ac->ops->destroy_authorizer(ac, s->s_authorizer);
3372 s->s_authorizer = NULL;
3374 if (s->s_authorizer == NULL) {
3375 if (ac->ops->create_authorizer) {
3376 ret = ac->ops->create_authorizer(
3377 ac, CEPH_ENTITY_TYPE_MDS,
3378 &s->s_authorizer,
3379 &s->s_authorizer_buf,
3380 &s->s_authorizer_buf_len,
3381 &s->s_authorizer_reply_buf,
3382 &s->s_authorizer_reply_buf_len);
3383 if (ret)
3384 return ret;
3388 *proto = ac->protocol;
3389 *buf = s->s_authorizer_buf;
3390 *len = s->s_authorizer_buf_len;
3391 *reply_buf = s->s_authorizer_reply_buf;
3392 *reply_len = s->s_authorizer_reply_buf_len;
3393 return 0;
3397 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3399 struct ceph_mds_session *s = con->private;
3400 struct ceph_mds_client *mdsc = s->s_mdsc;
3401 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3403 return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len);
3406 static int invalidate_authorizer(struct ceph_connection *con)
3408 struct ceph_mds_session *s = con->private;
3409 struct ceph_mds_client *mdsc = s->s_mdsc;
3410 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3412 if (ac->ops->invalidate_authorizer)
3413 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3415 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
3418 static const struct ceph_connection_operations mds_con_ops = {
3419 .get = con_get,
3420 .put = con_put,
3421 .dispatch = dispatch,
3422 .get_authorizer = get_authorizer,
3423 .verify_authorizer_reply = verify_authorizer_reply,
3424 .invalidate_authorizer = invalidate_authorizer,
3425 .peer_reset = peer_reset,
3428 /* eof */