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