]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/ceph/messenger.c
libceph: fall back to sendmsg for slab pages
[mirror_ubuntu-bionic-kernel.git] / net / ceph / messenger.c
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 /*
86 * connection states
87 */
88 #define CON_STATE_CLOSED 1 /* -> PREOPEN */
89 #define CON_STATE_PREOPEN 2 /* -> CONNECTING, CLOSED */
90 #define CON_STATE_CONNECTING 3 /* -> NEGOTIATING, CLOSED */
91 #define CON_STATE_NEGOTIATING 4 /* -> OPEN, CLOSED */
92 #define CON_STATE_OPEN 5 /* -> STANDBY, CLOSED */
93 #define CON_STATE_STANDBY 6 /* -> PREOPEN, CLOSED */
94
95 /*
96 * ceph_connection flag bits
97 */
98 #define CON_FLAG_LOSSYTX 0 /* we can close channel or drop
99 * messages on errors */
100 #define CON_FLAG_KEEPALIVE_PENDING 1 /* we need to send a keepalive */
101 #define CON_FLAG_WRITE_PENDING 2 /* we have data ready to send */
102 #define CON_FLAG_SOCK_CLOSED 3 /* socket state changed to closed */
103 #define CON_FLAG_BACKOFF 4 /* need to retry queuing delayed work */
104
105 static bool con_flag_valid(unsigned long con_flag)
106 {
107 switch (con_flag) {
108 case CON_FLAG_LOSSYTX:
109 case CON_FLAG_KEEPALIVE_PENDING:
110 case CON_FLAG_WRITE_PENDING:
111 case CON_FLAG_SOCK_CLOSED:
112 case CON_FLAG_BACKOFF:
113 return true;
114 default:
115 return false;
116 }
117 }
118
119 static void con_flag_clear(struct ceph_connection *con, unsigned long con_flag)
120 {
121 BUG_ON(!con_flag_valid(con_flag));
122
123 clear_bit(con_flag, &con->flags);
124 }
125
126 static void con_flag_set(struct ceph_connection *con, unsigned long con_flag)
127 {
128 BUG_ON(!con_flag_valid(con_flag));
129
130 set_bit(con_flag, &con->flags);
131 }
132
133 static bool con_flag_test(struct ceph_connection *con, unsigned long con_flag)
134 {
135 BUG_ON(!con_flag_valid(con_flag));
136
137 return test_bit(con_flag, &con->flags);
138 }
139
140 static bool con_flag_test_and_clear(struct ceph_connection *con,
141 unsigned long con_flag)
142 {
143 BUG_ON(!con_flag_valid(con_flag));
144
145 return test_and_clear_bit(con_flag, &con->flags);
146 }
147
148 static bool con_flag_test_and_set(struct ceph_connection *con,
149 unsigned long con_flag)
150 {
151 BUG_ON(!con_flag_valid(con_flag));
152
153 return test_and_set_bit(con_flag, &con->flags);
154 }
155
156 /* Slab caches for frequently-allocated structures */
157
158 static struct kmem_cache *ceph_msg_cache;
159 static struct kmem_cache *ceph_msg_data_cache;
160
161 /* static tag bytes (protocol control messages) */
162 static char tag_msg = CEPH_MSGR_TAG_MSG;
163 static char tag_ack = CEPH_MSGR_TAG_ACK;
164 static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
165 static char tag_keepalive2 = CEPH_MSGR_TAG_KEEPALIVE2;
166
167 #ifdef CONFIG_LOCKDEP
168 static struct lock_class_key socket_class;
169 #endif
170
171 /*
172 * When skipping (ignoring) a block of input we read it into a "skip
173 * buffer," which is this many bytes in size.
174 */
175 #define SKIP_BUF_SIZE 1024
176
177 static void queue_con(struct ceph_connection *con);
178 static void cancel_con(struct ceph_connection *con);
179 static void ceph_con_workfn(struct work_struct *);
180 static void con_fault(struct ceph_connection *con);
181
182 /*
183 * Nicely render a sockaddr as a string. An array of formatted
184 * strings is used, to approximate reentrancy.
185 */
186 #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
187 #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
188 #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
189 #define MAX_ADDR_STR_LEN 64 /* 54 is enough */
190
191 static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
192 static atomic_t addr_str_seq = ATOMIC_INIT(0);
193
194 static struct page *zero_page; /* used in certain error cases */
195
196 const char *ceph_pr_addr(const struct sockaddr_storage *ss)
197 {
198 int i;
199 char *s;
200 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
201 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
202
203 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
204 s = addr_str[i];
205
206 switch (ss->ss_family) {
207 case AF_INET:
208 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
209 ntohs(in4->sin_port));
210 break;
211
212 case AF_INET6:
213 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
214 ntohs(in6->sin6_port));
215 break;
216
217 default:
218 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
219 ss->ss_family);
220 }
221
222 return s;
223 }
224 EXPORT_SYMBOL(ceph_pr_addr);
225
226 static void encode_my_addr(struct ceph_messenger *msgr)
227 {
228 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
229 ceph_encode_addr(&msgr->my_enc_addr);
230 }
231
232 /*
233 * work queue for all reading and writing to/from the socket.
234 */
235 static struct workqueue_struct *ceph_msgr_wq;
236
237 static int ceph_msgr_slab_init(void)
238 {
239 BUG_ON(ceph_msg_cache);
240 ceph_msg_cache = KMEM_CACHE(ceph_msg, 0);
241 if (!ceph_msg_cache)
242 return -ENOMEM;
243
244 BUG_ON(ceph_msg_data_cache);
245 ceph_msg_data_cache = KMEM_CACHE(ceph_msg_data, 0);
246 if (ceph_msg_data_cache)
247 return 0;
248
249 kmem_cache_destroy(ceph_msg_cache);
250 ceph_msg_cache = NULL;
251
252 return -ENOMEM;
253 }
254
255 static void ceph_msgr_slab_exit(void)
256 {
257 BUG_ON(!ceph_msg_data_cache);
258 kmem_cache_destroy(ceph_msg_data_cache);
259 ceph_msg_data_cache = NULL;
260
261 BUG_ON(!ceph_msg_cache);
262 kmem_cache_destroy(ceph_msg_cache);
263 ceph_msg_cache = NULL;
264 }
265
266 static void _ceph_msgr_exit(void)
267 {
268 if (ceph_msgr_wq) {
269 destroy_workqueue(ceph_msgr_wq);
270 ceph_msgr_wq = NULL;
271 }
272
273 BUG_ON(zero_page == NULL);
274 put_page(zero_page);
275 zero_page = NULL;
276
277 ceph_msgr_slab_exit();
278 }
279
280 int ceph_msgr_init(void)
281 {
282 if (ceph_msgr_slab_init())
283 return -ENOMEM;
284
285 BUG_ON(zero_page != NULL);
286 zero_page = ZERO_PAGE(0);
287 get_page(zero_page);
288
289 /*
290 * The number of active work items is limited by the number of
291 * connections, so leave @max_active at default.
292 */
293 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_MEM_RECLAIM, 0);
294 if (ceph_msgr_wq)
295 return 0;
296
297 pr_err("msgr_init failed to create workqueue\n");
298 _ceph_msgr_exit();
299
300 return -ENOMEM;
301 }
302 EXPORT_SYMBOL(ceph_msgr_init);
303
304 void ceph_msgr_exit(void)
305 {
306 BUG_ON(ceph_msgr_wq == NULL);
307
308 _ceph_msgr_exit();
309 }
310 EXPORT_SYMBOL(ceph_msgr_exit);
311
312 void ceph_msgr_flush(void)
313 {
314 flush_workqueue(ceph_msgr_wq);
315 }
316 EXPORT_SYMBOL(ceph_msgr_flush);
317
318 /* Connection socket state transition functions */
319
320 static void con_sock_state_init(struct ceph_connection *con)
321 {
322 int old_state;
323
324 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
325 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
326 printk("%s: unexpected old state %d\n", __func__, old_state);
327 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
328 CON_SOCK_STATE_CLOSED);
329 }
330
331 static void con_sock_state_connecting(struct ceph_connection *con)
332 {
333 int old_state;
334
335 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
336 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
337 printk("%s: unexpected old state %d\n", __func__, old_state);
338 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
339 CON_SOCK_STATE_CONNECTING);
340 }
341
342 static void con_sock_state_connected(struct ceph_connection *con)
343 {
344 int old_state;
345
346 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
347 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
348 printk("%s: unexpected old state %d\n", __func__, old_state);
349 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
350 CON_SOCK_STATE_CONNECTED);
351 }
352
353 static void con_sock_state_closing(struct ceph_connection *con)
354 {
355 int old_state;
356
357 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
358 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
359 old_state != CON_SOCK_STATE_CONNECTED &&
360 old_state != CON_SOCK_STATE_CLOSING))
361 printk("%s: unexpected old state %d\n", __func__, old_state);
362 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
363 CON_SOCK_STATE_CLOSING);
364 }
365
366 static void con_sock_state_closed(struct ceph_connection *con)
367 {
368 int old_state;
369
370 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
371 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
372 old_state != CON_SOCK_STATE_CLOSING &&
373 old_state != CON_SOCK_STATE_CONNECTING &&
374 old_state != CON_SOCK_STATE_CLOSED))
375 printk("%s: unexpected old state %d\n", __func__, old_state);
376 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
377 CON_SOCK_STATE_CLOSED);
378 }
379
380 /*
381 * socket callback functions
382 */
383
384 /* data available on socket, or listen socket received a connect */
385 static void ceph_sock_data_ready(struct sock *sk)
386 {
387 struct ceph_connection *con = sk->sk_user_data;
388 if (atomic_read(&con->msgr->stopping)) {
389 return;
390 }
391
392 if (sk->sk_state != TCP_CLOSE_WAIT) {
393 dout("%s on %p state = %lu, queueing work\n", __func__,
394 con, con->state);
395 queue_con(con);
396 }
397 }
398
399 /* socket has buffer space for writing */
400 static void ceph_sock_write_space(struct sock *sk)
401 {
402 struct ceph_connection *con = sk->sk_user_data;
403
404 /* only queue to workqueue if there is data we want to write,
405 * and there is sufficient space in the socket buffer to accept
406 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
407 * doesn't get called again until try_write() fills the socket
408 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
409 * and net/core/stream.c:sk_stream_write_space().
410 */
411 if (con_flag_test(con, CON_FLAG_WRITE_PENDING)) {
412 if (sk_stream_is_writeable(sk)) {
413 dout("%s %p queueing write work\n", __func__, con);
414 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
415 queue_con(con);
416 }
417 } else {
418 dout("%s %p nothing to write\n", __func__, con);
419 }
420 }
421
422 /* socket's state has changed */
423 static void ceph_sock_state_change(struct sock *sk)
424 {
425 struct ceph_connection *con = sk->sk_user_data;
426
427 dout("%s %p state = %lu sk_state = %u\n", __func__,
428 con, con->state, sk->sk_state);
429
430 switch (sk->sk_state) {
431 case TCP_CLOSE:
432 dout("%s TCP_CLOSE\n", __func__);
433 /* fall through */
434 case TCP_CLOSE_WAIT:
435 dout("%s TCP_CLOSE_WAIT\n", __func__);
436 con_sock_state_closing(con);
437 con_flag_set(con, CON_FLAG_SOCK_CLOSED);
438 queue_con(con);
439 break;
440 case TCP_ESTABLISHED:
441 dout("%s TCP_ESTABLISHED\n", __func__);
442 con_sock_state_connected(con);
443 queue_con(con);
444 break;
445 default: /* Everything else is uninteresting */
446 break;
447 }
448 }
449
450 /*
451 * set up socket callbacks
452 */
453 static void set_sock_callbacks(struct socket *sock,
454 struct ceph_connection *con)
455 {
456 struct sock *sk = sock->sk;
457 sk->sk_user_data = con;
458 sk->sk_data_ready = ceph_sock_data_ready;
459 sk->sk_write_space = ceph_sock_write_space;
460 sk->sk_state_change = ceph_sock_state_change;
461 }
462
463
464 /*
465 * socket helpers
466 */
467
468 /*
469 * initiate connection to a remote socket.
470 */
471 static int ceph_tcp_connect(struct ceph_connection *con)
472 {
473 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
474 struct socket *sock;
475 unsigned int noio_flag;
476 int ret;
477
478 BUG_ON(con->sock);
479
480 /* sock_create_kern() allocates with GFP_KERNEL */
481 noio_flag = memalloc_noio_save();
482 ret = sock_create_kern(read_pnet(&con->msgr->net), paddr->ss_family,
483 SOCK_STREAM, IPPROTO_TCP, &sock);
484 memalloc_noio_restore(noio_flag);
485 if (ret)
486 return ret;
487 sock->sk->sk_allocation = GFP_NOFS;
488
489 #ifdef CONFIG_LOCKDEP
490 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
491 #endif
492
493 set_sock_callbacks(sock, con);
494
495 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
496
497 con_sock_state_connecting(con);
498 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
499 O_NONBLOCK);
500 if (ret == -EINPROGRESS) {
501 dout("connect %s EINPROGRESS sk_state = %u\n",
502 ceph_pr_addr(&con->peer_addr.in_addr),
503 sock->sk->sk_state);
504 } else if (ret < 0) {
505 pr_err("connect %s error %d\n",
506 ceph_pr_addr(&con->peer_addr.in_addr), ret);
507 sock_release(sock);
508 return ret;
509 }
510
511 if (ceph_test_opt(from_msgr(con->msgr), TCP_NODELAY)) {
512 int optval = 1;
513
514 ret = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
515 (char *)&optval, sizeof(optval));
516 if (ret)
517 pr_err("kernel_setsockopt(TCP_NODELAY) failed: %d",
518 ret);
519 }
520
521 con->sock = sock;
522 return 0;
523 }
524
525 static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
526 {
527 struct kvec iov = {buf, len};
528 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
529 int r;
530
531 iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, &iov, 1, len);
532 r = sock_recvmsg(sock, &msg, msg.msg_flags);
533 if (r == -EAGAIN)
534 r = 0;
535 return r;
536 }
537
538 static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
539 int page_offset, size_t length)
540 {
541 struct bio_vec bvec = {
542 .bv_page = page,
543 .bv_offset = page_offset,
544 .bv_len = length
545 };
546 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
547 int r;
548
549 BUG_ON(page_offset + length > PAGE_SIZE);
550 iov_iter_bvec(&msg.msg_iter, READ | ITER_BVEC, &bvec, 1, length);
551 r = sock_recvmsg(sock, &msg, msg.msg_flags);
552 if (r == -EAGAIN)
553 r = 0;
554 return r;
555 }
556
557 /*
558 * write something. @more is true if caller will be sending more data
559 * shortly.
560 */
561 static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
562 size_t kvlen, size_t len, int more)
563 {
564 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
565 int r;
566
567 if (more)
568 msg.msg_flags |= MSG_MORE;
569 else
570 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
571
572 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
573 if (r == -EAGAIN)
574 r = 0;
575 return r;
576 }
577
578 static int __ceph_tcp_sendpage(struct socket *sock, struct page *page,
579 int offset, size_t size, bool more)
580 {
581 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
582 int ret;
583
584 ret = kernel_sendpage(sock, page, offset, size, flags);
585 if (ret == -EAGAIN)
586 ret = 0;
587
588 return ret;
589 }
590
591 static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
592 int offset, size_t size, bool more)
593 {
594 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
595 struct bio_vec bvec;
596 int ret;
597
598 /*
599 * sendpage cannot properly handle pages with page_count == 0,
600 * we need to fall back to sendmsg if that's the case.
601 *
602 * Same goes for slab pages: skb_can_coalesce() allows
603 * coalescing neighboring slab objects into a single frag which
604 * triggers one of hardened usercopy checks.
605 */
606 if (page_count(page) >= 1 && !PageSlab(page))
607 return __ceph_tcp_sendpage(sock, page, offset, size, more);
608
609 bvec.bv_page = page;
610 bvec.bv_offset = offset;
611 bvec.bv_len = size;
612
613 if (more)
614 msg.msg_flags |= MSG_MORE;
615 else
616 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
617
618 iov_iter_bvec(&msg.msg_iter, WRITE | ITER_BVEC, &bvec, 1, size);
619 ret = sock_sendmsg(sock, &msg);
620 if (ret == -EAGAIN)
621 ret = 0;
622
623 return ret;
624 }
625
626 /*
627 * Shutdown/close the socket for the given connection.
628 */
629 static int con_close_socket(struct ceph_connection *con)
630 {
631 int rc = 0;
632
633 dout("con_close_socket on %p sock %p\n", con, con->sock);
634 if (con->sock) {
635 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
636 sock_release(con->sock);
637 con->sock = NULL;
638 }
639
640 /*
641 * Forcibly clear the SOCK_CLOSED flag. It gets set
642 * independent of the connection mutex, and we could have
643 * received a socket close event before we had the chance to
644 * shut the socket down.
645 */
646 con_flag_clear(con, CON_FLAG_SOCK_CLOSED);
647
648 con_sock_state_closed(con);
649 return rc;
650 }
651
652 /*
653 * Reset a connection. Discard all incoming and outgoing messages
654 * and clear *_seq state.
655 */
656 static void ceph_msg_remove(struct ceph_msg *msg)
657 {
658 list_del_init(&msg->list_head);
659
660 ceph_msg_put(msg);
661 }
662 static void ceph_msg_remove_list(struct list_head *head)
663 {
664 while (!list_empty(head)) {
665 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
666 list_head);
667 ceph_msg_remove(msg);
668 }
669 }
670
671 static void reset_connection(struct ceph_connection *con)
672 {
673 /* reset connection, out_queue, msg_ and connect_seq */
674 /* discard existing out_queue and msg_seq */
675 dout("reset_connection %p\n", con);
676 ceph_msg_remove_list(&con->out_queue);
677 ceph_msg_remove_list(&con->out_sent);
678
679 if (con->in_msg) {
680 BUG_ON(con->in_msg->con != con);
681 ceph_msg_put(con->in_msg);
682 con->in_msg = NULL;
683 }
684
685 con->connect_seq = 0;
686 con->out_seq = 0;
687 if (con->out_msg) {
688 BUG_ON(con->out_msg->con != con);
689 ceph_msg_put(con->out_msg);
690 con->out_msg = NULL;
691 }
692 con->in_seq = 0;
693 con->in_seq_acked = 0;
694
695 con->out_skip = 0;
696 }
697
698 /*
699 * mark a peer down. drop any open connections.
700 */
701 void ceph_con_close(struct ceph_connection *con)
702 {
703 mutex_lock(&con->mutex);
704 dout("con_close %p peer %s\n", con,
705 ceph_pr_addr(&con->peer_addr.in_addr));
706 con->state = CON_STATE_CLOSED;
707
708 con_flag_clear(con, CON_FLAG_LOSSYTX); /* so we retry next connect */
709 con_flag_clear(con, CON_FLAG_KEEPALIVE_PENDING);
710 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
711 con_flag_clear(con, CON_FLAG_BACKOFF);
712
713 reset_connection(con);
714 con->peer_global_seq = 0;
715 cancel_con(con);
716 con_close_socket(con);
717 mutex_unlock(&con->mutex);
718 }
719 EXPORT_SYMBOL(ceph_con_close);
720
721 /*
722 * Reopen a closed connection, with a new peer address.
723 */
724 void ceph_con_open(struct ceph_connection *con,
725 __u8 entity_type, __u64 entity_num,
726 struct ceph_entity_addr *addr)
727 {
728 mutex_lock(&con->mutex);
729 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
730
731 WARN_ON(con->state != CON_STATE_CLOSED);
732 con->state = CON_STATE_PREOPEN;
733
734 con->peer_name.type = (__u8) entity_type;
735 con->peer_name.num = cpu_to_le64(entity_num);
736
737 memcpy(&con->peer_addr, addr, sizeof(*addr));
738 con->delay = 0; /* reset backoff memory */
739 mutex_unlock(&con->mutex);
740 queue_con(con);
741 }
742 EXPORT_SYMBOL(ceph_con_open);
743
744 /*
745 * return true if this connection ever successfully opened
746 */
747 bool ceph_con_opened(struct ceph_connection *con)
748 {
749 return con->connect_seq > 0;
750 }
751
752 /*
753 * initialize a new connection.
754 */
755 void ceph_con_init(struct ceph_connection *con, void *private,
756 const struct ceph_connection_operations *ops,
757 struct ceph_messenger *msgr)
758 {
759 dout("con_init %p\n", con);
760 memset(con, 0, sizeof(*con));
761 con->private = private;
762 con->ops = ops;
763 con->msgr = msgr;
764
765 con_sock_state_init(con);
766
767 mutex_init(&con->mutex);
768 INIT_LIST_HEAD(&con->out_queue);
769 INIT_LIST_HEAD(&con->out_sent);
770 INIT_DELAYED_WORK(&con->work, ceph_con_workfn);
771
772 con->state = CON_STATE_CLOSED;
773 }
774 EXPORT_SYMBOL(ceph_con_init);
775
776
777 /*
778 * We maintain a global counter to order connection attempts. Get
779 * a unique seq greater than @gt.
780 */
781 static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
782 {
783 u32 ret;
784
785 spin_lock(&msgr->global_seq_lock);
786 if (msgr->global_seq < gt)
787 msgr->global_seq = gt;
788 ret = ++msgr->global_seq;
789 spin_unlock(&msgr->global_seq_lock);
790 return ret;
791 }
792
793 static void con_out_kvec_reset(struct ceph_connection *con)
794 {
795 BUG_ON(con->out_skip);
796
797 con->out_kvec_left = 0;
798 con->out_kvec_bytes = 0;
799 con->out_kvec_cur = &con->out_kvec[0];
800 }
801
802 static void con_out_kvec_add(struct ceph_connection *con,
803 size_t size, void *data)
804 {
805 int index = con->out_kvec_left;
806
807 BUG_ON(con->out_skip);
808 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
809
810 con->out_kvec[index].iov_len = size;
811 con->out_kvec[index].iov_base = data;
812 con->out_kvec_left++;
813 con->out_kvec_bytes += size;
814 }
815
816 /*
817 * Chop off a kvec from the end. Return residual number of bytes for
818 * that kvec, i.e. how many bytes would have been written if the kvec
819 * hadn't been nuked.
820 */
821 static int con_out_kvec_skip(struct ceph_connection *con)
822 {
823 int off = con->out_kvec_cur - con->out_kvec;
824 int skip = 0;
825
826 if (con->out_kvec_bytes > 0) {
827 skip = con->out_kvec[off + con->out_kvec_left - 1].iov_len;
828 BUG_ON(con->out_kvec_bytes < skip);
829 BUG_ON(!con->out_kvec_left);
830 con->out_kvec_bytes -= skip;
831 con->out_kvec_left--;
832 }
833
834 return skip;
835 }
836
837 #ifdef CONFIG_BLOCK
838
839 /*
840 * For a bio data item, a piece is whatever remains of the next
841 * entry in the current bio iovec, or the first entry in the next
842 * bio in the list.
843 */
844 static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor,
845 size_t length)
846 {
847 struct ceph_msg_data *data = cursor->data;
848 struct bio *bio;
849
850 BUG_ON(data->type != CEPH_MSG_DATA_BIO);
851
852 bio = data->bio;
853 BUG_ON(!bio);
854
855 cursor->resid = min(length, data->bio_length);
856 cursor->bio = bio;
857 cursor->bvec_iter = bio->bi_iter;
858 cursor->last_piece =
859 cursor->resid <= bio_iter_len(bio, cursor->bvec_iter);
860 }
861
862 static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor,
863 size_t *page_offset,
864 size_t *length)
865 {
866 struct ceph_msg_data *data = cursor->data;
867 struct bio *bio;
868 struct bio_vec bio_vec;
869
870 BUG_ON(data->type != CEPH_MSG_DATA_BIO);
871
872 bio = cursor->bio;
873 BUG_ON(!bio);
874
875 bio_vec = bio_iter_iovec(bio, cursor->bvec_iter);
876
877 *page_offset = (size_t) bio_vec.bv_offset;
878 BUG_ON(*page_offset >= PAGE_SIZE);
879 if (cursor->last_piece) /* pagelist offset is always 0 */
880 *length = cursor->resid;
881 else
882 *length = (size_t) bio_vec.bv_len;
883 BUG_ON(*length > cursor->resid);
884 BUG_ON(*page_offset + *length > PAGE_SIZE);
885
886 return bio_vec.bv_page;
887 }
888
889 static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
890 size_t bytes)
891 {
892 struct bio *bio;
893 struct bio_vec bio_vec;
894
895 BUG_ON(cursor->data->type != CEPH_MSG_DATA_BIO);
896
897 bio = cursor->bio;
898 BUG_ON(!bio);
899
900 bio_vec = bio_iter_iovec(bio, cursor->bvec_iter);
901
902 /* Advance the cursor offset */
903
904 BUG_ON(cursor->resid < bytes);
905 cursor->resid -= bytes;
906
907 bio_advance_iter(bio, &cursor->bvec_iter, bytes);
908
909 if (bytes < bio_vec.bv_len)
910 return false; /* more bytes to process in this segment */
911
912 /* Move on to the next segment, and possibly the next bio */
913
914 if (!cursor->bvec_iter.bi_size) {
915 bio = bio->bi_next;
916 cursor->bio = bio;
917 if (bio)
918 cursor->bvec_iter = bio->bi_iter;
919 else
920 memset(&cursor->bvec_iter, 0,
921 sizeof(cursor->bvec_iter));
922 }
923
924 if (!cursor->last_piece) {
925 BUG_ON(!cursor->resid);
926 BUG_ON(!bio);
927 /* A short read is OK, so use <= rather than == */
928 if (cursor->resid <= bio_iter_len(bio, cursor->bvec_iter))
929 cursor->last_piece = true;
930 }
931
932 return true;
933 }
934 #endif /* CONFIG_BLOCK */
935
936 /*
937 * For a page array, a piece comes from the first page in the array
938 * that has not already been fully consumed.
939 */
940 static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor,
941 size_t length)
942 {
943 struct ceph_msg_data *data = cursor->data;
944 int page_count;
945
946 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
947
948 BUG_ON(!data->pages);
949 BUG_ON(!data->length);
950
951 cursor->resid = min(length, data->length);
952 page_count = calc_pages_for(data->alignment, (u64)data->length);
953 cursor->page_offset = data->alignment & ~PAGE_MASK;
954 cursor->page_index = 0;
955 BUG_ON(page_count > (int)USHRT_MAX);
956 cursor->page_count = (unsigned short)page_count;
957 BUG_ON(length > SIZE_MAX - cursor->page_offset);
958 cursor->last_piece = cursor->page_offset + cursor->resid <= PAGE_SIZE;
959 }
960
961 static struct page *
962 ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
963 size_t *page_offset, size_t *length)
964 {
965 struct ceph_msg_data *data = cursor->data;
966
967 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
968
969 BUG_ON(cursor->page_index >= cursor->page_count);
970 BUG_ON(cursor->page_offset >= PAGE_SIZE);
971
972 *page_offset = cursor->page_offset;
973 if (cursor->last_piece)
974 *length = cursor->resid;
975 else
976 *length = PAGE_SIZE - *page_offset;
977
978 return data->pages[cursor->page_index];
979 }
980
981 static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
982 size_t bytes)
983 {
984 BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES);
985
986 BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
987
988 /* Advance the cursor page offset */
989
990 cursor->resid -= bytes;
991 cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK;
992 if (!bytes || cursor->page_offset)
993 return false; /* more bytes to process in the current page */
994
995 if (!cursor->resid)
996 return false; /* no more data */
997
998 /* Move on to the next page; offset is already at 0 */
999
1000 BUG_ON(cursor->page_index >= cursor->page_count);
1001 cursor->page_index++;
1002 cursor->last_piece = cursor->resid <= PAGE_SIZE;
1003
1004 return true;
1005 }
1006
1007 /*
1008 * For a pagelist, a piece is whatever remains to be consumed in the
1009 * first page in the list, or the front of the next page.
1010 */
1011 static void
1012 ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
1013 size_t length)
1014 {
1015 struct ceph_msg_data *data = cursor->data;
1016 struct ceph_pagelist *pagelist;
1017 struct page *page;
1018
1019 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1020
1021 pagelist = data->pagelist;
1022 BUG_ON(!pagelist);
1023
1024 if (!length)
1025 return; /* pagelist can be assigned but empty */
1026
1027 BUG_ON(list_empty(&pagelist->head));
1028 page = list_first_entry(&pagelist->head, struct page, lru);
1029
1030 cursor->resid = min(length, pagelist->length);
1031 cursor->page = page;
1032 cursor->offset = 0;
1033 cursor->last_piece = cursor->resid <= PAGE_SIZE;
1034 }
1035
1036 static struct page *
1037 ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
1038 size_t *page_offset, size_t *length)
1039 {
1040 struct ceph_msg_data *data = cursor->data;
1041 struct ceph_pagelist *pagelist;
1042
1043 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1044
1045 pagelist = data->pagelist;
1046 BUG_ON(!pagelist);
1047
1048 BUG_ON(!cursor->page);
1049 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
1050
1051 /* offset of first page in pagelist is always 0 */
1052 *page_offset = cursor->offset & ~PAGE_MASK;
1053 if (cursor->last_piece)
1054 *length = cursor->resid;
1055 else
1056 *length = PAGE_SIZE - *page_offset;
1057
1058 return cursor->page;
1059 }
1060
1061 static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
1062 size_t bytes)
1063 {
1064 struct ceph_msg_data *data = cursor->data;
1065 struct ceph_pagelist *pagelist;
1066
1067 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1068
1069 pagelist = data->pagelist;
1070 BUG_ON(!pagelist);
1071
1072 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
1073 BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
1074
1075 /* Advance the cursor offset */
1076
1077 cursor->resid -= bytes;
1078 cursor->offset += bytes;
1079 /* offset of first page in pagelist is always 0 */
1080 if (!bytes || cursor->offset & ~PAGE_MASK)
1081 return false; /* more bytes to process in the current page */
1082
1083 if (!cursor->resid)
1084 return false; /* no more data */
1085
1086 /* Move on to the next page */
1087
1088 BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
1089 cursor->page = list_next_entry(cursor->page, lru);
1090 cursor->last_piece = cursor->resid <= PAGE_SIZE;
1091
1092 return true;
1093 }
1094
1095 /*
1096 * Message data is handled (sent or received) in pieces, where each
1097 * piece resides on a single page. The network layer might not
1098 * consume an entire piece at once. A data item's cursor keeps
1099 * track of which piece is next to process and how much remains to
1100 * be processed in that piece. It also tracks whether the current
1101 * piece is the last one in the data item.
1102 */
1103 static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor)
1104 {
1105 size_t length = cursor->total_resid;
1106
1107 switch (cursor->data->type) {
1108 case CEPH_MSG_DATA_PAGELIST:
1109 ceph_msg_data_pagelist_cursor_init(cursor, length);
1110 break;
1111 case CEPH_MSG_DATA_PAGES:
1112 ceph_msg_data_pages_cursor_init(cursor, length);
1113 break;
1114 #ifdef CONFIG_BLOCK
1115 case CEPH_MSG_DATA_BIO:
1116 ceph_msg_data_bio_cursor_init(cursor, length);
1117 break;
1118 #endif /* CONFIG_BLOCK */
1119 case CEPH_MSG_DATA_NONE:
1120 default:
1121 /* BUG(); */
1122 break;
1123 }
1124 cursor->need_crc = true;
1125 }
1126
1127 static void ceph_msg_data_cursor_init(struct ceph_msg *msg, size_t length)
1128 {
1129 struct ceph_msg_data_cursor *cursor = &msg->cursor;
1130 struct ceph_msg_data *data;
1131
1132 BUG_ON(!length);
1133 BUG_ON(length > msg->data_length);
1134 BUG_ON(list_empty(&msg->data));
1135
1136 cursor->data_head = &msg->data;
1137 cursor->total_resid = length;
1138 data = list_first_entry(&msg->data, struct ceph_msg_data, links);
1139 cursor->data = data;
1140
1141 __ceph_msg_data_cursor_init(cursor);
1142 }
1143
1144 /*
1145 * Return the page containing the next piece to process for a given
1146 * data item, and supply the page offset and length of that piece.
1147 * Indicate whether this is the last piece in this data item.
1148 */
1149 static struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
1150 size_t *page_offset, size_t *length,
1151 bool *last_piece)
1152 {
1153 struct page *page;
1154
1155 switch (cursor->data->type) {
1156 case CEPH_MSG_DATA_PAGELIST:
1157 page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
1158 break;
1159 case CEPH_MSG_DATA_PAGES:
1160 page = ceph_msg_data_pages_next(cursor, page_offset, length);
1161 break;
1162 #ifdef CONFIG_BLOCK
1163 case CEPH_MSG_DATA_BIO:
1164 page = ceph_msg_data_bio_next(cursor, page_offset, length);
1165 break;
1166 #endif /* CONFIG_BLOCK */
1167 case CEPH_MSG_DATA_NONE:
1168 default:
1169 page = NULL;
1170 break;
1171 }
1172 BUG_ON(!page);
1173 BUG_ON(*page_offset + *length > PAGE_SIZE);
1174 BUG_ON(!*length);
1175 if (last_piece)
1176 *last_piece = cursor->last_piece;
1177
1178 return page;
1179 }
1180
1181 /*
1182 * Returns true if the result moves the cursor on to the next piece
1183 * of the data item.
1184 */
1185 static void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor,
1186 size_t bytes)
1187 {
1188 bool new_piece;
1189
1190 BUG_ON(bytes > cursor->resid);
1191 switch (cursor->data->type) {
1192 case CEPH_MSG_DATA_PAGELIST:
1193 new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
1194 break;
1195 case CEPH_MSG_DATA_PAGES:
1196 new_piece = ceph_msg_data_pages_advance(cursor, bytes);
1197 break;
1198 #ifdef CONFIG_BLOCK
1199 case CEPH_MSG_DATA_BIO:
1200 new_piece = ceph_msg_data_bio_advance(cursor, bytes);
1201 break;
1202 #endif /* CONFIG_BLOCK */
1203 case CEPH_MSG_DATA_NONE:
1204 default:
1205 BUG();
1206 break;
1207 }
1208 cursor->total_resid -= bytes;
1209
1210 if (!cursor->resid && cursor->total_resid) {
1211 WARN_ON(!cursor->last_piece);
1212 BUG_ON(list_is_last(&cursor->data->links, cursor->data_head));
1213 cursor->data = list_next_entry(cursor->data, links);
1214 __ceph_msg_data_cursor_init(cursor);
1215 new_piece = true;
1216 }
1217 cursor->need_crc = new_piece;
1218 }
1219
1220 static size_t sizeof_footer(struct ceph_connection *con)
1221 {
1222 return (con->peer_features & CEPH_FEATURE_MSG_AUTH) ?
1223 sizeof(struct ceph_msg_footer) :
1224 sizeof(struct ceph_msg_footer_old);
1225 }
1226
1227 static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
1228 {
1229 BUG_ON(!msg);
1230 BUG_ON(!data_len);
1231
1232 /* Initialize data cursor */
1233
1234 ceph_msg_data_cursor_init(msg, (size_t)data_len);
1235 }
1236
1237 /*
1238 * Prepare footer for currently outgoing message, and finish things
1239 * off. Assumes out_kvec* are already valid.. we just add on to the end.
1240 */
1241 static void prepare_write_message_footer(struct ceph_connection *con)
1242 {
1243 struct ceph_msg *m = con->out_msg;
1244
1245 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
1246
1247 dout("prepare_write_message_footer %p\n", con);
1248 con_out_kvec_add(con, sizeof_footer(con), &m->footer);
1249 if (con->peer_features & CEPH_FEATURE_MSG_AUTH) {
1250 if (con->ops->sign_message)
1251 con->ops->sign_message(m);
1252 else
1253 m->footer.sig = 0;
1254 } else {
1255 m->old_footer.flags = m->footer.flags;
1256 }
1257 con->out_more = m->more_to_follow;
1258 con->out_msg_done = true;
1259 }
1260
1261 /*
1262 * Prepare headers for the next outgoing message.
1263 */
1264 static void prepare_write_message(struct ceph_connection *con)
1265 {
1266 struct ceph_msg *m;
1267 u32 crc;
1268
1269 con_out_kvec_reset(con);
1270 con->out_msg_done = false;
1271
1272 /* Sneak an ack in there first? If we can get it into the same
1273 * TCP packet that's a good thing. */
1274 if (con->in_seq > con->in_seq_acked) {
1275 con->in_seq_acked = con->in_seq;
1276 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
1277 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1278 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1279 &con->out_temp_ack);
1280 }
1281
1282 BUG_ON(list_empty(&con->out_queue));
1283 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
1284 con->out_msg = m;
1285 BUG_ON(m->con != con);
1286
1287 /* put message on sent list */
1288 ceph_msg_get(m);
1289 list_move_tail(&m->list_head, &con->out_sent);
1290
1291 /*
1292 * only assign outgoing seq # if we haven't sent this message
1293 * yet. if it is requeued, resend with it's original seq.
1294 */
1295 if (m->needs_out_seq) {
1296 m->hdr.seq = cpu_to_le64(++con->out_seq);
1297 m->needs_out_seq = false;
1298
1299 if (con->ops->reencode_message)
1300 con->ops->reencode_message(m);
1301 }
1302
1303 dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
1304 m, con->out_seq, le16_to_cpu(m->hdr.type),
1305 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
1306 m->data_length);
1307 WARN_ON(m->front.iov_len != le32_to_cpu(m->hdr.front_len));
1308 WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
1309
1310 /* tag + hdr + front + middle */
1311 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
1312 con_out_kvec_add(con, sizeof(con->out_hdr), &con->out_hdr);
1313 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
1314
1315 if (m->middle)
1316 con_out_kvec_add(con, m->middle->vec.iov_len,
1317 m->middle->vec.iov_base);
1318
1319 /* fill in hdr crc and finalize hdr */
1320 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
1321 con->out_msg->hdr.crc = cpu_to_le32(crc);
1322 memcpy(&con->out_hdr, &con->out_msg->hdr, sizeof(con->out_hdr));
1323
1324 /* fill in front and middle crc, footer */
1325 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
1326 con->out_msg->footer.front_crc = cpu_to_le32(crc);
1327 if (m->middle) {
1328 crc = crc32c(0, m->middle->vec.iov_base,
1329 m->middle->vec.iov_len);
1330 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
1331 } else
1332 con->out_msg->footer.middle_crc = 0;
1333 dout("%s front_crc %u middle_crc %u\n", __func__,
1334 le32_to_cpu(con->out_msg->footer.front_crc),
1335 le32_to_cpu(con->out_msg->footer.middle_crc));
1336 con->out_msg->footer.flags = 0;
1337
1338 /* is there a data payload? */
1339 con->out_msg->footer.data_crc = 0;
1340 if (m->data_length) {
1341 prepare_message_data(con->out_msg, m->data_length);
1342 con->out_more = 1; /* data + footer will follow */
1343 } else {
1344 /* no, queue up footer too and be done */
1345 prepare_write_message_footer(con);
1346 }
1347
1348 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1349 }
1350
1351 /*
1352 * Prepare an ack.
1353 */
1354 static void prepare_write_ack(struct ceph_connection *con)
1355 {
1356 dout("prepare_write_ack %p %llu -> %llu\n", con,
1357 con->in_seq_acked, con->in_seq);
1358 con->in_seq_acked = con->in_seq;
1359
1360 con_out_kvec_reset(con);
1361
1362 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
1363
1364 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1365 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1366 &con->out_temp_ack);
1367
1368 con->out_more = 1; /* more will follow.. eventually.. */
1369 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1370 }
1371
1372 /*
1373 * Prepare to share the seq during handshake
1374 */
1375 static void prepare_write_seq(struct ceph_connection *con)
1376 {
1377 dout("prepare_write_seq %p %llu -> %llu\n", con,
1378 con->in_seq_acked, con->in_seq);
1379 con->in_seq_acked = con->in_seq;
1380
1381 con_out_kvec_reset(con);
1382
1383 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1384 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1385 &con->out_temp_ack);
1386
1387 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1388 }
1389
1390 /*
1391 * Prepare to write keepalive byte.
1392 */
1393 static void prepare_write_keepalive(struct ceph_connection *con)
1394 {
1395 dout("prepare_write_keepalive %p\n", con);
1396 con_out_kvec_reset(con);
1397 if (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2) {
1398 struct timespec now;
1399
1400 ktime_get_real_ts(&now);
1401 con_out_kvec_add(con, sizeof(tag_keepalive2), &tag_keepalive2);
1402 ceph_encode_timespec(&con->out_temp_keepalive2, &now);
1403 con_out_kvec_add(con, sizeof(con->out_temp_keepalive2),
1404 &con->out_temp_keepalive2);
1405 } else {
1406 con_out_kvec_add(con, sizeof(tag_keepalive), &tag_keepalive);
1407 }
1408 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1409 }
1410
1411 /*
1412 * Connection negotiation.
1413 */
1414
1415 static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
1416 int *auth_proto)
1417 {
1418 struct ceph_auth_handshake *auth;
1419
1420 if (!con->ops->get_authorizer) {
1421 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
1422 con->out_connect.authorizer_len = 0;
1423 return NULL;
1424 }
1425
1426 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
1427 if (IS_ERR(auth))
1428 return auth;
1429
1430 con->auth_reply_buf = auth->authorizer_reply_buf;
1431 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
1432 return auth;
1433 }
1434
1435 /*
1436 * We connected to a peer and are saying hello.
1437 */
1438 static void prepare_write_banner(struct ceph_connection *con)
1439 {
1440 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
1441 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
1442 &con->msgr->my_enc_addr);
1443
1444 con->out_more = 0;
1445 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1446 }
1447
1448 static int prepare_write_connect(struct ceph_connection *con)
1449 {
1450 unsigned int global_seq = get_global_seq(con->msgr, 0);
1451 int proto;
1452 int auth_proto;
1453 struct ceph_auth_handshake *auth;
1454
1455 switch (con->peer_name.type) {
1456 case CEPH_ENTITY_TYPE_MON:
1457 proto = CEPH_MONC_PROTOCOL;
1458 break;
1459 case CEPH_ENTITY_TYPE_OSD:
1460 proto = CEPH_OSDC_PROTOCOL;
1461 break;
1462 case CEPH_ENTITY_TYPE_MDS:
1463 proto = CEPH_MDSC_PROTOCOL;
1464 break;
1465 default:
1466 BUG();
1467 }
1468
1469 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
1470 con->connect_seq, global_seq, proto);
1471
1472 con->out_connect.features =
1473 cpu_to_le64(from_msgr(con->msgr)->supported_features);
1474 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
1475 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
1476 con->out_connect.global_seq = cpu_to_le32(global_seq);
1477 con->out_connect.protocol_version = cpu_to_le32(proto);
1478 con->out_connect.flags = 0;
1479
1480 auth_proto = CEPH_AUTH_UNKNOWN;
1481 auth = get_connect_authorizer(con, &auth_proto);
1482 if (IS_ERR(auth))
1483 return PTR_ERR(auth);
1484
1485 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
1486 con->out_connect.authorizer_len = auth ?
1487 cpu_to_le32(auth->authorizer_buf_len) : 0;
1488
1489 con_out_kvec_add(con, sizeof (con->out_connect),
1490 &con->out_connect);
1491 if (auth && auth->authorizer_buf_len)
1492 con_out_kvec_add(con, auth->authorizer_buf_len,
1493 auth->authorizer_buf);
1494
1495 con->out_more = 0;
1496 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1497
1498 return 0;
1499 }
1500
1501 /*
1502 * write as much of pending kvecs to the socket as we can.
1503 * 1 -> done
1504 * 0 -> socket full, but more to do
1505 * <0 -> error
1506 */
1507 static int write_partial_kvec(struct ceph_connection *con)
1508 {
1509 int ret;
1510
1511 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
1512 while (con->out_kvec_bytes > 0) {
1513 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
1514 con->out_kvec_left, con->out_kvec_bytes,
1515 con->out_more);
1516 if (ret <= 0)
1517 goto out;
1518 con->out_kvec_bytes -= ret;
1519 if (con->out_kvec_bytes == 0)
1520 break; /* done */
1521
1522 /* account for full iov entries consumed */
1523 while (ret >= con->out_kvec_cur->iov_len) {
1524 BUG_ON(!con->out_kvec_left);
1525 ret -= con->out_kvec_cur->iov_len;
1526 con->out_kvec_cur++;
1527 con->out_kvec_left--;
1528 }
1529 /* and for a partially-consumed entry */
1530 if (ret) {
1531 con->out_kvec_cur->iov_len -= ret;
1532 con->out_kvec_cur->iov_base += ret;
1533 }
1534 }
1535 con->out_kvec_left = 0;
1536 ret = 1;
1537 out:
1538 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
1539 con->out_kvec_bytes, con->out_kvec_left, ret);
1540 return ret; /* done! */
1541 }
1542
1543 static u32 ceph_crc32c_page(u32 crc, struct page *page,
1544 unsigned int page_offset,
1545 unsigned int length)
1546 {
1547 char *kaddr;
1548
1549 kaddr = kmap(page);
1550 BUG_ON(kaddr == NULL);
1551 crc = crc32c(crc, kaddr + page_offset, length);
1552 kunmap(page);
1553
1554 return crc;
1555 }
1556 /*
1557 * Write as much message data payload as we can. If we finish, queue
1558 * up the footer.
1559 * 1 -> done, footer is now queued in out_kvec[].
1560 * 0 -> socket full, but more to do
1561 * <0 -> error
1562 */
1563 static int write_partial_message_data(struct ceph_connection *con)
1564 {
1565 struct ceph_msg *msg = con->out_msg;
1566 struct ceph_msg_data_cursor *cursor = &msg->cursor;
1567 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
1568 u32 crc;
1569
1570 dout("%s %p msg %p\n", __func__, con, msg);
1571
1572 if (list_empty(&msg->data))
1573 return -EINVAL;
1574
1575 /*
1576 * Iterate through each page that contains data to be
1577 * written, and send as much as possible for each.
1578 *
1579 * If we are calculating the data crc (the default), we will
1580 * need to map the page. If we have no pages, they have
1581 * been revoked, so use the zero page.
1582 */
1583 crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
1584 while (cursor->resid) {
1585 struct page *page;
1586 size_t page_offset;
1587 size_t length;
1588 bool last_piece;
1589 int ret;
1590
1591 page = ceph_msg_data_next(cursor, &page_offset, &length,
1592 &last_piece);
1593 ret = ceph_tcp_sendpage(con->sock, page, page_offset,
1594 length, !last_piece);
1595 if (ret <= 0) {
1596 if (do_datacrc)
1597 msg->footer.data_crc = cpu_to_le32(crc);
1598
1599 return ret;
1600 }
1601 if (do_datacrc && cursor->need_crc)
1602 crc = ceph_crc32c_page(crc, page, page_offset, length);
1603 ceph_msg_data_advance(cursor, (size_t)ret);
1604 }
1605
1606 dout("%s %p msg %p done\n", __func__, con, msg);
1607
1608 /* prepare and queue up footer, too */
1609 if (do_datacrc)
1610 msg->footer.data_crc = cpu_to_le32(crc);
1611 else
1612 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
1613 con_out_kvec_reset(con);
1614 prepare_write_message_footer(con);
1615
1616 return 1; /* must return > 0 to indicate success */
1617 }
1618
1619 /*
1620 * write some zeros
1621 */
1622 static int write_partial_skip(struct ceph_connection *con)
1623 {
1624 int ret;
1625
1626 dout("%s %p %d left\n", __func__, con, con->out_skip);
1627 while (con->out_skip > 0) {
1628 size_t size = min(con->out_skip, (int) PAGE_SIZE);
1629
1630 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
1631 if (ret <= 0)
1632 goto out;
1633 con->out_skip -= ret;
1634 }
1635 ret = 1;
1636 out:
1637 return ret;
1638 }
1639
1640 /*
1641 * Prepare to read connection handshake, or an ack.
1642 */
1643 static void prepare_read_banner(struct ceph_connection *con)
1644 {
1645 dout("prepare_read_banner %p\n", con);
1646 con->in_base_pos = 0;
1647 }
1648
1649 static void prepare_read_connect(struct ceph_connection *con)
1650 {
1651 dout("prepare_read_connect %p\n", con);
1652 con->in_base_pos = 0;
1653 }
1654
1655 static void prepare_read_ack(struct ceph_connection *con)
1656 {
1657 dout("prepare_read_ack %p\n", con);
1658 con->in_base_pos = 0;
1659 }
1660
1661 static void prepare_read_seq(struct ceph_connection *con)
1662 {
1663 dout("prepare_read_seq %p\n", con);
1664 con->in_base_pos = 0;
1665 con->in_tag = CEPH_MSGR_TAG_SEQ;
1666 }
1667
1668 static void prepare_read_tag(struct ceph_connection *con)
1669 {
1670 dout("prepare_read_tag %p\n", con);
1671 con->in_base_pos = 0;
1672 con->in_tag = CEPH_MSGR_TAG_READY;
1673 }
1674
1675 static void prepare_read_keepalive_ack(struct ceph_connection *con)
1676 {
1677 dout("prepare_read_keepalive_ack %p\n", con);
1678 con->in_base_pos = 0;
1679 }
1680
1681 /*
1682 * Prepare to read a message.
1683 */
1684 static int prepare_read_message(struct ceph_connection *con)
1685 {
1686 dout("prepare_read_message %p\n", con);
1687 BUG_ON(con->in_msg != NULL);
1688 con->in_base_pos = 0;
1689 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1690 return 0;
1691 }
1692
1693
1694 static int read_partial(struct ceph_connection *con,
1695 int end, int size, void *object)
1696 {
1697 while (con->in_base_pos < end) {
1698 int left = end - con->in_base_pos;
1699 int have = size - left;
1700 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1701 if (ret <= 0)
1702 return ret;
1703 con->in_base_pos += ret;
1704 }
1705 return 1;
1706 }
1707
1708
1709 /*
1710 * Read all or part of the connect-side handshake on a new connection
1711 */
1712 static int read_partial_banner(struct ceph_connection *con)
1713 {
1714 int size;
1715 int end;
1716 int ret;
1717
1718 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
1719
1720 /* peer's banner */
1721 size = strlen(CEPH_BANNER);
1722 end = size;
1723 ret = read_partial(con, end, size, con->in_banner);
1724 if (ret <= 0)
1725 goto out;
1726
1727 size = sizeof (con->actual_peer_addr);
1728 end += size;
1729 ret = read_partial(con, end, size, &con->actual_peer_addr);
1730 if (ret <= 0)
1731 goto out;
1732
1733 size = sizeof (con->peer_addr_for_me);
1734 end += size;
1735 ret = read_partial(con, end, size, &con->peer_addr_for_me);
1736 if (ret <= 0)
1737 goto out;
1738
1739 out:
1740 return ret;
1741 }
1742
1743 static int read_partial_connect(struct ceph_connection *con)
1744 {
1745 int size;
1746 int end;
1747 int ret;
1748
1749 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1750
1751 size = sizeof (con->in_reply);
1752 end = size;
1753 ret = read_partial(con, end, size, &con->in_reply);
1754 if (ret <= 0)
1755 goto out;
1756
1757 size = le32_to_cpu(con->in_reply.authorizer_len);
1758 end += size;
1759 ret = read_partial(con, end, size, con->auth_reply_buf);
1760 if (ret <= 0)
1761 goto out;
1762
1763 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1764 con, (int)con->in_reply.tag,
1765 le32_to_cpu(con->in_reply.connect_seq),
1766 le32_to_cpu(con->in_reply.global_seq));
1767 out:
1768 return ret;
1769
1770 }
1771
1772 /*
1773 * Verify the hello banner looks okay.
1774 */
1775 static int verify_hello(struct ceph_connection *con)
1776 {
1777 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
1778 pr_err("connect to %s got bad banner\n",
1779 ceph_pr_addr(&con->peer_addr.in_addr));
1780 con->error_msg = "protocol error, bad banner";
1781 return -1;
1782 }
1783 return 0;
1784 }
1785
1786 static bool addr_is_blank(struct sockaddr_storage *ss)
1787 {
1788 struct in_addr *addr = &((struct sockaddr_in *)ss)->sin_addr;
1789 struct in6_addr *addr6 = &((struct sockaddr_in6 *)ss)->sin6_addr;
1790
1791 switch (ss->ss_family) {
1792 case AF_INET:
1793 return addr->s_addr == htonl(INADDR_ANY);
1794 case AF_INET6:
1795 return ipv6_addr_any(addr6);
1796 default:
1797 return true;
1798 }
1799 }
1800
1801 static int addr_port(struct sockaddr_storage *ss)
1802 {
1803 switch (ss->ss_family) {
1804 case AF_INET:
1805 return ntohs(((struct sockaddr_in *)ss)->sin_port);
1806 case AF_INET6:
1807 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
1808 }
1809 return 0;
1810 }
1811
1812 static void addr_set_port(struct sockaddr_storage *ss, int p)
1813 {
1814 switch (ss->ss_family) {
1815 case AF_INET:
1816 ((struct sockaddr_in *)ss)->sin_port = htons(p);
1817 break;
1818 case AF_INET6:
1819 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
1820 break;
1821 }
1822 }
1823
1824 /*
1825 * Unlike other *_pton function semantics, zero indicates success.
1826 */
1827 static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1828 char delim, const char **ipend)
1829 {
1830 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1831 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
1832
1833 memset(ss, 0, sizeof(*ss));
1834
1835 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1836 ss->ss_family = AF_INET;
1837 return 0;
1838 }
1839
1840 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1841 ss->ss_family = AF_INET6;
1842 return 0;
1843 }
1844
1845 return -EINVAL;
1846 }
1847
1848 /*
1849 * Extract hostname string and resolve using kernel DNS facility.
1850 */
1851 #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1852 static int ceph_dns_resolve_name(const char *name, size_t namelen,
1853 struct sockaddr_storage *ss, char delim, const char **ipend)
1854 {
1855 const char *end, *delim_p;
1856 char *colon_p, *ip_addr = NULL;
1857 int ip_len, ret;
1858
1859 /*
1860 * The end of the hostname occurs immediately preceding the delimiter or
1861 * the port marker (':') where the delimiter takes precedence.
1862 */
1863 delim_p = memchr(name, delim, namelen);
1864 colon_p = memchr(name, ':', namelen);
1865
1866 if (delim_p && colon_p)
1867 end = delim_p < colon_p ? delim_p : colon_p;
1868 else if (!delim_p && colon_p)
1869 end = colon_p;
1870 else {
1871 end = delim_p;
1872 if (!end) /* case: hostname:/ */
1873 end = name + namelen;
1874 }
1875
1876 if (end <= name)
1877 return -EINVAL;
1878
1879 /* do dns_resolve upcall */
1880 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1881 if (ip_len > 0)
1882 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1883 else
1884 ret = -ESRCH;
1885
1886 kfree(ip_addr);
1887
1888 *ipend = end;
1889
1890 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1891 ret, ret ? "failed" : ceph_pr_addr(ss));
1892
1893 return ret;
1894 }
1895 #else
1896 static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1897 struct sockaddr_storage *ss, char delim, const char **ipend)
1898 {
1899 return -EINVAL;
1900 }
1901 #endif
1902
1903 /*
1904 * Parse a server name (IP or hostname). If a valid IP address is not found
1905 * then try to extract a hostname to resolve using userspace DNS upcall.
1906 */
1907 static int ceph_parse_server_name(const char *name, size_t namelen,
1908 struct sockaddr_storage *ss, char delim, const char **ipend)
1909 {
1910 int ret;
1911
1912 ret = ceph_pton(name, namelen, ss, delim, ipend);
1913 if (ret)
1914 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1915
1916 return ret;
1917 }
1918
1919 /*
1920 * Parse an ip[:port] list into an addr array. Use the default
1921 * monitor port if a port isn't specified.
1922 */
1923 int ceph_parse_ips(const char *c, const char *end,
1924 struct ceph_entity_addr *addr,
1925 int max_count, int *count)
1926 {
1927 int i, ret = -EINVAL;
1928 const char *p = c;
1929
1930 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1931 for (i = 0; i < max_count; i++) {
1932 const char *ipend;
1933 struct sockaddr_storage *ss = &addr[i].in_addr;
1934 int port;
1935 char delim = ',';
1936
1937 if (*p == '[') {
1938 delim = ']';
1939 p++;
1940 }
1941
1942 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1943 if (ret)
1944 goto bad;
1945 ret = -EINVAL;
1946
1947 p = ipend;
1948
1949 if (delim == ']') {
1950 if (*p != ']') {
1951 dout("missing matching ']'\n");
1952 goto bad;
1953 }
1954 p++;
1955 }
1956
1957 /* port? */
1958 if (p < end && *p == ':') {
1959 port = 0;
1960 p++;
1961 while (p < end && *p >= '0' && *p <= '9') {
1962 port = (port * 10) + (*p - '0');
1963 p++;
1964 }
1965 if (port == 0)
1966 port = CEPH_MON_PORT;
1967 else if (port > 65535)
1968 goto bad;
1969 } else {
1970 port = CEPH_MON_PORT;
1971 }
1972
1973 addr_set_port(ss, port);
1974
1975 dout("parse_ips got %s\n", ceph_pr_addr(ss));
1976
1977 if (p == end)
1978 break;
1979 if (*p != ',')
1980 goto bad;
1981 p++;
1982 }
1983
1984 if (p != end)
1985 goto bad;
1986
1987 if (count)
1988 *count = i + 1;
1989 return 0;
1990
1991 bad:
1992 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
1993 return ret;
1994 }
1995 EXPORT_SYMBOL(ceph_parse_ips);
1996
1997 static int process_banner(struct ceph_connection *con)
1998 {
1999 dout("process_banner on %p\n", con);
2000
2001 if (verify_hello(con) < 0)
2002 return -1;
2003
2004 ceph_decode_addr(&con->actual_peer_addr);
2005 ceph_decode_addr(&con->peer_addr_for_me);
2006
2007 /*
2008 * Make sure the other end is who we wanted. note that the other
2009 * end may not yet know their ip address, so if it's 0.0.0.0, give
2010 * them the benefit of the doubt.
2011 */
2012 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
2013 sizeof(con->peer_addr)) != 0 &&
2014 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
2015 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
2016 pr_warn("wrong peer, want %s/%d, got %s/%d\n",
2017 ceph_pr_addr(&con->peer_addr.in_addr),
2018 (int)le32_to_cpu(con->peer_addr.nonce),
2019 ceph_pr_addr(&con->actual_peer_addr.in_addr),
2020 (int)le32_to_cpu(con->actual_peer_addr.nonce));
2021 con->error_msg = "wrong peer at address";
2022 return -1;
2023 }
2024
2025 /*
2026 * did we learn our address?
2027 */
2028 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
2029 int port = addr_port(&con->msgr->inst.addr.in_addr);
2030
2031 memcpy(&con->msgr->inst.addr.in_addr,
2032 &con->peer_addr_for_me.in_addr,
2033 sizeof(con->peer_addr_for_me.in_addr));
2034 addr_set_port(&con->msgr->inst.addr.in_addr, port);
2035 encode_my_addr(con->msgr);
2036 dout("process_banner learned my addr is %s\n",
2037 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
2038 }
2039
2040 return 0;
2041 }
2042
2043 static int process_connect(struct ceph_connection *con)
2044 {
2045 u64 sup_feat = from_msgr(con->msgr)->supported_features;
2046 u64 req_feat = from_msgr(con->msgr)->required_features;
2047 u64 server_feat = le64_to_cpu(con->in_reply.features);
2048 int ret;
2049
2050 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
2051
2052 if (con->auth_reply_buf) {
2053 /*
2054 * Any connection that defines ->get_authorizer()
2055 * should also define ->verify_authorizer_reply().
2056 * See get_connect_authorizer().
2057 */
2058 ret = con->ops->verify_authorizer_reply(con);
2059 if (ret < 0) {
2060 con->error_msg = "bad authorize reply";
2061 return ret;
2062 }
2063 }
2064
2065 switch (con->in_reply.tag) {
2066 case CEPH_MSGR_TAG_FEATURES:
2067 pr_err("%s%lld %s feature set mismatch,"
2068 " my %llx < server's %llx, missing %llx\n",
2069 ENTITY_NAME(con->peer_name),
2070 ceph_pr_addr(&con->peer_addr.in_addr),
2071 sup_feat, server_feat, server_feat & ~sup_feat);
2072 con->error_msg = "missing required protocol features";
2073 reset_connection(con);
2074 return -1;
2075
2076 case CEPH_MSGR_TAG_BADPROTOVER:
2077 pr_err("%s%lld %s protocol version mismatch,"
2078 " my %d != server's %d\n",
2079 ENTITY_NAME(con->peer_name),
2080 ceph_pr_addr(&con->peer_addr.in_addr),
2081 le32_to_cpu(con->out_connect.protocol_version),
2082 le32_to_cpu(con->in_reply.protocol_version));
2083 con->error_msg = "protocol version mismatch";
2084 reset_connection(con);
2085 return -1;
2086
2087 case CEPH_MSGR_TAG_BADAUTHORIZER:
2088 con->auth_retry++;
2089 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
2090 con->auth_retry);
2091 if (con->auth_retry == 2) {
2092 con->error_msg = "connect authorization failure";
2093 return -1;
2094 }
2095 con_out_kvec_reset(con);
2096 ret = prepare_write_connect(con);
2097 if (ret < 0)
2098 return ret;
2099 prepare_read_connect(con);
2100 break;
2101
2102 case CEPH_MSGR_TAG_RESETSESSION:
2103 /*
2104 * If we connected with a large connect_seq but the peer
2105 * has no record of a session with us (no connection, or
2106 * connect_seq == 0), they will send RESETSESION to indicate
2107 * that they must have reset their session, and may have
2108 * dropped messages.
2109 */
2110 dout("process_connect got RESET peer seq %u\n",
2111 le32_to_cpu(con->in_reply.connect_seq));
2112 pr_err("%s%lld %s connection reset\n",
2113 ENTITY_NAME(con->peer_name),
2114 ceph_pr_addr(&con->peer_addr.in_addr));
2115 reset_connection(con);
2116 con_out_kvec_reset(con);
2117 ret = prepare_write_connect(con);
2118 if (ret < 0)
2119 return ret;
2120 prepare_read_connect(con);
2121
2122 /* Tell ceph about it. */
2123 mutex_unlock(&con->mutex);
2124 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
2125 if (con->ops->peer_reset)
2126 con->ops->peer_reset(con);
2127 mutex_lock(&con->mutex);
2128 if (con->state != CON_STATE_NEGOTIATING)
2129 return -EAGAIN;
2130 break;
2131
2132 case CEPH_MSGR_TAG_RETRY_SESSION:
2133 /*
2134 * If we sent a smaller connect_seq than the peer has, try
2135 * again with a larger value.
2136 */
2137 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
2138 le32_to_cpu(con->out_connect.connect_seq),
2139 le32_to_cpu(con->in_reply.connect_seq));
2140 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
2141 con_out_kvec_reset(con);
2142 ret = prepare_write_connect(con);
2143 if (ret < 0)
2144 return ret;
2145 prepare_read_connect(con);
2146 break;
2147
2148 case CEPH_MSGR_TAG_RETRY_GLOBAL:
2149 /*
2150 * If we sent a smaller global_seq than the peer has, try
2151 * again with a larger value.
2152 */
2153 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
2154 con->peer_global_seq,
2155 le32_to_cpu(con->in_reply.global_seq));
2156 get_global_seq(con->msgr,
2157 le32_to_cpu(con->in_reply.global_seq));
2158 con_out_kvec_reset(con);
2159 ret = prepare_write_connect(con);
2160 if (ret < 0)
2161 return ret;
2162 prepare_read_connect(con);
2163 break;
2164
2165 case CEPH_MSGR_TAG_SEQ:
2166 case CEPH_MSGR_TAG_READY:
2167 if (req_feat & ~server_feat) {
2168 pr_err("%s%lld %s protocol feature mismatch,"
2169 " my required %llx > server's %llx, need %llx\n",
2170 ENTITY_NAME(con->peer_name),
2171 ceph_pr_addr(&con->peer_addr.in_addr),
2172 req_feat, server_feat, req_feat & ~server_feat);
2173 con->error_msg = "missing required protocol features";
2174 reset_connection(con);
2175 return -1;
2176 }
2177
2178 WARN_ON(con->state != CON_STATE_NEGOTIATING);
2179 con->state = CON_STATE_OPEN;
2180 con->auth_retry = 0; /* we authenticated; clear flag */
2181 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
2182 con->connect_seq++;
2183 con->peer_features = server_feat;
2184 dout("process_connect got READY gseq %d cseq %d (%d)\n",
2185 con->peer_global_seq,
2186 le32_to_cpu(con->in_reply.connect_seq),
2187 con->connect_seq);
2188 WARN_ON(con->connect_seq !=
2189 le32_to_cpu(con->in_reply.connect_seq));
2190
2191 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
2192 con_flag_set(con, CON_FLAG_LOSSYTX);
2193
2194 con->delay = 0; /* reset backoff memory */
2195
2196 if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
2197 prepare_write_seq(con);
2198 prepare_read_seq(con);
2199 } else {
2200 prepare_read_tag(con);
2201 }
2202 break;
2203
2204 case CEPH_MSGR_TAG_WAIT:
2205 /*
2206 * If there is a connection race (we are opening
2207 * connections to each other), one of us may just have
2208 * to WAIT. This shouldn't happen if we are the
2209 * client.
2210 */
2211 con->error_msg = "protocol error, got WAIT as client";
2212 return -1;
2213
2214 default:
2215 con->error_msg = "protocol error, garbage tag during connect";
2216 return -1;
2217 }
2218 return 0;
2219 }
2220
2221
2222 /*
2223 * read (part of) an ack
2224 */
2225 static int read_partial_ack(struct ceph_connection *con)
2226 {
2227 int size = sizeof (con->in_temp_ack);
2228 int end = size;
2229
2230 return read_partial(con, end, size, &con->in_temp_ack);
2231 }
2232
2233 /*
2234 * We can finally discard anything that's been acked.
2235 */
2236 static void process_ack(struct ceph_connection *con)
2237 {
2238 struct ceph_msg *m;
2239 u64 ack = le64_to_cpu(con->in_temp_ack);
2240 u64 seq;
2241 bool reconnect = (con->in_tag == CEPH_MSGR_TAG_SEQ);
2242 struct list_head *list = reconnect ? &con->out_queue : &con->out_sent;
2243
2244 /*
2245 * In the reconnect case, con_fault() has requeued messages
2246 * in out_sent. We should cleanup old messages according to
2247 * the reconnect seq.
2248 */
2249 while (!list_empty(list)) {
2250 m = list_first_entry(list, struct ceph_msg, list_head);
2251 if (reconnect && m->needs_out_seq)
2252 break;
2253 seq = le64_to_cpu(m->hdr.seq);
2254 if (seq > ack)
2255 break;
2256 dout("got ack for seq %llu type %d at %p\n", seq,
2257 le16_to_cpu(m->hdr.type), m);
2258 m->ack_stamp = jiffies;
2259 ceph_msg_remove(m);
2260 }
2261
2262 prepare_read_tag(con);
2263 }
2264
2265
2266 static int read_partial_message_section(struct ceph_connection *con,
2267 struct kvec *section,
2268 unsigned int sec_len, u32 *crc)
2269 {
2270 int ret, left;
2271
2272 BUG_ON(!section);
2273
2274 while (section->iov_len < sec_len) {
2275 BUG_ON(section->iov_base == NULL);
2276 left = sec_len - section->iov_len;
2277 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
2278 section->iov_len, left);
2279 if (ret <= 0)
2280 return ret;
2281 section->iov_len += ret;
2282 }
2283 if (section->iov_len == sec_len)
2284 *crc = crc32c(0, section->iov_base, section->iov_len);
2285
2286 return 1;
2287 }
2288
2289 static int read_partial_msg_data(struct ceph_connection *con)
2290 {
2291 struct ceph_msg *msg = con->in_msg;
2292 struct ceph_msg_data_cursor *cursor = &msg->cursor;
2293 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
2294 struct page *page;
2295 size_t page_offset;
2296 size_t length;
2297 u32 crc = 0;
2298 int ret;
2299
2300 BUG_ON(!msg);
2301 if (list_empty(&msg->data))
2302 return -EIO;
2303
2304 if (do_datacrc)
2305 crc = con->in_data_crc;
2306 while (cursor->resid) {
2307 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
2308 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
2309 if (ret <= 0) {
2310 if (do_datacrc)
2311 con->in_data_crc = crc;
2312
2313 return ret;
2314 }
2315
2316 if (do_datacrc)
2317 crc = ceph_crc32c_page(crc, page, page_offset, ret);
2318 ceph_msg_data_advance(cursor, (size_t)ret);
2319 }
2320 if (do_datacrc)
2321 con->in_data_crc = crc;
2322
2323 return 1; /* must return > 0 to indicate success */
2324 }
2325
2326 /*
2327 * read (part of) a message.
2328 */
2329 static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
2330
2331 static int read_partial_message(struct ceph_connection *con)
2332 {
2333 struct ceph_msg *m = con->in_msg;
2334 int size;
2335 int end;
2336 int ret;
2337 unsigned int front_len, middle_len, data_len;
2338 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
2339 bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
2340 u64 seq;
2341 u32 crc;
2342
2343 dout("read_partial_message con %p msg %p\n", con, m);
2344
2345 /* header */
2346 size = sizeof (con->in_hdr);
2347 end = size;
2348 ret = read_partial(con, end, size, &con->in_hdr);
2349 if (ret <= 0)
2350 return ret;
2351
2352 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
2353 if (cpu_to_le32(crc) != con->in_hdr.crc) {
2354 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
2355 crc, con->in_hdr.crc);
2356 return -EBADMSG;
2357 }
2358
2359 front_len = le32_to_cpu(con->in_hdr.front_len);
2360 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
2361 return -EIO;
2362 middle_len = le32_to_cpu(con->in_hdr.middle_len);
2363 if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
2364 return -EIO;
2365 data_len = le32_to_cpu(con->in_hdr.data_len);
2366 if (data_len > CEPH_MSG_MAX_DATA_LEN)
2367 return -EIO;
2368
2369 /* verify seq# */
2370 seq = le64_to_cpu(con->in_hdr.seq);
2371 if ((s64)seq - (s64)con->in_seq < 1) {
2372 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
2373 ENTITY_NAME(con->peer_name),
2374 ceph_pr_addr(&con->peer_addr.in_addr),
2375 seq, con->in_seq + 1);
2376 con->in_base_pos = -front_len - middle_len - data_len -
2377 sizeof_footer(con);
2378 con->in_tag = CEPH_MSGR_TAG_READY;
2379 return 1;
2380 } else if ((s64)seq - (s64)con->in_seq > 1) {
2381 pr_err("read_partial_message bad seq %lld expected %lld\n",
2382 seq, con->in_seq + 1);
2383 con->error_msg = "bad message sequence # for incoming message";
2384 return -EBADE;
2385 }
2386
2387 /* allocate message? */
2388 if (!con->in_msg) {
2389 int skip = 0;
2390
2391 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
2392 front_len, data_len);
2393 ret = ceph_con_in_msg_alloc(con, &skip);
2394 if (ret < 0)
2395 return ret;
2396
2397 BUG_ON(!con->in_msg ^ skip);
2398 if (skip) {
2399 /* skip this message */
2400 dout("alloc_msg said skip message\n");
2401 con->in_base_pos = -front_len - middle_len - data_len -
2402 sizeof_footer(con);
2403 con->in_tag = CEPH_MSGR_TAG_READY;
2404 con->in_seq++;
2405 return 1;
2406 }
2407
2408 BUG_ON(!con->in_msg);
2409 BUG_ON(con->in_msg->con != con);
2410 m = con->in_msg;
2411 m->front.iov_len = 0; /* haven't read it yet */
2412 if (m->middle)
2413 m->middle->vec.iov_len = 0;
2414
2415 /* prepare for data payload, if any */
2416
2417 if (data_len)
2418 prepare_message_data(con->in_msg, data_len);
2419 }
2420
2421 /* front */
2422 ret = read_partial_message_section(con, &m->front, front_len,
2423 &con->in_front_crc);
2424 if (ret <= 0)
2425 return ret;
2426
2427 /* middle */
2428 if (m->middle) {
2429 ret = read_partial_message_section(con, &m->middle->vec,
2430 middle_len,
2431 &con->in_middle_crc);
2432 if (ret <= 0)
2433 return ret;
2434 }
2435
2436 /* (page) data */
2437 if (data_len) {
2438 ret = read_partial_msg_data(con);
2439 if (ret <= 0)
2440 return ret;
2441 }
2442
2443 /* footer */
2444 size = sizeof_footer(con);
2445 end += size;
2446 ret = read_partial(con, end, size, &m->footer);
2447 if (ret <= 0)
2448 return ret;
2449
2450 if (!need_sign) {
2451 m->footer.flags = m->old_footer.flags;
2452 m->footer.sig = 0;
2453 }
2454
2455 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
2456 m, front_len, m->footer.front_crc, middle_len,
2457 m->footer.middle_crc, data_len, m->footer.data_crc);
2458
2459 /* crc ok? */
2460 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
2461 pr_err("read_partial_message %p front crc %u != exp. %u\n",
2462 m, con->in_front_crc, m->footer.front_crc);
2463 return -EBADMSG;
2464 }
2465 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
2466 pr_err("read_partial_message %p middle crc %u != exp %u\n",
2467 m, con->in_middle_crc, m->footer.middle_crc);
2468 return -EBADMSG;
2469 }
2470 if (do_datacrc &&
2471 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
2472 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
2473 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
2474 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
2475 return -EBADMSG;
2476 }
2477
2478 if (need_sign && con->ops->check_message_signature &&
2479 con->ops->check_message_signature(m)) {
2480 pr_err("read_partial_message %p signature check failed\n", m);
2481 return -EBADMSG;
2482 }
2483
2484 return 1; /* done! */
2485 }
2486
2487 /*
2488 * Process message. This happens in the worker thread. The callback should
2489 * be careful not to do anything that waits on other incoming messages or it
2490 * may deadlock.
2491 */
2492 static void process_message(struct ceph_connection *con)
2493 {
2494 struct ceph_msg *msg = con->in_msg;
2495
2496 BUG_ON(con->in_msg->con != con);
2497 con->in_msg = NULL;
2498
2499 /* if first message, set peer_name */
2500 if (con->peer_name.type == 0)
2501 con->peer_name = msg->hdr.src;
2502
2503 con->in_seq++;
2504 mutex_unlock(&con->mutex);
2505
2506 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
2507 msg, le64_to_cpu(msg->hdr.seq),
2508 ENTITY_NAME(msg->hdr.src),
2509 le16_to_cpu(msg->hdr.type),
2510 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2511 le32_to_cpu(msg->hdr.front_len),
2512 le32_to_cpu(msg->hdr.data_len),
2513 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
2514 con->ops->dispatch(con, msg);
2515
2516 mutex_lock(&con->mutex);
2517 }
2518
2519 static int read_keepalive_ack(struct ceph_connection *con)
2520 {
2521 struct ceph_timespec ceph_ts;
2522 size_t size = sizeof(ceph_ts);
2523 int ret = read_partial(con, size, size, &ceph_ts);
2524 if (ret <= 0)
2525 return ret;
2526 ceph_decode_timespec(&con->last_keepalive_ack, &ceph_ts);
2527 prepare_read_tag(con);
2528 return 1;
2529 }
2530
2531 /*
2532 * Write something to the socket. Called in a worker thread when the
2533 * socket appears to be writeable and we have something ready to send.
2534 */
2535 static int try_write(struct ceph_connection *con)
2536 {
2537 int ret = 1;
2538
2539 dout("try_write start %p state %lu\n", con, con->state);
2540 if (con->state != CON_STATE_PREOPEN &&
2541 con->state != CON_STATE_CONNECTING &&
2542 con->state != CON_STATE_NEGOTIATING &&
2543 con->state != CON_STATE_OPEN)
2544 return 0;
2545
2546 more:
2547 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
2548
2549 /* open the socket first? */
2550 if (con->state == CON_STATE_PREOPEN) {
2551 BUG_ON(con->sock);
2552 con->state = CON_STATE_CONNECTING;
2553
2554 con_out_kvec_reset(con);
2555 prepare_write_banner(con);
2556 prepare_read_banner(con);
2557
2558 BUG_ON(con->in_msg);
2559 con->in_tag = CEPH_MSGR_TAG_READY;
2560 dout("try_write initiating connect on %p new state %lu\n",
2561 con, con->state);
2562 ret = ceph_tcp_connect(con);
2563 if (ret < 0) {
2564 con->error_msg = "connect error";
2565 goto out;
2566 }
2567 }
2568
2569 more_kvec:
2570 BUG_ON(!con->sock);
2571
2572 /* kvec data queued? */
2573 if (con->out_kvec_left) {
2574 ret = write_partial_kvec(con);
2575 if (ret <= 0)
2576 goto out;
2577 }
2578 if (con->out_skip) {
2579 ret = write_partial_skip(con);
2580 if (ret <= 0)
2581 goto out;
2582 }
2583
2584 /* msg pages? */
2585 if (con->out_msg) {
2586 if (con->out_msg_done) {
2587 ceph_msg_put(con->out_msg);
2588 con->out_msg = NULL; /* we're done with this one */
2589 goto do_next;
2590 }
2591
2592 ret = write_partial_message_data(con);
2593 if (ret == 1)
2594 goto more_kvec; /* we need to send the footer, too! */
2595 if (ret == 0)
2596 goto out;
2597 if (ret < 0) {
2598 dout("try_write write_partial_message_data err %d\n",
2599 ret);
2600 goto out;
2601 }
2602 }
2603
2604 do_next:
2605 if (con->state == CON_STATE_OPEN) {
2606 if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
2607 prepare_write_keepalive(con);
2608 goto more;
2609 }
2610 /* is anything else pending? */
2611 if (!list_empty(&con->out_queue)) {
2612 prepare_write_message(con);
2613 goto more;
2614 }
2615 if (con->in_seq > con->in_seq_acked) {
2616 prepare_write_ack(con);
2617 goto more;
2618 }
2619 }
2620
2621 /* Nothing to do! */
2622 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
2623 dout("try_write nothing else to write.\n");
2624 ret = 0;
2625 out:
2626 dout("try_write done on %p ret %d\n", con, ret);
2627 return ret;
2628 }
2629
2630
2631
2632 /*
2633 * Read what we can from the socket.
2634 */
2635 static int try_read(struct ceph_connection *con)
2636 {
2637 int ret = -1;
2638
2639 more:
2640 dout("try_read start on %p state %lu\n", con, con->state);
2641 if (con->state != CON_STATE_CONNECTING &&
2642 con->state != CON_STATE_NEGOTIATING &&
2643 con->state != CON_STATE_OPEN)
2644 return 0;
2645
2646 BUG_ON(!con->sock);
2647
2648 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2649 con->in_base_pos);
2650
2651 if (con->state == CON_STATE_CONNECTING) {
2652 dout("try_read connecting\n");
2653 ret = read_partial_banner(con);
2654 if (ret <= 0)
2655 goto out;
2656 ret = process_banner(con);
2657 if (ret < 0)
2658 goto out;
2659
2660 con->state = CON_STATE_NEGOTIATING;
2661
2662 /*
2663 * Received banner is good, exchange connection info.
2664 * Do not reset out_kvec, as sending our banner raced
2665 * with receiving peer banner after connect completed.
2666 */
2667 ret = prepare_write_connect(con);
2668 if (ret < 0)
2669 goto out;
2670 prepare_read_connect(con);
2671
2672 /* Send connection info before awaiting response */
2673 goto out;
2674 }
2675
2676 if (con->state == CON_STATE_NEGOTIATING) {
2677 dout("try_read negotiating\n");
2678 ret = read_partial_connect(con);
2679 if (ret <= 0)
2680 goto out;
2681 ret = process_connect(con);
2682 if (ret < 0)
2683 goto out;
2684 goto more;
2685 }
2686
2687 WARN_ON(con->state != CON_STATE_OPEN);
2688
2689 if (con->in_base_pos < 0) {
2690 /*
2691 * skipping + discarding content.
2692 *
2693 * FIXME: there must be a better way to do this!
2694 */
2695 static char buf[SKIP_BUF_SIZE];
2696 int skip = min((int) sizeof (buf), -con->in_base_pos);
2697
2698 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2699 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2700 if (ret <= 0)
2701 goto out;
2702 con->in_base_pos += ret;
2703 if (con->in_base_pos)
2704 goto more;
2705 }
2706 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2707 /*
2708 * what's next?
2709 */
2710 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2711 if (ret <= 0)
2712 goto out;
2713 dout("try_read got tag %d\n", (int)con->in_tag);
2714 switch (con->in_tag) {
2715 case CEPH_MSGR_TAG_MSG:
2716 prepare_read_message(con);
2717 break;
2718 case CEPH_MSGR_TAG_ACK:
2719 prepare_read_ack(con);
2720 break;
2721 case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
2722 prepare_read_keepalive_ack(con);
2723 break;
2724 case CEPH_MSGR_TAG_CLOSE:
2725 con_close_socket(con);
2726 con->state = CON_STATE_CLOSED;
2727 goto out;
2728 default:
2729 goto bad_tag;
2730 }
2731 }
2732 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2733 ret = read_partial_message(con);
2734 if (ret <= 0) {
2735 switch (ret) {
2736 case -EBADMSG:
2737 con->error_msg = "bad crc/signature";
2738 /* fall through */
2739 case -EBADE:
2740 ret = -EIO;
2741 break;
2742 case -EIO:
2743 con->error_msg = "io error";
2744 break;
2745 }
2746 goto out;
2747 }
2748 if (con->in_tag == CEPH_MSGR_TAG_READY)
2749 goto more;
2750 process_message(con);
2751 if (con->state == CON_STATE_OPEN)
2752 prepare_read_tag(con);
2753 goto more;
2754 }
2755 if (con->in_tag == CEPH_MSGR_TAG_ACK ||
2756 con->in_tag == CEPH_MSGR_TAG_SEQ) {
2757 /*
2758 * the final handshake seq exchange is semantically
2759 * equivalent to an ACK
2760 */
2761 ret = read_partial_ack(con);
2762 if (ret <= 0)
2763 goto out;
2764 process_ack(con);
2765 goto more;
2766 }
2767 if (con->in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
2768 ret = read_keepalive_ack(con);
2769 if (ret <= 0)
2770 goto out;
2771 goto more;
2772 }
2773
2774 out:
2775 dout("try_read done on %p ret %d\n", con, ret);
2776 return ret;
2777
2778 bad_tag:
2779 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2780 con->error_msg = "protocol error, garbage tag";
2781 ret = -1;
2782 goto out;
2783 }
2784
2785
2786 /*
2787 * Atomically queue work on a connection after the specified delay.
2788 * Bump @con reference to avoid races with connection teardown.
2789 * Returns 0 if work was queued, or an error code otherwise.
2790 */
2791 static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
2792 {
2793 if (!con->ops->get(con)) {
2794 dout("%s %p ref count 0\n", __func__, con);
2795 return -ENOENT;
2796 }
2797
2798 if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
2799 dout("%s %p - already queued\n", __func__, con);
2800 con->ops->put(con);
2801 return -EBUSY;
2802 }
2803
2804 dout("%s %p %lu\n", __func__, con, delay);
2805 return 0;
2806 }
2807
2808 static void queue_con(struct ceph_connection *con)
2809 {
2810 (void) queue_con_delay(con, 0);
2811 }
2812
2813 static void cancel_con(struct ceph_connection *con)
2814 {
2815 if (cancel_delayed_work(&con->work)) {
2816 dout("%s %p\n", __func__, con);
2817 con->ops->put(con);
2818 }
2819 }
2820
2821 static bool con_sock_closed(struct ceph_connection *con)
2822 {
2823 if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
2824 return false;
2825
2826 #define CASE(x) \
2827 case CON_STATE_ ## x: \
2828 con->error_msg = "socket closed (con state " #x ")"; \
2829 break;
2830
2831 switch (con->state) {
2832 CASE(CLOSED);
2833 CASE(PREOPEN);
2834 CASE(CONNECTING);
2835 CASE(NEGOTIATING);
2836 CASE(OPEN);
2837 CASE(STANDBY);
2838 default:
2839 pr_warn("%s con %p unrecognized state %lu\n",
2840 __func__, con, con->state);
2841 con->error_msg = "unrecognized con state";
2842 BUG();
2843 break;
2844 }
2845 #undef CASE
2846
2847 return true;
2848 }
2849
2850 static bool con_backoff(struct ceph_connection *con)
2851 {
2852 int ret;
2853
2854 if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
2855 return false;
2856
2857 ret = queue_con_delay(con, round_jiffies_relative(con->delay));
2858 if (ret) {
2859 dout("%s: con %p FAILED to back off %lu\n", __func__,
2860 con, con->delay);
2861 BUG_ON(ret == -ENOENT);
2862 con_flag_set(con, CON_FLAG_BACKOFF);
2863 }
2864
2865 return true;
2866 }
2867
2868 /* Finish fault handling; con->mutex must *not* be held here */
2869
2870 static void con_fault_finish(struct ceph_connection *con)
2871 {
2872 dout("%s %p\n", __func__, con);
2873
2874 /*
2875 * in case we faulted due to authentication, invalidate our
2876 * current tickets so that we can get new ones.
2877 */
2878 if (con->auth_retry) {
2879 dout("auth_retry %d, invalidating\n", con->auth_retry);
2880 if (con->ops->invalidate_authorizer)
2881 con->ops->invalidate_authorizer(con);
2882 con->auth_retry = 0;
2883 }
2884
2885 if (con->ops->fault)
2886 con->ops->fault(con);
2887 }
2888
2889 /*
2890 * Do some work on a connection. Drop a connection ref when we're done.
2891 */
2892 static void ceph_con_workfn(struct work_struct *work)
2893 {
2894 struct ceph_connection *con = container_of(work, struct ceph_connection,
2895 work.work);
2896 bool fault;
2897
2898 mutex_lock(&con->mutex);
2899 while (true) {
2900 int ret;
2901
2902 if ((fault = con_sock_closed(con))) {
2903 dout("%s: con %p SOCK_CLOSED\n", __func__, con);
2904 break;
2905 }
2906 if (con_backoff(con)) {
2907 dout("%s: con %p BACKOFF\n", __func__, con);
2908 break;
2909 }
2910 if (con->state == CON_STATE_STANDBY) {
2911 dout("%s: con %p STANDBY\n", __func__, con);
2912 break;
2913 }
2914 if (con->state == CON_STATE_CLOSED) {
2915 dout("%s: con %p CLOSED\n", __func__, con);
2916 BUG_ON(con->sock);
2917 break;
2918 }
2919 if (con->state == CON_STATE_PREOPEN) {
2920 dout("%s: con %p PREOPEN\n", __func__, con);
2921 BUG_ON(con->sock);
2922 }
2923
2924 ret = try_read(con);
2925 if (ret < 0) {
2926 if (ret == -EAGAIN)
2927 continue;
2928 if (!con->error_msg)
2929 con->error_msg = "socket error on read";
2930 fault = true;
2931 break;
2932 }
2933
2934 ret = try_write(con);
2935 if (ret < 0) {
2936 if (ret == -EAGAIN)
2937 continue;
2938 if (!con->error_msg)
2939 con->error_msg = "socket error on write";
2940 fault = true;
2941 }
2942
2943 break; /* If we make it to here, we're done */
2944 }
2945 if (fault)
2946 con_fault(con);
2947 mutex_unlock(&con->mutex);
2948
2949 if (fault)
2950 con_fault_finish(con);
2951
2952 con->ops->put(con);
2953 }
2954
2955 /*
2956 * Generic error/fault handler. A retry mechanism is used with
2957 * exponential backoff
2958 */
2959 static void con_fault(struct ceph_connection *con)
2960 {
2961 dout("fault %p state %lu to peer %s\n",
2962 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
2963
2964 pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
2965 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
2966 con->error_msg = NULL;
2967
2968 WARN_ON(con->state != CON_STATE_CONNECTING &&
2969 con->state != CON_STATE_NEGOTIATING &&
2970 con->state != CON_STATE_OPEN);
2971
2972 con_close_socket(con);
2973
2974 if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
2975 dout("fault on LOSSYTX channel, marking CLOSED\n");
2976 con->state = CON_STATE_CLOSED;
2977 return;
2978 }
2979
2980 if (con->in_msg) {
2981 BUG_ON(con->in_msg->con != con);
2982 ceph_msg_put(con->in_msg);
2983 con->in_msg = NULL;
2984 }
2985
2986 /* Requeue anything that hasn't been acked */
2987 list_splice_init(&con->out_sent, &con->out_queue);
2988
2989 /* If there are no messages queued or keepalive pending, place
2990 * the connection in a STANDBY state */
2991 if (list_empty(&con->out_queue) &&
2992 !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
2993 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
2994 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
2995 con->state = CON_STATE_STANDBY;
2996 } else {
2997 /* retry after a delay. */
2998 con->state = CON_STATE_PREOPEN;
2999 if (con->delay == 0)
3000 con->delay = BASE_DELAY_INTERVAL;
3001 else if (con->delay < MAX_DELAY_INTERVAL)
3002 con->delay *= 2;
3003 con_flag_set(con, CON_FLAG_BACKOFF);
3004 queue_con(con);
3005 }
3006 }
3007
3008
3009
3010 /*
3011 * initialize a new messenger instance
3012 */
3013 void ceph_messenger_init(struct ceph_messenger *msgr,
3014 struct ceph_entity_addr *myaddr)
3015 {
3016 spin_lock_init(&msgr->global_seq_lock);
3017
3018 if (myaddr)
3019 msgr->inst.addr = *myaddr;
3020
3021 /* select a random nonce */
3022 msgr->inst.addr.type = 0;
3023 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
3024 encode_my_addr(msgr);
3025
3026 atomic_set(&msgr->stopping, 0);
3027 write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
3028
3029 dout("%s %p\n", __func__, msgr);
3030 }
3031 EXPORT_SYMBOL(ceph_messenger_init);
3032
3033 void ceph_messenger_fini(struct ceph_messenger *msgr)
3034 {
3035 put_net(read_pnet(&msgr->net));
3036 }
3037 EXPORT_SYMBOL(ceph_messenger_fini);
3038
3039 static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con)
3040 {
3041 if (msg->con)
3042 msg->con->ops->put(msg->con);
3043
3044 msg->con = con ? con->ops->get(con) : NULL;
3045 BUG_ON(msg->con != con);
3046 }
3047
3048 static void clear_standby(struct ceph_connection *con)
3049 {
3050 /* come back from STANDBY? */
3051 if (con->state == CON_STATE_STANDBY) {
3052 dout("clear_standby %p and ++connect_seq\n", con);
3053 con->state = CON_STATE_PREOPEN;
3054 con->connect_seq++;
3055 WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
3056 WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
3057 }
3058 }
3059
3060 /*
3061 * Queue up an outgoing message on the given connection.
3062 */
3063 void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
3064 {
3065 /* set src+dst */
3066 msg->hdr.src = con->msgr->inst.name;
3067 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
3068 msg->needs_out_seq = true;
3069
3070 mutex_lock(&con->mutex);
3071
3072 if (con->state == CON_STATE_CLOSED) {
3073 dout("con_send %p closed, dropping %p\n", con, msg);
3074 ceph_msg_put(msg);
3075 mutex_unlock(&con->mutex);
3076 return;
3077 }
3078
3079 msg_con_set(msg, con);
3080
3081 BUG_ON(!list_empty(&msg->list_head));
3082 list_add_tail(&msg->list_head, &con->out_queue);
3083 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
3084 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
3085 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
3086 le32_to_cpu(msg->hdr.front_len),
3087 le32_to_cpu(msg->hdr.middle_len),
3088 le32_to_cpu(msg->hdr.data_len));
3089
3090 clear_standby(con);
3091 mutex_unlock(&con->mutex);
3092
3093 /* if there wasn't anything waiting to send before, queue
3094 * new work */
3095 if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
3096 queue_con(con);
3097 }
3098 EXPORT_SYMBOL(ceph_con_send);
3099
3100 /*
3101 * Revoke a message that was previously queued for send
3102 */
3103 void ceph_msg_revoke(struct ceph_msg *msg)
3104 {
3105 struct ceph_connection *con = msg->con;
3106
3107 if (!con) {
3108 dout("%s msg %p null con\n", __func__, msg);
3109 return; /* Message not in our possession */
3110 }
3111
3112 mutex_lock(&con->mutex);
3113 if (!list_empty(&msg->list_head)) {
3114 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
3115 list_del_init(&msg->list_head);
3116 msg->hdr.seq = 0;
3117
3118 ceph_msg_put(msg);
3119 }
3120 if (con->out_msg == msg) {
3121 BUG_ON(con->out_skip);
3122 /* footer */
3123 if (con->out_msg_done) {
3124 con->out_skip += con_out_kvec_skip(con);
3125 } else {
3126 BUG_ON(!msg->data_length);
3127 con->out_skip += sizeof_footer(con);
3128 }
3129 /* data, middle, front */
3130 if (msg->data_length)
3131 con->out_skip += msg->cursor.total_resid;
3132 if (msg->middle)
3133 con->out_skip += con_out_kvec_skip(con);
3134 con->out_skip += con_out_kvec_skip(con);
3135
3136 dout("%s %p msg %p - was sending, will write %d skip %d\n",
3137 __func__, con, msg, con->out_kvec_bytes, con->out_skip);
3138 msg->hdr.seq = 0;
3139 con->out_msg = NULL;
3140 ceph_msg_put(msg);
3141 }
3142
3143 mutex_unlock(&con->mutex);
3144 }
3145
3146 /*
3147 * Revoke a message that we may be reading data into
3148 */
3149 void ceph_msg_revoke_incoming(struct ceph_msg *msg)
3150 {
3151 struct ceph_connection *con = msg->con;
3152
3153 if (!con) {
3154 dout("%s msg %p null con\n", __func__, msg);
3155 return; /* Message not in our possession */
3156 }
3157
3158 mutex_lock(&con->mutex);
3159 if (con->in_msg == msg) {
3160 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
3161 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
3162 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
3163
3164 /* skip rest of message */
3165 dout("%s %p msg %p revoked\n", __func__, con, msg);
3166 con->in_base_pos = con->in_base_pos -
3167 sizeof(struct ceph_msg_header) -
3168 front_len -
3169 middle_len -
3170 data_len -
3171 sizeof(struct ceph_msg_footer);
3172 ceph_msg_put(con->in_msg);
3173 con->in_msg = NULL;
3174 con->in_tag = CEPH_MSGR_TAG_READY;
3175 con->in_seq++;
3176 } else {
3177 dout("%s %p in_msg %p msg %p no-op\n",
3178 __func__, con, con->in_msg, msg);
3179 }
3180 mutex_unlock(&con->mutex);
3181 }
3182
3183 /*
3184 * Queue a keepalive byte to ensure the tcp connection is alive.
3185 */
3186 void ceph_con_keepalive(struct ceph_connection *con)
3187 {
3188 dout("con_keepalive %p\n", con);
3189 mutex_lock(&con->mutex);
3190 clear_standby(con);
3191 mutex_unlock(&con->mutex);
3192 if (con_flag_test_and_set(con, CON_FLAG_KEEPALIVE_PENDING) == 0 &&
3193 con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
3194 queue_con(con);
3195 }
3196 EXPORT_SYMBOL(ceph_con_keepalive);
3197
3198 bool ceph_con_keepalive_expired(struct ceph_connection *con,
3199 unsigned long interval)
3200 {
3201 if (interval > 0 &&
3202 (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) {
3203 struct timespec now;
3204 struct timespec ts;
3205 ktime_get_real_ts(&now);
3206 jiffies_to_timespec(interval, &ts);
3207 ts = timespec_add(con->last_keepalive_ack, ts);
3208 return timespec_compare(&now, &ts) >= 0;
3209 }
3210 return false;
3211 }
3212
3213 static struct ceph_msg_data *ceph_msg_data_create(enum ceph_msg_data_type type)
3214 {
3215 struct ceph_msg_data *data;
3216
3217 if (WARN_ON(!ceph_msg_data_type_valid(type)))
3218 return NULL;
3219
3220 data = kmem_cache_zalloc(ceph_msg_data_cache, GFP_NOFS);
3221 if (!data)
3222 return NULL;
3223
3224 data->type = type;
3225 INIT_LIST_HEAD(&data->links);
3226
3227 return data;
3228 }
3229
3230 static void ceph_msg_data_destroy(struct ceph_msg_data *data)
3231 {
3232 if (!data)
3233 return;
3234
3235 WARN_ON(!list_empty(&data->links));
3236 if (data->type == CEPH_MSG_DATA_PAGELIST)
3237 ceph_pagelist_release(data->pagelist);
3238 kmem_cache_free(ceph_msg_data_cache, data);
3239 }
3240
3241 void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
3242 size_t length, size_t alignment)
3243 {
3244 struct ceph_msg_data *data;
3245
3246 BUG_ON(!pages);
3247 BUG_ON(!length);
3248
3249 data = ceph_msg_data_create(CEPH_MSG_DATA_PAGES);
3250 BUG_ON(!data);
3251 data->pages = pages;
3252 data->length = length;
3253 data->alignment = alignment & ~PAGE_MASK;
3254
3255 list_add_tail(&data->links, &msg->data);
3256 msg->data_length += length;
3257 }
3258 EXPORT_SYMBOL(ceph_msg_data_add_pages);
3259
3260 void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
3261 struct ceph_pagelist *pagelist)
3262 {
3263 struct ceph_msg_data *data;
3264
3265 BUG_ON(!pagelist);
3266 BUG_ON(!pagelist->length);
3267
3268 data = ceph_msg_data_create(CEPH_MSG_DATA_PAGELIST);
3269 BUG_ON(!data);
3270 data->pagelist = pagelist;
3271
3272 list_add_tail(&data->links, &msg->data);
3273 msg->data_length += pagelist->length;
3274 }
3275 EXPORT_SYMBOL(ceph_msg_data_add_pagelist);
3276
3277 #ifdef CONFIG_BLOCK
3278 void ceph_msg_data_add_bio(struct ceph_msg *msg, struct bio *bio,
3279 size_t length)
3280 {
3281 struct ceph_msg_data *data;
3282
3283 BUG_ON(!bio);
3284
3285 data = ceph_msg_data_create(CEPH_MSG_DATA_BIO);
3286 BUG_ON(!data);
3287 data->bio = bio;
3288 data->bio_length = length;
3289
3290 list_add_tail(&data->links, &msg->data);
3291 msg->data_length += length;
3292 }
3293 EXPORT_SYMBOL(ceph_msg_data_add_bio);
3294 #endif /* CONFIG_BLOCK */
3295
3296 /*
3297 * construct a new message with given type, size
3298 * the new msg has a ref count of 1.
3299 */
3300 struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
3301 bool can_fail)
3302 {
3303 struct ceph_msg *m;
3304
3305 m = kmem_cache_zalloc(ceph_msg_cache, flags);
3306 if (m == NULL)
3307 goto out;
3308
3309 m->hdr.type = cpu_to_le16(type);
3310 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
3311 m->hdr.front_len = cpu_to_le32(front_len);
3312
3313 INIT_LIST_HEAD(&m->list_head);
3314 kref_init(&m->kref);
3315 INIT_LIST_HEAD(&m->data);
3316
3317 /* front */
3318 if (front_len) {
3319 m->front.iov_base = ceph_kvmalloc(front_len, flags);
3320 if (m->front.iov_base == NULL) {
3321 dout("ceph_msg_new can't allocate %d bytes\n",
3322 front_len);
3323 goto out2;
3324 }
3325 } else {
3326 m->front.iov_base = NULL;
3327 }
3328 m->front_alloc_len = m->front.iov_len = front_len;
3329
3330 dout("ceph_msg_new %p front %d\n", m, front_len);
3331 return m;
3332
3333 out2:
3334 ceph_msg_put(m);
3335 out:
3336 if (!can_fail) {
3337 pr_err("msg_new can't create type %d front %d\n", type,
3338 front_len);
3339 WARN_ON(1);
3340 } else {
3341 dout("msg_new can't create type %d front %d\n", type,
3342 front_len);
3343 }
3344 return NULL;
3345 }
3346 EXPORT_SYMBOL(ceph_msg_new);
3347
3348 /*
3349 * Allocate "middle" portion of a message, if it is needed and wasn't
3350 * allocated by alloc_msg. This allows us to read a small fixed-size
3351 * per-type header in the front and then gracefully fail (i.e.,
3352 * propagate the error to the caller based on info in the front) when
3353 * the middle is too large.
3354 */
3355 static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
3356 {
3357 int type = le16_to_cpu(msg->hdr.type);
3358 int middle_len = le32_to_cpu(msg->hdr.middle_len);
3359
3360 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
3361 ceph_msg_type_name(type), middle_len);
3362 BUG_ON(!middle_len);
3363 BUG_ON(msg->middle);
3364
3365 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
3366 if (!msg->middle)
3367 return -ENOMEM;
3368 return 0;
3369 }
3370
3371 /*
3372 * Allocate a message for receiving an incoming message on a
3373 * connection, and save the result in con->in_msg. Uses the
3374 * connection's private alloc_msg op if available.
3375 *
3376 * Returns 0 on success, or a negative error code.
3377 *
3378 * On success, if we set *skip = 1:
3379 * - the next message should be skipped and ignored.
3380 * - con->in_msg == NULL
3381 * or if we set *skip = 0:
3382 * - con->in_msg is non-null.
3383 * On error (ENOMEM, EAGAIN, ...),
3384 * - con->in_msg == NULL
3385 */
3386 static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
3387 {
3388 struct ceph_msg_header *hdr = &con->in_hdr;
3389 int middle_len = le32_to_cpu(hdr->middle_len);
3390 struct ceph_msg *msg;
3391 int ret = 0;
3392
3393 BUG_ON(con->in_msg != NULL);
3394 BUG_ON(!con->ops->alloc_msg);
3395
3396 mutex_unlock(&con->mutex);
3397 msg = con->ops->alloc_msg(con, hdr, skip);
3398 mutex_lock(&con->mutex);
3399 if (con->state != CON_STATE_OPEN) {
3400 if (msg)
3401 ceph_msg_put(msg);
3402 return -EAGAIN;
3403 }
3404 if (msg) {
3405 BUG_ON(*skip);
3406 msg_con_set(msg, con);
3407 con->in_msg = msg;
3408 } else {
3409 /*
3410 * Null message pointer means either we should skip
3411 * this message or we couldn't allocate memory. The
3412 * former is not an error.
3413 */
3414 if (*skip)
3415 return 0;
3416
3417 con->error_msg = "error allocating memory for incoming message";
3418 return -ENOMEM;
3419 }
3420 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
3421
3422 if (middle_len && !con->in_msg->middle) {
3423 ret = ceph_alloc_middle(con, con->in_msg);
3424 if (ret < 0) {
3425 ceph_msg_put(con->in_msg);
3426 con->in_msg = NULL;
3427 }
3428 }
3429
3430 return ret;
3431 }
3432
3433
3434 /*
3435 * Free a generically kmalloc'd message.
3436 */
3437 static void ceph_msg_free(struct ceph_msg *m)
3438 {
3439 dout("%s %p\n", __func__, m);
3440 kvfree(m->front.iov_base);
3441 kmem_cache_free(ceph_msg_cache, m);
3442 }
3443
3444 static void ceph_msg_release(struct kref *kref)
3445 {
3446 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
3447 struct ceph_msg_data *data, *next;
3448
3449 dout("%s %p\n", __func__, m);
3450 WARN_ON(!list_empty(&m->list_head));
3451
3452 msg_con_set(m, NULL);
3453
3454 /* drop middle, data, if any */
3455 if (m->middle) {
3456 ceph_buffer_put(m->middle);
3457 m->middle = NULL;
3458 }
3459
3460 list_for_each_entry_safe(data, next, &m->data, links) {
3461 list_del_init(&data->links);
3462 ceph_msg_data_destroy(data);
3463 }
3464 m->data_length = 0;
3465
3466 if (m->pool)
3467 ceph_msgpool_put(m->pool, m);
3468 else
3469 ceph_msg_free(m);
3470 }
3471
3472 struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
3473 {
3474 dout("%s %p (was %d)\n", __func__, msg,
3475 kref_read(&msg->kref));
3476 kref_get(&msg->kref);
3477 return msg;
3478 }
3479 EXPORT_SYMBOL(ceph_msg_get);
3480
3481 void ceph_msg_put(struct ceph_msg *msg)
3482 {
3483 dout("%s %p (was %d)\n", __func__, msg,
3484 kref_read(&msg->kref));
3485 kref_put(&msg->kref, ceph_msg_release);
3486 }
3487 EXPORT_SYMBOL(ceph_msg_put);
3488
3489 void ceph_msg_dump(struct ceph_msg *msg)
3490 {
3491 pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg,
3492 msg->front_alloc_len, msg->data_length);
3493 print_hex_dump(KERN_DEBUG, "header: ",
3494 DUMP_PREFIX_OFFSET, 16, 1,
3495 &msg->hdr, sizeof(msg->hdr), true);
3496 print_hex_dump(KERN_DEBUG, " front: ",
3497 DUMP_PREFIX_OFFSET, 16, 1,
3498 msg->front.iov_base, msg->front.iov_len, true);
3499 if (msg->middle)
3500 print_hex_dump(KERN_DEBUG, "middle: ",
3501 DUMP_PREFIX_OFFSET, 16, 1,
3502 msg->middle->vec.iov_base,
3503 msg->middle->vec.iov_len, true);
3504 print_hex_dump(KERN_DEBUG, "footer: ",
3505 DUMP_PREFIX_OFFSET, 16, 1,
3506 &msg->footer, sizeof(msg->footer), true);
3507 }
3508 EXPORT_SYMBOL(ceph_msg_dump);