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 */
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 */
96 * ceph_connection flag bits
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 */
105 static bool con_flag_valid(unsigned long 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:
119 static void con_flag_clear(struct ceph_connection *con, unsigned long con_flag)
121 BUG_ON(!con_flag_valid(con_flag));
123 clear_bit(con_flag, &con->flags);
126 static void con_flag_set(struct ceph_connection *con, unsigned long con_flag)
128 BUG_ON(!con_flag_valid(con_flag));
130 set_bit(con_flag, &con->flags);
133 static bool con_flag_test(struct ceph_connection *con, unsigned long con_flag)
135 BUG_ON(!con_flag_valid(con_flag));
137 return test_bit(con_flag, &con->flags);
140 static bool con_flag_test_and_clear(struct ceph_connection *con,
141 unsigned long con_flag)
143 BUG_ON(!con_flag_valid(con_flag));
145 return test_and_clear_bit(con_flag, &con->flags);
148 static bool con_flag_test_and_set(struct ceph_connection *con,
149 unsigned long con_flag)
151 BUG_ON(!con_flag_valid(con_flag));
153 return test_and_set_bit(con_flag, &con->flags);
156 /* Slab caches for frequently-allocated structures */
158 static struct kmem_cache *ceph_msg_cache;
160 /* static tag bytes (protocol control messages) */
161 static char tag_msg = CEPH_MSGR_TAG_MSG;
162 static char tag_ack = CEPH_MSGR_TAG_ACK;
163 static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
164 static char tag_keepalive2 = CEPH_MSGR_TAG_KEEPALIVE2;
166 #ifdef CONFIG_LOCKDEP
167 static struct lock_class_key socket_class;
170 static void queue_con(struct ceph_connection *con);
171 static void cancel_con(struct ceph_connection *con);
172 static void ceph_con_workfn(struct work_struct *);
173 static void con_fault(struct ceph_connection *con);
176 * Nicely render a sockaddr as a string. An array of formatted
177 * strings is used, to approximate reentrancy.
179 #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
180 #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
181 #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
182 #define MAX_ADDR_STR_LEN 64 /* 54 is enough */
184 static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
185 static atomic_t addr_str_seq = ATOMIC_INIT(0);
187 static struct page *zero_page; /* used in certain error cases */
189 const char *ceph_pr_addr(const struct sockaddr_storage *ss)
193 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
194 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
196 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
199 switch (ss->ss_family) {
201 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
202 ntohs(in4->sin_port));
206 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
207 ntohs(in6->sin6_port));
211 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
217 EXPORT_SYMBOL(ceph_pr_addr);
219 static void encode_my_addr(struct ceph_messenger *msgr)
221 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
222 ceph_encode_addr(&msgr->my_enc_addr);
226 * work queue for all reading and writing to/from the socket.
228 static struct workqueue_struct *ceph_msgr_wq;
230 static int ceph_msgr_slab_init(void)
232 BUG_ON(ceph_msg_cache);
233 ceph_msg_cache = KMEM_CACHE(ceph_msg, 0);
240 static void ceph_msgr_slab_exit(void)
242 BUG_ON(!ceph_msg_cache);
243 kmem_cache_destroy(ceph_msg_cache);
244 ceph_msg_cache = NULL;
247 static void _ceph_msgr_exit(void)
250 destroy_workqueue(ceph_msgr_wq);
254 BUG_ON(zero_page == NULL);
258 ceph_msgr_slab_exit();
261 int __init ceph_msgr_init(void)
263 if (ceph_msgr_slab_init())
266 BUG_ON(zero_page != NULL);
267 zero_page = ZERO_PAGE(0);
271 * The number of active work items is limited by the number of
272 * connections, so leave @max_active at default.
274 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_MEM_RECLAIM, 0);
278 pr_err("msgr_init failed to create workqueue\n");
284 void ceph_msgr_exit(void)
286 BUG_ON(ceph_msgr_wq == NULL);
291 void ceph_msgr_flush(void)
293 flush_workqueue(ceph_msgr_wq);
295 EXPORT_SYMBOL(ceph_msgr_flush);
297 /* Connection socket state transition functions */
299 static void con_sock_state_init(struct ceph_connection *con)
303 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
304 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
305 printk("%s: unexpected old state %d\n", __func__, old_state);
306 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
307 CON_SOCK_STATE_CLOSED);
310 static void con_sock_state_connecting(struct ceph_connection *con)
314 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
315 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
316 printk("%s: unexpected old state %d\n", __func__, old_state);
317 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
318 CON_SOCK_STATE_CONNECTING);
321 static void con_sock_state_connected(struct ceph_connection *con)
325 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
326 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
327 printk("%s: unexpected old state %d\n", __func__, old_state);
328 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
329 CON_SOCK_STATE_CONNECTED);
332 static void con_sock_state_closing(struct ceph_connection *con)
336 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
337 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
338 old_state != CON_SOCK_STATE_CONNECTED &&
339 old_state != CON_SOCK_STATE_CLOSING))
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_CLOSING);
345 static void con_sock_state_closed(struct ceph_connection *con)
349 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
350 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
351 old_state != CON_SOCK_STATE_CLOSING &&
352 old_state != CON_SOCK_STATE_CONNECTING &&
353 old_state != CON_SOCK_STATE_CLOSED))
354 printk("%s: unexpected old state %d\n", __func__, old_state);
355 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
356 CON_SOCK_STATE_CLOSED);
360 * socket callback functions
363 /* data available on socket, or listen socket received a connect */
364 static void ceph_sock_data_ready(struct sock *sk)
366 struct ceph_connection *con = sk->sk_user_data;
367 if (atomic_read(&con->msgr->stopping)) {
371 if (sk->sk_state != TCP_CLOSE_WAIT) {
372 dout("%s on %p state = %lu, queueing work\n", __func__,
378 /* socket has buffer space for writing */
379 static void ceph_sock_write_space(struct sock *sk)
381 struct ceph_connection *con = sk->sk_user_data;
383 /* only queue to workqueue if there is data we want to write,
384 * and there is sufficient space in the socket buffer to accept
385 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
386 * doesn't get called again until try_write() fills the socket
387 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
388 * and net/core/stream.c:sk_stream_write_space().
390 if (con_flag_test(con, CON_FLAG_WRITE_PENDING)) {
391 if (sk_stream_is_writeable(sk)) {
392 dout("%s %p queueing write work\n", __func__, con);
393 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
397 dout("%s %p nothing to write\n", __func__, con);
401 /* socket's state has changed */
402 static void ceph_sock_state_change(struct sock *sk)
404 struct ceph_connection *con = sk->sk_user_data;
406 dout("%s %p state = %lu sk_state = %u\n", __func__,
407 con, con->state, sk->sk_state);
409 switch (sk->sk_state) {
411 dout("%s TCP_CLOSE\n", __func__);
414 dout("%s TCP_CLOSE_WAIT\n", __func__);
415 con_sock_state_closing(con);
416 con_flag_set(con, CON_FLAG_SOCK_CLOSED);
419 case TCP_ESTABLISHED:
420 dout("%s TCP_ESTABLISHED\n", __func__);
421 con_sock_state_connected(con);
424 default: /* Everything else is uninteresting */
430 * set up socket callbacks
432 static void set_sock_callbacks(struct socket *sock,
433 struct ceph_connection *con)
435 struct sock *sk = sock->sk;
436 sk->sk_user_data = con;
437 sk->sk_data_ready = ceph_sock_data_ready;
438 sk->sk_write_space = ceph_sock_write_space;
439 sk->sk_state_change = ceph_sock_state_change;
448 * initiate connection to a remote socket.
450 static int ceph_tcp_connect(struct ceph_connection *con)
452 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
454 unsigned int noio_flag;
459 /* sock_create_kern() allocates with GFP_KERNEL */
460 noio_flag = memalloc_noio_save();
461 ret = sock_create_kern(read_pnet(&con->msgr->net), paddr->ss_family,
462 SOCK_STREAM, IPPROTO_TCP, &sock);
463 memalloc_noio_restore(noio_flag);
466 sock->sk->sk_allocation = GFP_NOFS;
468 #ifdef CONFIG_LOCKDEP
469 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
472 set_sock_callbacks(sock, con);
474 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
476 con_sock_state_connecting(con);
477 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
479 if (ret == -EINPROGRESS) {
480 dout("connect %s EINPROGRESS sk_state = %u\n",
481 ceph_pr_addr(&con->peer_addr.in_addr),
483 } else if (ret < 0) {
484 pr_err("connect %s error %d\n",
485 ceph_pr_addr(&con->peer_addr.in_addr), ret);
490 if (ceph_test_opt(from_msgr(con->msgr), TCP_NODELAY)) {
493 ret = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
494 (char *)&optval, sizeof(optval));
496 pr_err("kernel_setsockopt(TCP_NODELAY) failed: %d",
505 * If @buf is NULL, discard up to @len bytes.
507 static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
509 struct kvec iov = {buf, len};
510 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
514 msg.msg_flags |= MSG_TRUNC;
516 iov_iter_kvec(&msg.msg_iter, READ, &iov, 1, len);
517 r = sock_recvmsg(sock, &msg, msg.msg_flags);
523 static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
524 int page_offset, size_t length)
526 struct bio_vec bvec = {
528 .bv_offset = page_offset,
531 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
534 BUG_ON(page_offset + length > PAGE_SIZE);
535 iov_iter_bvec(&msg.msg_iter, READ, &bvec, 1, length);
536 r = sock_recvmsg(sock, &msg, msg.msg_flags);
543 * write something. @more is true if caller will be sending more data
546 static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
547 size_t kvlen, size_t len, int more)
549 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
553 msg.msg_flags |= MSG_MORE;
555 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
557 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
563 static int __ceph_tcp_sendpage(struct socket *sock, struct page *page,
564 int offset, size_t size, bool more)
566 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
569 ret = kernel_sendpage(sock, page, offset, size, flags);
576 static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
577 int offset, size_t size, bool more)
579 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
584 * sendpage cannot properly handle pages with page_count == 0,
585 * we need to fall back to sendmsg if that's the case.
587 * Same goes for slab pages: skb_can_coalesce() allows
588 * coalescing neighboring slab objects into a single frag which
589 * triggers one of hardened usercopy checks.
591 if (page_count(page) >= 1 && !PageSlab(page))
592 return __ceph_tcp_sendpage(sock, page, offset, size, more);
595 bvec.bv_offset = offset;
599 msg.msg_flags |= MSG_MORE;
601 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
603 iov_iter_bvec(&msg.msg_iter, WRITE, &bvec, 1, size);
604 ret = sock_sendmsg(sock, &msg);
612 * Shutdown/close the socket for the given connection.
614 static int con_close_socket(struct ceph_connection *con)
618 dout("con_close_socket on %p sock %p\n", con, con->sock);
620 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
621 sock_release(con->sock);
626 * Forcibly clear the SOCK_CLOSED flag. It gets set
627 * independent of the connection mutex, and we could have
628 * received a socket close event before we had the chance to
629 * shut the socket down.
631 con_flag_clear(con, CON_FLAG_SOCK_CLOSED);
633 con_sock_state_closed(con);
638 * Reset a connection. Discard all incoming and outgoing messages
639 * and clear *_seq state.
641 static void ceph_msg_remove(struct ceph_msg *msg)
643 list_del_init(&msg->list_head);
647 static void ceph_msg_remove_list(struct list_head *head)
649 while (!list_empty(head)) {
650 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
652 ceph_msg_remove(msg);
656 static void reset_connection(struct ceph_connection *con)
658 /* reset connection, out_queue, msg_ and connect_seq */
659 /* discard existing out_queue and msg_seq */
660 dout("reset_connection %p\n", con);
661 ceph_msg_remove_list(&con->out_queue);
662 ceph_msg_remove_list(&con->out_sent);
665 BUG_ON(con->in_msg->con != con);
666 ceph_msg_put(con->in_msg);
670 con->connect_seq = 0;
673 BUG_ON(con->out_msg->con != con);
674 ceph_msg_put(con->out_msg);
678 con->in_seq_acked = 0;
684 * mark a peer down. drop any open connections.
686 void ceph_con_close(struct ceph_connection *con)
688 mutex_lock(&con->mutex);
689 dout("con_close %p peer %s\n", con,
690 ceph_pr_addr(&con->peer_addr.in_addr));
691 con->state = CON_STATE_CLOSED;
693 con_flag_clear(con, CON_FLAG_LOSSYTX); /* so we retry next connect */
694 con_flag_clear(con, CON_FLAG_KEEPALIVE_PENDING);
695 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
696 con_flag_clear(con, CON_FLAG_BACKOFF);
698 reset_connection(con);
699 con->peer_global_seq = 0;
701 con_close_socket(con);
702 mutex_unlock(&con->mutex);
704 EXPORT_SYMBOL(ceph_con_close);
707 * Reopen a closed connection, with a new peer address.
709 void ceph_con_open(struct ceph_connection *con,
710 __u8 entity_type, __u64 entity_num,
711 struct ceph_entity_addr *addr)
713 mutex_lock(&con->mutex);
714 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
716 WARN_ON(con->state != CON_STATE_CLOSED);
717 con->state = CON_STATE_PREOPEN;
719 con->peer_name.type = (__u8) entity_type;
720 con->peer_name.num = cpu_to_le64(entity_num);
722 memcpy(&con->peer_addr, addr, sizeof(*addr));
723 con->delay = 0; /* reset backoff memory */
724 mutex_unlock(&con->mutex);
727 EXPORT_SYMBOL(ceph_con_open);
730 * return true if this connection ever successfully opened
732 bool ceph_con_opened(struct ceph_connection *con)
734 return con->connect_seq > 0;
738 * initialize a new connection.
740 void ceph_con_init(struct ceph_connection *con, void *private,
741 const struct ceph_connection_operations *ops,
742 struct ceph_messenger *msgr)
744 dout("con_init %p\n", con);
745 memset(con, 0, sizeof(*con));
746 con->private = private;
750 con_sock_state_init(con);
752 mutex_init(&con->mutex);
753 INIT_LIST_HEAD(&con->out_queue);
754 INIT_LIST_HEAD(&con->out_sent);
755 INIT_DELAYED_WORK(&con->work, ceph_con_workfn);
757 con->state = CON_STATE_CLOSED;
759 EXPORT_SYMBOL(ceph_con_init);
763 * We maintain a global counter to order connection attempts. Get
764 * a unique seq greater than @gt.
766 static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
770 spin_lock(&msgr->global_seq_lock);
771 if (msgr->global_seq < gt)
772 msgr->global_seq = gt;
773 ret = ++msgr->global_seq;
774 spin_unlock(&msgr->global_seq_lock);
778 static void con_out_kvec_reset(struct ceph_connection *con)
780 BUG_ON(con->out_skip);
782 con->out_kvec_left = 0;
783 con->out_kvec_bytes = 0;
784 con->out_kvec_cur = &con->out_kvec[0];
787 static void con_out_kvec_add(struct ceph_connection *con,
788 size_t size, void *data)
790 int index = con->out_kvec_left;
792 BUG_ON(con->out_skip);
793 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
795 con->out_kvec[index].iov_len = size;
796 con->out_kvec[index].iov_base = data;
797 con->out_kvec_left++;
798 con->out_kvec_bytes += size;
802 * Chop off a kvec from the end. Return residual number of bytes for
803 * that kvec, i.e. how many bytes would have been written if the kvec
806 static int con_out_kvec_skip(struct ceph_connection *con)
808 int off = con->out_kvec_cur - con->out_kvec;
811 if (con->out_kvec_bytes > 0) {
812 skip = con->out_kvec[off + con->out_kvec_left - 1].iov_len;
813 BUG_ON(con->out_kvec_bytes < skip);
814 BUG_ON(!con->out_kvec_left);
815 con->out_kvec_bytes -= skip;
816 con->out_kvec_left--;
825 * For a bio data item, a piece is whatever remains of the next
826 * entry in the current bio iovec, or the first entry in the next
829 static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor,
832 struct ceph_msg_data *data = cursor->data;
833 struct ceph_bio_iter *it = &cursor->bio_iter;
835 cursor->resid = min_t(size_t, length, data->bio_length);
837 if (cursor->resid < it->iter.bi_size)
838 it->iter.bi_size = cursor->resid;
840 BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter));
841 cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter);
844 static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor,
848 struct bio_vec bv = bio_iter_iovec(cursor->bio_iter.bio,
849 cursor->bio_iter.iter);
851 *page_offset = bv.bv_offset;
856 static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
859 struct ceph_bio_iter *it = &cursor->bio_iter;
861 BUG_ON(bytes > cursor->resid);
862 BUG_ON(bytes > bio_iter_len(it->bio, it->iter));
863 cursor->resid -= bytes;
864 bio_advance_iter(it->bio, &it->iter, bytes);
866 if (!cursor->resid) {
867 BUG_ON(!cursor->last_piece);
868 return false; /* no more data */
871 if (!bytes || (it->iter.bi_size && it->iter.bi_bvec_done))
872 return false; /* more bytes to process in this segment */
874 if (!it->iter.bi_size) {
875 it->bio = it->bio->bi_next;
876 it->iter = it->bio->bi_iter;
877 if (cursor->resid < it->iter.bi_size)
878 it->iter.bi_size = cursor->resid;
881 BUG_ON(cursor->last_piece);
882 BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter));
883 cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter);
886 #endif /* CONFIG_BLOCK */
888 static void ceph_msg_data_bvecs_cursor_init(struct ceph_msg_data_cursor *cursor,
891 struct ceph_msg_data *data = cursor->data;
892 struct bio_vec *bvecs = data->bvec_pos.bvecs;
894 cursor->resid = min_t(size_t, length, data->bvec_pos.iter.bi_size);
895 cursor->bvec_iter = data->bvec_pos.iter;
896 cursor->bvec_iter.bi_size = cursor->resid;
898 BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter));
900 cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter);
903 static struct page *ceph_msg_data_bvecs_next(struct ceph_msg_data_cursor *cursor,
907 struct bio_vec bv = bvec_iter_bvec(cursor->data->bvec_pos.bvecs,
910 *page_offset = bv.bv_offset;
915 static bool ceph_msg_data_bvecs_advance(struct ceph_msg_data_cursor *cursor,
918 struct bio_vec *bvecs = cursor->data->bvec_pos.bvecs;
920 BUG_ON(bytes > cursor->resid);
921 BUG_ON(bytes > bvec_iter_len(bvecs, cursor->bvec_iter));
922 cursor->resid -= bytes;
923 bvec_iter_advance(bvecs, &cursor->bvec_iter, bytes);
925 if (!cursor->resid) {
926 BUG_ON(!cursor->last_piece);
927 return false; /* no more data */
930 if (!bytes || cursor->bvec_iter.bi_bvec_done)
931 return false; /* more bytes to process in this segment */
933 BUG_ON(cursor->last_piece);
934 BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter));
936 cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter);
941 * For a page array, a piece comes from the first page in the array
942 * that has not already been fully consumed.
944 static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor,
947 struct ceph_msg_data *data = cursor->data;
950 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
952 BUG_ON(!data->pages);
953 BUG_ON(!data->length);
955 cursor->resid = min(length, data->length);
956 page_count = calc_pages_for(data->alignment, (u64)data->length);
957 cursor->page_offset = data->alignment & ~PAGE_MASK;
958 cursor->page_index = 0;
959 BUG_ON(page_count > (int)USHRT_MAX);
960 cursor->page_count = (unsigned short)page_count;
961 BUG_ON(length > SIZE_MAX - cursor->page_offset);
962 cursor->last_piece = cursor->page_offset + cursor->resid <= PAGE_SIZE;
966 ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
967 size_t *page_offset, size_t *length)
969 struct ceph_msg_data *data = cursor->data;
971 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
973 BUG_ON(cursor->page_index >= cursor->page_count);
974 BUG_ON(cursor->page_offset >= PAGE_SIZE);
976 *page_offset = cursor->page_offset;
977 if (cursor->last_piece)
978 *length = cursor->resid;
980 *length = PAGE_SIZE - *page_offset;
982 return data->pages[cursor->page_index];
985 static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
988 BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES);
990 BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
992 /* Advance the cursor page offset */
994 cursor->resid -= bytes;
995 cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK;
996 if (!bytes || cursor->page_offset)
997 return false; /* more bytes to process in the current page */
1000 return false; /* no more data */
1002 /* Move on to the next page; offset is already at 0 */
1004 BUG_ON(cursor->page_index >= cursor->page_count);
1005 cursor->page_index++;
1006 cursor->last_piece = cursor->resid <= PAGE_SIZE;
1012 * For a pagelist, a piece is whatever remains to be consumed in the
1013 * first page in the list, or the front of the next page.
1016 ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
1019 struct ceph_msg_data *data = cursor->data;
1020 struct ceph_pagelist *pagelist;
1023 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1025 pagelist = data->pagelist;
1029 return; /* pagelist can be assigned but empty */
1031 BUG_ON(list_empty(&pagelist->head));
1032 page = list_first_entry(&pagelist->head, struct page, lru);
1034 cursor->resid = min(length, pagelist->length);
1035 cursor->page = page;
1037 cursor->last_piece = cursor->resid <= PAGE_SIZE;
1040 static struct page *
1041 ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
1042 size_t *page_offset, size_t *length)
1044 struct ceph_msg_data *data = cursor->data;
1045 struct ceph_pagelist *pagelist;
1047 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1049 pagelist = data->pagelist;
1052 BUG_ON(!cursor->page);
1053 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
1055 /* offset of first page in pagelist is always 0 */
1056 *page_offset = cursor->offset & ~PAGE_MASK;
1057 if (cursor->last_piece)
1058 *length = cursor->resid;
1060 *length = PAGE_SIZE - *page_offset;
1062 return cursor->page;
1065 static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
1068 struct ceph_msg_data *data = cursor->data;
1069 struct ceph_pagelist *pagelist;
1071 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1073 pagelist = data->pagelist;
1076 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
1077 BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
1079 /* Advance the cursor offset */
1081 cursor->resid -= bytes;
1082 cursor->offset += bytes;
1083 /* offset of first page in pagelist is always 0 */
1084 if (!bytes || cursor->offset & ~PAGE_MASK)
1085 return false; /* more bytes to process in the current page */
1088 return false; /* no more data */
1090 /* Move on to the next page */
1092 BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
1093 cursor->page = list_next_entry(cursor->page, lru);
1094 cursor->last_piece = cursor->resid <= PAGE_SIZE;
1100 * Message data is handled (sent or received) in pieces, where each
1101 * piece resides on a single page. The network layer might not
1102 * consume an entire piece at once. A data item's cursor keeps
1103 * track of which piece is next to process and how much remains to
1104 * be processed in that piece. It also tracks whether the current
1105 * piece is the last one in the data item.
1107 static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor)
1109 size_t length = cursor->total_resid;
1111 switch (cursor->data->type) {
1112 case CEPH_MSG_DATA_PAGELIST:
1113 ceph_msg_data_pagelist_cursor_init(cursor, length);
1115 case CEPH_MSG_DATA_PAGES:
1116 ceph_msg_data_pages_cursor_init(cursor, length);
1119 case CEPH_MSG_DATA_BIO:
1120 ceph_msg_data_bio_cursor_init(cursor, length);
1122 #endif /* CONFIG_BLOCK */
1123 case CEPH_MSG_DATA_BVECS:
1124 ceph_msg_data_bvecs_cursor_init(cursor, length);
1126 case CEPH_MSG_DATA_NONE:
1131 cursor->need_crc = true;
1134 static void ceph_msg_data_cursor_init(struct ceph_msg *msg, size_t length)
1136 struct ceph_msg_data_cursor *cursor = &msg->cursor;
1139 BUG_ON(length > msg->data_length);
1140 BUG_ON(!msg->num_data_items);
1142 cursor->total_resid = length;
1143 cursor->data = msg->data;
1145 __ceph_msg_data_cursor_init(cursor);
1149 * Return the page containing the next piece to process for a given
1150 * data item, and supply the page offset and length of that piece.
1151 * Indicate whether this is the last piece in this data item.
1153 static struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
1154 size_t *page_offset, size_t *length,
1159 switch (cursor->data->type) {
1160 case CEPH_MSG_DATA_PAGELIST:
1161 page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
1163 case CEPH_MSG_DATA_PAGES:
1164 page = ceph_msg_data_pages_next(cursor, page_offset, length);
1167 case CEPH_MSG_DATA_BIO:
1168 page = ceph_msg_data_bio_next(cursor, page_offset, length);
1170 #endif /* CONFIG_BLOCK */
1171 case CEPH_MSG_DATA_BVECS:
1172 page = ceph_msg_data_bvecs_next(cursor, page_offset, length);
1174 case CEPH_MSG_DATA_NONE:
1181 BUG_ON(*page_offset + *length > PAGE_SIZE);
1183 BUG_ON(*length > cursor->resid);
1185 *last_piece = cursor->last_piece;
1191 * Returns true if the result moves the cursor on to the next piece
1194 static void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor,
1199 BUG_ON(bytes > cursor->resid);
1200 switch (cursor->data->type) {
1201 case CEPH_MSG_DATA_PAGELIST:
1202 new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
1204 case CEPH_MSG_DATA_PAGES:
1205 new_piece = ceph_msg_data_pages_advance(cursor, bytes);
1208 case CEPH_MSG_DATA_BIO:
1209 new_piece = ceph_msg_data_bio_advance(cursor, bytes);
1211 #endif /* CONFIG_BLOCK */
1212 case CEPH_MSG_DATA_BVECS:
1213 new_piece = ceph_msg_data_bvecs_advance(cursor, bytes);
1215 case CEPH_MSG_DATA_NONE:
1220 cursor->total_resid -= bytes;
1222 if (!cursor->resid && cursor->total_resid) {
1223 WARN_ON(!cursor->last_piece);
1225 __ceph_msg_data_cursor_init(cursor);
1228 cursor->need_crc = new_piece;
1231 static size_t sizeof_footer(struct ceph_connection *con)
1233 return (con->peer_features & CEPH_FEATURE_MSG_AUTH) ?
1234 sizeof(struct ceph_msg_footer) :
1235 sizeof(struct ceph_msg_footer_old);
1238 static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
1240 /* Initialize data cursor */
1242 ceph_msg_data_cursor_init(msg, (size_t)data_len);
1246 * Prepare footer for currently outgoing message, and finish things
1247 * off. Assumes out_kvec* are already valid.. we just add on to the end.
1249 static void prepare_write_message_footer(struct ceph_connection *con)
1251 struct ceph_msg *m = con->out_msg;
1253 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
1255 dout("prepare_write_message_footer %p\n", con);
1256 con_out_kvec_add(con, sizeof_footer(con), &m->footer);
1257 if (con->peer_features & CEPH_FEATURE_MSG_AUTH) {
1258 if (con->ops->sign_message)
1259 con->ops->sign_message(m);
1263 m->old_footer.flags = m->footer.flags;
1265 con->out_more = m->more_to_follow;
1266 con->out_msg_done = true;
1270 * Prepare headers for the next outgoing message.
1272 static void prepare_write_message(struct ceph_connection *con)
1277 con_out_kvec_reset(con);
1278 con->out_msg_done = false;
1280 /* Sneak an ack in there first? If we can get it into the same
1281 * TCP packet that's a good thing. */
1282 if (con->in_seq > con->in_seq_acked) {
1283 con->in_seq_acked = con->in_seq;
1284 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
1285 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1286 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1287 &con->out_temp_ack);
1290 BUG_ON(list_empty(&con->out_queue));
1291 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
1293 BUG_ON(m->con != con);
1295 /* put message on sent list */
1297 list_move_tail(&m->list_head, &con->out_sent);
1300 * only assign outgoing seq # if we haven't sent this message
1301 * yet. if it is requeued, resend with it's original seq.
1303 if (m->needs_out_seq) {
1304 m->hdr.seq = cpu_to_le64(++con->out_seq);
1305 m->needs_out_seq = false;
1307 if (con->ops->reencode_message)
1308 con->ops->reencode_message(m);
1311 dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
1312 m, con->out_seq, le16_to_cpu(m->hdr.type),
1313 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
1315 WARN_ON(m->front.iov_len != le32_to_cpu(m->hdr.front_len));
1316 WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
1318 /* tag + hdr + front + middle */
1319 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
1320 con_out_kvec_add(con, sizeof(con->out_hdr), &con->out_hdr);
1321 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
1324 con_out_kvec_add(con, m->middle->vec.iov_len,
1325 m->middle->vec.iov_base);
1327 /* fill in hdr crc and finalize hdr */
1328 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
1329 con->out_msg->hdr.crc = cpu_to_le32(crc);
1330 memcpy(&con->out_hdr, &con->out_msg->hdr, sizeof(con->out_hdr));
1332 /* fill in front and middle crc, footer */
1333 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
1334 con->out_msg->footer.front_crc = cpu_to_le32(crc);
1336 crc = crc32c(0, m->middle->vec.iov_base,
1337 m->middle->vec.iov_len);
1338 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
1340 con->out_msg->footer.middle_crc = 0;
1341 dout("%s front_crc %u middle_crc %u\n", __func__,
1342 le32_to_cpu(con->out_msg->footer.front_crc),
1343 le32_to_cpu(con->out_msg->footer.middle_crc));
1344 con->out_msg->footer.flags = 0;
1346 /* is there a data payload? */
1347 con->out_msg->footer.data_crc = 0;
1348 if (m->data_length) {
1349 prepare_message_data(con->out_msg, m->data_length);
1350 con->out_more = 1; /* data + footer will follow */
1352 /* no, queue up footer too and be done */
1353 prepare_write_message_footer(con);
1356 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1362 static void prepare_write_ack(struct ceph_connection *con)
1364 dout("prepare_write_ack %p %llu -> %llu\n", con,
1365 con->in_seq_acked, con->in_seq);
1366 con->in_seq_acked = con->in_seq;
1368 con_out_kvec_reset(con);
1370 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
1372 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1373 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1374 &con->out_temp_ack);
1376 con->out_more = 1; /* more will follow.. eventually.. */
1377 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1381 * Prepare to share the seq during handshake
1383 static void prepare_write_seq(struct ceph_connection *con)
1385 dout("prepare_write_seq %p %llu -> %llu\n", con,
1386 con->in_seq_acked, con->in_seq);
1387 con->in_seq_acked = con->in_seq;
1389 con_out_kvec_reset(con);
1391 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1392 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1393 &con->out_temp_ack);
1395 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1399 * Prepare to write keepalive byte.
1401 static void prepare_write_keepalive(struct ceph_connection *con)
1403 dout("prepare_write_keepalive %p\n", con);
1404 con_out_kvec_reset(con);
1405 if (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2) {
1406 struct timespec64 now;
1408 ktime_get_real_ts64(&now);
1409 con_out_kvec_add(con, sizeof(tag_keepalive2), &tag_keepalive2);
1410 ceph_encode_timespec64(&con->out_temp_keepalive2, &now);
1411 con_out_kvec_add(con, sizeof(con->out_temp_keepalive2),
1412 &con->out_temp_keepalive2);
1414 con_out_kvec_add(con, sizeof(tag_keepalive), &tag_keepalive);
1416 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1420 * Connection negotiation.
1423 static int get_connect_authorizer(struct ceph_connection *con)
1425 struct ceph_auth_handshake *auth;
1428 if (!con->ops->get_authorizer) {
1430 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
1431 con->out_connect.authorizer_len = 0;
1435 auth = con->ops->get_authorizer(con, &auth_proto, con->auth_retry);
1437 return PTR_ERR(auth);
1440 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
1441 con->out_connect.authorizer_len = cpu_to_le32(auth->authorizer_buf_len);
1446 * We connected to a peer and are saying hello.
1448 static void prepare_write_banner(struct ceph_connection *con)
1450 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
1451 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
1452 &con->msgr->my_enc_addr);
1455 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1458 static void __prepare_write_connect(struct ceph_connection *con)
1460 con_out_kvec_add(con, sizeof(con->out_connect), &con->out_connect);
1462 con_out_kvec_add(con, con->auth->authorizer_buf_len,
1463 con->auth->authorizer_buf);
1466 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1469 static int prepare_write_connect(struct ceph_connection *con)
1471 unsigned int global_seq = get_global_seq(con->msgr, 0);
1475 switch (con->peer_name.type) {
1476 case CEPH_ENTITY_TYPE_MON:
1477 proto = CEPH_MONC_PROTOCOL;
1479 case CEPH_ENTITY_TYPE_OSD:
1480 proto = CEPH_OSDC_PROTOCOL;
1482 case CEPH_ENTITY_TYPE_MDS:
1483 proto = CEPH_MDSC_PROTOCOL;
1489 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
1490 con->connect_seq, global_seq, proto);
1492 con->out_connect.features =
1493 cpu_to_le64(from_msgr(con->msgr)->supported_features);
1494 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
1495 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
1496 con->out_connect.global_seq = cpu_to_le32(global_seq);
1497 con->out_connect.protocol_version = cpu_to_le32(proto);
1498 con->out_connect.flags = 0;
1500 ret = get_connect_authorizer(con);
1504 __prepare_write_connect(con);
1509 * write as much of pending kvecs to the socket as we can.
1511 * 0 -> socket full, but more to do
1514 static int write_partial_kvec(struct ceph_connection *con)
1518 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
1519 while (con->out_kvec_bytes > 0) {
1520 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
1521 con->out_kvec_left, con->out_kvec_bytes,
1525 con->out_kvec_bytes -= ret;
1526 if (con->out_kvec_bytes == 0)
1529 /* account for full iov entries consumed */
1530 while (ret >= con->out_kvec_cur->iov_len) {
1531 BUG_ON(!con->out_kvec_left);
1532 ret -= con->out_kvec_cur->iov_len;
1533 con->out_kvec_cur++;
1534 con->out_kvec_left--;
1536 /* and for a partially-consumed entry */
1538 con->out_kvec_cur->iov_len -= ret;
1539 con->out_kvec_cur->iov_base += ret;
1542 con->out_kvec_left = 0;
1545 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
1546 con->out_kvec_bytes, con->out_kvec_left, ret);
1547 return ret; /* done! */
1550 static u32 ceph_crc32c_page(u32 crc, struct page *page,
1551 unsigned int page_offset,
1552 unsigned int length)
1557 BUG_ON(kaddr == NULL);
1558 crc = crc32c(crc, kaddr + page_offset, length);
1564 * Write as much message data payload as we can. If we finish, queue
1566 * 1 -> done, footer is now queued in out_kvec[].
1567 * 0 -> socket full, but more to do
1570 static int write_partial_message_data(struct ceph_connection *con)
1572 struct ceph_msg *msg = con->out_msg;
1573 struct ceph_msg_data_cursor *cursor = &msg->cursor;
1574 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
1577 dout("%s %p msg %p\n", __func__, con, msg);
1579 if (!msg->num_data_items)
1583 * Iterate through each page that contains data to be
1584 * written, and send as much as possible for each.
1586 * If we are calculating the data crc (the default), we will
1587 * need to map the page. If we have no pages, they have
1588 * been revoked, so use the zero page.
1590 crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
1591 while (cursor->total_resid) {
1598 if (!cursor->resid) {
1599 ceph_msg_data_advance(cursor, 0);
1603 page = ceph_msg_data_next(cursor, &page_offset, &length,
1605 ret = ceph_tcp_sendpage(con->sock, page, page_offset,
1606 length, !last_piece);
1609 msg->footer.data_crc = cpu_to_le32(crc);
1613 if (do_datacrc && cursor->need_crc)
1614 crc = ceph_crc32c_page(crc, page, page_offset, length);
1615 ceph_msg_data_advance(cursor, (size_t)ret);
1618 dout("%s %p msg %p done\n", __func__, con, msg);
1620 /* prepare and queue up footer, too */
1622 msg->footer.data_crc = cpu_to_le32(crc);
1624 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
1625 con_out_kvec_reset(con);
1626 prepare_write_message_footer(con);
1628 return 1; /* must return > 0 to indicate success */
1634 static int write_partial_skip(struct ceph_connection *con)
1638 dout("%s %p %d left\n", __func__, con, con->out_skip);
1639 while (con->out_skip > 0) {
1640 size_t size = min(con->out_skip, (int) PAGE_SIZE);
1642 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
1645 con->out_skip -= ret;
1653 * Prepare to read connection handshake, or an ack.
1655 static void prepare_read_banner(struct ceph_connection *con)
1657 dout("prepare_read_banner %p\n", con);
1658 con->in_base_pos = 0;
1661 static void prepare_read_connect(struct ceph_connection *con)
1663 dout("prepare_read_connect %p\n", con);
1664 con->in_base_pos = 0;
1667 static void prepare_read_ack(struct ceph_connection *con)
1669 dout("prepare_read_ack %p\n", con);
1670 con->in_base_pos = 0;
1673 static void prepare_read_seq(struct ceph_connection *con)
1675 dout("prepare_read_seq %p\n", con);
1676 con->in_base_pos = 0;
1677 con->in_tag = CEPH_MSGR_TAG_SEQ;
1680 static void prepare_read_tag(struct ceph_connection *con)
1682 dout("prepare_read_tag %p\n", con);
1683 con->in_base_pos = 0;
1684 con->in_tag = CEPH_MSGR_TAG_READY;
1687 static void prepare_read_keepalive_ack(struct ceph_connection *con)
1689 dout("prepare_read_keepalive_ack %p\n", con);
1690 con->in_base_pos = 0;
1694 * Prepare to read a message.
1696 static int prepare_read_message(struct ceph_connection *con)
1698 dout("prepare_read_message %p\n", con);
1699 BUG_ON(con->in_msg != NULL);
1700 con->in_base_pos = 0;
1701 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1706 static int read_partial(struct ceph_connection *con,
1707 int end, int size, void *object)
1709 while (con->in_base_pos < end) {
1710 int left = end - con->in_base_pos;
1711 int have = size - left;
1712 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1715 con->in_base_pos += ret;
1722 * Read all or part of the connect-side handshake on a new connection
1724 static int read_partial_banner(struct ceph_connection *con)
1730 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
1733 size = strlen(CEPH_BANNER);
1735 ret = read_partial(con, end, size, con->in_banner);
1739 size = sizeof (con->actual_peer_addr);
1741 ret = read_partial(con, end, size, &con->actual_peer_addr);
1745 size = sizeof (con->peer_addr_for_me);
1747 ret = read_partial(con, end, size, &con->peer_addr_for_me);
1755 static int read_partial_connect(struct ceph_connection *con)
1761 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1763 size = sizeof (con->in_reply);
1765 ret = read_partial(con, end, size, &con->in_reply);
1770 size = le32_to_cpu(con->in_reply.authorizer_len);
1771 if (size > con->auth->authorizer_reply_buf_len) {
1772 pr_err("authorizer reply too big: %d > %zu\n", size,
1773 con->auth->authorizer_reply_buf_len);
1779 ret = read_partial(con, end, size,
1780 con->auth->authorizer_reply_buf);
1785 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1786 con, (int)con->in_reply.tag,
1787 le32_to_cpu(con->in_reply.connect_seq),
1788 le32_to_cpu(con->in_reply.global_seq));
1794 * Verify the hello banner looks okay.
1796 static int verify_hello(struct ceph_connection *con)
1798 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
1799 pr_err("connect to %s got bad banner\n",
1800 ceph_pr_addr(&con->peer_addr.in_addr));
1801 con->error_msg = "protocol error, bad banner";
1807 static bool addr_is_blank(struct sockaddr_storage *ss)
1809 struct in_addr *addr = &((struct sockaddr_in *)ss)->sin_addr;
1810 struct in6_addr *addr6 = &((struct sockaddr_in6 *)ss)->sin6_addr;
1812 switch (ss->ss_family) {
1814 return addr->s_addr == htonl(INADDR_ANY);
1816 return ipv6_addr_any(addr6);
1822 static int addr_port(struct sockaddr_storage *ss)
1824 switch (ss->ss_family) {
1826 return ntohs(((struct sockaddr_in *)ss)->sin_port);
1828 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
1833 static void addr_set_port(struct sockaddr_storage *ss, int p)
1835 switch (ss->ss_family) {
1837 ((struct sockaddr_in *)ss)->sin_port = htons(p);
1840 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
1846 * Unlike other *_pton function semantics, zero indicates success.
1848 static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1849 char delim, const char **ipend)
1851 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1852 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
1854 memset(ss, 0, sizeof(*ss));
1856 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1857 ss->ss_family = AF_INET;
1861 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1862 ss->ss_family = AF_INET6;
1870 * Extract hostname string and resolve using kernel DNS facility.
1872 #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1873 static int ceph_dns_resolve_name(const char *name, size_t namelen,
1874 struct sockaddr_storage *ss, char delim, const char **ipend)
1876 const char *end, *delim_p;
1877 char *colon_p, *ip_addr = NULL;
1881 * The end of the hostname occurs immediately preceding the delimiter or
1882 * the port marker (':') where the delimiter takes precedence.
1884 delim_p = memchr(name, delim, namelen);
1885 colon_p = memchr(name, ':', namelen);
1887 if (delim_p && colon_p)
1888 end = delim_p < colon_p ? delim_p : colon_p;
1889 else if (!delim_p && colon_p)
1893 if (!end) /* case: hostname:/ */
1894 end = name + namelen;
1900 /* do dns_resolve upcall */
1901 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1903 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1911 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1912 ret, ret ? "failed" : ceph_pr_addr(ss));
1917 static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1918 struct sockaddr_storage *ss, char delim, const char **ipend)
1925 * Parse a server name (IP or hostname). If a valid IP address is not found
1926 * then try to extract a hostname to resolve using userspace DNS upcall.
1928 static int ceph_parse_server_name(const char *name, size_t namelen,
1929 struct sockaddr_storage *ss, char delim, const char **ipend)
1933 ret = ceph_pton(name, namelen, ss, delim, ipend);
1935 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1941 * Parse an ip[:port] list into an addr array. Use the default
1942 * monitor port if a port isn't specified.
1944 int ceph_parse_ips(const char *c, const char *end,
1945 struct ceph_entity_addr *addr,
1946 int max_count, int *count)
1948 int i, ret = -EINVAL;
1951 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1952 for (i = 0; i < max_count; i++) {
1954 struct sockaddr_storage *ss = &addr[i].in_addr;
1963 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1972 dout("missing matching ']'\n");
1979 if (p < end && *p == ':') {
1982 while (p < end && *p >= '0' && *p <= '9') {
1983 port = (port * 10) + (*p - '0');
1987 port = CEPH_MON_PORT;
1988 else if (port > 65535)
1991 port = CEPH_MON_PORT;
1994 addr_set_port(ss, port);
1996 dout("parse_ips got %s\n", ceph_pr_addr(ss));
2013 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
2016 EXPORT_SYMBOL(ceph_parse_ips);
2018 static int process_banner(struct ceph_connection *con)
2020 dout("process_banner on %p\n", con);
2022 if (verify_hello(con) < 0)
2025 ceph_decode_addr(&con->actual_peer_addr);
2026 ceph_decode_addr(&con->peer_addr_for_me);
2029 * Make sure the other end is who we wanted. note that the other
2030 * end may not yet know their ip address, so if it's 0.0.0.0, give
2031 * them the benefit of the doubt.
2033 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
2034 sizeof(con->peer_addr)) != 0 &&
2035 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
2036 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
2037 pr_warn("wrong peer, want %s/%d, got %s/%d\n",
2038 ceph_pr_addr(&con->peer_addr.in_addr),
2039 (int)le32_to_cpu(con->peer_addr.nonce),
2040 ceph_pr_addr(&con->actual_peer_addr.in_addr),
2041 (int)le32_to_cpu(con->actual_peer_addr.nonce));
2042 con->error_msg = "wrong peer at address";
2047 * did we learn our address?
2049 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
2050 int port = addr_port(&con->msgr->inst.addr.in_addr);
2052 memcpy(&con->msgr->inst.addr.in_addr,
2053 &con->peer_addr_for_me.in_addr,
2054 sizeof(con->peer_addr_for_me.in_addr));
2055 addr_set_port(&con->msgr->inst.addr.in_addr, port);
2056 encode_my_addr(con->msgr);
2057 dout("process_banner learned my addr is %s\n",
2058 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
2064 static int process_connect(struct ceph_connection *con)
2066 u64 sup_feat = from_msgr(con->msgr)->supported_features;
2067 u64 req_feat = from_msgr(con->msgr)->required_features;
2068 u64 server_feat = le64_to_cpu(con->in_reply.features);
2071 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
2075 * Any connection that defines ->get_authorizer()
2076 * should also define ->add_authorizer_challenge() and
2077 * ->verify_authorizer_reply().
2079 * See get_connect_authorizer().
2081 if (con->in_reply.tag == CEPH_MSGR_TAG_CHALLENGE_AUTHORIZER) {
2082 ret = con->ops->add_authorizer_challenge(
2083 con, con->auth->authorizer_reply_buf,
2084 le32_to_cpu(con->in_reply.authorizer_len));
2088 con_out_kvec_reset(con);
2089 __prepare_write_connect(con);
2090 prepare_read_connect(con);
2094 ret = con->ops->verify_authorizer_reply(con);
2096 con->error_msg = "bad authorize reply";
2101 switch (con->in_reply.tag) {
2102 case CEPH_MSGR_TAG_FEATURES:
2103 pr_err("%s%lld %s feature set mismatch,"
2104 " my %llx < server's %llx, missing %llx\n",
2105 ENTITY_NAME(con->peer_name),
2106 ceph_pr_addr(&con->peer_addr.in_addr),
2107 sup_feat, server_feat, server_feat & ~sup_feat);
2108 con->error_msg = "missing required protocol features";
2109 reset_connection(con);
2112 case CEPH_MSGR_TAG_BADPROTOVER:
2113 pr_err("%s%lld %s protocol version mismatch,"
2114 " my %d != server's %d\n",
2115 ENTITY_NAME(con->peer_name),
2116 ceph_pr_addr(&con->peer_addr.in_addr),
2117 le32_to_cpu(con->out_connect.protocol_version),
2118 le32_to_cpu(con->in_reply.protocol_version));
2119 con->error_msg = "protocol version mismatch";
2120 reset_connection(con);
2123 case CEPH_MSGR_TAG_BADAUTHORIZER:
2125 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
2127 if (con->auth_retry == 2) {
2128 con->error_msg = "connect authorization failure";
2131 con_out_kvec_reset(con);
2132 ret = prepare_write_connect(con);
2135 prepare_read_connect(con);
2138 case CEPH_MSGR_TAG_RESETSESSION:
2140 * If we connected with a large connect_seq but the peer
2141 * has no record of a session with us (no connection, or
2142 * connect_seq == 0), they will send RESETSESION to indicate
2143 * that they must have reset their session, and may have
2146 dout("process_connect got RESET peer seq %u\n",
2147 le32_to_cpu(con->in_reply.connect_seq));
2148 pr_err("%s%lld %s connection reset\n",
2149 ENTITY_NAME(con->peer_name),
2150 ceph_pr_addr(&con->peer_addr.in_addr));
2151 reset_connection(con);
2152 con_out_kvec_reset(con);
2153 ret = prepare_write_connect(con);
2156 prepare_read_connect(con);
2158 /* Tell ceph about it. */
2159 mutex_unlock(&con->mutex);
2160 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
2161 if (con->ops->peer_reset)
2162 con->ops->peer_reset(con);
2163 mutex_lock(&con->mutex);
2164 if (con->state != CON_STATE_NEGOTIATING)
2168 case CEPH_MSGR_TAG_RETRY_SESSION:
2170 * If we sent a smaller connect_seq than the peer has, try
2171 * again with a larger value.
2173 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
2174 le32_to_cpu(con->out_connect.connect_seq),
2175 le32_to_cpu(con->in_reply.connect_seq));
2176 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
2177 con_out_kvec_reset(con);
2178 ret = prepare_write_connect(con);
2181 prepare_read_connect(con);
2184 case CEPH_MSGR_TAG_RETRY_GLOBAL:
2186 * If we sent a smaller global_seq than the peer has, try
2187 * again with a larger value.
2189 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
2190 con->peer_global_seq,
2191 le32_to_cpu(con->in_reply.global_seq));
2192 get_global_seq(con->msgr,
2193 le32_to_cpu(con->in_reply.global_seq));
2194 con_out_kvec_reset(con);
2195 ret = prepare_write_connect(con);
2198 prepare_read_connect(con);
2201 case CEPH_MSGR_TAG_SEQ:
2202 case CEPH_MSGR_TAG_READY:
2203 if (req_feat & ~server_feat) {
2204 pr_err("%s%lld %s protocol feature mismatch,"
2205 " my required %llx > server's %llx, need %llx\n",
2206 ENTITY_NAME(con->peer_name),
2207 ceph_pr_addr(&con->peer_addr.in_addr),
2208 req_feat, server_feat, req_feat & ~server_feat);
2209 con->error_msg = "missing required protocol features";
2210 reset_connection(con);
2214 WARN_ON(con->state != CON_STATE_NEGOTIATING);
2215 con->state = CON_STATE_OPEN;
2216 con->auth_retry = 0; /* we authenticated; clear flag */
2217 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
2219 con->peer_features = server_feat;
2220 dout("process_connect got READY gseq %d cseq %d (%d)\n",
2221 con->peer_global_seq,
2222 le32_to_cpu(con->in_reply.connect_seq),
2224 WARN_ON(con->connect_seq !=
2225 le32_to_cpu(con->in_reply.connect_seq));
2227 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
2228 con_flag_set(con, CON_FLAG_LOSSYTX);
2230 con->delay = 0; /* reset backoff memory */
2232 if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
2233 prepare_write_seq(con);
2234 prepare_read_seq(con);
2236 prepare_read_tag(con);
2240 case CEPH_MSGR_TAG_WAIT:
2242 * If there is a connection race (we are opening
2243 * connections to each other), one of us may just have
2244 * to WAIT. This shouldn't happen if we are the
2247 con->error_msg = "protocol error, got WAIT as client";
2251 con->error_msg = "protocol error, garbage tag during connect";
2259 * read (part of) an ack
2261 static int read_partial_ack(struct ceph_connection *con)
2263 int size = sizeof (con->in_temp_ack);
2266 return read_partial(con, end, size, &con->in_temp_ack);
2270 * We can finally discard anything that's been acked.
2272 static void process_ack(struct ceph_connection *con)
2275 u64 ack = le64_to_cpu(con->in_temp_ack);
2277 bool reconnect = (con->in_tag == CEPH_MSGR_TAG_SEQ);
2278 struct list_head *list = reconnect ? &con->out_queue : &con->out_sent;
2281 * In the reconnect case, con_fault() has requeued messages
2282 * in out_sent. We should cleanup old messages according to
2283 * the reconnect seq.
2285 while (!list_empty(list)) {
2286 m = list_first_entry(list, struct ceph_msg, list_head);
2287 if (reconnect && m->needs_out_seq)
2289 seq = le64_to_cpu(m->hdr.seq);
2292 dout("got ack for seq %llu type %d at %p\n", seq,
2293 le16_to_cpu(m->hdr.type), m);
2294 m->ack_stamp = jiffies;
2298 prepare_read_tag(con);
2302 static int read_partial_message_section(struct ceph_connection *con,
2303 struct kvec *section,
2304 unsigned int sec_len, u32 *crc)
2310 while (section->iov_len < sec_len) {
2311 BUG_ON(section->iov_base == NULL);
2312 left = sec_len - section->iov_len;
2313 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
2314 section->iov_len, left);
2317 section->iov_len += ret;
2319 if (section->iov_len == sec_len)
2320 *crc = crc32c(0, section->iov_base, section->iov_len);
2325 static int read_partial_msg_data(struct ceph_connection *con)
2327 struct ceph_msg *msg = con->in_msg;
2328 struct ceph_msg_data_cursor *cursor = &msg->cursor;
2329 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
2336 if (!msg->num_data_items)
2340 crc = con->in_data_crc;
2341 while (cursor->total_resid) {
2342 if (!cursor->resid) {
2343 ceph_msg_data_advance(cursor, 0);
2347 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
2348 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
2351 con->in_data_crc = crc;
2357 crc = ceph_crc32c_page(crc, page, page_offset, ret);
2358 ceph_msg_data_advance(cursor, (size_t)ret);
2361 con->in_data_crc = crc;
2363 return 1; /* must return > 0 to indicate success */
2367 * read (part of) a message.
2369 static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
2371 static int read_partial_message(struct ceph_connection *con)
2373 struct ceph_msg *m = con->in_msg;
2377 unsigned int front_len, middle_len, data_len;
2378 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
2379 bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
2383 dout("read_partial_message con %p msg %p\n", con, m);
2386 size = sizeof (con->in_hdr);
2388 ret = read_partial(con, end, size, &con->in_hdr);
2392 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
2393 if (cpu_to_le32(crc) != con->in_hdr.crc) {
2394 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
2395 crc, con->in_hdr.crc);
2399 front_len = le32_to_cpu(con->in_hdr.front_len);
2400 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
2402 middle_len = le32_to_cpu(con->in_hdr.middle_len);
2403 if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
2405 data_len = le32_to_cpu(con->in_hdr.data_len);
2406 if (data_len > CEPH_MSG_MAX_DATA_LEN)
2410 seq = le64_to_cpu(con->in_hdr.seq);
2411 if ((s64)seq - (s64)con->in_seq < 1) {
2412 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
2413 ENTITY_NAME(con->peer_name),
2414 ceph_pr_addr(&con->peer_addr.in_addr),
2415 seq, con->in_seq + 1);
2416 con->in_base_pos = -front_len - middle_len - data_len -
2418 con->in_tag = CEPH_MSGR_TAG_READY;
2420 } else if ((s64)seq - (s64)con->in_seq > 1) {
2421 pr_err("read_partial_message bad seq %lld expected %lld\n",
2422 seq, con->in_seq + 1);
2423 con->error_msg = "bad message sequence # for incoming message";
2427 /* allocate message? */
2431 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
2432 front_len, data_len);
2433 ret = ceph_con_in_msg_alloc(con, &skip);
2437 BUG_ON(!con->in_msg ^ skip);
2439 /* skip this message */
2440 dout("alloc_msg said skip message\n");
2441 con->in_base_pos = -front_len - middle_len - data_len -
2443 con->in_tag = CEPH_MSGR_TAG_READY;
2448 BUG_ON(!con->in_msg);
2449 BUG_ON(con->in_msg->con != con);
2451 m->front.iov_len = 0; /* haven't read it yet */
2453 m->middle->vec.iov_len = 0;
2455 /* prepare for data payload, if any */
2458 prepare_message_data(con->in_msg, data_len);
2462 ret = read_partial_message_section(con, &m->front, front_len,
2463 &con->in_front_crc);
2469 ret = read_partial_message_section(con, &m->middle->vec,
2471 &con->in_middle_crc);
2478 ret = read_partial_msg_data(con);
2484 size = sizeof_footer(con);
2486 ret = read_partial(con, end, size, &m->footer);
2491 m->footer.flags = m->old_footer.flags;
2495 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
2496 m, front_len, m->footer.front_crc, middle_len,
2497 m->footer.middle_crc, data_len, m->footer.data_crc);
2500 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
2501 pr_err("read_partial_message %p front crc %u != exp. %u\n",
2502 m, con->in_front_crc, m->footer.front_crc);
2505 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
2506 pr_err("read_partial_message %p middle crc %u != exp %u\n",
2507 m, con->in_middle_crc, m->footer.middle_crc);
2511 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
2512 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
2513 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
2514 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
2518 if (need_sign && con->ops->check_message_signature &&
2519 con->ops->check_message_signature(m)) {
2520 pr_err("read_partial_message %p signature check failed\n", m);
2524 return 1; /* done! */
2528 * Process message. This happens in the worker thread. The callback should
2529 * be careful not to do anything that waits on other incoming messages or it
2532 static void process_message(struct ceph_connection *con)
2534 struct ceph_msg *msg = con->in_msg;
2536 BUG_ON(con->in_msg->con != con);
2539 /* if first message, set peer_name */
2540 if (con->peer_name.type == 0)
2541 con->peer_name = msg->hdr.src;
2544 mutex_unlock(&con->mutex);
2546 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
2547 msg, le64_to_cpu(msg->hdr.seq),
2548 ENTITY_NAME(msg->hdr.src),
2549 le16_to_cpu(msg->hdr.type),
2550 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2551 le32_to_cpu(msg->hdr.front_len),
2552 le32_to_cpu(msg->hdr.data_len),
2553 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
2554 con->ops->dispatch(con, msg);
2556 mutex_lock(&con->mutex);
2559 static int read_keepalive_ack(struct ceph_connection *con)
2561 struct ceph_timespec ceph_ts;
2562 size_t size = sizeof(ceph_ts);
2563 int ret = read_partial(con, size, size, &ceph_ts);
2566 ceph_decode_timespec64(&con->last_keepalive_ack, &ceph_ts);
2567 prepare_read_tag(con);
2572 * Write something to the socket. Called in a worker thread when the
2573 * socket appears to be writeable and we have something ready to send.
2575 static int try_write(struct ceph_connection *con)
2579 dout("try_write start %p state %lu\n", con, con->state);
2580 if (con->state != CON_STATE_PREOPEN &&
2581 con->state != CON_STATE_CONNECTING &&
2582 con->state != CON_STATE_NEGOTIATING &&
2583 con->state != CON_STATE_OPEN)
2586 /* open the socket first? */
2587 if (con->state == CON_STATE_PREOPEN) {
2589 con->state = CON_STATE_CONNECTING;
2591 con_out_kvec_reset(con);
2592 prepare_write_banner(con);
2593 prepare_read_banner(con);
2595 BUG_ON(con->in_msg);
2596 con->in_tag = CEPH_MSGR_TAG_READY;
2597 dout("try_write initiating connect on %p new state %lu\n",
2599 ret = ceph_tcp_connect(con);
2601 con->error_msg = "connect error";
2607 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
2610 /* kvec data queued? */
2611 if (con->out_kvec_left) {
2612 ret = write_partial_kvec(con);
2616 if (con->out_skip) {
2617 ret = write_partial_skip(con);
2624 if (con->out_msg_done) {
2625 ceph_msg_put(con->out_msg);
2626 con->out_msg = NULL; /* we're done with this one */
2630 ret = write_partial_message_data(con);
2632 goto more; /* we need to send the footer, too! */
2636 dout("try_write write_partial_message_data err %d\n",
2643 if (con->state == CON_STATE_OPEN) {
2644 if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
2645 prepare_write_keepalive(con);
2648 /* is anything else pending? */
2649 if (!list_empty(&con->out_queue)) {
2650 prepare_write_message(con);
2653 if (con->in_seq > con->in_seq_acked) {
2654 prepare_write_ack(con);
2659 /* Nothing to do! */
2660 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
2661 dout("try_write nothing else to write.\n");
2664 dout("try_write done on %p ret %d\n", con, ret);
2669 * Read what we can from the socket.
2671 static int try_read(struct ceph_connection *con)
2676 dout("try_read start on %p state %lu\n", con, con->state);
2677 if (con->state != CON_STATE_CONNECTING &&
2678 con->state != CON_STATE_NEGOTIATING &&
2679 con->state != CON_STATE_OPEN)
2684 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2687 if (con->state == CON_STATE_CONNECTING) {
2688 dout("try_read connecting\n");
2689 ret = read_partial_banner(con);
2692 ret = process_banner(con);
2696 con->state = CON_STATE_NEGOTIATING;
2699 * Received banner is good, exchange connection info.
2700 * Do not reset out_kvec, as sending our banner raced
2701 * with receiving peer banner after connect completed.
2703 ret = prepare_write_connect(con);
2706 prepare_read_connect(con);
2708 /* Send connection info before awaiting response */
2712 if (con->state == CON_STATE_NEGOTIATING) {
2713 dout("try_read negotiating\n");
2714 ret = read_partial_connect(con);
2717 ret = process_connect(con);
2723 WARN_ON(con->state != CON_STATE_OPEN);
2725 if (con->in_base_pos < 0) {
2727 * skipping + discarding content.
2729 ret = ceph_tcp_recvmsg(con->sock, NULL, -con->in_base_pos);
2732 dout("skipped %d / %d bytes\n", ret, -con->in_base_pos);
2733 con->in_base_pos += ret;
2734 if (con->in_base_pos)
2737 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2741 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2744 dout("try_read got tag %d\n", (int)con->in_tag);
2745 switch (con->in_tag) {
2746 case CEPH_MSGR_TAG_MSG:
2747 prepare_read_message(con);
2749 case CEPH_MSGR_TAG_ACK:
2750 prepare_read_ack(con);
2752 case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
2753 prepare_read_keepalive_ack(con);
2755 case CEPH_MSGR_TAG_CLOSE:
2756 con_close_socket(con);
2757 con->state = CON_STATE_CLOSED;
2763 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2764 ret = read_partial_message(con);
2768 con->error_msg = "bad crc/signature";
2774 con->error_msg = "io error";
2779 if (con->in_tag == CEPH_MSGR_TAG_READY)
2781 process_message(con);
2782 if (con->state == CON_STATE_OPEN)
2783 prepare_read_tag(con);
2786 if (con->in_tag == CEPH_MSGR_TAG_ACK ||
2787 con->in_tag == CEPH_MSGR_TAG_SEQ) {
2789 * the final handshake seq exchange is semantically
2790 * equivalent to an ACK
2792 ret = read_partial_ack(con);
2798 if (con->in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
2799 ret = read_keepalive_ack(con);
2806 dout("try_read done on %p ret %d\n", con, ret);
2810 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2811 con->error_msg = "protocol error, garbage tag";
2818 * Atomically queue work on a connection after the specified delay.
2819 * Bump @con reference to avoid races with connection teardown.
2820 * Returns 0 if work was queued, or an error code otherwise.
2822 static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
2824 if (!con->ops->get(con)) {
2825 dout("%s %p ref count 0\n", __func__, con);
2829 if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
2830 dout("%s %p - already queued\n", __func__, con);
2835 dout("%s %p %lu\n", __func__, con, delay);
2839 static void queue_con(struct ceph_connection *con)
2841 (void) queue_con_delay(con, 0);
2844 static void cancel_con(struct ceph_connection *con)
2846 if (cancel_delayed_work(&con->work)) {
2847 dout("%s %p\n", __func__, con);
2852 static bool con_sock_closed(struct ceph_connection *con)
2854 if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
2858 case CON_STATE_ ## x: \
2859 con->error_msg = "socket closed (con state " #x ")"; \
2862 switch (con->state) {
2870 pr_warn("%s con %p unrecognized state %lu\n",
2871 __func__, con, con->state);
2872 con->error_msg = "unrecognized con state";
2881 static bool con_backoff(struct ceph_connection *con)
2885 if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
2888 ret = queue_con_delay(con, round_jiffies_relative(con->delay));
2890 dout("%s: con %p FAILED to back off %lu\n", __func__,
2892 BUG_ON(ret == -ENOENT);
2893 con_flag_set(con, CON_FLAG_BACKOFF);
2899 /* Finish fault handling; con->mutex must *not* be held here */
2901 static void con_fault_finish(struct ceph_connection *con)
2903 dout("%s %p\n", __func__, con);
2906 * in case we faulted due to authentication, invalidate our
2907 * current tickets so that we can get new ones.
2909 if (con->auth_retry) {
2910 dout("auth_retry %d, invalidating\n", con->auth_retry);
2911 if (con->ops->invalidate_authorizer)
2912 con->ops->invalidate_authorizer(con);
2913 con->auth_retry = 0;
2916 if (con->ops->fault)
2917 con->ops->fault(con);
2921 * Do some work on a connection. Drop a connection ref when we're done.
2923 static void ceph_con_workfn(struct work_struct *work)
2925 struct ceph_connection *con = container_of(work, struct ceph_connection,
2929 mutex_lock(&con->mutex);
2933 if ((fault = con_sock_closed(con))) {
2934 dout("%s: con %p SOCK_CLOSED\n", __func__, con);
2937 if (con_backoff(con)) {
2938 dout("%s: con %p BACKOFF\n", __func__, con);
2941 if (con->state == CON_STATE_STANDBY) {
2942 dout("%s: con %p STANDBY\n", __func__, con);
2945 if (con->state == CON_STATE_CLOSED) {
2946 dout("%s: con %p CLOSED\n", __func__, con);
2950 if (con->state == CON_STATE_PREOPEN) {
2951 dout("%s: con %p PREOPEN\n", __func__, con);
2955 ret = try_read(con);
2959 if (!con->error_msg)
2960 con->error_msg = "socket error on read";
2965 ret = try_write(con);
2969 if (!con->error_msg)
2970 con->error_msg = "socket error on write";
2974 break; /* If we make it to here, we're done */
2978 mutex_unlock(&con->mutex);
2981 con_fault_finish(con);
2987 * Generic error/fault handler. A retry mechanism is used with
2988 * exponential backoff
2990 static void con_fault(struct ceph_connection *con)
2992 dout("fault %p state %lu to peer %s\n",
2993 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
2995 pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
2996 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
2997 con->error_msg = NULL;
2999 WARN_ON(con->state != CON_STATE_CONNECTING &&
3000 con->state != CON_STATE_NEGOTIATING &&
3001 con->state != CON_STATE_OPEN);
3003 con_close_socket(con);
3005 if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
3006 dout("fault on LOSSYTX channel, marking CLOSED\n");
3007 con->state = CON_STATE_CLOSED;
3012 BUG_ON(con->in_msg->con != con);
3013 ceph_msg_put(con->in_msg);
3017 /* Requeue anything that hasn't been acked */
3018 list_splice_init(&con->out_sent, &con->out_queue);
3020 /* If there are no messages queued or keepalive pending, place
3021 * the connection in a STANDBY state */
3022 if (list_empty(&con->out_queue) &&
3023 !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
3024 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
3025 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
3026 con->state = CON_STATE_STANDBY;
3028 /* retry after a delay. */
3029 con->state = CON_STATE_PREOPEN;
3030 if (con->delay == 0)
3031 con->delay = BASE_DELAY_INTERVAL;
3032 else if (con->delay < MAX_DELAY_INTERVAL)
3034 con_flag_set(con, CON_FLAG_BACKOFF);
3042 * initialize a new messenger instance
3044 void ceph_messenger_init(struct ceph_messenger *msgr,
3045 struct ceph_entity_addr *myaddr)
3047 spin_lock_init(&msgr->global_seq_lock);
3050 msgr->inst.addr = *myaddr;
3052 /* select a random nonce */
3053 msgr->inst.addr.type = 0;
3054 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
3055 encode_my_addr(msgr);
3057 atomic_set(&msgr->stopping, 0);
3058 write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
3060 dout("%s %p\n", __func__, msgr);
3062 EXPORT_SYMBOL(ceph_messenger_init);
3064 void ceph_messenger_fini(struct ceph_messenger *msgr)
3066 put_net(read_pnet(&msgr->net));
3068 EXPORT_SYMBOL(ceph_messenger_fini);
3070 static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con)
3073 msg->con->ops->put(msg->con);
3075 msg->con = con ? con->ops->get(con) : NULL;
3076 BUG_ON(msg->con != con);
3079 static void clear_standby(struct ceph_connection *con)
3081 /* come back from STANDBY? */
3082 if (con->state == CON_STATE_STANDBY) {
3083 dout("clear_standby %p and ++connect_seq\n", con);
3084 con->state = CON_STATE_PREOPEN;
3086 WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
3087 WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
3092 * Queue up an outgoing message on the given connection.
3094 void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
3097 msg->hdr.src = con->msgr->inst.name;
3098 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
3099 msg->needs_out_seq = true;
3101 mutex_lock(&con->mutex);
3103 if (con->state == CON_STATE_CLOSED) {
3104 dout("con_send %p closed, dropping %p\n", con, msg);
3106 mutex_unlock(&con->mutex);
3110 msg_con_set(msg, con);
3112 BUG_ON(!list_empty(&msg->list_head));
3113 list_add_tail(&msg->list_head, &con->out_queue);
3114 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
3115 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
3116 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
3117 le32_to_cpu(msg->hdr.front_len),
3118 le32_to_cpu(msg->hdr.middle_len),
3119 le32_to_cpu(msg->hdr.data_len));
3122 mutex_unlock(&con->mutex);
3124 /* if there wasn't anything waiting to send before, queue
3126 if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
3129 EXPORT_SYMBOL(ceph_con_send);
3132 * Revoke a message that was previously queued for send
3134 void ceph_msg_revoke(struct ceph_msg *msg)
3136 struct ceph_connection *con = msg->con;
3139 dout("%s msg %p null con\n", __func__, msg);
3140 return; /* Message not in our possession */
3143 mutex_lock(&con->mutex);
3144 if (!list_empty(&msg->list_head)) {
3145 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
3146 list_del_init(&msg->list_head);
3151 if (con->out_msg == msg) {
3152 BUG_ON(con->out_skip);
3154 if (con->out_msg_done) {
3155 con->out_skip += con_out_kvec_skip(con);
3157 BUG_ON(!msg->data_length);
3158 con->out_skip += sizeof_footer(con);
3160 /* data, middle, front */
3161 if (msg->data_length)
3162 con->out_skip += msg->cursor.total_resid;
3164 con->out_skip += con_out_kvec_skip(con);
3165 con->out_skip += con_out_kvec_skip(con);
3167 dout("%s %p msg %p - was sending, will write %d skip %d\n",
3168 __func__, con, msg, con->out_kvec_bytes, con->out_skip);
3170 con->out_msg = NULL;
3174 mutex_unlock(&con->mutex);
3178 * Revoke a message that we may be reading data into
3180 void ceph_msg_revoke_incoming(struct ceph_msg *msg)
3182 struct ceph_connection *con = msg->con;
3185 dout("%s msg %p null con\n", __func__, msg);
3186 return; /* Message not in our possession */
3189 mutex_lock(&con->mutex);
3190 if (con->in_msg == msg) {
3191 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
3192 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
3193 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
3195 /* skip rest of message */
3196 dout("%s %p msg %p revoked\n", __func__, con, msg);
3197 con->in_base_pos = con->in_base_pos -
3198 sizeof(struct ceph_msg_header) -
3202 sizeof(struct ceph_msg_footer);
3203 ceph_msg_put(con->in_msg);
3205 con->in_tag = CEPH_MSGR_TAG_READY;
3208 dout("%s %p in_msg %p msg %p no-op\n",
3209 __func__, con, con->in_msg, msg);
3211 mutex_unlock(&con->mutex);
3215 * Queue a keepalive byte to ensure the tcp connection is alive.
3217 void ceph_con_keepalive(struct ceph_connection *con)
3219 dout("con_keepalive %p\n", con);
3220 mutex_lock(&con->mutex);
3222 mutex_unlock(&con->mutex);
3223 if (con_flag_test_and_set(con, CON_FLAG_KEEPALIVE_PENDING) == 0 &&
3224 con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
3227 EXPORT_SYMBOL(ceph_con_keepalive);
3229 bool ceph_con_keepalive_expired(struct ceph_connection *con,
3230 unsigned long interval)
3233 (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) {
3234 struct timespec64 now;
3235 struct timespec64 ts;
3236 ktime_get_real_ts64(&now);
3237 jiffies_to_timespec64(interval, &ts);
3238 ts = timespec64_add(con->last_keepalive_ack, ts);
3239 return timespec64_compare(&now, &ts) >= 0;
3244 static struct ceph_msg_data *ceph_msg_data_add(struct ceph_msg *msg)
3246 BUG_ON(msg->num_data_items >= msg->max_data_items);
3247 return &msg->data[msg->num_data_items++];
3250 static void ceph_msg_data_destroy(struct ceph_msg_data *data)
3252 if (data->type == CEPH_MSG_DATA_PAGELIST)
3253 ceph_pagelist_release(data->pagelist);
3256 void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
3257 size_t length, size_t alignment)
3259 struct ceph_msg_data *data;
3264 data = ceph_msg_data_add(msg);
3265 data->type = CEPH_MSG_DATA_PAGES;
3266 data->pages = pages;
3267 data->length = length;
3268 data->alignment = alignment & ~PAGE_MASK;
3270 msg->data_length += length;
3272 EXPORT_SYMBOL(ceph_msg_data_add_pages);
3274 void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
3275 struct ceph_pagelist *pagelist)
3277 struct ceph_msg_data *data;
3280 BUG_ON(!pagelist->length);
3282 data = ceph_msg_data_add(msg);
3283 data->type = CEPH_MSG_DATA_PAGELIST;
3284 refcount_inc(&pagelist->refcnt);
3285 data->pagelist = pagelist;
3287 msg->data_length += pagelist->length;
3289 EXPORT_SYMBOL(ceph_msg_data_add_pagelist);
3292 void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
3295 struct ceph_msg_data *data;
3297 data = ceph_msg_data_add(msg);
3298 data->type = CEPH_MSG_DATA_BIO;
3299 data->bio_pos = *bio_pos;
3300 data->bio_length = length;
3302 msg->data_length += length;
3304 EXPORT_SYMBOL(ceph_msg_data_add_bio);
3305 #endif /* CONFIG_BLOCK */
3307 void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
3308 struct ceph_bvec_iter *bvec_pos)
3310 struct ceph_msg_data *data;
3312 data = ceph_msg_data_add(msg);
3313 data->type = CEPH_MSG_DATA_BVECS;
3314 data->bvec_pos = *bvec_pos;
3316 msg->data_length += bvec_pos->iter.bi_size;
3318 EXPORT_SYMBOL(ceph_msg_data_add_bvecs);
3321 * construct a new message with given type, size
3322 * the new msg has a ref count of 1.
3324 struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items,
3325 gfp_t flags, bool can_fail)
3329 m = kmem_cache_zalloc(ceph_msg_cache, flags);
3333 m->hdr.type = cpu_to_le16(type);
3334 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
3335 m->hdr.front_len = cpu_to_le32(front_len);
3337 INIT_LIST_HEAD(&m->list_head);
3338 kref_init(&m->kref);
3342 m->front.iov_base = ceph_kvmalloc(front_len, flags);
3343 if (m->front.iov_base == NULL) {
3344 dout("ceph_msg_new can't allocate %d bytes\n",
3349 m->front.iov_base = NULL;
3351 m->front_alloc_len = m->front.iov_len = front_len;
3353 if (max_data_items) {
3354 m->data = kmalloc_array(max_data_items, sizeof(*m->data),
3359 m->max_data_items = max_data_items;
3362 dout("ceph_msg_new %p front %d\n", m, front_len);
3369 pr_err("msg_new can't create type %d front %d\n", type,
3373 dout("msg_new can't create type %d front %d\n", type,
3378 EXPORT_SYMBOL(ceph_msg_new2);
3380 struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
3383 return ceph_msg_new2(type, front_len, 0, flags, can_fail);
3385 EXPORT_SYMBOL(ceph_msg_new);
3388 * Allocate "middle" portion of a message, if it is needed and wasn't
3389 * allocated by alloc_msg. This allows us to read a small fixed-size
3390 * per-type header in the front and then gracefully fail (i.e.,
3391 * propagate the error to the caller based on info in the front) when
3392 * the middle is too large.
3394 static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
3396 int type = le16_to_cpu(msg->hdr.type);
3397 int middle_len = le32_to_cpu(msg->hdr.middle_len);
3399 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
3400 ceph_msg_type_name(type), middle_len);
3401 BUG_ON(!middle_len);
3402 BUG_ON(msg->middle);
3404 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
3411 * Allocate a message for receiving an incoming message on a
3412 * connection, and save the result in con->in_msg. Uses the
3413 * connection's private alloc_msg op if available.
3415 * Returns 0 on success, or a negative error code.
3417 * On success, if we set *skip = 1:
3418 * - the next message should be skipped and ignored.
3419 * - con->in_msg == NULL
3420 * or if we set *skip = 0:
3421 * - con->in_msg is non-null.
3422 * On error (ENOMEM, EAGAIN, ...),
3423 * - con->in_msg == NULL
3425 static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
3427 struct ceph_msg_header *hdr = &con->in_hdr;
3428 int middle_len = le32_to_cpu(hdr->middle_len);
3429 struct ceph_msg *msg;
3432 BUG_ON(con->in_msg != NULL);
3433 BUG_ON(!con->ops->alloc_msg);
3435 mutex_unlock(&con->mutex);
3436 msg = con->ops->alloc_msg(con, hdr, skip);
3437 mutex_lock(&con->mutex);
3438 if (con->state != CON_STATE_OPEN) {
3445 msg_con_set(msg, con);
3449 * Null message pointer means either we should skip
3450 * this message or we couldn't allocate memory. The
3451 * former is not an error.
3456 con->error_msg = "error allocating memory for incoming message";
3459 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
3461 if (middle_len && !con->in_msg->middle) {
3462 ret = ceph_alloc_middle(con, con->in_msg);
3464 ceph_msg_put(con->in_msg);
3474 * Free a generically kmalloc'd message.
3476 static void ceph_msg_free(struct ceph_msg *m)
3478 dout("%s %p\n", __func__, m);
3479 kvfree(m->front.iov_base);
3481 kmem_cache_free(ceph_msg_cache, m);
3484 static void ceph_msg_release(struct kref *kref)
3486 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
3489 dout("%s %p\n", __func__, m);
3490 WARN_ON(!list_empty(&m->list_head));
3492 msg_con_set(m, NULL);
3494 /* drop middle, data, if any */
3496 ceph_buffer_put(m->middle);
3500 for (i = 0; i < m->num_data_items; i++)
3501 ceph_msg_data_destroy(&m->data[i]);
3504 ceph_msgpool_put(m->pool, m);
3509 struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
3511 dout("%s %p (was %d)\n", __func__, msg,
3512 kref_read(&msg->kref));
3513 kref_get(&msg->kref);
3516 EXPORT_SYMBOL(ceph_msg_get);
3518 void ceph_msg_put(struct ceph_msg *msg)
3520 dout("%s %p (was %d)\n", __func__, msg,
3521 kref_read(&msg->kref));
3522 kref_put(&msg->kref, ceph_msg_release);
3524 EXPORT_SYMBOL(ceph_msg_put);
3526 void ceph_msg_dump(struct ceph_msg *msg)
3528 pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg,
3529 msg->front_alloc_len, msg->data_length);
3530 print_hex_dump(KERN_DEBUG, "header: ",
3531 DUMP_PREFIX_OFFSET, 16, 1,
3532 &msg->hdr, sizeof(msg->hdr), true);
3533 print_hex_dump(KERN_DEBUG, " front: ",
3534 DUMP_PREFIX_OFFSET, 16, 1,
3535 msg->front.iov_base, msg->front.iov_len, true);
3537 print_hex_dump(KERN_DEBUG, "middle: ",
3538 DUMP_PREFIX_OFFSET, 16, 1,
3539 msg->middle->vec.iov_base,
3540 msg->middle->vec.iov_len, true);
3541 print_hex_dump(KERN_DEBUG, "footer: ",
3542 DUMP_PREFIX_OFFSET, 16, 1,
3543 &msg->footer, sizeof(msg->footer), true);
3545 EXPORT_SYMBOL(ceph_msg_dump);