blob: 500207bad5d6317923fee86c7bfbb5e3744ee40e [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil31b80062009-10-06 11:31:13 -07002
3#include <linux/crc32c.h>
4#include <linux/ctype.h>
5#include <linux/highmem.h>
6#include <linux/inet.h>
7#include <linux/kthread.h>
8#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Sage Weil31b80062009-10-06 11:31:13 -070010#include <linux/socket.h>
11#include <linux/string.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070012#include <linux/bio.h>
13#include <linux/blkdev.h>
Noah Watkinsee3b56f2011-09-23 11:48:42 -070014#include <linux/dns_resolver.h>
Sage Weil31b80062009-10-06 11:31:13 -070015#include <net/tcp.h>
16
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070017#include <linux/ceph/libceph.h>
18#include <linux/ceph/messenger.h>
19#include <linux/ceph/decode.h>
20#include <linux/ceph/pagelist.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040021#include <linux/export.h>
Sage Weil31b80062009-10-06 11:31:13 -070022
23/*
24 * Ceph uses the messenger to exchange ceph_msg messages with other
25 * hosts in the system. The messenger provides ordered and reliable
26 * delivery. We tolerate TCP disconnects by reconnecting (with
27 * exponential backoff) in the case of a fault (disconnection, bad
28 * crc, protocol error). Acks allow sent messages to be discarded by
29 * the sender.
30 */
31
Alex Elderce2c8902012-05-22 22:15:49 -050032/* State values for ceph_connection->sock_state; NEW is assumed to be 0 */
33
34#define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
35#define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
36#define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
37#define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
38#define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
39
Sage Weil31b80062009-10-06 11:31:13 -070040/* static tag bytes (protocol control messages) */
41static char tag_msg = CEPH_MSGR_TAG_MSG;
42static char tag_ack = CEPH_MSGR_TAG_ACK;
43static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
44
Sage Weila6a53492010-04-13 14:07:07 -070045#ifdef CONFIG_LOCKDEP
46static struct lock_class_key socket_class;
47#endif
48
Alex Elder84495f42012-02-15 07:43:55 -060049/*
50 * When skipping (ignoring) a block of input we read it into a "skip
51 * buffer," which is this many bytes in size.
52 */
53#define SKIP_BUF_SIZE 1024
Sage Weil31b80062009-10-06 11:31:13 -070054
55static void queue_con(struct ceph_connection *con);
56static void con_work(struct work_struct *);
57static void ceph_fault(struct ceph_connection *con);
58
Sage Weil31b80062009-10-06 11:31:13 -070059/*
Alex Elderf64a9312012-01-23 15:49:27 -060060 * Nicely render a sockaddr as a string. An array of formatted
61 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -070062 */
Alex Elderf64a9312012-01-23 15:49:27 -060063#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
64#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
65#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
66#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
67
68static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
69static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -070070
Alex Elder57666512012-01-23 15:49:27 -060071static struct page *zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -060072
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070073const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -070074{
75 int i;
76 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -060077 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
78 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -070079
Alex Elderf64a9312012-01-23 15:49:27 -060080 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -070081 s = addr_str[i];
82
83 switch (ss->ss_family) {
84 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -060085 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
86 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -070087 break;
88
89 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -060090 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
91 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -070092 break;
93
94 default:
Alex Elderd3002b92012-02-14 14:05:33 -060095 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
96 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -070097 }
98
99 return s;
100}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700101EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700102
Sage Weil63f2d212009-11-03 15:17:56 -0800103static void encode_my_addr(struct ceph_messenger *msgr)
104{
105 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
106 ceph_encode_addr(&msgr->my_enc_addr);
107}
108
Sage Weil31b80062009-10-06 11:31:13 -0700109/*
110 * work queue for all reading and writing to/from the socket.
111 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600112static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700113
Alex Elder6173d1f2012-02-14 14:05:33 -0600114void _ceph_msgr_exit(void)
115{
Alex Elderd3002b92012-02-14 14:05:33 -0600116 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600117 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600118 ceph_msgr_wq = NULL;
119 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600120
Alex Elder6173d1f2012-02-14 14:05:33 -0600121 BUG_ON(zero_page == NULL);
122 kunmap(zero_page);
123 page_cache_release(zero_page);
124 zero_page = NULL;
125}
126
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700127int ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700128{
Alex Elder57666512012-01-23 15:49:27 -0600129 BUG_ON(zero_page != NULL);
130 zero_page = ZERO_PAGE(0);
131 page_cache_get(zero_page);
132
Tejun Heof363e45fd12011-01-03 14:49:46 +0100133 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600134 if (ceph_msgr_wq)
135 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600136
Alex Elder6173d1f2012-02-14 14:05:33 -0600137 pr_err("msgr_init failed to create workqueue\n");
138 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600139
Alex Elder6173d1f2012-02-14 14:05:33 -0600140 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700141}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700142EXPORT_SYMBOL(ceph_msgr_init);
Sage Weil31b80062009-10-06 11:31:13 -0700143
144void ceph_msgr_exit(void)
145{
Alex Elder57666512012-01-23 15:49:27 -0600146 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600147
Alex Elder6173d1f2012-02-14 14:05:33 -0600148 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700149}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700150EXPORT_SYMBOL(ceph_msgr_exit);
Sage Weil31b80062009-10-06 11:31:13 -0700151
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700152void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700153{
154 flush_workqueue(ceph_msgr_wq);
155}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700156EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700157
Alex Elderce2c8902012-05-22 22:15:49 -0500158/* Connection socket state transition functions */
159
160static void con_sock_state_init(struct ceph_connection *con)
161{
162 int old_state;
163
164 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
165 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
166 printk("%s: unexpected old state %d\n", __func__, old_state);
167}
168
169static void con_sock_state_connecting(struct ceph_connection *con)
170{
171 int old_state;
172
173 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
174 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
175 printk("%s: unexpected old state %d\n", __func__, old_state);
176}
177
178static void con_sock_state_connected(struct ceph_connection *con)
179{
180 int old_state;
181
182 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
183 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
184 printk("%s: unexpected old state %d\n", __func__, old_state);
185}
186
187static void con_sock_state_closing(struct ceph_connection *con)
188{
189 int old_state;
190
191 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
192 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
193 old_state != CON_SOCK_STATE_CONNECTED &&
194 old_state != CON_SOCK_STATE_CLOSING))
195 printk("%s: unexpected old state %d\n", __func__, old_state);
196}
197
198static void con_sock_state_closed(struct ceph_connection *con)
199{
200 int old_state;
201
202 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
203 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
204 old_state != CON_SOCK_STATE_CLOSING))
205 printk("%s: unexpected old state %d\n", __func__, old_state);
206}
Sage Weila922d382010-05-29 09:41:23 -0700207
Sage Weil31b80062009-10-06 11:31:13 -0700208/*
209 * socket callback functions
210 */
211
212/* data available on socket, or listen socket received a connect */
Alex Elder327800b2012-05-22 11:41:43 -0500213static void ceph_sock_data_ready(struct sock *sk, int count_unused)
Sage Weil31b80062009-10-06 11:31:13 -0700214{
Alex Elderbd406142012-01-23 15:49:27 -0600215 struct ceph_connection *con = sk->sk_user_data;
216
Sage Weil31b80062009-10-06 11:31:13 -0700217 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500218 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700219 con, con->state);
220 queue_con(con);
221 }
222}
223
224/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500225static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700226{
Alex Elderd3002b92012-02-14 14:05:33 -0600227 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700228
Jim Schutt182fac22012-02-29 08:30:58 -0700229 /* only queue to workqueue if there is data we want to write,
230 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500231 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700232 * doesn't get called again until try_write() fills the socket
233 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
234 * and net/core/stream.c:sk_stream_write_space().
235 */
Alex Elder928443c2012-05-22 11:41:43 -0500236 if (test_bit(WRITE_PENDING, &con->flags)) {
Jim Schutt182fac22012-02-29 08:30:58 -0700237 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500238 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700239 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
240 queue_con(con);
241 }
Sage Weil31b80062009-10-06 11:31:13 -0700242 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500243 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700244 }
Sage Weil31b80062009-10-06 11:31:13 -0700245}
246
247/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500248static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700249{
Alex Elderbd406142012-01-23 15:49:27 -0600250 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700251
Alex Elder327800b2012-05-22 11:41:43 -0500252 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700253 con, con->state, sk->sk_state);
254
255 if (test_bit(CLOSED, &con->state))
256 return;
257
258 switch (sk->sk_state) {
259 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500260 dout("%s TCP_CLOSE\n", __func__);
Sage Weil31b80062009-10-06 11:31:13 -0700261 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500262 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500263 con_sock_state_closing(con);
Alex Elderd65c9e02012-06-20 21:53:53 -0500264 set_bit(SOCK_CLOSED, &con->flags);
265 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -0700266 break;
267 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500268 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500269 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700270 queue_con(con);
271 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600272 default: /* Everything else is uninteresting */
273 break;
Sage Weil31b80062009-10-06 11:31:13 -0700274 }
275}
276
277/*
278 * set up socket callbacks
279 */
280static void set_sock_callbacks(struct socket *sock,
281 struct ceph_connection *con)
282{
283 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600284 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500285 sk->sk_data_ready = ceph_sock_data_ready;
286 sk->sk_write_space = ceph_sock_write_space;
287 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700288}
289
290
291/*
292 * socket helpers
293 */
294
295/*
296 * initiate connection to a remote socket.
297 */
Alex Elder41617d02012-02-14 14:05:33 -0600298static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700299{
Sage Weilf91d3472010-07-01 15:18:31 -0700300 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700301 struct socket *sock;
302 int ret;
303
304 BUG_ON(con->sock);
Sage Weilf91d3472010-07-01 15:18:31 -0700305 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
306 IPPROTO_TCP, &sock);
Sage Weil31b80062009-10-06 11:31:13 -0700307 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600308 return ret;
Sage Weil31b80062009-10-06 11:31:13 -0700309 sock->sk->sk_allocation = GFP_NOFS;
310
Sage Weila6a53492010-04-13 14:07:07 -0700311#ifdef CONFIG_LOCKDEP
312 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
313#endif
314
Sage Weil31b80062009-10-06 11:31:13 -0700315 set_sock_callbacks(sock, con);
316
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700317 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700318
Sage Weil89a86be2012-06-09 14:19:21 -0700319 con_sock_state_connecting(con);
Sage Weilf91d3472010-07-01 15:18:31 -0700320 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
321 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700322 if (ret == -EINPROGRESS) {
323 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700324 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700325 sock->sk->sk_state);
Alex Eldera5bc3122012-01-23 15:49:27 -0600326 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700327 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700328 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700329 sock_release(sock);
Sage Weil31b80062009-10-06 11:31:13 -0700330 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -0700331
Alex Elder41617d02012-02-14 14:05:33 -0600332 return ret;
Alex Eldera5bc3122012-01-23 15:49:27 -0600333 }
334 con->sock = sock;
Alex Elder41617d02012-02-14 14:05:33 -0600335 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700336}
337
338static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
339{
340 struct kvec iov = {buf, len};
341 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800342 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700343
Sage Weil98bdb0a2011-01-25 08:17:48 -0800344 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
345 if (r == -EAGAIN)
346 r = 0;
347 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700348}
349
350/*
351 * write something. @more is true if caller will be sending more data
352 * shortly.
353 */
354static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
355 size_t kvlen, size_t len, int more)
356{
357 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800358 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700359
360 if (more)
361 msg.msg_flags |= MSG_MORE;
362 else
363 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
364
Sage Weil42961d22011-01-25 08:19:34 -0800365 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
366 if (r == -EAGAIN)
367 r = 0;
368 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700369}
370
Alex Elder31739132012-03-07 11:40:08 -0600371static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
372 int offset, size_t size, int more)
373{
374 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
375 int ret;
376
377 ret = kernel_sendpage(sock, page, offset, size, flags);
378 if (ret == -EAGAIN)
379 ret = 0;
380
381 return ret;
382}
383
Sage Weil31b80062009-10-06 11:31:13 -0700384
385/*
386 * Shutdown/close the socket for the given connection.
387 */
388static int con_close_socket(struct ceph_connection *con)
389{
390 int rc;
391
392 dout("con_close_socket on %p sock %p\n", con, con->sock);
393 if (!con->sock)
394 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700395 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
396 sock_release(con->sock);
397 con->sock = NULL;
Alex Elder456ea462012-06-20 21:53:53 -0500398
399 /*
400 * Forcibly clear the SOCK_CLOSE flag. It gets set
401 * independent of the connection mutex, and we could have
402 * received a socket close event before we had the chance to
403 * shut the socket down.
404 */
Alex Eldera8d00e32012-06-20 21:53:53 -0500405 clear_bit(SOCK_CLOSED, &con->flags);
Alex Elderce2c8902012-05-22 22:15:49 -0500406 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700407 return rc;
408}
409
410/*
411 * Reset a connection. Discard all incoming and outgoing messages
412 * and clear *_seq state.
413 */
414static void ceph_msg_remove(struct ceph_msg *msg)
415{
416 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500417 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -0700418 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -0500419 msg->con = NULL;
420
Sage Weil31b80062009-10-06 11:31:13 -0700421 ceph_msg_put(msg);
422}
423static void ceph_msg_remove_list(struct list_head *head)
424{
425 while (!list_empty(head)) {
426 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
427 list_head);
428 ceph_msg_remove(msg);
429 }
430}
431
432static void reset_connection(struct ceph_connection *con)
433{
434 /* reset connection, out_queue, msg_ and connect_seq */
435 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700436 ceph_msg_remove_list(&con->out_queue);
437 ceph_msg_remove_list(&con->out_sent);
438
Sage Weilcf3e5c42009-12-11 09:48:05 -0800439 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500440 BUG_ON(con->in_msg->con != con);
441 con->in_msg->con = NULL;
Sage Weilcf3e5c42009-12-11 09:48:05 -0800442 ceph_msg_put(con->in_msg);
443 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -0700444 con->ops->put(con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800445 }
446
Sage Weil31b80062009-10-06 11:31:13 -0700447 con->connect_seq = 0;
448 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800449 if (con->out_msg) {
450 ceph_msg_put(con->out_msg);
451 con->out_msg = NULL;
452 }
Sage Weil31b80062009-10-06 11:31:13 -0700453 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700454 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700455}
456
457/*
458 * mark a peer down. drop any open connections.
459 */
460void ceph_con_close(struct ceph_connection *con)
461{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700462 dout("con_close %p peer %s\n", con,
463 ceph_pr_addr(&con->peer_addr.in_addr));
Alex Eldera5988c42012-05-29 11:04:58 -0500464 clear_bit(NEGOTIATING, &con->state);
Alex Elderbb9e6bb2012-06-20 21:53:53 -0500465 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700466 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Alex Eldera5988c42012-05-29 11:04:58 -0500467 set_bit(CLOSED, &con->state);
468
Alex Elder928443c2012-05-22 11:41:43 -0500469 clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */
470 clear_bit(KEEPALIVE_PENDING, &con->flags);
471 clear_bit(WRITE_PENDING, &con->flags);
Alex Eldera5988c42012-05-29 11:04:58 -0500472
Sage Weilec302642009-12-22 10:43:42 -0800473 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700474 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700475 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800476 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800477 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700478 queue_con(con);
479}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700480EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700481
482/*
Sage Weil31b80062009-10-06 11:31:13 -0700483 * Reopen a closed connection, with a new peer address.
484 */
485void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
486{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700487 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700488 set_bit(OPENING, &con->state);
Alex Eldera5988c42012-05-29 11:04:58 -0500489 WARN_ON(!test_and_clear_bit(CLOSED, &con->state));
490
Sage Weil31b80062009-10-06 11:31:13 -0700491 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800492 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700493 queue_con(con);
494}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700495EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700496
497/*
Sage Weil87b315a2010-03-22 14:51:18 -0700498 * return true if this connection ever successfully opened
499 */
500bool ceph_con_opened(struct ceph_connection *con)
501{
502 return con->connect_seq > 0;
503}
504
505/*
Sage Weil31b80062009-10-06 11:31:13 -0700506 * initialize a new connection.
507 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500508void ceph_con_init(struct ceph_connection *con, void *private,
509 const struct ceph_connection_operations *ops,
510 struct ceph_messenger *msgr, __u8 entity_type, __u64 entity_num)
Sage Weil31b80062009-10-06 11:31:13 -0700511{
512 dout("con_init %p\n", con);
513 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500514 con->private = private;
515 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700516 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500517
518 con_sock_state_init(con);
519
Alex Elder1bfd89f2012-05-26 23:26:43 -0500520 con->peer_name.type = (__u8) entity_type;
521 con->peer_name.num = cpu_to_le64(entity_num);
522
Sage Weilec302642009-12-22 10:43:42 -0800523 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700524 INIT_LIST_HEAD(&con->out_queue);
525 INIT_LIST_HEAD(&con->out_sent);
526 INIT_DELAYED_WORK(&con->work, con_work);
Alex Eldera5988c42012-05-29 11:04:58 -0500527
528 set_bit(CLOSED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700529}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700530EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700531
532
533/*
534 * We maintain a global counter to order connection attempts. Get
535 * a unique seq greater than @gt.
536 */
537static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
538{
539 u32 ret;
540
541 spin_lock(&msgr->global_seq_lock);
542 if (msgr->global_seq < gt)
543 msgr->global_seq = gt;
544 ret = ++msgr->global_seq;
545 spin_unlock(&msgr->global_seq_lock);
546 return ret;
547}
548
Alex Eldere2200422012-05-23 14:35:23 -0500549static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600550{
551 con->out_kvec_left = 0;
552 con->out_kvec_bytes = 0;
553 con->out_kvec_cur = &con->out_kvec[0];
554}
555
Alex Eldere2200422012-05-23 14:35:23 -0500556static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600557 size_t size, void *data)
558{
559 int index;
560
561 index = con->out_kvec_left;
562 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
563
564 con->out_kvec[index].iov_len = size;
565 con->out_kvec[index].iov_base = data;
566 con->out_kvec_left++;
567 con->out_kvec_bytes += size;
568}
Sage Weil31b80062009-10-06 11:31:13 -0700569
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500570#ifdef CONFIG_BLOCK
571static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
572{
573 if (!bio) {
574 *iter = NULL;
575 *seg = 0;
576 return;
577 }
578 *iter = bio;
579 *seg = bio->bi_idx;
580}
581
582static void iter_bio_next(struct bio **bio_iter, int *seg)
583{
584 if (*bio_iter == NULL)
585 return;
586
587 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
588
589 (*seg)++;
590 if (*seg == (*bio_iter)->bi_vcnt)
591 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
592}
593#endif
594
Alex Elder739c9052012-06-11 14:57:13 -0500595static void prepare_write_message_data(struct ceph_connection *con)
596{
597 struct ceph_msg *msg = con->out_msg;
598
599 BUG_ON(!msg);
600 BUG_ON(!msg->hdr.data_len);
601
602 /* initialize page iterator */
603 con->out_msg_pos.page = 0;
604 if (msg->pages)
605 con->out_msg_pos.page_pos = msg->page_alignment;
606 else
607 con->out_msg_pos.page_pos = 0;
Alex Elder572c5882012-06-11 14:57:13 -0500608#ifdef CONFIG_BLOCK
Alex Elderabdaa6a2012-06-11 14:57:13 -0500609 if (msg->bio)
Alex Elder572c5882012-06-11 14:57:13 -0500610 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
611#endif
Alex Elder739c9052012-06-11 14:57:13 -0500612 con->out_msg_pos.data_pos = 0;
613 con->out_msg_pos.did_page_crc = false;
614 con->out_more = 1; /* data + footer will follow */
615}
616
Sage Weil31b80062009-10-06 11:31:13 -0700617/*
618 * Prepare footer for currently outgoing message, and finish things
619 * off. Assumes out_kvec* are already valid.. we just add on to the end.
620 */
Alex Elder859eb792012-02-14 14:05:33 -0600621static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700622{
623 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600624 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700625
Alex Elderfd154f32012-06-11 14:57:13 -0500626 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
627
Sage Weil31b80062009-10-06 11:31:13 -0700628 dout("prepare_write_message_footer %p\n", con);
629 con->out_kvec_is_msg = true;
630 con->out_kvec[v].iov_base = &m->footer;
631 con->out_kvec[v].iov_len = sizeof(m->footer);
632 con->out_kvec_bytes += sizeof(m->footer);
633 con->out_kvec_left++;
634 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800635 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700636}
637
638/*
639 * Prepare headers for the next outgoing message.
640 */
641static void prepare_write_message(struct ceph_connection *con)
642{
643 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600644 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700645
Alex Eldere2200422012-05-23 14:35:23 -0500646 con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700647 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800648 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700649
650 /* Sneak an ack in there first? If we can get it into the same
651 * TCP packet that's a good thing. */
652 if (con->in_seq > con->in_seq_acked) {
653 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -0500654 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700655 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500656 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600657 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700658 }
659
Alex Elder38941f82012-06-01 14:56:43 -0500660 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -0600661 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800662 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -0500663 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -0700664
665 /* put message on sent list */
666 ceph_msg_get(m);
667 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700668
Sage Weile84346b2010-05-11 21:20:38 -0700669 /*
670 * only assign outgoing seq # if we haven't sent this message
671 * yet. if it is requeued, resend with it's original seq.
672 */
673 if (m->needs_out_seq) {
674 m->hdr.seq = cpu_to_le64(++con->out_seq);
675 m->needs_out_seq = false;
676 }
Sage Weil31b80062009-10-06 11:31:13 -0700677
678 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
679 m, con->out_seq, le16_to_cpu(m->hdr.type),
680 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
681 le32_to_cpu(m->hdr.data_len),
682 m->nr_pages);
683 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
684
685 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -0500686 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
687 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
688 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600689
Sage Weil31b80062009-10-06 11:31:13 -0700690 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -0500691 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600692 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700693
694 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600695 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
696 con->out_msg->hdr.crc = cpu_to_le32(crc);
Alex Elderfd154f32012-06-11 14:57:13 -0500697 con->out_msg->footer.flags = 0;
Alex Eldera9a0c512012-02-15 07:43:54 -0600698
699 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
700 con->out_msg->footer.front_crc = cpu_to_le32(crc);
701 if (m->middle) {
702 crc = crc32c(0, m->middle->vec.iov_base,
703 m->middle->vec.iov_len);
704 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
705 } else
Sage Weil31b80062009-10-06 11:31:13 -0700706 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -0500707 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700708 le32_to_cpu(con->out_msg->footer.front_crc),
709 le32_to_cpu(con->out_msg->footer.middle_crc));
710
711 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -0500712 con->out_msg->footer.data_crc = 0;
713 if (m->hdr.data_len)
714 prepare_write_message_data(con);
715 else
Sage Weil31b80062009-10-06 11:31:13 -0700716 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600717 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700718
Alex Elder928443c2012-05-22 11:41:43 -0500719 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700720}
721
722/*
723 * Prepare an ack.
724 */
725static void prepare_write_ack(struct ceph_connection *con)
726{
727 dout("prepare_write_ack %p %llu -> %llu\n", con,
728 con->in_seq_acked, con->in_seq);
729 con->in_seq_acked = con->in_seq;
730
Alex Eldere2200422012-05-23 14:35:23 -0500731 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600732
Alex Eldere2200422012-05-23 14:35:23 -0500733 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600734
Sage Weil31b80062009-10-06 11:31:13 -0700735 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500736 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600737 &con->out_temp_ack);
738
Sage Weil31b80062009-10-06 11:31:13 -0700739 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elder928443c2012-05-22 11:41:43 -0500740 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700741}
742
743/*
744 * Prepare to write keepalive byte.
745 */
746static void prepare_write_keepalive(struct ceph_connection *con)
747{
748 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -0500749 con_out_kvec_reset(con);
750 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Alex Elder928443c2012-05-22 11:41:43 -0500751 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700752}
753
754/*
755 * Connection negotiation.
756 */
757
Alex Elderdac1e712012-05-16 15:16:39 -0500758static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
759 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800760{
Alex Eldera3530df2012-05-16 15:16:39 -0500761 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -0500762
763 if (!con->ops->get_authorizer) {
764 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
765 con->out_connect.authorizer_len = 0;
766
Alex Elder729796b2012-05-16 15:16:39 -0500767 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -0500768 }
769
770 /* Can't hold the mutex while getting authorizer */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800771
Sage Weilec302642009-12-22 10:43:42 -0800772 mutex_unlock(&con->mutex);
Alex Elderb1c6b982012-05-16 15:16:38 -0500773
Alex Elderdac1e712012-05-16 15:16:39 -0500774 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Elder8f43fb52012-05-16 15:16:39 -0500775
Sage Weilec302642009-12-22 10:43:42 -0800776 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800777
Alex Eldera3530df2012-05-16 15:16:39 -0500778 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -0500779 return auth;
Alex Elder928443c2012-05-22 11:41:43 -0500780 if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags))
Alex Elder729796b2012-05-16 15:16:39 -0500781 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700782
Alex Elder8f43fb52012-05-16 15:16:39 -0500783 con->auth_reply_buf = auth->authorizer_reply_buf;
784 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
785
Alex Elder859eb792012-02-14 14:05:33 -0600786
Alex Elder729796b2012-05-16 15:16:39 -0500787 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800788}
789
Sage Weil31b80062009-10-06 11:31:13 -0700790/*
791 * We connected to a peer and are saying hello.
792 */
Alex Eldere825a662012-05-16 15:16:38 -0500793static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700794{
Alex Eldere2200422012-05-23 14:35:23 -0500795 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
796 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -0500797 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800798
Sage Weileed0ef22009-11-10 14:34:36 -0800799 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500800 set_bit(WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800801}
802
Alex Eldere825a662012-05-16 15:16:38 -0500803static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800804{
Eric Dumazet95c96172012-04-15 05:58:06 +0000805 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -0700806 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -0500807 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -0500808 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700809
810 switch (con->peer_name.type) {
811 case CEPH_ENTITY_TYPE_MON:
812 proto = CEPH_MONC_PROTOCOL;
813 break;
814 case CEPH_ENTITY_TYPE_OSD:
815 proto = CEPH_OSDC_PROTOCOL;
816 break;
817 case CEPH_ENTITY_TYPE_MDS:
818 proto = CEPH_MDSC_PROTOCOL;
819 break;
820 default:
821 BUG();
822 }
823
824 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
825 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800826
Alex Eldere825a662012-05-16 15:16:38 -0500827 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700828 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
829 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
830 con->out_connect.global_seq = cpu_to_le32(global_seq);
831 con->out_connect.protocol_version = cpu_to_le32(proto);
832 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700833
Alex Elderdac1e712012-05-16 15:16:39 -0500834 auth_proto = CEPH_AUTH_UNKNOWN;
835 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -0500836 if (IS_ERR(auth))
837 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -0500838
Alex Elderdac1e712012-05-16 15:16:39 -0500839 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -0500840 con->out_connect.authorizer_len = auth ?
841 cpu_to_le32(auth->authorizer_buf_len) : 0;
842
Alex Eldere2200422012-05-23 14:35:23 -0500843 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -0500844 &con->out_connect);
845 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -0500846 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -0500847 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600848
Sage Weil31b80062009-10-06 11:31:13 -0700849 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500850 set_bit(WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800851
Alex Eldere10c7582012-05-16 15:16:38 -0500852 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700853}
854
Sage Weil31b80062009-10-06 11:31:13 -0700855/*
856 * write as much of pending kvecs to the socket as we can.
857 * 1 -> done
858 * 0 -> socket full, but more to do
859 * <0 -> error
860 */
861static int write_partial_kvec(struct ceph_connection *con)
862{
863 int ret;
864
865 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
866 while (con->out_kvec_bytes > 0) {
867 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
868 con->out_kvec_left, con->out_kvec_bytes,
869 con->out_more);
870 if (ret <= 0)
871 goto out;
872 con->out_kvec_bytes -= ret;
873 if (con->out_kvec_bytes == 0)
874 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600875
876 /* account for full iov entries consumed */
877 while (ret >= con->out_kvec_cur->iov_len) {
878 BUG_ON(!con->out_kvec_left);
879 ret -= con->out_kvec_cur->iov_len;
880 con->out_kvec_cur++;
881 con->out_kvec_left--;
882 }
883 /* and for a partially-consumed entry */
884 if (ret) {
885 con->out_kvec_cur->iov_len -= ret;
886 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700887 }
888 }
889 con->out_kvec_left = 0;
890 con->out_kvec_is_msg = false;
891 ret = 1;
892out:
893 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
894 con->out_kvec_bytes, con->out_kvec_left, ret);
895 return ret; /* done! */
896}
897
Alex Elder84ca8fc2012-06-11 14:57:13 -0500898static void out_msg_pos_next(struct ceph_connection *con, struct page *page,
899 size_t len, size_t sent, bool in_trail)
900{
901 struct ceph_msg *msg = con->out_msg;
902
903 BUG_ON(!msg);
904 BUG_ON(!sent);
905
906 con->out_msg_pos.data_pos += sent;
907 con->out_msg_pos.page_pos += sent;
908 if (sent == len) {
909 con->out_msg_pos.page_pos = 0;
910 con->out_msg_pos.page++;
911 con->out_msg_pos.did_page_crc = false;
912 if (in_trail)
913 list_move_tail(&page->lru,
914 &msg->trail->head);
915 else if (msg->pagelist)
916 list_move_tail(&page->lru,
917 &msg->pagelist->head);
918#ifdef CONFIG_BLOCK
919 else if (msg->bio)
920 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
921#endif
922 }
923}
924
Sage Weil31b80062009-10-06 11:31:13 -0700925/*
926 * Write as much message data payload as we can. If we finish, queue
927 * up the footer.
928 * 1 -> done, footer is now queued in out_kvec[].
929 * 0 -> socket full, but more to do
930 * <0 -> error
931 */
932static int write_partial_msg_pages(struct ceph_connection *con)
933{
934 struct ceph_msg *msg = con->out_msg;
Eric Dumazet95c96172012-04-15 05:58:06 +0000935 unsigned int data_len = le32_to_cpu(msg->hdr.data_len);
Sage Weil31b80062009-10-06 11:31:13 -0700936 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600937 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700938 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700939 int total_max_write;
Alex Elder84ca8fc2012-06-11 14:57:13 -0500940 bool in_trail = false;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700941 size_t trail_len = (msg->trail ? msg->trail->length : 0);
Sage Weil31b80062009-10-06 11:31:13 -0700942
943 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
Alex Elder84ca8fc2012-06-11 14:57:13 -0500944 con, msg, con->out_msg_pos.page, msg->nr_pages,
Sage Weil31b80062009-10-06 11:31:13 -0700945 con->out_msg_pos.page_pos);
946
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700947 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -0700948 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700949 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -0600950 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700951
952 total_max_write = data_len - trail_len -
953 con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -0700954
955 /*
956 * if we are calculating the data crc (the default), we need
957 * to map the page. if our pages[] has been revoked, use the
958 * zero page.
959 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700960
961 /* have we reached the trail part of the data? */
962 if (con->out_msg_pos.data_pos >= data_len - trail_len) {
Alex Elder84ca8fc2012-06-11 14:57:13 -0500963 in_trail = true;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700964
965 total_max_write = data_len - con->out_msg_pos.data_pos;
966
967 page = list_first_entry(&msg->trail->head,
968 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700969 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -0700970 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -0800971 } else if (msg->pagelist) {
972 page = list_first_entry(&msg->pagelist->head,
973 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700974#ifdef CONFIG_BLOCK
975 } else if (msg->bio) {
976 struct bio_vec *bv;
977
978 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
979 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -0600980 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700981 max_write = bv->bv_len;
982#endif
Sage Weil31b80062009-10-06 11:31:13 -0700983 } else {
Alex Elder57666512012-01-23 15:49:27 -0600984 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -0700985 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700986 len = min_t(int, max_write - con->out_msg_pos.page_pos,
987 total_max_write);
988
Alex Elder37675b02012-03-07 11:40:08 -0600989 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -0600990 void *base;
Alex Eldera9a0c512012-02-15 07:43:54 -0600991 u32 crc;
Alex Elder84ca8fc2012-06-11 14:57:13 -0500992 u32 tmpcrc = le32_to_cpu(msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -0600993 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -0700994
Alex Elder8d63e312012-03-07 11:40:08 -0600995 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -0700996 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -0600997 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Eldera9a0c512012-02-15 07:43:54 -0600998 crc = crc32c(tmpcrc, base, len);
Alex Elder84ca8fc2012-06-11 14:57:13 -0500999 msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -06001000 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -07001001 }
Alex Eldere36b13c2012-03-07 11:40:08 -06001002 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -06001003 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -06001004 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001005
Alex Elder0cdf9e62012-03-07 11:40:08 -06001006 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -07001007 kunmap(page);
1008
1009 if (ret <= 0)
1010 goto out;
1011
Alex Elder84ca8fc2012-06-11 14:57:13 -05001012 out_msg_pos_next(con, page, len, (size_t) ret, in_trail);
Sage Weil31b80062009-10-06 11:31:13 -07001013 }
1014
1015 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1016
1017 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001018 if (!do_datacrc)
Alex Elder84ca8fc2012-06-11 14:57:13 -05001019 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001020 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001021 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001022 ret = 1;
1023out:
1024 return ret;
1025}
1026
1027/*
1028 * write some zeros
1029 */
1030static int write_partial_skip(struct ceph_connection *con)
1031{
1032 int ret;
1033
1034 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001035 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001036
Alex Elder31739132012-03-07 11:40:08 -06001037 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001038 if (ret <= 0)
1039 goto out;
1040 con->out_skip -= ret;
1041 }
1042 ret = 1;
1043out:
1044 return ret;
1045}
1046
1047/*
1048 * Prepare to read connection handshake, or an ack.
1049 */
Sage Weileed0ef22009-11-10 14:34:36 -08001050static void prepare_read_banner(struct ceph_connection *con)
1051{
1052 dout("prepare_read_banner %p\n", con);
1053 con->in_base_pos = 0;
1054}
1055
Sage Weil31b80062009-10-06 11:31:13 -07001056static void prepare_read_connect(struct ceph_connection *con)
1057{
1058 dout("prepare_read_connect %p\n", con);
1059 con->in_base_pos = 0;
1060}
1061
1062static void prepare_read_ack(struct ceph_connection *con)
1063{
1064 dout("prepare_read_ack %p\n", con);
1065 con->in_base_pos = 0;
1066}
1067
1068static void prepare_read_tag(struct ceph_connection *con)
1069{
1070 dout("prepare_read_tag %p\n", con);
1071 con->in_base_pos = 0;
1072 con->in_tag = CEPH_MSGR_TAG_READY;
1073}
1074
1075/*
1076 * Prepare to read a message.
1077 */
1078static int prepare_read_message(struct ceph_connection *con)
1079{
1080 dout("prepare_read_message %p\n", con);
1081 BUG_ON(con->in_msg != NULL);
1082 con->in_base_pos = 0;
1083 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1084 return 0;
1085}
1086
1087
1088static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001089 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001090{
Alex Eldere6cee712012-05-10 10:29:50 -05001091 while (con->in_base_pos < end) {
1092 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001093 int have = size - left;
1094 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1095 if (ret <= 0)
1096 return ret;
1097 con->in_base_pos += ret;
1098 }
1099 return 1;
1100}
1101
1102
1103/*
1104 * Read all or part of the connect-side handshake on a new connection
1105 */
Sage Weileed0ef22009-11-10 14:34:36 -08001106static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001107{
Alex Elderfd516532012-05-10 10:29:50 -05001108 int size;
1109 int end;
1110 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001111
Sage Weileed0ef22009-11-10 14:34:36 -08001112 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001113
1114 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001115 size = strlen(CEPH_BANNER);
1116 end = size;
1117 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001118 if (ret <= 0)
1119 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001120
1121 size = sizeof (con->actual_peer_addr);
1122 end += size;
1123 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001124 if (ret <= 0)
1125 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001126
1127 size = sizeof (con->peer_addr_for_me);
1128 end += size;
1129 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001130 if (ret <= 0)
1131 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001132
Sage Weileed0ef22009-11-10 14:34:36 -08001133out:
1134 return ret;
1135}
1136
1137static int read_partial_connect(struct ceph_connection *con)
1138{
Alex Elderfd516532012-05-10 10:29:50 -05001139 int size;
1140 int end;
1141 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001142
1143 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1144
Alex Elderfd516532012-05-10 10:29:50 -05001145 size = sizeof (con->in_reply);
1146 end = size;
1147 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001148 if (ret <= 0)
1149 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001150
1151 size = le32_to_cpu(con->in_reply.authorizer_len);
1152 end += size;
1153 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001154 if (ret <= 0)
1155 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001156
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001157 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1158 con, (int)con->in_reply.tag,
1159 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001160 le32_to_cpu(con->in_reply.global_seq));
1161out:
1162 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001163
Sage Weil31b80062009-10-06 11:31:13 -07001164}
1165
1166/*
1167 * Verify the hello banner looks okay.
1168 */
1169static int verify_hello(struct ceph_connection *con)
1170{
1171 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001172 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001173 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001174 con->error_msg = "protocol error, bad banner";
1175 return -1;
1176 }
1177 return 0;
1178}
1179
1180static bool addr_is_blank(struct sockaddr_storage *ss)
1181{
1182 switch (ss->ss_family) {
1183 case AF_INET:
1184 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1185 case AF_INET6:
1186 return
1187 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1188 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1189 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1190 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1191 }
1192 return false;
1193}
1194
1195static int addr_port(struct sockaddr_storage *ss)
1196{
1197 switch (ss->ss_family) {
1198 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001199 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001200 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001201 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001202 }
1203 return 0;
1204}
1205
1206static void addr_set_port(struct sockaddr_storage *ss, int p)
1207{
1208 switch (ss->ss_family) {
1209 case AF_INET:
1210 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001211 break;
Sage Weil31b80062009-10-06 11:31:13 -07001212 case AF_INET6:
1213 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001214 break;
Sage Weil31b80062009-10-06 11:31:13 -07001215 }
1216}
1217
1218/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001219 * Unlike other *_pton function semantics, zero indicates success.
1220 */
1221static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1222 char delim, const char **ipend)
1223{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001224 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1225 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001226
1227 memset(ss, 0, sizeof(*ss));
1228
1229 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1230 ss->ss_family = AF_INET;
1231 return 0;
1232 }
1233
1234 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1235 ss->ss_family = AF_INET6;
1236 return 0;
1237 }
1238
1239 return -EINVAL;
1240}
1241
1242/*
1243 * Extract hostname string and resolve using kernel DNS facility.
1244 */
1245#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1246static int ceph_dns_resolve_name(const char *name, size_t namelen,
1247 struct sockaddr_storage *ss, char delim, const char **ipend)
1248{
1249 const char *end, *delim_p;
1250 char *colon_p, *ip_addr = NULL;
1251 int ip_len, ret;
1252
1253 /*
1254 * The end of the hostname occurs immediately preceding the delimiter or
1255 * the port marker (':') where the delimiter takes precedence.
1256 */
1257 delim_p = memchr(name, delim, namelen);
1258 colon_p = memchr(name, ':', namelen);
1259
1260 if (delim_p && colon_p)
1261 end = delim_p < colon_p ? delim_p : colon_p;
1262 else if (!delim_p && colon_p)
1263 end = colon_p;
1264 else {
1265 end = delim_p;
1266 if (!end) /* case: hostname:/ */
1267 end = name + namelen;
1268 }
1269
1270 if (end <= name)
1271 return -EINVAL;
1272
1273 /* do dns_resolve upcall */
1274 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1275 if (ip_len > 0)
1276 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1277 else
1278 ret = -ESRCH;
1279
1280 kfree(ip_addr);
1281
1282 *ipend = end;
1283
1284 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1285 ret, ret ? "failed" : ceph_pr_addr(ss));
1286
1287 return ret;
1288}
1289#else
1290static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1291 struct sockaddr_storage *ss, char delim, const char **ipend)
1292{
1293 return -EINVAL;
1294}
1295#endif
1296
1297/*
1298 * Parse a server name (IP or hostname). If a valid IP address is not found
1299 * then try to extract a hostname to resolve using userspace DNS upcall.
1300 */
1301static int ceph_parse_server_name(const char *name, size_t namelen,
1302 struct sockaddr_storage *ss, char delim, const char **ipend)
1303{
1304 int ret;
1305
1306 ret = ceph_pton(name, namelen, ss, delim, ipend);
1307 if (ret)
1308 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1309
1310 return ret;
1311}
1312
1313/*
Sage Weil31b80062009-10-06 11:31:13 -07001314 * Parse an ip[:port] list into an addr array. Use the default
1315 * monitor port if a port isn't specified.
1316 */
1317int ceph_parse_ips(const char *c, const char *end,
1318 struct ceph_entity_addr *addr,
1319 int max_count, int *count)
1320{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001321 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001322 const char *p = c;
1323
1324 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1325 for (i = 0; i < max_count; i++) {
1326 const char *ipend;
1327 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001328 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001329 char delim = ',';
1330
1331 if (*p == '[') {
1332 delim = ']';
1333 p++;
1334 }
Sage Weil31b80062009-10-06 11:31:13 -07001335
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001336 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1337 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001338 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001339 ret = -EINVAL;
1340
Sage Weil31b80062009-10-06 11:31:13 -07001341 p = ipend;
1342
Sage Weil39139f62010-07-08 09:54:52 -07001343 if (delim == ']') {
1344 if (*p != ']') {
1345 dout("missing matching ']'\n");
1346 goto bad;
1347 }
1348 p++;
1349 }
1350
Sage Weil31b80062009-10-06 11:31:13 -07001351 /* port? */
1352 if (p < end && *p == ':') {
1353 port = 0;
1354 p++;
1355 while (p < end && *p >= '0' && *p <= '9') {
1356 port = (port * 10) + (*p - '0');
1357 p++;
1358 }
1359 if (port > 65535 || port == 0)
1360 goto bad;
1361 } else {
1362 port = CEPH_MON_PORT;
1363 }
1364
1365 addr_set_port(ss, port);
1366
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001367 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001368
1369 if (p == end)
1370 break;
1371 if (*p != ',')
1372 goto bad;
1373 p++;
1374 }
1375
1376 if (p != end)
1377 goto bad;
1378
1379 if (count)
1380 *count = i + 1;
1381 return 0;
1382
1383bad:
Sage Weil39139f62010-07-08 09:54:52 -07001384 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001385 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001386}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001387EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001388
Sage Weileed0ef22009-11-10 14:34:36 -08001389static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001390{
Sage Weileed0ef22009-11-10 14:34:36 -08001391 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001392
1393 if (verify_hello(con) < 0)
1394 return -1;
1395
Sage Weil63f2d212009-11-03 15:17:56 -08001396 ceph_decode_addr(&con->actual_peer_addr);
1397 ceph_decode_addr(&con->peer_addr_for_me);
1398
Sage Weil31b80062009-10-06 11:31:13 -07001399 /*
1400 * Make sure the other end is who we wanted. note that the other
1401 * end may not yet know their ip address, so if it's 0.0.0.0, give
1402 * them the benefit of the doubt.
1403 */
Sage Weil103e2d32010-01-07 16:12:36 -08001404 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1405 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001406 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1407 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001408 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001409 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001410 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001411 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001412 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001413 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001414 return -1;
1415 }
1416
1417 /*
1418 * did we learn our address?
1419 */
1420 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1421 int port = addr_port(&con->msgr->inst.addr.in_addr);
1422
1423 memcpy(&con->msgr->inst.addr.in_addr,
1424 &con->peer_addr_for_me.in_addr,
1425 sizeof(con->peer_addr_for_me.in_addr));
1426 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001427 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001428 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001429 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001430 }
1431
Sage Weileed0ef22009-11-10 14:34:36 -08001432 set_bit(NEGOTIATING, &con->state);
1433 prepare_read_connect(con);
1434 return 0;
1435}
1436
Sage Weil04a419f2009-12-23 09:30:21 -08001437static void fail_protocol(struct ceph_connection *con)
1438{
1439 reset_connection(con);
1440 set_bit(CLOSED, &con->state); /* in case there's queued work */
Sage Weil04a419f2009-12-23 09:30:21 -08001441}
1442
Sage Weileed0ef22009-11-10 14:34:36 -08001443static int process_connect(struct ceph_connection *con)
1444{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001445 u64 sup_feat = con->msgr->supported_features;
1446 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001447 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001448 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001449
Sage Weileed0ef22009-11-10 14:34:36 -08001450 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1451
Sage Weil31b80062009-10-06 11:31:13 -07001452 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001453 case CEPH_MSGR_TAG_FEATURES:
1454 pr_err("%s%lld %s feature set mismatch,"
1455 " my %llx < server's %llx, missing %llx\n",
1456 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001457 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001458 sup_feat, server_feat, server_feat & ~sup_feat);
1459 con->error_msg = "missing required protocol features";
1460 fail_protocol(con);
1461 return -1;
1462
Sage Weil31b80062009-10-06 11:31:13 -07001463 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001464 pr_err("%s%lld %s protocol version mismatch,"
1465 " my %d != server's %d\n",
1466 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001467 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001468 le32_to_cpu(con->out_connect.protocol_version),
1469 le32_to_cpu(con->in_reply.protocol_version));
1470 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001471 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001472 return -1;
1473
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001474 case CEPH_MSGR_TAG_BADAUTHORIZER:
1475 con->auth_retry++;
1476 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1477 con->auth_retry);
1478 if (con->auth_retry == 2) {
1479 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001480 return -1;
1481 }
1482 con->auth_retry = 1;
Alex Eldere2200422012-05-23 14:35:23 -05001483 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001484 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001485 if (ret < 0)
1486 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001487 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001488 break;
Sage Weil31b80062009-10-06 11:31:13 -07001489
1490 case CEPH_MSGR_TAG_RESETSESSION:
1491 /*
1492 * If we connected with a large connect_seq but the peer
1493 * has no record of a session with us (no connection, or
1494 * connect_seq == 0), they will send RESETSESION to indicate
1495 * that they must have reset their session, and may have
1496 * dropped messages.
1497 */
1498 dout("process_connect got RESET peer seq %u\n",
1499 le32_to_cpu(con->in_connect.connect_seq));
1500 pr_err("%s%lld %s connection reset\n",
1501 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001502 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001503 reset_connection(con);
Alex Eldere2200422012-05-23 14:35:23 -05001504 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001505 ret = prepare_write_connect(con);
1506 if (ret < 0)
1507 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001508 prepare_read_connect(con);
1509
1510 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001511 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001512 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1513 if (con->ops->peer_reset)
1514 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001515 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07001516 if (test_bit(CLOSED, &con->state) ||
1517 test_bit(OPENING, &con->state))
1518 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001519 break;
1520
1521 case CEPH_MSGR_TAG_RETRY_SESSION:
1522 /*
1523 * If we sent a smaller connect_seq than the peer has, try
1524 * again with a larger value.
1525 */
1526 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1527 le32_to_cpu(con->out_connect.connect_seq),
1528 le32_to_cpu(con->in_connect.connect_seq));
1529 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Alex Eldere2200422012-05-23 14:35:23 -05001530 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001531 ret = prepare_write_connect(con);
1532 if (ret < 0)
1533 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001534 prepare_read_connect(con);
1535 break;
1536
1537 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1538 /*
1539 * If we sent a smaller global_seq than the peer has, try
1540 * again with a larger value.
1541 */
Sage Weileed0ef22009-11-10 14:34:36 -08001542 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001543 con->peer_global_seq,
1544 le32_to_cpu(con->in_connect.global_seq));
1545 get_global_seq(con->msgr,
1546 le32_to_cpu(con->in_connect.global_seq));
Alex Eldere2200422012-05-23 14:35:23 -05001547 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001548 ret = prepare_write_connect(con);
1549 if (ret < 0)
1550 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001551 prepare_read_connect(con);
1552 break;
1553
1554 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001555 if (req_feat & ~server_feat) {
1556 pr_err("%s%lld %s protocol feature mismatch,"
1557 " my required %llx > server's %llx, need %llx\n",
1558 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001559 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001560 req_feat, server_feat, req_feat & ~server_feat);
1561 con->error_msg = "missing required protocol features";
1562 fail_protocol(con);
1563 return -1;
1564 }
Alex Elder3ec50d12012-05-23 14:35:23 -05001565 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001566 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001567 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1568 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001569 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001570 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1571 con->peer_global_seq,
1572 le32_to_cpu(con->in_reply.connect_seq),
1573 con->connect_seq);
1574 WARN_ON(con->connect_seq !=
1575 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001576
1577 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elder928443c2012-05-22 11:41:43 -05001578 set_bit(LOSSYTX, &con->flags);
Sage Weil92ac41d2009-12-14 14:56:56 -08001579
Sage Weil31b80062009-10-06 11:31:13 -07001580 prepare_read_tag(con);
1581 break;
1582
1583 case CEPH_MSGR_TAG_WAIT:
1584 /*
1585 * If there is a connection race (we are opening
1586 * connections to each other), one of us may just have
1587 * to WAIT. This shouldn't happen if we are the
1588 * client.
1589 */
Sage Weil04177882011-05-12 15:33:17 -07001590 pr_err("process_connect got WAIT as client\n");
1591 con->error_msg = "protocol error, got WAIT as client";
1592 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001593
1594 default:
1595 pr_err("connect protocol error, will retry\n");
1596 con->error_msg = "protocol error, garbage tag during connect";
1597 return -1;
1598 }
1599 return 0;
1600}
1601
1602
1603/*
1604 * read (part of) an ack
1605 */
1606static int read_partial_ack(struct ceph_connection *con)
1607{
Alex Elderfd516532012-05-10 10:29:50 -05001608 int size = sizeof (con->in_temp_ack);
1609 int end = size;
1610
1611 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001612}
1613
1614
1615/*
1616 * We can finally discard anything that's been acked.
1617 */
1618static void process_ack(struct ceph_connection *con)
1619{
1620 struct ceph_msg *m;
1621 u64 ack = le64_to_cpu(con->in_temp_ack);
1622 u64 seq;
1623
Sage Weil31b80062009-10-06 11:31:13 -07001624 while (!list_empty(&con->out_sent)) {
1625 m = list_first_entry(&con->out_sent, struct ceph_msg,
1626 list_head);
1627 seq = le64_to_cpu(m->hdr.seq);
1628 if (seq > ack)
1629 break;
1630 dout("got ack for seq %llu type %d at %p\n", seq,
1631 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001632 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001633 ceph_msg_remove(m);
1634 }
Sage Weil31b80062009-10-06 11:31:13 -07001635 prepare_read_tag(con);
1636}
1637
1638
1639
1640
Yehuda Sadeh24504182010-01-08 13:58:34 -08001641static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001642 struct kvec *section,
1643 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001644{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001645 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001646
Yehuda Sadeh24504182010-01-08 13:58:34 -08001647 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001648
Yehuda Sadeh24504182010-01-08 13:58:34 -08001649 while (section->iov_len < sec_len) {
1650 BUG_ON(section->iov_base == NULL);
1651 left = sec_len - section->iov_len;
1652 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1653 section->iov_len, left);
1654 if (ret <= 0)
1655 return ret;
1656 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001657 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001658 if (section->iov_len == sec_len)
1659 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001660
1661 return 1;
1662}
1663
Alex Elder1c20f2d2012-06-04 14:43:32 -05001664static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
1665 struct ceph_msg_header *hdr);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001666
1667
1668static int read_partial_message_pages(struct ceph_connection *con,
1669 struct page **pages,
Eric Dumazet95c96172012-04-15 05:58:06 +00001670 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001671{
1672 void *p;
1673 int ret;
1674 int left;
1675
1676 left = min((int)(data_len - con->in_msg_pos.data_pos),
1677 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1678 /* (page) data */
1679 BUG_ON(pages == NULL);
1680 p = kmap(pages[con->in_msg_pos.page]);
1681 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1682 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001683 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001684 con->in_data_crc =
1685 crc32c(con->in_data_crc,
1686 p + con->in_msg_pos.page_pos, ret);
1687 kunmap(pages[con->in_msg_pos.page]);
1688 if (ret <= 0)
1689 return ret;
1690 con->in_msg_pos.data_pos += ret;
1691 con->in_msg_pos.page_pos += ret;
1692 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1693 con->in_msg_pos.page_pos = 0;
1694 con->in_msg_pos.page++;
1695 }
1696
1697 return ret;
1698}
1699
1700#ifdef CONFIG_BLOCK
1701static int read_partial_message_bio(struct ceph_connection *con,
1702 struct bio **bio_iter, int *bio_seg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001703 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001704{
1705 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1706 void *p;
1707 int ret, left;
1708
1709 if (IS_ERR(bv))
1710 return PTR_ERR(bv);
1711
1712 left = min((int)(data_len - con->in_msg_pos.data_pos),
1713 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1714
1715 p = kmap(bv->bv_page) + bv->bv_offset;
1716
1717 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1718 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001719 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001720 con->in_data_crc =
1721 crc32c(con->in_data_crc,
1722 p + con->in_msg_pos.page_pos, ret);
1723 kunmap(bv->bv_page);
1724 if (ret <= 0)
1725 return ret;
1726 con->in_msg_pos.data_pos += ret;
1727 con->in_msg_pos.page_pos += ret;
1728 if (con->in_msg_pos.page_pos == bv->bv_len) {
1729 con->in_msg_pos.page_pos = 0;
1730 iter_bio_next(bio_iter, bio_seg);
1731 }
1732
1733 return ret;
1734}
1735#endif
1736
Sage Weil31b80062009-10-06 11:31:13 -07001737/*
1738 * read (part of) a message.
1739 */
1740static int read_partial_message(struct ceph_connection *con)
1741{
1742 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05001743 int size;
1744 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001745 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00001746 unsigned int front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001747 bool do_datacrc = !con->msgr->nocrc;
Sage Weilae187562010-04-22 07:47:01 -07001748 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001749 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001750
1751 dout("read_partial_message con %p msg %p\n", con, m);
1752
1753 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05001754 size = sizeof (con->in_hdr);
1755 end = size;
1756 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05001757 if (ret <= 0)
1758 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001759
1760 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1761 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1762 pr_err("read_partial_message bad hdr "
1763 " crc %u != expected %u\n",
1764 crc, con->in_hdr.crc);
1765 return -EBADMSG;
1766 }
1767
Sage Weil31b80062009-10-06 11:31:13 -07001768 front_len = le32_to_cpu(con->in_hdr.front_len);
1769 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1770 return -EIO;
1771 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1772 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1773 return -EIO;
1774 data_len = le32_to_cpu(con->in_hdr.data_len);
1775 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1776 return -EIO;
1777
Sage Weilae187562010-04-22 07:47:01 -07001778 /* verify seq# */
1779 seq = le64_to_cpu(con->in_hdr.seq);
1780 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001781 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001782 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001783 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001784 seq, con->in_seq + 1);
1785 con->in_base_pos = -front_len - middle_len - data_len -
1786 sizeof(m->footer);
1787 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001788 return 0;
1789 } else if ((s64)seq - (s64)con->in_seq > 1) {
1790 pr_err("read_partial_message bad seq %lld expected %lld\n",
1791 seq, con->in_seq + 1);
1792 con->error_msg = "bad message sequence # for incoming message";
1793 return -EBADMSG;
1794 }
1795
Sage Weil31b80062009-10-06 11:31:13 -07001796 /* allocate message? */
1797 if (!con->in_msg) {
1798 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1799 con->in_hdr.front_len, con->in_hdr.data_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05001800 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
Sage Weil31b80062009-10-06 11:31:13 -07001801 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001802 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001803 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001804 con->in_base_pos = -front_len - middle_len - data_len -
1805 sizeof(m->footer);
1806 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001807 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001808 return 0;
1809 }
Sage Weila79832f2010-04-01 16:06:19 -07001810 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001811 con->error_msg =
1812 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001813 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001814 }
Alex Elder38941f82012-06-01 14:56:43 -05001815
1816 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07001817 m = con->in_msg;
1818 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001819 if (m->middle)
1820 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001821
1822 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001823 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001824 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001825 else
1826 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001827 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001828 }
1829
1830 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001831 ret = read_partial_message_section(con, &m->front, front_len,
1832 &con->in_front_crc);
1833 if (ret <= 0)
1834 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001835
1836 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001837 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001838 ret = read_partial_message_section(con, &m->middle->vec,
1839 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001840 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001841 if (ret <= 0)
1842 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001843 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001844#ifdef CONFIG_BLOCK
1845 if (m->bio && !m->bio_iter)
1846 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1847#endif
Sage Weil31b80062009-10-06 11:31:13 -07001848
1849 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001850 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001851 if (m->pages) {
1852 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001853 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001854 if (ret <= 0)
1855 return ret;
1856#ifdef CONFIG_BLOCK
1857 } else if (m->bio) {
1858
1859 ret = read_partial_message_bio(con,
1860 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001861 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001862 if (ret <= 0)
1863 return ret;
1864#endif
1865 } else {
1866 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001867 }
1868 }
1869
Sage Weil31b80062009-10-06 11:31:13 -07001870 /* footer */
Alex Elderfd516532012-05-10 10:29:50 -05001871 size = sizeof (m->footer);
1872 end += size;
1873 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05001874 if (ret <= 0)
1875 return ret;
1876
Sage Weil31b80062009-10-06 11:31:13 -07001877 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1878 m, front_len, m->footer.front_crc, middle_len,
1879 m->footer.middle_crc, data_len, m->footer.data_crc);
1880
1881 /* crc ok? */
1882 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1883 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1884 m, con->in_front_crc, m->footer.front_crc);
1885 return -EBADMSG;
1886 }
1887 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1888 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1889 m, con->in_middle_crc, m->footer.middle_crc);
1890 return -EBADMSG;
1891 }
Alex Elderbca064d2012-02-15 07:43:54 -06001892 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001893 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1894 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1895 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1896 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1897 return -EBADMSG;
1898 }
1899
1900 return 1; /* done! */
1901}
1902
1903/*
1904 * Process message. This happens in the worker thread. The callback should
1905 * be careful not to do anything that waits on other incoming messages or it
1906 * may deadlock.
1907 */
1908static void process_message(struct ceph_connection *con)
1909{
Sage Weil5e095e82009-12-14 14:30:34 -08001910 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001911
Alex Elder38941f82012-06-01 14:56:43 -05001912 BUG_ON(con->in_msg->con != con);
1913 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08001914 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001915 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07001916 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07001917
1918 /* if first message, set peer_name */
1919 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001920 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001921
Sage Weil31b80062009-10-06 11:31:13 -07001922 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001923 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001924
1925 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1926 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001927 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001928 le16_to_cpu(msg->hdr.type),
1929 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1930 le32_to_cpu(msg->hdr.front_len),
1931 le32_to_cpu(msg->hdr.data_len),
1932 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1933 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001934
1935 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001936 prepare_read_tag(con);
1937}
1938
1939
1940/*
1941 * Write something to the socket. Called in a worker thread when the
1942 * socket appears to be writeable and we have something ready to send.
1943 */
1944static int try_write(struct ceph_connection *con)
1945{
Sage Weil31b80062009-10-06 11:31:13 -07001946 int ret = 1;
1947
Sage Weild59315c2012-06-21 12:49:23 -07001948 dout("try_write start %p state %lu\n", con, con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001949
Sage Weil31b80062009-10-06 11:31:13 -07001950more:
1951 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1952
1953 /* open the socket first? */
1954 if (con->sock == NULL) {
Alex Eldera5988c42012-05-29 11:04:58 -05001955 set_bit(CONNECTING, &con->state);
1956
Alex Eldere2200422012-05-23 14:35:23 -05001957 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001958 prepare_write_banner(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001959 ret = prepare_write_connect(con);
1960 if (ret < 0)
1961 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08001962 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001963
Sage Weilcf3e5c42009-12-11 09:48:05 -08001964 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001965 con->in_tag = CEPH_MSGR_TAG_READY;
1966 dout("try_write initiating connect on %p new state %lu\n",
1967 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06001968 ret = ceph_tcp_connect(con);
1969 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07001970 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07001971 goto out;
1972 }
1973 }
1974
1975more_kvec:
1976 /* kvec data queued? */
1977 if (con->out_skip) {
1978 ret = write_partial_skip(con);
1979 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001980 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001981 }
1982 if (con->out_kvec_left) {
1983 ret = write_partial_kvec(con);
1984 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001985 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001986 }
1987
1988 /* msg pages? */
1989 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001990 if (con->out_msg_done) {
1991 ceph_msg_put(con->out_msg);
1992 con->out_msg = NULL; /* we're done with this one */
1993 goto do_next;
1994 }
1995
Sage Weil31b80062009-10-06 11:31:13 -07001996 ret = write_partial_msg_pages(con);
1997 if (ret == 1)
1998 goto more_kvec; /* we need to send the footer, too! */
1999 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002000 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002001 if (ret < 0) {
2002 dout("try_write write_partial_msg_pages err %d\n",
2003 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002004 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002005 }
2006 }
2007
Sage Weilc86a2932009-12-14 14:04:30 -08002008do_next:
Sage Weil31b80062009-10-06 11:31:13 -07002009 if (!test_bit(CONNECTING, &con->state)) {
2010 /* is anything else pending? */
2011 if (!list_empty(&con->out_queue)) {
2012 prepare_write_message(con);
2013 goto more;
2014 }
2015 if (con->in_seq > con->in_seq_acked) {
2016 prepare_write_ack(con);
2017 goto more;
2018 }
Alex Elder928443c2012-05-22 11:41:43 -05002019 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002020 prepare_write_keepalive(con);
2021 goto more;
2022 }
2023 }
2024
2025 /* Nothing to do! */
Alex Elder928443c2012-05-22 11:41:43 -05002026 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002027 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002028 ret = 0;
2029out:
Sage Weil42961d22011-01-25 08:19:34 -08002030 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002031 return ret;
2032}
2033
2034
2035
2036/*
2037 * Read what we can from the socket.
2038 */
2039static int try_read(struct ceph_connection *con)
2040{
Sage Weil31b80062009-10-06 11:31:13 -07002041 int ret = -1;
2042
2043 if (!con->sock)
2044 return 0;
2045
2046 if (test_bit(STANDBY, &con->state))
2047 return 0;
2048
2049 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08002050
Sage Weil31b80062009-10-06 11:31:13 -07002051more:
2052 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2053 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002054
2055 /*
2056 * process_connect and process_message drop and re-take
2057 * con->mutex. make sure we handle a racing close or reopen.
2058 */
2059 if (test_bit(CLOSED, &con->state) ||
2060 test_bit(OPENING, &con->state)) {
2061 ret = -EAGAIN;
2062 goto out;
2063 }
2064
Sage Weil31b80062009-10-06 11:31:13 -07002065 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08002066 if (!test_bit(NEGOTIATING, &con->state)) {
2067 dout("try_read connecting\n");
2068 ret = read_partial_banner(con);
2069 if (ret <= 0)
Sage Weileed0ef22009-11-10 14:34:36 -08002070 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002071 ret = process_banner(con);
2072 if (ret < 0)
2073 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08002074 }
Sage Weil31b80062009-10-06 11:31:13 -07002075 ret = read_partial_connect(con);
2076 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002077 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002078 ret = process_connect(con);
2079 if (ret < 0)
2080 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002081 goto more;
2082 }
2083
2084 if (con->in_base_pos < 0) {
2085 /*
2086 * skipping + discarding content.
2087 *
2088 * FIXME: there must be a better way to do this!
2089 */
Alex Elder84495f42012-02-15 07:43:55 -06002090 static char buf[SKIP_BUF_SIZE];
2091 int skip = min((int) sizeof (buf), -con->in_base_pos);
2092
Sage Weil31b80062009-10-06 11:31:13 -07002093 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2094 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2095 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002096 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002097 con->in_base_pos += ret;
2098 if (con->in_base_pos)
2099 goto more;
2100 }
2101 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2102 /*
2103 * what's next?
2104 */
2105 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2106 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002107 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002108 dout("try_read got tag %d\n", (int)con->in_tag);
2109 switch (con->in_tag) {
2110 case CEPH_MSGR_TAG_MSG:
2111 prepare_read_message(con);
2112 break;
2113 case CEPH_MSGR_TAG_ACK:
2114 prepare_read_ack(con);
2115 break;
2116 case CEPH_MSGR_TAG_CLOSE:
2117 set_bit(CLOSED, &con->state); /* fixme */
Sage Weil98bdb0a2011-01-25 08:17:48 -08002118 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002119 default:
2120 goto bad_tag;
2121 }
2122 }
2123 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2124 ret = read_partial_message(con);
2125 if (ret <= 0) {
2126 switch (ret) {
2127 case -EBADMSG:
2128 con->error_msg = "bad crc";
2129 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002130 break;
Sage Weil31b80062009-10-06 11:31:13 -07002131 case -EIO:
2132 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002133 break;
Sage Weil31b80062009-10-06 11:31:13 -07002134 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002135 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002136 }
2137 if (con->in_tag == CEPH_MSGR_TAG_READY)
2138 goto more;
2139 process_message(con);
2140 goto more;
2141 }
2142 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2143 ret = read_partial_ack(con);
2144 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002145 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002146 process_ack(con);
2147 goto more;
2148 }
2149
Sage Weil31b80062009-10-06 11:31:13 -07002150out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002151 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002152 return ret;
2153
2154bad_tag:
2155 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2156 con->error_msg = "protocol error, garbage tag";
2157 ret = -1;
2158 goto out;
2159}
2160
2161
2162/*
2163 * Atomically queue work on a connection. Bump @con reference to
2164 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002165 */
2166static void queue_con(struct ceph_connection *con)
2167{
Sage Weil31b80062009-10-06 11:31:13 -07002168 if (!con->ops->get(con)) {
2169 dout("queue_con %p ref count 0\n", con);
2170 return;
2171 }
2172
Tejun Heof363e45fd12011-01-03 14:49:46 +01002173 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002174 dout("queue_con %p - already queued\n", con);
2175 con->ops->put(con);
2176 } else {
2177 dout("queue_con %p\n", con);
2178 }
2179}
2180
2181/*
2182 * Do some work on a connection. Drop a connection ref when we're done.
2183 */
2184static void con_work(struct work_struct *work)
2185{
2186 struct ceph_connection *con = container_of(work, struct ceph_connection,
2187 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002188 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002189
Sage Weil9dd46582010-04-28 13:51:50 -07002190 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002191restart:
Alex Elder188048b2012-06-20 21:53:53 -05002192 if (test_and_clear_bit(SOCK_CLOSED, &con->flags)) {
Alex Elder3ec50d12012-05-23 14:35:23 -05002193 if (test_and_clear_bit(CONNECTING, &con->state)) {
2194 clear_bit(NEGOTIATING, &con->state);
Alex Elder188048b2012-06-20 21:53:53 -05002195 con->error_msg = "connection failed";
Alex Elder3ec50d12012-05-23 14:35:23 -05002196 } else {
Alex Elder188048b2012-06-20 21:53:53 -05002197 con->error_msg = "socket closed";
Alex Elder3ec50d12012-05-23 14:35:23 -05002198 }
Alex Elder188048b2012-06-20 21:53:53 -05002199 goto fault;
2200 }
2201
Alex Elder928443c2012-05-22 11:41:43 -05002202 if (test_and_clear_bit(BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002203 dout("con_work %p backing off\n", con);
2204 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2205 round_jiffies_relative(con->delay))) {
2206 dout("con_work %p backoff %lu\n", con, con->delay);
2207 mutex_unlock(&con->mutex);
2208 return;
2209 } else {
2210 con->ops->put(con);
2211 dout("con_work %p FAILED to back off %lu\n", con,
2212 con->delay);
2213 }
2214 }
Sage Weil9dd46582010-04-28 13:51:50 -07002215
Sage Weile00de342011-03-04 12:25:05 -08002216 if (test_bit(STANDBY, &con->state)) {
2217 dout("con_work %p STANDBY\n", con);
2218 goto done;
2219 }
Sage Weil31b80062009-10-06 11:31:13 -07002220 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2221 dout("con_work CLOSED\n");
2222 con_close_socket(con);
2223 goto done;
2224 }
2225 if (test_and_clear_bit(OPENING, &con->state)) {
2226 /* reopen w/ new peer */
2227 dout("con_work OPENING\n");
2228 con_close_socket(con);
2229 }
2230
Sage Weil0da5d702011-05-19 11:21:05 -07002231 ret = try_read(con);
2232 if (ret == -EAGAIN)
2233 goto restart;
2234 if (ret < 0)
2235 goto fault;
2236
2237 ret = try_write(con);
2238 if (ret == -EAGAIN)
2239 goto restart;
2240 if (ret < 0)
2241 goto fault;
Sage Weil31b80062009-10-06 11:31:13 -07002242
2243done:
Sage Weil9dd46582010-04-28 13:51:50 -07002244 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002245done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002246 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002247 return;
2248
2249fault:
2250 mutex_unlock(&con->mutex);
2251 ceph_fault(con); /* error/fault path */
2252 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002253}
2254
2255
2256/*
2257 * Generic error/fault handler. A retry mechanism is used with
2258 * exponential backoff
2259 */
2260static void ceph_fault(struct ceph_connection *con)
2261{
2262 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002263 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002264 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002265 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002266
Alex Elder928443c2012-05-22 11:41:43 -05002267 if (test_bit(LOSSYTX, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002268 dout("fault on LOSSYTX channel\n");
2269 goto out;
2270 }
2271
Sage Weilec302642009-12-22 10:43:42 -08002272 mutex_lock(&con->mutex);
Sage Weil91e45ce32010-02-15 12:05:09 -08002273 if (test_bit(CLOSED, &con->state))
2274 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002275
Sage Weil31b80062009-10-06 11:31:13 -07002276 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002277
2278 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002279 BUG_ON(con->in_msg->con != con);
2280 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08002281 ceph_msg_put(con->in_msg);
2282 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07002283 con->ops->put(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002284 }
Sage Weil31b80062009-10-06 11:31:13 -07002285
Sage Weile80a52d2010-02-25 12:40:45 -08002286 /* Requeue anything that hasn't been acked */
2287 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002288
Sage Weile76661d2011-03-03 10:10:15 -08002289 /* If there are no messages queued or keepalive pending, place
2290 * the connection in a STANDBY state */
2291 if (list_empty(&con->out_queue) &&
Alex Elder928443c2012-05-22 11:41:43 -05002292 !test_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weile00de342011-03-04 12:25:05 -08002293 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elder928443c2012-05-22 11:41:43 -05002294 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002295 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08002296 } else {
2297 /* retry after a delay. */
2298 if (con->delay == 0)
2299 con->delay = BASE_DELAY_INTERVAL;
2300 else if (con->delay < MAX_DELAY_INTERVAL)
2301 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002302 con->ops->get(con);
2303 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002304 round_jiffies_relative(con->delay))) {
2305 dout("fault queued %p delay %lu\n", con, con->delay);
2306 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002307 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002308 dout("fault failed to queue %p delay %lu, backoff\n",
2309 con, con->delay);
2310 /*
2311 * In many cases we see a socket state change
2312 * while con_work is running and end up
2313 * queuing (non-delayed) work, such that we
2314 * can't backoff with a delay. Set a flag so
2315 * that when con_work restarts we schedule the
2316 * delay then.
2317 */
Alex Elder928443c2012-05-22 11:41:43 -05002318 set_bit(BACKOFF, &con->flags);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002319 }
Sage Weil31b80062009-10-06 11:31:13 -07002320 }
2321
Sage Weil91e45ce32010-02-15 12:05:09 -08002322out_unlock:
2323 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002324out:
Sage Weil161fd652010-02-25 12:38:57 -08002325 /*
2326 * in case we faulted due to authentication, invalidate our
2327 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002328 */
Sage Weil161fd652010-02-25 12:38:57 -08002329 if (con->auth_retry && con->ops->invalidate_authorizer) {
2330 dout("calling invalidate_authorizer()\n");
2331 con->ops->invalidate_authorizer(con);
2332 }
2333
Sage Weil31b80062009-10-06 11:31:13 -07002334 if (con->ops->fault)
2335 con->ops->fault(con);
2336}
2337
2338
2339
2340/*
Alex Elder15d98822012-05-26 23:26:43 -05002341 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002342 */
Alex Elder15d98822012-05-26 23:26:43 -05002343void ceph_messenger_init(struct ceph_messenger *msgr,
2344 struct ceph_entity_addr *myaddr,
2345 u32 supported_features,
2346 u32 required_features,
2347 bool nocrc)
Sage Weil31b80062009-10-06 11:31:13 -07002348{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002349 msgr->supported_features = supported_features;
2350 msgr->required_features = required_features;
2351
Sage Weil31b80062009-10-06 11:31:13 -07002352 spin_lock_init(&msgr->global_seq_lock);
2353
Sage Weil31b80062009-10-06 11:31:13 -07002354 if (myaddr)
2355 msgr->inst.addr = *myaddr;
2356
2357 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002358 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002359 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002360 encode_my_addr(msgr);
Alex Elder15d98822012-05-26 23:26:43 -05002361 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002362
Alex Elder15d98822012-05-26 23:26:43 -05002363 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002364}
Alex Elder15d98822012-05-26 23:26:43 -05002365EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002366
Sage Weile00de342011-03-04 12:25:05 -08002367static void clear_standby(struct ceph_connection *con)
2368{
2369 /* come back from STANDBY? */
2370 if (test_and_clear_bit(STANDBY, &con->state)) {
2371 mutex_lock(&con->mutex);
2372 dout("clear_standby %p and ++connect_seq\n", con);
2373 con->connect_seq++;
Alex Elder928443c2012-05-22 11:41:43 -05002374 WARN_ON(test_bit(WRITE_PENDING, &con->flags));
2375 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags));
Sage Weile00de342011-03-04 12:25:05 -08002376 mutex_unlock(&con->mutex);
2377 }
2378}
2379
Sage Weil31b80062009-10-06 11:31:13 -07002380/*
2381 * Queue up an outgoing message on the given connection.
2382 */
2383void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2384{
2385 if (test_bit(CLOSED, &con->state)) {
2386 dout("con_send %p closed, dropping %p\n", con, msg);
2387 ceph_msg_put(msg);
2388 return;
2389 }
2390
2391 /* set src+dst */
Sage Weildbad1852010-03-25 15:45:38 -07002392 msg->hdr.src = con->msgr->inst.name;
Sage Weil31b80062009-10-06 11:31:13 -07002393
Sage Weil3ca02ef2010-03-01 15:25:00 -08002394 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2395
Sage Weile84346b2010-05-11 21:20:38 -07002396 msg->needs_out_seq = true;
2397
Sage Weil31b80062009-10-06 11:31:13 -07002398 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08002399 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002400
Alex Elder38941f82012-06-01 14:56:43 -05002401 BUG_ON(msg->con != NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002402 msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002403 BUG_ON(msg->con == NULL);
2404
Sage Weil31b80062009-10-06 11:31:13 -07002405 BUG_ON(!list_empty(&msg->list_head));
2406 list_add_tail(&msg->list_head, &con->out_queue);
2407 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2408 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2409 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2410 le32_to_cpu(msg->hdr.front_len),
2411 le32_to_cpu(msg->hdr.middle_len),
2412 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002413 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002414
2415 /* if there wasn't anything waiting to send before, queue
2416 * new work */
Sage Weile00de342011-03-04 12:25:05 -08002417 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002418 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002419 queue_con(con);
2420}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002421EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002422
2423/*
2424 * Revoke a message that was previously queued for send
2425 */
Alex Elder6740a842012-06-01 14:56:43 -05002426void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002427{
Alex Elder6740a842012-06-01 14:56:43 -05002428 struct ceph_connection *con = msg->con;
2429
2430 if (!con)
2431 return; /* Message not in our possession */
2432
Sage Weilec302642009-12-22 10:43:42 -08002433 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002434 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05002435 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002436 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -05002437 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002438 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -05002439 msg->con = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05002440 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05002441
Sage Weil31b80062009-10-06 11:31:13 -07002442 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002443 }
2444 if (con->out_msg == msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002445 dout("%s %p msg %p - was sending\n", __func__, con, msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002446 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002447 if (con->out_kvec_is_msg) {
2448 con->out_skip = con->out_kvec_bytes;
2449 con->out_kvec_is_msg = false;
2450 }
Sage Weiled98ada2010-07-05 12:15:14 -07002451 msg->hdr.seq = 0;
Alex Elder92ce0342012-06-04 14:43:33 -05002452
2453 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07002454 }
Sage Weilec302642009-12-22 10:43:42 -08002455 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002456}
2457
2458/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002459 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002460 */
Alex Elder8921d112012-06-01 14:56:43 -05002461void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002462{
Alex Elder8921d112012-06-01 14:56:43 -05002463 struct ceph_connection *con;
2464
2465 BUG_ON(msg == NULL);
2466 if (!msg->con) {
2467 dout("%s msg %p null con\n", __func__, msg);
2468
2469 return; /* Message not in our possession */
2470 }
2471
2472 con = msg->con;
Sage Weil350b1c32009-12-22 10:45:45 -08002473 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05002474 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002475 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
2476 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
2477 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002478
2479 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05002480 dout("%s %p msg %p revoked\n", __func__, con, msg);
2481 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08002482 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002483 front_len -
2484 middle_len -
2485 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002486 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002487 ceph_msg_put(con->in_msg);
2488 con->in_msg = NULL;
2489 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002490 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002491 } else {
Alex Elder8921d112012-06-01 14:56:43 -05002492 dout("%s %p in_msg %p msg %p no-op\n",
2493 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002494 }
2495 mutex_unlock(&con->mutex);
2496}
2497
2498/*
Sage Weil31b80062009-10-06 11:31:13 -07002499 * Queue a keepalive byte to ensure the tcp connection is alive.
2500 */
2501void ceph_con_keepalive(struct ceph_connection *con)
2502{
Sage Weile00de342011-03-04 12:25:05 -08002503 dout("con_keepalive %p\n", con);
2504 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002505 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2506 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002507 queue_con(con);
2508}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002509EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002510
2511
2512/*
2513 * construct a new message with given type, size
2514 * the new msg has a ref count of 1.
2515 */
Sage Weilb61c2762011-08-09 15:03:46 -07002516struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2517 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002518{
2519 struct ceph_msg *m;
2520
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002521 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002522 if (m == NULL)
2523 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002524 kref_init(&m->kref);
Alex Elder38941f82012-06-01 14:56:43 -05002525
2526 m->con = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002527 INIT_LIST_HEAD(&m->list_head);
2528
Sage Weil45c6ceb2010-05-11 15:01:51 -07002529 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002530 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002531 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2532 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002533 m->hdr.front_len = cpu_to_le32(front_len);
2534 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002535 m->hdr.data_len = 0;
2536 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002537 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002538 m->footer.front_crc = 0;
2539 m->footer.middle_crc = 0;
2540 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002541 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002542 m->front_max = front_len;
2543 m->front_is_vmalloc = false;
2544 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002545 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002546 m->pool = NULL;
2547
Henry C Changca208922011-05-03 02:29:56 +00002548 /* middle */
2549 m->middle = NULL;
2550
2551 /* data */
2552 m->nr_pages = 0;
2553 m->page_alignment = 0;
2554 m->pages = NULL;
2555 m->pagelist = NULL;
2556 m->bio = NULL;
2557 m->bio_iter = NULL;
2558 m->bio_seg = 0;
2559 m->trail = NULL;
2560
Sage Weil31b80062009-10-06 11:31:13 -07002561 /* front */
2562 if (front_len) {
2563 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002564 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002565 PAGE_KERNEL);
2566 m->front_is_vmalloc = true;
2567 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002568 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002569 }
2570 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002571 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002572 front_len);
2573 goto out2;
2574 }
2575 } else {
2576 m->front.iov_base = NULL;
2577 }
2578 m->front.iov_len = front_len;
2579
Sage Weilbb257662010-04-01 16:07:23 -07002580 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002581 return m;
2582
2583out2:
2584 ceph_msg_put(m);
2585out:
Sage Weilb61c2762011-08-09 15:03:46 -07002586 if (!can_fail) {
2587 pr_err("msg_new can't create type %d front %d\n", type,
2588 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002589 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002590 } else {
2591 dout("msg_new can't create type %d front %d\n", type,
2592 front_len);
2593 }
Sage Weila79832f2010-04-01 16:06:19 -07002594 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002595}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002596EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002597
2598/*
Sage Weil31b80062009-10-06 11:31:13 -07002599 * Allocate "middle" portion of a message, if it is needed and wasn't
2600 * allocated by alloc_msg. This allows us to read a small fixed-size
2601 * per-type header in the front and then gracefully fail (i.e.,
2602 * propagate the error to the caller based on info in the front) when
2603 * the middle is too large.
2604 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002605static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002606{
2607 int type = le16_to_cpu(msg->hdr.type);
2608 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2609
2610 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2611 ceph_msg_type_name(type), middle_len);
2612 BUG_ON(!middle_len);
2613 BUG_ON(msg->middle);
2614
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002615 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002616 if (!msg->middle)
2617 return -ENOMEM;
2618 return 0;
2619}
2620
Yehuda Sadeh24504182010-01-08 13:58:34 -08002621/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05002622 * Allocate a message for receiving an incoming message on a
2623 * connection, and save the result in con->in_msg. Uses the
2624 * connection's private alloc_msg op if available.
2625 *
2626 * Returns true if the message should be skipped, false otherwise.
2627 * If true is returned (skip message), con->in_msg will be NULL.
2628 * If false is returned, con->in_msg will contain a pointer to the
2629 * newly-allocated message, or NULL in case of memory exhaustion.
Yehuda Sadeh24504182010-01-08 13:58:34 -08002630 */
Alex Elder1c20f2d2012-06-04 14:43:32 -05002631static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2632 struct ceph_msg_header *hdr)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002633{
2634 int type = le16_to_cpu(hdr->type);
2635 int front_len = le32_to_cpu(hdr->front_len);
2636 int middle_len = le32_to_cpu(hdr->middle_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002637 int ret;
2638
Alex Elder1c20f2d2012-06-04 14:43:32 -05002639 BUG_ON(con->in_msg != NULL);
2640
Yehuda Sadeh24504182010-01-08 13:58:34 -08002641 if (con->ops->alloc_msg) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002642 int skip = 0;
2643
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002644 mutex_unlock(&con->mutex);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002645 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002646 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002647 if (con->in_msg) {
Sage Weil36eb71a2012-06-21 12:47:08 -07002648 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002649 BUG_ON(con->in_msg->con == NULL);
2650 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002651 if (skip)
2652 con->in_msg = NULL;
2653
2654 if (!con->in_msg)
2655 return skip != 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002656 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002657 if (!con->in_msg) {
2658 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2659 if (!con->in_msg) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002660 pr_err("unable to allocate msg type %d len %d\n",
2661 type, front_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002662 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002663 }
Sage Weil36eb71a2012-06-21 12:47:08 -07002664 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002665 BUG_ON(con->in_msg->con == NULL);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002666 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002667 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002668 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002669
Alex Elder1c20f2d2012-06-04 14:43:32 -05002670 if (middle_len && !con->in_msg->middle) {
2671 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002672 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002673 ceph_msg_put(con->in_msg);
2674 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002675 }
2676 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002677
Alex Elder1c20f2d2012-06-04 14:43:32 -05002678 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002679}
2680
Sage Weil31b80062009-10-06 11:31:13 -07002681
2682/*
2683 * Free a generically kmalloc'd message.
2684 */
2685void ceph_msg_kfree(struct ceph_msg *m)
2686{
2687 dout("msg_kfree %p\n", m);
2688 if (m->front_is_vmalloc)
2689 vfree(m->front.iov_base);
2690 else
2691 kfree(m->front.iov_base);
2692 kfree(m);
2693}
2694
2695/*
2696 * Drop a msg ref. Destroy as needed.
2697 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002698void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002699{
Sage Weilc2e552e2009-12-07 15:55:05 -08002700 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002701
Sage Weilc2e552e2009-12-07 15:55:05 -08002702 dout("ceph_msg_put last one on %p\n", m);
2703 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002704
Sage Weilc2e552e2009-12-07 15:55:05 -08002705 /* drop middle, data, if any */
2706 if (m->middle) {
2707 ceph_buffer_put(m->middle);
2708 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002709 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002710 m->nr_pages = 0;
2711 m->pages = NULL;
2712
Sage Weil58bb3b32009-12-23 12:12:31 -08002713 if (m->pagelist) {
2714 ceph_pagelist_release(m->pagelist);
2715 kfree(m->pagelist);
2716 m->pagelist = NULL;
2717 }
2718
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002719 m->trail = NULL;
2720
Sage Weilc2e552e2009-12-07 15:55:05 -08002721 if (m->pool)
2722 ceph_msgpool_put(m->pool, m);
2723 else
2724 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002725}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002726EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002727
2728void ceph_msg_dump(struct ceph_msg *msg)
2729{
2730 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2731 msg->front_max, msg->nr_pages);
2732 print_hex_dump(KERN_DEBUG, "header: ",
2733 DUMP_PREFIX_OFFSET, 16, 1,
2734 &msg->hdr, sizeof(msg->hdr), true);
2735 print_hex_dump(KERN_DEBUG, " front: ",
2736 DUMP_PREFIX_OFFSET, 16, 1,
2737 msg->front.iov_base, msg->front.iov_len, true);
2738 if (msg->middle)
2739 print_hex_dump(KERN_DEBUG, "middle: ",
2740 DUMP_PREFIX_OFFSET, 16, 1,
2741 msg->middle->vec.iov_base,
2742 msg->middle->vec.iov_len, true);
2743 print_hex_dump(KERN_DEBUG, "footer: ",
2744 DUMP_PREFIX_OFFSET, 16, 1,
2745 &msg->footer, sizeof(msg->footer), true);
2746}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002747EXPORT_SYMBOL(ceph_msg_dump);