perf bpf: Move perf_event_output() from stdio.h to bpf.h
[linux/fpc-iii.git] / fs / ceph / mds_client.c
blobbd13a3267ae03c401d7b0dd0c1f37626bbc42b0a
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/fs.h>
5 #include <linux/wait.h>
6 #include <linux/slab.h>
7 #include <linux/gfp.h>
8 #include <linux/sched.h>
9 #include <linux/debugfs.h>
10 #include <linux/seq_file.h>
11 #include <linux/ratelimit.h>
13 #include "super.h"
14 #include "mds_client.h"
16 #include <linux/ceph/ceph_features.h>
17 #include <linux/ceph/messenger.h>
18 #include <linux/ceph/decode.h>
19 #include <linux/ceph/pagelist.h>
20 #include <linux/ceph/auth.h>
21 #include <linux/ceph/debugfs.h>
24 * A cluster of MDS (metadata server) daemons is responsible for
25 * managing the file system namespace (the directory hierarchy and
26 * inodes) and for coordinating shared access to storage. Metadata is
27 * partitioning hierarchically across a number of servers, and that
28 * partition varies over time as the cluster adjusts the distribution
29 * in order to balance load.
31 * The MDS client is primarily responsible to managing synchronous
32 * metadata requests for operations like open, unlink, and so forth.
33 * If there is a MDS failure, we find out about it when we (possibly
34 * request and) receive a new MDS map, and can resubmit affected
35 * requests.
37 * For the most part, though, we take advantage of a lossless
38 * communications channel to the MDS, and do not need to worry about
39 * timing out or resubmitting requests.
41 * We maintain a stateful "session" with each MDS we interact with.
42 * Within each session, we sent periodic heartbeat messages to ensure
43 * any capabilities or leases we have been issues remain valid. If
44 * the session times out and goes stale, our leases and capabilities
45 * are no longer valid.
48 struct ceph_reconnect_state {
49 int nr_caps;
50 struct ceph_pagelist *pagelist;
51 unsigned msg_version;
54 static void __wake_requests(struct ceph_mds_client *mdsc,
55 struct list_head *head);
57 static const struct ceph_connection_operations mds_con_ops;
61 * mds reply parsing
65 * parse individual inode info
67 static int parse_reply_info_in(void **p, void *end,
68 struct ceph_mds_reply_info_in *info,
69 u64 features)
71 int err = -EIO;
73 info->in = *p;
74 *p += sizeof(struct ceph_mds_reply_inode) +
75 sizeof(*info->in->fragtree.splits) *
76 le32_to_cpu(info->in->fragtree.nsplits);
78 ceph_decode_32_safe(p, end, info->symlink_len, bad);
79 ceph_decode_need(p, end, info->symlink_len, bad);
80 info->symlink = *p;
81 *p += info->symlink_len;
83 ceph_decode_copy_safe(p, end, &info->dir_layout,
84 sizeof(info->dir_layout), bad);
85 ceph_decode_32_safe(p, end, info->xattr_len, bad);
86 ceph_decode_need(p, end, info->xattr_len, bad);
87 info->xattr_data = *p;
88 *p += info->xattr_len;
90 if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
91 ceph_decode_64_safe(p, end, info->inline_version, bad);
92 ceph_decode_32_safe(p, end, info->inline_len, bad);
93 ceph_decode_need(p, end, info->inline_len, bad);
94 info->inline_data = *p;
95 *p += info->inline_len;
96 } else
97 info->inline_version = CEPH_INLINE_NONE;
99 if (features & CEPH_FEATURE_MDS_QUOTA) {
100 u8 struct_v, struct_compat;
101 u32 struct_len;
104 * both struct_v and struct_compat are expected to be >= 1
106 ceph_decode_8_safe(p, end, struct_v, bad);
107 ceph_decode_8_safe(p, end, struct_compat, bad);
108 if (!struct_v || !struct_compat)
109 goto bad;
110 ceph_decode_32_safe(p, end, struct_len, bad);
111 ceph_decode_need(p, end, struct_len, bad);
112 ceph_decode_64_safe(p, end, info->max_bytes, bad);
113 ceph_decode_64_safe(p, end, info->max_files, bad);
114 } else {
115 info->max_bytes = 0;
116 info->max_files = 0;
119 info->pool_ns_len = 0;
120 info->pool_ns_data = NULL;
121 if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) {
122 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
123 if (info->pool_ns_len > 0) {
124 ceph_decode_need(p, end, info->pool_ns_len, bad);
125 info->pool_ns_data = *p;
126 *p += info->pool_ns_len;
130 return 0;
131 bad:
132 return err;
136 * parse a normal reply, which may contain a (dir+)dentry and/or a
137 * target inode.
139 static int parse_reply_info_trace(void **p, void *end,
140 struct ceph_mds_reply_info_parsed *info,
141 u64 features)
143 int err;
145 if (info->head->is_dentry) {
146 err = parse_reply_info_in(p, end, &info->diri, features);
147 if (err < 0)
148 goto out_bad;
150 if (unlikely(*p + sizeof(*info->dirfrag) > end))
151 goto bad;
152 info->dirfrag = *p;
153 *p += sizeof(*info->dirfrag) +
154 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
155 if (unlikely(*p > end))
156 goto bad;
158 ceph_decode_32_safe(p, end, info->dname_len, bad);
159 ceph_decode_need(p, end, info->dname_len, bad);
160 info->dname = *p;
161 *p += info->dname_len;
162 info->dlease = *p;
163 *p += sizeof(*info->dlease);
166 if (info->head->is_target) {
167 err = parse_reply_info_in(p, end, &info->targeti, features);
168 if (err < 0)
169 goto out_bad;
172 if (unlikely(*p != end))
173 goto bad;
174 return 0;
176 bad:
177 err = -EIO;
178 out_bad:
179 pr_err("problem parsing mds trace %d\n", err);
180 return err;
184 * parse readdir results
186 static int parse_reply_info_dir(void **p, void *end,
187 struct ceph_mds_reply_info_parsed *info,
188 u64 features)
190 u32 num, i = 0;
191 int err;
193 info->dir_dir = *p;
194 if (*p + sizeof(*info->dir_dir) > end)
195 goto bad;
196 *p += sizeof(*info->dir_dir) +
197 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
198 if (*p > end)
199 goto bad;
201 ceph_decode_need(p, end, sizeof(num) + 2, bad);
202 num = ceph_decode_32(p);
204 u16 flags = ceph_decode_16(p);
205 info->dir_end = !!(flags & CEPH_READDIR_FRAG_END);
206 info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE);
207 info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER);
208 info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH);
210 if (num == 0)
211 goto done;
213 BUG_ON(!info->dir_entries);
214 if ((unsigned long)(info->dir_entries + num) >
215 (unsigned long)info->dir_entries + info->dir_buf_size) {
216 pr_err("dir contents are larger than expected\n");
217 WARN_ON(1);
218 goto bad;
221 info->dir_nr = num;
222 while (num) {
223 struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i;
224 /* dentry */
225 ceph_decode_need(p, end, sizeof(u32)*2, bad);
226 rde->name_len = ceph_decode_32(p);
227 ceph_decode_need(p, end, rde->name_len, bad);
228 rde->name = *p;
229 *p += rde->name_len;
230 dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name);
231 rde->lease = *p;
232 *p += sizeof(struct ceph_mds_reply_lease);
234 /* inode */
235 err = parse_reply_info_in(p, end, &rde->inode, features);
236 if (err < 0)
237 goto out_bad;
238 /* ceph_readdir_prepopulate() will update it */
239 rde->offset = 0;
240 i++;
241 num--;
244 done:
245 if (*p != end)
246 goto bad;
247 return 0;
249 bad:
250 err = -EIO;
251 out_bad:
252 pr_err("problem parsing dir contents %d\n", err);
253 return err;
257 * parse fcntl F_GETLK results
259 static int parse_reply_info_filelock(void **p, void *end,
260 struct ceph_mds_reply_info_parsed *info,
261 u64 features)
263 if (*p + sizeof(*info->filelock_reply) > end)
264 goto bad;
266 info->filelock_reply = *p;
267 *p += sizeof(*info->filelock_reply);
269 if (unlikely(*p != end))
270 goto bad;
271 return 0;
273 bad:
274 return -EIO;
278 * parse create results
280 static int parse_reply_info_create(void **p, void *end,
281 struct ceph_mds_reply_info_parsed *info,
282 u64 features)
284 if (features & CEPH_FEATURE_REPLY_CREATE_INODE) {
285 if (*p == end) {
286 info->has_create_ino = false;
287 } else {
288 info->has_create_ino = true;
289 info->ino = ceph_decode_64(p);
293 if (unlikely(*p != end))
294 goto bad;
295 return 0;
297 bad:
298 return -EIO;
302 * parse extra results
304 static int parse_reply_info_extra(void **p, void *end,
305 struct ceph_mds_reply_info_parsed *info,
306 u64 features)
308 u32 op = le32_to_cpu(info->head->op);
310 if (op == CEPH_MDS_OP_GETFILELOCK)
311 return parse_reply_info_filelock(p, end, info, features);
312 else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
313 return parse_reply_info_dir(p, end, info, features);
314 else if (op == CEPH_MDS_OP_CREATE)
315 return parse_reply_info_create(p, end, info, features);
316 else
317 return -EIO;
321 * parse entire mds reply
323 static int parse_reply_info(struct ceph_msg *msg,
324 struct ceph_mds_reply_info_parsed *info,
325 u64 features)
327 void *p, *end;
328 u32 len;
329 int err;
331 info->head = msg->front.iov_base;
332 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
333 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
335 /* trace */
336 ceph_decode_32_safe(&p, end, len, bad);
337 if (len > 0) {
338 ceph_decode_need(&p, end, len, bad);
339 err = parse_reply_info_trace(&p, p+len, info, features);
340 if (err < 0)
341 goto out_bad;
344 /* extra */
345 ceph_decode_32_safe(&p, end, len, bad);
346 if (len > 0) {
347 ceph_decode_need(&p, end, len, bad);
348 err = parse_reply_info_extra(&p, p+len, info, features);
349 if (err < 0)
350 goto out_bad;
353 /* snap blob */
354 ceph_decode_32_safe(&p, end, len, bad);
355 info->snapblob_len = len;
356 info->snapblob = p;
357 p += len;
359 if (p != end)
360 goto bad;
361 return 0;
363 bad:
364 err = -EIO;
365 out_bad:
366 pr_err("mds parse_reply err %d\n", err);
367 return err;
370 static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
372 if (!info->dir_entries)
373 return;
374 free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
379 * sessions
381 const char *ceph_session_state_name(int s)
383 switch (s) {
384 case CEPH_MDS_SESSION_NEW: return "new";
385 case CEPH_MDS_SESSION_OPENING: return "opening";
386 case CEPH_MDS_SESSION_OPEN: return "open";
387 case CEPH_MDS_SESSION_HUNG: return "hung";
388 case CEPH_MDS_SESSION_CLOSING: return "closing";
389 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
390 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
391 case CEPH_MDS_SESSION_REJECTED: return "rejected";
392 default: return "???";
396 static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
398 if (refcount_inc_not_zero(&s->s_ref)) {
399 dout("mdsc get_session %p %d -> %d\n", s,
400 refcount_read(&s->s_ref)-1, refcount_read(&s->s_ref));
401 return s;
402 } else {
403 dout("mdsc get_session %p 0 -- FAIL\n", s);
404 return NULL;
408 void ceph_put_mds_session(struct ceph_mds_session *s)
410 dout("mdsc put_session %p %d -> %d\n", s,
411 refcount_read(&s->s_ref), refcount_read(&s->s_ref)-1);
412 if (refcount_dec_and_test(&s->s_ref)) {
413 if (s->s_auth.authorizer)
414 ceph_auth_destroy_authorizer(s->s_auth.authorizer);
415 kfree(s);
420 * called under mdsc->mutex
422 struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
423 int mds)
425 struct ceph_mds_session *session;
427 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
428 return NULL;
429 session = mdsc->sessions[mds];
430 dout("lookup_mds_session %p %d\n", session,
431 refcount_read(&session->s_ref));
432 get_session(session);
433 return session;
436 static bool __have_session(struct ceph_mds_client *mdsc, int mds)
438 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
439 return false;
440 else
441 return true;
444 static int __verify_registered_session(struct ceph_mds_client *mdsc,
445 struct ceph_mds_session *s)
447 if (s->s_mds >= mdsc->max_sessions ||
448 mdsc->sessions[s->s_mds] != s)
449 return -ENOENT;
450 return 0;
454 * create+register a new session for given mds.
455 * called under mdsc->mutex.
457 static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
458 int mds)
460 struct ceph_mds_session *s;
462 if (mds >= mdsc->mdsmap->m_num_mds)
463 return ERR_PTR(-EINVAL);
465 s = kzalloc(sizeof(*s), GFP_NOFS);
466 if (!s)
467 return ERR_PTR(-ENOMEM);
469 if (mds >= mdsc->max_sessions) {
470 int newmax = 1 << get_count_order(mds + 1);
471 struct ceph_mds_session **sa;
473 dout("%s: realloc to %d\n", __func__, newmax);
474 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
475 if (!sa)
476 goto fail_realloc;
477 if (mdsc->sessions) {
478 memcpy(sa, mdsc->sessions,
479 mdsc->max_sessions * sizeof(void *));
480 kfree(mdsc->sessions);
482 mdsc->sessions = sa;
483 mdsc->max_sessions = newmax;
486 dout("%s: mds%d\n", __func__, mds);
487 s->s_mdsc = mdsc;
488 s->s_mds = mds;
489 s->s_state = CEPH_MDS_SESSION_NEW;
490 s->s_ttl = 0;
491 s->s_seq = 0;
492 mutex_init(&s->s_mutex);
494 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
496 spin_lock_init(&s->s_gen_ttl_lock);
497 s->s_cap_gen = 0;
498 s->s_cap_ttl = jiffies - 1;
500 spin_lock_init(&s->s_cap_lock);
501 s->s_renew_requested = 0;
502 s->s_renew_seq = 0;
503 INIT_LIST_HEAD(&s->s_caps);
504 s->s_nr_caps = 0;
505 s->s_trim_caps = 0;
506 refcount_set(&s->s_ref, 1);
507 INIT_LIST_HEAD(&s->s_waiting);
508 INIT_LIST_HEAD(&s->s_unsafe);
509 s->s_num_cap_releases = 0;
510 s->s_cap_reconnect = 0;
511 s->s_cap_iterator = NULL;
512 INIT_LIST_HEAD(&s->s_cap_releases);
513 INIT_LIST_HEAD(&s->s_cap_flushing);
515 mdsc->sessions[mds] = s;
516 atomic_inc(&mdsc->num_sessions);
517 refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
519 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
520 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
522 return s;
524 fail_realloc:
525 kfree(s);
526 return ERR_PTR(-ENOMEM);
530 * called under mdsc->mutex
532 static void __unregister_session(struct ceph_mds_client *mdsc,
533 struct ceph_mds_session *s)
535 dout("__unregister_session mds%d %p\n", s->s_mds, s);
536 BUG_ON(mdsc->sessions[s->s_mds] != s);
537 mdsc->sessions[s->s_mds] = NULL;
538 ceph_con_close(&s->s_con);
539 ceph_put_mds_session(s);
540 atomic_dec(&mdsc->num_sessions);
544 * drop session refs in request.
546 * should be last request ref, or hold mdsc->mutex
548 static void put_request_session(struct ceph_mds_request *req)
550 if (req->r_session) {
551 ceph_put_mds_session(req->r_session);
552 req->r_session = NULL;
556 void ceph_mdsc_release_request(struct kref *kref)
558 struct ceph_mds_request *req = container_of(kref,
559 struct ceph_mds_request,
560 r_kref);
561 destroy_reply_info(&req->r_reply_info);
562 if (req->r_request)
563 ceph_msg_put(req->r_request);
564 if (req->r_reply)
565 ceph_msg_put(req->r_reply);
566 if (req->r_inode) {
567 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
568 iput(req->r_inode);
570 if (req->r_parent)
571 ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
572 iput(req->r_target_inode);
573 if (req->r_dentry)
574 dput(req->r_dentry);
575 if (req->r_old_dentry)
576 dput(req->r_old_dentry);
577 if (req->r_old_dentry_dir) {
579 * track (and drop pins for) r_old_dentry_dir
580 * separately, since r_old_dentry's d_parent may have
581 * changed between the dir mutex being dropped and
582 * this request being freed.
584 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
585 CEPH_CAP_PIN);
586 iput(req->r_old_dentry_dir);
588 kfree(req->r_path1);
589 kfree(req->r_path2);
590 if (req->r_pagelist)
591 ceph_pagelist_release(req->r_pagelist);
592 put_request_session(req);
593 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
594 kfree(req);
597 DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
600 * lookup session, bump ref if found.
602 * called under mdsc->mutex.
604 static struct ceph_mds_request *
605 lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
607 struct ceph_mds_request *req;
609 req = lookup_request(&mdsc->request_tree, tid);
610 if (req)
611 ceph_mdsc_get_request(req);
613 return req;
617 * Register an in-flight request, and assign a tid. Link to directory
618 * are modifying (if any).
620 * Called under mdsc->mutex.
622 static void __register_request(struct ceph_mds_client *mdsc,
623 struct ceph_mds_request *req,
624 struct inode *dir)
626 int ret = 0;
628 req->r_tid = ++mdsc->last_tid;
629 if (req->r_num_caps) {
630 ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
631 req->r_num_caps);
632 if (ret < 0) {
633 pr_err("__register_request %p "
634 "failed to reserve caps: %d\n", req, ret);
635 /* set req->r_err to fail early from __do_request */
636 req->r_err = ret;
637 return;
640 dout("__register_request %p tid %lld\n", req, req->r_tid);
641 ceph_mdsc_get_request(req);
642 insert_request(&mdsc->request_tree, req);
644 req->r_uid = current_fsuid();
645 req->r_gid = current_fsgid();
647 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
648 mdsc->oldest_tid = req->r_tid;
650 if (dir) {
651 ihold(dir);
652 req->r_unsafe_dir = dir;
656 static void __unregister_request(struct ceph_mds_client *mdsc,
657 struct ceph_mds_request *req)
659 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
661 /* Never leave an unregistered request on an unsafe list! */
662 list_del_init(&req->r_unsafe_item);
664 if (req->r_tid == mdsc->oldest_tid) {
665 struct rb_node *p = rb_next(&req->r_node);
666 mdsc->oldest_tid = 0;
667 while (p) {
668 struct ceph_mds_request *next_req =
669 rb_entry(p, struct ceph_mds_request, r_node);
670 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
671 mdsc->oldest_tid = next_req->r_tid;
672 break;
674 p = rb_next(p);
678 erase_request(&mdsc->request_tree, req);
680 if (req->r_unsafe_dir &&
681 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
682 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
683 spin_lock(&ci->i_unsafe_lock);
684 list_del_init(&req->r_unsafe_dir_item);
685 spin_unlock(&ci->i_unsafe_lock);
687 if (req->r_target_inode &&
688 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
689 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
690 spin_lock(&ci->i_unsafe_lock);
691 list_del_init(&req->r_unsafe_target_item);
692 spin_unlock(&ci->i_unsafe_lock);
695 if (req->r_unsafe_dir) {
696 iput(req->r_unsafe_dir);
697 req->r_unsafe_dir = NULL;
700 complete_all(&req->r_safe_completion);
702 ceph_mdsc_put_request(req);
706 * Walk back up the dentry tree until we hit a dentry representing a
707 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
708 * when calling this) to ensure that the objects won't disappear while we're
709 * working with them. Once we hit a candidate dentry, we attempt to take a
710 * reference to it, and return that as the result.
712 static struct inode *get_nonsnap_parent(struct dentry *dentry)
714 struct inode *inode = NULL;
716 while (dentry && !IS_ROOT(dentry)) {
717 inode = d_inode_rcu(dentry);
718 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
719 break;
720 dentry = dentry->d_parent;
722 if (inode)
723 inode = igrab(inode);
724 return inode;
728 * Choose mds to send request to next. If there is a hint set in the
729 * request (e.g., due to a prior forward hint from the mds), use that.
730 * Otherwise, consult frag tree and/or caps to identify the
731 * appropriate mds. If all else fails, choose randomly.
733 * Called under mdsc->mutex.
735 static int __choose_mds(struct ceph_mds_client *mdsc,
736 struct ceph_mds_request *req)
738 struct inode *inode;
739 struct ceph_inode_info *ci;
740 struct ceph_cap *cap;
741 int mode = req->r_direct_mode;
742 int mds = -1;
743 u32 hash = req->r_direct_hash;
744 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
747 * is there a specific mds we should try? ignore hint if we have
748 * no session and the mds is not up (active or recovering).
750 if (req->r_resend_mds >= 0 &&
751 (__have_session(mdsc, req->r_resend_mds) ||
752 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
753 dout("choose_mds using resend_mds mds%d\n",
754 req->r_resend_mds);
755 return req->r_resend_mds;
758 if (mode == USE_RANDOM_MDS)
759 goto random;
761 inode = NULL;
762 if (req->r_inode) {
763 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
764 inode = req->r_inode;
765 ihold(inode);
766 } else {
767 /* req->r_dentry is non-null for LSSNAP request */
768 rcu_read_lock();
769 inode = get_nonsnap_parent(req->r_dentry);
770 rcu_read_unlock();
771 dout("__choose_mds using snapdir's parent %p\n", inode);
773 } else if (req->r_dentry) {
774 /* ignore race with rename; old or new d_parent is okay */
775 struct dentry *parent;
776 struct inode *dir;
778 rcu_read_lock();
779 parent = req->r_dentry->d_parent;
780 dir = req->r_parent ? : d_inode_rcu(parent);
782 if (!dir || dir->i_sb != mdsc->fsc->sb) {
783 /* not this fs or parent went negative */
784 inode = d_inode(req->r_dentry);
785 if (inode)
786 ihold(inode);
787 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
788 /* direct snapped/virtual snapdir requests
789 * based on parent dir inode */
790 inode = get_nonsnap_parent(parent);
791 dout("__choose_mds using nonsnap parent %p\n", inode);
792 } else {
793 /* dentry target */
794 inode = d_inode(req->r_dentry);
795 if (!inode || mode == USE_AUTH_MDS) {
796 /* dir + name */
797 inode = igrab(dir);
798 hash = ceph_dentry_hash(dir, req->r_dentry);
799 is_hash = true;
800 } else {
801 ihold(inode);
804 rcu_read_unlock();
807 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
808 (int)hash, mode);
809 if (!inode)
810 goto random;
811 ci = ceph_inode(inode);
813 if (is_hash && S_ISDIR(inode->i_mode)) {
814 struct ceph_inode_frag frag;
815 int found;
817 ceph_choose_frag(ci, hash, &frag, &found);
818 if (found) {
819 if (mode == USE_ANY_MDS && frag.ndist > 0) {
820 u8 r;
822 /* choose a random replica */
823 get_random_bytes(&r, 1);
824 r %= frag.ndist;
825 mds = frag.dist[r];
826 dout("choose_mds %p %llx.%llx "
827 "frag %u mds%d (%d/%d)\n",
828 inode, ceph_vinop(inode),
829 frag.frag, mds,
830 (int)r, frag.ndist);
831 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
832 CEPH_MDS_STATE_ACTIVE)
833 goto out;
836 /* since this file/dir wasn't known to be
837 * replicated, then we want to look for the
838 * authoritative mds. */
839 mode = USE_AUTH_MDS;
840 if (frag.mds >= 0) {
841 /* choose auth mds */
842 mds = frag.mds;
843 dout("choose_mds %p %llx.%llx "
844 "frag %u mds%d (auth)\n",
845 inode, ceph_vinop(inode), frag.frag, mds);
846 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
847 CEPH_MDS_STATE_ACTIVE)
848 goto out;
853 spin_lock(&ci->i_ceph_lock);
854 cap = NULL;
855 if (mode == USE_AUTH_MDS)
856 cap = ci->i_auth_cap;
857 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
858 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
859 if (!cap) {
860 spin_unlock(&ci->i_ceph_lock);
861 iput(inode);
862 goto random;
864 mds = cap->session->s_mds;
865 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
866 inode, ceph_vinop(inode), mds,
867 cap == ci->i_auth_cap ? "auth " : "", cap);
868 spin_unlock(&ci->i_ceph_lock);
869 out:
870 iput(inode);
871 return mds;
873 random:
874 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
875 dout("choose_mds chose random mds%d\n", mds);
876 return mds;
881 * session messages
883 static struct ceph_msg *create_session_msg(u32 op, u64 seq)
885 struct ceph_msg *msg;
886 struct ceph_mds_session_head *h;
888 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
889 false);
890 if (!msg) {
891 pr_err("create_session_msg ENOMEM creating msg\n");
892 return NULL;
894 h = msg->front.iov_base;
895 h->op = cpu_to_le32(op);
896 h->seq = cpu_to_le64(seq);
898 return msg;
901 static void encode_supported_features(void **p, void *end)
903 static const unsigned char bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
904 static const size_t count = ARRAY_SIZE(bits);
906 if (count > 0) {
907 size_t i;
908 size_t size = ((size_t)bits[count - 1] + 64) / 64 * 8;
910 BUG_ON(*p + 4 + size > end);
911 ceph_encode_32(p, size);
912 memset(*p, 0, size);
913 for (i = 0; i < count; i++)
914 ((unsigned char*)(*p))[i / 8] |= 1 << (bits[i] % 8);
915 *p += size;
916 } else {
917 BUG_ON(*p + 4 > end);
918 ceph_encode_32(p, 0);
923 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
924 * to include additional client metadata fields.
926 static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
928 struct ceph_msg *msg;
929 struct ceph_mds_session_head *h;
930 int i = -1;
931 int extra_bytes = 0;
932 int metadata_key_count = 0;
933 struct ceph_options *opt = mdsc->fsc->client->options;
934 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
935 void *p, *end;
937 const char* metadata[][2] = {
938 {"hostname", mdsc->nodename},
939 {"kernel_version", init_utsname()->release},
940 {"entity_id", opt->name ? : ""},
941 {"root", fsopt->server_path ? : "/"},
942 {NULL, NULL}
945 /* Calculate serialized length of metadata */
946 extra_bytes = 4; /* map length */
947 for (i = 0; metadata[i][0]; ++i) {
948 extra_bytes += 8 + strlen(metadata[i][0]) +
949 strlen(metadata[i][1]);
950 metadata_key_count++;
952 /* supported feature */
953 extra_bytes += 4 + 8;
955 /* Allocate the message */
956 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
957 GFP_NOFS, false);
958 if (!msg) {
959 pr_err("create_session_msg ENOMEM creating msg\n");
960 return NULL;
962 p = msg->front.iov_base;
963 end = p + msg->front.iov_len;
965 h = p;
966 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
967 h->seq = cpu_to_le64(seq);
970 * Serialize client metadata into waiting buffer space, using
971 * the format that userspace expects for map<string, string>
973 * ClientSession messages with metadata are v2
975 msg->hdr.version = cpu_to_le16(3);
976 msg->hdr.compat_version = cpu_to_le16(1);
978 /* The write pointer, following the session_head structure */
979 p += sizeof(*h);
981 /* Number of entries in the map */
982 ceph_encode_32(&p, metadata_key_count);
984 /* Two length-prefixed strings for each entry in the map */
985 for (i = 0; metadata[i][0]; ++i) {
986 size_t const key_len = strlen(metadata[i][0]);
987 size_t const val_len = strlen(metadata[i][1]);
989 ceph_encode_32(&p, key_len);
990 memcpy(p, metadata[i][0], key_len);
991 p += key_len;
992 ceph_encode_32(&p, val_len);
993 memcpy(p, metadata[i][1], val_len);
994 p += val_len;
997 encode_supported_features(&p, end);
998 msg->front.iov_len = p - msg->front.iov_base;
999 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1001 return msg;
1005 * send session open request.
1007 * called under mdsc->mutex
1009 static int __open_session(struct ceph_mds_client *mdsc,
1010 struct ceph_mds_session *session)
1012 struct ceph_msg *msg;
1013 int mstate;
1014 int mds = session->s_mds;
1016 /* wait for mds to go active? */
1017 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
1018 dout("open_session to mds%d (%s)\n", mds,
1019 ceph_mds_state_name(mstate));
1020 session->s_state = CEPH_MDS_SESSION_OPENING;
1021 session->s_renew_requested = jiffies;
1023 /* send connect message */
1024 msg = create_session_open_msg(mdsc, session->s_seq);
1025 if (!msg)
1026 return -ENOMEM;
1027 ceph_con_send(&session->s_con, msg);
1028 return 0;
1032 * open sessions for any export targets for the given mds
1034 * called under mdsc->mutex
1036 static struct ceph_mds_session *
1037 __open_export_target_session(struct ceph_mds_client *mdsc, int target)
1039 struct ceph_mds_session *session;
1041 session = __ceph_lookup_mds_session(mdsc, target);
1042 if (!session) {
1043 session = register_session(mdsc, target);
1044 if (IS_ERR(session))
1045 return session;
1047 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1048 session->s_state == CEPH_MDS_SESSION_CLOSING)
1049 __open_session(mdsc, session);
1051 return session;
1054 struct ceph_mds_session *
1055 ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1057 struct ceph_mds_session *session;
1059 dout("open_export_target_session to mds%d\n", target);
1061 mutex_lock(&mdsc->mutex);
1062 session = __open_export_target_session(mdsc, target);
1063 mutex_unlock(&mdsc->mutex);
1065 return session;
1068 static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1069 struct ceph_mds_session *session)
1071 struct ceph_mds_info *mi;
1072 struct ceph_mds_session *ts;
1073 int i, mds = session->s_mds;
1075 if (mds >= mdsc->mdsmap->m_num_mds)
1076 return;
1078 mi = &mdsc->mdsmap->m_info[mds];
1079 dout("open_export_target_sessions for mds%d (%d targets)\n",
1080 session->s_mds, mi->num_export_targets);
1082 for (i = 0; i < mi->num_export_targets; i++) {
1083 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1084 if (!IS_ERR(ts))
1085 ceph_put_mds_session(ts);
1089 void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1090 struct ceph_mds_session *session)
1092 mutex_lock(&mdsc->mutex);
1093 __open_export_target_sessions(mdsc, session);
1094 mutex_unlock(&mdsc->mutex);
1098 * session caps
1101 static void detach_cap_releases(struct ceph_mds_session *session,
1102 struct list_head *target)
1104 lockdep_assert_held(&session->s_cap_lock);
1106 list_splice_init(&session->s_cap_releases, target);
1107 session->s_num_cap_releases = 0;
1108 dout("dispose_cap_releases mds%d\n", session->s_mds);
1111 static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1112 struct list_head *dispose)
1114 while (!list_empty(dispose)) {
1115 struct ceph_cap *cap;
1116 /* zero out the in-progress message */
1117 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
1118 list_del(&cap->session_caps);
1119 ceph_put_cap(mdsc, cap);
1123 static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1124 struct ceph_mds_session *session)
1126 struct ceph_mds_request *req;
1127 struct rb_node *p;
1129 dout("cleanup_session_requests mds%d\n", session->s_mds);
1130 mutex_lock(&mdsc->mutex);
1131 while (!list_empty(&session->s_unsafe)) {
1132 req = list_first_entry(&session->s_unsafe,
1133 struct ceph_mds_request, r_unsafe_item);
1134 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1135 req->r_tid);
1136 __unregister_request(mdsc, req);
1138 /* zero r_attempts, so kick_requests() will re-send requests */
1139 p = rb_first(&mdsc->request_tree);
1140 while (p) {
1141 req = rb_entry(p, struct ceph_mds_request, r_node);
1142 p = rb_next(p);
1143 if (req->r_session &&
1144 req->r_session->s_mds == session->s_mds)
1145 req->r_attempts = 0;
1147 mutex_unlock(&mdsc->mutex);
1151 * Helper to safely iterate over all caps associated with a session, with
1152 * special care taken to handle a racing __ceph_remove_cap().
1154 * Caller must hold session s_mutex.
1156 static int iterate_session_caps(struct ceph_mds_session *session,
1157 int (*cb)(struct inode *, struct ceph_cap *,
1158 void *), void *arg)
1160 struct list_head *p;
1161 struct ceph_cap *cap;
1162 struct inode *inode, *last_inode = NULL;
1163 struct ceph_cap *old_cap = NULL;
1164 int ret;
1166 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1167 spin_lock(&session->s_cap_lock);
1168 p = session->s_caps.next;
1169 while (p != &session->s_caps) {
1170 cap = list_entry(p, struct ceph_cap, session_caps);
1171 inode = igrab(&cap->ci->vfs_inode);
1172 if (!inode) {
1173 p = p->next;
1174 continue;
1176 session->s_cap_iterator = cap;
1177 spin_unlock(&session->s_cap_lock);
1179 if (last_inode) {
1180 iput(last_inode);
1181 last_inode = NULL;
1183 if (old_cap) {
1184 ceph_put_cap(session->s_mdsc, old_cap);
1185 old_cap = NULL;
1188 ret = cb(inode, cap, arg);
1189 last_inode = inode;
1191 spin_lock(&session->s_cap_lock);
1192 p = p->next;
1193 if (!cap->ci) {
1194 dout("iterate_session_caps finishing cap %p removal\n",
1195 cap);
1196 BUG_ON(cap->session != session);
1197 cap->session = NULL;
1198 list_del_init(&cap->session_caps);
1199 session->s_nr_caps--;
1200 if (cap->queue_release) {
1201 list_add_tail(&cap->session_caps,
1202 &session->s_cap_releases);
1203 session->s_num_cap_releases++;
1204 } else {
1205 old_cap = cap; /* put_cap it w/o locks held */
1208 if (ret < 0)
1209 goto out;
1211 ret = 0;
1212 out:
1213 session->s_cap_iterator = NULL;
1214 spin_unlock(&session->s_cap_lock);
1216 iput(last_inode);
1217 if (old_cap)
1218 ceph_put_cap(session->s_mdsc, old_cap);
1220 return ret;
1223 static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
1224 void *arg)
1226 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
1227 struct ceph_inode_info *ci = ceph_inode(inode);
1228 LIST_HEAD(to_remove);
1229 bool drop = false;
1230 bool invalidate = false;
1232 dout("removing cap %p, ci is %p, inode is %p\n",
1233 cap, ci, &ci->vfs_inode);
1234 spin_lock(&ci->i_ceph_lock);
1235 __ceph_remove_cap(cap, false);
1236 if (!ci->i_auth_cap) {
1237 struct ceph_cap_flush *cf;
1238 struct ceph_mds_client *mdsc = fsc->mdsc;
1240 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1242 if (ci->i_wrbuffer_ref > 0 &&
1243 READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
1244 invalidate = true;
1246 while (!list_empty(&ci->i_cap_flush_list)) {
1247 cf = list_first_entry(&ci->i_cap_flush_list,
1248 struct ceph_cap_flush, i_list);
1249 list_move(&cf->i_list, &to_remove);
1252 spin_lock(&mdsc->cap_dirty_lock);
1254 list_for_each_entry(cf, &to_remove, i_list)
1255 list_del(&cf->g_list);
1257 if (!list_empty(&ci->i_dirty_item)) {
1258 pr_warn_ratelimited(
1259 " dropping dirty %s state for %p %lld\n",
1260 ceph_cap_string(ci->i_dirty_caps),
1261 inode, ceph_ino(inode));
1262 ci->i_dirty_caps = 0;
1263 list_del_init(&ci->i_dirty_item);
1264 drop = true;
1266 if (!list_empty(&ci->i_flushing_item)) {
1267 pr_warn_ratelimited(
1268 " dropping dirty+flushing %s state for %p %lld\n",
1269 ceph_cap_string(ci->i_flushing_caps),
1270 inode, ceph_ino(inode));
1271 ci->i_flushing_caps = 0;
1272 list_del_init(&ci->i_flushing_item);
1273 mdsc->num_cap_flushing--;
1274 drop = true;
1276 spin_unlock(&mdsc->cap_dirty_lock);
1278 if (atomic_read(&ci->i_filelock_ref) > 0) {
1279 /* make further file lock syscall return -EIO */
1280 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1281 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1282 inode, ceph_ino(inode));
1285 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
1286 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
1287 ci->i_prealloc_cap_flush = NULL;
1290 spin_unlock(&ci->i_ceph_lock);
1291 while (!list_empty(&to_remove)) {
1292 struct ceph_cap_flush *cf;
1293 cf = list_first_entry(&to_remove,
1294 struct ceph_cap_flush, i_list);
1295 list_del(&cf->i_list);
1296 ceph_free_cap_flush(cf);
1299 wake_up_all(&ci->i_cap_wq);
1300 if (invalidate)
1301 ceph_queue_invalidate(inode);
1302 if (drop)
1303 iput(inode);
1304 return 0;
1308 * caller must hold session s_mutex
1310 static void remove_session_caps(struct ceph_mds_session *session)
1312 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1313 struct super_block *sb = fsc->sb;
1314 LIST_HEAD(dispose);
1316 dout("remove_session_caps on %p\n", session);
1317 iterate_session_caps(session, remove_session_caps_cb, fsc);
1319 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1321 spin_lock(&session->s_cap_lock);
1322 if (session->s_nr_caps > 0) {
1323 struct inode *inode;
1324 struct ceph_cap *cap, *prev = NULL;
1325 struct ceph_vino vino;
1327 * iterate_session_caps() skips inodes that are being
1328 * deleted, we need to wait until deletions are complete.
1329 * __wait_on_freeing_inode() is designed for the job,
1330 * but it is not exported, so use lookup inode function
1331 * to access it.
1333 while (!list_empty(&session->s_caps)) {
1334 cap = list_entry(session->s_caps.next,
1335 struct ceph_cap, session_caps);
1336 if (cap == prev)
1337 break;
1338 prev = cap;
1339 vino = cap->ci->i_vino;
1340 spin_unlock(&session->s_cap_lock);
1342 inode = ceph_find_inode(sb, vino);
1343 iput(inode);
1345 spin_lock(&session->s_cap_lock);
1349 // drop cap expires and unlock s_cap_lock
1350 detach_cap_releases(session, &dispose);
1352 BUG_ON(session->s_nr_caps > 0);
1353 BUG_ON(!list_empty(&session->s_cap_flushing));
1354 spin_unlock(&session->s_cap_lock);
1355 dispose_cap_releases(session->s_mdsc, &dispose);
1359 * wake up any threads waiting on this session's caps. if the cap is
1360 * old (didn't get renewed on the client reconnect), remove it now.
1362 * caller must hold s_mutex.
1364 static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1365 void *arg)
1367 struct ceph_inode_info *ci = ceph_inode(inode);
1369 if (arg) {
1370 spin_lock(&ci->i_ceph_lock);
1371 ci->i_wanted_max_size = 0;
1372 ci->i_requested_max_size = 0;
1373 spin_unlock(&ci->i_ceph_lock);
1375 wake_up_all(&ci->i_cap_wq);
1376 return 0;
1379 static void wake_up_session_caps(struct ceph_mds_session *session,
1380 int reconnect)
1382 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
1383 iterate_session_caps(session, wake_up_session_cb,
1384 (void *)(unsigned long)reconnect);
1388 * Send periodic message to MDS renewing all currently held caps. The
1389 * ack will reset the expiration for all caps from this session.
1391 * caller holds s_mutex
1393 static int send_renew_caps(struct ceph_mds_client *mdsc,
1394 struct ceph_mds_session *session)
1396 struct ceph_msg *msg;
1397 int state;
1399 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1400 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1401 pr_info("mds%d caps stale\n", session->s_mds);
1402 session->s_renew_requested = jiffies;
1404 /* do not try to renew caps until a recovering mds has reconnected
1405 * with its clients. */
1406 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1407 if (state < CEPH_MDS_STATE_RECONNECT) {
1408 dout("send_renew_caps ignoring mds%d (%s)\n",
1409 session->s_mds, ceph_mds_state_name(state));
1410 return 0;
1413 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1414 ceph_mds_state_name(state));
1415 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1416 ++session->s_renew_seq);
1417 if (!msg)
1418 return -ENOMEM;
1419 ceph_con_send(&session->s_con, msg);
1420 return 0;
1423 static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1424 struct ceph_mds_session *session, u64 seq)
1426 struct ceph_msg *msg;
1428 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
1429 session->s_mds, ceph_session_state_name(session->s_state), seq);
1430 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1431 if (!msg)
1432 return -ENOMEM;
1433 ceph_con_send(&session->s_con, msg);
1434 return 0;
1439 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
1441 * Called under session->s_mutex
1443 static void renewed_caps(struct ceph_mds_client *mdsc,
1444 struct ceph_mds_session *session, int is_renew)
1446 int was_stale;
1447 int wake = 0;
1449 spin_lock(&session->s_cap_lock);
1450 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
1452 session->s_cap_ttl = session->s_renew_requested +
1453 mdsc->mdsmap->m_session_timeout*HZ;
1455 if (was_stale) {
1456 if (time_before(jiffies, session->s_cap_ttl)) {
1457 pr_info("mds%d caps renewed\n", session->s_mds);
1458 wake = 1;
1459 } else {
1460 pr_info("mds%d caps still stale\n", session->s_mds);
1463 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1464 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1465 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1466 spin_unlock(&session->s_cap_lock);
1468 if (wake)
1469 wake_up_session_caps(session, 0);
1473 * send a session close request
1475 static int request_close_session(struct ceph_mds_client *mdsc,
1476 struct ceph_mds_session *session)
1478 struct ceph_msg *msg;
1480 dout("request_close_session mds%d state %s seq %lld\n",
1481 session->s_mds, ceph_session_state_name(session->s_state),
1482 session->s_seq);
1483 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
1484 if (!msg)
1485 return -ENOMEM;
1486 ceph_con_send(&session->s_con, msg);
1487 return 1;
1491 * Called with s_mutex held.
1493 static int __close_session(struct ceph_mds_client *mdsc,
1494 struct ceph_mds_session *session)
1496 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1497 return 0;
1498 session->s_state = CEPH_MDS_SESSION_CLOSING;
1499 return request_close_session(mdsc, session);
1502 static bool drop_negative_children(struct dentry *dentry)
1504 struct dentry *child;
1505 bool all_negative = true;
1507 if (!d_is_dir(dentry))
1508 goto out;
1510 spin_lock(&dentry->d_lock);
1511 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1512 if (d_really_is_positive(child)) {
1513 all_negative = false;
1514 break;
1517 spin_unlock(&dentry->d_lock);
1519 if (all_negative)
1520 shrink_dcache_parent(dentry);
1521 out:
1522 return all_negative;
1526 * Trim old(er) caps.
1528 * Because we can't cache an inode without one or more caps, we do
1529 * this indirectly: if a cap is unused, we prune its aliases, at which
1530 * point the inode will hopefully get dropped to.
1532 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1533 * memory pressure from the MDS, though, so it needn't be perfect.
1535 static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1537 struct ceph_mds_session *session = arg;
1538 struct ceph_inode_info *ci = ceph_inode(inode);
1539 int used, wanted, oissued, mine;
1541 if (session->s_trim_caps <= 0)
1542 return -1;
1544 spin_lock(&ci->i_ceph_lock);
1545 mine = cap->issued | cap->implemented;
1546 used = __ceph_caps_used(ci);
1547 wanted = __ceph_caps_file_wanted(ci);
1548 oissued = __ceph_caps_issued_other(ci, cap);
1550 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
1551 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
1552 ceph_cap_string(used), ceph_cap_string(wanted));
1553 if (cap == ci->i_auth_cap) {
1554 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1555 !list_empty(&ci->i_cap_snaps))
1556 goto out;
1557 if ((used | wanted) & CEPH_CAP_ANY_WR)
1558 goto out;
1559 /* Note: it's possible that i_filelock_ref becomes non-zero
1560 * after dropping auth caps. It doesn't hurt because reply
1561 * of lock mds request will re-add auth caps. */
1562 if (atomic_read(&ci->i_filelock_ref) > 0)
1563 goto out;
1565 /* The inode has cached pages, but it's no longer used.
1566 * we can safely drop it */
1567 if (wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
1568 !(oissued & CEPH_CAP_FILE_CACHE)) {
1569 used = 0;
1570 oissued = 0;
1572 if ((used | wanted) & ~oissued & mine)
1573 goto out; /* we need these caps */
1575 if (oissued) {
1576 /* we aren't the only cap.. just remove us */
1577 __ceph_remove_cap(cap, true);
1578 session->s_trim_caps--;
1579 } else {
1580 struct dentry *dentry;
1581 /* try dropping referring dentries */
1582 spin_unlock(&ci->i_ceph_lock);
1583 dentry = d_find_any_alias(inode);
1584 if (dentry && drop_negative_children(dentry)) {
1585 int count;
1586 dput(dentry);
1587 d_prune_aliases(inode);
1588 count = atomic_read(&inode->i_count);
1589 if (count == 1)
1590 session->s_trim_caps--;
1591 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1592 inode, cap, count);
1593 } else {
1594 dput(dentry);
1596 return 0;
1599 out:
1600 spin_unlock(&ci->i_ceph_lock);
1601 return 0;
1605 * Trim session cap count down to some max number.
1607 int ceph_trim_caps(struct ceph_mds_client *mdsc,
1608 struct ceph_mds_session *session,
1609 int max_caps)
1611 int trim_caps = session->s_nr_caps - max_caps;
1613 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1614 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1615 if (trim_caps > 0) {
1616 session->s_trim_caps = trim_caps;
1617 iterate_session_caps(session, trim_caps_cb, session);
1618 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1619 session->s_mds, session->s_nr_caps, max_caps,
1620 trim_caps - session->s_trim_caps);
1621 session->s_trim_caps = 0;
1624 ceph_send_cap_releases(mdsc, session);
1625 return 0;
1628 static int check_caps_flush(struct ceph_mds_client *mdsc,
1629 u64 want_flush_tid)
1631 int ret = 1;
1633 spin_lock(&mdsc->cap_dirty_lock);
1634 if (!list_empty(&mdsc->cap_flush_list)) {
1635 struct ceph_cap_flush *cf =
1636 list_first_entry(&mdsc->cap_flush_list,
1637 struct ceph_cap_flush, g_list);
1638 if (cf->tid <= want_flush_tid) {
1639 dout("check_caps_flush still flushing tid "
1640 "%llu <= %llu\n", cf->tid, want_flush_tid);
1641 ret = 0;
1644 spin_unlock(&mdsc->cap_dirty_lock);
1645 return ret;
1649 * flush all dirty inode data to disk.
1651 * returns true if we've flushed through want_flush_tid
1653 static void wait_caps_flush(struct ceph_mds_client *mdsc,
1654 u64 want_flush_tid)
1656 dout("check_caps_flush want %llu\n", want_flush_tid);
1658 wait_event(mdsc->cap_flushing_wq,
1659 check_caps_flush(mdsc, want_flush_tid));
1661 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
1665 * called under s_mutex
1667 void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1668 struct ceph_mds_session *session)
1670 struct ceph_msg *msg = NULL;
1671 struct ceph_mds_cap_release *head;
1672 struct ceph_mds_cap_item *item;
1673 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
1674 struct ceph_cap *cap;
1675 LIST_HEAD(tmp_list);
1676 int num_cap_releases;
1677 __le32 barrier, *cap_barrier;
1679 down_read(&osdc->lock);
1680 barrier = cpu_to_le32(osdc->epoch_barrier);
1681 up_read(&osdc->lock);
1683 spin_lock(&session->s_cap_lock);
1684 again:
1685 list_splice_init(&session->s_cap_releases, &tmp_list);
1686 num_cap_releases = session->s_num_cap_releases;
1687 session->s_num_cap_releases = 0;
1688 spin_unlock(&session->s_cap_lock);
1690 while (!list_empty(&tmp_list)) {
1691 if (!msg) {
1692 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
1693 PAGE_SIZE, GFP_NOFS, false);
1694 if (!msg)
1695 goto out_err;
1696 head = msg->front.iov_base;
1697 head->num = cpu_to_le32(0);
1698 msg->front.iov_len = sizeof(*head);
1700 msg->hdr.version = cpu_to_le16(2);
1701 msg->hdr.compat_version = cpu_to_le16(1);
1704 cap = list_first_entry(&tmp_list, struct ceph_cap,
1705 session_caps);
1706 list_del(&cap->session_caps);
1707 num_cap_releases--;
1709 head = msg->front.iov_base;
1710 le32_add_cpu(&head->num, 1);
1711 item = msg->front.iov_base + msg->front.iov_len;
1712 item->ino = cpu_to_le64(cap->cap_ino);
1713 item->cap_id = cpu_to_le64(cap->cap_id);
1714 item->migrate_seq = cpu_to_le32(cap->mseq);
1715 item->seq = cpu_to_le32(cap->issue_seq);
1716 msg->front.iov_len += sizeof(*item);
1718 ceph_put_cap(mdsc, cap);
1720 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
1721 // Append cap_barrier field
1722 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1723 *cap_barrier = barrier;
1724 msg->front.iov_len += sizeof(*cap_barrier);
1726 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1727 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1728 ceph_con_send(&session->s_con, msg);
1729 msg = NULL;
1733 BUG_ON(num_cap_releases != 0);
1735 spin_lock(&session->s_cap_lock);
1736 if (!list_empty(&session->s_cap_releases))
1737 goto again;
1738 spin_unlock(&session->s_cap_lock);
1740 if (msg) {
1741 // Append cap_barrier field
1742 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1743 *cap_barrier = barrier;
1744 msg->front.iov_len += sizeof(*cap_barrier);
1746 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1747 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1748 ceph_con_send(&session->s_con, msg);
1750 return;
1751 out_err:
1752 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1753 session->s_mds);
1754 spin_lock(&session->s_cap_lock);
1755 list_splice(&tmp_list, &session->s_cap_releases);
1756 session->s_num_cap_releases += num_cap_releases;
1757 spin_unlock(&session->s_cap_lock);
1761 * requests
1764 int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1765 struct inode *dir)
1767 struct ceph_inode_info *ci = ceph_inode(dir);
1768 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1769 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
1770 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
1771 int order, num_entries;
1773 spin_lock(&ci->i_ceph_lock);
1774 num_entries = ci->i_files + ci->i_subdirs;
1775 spin_unlock(&ci->i_ceph_lock);
1776 num_entries = max(num_entries, 1);
1777 num_entries = min(num_entries, opt->max_readdir);
1779 order = get_order(size * num_entries);
1780 while (order >= 0) {
1781 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
1782 __GFP_NOWARN,
1783 order);
1784 if (rinfo->dir_entries)
1785 break;
1786 order--;
1788 if (!rinfo->dir_entries)
1789 return -ENOMEM;
1791 num_entries = (PAGE_SIZE << order) / size;
1792 num_entries = min(num_entries, opt->max_readdir);
1794 rinfo->dir_buf_size = PAGE_SIZE << order;
1795 req->r_num_caps = num_entries + 1;
1796 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1797 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1798 return 0;
1802 * Create an mds request.
1804 struct ceph_mds_request *
1805 ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1807 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1808 struct timespec64 ts;
1810 if (!req)
1811 return ERR_PTR(-ENOMEM);
1813 mutex_init(&req->r_fill_mutex);
1814 req->r_mdsc = mdsc;
1815 req->r_started = jiffies;
1816 req->r_resend_mds = -1;
1817 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
1818 INIT_LIST_HEAD(&req->r_unsafe_target_item);
1819 req->r_fmode = -1;
1820 kref_init(&req->r_kref);
1821 RB_CLEAR_NODE(&req->r_node);
1822 INIT_LIST_HEAD(&req->r_wait);
1823 init_completion(&req->r_completion);
1824 init_completion(&req->r_safe_completion);
1825 INIT_LIST_HEAD(&req->r_unsafe_item);
1827 ktime_get_coarse_real_ts64(&ts);
1828 req->r_stamp = timespec64_trunc(ts, mdsc->fsc->sb->s_time_gran);
1830 req->r_op = op;
1831 req->r_direct_mode = mode;
1832 return req;
1836 * return oldest (lowest) request, tid in request tree, 0 if none.
1838 * called under mdsc->mutex.
1840 static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1842 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1843 return NULL;
1844 return rb_entry(rb_first(&mdsc->request_tree),
1845 struct ceph_mds_request, r_node);
1848 static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
1850 return mdsc->oldest_tid;
1854 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1855 * on build_path_from_dentry in fs/cifs/dir.c.
1857 * If @stop_on_nosnap, generate path relative to the first non-snapped
1858 * inode.
1860 * Encode hidden .snap dirs as a double /, i.e.
1861 * foo/.snap/bar -> foo//bar
1863 char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1864 int stop_on_nosnap)
1866 struct dentry *temp;
1867 char *path;
1868 int len, pos;
1869 unsigned seq;
1871 if (!dentry)
1872 return ERR_PTR(-EINVAL);
1874 retry:
1875 len = 0;
1876 seq = read_seqbegin(&rename_lock);
1877 rcu_read_lock();
1878 for (temp = dentry; !IS_ROOT(temp);) {
1879 struct inode *inode = d_inode(temp);
1880 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1881 len++; /* slash only */
1882 else if (stop_on_nosnap && inode &&
1883 ceph_snap(inode) == CEPH_NOSNAP)
1884 break;
1885 else
1886 len += 1 + temp->d_name.len;
1887 temp = temp->d_parent;
1889 rcu_read_unlock();
1890 if (len)
1891 len--; /* no leading '/' */
1893 path = kmalloc(len+1, GFP_NOFS);
1894 if (!path)
1895 return ERR_PTR(-ENOMEM);
1896 pos = len;
1897 path[pos] = 0; /* trailing null */
1898 rcu_read_lock();
1899 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1900 struct inode *inode;
1902 spin_lock(&temp->d_lock);
1903 inode = d_inode(temp);
1904 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1905 dout("build_path path+%d: %p SNAPDIR\n",
1906 pos, temp);
1907 } else if (stop_on_nosnap && inode &&
1908 ceph_snap(inode) == CEPH_NOSNAP) {
1909 spin_unlock(&temp->d_lock);
1910 break;
1911 } else {
1912 pos -= temp->d_name.len;
1913 if (pos < 0) {
1914 spin_unlock(&temp->d_lock);
1915 break;
1917 strncpy(path + pos, temp->d_name.name,
1918 temp->d_name.len);
1920 spin_unlock(&temp->d_lock);
1921 if (pos)
1922 path[--pos] = '/';
1923 temp = temp->d_parent;
1925 rcu_read_unlock();
1926 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
1927 pr_err("build_path did not end path lookup where "
1928 "expected, namelen is %d, pos is %d\n", len, pos);
1929 /* presumably this is only possible if racing with a
1930 rename of one of the parent directories (we can not
1931 lock the dentries above us to prevent this, but
1932 retrying should be harmless) */
1933 kfree(path);
1934 goto retry;
1937 *base = ceph_ino(d_inode(temp));
1938 *plen = len;
1939 dout("build_path on %p %d built %llx '%.*s'\n",
1940 dentry, d_count(dentry), *base, len, path);
1941 return path;
1944 static int build_dentry_path(struct dentry *dentry, struct inode *dir,
1945 const char **ppath, int *ppathlen, u64 *pino,
1946 int *pfreepath)
1948 char *path;
1950 rcu_read_lock();
1951 if (!dir)
1952 dir = d_inode_rcu(dentry->d_parent);
1953 if (dir && ceph_snap(dir) == CEPH_NOSNAP) {
1954 *pino = ceph_ino(dir);
1955 rcu_read_unlock();
1956 *ppath = dentry->d_name.name;
1957 *ppathlen = dentry->d_name.len;
1958 return 0;
1960 rcu_read_unlock();
1961 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1962 if (IS_ERR(path))
1963 return PTR_ERR(path);
1964 *ppath = path;
1965 *pfreepath = 1;
1966 return 0;
1969 static int build_inode_path(struct inode *inode,
1970 const char **ppath, int *ppathlen, u64 *pino,
1971 int *pfreepath)
1973 struct dentry *dentry;
1974 char *path;
1976 if (ceph_snap(inode) == CEPH_NOSNAP) {
1977 *pino = ceph_ino(inode);
1978 *ppathlen = 0;
1979 return 0;
1981 dentry = d_find_alias(inode);
1982 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1983 dput(dentry);
1984 if (IS_ERR(path))
1985 return PTR_ERR(path);
1986 *ppath = path;
1987 *pfreepath = 1;
1988 return 0;
1992 * request arguments may be specified via an inode *, a dentry *, or
1993 * an explicit ino+path.
1995 static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
1996 struct inode *rdiri, const char *rpath,
1997 u64 rino, const char **ppath, int *pathlen,
1998 u64 *ino, int *freepath)
2000 int r = 0;
2002 if (rinode) {
2003 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
2004 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
2005 ceph_snap(rinode));
2006 } else if (rdentry) {
2007 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
2008 freepath);
2009 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
2010 *ppath);
2011 } else if (rpath || rino) {
2012 *ino = rino;
2013 *ppath = rpath;
2014 *pathlen = rpath ? strlen(rpath) : 0;
2015 dout(" path %.*s\n", *pathlen, rpath);
2018 return r;
2022 * called under mdsc->mutex
2024 static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
2025 struct ceph_mds_request *req,
2026 int mds, bool drop_cap_releases)
2028 struct ceph_msg *msg;
2029 struct ceph_mds_request_head *head;
2030 const char *path1 = NULL;
2031 const char *path2 = NULL;
2032 u64 ino1 = 0, ino2 = 0;
2033 int pathlen1 = 0, pathlen2 = 0;
2034 int freepath1 = 0, freepath2 = 0;
2035 int len;
2036 u16 releases;
2037 void *p, *end;
2038 int ret;
2040 ret = set_request_path_attr(req->r_inode, req->r_dentry,
2041 req->r_parent, req->r_path1, req->r_ino1.ino,
2042 &path1, &pathlen1, &ino1, &freepath1);
2043 if (ret < 0) {
2044 msg = ERR_PTR(ret);
2045 goto out;
2048 ret = set_request_path_attr(NULL, req->r_old_dentry,
2049 req->r_old_dentry_dir,
2050 req->r_path2, req->r_ino2.ino,
2051 &path2, &pathlen2, &ino2, &freepath2);
2052 if (ret < 0) {
2053 msg = ERR_PTR(ret);
2054 goto out_free1;
2057 len = sizeof(*head) +
2058 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
2059 sizeof(struct ceph_timespec);
2061 /* calculate (max) length for cap releases */
2062 len += sizeof(struct ceph_mds_request_release) *
2063 (!!req->r_inode_drop + !!req->r_dentry_drop +
2064 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2065 if (req->r_dentry_drop)
2066 len += req->r_dentry->d_name.len;
2067 if (req->r_old_dentry_drop)
2068 len += req->r_old_dentry->d_name.len;
2070 msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
2071 if (!msg) {
2072 msg = ERR_PTR(-ENOMEM);
2073 goto out_free2;
2076 msg->hdr.version = cpu_to_le16(2);
2077 msg->hdr.tid = cpu_to_le64(req->r_tid);
2079 head = msg->front.iov_base;
2080 p = msg->front.iov_base + sizeof(*head);
2081 end = msg->front.iov_base + msg->front.iov_len;
2083 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2084 head->op = cpu_to_le32(req->r_op);
2085 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2086 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
2087 head->args = req->r_args;
2089 ceph_encode_filepath(&p, end, ino1, path1);
2090 ceph_encode_filepath(&p, end, ino2, path2);
2092 /* make note of release offset, in case we need to replay */
2093 req->r_request_release_offset = p - msg->front.iov_base;
2095 /* cap releases */
2096 releases = 0;
2097 if (req->r_inode_drop)
2098 releases += ceph_encode_inode_release(&p,
2099 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
2100 mds, req->r_inode_drop, req->r_inode_unless, 0);
2101 if (req->r_dentry_drop)
2102 releases += ceph_encode_dentry_release(&p, req->r_dentry,
2103 req->r_parent, mds, req->r_dentry_drop,
2104 req->r_dentry_unless);
2105 if (req->r_old_dentry_drop)
2106 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
2107 req->r_old_dentry_dir, mds,
2108 req->r_old_dentry_drop,
2109 req->r_old_dentry_unless);
2110 if (req->r_old_inode_drop)
2111 releases += ceph_encode_inode_release(&p,
2112 d_inode(req->r_old_dentry),
2113 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
2115 if (drop_cap_releases) {
2116 releases = 0;
2117 p = msg->front.iov_base + req->r_request_release_offset;
2120 head->num_releases = cpu_to_le16(releases);
2122 /* time stamp */
2124 struct ceph_timespec ts;
2125 ceph_encode_timespec64(&ts, &req->r_stamp);
2126 ceph_encode_copy(&p, &ts, sizeof(ts));
2129 BUG_ON(p > end);
2130 msg->front.iov_len = p - msg->front.iov_base;
2131 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2133 if (req->r_pagelist) {
2134 struct ceph_pagelist *pagelist = req->r_pagelist;
2135 ceph_msg_data_add_pagelist(msg, pagelist);
2136 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2137 } else {
2138 msg->hdr.data_len = 0;
2141 msg->hdr.data_off = cpu_to_le16(0);
2143 out_free2:
2144 if (freepath2)
2145 kfree((char *)path2);
2146 out_free1:
2147 if (freepath1)
2148 kfree((char *)path1);
2149 out:
2150 return msg;
2154 * called under mdsc->mutex if error, under no mutex if
2155 * success.
2157 static void complete_request(struct ceph_mds_client *mdsc,
2158 struct ceph_mds_request *req)
2160 if (req->r_callback)
2161 req->r_callback(mdsc, req);
2162 else
2163 complete_all(&req->r_completion);
2167 * called under mdsc->mutex
2169 static int __prepare_send_request(struct ceph_mds_client *mdsc,
2170 struct ceph_mds_request *req,
2171 int mds, bool drop_cap_releases)
2173 struct ceph_mds_request_head *rhead;
2174 struct ceph_msg *msg;
2175 int flags = 0;
2177 req->r_attempts++;
2178 if (req->r_inode) {
2179 struct ceph_cap *cap =
2180 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2182 if (cap)
2183 req->r_sent_on_mseq = cap->mseq;
2184 else
2185 req->r_sent_on_mseq = -1;
2187 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2188 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2190 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2191 void *p;
2193 * Replay. Do not regenerate message (and rebuild
2194 * paths, etc.); just use the original message.
2195 * Rebuilding paths will break for renames because
2196 * d_move mangles the src name.
2198 msg = req->r_request;
2199 rhead = msg->front.iov_base;
2201 flags = le32_to_cpu(rhead->flags);
2202 flags |= CEPH_MDS_FLAG_REPLAY;
2203 rhead->flags = cpu_to_le32(flags);
2205 if (req->r_target_inode)
2206 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2208 rhead->num_retry = req->r_attempts - 1;
2210 /* remove cap/dentry releases from message */
2211 rhead->num_releases = 0;
2213 /* time stamp */
2214 p = msg->front.iov_base + req->r_request_release_offset;
2216 struct ceph_timespec ts;
2217 ceph_encode_timespec64(&ts, &req->r_stamp);
2218 ceph_encode_copy(&p, &ts, sizeof(ts));
2221 msg->front.iov_len = p - msg->front.iov_base;
2222 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2223 return 0;
2226 if (req->r_request) {
2227 ceph_msg_put(req->r_request);
2228 req->r_request = NULL;
2230 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
2231 if (IS_ERR(msg)) {
2232 req->r_err = PTR_ERR(msg);
2233 return PTR_ERR(msg);
2235 req->r_request = msg;
2237 rhead = msg->front.iov_base;
2238 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
2239 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2240 flags |= CEPH_MDS_FLAG_REPLAY;
2241 if (req->r_parent)
2242 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2243 rhead->flags = cpu_to_le32(flags);
2244 rhead->num_fwd = req->r_num_fwd;
2245 rhead->num_retry = req->r_attempts - 1;
2246 rhead->ino = 0;
2248 dout(" r_parent = %p\n", req->r_parent);
2249 return 0;
2253 * send request, or put it on the appropriate wait list.
2255 static void __do_request(struct ceph_mds_client *mdsc,
2256 struct ceph_mds_request *req)
2258 struct ceph_mds_session *session = NULL;
2259 int mds = -1;
2260 int err = 0;
2262 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2263 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
2264 __unregister_request(mdsc, req);
2265 return;
2268 if (req->r_timeout &&
2269 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2270 dout("do_request timed out\n");
2271 err = -EIO;
2272 goto finish;
2274 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
2275 dout("do_request forced umount\n");
2276 err = -EIO;
2277 goto finish;
2279 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
2280 if (mdsc->mdsmap_err) {
2281 err = mdsc->mdsmap_err;
2282 dout("do_request mdsmap err %d\n", err);
2283 goto finish;
2285 if (mdsc->mdsmap->m_epoch == 0) {
2286 dout("do_request no mdsmap, waiting for map\n");
2287 list_add(&req->r_wait, &mdsc->waiting_for_map);
2288 return;
2290 if (!(mdsc->fsc->mount_options->flags &
2291 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2292 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
2293 err = -ENOENT;
2294 pr_info("probably no mds server is up\n");
2295 goto finish;
2299 put_request_session(req);
2301 mds = __choose_mds(mdsc, req);
2302 if (mds < 0 ||
2303 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2304 dout("do_request no mds or not active, waiting for map\n");
2305 list_add(&req->r_wait, &mdsc->waiting_for_map);
2306 return;
2309 /* get, open session */
2310 session = __ceph_lookup_mds_session(mdsc, mds);
2311 if (!session) {
2312 session = register_session(mdsc, mds);
2313 if (IS_ERR(session)) {
2314 err = PTR_ERR(session);
2315 goto finish;
2318 req->r_session = get_session(session);
2320 dout("do_request mds%d session %p state %s\n", mds, session,
2321 ceph_session_state_name(session->s_state));
2322 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2323 session->s_state != CEPH_MDS_SESSION_HUNG) {
2324 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2325 err = -EACCES;
2326 goto out_session;
2328 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2329 session->s_state == CEPH_MDS_SESSION_CLOSING)
2330 __open_session(mdsc, session);
2331 list_add(&req->r_wait, &session->s_waiting);
2332 goto out_session;
2335 /* send request */
2336 req->r_resend_mds = -1; /* forget any previous mds hint */
2338 if (req->r_request_started == 0) /* note request start time */
2339 req->r_request_started = jiffies;
2341 err = __prepare_send_request(mdsc, req, mds, false);
2342 if (!err) {
2343 ceph_msg_get(req->r_request);
2344 ceph_con_send(&session->s_con, req->r_request);
2347 out_session:
2348 ceph_put_mds_session(session);
2349 finish:
2350 if (err) {
2351 dout("__do_request early error %d\n", err);
2352 req->r_err = err;
2353 complete_request(mdsc, req);
2354 __unregister_request(mdsc, req);
2356 return;
2360 * called under mdsc->mutex
2362 static void __wake_requests(struct ceph_mds_client *mdsc,
2363 struct list_head *head)
2365 struct ceph_mds_request *req;
2366 LIST_HEAD(tmp_list);
2368 list_splice_init(head, &tmp_list);
2370 while (!list_empty(&tmp_list)) {
2371 req = list_entry(tmp_list.next,
2372 struct ceph_mds_request, r_wait);
2373 list_del_init(&req->r_wait);
2374 dout(" wake request %p tid %llu\n", req, req->r_tid);
2375 __do_request(mdsc, req);
2380 * Wake up threads with requests pending for @mds, so that they can
2381 * resubmit their requests to a possibly different mds.
2383 static void kick_requests(struct ceph_mds_client *mdsc, int mds)
2385 struct ceph_mds_request *req;
2386 struct rb_node *p = rb_first(&mdsc->request_tree);
2388 dout("kick_requests mds%d\n", mds);
2389 while (p) {
2390 req = rb_entry(p, struct ceph_mds_request, r_node);
2391 p = rb_next(p);
2392 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2393 continue;
2394 if (req->r_attempts > 0)
2395 continue; /* only new requests */
2396 if (req->r_session &&
2397 req->r_session->s_mds == mds) {
2398 dout(" kicking tid %llu\n", req->r_tid);
2399 list_del_init(&req->r_wait);
2400 __do_request(mdsc, req);
2405 void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2406 struct ceph_mds_request *req)
2408 dout("submit_request on %p\n", req);
2409 mutex_lock(&mdsc->mutex);
2410 __register_request(mdsc, req, NULL);
2411 __do_request(mdsc, req);
2412 mutex_unlock(&mdsc->mutex);
2416 * Synchrously perform an mds request. Take care of all of the
2417 * session setup, forwarding, retry details.
2419 int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2420 struct inode *dir,
2421 struct ceph_mds_request *req)
2423 int err;
2425 dout("do_request on %p\n", req);
2427 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
2428 if (req->r_inode)
2429 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
2430 if (req->r_parent)
2431 ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
2432 if (req->r_old_dentry_dir)
2433 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2434 CEPH_CAP_PIN);
2436 /* issue */
2437 mutex_lock(&mdsc->mutex);
2438 __register_request(mdsc, req, dir);
2439 __do_request(mdsc, req);
2441 if (req->r_err) {
2442 err = req->r_err;
2443 goto out;
2446 /* wait */
2447 mutex_unlock(&mdsc->mutex);
2448 dout("do_request waiting\n");
2449 if (!req->r_timeout && req->r_wait_for_completion) {
2450 err = req->r_wait_for_completion(mdsc, req);
2451 } else {
2452 long timeleft = wait_for_completion_killable_timeout(
2453 &req->r_completion,
2454 ceph_timeout_jiffies(req->r_timeout));
2455 if (timeleft > 0)
2456 err = 0;
2457 else if (!timeleft)
2458 err = -EIO; /* timed out */
2459 else
2460 err = timeleft; /* killed */
2462 dout("do_request waited, got %d\n", err);
2463 mutex_lock(&mdsc->mutex);
2465 /* only abort if we didn't race with a real reply */
2466 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2467 err = le32_to_cpu(req->r_reply_info.head->result);
2468 } else if (err < 0) {
2469 dout("aborted request %lld with %d\n", req->r_tid, err);
2472 * ensure we aren't running concurrently with
2473 * ceph_fill_trace or ceph_readdir_prepopulate, which
2474 * rely on locks (dir mutex) held by our caller.
2476 mutex_lock(&req->r_fill_mutex);
2477 req->r_err = err;
2478 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
2479 mutex_unlock(&req->r_fill_mutex);
2481 if (req->r_parent &&
2482 (req->r_op & CEPH_MDS_OP_WRITE))
2483 ceph_invalidate_dir_request(req);
2484 } else {
2485 err = req->r_err;
2488 out:
2489 mutex_unlock(&mdsc->mutex);
2490 dout("do_request %p done, result %d\n", req, err);
2491 return err;
2495 * Invalidate dir's completeness, dentry lease state on an aborted MDS
2496 * namespace request.
2498 void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2500 struct inode *dir = req->r_parent;
2501 struct inode *old_dir = req->r_old_dentry_dir;
2503 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
2505 ceph_dir_clear_complete(dir);
2506 if (old_dir)
2507 ceph_dir_clear_complete(old_dir);
2508 if (req->r_dentry)
2509 ceph_invalidate_dentry_lease(req->r_dentry);
2510 if (req->r_old_dentry)
2511 ceph_invalidate_dentry_lease(req->r_old_dentry);
2515 * Handle mds reply.
2517 * We take the session mutex and parse and process the reply immediately.
2518 * This preserves the logical ordering of replies, capabilities, etc., sent
2519 * by the MDS as they are applied to our local cache.
2521 static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2523 struct ceph_mds_client *mdsc = session->s_mdsc;
2524 struct ceph_mds_request *req;
2525 struct ceph_mds_reply_head *head = msg->front.iov_base;
2526 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
2527 struct ceph_snap_realm *realm;
2528 u64 tid;
2529 int err, result;
2530 int mds = session->s_mds;
2532 if (msg->front.iov_len < sizeof(*head)) {
2533 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
2534 ceph_msg_dump(msg);
2535 return;
2538 /* get request, session */
2539 tid = le64_to_cpu(msg->hdr.tid);
2540 mutex_lock(&mdsc->mutex);
2541 req = lookup_get_request(mdsc, tid);
2542 if (!req) {
2543 dout("handle_reply on unknown tid %llu\n", tid);
2544 mutex_unlock(&mdsc->mutex);
2545 return;
2547 dout("handle_reply %p\n", req);
2549 /* correct session? */
2550 if (req->r_session != session) {
2551 pr_err("mdsc_handle_reply got %llu on session mds%d"
2552 " not mds%d\n", tid, session->s_mds,
2553 req->r_session ? req->r_session->s_mds : -1);
2554 mutex_unlock(&mdsc->mutex);
2555 goto out;
2558 /* dup? */
2559 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
2560 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
2561 pr_warn("got a dup %s reply on %llu from mds%d\n",
2562 head->safe ? "safe" : "unsafe", tid, mds);
2563 mutex_unlock(&mdsc->mutex);
2564 goto out;
2566 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
2567 pr_warn("got unsafe after safe on %llu from mds%d\n",
2568 tid, mds);
2569 mutex_unlock(&mdsc->mutex);
2570 goto out;
2573 result = le32_to_cpu(head->result);
2576 * Handle an ESTALE
2577 * if we're not talking to the authority, send to them
2578 * if the authority has changed while we weren't looking,
2579 * send to new authority
2580 * Otherwise we just have to return an ESTALE
2582 if (result == -ESTALE) {
2583 dout("got ESTALE on request %llu\n", req->r_tid);
2584 req->r_resend_mds = -1;
2585 if (req->r_direct_mode != USE_AUTH_MDS) {
2586 dout("not using auth, setting for that now\n");
2587 req->r_direct_mode = USE_AUTH_MDS;
2588 __do_request(mdsc, req);
2589 mutex_unlock(&mdsc->mutex);
2590 goto out;
2591 } else {
2592 int mds = __choose_mds(mdsc, req);
2593 if (mds >= 0 && mds != req->r_session->s_mds) {
2594 dout("but auth changed, so resending\n");
2595 __do_request(mdsc, req);
2596 mutex_unlock(&mdsc->mutex);
2597 goto out;
2600 dout("have to return ESTALE on request %llu\n", req->r_tid);
2604 if (head->safe) {
2605 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
2606 __unregister_request(mdsc, req);
2608 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2610 * We already handled the unsafe response, now do the
2611 * cleanup. No need to examine the response; the MDS
2612 * doesn't include any result info in the safe
2613 * response. And even if it did, there is nothing
2614 * useful we could do with a revised return value.
2616 dout("got safe reply %llu, mds%d\n", tid, mds);
2618 /* last unsafe request during umount? */
2619 if (mdsc->stopping && !__get_oldest_req(mdsc))
2620 complete_all(&mdsc->safe_umount_waiters);
2621 mutex_unlock(&mdsc->mutex);
2622 goto out;
2624 } else {
2625 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
2626 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
2627 if (req->r_unsafe_dir) {
2628 struct ceph_inode_info *ci =
2629 ceph_inode(req->r_unsafe_dir);
2630 spin_lock(&ci->i_unsafe_lock);
2631 list_add_tail(&req->r_unsafe_dir_item,
2632 &ci->i_unsafe_dirops);
2633 spin_unlock(&ci->i_unsafe_lock);
2637 dout("handle_reply tid %lld result %d\n", tid, result);
2638 rinfo = &req->r_reply_info;
2639 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2640 mutex_unlock(&mdsc->mutex);
2642 mutex_lock(&session->s_mutex);
2643 if (err < 0) {
2644 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
2645 ceph_msg_dump(msg);
2646 goto out_err;
2649 /* snap trace */
2650 realm = NULL;
2651 if (rinfo->snapblob_len) {
2652 down_write(&mdsc->snap_rwsem);
2653 ceph_update_snap_trace(mdsc, rinfo->snapblob,
2654 rinfo->snapblob + rinfo->snapblob_len,
2655 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2656 &realm);
2657 downgrade_write(&mdsc->snap_rwsem);
2658 } else {
2659 down_read(&mdsc->snap_rwsem);
2662 /* insert trace into our cache */
2663 mutex_lock(&req->r_fill_mutex);
2664 current->journal_info = req;
2665 err = ceph_fill_trace(mdsc->fsc->sb, req);
2666 if (err == 0) {
2667 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
2668 req->r_op == CEPH_MDS_OP_LSSNAP))
2669 ceph_readdir_prepopulate(req, req->r_session);
2670 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2672 current->journal_info = NULL;
2673 mutex_unlock(&req->r_fill_mutex);
2675 up_read(&mdsc->snap_rwsem);
2676 if (realm)
2677 ceph_put_snap_realm(mdsc, realm);
2679 if (err == 0 && req->r_target_inode &&
2680 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2681 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
2682 spin_lock(&ci->i_unsafe_lock);
2683 list_add_tail(&req->r_unsafe_target_item, &ci->i_unsafe_iops);
2684 spin_unlock(&ci->i_unsafe_lock);
2686 out_err:
2687 mutex_lock(&mdsc->mutex);
2688 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
2689 if (err) {
2690 req->r_err = err;
2691 } else {
2692 req->r_reply = ceph_msg_get(msg);
2693 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
2695 } else {
2696 dout("reply arrived after request %lld was aborted\n", tid);
2698 mutex_unlock(&mdsc->mutex);
2700 mutex_unlock(&session->s_mutex);
2702 /* kick calling process */
2703 complete_request(mdsc, req);
2704 out:
2705 ceph_mdsc_put_request(req);
2706 return;
2712 * handle mds notification that our request has been forwarded.
2714 static void handle_forward(struct ceph_mds_client *mdsc,
2715 struct ceph_mds_session *session,
2716 struct ceph_msg *msg)
2718 struct ceph_mds_request *req;
2719 u64 tid = le64_to_cpu(msg->hdr.tid);
2720 u32 next_mds;
2721 u32 fwd_seq;
2722 int err = -EINVAL;
2723 void *p = msg->front.iov_base;
2724 void *end = p + msg->front.iov_len;
2726 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
2727 next_mds = ceph_decode_32(&p);
2728 fwd_seq = ceph_decode_32(&p);
2730 mutex_lock(&mdsc->mutex);
2731 req = lookup_get_request(mdsc, tid);
2732 if (!req) {
2733 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2734 goto out; /* dup reply? */
2737 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
2738 dout("forward tid %llu aborted, unregistering\n", tid);
2739 __unregister_request(mdsc, req);
2740 } else if (fwd_seq <= req->r_num_fwd) {
2741 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2742 tid, next_mds, req->r_num_fwd, fwd_seq);
2743 } else {
2744 /* resend. forward race not possible; mds would drop */
2745 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2746 BUG_ON(req->r_err);
2747 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
2748 req->r_attempts = 0;
2749 req->r_num_fwd = fwd_seq;
2750 req->r_resend_mds = next_mds;
2751 put_request_session(req);
2752 __do_request(mdsc, req);
2754 ceph_mdsc_put_request(req);
2755 out:
2756 mutex_unlock(&mdsc->mutex);
2757 return;
2759 bad:
2760 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2764 * handle a mds session control message
2766 static void handle_session(struct ceph_mds_session *session,
2767 struct ceph_msg *msg)
2769 struct ceph_mds_client *mdsc = session->s_mdsc;
2770 u32 op;
2771 u64 seq;
2772 int mds = session->s_mds;
2773 struct ceph_mds_session_head *h = msg->front.iov_base;
2774 int wake = 0;
2776 /* decode */
2777 if (msg->front.iov_len < sizeof(*h))
2778 goto bad;
2779 op = le32_to_cpu(h->op);
2780 seq = le64_to_cpu(h->seq);
2782 mutex_lock(&mdsc->mutex);
2783 if (op == CEPH_SESSION_CLOSE) {
2784 get_session(session);
2785 __unregister_session(mdsc, session);
2787 /* FIXME: this ttl calculation is generous */
2788 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2789 mutex_unlock(&mdsc->mutex);
2791 mutex_lock(&session->s_mutex);
2793 dout("handle_session mds%d %s %p state %s seq %llu\n",
2794 mds, ceph_session_op_name(op), session,
2795 ceph_session_state_name(session->s_state), seq);
2797 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2798 session->s_state = CEPH_MDS_SESSION_OPEN;
2799 pr_info("mds%d came back\n", session->s_mds);
2802 switch (op) {
2803 case CEPH_SESSION_OPEN:
2804 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2805 pr_info("mds%d reconnect success\n", session->s_mds);
2806 session->s_state = CEPH_MDS_SESSION_OPEN;
2807 renewed_caps(mdsc, session, 0);
2808 wake = 1;
2809 if (mdsc->stopping)
2810 __close_session(mdsc, session);
2811 break;
2813 case CEPH_SESSION_RENEWCAPS:
2814 if (session->s_renew_seq == seq)
2815 renewed_caps(mdsc, session, 1);
2816 break;
2818 case CEPH_SESSION_CLOSE:
2819 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2820 pr_info("mds%d reconnect denied\n", session->s_mds);
2821 cleanup_session_requests(mdsc, session);
2822 remove_session_caps(session);
2823 wake = 2; /* for good measure */
2824 wake_up_all(&mdsc->session_close_wq);
2825 break;
2827 case CEPH_SESSION_STALE:
2828 pr_info("mds%d caps went stale, renewing\n",
2829 session->s_mds);
2830 spin_lock(&session->s_gen_ttl_lock);
2831 session->s_cap_gen++;
2832 session->s_cap_ttl = jiffies - 1;
2833 spin_unlock(&session->s_gen_ttl_lock);
2834 send_renew_caps(mdsc, session);
2835 break;
2837 case CEPH_SESSION_RECALL_STATE:
2838 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2839 break;
2841 case CEPH_SESSION_FLUSHMSG:
2842 send_flushmsg_ack(mdsc, session, seq);
2843 break;
2845 case CEPH_SESSION_FORCE_RO:
2846 dout("force_session_readonly %p\n", session);
2847 spin_lock(&session->s_cap_lock);
2848 session->s_readonly = true;
2849 spin_unlock(&session->s_cap_lock);
2850 wake_up_session_caps(session, 0);
2851 break;
2853 case CEPH_SESSION_REJECT:
2854 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
2855 pr_info("mds%d rejected session\n", session->s_mds);
2856 session->s_state = CEPH_MDS_SESSION_REJECTED;
2857 cleanup_session_requests(mdsc, session);
2858 remove_session_caps(session);
2859 wake = 2; /* for good measure */
2860 break;
2862 default:
2863 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2864 WARN_ON(1);
2867 mutex_unlock(&session->s_mutex);
2868 if (wake) {
2869 mutex_lock(&mdsc->mutex);
2870 __wake_requests(mdsc, &session->s_waiting);
2871 if (wake == 2)
2872 kick_requests(mdsc, mds);
2873 mutex_unlock(&mdsc->mutex);
2875 if (op == CEPH_SESSION_CLOSE)
2876 ceph_put_mds_session(session);
2877 return;
2879 bad:
2880 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2881 (int)msg->front.iov_len);
2882 ceph_msg_dump(msg);
2883 return;
2888 * called under session->mutex.
2890 static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2891 struct ceph_mds_session *session)
2893 struct ceph_mds_request *req, *nreq;
2894 struct rb_node *p;
2895 int err;
2897 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2899 mutex_lock(&mdsc->mutex);
2900 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
2901 err = __prepare_send_request(mdsc, req, session->s_mds, true);
2902 if (!err) {
2903 ceph_msg_get(req->r_request);
2904 ceph_con_send(&session->s_con, req->r_request);
2909 * also re-send old requests when MDS enters reconnect stage. So that MDS
2910 * can process completed request in clientreplay stage.
2912 p = rb_first(&mdsc->request_tree);
2913 while (p) {
2914 req = rb_entry(p, struct ceph_mds_request, r_node);
2915 p = rb_next(p);
2916 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2917 continue;
2918 if (req->r_attempts == 0)
2919 continue; /* only old requests */
2920 if (req->r_session &&
2921 req->r_session->s_mds == session->s_mds) {
2922 err = __prepare_send_request(mdsc, req,
2923 session->s_mds, true);
2924 if (!err) {
2925 ceph_msg_get(req->r_request);
2926 ceph_con_send(&session->s_con, req->r_request);
2930 mutex_unlock(&mdsc->mutex);
2934 * Encode information about a cap for a reconnect with the MDS.
2936 static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2937 void *arg)
2939 union {
2940 struct ceph_mds_cap_reconnect v2;
2941 struct ceph_mds_cap_reconnect_v1 v1;
2942 } rec;
2943 struct ceph_inode_info *ci = cap->ci;
2944 struct ceph_reconnect_state *recon_state = arg;
2945 struct ceph_pagelist *pagelist = recon_state->pagelist;
2946 char *path;
2947 int pathlen, err;
2948 u64 pathbase;
2949 u64 snap_follows;
2950 struct dentry *dentry;
2952 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2953 inode, ceph_vinop(inode), cap, cap->cap_id,
2954 ceph_cap_string(cap->issued));
2955 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2956 if (err)
2957 return err;
2959 dentry = d_find_alias(inode);
2960 if (dentry) {
2961 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2962 if (IS_ERR(path)) {
2963 err = PTR_ERR(path);
2964 goto out_dput;
2966 } else {
2967 path = NULL;
2968 pathlen = 0;
2969 pathbase = 0;
2972 spin_lock(&ci->i_ceph_lock);
2973 cap->seq = 0; /* reset cap seq */
2974 cap->issue_seq = 0; /* and issue_seq */
2975 cap->mseq = 0; /* and migrate_seq */
2976 cap->cap_gen = cap->session->s_cap_gen;
2978 if (recon_state->msg_version >= 2) {
2979 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2980 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2981 rec.v2.issued = cpu_to_le32(cap->issued);
2982 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2983 rec.v2.pathbase = cpu_to_le64(pathbase);
2984 rec.v2.flock_len = (__force __le32)
2985 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
2986 } else {
2987 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2988 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2989 rec.v1.issued = cpu_to_le32(cap->issued);
2990 rec.v1.size = cpu_to_le64(inode->i_size);
2991 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
2992 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
2993 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2994 rec.v1.pathbase = cpu_to_le64(pathbase);
2997 if (list_empty(&ci->i_cap_snaps)) {
2998 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
2999 } else {
3000 struct ceph_cap_snap *capsnap =
3001 list_first_entry(&ci->i_cap_snaps,
3002 struct ceph_cap_snap, ci_item);
3003 snap_follows = capsnap->follows;
3005 spin_unlock(&ci->i_ceph_lock);
3007 if (recon_state->msg_version >= 2) {
3008 int num_fcntl_locks, num_flock_locks;
3009 struct ceph_filelock *flocks = NULL;
3010 size_t struct_len, total_len = 0;
3011 u8 struct_v = 0;
3013 encode_again:
3014 if (rec.v2.flock_len) {
3015 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
3016 } else {
3017 num_fcntl_locks = 0;
3018 num_flock_locks = 0;
3020 if (num_fcntl_locks + num_flock_locks > 0) {
3021 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
3022 sizeof(struct ceph_filelock),
3023 GFP_NOFS);
3024 if (!flocks) {
3025 err = -ENOMEM;
3026 goto out_free;
3028 err = ceph_encode_locks_to_buffer(inode, flocks,
3029 num_fcntl_locks,
3030 num_flock_locks);
3031 if (err) {
3032 kfree(flocks);
3033 flocks = NULL;
3034 if (err == -ENOSPC)
3035 goto encode_again;
3036 goto out_free;
3038 } else {
3039 kfree(flocks);
3040 flocks = NULL;
3043 if (recon_state->msg_version >= 3) {
3044 /* version, compat_version and struct_len */
3045 total_len = 2 * sizeof(u8) + sizeof(u32);
3046 struct_v = 2;
3049 * number of encoded locks is stable, so copy to pagelist
3051 struct_len = 2 * sizeof(u32) +
3052 (num_fcntl_locks + num_flock_locks) *
3053 sizeof(struct ceph_filelock);
3054 rec.v2.flock_len = cpu_to_le32(struct_len);
3056 struct_len += sizeof(rec.v2);
3057 struct_len += sizeof(u32) + pathlen;
3059 if (struct_v >= 2)
3060 struct_len += sizeof(u64); /* snap_follows */
3062 total_len += struct_len;
3063 err = ceph_pagelist_reserve(pagelist, total_len);
3065 if (!err) {
3066 if (recon_state->msg_version >= 3) {
3067 ceph_pagelist_encode_8(pagelist, struct_v);
3068 ceph_pagelist_encode_8(pagelist, 1);
3069 ceph_pagelist_encode_32(pagelist, struct_len);
3071 ceph_pagelist_encode_string(pagelist, path, pathlen);
3072 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3073 ceph_locks_to_pagelist(flocks, pagelist,
3074 num_fcntl_locks,
3075 num_flock_locks);
3076 if (struct_v >= 2)
3077 ceph_pagelist_encode_64(pagelist, snap_follows);
3079 kfree(flocks);
3080 } else {
3081 size_t size = sizeof(u32) + pathlen + sizeof(rec.v1);
3082 err = ceph_pagelist_reserve(pagelist, size);
3083 if (!err) {
3084 ceph_pagelist_encode_string(pagelist, path, pathlen);
3085 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
3089 recon_state->nr_caps++;
3090 out_free:
3091 kfree(path);
3092 out_dput:
3093 dput(dentry);
3094 return err;
3099 * If an MDS fails and recovers, clients need to reconnect in order to
3100 * reestablish shared state. This includes all caps issued through
3101 * this session _and_ the snap_realm hierarchy. Because it's not
3102 * clear which snap realms the mds cares about, we send everything we
3103 * know about.. that ensures we'll then get any new info the
3104 * recovering MDS might have.
3106 * This is a relatively heavyweight operation, but it's rare.
3108 * called with mdsc->mutex held.
3110 static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3111 struct ceph_mds_session *session)
3113 struct ceph_msg *reply;
3114 struct rb_node *p;
3115 int mds = session->s_mds;
3116 int err = -ENOMEM;
3117 int s_nr_caps;
3118 struct ceph_pagelist *pagelist;
3119 struct ceph_reconnect_state recon_state;
3120 LIST_HEAD(dispose);
3122 pr_info("mds%d reconnect start\n", mds);
3124 pagelist = ceph_pagelist_alloc(GFP_NOFS);
3125 if (!pagelist)
3126 goto fail_nopagelist;
3128 reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
3129 if (!reply)
3130 goto fail_nomsg;
3132 mutex_lock(&session->s_mutex);
3133 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3134 session->s_seq = 0;
3136 dout("session %p state %s\n", session,
3137 ceph_session_state_name(session->s_state));
3139 spin_lock(&session->s_gen_ttl_lock);
3140 session->s_cap_gen++;
3141 spin_unlock(&session->s_gen_ttl_lock);
3143 spin_lock(&session->s_cap_lock);
3144 /* don't know if session is readonly */
3145 session->s_readonly = 0;
3147 * notify __ceph_remove_cap() that we are composing cap reconnect.
3148 * If a cap get released before being added to the cap reconnect,
3149 * __ceph_remove_cap() should skip queuing cap release.
3151 session->s_cap_reconnect = 1;
3152 /* drop old cap expires; we're about to reestablish that state */
3153 detach_cap_releases(session, &dispose);
3154 spin_unlock(&session->s_cap_lock);
3155 dispose_cap_releases(mdsc, &dispose);
3157 /* trim unused caps to reduce MDS's cache rejoin time */
3158 if (mdsc->fsc->sb->s_root)
3159 shrink_dcache_parent(mdsc->fsc->sb->s_root);
3161 ceph_con_close(&session->s_con);
3162 ceph_con_open(&session->s_con,
3163 CEPH_ENTITY_TYPE_MDS, mds,
3164 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3166 /* replay unsafe requests */
3167 replay_unsafe_requests(mdsc, session);
3169 down_read(&mdsc->snap_rwsem);
3171 /* traverse this session's caps */
3172 s_nr_caps = session->s_nr_caps;
3173 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
3174 if (err)
3175 goto fail;
3177 recon_state.nr_caps = 0;
3178 recon_state.pagelist = pagelist;
3179 if (session->s_con.peer_features & CEPH_FEATURE_MDSENC)
3180 recon_state.msg_version = 3;
3181 else
3182 recon_state.msg_version = 2;
3183 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
3184 if (err < 0)
3185 goto fail;
3187 spin_lock(&session->s_cap_lock);
3188 session->s_cap_reconnect = 0;
3189 spin_unlock(&session->s_cap_lock);
3192 * snaprealms. we provide mds with the ino, seq (version), and
3193 * parent for all of our realms. If the mds has any newer info,
3194 * it will tell us.
3196 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3197 struct ceph_snap_realm *realm =
3198 rb_entry(p, struct ceph_snap_realm, node);
3199 struct ceph_mds_snaprealm_reconnect sr_rec;
3201 dout(" adding snap realm %llx seq %lld parent %llx\n",
3202 realm->ino, realm->seq, realm->parent_ino);
3203 sr_rec.ino = cpu_to_le64(realm->ino);
3204 sr_rec.seq = cpu_to_le64(realm->seq);
3205 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3206 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3207 if (err)
3208 goto fail;
3211 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
3213 /* raced with cap release? */
3214 if (s_nr_caps != recon_state.nr_caps) {
3215 struct page *page = list_first_entry(&pagelist->head,
3216 struct page, lru);
3217 __le32 *addr = kmap_atomic(page);
3218 *addr = cpu_to_le32(recon_state.nr_caps);
3219 kunmap_atomic(addr);
3222 reply->hdr.data_len = cpu_to_le32(pagelist->length);
3223 ceph_msg_data_add_pagelist(reply, pagelist);
3225 ceph_early_kick_flushing_caps(mdsc, session);
3227 ceph_con_send(&session->s_con, reply);
3229 mutex_unlock(&session->s_mutex);
3231 mutex_lock(&mdsc->mutex);
3232 __wake_requests(mdsc, &session->s_waiting);
3233 mutex_unlock(&mdsc->mutex);
3235 up_read(&mdsc->snap_rwsem);
3236 ceph_pagelist_release(pagelist);
3237 return;
3239 fail:
3240 ceph_msg_put(reply);
3241 up_read(&mdsc->snap_rwsem);
3242 mutex_unlock(&session->s_mutex);
3243 fail_nomsg:
3244 ceph_pagelist_release(pagelist);
3245 fail_nopagelist:
3246 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
3247 return;
3252 * compare old and new mdsmaps, kicking requests
3253 * and closing out old connections as necessary
3255 * called under mdsc->mutex.
3257 static void check_new_map(struct ceph_mds_client *mdsc,
3258 struct ceph_mdsmap *newmap,
3259 struct ceph_mdsmap *oldmap)
3261 int i;
3262 int oldstate, newstate;
3263 struct ceph_mds_session *s;
3265 dout("check_new_map new %u old %u\n",
3266 newmap->m_epoch, oldmap->m_epoch);
3268 for (i = 0; i < oldmap->m_num_mds && i < mdsc->max_sessions; i++) {
3269 if (!mdsc->sessions[i])
3270 continue;
3271 s = mdsc->sessions[i];
3272 oldstate = ceph_mdsmap_get_state(oldmap, i);
3273 newstate = ceph_mdsmap_get_state(newmap, i);
3275 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
3276 i, ceph_mds_state_name(oldstate),
3277 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
3278 ceph_mds_state_name(newstate),
3279 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
3280 ceph_session_state_name(s->s_state));
3282 if (i >= newmap->m_num_mds ||
3283 memcmp(ceph_mdsmap_get_addr(oldmap, i),
3284 ceph_mdsmap_get_addr(newmap, i),
3285 sizeof(struct ceph_entity_addr))) {
3286 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
3287 /* the session never opened, just close it
3288 * out now */
3289 get_session(s);
3290 __unregister_session(mdsc, s);
3291 __wake_requests(mdsc, &s->s_waiting);
3292 ceph_put_mds_session(s);
3293 } else if (i >= newmap->m_num_mds) {
3294 /* force close session for stopped mds */
3295 get_session(s);
3296 __unregister_session(mdsc, s);
3297 __wake_requests(mdsc, &s->s_waiting);
3298 kick_requests(mdsc, i);
3299 mutex_unlock(&mdsc->mutex);
3301 mutex_lock(&s->s_mutex);
3302 cleanup_session_requests(mdsc, s);
3303 remove_session_caps(s);
3304 mutex_unlock(&s->s_mutex);
3306 ceph_put_mds_session(s);
3308 mutex_lock(&mdsc->mutex);
3309 } else {
3310 /* just close it */
3311 mutex_unlock(&mdsc->mutex);
3312 mutex_lock(&s->s_mutex);
3313 mutex_lock(&mdsc->mutex);
3314 ceph_con_close(&s->s_con);
3315 mutex_unlock(&s->s_mutex);
3316 s->s_state = CEPH_MDS_SESSION_RESTARTING;
3318 } else if (oldstate == newstate) {
3319 continue; /* nothing new with this mds */
3323 * send reconnect?
3325 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
3326 newstate >= CEPH_MDS_STATE_RECONNECT) {
3327 mutex_unlock(&mdsc->mutex);
3328 send_mds_reconnect(mdsc, s);
3329 mutex_lock(&mdsc->mutex);
3333 * kick request on any mds that has gone active.
3335 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3336 newstate >= CEPH_MDS_STATE_ACTIVE) {
3337 if (oldstate != CEPH_MDS_STATE_CREATING &&
3338 oldstate != CEPH_MDS_STATE_STARTING)
3339 pr_info("mds%d recovery completed\n", s->s_mds);
3340 kick_requests(mdsc, i);
3341 ceph_kick_flushing_caps(mdsc, s);
3342 wake_up_session_caps(s, 1);
3346 for (i = 0; i < newmap->m_num_mds && i < mdsc->max_sessions; i++) {
3347 s = mdsc->sessions[i];
3348 if (!s)
3349 continue;
3350 if (!ceph_mdsmap_is_laggy(newmap, i))
3351 continue;
3352 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3353 s->s_state == CEPH_MDS_SESSION_HUNG ||
3354 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3355 dout(" connecting to export targets of laggy mds%d\n",
3357 __open_export_target_sessions(mdsc, s);
3365 * leases
3369 * caller must hold session s_mutex, dentry->d_lock
3371 void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3373 struct ceph_dentry_info *di = ceph_dentry(dentry);
3375 ceph_put_mds_session(di->lease_session);
3376 di->lease_session = NULL;
3379 static void handle_lease(struct ceph_mds_client *mdsc,
3380 struct ceph_mds_session *session,
3381 struct ceph_msg *msg)
3383 struct super_block *sb = mdsc->fsc->sb;
3384 struct inode *inode;
3385 struct dentry *parent, *dentry;
3386 struct ceph_dentry_info *di;
3387 int mds = session->s_mds;
3388 struct ceph_mds_lease *h = msg->front.iov_base;
3389 u32 seq;
3390 struct ceph_vino vino;
3391 struct qstr dname;
3392 int release = 0;
3394 dout("handle_lease from mds%d\n", mds);
3396 /* decode */
3397 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3398 goto bad;
3399 vino.ino = le64_to_cpu(h->ino);
3400 vino.snap = CEPH_NOSNAP;
3401 seq = le32_to_cpu(h->seq);
3402 dname.len = get_unaligned_le32(h + 1);
3403 if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
3404 goto bad;
3405 dname.name = (void *)(h + 1) + sizeof(u32);
3407 /* lookup inode */
3408 inode = ceph_find_inode(sb, vino);
3409 dout("handle_lease %s, ino %llx %p %.*s\n",
3410 ceph_lease_op_name(h->action), vino.ino, inode,
3411 dname.len, dname.name);
3413 mutex_lock(&session->s_mutex);
3414 session->s_seq++;
3416 if (!inode) {
3417 dout("handle_lease no inode %llx\n", vino.ino);
3418 goto release;
3421 /* dentry */
3422 parent = d_find_alias(inode);
3423 if (!parent) {
3424 dout("no parent dentry on inode %p\n", inode);
3425 WARN_ON(1);
3426 goto release; /* hrm... */
3428 dname.hash = full_name_hash(parent, dname.name, dname.len);
3429 dentry = d_lookup(parent, &dname);
3430 dput(parent);
3431 if (!dentry)
3432 goto release;
3434 spin_lock(&dentry->d_lock);
3435 di = ceph_dentry(dentry);
3436 switch (h->action) {
3437 case CEPH_MDS_LEASE_REVOKE:
3438 if (di->lease_session == session) {
3439 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3440 h->seq = cpu_to_le32(di->lease_seq);
3441 __ceph_mdsc_drop_dentry_lease(dentry);
3443 release = 1;
3444 break;
3446 case CEPH_MDS_LEASE_RENEW:
3447 if (di->lease_session == session &&
3448 di->lease_gen == session->s_cap_gen &&
3449 di->lease_renew_from &&
3450 di->lease_renew_after == 0) {
3451 unsigned long duration =
3452 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
3454 di->lease_seq = seq;
3455 di->time = di->lease_renew_from + duration;
3456 di->lease_renew_after = di->lease_renew_from +
3457 (duration >> 1);
3458 di->lease_renew_from = 0;
3460 break;
3462 spin_unlock(&dentry->d_lock);
3463 dput(dentry);
3465 if (!release)
3466 goto out;
3468 release:
3469 /* let's just reuse the same message */
3470 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3471 ceph_msg_get(msg);
3472 ceph_con_send(&session->s_con, msg);
3474 out:
3475 iput(inode);
3476 mutex_unlock(&session->s_mutex);
3477 return;
3479 bad:
3480 pr_err("corrupt lease message\n");
3481 ceph_msg_dump(msg);
3484 void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3485 struct inode *inode,
3486 struct dentry *dentry, char action,
3487 u32 seq)
3489 struct ceph_msg *msg;
3490 struct ceph_mds_lease *lease;
3491 int len = sizeof(*lease) + sizeof(u32);
3492 int dnamelen = 0;
3494 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3495 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3496 dnamelen = dentry->d_name.len;
3497 len += dnamelen;
3499 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
3500 if (!msg)
3501 return;
3502 lease = msg->front.iov_base;
3503 lease->action = action;
3504 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3505 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3506 lease->seq = cpu_to_le32(seq);
3507 put_unaligned_le32(dnamelen, lease + 1);
3508 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3511 * if this is a preemptive lease RELEASE, no need to
3512 * flush request stream, since the actual request will
3513 * soon follow.
3515 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3517 ceph_con_send(&session->s_con, msg);
3521 * lock unlock sessions, to wait ongoing session activities
3523 static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
3525 int i;
3527 mutex_lock(&mdsc->mutex);
3528 for (i = 0; i < mdsc->max_sessions; i++) {
3529 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3530 if (!s)
3531 continue;
3532 mutex_unlock(&mdsc->mutex);
3533 mutex_lock(&s->s_mutex);
3534 mutex_unlock(&s->s_mutex);
3535 ceph_put_mds_session(s);
3536 mutex_lock(&mdsc->mutex);
3538 mutex_unlock(&mdsc->mutex);
3544 * delayed work -- periodically trim expired leases, renew caps with mds
3546 static void schedule_delayed(struct ceph_mds_client *mdsc)
3548 int delay = 5;
3549 unsigned hz = round_jiffies_relative(HZ * delay);
3550 schedule_delayed_work(&mdsc->delayed_work, hz);
3553 static void delayed_work(struct work_struct *work)
3555 int i;
3556 struct ceph_mds_client *mdsc =
3557 container_of(work, struct ceph_mds_client, delayed_work.work);
3558 int renew_interval;
3559 int renew_caps;
3561 dout("mdsc delayed_work\n");
3562 ceph_check_delayed_caps(mdsc);
3564 mutex_lock(&mdsc->mutex);
3565 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3566 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3567 mdsc->last_renew_caps);
3568 if (renew_caps)
3569 mdsc->last_renew_caps = jiffies;
3571 for (i = 0; i < mdsc->max_sessions; i++) {
3572 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3573 if (!s)
3574 continue;
3575 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3576 dout("resending session close request for mds%d\n",
3577 s->s_mds);
3578 request_close_session(mdsc, s);
3579 ceph_put_mds_session(s);
3580 continue;
3582 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3583 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3584 s->s_state = CEPH_MDS_SESSION_HUNG;
3585 pr_info("mds%d hung\n", s->s_mds);
3588 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3589 /* this mds is failed or recovering, just wait */
3590 ceph_put_mds_session(s);
3591 continue;
3593 mutex_unlock(&mdsc->mutex);
3595 mutex_lock(&s->s_mutex);
3596 if (renew_caps)
3597 send_renew_caps(mdsc, s);
3598 else
3599 ceph_con_keepalive(&s->s_con);
3600 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3601 s->s_state == CEPH_MDS_SESSION_HUNG)
3602 ceph_send_cap_releases(mdsc, s);
3603 mutex_unlock(&s->s_mutex);
3604 ceph_put_mds_session(s);
3606 mutex_lock(&mdsc->mutex);
3608 mutex_unlock(&mdsc->mutex);
3610 schedule_delayed(mdsc);
3613 int ceph_mdsc_init(struct ceph_fs_client *fsc)
3616 struct ceph_mds_client *mdsc;
3618 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3619 if (!mdsc)
3620 return -ENOMEM;
3621 mdsc->fsc = fsc;
3622 mutex_init(&mdsc->mutex);
3623 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
3624 if (!mdsc->mdsmap) {
3625 kfree(mdsc);
3626 return -ENOMEM;
3629 fsc->mdsc = mdsc;
3630 init_completion(&mdsc->safe_umount_waiters);
3631 init_waitqueue_head(&mdsc->session_close_wq);
3632 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3633 mdsc->sessions = NULL;
3634 atomic_set(&mdsc->num_sessions, 0);
3635 mdsc->max_sessions = 0;
3636 mdsc->stopping = 0;
3637 atomic64_set(&mdsc->quotarealms_count, 0);
3638 mdsc->last_snap_seq = 0;
3639 init_rwsem(&mdsc->snap_rwsem);
3640 mdsc->snap_realms = RB_ROOT;
3641 INIT_LIST_HEAD(&mdsc->snap_empty);
3642 spin_lock_init(&mdsc->snap_empty_lock);
3643 mdsc->last_tid = 0;
3644 mdsc->oldest_tid = 0;
3645 mdsc->request_tree = RB_ROOT;
3646 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3647 mdsc->last_renew_caps = jiffies;
3648 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3649 spin_lock_init(&mdsc->cap_delay_lock);
3650 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3651 spin_lock_init(&mdsc->snap_flush_lock);
3652 mdsc->last_cap_flush_tid = 1;
3653 INIT_LIST_HEAD(&mdsc->cap_flush_list);
3654 INIT_LIST_HEAD(&mdsc->cap_dirty);
3655 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
3656 mdsc->num_cap_flushing = 0;
3657 spin_lock_init(&mdsc->cap_dirty_lock);
3658 init_waitqueue_head(&mdsc->cap_flushing_wq);
3659 spin_lock_init(&mdsc->dentry_lru_lock);
3660 INIT_LIST_HEAD(&mdsc->dentry_lru);
3662 ceph_caps_init(mdsc);
3663 ceph_adjust_min_caps(mdsc, fsc->min_caps);
3665 init_rwsem(&mdsc->pool_perm_rwsem);
3666 mdsc->pool_perm_tree = RB_ROOT;
3668 strscpy(mdsc->nodename, utsname()->nodename,
3669 sizeof(mdsc->nodename));
3670 return 0;
3674 * Wait for safe replies on open mds requests. If we time out, drop
3675 * all requests from the tree to avoid dangling dentry refs.
3677 static void wait_requests(struct ceph_mds_client *mdsc)
3679 struct ceph_options *opts = mdsc->fsc->client->options;
3680 struct ceph_mds_request *req;
3682 mutex_lock(&mdsc->mutex);
3683 if (__get_oldest_req(mdsc)) {
3684 mutex_unlock(&mdsc->mutex);
3686 dout("wait_requests waiting for requests\n");
3687 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
3688 ceph_timeout_jiffies(opts->mount_timeout));
3690 /* tear down remaining requests */
3691 mutex_lock(&mdsc->mutex);
3692 while ((req = __get_oldest_req(mdsc))) {
3693 dout("wait_requests timed out on tid %llu\n",
3694 req->r_tid);
3695 __unregister_request(mdsc, req);
3698 mutex_unlock(&mdsc->mutex);
3699 dout("wait_requests done\n");
3703 * called before mount is ro, and before dentries are torn down.
3704 * (hmm, does this still race with new lookups?)
3706 void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3708 dout("pre_umount\n");
3709 mdsc->stopping = 1;
3711 lock_unlock_sessions(mdsc);
3712 ceph_flush_dirty_caps(mdsc);
3713 wait_requests(mdsc);
3716 * wait for reply handlers to drop their request refs and
3717 * their inode/dcache refs
3719 ceph_msgr_flush();
3723 * wait for all write mds requests to flush.
3725 static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3727 struct ceph_mds_request *req = NULL, *nextreq;
3728 struct rb_node *n;
3730 mutex_lock(&mdsc->mutex);
3731 dout("wait_unsafe_requests want %lld\n", want_tid);
3732 restart:
3733 req = __get_oldest_req(mdsc);
3734 while (req && req->r_tid <= want_tid) {
3735 /* find next request */
3736 n = rb_next(&req->r_node);
3737 if (n)
3738 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3739 else
3740 nextreq = NULL;
3741 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
3742 (req->r_op & CEPH_MDS_OP_WRITE)) {
3743 /* write op */
3744 ceph_mdsc_get_request(req);
3745 if (nextreq)
3746 ceph_mdsc_get_request(nextreq);
3747 mutex_unlock(&mdsc->mutex);
3748 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3749 req->r_tid, want_tid);
3750 wait_for_completion(&req->r_safe_completion);
3751 mutex_lock(&mdsc->mutex);
3752 ceph_mdsc_put_request(req);
3753 if (!nextreq)
3754 break; /* next dne before, so we're done! */
3755 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3756 /* next request was removed from tree */
3757 ceph_mdsc_put_request(nextreq);
3758 goto restart;
3760 ceph_mdsc_put_request(nextreq); /* won't go away */
3762 req = nextreq;
3764 mutex_unlock(&mdsc->mutex);
3765 dout("wait_unsafe_requests done\n");
3768 void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3770 u64 want_tid, want_flush;
3772 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
3773 return;
3775 dout("sync\n");
3776 mutex_lock(&mdsc->mutex);
3777 want_tid = mdsc->last_tid;
3778 mutex_unlock(&mdsc->mutex);
3780 ceph_flush_dirty_caps(mdsc);
3781 spin_lock(&mdsc->cap_dirty_lock);
3782 want_flush = mdsc->last_cap_flush_tid;
3783 if (!list_empty(&mdsc->cap_flush_list)) {
3784 struct ceph_cap_flush *cf =
3785 list_last_entry(&mdsc->cap_flush_list,
3786 struct ceph_cap_flush, g_list);
3787 cf->wake = true;
3789 spin_unlock(&mdsc->cap_dirty_lock);
3791 dout("sync want tid %lld flush_seq %lld\n",
3792 want_tid, want_flush);
3794 wait_unsafe_requests(mdsc, want_tid);
3795 wait_caps_flush(mdsc, want_flush);
3799 * true if all sessions are closed, or we force unmount
3801 static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
3803 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
3804 return true;
3805 return atomic_read(&mdsc->num_sessions) <= skipped;
3809 * called after sb is ro.
3811 void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3813 struct ceph_options *opts = mdsc->fsc->client->options;
3814 struct ceph_mds_session *session;
3815 int i;
3816 int skipped = 0;
3818 dout("close_sessions\n");
3820 /* close sessions */
3821 mutex_lock(&mdsc->mutex);
3822 for (i = 0; i < mdsc->max_sessions; i++) {
3823 session = __ceph_lookup_mds_session(mdsc, i);
3824 if (!session)
3825 continue;
3826 mutex_unlock(&mdsc->mutex);
3827 mutex_lock(&session->s_mutex);
3828 if (__close_session(mdsc, session) <= 0)
3829 skipped++;
3830 mutex_unlock(&session->s_mutex);
3831 ceph_put_mds_session(session);
3832 mutex_lock(&mdsc->mutex);
3834 mutex_unlock(&mdsc->mutex);
3836 dout("waiting for sessions to close\n");
3837 wait_event_timeout(mdsc->session_close_wq,
3838 done_closing_sessions(mdsc, skipped),
3839 ceph_timeout_jiffies(opts->mount_timeout));
3841 /* tear down remaining sessions */
3842 mutex_lock(&mdsc->mutex);
3843 for (i = 0; i < mdsc->max_sessions; i++) {
3844 if (mdsc->sessions[i]) {
3845 session = get_session(mdsc->sessions[i]);
3846 __unregister_session(mdsc, session);
3847 mutex_unlock(&mdsc->mutex);
3848 mutex_lock(&session->s_mutex);
3849 remove_session_caps(session);
3850 mutex_unlock(&session->s_mutex);
3851 ceph_put_mds_session(session);
3852 mutex_lock(&mdsc->mutex);
3855 WARN_ON(!list_empty(&mdsc->cap_delay_list));
3856 mutex_unlock(&mdsc->mutex);
3858 ceph_cleanup_empty_realms(mdsc);
3860 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3862 dout("stopped\n");
3865 void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
3867 struct ceph_mds_session *session;
3868 int mds;
3870 dout("force umount\n");
3872 mutex_lock(&mdsc->mutex);
3873 for (mds = 0; mds < mdsc->max_sessions; mds++) {
3874 session = __ceph_lookup_mds_session(mdsc, mds);
3875 if (!session)
3876 continue;
3877 mutex_unlock(&mdsc->mutex);
3878 mutex_lock(&session->s_mutex);
3879 __close_session(mdsc, session);
3880 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
3881 cleanup_session_requests(mdsc, session);
3882 remove_session_caps(session);
3884 mutex_unlock(&session->s_mutex);
3885 ceph_put_mds_session(session);
3886 mutex_lock(&mdsc->mutex);
3887 kick_requests(mdsc, mds);
3889 __wake_requests(mdsc, &mdsc->waiting_for_map);
3890 mutex_unlock(&mdsc->mutex);
3893 static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
3895 dout("stop\n");
3896 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3897 if (mdsc->mdsmap)
3898 ceph_mdsmap_destroy(mdsc->mdsmap);
3899 kfree(mdsc->sessions);
3900 ceph_caps_finalize(mdsc);
3901 ceph_pool_perm_destroy(mdsc);
3904 void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3906 struct ceph_mds_client *mdsc = fsc->mdsc;
3907 dout("mdsc_destroy %p\n", mdsc);
3909 if (!mdsc)
3910 return;
3912 /* flush out any connection work with references to us */
3913 ceph_msgr_flush();
3915 ceph_mdsc_stop(mdsc);
3917 fsc->mdsc = NULL;
3918 kfree(mdsc);
3919 dout("mdsc_destroy %p done\n", mdsc);
3922 void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3924 struct ceph_fs_client *fsc = mdsc->fsc;
3925 const char *mds_namespace = fsc->mount_options->mds_namespace;
3926 void *p = msg->front.iov_base;
3927 void *end = p + msg->front.iov_len;
3928 u32 epoch;
3929 u32 map_len;
3930 u32 num_fs;
3931 u32 mount_fscid = (u32)-1;
3932 u8 struct_v, struct_cv;
3933 int err = -EINVAL;
3935 ceph_decode_need(&p, end, sizeof(u32), bad);
3936 epoch = ceph_decode_32(&p);
3938 dout("handle_fsmap epoch %u\n", epoch);
3940 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
3941 struct_v = ceph_decode_8(&p);
3942 struct_cv = ceph_decode_8(&p);
3943 map_len = ceph_decode_32(&p);
3945 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
3946 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
3948 num_fs = ceph_decode_32(&p);
3949 while (num_fs-- > 0) {
3950 void *info_p, *info_end;
3951 u32 info_len;
3952 u8 info_v, info_cv;
3953 u32 fscid, namelen;
3955 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
3956 info_v = ceph_decode_8(&p);
3957 info_cv = ceph_decode_8(&p);
3958 info_len = ceph_decode_32(&p);
3959 ceph_decode_need(&p, end, info_len, bad);
3960 info_p = p;
3961 info_end = p + info_len;
3962 p = info_end;
3964 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
3965 fscid = ceph_decode_32(&info_p);
3966 namelen = ceph_decode_32(&info_p);
3967 ceph_decode_need(&info_p, info_end, namelen, bad);
3969 if (mds_namespace &&
3970 strlen(mds_namespace) == namelen &&
3971 !strncmp(mds_namespace, (char *)info_p, namelen)) {
3972 mount_fscid = fscid;
3973 break;
3977 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
3978 if (mount_fscid != (u32)-1) {
3979 fsc->client->monc.fs_cluster_id = mount_fscid;
3980 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
3981 0, true);
3982 ceph_monc_renew_subs(&fsc->client->monc);
3983 } else {
3984 err = -ENOENT;
3985 goto err_out;
3987 return;
3989 bad:
3990 pr_err("error decoding fsmap\n");
3991 err_out:
3992 mutex_lock(&mdsc->mutex);
3993 mdsc->mdsmap_err = err;
3994 __wake_requests(mdsc, &mdsc->waiting_for_map);
3995 mutex_unlock(&mdsc->mutex);
3999 * handle mds map update.
4001 void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
4003 u32 epoch;
4004 u32 maplen;
4005 void *p = msg->front.iov_base;
4006 void *end = p + msg->front.iov_len;
4007 struct ceph_mdsmap *newmap, *oldmap;
4008 struct ceph_fsid fsid;
4009 int err = -EINVAL;
4011 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
4012 ceph_decode_copy(&p, &fsid, sizeof(fsid));
4013 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
4014 return;
4015 epoch = ceph_decode_32(&p);
4016 maplen = ceph_decode_32(&p);
4017 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
4019 /* do we need it? */
4020 mutex_lock(&mdsc->mutex);
4021 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
4022 dout("handle_map epoch %u <= our %u\n",
4023 epoch, mdsc->mdsmap->m_epoch);
4024 mutex_unlock(&mdsc->mutex);
4025 return;
4028 newmap = ceph_mdsmap_decode(&p, end);
4029 if (IS_ERR(newmap)) {
4030 err = PTR_ERR(newmap);
4031 goto bad_unlock;
4034 /* swap into place */
4035 if (mdsc->mdsmap) {
4036 oldmap = mdsc->mdsmap;
4037 mdsc->mdsmap = newmap;
4038 check_new_map(mdsc, newmap, oldmap);
4039 ceph_mdsmap_destroy(oldmap);
4040 } else {
4041 mdsc->mdsmap = newmap; /* first mds map */
4043 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
4044 MAX_LFS_FILESIZE);
4046 __wake_requests(mdsc, &mdsc->waiting_for_map);
4047 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
4048 mdsc->mdsmap->m_epoch);
4050 mutex_unlock(&mdsc->mutex);
4051 schedule_delayed(mdsc);
4052 return;
4054 bad_unlock:
4055 mutex_unlock(&mdsc->mutex);
4056 bad:
4057 pr_err("error decoding mdsmap %d\n", err);
4058 return;
4061 static struct ceph_connection *con_get(struct ceph_connection *con)
4063 struct ceph_mds_session *s = con->private;
4065 if (get_session(s)) {
4066 dout("mdsc con_get %p ok (%d)\n", s, refcount_read(&s->s_ref));
4067 return con;
4069 dout("mdsc con_get %p FAIL\n", s);
4070 return NULL;
4073 static void con_put(struct ceph_connection *con)
4075 struct ceph_mds_session *s = con->private;
4077 dout("mdsc con_put %p (%d)\n", s, refcount_read(&s->s_ref) - 1);
4078 ceph_put_mds_session(s);
4082 * if the client is unresponsive for long enough, the mds will kill
4083 * the session entirely.
4085 static void peer_reset(struct ceph_connection *con)
4087 struct ceph_mds_session *s = con->private;
4088 struct ceph_mds_client *mdsc = s->s_mdsc;
4090 pr_warn("mds%d closed our session\n", s->s_mds);
4091 send_mds_reconnect(mdsc, s);
4094 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4096 struct ceph_mds_session *s = con->private;
4097 struct ceph_mds_client *mdsc = s->s_mdsc;
4098 int type = le16_to_cpu(msg->hdr.type);
4100 mutex_lock(&mdsc->mutex);
4101 if (__verify_registered_session(mdsc, s) < 0) {
4102 mutex_unlock(&mdsc->mutex);
4103 goto out;
4105 mutex_unlock(&mdsc->mutex);
4107 switch (type) {
4108 case CEPH_MSG_MDS_MAP:
4109 ceph_mdsc_handle_mdsmap(mdsc, msg);
4110 break;
4111 case CEPH_MSG_FS_MAP_USER:
4112 ceph_mdsc_handle_fsmap(mdsc, msg);
4113 break;
4114 case CEPH_MSG_CLIENT_SESSION:
4115 handle_session(s, msg);
4116 break;
4117 case CEPH_MSG_CLIENT_REPLY:
4118 handle_reply(s, msg);
4119 break;
4120 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
4121 handle_forward(mdsc, s, msg);
4122 break;
4123 case CEPH_MSG_CLIENT_CAPS:
4124 ceph_handle_caps(s, msg);
4125 break;
4126 case CEPH_MSG_CLIENT_SNAP:
4127 ceph_handle_snap(mdsc, s, msg);
4128 break;
4129 case CEPH_MSG_CLIENT_LEASE:
4130 handle_lease(mdsc, s, msg);
4131 break;
4132 case CEPH_MSG_CLIENT_QUOTA:
4133 ceph_handle_quota(mdsc, s, msg);
4134 break;
4136 default:
4137 pr_err("received unknown message type %d %s\n", type,
4138 ceph_msg_type_name(type));
4140 out:
4141 ceph_msg_put(msg);
4145 * authentication
4149 * Note: returned pointer is the address of a structure that's
4150 * managed separately. Caller must *not* attempt to free it.
4152 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
4153 int *proto, int force_new)
4155 struct ceph_mds_session *s = con->private;
4156 struct ceph_mds_client *mdsc = s->s_mdsc;
4157 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4158 struct ceph_auth_handshake *auth = &s->s_auth;
4160 if (force_new && auth->authorizer) {
4161 ceph_auth_destroy_authorizer(auth->authorizer);
4162 auth->authorizer = NULL;
4164 if (!auth->authorizer) {
4165 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4166 auth);
4167 if (ret)
4168 return ERR_PTR(ret);
4169 } else {
4170 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4171 auth);
4172 if (ret)
4173 return ERR_PTR(ret);
4175 *proto = ac->protocol;
4177 return auth;
4180 static int add_authorizer_challenge(struct ceph_connection *con,
4181 void *challenge_buf, int challenge_buf_len)
4183 struct ceph_mds_session *s = con->private;
4184 struct ceph_mds_client *mdsc = s->s_mdsc;
4185 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4187 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
4188 challenge_buf, challenge_buf_len);
4191 static int verify_authorizer_reply(struct ceph_connection *con)
4193 struct ceph_mds_session *s = con->private;
4194 struct ceph_mds_client *mdsc = s->s_mdsc;
4195 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4197 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
4200 static int invalidate_authorizer(struct ceph_connection *con)
4202 struct ceph_mds_session *s = con->private;
4203 struct ceph_mds_client *mdsc = s->s_mdsc;
4204 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4206 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
4208 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
4211 static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
4212 struct ceph_msg_header *hdr, int *skip)
4214 struct ceph_msg *msg;
4215 int type = (int) le16_to_cpu(hdr->type);
4216 int front_len = (int) le32_to_cpu(hdr->front_len);
4218 if (con->in_msg)
4219 return con->in_msg;
4221 *skip = 0;
4222 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
4223 if (!msg) {
4224 pr_err("unable to allocate msg type %d len %d\n",
4225 type, front_len);
4226 return NULL;
4229 return msg;
4232 static int mds_sign_message(struct ceph_msg *msg)
4234 struct ceph_mds_session *s = msg->con->private;
4235 struct ceph_auth_handshake *auth = &s->s_auth;
4237 return ceph_auth_sign_message(auth, msg);
4240 static int mds_check_message_signature(struct ceph_msg *msg)
4242 struct ceph_mds_session *s = msg->con->private;
4243 struct ceph_auth_handshake *auth = &s->s_auth;
4245 return ceph_auth_check_message_signature(auth, msg);
4248 static const struct ceph_connection_operations mds_con_ops = {
4249 .get = con_get,
4250 .put = con_put,
4251 .dispatch = dispatch,
4252 .get_authorizer = get_authorizer,
4253 .add_authorizer_challenge = add_authorizer_challenge,
4254 .verify_authorizer_reply = verify_authorizer_reply,
4255 .invalidate_authorizer = invalidate_authorizer,
4256 .peer_reset = peer_reset,
4257 .alloc_msg = mds_alloc_msg,
4258 .sign_message = mds_sign_message,
4259 .check_message_signature = mds_check_message_signature,
4262 /* eof */