blob: ab690e2e1206582f04a1f0e542ee696f612e0958 [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 Weil89a86be2012-06-09 14:19:21 -0700324 con_sock_state_connecting(con);
Sage Weilf91d3472010-07-01 15:18:31 -0700325 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
326 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700327 if (ret == -EINPROGRESS) {
328 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700329 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700330 sock->sk->sk_state);
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600331 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700332 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700333 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700334 sock_release(sock);
Sage Weil31b80062009-10-06 11:31:13 -0700335 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -0700336
Alex Elder41617d02012-02-14 14:05:33 -0600337 return ret;
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600338 }
339 con->sock = sock;
Alex Elder41617d02012-02-14 14:05:33 -0600340 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700341}
342
343static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
344{
345 struct kvec iov = {buf, len};
346 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800347 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700348
Sage Weil98bdb0a2011-01-25 08:17:48 -0800349 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
350 if (r == -EAGAIN)
351 r = 0;
352 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700353}
354
355/*
356 * write something. @more is true if caller will be sending more data
357 * shortly.
358 */
359static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
360 size_t kvlen, size_t len, int more)
361{
362 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800363 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700364
365 if (more)
366 msg.msg_flags |= MSG_MORE;
367 else
368 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
369
Sage Weil42961d22011-01-25 08:19:34 -0800370 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
371 if (r == -EAGAIN)
372 r = 0;
373 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700374}
375
Alex Elder31739132012-03-07 11:40:08 -0600376static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
377 int offset, size_t size, int more)
378{
379 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
380 int ret;
381
382 ret = kernel_sendpage(sock, page, offset, size, flags);
383 if (ret == -EAGAIN)
384 ret = 0;
385
386 return ret;
387}
388
Sage Weil31b80062009-10-06 11:31:13 -0700389
390/*
391 * Shutdown/close the socket for the given connection.
392 */
393static int con_close_socket(struct ceph_connection *con)
394{
395 int rc;
396
397 dout("con_close_socket on %p sock %p\n", con, con->sock);
398 if (!con->sock)
399 return 0;
400 set_bit(SOCK_CLOSED, &con->state);
401 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
402 sock_release(con->sock);
403 con->sock = NULL;
404 clear_bit(SOCK_CLOSED, &con->state);
Alex Elderce2c8902012-05-22 22:15:49 -0500405 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700406 return rc;
407}
408
409/*
410 * Reset a connection. Discard all incoming and outgoing messages
411 * and clear *_seq state.
412 */
413static void ceph_msg_remove(struct ceph_msg *msg)
414{
415 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500416 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -0700417 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -0500418 msg->con = NULL;
419
Sage Weil31b80062009-10-06 11:31:13 -0700420 ceph_msg_put(msg);
421}
422static void ceph_msg_remove_list(struct list_head *head)
423{
424 while (!list_empty(head)) {
425 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
426 list_head);
427 ceph_msg_remove(msg);
428 }
429}
430
431static void reset_connection(struct ceph_connection *con)
432{
433 /* reset connection, out_queue, msg_ and connect_seq */
434 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700435 ceph_msg_remove_list(&con->out_queue);
436 ceph_msg_remove_list(&con->out_sent);
437
Sage Weilcf3e5c42009-12-11 09:48:05 -0800438 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500439 BUG_ON(con->in_msg->con != con);
440 con->in_msg->con = NULL;
Sage Weilcf3e5c42009-12-11 09:48:05 -0800441 ceph_msg_put(con->in_msg);
442 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -0700443 con->ops->put(con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800444 }
445
Sage Weil31b80062009-10-06 11:31:13 -0700446 con->connect_seq = 0;
447 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800448 if (con->out_msg) {
449 ceph_msg_put(con->out_msg);
450 con->out_msg = NULL;
451 }
Sage Weil31b80062009-10-06 11:31:13 -0700452 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700453 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700454}
455
456/*
457 * mark a peer down. drop any open connections.
458 */
459void ceph_con_close(struct ceph_connection *con)
460{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700461 dout("con_close %p peer %s\n", con,
462 ceph_pr_addr(&con->peer_addr.in_addr));
Alex Eldera5988c42012-05-29 11:04:58 -0500463 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700464 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Alex Eldera5988c42012-05-29 11:04:58 -0500465 set_bit(CLOSED, &con->state);
466
Alex Elder928443c2012-05-22 11:41:43 -0500467 clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */
468 clear_bit(KEEPALIVE_PENDING, &con->flags);
469 clear_bit(WRITE_PENDING, &con->flags);
Alex Eldera5988c42012-05-29 11:04:58 -0500470
Sage Weilec302642009-12-22 10:43:42 -0800471 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700472 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700473 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800474 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800475 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700476 queue_con(con);
477}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700478EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700479
480/*
Sage Weil31b80062009-10-06 11:31:13 -0700481 * Reopen a closed connection, with a new peer address.
482 */
483void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
484{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700485 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700486 set_bit(OPENING, &con->state);
Alex Eldera5988c42012-05-29 11:04:58 -0500487 WARN_ON(!test_and_clear_bit(CLOSED, &con->state));
488
Sage Weil31b80062009-10-06 11:31:13 -0700489 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800490 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700491 queue_con(con);
492}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700493EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700494
495/*
Sage Weil87b315a2010-03-22 14:51:18 -0700496 * return true if this connection ever successfully opened
497 */
498bool ceph_con_opened(struct ceph_connection *con)
499{
500 return con->connect_seq > 0;
501}
502
503/*
Sage Weil31b80062009-10-06 11:31:13 -0700504 * initialize a new connection.
505 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500506void ceph_con_init(struct ceph_connection *con, void *private,
507 const struct ceph_connection_operations *ops,
508 struct ceph_messenger *msgr, __u8 entity_type, __u64 entity_num)
Sage Weil31b80062009-10-06 11:31:13 -0700509{
510 dout("con_init %p\n", con);
511 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500512 con->private = private;
513 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700514 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500515
516 con_sock_state_init(con);
517
Alex Elder1bfd89f2012-05-26 23:26:43 -0500518 con->peer_name.type = (__u8) entity_type;
519 con->peer_name.num = cpu_to_le64(entity_num);
520
Sage Weilec302642009-12-22 10:43:42 -0800521 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700522 INIT_LIST_HEAD(&con->out_queue);
523 INIT_LIST_HEAD(&con->out_sent);
524 INIT_DELAYED_WORK(&con->work, con_work);
Alex Eldera5988c42012-05-29 11:04:58 -0500525
526 set_bit(CLOSED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700527}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700528EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700529
530
531/*
532 * We maintain a global counter to order connection attempts. Get
533 * a unique seq greater than @gt.
534 */
535static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
536{
537 u32 ret;
538
539 spin_lock(&msgr->global_seq_lock);
540 if (msgr->global_seq < gt)
541 msgr->global_seq = gt;
542 ret = ++msgr->global_seq;
543 spin_unlock(&msgr->global_seq_lock);
544 return ret;
545}
546
Alex Eldere2200422012-05-23 14:35:23 -0500547static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600548{
549 con->out_kvec_left = 0;
550 con->out_kvec_bytes = 0;
551 con->out_kvec_cur = &con->out_kvec[0];
552}
553
Alex Eldere2200422012-05-23 14:35:23 -0500554static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600555 size_t size, void *data)
556{
557 int index;
558
559 index = con->out_kvec_left;
560 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
561
562 con->out_kvec[index].iov_len = size;
563 con->out_kvec[index].iov_base = data;
564 con->out_kvec_left++;
565 con->out_kvec_bytes += size;
566}
Sage Weil31b80062009-10-06 11:31:13 -0700567
568/*
569 * Prepare footer for currently outgoing message, and finish things
570 * off. Assumes out_kvec* are already valid.. we just add on to the end.
571 */
Alex Elder859eb792012-02-14 14:05:33 -0600572static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700573{
574 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600575 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700576
577 dout("prepare_write_message_footer %p\n", con);
578 con->out_kvec_is_msg = true;
579 con->out_kvec[v].iov_base = &m->footer;
580 con->out_kvec[v].iov_len = sizeof(m->footer);
581 con->out_kvec_bytes += sizeof(m->footer);
582 con->out_kvec_left++;
583 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800584 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700585}
586
587/*
588 * Prepare headers for the next outgoing message.
589 */
590static void prepare_write_message(struct ceph_connection *con)
591{
592 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600593 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700594
Alex Eldere2200422012-05-23 14:35:23 -0500595 con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700596 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800597 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700598
599 /* Sneak an ack in there first? If we can get it into the same
600 * TCP packet that's a good thing. */
601 if (con->in_seq > con->in_seq_acked) {
602 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -0500603 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700604 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500605 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600606 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700607 }
608
Alex Elder38941f82012-06-01 14:56:43 -0500609 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -0600610 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800611 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -0500612 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -0700613
614 /* put message on sent list */
615 ceph_msg_get(m);
616 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700617
Sage Weile84346b2010-05-11 21:20:38 -0700618 /*
619 * only assign outgoing seq # if we haven't sent this message
620 * yet. if it is requeued, resend with it's original seq.
621 */
622 if (m->needs_out_seq) {
623 m->hdr.seq = cpu_to_le64(++con->out_seq);
624 m->needs_out_seq = false;
625 }
Yan, Zheng43643522012-06-06 19:35:55 -0500626#ifdef CONFIG_BLOCK
627 else
628 m->bio_iter = NULL;
629#endif
Sage Weil31b80062009-10-06 11:31:13 -0700630
631 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
632 m, con->out_seq, le16_to_cpu(m->hdr.type),
633 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
634 le32_to_cpu(m->hdr.data_len),
635 m->nr_pages);
636 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
637
638 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -0500639 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
640 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
641 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600642
Sage Weil31b80062009-10-06 11:31:13 -0700643 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -0500644 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600645 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700646
647 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600648 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
649 con->out_msg->hdr.crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -0700650 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
Alex Eldera9a0c512012-02-15 07:43:54 -0600651
652 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
653 con->out_msg->footer.front_crc = cpu_to_le32(crc);
654 if (m->middle) {
655 crc = crc32c(0, m->middle->vec.iov_base,
656 m->middle->vec.iov_len);
657 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
658 } else
Sage Weil31b80062009-10-06 11:31:13 -0700659 con->out_msg->footer.middle_crc = 0;
660 con->out_msg->footer.data_crc = 0;
661 dout("prepare_write_message front_crc %u data_crc %u\n",
662 le32_to_cpu(con->out_msg->footer.front_crc),
663 le32_to_cpu(con->out_msg->footer.middle_crc));
664
665 /* is there a data payload? */
666 if (le32_to_cpu(m->hdr.data_len) > 0) {
667 /* initialize page iterator */
668 con->out_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700669 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -0800670 con->out_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700671 else
672 con->out_msg_pos.page_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700673 con->out_msg_pos.data_pos = 0;
Alex Elderbca064d2012-02-15 07:43:54 -0600674 con->out_msg_pos.did_page_crc = false;
Sage Weil31b80062009-10-06 11:31:13 -0700675 con->out_more = 1; /* data + footer will follow */
676 } else {
677 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600678 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700679 }
680
Alex Elder928443c2012-05-22 11:41:43 -0500681 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700682}
683
684/*
685 * Prepare an ack.
686 */
687static void prepare_write_ack(struct ceph_connection *con)
688{
689 dout("prepare_write_ack %p %llu -> %llu\n", con,
690 con->in_seq_acked, con->in_seq);
691 con->in_seq_acked = con->in_seq;
692
Alex Eldere2200422012-05-23 14:35:23 -0500693 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600694
Alex Eldere2200422012-05-23 14:35:23 -0500695 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600696
Sage Weil31b80062009-10-06 11:31:13 -0700697 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500698 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600699 &con->out_temp_ack);
700
Sage Weil31b80062009-10-06 11:31:13 -0700701 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elder928443c2012-05-22 11:41:43 -0500702 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700703}
704
705/*
706 * Prepare to write keepalive byte.
707 */
708static void prepare_write_keepalive(struct ceph_connection *con)
709{
710 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -0500711 con_out_kvec_reset(con);
712 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Alex Elder928443c2012-05-22 11:41:43 -0500713 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700714}
715
716/*
717 * Connection negotiation.
718 */
719
Alex Elderdac1e712012-05-16 15:16:39 -0500720static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
721 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800722{
Alex Eldera3530df2012-05-16 15:16:39 -0500723 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -0500724
725 if (!con->ops->get_authorizer) {
726 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
727 con->out_connect.authorizer_len = 0;
728
Alex Elder729796b2012-05-16 15:16:39 -0500729 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -0500730 }
731
732 /* Can't hold the mutex while getting authorizer */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800733
Sage Weilec302642009-12-22 10:43:42 -0800734 mutex_unlock(&con->mutex);
Alex Elderb1c6b982012-05-16 15:16:38 -0500735
Alex Elderdac1e712012-05-16 15:16:39 -0500736 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Elder8f43fb52012-05-16 15:16:39 -0500737
Sage Weilec302642009-12-22 10:43:42 -0800738 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800739
Alex Eldera3530df2012-05-16 15:16:39 -0500740 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -0500741 return auth;
Alex Elder928443c2012-05-22 11:41:43 -0500742 if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags))
Alex Elder729796b2012-05-16 15:16:39 -0500743 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700744
Alex Elder8f43fb52012-05-16 15:16:39 -0500745 con->auth_reply_buf = auth->authorizer_reply_buf;
746 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
747
Alex Elder859eb792012-02-14 14:05:33 -0600748
Alex Elder729796b2012-05-16 15:16:39 -0500749 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800750}
751
Sage Weil31b80062009-10-06 11:31:13 -0700752/*
753 * We connected to a peer and are saying hello.
754 */
Alex Eldere825a662012-05-16 15:16:38 -0500755static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700756{
Alex Eldere2200422012-05-23 14:35:23 -0500757 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
758 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -0500759 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800760
Sage Weileed0ef22009-11-10 14:34:36 -0800761 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500762 set_bit(WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800763}
764
Alex Eldere825a662012-05-16 15:16:38 -0500765static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800766{
Eric Dumazet95c96172012-04-15 05:58:06 +0000767 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -0700768 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -0500769 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -0500770 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700771
772 switch (con->peer_name.type) {
773 case CEPH_ENTITY_TYPE_MON:
774 proto = CEPH_MONC_PROTOCOL;
775 break;
776 case CEPH_ENTITY_TYPE_OSD:
777 proto = CEPH_OSDC_PROTOCOL;
778 break;
779 case CEPH_ENTITY_TYPE_MDS:
780 proto = CEPH_MDSC_PROTOCOL;
781 break;
782 default:
783 BUG();
784 }
785
786 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
787 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800788
Alex Eldere825a662012-05-16 15:16:38 -0500789 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700790 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
791 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
792 con->out_connect.global_seq = cpu_to_le32(global_seq);
793 con->out_connect.protocol_version = cpu_to_le32(proto);
794 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700795
Alex Elderdac1e712012-05-16 15:16:39 -0500796 auth_proto = CEPH_AUTH_UNKNOWN;
797 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -0500798 if (IS_ERR(auth))
799 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -0500800
Alex Elderdac1e712012-05-16 15:16:39 -0500801 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -0500802 con->out_connect.authorizer_len = auth ?
803 cpu_to_le32(auth->authorizer_buf_len) : 0;
804
Alex Eldere2200422012-05-23 14:35:23 -0500805 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -0500806 &con->out_connect);
807 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -0500808 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -0500809 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600810
Sage Weil31b80062009-10-06 11:31:13 -0700811 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500812 set_bit(WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800813
Alex Eldere10c7582012-05-16 15:16:38 -0500814 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700815}
816
Sage Weil31b80062009-10-06 11:31:13 -0700817/*
818 * write as much of pending kvecs to the socket as we can.
819 * 1 -> done
820 * 0 -> socket full, but more to do
821 * <0 -> error
822 */
823static int write_partial_kvec(struct ceph_connection *con)
824{
825 int ret;
826
827 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
828 while (con->out_kvec_bytes > 0) {
829 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
830 con->out_kvec_left, con->out_kvec_bytes,
831 con->out_more);
832 if (ret <= 0)
833 goto out;
834 con->out_kvec_bytes -= ret;
835 if (con->out_kvec_bytes == 0)
836 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600837
838 /* account for full iov entries consumed */
839 while (ret >= con->out_kvec_cur->iov_len) {
840 BUG_ON(!con->out_kvec_left);
841 ret -= con->out_kvec_cur->iov_len;
842 con->out_kvec_cur++;
843 con->out_kvec_left--;
844 }
845 /* and for a partially-consumed entry */
846 if (ret) {
847 con->out_kvec_cur->iov_len -= ret;
848 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700849 }
850 }
851 con->out_kvec_left = 0;
852 con->out_kvec_is_msg = false;
853 ret = 1;
854out:
855 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
856 con->out_kvec_bytes, con->out_kvec_left, ret);
857 return ret; /* done! */
858}
859
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700860#ifdef CONFIG_BLOCK
861static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
862{
863 if (!bio) {
864 *iter = NULL;
865 *seg = 0;
866 return;
867 }
868 *iter = bio;
869 *seg = bio->bi_idx;
870}
871
872static void iter_bio_next(struct bio **bio_iter, int *seg)
873{
874 if (*bio_iter == NULL)
875 return;
876
877 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
878
879 (*seg)++;
880 if (*seg == (*bio_iter)->bi_vcnt)
881 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
882}
883#endif
884
Sage Weil31b80062009-10-06 11:31:13 -0700885/*
886 * Write as much message data payload as we can. If we finish, queue
887 * up the footer.
888 * 1 -> done, footer is now queued in out_kvec[].
889 * 0 -> socket full, but more to do
890 * <0 -> error
891 */
892static int write_partial_msg_pages(struct ceph_connection *con)
893{
894 struct ceph_msg *msg = con->out_msg;
Eric Dumazet95c96172012-04-15 05:58:06 +0000895 unsigned int data_len = le32_to_cpu(msg->hdr.data_len);
Sage Weil31b80062009-10-06 11:31:13 -0700896 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600897 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700898 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700899 int total_max_write;
900 int in_trail = 0;
901 size_t trail_len = (msg->trail ? msg->trail->length : 0);
Sage Weil31b80062009-10-06 11:31:13 -0700902
903 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
904 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
905 con->out_msg_pos.page_pos);
906
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700907#ifdef CONFIG_BLOCK
908 if (msg->bio && !msg->bio_iter)
909 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
910#endif
911
912 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -0700913 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700914 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -0600915 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700916
917 total_max_write = data_len - trail_len -
918 con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -0700919
920 /*
921 * if we are calculating the data crc (the default), we need
922 * to map the page. if our pages[] has been revoked, use the
923 * zero page.
924 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700925
926 /* have we reached the trail part of the data? */
927 if (con->out_msg_pos.data_pos >= data_len - trail_len) {
928 in_trail = 1;
929
930 total_max_write = data_len - con->out_msg_pos.data_pos;
931
932 page = list_first_entry(&msg->trail->head,
933 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700934 max_write = PAGE_SIZE;
935 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -0700936 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -0800937 } else if (msg->pagelist) {
938 page = list_first_entry(&msg->pagelist->head,
939 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700940#ifdef CONFIG_BLOCK
941 } else if (msg->bio) {
942 struct bio_vec *bv;
943
944 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
945 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -0600946 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700947 max_write = bv->bv_len;
948#endif
Sage Weil31b80062009-10-06 11:31:13 -0700949 } else {
Alex Elder57666512012-01-23 15:49:27 -0600950 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -0700951 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700952 len = min_t(int, max_write - con->out_msg_pos.page_pos,
953 total_max_write);
954
Alex Elder37675b02012-03-07 11:40:08 -0600955 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -0600956 void *base;
Alex Eldera9a0c512012-02-15 07:43:54 -0600957 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700958 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -0600959 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -0700960
Alex Elder8d63e312012-03-07 11:40:08 -0600961 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -0700962 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -0600963 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Eldera9a0c512012-02-15 07:43:54 -0600964 crc = crc32c(tmpcrc, base, len);
965 con->out_msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -0600966 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -0700967 }
Alex Eldere36b13c2012-03-07 11:40:08 -0600968 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -0600969 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -0600970 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -0700971
Alex Elder0cdf9e62012-03-07 11:40:08 -0600972 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -0700973 kunmap(page);
974
975 if (ret <= 0)
976 goto out;
977
978 con->out_msg_pos.data_pos += ret;
979 con->out_msg_pos.page_pos += ret;
980 if (ret == len) {
981 con->out_msg_pos.page_pos = 0;
982 con->out_msg_pos.page++;
Alex Elderbca064d2012-02-15 07:43:54 -0600983 con->out_msg_pos.did_page_crc = false;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700984 if (in_trail)
985 list_move_tail(&page->lru,
986 &msg->trail->head);
987 else if (msg->pagelist)
Sage Weil58bb3b32009-12-23 12:12:31 -0800988 list_move_tail(&page->lru,
989 &msg->pagelist->head);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700990#ifdef CONFIG_BLOCK
991 else if (msg->bio)
992 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
993#endif
Sage Weil31b80062009-10-06 11:31:13 -0700994 }
995 }
996
997 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
998
999 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001000 if (!do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -07001001 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001002 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001003 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001004 ret = 1;
1005out:
1006 return ret;
1007}
1008
1009/*
1010 * write some zeros
1011 */
1012static int write_partial_skip(struct ceph_connection *con)
1013{
1014 int ret;
1015
1016 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001017 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001018
Alex Elder31739132012-03-07 11:40:08 -06001019 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001020 if (ret <= 0)
1021 goto out;
1022 con->out_skip -= ret;
1023 }
1024 ret = 1;
1025out:
1026 return ret;
1027}
1028
1029/*
1030 * Prepare to read connection handshake, or an ack.
1031 */
Sage Weileed0ef22009-11-10 14:34:36 -08001032static void prepare_read_banner(struct ceph_connection *con)
1033{
1034 dout("prepare_read_banner %p\n", con);
1035 con->in_base_pos = 0;
1036}
1037
Sage Weil31b80062009-10-06 11:31:13 -07001038static void prepare_read_connect(struct ceph_connection *con)
1039{
1040 dout("prepare_read_connect %p\n", con);
1041 con->in_base_pos = 0;
1042}
1043
1044static void prepare_read_ack(struct ceph_connection *con)
1045{
1046 dout("prepare_read_ack %p\n", con);
1047 con->in_base_pos = 0;
1048}
1049
1050static void prepare_read_tag(struct ceph_connection *con)
1051{
1052 dout("prepare_read_tag %p\n", con);
1053 con->in_base_pos = 0;
1054 con->in_tag = CEPH_MSGR_TAG_READY;
1055}
1056
1057/*
1058 * Prepare to read a message.
1059 */
1060static int prepare_read_message(struct ceph_connection *con)
1061{
1062 dout("prepare_read_message %p\n", con);
1063 BUG_ON(con->in_msg != NULL);
1064 con->in_base_pos = 0;
1065 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1066 return 0;
1067}
1068
1069
1070static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001071 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001072{
Alex Eldere6cee712012-05-10 10:29:50 -05001073 while (con->in_base_pos < end) {
1074 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001075 int have = size - left;
1076 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1077 if (ret <= 0)
1078 return ret;
1079 con->in_base_pos += ret;
1080 }
1081 return 1;
1082}
1083
1084
1085/*
1086 * Read all or part of the connect-side handshake on a new connection
1087 */
Sage Weileed0ef22009-11-10 14:34:36 -08001088static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001089{
Alex Elderfd516532012-05-10 10:29:50 -05001090 int size;
1091 int end;
1092 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001093
Sage Weileed0ef22009-11-10 14:34:36 -08001094 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001095
1096 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001097 size = strlen(CEPH_BANNER);
1098 end = size;
1099 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001100 if (ret <= 0)
1101 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001102
1103 size = sizeof (con->actual_peer_addr);
1104 end += size;
1105 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001106 if (ret <= 0)
1107 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001108
1109 size = sizeof (con->peer_addr_for_me);
1110 end += size;
1111 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001112 if (ret <= 0)
1113 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001114
Sage Weileed0ef22009-11-10 14:34:36 -08001115out:
1116 return ret;
1117}
1118
1119static int read_partial_connect(struct ceph_connection *con)
1120{
Alex Elderfd516532012-05-10 10:29:50 -05001121 int size;
1122 int end;
1123 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001124
1125 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1126
Alex Elderfd516532012-05-10 10:29:50 -05001127 size = sizeof (con->in_reply);
1128 end = size;
1129 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001130 if (ret <= 0)
1131 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001132
1133 size = le32_to_cpu(con->in_reply.authorizer_len);
1134 end += size;
1135 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001136 if (ret <= 0)
1137 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001138
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001139 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1140 con, (int)con->in_reply.tag,
1141 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001142 le32_to_cpu(con->in_reply.global_seq));
1143out:
1144 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001145
Sage Weil31b80062009-10-06 11:31:13 -07001146}
1147
1148/*
1149 * Verify the hello banner looks okay.
1150 */
1151static int verify_hello(struct ceph_connection *con)
1152{
1153 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001154 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001155 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001156 con->error_msg = "protocol error, bad banner";
1157 return -1;
1158 }
1159 return 0;
1160}
1161
1162static bool addr_is_blank(struct sockaddr_storage *ss)
1163{
1164 switch (ss->ss_family) {
1165 case AF_INET:
1166 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1167 case AF_INET6:
1168 return
1169 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1170 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1171 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1172 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1173 }
1174 return false;
1175}
1176
1177static int addr_port(struct sockaddr_storage *ss)
1178{
1179 switch (ss->ss_family) {
1180 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001181 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001182 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001183 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001184 }
1185 return 0;
1186}
1187
1188static void addr_set_port(struct sockaddr_storage *ss, int p)
1189{
1190 switch (ss->ss_family) {
1191 case AF_INET:
1192 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001193 break;
Sage Weil31b80062009-10-06 11:31:13 -07001194 case AF_INET6:
1195 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001196 break;
Sage Weil31b80062009-10-06 11:31:13 -07001197 }
1198}
1199
1200/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001201 * Unlike other *_pton function semantics, zero indicates success.
1202 */
1203static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1204 char delim, const char **ipend)
1205{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001206 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1207 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001208
1209 memset(ss, 0, sizeof(*ss));
1210
1211 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1212 ss->ss_family = AF_INET;
1213 return 0;
1214 }
1215
1216 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1217 ss->ss_family = AF_INET6;
1218 return 0;
1219 }
1220
1221 return -EINVAL;
1222}
1223
1224/*
1225 * Extract hostname string and resolve using kernel DNS facility.
1226 */
1227#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1228static int ceph_dns_resolve_name(const char *name, size_t namelen,
1229 struct sockaddr_storage *ss, char delim, const char **ipend)
1230{
1231 const char *end, *delim_p;
1232 char *colon_p, *ip_addr = NULL;
1233 int ip_len, ret;
1234
1235 /*
1236 * The end of the hostname occurs immediately preceding the delimiter or
1237 * the port marker (':') where the delimiter takes precedence.
1238 */
1239 delim_p = memchr(name, delim, namelen);
1240 colon_p = memchr(name, ':', namelen);
1241
1242 if (delim_p && colon_p)
1243 end = delim_p < colon_p ? delim_p : colon_p;
1244 else if (!delim_p && colon_p)
1245 end = colon_p;
1246 else {
1247 end = delim_p;
1248 if (!end) /* case: hostname:/ */
1249 end = name + namelen;
1250 }
1251
1252 if (end <= name)
1253 return -EINVAL;
1254
1255 /* do dns_resolve upcall */
1256 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1257 if (ip_len > 0)
1258 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1259 else
1260 ret = -ESRCH;
1261
1262 kfree(ip_addr);
1263
1264 *ipend = end;
1265
1266 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1267 ret, ret ? "failed" : ceph_pr_addr(ss));
1268
1269 return ret;
1270}
1271#else
1272static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1273 struct sockaddr_storage *ss, char delim, const char **ipend)
1274{
1275 return -EINVAL;
1276}
1277#endif
1278
1279/*
1280 * Parse a server name (IP or hostname). If a valid IP address is not found
1281 * then try to extract a hostname to resolve using userspace DNS upcall.
1282 */
1283static int ceph_parse_server_name(const char *name, size_t namelen,
1284 struct sockaddr_storage *ss, char delim, const char **ipend)
1285{
1286 int ret;
1287
1288 ret = ceph_pton(name, namelen, ss, delim, ipend);
1289 if (ret)
1290 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1291
1292 return ret;
1293}
1294
1295/*
Sage Weil31b80062009-10-06 11:31:13 -07001296 * Parse an ip[:port] list into an addr array. Use the default
1297 * monitor port if a port isn't specified.
1298 */
1299int ceph_parse_ips(const char *c, const char *end,
1300 struct ceph_entity_addr *addr,
1301 int max_count, int *count)
1302{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001303 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001304 const char *p = c;
1305
1306 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1307 for (i = 0; i < max_count; i++) {
1308 const char *ipend;
1309 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001310 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001311 char delim = ',';
1312
1313 if (*p == '[') {
1314 delim = ']';
1315 p++;
1316 }
Sage Weil31b80062009-10-06 11:31:13 -07001317
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001318 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1319 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001320 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001321 ret = -EINVAL;
1322
Sage Weil31b80062009-10-06 11:31:13 -07001323 p = ipend;
1324
Sage Weil39139f62010-07-08 09:54:52 -07001325 if (delim == ']') {
1326 if (*p != ']') {
1327 dout("missing matching ']'\n");
1328 goto bad;
1329 }
1330 p++;
1331 }
1332
Sage Weil31b80062009-10-06 11:31:13 -07001333 /* port? */
1334 if (p < end && *p == ':') {
1335 port = 0;
1336 p++;
1337 while (p < end && *p >= '0' && *p <= '9') {
1338 port = (port * 10) + (*p - '0');
1339 p++;
1340 }
1341 if (port > 65535 || port == 0)
1342 goto bad;
1343 } else {
1344 port = CEPH_MON_PORT;
1345 }
1346
1347 addr_set_port(ss, port);
1348
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001349 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001350
1351 if (p == end)
1352 break;
1353 if (*p != ',')
1354 goto bad;
1355 p++;
1356 }
1357
1358 if (p != end)
1359 goto bad;
1360
1361 if (count)
1362 *count = i + 1;
1363 return 0;
1364
1365bad:
Sage Weil39139f62010-07-08 09:54:52 -07001366 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001367 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001368}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001369EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001370
Sage Weileed0ef22009-11-10 14:34:36 -08001371static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001372{
Sage Weileed0ef22009-11-10 14:34:36 -08001373 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001374
1375 if (verify_hello(con) < 0)
1376 return -1;
1377
Sage Weil63f2d212009-11-03 15:17:56 -08001378 ceph_decode_addr(&con->actual_peer_addr);
1379 ceph_decode_addr(&con->peer_addr_for_me);
1380
Sage Weil31b80062009-10-06 11:31:13 -07001381 /*
1382 * Make sure the other end is who we wanted. note that the other
1383 * end may not yet know their ip address, so if it's 0.0.0.0, give
1384 * them the benefit of the doubt.
1385 */
Sage Weil103e2d32010-01-07 16:12:36 -08001386 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1387 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001388 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1389 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001390 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001391 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001392 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001393 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001394 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001395 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001396 return -1;
1397 }
1398
1399 /*
1400 * did we learn our address?
1401 */
1402 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1403 int port = addr_port(&con->msgr->inst.addr.in_addr);
1404
1405 memcpy(&con->msgr->inst.addr.in_addr,
1406 &con->peer_addr_for_me.in_addr,
1407 sizeof(con->peer_addr_for_me.in_addr));
1408 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001409 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001410 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001411 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001412 }
1413
Sage Weileed0ef22009-11-10 14:34:36 -08001414 set_bit(NEGOTIATING, &con->state);
1415 prepare_read_connect(con);
1416 return 0;
1417}
1418
Sage Weil04a419f2009-12-23 09:30:21 -08001419static void fail_protocol(struct ceph_connection *con)
1420{
1421 reset_connection(con);
1422 set_bit(CLOSED, &con->state); /* in case there's queued work */
Sage Weil04a419f2009-12-23 09:30:21 -08001423}
1424
Sage Weileed0ef22009-11-10 14:34:36 -08001425static int process_connect(struct ceph_connection *con)
1426{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001427 u64 sup_feat = con->msgr->supported_features;
1428 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001429 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001430 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001431
Sage Weileed0ef22009-11-10 14:34:36 -08001432 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1433
Sage Weil31b80062009-10-06 11:31:13 -07001434 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001435 case CEPH_MSGR_TAG_FEATURES:
1436 pr_err("%s%lld %s feature set mismatch,"
1437 " my %llx < server's %llx, missing %llx\n",
1438 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001439 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001440 sup_feat, server_feat, server_feat & ~sup_feat);
1441 con->error_msg = "missing required protocol features";
1442 fail_protocol(con);
1443 return -1;
1444
Sage Weil31b80062009-10-06 11:31:13 -07001445 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001446 pr_err("%s%lld %s protocol version mismatch,"
1447 " my %d != server's %d\n",
1448 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001449 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001450 le32_to_cpu(con->out_connect.protocol_version),
1451 le32_to_cpu(con->in_reply.protocol_version));
1452 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001453 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001454 return -1;
1455
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001456 case CEPH_MSGR_TAG_BADAUTHORIZER:
1457 con->auth_retry++;
1458 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1459 con->auth_retry);
1460 if (con->auth_retry == 2) {
1461 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001462 return -1;
1463 }
1464 con->auth_retry = 1;
Alex Eldere2200422012-05-23 14:35:23 -05001465 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001466 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001467 if (ret < 0)
1468 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001469 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001470 break;
Sage Weil31b80062009-10-06 11:31:13 -07001471
1472 case CEPH_MSGR_TAG_RESETSESSION:
1473 /*
1474 * If we connected with a large connect_seq but the peer
1475 * has no record of a session with us (no connection, or
1476 * connect_seq == 0), they will send RESETSESION to indicate
1477 * that they must have reset their session, and may have
1478 * dropped messages.
1479 */
1480 dout("process_connect got RESET peer seq %u\n",
1481 le32_to_cpu(con->in_connect.connect_seq));
1482 pr_err("%s%lld %s connection reset\n",
1483 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001484 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001485 reset_connection(con);
Alex Eldere2200422012-05-23 14:35:23 -05001486 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001487 ret = prepare_write_connect(con);
1488 if (ret < 0)
1489 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001490 prepare_read_connect(con);
1491
1492 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001493 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001494 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1495 if (con->ops->peer_reset)
1496 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001497 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07001498 if (test_bit(CLOSED, &con->state) ||
1499 test_bit(OPENING, &con->state))
1500 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001501 break;
1502
1503 case CEPH_MSGR_TAG_RETRY_SESSION:
1504 /*
1505 * If we sent a smaller connect_seq than the peer has, try
1506 * again with a larger value.
1507 */
1508 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1509 le32_to_cpu(con->out_connect.connect_seq),
1510 le32_to_cpu(con->in_connect.connect_seq));
1511 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Alex Eldere2200422012-05-23 14:35:23 -05001512 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001513 ret = prepare_write_connect(con);
1514 if (ret < 0)
1515 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001516 prepare_read_connect(con);
1517 break;
1518
1519 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1520 /*
1521 * If we sent a smaller global_seq than the peer has, try
1522 * again with a larger value.
1523 */
Sage Weileed0ef22009-11-10 14:34:36 -08001524 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001525 con->peer_global_seq,
1526 le32_to_cpu(con->in_connect.global_seq));
1527 get_global_seq(con->msgr,
1528 le32_to_cpu(con->in_connect.global_seq));
Alex Eldere2200422012-05-23 14:35:23 -05001529 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001530 ret = prepare_write_connect(con);
1531 if (ret < 0)
1532 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001533 prepare_read_connect(con);
1534 break;
1535
1536 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001537 if (req_feat & ~server_feat) {
1538 pr_err("%s%lld %s protocol feature mismatch,"
1539 " my required %llx > server's %llx, need %llx\n",
1540 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001541 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001542 req_feat, server_feat, req_feat & ~server_feat);
1543 con->error_msg = "missing required protocol features";
1544 fail_protocol(con);
1545 return -1;
1546 }
Sage Weil31b80062009-10-06 11:31:13 -07001547 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001548 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1549 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001550 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001551 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1552 con->peer_global_seq,
1553 le32_to_cpu(con->in_reply.connect_seq),
1554 con->connect_seq);
1555 WARN_ON(con->connect_seq !=
1556 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001557
1558 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elder928443c2012-05-22 11:41:43 -05001559 set_bit(LOSSYTX, &con->flags);
Sage Weil92ac41d2009-12-14 14:56:56 -08001560
Sage Weil31b80062009-10-06 11:31:13 -07001561 prepare_read_tag(con);
1562 break;
1563
1564 case CEPH_MSGR_TAG_WAIT:
1565 /*
1566 * If there is a connection race (we are opening
1567 * connections to each other), one of us may just have
1568 * to WAIT. This shouldn't happen if we are the
1569 * client.
1570 */
Sage Weil04177882011-05-12 15:33:17 -07001571 pr_err("process_connect got WAIT as client\n");
1572 con->error_msg = "protocol error, got WAIT as client";
1573 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001574
1575 default:
1576 pr_err("connect protocol error, will retry\n");
1577 con->error_msg = "protocol error, garbage tag during connect";
1578 return -1;
1579 }
1580 return 0;
1581}
1582
1583
1584/*
1585 * read (part of) an ack
1586 */
1587static int read_partial_ack(struct ceph_connection *con)
1588{
Alex Elderfd516532012-05-10 10:29:50 -05001589 int size = sizeof (con->in_temp_ack);
1590 int end = size;
1591
1592 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001593}
1594
1595
1596/*
1597 * We can finally discard anything that's been acked.
1598 */
1599static void process_ack(struct ceph_connection *con)
1600{
1601 struct ceph_msg *m;
1602 u64 ack = le64_to_cpu(con->in_temp_ack);
1603 u64 seq;
1604
Sage Weil31b80062009-10-06 11:31:13 -07001605 while (!list_empty(&con->out_sent)) {
1606 m = list_first_entry(&con->out_sent, struct ceph_msg,
1607 list_head);
1608 seq = le64_to_cpu(m->hdr.seq);
1609 if (seq > ack)
1610 break;
1611 dout("got ack for seq %llu type %d at %p\n", seq,
1612 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001613 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001614 ceph_msg_remove(m);
1615 }
Sage Weil31b80062009-10-06 11:31:13 -07001616 prepare_read_tag(con);
1617}
1618
1619
1620
1621
Yehuda Sadeh24504182010-01-08 13:58:34 -08001622static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001623 struct kvec *section,
1624 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001625{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001626 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001627
Yehuda Sadeh24504182010-01-08 13:58:34 -08001628 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001629
Yehuda Sadeh24504182010-01-08 13:58:34 -08001630 while (section->iov_len < sec_len) {
1631 BUG_ON(section->iov_base == NULL);
1632 left = sec_len - section->iov_len;
1633 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1634 section->iov_len, left);
1635 if (ret <= 0)
1636 return ret;
1637 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001638 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001639 if (section->iov_len == sec_len)
1640 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001641
1642 return 1;
1643}
1644
Alex Elder1c20f2d2012-06-04 14:43:32 -05001645static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
1646 struct ceph_msg_header *hdr);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001647
1648
1649static int read_partial_message_pages(struct ceph_connection *con,
1650 struct page **pages,
Eric Dumazet95c96172012-04-15 05:58:06 +00001651 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001652{
1653 void *p;
1654 int ret;
1655 int left;
1656
1657 left = min((int)(data_len - con->in_msg_pos.data_pos),
1658 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1659 /* (page) data */
1660 BUG_ON(pages == NULL);
1661 p = kmap(pages[con->in_msg_pos.page]);
1662 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1663 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001664 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001665 con->in_data_crc =
1666 crc32c(con->in_data_crc,
1667 p + con->in_msg_pos.page_pos, ret);
1668 kunmap(pages[con->in_msg_pos.page]);
1669 if (ret <= 0)
1670 return ret;
1671 con->in_msg_pos.data_pos += ret;
1672 con->in_msg_pos.page_pos += ret;
1673 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1674 con->in_msg_pos.page_pos = 0;
1675 con->in_msg_pos.page++;
1676 }
1677
1678 return ret;
1679}
1680
1681#ifdef CONFIG_BLOCK
1682static int read_partial_message_bio(struct ceph_connection *con,
1683 struct bio **bio_iter, int *bio_seg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001684 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001685{
1686 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1687 void *p;
1688 int ret, left;
1689
1690 if (IS_ERR(bv))
1691 return PTR_ERR(bv);
1692
1693 left = min((int)(data_len - con->in_msg_pos.data_pos),
1694 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1695
1696 p = kmap(bv->bv_page) + bv->bv_offset;
1697
1698 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1699 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001700 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001701 con->in_data_crc =
1702 crc32c(con->in_data_crc,
1703 p + con->in_msg_pos.page_pos, ret);
1704 kunmap(bv->bv_page);
1705 if (ret <= 0)
1706 return ret;
1707 con->in_msg_pos.data_pos += ret;
1708 con->in_msg_pos.page_pos += ret;
1709 if (con->in_msg_pos.page_pos == bv->bv_len) {
1710 con->in_msg_pos.page_pos = 0;
1711 iter_bio_next(bio_iter, bio_seg);
1712 }
1713
1714 return ret;
1715}
1716#endif
1717
Sage Weil31b80062009-10-06 11:31:13 -07001718/*
1719 * read (part of) a message.
1720 */
1721static int read_partial_message(struct ceph_connection *con)
1722{
1723 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05001724 int size;
1725 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001726 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00001727 unsigned int front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001728 bool do_datacrc = !con->msgr->nocrc;
Sage Weilae187562010-04-22 07:47:01 -07001729 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001730 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001731
1732 dout("read_partial_message con %p msg %p\n", con, m);
1733
1734 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05001735 size = sizeof (con->in_hdr);
1736 end = size;
1737 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05001738 if (ret <= 0)
1739 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001740
1741 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1742 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1743 pr_err("read_partial_message bad hdr "
1744 " crc %u != expected %u\n",
1745 crc, con->in_hdr.crc);
1746 return -EBADMSG;
1747 }
1748
Sage Weil31b80062009-10-06 11:31:13 -07001749 front_len = le32_to_cpu(con->in_hdr.front_len);
1750 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1751 return -EIO;
1752 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1753 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1754 return -EIO;
1755 data_len = le32_to_cpu(con->in_hdr.data_len);
1756 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1757 return -EIO;
1758
Sage Weilae187562010-04-22 07:47:01 -07001759 /* verify seq# */
1760 seq = le64_to_cpu(con->in_hdr.seq);
1761 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001762 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001763 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001764 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001765 seq, con->in_seq + 1);
1766 con->in_base_pos = -front_len - middle_len - data_len -
1767 sizeof(m->footer);
1768 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001769 return 0;
1770 } else if ((s64)seq - (s64)con->in_seq > 1) {
1771 pr_err("read_partial_message bad seq %lld expected %lld\n",
1772 seq, con->in_seq + 1);
1773 con->error_msg = "bad message sequence # for incoming message";
1774 return -EBADMSG;
1775 }
1776
Sage Weil31b80062009-10-06 11:31:13 -07001777 /* allocate message? */
1778 if (!con->in_msg) {
1779 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1780 con->in_hdr.front_len, con->in_hdr.data_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05001781 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
Sage Weil31b80062009-10-06 11:31:13 -07001782 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001783 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001784 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001785 con->in_base_pos = -front_len - middle_len - data_len -
1786 sizeof(m->footer);
1787 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001788 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001789 return 0;
1790 }
Sage Weila79832f2010-04-01 16:06:19 -07001791 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001792 con->error_msg =
1793 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001794 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001795 }
Alex Elder38941f82012-06-01 14:56:43 -05001796
1797 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07001798 m = con->in_msg;
1799 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001800 if (m->middle)
1801 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001802
1803 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001804 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001805 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001806 else
1807 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001808 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001809 }
1810
1811 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001812 ret = read_partial_message_section(con, &m->front, front_len,
1813 &con->in_front_crc);
1814 if (ret <= 0)
1815 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001816
1817 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001818 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001819 ret = read_partial_message_section(con, &m->middle->vec,
1820 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001821 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001822 if (ret <= 0)
1823 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001824 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001825#ifdef CONFIG_BLOCK
1826 if (m->bio && !m->bio_iter)
1827 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1828#endif
Sage Weil31b80062009-10-06 11:31:13 -07001829
1830 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001831 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001832 if (m->pages) {
1833 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001834 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001835 if (ret <= 0)
1836 return ret;
1837#ifdef CONFIG_BLOCK
1838 } else if (m->bio) {
1839
1840 ret = read_partial_message_bio(con,
1841 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001842 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001843 if (ret <= 0)
1844 return ret;
1845#endif
1846 } else {
1847 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001848 }
1849 }
1850
Sage Weil31b80062009-10-06 11:31:13 -07001851 /* footer */
Alex Elderfd516532012-05-10 10:29:50 -05001852 size = sizeof (m->footer);
1853 end += size;
1854 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05001855 if (ret <= 0)
1856 return ret;
1857
Sage Weil31b80062009-10-06 11:31:13 -07001858 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1859 m, front_len, m->footer.front_crc, middle_len,
1860 m->footer.middle_crc, data_len, m->footer.data_crc);
1861
1862 /* crc ok? */
1863 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1864 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1865 m, con->in_front_crc, m->footer.front_crc);
1866 return -EBADMSG;
1867 }
1868 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1869 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1870 m, con->in_middle_crc, m->footer.middle_crc);
1871 return -EBADMSG;
1872 }
Alex Elderbca064d2012-02-15 07:43:54 -06001873 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001874 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1875 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1876 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1877 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1878 return -EBADMSG;
1879 }
1880
1881 return 1; /* done! */
1882}
1883
1884/*
1885 * Process message. This happens in the worker thread. The callback should
1886 * be careful not to do anything that waits on other incoming messages or it
1887 * may deadlock.
1888 */
1889static void process_message(struct ceph_connection *con)
1890{
Sage Weil5e095e82009-12-14 14:30:34 -08001891 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001892
Alex Elder38941f82012-06-01 14:56:43 -05001893 BUG_ON(con->in_msg->con != con);
1894 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08001895 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001896 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07001897 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07001898
1899 /* if first message, set peer_name */
1900 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001901 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001902
Sage Weil31b80062009-10-06 11:31:13 -07001903 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001904 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001905
1906 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1907 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001908 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001909 le16_to_cpu(msg->hdr.type),
1910 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1911 le32_to_cpu(msg->hdr.front_len),
1912 le32_to_cpu(msg->hdr.data_len),
1913 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1914 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001915
1916 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001917 prepare_read_tag(con);
1918}
1919
1920
1921/*
1922 * Write something to the socket. Called in a worker thread when the
1923 * socket appears to be writeable and we have something ready to send.
1924 */
1925static int try_write(struct ceph_connection *con)
1926{
Sage Weil31b80062009-10-06 11:31:13 -07001927 int ret = 1;
1928
Sage Weild59315c2012-06-21 12:49:23 -07001929 dout("try_write start %p state %lu\n", con, con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001930
Sage Weil31b80062009-10-06 11:31:13 -07001931more:
1932 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1933
1934 /* open the socket first? */
1935 if (con->sock == NULL) {
Alex Eldera5988c42012-05-29 11:04:58 -05001936 clear_bit(NEGOTIATING, &con->state);
1937 set_bit(CONNECTING, &con->state);
1938
Alex Eldere2200422012-05-23 14:35:23 -05001939 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001940 prepare_write_banner(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001941 ret = prepare_write_connect(con);
1942 if (ret < 0)
1943 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08001944 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001945
Sage Weilcf3e5c42009-12-11 09:48:05 -08001946 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001947 con->in_tag = CEPH_MSGR_TAG_READY;
1948 dout("try_write initiating connect on %p new state %lu\n",
1949 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06001950 ret = ceph_tcp_connect(con);
1951 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07001952 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07001953 goto out;
1954 }
1955 }
1956
1957more_kvec:
1958 /* kvec data queued? */
1959 if (con->out_skip) {
1960 ret = write_partial_skip(con);
1961 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001962 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001963 }
1964 if (con->out_kvec_left) {
1965 ret = write_partial_kvec(con);
1966 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001967 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001968 }
1969
1970 /* msg pages? */
1971 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001972 if (con->out_msg_done) {
1973 ceph_msg_put(con->out_msg);
1974 con->out_msg = NULL; /* we're done with this one */
1975 goto do_next;
1976 }
1977
Sage Weil31b80062009-10-06 11:31:13 -07001978 ret = write_partial_msg_pages(con);
1979 if (ret == 1)
1980 goto more_kvec; /* we need to send the footer, too! */
1981 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08001982 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001983 if (ret < 0) {
1984 dout("try_write write_partial_msg_pages err %d\n",
1985 ret);
Sage Weil42961d22011-01-25 08:19:34 -08001986 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001987 }
1988 }
1989
Sage Weilc86a2932009-12-14 14:04:30 -08001990do_next:
Sage Weil31b80062009-10-06 11:31:13 -07001991 if (!test_bit(CONNECTING, &con->state)) {
1992 /* is anything else pending? */
1993 if (!list_empty(&con->out_queue)) {
1994 prepare_write_message(con);
1995 goto more;
1996 }
1997 if (con->in_seq > con->in_seq_acked) {
1998 prepare_write_ack(con);
1999 goto more;
2000 }
Alex Elder928443c2012-05-22 11:41:43 -05002001 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002002 prepare_write_keepalive(con);
2003 goto more;
2004 }
2005 }
2006
2007 /* Nothing to do! */
Alex Elder928443c2012-05-22 11:41:43 -05002008 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002009 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002010 ret = 0;
2011out:
Sage Weil42961d22011-01-25 08:19:34 -08002012 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002013 return ret;
2014}
2015
2016
2017
2018/*
2019 * Read what we can from the socket.
2020 */
2021static int try_read(struct ceph_connection *con)
2022{
Sage Weil31b80062009-10-06 11:31:13 -07002023 int ret = -1;
2024
2025 if (!con->sock)
2026 return 0;
2027
2028 if (test_bit(STANDBY, &con->state))
2029 return 0;
2030
2031 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08002032
Sage Weil31b80062009-10-06 11:31:13 -07002033more:
2034 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2035 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002036
2037 /*
2038 * process_connect and process_message drop and re-take
2039 * con->mutex. make sure we handle a racing close or reopen.
2040 */
2041 if (test_bit(CLOSED, &con->state) ||
2042 test_bit(OPENING, &con->state)) {
2043 ret = -EAGAIN;
2044 goto out;
2045 }
2046
Sage Weil31b80062009-10-06 11:31:13 -07002047 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08002048 if (!test_bit(NEGOTIATING, &con->state)) {
2049 dout("try_read connecting\n");
2050 ret = read_partial_banner(con);
2051 if (ret <= 0)
Sage Weileed0ef22009-11-10 14:34:36 -08002052 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002053 ret = process_banner(con);
2054 if (ret < 0)
2055 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08002056 }
Sage Weil31b80062009-10-06 11:31:13 -07002057 ret = read_partial_connect(con);
2058 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002059 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002060 ret = process_connect(con);
2061 if (ret < 0)
2062 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002063 goto more;
2064 }
2065
2066 if (con->in_base_pos < 0) {
2067 /*
2068 * skipping + discarding content.
2069 *
2070 * FIXME: there must be a better way to do this!
2071 */
Alex Elder84495f42012-02-15 07:43:55 -06002072 static char buf[SKIP_BUF_SIZE];
2073 int skip = min((int) sizeof (buf), -con->in_base_pos);
2074
Sage Weil31b80062009-10-06 11:31:13 -07002075 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2076 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2077 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002078 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002079 con->in_base_pos += ret;
2080 if (con->in_base_pos)
2081 goto more;
2082 }
2083 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2084 /*
2085 * what's next?
2086 */
2087 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2088 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002089 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002090 dout("try_read got tag %d\n", (int)con->in_tag);
2091 switch (con->in_tag) {
2092 case CEPH_MSGR_TAG_MSG:
2093 prepare_read_message(con);
2094 break;
2095 case CEPH_MSGR_TAG_ACK:
2096 prepare_read_ack(con);
2097 break;
2098 case CEPH_MSGR_TAG_CLOSE:
2099 set_bit(CLOSED, &con->state); /* fixme */
Sage Weil98bdb0a2011-01-25 08:17:48 -08002100 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002101 default:
2102 goto bad_tag;
2103 }
2104 }
2105 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2106 ret = read_partial_message(con);
2107 if (ret <= 0) {
2108 switch (ret) {
2109 case -EBADMSG:
2110 con->error_msg = "bad crc";
2111 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002112 break;
Sage Weil31b80062009-10-06 11:31:13 -07002113 case -EIO:
2114 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002115 break;
Sage Weil31b80062009-10-06 11:31:13 -07002116 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002117 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002118 }
2119 if (con->in_tag == CEPH_MSGR_TAG_READY)
2120 goto more;
2121 process_message(con);
2122 goto more;
2123 }
2124 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2125 ret = read_partial_ack(con);
2126 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002127 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002128 process_ack(con);
2129 goto more;
2130 }
2131
Sage Weil31b80062009-10-06 11:31:13 -07002132out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002133 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002134 return ret;
2135
2136bad_tag:
2137 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2138 con->error_msg = "protocol error, garbage tag";
2139 ret = -1;
2140 goto out;
2141}
2142
2143
2144/*
2145 * Atomically queue work on a connection. Bump @con reference to
2146 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002147 */
2148static void queue_con(struct ceph_connection *con)
2149{
Sage Weil31b80062009-10-06 11:31:13 -07002150 if (!con->ops->get(con)) {
2151 dout("queue_con %p ref count 0\n", con);
2152 return;
2153 }
2154
Tejun Heof363e452011-01-03 14:49:46 +01002155 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002156 dout("queue_con %p - already queued\n", con);
2157 con->ops->put(con);
2158 } else {
2159 dout("queue_con %p\n", con);
2160 }
2161}
2162
2163/*
2164 * Do some work on a connection. Drop a connection ref when we're done.
2165 */
2166static void con_work(struct work_struct *work)
2167{
2168 struct ceph_connection *con = container_of(work, struct ceph_connection,
2169 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002170 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002171
Sage Weil9dd46582010-04-28 13:51:50 -07002172 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002173restart:
Alex Elder928443c2012-05-22 11:41:43 -05002174 if (test_and_clear_bit(BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002175 dout("con_work %p backing off\n", con);
2176 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2177 round_jiffies_relative(con->delay))) {
2178 dout("con_work %p backoff %lu\n", con, con->delay);
2179 mutex_unlock(&con->mutex);
2180 return;
2181 } else {
2182 con->ops->put(con);
2183 dout("con_work %p FAILED to back off %lu\n", con,
2184 con->delay);
2185 }
2186 }
Sage Weil9dd46582010-04-28 13:51:50 -07002187
Sage Weile00de342011-03-04 12:25:05 -08002188 if (test_bit(STANDBY, &con->state)) {
2189 dout("con_work %p STANDBY\n", con);
2190 goto done;
2191 }
Sage Weil31b80062009-10-06 11:31:13 -07002192 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2193 dout("con_work CLOSED\n");
2194 con_close_socket(con);
2195 goto done;
2196 }
2197 if (test_and_clear_bit(OPENING, &con->state)) {
2198 /* reopen w/ new peer */
2199 dout("con_work OPENING\n");
2200 con_close_socket(con);
2201 }
2202
Alex Elder928443c2012-05-22 11:41:43 -05002203 if (test_and_clear_bit(SOCK_CLOSED, &con->flags))
Sage Weil0da5d702011-05-19 11:21:05 -07002204 goto fault;
2205
2206 ret = try_read(con);
2207 if (ret == -EAGAIN)
2208 goto restart;
2209 if (ret < 0)
2210 goto fault;
2211
2212 ret = try_write(con);
2213 if (ret == -EAGAIN)
2214 goto restart;
2215 if (ret < 0)
2216 goto fault;
Sage Weil31b80062009-10-06 11:31:13 -07002217
2218done:
Sage Weil9dd46582010-04-28 13:51:50 -07002219 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002220done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002221 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002222 return;
2223
2224fault:
2225 mutex_unlock(&con->mutex);
2226 ceph_fault(con); /* error/fault path */
2227 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002228}
2229
2230
2231/*
2232 * Generic error/fault handler. A retry mechanism is used with
2233 * exponential backoff
2234 */
2235static void ceph_fault(struct ceph_connection *con)
2236{
2237 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002238 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002239 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002240 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002241
Alex Elder928443c2012-05-22 11:41:43 -05002242 if (test_bit(LOSSYTX, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002243 dout("fault on LOSSYTX channel\n");
2244 goto out;
2245 }
2246
Sage Weilec302642009-12-22 10:43:42 -08002247 mutex_lock(&con->mutex);
Sage Weil91e45ce32010-02-15 12:05:09 -08002248 if (test_bit(CLOSED, &con->state))
2249 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002250
Sage Weil31b80062009-10-06 11:31:13 -07002251 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002252
2253 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002254 BUG_ON(con->in_msg->con != con);
2255 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08002256 ceph_msg_put(con->in_msg);
2257 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07002258 con->ops->put(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002259 }
Sage Weil31b80062009-10-06 11:31:13 -07002260
Sage Weile80a52d2010-02-25 12:40:45 -08002261 /* Requeue anything that hasn't been acked */
2262 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002263
Sage Weile76661d2011-03-03 10:10:15 -08002264 /* If there are no messages queued or keepalive pending, place
2265 * the connection in a STANDBY state */
2266 if (list_empty(&con->out_queue) &&
Alex Elder928443c2012-05-22 11:41:43 -05002267 !test_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weile00de342011-03-04 12:25:05 -08002268 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elder928443c2012-05-22 11:41:43 -05002269 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002270 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08002271 } else {
2272 /* retry after a delay. */
2273 if (con->delay == 0)
2274 con->delay = BASE_DELAY_INTERVAL;
2275 else if (con->delay < MAX_DELAY_INTERVAL)
2276 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002277 con->ops->get(con);
2278 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002279 round_jiffies_relative(con->delay))) {
2280 dout("fault queued %p delay %lu\n", con, con->delay);
2281 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002282 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002283 dout("fault failed to queue %p delay %lu, backoff\n",
2284 con, con->delay);
2285 /*
2286 * In many cases we see a socket state change
2287 * while con_work is running and end up
2288 * queuing (non-delayed) work, such that we
2289 * can't backoff with a delay. Set a flag so
2290 * that when con_work restarts we schedule the
2291 * delay then.
2292 */
Alex Elder928443c2012-05-22 11:41:43 -05002293 set_bit(BACKOFF, &con->flags);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002294 }
Sage Weil31b80062009-10-06 11:31:13 -07002295 }
2296
Sage Weil91e45ce32010-02-15 12:05:09 -08002297out_unlock:
2298 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002299out:
Sage Weil161fd652010-02-25 12:38:57 -08002300 /*
2301 * in case we faulted due to authentication, invalidate our
2302 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002303 */
Sage Weil161fd652010-02-25 12:38:57 -08002304 if (con->auth_retry && con->ops->invalidate_authorizer) {
2305 dout("calling invalidate_authorizer()\n");
2306 con->ops->invalidate_authorizer(con);
2307 }
2308
Sage Weil31b80062009-10-06 11:31:13 -07002309 if (con->ops->fault)
2310 con->ops->fault(con);
2311}
2312
2313
2314
2315/*
Alex Elder15d98822012-05-26 23:26:43 -05002316 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002317 */
Alex Elder15d98822012-05-26 23:26:43 -05002318void ceph_messenger_init(struct ceph_messenger *msgr,
2319 struct ceph_entity_addr *myaddr,
2320 u32 supported_features,
2321 u32 required_features,
2322 bool nocrc)
Sage Weil31b80062009-10-06 11:31:13 -07002323{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002324 msgr->supported_features = supported_features;
2325 msgr->required_features = required_features;
2326
Sage Weil31b80062009-10-06 11:31:13 -07002327 spin_lock_init(&msgr->global_seq_lock);
2328
Sage Weil31b80062009-10-06 11:31:13 -07002329 if (myaddr)
2330 msgr->inst.addr = *myaddr;
2331
2332 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002333 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002334 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002335 encode_my_addr(msgr);
Alex Elder15d98822012-05-26 23:26:43 -05002336 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002337
Alex Elder15d98822012-05-26 23:26:43 -05002338 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002339}
Alex Elder15d98822012-05-26 23:26:43 -05002340EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002341
Sage Weile00de342011-03-04 12:25:05 -08002342static void clear_standby(struct ceph_connection *con)
2343{
2344 /* come back from STANDBY? */
2345 if (test_and_clear_bit(STANDBY, &con->state)) {
2346 mutex_lock(&con->mutex);
2347 dout("clear_standby %p and ++connect_seq\n", con);
2348 con->connect_seq++;
Alex Elder928443c2012-05-22 11:41:43 -05002349 WARN_ON(test_bit(WRITE_PENDING, &con->flags));
2350 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags));
Sage Weile00de342011-03-04 12:25:05 -08002351 mutex_unlock(&con->mutex);
2352 }
2353}
2354
Sage Weil31b80062009-10-06 11:31:13 -07002355/*
2356 * Queue up an outgoing message on the given connection.
2357 */
2358void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2359{
2360 if (test_bit(CLOSED, &con->state)) {
2361 dout("con_send %p closed, dropping %p\n", con, msg);
2362 ceph_msg_put(msg);
2363 return;
2364 }
2365
2366 /* set src+dst */
Sage Weildbad1852010-03-25 15:45:38 -07002367 msg->hdr.src = con->msgr->inst.name;
Sage Weil31b80062009-10-06 11:31:13 -07002368
Sage Weil3ca02ef2010-03-01 15:25:00 -08002369 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2370
Sage Weile84346b2010-05-11 21:20:38 -07002371 msg->needs_out_seq = true;
2372
Sage Weil31b80062009-10-06 11:31:13 -07002373 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08002374 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002375
Alex Elder38941f82012-06-01 14:56:43 -05002376 BUG_ON(msg->con != NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002377 msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002378 BUG_ON(msg->con == NULL);
2379
Sage Weil31b80062009-10-06 11:31:13 -07002380 BUG_ON(!list_empty(&msg->list_head));
2381 list_add_tail(&msg->list_head, &con->out_queue);
2382 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2383 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2384 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2385 le32_to_cpu(msg->hdr.front_len),
2386 le32_to_cpu(msg->hdr.middle_len),
2387 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002388 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002389
2390 /* if there wasn't anything waiting to send before, queue
2391 * new work */
Sage Weile00de342011-03-04 12:25:05 -08002392 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002393 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002394 queue_con(con);
2395}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002396EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002397
2398/*
2399 * Revoke a message that was previously queued for send
2400 */
Alex Elder6740a842012-06-01 14:56:43 -05002401void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002402{
Alex Elder6740a842012-06-01 14:56:43 -05002403 struct ceph_connection *con = msg->con;
2404
2405 if (!con)
2406 return; /* Message not in our possession */
2407
Sage Weilec302642009-12-22 10:43:42 -08002408 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002409 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05002410 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002411 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -05002412 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002413 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -05002414 msg->con = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05002415 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05002416
Sage Weil31b80062009-10-06 11:31:13 -07002417 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002418 }
2419 if (con->out_msg == msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002420 dout("%s %p msg %p - was sending\n", __func__, con, msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002421 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002422 if (con->out_kvec_is_msg) {
2423 con->out_skip = con->out_kvec_bytes;
2424 con->out_kvec_is_msg = false;
2425 }
Sage Weiled98ada2010-07-05 12:15:14 -07002426 msg->hdr.seq = 0;
Alex Elder92ce0342012-06-04 14:43:33 -05002427
2428 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07002429 }
Sage Weilec302642009-12-22 10:43:42 -08002430 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002431}
2432
2433/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002434 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002435 */
Alex Elder8921d112012-06-01 14:56:43 -05002436void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002437{
Alex Elder8921d112012-06-01 14:56:43 -05002438 struct ceph_connection *con;
2439
2440 BUG_ON(msg == NULL);
2441 if (!msg->con) {
2442 dout("%s msg %p null con\n", __func__, msg);
2443
2444 return; /* Message not in our possession */
2445 }
2446
2447 con = msg->con;
Sage Weil350b1c32009-12-22 10:45:45 -08002448 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05002449 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002450 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
2451 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
2452 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002453
2454 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05002455 dout("%s %p msg %p revoked\n", __func__, con, msg);
2456 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08002457 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002458 front_len -
2459 middle_len -
2460 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002461 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002462 ceph_msg_put(con->in_msg);
2463 con->in_msg = NULL;
2464 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002465 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002466 } else {
Alex Elder8921d112012-06-01 14:56:43 -05002467 dout("%s %p in_msg %p msg %p no-op\n",
2468 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002469 }
2470 mutex_unlock(&con->mutex);
2471}
2472
2473/*
Sage Weil31b80062009-10-06 11:31:13 -07002474 * Queue a keepalive byte to ensure the tcp connection is alive.
2475 */
2476void ceph_con_keepalive(struct ceph_connection *con)
2477{
Sage Weile00de342011-03-04 12:25:05 -08002478 dout("con_keepalive %p\n", con);
2479 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002480 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2481 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002482 queue_con(con);
2483}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002484EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002485
2486
2487/*
2488 * construct a new message with given type, size
2489 * the new msg has a ref count of 1.
2490 */
Sage Weilb61c2762011-08-09 15:03:46 -07002491struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2492 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002493{
2494 struct ceph_msg *m;
2495
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002496 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002497 if (m == NULL)
2498 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002499 kref_init(&m->kref);
Alex Elder38941f82012-06-01 14:56:43 -05002500
2501 m->con = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002502 INIT_LIST_HEAD(&m->list_head);
2503
Sage Weil45c6ceb2010-05-11 15:01:51 -07002504 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002505 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002506 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2507 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002508 m->hdr.front_len = cpu_to_le32(front_len);
2509 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002510 m->hdr.data_len = 0;
2511 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002512 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002513 m->footer.front_crc = 0;
2514 m->footer.middle_crc = 0;
2515 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002516 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002517 m->front_max = front_len;
2518 m->front_is_vmalloc = false;
2519 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002520 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002521 m->pool = NULL;
2522
Henry C Changca208922011-05-03 02:29:56 +00002523 /* middle */
2524 m->middle = NULL;
2525
2526 /* data */
2527 m->nr_pages = 0;
2528 m->page_alignment = 0;
2529 m->pages = NULL;
2530 m->pagelist = NULL;
2531 m->bio = NULL;
2532 m->bio_iter = NULL;
2533 m->bio_seg = 0;
2534 m->trail = NULL;
2535
Sage Weil31b80062009-10-06 11:31:13 -07002536 /* front */
2537 if (front_len) {
2538 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002539 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002540 PAGE_KERNEL);
2541 m->front_is_vmalloc = true;
2542 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002543 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002544 }
2545 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002546 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002547 front_len);
2548 goto out2;
2549 }
2550 } else {
2551 m->front.iov_base = NULL;
2552 }
2553 m->front.iov_len = front_len;
2554
Sage Weilbb257662010-04-01 16:07:23 -07002555 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002556 return m;
2557
2558out2:
2559 ceph_msg_put(m);
2560out:
Sage Weilb61c2762011-08-09 15:03:46 -07002561 if (!can_fail) {
2562 pr_err("msg_new can't create type %d front %d\n", type,
2563 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002564 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002565 } else {
2566 dout("msg_new can't create type %d front %d\n", type,
2567 front_len);
2568 }
Sage Weila79832f2010-04-01 16:06:19 -07002569 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002570}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002571EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002572
2573/*
Sage Weil31b80062009-10-06 11:31:13 -07002574 * Allocate "middle" portion of a message, if it is needed and wasn't
2575 * allocated by alloc_msg. This allows us to read a small fixed-size
2576 * per-type header in the front and then gracefully fail (i.e.,
2577 * propagate the error to the caller based on info in the front) when
2578 * the middle is too large.
2579 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002580static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002581{
2582 int type = le16_to_cpu(msg->hdr.type);
2583 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2584
2585 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2586 ceph_msg_type_name(type), middle_len);
2587 BUG_ON(!middle_len);
2588 BUG_ON(msg->middle);
2589
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002590 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002591 if (!msg->middle)
2592 return -ENOMEM;
2593 return 0;
2594}
2595
Yehuda Sadeh24504182010-01-08 13:58:34 -08002596/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05002597 * Allocate a message for receiving an incoming message on a
2598 * connection, and save the result in con->in_msg. Uses the
2599 * connection's private alloc_msg op if available.
2600 *
2601 * Returns true if the message should be skipped, false otherwise.
2602 * If true is returned (skip message), con->in_msg will be NULL.
2603 * If false is returned, con->in_msg will contain a pointer to the
2604 * newly-allocated message, or NULL in case of memory exhaustion.
Yehuda Sadeh24504182010-01-08 13:58:34 -08002605 */
Alex Elder1c20f2d2012-06-04 14:43:32 -05002606static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2607 struct ceph_msg_header *hdr)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002608{
2609 int type = le16_to_cpu(hdr->type);
2610 int front_len = le32_to_cpu(hdr->front_len);
2611 int middle_len = le32_to_cpu(hdr->middle_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002612 int ret;
2613
Alex Elder1c20f2d2012-06-04 14:43:32 -05002614 BUG_ON(con->in_msg != NULL);
2615
Yehuda Sadeh24504182010-01-08 13:58:34 -08002616 if (con->ops->alloc_msg) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002617 int skip = 0;
2618
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002619 mutex_unlock(&con->mutex);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002620 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002621 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002622 if (con->in_msg) {
Sage Weil36eb71a2012-06-21 12:47:08 -07002623 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002624 BUG_ON(con->in_msg->con == NULL);
2625 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002626 if (skip)
2627 con->in_msg = NULL;
2628
2629 if (!con->in_msg)
2630 return skip != 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002631 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002632 if (!con->in_msg) {
2633 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2634 if (!con->in_msg) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002635 pr_err("unable to allocate msg type %d len %d\n",
2636 type, front_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002637 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002638 }
Sage Weil36eb71a2012-06-21 12:47:08 -07002639 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002640 BUG_ON(con->in_msg->con == NULL);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002641 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002642 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002643 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002644
Alex Elder1c20f2d2012-06-04 14:43:32 -05002645 if (middle_len && !con->in_msg->middle) {
2646 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002647 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002648 ceph_msg_put(con->in_msg);
2649 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002650 }
2651 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002652
Alex Elder1c20f2d2012-06-04 14:43:32 -05002653 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002654}
2655
Sage Weil31b80062009-10-06 11:31:13 -07002656
2657/*
2658 * Free a generically kmalloc'd message.
2659 */
2660void ceph_msg_kfree(struct ceph_msg *m)
2661{
2662 dout("msg_kfree %p\n", m);
2663 if (m->front_is_vmalloc)
2664 vfree(m->front.iov_base);
2665 else
2666 kfree(m->front.iov_base);
2667 kfree(m);
2668}
2669
2670/*
2671 * Drop a msg ref. Destroy as needed.
2672 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002673void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002674{
Sage Weilc2e552e2009-12-07 15:55:05 -08002675 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002676
Sage Weilc2e552e2009-12-07 15:55:05 -08002677 dout("ceph_msg_put last one on %p\n", m);
2678 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002679
Sage Weilc2e552e2009-12-07 15:55:05 -08002680 /* drop middle, data, if any */
2681 if (m->middle) {
2682 ceph_buffer_put(m->middle);
2683 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002684 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002685 m->nr_pages = 0;
2686 m->pages = NULL;
2687
Sage Weil58bb3b32009-12-23 12:12:31 -08002688 if (m->pagelist) {
2689 ceph_pagelist_release(m->pagelist);
2690 kfree(m->pagelist);
2691 m->pagelist = NULL;
2692 }
2693
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002694 m->trail = NULL;
2695
Sage Weilc2e552e2009-12-07 15:55:05 -08002696 if (m->pool)
2697 ceph_msgpool_put(m->pool, m);
2698 else
2699 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002700}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002701EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002702
2703void ceph_msg_dump(struct ceph_msg *msg)
2704{
2705 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2706 msg->front_max, msg->nr_pages);
2707 print_hex_dump(KERN_DEBUG, "header: ",
2708 DUMP_PREFIX_OFFSET, 16, 1,
2709 &msg->hdr, sizeof(msg->hdr), true);
2710 print_hex_dump(KERN_DEBUG, " front: ",
2711 DUMP_PREFIX_OFFSET, 16, 1,
2712 msg->front.iov_base, msg->front.iov_len, true);
2713 if (msg->middle)
2714 print_hex_dump(KERN_DEBUG, "middle: ",
2715 DUMP_PREFIX_OFFSET, 16, 1,
2716 msg->middle->vec.iov_base,
2717 msg->middle->vec.iov_len, true);
2718 print_hex_dump(KERN_DEBUG, "footer: ",
2719 DUMP_PREFIX_OFFSET, 16, 1,
2720 &msg->footer, sizeof(msg->footer), true);
2721}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002722EXPORT_SYMBOL(ceph_msg_dump);