rbd: get rid of rbd_mapping::read_only
[linux-2.6-block.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
745a8e3b
YZ
1042/* caller holds s_cap_lock, we drop it */
1043static void cleanup_cap_releases(struct ceph_mds_client *mdsc,
1044 struct ceph_mds_session *session)
1045 __releases(session->s_cap_lock)
2f2dc053 1046{
745a8e3b
YZ
1047 LIST_HEAD(tmp_list);
1048 list_splice_init(&session->s_cap_releases, &tmp_list);
1049 session->s_num_cap_releases = 0;
1050 spin_unlock(&session->s_cap_lock);
2f2dc053 1051
745a8e3b
YZ
1052 dout("cleanup_cap_releases mds%d\n", session->s_mds);
1053 while (!list_empty(&tmp_list)) {
1054 struct ceph_cap *cap;
1055 /* zero out the in-progress message */
1056 cap = list_first_entry(&tmp_list,
1057 struct ceph_cap, session_caps);
1058 list_del(&cap->session_caps);
1059 ceph_put_cap(mdsc, cap);
2f2dc053 1060 }
2f2dc053
SW
1061}
1062
1c841a96
YZ
1063static void cleanup_session_requests(struct ceph_mds_client *mdsc,
1064 struct ceph_mds_session *session)
1065{
1066 struct ceph_mds_request *req;
1067 struct rb_node *p;
1068
1069 dout("cleanup_session_requests mds%d\n", session->s_mds);
1070 mutex_lock(&mdsc->mutex);
1071 while (!list_empty(&session->s_unsafe)) {
1072 req = list_first_entry(&session->s_unsafe,
1073 struct ceph_mds_request, r_unsafe_item);
3e0708b9
YZ
1074 pr_warn_ratelimited(" dropping unsafe request %llu\n",
1075 req->r_tid);
1c841a96
YZ
1076 __unregister_request(mdsc, req);
1077 }
1078 /* zero r_attempts, so kick_requests() will re-send requests */
1079 p = rb_first(&mdsc->request_tree);
1080 while (p) {
1081 req = rb_entry(p, struct ceph_mds_request, r_node);
1082 p = rb_next(p);
1083 if (req->r_session &&
1084 req->r_session->s_mds == session->s_mds)
1085 req->r_attempts = 0;
1086 }
1087 mutex_unlock(&mdsc->mutex);
1088}
1089
2f2dc053 1090/*
f818a736
SW
1091 * Helper to safely iterate over all caps associated with a session, with
1092 * special care taken to handle a racing __ceph_remove_cap().
2f2dc053 1093 *
f818a736 1094 * Caller must hold session s_mutex.
2f2dc053
SW
1095 */
1096static int iterate_session_caps(struct ceph_mds_session *session,
1097 int (*cb)(struct inode *, struct ceph_cap *,
1098 void *), void *arg)
1099{
7c1332b8
SW
1100 struct list_head *p;
1101 struct ceph_cap *cap;
1102 struct inode *inode, *last_inode = NULL;
1103 struct ceph_cap *old_cap = NULL;
2f2dc053
SW
1104 int ret;
1105
1106 dout("iterate_session_caps %p mds%d\n", session, session->s_mds);
1107 spin_lock(&session->s_cap_lock);
7c1332b8
SW
1108 p = session->s_caps.next;
1109 while (p != &session->s_caps) {
1110 cap = list_entry(p, struct ceph_cap, session_caps);
2f2dc053 1111 inode = igrab(&cap->ci->vfs_inode);
7c1332b8
SW
1112 if (!inode) {
1113 p = p->next;
2f2dc053 1114 continue;
7c1332b8
SW
1115 }
1116 session->s_cap_iterator = cap;
2f2dc053 1117 spin_unlock(&session->s_cap_lock);
7c1332b8
SW
1118
1119 if (last_inode) {
1120 iput(last_inode);
1121 last_inode = NULL;
1122 }
1123 if (old_cap) {
37151668 1124 ceph_put_cap(session->s_mdsc, old_cap);
7c1332b8
SW
1125 old_cap = NULL;
1126 }
1127
2f2dc053 1128 ret = cb(inode, cap, arg);
7c1332b8
SW
1129 last_inode = inode;
1130
2f2dc053 1131 spin_lock(&session->s_cap_lock);
7c1332b8 1132 p = p->next;
d37b1d99 1133 if (!cap->ci) {
7c1332b8
SW
1134 dout("iterate_session_caps finishing cap %p removal\n",
1135 cap);
1136 BUG_ON(cap->session != session);
745a8e3b 1137 cap->session = NULL;
7c1332b8
SW
1138 list_del_init(&cap->session_caps);
1139 session->s_nr_caps--;
745a8e3b
YZ
1140 if (cap->queue_release) {
1141 list_add_tail(&cap->session_caps,
1142 &session->s_cap_releases);
1143 session->s_num_cap_releases++;
1144 } else {
1145 old_cap = cap; /* put_cap it w/o locks held */
1146 }
7c1332b8 1147 }
5dacf091
SW
1148 if (ret < 0)
1149 goto out;
2f2dc053 1150 }
5dacf091
SW
1151 ret = 0;
1152out:
7c1332b8 1153 session->s_cap_iterator = NULL;
2f2dc053 1154 spin_unlock(&session->s_cap_lock);
7c1332b8 1155
e96a650a 1156 iput(last_inode);
7c1332b8 1157 if (old_cap)
37151668 1158 ceph_put_cap(session->s_mdsc, old_cap);
7c1332b8 1159
5dacf091 1160 return ret;
2f2dc053
SW
1161}
1162
1163static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap,
6c99f254 1164 void *arg)
2f2dc053 1165{
6c93df5d 1166 struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg;
2f2dc053 1167 struct ceph_inode_info *ci = ceph_inode(inode);
553adfd9 1168 LIST_HEAD(to_remove);
6c93df5d
YZ
1169 bool drop = false;
1170 bool invalidate = false;
6c99f254 1171
2f2dc053
SW
1172 dout("removing cap %p, ci is %p, inode is %p\n",
1173 cap, ci, &ci->vfs_inode);
be655596 1174 spin_lock(&ci->i_ceph_lock);
a096b09a 1175 __ceph_remove_cap(cap, false);
571ade33 1176 if (!ci->i_auth_cap) {
553adfd9 1177 struct ceph_cap_flush *cf;
6c93df5d 1178 struct ceph_mds_client *mdsc = fsc->mdsc;
6c99f254 1179
77310320
YZ
1180 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
1181
6c93df5d 1182 if (ci->i_wrbuffer_ref > 0 &&
52953d55 1183 READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
6c93df5d
YZ
1184 invalidate = true;
1185
e4500b5e
YZ
1186 while (!list_empty(&ci->i_cap_flush_list)) {
1187 cf = list_first_entry(&ci->i_cap_flush_list,
1188 struct ceph_cap_flush, i_list);
8cdcc07d 1189 list_move(&cf->i_list, &to_remove);
553adfd9
YZ
1190 }
1191
6c99f254 1192 spin_lock(&mdsc->cap_dirty_lock);
8310b089 1193
e4500b5e
YZ
1194 list_for_each_entry(cf, &to_remove, i_list)
1195 list_del(&cf->g_list);
8310b089 1196
6c99f254 1197 if (!list_empty(&ci->i_dirty_item)) {
3e0708b9
YZ
1198 pr_warn_ratelimited(
1199 " dropping dirty %s state for %p %lld\n",
6c99f254
SW
1200 ceph_cap_string(ci->i_dirty_caps),
1201 inode, ceph_ino(inode));
1202 ci->i_dirty_caps = 0;
1203 list_del_init(&ci->i_dirty_item);
6c93df5d 1204 drop = true;
6c99f254
SW
1205 }
1206 if (!list_empty(&ci->i_flushing_item)) {
3e0708b9
YZ
1207 pr_warn_ratelimited(
1208 " dropping dirty+flushing %s state for %p %lld\n",
6c99f254
SW
1209 ceph_cap_string(ci->i_flushing_caps),
1210 inode, ceph_ino(inode));
1211 ci->i_flushing_caps = 0;
1212 list_del_init(&ci->i_flushing_item);
1213 mdsc->num_cap_flushing--;
6c93df5d 1214 drop = true;
6c99f254 1215 }
6c99f254 1216 spin_unlock(&mdsc->cap_dirty_lock);
553adfd9 1217
b3f8d68f
YZ
1218 if (atomic_read(&ci->i_filelock_ref) > 0) {
1219 /* make further file lock syscall return -EIO */
1220 ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK;
1221 pr_warn_ratelimited(" dropping file locks for %p %lld\n",
1222 inode, ceph_ino(inode));
1223 }
1224
f66fd9f0 1225 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
e4500b5e 1226 list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove);
f66fd9f0
YZ
1227 ci->i_prealloc_cap_flush = NULL;
1228 }
6c99f254 1229 }
be655596 1230 spin_unlock(&ci->i_ceph_lock);
553adfd9
YZ
1231 while (!list_empty(&to_remove)) {
1232 struct ceph_cap_flush *cf;
1233 cf = list_first_entry(&to_remove,
e4500b5e
YZ
1234 struct ceph_cap_flush, i_list);
1235 list_del(&cf->i_list);
f66fd9f0 1236 ceph_free_cap_flush(cf);
553adfd9 1237 }
77310320
YZ
1238
1239 wake_up_all(&ci->i_cap_wq);
6c93df5d
YZ
1240 if (invalidate)
1241 ceph_queue_invalidate(inode);
77310320 1242 if (drop)
6c99f254 1243 iput(inode);
2f2dc053
SW
1244 return 0;
1245}
1246
1247/*
1248 * caller must hold session s_mutex
1249 */
1250static void remove_session_caps(struct ceph_mds_session *session)
1251{
6c93df5d
YZ
1252 struct ceph_fs_client *fsc = session->s_mdsc->fsc;
1253 struct super_block *sb = fsc->sb;
2f2dc053 1254 dout("remove_session_caps on %p\n", session);
6c93df5d 1255 iterate_session_caps(session, remove_session_caps_cb, fsc);
6f60f889 1256
c8799fc4
YZ
1257 wake_up_all(&fsc->mdsc->cap_flushing_wq);
1258
6f60f889
YZ
1259 spin_lock(&session->s_cap_lock);
1260 if (session->s_nr_caps > 0) {
6f60f889
YZ
1261 struct inode *inode;
1262 struct ceph_cap *cap, *prev = NULL;
1263 struct ceph_vino vino;
1264 /*
1265 * iterate_session_caps() skips inodes that are being
1266 * deleted, we need to wait until deletions are complete.
1267 * __wait_on_freeing_inode() is designed for the job,
1268 * but it is not exported, so use lookup inode function
1269 * to access it.
1270 */
1271 while (!list_empty(&session->s_caps)) {
1272 cap = list_entry(session->s_caps.next,
1273 struct ceph_cap, session_caps);
1274 if (cap == prev)
1275 break;
1276 prev = cap;
1277 vino = cap->ci->i_vino;
1278 spin_unlock(&session->s_cap_lock);
1279
ed284c49 1280 inode = ceph_find_inode(sb, vino);
6f60f889
YZ
1281 iput(inode);
1282
1283 spin_lock(&session->s_cap_lock);
1284 }
1285 }
745a8e3b
YZ
1286
1287 // drop cap expires and unlock s_cap_lock
1288 cleanup_cap_releases(session->s_mdsc, session);
6f60f889 1289
2f2dc053 1290 BUG_ON(session->s_nr_caps > 0);
6c99f254 1291 BUG_ON(!list_empty(&session->s_cap_flushing));
2f2dc053
SW
1292}
1293
1294/*
1295 * wake up any threads waiting on this session's caps. if the cap is
1296 * old (didn't get renewed on the client reconnect), remove it now.
1297 *
1298 * caller must hold s_mutex.
1299 */
1300static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap,
1301 void *arg)
1302{
0dc2570f
SW
1303 struct ceph_inode_info *ci = ceph_inode(inode);
1304
0dc2570f 1305 if (arg) {
be655596 1306 spin_lock(&ci->i_ceph_lock);
0dc2570f
SW
1307 ci->i_wanted_max_size = 0;
1308 ci->i_requested_max_size = 0;
be655596 1309 spin_unlock(&ci->i_ceph_lock);
0dc2570f 1310 }
e5360309 1311 wake_up_all(&ci->i_cap_wq);
2f2dc053
SW
1312 return 0;
1313}
1314
0dc2570f
SW
1315static void wake_up_session_caps(struct ceph_mds_session *session,
1316 int reconnect)
2f2dc053
SW
1317{
1318 dout("wake_up_session_caps %p mds%d\n", session, session->s_mds);
0dc2570f
SW
1319 iterate_session_caps(session, wake_up_session_cb,
1320 (void *)(unsigned long)reconnect);
2f2dc053
SW
1321}
1322
1323/*
1324 * Send periodic message to MDS renewing all currently held caps. The
1325 * ack will reset the expiration for all caps from this session.
1326 *
1327 * caller holds s_mutex
1328 */
1329static int send_renew_caps(struct ceph_mds_client *mdsc,
1330 struct ceph_mds_session *session)
1331{
1332 struct ceph_msg *msg;
1333 int state;
1334
1335 if (time_after_eq(jiffies, session->s_cap_ttl) &&
1336 time_after_eq(session->s_cap_ttl, session->s_renew_requested))
1337 pr_info("mds%d caps stale\n", session->s_mds);
e4cb4cb8 1338 session->s_renew_requested = jiffies;
2f2dc053
SW
1339
1340 /* do not try to renew caps until a recovering mds has reconnected
1341 * with its clients. */
1342 state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
1343 if (state < CEPH_MDS_STATE_RECONNECT) {
1344 dout("send_renew_caps ignoring mds%d (%s)\n",
1345 session->s_mds, ceph_mds_state_name(state));
1346 return 0;
1347 }
1348
1349 dout("send_renew_caps to mds%d (%s)\n", session->s_mds,
1350 ceph_mds_state_name(state));
2f2dc053
SW
1351 msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS,
1352 ++session->s_renew_seq);
a79832f2
SW
1353 if (!msg)
1354 return -ENOMEM;
2f2dc053
SW
1355 ceph_con_send(&session->s_con, msg);
1356 return 0;
1357}
1358
186e4f7a
YZ
1359static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
1360 struct ceph_mds_session *session, u64 seq)
1361{
1362 struct ceph_msg *msg;
1363
1364 dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n",
a687ecaf 1365 session->s_mds, ceph_session_state_name(session->s_state), seq);
186e4f7a
YZ
1366 msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
1367 if (!msg)
1368 return -ENOMEM;
1369 ceph_con_send(&session->s_con, msg);
1370 return 0;
1371}
1372
1373
2f2dc053
SW
1374/*
1375 * Note new cap ttl, and any transition from stale -> not stale (fresh?).
0dc2570f
SW
1376 *
1377 * Called under session->s_mutex
2f2dc053
SW
1378 */
1379static void renewed_caps(struct ceph_mds_client *mdsc,
1380 struct ceph_mds_session *session, int is_renew)
1381{
1382 int was_stale;
1383 int wake = 0;
1384
1385 spin_lock(&session->s_cap_lock);
1ce208a6 1386 was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
2f2dc053
SW
1387
1388 session->s_cap_ttl = session->s_renew_requested +
1389 mdsc->mdsmap->m_session_timeout*HZ;
1390
1391 if (was_stale) {
1392 if (time_before(jiffies, session->s_cap_ttl)) {
1393 pr_info("mds%d caps renewed\n", session->s_mds);
1394 wake = 1;
1395 } else {
1396 pr_info("mds%d caps still stale\n", session->s_mds);
1397 }
1398 }
1399 dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n",
1400 session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh",
1401 time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
1402 spin_unlock(&session->s_cap_lock);
1403
1404 if (wake)
0dc2570f 1405 wake_up_session_caps(session, 0);
2f2dc053
SW
1406}
1407
1408/*
1409 * send a session close request
1410 */
1411static int request_close_session(struct ceph_mds_client *mdsc,
1412 struct ceph_mds_session *session)
1413{
1414 struct ceph_msg *msg;
2f2dc053
SW
1415
1416 dout("request_close_session mds%d state %s seq %lld\n",
a687ecaf 1417 session->s_mds, ceph_session_state_name(session->s_state),
2f2dc053
SW
1418 session->s_seq);
1419 msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq);
a79832f2
SW
1420 if (!msg)
1421 return -ENOMEM;
1422 ceph_con_send(&session->s_con, msg);
fcff415c 1423 return 1;
2f2dc053
SW
1424}
1425
1426/*
1427 * Called with s_mutex held.
1428 */
1429static int __close_session(struct ceph_mds_client *mdsc,
1430 struct ceph_mds_session *session)
1431{
1432 if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
1433 return 0;
1434 session->s_state = CEPH_MDS_SESSION_CLOSING;
1435 return request_close_session(mdsc, session);
1436}
1437
1438/*
1439 * Trim old(er) caps.
1440 *
1441 * Because we can't cache an inode without one or more caps, we do
1442 * this indirectly: if a cap is unused, we prune its aliases, at which
1443 * point the inode will hopefully get dropped to.
1444 *
1445 * Yes, this is a bit sloppy. Our only real goal here is to respond to
1446 * memory pressure from the MDS, though, so it needn't be perfect.
1447 */
1448static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg)
1449{
1450 struct ceph_mds_session *session = arg;
1451 struct ceph_inode_info *ci = ceph_inode(inode);
979abfdd 1452 int used, wanted, oissued, mine;
2f2dc053
SW
1453
1454 if (session->s_trim_caps <= 0)
1455 return -1;
1456
be655596 1457 spin_lock(&ci->i_ceph_lock);
2f2dc053
SW
1458 mine = cap->issued | cap->implemented;
1459 used = __ceph_caps_used(ci);
979abfdd 1460 wanted = __ceph_caps_file_wanted(ci);
2f2dc053
SW
1461 oissued = __ceph_caps_issued_other(ci, cap);
1462
979abfdd 1463 dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n",
2f2dc053 1464 inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued),
979abfdd
YZ
1465 ceph_cap_string(used), ceph_cap_string(wanted));
1466 if (cap == ci->i_auth_cap) {
622f3e25
YZ
1467 if (ci->i_dirty_caps || ci->i_flushing_caps ||
1468 !list_empty(&ci->i_cap_snaps))
979abfdd
YZ
1469 goto out;
1470 if ((used | wanted) & CEPH_CAP_ANY_WR)
1471 goto out;
89aa5930
YZ
1472 /* Note: it's possible that i_filelock_ref becomes non-zero
1473 * after dropping auth caps. It doesn't hurt because reply
1474 * of lock mds request will re-add auth caps. */
1475 if (atomic_read(&ci->i_filelock_ref) > 0)
1476 goto out;
979abfdd 1477 }
5e804ac4
YZ
1478 /* The inode has cached pages, but it's no longer used.
1479 * we can safely drop it */
1480 if (wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
1481 !(oissued & CEPH_CAP_FILE_CACHE)) {
1482 used = 0;
1483 oissued = 0;
1484 }
979abfdd 1485 if ((used | wanted) & ~oissued & mine)
2f2dc053
SW
1486 goto out; /* we need these caps */
1487
1488 session->s_trim_caps--;
1489 if (oissued) {
1490 /* we aren't the only cap.. just remove us */
a096b09a 1491 __ceph_remove_cap(cap, true);
2f2dc053 1492 } else {
5e804ac4 1493 /* try dropping referring dentries */
be655596 1494 spin_unlock(&ci->i_ceph_lock);
2f2dc053
SW
1495 d_prune_aliases(inode);
1496 dout("trim_caps_cb %p cap %p pruned, count now %d\n",
1497 inode, cap, atomic_read(&inode->i_count));
1498 return 0;
1499 }
1500
1501out:
be655596 1502 spin_unlock(&ci->i_ceph_lock);
2f2dc053
SW
1503 return 0;
1504}
1505
1506/*
1507 * Trim session cap count down to some max number.
1508 */
1509static int trim_caps(struct ceph_mds_client *mdsc,
1510 struct ceph_mds_session *session,
1511 int max_caps)
1512{
1513 int trim_caps = session->s_nr_caps - max_caps;
1514
1515 dout("trim_caps mds%d start: %d / %d, trim %d\n",
1516 session->s_mds, session->s_nr_caps, max_caps, trim_caps);
1517 if (trim_caps > 0) {
1518 session->s_trim_caps = trim_caps;
1519 iterate_session_caps(session, trim_caps_cb, session);
1520 dout("trim_caps mds%d done: %d / %d, trimmed %d\n",
1521 session->s_mds, session->s_nr_caps, max_caps,
1522 trim_caps - session->s_trim_caps);
5dacf091 1523 session->s_trim_caps = 0;
2f2dc053 1524 }
a56371d9 1525
a56371d9 1526 ceph_send_cap_releases(mdsc, session);
2f2dc053
SW
1527 return 0;
1528}
1529
8310b089
YZ
1530static int check_caps_flush(struct ceph_mds_client *mdsc,
1531 u64 want_flush_tid)
1532{
8310b089
YZ
1533 int ret = 1;
1534
1535 spin_lock(&mdsc->cap_dirty_lock);
e4500b5e
YZ
1536 if (!list_empty(&mdsc->cap_flush_list)) {
1537 struct ceph_cap_flush *cf =
1538 list_first_entry(&mdsc->cap_flush_list,
1539 struct ceph_cap_flush, g_list);
1540 if (cf->tid <= want_flush_tid) {
1541 dout("check_caps_flush still flushing tid "
1542 "%llu <= %llu\n", cf->tid, want_flush_tid);
1543 ret = 0;
1544 }
8310b089
YZ
1545 }
1546 spin_unlock(&mdsc->cap_dirty_lock);
1547 return ret;
d3383a8e
YZ
1548}
1549
2f2dc053
SW
1550/*
1551 * flush all dirty inode data to disk.
1552 *
8310b089 1553 * returns true if we've flushed through want_flush_tid
2f2dc053 1554 */
affbc19a 1555static void wait_caps_flush(struct ceph_mds_client *mdsc,
0e294387 1556 u64 want_flush_tid)
2f2dc053 1557{
0e294387 1558 dout("check_caps_flush want %llu\n", want_flush_tid);
8310b089
YZ
1559
1560 wait_event(mdsc->cap_flushing_wq,
1561 check_caps_flush(mdsc, want_flush_tid));
1562
1563 dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid);
2f2dc053
SW
1564}
1565
1566/*
1567 * called under s_mutex
1568 */
3d7ded4d
SW
1569void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
1570 struct ceph_mds_session *session)
2f2dc053 1571{
745a8e3b
YZ
1572 struct ceph_msg *msg = NULL;
1573 struct ceph_mds_cap_release *head;
1574 struct ceph_mds_cap_item *item;
92475f05 1575 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
745a8e3b
YZ
1576 struct ceph_cap *cap;
1577 LIST_HEAD(tmp_list);
1578 int num_cap_releases;
92475f05
JL
1579 __le32 barrier, *cap_barrier;
1580
1581 down_read(&osdc->lock);
1582 barrier = cpu_to_le32(osdc->epoch_barrier);
1583 up_read(&osdc->lock);
2f2dc053 1584
0f8605f2 1585 spin_lock(&session->s_cap_lock);
745a8e3b
YZ
1586again:
1587 list_splice_init(&session->s_cap_releases, &tmp_list);
1588 num_cap_releases = session->s_num_cap_releases;
1589 session->s_num_cap_releases = 0;
2f2dc053 1590 spin_unlock(&session->s_cap_lock);
e01a5946 1591
745a8e3b
YZ
1592 while (!list_empty(&tmp_list)) {
1593 if (!msg) {
1594 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
09cbfeaf 1595 PAGE_SIZE, GFP_NOFS, false);
745a8e3b
YZ
1596 if (!msg)
1597 goto out_err;
1598 head = msg->front.iov_base;
1599 head->num = cpu_to_le32(0);
1600 msg->front.iov_len = sizeof(*head);
92475f05
JL
1601
1602 msg->hdr.version = cpu_to_le16(2);
1603 msg->hdr.compat_version = cpu_to_le16(1);
745a8e3b 1604 }
92475f05 1605
745a8e3b
YZ
1606 cap = list_first_entry(&tmp_list, struct ceph_cap,
1607 session_caps);
1608 list_del(&cap->session_caps);
1609 num_cap_releases--;
e01a5946 1610
00bd8edb 1611 head = msg->front.iov_base;
745a8e3b
YZ
1612 le32_add_cpu(&head->num, 1);
1613 item = msg->front.iov_base + msg->front.iov_len;
1614 item->ino = cpu_to_le64(cap->cap_ino);
1615 item->cap_id = cpu_to_le64(cap->cap_id);
1616 item->migrate_seq = cpu_to_le32(cap->mseq);
1617 item->seq = cpu_to_le32(cap->issue_seq);
1618 msg->front.iov_len += sizeof(*item);
1619
1620 ceph_put_cap(mdsc, cap);
1621
1622 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
92475f05
JL
1623 // Append cap_barrier field
1624 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1625 *cap_barrier = barrier;
1626 msg->front.iov_len += sizeof(*cap_barrier);
1627
745a8e3b
YZ
1628 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1629 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1630 ceph_con_send(&session->s_con, msg);
1631 msg = NULL;
1632 }
00bd8edb 1633 }
e01a5946 1634
745a8e3b 1635 BUG_ON(num_cap_releases != 0);
e01a5946 1636
745a8e3b
YZ
1637 spin_lock(&session->s_cap_lock);
1638 if (!list_empty(&session->s_cap_releases))
1639 goto again;
1640 spin_unlock(&session->s_cap_lock);
1641
1642 if (msg) {
92475f05
JL
1643 // Append cap_barrier field
1644 cap_barrier = msg->front.iov_base + msg->front.iov_len;
1645 *cap_barrier = barrier;
1646 msg->front.iov_len += sizeof(*cap_barrier);
1647
745a8e3b
YZ
1648 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1649 dout("send_cap_releases mds%d %p\n", session->s_mds, msg);
1650 ceph_con_send(&session->s_con, msg);
e01a5946 1651 }
745a8e3b
YZ
1652 return;
1653out_err:
1654 pr_err("send_cap_releases mds%d, failed to allocate message\n",
1655 session->s_mds);
1656 spin_lock(&session->s_cap_lock);
1657 list_splice(&tmp_list, &session->s_cap_releases);
1658 session->s_num_cap_releases += num_cap_releases;
1659 spin_unlock(&session->s_cap_lock);
e01a5946
SW
1660}
1661
2f2dc053
SW
1662/*
1663 * requests
1664 */
1665
54008399
YZ
1666int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
1667 struct inode *dir)
1668{
1669 struct ceph_inode_info *ci = ceph_inode(dir);
1670 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1671 struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
2a5beea3 1672 size_t size = sizeof(struct ceph_mds_reply_dir_entry);
54008399
YZ
1673 int order, num_entries;
1674
1675 spin_lock(&ci->i_ceph_lock);
1676 num_entries = ci->i_files + ci->i_subdirs;
1677 spin_unlock(&ci->i_ceph_lock);
1678 num_entries = max(num_entries, 1);
1679 num_entries = min(num_entries, opt->max_readdir);
1680
1681 order = get_order(size * num_entries);
1682 while (order >= 0) {
2a5beea3
YZ
1683 rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
1684 __GFP_NOWARN,
1685 order);
1686 if (rinfo->dir_entries)
54008399
YZ
1687 break;
1688 order--;
1689 }
2a5beea3 1690 if (!rinfo->dir_entries)
54008399
YZ
1691 return -ENOMEM;
1692
1693 num_entries = (PAGE_SIZE << order) / size;
1694 num_entries = min(num_entries, opt->max_readdir);
1695
1696 rinfo->dir_buf_size = PAGE_SIZE << order;
1697 req->r_num_caps = num_entries + 1;
1698 req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
1699 req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
1700 return 0;
1701}
1702
2f2dc053
SW
1703/*
1704 * Create an mds request.
1705 */
1706struct ceph_mds_request *
1707ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
1708{
1709 struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS);
1710
1711 if (!req)
1712 return ERR_PTR(-ENOMEM);
1713
b4556396 1714 mutex_init(&req->r_fill_mutex);
37151668 1715 req->r_mdsc = mdsc;
2f2dc053
SW
1716 req->r_started = jiffies;
1717 req->r_resend_mds = -1;
1718 INIT_LIST_HEAD(&req->r_unsafe_dir_item);
68cd5b4b 1719 INIT_LIST_HEAD(&req->r_unsafe_target_item);
2f2dc053 1720 req->r_fmode = -1;
153c8e6b 1721 kref_init(&req->r_kref);
fcd00b68 1722 RB_CLEAR_NODE(&req->r_node);
2f2dc053
SW
1723 INIT_LIST_HEAD(&req->r_wait);
1724 init_completion(&req->r_completion);
1725 init_completion(&req->r_safe_completion);
1726 INIT_LIST_HEAD(&req->r_unsafe_item);
1727
56199016 1728 req->r_stamp = timespec_trunc(current_kernel_time(), mdsc->fsc->sb->s_time_gran);
b8e69066 1729
2f2dc053
SW
1730 req->r_op = op;
1731 req->r_direct_mode = mode;
1732 return req;
1733}
1734
1735/*
44ca18f2 1736 * return oldest (lowest) request, tid in request tree, 0 if none.
2f2dc053
SW
1737 *
1738 * called under mdsc->mutex.
1739 */
44ca18f2
SW
1740static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
1741{
1742 if (RB_EMPTY_ROOT(&mdsc->request_tree))
1743 return NULL;
1744 return rb_entry(rb_first(&mdsc->request_tree),
1745 struct ceph_mds_request, r_node);
1746}
1747
e8a7b8b1 1748static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
2f2dc053 1749{
e8a7b8b1 1750 return mdsc->oldest_tid;
2f2dc053
SW
1751}
1752
1753/*
1754 * Build a dentry's path. Allocate on heap; caller must kfree. Based
1755 * on build_path_from_dentry in fs/cifs/dir.c.
1756 *
1757 * If @stop_on_nosnap, generate path relative to the first non-snapped
1758 * inode.
1759 *
1760 * Encode hidden .snap dirs as a double /, i.e.
1761 * foo/.snap/bar -> foo//bar
1762 */
1763char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base,
1764 int stop_on_nosnap)
1765{
1766 struct dentry *temp;
1767 char *path;
1768 int len, pos;
1b71fe2e 1769 unsigned seq;
2f2dc053 1770
d37b1d99 1771 if (!dentry)
2f2dc053
SW
1772 return ERR_PTR(-EINVAL);
1773
1774retry:
1775 len = 0;
1b71fe2e
AV
1776 seq = read_seqbegin(&rename_lock);
1777 rcu_read_lock();
2f2dc053 1778 for (temp = dentry; !IS_ROOT(temp);) {
2b0143b5 1779 struct inode *inode = d_inode(temp);
2f2dc053
SW
1780 if (inode && ceph_snap(inode) == CEPH_SNAPDIR)
1781 len++; /* slash only */
1782 else if (stop_on_nosnap && inode &&
1783 ceph_snap(inode) == CEPH_NOSNAP)
1784 break;
1785 else
1786 len += 1 + temp->d_name.len;
1787 temp = temp->d_parent;
2f2dc053 1788 }
1b71fe2e 1789 rcu_read_unlock();
2f2dc053
SW
1790 if (len)
1791 len--; /* no leading '/' */
1792
1793 path = kmalloc(len+1, GFP_NOFS);
d37b1d99 1794 if (!path)
2f2dc053
SW
1795 return ERR_PTR(-ENOMEM);
1796 pos = len;
1797 path[pos] = 0; /* trailing null */
1b71fe2e 1798 rcu_read_lock();
2f2dc053 1799 for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) {
1b71fe2e 1800 struct inode *inode;
2f2dc053 1801
1b71fe2e 1802 spin_lock(&temp->d_lock);
2b0143b5 1803 inode = d_inode(temp);
2f2dc053 1804 if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
104648ad 1805 dout("build_path path+%d: %p SNAPDIR\n",
2f2dc053
SW
1806 pos, temp);
1807 } else if (stop_on_nosnap && inode &&
1808 ceph_snap(inode) == CEPH_NOSNAP) {
9d5a09e6 1809 spin_unlock(&temp->d_lock);
2f2dc053
SW
1810 break;
1811 } else {
1812 pos -= temp->d_name.len;
1b71fe2e
AV
1813 if (pos < 0) {
1814 spin_unlock(&temp->d_lock);
2f2dc053 1815 break;
1b71fe2e 1816 }
2f2dc053
SW
1817 strncpy(path + pos, temp->d_name.name,
1818 temp->d_name.len);
2f2dc053 1819 }
1b71fe2e 1820 spin_unlock(&temp->d_lock);
2f2dc053
SW
1821 if (pos)
1822 path[--pos] = '/';
1823 temp = temp->d_parent;
2f2dc053 1824 }
1b71fe2e
AV
1825 rcu_read_unlock();
1826 if (pos != 0 || read_seqretry(&rename_lock, seq)) {
104648ad 1827 pr_err("build_path did not end path lookup where "
2f2dc053
SW
1828 "expected, namelen is %d, pos is %d\n", len, pos);
1829 /* presumably this is only possible if racing with a
1830 rename of one of the parent directories (we can not
1831 lock the dentries above us to prevent this, but
1832 retrying should be harmless) */
1833 kfree(path);
1834 goto retry;
1835 }
1836
2b0143b5 1837 *base = ceph_ino(d_inode(temp));
2f2dc053 1838 *plen = len;
104648ad 1839 dout("build_path on %p %d built %llx '%.*s'\n",
84d08fa8 1840 dentry, d_count(dentry), *base, len, path);
2f2dc053
SW
1841 return path;
1842}
1843
fd36a717 1844static int build_dentry_path(struct dentry *dentry, struct inode *dir,
2f2dc053
SW
1845 const char **ppath, int *ppathlen, u64 *pino,
1846 int *pfreepath)
1847{
1848 char *path;
1849
c6b0b656 1850 rcu_read_lock();
fd36a717
JL
1851 if (!dir)
1852 dir = d_inode_rcu(dentry->d_parent);
c6b0b656
JL
1853 if (dir && ceph_snap(dir) == CEPH_NOSNAP) {
1854 *pino = ceph_ino(dir);
1855 rcu_read_unlock();
2f2dc053
SW
1856 *ppath = dentry->d_name.name;
1857 *ppathlen = dentry->d_name.len;
1858 return 0;
1859 }
c6b0b656 1860 rcu_read_unlock();
2f2dc053
SW
1861 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1862 if (IS_ERR(path))
1863 return PTR_ERR(path);
1864 *ppath = path;
1865 *pfreepath = 1;
1866 return 0;
1867}
1868
1869static int build_inode_path(struct inode *inode,
1870 const char **ppath, int *ppathlen, u64 *pino,
1871 int *pfreepath)
1872{
1873 struct dentry *dentry;
1874 char *path;
1875
1876 if (ceph_snap(inode) == CEPH_NOSNAP) {
1877 *pino = ceph_ino(inode);
1878 *ppathlen = 0;
1879 return 0;
1880 }
1881 dentry = d_find_alias(inode);
1882 path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1);
1883 dput(dentry);
1884 if (IS_ERR(path))
1885 return PTR_ERR(path);
1886 *ppath = path;
1887 *pfreepath = 1;
1888 return 0;
1889}
1890
1891/*
1892 * request arguments may be specified via an inode *, a dentry *, or
1893 * an explicit ino+path.
1894 */
1895static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry,
fd36a717
JL
1896 struct inode *rdiri, const char *rpath,
1897 u64 rino, const char **ppath, int *pathlen,
2f2dc053
SW
1898 u64 *ino, int *freepath)
1899{
1900 int r = 0;
1901
1902 if (rinode) {
1903 r = build_inode_path(rinode, ppath, pathlen, ino, freepath);
1904 dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
1905 ceph_snap(rinode));
1906 } else if (rdentry) {
fd36a717
JL
1907 r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino,
1908 freepath);
2f2dc053
SW
1909 dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen,
1910 *ppath);
795858db 1911 } else if (rpath || rino) {
2f2dc053
SW
1912 *ino = rino;
1913 *ppath = rpath;
b000056a 1914 *pathlen = rpath ? strlen(rpath) : 0;
2f2dc053
SW
1915 dout(" path %.*s\n", *pathlen, rpath);
1916 }
1917
1918 return r;
1919}
1920
1921/*
1922 * called under mdsc->mutex
1923 */
1924static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc,
1925 struct ceph_mds_request *req,
6e6f0923 1926 int mds, bool drop_cap_releases)
2f2dc053
SW
1927{
1928 struct ceph_msg *msg;
1929 struct ceph_mds_request_head *head;
1930 const char *path1 = NULL;
1931 const char *path2 = NULL;
1932 u64 ino1 = 0, ino2 = 0;
1933 int pathlen1 = 0, pathlen2 = 0;
1934 int freepath1 = 0, freepath2 = 0;
1935 int len;
1936 u16 releases;
1937 void *p, *end;
1938 int ret;
1939
1940 ret = set_request_path_attr(req->r_inode, req->r_dentry,
3dd69aab 1941 req->r_parent, req->r_path1, req->r_ino1.ino,
2f2dc053
SW
1942 &path1, &pathlen1, &ino1, &freepath1);
1943 if (ret < 0) {
1944 msg = ERR_PTR(ret);
1945 goto out;
1946 }
1947
1948 ret = set_request_path_attr(NULL, req->r_old_dentry,
fd36a717 1949 req->r_old_dentry_dir,
2f2dc053
SW
1950 req->r_path2, req->r_ino2.ino,
1951 &path2, &pathlen2, &ino2, &freepath2);
1952 if (ret < 0) {
1953 msg = ERR_PTR(ret);
1954 goto out_free1;
1955 }
1956
1957 len = sizeof(*head) +
b8e69066 1958 pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) +
777d738a 1959 sizeof(struct ceph_timespec);
2f2dc053
SW
1960
1961 /* calculate (max) length for cap releases */
1962 len += sizeof(struct ceph_mds_request_release) *
1963 (!!req->r_inode_drop + !!req->r_dentry_drop +
1964 !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
1965 if (req->r_dentry_drop)
1966 len += req->r_dentry->d_name.len;
1967 if (req->r_old_dentry_drop)
1968 len += req->r_old_dentry->d_name.len;
1969
b61c2763 1970 msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, GFP_NOFS, false);
a79832f2
SW
1971 if (!msg) {
1972 msg = ERR_PTR(-ENOMEM);
2f2dc053 1973 goto out_free2;
a79832f2 1974 }
2f2dc053 1975
7cfa0313 1976 msg->hdr.version = cpu_to_le16(2);
6df058c0
SW
1977 msg->hdr.tid = cpu_to_le64(req->r_tid);
1978
2f2dc053
SW
1979 head = msg->front.iov_base;
1980 p = msg->front.iov_base + sizeof(*head);
1981 end = msg->front.iov_base + msg->front.iov_len;
1982
1983 head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
1984 head->op = cpu_to_le32(req->r_op);
ff3d0046
EB
1985 head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid));
1986 head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid));
2f2dc053
SW
1987 head->args = req->r_args;
1988
1989 ceph_encode_filepath(&p, end, ino1, path1);
1990 ceph_encode_filepath(&p, end, ino2, path2);
1991
e979cf50
SW
1992 /* make note of release offset, in case we need to replay */
1993 req->r_request_release_offset = p - msg->front.iov_base;
1994
2f2dc053
SW
1995 /* cap releases */
1996 releases = 0;
1997 if (req->r_inode_drop)
1998 releases += ceph_encode_inode_release(&p,
2b0143b5 1999 req->r_inode ? req->r_inode : d_inode(req->r_dentry),
2f2dc053
SW
2000 mds, req->r_inode_drop, req->r_inode_unless, 0);
2001 if (req->r_dentry_drop)
2002 releases += ceph_encode_dentry_release(&p, req->r_dentry,
3dd69aab 2003 req->r_parent, mds, req->r_dentry_drop,
ca6c8ae0 2004 req->r_dentry_unless);
2f2dc053
SW
2005 if (req->r_old_dentry_drop)
2006 releases += ceph_encode_dentry_release(&p, req->r_old_dentry,
ca6c8ae0
JL
2007 req->r_old_dentry_dir, mds,
2008 req->r_old_dentry_drop,
2009 req->r_old_dentry_unless);
2f2dc053
SW
2010 if (req->r_old_inode_drop)
2011 releases += ceph_encode_inode_release(&p,
2b0143b5 2012 d_inode(req->r_old_dentry),
2f2dc053 2013 mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
6e6f0923
YZ
2014
2015 if (drop_cap_releases) {
2016 releases = 0;
2017 p = msg->front.iov_base + req->r_request_release_offset;
2018 }
2019
2f2dc053
SW
2020 head->num_releases = cpu_to_le16(releases);
2021
b8e69066 2022 /* time stamp */
1f041a89
YZ
2023 {
2024 struct ceph_timespec ts;
2025 ceph_encode_timespec(&ts, &req->r_stamp);
2026 ceph_encode_copy(&p, &ts, sizeof(ts));
2027 }
b8e69066 2028
2f2dc053
SW
2029 BUG_ON(p > end);
2030 msg->front.iov_len = p - msg->front.iov_base;
2031 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2032
25e6bae3
YZ
2033 if (req->r_pagelist) {
2034 struct ceph_pagelist *pagelist = req->r_pagelist;
0e1a5ee6 2035 refcount_inc(&pagelist->refcnt);
25e6bae3
YZ
2036 ceph_msg_data_add_pagelist(msg, pagelist);
2037 msg->hdr.data_len = cpu_to_le32(pagelist->length);
2038 } else {
2039 msg->hdr.data_len = 0;
ebf18f47 2040 }
02afca6c 2041
2f2dc053
SW
2042 msg->hdr.data_off = cpu_to_le16(0);
2043
2044out_free2:
2045 if (freepath2)
2046 kfree((char *)path2);
2047out_free1:
2048 if (freepath1)
2049 kfree((char *)path1);
2050out:
2051 return msg;
2052}
2053
2054/*
2055 * called under mdsc->mutex if error, under no mutex if
2056 * success.
2057 */
2058static void complete_request(struct ceph_mds_client *mdsc,
2059 struct ceph_mds_request *req)
2060{
2061 if (req->r_callback)
2062 req->r_callback(mdsc, req);
2063 else
03066f23 2064 complete_all(&req->r_completion);
2f2dc053
SW
2065}
2066
2067/*
2068 * called under mdsc->mutex
2069 */
2070static int __prepare_send_request(struct ceph_mds_client *mdsc,
2071 struct ceph_mds_request *req,
6e6f0923 2072 int mds, bool drop_cap_releases)
2f2dc053
SW
2073{
2074 struct ceph_mds_request_head *rhead;
2075 struct ceph_msg *msg;
2076 int flags = 0;
2077
2f2dc053 2078 req->r_attempts++;
e55b71f8
GF
2079 if (req->r_inode) {
2080 struct ceph_cap *cap =
2081 ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
2082
2083 if (cap)
2084 req->r_sent_on_mseq = cap->mseq;
2085 else
2086 req->r_sent_on_mseq = -1;
2087 }
2f2dc053
SW
2088 dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req,
2089 req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts);
2090
bc2de10d 2091 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
c5c9a0bf 2092 void *p;
01a92f17
SW
2093 /*
2094 * Replay. Do not regenerate message (and rebuild
2095 * paths, etc.); just use the original message.
2096 * Rebuilding paths will break for renames because
2097 * d_move mangles the src name.
2098 */
2099 msg = req->r_request;
2100 rhead = msg->front.iov_base;
2101
2102 flags = le32_to_cpu(rhead->flags);
2103 flags |= CEPH_MDS_FLAG_REPLAY;
2104 rhead->flags = cpu_to_le32(flags);
2105
2106 if (req->r_target_inode)
2107 rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
2108
2109 rhead->num_retry = req->r_attempts - 1;
e979cf50
SW
2110
2111 /* remove cap/dentry releases from message */
2112 rhead->num_releases = 0;
c5c9a0bf
YZ
2113
2114 /* time stamp */
2115 p = msg->front.iov_base + req->r_request_release_offset;
1f041a89
YZ
2116 {
2117 struct ceph_timespec ts;
2118 ceph_encode_timespec(&ts, &req->r_stamp);
2119 ceph_encode_copy(&p, &ts, sizeof(ts));
2120 }
c5c9a0bf
YZ
2121
2122 msg->front.iov_len = p - msg->front.iov_base;
2123 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
01a92f17
SW
2124 return 0;
2125 }
2126
2f2dc053
SW
2127 if (req->r_request) {
2128 ceph_msg_put(req->r_request);
2129 req->r_request = NULL;
2130 }
6e6f0923 2131 msg = create_request_message(mdsc, req, mds, drop_cap_releases);
2f2dc053 2132 if (IS_ERR(msg)) {
e1518c7c 2133 req->r_err = PTR_ERR(msg);
a79832f2 2134 return PTR_ERR(msg);
2f2dc053
SW
2135 }
2136 req->r_request = msg;
2137
2138 rhead = msg->front.iov_base;
2f2dc053 2139 rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
bc2de10d 2140 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
2f2dc053 2141 flags |= CEPH_MDS_FLAG_REPLAY;
3dd69aab 2142 if (req->r_parent)
2f2dc053
SW
2143 flags |= CEPH_MDS_FLAG_WANT_DENTRY;
2144 rhead->flags = cpu_to_le32(flags);
2145 rhead->num_fwd = req->r_num_fwd;
2146 rhead->num_retry = req->r_attempts - 1;
01a92f17 2147 rhead->ino = 0;
2f2dc053 2148
3dd69aab 2149 dout(" r_parent = %p\n", req->r_parent);
2f2dc053
SW
2150 return 0;
2151}
2152
2153/*
2154 * send request, or put it on the appropriate wait list.
2155 */
2156static int __do_request(struct ceph_mds_client *mdsc,
2157 struct ceph_mds_request *req)
2158{
2159 struct ceph_mds_session *session = NULL;
2160 int mds = -1;
48fec5d0 2161 int err = 0;
2f2dc053 2162
bc2de10d
JL
2163 if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
2164 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
eb1b8af3 2165 __unregister_request(mdsc, req);
2f2dc053 2166 goto out;
eb1b8af3 2167 }
2f2dc053
SW
2168
2169 if (req->r_timeout &&
2170 time_after_eq(jiffies, req->r_started + req->r_timeout)) {
2171 dout("do_request timed out\n");
2172 err = -EIO;
2173 goto finish;
2174 }
52953d55 2175 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
48fec5d0
YZ
2176 dout("do_request forced umount\n");
2177 err = -EIO;
2178 goto finish;
2179 }
52953d55 2180 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
e9e427f0
YZ
2181 if (mdsc->mdsmap_err) {
2182 err = mdsc->mdsmap_err;
2183 dout("do_request mdsmap err %d\n", err);
2184 goto finish;
2185 }
cc8e8342
YZ
2186 if (mdsc->mdsmap->m_epoch == 0) {
2187 dout("do_request no mdsmap, waiting for map\n");
2188 list_add(&req->r_wait, &mdsc->waiting_for_map);
2189 goto finish;
2190 }
e9e427f0
YZ
2191 if (!(mdsc->fsc->mount_options->flags &
2192 CEPH_MOUNT_OPT_MOUNTWAIT) &&
2193 !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
2194 err = -ENOENT;
2195 pr_info("probably no mds server is up\n");
2196 goto finish;
2197 }
2198 }
2f2dc053 2199
dc69e2e9
SW
2200 put_request_session(req);
2201
2f2dc053
SW
2202 mds = __choose_mds(mdsc, req);
2203 if (mds < 0 ||
2204 ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
2205 dout("do_request no mds or not active, waiting for map\n");
2206 list_add(&req->r_wait, &mdsc->waiting_for_map);
2207 goto out;
2208 }
2209
2210 /* get, open session */
2211 session = __ceph_lookup_mds_session(mdsc, mds);
9c423956 2212 if (!session) {
2f2dc053 2213 session = register_session(mdsc, mds);
9c423956
SW
2214 if (IS_ERR(session)) {
2215 err = PTR_ERR(session);
2216 goto finish;
2217 }
2218 }
dc69e2e9
SW
2219 req->r_session = get_session(session);
2220
2f2dc053 2221 dout("do_request mds%d session %p state %s\n", mds, session,
a687ecaf 2222 ceph_session_state_name(session->s_state));
2f2dc053
SW
2223 if (session->s_state != CEPH_MDS_SESSION_OPEN &&
2224 session->s_state != CEPH_MDS_SESSION_HUNG) {
fcff415c
YZ
2225 if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
2226 err = -EACCES;
2227 goto out_session;
2228 }
2f2dc053
SW
2229 if (session->s_state == CEPH_MDS_SESSION_NEW ||
2230 session->s_state == CEPH_MDS_SESSION_CLOSING)
2231 __open_session(mdsc, session);
2232 list_add(&req->r_wait, &session->s_waiting);
2233 goto out_session;
2234 }
2235
2236 /* send request */
2f2dc053
SW
2237 req->r_resend_mds = -1; /* forget any previous mds hint */
2238
2239 if (req->r_request_started == 0) /* note request start time */
2240 req->r_request_started = jiffies;
2241
6e6f0923 2242 err = __prepare_send_request(mdsc, req, mds, false);
2f2dc053
SW
2243 if (!err) {
2244 ceph_msg_get(req->r_request);
2245 ceph_con_send(&session->s_con, req->r_request);
2246 }
2247
2248out_session:
2249 ceph_put_mds_session(session);
48fec5d0
YZ
2250finish:
2251 if (err) {
2252 dout("__do_request early error %d\n", err);
2253 req->r_err = err;
2254 complete_request(mdsc, req);
2255 __unregister_request(mdsc, req);
2256 }
2f2dc053
SW
2257out:
2258 return err;
2f2dc053
SW
2259}
2260
2261/*
2262 * called under mdsc->mutex
2263 */
2264static void __wake_requests(struct ceph_mds_client *mdsc,
2265 struct list_head *head)
2266{
ed75ec2c
YZ
2267 struct ceph_mds_request *req;
2268 LIST_HEAD(tmp_list);
2269
2270 list_splice_init(head, &tmp_list);
2f2dc053 2271
ed75ec2c
YZ
2272 while (!list_empty(&tmp_list)) {
2273 req = list_entry(tmp_list.next,
2274 struct ceph_mds_request, r_wait);
2f2dc053 2275 list_del_init(&req->r_wait);
7971bd92 2276 dout(" wake request %p tid %llu\n", req, req->r_tid);
2f2dc053
SW
2277 __do_request(mdsc, req);
2278 }
2279}
2280
2281/*
2282 * Wake up threads with requests pending for @mds, so that they can
29790f26 2283 * resubmit their requests to a possibly different mds.
2f2dc053 2284 */
29790f26 2285static void kick_requests(struct ceph_mds_client *mdsc, int mds)
2f2dc053 2286{
44ca18f2 2287 struct ceph_mds_request *req;
282c1052 2288 struct rb_node *p = rb_first(&mdsc->request_tree);
2f2dc053
SW
2289
2290 dout("kick_requests mds%d\n", mds);
282c1052 2291 while (p) {
44ca18f2 2292 req = rb_entry(p, struct ceph_mds_request, r_node);
282c1052 2293 p = rb_next(p);
bc2de10d 2294 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
44ca18f2 2295 continue;
3de22be6
YZ
2296 if (req->r_attempts > 0)
2297 continue; /* only new requests */
44ca18f2
SW
2298 if (req->r_session &&
2299 req->r_session->s_mds == mds) {
2300 dout(" kicking tid %llu\n", req->r_tid);
03974e81 2301 list_del_init(&req->r_wait);
44ca18f2 2302 __do_request(mdsc, req);
2f2dc053
SW
2303 }
2304 }
2305}
2306
2307void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc,
2308 struct ceph_mds_request *req)
2309{
2310 dout("submit_request on %p\n", req);
2311 mutex_lock(&mdsc->mutex);
2312 __register_request(mdsc, req, NULL);
2313 __do_request(mdsc, req);
2314 mutex_unlock(&mdsc->mutex);
2315}
2316
2317/*
2318 * Synchrously perform an mds request. Take care of all of the
2319 * session setup, forwarding, retry details.
2320 */
2321int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
2322 struct inode *dir,
2323 struct ceph_mds_request *req)
2324{
2325 int err;
2326
2327 dout("do_request on %p\n", req);
2328
3dd69aab 2329 /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
2f2dc053
SW
2330 if (req->r_inode)
2331 ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
3dd69aab
JL
2332 if (req->r_parent)
2333 ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
844d87c3 2334 if (req->r_old_dentry_dir)
41b02e1f
SW
2335 ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
2336 CEPH_CAP_PIN);
2f2dc053
SW
2337
2338 /* issue */
2339 mutex_lock(&mdsc->mutex);
2340 __register_request(mdsc, req, dir);
2341 __do_request(mdsc, req);
2342
e1518c7c
SW
2343 if (req->r_err) {
2344 err = req->r_err;
e1518c7c 2345 goto out;
2f2dc053
SW
2346 }
2347
e1518c7c
SW
2348 /* wait */
2349 mutex_unlock(&mdsc->mutex);
2350 dout("do_request waiting\n");
5be73034 2351 if (!req->r_timeout && req->r_wait_for_completion) {
9280be24 2352 err = req->r_wait_for_completion(mdsc, req);
e1518c7c 2353 } else {
5be73034
ID
2354 long timeleft = wait_for_completion_killable_timeout(
2355 &req->r_completion,
2356 ceph_timeout_jiffies(req->r_timeout));
2357 if (timeleft > 0)
2358 err = 0;
2359 else if (!timeleft)
2360 err = -EIO; /* timed out */
2361 else
2362 err = timeleft; /* killed */
e1518c7c
SW
2363 }
2364 dout("do_request waited, got %d\n", err);
2365 mutex_lock(&mdsc->mutex);
5b1daecd 2366
e1518c7c 2367 /* only abort if we didn't race with a real reply */
bc2de10d 2368 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
e1518c7c
SW
2369 err = le32_to_cpu(req->r_reply_info.head->result);
2370 } else if (err < 0) {
2371 dout("aborted request %lld with %d\n", req->r_tid, err);
b4556396
SW
2372
2373 /*
2374 * ensure we aren't running concurrently with
2375 * ceph_fill_trace or ceph_readdir_prepopulate, which
2376 * rely on locks (dir mutex) held by our caller.
2377 */
2378 mutex_lock(&req->r_fill_mutex);
e1518c7c 2379 req->r_err = err;
bc2de10d 2380 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
b4556396 2381 mutex_unlock(&req->r_fill_mutex);
5b1daecd 2382
3dd69aab 2383 if (req->r_parent &&
167c9e35
SW
2384 (req->r_op & CEPH_MDS_OP_WRITE))
2385 ceph_invalidate_dir_request(req);
2f2dc053 2386 } else {
e1518c7c 2387 err = req->r_err;
2f2dc053 2388 }
2f2dc053 2389
e1518c7c
SW
2390out:
2391 mutex_unlock(&mdsc->mutex);
2f2dc053
SW
2392 dout("do_request %p done, result %d\n", req, err);
2393 return err;
2394}
2395
167c9e35 2396/*
2f276c51 2397 * Invalidate dir's completeness, dentry lease state on an aborted MDS
167c9e35
SW
2398 * namespace request.
2399 */
2400void ceph_invalidate_dir_request(struct ceph_mds_request *req)
2401{
3dd69aab 2402 struct inode *inode = req->r_parent;
167c9e35 2403
2f276c51 2404 dout("invalidate_dir_request %p (complete, lease(s))\n", inode);
167c9e35 2405
2f276c51 2406 ceph_dir_clear_complete(inode);
167c9e35
SW
2407 if (req->r_dentry)
2408 ceph_invalidate_dentry_lease(req->r_dentry);
2409 if (req->r_old_dentry)
2410 ceph_invalidate_dentry_lease(req->r_old_dentry);
2411}
2412
2f2dc053
SW
2413/*
2414 * Handle mds reply.
2415 *
2416 * We take the session mutex and parse and process the reply immediately.
2417 * This preserves the logical ordering of replies, capabilities, etc., sent
2418 * by the MDS as they are applied to our local cache.
2419 */
2420static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
2421{
2422 struct ceph_mds_client *mdsc = session->s_mdsc;
2423 struct ceph_mds_request *req;
2424 struct ceph_mds_reply_head *head = msg->front.iov_base;
2425 struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
982d6011 2426 struct ceph_snap_realm *realm;
2f2dc053
SW
2427 u64 tid;
2428 int err, result;
2600d2dd 2429 int mds = session->s_mds;
2f2dc053 2430
2f2dc053
SW
2431 if (msg->front.iov_len < sizeof(*head)) {
2432 pr_err("mdsc_handle_reply got corrupt (short) reply\n");
9ec7cab1 2433 ceph_msg_dump(msg);
2f2dc053
SW
2434 return;
2435 }
2436
2437 /* get request, session */
6df058c0 2438 tid = le64_to_cpu(msg->hdr.tid);
2f2dc053 2439 mutex_lock(&mdsc->mutex);
fcd00b68 2440 req = lookup_get_request(mdsc, tid);
2f2dc053
SW
2441 if (!req) {
2442 dout("handle_reply on unknown tid %llu\n", tid);
2443 mutex_unlock(&mdsc->mutex);
2444 return;
2445 }
2446 dout("handle_reply %p\n", req);
2f2dc053
SW
2447
2448 /* correct session? */
d96d6049 2449 if (req->r_session != session) {
2f2dc053
SW
2450 pr_err("mdsc_handle_reply got %llu on session mds%d"
2451 " not mds%d\n", tid, session->s_mds,
2452 req->r_session ? req->r_session->s_mds : -1);
2453 mutex_unlock(&mdsc->mutex);
2454 goto out;
2455 }
2456
2457 /* dup? */
bc2de10d
JL
2458 if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
2459 (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
f3ae1b97 2460 pr_warn("got a dup %s reply on %llu from mds%d\n",
2f2dc053
SW
2461 head->safe ? "safe" : "unsafe", tid, mds);
2462 mutex_unlock(&mdsc->mutex);
2463 goto out;
2464 }
bc2de10d 2465 if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
f3ae1b97 2466 pr_warn("got unsafe after safe on %llu from mds%d\n",
85792d0d
SW
2467 tid, mds);
2468 mutex_unlock(&mdsc->mutex);
2469 goto out;
2470 }
2f2dc053
SW
2471
2472 result = le32_to_cpu(head->result);
2473
2474 /*
e55b71f8
GF
2475 * Handle an ESTALE
2476 * if we're not talking to the authority, send to them
2477 * if the authority has changed while we weren't looking,
2478 * send to new authority
2479 * Otherwise we just have to return an ESTALE
2f2dc053
SW
2480 */
2481 if (result == -ESTALE) {
e55b71f8 2482 dout("got ESTALE on request %llu", req->r_tid);
51da8e8c 2483 req->r_resend_mds = -1;
ca18bede 2484 if (req->r_direct_mode != USE_AUTH_MDS) {
e55b71f8
GF
2485 dout("not using auth, setting for that now");
2486 req->r_direct_mode = USE_AUTH_MDS;
2f2dc053
SW
2487 __do_request(mdsc, req);
2488 mutex_unlock(&mdsc->mutex);
2489 goto out;
e55b71f8 2490 } else {
ca18bede
YZ
2491 int mds = __choose_mds(mdsc, req);
2492 if (mds >= 0 && mds != req->r_session->s_mds) {
2493 dout("but auth changed, so resending");
e55b71f8
GF
2494 __do_request(mdsc, req);
2495 mutex_unlock(&mdsc->mutex);
2496 goto out;
2497 }
2f2dc053 2498 }
e55b71f8 2499 dout("have to return ESTALE on request %llu", req->r_tid);
2f2dc053
SW
2500 }
2501
e55b71f8 2502
2f2dc053 2503 if (head->safe) {
bc2de10d 2504 set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
2f2dc053 2505 __unregister_request(mdsc, req);
2f2dc053 2506
bc2de10d 2507 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
2f2dc053
SW
2508 /*
2509 * We already handled the unsafe response, now do the
2510 * cleanup. No need to examine the response; the MDS
2511 * doesn't include any result info in the safe
2512 * response. And even if it did, there is nothing
2513 * useful we could do with a revised return value.
2514 */
2515 dout("got safe reply %llu, mds%d\n", tid, mds);
2f2dc053
SW
2516
2517 /* last unsafe request during umount? */
44ca18f2 2518 if (mdsc->stopping && !__get_oldest_req(mdsc))
03066f23 2519 complete_all(&mdsc->safe_umount_waiters);
2f2dc053
SW
2520 mutex_unlock(&mdsc->mutex);
2521 goto out;
2522 }
e1518c7c 2523 } else {
bc2de10d 2524 set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
2f2dc053 2525 list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
4c06ace8
YZ
2526 if (req->r_unsafe_dir) {
2527 struct ceph_inode_info *ci =
2528 ceph_inode(req->r_unsafe_dir);
2529 spin_lock(&ci->i_unsafe_lock);
2530 list_add_tail(&req->r_unsafe_dir_item,
2531 &ci->i_unsafe_dirops);
2532 spin_unlock(&ci->i_unsafe_lock);
2533 }
2f2dc053
SW
2534 }
2535
2536 dout("handle_reply tid %lld result %d\n", tid, result);
2537 rinfo = &req->r_reply_info;
14303d20 2538 err = parse_reply_info(msg, rinfo, session->s_con.peer_features);
2f2dc053
SW
2539 mutex_unlock(&mdsc->mutex);
2540
2541 mutex_lock(&session->s_mutex);
2542 if (err < 0) {
25933abd 2543 pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid);
9ec7cab1 2544 ceph_msg_dump(msg);
2f2dc053
SW
2545 goto out_err;
2546 }
2547
2548 /* snap trace */
982d6011 2549 realm = NULL;
2f2dc053
SW
2550 if (rinfo->snapblob_len) {
2551 down_write(&mdsc->snap_rwsem);
2552 ceph_update_snap_trace(mdsc, rinfo->snapblob,
982d6011
YZ
2553 rinfo->snapblob + rinfo->snapblob_len,
2554 le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
2555 &realm);
2f2dc053
SW
2556 downgrade_write(&mdsc->snap_rwsem);
2557 } else {
2558 down_read(&mdsc->snap_rwsem);
2559 }
2560
2561 /* insert trace into our cache */
b4556396 2562 mutex_lock(&req->r_fill_mutex);
315f2408 2563 current->journal_info = req;
f5a03b08 2564 err = ceph_fill_trace(mdsc->fsc->sb, req);
2f2dc053 2565 if (err == 0) {
6e8575fa 2566 if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
81c6aea5 2567 req->r_op == CEPH_MDS_OP_LSSNAP))
2f2dc053 2568 ceph_readdir_prepopulate(req, req->r_session);
37151668 2569 ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
2f2dc053 2570 }
315f2408 2571 current->journal_info = NULL;
b4556396 2572 mutex_unlock(&req->r_fill_mutex);
2f2dc053
SW
2573
2574 up_read(&mdsc->snap_rwsem);
982d6011
YZ
2575 if (realm)
2576 ceph_put_snap_realm(mdsc, realm);
68cd5b4b 2577
bc2de10d
JL
2578 if (err == 0 && req->r_target_inode &&
2579 test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
68cd5b4b
YZ
2580 struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
2581 spin_lock(&ci->i_unsafe_lock);
2582 list_add_tail(&req->r_unsafe_target_item, &ci->i_unsafe_iops);
2583 spin_unlock(&ci->i_unsafe_lock);
2584 }
2f2dc053 2585out_err:
e1518c7c 2586 mutex_lock(&mdsc->mutex);
bc2de10d 2587 if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
e1518c7c
SW
2588 if (err) {
2589 req->r_err = err;
2590 } else {
5fdb1389 2591 req->r_reply = ceph_msg_get(msg);
bc2de10d 2592 set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
e1518c7c 2593 }
2f2dc053 2594 } else {
e1518c7c 2595 dout("reply arrived after request %lld was aborted\n", tid);
2f2dc053 2596 }
e1518c7c 2597 mutex_unlock(&mdsc->mutex);
2f2dc053 2598
2f2dc053
SW
2599 mutex_unlock(&session->s_mutex);
2600
2601 /* kick calling process */
2602 complete_request(mdsc, req);
2603out:
2604 ceph_mdsc_put_request(req);
2605 return;
2606}
2607
2608
2609
2610/*
2611 * handle mds notification that our request has been forwarded.
2612 */
2600d2dd
SW
2613static void handle_forward(struct ceph_mds_client *mdsc,
2614 struct ceph_mds_session *session,
2615 struct ceph_msg *msg)
2f2dc053
SW
2616{
2617 struct ceph_mds_request *req;
a1ea787c 2618 u64 tid = le64_to_cpu(msg->hdr.tid);
2f2dc053
SW
2619 u32 next_mds;
2620 u32 fwd_seq;
2f2dc053
SW
2621 int err = -EINVAL;
2622 void *p = msg->front.iov_base;
2623 void *end = p + msg->front.iov_len;
2f2dc053 2624
a1ea787c 2625 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
2626 next_mds = ceph_decode_32(&p);
2627 fwd_seq = ceph_decode_32(&p);
2f2dc053
SW
2628
2629 mutex_lock(&mdsc->mutex);
fcd00b68 2630 req = lookup_get_request(mdsc, tid);
2f2dc053 2631 if (!req) {
2a8e5e36 2632 dout("forward tid %llu to mds%d - req dne\n", tid, next_mds);
2f2dc053
SW
2633 goto out; /* dup reply? */
2634 }
2635
bc2de10d 2636 if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
2a8e5e36
SW
2637 dout("forward tid %llu aborted, unregistering\n", tid);
2638 __unregister_request(mdsc, req);
2639 } else if (fwd_seq <= req->r_num_fwd) {
2640 dout("forward tid %llu to mds%d - old seq %d <= %d\n",
2f2dc053
SW
2641 tid, next_mds, req->r_num_fwd, fwd_seq);
2642 } else {
2643 /* resend. forward race not possible; mds would drop */
2a8e5e36
SW
2644 dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds);
2645 BUG_ON(req->r_err);
bc2de10d 2646 BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
3de22be6 2647 req->r_attempts = 0;
2f2dc053
SW
2648 req->r_num_fwd = fwd_seq;
2649 req->r_resend_mds = next_mds;
2650 put_request_session(req);
2651 __do_request(mdsc, req);
2652 }
2653 ceph_mdsc_put_request(req);
2654out:
2655 mutex_unlock(&mdsc->mutex);
2656 return;
2657
2658bad:
2659 pr_err("mdsc_handle_forward decode error err=%d\n", err);
2660}
2661
2662/*
2663 * handle a mds session control message
2664 */
2665static void handle_session(struct ceph_mds_session *session,
2666 struct ceph_msg *msg)
2667{
2668 struct ceph_mds_client *mdsc = session->s_mdsc;
2669 u32 op;
2670 u64 seq;
2600d2dd 2671 int mds = session->s_mds;
2f2dc053
SW
2672 struct ceph_mds_session_head *h = msg->front.iov_base;
2673 int wake = 0;
2674
2f2dc053
SW
2675 /* decode */
2676 if (msg->front.iov_len != sizeof(*h))
2677 goto bad;
2678 op = le32_to_cpu(h->op);
2679 seq = le64_to_cpu(h->seq);
2680
2681 mutex_lock(&mdsc->mutex);
0a07fc8c
YZ
2682 if (op == CEPH_SESSION_CLOSE) {
2683 get_session(session);
2600d2dd 2684 __unregister_session(mdsc, session);
0a07fc8c 2685 }
2f2dc053
SW
2686 /* FIXME: this ttl calculation is generous */
2687 session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
2688 mutex_unlock(&mdsc->mutex);
2689
2690 mutex_lock(&session->s_mutex);
2691
2692 dout("handle_session mds%d %s %p state %s seq %llu\n",
2693 mds, ceph_session_op_name(op), session,
a687ecaf 2694 ceph_session_state_name(session->s_state), seq);
2f2dc053
SW
2695
2696 if (session->s_state == CEPH_MDS_SESSION_HUNG) {
2697 session->s_state = CEPH_MDS_SESSION_OPEN;
2698 pr_info("mds%d came back\n", session->s_mds);
2699 }
2700
2701 switch (op) {
2702 case CEPH_SESSION_OPEN:
29790f26
SW
2703 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2704 pr_info("mds%d reconnect success\n", session->s_mds);
2f2dc053
SW
2705 session->s_state = CEPH_MDS_SESSION_OPEN;
2706 renewed_caps(mdsc, session, 0);
2707 wake = 1;
2708 if (mdsc->stopping)
2709 __close_session(mdsc, session);
2710 break;
2711
2712 case CEPH_SESSION_RENEWCAPS:
2713 if (session->s_renew_seq == seq)
2714 renewed_caps(mdsc, session, 1);
2715 break;
2716
2717 case CEPH_SESSION_CLOSE:
29790f26
SW
2718 if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
2719 pr_info("mds%d reconnect denied\n", session->s_mds);
1c841a96 2720 cleanup_session_requests(mdsc, session);
2f2dc053 2721 remove_session_caps(session);
656e4382 2722 wake = 2; /* for good measure */
f3c60c59 2723 wake_up_all(&mdsc->session_close_wq);
2f2dc053
SW
2724 break;
2725
2726 case CEPH_SESSION_STALE:
2727 pr_info("mds%d caps went stale, renewing\n",
2728 session->s_mds);
d8fb02ab 2729 spin_lock(&session->s_gen_ttl_lock);
2f2dc053 2730 session->s_cap_gen++;
1ce208a6 2731 session->s_cap_ttl = jiffies - 1;
d8fb02ab 2732 spin_unlock(&session->s_gen_ttl_lock);
2f2dc053
SW
2733 send_renew_caps(mdsc, session);
2734 break;
2735
2736 case CEPH_SESSION_RECALL_STATE:
2737 trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
2738 break;
2739
186e4f7a
YZ
2740 case CEPH_SESSION_FLUSHMSG:
2741 send_flushmsg_ack(mdsc, session, seq);
2742 break;
2743
03f4fcb0
YZ
2744 case CEPH_SESSION_FORCE_RO:
2745 dout("force_session_readonly %p\n", session);
2746 spin_lock(&session->s_cap_lock);
2747 session->s_readonly = true;
2748 spin_unlock(&session->s_cap_lock);
2749 wake_up_session_caps(session, 0);
2750 break;
2751
fcff415c
YZ
2752 case CEPH_SESSION_REJECT:
2753 WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
2754 pr_info("mds%d rejected session\n", session->s_mds);
2755 session->s_state = CEPH_MDS_SESSION_REJECTED;
2756 cleanup_session_requests(mdsc, session);
2757 remove_session_caps(session);
2758 wake = 2; /* for good measure */
2759 break;
2760
2f2dc053
SW
2761 default:
2762 pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds);
2763 WARN_ON(1);
2764 }
2765
2766 mutex_unlock(&session->s_mutex);
2767 if (wake) {
2768 mutex_lock(&mdsc->mutex);
2769 __wake_requests(mdsc, &session->s_waiting);
656e4382
YZ
2770 if (wake == 2)
2771 kick_requests(mdsc, mds);
2f2dc053
SW
2772 mutex_unlock(&mdsc->mutex);
2773 }
0a07fc8c
YZ
2774 if (op == CEPH_SESSION_CLOSE)
2775 ceph_put_mds_session(session);
2f2dc053
SW
2776 return;
2777
2778bad:
2779 pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds,
2780 (int)msg->front.iov_len);
9ec7cab1 2781 ceph_msg_dump(msg);
2f2dc053
SW
2782 return;
2783}
2784
2785
2786/*
2787 * called under session->mutex.
2788 */
2789static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
2790 struct ceph_mds_session *session)
2791{
2792 struct ceph_mds_request *req, *nreq;
3de22be6 2793 struct rb_node *p;
2f2dc053
SW
2794 int err;
2795
2796 dout("replay_unsafe_requests mds%d\n", session->s_mds);
2797
2798 mutex_lock(&mdsc->mutex);
2799 list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) {
6e6f0923 2800 err = __prepare_send_request(mdsc, req, session->s_mds, true);
2f2dc053
SW
2801 if (!err) {
2802 ceph_msg_get(req->r_request);
2803 ceph_con_send(&session->s_con, req->r_request);
2804 }
2805 }
3de22be6
YZ
2806
2807 /*
2808 * also re-send old requests when MDS enters reconnect stage. So that MDS
2809 * can process completed request in clientreplay stage.
2810 */
2811 p = rb_first(&mdsc->request_tree);
2812 while (p) {
2813 req = rb_entry(p, struct ceph_mds_request, r_node);
2814 p = rb_next(p);
bc2de10d 2815 if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
3de22be6
YZ
2816 continue;
2817 if (req->r_attempts == 0)
2818 continue; /* only old requests */
2819 if (req->r_session &&
2820 req->r_session->s_mds == session->s_mds) {
6e6f0923
YZ
2821 err = __prepare_send_request(mdsc, req,
2822 session->s_mds, true);
3de22be6
YZ
2823 if (!err) {
2824 ceph_msg_get(req->r_request);
2825 ceph_con_send(&session->s_con, req->r_request);
2826 }
2827 }
2828 }
2f2dc053
SW
2829 mutex_unlock(&mdsc->mutex);
2830}
2831
2832/*
2833 * Encode information about a cap for a reconnect with the MDS.
2834 */
2f2dc053
SW
2835static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
2836 void *arg)
2837{
20cb34ae
SW
2838 union {
2839 struct ceph_mds_cap_reconnect v2;
2840 struct ceph_mds_cap_reconnect_v1 v1;
2841 } rec;
b3f8d68f 2842 struct ceph_inode_info *ci = cap->ci;
20cb34ae
SW
2843 struct ceph_reconnect_state *recon_state = arg;
2844 struct ceph_pagelist *pagelist = recon_state->pagelist;
2f2dc053
SW
2845 char *path;
2846 int pathlen, err;
2847 u64 pathbase;
3469ed0d 2848 u64 snap_follows;
2f2dc053
SW
2849 struct dentry *dentry;
2850
2f2dc053
SW
2851 dout(" adding %p ino %llx.%llx cap %p %lld %s\n",
2852 inode, ceph_vinop(inode), cap, cap->cap_id,
2853 ceph_cap_string(cap->issued));
93cea5be
SW
2854 err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
2855 if (err)
2856 return err;
2f2dc053
SW
2857
2858 dentry = d_find_alias(inode);
2859 if (dentry) {
2860 path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0);
2861 if (IS_ERR(path)) {
2862 err = PTR_ERR(path);
e072f8aa 2863 goto out_dput;
2f2dc053
SW
2864 }
2865 } else {
2866 path = NULL;
2867 pathlen = 0;
4eacd4cb 2868 pathbase = 0;
2f2dc053 2869 }
2f2dc053 2870
be655596 2871 spin_lock(&ci->i_ceph_lock);
2f2dc053
SW
2872 cap->seq = 0; /* reset cap seq */
2873 cap->issue_seq = 0; /* and issue_seq */
667ca05c 2874 cap->mseq = 0; /* and migrate_seq */
99a9c273 2875 cap->cap_gen = cap->session->s_cap_gen;
20cb34ae 2876
121f22a1 2877 if (recon_state->msg_version >= 2) {
20cb34ae
SW
2878 rec.v2.cap_id = cpu_to_le64(cap->cap_id);
2879 rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2880 rec.v2.issued = cpu_to_le32(cap->issued);
2881 rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2882 rec.v2.pathbase = cpu_to_le64(pathbase);
b3f8d68f
YZ
2883 rec.v2.flock_len =
2884 (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1;
20cb34ae
SW
2885 } else {
2886 rec.v1.cap_id = cpu_to_le64(cap->cap_id);
2887 rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
2888 rec.v1.issued = cpu_to_le32(cap->issued);
2889 rec.v1.size = cpu_to_le64(inode->i_size);
2890 ceph_encode_timespec(&rec.v1.mtime, &inode->i_mtime);
2891 ceph_encode_timespec(&rec.v1.atime, &inode->i_atime);
2892 rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
2893 rec.v1.pathbase = cpu_to_le64(pathbase);
20cb34ae 2894 }
3469ed0d
YZ
2895
2896 if (list_empty(&ci->i_cap_snaps)) {
92776fd2 2897 snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
3469ed0d
YZ
2898 } else {
2899 struct ceph_cap_snap *capsnap =
2900 list_first_entry(&ci->i_cap_snaps,
2901 struct ceph_cap_snap, ci_item);
2902 snap_follows = capsnap->follows;
20cb34ae 2903 }
be655596 2904 spin_unlock(&ci->i_ceph_lock);
2f2dc053 2905
121f22a1 2906 if (recon_state->msg_version >= 2) {
40819f6f 2907 int num_fcntl_locks, num_flock_locks;
4deb14a2 2908 struct ceph_filelock *flocks = NULL;
121f22a1
YZ
2909 size_t struct_len, total_len = 0;
2910 u8 struct_v = 0;
39be95e9
JS
2911
2912encode_again:
b3f8d68f
YZ
2913 if (rec.v2.flock_len) {
2914 ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
2915 } else {
2916 num_fcntl_locks = 0;
2917 num_flock_locks = 0;
2918 }
4deb14a2
YZ
2919 if (num_fcntl_locks + num_flock_locks > 0) {
2920 flocks = kmalloc((num_fcntl_locks + num_flock_locks) *
2921 sizeof(struct ceph_filelock), GFP_NOFS);
2922 if (!flocks) {
2923 err = -ENOMEM;
2924 goto out_free;
2925 }
2926 err = ceph_encode_locks_to_buffer(inode, flocks,
2927 num_fcntl_locks,
2928 num_flock_locks);
2929 if (err) {
2930 kfree(flocks);
2931 flocks = NULL;
2932 if (err == -ENOSPC)
2933 goto encode_again;
2934 goto out_free;
2935 }
2936 } else {
39be95e9 2937 kfree(flocks);
4deb14a2 2938 flocks = NULL;
39be95e9 2939 }
121f22a1
YZ
2940
2941 if (recon_state->msg_version >= 3) {
2942 /* version, compat_version and struct_len */
2943 total_len = 2 * sizeof(u8) + sizeof(u32);
3469ed0d 2944 struct_v = 2;
121f22a1 2945 }
39be95e9
JS
2946 /*
2947 * number of encoded locks is stable, so copy to pagelist
2948 */
121f22a1
YZ
2949 struct_len = 2 * sizeof(u32) +
2950 (num_fcntl_locks + num_flock_locks) *
2951 sizeof(struct ceph_filelock);
2952 rec.v2.flock_len = cpu_to_le32(struct_len);
2953
2954 struct_len += sizeof(rec.v2);
2955 struct_len += sizeof(u32) + pathlen;
2956
3469ed0d
YZ
2957 if (struct_v >= 2)
2958 struct_len += sizeof(u64); /* snap_follows */
2959
121f22a1
YZ
2960 total_len += struct_len;
2961 err = ceph_pagelist_reserve(pagelist, total_len);
2962
2963 if (!err) {
2964 if (recon_state->msg_version >= 3) {
2965 ceph_pagelist_encode_8(pagelist, struct_v);
2966 ceph_pagelist_encode_8(pagelist, 1);
2967 ceph_pagelist_encode_32(pagelist, struct_len);
2968 }
2969 ceph_pagelist_encode_string(pagelist, path, pathlen);
2970 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
2971 ceph_locks_to_pagelist(flocks, pagelist,
2972 num_fcntl_locks,
2973 num_flock_locks);
3469ed0d
YZ
2974 if (struct_v >= 2)
2975 ceph_pagelist_encode_64(pagelist, snap_follows);
121f22a1 2976 }
39be95e9 2977 kfree(flocks);
3612abbd 2978 } else {
121f22a1
YZ
2979 size_t size = sizeof(u32) + pathlen + sizeof(rec.v1);
2980 err = ceph_pagelist_reserve(pagelist, size);
2981 if (!err) {
2982 ceph_pagelist_encode_string(pagelist, path, pathlen);
2983 ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
2984 }
40819f6f 2985 }
44c99757
YZ
2986
2987 recon_state->nr_caps++;
e072f8aa 2988out_free:
2f2dc053 2989 kfree(path);
e072f8aa 2990out_dput:
2f2dc053 2991 dput(dentry);
93cea5be 2992 return err;
2f2dc053
SW
2993}
2994
2995
2996/*
2997 * If an MDS fails and recovers, clients need to reconnect in order to
2998 * reestablish shared state. This includes all caps issued through
2999 * this session _and_ the snap_realm hierarchy. Because it's not
3000 * clear which snap realms the mds cares about, we send everything we
3001 * know about.. that ensures we'll then get any new info the
3002 * recovering MDS might have.
3003 *
3004 * This is a relatively heavyweight operation, but it's rare.
3005 *
3006 * called with mdsc->mutex held.
3007 */
34b6c855
SW
3008static void send_mds_reconnect(struct ceph_mds_client *mdsc,
3009 struct ceph_mds_session *session)
2f2dc053 3010{
2f2dc053 3011 struct ceph_msg *reply;
a105f00c 3012 struct rb_node *p;
34b6c855 3013 int mds = session->s_mds;
9abf82b8 3014 int err = -ENOMEM;
44c99757 3015 int s_nr_caps;
93cea5be 3016 struct ceph_pagelist *pagelist;
20cb34ae 3017 struct ceph_reconnect_state recon_state;
2f2dc053 3018
34b6c855 3019 pr_info("mds%d reconnect start\n", mds);
2f2dc053 3020
93cea5be
SW
3021 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
3022 if (!pagelist)
3023 goto fail_nopagelist;
3024 ceph_pagelist_init(pagelist);
3025
b61c2763 3026 reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, GFP_NOFS, false);
a79832f2 3027 if (!reply)
93cea5be 3028 goto fail_nomsg;
93cea5be 3029
34b6c855
SW
3030 mutex_lock(&session->s_mutex);
3031 session->s_state = CEPH_MDS_SESSION_RECONNECTING;
3032 session->s_seq = 0;
2f2dc053 3033
2f2dc053 3034 dout("session %p state %s\n", session,
a687ecaf 3035 ceph_session_state_name(session->s_state));
2f2dc053 3036
99a9c273
YZ
3037 spin_lock(&session->s_gen_ttl_lock);
3038 session->s_cap_gen++;
3039 spin_unlock(&session->s_gen_ttl_lock);
3040
3041 spin_lock(&session->s_cap_lock);
03f4fcb0
YZ
3042 /* don't know if session is readonly */
3043 session->s_readonly = 0;
99a9c273
YZ
3044 /*
3045 * notify __ceph_remove_cap() that we are composing cap reconnect.
3046 * If a cap get released before being added to the cap reconnect,
3047 * __ceph_remove_cap() should skip queuing cap release.
3048 */
3049 session->s_cap_reconnect = 1;
e01a5946 3050 /* drop old cap expires; we're about to reestablish that state */
745a8e3b 3051 cleanup_cap_releases(mdsc, session);
e01a5946 3052
5d23371f 3053 /* trim unused caps to reduce MDS's cache rejoin time */
c0bd50e2
YZ
3054 if (mdsc->fsc->sb->s_root)
3055 shrink_dcache_parent(mdsc->fsc->sb->s_root);
5d23371f
YZ
3056
3057 ceph_con_close(&session->s_con);
3058 ceph_con_open(&session->s_con,
3059 CEPH_ENTITY_TYPE_MDS, mds,
3060 ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
3061
3062 /* replay unsafe requests */
3063 replay_unsafe_requests(mdsc, session);
3064
3065 down_read(&mdsc->snap_rwsem);
3066
2f2dc053 3067 /* traverse this session's caps */
44c99757
YZ
3068 s_nr_caps = session->s_nr_caps;
3069 err = ceph_pagelist_encode_32(pagelist, s_nr_caps);
93cea5be
SW
3070 if (err)
3071 goto fail;
20cb34ae 3072
44c99757 3073 recon_state.nr_caps = 0;
20cb34ae 3074 recon_state.pagelist = pagelist;
121f22a1
YZ
3075 if (session->s_con.peer_features & CEPH_FEATURE_MDSENC)
3076 recon_state.msg_version = 3;
3077 else if (session->s_con.peer_features & CEPH_FEATURE_FLOCK)
3078 recon_state.msg_version = 2;
3079 else
3080 recon_state.msg_version = 1;
20cb34ae 3081 err = iterate_session_caps(session, encode_caps_cb, &recon_state);
2f2dc053 3082 if (err < 0)
9abf82b8 3083 goto fail;
2f2dc053 3084
99a9c273
YZ
3085 spin_lock(&session->s_cap_lock);
3086 session->s_cap_reconnect = 0;
3087 spin_unlock(&session->s_cap_lock);
3088
2f2dc053
SW
3089 /*
3090 * snaprealms. we provide mds with the ino, seq (version), and
3091 * parent for all of our realms. If the mds has any newer info,
3092 * it will tell us.
3093 */
a105f00c
SW
3094 for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
3095 struct ceph_snap_realm *realm =
3096 rb_entry(p, struct ceph_snap_realm, node);
93cea5be 3097 struct ceph_mds_snaprealm_reconnect sr_rec;
2f2dc053
SW
3098
3099 dout(" adding snap realm %llx seq %lld parent %llx\n",
3100 realm->ino, realm->seq, realm->parent_ino);
93cea5be
SW
3101 sr_rec.ino = cpu_to_le64(realm->ino);
3102 sr_rec.seq = cpu_to_le64(realm->seq);
3103 sr_rec.parent = cpu_to_le64(realm->parent_ino);
3104 err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
3105 if (err)
3106 goto fail;
2f2dc053 3107 }
2f2dc053 3108
121f22a1 3109 reply->hdr.version = cpu_to_le16(recon_state.msg_version);
44c99757
YZ
3110
3111 /* raced with cap release? */
3112 if (s_nr_caps != recon_state.nr_caps) {
3113 struct page *page = list_first_entry(&pagelist->head,
3114 struct page, lru);
3115 __le32 *addr = kmap_atomic(page);
3116 *addr = cpu_to_le32(recon_state.nr_caps);
3117 kunmap_atomic(addr);
ebf18f47 3118 }
44c99757
YZ
3119
3120 reply->hdr.data_len = cpu_to_le32(pagelist->length);
3121 ceph_msg_data_add_pagelist(reply, pagelist);
e548e9b9
YZ
3122
3123 ceph_early_kick_flushing_caps(mdsc, session);
3124
2f2dc053
SW
3125 ceph_con_send(&session->s_con, reply);
3126
9abf82b8
SW
3127 mutex_unlock(&session->s_mutex);
3128
3129 mutex_lock(&mdsc->mutex);
3130 __wake_requests(mdsc, &session->s_waiting);
3131 mutex_unlock(&mdsc->mutex);
3132
2f2dc053 3133 up_read(&mdsc->snap_rwsem);
2f2dc053
SW
3134 return;
3135
93cea5be 3136fail:
2f2dc053 3137 ceph_msg_put(reply);
9abf82b8
SW
3138 up_read(&mdsc->snap_rwsem);
3139 mutex_unlock(&session->s_mutex);
93cea5be
SW
3140fail_nomsg:
3141 ceph_pagelist_release(pagelist);
93cea5be 3142fail_nopagelist:
9abf82b8 3143 pr_err("error %d preparing reconnect for mds%d\n", err, mds);
9abf82b8 3144 return;
2f2dc053
SW
3145}
3146
3147
3148/*
3149 * compare old and new mdsmaps, kicking requests
3150 * and closing out old connections as necessary
3151 *
3152 * called under mdsc->mutex.
3153 */
3154static void check_new_map(struct ceph_mds_client *mdsc,
3155 struct ceph_mdsmap *newmap,
3156 struct ceph_mdsmap *oldmap)
3157{
3158 int i;
3159 int oldstate, newstate;
3160 struct ceph_mds_session *s;
3161
3162 dout("check_new_map new %u old %u\n",
3163 newmap->m_epoch, oldmap->m_epoch);
3164
76201b63 3165 for (i = 0; i < oldmap->m_num_mds && i < mdsc->max_sessions; i++) {
d37b1d99 3166 if (!mdsc->sessions[i])
2f2dc053
SW
3167 continue;
3168 s = mdsc->sessions[i];
3169 oldstate = ceph_mdsmap_get_state(oldmap, i);
3170 newstate = ceph_mdsmap_get_state(newmap, i);
3171
0deb01c9 3172 dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n",
2f2dc053 3173 i, ceph_mds_state_name(oldstate),
0deb01c9 3174 ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
2f2dc053 3175 ceph_mds_state_name(newstate),
0deb01c9 3176 ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
a687ecaf 3177 ceph_session_state_name(s->s_state));
2f2dc053 3178
76201b63 3179 if (i >= newmap->m_num_mds ||
3e8f43a0 3180 memcmp(ceph_mdsmap_get_addr(oldmap, i),
2f2dc053
SW
3181 ceph_mdsmap_get_addr(newmap, i),
3182 sizeof(struct ceph_entity_addr))) {
3183 if (s->s_state == CEPH_MDS_SESSION_OPENING) {
3184 /* the session never opened, just close it
3185 * out now */
0a07fc8c 3186 get_session(s);
2600d2dd 3187 __unregister_session(mdsc, s);
2f2dc053 3188 __wake_requests(mdsc, &s->s_waiting);
0a07fc8c 3189 ceph_put_mds_session(s);
2827528d
YZ
3190 } else if (i >= newmap->m_num_mds) {
3191 /* force close session for stopped mds */
3192 get_session(s);
2600d2dd 3193 __unregister_session(mdsc, s);
2827528d
YZ
3194 __wake_requests(mdsc, &s->s_waiting);
3195 kick_requests(mdsc, i);
3196 mutex_unlock(&mdsc->mutex);
3197
3198 mutex_lock(&s->s_mutex);
3199 cleanup_session_requests(mdsc, s);
3200 remove_session_caps(s);
3201 mutex_unlock(&s->s_mutex);
3202
3203 ceph_put_mds_session(s);
3204
3205 mutex_lock(&mdsc->mutex);
2f2dc053
SW
3206 } else {
3207 /* just close it */
3208 mutex_unlock(&mdsc->mutex);
3209 mutex_lock(&s->s_mutex);
3210 mutex_lock(&mdsc->mutex);
3211 ceph_con_close(&s->s_con);
3212 mutex_unlock(&s->s_mutex);
3213 s->s_state = CEPH_MDS_SESSION_RESTARTING;
3214 }
2f2dc053
SW
3215 } else if (oldstate == newstate) {
3216 continue; /* nothing new with this mds */
3217 }
3218
3219 /*
3220 * send reconnect?
3221 */
3222 if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
34b6c855
SW
3223 newstate >= CEPH_MDS_STATE_RECONNECT) {
3224 mutex_unlock(&mdsc->mutex);
3225 send_mds_reconnect(mdsc, s);
3226 mutex_lock(&mdsc->mutex);
3227 }
2f2dc053
SW
3228
3229 /*
29790f26 3230 * kick request on any mds that has gone active.
2f2dc053
SW
3231 */
3232 if (oldstate < CEPH_MDS_STATE_ACTIVE &&
3233 newstate >= CEPH_MDS_STATE_ACTIVE) {
29790f26
SW
3234 if (oldstate != CEPH_MDS_STATE_CREATING &&
3235 oldstate != CEPH_MDS_STATE_STARTING)
3236 pr_info("mds%d recovery completed\n", s->s_mds);
3237 kick_requests(mdsc, i);
2f2dc053 3238 ceph_kick_flushing_caps(mdsc, s);
0dc2570f 3239 wake_up_session_caps(s, 1);
2f2dc053
SW
3240 }
3241 }
cb170a22 3242
76201b63 3243 for (i = 0; i < newmap->m_num_mds && i < mdsc->max_sessions; i++) {
cb170a22
SW
3244 s = mdsc->sessions[i];
3245 if (!s)
3246 continue;
3247 if (!ceph_mdsmap_is_laggy(newmap, i))
3248 continue;
3249 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3250 s->s_state == CEPH_MDS_SESSION_HUNG ||
3251 s->s_state == CEPH_MDS_SESSION_CLOSING) {
3252 dout(" connecting to export targets of laggy mds%d\n",
3253 i);
3254 __open_export_target_sessions(mdsc, s);
3255 }
3256 }
2f2dc053
SW
3257}
3258
3259
3260
3261/*
3262 * leases
3263 */
3264
3265/*
3266 * caller must hold session s_mutex, dentry->d_lock
3267 */
3268void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
3269{
3270 struct ceph_dentry_info *di = ceph_dentry(dentry);
3271
3272 ceph_put_mds_session(di->lease_session);
3273 di->lease_session = NULL;
3274}
3275
2600d2dd
SW
3276static void handle_lease(struct ceph_mds_client *mdsc,
3277 struct ceph_mds_session *session,
3278 struct ceph_msg *msg)
2f2dc053 3279{
3d14c5d2 3280 struct super_block *sb = mdsc->fsc->sb;
2f2dc053 3281 struct inode *inode;
2f2dc053
SW
3282 struct dentry *parent, *dentry;
3283 struct ceph_dentry_info *di;
2600d2dd 3284 int mds = session->s_mds;
2f2dc053 3285 struct ceph_mds_lease *h = msg->front.iov_base;
1e5ea23d 3286 u32 seq;
2f2dc053 3287 struct ceph_vino vino;
2f2dc053
SW
3288 struct qstr dname;
3289 int release = 0;
3290
2f2dc053
SW
3291 dout("handle_lease from mds%d\n", mds);
3292
3293 /* decode */
3294 if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
3295 goto bad;
3296 vino.ino = le64_to_cpu(h->ino);
3297 vino.snap = CEPH_NOSNAP;
1e5ea23d 3298 seq = le32_to_cpu(h->seq);
2f2dc053
SW
3299 dname.name = (void *)h + sizeof(*h) + sizeof(u32);
3300 dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32);
3301 if (dname.len != get_unaligned_le32(h+1))
3302 goto bad;
3303
2f2dc053
SW
3304 /* lookup inode */
3305 inode = ceph_find_inode(sb, vino);
2f90b852
SW
3306 dout("handle_lease %s, ino %llx %p %.*s\n",
3307 ceph_lease_op_name(h->action), vino.ino, inode,
1e5ea23d 3308 dname.len, dname.name);
6cd3bcad
YZ
3309
3310 mutex_lock(&session->s_mutex);
3311 session->s_seq++;
3312
d37b1d99 3313 if (!inode) {
2f2dc053
SW
3314 dout("handle_lease no inode %llx\n", vino.ino);
3315 goto release;
3316 }
2f2dc053
SW
3317
3318 /* dentry */
3319 parent = d_find_alias(inode);
3320 if (!parent) {
3321 dout("no parent dentry on inode %p\n", inode);
3322 WARN_ON(1);
3323 goto release; /* hrm... */
3324 }
8387ff25 3325 dname.hash = full_name_hash(parent, dname.name, dname.len);
2f2dc053
SW
3326 dentry = d_lookup(parent, &dname);
3327 dput(parent);
3328 if (!dentry)
3329 goto release;
3330
3331 spin_lock(&dentry->d_lock);
3332 di = ceph_dentry(dentry);
3333 switch (h->action) {
3334 case CEPH_MDS_LEASE_REVOKE:
3d8eb7a9 3335 if (di->lease_session == session) {
1e5ea23d
SW
3336 if (ceph_seq_cmp(di->lease_seq, seq) > 0)
3337 h->seq = cpu_to_le32(di->lease_seq);
2f2dc053
SW
3338 __ceph_mdsc_drop_dentry_lease(dentry);
3339 }
3340 release = 1;
3341 break;
3342
3343 case CEPH_MDS_LEASE_RENEW:
3d8eb7a9 3344 if (di->lease_session == session &&
2f2dc053
SW
3345 di->lease_gen == session->s_cap_gen &&
3346 di->lease_renew_from &&
3347 di->lease_renew_after == 0) {
3348 unsigned long duration =
3563dbdd 3349 msecs_to_jiffies(le32_to_cpu(h->duration_ms));
2f2dc053 3350
1e5ea23d 3351 di->lease_seq = seq;
9b16f03c 3352 di->time = di->lease_renew_from + duration;
2f2dc053
SW
3353 di->lease_renew_after = di->lease_renew_from +
3354 (duration >> 1);
3355 di->lease_renew_from = 0;
3356 }
3357 break;
3358 }
3359 spin_unlock(&dentry->d_lock);
3360 dput(dentry);
3361
3362 if (!release)
3363 goto out;
3364
3365release:
3366 /* let's just reuse the same message */
3367 h->action = CEPH_MDS_LEASE_REVOKE_ACK;
3368 ceph_msg_get(msg);
3369 ceph_con_send(&session->s_con, msg);
3370
3371out:
3372 iput(inode);
3373 mutex_unlock(&session->s_mutex);
2f2dc053
SW
3374 return;
3375
3376bad:
3377 pr_err("corrupt lease message\n");
9ec7cab1 3378 ceph_msg_dump(msg);
2f2dc053
SW
3379}
3380
3381void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
3382 struct inode *inode,
3383 struct dentry *dentry, char action,
3384 u32 seq)
3385{
3386 struct ceph_msg *msg;
3387 struct ceph_mds_lease *lease;
3388 int len = sizeof(*lease) + sizeof(u32);
3389 int dnamelen = 0;
3390
3391 dout("lease_send_msg inode %p dentry %p %s to mds%d\n",
3392 inode, dentry, ceph_lease_op_name(action), session->s_mds);
3393 dnamelen = dentry->d_name.len;
3394 len += dnamelen;
3395
b61c2763 3396 msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
a79832f2 3397 if (!msg)
2f2dc053
SW
3398 return;
3399 lease = msg->front.iov_base;
3400 lease->action = action;
2f2dc053
SW
3401 lease->ino = cpu_to_le64(ceph_vino(inode).ino);
3402 lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap);
3403 lease->seq = cpu_to_le32(seq);
3404 put_unaligned_le32(dnamelen, lease + 1);
3405 memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen);
3406
3407 /*
3408 * if this is a preemptive lease RELEASE, no need to
3409 * flush request stream, since the actual request will
3410 * soon follow.
3411 */
3412 msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE);
3413
3414 ceph_con_send(&session->s_con, msg);
3415}
3416
2f2dc053
SW
3417/*
3418 * drop all leases (and dentry refs) in preparation for umount
3419 */
3420static void drop_leases(struct ceph_mds_client *mdsc)
3421{
3422 int i;
3423
3424 dout("drop_leases\n");
3425 mutex_lock(&mdsc->mutex);
3426 for (i = 0; i < mdsc->max_sessions; i++) {
3427 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
3428 if (!s)
3429 continue;
3430 mutex_unlock(&mdsc->mutex);
3431 mutex_lock(&s->s_mutex);
3432 mutex_unlock(&s->s_mutex);
3433 ceph_put_mds_session(s);
3434 mutex_lock(&mdsc->mutex);
3435 }
3436 mutex_unlock(&mdsc->mutex);
3437}
3438
3439
3440
3441/*
3442 * delayed work -- periodically trim expired leases, renew caps with mds
3443 */
3444static void schedule_delayed(struct ceph_mds_client *mdsc)
3445{
3446 int delay = 5;
3447 unsigned hz = round_jiffies_relative(HZ * delay);
3448 schedule_delayed_work(&mdsc->delayed_work, hz);
3449}
3450
3451static void delayed_work(struct work_struct *work)
3452{
3453 int i;
3454 struct ceph_mds_client *mdsc =
3455 container_of(work, struct ceph_mds_client, delayed_work.work);
3456 int renew_interval;
3457 int renew_caps;
3458
3459 dout("mdsc delayed_work\n");
afcdaea3 3460 ceph_check_delayed_caps(mdsc);
2f2dc053
SW
3461
3462 mutex_lock(&mdsc->mutex);
3463 renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
3464 renew_caps = time_after_eq(jiffies, HZ*renew_interval +
3465 mdsc->last_renew_caps);
3466 if (renew_caps)
3467 mdsc->last_renew_caps = jiffies;
3468
3469 for (i = 0; i < mdsc->max_sessions; i++) {
3470 struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
d37b1d99 3471 if (!s)
2f2dc053
SW
3472 continue;
3473 if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
3474 dout("resending session close request for mds%d\n",
3475 s->s_mds);
3476 request_close_session(mdsc, s);
3477 ceph_put_mds_session(s);
3478 continue;
3479 }
3480 if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
3481 if (s->s_state == CEPH_MDS_SESSION_OPEN) {
3482 s->s_state = CEPH_MDS_SESSION_HUNG;
3483 pr_info("mds%d hung\n", s->s_mds);
3484 }
3485 }
3486 if (s->s_state < CEPH_MDS_SESSION_OPEN) {
3487 /* this mds is failed or recovering, just wait */
3488 ceph_put_mds_session(s);
3489 continue;
3490 }
3491 mutex_unlock(&mdsc->mutex);
3492
3493 mutex_lock(&s->s_mutex);
3494 if (renew_caps)
3495 send_renew_caps(mdsc, s);
3496 else
3497 ceph_con_keepalive(&s->s_con);
aab53dd9
SW
3498 if (s->s_state == CEPH_MDS_SESSION_OPEN ||
3499 s->s_state == CEPH_MDS_SESSION_HUNG)
3d7ded4d 3500 ceph_send_cap_releases(mdsc, s);
2f2dc053
SW
3501 mutex_unlock(&s->s_mutex);
3502 ceph_put_mds_session(s);
3503
3504 mutex_lock(&mdsc->mutex);
3505 }
3506 mutex_unlock(&mdsc->mutex);
3507
3508 schedule_delayed(mdsc);
3509}
3510
3d14c5d2 3511int ceph_mdsc_init(struct ceph_fs_client *fsc)
2f2dc053 3512
2f2dc053 3513{
3d14c5d2
YS
3514 struct ceph_mds_client *mdsc;
3515
3516 mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS);
3517 if (!mdsc)
3518 return -ENOMEM;
3519 mdsc->fsc = fsc;
3520 fsc->mdsc = mdsc;
2f2dc053
SW
3521 mutex_init(&mdsc->mutex);
3522 mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS);
d37b1d99 3523 if (!mdsc->mdsmap) {
fb3101b6 3524 kfree(mdsc);
2d06eeb8 3525 return -ENOMEM;
fb3101b6 3526 }
2d06eeb8 3527
2f2dc053 3528 init_completion(&mdsc->safe_umount_waiters);
f3c60c59 3529 init_waitqueue_head(&mdsc->session_close_wq);
2f2dc053
SW
3530 INIT_LIST_HEAD(&mdsc->waiting_for_map);
3531 mdsc->sessions = NULL;
86d8f67b 3532 atomic_set(&mdsc->num_sessions, 0);
2f2dc053
SW
3533 mdsc->max_sessions = 0;
3534 mdsc->stopping = 0;
affbc19a 3535 mdsc->last_snap_seq = 0;
2f2dc053 3536 init_rwsem(&mdsc->snap_rwsem);
a105f00c 3537 mdsc->snap_realms = RB_ROOT;
2f2dc053
SW
3538 INIT_LIST_HEAD(&mdsc->snap_empty);
3539 spin_lock_init(&mdsc->snap_empty_lock);
3540 mdsc->last_tid = 0;
e8a7b8b1 3541 mdsc->oldest_tid = 0;
44ca18f2 3542 mdsc->request_tree = RB_ROOT;
2f2dc053
SW
3543 INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
3544 mdsc->last_renew_caps = jiffies;
3545 INIT_LIST_HEAD(&mdsc->cap_delay_list);
3546 spin_lock_init(&mdsc->cap_delay_lock);
3547 INIT_LIST_HEAD(&mdsc->snap_flush_list);
3548 spin_lock_init(&mdsc->snap_flush_lock);
553adfd9 3549 mdsc->last_cap_flush_tid = 1;
e4500b5e 3550 INIT_LIST_HEAD(&mdsc->cap_flush_list);
2f2dc053 3551 INIT_LIST_HEAD(&mdsc->cap_dirty);
db354052 3552 INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
2f2dc053
SW
3553 mdsc->num_cap_flushing = 0;
3554 spin_lock_init(&mdsc->cap_dirty_lock);
3555 init_waitqueue_head(&mdsc->cap_flushing_wq);
3556 spin_lock_init(&mdsc->dentry_lru_lock);
3557 INIT_LIST_HEAD(&mdsc->dentry_lru);
2d06eeb8 3558
37151668 3559 ceph_caps_init(mdsc);
3d14c5d2 3560 ceph_adjust_min_caps(mdsc, fsc->min_caps);
37151668 3561
10183a69
YZ
3562 init_rwsem(&mdsc->pool_perm_rwsem);
3563 mdsc->pool_perm_tree = RB_ROOT;
3564
717e6f28
YZ
3565 strncpy(mdsc->nodename, utsname()->nodename,
3566 sizeof(mdsc->nodename) - 1);
5f44f142 3567 return 0;
2f2dc053
SW
3568}
3569
3570/*
3571 * Wait for safe replies on open mds requests. If we time out, drop
3572 * all requests from the tree to avoid dangling dentry refs.
3573 */
3574static void wait_requests(struct ceph_mds_client *mdsc)
3575{
a319bf56 3576 struct ceph_options *opts = mdsc->fsc->client->options;
2f2dc053 3577 struct ceph_mds_request *req;
2f2dc053
SW
3578
3579 mutex_lock(&mdsc->mutex);
44ca18f2 3580 if (__get_oldest_req(mdsc)) {
2f2dc053 3581 mutex_unlock(&mdsc->mutex);
44ca18f2 3582
2f2dc053
SW
3583 dout("wait_requests waiting for requests\n");
3584 wait_for_completion_timeout(&mdsc->safe_umount_waiters,
a319bf56 3585 ceph_timeout_jiffies(opts->mount_timeout));
2f2dc053
SW
3586
3587 /* tear down remaining requests */
44ca18f2
SW
3588 mutex_lock(&mdsc->mutex);
3589 while ((req = __get_oldest_req(mdsc))) {
2f2dc053
SW
3590 dout("wait_requests timed out on tid %llu\n",
3591 req->r_tid);
44ca18f2 3592 __unregister_request(mdsc, req);
2f2dc053
SW
3593 }
3594 }
3595 mutex_unlock(&mdsc->mutex);
3596 dout("wait_requests done\n");
3597}
3598
3599/*
3600 * called before mount is ro, and before dentries are torn down.
3601 * (hmm, does this still race with new lookups?)
3602 */
3603void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
3604{
3605 dout("pre_umount\n");
3606 mdsc->stopping = 1;
3607
3608 drop_leases(mdsc);
afcdaea3 3609 ceph_flush_dirty_caps(mdsc);
2f2dc053 3610 wait_requests(mdsc);
17c688c3
SW
3611
3612 /*
3613 * wait for reply handlers to drop their request refs and
3614 * their inode/dcache refs
3615 */
3616 ceph_msgr_flush();
2f2dc053
SW
3617}
3618
3619/*
3620 * wait for all write mds requests to flush.
3621 */
3622static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid)
3623{
80fc7314 3624 struct ceph_mds_request *req = NULL, *nextreq;
44ca18f2 3625 struct rb_node *n;
2f2dc053
SW
3626
3627 mutex_lock(&mdsc->mutex);
3628 dout("wait_unsafe_requests want %lld\n", want_tid);
80fc7314 3629restart:
44ca18f2
SW
3630 req = __get_oldest_req(mdsc);
3631 while (req && req->r_tid <= want_tid) {
80fc7314
SW
3632 /* find next request */
3633 n = rb_next(&req->r_node);
3634 if (n)
3635 nextreq = rb_entry(n, struct ceph_mds_request, r_node);
3636 else
3637 nextreq = NULL;
e8a7b8b1
YZ
3638 if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
3639 (req->r_op & CEPH_MDS_OP_WRITE)) {
44ca18f2
SW
3640 /* write op */
3641 ceph_mdsc_get_request(req);
80fc7314
SW
3642 if (nextreq)
3643 ceph_mdsc_get_request(nextreq);
44ca18f2
SW
3644 mutex_unlock(&mdsc->mutex);
3645 dout("wait_unsafe_requests wait on %llu (want %llu)\n",
3646 req->r_tid, want_tid);
3647 wait_for_completion(&req->r_safe_completion);
3648 mutex_lock(&mdsc->mutex);
44ca18f2 3649 ceph_mdsc_put_request(req);
80fc7314
SW
3650 if (!nextreq)
3651 break; /* next dne before, so we're done! */
3652 if (RB_EMPTY_NODE(&nextreq->r_node)) {
3653 /* next request was removed from tree */
3654 ceph_mdsc_put_request(nextreq);
3655 goto restart;
3656 }
3657 ceph_mdsc_put_request(nextreq); /* won't go away */
44ca18f2 3658 }
80fc7314 3659 req = nextreq;
2f2dc053
SW
3660 }
3661 mutex_unlock(&mdsc->mutex);
3662 dout("wait_unsafe_requests done\n");
3663}
3664
3665void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
3666{
0e294387 3667 u64 want_tid, want_flush;
2f2dc053 3668
52953d55 3669 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
56b7cf95
SW
3670 return;
3671
2f2dc053
SW
3672 dout("sync\n");
3673 mutex_lock(&mdsc->mutex);
3674 want_tid = mdsc->last_tid;
2f2dc053 3675 mutex_unlock(&mdsc->mutex);
2f2dc053 3676
afcdaea3 3677 ceph_flush_dirty_caps(mdsc);
d3383a8e 3678 spin_lock(&mdsc->cap_dirty_lock);
8310b089 3679 want_flush = mdsc->last_cap_flush_tid;
c8799fc4
YZ
3680 if (!list_empty(&mdsc->cap_flush_list)) {
3681 struct ceph_cap_flush *cf =
3682 list_last_entry(&mdsc->cap_flush_list,
3683 struct ceph_cap_flush, g_list);
3684 cf->wake = true;
3685 }
d3383a8e
YZ
3686 spin_unlock(&mdsc->cap_dirty_lock);
3687
0e294387
YZ
3688 dout("sync want tid %lld flush_seq %lld\n",
3689 want_tid, want_flush);
2f2dc053
SW
3690
3691 wait_unsafe_requests(mdsc, want_tid);
0e294387 3692 wait_caps_flush(mdsc, want_flush);
2f2dc053
SW
3693}
3694
f3c60c59
SW
3695/*
3696 * true if all sessions are closed, or we force unmount
3697 */
fcff415c 3698static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
f3c60c59 3699{
52953d55 3700 if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
f3c60c59 3701 return true;
fcff415c 3702 return atomic_read(&mdsc->num_sessions) <= skipped;
f3c60c59 3703}
2f2dc053
SW
3704
3705/*
3706 * called after sb is ro.
3707 */
3708void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
3709{
a319bf56 3710 struct ceph_options *opts = mdsc->fsc->client->options;
2f2dc053
SW
3711 struct ceph_mds_session *session;
3712 int i;
fcff415c 3713 int skipped = 0;
2f2dc053
SW
3714
3715 dout("close_sessions\n");
3716
2f2dc053 3717 /* close sessions */
f3c60c59
SW
3718 mutex_lock(&mdsc->mutex);
3719 for (i = 0; i < mdsc->max_sessions; i++) {
3720 session = __ceph_lookup_mds_session(mdsc, i);
3721 if (!session)
3722 continue;
2f2dc053 3723 mutex_unlock(&mdsc->mutex);
f3c60c59 3724 mutex_lock(&session->s_mutex);
fcff415c
YZ
3725 if (__close_session(mdsc, session) <= 0)
3726 skipped++;
f3c60c59
SW
3727 mutex_unlock(&session->s_mutex);
3728 ceph_put_mds_session(session);
2f2dc053
SW
3729 mutex_lock(&mdsc->mutex);
3730 }
f3c60c59
SW
3731 mutex_unlock(&mdsc->mutex);
3732
3733 dout("waiting for sessions to close\n");
fcff415c
YZ
3734 wait_event_timeout(mdsc->session_close_wq,
3735 done_closing_sessions(mdsc, skipped),
a319bf56 3736 ceph_timeout_jiffies(opts->mount_timeout));
2f2dc053
SW
3737
3738 /* tear down remaining sessions */
f3c60c59 3739 mutex_lock(&mdsc->mutex);
2f2dc053
SW
3740 for (i = 0; i < mdsc->max_sessions; i++) {
3741 if (mdsc->sessions[i]) {
3742 session = get_session(mdsc->sessions[i]);
2600d2dd 3743 __unregister_session(mdsc, session);
2f2dc053
SW
3744 mutex_unlock(&mdsc->mutex);
3745 mutex_lock(&session->s_mutex);
3746 remove_session_caps(session);
3747 mutex_unlock(&session->s_mutex);
3748 ceph_put_mds_session(session);
3749 mutex_lock(&mdsc->mutex);
3750 }
3751 }
2f2dc053 3752 WARN_ON(!list_empty(&mdsc->cap_delay_list));
2f2dc053
SW
3753 mutex_unlock(&mdsc->mutex);
3754
3755 ceph_cleanup_empty_realms(mdsc);
3756
3757 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3758
3759 dout("stopped\n");
3760}
3761
48fec5d0
YZ
3762void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
3763{
3764 struct ceph_mds_session *session;
3765 int mds;
3766
3767 dout("force umount\n");
3768
3769 mutex_lock(&mdsc->mutex);
3770 for (mds = 0; mds < mdsc->max_sessions; mds++) {
3771 session = __ceph_lookup_mds_session(mdsc, mds);
3772 if (!session)
3773 continue;
3774 mutex_unlock(&mdsc->mutex);
3775 mutex_lock(&session->s_mutex);
3776 __close_session(mdsc, session);
3777 if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
3778 cleanup_session_requests(mdsc, session);
3779 remove_session_caps(session);
3780 }
3781 mutex_unlock(&session->s_mutex);
3782 ceph_put_mds_session(session);
3783 mutex_lock(&mdsc->mutex);
3784 kick_requests(mdsc, mds);
3785 }
3786 __wake_requests(mdsc, &mdsc->waiting_for_map);
3787 mutex_unlock(&mdsc->mutex);
3788}
3789
3d14c5d2 3790static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
2f2dc053
SW
3791{
3792 dout("stop\n");
3793 cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
3794 if (mdsc->mdsmap)
3795 ceph_mdsmap_destroy(mdsc->mdsmap);
3796 kfree(mdsc->sessions);
37151668 3797 ceph_caps_finalize(mdsc);
10183a69 3798 ceph_pool_perm_destroy(mdsc);
2f2dc053
SW
3799}
3800
3d14c5d2
YS
3801void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
3802{
3803 struct ceph_mds_client *mdsc = fsc->mdsc;
ef550f6f 3804 dout("mdsc_destroy %p\n", mdsc);
ef550f6f
SW
3805
3806 /* flush out any connection work with references to us */
3807 ceph_msgr_flush();
3808
62a65f36
YZ
3809 ceph_mdsc_stop(mdsc);
3810
3d14c5d2
YS
3811 fsc->mdsc = NULL;
3812 kfree(mdsc);
ef550f6f 3813 dout("mdsc_destroy %p done\n", mdsc);
3d14c5d2
YS
3814}
3815
430afbad
YZ
3816void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
3817{
3818 struct ceph_fs_client *fsc = mdsc->fsc;
3819 const char *mds_namespace = fsc->mount_options->mds_namespace;
3820 void *p = msg->front.iov_base;
3821 void *end = p + msg->front.iov_len;
3822 u32 epoch;
3823 u32 map_len;
3824 u32 num_fs;
3825 u32 mount_fscid = (u32)-1;
3826 u8 struct_v, struct_cv;
3827 int err = -EINVAL;
3828
3829 ceph_decode_need(&p, end, sizeof(u32), bad);
3830 epoch = ceph_decode_32(&p);
3831
3832 dout("handle_fsmap epoch %u\n", epoch);
3833
3834 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
3835 struct_v = ceph_decode_8(&p);
3836 struct_cv = ceph_decode_8(&p);
3837 map_len = ceph_decode_32(&p);
3838
3839 ceph_decode_need(&p, end, sizeof(u32) * 3, bad);
3840 p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */
3841
3842 num_fs = ceph_decode_32(&p);
3843 while (num_fs-- > 0) {
3844 void *info_p, *info_end;
3845 u32 info_len;
3846 u8 info_v, info_cv;
3847 u32 fscid, namelen;
3848
3849 ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
3850 info_v = ceph_decode_8(&p);
3851 info_cv = ceph_decode_8(&p);
3852 info_len = ceph_decode_32(&p);
3853 ceph_decode_need(&p, end, info_len, bad);
3854 info_p = p;
3855 info_end = p + info_len;
3856 p = info_end;
3857
3858 ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
3859 fscid = ceph_decode_32(&info_p);
3860 namelen = ceph_decode_32(&info_p);
3861 ceph_decode_need(&info_p, info_end, namelen, bad);
3862
3863 if (mds_namespace &&
3864 strlen(mds_namespace) == namelen &&
3865 !strncmp(mds_namespace, (char *)info_p, namelen)) {
3866 mount_fscid = fscid;
3867 break;
3868 }
3869 }
3870
3871 ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
3872 if (mount_fscid != (u32)-1) {
3873 fsc->client->monc.fs_cluster_id = mount_fscid;
3874 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
3875 0, true);
3876 ceph_monc_renew_subs(&fsc->client->monc);
3877 } else {
3878 err = -ENOENT;
3879 goto err_out;
3880 }
3881 return;
76bd6ec4 3882
430afbad
YZ
3883bad:
3884 pr_err("error decoding fsmap\n");
3885err_out:
3886 mutex_lock(&mdsc->mutex);
76bd6ec4 3887 mdsc->mdsmap_err = err;
430afbad
YZ
3888 __wake_requests(mdsc, &mdsc->waiting_for_map);
3889 mutex_unlock(&mdsc->mutex);
430afbad 3890}
2f2dc053
SW
3891
3892/*
3893 * handle mds map update.
3894 */
430afbad 3895void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
2f2dc053
SW
3896{
3897 u32 epoch;
3898 u32 maplen;
3899 void *p = msg->front.iov_base;
3900 void *end = p + msg->front.iov_len;
3901 struct ceph_mdsmap *newmap, *oldmap;
3902 struct ceph_fsid fsid;
3903 int err = -EINVAL;
3904
3905 ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
3906 ceph_decode_copy(&p, &fsid, sizeof(fsid));
3d14c5d2 3907 if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
0743304d 3908 return;
c89136ea
SW
3909 epoch = ceph_decode_32(&p);
3910 maplen = ceph_decode_32(&p);
2f2dc053
SW
3911 dout("handle_map epoch %u len %d\n", epoch, (int)maplen);
3912
3913 /* do we need it? */
2f2dc053
SW
3914 mutex_lock(&mdsc->mutex);
3915 if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
3916 dout("handle_map epoch %u <= our %u\n",
3917 epoch, mdsc->mdsmap->m_epoch);
3918 mutex_unlock(&mdsc->mutex);
3919 return;
3920 }
3921
3922 newmap = ceph_mdsmap_decode(&p, end);
3923 if (IS_ERR(newmap)) {
3924 err = PTR_ERR(newmap);
3925 goto bad_unlock;
3926 }
3927
3928 /* swap into place */
3929 if (mdsc->mdsmap) {
3930 oldmap = mdsc->mdsmap;
3931 mdsc->mdsmap = newmap;
3932 check_new_map(mdsc, newmap, oldmap);
3933 ceph_mdsmap_destroy(oldmap);
3934 } else {
3935 mdsc->mdsmap = newmap; /* first mds map */
3936 }
3d14c5d2 3937 mdsc->fsc->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size;
2f2dc053
SW
3938
3939 __wake_requests(mdsc, &mdsc->waiting_for_map);
82dcabad
ID
3940 ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
3941 mdsc->mdsmap->m_epoch);
2f2dc053
SW
3942
3943 mutex_unlock(&mdsc->mutex);
3944 schedule_delayed(mdsc);
3945 return;
3946
3947bad_unlock:
3948 mutex_unlock(&mdsc->mutex);
3949bad:
3950 pr_err("error decoding mdsmap %d\n", err);
3951 return;
3952}
3953
3954static struct ceph_connection *con_get(struct ceph_connection *con)
3955{
3956 struct ceph_mds_session *s = con->private;
3957
3958 if (get_session(s)) {
3997c01d 3959 dout("mdsc con_get %p ok (%d)\n", s, refcount_read(&s->s_ref));
2f2dc053
SW
3960 return con;
3961 }
3962 dout("mdsc con_get %p FAIL\n", s);
3963 return NULL;
3964}
3965
3966static void con_put(struct ceph_connection *con)
3967{
3968 struct ceph_mds_session *s = con->private;
3969
3997c01d 3970 dout("mdsc con_put %p (%d)\n", s, refcount_read(&s->s_ref) - 1);
2f2dc053
SW
3971 ceph_put_mds_session(s);
3972}
3973
3974/*
3975 * if the client is unresponsive for long enough, the mds will kill
3976 * the session entirely.
3977 */
3978static void peer_reset(struct ceph_connection *con)
3979{
3980 struct ceph_mds_session *s = con->private;
7e70f0ed 3981 struct ceph_mds_client *mdsc = s->s_mdsc;
2f2dc053 3982
f3ae1b97 3983 pr_warn("mds%d closed our session\n", s->s_mds);
7e70f0ed 3984 send_mds_reconnect(mdsc, s);
2f2dc053
SW
3985}
3986
3987static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
3988{
3989 struct ceph_mds_session *s = con->private;
3990 struct ceph_mds_client *mdsc = s->s_mdsc;
3991 int type = le16_to_cpu(msg->hdr.type);
3992
2600d2dd
SW
3993 mutex_lock(&mdsc->mutex);
3994 if (__verify_registered_session(mdsc, s) < 0) {
3995 mutex_unlock(&mdsc->mutex);
3996 goto out;
3997 }
3998 mutex_unlock(&mdsc->mutex);
3999
2f2dc053
SW
4000 switch (type) {
4001 case CEPH_MSG_MDS_MAP:
430afbad
YZ
4002 ceph_mdsc_handle_mdsmap(mdsc, msg);
4003 break;
4004 case CEPH_MSG_FS_MAP_USER:
4005 ceph_mdsc_handle_fsmap(mdsc, msg);
2f2dc053
SW
4006 break;
4007 case CEPH_MSG_CLIENT_SESSION:
4008 handle_session(s, msg);
4009 break;
4010 case CEPH_MSG_CLIENT_REPLY:
4011 handle_reply(s, msg);
4012 break;
4013 case CEPH_MSG_CLIENT_REQUEST_FORWARD:
2600d2dd 4014 handle_forward(mdsc, s, msg);
2f2dc053
SW
4015 break;
4016 case CEPH_MSG_CLIENT_CAPS:
4017 ceph_handle_caps(s, msg);
4018 break;
4019 case CEPH_MSG_CLIENT_SNAP:
2600d2dd 4020 ceph_handle_snap(mdsc, s, msg);
2f2dc053
SW
4021 break;
4022 case CEPH_MSG_CLIENT_LEASE:
2600d2dd 4023 handle_lease(mdsc, s, msg);
2f2dc053
SW
4024 break;
4025
4026 default:
4027 pr_err("received unknown message type %d %s\n", type,
4028 ceph_msg_type_name(type));
4029 }
2600d2dd 4030out:
2f2dc053
SW
4031 ceph_msg_put(msg);
4032}
4033
4e7a5dcd
SW
4034/*
4035 * authentication
4036 */
a3530df3
AE
4037
4038/*
4039 * Note: returned pointer is the address of a structure that's
4040 * managed separately. Caller must *not* attempt to free it.
4041 */
4042static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
8f43fb53 4043 int *proto, int force_new)
4e7a5dcd
SW
4044{
4045 struct ceph_mds_session *s = con->private;
4046 struct ceph_mds_client *mdsc = s->s_mdsc;
3d14c5d2 4047 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
74f1869f 4048 struct ceph_auth_handshake *auth = &s->s_auth;
4e7a5dcd 4049
74f1869f 4050 if (force_new && auth->authorizer) {
6c1ea260 4051 ceph_auth_destroy_authorizer(auth->authorizer);
74f1869f 4052 auth->authorizer = NULL;
4e7a5dcd 4053 }
27859f97
SW
4054 if (!auth->authorizer) {
4055 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4056 auth);
0bed9b5c
SW
4057 if (ret)
4058 return ERR_PTR(ret);
27859f97
SW
4059 } else {
4060 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS,
4061 auth);
a255651d 4062 if (ret)
a3530df3 4063 return ERR_PTR(ret);
4e7a5dcd 4064 }
4e7a5dcd 4065 *proto = ac->protocol;
74f1869f 4066
a3530df3 4067 return auth;
4e7a5dcd
SW
4068}
4069
4070
0dde5848 4071static int verify_authorizer_reply(struct ceph_connection *con)
4e7a5dcd
SW
4072{
4073 struct ceph_mds_session *s = con->private;
4074 struct ceph_mds_client *mdsc = s->s_mdsc;
3d14c5d2 4075 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
4e7a5dcd 4076
0dde5848 4077 return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer);
4e7a5dcd
SW
4078}
4079
9bd2e6f8
SW
4080static int invalidate_authorizer(struct ceph_connection *con)
4081{
4082 struct ceph_mds_session *s = con->private;
4083 struct ceph_mds_client *mdsc = s->s_mdsc;
3d14c5d2 4084 struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
9bd2e6f8 4085
27859f97 4086 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
9bd2e6f8 4087
3d14c5d2 4088 return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
9bd2e6f8
SW
4089}
4090
53ded495
AE
4091static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
4092 struct ceph_msg_header *hdr, int *skip)
4093{
4094 struct ceph_msg *msg;
4095 int type = (int) le16_to_cpu(hdr->type);
4096 int front_len = (int) le32_to_cpu(hdr->front_len);
4097
4098 if (con->in_msg)
4099 return con->in_msg;
4100
4101 *skip = 0;
4102 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
4103 if (!msg) {
4104 pr_err("unable to allocate msg type %d len %d\n",
4105 type, front_len);
4106 return NULL;
4107 }
53ded495
AE
4108
4109 return msg;
4110}
4111
79dbd1ba 4112static int mds_sign_message(struct ceph_msg *msg)
33d07337 4113{
79dbd1ba 4114 struct ceph_mds_session *s = msg->con->private;
33d07337 4115 struct ceph_auth_handshake *auth = &s->s_auth;
79dbd1ba 4116
33d07337
YZ
4117 return ceph_auth_sign_message(auth, msg);
4118}
4119
79dbd1ba 4120static int mds_check_message_signature(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_check_message_signature(auth, msg);
4126}
4127
9e32789f 4128static const struct ceph_connection_operations mds_con_ops = {
2f2dc053
SW
4129 .get = con_get,
4130 .put = con_put,
4131 .dispatch = dispatch,
4e7a5dcd
SW
4132 .get_authorizer = get_authorizer,
4133 .verify_authorizer_reply = verify_authorizer_reply,
9bd2e6f8 4134 .invalidate_authorizer = invalidate_authorizer,
2f2dc053 4135 .peer_reset = peer_reset,
53ded495 4136 .alloc_msg = mds_alloc_msg,
79dbd1ba
ID
4137 .sign_message = mds_sign_message,
4138 .check_message_signature = mds_check_message_signature,
2f2dc053
SW
4139};
4140
2f2dc053 4141/* eof */