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