blob: adca1e6537ab0d03290902406e9021336adaf57e [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
32/* static tag bytes (protocol control messages) */
33static char tag_msg = CEPH_MSGR_TAG_MSG;
34static char tag_ack = CEPH_MSGR_TAG_ACK;
35static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
36
Sage Weila6a53492010-04-13 14:07:07 -070037#ifdef CONFIG_LOCKDEP
38static struct lock_class_key socket_class;
39#endif
40
Alex Elder84495f42012-02-15 07:43:55 -060041/*
42 * When skipping (ignoring) a block of input we read it into a "skip
43 * buffer," which is this many bytes in size.
44 */
45#define SKIP_BUF_SIZE 1024
Sage Weil31b80062009-10-06 11:31:13 -070046
47static void queue_con(struct ceph_connection *con);
48static void con_work(struct work_struct *);
49static void ceph_fault(struct ceph_connection *con);
50
Sage Weil31b80062009-10-06 11:31:13 -070051/*
Alex Elderf64a9312012-01-23 15:49:27 -060052 * Nicely render a sockaddr as a string. An array of formatted
53 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -070054 */
Alex Elderf64a9312012-01-23 15:49:27 -060055#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
56#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
57#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
58#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
59
60static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
61static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -070062
Alex Elder57666512012-01-23 15:49:27 -060063static struct page *zero_page; /* used in certain error cases */
64static void *zero_page_address; /* kernel virtual addr of zero_page */
65
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070066const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -070067{
68 int i;
69 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -060070 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
71 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -070072
Alex Elderf64a9312012-01-23 15:49:27 -060073 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -070074 s = addr_str[i];
75
76 switch (ss->ss_family) {
77 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -060078 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
79 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -070080 break;
81
82 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -060083 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
84 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -070085 break;
86
87 default:
Alex Elderd3002b92012-02-14 14:05:33 -060088 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
89 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -070090 }
91
92 return s;
93}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070094EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -070095
Sage Weil63f2d212009-11-03 15:17:56 -080096static void encode_my_addr(struct ceph_messenger *msgr)
97{
98 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
99 ceph_encode_addr(&msgr->my_enc_addr);
100}
101
Sage Weil31b80062009-10-06 11:31:13 -0700102/*
103 * work queue for all reading and writing to/from the socket.
104 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600105static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700106
Alex Elder6173d1f2012-02-14 14:05:33 -0600107void _ceph_msgr_exit(void)
108{
Alex Elderd3002b92012-02-14 14:05:33 -0600109 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600110 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600111 ceph_msgr_wq = NULL;
112 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600113
114 BUG_ON(zero_page_address == NULL);
115 zero_page_address = NULL;
116
117 BUG_ON(zero_page == NULL);
118 kunmap(zero_page);
119 page_cache_release(zero_page);
120 zero_page = NULL;
121}
122
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700123int ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700124{
Alex Elder57666512012-01-23 15:49:27 -0600125 BUG_ON(zero_page != NULL);
126 zero_page = ZERO_PAGE(0);
127 page_cache_get(zero_page);
128
129 BUG_ON(zero_page_address != NULL);
130 zero_page_address = kmap(zero_page);
131
Tejun Heof363e45fd12011-01-03 14:49:46 +0100132 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600133 if (ceph_msgr_wq)
134 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600135
Alex Elder6173d1f2012-02-14 14:05:33 -0600136 pr_err("msgr_init failed to create workqueue\n");
137 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600138
Alex Elder6173d1f2012-02-14 14:05:33 -0600139 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700140}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700141EXPORT_SYMBOL(ceph_msgr_init);
Sage Weil31b80062009-10-06 11:31:13 -0700142
143void ceph_msgr_exit(void)
144{
Alex Elder57666512012-01-23 15:49:27 -0600145 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600146
Alex Elder6173d1f2012-02-14 14:05:33 -0600147 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700148}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700149EXPORT_SYMBOL(ceph_msgr_exit);
Sage Weil31b80062009-10-06 11:31:13 -0700150
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700151void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700152{
153 flush_workqueue(ceph_msgr_wq);
154}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700155EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700156
157
Sage Weil31b80062009-10-06 11:31:13 -0700158/*
159 * socket callback functions
160 */
161
162/* data available on socket, or listen socket received a connect */
163static void ceph_data_ready(struct sock *sk, int count_unused)
164{
Alex Elderbd406142012-01-23 15:49:27 -0600165 struct ceph_connection *con = sk->sk_user_data;
166
Sage Weil31b80062009-10-06 11:31:13 -0700167 if (sk->sk_state != TCP_CLOSE_WAIT) {
168 dout("ceph_data_ready on %p state = %lu, queueing work\n",
169 con, con->state);
170 queue_con(con);
171 }
172}
173
174/* socket has buffer space for writing */
175static void ceph_write_space(struct sock *sk)
176{
Alex Elderd3002b92012-02-14 14:05:33 -0600177 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700178
Jim Schutt182fac22012-02-29 08:30:58 -0700179 /* only queue to workqueue if there is data we want to write,
180 * and there is sufficient space in the socket buffer to accept
181 * more data. clear SOCK_NOSPACE so that ceph_write_space()
182 * doesn't get called again until try_write() fills the socket
183 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
184 * and net/core/stream.c:sk_stream_write_space().
185 */
Sage Weil31b80062009-10-06 11:31:13 -0700186 if (test_bit(WRITE_PENDING, &con->state)) {
Jim Schutt182fac22012-02-29 08:30:58 -0700187 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
188 dout("ceph_write_space %p queueing write work\n", con);
189 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
190 queue_con(con);
191 }
Sage Weil31b80062009-10-06 11:31:13 -0700192 } else {
193 dout("ceph_write_space %p nothing to write\n", con);
194 }
Sage Weil31b80062009-10-06 11:31:13 -0700195}
196
197/* socket's state has changed */
198static void ceph_state_change(struct sock *sk)
199{
Alex Elderbd406142012-01-23 15:49:27 -0600200 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700201
202 dout("ceph_state_change %p state = %lu sk_state = %u\n",
203 con, con->state, sk->sk_state);
204
205 if (test_bit(CLOSED, &con->state))
206 return;
207
208 switch (sk->sk_state) {
209 case TCP_CLOSE:
210 dout("ceph_state_change TCP_CLOSE\n");
211 case TCP_CLOSE_WAIT:
212 dout("ceph_state_change TCP_CLOSE_WAIT\n");
213 if (test_and_set_bit(SOCK_CLOSED, &con->state) == 0) {
214 if (test_bit(CONNECTING, &con->state))
215 con->error_msg = "connection failed";
216 else
217 con->error_msg = "socket closed";
218 queue_con(con);
219 }
220 break;
221 case TCP_ESTABLISHED:
222 dout("ceph_state_change TCP_ESTABLISHED\n");
223 queue_con(con);
224 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600225 default: /* Everything else is uninteresting */
226 break;
Sage Weil31b80062009-10-06 11:31:13 -0700227 }
228}
229
230/*
231 * set up socket callbacks
232 */
233static void set_sock_callbacks(struct socket *sock,
234 struct ceph_connection *con)
235{
236 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600237 sk->sk_user_data = con;
Sage Weil31b80062009-10-06 11:31:13 -0700238 sk->sk_data_ready = ceph_data_ready;
239 sk->sk_write_space = ceph_write_space;
240 sk->sk_state_change = ceph_state_change;
241}
242
243
244/*
245 * socket helpers
246 */
247
248/*
249 * initiate connection to a remote socket.
250 */
Alex Elder41617d02012-02-14 14:05:33 -0600251static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700252{
Sage Weilf91d3472010-07-01 15:18:31 -0700253 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700254 struct socket *sock;
255 int ret;
256
257 BUG_ON(con->sock);
Sage Weilf91d3472010-07-01 15:18:31 -0700258 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
259 IPPROTO_TCP, &sock);
Sage Weil31b80062009-10-06 11:31:13 -0700260 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600261 return ret;
Sage Weil31b80062009-10-06 11:31:13 -0700262 sock->sk->sk_allocation = GFP_NOFS;
263
Sage Weila6a53492010-04-13 14:07:07 -0700264#ifdef CONFIG_LOCKDEP
265 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
266#endif
267
Sage Weil31b80062009-10-06 11:31:13 -0700268 set_sock_callbacks(sock, con);
269
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700270 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700271
Sage Weilf91d3472010-07-01 15:18:31 -0700272 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
273 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700274 if (ret == -EINPROGRESS) {
275 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700276 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700277 sock->sk->sk_state);
Alex Eldera5bc3122012-01-23 15:49:27 -0600278 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700279 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700280 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700281 sock_release(sock);
Sage Weil31b80062009-10-06 11:31:13 -0700282 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -0700283
Alex Elder41617d02012-02-14 14:05:33 -0600284 return ret;
Alex Eldera5bc3122012-01-23 15:49:27 -0600285 }
286 con->sock = sock;
287
Alex Elder41617d02012-02-14 14:05:33 -0600288 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700289}
290
291static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
292{
293 struct kvec iov = {buf, len};
294 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800295 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700296
Sage Weil98bdb0a2011-01-25 08:17:48 -0800297 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
298 if (r == -EAGAIN)
299 r = 0;
300 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700301}
302
303/*
304 * write something. @more is true if caller will be sending more data
305 * shortly.
306 */
307static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
308 size_t kvlen, size_t len, int more)
309{
310 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800311 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700312
313 if (more)
314 msg.msg_flags |= MSG_MORE;
315 else
316 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
317
Sage Weil42961d22011-01-25 08:19:34 -0800318 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
319 if (r == -EAGAIN)
320 r = 0;
321 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700322}
323
Alex Elder31739132012-03-07 11:40:08 -0600324static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
325 int offset, size_t size, int more)
326{
327 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
328 int ret;
329
330 ret = kernel_sendpage(sock, page, offset, size, flags);
331 if (ret == -EAGAIN)
332 ret = 0;
333
334 return ret;
335}
336
Sage Weil31b80062009-10-06 11:31:13 -0700337
338/*
339 * Shutdown/close the socket for the given connection.
340 */
341static int con_close_socket(struct ceph_connection *con)
342{
343 int rc;
344
345 dout("con_close_socket on %p sock %p\n", con, con->sock);
346 if (!con->sock)
347 return 0;
348 set_bit(SOCK_CLOSED, &con->state);
349 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
350 sock_release(con->sock);
351 con->sock = NULL;
352 clear_bit(SOCK_CLOSED, &con->state);
353 return rc;
354}
355
356/*
357 * Reset a connection. Discard all incoming and outgoing messages
358 * and clear *_seq state.
359 */
360static void ceph_msg_remove(struct ceph_msg *msg)
361{
362 list_del_init(&msg->list_head);
363 ceph_msg_put(msg);
364}
365static void ceph_msg_remove_list(struct list_head *head)
366{
367 while (!list_empty(head)) {
368 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
369 list_head);
370 ceph_msg_remove(msg);
371 }
372}
373
374static void reset_connection(struct ceph_connection *con)
375{
376 /* reset connection, out_queue, msg_ and connect_seq */
377 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700378 ceph_msg_remove_list(&con->out_queue);
379 ceph_msg_remove_list(&con->out_sent);
380
Sage Weilcf3e5c42009-12-11 09:48:05 -0800381 if (con->in_msg) {
382 ceph_msg_put(con->in_msg);
383 con->in_msg = NULL;
384 }
385
Sage Weil31b80062009-10-06 11:31:13 -0700386 con->connect_seq = 0;
387 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800388 if (con->out_msg) {
389 ceph_msg_put(con->out_msg);
390 con->out_msg = NULL;
391 }
Sage Weil31b80062009-10-06 11:31:13 -0700392 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700393 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700394}
395
396/*
397 * mark a peer down. drop any open connections.
398 */
399void ceph_con_close(struct ceph_connection *con)
400{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700401 dout("con_close %p peer %s\n", con,
402 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700403 set_bit(CLOSED, &con->state); /* in case there's queued work */
404 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Sage Weil1679f872010-02-26 13:55:51 -0800405 clear_bit(LOSSYTX, &con->state); /* so we retry next connect */
406 clear_bit(KEEPALIVE_PENDING, &con->state);
407 clear_bit(WRITE_PENDING, &con->state);
Sage Weilec302642009-12-22 10:43:42 -0800408 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700409 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700410 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800411 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800412 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700413 queue_con(con);
414}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700415EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700416
417/*
Sage Weil31b80062009-10-06 11:31:13 -0700418 * Reopen a closed connection, with a new peer address.
419 */
420void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
421{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700422 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700423 set_bit(OPENING, &con->state);
424 clear_bit(CLOSED, &con->state);
425 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800426 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700427 queue_con(con);
428}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700429EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700430
431/*
Sage Weil87b315a2010-03-22 14:51:18 -0700432 * return true if this connection ever successfully opened
433 */
434bool ceph_con_opened(struct ceph_connection *con)
435{
436 return con->connect_seq > 0;
437}
438
439/*
Sage Weil31b80062009-10-06 11:31:13 -0700440 * generic get/put
441 */
442struct ceph_connection *ceph_con_get(struct ceph_connection *con)
443{
Alex Elderd3002b92012-02-14 14:05:33 -0600444 int nref = __atomic_add_unless(&con->nref, 1, 0);
445
446 dout("con_get %p nref = %d -> %d\n", con, nref, nref + 1);
447
448 return nref ? con : NULL;
Sage Weil31b80062009-10-06 11:31:13 -0700449}
450
451void ceph_con_put(struct ceph_connection *con)
452{
Alex Elderd3002b92012-02-14 14:05:33 -0600453 int nref = atomic_dec_return(&con->nref);
454
455 BUG_ON(nref < 0);
456 if (nref == 0) {
Sage Weil71ececd2009-11-18 11:27:06 -0800457 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -0700458 kfree(con);
459 }
Alex Elderd3002b92012-02-14 14:05:33 -0600460 dout("con_put %p nref = %d -> %d\n", con, nref + 1, nref);
Sage Weil31b80062009-10-06 11:31:13 -0700461}
462
463/*
464 * initialize a new connection.
465 */
466void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con)
467{
468 dout("con_init %p\n", con);
469 memset(con, 0, sizeof(*con));
470 atomic_set(&con->nref, 1);
471 con->msgr = msgr;
Sage Weilec302642009-12-22 10:43:42 -0800472 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700473 INIT_LIST_HEAD(&con->out_queue);
474 INIT_LIST_HEAD(&con->out_sent);
475 INIT_DELAYED_WORK(&con->work, con_work);
476}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700477EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700478
479
480/*
481 * We maintain a global counter to order connection attempts. Get
482 * a unique seq greater than @gt.
483 */
484static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
485{
486 u32 ret;
487
488 spin_lock(&msgr->global_seq_lock);
489 if (msgr->global_seq < gt)
490 msgr->global_seq = gt;
491 ret = ++msgr->global_seq;
492 spin_unlock(&msgr->global_seq_lock);
493 return ret;
494}
495
Alex Elder859eb792012-02-14 14:05:33 -0600496static void ceph_con_out_kvec_reset(struct ceph_connection *con)
497{
498 con->out_kvec_left = 0;
499 con->out_kvec_bytes = 0;
500 con->out_kvec_cur = &con->out_kvec[0];
501}
502
503static void ceph_con_out_kvec_add(struct ceph_connection *con,
504 size_t size, void *data)
505{
506 int index;
507
508 index = con->out_kvec_left;
509 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
510
511 con->out_kvec[index].iov_len = size;
512 con->out_kvec[index].iov_base = data;
513 con->out_kvec_left++;
514 con->out_kvec_bytes += size;
515}
Sage Weil31b80062009-10-06 11:31:13 -0700516
517/*
518 * Prepare footer for currently outgoing message, and finish things
519 * off. Assumes out_kvec* are already valid.. we just add on to the end.
520 */
Alex Elder859eb792012-02-14 14:05:33 -0600521static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700522{
523 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600524 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700525
526 dout("prepare_write_message_footer %p\n", con);
527 con->out_kvec_is_msg = true;
528 con->out_kvec[v].iov_base = &m->footer;
529 con->out_kvec[v].iov_len = sizeof(m->footer);
530 con->out_kvec_bytes += sizeof(m->footer);
531 con->out_kvec_left++;
532 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800533 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700534}
535
536/*
537 * Prepare headers for the next outgoing message.
538 */
539static void prepare_write_message(struct ceph_connection *con)
540{
541 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600542 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700543
Alex Elder859eb792012-02-14 14:05:33 -0600544 ceph_con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700545 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800546 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700547
548 /* Sneak an ack in there first? If we can get it into the same
549 * TCP packet that's a good thing. */
550 if (con->in_seq > con->in_seq_acked) {
551 con->in_seq_acked = con->in_seq;
Alex Elder859eb792012-02-14 14:05:33 -0600552 ceph_con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700553 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Elder859eb792012-02-14 14:05:33 -0600554 ceph_con_out_kvec_add(con, sizeof (con->out_temp_ack),
555 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700556 }
557
Alex Elder859eb792012-02-14 14:05:33 -0600558 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800559 con->out_msg = m;
Sage Weil4cf9d542011-07-26 11:27:24 -0700560
561 /* put message on sent list */
562 ceph_msg_get(m);
563 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700564
Sage Weile84346b2010-05-11 21:20:38 -0700565 /*
566 * only assign outgoing seq # if we haven't sent this message
567 * yet. if it is requeued, resend with it's original seq.
568 */
569 if (m->needs_out_seq) {
570 m->hdr.seq = cpu_to_le64(++con->out_seq);
571 m->needs_out_seq = false;
572 }
Sage Weil31b80062009-10-06 11:31:13 -0700573
574 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
575 m, con->out_seq, le16_to_cpu(m->hdr.type),
576 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
577 le32_to_cpu(m->hdr.data_len),
578 m->nr_pages);
579 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
580
581 /* tag + hdr + front + middle */
Alex Elder859eb792012-02-14 14:05:33 -0600582 ceph_con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
583 ceph_con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
584 ceph_con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
585
Sage Weil31b80062009-10-06 11:31:13 -0700586 if (m->middle)
Alex Elder859eb792012-02-14 14:05:33 -0600587 ceph_con_out_kvec_add(con, m->middle->vec.iov_len,
588 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700589
590 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600591 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
592 con->out_msg->hdr.crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -0700593 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
Alex Eldera9a0c512012-02-15 07:43:54 -0600594
595 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
596 con->out_msg->footer.front_crc = cpu_to_le32(crc);
597 if (m->middle) {
598 crc = crc32c(0, m->middle->vec.iov_base,
599 m->middle->vec.iov_len);
600 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
601 } else
Sage Weil31b80062009-10-06 11:31:13 -0700602 con->out_msg->footer.middle_crc = 0;
603 con->out_msg->footer.data_crc = 0;
604 dout("prepare_write_message front_crc %u data_crc %u\n",
605 le32_to_cpu(con->out_msg->footer.front_crc),
606 le32_to_cpu(con->out_msg->footer.middle_crc));
607
608 /* is there a data payload? */
609 if (le32_to_cpu(m->hdr.data_len) > 0) {
610 /* initialize page iterator */
611 con->out_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700612 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -0800613 con->out_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700614 else
615 con->out_msg_pos.page_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700616 con->out_msg_pos.data_pos = 0;
Alex Elderbca064d2012-02-15 07:43:54 -0600617 con->out_msg_pos.did_page_crc = false;
Sage Weil31b80062009-10-06 11:31:13 -0700618 con->out_more = 1; /* data + footer will follow */
619 } else {
620 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600621 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700622 }
623
624 set_bit(WRITE_PENDING, &con->state);
625}
626
627/*
628 * Prepare an ack.
629 */
630static void prepare_write_ack(struct ceph_connection *con)
631{
632 dout("prepare_write_ack %p %llu -> %llu\n", con,
633 con->in_seq_acked, con->in_seq);
634 con->in_seq_acked = con->in_seq;
635
Alex Elder859eb792012-02-14 14:05:33 -0600636 ceph_con_out_kvec_reset(con);
637
638 ceph_con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
639
Sage Weil31b80062009-10-06 11:31:13 -0700640 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Elder859eb792012-02-14 14:05:33 -0600641 ceph_con_out_kvec_add(con, sizeof (con->out_temp_ack),
642 &con->out_temp_ack);
643
Sage Weil31b80062009-10-06 11:31:13 -0700644 con->out_more = 1; /* more will follow.. eventually.. */
645 set_bit(WRITE_PENDING, &con->state);
646}
647
648/*
649 * Prepare to write keepalive byte.
650 */
651static void prepare_write_keepalive(struct ceph_connection *con)
652{
653 dout("prepare_write_keepalive %p\n", con);
Alex Elder859eb792012-02-14 14:05:33 -0600654 ceph_con_out_kvec_reset(con);
655 ceph_con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -0700656 set_bit(WRITE_PENDING, &con->state);
657}
658
659/*
660 * Connection negotiation.
661 */
662
Sage Weil0da5d702011-05-19 11:21:05 -0700663static int prepare_connect_authorizer(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800664{
665 void *auth_buf;
666 int auth_len = 0;
667 int auth_protocol = 0;
668
Sage Weilec302642009-12-22 10:43:42 -0800669 mutex_unlock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800670 if (con->ops->get_authorizer)
671 con->ops->get_authorizer(con, &auth_buf, &auth_len,
672 &auth_protocol, &con->auth_reply_buf,
673 &con->auth_reply_buf_len,
674 con->auth_retry);
Sage Weilec302642009-12-22 10:43:42 -0800675 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800676
Sage Weil0da5d702011-05-19 11:21:05 -0700677 if (test_bit(CLOSED, &con->state) ||
678 test_bit(OPENING, &con->state))
679 return -EAGAIN;
680
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800681 con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol);
682 con->out_connect.authorizer_len = cpu_to_le32(auth_len);
683
Alex Elder859eb792012-02-14 14:05:33 -0600684 if (auth_len)
685 ceph_con_out_kvec_add(con, auth_len, auth_buf);
686
Sage Weil0da5d702011-05-19 11:21:05 -0700687 return 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800688}
689
Sage Weil31b80062009-10-06 11:31:13 -0700690/*
691 * We connected to a peer and are saying hello.
692 */
Sage Weileed0ef22009-11-10 14:34:36 -0800693static void prepare_write_banner(struct ceph_messenger *msgr,
694 struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700695{
Alex Elder859eb792012-02-14 14:05:33 -0600696 ceph_con_out_kvec_reset(con);
697 ceph_con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
698 ceph_con_out_kvec_add(con, sizeof (msgr->my_enc_addr),
699 &msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800700
Sage Weileed0ef22009-11-10 14:34:36 -0800701 con->out_more = 0;
702 set_bit(WRITE_PENDING, &con->state);
703}
704
Sage Weil0da5d702011-05-19 11:21:05 -0700705static int prepare_write_connect(struct ceph_messenger *msgr,
706 struct ceph_connection *con,
Alex Elder963be4d2012-02-14 14:05:33 -0600707 int include_banner)
Sage Weileed0ef22009-11-10 14:34:36 -0800708{
Sage Weil31b80062009-10-06 11:31:13 -0700709 unsigned global_seq = get_global_seq(con->msgr, 0);
710 int proto;
711
712 switch (con->peer_name.type) {
713 case CEPH_ENTITY_TYPE_MON:
714 proto = CEPH_MONC_PROTOCOL;
715 break;
716 case CEPH_ENTITY_TYPE_OSD:
717 proto = CEPH_OSDC_PROTOCOL;
718 break;
719 case CEPH_ENTITY_TYPE_MDS:
720 proto = CEPH_MDSC_PROTOCOL;
721 break;
722 default:
723 BUG();
724 }
725
726 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
727 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800728
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700729 con->out_connect.features = cpu_to_le64(msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700730 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
731 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
732 con->out_connect.global_seq = cpu_to_le32(global_seq);
733 con->out_connect.protocol_version = cpu_to_le32(proto);
734 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700735
Alex Elder963be4d2012-02-14 14:05:33 -0600736 if (include_banner)
737 prepare_write_banner(msgr, con);
Alex Elder859eb792012-02-14 14:05:33 -0600738 else
739 ceph_con_out_kvec_reset(con);
740 ceph_con_out_kvec_add(con, sizeof (con->out_connect), &con->out_connect);
741
Sage Weil31b80062009-10-06 11:31:13 -0700742 con->out_more = 0;
743 set_bit(WRITE_PENDING, &con->state);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800744
Sage Weil0da5d702011-05-19 11:21:05 -0700745 return prepare_connect_authorizer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700746}
747
Sage Weil31b80062009-10-06 11:31:13 -0700748/*
749 * write as much of pending kvecs to the socket as we can.
750 * 1 -> done
751 * 0 -> socket full, but more to do
752 * <0 -> error
753 */
754static int write_partial_kvec(struct ceph_connection *con)
755{
756 int ret;
757
758 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
759 while (con->out_kvec_bytes > 0) {
760 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
761 con->out_kvec_left, con->out_kvec_bytes,
762 con->out_more);
763 if (ret <= 0)
764 goto out;
765 con->out_kvec_bytes -= ret;
766 if (con->out_kvec_bytes == 0)
767 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600768
769 /* account for full iov entries consumed */
770 while (ret >= con->out_kvec_cur->iov_len) {
771 BUG_ON(!con->out_kvec_left);
772 ret -= con->out_kvec_cur->iov_len;
773 con->out_kvec_cur++;
774 con->out_kvec_left--;
775 }
776 /* and for a partially-consumed entry */
777 if (ret) {
778 con->out_kvec_cur->iov_len -= ret;
779 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700780 }
781 }
782 con->out_kvec_left = 0;
783 con->out_kvec_is_msg = false;
784 ret = 1;
785out:
786 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
787 con->out_kvec_bytes, con->out_kvec_left, ret);
788 return ret; /* done! */
789}
790
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700791#ifdef CONFIG_BLOCK
792static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
793{
794 if (!bio) {
795 *iter = NULL;
796 *seg = 0;
797 return;
798 }
799 *iter = bio;
800 *seg = bio->bi_idx;
801}
802
803static void iter_bio_next(struct bio **bio_iter, int *seg)
804{
805 if (*bio_iter == NULL)
806 return;
807
808 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
809
810 (*seg)++;
811 if (*seg == (*bio_iter)->bi_vcnt)
812 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
813}
814#endif
815
Sage Weil31b80062009-10-06 11:31:13 -0700816/*
817 * Write as much message data payload as we can. If we finish, queue
818 * up the footer.
819 * 1 -> done, footer is now queued in out_kvec[].
820 * 0 -> socket full, but more to do
821 * <0 -> error
822 */
823static int write_partial_msg_pages(struct ceph_connection *con)
824{
825 struct ceph_msg *msg = con->out_msg;
826 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
827 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600828 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700829 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700830 int total_max_write;
831 int in_trail = 0;
832 size_t trail_len = (msg->trail ? msg->trail->length : 0);
Sage Weil31b80062009-10-06 11:31:13 -0700833
834 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
835 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
836 con->out_msg_pos.page_pos);
837
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700838#ifdef CONFIG_BLOCK
839 if (msg->bio && !msg->bio_iter)
840 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
841#endif
842
843 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -0700844 struct page *page = NULL;
845 void *kaddr = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700846 int max_write = PAGE_SIZE;
847 int page_shift = 0;
848
849 total_max_write = data_len - trail_len -
850 con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -0700851
852 /*
853 * if we are calculating the data crc (the default), we need
854 * to map the page. if our pages[] has been revoked, use the
855 * zero page.
856 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700857
858 /* have we reached the trail part of the data? */
859 if (con->out_msg_pos.data_pos >= data_len - trail_len) {
860 in_trail = 1;
861
862 total_max_write = data_len - con->out_msg_pos.data_pos;
863
864 page = list_first_entry(&msg->trail->head,
865 struct page, lru);
Alex Elder37675b02012-03-07 11:40:08 -0600866 if (do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700867 kaddr = kmap(page);
868 max_write = PAGE_SIZE;
869 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -0700870 page = msg->pages[con->out_msg_pos.page];
Alex Elder37675b02012-03-07 11:40:08 -0600871 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -0700872 kaddr = kmap(page);
Sage Weil58bb3b32009-12-23 12:12:31 -0800873 } else if (msg->pagelist) {
874 page = list_first_entry(&msg->pagelist->head,
875 struct page, lru);
Alex Elder37675b02012-03-07 11:40:08 -0600876 if (do_datacrc)
Sage Weil58bb3b32009-12-23 12:12:31 -0800877 kaddr = kmap(page);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700878#ifdef CONFIG_BLOCK
879 } else if (msg->bio) {
880 struct bio_vec *bv;
881
882 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
883 page = bv->bv_page;
884 page_shift = bv->bv_offset;
Alex Elder37675b02012-03-07 11:40:08 -0600885 if (do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700886 kaddr = kmap(page) + page_shift;
887 max_write = bv->bv_len;
888#endif
Sage Weil31b80062009-10-06 11:31:13 -0700889 } else {
Alex Elder57666512012-01-23 15:49:27 -0600890 page = zero_page;
Alex Elder37675b02012-03-07 11:40:08 -0600891 if (do_datacrc)
Alex Elder57666512012-01-23 15:49:27 -0600892 kaddr = zero_page_address;
Sage Weil31b80062009-10-06 11:31:13 -0700893 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700894 len = min_t(int, max_write - con->out_msg_pos.page_pos,
895 total_max_write);
896
Alex Elder37675b02012-03-07 11:40:08 -0600897 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Eldera9a0c512012-02-15 07:43:54 -0600898 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700899 void *base = kaddr + con->out_msg_pos.page_pos;
900 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
901
902 BUG_ON(kaddr == NULL);
Alex Eldera9a0c512012-02-15 07:43:54 -0600903 crc = crc32c(tmpcrc, base, len);
904 con->out_msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -0600905 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -0700906 }
Alex Eldere36b13c2012-03-07 11:40:08 -0600907 ret = ceph_tcp_sendpage(con->sock, page,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700908 con->out_msg_pos.page_pos + page_shift,
Alex Eldere36b13c2012-03-07 11:40:08 -0600909 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -0700910
Alex Elder37675b02012-03-07 11:40:08 -0600911 if (do_datacrc && kaddr != zero_page_address)
Sage Weil31b80062009-10-06 11:31:13 -0700912 kunmap(page);
913
914 if (ret <= 0)
915 goto out;
916
917 con->out_msg_pos.data_pos += ret;
918 con->out_msg_pos.page_pos += ret;
919 if (ret == len) {
920 con->out_msg_pos.page_pos = 0;
921 con->out_msg_pos.page++;
Alex Elderbca064d2012-02-15 07:43:54 -0600922 con->out_msg_pos.did_page_crc = false;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700923 if (in_trail)
924 list_move_tail(&page->lru,
925 &msg->trail->head);
926 else if (msg->pagelist)
Sage Weil58bb3b32009-12-23 12:12:31 -0800927 list_move_tail(&page->lru,
928 &msg->pagelist->head);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700929#ifdef CONFIG_BLOCK
930 else if (msg->bio)
931 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
932#endif
Sage Weil31b80062009-10-06 11:31:13 -0700933 }
934 }
935
936 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
937
938 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -0600939 if (!do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -0700940 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Elder859eb792012-02-14 14:05:33 -0600941 ceph_con_out_kvec_reset(con);
942 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700943 ret = 1;
944out:
945 return ret;
946}
947
948/*
949 * write some zeros
950 */
951static int write_partial_skip(struct ceph_connection *con)
952{
953 int ret;
954
955 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -0600956 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -0700957
Alex Elder31739132012-03-07 11:40:08 -0600958 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -0700959 if (ret <= 0)
960 goto out;
961 con->out_skip -= ret;
962 }
963 ret = 1;
964out:
965 return ret;
966}
967
968/*
969 * Prepare to read connection handshake, or an ack.
970 */
Sage Weileed0ef22009-11-10 14:34:36 -0800971static void prepare_read_banner(struct ceph_connection *con)
972{
973 dout("prepare_read_banner %p\n", con);
974 con->in_base_pos = 0;
975}
976
Sage Weil31b80062009-10-06 11:31:13 -0700977static void prepare_read_connect(struct ceph_connection *con)
978{
979 dout("prepare_read_connect %p\n", con);
980 con->in_base_pos = 0;
981}
982
983static void prepare_read_ack(struct ceph_connection *con)
984{
985 dout("prepare_read_ack %p\n", con);
986 con->in_base_pos = 0;
987}
988
989static void prepare_read_tag(struct ceph_connection *con)
990{
991 dout("prepare_read_tag %p\n", con);
992 con->in_base_pos = 0;
993 con->in_tag = CEPH_MSGR_TAG_READY;
994}
995
996/*
997 * Prepare to read a message.
998 */
999static int prepare_read_message(struct ceph_connection *con)
1000{
1001 dout("prepare_read_message %p\n", con);
1002 BUG_ON(con->in_msg != NULL);
1003 con->in_base_pos = 0;
1004 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1005 return 0;
1006}
1007
1008
1009static int read_partial(struct ceph_connection *con,
1010 int *to, int size, void *object)
1011{
1012 *to += size;
1013 while (con->in_base_pos < *to) {
1014 int left = *to - con->in_base_pos;
1015 int have = size - left;
1016 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1017 if (ret <= 0)
1018 return ret;
1019 con->in_base_pos += ret;
1020 }
1021 return 1;
1022}
1023
1024
1025/*
1026 * Read all or part of the connect-side handshake on a new connection
1027 */
Sage Weileed0ef22009-11-10 14:34:36 -08001028static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001029{
1030 int ret, to = 0;
1031
Sage Weileed0ef22009-11-10 14:34:36 -08001032 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001033
1034 /* peer's banner */
1035 ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner);
1036 if (ret <= 0)
1037 goto out;
1038 ret = read_partial(con, &to, sizeof(con->actual_peer_addr),
1039 &con->actual_peer_addr);
1040 if (ret <= 0)
1041 goto out;
1042 ret = read_partial(con, &to, sizeof(con->peer_addr_for_me),
1043 &con->peer_addr_for_me);
1044 if (ret <= 0)
1045 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08001046out:
1047 return ret;
1048}
1049
1050static int read_partial_connect(struct ceph_connection *con)
1051{
1052 int ret, to = 0;
1053
1054 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1055
Sage Weil31b80062009-10-06 11:31:13 -07001056 ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply);
1057 if (ret <= 0)
1058 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001059 ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len),
1060 con->auth_reply_buf);
1061 if (ret <= 0)
1062 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001063
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001064 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1065 con, (int)con->in_reply.tag,
1066 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001067 le32_to_cpu(con->in_reply.global_seq));
1068out:
1069 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001070
Sage Weil31b80062009-10-06 11:31:13 -07001071}
1072
1073/*
1074 * Verify the hello banner looks okay.
1075 */
1076static int verify_hello(struct ceph_connection *con)
1077{
1078 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001079 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001080 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001081 con->error_msg = "protocol error, bad banner";
1082 return -1;
1083 }
1084 return 0;
1085}
1086
1087static bool addr_is_blank(struct sockaddr_storage *ss)
1088{
1089 switch (ss->ss_family) {
1090 case AF_INET:
1091 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1092 case AF_INET6:
1093 return
1094 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1095 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1096 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1097 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1098 }
1099 return false;
1100}
1101
1102static int addr_port(struct sockaddr_storage *ss)
1103{
1104 switch (ss->ss_family) {
1105 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001106 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001107 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001108 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001109 }
1110 return 0;
1111}
1112
1113static void addr_set_port(struct sockaddr_storage *ss, int p)
1114{
1115 switch (ss->ss_family) {
1116 case AF_INET:
1117 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001118 break;
Sage Weil31b80062009-10-06 11:31:13 -07001119 case AF_INET6:
1120 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001121 break;
Sage Weil31b80062009-10-06 11:31:13 -07001122 }
1123}
1124
1125/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001126 * Unlike other *_pton function semantics, zero indicates success.
1127 */
1128static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1129 char delim, const char **ipend)
1130{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001131 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1132 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001133
1134 memset(ss, 0, sizeof(*ss));
1135
1136 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1137 ss->ss_family = AF_INET;
1138 return 0;
1139 }
1140
1141 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1142 ss->ss_family = AF_INET6;
1143 return 0;
1144 }
1145
1146 return -EINVAL;
1147}
1148
1149/*
1150 * Extract hostname string and resolve using kernel DNS facility.
1151 */
1152#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1153static int ceph_dns_resolve_name(const char *name, size_t namelen,
1154 struct sockaddr_storage *ss, char delim, const char **ipend)
1155{
1156 const char *end, *delim_p;
1157 char *colon_p, *ip_addr = NULL;
1158 int ip_len, ret;
1159
1160 /*
1161 * The end of the hostname occurs immediately preceding the delimiter or
1162 * the port marker (':') where the delimiter takes precedence.
1163 */
1164 delim_p = memchr(name, delim, namelen);
1165 colon_p = memchr(name, ':', namelen);
1166
1167 if (delim_p && colon_p)
1168 end = delim_p < colon_p ? delim_p : colon_p;
1169 else if (!delim_p && colon_p)
1170 end = colon_p;
1171 else {
1172 end = delim_p;
1173 if (!end) /* case: hostname:/ */
1174 end = name + namelen;
1175 }
1176
1177 if (end <= name)
1178 return -EINVAL;
1179
1180 /* do dns_resolve upcall */
1181 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1182 if (ip_len > 0)
1183 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1184 else
1185 ret = -ESRCH;
1186
1187 kfree(ip_addr);
1188
1189 *ipend = end;
1190
1191 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1192 ret, ret ? "failed" : ceph_pr_addr(ss));
1193
1194 return ret;
1195}
1196#else
1197static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1198 struct sockaddr_storage *ss, char delim, const char **ipend)
1199{
1200 return -EINVAL;
1201}
1202#endif
1203
1204/*
1205 * Parse a server name (IP or hostname). If a valid IP address is not found
1206 * then try to extract a hostname to resolve using userspace DNS upcall.
1207 */
1208static int ceph_parse_server_name(const char *name, size_t namelen,
1209 struct sockaddr_storage *ss, char delim, const char **ipend)
1210{
1211 int ret;
1212
1213 ret = ceph_pton(name, namelen, ss, delim, ipend);
1214 if (ret)
1215 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1216
1217 return ret;
1218}
1219
1220/*
Sage Weil31b80062009-10-06 11:31:13 -07001221 * Parse an ip[:port] list into an addr array. Use the default
1222 * monitor port if a port isn't specified.
1223 */
1224int ceph_parse_ips(const char *c, const char *end,
1225 struct ceph_entity_addr *addr,
1226 int max_count, int *count)
1227{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001228 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001229 const char *p = c;
1230
1231 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1232 for (i = 0; i < max_count; i++) {
1233 const char *ipend;
1234 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001235 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001236 char delim = ',';
1237
1238 if (*p == '[') {
1239 delim = ']';
1240 p++;
1241 }
Sage Weil31b80062009-10-06 11:31:13 -07001242
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001243 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1244 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001245 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001246 ret = -EINVAL;
1247
Sage Weil31b80062009-10-06 11:31:13 -07001248 p = ipend;
1249
Sage Weil39139f62010-07-08 09:54:52 -07001250 if (delim == ']') {
1251 if (*p != ']') {
1252 dout("missing matching ']'\n");
1253 goto bad;
1254 }
1255 p++;
1256 }
1257
Sage Weil31b80062009-10-06 11:31:13 -07001258 /* port? */
1259 if (p < end && *p == ':') {
1260 port = 0;
1261 p++;
1262 while (p < end && *p >= '0' && *p <= '9') {
1263 port = (port * 10) + (*p - '0');
1264 p++;
1265 }
1266 if (port > 65535 || port == 0)
1267 goto bad;
1268 } else {
1269 port = CEPH_MON_PORT;
1270 }
1271
1272 addr_set_port(ss, port);
1273
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001274 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001275
1276 if (p == end)
1277 break;
1278 if (*p != ',')
1279 goto bad;
1280 p++;
1281 }
1282
1283 if (p != end)
1284 goto bad;
1285
1286 if (count)
1287 *count = i + 1;
1288 return 0;
1289
1290bad:
Sage Weil39139f62010-07-08 09:54:52 -07001291 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001292 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001293}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001294EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001295
Sage Weileed0ef22009-11-10 14:34:36 -08001296static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001297{
Sage Weileed0ef22009-11-10 14:34:36 -08001298 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001299
1300 if (verify_hello(con) < 0)
1301 return -1;
1302
Sage Weil63f2d212009-11-03 15:17:56 -08001303 ceph_decode_addr(&con->actual_peer_addr);
1304 ceph_decode_addr(&con->peer_addr_for_me);
1305
Sage Weil31b80062009-10-06 11:31:13 -07001306 /*
1307 * Make sure the other end is who we wanted. note that the other
1308 * end may not yet know their ip address, so if it's 0.0.0.0, give
1309 * them the benefit of the doubt.
1310 */
Sage Weil103e2d32010-01-07 16:12:36 -08001311 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1312 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001313 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1314 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001315 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001316 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001317 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001318 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001319 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001320 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001321 return -1;
1322 }
1323
1324 /*
1325 * did we learn our address?
1326 */
1327 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1328 int port = addr_port(&con->msgr->inst.addr.in_addr);
1329
1330 memcpy(&con->msgr->inst.addr.in_addr,
1331 &con->peer_addr_for_me.in_addr,
1332 sizeof(con->peer_addr_for_me.in_addr));
1333 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001334 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001335 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001336 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001337 }
1338
Sage Weileed0ef22009-11-10 14:34:36 -08001339 set_bit(NEGOTIATING, &con->state);
1340 prepare_read_connect(con);
1341 return 0;
1342}
1343
Sage Weil04a419f2009-12-23 09:30:21 -08001344static void fail_protocol(struct ceph_connection *con)
1345{
1346 reset_connection(con);
1347 set_bit(CLOSED, &con->state); /* in case there's queued work */
1348
1349 mutex_unlock(&con->mutex);
1350 if (con->ops->bad_proto)
1351 con->ops->bad_proto(con);
1352 mutex_lock(&con->mutex);
1353}
1354
Sage Weileed0ef22009-11-10 14:34:36 -08001355static int process_connect(struct ceph_connection *con)
1356{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001357 u64 sup_feat = con->msgr->supported_features;
1358 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001359 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001360 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001361
Sage Weileed0ef22009-11-10 14:34:36 -08001362 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1363
Sage Weil31b80062009-10-06 11:31:13 -07001364 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001365 case CEPH_MSGR_TAG_FEATURES:
1366 pr_err("%s%lld %s feature set mismatch,"
1367 " my %llx < server's %llx, missing %llx\n",
1368 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001369 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001370 sup_feat, server_feat, server_feat & ~sup_feat);
1371 con->error_msg = "missing required protocol features";
1372 fail_protocol(con);
1373 return -1;
1374
Sage Weil31b80062009-10-06 11:31:13 -07001375 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001376 pr_err("%s%lld %s protocol version mismatch,"
1377 " my %d != server's %d\n",
1378 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001379 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001380 le32_to_cpu(con->out_connect.protocol_version),
1381 le32_to_cpu(con->in_reply.protocol_version));
1382 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001383 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001384 return -1;
1385
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001386 case CEPH_MSGR_TAG_BADAUTHORIZER:
1387 con->auth_retry++;
1388 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1389 con->auth_retry);
1390 if (con->auth_retry == 2) {
1391 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001392 return -1;
1393 }
1394 con->auth_retry = 1;
Sage Weil0da5d702011-05-19 11:21:05 -07001395 ret = prepare_write_connect(con->msgr, con, 0);
1396 if (ret < 0)
1397 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001398 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001399 break;
Sage Weil31b80062009-10-06 11:31:13 -07001400
1401 case CEPH_MSGR_TAG_RESETSESSION:
1402 /*
1403 * If we connected with a large connect_seq but the peer
1404 * has no record of a session with us (no connection, or
1405 * connect_seq == 0), they will send RESETSESION to indicate
1406 * that they must have reset their session, and may have
1407 * dropped messages.
1408 */
1409 dout("process_connect got RESET peer seq %u\n",
1410 le32_to_cpu(con->in_connect.connect_seq));
1411 pr_err("%s%lld %s connection reset\n",
1412 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001413 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001414 reset_connection(con);
Sage Weileed0ef22009-11-10 14:34:36 -08001415 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001416 prepare_read_connect(con);
1417
1418 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001419 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001420 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1421 if (con->ops->peer_reset)
1422 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001423 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07001424 if (test_bit(CLOSED, &con->state) ||
1425 test_bit(OPENING, &con->state))
1426 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001427 break;
1428
1429 case CEPH_MSGR_TAG_RETRY_SESSION:
1430 /*
1431 * If we sent a smaller connect_seq than the peer has, try
1432 * again with a larger value.
1433 */
1434 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1435 le32_to_cpu(con->out_connect.connect_seq),
1436 le32_to_cpu(con->in_connect.connect_seq));
1437 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Sage Weileed0ef22009-11-10 14:34:36 -08001438 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001439 prepare_read_connect(con);
1440 break;
1441
1442 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1443 /*
1444 * If we sent a smaller global_seq than the peer has, try
1445 * again with a larger value.
1446 */
Sage Weileed0ef22009-11-10 14:34:36 -08001447 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001448 con->peer_global_seq,
1449 le32_to_cpu(con->in_connect.global_seq));
1450 get_global_seq(con->msgr,
1451 le32_to_cpu(con->in_connect.global_seq));
Sage Weileed0ef22009-11-10 14:34:36 -08001452 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001453 prepare_read_connect(con);
1454 break;
1455
1456 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001457 if (req_feat & ~server_feat) {
1458 pr_err("%s%lld %s protocol feature mismatch,"
1459 " my required %llx > server's %llx, need %llx\n",
1460 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001461 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001462 req_feat, server_feat, req_feat & ~server_feat);
1463 con->error_msg = "missing required protocol features";
1464 fail_protocol(con);
1465 return -1;
1466 }
Sage Weil31b80062009-10-06 11:31:13 -07001467 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001468 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1469 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001470 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001471 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1472 con->peer_global_seq,
1473 le32_to_cpu(con->in_reply.connect_seq),
1474 con->connect_seq);
1475 WARN_ON(con->connect_seq !=
1476 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001477
1478 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
1479 set_bit(LOSSYTX, &con->state);
1480
Sage Weil31b80062009-10-06 11:31:13 -07001481 prepare_read_tag(con);
1482 break;
1483
1484 case CEPH_MSGR_TAG_WAIT:
1485 /*
1486 * If there is a connection race (we are opening
1487 * connections to each other), one of us may just have
1488 * to WAIT. This shouldn't happen if we are the
1489 * client.
1490 */
Sage Weil04177882011-05-12 15:33:17 -07001491 pr_err("process_connect got WAIT as client\n");
1492 con->error_msg = "protocol error, got WAIT as client";
1493 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001494
1495 default:
1496 pr_err("connect protocol error, will retry\n");
1497 con->error_msg = "protocol error, garbage tag during connect";
1498 return -1;
1499 }
1500 return 0;
1501}
1502
1503
1504/*
1505 * read (part of) an ack
1506 */
1507static int read_partial_ack(struct ceph_connection *con)
1508{
1509 int to = 0;
1510
1511 return read_partial(con, &to, sizeof(con->in_temp_ack),
1512 &con->in_temp_ack);
1513}
1514
1515
1516/*
1517 * We can finally discard anything that's been acked.
1518 */
1519static void process_ack(struct ceph_connection *con)
1520{
1521 struct ceph_msg *m;
1522 u64 ack = le64_to_cpu(con->in_temp_ack);
1523 u64 seq;
1524
Sage Weil31b80062009-10-06 11:31:13 -07001525 while (!list_empty(&con->out_sent)) {
1526 m = list_first_entry(&con->out_sent, struct ceph_msg,
1527 list_head);
1528 seq = le64_to_cpu(m->hdr.seq);
1529 if (seq > ack)
1530 break;
1531 dout("got ack for seq %llu type %d at %p\n", seq,
1532 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001533 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001534 ceph_msg_remove(m);
1535 }
Sage Weil31b80062009-10-06 11:31:13 -07001536 prepare_read_tag(con);
1537}
1538
1539
1540
1541
Yehuda Sadeh24504182010-01-08 13:58:34 -08001542static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001543 struct kvec *section,
1544 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001545{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001546 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001547
Yehuda Sadeh24504182010-01-08 13:58:34 -08001548 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001549
Yehuda Sadeh24504182010-01-08 13:58:34 -08001550 while (section->iov_len < sec_len) {
1551 BUG_ON(section->iov_base == NULL);
1552 left = sec_len - section->iov_len;
1553 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1554 section->iov_len, left);
1555 if (ret <= 0)
1556 return ret;
1557 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001558 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001559 if (section->iov_len == sec_len)
1560 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001561
1562 return 1;
1563}
1564
1565static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
1566 struct ceph_msg_header *hdr,
1567 int *skip);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001568
1569
1570static int read_partial_message_pages(struct ceph_connection *con,
1571 struct page **pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001572 unsigned data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001573{
1574 void *p;
1575 int ret;
1576 int left;
1577
1578 left = min((int)(data_len - con->in_msg_pos.data_pos),
1579 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1580 /* (page) data */
1581 BUG_ON(pages == NULL);
1582 p = kmap(pages[con->in_msg_pos.page]);
1583 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1584 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001585 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001586 con->in_data_crc =
1587 crc32c(con->in_data_crc,
1588 p + con->in_msg_pos.page_pos, ret);
1589 kunmap(pages[con->in_msg_pos.page]);
1590 if (ret <= 0)
1591 return ret;
1592 con->in_msg_pos.data_pos += ret;
1593 con->in_msg_pos.page_pos += ret;
1594 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1595 con->in_msg_pos.page_pos = 0;
1596 con->in_msg_pos.page++;
1597 }
1598
1599 return ret;
1600}
1601
1602#ifdef CONFIG_BLOCK
1603static int read_partial_message_bio(struct ceph_connection *con,
1604 struct bio **bio_iter, int *bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001605 unsigned data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001606{
1607 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1608 void *p;
1609 int ret, left;
1610
1611 if (IS_ERR(bv))
1612 return PTR_ERR(bv);
1613
1614 left = min((int)(data_len - con->in_msg_pos.data_pos),
1615 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1616
1617 p = kmap(bv->bv_page) + bv->bv_offset;
1618
1619 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1620 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001621 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001622 con->in_data_crc =
1623 crc32c(con->in_data_crc,
1624 p + con->in_msg_pos.page_pos, ret);
1625 kunmap(bv->bv_page);
1626 if (ret <= 0)
1627 return ret;
1628 con->in_msg_pos.data_pos += ret;
1629 con->in_msg_pos.page_pos += ret;
1630 if (con->in_msg_pos.page_pos == bv->bv_len) {
1631 con->in_msg_pos.page_pos = 0;
1632 iter_bio_next(bio_iter, bio_seg);
1633 }
1634
1635 return ret;
1636}
1637#endif
1638
Sage Weil31b80062009-10-06 11:31:13 -07001639/*
1640 * read (part of) a message.
1641 */
1642static int read_partial_message(struct ceph_connection *con)
1643{
1644 struct ceph_msg *m = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001645 int ret;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001646 int to, left;
Sage Weilc5c6b192010-11-09 12:40:00 -08001647 unsigned front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001648 bool do_datacrc = !con->msgr->nocrc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001649 int skip;
Sage Weilae187562010-04-22 07:47:01 -07001650 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001651 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001652
1653 dout("read_partial_message con %p msg %p\n", con, m);
1654
1655 /* header */
1656 while (con->in_base_pos < sizeof(con->in_hdr)) {
1657 left = sizeof(con->in_hdr) - con->in_base_pos;
1658 ret = ceph_tcp_recvmsg(con->sock,
1659 (char *)&con->in_hdr + con->in_base_pos,
1660 left);
1661 if (ret <= 0)
1662 return ret;
1663 con->in_base_pos += ret;
Sage Weil31b80062009-10-06 11:31:13 -07001664 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001665
1666 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1667 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1668 pr_err("read_partial_message bad hdr "
1669 " crc %u != expected %u\n",
1670 crc, con->in_hdr.crc);
1671 return -EBADMSG;
1672 }
1673
Sage Weil31b80062009-10-06 11:31:13 -07001674 front_len = le32_to_cpu(con->in_hdr.front_len);
1675 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1676 return -EIO;
1677 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1678 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1679 return -EIO;
1680 data_len = le32_to_cpu(con->in_hdr.data_len);
1681 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1682 return -EIO;
1683
Sage Weilae187562010-04-22 07:47:01 -07001684 /* verify seq# */
1685 seq = le64_to_cpu(con->in_hdr.seq);
1686 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001687 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001688 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001689 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001690 seq, con->in_seq + 1);
1691 con->in_base_pos = -front_len - middle_len - data_len -
1692 sizeof(m->footer);
1693 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001694 return 0;
1695 } else if ((s64)seq - (s64)con->in_seq > 1) {
1696 pr_err("read_partial_message bad seq %lld expected %lld\n",
1697 seq, con->in_seq + 1);
1698 con->error_msg = "bad message sequence # for incoming message";
1699 return -EBADMSG;
1700 }
1701
Sage Weil31b80062009-10-06 11:31:13 -07001702 /* allocate message? */
1703 if (!con->in_msg) {
1704 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1705 con->in_hdr.front_len, con->in_hdr.data_len);
Sage Weilae32be32010-06-13 10:30:19 -07001706 skip = 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001707 con->in_msg = ceph_alloc_msg(con, &con->in_hdr, &skip);
1708 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07001709 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001710 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001711 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001712 con->in_base_pos = -front_len - middle_len - data_len -
1713 sizeof(m->footer);
1714 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001715 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001716 return 0;
1717 }
Sage Weila79832f2010-04-01 16:06:19 -07001718 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001719 con->error_msg =
1720 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001721 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001722 }
1723 m = con->in_msg;
1724 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001725 if (m->middle)
1726 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001727
1728 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001729 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001730 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001731 else
1732 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001733 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001734 }
1735
1736 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001737 ret = read_partial_message_section(con, &m->front, front_len,
1738 &con->in_front_crc);
1739 if (ret <= 0)
1740 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001741
1742 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001743 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001744 ret = read_partial_message_section(con, &m->middle->vec,
1745 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001746 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001747 if (ret <= 0)
1748 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001749 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001750#ifdef CONFIG_BLOCK
1751 if (m->bio && !m->bio_iter)
1752 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1753#endif
Sage Weil31b80062009-10-06 11:31:13 -07001754
1755 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001756 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001757 if (m->pages) {
1758 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001759 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001760 if (ret <= 0)
1761 return ret;
1762#ifdef CONFIG_BLOCK
1763 } else if (m->bio) {
1764
1765 ret = read_partial_message_bio(con,
1766 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001767 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001768 if (ret <= 0)
1769 return ret;
1770#endif
1771 } else {
1772 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001773 }
1774 }
1775
Sage Weil31b80062009-10-06 11:31:13 -07001776 /* footer */
1777 to = sizeof(m->hdr) + sizeof(m->footer);
1778 while (con->in_base_pos < to) {
1779 left = to - con->in_base_pos;
1780 ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer +
1781 (con->in_base_pos - sizeof(m->hdr)),
1782 left);
1783 if (ret <= 0)
1784 return ret;
1785 con->in_base_pos += ret;
1786 }
1787 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1788 m, front_len, m->footer.front_crc, middle_len,
1789 m->footer.middle_crc, data_len, m->footer.data_crc);
1790
1791 /* crc ok? */
1792 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1793 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1794 m, con->in_front_crc, m->footer.front_crc);
1795 return -EBADMSG;
1796 }
1797 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1798 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1799 m, con->in_middle_crc, m->footer.middle_crc);
1800 return -EBADMSG;
1801 }
Alex Elderbca064d2012-02-15 07:43:54 -06001802 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001803 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1804 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1805 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1806 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1807 return -EBADMSG;
1808 }
1809
1810 return 1; /* done! */
1811}
1812
1813/*
1814 * Process message. This happens in the worker thread. The callback should
1815 * be careful not to do anything that waits on other incoming messages or it
1816 * may deadlock.
1817 */
1818static void process_message(struct ceph_connection *con)
1819{
Sage Weil5e095e82009-12-14 14:30:34 -08001820 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001821
Sage Weil5e095e82009-12-14 14:30:34 -08001822 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001823 con->in_msg = NULL;
1824
1825 /* if first message, set peer_name */
1826 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001827 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001828
Sage Weil31b80062009-10-06 11:31:13 -07001829 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001830 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001831
1832 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1833 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001834 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001835 le16_to_cpu(msg->hdr.type),
1836 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1837 le32_to_cpu(msg->hdr.front_len),
1838 le32_to_cpu(msg->hdr.data_len),
1839 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1840 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001841
1842 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001843 prepare_read_tag(con);
1844}
1845
1846
1847/*
1848 * Write something to the socket. Called in a worker thread when the
1849 * socket appears to be writeable and we have something ready to send.
1850 */
1851static int try_write(struct ceph_connection *con)
1852{
1853 struct ceph_messenger *msgr = con->msgr;
1854 int ret = 1;
1855
1856 dout("try_write start %p state %lu nref %d\n", con, con->state,
1857 atomic_read(&con->nref));
1858
Sage Weil31b80062009-10-06 11:31:13 -07001859more:
1860 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1861
1862 /* open the socket first? */
1863 if (con->sock == NULL) {
Sage Weileed0ef22009-11-10 14:34:36 -08001864 prepare_write_connect(msgr, con, 1);
1865 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001866 set_bit(CONNECTING, &con->state);
Sage Weileed0ef22009-11-10 14:34:36 -08001867 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001868
Sage Weilcf3e5c42009-12-11 09:48:05 -08001869 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001870 con->in_tag = CEPH_MSGR_TAG_READY;
1871 dout("try_write initiating connect on %p new state %lu\n",
1872 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06001873 ret = ceph_tcp_connect(con);
1874 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07001875 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07001876 goto out;
1877 }
1878 }
1879
1880more_kvec:
1881 /* kvec data queued? */
1882 if (con->out_skip) {
1883 ret = write_partial_skip(con);
1884 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001885 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001886 }
1887 if (con->out_kvec_left) {
1888 ret = write_partial_kvec(con);
1889 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001890 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001891 }
1892
1893 /* msg pages? */
1894 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001895 if (con->out_msg_done) {
1896 ceph_msg_put(con->out_msg);
1897 con->out_msg = NULL; /* we're done with this one */
1898 goto do_next;
1899 }
1900
Sage Weil31b80062009-10-06 11:31:13 -07001901 ret = write_partial_msg_pages(con);
1902 if (ret == 1)
1903 goto more_kvec; /* we need to send the footer, too! */
1904 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08001905 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001906 if (ret < 0) {
1907 dout("try_write write_partial_msg_pages err %d\n",
1908 ret);
Sage Weil42961d22011-01-25 08:19:34 -08001909 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001910 }
1911 }
1912
Sage Weilc86a2932009-12-14 14:04:30 -08001913do_next:
Sage Weil31b80062009-10-06 11:31:13 -07001914 if (!test_bit(CONNECTING, &con->state)) {
1915 /* is anything else pending? */
1916 if (!list_empty(&con->out_queue)) {
1917 prepare_write_message(con);
1918 goto more;
1919 }
1920 if (con->in_seq > con->in_seq_acked) {
1921 prepare_write_ack(con);
1922 goto more;
1923 }
1924 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
1925 prepare_write_keepalive(con);
1926 goto more;
1927 }
1928 }
1929
1930 /* Nothing to do! */
1931 clear_bit(WRITE_PENDING, &con->state);
1932 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07001933 ret = 0;
1934out:
Sage Weil42961d22011-01-25 08:19:34 -08001935 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07001936 return ret;
1937}
1938
1939
1940
1941/*
1942 * Read what we can from the socket.
1943 */
1944static int try_read(struct ceph_connection *con)
1945{
Sage Weil31b80062009-10-06 11:31:13 -07001946 int ret = -1;
1947
1948 if (!con->sock)
1949 return 0;
1950
1951 if (test_bit(STANDBY, &con->state))
1952 return 0;
1953
1954 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08001955
Sage Weil31b80062009-10-06 11:31:13 -07001956more:
1957 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
1958 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07001959
1960 /*
1961 * process_connect and process_message drop and re-take
1962 * con->mutex. make sure we handle a racing close or reopen.
1963 */
1964 if (test_bit(CLOSED, &con->state) ||
1965 test_bit(OPENING, &con->state)) {
1966 ret = -EAGAIN;
1967 goto out;
1968 }
1969
Sage Weil31b80062009-10-06 11:31:13 -07001970 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08001971 if (!test_bit(NEGOTIATING, &con->state)) {
1972 dout("try_read connecting\n");
1973 ret = read_partial_banner(con);
1974 if (ret <= 0)
Sage Weileed0ef22009-11-10 14:34:36 -08001975 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08001976 ret = process_banner(con);
1977 if (ret < 0)
1978 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08001979 }
Sage Weil31b80062009-10-06 11:31:13 -07001980 ret = read_partial_connect(con);
1981 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07001982 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08001983 ret = process_connect(con);
1984 if (ret < 0)
1985 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001986 goto more;
1987 }
1988
1989 if (con->in_base_pos < 0) {
1990 /*
1991 * skipping + discarding content.
1992 *
1993 * FIXME: there must be a better way to do this!
1994 */
Alex Elder84495f42012-02-15 07:43:55 -06001995 static char buf[SKIP_BUF_SIZE];
1996 int skip = min((int) sizeof (buf), -con->in_base_pos);
1997
Sage Weil31b80062009-10-06 11:31:13 -07001998 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
1999 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2000 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002001 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002002 con->in_base_pos += ret;
2003 if (con->in_base_pos)
2004 goto more;
2005 }
2006 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2007 /*
2008 * what's next?
2009 */
2010 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2011 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002012 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002013 dout("try_read got tag %d\n", (int)con->in_tag);
2014 switch (con->in_tag) {
2015 case CEPH_MSGR_TAG_MSG:
2016 prepare_read_message(con);
2017 break;
2018 case CEPH_MSGR_TAG_ACK:
2019 prepare_read_ack(con);
2020 break;
2021 case CEPH_MSGR_TAG_CLOSE:
2022 set_bit(CLOSED, &con->state); /* fixme */
Sage Weil98bdb0a2011-01-25 08:17:48 -08002023 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002024 default:
2025 goto bad_tag;
2026 }
2027 }
2028 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2029 ret = read_partial_message(con);
2030 if (ret <= 0) {
2031 switch (ret) {
2032 case -EBADMSG:
2033 con->error_msg = "bad crc";
2034 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002035 break;
Sage Weil31b80062009-10-06 11:31:13 -07002036 case -EIO:
2037 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002038 break;
Sage Weil31b80062009-10-06 11:31:13 -07002039 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002040 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002041 }
2042 if (con->in_tag == CEPH_MSGR_TAG_READY)
2043 goto more;
2044 process_message(con);
2045 goto more;
2046 }
2047 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2048 ret = read_partial_ack(con);
2049 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002050 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002051 process_ack(con);
2052 goto more;
2053 }
2054
Sage Weil31b80062009-10-06 11:31:13 -07002055out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002056 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002057 return ret;
2058
2059bad_tag:
2060 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2061 con->error_msg = "protocol error, garbage tag";
2062 ret = -1;
2063 goto out;
2064}
2065
2066
2067/*
2068 * Atomically queue work on a connection. Bump @con reference to
2069 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002070 */
2071static void queue_con(struct ceph_connection *con)
2072{
2073 if (test_bit(DEAD, &con->state)) {
2074 dout("queue_con %p ignoring: DEAD\n",
2075 con);
2076 return;
2077 }
2078
2079 if (!con->ops->get(con)) {
2080 dout("queue_con %p ref count 0\n", con);
2081 return;
2082 }
2083
Tejun Heof363e45fd12011-01-03 14:49:46 +01002084 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002085 dout("queue_con %p - already queued\n", con);
2086 con->ops->put(con);
2087 } else {
2088 dout("queue_con %p\n", con);
2089 }
2090}
2091
2092/*
2093 * Do some work on a connection. Drop a connection ref when we're done.
2094 */
2095static void con_work(struct work_struct *work)
2096{
2097 struct ceph_connection *con = container_of(work, struct ceph_connection,
2098 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002099 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002100
Sage Weil9dd46582010-04-28 13:51:50 -07002101 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002102restart:
Sage Weil60bf8bf2011-03-04 12:24:28 -08002103 if (test_and_clear_bit(BACKOFF, &con->state)) {
2104 dout("con_work %p backing off\n", con);
2105 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2106 round_jiffies_relative(con->delay))) {
2107 dout("con_work %p backoff %lu\n", con, con->delay);
2108 mutex_unlock(&con->mutex);
2109 return;
2110 } else {
2111 con->ops->put(con);
2112 dout("con_work %p FAILED to back off %lu\n", con,
2113 con->delay);
2114 }
2115 }
Sage Weil9dd46582010-04-28 13:51:50 -07002116
Sage Weile00de342011-03-04 12:25:05 -08002117 if (test_bit(STANDBY, &con->state)) {
2118 dout("con_work %p STANDBY\n", con);
2119 goto done;
2120 }
Sage Weil31b80062009-10-06 11:31:13 -07002121 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2122 dout("con_work CLOSED\n");
2123 con_close_socket(con);
2124 goto done;
2125 }
2126 if (test_and_clear_bit(OPENING, &con->state)) {
2127 /* reopen w/ new peer */
2128 dout("con_work OPENING\n");
2129 con_close_socket(con);
2130 }
2131
Sage Weil0da5d702011-05-19 11:21:05 -07002132 if (test_and_clear_bit(SOCK_CLOSED, &con->state))
2133 goto fault;
2134
2135 ret = try_read(con);
2136 if (ret == -EAGAIN)
2137 goto restart;
2138 if (ret < 0)
2139 goto fault;
2140
2141 ret = try_write(con);
2142 if (ret == -EAGAIN)
2143 goto restart;
2144 if (ret < 0)
2145 goto fault;
Sage Weil31b80062009-10-06 11:31:13 -07002146
2147done:
Sage Weil9dd46582010-04-28 13:51:50 -07002148 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002149done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002150 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002151 return;
2152
2153fault:
2154 mutex_unlock(&con->mutex);
2155 ceph_fault(con); /* error/fault path */
2156 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002157}
2158
2159
2160/*
2161 * Generic error/fault handler. A retry mechanism is used with
2162 * exponential backoff
2163 */
2164static void ceph_fault(struct ceph_connection *con)
2165{
2166 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002167 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002168 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002169 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002170
2171 if (test_bit(LOSSYTX, &con->state)) {
2172 dout("fault on LOSSYTX channel\n");
2173 goto out;
2174 }
2175
Sage Weilec302642009-12-22 10:43:42 -08002176 mutex_lock(&con->mutex);
Sage Weil91e45ce32010-02-15 12:05:09 -08002177 if (test_bit(CLOSED, &con->state))
2178 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002179
Sage Weil31b80062009-10-06 11:31:13 -07002180 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002181
2182 if (con->in_msg) {
2183 ceph_msg_put(con->in_msg);
2184 con->in_msg = NULL;
2185 }
Sage Weil31b80062009-10-06 11:31:13 -07002186
Sage Weile80a52d2010-02-25 12:40:45 -08002187 /* Requeue anything that hasn't been acked */
2188 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002189
Sage Weile76661d2011-03-03 10:10:15 -08002190 /* If there are no messages queued or keepalive pending, place
2191 * the connection in a STANDBY state */
2192 if (list_empty(&con->out_queue) &&
2193 !test_bit(KEEPALIVE_PENDING, &con->state)) {
Sage Weile00de342011-03-04 12:25:05 -08002194 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
2195 clear_bit(WRITE_PENDING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07002196 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08002197 } else {
2198 /* retry after a delay. */
2199 if (con->delay == 0)
2200 con->delay = BASE_DELAY_INTERVAL;
2201 else if (con->delay < MAX_DELAY_INTERVAL)
2202 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002203 con->ops->get(con);
2204 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002205 round_jiffies_relative(con->delay))) {
2206 dout("fault queued %p delay %lu\n", con, con->delay);
2207 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002208 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002209 dout("fault failed to queue %p delay %lu, backoff\n",
2210 con, con->delay);
2211 /*
2212 * In many cases we see a socket state change
2213 * while con_work is running and end up
2214 * queuing (non-delayed) work, such that we
2215 * can't backoff with a delay. Set a flag so
2216 * that when con_work restarts we schedule the
2217 * delay then.
2218 */
2219 set_bit(BACKOFF, &con->state);
2220 }
Sage Weil31b80062009-10-06 11:31:13 -07002221 }
2222
Sage Weil91e45ce32010-02-15 12:05:09 -08002223out_unlock:
2224 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002225out:
Sage Weil161fd652010-02-25 12:38:57 -08002226 /*
2227 * in case we faulted due to authentication, invalidate our
2228 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002229 */
Sage Weil161fd652010-02-25 12:38:57 -08002230 if (con->auth_retry && con->ops->invalidate_authorizer) {
2231 dout("calling invalidate_authorizer()\n");
2232 con->ops->invalidate_authorizer(con);
2233 }
2234
Sage Weil31b80062009-10-06 11:31:13 -07002235 if (con->ops->fault)
2236 con->ops->fault(con);
2237}
2238
2239
2240
2241/*
2242 * create a new messenger instance
2243 */
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002244struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr,
2245 u32 supported_features,
2246 u32 required_features)
Sage Weil31b80062009-10-06 11:31:13 -07002247{
2248 struct ceph_messenger *msgr;
2249
2250 msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
2251 if (msgr == NULL)
2252 return ERR_PTR(-ENOMEM);
2253
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002254 msgr->supported_features = supported_features;
2255 msgr->required_features = required_features;
2256
Sage Weil31b80062009-10-06 11:31:13 -07002257 spin_lock_init(&msgr->global_seq_lock);
2258
Sage Weil31b80062009-10-06 11:31:13 -07002259 if (myaddr)
2260 msgr->inst.addr = *myaddr;
2261
2262 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002263 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002264 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002265 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002266
2267 dout("messenger_create %p\n", msgr);
2268 return msgr;
2269}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002270EXPORT_SYMBOL(ceph_messenger_create);
Sage Weil31b80062009-10-06 11:31:13 -07002271
2272void ceph_messenger_destroy(struct ceph_messenger *msgr)
2273{
2274 dout("destroy %p\n", msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002275 kfree(msgr);
2276 dout("destroyed messenger %p\n", msgr);
2277}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002278EXPORT_SYMBOL(ceph_messenger_destroy);
Sage Weil31b80062009-10-06 11:31:13 -07002279
Sage Weile00de342011-03-04 12:25:05 -08002280static void clear_standby(struct ceph_connection *con)
2281{
2282 /* come back from STANDBY? */
2283 if (test_and_clear_bit(STANDBY, &con->state)) {
2284 mutex_lock(&con->mutex);
2285 dout("clear_standby %p and ++connect_seq\n", con);
2286 con->connect_seq++;
2287 WARN_ON(test_bit(WRITE_PENDING, &con->state));
2288 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->state));
2289 mutex_unlock(&con->mutex);
2290 }
2291}
2292
Sage Weil31b80062009-10-06 11:31:13 -07002293/*
2294 * Queue up an outgoing message on the given connection.
2295 */
2296void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2297{
2298 if (test_bit(CLOSED, &con->state)) {
2299 dout("con_send %p closed, dropping %p\n", con, msg);
2300 ceph_msg_put(msg);
2301 return;
2302 }
2303
2304 /* set src+dst */
Sage Weildbad1852010-03-25 15:45:38 -07002305 msg->hdr.src = con->msgr->inst.name;
Sage Weil31b80062009-10-06 11:31:13 -07002306
Sage Weil3ca02ef2010-03-01 15:25:00 -08002307 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2308
Sage Weile84346b2010-05-11 21:20:38 -07002309 msg->needs_out_seq = true;
2310
Sage Weil31b80062009-10-06 11:31:13 -07002311 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08002312 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002313 BUG_ON(!list_empty(&msg->list_head));
2314 list_add_tail(&msg->list_head, &con->out_queue);
2315 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2316 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2317 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2318 le32_to_cpu(msg->hdr.front_len),
2319 le32_to_cpu(msg->hdr.middle_len),
2320 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002321 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002322
2323 /* if there wasn't anything waiting to send before, queue
2324 * new work */
Sage Weile00de342011-03-04 12:25:05 -08002325 clear_standby(con);
Sage Weil31b80062009-10-06 11:31:13 -07002326 if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2327 queue_con(con);
2328}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002329EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002330
2331/*
2332 * Revoke a message that was previously queued for send
2333 */
2334void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
2335{
Sage Weilec302642009-12-22 10:43:42 -08002336 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002337 if (!list_empty(&msg->list_head)) {
Sage Weiled98ada2010-07-05 12:15:14 -07002338 dout("con_revoke %p msg %p - was on queue\n", con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002339 list_del_init(&msg->list_head);
2340 ceph_msg_put(msg);
2341 msg->hdr.seq = 0;
Sage Weiled98ada2010-07-05 12:15:14 -07002342 }
2343 if (con->out_msg == msg) {
2344 dout("con_revoke %p msg %p - was sending\n", con, msg);
2345 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002346 if (con->out_kvec_is_msg) {
2347 con->out_skip = con->out_kvec_bytes;
2348 con->out_kvec_is_msg = false;
2349 }
Sage Weiled98ada2010-07-05 12:15:14 -07002350 ceph_msg_put(msg);
2351 msg->hdr.seq = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002352 }
Sage Weilec302642009-12-22 10:43:42 -08002353 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002354}
2355
2356/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002357 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002358 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002359void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002360{
2361 mutex_lock(&con->mutex);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002362 if (con->in_msg && con->in_msg == msg) {
2363 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2364 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002365 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2366
2367 /* skip rest of message */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002368 dout("con_revoke_pages %p msg %p revoked\n", con, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002369 con->in_base_pos = con->in_base_pos -
2370 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002371 front_len -
2372 middle_len -
2373 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002374 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002375 ceph_msg_put(con->in_msg);
2376 con->in_msg = NULL;
2377 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002378 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002379 } else {
2380 dout("con_revoke_pages %p msg %p pages %p no-op\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002381 con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002382 }
2383 mutex_unlock(&con->mutex);
2384}
2385
2386/*
Sage Weil31b80062009-10-06 11:31:13 -07002387 * Queue a keepalive byte to ensure the tcp connection is alive.
2388 */
2389void ceph_con_keepalive(struct ceph_connection *con)
2390{
Sage Weile00de342011-03-04 12:25:05 -08002391 dout("con_keepalive %p\n", con);
2392 clear_standby(con);
Sage Weil31b80062009-10-06 11:31:13 -07002393 if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
2394 test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2395 queue_con(con);
2396}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002397EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002398
2399
2400/*
2401 * construct a new message with given type, size
2402 * the new msg has a ref count of 1.
2403 */
Sage Weilb61c2762011-08-09 15:03:46 -07002404struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2405 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002406{
2407 struct ceph_msg *m;
2408
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002409 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002410 if (m == NULL)
2411 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002412 kref_init(&m->kref);
Sage Weil31b80062009-10-06 11:31:13 -07002413 INIT_LIST_HEAD(&m->list_head);
2414
Sage Weil45c6ceb2010-05-11 15:01:51 -07002415 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002416 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002417 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2418 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002419 m->hdr.front_len = cpu_to_le32(front_len);
2420 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002421 m->hdr.data_len = 0;
2422 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002423 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002424 m->footer.front_crc = 0;
2425 m->footer.middle_crc = 0;
2426 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002427 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002428 m->front_max = front_len;
2429 m->front_is_vmalloc = false;
2430 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002431 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002432 m->pool = NULL;
2433
Henry C Changca208922011-05-03 02:29:56 +00002434 /* middle */
2435 m->middle = NULL;
2436
2437 /* data */
2438 m->nr_pages = 0;
2439 m->page_alignment = 0;
2440 m->pages = NULL;
2441 m->pagelist = NULL;
2442 m->bio = NULL;
2443 m->bio_iter = NULL;
2444 m->bio_seg = 0;
2445 m->trail = NULL;
2446
Sage Weil31b80062009-10-06 11:31:13 -07002447 /* front */
2448 if (front_len) {
2449 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002450 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002451 PAGE_KERNEL);
2452 m->front_is_vmalloc = true;
2453 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002454 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002455 }
2456 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002457 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002458 front_len);
2459 goto out2;
2460 }
2461 } else {
2462 m->front.iov_base = NULL;
2463 }
2464 m->front.iov_len = front_len;
2465
Sage Weilbb257662010-04-01 16:07:23 -07002466 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002467 return m;
2468
2469out2:
2470 ceph_msg_put(m);
2471out:
Sage Weilb61c2762011-08-09 15:03:46 -07002472 if (!can_fail) {
2473 pr_err("msg_new can't create type %d front %d\n", type,
2474 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002475 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002476 } else {
2477 dout("msg_new can't create type %d front %d\n", type,
2478 front_len);
2479 }
Sage Weila79832f2010-04-01 16:06:19 -07002480 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002481}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002482EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002483
2484/*
Sage Weil31b80062009-10-06 11:31:13 -07002485 * Allocate "middle" portion of a message, if it is needed and wasn't
2486 * allocated by alloc_msg. This allows us to read a small fixed-size
2487 * per-type header in the front and then gracefully fail (i.e.,
2488 * propagate the error to the caller based on info in the front) when
2489 * the middle is too large.
2490 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002491static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002492{
2493 int type = le16_to_cpu(msg->hdr.type);
2494 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2495
2496 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2497 ceph_msg_type_name(type), middle_len);
2498 BUG_ON(!middle_len);
2499 BUG_ON(msg->middle);
2500
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002501 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002502 if (!msg->middle)
2503 return -ENOMEM;
2504 return 0;
2505}
2506
Yehuda Sadeh24504182010-01-08 13:58:34 -08002507/*
2508 * Generic message allocator, for incoming messages.
2509 */
2510static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
2511 struct ceph_msg_header *hdr,
2512 int *skip)
2513{
2514 int type = le16_to_cpu(hdr->type);
2515 int front_len = le32_to_cpu(hdr->front_len);
2516 int middle_len = le32_to_cpu(hdr->middle_len);
2517 struct ceph_msg *msg = NULL;
2518 int ret;
2519
2520 if (con->ops->alloc_msg) {
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002521 mutex_unlock(&con->mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002522 msg = con->ops->alloc_msg(con, hdr, skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002523 mutex_lock(&con->mutex);
Sage Weila79832f2010-04-01 16:06:19 -07002524 if (!msg || *skip)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002525 return NULL;
2526 }
2527 if (!msg) {
2528 *skip = 0;
Sage Weilb61c2762011-08-09 15:03:46 -07002529 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002530 if (!msg) {
2531 pr_err("unable to allocate msg type %d len %d\n",
2532 type, front_len);
Sage Weila79832f2010-04-01 16:06:19 -07002533 return NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002534 }
Sage Weilc5c6b192010-11-09 12:40:00 -08002535 msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002536 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002537 memcpy(&msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002538
Sage Weilbb257662010-04-01 16:07:23 -07002539 if (middle_len && !msg->middle) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002540 ret = ceph_alloc_middle(con, msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002541 if (ret < 0) {
2542 ceph_msg_put(msg);
Sage Weila79832f2010-04-01 16:06:19 -07002543 return NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002544 }
2545 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002546
Yehuda Sadeh24504182010-01-08 13:58:34 -08002547 return msg;
2548}
2549
Sage Weil31b80062009-10-06 11:31:13 -07002550
2551/*
2552 * Free a generically kmalloc'd message.
2553 */
2554void ceph_msg_kfree(struct ceph_msg *m)
2555{
2556 dout("msg_kfree %p\n", m);
2557 if (m->front_is_vmalloc)
2558 vfree(m->front.iov_base);
2559 else
2560 kfree(m->front.iov_base);
2561 kfree(m);
2562}
2563
2564/*
2565 * Drop a msg ref. Destroy as needed.
2566 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002567void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002568{
Sage Weilc2e552e2009-12-07 15:55:05 -08002569 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002570
Sage Weilc2e552e2009-12-07 15:55:05 -08002571 dout("ceph_msg_put last one on %p\n", m);
2572 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002573
Sage Weilc2e552e2009-12-07 15:55:05 -08002574 /* drop middle, data, if any */
2575 if (m->middle) {
2576 ceph_buffer_put(m->middle);
2577 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002578 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002579 m->nr_pages = 0;
2580 m->pages = NULL;
2581
Sage Weil58bb3b32009-12-23 12:12:31 -08002582 if (m->pagelist) {
2583 ceph_pagelist_release(m->pagelist);
2584 kfree(m->pagelist);
2585 m->pagelist = NULL;
2586 }
2587
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002588 m->trail = NULL;
2589
Sage Weilc2e552e2009-12-07 15:55:05 -08002590 if (m->pool)
2591 ceph_msgpool_put(m->pool, m);
2592 else
2593 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002594}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002595EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002596
2597void ceph_msg_dump(struct ceph_msg *msg)
2598{
2599 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2600 msg->front_max, msg->nr_pages);
2601 print_hex_dump(KERN_DEBUG, "header: ",
2602 DUMP_PREFIX_OFFSET, 16, 1,
2603 &msg->hdr, sizeof(msg->hdr), true);
2604 print_hex_dump(KERN_DEBUG, " front: ",
2605 DUMP_PREFIX_OFFSET, 16, 1,
2606 msg->front.iov_base, msg->front.iov_len, true);
2607 if (msg->middle)
2608 print_hex_dump(KERN_DEBUG, "middle: ",
2609 DUMP_PREFIX_OFFSET, 16, 1,
2610 msg->middle->vec.iov_base,
2611 msg->middle->vec.iov_len, true);
2612 print_hex_dump(KERN_DEBUG, "footer: ",
2613 DUMP_PREFIX_OFFSET, 16, 1,
2614 &msg->footer, sizeof(msg->footer), true);
2615}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002616EXPORT_SYMBOL(ceph_msg_dump);