1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/ceph/ceph_debug.h> 3 4 #include <linux/module.h> 5 #include <linux/types.h> 6 #include <linux/slab.h> 7 #include <linux/random.h> 8 #include <linux/sched.h> 9 10 #include <linux/ceph/ceph_features.h> 11 #include <linux/ceph/mon_client.h> 12 #include <linux/ceph/libceph.h> 13 #include <linux/ceph/debugfs.h> 14 #include <linux/ceph/decode.h> 15 #include <linux/ceph/auth.h> 16 17 /* 18 * Interact with Ceph monitor cluster. Handle requests for new map 19 * versions, and periodically resend as needed. Also implement 20 * statfs() and umount(). 21 * 22 * A small cluster of Ceph "monitors" are responsible for managing critical 23 * cluster configuration and state information. An odd number (e.g., 3, 5) 24 * of cmon daemons use a modified version of the Paxos part-time parliament 25 * algorithm to manage the MDS map (mds cluster membership), OSD map, and 26 * list of clients who have mounted the file system. 27 * 28 * We maintain an open, active session with a monitor at all times in order to 29 * receive timely MDSMap updates. We periodically send a keepalive byte on the 30 * TCP socket to ensure we detect a failure. If the connection does break, we 31 * randomly hunt for a new monitor. Once the connection is reestablished, we 32 * resend any outstanding requests. 33 */ 34 35 static const struct ceph_connection_operations mon_con_ops; 36 37 static int __validate_auth(struct ceph_mon_client *monc); 38 39 static int decode_mon_info(void **p, void *end, bool msgr2, 40 struct ceph_entity_addr *addr) 41 { 42 void *mon_info_end; 43 u32 struct_len; 44 u8 struct_v; 45 int ret; 46 47 ret = ceph_start_decoding(p, end, 1, "mon_info_t", &struct_v, 48 &struct_len); 49 if (ret) 50 return ret; 51 52 mon_info_end = *p + struct_len; 53 ceph_decode_skip_string(p, end, e_inval); /* skip mon name */ 54 ret = ceph_decode_entity_addrvec(p, end, msgr2, addr); 55 if (ret) 56 return ret; 57 58 *p = mon_info_end; 59 return 0; 60 61 e_inval: 62 return -EINVAL; 63 } 64 65 /* 66 * Decode a monmap blob (e.g., during mount). 67 * 68 * Assume MonMap v3 (i.e. encoding with MONNAMES and MONENC). 69 */ 70 static struct ceph_monmap *ceph_monmap_decode(void **p, void *end, bool msgr2) 71 { 72 struct ceph_monmap *monmap = NULL; 73 struct ceph_fsid fsid; 74 u32 struct_len; 75 u32 blob_len; 76 u32 num_mon; 77 u8 struct_v; 78 u32 epoch; 79 int ret; 80 int i; 81 82 ceph_decode_32_safe(p, end, blob_len, e_inval); 83 ceph_decode_need(p, end, blob_len, e_inval); 84 85 ret = ceph_start_decoding(p, end, 6, "monmap", &struct_v, &struct_len); 86 if (ret) 87 goto fail; 88 89 dout("%s struct_v %d\n", __func__, struct_v); 90 ceph_decode_copy_safe(p, end, &fsid, sizeof(fsid), e_inval); 91 ceph_decode_32_safe(p, end, epoch, e_inval); 92 if (struct_v >= 6) { 93 u32 feat_struct_len; 94 u8 feat_struct_v; 95 96 *p += sizeof(struct ceph_timespec); /* skip last_changed */ 97 *p += sizeof(struct ceph_timespec); /* skip created */ 98 99 ret = ceph_start_decoding(p, end, 1, "mon_feature_t", 100 &feat_struct_v, &feat_struct_len); 101 if (ret) 102 goto fail; 103 104 *p += feat_struct_len; /* skip persistent_features */ 105 106 ret = ceph_start_decoding(p, end, 1, "mon_feature_t", 107 &feat_struct_v, &feat_struct_len); 108 if (ret) 109 goto fail; 110 111 *p += feat_struct_len; /* skip optional_features */ 112 } 113 ceph_decode_32_safe(p, end, num_mon, e_inval); 114 115 dout("%s fsid %pU epoch %u num_mon %u\n", __func__, &fsid, epoch, 116 num_mon); 117 if (num_mon > CEPH_MAX_MON) 118 goto e_inval; 119 120 monmap = kmalloc_flex(*monmap, mon_inst, num_mon, GFP_NOIO); 121 if (!monmap) { 122 ret = -ENOMEM; 123 goto fail; 124 } 125 monmap->fsid = fsid; 126 monmap->epoch = epoch; 127 monmap->num_mon = num_mon; 128 129 /* legacy_mon_addr map or mon_info map */ 130 for (i = 0; i < num_mon; i++) { 131 struct ceph_entity_inst *inst = &monmap->mon_inst[i]; 132 133 ceph_decode_skip_string(p, end, e_inval); /* skip mon name */ 134 inst->name.type = CEPH_ENTITY_TYPE_MON; 135 inst->name.num = cpu_to_le64(i); 136 137 if (struct_v >= 6) 138 ret = decode_mon_info(p, end, msgr2, &inst->addr); 139 else 140 ret = ceph_decode_entity_addr(p, end, &inst->addr); 141 if (ret) 142 goto fail; 143 144 dout("%s mon%d addr %s\n", __func__, i, 145 ceph_pr_addr(&inst->addr)); 146 } 147 148 return monmap; 149 150 e_inval: 151 ret = -EINVAL; 152 fail: 153 kfree(monmap); 154 return ERR_PTR(ret); 155 } 156 157 /* 158 * return true if *addr is included in the monmap. 159 */ 160 int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr) 161 { 162 int i; 163 164 for (i = 0; i < m->num_mon; i++) { 165 if (ceph_addr_equal_no_type(addr, &m->mon_inst[i].addr)) 166 return 1; 167 } 168 169 return 0; 170 } 171 172 /* 173 * Send an auth request. 174 */ 175 static void __send_prepared_auth_request(struct ceph_mon_client *monc, int len) 176 { 177 BUG_ON(len > monc->m_auth->front_alloc_len); 178 179 monc->pending_auth = 1; 180 monc->m_auth->front.iov_len = len; 181 monc->m_auth->hdr.front_len = cpu_to_le32(len); 182 ceph_msg_revoke(monc->m_auth); 183 ceph_msg_get(monc->m_auth); /* keep our ref */ 184 ceph_con_send(&monc->con, monc->m_auth); 185 } 186 187 /* 188 * Close monitor session, if any. 189 */ 190 static void __close_session(struct ceph_mon_client *monc) 191 { 192 dout("__close_session closing mon%d\n", monc->cur_mon); 193 ceph_msg_revoke(monc->m_auth); 194 ceph_msg_revoke_incoming(monc->m_auth_reply); 195 ceph_msg_revoke(monc->m_subscribe); 196 ceph_msg_revoke_incoming(monc->m_subscribe_ack); 197 ceph_con_close(&monc->con); 198 199 monc->pending_auth = 0; 200 ceph_auth_reset(monc->auth); 201 } 202 203 /* 204 * Pick a new monitor at random and set cur_mon. If we are repicking 205 * (i.e. cur_mon is already set), be sure to pick a different one. 206 */ 207 static void pick_new_mon(struct ceph_mon_client *monc) 208 { 209 int old_mon = monc->cur_mon; 210 211 BUG_ON(monc->monmap->num_mon < 1); 212 213 if (monc->monmap->num_mon == 1) { 214 monc->cur_mon = 0; 215 } else { 216 int max = monc->monmap->num_mon; 217 int o = -1; 218 int n; 219 220 if (monc->cur_mon >= 0) { 221 if (monc->cur_mon < monc->monmap->num_mon) 222 o = monc->cur_mon; 223 if (o >= 0) 224 max--; 225 } 226 227 n = get_random_u32_below(max); 228 if (o >= 0 && n >= o) 229 n++; 230 231 monc->cur_mon = n; 232 } 233 234 dout("%s mon%d -> mon%d out of %d mons\n", __func__, old_mon, 235 monc->cur_mon, monc->monmap->num_mon); 236 } 237 238 /* 239 * Open a session with a new monitor. 240 */ 241 static void __open_session(struct ceph_mon_client *monc) 242 { 243 int ret; 244 245 pick_new_mon(monc); 246 247 monc->hunting = true; 248 if (monc->had_a_connection) { 249 monc->hunt_mult *= CEPH_MONC_HUNT_BACKOFF; 250 if (monc->hunt_mult > CEPH_MONC_HUNT_MAX_MULT) 251 monc->hunt_mult = CEPH_MONC_HUNT_MAX_MULT; 252 } 253 254 monc->sub_renew_after = jiffies; /* i.e., expired */ 255 monc->sub_renew_sent = 0; 256 257 dout("%s opening mon%d\n", __func__, monc->cur_mon); 258 ceph_con_open(&monc->con, CEPH_ENTITY_TYPE_MON, monc->cur_mon, 259 &monc->monmap->mon_inst[monc->cur_mon].addr); 260 261 /* 262 * Queue a keepalive to ensure that in case of an early fault 263 * the messenger doesn't put us into STANDBY state and instead 264 * retries. This also ensures that our timestamp is valid by 265 * the time we finish hunting and delayed_work() checks it. 266 */ 267 ceph_con_keepalive(&monc->con); 268 if (ceph_msgr2(monc->client)) { 269 monc->pending_auth = 1; 270 return; 271 } 272 273 /* initiate authentication handshake */ 274 ret = ceph_auth_build_hello(monc->auth, 275 monc->m_auth->front.iov_base, 276 monc->m_auth->front_alloc_len); 277 BUG_ON(ret <= 0); 278 __send_prepared_auth_request(monc, ret); 279 } 280 281 static void reopen_session(struct ceph_mon_client *monc) 282 { 283 if (!monc->hunting) 284 pr_info("mon%d %s session lost, hunting for new mon\n", 285 monc->cur_mon, ceph_pr_addr(&monc->con.peer_addr)); 286 287 __close_session(monc); 288 __open_session(monc); 289 } 290 291 void ceph_monc_reopen_session(struct ceph_mon_client *monc) 292 { 293 mutex_lock(&monc->mutex); 294 reopen_session(monc); 295 mutex_unlock(&monc->mutex); 296 } 297 298 static void un_backoff(struct ceph_mon_client *monc) 299 { 300 monc->hunt_mult /= 2; /* reduce by 50% */ 301 if (monc->hunt_mult < 1) 302 monc->hunt_mult = 1; 303 dout("%s hunt_mult now %d\n", __func__, monc->hunt_mult); 304 } 305 306 /* 307 * Reschedule delayed work timer. 308 */ 309 static void __schedule_delayed(struct ceph_mon_client *monc) 310 { 311 unsigned long delay; 312 313 if (monc->hunting) 314 delay = CEPH_MONC_HUNT_INTERVAL * monc->hunt_mult; 315 else 316 delay = CEPH_MONC_PING_INTERVAL; 317 318 dout("__schedule_delayed after %lu\n", delay); 319 mod_delayed_work(system_percpu_wq, &monc->delayed_work, 320 round_jiffies_relative(delay)); 321 } 322 323 const char *ceph_sub_str[] = { 324 [CEPH_SUB_MONMAP] = "monmap", 325 [CEPH_SUB_OSDMAP] = "osdmap", 326 [CEPH_SUB_FSMAP] = "fsmap.user", 327 [CEPH_SUB_MDSMAP] = "mdsmap", 328 }; 329 330 /* 331 * Send subscribe request for one or more maps, according to 332 * monc->subs. 333 */ 334 static void __send_subscribe(struct ceph_mon_client *monc) 335 { 336 struct ceph_msg *msg = monc->m_subscribe; 337 void *p = msg->front.iov_base; 338 void *const end = p + msg->front_alloc_len; 339 int num = 0; 340 int i; 341 342 dout("%s sent %lu\n", __func__, monc->sub_renew_sent); 343 344 BUG_ON(monc->cur_mon < 0); 345 346 if (!monc->sub_renew_sent) 347 monc->sub_renew_sent = jiffies | 1; /* never 0 */ 348 349 msg->hdr.version = cpu_to_le16(2); 350 351 for (i = 0; i < ARRAY_SIZE(monc->subs); i++) { 352 if (monc->subs[i].want) 353 num++; 354 } 355 BUG_ON(num < 1); /* monmap sub is always there */ 356 ceph_encode_32(&p, num); 357 for (i = 0; i < ARRAY_SIZE(monc->subs); i++) { 358 char buf[32]; 359 int len; 360 361 if (!monc->subs[i].want) 362 continue; 363 364 len = sprintf(buf, "%s", ceph_sub_str[i]); 365 if (i == CEPH_SUB_MDSMAP && 366 monc->fs_cluster_id != CEPH_FS_CLUSTER_ID_NONE) 367 len += sprintf(buf + len, ".%d", monc->fs_cluster_id); 368 369 dout("%s %s start %llu flags 0x%x\n", __func__, buf, 370 le64_to_cpu(monc->subs[i].item.start), 371 monc->subs[i].item.flags); 372 ceph_encode_string(&p, end, buf, len); 373 memcpy(p, &monc->subs[i].item, sizeof(monc->subs[i].item)); 374 p += sizeof(monc->subs[i].item); 375 } 376 377 BUG_ON(p > end); 378 msg->front.iov_len = p - msg->front.iov_base; 379 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); 380 ceph_msg_revoke(msg); 381 ceph_con_send(&monc->con, ceph_msg_get(msg)); 382 } 383 384 static void handle_subscribe_ack(struct ceph_mon_client *monc, 385 struct ceph_msg *msg) 386 { 387 unsigned int seconds; 388 struct ceph_mon_subscribe_ack *h = msg->front.iov_base; 389 390 if (msg->front.iov_len < sizeof(*h)) 391 goto bad; 392 seconds = le32_to_cpu(h->duration); 393 394 mutex_lock(&monc->mutex); 395 if (monc->sub_renew_sent) { 396 /* 397 * This is only needed for legacy (infernalis or older) 398 * MONs -- see delayed_work(). 399 */ 400 monc->sub_renew_after = monc->sub_renew_sent + 401 (seconds >> 1) * HZ - 1; 402 dout("%s sent %lu duration %d renew after %lu\n", __func__, 403 monc->sub_renew_sent, seconds, monc->sub_renew_after); 404 monc->sub_renew_sent = 0; 405 } else { 406 dout("%s sent %lu renew after %lu, ignoring\n", __func__, 407 monc->sub_renew_sent, monc->sub_renew_after); 408 } 409 mutex_unlock(&monc->mutex); 410 return; 411 bad: 412 pr_err("got corrupt subscribe-ack msg\n"); 413 ceph_msg_dump(msg); 414 } 415 416 /* 417 * Register interest in a map 418 * 419 * @sub: one of CEPH_SUB_* 420 * @epoch: X for "every map since X", or 0 for "just the latest" 421 */ 422 static bool __ceph_monc_want_map(struct ceph_mon_client *monc, int sub, 423 u32 epoch, bool continuous) 424 { 425 __le64 start = cpu_to_le64(epoch); 426 u8 flags = !continuous ? CEPH_SUBSCRIBE_ONETIME : 0; 427 428 dout("%s %s epoch %u continuous %d\n", __func__, ceph_sub_str[sub], 429 epoch, continuous); 430 431 if (monc->subs[sub].want && 432 monc->subs[sub].item.start == start && 433 monc->subs[sub].item.flags == flags) 434 return false; 435 436 monc->subs[sub].item.start = start; 437 monc->subs[sub].item.flags = flags; 438 monc->subs[sub].want = true; 439 440 return true; 441 } 442 443 bool ceph_monc_want_map(struct ceph_mon_client *monc, int sub, u32 epoch, 444 bool continuous) 445 { 446 bool need_request; 447 448 mutex_lock(&monc->mutex); 449 need_request = __ceph_monc_want_map(monc, sub, epoch, continuous); 450 mutex_unlock(&monc->mutex); 451 452 return need_request; 453 } 454 EXPORT_SYMBOL(ceph_monc_want_map); 455 456 /* 457 * Keep track of which maps we have 458 * 459 * @sub: one of CEPH_SUB_* 460 */ 461 static void __ceph_monc_got_map(struct ceph_mon_client *monc, int sub, 462 u32 epoch) 463 { 464 dout("%s %s epoch %u\n", __func__, ceph_sub_str[sub], epoch); 465 466 if (monc->subs[sub].want) { 467 if (monc->subs[sub].item.flags & CEPH_SUBSCRIBE_ONETIME) 468 monc->subs[sub].want = false; 469 else 470 monc->subs[sub].item.start = cpu_to_le64(epoch + 1); 471 } 472 473 monc->subs[sub].have = epoch; 474 } 475 476 void ceph_monc_got_map(struct ceph_mon_client *monc, int sub, u32 epoch) 477 { 478 mutex_lock(&monc->mutex); 479 __ceph_monc_got_map(monc, sub, epoch); 480 mutex_unlock(&monc->mutex); 481 } 482 EXPORT_SYMBOL(ceph_monc_got_map); 483 484 void ceph_monc_renew_subs(struct ceph_mon_client *monc) 485 { 486 mutex_lock(&monc->mutex); 487 __send_subscribe(monc); 488 mutex_unlock(&monc->mutex); 489 } 490 EXPORT_SYMBOL(ceph_monc_renew_subs); 491 492 /* 493 * Wait for an osdmap with a given epoch. 494 * 495 * @epoch: epoch to wait for 496 * @timeout: in jiffies, 0 means "wait forever" 497 */ 498 int ceph_monc_wait_osdmap(struct ceph_mon_client *monc, u32 epoch, 499 unsigned long timeout) 500 { 501 unsigned long started = jiffies; 502 long ret; 503 504 mutex_lock(&monc->mutex); 505 while (monc->subs[CEPH_SUB_OSDMAP].have < epoch) { 506 mutex_unlock(&monc->mutex); 507 508 if (timeout && time_after_eq(jiffies, started + timeout)) 509 return -ETIMEDOUT; 510 511 ret = wait_event_interruptible_timeout(monc->client->auth_wq, 512 monc->subs[CEPH_SUB_OSDMAP].have >= epoch, 513 ceph_timeout_jiffies(timeout)); 514 if (ret < 0) 515 return ret; 516 517 mutex_lock(&monc->mutex); 518 } 519 520 mutex_unlock(&monc->mutex); 521 return 0; 522 } 523 EXPORT_SYMBOL(ceph_monc_wait_osdmap); 524 525 /* 526 * Open a session with a random monitor. Request monmap and osdmap, 527 * which are waited upon in __ceph_open_session(). 528 */ 529 int ceph_monc_open_session(struct ceph_mon_client *monc) 530 { 531 mutex_lock(&monc->mutex); 532 __ceph_monc_want_map(monc, CEPH_SUB_MONMAP, 0, true); 533 __ceph_monc_want_map(monc, CEPH_SUB_OSDMAP, 0, false); 534 __open_session(monc); 535 __schedule_delayed(monc); 536 mutex_unlock(&monc->mutex); 537 return 0; 538 } 539 EXPORT_SYMBOL(ceph_monc_open_session); 540 541 static void ceph_monc_handle_map(struct ceph_mon_client *monc, 542 struct ceph_msg *msg) 543 { 544 struct ceph_client *client = monc->client; 545 struct ceph_monmap *monmap; 546 void *p, *end; 547 548 mutex_lock(&monc->mutex); 549 550 dout("handle_monmap\n"); 551 p = msg->front.iov_base; 552 end = p + msg->front.iov_len; 553 554 monmap = ceph_monmap_decode(&p, end, ceph_msgr2(client)); 555 if (IS_ERR(monmap)) { 556 pr_err("problem decoding monmap, %d\n", 557 (int)PTR_ERR(monmap)); 558 ceph_msg_dump(msg); 559 goto out; 560 } 561 562 if (ceph_check_fsid(client, &monmap->fsid) < 0) { 563 kfree(monmap); 564 goto out; 565 } 566 567 kfree(monc->monmap); 568 monc->monmap = monmap; 569 570 __ceph_monc_got_map(monc, CEPH_SUB_MONMAP, monc->monmap->epoch); 571 client->have_fsid = true; 572 573 out: 574 mutex_unlock(&monc->mutex); 575 wake_up_all(&client->auth_wq); 576 } 577 578 /* 579 * generic requests (currently statfs, mon_get_version) 580 */ 581 DEFINE_RB_FUNCS(generic_request, struct ceph_mon_generic_request, tid, node) 582 583 static void release_generic_request(struct kref *kref) 584 { 585 struct ceph_mon_generic_request *req = 586 container_of(kref, struct ceph_mon_generic_request, kref); 587 588 dout("%s greq %p request %p reply %p\n", __func__, req, req->request, 589 req->reply); 590 WARN_ON(!RB_EMPTY_NODE(&req->node)); 591 592 if (req->reply) 593 ceph_msg_put(req->reply); 594 if (req->request) 595 ceph_msg_put(req->request); 596 597 kfree(req); 598 } 599 600 static void put_generic_request(struct ceph_mon_generic_request *req) 601 { 602 if (req) 603 kref_put(&req->kref, release_generic_request); 604 } 605 606 static void get_generic_request(struct ceph_mon_generic_request *req) 607 { 608 kref_get(&req->kref); 609 } 610 611 static struct ceph_mon_generic_request * 612 alloc_generic_request(struct ceph_mon_client *monc, gfp_t gfp) 613 { 614 struct ceph_mon_generic_request *req; 615 616 req = kzalloc_obj(*req, gfp); 617 if (!req) 618 return NULL; 619 620 req->monc = monc; 621 kref_init(&req->kref); 622 RB_CLEAR_NODE(&req->node); 623 init_completion(&req->completion); 624 625 dout("%s greq %p\n", __func__, req); 626 return req; 627 } 628 629 static void register_generic_request(struct ceph_mon_generic_request *req) 630 { 631 struct ceph_mon_client *monc = req->monc; 632 633 WARN_ON(req->tid); 634 635 get_generic_request(req); 636 req->tid = ++monc->last_tid; 637 insert_generic_request(&monc->generic_request_tree, req); 638 } 639 640 static void send_generic_request(struct ceph_mon_client *monc, 641 struct ceph_mon_generic_request *req) 642 { 643 WARN_ON(!req->tid); 644 645 dout("%s greq %p tid %llu\n", __func__, req, req->tid); 646 req->request->hdr.tid = cpu_to_le64(req->tid); 647 ceph_con_send(&monc->con, ceph_msg_get(req->request)); 648 } 649 650 static void __finish_generic_request(struct ceph_mon_generic_request *req) 651 { 652 struct ceph_mon_client *monc = req->monc; 653 654 dout("%s greq %p tid %llu\n", __func__, req, req->tid); 655 erase_generic_request(&monc->generic_request_tree, req); 656 657 ceph_msg_revoke(req->request); 658 ceph_msg_revoke_incoming(req->reply); 659 } 660 661 static void finish_generic_request(struct ceph_mon_generic_request *req) 662 { 663 __finish_generic_request(req); 664 put_generic_request(req); 665 } 666 667 static void complete_generic_request(struct ceph_mon_generic_request *req) 668 { 669 if (req->complete_cb) 670 req->complete_cb(req); 671 else 672 complete_all(&req->completion); 673 put_generic_request(req); 674 } 675 676 static void cancel_generic_request(struct ceph_mon_generic_request *req) 677 { 678 struct ceph_mon_client *monc = req->monc; 679 struct ceph_mon_generic_request *lookup_req; 680 681 dout("%s greq %p tid %llu\n", __func__, req, req->tid); 682 683 mutex_lock(&monc->mutex); 684 lookup_req = lookup_generic_request(&monc->generic_request_tree, 685 req->tid); 686 if (lookup_req) { 687 WARN_ON(lookup_req != req); 688 finish_generic_request(req); 689 } 690 691 mutex_unlock(&monc->mutex); 692 } 693 694 static int wait_generic_request(struct ceph_mon_generic_request *req) 695 { 696 int ret; 697 698 dout("%s greq %p tid %llu\n", __func__, req, req->tid); 699 ret = wait_for_completion_interruptible(&req->completion); 700 if (ret) 701 cancel_generic_request(req); 702 else 703 ret = req->result; /* completed */ 704 705 return ret; 706 } 707 708 static struct ceph_msg *get_generic_reply(struct ceph_connection *con, 709 struct ceph_msg_header *hdr, 710 int *skip) 711 { 712 struct ceph_mon_client *monc = con->private; 713 struct ceph_mon_generic_request *req; 714 u64 tid = le64_to_cpu(hdr->tid); 715 struct ceph_msg *m; 716 717 mutex_lock(&monc->mutex); 718 req = lookup_generic_request(&monc->generic_request_tree, tid); 719 if (!req) { 720 dout("get_generic_reply %lld dne\n", tid); 721 *skip = 1; 722 m = NULL; 723 } else { 724 dout("get_generic_reply %lld got %p\n", tid, req->reply); 725 *skip = 0; 726 m = ceph_msg_get(req->reply); 727 /* 728 * we don't need to track the connection reading into 729 * this reply because we only have one open connection 730 * at a time, ever. 731 */ 732 } 733 mutex_unlock(&monc->mutex); 734 return m; 735 } 736 737 /* 738 * statfs 739 */ 740 static void handle_statfs_reply(struct ceph_mon_client *monc, 741 struct ceph_msg *msg) 742 { 743 struct ceph_mon_generic_request *req; 744 struct ceph_mon_statfs_reply *reply = msg->front.iov_base; 745 u64 tid = le64_to_cpu(msg->hdr.tid); 746 747 dout("%s msg %p tid %llu\n", __func__, msg, tid); 748 749 if (msg->front.iov_len != sizeof(*reply)) 750 goto bad; 751 752 mutex_lock(&monc->mutex); 753 req = lookup_generic_request(&monc->generic_request_tree, tid); 754 if (!req) { 755 mutex_unlock(&monc->mutex); 756 return; 757 } 758 759 req->result = 0; 760 *req->u.st = reply->st; /* struct */ 761 __finish_generic_request(req); 762 mutex_unlock(&monc->mutex); 763 764 complete_generic_request(req); 765 return; 766 767 bad: 768 pr_err("corrupt statfs reply, tid %llu\n", tid); 769 ceph_msg_dump(msg); 770 } 771 772 /* 773 * Do a synchronous statfs(). 774 */ 775 int ceph_monc_do_statfs(struct ceph_mon_client *monc, u64 data_pool, 776 struct ceph_statfs *buf) 777 { 778 struct ceph_mon_generic_request *req; 779 struct ceph_mon_statfs *h; 780 int ret = -ENOMEM; 781 782 req = alloc_generic_request(monc, GFP_NOFS); 783 if (!req) 784 goto out; 785 786 req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS, 787 true); 788 if (!req->request) 789 goto out; 790 791 req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 64, GFP_NOFS, true); 792 if (!req->reply) 793 goto out; 794 795 req->u.st = buf; 796 req->request->hdr.version = cpu_to_le16(2); 797 798 mutex_lock(&monc->mutex); 799 register_generic_request(req); 800 /* fill out request */ 801 h = req->request->front.iov_base; 802 h->monhdr.have_version = 0; 803 h->monhdr.session_mon = cpu_to_le16(-1); 804 h->monhdr.session_mon_tid = 0; 805 h->fsid = monc->monmap->fsid; 806 h->contains_data_pool = (data_pool != CEPH_NOPOOL); 807 h->data_pool = cpu_to_le64(data_pool); 808 send_generic_request(monc, req); 809 mutex_unlock(&monc->mutex); 810 811 ret = wait_generic_request(req); 812 out: 813 put_generic_request(req); 814 return ret; 815 } 816 EXPORT_SYMBOL(ceph_monc_do_statfs); 817 818 static void handle_get_version_reply(struct ceph_mon_client *monc, 819 struct ceph_msg *msg) 820 { 821 struct ceph_mon_generic_request *req; 822 u64 tid = le64_to_cpu(msg->hdr.tid); 823 void *p = msg->front.iov_base; 824 void *end = p + msg->front_alloc_len; 825 u64 handle; 826 827 dout("%s msg %p tid %llu\n", __func__, msg, tid); 828 829 ceph_decode_need(&p, end, 2*sizeof(u64), bad); 830 handle = ceph_decode_64(&p); 831 if (tid != 0 && tid != handle) 832 goto bad; 833 834 mutex_lock(&monc->mutex); 835 req = lookup_generic_request(&monc->generic_request_tree, handle); 836 if (!req) { 837 mutex_unlock(&monc->mutex); 838 return; 839 } 840 841 req->result = 0; 842 req->u.newest = ceph_decode_64(&p); 843 __finish_generic_request(req); 844 mutex_unlock(&monc->mutex); 845 846 complete_generic_request(req); 847 return; 848 849 bad: 850 pr_err("corrupt mon_get_version reply, tid %llu\n", tid); 851 ceph_msg_dump(msg); 852 } 853 854 static struct ceph_mon_generic_request * 855 __ceph_monc_get_version(struct ceph_mon_client *monc, const char *what, 856 ceph_monc_callback_t cb, u64 private_data) 857 { 858 struct ceph_mon_generic_request *req; 859 860 req = alloc_generic_request(monc, GFP_NOIO); 861 if (!req) 862 goto err_put_req; 863 864 req->request = ceph_msg_new(CEPH_MSG_MON_GET_VERSION, 865 sizeof(u64) + sizeof(u32) + strlen(what), 866 GFP_NOIO, true); 867 if (!req->request) 868 goto err_put_req; 869 870 req->reply = ceph_msg_new(CEPH_MSG_MON_GET_VERSION_REPLY, 32, GFP_NOIO, 871 true); 872 if (!req->reply) 873 goto err_put_req; 874 875 req->complete_cb = cb; 876 req->private_data = private_data; 877 878 mutex_lock(&monc->mutex); 879 register_generic_request(req); 880 { 881 void *p = req->request->front.iov_base; 882 void *const end = p + req->request->front_alloc_len; 883 884 ceph_encode_64(&p, req->tid); /* handle */ 885 ceph_encode_string(&p, end, what, strlen(what)); 886 WARN_ON(p != end); 887 } 888 send_generic_request(monc, req); 889 mutex_unlock(&monc->mutex); 890 891 return req; 892 893 err_put_req: 894 put_generic_request(req); 895 return ERR_PTR(-ENOMEM); 896 } 897 898 /* 899 * Send MMonGetVersion and wait for the reply. 900 * 901 * @what: one of "mdsmap", "osdmap" or "monmap" 902 */ 903 int ceph_monc_get_version(struct ceph_mon_client *monc, const char *what, 904 u64 *newest) 905 { 906 struct ceph_mon_generic_request *req; 907 int ret; 908 909 req = __ceph_monc_get_version(monc, what, NULL, 0); 910 if (IS_ERR(req)) 911 return PTR_ERR(req); 912 913 ret = wait_generic_request(req); 914 if (!ret) 915 *newest = req->u.newest; 916 917 put_generic_request(req); 918 return ret; 919 } 920 EXPORT_SYMBOL(ceph_monc_get_version); 921 922 /* 923 * Send MMonGetVersion, 924 * 925 * @what: one of "mdsmap", "osdmap" or "monmap" 926 */ 927 int ceph_monc_get_version_async(struct ceph_mon_client *monc, const char *what, 928 ceph_monc_callback_t cb, u64 private_data) 929 { 930 struct ceph_mon_generic_request *req; 931 932 req = __ceph_monc_get_version(monc, what, cb, private_data); 933 if (IS_ERR(req)) 934 return PTR_ERR(req); 935 936 put_generic_request(req); 937 return 0; 938 } 939 EXPORT_SYMBOL(ceph_monc_get_version_async); 940 941 static void handle_command_ack(struct ceph_mon_client *monc, 942 struct ceph_msg *msg) 943 { 944 struct ceph_mon_generic_request *req; 945 void *p = msg->front.iov_base; 946 void *const end = p + msg->front_alloc_len; 947 u64 tid = le64_to_cpu(msg->hdr.tid); 948 949 dout("%s msg %p tid %llu\n", __func__, msg, tid); 950 951 ceph_decode_need(&p, end, sizeof(struct ceph_mon_request_header) + 952 sizeof(u32), bad); 953 p += sizeof(struct ceph_mon_request_header); 954 955 mutex_lock(&monc->mutex); 956 req = lookup_generic_request(&monc->generic_request_tree, tid); 957 if (!req) { 958 mutex_unlock(&monc->mutex); 959 return; 960 } 961 962 req->result = ceph_decode_32(&p); 963 __finish_generic_request(req); 964 mutex_unlock(&monc->mutex); 965 966 complete_generic_request(req); 967 return; 968 969 bad: 970 pr_err("corrupt mon_command ack, tid %llu\n", tid); 971 ceph_msg_dump(msg); 972 } 973 974 static __printf(2, 0) 975 int do_mon_command_vargs(struct ceph_mon_client *monc, const char *fmt, 976 va_list ap) 977 { 978 struct ceph_mon_generic_request *req; 979 struct ceph_mon_command *h; 980 int ret = -ENOMEM; 981 int len; 982 983 req = alloc_generic_request(monc, GFP_NOIO); 984 if (!req) 985 goto out; 986 987 req->request = ceph_msg_new(CEPH_MSG_MON_COMMAND, 256, GFP_NOIO, true); 988 if (!req->request) 989 goto out; 990 991 req->reply = ceph_msg_new(CEPH_MSG_MON_COMMAND_ACK, 512, GFP_NOIO, 992 true); 993 if (!req->reply) 994 goto out; 995 996 mutex_lock(&monc->mutex); 997 register_generic_request(req); 998 h = req->request->front.iov_base; 999 h->monhdr.have_version = 0; 1000 h->monhdr.session_mon = cpu_to_le16(-1); 1001 h->monhdr.session_mon_tid = 0; 1002 h->fsid = monc->monmap->fsid; 1003 h->num_strs = cpu_to_le32(1); 1004 len = vsprintf(h->str, fmt, ap); 1005 h->str_len = cpu_to_le32(len); 1006 send_generic_request(monc, req); 1007 mutex_unlock(&monc->mutex); 1008 1009 ret = wait_generic_request(req); 1010 out: 1011 put_generic_request(req); 1012 return ret; 1013 } 1014 1015 static __printf(2, 3) 1016 int do_mon_command(struct ceph_mon_client *monc, const char *fmt, ...) 1017 { 1018 va_list ap; 1019 int ret; 1020 1021 va_start(ap, fmt); 1022 ret = do_mon_command_vargs(monc, fmt, ap); 1023 va_end(ap); 1024 return ret; 1025 } 1026 1027 int ceph_monc_blocklist_add(struct ceph_mon_client *monc, 1028 struct ceph_entity_addr *client_addr) 1029 { 1030 int ret; 1031 1032 ret = do_mon_command(monc, 1033 "{ \"prefix\": \"osd blocklist\", \ 1034 \"blocklistop\": \"add\", \ 1035 \"addr\": \"%pISpc/%u\" }", 1036 &client_addr->in_addr, 1037 le32_to_cpu(client_addr->nonce)); 1038 if (ret == -EINVAL) { 1039 /* 1040 * The monitor returns EINVAL on an unrecognized command. 1041 * Try the legacy command -- it is exactly the same except 1042 * for the name. 1043 */ 1044 ret = do_mon_command(monc, 1045 "{ \"prefix\": \"osd blacklist\", \ 1046 \"blacklistop\": \"add\", \ 1047 \"addr\": \"%pISpc/%u\" }", 1048 &client_addr->in_addr, 1049 le32_to_cpu(client_addr->nonce)); 1050 } 1051 if (ret) 1052 return ret; 1053 1054 /* 1055 * Make sure we have the osdmap that includes the blocklist 1056 * entry. This is needed to ensure that the OSDs pick up the 1057 * new blocklist before processing any future requests from 1058 * this client. 1059 */ 1060 return ceph_wait_for_latest_osdmap(monc->client, 0); 1061 } 1062 EXPORT_SYMBOL(ceph_monc_blocklist_add); 1063 1064 /* 1065 * Resend pending generic requests. 1066 */ 1067 static void __resend_generic_request(struct ceph_mon_client *monc) 1068 { 1069 struct ceph_mon_generic_request *req; 1070 struct rb_node *p; 1071 1072 for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) { 1073 req = rb_entry(p, struct ceph_mon_generic_request, node); 1074 ceph_msg_revoke(req->request); 1075 ceph_msg_revoke_incoming(req->reply); 1076 ceph_con_send(&monc->con, ceph_msg_get(req->request)); 1077 } 1078 } 1079 1080 /* 1081 * Delayed work. If we haven't mounted yet, retry. Otherwise, 1082 * renew/retry subscription as needed (in case it is timing out, or we 1083 * got an ENOMEM). And keep the monitor connection alive. 1084 */ 1085 static void delayed_work(struct work_struct *work) 1086 { 1087 struct ceph_mon_client *monc = 1088 container_of(work, struct ceph_mon_client, delayed_work.work); 1089 1090 mutex_lock(&monc->mutex); 1091 dout("%s mon%d\n", __func__, monc->cur_mon); 1092 if (monc->cur_mon < 0) { 1093 goto out; 1094 } 1095 1096 if (monc->hunting) { 1097 dout("%s continuing hunt\n", __func__); 1098 reopen_session(monc); 1099 } else { 1100 int is_auth = ceph_auth_is_authenticated(monc->auth); 1101 1102 dout("%s is_authed %d\n", __func__, is_auth); 1103 if (ceph_con_keepalive_expired(&monc->con, 1104 CEPH_MONC_PING_TIMEOUT)) { 1105 dout("monc keepalive timeout\n"); 1106 is_auth = 0; 1107 reopen_session(monc); 1108 } 1109 1110 if (!monc->hunting) { 1111 ceph_con_keepalive(&monc->con); 1112 __validate_auth(monc); 1113 un_backoff(monc); 1114 } 1115 1116 if (is_auth && 1117 !(monc->con.peer_features & CEPH_FEATURE_MON_STATEFUL_SUB)) { 1118 unsigned long now = jiffies; 1119 1120 dout("%s renew subs? now %lu renew after %lu\n", 1121 __func__, now, monc->sub_renew_after); 1122 if (time_after_eq(now, monc->sub_renew_after)) 1123 __send_subscribe(monc); 1124 } 1125 } 1126 __schedule_delayed(monc); 1127 1128 out: 1129 mutex_unlock(&monc->mutex); 1130 } 1131 1132 /* 1133 * On startup, we build a temporary monmap populated with the IPs 1134 * provided by mount(2). 1135 */ 1136 static int build_initial_monmap(struct ceph_mon_client *monc) 1137 { 1138 __le32 my_type = ceph_msgr2(monc->client) ? 1139 CEPH_ENTITY_ADDR_TYPE_MSGR2 : CEPH_ENTITY_ADDR_TYPE_LEGACY; 1140 struct ceph_options *opt = monc->client->options; 1141 int num_mon = opt->num_mon; 1142 int i; 1143 1144 /* build initial monmap */ 1145 monc->monmap = kzalloc_flex(*monc->monmap, mon_inst, num_mon); 1146 if (!monc->monmap) 1147 return -ENOMEM; 1148 monc->monmap->num_mon = num_mon; 1149 1150 for (i = 0; i < num_mon; i++) { 1151 struct ceph_entity_inst *inst = &monc->monmap->mon_inst[i]; 1152 1153 memcpy(&inst->addr.in_addr, &opt->mon_addr[i].in_addr, 1154 sizeof(inst->addr.in_addr)); 1155 inst->addr.type = my_type; 1156 inst->addr.nonce = 0; 1157 inst->name.type = CEPH_ENTITY_TYPE_MON; 1158 inst->name.num = cpu_to_le64(i); 1159 } 1160 return 0; 1161 } 1162 1163 int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl) 1164 { 1165 int err; 1166 1167 dout("init\n"); 1168 memset(monc, 0, sizeof(*monc)); 1169 monc->client = cl; 1170 mutex_init(&monc->mutex); 1171 1172 err = build_initial_monmap(monc); 1173 if (err) 1174 goto out; 1175 1176 /* connection */ 1177 /* authentication */ 1178 monc->auth = ceph_auth_init(cl->options->name, cl->options->key, 1179 cl->options->con_modes); 1180 if (IS_ERR(monc->auth)) { 1181 err = PTR_ERR(monc->auth); 1182 goto out_monmap; 1183 } 1184 monc->auth->want_keys = 1185 CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON | 1186 CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS; 1187 1188 /* msgs */ 1189 err = -ENOMEM; 1190 monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK, 1191 sizeof(struct ceph_mon_subscribe_ack), 1192 GFP_KERNEL, true); 1193 if (!monc->m_subscribe_ack) 1194 goto out_auth; 1195 1196 monc->m_subscribe = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 128, 1197 GFP_KERNEL, true); 1198 if (!monc->m_subscribe) 1199 goto out_subscribe_ack; 1200 1201 monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096, 1202 GFP_KERNEL, true); 1203 if (!monc->m_auth_reply) 1204 goto out_subscribe; 1205 1206 monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_KERNEL, true); 1207 monc->pending_auth = 0; 1208 if (!monc->m_auth) 1209 goto out_auth_reply; 1210 1211 ceph_con_init(&monc->con, monc, &mon_con_ops, 1212 &monc->client->msgr); 1213 1214 monc->cur_mon = -1; 1215 monc->had_a_connection = false; 1216 monc->hunt_mult = 1; 1217 1218 INIT_DELAYED_WORK(&monc->delayed_work, delayed_work); 1219 monc->generic_request_tree = RB_ROOT; 1220 monc->last_tid = 0; 1221 1222 monc->fs_cluster_id = CEPH_FS_CLUSTER_ID_NONE; 1223 1224 return 0; 1225 1226 out_auth_reply: 1227 ceph_msg_put(monc->m_auth_reply); 1228 out_subscribe: 1229 ceph_msg_put(monc->m_subscribe); 1230 out_subscribe_ack: 1231 ceph_msg_put(monc->m_subscribe_ack); 1232 out_auth: 1233 ceph_auth_destroy(monc->auth); 1234 out_monmap: 1235 kfree(monc->monmap); 1236 out: 1237 return err; 1238 } 1239 EXPORT_SYMBOL(ceph_monc_init); 1240 1241 void ceph_monc_stop(struct ceph_mon_client *monc) 1242 { 1243 dout("stop\n"); 1244 1245 mutex_lock(&monc->mutex); 1246 __close_session(monc); 1247 monc->hunting = false; 1248 monc->cur_mon = -1; 1249 mutex_unlock(&monc->mutex); 1250 1251 cancel_delayed_work_sync(&monc->delayed_work); 1252 1253 /* 1254 * flush msgr queue before we destroy ourselves to ensure that: 1255 * - any work that references our embedded con is finished. 1256 * - any osd_client or other work that may reference an authorizer 1257 * finishes before we shut down the auth subsystem. 1258 */ 1259 ceph_msgr_flush(); 1260 1261 ceph_auth_destroy(monc->auth); 1262 1263 WARN_ON(!RB_EMPTY_ROOT(&monc->generic_request_tree)); 1264 1265 ceph_msg_put(monc->m_auth); 1266 ceph_msg_put(monc->m_auth_reply); 1267 ceph_msg_put(monc->m_subscribe); 1268 ceph_msg_put(monc->m_subscribe_ack); 1269 1270 kfree(monc->monmap); 1271 } 1272 EXPORT_SYMBOL(ceph_monc_stop); 1273 1274 static void finish_hunting(struct ceph_mon_client *monc) 1275 { 1276 if (monc->hunting) { 1277 dout("%s found mon%d\n", __func__, monc->cur_mon); 1278 monc->hunting = false; 1279 monc->had_a_connection = true; 1280 un_backoff(monc); 1281 __schedule_delayed(monc); 1282 } 1283 } 1284 1285 static void finish_auth(struct ceph_mon_client *monc, int auth_err, 1286 bool was_authed) 1287 { 1288 dout("%s auth_err %d was_authed %d\n", __func__, auth_err, was_authed); 1289 WARN_ON(auth_err > 0); 1290 1291 monc->pending_auth = 0; 1292 if (auth_err) { 1293 monc->client->auth_err = auth_err; 1294 wake_up_all(&monc->client->auth_wq); 1295 return; 1296 } 1297 1298 if (!was_authed && ceph_auth_is_authenticated(monc->auth)) { 1299 dout("%s authenticated, starting session global_id %llu\n", 1300 __func__, monc->auth->global_id); 1301 1302 monc->client->msgr.inst.name.type = CEPH_ENTITY_TYPE_CLIENT; 1303 monc->client->msgr.inst.name.num = 1304 cpu_to_le64(monc->auth->global_id); 1305 1306 __send_subscribe(monc); 1307 __resend_generic_request(monc); 1308 1309 pr_info("mon%d %s session established\n", monc->cur_mon, 1310 ceph_pr_addr(&monc->con.peer_addr)); 1311 } 1312 } 1313 1314 static void handle_auth_reply(struct ceph_mon_client *monc, 1315 struct ceph_msg *msg) 1316 { 1317 bool was_authed; 1318 int ret; 1319 1320 mutex_lock(&monc->mutex); 1321 was_authed = ceph_auth_is_authenticated(monc->auth); 1322 ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base, 1323 msg->front.iov_len, 1324 monc->m_auth->front.iov_base, 1325 monc->m_auth->front_alloc_len); 1326 if (ret > 0) { 1327 __send_prepared_auth_request(monc, ret); 1328 } else { 1329 finish_auth(monc, ret, was_authed); 1330 finish_hunting(monc); 1331 } 1332 mutex_unlock(&monc->mutex); 1333 } 1334 1335 static int __validate_auth(struct ceph_mon_client *monc) 1336 { 1337 int ret; 1338 1339 if (monc->pending_auth) 1340 return 0; 1341 1342 ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base, 1343 monc->m_auth->front_alloc_len); 1344 if (ret <= 0) 1345 return ret; /* either an error, or no need to authenticate */ 1346 __send_prepared_auth_request(monc, ret); 1347 return 0; 1348 } 1349 1350 int ceph_monc_validate_auth(struct ceph_mon_client *monc) 1351 { 1352 int ret; 1353 1354 mutex_lock(&monc->mutex); 1355 ret = __validate_auth(monc); 1356 mutex_unlock(&monc->mutex); 1357 return ret; 1358 } 1359 EXPORT_SYMBOL(ceph_monc_validate_auth); 1360 1361 static int mon_get_auth_request(struct ceph_connection *con, 1362 void *buf, int *buf_len, 1363 void **authorizer, int *authorizer_len) 1364 { 1365 struct ceph_mon_client *monc = con->private; 1366 int ret; 1367 1368 mutex_lock(&monc->mutex); 1369 ret = ceph_auth_get_request(monc->auth, buf, *buf_len); 1370 mutex_unlock(&monc->mutex); 1371 if (ret < 0) 1372 return ret; 1373 1374 *buf_len = ret; 1375 *authorizer = NULL; 1376 *authorizer_len = 0; 1377 return 0; 1378 } 1379 1380 static int mon_handle_auth_reply_more(struct ceph_connection *con, 1381 void *reply, int reply_len, 1382 void *buf, int *buf_len, 1383 void **authorizer, int *authorizer_len) 1384 { 1385 struct ceph_mon_client *monc = con->private; 1386 int ret; 1387 1388 mutex_lock(&monc->mutex); 1389 ret = ceph_auth_handle_reply_more(monc->auth, reply, reply_len, 1390 buf, *buf_len); 1391 mutex_unlock(&monc->mutex); 1392 if (ret < 0) 1393 return ret; 1394 1395 *buf_len = ret; 1396 *authorizer = NULL; 1397 *authorizer_len = 0; 1398 return 0; 1399 } 1400 1401 static int mon_handle_auth_done(struct ceph_connection *con, 1402 u64 global_id, void *reply, int reply_len, 1403 u8 *session_key, int *session_key_len, 1404 u8 *con_secret, int *con_secret_len) 1405 { 1406 struct ceph_mon_client *monc = con->private; 1407 bool was_authed; 1408 int ret; 1409 1410 mutex_lock(&monc->mutex); 1411 WARN_ON(!monc->hunting); 1412 was_authed = ceph_auth_is_authenticated(monc->auth); 1413 ret = ceph_auth_handle_reply_done(monc->auth, global_id, 1414 reply, reply_len, 1415 session_key, session_key_len, 1416 con_secret, con_secret_len); 1417 finish_auth(monc, ret, was_authed); 1418 if (!ret) 1419 finish_hunting(monc); 1420 mutex_unlock(&monc->mutex); 1421 return ret; 1422 } 1423 1424 static int mon_handle_auth_bad_method(struct ceph_connection *con, 1425 int used_proto, int result, 1426 const int *allowed_protos, int proto_cnt, 1427 const int *allowed_modes, int mode_cnt) 1428 { 1429 struct ceph_mon_client *monc = con->private; 1430 bool was_authed; 1431 1432 mutex_lock(&monc->mutex); 1433 WARN_ON(!monc->hunting); 1434 was_authed = ceph_auth_is_authenticated(monc->auth); 1435 ceph_auth_handle_bad_method(monc->auth, used_proto, result, 1436 allowed_protos, proto_cnt, 1437 allowed_modes, mode_cnt); 1438 finish_auth(monc, -EACCES, was_authed); 1439 mutex_unlock(&monc->mutex); 1440 return 0; 1441 } 1442 1443 /* 1444 * handle incoming message 1445 */ 1446 static void mon_dispatch(struct ceph_connection *con, struct ceph_msg *msg) 1447 { 1448 struct ceph_mon_client *monc = con->private; 1449 int type = le16_to_cpu(msg->hdr.type); 1450 1451 switch (type) { 1452 case CEPH_MSG_AUTH_REPLY: 1453 handle_auth_reply(monc, msg); 1454 break; 1455 1456 case CEPH_MSG_MON_SUBSCRIBE_ACK: 1457 handle_subscribe_ack(monc, msg); 1458 break; 1459 1460 case CEPH_MSG_STATFS_REPLY: 1461 handle_statfs_reply(monc, msg); 1462 break; 1463 1464 case CEPH_MSG_MON_GET_VERSION_REPLY: 1465 handle_get_version_reply(monc, msg); 1466 break; 1467 1468 case CEPH_MSG_MON_COMMAND_ACK: 1469 handle_command_ack(monc, msg); 1470 break; 1471 1472 case CEPH_MSG_MON_MAP: 1473 ceph_monc_handle_map(monc, msg); 1474 break; 1475 1476 case CEPH_MSG_OSD_MAP: 1477 ceph_osdc_handle_map(&monc->client->osdc, msg); 1478 break; 1479 1480 default: 1481 /* can the chained handler handle it? */ 1482 if (monc->client->extra_mon_dispatch && 1483 monc->client->extra_mon_dispatch(monc->client, msg) == 0) 1484 break; 1485 1486 pr_err("received unknown message type %d %s\n", type, 1487 ceph_msg_type_name(type)); 1488 } 1489 ceph_msg_put(msg); 1490 } 1491 1492 /* 1493 * Allocate memory for incoming message 1494 */ 1495 static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con, 1496 struct ceph_msg_header *hdr, 1497 int *skip) 1498 { 1499 struct ceph_mon_client *monc = con->private; 1500 int type = le16_to_cpu(hdr->type); 1501 int front_len = le32_to_cpu(hdr->front_len); 1502 struct ceph_msg *m = NULL; 1503 1504 *skip = 0; 1505 1506 switch (type) { 1507 case CEPH_MSG_MON_SUBSCRIBE_ACK: 1508 m = ceph_msg_get(monc->m_subscribe_ack); 1509 break; 1510 case CEPH_MSG_STATFS_REPLY: 1511 case CEPH_MSG_MON_COMMAND_ACK: 1512 return get_generic_reply(con, hdr, skip); 1513 case CEPH_MSG_AUTH_REPLY: 1514 m = ceph_msg_get(monc->m_auth_reply); 1515 break; 1516 case CEPH_MSG_MON_GET_VERSION_REPLY: 1517 if (le64_to_cpu(hdr->tid) != 0) 1518 return get_generic_reply(con, hdr, skip); 1519 1520 /* 1521 * Older OSDs don't set reply tid even if the original 1522 * request had a non-zero tid. Work around this weirdness 1523 * by allocating a new message. 1524 */ 1525 fallthrough; 1526 case CEPH_MSG_MON_MAP: 1527 case CEPH_MSG_MDS_MAP: 1528 case CEPH_MSG_OSD_MAP: 1529 case CEPH_MSG_FS_MAP_USER: 1530 m = ceph_msg_new(type, front_len, GFP_NOFS, false); 1531 if (!m) 1532 return NULL; /* ENOMEM--return skip == 0 */ 1533 break; 1534 } 1535 1536 if (!m) { 1537 pr_info("alloc_msg unknown type %d\n", type); 1538 *skip = 1; 1539 } else if (front_len > m->front_alloc_len) { 1540 pr_warn("mon_alloc_msg front %d > prealloc %d (%u#%llu)\n", 1541 front_len, m->front_alloc_len, 1542 (unsigned int)con->peer_name.type, 1543 le64_to_cpu(con->peer_name.num)); 1544 ceph_msg_put(m); 1545 m = ceph_msg_new(type, front_len, GFP_NOFS, false); 1546 } 1547 1548 return m; 1549 } 1550 1551 /* 1552 * If the monitor connection resets, pick a new monitor and resubmit 1553 * any pending requests. 1554 */ 1555 static void mon_fault(struct ceph_connection *con) 1556 { 1557 struct ceph_mon_client *monc = con->private; 1558 1559 mutex_lock(&monc->mutex); 1560 dout("%s mon%d\n", __func__, monc->cur_mon); 1561 if (monc->cur_mon >= 0) { 1562 if (!monc->hunting) { 1563 dout("%s hunting for new mon\n", __func__); 1564 reopen_session(monc); 1565 __schedule_delayed(monc); 1566 } else { 1567 dout("%s already hunting\n", __func__); 1568 } 1569 } 1570 mutex_unlock(&monc->mutex); 1571 } 1572 1573 /* 1574 * We can ignore refcounting on the connection struct, as all references 1575 * will come from the messenger workqueue, which is drained prior to 1576 * mon_client destruction. 1577 */ 1578 static struct ceph_connection *mon_get_con(struct ceph_connection *con) 1579 { 1580 return con; 1581 } 1582 1583 static void mon_put_con(struct ceph_connection *con) 1584 { 1585 } 1586 1587 static const struct ceph_connection_operations mon_con_ops = { 1588 .get = mon_get_con, 1589 .put = mon_put_con, 1590 .alloc_msg = mon_alloc_msg, 1591 .dispatch = mon_dispatch, 1592 .fault = mon_fault, 1593 .get_auth_request = mon_get_auth_request, 1594 .handle_auth_reply_more = mon_handle_auth_reply_more, 1595 .handle_auth_done = mon_handle_auth_done, 1596 .handle_auth_bad_method = mon_handle_auth_bad_method, 1597 }; 1598