drm/dp/mst: fix kernel oops when turning off secondary monitor
[linux/fpc-iii.git] / fs / ceph / mds_client.c
blob4f3bf0f527f6c2d98c4480f1db2343ad694d65d9
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/gfp.h>
7 #include <linux/sched.h>
8 #include <linux/debugfs.h>
9 #include <linux/seq_file.h>
10 #include <linux/utsname.h>
12 #include "super.h"
13 #include "mds_client.h"
15 #include <linux/ceph/ceph_features.h>
16 #include <linux/ceph/messenger.h>
17 #include <linux/ceph/decode.h>
18 #include <linux/ceph/pagelist.h>
19 #include <linux/ceph/auth.h>
20 #include <linux/ceph/debugfs.h>
23 * A cluster of MDS (metadata server) daemons is responsible for
24 * managing the file system namespace (the directory hierarchy and
25 * inodes) and for coordinating shared access to storage. Metadata is
26 * partitioning hierarchically across a number of servers, and that
27 * partition varies over time as the cluster adjusts the distribution
28 * in order to balance load.
30 * The MDS client is primarily responsible to managing synchronous
31 * metadata requests for operations like open, unlink, and so forth.
32 * If there is a MDS failure, we find out about it when we (possibly
33 * request and) receive a new MDS map, and can resubmit affected
34 * requests.
36 * For the most part, though, we take advantage of a lossless
37 * communications channel to the MDS, and do not need to worry about
38 * timing out or resubmitting requests.
40 * We maintain a stateful "session" with each MDS we interact with.
41 * Within each session, we sent periodic heartbeat messages to ensure
42 * any capabilities or leases we have been issues remain valid. If
43 * the session times out and goes stale, our leases and capabilities
44 * are no longer valid.
47 struct ceph_reconnect_state {
48 int nr_caps;
49 struct ceph_pagelist *pagelist;
50 bool flock;
53 static void __wake_requests(struct ceph_mds_client *mdsc,
54 struct list_head *head);
56 static const struct ceph_connection_operations mds_con_ops;
60 * mds reply parsing
64 * parse individual inode info
66 static int parse_reply_info_in(void **p, void *end,
67 struct ceph_mds_reply_info_in *info,
68 u64 features)
70 int err = -EIO;
72 info->in = *p;
73 *p += sizeof(struct ceph_mds_reply_inode) +
74 sizeof(*info->in->fragtree.splits) *
75 le32_to_cpu(info->in->fragtree.nsplits);
77 ceph_decode_32_safe(p, end, info->symlink_len, bad);
78 ceph_decode_need(p, end, info->symlink_len, bad);
79 info->symlink = *p;
80 *p += info->symlink_len;
82 if (features & CEPH_FEATURE_DIRLAYOUTHASH)
83 ceph_decode_copy_safe(p, end, &info->dir_layout,
84 sizeof(info->dir_layout), bad);
85 else
86 memset(&info->dir_layout, 0, sizeof(info->dir_layout));
88 ceph_decode_32_safe(p, end, info->xattr_len, bad);
89 ceph_decode_need(p, end, info->xattr_len, bad);
90 info->xattr_data = *p;
91 *p += info->xattr_len;
93 if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
94 ceph_decode_64_safe(p, end, info->inline_version, bad);
95 ceph_decode_32_safe(p, end, info->inline_len, bad);
96 ceph_decode_need(p, end, info->inline_len, bad);
97 info->inline_data = *p;
98 *p += info->inline_len;
99 } else
100 info->inline_version = CEPH_INLINE_NONE;
102 return 0;
103 bad:
104 return err;
108 * parse a normal reply, which may contain a (dir+)dentry and/or a
109 * target inode.
111 static int parse_reply_info_trace(void **p, void *end,
112 struct ceph_mds_reply_info_parsed *info,
113 u64 features)
115 int err;
117 if (info->head->is_dentry) {
118 err = parse_reply_info_in(p, end, &info->diri, features);
119 if (err < 0)
120 goto out_bad;
122 if (unlikely(*p + sizeof(*info->dirfrag) > end))
123 goto bad;
124 info->dirfrag = *p;
125 *p += sizeof(*info->dirfrag) +
126 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
127 if (unlikely(*p > end))
128 goto bad;
130 ceph_decode_32_safe(p, end, info->dname_len, bad);
131 ceph_decode_need(p, end, info->dname_len, bad);
132 info->dname = *p;
133 *p += info->dname_len;
134 info->dlease = *p;
135 *p += sizeof(*info->dlease);
138 if (info->head->is_target) {
139 err = parse_reply_info_in(p, end, &info->targeti, features);
140 if (err < 0)
141 goto out_bad;
144 if (unlikely(*p != end))
145 goto bad;
146 return 0;
148 bad:
149 err = -EIO;
150 out_bad:
151 pr_err("problem parsing mds trace %d\n", err);
152 return err;
156 * parse readdir results
158 static int parse_reply_info_dir(void **p, void *end,
159 struct ceph_mds_reply_info_parsed *info,
160 u64 features)
162 u32 num, i = 0;
163 int err;
165 info->dir_dir = *p;
166 if (*p + sizeof(*info->dir_dir) > end)
167 goto bad;
168 *p += sizeof(*info->dir_dir) +
169 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
170 if (*p > end)
171 goto bad;
173 ceph_decode_need(p, end, sizeof(num) + 2, bad);
174 num = ceph_decode_32(p);
175 info->dir_end = ceph_decode_8(p);
176 info->dir_complete = ceph_decode_8(p);
177 if (num == 0)
178 goto done;
180 BUG_ON(!info->dir_in);
181 info->dir_dname = (void *)(info->dir_in + num);
182 info->dir_dname_len = (void *)(info->dir_dname + num);
183 info->dir_dlease = (void *)(info->dir_dname_len + num);
184 if ((unsigned long)(info->dir_dlease + num) >
185 (unsigned long)info->dir_in + info->dir_buf_size) {
186 pr_err("dir contents are larger than expected\n");
187 WARN_ON(1);
188 goto bad;
191 info->dir_nr = num;
192 while (num) {
193 /* dentry */
194 ceph_decode_need(p, end, sizeof(u32)*2, bad);
195 info->dir_dname_len[i] = ceph_decode_32(p);
196 ceph_decode_need(p, end, info->dir_dname_len[i], bad);
197 info->dir_dname[i] = *p;
198 *p += info->dir_dname_len[i];
199 dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i],
200 info->dir_dname[i]);
201 info->dir_dlease[i] = *p;
202 *p += sizeof(struct ceph_mds_reply_lease);
204 /* inode */
205 err = parse_reply_info_in(p, end, &info->dir_in[i], features);
206 if (err < 0)
207 goto out_bad;
208 i++;
209 num--;
212 done:
213 if (*p != end)
214 goto bad;
215 return 0;
217 bad:
218 err = -EIO;
219 out_bad:
220 pr_err("problem parsing dir contents %d\n", err);
221 return err;
225 * parse fcntl F_GETLK results
227 static int parse_reply_info_filelock(void **p, void *end,
228 struct ceph_mds_reply_info_parsed *info,
229 u64 features)
231 if (*p + sizeof(*info->filelock_reply) > end)
232 goto bad;
234 info->filelock_reply = *p;
235 *p += sizeof(*info->filelock_reply);
237 if (unlikely(*p != end))
238 goto bad;
239 return 0;
241 bad:
242 return -EIO;
246 * parse create results
248 static int parse_reply_info_create(void **p, void *end,
249 struct ceph_mds_reply_info_parsed *info,
250 u64 features)
252 if (features & CEPH_FEATURE_REPLY_CREATE_INODE) {
253 if (*p == end) {
254 info->has_create_ino = false;
255 } else {
256 info->has_create_ino = true;
257 info->ino = ceph_decode_64(p);
261 if (unlikely(*p != end))
262 goto bad;
263 return 0;
265 bad:
266 return -EIO;
270 * parse extra results
272 static int parse_reply_info_extra(void **p, void *end,
273 struct ceph_mds_reply_info_parsed *info,
274 u64 features)
276 u32 op = le32_to_cpu(info->head->op);
278 if (op == CEPH_MDS_OP_GETFILELOCK)
279 return parse_reply_info_filelock(p, end, info, features);
280 else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
281 return parse_reply_info_dir(p, end, info, features);
282 else if (op == CEPH_MDS_OP_CREATE)
283 return parse_reply_info_create(p, end, info, features);
284 else
285 return -EIO;
289 * parse entire mds reply
291 static int parse_reply_info(struct ceph_msg *msg,
292 struct ceph_mds_reply_info_parsed *info,
293 u64 features)
295 void *p, *end;
296 u32 len;
297 int err;
299 info->head = msg->front.iov_base;
300 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
301 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
303 /* trace */
304 ceph_decode_32_safe(&p, end, len, bad);
305 if (len > 0) {
306 ceph_decode_need(&p, end, len, bad);
307 err = parse_reply_info_trace(&p, p+len, info, features);
308 if (err < 0)
309 goto out_bad;
312 /* extra */
313 ceph_decode_32_safe(&p, end, len, bad);
314 if (len > 0) {
315 ceph_decode_need(&p, end, len, bad);
316 err = parse_reply_info_extra(&p, p+len, info, features);
317 if (err < 0)
318 goto out_bad;
321 /* snap blob */
322 ceph_decode_32_safe(&p, end, len, bad);
323 info->snapblob_len = len;
324 info->snapblob = p;
325 p += len;
327 if (p != end)
328 goto bad;
329 return 0;
331 bad:
332 err = -EIO;
333 out_bad:
334 pr_err("mds parse_reply err %d\n", err);
335 return err;
338 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
340 if (!info->dir_in)
341 return;
342 free_pages((unsigned long)info->dir_in, get_order(info->dir_buf_size));
347 * sessions
349 const char *ceph_session_state_name(int s)
351 switch (s) {
352 case CEPH_MDS_SESSION_NEW: return "new";
353 case CEPH_MDS_SESSION_OPENING: return "opening";
354 case CEPH_MDS_SESSION_OPEN: return "open";
355 case CEPH_MDS_SESSION_HUNG: return "hung";
356 case CEPH_MDS_SESSION_CLOSING: return "closing";
357 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
358 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
359 default: return "???";
363 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
365 if (atomic_inc_not_zero(&s->s_ref)) {
366 dout("mdsc get_session %p %d -> %d\n", s,
367 atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref));
368 return s;
369 } else {
370 dout("mdsc get_session %p 0 -- FAIL", s);
371 return NULL;
375 void ceph_put_mds_session(struct ceph_mds_session *s)
377 dout("mdsc put_session %p %d -> %d\n", s,
378 atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1);
379 if (atomic_dec_and_test(&s->s_ref)) {
380 if (s->s_auth.authorizer)
381 ceph_auth_destroy_authorizer(
382 s->s_mdsc->fsc->client->monc.auth,
383 s->s_auth.authorizer);
384 kfree(s);
389 * called under mdsc->mutex
391 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
392 int mds)
394 struct ceph_mds_session *session;
396 if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL)
397 return NULL;
398 session = mdsc->sessions[mds];
399 dout("lookup_mds_session %p %d\n", session,
400 atomic_read(&session->s_ref));
401 get_session(session);
402 return session;
405 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
407 if (mds >= mdsc->max_sessions)
408 return false;
409 return mdsc->sessions[mds];
412 static int __verify_registered_session(struct ceph_mds_client *mdsc,
413 struct ceph_mds_session *s)
415 if (s->s_mds >= mdsc->max_sessions ||
416 mdsc->sessions[s->s_mds] != s)
417 return -ENOENT;
418 return 0;
422 * create+register a new session for given mds.
423 * called under mdsc->mutex.
425 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
426 int mds)
428 struct ceph_mds_session *s;
430 if (mds >= mdsc->mdsmap->m_max_mds)
431 return ERR_PTR(-EINVAL);
433 s = kzalloc(sizeof(*s), GFP_NOFS);
434 if (!s)
435 return ERR_PTR(-ENOMEM);
436 s->s_mdsc = mdsc;
437 s->s_mds = mds;
438 s->s_state = CEPH_MDS_SESSION_NEW;
439 s->s_ttl = 0;
440 s->s_seq = 0;
441 mutex_init(&s->s_mutex);
443 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
445 spin_lock_init(&s->s_gen_ttl_lock);
446 s->s_cap_gen = 0;
447 s->s_cap_ttl = jiffies - 1;
449 spin_lock_init(&s->s_cap_lock);
450 s->s_renew_requested = 0;
451 s->s_renew_seq = 0;
452 INIT_LIST_HEAD(&s->s_caps);
453 s->s_nr_caps = 0;
454 s->s_trim_caps = 0;
455 atomic_set(&s->s_ref, 1);
456 INIT_LIST_HEAD(&s->s_waiting);
457 INIT_LIST_HEAD(&s->s_unsafe);
458 s->s_num_cap_releases = 0;
459 s->s_cap_reconnect = 0;
460 s->s_cap_iterator = NULL;
461 INIT_LIST_HEAD(&s->s_cap_releases);
462 INIT_LIST_HEAD(&s->s_cap_releases_done);
463 INIT_LIST_HEAD(&s->s_cap_flushing);
464 INIT_LIST_HEAD(&s->s_cap_snaps_flushing);
466 dout("register_session mds%d\n", mds);
467 if (mds >= mdsc->max_sessions) {
468 int newmax = 1 << get_count_order(mds+1);
469 struct ceph_mds_session **sa;
471 dout("register_session realloc to %d\n", newmax);
472 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
473 if (sa == NULL)
474 goto fail_realloc;
475 if (mdsc->sessions) {
476 memcpy(sa, mdsc->sessions,
477 mdsc->max_sessions * sizeof(void *));
478 kfree(mdsc->sessions);
480 mdsc->sessions = sa;
481 mdsc->max_sessions = newmax;
483 mdsc->sessions[mds] = s;
484 atomic_inc(&mdsc->num_sessions);
485 atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */
487 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
488 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
490 return s;
492 fail_realloc:
493 kfree(s);
494 return ERR_PTR(-ENOMEM);
498 * called under mdsc->mutex
500 static void __unregister_session(struct ceph_mds_client *mdsc,
501 struct ceph_mds_session *s)
503 dout("__unregister_session mds%d %p\n", s->s_mds, s);
504 BUG_ON(mdsc->sessions[s->s_mds] != s);
505 mdsc->sessions[s->s_mds] = NULL;
506 ceph_con_close(&s->s_con);
507 ceph_put_mds_session(s);
508 atomic_dec(&mdsc->num_sessions);
512 * drop session refs in request.
514 * should be last request ref, or hold mdsc->mutex
516 static void put_request_session(struct ceph_mds_request *req)
518 if (req->r_session) {
519 ceph_put_mds_session(req->r_session);
520 req->r_session = NULL;
524 void ceph_mdsc_release_request(struct kref *kref)
526 struct ceph_mds_request *req = container_of(kref,
527 struct ceph_mds_request,
528 r_kref);
529 destroy_reply_info(&req->r_reply_info);
530 if (req->r_request)
531 ceph_msg_put(req->r_request);
532 if (req->r_reply)
533 ceph_msg_put(req->r_reply);
534 if (req->r_inode) {
535 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
536 iput(req->r_inode);
538 if (req->r_locked_dir)
539 ceph_put_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
540 iput(req->r_target_inode);
541 if (req->r_dentry)
542 dput(req->r_dentry);
543 if (req->r_old_dentry)
544 dput(req->r_old_dentry);
545 if (req->r_old_dentry_dir) {
547 * track (and drop pins for) r_old_dentry_dir
548 * separately, since r_old_dentry's d_parent may have
549 * changed between the dir mutex being dropped and
550 * this request being freed.
552 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
553 CEPH_CAP_PIN);
554 iput(req->r_old_dentry_dir);
556 kfree(req->r_path1);
557 kfree(req->r_path2);
558 if (req->r_pagelist)
559 ceph_pagelist_release(req->r_pagelist);
560 put_request_session(req);
561 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
562 kfree(req);
566 * lookup session, bump ref if found.
568 * called under mdsc->mutex.
570 static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc,
571 u64 tid)
573 struct ceph_mds_request *req;
574 struct rb_node *n = mdsc->request_tree.rb_node;
576 while (n) {
577 req = rb_entry(n, struct ceph_mds_request, r_node);
578 if (tid < req->r_tid)
579 n = n->rb_left;
580 else if (tid > req->r_tid)
581 n = n->rb_right;
582 else {
583 ceph_mdsc_get_request(req);
584 return req;
587 return NULL;
590 static void __insert_request(struct ceph_mds_client *mdsc,
591 struct ceph_mds_request *new)
593 struct rb_node **p = &mdsc->request_tree.rb_node;
594 struct rb_node *parent = NULL;
595 struct ceph_mds_request *req = NULL;
597 while (*p) {
598 parent = *p;
599 req = rb_entry(parent, struct ceph_mds_request, r_node);
600 if (new->r_tid < req->r_tid)
601 p = &(*p)->rb_left;
602 else if (new->r_tid > req->r_tid)
603 p = &(*p)->rb_right;
604 else
605 BUG();
608 rb_link_node(&new->r_node, parent, p);
609 rb_insert_color(&new->r_node, &mdsc->request_tree);
613 * Register an in-flight request, and assign a tid. Link to directory
614 * are modifying (if any).
616 * Called under mdsc->mutex.
618 static void __register_request(struct ceph_mds_client *mdsc,
619 struct ceph_mds_request *req,
620 struct inode *dir)
622 req->r_tid = ++mdsc->last_tid;
623 if (req->r_num_caps)
624 ceph_reserve_caps(mdsc, &req->r_caps_reservation,
625 req->r_num_caps);
626 dout("__register_request %p tid %lld\n", req, req->r_tid);
627 ceph_mdsc_get_request(req);
628 __insert_request(mdsc, req);
630 req->r_uid = current_fsuid();
631 req->r_gid = current_fsgid();
633 if (dir) {
634 struct ceph_inode_info *ci = ceph_inode(dir);
636 ihold(dir);
637 spin_lock(&ci->i_unsafe_lock);
638 req->r_unsafe_dir = dir;
639 list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
640 spin_unlock(&ci->i_unsafe_lock);
644 static void __unregister_request(struct ceph_mds_client *mdsc,
645 struct ceph_mds_request *req)
647 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
648 rb_erase(&req->r_node, &mdsc->request_tree);
649 RB_CLEAR_NODE(&req->r_node);
651 if (req->r_unsafe_dir) {
652 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
654 spin_lock(&ci->i_unsafe_lock);
655 list_del_init(&req->r_unsafe_dir_item);
656 spin_unlock(&ci->i_unsafe_lock);
658 iput(req->r_unsafe_dir);
659 req->r_unsafe_dir = NULL;
662 complete_all(&req->r_safe_completion);
664 ceph_mdsc_put_request(req);
668 * Choose mds to send request to next. If there is a hint set in the
669 * request (e.g., due to a prior forward hint from the mds), use that.
670 * Otherwise, consult frag tree and/or caps to identify the
671 * appropriate mds. If all else fails, choose randomly.
673 * Called under mdsc->mutex.
675 static struct dentry *get_nonsnap_parent(struct dentry *dentry)
678 * we don't need to worry about protecting the d_parent access
679 * here because we never renaming inside the snapped namespace
680 * except to resplice to another snapdir, and either the old or new
681 * result is a valid result.
683 while (!IS_ROOT(dentry) && ceph_snap(d_inode(dentry)) != CEPH_NOSNAP)
684 dentry = dentry->d_parent;
685 return dentry;
688 static int __choose_mds(struct ceph_mds_client *mdsc,
689 struct ceph_mds_request *req)
691 struct inode *inode;
692 struct ceph_inode_info *ci;
693 struct ceph_cap *cap;
694 int mode = req->r_direct_mode;
695 int mds = -1;
696 u32 hash = req->r_direct_hash;
697 bool is_hash = req->r_direct_is_hash;
700 * is there a specific mds we should try? ignore hint if we have
701 * no session and the mds is not up (active or recovering).
703 if (req->r_resend_mds >= 0 &&
704 (__have_session(mdsc, req->r_resend_mds) ||
705 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
706 dout("choose_mds using resend_mds mds%d\n",
707 req->r_resend_mds);
708 return req->r_resend_mds;
711 if (mode == USE_RANDOM_MDS)
712 goto random;
714 inode = NULL;
715 if (req->r_inode) {
716 inode = req->r_inode;
717 } else if (req->r_dentry) {
718 /* ignore race with rename; old or new d_parent is okay */
719 struct dentry *parent = req->r_dentry->d_parent;
720 struct inode *dir = d_inode(parent);
722 if (dir->i_sb != mdsc->fsc->sb) {
723 /* not this fs! */
724 inode = d_inode(req->r_dentry);
725 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
726 /* direct snapped/virtual snapdir requests
727 * based on parent dir inode */
728 struct dentry *dn = get_nonsnap_parent(parent);
729 inode = d_inode(dn);
730 dout("__choose_mds using nonsnap parent %p\n", inode);
731 } else {
732 /* dentry target */
733 inode = d_inode(req->r_dentry);
734 if (!inode || mode == USE_AUTH_MDS) {
735 /* dir + name */
736 inode = dir;
737 hash = ceph_dentry_hash(dir, req->r_dentry);
738 is_hash = true;
743 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
744 (int)hash, mode);
745 if (!inode)
746 goto random;
747 ci = ceph_inode(inode);
749 if (is_hash && S_ISDIR(inode->i_mode)) {
750 struct ceph_inode_frag frag;
751 int found;
753 ceph_choose_frag(ci, hash, &frag, &found);
754 if (found) {
755 if (mode == USE_ANY_MDS && frag.ndist > 0) {
756 u8 r;
758 /* choose a random replica */
759 get_random_bytes(&r, 1);
760 r %= frag.ndist;
761 mds = frag.dist[r];
762 dout("choose_mds %p %llx.%llx "
763 "frag %u mds%d (%d/%d)\n",
764 inode, ceph_vinop(inode),
765 frag.frag, mds,
766 (int)r, frag.ndist);
767 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
768 CEPH_MDS_STATE_ACTIVE)
769 return mds;
772 /* since this file/dir wasn't known to be
773 * replicated, then we want to look for the
774 * authoritative mds. */
775 mode = USE_AUTH_MDS;
776 if (frag.mds >= 0) {
777 /* choose auth mds */
778 mds = frag.mds;
779 dout("choose_mds %p %llx.%llx "
780 "frag %u mds%d (auth)\n",
781 inode, ceph_vinop(inode), frag.frag, mds);
782 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
783 CEPH_MDS_STATE_ACTIVE)
784 return mds;
789 spin_lock(&ci->i_ceph_lock);
790 cap = NULL;
791 if (mode == USE_AUTH_MDS)
792 cap = ci->i_auth_cap;
793 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
794 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
795 if (!cap) {
796 spin_unlock(&ci->i_ceph_lock);
797 goto random;
799 mds = cap->session->s_mds;
800 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
801 inode, ceph_vinop(inode), mds,
802 cap == ci->i_auth_cap ? "auth " : "", cap);
803 spin_unlock(&ci->i_ceph_lock);
804 return mds;
806 random:
807 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
808 dout("choose_mds chose random mds%d\n", mds);
809 return mds;
814 * session messages
816 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
818 struct ceph_msg *msg;
819 struct ceph_mds_session_head *h;
821 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
822 false);
823 if (!msg) {
824 pr_err("create_session_msg ENOMEM creating msg\n");
825 return NULL;
827 h = msg->front.iov_base;
828 h->op = cpu_to_le32(op);
829 h->seq = cpu_to_le64(seq);
831 return msg;
835 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
836 * to include additional client metadata fields.
838 static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
840 struct ceph_msg *msg;
841 struct ceph_mds_session_head *h;
842 int i = -1;
843 int metadata_bytes = 0;
844 int metadata_key_count = 0;
845 struct ceph_options *opt = mdsc->fsc->client->options;
846 void *p;
848 const char* metadata[][2] = {
849 {"hostname", utsname()->nodename},
850 {"kernel_version", utsname()->release},
851 {"entity_id", opt->name ? opt->name : ""},
852 {NULL, NULL}
855 /* Calculate serialized length of metadata */
856 metadata_bytes = 4; /* map length */
857 for (i = 0; metadata[i][0] != NULL; ++i) {
858 metadata_bytes += 8 + strlen(metadata[i][0]) +
859 strlen(metadata[i][1]);
860 metadata_key_count++;
863 /* Allocate the message */
864 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + metadata_bytes,
865 GFP_NOFS, false);
866 if (!msg) {
867 pr_err("create_session_msg ENOMEM creating msg\n");
868 return NULL;
870 h = msg->front.iov_base;
871 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
872 h->seq = cpu_to_le64(seq);
875 * Serialize client metadata into waiting buffer space, using
876 * the format that userspace expects for map<string, string>
878 * ClientSession messages with metadata are v2
880 msg->hdr.version = cpu_to_le16(2);
881 msg->hdr.compat_version = cpu_to_le16(1);
883 /* The write pointer, following the session_head structure */
884 p = msg->front.iov_base + sizeof(*h);
886 /* Number of entries in the map */
887 ceph_encode_32(&p, metadata_key_count);
889 /* Two length-prefixed strings for each entry in the map */
890 for (i = 0; metadata[i][0] != NULL; ++i) {
891 size_t const key_len = strlen(metadata[i][0]);
892 size_t const val_len = strlen(metadata[i][1]);
894 ceph_encode_32(&p, key_len);
895 memcpy(p, metadata[i][0], key_len);
896 p += key_len;
897 ceph_encode_32(&p, val_len);
898 memcpy(p, metadata[i][1], val_len);
899 p += val_len;
902 return msg;
906 * send session open request.
908 * called under mdsc->mutex
910 static int __open_session(struct ceph_mds_client *mdsc,
911 struct ceph_mds_session *session)
913 struct ceph_msg *msg;
914 int mstate;
915 int mds = session->s_mds;
917 /* wait for mds to go active? */
918 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
919 dout("open_session to mds%d (%s)\n", mds,
920 ceph_mds_state_name(mstate));
921 session->s_state = CEPH_MDS_SESSION_OPENING;
922 session->s_renew_requested = jiffies;
924 /* send connect message */
925 msg = create_session_open_msg(mdsc, session->s_seq);
926 if (!msg)
927 return -ENOMEM;
928 ceph_con_send(&session->s_con, msg);
929 return 0;
933 * open sessions for any export targets for the given mds
935 * called under mdsc->mutex
937 static struct ceph_mds_session *
938 __open_export_target_session(struct ceph_mds_client *mdsc, int target)
940 struct ceph_mds_session *session;
942 session = __ceph_lookup_mds_session(mdsc, target);
943 if (!session) {
944 session = register_session(mdsc, target);
945 if (IS_ERR(session))
946 return session;
948 if (session->s_state == CEPH_MDS_SESSION_NEW ||
949 session->s_state == CEPH_MDS_SESSION_CLOSING)
950 __open_session(mdsc, session);
952 return session;
955 struct ceph_mds_session *
956 ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
958 struct ceph_mds_session *session;
960 dout("open_export_target_session to mds%d\n", target);
962 mutex_lock(&mdsc->mutex);
963 session = __open_export_target_session(mdsc, target);
964 mutex_unlock(&mdsc->mutex);
966 return session;
969 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
970 struct ceph_mds_session *session)
972 struct ceph_mds_info *mi;
973 struct ceph_mds_session *ts;
974 int i, mds = session->s_mds;
976 if (mds >= mdsc->mdsmap->m_max_mds)
977 return;
979 mi = &mdsc->mdsmap->m_info[mds];
980 dout("open_export_target_sessions for mds%d (%d targets)\n",
981 session->s_mds, mi->num_export_targets);
983 for (i = 0; i < mi->num_export_targets; i++) {
984 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
985 if (!IS_ERR(ts))
986 ceph_put_mds_session(ts);
990 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
991 struct ceph_mds_session *session)
993 mutex_lock(&mdsc->mutex);
994 __open_export_target_sessions(mdsc, session);
995 mutex_unlock(&mdsc->mutex);
999 * session caps
1003 * Free preallocated cap messages assigned to this session
1005 static void cleanup_cap_releases(struct ceph_mds_session *session)
1007 struct ceph_msg *msg;
1009 spin_lock(&session->s_cap_lock);
1010 while (!list_empty(&session->s_cap_releases)) {
1011 msg = list_first_entry(&session->s_cap_releases,
1012 struct ceph_msg, list_head);
1013 list_del_init(&msg->list_head);
1014 ceph_msg_put(msg);
1016 while (!list_empty(&session->s_cap_releases_done)) {
1017 msg = list_first_entry(&session->s_cap_releases_done,
1018 struct ceph_msg, list_head);
1019 list_del_init(&msg->list_head);
1020 ceph_msg_put(msg);
1022 spin_unlock(&session->s_cap_lock);
1025 static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1026 struct ceph_mds_session *session)
1028 struct ceph_mds_request *req;
1029 struct rb_node *p;
1031 dout("cleanup_session_requests mds%d\n", session->s_mds);
1032 mutex_lock(&mdsc->mutex);
1033 while (!list_empty(&session->s_unsafe)) {
1034 req = list_first_entry(&session->s_unsafe,
1035 struct ceph_mds_request, r_unsafe_item);
1036 list_del_init(&req->r_unsafe_item);
1037 pr_info(" dropping unsafe request %llu\n", req->r_tid);
1038 __unregister_request(mdsc, req);
1040 /* zero r_attempts, so kick_requests() will re-send requests */
1041 p = rb_first(&mdsc->request_tree);
1042 while (p) {
1043 req = rb_entry(p, struct ceph_mds_request, r_node);
1044 p = rb_next(p);
1045 if (req->r_session &&
1046 req->r_session->s_mds == session->s_mds)
1047 req->r_attempts = 0;
1049 mutex_unlock(&mdsc->mutex);
1053 * Helper to safely iterate over all caps associated with a session, with
1054 * special care taken to handle a racing __ceph_remove_cap().
1056 * Caller must hold session s_mutex.
1058 static int iterate_session_caps(struct ceph_mds_session *session,
1059 int (*cb)(struct inode *, struct ceph_cap *,
1060 void *), void *arg)
1062 struct list_head *p;
1063 struct ceph_cap *cap;
1064 struct inode *inode, *last_inode = NULL;
1065 struct ceph_cap *old_cap = NULL;
1066 int ret;
1068 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1069 spin_lock(&session->s_cap_lock);
1070 p = session->s_caps.next;
1071 while (p != &session->s_caps) {
1072 cap = list_entry(p, struct ceph_cap, session_caps);
1073 inode = igrab(&cap->ci->vfs_inode);
1074 if (!inode) {
1075 p = p->next;
1076 continue;
1078 session->s_cap_iterator = cap;
1079 spin_unlock(&session->s_cap_lock);
1081 if (last_inode) {
1082 iput(last_inode);
1083 last_inode = NULL;
1085 if (old_cap) {
1086 ceph_put_cap(session->s_mdsc, old_cap);
1087 old_cap = NULL;
1090 ret = cb(inode, cap, arg);
1091 last_inode = inode;
1093 spin_lock(&session->s_cap_lock);
1094 p = p->next;
1095 if (cap->ci == NULL) {
1096 dout("iterate_session_caps finishing cap %p removal\n",
1097 cap);
1098 BUG_ON(cap->session != session);
1099 list_del_init(&cap->session_caps);
1100 session->s_nr_caps--;
1101 cap->session = NULL;
1102 old_cap = cap; /* put_cap it w/o locks held */
1104 if (ret < 0)
1105 goto out;
1107 ret = 0;
1108 out:
1109 session->s_cap_iterator = NULL;
1110 spin_unlock(&session->s_cap_lock);
1112 iput(last_inode);
1113 if (old_cap)
1114 ceph_put_cap(session->s_mdsc, old_cap);
1116 return ret;
1119 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
1120 void *arg)
1122 struct ceph_inode_info *ci = ceph_inode(inode);
1123 int drop = 0;
1125 dout("removing cap %p, ci is %p, inode is %p\n",
1126 cap, ci, &ci->vfs_inode);
1127 spin_lock(&ci->i_ceph_lock);
1128 __ceph_remove_cap(cap, false);
1129 if (!ci->i_auth_cap) {
1130 struct ceph_mds_client *mdsc =
1131 ceph_sb_to_client(inode->i_sb)->mdsc;
1133 spin_lock(&mdsc->cap_dirty_lock);
1134 if (!list_empty(&ci->i_dirty_item)) {
1135 pr_info(" dropping dirty %s state for %p %lld\n",
1136 ceph_cap_string(ci->i_dirty_caps),
1137 inode, ceph_ino(inode));
1138 ci->i_dirty_caps = 0;
1139 list_del_init(&ci->i_dirty_item);
1140 drop = 1;
1142 if (!list_empty(&ci->i_flushing_item)) {
1143 pr_info(" dropping dirty+flushing %s state for %p %lld\n",
1144 ceph_cap_string(ci->i_flushing_caps),
1145 inode, ceph_ino(inode));
1146 ci->i_flushing_caps = 0;
1147 list_del_init(&ci->i_flushing_item);
1148 mdsc->num_cap_flushing--;
1149 drop = 1;
1151 spin_unlock(&mdsc->cap_dirty_lock);
1153 spin_unlock(&ci->i_ceph_lock);
1154 while (drop--)
1155 iput(inode);
1156 return 0;
1160 * caller must hold session s_mutex
1162 static void remove_session_caps(struct ceph_mds_session *session)
1164 dout("remove_session_caps on %p\n", session);
1165 iterate_session_caps(session, remove_session_caps_cb, NULL);
1167 spin_lock(&session->s_cap_lock);
1168 if (session->s_nr_caps > 0) {
1169 struct super_block *sb = session->s_mdsc->fsc->sb;
1170 struct inode *inode;
1171 struct ceph_cap *cap, *prev = NULL;
1172 struct ceph_vino vino;
1174 * iterate_session_caps() skips inodes that are being
1175 * deleted, we need to wait until deletions are complete.
1176 * __wait_on_freeing_inode() is designed for the job,
1177 * but it is not exported, so use lookup inode function
1178 * to access it.
1180 while (!list_empty(&session->s_caps)) {
1181 cap = list_entry(session->s_caps.next,
1182 struct ceph_cap, session_caps);
1183 if (cap == prev)
1184 break;
1185 prev = cap;
1186 vino = cap->ci->i_vino;
1187 spin_unlock(&session->s_cap_lock);
1189 inode = ceph_find_inode(sb, vino);
1190 iput(inode);
1192 spin_lock(&session->s_cap_lock);
1195 spin_unlock(&session->s_cap_lock);
1197 BUG_ON(session->s_nr_caps > 0);
1198 BUG_ON(!list_empty(&session->s_cap_flushing));
1199 cleanup_cap_releases(session);
1203 * wake up any threads waiting on this session's caps. if the cap is
1204 * old (didn't get renewed on the client reconnect), remove it now.
1206 * caller must hold s_mutex.
1208 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1209 void *arg)
1211 struct ceph_inode_info *ci = ceph_inode(inode);
1213 wake_up_all(&ci->i_cap_wq);
1214 if (arg) {
1215 spin_lock(&ci->i_ceph_lock);
1216 ci->i_wanted_max_size = 0;
1217 ci->i_requested_max_size = 0;
1218 spin_unlock(&ci->i_ceph_lock);
1220 return 0;
1223 static void wake_up_session_caps(struct ceph_mds_session *session,
1224 int reconnect)
1226 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1227 iterate_session_caps(session, wake_up_session_cb,
1228 (void *)(unsigned long)reconnect);
1232 * Send periodic message to MDS renewing all currently held caps. The
1233 * ack will reset the expiration for all caps from this session.
1235 * caller holds s_mutex
1237 static int send_renew_caps(struct ceph_mds_client *mdsc,
1238 struct ceph_mds_session *session)
1240 struct ceph_msg *msg;
1241 int state;
1243 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1244 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1245 pr_info("mds%d caps stale\n", session->s_mds);
1246 session->s_renew_requested = jiffies;
1248 /* do not try to renew caps until a recovering mds has reconnected
1249 * with its clients. */
1250 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1251 if (state < CEPH_MDS_STATE_RECONNECT) {
1252 dout("send_renew_caps ignoring mds%d (%s)\n",
1253 session->s_mds, ceph_mds_state_name(state));
1254 return 0;
1257 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1258 ceph_mds_state_name(state));
1259 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1260 ++session->s_renew_seq);
1261 if (!msg)
1262 return -ENOMEM;
1263 ceph_con_send(&session->s_con, msg);
1264 return 0;
1267 static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1268 struct ceph_mds_session *session, u64 seq)
1270 struct ceph_msg *msg;
1272 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1273 session->s_mds, ceph_session_state_name(session->s_state), seq);
1274 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1275 if (!msg)
1276 return -ENOMEM;
1277 ceph_con_send(&session->s_con, msg);
1278 return 0;
1283 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1285 * Called under session->s_mutex
1287 static void renewed_caps(struct ceph_mds_client *mdsc,
1288 struct ceph_mds_session *session, int is_renew)
1290 int was_stale;
1291 int wake = 0;
1293 spin_lock(&session->s_cap_lock);
1294 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
1296 session->s_cap_ttl = session->s_renew_requested +
1297 mdsc->mdsmap->m_session_timeout*HZ;
1299 if (was_stale) {
1300 if (time_before(jiffies, session->s_cap_ttl)) {
1301 pr_info("mds%d caps renewed\n", session->s_mds);
1302 wake = 1;
1303 } else {
1304 pr_info("mds%d caps still stale\n", session->s_mds);
1307 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1308 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1309 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1310 spin_unlock(&session->s_cap_lock);
1312 if (wake)
1313 wake_up_session_caps(session, 0);
1317 * send a session close request
1319 static int request_close_session(struct ceph_mds_client *mdsc,
1320 struct ceph_mds_session *session)
1322 struct ceph_msg *msg;
1324 dout("request_close_session mds%d state %s seq %lld\n",
1325 session->s_mds, ceph_session_state_name(session->s_state),
1326 session->s_seq);
1327 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1328 if (!msg)
1329 return -ENOMEM;
1330 ceph_con_send(&session->s_con, msg);
1331 return 0;
1335 * Called with s_mutex held.
1337 static int __close_session(struct ceph_mds_client *mdsc,
1338 struct ceph_mds_session *session)
1340 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1341 return 0;
1342 session->s_state = CEPH_MDS_SESSION_CLOSING;
1343 return request_close_session(mdsc, session);
1347 * Trim old(er) caps.
1349 * Because we can't cache an inode without one or more caps, we do
1350 * this indirectly: if a cap is unused, we prune its aliases, at which
1351 * point the inode will hopefully get dropped to.
1353 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1354 * memory pressure from the MDS, though, so it needn't be perfect.
1356 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1358 struct ceph_mds_session *session = arg;
1359 struct ceph_inode_info *ci = ceph_inode(inode);
1360 int used, wanted, oissued, mine;
1362 if (session->s_trim_caps <= 0)
1363 return -1;
1365 spin_lock(&ci->i_ceph_lock);
1366 mine = cap->issued | cap->implemented;
1367 used = __ceph_caps_used(ci);
1368 wanted = __ceph_caps_file_wanted(ci);
1369 oissued = __ceph_caps_issued_other(ci, cap);
1371 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1372 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1373 ceph_cap_string(used), ceph_cap_string(wanted));
1374 if (cap == ci->i_auth_cap) {
1375 if (ci->i_dirty_caps | ci->i_flushing_caps)
1376 goto out;
1377 if ((used | wanted) & CEPH_CAP_ANY_WR)
1378 goto out;
1380 if ((used | wanted) & ~oissued & mine)
1381 goto out; /* we need these caps */
1383 session->s_trim_caps--;
1384 if (oissued) {
1385 /* we aren't the only cap.. just remove us */
1386 __ceph_remove_cap(cap, true);
1387 } else {
1388 /* try to drop referring dentries */
1389 spin_unlock(&ci->i_ceph_lock);
1390 d_prune_aliases(inode);
1391 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1392 inode, cap, atomic_read(&inode->i_count));
1393 return 0;
1396 out:
1397 spin_unlock(&ci->i_ceph_lock);
1398 return 0;
1402 * Trim session cap count down to some max number.
1404 static int trim_caps(struct ceph_mds_client *mdsc,
1405 struct ceph_mds_session *session,
1406 int max_caps)
1408 int trim_caps = session->s_nr_caps - max_caps;
1410 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1411 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1412 if (trim_caps > 0) {
1413 session->s_trim_caps = trim_caps;
1414 iterate_session_caps(session, trim_caps_cb, session);
1415 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1416 session->s_mds, session->s_nr_caps, max_caps,
1417 trim_caps - session->s_trim_caps);
1418 session->s_trim_caps = 0;
1421 ceph_add_cap_releases(mdsc, session);
1422 ceph_send_cap_releases(mdsc, session);
1423 return 0;
1427 * Allocate cap_release messages. If there is a partially full message
1428 * in the queue, try to allocate enough to cover it's remainder, so that
1429 * we can send it immediately.
1431 * Called under s_mutex.
1433 int ceph_add_cap_releases(struct ceph_mds_client *mdsc,
1434 struct ceph_mds_session *session)
1436 struct ceph_msg *msg, *partial = NULL;
1437 struct ceph_mds_cap_release *head;
1438 int err = -ENOMEM;
1439 int extra = mdsc->fsc->mount_options->cap_release_safety;
1440 int num;
1442 dout("add_cap_releases %p mds%d extra %d\n", session, session->s_mds,
1443 extra);
1445 spin_lock(&session->s_cap_lock);
1447 if (!list_empty(&session->s_cap_releases)) {
1448 msg = list_first_entry(&session->s_cap_releases,
1449 struct ceph_msg,
1450 list_head);
1451 head = msg->front.iov_base;
1452 num = le32_to_cpu(head->num);
1453 if (num) {
1454 dout(" partial %p with (%d/%d)\n", msg, num,
1455 (int)CEPH_CAPS_PER_RELEASE);
1456 extra += CEPH_CAPS_PER_RELEASE - num;
1457 partial = msg;
1460 while (session->s_num_cap_releases < session->s_nr_caps + extra) {
1461 spin_unlock(&session->s_cap_lock);
1462 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE,
1463 GFP_NOFS, false);
1464 if (!msg)
1465 goto out_unlocked;
1466 dout("add_cap_releases %p msg %p now %d\n", session, msg,
1467 (int)msg->front.iov_len);
1468 head = msg->front.iov_base;
1469 head->num = cpu_to_le32(0);
1470 msg->front.iov_len = sizeof(*head);
1471 spin_lock(&session->s_cap_lock);
1472 list_add(&msg->list_head, &session->s_cap_releases);
1473 session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE;
1476 if (partial) {
1477 head = partial->front.iov_base;
1478 num = le32_to_cpu(head->num);
1479 dout(" queueing partial %p with %d/%d\n", partial, num,
1480 (int)CEPH_CAPS_PER_RELEASE);
1481 list_move_tail(&partial->list_head,
1482 &session->s_cap_releases_done);
1483 session->s_num_cap_releases -= CEPH_CAPS_PER_RELEASE - num;
1485 err = 0;
1486 spin_unlock(&session->s_cap_lock);
1487 out_unlocked:
1488 return err;
1491 static int check_cap_flush(struct inode *inode, u64 want_flush_seq)
1493 struct ceph_inode_info *ci = ceph_inode(inode);
1494 int ret;
1495 spin_lock(&ci->i_ceph_lock);
1496 if (ci->i_flushing_caps)
1497 ret = ci->i_cap_flush_seq >= want_flush_seq;
1498 else
1499 ret = 1;
1500 spin_unlock(&ci->i_ceph_lock);
1501 return ret;
1505 * flush all dirty inode data to disk.
1507 * returns true if we've flushed through want_flush_seq
1509 static void wait_caps_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq)
1511 int mds;
1513 dout("check_cap_flush want %lld\n", want_flush_seq);
1514 mutex_lock(&mdsc->mutex);
1515 for (mds = 0; mds < mdsc->max_sessions; mds++) {
1516 struct ceph_mds_session *session = mdsc->sessions[mds];
1517 struct inode *inode = NULL;
1519 if (!session)
1520 continue;
1521 get_session(session);
1522 mutex_unlock(&mdsc->mutex);
1524 mutex_lock(&session->s_mutex);
1525 if (!list_empty(&session->s_cap_flushing)) {
1526 struct ceph_inode_info *ci =
1527 list_entry(session->s_cap_flushing.next,
1528 struct ceph_inode_info,
1529 i_flushing_item);
1531 if (!check_cap_flush(&ci->vfs_inode, want_flush_seq)) {
1532 dout("check_cap_flush still flushing %p "
1533 "seq %lld <= %lld to mds%d\n",
1534 &ci->vfs_inode, ci->i_cap_flush_seq,
1535 want_flush_seq, session->s_mds);
1536 inode = igrab(&ci->vfs_inode);
1539 mutex_unlock(&session->s_mutex);
1540 ceph_put_mds_session(session);
1542 if (inode) {
1543 wait_event(mdsc->cap_flushing_wq,
1544 check_cap_flush(inode, want_flush_seq));
1545 iput(inode);
1548 mutex_lock(&mdsc->mutex);
1551 mutex_unlock(&mdsc->mutex);
1552 dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq);
1556 * called under s_mutex
1558 void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1559 struct ceph_mds_session *session)
1561 struct ceph_msg *msg;
1563 dout("send_cap_releases mds%d\n", session->s_mds);
1564 spin_lock(&session->s_cap_lock);
1565 while (!list_empty(&session->s_cap_releases_done)) {
1566 msg = list_first_entry(&session->s_cap_releases_done,
1567 struct ceph_msg, list_head);
1568 list_del_init(&msg->list_head);
1569 spin_unlock(&session->s_cap_lock);
1570 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1571 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1572 ceph_con_send(&session->s_con, msg);
1573 spin_lock(&session->s_cap_lock);
1575 spin_unlock(&session->s_cap_lock);
1578 static void discard_cap_releases(struct ceph_mds_client *mdsc,
1579 struct ceph_mds_session *session)
1581 struct ceph_msg *msg;
1582 struct ceph_mds_cap_release *head;
1583 unsigned num;
1585 dout("discard_cap_releases mds%d\n", session->s_mds);
1587 if (!list_empty(&session->s_cap_releases)) {
1588 /* zero out the in-progress message */
1589 msg = list_first_entry(&session->s_cap_releases,
1590 struct ceph_msg, list_head);
1591 head = msg->front.iov_base;
1592 num = le32_to_cpu(head->num);
1593 dout("discard_cap_releases mds%d %p %u\n",
1594 session->s_mds, msg, num);
1595 head->num = cpu_to_le32(0);
1596 msg->front.iov_len = sizeof(*head);
1597 session->s_num_cap_releases += num;
1600 /* requeue completed messages */
1601 while (!list_empty(&session->s_cap_releases_done)) {
1602 msg = list_first_entry(&session->s_cap_releases_done,
1603 struct ceph_msg, list_head);
1604 list_del_init(&msg->list_head);
1606 head = msg->front.iov_base;
1607 num = le32_to_cpu(head->num);
1608 dout("discard_cap_releases mds%d %p %u\n", session->s_mds, msg,
1609 num);
1610 session->s_num_cap_releases += num;
1611 head->num = cpu_to_le32(0);
1612 msg->front.iov_len = sizeof(*head);
1613 list_add(&msg->list_head, &session->s_cap_releases);
1618 * requests
1621 int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1622 struct inode *dir)
1624 struct ceph_inode_info *ci = ceph_inode(dir);
1625 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1626 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
1627 size_t size = sizeof(*rinfo->dir_in) + sizeof(*rinfo->dir_dname_len) +
1628 sizeof(*rinfo->dir_dname) + sizeof(*rinfo->dir_dlease);
1629 int order, num_entries;
1631 spin_lock(&ci->i_ceph_lock);
1632 num_entries = ci->i_files + ci->i_subdirs;
1633 spin_unlock(&ci->i_ceph_lock);
1634 num_entries = max(num_entries, 1);
1635 num_entries = min(num_entries, opt->max_readdir);
1637 order = get_order(size * num_entries);
1638 while (order >= 0) {
1639 rinfo->dir_in = (void*)__get_free_pages(GFP_NOFS | __GFP_NOWARN,
1640 order);
1641 if (rinfo->dir_in)
1642 break;
1643 order--;
1645 if (!rinfo->dir_in)
1646 return -ENOMEM;
1648 num_entries = (PAGE_SIZE << order) / size;
1649 num_entries = min(num_entries, opt->max_readdir);
1651 rinfo->dir_buf_size = PAGE_SIZE << order;
1652 req->r_num_caps = num_entries + 1;
1653 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1654 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1655 return 0;
1659 * Create an mds request.
1661 struct ceph_mds_request *
1662 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1664 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1666 if (!req)
1667 return ERR_PTR(-ENOMEM);
1669 mutex_init(&req->r_fill_mutex);
1670 req->r_mdsc = mdsc;
1671 req->r_started = jiffies;
1672 req->r_resend_mds = -1;
1673 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1674 req->r_fmode = -1;
1675 kref_init(&req->r_kref);
1676 INIT_LIST_HEAD(&req->r_wait);
1677 init_completion(&req->r_completion);
1678 init_completion(&req->r_safe_completion);
1679 INIT_LIST_HEAD(&req->r_unsafe_item);
1681 req->r_stamp = CURRENT_TIME;
1683 req->r_op = op;
1684 req->r_direct_mode = mode;
1685 return req;
1689 * return oldest (lowest) request, tid in request tree, 0 if none.
1691 * called under mdsc->mutex.
1693 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1695 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1696 return NULL;
1697 return rb_entry(rb_first(&mdsc->request_tree),
1698 struct ceph_mds_request, r_node);
1701 static u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1703 struct ceph_mds_request *req = __get_oldest_req(mdsc);
1705 if (req)
1706 return req->r_tid;
1707 return 0;
1711 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1712 * on build_path_from_dentry in fs/cifs/dir.c.
1714 * If @stop_on_nosnap, generate path relative to the first non-snapped
1715 * inode.
1717 * Encode hidden .snap dirs as a double /, i.e.
1718 * foo/.snap/bar -> foo//bar
1720 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1721 int stop_on_nosnap)
1723 struct dentry *temp;
1724 char *path;
1725 int len, pos;
1726 unsigned seq;
1728 if (dentry == NULL)
1729 return ERR_PTR(-EINVAL);
1731 retry:
1732 len = 0;
1733 seq = read_seqbegin(&rename_lock);
1734 rcu_read_lock();
1735 for (temp = dentry; !IS_ROOT(temp);) {
1736 struct inode *inode = d_inode(temp);
1737 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1738 len++; /* slash only */
1739 else if (stop_on_nosnap && inode &&
1740 ceph_snap(inode) == CEPH_NOSNAP)
1741 break;
1742 else
1743 len += 1 + temp->d_name.len;
1744 temp = temp->d_parent;
1746 rcu_read_unlock();
1747 if (len)
1748 len--; /* no leading '/' */
1750 path = kmalloc(len+1, GFP_NOFS);
1751 if (path == NULL)
1752 return ERR_PTR(-ENOMEM);
1753 pos = len;
1754 path[pos] = 0; /* trailing null */
1755 rcu_read_lock();
1756 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1757 struct inode *inode;
1759 spin_lock(&temp->d_lock);
1760 inode = d_inode(temp);
1761 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1762 dout("build_path path+%d: %p SNAPDIR\n",
1763 pos, temp);
1764 } else if (stop_on_nosnap && inode &&
1765 ceph_snap(inode) == CEPH_NOSNAP) {
1766 spin_unlock(&temp->d_lock);
1767 break;
1768 } else {
1769 pos -= temp->d_name.len;
1770 if (pos < 0) {
1771 spin_unlock(&temp->d_lock);
1772 break;
1774 strncpy(path + pos, temp->d_name.name,
1775 temp->d_name.len);
1777 spin_unlock(&temp->d_lock);
1778 if (pos)
1779 path[--pos] = '/';
1780 temp = temp->d_parent;
1782 rcu_read_unlock();
1783 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
1784 pr_err("build_path did not end path lookup where "
1785 "expected, namelen is %d, pos is %d\n", len, pos);
1786 /* presumably this is only possible if racing with a
1787 rename of one of the parent directories (we can not
1788 lock the dentries above us to prevent this, but
1789 retrying should be harmless) */
1790 kfree(path);
1791 goto retry;
1794 *base = ceph_ino(d_inode(temp));
1795 *plen = len;
1796 dout("build_path on %p %d built %llx '%.*s'\n",
1797 dentry, d_count(dentry), *base, len, path);
1798 return path;
1801 static int build_dentry_path(struct dentry *dentry,
1802 const char **ppath, int *ppathlen, u64 *pino,
1803 int *pfreepath)
1805 char *path;
1807 if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_NOSNAP) {
1808 *pino = ceph_ino(d_inode(dentry->d_parent));
1809 *ppath = dentry->d_name.name;
1810 *ppathlen = dentry->d_name.len;
1811 return 0;
1813 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1814 if (IS_ERR(path))
1815 return PTR_ERR(path);
1816 *ppath = path;
1817 *pfreepath = 1;
1818 return 0;
1821 static int build_inode_path(struct inode *inode,
1822 const char **ppath, int *ppathlen, u64 *pino,
1823 int *pfreepath)
1825 struct dentry *dentry;
1826 char *path;
1828 if (ceph_snap(inode) == CEPH_NOSNAP) {
1829 *pino = ceph_ino(inode);
1830 *ppathlen = 0;
1831 return 0;
1833 dentry = d_find_alias(inode);
1834 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1835 dput(dentry);
1836 if (IS_ERR(path))
1837 return PTR_ERR(path);
1838 *ppath = path;
1839 *pfreepath = 1;
1840 return 0;
1844 * request arguments may be specified via an inode *, a dentry *, or
1845 * an explicit ino+path.
1847 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1848 const char *rpath, u64 rino,
1849 const char **ppath, int *pathlen,
1850 u64 *ino, int *freepath)
1852 int r = 0;
1854 if (rinode) {
1855 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1856 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1857 ceph_snap(rinode));
1858 } else if (rdentry) {
1859 r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath);
1860 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1861 *ppath);
1862 } else if (rpath || rino) {
1863 *ino = rino;
1864 *ppath = rpath;
1865 *pathlen = rpath ? strlen(rpath) : 0;
1866 dout(" path %.*s\n", *pathlen, rpath);
1869 return r;
1873 * called under mdsc->mutex
1875 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1876 struct ceph_mds_request *req,
1877 int mds, bool drop_cap_releases)
1879 struct ceph_msg *msg;
1880 struct ceph_mds_request_head *head;
1881 const char *path1 = NULL;
1882 const char *path2 = NULL;
1883 u64 ino1 = 0, ino2 = 0;
1884 int pathlen1 = 0, pathlen2 = 0;
1885 int freepath1 = 0, freepath2 = 0;
1886 int len;
1887 u16 releases;
1888 void *p, *end;
1889 int ret;
1891 ret = set_request_path_attr(req->r_inode, req->r_dentry,
1892 req->r_path1, req->r_ino1.ino,
1893 &path1, &pathlen1, &ino1, &freepath1);
1894 if (ret < 0) {
1895 msg = ERR_PTR(ret);
1896 goto out;
1899 ret = set_request_path_attr(NULL, req->r_old_dentry,
1900 req->r_path2, req->r_ino2.ino,
1901 &path2, &pathlen2, &ino2, &freepath2);
1902 if (ret < 0) {
1903 msg = ERR_PTR(ret);
1904 goto out_free1;
1907 len = sizeof(*head) +
1908 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
1909 sizeof(struct ceph_timespec);
1911 /* calculate (max) length for cap releases */
1912 len += sizeof(struct ceph_mds_request_release) *
1913 (!!req->r_inode_drop + !!req->r_dentry_drop +
1914 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1915 if (req->r_dentry_drop)
1916 len += req->r_dentry->d_name.len;
1917 if (req->r_old_dentry_drop)
1918 len += req->r_old_dentry->d_name.len;
1920 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
1921 if (!msg) {
1922 msg = ERR_PTR(-ENOMEM);
1923 goto out_free2;
1926 msg->hdr.version = cpu_to_le16(2);
1927 msg->hdr.tid = cpu_to_le64(req->r_tid);
1929 head = msg->front.iov_base;
1930 p = msg->front.iov_base + sizeof(*head);
1931 end = msg->front.iov_base + msg->front.iov_len;
1933 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1934 head->op = cpu_to_le32(req->r_op);
1935 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
1936 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
1937 head->args = req->r_args;
1939 ceph_encode_filepath(&p, end, ino1, path1);
1940 ceph_encode_filepath(&p, end, ino2, path2);
1942 /* make note of release offset, in case we need to replay */
1943 req->r_request_release_offset = p - msg->front.iov_base;
1945 /* cap releases */
1946 releases = 0;
1947 if (req->r_inode_drop)
1948 releases += ceph_encode_inode_release(&p,
1949 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
1950 mds, req->r_inode_drop, req->r_inode_unless, 0);
1951 if (req->r_dentry_drop)
1952 releases += ceph_encode_dentry_release(&p, req->r_dentry,
1953 mds, req->r_dentry_drop, req->r_dentry_unless);
1954 if (req->r_old_dentry_drop)
1955 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
1956 mds, req->r_old_dentry_drop, req->r_old_dentry_unless);
1957 if (req->r_old_inode_drop)
1958 releases += ceph_encode_inode_release(&p,
1959 d_inode(req->r_old_dentry),
1960 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
1962 if (drop_cap_releases) {
1963 releases = 0;
1964 p = msg->front.iov_base + req->r_request_release_offset;
1967 head->num_releases = cpu_to_le16(releases);
1969 /* time stamp */
1971 struct ceph_timespec ts;
1972 ceph_encode_timespec(&ts, &req->r_stamp);
1973 ceph_encode_copy(&p, &ts, sizeof(ts));
1976 BUG_ON(p > end);
1977 msg->front.iov_len = p - msg->front.iov_base;
1978 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1980 if (req->r_pagelist) {
1981 struct ceph_pagelist *pagelist = req->r_pagelist;
1982 atomic_inc(&pagelist->refcnt);
1983 ceph_msg_data_add_pagelist(msg, pagelist);
1984 msg->hdr.data_len = cpu_to_le32(pagelist->length);
1985 } else {
1986 msg->hdr.data_len = 0;
1989 msg->hdr.data_off = cpu_to_le16(0);
1991 out_free2:
1992 if (freepath2)
1993 kfree((char *)path2);
1994 out_free1:
1995 if (freepath1)
1996 kfree((char *)path1);
1997 out:
1998 return msg;
2002 * called under mdsc->mutex if error, under no mutex if
2003 * success.
2005 static void complete_request(struct ceph_mds_client *mdsc,
2006 struct ceph_mds_request *req)
2008 if (req->r_callback)
2009 req->r_callback(mdsc, req);
2010 else
2011 complete_all(&req->r_completion);
2015 * called under mdsc->mutex
2017 static int __prepare_send_request(struct ceph_mds_client *mdsc,
2018 struct ceph_mds_request *req,
2019 int mds, bool drop_cap_releases)
2021 struct ceph_mds_request_head *rhead;
2022 struct ceph_msg *msg;
2023 int flags = 0;
2025 req->r_attempts++;
2026 if (req->r_inode) {
2027 struct ceph_cap *cap =
2028 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2030 if (cap)
2031 req->r_sent_on_mseq = cap->mseq;
2032 else
2033 req->r_sent_on_mseq = -1;
2035 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2036 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2038 if (req->r_got_unsafe) {
2039 void *p;
2041 * Replay. Do not regenerate message (and rebuild
2042 * paths, etc.); just use the original message.
2043 * Rebuilding paths will break for renames because
2044 * d_move mangles the src name.
2046 msg = req->r_request;
2047 rhead = msg->front.iov_base;
2049 flags = le32_to_cpu(rhead->flags);
2050 flags |= CEPH_MDS_FLAG_REPLAY;
2051 rhead->flags = cpu_to_le32(flags);
2053 if (req->r_target_inode)
2054 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2056 rhead->num_retry = req->r_attempts - 1;
2058 /* remove cap/dentry releases from message */
2059 rhead->num_releases = 0;
2061 /* time stamp */
2062 p = msg->front.iov_base + req->r_request_release_offset;
2064 struct ceph_timespec ts;
2065 ceph_encode_timespec(&ts, &req->r_stamp);
2066 ceph_encode_copy(&p, &ts, sizeof(ts));
2069 msg->front.iov_len = p - msg->front.iov_base;
2070 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2071 return 0;
2074 if (req->r_request) {
2075 ceph_msg_put(req->r_request);
2076 req->r_request = NULL;
2078 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
2079 if (IS_ERR(msg)) {
2080 req->r_err = PTR_ERR(msg);
2081 complete_request(mdsc, req);
2082 return PTR_ERR(msg);
2084 req->r_request = msg;
2086 rhead = msg->front.iov_base;
2087 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
2088 if (req->r_got_unsafe)
2089 flags |= CEPH_MDS_FLAG_REPLAY;
2090 if (req->r_locked_dir)
2091 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2092 rhead->flags = cpu_to_le32(flags);
2093 rhead->num_fwd = req->r_num_fwd;
2094 rhead->num_retry = req->r_attempts - 1;
2095 rhead->ino = 0;
2097 dout(" r_locked_dir = %p\n", req->r_locked_dir);
2098 return 0;
2102 * send request, or put it on the appropriate wait list.
2104 static int __do_request(struct ceph_mds_client *mdsc,
2105 struct ceph_mds_request *req)
2107 struct ceph_mds_session *session = NULL;
2108 int mds = -1;
2109 int err = -EAGAIN;
2111 if (req->r_err || req->r_got_result) {
2112 if (req->r_aborted)
2113 __unregister_request(mdsc, req);
2114 goto out;
2117 if (req->r_timeout &&
2118 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2119 dout("do_request timed out\n");
2120 err = -EIO;
2121 goto finish;
2124 put_request_session(req);
2126 mds = __choose_mds(mdsc, req);
2127 if (mds < 0 ||
2128 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2129 dout("do_request no mds or not active, waiting for map\n");
2130 list_add(&req->r_wait, &mdsc->waiting_for_map);
2131 goto out;
2134 /* get, open session */
2135 session = __ceph_lookup_mds_session(mdsc, mds);
2136 if (!session) {
2137 session = register_session(mdsc, mds);
2138 if (IS_ERR(session)) {
2139 err = PTR_ERR(session);
2140 goto finish;
2143 req->r_session = get_session(session);
2145 dout("do_request mds%d session %p state %s\n", mds, session,
2146 ceph_session_state_name(session->s_state));
2147 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2148 session->s_state != CEPH_MDS_SESSION_HUNG) {
2149 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2150 session->s_state == CEPH_MDS_SESSION_CLOSING)
2151 __open_session(mdsc, session);
2152 list_add(&req->r_wait, &session->s_waiting);
2153 goto out_session;
2156 /* send request */
2157 req->r_resend_mds = -1; /* forget any previous mds hint */
2159 if (req->r_request_started == 0) /* note request start time */
2160 req->r_request_started = jiffies;
2162 err = __prepare_send_request(mdsc, req, mds, false);
2163 if (!err) {
2164 ceph_msg_get(req->r_request);
2165 ceph_con_send(&session->s_con, req->r_request);
2168 out_session:
2169 ceph_put_mds_session(session);
2170 out:
2171 return err;
2173 finish:
2174 req->r_err = err;
2175 complete_request(mdsc, req);
2176 goto out;
2180 * called under mdsc->mutex
2182 static void __wake_requests(struct ceph_mds_client *mdsc,
2183 struct list_head *head)
2185 struct ceph_mds_request *req;
2186 LIST_HEAD(tmp_list);
2188 list_splice_init(head, &tmp_list);
2190 while (!list_empty(&tmp_list)) {
2191 req = list_entry(tmp_list.next,
2192 struct ceph_mds_request, r_wait);
2193 list_del_init(&req->r_wait);
2194 dout(" wake request %p tid %llu\n", req, req->r_tid);
2195 __do_request(mdsc, req);
2200 * Wake up threads with requests pending for @mds, so that they can
2201 * resubmit their requests to a possibly different mds.
2203 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
2205 struct ceph_mds_request *req;
2206 struct rb_node *p = rb_first(&mdsc->request_tree);
2208 dout("kick_requests mds%d\n", mds);
2209 while (p) {
2210 req = rb_entry(p, struct ceph_mds_request, r_node);
2211 p = rb_next(p);
2212 if (req->r_got_unsafe)
2213 continue;
2214 if (req->r_attempts > 0)
2215 continue; /* only new requests */
2216 if (req->r_session &&
2217 req->r_session->s_mds == mds) {
2218 dout(" kicking tid %llu\n", req->r_tid);
2219 list_del_init(&req->r_wait);
2220 __do_request(mdsc, req);
2225 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2226 struct ceph_mds_request *req)
2228 dout("submit_request on %p\n", req);
2229 mutex_lock(&mdsc->mutex);
2230 __register_request(mdsc, req, NULL);
2231 __do_request(mdsc, req);
2232 mutex_unlock(&mdsc->mutex);
2236 * Synchrously perform an mds request. Take care of all of the
2237 * session setup, forwarding, retry details.
2239 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2240 struct inode *dir,
2241 struct ceph_mds_request *req)
2243 int err;
2245 dout("do_request on %p\n", req);
2247 /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */
2248 if (req->r_inode)
2249 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
2250 if (req->r_locked_dir)
2251 ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN);
2252 if (req->r_old_dentry_dir)
2253 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2254 CEPH_CAP_PIN);
2256 /* issue */
2257 mutex_lock(&mdsc->mutex);
2258 __register_request(mdsc, req, dir);
2259 __do_request(mdsc, req);
2261 if (req->r_err) {
2262 err = req->r_err;
2263 __unregister_request(mdsc, req);
2264 dout("do_request early error %d\n", err);
2265 goto out;
2268 /* wait */
2269 mutex_unlock(&mdsc->mutex);
2270 dout("do_request waiting\n");
2271 if (req->r_timeout) {
2272 err = (long)wait_for_completion_killable_timeout(
2273 &req->r_completion, req->r_timeout);
2274 if (err == 0)
2275 err = -EIO;
2276 } else if (req->r_wait_for_completion) {
2277 err = req->r_wait_for_completion(mdsc, req);
2278 } else {
2279 err = wait_for_completion_killable(&req->r_completion);
2281 dout("do_request waited, got %d\n", err);
2282 mutex_lock(&mdsc->mutex);
2284 /* only abort if we didn't race with a real reply */
2285 if (req->r_got_result) {
2286 err = le32_to_cpu(req->r_reply_info.head->result);
2287 } else if (err < 0) {
2288 dout("aborted request %lld with %d\n", req->r_tid, err);
2291 * ensure we aren't running concurrently with
2292 * ceph_fill_trace or ceph_readdir_prepopulate, which
2293 * rely on locks (dir mutex) held by our caller.
2295 mutex_lock(&req->r_fill_mutex);
2296 req->r_err = err;
2297 req->r_aborted = true;
2298 mutex_unlock(&req->r_fill_mutex);
2300 if (req->r_locked_dir &&
2301 (req->r_op & CEPH_MDS_OP_WRITE))
2302 ceph_invalidate_dir_request(req);
2303 } else {
2304 err = req->r_err;
2307 out:
2308 mutex_unlock(&mdsc->mutex);
2309 dout("do_request %p done, result %d\n", req, err);
2310 return err;
2314 * Invalidate dir's completeness, dentry lease state on an aborted MDS
2315 * namespace request.
2317 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2319 struct inode *inode = req->r_locked_dir;
2321 dout("invalidate_dir_request %p (complete, lease(s))\n", inode);
2323 ceph_dir_clear_complete(inode);
2324 if (req->r_dentry)
2325 ceph_invalidate_dentry_lease(req->r_dentry);
2326 if (req->r_old_dentry)
2327 ceph_invalidate_dentry_lease(req->r_old_dentry);
2331 * Handle mds reply.
2333 * We take the session mutex and parse and process the reply immediately.
2334 * This preserves the logical ordering of replies, capabilities, etc., sent
2335 * by the MDS as they are applied to our local cache.
2337 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2339 struct ceph_mds_client *mdsc = session->s_mdsc;
2340 struct ceph_mds_request *req;
2341 struct ceph_mds_reply_head *head = msg->front.iov_base;
2342 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2343 struct ceph_snap_realm *realm;
2344 u64 tid;
2345 int err, result;
2346 int mds = session->s_mds;
2348 if (msg->front.iov_len < sizeof(*head)) {
2349 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2350 ceph_msg_dump(msg);
2351 return;
2354 /* get request, session */
2355 tid = le64_to_cpu(msg->hdr.tid);
2356 mutex_lock(&mdsc->mutex);
2357 req = __lookup_request(mdsc, tid);
2358 if (!req) {
2359 dout("handle_reply on unknown tid %llu\n", tid);
2360 mutex_unlock(&mdsc->mutex);
2361 return;
2363 dout("handle_reply %p\n", req);
2365 /* correct session? */
2366 if (req->r_session != session) {
2367 pr_err("mdsc_handle_reply got %llu on session mds%d"
2368 " not mds%d\n", tid, session->s_mds,
2369 req->r_session ? req->r_session->s_mds : -1);
2370 mutex_unlock(&mdsc->mutex);
2371 goto out;
2374 /* dup? */
2375 if ((req->r_got_unsafe && !head->safe) ||
2376 (req->r_got_safe && head->safe)) {
2377 pr_warn("got a dup %s reply on %llu from mds%d\n",
2378 head->safe ? "safe" : "unsafe", tid, mds);
2379 mutex_unlock(&mdsc->mutex);
2380 goto out;
2382 if (req->r_got_safe && !head->safe) {
2383 pr_warn("got unsafe after safe on %llu from mds%d\n",
2384 tid, mds);
2385 mutex_unlock(&mdsc->mutex);
2386 goto out;
2389 result = le32_to_cpu(head->result);
2392 * Handle an ESTALE
2393 * if we're not talking to the authority, send to them
2394 * if the authority has changed while we weren't looking,
2395 * send to new authority
2396 * Otherwise we just have to return an ESTALE
2398 if (result == -ESTALE) {
2399 dout("got ESTALE on request %llu", req->r_tid);
2400 req->r_resend_mds = -1;
2401 if (req->r_direct_mode != USE_AUTH_MDS) {
2402 dout("not using auth, setting for that now");
2403 req->r_direct_mode = USE_AUTH_MDS;
2404 __do_request(mdsc, req);
2405 mutex_unlock(&mdsc->mutex);
2406 goto out;
2407 } else {
2408 int mds = __choose_mds(mdsc, req);
2409 if (mds >= 0 && mds != req->r_session->s_mds) {
2410 dout("but auth changed, so resending");
2411 __do_request(mdsc, req);
2412 mutex_unlock(&mdsc->mutex);
2413 goto out;
2416 dout("have to return ESTALE on request %llu", req->r_tid);
2420 if (head->safe) {
2421 req->r_got_safe = true;
2422 __unregister_request(mdsc, req);
2424 if (req->r_got_unsafe) {
2426 * We already handled the unsafe response, now do the
2427 * cleanup. No need to examine the response; the MDS
2428 * doesn't include any result info in the safe
2429 * response. And even if it did, there is nothing
2430 * useful we could do with a revised return value.
2432 dout("got safe reply %llu, mds%d\n", tid, mds);
2433 list_del_init(&req->r_unsafe_item);
2435 /* last unsafe request during umount? */
2436 if (mdsc->stopping && !__get_oldest_req(mdsc))
2437 complete_all(&mdsc->safe_umount_waiters);
2438 mutex_unlock(&mdsc->mutex);
2439 goto out;
2441 } else {
2442 req->r_got_unsafe = true;
2443 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2446 dout("handle_reply tid %lld result %d\n", tid, result);
2447 rinfo = &req->r_reply_info;
2448 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2449 mutex_unlock(&mdsc->mutex);
2451 mutex_lock(&session->s_mutex);
2452 if (err < 0) {
2453 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
2454 ceph_msg_dump(msg);
2455 goto out_err;
2458 /* snap trace */
2459 realm = NULL;
2460 if (rinfo->snapblob_len) {
2461 down_write(&mdsc->snap_rwsem);
2462 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2463 rinfo->snapblob + rinfo->snapblob_len,
2464 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2465 &realm);
2466 downgrade_write(&mdsc->snap_rwsem);
2467 } else {
2468 down_read(&mdsc->snap_rwsem);
2471 /* insert trace into our cache */
2472 mutex_lock(&req->r_fill_mutex);
2473 err = ceph_fill_trace(mdsc->fsc->sb, req, req->r_session);
2474 if (err == 0) {
2475 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
2476 req->r_op == CEPH_MDS_OP_LSSNAP))
2477 ceph_readdir_prepopulate(req, req->r_session);
2478 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2480 mutex_unlock(&req->r_fill_mutex);
2482 up_read(&mdsc->snap_rwsem);
2483 if (realm)
2484 ceph_put_snap_realm(mdsc, realm);
2485 out_err:
2486 mutex_lock(&mdsc->mutex);
2487 if (!req->r_aborted) {
2488 if (err) {
2489 req->r_err = err;
2490 } else {
2491 req->r_reply = msg;
2492 ceph_msg_get(msg);
2493 req->r_got_result = true;
2495 } else {
2496 dout("reply arrived after request %lld was aborted\n", tid);
2498 mutex_unlock(&mdsc->mutex);
2500 ceph_add_cap_releases(mdsc, req->r_session);
2501 mutex_unlock(&session->s_mutex);
2503 /* kick calling process */
2504 complete_request(mdsc, req);
2505 out:
2506 ceph_mdsc_put_request(req);
2507 return;
2513 * handle mds notification that our request has been forwarded.
2515 static void handle_forward(struct ceph_mds_client *mdsc,
2516 struct ceph_mds_session *session,
2517 struct ceph_msg *msg)
2519 struct ceph_mds_request *req;
2520 u64 tid = le64_to_cpu(msg->hdr.tid);
2521 u32 next_mds;
2522 u32 fwd_seq;
2523 int err = -EINVAL;
2524 void *p = msg->front.iov_base;
2525 void *end = p + msg->front.iov_len;
2527 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2528 next_mds = ceph_decode_32(&p);
2529 fwd_seq = ceph_decode_32(&p);
2531 mutex_lock(&mdsc->mutex);
2532 req = __lookup_request(mdsc, tid);
2533 if (!req) {
2534 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2535 goto out; /* dup reply? */
2538 if (req->r_aborted) {
2539 dout("forward tid %llu aborted, unregistering\n", tid);
2540 __unregister_request(mdsc, req);
2541 } else if (fwd_seq <= req->r_num_fwd) {
2542 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2543 tid, next_mds, req->r_num_fwd, fwd_seq);
2544 } else {
2545 /* resend. forward race not possible; mds would drop */
2546 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2547 BUG_ON(req->r_err);
2548 BUG_ON(req->r_got_result);
2549 req->r_attempts = 0;
2550 req->r_num_fwd = fwd_seq;
2551 req->r_resend_mds = next_mds;
2552 put_request_session(req);
2553 __do_request(mdsc, req);
2555 ceph_mdsc_put_request(req);
2556 out:
2557 mutex_unlock(&mdsc->mutex);
2558 return;
2560 bad:
2561 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2565 * handle a mds session control message
2567 static void handle_session(struct ceph_mds_session *session,
2568 struct ceph_msg *msg)
2570 struct ceph_mds_client *mdsc = session->s_mdsc;
2571 u32 op;
2572 u64 seq;
2573 int mds = session->s_mds;
2574 struct ceph_mds_session_head *h = msg->front.iov_base;
2575 int wake = 0;
2577 /* decode */
2578 if (msg->front.iov_len != sizeof(*h))
2579 goto bad;
2580 op = le32_to_cpu(h->op);
2581 seq = le64_to_cpu(h->seq);
2583 mutex_lock(&mdsc->mutex);
2584 if (op == CEPH_SESSION_CLOSE)
2585 __unregister_session(mdsc, session);
2586 /* FIXME: this ttl calculation is generous */
2587 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2588 mutex_unlock(&mdsc->mutex);
2590 mutex_lock(&session->s_mutex);
2592 dout("handle_session mds%d %s %p state %s seq %llu\n",
2593 mds, ceph_session_op_name(op), session,
2594 ceph_session_state_name(session->s_state), seq);
2596 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2597 session->s_state = CEPH_MDS_SESSION_OPEN;
2598 pr_info("mds%d came back\n", session->s_mds);
2601 switch (op) {
2602 case CEPH_SESSION_OPEN:
2603 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2604 pr_info("mds%d reconnect success\n", session->s_mds);
2605 session->s_state = CEPH_MDS_SESSION_OPEN;
2606 renewed_caps(mdsc, session, 0);
2607 wake = 1;
2608 if (mdsc->stopping)
2609 __close_session(mdsc, session);
2610 break;
2612 case CEPH_SESSION_RENEWCAPS:
2613 if (session->s_renew_seq == seq)
2614 renewed_caps(mdsc, session, 1);
2615 break;
2617 case CEPH_SESSION_CLOSE:
2618 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2619 pr_info("mds%d reconnect denied\n", session->s_mds);
2620 cleanup_session_requests(mdsc, session);
2621 remove_session_caps(session);
2622 wake = 2; /* for good measure */
2623 wake_up_all(&mdsc->session_close_wq);
2624 break;
2626 case CEPH_SESSION_STALE:
2627 pr_info("mds%d caps went stale, renewing\n",
2628 session->s_mds);
2629 spin_lock(&session->s_gen_ttl_lock);
2630 session->s_cap_gen++;
2631 session->s_cap_ttl = jiffies - 1;
2632 spin_unlock(&session->s_gen_ttl_lock);
2633 send_renew_caps(mdsc, session);
2634 break;
2636 case CEPH_SESSION_RECALL_STATE:
2637 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2638 break;
2640 case CEPH_SESSION_FLUSHMSG:
2641 send_flushmsg_ack(mdsc, session, seq);
2642 break;
2644 case CEPH_SESSION_FORCE_RO:
2645 dout("force_session_readonly %p\n", session);
2646 spin_lock(&session->s_cap_lock);
2647 session->s_readonly = true;
2648 spin_unlock(&session->s_cap_lock);
2649 wake_up_session_caps(session, 0);
2650 break;
2652 default:
2653 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2654 WARN_ON(1);
2657 mutex_unlock(&session->s_mutex);
2658 if (wake) {
2659 mutex_lock(&mdsc->mutex);
2660 __wake_requests(mdsc, &session->s_waiting);
2661 if (wake == 2)
2662 kick_requests(mdsc, mds);
2663 mutex_unlock(&mdsc->mutex);
2665 return;
2667 bad:
2668 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2669 (int)msg->front.iov_len);
2670 ceph_msg_dump(msg);
2671 return;
2676 * called under session->mutex.
2678 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2679 struct ceph_mds_session *session)
2681 struct ceph_mds_request *req, *nreq;
2682 struct rb_node *p;
2683 int err;
2685 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2687 mutex_lock(&mdsc->mutex);
2688 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2689 err = __prepare_send_request(mdsc, req, session->s_mds, true);
2690 if (!err) {
2691 ceph_msg_get(req->r_request);
2692 ceph_con_send(&session->s_con, req->r_request);
2697 * also re-send old requests when MDS enters reconnect stage. So that MDS
2698 * can process completed request in clientreplay stage.
2700 p = rb_first(&mdsc->request_tree);
2701 while (p) {
2702 req = rb_entry(p, struct ceph_mds_request, r_node);
2703 p = rb_next(p);
2704 if (req->r_got_unsafe)
2705 continue;
2706 if (req->r_attempts == 0)
2707 continue; /* only old requests */
2708 if (req->r_session &&
2709 req->r_session->s_mds == session->s_mds) {
2710 err = __prepare_send_request(mdsc, req,
2711 session->s_mds, true);
2712 if (!err) {
2713 ceph_msg_get(req->r_request);
2714 ceph_con_send(&session->s_con, req->r_request);
2718 mutex_unlock(&mdsc->mutex);
2722 * Encode information about a cap for a reconnect with the MDS.
2724 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2725 void *arg)
2727 union {
2728 struct ceph_mds_cap_reconnect v2;
2729 struct ceph_mds_cap_reconnect_v1 v1;
2730 } rec;
2731 size_t reclen;
2732 struct ceph_inode_info *ci;
2733 struct ceph_reconnect_state *recon_state = arg;
2734 struct ceph_pagelist *pagelist = recon_state->pagelist;
2735 char *path;
2736 int pathlen, err;
2737 u64 pathbase;
2738 struct dentry *dentry;
2740 ci = cap->ci;
2742 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2743 inode, ceph_vinop(inode), cap, cap->cap_id,
2744 ceph_cap_string(cap->issued));
2745 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2746 if (err)
2747 return err;
2749 dentry = d_find_alias(inode);
2750 if (dentry) {
2751 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2752 if (IS_ERR(path)) {
2753 err = PTR_ERR(path);
2754 goto out_dput;
2756 } else {
2757 path = NULL;
2758 pathlen = 0;
2760 err = ceph_pagelist_encode_string(pagelist, path, pathlen);
2761 if (err)
2762 goto out_free;
2764 spin_lock(&ci->i_ceph_lock);
2765 cap->seq = 0; /* reset cap seq */
2766 cap->issue_seq = 0; /* and issue_seq */
2767 cap->mseq = 0; /* and migrate_seq */
2768 cap->cap_gen = cap->session->s_cap_gen;
2770 if (recon_state->flock) {
2771 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2772 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2773 rec.v2.issued = cpu_to_le32(cap->issued);
2774 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2775 rec.v2.pathbase = cpu_to_le64(pathbase);
2776 rec.v2.flock_len = 0;
2777 reclen = sizeof(rec.v2);
2778 } else {
2779 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2780 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2781 rec.v1.issued = cpu_to_le32(cap->issued);
2782 rec.v1.size = cpu_to_le64(inode->i_size);
2783 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2784 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2785 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2786 rec.v1.pathbase = cpu_to_le64(pathbase);
2787 reclen = sizeof(rec.v1);
2789 spin_unlock(&ci->i_ceph_lock);
2791 if (recon_state->flock) {
2792 int num_fcntl_locks, num_flock_locks;
2793 struct ceph_filelock *flocks;
2795 encode_again:
2796 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
2797 flocks = kmalloc((num_fcntl_locks+num_flock_locks) *
2798 sizeof(struct ceph_filelock), GFP_NOFS);
2799 if (!flocks) {
2800 err = -ENOMEM;
2801 goto out_free;
2803 err = ceph_encode_locks_to_buffer(inode, flocks,
2804 num_fcntl_locks,
2805 num_flock_locks);
2806 if (err) {
2807 kfree(flocks);
2808 if (err == -ENOSPC)
2809 goto encode_again;
2810 goto out_free;
2813 * number of encoded locks is stable, so copy to pagelist
2815 rec.v2.flock_len = cpu_to_le32(2*sizeof(u32) +
2816 (num_fcntl_locks+num_flock_locks) *
2817 sizeof(struct ceph_filelock));
2818 err = ceph_pagelist_append(pagelist, &rec, reclen);
2819 if (!err)
2820 err = ceph_locks_to_pagelist(flocks, pagelist,
2821 num_fcntl_locks,
2822 num_flock_locks);
2823 kfree(flocks);
2824 } else {
2825 err = ceph_pagelist_append(pagelist, &rec, reclen);
2828 recon_state->nr_caps++;
2829 out_free:
2830 kfree(path);
2831 out_dput:
2832 dput(dentry);
2833 return err;
2838 * If an MDS fails and recovers, clients need to reconnect in order to
2839 * reestablish shared state. This includes all caps issued through
2840 * this session _and_ the snap_realm hierarchy. Because it's not
2841 * clear which snap realms the mds cares about, we send everything we
2842 * know about.. that ensures we'll then get any new info the
2843 * recovering MDS might have.
2845 * This is a relatively heavyweight operation, but it's rare.
2847 * called with mdsc->mutex held.
2849 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
2850 struct ceph_mds_session *session)
2852 struct ceph_msg *reply;
2853 struct rb_node *p;
2854 int mds = session->s_mds;
2855 int err = -ENOMEM;
2856 int s_nr_caps;
2857 struct ceph_pagelist *pagelist;
2858 struct ceph_reconnect_state recon_state;
2860 pr_info("mds%d reconnect start\n", mds);
2862 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
2863 if (!pagelist)
2864 goto fail_nopagelist;
2865 ceph_pagelist_init(pagelist);
2867 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
2868 if (!reply)
2869 goto fail_nomsg;
2871 mutex_lock(&session->s_mutex);
2872 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
2873 session->s_seq = 0;
2875 dout("session %p state %s\n", session,
2876 ceph_session_state_name(session->s_state));
2878 spin_lock(&session->s_gen_ttl_lock);
2879 session->s_cap_gen++;
2880 spin_unlock(&session->s_gen_ttl_lock);
2882 spin_lock(&session->s_cap_lock);
2883 /* don't know if session is readonly */
2884 session->s_readonly = 0;
2886 * notify __ceph_remove_cap() that we are composing cap reconnect.
2887 * If a cap get released before being added to the cap reconnect,
2888 * __ceph_remove_cap() should skip queuing cap release.
2890 session->s_cap_reconnect = 1;
2891 /* drop old cap expires; we're about to reestablish that state */
2892 discard_cap_releases(mdsc, session);
2893 spin_unlock(&session->s_cap_lock);
2895 /* trim unused caps to reduce MDS's cache rejoin time */
2896 if (mdsc->fsc->sb->s_root)
2897 shrink_dcache_parent(mdsc->fsc->sb->s_root);
2899 ceph_con_close(&session->s_con);
2900 ceph_con_open(&session->s_con,
2901 CEPH_ENTITY_TYPE_MDS, mds,
2902 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
2904 /* replay unsafe requests */
2905 replay_unsafe_requests(mdsc, session);
2907 down_read(&mdsc->snap_rwsem);
2909 /* traverse this session's caps */
2910 s_nr_caps = session->s_nr_caps;
2911 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
2912 if (err)
2913 goto fail;
2915 recon_state.nr_caps = 0;
2916 recon_state.pagelist = pagelist;
2917 recon_state.flock = session->s_con.peer_features & CEPH_FEATURE_FLOCK;
2918 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
2919 if (err < 0)
2920 goto fail;
2922 spin_lock(&session->s_cap_lock);
2923 session->s_cap_reconnect = 0;
2924 spin_unlock(&session->s_cap_lock);
2927 * snaprealms. we provide mds with the ino, seq (version), and
2928 * parent for all of our realms. If the mds has any newer info,
2929 * it will tell us.
2931 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
2932 struct ceph_snap_realm *realm =
2933 rb_entry(p, struct ceph_snap_realm, node);
2934 struct ceph_mds_snaprealm_reconnect sr_rec;
2936 dout(" adding snap realm %llx seq %lld parent %llx\n",
2937 realm->ino, realm->seq, realm->parent_ino);
2938 sr_rec.ino = cpu_to_le64(realm->ino);
2939 sr_rec.seq = cpu_to_le64(realm->seq);
2940 sr_rec.parent = cpu_to_le64(realm->parent_ino);
2941 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
2942 if (err)
2943 goto fail;
2946 if (recon_state.flock)
2947 reply->hdr.version = cpu_to_le16(2);
2949 /* raced with cap release? */
2950 if (s_nr_caps != recon_state.nr_caps) {
2951 struct page *page = list_first_entry(&pagelist->head,
2952 struct page, lru);
2953 __le32 *addr = kmap_atomic(page);
2954 *addr = cpu_to_le32(recon_state.nr_caps);
2955 kunmap_atomic(addr);
2958 reply->hdr.data_len = cpu_to_le32(pagelist->length);
2959 ceph_msg_data_add_pagelist(reply, pagelist);
2960 ceph_con_send(&session->s_con, reply);
2962 mutex_unlock(&session->s_mutex);
2964 mutex_lock(&mdsc->mutex);
2965 __wake_requests(mdsc, &session->s_waiting);
2966 mutex_unlock(&mdsc->mutex);
2968 up_read(&mdsc->snap_rwsem);
2969 return;
2971 fail:
2972 ceph_msg_put(reply);
2973 up_read(&mdsc->snap_rwsem);
2974 mutex_unlock(&session->s_mutex);
2975 fail_nomsg:
2976 ceph_pagelist_release(pagelist);
2977 fail_nopagelist:
2978 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
2979 return;
2984 * compare old and new mdsmaps, kicking requests
2985 * and closing out old connections as necessary
2987 * called under mdsc->mutex.
2989 static void check_new_map(struct ceph_mds_client *mdsc,
2990 struct ceph_mdsmap *newmap,
2991 struct ceph_mdsmap *oldmap)
2993 int i;
2994 int oldstate, newstate;
2995 struct ceph_mds_session *s;
2997 dout("check_new_map new %u old %u\n",
2998 newmap->m_epoch, oldmap->m_epoch);
3000 for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) {
3001 if (mdsc->sessions[i] == NULL)
3002 continue;
3003 s = mdsc->sessions[i];
3004 oldstate = ceph_mdsmap_get_state(oldmap, i);
3005 newstate = ceph_mdsmap_get_state(newmap, i);
3007 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
3008 i, ceph_mds_state_name(oldstate),
3009 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
3010 ceph_mds_state_name(newstate),
3011 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
3012 ceph_session_state_name(s->s_state));
3014 if (i >= newmap->m_max_mds ||
3015 memcmp(ceph_mdsmap_get_addr(oldmap, i),
3016 ceph_mdsmap_get_addr(newmap, i),
3017 sizeof(struct ceph_entity_addr))) {
3018 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
3019 /* the session never opened, just close it
3020 * out now */
3021 __wake_requests(mdsc, &s->s_waiting);
3022 __unregister_session(mdsc, s);
3023 } else {
3024 /* just close it */
3025 mutex_unlock(&mdsc->mutex);
3026 mutex_lock(&s->s_mutex);
3027 mutex_lock(&mdsc->mutex);
3028 ceph_con_close(&s->s_con);
3029 mutex_unlock(&s->s_mutex);
3030 s->s_state = CEPH_MDS_SESSION_RESTARTING;
3032 } else if (oldstate == newstate) {
3033 continue; /* nothing new with this mds */
3037 * send reconnect?
3039 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
3040 newstate >= CEPH_MDS_STATE_RECONNECT) {
3041 mutex_unlock(&mdsc->mutex);
3042 send_mds_reconnect(mdsc, s);
3043 mutex_lock(&mdsc->mutex);
3047 * kick request on any mds that has gone active.
3049 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3050 newstate >= CEPH_MDS_STATE_ACTIVE) {
3051 if (oldstate != CEPH_MDS_STATE_CREATING &&
3052 oldstate != CEPH_MDS_STATE_STARTING)
3053 pr_info("mds%d recovery completed\n", s->s_mds);
3054 kick_requests(mdsc, i);
3055 ceph_kick_flushing_caps(mdsc, s);
3056 wake_up_session_caps(s, 1);
3060 for (i = 0; i < newmap->m_max_mds && i < mdsc->max_sessions; i++) {
3061 s = mdsc->sessions[i];
3062 if (!s)
3063 continue;
3064 if (!ceph_mdsmap_is_laggy(newmap, i))
3065 continue;
3066 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3067 s->s_state == CEPH_MDS_SESSION_HUNG ||
3068 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3069 dout(" connecting to export targets of laggy mds%d\n",
3071 __open_export_target_sessions(mdsc, s);
3079 * leases
3083 * caller must hold session s_mutex, dentry->d_lock
3085 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3087 struct ceph_dentry_info *di = ceph_dentry(dentry);
3089 ceph_put_mds_session(di->lease_session);
3090 di->lease_session = NULL;
3093 static void handle_lease(struct ceph_mds_client *mdsc,
3094 struct ceph_mds_session *session,
3095 struct ceph_msg *msg)
3097 struct super_block *sb = mdsc->fsc->sb;
3098 struct inode *inode;
3099 struct dentry *parent, *dentry;
3100 struct ceph_dentry_info *di;
3101 int mds = session->s_mds;
3102 struct ceph_mds_lease *h = msg->front.iov_base;
3103 u32 seq;
3104 struct ceph_vino vino;
3105 struct qstr dname;
3106 int release = 0;
3108 dout("handle_lease from mds%d\n", mds);
3110 /* decode */
3111 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3112 goto bad;
3113 vino.ino = le64_to_cpu(h->ino);
3114 vino.snap = CEPH_NOSNAP;
3115 seq = le32_to_cpu(h->seq);
3116 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
3117 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
3118 if (dname.len != get_unaligned_le32(h+1))
3119 goto bad;
3121 /* lookup inode */
3122 inode = ceph_find_inode(sb, vino);
3123 dout("handle_lease %s, ino %llx %p %.*s\n",
3124 ceph_lease_op_name(h->action), vino.ino, inode,
3125 dname.len, dname.name);
3127 mutex_lock(&session->s_mutex);
3128 session->s_seq++;
3130 if (inode == NULL) {
3131 dout("handle_lease no inode %llx\n", vino.ino);
3132 goto release;
3135 /* dentry */
3136 parent = d_find_alias(inode);
3137 if (!parent) {
3138 dout("no parent dentry on inode %p\n", inode);
3139 WARN_ON(1);
3140 goto release; /* hrm... */
3142 dname.hash = full_name_hash(dname.name, dname.len);
3143 dentry = d_lookup(parent, &dname);
3144 dput(parent);
3145 if (!dentry)
3146 goto release;
3148 spin_lock(&dentry->d_lock);
3149 di = ceph_dentry(dentry);
3150 switch (h->action) {
3151 case CEPH_MDS_LEASE_REVOKE:
3152 if (di->lease_session == session) {
3153 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3154 h->seq = cpu_to_le32(di->lease_seq);
3155 __ceph_mdsc_drop_dentry_lease(dentry);
3157 release = 1;
3158 break;
3160 case CEPH_MDS_LEASE_RENEW:
3161 if (di->lease_session == session &&
3162 di->lease_gen == session->s_cap_gen &&
3163 di->lease_renew_from &&
3164 di->lease_renew_after == 0) {
3165 unsigned long duration =
3166 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
3168 di->lease_seq = seq;
3169 dentry->d_time = di->lease_renew_from + duration;
3170 di->lease_renew_after = di->lease_renew_from +
3171 (duration >> 1);
3172 di->lease_renew_from = 0;
3174 break;
3176 spin_unlock(&dentry->d_lock);
3177 dput(dentry);
3179 if (!release)
3180 goto out;
3182 release:
3183 /* let's just reuse the same message */
3184 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3185 ceph_msg_get(msg);
3186 ceph_con_send(&session->s_con, msg);
3188 out:
3189 iput(inode);
3190 mutex_unlock(&session->s_mutex);
3191 return;
3193 bad:
3194 pr_err("corrupt lease message\n");
3195 ceph_msg_dump(msg);
3198 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3199 struct inode *inode,
3200 struct dentry *dentry, char action,
3201 u32 seq)
3203 struct ceph_msg *msg;
3204 struct ceph_mds_lease *lease;
3205 int len = sizeof(*lease) + sizeof(u32);
3206 int dnamelen = 0;
3208 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3209 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3210 dnamelen = dentry->d_name.len;
3211 len += dnamelen;
3213 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
3214 if (!msg)
3215 return;
3216 lease = msg->front.iov_base;
3217 lease->action = action;
3218 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3219 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3220 lease->seq = cpu_to_le32(seq);
3221 put_unaligned_le32(dnamelen, lease + 1);
3222 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3225 * if this is a preemptive lease RELEASE, no need to
3226 * flush request stream, since the actual request will
3227 * soon follow.
3229 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3231 ceph_con_send(&session->s_con, msg);
3235 * Preemptively release a lease we expect to invalidate anyway.
3236 * Pass @inode always, @dentry is optional.
3238 void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode,
3239 struct dentry *dentry)
3241 struct ceph_dentry_info *di;
3242 struct ceph_mds_session *session;
3243 u32 seq;
3245 BUG_ON(inode == NULL);
3246 BUG_ON(dentry == NULL);
3248 /* is dentry lease valid? */
3249 spin_lock(&dentry->d_lock);
3250 di = ceph_dentry(dentry);
3251 if (!di || !di->lease_session ||
3252 di->lease_session->s_mds < 0 ||
3253 di->lease_gen != di->lease_session->s_cap_gen ||
3254 !time_before(jiffies, dentry->d_time)) {
3255 dout("lease_release inode %p dentry %p -- "
3256 "no lease\n",
3257 inode, dentry);
3258 spin_unlock(&dentry->d_lock);
3259 return;
3262 /* we do have a lease on this dentry; note mds and seq */
3263 session = ceph_get_mds_session(di->lease_session);
3264 seq = di->lease_seq;
3265 __ceph_mdsc_drop_dentry_lease(dentry);
3266 spin_unlock(&dentry->d_lock);
3268 dout("lease_release inode %p dentry %p to mds%d\n",
3269 inode, dentry, session->s_mds);
3270 ceph_mdsc_lease_send_msg(session, inode, dentry,
3271 CEPH_MDS_LEASE_RELEASE, seq);
3272 ceph_put_mds_session(session);
3276 * drop all leases (and dentry refs) in preparation for umount
3278 static void drop_leases(struct ceph_mds_client *mdsc)
3280 int i;
3282 dout("drop_leases\n");
3283 mutex_lock(&mdsc->mutex);
3284 for (i = 0; i < mdsc->max_sessions; i++) {
3285 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3286 if (!s)
3287 continue;
3288 mutex_unlock(&mdsc->mutex);
3289 mutex_lock(&s->s_mutex);
3290 mutex_unlock(&s->s_mutex);
3291 ceph_put_mds_session(s);
3292 mutex_lock(&mdsc->mutex);
3294 mutex_unlock(&mdsc->mutex);
3300 * delayed work -- periodically trim expired leases, renew caps with mds
3302 static void schedule_delayed(struct ceph_mds_client *mdsc)
3304 int delay = 5;
3305 unsigned hz = round_jiffies_relative(HZ * delay);
3306 schedule_delayed_work(&mdsc->delayed_work, hz);
3309 static void delayed_work(struct work_struct *work)
3311 int i;
3312 struct ceph_mds_client *mdsc =
3313 container_of(work, struct ceph_mds_client, delayed_work.work);
3314 int renew_interval;
3315 int renew_caps;
3317 dout("mdsc delayed_work\n");
3318 ceph_check_delayed_caps(mdsc);
3320 mutex_lock(&mdsc->mutex);
3321 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3322 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3323 mdsc->last_renew_caps);
3324 if (renew_caps)
3325 mdsc->last_renew_caps = jiffies;
3327 for (i = 0; i < mdsc->max_sessions; i++) {
3328 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3329 if (s == NULL)
3330 continue;
3331 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3332 dout("resending session close request for mds%d\n",
3333 s->s_mds);
3334 request_close_session(mdsc, s);
3335 ceph_put_mds_session(s);
3336 continue;
3338 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3339 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3340 s->s_state = CEPH_MDS_SESSION_HUNG;
3341 pr_info("mds%d hung\n", s->s_mds);
3344 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3345 /* this mds is failed or recovering, just wait */
3346 ceph_put_mds_session(s);
3347 continue;
3349 mutex_unlock(&mdsc->mutex);
3351 mutex_lock(&s->s_mutex);
3352 if (renew_caps)
3353 send_renew_caps(mdsc, s);
3354 else
3355 ceph_con_keepalive(&s->s_con);
3356 ceph_add_cap_releases(mdsc, s);
3357 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3358 s->s_state == CEPH_MDS_SESSION_HUNG)
3359 ceph_send_cap_releases(mdsc, s);
3360 mutex_unlock(&s->s_mutex);
3361 ceph_put_mds_session(s);
3363 mutex_lock(&mdsc->mutex);
3365 mutex_unlock(&mdsc->mutex);
3367 schedule_delayed(mdsc);
3370 int ceph_mdsc_init(struct ceph_fs_client *fsc)
3373 struct ceph_mds_client *mdsc;
3375 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3376 if (!mdsc)
3377 return -ENOMEM;
3378 mdsc->fsc = fsc;
3379 fsc->mdsc = mdsc;
3380 mutex_init(&mdsc->mutex);
3381 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
3382 if (mdsc->mdsmap == NULL) {
3383 kfree(mdsc);
3384 return -ENOMEM;
3387 init_completion(&mdsc->safe_umount_waiters);
3388 init_waitqueue_head(&mdsc->session_close_wq);
3389 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3390 mdsc->sessions = NULL;
3391 atomic_set(&mdsc->num_sessions, 0);
3392 mdsc->max_sessions = 0;
3393 mdsc->stopping = 0;
3394 init_rwsem(&mdsc->snap_rwsem);
3395 mdsc->snap_realms = RB_ROOT;
3396 INIT_LIST_HEAD(&mdsc->snap_empty);
3397 spin_lock_init(&mdsc->snap_empty_lock);
3398 mdsc->last_tid = 0;
3399 mdsc->request_tree = RB_ROOT;
3400 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3401 mdsc->last_renew_caps = jiffies;
3402 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3403 spin_lock_init(&mdsc->cap_delay_lock);
3404 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3405 spin_lock_init(&mdsc->snap_flush_lock);
3406 mdsc->cap_flush_seq = 0;
3407 INIT_LIST_HEAD(&mdsc->cap_dirty);
3408 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
3409 mdsc->num_cap_flushing = 0;
3410 spin_lock_init(&mdsc->cap_dirty_lock);
3411 init_waitqueue_head(&mdsc->cap_flushing_wq);
3412 spin_lock_init(&mdsc->dentry_lru_lock);
3413 INIT_LIST_HEAD(&mdsc->dentry_lru);
3415 ceph_caps_init(mdsc);
3416 ceph_adjust_min_caps(mdsc, fsc->min_caps);
3418 return 0;
3422 * Wait for safe replies on open mds requests. If we time out, drop
3423 * all requests from the tree to avoid dangling dentry refs.
3425 static void wait_requests(struct ceph_mds_client *mdsc)
3427 struct ceph_mds_request *req;
3428 struct ceph_fs_client *fsc = mdsc->fsc;
3430 mutex_lock(&mdsc->mutex);
3431 if (__get_oldest_req(mdsc)) {
3432 mutex_unlock(&mdsc->mutex);
3434 dout("wait_requests waiting for requests\n");
3435 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
3436 fsc->client->options->mount_timeout * HZ);
3438 /* tear down remaining requests */
3439 mutex_lock(&mdsc->mutex);
3440 while ((req = __get_oldest_req(mdsc))) {
3441 dout("wait_requests timed out on tid %llu\n",
3442 req->r_tid);
3443 __unregister_request(mdsc, req);
3446 mutex_unlock(&mdsc->mutex);
3447 dout("wait_requests done\n");
3451 * called before mount is ro, and before dentries are torn down.
3452 * (hmm, does this still race with new lookups?)
3454 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3456 dout("pre_umount\n");
3457 mdsc->stopping = 1;
3459 drop_leases(mdsc);
3460 ceph_flush_dirty_caps(mdsc);
3461 wait_requests(mdsc);
3464 * wait for reply handlers to drop their request refs and
3465 * their inode/dcache refs
3467 ceph_msgr_flush();
3471 * wait for all write mds requests to flush.
3473 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3475 struct ceph_mds_request *req = NULL, *nextreq;
3476 struct rb_node *n;
3478 mutex_lock(&mdsc->mutex);
3479 dout("wait_unsafe_requests want %lld\n", want_tid);
3480 restart:
3481 req = __get_oldest_req(mdsc);
3482 while (req && req->r_tid <= want_tid) {
3483 /* find next request */
3484 n = rb_next(&req->r_node);
3485 if (n)
3486 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3487 else
3488 nextreq = NULL;
3489 if ((req->r_op & CEPH_MDS_OP_WRITE)) {
3490 /* write op */
3491 ceph_mdsc_get_request(req);
3492 if (nextreq)
3493 ceph_mdsc_get_request(nextreq);
3494 mutex_unlock(&mdsc->mutex);
3495 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3496 req->r_tid, want_tid);
3497 wait_for_completion(&req->r_safe_completion);
3498 mutex_lock(&mdsc->mutex);
3499 ceph_mdsc_put_request(req);
3500 if (!nextreq)
3501 break; /* next dne before, so we're done! */
3502 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3503 /* next request was removed from tree */
3504 ceph_mdsc_put_request(nextreq);
3505 goto restart;
3507 ceph_mdsc_put_request(nextreq); /* won't go away */
3509 req = nextreq;
3511 mutex_unlock(&mdsc->mutex);
3512 dout("wait_unsafe_requests done\n");
3515 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3517 u64 want_tid, want_flush;
3519 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3520 return;
3522 dout("sync\n");
3523 mutex_lock(&mdsc->mutex);
3524 want_tid = mdsc->last_tid;
3525 mutex_unlock(&mdsc->mutex);
3527 ceph_flush_dirty_caps(mdsc);
3528 spin_lock(&mdsc->cap_dirty_lock);
3529 want_flush = mdsc->cap_flush_seq;
3530 spin_unlock(&mdsc->cap_dirty_lock);
3532 dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
3534 wait_unsafe_requests(mdsc, want_tid);
3535 wait_caps_flush(mdsc, want_flush);
3539 * true if all sessions are closed, or we force unmount
3541 static bool done_closing_sessions(struct ceph_mds_client *mdsc)
3543 if (mdsc->fsc->mount_state == CEPH_MOUNT_SHUTDOWN)
3544 return true;
3545 return atomic_read(&mdsc->num_sessions) == 0;
3549 * called after sb is ro.
3551 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3553 struct ceph_mds_session *session;
3554 int i;
3555 struct ceph_fs_client *fsc = mdsc->fsc;
3556 unsigned long timeout = fsc->client->options->mount_timeout * HZ;
3558 dout("close_sessions\n");
3560 /* close sessions */
3561 mutex_lock(&mdsc->mutex);
3562 for (i = 0; i < mdsc->max_sessions; i++) {
3563 session = __ceph_lookup_mds_session(mdsc, i);
3564 if (!session)
3565 continue;
3566 mutex_unlock(&mdsc->mutex);
3567 mutex_lock(&session->s_mutex);
3568 __close_session(mdsc, session);
3569 mutex_unlock(&session->s_mutex);
3570 ceph_put_mds_session(session);
3571 mutex_lock(&mdsc->mutex);
3573 mutex_unlock(&mdsc->mutex);
3575 dout("waiting for sessions to close\n");
3576 wait_event_timeout(mdsc->session_close_wq, done_closing_sessions(mdsc),
3577 timeout);
3579 /* tear down remaining sessions */
3580 mutex_lock(&mdsc->mutex);
3581 for (i = 0; i < mdsc->max_sessions; i++) {
3582 if (mdsc->sessions[i]) {
3583 session = get_session(mdsc->sessions[i]);
3584 __unregister_session(mdsc, session);
3585 mutex_unlock(&mdsc->mutex);
3586 mutex_lock(&session->s_mutex);
3587 remove_session_caps(session);
3588 mutex_unlock(&session->s_mutex);
3589 ceph_put_mds_session(session);
3590 mutex_lock(&mdsc->mutex);
3593 WARN_ON(!list_empty(&mdsc->cap_delay_list));
3594 mutex_unlock(&mdsc->mutex);
3596 ceph_cleanup_empty_realms(mdsc);
3598 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3600 dout("stopped\n");
3603 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
3605 dout("stop\n");
3606 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3607 if (mdsc->mdsmap)
3608 ceph_mdsmap_destroy(mdsc->mdsmap);
3609 kfree(mdsc->sessions);
3610 ceph_caps_finalize(mdsc);
3613 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3615 struct ceph_mds_client *mdsc = fsc->mdsc;
3617 dout("mdsc_destroy %p\n", mdsc);
3618 ceph_mdsc_stop(mdsc);
3620 /* flush out any connection work with references to us */
3621 ceph_msgr_flush();
3623 fsc->mdsc = NULL;
3624 kfree(mdsc);
3625 dout("mdsc_destroy %p done\n", mdsc);
3630 * handle mds map update.
3632 void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3634 u32 epoch;
3635 u32 maplen;
3636 void *p = msg->front.iov_base;
3637 void *end = p + msg->front.iov_len;
3638 struct ceph_mdsmap *newmap, *oldmap;
3639 struct ceph_fsid fsid;
3640 int err = -EINVAL;
3642 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3643 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3644 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
3645 return;
3646 epoch = ceph_decode_32(&p);
3647 maplen = ceph_decode_32(&p);
3648 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3650 /* do we need it? */
3651 ceph_monc_got_mdsmap(&mdsc->fsc->client->monc, epoch);
3652 mutex_lock(&mdsc->mutex);
3653 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3654 dout("handle_map epoch %u <= our %u\n",
3655 epoch, mdsc->mdsmap->m_epoch);
3656 mutex_unlock(&mdsc->mutex);
3657 return;
3660 newmap = ceph_mdsmap_decode(&p, end);
3661 if (IS_ERR(newmap)) {
3662 err = PTR_ERR(newmap);
3663 goto bad_unlock;
3666 /* swap into place */
3667 if (mdsc->mdsmap) {
3668 oldmap = mdsc->mdsmap;
3669 mdsc->mdsmap = newmap;
3670 check_new_map(mdsc, newmap, oldmap);
3671 ceph_mdsmap_destroy(oldmap);
3672 } else {
3673 mdsc->mdsmap = newmap; /* first mds map */
3675 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
3677 __wake_requests(mdsc, &mdsc->waiting_for_map);
3679 mutex_unlock(&mdsc->mutex);
3680 schedule_delayed(mdsc);
3681 return;
3683 bad_unlock:
3684 mutex_unlock(&mdsc->mutex);
3685 bad:
3686 pr_err("error decoding mdsmap %d\n", err);
3687 return;
3690 static struct ceph_connection *con_get(struct ceph_connection *con)
3692 struct ceph_mds_session *s = con->private;
3694 if (get_session(s)) {
3695 dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref));
3696 return con;
3698 dout("mdsc con_get %p FAIL\n", s);
3699 return NULL;
3702 static void con_put(struct ceph_connection *con)
3704 struct ceph_mds_session *s = con->private;
3706 dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref) - 1);
3707 ceph_put_mds_session(s);
3711 * if the client is unresponsive for long enough, the mds will kill
3712 * the session entirely.
3714 static void peer_reset(struct ceph_connection *con)
3716 struct ceph_mds_session *s = con->private;
3717 struct ceph_mds_client *mdsc = s->s_mdsc;
3719 pr_warn("mds%d closed our session\n", s->s_mds);
3720 send_mds_reconnect(mdsc, s);
3723 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3725 struct ceph_mds_session *s = con->private;
3726 struct ceph_mds_client *mdsc = s->s_mdsc;
3727 int type = le16_to_cpu(msg->hdr.type);
3729 mutex_lock(&mdsc->mutex);
3730 if (__verify_registered_session(mdsc, s) < 0) {
3731 mutex_unlock(&mdsc->mutex);
3732 goto out;
3734 mutex_unlock(&mdsc->mutex);
3736 switch (type) {
3737 case CEPH_MSG_MDS_MAP:
3738 ceph_mdsc_handle_map(mdsc, msg);
3739 break;
3740 case CEPH_MSG_CLIENT_SESSION:
3741 handle_session(s, msg);
3742 break;
3743 case CEPH_MSG_CLIENT_REPLY:
3744 handle_reply(s, msg);
3745 break;
3746 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
3747 handle_forward(mdsc, s, msg);
3748 break;
3749 case CEPH_MSG_CLIENT_CAPS:
3750 ceph_handle_caps(s, msg);
3751 break;
3752 case CEPH_MSG_CLIENT_SNAP:
3753 ceph_handle_snap(mdsc, s, msg);
3754 break;
3755 case CEPH_MSG_CLIENT_LEASE:
3756 handle_lease(mdsc, s, msg);
3757 break;
3759 default:
3760 pr_err("received unknown message type %d %s\n", type,
3761 ceph_msg_type_name(type));
3763 out:
3764 ceph_msg_put(msg);
3768 * authentication
3772 * Note: returned pointer is the address of a structure that's
3773 * managed separately. Caller must *not* attempt to free it.
3775 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
3776 int *proto, int force_new)
3778 struct ceph_mds_session *s = con->private;
3779 struct ceph_mds_client *mdsc = s->s_mdsc;
3780 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3781 struct ceph_auth_handshake *auth = &s->s_auth;
3783 if (force_new && auth->authorizer) {
3784 ceph_auth_destroy_authorizer(ac, auth->authorizer);
3785 auth->authorizer = NULL;
3787 if (!auth->authorizer) {
3788 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3789 auth);
3790 if (ret)
3791 return ERR_PTR(ret);
3792 } else {
3793 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
3794 auth);
3795 if (ret)
3796 return ERR_PTR(ret);
3798 *proto = ac->protocol;
3800 return auth;
3804 static int verify_authorizer_reply(struct ceph_connection *con, int len)
3806 struct ceph_mds_session *s = con->private;
3807 struct ceph_mds_client *mdsc = s->s_mdsc;
3808 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3810 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer, len);
3813 static int invalidate_authorizer(struct ceph_connection *con)
3815 struct ceph_mds_session *s = con->private;
3816 struct ceph_mds_client *mdsc = s->s_mdsc;
3817 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
3819 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
3821 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
3824 static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
3825 struct ceph_msg_header *hdr, int *skip)
3827 struct ceph_msg *msg;
3828 int type = (int) le16_to_cpu(hdr->type);
3829 int front_len = (int) le32_to_cpu(hdr->front_len);
3831 if (con->in_msg)
3832 return con->in_msg;
3834 *skip = 0;
3835 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
3836 if (!msg) {
3837 pr_err("unable to allocate msg type %d len %d\n",
3838 type, front_len);
3839 return NULL;
3842 return msg;
3845 static int sign_message(struct ceph_connection *con, struct ceph_msg *msg)
3847 struct ceph_mds_session *s = con->private;
3848 struct ceph_auth_handshake *auth = &s->s_auth;
3849 return ceph_auth_sign_message(auth, msg);
3852 static int check_message_signature(struct ceph_connection *con, struct ceph_msg *msg)
3854 struct ceph_mds_session *s = con->private;
3855 struct ceph_auth_handshake *auth = &s->s_auth;
3856 return ceph_auth_check_message_signature(auth, msg);
3859 static const struct ceph_connection_operations mds_con_ops = {
3860 .get = con_get,
3861 .put = con_put,
3862 .dispatch = dispatch,
3863 .get_authorizer = get_authorizer,
3864 .verify_authorizer_reply = verify_authorizer_reply,
3865 .invalidate_authorizer = invalidate_authorizer,
3866 .peer_reset = peer_reset,
3867 .alloc_msg = mds_alloc_msg,
3868 .sign_message = sign_message,
3869 .check_message_signature = check_message_signature,
3872 /* eof */