blob: ae082d95fc726fd7369a39234acad4df228cb6f5 [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 Elderbc18f4b2012-06-20 21:53:53 -050032/*
33 * We track the state of the socket on a given connection using
34 * values defined below. The transition to a new socket state is
35 * handled by a function which verifies we aren't coming from an
36 * unexpected state.
37 *
38 * --------
39 * | NEW* | transient initial state
40 * --------
41 * | con_sock_state_init()
42 * v
43 * ----------
44 * | CLOSED | initialized, but no socket (and no
45 * ---------- TCP connection)
46 * ^ \
47 * | \ con_sock_state_connecting()
48 * | ----------------------
49 * | \
50 * + con_sock_state_closed() \
51 * |\ \
52 * | \ \
53 * | ----------- \
54 * | | CLOSING | socket event; \
55 * | ----------- await close \
56 * | ^ |
57 * | | |
58 * | + con_sock_state_closing() |
59 * | / \ |
60 * | / --------------- |
61 * | / \ v
62 * | / --------------
63 * | / -----------------| CONNECTING | socket created, TCP
64 * | | / -------------- connect initiated
65 * | | | con_sock_state_connected()
66 * | | v
67 * -------------
68 * | CONNECTED | TCP connection established
69 * -------------
70 *
71 * State values for ceph_connection->sock_state; NEW is assumed to be 0.
72 */
Alex Elderce2c8902012-05-22 22:15:49 -050073
74#define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
75#define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
76#define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
77#define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
78#define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
79
Sage Weil31b80062009-10-06 11:31:13 -070080/* static tag bytes (protocol control messages) */
81static char tag_msg = CEPH_MSGR_TAG_MSG;
82static char tag_ack = CEPH_MSGR_TAG_ACK;
83static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
84
Sage Weila6a53492010-04-13 14:07:07 -070085#ifdef CONFIG_LOCKDEP
86static struct lock_class_key socket_class;
87#endif
88
Alex Elder84495f42012-02-15 07:43:55 -060089/*
90 * When skipping (ignoring) a block of input we read it into a "skip
91 * buffer," which is this many bytes in size.
92 */
93#define SKIP_BUF_SIZE 1024
Sage Weil31b80062009-10-06 11:31:13 -070094
95static void queue_con(struct ceph_connection *con);
96static void con_work(struct work_struct *);
97static void ceph_fault(struct ceph_connection *con);
98
Sage Weil31b80062009-10-06 11:31:13 -070099/*
Alex Elderf64a9312012-01-23 15:49:27 -0600100 * Nicely render a sockaddr as a string. An array of formatted
101 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -0700102 */
Alex Elderf64a9312012-01-23 15:49:27 -0600103#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
104#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
105#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
106#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
107
108static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
109static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -0700110
Alex Elder57666512012-01-23 15:49:27 -0600111static struct page *zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -0600112
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700113const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -0700114{
115 int i;
116 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -0600117 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
118 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -0700119
Alex Elderf64a9312012-01-23 15:49:27 -0600120 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -0700121 s = addr_str[i];
122
123 switch (ss->ss_family) {
124 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -0600125 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
126 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -0700127 break;
128
129 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -0600130 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
131 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -0700132 break;
133
134 default:
Alex Elderd3002b92012-02-14 14:05:33 -0600135 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
136 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -0700137 }
138
139 return s;
140}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700141EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700142
Sage Weil63f2d212009-11-03 15:17:56 -0800143static void encode_my_addr(struct ceph_messenger *msgr)
144{
145 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
146 ceph_encode_addr(&msgr->my_enc_addr);
147}
148
Sage Weil31b80062009-10-06 11:31:13 -0700149/*
150 * work queue for all reading and writing to/from the socket.
151 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600152static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700153
Alex Elder6173d1f2012-02-14 14:05:33 -0600154void _ceph_msgr_exit(void)
155{
Alex Elderd3002b92012-02-14 14:05:33 -0600156 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600157 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600158 ceph_msgr_wq = NULL;
159 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600160
Alex Elder6173d1f2012-02-14 14:05:33 -0600161 BUG_ON(zero_page == NULL);
162 kunmap(zero_page);
163 page_cache_release(zero_page);
164 zero_page = NULL;
165}
166
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700167int ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700168{
Alex Elder57666512012-01-23 15:49:27 -0600169 BUG_ON(zero_page != NULL);
170 zero_page = ZERO_PAGE(0);
171 page_cache_get(zero_page);
172
Tejun Heof363e452011-01-03 14:49:46 +0100173 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600174 if (ceph_msgr_wq)
175 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600176
Alex Elder6173d1f2012-02-14 14:05:33 -0600177 pr_err("msgr_init failed to create workqueue\n");
178 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600179
Alex Elder6173d1f2012-02-14 14:05:33 -0600180 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700181}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700182EXPORT_SYMBOL(ceph_msgr_init);
Sage Weil31b80062009-10-06 11:31:13 -0700183
184void ceph_msgr_exit(void)
185{
Alex Elder57666512012-01-23 15:49:27 -0600186 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600187
Alex Elder6173d1f2012-02-14 14:05:33 -0600188 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700189}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700190EXPORT_SYMBOL(ceph_msgr_exit);
Sage Weil31b80062009-10-06 11:31:13 -0700191
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700192void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700193{
194 flush_workqueue(ceph_msgr_wq);
195}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700196EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700197
Alex Elderce2c8902012-05-22 22:15:49 -0500198/* Connection socket state transition functions */
199
200static void con_sock_state_init(struct ceph_connection *con)
201{
202 int old_state;
203
204 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
205 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
206 printk("%s: unexpected old state %d\n", __func__, old_state);
207}
208
209static void con_sock_state_connecting(struct ceph_connection *con)
210{
211 int old_state;
212
213 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
214 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
215 printk("%s: unexpected old state %d\n", __func__, old_state);
216}
217
218static void con_sock_state_connected(struct ceph_connection *con)
219{
220 int old_state;
221
222 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
223 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
224 printk("%s: unexpected old state %d\n", __func__, old_state);
225}
226
227static void con_sock_state_closing(struct ceph_connection *con)
228{
229 int old_state;
230
231 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
232 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
233 old_state != CON_SOCK_STATE_CONNECTED &&
234 old_state != CON_SOCK_STATE_CLOSING))
235 printk("%s: unexpected old state %d\n", __func__, old_state);
236}
237
238static void con_sock_state_closed(struct ceph_connection *con)
239{
240 int old_state;
241
242 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
243 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
244 old_state != CON_SOCK_STATE_CLOSING))
245 printk("%s: unexpected old state %d\n", __func__, old_state);
246}
Sage Weila922d382010-05-29 09:41:23 -0700247
Sage Weil31b80062009-10-06 11:31:13 -0700248/*
249 * socket callback functions
250 */
251
252/* data available on socket, or listen socket received a connect */
Alex Elder327800b2012-05-22 11:41:43 -0500253static void ceph_sock_data_ready(struct sock *sk, int count_unused)
Sage Weil31b80062009-10-06 11:31:13 -0700254{
Alex Elderbd406142012-01-23 15:49:27 -0600255 struct ceph_connection *con = sk->sk_user_data;
256
Sage Weil31b80062009-10-06 11:31:13 -0700257 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500258 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700259 con, con->state);
260 queue_con(con);
261 }
262}
263
264/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500265static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700266{
Alex Elderd3002b92012-02-14 14:05:33 -0600267 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700268
Jim Schutt182fac22012-02-29 08:30:58 -0700269 /* only queue to workqueue if there is data we want to write,
270 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500271 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700272 * doesn't get called again until try_write() fills the socket
273 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
274 * and net/core/stream.c:sk_stream_write_space().
275 */
Alex Elder928443c2012-05-22 11:41:43 -0500276 if (test_bit(WRITE_PENDING, &con->flags)) {
Jim Schutt182fac22012-02-29 08:30:58 -0700277 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500278 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700279 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
280 queue_con(con);
281 }
Sage Weil31b80062009-10-06 11:31:13 -0700282 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500283 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700284 }
Sage Weil31b80062009-10-06 11:31:13 -0700285}
286
287/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500288static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700289{
Alex Elderbd406142012-01-23 15:49:27 -0600290 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700291
Alex Elder327800b2012-05-22 11:41:43 -0500292 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700293 con, con->state, sk->sk_state);
294
295 if (test_bit(CLOSED, &con->state))
296 return;
297
298 switch (sk->sk_state) {
299 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500300 dout("%s TCP_CLOSE\n", __func__);
Sage Weil31b80062009-10-06 11:31:13 -0700301 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500302 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500303 con_sock_state_closing(con);
Alex Elderd65c9e02012-06-20 21:53:53 -0500304 set_bit(SOCK_CLOSED, &con->flags);
305 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -0700306 break;
307 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500308 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500309 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700310 queue_con(con);
311 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600312 default: /* Everything else is uninteresting */
313 break;
Sage Weil31b80062009-10-06 11:31:13 -0700314 }
315}
316
317/*
318 * set up socket callbacks
319 */
320static void set_sock_callbacks(struct socket *sock,
321 struct ceph_connection *con)
322{
323 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600324 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500325 sk->sk_data_ready = ceph_sock_data_ready;
326 sk->sk_write_space = ceph_sock_write_space;
327 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700328}
329
330
331/*
332 * socket helpers
333 */
334
335/*
336 * initiate connection to a remote socket.
337 */
Alex Elder41617d02012-02-14 14:05:33 -0600338static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700339{
Sage Weilf91d3472010-07-01 15:18:31 -0700340 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700341 struct socket *sock;
342 int ret;
343
344 BUG_ON(con->sock);
Sage Weilf91d3472010-07-01 15:18:31 -0700345 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
346 IPPROTO_TCP, &sock);
Sage Weil31b80062009-10-06 11:31:13 -0700347 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600348 return ret;
Sage Weil31b80062009-10-06 11:31:13 -0700349 sock->sk->sk_allocation = GFP_NOFS;
350
Sage Weila6a53492010-04-13 14:07:07 -0700351#ifdef CONFIG_LOCKDEP
352 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
353#endif
354
Sage Weil31b80062009-10-06 11:31:13 -0700355 set_sock_callbacks(sock, con);
356
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700357 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700358
Sage Weil89a86be2012-06-09 14:19:21 -0700359 con_sock_state_connecting(con);
Sage Weilf91d3472010-07-01 15:18:31 -0700360 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
361 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700362 if (ret == -EINPROGRESS) {
363 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700364 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700365 sock->sk->sk_state);
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600366 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700367 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700368 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700369 sock_release(sock);
Sage Weil31b80062009-10-06 11:31:13 -0700370 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -0700371
Alex Elder41617d02012-02-14 14:05:33 -0600372 return ret;
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600373 }
374 con->sock = sock;
Alex Elder41617d02012-02-14 14:05:33 -0600375 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700376}
377
378static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
379{
380 struct kvec iov = {buf, len};
381 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800382 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700383
Sage Weil98bdb0a2011-01-25 08:17:48 -0800384 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
385 if (r == -EAGAIN)
386 r = 0;
387 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700388}
389
390/*
391 * write something. @more is true if caller will be sending more data
392 * shortly.
393 */
394static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
395 size_t kvlen, size_t len, int more)
396{
397 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800398 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700399
400 if (more)
401 msg.msg_flags |= MSG_MORE;
402 else
403 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
404
Sage Weil42961d22011-01-25 08:19:34 -0800405 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
406 if (r == -EAGAIN)
407 r = 0;
408 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700409}
410
Alex Elder31739132012-03-07 11:40:08 -0600411static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
412 int offset, size_t size, int more)
413{
414 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
415 int ret;
416
417 ret = kernel_sendpage(sock, page, offset, size, flags);
418 if (ret == -EAGAIN)
419 ret = 0;
420
421 return ret;
422}
423
Sage Weil31b80062009-10-06 11:31:13 -0700424
425/*
426 * Shutdown/close the socket for the given connection.
427 */
428static int con_close_socket(struct ceph_connection *con)
429{
430 int rc;
431
432 dout("con_close_socket on %p sock %p\n", con, con->sock);
433 if (!con->sock)
434 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700435 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
436 sock_release(con->sock);
437 con->sock = NULL;
Alex Elder456ea462012-06-20 21:53:53 -0500438
439 /*
440 * Forcibly clear the SOCK_CLOSE flag. It gets set
441 * independent of the connection mutex, and we could have
442 * received a socket close event before we had the chance to
443 * shut the socket down.
444 */
Alex Eldera8d00e32012-06-20 21:53:53 -0500445 clear_bit(SOCK_CLOSED, &con->flags);
Alex Elderce2c8902012-05-22 22:15:49 -0500446 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700447 return rc;
448}
449
450/*
451 * Reset a connection. Discard all incoming and outgoing messages
452 * and clear *_seq state.
453 */
454static void ceph_msg_remove(struct ceph_msg *msg)
455{
456 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500457 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -0700458 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -0500459 msg->con = NULL;
460
Sage Weil31b80062009-10-06 11:31:13 -0700461 ceph_msg_put(msg);
462}
463static void ceph_msg_remove_list(struct list_head *head)
464{
465 while (!list_empty(head)) {
466 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
467 list_head);
468 ceph_msg_remove(msg);
469 }
470}
471
472static void reset_connection(struct ceph_connection *con)
473{
474 /* reset connection, out_queue, msg_ and connect_seq */
475 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700476 ceph_msg_remove_list(&con->out_queue);
477 ceph_msg_remove_list(&con->out_sent);
478
Sage Weilcf3e5c42009-12-11 09:48:05 -0800479 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500480 BUG_ON(con->in_msg->con != con);
481 con->in_msg->con = NULL;
Sage Weilcf3e5c42009-12-11 09:48:05 -0800482 ceph_msg_put(con->in_msg);
483 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -0700484 con->ops->put(con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800485 }
486
Sage Weil31b80062009-10-06 11:31:13 -0700487 con->connect_seq = 0;
488 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800489 if (con->out_msg) {
490 ceph_msg_put(con->out_msg);
491 con->out_msg = NULL;
492 }
Sage Weil31b80062009-10-06 11:31:13 -0700493 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700494 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700495}
496
497/*
498 * mark a peer down. drop any open connections.
499 */
500void ceph_con_close(struct ceph_connection *con)
501{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700502 dout("con_close %p peer %s\n", con,
503 ceph_pr_addr(&con->peer_addr.in_addr));
Alex Eldera5988c42012-05-29 11:04:58 -0500504 clear_bit(NEGOTIATING, &con->state);
Alex Elderbb9e6bb2012-06-20 21:53:53 -0500505 clear_bit(CONNECTING, &con->state);
Alex Eldere27947c2012-05-23 14:35:23 -0500506 clear_bit(CONNECTED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700507 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Alex Eldera5988c42012-05-29 11:04:58 -0500508 set_bit(CLOSED, &con->state);
509
Alex Elder928443c2012-05-22 11:41:43 -0500510 clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */
511 clear_bit(KEEPALIVE_PENDING, &con->flags);
512 clear_bit(WRITE_PENDING, &con->flags);
Alex Eldera5988c42012-05-29 11:04:58 -0500513
Sage Weilec302642009-12-22 10:43:42 -0800514 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700515 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700516 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800517 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800518 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700519 queue_con(con);
520}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700521EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700522
523/*
Sage Weil31b80062009-10-06 11:31:13 -0700524 * Reopen a closed connection, with a new peer address.
525 */
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700526void ceph_con_open(struct ceph_connection *con,
527 __u8 entity_type, __u64 entity_num,
528 struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -0700529{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700530 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700531 set_bit(OPENING, &con->state);
Alex Eldera5988c42012-05-29 11:04:58 -0500532 WARN_ON(!test_and_clear_bit(CLOSED, &con->state));
533
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700534 con->peer_name.type = (__u8) entity_type;
535 con->peer_name.num = cpu_to_le64(entity_num);
536
Sage Weil31b80062009-10-06 11:31:13 -0700537 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800538 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700539 queue_con(con);
540}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700541EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700542
543/*
Sage Weil87b315a2010-03-22 14:51:18 -0700544 * return true if this connection ever successfully opened
545 */
546bool ceph_con_opened(struct ceph_connection *con)
547{
548 return con->connect_seq > 0;
549}
550
551/*
Sage Weil31b80062009-10-06 11:31:13 -0700552 * initialize a new connection.
553 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500554void ceph_con_init(struct ceph_connection *con, void *private,
555 const struct ceph_connection_operations *ops,
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700556 struct ceph_messenger *msgr)
Sage Weil31b80062009-10-06 11:31:13 -0700557{
558 dout("con_init %p\n", con);
559 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500560 con->private = private;
561 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700562 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500563
564 con_sock_state_init(con);
565
Sage Weilec302642009-12-22 10:43:42 -0800566 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700567 INIT_LIST_HEAD(&con->out_queue);
568 INIT_LIST_HEAD(&con->out_sent);
569 INIT_DELAYED_WORK(&con->work, con_work);
Alex Eldera5988c42012-05-29 11:04:58 -0500570
571 set_bit(CLOSED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700572}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700573EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700574
575
576/*
577 * We maintain a global counter to order connection attempts. Get
578 * a unique seq greater than @gt.
579 */
580static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
581{
582 u32 ret;
583
584 spin_lock(&msgr->global_seq_lock);
585 if (msgr->global_seq < gt)
586 msgr->global_seq = gt;
587 ret = ++msgr->global_seq;
588 spin_unlock(&msgr->global_seq_lock);
589 return ret;
590}
591
Alex Eldere2200422012-05-23 14:35:23 -0500592static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600593{
594 con->out_kvec_left = 0;
595 con->out_kvec_bytes = 0;
596 con->out_kvec_cur = &con->out_kvec[0];
597}
598
Alex Eldere2200422012-05-23 14:35:23 -0500599static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600600 size_t size, void *data)
601{
602 int index;
603
604 index = con->out_kvec_left;
605 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
606
607 con->out_kvec[index].iov_len = size;
608 con->out_kvec[index].iov_base = data;
609 con->out_kvec_left++;
610 con->out_kvec_bytes += size;
611}
Sage Weil31b80062009-10-06 11:31:13 -0700612
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500613#ifdef CONFIG_BLOCK
614static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
615{
616 if (!bio) {
617 *iter = NULL;
618 *seg = 0;
619 return;
620 }
621 *iter = bio;
622 *seg = bio->bi_idx;
623}
624
625static void iter_bio_next(struct bio **bio_iter, int *seg)
626{
627 if (*bio_iter == NULL)
628 return;
629
630 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
631
632 (*seg)++;
633 if (*seg == (*bio_iter)->bi_vcnt)
634 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
635}
636#endif
637
Alex Elder739c9052012-06-11 14:57:13 -0500638static void prepare_write_message_data(struct ceph_connection *con)
639{
640 struct ceph_msg *msg = con->out_msg;
641
642 BUG_ON(!msg);
643 BUG_ON(!msg->hdr.data_len);
644
645 /* initialize page iterator */
646 con->out_msg_pos.page = 0;
647 if (msg->pages)
648 con->out_msg_pos.page_pos = msg->page_alignment;
649 else
650 con->out_msg_pos.page_pos = 0;
Alex Elder572c5882012-06-11 14:57:13 -0500651#ifdef CONFIG_BLOCK
Alex Elderabdaa6a2012-06-11 14:57:13 -0500652 if (msg->bio)
Alex Elder572c5882012-06-11 14:57:13 -0500653 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
654#endif
Alex Elder739c9052012-06-11 14:57:13 -0500655 con->out_msg_pos.data_pos = 0;
656 con->out_msg_pos.did_page_crc = false;
657 con->out_more = 1; /* data + footer will follow */
658}
659
Sage Weil31b80062009-10-06 11:31:13 -0700660/*
661 * Prepare footer for currently outgoing message, and finish things
662 * off. Assumes out_kvec* are already valid.. we just add on to the end.
663 */
Alex Elder859eb792012-02-14 14:05:33 -0600664static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700665{
666 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600667 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700668
Alex Elderfd154f32012-06-11 14:57:13 -0500669 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
670
Sage Weil31b80062009-10-06 11:31:13 -0700671 dout("prepare_write_message_footer %p\n", con);
672 con->out_kvec_is_msg = true;
673 con->out_kvec[v].iov_base = &m->footer;
674 con->out_kvec[v].iov_len = sizeof(m->footer);
675 con->out_kvec_bytes += sizeof(m->footer);
676 con->out_kvec_left++;
677 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800678 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700679}
680
681/*
682 * Prepare headers for the next outgoing message.
683 */
684static void prepare_write_message(struct ceph_connection *con)
685{
686 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600687 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700688
Alex Eldere2200422012-05-23 14:35:23 -0500689 con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700690 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800691 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700692
693 /* Sneak an ack in there first? If we can get it into the same
694 * TCP packet that's a good thing. */
695 if (con->in_seq > con->in_seq_acked) {
696 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -0500697 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700698 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500699 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600700 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700701 }
702
Alex Elder38941f82012-06-01 14:56:43 -0500703 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -0600704 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800705 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -0500706 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -0700707
708 /* put message on sent list */
709 ceph_msg_get(m);
710 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700711
Sage Weile84346b2010-05-11 21:20:38 -0700712 /*
713 * only assign outgoing seq # if we haven't sent this message
714 * yet. if it is requeued, resend with it's original seq.
715 */
716 if (m->needs_out_seq) {
717 m->hdr.seq = cpu_to_le64(++con->out_seq);
718 m->needs_out_seq = false;
719 }
Sage Weil31b80062009-10-06 11:31:13 -0700720
721 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
722 m, con->out_seq, le16_to_cpu(m->hdr.type),
723 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
724 le32_to_cpu(m->hdr.data_len),
725 m->nr_pages);
726 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
727
728 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -0500729 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
730 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
731 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600732
Sage Weil31b80062009-10-06 11:31:13 -0700733 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -0500734 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600735 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700736
737 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600738 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
739 con->out_msg->hdr.crc = cpu_to_le32(crc);
Alex Elderfd154f32012-06-11 14:57:13 -0500740 con->out_msg->footer.flags = 0;
Alex Eldera9a0c512012-02-15 07:43:54 -0600741
742 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
743 con->out_msg->footer.front_crc = cpu_to_le32(crc);
744 if (m->middle) {
745 crc = crc32c(0, m->middle->vec.iov_base,
746 m->middle->vec.iov_len);
747 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
748 } else
Sage Weil31b80062009-10-06 11:31:13 -0700749 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -0500750 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700751 le32_to_cpu(con->out_msg->footer.front_crc),
752 le32_to_cpu(con->out_msg->footer.middle_crc));
753
754 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -0500755 con->out_msg->footer.data_crc = 0;
756 if (m->hdr.data_len)
757 prepare_write_message_data(con);
758 else
Sage Weil31b80062009-10-06 11:31:13 -0700759 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600760 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700761
Alex Elder928443c2012-05-22 11:41:43 -0500762 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700763}
764
765/*
766 * Prepare an ack.
767 */
768static void prepare_write_ack(struct ceph_connection *con)
769{
770 dout("prepare_write_ack %p %llu -> %llu\n", con,
771 con->in_seq_acked, con->in_seq);
772 con->in_seq_acked = con->in_seq;
773
Alex Eldere2200422012-05-23 14:35:23 -0500774 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600775
Alex Eldere2200422012-05-23 14:35:23 -0500776 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600777
Sage Weil31b80062009-10-06 11:31:13 -0700778 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500779 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600780 &con->out_temp_ack);
781
Sage Weil31b80062009-10-06 11:31:13 -0700782 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elder928443c2012-05-22 11:41:43 -0500783 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700784}
785
786/*
787 * Prepare to write keepalive byte.
788 */
789static void prepare_write_keepalive(struct ceph_connection *con)
790{
791 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -0500792 con_out_kvec_reset(con);
793 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Alex Elder928443c2012-05-22 11:41:43 -0500794 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700795}
796
797/*
798 * Connection negotiation.
799 */
800
Alex Elderdac1e712012-05-16 15:16:39 -0500801static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
802 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800803{
Alex Eldera3530df2012-05-16 15:16:39 -0500804 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -0500805
806 if (!con->ops->get_authorizer) {
807 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
808 con->out_connect.authorizer_len = 0;
809
Alex Elder729796b2012-05-16 15:16:39 -0500810 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -0500811 }
812
813 /* Can't hold the mutex while getting authorizer */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800814
Sage Weilec302642009-12-22 10:43:42 -0800815 mutex_unlock(&con->mutex);
Alex Elderb1c6b982012-05-16 15:16:38 -0500816
Alex Elderdac1e712012-05-16 15:16:39 -0500817 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Elder8f43fb52012-05-16 15:16:39 -0500818
Sage Weilec302642009-12-22 10:43:42 -0800819 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800820
Alex Eldera3530df2012-05-16 15:16:39 -0500821 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -0500822 return auth;
Alex Elder928443c2012-05-22 11:41:43 -0500823 if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags))
Alex Elder729796b2012-05-16 15:16:39 -0500824 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700825
Alex Elder8f43fb52012-05-16 15:16:39 -0500826 con->auth_reply_buf = auth->authorizer_reply_buf;
827 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
828
Alex Elder859eb792012-02-14 14:05:33 -0600829
Alex Elder729796b2012-05-16 15:16:39 -0500830 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800831}
832
Sage Weil31b80062009-10-06 11:31:13 -0700833/*
834 * We connected to a peer and are saying hello.
835 */
Alex Eldere825a662012-05-16 15:16:38 -0500836static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700837{
Alex Eldere2200422012-05-23 14:35:23 -0500838 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
839 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -0500840 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800841
Sage Weileed0ef22009-11-10 14:34:36 -0800842 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500843 set_bit(WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800844}
845
Alex Eldere825a662012-05-16 15:16:38 -0500846static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800847{
Eric Dumazet95c96172012-04-15 05:58:06 +0000848 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -0700849 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -0500850 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -0500851 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700852
853 switch (con->peer_name.type) {
854 case CEPH_ENTITY_TYPE_MON:
855 proto = CEPH_MONC_PROTOCOL;
856 break;
857 case CEPH_ENTITY_TYPE_OSD:
858 proto = CEPH_OSDC_PROTOCOL;
859 break;
860 case CEPH_ENTITY_TYPE_MDS:
861 proto = CEPH_MDSC_PROTOCOL;
862 break;
863 default:
864 BUG();
865 }
866
867 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
868 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800869
Alex Eldere825a662012-05-16 15:16:38 -0500870 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700871 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
872 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
873 con->out_connect.global_seq = cpu_to_le32(global_seq);
874 con->out_connect.protocol_version = cpu_to_le32(proto);
875 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700876
Alex Elderdac1e712012-05-16 15:16:39 -0500877 auth_proto = CEPH_AUTH_UNKNOWN;
878 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -0500879 if (IS_ERR(auth))
880 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -0500881
Alex Elderdac1e712012-05-16 15:16:39 -0500882 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -0500883 con->out_connect.authorizer_len = auth ?
884 cpu_to_le32(auth->authorizer_buf_len) : 0;
885
Alex Elderab166d52012-05-31 11:37:29 -0500886 con_out_kvec_reset(con);
Alex Eldere2200422012-05-23 14:35:23 -0500887 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -0500888 &con->out_connect);
889 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -0500890 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -0500891 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600892
Sage Weil31b80062009-10-06 11:31:13 -0700893 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500894 set_bit(WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800895
Alex Eldere10c7582012-05-16 15:16:38 -0500896 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700897}
898
Sage Weil31b80062009-10-06 11:31:13 -0700899/*
900 * write as much of pending kvecs to the socket as we can.
901 * 1 -> done
902 * 0 -> socket full, but more to do
903 * <0 -> error
904 */
905static int write_partial_kvec(struct ceph_connection *con)
906{
907 int ret;
908
909 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
910 while (con->out_kvec_bytes > 0) {
911 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
912 con->out_kvec_left, con->out_kvec_bytes,
913 con->out_more);
914 if (ret <= 0)
915 goto out;
916 con->out_kvec_bytes -= ret;
917 if (con->out_kvec_bytes == 0)
918 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600919
920 /* account for full iov entries consumed */
921 while (ret >= con->out_kvec_cur->iov_len) {
922 BUG_ON(!con->out_kvec_left);
923 ret -= con->out_kvec_cur->iov_len;
924 con->out_kvec_cur++;
925 con->out_kvec_left--;
926 }
927 /* and for a partially-consumed entry */
928 if (ret) {
929 con->out_kvec_cur->iov_len -= ret;
930 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700931 }
932 }
933 con->out_kvec_left = 0;
934 con->out_kvec_is_msg = false;
935 ret = 1;
936out:
937 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
938 con->out_kvec_bytes, con->out_kvec_left, ret);
939 return ret; /* done! */
940}
941
Alex Elder84ca8fc2012-06-11 14:57:13 -0500942static void out_msg_pos_next(struct ceph_connection *con, struct page *page,
943 size_t len, size_t sent, bool in_trail)
944{
945 struct ceph_msg *msg = con->out_msg;
946
947 BUG_ON(!msg);
948 BUG_ON(!sent);
949
950 con->out_msg_pos.data_pos += sent;
951 con->out_msg_pos.page_pos += sent;
Alex Elder5821bd82012-06-11 14:57:13 -0500952 if (sent < len)
953 return;
954
955 BUG_ON(sent != len);
956 con->out_msg_pos.page_pos = 0;
957 con->out_msg_pos.page++;
958 con->out_msg_pos.did_page_crc = false;
959 if (in_trail)
960 list_move_tail(&page->lru,
961 &msg->trail->head);
962 else if (msg->pagelist)
963 list_move_tail(&page->lru,
964 &msg->pagelist->head);
Alex Elder84ca8fc2012-06-11 14:57:13 -0500965#ifdef CONFIG_BLOCK
Alex Elder5821bd82012-06-11 14:57:13 -0500966 else if (msg->bio)
967 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
Alex Elder84ca8fc2012-06-11 14:57:13 -0500968#endif
Alex Elder84ca8fc2012-06-11 14:57:13 -0500969}
970
Sage Weil31b80062009-10-06 11:31:13 -0700971/*
972 * Write as much message data payload as we can. If we finish, queue
973 * up the footer.
974 * 1 -> done, footer is now queued in out_kvec[].
975 * 0 -> socket full, but more to do
976 * <0 -> error
977 */
978static int write_partial_msg_pages(struct ceph_connection *con)
979{
980 struct ceph_msg *msg = con->out_msg;
Eric Dumazet95c96172012-04-15 05:58:06 +0000981 unsigned int data_len = le32_to_cpu(msg->hdr.data_len);
Sage Weil31b80062009-10-06 11:31:13 -0700982 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600983 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700984 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700985 int total_max_write;
Alex Elder84ca8fc2012-06-11 14:57:13 -0500986 bool in_trail = false;
Alex Elder5821bd82012-06-11 14:57:13 -0500987 const size_t trail_len = (msg->trail ? msg->trail->length : 0);
988 const size_t trail_off = data_len - trail_len;
Sage Weil31b80062009-10-06 11:31:13 -0700989
990 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
Alex Elder84ca8fc2012-06-11 14:57:13 -0500991 con, msg, con->out_msg_pos.page, msg->nr_pages,
Sage Weil31b80062009-10-06 11:31:13 -0700992 con->out_msg_pos.page_pos);
993
Alex Elder5821bd82012-06-11 14:57:13 -0500994 /*
995 * Iterate through each page that contains data to be
996 * written, and send as much as possible for each.
997 *
998 * If we are calculating the data crc (the default), we will
999 * need to map the page. If we have no pages, they have
1000 * been revoked, so use the zero page.
1001 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001002 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -07001003 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001004 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -06001005 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001006
Alex Elder5821bd82012-06-11 14:57:13 -05001007 in_trail = in_trail || con->out_msg_pos.data_pos >= trail_off;
1008 if (!in_trail)
1009 total_max_write = trail_off - con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001010
Alex Elder5821bd82012-06-11 14:57:13 -05001011 if (in_trail) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001012 total_max_write = data_len - con->out_msg_pos.data_pos;
1013
1014 page = list_first_entry(&msg->trail->head,
1015 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001016 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -07001017 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -08001018 } else if (msg->pagelist) {
1019 page = list_first_entry(&msg->pagelist->head,
1020 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001021#ifdef CONFIG_BLOCK
1022 } else if (msg->bio) {
1023 struct bio_vec *bv;
1024
1025 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
1026 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -06001027 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001028 max_write = bv->bv_len;
1029#endif
Sage Weil31b80062009-10-06 11:31:13 -07001030 } else {
Alex Elder57666512012-01-23 15:49:27 -06001031 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -07001032 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001033 len = min_t(int, max_write - con->out_msg_pos.page_pos,
1034 total_max_write);
1035
Alex Elder37675b02012-03-07 11:40:08 -06001036 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -06001037 void *base;
Alex Elder5821bd82012-06-11 14:57:13 -05001038 u32 crc = le32_to_cpu(msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -06001039 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -07001040
Alex Elder8d63e312012-03-07 11:40:08 -06001041 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -07001042 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -06001043 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Elder5821bd82012-06-11 14:57:13 -05001044 crc = crc32c(crc, base, len);
Alex Elder84ca8fc2012-06-11 14:57:13 -05001045 msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -06001046 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -07001047 }
Alex Eldere36b13c2012-03-07 11:40:08 -06001048 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -06001049 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -06001050 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001051
Alex Elder0cdf9e62012-03-07 11:40:08 -06001052 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -07001053 kunmap(page);
1054
1055 if (ret <= 0)
1056 goto out;
1057
Alex Elder84ca8fc2012-06-11 14:57:13 -05001058 out_msg_pos_next(con, page, len, (size_t) ret, in_trail);
Sage Weil31b80062009-10-06 11:31:13 -07001059 }
1060
1061 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1062
1063 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001064 if (!do_datacrc)
Alex Elder84ca8fc2012-06-11 14:57:13 -05001065 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001066 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001067 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001068 ret = 1;
1069out:
1070 return ret;
1071}
1072
1073/*
1074 * write some zeros
1075 */
1076static int write_partial_skip(struct ceph_connection *con)
1077{
1078 int ret;
1079
1080 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001081 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001082
Alex Elder31739132012-03-07 11:40:08 -06001083 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001084 if (ret <= 0)
1085 goto out;
1086 con->out_skip -= ret;
1087 }
1088 ret = 1;
1089out:
1090 return ret;
1091}
1092
1093/*
1094 * Prepare to read connection handshake, or an ack.
1095 */
Sage Weileed0ef22009-11-10 14:34:36 -08001096static void prepare_read_banner(struct ceph_connection *con)
1097{
1098 dout("prepare_read_banner %p\n", con);
1099 con->in_base_pos = 0;
1100}
1101
Sage Weil31b80062009-10-06 11:31:13 -07001102static void prepare_read_connect(struct ceph_connection *con)
1103{
1104 dout("prepare_read_connect %p\n", con);
1105 con->in_base_pos = 0;
1106}
1107
1108static void prepare_read_ack(struct ceph_connection *con)
1109{
1110 dout("prepare_read_ack %p\n", con);
1111 con->in_base_pos = 0;
1112}
1113
1114static void prepare_read_tag(struct ceph_connection *con)
1115{
1116 dout("prepare_read_tag %p\n", con);
1117 con->in_base_pos = 0;
1118 con->in_tag = CEPH_MSGR_TAG_READY;
1119}
1120
1121/*
1122 * Prepare to read a message.
1123 */
1124static int prepare_read_message(struct ceph_connection *con)
1125{
1126 dout("prepare_read_message %p\n", con);
1127 BUG_ON(con->in_msg != NULL);
1128 con->in_base_pos = 0;
1129 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1130 return 0;
1131}
1132
1133
1134static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001135 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001136{
Alex Eldere6cee712012-05-10 10:29:50 -05001137 while (con->in_base_pos < end) {
1138 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001139 int have = size - left;
1140 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1141 if (ret <= 0)
1142 return ret;
1143 con->in_base_pos += ret;
1144 }
1145 return 1;
1146}
1147
1148
1149/*
1150 * Read all or part of the connect-side handshake on a new connection
1151 */
Sage Weileed0ef22009-11-10 14:34:36 -08001152static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001153{
Alex Elderfd516532012-05-10 10:29:50 -05001154 int size;
1155 int end;
1156 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001157
Sage Weileed0ef22009-11-10 14:34:36 -08001158 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001159
1160 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001161 size = strlen(CEPH_BANNER);
1162 end = size;
1163 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001164 if (ret <= 0)
1165 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001166
1167 size = sizeof (con->actual_peer_addr);
1168 end += size;
1169 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001170 if (ret <= 0)
1171 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001172
1173 size = sizeof (con->peer_addr_for_me);
1174 end += size;
1175 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001176 if (ret <= 0)
1177 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001178
Sage Weileed0ef22009-11-10 14:34:36 -08001179out:
1180 return ret;
1181}
1182
1183static int read_partial_connect(struct ceph_connection *con)
1184{
Alex Elderfd516532012-05-10 10:29:50 -05001185 int size;
1186 int end;
1187 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001188
1189 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1190
Alex Elderfd516532012-05-10 10:29:50 -05001191 size = sizeof (con->in_reply);
1192 end = size;
1193 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001194 if (ret <= 0)
1195 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001196
1197 size = le32_to_cpu(con->in_reply.authorizer_len);
1198 end += size;
1199 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001200 if (ret <= 0)
1201 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001202
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001203 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1204 con, (int)con->in_reply.tag,
1205 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001206 le32_to_cpu(con->in_reply.global_seq));
1207out:
1208 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001209
Sage Weil31b80062009-10-06 11:31:13 -07001210}
1211
1212/*
1213 * Verify the hello banner looks okay.
1214 */
1215static int verify_hello(struct ceph_connection *con)
1216{
1217 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001218 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001219 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001220 con->error_msg = "protocol error, bad banner";
1221 return -1;
1222 }
1223 return 0;
1224}
1225
1226static bool addr_is_blank(struct sockaddr_storage *ss)
1227{
1228 switch (ss->ss_family) {
1229 case AF_INET:
1230 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1231 case AF_INET6:
1232 return
1233 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1234 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1235 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1236 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1237 }
1238 return false;
1239}
1240
1241static int addr_port(struct sockaddr_storage *ss)
1242{
1243 switch (ss->ss_family) {
1244 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001245 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001246 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001247 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001248 }
1249 return 0;
1250}
1251
1252static void addr_set_port(struct sockaddr_storage *ss, int p)
1253{
1254 switch (ss->ss_family) {
1255 case AF_INET:
1256 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001257 break;
Sage Weil31b80062009-10-06 11:31:13 -07001258 case AF_INET6:
1259 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001260 break;
Sage Weil31b80062009-10-06 11:31:13 -07001261 }
1262}
1263
1264/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001265 * Unlike other *_pton function semantics, zero indicates success.
1266 */
1267static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1268 char delim, const char **ipend)
1269{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001270 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1271 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001272
1273 memset(ss, 0, sizeof(*ss));
1274
1275 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1276 ss->ss_family = AF_INET;
1277 return 0;
1278 }
1279
1280 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1281 ss->ss_family = AF_INET6;
1282 return 0;
1283 }
1284
1285 return -EINVAL;
1286}
1287
1288/*
1289 * Extract hostname string and resolve using kernel DNS facility.
1290 */
1291#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1292static int ceph_dns_resolve_name(const char *name, size_t namelen,
1293 struct sockaddr_storage *ss, char delim, const char **ipend)
1294{
1295 const char *end, *delim_p;
1296 char *colon_p, *ip_addr = NULL;
1297 int ip_len, ret;
1298
1299 /*
1300 * The end of the hostname occurs immediately preceding the delimiter or
1301 * the port marker (':') where the delimiter takes precedence.
1302 */
1303 delim_p = memchr(name, delim, namelen);
1304 colon_p = memchr(name, ':', namelen);
1305
1306 if (delim_p && colon_p)
1307 end = delim_p < colon_p ? delim_p : colon_p;
1308 else if (!delim_p && colon_p)
1309 end = colon_p;
1310 else {
1311 end = delim_p;
1312 if (!end) /* case: hostname:/ */
1313 end = name + namelen;
1314 }
1315
1316 if (end <= name)
1317 return -EINVAL;
1318
1319 /* do dns_resolve upcall */
1320 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1321 if (ip_len > 0)
1322 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1323 else
1324 ret = -ESRCH;
1325
1326 kfree(ip_addr);
1327
1328 *ipend = end;
1329
1330 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1331 ret, ret ? "failed" : ceph_pr_addr(ss));
1332
1333 return ret;
1334}
1335#else
1336static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1337 struct sockaddr_storage *ss, char delim, const char **ipend)
1338{
1339 return -EINVAL;
1340}
1341#endif
1342
1343/*
1344 * Parse a server name (IP or hostname). If a valid IP address is not found
1345 * then try to extract a hostname to resolve using userspace DNS upcall.
1346 */
1347static int ceph_parse_server_name(const char *name, size_t namelen,
1348 struct sockaddr_storage *ss, char delim, const char **ipend)
1349{
1350 int ret;
1351
1352 ret = ceph_pton(name, namelen, ss, delim, ipend);
1353 if (ret)
1354 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1355
1356 return ret;
1357}
1358
1359/*
Sage Weil31b80062009-10-06 11:31:13 -07001360 * Parse an ip[:port] list into an addr array. Use the default
1361 * monitor port if a port isn't specified.
1362 */
1363int ceph_parse_ips(const char *c, const char *end,
1364 struct ceph_entity_addr *addr,
1365 int max_count, int *count)
1366{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001367 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001368 const char *p = c;
1369
1370 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1371 for (i = 0; i < max_count; i++) {
1372 const char *ipend;
1373 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001374 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001375 char delim = ',';
1376
1377 if (*p == '[') {
1378 delim = ']';
1379 p++;
1380 }
Sage Weil31b80062009-10-06 11:31:13 -07001381
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001382 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1383 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001384 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001385 ret = -EINVAL;
1386
Sage Weil31b80062009-10-06 11:31:13 -07001387 p = ipend;
1388
Sage Weil39139f62010-07-08 09:54:52 -07001389 if (delim == ']') {
1390 if (*p != ']') {
1391 dout("missing matching ']'\n");
1392 goto bad;
1393 }
1394 p++;
1395 }
1396
Sage Weil31b80062009-10-06 11:31:13 -07001397 /* port? */
1398 if (p < end && *p == ':') {
1399 port = 0;
1400 p++;
1401 while (p < end && *p >= '0' && *p <= '9') {
1402 port = (port * 10) + (*p - '0');
1403 p++;
1404 }
1405 if (port > 65535 || port == 0)
1406 goto bad;
1407 } else {
1408 port = CEPH_MON_PORT;
1409 }
1410
1411 addr_set_port(ss, port);
1412
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001413 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001414
1415 if (p == end)
1416 break;
1417 if (*p != ',')
1418 goto bad;
1419 p++;
1420 }
1421
1422 if (p != end)
1423 goto bad;
1424
1425 if (count)
1426 *count = i + 1;
1427 return 0;
1428
1429bad:
Sage Weil39139f62010-07-08 09:54:52 -07001430 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001431 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001432}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001433EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001434
Sage Weileed0ef22009-11-10 14:34:36 -08001435static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001436{
Sage Weileed0ef22009-11-10 14:34:36 -08001437 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001438
1439 if (verify_hello(con) < 0)
1440 return -1;
1441
Sage Weil63f2d212009-11-03 15:17:56 -08001442 ceph_decode_addr(&con->actual_peer_addr);
1443 ceph_decode_addr(&con->peer_addr_for_me);
1444
Sage Weil31b80062009-10-06 11:31:13 -07001445 /*
1446 * Make sure the other end is who we wanted. note that the other
1447 * end may not yet know their ip address, so if it's 0.0.0.0, give
1448 * them the benefit of the doubt.
1449 */
Sage Weil103e2d32010-01-07 16:12:36 -08001450 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1451 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001452 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1453 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001454 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001455 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001456 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001457 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001458 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001459 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001460 return -1;
1461 }
1462
1463 /*
1464 * did we learn our address?
1465 */
1466 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1467 int port = addr_port(&con->msgr->inst.addr.in_addr);
1468
1469 memcpy(&con->msgr->inst.addr.in_addr,
1470 &con->peer_addr_for_me.in_addr,
1471 sizeof(con->peer_addr_for_me.in_addr));
1472 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001473 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001474 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001475 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001476 }
1477
Sage Weileed0ef22009-11-10 14:34:36 -08001478 return 0;
1479}
1480
Sage Weil04a419f2009-12-23 09:30:21 -08001481static void fail_protocol(struct ceph_connection *con)
1482{
1483 reset_connection(con);
1484 set_bit(CLOSED, &con->state); /* in case there's queued work */
Sage Weil04a419f2009-12-23 09:30:21 -08001485}
1486
Sage Weileed0ef22009-11-10 14:34:36 -08001487static int process_connect(struct ceph_connection *con)
1488{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001489 u64 sup_feat = con->msgr->supported_features;
1490 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001491 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001492 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001493
Sage Weileed0ef22009-11-10 14:34:36 -08001494 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1495
Sage Weil31b80062009-10-06 11:31:13 -07001496 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001497 case CEPH_MSGR_TAG_FEATURES:
1498 pr_err("%s%lld %s feature set mismatch,"
1499 " my %llx < server's %llx, missing %llx\n",
1500 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001501 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001502 sup_feat, server_feat, server_feat & ~sup_feat);
1503 con->error_msg = "missing required protocol features";
1504 fail_protocol(con);
1505 return -1;
1506
Sage Weil31b80062009-10-06 11:31:13 -07001507 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001508 pr_err("%s%lld %s protocol version mismatch,"
1509 " my %d != server's %d\n",
1510 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001511 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001512 le32_to_cpu(con->out_connect.protocol_version),
1513 le32_to_cpu(con->in_reply.protocol_version));
1514 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001515 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001516 return -1;
1517
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001518 case CEPH_MSGR_TAG_BADAUTHORIZER:
1519 con->auth_retry++;
1520 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1521 con->auth_retry);
1522 if (con->auth_retry == 2) {
1523 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001524 return -1;
1525 }
1526 con->auth_retry = 1;
Alex Eldere825a662012-05-16 15:16:38 -05001527 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001528 if (ret < 0)
1529 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001530 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001531 break;
Sage Weil31b80062009-10-06 11:31:13 -07001532
1533 case CEPH_MSGR_TAG_RESETSESSION:
1534 /*
1535 * If we connected with a large connect_seq but the peer
1536 * has no record of a session with us (no connection, or
1537 * connect_seq == 0), they will send RESETSESION to indicate
1538 * that they must have reset their session, and may have
1539 * dropped messages.
1540 */
1541 dout("process_connect got RESET peer seq %u\n",
1542 le32_to_cpu(con->in_connect.connect_seq));
1543 pr_err("%s%lld %s connection reset\n",
1544 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001545 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001546 reset_connection(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
1552 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001553 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001554 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1555 if (con->ops->peer_reset)
1556 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001557 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07001558 if (test_bit(CLOSED, &con->state) ||
1559 test_bit(OPENING, &con->state))
1560 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001561 break;
1562
1563 case CEPH_MSGR_TAG_RETRY_SESSION:
1564 /*
1565 * If we sent a smaller connect_seq than the peer has, try
1566 * again with a larger value.
1567 */
1568 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1569 le32_to_cpu(con->out_connect.connect_seq),
1570 le32_to_cpu(con->in_connect.connect_seq));
1571 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001572 ret = prepare_write_connect(con);
1573 if (ret < 0)
1574 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001575 prepare_read_connect(con);
1576 break;
1577
1578 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1579 /*
1580 * If we sent a smaller global_seq than the peer has, try
1581 * again with a larger value.
1582 */
Sage Weileed0ef22009-11-10 14:34:36 -08001583 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001584 con->peer_global_seq,
1585 le32_to_cpu(con->in_connect.global_seq));
1586 get_global_seq(con->msgr,
1587 le32_to_cpu(con->in_connect.global_seq));
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001588 ret = prepare_write_connect(con);
1589 if (ret < 0)
1590 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001591 prepare_read_connect(con);
1592 break;
1593
1594 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001595 if (req_feat & ~server_feat) {
1596 pr_err("%s%lld %s protocol feature mismatch,"
1597 " my required %llx > server's %llx, need %llx\n",
1598 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001599 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001600 req_feat, server_feat, req_feat & ~server_feat);
1601 con->error_msg = "missing required protocol features";
1602 fail_protocol(con);
1603 return -1;
1604 }
Alex Elder3ec50d12012-05-23 14:35:23 -05001605 clear_bit(NEGOTIATING, &con->state);
Alex Eldere27947c2012-05-23 14:35:23 -05001606 set_bit(CONNECTED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001607 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1608 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001609 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001610 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1611 con->peer_global_seq,
1612 le32_to_cpu(con->in_reply.connect_seq),
1613 con->connect_seq);
1614 WARN_ON(con->connect_seq !=
1615 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001616
1617 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elder928443c2012-05-22 11:41:43 -05001618 set_bit(LOSSYTX, &con->flags);
Sage Weil92ac41d2009-12-14 14:56:56 -08001619
Sage Weil31b80062009-10-06 11:31:13 -07001620 prepare_read_tag(con);
1621 break;
1622
1623 case CEPH_MSGR_TAG_WAIT:
1624 /*
1625 * If there is a connection race (we are opening
1626 * connections to each other), one of us may just have
1627 * to WAIT. This shouldn't happen if we are the
1628 * client.
1629 */
Sage Weil04177882011-05-12 15:33:17 -07001630 pr_err("process_connect got WAIT as client\n");
1631 con->error_msg = "protocol error, got WAIT as client";
1632 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001633
1634 default:
1635 pr_err("connect protocol error, will retry\n");
1636 con->error_msg = "protocol error, garbage tag during connect";
1637 return -1;
1638 }
1639 return 0;
1640}
1641
1642
1643/*
1644 * read (part of) an ack
1645 */
1646static int read_partial_ack(struct ceph_connection *con)
1647{
Alex Elderfd516532012-05-10 10:29:50 -05001648 int size = sizeof (con->in_temp_ack);
1649 int end = size;
1650
1651 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001652}
1653
1654
1655/*
1656 * We can finally discard anything that's been acked.
1657 */
1658static void process_ack(struct ceph_connection *con)
1659{
1660 struct ceph_msg *m;
1661 u64 ack = le64_to_cpu(con->in_temp_ack);
1662 u64 seq;
1663
Sage Weil31b80062009-10-06 11:31:13 -07001664 while (!list_empty(&con->out_sent)) {
1665 m = list_first_entry(&con->out_sent, struct ceph_msg,
1666 list_head);
1667 seq = le64_to_cpu(m->hdr.seq);
1668 if (seq > ack)
1669 break;
1670 dout("got ack for seq %llu type %d at %p\n", seq,
1671 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001672 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001673 ceph_msg_remove(m);
1674 }
Sage Weil31b80062009-10-06 11:31:13 -07001675 prepare_read_tag(con);
1676}
1677
1678
1679
1680
Yehuda Sadeh24504182010-01-08 13:58:34 -08001681static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001682 struct kvec *section,
1683 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001684{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001685 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001686
Yehuda Sadeh24504182010-01-08 13:58:34 -08001687 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001688
Yehuda Sadeh24504182010-01-08 13:58:34 -08001689 while (section->iov_len < sec_len) {
1690 BUG_ON(section->iov_base == NULL);
1691 left = sec_len - section->iov_len;
1692 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1693 section->iov_len, left);
1694 if (ret <= 0)
1695 return ret;
1696 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001697 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001698 if (section->iov_len == sec_len)
1699 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001700
1701 return 1;
1702}
1703
Alex Elder1c20f2d2012-06-04 14:43:32 -05001704static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
1705 struct ceph_msg_header *hdr);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001706
1707
1708static int read_partial_message_pages(struct ceph_connection *con,
1709 struct page **pages,
Eric Dumazet95c96172012-04-15 05:58:06 +00001710 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001711{
1712 void *p;
1713 int ret;
1714 int left;
1715
1716 left = min((int)(data_len - con->in_msg_pos.data_pos),
1717 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1718 /* (page) data */
1719 BUG_ON(pages == NULL);
1720 p = kmap(pages[con->in_msg_pos.page]);
1721 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1722 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001723 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001724 con->in_data_crc =
1725 crc32c(con->in_data_crc,
1726 p + con->in_msg_pos.page_pos, ret);
1727 kunmap(pages[con->in_msg_pos.page]);
1728 if (ret <= 0)
1729 return ret;
1730 con->in_msg_pos.data_pos += ret;
1731 con->in_msg_pos.page_pos += ret;
1732 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1733 con->in_msg_pos.page_pos = 0;
1734 con->in_msg_pos.page++;
1735 }
1736
1737 return ret;
1738}
1739
1740#ifdef CONFIG_BLOCK
1741static int read_partial_message_bio(struct ceph_connection *con,
1742 struct bio **bio_iter, int *bio_seg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001743 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001744{
1745 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1746 void *p;
1747 int ret, left;
1748
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001749 left = min((int)(data_len - con->in_msg_pos.data_pos),
1750 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1751
1752 p = kmap(bv->bv_page) + bv->bv_offset;
1753
1754 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1755 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001756 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001757 con->in_data_crc =
1758 crc32c(con->in_data_crc,
1759 p + con->in_msg_pos.page_pos, ret);
1760 kunmap(bv->bv_page);
1761 if (ret <= 0)
1762 return ret;
1763 con->in_msg_pos.data_pos += ret;
1764 con->in_msg_pos.page_pos += ret;
1765 if (con->in_msg_pos.page_pos == bv->bv_len) {
1766 con->in_msg_pos.page_pos = 0;
1767 iter_bio_next(bio_iter, bio_seg);
1768 }
1769
1770 return ret;
1771}
1772#endif
1773
Sage Weil31b80062009-10-06 11:31:13 -07001774/*
1775 * read (part of) a message.
1776 */
1777static int read_partial_message(struct ceph_connection *con)
1778{
1779 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05001780 int size;
1781 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001782 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00001783 unsigned int front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001784 bool do_datacrc = !con->msgr->nocrc;
Sage Weilae187562010-04-22 07:47:01 -07001785 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001786 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001787
1788 dout("read_partial_message con %p msg %p\n", con, m);
1789
1790 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05001791 size = sizeof (con->in_hdr);
1792 end = size;
1793 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05001794 if (ret <= 0)
1795 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001796
1797 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1798 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1799 pr_err("read_partial_message bad hdr "
1800 " crc %u != expected %u\n",
1801 crc, con->in_hdr.crc);
1802 return -EBADMSG;
1803 }
1804
Sage Weil31b80062009-10-06 11:31:13 -07001805 front_len = le32_to_cpu(con->in_hdr.front_len);
1806 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1807 return -EIO;
1808 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1809 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1810 return -EIO;
1811 data_len = le32_to_cpu(con->in_hdr.data_len);
1812 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1813 return -EIO;
1814
Sage Weilae187562010-04-22 07:47:01 -07001815 /* verify seq# */
1816 seq = le64_to_cpu(con->in_hdr.seq);
1817 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001818 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001819 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001820 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001821 seq, con->in_seq + 1);
1822 con->in_base_pos = -front_len - middle_len - data_len -
1823 sizeof(m->footer);
1824 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001825 return 0;
1826 } else if ((s64)seq - (s64)con->in_seq > 1) {
1827 pr_err("read_partial_message bad seq %lld expected %lld\n",
1828 seq, con->in_seq + 1);
1829 con->error_msg = "bad message sequence # for incoming message";
1830 return -EBADMSG;
1831 }
1832
Sage Weil31b80062009-10-06 11:31:13 -07001833 /* allocate message? */
1834 if (!con->in_msg) {
1835 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1836 con->in_hdr.front_len, con->in_hdr.data_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05001837 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
Sage Weil31b80062009-10-06 11:31:13 -07001838 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001839 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001840 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001841 con->in_base_pos = -front_len - middle_len - data_len -
1842 sizeof(m->footer);
1843 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001844 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001845 return 0;
1846 }
Sage Weila79832f2010-04-01 16:06:19 -07001847 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001848 con->error_msg =
1849 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001850 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001851 }
Alex Elder38941f82012-06-01 14:56:43 -05001852
1853 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07001854 m = con->in_msg;
1855 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001856 if (m->middle)
1857 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001858
1859 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001860 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001861 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001862 else
1863 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001864 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001865 }
1866
1867 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001868 ret = read_partial_message_section(con, &m->front, front_len,
1869 &con->in_front_crc);
1870 if (ret <= 0)
1871 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001872
1873 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001874 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001875 ret = read_partial_message_section(con, &m->middle->vec,
1876 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001877 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001878 if (ret <= 0)
1879 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001880 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001881#ifdef CONFIG_BLOCK
1882 if (m->bio && !m->bio_iter)
1883 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1884#endif
Sage Weil31b80062009-10-06 11:31:13 -07001885
1886 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001887 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001888 if (m->pages) {
1889 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001890 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001891 if (ret <= 0)
1892 return ret;
1893#ifdef CONFIG_BLOCK
1894 } else if (m->bio) {
1895
1896 ret = read_partial_message_bio(con,
1897 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001898 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001899 if (ret <= 0)
1900 return ret;
1901#endif
1902 } else {
1903 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001904 }
1905 }
1906
Sage Weil31b80062009-10-06 11:31:13 -07001907 /* footer */
Alex Elderfd516532012-05-10 10:29:50 -05001908 size = sizeof (m->footer);
1909 end += size;
1910 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05001911 if (ret <= 0)
1912 return ret;
1913
Sage Weil31b80062009-10-06 11:31:13 -07001914 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1915 m, front_len, m->footer.front_crc, middle_len,
1916 m->footer.middle_crc, data_len, m->footer.data_crc);
1917
1918 /* crc ok? */
1919 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1920 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1921 m, con->in_front_crc, m->footer.front_crc);
1922 return -EBADMSG;
1923 }
1924 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1925 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1926 m, con->in_middle_crc, m->footer.middle_crc);
1927 return -EBADMSG;
1928 }
Alex Elderbca064d2012-02-15 07:43:54 -06001929 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001930 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1931 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1932 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1933 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1934 return -EBADMSG;
1935 }
1936
1937 return 1; /* done! */
1938}
1939
1940/*
1941 * Process message. This happens in the worker thread. The callback should
1942 * be careful not to do anything that waits on other incoming messages or it
1943 * may deadlock.
1944 */
1945static void process_message(struct ceph_connection *con)
1946{
Sage Weil5e095e82009-12-14 14:30:34 -08001947 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001948
Alex Elder38941f82012-06-01 14:56:43 -05001949 BUG_ON(con->in_msg->con != con);
1950 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08001951 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001952 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07001953 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07001954
1955 /* if first message, set peer_name */
1956 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001957 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001958
Sage Weil31b80062009-10-06 11:31:13 -07001959 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001960 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001961
1962 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1963 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001964 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001965 le16_to_cpu(msg->hdr.type),
1966 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1967 le32_to_cpu(msg->hdr.front_len),
1968 le32_to_cpu(msg->hdr.data_len),
1969 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1970 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001971
1972 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001973 prepare_read_tag(con);
1974}
1975
1976
1977/*
1978 * Write something to the socket. Called in a worker thread when the
1979 * socket appears to be writeable and we have something ready to send.
1980 */
1981static int try_write(struct ceph_connection *con)
1982{
Sage Weil31b80062009-10-06 11:31:13 -07001983 int ret = 1;
1984
Sage Weild59315c2012-06-21 12:49:23 -07001985 dout("try_write start %p state %lu\n", con, con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001986
Sage Weil31b80062009-10-06 11:31:13 -07001987more:
1988 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1989
1990 /* open the socket first? */
1991 if (con->sock == NULL) {
Alex Eldera5988c42012-05-29 11:04:58 -05001992 set_bit(CONNECTING, &con->state);
1993
Alex Eldere2200422012-05-23 14:35:23 -05001994 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001995 prepare_write_banner(con);
Sage Weileed0ef22009-11-10 14:34:36 -08001996 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001997
Sage Weilcf3e5c42009-12-11 09:48:05 -08001998 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001999 con->in_tag = CEPH_MSGR_TAG_READY;
2000 dout("try_write initiating connect on %p new state %lu\n",
2001 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06002002 ret = ceph_tcp_connect(con);
2003 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07002004 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07002005 goto out;
2006 }
2007 }
2008
2009more_kvec:
2010 /* kvec data queued? */
2011 if (con->out_skip) {
2012 ret = write_partial_skip(con);
2013 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002014 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002015 }
2016 if (con->out_kvec_left) {
2017 ret = write_partial_kvec(con);
2018 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002019 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002020 }
2021
2022 /* msg pages? */
2023 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08002024 if (con->out_msg_done) {
2025 ceph_msg_put(con->out_msg);
2026 con->out_msg = NULL; /* we're done with this one */
2027 goto do_next;
2028 }
2029
Sage Weil31b80062009-10-06 11:31:13 -07002030 ret = write_partial_msg_pages(con);
2031 if (ret == 1)
2032 goto more_kvec; /* we need to send the footer, too! */
2033 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002034 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002035 if (ret < 0) {
2036 dout("try_write write_partial_msg_pages err %d\n",
2037 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002038 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002039 }
2040 }
2041
Sage Weilc86a2932009-12-14 14:04:30 -08002042do_next:
Alex Elder7593af92012-05-24 11:55:03 -05002043 if (!test_bit(CONNECTING, &con->state) &&
2044 !test_bit(NEGOTIATING, &con->state)) {
Sage Weil31b80062009-10-06 11:31:13 -07002045 /* is anything else pending? */
2046 if (!list_empty(&con->out_queue)) {
2047 prepare_write_message(con);
2048 goto more;
2049 }
2050 if (con->in_seq > con->in_seq_acked) {
2051 prepare_write_ack(con);
2052 goto more;
2053 }
Alex Elder928443c2012-05-22 11:41:43 -05002054 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002055 prepare_write_keepalive(con);
2056 goto more;
2057 }
2058 }
2059
2060 /* Nothing to do! */
Alex Elder928443c2012-05-22 11:41:43 -05002061 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002062 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002063 ret = 0;
2064out:
Sage Weil42961d22011-01-25 08:19:34 -08002065 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002066 return ret;
2067}
2068
2069
2070
2071/*
2072 * Read what we can from the socket.
2073 */
2074static int try_read(struct ceph_connection *con)
2075{
Sage Weil31b80062009-10-06 11:31:13 -07002076 int ret = -1;
2077
2078 if (!con->sock)
2079 return 0;
2080
2081 if (test_bit(STANDBY, &con->state))
2082 return 0;
2083
2084 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08002085
Sage Weil31b80062009-10-06 11:31:13 -07002086more:
2087 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2088 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002089
2090 /*
2091 * process_connect and process_message drop and re-take
2092 * con->mutex. make sure we handle a racing close or reopen.
2093 */
2094 if (test_bit(CLOSED, &con->state) ||
2095 test_bit(OPENING, &con->state)) {
2096 ret = -EAGAIN;
2097 goto out;
2098 }
2099
Sage Weil31b80062009-10-06 11:31:13 -07002100 if (test_bit(CONNECTING, &con->state)) {
Alex Elder7593af92012-05-24 11:55:03 -05002101 dout("try_read connecting\n");
2102 ret = read_partial_banner(con);
2103 if (ret <= 0)
Alex Elderab166d52012-05-31 11:37:29 -05002104 goto out;
Alex Elder7593af92012-05-24 11:55:03 -05002105 ret = process_banner(con);
2106 if (ret < 0)
2107 goto out;
2108
2109 clear_bit(CONNECTING, &con->state);
2110 set_bit(NEGOTIATING, &con->state);
2111
2112 /* Banner is good, exchange connection info */
2113 ret = prepare_write_connect(con);
2114 if (ret < 0)
2115 goto out;
2116 prepare_read_connect(con);
2117
2118 /* Send connection info before awaiting response */
2119 goto out;
2120 }
2121
2122 if (test_bit(NEGOTIATING, &con->state)) {
2123 dout("try_read negotiating\n");
Sage Weil31b80062009-10-06 11:31:13 -07002124 ret = read_partial_connect(con);
2125 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002126 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002127 ret = process_connect(con);
2128 if (ret < 0)
2129 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002130 goto more;
2131 }
2132
2133 if (con->in_base_pos < 0) {
2134 /*
2135 * skipping + discarding content.
2136 *
2137 * FIXME: there must be a better way to do this!
2138 */
Alex Elder84495f42012-02-15 07:43:55 -06002139 static char buf[SKIP_BUF_SIZE];
2140 int skip = min((int) sizeof (buf), -con->in_base_pos);
2141
Sage Weil31b80062009-10-06 11:31:13 -07002142 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2143 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2144 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002145 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002146 con->in_base_pos += ret;
2147 if (con->in_base_pos)
2148 goto more;
2149 }
2150 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2151 /*
2152 * what's next?
2153 */
2154 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2155 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002156 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002157 dout("try_read got tag %d\n", (int)con->in_tag);
2158 switch (con->in_tag) {
2159 case CEPH_MSGR_TAG_MSG:
2160 prepare_read_message(con);
2161 break;
2162 case CEPH_MSGR_TAG_ACK:
2163 prepare_read_ack(con);
2164 break;
2165 case CEPH_MSGR_TAG_CLOSE:
Alex Eldere27947c2012-05-23 14:35:23 -05002166 clear_bit(CONNECTED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07002167 set_bit(CLOSED, &con->state); /* fixme */
Sage Weil98bdb0a2011-01-25 08:17:48 -08002168 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002169 default:
2170 goto bad_tag;
2171 }
2172 }
2173 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2174 ret = read_partial_message(con);
2175 if (ret <= 0) {
2176 switch (ret) {
2177 case -EBADMSG:
2178 con->error_msg = "bad crc";
2179 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002180 break;
Sage Weil31b80062009-10-06 11:31:13 -07002181 case -EIO:
2182 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002183 break;
Sage Weil31b80062009-10-06 11:31:13 -07002184 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002185 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002186 }
2187 if (con->in_tag == CEPH_MSGR_TAG_READY)
2188 goto more;
2189 process_message(con);
2190 goto more;
2191 }
2192 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2193 ret = read_partial_ack(con);
2194 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002195 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002196 process_ack(con);
2197 goto more;
2198 }
2199
Sage Weil31b80062009-10-06 11:31:13 -07002200out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002201 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002202 return ret;
2203
2204bad_tag:
2205 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2206 con->error_msg = "protocol error, garbage tag";
2207 ret = -1;
2208 goto out;
2209}
2210
2211
2212/*
2213 * Atomically queue work on a connection. Bump @con reference to
2214 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002215 */
2216static void queue_con(struct ceph_connection *con)
2217{
Sage Weil31b80062009-10-06 11:31:13 -07002218 if (!con->ops->get(con)) {
2219 dout("queue_con %p ref count 0\n", con);
2220 return;
2221 }
2222
Tejun Heof363e452011-01-03 14:49:46 +01002223 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002224 dout("queue_con %p - already queued\n", con);
2225 con->ops->put(con);
2226 } else {
2227 dout("queue_con %p\n", con);
2228 }
2229}
2230
2231/*
2232 * Do some work on a connection. Drop a connection ref when we're done.
2233 */
2234static void con_work(struct work_struct *work)
2235{
2236 struct ceph_connection *con = container_of(work, struct ceph_connection,
2237 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002238 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002239
Sage Weil9dd46582010-04-28 13:51:50 -07002240 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002241restart:
Alex Elder188048b2012-06-20 21:53:53 -05002242 if (test_and_clear_bit(SOCK_CLOSED, &con->flags)) {
Alex Eldere27947c2012-05-23 14:35:23 -05002243 if (test_and_clear_bit(CONNECTED, &con->state))
2244 con->error_msg = "socket closed";
Alex Elder7593af92012-05-24 11:55:03 -05002245 else if (test_and_clear_bit(NEGOTIATING, &con->state))
2246 con->error_msg = "negotiation failed";
2247 else if (test_and_clear_bit(CONNECTING, &con->state))
Alex Elder188048b2012-06-20 21:53:53 -05002248 con->error_msg = "connection failed";
Alex Elder7593af92012-05-24 11:55:03 -05002249 else
Alex Eldere27947c2012-05-23 14:35:23 -05002250 con->error_msg = "unrecognized con state";
Alex Elder188048b2012-06-20 21:53:53 -05002251 goto fault;
2252 }
2253
Alex Elder928443c2012-05-22 11:41:43 -05002254 if (test_and_clear_bit(BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002255 dout("con_work %p backing off\n", con);
2256 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2257 round_jiffies_relative(con->delay))) {
2258 dout("con_work %p backoff %lu\n", con, con->delay);
2259 mutex_unlock(&con->mutex);
2260 return;
2261 } else {
2262 con->ops->put(con);
2263 dout("con_work %p FAILED to back off %lu\n", con,
2264 con->delay);
2265 }
2266 }
Sage Weil9dd46582010-04-28 13:51:50 -07002267
Sage Weile00de342011-03-04 12:25:05 -08002268 if (test_bit(STANDBY, &con->state)) {
2269 dout("con_work %p STANDBY\n", con);
2270 goto done;
2271 }
Sage Weil31b80062009-10-06 11:31:13 -07002272 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2273 dout("con_work CLOSED\n");
2274 con_close_socket(con);
2275 goto done;
2276 }
2277 if (test_and_clear_bit(OPENING, &con->state)) {
2278 /* reopen w/ new peer */
2279 dout("con_work OPENING\n");
2280 con_close_socket(con);
2281 }
2282
Sage Weil0da5d702011-05-19 11:21:05 -07002283 ret = try_read(con);
2284 if (ret == -EAGAIN)
2285 goto restart;
2286 if (ret < 0)
2287 goto fault;
2288
2289 ret = try_write(con);
2290 if (ret == -EAGAIN)
2291 goto restart;
2292 if (ret < 0)
2293 goto fault;
Sage Weil31b80062009-10-06 11:31:13 -07002294
2295done:
Sage Weil9dd46582010-04-28 13:51:50 -07002296 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002297done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002298 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002299 return;
2300
2301fault:
2302 mutex_unlock(&con->mutex);
2303 ceph_fault(con); /* error/fault path */
2304 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002305}
2306
2307
2308/*
2309 * Generic error/fault handler. A retry mechanism is used with
2310 * exponential backoff
2311 */
2312static void ceph_fault(struct ceph_connection *con)
2313{
2314 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002315 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002316 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002317 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002318
Alex Elder928443c2012-05-22 11:41:43 -05002319 if (test_bit(LOSSYTX, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002320 dout("fault on LOSSYTX channel\n");
2321 goto out;
2322 }
2323
Sage Weilec302642009-12-22 10:43:42 -08002324 mutex_lock(&con->mutex);
Sage Weil91e45ce32010-02-15 12:05:09 -08002325 if (test_bit(CLOSED, &con->state))
2326 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002327
Sage Weil31b80062009-10-06 11:31:13 -07002328 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002329
2330 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002331 BUG_ON(con->in_msg->con != con);
2332 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08002333 ceph_msg_put(con->in_msg);
2334 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07002335 con->ops->put(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002336 }
Sage Weil31b80062009-10-06 11:31:13 -07002337
Sage Weile80a52d2010-02-25 12:40:45 -08002338 /* Requeue anything that hasn't been acked */
2339 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002340
Sage Weile76661d2011-03-03 10:10:15 -08002341 /* If there are no messages queued or keepalive pending, place
2342 * the connection in a STANDBY state */
2343 if (list_empty(&con->out_queue) &&
Alex Elder928443c2012-05-22 11:41:43 -05002344 !test_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weile00de342011-03-04 12:25:05 -08002345 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elder928443c2012-05-22 11:41:43 -05002346 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002347 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08002348 } else {
2349 /* retry after a delay. */
2350 if (con->delay == 0)
2351 con->delay = BASE_DELAY_INTERVAL;
2352 else if (con->delay < MAX_DELAY_INTERVAL)
2353 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002354 con->ops->get(con);
2355 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002356 round_jiffies_relative(con->delay))) {
2357 dout("fault queued %p delay %lu\n", con, con->delay);
2358 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002359 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002360 dout("fault failed to queue %p delay %lu, backoff\n",
2361 con, con->delay);
2362 /*
2363 * In many cases we see a socket state change
2364 * while con_work is running and end up
2365 * queuing (non-delayed) work, such that we
2366 * can't backoff with a delay. Set a flag so
2367 * that when con_work restarts we schedule the
2368 * delay then.
2369 */
Alex Elder928443c2012-05-22 11:41:43 -05002370 set_bit(BACKOFF, &con->flags);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002371 }
Sage Weil31b80062009-10-06 11:31:13 -07002372 }
2373
Sage Weil91e45ce32010-02-15 12:05:09 -08002374out_unlock:
2375 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002376out:
Sage Weil161fd652010-02-25 12:38:57 -08002377 /*
2378 * in case we faulted due to authentication, invalidate our
2379 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002380 */
Sage Weil161fd652010-02-25 12:38:57 -08002381 if (con->auth_retry && con->ops->invalidate_authorizer) {
2382 dout("calling invalidate_authorizer()\n");
2383 con->ops->invalidate_authorizer(con);
2384 }
2385
Sage Weil31b80062009-10-06 11:31:13 -07002386 if (con->ops->fault)
2387 con->ops->fault(con);
2388}
2389
2390
2391
2392/*
Alex Elder15d98822012-05-26 23:26:43 -05002393 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002394 */
Alex Elder15d98822012-05-26 23:26:43 -05002395void ceph_messenger_init(struct ceph_messenger *msgr,
2396 struct ceph_entity_addr *myaddr,
2397 u32 supported_features,
2398 u32 required_features,
2399 bool nocrc)
Sage Weil31b80062009-10-06 11:31:13 -07002400{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002401 msgr->supported_features = supported_features;
2402 msgr->required_features = required_features;
2403
Sage Weil31b80062009-10-06 11:31:13 -07002404 spin_lock_init(&msgr->global_seq_lock);
2405
Sage Weil31b80062009-10-06 11:31:13 -07002406 if (myaddr)
2407 msgr->inst.addr = *myaddr;
2408
2409 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002410 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002411 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002412 encode_my_addr(msgr);
Alex Elder15d98822012-05-26 23:26:43 -05002413 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002414
Alex Elder15d98822012-05-26 23:26:43 -05002415 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002416}
Alex Elder15d98822012-05-26 23:26:43 -05002417EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002418
Sage Weile00de342011-03-04 12:25:05 -08002419static void clear_standby(struct ceph_connection *con)
2420{
2421 /* come back from STANDBY? */
2422 if (test_and_clear_bit(STANDBY, &con->state)) {
2423 mutex_lock(&con->mutex);
2424 dout("clear_standby %p and ++connect_seq\n", con);
2425 con->connect_seq++;
Alex Elder928443c2012-05-22 11:41:43 -05002426 WARN_ON(test_bit(WRITE_PENDING, &con->flags));
2427 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags));
Sage Weile00de342011-03-04 12:25:05 -08002428 mutex_unlock(&con->mutex);
2429 }
2430}
2431
Sage Weil31b80062009-10-06 11:31:13 -07002432/*
2433 * Queue up an outgoing message on the given connection.
2434 */
2435void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2436{
2437 if (test_bit(CLOSED, &con->state)) {
2438 dout("con_send %p closed, dropping %p\n", con, msg);
2439 ceph_msg_put(msg);
2440 return;
2441 }
2442
2443 /* set src+dst */
Sage Weildbad1852010-03-25 15:45:38 -07002444 msg->hdr.src = con->msgr->inst.name;
Sage Weil31b80062009-10-06 11:31:13 -07002445
Sage Weil3ca02ef2010-03-01 15:25:00 -08002446 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2447
Sage Weile84346b2010-05-11 21:20:38 -07002448 msg->needs_out_seq = true;
2449
Sage Weil31b80062009-10-06 11:31:13 -07002450 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08002451 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002452
Alex Elder38941f82012-06-01 14:56:43 -05002453 BUG_ON(msg->con != NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002454 msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002455 BUG_ON(msg->con == NULL);
2456
Sage Weil31b80062009-10-06 11:31:13 -07002457 BUG_ON(!list_empty(&msg->list_head));
2458 list_add_tail(&msg->list_head, &con->out_queue);
2459 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2460 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2461 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2462 le32_to_cpu(msg->hdr.front_len),
2463 le32_to_cpu(msg->hdr.middle_len),
2464 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002465 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002466
2467 /* if there wasn't anything waiting to send before, queue
2468 * new work */
Sage Weile00de342011-03-04 12:25:05 -08002469 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002470 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002471 queue_con(con);
2472}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002473EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002474
2475/*
2476 * Revoke a message that was previously queued for send
2477 */
Alex Elder6740a842012-06-01 14:56:43 -05002478void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002479{
Alex Elder6740a842012-06-01 14:56:43 -05002480 struct ceph_connection *con = msg->con;
2481
2482 if (!con)
2483 return; /* Message not in our possession */
2484
Sage Weilec302642009-12-22 10:43:42 -08002485 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002486 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05002487 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002488 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -05002489 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002490 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -05002491 msg->con = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05002492 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05002493
Sage Weil31b80062009-10-06 11:31:13 -07002494 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002495 }
2496 if (con->out_msg == msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002497 dout("%s %p msg %p - was sending\n", __func__, con, msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002498 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002499 if (con->out_kvec_is_msg) {
2500 con->out_skip = con->out_kvec_bytes;
2501 con->out_kvec_is_msg = false;
2502 }
Sage Weiled98ada2010-07-05 12:15:14 -07002503 msg->hdr.seq = 0;
Alex Elder92ce0342012-06-04 14:43:33 -05002504
2505 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07002506 }
Sage Weilec302642009-12-22 10:43:42 -08002507 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002508}
2509
2510/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002511 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002512 */
Alex Elder8921d112012-06-01 14:56:43 -05002513void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002514{
Alex Elder8921d112012-06-01 14:56:43 -05002515 struct ceph_connection *con;
2516
2517 BUG_ON(msg == NULL);
2518 if (!msg->con) {
2519 dout("%s msg %p null con\n", __func__, msg);
2520
2521 return; /* Message not in our possession */
2522 }
2523
2524 con = msg->con;
Sage Weil350b1c32009-12-22 10:45:45 -08002525 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05002526 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002527 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
2528 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
2529 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002530
2531 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05002532 dout("%s %p msg %p revoked\n", __func__, con, msg);
2533 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08002534 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002535 front_len -
2536 middle_len -
2537 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002538 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002539 ceph_msg_put(con->in_msg);
2540 con->in_msg = NULL;
2541 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002542 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002543 } else {
Alex Elder8921d112012-06-01 14:56:43 -05002544 dout("%s %p in_msg %p msg %p no-op\n",
2545 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002546 }
2547 mutex_unlock(&con->mutex);
2548}
2549
2550/*
Sage Weil31b80062009-10-06 11:31:13 -07002551 * Queue a keepalive byte to ensure the tcp connection is alive.
2552 */
2553void ceph_con_keepalive(struct ceph_connection *con)
2554{
Sage Weile00de342011-03-04 12:25:05 -08002555 dout("con_keepalive %p\n", con);
2556 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002557 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2558 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002559 queue_con(con);
2560}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002561EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002562
2563
2564/*
2565 * construct a new message with given type, size
2566 * the new msg has a ref count of 1.
2567 */
Sage Weilb61c2762011-08-09 15:03:46 -07002568struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2569 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002570{
2571 struct ceph_msg *m;
2572
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002573 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002574 if (m == NULL)
2575 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002576 kref_init(&m->kref);
Alex Elder38941f82012-06-01 14:56:43 -05002577
2578 m->con = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002579 INIT_LIST_HEAD(&m->list_head);
2580
Sage Weil45c6ceb2010-05-11 15:01:51 -07002581 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002582 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002583 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2584 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002585 m->hdr.front_len = cpu_to_le32(front_len);
2586 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002587 m->hdr.data_len = 0;
2588 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002589 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002590 m->footer.front_crc = 0;
2591 m->footer.middle_crc = 0;
2592 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002593 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002594 m->front_max = front_len;
2595 m->front_is_vmalloc = false;
2596 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002597 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002598 m->pool = NULL;
2599
Henry C Changca208922011-05-03 02:29:56 +00002600 /* middle */
2601 m->middle = NULL;
2602
2603 /* data */
2604 m->nr_pages = 0;
2605 m->page_alignment = 0;
2606 m->pages = NULL;
2607 m->pagelist = NULL;
2608 m->bio = NULL;
2609 m->bio_iter = NULL;
2610 m->bio_seg = 0;
2611 m->trail = NULL;
2612
Sage Weil31b80062009-10-06 11:31:13 -07002613 /* front */
2614 if (front_len) {
2615 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002616 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002617 PAGE_KERNEL);
2618 m->front_is_vmalloc = true;
2619 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002620 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002621 }
2622 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002623 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002624 front_len);
2625 goto out2;
2626 }
2627 } else {
2628 m->front.iov_base = NULL;
2629 }
2630 m->front.iov_len = front_len;
2631
Sage Weilbb257662010-04-01 16:07:23 -07002632 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002633 return m;
2634
2635out2:
2636 ceph_msg_put(m);
2637out:
Sage Weilb61c2762011-08-09 15:03:46 -07002638 if (!can_fail) {
2639 pr_err("msg_new can't create type %d front %d\n", type,
2640 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002641 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002642 } else {
2643 dout("msg_new can't create type %d front %d\n", type,
2644 front_len);
2645 }
Sage Weila79832f2010-04-01 16:06:19 -07002646 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002647}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002648EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002649
2650/*
Sage Weil31b80062009-10-06 11:31:13 -07002651 * Allocate "middle" portion of a message, if it is needed and wasn't
2652 * allocated by alloc_msg. This allows us to read a small fixed-size
2653 * per-type header in the front and then gracefully fail (i.e.,
2654 * propagate the error to the caller based on info in the front) when
2655 * the middle is too large.
2656 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002657static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002658{
2659 int type = le16_to_cpu(msg->hdr.type);
2660 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2661
2662 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2663 ceph_msg_type_name(type), middle_len);
2664 BUG_ON(!middle_len);
2665 BUG_ON(msg->middle);
2666
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002667 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002668 if (!msg->middle)
2669 return -ENOMEM;
2670 return 0;
2671}
2672
Yehuda Sadeh24504182010-01-08 13:58:34 -08002673/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05002674 * Allocate a message for receiving an incoming message on a
2675 * connection, and save the result in con->in_msg. Uses the
2676 * connection's private alloc_msg op if available.
2677 *
2678 * Returns true if the message should be skipped, false otherwise.
2679 * If true is returned (skip message), con->in_msg will be NULL.
2680 * If false is returned, con->in_msg will contain a pointer to the
2681 * newly-allocated message, or NULL in case of memory exhaustion.
Yehuda Sadeh24504182010-01-08 13:58:34 -08002682 */
Alex Elder1c20f2d2012-06-04 14:43:32 -05002683static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2684 struct ceph_msg_header *hdr)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002685{
2686 int type = le16_to_cpu(hdr->type);
2687 int front_len = le32_to_cpu(hdr->front_len);
2688 int middle_len = le32_to_cpu(hdr->middle_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002689 int ret;
2690
Alex Elder1c20f2d2012-06-04 14:43:32 -05002691 BUG_ON(con->in_msg != NULL);
2692
Yehuda Sadeh24504182010-01-08 13:58:34 -08002693 if (con->ops->alloc_msg) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002694 int skip = 0;
2695
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002696 mutex_unlock(&con->mutex);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002697 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002698 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002699 if (con->in_msg) {
Sage Weil36eb71a2012-06-21 12:47:08 -07002700 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002701 BUG_ON(con->in_msg->con == NULL);
2702 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002703 if (skip)
2704 con->in_msg = NULL;
2705
2706 if (!con->in_msg)
2707 return skip != 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002708 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002709 if (!con->in_msg) {
2710 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2711 if (!con->in_msg) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002712 pr_err("unable to allocate msg type %d len %d\n",
2713 type, front_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002714 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002715 }
Sage Weil36eb71a2012-06-21 12:47:08 -07002716 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002717 BUG_ON(con->in_msg->con == NULL);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002718 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002719 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002720 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002721
Alex Elder1c20f2d2012-06-04 14:43:32 -05002722 if (middle_len && !con->in_msg->middle) {
2723 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002724 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002725 ceph_msg_put(con->in_msg);
2726 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002727 }
2728 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002729
Alex Elder1c20f2d2012-06-04 14:43:32 -05002730 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002731}
2732
Sage Weil31b80062009-10-06 11:31:13 -07002733
2734/*
2735 * Free a generically kmalloc'd message.
2736 */
2737void ceph_msg_kfree(struct ceph_msg *m)
2738{
2739 dout("msg_kfree %p\n", m);
2740 if (m->front_is_vmalloc)
2741 vfree(m->front.iov_base);
2742 else
2743 kfree(m->front.iov_base);
2744 kfree(m);
2745}
2746
2747/*
2748 * Drop a msg ref. Destroy as needed.
2749 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002750void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002751{
Sage Weilc2e552e2009-12-07 15:55:05 -08002752 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002753
Sage Weilc2e552e2009-12-07 15:55:05 -08002754 dout("ceph_msg_put last one on %p\n", m);
2755 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002756
Sage Weilc2e552e2009-12-07 15:55:05 -08002757 /* drop middle, data, if any */
2758 if (m->middle) {
2759 ceph_buffer_put(m->middle);
2760 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002761 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002762 m->nr_pages = 0;
2763 m->pages = NULL;
2764
Sage Weil58bb3b32009-12-23 12:12:31 -08002765 if (m->pagelist) {
2766 ceph_pagelist_release(m->pagelist);
2767 kfree(m->pagelist);
2768 m->pagelist = NULL;
2769 }
2770
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002771 m->trail = NULL;
2772
Sage Weilc2e552e2009-12-07 15:55:05 -08002773 if (m->pool)
2774 ceph_msgpool_put(m->pool, m);
2775 else
2776 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002777}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002778EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002779
2780void ceph_msg_dump(struct ceph_msg *msg)
2781{
2782 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2783 msg->front_max, msg->nr_pages);
2784 print_hex_dump(KERN_DEBUG, "header: ",
2785 DUMP_PREFIX_OFFSET, 16, 1,
2786 &msg->hdr, sizeof(msg->hdr), true);
2787 print_hex_dump(KERN_DEBUG, " front: ",
2788 DUMP_PREFIX_OFFSET, 16, 1,
2789 msg->front.iov_base, msg->front.iov_len, true);
2790 if (msg->middle)
2791 print_hex_dump(KERN_DEBUG, "middle: ",
2792 DUMP_PREFIX_OFFSET, 16, 1,
2793 msg->middle->vec.iov_base,
2794 msg->middle->vec.iov_len, true);
2795 print_hex_dump(KERN_DEBUG, "footer: ",
2796 DUMP_PREFIX_OFFSET, 16, 1,
2797 &msg->footer, sizeof(msg->footer), true);
2798}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002799EXPORT_SYMBOL(ceph_msg_dump);