blob: 563e46aa4d6d5c3d87688439f263e2d484b8f0c7 [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() \
Sage Weilfbb85a42012-06-27 12:31:02 -070051 * |+--------------------------- \
52 * | \ \ \
53 * | ----------- \ \
54 * | | CLOSING | socket event; \ \
55 * | ----------- await close \ \
56 * | ^ \ |
57 * | | \ |
58 * | + con_sock_state_closing() \ |
59 * | / \ | |
60 * | / --------------- | |
61 * | / \ v v
Alex Elderbc18f4b2012-06-20 21:53:53 -050062 * | / --------------
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 Weil8dacc7d2012-07-20 17:24:40 -070080/*
81 * connection states
82 */
83#define CON_STATE_CLOSED 1 /* -> PREOPEN */
84#define CON_STATE_PREOPEN 2 /* -> CONNECTING, CLOSED */
85#define CON_STATE_CONNECTING 3 /* -> NEGOTIATING, CLOSED */
86#define CON_STATE_NEGOTIATING 4 /* -> OPEN, CLOSED */
87#define CON_STATE_OPEN 5 /* -> STANDBY, CLOSED */
88#define CON_STATE_STANDBY 6 /* -> PREOPEN, CLOSED */
89
90
Sage Weil31b80062009-10-06 11:31:13 -070091/* static tag bytes (protocol control messages) */
92static char tag_msg = CEPH_MSGR_TAG_MSG;
93static char tag_ack = CEPH_MSGR_TAG_ACK;
94static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
95
Sage Weila6a53492010-04-13 14:07:07 -070096#ifdef CONFIG_LOCKDEP
97static struct lock_class_key socket_class;
98#endif
99
Alex Elder84495f42012-02-15 07:43:55 -0600100/*
101 * When skipping (ignoring) a block of input we read it into a "skip
102 * buffer," which is this many bytes in size.
103 */
104#define SKIP_BUF_SIZE 1024
Sage Weil31b80062009-10-06 11:31:13 -0700105
106static void queue_con(struct ceph_connection *con);
107static void con_work(struct work_struct *);
108static void ceph_fault(struct ceph_connection *con);
109
Sage Weil31b80062009-10-06 11:31:13 -0700110/*
Alex Elderf64a9312012-01-23 15:49:27 -0600111 * Nicely render a sockaddr as a string. An array of formatted
112 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -0700113 */
Alex Elderf64a9312012-01-23 15:49:27 -0600114#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
115#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
116#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
117#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
118
119static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
120static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -0700121
Alex Elder57666512012-01-23 15:49:27 -0600122static struct page *zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -0600123
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700124const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -0700125{
126 int i;
127 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -0600128 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
129 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -0700130
Alex Elderf64a9312012-01-23 15:49:27 -0600131 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -0700132 s = addr_str[i];
133
134 switch (ss->ss_family) {
135 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -0600136 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
137 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -0700138 break;
139
140 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -0600141 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
142 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -0700143 break;
144
145 default:
Alex Elderd3002b92012-02-14 14:05:33 -0600146 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
147 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -0700148 }
149
150 return s;
151}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700152EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700153
Sage Weil63f2d212009-11-03 15:17:56 -0800154static void encode_my_addr(struct ceph_messenger *msgr)
155{
156 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
157 ceph_encode_addr(&msgr->my_enc_addr);
158}
159
Sage Weil31b80062009-10-06 11:31:13 -0700160/*
161 * work queue for all reading and writing to/from the socket.
162 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600163static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700164
Alex Elder6173d1f2012-02-14 14:05:33 -0600165void _ceph_msgr_exit(void)
166{
Alex Elderd3002b92012-02-14 14:05:33 -0600167 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600168 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600169 ceph_msgr_wq = NULL;
170 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600171
Alex Elder6173d1f2012-02-14 14:05:33 -0600172 BUG_ON(zero_page == NULL);
173 kunmap(zero_page);
174 page_cache_release(zero_page);
175 zero_page = NULL;
176}
177
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700178int ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700179{
Alex Elder57666512012-01-23 15:49:27 -0600180 BUG_ON(zero_page != NULL);
181 zero_page = ZERO_PAGE(0);
182 page_cache_get(zero_page);
183
Tejun Heof363e452011-01-03 14:49:46 +0100184 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600185 if (ceph_msgr_wq)
186 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600187
Alex Elder6173d1f2012-02-14 14:05:33 -0600188 pr_err("msgr_init failed to create workqueue\n");
189 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600190
Alex Elder6173d1f2012-02-14 14:05:33 -0600191 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700192}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700193EXPORT_SYMBOL(ceph_msgr_init);
Sage Weil31b80062009-10-06 11:31:13 -0700194
195void ceph_msgr_exit(void)
196{
Alex Elder57666512012-01-23 15:49:27 -0600197 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600198
Alex Elder6173d1f2012-02-14 14:05:33 -0600199 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700200}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700201EXPORT_SYMBOL(ceph_msgr_exit);
Sage Weil31b80062009-10-06 11:31:13 -0700202
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700203void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700204{
205 flush_workqueue(ceph_msgr_wq);
206}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700207EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700208
Alex Elderce2c8902012-05-22 22:15:49 -0500209/* Connection socket state transition functions */
210
211static void con_sock_state_init(struct ceph_connection *con)
212{
213 int old_state;
214
215 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
216 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
217 printk("%s: unexpected old state %d\n", __func__, old_state);
218}
219
220static void con_sock_state_connecting(struct ceph_connection *con)
221{
222 int old_state;
223
224 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
225 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
226 printk("%s: unexpected old state %d\n", __func__, old_state);
227}
228
229static void con_sock_state_connected(struct ceph_connection *con)
230{
231 int old_state;
232
233 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
234 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
235 printk("%s: unexpected old state %d\n", __func__, old_state);
236}
237
238static void con_sock_state_closing(struct ceph_connection *con)
239{
240 int old_state;
241
242 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
243 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
244 old_state != CON_SOCK_STATE_CONNECTED &&
245 old_state != CON_SOCK_STATE_CLOSING))
246 printk("%s: unexpected old state %d\n", __func__, old_state);
247}
248
249static void con_sock_state_closed(struct ceph_connection *con)
250{
251 int old_state;
252
253 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
254 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
Sage Weilfbb85a42012-06-27 12:31:02 -0700255 old_state != CON_SOCK_STATE_CLOSING &&
256 old_state != CON_SOCK_STATE_CONNECTING))
Alex Elderce2c8902012-05-22 22:15:49 -0500257 printk("%s: unexpected old state %d\n", __func__, old_state);
258}
Sage Weila922d382010-05-29 09:41:23 -0700259
Sage Weil31b80062009-10-06 11:31:13 -0700260/*
261 * socket callback functions
262 */
263
264/* data available on socket, or listen socket received a connect */
Alex Elder327800b2012-05-22 11:41:43 -0500265static void ceph_sock_data_ready(struct sock *sk, int count_unused)
Sage Weil31b80062009-10-06 11:31:13 -0700266{
Alex Elderbd406142012-01-23 15:49:27 -0600267 struct ceph_connection *con = sk->sk_user_data;
Guanjun Hea2a32582012-07-08 19:50:33 -0700268 if (atomic_read(&con->msgr->stopping)) {
269 return;
270 }
Alex Elderbd406142012-01-23 15:49:27 -0600271
Sage Weil31b80062009-10-06 11:31:13 -0700272 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500273 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700274 con, con->state);
275 queue_con(con);
276 }
277}
278
279/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500280static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700281{
Alex Elderd3002b92012-02-14 14:05:33 -0600282 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700283
Jim Schutt182fac22012-02-29 08:30:58 -0700284 /* only queue to workqueue if there is data we want to write,
285 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500286 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700287 * doesn't get called again until try_write() fills the socket
288 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
289 * and net/core/stream.c:sk_stream_write_space().
290 */
Alex Elder928443c2012-05-22 11:41:43 -0500291 if (test_bit(WRITE_PENDING, &con->flags)) {
Jim Schutt182fac22012-02-29 08:30:58 -0700292 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500293 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700294 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
295 queue_con(con);
296 }
Sage Weil31b80062009-10-06 11:31:13 -0700297 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500298 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700299 }
Sage Weil31b80062009-10-06 11:31:13 -0700300}
301
302/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500303static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700304{
Alex Elderbd406142012-01-23 15:49:27 -0600305 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700306
Alex Elder327800b2012-05-22 11:41:43 -0500307 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700308 con, con->state, sk->sk_state);
309
Sage Weil31b80062009-10-06 11:31:13 -0700310 switch (sk->sk_state) {
311 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500312 dout("%s TCP_CLOSE\n", __func__);
Sage Weil31b80062009-10-06 11:31:13 -0700313 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500314 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500315 con_sock_state_closing(con);
Alex Elderd65c9e02012-06-20 21:53:53 -0500316 set_bit(SOCK_CLOSED, &con->flags);
317 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -0700318 break;
319 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500320 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500321 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700322 queue_con(con);
323 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600324 default: /* Everything else is uninteresting */
325 break;
Sage Weil31b80062009-10-06 11:31:13 -0700326 }
327}
328
329/*
330 * set up socket callbacks
331 */
332static void set_sock_callbacks(struct socket *sock,
333 struct ceph_connection *con)
334{
335 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600336 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500337 sk->sk_data_ready = ceph_sock_data_ready;
338 sk->sk_write_space = ceph_sock_write_space;
339 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700340}
341
342
343/*
344 * socket helpers
345 */
346
347/*
348 * initiate connection to a remote socket.
349 */
Alex Elder41617d02012-02-14 14:05:33 -0600350static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700351{
Sage Weilf91d3472010-07-01 15:18:31 -0700352 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700353 struct socket *sock;
354 int ret;
355
356 BUG_ON(con->sock);
Sage Weilf91d3472010-07-01 15:18:31 -0700357 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
358 IPPROTO_TCP, &sock);
Sage Weil31b80062009-10-06 11:31:13 -0700359 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600360 return ret;
Sage Weil31b80062009-10-06 11:31:13 -0700361 sock->sk->sk_allocation = GFP_NOFS;
362
Sage Weila6a53492010-04-13 14:07:07 -0700363#ifdef CONFIG_LOCKDEP
364 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
365#endif
366
Sage Weil31b80062009-10-06 11:31:13 -0700367 set_sock_callbacks(sock, con);
368
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700369 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700370
Sage Weil89a86be2012-06-09 14:19:21 -0700371 con_sock_state_connecting(con);
Sage Weilf91d3472010-07-01 15:18:31 -0700372 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
373 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700374 if (ret == -EINPROGRESS) {
375 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700376 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700377 sock->sk->sk_state);
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600378 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700379 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700380 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700381 sock_release(sock);
Sage Weil31b80062009-10-06 11:31:13 -0700382 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -0700383
Alex Elder41617d02012-02-14 14:05:33 -0600384 return ret;
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600385 }
386 con->sock = sock;
Alex Elder41617d02012-02-14 14:05:33 -0600387 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700388}
389
390static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
391{
392 struct kvec iov = {buf, len};
393 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800394 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700395
Sage Weil98bdb0a2011-01-25 08:17:48 -0800396 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
397 if (r == -EAGAIN)
398 r = 0;
399 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700400}
401
402/*
403 * write something. @more is true if caller will be sending more data
404 * shortly.
405 */
406static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
407 size_t kvlen, size_t len, int more)
408{
409 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800410 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700411
412 if (more)
413 msg.msg_flags |= MSG_MORE;
414 else
415 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
416
Sage Weil42961d22011-01-25 08:19:34 -0800417 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
418 if (r == -EAGAIN)
419 r = 0;
420 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700421}
422
Alex Elder31739132012-03-07 11:40:08 -0600423static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
424 int offset, size_t size, int more)
425{
426 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
427 int ret;
428
429 ret = kernel_sendpage(sock, page, offset, size, flags);
430 if (ret == -EAGAIN)
431 ret = 0;
432
433 return ret;
434}
435
Sage Weil31b80062009-10-06 11:31:13 -0700436
437/*
438 * Shutdown/close the socket for the given connection.
439 */
440static int con_close_socket(struct ceph_connection *con)
441{
442 int rc;
443
444 dout("con_close_socket on %p sock %p\n", con, con->sock);
445 if (!con->sock)
446 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700447 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
448 sock_release(con->sock);
449 con->sock = NULL;
Alex Elder456ea462012-06-20 21:53:53 -0500450
451 /*
452 * Forcibly clear the SOCK_CLOSE flag. It gets set
453 * independent of the connection mutex, and we could have
454 * received a socket close event before we had the chance to
455 * shut the socket down.
456 */
Alex Eldera8d00e32012-06-20 21:53:53 -0500457 clear_bit(SOCK_CLOSED, &con->flags);
Alex Elderce2c8902012-05-22 22:15:49 -0500458 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700459 return rc;
460}
461
462/*
463 * Reset a connection. Discard all incoming and outgoing messages
464 * and clear *_seq state.
465 */
466static void ceph_msg_remove(struct ceph_msg *msg)
467{
468 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500469 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -0700470 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -0500471 msg->con = NULL;
472
Sage Weil31b80062009-10-06 11:31:13 -0700473 ceph_msg_put(msg);
474}
475static void ceph_msg_remove_list(struct list_head *head)
476{
477 while (!list_empty(head)) {
478 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
479 list_head);
480 ceph_msg_remove(msg);
481 }
482}
483
484static void reset_connection(struct ceph_connection *con)
485{
486 /* reset connection, out_queue, msg_ and connect_seq */
487 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700488 ceph_msg_remove_list(&con->out_queue);
489 ceph_msg_remove_list(&con->out_sent);
490
Sage Weilcf3e5c42009-12-11 09:48:05 -0800491 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500492 BUG_ON(con->in_msg->con != con);
493 con->in_msg->con = NULL;
Sage Weilcf3e5c42009-12-11 09:48:05 -0800494 ceph_msg_put(con->in_msg);
495 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -0700496 con->ops->put(con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800497 }
498
Sage Weil31b80062009-10-06 11:31:13 -0700499 con->connect_seq = 0;
500 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800501 if (con->out_msg) {
502 ceph_msg_put(con->out_msg);
503 con->out_msg = NULL;
504 }
Sage Weil31b80062009-10-06 11:31:13 -0700505 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700506 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700507}
508
509/*
510 * mark a peer down. drop any open connections.
511 */
512void ceph_con_close(struct ceph_connection *con)
513{
Sage Weil8c50c812012-07-30 16:24:37 -0700514 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700515 dout("con_close %p peer %s\n", con,
516 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700517 con->state = CON_STATE_CLOSED;
Alex Eldera5988c42012-05-29 11:04:58 -0500518
Alex Elder928443c2012-05-22 11:41:43 -0500519 clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */
520 clear_bit(KEEPALIVE_PENDING, &con->flags);
521 clear_bit(WRITE_PENDING, &con->flags);
Alex Eldera5988c42012-05-29 11:04:58 -0500522
Sage Weil31b80062009-10-06 11:31:13 -0700523 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700524 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800525 cancel_delayed_work(&con->work);
Sage Weilee76e072012-07-20 16:45:49 -0700526 con_close_socket(con);
Sage Weilec302642009-12-22 10:43:42 -0800527 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700528}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700529EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700530
531/*
Sage Weil31b80062009-10-06 11:31:13 -0700532 * Reopen a closed connection, with a new peer address.
533 */
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700534void ceph_con_open(struct ceph_connection *con,
535 __u8 entity_type, __u64 entity_num,
536 struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -0700537{
Sage Weil54691552012-07-30 16:21:40 -0700538 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700539 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700540
541 BUG_ON(con->state != CON_STATE_CLOSED);
542 con->state = CON_STATE_PREOPEN;
Alex Eldera5988c42012-05-29 11:04:58 -0500543
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700544 con->peer_name.type = (__u8) entity_type;
545 con->peer_name.num = cpu_to_le64(entity_num);
546
Sage Weil31b80062009-10-06 11:31:13 -0700547 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800548 con->delay = 0; /* reset backoff memory */
Sage Weil54691552012-07-30 16:21:40 -0700549 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700550 queue_con(con);
551}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700552EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700553
554/*
Sage Weil87b315a2010-03-22 14:51:18 -0700555 * return true if this connection ever successfully opened
556 */
557bool ceph_con_opened(struct ceph_connection *con)
558{
559 return con->connect_seq > 0;
560}
561
562/*
Sage Weil31b80062009-10-06 11:31:13 -0700563 * initialize a new connection.
564 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500565void ceph_con_init(struct ceph_connection *con, void *private,
566 const struct ceph_connection_operations *ops,
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700567 struct ceph_messenger *msgr)
Sage Weil31b80062009-10-06 11:31:13 -0700568{
569 dout("con_init %p\n", con);
570 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500571 con->private = private;
572 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700573 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500574
575 con_sock_state_init(con);
576
Sage Weilec302642009-12-22 10:43:42 -0800577 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700578 INIT_LIST_HEAD(&con->out_queue);
579 INIT_LIST_HEAD(&con->out_sent);
580 INIT_DELAYED_WORK(&con->work, con_work);
Alex Eldera5988c42012-05-29 11:04:58 -0500581
Sage Weil8dacc7d2012-07-20 17:24:40 -0700582 con->state = CON_STATE_CLOSED;
Sage Weil31b80062009-10-06 11:31:13 -0700583}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700584EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700585
586
587/*
588 * We maintain a global counter to order connection attempts. Get
589 * a unique seq greater than @gt.
590 */
591static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
592{
593 u32 ret;
594
595 spin_lock(&msgr->global_seq_lock);
596 if (msgr->global_seq < gt)
597 msgr->global_seq = gt;
598 ret = ++msgr->global_seq;
599 spin_unlock(&msgr->global_seq_lock);
600 return ret;
601}
602
Alex Eldere2200422012-05-23 14:35:23 -0500603static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600604{
605 con->out_kvec_left = 0;
606 con->out_kvec_bytes = 0;
607 con->out_kvec_cur = &con->out_kvec[0];
608}
609
Alex Eldere2200422012-05-23 14:35:23 -0500610static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600611 size_t size, void *data)
612{
613 int index;
614
615 index = con->out_kvec_left;
616 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
617
618 con->out_kvec[index].iov_len = size;
619 con->out_kvec[index].iov_base = data;
620 con->out_kvec_left++;
621 con->out_kvec_bytes += size;
622}
Sage Weil31b80062009-10-06 11:31:13 -0700623
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500624#ifdef CONFIG_BLOCK
625static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
626{
627 if (!bio) {
628 *iter = NULL;
629 *seg = 0;
630 return;
631 }
632 *iter = bio;
633 *seg = bio->bi_idx;
634}
635
636static void iter_bio_next(struct bio **bio_iter, int *seg)
637{
638 if (*bio_iter == NULL)
639 return;
640
641 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
642
643 (*seg)++;
644 if (*seg == (*bio_iter)->bi_vcnt)
645 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
646}
647#endif
648
Alex Elder739c9052012-06-11 14:57:13 -0500649static void prepare_write_message_data(struct ceph_connection *con)
650{
651 struct ceph_msg *msg = con->out_msg;
652
653 BUG_ON(!msg);
654 BUG_ON(!msg->hdr.data_len);
655
656 /* initialize page iterator */
657 con->out_msg_pos.page = 0;
658 if (msg->pages)
659 con->out_msg_pos.page_pos = msg->page_alignment;
660 else
661 con->out_msg_pos.page_pos = 0;
Alex Elder572c5882012-06-11 14:57:13 -0500662#ifdef CONFIG_BLOCK
Alex Elderabdaa6a2012-06-11 14:57:13 -0500663 if (msg->bio)
Alex Elder572c5882012-06-11 14:57:13 -0500664 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
665#endif
Alex Elder739c9052012-06-11 14:57:13 -0500666 con->out_msg_pos.data_pos = 0;
667 con->out_msg_pos.did_page_crc = false;
668 con->out_more = 1; /* data + footer will follow */
669}
670
Sage Weil31b80062009-10-06 11:31:13 -0700671/*
672 * Prepare footer for currently outgoing message, and finish things
673 * off. Assumes out_kvec* are already valid.. we just add on to the end.
674 */
Alex Elder859eb792012-02-14 14:05:33 -0600675static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700676{
677 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600678 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700679
Alex Elderfd154f32012-06-11 14:57:13 -0500680 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
681
Sage Weil31b80062009-10-06 11:31:13 -0700682 dout("prepare_write_message_footer %p\n", con);
683 con->out_kvec_is_msg = true;
684 con->out_kvec[v].iov_base = &m->footer;
685 con->out_kvec[v].iov_len = sizeof(m->footer);
686 con->out_kvec_bytes += sizeof(m->footer);
687 con->out_kvec_left++;
688 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800689 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700690}
691
692/*
693 * Prepare headers for the next outgoing message.
694 */
695static void prepare_write_message(struct ceph_connection *con)
696{
697 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600698 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700699
Alex Eldere2200422012-05-23 14:35:23 -0500700 con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700701 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800702 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700703
704 /* Sneak an ack in there first? If we can get it into the same
705 * TCP packet that's a good thing. */
706 if (con->in_seq > con->in_seq_acked) {
707 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -0500708 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700709 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500710 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600711 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700712 }
713
Alex Elder38941f82012-06-01 14:56:43 -0500714 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -0600715 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800716 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -0500717 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -0700718
719 /* put message on sent list */
720 ceph_msg_get(m);
721 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700722
Sage Weile84346b2010-05-11 21:20:38 -0700723 /*
724 * only assign outgoing seq # if we haven't sent this message
725 * yet. if it is requeued, resend with it's original seq.
726 */
727 if (m->needs_out_seq) {
728 m->hdr.seq = cpu_to_le64(++con->out_seq);
729 m->needs_out_seq = false;
730 }
Sage Weil31b80062009-10-06 11:31:13 -0700731
732 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
733 m, con->out_seq, le16_to_cpu(m->hdr.type),
734 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
735 le32_to_cpu(m->hdr.data_len),
736 m->nr_pages);
737 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
738
739 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -0500740 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
741 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
742 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600743
Sage Weil31b80062009-10-06 11:31:13 -0700744 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -0500745 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600746 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700747
748 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600749 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
750 con->out_msg->hdr.crc = cpu_to_le32(crc);
Alex Elderfd154f32012-06-11 14:57:13 -0500751 con->out_msg->footer.flags = 0;
Alex Eldera9a0c512012-02-15 07:43:54 -0600752
753 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
754 con->out_msg->footer.front_crc = cpu_to_le32(crc);
755 if (m->middle) {
756 crc = crc32c(0, m->middle->vec.iov_base,
757 m->middle->vec.iov_len);
758 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
759 } else
Sage Weil31b80062009-10-06 11:31:13 -0700760 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -0500761 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700762 le32_to_cpu(con->out_msg->footer.front_crc),
763 le32_to_cpu(con->out_msg->footer.middle_crc));
764
765 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -0500766 con->out_msg->footer.data_crc = 0;
767 if (m->hdr.data_len)
768 prepare_write_message_data(con);
769 else
Sage Weil31b80062009-10-06 11:31:13 -0700770 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600771 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700772
Alex Elder928443c2012-05-22 11:41:43 -0500773 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700774}
775
776/*
777 * Prepare an ack.
778 */
779static void prepare_write_ack(struct ceph_connection *con)
780{
781 dout("prepare_write_ack %p %llu -> %llu\n", con,
782 con->in_seq_acked, con->in_seq);
783 con->in_seq_acked = con->in_seq;
784
Alex Eldere2200422012-05-23 14:35:23 -0500785 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600786
Alex Eldere2200422012-05-23 14:35:23 -0500787 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600788
Sage Weil31b80062009-10-06 11:31:13 -0700789 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500790 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600791 &con->out_temp_ack);
792
Sage Weil31b80062009-10-06 11:31:13 -0700793 con->out_more = 1; /* more will follow.. eventually.. */
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 * Prepare to write keepalive byte.
799 */
800static void prepare_write_keepalive(struct ceph_connection *con)
801{
802 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -0500803 con_out_kvec_reset(con);
804 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Alex Elder928443c2012-05-22 11:41:43 -0500805 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700806}
807
808/*
809 * Connection negotiation.
810 */
811
Alex Elderdac1e712012-05-16 15:16:39 -0500812static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
813 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800814{
Alex Eldera3530df2012-05-16 15:16:39 -0500815 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -0500816
817 if (!con->ops->get_authorizer) {
818 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
819 con->out_connect.authorizer_len = 0;
Alex Elder729796b2012-05-16 15:16:39 -0500820 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -0500821 }
822
823 /* Can't hold the mutex while getting authorizer */
Sage Weilec302642009-12-22 10:43:42 -0800824 mutex_unlock(&con->mutex);
Alex Elderdac1e712012-05-16 15:16:39 -0500825 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Sage Weilec302642009-12-22 10:43:42 -0800826 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800827
Alex Eldera3530df2012-05-16 15:16:39 -0500828 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -0500829 return auth;
Sage Weil8dacc7d2012-07-20 17:24:40 -0700830 if (con->state != CON_STATE_NEGOTIATING)
Alex Elder729796b2012-05-16 15:16:39 -0500831 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700832
Alex Elder8f43fb52012-05-16 15:16:39 -0500833 con->auth_reply_buf = auth->authorizer_reply_buf;
834 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
Alex Elder729796b2012-05-16 15:16:39 -0500835 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800836}
837
Sage Weil31b80062009-10-06 11:31:13 -0700838/*
839 * We connected to a peer and are saying hello.
840 */
Alex Eldere825a662012-05-16 15:16:38 -0500841static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700842{
Alex Eldere2200422012-05-23 14:35:23 -0500843 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
844 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -0500845 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800846
Sage Weileed0ef22009-11-10 14:34:36 -0800847 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500848 set_bit(WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800849}
850
Alex Eldere825a662012-05-16 15:16:38 -0500851static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800852{
Eric Dumazet95c96172012-04-15 05:58:06 +0000853 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -0700854 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -0500855 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -0500856 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700857
858 switch (con->peer_name.type) {
859 case CEPH_ENTITY_TYPE_MON:
860 proto = CEPH_MONC_PROTOCOL;
861 break;
862 case CEPH_ENTITY_TYPE_OSD:
863 proto = CEPH_OSDC_PROTOCOL;
864 break;
865 case CEPH_ENTITY_TYPE_MDS:
866 proto = CEPH_MDSC_PROTOCOL;
867 break;
868 default:
869 BUG();
870 }
871
872 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
873 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800874
Alex Eldere825a662012-05-16 15:16:38 -0500875 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700876 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
877 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
878 con->out_connect.global_seq = cpu_to_le32(global_seq);
879 con->out_connect.protocol_version = cpu_to_le32(proto);
880 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700881
Alex Elderdac1e712012-05-16 15:16:39 -0500882 auth_proto = CEPH_AUTH_UNKNOWN;
883 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -0500884 if (IS_ERR(auth))
885 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -0500886
Alex Elderdac1e712012-05-16 15:16:39 -0500887 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -0500888 con->out_connect.authorizer_len = auth ?
889 cpu_to_le32(auth->authorizer_buf_len) : 0;
890
Alex Elderab166d52012-05-31 11:37:29 -0500891 con_out_kvec_reset(con);
Alex Eldere2200422012-05-23 14:35:23 -0500892 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -0500893 &con->out_connect);
894 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -0500895 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -0500896 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600897
Sage Weil31b80062009-10-06 11:31:13 -0700898 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500899 set_bit(WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800900
Alex Eldere10c7582012-05-16 15:16:38 -0500901 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700902}
903
Sage Weil31b80062009-10-06 11:31:13 -0700904/*
905 * write as much of pending kvecs to the socket as we can.
906 * 1 -> done
907 * 0 -> socket full, but more to do
908 * <0 -> error
909 */
910static int write_partial_kvec(struct ceph_connection *con)
911{
912 int ret;
913
914 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
915 while (con->out_kvec_bytes > 0) {
916 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
917 con->out_kvec_left, con->out_kvec_bytes,
918 con->out_more);
919 if (ret <= 0)
920 goto out;
921 con->out_kvec_bytes -= ret;
922 if (con->out_kvec_bytes == 0)
923 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600924
925 /* account for full iov entries consumed */
926 while (ret >= con->out_kvec_cur->iov_len) {
927 BUG_ON(!con->out_kvec_left);
928 ret -= con->out_kvec_cur->iov_len;
929 con->out_kvec_cur++;
930 con->out_kvec_left--;
931 }
932 /* and for a partially-consumed entry */
933 if (ret) {
934 con->out_kvec_cur->iov_len -= ret;
935 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700936 }
937 }
938 con->out_kvec_left = 0;
939 con->out_kvec_is_msg = false;
940 ret = 1;
941out:
942 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
943 con->out_kvec_bytes, con->out_kvec_left, ret);
944 return ret; /* done! */
945}
946
Alex Elder84ca8fc2012-06-11 14:57:13 -0500947static void out_msg_pos_next(struct ceph_connection *con, struct page *page,
948 size_t len, size_t sent, bool in_trail)
949{
950 struct ceph_msg *msg = con->out_msg;
951
952 BUG_ON(!msg);
953 BUG_ON(!sent);
954
955 con->out_msg_pos.data_pos += sent;
956 con->out_msg_pos.page_pos += sent;
Alex Elder5821bd82012-06-11 14:57:13 -0500957 if (sent < len)
958 return;
959
960 BUG_ON(sent != len);
961 con->out_msg_pos.page_pos = 0;
962 con->out_msg_pos.page++;
963 con->out_msg_pos.did_page_crc = false;
964 if (in_trail)
965 list_move_tail(&page->lru,
966 &msg->trail->head);
967 else if (msg->pagelist)
968 list_move_tail(&page->lru,
969 &msg->pagelist->head);
Alex Elder84ca8fc2012-06-11 14:57:13 -0500970#ifdef CONFIG_BLOCK
Alex Elder5821bd82012-06-11 14:57:13 -0500971 else if (msg->bio)
972 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
Alex Elder84ca8fc2012-06-11 14:57:13 -0500973#endif
Alex Elder84ca8fc2012-06-11 14:57:13 -0500974}
975
Sage Weil31b80062009-10-06 11:31:13 -0700976/*
977 * Write as much message data payload as we can. If we finish, queue
978 * up the footer.
979 * 1 -> done, footer is now queued in out_kvec[].
980 * 0 -> socket full, but more to do
981 * <0 -> error
982 */
983static int write_partial_msg_pages(struct ceph_connection *con)
984{
985 struct ceph_msg *msg = con->out_msg;
Eric Dumazet95c96172012-04-15 05:58:06 +0000986 unsigned int data_len = le32_to_cpu(msg->hdr.data_len);
Sage Weil31b80062009-10-06 11:31:13 -0700987 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600988 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700989 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700990 int total_max_write;
Alex Elder84ca8fc2012-06-11 14:57:13 -0500991 bool in_trail = false;
Alex Elder5821bd82012-06-11 14:57:13 -0500992 const size_t trail_len = (msg->trail ? msg->trail->length : 0);
993 const size_t trail_off = data_len - trail_len;
Sage Weil31b80062009-10-06 11:31:13 -0700994
995 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
Alex Elder84ca8fc2012-06-11 14:57:13 -0500996 con, msg, con->out_msg_pos.page, msg->nr_pages,
Sage Weil31b80062009-10-06 11:31:13 -0700997 con->out_msg_pos.page_pos);
998
Alex Elder5821bd82012-06-11 14:57:13 -0500999 /*
1000 * Iterate through each page that contains data to be
1001 * written, and send as much as possible for each.
1002 *
1003 * If we are calculating the data crc (the default), we will
1004 * need to map the page. If we have no pages, they have
1005 * been revoked, so use the zero page.
1006 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001007 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -07001008 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001009 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -06001010 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001011
Alex Elder5821bd82012-06-11 14:57:13 -05001012 in_trail = in_trail || con->out_msg_pos.data_pos >= trail_off;
1013 if (!in_trail)
1014 total_max_write = trail_off - con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001015
Alex Elder5821bd82012-06-11 14:57:13 -05001016 if (in_trail) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001017 total_max_write = data_len - con->out_msg_pos.data_pos;
1018
1019 page = list_first_entry(&msg->trail->head,
1020 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001021 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -07001022 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -08001023 } else if (msg->pagelist) {
1024 page = list_first_entry(&msg->pagelist->head,
1025 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001026#ifdef CONFIG_BLOCK
1027 } else if (msg->bio) {
1028 struct bio_vec *bv;
1029
1030 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
1031 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -06001032 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001033 max_write = bv->bv_len;
1034#endif
Sage Weil31b80062009-10-06 11:31:13 -07001035 } else {
Alex Elder57666512012-01-23 15:49:27 -06001036 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -07001037 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001038 len = min_t(int, max_write - con->out_msg_pos.page_pos,
1039 total_max_write);
1040
Alex Elder37675b02012-03-07 11:40:08 -06001041 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -06001042 void *base;
Alex Elder5821bd82012-06-11 14:57:13 -05001043 u32 crc = le32_to_cpu(msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -06001044 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -07001045
Alex Elder8d63e312012-03-07 11:40:08 -06001046 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -07001047 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -06001048 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Elder5821bd82012-06-11 14:57:13 -05001049 crc = crc32c(crc, base, len);
Alex Elder84ca8fc2012-06-11 14:57:13 -05001050 msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -06001051 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -07001052 }
Alex Eldere36b13c2012-03-07 11:40:08 -06001053 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -06001054 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -06001055 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001056
Alex Elder0cdf9e62012-03-07 11:40:08 -06001057 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -07001058 kunmap(page);
1059
1060 if (ret <= 0)
1061 goto out;
1062
Alex Elder84ca8fc2012-06-11 14:57:13 -05001063 out_msg_pos_next(con, page, len, (size_t) ret, in_trail);
Sage Weil31b80062009-10-06 11:31:13 -07001064 }
1065
1066 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1067
1068 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001069 if (!do_datacrc)
Alex Elder84ca8fc2012-06-11 14:57:13 -05001070 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001071 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001072 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001073 ret = 1;
1074out:
1075 return ret;
1076}
1077
1078/*
1079 * write some zeros
1080 */
1081static int write_partial_skip(struct ceph_connection *con)
1082{
1083 int ret;
1084
1085 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001086 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001087
Alex Elder31739132012-03-07 11:40:08 -06001088 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001089 if (ret <= 0)
1090 goto out;
1091 con->out_skip -= ret;
1092 }
1093 ret = 1;
1094out:
1095 return ret;
1096}
1097
1098/*
1099 * Prepare to read connection handshake, or an ack.
1100 */
Sage Weileed0ef22009-11-10 14:34:36 -08001101static void prepare_read_banner(struct ceph_connection *con)
1102{
1103 dout("prepare_read_banner %p\n", con);
1104 con->in_base_pos = 0;
1105}
1106
Sage Weil31b80062009-10-06 11:31:13 -07001107static void prepare_read_connect(struct ceph_connection *con)
1108{
1109 dout("prepare_read_connect %p\n", con);
1110 con->in_base_pos = 0;
1111}
1112
1113static void prepare_read_ack(struct ceph_connection *con)
1114{
1115 dout("prepare_read_ack %p\n", con);
1116 con->in_base_pos = 0;
1117}
1118
1119static void prepare_read_tag(struct ceph_connection *con)
1120{
1121 dout("prepare_read_tag %p\n", con);
1122 con->in_base_pos = 0;
1123 con->in_tag = CEPH_MSGR_TAG_READY;
1124}
1125
1126/*
1127 * Prepare to read a message.
1128 */
1129static int prepare_read_message(struct ceph_connection *con)
1130{
1131 dout("prepare_read_message %p\n", con);
1132 BUG_ON(con->in_msg != NULL);
1133 con->in_base_pos = 0;
1134 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1135 return 0;
1136}
1137
1138
1139static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001140 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001141{
Alex Eldere6cee712012-05-10 10:29:50 -05001142 while (con->in_base_pos < end) {
1143 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001144 int have = size - left;
1145 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1146 if (ret <= 0)
1147 return ret;
1148 con->in_base_pos += ret;
1149 }
1150 return 1;
1151}
1152
1153
1154/*
1155 * Read all or part of the connect-side handshake on a new connection
1156 */
Sage Weileed0ef22009-11-10 14:34:36 -08001157static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001158{
Alex Elderfd516532012-05-10 10:29:50 -05001159 int size;
1160 int end;
1161 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001162
Sage Weileed0ef22009-11-10 14:34:36 -08001163 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001164
1165 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001166 size = strlen(CEPH_BANNER);
1167 end = size;
1168 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001169 if (ret <= 0)
1170 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001171
1172 size = sizeof (con->actual_peer_addr);
1173 end += size;
1174 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001175 if (ret <= 0)
1176 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001177
1178 size = sizeof (con->peer_addr_for_me);
1179 end += size;
1180 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001181 if (ret <= 0)
1182 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001183
Sage Weileed0ef22009-11-10 14:34:36 -08001184out:
1185 return ret;
1186}
1187
1188static int read_partial_connect(struct ceph_connection *con)
1189{
Alex Elderfd516532012-05-10 10:29:50 -05001190 int size;
1191 int end;
1192 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001193
1194 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1195
Alex Elderfd516532012-05-10 10:29:50 -05001196 size = sizeof (con->in_reply);
1197 end = size;
1198 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001199 if (ret <= 0)
1200 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001201
1202 size = le32_to_cpu(con->in_reply.authorizer_len);
1203 end += size;
1204 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001205 if (ret <= 0)
1206 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001207
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001208 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1209 con, (int)con->in_reply.tag,
1210 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001211 le32_to_cpu(con->in_reply.global_seq));
1212out:
1213 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001214
Sage Weil31b80062009-10-06 11:31:13 -07001215}
1216
1217/*
1218 * Verify the hello banner looks okay.
1219 */
1220static int verify_hello(struct ceph_connection *con)
1221{
1222 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001223 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001224 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001225 con->error_msg = "protocol error, bad banner";
1226 return -1;
1227 }
1228 return 0;
1229}
1230
1231static bool addr_is_blank(struct sockaddr_storage *ss)
1232{
1233 switch (ss->ss_family) {
1234 case AF_INET:
1235 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1236 case AF_INET6:
1237 return
1238 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1239 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1240 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1241 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1242 }
1243 return false;
1244}
1245
1246static int addr_port(struct sockaddr_storage *ss)
1247{
1248 switch (ss->ss_family) {
1249 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001250 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001251 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001252 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001253 }
1254 return 0;
1255}
1256
1257static void addr_set_port(struct sockaddr_storage *ss, int p)
1258{
1259 switch (ss->ss_family) {
1260 case AF_INET:
1261 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001262 break;
Sage Weil31b80062009-10-06 11:31:13 -07001263 case AF_INET6:
1264 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001265 break;
Sage Weil31b80062009-10-06 11:31:13 -07001266 }
1267}
1268
1269/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001270 * Unlike other *_pton function semantics, zero indicates success.
1271 */
1272static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1273 char delim, const char **ipend)
1274{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001275 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1276 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001277
1278 memset(ss, 0, sizeof(*ss));
1279
1280 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1281 ss->ss_family = AF_INET;
1282 return 0;
1283 }
1284
1285 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1286 ss->ss_family = AF_INET6;
1287 return 0;
1288 }
1289
1290 return -EINVAL;
1291}
1292
1293/*
1294 * Extract hostname string and resolve using kernel DNS facility.
1295 */
1296#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1297static int ceph_dns_resolve_name(const char *name, size_t namelen,
1298 struct sockaddr_storage *ss, char delim, const char **ipend)
1299{
1300 const char *end, *delim_p;
1301 char *colon_p, *ip_addr = NULL;
1302 int ip_len, ret;
1303
1304 /*
1305 * The end of the hostname occurs immediately preceding the delimiter or
1306 * the port marker (':') where the delimiter takes precedence.
1307 */
1308 delim_p = memchr(name, delim, namelen);
1309 colon_p = memchr(name, ':', namelen);
1310
1311 if (delim_p && colon_p)
1312 end = delim_p < colon_p ? delim_p : colon_p;
1313 else if (!delim_p && colon_p)
1314 end = colon_p;
1315 else {
1316 end = delim_p;
1317 if (!end) /* case: hostname:/ */
1318 end = name + namelen;
1319 }
1320
1321 if (end <= name)
1322 return -EINVAL;
1323
1324 /* do dns_resolve upcall */
1325 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1326 if (ip_len > 0)
1327 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1328 else
1329 ret = -ESRCH;
1330
1331 kfree(ip_addr);
1332
1333 *ipend = end;
1334
1335 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1336 ret, ret ? "failed" : ceph_pr_addr(ss));
1337
1338 return ret;
1339}
1340#else
1341static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1342 struct sockaddr_storage *ss, char delim, const char **ipend)
1343{
1344 return -EINVAL;
1345}
1346#endif
1347
1348/*
1349 * Parse a server name (IP or hostname). If a valid IP address is not found
1350 * then try to extract a hostname to resolve using userspace DNS upcall.
1351 */
1352static int ceph_parse_server_name(const char *name, size_t namelen,
1353 struct sockaddr_storage *ss, char delim, const char **ipend)
1354{
1355 int ret;
1356
1357 ret = ceph_pton(name, namelen, ss, delim, ipend);
1358 if (ret)
1359 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1360
1361 return ret;
1362}
1363
1364/*
Sage Weil31b80062009-10-06 11:31:13 -07001365 * Parse an ip[:port] list into an addr array. Use the default
1366 * monitor port if a port isn't specified.
1367 */
1368int ceph_parse_ips(const char *c, const char *end,
1369 struct ceph_entity_addr *addr,
1370 int max_count, int *count)
1371{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001372 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001373 const char *p = c;
1374
1375 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1376 for (i = 0; i < max_count; i++) {
1377 const char *ipend;
1378 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001379 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001380 char delim = ',';
1381
1382 if (*p == '[') {
1383 delim = ']';
1384 p++;
1385 }
Sage Weil31b80062009-10-06 11:31:13 -07001386
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001387 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1388 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001389 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001390 ret = -EINVAL;
1391
Sage Weil31b80062009-10-06 11:31:13 -07001392 p = ipend;
1393
Sage Weil39139f62010-07-08 09:54:52 -07001394 if (delim == ']') {
1395 if (*p != ']') {
1396 dout("missing matching ']'\n");
1397 goto bad;
1398 }
1399 p++;
1400 }
1401
Sage Weil31b80062009-10-06 11:31:13 -07001402 /* port? */
1403 if (p < end && *p == ':') {
1404 port = 0;
1405 p++;
1406 while (p < end && *p >= '0' && *p <= '9') {
1407 port = (port * 10) + (*p - '0');
1408 p++;
1409 }
1410 if (port > 65535 || port == 0)
1411 goto bad;
1412 } else {
1413 port = CEPH_MON_PORT;
1414 }
1415
1416 addr_set_port(ss, port);
1417
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001418 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001419
1420 if (p == end)
1421 break;
1422 if (*p != ',')
1423 goto bad;
1424 p++;
1425 }
1426
1427 if (p != end)
1428 goto bad;
1429
1430 if (count)
1431 *count = i + 1;
1432 return 0;
1433
1434bad:
Sage Weil39139f62010-07-08 09:54:52 -07001435 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001436 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001437}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001438EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001439
Sage Weileed0ef22009-11-10 14:34:36 -08001440static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001441{
Sage Weileed0ef22009-11-10 14:34:36 -08001442 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001443
1444 if (verify_hello(con) < 0)
1445 return -1;
1446
Sage Weil63f2d212009-11-03 15:17:56 -08001447 ceph_decode_addr(&con->actual_peer_addr);
1448 ceph_decode_addr(&con->peer_addr_for_me);
1449
Sage Weil31b80062009-10-06 11:31:13 -07001450 /*
1451 * Make sure the other end is who we wanted. note that the other
1452 * end may not yet know their ip address, so if it's 0.0.0.0, give
1453 * them the benefit of the doubt.
1454 */
Sage Weil103e2d32010-01-07 16:12:36 -08001455 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1456 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001457 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1458 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001459 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001460 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001461 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001462 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001463 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001464 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001465 return -1;
1466 }
1467
1468 /*
1469 * did we learn our address?
1470 */
1471 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1472 int port = addr_port(&con->msgr->inst.addr.in_addr);
1473
1474 memcpy(&con->msgr->inst.addr.in_addr,
1475 &con->peer_addr_for_me.in_addr,
1476 sizeof(con->peer_addr_for_me.in_addr));
1477 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001478 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001479 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001480 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001481 }
1482
Sage Weileed0ef22009-11-10 14:34:36 -08001483 return 0;
1484}
1485
Sage Weil04a419f2009-12-23 09:30:21 -08001486static void fail_protocol(struct ceph_connection *con)
1487{
1488 reset_connection(con);
Sage Weil8dacc7d2012-07-20 17:24:40 -07001489 BUG_ON(con->state != CON_STATE_NEGOTIATING);
1490 con->state = CON_STATE_CLOSED;
Sage Weil04a419f2009-12-23 09:30:21 -08001491}
1492
Sage Weileed0ef22009-11-10 14:34:36 -08001493static int process_connect(struct ceph_connection *con)
1494{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001495 u64 sup_feat = con->msgr->supported_features;
1496 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001497 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001498 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001499
Sage Weileed0ef22009-11-10 14:34:36 -08001500 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1501
Sage Weil31b80062009-10-06 11:31:13 -07001502 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001503 case CEPH_MSGR_TAG_FEATURES:
1504 pr_err("%s%lld %s feature set mismatch,"
1505 " my %llx < server's %llx, missing %llx\n",
1506 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001507 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001508 sup_feat, server_feat, server_feat & ~sup_feat);
1509 con->error_msg = "missing required protocol features";
1510 fail_protocol(con);
1511 return -1;
1512
Sage Weil31b80062009-10-06 11:31:13 -07001513 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001514 pr_err("%s%lld %s protocol version mismatch,"
1515 " my %d != server's %d\n",
1516 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001517 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001518 le32_to_cpu(con->out_connect.protocol_version),
1519 le32_to_cpu(con->in_reply.protocol_version));
1520 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001521 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001522 return -1;
1523
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001524 case CEPH_MSGR_TAG_BADAUTHORIZER:
1525 con->auth_retry++;
1526 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1527 con->auth_retry);
1528 if (con->auth_retry == 2) {
1529 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001530 return -1;
1531 }
1532 con->auth_retry = 1;
Alex Eldere825a662012-05-16 15:16:38 -05001533 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001534 if (ret < 0)
1535 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001536 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001537 break;
Sage Weil31b80062009-10-06 11:31:13 -07001538
1539 case CEPH_MSGR_TAG_RESETSESSION:
1540 /*
1541 * If we connected with a large connect_seq but the peer
1542 * has no record of a session with us (no connection, or
1543 * connect_seq == 0), they will send RESETSESION to indicate
1544 * that they must have reset their session, and may have
1545 * dropped messages.
1546 */
1547 dout("process_connect got RESET peer seq %u\n",
Sage Weila16cb1f2012-07-10 11:53:34 -07001548 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil31b80062009-10-06 11:31:13 -07001549 pr_err("%s%lld %s connection reset\n",
1550 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001551 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001552 reset_connection(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001553 ret = prepare_write_connect(con);
1554 if (ret < 0)
1555 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001556 prepare_read_connect(con);
1557
1558 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001559 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001560 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1561 if (con->ops->peer_reset)
1562 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001563 mutex_lock(&con->mutex);
Sage Weil8dacc7d2012-07-20 17:24:40 -07001564 if (con->state != CON_STATE_NEGOTIATING)
Sage Weil0da5d702011-05-19 11:21:05 -07001565 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001566 break;
1567
1568 case CEPH_MSGR_TAG_RETRY_SESSION:
1569 /*
1570 * If we sent a smaller connect_seq than the peer has, try
1571 * again with a larger value.
1572 */
Sage Weila16cb1f2012-07-10 11:53:34 -07001573 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001574 le32_to_cpu(con->out_connect.connect_seq),
Sage Weila16cb1f2012-07-10 11:53:34 -07001575 le32_to_cpu(con->in_reply.connect_seq));
1576 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001577 ret = prepare_write_connect(con);
1578 if (ret < 0)
1579 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001580 prepare_read_connect(con);
1581 break;
1582
1583 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1584 /*
1585 * If we sent a smaller global_seq than the peer has, try
1586 * again with a larger value.
1587 */
Sage Weileed0ef22009-11-10 14:34:36 -08001588 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001589 con->peer_global_seq,
Sage Weila16cb1f2012-07-10 11:53:34 -07001590 le32_to_cpu(con->in_reply.global_seq));
Sage Weil31b80062009-10-06 11:31:13 -07001591 get_global_seq(con->msgr,
Sage Weila16cb1f2012-07-10 11:53:34 -07001592 le32_to_cpu(con->in_reply.global_seq));
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001593 ret = prepare_write_connect(con);
1594 if (ret < 0)
1595 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001596 prepare_read_connect(con);
1597 break;
1598
1599 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001600 if (req_feat & ~server_feat) {
1601 pr_err("%s%lld %s protocol feature mismatch,"
1602 " my required %llx > server's %llx, need %llx\n",
1603 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001604 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001605 req_feat, server_feat, req_feat & ~server_feat);
1606 con->error_msg = "missing required protocol features";
1607 fail_protocol(con);
1608 return -1;
1609 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07001610
1611 BUG_ON(con->state != CON_STATE_NEGOTIATING);
1612 con->state = CON_STATE_OPEN;
1613
Sage Weil31b80062009-10-06 11:31:13 -07001614 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1615 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001616 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001617 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1618 con->peer_global_seq,
1619 le32_to_cpu(con->in_reply.connect_seq),
1620 con->connect_seq);
1621 WARN_ON(con->connect_seq !=
1622 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001623
1624 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elder928443c2012-05-22 11:41:43 -05001625 set_bit(LOSSYTX, &con->flags);
Sage Weil92ac41d2009-12-14 14:56:56 -08001626
Sage Weil85effe12012-07-30 16:22:05 -07001627 con->delay = 0; /* reset backoff memory */
1628
Sage Weil31b80062009-10-06 11:31:13 -07001629 prepare_read_tag(con);
1630 break;
1631
1632 case CEPH_MSGR_TAG_WAIT:
1633 /*
1634 * If there is a connection race (we are opening
1635 * connections to each other), one of us may just have
1636 * to WAIT. This shouldn't happen if we are the
1637 * client.
1638 */
Sage Weil04177882011-05-12 15:33:17 -07001639 pr_err("process_connect got WAIT as client\n");
1640 con->error_msg = "protocol error, got WAIT as client";
1641 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001642
1643 default:
1644 pr_err("connect protocol error, will retry\n");
1645 con->error_msg = "protocol error, garbage tag during connect";
1646 return -1;
1647 }
1648 return 0;
1649}
1650
1651
1652/*
1653 * read (part of) an ack
1654 */
1655static int read_partial_ack(struct ceph_connection *con)
1656{
Alex Elderfd516532012-05-10 10:29:50 -05001657 int size = sizeof (con->in_temp_ack);
1658 int end = size;
1659
1660 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001661}
1662
1663
1664/*
1665 * We can finally discard anything that's been acked.
1666 */
1667static void process_ack(struct ceph_connection *con)
1668{
1669 struct ceph_msg *m;
1670 u64 ack = le64_to_cpu(con->in_temp_ack);
1671 u64 seq;
1672
Sage Weil31b80062009-10-06 11:31:13 -07001673 while (!list_empty(&con->out_sent)) {
1674 m = list_first_entry(&con->out_sent, struct ceph_msg,
1675 list_head);
1676 seq = le64_to_cpu(m->hdr.seq);
1677 if (seq > ack)
1678 break;
1679 dout("got ack for seq %llu type %d at %p\n", seq,
1680 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001681 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001682 ceph_msg_remove(m);
1683 }
Sage Weil31b80062009-10-06 11:31:13 -07001684 prepare_read_tag(con);
1685}
1686
1687
1688
1689
Yehuda Sadeh24504182010-01-08 13:58:34 -08001690static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001691 struct kvec *section,
1692 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001693{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001694 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001695
Yehuda Sadeh24504182010-01-08 13:58:34 -08001696 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001697
Yehuda Sadeh24504182010-01-08 13:58:34 -08001698 while (section->iov_len < sec_len) {
1699 BUG_ON(section->iov_base == NULL);
1700 left = sec_len - section->iov_len;
1701 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1702 section->iov_len, left);
1703 if (ret <= 0)
1704 return ret;
1705 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001706 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001707 if (section->iov_len == sec_len)
1708 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001709
1710 return 1;
1711}
1712
Alex Elder1c20f2d2012-06-04 14:43:32 -05001713static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
1714 struct ceph_msg_header *hdr);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001715
1716
1717static int read_partial_message_pages(struct ceph_connection *con,
1718 struct page **pages,
Eric Dumazet95c96172012-04-15 05:58:06 +00001719 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001720{
1721 void *p;
1722 int ret;
1723 int left;
1724
1725 left = min((int)(data_len - con->in_msg_pos.data_pos),
1726 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1727 /* (page) data */
1728 BUG_ON(pages == NULL);
1729 p = kmap(pages[con->in_msg_pos.page]);
1730 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1731 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001732 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001733 con->in_data_crc =
1734 crc32c(con->in_data_crc,
1735 p + con->in_msg_pos.page_pos, ret);
1736 kunmap(pages[con->in_msg_pos.page]);
1737 if (ret <= 0)
1738 return ret;
1739 con->in_msg_pos.data_pos += ret;
1740 con->in_msg_pos.page_pos += ret;
1741 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1742 con->in_msg_pos.page_pos = 0;
1743 con->in_msg_pos.page++;
1744 }
1745
1746 return ret;
1747}
1748
1749#ifdef CONFIG_BLOCK
1750static int read_partial_message_bio(struct ceph_connection *con,
1751 struct bio **bio_iter, int *bio_seg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001752 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001753{
1754 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1755 void *p;
1756 int ret, left;
1757
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001758 left = min((int)(data_len - con->in_msg_pos.data_pos),
1759 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1760
1761 p = kmap(bv->bv_page) + bv->bv_offset;
1762
1763 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1764 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001765 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001766 con->in_data_crc =
1767 crc32c(con->in_data_crc,
1768 p + con->in_msg_pos.page_pos, ret);
1769 kunmap(bv->bv_page);
1770 if (ret <= 0)
1771 return ret;
1772 con->in_msg_pos.data_pos += ret;
1773 con->in_msg_pos.page_pos += ret;
1774 if (con->in_msg_pos.page_pos == bv->bv_len) {
1775 con->in_msg_pos.page_pos = 0;
1776 iter_bio_next(bio_iter, bio_seg);
1777 }
1778
1779 return ret;
1780}
1781#endif
1782
Sage Weil31b80062009-10-06 11:31:13 -07001783/*
1784 * read (part of) a message.
1785 */
1786static int read_partial_message(struct ceph_connection *con)
1787{
1788 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05001789 int size;
1790 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001791 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00001792 unsigned int front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001793 bool do_datacrc = !con->msgr->nocrc;
Sage Weilae187562010-04-22 07:47:01 -07001794 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001795 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001796
1797 dout("read_partial_message con %p msg %p\n", con, m);
1798
1799 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05001800 size = sizeof (con->in_hdr);
1801 end = size;
1802 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05001803 if (ret <= 0)
1804 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001805
1806 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1807 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1808 pr_err("read_partial_message bad hdr "
1809 " crc %u != expected %u\n",
1810 crc, con->in_hdr.crc);
1811 return -EBADMSG;
1812 }
1813
Sage Weil31b80062009-10-06 11:31:13 -07001814 front_len = le32_to_cpu(con->in_hdr.front_len);
1815 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1816 return -EIO;
1817 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1818 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1819 return -EIO;
1820 data_len = le32_to_cpu(con->in_hdr.data_len);
1821 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1822 return -EIO;
1823
Sage Weilae187562010-04-22 07:47:01 -07001824 /* verify seq# */
1825 seq = le64_to_cpu(con->in_hdr.seq);
1826 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001827 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001828 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001829 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001830 seq, con->in_seq + 1);
1831 con->in_base_pos = -front_len - middle_len - data_len -
1832 sizeof(m->footer);
1833 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001834 return 0;
1835 } else if ((s64)seq - (s64)con->in_seq > 1) {
1836 pr_err("read_partial_message bad seq %lld expected %lld\n",
1837 seq, con->in_seq + 1);
1838 con->error_msg = "bad message sequence # for incoming message";
1839 return -EBADMSG;
1840 }
1841
Sage Weil31b80062009-10-06 11:31:13 -07001842 /* allocate message? */
1843 if (!con->in_msg) {
1844 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1845 con->in_hdr.front_len, con->in_hdr.data_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05001846 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
Sage Weil31b80062009-10-06 11:31:13 -07001847 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001848 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001849 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001850 con->in_base_pos = -front_len - middle_len - data_len -
1851 sizeof(m->footer);
1852 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001853 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001854 return 0;
1855 }
Sage Weila79832f2010-04-01 16:06:19 -07001856 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001857 con->error_msg =
1858 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001859 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001860 }
Alex Elder38941f82012-06-01 14:56:43 -05001861
1862 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07001863 m = con->in_msg;
1864 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001865 if (m->middle)
1866 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001867
1868 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001869 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001870 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001871 else
1872 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001873 con->in_msg_pos.data_pos = 0;
Sage Weila4107022012-07-30 16:20:25 -07001874
1875#ifdef CONFIG_BLOCK
1876 if (m->bio)
1877 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1878#endif
Sage Weil31b80062009-10-06 11:31:13 -07001879 }
1880
1881 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001882 ret = read_partial_message_section(con, &m->front, front_len,
1883 &con->in_front_crc);
1884 if (ret <= 0)
1885 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001886
1887 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001888 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001889 ret = read_partial_message_section(con, &m->middle->vec,
1890 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001891 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001892 if (ret <= 0)
1893 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001894 }
1895
1896 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001897 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001898 if (m->pages) {
1899 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001900 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001901 if (ret <= 0)
1902 return ret;
1903#ifdef CONFIG_BLOCK
1904 } else if (m->bio) {
Sage Weila4107022012-07-30 16:20:25 -07001905 BUG_ON(!m->bio_iter);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001906 ret = read_partial_message_bio(con,
1907 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001908 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001909 if (ret <= 0)
1910 return ret;
1911#endif
1912 } else {
1913 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001914 }
1915 }
1916
Sage Weil31b80062009-10-06 11:31:13 -07001917 /* footer */
Alex Elderfd516532012-05-10 10:29:50 -05001918 size = sizeof (m->footer);
1919 end += size;
1920 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05001921 if (ret <= 0)
1922 return ret;
1923
Sage Weil31b80062009-10-06 11:31:13 -07001924 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1925 m, front_len, m->footer.front_crc, middle_len,
1926 m->footer.middle_crc, data_len, m->footer.data_crc);
1927
1928 /* crc ok? */
1929 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1930 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1931 m, con->in_front_crc, m->footer.front_crc);
1932 return -EBADMSG;
1933 }
1934 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1935 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1936 m, con->in_middle_crc, m->footer.middle_crc);
1937 return -EBADMSG;
1938 }
Alex Elderbca064d2012-02-15 07:43:54 -06001939 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001940 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1941 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1942 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1943 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1944 return -EBADMSG;
1945 }
1946
1947 return 1; /* done! */
1948}
1949
1950/*
1951 * Process message. This happens in the worker thread. The callback should
1952 * be careful not to do anything that waits on other incoming messages or it
1953 * may deadlock.
1954 */
1955static void process_message(struct ceph_connection *con)
1956{
Sage Weil5e095e82009-12-14 14:30:34 -08001957 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001958
Alex Elder38941f82012-06-01 14:56:43 -05001959 BUG_ON(con->in_msg->con != con);
1960 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08001961 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001962 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07001963 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07001964
1965 /* if first message, set peer_name */
1966 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001967 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001968
Sage Weil31b80062009-10-06 11:31:13 -07001969 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001970 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001971
1972 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1973 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001974 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001975 le16_to_cpu(msg->hdr.type),
1976 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1977 le32_to_cpu(msg->hdr.front_len),
1978 le32_to_cpu(msg->hdr.data_len),
1979 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1980 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001981
1982 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001983 prepare_read_tag(con);
1984}
1985
1986
1987/*
1988 * Write something to the socket. Called in a worker thread when the
1989 * socket appears to be writeable and we have something ready to send.
1990 */
1991static int try_write(struct ceph_connection *con)
1992{
Sage Weil31b80062009-10-06 11:31:13 -07001993 int ret = 1;
1994
Sage Weild59315c2012-06-21 12:49:23 -07001995 dout("try_write start %p state %lu\n", con, con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001996
Sage Weil31b80062009-10-06 11:31:13 -07001997more:
1998 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1999
2000 /* open the socket first? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002001 if (con->state == CON_STATE_PREOPEN) {
2002 BUG_ON(con->sock);
2003 con->state = CON_STATE_CONNECTING;
Alex Eldera5988c42012-05-29 11:04:58 -05002004
Alex Eldere2200422012-05-23 14:35:23 -05002005 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002006 prepare_write_banner(con);
Sage Weileed0ef22009-11-10 14:34:36 -08002007 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07002008
Sage Weilcf3e5c42009-12-11 09:48:05 -08002009 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002010 con->in_tag = CEPH_MSGR_TAG_READY;
2011 dout("try_write initiating connect on %p new state %lu\n",
2012 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06002013 ret = ceph_tcp_connect(con);
2014 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07002015 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07002016 goto out;
2017 }
2018 }
2019
2020more_kvec:
2021 /* kvec data queued? */
2022 if (con->out_skip) {
2023 ret = write_partial_skip(con);
2024 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002025 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002026 }
2027 if (con->out_kvec_left) {
2028 ret = write_partial_kvec(con);
2029 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002030 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002031 }
2032
2033 /* msg pages? */
2034 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08002035 if (con->out_msg_done) {
2036 ceph_msg_put(con->out_msg);
2037 con->out_msg = NULL; /* we're done with this one */
2038 goto do_next;
2039 }
2040
Sage Weil31b80062009-10-06 11:31:13 -07002041 ret = write_partial_msg_pages(con);
2042 if (ret == 1)
2043 goto more_kvec; /* we need to send the footer, too! */
2044 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002045 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002046 if (ret < 0) {
2047 dout("try_write write_partial_msg_pages err %d\n",
2048 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002049 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002050 }
2051 }
2052
Sage Weilc86a2932009-12-14 14:04:30 -08002053do_next:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002054 if (con->state == CON_STATE_OPEN) {
Sage Weil31b80062009-10-06 11:31:13 -07002055 /* is anything else pending? */
2056 if (!list_empty(&con->out_queue)) {
2057 prepare_write_message(con);
2058 goto more;
2059 }
2060 if (con->in_seq > con->in_seq_acked) {
2061 prepare_write_ack(con);
2062 goto more;
2063 }
Alex Elder928443c2012-05-22 11:41:43 -05002064 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002065 prepare_write_keepalive(con);
2066 goto more;
2067 }
2068 }
2069
2070 /* Nothing to do! */
Alex Elder928443c2012-05-22 11:41:43 -05002071 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002072 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002073 ret = 0;
2074out:
Sage Weil42961d22011-01-25 08:19:34 -08002075 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002076 return ret;
2077}
2078
2079
2080
2081/*
2082 * Read what we can from the socket.
2083 */
2084static int try_read(struct ceph_connection *con)
2085{
Sage Weil31b80062009-10-06 11:31:13 -07002086 int ret = -1;
2087
Sage Weil31b80062009-10-06 11:31:13 -07002088more:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002089 dout("try_read start on %p state %lu\n", con, con->state);
2090 if (con->state != CON_STATE_CONNECTING &&
2091 con->state != CON_STATE_NEGOTIATING &&
2092 con->state != CON_STATE_OPEN)
2093 return 0;
2094
2095 BUG_ON(!con->sock);
2096
Sage Weil31b80062009-10-06 11:31:13 -07002097 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2098 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002099
Sage Weil8dacc7d2012-07-20 17:24:40 -07002100 if (con->state == CON_STATE_CONNECTING) {
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
Sage Weil8dacc7d2012-07-20 17:24:40 -07002109 BUG_ON(con->state != CON_STATE_CONNECTING);
2110 con->state = CON_STATE_NEGOTIATING;
Alex Elder7593af92012-05-24 11:55:03 -05002111
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
Sage Weil8dacc7d2012-07-20 17:24:40 -07002122 if (con->state == CON_STATE_NEGOTIATING) {
Alex Elder7593af92012-05-24 11:55:03 -05002123 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
Sage Weil8dacc7d2012-07-20 17:24:40 -07002133 BUG_ON(con->state != CON_STATE_OPEN);
2134
Sage Weil31b80062009-10-06 11:31:13 -07002135 if (con->in_base_pos < 0) {
2136 /*
2137 * skipping + discarding content.
2138 *
2139 * FIXME: there must be a better way to do this!
2140 */
Alex Elder84495f42012-02-15 07:43:55 -06002141 static char buf[SKIP_BUF_SIZE];
2142 int skip = min((int) sizeof (buf), -con->in_base_pos);
2143
Sage Weil31b80062009-10-06 11:31:13 -07002144 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2145 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2146 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002147 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002148 con->in_base_pos += ret;
2149 if (con->in_base_pos)
2150 goto more;
2151 }
2152 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2153 /*
2154 * what's next?
2155 */
2156 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2157 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002158 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002159 dout("try_read got tag %d\n", (int)con->in_tag);
2160 switch (con->in_tag) {
2161 case CEPH_MSGR_TAG_MSG:
2162 prepare_read_message(con);
2163 break;
2164 case CEPH_MSGR_TAG_ACK:
2165 prepare_read_ack(con);
2166 break;
2167 case CEPH_MSGR_TAG_CLOSE:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002168 con_close_socket(con);
2169 con->state = CON_STATE_CLOSED;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002170 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002171 default:
2172 goto bad_tag;
2173 }
2174 }
2175 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2176 ret = read_partial_message(con);
2177 if (ret <= 0) {
2178 switch (ret) {
2179 case -EBADMSG:
2180 con->error_msg = "bad crc";
2181 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002182 break;
Sage Weil31b80062009-10-06 11:31:13 -07002183 case -EIO:
2184 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002185 break;
Sage Weil31b80062009-10-06 11:31:13 -07002186 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002187 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002188 }
2189 if (con->in_tag == CEPH_MSGR_TAG_READY)
2190 goto more;
2191 process_message(con);
2192 goto more;
2193 }
2194 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2195 ret = read_partial_ack(con);
2196 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002197 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002198 process_ack(con);
2199 goto more;
2200 }
2201
Sage Weil31b80062009-10-06 11:31:13 -07002202out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002203 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002204 return ret;
2205
2206bad_tag:
2207 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2208 con->error_msg = "protocol error, garbage tag";
2209 ret = -1;
2210 goto out;
2211}
2212
2213
2214/*
2215 * Atomically queue work on a connection. Bump @con reference to
2216 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002217 */
2218static void queue_con(struct ceph_connection *con)
2219{
Sage Weil31b80062009-10-06 11:31:13 -07002220 if (!con->ops->get(con)) {
2221 dout("queue_con %p ref count 0\n", con);
2222 return;
2223 }
2224
Tejun Heof363e452011-01-03 14:49:46 +01002225 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002226 dout("queue_con %p - already queued\n", con);
2227 con->ops->put(con);
2228 } else {
2229 dout("queue_con %p\n", con);
2230 }
2231}
2232
2233/*
2234 * Do some work on a connection. Drop a connection ref when we're done.
2235 */
2236static void con_work(struct work_struct *work)
2237{
2238 struct ceph_connection *con = container_of(work, struct ceph_connection,
2239 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002240 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002241
Sage Weil9dd46582010-04-28 13:51:50 -07002242 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002243restart:
Alex Elder188048b2012-06-20 21:53:53 -05002244 if (test_and_clear_bit(SOCK_CLOSED, &con->flags)) {
Sage Weil8dacc7d2012-07-20 17:24:40 -07002245 switch (con->state) {
2246 case CON_STATE_CONNECTING:
Alex Elder188048b2012-06-20 21:53:53 -05002247 con->error_msg = "connection failed";
Sage Weil8dacc7d2012-07-20 17:24:40 -07002248 break;
2249 case CON_STATE_NEGOTIATING:
2250 con->error_msg = "negotiation failed";
2251 break;
2252 case CON_STATE_OPEN:
2253 con->error_msg = "socket closed";
2254 break;
2255 default:
2256 dout("unrecognized con state %d\n", (int)con->state);
Alex Eldere27947c2012-05-23 14:35:23 -05002257 con->error_msg = "unrecognized con state";
Sage Weil8dacc7d2012-07-20 17:24:40 -07002258 BUG();
2259 }
Alex Elder188048b2012-06-20 21:53:53 -05002260 goto fault;
2261 }
2262
Alex Elder928443c2012-05-22 11:41:43 -05002263 if (test_and_clear_bit(BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002264 dout("con_work %p backing off\n", con);
2265 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2266 round_jiffies_relative(con->delay))) {
2267 dout("con_work %p backoff %lu\n", con, con->delay);
2268 mutex_unlock(&con->mutex);
2269 return;
2270 } else {
2271 con->ops->put(con);
2272 dout("con_work %p FAILED to back off %lu\n", con,
2273 con->delay);
2274 }
2275 }
Sage Weil9dd46582010-04-28 13:51:50 -07002276
Sage Weil8dacc7d2012-07-20 17:24:40 -07002277 if (con->state == CON_STATE_STANDBY) {
Sage Weile00de342011-03-04 12:25:05 -08002278 dout("con_work %p STANDBY\n", con);
2279 goto done;
2280 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07002281 if (con->state == CON_STATE_CLOSED) {
Sage Weil2e8cb102012-07-20 15:40:04 -07002282 dout("con_work %p CLOSED\n", con);
2283 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -07002284 goto done;
2285 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07002286 if (con->state == CON_STATE_PREOPEN) {
Sage Weil31b80062009-10-06 11:31:13 -07002287 dout("con_work OPENING\n");
Sage Weil2e8cb102012-07-20 15:40:04 -07002288 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -07002289 }
2290
Sage Weil0da5d702011-05-19 11:21:05 -07002291 ret = try_read(con);
2292 if (ret == -EAGAIN)
2293 goto restart;
Sage Weil3a140a02012-07-30 16:24:21 -07002294 if (ret < 0) {
2295 con->error_msg = "socket error on read";
Sage Weil0da5d702011-05-19 11:21:05 -07002296 goto fault;
Sage Weil3a140a02012-07-30 16:24:21 -07002297 }
Sage Weil0da5d702011-05-19 11:21:05 -07002298
2299 ret = try_write(con);
2300 if (ret == -EAGAIN)
2301 goto restart;
Sage Weil3a140a02012-07-30 16:24:21 -07002302 if (ret < 0) {
2303 con->error_msg = "socket error on write";
Sage Weil0da5d702011-05-19 11:21:05 -07002304 goto fault;
Sage Weil3a140a02012-07-30 16:24:21 -07002305 }
Sage Weil31b80062009-10-06 11:31:13 -07002306
2307done:
Sage Weil9dd46582010-04-28 13:51:50 -07002308 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002309done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002310 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002311 return;
2312
2313fault:
2314 mutex_unlock(&con->mutex);
2315 ceph_fault(con); /* error/fault path */
2316 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002317}
2318
2319
2320/*
2321 * Generic error/fault handler. A retry mechanism is used with
2322 * exponential backoff
2323 */
2324static void ceph_fault(struct ceph_connection *con)
2325{
Sage Weil3b5ede02012-07-20 15:22:53 -07002326 mutex_lock(&con->mutex);
2327
Sage Weil31b80062009-10-06 11:31:13 -07002328 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002329 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002330 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002331 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002332
Sage Weil8dacc7d2012-07-20 17:24:40 -07002333 BUG_ON(con->state != CON_STATE_CONNECTING &&
2334 con->state != CON_STATE_NEGOTIATING &&
2335 con->state != CON_STATE_OPEN);
Sage Weilec302642009-12-22 10:43:42 -08002336
Sage Weil31b80062009-10-06 11:31:13 -07002337 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002338
Sage Weil3b5ede02012-07-20 15:22:53 -07002339 if (test_bit(LOSSYTX, &con->flags)) {
Sage Weil8dacc7d2012-07-20 17:24:40 -07002340 dout("fault on LOSSYTX channel, marking CLOSED\n");
2341 con->state = CON_STATE_CLOSED;
Sage Weil3b5ede02012-07-20 15:22:53 -07002342 goto out_unlock;
2343 }
2344
Sage Weil5e095e82009-12-14 14:30:34 -08002345 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002346 BUG_ON(con->in_msg->con != con);
2347 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08002348 ceph_msg_put(con->in_msg);
2349 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07002350 con->ops->put(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002351 }
Sage Weil31b80062009-10-06 11:31:13 -07002352
Sage Weile80a52d2010-02-25 12:40:45 -08002353 /* Requeue anything that hasn't been acked */
2354 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002355
Sage Weile76661d2011-03-03 10:10:15 -08002356 /* If there are no messages queued or keepalive pending, place
2357 * the connection in a STANDBY state */
2358 if (list_empty(&con->out_queue) &&
Alex Elder928443c2012-05-22 11:41:43 -05002359 !test_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weile00de342011-03-04 12:25:05 -08002360 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elder928443c2012-05-22 11:41:43 -05002361 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002362 con->state = CON_STATE_STANDBY;
Sage Weile80a52d2010-02-25 12:40:45 -08002363 } else {
2364 /* retry after a delay. */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002365 con->state = CON_STATE_PREOPEN;
Sage Weile80a52d2010-02-25 12:40:45 -08002366 if (con->delay == 0)
2367 con->delay = BASE_DELAY_INTERVAL;
2368 else if (con->delay < MAX_DELAY_INTERVAL)
2369 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002370 con->ops->get(con);
2371 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002372 round_jiffies_relative(con->delay))) {
2373 dout("fault queued %p delay %lu\n", con, con->delay);
2374 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002375 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002376 dout("fault failed to queue %p delay %lu, backoff\n",
2377 con, con->delay);
2378 /*
2379 * In many cases we see a socket state change
2380 * while con_work is running and end up
2381 * queuing (non-delayed) work, such that we
2382 * can't backoff with a delay. Set a flag so
2383 * that when con_work restarts we schedule the
2384 * delay then.
2385 */
Alex Elder928443c2012-05-22 11:41:43 -05002386 set_bit(BACKOFF, &con->flags);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002387 }
Sage Weil31b80062009-10-06 11:31:13 -07002388 }
2389
Sage Weil91e45ce32010-02-15 12:05:09 -08002390out_unlock:
2391 mutex_unlock(&con->mutex);
Sage Weil161fd652010-02-25 12:38:57 -08002392 /*
2393 * in case we faulted due to authentication, invalidate our
2394 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002395 */
Sage Weil161fd652010-02-25 12:38:57 -08002396 if (con->auth_retry && con->ops->invalidate_authorizer) {
2397 dout("calling invalidate_authorizer()\n");
2398 con->ops->invalidate_authorizer(con);
2399 }
2400
Sage Weil31b80062009-10-06 11:31:13 -07002401 if (con->ops->fault)
2402 con->ops->fault(con);
2403}
2404
2405
2406
2407/*
Alex Elder15d98822012-05-26 23:26:43 -05002408 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002409 */
Alex Elder15d98822012-05-26 23:26:43 -05002410void ceph_messenger_init(struct ceph_messenger *msgr,
2411 struct ceph_entity_addr *myaddr,
2412 u32 supported_features,
2413 u32 required_features,
2414 bool nocrc)
Sage Weil31b80062009-10-06 11:31:13 -07002415{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002416 msgr->supported_features = supported_features;
2417 msgr->required_features = required_features;
2418
Sage Weil31b80062009-10-06 11:31:13 -07002419 spin_lock_init(&msgr->global_seq_lock);
2420
Sage Weil31b80062009-10-06 11:31:13 -07002421 if (myaddr)
2422 msgr->inst.addr = *myaddr;
2423
2424 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002425 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002426 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002427 encode_my_addr(msgr);
Alex Elder15d98822012-05-26 23:26:43 -05002428 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002429
Guanjun Hea2a32582012-07-08 19:50:33 -07002430 atomic_set(&msgr->stopping, 0);
2431
Alex Elder15d98822012-05-26 23:26:43 -05002432 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002433}
Alex Elder15d98822012-05-26 23:26:43 -05002434EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002435
Sage Weile00de342011-03-04 12:25:05 -08002436static void clear_standby(struct ceph_connection *con)
2437{
2438 /* come back from STANDBY? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002439 if (con->state == CON_STATE_STANDBY) {
Sage Weile00de342011-03-04 12:25:05 -08002440 dout("clear_standby %p and ++connect_seq\n", con);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002441 con->state = CON_STATE_PREOPEN;
Sage Weile00de342011-03-04 12:25:05 -08002442 con->connect_seq++;
Alex Elder928443c2012-05-22 11:41:43 -05002443 WARN_ON(test_bit(WRITE_PENDING, &con->flags));
2444 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags));
Sage Weile00de342011-03-04 12:25:05 -08002445 }
2446}
2447
Sage Weil31b80062009-10-06 11:31:13 -07002448/*
2449 * Queue up an outgoing message on the given connection.
2450 */
2451void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2452{
Sage Weila59b55a2012-07-20 15:34:04 -07002453 /* set src+dst */
2454 msg->hdr.src = con->msgr->inst.name;
2455 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2456 msg->needs_out_seq = true;
2457
2458 mutex_lock(&con->mutex);
2459
Sage Weil8dacc7d2012-07-20 17:24:40 -07002460 if (con->state == CON_STATE_CLOSED) {
Sage Weil31b80062009-10-06 11:31:13 -07002461 dout("con_send %p closed, dropping %p\n", con, msg);
2462 ceph_msg_put(msg);
Sage Weila59b55a2012-07-20 15:34:04 -07002463 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002464 return;
2465 }
2466
Alex Elder38941f82012-06-01 14:56:43 -05002467 BUG_ON(msg->con != NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002468 msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002469 BUG_ON(msg->con == NULL);
2470
Sage Weil31b80062009-10-06 11:31:13 -07002471 BUG_ON(!list_empty(&msg->list_head));
2472 list_add_tail(&msg->list_head, &con->out_queue);
2473 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2474 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2475 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2476 le32_to_cpu(msg->hdr.front_len),
2477 le32_to_cpu(msg->hdr.middle_len),
2478 le32_to_cpu(msg->hdr.data_len));
Sage Weil00650932012-07-20 15:33:04 -07002479
2480 clear_standby(con);
Sage Weilec302642009-12-22 10:43:42 -08002481 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002482
2483 /* if there wasn't anything waiting to send before, queue
2484 * new work */
Alex Elder928443c2012-05-22 11:41:43 -05002485 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002486 queue_con(con);
2487}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002488EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002489
2490/*
2491 * Revoke a message that was previously queued for send
2492 */
Alex Elder6740a842012-06-01 14:56:43 -05002493void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002494{
Alex Elder6740a842012-06-01 14:56:43 -05002495 struct ceph_connection *con = msg->con;
2496
2497 if (!con)
2498 return; /* Message not in our possession */
2499
Sage Weilec302642009-12-22 10:43:42 -08002500 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002501 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05002502 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002503 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -05002504 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002505 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -05002506 msg->con = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05002507 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05002508
Sage Weil31b80062009-10-06 11:31:13 -07002509 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002510 }
2511 if (con->out_msg == msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002512 dout("%s %p msg %p - was sending\n", __func__, con, msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002513 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002514 if (con->out_kvec_is_msg) {
2515 con->out_skip = con->out_kvec_bytes;
2516 con->out_kvec_is_msg = false;
2517 }
Sage Weiled98ada2010-07-05 12:15:14 -07002518 msg->hdr.seq = 0;
Alex Elder92ce0342012-06-04 14:43:33 -05002519
2520 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07002521 }
Sage Weilec302642009-12-22 10:43:42 -08002522 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002523}
2524
2525/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002526 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002527 */
Alex Elder8921d112012-06-01 14:56:43 -05002528void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002529{
Alex Elder8921d112012-06-01 14:56:43 -05002530 struct ceph_connection *con;
2531
2532 BUG_ON(msg == NULL);
2533 if (!msg->con) {
2534 dout("%s msg %p null con\n", __func__, msg);
2535
2536 return; /* Message not in our possession */
2537 }
2538
2539 con = msg->con;
Sage Weil350b1c32009-12-22 10:45:45 -08002540 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05002541 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002542 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
2543 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
2544 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002545
2546 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05002547 dout("%s %p msg %p revoked\n", __func__, con, msg);
2548 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08002549 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002550 front_len -
2551 middle_len -
2552 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002553 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002554 ceph_msg_put(con->in_msg);
2555 con->in_msg = NULL;
2556 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002557 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002558 } else {
Alex Elder8921d112012-06-01 14:56:43 -05002559 dout("%s %p in_msg %p msg %p no-op\n",
2560 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002561 }
2562 mutex_unlock(&con->mutex);
2563}
2564
2565/*
Sage Weil31b80062009-10-06 11:31:13 -07002566 * Queue a keepalive byte to ensure the tcp connection is alive.
2567 */
2568void ceph_con_keepalive(struct ceph_connection *con)
2569{
Sage Weile00de342011-03-04 12:25:05 -08002570 dout("con_keepalive %p\n", con);
Sage Weil00650932012-07-20 15:33:04 -07002571 mutex_lock(&con->mutex);
Sage Weile00de342011-03-04 12:25:05 -08002572 clear_standby(con);
Sage Weil00650932012-07-20 15:33:04 -07002573 mutex_unlock(&con->mutex);
Alex Elder928443c2012-05-22 11:41:43 -05002574 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2575 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002576 queue_con(con);
2577}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002578EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002579
2580
2581/*
2582 * construct a new message with given type, size
2583 * the new msg has a ref count of 1.
2584 */
Sage Weilb61c2762011-08-09 15:03:46 -07002585struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2586 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002587{
2588 struct ceph_msg *m;
2589
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002590 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002591 if (m == NULL)
2592 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002593 kref_init(&m->kref);
Alex Elder38941f82012-06-01 14:56:43 -05002594
2595 m->con = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002596 INIT_LIST_HEAD(&m->list_head);
2597
Sage Weil45c6ceb2010-05-11 15:01:51 -07002598 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002599 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002600 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2601 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002602 m->hdr.front_len = cpu_to_le32(front_len);
2603 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002604 m->hdr.data_len = 0;
2605 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002606 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002607 m->footer.front_crc = 0;
2608 m->footer.middle_crc = 0;
2609 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002610 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002611 m->front_max = front_len;
2612 m->front_is_vmalloc = false;
2613 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002614 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002615 m->pool = NULL;
2616
Henry C Changca208922011-05-03 02:29:56 +00002617 /* middle */
2618 m->middle = NULL;
2619
2620 /* data */
2621 m->nr_pages = 0;
2622 m->page_alignment = 0;
2623 m->pages = NULL;
2624 m->pagelist = NULL;
2625 m->bio = NULL;
2626 m->bio_iter = NULL;
2627 m->bio_seg = 0;
2628 m->trail = NULL;
2629
Sage Weil31b80062009-10-06 11:31:13 -07002630 /* front */
2631 if (front_len) {
2632 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002633 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002634 PAGE_KERNEL);
2635 m->front_is_vmalloc = true;
2636 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002637 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002638 }
2639 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002640 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002641 front_len);
2642 goto out2;
2643 }
2644 } else {
2645 m->front.iov_base = NULL;
2646 }
2647 m->front.iov_len = front_len;
2648
Sage Weilbb257662010-04-01 16:07:23 -07002649 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002650 return m;
2651
2652out2:
2653 ceph_msg_put(m);
2654out:
Sage Weilb61c2762011-08-09 15:03:46 -07002655 if (!can_fail) {
2656 pr_err("msg_new can't create type %d front %d\n", type,
2657 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002658 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002659 } else {
2660 dout("msg_new can't create type %d front %d\n", type,
2661 front_len);
2662 }
Sage Weila79832f2010-04-01 16:06:19 -07002663 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002664}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002665EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002666
2667/*
Sage Weil31b80062009-10-06 11:31:13 -07002668 * Allocate "middle" portion of a message, if it is needed and wasn't
2669 * allocated by alloc_msg. This allows us to read a small fixed-size
2670 * per-type header in the front and then gracefully fail (i.e.,
2671 * propagate the error to the caller based on info in the front) when
2672 * the middle is too large.
2673 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002674static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002675{
2676 int type = le16_to_cpu(msg->hdr.type);
2677 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2678
2679 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2680 ceph_msg_type_name(type), middle_len);
2681 BUG_ON(!middle_len);
2682 BUG_ON(msg->middle);
2683
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002684 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002685 if (!msg->middle)
2686 return -ENOMEM;
2687 return 0;
2688}
2689
Yehuda Sadeh24504182010-01-08 13:58:34 -08002690/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05002691 * Allocate a message for receiving an incoming message on a
2692 * connection, and save the result in con->in_msg. Uses the
2693 * connection's private alloc_msg op if available.
2694 *
2695 * Returns true if the message should be skipped, false otherwise.
2696 * If true is returned (skip message), con->in_msg will be NULL.
2697 * If false is returned, con->in_msg will contain a pointer to the
2698 * newly-allocated message, or NULL in case of memory exhaustion.
Yehuda Sadeh24504182010-01-08 13:58:34 -08002699 */
Alex Elder1c20f2d2012-06-04 14:43:32 -05002700static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2701 struct ceph_msg_header *hdr)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002702{
2703 int type = le16_to_cpu(hdr->type);
2704 int front_len = le32_to_cpu(hdr->front_len);
2705 int middle_len = le32_to_cpu(hdr->middle_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002706 int ret;
2707
Alex Elder1c20f2d2012-06-04 14:43:32 -05002708 BUG_ON(con->in_msg != NULL);
2709
Yehuda Sadeh24504182010-01-08 13:58:34 -08002710 if (con->ops->alloc_msg) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002711 int skip = 0;
2712
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002713 mutex_unlock(&con->mutex);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002714 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002715 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002716 if (con->in_msg) {
Sage Weil36eb71a2012-06-21 12:47:08 -07002717 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002718 BUG_ON(con->in_msg->con == NULL);
2719 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002720 if (skip)
2721 con->in_msg = NULL;
2722
2723 if (!con->in_msg)
2724 return skip != 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002725 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002726 if (!con->in_msg) {
2727 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2728 if (!con->in_msg) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002729 pr_err("unable to allocate msg type %d len %d\n",
2730 type, front_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002731 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002732 }
Sage Weil36eb71a2012-06-21 12:47:08 -07002733 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002734 BUG_ON(con->in_msg->con == NULL);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002735 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002736 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002737 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002738
Alex Elder1c20f2d2012-06-04 14:43:32 -05002739 if (middle_len && !con->in_msg->middle) {
2740 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002741 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002742 ceph_msg_put(con->in_msg);
2743 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002744 }
2745 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002746
Alex Elder1c20f2d2012-06-04 14:43:32 -05002747 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002748}
2749
Sage Weil31b80062009-10-06 11:31:13 -07002750
2751/*
2752 * Free a generically kmalloc'd message.
2753 */
2754void ceph_msg_kfree(struct ceph_msg *m)
2755{
2756 dout("msg_kfree %p\n", m);
2757 if (m->front_is_vmalloc)
2758 vfree(m->front.iov_base);
2759 else
2760 kfree(m->front.iov_base);
2761 kfree(m);
2762}
2763
2764/*
2765 * Drop a msg ref. Destroy as needed.
2766 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002767void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002768{
Sage Weilc2e552e2009-12-07 15:55:05 -08002769 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002770
Sage Weilc2e552e2009-12-07 15:55:05 -08002771 dout("ceph_msg_put last one on %p\n", m);
2772 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002773
Sage Weilc2e552e2009-12-07 15:55:05 -08002774 /* drop middle, data, if any */
2775 if (m->middle) {
2776 ceph_buffer_put(m->middle);
2777 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002778 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002779 m->nr_pages = 0;
2780 m->pages = NULL;
2781
Sage Weil58bb3b32009-12-23 12:12:31 -08002782 if (m->pagelist) {
2783 ceph_pagelist_release(m->pagelist);
2784 kfree(m->pagelist);
2785 m->pagelist = NULL;
2786 }
2787
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002788 m->trail = NULL;
2789
Sage Weilc2e552e2009-12-07 15:55:05 -08002790 if (m->pool)
2791 ceph_msgpool_put(m->pool, m);
2792 else
2793 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002794}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002795EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002796
2797void ceph_msg_dump(struct ceph_msg *msg)
2798{
2799 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2800 msg->front_max, msg->nr_pages);
2801 print_hex_dump(KERN_DEBUG, "header: ",
2802 DUMP_PREFIX_OFFSET, 16, 1,
2803 &msg->hdr, sizeof(msg->hdr), true);
2804 print_hex_dump(KERN_DEBUG, " front: ",
2805 DUMP_PREFIX_OFFSET, 16, 1,
2806 msg->front.iov_base, msg->front.iov_len, true);
2807 if (msg->middle)
2808 print_hex_dump(KERN_DEBUG, "middle: ",
2809 DUMP_PREFIX_OFFSET, 16, 1,
2810 msg->middle->vec.iov_base,
2811 msg->middle->vec.iov_len, true);
2812 print_hex_dump(KERN_DEBUG, "footer: ",
2813 DUMP_PREFIX_OFFSET, 16, 1,
2814 &msg->footer, sizeof(msg->footer), true);
2815}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002816EXPORT_SYMBOL(ceph_msg_dump);