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