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