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