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