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