1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/ceph/ceph_debug.h> 3 4 #include <linux/crc32c.h> 5 #include <linux/ctype.h> 6 #include <linux/highmem.h> 7 #include <linux/inet.h> 8 #include <linux/kthread.h> 9 #include <linux/net.h> 10 #include <linux/nsproxy.h> 11 #include <linux/sched/mm.h> 12 #include <linux/slab.h> 13 #include <linux/socket.h> 14 #include <linux/string.h> 15 #ifdef CONFIG_BLOCK 16 #include <linux/bio.h> 17 #endif /* CONFIG_BLOCK */ 18 #include <linux/dns_resolver.h> 19 #include <net/tcp.h> 20 21 #include <linux/ceph/ceph_features.h> 22 #include <linux/ceph/libceph.h> 23 #include <linux/ceph/messenger.h> 24 #include <linux/ceph/decode.h> 25 #include <linux/ceph/pagelist.h> 26 #include <linux/export.h> 27 28 /* 29 * Ceph uses the messenger to exchange ceph_msg messages with other 30 * hosts in the system. The messenger provides ordered and reliable 31 * delivery. We tolerate TCP disconnects by reconnecting (with 32 * exponential backoff) in the case of a fault (disconnection, bad 33 * crc, protocol error). Acks allow sent messages to be discarded by 34 * the sender. 35 */ 36 37 /* 38 * We track the state of the socket on a given connection using 39 * values defined below. The transition to a new socket state is 40 * handled by a function which verifies we aren't coming from an 41 * unexpected state. 42 * 43 * -------- 44 * | NEW* | transient initial state 45 * -------- 46 * | con_sock_state_init() 47 * v 48 * ---------- 49 * | CLOSED | initialized, but no socket (and no 50 * ---------- TCP connection) 51 * ^ \ 52 * | \ con_sock_state_connecting() 53 * | ---------------------- 54 * | \ 55 * + con_sock_state_closed() \ 56 * |+--------------------------- \ 57 * | \ \ \ 58 * | ----------- \ \ 59 * | | CLOSING | socket event; \ \ 60 * | ----------- await close \ \ 61 * | ^ \ | 62 * | | \ | 63 * | + con_sock_state_closing() \ | 64 * | / \ | | 65 * | / --------------- | | 66 * | / \ v v 67 * | / -------------- 68 * | / -----------------| CONNECTING | socket created, TCP 69 * | | / -------------- connect initiated 70 * | | | con_sock_state_connected() 71 * | | v 72 * ------------- 73 * | CONNECTED | TCP connection established 74 * ------------- 75 * 76 * State values for ceph_connection->sock_state; NEW is assumed to be 0. 77 */ 78 79 #define CON_SOCK_STATE_NEW 0 /* -> CLOSED */ 80 #define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */ 81 #define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */ 82 #define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */ 83 #define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */ 84 85 static bool con_flag_valid(unsigned long con_flag) 86 { 87 switch (con_flag) { 88 case CEPH_CON_F_LOSSYTX: 89 case CEPH_CON_F_KEEPALIVE_PENDING: 90 case CEPH_CON_F_WRITE_PENDING: 91 case CEPH_CON_F_SOCK_CLOSED: 92 case CEPH_CON_F_BACKOFF: 93 return true; 94 default: 95 return false; 96 } 97 } 98 99 void ceph_con_flag_clear(struct ceph_connection *con, unsigned long con_flag) 100 { 101 BUG_ON(!con_flag_valid(con_flag)); 102 103 clear_bit(con_flag, &con->flags); 104 } 105 106 void ceph_con_flag_set(struct ceph_connection *con, unsigned long con_flag) 107 { 108 BUG_ON(!con_flag_valid(con_flag)); 109 110 set_bit(con_flag, &con->flags); 111 } 112 113 bool ceph_con_flag_test(struct ceph_connection *con, unsigned long con_flag) 114 { 115 BUG_ON(!con_flag_valid(con_flag)); 116 117 return test_bit(con_flag, &con->flags); 118 } 119 120 bool ceph_con_flag_test_and_clear(struct ceph_connection *con, 121 unsigned long con_flag) 122 { 123 BUG_ON(!con_flag_valid(con_flag)); 124 125 return test_and_clear_bit(con_flag, &con->flags); 126 } 127 128 bool ceph_con_flag_test_and_set(struct ceph_connection *con, 129 unsigned long con_flag) 130 { 131 BUG_ON(!con_flag_valid(con_flag)); 132 133 return test_and_set_bit(con_flag, &con->flags); 134 } 135 136 /* Slab caches for frequently-allocated structures */ 137 138 static struct kmem_cache *ceph_msg_cache; 139 140 #ifdef CONFIG_LOCKDEP 141 static struct lock_class_key socket_class; 142 #endif 143 144 static void queue_con(struct ceph_connection *con); 145 static void cancel_con(struct ceph_connection *con); 146 static void ceph_con_workfn(struct work_struct *); 147 static void con_fault(struct ceph_connection *con); 148 149 /* 150 * Nicely render a sockaddr as a string. An array of formatted 151 * strings is used, to approximate reentrancy. 152 */ 153 #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */ 154 #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG) 155 #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1) 156 #define MAX_ADDR_STR_LEN 64 /* 54 is enough */ 157 158 static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN]; 159 static atomic_t addr_str_seq = ATOMIC_INIT(0); 160 161 struct page *ceph_zero_page; /* used in certain error cases */ 162 163 const char *ceph_pr_addr(const struct ceph_entity_addr *addr) 164 { 165 int i; 166 char *s; 167 struct sockaddr_storage ss = addr->in_addr; /* align */ 168 struct sockaddr_in *in4 = (struct sockaddr_in *)&ss; 169 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&ss; 170 171 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK; 172 s = addr_str[i]; 173 174 switch (ss.ss_family) { 175 case AF_INET: 176 snprintf(s, MAX_ADDR_STR_LEN, "(%d)%pI4:%hu", 177 le32_to_cpu(addr->type), &in4->sin_addr, 178 ntohs(in4->sin_port)); 179 break; 180 181 case AF_INET6: 182 snprintf(s, MAX_ADDR_STR_LEN, "(%d)[%pI6c]:%hu", 183 le32_to_cpu(addr->type), &in6->sin6_addr, 184 ntohs(in6->sin6_port)); 185 break; 186 187 default: 188 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)", 189 ss.ss_family); 190 } 191 192 return s; 193 } 194 EXPORT_SYMBOL(ceph_pr_addr); 195 196 void ceph_encode_my_addr(struct ceph_messenger *msgr) 197 { 198 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr)); 199 ceph_encode_banner_addr(&msgr->my_enc_addr); 200 } 201 202 /* 203 * work queue for all reading and writing to/from the socket. 204 */ 205 static struct workqueue_struct *ceph_msgr_wq; 206 207 static int ceph_msgr_slab_init(void) 208 { 209 BUG_ON(ceph_msg_cache); 210 ceph_msg_cache = KMEM_CACHE(ceph_msg, 0); 211 if (!ceph_msg_cache) 212 return -ENOMEM; 213 214 return 0; 215 } 216 217 static void ceph_msgr_slab_exit(void) 218 { 219 BUG_ON(!ceph_msg_cache); 220 kmem_cache_destroy(ceph_msg_cache); 221 ceph_msg_cache = NULL; 222 } 223 224 static void _ceph_msgr_exit(void) 225 { 226 if (ceph_msgr_wq) { 227 destroy_workqueue(ceph_msgr_wq); 228 ceph_msgr_wq = NULL; 229 } 230 231 BUG_ON(!ceph_zero_page); 232 put_page(ceph_zero_page); 233 ceph_zero_page = NULL; 234 235 ceph_msgr_slab_exit(); 236 } 237 238 int __init ceph_msgr_init(void) 239 { 240 if (ceph_msgr_slab_init()) 241 return -ENOMEM; 242 243 BUG_ON(ceph_zero_page); 244 ceph_zero_page = ZERO_PAGE(0); 245 get_page(ceph_zero_page); 246 247 /* 248 * The number of active work items is limited by the number of 249 * connections, so leave @max_active at default. 250 */ 251 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_MEM_RECLAIM, 0); 252 if (ceph_msgr_wq) 253 return 0; 254 255 pr_err("msgr_init failed to create workqueue\n"); 256 _ceph_msgr_exit(); 257 258 return -ENOMEM; 259 } 260 261 void ceph_msgr_exit(void) 262 { 263 BUG_ON(ceph_msgr_wq == NULL); 264 265 _ceph_msgr_exit(); 266 } 267 268 void ceph_msgr_flush(void) 269 { 270 flush_workqueue(ceph_msgr_wq); 271 } 272 EXPORT_SYMBOL(ceph_msgr_flush); 273 274 /* Connection socket state transition functions */ 275 276 static void con_sock_state_init(struct ceph_connection *con) 277 { 278 int old_state; 279 280 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED); 281 if (WARN_ON(old_state != CON_SOCK_STATE_NEW)) 282 printk("%s: unexpected old state %d\n", __func__, old_state); 283 dout("%s con %p sock %d -> %d\n", __func__, con, old_state, 284 CON_SOCK_STATE_CLOSED); 285 } 286 287 static void con_sock_state_connecting(struct ceph_connection *con) 288 { 289 int old_state; 290 291 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING); 292 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED)) 293 printk("%s: unexpected old state %d\n", __func__, old_state); 294 dout("%s con %p sock %d -> %d\n", __func__, con, old_state, 295 CON_SOCK_STATE_CONNECTING); 296 } 297 298 static void con_sock_state_connected(struct ceph_connection *con) 299 { 300 int old_state; 301 302 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED); 303 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING)) 304 printk("%s: unexpected old state %d\n", __func__, old_state); 305 dout("%s con %p sock %d -> %d\n", __func__, con, old_state, 306 CON_SOCK_STATE_CONNECTED); 307 } 308 309 static void con_sock_state_closing(struct ceph_connection *con) 310 { 311 int old_state; 312 313 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING); 314 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING && 315 old_state != CON_SOCK_STATE_CONNECTED && 316 old_state != CON_SOCK_STATE_CLOSING)) 317 printk("%s: unexpected old state %d\n", __func__, old_state); 318 dout("%s con %p sock %d -> %d\n", __func__, con, old_state, 319 CON_SOCK_STATE_CLOSING); 320 } 321 322 static void con_sock_state_closed(struct ceph_connection *con) 323 { 324 int old_state; 325 326 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED); 327 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED && 328 old_state != CON_SOCK_STATE_CLOSING && 329 old_state != CON_SOCK_STATE_CONNECTING && 330 old_state != CON_SOCK_STATE_CLOSED)) 331 printk("%s: unexpected old state %d\n", __func__, old_state); 332 dout("%s con %p sock %d -> %d\n", __func__, con, old_state, 333 CON_SOCK_STATE_CLOSED); 334 } 335 336 /* 337 * socket callback functions 338 */ 339 340 /* data available on socket, or listen socket received a connect */ 341 static void ceph_sock_data_ready(struct sock *sk) 342 { 343 struct ceph_connection *con = sk->sk_user_data; 344 if (atomic_read(&con->msgr->stopping)) { 345 return; 346 } 347 348 if (sk->sk_state != TCP_CLOSE_WAIT) { 349 dout("%s %p state = %d, queueing work\n", __func__, 350 con, con->state); 351 queue_con(con); 352 } 353 } 354 355 /* socket has buffer space for writing */ 356 static void ceph_sock_write_space(struct sock *sk) 357 { 358 struct ceph_connection *con = sk->sk_user_data; 359 360 /* only queue to workqueue if there is data we want to write, 361 * and there is sufficient space in the socket buffer to accept 362 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space() 363 * doesn't get called again until try_write() fills the socket 364 * buffer. See net/ipv4/tcp_input.c:tcp_check_space() 365 * and net/core/stream.c:sk_stream_write_space(). 366 */ 367 if (ceph_con_flag_test(con, CEPH_CON_F_WRITE_PENDING)) { 368 if (sk_stream_is_writeable(sk)) { 369 dout("%s %p queueing write work\n", __func__, con); 370 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 371 queue_con(con); 372 } 373 } else { 374 dout("%s %p nothing to write\n", __func__, con); 375 } 376 } 377 378 /* socket's state has changed */ 379 static void ceph_sock_state_change(struct sock *sk) 380 { 381 struct ceph_connection *con = sk->sk_user_data; 382 383 dout("%s %p state = %d sk_state = %u\n", __func__, 384 con, con->state, sk->sk_state); 385 386 switch (sk->sk_state) { 387 case TCP_CLOSE: 388 dout("%s TCP_CLOSE\n", __func__); 389 fallthrough; 390 case TCP_CLOSE_WAIT: 391 dout("%s TCP_CLOSE_WAIT\n", __func__); 392 con_sock_state_closing(con); 393 ceph_con_flag_set(con, CEPH_CON_F_SOCK_CLOSED); 394 queue_con(con); 395 break; 396 case TCP_ESTABLISHED: 397 dout("%s TCP_ESTABLISHED\n", __func__); 398 con_sock_state_connected(con); 399 queue_con(con); 400 break; 401 default: /* Everything else is uninteresting */ 402 break; 403 } 404 } 405 406 /* 407 * set up socket callbacks 408 */ 409 static void set_sock_callbacks(struct socket *sock, 410 struct ceph_connection *con) 411 { 412 struct sock *sk = sock->sk; 413 sk->sk_user_data = con; 414 sk->sk_data_ready = ceph_sock_data_ready; 415 sk->sk_write_space = ceph_sock_write_space; 416 sk->sk_state_change = ceph_sock_state_change; 417 } 418 419 420 /* 421 * socket helpers 422 */ 423 424 /* 425 * initiate connection to a remote socket. 426 */ 427 int ceph_tcp_connect(struct ceph_connection *con) 428 { 429 struct sockaddr_storage ss = con->peer_addr.in_addr; /* align */ 430 struct socket *sock; 431 unsigned int noio_flag; 432 int ret; 433 434 dout("%s con %p peer_addr %s\n", __func__, con, 435 ceph_pr_addr(&con->peer_addr)); 436 BUG_ON(con->sock); 437 438 /* sock_create_kern() allocates with GFP_KERNEL */ 439 noio_flag = memalloc_noio_save(); 440 ret = sock_create_kern(read_pnet(&con->msgr->net), ss.ss_family, 441 SOCK_STREAM, IPPROTO_TCP, &sock); 442 memalloc_noio_restore(noio_flag); 443 if (ret) 444 return ret; 445 sock->sk->sk_allocation = GFP_NOFS; 446 447 #ifdef CONFIG_LOCKDEP 448 lockdep_set_class(&sock->sk->sk_lock, &socket_class); 449 #endif 450 451 set_sock_callbacks(sock, con); 452 453 con_sock_state_connecting(con); 454 ret = sock->ops->connect(sock, (struct sockaddr *)&ss, sizeof(ss), 455 O_NONBLOCK); 456 if (ret == -EINPROGRESS) { 457 dout("connect %s EINPROGRESS sk_state = %u\n", 458 ceph_pr_addr(&con->peer_addr), 459 sock->sk->sk_state); 460 } else if (ret < 0) { 461 pr_err("connect %s error %d\n", 462 ceph_pr_addr(&con->peer_addr), ret); 463 sock_release(sock); 464 return ret; 465 } 466 467 if (ceph_test_opt(from_msgr(con->msgr), TCP_NODELAY)) 468 tcp_sock_set_nodelay(sock->sk); 469 470 con->sock = sock; 471 return 0; 472 } 473 474 /* 475 * Shutdown/close the socket for the given connection. 476 */ 477 int ceph_con_close_socket(struct ceph_connection *con) 478 { 479 int rc = 0; 480 481 dout("%s con %p sock %p\n", __func__, con, con->sock); 482 if (con->sock) { 483 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR); 484 sock_release(con->sock); 485 con->sock = NULL; 486 } 487 488 /* 489 * Forcibly clear the SOCK_CLOSED flag. It gets set 490 * independent of the connection mutex, and we could have 491 * received a socket close event before we had the chance to 492 * shut the socket down. 493 */ 494 ceph_con_flag_clear(con, CEPH_CON_F_SOCK_CLOSED); 495 496 con_sock_state_closed(con); 497 return rc; 498 } 499 500 static void ceph_con_reset_protocol(struct ceph_connection *con) 501 { 502 dout("%s con %p\n", __func__, con); 503 504 ceph_con_close_socket(con); 505 if (con->in_msg) { 506 WARN_ON(con->in_msg->con != con); 507 ceph_msg_put(con->in_msg); 508 con->in_msg = NULL; 509 } 510 if (con->out_msg) { 511 WARN_ON(con->out_msg->con != con); 512 ceph_msg_put(con->out_msg); 513 con->out_msg = NULL; 514 } 515 516 ceph_con_v1_reset_protocol(con); 517 } 518 519 /* 520 * Reset a connection. Discard all incoming and outgoing messages 521 * and clear *_seq state. 522 */ 523 static void ceph_msg_remove(struct ceph_msg *msg) 524 { 525 list_del_init(&msg->list_head); 526 527 ceph_msg_put(msg); 528 } 529 static void ceph_msg_remove_list(struct list_head *head) 530 { 531 while (!list_empty(head)) { 532 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg, 533 list_head); 534 ceph_msg_remove(msg); 535 } 536 } 537 538 void ceph_con_reset_session(struct ceph_connection *con) 539 { 540 dout("%s con %p\n", __func__, con); 541 542 WARN_ON(con->in_msg); 543 WARN_ON(con->out_msg); 544 ceph_msg_remove_list(&con->out_queue); 545 ceph_msg_remove_list(&con->out_sent); 546 con->out_seq = 0; 547 con->in_seq = 0; 548 con->in_seq_acked = 0; 549 550 ceph_con_v1_reset_session(con); 551 } 552 553 /* 554 * mark a peer down. drop any open connections. 555 */ 556 void ceph_con_close(struct ceph_connection *con) 557 { 558 mutex_lock(&con->mutex); 559 dout("con_close %p peer %s\n", con, ceph_pr_addr(&con->peer_addr)); 560 con->state = CEPH_CON_S_CLOSED; 561 562 ceph_con_flag_clear(con, CEPH_CON_F_LOSSYTX); /* so we retry next 563 connect */ 564 ceph_con_flag_clear(con, CEPH_CON_F_KEEPALIVE_PENDING); 565 ceph_con_flag_clear(con, CEPH_CON_F_WRITE_PENDING); 566 ceph_con_flag_clear(con, CEPH_CON_F_BACKOFF); 567 568 ceph_con_reset_protocol(con); 569 ceph_con_reset_session(con); 570 cancel_con(con); 571 mutex_unlock(&con->mutex); 572 } 573 EXPORT_SYMBOL(ceph_con_close); 574 575 /* 576 * Reopen a closed connection, with a new peer address. 577 */ 578 void ceph_con_open(struct ceph_connection *con, 579 __u8 entity_type, __u64 entity_num, 580 struct ceph_entity_addr *addr) 581 { 582 mutex_lock(&con->mutex); 583 dout("con_open %p %s\n", con, ceph_pr_addr(addr)); 584 585 WARN_ON(con->state != CEPH_CON_S_CLOSED); 586 con->state = CEPH_CON_S_PREOPEN; 587 588 con->peer_name.type = (__u8) entity_type; 589 con->peer_name.num = cpu_to_le64(entity_num); 590 591 memcpy(&con->peer_addr, addr, sizeof(*addr)); 592 con->delay = 0; /* reset backoff memory */ 593 mutex_unlock(&con->mutex); 594 queue_con(con); 595 } 596 EXPORT_SYMBOL(ceph_con_open); 597 598 /* 599 * return true if this connection ever successfully opened 600 */ 601 bool ceph_con_opened(struct ceph_connection *con) 602 { 603 return ceph_con_v1_opened(con); 604 } 605 606 /* 607 * initialize a new connection. 608 */ 609 void ceph_con_init(struct ceph_connection *con, void *private, 610 const struct ceph_connection_operations *ops, 611 struct ceph_messenger *msgr) 612 { 613 dout("con_init %p\n", con); 614 memset(con, 0, sizeof(*con)); 615 con->private = private; 616 con->ops = ops; 617 con->msgr = msgr; 618 619 con_sock_state_init(con); 620 621 mutex_init(&con->mutex); 622 INIT_LIST_HEAD(&con->out_queue); 623 INIT_LIST_HEAD(&con->out_sent); 624 INIT_DELAYED_WORK(&con->work, ceph_con_workfn); 625 626 con->state = CEPH_CON_S_CLOSED; 627 } 628 EXPORT_SYMBOL(ceph_con_init); 629 630 /* 631 * We maintain a global counter to order connection attempts. Get 632 * a unique seq greater than @gt. 633 */ 634 u32 ceph_get_global_seq(struct ceph_messenger *msgr, u32 gt) 635 { 636 u32 ret; 637 638 spin_lock(&msgr->global_seq_lock); 639 if (msgr->global_seq < gt) 640 msgr->global_seq = gt; 641 ret = ++msgr->global_seq; 642 spin_unlock(&msgr->global_seq_lock); 643 return ret; 644 } 645 646 /* 647 * Discard messages that have been acked by the server. 648 */ 649 void ceph_con_discard_sent(struct ceph_connection *con, u64 ack_seq) 650 { 651 struct ceph_msg *msg; 652 u64 seq; 653 654 dout("%s con %p ack_seq %llu\n", __func__, con, ack_seq); 655 while (!list_empty(&con->out_sent)) { 656 msg = list_first_entry(&con->out_sent, struct ceph_msg, 657 list_head); 658 WARN_ON(msg->needs_out_seq); 659 seq = le64_to_cpu(msg->hdr.seq); 660 if (seq > ack_seq) 661 break; 662 663 dout("%s con %p discarding msg %p seq %llu\n", __func__, con, 664 msg, seq); 665 ceph_msg_remove(msg); 666 } 667 } 668 669 /* 670 * Discard messages that have been requeued in con_fault(), up to 671 * reconnect_seq. This avoids gratuitously resending messages that 672 * the server had received and handled prior to reconnect. 673 */ 674 void ceph_con_discard_requeued(struct ceph_connection *con, u64 reconnect_seq) 675 { 676 struct ceph_msg *msg; 677 u64 seq; 678 679 dout("%s con %p reconnect_seq %llu\n", __func__, con, reconnect_seq); 680 while (!list_empty(&con->out_queue)) { 681 msg = list_first_entry(&con->out_queue, struct ceph_msg, 682 list_head); 683 if (msg->needs_out_seq) 684 break; 685 seq = le64_to_cpu(msg->hdr.seq); 686 if (seq > reconnect_seq) 687 break; 688 689 dout("%s con %p discarding msg %p seq %llu\n", __func__, con, 690 msg, seq); 691 ceph_msg_remove(msg); 692 } 693 } 694 695 #ifdef CONFIG_BLOCK 696 697 /* 698 * For a bio data item, a piece is whatever remains of the next 699 * entry in the current bio iovec, or the first entry in the next 700 * bio in the list. 701 */ 702 static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor, 703 size_t length) 704 { 705 struct ceph_msg_data *data = cursor->data; 706 struct ceph_bio_iter *it = &cursor->bio_iter; 707 708 cursor->resid = min_t(size_t, length, data->bio_length); 709 *it = data->bio_pos; 710 if (cursor->resid < it->iter.bi_size) 711 it->iter.bi_size = cursor->resid; 712 713 BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter)); 714 cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter); 715 } 716 717 static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor, 718 size_t *page_offset, 719 size_t *length) 720 { 721 struct bio_vec bv = bio_iter_iovec(cursor->bio_iter.bio, 722 cursor->bio_iter.iter); 723 724 *page_offset = bv.bv_offset; 725 *length = bv.bv_len; 726 return bv.bv_page; 727 } 728 729 static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor, 730 size_t bytes) 731 { 732 struct ceph_bio_iter *it = &cursor->bio_iter; 733 struct page *page = bio_iter_page(it->bio, it->iter); 734 735 BUG_ON(bytes > cursor->resid); 736 BUG_ON(bytes > bio_iter_len(it->bio, it->iter)); 737 cursor->resid -= bytes; 738 bio_advance_iter(it->bio, &it->iter, bytes); 739 740 if (!cursor->resid) { 741 BUG_ON(!cursor->last_piece); 742 return false; /* no more data */ 743 } 744 745 if (!bytes || (it->iter.bi_size && it->iter.bi_bvec_done && 746 page == bio_iter_page(it->bio, it->iter))) 747 return false; /* more bytes to process in this segment */ 748 749 if (!it->iter.bi_size) { 750 it->bio = it->bio->bi_next; 751 it->iter = it->bio->bi_iter; 752 if (cursor->resid < it->iter.bi_size) 753 it->iter.bi_size = cursor->resid; 754 } 755 756 BUG_ON(cursor->last_piece); 757 BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter)); 758 cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter); 759 return true; 760 } 761 #endif /* CONFIG_BLOCK */ 762 763 static void ceph_msg_data_bvecs_cursor_init(struct ceph_msg_data_cursor *cursor, 764 size_t length) 765 { 766 struct ceph_msg_data *data = cursor->data; 767 struct bio_vec *bvecs = data->bvec_pos.bvecs; 768 769 cursor->resid = min_t(size_t, length, data->bvec_pos.iter.bi_size); 770 cursor->bvec_iter = data->bvec_pos.iter; 771 cursor->bvec_iter.bi_size = cursor->resid; 772 773 BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter)); 774 cursor->last_piece = 775 cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter); 776 } 777 778 static struct page *ceph_msg_data_bvecs_next(struct ceph_msg_data_cursor *cursor, 779 size_t *page_offset, 780 size_t *length) 781 { 782 struct bio_vec bv = bvec_iter_bvec(cursor->data->bvec_pos.bvecs, 783 cursor->bvec_iter); 784 785 *page_offset = bv.bv_offset; 786 *length = bv.bv_len; 787 return bv.bv_page; 788 } 789 790 static bool ceph_msg_data_bvecs_advance(struct ceph_msg_data_cursor *cursor, 791 size_t bytes) 792 { 793 struct bio_vec *bvecs = cursor->data->bvec_pos.bvecs; 794 struct page *page = bvec_iter_page(bvecs, cursor->bvec_iter); 795 796 BUG_ON(bytes > cursor->resid); 797 BUG_ON(bytes > bvec_iter_len(bvecs, cursor->bvec_iter)); 798 cursor->resid -= bytes; 799 bvec_iter_advance(bvecs, &cursor->bvec_iter, bytes); 800 801 if (!cursor->resid) { 802 BUG_ON(!cursor->last_piece); 803 return false; /* no more data */ 804 } 805 806 if (!bytes || (cursor->bvec_iter.bi_bvec_done && 807 page == bvec_iter_page(bvecs, cursor->bvec_iter))) 808 return false; /* more bytes to process in this segment */ 809 810 BUG_ON(cursor->last_piece); 811 BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter)); 812 cursor->last_piece = 813 cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter); 814 return true; 815 } 816 817 /* 818 * For a page array, a piece comes from the first page in the array 819 * that has not already been fully consumed. 820 */ 821 static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor, 822 size_t length) 823 { 824 struct ceph_msg_data *data = cursor->data; 825 int page_count; 826 827 BUG_ON(data->type != CEPH_MSG_DATA_PAGES); 828 829 BUG_ON(!data->pages); 830 BUG_ON(!data->length); 831 832 cursor->resid = min(length, data->length); 833 page_count = calc_pages_for(data->alignment, (u64)data->length); 834 cursor->page_offset = data->alignment & ~PAGE_MASK; 835 cursor->page_index = 0; 836 BUG_ON(page_count > (int)USHRT_MAX); 837 cursor->page_count = (unsigned short)page_count; 838 BUG_ON(length > SIZE_MAX - cursor->page_offset); 839 cursor->last_piece = cursor->page_offset + cursor->resid <= PAGE_SIZE; 840 } 841 842 static struct page * 843 ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor, 844 size_t *page_offset, size_t *length) 845 { 846 struct ceph_msg_data *data = cursor->data; 847 848 BUG_ON(data->type != CEPH_MSG_DATA_PAGES); 849 850 BUG_ON(cursor->page_index >= cursor->page_count); 851 BUG_ON(cursor->page_offset >= PAGE_SIZE); 852 853 *page_offset = cursor->page_offset; 854 if (cursor->last_piece) 855 *length = cursor->resid; 856 else 857 *length = PAGE_SIZE - *page_offset; 858 859 return data->pages[cursor->page_index]; 860 } 861 862 static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor, 863 size_t bytes) 864 { 865 BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES); 866 867 BUG_ON(cursor->page_offset + bytes > PAGE_SIZE); 868 869 /* Advance the cursor page offset */ 870 871 cursor->resid -= bytes; 872 cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK; 873 if (!bytes || cursor->page_offset) 874 return false; /* more bytes to process in the current page */ 875 876 if (!cursor->resid) 877 return false; /* no more data */ 878 879 /* Move on to the next page; offset is already at 0 */ 880 881 BUG_ON(cursor->page_index >= cursor->page_count); 882 cursor->page_index++; 883 cursor->last_piece = cursor->resid <= PAGE_SIZE; 884 885 return true; 886 } 887 888 /* 889 * For a pagelist, a piece is whatever remains to be consumed in the 890 * first page in the list, or the front of the next page. 891 */ 892 static void 893 ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor, 894 size_t length) 895 { 896 struct ceph_msg_data *data = cursor->data; 897 struct ceph_pagelist *pagelist; 898 struct page *page; 899 900 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST); 901 902 pagelist = data->pagelist; 903 BUG_ON(!pagelist); 904 905 if (!length) 906 return; /* pagelist can be assigned but empty */ 907 908 BUG_ON(list_empty(&pagelist->head)); 909 page = list_first_entry(&pagelist->head, struct page, lru); 910 911 cursor->resid = min(length, pagelist->length); 912 cursor->page = page; 913 cursor->offset = 0; 914 cursor->last_piece = cursor->resid <= PAGE_SIZE; 915 } 916 917 static struct page * 918 ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor, 919 size_t *page_offset, size_t *length) 920 { 921 struct ceph_msg_data *data = cursor->data; 922 struct ceph_pagelist *pagelist; 923 924 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST); 925 926 pagelist = data->pagelist; 927 BUG_ON(!pagelist); 928 929 BUG_ON(!cursor->page); 930 BUG_ON(cursor->offset + cursor->resid != pagelist->length); 931 932 /* offset of first page in pagelist is always 0 */ 933 *page_offset = cursor->offset & ~PAGE_MASK; 934 if (cursor->last_piece) 935 *length = cursor->resid; 936 else 937 *length = PAGE_SIZE - *page_offset; 938 939 return cursor->page; 940 } 941 942 static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor, 943 size_t bytes) 944 { 945 struct ceph_msg_data *data = cursor->data; 946 struct ceph_pagelist *pagelist; 947 948 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST); 949 950 pagelist = data->pagelist; 951 BUG_ON(!pagelist); 952 953 BUG_ON(cursor->offset + cursor->resid != pagelist->length); 954 BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE); 955 956 /* Advance the cursor offset */ 957 958 cursor->resid -= bytes; 959 cursor->offset += bytes; 960 /* offset of first page in pagelist is always 0 */ 961 if (!bytes || cursor->offset & ~PAGE_MASK) 962 return false; /* more bytes to process in the current page */ 963 964 if (!cursor->resid) 965 return false; /* no more data */ 966 967 /* Move on to the next page */ 968 969 BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head)); 970 cursor->page = list_next_entry(cursor->page, lru); 971 cursor->last_piece = cursor->resid <= PAGE_SIZE; 972 973 return true; 974 } 975 976 /* 977 * Message data is handled (sent or received) in pieces, where each 978 * piece resides on a single page. The network layer might not 979 * consume an entire piece at once. A data item's cursor keeps 980 * track of which piece is next to process and how much remains to 981 * be processed in that piece. It also tracks whether the current 982 * piece is the last one in the data item. 983 */ 984 static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor) 985 { 986 size_t length = cursor->total_resid; 987 988 switch (cursor->data->type) { 989 case CEPH_MSG_DATA_PAGELIST: 990 ceph_msg_data_pagelist_cursor_init(cursor, length); 991 break; 992 case CEPH_MSG_DATA_PAGES: 993 ceph_msg_data_pages_cursor_init(cursor, length); 994 break; 995 #ifdef CONFIG_BLOCK 996 case CEPH_MSG_DATA_BIO: 997 ceph_msg_data_bio_cursor_init(cursor, length); 998 break; 999 #endif /* CONFIG_BLOCK */ 1000 case CEPH_MSG_DATA_BVECS: 1001 ceph_msg_data_bvecs_cursor_init(cursor, length); 1002 break; 1003 case CEPH_MSG_DATA_NONE: 1004 default: 1005 /* BUG(); */ 1006 break; 1007 } 1008 cursor->need_crc = true; 1009 } 1010 1011 void ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor, 1012 struct ceph_msg *msg, size_t length) 1013 { 1014 BUG_ON(!length); 1015 BUG_ON(length > msg->data_length); 1016 BUG_ON(!msg->num_data_items); 1017 1018 cursor->total_resid = length; 1019 cursor->data = msg->data; 1020 1021 __ceph_msg_data_cursor_init(cursor); 1022 } 1023 1024 /* 1025 * Return the page containing the next piece to process for a given 1026 * data item, and supply the page offset and length of that piece. 1027 * Indicate whether this is the last piece in this data item. 1028 */ 1029 struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor, 1030 size_t *page_offset, size_t *length, 1031 bool *last_piece) 1032 { 1033 struct page *page; 1034 1035 switch (cursor->data->type) { 1036 case CEPH_MSG_DATA_PAGELIST: 1037 page = ceph_msg_data_pagelist_next(cursor, page_offset, length); 1038 break; 1039 case CEPH_MSG_DATA_PAGES: 1040 page = ceph_msg_data_pages_next(cursor, page_offset, length); 1041 break; 1042 #ifdef CONFIG_BLOCK 1043 case CEPH_MSG_DATA_BIO: 1044 page = ceph_msg_data_bio_next(cursor, page_offset, length); 1045 break; 1046 #endif /* CONFIG_BLOCK */ 1047 case CEPH_MSG_DATA_BVECS: 1048 page = ceph_msg_data_bvecs_next(cursor, page_offset, length); 1049 break; 1050 case CEPH_MSG_DATA_NONE: 1051 default: 1052 page = NULL; 1053 break; 1054 } 1055 1056 BUG_ON(!page); 1057 BUG_ON(*page_offset + *length > PAGE_SIZE); 1058 BUG_ON(!*length); 1059 BUG_ON(*length > cursor->resid); 1060 if (last_piece) 1061 *last_piece = cursor->last_piece; 1062 1063 return page; 1064 } 1065 1066 /* 1067 * Returns true if the result moves the cursor on to the next piece 1068 * of the data item. 1069 */ 1070 void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor, size_t bytes) 1071 { 1072 bool new_piece; 1073 1074 BUG_ON(bytes > cursor->resid); 1075 switch (cursor->data->type) { 1076 case CEPH_MSG_DATA_PAGELIST: 1077 new_piece = ceph_msg_data_pagelist_advance(cursor, bytes); 1078 break; 1079 case CEPH_MSG_DATA_PAGES: 1080 new_piece = ceph_msg_data_pages_advance(cursor, bytes); 1081 break; 1082 #ifdef CONFIG_BLOCK 1083 case CEPH_MSG_DATA_BIO: 1084 new_piece = ceph_msg_data_bio_advance(cursor, bytes); 1085 break; 1086 #endif /* CONFIG_BLOCK */ 1087 case CEPH_MSG_DATA_BVECS: 1088 new_piece = ceph_msg_data_bvecs_advance(cursor, bytes); 1089 break; 1090 case CEPH_MSG_DATA_NONE: 1091 default: 1092 BUG(); 1093 break; 1094 } 1095 cursor->total_resid -= bytes; 1096 1097 if (!cursor->resid && cursor->total_resid) { 1098 WARN_ON(!cursor->last_piece); 1099 cursor->data++; 1100 __ceph_msg_data_cursor_init(cursor); 1101 new_piece = true; 1102 } 1103 cursor->need_crc = new_piece; 1104 } 1105 1106 u32 ceph_crc32c_page(u32 crc, struct page *page, unsigned int page_offset, 1107 unsigned int length) 1108 { 1109 char *kaddr; 1110 1111 kaddr = kmap(page); 1112 BUG_ON(kaddr == NULL); 1113 crc = crc32c(crc, kaddr + page_offset, length); 1114 kunmap(page); 1115 1116 return crc; 1117 } 1118 1119 bool ceph_addr_is_blank(const struct ceph_entity_addr *addr) 1120 { 1121 struct sockaddr_storage ss = addr->in_addr; /* align */ 1122 struct in_addr *addr4 = &((struct sockaddr_in *)&ss)->sin_addr; 1123 struct in6_addr *addr6 = &((struct sockaddr_in6 *)&ss)->sin6_addr; 1124 1125 switch (ss.ss_family) { 1126 case AF_INET: 1127 return addr4->s_addr == htonl(INADDR_ANY); 1128 case AF_INET6: 1129 return ipv6_addr_any(addr6); 1130 default: 1131 return true; 1132 } 1133 } 1134 1135 int ceph_addr_port(const struct ceph_entity_addr *addr) 1136 { 1137 switch (get_unaligned(&addr->in_addr.ss_family)) { 1138 case AF_INET: 1139 return ntohs(get_unaligned(&((struct sockaddr_in *)&addr->in_addr)->sin_port)); 1140 case AF_INET6: 1141 return ntohs(get_unaligned(&((struct sockaddr_in6 *)&addr->in_addr)->sin6_port)); 1142 } 1143 return 0; 1144 } 1145 1146 void ceph_addr_set_port(struct ceph_entity_addr *addr, int p) 1147 { 1148 switch (get_unaligned(&addr->in_addr.ss_family)) { 1149 case AF_INET: 1150 put_unaligned(htons(p), &((struct sockaddr_in *)&addr->in_addr)->sin_port); 1151 break; 1152 case AF_INET6: 1153 put_unaligned(htons(p), &((struct sockaddr_in6 *)&addr->in_addr)->sin6_port); 1154 break; 1155 } 1156 } 1157 1158 /* 1159 * Unlike other *_pton function semantics, zero indicates success. 1160 */ 1161 static int ceph_pton(const char *str, size_t len, struct ceph_entity_addr *addr, 1162 char delim, const char **ipend) 1163 { 1164 memset(&addr->in_addr, 0, sizeof(addr->in_addr)); 1165 1166 if (in4_pton(str, len, (u8 *)&((struct sockaddr_in *)&addr->in_addr)->sin_addr.s_addr, delim, ipend)) { 1167 put_unaligned(AF_INET, &addr->in_addr.ss_family); 1168 return 0; 1169 } 1170 1171 if (in6_pton(str, len, (u8 *)&((struct sockaddr_in6 *)&addr->in_addr)->sin6_addr.s6_addr, delim, ipend)) { 1172 put_unaligned(AF_INET6, &addr->in_addr.ss_family); 1173 return 0; 1174 } 1175 1176 return -EINVAL; 1177 } 1178 1179 /* 1180 * Extract hostname string and resolve using kernel DNS facility. 1181 */ 1182 #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER 1183 static int ceph_dns_resolve_name(const char *name, size_t namelen, 1184 struct ceph_entity_addr *addr, char delim, const char **ipend) 1185 { 1186 const char *end, *delim_p; 1187 char *colon_p, *ip_addr = NULL; 1188 int ip_len, ret; 1189 1190 /* 1191 * The end of the hostname occurs immediately preceding the delimiter or 1192 * the port marker (':') where the delimiter takes precedence. 1193 */ 1194 delim_p = memchr(name, delim, namelen); 1195 colon_p = memchr(name, ':', namelen); 1196 1197 if (delim_p && colon_p) 1198 end = delim_p < colon_p ? delim_p : colon_p; 1199 else if (!delim_p && colon_p) 1200 end = colon_p; 1201 else { 1202 end = delim_p; 1203 if (!end) /* case: hostname:/ */ 1204 end = name + namelen; 1205 } 1206 1207 if (end <= name) 1208 return -EINVAL; 1209 1210 /* do dns_resolve upcall */ 1211 ip_len = dns_query(current->nsproxy->net_ns, 1212 NULL, name, end - name, NULL, &ip_addr, NULL, false); 1213 if (ip_len > 0) 1214 ret = ceph_pton(ip_addr, ip_len, addr, -1, NULL); 1215 else 1216 ret = -ESRCH; 1217 1218 kfree(ip_addr); 1219 1220 *ipend = end; 1221 1222 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name, 1223 ret, ret ? "failed" : ceph_pr_addr(addr)); 1224 1225 return ret; 1226 } 1227 #else 1228 static inline int ceph_dns_resolve_name(const char *name, size_t namelen, 1229 struct ceph_entity_addr *addr, char delim, const char **ipend) 1230 { 1231 return -EINVAL; 1232 } 1233 #endif 1234 1235 /* 1236 * Parse a server name (IP or hostname). If a valid IP address is not found 1237 * then try to extract a hostname to resolve using userspace DNS upcall. 1238 */ 1239 static int ceph_parse_server_name(const char *name, size_t namelen, 1240 struct ceph_entity_addr *addr, char delim, const char **ipend) 1241 { 1242 int ret; 1243 1244 ret = ceph_pton(name, namelen, addr, delim, ipend); 1245 if (ret) 1246 ret = ceph_dns_resolve_name(name, namelen, addr, delim, ipend); 1247 1248 return ret; 1249 } 1250 1251 /* 1252 * Parse an ip[:port] list into an addr array. Use the default 1253 * monitor port if a port isn't specified. 1254 */ 1255 int ceph_parse_ips(const char *c, const char *end, 1256 struct ceph_entity_addr *addr, 1257 int max_count, int *count) 1258 { 1259 int i, ret = -EINVAL; 1260 const char *p = c; 1261 1262 dout("parse_ips on '%.*s'\n", (int)(end-c), c); 1263 for (i = 0; i < max_count; i++) { 1264 const char *ipend; 1265 int port; 1266 char delim = ','; 1267 1268 if (*p == '[') { 1269 delim = ']'; 1270 p++; 1271 } 1272 1273 ret = ceph_parse_server_name(p, end - p, &addr[i], delim, &ipend); 1274 if (ret) 1275 goto bad; 1276 ret = -EINVAL; 1277 1278 p = ipend; 1279 1280 if (delim == ']') { 1281 if (*p != ']') { 1282 dout("missing matching ']'\n"); 1283 goto bad; 1284 } 1285 p++; 1286 } 1287 1288 /* port? */ 1289 if (p < end && *p == ':') { 1290 port = 0; 1291 p++; 1292 while (p < end && *p >= '0' && *p <= '9') { 1293 port = (port * 10) + (*p - '0'); 1294 p++; 1295 } 1296 if (port == 0) 1297 port = CEPH_MON_PORT; 1298 else if (port > 65535) 1299 goto bad; 1300 } else { 1301 port = CEPH_MON_PORT; 1302 } 1303 1304 ceph_addr_set_port(&addr[i], port); 1305 addr[i].type = CEPH_ENTITY_ADDR_TYPE_LEGACY; 1306 1307 dout("parse_ips got %s\n", ceph_pr_addr(&addr[i])); 1308 1309 if (p == end) 1310 break; 1311 if (*p != ',') 1312 goto bad; 1313 p++; 1314 } 1315 1316 if (p != end) 1317 goto bad; 1318 1319 if (count) 1320 *count = i + 1; 1321 return 0; 1322 1323 bad: 1324 return ret; 1325 } 1326 1327 /* 1328 * Process message. This happens in the worker thread. The callback should 1329 * be careful not to do anything that waits on other incoming messages or it 1330 * may deadlock. 1331 */ 1332 void ceph_con_process_message(struct ceph_connection *con) 1333 { 1334 struct ceph_msg *msg = con->in_msg; 1335 1336 BUG_ON(con->in_msg->con != con); 1337 con->in_msg = NULL; 1338 1339 /* if first message, set peer_name */ 1340 if (con->peer_name.type == 0) 1341 con->peer_name = msg->hdr.src; 1342 1343 con->in_seq++; 1344 mutex_unlock(&con->mutex); 1345 1346 dout("===== %p %llu from %s%lld %d=%s len %d+%d+%d (%u %u %u) =====\n", 1347 msg, le64_to_cpu(msg->hdr.seq), 1348 ENTITY_NAME(msg->hdr.src), 1349 le16_to_cpu(msg->hdr.type), 1350 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), 1351 le32_to_cpu(msg->hdr.front_len), 1352 le32_to_cpu(msg->hdr.middle_len), 1353 le32_to_cpu(msg->hdr.data_len), 1354 con->in_front_crc, con->in_middle_crc, con->in_data_crc); 1355 con->ops->dispatch(con, msg); 1356 1357 mutex_lock(&con->mutex); 1358 } 1359 1360 /* 1361 * Atomically queue work on a connection after the specified delay. 1362 * Bump @con reference to avoid races with connection teardown. 1363 * Returns 0 if work was queued, or an error code otherwise. 1364 */ 1365 static int queue_con_delay(struct ceph_connection *con, unsigned long delay) 1366 { 1367 if (!con->ops->get(con)) { 1368 dout("%s %p ref count 0\n", __func__, con); 1369 return -ENOENT; 1370 } 1371 1372 if (delay >= HZ) 1373 delay = round_jiffies_relative(delay); 1374 1375 dout("%s %p %lu\n", __func__, con, delay); 1376 if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) { 1377 dout("%s %p - already queued\n", __func__, con); 1378 con->ops->put(con); 1379 return -EBUSY; 1380 } 1381 1382 return 0; 1383 } 1384 1385 static void queue_con(struct ceph_connection *con) 1386 { 1387 (void) queue_con_delay(con, 0); 1388 } 1389 1390 static void cancel_con(struct ceph_connection *con) 1391 { 1392 if (cancel_delayed_work(&con->work)) { 1393 dout("%s %p\n", __func__, con); 1394 con->ops->put(con); 1395 } 1396 } 1397 1398 static bool con_sock_closed(struct ceph_connection *con) 1399 { 1400 if (!ceph_con_flag_test_and_clear(con, CEPH_CON_F_SOCK_CLOSED)) 1401 return false; 1402 1403 #define CASE(x) \ 1404 case CEPH_CON_S_ ## x: \ 1405 con->error_msg = "socket closed (con state " #x ")"; \ 1406 break; 1407 1408 switch (con->state) { 1409 CASE(CLOSED); 1410 CASE(PREOPEN); 1411 CASE(V1_BANNER); 1412 CASE(V1_CONNECT_MSG); 1413 CASE(OPEN); 1414 CASE(STANDBY); 1415 default: 1416 BUG(); 1417 } 1418 #undef CASE 1419 1420 return true; 1421 } 1422 1423 static bool con_backoff(struct ceph_connection *con) 1424 { 1425 int ret; 1426 1427 if (!ceph_con_flag_test_and_clear(con, CEPH_CON_F_BACKOFF)) 1428 return false; 1429 1430 ret = queue_con_delay(con, con->delay); 1431 if (ret) { 1432 dout("%s: con %p FAILED to back off %lu\n", __func__, 1433 con, con->delay); 1434 BUG_ON(ret == -ENOENT); 1435 ceph_con_flag_set(con, CEPH_CON_F_BACKOFF); 1436 } 1437 1438 return true; 1439 } 1440 1441 /* Finish fault handling; con->mutex must *not* be held here */ 1442 1443 static void con_fault_finish(struct ceph_connection *con) 1444 { 1445 dout("%s %p\n", __func__, con); 1446 1447 /* 1448 * in case we faulted due to authentication, invalidate our 1449 * current tickets so that we can get new ones. 1450 */ 1451 if (con->auth_retry) { 1452 dout("auth_retry %d, invalidating\n", con->auth_retry); 1453 if (con->ops->invalidate_authorizer) 1454 con->ops->invalidate_authorizer(con); 1455 con->auth_retry = 0; 1456 } 1457 1458 if (con->ops->fault) 1459 con->ops->fault(con); 1460 } 1461 1462 /* 1463 * Do some work on a connection. Drop a connection ref when we're done. 1464 */ 1465 static void ceph_con_workfn(struct work_struct *work) 1466 { 1467 struct ceph_connection *con = container_of(work, struct ceph_connection, 1468 work.work); 1469 bool fault; 1470 1471 mutex_lock(&con->mutex); 1472 while (true) { 1473 int ret; 1474 1475 if ((fault = con_sock_closed(con))) { 1476 dout("%s: con %p SOCK_CLOSED\n", __func__, con); 1477 break; 1478 } 1479 if (con_backoff(con)) { 1480 dout("%s: con %p BACKOFF\n", __func__, con); 1481 break; 1482 } 1483 if (con->state == CEPH_CON_S_STANDBY) { 1484 dout("%s: con %p STANDBY\n", __func__, con); 1485 break; 1486 } 1487 if (con->state == CEPH_CON_S_CLOSED) { 1488 dout("%s: con %p CLOSED\n", __func__, con); 1489 BUG_ON(con->sock); 1490 break; 1491 } 1492 if (con->state == CEPH_CON_S_PREOPEN) { 1493 dout("%s: con %p PREOPEN\n", __func__, con); 1494 BUG_ON(con->sock); 1495 } 1496 1497 ret = ceph_con_v1_try_read(con); 1498 if (ret < 0) { 1499 if (ret == -EAGAIN) 1500 continue; 1501 if (!con->error_msg) 1502 con->error_msg = "socket error on read"; 1503 fault = true; 1504 break; 1505 } 1506 1507 ret = ceph_con_v1_try_write(con); 1508 if (ret < 0) { 1509 if (ret == -EAGAIN) 1510 continue; 1511 if (!con->error_msg) 1512 con->error_msg = "socket error on write"; 1513 fault = true; 1514 } 1515 1516 break; /* If we make it to here, we're done */ 1517 } 1518 if (fault) 1519 con_fault(con); 1520 mutex_unlock(&con->mutex); 1521 1522 if (fault) 1523 con_fault_finish(con); 1524 1525 con->ops->put(con); 1526 } 1527 1528 /* 1529 * Generic error/fault handler. A retry mechanism is used with 1530 * exponential backoff 1531 */ 1532 static void con_fault(struct ceph_connection *con) 1533 { 1534 dout("fault %p state %d to peer %s\n", 1535 con, con->state, ceph_pr_addr(&con->peer_addr)); 1536 1537 pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name), 1538 ceph_pr_addr(&con->peer_addr), con->error_msg); 1539 con->error_msg = NULL; 1540 1541 WARN_ON(con->state != CEPH_CON_S_V1_BANNER && 1542 con->state != CEPH_CON_S_V1_CONNECT_MSG && 1543 con->state != CEPH_CON_S_OPEN); 1544 1545 ceph_con_reset_protocol(con); 1546 1547 if (ceph_con_flag_test(con, CEPH_CON_F_LOSSYTX)) { 1548 dout("fault on LOSSYTX channel, marking CLOSED\n"); 1549 con->state = CEPH_CON_S_CLOSED; 1550 return; 1551 } 1552 1553 /* Requeue anything that hasn't been acked */ 1554 list_splice_init(&con->out_sent, &con->out_queue); 1555 1556 /* If there are no messages queued or keepalive pending, place 1557 * the connection in a STANDBY state */ 1558 if (list_empty(&con->out_queue) && 1559 !ceph_con_flag_test(con, CEPH_CON_F_KEEPALIVE_PENDING)) { 1560 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con); 1561 ceph_con_flag_clear(con, CEPH_CON_F_WRITE_PENDING); 1562 con->state = CEPH_CON_S_STANDBY; 1563 } else { 1564 /* retry after a delay. */ 1565 con->state = CEPH_CON_S_PREOPEN; 1566 if (!con->delay) { 1567 con->delay = BASE_DELAY_INTERVAL; 1568 } else if (con->delay < MAX_DELAY_INTERVAL) { 1569 con->delay *= 2; 1570 if (con->delay > MAX_DELAY_INTERVAL) 1571 con->delay = MAX_DELAY_INTERVAL; 1572 } 1573 ceph_con_flag_set(con, CEPH_CON_F_BACKOFF); 1574 queue_con(con); 1575 } 1576 } 1577 1578 void ceph_messenger_reset_nonce(struct ceph_messenger *msgr) 1579 { 1580 u32 nonce = le32_to_cpu(msgr->inst.addr.nonce) + 1000000; 1581 msgr->inst.addr.nonce = cpu_to_le32(nonce); 1582 ceph_encode_my_addr(msgr); 1583 } 1584 1585 /* 1586 * initialize a new messenger instance 1587 */ 1588 void ceph_messenger_init(struct ceph_messenger *msgr, 1589 struct ceph_entity_addr *myaddr) 1590 { 1591 spin_lock_init(&msgr->global_seq_lock); 1592 1593 if (myaddr) { 1594 memcpy(&msgr->inst.addr.in_addr, &myaddr->in_addr, 1595 sizeof(msgr->inst.addr.in_addr)); 1596 ceph_addr_set_port(&msgr->inst.addr, 0); 1597 } 1598 1599 msgr->inst.addr.type = 0; 1600 1601 /* generate a random non-zero nonce */ 1602 do { 1603 get_random_bytes(&msgr->inst.addr.nonce, 1604 sizeof(msgr->inst.addr.nonce)); 1605 } while (!msgr->inst.addr.nonce); 1606 ceph_encode_my_addr(msgr); 1607 1608 atomic_set(&msgr->stopping, 0); 1609 write_pnet(&msgr->net, get_net(current->nsproxy->net_ns)); 1610 1611 dout("%s %p\n", __func__, msgr); 1612 } 1613 1614 void ceph_messenger_fini(struct ceph_messenger *msgr) 1615 { 1616 put_net(read_pnet(&msgr->net)); 1617 } 1618 1619 static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con) 1620 { 1621 if (msg->con) 1622 msg->con->ops->put(msg->con); 1623 1624 msg->con = con ? con->ops->get(con) : NULL; 1625 BUG_ON(msg->con != con); 1626 } 1627 1628 static void clear_standby(struct ceph_connection *con) 1629 { 1630 /* come back from STANDBY? */ 1631 if (con->state == CEPH_CON_S_STANDBY) { 1632 dout("clear_standby %p and ++connect_seq\n", con); 1633 con->state = CEPH_CON_S_PREOPEN; 1634 con->connect_seq++; 1635 WARN_ON(ceph_con_flag_test(con, CEPH_CON_F_WRITE_PENDING)); 1636 WARN_ON(ceph_con_flag_test(con, CEPH_CON_F_KEEPALIVE_PENDING)); 1637 } 1638 } 1639 1640 /* 1641 * Queue up an outgoing message on the given connection. 1642 * 1643 * Consumes a ref on @msg. 1644 */ 1645 void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg) 1646 { 1647 /* set src+dst */ 1648 msg->hdr.src = con->msgr->inst.name; 1649 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len)); 1650 msg->needs_out_seq = true; 1651 1652 mutex_lock(&con->mutex); 1653 1654 if (con->state == CEPH_CON_S_CLOSED) { 1655 dout("con_send %p closed, dropping %p\n", con, msg); 1656 ceph_msg_put(msg); 1657 mutex_unlock(&con->mutex); 1658 return; 1659 } 1660 1661 msg_con_set(msg, con); 1662 1663 BUG_ON(!list_empty(&msg->list_head)); 1664 list_add_tail(&msg->list_head, &con->out_queue); 1665 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg, 1666 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type), 1667 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), 1668 le32_to_cpu(msg->hdr.front_len), 1669 le32_to_cpu(msg->hdr.middle_len), 1670 le32_to_cpu(msg->hdr.data_len)); 1671 1672 clear_standby(con); 1673 mutex_unlock(&con->mutex); 1674 1675 /* if there wasn't anything waiting to send before, queue 1676 * new work */ 1677 if (!ceph_con_flag_test_and_set(con, CEPH_CON_F_WRITE_PENDING)) 1678 queue_con(con); 1679 } 1680 EXPORT_SYMBOL(ceph_con_send); 1681 1682 /* 1683 * Revoke a message that was previously queued for send 1684 */ 1685 void ceph_msg_revoke(struct ceph_msg *msg) 1686 { 1687 struct ceph_connection *con = msg->con; 1688 1689 if (!con) { 1690 dout("%s msg %p null con\n", __func__, msg); 1691 return; /* Message not in our possession */ 1692 } 1693 1694 mutex_lock(&con->mutex); 1695 if (list_empty(&msg->list_head)) { 1696 WARN_ON(con->out_msg == msg); 1697 dout("%s con %p msg %p not linked\n", __func__, con, msg); 1698 mutex_unlock(&con->mutex); 1699 return; 1700 } 1701 1702 dout("%s con %p msg %p was linked\n", __func__, con, msg); 1703 msg->hdr.seq = 0; 1704 ceph_msg_remove(msg); 1705 1706 if (con->out_msg == msg) { 1707 WARN_ON(con->state != CEPH_CON_S_OPEN); 1708 dout("%s con %p msg %p was sending\n", __func__, con, msg); 1709 ceph_con_v1_revoke(con); 1710 ceph_msg_put(con->out_msg); 1711 con->out_msg = NULL; 1712 } else { 1713 dout("%s con %p msg %p not current, out_msg %p\n", __func__, 1714 con, msg, con->out_msg); 1715 } 1716 mutex_unlock(&con->mutex); 1717 } 1718 1719 /* 1720 * Revoke a message that we may be reading data into 1721 */ 1722 void ceph_msg_revoke_incoming(struct ceph_msg *msg) 1723 { 1724 struct ceph_connection *con = msg->con; 1725 1726 if (!con) { 1727 dout("%s msg %p null con\n", __func__, msg); 1728 return; /* Message not in our possession */ 1729 } 1730 1731 mutex_lock(&con->mutex); 1732 if (con->in_msg == msg) { 1733 WARN_ON(con->state != CEPH_CON_S_OPEN); 1734 dout("%s con %p msg %p was recving\n", __func__, con, msg); 1735 ceph_con_v1_revoke_incoming(con); 1736 ceph_msg_put(con->in_msg); 1737 con->in_msg = NULL; 1738 } else { 1739 dout("%s con %p msg %p not current, in_msg %p\n", __func__, 1740 con, msg, con->in_msg); 1741 } 1742 mutex_unlock(&con->mutex); 1743 } 1744 1745 /* 1746 * Queue a keepalive byte to ensure the tcp connection is alive. 1747 */ 1748 void ceph_con_keepalive(struct ceph_connection *con) 1749 { 1750 dout("con_keepalive %p\n", con); 1751 mutex_lock(&con->mutex); 1752 clear_standby(con); 1753 ceph_con_flag_set(con, CEPH_CON_F_KEEPALIVE_PENDING); 1754 mutex_unlock(&con->mutex); 1755 1756 if (!ceph_con_flag_test_and_set(con, CEPH_CON_F_WRITE_PENDING)) 1757 queue_con(con); 1758 } 1759 EXPORT_SYMBOL(ceph_con_keepalive); 1760 1761 bool ceph_con_keepalive_expired(struct ceph_connection *con, 1762 unsigned long interval) 1763 { 1764 if (interval > 0 && 1765 (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) { 1766 struct timespec64 now; 1767 struct timespec64 ts; 1768 ktime_get_real_ts64(&now); 1769 jiffies_to_timespec64(interval, &ts); 1770 ts = timespec64_add(con->last_keepalive_ack, ts); 1771 return timespec64_compare(&now, &ts) >= 0; 1772 } 1773 return false; 1774 } 1775 1776 static struct ceph_msg_data *ceph_msg_data_add(struct ceph_msg *msg) 1777 { 1778 BUG_ON(msg->num_data_items >= msg->max_data_items); 1779 return &msg->data[msg->num_data_items++]; 1780 } 1781 1782 static void ceph_msg_data_destroy(struct ceph_msg_data *data) 1783 { 1784 if (data->type == CEPH_MSG_DATA_PAGES && data->own_pages) { 1785 int num_pages = calc_pages_for(data->alignment, data->length); 1786 ceph_release_page_vector(data->pages, num_pages); 1787 } else if (data->type == CEPH_MSG_DATA_PAGELIST) { 1788 ceph_pagelist_release(data->pagelist); 1789 } 1790 } 1791 1792 void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages, 1793 size_t length, size_t alignment, bool own_pages) 1794 { 1795 struct ceph_msg_data *data; 1796 1797 BUG_ON(!pages); 1798 BUG_ON(!length); 1799 1800 data = ceph_msg_data_add(msg); 1801 data->type = CEPH_MSG_DATA_PAGES; 1802 data->pages = pages; 1803 data->length = length; 1804 data->alignment = alignment & ~PAGE_MASK; 1805 data->own_pages = own_pages; 1806 1807 msg->data_length += length; 1808 } 1809 EXPORT_SYMBOL(ceph_msg_data_add_pages); 1810 1811 void ceph_msg_data_add_pagelist(struct ceph_msg *msg, 1812 struct ceph_pagelist *pagelist) 1813 { 1814 struct ceph_msg_data *data; 1815 1816 BUG_ON(!pagelist); 1817 BUG_ON(!pagelist->length); 1818 1819 data = ceph_msg_data_add(msg); 1820 data->type = CEPH_MSG_DATA_PAGELIST; 1821 refcount_inc(&pagelist->refcnt); 1822 data->pagelist = pagelist; 1823 1824 msg->data_length += pagelist->length; 1825 } 1826 EXPORT_SYMBOL(ceph_msg_data_add_pagelist); 1827 1828 #ifdef CONFIG_BLOCK 1829 void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos, 1830 u32 length) 1831 { 1832 struct ceph_msg_data *data; 1833 1834 data = ceph_msg_data_add(msg); 1835 data->type = CEPH_MSG_DATA_BIO; 1836 data->bio_pos = *bio_pos; 1837 data->bio_length = length; 1838 1839 msg->data_length += length; 1840 } 1841 EXPORT_SYMBOL(ceph_msg_data_add_bio); 1842 #endif /* CONFIG_BLOCK */ 1843 1844 void ceph_msg_data_add_bvecs(struct ceph_msg *msg, 1845 struct ceph_bvec_iter *bvec_pos) 1846 { 1847 struct ceph_msg_data *data; 1848 1849 data = ceph_msg_data_add(msg); 1850 data->type = CEPH_MSG_DATA_BVECS; 1851 data->bvec_pos = *bvec_pos; 1852 1853 msg->data_length += bvec_pos->iter.bi_size; 1854 } 1855 EXPORT_SYMBOL(ceph_msg_data_add_bvecs); 1856 1857 /* 1858 * construct a new message with given type, size 1859 * the new msg has a ref count of 1. 1860 */ 1861 struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items, 1862 gfp_t flags, bool can_fail) 1863 { 1864 struct ceph_msg *m; 1865 1866 m = kmem_cache_zalloc(ceph_msg_cache, flags); 1867 if (m == NULL) 1868 goto out; 1869 1870 m->hdr.type = cpu_to_le16(type); 1871 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT); 1872 m->hdr.front_len = cpu_to_le32(front_len); 1873 1874 INIT_LIST_HEAD(&m->list_head); 1875 kref_init(&m->kref); 1876 1877 /* front */ 1878 if (front_len) { 1879 m->front.iov_base = ceph_kvmalloc(front_len, flags); 1880 if (m->front.iov_base == NULL) { 1881 dout("ceph_msg_new can't allocate %d bytes\n", 1882 front_len); 1883 goto out2; 1884 } 1885 } else { 1886 m->front.iov_base = NULL; 1887 } 1888 m->front_alloc_len = m->front.iov_len = front_len; 1889 1890 if (max_data_items) { 1891 m->data = kmalloc_array(max_data_items, sizeof(*m->data), 1892 flags); 1893 if (!m->data) 1894 goto out2; 1895 1896 m->max_data_items = max_data_items; 1897 } 1898 1899 dout("ceph_msg_new %p front %d\n", m, front_len); 1900 return m; 1901 1902 out2: 1903 ceph_msg_put(m); 1904 out: 1905 if (!can_fail) { 1906 pr_err("msg_new can't create type %d front %d\n", type, 1907 front_len); 1908 WARN_ON(1); 1909 } else { 1910 dout("msg_new can't create type %d front %d\n", type, 1911 front_len); 1912 } 1913 return NULL; 1914 } 1915 EXPORT_SYMBOL(ceph_msg_new2); 1916 1917 struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags, 1918 bool can_fail) 1919 { 1920 return ceph_msg_new2(type, front_len, 0, flags, can_fail); 1921 } 1922 EXPORT_SYMBOL(ceph_msg_new); 1923 1924 /* 1925 * Allocate "middle" portion of a message, if it is needed and wasn't 1926 * allocated by alloc_msg. This allows us to read a small fixed-size 1927 * per-type header in the front and then gracefully fail (i.e., 1928 * propagate the error to the caller based on info in the front) when 1929 * the middle is too large. 1930 */ 1931 static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg) 1932 { 1933 int type = le16_to_cpu(msg->hdr.type); 1934 int middle_len = le32_to_cpu(msg->hdr.middle_len); 1935 1936 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type, 1937 ceph_msg_type_name(type), middle_len); 1938 BUG_ON(!middle_len); 1939 BUG_ON(msg->middle); 1940 1941 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS); 1942 if (!msg->middle) 1943 return -ENOMEM; 1944 return 0; 1945 } 1946 1947 /* 1948 * Allocate a message for receiving an incoming message on a 1949 * connection, and save the result in con->in_msg. Uses the 1950 * connection's private alloc_msg op if available. 1951 * 1952 * Returns 0 on success, or a negative error code. 1953 * 1954 * On success, if we set *skip = 1: 1955 * - the next message should be skipped and ignored. 1956 * - con->in_msg == NULL 1957 * or if we set *skip = 0: 1958 * - con->in_msg is non-null. 1959 * On error (ENOMEM, EAGAIN, ...), 1960 * - con->in_msg == NULL 1961 */ 1962 int ceph_con_in_msg_alloc(struct ceph_connection *con, 1963 struct ceph_msg_header *hdr, int *skip) 1964 { 1965 int middle_len = le32_to_cpu(hdr->middle_len); 1966 struct ceph_msg *msg; 1967 int ret = 0; 1968 1969 BUG_ON(con->in_msg != NULL); 1970 BUG_ON(!con->ops->alloc_msg); 1971 1972 mutex_unlock(&con->mutex); 1973 msg = con->ops->alloc_msg(con, hdr, skip); 1974 mutex_lock(&con->mutex); 1975 if (con->state != CEPH_CON_S_OPEN) { 1976 if (msg) 1977 ceph_msg_put(msg); 1978 return -EAGAIN; 1979 } 1980 if (msg) { 1981 BUG_ON(*skip); 1982 msg_con_set(msg, con); 1983 con->in_msg = msg; 1984 } else { 1985 /* 1986 * Null message pointer means either we should skip 1987 * this message or we couldn't allocate memory. The 1988 * former is not an error. 1989 */ 1990 if (*skip) 1991 return 0; 1992 1993 con->error_msg = "error allocating memory for incoming message"; 1994 return -ENOMEM; 1995 } 1996 memcpy(&con->in_msg->hdr, hdr, sizeof(*hdr)); 1997 1998 if (middle_len && !con->in_msg->middle) { 1999 ret = ceph_alloc_middle(con, con->in_msg); 2000 if (ret < 0) { 2001 ceph_msg_put(con->in_msg); 2002 con->in_msg = NULL; 2003 } 2004 } 2005 2006 return ret; 2007 } 2008 2009 void ceph_con_get_out_msg(struct ceph_connection *con) 2010 { 2011 struct ceph_msg *msg; 2012 2013 BUG_ON(list_empty(&con->out_queue)); 2014 msg = list_first_entry(&con->out_queue, struct ceph_msg, list_head); 2015 WARN_ON(msg->con != con); 2016 2017 /* 2018 * Put the message on "sent" list using a ref from ceph_con_send(). 2019 * It is put when the message is acked or revoked. 2020 */ 2021 list_move_tail(&msg->list_head, &con->out_sent); 2022 2023 /* 2024 * Only assign outgoing seq # if we haven't sent this message 2025 * yet. If it is requeued, resend with it's original seq. 2026 */ 2027 if (msg->needs_out_seq) { 2028 msg->hdr.seq = cpu_to_le64(++con->out_seq); 2029 msg->needs_out_seq = false; 2030 2031 if (con->ops->reencode_message) 2032 con->ops->reencode_message(msg); 2033 } 2034 2035 /* 2036 * Get a ref for out_msg. It is put when we are done sending the 2037 * message or in case of a fault. 2038 */ 2039 WARN_ON(con->out_msg); 2040 con->out_msg = ceph_msg_get(msg); 2041 } 2042 2043 /* 2044 * Free a generically kmalloc'd message. 2045 */ 2046 static void ceph_msg_free(struct ceph_msg *m) 2047 { 2048 dout("%s %p\n", __func__, m); 2049 kvfree(m->front.iov_base); 2050 kfree(m->data); 2051 kmem_cache_free(ceph_msg_cache, m); 2052 } 2053 2054 static void ceph_msg_release(struct kref *kref) 2055 { 2056 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref); 2057 int i; 2058 2059 dout("%s %p\n", __func__, m); 2060 WARN_ON(!list_empty(&m->list_head)); 2061 2062 msg_con_set(m, NULL); 2063 2064 /* drop middle, data, if any */ 2065 if (m->middle) { 2066 ceph_buffer_put(m->middle); 2067 m->middle = NULL; 2068 } 2069 2070 for (i = 0; i < m->num_data_items; i++) 2071 ceph_msg_data_destroy(&m->data[i]); 2072 2073 if (m->pool) 2074 ceph_msgpool_put(m->pool, m); 2075 else 2076 ceph_msg_free(m); 2077 } 2078 2079 struct ceph_msg *ceph_msg_get(struct ceph_msg *msg) 2080 { 2081 dout("%s %p (was %d)\n", __func__, msg, 2082 kref_read(&msg->kref)); 2083 kref_get(&msg->kref); 2084 return msg; 2085 } 2086 EXPORT_SYMBOL(ceph_msg_get); 2087 2088 void ceph_msg_put(struct ceph_msg *msg) 2089 { 2090 dout("%s %p (was %d)\n", __func__, msg, 2091 kref_read(&msg->kref)); 2092 kref_put(&msg->kref, ceph_msg_release); 2093 } 2094 EXPORT_SYMBOL(ceph_msg_put); 2095 2096 void ceph_msg_dump(struct ceph_msg *msg) 2097 { 2098 pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg, 2099 msg->front_alloc_len, msg->data_length); 2100 print_hex_dump(KERN_DEBUG, "header: ", 2101 DUMP_PREFIX_OFFSET, 16, 1, 2102 &msg->hdr, sizeof(msg->hdr), true); 2103 print_hex_dump(KERN_DEBUG, " front: ", 2104 DUMP_PREFIX_OFFSET, 16, 1, 2105 msg->front.iov_base, msg->front.iov_len, true); 2106 if (msg->middle) 2107 print_hex_dump(KERN_DEBUG, "middle: ", 2108 DUMP_PREFIX_OFFSET, 16, 1, 2109 msg->middle->vec.iov_base, 2110 msg->middle->vec.iov_len, true); 2111 print_hex_dump(KERN_DEBUG, "footer: ", 2112 DUMP_PREFIX_OFFSET, 16, 1, 2113 &msg->footer, sizeof(msg->footer), true); 2114 } 2115 EXPORT_SYMBOL(ceph_msg_dump); 2116