blob: 4578e99bbef1543cf6371e1a4e11f1afc3267466 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil31b80062009-10-06 11:31:13 -07002
3#include <linux/crc32c.h>
4#include <linux/ctype.h>
5#include <linux/highmem.h>
6#include <linux/inet.h>
7#include <linux/kthread.h>
8#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Sage Weil31b80062009-10-06 11:31:13 -070010#include <linux/socket.h>
11#include <linux/string.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070012#include <linux/bio.h>
13#include <linux/blkdev.h>
Noah Watkinsee3b56f2011-09-23 11:48:42 -070014#include <linux/dns_resolver.h>
Sage Weil31b80062009-10-06 11:31:13 -070015#include <net/tcp.h>
16
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070017#include <linux/ceph/libceph.h>
18#include <linux/ceph/messenger.h>
19#include <linux/ceph/decode.h>
20#include <linux/ceph/pagelist.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040021#include <linux/export.h>
Sage Weil31b80062009-10-06 11:31:13 -070022
23/*
24 * Ceph uses the messenger to exchange ceph_msg messages with other
25 * hosts in the system. The messenger provides ordered and reliable
26 * delivery. We tolerate TCP disconnects by reconnecting (with
27 * exponential backoff) in the case of a fault (disconnection, bad
28 * crc, protocol error). Acks allow sent messages to be discarded by
29 * the sender.
30 */
31
Alex Elderce2c8902012-05-22 22:15:49 -050032/* State values for ceph_connection->sock_state; NEW is assumed to be 0 */
33
34#define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
35#define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
36#define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
37#define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
38#define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
39
Sage Weil31b80062009-10-06 11:31:13 -070040/* static tag bytes (protocol control messages) */
41static char tag_msg = CEPH_MSGR_TAG_MSG;
42static char tag_ack = CEPH_MSGR_TAG_ACK;
43static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
44
Sage Weila6a53492010-04-13 14:07:07 -070045#ifdef CONFIG_LOCKDEP
46static struct lock_class_key socket_class;
47#endif
48
Alex Elder84495f42012-02-15 07:43:55 -060049/*
50 * When skipping (ignoring) a block of input we read it into a "skip
51 * buffer," which is this many bytes in size.
52 */
53#define SKIP_BUF_SIZE 1024
Sage Weil31b80062009-10-06 11:31:13 -070054
55static void queue_con(struct ceph_connection *con);
56static void con_work(struct work_struct *);
57static void ceph_fault(struct ceph_connection *con);
58
Sage Weil31b80062009-10-06 11:31:13 -070059/*
Alex Elderf64a9312012-01-23 15:49:27 -060060 * Nicely render a sockaddr as a string. An array of formatted
61 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -070062 */
Alex Elderf64a9312012-01-23 15:49:27 -060063#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
64#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
65#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
66#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
67
68static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
69static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -070070
Alex Elder57666512012-01-23 15:49:27 -060071static struct page *zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -060072
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070073const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -070074{
75 int i;
76 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -060077 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
78 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -070079
Alex Elderf64a9312012-01-23 15:49:27 -060080 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -070081 s = addr_str[i];
82
83 switch (ss->ss_family) {
84 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -060085 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
86 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -070087 break;
88
89 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -060090 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
91 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -070092 break;
93
94 default:
Alex Elderd3002b92012-02-14 14:05:33 -060095 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
96 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -070097 }
98
99 return s;
100}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700101EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700102
Sage Weil63f2d212009-11-03 15:17:56 -0800103static void encode_my_addr(struct ceph_messenger *msgr)
104{
105 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
106 ceph_encode_addr(&msgr->my_enc_addr);
107}
108
Sage Weil31b80062009-10-06 11:31:13 -0700109/*
110 * work queue for all reading and writing to/from the socket.
111 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600112static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700113
Alex Elder6173d1f2012-02-14 14:05:33 -0600114void _ceph_msgr_exit(void)
115{
Alex Elderd3002b92012-02-14 14:05:33 -0600116 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600117 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600118 ceph_msgr_wq = NULL;
119 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600120
Alex Elder6173d1f2012-02-14 14:05:33 -0600121 BUG_ON(zero_page == NULL);
122 kunmap(zero_page);
123 page_cache_release(zero_page);
124 zero_page = NULL;
125}
126
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700127int ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700128{
Alex Elder57666512012-01-23 15:49:27 -0600129 BUG_ON(zero_page != NULL);
130 zero_page = ZERO_PAGE(0);
131 page_cache_get(zero_page);
132
Tejun Heof363e452011-01-03 14:49:46 +0100133 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600134 if (ceph_msgr_wq)
135 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600136
Alex Elder6173d1f2012-02-14 14:05:33 -0600137 pr_err("msgr_init failed to create workqueue\n");
138 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600139
Alex Elder6173d1f2012-02-14 14:05:33 -0600140 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700141}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700142EXPORT_SYMBOL(ceph_msgr_init);
Sage Weil31b80062009-10-06 11:31:13 -0700143
144void ceph_msgr_exit(void)
145{
Alex Elder57666512012-01-23 15:49:27 -0600146 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600147
Alex Elder6173d1f2012-02-14 14:05:33 -0600148 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700149}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700150EXPORT_SYMBOL(ceph_msgr_exit);
Sage Weil31b80062009-10-06 11:31:13 -0700151
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700152void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700153{
154 flush_workqueue(ceph_msgr_wq);
155}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700156EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700157
Alex Elderce2c8902012-05-22 22:15:49 -0500158/* Connection socket state transition functions */
159
160static void con_sock_state_init(struct ceph_connection *con)
161{
162 int old_state;
163
164 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
165 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
166 printk("%s: unexpected old state %d\n", __func__, old_state);
167}
168
169static void con_sock_state_connecting(struct ceph_connection *con)
170{
171 int old_state;
172
173 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
174 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
175 printk("%s: unexpected old state %d\n", __func__, old_state);
176}
177
178static void con_sock_state_connected(struct ceph_connection *con)
179{
180 int old_state;
181
182 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
183 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
184 printk("%s: unexpected old state %d\n", __func__, old_state);
185}
186
187static void con_sock_state_closing(struct ceph_connection *con)
188{
189 int old_state;
190
191 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
192 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
193 old_state != CON_SOCK_STATE_CONNECTED &&
194 old_state != CON_SOCK_STATE_CLOSING))
195 printk("%s: unexpected old state %d\n", __func__, old_state);
196}
197
198static void con_sock_state_closed(struct ceph_connection *con)
199{
200 int old_state;
201
202 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
203 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
204 old_state != CON_SOCK_STATE_CLOSING))
205 printk("%s: unexpected old state %d\n", __func__, old_state);
206}
Sage Weila922d382010-05-29 09:41:23 -0700207
Sage Weil31b80062009-10-06 11:31:13 -0700208/*
209 * socket callback functions
210 */
211
212/* data available on socket, or listen socket received a connect */
Alex Elder327800b2012-05-22 11:41:43 -0500213static void ceph_sock_data_ready(struct sock *sk, int count_unused)
Sage Weil31b80062009-10-06 11:31:13 -0700214{
Alex Elderbd406142012-01-23 15:49:27 -0600215 struct ceph_connection *con = sk->sk_user_data;
216
Sage Weil31b80062009-10-06 11:31:13 -0700217 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500218 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700219 con, con->state);
220 queue_con(con);
221 }
222}
223
224/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500225static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700226{
Alex Elderd3002b92012-02-14 14:05:33 -0600227 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700228
Jim Schutt182fac22012-02-29 08:30:58 -0700229 /* only queue to workqueue if there is data we want to write,
230 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500231 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700232 * doesn't get called again until try_write() fills the socket
233 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
234 * and net/core/stream.c:sk_stream_write_space().
235 */
Alex Elder928443c2012-05-22 11:41:43 -0500236 if (test_bit(WRITE_PENDING, &con->flags)) {
Jim Schutt182fac22012-02-29 08:30:58 -0700237 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500238 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700239 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
240 queue_con(con);
241 }
Sage Weil31b80062009-10-06 11:31:13 -0700242 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500243 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700244 }
Sage Weil31b80062009-10-06 11:31:13 -0700245}
246
247/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500248static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700249{
Alex Elderbd406142012-01-23 15:49:27 -0600250 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700251
Alex Elder327800b2012-05-22 11:41:43 -0500252 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700253 con, con->state, sk->sk_state);
254
255 if (test_bit(CLOSED, &con->state))
256 return;
257
258 switch (sk->sk_state) {
259 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500260 dout("%s TCP_CLOSE\n", __func__);
Sage Weil31b80062009-10-06 11:31:13 -0700261 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500262 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500263 con_sock_state_closing(con);
Alex 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 Eldera5bc3129a2012-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 Eldera5bc3129a2012-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);
Alex Eldere27947c2012-05-23 14:35:23 -0500466 clear_bit(CONNECTED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700467 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Alex Eldera5988c42012-05-29 11:04:58 -0500468 set_bit(CLOSED, &con->state);
469
Alex Elder928443c2012-05-22 11:41:43 -0500470 clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */
471 clear_bit(KEEPALIVE_PENDING, &con->flags);
472 clear_bit(WRITE_PENDING, &con->flags);
Alex Eldera5988c42012-05-29 11:04:58 -0500473
Sage Weilec302642009-12-22 10:43:42 -0800474 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700475 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700476 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800477 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800478 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700479 queue_con(con);
480}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700481EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700482
483/*
Sage Weil31b80062009-10-06 11:31:13 -0700484 * Reopen a closed connection, with a new peer address.
485 */
486void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
487{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700488 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700489 set_bit(OPENING, &con->state);
Alex Eldera5988c42012-05-29 11:04:58 -0500490 WARN_ON(!test_and_clear_bit(CLOSED, &con->state));
491
Sage Weil31b80062009-10-06 11:31:13 -0700492 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800493 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700494 queue_con(con);
495}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700496EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700497
498/*
Sage Weil87b315a2010-03-22 14:51:18 -0700499 * return true if this connection ever successfully opened
500 */
501bool ceph_con_opened(struct ceph_connection *con)
502{
503 return con->connect_seq > 0;
504}
505
506/*
Sage Weil31b80062009-10-06 11:31:13 -0700507 * initialize a new connection.
508 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500509void ceph_con_init(struct ceph_connection *con, void *private,
510 const struct ceph_connection_operations *ops,
511 struct ceph_messenger *msgr, __u8 entity_type, __u64 entity_num)
Sage Weil31b80062009-10-06 11:31:13 -0700512{
513 dout("con_init %p\n", con);
514 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500515 con->private = private;
516 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700517 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500518
519 con_sock_state_init(con);
520
Alex Elder1bfd89f2012-05-26 23:26:43 -0500521 con->peer_name.type = (__u8) entity_type;
522 con->peer_name.num = cpu_to_le64(entity_num);
523
Sage Weilec302642009-12-22 10:43:42 -0800524 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700525 INIT_LIST_HEAD(&con->out_queue);
526 INIT_LIST_HEAD(&con->out_sent);
527 INIT_DELAYED_WORK(&con->work, con_work);
Alex Eldera5988c42012-05-29 11:04:58 -0500528
529 set_bit(CLOSED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700530}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700531EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700532
533
534/*
535 * We maintain a global counter to order connection attempts. Get
536 * a unique seq greater than @gt.
537 */
538static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
539{
540 u32 ret;
541
542 spin_lock(&msgr->global_seq_lock);
543 if (msgr->global_seq < gt)
544 msgr->global_seq = gt;
545 ret = ++msgr->global_seq;
546 spin_unlock(&msgr->global_seq_lock);
547 return ret;
548}
549
Alex Eldere2200422012-05-23 14:35:23 -0500550static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600551{
552 con->out_kvec_left = 0;
553 con->out_kvec_bytes = 0;
554 con->out_kvec_cur = &con->out_kvec[0];
555}
556
Alex Eldere2200422012-05-23 14:35:23 -0500557static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600558 size_t size, void *data)
559{
560 int index;
561
562 index = con->out_kvec_left;
563 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
564
565 con->out_kvec[index].iov_len = size;
566 con->out_kvec[index].iov_base = data;
567 con->out_kvec_left++;
568 con->out_kvec_bytes += size;
569}
Sage Weil31b80062009-10-06 11:31:13 -0700570
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500571#ifdef CONFIG_BLOCK
572static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
573{
574 if (!bio) {
575 *iter = NULL;
576 *seg = 0;
577 return;
578 }
579 *iter = bio;
580 *seg = bio->bi_idx;
581}
582
583static void iter_bio_next(struct bio **bio_iter, int *seg)
584{
585 if (*bio_iter == NULL)
586 return;
587
588 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
589
590 (*seg)++;
591 if (*seg == (*bio_iter)->bi_vcnt)
592 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
593}
594#endif
595
Alex Elder739c9052012-06-11 14:57:13 -0500596static void prepare_write_message_data(struct ceph_connection *con)
597{
598 struct ceph_msg *msg = con->out_msg;
599
600 BUG_ON(!msg);
601 BUG_ON(!msg->hdr.data_len);
602
603 /* initialize page iterator */
604 con->out_msg_pos.page = 0;
605 if (msg->pages)
606 con->out_msg_pos.page_pos = msg->page_alignment;
607 else
608 con->out_msg_pos.page_pos = 0;
Alex Elder572c5882012-06-11 14:57:13 -0500609#ifdef CONFIG_BLOCK
Alex Elderabdaa6a2012-06-11 14:57:13 -0500610 if (msg->bio)
Alex Elder572c5882012-06-11 14:57:13 -0500611 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
612#endif
Alex Elder739c9052012-06-11 14:57:13 -0500613 con->out_msg_pos.data_pos = 0;
614 con->out_msg_pos.did_page_crc = false;
615 con->out_more = 1; /* data + footer will follow */
616}
617
Sage Weil31b80062009-10-06 11:31:13 -0700618/*
619 * Prepare footer for currently outgoing message, and finish things
620 * off. Assumes out_kvec* are already valid.. we just add on to the end.
621 */
Alex Elder859eb792012-02-14 14:05:33 -0600622static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700623{
624 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600625 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700626
Alex Elderfd154f32012-06-11 14:57:13 -0500627 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
628
Sage Weil31b80062009-10-06 11:31:13 -0700629 dout("prepare_write_message_footer %p\n", con);
630 con->out_kvec_is_msg = true;
631 con->out_kvec[v].iov_base = &m->footer;
632 con->out_kvec[v].iov_len = sizeof(m->footer);
633 con->out_kvec_bytes += sizeof(m->footer);
634 con->out_kvec_left++;
635 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800636 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700637}
638
639/*
640 * Prepare headers for the next outgoing message.
641 */
642static void prepare_write_message(struct ceph_connection *con)
643{
644 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600645 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700646
Alex Eldere2200422012-05-23 14:35:23 -0500647 con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700648 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800649 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700650
651 /* Sneak an ack in there first? If we can get it into the same
652 * TCP packet that's a good thing. */
653 if (con->in_seq > con->in_seq_acked) {
654 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -0500655 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700656 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500657 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600658 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700659 }
660
Alex Elder38941f82012-06-01 14:56:43 -0500661 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -0600662 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800663 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -0500664 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -0700665
666 /* put message on sent list */
667 ceph_msg_get(m);
668 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700669
Sage Weile84346b2010-05-11 21:20:38 -0700670 /*
671 * only assign outgoing seq # if we haven't sent this message
672 * yet. if it is requeued, resend with it's original seq.
673 */
674 if (m->needs_out_seq) {
675 m->hdr.seq = cpu_to_le64(++con->out_seq);
676 m->needs_out_seq = false;
677 }
Sage Weil31b80062009-10-06 11:31:13 -0700678
679 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
680 m, con->out_seq, le16_to_cpu(m->hdr.type),
681 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
682 le32_to_cpu(m->hdr.data_len),
683 m->nr_pages);
684 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
685
686 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -0500687 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
688 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
689 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600690
Sage Weil31b80062009-10-06 11:31:13 -0700691 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -0500692 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600693 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700694
695 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600696 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
697 con->out_msg->hdr.crc = cpu_to_le32(crc);
Alex Elderfd154f32012-06-11 14:57:13 -0500698 con->out_msg->footer.flags = 0;
Alex Eldera9a0c512012-02-15 07:43:54 -0600699
700 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
701 con->out_msg->footer.front_crc = cpu_to_le32(crc);
702 if (m->middle) {
703 crc = crc32c(0, m->middle->vec.iov_base,
704 m->middle->vec.iov_len);
705 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
706 } else
Sage Weil31b80062009-10-06 11:31:13 -0700707 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -0500708 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700709 le32_to_cpu(con->out_msg->footer.front_crc),
710 le32_to_cpu(con->out_msg->footer.middle_crc));
711
712 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -0500713 con->out_msg->footer.data_crc = 0;
714 if (m->hdr.data_len)
715 prepare_write_message_data(con);
716 else
Sage Weil31b80062009-10-06 11:31:13 -0700717 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600718 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700719
Alex Elder928443c2012-05-22 11:41:43 -0500720 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700721}
722
723/*
724 * Prepare an ack.
725 */
726static void prepare_write_ack(struct ceph_connection *con)
727{
728 dout("prepare_write_ack %p %llu -> %llu\n", con,
729 con->in_seq_acked, con->in_seq);
730 con->in_seq_acked = con->in_seq;
731
Alex Eldere2200422012-05-23 14:35:23 -0500732 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600733
Alex Eldere2200422012-05-23 14:35:23 -0500734 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600735
Sage Weil31b80062009-10-06 11:31:13 -0700736 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500737 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600738 &con->out_temp_ack);
739
Sage Weil31b80062009-10-06 11:31:13 -0700740 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elder928443c2012-05-22 11:41:43 -0500741 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700742}
743
744/*
745 * Prepare to write keepalive byte.
746 */
747static void prepare_write_keepalive(struct ceph_connection *con)
748{
749 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -0500750 con_out_kvec_reset(con);
751 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Alex Elder928443c2012-05-22 11:41:43 -0500752 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700753}
754
755/*
756 * Connection negotiation.
757 */
758
Alex Elderdac1e712012-05-16 15:16:39 -0500759static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
760 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800761{
Alex Eldera3530df2012-05-16 15:16:39 -0500762 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -0500763
764 if (!con->ops->get_authorizer) {
765 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
766 con->out_connect.authorizer_len = 0;
767
Alex Elder729796b2012-05-16 15:16:39 -0500768 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -0500769 }
770
771 /* Can't hold the mutex while getting authorizer */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800772
Sage Weilec302642009-12-22 10:43:42 -0800773 mutex_unlock(&con->mutex);
Alex Elderb1c6b982012-05-16 15:16:38 -0500774
Alex Elderdac1e712012-05-16 15:16:39 -0500775 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Elder8f43fb52012-05-16 15:16:39 -0500776
Sage Weilec302642009-12-22 10:43:42 -0800777 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800778
Alex Eldera3530df2012-05-16 15:16:39 -0500779 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -0500780 return auth;
Alex Elder928443c2012-05-22 11:41:43 -0500781 if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags))
Alex Elder729796b2012-05-16 15:16:39 -0500782 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700783
Alex Elder8f43fb52012-05-16 15:16:39 -0500784 con->auth_reply_buf = auth->authorizer_reply_buf;
785 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
786
Alex Elder859eb792012-02-14 14:05:33 -0600787
Alex Elder729796b2012-05-16 15:16:39 -0500788 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800789}
790
Sage Weil31b80062009-10-06 11:31:13 -0700791/*
792 * We connected to a peer and are saying hello.
793 */
Alex Eldere825a662012-05-16 15:16:38 -0500794static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700795{
Alex Eldere2200422012-05-23 14:35:23 -0500796 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
797 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -0500798 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800799
Sage Weileed0ef22009-11-10 14:34:36 -0800800 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500801 set_bit(WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800802}
803
Alex Eldere825a662012-05-16 15:16:38 -0500804static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800805{
Eric Dumazet95c96172012-04-15 05:58:06 +0000806 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -0700807 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -0500808 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -0500809 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700810
811 switch (con->peer_name.type) {
812 case CEPH_ENTITY_TYPE_MON:
813 proto = CEPH_MONC_PROTOCOL;
814 break;
815 case CEPH_ENTITY_TYPE_OSD:
816 proto = CEPH_OSDC_PROTOCOL;
817 break;
818 case CEPH_ENTITY_TYPE_MDS:
819 proto = CEPH_MDSC_PROTOCOL;
820 break;
821 default:
822 BUG();
823 }
824
825 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
826 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800827
Alex Eldere825a662012-05-16 15:16:38 -0500828 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700829 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
830 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
831 con->out_connect.global_seq = cpu_to_le32(global_seq);
832 con->out_connect.protocol_version = cpu_to_le32(proto);
833 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700834
Alex Elderdac1e712012-05-16 15:16:39 -0500835 auth_proto = CEPH_AUTH_UNKNOWN;
836 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -0500837 if (IS_ERR(auth))
838 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -0500839
Alex Elderdac1e712012-05-16 15:16:39 -0500840 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -0500841 con->out_connect.authorizer_len = auth ?
842 cpu_to_le32(auth->authorizer_buf_len) : 0;
843
Alex Elderab166d52012-05-31 11:37:29 -0500844 con_out_kvec_reset(con);
Alex Eldere2200422012-05-23 14:35:23 -0500845 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -0500846 &con->out_connect);
847 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -0500848 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -0500849 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600850
Sage Weil31b80062009-10-06 11:31:13 -0700851 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500852 set_bit(WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800853
Alex Eldere10c7582012-05-16 15:16:38 -0500854 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700855}
856
Sage Weil31b80062009-10-06 11:31:13 -0700857/*
858 * write as much of pending kvecs to the socket as we can.
859 * 1 -> done
860 * 0 -> socket full, but more to do
861 * <0 -> error
862 */
863static int write_partial_kvec(struct ceph_connection *con)
864{
865 int ret;
866
867 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
868 while (con->out_kvec_bytes > 0) {
869 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
870 con->out_kvec_left, con->out_kvec_bytes,
871 con->out_more);
872 if (ret <= 0)
873 goto out;
874 con->out_kvec_bytes -= ret;
875 if (con->out_kvec_bytes == 0)
876 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600877
878 /* account for full iov entries consumed */
879 while (ret >= con->out_kvec_cur->iov_len) {
880 BUG_ON(!con->out_kvec_left);
881 ret -= con->out_kvec_cur->iov_len;
882 con->out_kvec_cur++;
883 con->out_kvec_left--;
884 }
885 /* and for a partially-consumed entry */
886 if (ret) {
887 con->out_kvec_cur->iov_len -= ret;
888 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700889 }
890 }
891 con->out_kvec_left = 0;
892 con->out_kvec_is_msg = false;
893 ret = 1;
894out:
895 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
896 con->out_kvec_bytes, con->out_kvec_left, ret);
897 return ret; /* done! */
898}
899
Alex Elder84ca8fc2012-06-11 14:57:13 -0500900static void out_msg_pos_next(struct ceph_connection *con, struct page *page,
901 size_t len, size_t sent, bool in_trail)
902{
903 struct ceph_msg *msg = con->out_msg;
904
905 BUG_ON(!msg);
906 BUG_ON(!sent);
907
908 con->out_msg_pos.data_pos += sent;
909 con->out_msg_pos.page_pos += sent;
Alex Elder5821bd82012-06-11 14:57:13 -0500910 if (sent < len)
911 return;
912
913 BUG_ON(sent != len);
914 con->out_msg_pos.page_pos = 0;
915 con->out_msg_pos.page++;
916 con->out_msg_pos.did_page_crc = false;
917 if (in_trail)
918 list_move_tail(&page->lru,
919 &msg->trail->head);
920 else if (msg->pagelist)
921 list_move_tail(&page->lru,
922 &msg->pagelist->head);
Alex Elder84ca8fc2012-06-11 14:57:13 -0500923#ifdef CONFIG_BLOCK
Alex Elder5821bd82012-06-11 14:57:13 -0500924 else if (msg->bio)
925 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
Alex Elder84ca8fc2012-06-11 14:57:13 -0500926#endif
Alex Elder84ca8fc2012-06-11 14:57:13 -0500927}
928
Sage Weil31b80062009-10-06 11:31:13 -0700929/*
930 * Write as much message data payload as we can. If we finish, queue
931 * up the footer.
932 * 1 -> done, footer is now queued in out_kvec[].
933 * 0 -> socket full, but more to do
934 * <0 -> error
935 */
936static int write_partial_msg_pages(struct ceph_connection *con)
937{
938 struct ceph_msg *msg = con->out_msg;
Eric Dumazet95c96172012-04-15 05:58:06 +0000939 unsigned int data_len = le32_to_cpu(msg->hdr.data_len);
Sage Weil31b80062009-10-06 11:31:13 -0700940 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600941 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700942 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700943 int total_max_write;
Alex Elder84ca8fc2012-06-11 14:57:13 -0500944 bool in_trail = false;
Alex Elder5821bd82012-06-11 14:57:13 -0500945 const size_t trail_len = (msg->trail ? msg->trail->length : 0);
946 const size_t trail_off = data_len - trail_len;
Sage Weil31b80062009-10-06 11:31:13 -0700947
948 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
Alex Elder84ca8fc2012-06-11 14:57:13 -0500949 con, msg, con->out_msg_pos.page, msg->nr_pages,
Sage Weil31b80062009-10-06 11:31:13 -0700950 con->out_msg_pos.page_pos);
951
Alex Elder5821bd82012-06-11 14:57:13 -0500952 /*
953 * Iterate through each page that contains data to be
954 * written, and send as much as possible for each.
955 *
956 * If we are calculating the data crc (the default), we will
957 * need to map the page. If we have no pages, they have
958 * been revoked, so use the zero page.
959 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700960 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -0700961 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700962 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -0600963 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700964
Alex Elder5821bd82012-06-11 14:57:13 -0500965 in_trail = in_trail || con->out_msg_pos.data_pos >= trail_off;
966 if (!in_trail)
967 total_max_write = trail_off - con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -0700968
Alex Elder5821bd82012-06-11 14:57:13 -0500969 if (in_trail) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700970 total_max_write = data_len - con->out_msg_pos.data_pos;
971
972 page = list_first_entry(&msg->trail->head,
973 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700974 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -0700975 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -0800976 } else if (msg->pagelist) {
977 page = list_first_entry(&msg->pagelist->head,
978 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700979#ifdef CONFIG_BLOCK
980 } else if (msg->bio) {
981 struct bio_vec *bv;
982
983 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
984 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -0600985 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700986 max_write = bv->bv_len;
987#endif
Sage Weil31b80062009-10-06 11:31:13 -0700988 } else {
Alex Elder57666512012-01-23 15:49:27 -0600989 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -0700990 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700991 len = min_t(int, max_write - con->out_msg_pos.page_pos,
992 total_max_write);
993
Alex Elder37675b02012-03-07 11:40:08 -0600994 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -0600995 void *base;
Alex Elder5821bd82012-06-11 14:57:13 -0500996 u32 crc = le32_to_cpu(msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -0600997 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -0700998
Alex Elder8d63e312012-03-07 11:40:08 -0600999 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -07001000 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -06001001 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Elder5821bd82012-06-11 14:57:13 -05001002 crc = crc32c(crc, base, len);
Alex Elder84ca8fc2012-06-11 14:57:13 -05001003 msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -06001004 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -07001005 }
Alex Eldere36b13c2012-03-07 11:40:08 -06001006 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -06001007 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -06001008 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001009
Alex Elder0cdf9e62012-03-07 11:40:08 -06001010 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -07001011 kunmap(page);
1012
1013 if (ret <= 0)
1014 goto out;
1015
Alex Elder84ca8fc2012-06-11 14:57:13 -05001016 out_msg_pos_next(con, page, len, (size_t) ret, in_trail);
Sage Weil31b80062009-10-06 11:31:13 -07001017 }
1018
1019 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1020
1021 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001022 if (!do_datacrc)
Alex Elder84ca8fc2012-06-11 14:57:13 -05001023 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001024 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001025 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001026 ret = 1;
1027out:
1028 return ret;
1029}
1030
1031/*
1032 * write some zeros
1033 */
1034static int write_partial_skip(struct ceph_connection *con)
1035{
1036 int ret;
1037
1038 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001039 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001040
Alex Elder31739132012-03-07 11:40:08 -06001041 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001042 if (ret <= 0)
1043 goto out;
1044 con->out_skip -= ret;
1045 }
1046 ret = 1;
1047out:
1048 return ret;
1049}
1050
1051/*
1052 * Prepare to read connection handshake, or an ack.
1053 */
Sage Weileed0ef22009-11-10 14:34:36 -08001054static void prepare_read_banner(struct ceph_connection *con)
1055{
1056 dout("prepare_read_banner %p\n", con);
1057 con->in_base_pos = 0;
1058}
1059
Sage Weil31b80062009-10-06 11:31:13 -07001060static void prepare_read_connect(struct ceph_connection *con)
1061{
1062 dout("prepare_read_connect %p\n", con);
1063 con->in_base_pos = 0;
1064}
1065
1066static void prepare_read_ack(struct ceph_connection *con)
1067{
1068 dout("prepare_read_ack %p\n", con);
1069 con->in_base_pos = 0;
1070}
1071
1072static void prepare_read_tag(struct ceph_connection *con)
1073{
1074 dout("prepare_read_tag %p\n", con);
1075 con->in_base_pos = 0;
1076 con->in_tag = CEPH_MSGR_TAG_READY;
1077}
1078
1079/*
1080 * Prepare to read a message.
1081 */
1082static int prepare_read_message(struct ceph_connection *con)
1083{
1084 dout("prepare_read_message %p\n", con);
1085 BUG_ON(con->in_msg != NULL);
1086 con->in_base_pos = 0;
1087 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1088 return 0;
1089}
1090
1091
1092static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001093 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001094{
Alex Eldere6cee712012-05-10 10:29:50 -05001095 while (con->in_base_pos < end) {
1096 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001097 int have = size - left;
1098 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1099 if (ret <= 0)
1100 return ret;
1101 con->in_base_pos += ret;
1102 }
1103 return 1;
1104}
1105
1106
1107/*
1108 * Read all or part of the connect-side handshake on a new connection
1109 */
Sage Weileed0ef22009-11-10 14:34:36 -08001110static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001111{
Alex Elderfd516532012-05-10 10:29:50 -05001112 int size;
1113 int end;
1114 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001115
Sage Weileed0ef22009-11-10 14:34:36 -08001116 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001117
1118 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001119 size = strlen(CEPH_BANNER);
1120 end = size;
1121 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001122 if (ret <= 0)
1123 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001124
1125 size = sizeof (con->actual_peer_addr);
1126 end += size;
1127 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001128 if (ret <= 0)
1129 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001130
1131 size = sizeof (con->peer_addr_for_me);
1132 end += size;
1133 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001134 if (ret <= 0)
1135 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001136
Sage Weileed0ef22009-11-10 14:34:36 -08001137out:
1138 return ret;
1139}
1140
1141static int read_partial_connect(struct ceph_connection *con)
1142{
Alex Elderfd516532012-05-10 10:29:50 -05001143 int size;
1144 int end;
1145 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001146
1147 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1148
Alex Elderfd516532012-05-10 10:29:50 -05001149 size = sizeof (con->in_reply);
1150 end = size;
1151 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001152 if (ret <= 0)
1153 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001154
1155 size = le32_to_cpu(con->in_reply.authorizer_len);
1156 end += size;
1157 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001158 if (ret <= 0)
1159 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001160
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001161 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1162 con, (int)con->in_reply.tag,
1163 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001164 le32_to_cpu(con->in_reply.global_seq));
1165out:
1166 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001167
Sage Weil31b80062009-10-06 11:31:13 -07001168}
1169
1170/*
1171 * Verify the hello banner looks okay.
1172 */
1173static int verify_hello(struct ceph_connection *con)
1174{
1175 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001176 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001177 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001178 con->error_msg = "protocol error, bad banner";
1179 return -1;
1180 }
1181 return 0;
1182}
1183
1184static bool addr_is_blank(struct sockaddr_storage *ss)
1185{
1186 switch (ss->ss_family) {
1187 case AF_INET:
1188 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1189 case AF_INET6:
1190 return
1191 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1192 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1193 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1194 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1195 }
1196 return false;
1197}
1198
1199static int addr_port(struct sockaddr_storage *ss)
1200{
1201 switch (ss->ss_family) {
1202 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001203 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001204 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001205 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001206 }
1207 return 0;
1208}
1209
1210static void addr_set_port(struct sockaddr_storage *ss, int p)
1211{
1212 switch (ss->ss_family) {
1213 case AF_INET:
1214 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001215 break;
Sage Weil31b80062009-10-06 11:31:13 -07001216 case AF_INET6:
1217 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001218 break;
Sage Weil31b80062009-10-06 11:31:13 -07001219 }
1220}
1221
1222/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001223 * Unlike other *_pton function semantics, zero indicates success.
1224 */
1225static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1226 char delim, const char **ipend)
1227{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001228 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1229 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001230
1231 memset(ss, 0, sizeof(*ss));
1232
1233 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1234 ss->ss_family = AF_INET;
1235 return 0;
1236 }
1237
1238 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1239 ss->ss_family = AF_INET6;
1240 return 0;
1241 }
1242
1243 return -EINVAL;
1244}
1245
1246/*
1247 * Extract hostname string and resolve using kernel DNS facility.
1248 */
1249#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1250static int ceph_dns_resolve_name(const char *name, size_t namelen,
1251 struct sockaddr_storage *ss, char delim, const char **ipend)
1252{
1253 const char *end, *delim_p;
1254 char *colon_p, *ip_addr = NULL;
1255 int ip_len, ret;
1256
1257 /*
1258 * The end of the hostname occurs immediately preceding the delimiter or
1259 * the port marker (':') where the delimiter takes precedence.
1260 */
1261 delim_p = memchr(name, delim, namelen);
1262 colon_p = memchr(name, ':', namelen);
1263
1264 if (delim_p && colon_p)
1265 end = delim_p < colon_p ? delim_p : colon_p;
1266 else if (!delim_p && colon_p)
1267 end = colon_p;
1268 else {
1269 end = delim_p;
1270 if (!end) /* case: hostname:/ */
1271 end = name + namelen;
1272 }
1273
1274 if (end <= name)
1275 return -EINVAL;
1276
1277 /* do dns_resolve upcall */
1278 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1279 if (ip_len > 0)
1280 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1281 else
1282 ret = -ESRCH;
1283
1284 kfree(ip_addr);
1285
1286 *ipend = end;
1287
1288 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1289 ret, ret ? "failed" : ceph_pr_addr(ss));
1290
1291 return ret;
1292}
1293#else
1294static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1295 struct sockaddr_storage *ss, char delim, const char **ipend)
1296{
1297 return -EINVAL;
1298}
1299#endif
1300
1301/*
1302 * Parse a server name (IP or hostname). If a valid IP address is not found
1303 * then try to extract a hostname to resolve using userspace DNS upcall.
1304 */
1305static int ceph_parse_server_name(const char *name, size_t namelen,
1306 struct sockaddr_storage *ss, char delim, const char **ipend)
1307{
1308 int ret;
1309
1310 ret = ceph_pton(name, namelen, ss, delim, ipend);
1311 if (ret)
1312 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1313
1314 return ret;
1315}
1316
1317/*
Sage Weil31b80062009-10-06 11:31:13 -07001318 * Parse an ip[:port] list into an addr array. Use the default
1319 * monitor port if a port isn't specified.
1320 */
1321int ceph_parse_ips(const char *c, const char *end,
1322 struct ceph_entity_addr *addr,
1323 int max_count, int *count)
1324{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001325 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001326 const char *p = c;
1327
1328 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1329 for (i = 0; i < max_count; i++) {
1330 const char *ipend;
1331 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001332 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001333 char delim = ',';
1334
1335 if (*p == '[') {
1336 delim = ']';
1337 p++;
1338 }
Sage Weil31b80062009-10-06 11:31:13 -07001339
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001340 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1341 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001342 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001343 ret = -EINVAL;
1344
Sage Weil31b80062009-10-06 11:31:13 -07001345 p = ipend;
1346
Sage Weil39139f62010-07-08 09:54:52 -07001347 if (delim == ']') {
1348 if (*p != ']') {
1349 dout("missing matching ']'\n");
1350 goto bad;
1351 }
1352 p++;
1353 }
1354
Sage Weil31b80062009-10-06 11:31:13 -07001355 /* port? */
1356 if (p < end && *p == ':') {
1357 port = 0;
1358 p++;
1359 while (p < end && *p >= '0' && *p <= '9') {
1360 port = (port * 10) + (*p - '0');
1361 p++;
1362 }
1363 if (port > 65535 || port == 0)
1364 goto bad;
1365 } else {
1366 port = CEPH_MON_PORT;
1367 }
1368
1369 addr_set_port(ss, port);
1370
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001371 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001372
1373 if (p == end)
1374 break;
1375 if (*p != ',')
1376 goto bad;
1377 p++;
1378 }
1379
1380 if (p != end)
1381 goto bad;
1382
1383 if (count)
1384 *count = i + 1;
1385 return 0;
1386
1387bad:
Sage Weil39139f62010-07-08 09:54:52 -07001388 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001389 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001390}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001391EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001392
Sage Weileed0ef22009-11-10 14:34:36 -08001393static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001394{
Sage Weileed0ef22009-11-10 14:34:36 -08001395 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001396
1397 if (verify_hello(con) < 0)
1398 return -1;
1399
Sage Weil63f2d212009-11-03 15:17:56 -08001400 ceph_decode_addr(&con->actual_peer_addr);
1401 ceph_decode_addr(&con->peer_addr_for_me);
1402
Sage Weil31b80062009-10-06 11:31:13 -07001403 /*
1404 * Make sure the other end is who we wanted. note that the other
1405 * end may not yet know their ip address, so if it's 0.0.0.0, give
1406 * them the benefit of the doubt.
1407 */
Sage Weil103e2d32010-01-07 16:12:36 -08001408 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1409 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001410 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1411 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001412 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001413 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001414 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001415 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001416 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001417 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001418 return -1;
1419 }
1420
1421 /*
1422 * did we learn our address?
1423 */
1424 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1425 int port = addr_port(&con->msgr->inst.addr.in_addr);
1426
1427 memcpy(&con->msgr->inst.addr.in_addr,
1428 &con->peer_addr_for_me.in_addr,
1429 sizeof(con->peer_addr_for_me.in_addr));
1430 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001431 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001432 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001433 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001434 }
1435
Sage Weileed0ef22009-11-10 14:34:36 -08001436 return 0;
1437}
1438
Sage Weil04a419f2009-12-23 09:30:21 -08001439static void fail_protocol(struct ceph_connection *con)
1440{
1441 reset_connection(con);
1442 set_bit(CLOSED, &con->state); /* in case there's queued work */
Sage Weil04a419f2009-12-23 09:30:21 -08001443}
1444
Sage Weileed0ef22009-11-10 14:34:36 -08001445static int process_connect(struct ceph_connection *con)
1446{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001447 u64 sup_feat = con->msgr->supported_features;
1448 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001449 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001450 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001451
Sage Weileed0ef22009-11-10 14:34:36 -08001452 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1453
Sage Weil31b80062009-10-06 11:31:13 -07001454 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001455 case CEPH_MSGR_TAG_FEATURES:
1456 pr_err("%s%lld %s feature set mismatch,"
1457 " my %llx < server's %llx, missing %llx\n",
1458 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001459 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001460 sup_feat, server_feat, server_feat & ~sup_feat);
1461 con->error_msg = "missing required protocol features";
1462 fail_protocol(con);
1463 return -1;
1464
Sage Weil31b80062009-10-06 11:31:13 -07001465 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001466 pr_err("%s%lld %s protocol version mismatch,"
1467 " my %d != server's %d\n",
1468 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001469 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001470 le32_to_cpu(con->out_connect.protocol_version),
1471 le32_to_cpu(con->in_reply.protocol_version));
1472 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001473 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001474 return -1;
1475
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001476 case CEPH_MSGR_TAG_BADAUTHORIZER:
1477 con->auth_retry++;
1478 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1479 con->auth_retry);
1480 if (con->auth_retry == 2) {
1481 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001482 return -1;
1483 }
1484 con->auth_retry = 1;
Alex Eldere825a662012-05-16 15:16:38 -05001485 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001486 if (ret < 0)
1487 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001488 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001489 break;
Sage Weil31b80062009-10-06 11:31:13 -07001490
1491 case CEPH_MSGR_TAG_RESETSESSION:
1492 /*
1493 * If we connected with a large connect_seq but the peer
1494 * has no record of a session with us (no connection, or
1495 * connect_seq == 0), they will send RESETSESION to indicate
1496 * that they must have reset their session, and may have
1497 * dropped messages.
1498 */
1499 dout("process_connect got RESET peer seq %u\n",
1500 le32_to_cpu(con->in_connect.connect_seq));
1501 pr_err("%s%lld %s connection reset\n",
1502 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001503 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001504 reset_connection(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 Elder5a0f8fd2012-05-16 21:51:59 -05001530 ret = prepare_write_connect(con);
1531 if (ret < 0)
1532 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001533 prepare_read_connect(con);
1534 break;
1535
1536 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1537 /*
1538 * If we sent a smaller global_seq than the peer has, try
1539 * again with a larger value.
1540 */
Sage Weileed0ef22009-11-10 14:34:36 -08001541 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001542 con->peer_global_seq,
1543 le32_to_cpu(con->in_connect.global_seq));
1544 get_global_seq(con->msgr,
1545 le32_to_cpu(con->in_connect.global_seq));
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001546 ret = prepare_write_connect(con);
1547 if (ret < 0)
1548 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001549 prepare_read_connect(con);
1550 break;
1551
1552 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001553 if (req_feat & ~server_feat) {
1554 pr_err("%s%lld %s protocol feature mismatch,"
1555 " my required %llx > server's %llx, need %llx\n",
1556 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001557 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001558 req_feat, server_feat, req_feat & ~server_feat);
1559 con->error_msg = "missing required protocol features";
1560 fail_protocol(con);
1561 return -1;
1562 }
Alex Elder3ec50d12012-05-23 14:35:23 -05001563 clear_bit(NEGOTIATING, &con->state);
Alex Eldere27947c2012-05-23 14:35:23 -05001564 set_bit(CONNECTED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001565 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1566 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001567 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001568 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1569 con->peer_global_seq,
1570 le32_to_cpu(con->in_reply.connect_seq),
1571 con->connect_seq);
1572 WARN_ON(con->connect_seq !=
1573 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001574
1575 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elder928443c2012-05-22 11:41:43 -05001576 set_bit(LOSSYTX, &con->flags);
Sage Weil92ac41d2009-12-14 14:56:56 -08001577
Sage Weil31b80062009-10-06 11:31:13 -07001578 prepare_read_tag(con);
1579 break;
1580
1581 case CEPH_MSGR_TAG_WAIT:
1582 /*
1583 * If there is a connection race (we are opening
1584 * connections to each other), one of us may just have
1585 * to WAIT. This shouldn't happen if we are the
1586 * client.
1587 */
Sage Weil04177882011-05-12 15:33:17 -07001588 pr_err("process_connect got WAIT as client\n");
1589 con->error_msg = "protocol error, got WAIT as client";
1590 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001591
1592 default:
1593 pr_err("connect protocol error, will retry\n");
1594 con->error_msg = "protocol error, garbage tag during connect";
1595 return -1;
1596 }
1597 return 0;
1598}
1599
1600
1601/*
1602 * read (part of) an ack
1603 */
1604static int read_partial_ack(struct ceph_connection *con)
1605{
Alex Elderfd516532012-05-10 10:29:50 -05001606 int size = sizeof (con->in_temp_ack);
1607 int end = size;
1608
1609 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001610}
1611
1612
1613/*
1614 * We can finally discard anything that's been acked.
1615 */
1616static void process_ack(struct ceph_connection *con)
1617{
1618 struct ceph_msg *m;
1619 u64 ack = le64_to_cpu(con->in_temp_ack);
1620 u64 seq;
1621
Sage Weil31b80062009-10-06 11:31:13 -07001622 while (!list_empty(&con->out_sent)) {
1623 m = list_first_entry(&con->out_sent, struct ceph_msg,
1624 list_head);
1625 seq = le64_to_cpu(m->hdr.seq);
1626 if (seq > ack)
1627 break;
1628 dout("got ack for seq %llu type %d at %p\n", seq,
1629 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001630 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001631 ceph_msg_remove(m);
1632 }
Sage Weil31b80062009-10-06 11:31:13 -07001633 prepare_read_tag(con);
1634}
1635
1636
1637
1638
Yehuda Sadeh24504182010-01-08 13:58:34 -08001639static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001640 struct kvec *section,
1641 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001642{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001643 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001644
Yehuda Sadeh24504182010-01-08 13:58:34 -08001645 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001646
Yehuda Sadeh24504182010-01-08 13:58:34 -08001647 while (section->iov_len < sec_len) {
1648 BUG_ON(section->iov_base == NULL);
1649 left = sec_len - section->iov_len;
1650 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1651 section->iov_len, left);
1652 if (ret <= 0)
1653 return ret;
1654 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001655 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001656 if (section->iov_len == sec_len)
1657 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001658
1659 return 1;
1660}
1661
Alex Elder1c20f2d2012-06-04 14:43:32 -05001662static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
1663 struct ceph_msg_header *hdr);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001664
1665
1666static int read_partial_message_pages(struct ceph_connection *con,
1667 struct page **pages,
Eric Dumazet95c96172012-04-15 05:58:06 +00001668 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001669{
1670 void *p;
1671 int ret;
1672 int left;
1673
1674 left = min((int)(data_len - con->in_msg_pos.data_pos),
1675 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1676 /* (page) data */
1677 BUG_ON(pages == NULL);
1678 p = kmap(pages[con->in_msg_pos.page]);
1679 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1680 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001681 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001682 con->in_data_crc =
1683 crc32c(con->in_data_crc,
1684 p + con->in_msg_pos.page_pos, ret);
1685 kunmap(pages[con->in_msg_pos.page]);
1686 if (ret <= 0)
1687 return ret;
1688 con->in_msg_pos.data_pos += ret;
1689 con->in_msg_pos.page_pos += ret;
1690 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1691 con->in_msg_pos.page_pos = 0;
1692 con->in_msg_pos.page++;
1693 }
1694
1695 return ret;
1696}
1697
1698#ifdef CONFIG_BLOCK
1699static int read_partial_message_bio(struct ceph_connection *con,
1700 struct bio **bio_iter, int *bio_seg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001701 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001702{
1703 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1704 void *p;
1705 int ret, left;
1706
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001707 left = min((int)(data_len - con->in_msg_pos.data_pos),
1708 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1709
1710 p = kmap(bv->bv_page) + bv->bv_offset;
1711
1712 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1713 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001714 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001715 con->in_data_crc =
1716 crc32c(con->in_data_crc,
1717 p + con->in_msg_pos.page_pos, ret);
1718 kunmap(bv->bv_page);
1719 if (ret <= 0)
1720 return ret;
1721 con->in_msg_pos.data_pos += ret;
1722 con->in_msg_pos.page_pos += ret;
1723 if (con->in_msg_pos.page_pos == bv->bv_len) {
1724 con->in_msg_pos.page_pos = 0;
1725 iter_bio_next(bio_iter, bio_seg);
1726 }
1727
1728 return ret;
1729}
1730#endif
1731
Sage Weil31b80062009-10-06 11:31:13 -07001732/*
1733 * read (part of) a message.
1734 */
1735static int read_partial_message(struct ceph_connection *con)
1736{
1737 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05001738 int size;
1739 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001740 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00001741 unsigned int front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001742 bool do_datacrc = !con->msgr->nocrc;
Sage Weilae187562010-04-22 07:47:01 -07001743 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001744 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001745
1746 dout("read_partial_message con %p msg %p\n", con, m);
1747
1748 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05001749 size = sizeof (con->in_hdr);
1750 end = size;
1751 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05001752 if (ret <= 0)
1753 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001754
1755 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1756 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1757 pr_err("read_partial_message bad hdr "
1758 " crc %u != expected %u\n",
1759 crc, con->in_hdr.crc);
1760 return -EBADMSG;
1761 }
1762
Sage Weil31b80062009-10-06 11:31:13 -07001763 front_len = le32_to_cpu(con->in_hdr.front_len);
1764 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1765 return -EIO;
1766 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1767 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1768 return -EIO;
1769 data_len = le32_to_cpu(con->in_hdr.data_len);
1770 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1771 return -EIO;
1772
Sage Weilae187562010-04-22 07:47:01 -07001773 /* verify seq# */
1774 seq = le64_to_cpu(con->in_hdr.seq);
1775 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001776 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001777 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001778 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001779 seq, con->in_seq + 1);
1780 con->in_base_pos = -front_len - middle_len - data_len -
1781 sizeof(m->footer);
1782 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001783 return 0;
1784 } else if ((s64)seq - (s64)con->in_seq > 1) {
1785 pr_err("read_partial_message bad seq %lld expected %lld\n",
1786 seq, con->in_seq + 1);
1787 con->error_msg = "bad message sequence # for incoming message";
1788 return -EBADMSG;
1789 }
1790
Sage Weil31b80062009-10-06 11:31:13 -07001791 /* allocate message? */
1792 if (!con->in_msg) {
1793 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1794 con->in_hdr.front_len, con->in_hdr.data_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05001795 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
Sage Weil31b80062009-10-06 11:31:13 -07001796 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001797 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001798 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001799 con->in_base_pos = -front_len - middle_len - data_len -
1800 sizeof(m->footer);
1801 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001802 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001803 return 0;
1804 }
Sage Weila79832f2010-04-01 16:06:19 -07001805 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001806 con->error_msg =
1807 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001808 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001809 }
Alex Elder38941f82012-06-01 14:56:43 -05001810
1811 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07001812 m = con->in_msg;
1813 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001814 if (m->middle)
1815 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001816
1817 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001818 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001819 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001820 else
1821 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001822 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001823 }
1824
1825 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001826 ret = read_partial_message_section(con, &m->front, front_len,
1827 &con->in_front_crc);
1828 if (ret <= 0)
1829 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001830
1831 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001832 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001833 ret = read_partial_message_section(con, &m->middle->vec,
1834 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001835 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001836 if (ret <= 0)
1837 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001838 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001839#ifdef CONFIG_BLOCK
1840 if (m->bio && !m->bio_iter)
1841 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1842#endif
Sage Weil31b80062009-10-06 11:31:13 -07001843
1844 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001845 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001846 if (m->pages) {
1847 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001848 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001849 if (ret <= 0)
1850 return ret;
1851#ifdef CONFIG_BLOCK
1852 } else if (m->bio) {
1853
1854 ret = read_partial_message_bio(con,
1855 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001856 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001857 if (ret <= 0)
1858 return ret;
1859#endif
1860 } else {
1861 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001862 }
1863 }
1864
Sage Weil31b80062009-10-06 11:31:13 -07001865 /* footer */
Alex Elderfd516532012-05-10 10:29:50 -05001866 size = sizeof (m->footer);
1867 end += size;
1868 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05001869 if (ret <= 0)
1870 return ret;
1871
Sage Weil31b80062009-10-06 11:31:13 -07001872 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1873 m, front_len, m->footer.front_crc, middle_len,
1874 m->footer.middle_crc, data_len, m->footer.data_crc);
1875
1876 /* crc ok? */
1877 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1878 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1879 m, con->in_front_crc, m->footer.front_crc);
1880 return -EBADMSG;
1881 }
1882 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1883 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1884 m, con->in_middle_crc, m->footer.middle_crc);
1885 return -EBADMSG;
1886 }
Alex Elderbca064d2012-02-15 07:43:54 -06001887 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001888 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1889 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1890 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1891 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1892 return -EBADMSG;
1893 }
1894
1895 return 1; /* done! */
1896}
1897
1898/*
1899 * Process message. This happens in the worker thread. The callback should
1900 * be careful not to do anything that waits on other incoming messages or it
1901 * may deadlock.
1902 */
1903static void process_message(struct ceph_connection *con)
1904{
Sage Weil5e095e82009-12-14 14:30:34 -08001905 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001906
Alex Elder38941f82012-06-01 14:56:43 -05001907 BUG_ON(con->in_msg->con != con);
1908 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08001909 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001910 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07001911 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07001912
1913 /* if first message, set peer_name */
1914 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001915 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001916
Sage Weil31b80062009-10-06 11:31:13 -07001917 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001918 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001919
1920 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1921 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001922 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001923 le16_to_cpu(msg->hdr.type),
1924 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1925 le32_to_cpu(msg->hdr.front_len),
1926 le32_to_cpu(msg->hdr.data_len),
1927 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1928 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001929
1930 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001931 prepare_read_tag(con);
1932}
1933
1934
1935/*
1936 * Write something to the socket. Called in a worker thread when the
1937 * socket appears to be writeable and we have something ready to send.
1938 */
1939static int try_write(struct ceph_connection *con)
1940{
Sage Weil31b80062009-10-06 11:31:13 -07001941 int ret = 1;
1942
Sage Weild59315c2012-06-21 12:49:23 -07001943 dout("try_write start %p state %lu\n", con, con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001944
Sage Weil31b80062009-10-06 11:31:13 -07001945more:
1946 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1947
1948 /* open the socket first? */
1949 if (con->sock == NULL) {
Alex Eldera5988c42012-05-29 11:04:58 -05001950 set_bit(CONNECTING, &con->state);
1951
Alex Eldere2200422012-05-23 14:35:23 -05001952 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001953 prepare_write_banner(con);
Sage Weileed0ef22009-11-10 14:34:36 -08001954 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001955
Sage Weilcf3e5c42009-12-11 09:48:05 -08001956 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001957 con->in_tag = CEPH_MSGR_TAG_READY;
1958 dout("try_write initiating connect on %p new state %lu\n",
1959 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06001960 ret = ceph_tcp_connect(con);
1961 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07001962 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07001963 goto out;
1964 }
1965 }
1966
1967more_kvec:
1968 /* kvec data queued? */
1969 if (con->out_skip) {
1970 ret = write_partial_skip(con);
1971 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001972 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001973 }
1974 if (con->out_kvec_left) {
1975 ret = write_partial_kvec(con);
1976 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001977 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001978 }
1979
1980 /* msg pages? */
1981 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001982 if (con->out_msg_done) {
1983 ceph_msg_put(con->out_msg);
1984 con->out_msg = NULL; /* we're done with this one */
1985 goto do_next;
1986 }
1987
Sage Weil31b80062009-10-06 11:31:13 -07001988 ret = write_partial_msg_pages(con);
1989 if (ret == 1)
1990 goto more_kvec; /* we need to send the footer, too! */
1991 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08001992 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001993 if (ret < 0) {
1994 dout("try_write write_partial_msg_pages err %d\n",
1995 ret);
Sage Weil42961d22011-01-25 08:19:34 -08001996 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001997 }
1998 }
1999
Sage Weilc86a2932009-12-14 14:04:30 -08002000do_next:
Alex Elder7593af92012-05-24 11:55:03 -05002001 if (!test_bit(CONNECTING, &con->state) &&
2002 !test_bit(NEGOTIATING, &con->state)) {
Sage Weil31b80062009-10-06 11:31:13 -07002003 /* is anything else pending? */
2004 if (!list_empty(&con->out_queue)) {
2005 prepare_write_message(con);
2006 goto more;
2007 }
2008 if (con->in_seq > con->in_seq_acked) {
2009 prepare_write_ack(con);
2010 goto more;
2011 }
Alex Elder928443c2012-05-22 11:41:43 -05002012 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002013 prepare_write_keepalive(con);
2014 goto more;
2015 }
2016 }
2017
2018 /* Nothing to do! */
Alex Elder928443c2012-05-22 11:41:43 -05002019 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002020 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002021 ret = 0;
2022out:
Sage Weil42961d22011-01-25 08:19:34 -08002023 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002024 return ret;
2025}
2026
2027
2028
2029/*
2030 * Read what we can from the socket.
2031 */
2032static int try_read(struct ceph_connection *con)
2033{
Sage Weil31b80062009-10-06 11:31:13 -07002034 int ret = -1;
2035
2036 if (!con->sock)
2037 return 0;
2038
2039 if (test_bit(STANDBY, &con->state))
2040 return 0;
2041
2042 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08002043
Sage Weil31b80062009-10-06 11:31:13 -07002044more:
2045 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2046 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002047
2048 /*
2049 * process_connect and process_message drop and re-take
2050 * con->mutex. make sure we handle a racing close or reopen.
2051 */
2052 if (test_bit(CLOSED, &con->state) ||
2053 test_bit(OPENING, &con->state)) {
2054 ret = -EAGAIN;
2055 goto out;
2056 }
2057
Sage Weil31b80062009-10-06 11:31:13 -07002058 if (test_bit(CONNECTING, &con->state)) {
Alex Elder7593af92012-05-24 11:55:03 -05002059 dout("try_read connecting\n");
2060 ret = read_partial_banner(con);
2061 if (ret <= 0)
Alex Elderab166d52012-05-31 11:37:29 -05002062 goto out;
Alex Elder7593af92012-05-24 11:55:03 -05002063 ret = process_banner(con);
2064 if (ret < 0)
2065 goto out;
2066
2067 clear_bit(CONNECTING, &con->state);
2068 set_bit(NEGOTIATING, &con->state);
2069
2070 /* Banner is good, exchange connection info */
2071 ret = prepare_write_connect(con);
2072 if (ret < 0)
2073 goto out;
2074 prepare_read_connect(con);
2075
2076 /* Send connection info before awaiting response */
2077 goto out;
2078 }
2079
2080 if (test_bit(NEGOTIATING, &con->state)) {
2081 dout("try_read negotiating\n");
Sage Weil31b80062009-10-06 11:31:13 -07002082 ret = read_partial_connect(con);
2083 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002084 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002085 ret = process_connect(con);
2086 if (ret < 0)
2087 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002088 goto more;
2089 }
2090
2091 if (con->in_base_pos < 0) {
2092 /*
2093 * skipping + discarding content.
2094 *
2095 * FIXME: there must be a better way to do this!
2096 */
Alex Elder84495f42012-02-15 07:43:55 -06002097 static char buf[SKIP_BUF_SIZE];
2098 int skip = min((int) sizeof (buf), -con->in_base_pos);
2099
Sage Weil31b80062009-10-06 11:31:13 -07002100 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2101 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2102 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002103 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002104 con->in_base_pos += ret;
2105 if (con->in_base_pos)
2106 goto more;
2107 }
2108 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2109 /*
2110 * what's next?
2111 */
2112 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2113 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002114 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002115 dout("try_read got tag %d\n", (int)con->in_tag);
2116 switch (con->in_tag) {
2117 case CEPH_MSGR_TAG_MSG:
2118 prepare_read_message(con);
2119 break;
2120 case CEPH_MSGR_TAG_ACK:
2121 prepare_read_ack(con);
2122 break;
2123 case CEPH_MSGR_TAG_CLOSE:
Alex Eldere27947c2012-05-23 14:35:23 -05002124 clear_bit(CONNECTED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07002125 set_bit(CLOSED, &con->state); /* fixme */
Sage Weil98bdb0a2011-01-25 08:17:48 -08002126 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002127 default:
2128 goto bad_tag;
2129 }
2130 }
2131 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2132 ret = read_partial_message(con);
2133 if (ret <= 0) {
2134 switch (ret) {
2135 case -EBADMSG:
2136 con->error_msg = "bad crc";
2137 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002138 break;
Sage Weil31b80062009-10-06 11:31:13 -07002139 case -EIO:
2140 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002141 break;
Sage Weil31b80062009-10-06 11:31:13 -07002142 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002143 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002144 }
2145 if (con->in_tag == CEPH_MSGR_TAG_READY)
2146 goto more;
2147 process_message(con);
2148 goto more;
2149 }
2150 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2151 ret = read_partial_ack(con);
2152 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002153 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002154 process_ack(con);
2155 goto more;
2156 }
2157
Sage Weil31b80062009-10-06 11:31:13 -07002158out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002159 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002160 return ret;
2161
2162bad_tag:
2163 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2164 con->error_msg = "protocol error, garbage tag";
2165 ret = -1;
2166 goto out;
2167}
2168
2169
2170/*
2171 * Atomically queue work on a connection. Bump @con reference to
2172 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002173 */
2174static void queue_con(struct ceph_connection *con)
2175{
Sage Weil31b80062009-10-06 11:31:13 -07002176 if (!con->ops->get(con)) {
2177 dout("queue_con %p ref count 0\n", con);
2178 return;
2179 }
2180
Tejun Heof363e452011-01-03 14:49:46 +01002181 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002182 dout("queue_con %p - already queued\n", con);
2183 con->ops->put(con);
2184 } else {
2185 dout("queue_con %p\n", con);
2186 }
2187}
2188
2189/*
2190 * Do some work on a connection. Drop a connection ref when we're done.
2191 */
2192static void con_work(struct work_struct *work)
2193{
2194 struct ceph_connection *con = container_of(work, struct ceph_connection,
2195 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002196 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002197
Sage Weil9dd46582010-04-28 13:51:50 -07002198 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002199restart:
Alex Elder188048b2012-06-20 21:53:53 -05002200 if (test_and_clear_bit(SOCK_CLOSED, &con->flags)) {
Alex Eldere27947c2012-05-23 14:35:23 -05002201 if (test_and_clear_bit(CONNECTED, &con->state))
2202 con->error_msg = "socket closed";
Alex Elder7593af92012-05-24 11:55:03 -05002203 else if (test_and_clear_bit(NEGOTIATING, &con->state))
2204 con->error_msg = "negotiation failed";
2205 else if (test_and_clear_bit(CONNECTING, &con->state))
Alex Elder188048b2012-06-20 21:53:53 -05002206 con->error_msg = "connection failed";
Alex Elder7593af92012-05-24 11:55:03 -05002207 else
Alex Eldere27947c2012-05-23 14:35:23 -05002208 con->error_msg = "unrecognized con state";
Alex Elder188048b2012-06-20 21:53:53 -05002209 goto fault;
2210 }
2211
Alex Elder928443c2012-05-22 11:41:43 -05002212 if (test_and_clear_bit(BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002213 dout("con_work %p backing off\n", con);
2214 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2215 round_jiffies_relative(con->delay))) {
2216 dout("con_work %p backoff %lu\n", con, con->delay);
2217 mutex_unlock(&con->mutex);
2218 return;
2219 } else {
2220 con->ops->put(con);
2221 dout("con_work %p FAILED to back off %lu\n", con,
2222 con->delay);
2223 }
2224 }
Sage Weil9dd46582010-04-28 13:51:50 -07002225
Sage Weile00de342011-03-04 12:25:05 -08002226 if (test_bit(STANDBY, &con->state)) {
2227 dout("con_work %p STANDBY\n", con);
2228 goto done;
2229 }
Sage Weil31b80062009-10-06 11:31:13 -07002230 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2231 dout("con_work CLOSED\n");
2232 con_close_socket(con);
2233 goto done;
2234 }
2235 if (test_and_clear_bit(OPENING, &con->state)) {
2236 /* reopen w/ new peer */
2237 dout("con_work OPENING\n");
2238 con_close_socket(con);
2239 }
2240
Sage Weil0da5d702011-05-19 11:21:05 -07002241 ret = try_read(con);
2242 if (ret == -EAGAIN)
2243 goto restart;
2244 if (ret < 0)
2245 goto fault;
2246
2247 ret = try_write(con);
2248 if (ret == -EAGAIN)
2249 goto restart;
2250 if (ret < 0)
2251 goto fault;
Sage Weil31b80062009-10-06 11:31:13 -07002252
2253done:
Sage Weil9dd46582010-04-28 13:51:50 -07002254 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002255done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002256 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002257 return;
2258
2259fault:
2260 mutex_unlock(&con->mutex);
2261 ceph_fault(con); /* error/fault path */
2262 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002263}
2264
2265
2266/*
2267 * Generic error/fault handler. A retry mechanism is used with
2268 * exponential backoff
2269 */
2270static void ceph_fault(struct ceph_connection *con)
2271{
2272 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002273 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002274 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002275 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002276
Alex Elder928443c2012-05-22 11:41:43 -05002277 if (test_bit(LOSSYTX, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002278 dout("fault on LOSSYTX channel\n");
2279 goto out;
2280 }
2281
Sage Weilec302642009-12-22 10:43:42 -08002282 mutex_lock(&con->mutex);
Sage Weil91e45ce32010-02-15 12:05:09 -08002283 if (test_bit(CLOSED, &con->state))
2284 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002285
Sage Weil31b80062009-10-06 11:31:13 -07002286 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002287
2288 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002289 BUG_ON(con->in_msg->con != con);
2290 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08002291 ceph_msg_put(con->in_msg);
2292 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07002293 con->ops->put(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002294 }
Sage Weil31b80062009-10-06 11:31:13 -07002295
Sage Weile80a52d2010-02-25 12:40:45 -08002296 /* Requeue anything that hasn't been acked */
2297 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002298
Sage Weile76661d2011-03-03 10:10:15 -08002299 /* If there are no messages queued or keepalive pending, place
2300 * the connection in a STANDBY state */
2301 if (list_empty(&con->out_queue) &&
Alex Elder928443c2012-05-22 11:41:43 -05002302 !test_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weile00de342011-03-04 12:25:05 -08002303 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elder928443c2012-05-22 11:41:43 -05002304 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002305 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08002306 } else {
2307 /* retry after a delay. */
2308 if (con->delay == 0)
2309 con->delay = BASE_DELAY_INTERVAL;
2310 else if (con->delay < MAX_DELAY_INTERVAL)
2311 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002312 con->ops->get(con);
2313 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002314 round_jiffies_relative(con->delay))) {
2315 dout("fault queued %p delay %lu\n", con, con->delay);
2316 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002317 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002318 dout("fault failed to queue %p delay %lu, backoff\n",
2319 con, con->delay);
2320 /*
2321 * In many cases we see a socket state change
2322 * while con_work is running and end up
2323 * queuing (non-delayed) work, such that we
2324 * can't backoff with a delay. Set a flag so
2325 * that when con_work restarts we schedule the
2326 * delay then.
2327 */
Alex Elder928443c2012-05-22 11:41:43 -05002328 set_bit(BACKOFF, &con->flags);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002329 }
Sage Weil31b80062009-10-06 11:31:13 -07002330 }
2331
Sage Weil91e45ce32010-02-15 12:05:09 -08002332out_unlock:
2333 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002334out:
Sage Weil161fd652010-02-25 12:38:57 -08002335 /*
2336 * in case we faulted due to authentication, invalidate our
2337 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002338 */
Sage Weil161fd652010-02-25 12:38:57 -08002339 if (con->auth_retry && con->ops->invalidate_authorizer) {
2340 dout("calling invalidate_authorizer()\n");
2341 con->ops->invalidate_authorizer(con);
2342 }
2343
Sage Weil31b80062009-10-06 11:31:13 -07002344 if (con->ops->fault)
2345 con->ops->fault(con);
2346}
2347
2348
2349
2350/*
Alex Elder15d98822012-05-26 23:26:43 -05002351 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002352 */
Alex Elder15d98822012-05-26 23:26:43 -05002353void ceph_messenger_init(struct ceph_messenger *msgr,
2354 struct ceph_entity_addr *myaddr,
2355 u32 supported_features,
2356 u32 required_features,
2357 bool nocrc)
Sage Weil31b80062009-10-06 11:31:13 -07002358{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002359 msgr->supported_features = supported_features;
2360 msgr->required_features = required_features;
2361
Sage Weil31b80062009-10-06 11:31:13 -07002362 spin_lock_init(&msgr->global_seq_lock);
2363
Sage Weil31b80062009-10-06 11:31:13 -07002364 if (myaddr)
2365 msgr->inst.addr = *myaddr;
2366
2367 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002368 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002369 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002370 encode_my_addr(msgr);
Alex Elder15d98822012-05-26 23:26:43 -05002371 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002372
Alex Elder15d98822012-05-26 23:26:43 -05002373 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002374}
Alex Elder15d98822012-05-26 23:26:43 -05002375EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002376
Sage Weile00de342011-03-04 12:25:05 -08002377static void clear_standby(struct ceph_connection *con)
2378{
2379 /* come back from STANDBY? */
2380 if (test_and_clear_bit(STANDBY, &con->state)) {
2381 mutex_lock(&con->mutex);
2382 dout("clear_standby %p and ++connect_seq\n", con);
2383 con->connect_seq++;
Alex Elder928443c2012-05-22 11:41:43 -05002384 WARN_ON(test_bit(WRITE_PENDING, &con->flags));
2385 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags));
Sage Weile00de342011-03-04 12:25:05 -08002386 mutex_unlock(&con->mutex);
2387 }
2388}
2389
Sage Weil31b80062009-10-06 11:31:13 -07002390/*
2391 * Queue up an outgoing message on the given connection.
2392 */
2393void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2394{
2395 if (test_bit(CLOSED, &con->state)) {
2396 dout("con_send %p closed, dropping %p\n", con, msg);
2397 ceph_msg_put(msg);
2398 return;
2399 }
2400
2401 /* set src+dst */
Sage Weildbad1852010-03-25 15:45:38 -07002402 msg->hdr.src = con->msgr->inst.name;
Sage Weil31b80062009-10-06 11:31:13 -07002403
Sage Weil3ca02ef2010-03-01 15:25:00 -08002404 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2405
Sage Weile84346b2010-05-11 21:20:38 -07002406 msg->needs_out_seq = true;
2407
Sage Weil31b80062009-10-06 11:31:13 -07002408 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08002409 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002410
Alex Elder38941f82012-06-01 14:56:43 -05002411 BUG_ON(msg->con != NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002412 msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002413 BUG_ON(msg->con == NULL);
2414
Sage Weil31b80062009-10-06 11:31:13 -07002415 BUG_ON(!list_empty(&msg->list_head));
2416 list_add_tail(&msg->list_head, &con->out_queue);
2417 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2418 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2419 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2420 le32_to_cpu(msg->hdr.front_len),
2421 le32_to_cpu(msg->hdr.middle_len),
2422 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002423 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002424
2425 /* if there wasn't anything waiting to send before, queue
2426 * new work */
Sage Weile00de342011-03-04 12:25:05 -08002427 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002428 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002429 queue_con(con);
2430}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002431EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002432
2433/*
2434 * Revoke a message that was previously queued for send
2435 */
Alex Elder6740a842012-06-01 14:56:43 -05002436void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002437{
Alex Elder6740a842012-06-01 14:56:43 -05002438 struct ceph_connection *con = msg->con;
2439
2440 if (!con)
2441 return; /* Message not in our possession */
2442
Sage Weilec302642009-12-22 10:43:42 -08002443 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002444 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05002445 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002446 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -05002447 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002448 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -05002449 msg->con = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05002450 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05002451
Sage Weil31b80062009-10-06 11:31:13 -07002452 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002453 }
2454 if (con->out_msg == msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002455 dout("%s %p msg %p - was sending\n", __func__, con, msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002456 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002457 if (con->out_kvec_is_msg) {
2458 con->out_skip = con->out_kvec_bytes;
2459 con->out_kvec_is_msg = false;
2460 }
Sage Weiled98ada2010-07-05 12:15:14 -07002461 msg->hdr.seq = 0;
Alex Elder92ce0342012-06-04 14:43:33 -05002462
2463 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07002464 }
Sage Weilec302642009-12-22 10:43:42 -08002465 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002466}
2467
2468/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002469 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002470 */
Alex Elder8921d112012-06-01 14:56:43 -05002471void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002472{
Alex Elder8921d112012-06-01 14:56:43 -05002473 struct ceph_connection *con;
2474
2475 BUG_ON(msg == NULL);
2476 if (!msg->con) {
2477 dout("%s msg %p null con\n", __func__, msg);
2478
2479 return; /* Message not in our possession */
2480 }
2481
2482 con = msg->con;
Sage Weil350b1c32009-12-22 10:45:45 -08002483 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05002484 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002485 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
2486 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
2487 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002488
2489 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05002490 dout("%s %p msg %p revoked\n", __func__, con, msg);
2491 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08002492 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002493 front_len -
2494 middle_len -
2495 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002496 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002497 ceph_msg_put(con->in_msg);
2498 con->in_msg = NULL;
2499 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002500 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002501 } else {
Alex Elder8921d112012-06-01 14:56:43 -05002502 dout("%s %p in_msg %p msg %p no-op\n",
2503 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002504 }
2505 mutex_unlock(&con->mutex);
2506}
2507
2508/*
Sage Weil31b80062009-10-06 11:31:13 -07002509 * Queue a keepalive byte to ensure the tcp connection is alive.
2510 */
2511void ceph_con_keepalive(struct ceph_connection *con)
2512{
Sage Weile00de342011-03-04 12:25:05 -08002513 dout("con_keepalive %p\n", con);
2514 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002515 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2516 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002517 queue_con(con);
2518}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002519EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002520
2521
2522/*
2523 * construct a new message with given type, size
2524 * the new msg has a ref count of 1.
2525 */
Sage Weilb61c2762011-08-09 15:03:46 -07002526struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2527 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002528{
2529 struct ceph_msg *m;
2530
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002531 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002532 if (m == NULL)
2533 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002534 kref_init(&m->kref);
Alex Elder38941f82012-06-01 14:56:43 -05002535
2536 m->con = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002537 INIT_LIST_HEAD(&m->list_head);
2538
Sage Weil45c6ceb2010-05-11 15:01:51 -07002539 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002540 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002541 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2542 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002543 m->hdr.front_len = cpu_to_le32(front_len);
2544 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002545 m->hdr.data_len = 0;
2546 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002547 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002548 m->footer.front_crc = 0;
2549 m->footer.middle_crc = 0;
2550 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002551 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002552 m->front_max = front_len;
2553 m->front_is_vmalloc = false;
2554 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002555 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002556 m->pool = NULL;
2557
Henry C Changca208922011-05-03 02:29:56 +00002558 /* middle */
2559 m->middle = NULL;
2560
2561 /* data */
2562 m->nr_pages = 0;
2563 m->page_alignment = 0;
2564 m->pages = NULL;
2565 m->pagelist = NULL;
2566 m->bio = NULL;
2567 m->bio_iter = NULL;
2568 m->bio_seg = 0;
2569 m->trail = NULL;
2570
Sage Weil31b80062009-10-06 11:31:13 -07002571 /* front */
2572 if (front_len) {
2573 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002574 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002575 PAGE_KERNEL);
2576 m->front_is_vmalloc = true;
2577 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002578 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002579 }
2580 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002581 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002582 front_len);
2583 goto out2;
2584 }
2585 } else {
2586 m->front.iov_base = NULL;
2587 }
2588 m->front.iov_len = front_len;
2589
Sage Weilbb257662010-04-01 16:07:23 -07002590 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002591 return m;
2592
2593out2:
2594 ceph_msg_put(m);
2595out:
Sage Weilb61c2762011-08-09 15:03:46 -07002596 if (!can_fail) {
2597 pr_err("msg_new can't create type %d front %d\n", type,
2598 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002599 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002600 } else {
2601 dout("msg_new can't create type %d front %d\n", type,
2602 front_len);
2603 }
Sage Weila79832f2010-04-01 16:06:19 -07002604 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002605}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002606EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002607
2608/*
Sage Weil31b80062009-10-06 11:31:13 -07002609 * Allocate "middle" portion of a message, if it is needed and wasn't
2610 * allocated by alloc_msg. This allows us to read a small fixed-size
2611 * per-type header in the front and then gracefully fail (i.e.,
2612 * propagate the error to the caller based on info in the front) when
2613 * the middle is too large.
2614 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002615static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002616{
2617 int type = le16_to_cpu(msg->hdr.type);
2618 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2619
2620 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2621 ceph_msg_type_name(type), middle_len);
2622 BUG_ON(!middle_len);
2623 BUG_ON(msg->middle);
2624
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002625 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002626 if (!msg->middle)
2627 return -ENOMEM;
2628 return 0;
2629}
2630
Yehuda Sadeh24504182010-01-08 13:58:34 -08002631/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05002632 * Allocate a message for receiving an incoming message on a
2633 * connection, and save the result in con->in_msg. Uses the
2634 * connection's private alloc_msg op if available.
2635 *
2636 * Returns true if the message should be skipped, false otherwise.
2637 * If true is returned (skip message), con->in_msg will be NULL.
2638 * If false is returned, con->in_msg will contain a pointer to the
2639 * newly-allocated message, or NULL in case of memory exhaustion.
Yehuda Sadeh24504182010-01-08 13:58:34 -08002640 */
Alex Elder1c20f2d2012-06-04 14:43:32 -05002641static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2642 struct ceph_msg_header *hdr)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002643{
2644 int type = le16_to_cpu(hdr->type);
2645 int front_len = le32_to_cpu(hdr->front_len);
2646 int middle_len = le32_to_cpu(hdr->middle_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002647 int ret;
2648
Alex Elder1c20f2d2012-06-04 14:43:32 -05002649 BUG_ON(con->in_msg != NULL);
2650
Yehuda Sadeh24504182010-01-08 13:58:34 -08002651 if (con->ops->alloc_msg) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002652 int skip = 0;
2653
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002654 mutex_unlock(&con->mutex);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002655 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002656 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002657 if (con->in_msg) {
Sage Weil36eb71a2012-06-21 12:47:08 -07002658 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002659 BUG_ON(con->in_msg->con == NULL);
2660 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002661 if (skip)
2662 con->in_msg = NULL;
2663
2664 if (!con->in_msg)
2665 return skip != 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002666 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002667 if (!con->in_msg) {
2668 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2669 if (!con->in_msg) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002670 pr_err("unable to allocate msg type %d len %d\n",
2671 type, front_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002672 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002673 }
Sage Weil36eb71a2012-06-21 12:47:08 -07002674 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002675 BUG_ON(con->in_msg->con == NULL);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002676 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002677 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002678 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002679
Alex Elder1c20f2d2012-06-04 14:43:32 -05002680 if (middle_len && !con->in_msg->middle) {
2681 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002682 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002683 ceph_msg_put(con->in_msg);
2684 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002685 }
2686 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002687
Alex Elder1c20f2d2012-06-04 14:43:32 -05002688 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002689}
2690
Sage Weil31b80062009-10-06 11:31:13 -07002691
2692/*
2693 * Free a generically kmalloc'd message.
2694 */
2695void ceph_msg_kfree(struct ceph_msg *m)
2696{
2697 dout("msg_kfree %p\n", m);
2698 if (m->front_is_vmalloc)
2699 vfree(m->front.iov_base);
2700 else
2701 kfree(m->front.iov_base);
2702 kfree(m);
2703}
2704
2705/*
2706 * Drop a msg ref. Destroy as needed.
2707 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002708void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002709{
Sage Weilc2e552e2009-12-07 15:55:05 -08002710 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002711
Sage Weilc2e552e2009-12-07 15:55:05 -08002712 dout("ceph_msg_put last one on %p\n", m);
2713 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002714
Sage Weilc2e552e2009-12-07 15:55:05 -08002715 /* drop middle, data, if any */
2716 if (m->middle) {
2717 ceph_buffer_put(m->middle);
2718 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002719 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002720 m->nr_pages = 0;
2721 m->pages = NULL;
2722
Sage Weil58bb3b32009-12-23 12:12:31 -08002723 if (m->pagelist) {
2724 ceph_pagelist_release(m->pagelist);
2725 kfree(m->pagelist);
2726 m->pagelist = NULL;
2727 }
2728
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002729 m->trail = NULL;
2730
Sage Weilc2e552e2009-12-07 15:55:05 -08002731 if (m->pool)
2732 ceph_msgpool_put(m->pool, m);
2733 else
2734 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002735}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002736EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002737
2738void ceph_msg_dump(struct ceph_msg *msg)
2739{
2740 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2741 msg->front_max, msg->nr_pages);
2742 print_hex_dump(KERN_DEBUG, "header: ",
2743 DUMP_PREFIX_OFFSET, 16, 1,
2744 &msg->hdr, sizeof(msg->hdr), true);
2745 print_hex_dump(KERN_DEBUG, " front: ",
2746 DUMP_PREFIX_OFFSET, 16, 1,
2747 msg->front.iov_base, msg->front.iov_len, true);
2748 if (msg->middle)
2749 print_hex_dump(KERN_DEBUG, "middle: ",
2750 DUMP_PREFIX_OFFSET, 16, 1,
2751 msg->middle->vec.iov_base,
2752 msg->middle->vec.iov_len, true);
2753 print_hex_dump(KERN_DEBUG, "footer: ",
2754 DUMP_PREFIX_OFFSET, 16, 1,
2755 &msg->footer, sizeof(msg->footer), true);
2756}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002757EXPORT_SYMBOL(ceph_msg_dump);