]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - fs/ceph/mds_client.c
Merge tag 'gvt-fixes-2018-09-10' of https://github.com/intel/gvt-linux into drm-intel...
[mirror_ubuntu-eoan-kernel.git] / fs / ceph / mds_client.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
3d14c5d2 2#include <linux/ceph/ceph_debug.h>
2f2dc053 3
496e5955 4#include <linux/fs.h>
2f2dc053 5#include <linux/wait.h>
5a0e3ad6 6#include <linux/slab.h>
54008399 7#include <linux/gfp.h>
2f2dc053 8#include <linux/sched.h>
3d14c5d2
YS
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
3e0708b9 11#include <linux/ratelimit.h>
2f2dc053 12
2f2dc053 13#include "super.h"
3d14c5d2
YS
14#include "mds_client.h"
15
1fe60e51 16#include <linux/ceph/ceph_features.h>
3d14c5d2
YS
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>
2f2dc053
SW
22
23/*
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.
30 *
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.
36 *
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.
40 *
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.
46 */
47
20cb34ae 48struct ceph_reconnect_state {
44c99757 49 int nr_caps;
20cb34ae 50 struct ceph_pagelist *pagelist;
121f22a1 51 unsigned msg_version;
20cb34ae
SW
52};
53
2f2dc053
SW
54static void __wake_requests(struct ceph_mds_client *mdsc,
55 struct list_head *head);
56
9e32789f 57static const struct ceph_connection_operations mds_con_ops;
2f2dc053
SW
58
59
60/*
61 * mds reply parsing
62 */
63
64/*
65 * parse individual inode info
66 */
67static int parse_reply_info_in(void **p, void *end,
14303d20 68 struct ceph_mds_reply_info_in *info,
12b4629a 69 u64 features)
2f2dc053
SW
70{
71 int err = -EIO;
72
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);
77
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;
82
14303d20
SW
83 if (features & CEPH_FEATURE_DIRLAYOUTHASH)
84 ceph_decode_copy_safe(p, end, &info->dir_layout,
85 sizeof(info->dir_layout), bad);
86 else
87 memset(&info->dir_layout, 0, sizeof(info->dir_layout));
88
2f2dc053
SW
89 ceph_decode_32_safe(p, end, info->xattr_len, bad);
90 ceph_decode_need(p, end, info->xattr_len, bad);
91 info->xattr_data = *p;
92 *p += info->xattr_len;
fb01d1f8
YZ
93
94 if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
95 ceph_decode_64_safe(p, end, info->inline_version, bad);
96 ceph_decode_32_safe(p, end, info->inline_len, bad);
97 ceph_decode_need(p, end, info->inline_len, bad);
98 info->inline_data = *p;
99 *p += info->inline_len;
100 } else
101 info->inline_version = CEPH_INLINE_NONE;
102
fb18a575
LH
103 if (features & CEPH_FEATURE_MDS_QUOTA) {
104 u8 struct_v, struct_compat;
105 u32 struct_len;
106
107 /*
108 * both struct_v and struct_compat are expected to be >= 1
109 */
110 ceph_decode_8_safe(p, end, struct_v, bad);
111 ceph_decode_8_safe(p, end, struct_compat, bad);
112 if (!struct_v || !struct_compat)
113 goto bad;
114 ceph_decode_32_safe(p, end, struct_len, bad);
115 ceph_decode_need(p, end, struct_len, bad);
116 ceph_decode_64_safe(p, end, info->max_bytes, bad);
117 ceph_decode_64_safe(p, end, info->max_files, bad);
118 } else {
119 info->max_bytes = 0;
120 info->max_files = 0;
121 }
122
779fe0fb
YZ
123 info->pool_ns_len = 0;
124 info->pool_ns_data = NULL;
5ea5c5e0
YZ
125 if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) {
126 ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
779fe0fb
YZ
127 if (info->pool_ns_len > 0) {
128 ceph_decode_need(p, end, info->pool_ns_len, bad);
129 info->pool_ns_data = *p;
130 *p += info->pool_ns_len;
131 }
5ea5c5e0
YZ
132 }
133
2f2dc053
SW
134 return 0;
135bad:
136 return err;
137}
138
139/*
140 * parse a normal reply, which may contain a (dir+)dentry and/or a
141 * target inode.
142 */
143static int parse_reply_info_trace(void **p, void *end,
14303d20 144 struct ceph_mds_reply_info_parsed *info,
12b4629a 145 u64 features)
2f2dc053
SW
146{
147 int err;
148
149 if (info->head->is_dentry) {
14303d20 150 err = parse_reply_info_in(p, end, &info->diri, features);
2f2dc053
SW
151 if (err < 0)
152 goto out_bad;
153
154 if (unlikely(*p + sizeof(*info->dirfrag) > end))
155 goto bad;
156 info->dirfrag = *p;
157 *p += sizeof(*info->dirfrag) +
158 sizeof(u32)*le32_to_cpu(info->dirfrag->ndist);
159 if (unlikely(*p > end))
160 goto bad;
161
162 ceph_decode_32_safe(p, end, info->dname_len, bad);
163 ceph_decode_need(p, end, info->dname_len, bad);
164 info->dname = *p;
165 *p += info->dname_len;
166 info->dlease = *p;
167 *p += sizeof(*info->dlease);
168 }
169
170 if (info->head->is_target) {
14303d20 171 err = parse_reply_info_in(p, end, &info->targeti, features);
2f2dc053
SW
172 if (err < 0)
173 goto out_bad;
174 }
175
176 if (unlikely(*p != end))
177 goto bad;
178 return 0;
179
180bad:
181 err = -EIO;
182out_bad:
183 pr_err("problem parsing mds trace %d\n", err);
184 return err;
185}
186
187/*
188 * parse readdir results
189 */
190static int parse_reply_info_dir(void **p, void *end,
14303d20 191 struct ceph_mds_reply_info_parsed *info,
12b4629a 192 u64 features)
2f2dc053
SW
193{
194 u32 num, i = 0;
195 int err;
196
197 info->dir_dir = *p;
198 if (*p + sizeof(*info->dir_dir) > end)
199 goto bad;
200 *p += sizeof(*info->dir_dir) +
201 sizeof(u32)*le32_to_cpu(info->dir_dir->ndist);
202 if (*p > end)
203 goto bad;
204
205 ceph_decode_need(p, end, sizeof(num) + 2, bad);
c89136ea 206 num = ceph_decode_32(p);
956d39d6
YZ
207 {
208 u16 flags = ceph_decode_16(p);
209 info->dir_end = !!(flags & CEPH_READDIR_FRAG_END);
210 info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE);
f3c4ebe6 211 info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER);
79162547 212 info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH);
956d39d6 213 }
2f2dc053
SW
214 if (num == 0)
215 goto done;
216
2a5beea3
YZ
217 BUG_ON(!info->dir_entries);
218 if ((unsigned long)(info->dir_entries + num) >
219 (unsigned long)info->dir_entries + info->dir_buf_size) {
54008399
YZ
220 pr_err("dir contents are larger than expected\n");
221 WARN_ON(1);
222 goto bad;
223 }
2f2dc053 224
54008399 225 info->dir_nr = num;
2f2dc053 226 while (num) {
2a5beea3 227 struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i;
2f2dc053
SW
228 /* dentry */
229 ceph_decode_need(p, end, sizeof(u32)*2, bad);
2a5beea3
YZ
230 rde->name_len = ceph_decode_32(p);
231 ceph_decode_need(p, end, rde->name_len, bad);
232 rde->name = *p;
233 *p += rde->name_len;
234 dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name);
235 rde->lease = *p;
2f2dc053
SW
236 *p += sizeof(struct ceph_mds_reply_lease);
237
238 /* inode */
2a5beea3 239 err = parse_reply_info_in(p, end, &rde->inode, features);
2f2dc053
SW
240 if (err < 0)
241 goto out_bad;
8974eebd
YZ
242 /* ceph_readdir_prepopulate() will update it */
243 rde->offset = 0;
2f2dc053
SW
244 i++;
245 num--;
246 }
247
248done:
249 if (*p != end)
250 goto bad;
251 return 0;
252
253bad:
254 err = -EIO;
255out_bad:
256 pr_err("problem parsing dir contents %d\n", err);
257 return err;
258}
259
25933abd
HS
260/*
261 * parse fcntl F_GETLK results
262 */
263static int parse_reply_info_filelock(void **p, void *end,
14303d20 264 struct ceph_mds_reply_info_parsed *info,
12b4629a 265 u64 features)
25933abd
HS
266{
267 if (*p + sizeof(*info->filelock_reply) > end)
268 goto bad;
269
270 info->filelock_reply = *p;
271 *p += sizeof(*info->filelock_reply);
272
273 if (unlikely(*p != end))
274 goto bad;
275 return 0;
276
277bad:
278 return -EIO;
279}
280
6e8575fa
SL
281/*
282 * parse create results
283 */
284static int parse_reply_info_create(void **p, void *end,
285 struct ceph_mds_reply_info_parsed *info,
12b4629a 286 u64 features)
6e8575fa
SL
287{
288 if (features & CEPH_FEATURE_REPLY_CREATE_INODE) {
289 if (*p == end) {
290 info->has_create_ino = false;
291 } else {
292 info->has_create_ino = true;
293 info->ino = ceph_decode_64(p);
294 }
295 }
296
297 if (unlikely(*p != end))
298 goto bad;
299 return 0;
300
301bad:
302 return -EIO;
303}
304
25933abd
HS
305/*
306 * parse extra results
307 */
308static int parse_reply_info_extra(void **p, void *end,
14303d20 309 struct ceph_mds_reply_info_parsed *info,
12b4629a 310 u64 features)
25933abd 311{
6df8c9d8
JL
312 u32 op = le32_to_cpu(info->head->op);
313
314 if (op == CEPH_MDS_OP_GETFILELOCK)
14303d20 315 return parse_reply_info_filelock(p, end, info, features);
6df8c9d8 316 else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
14303d20 317 return parse_reply_info_dir(p, end, info, features);
6df8c9d8 318 else if (op == CEPH_MDS_OP_CREATE)
6e8575fa
SL
319 return parse_reply_info_create(p, end, info, features);
320 else
321 return -EIO;
25933abd
HS
322}
323
2f2dc053
SW
324/*
325 * parse entire mds reply
326 */
327static int parse_reply_info(struct ceph_msg *msg,
14303d20 328 struct ceph_mds_reply_info_parsed *info,
12b4629a 329 u64 features)
2f2dc053
SW
330{
331 void *p, *end;
332 u32 len;
333 int err;
334
335 info->head = msg->front.iov_base;
336 p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
337 end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
338
339 /* trace */
340 ceph_decode_32_safe(&p, end, len, bad);
341 if (len > 0) {
32852a81 342 ceph_decode_need(&p, end, len, bad);
14303d20 343 err = parse_reply_info_trace(&p, p+len, info, features);
2f2dc053
SW
344 if (err < 0)
345 goto out_bad;
346 }
347
25933abd 348 /* extra */
2f2dc053
SW
349 ceph_decode_32_safe(&p, end, len, bad);
350 if (len > 0) {
32852a81 351 ceph_decode_need(&p, end, len, bad);
14303d20 352 err = parse_reply_info_extra(&p, p+len, info, features);
2f2dc053
SW
353 if (err < 0)
354 goto out_bad;
355 }
356
357 /* snap blob */
358 ceph_decode_32_safe(&p, end, len, bad);
359 info->snapblob_len = len;
360 info->snapblob = p;
361 p += len;
362
363 if (p != end)
364 goto bad;
365 return 0;
366
367bad:
368 err = -EIO;
369out_bad:
370 pr_err("mds parse_reply err %d\n", err);
371 return err;
372}
373
374static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
375{
2a5beea3 376 if (!info->dir_entries)
54008399 377 return;
2a5beea3 378 free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
2f2dc053
SW
379}
380
381
382/*
383 * sessions
384 */
a687ecaf 385const char *ceph_session_state_name(int s)
2f2dc053
SW
386{
387 switch (s) {
388 case CEPH_MDS_SESSION_NEW: return "new";
389 case CEPH_MDS_SESSION_OPENING: return "opening";
390 case CEPH_MDS_SESSION_OPEN: return "open";
391 case CEPH_MDS_SESSION_HUNG: return "hung";
392 case CEPH_MDS_SESSION_CLOSING: return "closing";
44ca18f2 393 case CEPH_MDS_SESSION_RESTARTING: return "restarting";
2f2dc053 394 case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
fcff415c 395 case CEPH_MDS_SESSION_REJECTED: return "rejected";
2f2dc053
SW
396 default: return "???";
397 }
398}
399
400static struct ceph_mds_session *get_session(struct ceph_mds_session *s)
401{
3997c01d 402 if (refcount_inc_not_zero(&s->s_ref)) {
2f2dc053 403 dout("mdsc get_session %p %d -> %d\n", s,
3997c01d 404 refcount_read(&s->s_ref)-1, refcount_read(&s->s_ref));
2f2dc053
SW
405 return s;
406 } else {
4c069a58 407 dout("mdsc get_session %p 0 -- FAIL\n", s);
2f2dc053
SW
408 return NULL;
409 }
410}
411
412void ceph_put_mds_session(struct ceph_mds_session *s)
413{
414 dout("mdsc put_session %p %d -> %d\n", s,
3997c01d
ER
415 refcount_read(&s->s_ref), refcount_read(&s->s_ref)-1);
416 if (refcount_dec_and_test(&s->s_ref)) {
6c4a1915 417 if (s->s_auth.authorizer)
6c1ea260 418 ceph_auth_destroy_authorizer(s->s_auth.authorizer);
2f2dc053 419 kfree(s);
4e7a5dcd 420 }
2f2dc053
SW
421}
422
423/*
424 * called under mdsc->mutex
425 */
426struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
427 int mds)
428{
429 struct ceph_mds_session *session;
430
d37b1d99 431 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
2f2dc053
SW
432 return NULL;
433 session = mdsc->sessions[mds];
434 dout("lookup_mds_session %p %d\n", session,
3997c01d 435 refcount_read(&session->s_ref));
2f2dc053
SW
436 get_session(session);
437 return session;
438}
439
440static bool __have_session(struct ceph_mds_client *mdsc, int mds)
441{
98cfda81 442 if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
2f2dc053 443 return false;
98cfda81
CX
444 else
445 return true;
2f2dc053
SW
446}
447
2600d2dd
SW
448static int __verify_registered_session(struct ceph_mds_client *mdsc,
449 struct ceph_mds_session *s)
450{
451 if (s->s_mds >= mdsc->max_sessions ||
452 mdsc->sessions[s->s_mds] != s)
453 return -ENOENT;
454 return 0;
455}
456
2f2dc053
SW
457/*
458 * create+register a new session for given mds.
459 * called under mdsc->mutex.
460 */
461static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
462 int mds)
463{
464 struct ceph_mds_session *s;
465
76201b63 466 if (mds >= mdsc->mdsmap->m_num_mds)
c338c07c
NY
467 return ERR_PTR(-EINVAL);
468
2f2dc053 469 s = kzalloc(sizeof(*s), GFP_NOFS);
4736b009
DC
470 if (!s)
471 return ERR_PTR(-ENOMEM);
47474d0b
CX
472
473 if (mds >= mdsc->max_sessions) {
474 int newmax = 1 << get_count_order(mds + 1);
475 struct ceph_mds_session **sa;
476
477 dout("%s: realloc to %d\n", __func__, newmax);
478 sa = kcalloc(newmax, sizeof(void *), GFP_NOFS);
479 if (!sa)
480 goto fail_realloc;
481 if (mdsc->sessions) {
482 memcpy(sa, mdsc->sessions,
483 mdsc->max_sessions * sizeof(void *));
484 kfree(mdsc->sessions);
485 }
486 mdsc->sessions = sa;
487 mdsc->max_sessions = newmax;
488 }
489
490 dout("%s: mds%d\n", __func__, mds);
2f2dc053
SW
491 s->s_mdsc = mdsc;
492 s->s_mds = mds;
493 s->s_state = CEPH_MDS_SESSION_NEW;
494 s->s_ttl = 0;
495 s->s_seq = 0;
496 mutex_init(&s->s_mutex);
497
b7a9e5dd 498 ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
2f2dc053 499
d8fb02ab 500 spin_lock_init(&s->s_gen_ttl_lock);
2f2dc053 501 s->s_cap_gen = 0;
1ce208a6 502 s->s_cap_ttl = jiffies - 1;
d8fb02ab
AE
503
504 spin_lock_init(&s->s_cap_lock);
2f2dc053
SW
505 s->s_renew_requested = 0;
506 s->s_renew_seq = 0;
507 INIT_LIST_HEAD(&s->s_caps);
508 s->s_nr_caps = 0;
5dacf091 509 s->s_trim_caps = 0;
3997c01d 510 refcount_set(&s->s_ref, 1);
2f2dc053
SW
511 INIT_LIST_HEAD(&s->s_waiting);
512 INIT_LIST_HEAD(&s->s_unsafe);
513 s->s_num_cap_releases = 0;
99a9c273 514 s->s_cap_reconnect = 0;
7c1332b8 515 s->s_cap_iterator = NULL;
2f2dc053 516 INIT_LIST_HEAD(&s->s_cap_releases);
2f2dc053 517 INIT_LIST_HEAD(&s->s_cap_flushing);
2f2dc053 518
2f2dc053 519 mdsc->sessions[mds] = s;
86d8f67b 520 atomic_inc(&mdsc->num_sessions);
3997c01d 521 refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
42ce56e5 522
b7a9e5dd
SW
523 ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
524 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
42ce56e5 525
2f2dc053 526 return s;
42ce56e5
SW
527
528fail_realloc:
529 kfree(s);
530 return ERR_PTR(-ENOMEM);
2f2dc053
SW
531}
532
533/*
534 * called under mdsc->mutex
535 */
2600d2dd 536static void __unregister_session(struct ceph_mds_client *mdsc,
42ce56e5 537 struct ceph_mds_session *s)
2f2dc053 538{
2600d2dd
SW
539 dout("__unregister_session mds%d %p\n", s->s_mds, s);
540 BUG_ON(mdsc->sessions[s->s_mds] != s);
42ce56e5
SW
541 mdsc->sessions[s->s_mds] = NULL;
542 ceph_con_close(&s->s_con);
543 ceph_put_mds_session(s);
86d8f67b 544 atomic_dec(&mdsc->num_sessions);
2f2dc053
SW
545}
546
547/*
548 * drop session refs in request.
549 *
550 * should be last request ref, or hold mdsc->mutex
551 */
552static void put_request_session(struct ceph_mds_request *req)
553{
554 if (req->r_session) {
555 ceph_put_mds_session(req->r_session);
556 req->r_session = NULL;
557 }
558}
559
153c8e6b 560void ceph_mdsc_release_request(struct kref *kref)
2f2dc053 561{
153c8e6b
SW
562 struct ceph_mds_request *req = container_of(kref,
563 struct ceph_mds_request,
564 r_kref);
54008399 565 destroy_reply_info(&req->r_reply_info);
153c8e6b
SW
566 if (req->r_request)
567 ceph_msg_put(req->r_request);
54008399 568 if (req->r_reply)
153c8e6b 569 ceph_msg_put(req->r_reply);
153c8e6b 570 if (req->r_inode) {
41b02e1f 571 ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
153c8e6b
SW
572 iput(req->r_inode);
573 }
3dd69aab
JL
574 if (req->r_parent)
575 ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
e96a650a 576 iput(req->r_target_inode);
153c8e6b
SW
577 if (req->r_dentry)
578 dput(req->r_dentry);
844d87c3
SW
579 if (req->r_old_dentry)
580 dput(req->r_old_dentry);
581 if (req->r_old_dentry_dir) {
41b02e1f
SW
582 /*
583 * track (and drop pins for) r_old_dentry_dir
584 * separately, since r_old_dentry's d_parent may have
585 * changed between the dir mutex being dropped and
586 * this request being freed.
587 */
588 ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
589 CEPH_CAP_PIN);
41b02e1f 590 iput(req->r_old_dentry_dir);
2f2dc053 591 }
153c8e6b
SW
592 kfree(req->r_path1);
593 kfree(req->r_path2);
25e6bae3
YZ
594 if (req->r_pagelist)
595 ceph_pagelist_release(req->r_pagelist);
153c8e6b 596 put_request_session(req);
37151668 597 ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
153c8e6b 598 kfree(req);
2f2dc053
SW
599}
600
fcd00b68
ID
601DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
602
2f2dc053
SW
603/*
604 * lookup session, bump ref if found.
605 *
606 * called under mdsc->mutex.
607 */
fcd00b68
ID
608static struct ceph_mds_request *
609lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
2f2dc053
SW
610{
611 struct ceph_mds_request *req;
44ca18f2 612
fcd00b68
ID
613 req = lookup_request(&mdsc->request_tree, tid);
614 if (req)
615 ceph_mdsc_get_request(req);
44ca18f2 616
fcd00b68 617 return req;
2f2dc053
SW
618}
619
620/*
621 * Register an in-flight request, and assign a tid. Link to directory
622 * are modifying (if any).
623 *
624 * Called under mdsc->mutex.
625 */
626static void __register_request(struct ceph_mds_client *mdsc,
627 struct ceph_mds_request *req,
628 struct inode *dir)
629{
e30ee581
ZZ
630 int ret = 0;
631
2f2dc053 632 req->r_tid = ++mdsc->last_tid;
e30ee581
ZZ
633 if (req->r_num_caps) {
634 ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
635 req->r_num_caps);
636 if (ret < 0) {
637 pr_err("__register_request %p "
638 "failed to reserve caps: %d\n", req, ret);
639 /* set req->r_err to fail early from __do_request */
640 req->r_err = ret;
641 return;
642 }
643 }
2f2dc053
SW
644 dout("__register_request %p tid %lld\n", req, req->r_tid);
645 ceph_mdsc_get_request(req);
fcd00b68 646 insert_request(&mdsc->request_tree, req);
2f2dc053 647
cb4276cc
SW
648 req->r_uid = current_fsuid();
649 req->r_gid = current_fsgid();
650
e8a7b8b1
YZ
651 if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
652 mdsc->oldest_tid = req->r_tid;
653
2f2dc053 654 if (dir) {
3b663780 655 ihold(dir);
2f2dc053 656 req->r_unsafe_dir = dir;
2f2dc053
SW
657 }
658}
659
660static void __unregister_request(struct ceph_mds_client *mdsc,
661 struct ceph_mds_request *req)
662{
663 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
e8a7b8b1 664
df963ea8
JL
665 /* Never leave an unregistered request on an unsafe list! */
666 list_del_init(&req->r_unsafe_item);
667
e8a7b8b1
YZ
668 if (req->r_tid == mdsc->oldest_tid) {
669 struct rb_node *p = rb_next(&req->r_node);
670 mdsc->oldest_tid = 0;
671 while (p) {
672 struct ceph_mds_request *next_req =
673 rb_entry(p, struct ceph_mds_request, r_node);
674 if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
675 mdsc->oldest_tid = next_req->r_tid;
676 break;
677 }
678 p = rb_next(p);
679 }
680 }
681
fcd00b68 682 erase_request(&mdsc->request_tree, req);
2f2dc053 683
bc2de10d
JL
684 if (req->r_unsafe_dir &&
685 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2f2dc053 686 struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
2f2dc053
SW
687 spin_lock(&ci->i_unsafe_lock);
688 list_del_init(&req->r_unsafe_dir_item);
689 spin_unlock(&ci->i_unsafe_lock);
4c06ace8 690 }
bc2de10d
JL
691 if (req->r_target_inode &&
692 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
68cd5b4b
YZ
693 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
694 spin_lock(&ci->i_unsafe_lock);
695 list_del_init(&req->r_unsafe_target_item);
696 spin_unlock(&ci->i_unsafe_lock);
697 }
3b663780 698
4c06ace8 699 if (req->r_unsafe_dir) {
3b663780
SW
700 iput(req->r_unsafe_dir);
701 req->r_unsafe_dir = NULL;
2f2dc053 702 }
94aa8ae1 703
fc55d2c9
YZ
704 complete_all(&req->r_safe_completion);
705
94aa8ae1 706 ceph_mdsc_put_request(req);
2f2dc053
SW
707}
708
30c71233
JL
709/*
710 * Walk back up the dentry tree until we hit a dentry representing a
711 * non-snapshot inode. We do this using the rcu_read_lock (which must be held
712 * when calling this) to ensure that the objects won't disappear while we're
713 * working with them. Once we hit a candidate dentry, we attempt to take a
714 * reference to it, and return that as the result.
715 */
f1075480
DC
716static struct inode *get_nonsnap_parent(struct dentry *dentry)
717{
718 struct inode *inode = NULL;
30c71233
JL
719
720 while (dentry && !IS_ROOT(dentry)) {
721 inode = d_inode_rcu(dentry);
722 if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
723 break;
724 dentry = dentry->d_parent;
725 }
726 if (inode)
727 inode = igrab(inode);
728 return inode;
729}
730
2f2dc053
SW
731/*
732 * Choose mds to send request to next. If there is a hint set in the
733 * request (e.g., due to a prior forward hint from the mds), use that.
734 * Otherwise, consult frag tree and/or caps to identify the
735 * appropriate mds. If all else fails, choose randomly.
736 *
737 * Called under mdsc->mutex.
738 */
739static int __choose_mds(struct ceph_mds_client *mdsc,
740 struct ceph_mds_request *req)
741{
742 struct inode *inode;
743 struct ceph_inode_info *ci;
744 struct ceph_cap *cap;
745 int mode = req->r_direct_mode;
746 int mds = -1;
747 u32 hash = req->r_direct_hash;
bc2de10d 748 bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
2f2dc053
SW
749
750 /*
751 * is there a specific mds we should try? ignore hint if we have
752 * no session and the mds is not up (active or recovering).
753 */
754 if (req->r_resend_mds >= 0 &&
755 (__have_session(mdsc, req->r_resend_mds) ||
756 ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
757 dout("choose_mds using resend_mds mds%d\n",
758 req->r_resend_mds);
759 return req->r_resend_mds;
760 }
761
762 if (mode == USE_RANDOM_MDS)
763 goto random;
764
765 inode = NULL;
766 if (req->r_inode) {
5d37ca14
YZ
767 if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
768 inode = req->r_inode;
769 ihold(inode);
770 } else {
38f340cc
YZ
771 /* req->r_dentry is non-null for LSSNAP request */
772 rcu_read_lock();
773 inode = get_nonsnap_parent(req->r_dentry);
774 rcu_read_unlock();
775 dout("__choose_mds using snapdir's parent %p\n", inode);
5d37ca14 776 }
38f340cc 777 } else if (req->r_dentry) {
d79698da 778 /* ignore race with rename; old or new d_parent is okay */
30c71233
JL
779 struct dentry *parent;
780 struct inode *dir;
781
782 rcu_read_lock();
783 parent = req->r_dentry->d_parent;
3dd69aab 784 dir = req->r_parent ? : d_inode_rcu(parent);
eb6bb1c5 785
30c71233
JL
786 if (!dir || dir->i_sb != mdsc->fsc->sb) {
787 /* not this fs or parent went negative */
2b0143b5 788 inode = d_inode(req->r_dentry);
30c71233
JL
789 if (inode)
790 ihold(inode);
eb6bb1c5
SW
791 } else if (ceph_snap(dir) != CEPH_NOSNAP) {
792 /* direct snapped/virtual snapdir requests
793 * based on parent dir inode */
30c71233 794 inode = get_nonsnap_parent(parent);
eb6bb1c5 795 dout("__choose_mds using nonsnap parent %p\n", inode);
ca18bede 796 } else {
eb6bb1c5 797 /* dentry target */
2b0143b5 798 inode = d_inode(req->r_dentry);
ca18bede
YZ
799 if (!inode || mode == USE_AUTH_MDS) {
800 /* dir + name */
30c71233 801 inode = igrab(dir);
ca18bede
YZ
802 hash = ceph_dentry_hash(dir, req->r_dentry);
803 is_hash = true;
30c71233
JL
804 } else {
805 ihold(inode);
ca18bede 806 }
2f2dc053 807 }
30c71233 808 rcu_read_unlock();
2f2dc053 809 }
eb6bb1c5 810
2f2dc053
SW
811 dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash,
812 (int)hash, mode);
813 if (!inode)
814 goto random;
815 ci = ceph_inode(inode);
816
817 if (is_hash && S_ISDIR(inode->i_mode)) {
818 struct ceph_inode_frag frag;
819 int found;
820
821 ceph_choose_frag(ci, hash, &frag, &found);
822 if (found) {
823 if (mode == USE_ANY_MDS && frag.ndist > 0) {
824 u8 r;
825
826 /* choose a random replica */
827 get_random_bytes(&r, 1);
828 r %= frag.ndist;
829 mds = frag.dist[r];
830 dout("choose_mds %p %llx.%llx "
831 "frag %u mds%d (%d/%d)\n",
832 inode, ceph_vinop(inode),
d66bbd44 833 frag.frag, mds,
2f2dc053 834 (int)r, frag.ndist);
d66bbd44
SW
835 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
836 CEPH_MDS_STATE_ACTIVE)
30c71233 837 goto out;
2f2dc053
SW
838 }
839
840 /* since this file/dir wasn't known to be
841 * replicated, then we want to look for the
842 * authoritative mds. */
843 mode = USE_AUTH_MDS;
844 if (frag.mds >= 0) {
845 /* choose auth mds */
846 mds = frag.mds;
847 dout("choose_mds %p %llx.%llx "
848 "frag %u mds%d (auth)\n",
849 inode, ceph_vinop(inode), frag.frag, mds);
d66bbd44
SW
850 if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
851 CEPH_MDS_STATE_ACTIVE)
30c71233 852 goto out;
2f2dc053
SW
853 }
854 }
855 }
856
be655596 857 spin_lock(&ci->i_ceph_lock);
2f2dc053
SW
858 cap = NULL;
859 if (mode == USE_AUTH_MDS)
860 cap = ci->i_auth_cap;
861 if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
862 cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
863 if (!cap) {
be655596 864 spin_unlock(&ci->i_ceph_lock);
30c71233 865 iput(inode);
2f2dc053
SW
866 goto random;
867 }
868 mds = cap->session->s_mds;
869 dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n",
870 inode, ceph_vinop(inode), mds,
871 cap == ci->i_auth_cap ? "auth " : "", cap);
be655596 872 spin_unlock(&ci->i_ceph_lock);
30c71233
JL
873out:
874 iput(inode);
2f2dc053
SW
875 return mds;
876
877random:
878 mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
879 dout("choose_mds chose random mds%d\n", mds);
880 return mds;
881}
882
883
884/*
885 * session messages
886 */
887static struct ceph_msg *create_session_msg(u32 op, u64 seq)
888{
889 struct ceph_msg *msg;
890 struct ceph_mds_session_head *h;
891
b61c2763
SW
892 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
893 false);
a79832f2 894 if (!msg) {
2f2dc053 895 pr_err("create_session_msg ENOMEM creating msg\n");
a79832f2 896 return NULL;
2f2dc053
SW
897 }
898 h = msg->front.iov_base;
899 h->op = cpu_to_le32(op);
900 h->seq = cpu_to_le64(seq);
dbd0c8bf
JS
901
902 return msg;
903}
904
342ce182
YZ
905static void encode_supported_features(void **p, void *end)
906{
907 static const unsigned char bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
908 static const size_t count = ARRAY_SIZE(bits);
909
910 if (count > 0) {
911 size_t i;
912 size_t size = ((size_t)bits[count - 1] + 64) / 64 * 8;
913
914 BUG_ON(*p + 4 + size > end);
915 ceph_encode_32(p, size);
916 memset(*p, 0, size);
917 for (i = 0; i < count; i++)
918 ((unsigned char*)(*p))[i / 8] |= 1 << (bits[i] % 8);
919 *p += size;
920 } else {
921 BUG_ON(*p + 4 > end);
922 ceph_encode_32(p, 0);
923 }
924}
925
dbd0c8bf
JS
926/*
927 * session message, specialization for CEPH_SESSION_REQUEST_OPEN
928 * to include additional client metadata fields.
929 */
930static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq)
931{
932 struct ceph_msg *msg;
933 struct ceph_mds_session_head *h;
934 int i = -1;
342ce182 935 int extra_bytes = 0;
dbd0c8bf
JS
936 int metadata_key_count = 0;
937 struct ceph_options *opt = mdsc->fsc->client->options;
3f384954 938 struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
342ce182 939 void *p, *end;
dbd0c8bf 940
a6a5ce4f 941 const char* metadata[][2] = {
717e6f28
YZ
942 {"hostname", mdsc->nodename},
943 {"kernel_version", init_utsname()->release},
3f384954
YZ
944 {"entity_id", opt->name ? : ""},
945 {"root", fsopt->server_path ? : "/"},
dbd0c8bf
JS
946 {NULL, NULL}
947 };
948
949 /* Calculate serialized length of metadata */
342ce182 950 extra_bytes = 4; /* map length */
d37b1d99 951 for (i = 0; metadata[i][0]; ++i) {
342ce182 952 extra_bytes += 8 + strlen(metadata[i][0]) +
dbd0c8bf
JS
953 strlen(metadata[i][1]);
954 metadata_key_count++;
955 }
342ce182
YZ
956 /* supported feature */
957 extra_bytes += 4 + 8;
dbd0c8bf
JS
958
959 /* Allocate the message */
342ce182 960 msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
dbd0c8bf
JS
961 GFP_NOFS, false);
962 if (!msg) {
963 pr_err("create_session_msg ENOMEM creating msg\n");
964 return NULL;
965 }
342ce182
YZ
966 p = msg->front.iov_base;
967 end = p + msg->front.iov_len;
968
969 h = p;
dbd0c8bf
JS
970 h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN);
971 h->seq = cpu_to_le64(seq);
972
973 /*
974 * Serialize client metadata into waiting buffer space, using
975 * the format that userspace expects for map<string, string>
7cfa0313
JS
976 *
977 * ClientSession messages with metadata are v2
dbd0c8bf 978 */
342ce182 979 msg->hdr.version = cpu_to_le16(3);
7cfa0313 980 msg->hdr.compat_version = cpu_to_le16(1);
dbd0c8bf
JS
981
982 /* The write pointer, following the session_head structure */
342ce182 983 p += sizeof(*h);
dbd0c8bf
JS
984
985 /* Number of entries in the map */
986 ceph_encode_32(&p, metadata_key_count);
987
988 /* Two length-prefixed strings for each entry in the map */
d37b1d99 989 for (i = 0; metadata[i][0]; ++i) {
dbd0c8bf
JS
990 size_t const key_len = strlen(metadata[i][0]);
991 size_t const val_len = strlen(metadata[i][1]);
992
993 ceph_encode_32(&p, key_len);
994 memcpy(p, metadata[i][0], key_len);
995 p += key_len;
996 ceph_encode_32(&p, val_len);
997 memcpy(p, metadata[i][1], val_len);
998 p += val_len;
999 }
1000
342ce182
YZ
1001 encode_supported_features(&p, end);
1002 msg->front.iov_len = p - msg->front.iov_base;
1003 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1004
2f2dc053
SW
1005 return msg;
1006}
1007
1008/*
1009 * send session open request.
1010 *
1011 * called under mdsc->mutex
1012 */
1013static int __open_session(struct ceph_mds_client *mdsc,
1014 struct ceph_mds_session *session)
1015{
1016 struct ceph_msg *msg;
1017 int mstate;
1018 int mds = session->s_mds;
2f2dc053
SW
1019
1020 /* wait for mds to go active? */
1021 mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
1022 dout("open_session to mds%d (%s)\n", mds,
1023 ceph_mds_state_name(mstate));
1024 session->s_state = CEPH_MDS_SESSION_OPENING;
1025 session->s_renew_requested = jiffies;
1026
1027 /* send connect message */
dbd0c8bf 1028 msg = create_session_open_msg(mdsc, session->s_seq);
a79832f2
SW
1029 if (!msg)
1030 return -ENOMEM;
2f2dc053 1031 ceph_con_send(&session->s_con, msg);
2f2dc053
SW
1032 return 0;
1033}
1034
ed0552a1
SW
1035/*
1036 * open sessions for any export targets for the given mds
1037 *
1038 * called under mdsc->mutex
1039 */
5d72d13c
YZ
1040static struct ceph_mds_session *
1041__open_export_target_session(struct ceph_mds_client *mdsc, int target)
1042{
1043 struct ceph_mds_session *session;
1044
1045 session = __ceph_lookup_mds_session(mdsc, target);
1046 if (!session) {
1047 session = register_session(mdsc, target);
1048 if (IS_ERR(session))
1049 return session;
1050 }
1051 if (session->s_state == CEPH_MDS_SESSION_NEW ||
1052 session->s_state == CEPH_MDS_SESSION_CLOSING)
1053 __open_session(mdsc, session);
1054
1055 return session;
1056}
1057
1058struct ceph_mds_session *
1059ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
1060{
1061 struct ceph_mds_session *session;
1062
1063 dout("open_export_target_session to mds%d\n", target);
1064
1065 mutex_lock(&mdsc->mutex);
1066 session = __open_export_target_session(mdsc, target);
1067 mutex_unlock(&mdsc->mutex);
1068
1069 return session;
1070}
1071
ed0552a1
SW
1072static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
1073 struct ceph_mds_session *session)
1074{
1075 struct ceph_mds_info *mi;
1076 struct ceph_mds_session *ts;
1077 int i, mds = session->s_mds;
ed0552a1 1078
76201b63 1079 if (mds >= mdsc->mdsmap->m_num_mds)
ed0552a1 1080 return;
5d72d13c 1081
ed0552a1
SW
1082 mi = &mdsc->mdsmap->m_info[mds];
1083 dout("open_export_target_sessions for mds%d (%d targets)\n",
1084 session->s_mds, mi->num_export_targets);
1085
1086 for (i = 0; i < mi->num_export_targets; i++) {
5d72d13c
YZ
1087 ts = __open_export_target_session(mdsc, mi->export_targets[i]);
1088 if (!IS_ERR(ts))
1089 ceph_put_mds_session(ts);
ed0552a1
SW
1090 }
1091}
1092
154f42c2
SW
1093void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc,
1094 struct ceph_mds_session *session)
1095{
1096 mutex_lock(&mdsc->mutex);
1097 __open_export_target_sessions(mdsc, session);
1098 mutex_unlock(&mdsc->mutex);
1099}
1100
2f2dc053
SW
1101/*
1102 * session caps
1103 */
1104
c8a96a31
JL
1105static void detach_cap_releases(struct ceph_mds_session *session,
1106 struct list_head *target)
2f2dc053 1107{
c8a96a31
JL
1108 lockdep_assert_held(&session->s_cap_lock);
1109
1110 list_splice_init(&session->s_cap_releases, target);
745a8e3b 1111 session->s_num_cap_releases = 0;
c8a96a31
JL
1112 dout("dispose_cap_releases mds%d\n", session->s_mds);
1113}
2f2dc053 1114
c8a96a31
JL
1115static void dispose_cap_releases(struct ceph_mds_client *mdsc,
1116 struct list_head *dispose)
1117{
1118 while (!list_empty(dispose)) {
745a8e3b
YZ
1119 struct ceph_cap *cap;
1120 /* zero out the in-progress message */
c8a96a31 1121 cap = list_first_entry(dispose, struct ceph_cap, session_caps);
745a8e3b
YZ
1122 list_del(&cap->session_caps);
1123 ceph_put_cap(mdsc, cap);
2f2dc053 1124 }
2f2dc053
SW
1125}
1126
1c841a96
YZ
1127static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1128 struct ceph_mds_session *session)
1129{
1130 struct ceph_mds_request *req;
1131 struct rb_node *p;
1132
1133 dout("cleanup_session_requests mds%d\n", session->s_mds);
1134 mutex_lock(&mdsc->mutex);
1135 while (!list_empty(&session->s_unsafe)) {
1136 req = list_first_entry(&session->s_unsafe,
1137 struct ceph_mds_request, r_unsafe_item);
3e0708b9
YZ
1138 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1139 req->r_tid);
1c841a96
YZ
1140 __unregister_request(mdsc, req);
1141 }
1142 /* zero r_attempts, so kick_requests() will re-send requests */
1143 p = rb_first(&mdsc->request_tree);
1144 while (p) {
1145 req = rb_entry(p, struct ceph_mds_request, r_node);
1146 p = rb_next(p);
1147 if (req->r_session &&
1148 req->r_session->s_mds == session->s_mds)
1149 req->r_attempts = 0;
1150 }
1151 mutex_unlock(&mdsc->mutex);
1152}
1153
2f2dc053 1154/*
f818a736
SW
1155 * Helper to safely iterate over all caps associated with a session, with
1156 * special care taken to handle a racing __ceph_remove_cap().
2f2dc053 1157 *
f818a736 1158 * Caller must hold session s_mutex.
2f2dc053
SW
1159 */
1160static int iterate_session_caps(struct ceph_mds_session *session,
1161 int (*cb)(struct inode *, struct ceph_cap *,
1162 void *), void *arg)
1163{
7c1332b8
SW
1164 struct list_head *p;
1165 struct ceph_cap *cap;
1166 struct inode *inode, *last_inode = NULL;
1167 struct ceph_cap *old_cap = NULL;
2f2dc053
SW
1168 int ret;
1169
1170 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1171 spin_lock(&session->s_cap_lock);
7c1332b8
SW
1172 p = session->s_caps.next;
1173 while (p != &session->s_caps) {
1174 cap = list_entry(p, struct ceph_cap, session_caps);
2f2dc053 1175 inode = igrab(&cap->ci->vfs_inode);
7c1332b8
SW
1176 if (!inode) {
1177 p = p->next;
2f2dc053 1178 continue;
7c1332b8
SW
1179 }
1180 session->s_cap_iterator = cap;
2f2dc053 1181 spin_unlock(&session->s_cap_lock);
7c1332b8
SW
1182
1183 if (last_inode) {
1184 iput(last_inode);
1185 last_inode = NULL;
1186 }
1187 if (old_cap) {
37151668 1188 ceph_put_cap(session->s_mdsc, old_cap);
7c1332b8
SW
1189 old_cap = NULL;
1190 }
1191
2f2dc053 1192 ret = cb(inode, cap, arg);
7c1332b8
SW
1193 last_inode = inode;
1194
2f2dc053 1195 spin_lock(&session->s_cap_lock);
7c1332b8 1196 p = p->next;
d37b1d99 1197 if (!cap->ci) {
7c1332b8
SW
1198 dout("iterate_session_caps finishing cap %p removal\n",
1199 cap);
1200 BUG_ON(cap->session != session);
745a8e3b 1201 cap->session = NULL;
7c1332b8
SW
1202 list_del_init(&cap->session_caps);
1203 session->s_nr_caps--;
745a8e3b
YZ
1204 if (cap->queue_release) {
1205 list_add_tail(&cap->session_caps,
1206 &session->s_cap_releases);
1207 session->s_num_cap_releases++;
1208 } else {
1209 old_cap = cap; /* put_cap it w/o locks held */
1210 }
7c1332b8 1211 }
5dacf091
SW
1212 if (ret < 0)
1213 goto out;
2f2dc053 1214 }
5dacf091
SW
1215 ret = 0;
1216out:
7c1332b8 1217 session->s_cap_iterator = NULL;
2f2dc053 1218 spin_unlock(&session->s_cap_lock);
7c1332b8 1219
e96a650a 1220 iput(last_inode);
7c1332b8 1221 if (old_cap)
37151668 1222 ceph_put_cap(session->s_mdsc, old_cap);
7c1332b8 1223
5dacf091 1224 return ret;
2f2dc053
SW
1225}
1226
1227static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
6c99f254 1228 void *arg)
2f2dc053 1229{
6c93df5d 1230 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
2f2dc053 1231 struct ceph_inode_info *ci = ceph_inode(inode);
553adfd9 1232 LIST_HEAD(to_remove);
6c93df5d
YZ
1233 bool drop = false;
1234 bool invalidate = false;
6c99f254 1235
2f2dc053
SW
1236 dout("removing cap %p, ci is %p, inode is %p\n",
1237 cap, ci, &ci->vfs_inode);
be655596 1238 spin_lock(&ci->i_ceph_lock);
a096b09a 1239 __ceph_remove_cap(cap, false);
571ade33 1240 if (!ci->i_auth_cap) {
553adfd9 1241 struct ceph_cap_flush *cf;
6c93df5d 1242 struct ceph_mds_client *mdsc = fsc->mdsc;
6c99f254 1243
77310320
YZ
1244 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1245
6c93df5d 1246 if (ci->i_wrbuffer_ref > 0 &&
52953d55 1247 READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
6c93df5d
YZ
1248 invalidate = true;
1249
e4500b5e
YZ
1250 while (!list_empty(&ci->i_cap_flush_list)) {
1251 cf = list_first_entry(&ci->i_cap_flush_list,
1252 struct ceph_cap_flush, i_list);
8cdcc07d 1253 list_move(&cf->i_list, &to_remove);
553adfd9
YZ
1254 }
1255
6c99f254 1256 spin_lock(&mdsc->cap_dirty_lock);
8310b089 1257
e4500b5e
YZ
1258 list_for_each_entry(cf, &to_remove, i_list)
1259 list_del(&cf->g_list);
8310b089 1260
6c99f254 1261 if (!list_empty(&ci->i_dirty_item)) {
3e0708b9
YZ
1262 pr_warn_ratelimited(
1263 " dropping dirty %s state for %p %lld\n",
6c99f254
SW
1264 ceph_cap_string(ci->i_dirty_caps),
1265 inode, ceph_ino(inode));
1266 ci->i_dirty_caps = 0;
1267 list_del_init(&ci->i_dirty_item);
6c93df5d 1268 drop = true;
6c99f254
SW
1269 }
1270 if (!list_empty(&ci->i_flushing_item)) {
3e0708b9
YZ
1271 pr_warn_ratelimited(
1272 " dropping dirty+flushing %s state for %p %lld\n",
6c99f254
SW
1273 ceph_cap_string(ci->i_flushing_caps),
1274 inode, ceph_ino(inode));
1275 ci->i_flushing_caps = 0;
1276 list_del_init(&ci->i_flushing_item);
1277 mdsc->num_cap_flushing--;
6c93df5d 1278 drop = true;
6c99f254 1279 }
6c99f254 1280 spin_unlock(&mdsc->cap_dirty_lock);
553adfd9 1281
b3f8d68f
YZ
1282 if (atomic_read(&ci->i_filelock_ref) > 0) {
1283 /* make further file lock syscall return -EIO */
1284 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1285 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1286 inode, ceph_ino(inode));
1287 }
1288
f66fd9f0 1289 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
e4500b5e 1290 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
f66fd9f0
YZ
1291 ci->i_prealloc_cap_flush = NULL;
1292 }
6c99f254 1293 }
be655596 1294 spin_unlock(&ci->i_ceph_lock);
553adfd9
YZ
1295 while (!list_empty(&to_remove)) {
1296 struct ceph_cap_flush *cf;
1297 cf = list_first_entry(&to_remove,
e4500b5e
YZ
1298 struct ceph_cap_flush, i_list);
1299 list_del(&cf->i_list);
f66fd9f0 1300 ceph_free_cap_flush(cf);
553adfd9 1301 }
77310320
YZ
1302
1303 wake_up_all(&ci->i_cap_wq);
6c93df5d
YZ
1304 if (invalidate)
1305 ceph_queue_invalidate(inode);
77310320 1306 if (drop)
6c99f254 1307 iput(inode);
2f2dc053
SW
1308 return 0;
1309}
1310
1311/*
1312 * caller must hold session s_mutex
1313 */
1314static void remove_session_caps(struct ceph_mds_session *session)
1315{
6c93df5d
YZ
1316 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1317 struct super_block *sb = fsc->sb;
c8a96a31
JL
1318 LIST_HEAD(dispose);
1319
2f2dc053 1320 dout("remove_session_caps on %p\n", session);
6c93df5d 1321 iterate_session_caps(session, remove_session_caps_cb, fsc);
6f60f889 1322
c8799fc4
YZ
1323 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1324
6f60f889
YZ
1325 spin_lock(&session->s_cap_lock);
1326 if (session->s_nr_caps > 0) {
6f60f889
YZ
1327 struct inode *inode;
1328 struct ceph_cap *cap, *prev = NULL;
1329 struct ceph_vino vino;
1330 /*
1331 * iterate_session_caps() skips inodes that are being
1332 * deleted, we need to wait until deletions are complete.
1333 * __wait_on_freeing_inode() is designed for the job,
1334 * but it is not exported, so use lookup inode function
1335 * to access it.
1336 */
1337 while (!list_empty(&session->s_caps)) {
1338 cap = list_entry(session->s_caps.next,
1339 struct ceph_cap, session_caps);
1340 if (cap == prev)
1341 break;
1342 prev = cap;
1343 vino = cap->ci->i_vino;
1344 spin_unlock(&session->s_cap_lock);
1345
ed284c49 1346 inode = ceph_find_inode(sb, vino);
6f60f889
YZ
1347 iput(inode);
1348
1349 spin_lock(&session->s_cap_lock);
1350 }
1351 }
745a8e3b
YZ
1352
1353 // drop cap expires and unlock s_cap_lock
c8a96a31 1354 detach_cap_releases(session, &dispose);
6f60f889 1355
2f2dc053 1356 BUG_ON(session->s_nr_caps > 0);
6c99f254 1357 BUG_ON(!list_empty(&session->s_cap_flushing));
c8a96a31
JL
1358 spin_unlock(&session->s_cap_lock);
1359 dispose_cap_releases(session->s_mdsc, &dispose);
2f2dc053
SW
1360}
1361
1362/*
1363 * wake up any threads waiting on this session's caps. if the cap is
1364 * old (didn't get renewed on the client reconnect), remove it now.
1365 *
1366 * caller must hold s_mutex.
1367 */
1368static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1369 void *arg)
1370{
0dc2570f
SW
1371 struct ceph_inode_info *ci = ceph_inode(inode);
1372
0dc2570f 1373 if (arg) {
be655596 1374 spin_lock(&ci->i_ceph_lock);
0dc2570f
SW
1375 ci->i_wanted_max_size = 0;
1376 ci->i_requested_max_size = 0;
be655596 1377 spin_unlock(&ci->i_ceph_lock);
0dc2570f 1378 }
e5360309 1379 wake_up_all(&ci->i_cap_wq);
2f2dc053
SW
1380 return 0;
1381}
1382
0dc2570f
SW
1383static void wake_up_session_caps(struct ceph_mds_session *session,
1384 int reconnect)
2f2dc053
SW
1385{
1386 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
0dc2570f
SW
1387 iterate_session_caps(session, wake_up_session_cb,
1388 (void *)(unsigned long)reconnect);
2f2dc053
SW
1389}
1390
1391/*
1392 * Send periodic message to MDS renewing all currently held caps. The
1393 * ack will reset the expiration for all caps from this session.
1394 *
1395 * caller holds s_mutex
1396 */
1397static int send_renew_caps(struct ceph_mds_client *mdsc,
1398 struct ceph_mds_session *session)
1399{
1400 struct ceph_msg *msg;
1401 int state;
1402
1403 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1404 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1405 pr_info("mds%d caps stale\n", session->s_mds);
e4cb4cb8 1406 session->s_renew_requested = jiffies;
2f2dc053
SW
1407
1408 /* do not try to renew caps until a recovering mds has reconnected
1409 * with its clients. */
1410 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1411 if (state < CEPH_MDS_STATE_RECONNECT) {
1412 dout("send_renew_caps ignoring mds%d (%s)\n",
1413 session->s_mds, ceph_mds_state_name(state));
1414 return 0;
1415 }
1416
1417 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1418 ceph_mds_state_name(state));
2f2dc053
SW
1419 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1420 ++session->s_renew_seq);
a79832f2
SW
1421 if (!msg)
1422 return -ENOMEM;
2f2dc053
SW
1423 ceph_con_send(&session->s_con, msg);
1424 return 0;
1425}
1426
186e4f7a
YZ
1427static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1428 struct ceph_mds_session *session, u64 seq)
1429{
1430 struct ceph_msg *msg;
1431
1432 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
a687ecaf 1433 session->s_mds, ceph_session_state_name(session->s_state), seq);
186e4f7a
YZ
1434 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1435 if (!msg)
1436 return -ENOMEM;
1437 ceph_con_send(&session->s_con, msg);
1438 return 0;
1439}
1440
1441
2f2dc053
SW
1442/*
1443 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
0dc2570f
SW
1444 *
1445 * Called under session->s_mutex
2f2dc053
SW
1446 */
1447static void renewed_caps(struct ceph_mds_client *mdsc,
1448 struct ceph_mds_session *session, int is_renew)
1449{
1450 int was_stale;
1451 int wake = 0;
1452
1453 spin_lock(&session->s_cap_lock);
1ce208a6 1454 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
2f2dc053
SW
1455
1456 session->s_cap_ttl = session->s_renew_requested +
1457 mdsc->mdsmap->m_session_timeout*HZ;
1458
1459 if (was_stale) {
1460 if (time_before(jiffies, session->s_cap_ttl)) {
1461 pr_info("mds%d caps renewed\n", session->s_mds);
1462 wake = 1;
1463 } else {
1464 pr_info("mds%d caps still stale\n", session->s_mds);
1465 }
1466 }
1467 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1468 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1469 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1470 spin_unlock(&session->s_cap_lock);
1471
1472 if (wake)
0dc2570f 1473 wake_up_session_caps(session, 0);
2f2dc053
SW
1474}
1475
1476/*
1477 * send a session close request
1478 */
1479static int request_close_session(struct ceph_mds_client *mdsc,
1480 struct ceph_mds_session *session)
1481{
1482 struct ceph_msg *msg;
2f2dc053
SW
1483
1484 dout("request_close_session mds%d state %s seq %lld\n",
a687ecaf 1485 session->s_mds, ceph_session_state_name(session->s_state),
2f2dc053
SW
1486 session->s_seq);
1487 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
a79832f2
SW
1488 if (!msg)
1489 return -ENOMEM;
1490 ceph_con_send(&session->s_con, msg);
fcff415c 1491 return 1;
2f2dc053
SW
1492}
1493
1494/*
1495 * Called with s_mutex held.
1496 */
1497static int __close_session(struct ceph_mds_client *mdsc,
1498 struct ceph_mds_session *session)
1499{
1500 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1501 return 0;
1502 session->s_state = CEPH_MDS_SESSION_CLOSING;
1503 return request_close_session(mdsc, session);
1504}
1505
040d7860
YZ
1506static bool drop_negative_children(struct dentry *dentry)
1507{
1508 struct dentry *child;
1509 bool all_negative = true;
1510
1511 if (!d_is_dir(dentry))
1512 goto out;
1513
1514 spin_lock(&dentry->d_lock);
1515 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
1516 if (d_really_is_positive(child)) {
1517 all_negative = false;
1518 break;
1519 }
1520 }
1521 spin_unlock(&dentry->d_lock);
1522
1523 if (all_negative)
1524 shrink_dcache_parent(dentry);
1525out:
1526 return all_negative;
1527}
1528
2f2dc053
SW
1529/*
1530 * Trim old(er) caps.
1531 *
1532 * Because we can't cache an inode without one or more caps, we do
1533 * this indirectly: if a cap is unused, we prune its aliases, at which
1534 * point the inode will hopefully get dropped to.
1535 *
1536 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1537 * memory pressure from the MDS, though, so it needn't be perfect.
1538 */
1539static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1540{
1541 struct ceph_mds_session *session = arg;
1542 struct ceph_inode_info *ci = ceph_inode(inode);
979abfdd 1543 int used, wanted, oissued, mine;
2f2dc053
SW
1544
1545 if (session->s_trim_caps <= 0)
1546 return -1;
1547
be655596 1548 spin_lock(&ci->i_ceph_lock);
2f2dc053
SW
1549 mine = cap->issued | cap->implemented;
1550 used = __ceph_caps_used(ci);
979abfdd 1551 wanted = __ceph_caps_file_wanted(ci);
2f2dc053
SW
1552 oissued = __ceph_caps_issued_other(ci, cap);
1553
979abfdd 1554 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
2f2dc053 1555 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
979abfdd
YZ
1556 ceph_cap_string(used), ceph_cap_string(wanted));
1557 if (cap == ci->i_auth_cap) {
622f3e25
YZ
1558 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1559 !list_empty(&ci->i_cap_snaps))
979abfdd
YZ
1560 goto out;
1561 if ((used | wanted) & CEPH_CAP_ANY_WR)
1562 goto out;
89aa5930
YZ
1563 /* Note: it's possible that i_filelock_ref becomes non-zero
1564 * after dropping auth caps. It doesn't hurt because reply
1565 * of lock mds request will re-add auth caps. */
1566 if (atomic_read(&ci->i_filelock_ref) > 0)
1567 goto out;
979abfdd 1568 }
5e804ac4
YZ
1569 /* The inode has cached pages, but it's no longer used.
1570 * we can safely drop it */
1571 if (wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
1572 !(oissued & CEPH_CAP_FILE_CACHE)) {
1573 used = 0;
1574 oissued = 0;
1575 }
979abfdd 1576 if ((used | wanted) & ~oissued & mine)
2f2dc053
SW
1577 goto out; /* we need these caps */
1578
2f2dc053
SW
1579 if (oissued) {
1580 /* we aren't the only cap.. just remove us */
a096b09a 1581 __ceph_remove_cap(cap, true);
040d7860 1582 session->s_trim_caps--;
2f2dc053 1583 } else {
040d7860 1584 struct dentry *dentry;
5e804ac4 1585 /* try dropping referring dentries */
be655596 1586 spin_unlock(&ci->i_ceph_lock);
040d7860
YZ
1587 dentry = d_find_any_alias(inode);
1588 if (dentry && drop_negative_children(dentry)) {
1589 int count;
1590 dput(dentry);
1591 d_prune_aliases(inode);
1592 count = atomic_read(&inode->i_count);
1593 if (count == 1)
1594 session->s_trim_caps--;
1595 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1596 inode, cap, count);
1597 } else {
1598 dput(dentry);
1599 }
2f2dc053
SW
1600 return 0;
1601 }
1602
1603out:
be655596 1604 spin_unlock(&ci->i_ceph_lock);
2f2dc053
SW
1605 return 0;
1606}
1607
1608/*
1609 * Trim session cap count down to some max number.
1610 */
e30ee581
ZZ
1611int ceph_trim_caps(struct ceph_mds_client *mdsc,
1612 struct ceph_mds_session *session,
1613 int max_caps)
2f2dc053
SW
1614{
1615 int trim_caps = session->s_nr_caps - max_caps;
1616
1617 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1618 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1619 if (trim_caps > 0) {
1620 session->s_trim_caps = trim_caps;
1621 iterate_session_caps(session, trim_caps_cb, session);
1622 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1623 session->s_mds, session->s_nr_caps, max_caps,
1624 trim_caps - session->s_trim_caps);
5dacf091 1625 session->s_trim_caps = 0;
2f2dc053 1626 }
a56371d9 1627
a56371d9 1628 ceph_send_cap_releases(mdsc, session);
2f2dc053
SW
1629 return 0;
1630}
1631
8310b089
YZ
1632static int check_caps_flush(struct ceph_mds_client *mdsc,
1633 u64 want_flush_tid)
1634{
8310b089
YZ
1635 int ret = 1;
1636
1637 spin_lock(&mdsc->cap_dirty_lock);
e4500b5e
YZ
1638 if (!list_empty(&mdsc->cap_flush_list)) {
1639 struct ceph_cap_flush *cf =
1640 list_first_entry(&mdsc->cap_flush_list,
1641 struct ceph_cap_flush, g_list);
1642 if (cf->tid <= want_flush_tid) {
1643 dout("check_caps_flush still flushing tid "
1644 "%llu <= %llu\n", cf->tid, want_flush_tid);
1645 ret = 0;
1646 }
8310b089
YZ
1647 }
1648 spin_unlock(&mdsc->cap_dirty_lock);
1649 return ret;
d3383a8e
YZ
1650}
1651
2f2dc053
SW
1652/*
1653 * flush all dirty inode data to disk.
1654 *
8310b089 1655 * returns true if we've flushed through want_flush_tid
2f2dc053 1656 */
affbc19a 1657static void wait_caps_flush(struct ceph_mds_client *mdsc,
0e294387 1658 u64 want_flush_tid)
2f2dc053 1659{
0e294387 1660 dout("check_caps_flush want %llu\n", want_flush_tid);
8310b089
YZ
1661
1662 wait_event(mdsc->cap_flushing_wq,
1663 check_caps_flush(mdsc, want_flush_tid));
1664
1665 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
2f2dc053
SW
1666}
1667
1668/*
1669 * called under s_mutex
1670 */
3d7ded4d
SW
1671void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1672 struct ceph_mds_session *session)
2f2dc053 1673{
745a8e3b
YZ
1674 struct ceph_msg *msg = NULL;
1675 struct ceph_mds_cap_release *head;
1676 struct ceph_mds_cap_item *item;
92475f05 1677 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
745a8e3b
YZ
1678 struct ceph_cap *cap;
1679 LIST_HEAD(tmp_list);
1680 int num_cap_releases;
92475f05
JL
1681 __le32 barrier, *cap_barrier;
1682
1683 down_read(&osdc->lock);
1684 barrier = cpu_to_le32(osdc->epoch_barrier);
1685 up_read(&osdc->lock);
2f2dc053 1686
0f8605f2 1687 spin_lock(&session->s_cap_lock);
745a8e3b
YZ
1688again:
1689 list_splice_init(&session->s_cap_releases, &tmp_list);
1690 num_cap_releases = session->s_num_cap_releases;
1691 session->s_num_cap_releases = 0;
2f2dc053 1692 spin_unlock(&session->s_cap_lock);
e01a5946 1693
745a8e3b
YZ
1694 while (!list_empty(&tmp_list)) {
1695 if (!msg) {
1696 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
09cbfeaf 1697 PAGE_SIZE, GFP_NOFS, false);
745a8e3b
YZ
1698 if (!msg)
1699 goto out_err;
1700 head = msg->front.iov_base;
1701 head->num = cpu_to_le32(0);
1702 msg->front.iov_len = sizeof(*head);
92475f05
JL
1703
1704 msg->hdr.version = cpu_to_le16(2);
1705 msg->hdr.compat_version = cpu_to_le16(1);
745a8e3b 1706 }
92475f05 1707
745a8e3b
YZ
1708 cap = list_first_entry(&tmp_list, struct ceph_cap,
1709 session_caps);
1710 list_del(&cap->session_caps);
1711 num_cap_releases--;
e01a5946 1712
00bd8edb 1713 head = msg->front.iov_base;
745a8e3b
YZ
1714 le32_add_cpu(&head->num, 1);
1715 item = msg->front.iov_base + msg->front.iov_len;
1716 item->ino = cpu_to_le64(cap->cap_ino);
1717 item->cap_id = cpu_to_le64(cap->cap_id);
1718 item->migrate_seq = cpu_to_le32(cap->mseq);
1719 item->seq = cpu_to_le32(cap->issue_seq);
1720 msg->front.iov_len += sizeof(*item);
1721
1722 ceph_put_cap(mdsc, cap);
1723
1724 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
92475f05
JL
1725 // Append cap_barrier field
1726 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1727 *cap_barrier = barrier;
1728 msg->front.iov_len += sizeof(*cap_barrier);
1729
745a8e3b
YZ
1730 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1731 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1732 ceph_con_send(&session->s_con, msg);
1733 msg = NULL;
1734 }
00bd8edb 1735 }
e01a5946 1736
745a8e3b 1737 BUG_ON(num_cap_releases != 0);
e01a5946 1738
745a8e3b
YZ
1739 spin_lock(&session->s_cap_lock);
1740 if (!list_empty(&session->s_cap_releases))
1741 goto again;
1742 spin_unlock(&session->s_cap_lock);
1743
1744 if (msg) {
92475f05
JL
1745 // Append cap_barrier field
1746 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1747 *cap_barrier = barrier;
1748 msg->front.iov_len += sizeof(*cap_barrier);
1749
745a8e3b
YZ
1750 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1751 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1752 ceph_con_send(&session->s_con, msg);
e01a5946 1753 }
745a8e3b
YZ
1754 return;
1755out_err:
1756 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1757 session->s_mds);
1758 spin_lock(&session->s_cap_lock);
1759 list_splice(&tmp_list, &session->s_cap_releases);
1760 session->s_num_cap_releases += num_cap_releases;
1761 spin_unlock(&session->s_cap_lock);
e01a5946
SW
1762}
1763
2f2dc053
SW
1764/*
1765 * requests
1766 */
1767
54008399
YZ
1768int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1769 struct inode *dir)
1770{
1771 struct ceph_inode_info *ci = ceph_inode(dir);
1772 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1773 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
2a5beea3 1774 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
54008399
YZ
1775 int order, num_entries;
1776
1777 spin_lock(&ci->i_ceph_lock);
1778 num_entries = ci->i_files + ci->i_subdirs;
1779 spin_unlock(&ci->i_ceph_lock);
1780 num_entries = max(num_entries, 1);
1781 num_entries = min(num_entries, opt->max_readdir);
1782
1783 order = get_order(size * num_entries);
1784 while (order >= 0) {
2a5beea3
YZ
1785 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
1786 __GFP_NOWARN,
1787 order);
1788 if (rinfo->dir_entries)
54008399
YZ
1789 break;
1790 order--;
1791 }
2a5beea3 1792 if (!rinfo->dir_entries)
54008399
YZ
1793 return -ENOMEM;
1794
1795 num_entries = (PAGE_SIZE << order) / size;
1796 num_entries = min(num_entries, opt->max_readdir);
1797
1798 rinfo->dir_buf_size = PAGE_SIZE << order;
1799 req->r_num_caps = num_entries + 1;
1800 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1801 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1802 return 0;
1803}
1804
2f2dc053
SW
1805/*
1806 * Create an mds request.
1807 */
1808struct ceph_mds_request *
1809ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1810{
1811 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
63ecae7e 1812 struct timespec64 ts;
2f2dc053
SW
1813
1814 if (!req)
1815 return ERR_PTR(-ENOMEM);
1816
b4556396 1817 mutex_init(&req->r_fill_mutex);
37151668 1818 req->r_mdsc = mdsc;
2f2dc053
SW
1819 req->r_started = jiffies;
1820 req->r_resend_mds = -1;
1821 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
68cd5b4b 1822 INIT_LIST_HEAD(&req->r_unsafe_target_item);
2f2dc053 1823 req->r_fmode = -1;
153c8e6b 1824 kref_init(&req->r_kref);
fcd00b68 1825 RB_CLEAR_NODE(&req->r_node);
2f2dc053
SW
1826 INIT_LIST_HEAD(&req->r_wait);
1827 init_completion(&req->r_completion);
1828 init_completion(&req->r_safe_completion);
1829 INIT_LIST_HEAD(&req->r_unsafe_item);
1830
63ecae7e 1831 ktime_get_coarse_real_ts64(&ts);
0ed1e90a 1832 req->r_stamp = timespec64_trunc(ts, mdsc->fsc->sb->s_time_gran);
b8e69066 1833
2f2dc053
SW
1834 req->r_op = op;
1835 req->r_direct_mode = mode;
1836 return req;
1837}
1838
1839/*
44ca18f2 1840 * return oldest (lowest) request, tid in request tree, 0 if none.
2f2dc053
SW
1841 *
1842 * called under mdsc->mutex.
1843 */
44ca18f2
SW
1844static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1845{
1846 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1847 return NULL;
1848 return rb_entry(rb_first(&mdsc->request_tree),
1849 struct ceph_mds_request, r_node);
1850}
1851
e8a7b8b1 1852static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
2f2dc053 1853{
e8a7b8b1 1854 return mdsc->oldest_tid;
2f2dc053
SW
1855}
1856
1857/*
1858 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1859 * on build_path_from_dentry in fs/cifs/dir.c.
1860 *
1861 * If @stop_on_nosnap, generate path relative to the first non-snapped
1862 * inode.
1863 *
1864 * Encode hidden .snap dirs as a double /, i.e.
1865 * foo/.snap/bar -> foo//bar
1866 */
1867char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1868 int stop_on_nosnap)
1869{
1870 struct dentry *temp;
1871 char *path;
1872 int len, pos;
1b71fe2e 1873 unsigned seq;
2f2dc053 1874
d37b1d99 1875 if (!dentry)
2f2dc053
SW
1876 return ERR_PTR(-EINVAL);
1877
1878retry:
1879 len = 0;
1b71fe2e
AV
1880 seq = read_seqbegin(&rename_lock);
1881 rcu_read_lock();
2f2dc053 1882 for (temp = dentry; !IS_ROOT(temp);) {
2b0143b5 1883 struct inode *inode = d_inode(temp);
2f2dc053
SW
1884 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1885 len++; /* slash only */
1886 else if (stop_on_nosnap && inode &&
1887 ceph_snap(inode) == CEPH_NOSNAP)
1888 break;
1889 else
1890 len += 1 + temp->d_name.len;
1891 temp = temp->d_parent;
2f2dc053 1892 }
1b71fe2e 1893 rcu_read_unlock();
2f2dc053
SW
1894 if (len)
1895 len--; /* no leading '/' */
1896
1897 path = kmalloc(len+1, GFP_NOFS);
d37b1d99 1898 if (!path)
2f2dc053
SW
1899 return ERR_PTR(-ENOMEM);
1900 pos = len;
1901 path[pos] = 0; /* trailing null */
1b71fe2e 1902 rcu_read_lock();
2f2dc053 1903 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1b71fe2e 1904 struct inode *inode;
2f2dc053 1905
1b71fe2e 1906 spin_lock(&temp->d_lock);
2b0143b5 1907 inode = d_inode(temp);
2f2dc053 1908 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
104648ad 1909 dout("build_path path+%d: %p SNAPDIR\n",
2f2dc053
SW
1910 pos, temp);
1911 } else if (stop_on_nosnap && inode &&
1912 ceph_snap(inode) == CEPH_NOSNAP) {
9d5a09e6 1913 spin_unlock(&temp->d_lock);
2f2dc053
SW
1914 break;
1915 } else {
1916 pos -= temp->d_name.len;
1b71fe2e
AV
1917 if (pos < 0) {
1918 spin_unlock(&temp->d_lock);
2f2dc053 1919 break;
1b71fe2e 1920 }
2f2dc053
SW
1921 strncpy(path + pos, temp->d_name.name,
1922 temp->d_name.len);
2f2dc053 1923 }
1b71fe2e 1924 spin_unlock(&temp->d_lock);
2f2dc053
SW
1925 if (pos)
1926 path[--pos] = '/';
1927 temp = temp->d_parent;
2f2dc053 1928 }
1b71fe2e
AV
1929 rcu_read_unlock();
1930 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
104648ad 1931 pr_err("build_path did not end path lookup where "
2f2dc053
SW
1932 "expected, namelen is %d, pos is %d\n", len, pos);
1933 /* presumably this is only possible if racing with a
1934 rename of one of the parent directories (we can not
1935 lock the dentries above us to prevent this, but
1936 retrying should be harmless) */
1937 kfree(path);
1938 goto retry;
1939 }
1940
2b0143b5 1941 *base = ceph_ino(d_inode(temp));
2f2dc053 1942 *plen = len;
104648ad 1943 dout("build_path on %p %d built %llx '%.*s'\n",
84d08fa8 1944 dentry, d_count(dentry), *base, len, path);
2f2dc053
SW
1945 return path;
1946}
1947
fd36a717 1948static int build_dentry_path(struct dentry *dentry, struct inode *dir,
2f2dc053
SW
1949 const char **ppath, int *ppathlen, u64 *pino,
1950 int *pfreepath)
1951{
1952 char *path;
1953
c6b0b656 1954 rcu_read_lock();
fd36a717
JL
1955 if (!dir)
1956 dir = d_inode_rcu(dentry->d_parent);
c6b0b656
JL
1957 if (dir && ceph_snap(dir) == CEPH_NOSNAP) {
1958 *pino = ceph_ino(dir);
1959 rcu_read_unlock();
2f2dc053
SW
1960 *ppath = dentry->d_name.name;
1961 *ppathlen = dentry->d_name.len;
1962 return 0;
1963 }
c6b0b656 1964 rcu_read_unlock();
2f2dc053
SW
1965 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1966 if (IS_ERR(path))
1967 return PTR_ERR(path);
1968 *ppath = path;
1969 *pfreepath = 1;
1970 return 0;
1971}
1972
1973static int build_inode_path(struct inode *inode,
1974 const char **ppath, int *ppathlen, u64 *pino,
1975 int *pfreepath)
1976{
1977 struct dentry *dentry;
1978 char *path;
1979
1980 if (ceph_snap(inode) == CEPH_NOSNAP) {
1981 *pino = ceph_ino(inode);
1982 *ppathlen = 0;
1983 return 0;
1984 }
1985 dentry = d_find_alias(inode);
1986 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1987 dput(dentry);
1988 if (IS_ERR(path))
1989 return PTR_ERR(path);
1990 *ppath = path;
1991 *pfreepath = 1;
1992 return 0;
1993}
1994
1995/*
1996 * request arguments may be specified via an inode *, a dentry *, or
1997 * an explicit ino+path.
1998 */
1999static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
fd36a717
JL
2000 struct inode *rdiri, const char *rpath,
2001 u64 rino, const char **ppath, int *pathlen,
2f2dc053
SW
2002 u64 *ino, int *freepath)
2003{
2004 int r = 0;
2005
2006 if (rinode) {
2007 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
2008 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
2009 ceph_snap(rinode));
2010 } else if (rdentry) {
fd36a717
JL
2011 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
2012 freepath);
2f2dc053
SW
2013 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
2014 *ppath);
795858db 2015 } else if (rpath || rino) {
2f2dc053
SW
2016 *ino = rino;
2017 *ppath = rpath;
b000056a 2018 *pathlen = rpath ? strlen(rpath) : 0;
2f2dc053
SW
2019 dout(" path %.*s\n", *pathlen, rpath);
2020 }
2021
2022 return r;
2023}
2024
2025/*
2026 * called under mdsc->mutex
2027 */
2028static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
2029 struct ceph_mds_request *req,
6e6f0923 2030 int mds, bool drop_cap_releases)
2f2dc053
SW
2031{
2032 struct ceph_msg *msg;
2033 struct ceph_mds_request_head *head;
2034 const char *path1 = NULL;
2035 const char *path2 = NULL;
2036 u64 ino1 = 0, ino2 = 0;
2037 int pathlen1 = 0, pathlen2 = 0;
2038 int freepath1 = 0, freepath2 = 0;
2039 int len;
2040 u16 releases;
2041 void *p, *end;
2042 int ret;
2043
2044 ret = set_request_path_attr(req->r_inode, req->r_dentry,
3dd69aab 2045 req->r_parent, req->r_path1, req->r_ino1.ino,
2f2dc053
SW
2046 &path1, &pathlen1, &ino1, &freepath1);
2047 if (ret < 0) {
2048 msg = ERR_PTR(ret);
2049 goto out;
2050 }
2051
2052 ret = set_request_path_attr(NULL, req->r_old_dentry,
fd36a717 2053 req->r_old_dentry_dir,
2f2dc053
SW
2054 req->r_path2, req->r_ino2.ino,
2055 &path2, &pathlen2, &ino2, &freepath2);
2056 if (ret < 0) {
2057 msg = ERR_PTR(ret);
2058 goto out_free1;
2059 }
2060
2061 len = sizeof(*head) +
b8e69066 2062 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
777d738a 2063 sizeof(struct ceph_timespec);
2f2dc053
SW
2064
2065 /* calculate (max) length for cap releases */
2066 len += sizeof(struct ceph_mds_request_release) *
2067 (!!req->r_inode_drop + !!req->r_dentry_drop +
2068 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
2069 if (req->r_dentry_drop)
2070 len += req->r_dentry->d_name.len;
2071 if (req->r_old_dentry_drop)
2072 len += req->r_old_dentry->d_name.len;
2073
b61c2763 2074 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
a79832f2
SW
2075 if (!msg) {
2076 msg = ERR_PTR(-ENOMEM);
2f2dc053 2077 goto out_free2;
a79832f2 2078 }
2f2dc053 2079
7cfa0313 2080 msg->hdr.version = cpu_to_le16(2);
6df058c0
SW
2081 msg->hdr.tid = cpu_to_le64(req->r_tid);
2082
2f2dc053
SW
2083 head = msg->front.iov_base;
2084 p = msg->front.iov_base + sizeof(*head);
2085 end = msg->front.iov_base + msg->front.iov_len;
2086
2087 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
2088 head->op = cpu_to_le32(req->r_op);
ff3d0046
EB
2089 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
2090 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
2f2dc053
SW
2091 head->args = req->r_args;
2092
2093 ceph_encode_filepath(&p, end, ino1, path1);
2094 ceph_encode_filepath(&p, end, ino2, path2);
2095
e979cf50
SW
2096 /* make note of release offset, in case we need to replay */
2097 req->r_request_release_offset = p - msg->front.iov_base;
2098
2f2dc053
SW
2099 /* cap releases */
2100 releases = 0;
2101 if (req->r_inode_drop)
2102 releases += ceph_encode_inode_release(&p,
2b0143b5 2103 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
2f2dc053
SW
2104 mds, req->r_inode_drop, req->r_inode_unless, 0);
2105 if (req->r_dentry_drop)
2106 releases += ceph_encode_dentry_release(&p, req->r_dentry,
3dd69aab 2107 req->r_parent, mds, req->r_dentry_drop,
ca6c8ae0 2108 req->r_dentry_unless);
2f2dc053
SW
2109 if (req->r_old_dentry_drop)
2110 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
ca6c8ae0
JL
2111 req->r_old_dentry_dir, mds,
2112 req->r_old_dentry_drop,
2113 req->r_old_dentry_unless);
2f2dc053
SW
2114 if (req->r_old_inode_drop)
2115 releases += ceph_encode_inode_release(&p,
2b0143b5 2116 d_inode(req->r_old_dentry),
2f2dc053 2117 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
6e6f0923
YZ
2118
2119 if (drop_cap_releases) {
2120 releases = 0;
2121 p = msg->front.iov_base + req->r_request_release_offset;
2122 }
2123
2f2dc053
SW
2124 head->num_releases = cpu_to_le16(releases);
2125
b8e69066 2126 /* time stamp */
1f041a89
YZ
2127 {
2128 struct ceph_timespec ts;
0ed1e90a 2129 ceph_encode_timespec64(&ts, &req->r_stamp);
1f041a89
YZ
2130 ceph_encode_copy(&p, &ts, sizeof(ts));
2131 }
b8e69066 2132
2f2dc053
SW
2133 BUG_ON(p > end);
2134 msg->front.iov_len = p - msg->front.iov_base;
2135 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2136
25e6bae3
YZ
2137 if (req->r_pagelist) {
2138 struct ceph_pagelist *pagelist = req->r_pagelist;
0e1a5ee6 2139 refcount_inc(&pagelist->refcnt);
25e6bae3
YZ
2140 ceph_msg_data_add_pagelist(msg, pagelist);
2141 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2142 } else {
2143 msg->hdr.data_len = 0;
ebf18f47 2144 }
02afca6c 2145
2f2dc053
SW
2146 msg->hdr.data_off = cpu_to_le16(0);
2147
2148out_free2:
2149 if (freepath2)
2150 kfree((char *)path2);
2151out_free1:
2152 if (freepath1)
2153 kfree((char *)path1);
2154out:
2155 return msg;
2156}
2157
2158/*
2159 * called under mdsc->mutex if error, under no mutex if
2160 * success.
2161 */
2162static void complete_request(struct ceph_mds_client *mdsc,
2163 struct ceph_mds_request *req)
2164{
2165 if (req->r_callback)
2166 req->r_callback(mdsc, req);
2167 else
03066f23 2168 complete_all(&req->r_completion);
2f2dc053
SW
2169}
2170
2171/*
2172 * called under mdsc->mutex
2173 */
2174static int __prepare_send_request(struct ceph_mds_client *mdsc,
2175 struct ceph_mds_request *req,
6e6f0923 2176 int mds, bool drop_cap_releases)
2f2dc053
SW
2177{
2178 struct ceph_mds_request_head *rhead;
2179 struct ceph_msg *msg;
2180 int flags = 0;
2181
2f2dc053 2182 req->r_attempts++;
e55b71f8
GF
2183 if (req->r_inode) {
2184 struct ceph_cap *cap =
2185 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2186
2187 if (cap)
2188 req->r_sent_on_mseq = cap->mseq;
2189 else
2190 req->r_sent_on_mseq = -1;
2191 }
2f2dc053
SW
2192 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2193 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2194
bc2de10d 2195 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
c5c9a0bf 2196 void *p;
01a92f17
SW
2197 /*
2198 * Replay. Do not regenerate message (and rebuild
2199 * paths, etc.); just use the original message.
2200 * Rebuilding paths will break for renames because
2201 * d_move mangles the src name.
2202 */
2203 msg = req->r_request;
2204 rhead = msg->front.iov_base;
2205
2206 flags = le32_to_cpu(rhead->flags);
2207 flags |= CEPH_MDS_FLAG_REPLAY;
2208 rhead->flags = cpu_to_le32(flags);
2209
2210 if (req->r_target_inode)
2211 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2212
2213 rhead->num_retry = req->r_attempts - 1;
e979cf50
SW
2214
2215 /* remove cap/dentry releases from message */
2216 rhead->num_releases = 0;
c5c9a0bf
YZ
2217
2218 /* time stamp */
2219 p = msg->front.iov_base + req->r_request_release_offset;
1f041a89
YZ
2220 {
2221 struct ceph_timespec ts;
0ed1e90a 2222 ceph_encode_timespec64(&ts, &req->r_stamp);
1f041a89
YZ
2223 ceph_encode_copy(&p, &ts, sizeof(ts));
2224 }
c5c9a0bf
YZ
2225
2226 msg->front.iov_len = p - msg->front.iov_base;
2227 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
01a92f17
SW
2228 return 0;
2229 }
2230
2f2dc053
SW
2231 if (req->r_request) {
2232 ceph_msg_put(req->r_request);
2233 req->r_request = NULL;
2234 }
6e6f0923 2235 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
2f2dc053 2236 if (IS_ERR(msg)) {
e1518c7c 2237 req->r_err = PTR_ERR(msg);
a79832f2 2238 return PTR_ERR(msg);
2f2dc053
SW
2239 }
2240 req->r_request = msg;
2241
2242 rhead = msg->front.iov_base;
2f2dc053 2243 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
bc2de10d 2244 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2f2dc053 2245 flags |= CEPH_MDS_FLAG_REPLAY;
3dd69aab 2246 if (req->r_parent)
2f2dc053
SW
2247 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2248 rhead->flags = cpu_to_le32(flags);
2249 rhead->num_fwd = req->r_num_fwd;
2250 rhead->num_retry = req->r_attempts - 1;
01a92f17 2251 rhead->ino = 0;
2f2dc053 2252
3dd69aab 2253 dout(" r_parent = %p\n", req->r_parent);
2f2dc053
SW
2254 return 0;
2255}
2256
2257/*
2258 * send request, or put it on the appropriate wait list.
2259 */
d5548492 2260static void __do_request(struct ceph_mds_client *mdsc,
2f2dc053
SW
2261 struct ceph_mds_request *req)
2262{
2263 struct ceph_mds_session *session = NULL;
2264 int mds = -1;
48fec5d0 2265 int err = 0;
2f2dc053 2266
bc2de10d
JL
2267 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2268 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
eb1b8af3 2269 __unregister_request(mdsc, req);
d5548492 2270 return;
eb1b8af3 2271 }
2f2dc053
SW
2272
2273 if (req->r_timeout &&
2274 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2275 dout("do_request timed out\n");
2276 err = -EIO;
2277 goto finish;
2278 }
52953d55 2279 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
48fec5d0
YZ
2280 dout("do_request forced umount\n");
2281 err = -EIO;
2282 goto finish;
2283 }
52953d55 2284 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
e9e427f0
YZ
2285 if (mdsc->mdsmap_err) {
2286 err = mdsc->mdsmap_err;
2287 dout("do_request mdsmap err %d\n", err);
2288 goto finish;
2289 }
cc8e8342
YZ
2290 if (mdsc->mdsmap->m_epoch == 0) {
2291 dout("do_request no mdsmap, waiting for map\n");
2292 list_add(&req->r_wait, &mdsc->waiting_for_map);
d5548492 2293 return;
cc8e8342 2294 }
e9e427f0
YZ
2295 if (!(mdsc->fsc->mount_options->flags &
2296 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2297 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
2298 err = -ENOENT;
2299 pr_info("probably no mds server is up\n");
2300 goto finish;
2301 }
2302 }
2f2dc053 2303
dc69e2e9
SW
2304 put_request_session(req);
2305
2f2dc053
SW
2306 mds = __choose_mds(mdsc, req);
2307 if (mds < 0 ||
2308 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2309 dout("do_request no mds or not active, waiting for map\n");
2310 list_add(&req->r_wait, &mdsc->waiting_for_map);
d5548492 2311 return;
2f2dc053
SW
2312 }
2313
2314 /* get, open session */
2315 session = __ceph_lookup_mds_session(mdsc, mds);
9c423956 2316 if (!session) {
2f2dc053 2317 session = register_session(mdsc, mds);
9c423956
SW
2318 if (IS_ERR(session)) {
2319 err = PTR_ERR(session);
2320 goto finish;
2321 }
2322 }
dc69e2e9
SW
2323 req->r_session = get_session(session);
2324
2f2dc053 2325 dout("do_request mds%d session %p state %s\n", mds, session,
a687ecaf 2326 ceph_session_state_name(session->s_state));
2f2dc053
SW
2327 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2328 session->s_state != CEPH_MDS_SESSION_HUNG) {
fcff415c
YZ
2329 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2330 err = -EACCES;
2331 goto out_session;
2332 }
2f2dc053
SW
2333 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2334 session->s_state == CEPH_MDS_SESSION_CLOSING)
2335 __open_session(mdsc, session);
2336 list_add(&req->r_wait, &session->s_waiting);
2337 goto out_session;
2338 }
2339
2340 /* send request */
2f2dc053
SW
2341 req->r_resend_mds = -1; /* forget any previous mds hint */
2342
2343 if (req->r_request_started == 0) /* note request start time */
2344 req->r_request_started = jiffies;
2345
6e6f0923 2346 err = __prepare_send_request(mdsc, req, mds, false);
2f2dc053
SW
2347 if (!err) {
2348 ceph_msg_get(req->r_request);
2349 ceph_con_send(&session->s_con, req->r_request);
2350 }
2351
2352out_session:
2353 ceph_put_mds_session(session);
48fec5d0
YZ
2354finish:
2355 if (err) {
2356 dout("__do_request early error %d\n", err);
2357 req->r_err = err;
2358 complete_request(mdsc, req);
2359 __unregister_request(mdsc, req);
2360 }
d5548492 2361 return;
2f2dc053
SW
2362}
2363
2364/*
2365 * called under mdsc->mutex
2366 */
2367static void __wake_requests(struct ceph_mds_client *mdsc,
2368 struct list_head *head)
2369{
ed75ec2c
YZ
2370 struct ceph_mds_request *req;
2371 LIST_HEAD(tmp_list);
2372
2373 list_splice_init(head, &tmp_list);
2f2dc053 2374
ed75ec2c
YZ
2375 while (!list_empty(&tmp_list)) {
2376 req = list_entry(tmp_list.next,
2377 struct ceph_mds_request, r_wait);
2f2dc053 2378 list_del_init(&req->r_wait);
7971bd92 2379 dout(" wake request %p tid %llu\n", req, req->r_tid);
2f2dc053
SW
2380 __do_request(mdsc, req);
2381 }
2382}
2383
2384/*
2385 * Wake up threads with requests pending for @mds, so that they can
29790f26 2386 * resubmit their requests to a possibly different mds.
2f2dc053 2387 */
29790f26 2388static void kick_requests(struct ceph_mds_client *mdsc, int mds)
2f2dc053 2389{
44ca18f2 2390 struct ceph_mds_request *req;
282c1052 2391 struct rb_node *p = rb_first(&mdsc->request_tree);
2f2dc053
SW
2392
2393 dout("kick_requests mds%d\n", mds);
282c1052 2394 while (p) {
44ca18f2 2395 req = rb_entry(p, struct ceph_mds_request, r_node);
282c1052 2396 p = rb_next(p);
bc2de10d 2397 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
44ca18f2 2398 continue;
3de22be6
YZ
2399 if (req->r_attempts > 0)
2400 continue; /* only new requests */
44ca18f2
SW
2401 if (req->r_session &&
2402 req->r_session->s_mds == mds) {
2403 dout(" kicking tid %llu\n", req->r_tid);
03974e81 2404 list_del_init(&req->r_wait);
44ca18f2 2405 __do_request(mdsc, req);
2f2dc053
SW
2406 }
2407 }
2408}
2409
2410void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2411 struct ceph_mds_request *req)
2412{
2413 dout("submit_request on %p\n", req);
2414 mutex_lock(&mdsc->mutex);
2415 __register_request(mdsc, req, NULL);
2416 __do_request(mdsc, req);
2417 mutex_unlock(&mdsc->mutex);
2418}
2419
2420/*
2421 * Synchrously perform an mds request. Take care of all of the
2422 * session setup, forwarding, retry details.
2423 */
2424int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2425 struct inode *dir,
2426 struct ceph_mds_request *req)
2427{
2428 int err;
2429
2430 dout("do_request on %p\n", req);
2431
3dd69aab 2432 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
2f2dc053
SW
2433 if (req->r_inode)
2434 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
3dd69aab
JL
2435 if (req->r_parent)
2436 ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
844d87c3 2437 if (req->r_old_dentry_dir)
41b02e1f
SW
2438 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2439 CEPH_CAP_PIN);
2f2dc053
SW
2440
2441 /* issue */
2442 mutex_lock(&mdsc->mutex);
2443 __register_request(mdsc, req, dir);
2444 __do_request(mdsc, req);
2445
e1518c7c
SW
2446 if (req->r_err) {
2447 err = req->r_err;
e1518c7c 2448 goto out;
2f2dc053
SW
2449 }
2450
e1518c7c
SW
2451 /* wait */
2452 mutex_unlock(&mdsc->mutex);
2453 dout("do_request waiting\n");
5be73034 2454 if (!req->r_timeout && req->r_wait_for_completion) {
9280be24 2455 err = req->r_wait_for_completion(mdsc, req);
e1518c7c 2456 } else {
5be73034
ID
2457 long timeleft = wait_for_completion_killable_timeout(
2458 &req->r_completion,
2459 ceph_timeout_jiffies(req->r_timeout));
2460 if (timeleft > 0)
2461 err = 0;
2462 else if (!timeleft)
2463 err = -EIO; /* timed out */
2464 else
2465 err = timeleft; /* killed */
e1518c7c
SW
2466 }
2467 dout("do_request waited, got %d\n", err);
2468 mutex_lock(&mdsc->mutex);
5b1daecd 2469
e1518c7c 2470 /* only abort if we didn't race with a real reply */
bc2de10d 2471 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
e1518c7c
SW
2472 err = le32_to_cpu(req->r_reply_info.head->result);
2473 } else if (err < 0) {
2474 dout("aborted request %lld with %d\n", req->r_tid, err);
b4556396
SW
2475
2476 /*
2477 * ensure we aren't running concurrently with
2478 * ceph_fill_trace or ceph_readdir_prepopulate, which
2479 * rely on locks (dir mutex) held by our caller.
2480 */
2481 mutex_lock(&req->r_fill_mutex);
e1518c7c 2482 req->r_err = err;
bc2de10d 2483 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
b4556396 2484 mutex_unlock(&req->r_fill_mutex);
5b1daecd 2485
3dd69aab 2486 if (req->r_parent &&
167c9e35
SW
2487 (req->r_op & CEPH_MDS_OP_WRITE))
2488 ceph_invalidate_dir_request(req);
2f2dc053 2489 } else {
e1518c7c 2490 err = req->r_err;
2f2dc053 2491 }
2f2dc053 2492
e1518c7c
SW
2493out:
2494 mutex_unlock(&mdsc->mutex);
2f2dc053
SW
2495 dout("do_request %p done, result %d\n", req, err);
2496 return err;
2497}
2498
167c9e35 2499/*
2f276c51 2500 * Invalidate dir's completeness, dentry lease state on an aborted MDS
167c9e35
SW
2501 * namespace request.
2502 */
2503void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2504{
8d8f371c
YZ
2505 struct inode *dir = req->r_parent;
2506 struct inode *old_dir = req->r_old_dentry_dir;
167c9e35 2507
8d8f371c 2508 dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir);
167c9e35 2509
8d8f371c
YZ
2510 ceph_dir_clear_complete(dir);
2511 if (old_dir)
2512 ceph_dir_clear_complete(old_dir);
167c9e35
SW
2513 if (req->r_dentry)
2514 ceph_invalidate_dentry_lease(req->r_dentry);
2515 if (req->r_old_dentry)
2516 ceph_invalidate_dentry_lease(req->r_old_dentry);
2517}
2518
2f2dc053
SW
2519/*
2520 * Handle mds reply.
2521 *
2522 * We take the session mutex and parse and process the reply immediately.
2523 * This preserves the logical ordering of replies, capabilities, etc., sent
2524 * by the MDS as they are applied to our local cache.
2525 */
2526static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2527{
2528 struct ceph_mds_client *mdsc = session->s_mdsc;
2529 struct ceph_mds_request *req;
2530 struct ceph_mds_reply_head *head = msg->front.iov_base;
2531 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
982d6011 2532 struct ceph_snap_realm *realm;
2f2dc053
SW
2533 u64 tid;
2534 int err, result;
2600d2dd 2535 int mds = session->s_mds;
2f2dc053 2536
2f2dc053
SW
2537 if (msg->front.iov_len < sizeof(*head)) {
2538 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
9ec7cab1 2539 ceph_msg_dump(msg);
2f2dc053
SW
2540 return;
2541 }
2542
2543 /* get request, session */
6df058c0 2544 tid = le64_to_cpu(msg->hdr.tid);
2f2dc053 2545 mutex_lock(&mdsc->mutex);
fcd00b68 2546 req = lookup_get_request(mdsc, tid);
2f2dc053
SW
2547 if (!req) {
2548 dout("handle_reply on unknown tid %llu\n", tid);
2549 mutex_unlock(&mdsc->mutex);
2550 return;
2551 }
2552 dout("handle_reply %p\n", req);
2f2dc053
SW
2553
2554 /* correct session? */
d96d6049 2555 if (req->r_session != session) {
2f2dc053
SW
2556 pr_err("mdsc_handle_reply got %llu on session mds%d"
2557 " not mds%d\n", tid, session->s_mds,
2558 req->r_session ? req->r_session->s_mds : -1);
2559 mutex_unlock(&mdsc->mutex);
2560 goto out;
2561 }
2562
2563 /* dup? */
bc2de10d
JL
2564 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
2565 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
f3ae1b97 2566 pr_warn("got a dup %s reply on %llu from mds%d\n",
2f2dc053
SW
2567 head->safe ? "safe" : "unsafe", tid, mds);
2568 mutex_unlock(&mdsc->mutex);
2569 goto out;
2570 }
bc2de10d 2571 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
f3ae1b97 2572 pr_warn("got unsafe after safe on %llu from mds%d\n",
85792d0d
SW
2573 tid, mds);
2574 mutex_unlock(&mdsc->mutex);
2575 goto out;
2576 }
2f2dc053
SW
2577
2578 result = le32_to_cpu(head->result);
2579
2580 /*
e55b71f8
GF
2581 * Handle an ESTALE
2582 * if we're not talking to the authority, send to them
2583 * if the authority has changed while we weren't looking,
2584 * send to new authority
2585 * Otherwise we just have to return an ESTALE
2f2dc053
SW
2586 */
2587 if (result == -ESTALE) {
4c069a58 2588 dout("got ESTALE on request %llu\n", req->r_tid);
51da8e8c 2589 req->r_resend_mds = -1;
ca18bede 2590 if (req->r_direct_mode != USE_AUTH_MDS) {
4c069a58 2591 dout("not using auth, setting for that now\n");
e55b71f8 2592 req->r_direct_mode = USE_AUTH_MDS;
2f2dc053
SW
2593 __do_request(mdsc, req);
2594 mutex_unlock(&mdsc->mutex);
2595 goto out;
e55b71f8 2596 } else {
ca18bede
YZ
2597 int mds = __choose_mds(mdsc, req);
2598 if (mds >= 0 && mds != req->r_session->s_mds) {
4c069a58 2599 dout("but auth changed, so resending\n");
e55b71f8
GF
2600 __do_request(mdsc, req);
2601 mutex_unlock(&mdsc->mutex);
2602 goto out;
2603 }
2f2dc053 2604 }
4c069a58 2605 dout("have to return ESTALE on request %llu\n", req->r_tid);
2f2dc053
SW
2606 }
2607
e55b71f8 2608
2f2dc053 2609 if (head->safe) {
bc2de10d 2610 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
2f2dc053 2611 __unregister_request(mdsc, req);
2f2dc053 2612
bc2de10d 2613 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2f2dc053
SW
2614 /*
2615 * We already handled the unsafe response, now do the
2616 * cleanup. No need to examine the response; the MDS
2617 * doesn't include any result info in the safe
2618 * response. And even if it did, there is nothing
2619 * useful we could do with a revised return value.
2620 */
2621 dout("got safe reply %llu, mds%d\n", tid, mds);
2f2dc053
SW
2622
2623 /* last unsafe request during umount? */
44ca18f2 2624 if (mdsc->stopping && !__get_oldest_req(mdsc))
03066f23 2625 complete_all(&mdsc->safe_umount_waiters);
2f2dc053
SW
2626 mutex_unlock(&mdsc->mutex);
2627 goto out;
2628 }
e1518c7c 2629 } else {
bc2de10d 2630 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
2f2dc053 2631 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
4c06ace8
YZ
2632 if (req->r_unsafe_dir) {
2633 struct ceph_inode_info *ci =
2634 ceph_inode(req->r_unsafe_dir);
2635 spin_lock(&ci->i_unsafe_lock);
2636 list_add_tail(&req->r_unsafe_dir_item,
2637 &ci->i_unsafe_dirops);
2638 spin_unlock(&ci->i_unsafe_lock);
2639 }
2f2dc053
SW
2640 }
2641
2642 dout("handle_reply tid %lld result %d\n", tid, result);
2643 rinfo = &req->r_reply_info;
14303d20 2644 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2f2dc053
SW
2645 mutex_unlock(&mdsc->mutex);
2646
2647 mutex_lock(&session->s_mutex);
2648 if (err < 0) {
25933abd 2649 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
9ec7cab1 2650 ceph_msg_dump(msg);
2f2dc053
SW
2651 goto out_err;
2652 }
2653
2654 /* snap trace */
982d6011 2655 realm = NULL;
2f2dc053
SW
2656 if (rinfo->snapblob_len) {
2657 down_write(&mdsc->snap_rwsem);
2658 ceph_update_snap_trace(mdsc, rinfo->snapblob,
982d6011
YZ
2659 rinfo->snapblob + rinfo->snapblob_len,
2660 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2661 &realm);
2f2dc053
SW
2662 downgrade_write(&mdsc->snap_rwsem);
2663 } else {
2664 down_read(&mdsc->snap_rwsem);
2665 }
2666
2667 /* insert trace into our cache */
b4556396 2668 mutex_lock(&req->r_fill_mutex);
315f2408 2669 current->journal_info = req;
f5a03b08 2670 err = ceph_fill_trace(mdsc->fsc->sb, req);
2f2dc053 2671 if (err == 0) {
6e8575fa 2672 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
81c6aea5 2673 req->r_op == CEPH_MDS_OP_LSSNAP))
2f2dc053 2674 ceph_readdir_prepopulate(req, req->r_session);
37151668 2675 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2f2dc053 2676 }
315f2408 2677 current->journal_info = NULL;
b4556396 2678 mutex_unlock(&req->r_fill_mutex);
2f2dc053
SW
2679
2680 up_read(&mdsc->snap_rwsem);
982d6011
YZ
2681 if (realm)
2682 ceph_put_snap_realm(mdsc, realm);
68cd5b4b 2683
bc2de10d
JL
2684 if (err == 0 && req->r_target_inode &&
2685 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
68cd5b4b
YZ
2686 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
2687 spin_lock(&ci->i_unsafe_lock);
2688 list_add_tail(&req->r_unsafe_target_item, &ci->i_unsafe_iops);
2689 spin_unlock(&ci->i_unsafe_lock);
2690 }
2f2dc053 2691out_err:
e1518c7c 2692 mutex_lock(&mdsc->mutex);
bc2de10d 2693 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
e1518c7c
SW
2694 if (err) {
2695 req->r_err = err;
2696 } else {
5fdb1389 2697 req->r_reply = ceph_msg_get(msg);
bc2de10d 2698 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
e1518c7c 2699 }
2f2dc053 2700 } else {
e1518c7c 2701 dout("reply arrived after request %lld was aborted\n", tid);
2f2dc053 2702 }
e1518c7c 2703 mutex_unlock(&mdsc->mutex);
2f2dc053 2704
2f2dc053
SW
2705 mutex_unlock(&session->s_mutex);
2706
2707 /* kick calling process */
2708 complete_request(mdsc, req);
2709out:
2710 ceph_mdsc_put_request(req);
2711 return;
2712}
2713
2714
2715
2716/*
2717 * handle mds notification that our request has been forwarded.
2718 */
2600d2dd
SW
2719static void handle_forward(struct ceph_mds_client *mdsc,
2720 struct ceph_mds_session *session,
2721 struct ceph_msg *msg)
2f2dc053
SW
2722{
2723 struct ceph_mds_request *req;
a1ea787c 2724 u64 tid = le64_to_cpu(msg->hdr.tid);
2f2dc053
SW
2725 u32 next_mds;
2726 u32 fwd_seq;
2f2dc053
SW
2727 int err = -EINVAL;
2728 void *p = msg->front.iov_base;
2729 void *end = p + msg->front.iov_len;
2f2dc053 2730
a1ea787c 2731 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
2732 next_mds = ceph_decode_32(&p);
2733 fwd_seq = ceph_decode_32(&p);
2f2dc053
SW
2734
2735 mutex_lock(&mdsc->mutex);
fcd00b68 2736 req = lookup_get_request(mdsc, tid);
2f2dc053 2737 if (!req) {
2a8e5e36 2738 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2f2dc053
SW
2739 goto out; /* dup reply? */
2740 }
2741
bc2de10d 2742 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
2a8e5e36
SW
2743 dout("forward tid %llu aborted, unregistering\n", tid);
2744 __unregister_request(mdsc, req);
2745 } else if (fwd_seq <= req->r_num_fwd) {
2746 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2f2dc053
SW
2747 tid, next_mds, req->r_num_fwd, fwd_seq);
2748 } else {
2749 /* resend. forward race not possible; mds would drop */
2a8e5e36
SW
2750 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2751 BUG_ON(req->r_err);
bc2de10d 2752 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
3de22be6 2753 req->r_attempts = 0;
2f2dc053
SW
2754 req->r_num_fwd = fwd_seq;
2755 req->r_resend_mds = next_mds;
2756 put_request_session(req);
2757 __do_request(mdsc, req);
2758 }
2759 ceph_mdsc_put_request(req);
2760out:
2761 mutex_unlock(&mdsc->mutex);
2762 return;
2763
2764bad:
2765 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2766}
2767
2768/*
2769 * handle a mds session control message
2770 */
2771static void handle_session(struct ceph_mds_session *session,
2772 struct ceph_msg *msg)
2773{
2774 struct ceph_mds_client *mdsc = session->s_mdsc;
2775 u32 op;
2776 u64 seq;
2600d2dd 2777 int mds = session->s_mds;
2f2dc053
SW
2778 struct ceph_mds_session_head *h = msg->front.iov_base;
2779 int wake = 0;
2780
2f2dc053 2781 /* decode */
342ce182 2782 if (msg->front.iov_len < sizeof(*h))
2f2dc053
SW
2783 goto bad;
2784 op = le32_to_cpu(h->op);
2785 seq = le64_to_cpu(h->seq);
2786
2787 mutex_lock(&mdsc->mutex);
0a07fc8c
YZ
2788 if (op == CEPH_SESSION_CLOSE) {
2789 get_session(session);
2600d2dd 2790 __unregister_session(mdsc, session);
0a07fc8c 2791 }
2f2dc053
SW
2792 /* FIXME: this ttl calculation is generous */
2793 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2794 mutex_unlock(&mdsc->mutex);
2795
2796 mutex_lock(&session->s_mutex);
2797
2798 dout("handle_session mds%d %s %p state %s seq %llu\n",
2799 mds, ceph_session_op_name(op), session,
a687ecaf 2800 ceph_session_state_name(session->s_state), seq);
2f2dc053
SW
2801
2802 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2803 session->s_state = CEPH_MDS_SESSION_OPEN;
2804 pr_info("mds%d came back\n", session->s_mds);
2805 }
2806
2807 switch (op) {
2808 case CEPH_SESSION_OPEN:
29790f26
SW
2809 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2810 pr_info("mds%d reconnect success\n", session->s_mds);
2f2dc053
SW
2811 session->s_state = CEPH_MDS_SESSION_OPEN;
2812 renewed_caps(mdsc, session, 0);
2813 wake = 1;
2814 if (mdsc->stopping)
2815 __close_session(mdsc, session);
2816 break;
2817
2818 case CEPH_SESSION_RENEWCAPS:
2819 if (session->s_renew_seq == seq)
2820 renewed_caps(mdsc, session, 1);
2821 break;
2822
2823 case CEPH_SESSION_CLOSE:
29790f26
SW
2824 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2825 pr_info("mds%d reconnect denied\n", session->s_mds);
1c841a96 2826 cleanup_session_requests(mdsc, session);
2f2dc053 2827 remove_session_caps(session);
656e4382 2828 wake = 2; /* for good measure */
f3c60c59 2829 wake_up_all(&mdsc->session_close_wq);
2f2dc053
SW
2830 break;
2831
2832 case CEPH_SESSION_STALE:
2833 pr_info("mds%d caps went stale, renewing\n",
2834 session->s_mds);
d8fb02ab 2835 spin_lock(&session->s_gen_ttl_lock);
2f2dc053 2836 session->s_cap_gen++;
1ce208a6 2837 session->s_cap_ttl = jiffies - 1;
d8fb02ab 2838 spin_unlock(&session->s_gen_ttl_lock);
2f2dc053
SW
2839 send_renew_caps(mdsc, session);
2840 break;
2841
2842 case CEPH_SESSION_RECALL_STATE:
e30ee581 2843 ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2f2dc053
SW
2844 break;
2845
186e4f7a
YZ
2846 case CEPH_SESSION_FLUSHMSG:
2847 send_flushmsg_ack(mdsc, session, seq);
2848 break;
2849
03f4fcb0
YZ
2850 case CEPH_SESSION_FORCE_RO:
2851 dout("force_session_readonly %p\n", session);
2852 spin_lock(&session->s_cap_lock);
2853 session->s_readonly = true;
2854 spin_unlock(&session->s_cap_lock);
2855 wake_up_session_caps(session, 0);
2856 break;
2857
fcff415c
YZ
2858 case CEPH_SESSION_REJECT:
2859 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
2860 pr_info("mds%d rejected session\n", session->s_mds);
2861 session->s_state = CEPH_MDS_SESSION_REJECTED;
2862 cleanup_session_requests(mdsc, session);
2863 remove_session_caps(session);
2864 wake = 2; /* for good measure */
2865 break;
2866
2f2dc053
SW
2867 default:
2868 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2869 WARN_ON(1);
2870 }
2871
2872 mutex_unlock(&session->s_mutex);
2873 if (wake) {
2874 mutex_lock(&mdsc->mutex);
2875 __wake_requests(mdsc, &session->s_waiting);
656e4382
YZ
2876 if (wake == 2)
2877 kick_requests(mdsc, mds);
2f2dc053
SW
2878 mutex_unlock(&mdsc->mutex);
2879 }
0a07fc8c
YZ
2880 if (op == CEPH_SESSION_CLOSE)
2881 ceph_put_mds_session(session);
2f2dc053
SW
2882 return;
2883
2884bad:
2885 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2886 (int)msg->front.iov_len);
9ec7cab1 2887 ceph_msg_dump(msg);
2f2dc053
SW
2888 return;
2889}
2890
2891
2892/*
2893 * called under session->mutex.
2894 */
2895static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2896 struct ceph_mds_session *session)
2897{
2898 struct ceph_mds_request *req, *nreq;
3de22be6 2899 struct rb_node *p;
2f2dc053
SW
2900 int err;
2901
2902 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2903
2904 mutex_lock(&mdsc->mutex);
2905 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
6e6f0923 2906 err = __prepare_send_request(mdsc, req, session->s_mds, true);
2f2dc053
SW
2907 if (!err) {
2908 ceph_msg_get(req->r_request);
2909 ceph_con_send(&session->s_con, req->r_request);
2910 }
2911 }
3de22be6
YZ
2912
2913 /*
2914 * also re-send old requests when MDS enters reconnect stage. So that MDS
2915 * can process completed request in clientreplay stage.
2916 */
2917 p = rb_first(&mdsc->request_tree);
2918 while (p) {
2919 req = rb_entry(p, struct ceph_mds_request, r_node);
2920 p = rb_next(p);
bc2de10d 2921 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
3de22be6
YZ
2922 continue;
2923 if (req->r_attempts == 0)
2924 continue; /* only old requests */
2925 if (req->r_session &&
2926 req->r_session->s_mds == session->s_mds) {
6e6f0923
YZ
2927 err = __prepare_send_request(mdsc, req,
2928 session->s_mds, true);
3de22be6
YZ
2929 if (!err) {
2930 ceph_msg_get(req->r_request);
2931 ceph_con_send(&session->s_con, req->r_request);
2932 }
2933 }
2934 }
2f2dc053
SW
2935 mutex_unlock(&mdsc->mutex);
2936}
2937
2938/*
2939 * Encode information about a cap for a reconnect with the MDS.
2940 */
2f2dc053
SW
2941static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2942 void *arg)
2943{
20cb34ae
SW
2944 union {
2945 struct ceph_mds_cap_reconnect v2;
2946 struct ceph_mds_cap_reconnect_v1 v1;
2947 } rec;
b3f8d68f 2948 struct ceph_inode_info *ci = cap->ci;
20cb34ae
SW
2949 struct ceph_reconnect_state *recon_state = arg;
2950 struct ceph_pagelist *pagelist = recon_state->pagelist;
2f2dc053
SW
2951 char *path;
2952 int pathlen, err;
2953 u64 pathbase;
3469ed0d 2954 u64 snap_follows;
2f2dc053
SW
2955 struct dentry *dentry;
2956
2f2dc053
SW
2957 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2958 inode, ceph_vinop(inode), cap, cap->cap_id,
2959 ceph_cap_string(cap->issued));
93cea5be
SW
2960 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2961 if (err)
2962 return err;
2f2dc053
SW
2963
2964 dentry = d_find_alias(inode);
2965 if (dentry) {
2966 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2967 if (IS_ERR(path)) {
2968 err = PTR_ERR(path);
e072f8aa 2969 goto out_dput;
2f2dc053
SW
2970 }
2971 } else {
2972 path = NULL;
2973 pathlen = 0;
4eacd4cb 2974 pathbase = 0;
2f2dc053 2975 }
2f2dc053 2976
be655596 2977 spin_lock(&ci->i_ceph_lock);
2f2dc053
SW
2978 cap->seq = 0; /* reset cap seq */
2979 cap->issue_seq = 0; /* and issue_seq */
667ca05c 2980 cap->mseq = 0; /* and migrate_seq */
99a9c273 2981 cap->cap_gen = cap->session->s_cap_gen;
20cb34ae 2982
121f22a1 2983 if (recon_state->msg_version >= 2) {
20cb34ae
SW
2984 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2985 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2986 rec.v2.issued = cpu_to_le32(cap->issued);
2987 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2988 rec.v2.pathbase = cpu_to_le64(pathbase);
ec1dff25
JL
2989 rec.v2.flock_len = (__force __le32)
2990 ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
20cb34ae
SW
2991 } else {
2992 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2993 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2994 rec.v1.issued = cpu_to_le32(cap->issued);
2995 rec.v1.size = cpu_to_le64(inode->i_size);
9bbeab41
AB
2996 ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime);
2997 ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime);
20cb34ae
SW
2998 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2999 rec.v1.pathbase = cpu_to_le64(pathbase);
20cb34ae 3000 }
3469ed0d
YZ
3001
3002 if (list_empty(&ci->i_cap_snaps)) {
92776fd2 3003 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
3469ed0d
YZ
3004 } else {
3005 struct ceph_cap_snap *capsnap =
3006 list_first_entry(&ci->i_cap_snaps,
3007 struct ceph_cap_snap, ci_item);
3008 snap_follows = capsnap->follows;
20cb34ae 3009 }
be655596 3010 spin_unlock(&ci->i_ceph_lock);
2f2dc053 3011
121f22a1 3012 if (recon_state->msg_version >= 2) {
40819f6f 3013 int num_fcntl_locks, num_flock_locks;
4deb14a2 3014 struct ceph_filelock *flocks = NULL;
121f22a1
YZ
3015 size_t struct_len, total_len = 0;
3016 u8 struct_v = 0;
39be95e9
JS
3017
3018encode_again:
b3f8d68f
YZ
3019 if (rec.v2.flock_len) {
3020 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
3021 } else {
3022 num_fcntl_locks = 0;
3023 num_flock_locks = 0;
3024 }
4deb14a2 3025 if (num_fcntl_locks + num_flock_locks > 0) {
6da2ec56
KC
3026 flocks = kmalloc_array(num_fcntl_locks + num_flock_locks,
3027 sizeof(struct ceph_filelock),
3028 GFP_NOFS);
4deb14a2
YZ
3029 if (!flocks) {
3030 err = -ENOMEM;
3031 goto out_free;
3032 }
3033 err = ceph_encode_locks_to_buffer(inode, flocks,
3034 num_fcntl_locks,
3035 num_flock_locks);
3036 if (err) {
3037 kfree(flocks);
3038 flocks = NULL;
3039 if (err == -ENOSPC)
3040 goto encode_again;
3041 goto out_free;
3042 }
3043 } else {
39be95e9 3044 kfree(flocks);
4deb14a2 3045 flocks = NULL;
39be95e9 3046 }
121f22a1
YZ
3047
3048 if (recon_state->msg_version >= 3) {
3049 /* version, compat_version and struct_len */
3050 total_len = 2 * sizeof(u8) + sizeof(u32);
3469ed0d 3051 struct_v = 2;
121f22a1 3052 }
39be95e9
JS
3053 /*
3054 * number of encoded locks is stable, so copy to pagelist
3055 */
121f22a1
YZ
3056 struct_len = 2 * sizeof(u32) +
3057 (num_fcntl_locks + num_flock_locks) *
3058 sizeof(struct ceph_filelock);
3059 rec.v2.flock_len = cpu_to_le32(struct_len);
3060
3061 struct_len += sizeof(rec.v2);
3062 struct_len += sizeof(u32) + pathlen;
3063
3469ed0d
YZ
3064 if (struct_v >= 2)
3065 struct_len += sizeof(u64); /* snap_follows */
3066
121f22a1
YZ
3067 total_len += struct_len;
3068 err = ceph_pagelist_reserve(pagelist, total_len);
3069
3070 if (!err) {
3071 if (recon_state->msg_version >= 3) {
3072 ceph_pagelist_encode_8(pagelist, struct_v);
3073 ceph_pagelist_encode_8(pagelist, 1);
3074 ceph_pagelist_encode_32(pagelist, struct_len);
3075 }
3076 ceph_pagelist_encode_string(pagelist, path, pathlen);
3077 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
3078 ceph_locks_to_pagelist(flocks, pagelist,
3079 num_fcntl_locks,
3080 num_flock_locks);
3469ed0d
YZ
3081 if (struct_v >= 2)
3082 ceph_pagelist_encode_64(pagelist, snap_follows);
121f22a1 3083 }
39be95e9 3084 kfree(flocks);
3612abbd 3085 } else {
121f22a1
YZ
3086 size_t size = sizeof(u32) + pathlen + sizeof(rec.v1);
3087 err = ceph_pagelist_reserve(pagelist, size);
3088 if (!err) {
3089 ceph_pagelist_encode_string(pagelist, path, pathlen);
3090 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
3091 }
40819f6f 3092 }
44c99757
YZ
3093
3094 recon_state->nr_caps++;
e072f8aa 3095out_free:
2f2dc053 3096 kfree(path);
e072f8aa 3097out_dput:
2f2dc053 3098 dput(dentry);
93cea5be 3099 return err;
2f2dc053
SW
3100}
3101
3102
3103/*
3104 * If an MDS fails and recovers, clients need to reconnect in order to
3105 * reestablish shared state. This includes all caps issued through
3106 * this session _and_ the snap_realm hierarchy. Because it's not
3107 * clear which snap realms the mds cares about, we send everything we
3108 * know about.. that ensures we'll then get any new info the
3109 * recovering MDS might have.
3110 *
3111 * This is a relatively heavyweight operation, but it's rare.
3112 *
3113 * called with mdsc->mutex held.
3114 */
34b6c855
SW
3115static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3116 struct ceph_mds_session *session)
2f2dc053 3117{
2f2dc053 3118 struct ceph_msg *reply;
a105f00c 3119 struct rb_node *p;
34b6c855 3120 int mds = session->s_mds;
9abf82b8 3121 int err = -ENOMEM;
44c99757 3122 int s_nr_caps;
93cea5be 3123 struct ceph_pagelist *pagelist;
20cb34ae 3124 struct ceph_reconnect_state recon_state;
c8a96a31 3125 LIST_HEAD(dispose);
2f2dc053 3126
34b6c855 3127 pr_info("mds%d reconnect start\n", mds);
2f2dc053 3128
93cea5be
SW
3129 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
3130 if (!pagelist)
3131 goto fail_nopagelist;
3132 ceph_pagelist_init(pagelist);
3133
b61c2763 3134 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
a79832f2 3135 if (!reply)
93cea5be 3136 goto fail_nomsg;
93cea5be 3137
34b6c855
SW
3138 mutex_lock(&session->s_mutex);
3139 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3140 session->s_seq = 0;
2f2dc053 3141
2f2dc053 3142 dout("session %p state %s\n", session,
a687ecaf 3143 ceph_session_state_name(session->s_state));
2f2dc053 3144
99a9c273
YZ
3145 spin_lock(&session->s_gen_ttl_lock);
3146 session->s_cap_gen++;
3147 spin_unlock(&session->s_gen_ttl_lock);
3148
3149 spin_lock(&session->s_cap_lock);
03f4fcb0
YZ
3150 /* don't know if session is readonly */
3151 session->s_readonly = 0;
99a9c273
YZ
3152 /*
3153 * notify __ceph_remove_cap() that we are composing cap reconnect.
3154 * If a cap get released before being added to the cap reconnect,
3155 * __ceph_remove_cap() should skip queuing cap release.
3156 */
3157 session->s_cap_reconnect = 1;
e01a5946 3158 /* drop old cap expires; we're about to reestablish that state */
c8a96a31
JL
3159 detach_cap_releases(session, &dispose);
3160 spin_unlock(&session->s_cap_lock);
3161 dispose_cap_releases(mdsc, &dispose);
e01a5946 3162
5d23371f 3163 /* trim unused caps to reduce MDS's cache rejoin time */
c0bd50e2
YZ
3164 if (mdsc->fsc->sb->s_root)
3165 shrink_dcache_parent(mdsc->fsc->sb->s_root);
5d23371f
YZ
3166
3167 ceph_con_close(&session->s_con);
3168 ceph_con_open(&session->s_con,
3169 CEPH_ENTITY_TYPE_MDS, mds,
3170 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3171
3172 /* replay unsafe requests */
3173 replay_unsafe_requests(mdsc, session);
3174
3175 down_read(&mdsc->snap_rwsem);
3176
2f2dc053 3177 /* traverse this session's caps */
44c99757
YZ
3178 s_nr_caps = session->s_nr_caps;
3179 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
93cea5be
SW
3180 if (err)
3181 goto fail;
20cb34ae 3182
44c99757 3183 recon_state.nr_caps = 0;
20cb34ae 3184 recon_state.pagelist = pagelist;
121f22a1
YZ
3185 if (session->s_con.peer_features & CEPH_FEATURE_MDSENC)
3186 recon_state.msg_version = 3;
3187 else if (session->s_con.peer_features & CEPH_FEATURE_FLOCK)
3188 recon_state.msg_version = 2;
3189 else
3190 recon_state.msg_version = 1;
20cb34ae 3191 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
2f2dc053 3192 if (err < 0)
9abf82b8 3193 goto fail;
2f2dc053 3194
99a9c273
YZ
3195 spin_lock(&session->s_cap_lock);
3196 session->s_cap_reconnect = 0;
3197 spin_unlock(&session->s_cap_lock);
3198
2f2dc053
SW
3199 /*
3200 * snaprealms. we provide mds with the ino, seq (version), and
3201 * parent for all of our realms. If the mds has any newer info,
3202 * it will tell us.
3203 */
a105f00c
SW
3204 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3205 struct ceph_snap_realm *realm =
3206 rb_entry(p, struct ceph_snap_realm, node);
93cea5be 3207 struct ceph_mds_snaprealm_reconnect sr_rec;
2f2dc053
SW
3208
3209 dout(" adding snap realm %llx seq %lld parent %llx\n",
3210 realm->ino, realm->seq, realm->parent_ino);
93cea5be
SW
3211 sr_rec.ino = cpu_to_le64(realm->ino);
3212 sr_rec.seq = cpu_to_le64(realm->seq);
3213 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3214 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3215 if (err)
3216 goto fail;
2f2dc053 3217 }
2f2dc053 3218
121f22a1 3219 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
44c99757
YZ
3220
3221 /* raced with cap release? */
3222 if (s_nr_caps != recon_state.nr_caps) {
3223 struct page *page = list_first_entry(&pagelist->head,
3224 struct page, lru);
3225 __le32 *addr = kmap_atomic(page);
3226 *addr = cpu_to_le32(recon_state.nr_caps);
3227 kunmap_atomic(addr);
ebf18f47 3228 }
44c99757
YZ
3229
3230 reply->hdr.data_len = cpu_to_le32(pagelist->length);
3231 ceph_msg_data_add_pagelist(reply, pagelist);
e548e9b9
YZ
3232
3233 ceph_early_kick_flushing_caps(mdsc, session);
3234
2f2dc053
SW
3235 ceph_con_send(&session->s_con, reply);
3236
9abf82b8
SW
3237 mutex_unlock(&session->s_mutex);
3238
3239 mutex_lock(&mdsc->mutex);
3240 __wake_requests(mdsc, &session->s_waiting);
3241 mutex_unlock(&mdsc->mutex);
3242
2f2dc053 3243 up_read(&mdsc->snap_rwsem);
2f2dc053
SW
3244 return;
3245
93cea5be 3246fail:
2f2dc053 3247 ceph_msg_put(reply);
9abf82b8
SW
3248 up_read(&mdsc->snap_rwsem);
3249 mutex_unlock(&session->s_mutex);
93cea5be
SW
3250fail_nomsg:
3251 ceph_pagelist_release(pagelist);
93cea5be 3252fail_nopagelist:
9abf82b8 3253 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
9abf82b8 3254 return;
2f2dc053
SW
3255}
3256
3257
3258/*
3259 * compare old and new mdsmaps, kicking requests
3260 * and closing out old connections as necessary
3261 *
3262 * called under mdsc->mutex.
3263 */
3264static void check_new_map(struct ceph_mds_client *mdsc,
3265 struct ceph_mdsmap *newmap,
3266 struct ceph_mdsmap *oldmap)
3267{
3268 int i;
3269 int oldstate, newstate;
3270 struct ceph_mds_session *s;
3271
3272 dout("check_new_map new %u old %u\n",
3273 newmap->m_epoch, oldmap->m_epoch);
3274
76201b63 3275 for (i = 0; i < oldmap->m_num_mds && i < mdsc->max_sessions; i++) {
d37b1d99 3276 if (!mdsc->sessions[i])
2f2dc053
SW
3277 continue;
3278 s = mdsc->sessions[i];
3279 oldstate = ceph_mdsmap_get_state(oldmap, i);
3280 newstate = ceph_mdsmap_get_state(newmap, i);
3281
0deb01c9 3282 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2f2dc053 3283 i, ceph_mds_state_name(oldstate),
0deb01c9 3284 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
2f2dc053 3285 ceph_mds_state_name(newstate),
0deb01c9 3286 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
a687ecaf 3287 ceph_session_state_name(s->s_state));
2f2dc053 3288
76201b63 3289 if (i >= newmap->m_num_mds ||
3e8f43a0 3290 memcmp(ceph_mdsmap_get_addr(oldmap, i),
2f2dc053
SW
3291 ceph_mdsmap_get_addr(newmap, i),
3292 sizeof(struct ceph_entity_addr))) {
3293 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
3294 /* the session never opened, just close it
3295 * out now */
0a07fc8c 3296 get_session(s);
2600d2dd 3297 __unregister_session(mdsc, s);
2f2dc053 3298 __wake_requests(mdsc, &s->s_waiting);
0a07fc8c 3299 ceph_put_mds_session(s);
2827528d
YZ
3300 } else if (i >= newmap->m_num_mds) {
3301 /* force close session for stopped mds */
3302 get_session(s);
2600d2dd 3303 __unregister_session(mdsc, s);
2827528d
YZ
3304 __wake_requests(mdsc, &s->s_waiting);
3305 kick_requests(mdsc, i);
3306 mutex_unlock(&mdsc->mutex);
3307
3308 mutex_lock(&s->s_mutex);
3309 cleanup_session_requests(mdsc, s);
3310 remove_session_caps(s);
3311 mutex_unlock(&s->s_mutex);
3312
3313 ceph_put_mds_session(s);
3314
3315 mutex_lock(&mdsc->mutex);
2f2dc053
SW
3316 } else {
3317 /* just close it */
3318 mutex_unlock(&mdsc->mutex);
3319 mutex_lock(&s->s_mutex);
3320 mutex_lock(&mdsc->mutex);
3321 ceph_con_close(&s->s_con);
3322 mutex_unlock(&s->s_mutex);
3323 s->s_state = CEPH_MDS_SESSION_RESTARTING;
3324 }
2f2dc053
SW
3325 } else if (oldstate == newstate) {
3326 continue; /* nothing new with this mds */
3327 }
3328
3329 /*
3330 * send reconnect?
3331 */
3332 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
34b6c855
SW
3333 newstate >= CEPH_MDS_STATE_RECONNECT) {
3334 mutex_unlock(&mdsc->mutex);
3335 send_mds_reconnect(mdsc, s);
3336 mutex_lock(&mdsc->mutex);
3337 }
2f2dc053
SW
3338
3339 /*
29790f26 3340 * kick request on any mds that has gone active.
2f2dc053
SW
3341 */
3342 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3343 newstate >= CEPH_MDS_STATE_ACTIVE) {
29790f26
SW
3344 if (oldstate != CEPH_MDS_STATE_CREATING &&
3345 oldstate != CEPH_MDS_STATE_STARTING)
3346 pr_info("mds%d recovery completed\n", s->s_mds);
3347 kick_requests(mdsc, i);
2f2dc053 3348 ceph_kick_flushing_caps(mdsc, s);
0dc2570f 3349 wake_up_session_caps(s, 1);
2f2dc053
SW
3350 }
3351 }
cb170a22 3352
76201b63 3353 for (i = 0; i < newmap->m_num_mds && i < mdsc->max_sessions; i++) {
cb170a22
SW
3354 s = mdsc->sessions[i];
3355 if (!s)
3356 continue;
3357 if (!ceph_mdsmap_is_laggy(newmap, i))
3358 continue;
3359 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3360 s->s_state == CEPH_MDS_SESSION_HUNG ||
3361 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3362 dout(" connecting to export targets of laggy mds%d\n",
3363 i);
3364 __open_export_target_sessions(mdsc, s);
3365 }
3366 }
2f2dc053
SW
3367}
3368
3369
3370
3371/*
3372 * leases
3373 */
3374
3375/*
3376 * caller must hold session s_mutex, dentry->d_lock
3377 */
3378void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3379{
3380 struct ceph_dentry_info *di = ceph_dentry(dentry);
3381
3382 ceph_put_mds_session(di->lease_session);
3383 di->lease_session = NULL;
3384}
3385
2600d2dd
SW
3386static void handle_lease(struct ceph_mds_client *mdsc,
3387 struct ceph_mds_session *session,
3388 struct ceph_msg *msg)
2f2dc053 3389{
3d14c5d2 3390 struct super_block *sb = mdsc->fsc->sb;
2f2dc053 3391 struct inode *inode;
2f2dc053
SW
3392 struct dentry *parent, *dentry;
3393 struct ceph_dentry_info *di;
2600d2dd 3394 int mds = session->s_mds;
2f2dc053 3395 struct ceph_mds_lease *h = msg->front.iov_base;
1e5ea23d 3396 u32 seq;
2f2dc053 3397 struct ceph_vino vino;
2f2dc053
SW
3398 struct qstr dname;
3399 int release = 0;
3400
2f2dc053
SW
3401 dout("handle_lease from mds%d\n", mds);
3402
3403 /* decode */
3404 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3405 goto bad;
3406 vino.ino = le64_to_cpu(h->ino);
3407 vino.snap = CEPH_NOSNAP;
1e5ea23d 3408 seq = le32_to_cpu(h->seq);
0fcf6c02
YZ
3409 dname.len = get_unaligned_le32(h + 1);
3410 if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
2f2dc053 3411 goto bad;
0fcf6c02 3412 dname.name = (void *)(h + 1) + sizeof(u32);
2f2dc053 3413
2f2dc053
SW
3414 /* lookup inode */
3415 inode = ceph_find_inode(sb, vino);
2f90b852
SW
3416 dout("handle_lease %s, ino %llx %p %.*s\n",
3417 ceph_lease_op_name(h->action), vino.ino, inode,
1e5ea23d 3418 dname.len, dname.name);
6cd3bcad
YZ
3419
3420 mutex_lock(&session->s_mutex);
3421 session->s_seq++;
3422
d37b1d99 3423 if (!inode) {
2f2dc053
SW
3424 dout("handle_lease no inode %llx\n", vino.ino);
3425 goto release;
3426 }
2f2dc053
SW
3427
3428 /* dentry */
3429 parent = d_find_alias(inode);
3430 if (!parent) {
3431 dout("no parent dentry on inode %p\n", inode);
3432 WARN_ON(1);
3433 goto release; /* hrm... */
3434 }
8387ff25 3435 dname.hash = full_name_hash(parent, dname.name, dname.len);
2f2dc053
SW
3436 dentry = d_lookup(parent, &dname);
3437 dput(parent);
3438 if (!dentry)
3439 goto release;
3440
3441 spin_lock(&dentry->d_lock);
3442 di = ceph_dentry(dentry);
3443 switch (h->action) {
3444 case CEPH_MDS_LEASE_REVOKE:
3d8eb7a9 3445 if (di->lease_session == session) {
1e5ea23d
SW
3446 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3447 h->seq = cpu_to_le32(di->lease_seq);
2f2dc053
SW
3448 __ceph_mdsc_drop_dentry_lease(dentry);
3449 }
3450 release = 1;
3451 break;
3452
3453 case CEPH_MDS_LEASE_RENEW:
3d8eb7a9 3454 if (di->lease_session == session &&
2f2dc053
SW
3455 di->lease_gen == session->s_cap_gen &&
3456 di->lease_renew_from &&
3457 di->lease_renew_after == 0) {
3458 unsigned long duration =
3563dbdd 3459 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
2f2dc053 3460
1e5ea23d 3461 di->lease_seq = seq;
9b16f03c 3462 di->time = di->lease_renew_from + duration;
2f2dc053
SW
3463 di->lease_renew_after = di->lease_renew_from +
3464 (duration >> 1);
3465 di->lease_renew_from = 0;
3466 }
3467 break;
3468 }
3469 spin_unlock(&dentry->d_lock);
3470 dput(dentry);
3471
3472 if (!release)
3473 goto out;
3474
3475release:
3476 /* let's just reuse the same message */
3477 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3478 ceph_msg_get(msg);
3479 ceph_con_send(&session->s_con, msg);
3480
3481out:
3482 iput(inode);
3483 mutex_unlock(&session->s_mutex);
2f2dc053
SW
3484 return;
3485
3486bad:
3487 pr_err("corrupt lease message\n");
9ec7cab1 3488 ceph_msg_dump(msg);
2f2dc053
SW
3489}
3490
3491void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3492 struct inode *inode,
3493 struct dentry *dentry, char action,
3494 u32 seq)
3495{
3496 struct ceph_msg *msg;
3497 struct ceph_mds_lease *lease;
3498 int len = sizeof(*lease) + sizeof(u32);
3499 int dnamelen = 0;
3500
3501 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3502 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3503 dnamelen = dentry->d_name.len;
3504 len += dnamelen;
3505
b61c2763 3506 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
a79832f2 3507 if (!msg)
2f2dc053
SW
3508 return;
3509 lease = msg->front.iov_base;
3510 lease->action = action;
2f2dc053
SW
3511 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3512 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3513 lease->seq = cpu_to_le32(seq);
3514 put_unaligned_le32(dnamelen, lease + 1);
3515 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3516
3517 /*
3518 * if this is a preemptive lease RELEASE, no need to
3519 * flush request stream, since the actual request will
3520 * soon follow.
3521 */
3522 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3523
3524 ceph_con_send(&session->s_con, msg);
3525}
3526
2f2dc053 3527/*
7aac453a 3528 * lock unlock sessions, to wait ongoing session activities
2f2dc053 3529 */
7aac453a 3530static void lock_unlock_sessions(struct ceph_mds_client *mdsc)
2f2dc053
SW
3531{
3532 int i;
3533
2f2dc053
SW
3534 mutex_lock(&mdsc->mutex);
3535 for (i = 0; i < mdsc->max_sessions; i++) {
3536 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3537 if (!s)
3538 continue;
3539 mutex_unlock(&mdsc->mutex);
3540 mutex_lock(&s->s_mutex);
3541 mutex_unlock(&s->s_mutex);
3542 ceph_put_mds_session(s);
3543 mutex_lock(&mdsc->mutex);
3544 }
3545 mutex_unlock(&mdsc->mutex);
3546}
3547
3548
3549
3550/*
3551 * delayed work -- periodically trim expired leases, renew caps with mds
3552 */
3553static void schedule_delayed(struct ceph_mds_client *mdsc)
3554{
3555 int delay = 5;
3556 unsigned hz = round_jiffies_relative(HZ * delay);
3557 schedule_delayed_work(&mdsc->delayed_work, hz);
3558}
3559
3560static void delayed_work(struct work_struct *work)
3561{
3562 int i;
3563 struct ceph_mds_client *mdsc =
3564 container_of(work, struct ceph_mds_client, delayed_work.work);
3565 int renew_interval;
3566 int renew_caps;
3567
3568 dout("mdsc delayed_work\n");
afcdaea3 3569 ceph_check_delayed_caps(mdsc);
2f2dc053
SW
3570
3571 mutex_lock(&mdsc->mutex);
3572 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3573 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3574 mdsc->last_renew_caps);
3575 if (renew_caps)
3576 mdsc->last_renew_caps = jiffies;
3577
3578 for (i = 0; i < mdsc->max_sessions; i++) {
3579 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
d37b1d99 3580 if (!s)
2f2dc053
SW
3581 continue;
3582 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3583 dout("resending session close request for mds%d\n",
3584 s->s_mds);
3585 request_close_session(mdsc, s);
3586 ceph_put_mds_session(s);
3587 continue;
3588 }
3589 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3590 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3591 s->s_state = CEPH_MDS_SESSION_HUNG;
3592 pr_info("mds%d hung\n", s->s_mds);
3593 }
3594 }
3595 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3596 /* this mds is failed or recovering, just wait */
3597 ceph_put_mds_session(s);
3598 continue;
3599 }
3600 mutex_unlock(&mdsc->mutex);
3601
3602 mutex_lock(&s->s_mutex);
3603 if (renew_caps)
3604 send_renew_caps(mdsc, s);
3605 else
3606 ceph_con_keepalive(&s->s_con);
aab53dd9
SW
3607 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3608 s->s_state == CEPH_MDS_SESSION_HUNG)
3d7ded4d 3609 ceph_send_cap_releases(mdsc, s);
2f2dc053
SW
3610 mutex_unlock(&s->s_mutex);
3611 ceph_put_mds_session(s);
3612
3613 mutex_lock(&mdsc->mutex);
3614 }
3615 mutex_unlock(&mdsc->mutex);
3616
3617 schedule_delayed(mdsc);
3618}
3619
3d14c5d2 3620int ceph_mdsc_init(struct ceph_fs_client *fsc)
2f2dc053 3621
2f2dc053 3622{
3d14c5d2
YS
3623 struct ceph_mds_client *mdsc;
3624
3625 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3626 if (!mdsc)
3627 return -ENOMEM;
3628 mdsc->fsc = fsc;
2f2dc053
SW
3629 mutex_init(&mdsc->mutex);
3630 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
d37b1d99 3631 if (!mdsc->mdsmap) {
fb3101b6 3632 kfree(mdsc);
2d06eeb8 3633 return -ENOMEM;
fb3101b6 3634 }
2d06eeb8 3635
50c55aec 3636 fsc->mdsc = mdsc;
2f2dc053 3637 init_completion(&mdsc->safe_umount_waiters);
f3c60c59 3638 init_waitqueue_head(&mdsc->session_close_wq);
2f2dc053
SW
3639 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3640 mdsc->sessions = NULL;
86d8f67b 3641 atomic_set(&mdsc->num_sessions, 0);
2f2dc053
SW
3642 mdsc->max_sessions = 0;
3643 mdsc->stopping = 0;
d557c48d 3644 atomic64_set(&mdsc->quotarealms_count, 0);
affbc19a 3645 mdsc->last_snap_seq = 0;
2f2dc053 3646 init_rwsem(&mdsc->snap_rwsem);
a105f00c 3647 mdsc->snap_realms = RB_ROOT;
2f2dc053
SW
3648 INIT_LIST_HEAD(&mdsc->snap_empty);
3649 spin_lock_init(&mdsc->snap_empty_lock);
3650 mdsc->last_tid = 0;
e8a7b8b1 3651 mdsc->oldest_tid = 0;
44ca18f2 3652 mdsc->request_tree = RB_ROOT;
2f2dc053
SW
3653 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3654 mdsc->last_renew_caps = jiffies;
3655 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3656 spin_lock_init(&mdsc->cap_delay_lock);
3657 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3658 spin_lock_init(&mdsc->snap_flush_lock);
553adfd9 3659 mdsc->last_cap_flush_tid = 1;
e4500b5e 3660 INIT_LIST_HEAD(&mdsc->cap_flush_list);
2f2dc053 3661 INIT_LIST_HEAD(&mdsc->cap_dirty);
db354052 3662 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
2f2dc053
SW
3663 mdsc->num_cap_flushing = 0;
3664 spin_lock_init(&mdsc->cap_dirty_lock);
3665 init_waitqueue_head(&mdsc->cap_flushing_wq);
3666 spin_lock_init(&mdsc->dentry_lru_lock);
3667 INIT_LIST_HEAD(&mdsc->dentry_lru);
2d06eeb8 3668
37151668 3669 ceph_caps_init(mdsc);
3d14c5d2 3670 ceph_adjust_min_caps(mdsc, fsc->min_caps);
37151668 3671
10183a69
YZ
3672 init_rwsem(&mdsc->pool_perm_rwsem);
3673 mdsc->pool_perm_tree = RB_ROOT;
3674
dfeb84d4
YZ
3675 strscpy(mdsc->nodename, utsname()->nodename,
3676 sizeof(mdsc->nodename));
5f44f142 3677 return 0;
2f2dc053
SW
3678}
3679
3680/*
3681 * Wait for safe replies on open mds requests. If we time out, drop
3682 * all requests from the tree to avoid dangling dentry refs.
3683 */
3684static void wait_requests(struct ceph_mds_client *mdsc)
3685{
a319bf56 3686 struct ceph_options *opts = mdsc->fsc->client->options;
2f2dc053 3687 struct ceph_mds_request *req;
2f2dc053
SW
3688
3689 mutex_lock(&mdsc->mutex);
44ca18f2 3690 if (__get_oldest_req(mdsc)) {
2f2dc053 3691 mutex_unlock(&mdsc->mutex);
44ca18f2 3692
2f2dc053
SW
3693 dout("wait_requests waiting for requests\n");
3694 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
a319bf56 3695 ceph_timeout_jiffies(opts->mount_timeout));
2f2dc053
SW
3696
3697 /* tear down remaining requests */
44ca18f2
SW
3698 mutex_lock(&mdsc->mutex);
3699 while ((req = __get_oldest_req(mdsc))) {
2f2dc053
SW
3700 dout("wait_requests timed out on tid %llu\n",
3701 req->r_tid);
44ca18f2 3702 __unregister_request(mdsc, req);
2f2dc053
SW
3703 }
3704 }
3705 mutex_unlock(&mdsc->mutex);
3706 dout("wait_requests done\n");
3707}
3708
3709/*
3710 * called before mount is ro, and before dentries are torn down.
3711 * (hmm, does this still race with new lookups?)
3712 */
3713void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3714{
3715 dout("pre_umount\n");
3716 mdsc->stopping = 1;
3717
7aac453a 3718 lock_unlock_sessions(mdsc);
afcdaea3 3719 ceph_flush_dirty_caps(mdsc);
2f2dc053 3720 wait_requests(mdsc);
17c688c3
SW
3721
3722 /*
3723 * wait for reply handlers to drop their request refs and
3724 * their inode/dcache refs
3725 */
3726 ceph_msgr_flush();
2f2dc053
SW
3727}
3728
3729/*
3730 * wait for all write mds requests to flush.
3731 */
3732static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3733{
80fc7314 3734 struct ceph_mds_request *req = NULL, *nextreq;
44ca18f2 3735 struct rb_node *n;
2f2dc053
SW
3736
3737 mutex_lock(&mdsc->mutex);
3738 dout("wait_unsafe_requests want %lld\n", want_tid);
80fc7314 3739restart:
44ca18f2
SW
3740 req = __get_oldest_req(mdsc);
3741 while (req && req->r_tid <= want_tid) {
80fc7314
SW
3742 /* find next request */
3743 n = rb_next(&req->r_node);
3744 if (n)
3745 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3746 else
3747 nextreq = NULL;
e8a7b8b1
YZ
3748 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
3749 (req->r_op & CEPH_MDS_OP_WRITE)) {
44ca18f2
SW
3750 /* write op */
3751 ceph_mdsc_get_request(req);
80fc7314
SW
3752 if (nextreq)
3753 ceph_mdsc_get_request(nextreq);
44ca18f2
SW
3754 mutex_unlock(&mdsc->mutex);
3755 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3756 req->r_tid, want_tid);
3757 wait_for_completion(&req->r_safe_completion);
3758 mutex_lock(&mdsc->mutex);
44ca18f2 3759 ceph_mdsc_put_request(req);
80fc7314
SW
3760 if (!nextreq)
3761 break; /* next dne before, so we're done! */
3762 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3763 /* next request was removed from tree */
3764 ceph_mdsc_put_request(nextreq);
3765 goto restart;
3766 }
3767 ceph_mdsc_put_request(nextreq); /* won't go away */
44ca18f2 3768 }
80fc7314 3769 req = nextreq;
2f2dc053
SW
3770 }
3771 mutex_unlock(&mdsc->mutex);
3772 dout("wait_unsafe_requests done\n");
3773}
3774
3775void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3776{
0e294387 3777 u64 want_tid, want_flush;
2f2dc053 3778
52953d55 3779 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
56b7cf95
SW
3780 return;
3781
2f2dc053
SW
3782 dout("sync\n");
3783 mutex_lock(&mdsc->mutex);
3784 want_tid = mdsc->last_tid;
2f2dc053 3785 mutex_unlock(&mdsc->mutex);
2f2dc053 3786
afcdaea3 3787 ceph_flush_dirty_caps(mdsc);
d3383a8e 3788 spin_lock(&mdsc->cap_dirty_lock);
8310b089 3789 want_flush = mdsc->last_cap_flush_tid;
c8799fc4
YZ
3790 if (!list_empty(&mdsc->cap_flush_list)) {
3791 struct ceph_cap_flush *cf =
3792 list_last_entry(&mdsc->cap_flush_list,
3793 struct ceph_cap_flush, g_list);
3794 cf->wake = true;
3795 }
d3383a8e
YZ
3796 spin_unlock(&mdsc->cap_dirty_lock);
3797
0e294387
YZ
3798 dout("sync want tid %lld flush_seq %lld\n",
3799 want_tid, want_flush);
2f2dc053
SW
3800
3801 wait_unsafe_requests(mdsc, want_tid);
0e294387 3802 wait_caps_flush(mdsc, want_flush);
2f2dc053
SW
3803}
3804
f3c60c59
SW
3805/*
3806 * true if all sessions are closed, or we force unmount
3807 */
fcff415c 3808static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
f3c60c59 3809{
52953d55 3810 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
f3c60c59 3811 return true;
fcff415c 3812 return atomic_read(&mdsc->num_sessions) <= skipped;
f3c60c59 3813}
2f2dc053
SW
3814
3815/*
3816 * called after sb is ro.
3817 */
3818void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3819{
a319bf56 3820 struct ceph_options *opts = mdsc->fsc->client->options;
2f2dc053
SW
3821 struct ceph_mds_session *session;
3822 int i;
fcff415c 3823 int skipped = 0;
2f2dc053
SW
3824
3825 dout("close_sessions\n");
3826
2f2dc053 3827 /* close sessions */
f3c60c59
SW
3828 mutex_lock(&mdsc->mutex);
3829 for (i = 0; i < mdsc->max_sessions; i++) {
3830 session = __ceph_lookup_mds_session(mdsc, i);
3831 if (!session)
3832 continue;
2f2dc053 3833 mutex_unlock(&mdsc->mutex);
f3c60c59 3834 mutex_lock(&session->s_mutex);
fcff415c
YZ
3835 if (__close_session(mdsc, session) <= 0)
3836 skipped++;
f3c60c59
SW
3837 mutex_unlock(&session->s_mutex);
3838 ceph_put_mds_session(session);
2f2dc053
SW
3839 mutex_lock(&mdsc->mutex);
3840 }
f3c60c59
SW
3841 mutex_unlock(&mdsc->mutex);
3842
3843 dout("waiting for sessions to close\n");
fcff415c
YZ
3844 wait_event_timeout(mdsc->session_close_wq,
3845 done_closing_sessions(mdsc, skipped),
a319bf56 3846 ceph_timeout_jiffies(opts->mount_timeout));
2f2dc053
SW
3847
3848 /* tear down remaining sessions */
f3c60c59 3849 mutex_lock(&mdsc->mutex);
2f2dc053
SW
3850 for (i = 0; i < mdsc->max_sessions; i++) {
3851 if (mdsc->sessions[i]) {
3852 session = get_session(mdsc->sessions[i]);
2600d2dd 3853 __unregister_session(mdsc, session);
2f2dc053
SW
3854 mutex_unlock(&mdsc->mutex);
3855 mutex_lock(&session->s_mutex);
3856 remove_session_caps(session);
3857 mutex_unlock(&session->s_mutex);
3858 ceph_put_mds_session(session);
3859 mutex_lock(&mdsc->mutex);
3860 }
3861 }
2f2dc053 3862 WARN_ON(!list_empty(&mdsc->cap_delay_list));
2f2dc053
SW
3863 mutex_unlock(&mdsc->mutex);
3864
3865 ceph_cleanup_empty_realms(mdsc);
3866
3867 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3868
3869 dout("stopped\n");
3870}
3871
48fec5d0
YZ
3872void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
3873{
3874 struct ceph_mds_session *session;
3875 int mds;
3876
3877 dout("force umount\n");
3878
3879 mutex_lock(&mdsc->mutex);
3880 for (mds = 0; mds < mdsc->max_sessions; mds++) {
3881 session = __ceph_lookup_mds_session(mdsc, mds);
3882 if (!session)
3883 continue;
3884 mutex_unlock(&mdsc->mutex);
3885 mutex_lock(&session->s_mutex);
3886 __close_session(mdsc, session);
3887 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
3888 cleanup_session_requests(mdsc, session);
3889 remove_session_caps(session);
3890 }
3891 mutex_unlock(&session->s_mutex);
3892 ceph_put_mds_session(session);
3893 mutex_lock(&mdsc->mutex);
3894 kick_requests(mdsc, mds);
3895 }
3896 __wake_requests(mdsc, &mdsc->waiting_for_map);
3897 mutex_unlock(&mdsc->mutex);
3898}
3899
3d14c5d2 3900static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
2f2dc053
SW
3901{
3902 dout("stop\n");
3903 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3904 if (mdsc->mdsmap)
3905 ceph_mdsmap_destroy(mdsc->mdsmap);
3906 kfree(mdsc->sessions);
37151668 3907 ceph_caps_finalize(mdsc);
10183a69 3908 ceph_pool_perm_destroy(mdsc);
2f2dc053
SW
3909}
3910
3d14c5d2
YS
3911void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3912{
3913 struct ceph_mds_client *mdsc = fsc->mdsc;
ef550f6f 3914 dout("mdsc_destroy %p\n", mdsc);
ef550f6f 3915
50c55aec
CX
3916 if (!mdsc)
3917 return;
3918
ef550f6f
SW
3919 /* flush out any connection work with references to us */
3920 ceph_msgr_flush();
3921
62a65f36
YZ
3922 ceph_mdsc_stop(mdsc);
3923
3d14c5d2
YS
3924 fsc->mdsc = NULL;
3925 kfree(mdsc);
ef550f6f 3926 dout("mdsc_destroy %p done\n", mdsc);
3d14c5d2
YS
3927}
3928
430afbad
YZ
3929void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3930{
3931 struct ceph_fs_client *fsc = mdsc->fsc;
3932 const char *mds_namespace = fsc->mount_options->mds_namespace;
3933 void *p = msg->front.iov_base;
3934 void *end = p + msg->front.iov_len;
3935 u32 epoch;
3936 u32 map_len;
3937 u32 num_fs;
3938 u32 mount_fscid = (u32)-1;
3939 u8 struct_v, struct_cv;
3940 int err = -EINVAL;
3941
3942 ceph_decode_need(&p, end, sizeof(u32), bad);
3943 epoch = ceph_decode_32(&p);
3944
3945 dout("handle_fsmap epoch %u\n", epoch);
3946
3947 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
3948 struct_v = ceph_decode_8(&p);
3949 struct_cv = ceph_decode_8(&p);
3950 map_len = ceph_decode_32(&p);
3951
3952 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
3953 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
3954
3955 num_fs = ceph_decode_32(&p);
3956 while (num_fs-- > 0) {
3957 void *info_p, *info_end;
3958 u32 info_len;
3959 u8 info_v, info_cv;
3960 u32 fscid, namelen;
3961
3962 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
3963 info_v = ceph_decode_8(&p);
3964 info_cv = ceph_decode_8(&p);
3965 info_len = ceph_decode_32(&p);
3966 ceph_decode_need(&p, end, info_len, bad);
3967 info_p = p;
3968 info_end = p + info_len;
3969 p = info_end;
3970
3971 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
3972 fscid = ceph_decode_32(&info_p);
3973 namelen = ceph_decode_32(&info_p);
3974 ceph_decode_need(&info_p, info_end, namelen, bad);
3975
3976 if (mds_namespace &&
3977 strlen(mds_namespace) == namelen &&
3978 !strncmp(mds_namespace, (char *)info_p, namelen)) {
3979 mount_fscid = fscid;
3980 break;
3981 }
3982 }
3983
3984 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
3985 if (mount_fscid != (u32)-1) {
3986 fsc->client->monc.fs_cluster_id = mount_fscid;
3987 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
3988 0, true);
3989 ceph_monc_renew_subs(&fsc->client->monc);
3990 } else {
3991 err = -ENOENT;
3992 goto err_out;
3993 }
3994 return;
76bd6ec4 3995
430afbad
YZ
3996bad:
3997 pr_err("error decoding fsmap\n");
3998err_out:
3999 mutex_lock(&mdsc->mutex);
76bd6ec4 4000 mdsc->mdsmap_err = err;
430afbad
YZ
4001 __wake_requests(mdsc, &mdsc->waiting_for_map);
4002 mutex_unlock(&mdsc->mutex);
430afbad 4003}
2f2dc053
SW
4004
4005/*
4006 * handle mds map update.
4007 */
430afbad 4008void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
2f2dc053
SW
4009{
4010 u32 epoch;
4011 u32 maplen;
4012 void *p = msg->front.iov_base;
4013 void *end = p + msg->front.iov_len;
4014 struct ceph_mdsmap *newmap, *oldmap;
4015 struct ceph_fsid fsid;
4016 int err = -EINVAL;
4017
4018 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
4019 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3d14c5d2 4020 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
0743304d 4021 return;
c89136ea
SW
4022 epoch = ceph_decode_32(&p);
4023 maplen = ceph_decode_32(&p);
2f2dc053
SW
4024 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
4025
4026 /* do we need it? */
2f2dc053
SW
4027 mutex_lock(&mdsc->mutex);
4028 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
4029 dout("handle_map epoch %u <= our %u\n",
4030 epoch, mdsc->mdsmap->m_epoch);
4031 mutex_unlock(&mdsc->mutex);
4032 return;
4033 }
4034
4035 newmap = ceph_mdsmap_decode(&p, end);
4036 if (IS_ERR(newmap)) {
4037 err = PTR_ERR(newmap);
4038 goto bad_unlock;
4039 }
4040
4041 /* swap into place */
4042 if (mdsc->mdsmap) {
4043 oldmap = mdsc->mdsmap;
4044 mdsc->mdsmap = newmap;
4045 check_new_map(mdsc, newmap, oldmap);
4046 ceph_mdsmap_destroy(oldmap);
4047 } else {
4048 mdsc->mdsmap = newmap; /* first mds map */
4049 }
719784ba
CX
4050 mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
4051 MAX_LFS_FILESIZE);
2f2dc053
SW
4052
4053 __wake_requests(mdsc, &mdsc->waiting_for_map);
82dcabad
ID
4054 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
4055 mdsc->mdsmap->m_epoch);
2f2dc053
SW
4056
4057 mutex_unlock(&mdsc->mutex);
4058 schedule_delayed(mdsc);
4059 return;
4060
4061bad_unlock:
4062 mutex_unlock(&mdsc->mutex);
4063bad:
4064 pr_err("error decoding mdsmap %d\n", err);
4065 return;
4066}
4067
4068static struct ceph_connection *con_get(struct ceph_connection *con)
4069{
4070 struct ceph_mds_session *s = con->private;
4071
4072 if (get_session(s)) {
3997c01d 4073 dout("mdsc con_get %p ok (%d)\n", s, refcount_read(&s->s_ref));
2f2dc053
SW
4074 return con;
4075 }
4076 dout("mdsc con_get %p FAIL\n", s);
4077 return NULL;
4078}
4079
4080static void con_put(struct ceph_connection *con)
4081{
4082 struct ceph_mds_session *s = con->private;
4083
3997c01d 4084 dout("mdsc con_put %p (%d)\n", s, refcount_read(&s->s_ref) - 1);
2f2dc053
SW
4085 ceph_put_mds_session(s);
4086}
4087
4088/*
4089 * if the client is unresponsive for long enough, the mds will kill
4090 * the session entirely.
4091 */
4092static void peer_reset(struct ceph_connection *con)
4093{
4094 struct ceph_mds_session *s = con->private;
7e70f0ed 4095 struct ceph_mds_client *mdsc = s->s_mdsc;
2f2dc053 4096
f3ae1b97 4097 pr_warn("mds%d closed our session\n", s->s_mds);
7e70f0ed 4098 send_mds_reconnect(mdsc, s);
2f2dc053
SW
4099}
4100
4101static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4102{
4103 struct ceph_mds_session *s = con->private;
4104 struct ceph_mds_client *mdsc = s->s_mdsc;
4105 int type = le16_to_cpu(msg->hdr.type);
4106
2600d2dd
SW
4107 mutex_lock(&mdsc->mutex);
4108 if (__verify_registered_session(mdsc, s) < 0) {
4109 mutex_unlock(&mdsc->mutex);
4110 goto out;
4111 }
4112 mutex_unlock(&mdsc->mutex);
4113
2f2dc053
SW
4114 switch (type) {
4115 case CEPH_MSG_MDS_MAP:
430afbad
YZ
4116 ceph_mdsc_handle_mdsmap(mdsc, msg);
4117 break;
4118 case CEPH_MSG_FS_MAP_USER:
4119 ceph_mdsc_handle_fsmap(mdsc, msg);
2f2dc053
SW
4120 break;
4121 case CEPH_MSG_CLIENT_SESSION:
4122 handle_session(s, msg);
4123 break;
4124 case CEPH_MSG_CLIENT_REPLY:
4125 handle_reply(s, msg);
4126 break;
4127 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
2600d2dd 4128 handle_forward(mdsc, s, msg);
2f2dc053
SW
4129 break;
4130 case CEPH_MSG_CLIENT_CAPS:
4131 ceph_handle_caps(s, msg);
4132 break;
4133 case CEPH_MSG_CLIENT_SNAP:
2600d2dd 4134 ceph_handle_snap(mdsc, s, msg);
2f2dc053
SW
4135 break;
4136 case CEPH_MSG_CLIENT_LEASE:
2600d2dd 4137 handle_lease(mdsc, s, msg);
2f2dc053 4138 break;
fb18a575
LH
4139 case CEPH_MSG_CLIENT_QUOTA:
4140 ceph_handle_quota(mdsc, s, msg);
4141 break;
2f2dc053
SW
4142
4143 default:
4144 pr_err("received unknown message type %d %s\n", type,
4145 ceph_msg_type_name(type));
4146 }
2600d2dd 4147out:
2f2dc053
SW
4148 ceph_msg_put(msg);
4149}
4150
4e7a5dcd
SW
4151/*
4152 * authentication
4153 */
a3530df3
AE
4154
4155/*
4156 * Note: returned pointer is the address of a structure that's
4157 * managed separately. Caller must *not* attempt to free it.
4158 */
4159static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
8f43fb53 4160 int *proto, int force_new)
4e7a5dcd
SW
4161{
4162 struct ceph_mds_session *s = con->private;
4163 struct ceph_mds_client *mdsc = s->s_mdsc;
3d14c5d2 4164 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
74f1869f 4165 struct ceph_auth_handshake *auth = &s->s_auth;
4e7a5dcd 4166
74f1869f 4167 if (force_new && auth->authorizer) {
6c1ea260 4168 ceph_auth_destroy_authorizer(auth->authorizer);
74f1869f 4169 auth->authorizer = NULL;
4e7a5dcd 4170 }
27859f97
SW
4171 if (!auth->authorizer) {
4172 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4173 auth);
0bed9b5c
SW
4174 if (ret)
4175 return ERR_PTR(ret);
27859f97
SW
4176 } else {
4177 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4178 auth);
a255651d 4179 if (ret)
a3530df3 4180 return ERR_PTR(ret);
4e7a5dcd 4181 }
4e7a5dcd 4182 *proto = ac->protocol;
74f1869f 4183
a3530df3 4184 return auth;
4e7a5dcd
SW
4185}
4186
6daca13d
ID
4187static int add_authorizer_challenge(struct ceph_connection *con,
4188 void *challenge_buf, int challenge_buf_len)
4189{
4190 struct ceph_mds_session *s = con->private;
4191 struct ceph_mds_client *mdsc = s->s_mdsc;
4192 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4193
4194 return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
4195 challenge_buf, challenge_buf_len);
4196}
4e7a5dcd 4197
0dde5848 4198static int verify_authorizer_reply(struct ceph_connection *con)
4e7a5dcd
SW
4199{
4200 struct ceph_mds_session *s = con->private;
4201 struct ceph_mds_client *mdsc = s->s_mdsc;
3d14c5d2 4202 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4e7a5dcd 4203
0dde5848 4204 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
4e7a5dcd
SW
4205}
4206
9bd2e6f8
SW
4207static int invalidate_authorizer(struct ceph_connection *con)
4208{
4209 struct ceph_mds_session *s = con->private;
4210 struct ceph_mds_client *mdsc = s->s_mdsc;
3d14c5d2 4211 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
9bd2e6f8 4212
27859f97 4213 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
9bd2e6f8 4214
3d14c5d2 4215 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
9bd2e6f8
SW
4216}
4217
53ded495
AE
4218static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
4219 struct ceph_msg_header *hdr, int *skip)
4220{
4221 struct ceph_msg *msg;
4222 int type = (int) le16_to_cpu(hdr->type);
4223 int front_len = (int) le32_to_cpu(hdr->front_len);
4224
4225 if (con->in_msg)
4226 return con->in_msg;
4227
4228 *skip = 0;
4229 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
4230 if (!msg) {
4231 pr_err("unable to allocate msg type %d len %d\n",
4232 type, front_len);
4233 return NULL;
4234 }
53ded495
AE
4235
4236 return msg;
4237}
4238
79dbd1ba 4239static int mds_sign_message(struct ceph_msg *msg)
33d07337 4240{
79dbd1ba 4241 struct ceph_mds_session *s = msg->con->private;
33d07337 4242 struct ceph_auth_handshake *auth = &s->s_auth;
79dbd1ba 4243
33d07337
YZ
4244 return ceph_auth_sign_message(auth, msg);
4245}
4246
79dbd1ba 4247static int mds_check_message_signature(struct ceph_msg *msg)
33d07337 4248{
79dbd1ba 4249 struct ceph_mds_session *s = msg->con->private;
33d07337 4250 struct ceph_auth_handshake *auth = &s->s_auth;
79dbd1ba 4251
33d07337
YZ
4252 return ceph_auth_check_message_signature(auth, msg);
4253}
4254
9e32789f 4255static const struct ceph_connection_operations mds_con_ops = {
2f2dc053
SW
4256 .get = con_get,
4257 .put = con_put,
4258 .dispatch = dispatch,
4e7a5dcd 4259 .get_authorizer = get_authorizer,
6daca13d 4260 .add_authorizer_challenge = add_authorizer_challenge,
4e7a5dcd 4261 .verify_authorizer_reply = verify_authorizer_reply,
9bd2e6f8 4262 .invalidate_authorizer = invalidate_authorizer,
2f2dc053 4263 .peer_reset = peer_reset,
53ded495 4264 .alloc_msg = mds_alloc_msg,
79dbd1ba
ID
4265 .sign_message = mds_sign_message,
4266 .check_message_signature = mds_check_message_signature,
2f2dc053
SW
4267};
4268
2f2dc053 4269/* eof */