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