blob: 98ca23726ea675990de0ea60fc74f5241add99e3 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil31b80062009-10-06 11:31:13 -07002
3#include <linux/crc32c.h>
4#include <linux/ctype.h>
5#include <linux/highmem.h>
6#include <linux/inet.h>
7#include <linux/kthread.h>
8#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Sage Weil31b80062009-10-06 11:31:13 -070010#include <linux/socket.h>
11#include <linux/string.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070012#include <linux/bio.h>
13#include <linux/blkdev.h>
Noah Watkinsee3b56f2011-09-23 11:48:42 -070014#include <linux/dns_resolver.h>
Sage Weil31b80062009-10-06 11:31:13 -070015#include <net/tcp.h>
16
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070017#include <linux/ceph/libceph.h>
18#include <linux/ceph/messenger.h>
19#include <linux/ceph/decode.h>
20#include <linux/ceph/pagelist.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040021#include <linux/export.h>
Sage Weil31b80062009-10-06 11:31:13 -070022
23/*
24 * Ceph uses the messenger to exchange ceph_msg messages with other
25 * hosts in the system. The messenger provides ordered and reliable
26 * delivery. We tolerate TCP disconnects by reconnecting (with
27 * exponential backoff) in the case of a fault (disconnection, bad
28 * crc, protocol error). Acks allow sent messages to be discarded by
29 * the sender.
30 */
31
Alex Elderce2c8902012-05-22 22:15:49 -050032/* State values for ceph_connection->sock_state; NEW is assumed to be 0 */
33
34#define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
35#define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
36#define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
37#define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
38#define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
39
Sage Weil31b80062009-10-06 11:31:13 -070040/* static tag bytes (protocol control messages) */
41static char tag_msg = CEPH_MSGR_TAG_MSG;
42static char tag_ack = CEPH_MSGR_TAG_ACK;
43static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
44
Sage Weila6a53492010-04-13 14:07:07 -070045#ifdef CONFIG_LOCKDEP
46static struct lock_class_key socket_class;
47#endif
48
Alex Elder84495f42012-02-15 07:43:55 -060049/*
50 * When skipping (ignoring) a block of input we read it into a "skip
51 * buffer," which is this many bytes in size.
52 */
53#define SKIP_BUF_SIZE 1024
Sage Weil31b80062009-10-06 11:31:13 -070054
55static void queue_con(struct ceph_connection *con);
56static void con_work(struct work_struct *);
57static void ceph_fault(struct ceph_connection *con);
58
Sage Weil31b80062009-10-06 11:31:13 -070059/*
Alex Elderf64a9312012-01-23 15:49:27 -060060 * Nicely render a sockaddr as a string. An array of formatted
61 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -070062 */
Alex Elderf64a9312012-01-23 15:49:27 -060063#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
64#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
65#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
66#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
67
68static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
69static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -070070
Alex Elder57666512012-01-23 15:49:27 -060071static struct page *zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -060072
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070073const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -070074{
75 int i;
76 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -060077 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
78 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -070079
Alex Elderf64a9312012-01-23 15:49:27 -060080 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -070081 s = addr_str[i];
82
83 switch (ss->ss_family) {
84 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -060085 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
86 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -070087 break;
88
89 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -060090 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
91 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -070092 break;
93
94 default:
Alex Elderd3002b92012-02-14 14:05:33 -060095 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
96 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -070097 }
98
99 return s;
100}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700101EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700102
Sage Weil63f2d212009-11-03 15:17:56 -0800103static void encode_my_addr(struct ceph_messenger *msgr)
104{
105 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
106 ceph_encode_addr(&msgr->my_enc_addr);
107}
108
Sage Weil31b80062009-10-06 11:31:13 -0700109/*
110 * work queue for all reading and writing to/from the socket.
111 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600112static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700113
Alex Elder6173d1f2012-02-14 14:05:33 -0600114void _ceph_msgr_exit(void)
115{
Alex Elderd3002b92012-02-14 14:05:33 -0600116 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600117 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600118 ceph_msgr_wq = NULL;
119 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600120
Alex Elder6173d1f2012-02-14 14:05:33 -0600121 BUG_ON(zero_page == NULL);
122 kunmap(zero_page);
123 page_cache_release(zero_page);
124 zero_page = NULL;
125}
126
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700127int ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700128{
Alex Elder57666512012-01-23 15:49:27 -0600129 BUG_ON(zero_page != NULL);
130 zero_page = ZERO_PAGE(0);
131 page_cache_get(zero_page);
132
Tejun Heof363e452011-01-03 14:49:46 +0100133 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600134 if (ceph_msgr_wq)
135 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600136
Alex Elder6173d1f2012-02-14 14:05:33 -0600137 pr_err("msgr_init failed to create workqueue\n");
138 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600139
Alex Elder6173d1f2012-02-14 14:05:33 -0600140 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700141}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700142EXPORT_SYMBOL(ceph_msgr_init);
Sage Weil31b80062009-10-06 11:31:13 -0700143
144void ceph_msgr_exit(void)
145{
Alex Elder57666512012-01-23 15:49:27 -0600146 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600147
Alex Elder6173d1f2012-02-14 14:05:33 -0600148 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700149}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700150EXPORT_SYMBOL(ceph_msgr_exit);
Sage Weil31b80062009-10-06 11:31:13 -0700151
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700152void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700153{
154 flush_workqueue(ceph_msgr_wq);
155}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700156EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700157
Alex Elderce2c8902012-05-22 22:15:49 -0500158/* Connection socket state transition functions */
159
160static void con_sock_state_init(struct ceph_connection *con)
161{
162 int old_state;
163
164 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
165 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
166 printk("%s: unexpected old state %d\n", __func__, old_state);
167}
168
169static void con_sock_state_connecting(struct ceph_connection *con)
170{
171 int old_state;
172
173 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
174 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
175 printk("%s: unexpected old state %d\n", __func__, old_state);
176}
177
178static void con_sock_state_connected(struct ceph_connection *con)
179{
180 int old_state;
181
182 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
183 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
184 printk("%s: unexpected old state %d\n", __func__, old_state);
185}
186
187static void con_sock_state_closing(struct ceph_connection *con)
188{
189 int old_state;
190
191 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
192 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
193 old_state != CON_SOCK_STATE_CONNECTED &&
194 old_state != CON_SOCK_STATE_CLOSING))
195 printk("%s: unexpected old state %d\n", __func__, old_state);
196}
197
198static void con_sock_state_closed(struct ceph_connection *con)
199{
200 int old_state;
201
202 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
203 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
204 old_state != CON_SOCK_STATE_CLOSING))
205 printk("%s: unexpected old state %d\n", __func__, old_state);
206}
Sage Weila922d382010-05-29 09:41:23 -0700207
Sage Weil31b80062009-10-06 11:31:13 -0700208/*
209 * socket callback functions
210 */
211
212/* data available on socket, or listen socket received a connect */
Alex Elder327800b2012-05-22 11:41:43 -0500213static void ceph_sock_data_ready(struct sock *sk, int count_unused)
Sage Weil31b80062009-10-06 11:31:13 -0700214{
Alex Elderbd406142012-01-23 15:49:27 -0600215 struct ceph_connection *con = sk->sk_user_data;
216
Sage Weil31b80062009-10-06 11:31:13 -0700217 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500218 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700219 con, con->state);
220 queue_con(con);
221 }
222}
223
224/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500225static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700226{
Alex Elderd3002b92012-02-14 14:05:33 -0600227 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700228
Jim Schutt182fac22012-02-29 08:30:58 -0700229 /* only queue to workqueue if there is data we want to write,
230 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500231 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700232 * doesn't get called again until try_write() fills the socket
233 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
234 * and net/core/stream.c:sk_stream_write_space().
235 */
Alex Elder928443c2012-05-22 11:41:43 -0500236 if (test_bit(WRITE_PENDING, &con->flags)) {
Jim Schutt182fac22012-02-29 08:30:58 -0700237 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500238 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700239 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
240 queue_con(con);
241 }
Sage Weil31b80062009-10-06 11:31:13 -0700242 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500243 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700244 }
Sage Weil31b80062009-10-06 11:31:13 -0700245}
246
247/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500248static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700249{
Alex Elderbd406142012-01-23 15:49:27 -0600250 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700251
Alex Elder327800b2012-05-22 11:41:43 -0500252 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700253 con, con->state, sk->sk_state);
254
255 if (test_bit(CLOSED, &con->state))
256 return;
257
258 switch (sk->sk_state) {
259 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500260 dout("%s TCP_CLOSE\n", __func__);
Sage Weil31b80062009-10-06 11:31:13 -0700261 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500262 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500263 con_sock_state_closing(con);
Alex Elder928443c2012-05-22 11:41:43 -0500264 if (test_and_set_bit(SOCK_CLOSED, &con->flags) == 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700265 if (test_bit(CONNECTING, &con->state))
266 con->error_msg = "connection failed";
267 else
268 con->error_msg = "socket closed";
269 queue_con(con);
270 }
271 break;
272 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500273 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500274 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700275 queue_con(con);
276 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600277 default: /* Everything else is uninteresting */
278 break;
Sage Weil31b80062009-10-06 11:31:13 -0700279 }
280}
281
282/*
283 * set up socket callbacks
284 */
285static void set_sock_callbacks(struct socket *sock,
286 struct ceph_connection *con)
287{
288 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600289 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500290 sk->sk_data_ready = ceph_sock_data_ready;
291 sk->sk_write_space = ceph_sock_write_space;
292 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700293}
294
295
296/*
297 * socket helpers
298 */
299
300/*
301 * initiate connection to a remote socket.
302 */
Alex Elder41617d02012-02-14 14:05:33 -0600303static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700304{
Sage Weilf91d3472010-07-01 15:18:31 -0700305 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700306 struct socket *sock;
307 int ret;
308
309 BUG_ON(con->sock);
Sage Weilf91d3472010-07-01 15:18:31 -0700310 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
311 IPPROTO_TCP, &sock);
Sage Weil31b80062009-10-06 11:31:13 -0700312 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600313 return ret;
Sage Weil31b80062009-10-06 11:31:13 -0700314 sock->sk->sk_allocation = GFP_NOFS;
315
Sage Weila6a53492010-04-13 14:07:07 -0700316#ifdef CONFIG_LOCKDEP
317 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
318#endif
319
Sage Weil31b80062009-10-06 11:31:13 -0700320 set_sock_callbacks(sock, con);
321
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700322 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700323
Sage Weilf91d3472010-07-01 15:18:31 -0700324 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
325 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700326 if (ret == -EINPROGRESS) {
327 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700328 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700329 sock->sk->sk_state);
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600330 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700331 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700332 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700333 sock_release(sock);
Sage Weil31b80062009-10-06 11:31:13 -0700334 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -0700335
Alex Elder41617d02012-02-14 14:05:33 -0600336 return ret;
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600337 }
338 con->sock = sock;
Alex Elderce2c8902012-05-22 22:15:49 -0500339 con_sock_state_connecting(con);
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600340
Alex Elder41617d02012-02-14 14:05:33 -0600341 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700342}
343
344static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
345{
346 struct kvec iov = {buf, len};
347 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800348 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700349
Sage Weil98bdb0a2011-01-25 08:17:48 -0800350 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
351 if (r == -EAGAIN)
352 r = 0;
353 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700354}
355
356/*
357 * write something. @more is true if caller will be sending more data
358 * shortly.
359 */
360static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
361 size_t kvlen, size_t len, int more)
362{
363 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800364 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700365
366 if (more)
367 msg.msg_flags |= MSG_MORE;
368 else
369 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
370
Sage Weil42961d22011-01-25 08:19:34 -0800371 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
372 if (r == -EAGAIN)
373 r = 0;
374 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700375}
376
Alex Elder31739132012-03-07 11:40:08 -0600377static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
378 int offset, size_t size, int more)
379{
380 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
381 int ret;
382
383 ret = kernel_sendpage(sock, page, offset, size, flags);
384 if (ret == -EAGAIN)
385 ret = 0;
386
387 return ret;
388}
389
Sage Weil31b80062009-10-06 11:31:13 -0700390
391/*
392 * Shutdown/close the socket for the given connection.
393 */
394static int con_close_socket(struct ceph_connection *con)
395{
396 int rc;
397
398 dout("con_close_socket on %p sock %p\n", con, con->sock);
399 if (!con->sock)
400 return 0;
401 set_bit(SOCK_CLOSED, &con->state);
402 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
403 sock_release(con->sock);
404 con->sock = NULL;
405 clear_bit(SOCK_CLOSED, &con->state);
Alex Elderce2c8902012-05-22 22:15:49 -0500406 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700407 return rc;
408}
409
410/*
411 * Reset a connection. Discard all incoming and outgoing messages
412 * and clear *_seq state.
413 */
414static void ceph_msg_remove(struct ceph_msg *msg)
415{
416 list_del_init(&msg->list_head);
417 ceph_msg_put(msg);
418}
419static void ceph_msg_remove_list(struct list_head *head)
420{
421 while (!list_empty(head)) {
422 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
423 list_head);
424 ceph_msg_remove(msg);
425 }
426}
427
428static void reset_connection(struct ceph_connection *con)
429{
430 /* reset connection, out_queue, msg_ and connect_seq */
431 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700432 ceph_msg_remove_list(&con->out_queue);
433 ceph_msg_remove_list(&con->out_sent);
434
Sage Weilcf3e5c42009-12-11 09:48:05 -0800435 if (con->in_msg) {
436 ceph_msg_put(con->in_msg);
437 con->in_msg = NULL;
438 }
439
Sage Weil31b80062009-10-06 11:31:13 -0700440 con->connect_seq = 0;
441 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800442 if (con->out_msg) {
443 ceph_msg_put(con->out_msg);
444 con->out_msg = NULL;
445 }
Sage Weil31b80062009-10-06 11:31:13 -0700446 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700447 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700448}
449
450/*
451 * mark a peer down. drop any open connections.
452 */
453void ceph_con_close(struct ceph_connection *con)
454{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700455 dout("con_close %p peer %s\n", con,
456 ceph_pr_addr(&con->peer_addr.in_addr));
Alex Eldera5988c42012-05-29 11:04:58 -0500457 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700458 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Alex Eldera5988c42012-05-29 11:04:58 -0500459 set_bit(CLOSED, &con->state);
460
Alex Elder928443c2012-05-22 11:41:43 -0500461 clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */
462 clear_bit(KEEPALIVE_PENDING, &con->flags);
463 clear_bit(WRITE_PENDING, &con->flags);
Alex Eldera5988c42012-05-29 11:04:58 -0500464
Sage Weilec302642009-12-22 10:43:42 -0800465 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700466 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700467 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800468 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800469 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700470 queue_con(con);
471}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700472EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700473
474/*
Sage Weil31b80062009-10-06 11:31:13 -0700475 * Reopen a closed connection, with a new peer address.
476 */
477void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
478{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700479 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700480 set_bit(OPENING, &con->state);
Alex Eldera5988c42012-05-29 11:04:58 -0500481 WARN_ON(!test_and_clear_bit(CLOSED, &con->state));
482
Sage Weil31b80062009-10-06 11:31:13 -0700483 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800484 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700485 queue_con(con);
486}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700487EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700488
489/*
Sage Weil87b315a2010-03-22 14:51:18 -0700490 * return true if this connection ever successfully opened
491 */
492bool ceph_con_opened(struct ceph_connection *con)
493{
494 return con->connect_seq > 0;
495}
496
497/*
Sage Weil31b80062009-10-06 11:31:13 -0700498 * generic get/put
499 */
500struct ceph_connection *ceph_con_get(struct ceph_connection *con)
501{
Alex Elderd3002b92012-02-14 14:05:33 -0600502 int nref = __atomic_add_unless(&con->nref, 1, 0);
503
504 dout("con_get %p nref = %d -> %d\n", con, nref, nref + 1);
505
506 return nref ? con : NULL;
Sage Weil31b80062009-10-06 11:31:13 -0700507}
508
509void ceph_con_put(struct ceph_connection *con)
510{
Alex Elderd3002b92012-02-14 14:05:33 -0600511 int nref = atomic_dec_return(&con->nref);
512
513 BUG_ON(nref < 0);
514 if (nref == 0) {
Sage Weil71ececd2009-11-18 11:27:06 -0800515 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -0700516 kfree(con);
517 }
Alex Elderd3002b92012-02-14 14:05:33 -0600518 dout("con_put %p nref = %d -> %d\n", con, nref + 1, nref);
Sage Weil31b80062009-10-06 11:31:13 -0700519}
520
521/*
522 * initialize a new connection.
523 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500524void ceph_con_init(struct ceph_connection *con, void *private,
525 const struct ceph_connection_operations *ops,
526 struct ceph_messenger *msgr, __u8 entity_type, __u64 entity_num)
Sage Weil31b80062009-10-06 11:31:13 -0700527{
528 dout("con_init %p\n", con);
529 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500530 con->private = private;
531 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700532 atomic_set(&con->nref, 1);
533 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500534
535 con_sock_state_init(con);
536
Alex Elder1bfd89f2012-05-26 23:26:43 -0500537 con->peer_name.type = (__u8) entity_type;
538 con->peer_name.num = cpu_to_le64(entity_num);
539
Sage Weilec302642009-12-22 10:43:42 -0800540 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700541 INIT_LIST_HEAD(&con->out_queue);
542 INIT_LIST_HEAD(&con->out_sent);
543 INIT_DELAYED_WORK(&con->work, con_work);
Alex Eldera5988c42012-05-29 11:04:58 -0500544
545 set_bit(CLOSED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700546}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700547EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700548
549
550/*
551 * We maintain a global counter to order connection attempts. Get
552 * a unique seq greater than @gt.
553 */
554static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
555{
556 u32 ret;
557
558 spin_lock(&msgr->global_seq_lock);
559 if (msgr->global_seq < gt)
560 msgr->global_seq = gt;
561 ret = ++msgr->global_seq;
562 spin_unlock(&msgr->global_seq_lock);
563 return ret;
564}
565
Alex Eldere2200422012-05-23 14:35:23 -0500566static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600567{
568 con->out_kvec_left = 0;
569 con->out_kvec_bytes = 0;
570 con->out_kvec_cur = &con->out_kvec[0];
571}
572
Alex Eldere2200422012-05-23 14:35:23 -0500573static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600574 size_t size, void *data)
575{
576 int index;
577
578 index = con->out_kvec_left;
579 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
580
581 con->out_kvec[index].iov_len = size;
582 con->out_kvec[index].iov_base = data;
583 con->out_kvec_left++;
584 con->out_kvec_bytes += size;
585}
Sage Weil31b80062009-10-06 11:31:13 -0700586
587/*
588 * Prepare footer for currently outgoing message, and finish things
589 * off. Assumes out_kvec* are already valid.. we just add on to the end.
590 */
Alex Elder859eb792012-02-14 14:05:33 -0600591static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700592{
593 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600594 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700595
596 dout("prepare_write_message_footer %p\n", con);
597 con->out_kvec_is_msg = true;
598 con->out_kvec[v].iov_base = &m->footer;
599 con->out_kvec[v].iov_len = sizeof(m->footer);
600 con->out_kvec_bytes += sizeof(m->footer);
601 con->out_kvec_left++;
602 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800603 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700604}
605
606/*
607 * Prepare headers for the next outgoing message.
608 */
609static void prepare_write_message(struct ceph_connection *con)
610{
611 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600612 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700613
Alex Eldere2200422012-05-23 14:35:23 -0500614 con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700615 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800616 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700617
618 /* Sneak an ack in there first? If we can get it into the same
619 * TCP packet that's a good thing. */
620 if (con->in_seq > con->in_seq_acked) {
621 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -0500622 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700623 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500624 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600625 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700626 }
627
Alex Elder859eb792012-02-14 14:05:33 -0600628 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800629 con->out_msg = m;
Sage Weil4cf9d542011-07-26 11:27:24 -0700630
631 /* put message on sent list */
632 ceph_msg_get(m);
633 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700634
Sage Weile84346b2010-05-11 21:20:38 -0700635 /*
636 * only assign outgoing seq # if we haven't sent this message
637 * yet. if it is requeued, resend with it's original seq.
638 */
639 if (m->needs_out_seq) {
640 m->hdr.seq = cpu_to_le64(++con->out_seq);
641 m->needs_out_seq = false;
642 }
Sage Weil31b80062009-10-06 11:31:13 -0700643
644 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
645 m, con->out_seq, le16_to_cpu(m->hdr.type),
646 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
647 le32_to_cpu(m->hdr.data_len),
648 m->nr_pages);
649 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
650
651 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -0500652 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
653 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
654 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600655
Sage Weil31b80062009-10-06 11:31:13 -0700656 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -0500657 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600658 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700659
660 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600661 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
662 con->out_msg->hdr.crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -0700663 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
Alex Eldera9a0c512012-02-15 07:43:54 -0600664
665 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
666 con->out_msg->footer.front_crc = cpu_to_le32(crc);
667 if (m->middle) {
668 crc = crc32c(0, m->middle->vec.iov_base,
669 m->middle->vec.iov_len);
670 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
671 } else
Sage Weil31b80062009-10-06 11:31:13 -0700672 con->out_msg->footer.middle_crc = 0;
673 con->out_msg->footer.data_crc = 0;
674 dout("prepare_write_message front_crc %u data_crc %u\n",
675 le32_to_cpu(con->out_msg->footer.front_crc),
676 le32_to_cpu(con->out_msg->footer.middle_crc));
677
678 /* is there a data payload? */
679 if (le32_to_cpu(m->hdr.data_len) > 0) {
680 /* initialize page iterator */
681 con->out_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700682 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -0800683 con->out_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700684 else
685 con->out_msg_pos.page_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700686 con->out_msg_pos.data_pos = 0;
Alex Elderbca064d2012-02-15 07:43:54 -0600687 con->out_msg_pos.did_page_crc = false;
Sage Weil31b80062009-10-06 11:31:13 -0700688 con->out_more = 1; /* data + footer will follow */
689 } else {
690 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600691 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700692 }
693
Alex Elder928443c2012-05-22 11:41:43 -0500694 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700695}
696
697/*
698 * Prepare an ack.
699 */
700static void prepare_write_ack(struct ceph_connection *con)
701{
702 dout("prepare_write_ack %p %llu -> %llu\n", con,
703 con->in_seq_acked, con->in_seq);
704 con->in_seq_acked = con->in_seq;
705
Alex Eldere2200422012-05-23 14:35:23 -0500706 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600707
Alex Eldere2200422012-05-23 14:35:23 -0500708 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600709
Sage Weil31b80062009-10-06 11:31:13 -0700710 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500711 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600712 &con->out_temp_ack);
713
Sage Weil31b80062009-10-06 11:31:13 -0700714 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elder928443c2012-05-22 11:41:43 -0500715 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700716}
717
718/*
719 * Prepare to write keepalive byte.
720 */
721static void prepare_write_keepalive(struct ceph_connection *con)
722{
723 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -0500724 con_out_kvec_reset(con);
725 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Alex Elder928443c2012-05-22 11:41:43 -0500726 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700727}
728
729/*
730 * Connection negotiation.
731 */
732
Alex Elderdac1e712012-05-16 15:16:39 -0500733static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
734 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800735{
Alex Eldera3530df2012-05-16 15:16:39 -0500736 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -0500737
738 if (!con->ops->get_authorizer) {
739 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
740 con->out_connect.authorizer_len = 0;
741
Alex Elder729796b2012-05-16 15:16:39 -0500742 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -0500743 }
744
745 /* Can't hold the mutex while getting authorizer */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800746
Sage Weilec302642009-12-22 10:43:42 -0800747 mutex_unlock(&con->mutex);
Alex Elderb1c6b982012-05-16 15:16:38 -0500748
Alex Elderdac1e712012-05-16 15:16:39 -0500749 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Elder8f43fb52012-05-16 15:16:39 -0500750
Sage Weilec302642009-12-22 10:43:42 -0800751 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800752
Alex Eldera3530df2012-05-16 15:16:39 -0500753 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -0500754 return auth;
Alex Elder928443c2012-05-22 11:41:43 -0500755 if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags))
Alex Elder729796b2012-05-16 15:16:39 -0500756 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700757
Alex Elder8f43fb52012-05-16 15:16:39 -0500758 con->auth_reply_buf = auth->authorizer_reply_buf;
759 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
760
Alex Elder859eb792012-02-14 14:05:33 -0600761
Alex Elder729796b2012-05-16 15:16:39 -0500762 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800763}
764
Sage Weil31b80062009-10-06 11:31:13 -0700765/*
766 * We connected to a peer and are saying hello.
767 */
Alex Eldere825a662012-05-16 15:16:38 -0500768static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700769{
Alex Eldere2200422012-05-23 14:35:23 -0500770 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
771 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -0500772 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800773
Sage Weileed0ef22009-11-10 14:34:36 -0800774 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500775 set_bit(WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800776}
777
Alex Eldere825a662012-05-16 15:16:38 -0500778static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800779{
Sage Weil31b80062009-10-06 11:31:13 -0700780 unsigned global_seq = get_global_seq(con->msgr, 0);
781 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -0500782 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -0500783 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700784
785 switch (con->peer_name.type) {
786 case CEPH_ENTITY_TYPE_MON:
787 proto = CEPH_MONC_PROTOCOL;
788 break;
789 case CEPH_ENTITY_TYPE_OSD:
790 proto = CEPH_OSDC_PROTOCOL;
791 break;
792 case CEPH_ENTITY_TYPE_MDS:
793 proto = CEPH_MDSC_PROTOCOL;
794 break;
795 default:
796 BUG();
797 }
798
799 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
800 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800801
Alex Eldere825a662012-05-16 15:16:38 -0500802 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700803 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
804 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
805 con->out_connect.global_seq = cpu_to_le32(global_seq);
806 con->out_connect.protocol_version = cpu_to_le32(proto);
807 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700808
Alex Elderdac1e712012-05-16 15:16:39 -0500809 auth_proto = CEPH_AUTH_UNKNOWN;
810 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -0500811 if (IS_ERR(auth))
812 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -0500813
Alex Elderdac1e712012-05-16 15:16:39 -0500814 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -0500815 con->out_connect.authorizer_len = auth ?
816 cpu_to_le32(auth->authorizer_buf_len) : 0;
817
Alex Eldere2200422012-05-23 14:35:23 -0500818 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -0500819 &con->out_connect);
820 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -0500821 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -0500822 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600823
Sage Weil31b80062009-10-06 11:31:13 -0700824 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500825 set_bit(WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800826
Alex Eldere10c7582012-05-16 15:16:38 -0500827 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700828}
829
Sage Weil31b80062009-10-06 11:31:13 -0700830/*
831 * write as much of pending kvecs to the socket as we can.
832 * 1 -> done
833 * 0 -> socket full, but more to do
834 * <0 -> error
835 */
836static int write_partial_kvec(struct ceph_connection *con)
837{
838 int ret;
839
840 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
841 while (con->out_kvec_bytes > 0) {
842 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
843 con->out_kvec_left, con->out_kvec_bytes,
844 con->out_more);
845 if (ret <= 0)
846 goto out;
847 con->out_kvec_bytes -= ret;
848 if (con->out_kvec_bytes == 0)
849 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600850
851 /* account for full iov entries consumed */
852 while (ret >= con->out_kvec_cur->iov_len) {
853 BUG_ON(!con->out_kvec_left);
854 ret -= con->out_kvec_cur->iov_len;
855 con->out_kvec_cur++;
856 con->out_kvec_left--;
857 }
858 /* and for a partially-consumed entry */
859 if (ret) {
860 con->out_kvec_cur->iov_len -= ret;
861 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700862 }
863 }
864 con->out_kvec_left = 0;
865 con->out_kvec_is_msg = false;
866 ret = 1;
867out:
868 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
869 con->out_kvec_bytes, con->out_kvec_left, ret);
870 return ret; /* done! */
871}
872
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700873#ifdef CONFIG_BLOCK
874static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
875{
876 if (!bio) {
877 *iter = NULL;
878 *seg = 0;
879 return;
880 }
881 *iter = bio;
882 *seg = bio->bi_idx;
883}
884
885static void iter_bio_next(struct bio **bio_iter, int *seg)
886{
887 if (*bio_iter == NULL)
888 return;
889
890 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
891
892 (*seg)++;
893 if (*seg == (*bio_iter)->bi_vcnt)
894 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
895}
896#endif
897
Sage Weil31b80062009-10-06 11:31:13 -0700898/*
899 * Write as much message data payload as we can. If we finish, queue
900 * up the footer.
901 * 1 -> done, footer is now queued in out_kvec[].
902 * 0 -> socket full, but more to do
903 * <0 -> error
904 */
905static int write_partial_msg_pages(struct ceph_connection *con)
906{
907 struct ceph_msg *msg = con->out_msg;
908 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
909 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600910 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700911 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700912 int total_max_write;
913 int in_trail = 0;
914 size_t trail_len = (msg->trail ? msg->trail->length : 0);
Sage Weil31b80062009-10-06 11:31:13 -0700915
916 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
917 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
918 con->out_msg_pos.page_pos);
919
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700920#ifdef CONFIG_BLOCK
921 if (msg->bio && !msg->bio_iter)
922 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
923#endif
924
925 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -0700926 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700927 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -0600928 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700929
930 total_max_write = data_len - trail_len -
931 con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -0700932
933 /*
934 * if we are calculating the data crc (the default), we need
935 * to map the page. if our pages[] has been revoked, use the
936 * zero page.
937 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700938
939 /* have we reached the trail part of the data? */
940 if (con->out_msg_pos.data_pos >= data_len - trail_len) {
941 in_trail = 1;
942
943 total_max_write = data_len - con->out_msg_pos.data_pos;
944
945 page = list_first_entry(&msg->trail->head,
946 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700947 max_write = PAGE_SIZE;
948 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -0700949 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -0800950 } else if (msg->pagelist) {
951 page = list_first_entry(&msg->pagelist->head,
952 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700953#ifdef CONFIG_BLOCK
954 } else if (msg->bio) {
955 struct bio_vec *bv;
956
957 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
958 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -0600959 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700960 max_write = bv->bv_len;
961#endif
Sage Weil31b80062009-10-06 11:31:13 -0700962 } else {
Alex Elder57666512012-01-23 15:49:27 -0600963 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -0700964 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700965 len = min_t(int, max_write - con->out_msg_pos.page_pos,
966 total_max_write);
967
Alex Elder37675b02012-03-07 11:40:08 -0600968 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -0600969 void *base;
Alex Eldera9a0c512012-02-15 07:43:54 -0600970 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700971 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -0600972 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -0700973
Alex Elder8d63e312012-03-07 11:40:08 -0600974 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -0700975 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -0600976 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Eldera9a0c512012-02-15 07:43:54 -0600977 crc = crc32c(tmpcrc, base, len);
978 con->out_msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -0600979 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -0700980 }
Alex Eldere36b13c2012-03-07 11:40:08 -0600981 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -0600982 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -0600983 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -0700984
Alex Elder0cdf9e62012-03-07 11:40:08 -0600985 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -0700986 kunmap(page);
987
988 if (ret <= 0)
989 goto out;
990
991 con->out_msg_pos.data_pos += ret;
992 con->out_msg_pos.page_pos += ret;
993 if (ret == len) {
994 con->out_msg_pos.page_pos = 0;
995 con->out_msg_pos.page++;
Alex Elderbca064d2012-02-15 07:43:54 -0600996 con->out_msg_pos.did_page_crc = false;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700997 if (in_trail)
998 list_move_tail(&page->lru,
999 &msg->trail->head);
1000 else if (msg->pagelist)
Sage Weil58bb3b32009-12-23 12:12:31 -08001001 list_move_tail(&page->lru,
1002 &msg->pagelist->head);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001003#ifdef CONFIG_BLOCK
1004 else if (msg->bio)
1005 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
1006#endif
Sage Weil31b80062009-10-06 11:31:13 -07001007 }
1008 }
1009
1010 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1011
1012 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001013 if (!do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -07001014 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001015 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001016 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001017 ret = 1;
1018out:
1019 return ret;
1020}
1021
1022/*
1023 * write some zeros
1024 */
1025static int write_partial_skip(struct ceph_connection *con)
1026{
1027 int ret;
1028
1029 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001030 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001031
Alex Elder31739132012-03-07 11:40:08 -06001032 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001033 if (ret <= 0)
1034 goto out;
1035 con->out_skip -= ret;
1036 }
1037 ret = 1;
1038out:
1039 return ret;
1040}
1041
1042/*
1043 * Prepare to read connection handshake, or an ack.
1044 */
Sage Weileed0ef22009-11-10 14:34:36 -08001045static void prepare_read_banner(struct ceph_connection *con)
1046{
1047 dout("prepare_read_banner %p\n", con);
1048 con->in_base_pos = 0;
1049}
1050
Sage Weil31b80062009-10-06 11:31:13 -07001051static void prepare_read_connect(struct ceph_connection *con)
1052{
1053 dout("prepare_read_connect %p\n", con);
1054 con->in_base_pos = 0;
1055}
1056
1057static void prepare_read_ack(struct ceph_connection *con)
1058{
1059 dout("prepare_read_ack %p\n", con);
1060 con->in_base_pos = 0;
1061}
1062
1063static void prepare_read_tag(struct ceph_connection *con)
1064{
1065 dout("prepare_read_tag %p\n", con);
1066 con->in_base_pos = 0;
1067 con->in_tag = CEPH_MSGR_TAG_READY;
1068}
1069
1070/*
1071 * Prepare to read a message.
1072 */
1073static int prepare_read_message(struct ceph_connection *con)
1074{
1075 dout("prepare_read_message %p\n", con);
1076 BUG_ON(con->in_msg != NULL);
1077 con->in_base_pos = 0;
1078 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1079 return 0;
1080}
1081
1082
1083static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001084 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001085{
Alex Eldere6cee712012-05-10 10:29:50 -05001086 while (con->in_base_pos < end) {
1087 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001088 int have = size - left;
1089 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1090 if (ret <= 0)
1091 return ret;
1092 con->in_base_pos += ret;
1093 }
1094 return 1;
1095}
1096
1097
1098/*
1099 * Read all or part of the connect-side handshake on a new connection
1100 */
Sage Weileed0ef22009-11-10 14:34:36 -08001101static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001102{
Alex Elderfd516532012-05-10 10:29:50 -05001103 int size;
1104 int end;
1105 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001106
Sage Weileed0ef22009-11-10 14:34:36 -08001107 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001108
1109 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001110 size = strlen(CEPH_BANNER);
1111 end = size;
1112 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001113 if (ret <= 0)
1114 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001115
1116 size = sizeof (con->actual_peer_addr);
1117 end += size;
1118 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001119 if (ret <= 0)
1120 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001121
1122 size = sizeof (con->peer_addr_for_me);
1123 end += size;
1124 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001125 if (ret <= 0)
1126 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001127
Sage Weileed0ef22009-11-10 14:34:36 -08001128out:
1129 return ret;
1130}
1131
1132static int read_partial_connect(struct ceph_connection *con)
1133{
Alex Elderfd516532012-05-10 10:29:50 -05001134 int size;
1135 int end;
1136 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001137
1138 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1139
Alex Elderfd516532012-05-10 10:29:50 -05001140 size = sizeof (con->in_reply);
1141 end = size;
1142 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001143 if (ret <= 0)
1144 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001145
1146 size = le32_to_cpu(con->in_reply.authorizer_len);
1147 end += size;
1148 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001149 if (ret <= 0)
1150 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001151
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001152 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1153 con, (int)con->in_reply.tag,
1154 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001155 le32_to_cpu(con->in_reply.global_seq));
1156out:
1157 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001158
Sage Weil31b80062009-10-06 11:31:13 -07001159}
1160
1161/*
1162 * Verify the hello banner looks okay.
1163 */
1164static int verify_hello(struct ceph_connection *con)
1165{
1166 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001167 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001168 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001169 con->error_msg = "protocol error, bad banner";
1170 return -1;
1171 }
1172 return 0;
1173}
1174
1175static bool addr_is_blank(struct sockaddr_storage *ss)
1176{
1177 switch (ss->ss_family) {
1178 case AF_INET:
1179 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1180 case AF_INET6:
1181 return
1182 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1183 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1184 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1185 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1186 }
1187 return false;
1188}
1189
1190static int addr_port(struct sockaddr_storage *ss)
1191{
1192 switch (ss->ss_family) {
1193 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001194 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001195 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001196 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001197 }
1198 return 0;
1199}
1200
1201static void addr_set_port(struct sockaddr_storage *ss, int p)
1202{
1203 switch (ss->ss_family) {
1204 case AF_INET:
1205 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001206 break;
Sage Weil31b80062009-10-06 11:31:13 -07001207 case AF_INET6:
1208 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001209 break;
Sage Weil31b80062009-10-06 11:31:13 -07001210 }
1211}
1212
1213/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001214 * Unlike other *_pton function semantics, zero indicates success.
1215 */
1216static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1217 char delim, const char **ipend)
1218{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001219 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1220 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001221
1222 memset(ss, 0, sizeof(*ss));
1223
1224 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1225 ss->ss_family = AF_INET;
1226 return 0;
1227 }
1228
1229 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1230 ss->ss_family = AF_INET6;
1231 return 0;
1232 }
1233
1234 return -EINVAL;
1235}
1236
1237/*
1238 * Extract hostname string and resolve using kernel DNS facility.
1239 */
1240#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1241static int ceph_dns_resolve_name(const char *name, size_t namelen,
1242 struct sockaddr_storage *ss, char delim, const char **ipend)
1243{
1244 const char *end, *delim_p;
1245 char *colon_p, *ip_addr = NULL;
1246 int ip_len, ret;
1247
1248 /*
1249 * The end of the hostname occurs immediately preceding the delimiter or
1250 * the port marker (':') where the delimiter takes precedence.
1251 */
1252 delim_p = memchr(name, delim, namelen);
1253 colon_p = memchr(name, ':', namelen);
1254
1255 if (delim_p && colon_p)
1256 end = delim_p < colon_p ? delim_p : colon_p;
1257 else if (!delim_p && colon_p)
1258 end = colon_p;
1259 else {
1260 end = delim_p;
1261 if (!end) /* case: hostname:/ */
1262 end = name + namelen;
1263 }
1264
1265 if (end <= name)
1266 return -EINVAL;
1267
1268 /* do dns_resolve upcall */
1269 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1270 if (ip_len > 0)
1271 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1272 else
1273 ret = -ESRCH;
1274
1275 kfree(ip_addr);
1276
1277 *ipend = end;
1278
1279 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1280 ret, ret ? "failed" : ceph_pr_addr(ss));
1281
1282 return ret;
1283}
1284#else
1285static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1286 struct sockaddr_storage *ss, char delim, const char **ipend)
1287{
1288 return -EINVAL;
1289}
1290#endif
1291
1292/*
1293 * Parse a server name (IP or hostname). If a valid IP address is not found
1294 * then try to extract a hostname to resolve using userspace DNS upcall.
1295 */
1296static int ceph_parse_server_name(const char *name, size_t namelen,
1297 struct sockaddr_storage *ss, char delim, const char **ipend)
1298{
1299 int ret;
1300
1301 ret = ceph_pton(name, namelen, ss, delim, ipend);
1302 if (ret)
1303 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1304
1305 return ret;
1306}
1307
1308/*
Sage Weil31b80062009-10-06 11:31:13 -07001309 * Parse an ip[:port] list into an addr array. Use the default
1310 * monitor port if a port isn't specified.
1311 */
1312int ceph_parse_ips(const char *c, const char *end,
1313 struct ceph_entity_addr *addr,
1314 int max_count, int *count)
1315{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001316 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001317 const char *p = c;
1318
1319 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1320 for (i = 0; i < max_count; i++) {
1321 const char *ipend;
1322 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001323 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001324 char delim = ',';
1325
1326 if (*p == '[') {
1327 delim = ']';
1328 p++;
1329 }
Sage Weil31b80062009-10-06 11:31:13 -07001330
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001331 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1332 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001333 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001334 ret = -EINVAL;
1335
Sage Weil31b80062009-10-06 11:31:13 -07001336 p = ipend;
1337
Sage Weil39139f62010-07-08 09:54:52 -07001338 if (delim == ']') {
1339 if (*p != ']') {
1340 dout("missing matching ']'\n");
1341 goto bad;
1342 }
1343 p++;
1344 }
1345
Sage Weil31b80062009-10-06 11:31:13 -07001346 /* port? */
1347 if (p < end && *p == ':') {
1348 port = 0;
1349 p++;
1350 while (p < end && *p >= '0' && *p <= '9') {
1351 port = (port * 10) + (*p - '0');
1352 p++;
1353 }
1354 if (port > 65535 || port == 0)
1355 goto bad;
1356 } else {
1357 port = CEPH_MON_PORT;
1358 }
1359
1360 addr_set_port(ss, port);
1361
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001362 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001363
1364 if (p == end)
1365 break;
1366 if (*p != ',')
1367 goto bad;
1368 p++;
1369 }
1370
1371 if (p != end)
1372 goto bad;
1373
1374 if (count)
1375 *count = i + 1;
1376 return 0;
1377
1378bad:
Sage Weil39139f62010-07-08 09:54:52 -07001379 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001380 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001381}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001382EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001383
Sage Weileed0ef22009-11-10 14:34:36 -08001384static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001385{
Sage Weileed0ef22009-11-10 14:34:36 -08001386 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001387
1388 if (verify_hello(con) < 0)
1389 return -1;
1390
Sage Weil63f2d212009-11-03 15:17:56 -08001391 ceph_decode_addr(&con->actual_peer_addr);
1392 ceph_decode_addr(&con->peer_addr_for_me);
1393
Sage Weil31b80062009-10-06 11:31:13 -07001394 /*
1395 * Make sure the other end is who we wanted. note that the other
1396 * end may not yet know their ip address, so if it's 0.0.0.0, give
1397 * them the benefit of the doubt.
1398 */
Sage Weil103e2d32010-01-07 16:12:36 -08001399 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1400 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001401 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1402 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001403 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001404 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001405 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001406 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001407 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001408 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001409 return -1;
1410 }
1411
1412 /*
1413 * did we learn our address?
1414 */
1415 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1416 int port = addr_port(&con->msgr->inst.addr.in_addr);
1417
1418 memcpy(&con->msgr->inst.addr.in_addr,
1419 &con->peer_addr_for_me.in_addr,
1420 sizeof(con->peer_addr_for_me.in_addr));
1421 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001422 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001423 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001424 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001425 }
1426
Sage Weileed0ef22009-11-10 14:34:36 -08001427 set_bit(NEGOTIATING, &con->state);
1428 prepare_read_connect(con);
1429 return 0;
1430}
1431
Sage Weil04a419f2009-12-23 09:30:21 -08001432static void fail_protocol(struct ceph_connection *con)
1433{
1434 reset_connection(con);
1435 set_bit(CLOSED, &con->state); /* in case there's queued work */
Sage Weil04a419f2009-12-23 09:30:21 -08001436}
1437
Sage Weileed0ef22009-11-10 14:34:36 -08001438static int process_connect(struct ceph_connection *con)
1439{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001440 u64 sup_feat = con->msgr->supported_features;
1441 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001442 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001443 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001444
Sage Weileed0ef22009-11-10 14:34:36 -08001445 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1446
Sage Weil31b80062009-10-06 11:31:13 -07001447 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001448 case CEPH_MSGR_TAG_FEATURES:
1449 pr_err("%s%lld %s feature set mismatch,"
1450 " my %llx < server's %llx, missing %llx\n",
1451 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001452 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001453 sup_feat, server_feat, server_feat & ~sup_feat);
1454 con->error_msg = "missing required protocol features";
1455 fail_protocol(con);
1456 return -1;
1457
Sage Weil31b80062009-10-06 11:31:13 -07001458 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001459 pr_err("%s%lld %s protocol version mismatch,"
1460 " my %d != server's %d\n",
1461 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001462 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001463 le32_to_cpu(con->out_connect.protocol_version),
1464 le32_to_cpu(con->in_reply.protocol_version));
1465 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001466 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001467 return -1;
1468
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001469 case CEPH_MSGR_TAG_BADAUTHORIZER:
1470 con->auth_retry++;
1471 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1472 con->auth_retry);
1473 if (con->auth_retry == 2) {
1474 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001475 return -1;
1476 }
1477 con->auth_retry = 1;
Alex Eldere2200422012-05-23 14:35:23 -05001478 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001479 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001480 if (ret < 0)
1481 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001482 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001483 break;
Sage Weil31b80062009-10-06 11:31:13 -07001484
1485 case CEPH_MSGR_TAG_RESETSESSION:
1486 /*
1487 * If we connected with a large connect_seq but the peer
1488 * has no record of a session with us (no connection, or
1489 * connect_seq == 0), they will send RESETSESION to indicate
1490 * that they must have reset their session, and may have
1491 * dropped messages.
1492 */
1493 dout("process_connect got RESET peer seq %u\n",
1494 le32_to_cpu(con->in_connect.connect_seq));
1495 pr_err("%s%lld %s connection reset\n",
1496 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001497 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001498 reset_connection(con);
Alex Eldere2200422012-05-23 14:35:23 -05001499 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001500 ret = prepare_write_connect(con);
1501 if (ret < 0)
1502 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001503 prepare_read_connect(con);
1504
1505 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001506 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001507 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1508 if (con->ops->peer_reset)
1509 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001510 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07001511 if (test_bit(CLOSED, &con->state) ||
1512 test_bit(OPENING, &con->state))
1513 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001514 break;
1515
1516 case CEPH_MSGR_TAG_RETRY_SESSION:
1517 /*
1518 * If we sent a smaller connect_seq than the peer has, try
1519 * again with a larger value.
1520 */
1521 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1522 le32_to_cpu(con->out_connect.connect_seq),
1523 le32_to_cpu(con->in_connect.connect_seq));
1524 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Alex Eldere2200422012-05-23 14:35:23 -05001525 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001526 ret = prepare_write_connect(con);
1527 if (ret < 0)
1528 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001529 prepare_read_connect(con);
1530 break;
1531
1532 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1533 /*
1534 * If we sent a smaller global_seq than the peer has, try
1535 * again with a larger value.
1536 */
Sage Weileed0ef22009-11-10 14:34:36 -08001537 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001538 con->peer_global_seq,
1539 le32_to_cpu(con->in_connect.global_seq));
1540 get_global_seq(con->msgr,
1541 le32_to_cpu(con->in_connect.global_seq));
Alex Eldere2200422012-05-23 14:35:23 -05001542 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001543 ret = prepare_write_connect(con);
1544 if (ret < 0)
1545 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001546 prepare_read_connect(con);
1547 break;
1548
1549 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001550 if (req_feat & ~server_feat) {
1551 pr_err("%s%lld %s protocol feature mismatch,"
1552 " my required %llx > server's %llx, need %llx\n",
1553 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001554 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001555 req_feat, server_feat, req_feat & ~server_feat);
1556 con->error_msg = "missing required protocol features";
1557 fail_protocol(con);
1558 return -1;
1559 }
Sage Weil31b80062009-10-06 11:31:13 -07001560 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001561 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1562 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001563 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001564 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1565 con->peer_global_seq,
1566 le32_to_cpu(con->in_reply.connect_seq),
1567 con->connect_seq);
1568 WARN_ON(con->connect_seq !=
1569 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001570
1571 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elder928443c2012-05-22 11:41:43 -05001572 set_bit(LOSSYTX, &con->flags);
Sage Weil92ac41d2009-12-14 14:56:56 -08001573
Sage Weil31b80062009-10-06 11:31:13 -07001574 prepare_read_tag(con);
1575 break;
1576
1577 case CEPH_MSGR_TAG_WAIT:
1578 /*
1579 * If there is a connection race (we are opening
1580 * connections to each other), one of us may just have
1581 * to WAIT. This shouldn't happen if we are the
1582 * client.
1583 */
Sage Weil04177882011-05-12 15:33:17 -07001584 pr_err("process_connect got WAIT as client\n");
1585 con->error_msg = "protocol error, got WAIT as client";
1586 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001587
1588 default:
1589 pr_err("connect protocol error, will retry\n");
1590 con->error_msg = "protocol error, garbage tag during connect";
1591 return -1;
1592 }
1593 return 0;
1594}
1595
1596
1597/*
1598 * read (part of) an ack
1599 */
1600static int read_partial_ack(struct ceph_connection *con)
1601{
Alex Elderfd516532012-05-10 10:29:50 -05001602 int size = sizeof (con->in_temp_ack);
1603 int end = size;
1604
1605 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001606}
1607
1608
1609/*
1610 * We can finally discard anything that's been acked.
1611 */
1612static void process_ack(struct ceph_connection *con)
1613{
1614 struct ceph_msg *m;
1615 u64 ack = le64_to_cpu(con->in_temp_ack);
1616 u64 seq;
1617
Sage Weil31b80062009-10-06 11:31:13 -07001618 while (!list_empty(&con->out_sent)) {
1619 m = list_first_entry(&con->out_sent, struct ceph_msg,
1620 list_head);
1621 seq = le64_to_cpu(m->hdr.seq);
1622 if (seq > ack)
1623 break;
1624 dout("got ack for seq %llu type %d at %p\n", seq,
1625 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001626 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001627 ceph_msg_remove(m);
1628 }
Sage Weil31b80062009-10-06 11:31:13 -07001629 prepare_read_tag(con);
1630}
1631
1632
1633
1634
Yehuda Sadeh24504182010-01-08 13:58:34 -08001635static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001636 struct kvec *section,
1637 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001638{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001639 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001640
Yehuda Sadeh24504182010-01-08 13:58:34 -08001641 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001642
Yehuda Sadeh24504182010-01-08 13:58:34 -08001643 while (section->iov_len < sec_len) {
1644 BUG_ON(section->iov_base == NULL);
1645 left = sec_len - section->iov_len;
1646 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1647 section->iov_len, left);
1648 if (ret <= 0)
1649 return ret;
1650 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001651 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001652 if (section->iov_len == sec_len)
1653 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001654
1655 return 1;
1656}
1657
Alex Elder1c20f2d2012-06-04 14:43:32 -05001658static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
1659 struct ceph_msg_header *hdr);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001660
1661
1662static int read_partial_message_pages(struct ceph_connection *con,
1663 struct page **pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001664 unsigned data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001665{
1666 void *p;
1667 int ret;
1668 int left;
1669
1670 left = min((int)(data_len - con->in_msg_pos.data_pos),
1671 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1672 /* (page) data */
1673 BUG_ON(pages == NULL);
1674 p = kmap(pages[con->in_msg_pos.page]);
1675 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1676 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001677 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001678 con->in_data_crc =
1679 crc32c(con->in_data_crc,
1680 p + con->in_msg_pos.page_pos, ret);
1681 kunmap(pages[con->in_msg_pos.page]);
1682 if (ret <= 0)
1683 return ret;
1684 con->in_msg_pos.data_pos += ret;
1685 con->in_msg_pos.page_pos += ret;
1686 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1687 con->in_msg_pos.page_pos = 0;
1688 con->in_msg_pos.page++;
1689 }
1690
1691 return ret;
1692}
1693
1694#ifdef CONFIG_BLOCK
1695static int read_partial_message_bio(struct ceph_connection *con,
1696 struct bio **bio_iter, int *bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001697 unsigned data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001698{
1699 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1700 void *p;
1701 int ret, left;
1702
1703 if (IS_ERR(bv))
1704 return PTR_ERR(bv);
1705
1706 left = min((int)(data_len - con->in_msg_pos.data_pos),
1707 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1708
1709 p = kmap(bv->bv_page) + bv->bv_offset;
1710
1711 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1712 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001713 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001714 con->in_data_crc =
1715 crc32c(con->in_data_crc,
1716 p + con->in_msg_pos.page_pos, ret);
1717 kunmap(bv->bv_page);
1718 if (ret <= 0)
1719 return ret;
1720 con->in_msg_pos.data_pos += ret;
1721 con->in_msg_pos.page_pos += ret;
1722 if (con->in_msg_pos.page_pos == bv->bv_len) {
1723 con->in_msg_pos.page_pos = 0;
1724 iter_bio_next(bio_iter, bio_seg);
1725 }
1726
1727 return ret;
1728}
1729#endif
1730
Sage Weil31b80062009-10-06 11:31:13 -07001731/*
1732 * read (part of) a message.
1733 */
1734static int read_partial_message(struct ceph_connection *con)
1735{
1736 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05001737 int size;
1738 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001739 int ret;
Sage Weilc5c6b192010-11-09 12:40:00 -08001740 unsigned front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001741 bool do_datacrc = !con->msgr->nocrc;
Sage Weilae187562010-04-22 07:47:01 -07001742 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001743 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001744
1745 dout("read_partial_message con %p msg %p\n", con, m);
1746
1747 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05001748 size = sizeof (con->in_hdr);
1749 end = size;
1750 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05001751 if (ret <= 0)
1752 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001753
1754 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1755 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1756 pr_err("read_partial_message bad hdr "
1757 " crc %u != expected %u\n",
1758 crc, con->in_hdr.crc);
1759 return -EBADMSG;
1760 }
1761
Sage Weil31b80062009-10-06 11:31:13 -07001762 front_len = le32_to_cpu(con->in_hdr.front_len);
1763 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1764 return -EIO;
1765 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1766 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1767 return -EIO;
1768 data_len = le32_to_cpu(con->in_hdr.data_len);
1769 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1770 return -EIO;
1771
Sage Weilae187562010-04-22 07:47:01 -07001772 /* verify seq# */
1773 seq = le64_to_cpu(con->in_hdr.seq);
1774 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001775 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001776 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001777 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001778 seq, con->in_seq + 1);
1779 con->in_base_pos = -front_len - middle_len - data_len -
1780 sizeof(m->footer);
1781 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001782 return 0;
1783 } else if ((s64)seq - (s64)con->in_seq > 1) {
1784 pr_err("read_partial_message bad seq %lld expected %lld\n",
1785 seq, con->in_seq + 1);
1786 con->error_msg = "bad message sequence # for incoming message";
1787 return -EBADMSG;
1788 }
1789
Sage Weil31b80062009-10-06 11:31:13 -07001790 /* allocate message? */
1791 if (!con->in_msg) {
1792 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1793 con->in_hdr.front_len, con->in_hdr.data_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05001794 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
Sage Weil31b80062009-10-06 11:31:13 -07001795 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001796 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001797 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001798 con->in_base_pos = -front_len - middle_len - data_len -
1799 sizeof(m->footer);
1800 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001801 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001802 return 0;
1803 }
Sage Weila79832f2010-04-01 16:06:19 -07001804 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001805 con->error_msg =
1806 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001807 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001808 }
1809 m = con->in_msg;
1810 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001811 if (m->middle)
1812 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001813
1814 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001815 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001816 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001817 else
1818 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001819 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001820 }
1821
1822 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001823 ret = read_partial_message_section(con, &m->front, front_len,
1824 &con->in_front_crc);
1825 if (ret <= 0)
1826 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001827
1828 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001829 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001830 ret = read_partial_message_section(con, &m->middle->vec,
1831 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001832 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001833 if (ret <= 0)
1834 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001835 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001836#ifdef CONFIG_BLOCK
1837 if (m->bio && !m->bio_iter)
1838 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1839#endif
Sage Weil31b80062009-10-06 11:31:13 -07001840
1841 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001842 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001843 if (m->pages) {
1844 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001845 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001846 if (ret <= 0)
1847 return ret;
1848#ifdef CONFIG_BLOCK
1849 } else if (m->bio) {
1850
1851 ret = read_partial_message_bio(con,
1852 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001853 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001854 if (ret <= 0)
1855 return ret;
1856#endif
1857 } else {
1858 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001859 }
1860 }
1861
Sage Weil31b80062009-10-06 11:31:13 -07001862 /* footer */
Alex Elderfd516532012-05-10 10:29:50 -05001863 size = sizeof (m->footer);
1864 end += size;
1865 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05001866 if (ret <= 0)
1867 return ret;
1868
Sage Weil31b80062009-10-06 11:31:13 -07001869 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1870 m, front_len, m->footer.front_crc, middle_len,
1871 m->footer.middle_crc, data_len, m->footer.data_crc);
1872
1873 /* crc ok? */
1874 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1875 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1876 m, con->in_front_crc, m->footer.front_crc);
1877 return -EBADMSG;
1878 }
1879 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1880 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1881 m, con->in_middle_crc, m->footer.middle_crc);
1882 return -EBADMSG;
1883 }
Alex Elderbca064d2012-02-15 07:43:54 -06001884 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001885 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1886 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1887 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1888 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1889 return -EBADMSG;
1890 }
1891
1892 return 1; /* done! */
1893}
1894
1895/*
1896 * Process message. This happens in the worker thread. The callback should
1897 * be careful not to do anything that waits on other incoming messages or it
1898 * may deadlock.
1899 */
1900static void process_message(struct ceph_connection *con)
1901{
Sage Weil5e095e82009-12-14 14:30:34 -08001902 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001903
Sage Weil5e095e82009-12-14 14:30:34 -08001904 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001905 con->in_msg = NULL;
1906
1907 /* if first message, set peer_name */
1908 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001909 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001910
Sage Weil31b80062009-10-06 11:31:13 -07001911 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001912 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001913
1914 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1915 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001916 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001917 le16_to_cpu(msg->hdr.type),
1918 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1919 le32_to_cpu(msg->hdr.front_len),
1920 le32_to_cpu(msg->hdr.data_len),
1921 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1922 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001923
1924 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001925 prepare_read_tag(con);
1926}
1927
1928
1929/*
1930 * Write something to the socket. Called in a worker thread when the
1931 * socket appears to be writeable and we have something ready to send.
1932 */
1933static int try_write(struct ceph_connection *con)
1934{
Sage Weil31b80062009-10-06 11:31:13 -07001935 int ret = 1;
1936
1937 dout("try_write start %p state %lu nref %d\n", con, con->state,
1938 atomic_read(&con->nref));
1939
Sage Weil31b80062009-10-06 11:31:13 -07001940more:
1941 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1942
1943 /* open the socket first? */
1944 if (con->sock == NULL) {
Alex Eldera5988c42012-05-29 11:04:58 -05001945 clear_bit(NEGOTIATING, &con->state);
1946 set_bit(CONNECTING, &con->state);
1947
Alex Eldere2200422012-05-23 14:35:23 -05001948 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001949 prepare_write_banner(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001950 ret = prepare_write_connect(con);
1951 if (ret < 0)
1952 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08001953 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001954
Sage Weilcf3e5c42009-12-11 09:48:05 -08001955 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001956 con->in_tag = CEPH_MSGR_TAG_READY;
1957 dout("try_write initiating connect on %p new state %lu\n",
1958 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06001959 ret = ceph_tcp_connect(con);
1960 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07001961 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07001962 goto out;
1963 }
1964 }
1965
1966more_kvec:
1967 /* kvec data queued? */
1968 if (con->out_skip) {
1969 ret = write_partial_skip(con);
1970 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001971 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001972 }
1973 if (con->out_kvec_left) {
1974 ret = write_partial_kvec(con);
1975 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001976 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001977 }
1978
1979 /* msg pages? */
1980 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001981 if (con->out_msg_done) {
1982 ceph_msg_put(con->out_msg);
1983 con->out_msg = NULL; /* we're done with this one */
1984 goto do_next;
1985 }
1986
Sage Weil31b80062009-10-06 11:31:13 -07001987 ret = write_partial_msg_pages(con);
1988 if (ret == 1)
1989 goto more_kvec; /* we need to send the footer, too! */
1990 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08001991 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001992 if (ret < 0) {
1993 dout("try_write write_partial_msg_pages err %d\n",
1994 ret);
Sage Weil42961d22011-01-25 08:19:34 -08001995 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001996 }
1997 }
1998
Sage Weilc86a2932009-12-14 14:04:30 -08001999do_next:
Sage Weil31b80062009-10-06 11:31:13 -07002000 if (!test_bit(CONNECTING, &con->state)) {
2001 /* is anything else pending? */
2002 if (!list_empty(&con->out_queue)) {
2003 prepare_write_message(con);
2004 goto more;
2005 }
2006 if (con->in_seq > con->in_seq_acked) {
2007 prepare_write_ack(con);
2008 goto more;
2009 }
Alex Elder928443c2012-05-22 11:41:43 -05002010 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002011 prepare_write_keepalive(con);
2012 goto more;
2013 }
2014 }
2015
2016 /* Nothing to do! */
Alex Elder928443c2012-05-22 11:41:43 -05002017 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002018 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002019 ret = 0;
2020out:
Sage Weil42961d22011-01-25 08:19:34 -08002021 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002022 return ret;
2023}
2024
2025
2026
2027/*
2028 * Read what we can from the socket.
2029 */
2030static int try_read(struct ceph_connection *con)
2031{
Sage Weil31b80062009-10-06 11:31:13 -07002032 int ret = -1;
2033
2034 if (!con->sock)
2035 return 0;
2036
2037 if (test_bit(STANDBY, &con->state))
2038 return 0;
2039
2040 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08002041
Sage Weil31b80062009-10-06 11:31:13 -07002042more:
2043 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2044 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002045
2046 /*
2047 * process_connect and process_message drop and re-take
2048 * con->mutex. make sure we handle a racing close or reopen.
2049 */
2050 if (test_bit(CLOSED, &con->state) ||
2051 test_bit(OPENING, &con->state)) {
2052 ret = -EAGAIN;
2053 goto out;
2054 }
2055
Sage Weil31b80062009-10-06 11:31:13 -07002056 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08002057 if (!test_bit(NEGOTIATING, &con->state)) {
2058 dout("try_read connecting\n");
2059 ret = read_partial_banner(con);
2060 if (ret <= 0)
Sage Weileed0ef22009-11-10 14:34:36 -08002061 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002062 ret = process_banner(con);
2063 if (ret < 0)
2064 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08002065 }
Sage Weil31b80062009-10-06 11:31:13 -07002066 ret = read_partial_connect(con);
2067 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002068 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002069 ret = process_connect(con);
2070 if (ret < 0)
2071 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002072 goto more;
2073 }
2074
2075 if (con->in_base_pos < 0) {
2076 /*
2077 * skipping + discarding content.
2078 *
2079 * FIXME: there must be a better way to do this!
2080 */
Alex Elder84495f42012-02-15 07:43:55 -06002081 static char buf[SKIP_BUF_SIZE];
2082 int skip = min((int) sizeof (buf), -con->in_base_pos);
2083
Sage Weil31b80062009-10-06 11:31:13 -07002084 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2085 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2086 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002087 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002088 con->in_base_pos += ret;
2089 if (con->in_base_pos)
2090 goto more;
2091 }
2092 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2093 /*
2094 * what's next?
2095 */
2096 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2097 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002098 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002099 dout("try_read got tag %d\n", (int)con->in_tag);
2100 switch (con->in_tag) {
2101 case CEPH_MSGR_TAG_MSG:
2102 prepare_read_message(con);
2103 break;
2104 case CEPH_MSGR_TAG_ACK:
2105 prepare_read_ack(con);
2106 break;
2107 case CEPH_MSGR_TAG_CLOSE:
2108 set_bit(CLOSED, &con->state); /* fixme */
Sage Weil98bdb0a2011-01-25 08:17:48 -08002109 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002110 default:
2111 goto bad_tag;
2112 }
2113 }
2114 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2115 ret = read_partial_message(con);
2116 if (ret <= 0) {
2117 switch (ret) {
2118 case -EBADMSG:
2119 con->error_msg = "bad crc";
2120 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002121 break;
Sage Weil31b80062009-10-06 11:31:13 -07002122 case -EIO:
2123 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002124 break;
Sage Weil31b80062009-10-06 11:31:13 -07002125 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002126 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002127 }
2128 if (con->in_tag == CEPH_MSGR_TAG_READY)
2129 goto more;
2130 process_message(con);
2131 goto more;
2132 }
2133 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2134 ret = read_partial_ack(con);
2135 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002136 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002137 process_ack(con);
2138 goto more;
2139 }
2140
Sage Weil31b80062009-10-06 11:31:13 -07002141out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002142 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002143 return ret;
2144
2145bad_tag:
2146 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2147 con->error_msg = "protocol error, garbage tag";
2148 ret = -1;
2149 goto out;
2150}
2151
2152
2153/*
2154 * Atomically queue work on a connection. Bump @con reference to
2155 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002156 */
2157static void queue_con(struct ceph_connection *con)
2158{
Sage Weil31b80062009-10-06 11:31:13 -07002159 if (!con->ops->get(con)) {
2160 dout("queue_con %p ref count 0\n", con);
2161 return;
2162 }
2163
Tejun Heof363e452011-01-03 14:49:46 +01002164 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002165 dout("queue_con %p - already queued\n", con);
2166 con->ops->put(con);
2167 } else {
2168 dout("queue_con %p\n", con);
2169 }
2170}
2171
2172/*
2173 * Do some work on a connection. Drop a connection ref when we're done.
2174 */
2175static void con_work(struct work_struct *work)
2176{
2177 struct ceph_connection *con = container_of(work, struct ceph_connection,
2178 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002179 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002180
Sage Weil9dd46582010-04-28 13:51:50 -07002181 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002182restart:
Alex Elder928443c2012-05-22 11:41:43 -05002183 if (test_and_clear_bit(BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002184 dout("con_work %p backing off\n", con);
2185 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2186 round_jiffies_relative(con->delay))) {
2187 dout("con_work %p backoff %lu\n", con, con->delay);
2188 mutex_unlock(&con->mutex);
2189 return;
2190 } else {
2191 con->ops->put(con);
2192 dout("con_work %p FAILED to back off %lu\n", con,
2193 con->delay);
2194 }
2195 }
Sage Weil9dd46582010-04-28 13:51:50 -07002196
Sage Weile00de342011-03-04 12:25:05 -08002197 if (test_bit(STANDBY, &con->state)) {
2198 dout("con_work %p STANDBY\n", con);
2199 goto done;
2200 }
Sage Weil31b80062009-10-06 11:31:13 -07002201 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2202 dout("con_work CLOSED\n");
2203 con_close_socket(con);
2204 goto done;
2205 }
2206 if (test_and_clear_bit(OPENING, &con->state)) {
2207 /* reopen w/ new peer */
2208 dout("con_work OPENING\n");
2209 con_close_socket(con);
2210 }
2211
Alex Elder928443c2012-05-22 11:41:43 -05002212 if (test_and_clear_bit(SOCK_CLOSED, &con->flags))
Sage Weil0da5d702011-05-19 11:21:05 -07002213 goto fault;
2214
2215 ret = try_read(con);
2216 if (ret == -EAGAIN)
2217 goto restart;
2218 if (ret < 0)
2219 goto fault;
2220
2221 ret = try_write(con);
2222 if (ret == -EAGAIN)
2223 goto restart;
2224 if (ret < 0)
2225 goto fault;
Sage Weil31b80062009-10-06 11:31:13 -07002226
2227done:
Sage Weil9dd46582010-04-28 13:51:50 -07002228 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002229done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002230 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002231 return;
2232
2233fault:
2234 mutex_unlock(&con->mutex);
2235 ceph_fault(con); /* error/fault path */
2236 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002237}
2238
2239
2240/*
2241 * Generic error/fault handler. A retry mechanism is used with
2242 * exponential backoff
2243 */
2244static void ceph_fault(struct ceph_connection *con)
2245{
2246 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002247 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002248 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002249 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002250
Alex Elder928443c2012-05-22 11:41:43 -05002251 if (test_bit(LOSSYTX, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002252 dout("fault on LOSSYTX channel\n");
2253 goto out;
2254 }
2255
Sage Weilec302642009-12-22 10:43:42 -08002256 mutex_lock(&con->mutex);
Sage Weil91e45ce32010-02-15 12:05:09 -08002257 if (test_bit(CLOSED, &con->state))
2258 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002259
Sage Weil31b80062009-10-06 11:31:13 -07002260 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002261
2262 if (con->in_msg) {
2263 ceph_msg_put(con->in_msg);
2264 con->in_msg = NULL;
2265 }
Sage Weil31b80062009-10-06 11:31:13 -07002266
Sage Weile80a52d2010-02-25 12:40:45 -08002267 /* Requeue anything that hasn't been acked */
2268 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002269
Sage Weile76661d2011-03-03 10:10:15 -08002270 /* If there are no messages queued or keepalive pending, place
2271 * the connection in a STANDBY state */
2272 if (list_empty(&con->out_queue) &&
Alex Elder928443c2012-05-22 11:41:43 -05002273 !test_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weile00de342011-03-04 12:25:05 -08002274 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elder928443c2012-05-22 11:41:43 -05002275 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002276 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08002277 } else {
2278 /* retry after a delay. */
2279 if (con->delay == 0)
2280 con->delay = BASE_DELAY_INTERVAL;
2281 else if (con->delay < MAX_DELAY_INTERVAL)
2282 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002283 con->ops->get(con);
2284 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002285 round_jiffies_relative(con->delay))) {
2286 dout("fault queued %p delay %lu\n", con, con->delay);
2287 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002288 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002289 dout("fault failed to queue %p delay %lu, backoff\n",
2290 con, con->delay);
2291 /*
2292 * In many cases we see a socket state change
2293 * while con_work is running and end up
2294 * queuing (non-delayed) work, such that we
2295 * can't backoff with a delay. Set a flag so
2296 * that when con_work restarts we schedule the
2297 * delay then.
2298 */
Alex Elder928443c2012-05-22 11:41:43 -05002299 set_bit(BACKOFF, &con->flags);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002300 }
Sage Weil31b80062009-10-06 11:31:13 -07002301 }
2302
Sage Weil91e45ce32010-02-15 12:05:09 -08002303out_unlock:
2304 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002305out:
Sage Weil161fd652010-02-25 12:38:57 -08002306 /*
2307 * in case we faulted due to authentication, invalidate our
2308 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002309 */
Sage Weil161fd652010-02-25 12:38:57 -08002310 if (con->auth_retry && con->ops->invalidate_authorizer) {
2311 dout("calling invalidate_authorizer()\n");
2312 con->ops->invalidate_authorizer(con);
2313 }
2314
Sage Weil31b80062009-10-06 11:31:13 -07002315 if (con->ops->fault)
2316 con->ops->fault(con);
2317}
2318
2319
2320
2321/*
Alex Elder15d98822012-05-26 23:26:43 -05002322 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002323 */
Alex Elder15d98822012-05-26 23:26:43 -05002324void ceph_messenger_init(struct ceph_messenger *msgr,
2325 struct ceph_entity_addr *myaddr,
2326 u32 supported_features,
2327 u32 required_features,
2328 bool nocrc)
Sage Weil31b80062009-10-06 11:31:13 -07002329{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002330 msgr->supported_features = supported_features;
2331 msgr->required_features = required_features;
2332
Sage Weil31b80062009-10-06 11:31:13 -07002333 spin_lock_init(&msgr->global_seq_lock);
2334
Sage Weil31b80062009-10-06 11:31:13 -07002335 if (myaddr)
2336 msgr->inst.addr = *myaddr;
2337
2338 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002339 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002340 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002341 encode_my_addr(msgr);
Alex Elder15d98822012-05-26 23:26:43 -05002342 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002343
Alex Elder15d98822012-05-26 23:26:43 -05002344 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002345}
Alex Elder15d98822012-05-26 23:26:43 -05002346EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002347
Sage Weile00de342011-03-04 12:25:05 -08002348static void clear_standby(struct ceph_connection *con)
2349{
2350 /* come back from STANDBY? */
2351 if (test_and_clear_bit(STANDBY, &con->state)) {
2352 mutex_lock(&con->mutex);
2353 dout("clear_standby %p and ++connect_seq\n", con);
2354 con->connect_seq++;
Alex Elder928443c2012-05-22 11:41:43 -05002355 WARN_ON(test_bit(WRITE_PENDING, &con->flags));
2356 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags));
Sage Weile00de342011-03-04 12:25:05 -08002357 mutex_unlock(&con->mutex);
2358 }
2359}
2360
Sage Weil31b80062009-10-06 11:31:13 -07002361/*
2362 * Queue up an outgoing message on the given connection.
2363 */
2364void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2365{
2366 if (test_bit(CLOSED, &con->state)) {
2367 dout("con_send %p closed, dropping %p\n", con, msg);
2368 ceph_msg_put(msg);
2369 return;
2370 }
2371
2372 /* set src+dst */
Sage Weildbad1852010-03-25 15:45:38 -07002373 msg->hdr.src = con->msgr->inst.name;
Sage Weil31b80062009-10-06 11:31:13 -07002374
Sage Weil3ca02ef2010-03-01 15:25:00 -08002375 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2376
Sage Weile84346b2010-05-11 21:20:38 -07002377 msg->needs_out_seq = true;
2378
Sage Weil31b80062009-10-06 11:31:13 -07002379 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08002380 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002381 BUG_ON(!list_empty(&msg->list_head));
2382 list_add_tail(&msg->list_head, &con->out_queue);
2383 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2384 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2385 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2386 le32_to_cpu(msg->hdr.front_len),
2387 le32_to_cpu(msg->hdr.middle_len),
2388 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002389 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002390
2391 /* if there wasn't anything waiting to send before, queue
2392 * new work */
Sage Weile00de342011-03-04 12:25:05 -08002393 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002394 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002395 queue_con(con);
2396}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002397EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002398
2399/*
2400 * Revoke a message that was previously queued for send
2401 */
2402void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
2403{
Sage Weilec302642009-12-22 10:43:42 -08002404 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002405 if (!list_empty(&msg->list_head)) {
Sage Weiled98ada2010-07-05 12:15:14 -07002406 dout("con_revoke %p msg %p - was on queue\n", con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002407 list_del_init(&msg->list_head);
2408 ceph_msg_put(msg);
2409 msg->hdr.seq = 0;
Sage Weiled98ada2010-07-05 12:15:14 -07002410 }
2411 if (con->out_msg == msg) {
2412 dout("con_revoke %p msg %p - was sending\n", con, msg);
2413 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002414 if (con->out_kvec_is_msg) {
2415 con->out_skip = con->out_kvec_bytes;
2416 con->out_kvec_is_msg = false;
2417 }
Sage Weiled98ada2010-07-05 12:15:14 -07002418 ceph_msg_put(msg);
2419 msg->hdr.seq = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002420 }
Sage Weilec302642009-12-22 10:43:42 -08002421 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002422}
2423
2424/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002425 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002426 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002427void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002428{
2429 mutex_lock(&con->mutex);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002430 if (con->in_msg && con->in_msg == msg) {
2431 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2432 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002433 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2434
2435 /* skip rest of message */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002436 dout("con_revoke_pages %p msg %p revoked\n", con, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002437 con->in_base_pos = con->in_base_pos -
2438 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002439 front_len -
2440 middle_len -
2441 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002442 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002443 ceph_msg_put(con->in_msg);
2444 con->in_msg = NULL;
2445 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002446 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002447 } else {
2448 dout("con_revoke_pages %p msg %p pages %p no-op\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002449 con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002450 }
2451 mutex_unlock(&con->mutex);
2452}
2453
2454/*
Sage Weil31b80062009-10-06 11:31:13 -07002455 * Queue a keepalive byte to ensure the tcp connection is alive.
2456 */
2457void ceph_con_keepalive(struct ceph_connection *con)
2458{
Sage Weile00de342011-03-04 12:25:05 -08002459 dout("con_keepalive %p\n", con);
2460 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002461 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2462 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002463 queue_con(con);
2464}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002465EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002466
2467
2468/*
2469 * construct a new message with given type, size
2470 * the new msg has a ref count of 1.
2471 */
Sage Weilb61c2762011-08-09 15:03:46 -07002472struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2473 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002474{
2475 struct ceph_msg *m;
2476
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002477 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002478 if (m == NULL)
2479 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002480 kref_init(&m->kref);
Sage Weil31b80062009-10-06 11:31:13 -07002481 INIT_LIST_HEAD(&m->list_head);
2482
Sage Weil45c6ceb2010-05-11 15:01:51 -07002483 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002484 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002485 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2486 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002487 m->hdr.front_len = cpu_to_le32(front_len);
2488 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002489 m->hdr.data_len = 0;
2490 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002491 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002492 m->footer.front_crc = 0;
2493 m->footer.middle_crc = 0;
2494 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002495 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002496 m->front_max = front_len;
2497 m->front_is_vmalloc = false;
2498 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002499 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002500 m->pool = NULL;
2501
Henry C Changca208922011-05-03 02:29:56 +00002502 /* middle */
2503 m->middle = NULL;
2504
2505 /* data */
2506 m->nr_pages = 0;
2507 m->page_alignment = 0;
2508 m->pages = NULL;
2509 m->pagelist = NULL;
2510 m->bio = NULL;
2511 m->bio_iter = NULL;
2512 m->bio_seg = 0;
2513 m->trail = NULL;
2514
Sage Weil31b80062009-10-06 11:31:13 -07002515 /* front */
2516 if (front_len) {
2517 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002518 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002519 PAGE_KERNEL);
2520 m->front_is_vmalloc = true;
2521 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002522 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002523 }
2524 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002525 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002526 front_len);
2527 goto out2;
2528 }
2529 } else {
2530 m->front.iov_base = NULL;
2531 }
2532 m->front.iov_len = front_len;
2533
Sage Weilbb257662010-04-01 16:07:23 -07002534 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002535 return m;
2536
2537out2:
2538 ceph_msg_put(m);
2539out:
Sage Weilb61c2762011-08-09 15:03:46 -07002540 if (!can_fail) {
2541 pr_err("msg_new can't create type %d front %d\n", type,
2542 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002543 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002544 } else {
2545 dout("msg_new can't create type %d front %d\n", type,
2546 front_len);
2547 }
Sage Weila79832f2010-04-01 16:06:19 -07002548 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002549}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002550EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002551
2552/*
Sage Weil31b80062009-10-06 11:31:13 -07002553 * Allocate "middle" portion of a message, if it is needed and wasn't
2554 * allocated by alloc_msg. This allows us to read a small fixed-size
2555 * per-type header in the front and then gracefully fail (i.e.,
2556 * propagate the error to the caller based on info in the front) when
2557 * the middle is too large.
2558 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002559static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002560{
2561 int type = le16_to_cpu(msg->hdr.type);
2562 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2563
2564 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2565 ceph_msg_type_name(type), middle_len);
2566 BUG_ON(!middle_len);
2567 BUG_ON(msg->middle);
2568
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002569 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002570 if (!msg->middle)
2571 return -ENOMEM;
2572 return 0;
2573}
2574
Yehuda Sadeh24504182010-01-08 13:58:34 -08002575/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05002576 * Allocate a message for receiving an incoming message on a
2577 * connection, and save the result in con->in_msg. Uses the
2578 * connection's private alloc_msg op if available.
2579 *
2580 * Returns true if the message should be skipped, false otherwise.
2581 * If true is returned (skip message), con->in_msg will be NULL.
2582 * If false is returned, con->in_msg will contain a pointer to the
2583 * newly-allocated message, or NULL in case of memory exhaustion.
Yehuda Sadeh24504182010-01-08 13:58:34 -08002584 */
Alex Elder1c20f2d2012-06-04 14:43:32 -05002585static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2586 struct ceph_msg_header *hdr)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002587{
2588 int type = le16_to_cpu(hdr->type);
2589 int front_len = le32_to_cpu(hdr->front_len);
2590 int middle_len = le32_to_cpu(hdr->middle_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002591 int ret;
2592
Alex Elder1c20f2d2012-06-04 14:43:32 -05002593 BUG_ON(con->in_msg != NULL);
2594
Yehuda Sadeh24504182010-01-08 13:58:34 -08002595 if (con->ops->alloc_msg) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002596 int skip = 0;
2597
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002598 mutex_unlock(&con->mutex);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002599 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002600 mutex_lock(&con->mutex);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002601 if (skip)
2602 con->in_msg = NULL;
2603
2604 if (!con->in_msg)
2605 return skip != 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002606 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002607 if (!con->in_msg) {
2608 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2609 if (!con->in_msg) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002610 pr_err("unable to allocate msg type %d len %d\n",
2611 type, front_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002612 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002613 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002614 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002615 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002616 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002617
Alex Elder1c20f2d2012-06-04 14:43:32 -05002618 if (middle_len && !con->in_msg->middle) {
2619 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002620 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002621 ceph_msg_put(con->in_msg);
2622 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002623 }
2624 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002625
Alex Elder1c20f2d2012-06-04 14:43:32 -05002626 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002627}
2628
Sage Weil31b80062009-10-06 11:31:13 -07002629
2630/*
2631 * Free a generically kmalloc'd message.
2632 */
2633void ceph_msg_kfree(struct ceph_msg *m)
2634{
2635 dout("msg_kfree %p\n", m);
2636 if (m->front_is_vmalloc)
2637 vfree(m->front.iov_base);
2638 else
2639 kfree(m->front.iov_base);
2640 kfree(m);
2641}
2642
2643/*
2644 * Drop a msg ref. Destroy as needed.
2645 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002646void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002647{
Sage Weilc2e552e2009-12-07 15:55:05 -08002648 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002649
Sage Weilc2e552e2009-12-07 15:55:05 -08002650 dout("ceph_msg_put last one on %p\n", m);
2651 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002652
Sage Weilc2e552e2009-12-07 15:55:05 -08002653 /* drop middle, data, if any */
2654 if (m->middle) {
2655 ceph_buffer_put(m->middle);
2656 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002657 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002658 m->nr_pages = 0;
2659 m->pages = NULL;
2660
Sage Weil58bb3b32009-12-23 12:12:31 -08002661 if (m->pagelist) {
2662 ceph_pagelist_release(m->pagelist);
2663 kfree(m->pagelist);
2664 m->pagelist = NULL;
2665 }
2666
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002667 m->trail = NULL;
2668
Sage Weilc2e552e2009-12-07 15:55:05 -08002669 if (m->pool)
2670 ceph_msgpool_put(m->pool, m);
2671 else
2672 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002673}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002674EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002675
2676void ceph_msg_dump(struct ceph_msg *msg)
2677{
2678 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2679 msg->front_max, msg->nr_pages);
2680 print_hex_dump(KERN_DEBUG, "header: ",
2681 DUMP_PREFIX_OFFSET, 16, 1,
2682 &msg->hdr, sizeof(msg->hdr), true);
2683 print_hex_dump(KERN_DEBUG, " front: ",
2684 DUMP_PREFIX_OFFSET, 16, 1,
2685 msg->front.iov_base, msg->front.iov_len, true);
2686 if (msg->middle)
2687 print_hex_dump(KERN_DEBUG, "middle: ",
2688 DUMP_PREFIX_OFFSET, 16, 1,
2689 msg->middle->vec.iov_base,
2690 msg->middle->vec.iov_len, true);
2691 print_hex_dump(KERN_DEBUG, "footer: ",
2692 DUMP_PREFIX_OFFSET, 16, 1,
2693 &msg->footer, sizeof(msg->footer), true);
2694}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002695EXPORT_SYMBOL(ceph_msg_dump);