| 1 | #include <linux/ceph/ceph_debug.h> |
| 2 | |
| 3 | #include <linux/crc32c.h> |
| 4 | #include <linux/ctype.h> |
| 5 | #include <linux/highmem.h> |
| 6 | #include <linux/inet.h> |
| 7 | #include <linux/kthread.h> |
| 8 | #include <linux/net.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/socket.h> |
| 11 | #include <linux/string.h> |
| 12 | #include <linux/bio.h> |
| 13 | #include <linux/blkdev.h> |
| 14 | #include <linux/dns_resolver.h> |
| 15 | #include <net/tcp.h> |
| 16 | |
| 17 | #include <linux/ceph/libceph.h> |
| 18 | #include <linux/ceph/messenger.h> |
| 19 | #include <linux/ceph/decode.h> |
| 20 | #include <linux/ceph/pagelist.h> |
| 21 | #include <linux/export.h> |
| 22 | |
| 23 | /* |
| 24 | * Ceph uses the messenger to exchange ceph_msg messages with other |
| 25 | * hosts in the system. The messenger provides ordered and reliable |
| 26 | * delivery. We tolerate TCP disconnects by reconnecting (with |
| 27 | * exponential backoff) in the case of a fault (disconnection, bad |
| 28 | * crc, protocol error). Acks allow sent messages to be discarded by |
| 29 | * the sender. |
| 30 | */ |
| 31 | |
| 32 | /* State values for ceph_connection->sock_state; NEW is assumed to be 0 */ |
| 33 | |
| 34 | #define CON_SOCK_STATE_NEW 0 /* -> CLOSED */ |
| 35 | #define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */ |
| 36 | #define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */ |
| 37 | #define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */ |
| 38 | #define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */ |
| 39 | |
| 40 | /* static tag bytes (protocol control messages) */ |
| 41 | static char tag_msg = CEPH_MSGR_TAG_MSG; |
| 42 | static char tag_ack = CEPH_MSGR_TAG_ACK; |
| 43 | static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE; |
| 44 | |
| 45 | #ifdef CONFIG_LOCKDEP |
| 46 | static struct lock_class_key socket_class; |
| 47 | #endif |
| 48 | |
| 49 | /* |
| 50 | * When skipping (ignoring) a block of input we read it into a "skip |
| 51 | * buffer," which is this many bytes in size. |
| 52 | */ |
| 53 | #define SKIP_BUF_SIZE 1024 |
| 54 | |
| 55 | static void queue_con(struct ceph_connection *con); |
| 56 | static void con_work(struct work_struct *); |
| 57 | static void ceph_fault(struct ceph_connection *con); |
| 58 | |
| 59 | /* |
| 60 | * Nicely render a sockaddr as a string. An array of formatted |
| 61 | * strings is used, to approximate reentrancy. |
| 62 | */ |
| 63 | #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */ |
| 64 | #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG) |
| 65 | #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1) |
| 66 | #define MAX_ADDR_STR_LEN 64 /* 54 is enough */ |
| 67 | |
| 68 | static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN]; |
| 69 | static atomic_t addr_str_seq = ATOMIC_INIT(0); |
| 70 | |
| 71 | static struct page *zero_page; /* used in certain error cases */ |
| 72 | |
| 73 | const char *ceph_pr_addr(const struct sockaddr_storage *ss) |
| 74 | { |
| 75 | int i; |
| 76 | char *s; |
| 77 | struct sockaddr_in *in4 = (struct sockaddr_in *) ss; |
| 78 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss; |
| 79 | |
| 80 | i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK; |
| 81 | s = addr_str[i]; |
| 82 | |
| 83 | switch (ss->ss_family) { |
| 84 | case AF_INET: |
| 85 | snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr, |
| 86 | ntohs(in4->sin_port)); |
| 87 | break; |
| 88 | |
| 89 | case AF_INET6: |
| 90 | snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr, |
| 91 | ntohs(in6->sin6_port)); |
| 92 | break; |
| 93 | |
| 94 | default: |
| 95 | snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)", |
| 96 | ss->ss_family); |
| 97 | } |
| 98 | |
| 99 | return s; |
| 100 | } |
| 101 | EXPORT_SYMBOL(ceph_pr_addr); |
| 102 | |
| 103 | static void encode_my_addr(struct ceph_messenger *msgr) |
| 104 | { |
| 105 | memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr)); |
| 106 | ceph_encode_addr(&msgr->my_enc_addr); |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * work queue for all reading and writing to/from the socket. |
| 111 | */ |
| 112 | static struct workqueue_struct *ceph_msgr_wq; |
| 113 | |
| 114 | void _ceph_msgr_exit(void) |
| 115 | { |
| 116 | if (ceph_msgr_wq) { |
| 117 | destroy_workqueue(ceph_msgr_wq); |
| 118 | ceph_msgr_wq = NULL; |
| 119 | } |
| 120 | |
| 121 | BUG_ON(zero_page == NULL); |
| 122 | kunmap(zero_page); |
| 123 | page_cache_release(zero_page); |
| 124 | zero_page = NULL; |
| 125 | } |
| 126 | |
| 127 | int ceph_msgr_init(void) |
| 128 | { |
| 129 | BUG_ON(zero_page != NULL); |
| 130 | zero_page = ZERO_PAGE(0); |
| 131 | page_cache_get(zero_page); |
| 132 | |
| 133 | ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0); |
| 134 | if (ceph_msgr_wq) |
| 135 | return 0; |
| 136 | |
| 137 | pr_err("msgr_init failed to create workqueue\n"); |
| 138 | _ceph_msgr_exit(); |
| 139 | |
| 140 | return -ENOMEM; |
| 141 | } |
| 142 | EXPORT_SYMBOL(ceph_msgr_init); |
| 143 | |
| 144 | void ceph_msgr_exit(void) |
| 145 | { |
| 146 | BUG_ON(ceph_msgr_wq == NULL); |
| 147 | |
| 148 | _ceph_msgr_exit(); |
| 149 | } |
| 150 | EXPORT_SYMBOL(ceph_msgr_exit); |
| 151 | |
| 152 | void ceph_msgr_flush(void) |
| 153 | { |
| 154 | flush_workqueue(ceph_msgr_wq); |
| 155 | } |
| 156 | EXPORT_SYMBOL(ceph_msgr_flush); |
| 157 | |
| 158 | /* Connection socket state transition functions */ |
| 159 | |
| 160 | static void con_sock_state_init(struct ceph_connection *con) |
| 161 | { |
| 162 | int old_state; |
| 163 | |
| 164 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED); |
| 165 | if (WARN_ON(old_state != CON_SOCK_STATE_NEW)) |
| 166 | printk("%s: unexpected old state %d\n", __func__, old_state); |
| 167 | } |
| 168 | |
| 169 | static void con_sock_state_connecting(struct ceph_connection *con) |
| 170 | { |
| 171 | int old_state; |
| 172 | |
| 173 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING); |
| 174 | if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED)) |
| 175 | printk("%s: unexpected old state %d\n", __func__, old_state); |
| 176 | } |
| 177 | |
| 178 | static void con_sock_state_connected(struct ceph_connection *con) |
| 179 | { |
| 180 | int old_state; |
| 181 | |
| 182 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED); |
| 183 | if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING)) |
| 184 | printk("%s: unexpected old state %d\n", __func__, old_state); |
| 185 | } |
| 186 | |
| 187 | static void con_sock_state_closing(struct ceph_connection *con) |
| 188 | { |
| 189 | int old_state; |
| 190 | |
| 191 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING); |
| 192 | if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING && |
| 193 | old_state != CON_SOCK_STATE_CONNECTED && |
| 194 | old_state != CON_SOCK_STATE_CLOSING)) |
| 195 | printk("%s: unexpected old state %d\n", __func__, old_state); |
| 196 | } |
| 197 | |
| 198 | static void con_sock_state_closed(struct ceph_connection *con) |
| 199 | { |
| 200 | int old_state; |
| 201 | |
| 202 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED); |
| 203 | if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED && |
| 204 | old_state != CON_SOCK_STATE_CLOSING)) |
| 205 | printk("%s: unexpected old state %d\n", __func__, old_state); |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * socket callback functions |
| 210 | */ |
| 211 | |
| 212 | /* data available on socket, or listen socket received a connect */ |
| 213 | static void ceph_sock_data_ready(struct sock *sk, int count_unused) |
| 214 | { |
| 215 | struct ceph_connection *con = sk->sk_user_data; |
| 216 | |
| 217 | if (sk->sk_state != TCP_CLOSE_WAIT) { |
| 218 | dout("%s on %p state = %lu, queueing work\n", __func__, |
| 219 | con, con->state); |
| 220 | queue_con(con); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* socket has buffer space for writing */ |
| 225 | static void ceph_sock_write_space(struct sock *sk) |
| 226 | { |
| 227 | struct ceph_connection *con = sk->sk_user_data; |
| 228 | |
| 229 | /* only queue to workqueue if there is data we want to write, |
| 230 | * and there is sufficient space in the socket buffer to accept |
| 231 | * more data. clear SOCK_NOSPACE so that ceph_sock_write_space() |
| 232 | * doesn't get called again until try_write() fills the socket |
| 233 | * buffer. See net/ipv4/tcp_input.c:tcp_check_space() |
| 234 | * and net/core/stream.c:sk_stream_write_space(). |
| 235 | */ |
| 236 | if (test_bit(WRITE_PENDING, &con->flags)) { |
| 237 | if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { |
| 238 | dout("%s %p queueing write work\n", __func__, con); |
| 239 | clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 240 | queue_con(con); |
| 241 | } |
| 242 | } else { |
| 243 | dout("%s %p nothing to write\n", __func__, con); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /* socket's state has changed */ |
| 248 | static void ceph_sock_state_change(struct sock *sk) |
| 249 | { |
| 250 | struct ceph_connection *con = sk->sk_user_data; |
| 251 | |
| 252 | dout("%s %p state = %lu sk_state = %u\n", __func__, |
| 253 | con, con->state, sk->sk_state); |
| 254 | |
| 255 | if (test_bit(CLOSED, &con->state)) |
| 256 | return; |
| 257 | |
| 258 | switch (sk->sk_state) { |
| 259 | case TCP_CLOSE: |
| 260 | dout("%s TCP_CLOSE\n", __func__); |
| 261 | case TCP_CLOSE_WAIT: |
| 262 | dout("%s TCP_CLOSE_WAIT\n", __func__); |
| 263 | con_sock_state_closing(con); |
| 264 | if (test_and_set_bit(SOCK_CLOSED, &con->flags) == 0) { |
| 265 | if (test_bit(CONNECTING, &con->state)) |
| 266 | con->error_msg = "connection failed"; |
| 267 | else |
| 268 | con->error_msg = "socket closed"; |
| 269 | queue_con(con); |
| 270 | } |
| 271 | break; |
| 272 | case TCP_ESTABLISHED: |
| 273 | dout("%s TCP_ESTABLISHED\n", __func__); |
| 274 | con_sock_state_connected(con); |
| 275 | queue_con(con); |
| 276 | break; |
| 277 | default: /* Everything else is uninteresting */ |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * set up socket callbacks |
| 284 | */ |
| 285 | static void set_sock_callbacks(struct socket *sock, |
| 286 | struct ceph_connection *con) |
| 287 | { |
| 288 | struct sock *sk = sock->sk; |
| 289 | sk->sk_user_data = con; |
| 290 | sk->sk_data_ready = ceph_sock_data_ready; |
| 291 | sk->sk_write_space = ceph_sock_write_space; |
| 292 | sk->sk_state_change = ceph_sock_state_change; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | /* |
| 297 | * socket helpers |
| 298 | */ |
| 299 | |
| 300 | /* |
| 301 | * initiate connection to a remote socket. |
| 302 | */ |
| 303 | static int ceph_tcp_connect(struct ceph_connection *con) |
| 304 | { |
| 305 | struct sockaddr_storage *paddr = &con->peer_addr.in_addr; |
| 306 | struct socket *sock; |
| 307 | int ret; |
| 308 | |
| 309 | BUG_ON(con->sock); |
| 310 | ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM, |
| 311 | IPPROTO_TCP, &sock); |
| 312 | if (ret) |
| 313 | return ret; |
| 314 | sock->sk->sk_allocation = GFP_NOFS; |
| 315 | |
| 316 | #ifdef CONFIG_LOCKDEP |
| 317 | lockdep_set_class(&sock->sk->sk_lock, &socket_class); |
| 318 | #endif |
| 319 | |
| 320 | set_sock_callbacks(sock, con); |
| 321 | |
| 322 | dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr)); |
| 323 | |
| 324 | ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr), |
| 325 | O_NONBLOCK); |
| 326 | if (ret == -EINPROGRESS) { |
| 327 | dout("connect %s EINPROGRESS sk_state = %u\n", |
| 328 | ceph_pr_addr(&con->peer_addr.in_addr), |
| 329 | sock->sk->sk_state); |
| 330 | } else if (ret < 0) { |
| 331 | pr_err("connect %s error %d\n", |
| 332 | ceph_pr_addr(&con->peer_addr.in_addr), ret); |
| 333 | sock_release(sock); |
| 334 | con->error_msg = "connect error"; |
| 335 | |
| 336 | return ret; |
| 337 | } |
| 338 | con->sock = sock; |
| 339 | con_sock_state_connecting(con); |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len) |
| 345 | { |
| 346 | struct kvec iov = {buf, len}; |
| 347 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; |
| 348 | int r; |
| 349 | |
| 350 | r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags); |
| 351 | if (r == -EAGAIN) |
| 352 | r = 0; |
| 353 | return r; |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * write something. @more is true if caller will be sending more data |
| 358 | * shortly. |
| 359 | */ |
| 360 | static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov, |
| 361 | size_t kvlen, size_t len, int more) |
| 362 | { |
| 363 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; |
| 364 | int r; |
| 365 | |
| 366 | if (more) |
| 367 | msg.msg_flags |= MSG_MORE; |
| 368 | else |
| 369 | msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */ |
| 370 | |
| 371 | r = kernel_sendmsg(sock, &msg, iov, kvlen, len); |
| 372 | if (r == -EAGAIN) |
| 373 | r = 0; |
| 374 | return r; |
| 375 | } |
| 376 | |
| 377 | static int ceph_tcp_sendpage(struct socket *sock, struct page *page, |
| 378 | int offset, size_t size, int more) |
| 379 | { |
| 380 | int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR); |
| 381 | int ret; |
| 382 | |
| 383 | ret = kernel_sendpage(sock, page, offset, size, flags); |
| 384 | if (ret == -EAGAIN) |
| 385 | ret = 0; |
| 386 | |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | |
| 391 | /* |
| 392 | * Shutdown/close the socket for the given connection. |
| 393 | */ |
| 394 | static int con_close_socket(struct ceph_connection *con) |
| 395 | { |
| 396 | int rc; |
| 397 | |
| 398 | dout("con_close_socket on %p sock %p\n", con, con->sock); |
| 399 | if (!con->sock) |
| 400 | return 0; |
| 401 | set_bit(SOCK_CLOSED, &con->state); |
| 402 | rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR); |
| 403 | sock_release(con->sock); |
| 404 | con->sock = NULL; |
| 405 | clear_bit(SOCK_CLOSED, &con->state); |
| 406 | con_sock_state_closed(con); |
| 407 | return rc; |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * Reset a connection. Discard all incoming and outgoing messages |
| 412 | * and clear *_seq state. |
| 413 | */ |
| 414 | static void ceph_msg_remove(struct ceph_msg *msg) |
| 415 | { |
| 416 | list_del_init(&msg->list_head); |
| 417 | ceph_msg_put(msg); |
| 418 | } |
| 419 | static void ceph_msg_remove_list(struct list_head *head) |
| 420 | { |
| 421 | while (!list_empty(head)) { |
| 422 | struct ceph_msg *msg = list_first_entry(head, struct ceph_msg, |
| 423 | list_head); |
| 424 | ceph_msg_remove(msg); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | static void reset_connection(struct ceph_connection *con) |
| 429 | { |
| 430 | /* reset connection, out_queue, msg_ and connect_seq */ |
| 431 | /* discard existing out_queue and msg_seq */ |
| 432 | ceph_msg_remove_list(&con->out_queue); |
| 433 | ceph_msg_remove_list(&con->out_sent); |
| 434 | |
| 435 | if (con->in_msg) { |
| 436 | ceph_msg_put(con->in_msg); |
| 437 | con->in_msg = NULL; |
| 438 | } |
| 439 | |
| 440 | con->connect_seq = 0; |
| 441 | con->out_seq = 0; |
| 442 | if (con->out_msg) { |
| 443 | ceph_msg_put(con->out_msg); |
| 444 | con->out_msg = NULL; |
| 445 | } |
| 446 | con->in_seq = 0; |
| 447 | con->in_seq_acked = 0; |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * mark a peer down. drop any open connections. |
| 452 | */ |
| 453 | void ceph_con_close(struct ceph_connection *con) |
| 454 | { |
| 455 | dout("con_close %p peer %s\n", con, |
| 456 | ceph_pr_addr(&con->peer_addr.in_addr)); |
| 457 | set_bit(CLOSED, &con->state); /* in case there's queued work */ |
| 458 | clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */ |
| 459 | clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */ |
| 460 | clear_bit(KEEPALIVE_PENDING, &con->flags); |
| 461 | clear_bit(WRITE_PENDING, &con->flags); |
| 462 | mutex_lock(&con->mutex); |
| 463 | reset_connection(con); |
| 464 | con->peer_global_seq = 0; |
| 465 | cancel_delayed_work(&con->work); |
| 466 | mutex_unlock(&con->mutex); |
| 467 | queue_con(con); |
| 468 | } |
| 469 | EXPORT_SYMBOL(ceph_con_close); |
| 470 | |
| 471 | /* |
| 472 | * Reopen a closed connection, with a new peer address. |
| 473 | */ |
| 474 | void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr) |
| 475 | { |
| 476 | dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr)); |
| 477 | set_bit(OPENING, &con->state); |
| 478 | clear_bit(CLOSED, &con->state); |
| 479 | memcpy(&con->peer_addr, addr, sizeof(*addr)); |
| 480 | con->delay = 0; /* reset backoff memory */ |
| 481 | queue_con(con); |
| 482 | } |
| 483 | EXPORT_SYMBOL(ceph_con_open); |
| 484 | |
| 485 | /* |
| 486 | * return true if this connection ever successfully opened |
| 487 | */ |
| 488 | bool ceph_con_opened(struct ceph_connection *con) |
| 489 | { |
| 490 | return con->connect_seq > 0; |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * generic get/put |
| 495 | */ |
| 496 | struct ceph_connection *ceph_con_get(struct ceph_connection *con) |
| 497 | { |
| 498 | int nref = __atomic_add_unless(&con->nref, 1, 0); |
| 499 | |
| 500 | dout("con_get %p nref = %d -> %d\n", con, nref, nref + 1); |
| 501 | |
| 502 | return nref ? con : NULL; |
| 503 | } |
| 504 | |
| 505 | void ceph_con_put(struct ceph_connection *con) |
| 506 | { |
| 507 | int nref = atomic_dec_return(&con->nref); |
| 508 | |
| 509 | BUG_ON(nref < 0); |
| 510 | if (nref == 0) { |
| 511 | BUG_ON(con->sock); |
| 512 | kfree(con); |
| 513 | } |
| 514 | dout("con_put %p nref = %d -> %d\n", con, nref + 1, nref); |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * initialize a new connection. |
| 519 | */ |
| 520 | void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con) |
| 521 | { |
| 522 | dout("con_init %p\n", con); |
| 523 | memset(con, 0, sizeof(*con)); |
| 524 | atomic_set(&con->nref, 1); |
| 525 | con->msgr = msgr; |
| 526 | |
| 527 | con_sock_state_init(con); |
| 528 | |
| 529 | mutex_init(&con->mutex); |
| 530 | INIT_LIST_HEAD(&con->out_queue); |
| 531 | INIT_LIST_HEAD(&con->out_sent); |
| 532 | INIT_DELAYED_WORK(&con->work, con_work); |
| 533 | } |
| 534 | EXPORT_SYMBOL(ceph_con_init); |
| 535 | |
| 536 | |
| 537 | /* |
| 538 | * We maintain a global counter to order connection attempts. Get |
| 539 | * a unique seq greater than @gt. |
| 540 | */ |
| 541 | static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt) |
| 542 | { |
| 543 | u32 ret; |
| 544 | |
| 545 | spin_lock(&msgr->global_seq_lock); |
| 546 | if (msgr->global_seq < gt) |
| 547 | msgr->global_seq = gt; |
| 548 | ret = ++msgr->global_seq; |
| 549 | spin_unlock(&msgr->global_seq_lock); |
| 550 | return ret; |
| 551 | } |
| 552 | |
| 553 | static void con_out_kvec_reset(struct ceph_connection *con) |
| 554 | { |
| 555 | con->out_kvec_left = 0; |
| 556 | con->out_kvec_bytes = 0; |
| 557 | con->out_kvec_cur = &con->out_kvec[0]; |
| 558 | } |
| 559 | |
| 560 | static void con_out_kvec_add(struct ceph_connection *con, |
| 561 | size_t size, void *data) |
| 562 | { |
| 563 | int index; |
| 564 | |
| 565 | index = con->out_kvec_left; |
| 566 | BUG_ON(index >= ARRAY_SIZE(con->out_kvec)); |
| 567 | |
| 568 | con->out_kvec[index].iov_len = size; |
| 569 | con->out_kvec[index].iov_base = data; |
| 570 | con->out_kvec_left++; |
| 571 | con->out_kvec_bytes += size; |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | * Prepare footer for currently outgoing message, and finish things |
| 576 | * off. Assumes out_kvec* are already valid.. we just add on to the end. |
| 577 | */ |
| 578 | static void prepare_write_message_footer(struct ceph_connection *con) |
| 579 | { |
| 580 | struct ceph_msg *m = con->out_msg; |
| 581 | int v = con->out_kvec_left; |
| 582 | |
| 583 | dout("prepare_write_message_footer %p\n", con); |
| 584 | con->out_kvec_is_msg = true; |
| 585 | con->out_kvec[v].iov_base = &m->footer; |
| 586 | con->out_kvec[v].iov_len = sizeof(m->footer); |
| 587 | con->out_kvec_bytes += sizeof(m->footer); |
| 588 | con->out_kvec_left++; |
| 589 | con->out_more = m->more_to_follow; |
| 590 | con->out_msg_done = true; |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * Prepare headers for the next outgoing message. |
| 595 | */ |
| 596 | static void prepare_write_message(struct ceph_connection *con) |
| 597 | { |
| 598 | struct ceph_msg *m; |
| 599 | u32 crc; |
| 600 | |
| 601 | con_out_kvec_reset(con); |
| 602 | con->out_kvec_is_msg = true; |
| 603 | con->out_msg_done = false; |
| 604 | |
| 605 | /* Sneak an ack in there first? If we can get it into the same |
| 606 | * TCP packet that's a good thing. */ |
| 607 | if (con->in_seq > con->in_seq_acked) { |
| 608 | con->in_seq_acked = con->in_seq; |
| 609 | con_out_kvec_add(con, sizeof (tag_ack), &tag_ack); |
| 610 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); |
| 611 | con_out_kvec_add(con, sizeof (con->out_temp_ack), |
| 612 | &con->out_temp_ack); |
| 613 | } |
| 614 | |
| 615 | m = list_first_entry(&con->out_queue, struct ceph_msg, list_head); |
| 616 | con->out_msg = m; |
| 617 | |
| 618 | /* put message on sent list */ |
| 619 | ceph_msg_get(m); |
| 620 | list_move_tail(&m->list_head, &con->out_sent); |
| 621 | |
| 622 | /* |
| 623 | * only assign outgoing seq # if we haven't sent this message |
| 624 | * yet. if it is requeued, resend with it's original seq. |
| 625 | */ |
| 626 | if (m->needs_out_seq) { |
| 627 | m->hdr.seq = cpu_to_le64(++con->out_seq); |
| 628 | m->needs_out_seq = false; |
| 629 | } |
| 630 | |
| 631 | dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n", |
| 632 | m, con->out_seq, le16_to_cpu(m->hdr.type), |
| 633 | le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len), |
| 634 | le32_to_cpu(m->hdr.data_len), |
| 635 | m->nr_pages); |
| 636 | BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len); |
| 637 | |
| 638 | /* tag + hdr + front + middle */ |
| 639 | con_out_kvec_add(con, sizeof (tag_msg), &tag_msg); |
| 640 | con_out_kvec_add(con, sizeof (m->hdr), &m->hdr); |
| 641 | con_out_kvec_add(con, m->front.iov_len, m->front.iov_base); |
| 642 | |
| 643 | if (m->middle) |
| 644 | con_out_kvec_add(con, m->middle->vec.iov_len, |
| 645 | m->middle->vec.iov_base); |
| 646 | |
| 647 | /* fill in crc (except data pages), footer */ |
| 648 | crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc)); |
| 649 | con->out_msg->hdr.crc = cpu_to_le32(crc); |
| 650 | con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE; |
| 651 | |
| 652 | crc = crc32c(0, m->front.iov_base, m->front.iov_len); |
| 653 | con->out_msg->footer.front_crc = cpu_to_le32(crc); |
| 654 | if (m->middle) { |
| 655 | crc = crc32c(0, m->middle->vec.iov_base, |
| 656 | m->middle->vec.iov_len); |
| 657 | con->out_msg->footer.middle_crc = cpu_to_le32(crc); |
| 658 | } else |
| 659 | con->out_msg->footer.middle_crc = 0; |
| 660 | con->out_msg->footer.data_crc = 0; |
| 661 | dout("prepare_write_message front_crc %u data_crc %u\n", |
| 662 | le32_to_cpu(con->out_msg->footer.front_crc), |
| 663 | le32_to_cpu(con->out_msg->footer.middle_crc)); |
| 664 | |
| 665 | /* is there a data payload? */ |
| 666 | if (le32_to_cpu(m->hdr.data_len) > 0) { |
| 667 | /* initialize page iterator */ |
| 668 | con->out_msg_pos.page = 0; |
| 669 | if (m->pages) |
| 670 | con->out_msg_pos.page_pos = m->page_alignment; |
| 671 | else |
| 672 | con->out_msg_pos.page_pos = 0; |
| 673 | con->out_msg_pos.data_pos = 0; |
| 674 | con->out_msg_pos.did_page_crc = false; |
| 675 | con->out_more = 1; /* data + footer will follow */ |
| 676 | } else { |
| 677 | /* no, queue up footer too and be done */ |
| 678 | prepare_write_message_footer(con); |
| 679 | } |
| 680 | |
| 681 | set_bit(WRITE_PENDING, &con->flags); |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | * Prepare an ack. |
| 686 | */ |
| 687 | static void prepare_write_ack(struct ceph_connection *con) |
| 688 | { |
| 689 | dout("prepare_write_ack %p %llu -> %llu\n", con, |
| 690 | con->in_seq_acked, con->in_seq); |
| 691 | con->in_seq_acked = con->in_seq; |
| 692 | |
| 693 | con_out_kvec_reset(con); |
| 694 | |
| 695 | con_out_kvec_add(con, sizeof (tag_ack), &tag_ack); |
| 696 | |
| 697 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); |
| 698 | con_out_kvec_add(con, sizeof (con->out_temp_ack), |
| 699 | &con->out_temp_ack); |
| 700 | |
| 701 | con->out_more = 1; /* more will follow.. eventually.. */ |
| 702 | set_bit(WRITE_PENDING, &con->flags); |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * Prepare to write keepalive byte. |
| 707 | */ |
| 708 | static void prepare_write_keepalive(struct ceph_connection *con) |
| 709 | { |
| 710 | dout("prepare_write_keepalive %p\n", con); |
| 711 | con_out_kvec_reset(con); |
| 712 | con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive); |
| 713 | set_bit(WRITE_PENDING, &con->flags); |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Connection negotiation. |
| 718 | */ |
| 719 | |
| 720 | static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con, |
| 721 | int *auth_proto) |
| 722 | { |
| 723 | struct ceph_auth_handshake *auth; |
| 724 | |
| 725 | if (!con->ops->get_authorizer) { |
| 726 | con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN; |
| 727 | con->out_connect.authorizer_len = 0; |
| 728 | |
| 729 | return NULL; |
| 730 | } |
| 731 | |
| 732 | /* Can't hold the mutex while getting authorizer */ |
| 733 | |
| 734 | mutex_unlock(&con->mutex); |
| 735 | |
| 736 | auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry); |
| 737 | |
| 738 | mutex_lock(&con->mutex); |
| 739 | |
| 740 | if (IS_ERR(auth)) |
| 741 | return auth; |
| 742 | if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags)) |
| 743 | return ERR_PTR(-EAGAIN); |
| 744 | |
| 745 | con->auth_reply_buf = auth->authorizer_reply_buf; |
| 746 | con->auth_reply_buf_len = auth->authorizer_reply_buf_len; |
| 747 | |
| 748 | |
| 749 | return auth; |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | * We connected to a peer and are saying hello. |
| 754 | */ |
| 755 | static void prepare_write_banner(struct ceph_connection *con) |
| 756 | { |
| 757 | con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER); |
| 758 | con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr), |
| 759 | &con->msgr->my_enc_addr); |
| 760 | |
| 761 | con->out_more = 0; |
| 762 | set_bit(WRITE_PENDING, &con->flags); |
| 763 | } |
| 764 | |
| 765 | static int prepare_write_connect(struct ceph_connection *con) |
| 766 | { |
| 767 | unsigned global_seq = get_global_seq(con->msgr, 0); |
| 768 | int proto; |
| 769 | int auth_proto; |
| 770 | struct ceph_auth_handshake *auth; |
| 771 | |
| 772 | switch (con->peer_name.type) { |
| 773 | case CEPH_ENTITY_TYPE_MON: |
| 774 | proto = CEPH_MONC_PROTOCOL; |
| 775 | break; |
| 776 | case CEPH_ENTITY_TYPE_OSD: |
| 777 | proto = CEPH_OSDC_PROTOCOL; |
| 778 | break; |
| 779 | case CEPH_ENTITY_TYPE_MDS: |
| 780 | proto = CEPH_MDSC_PROTOCOL; |
| 781 | break; |
| 782 | default: |
| 783 | BUG(); |
| 784 | } |
| 785 | |
| 786 | dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con, |
| 787 | con->connect_seq, global_seq, proto); |
| 788 | |
| 789 | con->out_connect.features = cpu_to_le64(con->msgr->supported_features); |
| 790 | con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT); |
| 791 | con->out_connect.connect_seq = cpu_to_le32(con->connect_seq); |
| 792 | con->out_connect.global_seq = cpu_to_le32(global_seq); |
| 793 | con->out_connect.protocol_version = cpu_to_le32(proto); |
| 794 | con->out_connect.flags = 0; |
| 795 | |
| 796 | auth_proto = CEPH_AUTH_UNKNOWN; |
| 797 | auth = get_connect_authorizer(con, &auth_proto); |
| 798 | if (IS_ERR(auth)) |
| 799 | return PTR_ERR(auth); |
| 800 | |
| 801 | con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto); |
| 802 | con->out_connect.authorizer_len = auth ? |
| 803 | cpu_to_le32(auth->authorizer_buf_len) : 0; |
| 804 | |
| 805 | con_out_kvec_add(con, sizeof (con->out_connect), |
| 806 | &con->out_connect); |
| 807 | if (auth && auth->authorizer_buf_len) |
| 808 | con_out_kvec_add(con, auth->authorizer_buf_len, |
| 809 | auth->authorizer_buf); |
| 810 | |
| 811 | con->out_more = 0; |
| 812 | set_bit(WRITE_PENDING, &con->flags); |
| 813 | |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | /* |
| 818 | * write as much of pending kvecs to the socket as we can. |
| 819 | * 1 -> done |
| 820 | * 0 -> socket full, but more to do |
| 821 | * <0 -> error |
| 822 | */ |
| 823 | static int write_partial_kvec(struct ceph_connection *con) |
| 824 | { |
| 825 | int ret; |
| 826 | |
| 827 | dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes); |
| 828 | while (con->out_kvec_bytes > 0) { |
| 829 | ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur, |
| 830 | con->out_kvec_left, con->out_kvec_bytes, |
| 831 | con->out_more); |
| 832 | if (ret <= 0) |
| 833 | goto out; |
| 834 | con->out_kvec_bytes -= ret; |
| 835 | if (con->out_kvec_bytes == 0) |
| 836 | break; /* done */ |
| 837 | |
| 838 | /* account for full iov entries consumed */ |
| 839 | while (ret >= con->out_kvec_cur->iov_len) { |
| 840 | BUG_ON(!con->out_kvec_left); |
| 841 | ret -= con->out_kvec_cur->iov_len; |
| 842 | con->out_kvec_cur++; |
| 843 | con->out_kvec_left--; |
| 844 | } |
| 845 | /* and for a partially-consumed entry */ |
| 846 | if (ret) { |
| 847 | con->out_kvec_cur->iov_len -= ret; |
| 848 | con->out_kvec_cur->iov_base += ret; |
| 849 | } |
| 850 | } |
| 851 | con->out_kvec_left = 0; |
| 852 | con->out_kvec_is_msg = false; |
| 853 | ret = 1; |
| 854 | out: |
| 855 | dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con, |
| 856 | con->out_kvec_bytes, con->out_kvec_left, ret); |
| 857 | return ret; /* done! */ |
| 858 | } |
| 859 | |
| 860 | #ifdef CONFIG_BLOCK |
| 861 | static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg) |
| 862 | { |
| 863 | if (!bio) { |
| 864 | *iter = NULL; |
| 865 | *seg = 0; |
| 866 | return; |
| 867 | } |
| 868 | *iter = bio; |
| 869 | *seg = bio->bi_idx; |
| 870 | } |
| 871 | |
| 872 | static void iter_bio_next(struct bio **bio_iter, int *seg) |
| 873 | { |
| 874 | if (*bio_iter == NULL) |
| 875 | return; |
| 876 | |
| 877 | BUG_ON(*seg >= (*bio_iter)->bi_vcnt); |
| 878 | |
| 879 | (*seg)++; |
| 880 | if (*seg == (*bio_iter)->bi_vcnt) |
| 881 | init_bio_iter((*bio_iter)->bi_next, bio_iter, seg); |
| 882 | } |
| 883 | #endif |
| 884 | |
| 885 | /* |
| 886 | * Write as much message data payload as we can. If we finish, queue |
| 887 | * up the footer. |
| 888 | * 1 -> done, footer is now queued in out_kvec[]. |
| 889 | * 0 -> socket full, but more to do |
| 890 | * <0 -> error |
| 891 | */ |
| 892 | static int write_partial_msg_pages(struct ceph_connection *con) |
| 893 | { |
| 894 | struct ceph_msg *msg = con->out_msg; |
| 895 | unsigned data_len = le32_to_cpu(msg->hdr.data_len); |
| 896 | size_t len; |
| 897 | bool do_datacrc = !con->msgr->nocrc; |
| 898 | int ret; |
| 899 | int total_max_write; |
| 900 | int in_trail = 0; |
| 901 | size_t trail_len = (msg->trail ? msg->trail->length : 0); |
| 902 | |
| 903 | dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n", |
| 904 | con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages, |
| 905 | con->out_msg_pos.page_pos); |
| 906 | |
| 907 | #ifdef CONFIG_BLOCK |
| 908 | if (msg->bio && !msg->bio_iter) |
| 909 | init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg); |
| 910 | #endif |
| 911 | |
| 912 | while (data_len > con->out_msg_pos.data_pos) { |
| 913 | struct page *page = NULL; |
| 914 | int max_write = PAGE_SIZE; |
| 915 | int bio_offset = 0; |
| 916 | |
| 917 | total_max_write = data_len - trail_len - |
| 918 | con->out_msg_pos.data_pos; |
| 919 | |
| 920 | /* |
| 921 | * if we are calculating the data crc (the default), we need |
| 922 | * to map the page. if our pages[] has been revoked, use the |
| 923 | * zero page. |
| 924 | */ |
| 925 | |
| 926 | /* have we reached the trail part of the data? */ |
| 927 | if (con->out_msg_pos.data_pos >= data_len - trail_len) { |
| 928 | in_trail = 1; |
| 929 | |
| 930 | total_max_write = data_len - con->out_msg_pos.data_pos; |
| 931 | |
| 932 | page = list_first_entry(&msg->trail->head, |
| 933 | struct page, lru); |
| 934 | max_write = PAGE_SIZE; |
| 935 | } else if (msg->pages) { |
| 936 | page = msg->pages[con->out_msg_pos.page]; |
| 937 | } else if (msg->pagelist) { |
| 938 | page = list_first_entry(&msg->pagelist->head, |
| 939 | struct page, lru); |
| 940 | #ifdef CONFIG_BLOCK |
| 941 | } else if (msg->bio) { |
| 942 | struct bio_vec *bv; |
| 943 | |
| 944 | bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg); |
| 945 | page = bv->bv_page; |
| 946 | bio_offset = bv->bv_offset; |
| 947 | max_write = bv->bv_len; |
| 948 | #endif |
| 949 | } else { |
| 950 | page = zero_page; |
| 951 | } |
| 952 | len = min_t(int, max_write - con->out_msg_pos.page_pos, |
| 953 | total_max_write); |
| 954 | |
| 955 | if (do_datacrc && !con->out_msg_pos.did_page_crc) { |
| 956 | void *base; |
| 957 | u32 crc; |
| 958 | u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc); |
| 959 | char *kaddr; |
| 960 | |
| 961 | kaddr = kmap(page); |
| 962 | BUG_ON(kaddr == NULL); |
| 963 | base = kaddr + con->out_msg_pos.page_pos + bio_offset; |
| 964 | crc = crc32c(tmpcrc, base, len); |
| 965 | con->out_msg->footer.data_crc = cpu_to_le32(crc); |
| 966 | con->out_msg_pos.did_page_crc = true; |
| 967 | } |
| 968 | ret = ceph_tcp_sendpage(con->sock, page, |
| 969 | con->out_msg_pos.page_pos + bio_offset, |
| 970 | len, 1); |
| 971 | |
| 972 | if (do_datacrc) |
| 973 | kunmap(page); |
| 974 | |
| 975 | if (ret <= 0) |
| 976 | goto out; |
| 977 | |
| 978 | con->out_msg_pos.data_pos += ret; |
| 979 | con->out_msg_pos.page_pos += ret; |
| 980 | if (ret == len) { |
| 981 | con->out_msg_pos.page_pos = 0; |
| 982 | con->out_msg_pos.page++; |
| 983 | con->out_msg_pos.did_page_crc = false; |
| 984 | if (in_trail) |
| 985 | list_move_tail(&page->lru, |
| 986 | &msg->trail->head); |
| 987 | else if (msg->pagelist) |
| 988 | list_move_tail(&page->lru, |
| 989 | &msg->pagelist->head); |
| 990 | #ifdef CONFIG_BLOCK |
| 991 | else if (msg->bio) |
| 992 | iter_bio_next(&msg->bio_iter, &msg->bio_seg); |
| 993 | #endif |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | dout("write_partial_msg_pages %p msg %p done\n", con, msg); |
| 998 | |
| 999 | /* prepare and queue up footer, too */ |
| 1000 | if (!do_datacrc) |
| 1001 | con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC; |
| 1002 | con_out_kvec_reset(con); |
| 1003 | prepare_write_message_footer(con); |
| 1004 | ret = 1; |
| 1005 | out: |
| 1006 | return ret; |
| 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | * write some zeros |
| 1011 | */ |
| 1012 | static int write_partial_skip(struct ceph_connection *con) |
| 1013 | { |
| 1014 | int ret; |
| 1015 | |
| 1016 | while (con->out_skip > 0) { |
| 1017 | size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE); |
| 1018 | |
| 1019 | ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1); |
| 1020 | if (ret <= 0) |
| 1021 | goto out; |
| 1022 | con->out_skip -= ret; |
| 1023 | } |
| 1024 | ret = 1; |
| 1025 | out: |
| 1026 | return ret; |
| 1027 | } |
| 1028 | |
| 1029 | /* |
| 1030 | * Prepare to read connection handshake, or an ack. |
| 1031 | */ |
| 1032 | static void prepare_read_banner(struct ceph_connection *con) |
| 1033 | { |
| 1034 | dout("prepare_read_banner %p\n", con); |
| 1035 | con->in_base_pos = 0; |
| 1036 | } |
| 1037 | |
| 1038 | static void prepare_read_connect(struct ceph_connection *con) |
| 1039 | { |
| 1040 | dout("prepare_read_connect %p\n", con); |
| 1041 | con->in_base_pos = 0; |
| 1042 | } |
| 1043 | |
| 1044 | static void prepare_read_ack(struct ceph_connection *con) |
| 1045 | { |
| 1046 | dout("prepare_read_ack %p\n", con); |
| 1047 | con->in_base_pos = 0; |
| 1048 | } |
| 1049 | |
| 1050 | static void prepare_read_tag(struct ceph_connection *con) |
| 1051 | { |
| 1052 | dout("prepare_read_tag %p\n", con); |
| 1053 | con->in_base_pos = 0; |
| 1054 | con->in_tag = CEPH_MSGR_TAG_READY; |
| 1055 | } |
| 1056 | |
| 1057 | /* |
| 1058 | * Prepare to read a message. |
| 1059 | */ |
| 1060 | static int prepare_read_message(struct ceph_connection *con) |
| 1061 | { |
| 1062 | dout("prepare_read_message %p\n", con); |
| 1063 | BUG_ON(con->in_msg != NULL); |
| 1064 | con->in_base_pos = 0; |
| 1065 | con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0; |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
| 1069 | |
| 1070 | static int read_partial(struct ceph_connection *con, |
| 1071 | int end, int size, void *object) |
| 1072 | { |
| 1073 | while (con->in_base_pos < end) { |
| 1074 | int left = end - con->in_base_pos; |
| 1075 | int have = size - left; |
| 1076 | int ret = ceph_tcp_recvmsg(con->sock, object + have, left); |
| 1077 | if (ret <= 0) |
| 1078 | return ret; |
| 1079 | con->in_base_pos += ret; |
| 1080 | } |
| 1081 | return 1; |
| 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | /* |
| 1086 | * Read all or part of the connect-side handshake on a new connection |
| 1087 | */ |
| 1088 | static int read_partial_banner(struct ceph_connection *con) |
| 1089 | { |
| 1090 | int size; |
| 1091 | int end; |
| 1092 | int ret; |
| 1093 | |
| 1094 | dout("read_partial_banner %p at %d\n", con, con->in_base_pos); |
| 1095 | |
| 1096 | /* peer's banner */ |
| 1097 | size = strlen(CEPH_BANNER); |
| 1098 | end = size; |
| 1099 | ret = read_partial(con, end, size, con->in_banner); |
| 1100 | if (ret <= 0) |
| 1101 | goto out; |
| 1102 | |
| 1103 | size = sizeof (con->actual_peer_addr); |
| 1104 | end += size; |
| 1105 | ret = read_partial(con, end, size, &con->actual_peer_addr); |
| 1106 | if (ret <= 0) |
| 1107 | goto out; |
| 1108 | |
| 1109 | size = sizeof (con->peer_addr_for_me); |
| 1110 | end += size; |
| 1111 | ret = read_partial(con, end, size, &con->peer_addr_for_me); |
| 1112 | if (ret <= 0) |
| 1113 | goto out; |
| 1114 | |
| 1115 | out: |
| 1116 | return ret; |
| 1117 | } |
| 1118 | |
| 1119 | static int read_partial_connect(struct ceph_connection *con) |
| 1120 | { |
| 1121 | int size; |
| 1122 | int end; |
| 1123 | int ret; |
| 1124 | |
| 1125 | dout("read_partial_connect %p at %d\n", con, con->in_base_pos); |
| 1126 | |
| 1127 | size = sizeof (con->in_reply); |
| 1128 | end = size; |
| 1129 | ret = read_partial(con, end, size, &con->in_reply); |
| 1130 | if (ret <= 0) |
| 1131 | goto out; |
| 1132 | |
| 1133 | size = le32_to_cpu(con->in_reply.authorizer_len); |
| 1134 | end += size; |
| 1135 | ret = read_partial(con, end, size, con->auth_reply_buf); |
| 1136 | if (ret <= 0) |
| 1137 | goto out; |
| 1138 | |
| 1139 | dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n", |
| 1140 | con, (int)con->in_reply.tag, |
| 1141 | le32_to_cpu(con->in_reply.connect_seq), |
| 1142 | le32_to_cpu(con->in_reply.global_seq)); |
| 1143 | out: |
| 1144 | return ret; |
| 1145 | |
| 1146 | } |
| 1147 | |
| 1148 | /* |
| 1149 | * Verify the hello banner looks okay. |
| 1150 | */ |
| 1151 | static int verify_hello(struct ceph_connection *con) |
| 1152 | { |
| 1153 | if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) { |
| 1154 | pr_err("connect to %s got bad banner\n", |
| 1155 | ceph_pr_addr(&con->peer_addr.in_addr)); |
| 1156 | con->error_msg = "protocol error, bad banner"; |
| 1157 | return -1; |
| 1158 | } |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | static bool addr_is_blank(struct sockaddr_storage *ss) |
| 1163 | { |
| 1164 | switch (ss->ss_family) { |
| 1165 | case AF_INET: |
| 1166 | return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0; |
| 1167 | case AF_INET6: |
| 1168 | return |
| 1169 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 && |
| 1170 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 && |
| 1171 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 && |
| 1172 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0; |
| 1173 | } |
| 1174 | return false; |
| 1175 | } |
| 1176 | |
| 1177 | static int addr_port(struct sockaddr_storage *ss) |
| 1178 | { |
| 1179 | switch (ss->ss_family) { |
| 1180 | case AF_INET: |
| 1181 | return ntohs(((struct sockaddr_in *)ss)->sin_port); |
| 1182 | case AF_INET6: |
| 1183 | return ntohs(((struct sockaddr_in6 *)ss)->sin6_port); |
| 1184 | } |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | static void addr_set_port(struct sockaddr_storage *ss, int p) |
| 1189 | { |
| 1190 | switch (ss->ss_family) { |
| 1191 | case AF_INET: |
| 1192 | ((struct sockaddr_in *)ss)->sin_port = htons(p); |
| 1193 | break; |
| 1194 | case AF_INET6: |
| 1195 | ((struct sockaddr_in6 *)ss)->sin6_port = htons(p); |
| 1196 | break; |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | /* |
| 1201 | * Unlike other *_pton function semantics, zero indicates success. |
| 1202 | */ |
| 1203 | static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss, |
| 1204 | char delim, const char **ipend) |
| 1205 | { |
| 1206 | struct sockaddr_in *in4 = (struct sockaddr_in *) ss; |
| 1207 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss; |
| 1208 | |
| 1209 | memset(ss, 0, sizeof(*ss)); |
| 1210 | |
| 1211 | if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) { |
| 1212 | ss->ss_family = AF_INET; |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) { |
| 1217 | ss->ss_family = AF_INET6; |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
| 1221 | return -EINVAL; |
| 1222 | } |
| 1223 | |
| 1224 | /* |
| 1225 | * Extract hostname string and resolve using kernel DNS facility. |
| 1226 | */ |
| 1227 | #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER |
| 1228 | static int ceph_dns_resolve_name(const char *name, size_t namelen, |
| 1229 | struct sockaddr_storage *ss, char delim, const char **ipend) |
| 1230 | { |
| 1231 | const char *end, *delim_p; |
| 1232 | char *colon_p, *ip_addr = NULL; |
| 1233 | int ip_len, ret; |
| 1234 | |
| 1235 | /* |
| 1236 | * The end of the hostname occurs immediately preceding the delimiter or |
| 1237 | * the port marker (':') where the delimiter takes precedence. |
| 1238 | */ |
| 1239 | delim_p = memchr(name, delim, namelen); |
| 1240 | colon_p = memchr(name, ':', namelen); |
| 1241 | |
| 1242 | if (delim_p && colon_p) |
| 1243 | end = delim_p < colon_p ? delim_p : colon_p; |
| 1244 | else if (!delim_p && colon_p) |
| 1245 | end = colon_p; |
| 1246 | else { |
| 1247 | end = delim_p; |
| 1248 | if (!end) /* case: hostname:/ */ |
| 1249 | end = name + namelen; |
| 1250 | } |
| 1251 | |
| 1252 | if (end <= name) |
| 1253 | return -EINVAL; |
| 1254 | |
| 1255 | /* do dns_resolve upcall */ |
| 1256 | ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL); |
| 1257 | if (ip_len > 0) |
| 1258 | ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL); |
| 1259 | else |
| 1260 | ret = -ESRCH; |
| 1261 | |
| 1262 | kfree(ip_addr); |
| 1263 | |
| 1264 | *ipend = end; |
| 1265 | |
| 1266 | pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name, |
| 1267 | ret, ret ? "failed" : ceph_pr_addr(ss)); |
| 1268 | |
| 1269 | return ret; |
| 1270 | } |
| 1271 | #else |
| 1272 | static inline int ceph_dns_resolve_name(const char *name, size_t namelen, |
| 1273 | struct sockaddr_storage *ss, char delim, const char **ipend) |
| 1274 | { |
| 1275 | return -EINVAL; |
| 1276 | } |
| 1277 | #endif |
| 1278 | |
| 1279 | /* |
| 1280 | * Parse a server name (IP or hostname). If a valid IP address is not found |
| 1281 | * then try to extract a hostname to resolve using userspace DNS upcall. |
| 1282 | */ |
| 1283 | static int ceph_parse_server_name(const char *name, size_t namelen, |
| 1284 | struct sockaddr_storage *ss, char delim, const char **ipend) |
| 1285 | { |
| 1286 | int ret; |
| 1287 | |
| 1288 | ret = ceph_pton(name, namelen, ss, delim, ipend); |
| 1289 | if (ret) |
| 1290 | ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend); |
| 1291 | |
| 1292 | return ret; |
| 1293 | } |
| 1294 | |
| 1295 | /* |
| 1296 | * Parse an ip[:port] list into an addr array. Use the default |
| 1297 | * monitor port if a port isn't specified. |
| 1298 | */ |
| 1299 | int ceph_parse_ips(const char *c, const char *end, |
| 1300 | struct ceph_entity_addr *addr, |
| 1301 | int max_count, int *count) |
| 1302 | { |
| 1303 | int i, ret = -EINVAL; |
| 1304 | const char *p = c; |
| 1305 | |
| 1306 | dout("parse_ips on '%.*s'\n", (int)(end-c), c); |
| 1307 | for (i = 0; i < max_count; i++) { |
| 1308 | const char *ipend; |
| 1309 | struct sockaddr_storage *ss = &addr[i].in_addr; |
| 1310 | int port; |
| 1311 | char delim = ','; |
| 1312 | |
| 1313 | if (*p == '[') { |
| 1314 | delim = ']'; |
| 1315 | p++; |
| 1316 | } |
| 1317 | |
| 1318 | ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend); |
| 1319 | if (ret) |
| 1320 | goto bad; |
| 1321 | ret = -EINVAL; |
| 1322 | |
| 1323 | p = ipend; |
| 1324 | |
| 1325 | if (delim == ']') { |
| 1326 | if (*p != ']') { |
| 1327 | dout("missing matching ']'\n"); |
| 1328 | goto bad; |
| 1329 | } |
| 1330 | p++; |
| 1331 | } |
| 1332 | |
| 1333 | /* port? */ |
| 1334 | if (p < end && *p == ':') { |
| 1335 | port = 0; |
| 1336 | p++; |
| 1337 | while (p < end && *p >= '0' && *p <= '9') { |
| 1338 | port = (port * 10) + (*p - '0'); |
| 1339 | p++; |
| 1340 | } |
| 1341 | if (port > 65535 || port == 0) |
| 1342 | goto bad; |
| 1343 | } else { |
| 1344 | port = CEPH_MON_PORT; |
| 1345 | } |
| 1346 | |
| 1347 | addr_set_port(ss, port); |
| 1348 | |
| 1349 | dout("parse_ips got %s\n", ceph_pr_addr(ss)); |
| 1350 | |
| 1351 | if (p == end) |
| 1352 | break; |
| 1353 | if (*p != ',') |
| 1354 | goto bad; |
| 1355 | p++; |
| 1356 | } |
| 1357 | |
| 1358 | if (p != end) |
| 1359 | goto bad; |
| 1360 | |
| 1361 | if (count) |
| 1362 | *count = i + 1; |
| 1363 | return 0; |
| 1364 | |
| 1365 | bad: |
| 1366 | pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c); |
| 1367 | return ret; |
| 1368 | } |
| 1369 | EXPORT_SYMBOL(ceph_parse_ips); |
| 1370 | |
| 1371 | static int process_banner(struct ceph_connection *con) |
| 1372 | { |
| 1373 | dout("process_banner on %p\n", con); |
| 1374 | |
| 1375 | if (verify_hello(con) < 0) |
| 1376 | return -1; |
| 1377 | |
| 1378 | ceph_decode_addr(&con->actual_peer_addr); |
| 1379 | ceph_decode_addr(&con->peer_addr_for_me); |
| 1380 | |
| 1381 | /* |
| 1382 | * Make sure the other end is who we wanted. note that the other |
| 1383 | * end may not yet know their ip address, so if it's 0.0.0.0, give |
| 1384 | * them the benefit of the doubt. |
| 1385 | */ |
| 1386 | if (memcmp(&con->peer_addr, &con->actual_peer_addr, |
| 1387 | sizeof(con->peer_addr)) != 0 && |
| 1388 | !(addr_is_blank(&con->actual_peer_addr.in_addr) && |
| 1389 | con->actual_peer_addr.nonce == con->peer_addr.nonce)) { |
| 1390 | pr_warning("wrong peer, want %s/%d, got %s/%d\n", |
| 1391 | ceph_pr_addr(&con->peer_addr.in_addr), |
| 1392 | (int)le32_to_cpu(con->peer_addr.nonce), |
| 1393 | ceph_pr_addr(&con->actual_peer_addr.in_addr), |
| 1394 | (int)le32_to_cpu(con->actual_peer_addr.nonce)); |
| 1395 | con->error_msg = "wrong peer at address"; |
| 1396 | return -1; |
| 1397 | } |
| 1398 | |
| 1399 | /* |
| 1400 | * did we learn our address? |
| 1401 | */ |
| 1402 | if (addr_is_blank(&con->msgr->inst.addr.in_addr)) { |
| 1403 | int port = addr_port(&con->msgr->inst.addr.in_addr); |
| 1404 | |
| 1405 | memcpy(&con->msgr->inst.addr.in_addr, |
| 1406 | &con->peer_addr_for_me.in_addr, |
| 1407 | sizeof(con->peer_addr_for_me.in_addr)); |
| 1408 | addr_set_port(&con->msgr->inst.addr.in_addr, port); |
| 1409 | encode_my_addr(con->msgr); |
| 1410 | dout("process_banner learned my addr is %s\n", |
| 1411 | ceph_pr_addr(&con->msgr->inst.addr.in_addr)); |
| 1412 | } |
| 1413 | |
| 1414 | set_bit(NEGOTIATING, &con->state); |
| 1415 | prepare_read_connect(con); |
| 1416 | return 0; |
| 1417 | } |
| 1418 | |
| 1419 | static void fail_protocol(struct ceph_connection *con) |
| 1420 | { |
| 1421 | reset_connection(con); |
| 1422 | set_bit(CLOSED, &con->state); /* in case there's queued work */ |
| 1423 | } |
| 1424 | |
| 1425 | static int process_connect(struct ceph_connection *con) |
| 1426 | { |
| 1427 | u64 sup_feat = con->msgr->supported_features; |
| 1428 | u64 req_feat = con->msgr->required_features; |
| 1429 | u64 server_feat = le64_to_cpu(con->in_reply.features); |
| 1430 | int ret; |
| 1431 | |
| 1432 | dout("process_connect on %p tag %d\n", con, (int)con->in_tag); |
| 1433 | |
| 1434 | switch (con->in_reply.tag) { |
| 1435 | case CEPH_MSGR_TAG_FEATURES: |
| 1436 | pr_err("%s%lld %s feature set mismatch," |
| 1437 | " my %llx < server's %llx, missing %llx\n", |
| 1438 | ENTITY_NAME(con->peer_name), |
| 1439 | ceph_pr_addr(&con->peer_addr.in_addr), |
| 1440 | sup_feat, server_feat, server_feat & ~sup_feat); |
| 1441 | con->error_msg = "missing required protocol features"; |
| 1442 | fail_protocol(con); |
| 1443 | return -1; |
| 1444 | |
| 1445 | case CEPH_MSGR_TAG_BADPROTOVER: |
| 1446 | pr_err("%s%lld %s protocol version mismatch," |
| 1447 | " my %d != server's %d\n", |
| 1448 | ENTITY_NAME(con->peer_name), |
| 1449 | ceph_pr_addr(&con->peer_addr.in_addr), |
| 1450 | le32_to_cpu(con->out_connect.protocol_version), |
| 1451 | le32_to_cpu(con->in_reply.protocol_version)); |
| 1452 | con->error_msg = "protocol version mismatch"; |
| 1453 | fail_protocol(con); |
| 1454 | return -1; |
| 1455 | |
| 1456 | case CEPH_MSGR_TAG_BADAUTHORIZER: |
| 1457 | con->auth_retry++; |
| 1458 | dout("process_connect %p got BADAUTHORIZER attempt %d\n", con, |
| 1459 | con->auth_retry); |
| 1460 | if (con->auth_retry == 2) { |
| 1461 | con->error_msg = "connect authorization failure"; |
| 1462 | return -1; |
| 1463 | } |
| 1464 | con->auth_retry = 1; |
| 1465 | con_out_kvec_reset(con); |
| 1466 | ret = prepare_write_connect(con); |
| 1467 | if (ret < 0) |
| 1468 | return ret; |
| 1469 | prepare_read_connect(con); |
| 1470 | break; |
| 1471 | |
| 1472 | case CEPH_MSGR_TAG_RESETSESSION: |
| 1473 | /* |
| 1474 | * If we connected with a large connect_seq but the peer |
| 1475 | * has no record of a session with us (no connection, or |
| 1476 | * connect_seq == 0), they will send RESETSESION to indicate |
| 1477 | * that they must have reset their session, and may have |
| 1478 | * dropped messages. |
| 1479 | */ |
| 1480 | dout("process_connect got RESET peer seq %u\n", |
| 1481 | le32_to_cpu(con->in_connect.connect_seq)); |
| 1482 | pr_err("%s%lld %s connection reset\n", |
| 1483 | ENTITY_NAME(con->peer_name), |
| 1484 | ceph_pr_addr(&con->peer_addr.in_addr)); |
| 1485 | reset_connection(con); |
| 1486 | con_out_kvec_reset(con); |
| 1487 | ret = prepare_write_connect(con); |
| 1488 | if (ret < 0) |
| 1489 | return ret; |
| 1490 | prepare_read_connect(con); |
| 1491 | |
| 1492 | /* Tell ceph about it. */ |
| 1493 | mutex_unlock(&con->mutex); |
| 1494 | pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name)); |
| 1495 | if (con->ops->peer_reset) |
| 1496 | con->ops->peer_reset(con); |
| 1497 | mutex_lock(&con->mutex); |
| 1498 | if (test_bit(CLOSED, &con->state) || |
| 1499 | test_bit(OPENING, &con->state)) |
| 1500 | return -EAGAIN; |
| 1501 | break; |
| 1502 | |
| 1503 | case CEPH_MSGR_TAG_RETRY_SESSION: |
| 1504 | /* |
| 1505 | * If we sent a smaller connect_seq than the peer has, try |
| 1506 | * again with a larger value. |
| 1507 | */ |
| 1508 | dout("process_connect got RETRY my seq = %u, peer_seq = %u\n", |
| 1509 | le32_to_cpu(con->out_connect.connect_seq), |
| 1510 | le32_to_cpu(con->in_connect.connect_seq)); |
| 1511 | con->connect_seq = le32_to_cpu(con->in_connect.connect_seq); |
| 1512 | con_out_kvec_reset(con); |
| 1513 | ret = prepare_write_connect(con); |
| 1514 | if (ret < 0) |
| 1515 | return ret; |
| 1516 | prepare_read_connect(con); |
| 1517 | break; |
| 1518 | |
| 1519 | case CEPH_MSGR_TAG_RETRY_GLOBAL: |
| 1520 | /* |
| 1521 | * If we sent a smaller global_seq than the peer has, try |
| 1522 | * again with a larger value. |
| 1523 | */ |
| 1524 | dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n", |
| 1525 | con->peer_global_seq, |
| 1526 | le32_to_cpu(con->in_connect.global_seq)); |
| 1527 | get_global_seq(con->msgr, |
| 1528 | le32_to_cpu(con->in_connect.global_seq)); |
| 1529 | con_out_kvec_reset(con); |
| 1530 | ret = prepare_write_connect(con); |
| 1531 | if (ret < 0) |
| 1532 | return ret; |
| 1533 | prepare_read_connect(con); |
| 1534 | break; |
| 1535 | |
| 1536 | case CEPH_MSGR_TAG_READY: |
| 1537 | if (req_feat & ~server_feat) { |
| 1538 | pr_err("%s%lld %s protocol feature mismatch," |
| 1539 | " my required %llx > server's %llx, need %llx\n", |
| 1540 | ENTITY_NAME(con->peer_name), |
| 1541 | ceph_pr_addr(&con->peer_addr.in_addr), |
| 1542 | req_feat, server_feat, req_feat & ~server_feat); |
| 1543 | con->error_msg = "missing required protocol features"; |
| 1544 | fail_protocol(con); |
| 1545 | return -1; |
| 1546 | } |
| 1547 | clear_bit(CONNECTING, &con->state); |
| 1548 | con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq); |
| 1549 | con->connect_seq++; |
| 1550 | con->peer_features = server_feat; |
| 1551 | dout("process_connect got READY gseq %d cseq %d (%d)\n", |
| 1552 | con->peer_global_seq, |
| 1553 | le32_to_cpu(con->in_reply.connect_seq), |
| 1554 | con->connect_seq); |
| 1555 | WARN_ON(con->connect_seq != |
| 1556 | le32_to_cpu(con->in_reply.connect_seq)); |
| 1557 | |
| 1558 | if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY) |
| 1559 | set_bit(LOSSYTX, &con->flags); |
| 1560 | |
| 1561 | prepare_read_tag(con); |
| 1562 | break; |
| 1563 | |
| 1564 | case CEPH_MSGR_TAG_WAIT: |
| 1565 | /* |
| 1566 | * If there is a connection race (we are opening |
| 1567 | * connections to each other), one of us may just have |
| 1568 | * to WAIT. This shouldn't happen if we are the |
| 1569 | * client. |
| 1570 | */ |
| 1571 | pr_err("process_connect got WAIT as client\n"); |
| 1572 | con->error_msg = "protocol error, got WAIT as client"; |
| 1573 | return -1; |
| 1574 | |
| 1575 | default: |
| 1576 | pr_err("connect protocol error, will retry\n"); |
| 1577 | con->error_msg = "protocol error, garbage tag during connect"; |
| 1578 | return -1; |
| 1579 | } |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
| 1583 | |
| 1584 | /* |
| 1585 | * read (part of) an ack |
| 1586 | */ |
| 1587 | static int read_partial_ack(struct ceph_connection *con) |
| 1588 | { |
| 1589 | int size = sizeof (con->in_temp_ack); |
| 1590 | int end = size; |
| 1591 | |
| 1592 | return read_partial(con, end, size, &con->in_temp_ack); |
| 1593 | } |
| 1594 | |
| 1595 | |
| 1596 | /* |
| 1597 | * We can finally discard anything that's been acked. |
| 1598 | */ |
| 1599 | static void process_ack(struct ceph_connection *con) |
| 1600 | { |
| 1601 | struct ceph_msg *m; |
| 1602 | u64 ack = le64_to_cpu(con->in_temp_ack); |
| 1603 | u64 seq; |
| 1604 | |
| 1605 | while (!list_empty(&con->out_sent)) { |
| 1606 | m = list_first_entry(&con->out_sent, struct ceph_msg, |
| 1607 | list_head); |
| 1608 | seq = le64_to_cpu(m->hdr.seq); |
| 1609 | if (seq > ack) |
| 1610 | break; |
| 1611 | dout("got ack for seq %llu type %d at %p\n", seq, |
| 1612 | le16_to_cpu(m->hdr.type), m); |
| 1613 | m->ack_stamp = jiffies; |
| 1614 | ceph_msg_remove(m); |
| 1615 | } |
| 1616 | prepare_read_tag(con); |
| 1617 | } |
| 1618 | |
| 1619 | |
| 1620 | |
| 1621 | |
| 1622 | static int read_partial_message_section(struct ceph_connection *con, |
| 1623 | struct kvec *section, |
| 1624 | unsigned int sec_len, u32 *crc) |
| 1625 | { |
| 1626 | int ret, left; |
| 1627 | |
| 1628 | BUG_ON(!section); |
| 1629 | |
| 1630 | while (section->iov_len < sec_len) { |
| 1631 | BUG_ON(section->iov_base == NULL); |
| 1632 | left = sec_len - section->iov_len; |
| 1633 | ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base + |
| 1634 | section->iov_len, left); |
| 1635 | if (ret <= 0) |
| 1636 | return ret; |
| 1637 | section->iov_len += ret; |
| 1638 | } |
| 1639 | if (section->iov_len == sec_len) |
| 1640 | *crc = crc32c(0, section->iov_base, section->iov_len); |
| 1641 | |
| 1642 | return 1; |
| 1643 | } |
| 1644 | |
| 1645 | static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con, |
| 1646 | struct ceph_msg_header *hdr, |
| 1647 | int *skip); |
| 1648 | |
| 1649 | |
| 1650 | static int read_partial_message_pages(struct ceph_connection *con, |
| 1651 | struct page **pages, |
| 1652 | unsigned data_len, bool do_datacrc) |
| 1653 | { |
| 1654 | void *p; |
| 1655 | int ret; |
| 1656 | int left; |
| 1657 | |
| 1658 | left = min((int)(data_len - con->in_msg_pos.data_pos), |
| 1659 | (int)(PAGE_SIZE - con->in_msg_pos.page_pos)); |
| 1660 | /* (page) data */ |
| 1661 | BUG_ON(pages == NULL); |
| 1662 | p = kmap(pages[con->in_msg_pos.page]); |
| 1663 | ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos, |
| 1664 | left); |
| 1665 | if (ret > 0 && do_datacrc) |
| 1666 | con->in_data_crc = |
| 1667 | crc32c(con->in_data_crc, |
| 1668 | p + con->in_msg_pos.page_pos, ret); |
| 1669 | kunmap(pages[con->in_msg_pos.page]); |
| 1670 | if (ret <= 0) |
| 1671 | return ret; |
| 1672 | con->in_msg_pos.data_pos += ret; |
| 1673 | con->in_msg_pos.page_pos += ret; |
| 1674 | if (con->in_msg_pos.page_pos == PAGE_SIZE) { |
| 1675 | con->in_msg_pos.page_pos = 0; |
| 1676 | con->in_msg_pos.page++; |
| 1677 | } |
| 1678 | |
| 1679 | return ret; |
| 1680 | } |
| 1681 | |
| 1682 | #ifdef CONFIG_BLOCK |
| 1683 | static int read_partial_message_bio(struct ceph_connection *con, |
| 1684 | struct bio **bio_iter, int *bio_seg, |
| 1685 | unsigned data_len, bool do_datacrc) |
| 1686 | { |
| 1687 | struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg); |
| 1688 | void *p; |
| 1689 | int ret, left; |
| 1690 | |
| 1691 | if (IS_ERR(bv)) |
| 1692 | return PTR_ERR(bv); |
| 1693 | |
| 1694 | left = min((int)(data_len - con->in_msg_pos.data_pos), |
| 1695 | (int)(bv->bv_len - con->in_msg_pos.page_pos)); |
| 1696 | |
| 1697 | p = kmap(bv->bv_page) + bv->bv_offset; |
| 1698 | |
| 1699 | ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos, |
| 1700 | left); |
| 1701 | if (ret > 0 && do_datacrc) |
| 1702 | con->in_data_crc = |
| 1703 | crc32c(con->in_data_crc, |
| 1704 | p + con->in_msg_pos.page_pos, ret); |
| 1705 | kunmap(bv->bv_page); |
| 1706 | if (ret <= 0) |
| 1707 | return ret; |
| 1708 | con->in_msg_pos.data_pos += ret; |
| 1709 | con->in_msg_pos.page_pos += ret; |
| 1710 | if (con->in_msg_pos.page_pos == bv->bv_len) { |
| 1711 | con->in_msg_pos.page_pos = 0; |
| 1712 | iter_bio_next(bio_iter, bio_seg); |
| 1713 | } |
| 1714 | |
| 1715 | return ret; |
| 1716 | } |
| 1717 | #endif |
| 1718 | |
| 1719 | /* |
| 1720 | * read (part of) a message. |
| 1721 | */ |
| 1722 | static int read_partial_message(struct ceph_connection *con) |
| 1723 | { |
| 1724 | struct ceph_msg *m = con->in_msg; |
| 1725 | int size; |
| 1726 | int end; |
| 1727 | int ret; |
| 1728 | unsigned front_len, middle_len, data_len; |
| 1729 | bool do_datacrc = !con->msgr->nocrc; |
| 1730 | int skip; |
| 1731 | u64 seq; |
| 1732 | u32 crc; |
| 1733 | |
| 1734 | dout("read_partial_message con %p msg %p\n", con, m); |
| 1735 | |
| 1736 | /* header */ |
| 1737 | size = sizeof (con->in_hdr); |
| 1738 | end = size; |
| 1739 | ret = read_partial(con, end, size, &con->in_hdr); |
| 1740 | if (ret <= 0) |
| 1741 | return ret; |
| 1742 | |
| 1743 | crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc)); |
| 1744 | if (cpu_to_le32(crc) != con->in_hdr.crc) { |
| 1745 | pr_err("read_partial_message bad hdr " |
| 1746 | " crc %u != expected %u\n", |
| 1747 | crc, con->in_hdr.crc); |
| 1748 | return -EBADMSG; |
| 1749 | } |
| 1750 | |
| 1751 | front_len = le32_to_cpu(con->in_hdr.front_len); |
| 1752 | if (front_len > CEPH_MSG_MAX_FRONT_LEN) |
| 1753 | return -EIO; |
| 1754 | middle_len = le32_to_cpu(con->in_hdr.middle_len); |
| 1755 | if (middle_len > CEPH_MSG_MAX_DATA_LEN) |
| 1756 | return -EIO; |
| 1757 | data_len = le32_to_cpu(con->in_hdr.data_len); |
| 1758 | if (data_len > CEPH_MSG_MAX_DATA_LEN) |
| 1759 | return -EIO; |
| 1760 | |
| 1761 | /* verify seq# */ |
| 1762 | seq = le64_to_cpu(con->in_hdr.seq); |
| 1763 | if ((s64)seq - (s64)con->in_seq < 1) { |
| 1764 | pr_info("skipping %s%lld %s seq %lld expected %lld\n", |
| 1765 | ENTITY_NAME(con->peer_name), |
| 1766 | ceph_pr_addr(&con->peer_addr.in_addr), |
| 1767 | seq, con->in_seq + 1); |
| 1768 | con->in_base_pos = -front_len - middle_len - data_len - |
| 1769 | sizeof(m->footer); |
| 1770 | con->in_tag = CEPH_MSGR_TAG_READY; |
| 1771 | return 0; |
| 1772 | } else if ((s64)seq - (s64)con->in_seq > 1) { |
| 1773 | pr_err("read_partial_message bad seq %lld expected %lld\n", |
| 1774 | seq, con->in_seq + 1); |
| 1775 | con->error_msg = "bad message sequence # for incoming message"; |
| 1776 | return -EBADMSG; |
| 1777 | } |
| 1778 | |
| 1779 | /* allocate message? */ |
| 1780 | if (!con->in_msg) { |
| 1781 | dout("got hdr type %d front %d data %d\n", con->in_hdr.type, |
| 1782 | con->in_hdr.front_len, con->in_hdr.data_len); |
| 1783 | skip = 0; |
| 1784 | con->in_msg = ceph_alloc_msg(con, &con->in_hdr, &skip); |
| 1785 | if (skip) { |
| 1786 | /* skip this message */ |
| 1787 | dout("alloc_msg said skip message\n"); |
| 1788 | BUG_ON(con->in_msg); |
| 1789 | con->in_base_pos = -front_len - middle_len - data_len - |
| 1790 | sizeof(m->footer); |
| 1791 | con->in_tag = CEPH_MSGR_TAG_READY; |
| 1792 | con->in_seq++; |
| 1793 | return 0; |
| 1794 | } |
| 1795 | if (!con->in_msg) { |
| 1796 | con->error_msg = |
| 1797 | "error allocating memory for incoming message"; |
| 1798 | return -ENOMEM; |
| 1799 | } |
| 1800 | m = con->in_msg; |
| 1801 | m->front.iov_len = 0; /* haven't read it yet */ |
| 1802 | if (m->middle) |
| 1803 | m->middle->vec.iov_len = 0; |
| 1804 | |
| 1805 | con->in_msg_pos.page = 0; |
| 1806 | if (m->pages) |
| 1807 | con->in_msg_pos.page_pos = m->page_alignment; |
| 1808 | else |
| 1809 | con->in_msg_pos.page_pos = 0; |
| 1810 | con->in_msg_pos.data_pos = 0; |
| 1811 | } |
| 1812 | |
| 1813 | /* front */ |
| 1814 | ret = read_partial_message_section(con, &m->front, front_len, |
| 1815 | &con->in_front_crc); |
| 1816 | if (ret <= 0) |
| 1817 | return ret; |
| 1818 | |
| 1819 | /* middle */ |
| 1820 | if (m->middle) { |
| 1821 | ret = read_partial_message_section(con, &m->middle->vec, |
| 1822 | middle_len, |
| 1823 | &con->in_middle_crc); |
| 1824 | if (ret <= 0) |
| 1825 | return ret; |
| 1826 | } |
| 1827 | #ifdef CONFIG_BLOCK |
| 1828 | if (m->bio && !m->bio_iter) |
| 1829 | init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg); |
| 1830 | #endif |
| 1831 | |
| 1832 | /* (page) data */ |
| 1833 | while (con->in_msg_pos.data_pos < data_len) { |
| 1834 | if (m->pages) { |
| 1835 | ret = read_partial_message_pages(con, m->pages, |
| 1836 | data_len, do_datacrc); |
| 1837 | if (ret <= 0) |
| 1838 | return ret; |
| 1839 | #ifdef CONFIG_BLOCK |
| 1840 | } else if (m->bio) { |
| 1841 | |
| 1842 | ret = read_partial_message_bio(con, |
| 1843 | &m->bio_iter, &m->bio_seg, |
| 1844 | data_len, do_datacrc); |
| 1845 | if (ret <= 0) |
| 1846 | return ret; |
| 1847 | #endif |
| 1848 | } else { |
| 1849 | BUG_ON(1); |
| 1850 | } |
| 1851 | } |
| 1852 | |
| 1853 | /* footer */ |
| 1854 | size = sizeof (m->footer); |
| 1855 | end += size; |
| 1856 | ret = read_partial(con, end, size, &m->footer); |
| 1857 | if (ret <= 0) |
| 1858 | return ret; |
| 1859 | |
| 1860 | dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n", |
| 1861 | m, front_len, m->footer.front_crc, middle_len, |
| 1862 | m->footer.middle_crc, data_len, m->footer.data_crc); |
| 1863 | |
| 1864 | /* crc ok? */ |
| 1865 | if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) { |
| 1866 | pr_err("read_partial_message %p front crc %u != exp. %u\n", |
| 1867 | m, con->in_front_crc, m->footer.front_crc); |
| 1868 | return -EBADMSG; |
| 1869 | } |
| 1870 | if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) { |
| 1871 | pr_err("read_partial_message %p middle crc %u != exp %u\n", |
| 1872 | m, con->in_middle_crc, m->footer.middle_crc); |
| 1873 | return -EBADMSG; |
| 1874 | } |
| 1875 | if (do_datacrc && |
| 1876 | (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 && |
| 1877 | con->in_data_crc != le32_to_cpu(m->footer.data_crc)) { |
| 1878 | pr_err("read_partial_message %p data crc %u != exp. %u\n", m, |
| 1879 | con->in_data_crc, le32_to_cpu(m->footer.data_crc)); |
| 1880 | return -EBADMSG; |
| 1881 | } |
| 1882 | |
| 1883 | return 1; /* done! */ |
| 1884 | } |
| 1885 | |
| 1886 | /* |
| 1887 | * Process message. This happens in the worker thread. The callback should |
| 1888 | * be careful not to do anything that waits on other incoming messages or it |
| 1889 | * may deadlock. |
| 1890 | */ |
| 1891 | static void process_message(struct ceph_connection *con) |
| 1892 | { |
| 1893 | struct ceph_msg *msg; |
| 1894 | |
| 1895 | msg = con->in_msg; |
| 1896 | con->in_msg = NULL; |
| 1897 | |
| 1898 | /* if first message, set peer_name */ |
| 1899 | if (con->peer_name.type == 0) |
| 1900 | con->peer_name = msg->hdr.src; |
| 1901 | |
| 1902 | con->in_seq++; |
| 1903 | mutex_unlock(&con->mutex); |
| 1904 | |
| 1905 | dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n", |
| 1906 | msg, le64_to_cpu(msg->hdr.seq), |
| 1907 | ENTITY_NAME(msg->hdr.src), |
| 1908 | le16_to_cpu(msg->hdr.type), |
| 1909 | ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), |
| 1910 | le32_to_cpu(msg->hdr.front_len), |
| 1911 | le32_to_cpu(msg->hdr.data_len), |
| 1912 | con->in_front_crc, con->in_middle_crc, con->in_data_crc); |
| 1913 | con->ops->dispatch(con, msg); |
| 1914 | |
| 1915 | mutex_lock(&con->mutex); |
| 1916 | prepare_read_tag(con); |
| 1917 | } |
| 1918 | |
| 1919 | |
| 1920 | /* |
| 1921 | * Write something to the socket. Called in a worker thread when the |
| 1922 | * socket appears to be writeable and we have something ready to send. |
| 1923 | */ |
| 1924 | static int try_write(struct ceph_connection *con) |
| 1925 | { |
| 1926 | int ret = 1; |
| 1927 | |
| 1928 | dout("try_write start %p state %lu nref %d\n", con, con->state, |
| 1929 | atomic_read(&con->nref)); |
| 1930 | |
| 1931 | more: |
| 1932 | dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes); |
| 1933 | |
| 1934 | /* open the socket first? */ |
| 1935 | if (con->sock == NULL) { |
| 1936 | con_out_kvec_reset(con); |
| 1937 | prepare_write_banner(con); |
| 1938 | ret = prepare_write_connect(con); |
| 1939 | if (ret < 0) |
| 1940 | goto out; |
| 1941 | prepare_read_banner(con); |
| 1942 | set_bit(CONNECTING, &con->state); |
| 1943 | clear_bit(NEGOTIATING, &con->state); |
| 1944 | |
| 1945 | BUG_ON(con->in_msg); |
| 1946 | con->in_tag = CEPH_MSGR_TAG_READY; |
| 1947 | dout("try_write initiating connect on %p new state %lu\n", |
| 1948 | con, con->state); |
| 1949 | ret = ceph_tcp_connect(con); |
| 1950 | if (ret < 0) { |
| 1951 | con->error_msg = "connect error"; |
| 1952 | goto out; |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | more_kvec: |
| 1957 | /* kvec data queued? */ |
| 1958 | if (con->out_skip) { |
| 1959 | ret = write_partial_skip(con); |
| 1960 | if (ret <= 0) |
| 1961 | goto out; |
| 1962 | } |
| 1963 | if (con->out_kvec_left) { |
| 1964 | ret = write_partial_kvec(con); |
| 1965 | if (ret <= 0) |
| 1966 | goto out; |
| 1967 | } |
| 1968 | |
| 1969 | /* msg pages? */ |
| 1970 | if (con->out_msg) { |
| 1971 | if (con->out_msg_done) { |
| 1972 | ceph_msg_put(con->out_msg); |
| 1973 | con->out_msg = NULL; /* we're done with this one */ |
| 1974 | goto do_next; |
| 1975 | } |
| 1976 | |
| 1977 | ret = write_partial_msg_pages(con); |
| 1978 | if (ret == 1) |
| 1979 | goto more_kvec; /* we need to send the footer, too! */ |
| 1980 | if (ret == 0) |
| 1981 | goto out; |
| 1982 | if (ret < 0) { |
| 1983 | dout("try_write write_partial_msg_pages err %d\n", |
| 1984 | ret); |
| 1985 | goto out; |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | do_next: |
| 1990 | if (!test_bit(CONNECTING, &con->state)) { |
| 1991 | /* is anything else pending? */ |
| 1992 | if (!list_empty(&con->out_queue)) { |
| 1993 | prepare_write_message(con); |
| 1994 | goto more; |
| 1995 | } |
| 1996 | if (con->in_seq > con->in_seq_acked) { |
| 1997 | prepare_write_ack(con); |
| 1998 | goto more; |
| 1999 | } |
| 2000 | if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) { |
| 2001 | prepare_write_keepalive(con); |
| 2002 | goto more; |
| 2003 | } |
| 2004 | } |
| 2005 | |
| 2006 | /* Nothing to do! */ |
| 2007 | clear_bit(WRITE_PENDING, &con->flags); |
| 2008 | dout("try_write nothing else to write.\n"); |
| 2009 | ret = 0; |
| 2010 | out: |
| 2011 | dout("try_write done on %p ret %d\n", con, ret); |
| 2012 | return ret; |
| 2013 | } |
| 2014 | |
| 2015 | |
| 2016 | |
| 2017 | /* |
| 2018 | * Read what we can from the socket. |
| 2019 | */ |
| 2020 | static int try_read(struct ceph_connection *con) |
| 2021 | { |
| 2022 | int ret = -1; |
| 2023 | |
| 2024 | if (!con->sock) |
| 2025 | return 0; |
| 2026 | |
| 2027 | if (test_bit(STANDBY, &con->state)) |
| 2028 | return 0; |
| 2029 | |
| 2030 | dout("try_read start on %p\n", con); |
| 2031 | |
| 2032 | more: |
| 2033 | dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag, |
| 2034 | con->in_base_pos); |
| 2035 | |
| 2036 | /* |
| 2037 | * process_connect and process_message drop and re-take |
| 2038 | * con->mutex. make sure we handle a racing close or reopen. |
| 2039 | */ |
| 2040 | if (test_bit(CLOSED, &con->state) || |
| 2041 | test_bit(OPENING, &con->state)) { |
| 2042 | ret = -EAGAIN; |
| 2043 | goto out; |
| 2044 | } |
| 2045 | |
| 2046 | if (test_bit(CONNECTING, &con->state)) { |
| 2047 | if (!test_bit(NEGOTIATING, &con->state)) { |
| 2048 | dout("try_read connecting\n"); |
| 2049 | ret = read_partial_banner(con); |
| 2050 | if (ret <= 0) |
| 2051 | goto out; |
| 2052 | ret = process_banner(con); |
| 2053 | if (ret < 0) |
| 2054 | goto out; |
| 2055 | } |
| 2056 | ret = read_partial_connect(con); |
| 2057 | if (ret <= 0) |
| 2058 | goto out; |
| 2059 | ret = process_connect(con); |
| 2060 | if (ret < 0) |
| 2061 | goto out; |
| 2062 | goto more; |
| 2063 | } |
| 2064 | |
| 2065 | if (con->in_base_pos < 0) { |
| 2066 | /* |
| 2067 | * skipping + discarding content. |
| 2068 | * |
| 2069 | * FIXME: there must be a better way to do this! |
| 2070 | */ |
| 2071 | static char buf[SKIP_BUF_SIZE]; |
| 2072 | int skip = min((int) sizeof (buf), -con->in_base_pos); |
| 2073 | |
| 2074 | dout("skipping %d / %d bytes\n", skip, -con->in_base_pos); |
| 2075 | ret = ceph_tcp_recvmsg(con->sock, buf, skip); |
| 2076 | if (ret <= 0) |
| 2077 | goto out; |
| 2078 | con->in_base_pos += ret; |
| 2079 | if (con->in_base_pos) |
| 2080 | goto more; |
| 2081 | } |
| 2082 | if (con->in_tag == CEPH_MSGR_TAG_READY) { |
| 2083 | /* |
| 2084 | * what's next? |
| 2085 | */ |
| 2086 | ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1); |
| 2087 | if (ret <= 0) |
| 2088 | goto out; |
| 2089 | dout("try_read got tag %d\n", (int)con->in_tag); |
| 2090 | switch (con->in_tag) { |
| 2091 | case CEPH_MSGR_TAG_MSG: |
| 2092 | prepare_read_message(con); |
| 2093 | break; |
| 2094 | case CEPH_MSGR_TAG_ACK: |
| 2095 | prepare_read_ack(con); |
| 2096 | break; |
| 2097 | case CEPH_MSGR_TAG_CLOSE: |
| 2098 | set_bit(CLOSED, &con->state); /* fixme */ |
| 2099 | goto out; |
| 2100 | default: |
| 2101 | goto bad_tag; |
| 2102 | } |
| 2103 | } |
| 2104 | if (con->in_tag == CEPH_MSGR_TAG_MSG) { |
| 2105 | ret = read_partial_message(con); |
| 2106 | if (ret <= 0) { |
| 2107 | switch (ret) { |
| 2108 | case -EBADMSG: |
| 2109 | con->error_msg = "bad crc"; |
| 2110 | ret = -EIO; |
| 2111 | break; |
| 2112 | case -EIO: |
| 2113 | con->error_msg = "io error"; |
| 2114 | break; |
| 2115 | } |
| 2116 | goto out; |
| 2117 | } |
| 2118 | if (con->in_tag == CEPH_MSGR_TAG_READY) |
| 2119 | goto more; |
| 2120 | process_message(con); |
| 2121 | goto more; |
| 2122 | } |
| 2123 | if (con->in_tag == CEPH_MSGR_TAG_ACK) { |
| 2124 | ret = read_partial_ack(con); |
| 2125 | if (ret <= 0) |
| 2126 | goto out; |
| 2127 | process_ack(con); |
| 2128 | goto more; |
| 2129 | } |
| 2130 | |
| 2131 | out: |
| 2132 | dout("try_read done on %p ret %d\n", con, ret); |
| 2133 | return ret; |
| 2134 | |
| 2135 | bad_tag: |
| 2136 | pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag); |
| 2137 | con->error_msg = "protocol error, garbage tag"; |
| 2138 | ret = -1; |
| 2139 | goto out; |
| 2140 | } |
| 2141 | |
| 2142 | |
| 2143 | /* |
| 2144 | * Atomically queue work on a connection. Bump @con reference to |
| 2145 | * avoid races with connection teardown. |
| 2146 | */ |
| 2147 | static void queue_con(struct ceph_connection *con) |
| 2148 | { |
| 2149 | if (!con->ops->get(con)) { |
| 2150 | dout("queue_con %p ref count 0\n", con); |
| 2151 | return; |
| 2152 | } |
| 2153 | |
| 2154 | if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) { |
| 2155 | dout("queue_con %p - already queued\n", con); |
| 2156 | con->ops->put(con); |
| 2157 | } else { |
| 2158 | dout("queue_con %p\n", con); |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | /* |
| 2163 | * Do some work on a connection. Drop a connection ref when we're done. |
| 2164 | */ |
| 2165 | static void con_work(struct work_struct *work) |
| 2166 | { |
| 2167 | struct ceph_connection *con = container_of(work, struct ceph_connection, |
| 2168 | work.work); |
| 2169 | int ret; |
| 2170 | |
| 2171 | mutex_lock(&con->mutex); |
| 2172 | restart: |
| 2173 | if (test_and_clear_bit(BACKOFF, &con->flags)) { |
| 2174 | dout("con_work %p backing off\n", con); |
| 2175 | if (queue_delayed_work(ceph_msgr_wq, &con->work, |
| 2176 | round_jiffies_relative(con->delay))) { |
| 2177 | dout("con_work %p backoff %lu\n", con, con->delay); |
| 2178 | mutex_unlock(&con->mutex); |
| 2179 | return; |
| 2180 | } else { |
| 2181 | con->ops->put(con); |
| 2182 | dout("con_work %p FAILED to back off %lu\n", con, |
| 2183 | con->delay); |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | if (test_bit(STANDBY, &con->state)) { |
| 2188 | dout("con_work %p STANDBY\n", con); |
| 2189 | goto done; |
| 2190 | } |
| 2191 | if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */ |
| 2192 | dout("con_work CLOSED\n"); |
| 2193 | con_close_socket(con); |
| 2194 | goto done; |
| 2195 | } |
| 2196 | if (test_and_clear_bit(OPENING, &con->state)) { |
| 2197 | /* reopen w/ new peer */ |
| 2198 | dout("con_work OPENING\n"); |
| 2199 | con_close_socket(con); |
| 2200 | } |
| 2201 | |
| 2202 | if (test_and_clear_bit(SOCK_CLOSED, &con->flags)) |
| 2203 | goto fault; |
| 2204 | |
| 2205 | ret = try_read(con); |
| 2206 | if (ret == -EAGAIN) |
| 2207 | goto restart; |
| 2208 | if (ret < 0) |
| 2209 | goto fault; |
| 2210 | |
| 2211 | ret = try_write(con); |
| 2212 | if (ret == -EAGAIN) |
| 2213 | goto restart; |
| 2214 | if (ret < 0) |
| 2215 | goto fault; |
| 2216 | |
| 2217 | done: |
| 2218 | mutex_unlock(&con->mutex); |
| 2219 | done_unlocked: |
| 2220 | con->ops->put(con); |
| 2221 | return; |
| 2222 | |
| 2223 | fault: |
| 2224 | mutex_unlock(&con->mutex); |
| 2225 | ceph_fault(con); /* error/fault path */ |
| 2226 | goto done_unlocked; |
| 2227 | } |
| 2228 | |
| 2229 | |
| 2230 | /* |
| 2231 | * Generic error/fault handler. A retry mechanism is used with |
| 2232 | * exponential backoff |
| 2233 | */ |
| 2234 | static void ceph_fault(struct ceph_connection *con) |
| 2235 | { |
| 2236 | pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name), |
| 2237 | ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg); |
| 2238 | dout("fault %p state %lu to peer %s\n", |
| 2239 | con, con->state, ceph_pr_addr(&con->peer_addr.in_addr)); |
| 2240 | |
| 2241 | if (test_bit(LOSSYTX, &con->flags)) { |
| 2242 | dout("fault on LOSSYTX channel\n"); |
| 2243 | goto out; |
| 2244 | } |
| 2245 | |
| 2246 | mutex_lock(&con->mutex); |
| 2247 | if (test_bit(CLOSED, &con->state)) |
| 2248 | goto out_unlock; |
| 2249 | |
| 2250 | con_close_socket(con); |
| 2251 | |
| 2252 | if (con->in_msg) { |
| 2253 | ceph_msg_put(con->in_msg); |
| 2254 | con->in_msg = NULL; |
| 2255 | } |
| 2256 | |
| 2257 | /* Requeue anything that hasn't been acked */ |
| 2258 | list_splice_init(&con->out_sent, &con->out_queue); |
| 2259 | |
| 2260 | /* If there are no messages queued or keepalive pending, place |
| 2261 | * the connection in a STANDBY state */ |
| 2262 | if (list_empty(&con->out_queue) && |
| 2263 | !test_bit(KEEPALIVE_PENDING, &con->flags)) { |
| 2264 | dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con); |
| 2265 | clear_bit(WRITE_PENDING, &con->flags); |
| 2266 | set_bit(STANDBY, &con->state); |
| 2267 | } else { |
| 2268 | /* retry after a delay. */ |
| 2269 | if (con->delay == 0) |
| 2270 | con->delay = BASE_DELAY_INTERVAL; |
| 2271 | else if (con->delay < MAX_DELAY_INTERVAL) |
| 2272 | con->delay *= 2; |
| 2273 | con->ops->get(con); |
| 2274 | if (queue_delayed_work(ceph_msgr_wq, &con->work, |
| 2275 | round_jiffies_relative(con->delay))) { |
| 2276 | dout("fault queued %p delay %lu\n", con, con->delay); |
| 2277 | } else { |
| 2278 | con->ops->put(con); |
| 2279 | dout("fault failed to queue %p delay %lu, backoff\n", |
| 2280 | con, con->delay); |
| 2281 | /* |
| 2282 | * In many cases we see a socket state change |
| 2283 | * while con_work is running and end up |
| 2284 | * queuing (non-delayed) work, such that we |
| 2285 | * can't backoff with a delay. Set a flag so |
| 2286 | * that when con_work restarts we schedule the |
| 2287 | * delay then. |
| 2288 | */ |
| 2289 | set_bit(BACKOFF, &con->flags); |
| 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | out_unlock: |
| 2294 | mutex_unlock(&con->mutex); |
| 2295 | out: |
| 2296 | /* |
| 2297 | * in case we faulted due to authentication, invalidate our |
| 2298 | * current tickets so that we can get new ones. |
| 2299 | */ |
| 2300 | if (con->auth_retry && con->ops->invalidate_authorizer) { |
| 2301 | dout("calling invalidate_authorizer()\n"); |
| 2302 | con->ops->invalidate_authorizer(con); |
| 2303 | } |
| 2304 | |
| 2305 | if (con->ops->fault) |
| 2306 | con->ops->fault(con); |
| 2307 | } |
| 2308 | |
| 2309 | |
| 2310 | |
| 2311 | /* |
| 2312 | * initialize a new messenger instance |
| 2313 | */ |
| 2314 | void ceph_messenger_init(struct ceph_messenger *msgr, |
| 2315 | struct ceph_entity_addr *myaddr, |
| 2316 | u32 supported_features, |
| 2317 | u32 required_features, |
| 2318 | bool nocrc) |
| 2319 | { |
| 2320 | msgr->supported_features = supported_features; |
| 2321 | msgr->required_features = required_features; |
| 2322 | |
| 2323 | spin_lock_init(&msgr->global_seq_lock); |
| 2324 | |
| 2325 | if (myaddr) |
| 2326 | msgr->inst.addr = *myaddr; |
| 2327 | |
| 2328 | /* select a random nonce */ |
| 2329 | msgr->inst.addr.type = 0; |
| 2330 | get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce)); |
| 2331 | encode_my_addr(msgr); |
| 2332 | msgr->nocrc = nocrc; |
| 2333 | |
| 2334 | dout("%s %p\n", __func__, msgr); |
| 2335 | } |
| 2336 | EXPORT_SYMBOL(ceph_messenger_init); |
| 2337 | |
| 2338 | static void clear_standby(struct ceph_connection *con) |
| 2339 | { |
| 2340 | /* come back from STANDBY? */ |
| 2341 | if (test_and_clear_bit(STANDBY, &con->state)) { |
| 2342 | mutex_lock(&con->mutex); |
| 2343 | dout("clear_standby %p and ++connect_seq\n", con); |
| 2344 | con->connect_seq++; |
| 2345 | WARN_ON(test_bit(WRITE_PENDING, &con->flags)); |
| 2346 | WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags)); |
| 2347 | mutex_unlock(&con->mutex); |
| 2348 | } |
| 2349 | } |
| 2350 | |
| 2351 | /* |
| 2352 | * Queue up an outgoing message on the given connection. |
| 2353 | */ |
| 2354 | void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg) |
| 2355 | { |
| 2356 | if (test_bit(CLOSED, &con->state)) { |
| 2357 | dout("con_send %p closed, dropping %p\n", con, msg); |
| 2358 | ceph_msg_put(msg); |
| 2359 | return; |
| 2360 | } |
| 2361 | |
| 2362 | /* set src+dst */ |
| 2363 | msg->hdr.src = con->msgr->inst.name; |
| 2364 | |
| 2365 | BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len)); |
| 2366 | |
| 2367 | msg->needs_out_seq = true; |
| 2368 | |
| 2369 | /* queue */ |
| 2370 | mutex_lock(&con->mutex); |
| 2371 | BUG_ON(!list_empty(&msg->list_head)); |
| 2372 | list_add_tail(&msg->list_head, &con->out_queue); |
| 2373 | dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg, |
| 2374 | ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type), |
| 2375 | ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), |
| 2376 | le32_to_cpu(msg->hdr.front_len), |
| 2377 | le32_to_cpu(msg->hdr.middle_len), |
| 2378 | le32_to_cpu(msg->hdr.data_len)); |
| 2379 | mutex_unlock(&con->mutex); |
| 2380 | |
| 2381 | /* if there wasn't anything waiting to send before, queue |
| 2382 | * new work */ |
| 2383 | clear_standby(con); |
| 2384 | if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0) |
| 2385 | queue_con(con); |
| 2386 | } |
| 2387 | EXPORT_SYMBOL(ceph_con_send); |
| 2388 | |
| 2389 | /* |
| 2390 | * Revoke a message that was previously queued for send |
| 2391 | */ |
| 2392 | void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg) |
| 2393 | { |
| 2394 | mutex_lock(&con->mutex); |
| 2395 | if (!list_empty(&msg->list_head)) { |
| 2396 | dout("con_revoke %p msg %p - was on queue\n", con, msg); |
| 2397 | list_del_init(&msg->list_head); |
| 2398 | ceph_msg_put(msg); |
| 2399 | msg->hdr.seq = 0; |
| 2400 | } |
| 2401 | if (con->out_msg == msg) { |
| 2402 | dout("con_revoke %p msg %p - was sending\n", con, msg); |
| 2403 | con->out_msg = NULL; |
| 2404 | if (con->out_kvec_is_msg) { |
| 2405 | con->out_skip = con->out_kvec_bytes; |
| 2406 | con->out_kvec_is_msg = false; |
| 2407 | } |
| 2408 | ceph_msg_put(msg); |
| 2409 | msg->hdr.seq = 0; |
| 2410 | } |
| 2411 | mutex_unlock(&con->mutex); |
| 2412 | } |
| 2413 | |
| 2414 | /* |
| 2415 | * Revoke a message that we may be reading data into |
| 2416 | */ |
| 2417 | void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg) |
| 2418 | { |
| 2419 | mutex_lock(&con->mutex); |
| 2420 | if (con->in_msg && con->in_msg == msg) { |
| 2421 | unsigned front_len = le32_to_cpu(con->in_hdr.front_len); |
| 2422 | unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len); |
| 2423 | unsigned data_len = le32_to_cpu(con->in_hdr.data_len); |
| 2424 | |
| 2425 | /* skip rest of message */ |
| 2426 | dout("con_revoke_pages %p msg %p revoked\n", con, msg); |
| 2427 | con->in_base_pos = con->in_base_pos - |
| 2428 | sizeof(struct ceph_msg_header) - |
| 2429 | front_len - |
| 2430 | middle_len - |
| 2431 | data_len - |
| 2432 | sizeof(struct ceph_msg_footer); |
| 2433 | ceph_msg_put(con->in_msg); |
| 2434 | con->in_msg = NULL; |
| 2435 | con->in_tag = CEPH_MSGR_TAG_READY; |
| 2436 | con->in_seq++; |
| 2437 | } else { |
| 2438 | dout("con_revoke_pages %p msg %p pages %p no-op\n", |
| 2439 | con, con->in_msg, msg); |
| 2440 | } |
| 2441 | mutex_unlock(&con->mutex); |
| 2442 | } |
| 2443 | |
| 2444 | /* |
| 2445 | * Queue a keepalive byte to ensure the tcp connection is alive. |
| 2446 | */ |
| 2447 | void ceph_con_keepalive(struct ceph_connection *con) |
| 2448 | { |
| 2449 | dout("con_keepalive %p\n", con); |
| 2450 | clear_standby(con); |
| 2451 | if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 && |
| 2452 | test_and_set_bit(WRITE_PENDING, &con->flags) == 0) |
| 2453 | queue_con(con); |
| 2454 | } |
| 2455 | EXPORT_SYMBOL(ceph_con_keepalive); |
| 2456 | |
| 2457 | |
| 2458 | /* |
| 2459 | * construct a new message with given type, size |
| 2460 | * the new msg has a ref count of 1. |
| 2461 | */ |
| 2462 | struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags, |
| 2463 | bool can_fail) |
| 2464 | { |
| 2465 | struct ceph_msg *m; |
| 2466 | |
| 2467 | m = kmalloc(sizeof(*m), flags); |
| 2468 | if (m == NULL) |
| 2469 | goto out; |
| 2470 | kref_init(&m->kref); |
| 2471 | INIT_LIST_HEAD(&m->list_head); |
| 2472 | |
| 2473 | m->hdr.tid = 0; |
| 2474 | m->hdr.type = cpu_to_le16(type); |
| 2475 | m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT); |
| 2476 | m->hdr.version = 0; |
| 2477 | m->hdr.front_len = cpu_to_le32(front_len); |
| 2478 | m->hdr.middle_len = 0; |
| 2479 | m->hdr.data_len = 0; |
| 2480 | m->hdr.data_off = 0; |
| 2481 | m->hdr.reserved = 0; |
| 2482 | m->footer.front_crc = 0; |
| 2483 | m->footer.middle_crc = 0; |
| 2484 | m->footer.data_crc = 0; |
| 2485 | m->footer.flags = 0; |
| 2486 | m->front_max = front_len; |
| 2487 | m->front_is_vmalloc = false; |
| 2488 | m->more_to_follow = false; |
| 2489 | m->ack_stamp = 0; |
| 2490 | m->pool = NULL; |
| 2491 | |
| 2492 | /* middle */ |
| 2493 | m->middle = NULL; |
| 2494 | |
| 2495 | /* data */ |
| 2496 | m->nr_pages = 0; |
| 2497 | m->page_alignment = 0; |
| 2498 | m->pages = NULL; |
| 2499 | m->pagelist = NULL; |
| 2500 | m->bio = NULL; |
| 2501 | m->bio_iter = NULL; |
| 2502 | m->bio_seg = 0; |
| 2503 | m->trail = NULL; |
| 2504 | |
| 2505 | /* front */ |
| 2506 | if (front_len) { |
| 2507 | if (front_len > PAGE_CACHE_SIZE) { |
| 2508 | m->front.iov_base = __vmalloc(front_len, flags, |
| 2509 | PAGE_KERNEL); |
| 2510 | m->front_is_vmalloc = true; |
| 2511 | } else { |
| 2512 | m->front.iov_base = kmalloc(front_len, flags); |
| 2513 | } |
| 2514 | if (m->front.iov_base == NULL) { |
| 2515 | dout("ceph_msg_new can't allocate %d bytes\n", |
| 2516 | front_len); |
| 2517 | goto out2; |
| 2518 | } |
| 2519 | } else { |
| 2520 | m->front.iov_base = NULL; |
| 2521 | } |
| 2522 | m->front.iov_len = front_len; |
| 2523 | |
| 2524 | dout("ceph_msg_new %p front %d\n", m, front_len); |
| 2525 | return m; |
| 2526 | |
| 2527 | out2: |
| 2528 | ceph_msg_put(m); |
| 2529 | out: |
| 2530 | if (!can_fail) { |
| 2531 | pr_err("msg_new can't create type %d front %d\n", type, |
| 2532 | front_len); |
| 2533 | WARN_ON(1); |
| 2534 | } else { |
| 2535 | dout("msg_new can't create type %d front %d\n", type, |
| 2536 | front_len); |
| 2537 | } |
| 2538 | return NULL; |
| 2539 | } |
| 2540 | EXPORT_SYMBOL(ceph_msg_new); |
| 2541 | |
| 2542 | /* |
| 2543 | * Allocate "middle" portion of a message, if it is needed and wasn't |
| 2544 | * allocated by alloc_msg. This allows us to read a small fixed-size |
| 2545 | * per-type header in the front and then gracefully fail (i.e., |
| 2546 | * propagate the error to the caller based on info in the front) when |
| 2547 | * the middle is too large. |
| 2548 | */ |
| 2549 | static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg) |
| 2550 | { |
| 2551 | int type = le16_to_cpu(msg->hdr.type); |
| 2552 | int middle_len = le32_to_cpu(msg->hdr.middle_len); |
| 2553 | |
| 2554 | dout("alloc_middle %p type %d %s middle_len %d\n", msg, type, |
| 2555 | ceph_msg_type_name(type), middle_len); |
| 2556 | BUG_ON(!middle_len); |
| 2557 | BUG_ON(msg->middle); |
| 2558 | |
| 2559 | msg->middle = ceph_buffer_new(middle_len, GFP_NOFS); |
| 2560 | if (!msg->middle) |
| 2561 | return -ENOMEM; |
| 2562 | return 0; |
| 2563 | } |
| 2564 | |
| 2565 | /* |
| 2566 | * Generic message allocator, for incoming messages. |
| 2567 | */ |
| 2568 | static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con, |
| 2569 | struct ceph_msg_header *hdr, |
| 2570 | int *skip) |
| 2571 | { |
| 2572 | int type = le16_to_cpu(hdr->type); |
| 2573 | int front_len = le32_to_cpu(hdr->front_len); |
| 2574 | int middle_len = le32_to_cpu(hdr->middle_len); |
| 2575 | struct ceph_msg *msg = NULL; |
| 2576 | int ret; |
| 2577 | |
| 2578 | if (con->ops->alloc_msg) { |
| 2579 | mutex_unlock(&con->mutex); |
| 2580 | msg = con->ops->alloc_msg(con, hdr, skip); |
| 2581 | mutex_lock(&con->mutex); |
| 2582 | if (!msg || *skip) |
| 2583 | return NULL; |
| 2584 | } |
| 2585 | if (!msg) { |
| 2586 | *skip = 0; |
| 2587 | msg = ceph_msg_new(type, front_len, GFP_NOFS, false); |
| 2588 | if (!msg) { |
| 2589 | pr_err("unable to allocate msg type %d len %d\n", |
| 2590 | type, front_len); |
| 2591 | return NULL; |
| 2592 | } |
| 2593 | msg->page_alignment = le16_to_cpu(hdr->data_off); |
| 2594 | } |
| 2595 | memcpy(&msg->hdr, &con->in_hdr, sizeof(con->in_hdr)); |
| 2596 | |
| 2597 | if (middle_len && !msg->middle) { |
| 2598 | ret = ceph_alloc_middle(con, msg); |
| 2599 | if (ret < 0) { |
| 2600 | ceph_msg_put(msg); |
| 2601 | return NULL; |
| 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | return msg; |
| 2606 | } |
| 2607 | |
| 2608 | |
| 2609 | /* |
| 2610 | * Free a generically kmalloc'd message. |
| 2611 | */ |
| 2612 | void ceph_msg_kfree(struct ceph_msg *m) |
| 2613 | { |
| 2614 | dout("msg_kfree %p\n", m); |
| 2615 | if (m->front_is_vmalloc) |
| 2616 | vfree(m->front.iov_base); |
| 2617 | else |
| 2618 | kfree(m->front.iov_base); |
| 2619 | kfree(m); |
| 2620 | } |
| 2621 | |
| 2622 | /* |
| 2623 | * Drop a msg ref. Destroy as needed. |
| 2624 | */ |
| 2625 | void ceph_msg_last_put(struct kref *kref) |
| 2626 | { |
| 2627 | struct ceph_msg *m = container_of(kref, struct ceph_msg, kref); |
| 2628 | |
| 2629 | dout("ceph_msg_put last one on %p\n", m); |
| 2630 | WARN_ON(!list_empty(&m->list_head)); |
| 2631 | |
| 2632 | /* drop middle, data, if any */ |
| 2633 | if (m->middle) { |
| 2634 | ceph_buffer_put(m->middle); |
| 2635 | m->middle = NULL; |
| 2636 | } |
| 2637 | m->nr_pages = 0; |
| 2638 | m->pages = NULL; |
| 2639 | |
| 2640 | if (m->pagelist) { |
| 2641 | ceph_pagelist_release(m->pagelist); |
| 2642 | kfree(m->pagelist); |
| 2643 | m->pagelist = NULL; |
| 2644 | } |
| 2645 | |
| 2646 | m->trail = NULL; |
| 2647 | |
| 2648 | if (m->pool) |
| 2649 | ceph_msgpool_put(m->pool, m); |
| 2650 | else |
| 2651 | ceph_msg_kfree(m); |
| 2652 | } |
| 2653 | EXPORT_SYMBOL(ceph_msg_last_put); |
| 2654 | |
| 2655 | void ceph_msg_dump(struct ceph_msg *msg) |
| 2656 | { |
| 2657 | pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg, |
| 2658 | msg->front_max, msg->nr_pages); |
| 2659 | print_hex_dump(KERN_DEBUG, "header: ", |
| 2660 | DUMP_PREFIX_OFFSET, 16, 1, |
| 2661 | &msg->hdr, sizeof(msg->hdr), true); |
| 2662 | print_hex_dump(KERN_DEBUG, " front: ", |
| 2663 | DUMP_PREFIX_OFFSET, 16, 1, |
| 2664 | msg->front.iov_base, msg->front.iov_len, true); |
| 2665 | if (msg->middle) |
| 2666 | print_hex_dump(KERN_DEBUG, "middle: ", |
| 2667 | DUMP_PREFIX_OFFSET, 16, 1, |
| 2668 | msg->middle->vec.iov_base, |
| 2669 | msg->middle->vec.iov_len, true); |
| 2670 | print_hex_dump(KERN_DEBUG, "footer: ", |
| 2671 | DUMP_PREFIX_OFFSET, 16, 1, |
| 2672 | &msg->footer, sizeof(msg->footer), true); |
| 2673 | } |
| 2674 | EXPORT_SYMBOL(ceph_msg_dump); |