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