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