blob: cfcca1f5be67be38fb6a255629cb42ed68b6ef55 [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);
Sage Weil31b80062009-10-06 11:31:13 -0700465 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Alex Eldera5988c42012-05-29 11:04:58 -0500466 set_bit(CLOSED, &con->state);
467
Alex Elder928443c2012-05-22 11:41:43 -0500468 clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */
469 clear_bit(KEEPALIVE_PENDING, &con->flags);
470 clear_bit(WRITE_PENDING, &con->flags);
Alex Eldera5988c42012-05-29 11:04:58 -0500471
Sage Weilec302642009-12-22 10:43:42 -0800472 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700473 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700474 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800475 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800476 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700477 queue_con(con);
478}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700479EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700480
481/*
Sage Weil31b80062009-10-06 11:31:13 -0700482 * Reopen a closed connection, with a new peer address.
483 */
484void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
485{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700486 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700487 set_bit(OPENING, &con->state);
Alex Eldera5988c42012-05-29 11:04:58 -0500488 WARN_ON(!test_and_clear_bit(CLOSED, &con->state));
489
Sage Weil31b80062009-10-06 11:31:13 -0700490 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800491 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700492 queue_con(con);
493}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700494EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700495
496/*
Sage Weil87b315a2010-03-22 14:51:18 -0700497 * return true if this connection ever successfully opened
498 */
499bool ceph_con_opened(struct ceph_connection *con)
500{
501 return con->connect_seq > 0;
502}
503
504/*
Sage Weil31b80062009-10-06 11:31:13 -0700505 * initialize a new connection.
506 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500507void ceph_con_init(struct ceph_connection *con, void *private,
508 const struct ceph_connection_operations *ops,
509 struct ceph_messenger *msgr, __u8 entity_type, __u64 entity_num)
Sage Weil31b80062009-10-06 11:31:13 -0700510{
511 dout("con_init %p\n", con);
512 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500513 con->private = private;
514 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700515 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500516
517 con_sock_state_init(con);
518
Alex Elder1bfd89f2012-05-26 23:26:43 -0500519 con->peer_name.type = (__u8) entity_type;
520 con->peer_name.num = cpu_to_le64(entity_num);
521
Sage Weilec302642009-12-22 10:43:42 -0800522 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700523 INIT_LIST_HEAD(&con->out_queue);
524 INIT_LIST_HEAD(&con->out_sent);
525 INIT_DELAYED_WORK(&con->work, con_work);
Alex Eldera5988c42012-05-29 11:04:58 -0500526
527 set_bit(CLOSED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700528}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700529EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700530
531
532/*
533 * We maintain a global counter to order connection attempts. Get
534 * a unique seq greater than @gt.
535 */
536static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
537{
538 u32 ret;
539
540 spin_lock(&msgr->global_seq_lock);
541 if (msgr->global_seq < gt)
542 msgr->global_seq = gt;
543 ret = ++msgr->global_seq;
544 spin_unlock(&msgr->global_seq_lock);
545 return ret;
546}
547
Alex Eldere2200422012-05-23 14:35:23 -0500548static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600549{
550 con->out_kvec_left = 0;
551 con->out_kvec_bytes = 0;
552 con->out_kvec_cur = &con->out_kvec[0];
553}
554
Alex Eldere2200422012-05-23 14:35:23 -0500555static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600556 size_t size, void *data)
557{
558 int index;
559
560 index = con->out_kvec_left;
561 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
562
563 con->out_kvec[index].iov_len = size;
564 con->out_kvec[index].iov_base = data;
565 con->out_kvec_left++;
566 con->out_kvec_bytes += size;
567}
Sage Weil31b80062009-10-06 11:31:13 -0700568
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500569#ifdef CONFIG_BLOCK
570static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
571{
572 if (!bio) {
573 *iter = NULL;
574 *seg = 0;
575 return;
576 }
577 *iter = bio;
578 *seg = bio->bi_idx;
579}
580
581static void iter_bio_next(struct bio **bio_iter, int *seg)
582{
583 if (*bio_iter == NULL)
584 return;
585
586 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
587
588 (*seg)++;
589 if (*seg == (*bio_iter)->bi_vcnt)
590 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
591}
592#endif
593
Alex Elder739c9052012-06-11 14:57:13 -0500594static void prepare_write_message_data(struct ceph_connection *con)
595{
596 struct ceph_msg *msg = con->out_msg;
597
598 BUG_ON(!msg);
599 BUG_ON(!msg->hdr.data_len);
600
601 /* initialize page iterator */
602 con->out_msg_pos.page = 0;
603 if (msg->pages)
604 con->out_msg_pos.page_pos = msg->page_alignment;
605 else
606 con->out_msg_pos.page_pos = 0;
Alex Elder572c5882012-06-11 14:57:13 -0500607#ifdef CONFIG_BLOCK
Alex Elderabdaa6a2012-06-11 14:57:13 -0500608 if (msg->bio)
Alex Elder572c5882012-06-11 14:57:13 -0500609 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
610#endif
Alex Elder739c9052012-06-11 14:57:13 -0500611 con->out_msg_pos.data_pos = 0;
612 con->out_msg_pos.did_page_crc = false;
613 con->out_more = 1; /* data + footer will follow */
614}
615
Sage Weil31b80062009-10-06 11:31:13 -0700616/*
617 * Prepare footer for currently outgoing message, and finish things
618 * off. Assumes out_kvec* are already valid.. we just add on to the end.
619 */
Alex Elder859eb792012-02-14 14:05:33 -0600620static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700621{
622 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600623 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700624
Alex Elderfd154f32012-06-11 14:57:13 -0500625 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
626
Sage Weil31b80062009-10-06 11:31:13 -0700627 dout("prepare_write_message_footer %p\n", con);
628 con->out_kvec_is_msg = true;
629 con->out_kvec[v].iov_base = &m->footer;
630 con->out_kvec[v].iov_len = sizeof(m->footer);
631 con->out_kvec_bytes += sizeof(m->footer);
632 con->out_kvec_left++;
633 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800634 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700635}
636
637/*
638 * Prepare headers for the next outgoing message.
639 */
640static void prepare_write_message(struct ceph_connection *con)
641{
642 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600643 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700644
Alex Eldere2200422012-05-23 14:35:23 -0500645 con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700646 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800647 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700648
649 /* Sneak an ack in there first? If we can get it into the same
650 * TCP packet that's a good thing. */
651 if (con->in_seq > con->in_seq_acked) {
652 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -0500653 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700654 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500655 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600656 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700657 }
658
Alex Elder38941f82012-06-01 14:56:43 -0500659 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -0600660 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800661 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -0500662 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -0700663
664 /* put message on sent list */
665 ceph_msg_get(m);
666 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700667
Sage Weile84346b2010-05-11 21:20:38 -0700668 /*
669 * only assign outgoing seq # if we haven't sent this message
670 * yet. if it is requeued, resend with it's original seq.
671 */
672 if (m->needs_out_seq) {
673 m->hdr.seq = cpu_to_le64(++con->out_seq);
674 m->needs_out_seq = false;
675 }
Sage Weil31b80062009-10-06 11:31:13 -0700676
677 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
678 m, con->out_seq, le16_to_cpu(m->hdr.type),
679 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
680 le32_to_cpu(m->hdr.data_len),
681 m->nr_pages);
682 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
683
684 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -0500685 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
686 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
687 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600688
Sage Weil31b80062009-10-06 11:31:13 -0700689 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -0500690 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600691 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700692
693 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600694 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
695 con->out_msg->hdr.crc = cpu_to_le32(crc);
Alex Elderfd154f32012-06-11 14:57:13 -0500696 con->out_msg->footer.flags = 0;
Alex Eldera9a0c512012-02-15 07:43:54 -0600697
698 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
699 con->out_msg->footer.front_crc = cpu_to_le32(crc);
700 if (m->middle) {
701 crc = crc32c(0, m->middle->vec.iov_base,
702 m->middle->vec.iov_len);
703 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
704 } else
Sage Weil31b80062009-10-06 11:31:13 -0700705 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -0500706 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700707 le32_to_cpu(con->out_msg->footer.front_crc),
708 le32_to_cpu(con->out_msg->footer.middle_crc));
709
710 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -0500711 con->out_msg->footer.data_crc = 0;
712 if (m->hdr.data_len)
713 prepare_write_message_data(con);
714 else
Sage Weil31b80062009-10-06 11:31:13 -0700715 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600716 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700717
Alex Elder928443c2012-05-22 11:41:43 -0500718 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700719}
720
721/*
722 * Prepare an ack.
723 */
724static void prepare_write_ack(struct ceph_connection *con)
725{
726 dout("prepare_write_ack %p %llu -> %llu\n", con,
727 con->in_seq_acked, con->in_seq);
728 con->in_seq_acked = con->in_seq;
729
Alex Eldere2200422012-05-23 14:35:23 -0500730 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600731
Alex Eldere2200422012-05-23 14:35:23 -0500732 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600733
Sage Weil31b80062009-10-06 11:31:13 -0700734 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500735 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600736 &con->out_temp_ack);
737
Sage Weil31b80062009-10-06 11:31:13 -0700738 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elder928443c2012-05-22 11:41:43 -0500739 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700740}
741
742/*
743 * Prepare to write keepalive byte.
744 */
745static void prepare_write_keepalive(struct ceph_connection *con)
746{
747 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -0500748 con_out_kvec_reset(con);
749 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Alex Elder928443c2012-05-22 11:41:43 -0500750 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700751}
752
753/*
754 * Connection negotiation.
755 */
756
Alex Elderdac1e712012-05-16 15:16:39 -0500757static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
758 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800759{
Alex Eldera3530df2012-05-16 15:16:39 -0500760 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -0500761
762 if (!con->ops->get_authorizer) {
763 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
764 con->out_connect.authorizer_len = 0;
765
Alex Elder729796b2012-05-16 15:16:39 -0500766 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -0500767 }
768
769 /* Can't hold the mutex while getting authorizer */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800770
Sage Weilec302642009-12-22 10:43:42 -0800771 mutex_unlock(&con->mutex);
Alex Elderb1c6b982012-05-16 15:16:38 -0500772
Alex Elderdac1e712012-05-16 15:16:39 -0500773 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Elder8f43fb52012-05-16 15:16:39 -0500774
Sage Weilec302642009-12-22 10:43:42 -0800775 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800776
Alex Eldera3530df2012-05-16 15:16:39 -0500777 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -0500778 return auth;
Alex Elder928443c2012-05-22 11:41:43 -0500779 if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags))
Alex Elder729796b2012-05-16 15:16:39 -0500780 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700781
Alex Elder8f43fb52012-05-16 15:16:39 -0500782 con->auth_reply_buf = auth->authorizer_reply_buf;
783 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
784
Alex Elder859eb792012-02-14 14:05:33 -0600785
Alex Elder729796b2012-05-16 15:16:39 -0500786 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800787}
788
Sage Weil31b80062009-10-06 11:31:13 -0700789/*
790 * We connected to a peer and are saying hello.
791 */
Alex Eldere825a662012-05-16 15:16:38 -0500792static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700793{
Alex Eldere2200422012-05-23 14:35:23 -0500794 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
795 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -0500796 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800797
Sage Weileed0ef22009-11-10 14:34:36 -0800798 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500799 set_bit(WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800800}
801
Alex Eldere825a662012-05-16 15:16:38 -0500802static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800803{
Eric Dumazet95c96172012-04-15 05:58:06 +0000804 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -0700805 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -0500806 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -0500807 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700808
809 switch (con->peer_name.type) {
810 case CEPH_ENTITY_TYPE_MON:
811 proto = CEPH_MONC_PROTOCOL;
812 break;
813 case CEPH_ENTITY_TYPE_OSD:
814 proto = CEPH_OSDC_PROTOCOL;
815 break;
816 case CEPH_ENTITY_TYPE_MDS:
817 proto = CEPH_MDSC_PROTOCOL;
818 break;
819 default:
820 BUG();
821 }
822
823 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
824 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800825
Alex Eldere825a662012-05-16 15:16:38 -0500826 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700827 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
828 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
829 con->out_connect.global_seq = cpu_to_le32(global_seq);
830 con->out_connect.protocol_version = cpu_to_le32(proto);
831 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700832
Alex Elderdac1e712012-05-16 15:16:39 -0500833 auth_proto = CEPH_AUTH_UNKNOWN;
834 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -0500835 if (IS_ERR(auth))
836 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -0500837
Alex Elderdac1e712012-05-16 15:16:39 -0500838 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -0500839 con->out_connect.authorizer_len = auth ?
840 cpu_to_le32(auth->authorizer_buf_len) : 0;
841
Alex Eldere2200422012-05-23 14:35:23 -0500842 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -0500843 &con->out_connect);
844 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -0500845 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -0500846 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600847
Sage Weil31b80062009-10-06 11:31:13 -0700848 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500849 set_bit(WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800850
Alex Eldere10c7582012-05-16 15:16:38 -0500851 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700852}
853
Sage Weil31b80062009-10-06 11:31:13 -0700854/*
855 * write as much of pending kvecs to the socket as we can.
856 * 1 -> done
857 * 0 -> socket full, but more to do
858 * <0 -> error
859 */
860static int write_partial_kvec(struct ceph_connection *con)
861{
862 int ret;
863
864 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
865 while (con->out_kvec_bytes > 0) {
866 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
867 con->out_kvec_left, con->out_kvec_bytes,
868 con->out_more);
869 if (ret <= 0)
870 goto out;
871 con->out_kvec_bytes -= ret;
872 if (con->out_kvec_bytes == 0)
873 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600874
875 /* account for full iov entries consumed */
876 while (ret >= con->out_kvec_cur->iov_len) {
877 BUG_ON(!con->out_kvec_left);
878 ret -= con->out_kvec_cur->iov_len;
879 con->out_kvec_cur++;
880 con->out_kvec_left--;
881 }
882 /* and for a partially-consumed entry */
883 if (ret) {
884 con->out_kvec_cur->iov_len -= ret;
885 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700886 }
887 }
888 con->out_kvec_left = 0;
889 con->out_kvec_is_msg = false;
890 ret = 1;
891out:
892 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
893 con->out_kvec_bytes, con->out_kvec_left, ret);
894 return ret; /* done! */
895}
896
Alex Elder84ca8fc2012-06-11 14:57:13 -0500897static void out_msg_pos_next(struct ceph_connection *con, struct page *page,
898 size_t len, size_t sent, bool in_trail)
899{
900 struct ceph_msg *msg = con->out_msg;
901
902 BUG_ON(!msg);
903 BUG_ON(!sent);
904
905 con->out_msg_pos.data_pos += sent;
906 con->out_msg_pos.page_pos += sent;
907 if (sent == len) {
908 con->out_msg_pos.page_pos = 0;
909 con->out_msg_pos.page++;
910 con->out_msg_pos.did_page_crc = false;
911 if (in_trail)
912 list_move_tail(&page->lru,
913 &msg->trail->head);
914 else if (msg->pagelist)
915 list_move_tail(&page->lru,
916 &msg->pagelist->head);
917#ifdef CONFIG_BLOCK
918 else if (msg->bio)
919 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
920#endif
921 }
922}
923
Sage Weil31b80062009-10-06 11:31:13 -0700924/*
925 * Write as much message data payload as we can. If we finish, queue
926 * up the footer.
927 * 1 -> done, footer is now queued in out_kvec[].
928 * 0 -> socket full, but more to do
929 * <0 -> error
930 */
931static int write_partial_msg_pages(struct ceph_connection *con)
932{
933 struct ceph_msg *msg = con->out_msg;
Eric Dumazet95c96172012-04-15 05:58:06 +0000934 unsigned int data_len = le32_to_cpu(msg->hdr.data_len);
Sage Weil31b80062009-10-06 11:31:13 -0700935 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600936 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700937 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700938 int total_max_write;
Alex Elder84ca8fc2012-06-11 14:57:13 -0500939 bool in_trail = false;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700940 size_t trail_len = (msg->trail ? msg->trail->length : 0);
Sage Weil31b80062009-10-06 11:31:13 -0700941
942 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
Alex Elder84ca8fc2012-06-11 14:57:13 -0500943 con, msg, con->out_msg_pos.page, msg->nr_pages,
Sage Weil31b80062009-10-06 11:31:13 -0700944 con->out_msg_pos.page_pos);
945
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700946 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -0700947 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700948 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -0600949 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700950
951 total_max_write = data_len - trail_len -
952 con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -0700953
954 /*
955 * if we are calculating the data crc (the default), we need
956 * to map the page. if our pages[] has been revoked, use the
957 * zero page.
958 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700959
960 /* have we reached the trail part of the data? */
961 if (con->out_msg_pos.data_pos >= data_len - trail_len) {
Alex Elder84ca8fc2012-06-11 14:57:13 -0500962 in_trail = true;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700963
964 total_max_write = data_len - con->out_msg_pos.data_pos;
965
966 page = list_first_entry(&msg->trail->head,
967 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700968 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -0700969 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -0800970 } else if (msg->pagelist) {
971 page = list_first_entry(&msg->pagelist->head,
972 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700973#ifdef CONFIG_BLOCK
974 } else if (msg->bio) {
975 struct bio_vec *bv;
976
977 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
978 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -0600979 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700980 max_write = bv->bv_len;
981#endif
Sage Weil31b80062009-10-06 11:31:13 -0700982 } else {
Alex Elder57666512012-01-23 15:49:27 -0600983 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -0700984 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700985 len = min_t(int, max_write - con->out_msg_pos.page_pos,
986 total_max_write);
987
Alex Elder37675b02012-03-07 11:40:08 -0600988 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -0600989 void *base;
Alex Eldera9a0c512012-02-15 07:43:54 -0600990 u32 crc;
Alex Elder84ca8fc2012-06-11 14:57:13 -0500991 u32 tmpcrc = le32_to_cpu(msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -0600992 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -0700993
Alex Elder8d63e312012-03-07 11:40:08 -0600994 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -0700995 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -0600996 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Eldera9a0c512012-02-15 07:43:54 -0600997 crc = crc32c(tmpcrc, base, len);
Alex Elder84ca8fc2012-06-11 14:57:13 -0500998 msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -0600999 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -07001000 }
Alex Eldere36b13c2012-03-07 11:40:08 -06001001 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -06001002 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -06001003 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001004
Alex Elder0cdf9e62012-03-07 11:40:08 -06001005 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -07001006 kunmap(page);
1007
1008 if (ret <= 0)
1009 goto out;
1010
Alex Elder84ca8fc2012-06-11 14:57:13 -05001011 out_msg_pos_next(con, page, len, (size_t) ret, in_trail);
Sage Weil31b80062009-10-06 11:31:13 -07001012 }
1013
1014 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1015
1016 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001017 if (!do_datacrc)
Alex Elder84ca8fc2012-06-11 14:57:13 -05001018 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001019 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001020 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001021 ret = 1;
1022out:
1023 return ret;
1024}
1025
1026/*
1027 * write some zeros
1028 */
1029static int write_partial_skip(struct ceph_connection *con)
1030{
1031 int ret;
1032
1033 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001034 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001035
Alex Elder31739132012-03-07 11:40:08 -06001036 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001037 if (ret <= 0)
1038 goto out;
1039 con->out_skip -= ret;
1040 }
1041 ret = 1;
1042out:
1043 return ret;
1044}
1045
1046/*
1047 * Prepare to read connection handshake, or an ack.
1048 */
Sage Weileed0ef22009-11-10 14:34:36 -08001049static void prepare_read_banner(struct ceph_connection *con)
1050{
1051 dout("prepare_read_banner %p\n", con);
1052 con->in_base_pos = 0;
1053}
1054
Sage Weil31b80062009-10-06 11:31:13 -07001055static void prepare_read_connect(struct ceph_connection *con)
1056{
1057 dout("prepare_read_connect %p\n", con);
1058 con->in_base_pos = 0;
1059}
1060
1061static void prepare_read_ack(struct ceph_connection *con)
1062{
1063 dout("prepare_read_ack %p\n", con);
1064 con->in_base_pos = 0;
1065}
1066
1067static void prepare_read_tag(struct ceph_connection *con)
1068{
1069 dout("prepare_read_tag %p\n", con);
1070 con->in_base_pos = 0;
1071 con->in_tag = CEPH_MSGR_TAG_READY;
1072}
1073
1074/*
1075 * Prepare to read a message.
1076 */
1077static int prepare_read_message(struct ceph_connection *con)
1078{
1079 dout("prepare_read_message %p\n", con);
1080 BUG_ON(con->in_msg != NULL);
1081 con->in_base_pos = 0;
1082 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1083 return 0;
1084}
1085
1086
1087static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001088 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001089{
Alex Eldere6cee712012-05-10 10:29:50 -05001090 while (con->in_base_pos < end) {
1091 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001092 int have = size - left;
1093 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1094 if (ret <= 0)
1095 return ret;
1096 con->in_base_pos += ret;
1097 }
1098 return 1;
1099}
1100
1101
1102/*
1103 * Read all or part of the connect-side handshake on a new connection
1104 */
Sage Weileed0ef22009-11-10 14:34:36 -08001105static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001106{
Alex Elderfd516532012-05-10 10:29:50 -05001107 int size;
1108 int end;
1109 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001110
Sage Weileed0ef22009-11-10 14:34:36 -08001111 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001112
1113 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001114 size = strlen(CEPH_BANNER);
1115 end = size;
1116 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001117 if (ret <= 0)
1118 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001119
1120 size = sizeof (con->actual_peer_addr);
1121 end += size;
1122 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001123 if (ret <= 0)
1124 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001125
1126 size = sizeof (con->peer_addr_for_me);
1127 end += size;
1128 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001129 if (ret <= 0)
1130 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001131
Sage Weileed0ef22009-11-10 14:34:36 -08001132out:
1133 return ret;
1134}
1135
1136static int read_partial_connect(struct ceph_connection *con)
1137{
Alex Elderfd516532012-05-10 10:29:50 -05001138 int size;
1139 int end;
1140 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001141
1142 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1143
Alex Elderfd516532012-05-10 10:29:50 -05001144 size = sizeof (con->in_reply);
1145 end = size;
1146 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001147 if (ret <= 0)
1148 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001149
1150 size = le32_to_cpu(con->in_reply.authorizer_len);
1151 end += size;
1152 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001153 if (ret <= 0)
1154 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001155
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001156 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1157 con, (int)con->in_reply.tag,
1158 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001159 le32_to_cpu(con->in_reply.global_seq));
1160out:
1161 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001162
Sage Weil31b80062009-10-06 11:31:13 -07001163}
1164
1165/*
1166 * Verify the hello banner looks okay.
1167 */
1168static int verify_hello(struct ceph_connection *con)
1169{
1170 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001171 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001172 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001173 con->error_msg = "protocol error, bad banner";
1174 return -1;
1175 }
1176 return 0;
1177}
1178
1179static bool addr_is_blank(struct sockaddr_storage *ss)
1180{
1181 switch (ss->ss_family) {
1182 case AF_INET:
1183 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1184 case AF_INET6:
1185 return
1186 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1187 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1188 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1189 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1190 }
1191 return false;
1192}
1193
1194static int addr_port(struct sockaddr_storage *ss)
1195{
1196 switch (ss->ss_family) {
1197 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001198 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001199 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001200 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001201 }
1202 return 0;
1203}
1204
1205static void addr_set_port(struct sockaddr_storage *ss, int p)
1206{
1207 switch (ss->ss_family) {
1208 case AF_INET:
1209 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001210 break;
Sage Weil31b80062009-10-06 11:31:13 -07001211 case AF_INET6:
1212 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001213 break;
Sage Weil31b80062009-10-06 11:31:13 -07001214 }
1215}
1216
1217/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001218 * Unlike other *_pton function semantics, zero indicates success.
1219 */
1220static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1221 char delim, const char **ipend)
1222{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001223 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1224 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001225
1226 memset(ss, 0, sizeof(*ss));
1227
1228 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1229 ss->ss_family = AF_INET;
1230 return 0;
1231 }
1232
1233 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1234 ss->ss_family = AF_INET6;
1235 return 0;
1236 }
1237
1238 return -EINVAL;
1239}
1240
1241/*
1242 * Extract hostname string and resolve using kernel DNS facility.
1243 */
1244#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1245static int ceph_dns_resolve_name(const char *name, size_t namelen,
1246 struct sockaddr_storage *ss, char delim, const char **ipend)
1247{
1248 const char *end, *delim_p;
1249 char *colon_p, *ip_addr = NULL;
1250 int ip_len, ret;
1251
1252 /*
1253 * The end of the hostname occurs immediately preceding the delimiter or
1254 * the port marker (':') where the delimiter takes precedence.
1255 */
1256 delim_p = memchr(name, delim, namelen);
1257 colon_p = memchr(name, ':', namelen);
1258
1259 if (delim_p && colon_p)
1260 end = delim_p < colon_p ? delim_p : colon_p;
1261 else if (!delim_p && colon_p)
1262 end = colon_p;
1263 else {
1264 end = delim_p;
1265 if (!end) /* case: hostname:/ */
1266 end = name + namelen;
1267 }
1268
1269 if (end <= name)
1270 return -EINVAL;
1271
1272 /* do dns_resolve upcall */
1273 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1274 if (ip_len > 0)
1275 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1276 else
1277 ret = -ESRCH;
1278
1279 kfree(ip_addr);
1280
1281 *ipend = end;
1282
1283 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1284 ret, ret ? "failed" : ceph_pr_addr(ss));
1285
1286 return ret;
1287}
1288#else
1289static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1290 struct sockaddr_storage *ss, char delim, const char **ipend)
1291{
1292 return -EINVAL;
1293}
1294#endif
1295
1296/*
1297 * Parse a server name (IP or hostname). If a valid IP address is not found
1298 * then try to extract a hostname to resolve using userspace DNS upcall.
1299 */
1300static int ceph_parse_server_name(const char *name, size_t namelen,
1301 struct sockaddr_storage *ss, char delim, const char **ipend)
1302{
1303 int ret;
1304
1305 ret = ceph_pton(name, namelen, ss, delim, ipend);
1306 if (ret)
1307 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1308
1309 return ret;
1310}
1311
1312/*
Sage Weil31b80062009-10-06 11:31:13 -07001313 * Parse an ip[:port] list into an addr array. Use the default
1314 * monitor port if a port isn't specified.
1315 */
1316int ceph_parse_ips(const char *c, const char *end,
1317 struct ceph_entity_addr *addr,
1318 int max_count, int *count)
1319{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001320 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001321 const char *p = c;
1322
1323 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1324 for (i = 0; i < max_count; i++) {
1325 const char *ipend;
1326 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001327 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001328 char delim = ',';
1329
1330 if (*p == '[') {
1331 delim = ']';
1332 p++;
1333 }
Sage Weil31b80062009-10-06 11:31:13 -07001334
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001335 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1336 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001337 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001338 ret = -EINVAL;
1339
Sage Weil31b80062009-10-06 11:31:13 -07001340 p = ipend;
1341
Sage Weil39139f62010-07-08 09:54:52 -07001342 if (delim == ']') {
1343 if (*p != ']') {
1344 dout("missing matching ']'\n");
1345 goto bad;
1346 }
1347 p++;
1348 }
1349
Sage Weil31b80062009-10-06 11:31:13 -07001350 /* port? */
1351 if (p < end && *p == ':') {
1352 port = 0;
1353 p++;
1354 while (p < end && *p >= '0' && *p <= '9') {
1355 port = (port * 10) + (*p - '0');
1356 p++;
1357 }
1358 if (port > 65535 || port == 0)
1359 goto bad;
1360 } else {
1361 port = CEPH_MON_PORT;
1362 }
1363
1364 addr_set_port(ss, port);
1365
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001366 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001367
1368 if (p == end)
1369 break;
1370 if (*p != ',')
1371 goto bad;
1372 p++;
1373 }
1374
1375 if (p != end)
1376 goto bad;
1377
1378 if (count)
1379 *count = i + 1;
1380 return 0;
1381
1382bad:
Sage Weil39139f62010-07-08 09:54:52 -07001383 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001384 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001385}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001386EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001387
Sage Weileed0ef22009-11-10 14:34:36 -08001388static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001389{
Sage Weileed0ef22009-11-10 14:34:36 -08001390 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001391
1392 if (verify_hello(con) < 0)
1393 return -1;
1394
Sage Weil63f2d212009-11-03 15:17:56 -08001395 ceph_decode_addr(&con->actual_peer_addr);
1396 ceph_decode_addr(&con->peer_addr_for_me);
1397
Sage Weil31b80062009-10-06 11:31:13 -07001398 /*
1399 * Make sure the other end is who we wanted. note that the other
1400 * end may not yet know their ip address, so if it's 0.0.0.0, give
1401 * them the benefit of the doubt.
1402 */
Sage Weil103e2d32010-01-07 16:12:36 -08001403 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1404 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001405 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1406 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001407 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001408 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001409 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001410 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001411 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001412 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001413 return -1;
1414 }
1415
1416 /*
1417 * did we learn our address?
1418 */
1419 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1420 int port = addr_port(&con->msgr->inst.addr.in_addr);
1421
1422 memcpy(&con->msgr->inst.addr.in_addr,
1423 &con->peer_addr_for_me.in_addr,
1424 sizeof(con->peer_addr_for_me.in_addr));
1425 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001426 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001427 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001428 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001429 }
1430
Sage Weileed0ef22009-11-10 14:34:36 -08001431 set_bit(NEGOTIATING, &con->state);
1432 prepare_read_connect(con);
1433 return 0;
1434}
1435
Sage Weil04a419f2009-12-23 09:30:21 -08001436static void fail_protocol(struct ceph_connection *con)
1437{
1438 reset_connection(con);
1439 set_bit(CLOSED, &con->state); /* in case there's queued work */
Sage Weil04a419f2009-12-23 09:30:21 -08001440}
1441
Sage Weileed0ef22009-11-10 14:34:36 -08001442static int process_connect(struct ceph_connection *con)
1443{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001444 u64 sup_feat = con->msgr->supported_features;
1445 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001446 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001447 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001448
Sage Weileed0ef22009-11-10 14:34:36 -08001449 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1450
Sage Weil31b80062009-10-06 11:31:13 -07001451 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001452 case CEPH_MSGR_TAG_FEATURES:
1453 pr_err("%s%lld %s feature set mismatch,"
1454 " my %llx < server's %llx, missing %llx\n",
1455 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001456 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001457 sup_feat, server_feat, server_feat & ~sup_feat);
1458 con->error_msg = "missing required protocol features";
1459 fail_protocol(con);
1460 return -1;
1461
Sage Weil31b80062009-10-06 11:31:13 -07001462 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001463 pr_err("%s%lld %s protocol version mismatch,"
1464 " my %d != server's %d\n",
1465 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001466 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001467 le32_to_cpu(con->out_connect.protocol_version),
1468 le32_to_cpu(con->in_reply.protocol_version));
1469 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001470 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001471 return -1;
1472
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001473 case CEPH_MSGR_TAG_BADAUTHORIZER:
1474 con->auth_retry++;
1475 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1476 con->auth_retry);
1477 if (con->auth_retry == 2) {
1478 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001479 return -1;
1480 }
1481 con->auth_retry = 1;
Alex Eldere2200422012-05-23 14:35:23 -05001482 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001483 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001484 if (ret < 0)
1485 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001486 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001487 break;
Sage Weil31b80062009-10-06 11:31:13 -07001488
1489 case CEPH_MSGR_TAG_RESETSESSION:
1490 /*
1491 * If we connected with a large connect_seq but the peer
1492 * has no record of a session with us (no connection, or
1493 * connect_seq == 0), they will send RESETSESION to indicate
1494 * that they must have reset their session, and may have
1495 * dropped messages.
1496 */
1497 dout("process_connect got RESET peer seq %u\n",
1498 le32_to_cpu(con->in_connect.connect_seq));
1499 pr_err("%s%lld %s connection reset\n",
1500 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001501 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001502 reset_connection(con);
Alex Eldere2200422012-05-23 14:35:23 -05001503 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001504 ret = prepare_write_connect(con);
1505 if (ret < 0)
1506 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001507 prepare_read_connect(con);
1508
1509 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001510 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001511 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1512 if (con->ops->peer_reset)
1513 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001514 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07001515 if (test_bit(CLOSED, &con->state) ||
1516 test_bit(OPENING, &con->state))
1517 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001518 break;
1519
1520 case CEPH_MSGR_TAG_RETRY_SESSION:
1521 /*
1522 * If we sent a smaller connect_seq than the peer has, try
1523 * again with a larger value.
1524 */
1525 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1526 le32_to_cpu(con->out_connect.connect_seq),
1527 le32_to_cpu(con->in_connect.connect_seq));
1528 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Alex Eldere2200422012-05-23 14:35:23 -05001529 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001530 ret = prepare_write_connect(con);
1531 if (ret < 0)
1532 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001533 prepare_read_connect(con);
1534 break;
1535
1536 case CEPH_MSGR_TAG_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 Eldere2200422012-05-23 14:35:23 -05001546 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001547 ret = prepare_write_connect(con);
1548 if (ret < 0)
1549 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001550 prepare_read_connect(con);
1551 break;
1552
1553 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001554 if (req_feat & ~server_feat) {
1555 pr_err("%s%lld %s protocol feature mismatch,"
1556 " my required %llx > server's %llx, need %llx\n",
1557 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001558 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001559 req_feat, server_feat, req_feat & ~server_feat);
1560 con->error_msg = "missing required protocol features";
1561 fail_protocol(con);
1562 return -1;
1563 }
Sage Weil31b80062009-10-06 11:31:13 -07001564 clear_bit(CONNECTING, &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
1707 if (IS_ERR(bv))
1708 return PTR_ERR(bv);
1709
1710 left = min((int)(data_len - con->in_msg_pos.data_pos),
1711 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1712
1713 p = kmap(bv->bv_page) + bv->bv_offset;
1714
1715 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1716 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001717 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001718 con->in_data_crc =
1719 crc32c(con->in_data_crc,
1720 p + con->in_msg_pos.page_pos, ret);
1721 kunmap(bv->bv_page);
1722 if (ret <= 0)
1723 return ret;
1724 con->in_msg_pos.data_pos += ret;
1725 con->in_msg_pos.page_pos += ret;
1726 if (con->in_msg_pos.page_pos == bv->bv_len) {
1727 con->in_msg_pos.page_pos = 0;
1728 iter_bio_next(bio_iter, bio_seg);
1729 }
1730
1731 return ret;
1732}
1733#endif
1734
Sage Weil31b80062009-10-06 11:31:13 -07001735/*
1736 * read (part of) a message.
1737 */
1738static int read_partial_message(struct ceph_connection *con)
1739{
1740 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05001741 int size;
1742 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001743 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00001744 unsigned int front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001745 bool do_datacrc = !con->msgr->nocrc;
Sage Weilae187562010-04-22 07:47:01 -07001746 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001747 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001748
1749 dout("read_partial_message con %p msg %p\n", con, m);
1750
1751 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05001752 size = sizeof (con->in_hdr);
1753 end = size;
1754 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05001755 if (ret <= 0)
1756 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001757
1758 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1759 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1760 pr_err("read_partial_message bad hdr "
1761 " crc %u != expected %u\n",
1762 crc, con->in_hdr.crc);
1763 return -EBADMSG;
1764 }
1765
Sage Weil31b80062009-10-06 11:31:13 -07001766 front_len = le32_to_cpu(con->in_hdr.front_len);
1767 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1768 return -EIO;
1769 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1770 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1771 return -EIO;
1772 data_len = le32_to_cpu(con->in_hdr.data_len);
1773 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1774 return -EIO;
1775
Sage Weilae187562010-04-22 07:47:01 -07001776 /* verify seq# */
1777 seq = le64_to_cpu(con->in_hdr.seq);
1778 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001779 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001780 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001781 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001782 seq, con->in_seq + 1);
1783 con->in_base_pos = -front_len - middle_len - data_len -
1784 sizeof(m->footer);
1785 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001786 return 0;
1787 } else if ((s64)seq - (s64)con->in_seq > 1) {
1788 pr_err("read_partial_message bad seq %lld expected %lld\n",
1789 seq, con->in_seq + 1);
1790 con->error_msg = "bad message sequence # for incoming message";
1791 return -EBADMSG;
1792 }
1793
Sage Weil31b80062009-10-06 11:31:13 -07001794 /* allocate message? */
1795 if (!con->in_msg) {
1796 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1797 con->in_hdr.front_len, con->in_hdr.data_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05001798 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
Sage Weil31b80062009-10-06 11:31:13 -07001799 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001800 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001801 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001802 con->in_base_pos = -front_len - middle_len - data_len -
1803 sizeof(m->footer);
1804 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001805 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001806 return 0;
1807 }
Sage Weila79832f2010-04-01 16:06:19 -07001808 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001809 con->error_msg =
1810 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001811 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001812 }
Alex Elder38941f82012-06-01 14:56:43 -05001813
1814 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07001815 m = con->in_msg;
1816 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001817 if (m->middle)
1818 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001819
1820 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001821 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001822 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001823 else
1824 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001825 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001826 }
1827
1828 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001829 ret = read_partial_message_section(con, &m->front, front_len,
1830 &con->in_front_crc);
1831 if (ret <= 0)
1832 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001833
1834 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001835 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001836 ret = read_partial_message_section(con, &m->middle->vec,
1837 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001838 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001839 if (ret <= 0)
1840 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001841 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001842#ifdef CONFIG_BLOCK
1843 if (m->bio && !m->bio_iter)
1844 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1845#endif
Sage Weil31b80062009-10-06 11:31:13 -07001846
1847 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001848 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001849 if (m->pages) {
1850 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001851 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001852 if (ret <= 0)
1853 return ret;
1854#ifdef CONFIG_BLOCK
1855 } else if (m->bio) {
1856
1857 ret = read_partial_message_bio(con,
1858 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001859 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001860 if (ret <= 0)
1861 return ret;
1862#endif
1863 } else {
1864 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001865 }
1866 }
1867
Sage Weil31b80062009-10-06 11:31:13 -07001868 /* footer */
Alex Elderfd516532012-05-10 10:29:50 -05001869 size = sizeof (m->footer);
1870 end += size;
1871 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05001872 if (ret <= 0)
1873 return ret;
1874
Sage Weil31b80062009-10-06 11:31:13 -07001875 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1876 m, front_len, m->footer.front_crc, middle_len,
1877 m->footer.middle_crc, data_len, m->footer.data_crc);
1878
1879 /* crc ok? */
1880 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1881 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1882 m, con->in_front_crc, m->footer.front_crc);
1883 return -EBADMSG;
1884 }
1885 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1886 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1887 m, con->in_middle_crc, m->footer.middle_crc);
1888 return -EBADMSG;
1889 }
Alex Elderbca064d2012-02-15 07:43:54 -06001890 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001891 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1892 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1893 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1894 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1895 return -EBADMSG;
1896 }
1897
1898 return 1; /* done! */
1899}
1900
1901/*
1902 * Process message. This happens in the worker thread. The callback should
1903 * be careful not to do anything that waits on other incoming messages or it
1904 * may deadlock.
1905 */
1906static void process_message(struct ceph_connection *con)
1907{
Sage Weil5e095e82009-12-14 14:30:34 -08001908 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001909
Alex Elder38941f82012-06-01 14:56:43 -05001910 BUG_ON(con->in_msg->con != con);
1911 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08001912 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001913 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07001914 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07001915
1916 /* if first message, set peer_name */
1917 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001918 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001919
Sage Weil31b80062009-10-06 11:31:13 -07001920 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001921 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001922
1923 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1924 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001925 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001926 le16_to_cpu(msg->hdr.type),
1927 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1928 le32_to_cpu(msg->hdr.front_len),
1929 le32_to_cpu(msg->hdr.data_len),
1930 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1931 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001932
1933 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001934 prepare_read_tag(con);
1935}
1936
1937
1938/*
1939 * Write something to the socket. Called in a worker thread when the
1940 * socket appears to be writeable and we have something ready to send.
1941 */
1942static int try_write(struct ceph_connection *con)
1943{
Sage Weil31b80062009-10-06 11:31:13 -07001944 int ret = 1;
1945
Sage Weild59315c2012-06-21 12:49:23 -07001946 dout("try_write start %p state %lu\n", con, con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001947
Sage Weil31b80062009-10-06 11:31:13 -07001948more:
1949 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1950
1951 /* open the socket first? */
1952 if (con->sock == NULL) {
Alex Eldera5988c42012-05-29 11:04:58 -05001953 clear_bit(NEGOTIATING, &con->state);
1954 set_bit(CONNECTING, &con->state);
1955
Alex Eldere2200422012-05-23 14:35:23 -05001956 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001957 prepare_write_banner(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001958 ret = prepare_write_connect(con);
1959 if (ret < 0)
1960 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08001961 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001962
Sage Weilcf3e5c42009-12-11 09:48:05 -08001963 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001964 con->in_tag = CEPH_MSGR_TAG_READY;
1965 dout("try_write initiating connect on %p new state %lu\n",
1966 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06001967 ret = ceph_tcp_connect(con);
1968 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07001969 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07001970 goto out;
1971 }
1972 }
1973
1974more_kvec:
1975 /* kvec data queued? */
1976 if (con->out_skip) {
1977 ret = write_partial_skip(con);
1978 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001979 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001980 }
1981 if (con->out_kvec_left) {
1982 ret = write_partial_kvec(con);
1983 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001984 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001985 }
1986
1987 /* msg pages? */
1988 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001989 if (con->out_msg_done) {
1990 ceph_msg_put(con->out_msg);
1991 con->out_msg = NULL; /* we're done with this one */
1992 goto do_next;
1993 }
1994
Sage Weil31b80062009-10-06 11:31:13 -07001995 ret = write_partial_msg_pages(con);
1996 if (ret == 1)
1997 goto more_kvec; /* we need to send the footer, too! */
1998 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08001999 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002000 if (ret < 0) {
2001 dout("try_write write_partial_msg_pages err %d\n",
2002 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002003 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002004 }
2005 }
2006
Sage Weilc86a2932009-12-14 14:04:30 -08002007do_next:
Sage Weil31b80062009-10-06 11:31:13 -07002008 if (!test_bit(CONNECTING, &con->state)) {
2009 /* is anything else pending? */
2010 if (!list_empty(&con->out_queue)) {
2011 prepare_write_message(con);
2012 goto more;
2013 }
2014 if (con->in_seq > con->in_seq_acked) {
2015 prepare_write_ack(con);
2016 goto more;
2017 }
Alex Elder928443c2012-05-22 11:41:43 -05002018 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002019 prepare_write_keepalive(con);
2020 goto more;
2021 }
2022 }
2023
2024 /* Nothing to do! */
Alex Elder928443c2012-05-22 11:41:43 -05002025 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002026 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002027 ret = 0;
2028out:
Sage Weil42961d22011-01-25 08:19:34 -08002029 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002030 return ret;
2031}
2032
2033
2034
2035/*
2036 * Read what we can from the socket.
2037 */
2038static int try_read(struct ceph_connection *con)
2039{
Sage Weil31b80062009-10-06 11:31:13 -07002040 int ret = -1;
2041
2042 if (!con->sock)
2043 return 0;
2044
2045 if (test_bit(STANDBY, &con->state))
2046 return 0;
2047
2048 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08002049
Sage Weil31b80062009-10-06 11:31:13 -07002050more:
2051 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2052 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002053
2054 /*
2055 * process_connect and process_message drop and re-take
2056 * con->mutex. make sure we handle a racing close or reopen.
2057 */
2058 if (test_bit(CLOSED, &con->state) ||
2059 test_bit(OPENING, &con->state)) {
2060 ret = -EAGAIN;
2061 goto out;
2062 }
2063
Sage Weil31b80062009-10-06 11:31:13 -07002064 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08002065 if (!test_bit(NEGOTIATING, &con->state)) {
2066 dout("try_read connecting\n");
2067 ret = read_partial_banner(con);
2068 if (ret <= 0)
Sage Weileed0ef22009-11-10 14:34:36 -08002069 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002070 ret = process_banner(con);
2071 if (ret < 0)
2072 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08002073 }
Sage Weil31b80062009-10-06 11:31:13 -07002074 ret = read_partial_connect(con);
2075 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002076 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002077 ret = process_connect(con);
2078 if (ret < 0)
2079 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002080 goto more;
2081 }
2082
2083 if (con->in_base_pos < 0) {
2084 /*
2085 * skipping + discarding content.
2086 *
2087 * FIXME: there must be a better way to do this!
2088 */
Alex Elder84495f42012-02-15 07:43:55 -06002089 static char buf[SKIP_BUF_SIZE];
2090 int skip = min((int) sizeof (buf), -con->in_base_pos);
2091
Sage Weil31b80062009-10-06 11:31:13 -07002092 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2093 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2094 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002095 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002096 con->in_base_pos += ret;
2097 if (con->in_base_pos)
2098 goto more;
2099 }
2100 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2101 /*
2102 * what's next?
2103 */
2104 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2105 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002106 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002107 dout("try_read got tag %d\n", (int)con->in_tag);
2108 switch (con->in_tag) {
2109 case CEPH_MSGR_TAG_MSG:
2110 prepare_read_message(con);
2111 break;
2112 case CEPH_MSGR_TAG_ACK:
2113 prepare_read_ack(con);
2114 break;
2115 case CEPH_MSGR_TAG_CLOSE:
2116 set_bit(CLOSED, &con->state); /* fixme */
Sage Weil98bdb0a2011-01-25 08:17:48 -08002117 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002118 default:
2119 goto bad_tag;
2120 }
2121 }
2122 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2123 ret = read_partial_message(con);
2124 if (ret <= 0) {
2125 switch (ret) {
2126 case -EBADMSG:
2127 con->error_msg = "bad crc";
2128 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002129 break;
Sage Weil31b80062009-10-06 11:31:13 -07002130 case -EIO:
2131 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002132 break;
Sage Weil31b80062009-10-06 11:31:13 -07002133 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002134 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002135 }
2136 if (con->in_tag == CEPH_MSGR_TAG_READY)
2137 goto more;
2138 process_message(con);
2139 goto more;
2140 }
2141 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2142 ret = read_partial_ack(con);
2143 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002144 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002145 process_ack(con);
2146 goto more;
2147 }
2148
Sage Weil31b80062009-10-06 11:31:13 -07002149out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002150 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002151 return ret;
2152
2153bad_tag:
2154 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2155 con->error_msg = "protocol error, garbage tag";
2156 ret = -1;
2157 goto out;
2158}
2159
2160
2161/*
2162 * Atomically queue work on a connection. Bump @con reference to
2163 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002164 */
2165static void queue_con(struct ceph_connection *con)
2166{
Sage Weil31b80062009-10-06 11:31:13 -07002167 if (!con->ops->get(con)) {
2168 dout("queue_con %p ref count 0\n", con);
2169 return;
2170 }
2171
Tejun Heof363e452011-01-03 14:49:46 +01002172 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002173 dout("queue_con %p - already queued\n", con);
2174 con->ops->put(con);
2175 } else {
2176 dout("queue_con %p\n", con);
2177 }
2178}
2179
2180/*
2181 * Do some work on a connection. Drop a connection ref when we're done.
2182 */
2183static void con_work(struct work_struct *work)
2184{
2185 struct ceph_connection *con = container_of(work, struct ceph_connection,
2186 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002187 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002188
Sage Weil9dd46582010-04-28 13:51:50 -07002189 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002190restart:
Alex Elder188048b2012-06-20 21:53:53 -05002191 if (test_and_clear_bit(SOCK_CLOSED, &con->flags)) {
2192 if (test_bit(CONNECTING, &con->state))
2193 con->error_msg = "connection failed";
2194 else
2195 con->error_msg = "socket closed";
2196 goto fault;
2197 }
2198
Alex Elder928443c2012-05-22 11:41:43 -05002199 if (test_and_clear_bit(BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002200 dout("con_work %p backing off\n", con);
2201 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2202 round_jiffies_relative(con->delay))) {
2203 dout("con_work %p backoff %lu\n", con, con->delay);
2204 mutex_unlock(&con->mutex);
2205 return;
2206 } else {
2207 con->ops->put(con);
2208 dout("con_work %p FAILED to back off %lu\n", con,
2209 con->delay);
2210 }
2211 }
Sage Weil9dd46582010-04-28 13:51:50 -07002212
Sage Weile00de342011-03-04 12:25:05 -08002213 if (test_bit(STANDBY, &con->state)) {
2214 dout("con_work %p STANDBY\n", con);
2215 goto done;
2216 }
Sage Weil31b80062009-10-06 11:31:13 -07002217 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2218 dout("con_work CLOSED\n");
2219 con_close_socket(con);
2220 goto done;
2221 }
2222 if (test_and_clear_bit(OPENING, &con->state)) {
2223 /* reopen w/ new peer */
2224 dout("con_work OPENING\n");
2225 con_close_socket(con);
2226 }
2227
Sage Weil0da5d702011-05-19 11:21:05 -07002228 ret = try_read(con);
2229 if (ret == -EAGAIN)
2230 goto restart;
2231 if (ret < 0)
2232 goto fault;
2233
2234 ret = try_write(con);
2235 if (ret == -EAGAIN)
2236 goto restart;
2237 if (ret < 0)
2238 goto fault;
Sage Weil31b80062009-10-06 11:31:13 -07002239
2240done:
Sage Weil9dd46582010-04-28 13:51:50 -07002241 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002242done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002243 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002244 return;
2245
2246fault:
2247 mutex_unlock(&con->mutex);
2248 ceph_fault(con); /* error/fault path */
2249 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002250}
2251
2252
2253/*
2254 * Generic error/fault handler. A retry mechanism is used with
2255 * exponential backoff
2256 */
2257static void ceph_fault(struct ceph_connection *con)
2258{
2259 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002260 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002261 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002262 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002263
Alex Elder928443c2012-05-22 11:41:43 -05002264 if (test_bit(LOSSYTX, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002265 dout("fault on LOSSYTX channel\n");
2266 goto out;
2267 }
2268
Sage Weilec302642009-12-22 10:43:42 -08002269 mutex_lock(&con->mutex);
Sage Weil91e45ce32010-02-15 12:05:09 -08002270 if (test_bit(CLOSED, &con->state))
2271 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002272
Sage Weil31b80062009-10-06 11:31:13 -07002273 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002274
2275 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002276 BUG_ON(con->in_msg->con != con);
2277 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08002278 ceph_msg_put(con->in_msg);
2279 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07002280 con->ops->put(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002281 }
Sage Weil31b80062009-10-06 11:31:13 -07002282
Sage Weile80a52d2010-02-25 12:40:45 -08002283 /* Requeue anything that hasn't been acked */
2284 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002285
Sage Weile76661d2011-03-03 10:10:15 -08002286 /* If there are no messages queued or keepalive pending, place
2287 * the connection in a STANDBY state */
2288 if (list_empty(&con->out_queue) &&
Alex Elder928443c2012-05-22 11:41:43 -05002289 !test_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weile00de342011-03-04 12:25:05 -08002290 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elder928443c2012-05-22 11:41:43 -05002291 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002292 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08002293 } else {
2294 /* retry after a delay. */
2295 if (con->delay == 0)
2296 con->delay = BASE_DELAY_INTERVAL;
2297 else if (con->delay < MAX_DELAY_INTERVAL)
2298 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002299 con->ops->get(con);
2300 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002301 round_jiffies_relative(con->delay))) {
2302 dout("fault queued %p delay %lu\n", con, con->delay);
2303 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002304 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002305 dout("fault failed to queue %p delay %lu, backoff\n",
2306 con, con->delay);
2307 /*
2308 * In many cases we see a socket state change
2309 * while con_work is running and end up
2310 * queuing (non-delayed) work, such that we
2311 * can't backoff with a delay. Set a flag so
2312 * that when con_work restarts we schedule the
2313 * delay then.
2314 */
Alex Elder928443c2012-05-22 11:41:43 -05002315 set_bit(BACKOFF, &con->flags);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002316 }
Sage Weil31b80062009-10-06 11:31:13 -07002317 }
2318
Sage Weil91e45ce32010-02-15 12:05:09 -08002319out_unlock:
2320 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002321out:
Sage Weil161fd652010-02-25 12:38:57 -08002322 /*
2323 * in case we faulted due to authentication, invalidate our
2324 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002325 */
Sage Weil161fd652010-02-25 12:38:57 -08002326 if (con->auth_retry && con->ops->invalidate_authorizer) {
2327 dout("calling invalidate_authorizer()\n");
2328 con->ops->invalidate_authorizer(con);
2329 }
2330
Sage Weil31b80062009-10-06 11:31:13 -07002331 if (con->ops->fault)
2332 con->ops->fault(con);
2333}
2334
2335
2336
2337/*
Alex Elder15d98822012-05-26 23:26:43 -05002338 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002339 */
Alex Elder15d98822012-05-26 23:26:43 -05002340void ceph_messenger_init(struct ceph_messenger *msgr,
2341 struct ceph_entity_addr *myaddr,
2342 u32 supported_features,
2343 u32 required_features,
2344 bool nocrc)
Sage Weil31b80062009-10-06 11:31:13 -07002345{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002346 msgr->supported_features = supported_features;
2347 msgr->required_features = required_features;
2348
Sage Weil31b80062009-10-06 11:31:13 -07002349 spin_lock_init(&msgr->global_seq_lock);
2350
Sage Weil31b80062009-10-06 11:31:13 -07002351 if (myaddr)
2352 msgr->inst.addr = *myaddr;
2353
2354 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002355 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002356 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002357 encode_my_addr(msgr);
Alex Elder15d98822012-05-26 23:26:43 -05002358 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002359
Alex Elder15d98822012-05-26 23:26:43 -05002360 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002361}
Alex Elder15d98822012-05-26 23:26:43 -05002362EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002363
Sage Weile00de342011-03-04 12:25:05 -08002364static void clear_standby(struct ceph_connection *con)
2365{
2366 /* come back from STANDBY? */
2367 if (test_and_clear_bit(STANDBY, &con->state)) {
2368 mutex_lock(&con->mutex);
2369 dout("clear_standby %p and ++connect_seq\n", con);
2370 con->connect_seq++;
Alex Elder928443c2012-05-22 11:41:43 -05002371 WARN_ON(test_bit(WRITE_PENDING, &con->flags));
2372 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags));
Sage Weile00de342011-03-04 12:25:05 -08002373 mutex_unlock(&con->mutex);
2374 }
2375}
2376
Sage Weil31b80062009-10-06 11:31:13 -07002377/*
2378 * Queue up an outgoing message on the given connection.
2379 */
2380void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2381{
2382 if (test_bit(CLOSED, &con->state)) {
2383 dout("con_send %p closed, dropping %p\n", con, msg);
2384 ceph_msg_put(msg);
2385 return;
2386 }
2387
2388 /* set src+dst */
Sage Weildbad1852010-03-25 15:45:38 -07002389 msg->hdr.src = con->msgr->inst.name;
Sage Weil31b80062009-10-06 11:31:13 -07002390
Sage Weil3ca02ef2010-03-01 15:25:00 -08002391 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2392
Sage Weile84346b2010-05-11 21:20:38 -07002393 msg->needs_out_seq = true;
2394
Sage Weil31b80062009-10-06 11:31:13 -07002395 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08002396 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002397
Alex Elder38941f82012-06-01 14:56:43 -05002398 BUG_ON(msg->con != NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002399 msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002400 BUG_ON(msg->con == NULL);
2401
Sage Weil31b80062009-10-06 11:31:13 -07002402 BUG_ON(!list_empty(&msg->list_head));
2403 list_add_tail(&msg->list_head, &con->out_queue);
2404 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2405 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2406 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2407 le32_to_cpu(msg->hdr.front_len),
2408 le32_to_cpu(msg->hdr.middle_len),
2409 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002410 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002411
2412 /* if there wasn't anything waiting to send before, queue
2413 * new work */
Sage Weile00de342011-03-04 12:25:05 -08002414 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002415 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002416 queue_con(con);
2417}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002418EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002419
2420/*
2421 * Revoke a message that was previously queued for send
2422 */
Alex Elder6740a842012-06-01 14:56:43 -05002423void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002424{
Alex Elder6740a842012-06-01 14:56:43 -05002425 struct ceph_connection *con = msg->con;
2426
2427 if (!con)
2428 return; /* Message not in our possession */
2429
Sage Weilec302642009-12-22 10:43:42 -08002430 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002431 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05002432 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002433 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -05002434 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002435 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -05002436 msg->con = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05002437 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05002438
Sage Weil31b80062009-10-06 11:31:13 -07002439 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002440 }
2441 if (con->out_msg == msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002442 dout("%s %p msg %p - was sending\n", __func__, con, msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002443 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002444 if (con->out_kvec_is_msg) {
2445 con->out_skip = con->out_kvec_bytes;
2446 con->out_kvec_is_msg = false;
2447 }
Sage Weiled98ada2010-07-05 12:15:14 -07002448 msg->hdr.seq = 0;
Alex Elder92ce0342012-06-04 14:43:33 -05002449
2450 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07002451 }
Sage Weilec302642009-12-22 10:43:42 -08002452 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002453}
2454
2455/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002456 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002457 */
Alex Elder8921d112012-06-01 14:56:43 -05002458void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002459{
Alex Elder8921d112012-06-01 14:56:43 -05002460 struct ceph_connection *con;
2461
2462 BUG_ON(msg == NULL);
2463 if (!msg->con) {
2464 dout("%s msg %p null con\n", __func__, msg);
2465
2466 return; /* Message not in our possession */
2467 }
2468
2469 con = msg->con;
Sage Weil350b1c32009-12-22 10:45:45 -08002470 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05002471 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002472 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
2473 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
2474 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002475
2476 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05002477 dout("%s %p msg %p revoked\n", __func__, con, msg);
2478 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08002479 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002480 front_len -
2481 middle_len -
2482 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002483 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002484 ceph_msg_put(con->in_msg);
2485 con->in_msg = NULL;
2486 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002487 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002488 } else {
Alex Elder8921d112012-06-01 14:56:43 -05002489 dout("%s %p in_msg %p msg %p no-op\n",
2490 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002491 }
2492 mutex_unlock(&con->mutex);
2493}
2494
2495/*
Sage Weil31b80062009-10-06 11:31:13 -07002496 * Queue a keepalive byte to ensure the tcp connection is alive.
2497 */
2498void ceph_con_keepalive(struct ceph_connection *con)
2499{
Sage Weile00de342011-03-04 12:25:05 -08002500 dout("con_keepalive %p\n", con);
2501 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002502 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2503 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002504 queue_con(con);
2505}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002506EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002507
2508
2509/*
2510 * construct a new message with given type, size
2511 * the new msg has a ref count of 1.
2512 */
Sage Weilb61c2762011-08-09 15:03:46 -07002513struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2514 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002515{
2516 struct ceph_msg *m;
2517
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002518 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002519 if (m == NULL)
2520 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002521 kref_init(&m->kref);
Alex Elder38941f82012-06-01 14:56:43 -05002522
2523 m->con = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002524 INIT_LIST_HEAD(&m->list_head);
2525
Sage Weil45c6ceb2010-05-11 15:01:51 -07002526 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002527 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002528 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2529 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002530 m->hdr.front_len = cpu_to_le32(front_len);
2531 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002532 m->hdr.data_len = 0;
2533 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002534 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002535 m->footer.front_crc = 0;
2536 m->footer.middle_crc = 0;
2537 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002538 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002539 m->front_max = front_len;
2540 m->front_is_vmalloc = false;
2541 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002542 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002543 m->pool = NULL;
2544
Henry C Changca208922011-05-03 02:29:56 +00002545 /* middle */
2546 m->middle = NULL;
2547
2548 /* data */
2549 m->nr_pages = 0;
2550 m->page_alignment = 0;
2551 m->pages = NULL;
2552 m->pagelist = NULL;
2553 m->bio = NULL;
2554 m->bio_iter = NULL;
2555 m->bio_seg = 0;
2556 m->trail = NULL;
2557
Sage Weil31b80062009-10-06 11:31:13 -07002558 /* front */
2559 if (front_len) {
2560 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002561 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002562 PAGE_KERNEL);
2563 m->front_is_vmalloc = true;
2564 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002565 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002566 }
2567 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002568 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002569 front_len);
2570 goto out2;
2571 }
2572 } else {
2573 m->front.iov_base = NULL;
2574 }
2575 m->front.iov_len = front_len;
2576
Sage Weilbb257662010-04-01 16:07:23 -07002577 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002578 return m;
2579
2580out2:
2581 ceph_msg_put(m);
2582out:
Sage Weilb61c2762011-08-09 15:03:46 -07002583 if (!can_fail) {
2584 pr_err("msg_new can't create type %d front %d\n", type,
2585 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002586 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002587 } else {
2588 dout("msg_new can't create type %d front %d\n", type,
2589 front_len);
2590 }
Sage Weila79832f2010-04-01 16:06:19 -07002591 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002592}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002593EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002594
2595/*
Sage Weil31b80062009-10-06 11:31:13 -07002596 * Allocate "middle" portion of a message, if it is needed and wasn't
2597 * allocated by alloc_msg. This allows us to read a small fixed-size
2598 * per-type header in the front and then gracefully fail (i.e.,
2599 * propagate the error to the caller based on info in the front) when
2600 * the middle is too large.
2601 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002602static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002603{
2604 int type = le16_to_cpu(msg->hdr.type);
2605 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2606
2607 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2608 ceph_msg_type_name(type), middle_len);
2609 BUG_ON(!middle_len);
2610 BUG_ON(msg->middle);
2611
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002612 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002613 if (!msg->middle)
2614 return -ENOMEM;
2615 return 0;
2616}
2617
Yehuda Sadeh24504182010-01-08 13:58:34 -08002618/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05002619 * Allocate a message for receiving an incoming message on a
2620 * connection, and save the result in con->in_msg. Uses the
2621 * connection's private alloc_msg op if available.
2622 *
2623 * Returns true if the message should be skipped, false otherwise.
2624 * If true is returned (skip message), con->in_msg will be NULL.
2625 * If false is returned, con->in_msg will contain a pointer to the
2626 * newly-allocated message, or NULL in case of memory exhaustion.
Yehuda Sadeh24504182010-01-08 13:58:34 -08002627 */
Alex Elder1c20f2d2012-06-04 14:43:32 -05002628static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2629 struct ceph_msg_header *hdr)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002630{
2631 int type = le16_to_cpu(hdr->type);
2632 int front_len = le32_to_cpu(hdr->front_len);
2633 int middle_len = le32_to_cpu(hdr->middle_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002634 int ret;
2635
Alex Elder1c20f2d2012-06-04 14:43:32 -05002636 BUG_ON(con->in_msg != NULL);
2637
Yehuda Sadeh24504182010-01-08 13:58:34 -08002638 if (con->ops->alloc_msg) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002639 int skip = 0;
2640
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002641 mutex_unlock(&con->mutex);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002642 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002643 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002644 if (con->in_msg) {
Sage Weil36eb71a2012-06-21 12:47:08 -07002645 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002646 BUG_ON(con->in_msg->con == NULL);
2647 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002648 if (skip)
2649 con->in_msg = NULL;
2650
2651 if (!con->in_msg)
2652 return skip != 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002653 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002654 if (!con->in_msg) {
2655 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2656 if (!con->in_msg) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002657 pr_err("unable to allocate msg type %d len %d\n",
2658 type, front_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002659 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002660 }
Sage Weil36eb71a2012-06-21 12:47:08 -07002661 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002662 BUG_ON(con->in_msg->con == NULL);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002663 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002664 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002665 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002666
Alex Elder1c20f2d2012-06-04 14:43:32 -05002667 if (middle_len && !con->in_msg->middle) {
2668 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002669 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002670 ceph_msg_put(con->in_msg);
2671 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002672 }
2673 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002674
Alex Elder1c20f2d2012-06-04 14:43:32 -05002675 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002676}
2677
Sage Weil31b80062009-10-06 11:31:13 -07002678
2679/*
2680 * Free a generically kmalloc'd message.
2681 */
2682void ceph_msg_kfree(struct ceph_msg *m)
2683{
2684 dout("msg_kfree %p\n", m);
2685 if (m->front_is_vmalloc)
2686 vfree(m->front.iov_base);
2687 else
2688 kfree(m->front.iov_base);
2689 kfree(m);
2690}
2691
2692/*
2693 * Drop a msg ref. Destroy as needed.
2694 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002695void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002696{
Sage Weilc2e552e2009-12-07 15:55:05 -08002697 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002698
Sage Weilc2e552e2009-12-07 15:55:05 -08002699 dout("ceph_msg_put last one on %p\n", m);
2700 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002701
Sage Weilc2e552e2009-12-07 15:55:05 -08002702 /* drop middle, data, if any */
2703 if (m->middle) {
2704 ceph_buffer_put(m->middle);
2705 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002706 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002707 m->nr_pages = 0;
2708 m->pages = NULL;
2709
Sage Weil58bb3b32009-12-23 12:12:31 -08002710 if (m->pagelist) {
2711 ceph_pagelist_release(m->pagelist);
2712 kfree(m->pagelist);
2713 m->pagelist = NULL;
2714 }
2715
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002716 m->trail = NULL;
2717
Sage Weilc2e552e2009-12-07 15:55:05 -08002718 if (m->pool)
2719 ceph_msgpool_put(m->pool, m);
2720 else
2721 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002722}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002723EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002724
2725void ceph_msg_dump(struct ceph_msg *msg)
2726{
2727 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2728 msg->front_max, msg->nr_pages);
2729 print_hex_dump(KERN_DEBUG, "header: ",
2730 DUMP_PREFIX_OFFSET, 16, 1,
2731 &msg->hdr, sizeof(msg->hdr), true);
2732 print_hex_dump(KERN_DEBUG, " front: ",
2733 DUMP_PREFIX_OFFSET, 16, 1,
2734 msg->front.iov_base, msg->front.iov_len, true);
2735 if (msg->middle)
2736 print_hex_dump(KERN_DEBUG, "middle: ",
2737 DUMP_PREFIX_OFFSET, 16, 1,
2738 msg->middle->vec.iov_base,
2739 msg->middle->vec.iov_len, true);
2740 print_hex_dump(KERN_DEBUG, "footer: ",
2741 DUMP_PREFIX_OFFSET, 16, 1,
2742 &msg->footer, sizeof(msg->footer), true);
2743}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002744EXPORT_SYMBOL(ceph_msg_dump);