1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
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>
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>
16 #include <linux/bio.h>
17 #endif /* CONFIG_BLOCK */
18 #include <linux/dns_resolver.h>
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>
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
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
44 * | NEW* | transient initial state
46 * | con_sock_state_init()
49 * | CLOSED | initialized, but no socket (and no
50 * ---------- TCP connection)
52 * | \ con_sock_state_connecting()
53 * | ----------------------
55 * + con_sock_state_closed() \
56 * |+--------------------------- \
59 * | | CLOSING | socket event; \ \
60 * | ----------- await close \ \
63 * | + con_sock_state_closing() \ |
65 * | / --------------- | |
68 * | / -----------------| CONNECTING | socket created, TCP
69 * | | / -------------- connect initiated
70 * | | | con_sock_state_connected()
73 * | CONNECTED | TCP connection established
76 * State values for ceph_connection->sock_state; NEW is assumed to be 0.
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 */
86 * ceph_connection flag bits
88 #define CON_FLAG_LOSSYTX 0 /* we can close channel or drop
89 * messages on errors */
90 #define CON_FLAG_KEEPALIVE_PENDING 1 /* we need to send a keepalive */
91 #define CON_FLAG_WRITE_PENDING 2 /* we have data ready to send */
92 #define CON_FLAG_SOCK_CLOSED 3 /* socket state changed to closed */
93 #define CON_FLAG_BACKOFF 4 /* need to retry queuing delayed work */
95 static bool con_flag_valid(unsigned long con_flag)
98 case CON_FLAG_LOSSYTX:
99 case CON_FLAG_KEEPALIVE_PENDING:
100 case CON_FLAG_WRITE_PENDING:
101 case CON_FLAG_SOCK_CLOSED:
102 case CON_FLAG_BACKOFF:
109 static void con_flag_clear(struct ceph_connection *con, unsigned long con_flag)
111 BUG_ON(!con_flag_valid(con_flag));
113 clear_bit(con_flag, &con->flags);
116 static void con_flag_set(struct ceph_connection *con, unsigned long con_flag)
118 BUG_ON(!con_flag_valid(con_flag));
120 set_bit(con_flag, &con->flags);
123 static bool con_flag_test(struct ceph_connection *con, unsigned long con_flag)
125 BUG_ON(!con_flag_valid(con_flag));
127 return test_bit(con_flag, &con->flags);
130 static bool con_flag_test_and_clear(struct ceph_connection *con,
131 unsigned long con_flag)
133 BUG_ON(!con_flag_valid(con_flag));
135 return test_and_clear_bit(con_flag, &con->flags);
138 static bool con_flag_test_and_set(struct ceph_connection *con,
139 unsigned long con_flag)
141 BUG_ON(!con_flag_valid(con_flag));
143 return test_and_set_bit(con_flag, &con->flags);
146 /* Slab caches for frequently-allocated structures */
148 static struct kmem_cache *ceph_msg_cache;
150 /* static tag bytes (protocol control messages) */
151 static char tag_msg = CEPH_MSGR_TAG_MSG;
152 static char tag_ack = CEPH_MSGR_TAG_ACK;
153 static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
154 static char tag_keepalive2 = CEPH_MSGR_TAG_KEEPALIVE2;
156 #ifdef CONFIG_LOCKDEP
157 static struct lock_class_key socket_class;
160 static void queue_con(struct ceph_connection *con);
161 static void cancel_con(struct ceph_connection *con);
162 static void ceph_con_workfn(struct work_struct *);
163 static void con_fault(struct ceph_connection *con);
166 * Nicely render a sockaddr as a string. An array of formatted
167 * strings is used, to approximate reentrancy.
169 #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
170 #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
171 #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
172 #define MAX_ADDR_STR_LEN 64 /* 54 is enough */
174 static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
175 static atomic_t addr_str_seq = ATOMIC_INIT(0);
177 static struct page *zero_page; /* used in certain error cases */
179 const char *ceph_pr_addr(const struct ceph_entity_addr *addr)
183 struct sockaddr_storage ss = addr->in_addr; /* align */
184 struct sockaddr_in *in4 = (struct sockaddr_in *)&ss;
185 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&ss;
187 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
190 switch (ss.ss_family) {
192 snprintf(s, MAX_ADDR_STR_LEN, "(%d)%pI4:%hu",
193 le32_to_cpu(addr->type), &in4->sin_addr,
194 ntohs(in4->sin_port));
198 snprintf(s, MAX_ADDR_STR_LEN, "(%d)[%pI6c]:%hu",
199 le32_to_cpu(addr->type), &in6->sin6_addr,
200 ntohs(in6->sin6_port));
204 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
210 EXPORT_SYMBOL(ceph_pr_addr);
212 static void encode_my_addr(struct ceph_messenger *msgr)
214 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
215 ceph_encode_banner_addr(&msgr->my_enc_addr);
219 * work queue for all reading and writing to/from the socket.
221 static struct workqueue_struct *ceph_msgr_wq;
223 static int ceph_msgr_slab_init(void)
225 BUG_ON(ceph_msg_cache);
226 ceph_msg_cache = KMEM_CACHE(ceph_msg, 0);
233 static void ceph_msgr_slab_exit(void)
235 BUG_ON(!ceph_msg_cache);
236 kmem_cache_destroy(ceph_msg_cache);
237 ceph_msg_cache = NULL;
240 static void _ceph_msgr_exit(void)
243 destroy_workqueue(ceph_msgr_wq);
247 BUG_ON(zero_page == NULL);
251 ceph_msgr_slab_exit();
254 int __init ceph_msgr_init(void)
256 if (ceph_msgr_slab_init())
259 BUG_ON(zero_page != NULL);
260 zero_page = ZERO_PAGE(0);
264 * The number of active work items is limited by the number of
265 * connections, so leave @max_active at default.
267 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_MEM_RECLAIM, 0);
271 pr_err("msgr_init failed to create workqueue\n");
277 void ceph_msgr_exit(void)
279 BUG_ON(ceph_msgr_wq == NULL);
284 void ceph_msgr_flush(void)
286 flush_workqueue(ceph_msgr_wq);
288 EXPORT_SYMBOL(ceph_msgr_flush);
290 /* Connection socket state transition functions */
292 static void con_sock_state_init(struct ceph_connection *con)
296 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
297 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
298 printk("%s: unexpected old state %d\n", __func__, old_state);
299 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
300 CON_SOCK_STATE_CLOSED);
303 static void con_sock_state_connecting(struct ceph_connection *con)
307 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
308 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
309 printk("%s: unexpected old state %d\n", __func__, old_state);
310 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
311 CON_SOCK_STATE_CONNECTING);
314 static void con_sock_state_connected(struct ceph_connection *con)
318 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
319 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
320 printk("%s: unexpected old state %d\n", __func__, old_state);
321 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
322 CON_SOCK_STATE_CONNECTED);
325 static void con_sock_state_closing(struct ceph_connection *con)
329 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
330 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
331 old_state != CON_SOCK_STATE_CONNECTED &&
332 old_state != CON_SOCK_STATE_CLOSING))
333 printk("%s: unexpected old state %d\n", __func__, old_state);
334 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
335 CON_SOCK_STATE_CLOSING);
338 static void con_sock_state_closed(struct ceph_connection *con)
342 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
343 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
344 old_state != CON_SOCK_STATE_CLOSING &&
345 old_state != CON_SOCK_STATE_CONNECTING &&
346 old_state != CON_SOCK_STATE_CLOSED))
347 printk("%s: unexpected old state %d\n", __func__, old_state);
348 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
349 CON_SOCK_STATE_CLOSED);
353 * socket callback functions
356 /* data available on socket, or listen socket received a connect */
357 static void ceph_sock_data_ready(struct sock *sk)
359 struct ceph_connection *con = sk->sk_user_data;
360 if (atomic_read(&con->msgr->stopping)) {
364 if (sk->sk_state != TCP_CLOSE_WAIT) {
365 dout("%s %p state = %d, queueing work\n", __func__,
371 /* socket has buffer space for writing */
372 static void ceph_sock_write_space(struct sock *sk)
374 struct ceph_connection *con = sk->sk_user_data;
376 /* only queue to workqueue if there is data we want to write,
377 * and there is sufficient space in the socket buffer to accept
378 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
379 * doesn't get called again until try_write() fills the socket
380 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
381 * and net/core/stream.c:sk_stream_write_space().
383 if (con_flag_test(con, CON_FLAG_WRITE_PENDING)) {
384 if (sk_stream_is_writeable(sk)) {
385 dout("%s %p queueing write work\n", __func__, con);
386 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
390 dout("%s %p nothing to write\n", __func__, con);
394 /* socket's state has changed */
395 static void ceph_sock_state_change(struct sock *sk)
397 struct ceph_connection *con = sk->sk_user_data;
399 dout("%s %p state = %d sk_state = %u\n", __func__,
400 con, con->state, sk->sk_state);
402 switch (sk->sk_state) {
404 dout("%s TCP_CLOSE\n", __func__);
407 dout("%s TCP_CLOSE_WAIT\n", __func__);
408 con_sock_state_closing(con);
409 con_flag_set(con, CON_FLAG_SOCK_CLOSED);
412 case TCP_ESTABLISHED:
413 dout("%s TCP_ESTABLISHED\n", __func__);
414 con_sock_state_connected(con);
417 default: /* Everything else is uninteresting */
423 * set up socket callbacks
425 static void set_sock_callbacks(struct socket *sock,
426 struct ceph_connection *con)
428 struct sock *sk = sock->sk;
429 sk->sk_user_data = con;
430 sk->sk_data_ready = ceph_sock_data_ready;
431 sk->sk_write_space = ceph_sock_write_space;
432 sk->sk_state_change = ceph_sock_state_change;
441 * initiate connection to a remote socket.
443 static int ceph_tcp_connect(struct ceph_connection *con)
445 struct sockaddr_storage ss = con->peer_addr.in_addr; /* align */
447 unsigned int noio_flag;
452 /* sock_create_kern() allocates with GFP_KERNEL */
453 noio_flag = memalloc_noio_save();
454 ret = sock_create_kern(read_pnet(&con->msgr->net), ss.ss_family,
455 SOCK_STREAM, IPPROTO_TCP, &sock);
456 memalloc_noio_restore(noio_flag);
459 sock->sk->sk_allocation = GFP_NOFS;
461 #ifdef CONFIG_LOCKDEP
462 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
465 set_sock_callbacks(sock, con);
467 dout("connect %s\n", ceph_pr_addr(&con->peer_addr));
469 con_sock_state_connecting(con);
470 ret = sock->ops->connect(sock, (struct sockaddr *)&ss, sizeof(ss),
472 if (ret == -EINPROGRESS) {
473 dout("connect %s EINPROGRESS sk_state = %u\n",
474 ceph_pr_addr(&con->peer_addr),
476 } else if (ret < 0) {
477 pr_err("connect %s error %d\n",
478 ceph_pr_addr(&con->peer_addr), ret);
483 if (ceph_test_opt(from_msgr(con->msgr), TCP_NODELAY))
484 tcp_sock_set_nodelay(sock->sk);
491 * If @buf is NULL, discard up to @len bytes.
493 static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
495 struct kvec iov = {buf, len};
496 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
500 msg.msg_flags |= MSG_TRUNC;
502 iov_iter_kvec(&msg.msg_iter, READ, &iov, 1, len);
503 r = sock_recvmsg(sock, &msg, msg.msg_flags);
509 static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
510 int page_offset, size_t length)
512 struct bio_vec bvec = {
514 .bv_offset = page_offset,
517 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
520 BUG_ON(page_offset + length > PAGE_SIZE);
521 iov_iter_bvec(&msg.msg_iter, READ, &bvec, 1, length);
522 r = sock_recvmsg(sock, &msg, msg.msg_flags);
529 * write something. @more is true if caller will be sending more data
532 static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
533 size_t kvlen, size_t len, bool more)
535 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
539 msg.msg_flags |= MSG_MORE;
541 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
543 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
550 * @more: either or both of MSG_MORE and MSG_SENDPAGE_NOTLAST
552 static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
553 int offset, size_t size, int more)
555 ssize_t (*sendpage)(struct socket *sock, struct page *page,
556 int offset, size_t size, int flags);
557 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | more;
561 * sendpage cannot properly handle pages with page_count == 0,
562 * we need to fall back to sendmsg if that's the case.
564 * Same goes for slab pages: skb_can_coalesce() allows
565 * coalescing neighboring slab objects into a single frag which
566 * triggers one of hardened usercopy checks.
568 if (sendpage_ok(page))
569 sendpage = sock->ops->sendpage;
571 sendpage = sock_no_sendpage;
573 ret = sendpage(sock, page, offset, size, flags);
581 * Shutdown/close the socket for the given connection.
583 static int con_close_socket(struct ceph_connection *con)
587 dout("con_close_socket on %p sock %p\n", con, con->sock);
589 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
590 sock_release(con->sock);
595 * Forcibly clear the SOCK_CLOSED flag. It gets set
596 * independent of the connection mutex, and we could have
597 * received a socket close event before we had the chance to
598 * shut the socket down.
600 con_flag_clear(con, CON_FLAG_SOCK_CLOSED);
602 con_sock_state_closed(con);
606 static void ceph_con_reset_protocol(struct ceph_connection *con)
608 dout("%s con %p\n", __func__, con);
610 con_close_socket(con);
612 WARN_ON(con->in_msg->con != con);
613 ceph_msg_put(con->in_msg);
617 WARN_ON(con->out_msg->con != con);
618 ceph_msg_put(con->out_msg);
626 * Reset a connection. Discard all incoming and outgoing messages
627 * and clear *_seq state.
629 static void ceph_msg_remove(struct ceph_msg *msg)
631 list_del_init(&msg->list_head);
635 static void ceph_msg_remove_list(struct list_head *head)
637 while (!list_empty(head)) {
638 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
640 ceph_msg_remove(msg);
644 static void ceph_con_reset_session(struct ceph_connection *con)
646 dout("%s con %p\n", __func__, con);
648 WARN_ON(con->in_msg);
649 WARN_ON(con->out_msg);
650 ceph_msg_remove_list(&con->out_queue);
651 ceph_msg_remove_list(&con->out_sent);
654 con->in_seq_acked = 0;
656 con->connect_seq = 0;
657 con->peer_global_seq = 0;
661 * mark a peer down. drop any open connections.
663 void ceph_con_close(struct ceph_connection *con)
665 mutex_lock(&con->mutex);
666 dout("con_close %p peer %s\n", con, ceph_pr_addr(&con->peer_addr));
667 con->state = CEPH_CON_S_CLOSED;
669 con_flag_clear(con, CON_FLAG_LOSSYTX); /* so we retry next connect */
670 con_flag_clear(con, CON_FLAG_KEEPALIVE_PENDING);
671 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
672 con_flag_clear(con, CON_FLAG_BACKOFF);
674 ceph_con_reset_protocol(con);
675 ceph_con_reset_session(con);
677 mutex_unlock(&con->mutex);
679 EXPORT_SYMBOL(ceph_con_close);
682 * Reopen a closed connection, with a new peer address.
684 void ceph_con_open(struct ceph_connection *con,
685 __u8 entity_type, __u64 entity_num,
686 struct ceph_entity_addr *addr)
688 mutex_lock(&con->mutex);
689 dout("con_open %p %s\n", con, ceph_pr_addr(addr));
691 WARN_ON(con->state != CEPH_CON_S_CLOSED);
692 con->state = CEPH_CON_S_PREOPEN;
694 con->peer_name.type = (__u8) entity_type;
695 con->peer_name.num = cpu_to_le64(entity_num);
697 memcpy(&con->peer_addr, addr, sizeof(*addr));
698 con->delay = 0; /* reset backoff memory */
699 mutex_unlock(&con->mutex);
702 EXPORT_SYMBOL(ceph_con_open);
705 * return true if this connection ever successfully opened
707 bool ceph_con_opened(struct ceph_connection *con)
709 return con->connect_seq > 0;
713 * initialize a new connection.
715 void ceph_con_init(struct ceph_connection *con, void *private,
716 const struct ceph_connection_operations *ops,
717 struct ceph_messenger *msgr)
719 dout("con_init %p\n", con);
720 memset(con, 0, sizeof(*con));
721 con->private = private;
725 con_sock_state_init(con);
727 mutex_init(&con->mutex);
728 INIT_LIST_HEAD(&con->out_queue);
729 INIT_LIST_HEAD(&con->out_sent);
730 INIT_DELAYED_WORK(&con->work, ceph_con_workfn);
732 con->state = CEPH_CON_S_CLOSED;
734 EXPORT_SYMBOL(ceph_con_init);
738 * We maintain a global counter to order connection attempts. Get
739 * a unique seq greater than @gt.
741 static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
745 spin_lock(&msgr->global_seq_lock);
746 if (msgr->global_seq < gt)
747 msgr->global_seq = gt;
748 ret = ++msgr->global_seq;
749 spin_unlock(&msgr->global_seq_lock);
754 * Discard messages that have been acked by the server.
756 static void ceph_con_discard_sent(struct ceph_connection *con, u64 ack_seq)
758 struct ceph_msg *msg;
761 dout("%s con %p ack_seq %llu\n", __func__, con, ack_seq);
762 while (!list_empty(&con->out_sent)) {
763 msg = list_first_entry(&con->out_sent, struct ceph_msg,
765 WARN_ON(msg->needs_out_seq);
766 seq = le64_to_cpu(msg->hdr.seq);
770 dout("%s con %p discarding msg %p seq %llu\n", __func__, con,
772 ceph_msg_remove(msg);
777 * Discard messages that have been requeued in con_fault(), up to
778 * reconnect_seq. This avoids gratuitously resending messages that
779 * the server had received and handled prior to reconnect.
781 static void ceph_con_discard_requeued(struct ceph_connection *con,
784 struct ceph_msg *msg;
787 dout("%s con %p reconnect_seq %llu\n", __func__, con, reconnect_seq);
788 while (!list_empty(&con->out_queue)) {
789 msg = list_first_entry(&con->out_queue, struct ceph_msg,
791 if (msg->needs_out_seq)
793 seq = le64_to_cpu(msg->hdr.seq);
794 if (seq > reconnect_seq)
797 dout("%s con %p discarding msg %p seq %llu\n", __func__, con,
799 ceph_msg_remove(msg);
803 static void con_out_kvec_reset(struct ceph_connection *con)
805 BUG_ON(con->out_skip);
807 con->out_kvec_left = 0;
808 con->out_kvec_bytes = 0;
809 con->out_kvec_cur = &con->out_kvec[0];
812 static void con_out_kvec_add(struct ceph_connection *con,
813 size_t size, void *data)
815 int index = con->out_kvec_left;
817 BUG_ON(con->out_skip);
818 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
820 con->out_kvec[index].iov_len = size;
821 con->out_kvec[index].iov_base = data;
822 con->out_kvec_left++;
823 con->out_kvec_bytes += size;
827 * Chop off a kvec from the end. Return residual number of bytes for
828 * that kvec, i.e. how many bytes would have been written if the kvec
831 static int con_out_kvec_skip(struct ceph_connection *con)
833 int off = con->out_kvec_cur - con->out_kvec;
836 if (con->out_kvec_bytes > 0) {
837 skip = con->out_kvec[off + con->out_kvec_left - 1].iov_len;
838 BUG_ON(con->out_kvec_bytes < skip);
839 BUG_ON(!con->out_kvec_left);
840 con->out_kvec_bytes -= skip;
841 con->out_kvec_left--;
850 * For a bio data item, a piece is whatever remains of the next
851 * entry in the current bio iovec, or the first entry in the next
854 static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor,
857 struct ceph_msg_data *data = cursor->data;
858 struct ceph_bio_iter *it = &cursor->bio_iter;
860 cursor->resid = min_t(size_t, length, data->bio_length);
862 if (cursor->resid < it->iter.bi_size)
863 it->iter.bi_size = cursor->resid;
865 BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter));
866 cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter);
869 static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor,
873 struct bio_vec bv = bio_iter_iovec(cursor->bio_iter.bio,
874 cursor->bio_iter.iter);
876 *page_offset = bv.bv_offset;
881 static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
884 struct ceph_bio_iter *it = &cursor->bio_iter;
885 struct page *page = bio_iter_page(it->bio, it->iter);
887 BUG_ON(bytes > cursor->resid);
888 BUG_ON(bytes > bio_iter_len(it->bio, it->iter));
889 cursor->resid -= bytes;
890 bio_advance_iter(it->bio, &it->iter, bytes);
892 if (!cursor->resid) {
893 BUG_ON(!cursor->last_piece);
894 return false; /* no more data */
897 if (!bytes || (it->iter.bi_size && it->iter.bi_bvec_done &&
898 page == bio_iter_page(it->bio, it->iter)))
899 return false; /* more bytes to process in this segment */
901 if (!it->iter.bi_size) {
902 it->bio = it->bio->bi_next;
903 it->iter = it->bio->bi_iter;
904 if (cursor->resid < it->iter.bi_size)
905 it->iter.bi_size = cursor->resid;
908 BUG_ON(cursor->last_piece);
909 BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter));
910 cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter);
913 #endif /* CONFIG_BLOCK */
915 static void ceph_msg_data_bvecs_cursor_init(struct ceph_msg_data_cursor *cursor,
918 struct ceph_msg_data *data = cursor->data;
919 struct bio_vec *bvecs = data->bvec_pos.bvecs;
921 cursor->resid = min_t(size_t, length, data->bvec_pos.iter.bi_size);
922 cursor->bvec_iter = data->bvec_pos.iter;
923 cursor->bvec_iter.bi_size = cursor->resid;
925 BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter));
927 cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter);
930 static struct page *ceph_msg_data_bvecs_next(struct ceph_msg_data_cursor *cursor,
934 struct bio_vec bv = bvec_iter_bvec(cursor->data->bvec_pos.bvecs,
937 *page_offset = bv.bv_offset;
942 static bool ceph_msg_data_bvecs_advance(struct ceph_msg_data_cursor *cursor,
945 struct bio_vec *bvecs = cursor->data->bvec_pos.bvecs;
946 struct page *page = bvec_iter_page(bvecs, cursor->bvec_iter);
948 BUG_ON(bytes > cursor->resid);
949 BUG_ON(bytes > bvec_iter_len(bvecs, cursor->bvec_iter));
950 cursor->resid -= bytes;
951 bvec_iter_advance(bvecs, &cursor->bvec_iter, bytes);
953 if (!cursor->resid) {
954 BUG_ON(!cursor->last_piece);
955 return false; /* no more data */
958 if (!bytes || (cursor->bvec_iter.bi_bvec_done &&
959 page == bvec_iter_page(bvecs, cursor->bvec_iter)))
960 return false; /* more bytes to process in this segment */
962 BUG_ON(cursor->last_piece);
963 BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter));
965 cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter);
970 * For a page array, a piece comes from the first page in the array
971 * that has not already been fully consumed.
973 static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor,
976 struct ceph_msg_data *data = cursor->data;
979 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
981 BUG_ON(!data->pages);
982 BUG_ON(!data->length);
984 cursor->resid = min(length, data->length);
985 page_count = calc_pages_for(data->alignment, (u64)data->length);
986 cursor->page_offset = data->alignment & ~PAGE_MASK;
987 cursor->page_index = 0;
988 BUG_ON(page_count > (int)USHRT_MAX);
989 cursor->page_count = (unsigned short)page_count;
990 BUG_ON(length > SIZE_MAX - cursor->page_offset);
991 cursor->last_piece = cursor->page_offset + cursor->resid <= PAGE_SIZE;
995 ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
996 size_t *page_offset, size_t *length)
998 struct ceph_msg_data *data = cursor->data;
1000 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
1002 BUG_ON(cursor->page_index >= cursor->page_count);
1003 BUG_ON(cursor->page_offset >= PAGE_SIZE);
1005 *page_offset = cursor->page_offset;
1006 if (cursor->last_piece)
1007 *length = cursor->resid;
1009 *length = PAGE_SIZE - *page_offset;
1011 return data->pages[cursor->page_index];
1014 static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
1017 BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES);
1019 BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
1021 /* Advance the cursor page offset */
1023 cursor->resid -= bytes;
1024 cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK;
1025 if (!bytes || cursor->page_offset)
1026 return false; /* more bytes to process in the current page */
1029 return false; /* no more data */
1031 /* Move on to the next page; offset is already at 0 */
1033 BUG_ON(cursor->page_index >= cursor->page_count);
1034 cursor->page_index++;
1035 cursor->last_piece = cursor->resid <= PAGE_SIZE;
1041 * For a pagelist, a piece is whatever remains to be consumed in the
1042 * first page in the list, or the front of the next page.
1045 ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
1048 struct ceph_msg_data *data = cursor->data;
1049 struct ceph_pagelist *pagelist;
1052 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1054 pagelist = data->pagelist;
1058 return; /* pagelist can be assigned but empty */
1060 BUG_ON(list_empty(&pagelist->head));
1061 page = list_first_entry(&pagelist->head, struct page, lru);
1063 cursor->resid = min(length, pagelist->length);
1064 cursor->page = page;
1066 cursor->last_piece = cursor->resid <= PAGE_SIZE;
1069 static struct page *
1070 ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
1071 size_t *page_offset, size_t *length)
1073 struct ceph_msg_data *data = cursor->data;
1074 struct ceph_pagelist *pagelist;
1076 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1078 pagelist = data->pagelist;
1081 BUG_ON(!cursor->page);
1082 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
1084 /* offset of first page in pagelist is always 0 */
1085 *page_offset = cursor->offset & ~PAGE_MASK;
1086 if (cursor->last_piece)
1087 *length = cursor->resid;
1089 *length = PAGE_SIZE - *page_offset;
1091 return cursor->page;
1094 static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
1097 struct ceph_msg_data *data = cursor->data;
1098 struct ceph_pagelist *pagelist;
1100 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1102 pagelist = data->pagelist;
1105 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
1106 BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
1108 /* Advance the cursor offset */
1110 cursor->resid -= bytes;
1111 cursor->offset += bytes;
1112 /* offset of first page in pagelist is always 0 */
1113 if (!bytes || cursor->offset & ~PAGE_MASK)
1114 return false; /* more bytes to process in the current page */
1117 return false; /* no more data */
1119 /* Move on to the next page */
1121 BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
1122 cursor->page = list_next_entry(cursor->page, lru);
1123 cursor->last_piece = cursor->resid <= PAGE_SIZE;
1129 * Message data is handled (sent or received) in pieces, where each
1130 * piece resides on a single page. The network layer might not
1131 * consume an entire piece at once. A data item's cursor keeps
1132 * track of which piece is next to process and how much remains to
1133 * be processed in that piece. It also tracks whether the current
1134 * piece is the last one in the data item.
1136 static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor)
1138 size_t length = cursor->total_resid;
1140 switch (cursor->data->type) {
1141 case CEPH_MSG_DATA_PAGELIST:
1142 ceph_msg_data_pagelist_cursor_init(cursor, length);
1144 case CEPH_MSG_DATA_PAGES:
1145 ceph_msg_data_pages_cursor_init(cursor, length);
1148 case CEPH_MSG_DATA_BIO:
1149 ceph_msg_data_bio_cursor_init(cursor, length);
1151 #endif /* CONFIG_BLOCK */
1152 case CEPH_MSG_DATA_BVECS:
1153 ceph_msg_data_bvecs_cursor_init(cursor, length);
1155 case CEPH_MSG_DATA_NONE:
1160 cursor->need_crc = true;
1163 static void ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor,
1164 struct ceph_msg *msg, size_t length)
1167 BUG_ON(length > msg->data_length);
1168 BUG_ON(!msg->num_data_items);
1170 cursor->total_resid = length;
1171 cursor->data = msg->data;
1173 __ceph_msg_data_cursor_init(cursor);
1177 * Return the page containing the next piece to process for a given
1178 * data item, and supply the page offset and length of that piece.
1179 * Indicate whether this is the last piece in this data item.
1181 static struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
1182 size_t *page_offset, size_t *length,
1187 switch (cursor->data->type) {
1188 case CEPH_MSG_DATA_PAGELIST:
1189 page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
1191 case CEPH_MSG_DATA_PAGES:
1192 page = ceph_msg_data_pages_next(cursor, page_offset, length);
1195 case CEPH_MSG_DATA_BIO:
1196 page = ceph_msg_data_bio_next(cursor, page_offset, length);
1198 #endif /* CONFIG_BLOCK */
1199 case CEPH_MSG_DATA_BVECS:
1200 page = ceph_msg_data_bvecs_next(cursor, page_offset, length);
1202 case CEPH_MSG_DATA_NONE:
1209 BUG_ON(*page_offset + *length > PAGE_SIZE);
1211 BUG_ON(*length > cursor->resid);
1213 *last_piece = cursor->last_piece;
1219 * Returns true if the result moves the cursor on to the next piece
1222 static void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor,
1227 BUG_ON(bytes > cursor->resid);
1228 switch (cursor->data->type) {
1229 case CEPH_MSG_DATA_PAGELIST:
1230 new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
1232 case CEPH_MSG_DATA_PAGES:
1233 new_piece = ceph_msg_data_pages_advance(cursor, bytes);
1236 case CEPH_MSG_DATA_BIO:
1237 new_piece = ceph_msg_data_bio_advance(cursor, bytes);
1239 #endif /* CONFIG_BLOCK */
1240 case CEPH_MSG_DATA_BVECS:
1241 new_piece = ceph_msg_data_bvecs_advance(cursor, bytes);
1243 case CEPH_MSG_DATA_NONE:
1248 cursor->total_resid -= bytes;
1250 if (!cursor->resid && cursor->total_resid) {
1251 WARN_ON(!cursor->last_piece);
1253 __ceph_msg_data_cursor_init(cursor);
1256 cursor->need_crc = new_piece;
1259 static size_t sizeof_footer(struct ceph_connection *con)
1261 return (con->peer_features & CEPH_FEATURE_MSG_AUTH) ?
1262 sizeof(struct ceph_msg_footer) :
1263 sizeof(struct ceph_msg_footer_old);
1266 static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
1268 /* Initialize data cursor */
1270 ceph_msg_data_cursor_init(&msg->cursor, msg, data_len);
1274 * Prepare footer for currently outgoing message, and finish things
1275 * off. Assumes out_kvec* are already valid.. we just add on to the end.
1277 static void prepare_write_message_footer(struct ceph_connection *con)
1279 struct ceph_msg *m = con->out_msg;
1281 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
1283 dout("prepare_write_message_footer %p\n", con);
1284 con_out_kvec_add(con, sizeof_footer(con), &m->footer);
1285 if (con->peer_features & CEPH_FEATURE_MSG_AUTH) {
1286 if (con->ops->sign_message)
1287 con->ops->sign_message(m);
1291 m->old_footer.flags = m->footer.flags;
1293 con->out_more = m->more_to_follow;
1294 con->out_msg_done = true;
1297 static void ceph_con_get_out_msg(struct ceph_connection *con);
1300 * Prepare headers for the next outgoing message.
1302 static void prepare_write_message(struct ceph_connection *con)
1307 con_out_kvec_reset(con);
1308 con->out_msg_done = false;
1310 /* Sneak an ack in there first? If we can get it into the same
1311 * TCP packet that's a good thing. */
1312 if (con->in_seq > con->in_seq_acked) {
1313 con->in_seq_acked = con->in_seq;
1314 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
1315 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1316 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1317 &con->out_temp_ack);
1320 ceph_con_get_out_msg(con);
1323 dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
1324 m, con->out_seq, le16_to_cpu(m->hdr.type),
1325 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
1327 WARN_ON(m->front.iov_len != le32_to_cpu(m->hdr.front_len));
1328 WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
1330 /* tag + hdr + front + middle */
1331 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
1332 con_out_kvec_add(con, sizeof(con->out_hdr), &con->out_hdr);
1333 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
1336 con_out_kvec_add(con, m->middle->vec.iov_len,
1337 m->middle->vec.iov_base);
1339 /* fill in hdr crc and finalize hdr */
1340 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
1341 con->out_msg->hdr.crc = cpu_to_le32(crc);
1342 memcpy(&con->out_hdr, &con->out_msg->hdr, sizeof(con->out_hdr));
1344 /* fill in front and middle crc, footer */
1345 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
1346 con->out_msg->footer.front_crc = cpu_to_le32(crc);
1348 crc = crc32c(0, m->middle->vec.iov_base,
1349 m->middle->vec.iov_len);
1350 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
1352 con->out_msg->footer.middle_crc = 0;
1353 dout("%s front_crc %u middle_crc %u\n", __func__,
1354 le32_to_cpu(con->out_msg->footer.front_crc),
1355 le32_to_cpu(con->out_msg->footer.middle_crc));
1356 con->out_msg->footer.flags = 0;
1358 /* is there a data payload? */
1359 con->out_msg->footer.data_crc = 0;
1360 if (m->data_length) {
1361 prepare_message_data(con->out_msg, m->data_length);
1362 con->out_more = 1; /* data + footer will follow */
1364 /* no, queue up footer too and be done */
1365 prepare_write_message_footer(con);
1368 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1374 static void prepare_write_ack(struct ceph_connection *con)
1376 dout("prepare_write_ack %p %llu -> %llu\n", con,
1377 con->in_seq_acked, con->in_seq);
1378 con->in_seq_acked = con->in_seq;
1380 con_out_kvec_reset(con);
1382 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
1384 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1385 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1386 &con->out_temp_ack);
1388 con->out_more = 1; /* more will follow.. eventually.. */
1389 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1393 * Prepare to share the seq during handshake
1395 static void prepare_write_seq(struct ceph_connection *con)
1397 dout("prepare_write_seq %p %llu -> %llu\n", con,
1398 con->in_seq_acked, con->in_seq);
1399 con->in_seq_acked = con->in_seq;
1401 con_out_kvec_reset(con);
1403 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1404 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1405 &con->out_temp_ack);
1407 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1411 * Prepare to write keepalive byte.
1413 static void prepare_write_keepalive(struct ceph_connection *con)
1415 dout("prepare_write_keepalive %p\n", con);
1416 con_out_kvec_reset(con);
1417 if (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2) {
1418 struct timespec64 now;
1420 ktime_get_real_ts64(&now);
1421 con_out_kvec_add(con, sizeof(tag_keepalive2), &tag_keepalive2);
1422 ceph_encode_timespec64(&con->out_temp_keepalive2, &now);
1423 con_out_kvec_add(con, sizeof(con->out_temp_keepalive2),
1424 &con->out_temp_keepalive2);
1426 con_out_kvec_add(con, sizeof(tag_keepalive), &tag_keepalive);
1428 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1432 * Connection negotiation.
1435 static int get_connect_authorizer(struct ceph_connection *con)
1437 struct ceph_auth_handshake *auth;
1440 if (!con->ops->get_authorizer) {
1442 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
1443 con->out_connect.authorizer_len = 0;
1447 auth = con->ops->get_authorizer(con, &auth_proto, con->auth_retry);
1449 return PTR_ERR(auth);
1452 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
1453 con->out_connect.authorizer_len = cpu_to_le32(auth->authorizer_buf_len);
1458 * We connected to a peer and are saying hello.
1460 static void prepare_write_banner(struct ceph_connection *con)
1462 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
1463 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
1464 &con->msgr->my_enc_addr);
1467 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1470 static void __prepare_write_connect(struct ceph_connection *con)
1472 con_out_kvec_add(con, sizeof(con->out_connect), &con->out_connect);
1474 con_out_kvec_add(con, con->auth->authorizer_buf_len,
1475 con->auth->authorizer_buf);
1478 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1481 static int prepare_write_connect(struct ceph_connection *con)
1483 unsigned int global_seq = get_global_seq(con->msgr, 0);
1487 switch (con->peer_name.type) {
1488 case CEPH_ENTITY_TYPE_MON:
1489 proto = CEPH_MONC_PROTOCOL;
1491 case CEPH_ENTITY_TYPE_OSD:
1492 proto = CEPH_OSDC_PROTOCOL;
1494 case CEPH_ENTITY_TYPE_MDS:
1495 proto = CEPH_MDSC_PROTOCOL;
1501 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
1502 con->connect_seq, global_seq, proto);
1504 con->out_connect.features =
1505 cpu_to_le64(from_msgr(con->msgr)->supported_features);
1506 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
1507 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
1508 con->out_connect.global_seq = cpu_to_le32(global_seq);
1509 con->out_connect.protocol_version = cpu_to_le32(proto);
1510 con->out_connect.flags = 0;
1512 ret = get_connect_authorizer(con);
1516 __prepare_write_connect(con);
1521 * write as much of pending kvecs to the socket as we can.
1523 * 0 -> socket full, but more to do
1526 static int write_partial_kvec(struct ceph_connection *con)
1530 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
1531 while (con->out_kvec_bytes > 0) {
1532 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
1533 con->out_kvec_left, con->out_kvec_bytes,
1537 con->out_kvec_bytes -= ret;
1538 if (con->out_kvec_bytes == 0)
1541 /* account for full iov entries consumed */
1542 while (ret >= con->out_kvec_cur->iov_len) {
1543 BUG_ON(!con->out_kvec_left);
1544 ret -= con->out_kvec_cur->iov_len;
1545 con->out_kvec_cur++;
1546 con->out_kvec_left--;
1548 /* and for a partially-consumed entry */
1550 con->out_kvec_cur->iov_len -= ret;
1551 con->out_kvec_cur->iov_base += ret;
1554 con->out_kvec_left = 0;
1557 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
1558 con->out_kvec_bytes, con->out_kvec_left, ret);
1559 return ret; /* done! */
1562 static u32 ceph_crc32c_page(u32 crc, struct page *page,
1563 unsigned int page_offset,
1564 unsigned int length)
1569 BUG_ON(kaddr == NULL);
1570 crc = crc32c(crc, kaddr + page_offset, length);
1576 * Write as much message data payload as we can. If we finish, queue
1578 * 1 -> done, footer is now queued in out_kvec[].
1579 * 0 -> socket full, but more to do
1582 static int write_partial_message_data(struct ceph_connection *con)
1584 struct ceph_msg *msg = con->out_msg;
1585 struct ceph_msg_data_cursor *cursor = &msg->cursor;
1586 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
1587 int more = MSG_MORE | MSG_SENDPAGE_NOTLAST;
1590 dout("%s %p msg %p\n", __func__, con, msg);
1592 if (!msg->num_data_items)
1596 * Iterate through each page that contains data to be
1597 * written, and send as much as possible for each.
1599 * If we are calculating the data crc (the default), we will
1600 * need to map the page. If we have no pages, they have
1601 * been revoked, so use the zero page.
1603 crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
1604 while (cursor->total_resid) {
1610 if (!cursor->resid) {
1611 ceph_msg_data_advance(cursor, 0);
1615 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
1616 if (length == cursor->total_resid)
1618 ret = ceph_tcp_sendpage(con->sock, page, page_offset, length,
1622 msg->footer.data_crc = cpu_to_le32(crc);
1626 if (do_datacrc && cursor->need_crc)
1627 crc = ceph_crc32c_page(crc, page, page_offset, length);
1628 ceph_msg_data_advance(cursor, (size_t)ret);
1631 dout("%s %p msg %p done\n", __func__, con, msg);
1633 /* prepare and queue up footer, too */
1635 msg->footer.data_crc = cpu_to_le32(crc);
1637 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
1638 con_out_kvec_reset(con);
1639 prepare_write_message_footer(con);
1641 return 1; /* must return > 0 to indicate success */
1647 static int write_partial_skip(struct ceph_connection *con)
1649 int more = MSG_MORE | MSG_SENDPAGE_NOTLAST;
1652 dout("%s %p %d left\n", __func__, con, con->out_skip);
1653 while (con->out_skip > 0) {
1654 size_t size = min(con->out_skip, (int) PAGE_SIZE);
1656 if (size == con->out_skip)
1658 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, more);
1661 con->out_skip -= ret;
1669 * Prepare to read connection handshake, or an ack.
1671 static void prepare_read_banner(struct ceph_connection *con)
1673 dout("prepare_read_banner %p\n", con);
1674 con->in_base_pos = 0;
1677 static void prepare_read_connect(struct ceph_connection *con)
1679 dout("prepare_read_connect %p\n", con);
1680 con->in_base_pos = 0;
1683 static void prepare_read_ack(struct ceph_connection *con)
1685 dout("prepare_read_ack %p\n", con);
1686 con->in_base_pos = 0;
1689 static void prepare_read_seq(struct ceph_connection *con)
1691 dout("prepare_read_seq %p\n", con);
1692 con->in_base_pos = 0;
1693 con->in_tag = CEPH_MSGR_TAG_SEQ;
1696 static void prepare_read_tag(struct ceph_connection *con)
1698 dout("prepare_read_tag %p\n", con);
1699 con->in_base_pos = 0;
1700 con->in_tag = CEPH_MSGR_TAG_READY;
1703 static void prepare_read_keepalive_ack(struct ceph_connection *con)
1705 dout("prepare_read_keepalive_ack %p\n", con);
1706 con->in_base_pos = 0;
1710 * Prepare to read a message.
1712 static int prepare_read_message(struct ceph_connection *con)
1714 dout("prepare_read_message %p\n", con);
1715 BUG_ON(con->in_msg != NULL);
1716 con->in_base_pos = 0;
1717 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1722 static int read_partial(struct ceph_connection *con,
1723 int end, int size, void *object)
1725 while (con->in_base_pos < end) {
1726 int left = end - con->in_base_pos;
1727 int have = size - left;
1728 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1731 con->in_base_pos += ret;
1738 * Read all or part of the connect-side handshake on a new connection
1740 static int read_partial_banner(struct ceph_connection *con)
1746 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
1749 size = strlen(CEPH_BANNER);
1751 ret = read_partial(con, end, size, con->in_banner);
1755 size = sizeof (con->actual_peer_addr);
1757 ret = read_partial(con, end, size, &con->actual_peer_addr);
1760 ceph_decode_banner_addr(&con->actual_peer_addr);
1762 size = sizeof (con->peer_addr_for_me);
1764 ret = read_partial(con, end, size, &con->peer_addr_for_me);
1767 ceph_decode_banner_addr(&con->peer_addr_for_me);
1773 static int read_partial_connect(struct ceph_connection *con)
1779 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1781 size = sizeof (con->in_reply);
1783 ret = read_partial(con, end, size, &con->in_reply);
1788 size = le32_to_cpu(con->in_reply.authorizer_len);
1789 if (size > con->auth->authorizer_reply_buf_len) {
1790 pr_err("authorizer reply too big: %d > %zu\n", size,
1791 con->auth->authorizer_reply_buf_len);
1797 ret = read_partial(con, end, size,
1798 con->auth->authorizer_reply_buf);
1803 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1804 con, (int)con->in_reply.tag,
1805 le32_to_cpu(con->in_reply.connect_seq),
1806 le32_to_cpu(con->in_reply.global_seq));
1812 * Verify the hello banner looks okay.
1814 static int verify_hello(struct ceph_connection *con)
1816 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
1817 pr_err("connect to %s got bad banner\n",
1818 ceph_pr_addr(&con->peer_addr));
1819 con->error_msg = "protocol error, bad banner";
1825 static bool addr_is_blank(struct ceph_entity_addr *addr)
1827 struct sockaddr_storage ss = addr->in_addr; /* align */
1828 struct in_addr *addr4 = &((struct sockaddr_in *)&ss)->sin_addr;
1829 struct in6_addr *addr6 = &((struct sockaddr_in6 *)&ss)->sin6_addr;
1831 switch (ss.ss_family) {
1833 return addr4->s_addr == htonl(INADDR_ANY);
1835 return ipv6_addr_any(addr6);
1841 static int addr_port(struct ceph_entity_addr *addr)
1843 switch (get_unaligned(&addr->in_addr.ss_family)) {
1845 return ntohs(get_unaligned(&((struct sockaddr_in *)&addr->in_addr)->sin_port));
1847 return ntohs(get_unaligned(&((struct sockaddr_in6 *)&addr->in_addr)->sin6_port));
1852 static void addr_set_port(struct ceph_entity_addr *addr, int p)
1854 switch (get_unaligned(&addr->in_addr.ss_family)) {
1856 put_unaligned(htons(p), &((struct sockaddr_in *)&addr->in_addr)->sin_port);
1859 put_unaligned(htons(p), &((struct sockaddr_in6 *)&addr->in_addr)->sin6_port);
1865 * Unlike other *_pton function semantics, zero indicates success.
1867 static int ceph_pton(const char *str, size_t len, struct ceph_entity_addr *addr,
1868 char delim, const char **ipend)
1870 memset(&addr->in_addr, 0, sizeof(addr->in_addr));
1872 if (in4_pton(str, len, (u8 *)&((struct sockaddr_in *)&addr->in_addr)->sin_addr.s_addr, delim, ipend)) {
1873 put_unaligned(AF_INET, &addr->in_addr.ss_family);
1877 if (in6_pton(str, len, (u8 *)&((struct sockaddr_in6 *)&addr->in_addr)->sin6_addr.s6_addr, delim, ipend)) {
1878 put_unaligned(AF_INET6, &addr->in_addr.ss_family);
1886 * Extract hostname string and resolve using kernel DNS facility.
1888 #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1889 static int ceph_dns_resolve_name(const char *name, size_t namelen,
1890 struct ceph_entity_addr *addr, char delim, const char **ipend)
1892 const char *end, *delim_p;
1893 char *colon_p, *ip_addr = NULL;
1897 * The end of the hostname occurs immediately preceding the delimiter or
1898 * the port marker (':') where the delimiter takes precedence.
1900 delim_p = memchr(name, delim, namelen);
1901 colon_p = memchr(name, ':', namelen);
1903 if (delim_p && colon_p)
1904 end = delim_p < colon_p ? delim_p : colon_p;
1905 else if (!delim_p && colon_p)
1909 if (!end) /* case: hostname:/ */
1910 end = name + namelen;
1916 /* do dns_resolve upcall */
1917 ip_len = dns_query(current->nsproxy->net_ns,
1918 NULL, name, end - name, NULL, &ip_addr, NULL, false);
1920 ret = ceph_pton(ip_addr, ip_len, addr, -1, NULL);
1928 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1929 ret, ret ? "failed" : ceph_pr_addr(addr));
1934 static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1935 struct ceph_entity_addr *addr, char delim, const char **ipend)
1942 * Parse a server name (IP or hostname). If a valid IP address is not found
1943 * then try to extract a hostname to resolve using userspace DNS upcall.
1945 static int ceph_parse_server_name(const char *name, size_t namelen,
1946 struct ceph_entity_addr *addr, char delim, const char **ipend)
1950 ret = ceph_pton(name, namelen, addr, delim, ipend);
1952 ret = ceph_dns_resolve_name(name, namelen, addr, delim, ipend);
1958 * Parse an ip[:port] list into an addr array. Use the default
1959 * monitor port if a port isn't specified.
1961 int ceph_parse_ips(const char *c, const char *end,
1962 struct ceph_entity_addr *addr,
1963 int max_count, int *count)
1965 int i, ret = -EINVAL;
1968 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1969 for (i = 0; i < max_count; i++) {
1979 ret = ceph_parse_server_name(p, end - p, &addr[i], delim, &ipend);
1988 dout("missing matching ']'\n");
1995 if (p < end && *p == ':') {
1998 while (p < end && *p >= '0' && *p <= '9') {
1999 port = (port * 10) + (*p - '0');
2003 port = CEPH_MON_PORT;
2004 else if (port > 65535)
2007 port = CEPH_MON_PORT;
2010 addr_set_port(&addr[i], port);
2011 addr[i].type = CEPH_ENTITY_ADDR_TYPE_LEGACY;
2013 dout("parse_ips got %s\n", ceph_pr_addr(&addr[i]));
2033 static int process_banner(struct ceph_connection *con)
2035 struct ceph_entity_addr *my_addr = &con->msgr->inst.addr;
2037 dout("process_banner on %p\n", con);
2039 if (verify_hello(con) < 0)
2043 * Make sure the other end is who we wanted. note that the other
2044 * end may not yet know their ip address, so if it's 0.0.0.0, give
2045 * them the benefit of the doubt.
2047 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
2048 sizeof(con->peer_addr)) != 0 &&
2049 !(addr_is_blank(&con->actual_peer_addr) &&
2050 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
2051 pr_warn("wrong peer, want %s/%u, got %s/%u\n",
2052 ceph_pr_addr(&con->peer_addr),
2053 le32_to_cpu(con->peer_addr.nonce),
2054 ceph_pr_addr(&con->actual_peer_addr),
2055 le32_to_cpu(con->actual_peer_addr.nonce));
2056 con->error_msg = "wrong peer at address";
2061 * did we learn our address?
2063 if (addr_is_blank(my_addr)) {
2064 memcpy(&my_addr->in_addr,
2065 &con->peer_addr_for_me.in_addr,
2066 sizeof(con->peer_addr_for_me.in_addr));
2067 addr_set_port(my_addr, 0);
2068 encode_my_addr(con->msgr);
2069 dout("process_banner learned my addr is %s\n",
2070 ceph_pr_addr(my_addr));
2076 static int process_connect(struct ceph_connection *con)
2078 u64 sup_feat = from_msgr(con->msgr)->supported_features;
2079 u64 req_feat = from_msgr(con->msgr)->required_features;
2080 u64 server_feat = le64_to_cpu(con->in_reply.features);
2083 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
2086 int len = le32_to_cpu(con->in_reply.authorizer_len);
2089 * Any connection that defines ->get_authorizer()
2090 * should also define ->add_authorizer_challenge() and
2091 * ->verify_authorizer_reply().
2093 * See get_connect_authorizer().
2095 if (con->in_reply.tag == CEPH_MSGR_TAG_CHALLENGE_AUTHORIZER) {
2096 ret = con->ops->add_authorizer_challenge(
2097 con, con->auth->authorizer_reply_buf, len);
2101 con_out_kvec_reset(con);
2102 __prepare_write_connect(con);
2103 prepare_read_connect(con);
2108 ret = con->ops->verify_authorizer_reply(con);
2110 con->error_msg = "bad authorize reply";
2116 switch (con->in_reply.tag) {
2117 case CEPH_MSGR_TAG_FEATURES:
2118 pr_err("%s%lld %s feature set mismatch,"
2119 " my %llx < server's %llx, missing %llx\n",
2120 ENTITY_NAME(con->peer_name),
2121 ceph_pr_addr(&con->peer_addr),
2122 sup_feat, server_feat, server_feat & ~sup_feat);
2123 con->error_msg = "missing required protocol features";
2126 case CEPH_MSGR_TAG_BADPROTOVER:
2127 pr_err("%s%lld %s protocol version mismatch,"
2128 " my %d != server's %d\n",
2129 ENTITY_NAME(con->peer_name),
2130 ceph_pr_addr(&con->peer_addr),
2131 le32_to_cpu(con->out_connect.protocol_version),
2132 le32_to_cpu(con->in_reply.protocol_version));
2133 con->error_msg = "protocol version mismatch";
2136 case CEPH_MSGR_TAG_BADAUTHORIZER:
2138 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
2140 if (con->auth_retry == 2) {
2141 con->error_msg = "connect authorization failure";
2144 con_out_kvec_reset(con);
2145 ret = prepare_write_connect(con);
2148 prepare_read_connect(con);
2151 case CEPH_MSGR_TAG_RESETSESSION:
2153 * If we connected with a large connect_seq but the peer
2154 * has no record of a session with us (no connection, or
2155 * connect_seq == 0), they will send RESETSESION to indicate
2156 * that they must have reset their session, and may have
2159 dout("process_connect got RESET peer seq %u\n",
2160 le32_to_cpu(con->in_reply.connect_seq));
2161 pr_info("%s%lld %s session reset\n",
2162 ENTITY_NAME(con->peer_name),
2163 ceph_pr_addr(&con->peer_addr));
2164 ceph_con_reset_session(con);
2165 con_out_kvec_reset(con);
2166 ret = prepare_write_connect(con);
2169 prepare_read_connect(con);
2171 /* Tell ceph about it. */
2172 mutex_unlock(&con->mutex);
2173 if (con->ops->peer_reset)
2174 con->ops->peer_reset(con);
2175 mutex_lock(&con->mutex);
2176 if (con->state != CEPH_CON_S_V1_CONNECT_MSG)
2180 case CEPH_MSGR_TAG_RETRY_SESSION:
2182 * If we sent a smaller connect_seq than the peer has, try
2183 * again with a larger value.
2185 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
2186 le32_to_cpu(con->out_connect.connect_seq),
2187 le32_to_cpu(con->in_reply.connect_seq));
2188 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
2189 con_out_kvec_reset(con);
2190 ret = prepare_write_connect(con);
2193 prepare_read_connect(con);
2196 case CEPH_MSGR_TAG_RETRY_GLOBAL:
2198 * If we sent a smaller global_seq than the peer has, try
2199 * again with a larger value.
2201 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
2202 con->peer_global_seq,
2203 le32_to_cpu(con->in_reply.global_seq));
2204 get_global_seq(con->msgr,
2205 le32_to_cpu(con->in_reply.global_seq));
2206 con_out_kvec_reset(con);
2207 ret = prepare_write_connect(con);
2210 prepare_read_connect(con);
2213 case CEPH_MSGR_TAG_SEQ:
2214 case CEPH_MSGR_TAG_READY:
2215 if (req_feat & ~server_feat) {
2216 pr_err("%s%lld %s protocol feature mismatch,"
2217 " my required %llx > server's %llx, need %llx\n",
2218 ENTITY_NAME(con->peer_name),
2219 ceph_pr_addr(&con->peer_addr),
2220 req_feat, server_feat, req_feat & ~server_feat);
2221 con->error_msg = "missing required protocol features";
2225 WARN_ON(con->state != CEPH_CON_S_V1_CONNECT_MSG);
2226 con->state = CEPH_CON_S_OPEN;
2227 con->auth_retry = 0; /* we authenticated; clear flag */
2228 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
2230 con->peer_features = server_feat;
2231 dout("process_connect got READY gseq %d cseq %d (%d)\n",
2232 con->peer_global_seq,
2233 le32_to_cpu(con->in_reply.connect_seq),
2235 WARN_ON(con->connect_seq !=
2236 le32_to_cpu(con->in_reply.connect_seq));
2238 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
2239 con_flag_set(con, CON_FLAG_LOSSYTX);
2241 con->delay = 0; /* reset backoff memory */
2243 if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
2244 prepare_write_seq(con);
2245 prepare_read_seq(con);
2247 prepare_read_tag(con);
2251 case CEPH_MSGR_TAG_WAIT:
2253 * If there is a connection race (we are opening
2254 * connections to each other), one of us may just have
2255 * to WAIT. This shouldn't happen if we are the
2258 con->error_msg = "protocol error, got WAIT as client";
2262 con->error_msg = "protocol error, garbage tag during connect";
2270 * read (part of) an ack
2272 static int read_partial_ack(struct ceph_connection *con)
2274 int size = sizeof (con->in_temp_ack);
2277 return read_partial(con, end, size, &con->in_temp_ack);
2281 * We can finally discard anything that's been acked.
2283 static void process_ack(struct ceph_connection *con)
2285 u64 ack = le64_to_cpu(con->in_temp_ack);
2287 if (con->in_tag == CEPH_MSGR_TAG_ACK)
2288 ceph_con_discard_sent(con, ack);
2290 ceph_con_discard_requeued(con, ack);
2292 prepare_read_tag(con);
2296 static int read_partial_message_section(struct ceph_connection *con,
2297 struct kvec *section,
2298 unsigned int sec_len, u32 *crc)
2304 while (section->iov_len < sec_len) {
2305 BUG_ON(section->iov_base == NULL);
2306 left = sec_len - section->iov_len;
2307 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
2308 section->iov_len, left);
2311 section->iov_len += ret;
2313 if (section->iov_len == sec_len)
2314 *crc = crc32c(0, section->iov_base, section->iov_len);
2319 static int read_partial_msg_data(struct ceph_connection *con)
2321 struct ceph_msg *msg = con->in_msg;
2322 struct ceph_msg_data_cursor *cursor = &msg->cursor;
2323 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
2330 if (!msg->num_data_items)
2334 crc = con->in_data_crc;
2335 while (cursor->total_resid) {
2336 if (!cursor->resid) {
2337 ceph_msg_data_advance(cursor, 0);
2341 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
2342 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
2345 con->in_data_crc = crc;
2351 crc = ceph_crc32c_page(crc, page, page_offset, ret);
2352 ceph_msg_data_advance(cursor, (size_t)ret);
2355 con->in_data_crc = crc;
2357 return 1; /* must return > 0 to indicate success */
2361 * read (part of) a message.
2363 static int ceph_con_in_msg_alloc(struct ceph_connection *con,
2364 struct ceph_msg_header *hdr, int *skip);
2366 static int read_partial_message(struct ceph_connection *con)
2368 struct ceph_msg *m = con->in_msg;
2372 unsigned int front_len, middle_len, data_len;
2373 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
2374 bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
2378 dout("read_partial_message con %p msg %p\n", con, m);
2381 size = sizeof (con->in_hdr);
2383 ret = read_partial(con, end, size, &con->in_hdr);
2387 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
2388 if (cpu_to_le32(crc) != con->in_hdr.crc) {
2389 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
2390 crc, con->in_hdr.crc);
2394 front_len = le32_to_cpu(con->in_hdr.front_len);
2395 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
2397 middle_len = le32_to_cpu(con->in_hdr.middle_len);
2398 if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
2400 data_len = le32_to_cpu(con->in_hdr.data_len);
2401 if (data_len > CEPH_MSG_MAX_DATA_LEN)
2405 seq = le64_to_cpu(con->in_hdr.seq);
2406 if ((s64)seq - (s64)con->in_seq < 1) {
2407 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
2408 ENTITY_NAME(con->peer_name),
2409 ceph_pr_addr(&con->peer_addr),
2410 seq, con->in_seq + 1);
2411 con->in_base_pos = -front_len - middle_len - data_len -
2413 con->in_tag = CEPH_MSGR_TAG_READY;
2415 } else if ((s64)seq - (s64)con->in_seq > 1) {
2416 pr_err("read_partial_message bad seq %lld expected %lld\n",
2417 seq, con->in_seq + 1);
2418 con->error_msg = "bad message sequence # for incoming message";
2422 /* allocate message? */
2426 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
2427 front_len, data_len);
2428 ret = ceph_con_in_msg_alloc(con, &con->in_hdr, &skip);
2432 BUG_ON(!con->in_msg ^ skip);
2434 /* skip this message */
2435 dout("alloc_msg said skip message\n");
2436 con->in_base_pos = -front_len - middle_len - data_len -
2438 con->in_tag = CEPH_MSGR_TAG_READY;
2443 BUG_ON(!con->in_msg);
2444 BUG_ON(con->in_msg->con != con);
2446 m->front.iov_len = 0; /* haven't read it yet */
2448 m->middle->vec.iov_len = 0;
2450 /* prepare for data payload, if any */
2453 prepare_message_data(con->in_msg, data_len);
2457 ret = read_partial_message_section(con, &m->front, front_len,
2458 &con->in_front_crc);
2464 ret = read_partial_message_section(con, &m->middle->vec,
2466 &con->in_middle_crc);
2473 ret = read_partial_msg_data(con);
2479 size = sizeof_footer(con);
2481 ret = read_partial(con, end, size, &m->footer);
2486 m->footer.flags = m->old_footer.flags;
2490 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
2491 m, front_len, m->footer.front_crc, middle_len,
2492 m->footer.middle_crc, data_len, m->footer.data_crc);
2495 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
2496 pr_err("read_partial_message %p front crc %u != exp. %u\n",
2497 m, con->in_front_crc, m->footer.front_crc);
2500 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
2501 pr_err("read_partial_message %p middle crc %u != exp %u\n",
2502 m, con->in_middle_crc, m->footer.middle_crc);
2506 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
2507 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
2508 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
2509 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
2513 if (need_sign && con->ops->check_message_signature &&
2514 con->ops->check_message_signature(m)) {
2515 pr_err("read_partial_message %p signature check failed\n", m);
2519 return 1; /* done! */
2523 * Process message. This happens in the worker thread. The callback should
2524 * be careful not to do anything that waits on other incoming messages or it
2527 static void process_message(struct ceph_connection *con)
2529 struct ceph_msg *msg = con->in_msg;
2531 BUG_ON(con->in_msg->con != con);
2534 /* if first message, set peer_name */
2535 if (con->peer_name.type == 0)
2536 con->peer_name = msg->hdr.src;
2539 mutex_unlock(&con->mutex);
2541 dout("===== %p %llu from %s%lld %d=%s len %d+%d+%d (%u %u %u) =====\n",
2542 msg, le64_to_cpu(msg->hdr.seq),
2543 ENTITY_NAME(msg->hdr.src),
2544 le16_to_cpu(msg->hdr.type),
2545 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2546 le32_to_cpu(msg->hdr.front_len),
2547 le32_to_cpu(msg->hdr.middle_len),
2548 le32_to_cpu(msg->hdr.data_len),
2549 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
2550 con->ops->dispatch(con, msg);
2552 mutex_lock(&con->mutex);
2555 static int read_keepalive_ack(struct ceph_connection *con)
2557 struct ceph_timespec ceph_ts;
2558 size_t size = sizeof(ceph_ts);
2559 int ret = read_partial(con, size, size, &ceph_ts);
2562 ceph_decode_timespec64(&con->last_keepalive_ack, &ceph_ts);
2563 prepare_read_tag(con);
2568 * Write something to the socket. Called in a worker thread when the
2569 * socket appears to be writeable and we have something ready to send.
2571 static int try_write(struct ceph_connection *con)
2575 dout("try_write start %p state %d\n", con, con->state);
2576 if (con->state != CEPH_CON_S_PREOPEN &&
2577 con->state != CEPH_CON_S_V1_BANNER &&
2578 con->state != CEPH_CON_S_V1_CONNECT_MSG &&
2579 con->state != CEPH_CON_S_OPEN)
2582 /* open the socket first? */
2583 if (con->state == CEPH_CON_S_PREOPEN) {
2585 con->state = CEPH_CON_S_V1_BANNER;
2587 con_out_kvec_reset(con);
2588 prepare_write_banner(con);
2589 prepare_read_banner(con);
2591 BUG_ON(con->in_msg);
2592 con->in_tag = CEPH_MSGR_TAG_READY;
2593 dout("try_write initiating connect on %p new state %d\n",
2595 ret = ceph_tcp_connect(con);
2597 con->error_msg = "connect error";
2603 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
2606 /* kvec data queued? */
2607 if (con->out_kvec_left) {
2608 ret = write_partial_kvec(con);
2612 if (con->out_skip) {
2613 ret = write_partial_skip(con);
2620 if (con->out_msg_done) {
2621 ceph_msg_put(con->out_msg);
2622 con->out_msg = NULL; /* we're done with this one */
2626 ret = write_partial_message_data(con);
2628 goto more; /* we need to send the footer, too! */
2632 dout("try_write write_partial_message_data err %d\n",
2639 if (con->state == CEPH_CON_S_OPEN) {
2640 if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
2641 prepare_write_keepalive(con);
2644 /* is anything else pending? */
2645 if (!list_empty(&con->out_queue)) {
2646 prepare_write_message(con);
2649 if (con->in_seq > con->in_seq_acked) {
2650 prepare_write_ack(con);
2655 /* Nothing to do! */
2656 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
2657 dout("try_write nothing else to write.\n");
2660 dout("try_write done on %p ret %d\n", con, ret);
2665 * Read what we can from the socket.
2667 static int try_read(struct ceph_connection *con)
2672 dout("try_read start %p state %d\n", con, con->state);
2673 if (con->state != CEPH_CON_S_V1_BANNER &&
2674 con->state != CEPH_CON_S_V1_CONNECT_MSG &&
2675 con->state != CEPH_CON_S_OPEN)
2680 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2683 if (con->state == CEPH_CON_S_V1_BANNER) {
2684 ret = read_partial_banner(con);
2687 ret = process_banner(con);
2691 con->state = CEPH_CON_S_V1_CONNECT_MSG;
2694 * Received banner is good, exchange connection info.
2695 * Do not reset out_kvec, as sending our banner raced
2696 * with receiving peer banner after connect completed.
2698 ret = prepare_write_connect(con);
2701 prepare_read_connect(con);
2703 /* Send connection info before awaiting response */
2707 if (con->state == CEPH_CON_S_V1_CONNECT_MSG) {
2708 ret = read_partial_connect(con);
2711 ret = process_connect(con);
2717 WARN_ON(con->state != CEPH_CON_S_OPEN);
2719 if (con->in_base_pos < 0) {
2721 * skipping + discarding content.
2723 ret = ceph_tcp_recvmsg(con->sock, NULL, -con->in_base_pos);
2726 dout("skipped %d / %d bytes\n", ret, -con->in_base_pos);
2727 con->in_base_pos += ret;
2728 if (con->in_base_pos)
2731 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2735 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2738 dout("try_read got tag %d\n", (int)con->in_tag);
2739 switch (con->in_tag) {
2740 case CEPH_MSGR_TAG_MSG:
2741 prepare_read_message(con);
2743 case CEPH_MSGR_TAG_ACK:
2744 prepare_read_ack(con);
2746 case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
2747 prepare_read_keepalive_ack(con);
2749 case CEPH_MSGR_TAG_CLOSE:
2750 con_close_socket(con);
2751 con->state = CEPH_CON_S_CLOSED;
2757 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2758 ret = read_partial_message(con);
2762 con->error_msg = "bad crc/signature";
2768 con->error_msg = "io error";
2773 if (con->in_tag == CEPH_MSGR_TAG_READY)
2775 process_message(con);
2776 if (con->state == CEPH_CON_S_OPEN)
2777 prepare_read_tag(con);
2780 if (con->in_tag == CEPH_MSGR_TAG_ACK ||
2781 con->in_tag == CEPH_MSGR_TAG_SEQ) {
2783 * the final handshake seq exchange is semantically
2784 * equivalent to an ACK
2786 ret = read_partial_ack(con);
2792 if (con->in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
2793 ret = read_keepalive_ack(con);
2800 dout("try_read done on %p ret %d\n", con, ret);
2804 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2805 con->error_msg = "protocol error, garbage tag";
2812 * Atomically queue work on a connection after the specified delay.
2813 * Bump @con reference to avoid races with connection teardown.
2814 * Returns 0 if work was queued, or an error code otherwise.
2816 static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
2818 if (!con->ops->get(con)) {
2819 dout("%s %p ref count 0\n", __func__, con);
2824 delay = round_jiffies_relative(delay);
2826 dout("%s %p %lu\n", __func__, con, delay);
2827 if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
2828 dout("%s %p - already queued\n", __func__, con);
2836 static void queue_con(struct ceph_connection *con)
2838 (void) queue_con_delay(con, 0);
2841 static void cancel_con(struct ceph_connection *con)
2843 if (cancel_delayed_work(&con->work)) {
2844 dout("%s %p\n", __func__, con);
2849 static bool con_sock_closed(struct ceph_connection *con)
2851 if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
2855 case CEPH_CON_S_ ## x: \
2856 con->error_msg = "socket closed (con state " #x ")"; \
2859 switch (con->state) {
2863 CASE(V1_CONNECT_MSG);
2874 static bool con_backoff(struct ceph_connection *con)
2878 if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
2881 ret = queue_con_delay(con, con->delay);
2883 dout("%s: con %p FAILED to back off %lu\n", __func__,
2885 BUG_ON(ret == -ENOENT);
2886 con_flag_set(con, CON_FLAG_BACKOFF);
2892 /* Finish fault handling; con->mutex must *not* be held here */
2894 static void con_fault_finish(struct ceph_connection *con)
2896 dout("%s %p\n", __func__, con);
2899 * in case we faulted due to authentication, invalidate our
2900 * current tickets so that we can get new ones.
2902 if (con->auth_retry) {
2903 dout("auth_retry %d, invalidating\n", con->auth_retry);
2904 if (con->ops->invalidate_authorizer)
2905 con->ops->invalidate_authorizer(con);
2906 con->auth_retry = 0;
2909 if (con->ops->fault)
2910 con->ops->fault(con);
2914 * Do some work on a connection. Drop a connection ref when we're done.
2916 static void ceph_con_workfn(struct work_struct *work)
2918 struct ceph_connection *con = container_of(work, struct ceph_connection,
2922 mutex_lock(&con->mutex);
2926 if ((fault = con_sock_closed(con))) {
2927 dout("%s: con %p SOCK_CLOSED\n", __func__, con);
2930 if (con_backoff(con)) {
2931 dout("%s: con %p BACKOFF\n", __func__, con);
2934 if (con->state == CEPH_CON_S_STANDBY) {
2935 dout("%s: con %p STANDBY\n", __func__, con);
2938 if (con->state == CEPH_CON_S_CLOSED) {
2939 dout("%s: con %p CLOSED\n", __func__, con);
2943 if (con->state == CEPH_CON_S_PREOPEN) {
2944 dout("%s: con %p PREOPEN\n", __func__, con);
2948 ret = try_read(con);
2952 if (!con->error_msg)
2953 con->error_msg = "socket error on read";
2958 ret = try_write(con);
2962 if (!con->error_msg)
2963 con->error_msg = "socket error on write";
2967 break; /* If we make it to here, we're done */
2971 mutex_unlock(&con->mutex);
2974 con_fault_finish(con);
2980 * Generic error/fault handler. A retry mechanism is used with
2981 * exponential backoff
2983 static void con_fault(struct ceph_connection *con)
2985 dout("fault %p state %d to peer %s\n",
2986 con, con->state, ceph_pr_addr(&con->peer_addr));
2988 pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
2989 ceph_pr_addr(&con->peer_addr), con->error_msg);
2990 con->error_msg = NULL;
2992 WARN_ON(con->state != CEPH_CON_S_V1_BANNER &&
2993 con->state != CEPH_CON_S_V1_CONNECT_MSG &&
2994 con->state != CEPH_CON_S_OPEN);
2996 ceph_con_reset_protocol(con);
2998 if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
2999 dout("fault on LOSSYTX channel, marking CLOSED\n");
3000 con->state = CEPH_CON_S_CLOSED;
3004 /* Requeue anything that hasn't been acked */
3005 list_splice_init(&con->out_sent, &con->out_queue);
3007 /* If there are no messages queued or keepalive pending, place
3008 * the connection in a STANDBY state */
3009 if (list_empty(&con->out_queue) &&
3010 !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
3011 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
3012 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
3013 con->state = CEPH_CON_S_STANDBY;
3015 /* retry after a delay. */
3016 con->state = CEPH_CON_S_PREOPEN;
3018 con->delay = BASE_DELAY_INTERVAL;
3019 } else if (con->delay < MAX_DELAY_INTERVAL) {
3021 if (con->delay > MAX_DELAY_INTERVAL)
3022 con->delay = MAX_DELAY_INTERVAL;
3024 con_flag_set(con, CON_FLAG_BACKOFF);
3030 void ceph_messenger_reset_nonce(struct ceph_messenger *msgr)
3032 u32 nonce = le32_to_cpu(msgr->inst.addr.nonce) + 1000000;
3033 msgr->inst.addr.nonce = cpu_to_le32(nonce);
3034 encode_my_addr(msgr);
3038 * initialize a new messenger instance
3040 void ceph_messenger_init(struct ceph_messenger *msgr,
3041 struct ceph_entity_addr *myaddr)
3043 spin_lock_init(&msgr->global_seq_lock);
3046 memcpy(&msgr->inst.addr.in_addr, &myaddr->in_addr,
3047 sizeof(msgr->inst.addr.in_addr));
3048 addr_set_port(&msgr->inst.addr, 0);
3051 msgr->inst.addr.type = 0;
3053 /* generate a random non-zero nonce */
3055 get_random_bytes(&msgr->inst.addr.nonce,
3056 sizeof(msgr->inst.addr.nonce));
3057 } while (!msgr->inst.addr.nonce);
3058 encode_my_addr(msgr);
3060 atomic_set(&msgr->stopping, 0);
3061 write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
3063 dout("%s %p\n", __func__, msgr);
3066 void ceph_messenger_fini(struct ceph_messenger *msgr)
3068 put_net(read_pnet(&msgr->net));
3071 static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con)
3074 msg->con->ops->put(msg->con);
3076 msg->con = con ? con->ops->get(con) : NULL;
3077 BUG_ON(msg->con != con);
3080 static void clear_standby(struct ceph_connection *con)
3082 /* come back from STANDBY? */
3083 if (con->state == CEPH_CON_S_STANDBY) {
3084 dout("clear_standby %p and ++connect_seq\n", con);
3085 con->state = CEPH_CON_S_PREOPEN;
3087 WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
3088 WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
3093 * Queue up an outgoing message on the given connection.
3095 * Consumes a ref on @msg.
3097 void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
3100 msg->hdr.src = con->msgr->inst.name;
3101 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
3102 msg->needs_out_seq = true;
3104 mutex_lock(&con->mutex);
3106 if (con->state == CEPH_CON_S_CLOSED) {
3107 dout("con_send %p closed, dropping %p\n", con, msg);
3109 mutex_unlock(&con->mutex);
3113 msg_con_set(msg, con);
3115 BUG_ON(!list_empty(&msg->list_head));
3116 list_add_tail(&msg->list_head, &con->out_queue);
3117 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
3118 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
3119 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
3120 le32_to_cpu(msg->hdr.front_len),
3121 le32_to_cpu(msg->hdr.middle_len),
3122 le32_to_cpu(msg->hdr.data_len));
3125 mutex_unlock(&con->mutex);
3127 /* if there wasn't anything waiting to send before, queue
3129 if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
3132 EXPORT_SYMBOL(ceph_con_send);
3135 * Revoke a message that was previously queued for send
3137 void ceph_msg_revoke(struct ceph_msg *msg)
3139 struct ceph_connection *con = msg->con;
3142 dout("%s msg %p null con\n", __func__, msg);
3143 return; /* Message not in our possession */
3146 mutex_lock(&con->mutex);
3147 if (!list_empty(&msg->list_head)) {
3148 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
3149 list_del_init(&msg->list_head);
3154 if (con->out_msg == msg) {
3155 BUG_ON(con->out_skip);
3157 if (con->out_msg_done) {
3158 con->out_skip += con_out_kvec_skip(con);
3160 BUG_ON(!msg->data_length);
3161 con->out_skip += sizeof_footer(con);
3163 /* data, middle, front */
3164 if (msg->data_length)
3165 con->out_skip += msg->cursor.total_resid;
3167 con->out_skip += con_out_kvec_skip(con);
3168 con->out_skip += con_out_kvec_skip(con);
3170 dout("%s %p msg %p - was sending, will write %d skip %d\n",
3171 __func__, con, msg, con->out_kvec_bytes, con->out_skip);
3173 con->out_msg = NULL;
3177 mutex_unlock(&con->mutex);
3181 * Revoke a message that we may be reading data into
3183 void ceph_msg_revoke_incoming(struct ceph_msg *msg)
3185 struct ceph_connection *con = msg->con;
3188 dout("%s msg %p null con\n", __func__, msg);
3189 return; /* Message not in our possession */
3192 mutex_lock(&con->mutex);
3193 if (con->in_msg == msg) {
3194 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
3195 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
3196 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
3198 /* skip rest of message */
3199 dout("%s %p msg %p revoked\n", __func__, con, msg);
3200 con->in_base_pos = con->in_base_pos -
3201 sizeof(struct ceph_msg_header) -
3205 sizeof(struct ceph_msg_footer);
3206 ceph_msg_put(con->in_msg);
3208 con->in_tag = CEPH_MSGR_TAG_READY;
3211 dout("%s %p in_msg %p msg %p no-op\n",
3212 __func__, con, con->in_msg, msg);
3214 mutex_unlock(&con->mutex);
3218 * Queue a keepalive byte to ensure the tcp connection is alive.
3220 void ceph_con_keepalive(struct ceph_connection *con)
3222 dout("con_keepalive %p\n", con);
3223 mutex_lock(&con->mutex);
3225 con_flag_set(con, CON_FLAG_KEEPALIVE_PENDING);
3226 mutex_unlock(&con->mutex);
3228 if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
3231 EXPORT_SYMBOL(ceph_con_keepalive);
3233 bool ceph_con_keepalive_expired(struct ceph_connection *con,
3234 unsigned long interval)
3237 (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) {
3238 struct timespec64 now;
3239 struct timespec64 ts;
3240 ktime_get_real_ts64(&now);
3241 jiffies_to_timespec64(interval, &ts);
3242 ts = timespec64_add(con->last_keepalive_ack, ts);
3243 return timespec64_compare(&now, &ts) >= 0;
3248 static struct ceph_msg_data *ceph_msg_data_add(struct ceph_msg *msg)
3250 BUG_ON(msg->num_data_items >= msg->max_data_items);
3251 return &msg->data[msg->num_data_items++];
3254 static void ceph_msg_data_destroy(struct ceph_msg_data *data)
3256 if (data->type == CEPH_MSG_DATA_PAGES && data->own_pages) {
3257 int num_pages = calc_pages_for(data->alignment, data->length);
3258 ceph_release_page_vector(data->pages, num_pages);
3259 } else if (data->type == CEPH_MSG_DATA_PAGELIST) {
3260 ceph_pagelist_release(data->pagelist);
3264 void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
3265 size_t length, size_t alignment, bool own_pages)
3267 struct ceph_msg_data *data;
3272 data = ceph_msg_data_add(msg);
3273 data->type = CEPH_MSG_DATA_PAGES;
3274 data->pages = pages;
3275 data->length = length;
3276 data->alignment = alignment & ~PAGE_MASK;
3277 data->own_pages = own_pages;
3279 msg->data_length += length;
3281 EXPORT_SYMBOL(ceph_msg_data_add_pages);
3283 void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
3284 struct ceph_pagelist *pagelist)
3286 struct ceph_msg_data *data;
3289 BUG_ON(!pagelist->length);
3291 data = ceph_msg_data_add(msg);
3292 data->type = CEPH_MSG_DATA_PAGELIST;
3293 refcount_inc(&pagelist->refcnt);
3294 data->pagelist = pagelist;
3296 msg->data_length += pagelist->length;
3298 EXPORT_SYMBOL(ceph_msg_data_add_pagelist);
3301 void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
3304 struct ceph_msg_data *data;
3306 data = ceph_msg_data_add(msg);
3307 data->type = CEPH_MSG_DATA_BIO;
3308 data->bio_pos = *bio_pos;
3309 data->bio_length = length;
3311 msg->data_length += length;
3313 EXPORT_SYMBOL(ceph_msg_data_add_bio);
3314 #endif /* CONFIG_BLOCK */
3316 void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
3317 struct ceph_bvec_iter *bvec_pos)
3319 struct ceph_msg_data *data;
3321 data = ceph_msg_data_add(msg);
3322 data->type = CEPH_MSG_DATA_BVECS;
3323 data->bvec_pos = *bvec_pos;
3325 msg->data_length += bvec_pos->iter.bi_size;
3327 EXPORT_SYMBOL(ceph_msg_data_add_bvecs);
3330 * construct a new message with given type, size
3331 * the new msg has a ref count of 1.
3333 struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items,
3334 gfp_t flags, bool can_fail)
3338 m = kmem_cache_zalloc(ceph_msg_cache, flags);
3342 m->hdr.type = cpu_to_le16(type);
3343 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
3344 m->hdr.front_len = cpu_to_le32(front_len);
3346 INIT_LIST_HEAD(&m->list_head);
3347 kref_init(&m->kref);
3351 m->front.iov_base = ceph_kvmalloc(front_len, flags);
3352 if (m->front.iov_base == NULL) {
3353 dout("ceph_msg_new can't allocate %d bytes\n",
3358 m->front.iov_base = NULL;
3360 m->front_alloc_len = m->front.iov_len = front_len;
3362 if (max_data_items) {
3363 m->data = kmalloc_array(max_data_items, sizeof(*m->data),
3368 m->max_data_items = max_data_items;
3371 dout("ceph_msg_new %p front %d\n", m, front_len);
3378 pr_err("msg_new can't create type %d front %d\n", type,
3382 dout("msg_new can't create type %d front %d\n", type,
3387 EXPORT_SYMBOL(ceph_msg_new2);
3389 struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
3392 return ceph_msg_new2(type, front_len, 0, flags, can_fail);
3394 EXPORT_SYMBOL(ceph_msg_new);
3397 * Allocate "middle" portion of a message, if it is needed and wasn't
3398 * allocated by alloc_msg. This allows us to read a small fixed-size
3399 * per-type header in the front and then gracefully fail (i.e.,
3400 * propagate the error to the caller based on info in the front) when
3401 * the middle is too large.
3403 static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
3405 int type = le16_to_cpu(msg->hdr.type);
3406 int middle_len = le32_to_cpu(msg->hdr.middle_len);
3408 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
3409 ceph_msg_type_name(type), middle_len);
3410 BUG_ON(!middle_len);
3411 BUG_ON(msg->middle);
3413 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
3420 * Allocate a message for receiving an incoming message on a
3421 * connection, and save the result in con->in_msg. Uses the
3422 * connection's private alloc_msg op if available.
3424 * Returns 0 on success, or a negative error code.
3426 * On success, if we set *skip = 1:
3427 * - the next message should be skipped and ignored.
3428 * - con->in_msg == NULL
3429 * or if we set *skip = 0:
3430 * - con->in_msg is non-null.
3431 * On error (ENOMEM, EAGAIN, ...),
3432 * - con->in_msg == NULL
3434 static int ceph_con_in_msg_alloc(struct ceph_connection *con,
3435 struct ceph_msg_header *hdr, int *skip)
3437 int middle_len = le32_to_cpu(hdr->middle_len);
3438 struct ceph_msg *msg;
3441 BUG_ON(con->in_msg != NULL);
3442 BUG_ON(!con->ops->alloc_msg);
3444 mutex_unlock(&con->mutex);
3445 msg = con->ops->alloc_msg(con, hdr, skip);
3446 mutex_lock(&con->mutex);
3447 if (con->state != CEPH_CON_S_OPEN) {
3454 msg_con_set(msg, con);
3458 * Null message pointer means either we should skip
3459 * this message or we couldn't allocate memory. The
3460 * former is not an error.
3465 con->error_msg = "error allocating memory for incoming message";
3468 memcpy(&con->in_msg->hdr, hdr, sizeof(*hdr));
3470 if (middle_len && !con->in_msg->middle) {
3471 ret = ceph_alloc_middle(con, con->in_msg);
3473 ceph_msg_put(con->in_msg);
3481 static void ceph_con_get_out_msg(struct ceph_connection *con)
3483 struct ceph_msg *msg;
3485 BUG_ON(list_empty(&con->out_queue));
3486 msg = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
3487 WARN_ON(msg->con != con);
3490 * Put the message on "sent" list using a ref from ceph_con_send().
3491 * It is put when the message is acked or revoked.
3493 list_move_tail(&msg->list_head, &con->out_sent);
3496 * Only assign outgoing seq # if we haven't sent this message
3497 * yet. If it is requeued, resend with it's original seq.
3499 if (msg->needs_out_seq) {
3500 msg->hdr.seq = cpu_to_le64(++con->out_seq);
3501 msg->needs_out_seq = false;
3503 if (con->ops->reencode_message)
3504 con->ops->reencode_message(msg);
3508 * Get a ref for out_msg. It is put when we are done sending the
3509 * message or in case of a fault.
3511 WARN_ON(con->out_msg);
3512 con->out_msg = ceph_msg_get(msg);
3516 * Free a generically kmalloc'd message.
3518 static void ceph_msg_free(struct ceph_msg *m)
3520 dout("%s %p\n", __func__, m);
3521 kvfree(m->front.iov_base);
3523 kmem_cache_free(ceph_msg_cache, m);
3526 static void ceph_msg_release(struct kref *kref)
3528 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
3531 dout("%s %p\n", __func__, m);
3532 WARN_ON(!list_empty(&m->list_head));
3534 msg_con_set(m, NULL);
3536 /* drop middle, data, if any */
3538 ceph_buffer_put(m->middle);
3542 for (i = 0; i < m->num_data_items; i++)
3543 ceph_msg_data_destroy(&m->data[i]);
3546 ceph_msgpool_put(m->pool, m);
3551 struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
3553 dout("%s %p (was %d)\n", __func__, msg,
3554 kref_read(&msg->kref));
3555 kref_get(&msg->kref);
3558 EXPORT_SYMBOL(ceph_msg_get);
3560 void ceph_msg_put(struct ceph_msg *msg)
3562 dout("%s %p (was %d)\n", __func__, msg,
3563 kref_read(&msg->kref));
3564 kref_put(&msg->kref, ceph_msg_release);
3566 EXPORT_SYMBOL(ceph_msg_put);
3568 void ceph_msg_dump(struct ceph_msg *msg)
3570 pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg,
3571 msg->front_alloc_len, msg->data_length);
3572 print_hex_dump(KERN_DEBUG, "header: ",
3573 DUMP_PREFIX_OFFSET, 16, 1,
3574 &msg->hdr, sizeof(msg->hdr), true);
3575 print_hex_dump(KERN_DEBUG, " front: ",
3576 DUMP_PREFIX_OFFSET, 16, 1,
3577 msg->front.iov_base, msg->front.iov_len, true);
3579 print_hex_dump(KERN_DEBUG, "middle: ",
3580 DUMP_PREFIX_OFFSET, 16, 1,
3581 msg->middle->vec.iov_base,
3582 msg->middle->vec.iov_len, true);
3583 print_hex_dump(KERN_DEBUG, "footer: ",
3584 DUMP_PREFIX_OFFSET, 16, 1,
3585 &msg->footer, sizeof(msg->footer), true);
3587 EXPORT_SYMBOL(ceph_msg_dump);