blob: 39653944f21b0d87924fb7b27d894aa8483ca282 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil31b80062009-10-06 11:31:13 -07002
3#include <linux/crc32c.h>
4#include <linux/ctype.h>
5#include <linux/highmem.h>
6#include <linux/inet.h>
7#include <linux/kthread.h>
8#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Sage Weil31b80062009-10-06 11:31:13 -070010#include <linux/socket.h>
11#include <linux/string.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070012#include <linux/bio.h>
13#include <linux/blkdev.h>
Noah Watkinsee3b56f2011-09-23 11:48:42 -070014#include <linux/dns_resolver.h>
Sage Weil31b80062009-10-06 11:31:13 -070015#include <net/tcp.h>
16
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070017#include <linux/ceph/libceph.h>
18#include <linux/ceph/messenger.h>
19#include <linux/ceph/decode.h>
20#include <linux/ceph/pagelist.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040021#include <linux/export.h>
Sage Weil31b80062009-10-06 11:31:13 -070022
23/*
24 * Ceph uses the messenger to exchange ceph_msg messages with other
25 * hosts in the system. The messenger provides ordered and reliable
26 * delivery. We tolerate TCP disconnects by reconnecting (with
27 * exponential backoff) in the case of a fault (disconnection, bad
28 * crc, protocol error). Acks allow sent messages to be discarded by
29 * the sender.
30 */
31
Alex Elderce2c8902012-05-22 22:15:49 -050032/* State values for ceph_connection->sock_state; NEW is assumed to be 0 */
33
34#define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
35#define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
36#define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
37#define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
38#define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
39
Sage Weil31b80062009-10-06 11:31:13 -070040/* static tag bytes (protocol control messages) */
41static char tag_msg = CEPH_MSGR_TAG_MSG;
42static char tag_ack = CEPH_MSGR_TAG_ACK;
43static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
44
Sage Weila6a53492010-04-13 14:07:07 -070045#ifdef CONFIG_LOCKDEP
46static struct lock_class_key socket_class;
47#endif
48
Alex Elder84495f42012-02-15 07:43:55 -060049/*
50 * When skipping (ignoring) a block of input we read it into a "skip
51 * buffer," which is this many bytes in size.
52 */
53#define SKIP_BUF_SIZE 1024
Sage Weil31b80062009-10-06 11:31:13 -070054
55static void queue_con(struct ceph_connection *con);
56static void con_work(struct work_struct *);
57static void ceph_fault(struct ceph_connection *con);
58
Sage Weil31b80062009-10-06 11:31:13 -070059/*
Alex Elderf64a9312012-01-23 15:49:27 -060060 * Nicely render a sockaddr as a string. An array of formatted
61 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -070062 */
Alex Elderf64a9312012-01-23 15:49:27 -060063#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
64#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
65#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
66#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
67
68static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
69static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -070070
Alex Elder57666512012-01-23 15:49:27 -060071static struct page *zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -060072
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070073const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -070074{
75 int i;
76 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -060077 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
78 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -070079
Alex Elderf64a9312012-01-23 15:49:27 -060080 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -070081 s = addr_str[i];
82
83 switch (ss->ss_family) {
84 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -060085 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
86 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -070087 break;
88
89 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -060090 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
91 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -070092 break;
93
94 default:
Alex Elderd3002b92012-02-14 14:05:33 -060095 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
96 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -070097 }
98
99 return s;
100}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700101EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700102
Sage Weil63f2d212009-11-03 15:17:56 -0800103static void encode_my_addr(struct ceph_messenger *msgr)
104{
105 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
106 ceph_encode_addr(&msgr->my_enc_addr);
107}
108
Sage Weil31b80062009-10-06 11:31:13 -0700109/*
110 * work queue for all reading and writing to/from the socket.
111 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600112static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700113
Alex Elder6173d1f2012-02-14 14:05:33 -0600114void _ceph_msgr_exit(void)
115{
Alex Elderd3002b92012-02-14 14:05:33 -0600116 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600117 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600118 ceph_msgr_wq = NULL;
119 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600120
Alex Elder6173d1f2012-02-14 14:05:33 -0600121 BUG_ON(zero_page == NULL);
122 kunmap(zero_page);
123 page_cache_release(zero_page);
124 zero_page = NULL;
125}
126
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700127int ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700128{
Alex Elder57666512012-01-23 15:49:27 -0600129 BUG_ON(zero_page != NULL);
130 zero_page = ZERO_PAGE(0);
131 page_cache_get(zero_page);
132
Tejun Heof363e45fd12011-01-03 14:49:46 +0100133 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600134 if (ceph_msgr_wq)
135 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600136
Alex Elder6173d1f2012-02-14 14:05:33 -0600137 pr_err("msgr_init failed to create workqueue\n");
138 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600139
Alex Elder6173d1f2012-02-14 14:05:33 -0600140 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700141}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700142EXPORT_SYMBOL(ceph_msgr_init);
Sage Weil31b80062009-10-06 11:31:13 -0700143
144void ceph_msgr_exit(void)
145{
Alex Elder57666512012-01-23 15:49:27 -0600146 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600147
Alex Elder6173d1f2012-02-14 14:05:33 -0600148 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700149}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700150EXPORT_SYMBOL(ceph_msgr_exit);
Sage Weil31b80062009-10-06 11:31:13 -0700151
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700152void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700153{
154 flush_workqueue(ceph_msgr_wq);
155}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700156EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700157
Alex Elderce2c8902012-05-22 22:15:49 -0500158/* Connection socket state transition functions */
159
160static void con_sock_state_init(struct ceph_connection *con)
161{
162 int old_state;
163
164 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
165 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
166 printk("%s: unexpected old state %d\n", __func__, old_state);
167}
168
169static void con_sock_state_connecting(struct ceph_connection *con)
170{
171 int old_state;
172
173 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
174 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
175 printk("%s: unexpected old state %d\n", __func__, old_state);
176}
177
178static void con_sock_state_connected(struct ceph_connection *con)
179{
180 int old_state;
181
182 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
183 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
184 printk("%s: unexpected old state %d\n", __func__, old_state);
185}
186
187static void con_sock_state_closing(struct ceph_connection *con)
188{
189 int old_state;
190
191 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
192 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
193 old_state != CON_SOCK_STATE_CONNECTED &&
194 old_state != CON_SOCK_STATE_CLOSING))
195 printk("%s: unexpected old state %d\n", __func__, old_state);
196}
197
198static void con_sock_state_closed(struct ceph_connection *con)
199{
200 int old_state;
201
202 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
203 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
204 old_state != CON_SOCK_STATE_CLOSING))
205 printk("%s: unexpected old state %d\n", __func__, old_state);
206}
Sage Weila922d382010-05-29 09:41:23 -0700207
Sage Weil31b80062009-10-06 11:31:13 -0700208/*
209 * socket callback functions
210 */
211
212/* data available on socket, or listen socket received a connect */
Alex Elder327800b2012-05-22 11:41:43 -0500213static void ceph_sock_data_ready(struct sock *sk, int count_unused)
Sage Weil31b80062009-10-06 11:31:13 -0700214{
Alex Elderbd406142012-01-23 15:49:27 -0600215 struct ceph_connection *con = sk->sk_user_data;
216
Sage Weil31b80062009-10-06 11:31:13 -0700217 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500218 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700219 con, con->state);
220 queue_con(con);
221 }
222}
223
224/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500225static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700226{
Alex Elderd3002b92012-02-14 14:05:33 -0600227 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700228
Jim Schutt182fac22012-02-29 08:30:58 -0700229 /* only queue to workqueue if there is data we want to write,
230 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500231 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700232 * doesn't get called again until try_write() fills the socket
233 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
234 * and net/core/stream.c:sk_stream_write_space().
235 */
Alex Elder928443c2012-05-22 11:41:43 -0500236 if (test_bit(WRITE_PENDING, &con->flags)) {
Jim Schutt182fac22012-02-29 08:30:58 -0700237 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500238 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700239 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
240 queue_con(con);
241 }
Sage Weil31b80062009-10-06 11:31:13 -0700242 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500243 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700244 }
Sage Weil31b80062009-10-06 11:31:13 -0700245}
246
247/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500248static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700249{
Alex Elderbd406142012-01-23 15:49:27 -0600250 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700251
Alex Elder327800b2012-05-22 11:41:43 -0500252 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700253 con, con->state, sk->sk_state);
254
255 if (test_bit(CLOSED, &con->state))
256 return;
257
258 switch (sk->sk_state) {
259 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500260 dout("%s TCP_CLOSE\n", __func__);
Sage Weil31b80062009-10-06 11:31:13 -0700261 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500262 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500263 con_sock_state_closing(con);
Alex Elder928443c2012-05-22 11:41:43 -0500264 if (test_and_set_bit(SOCK_CLOSED, &con->flags) == 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700265 if (test_bit(CONNECTING, &con->state))
266 con->error_msg = "connection failed";
267 else
268 con->error_msg = "socket closed";
269 queue_con(con);
270 }
271 break;
272 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500273 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500274 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700275 queue_con(con);
276 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600277 default: /* Everything else is uninteresting */
278 break;
Sage Weil31b80062009-10-06 11:31:13 -0700279 }
280}
281
282/*
283 * set up socket callbacks
284 */
285static void set_sock_callbacks(struct socket *sock,
286 struct ceph_connection *con)
287{
288 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600289 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500290 sk->sk_data_ready = ceph_sock_data_ready;
291 sk->sk_write_space = ceph_sock_write_space;
292 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700293}
294
295
296/*
297 * socket helpers
298 */
299
300/*
301 * initiate connection to a remote socket.
302 */
Alex Elder41617d02012-02-14 14:05:33 -0600303static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700304{
Sage Weilf91d3472010-07-01 15:18:31 -0700305 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700306 struct socket *sock;
307 int ret;
308
309 BUG_ON(con->sock);
Sage Weilf91d3472010-07-01 15:18:31 -0700310 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
311 IPPROTO_TCP, &sock);
Sage Weil31b80062009-10-06 11:31:13 -0700312 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600313 return ret;
Sage Weil31b80062009-10-06 11:31:13 -0700314 sock->sk->sk_allocation = GFP_NOFS;
315
Sage Weila6a53492010-04-13 14:07:07 -0700316#ifdef CONFIG_LOCKDEP
317 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
318#endif
319
Sage Weil31b80062009-10-06 11:31:13 -0700320 set_sock_callbacks(sock, con);
321
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700322 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700323
Sage Weil89a86be2012-06-09 14:19:21 -0700324 con_sock_state_connecting(con);
Sage Weilf91d3472010-07-01 15:18:31 -0700325 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
326 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700327 if (ret == -EINPROGRESS) {
328 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700329 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700330 sock->sk->sk_state);
Alex Eldera5bc3122012-01-23 15:49:27 -0600331 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700332 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700333 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700334 sock_release(sock);
Sage Weil31b80062009-10-06 11:31:13 -0700335 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -0700336
Alex Elder41617d02012-02-14 14:05:33 -0600337 return ret;
Alex Eldera5bc3122012-01-23 15:49:27 -0600338 }
339 con->sock = sock;
Alex Elder41617d02012-02-14 14:05:33 -0600340 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700341}
342
343static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
344{
345 struct kvec iov = {buf, len};
346 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800347 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700348
Sage Weil98bdb0a2011-01-25 08:17:48 -0800349 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
350 if (r == -EAGAIN)
351 r = 0;
352 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700353}
354
355/*
356 * write something. @more is true if caller will be sending more data
357 * shortly.
358 */
359static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
360 size_t kvlen, size_t len, int more)
361{
362 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800363 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700364
365 if (more)
366 msg.msg_flags |= MSG_MORE;
367 else
368 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
369
Sage Weil42961d22011-01-25 08:19:34 -0800370 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
371 if (r == -EAGAIN)
372 r = 0;
373 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700374}
375
Alex Elder31739132012-03-07 11:40:08 -0600376static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
377 int offset, size_t size, int more)
378{
379 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
380 int ret;
381
382 ret = kernel_sendpage(sock, page, offset, size, flags);
383 if (ret == -EAGAIN)
384 ret = 0;
385
386 return ret;
387}
388
Sage Weil31b80062009-10-06 11:31:13 -0700389
390/*
391 * Shutdown/close the socket for the given connection.
392 */
393static int con_close_socket(struct ceph_connection *con)
394{
395 int rc;
396
397 dout("con_close_socket on %p sock %p\n", con, con->sock);
398 if (!con->sock)
399 return 0;
Alex Eldera8d00e32012-06-20 21:53:53 -0500400 set_bit(SOCK_CLOSED, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700401 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
402 sock_release(con->sock);
403 con->sock = NULL;
Alex Eldera8d00e32012-06-20 21:53:53 -0500404 clear_bit(SOCK_CLOSED, &con->flags);
Alex Elderce2c8902012-05-22 22:15:49 -0500405 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700406 return rc;
407}
408
409/*
410 * Reset a connection. Discard all incoming and outgoing messages
411 * and clear *_seq state.
412 */
413static void ceph_msg_remove(struct ceph_msg *msg)
414{
415 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500416 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -0700417 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -0500418 msg->con = NULL;
419
Sage Weil31b80062009-10-06 11:31:13 -0700420 ceph_msg_put(msg);
421}
422static void ceph_msg_remove_list(struct list_head *head)
423{
424 while (!list_empty(head)) {
425 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
426 list_head);
427 ceph_msg_remove(msg);
428 }
429}
430
431static void reset_connection(struct ceph_connection *con)
432{
433 /* reset connection, out_queue, msg_ and connect_seq */
434 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700435 ceph_msg_remove_list(&con->out_queue);
436 ceph_msg_remove_list(&con->out_sent);
437
Sage Weilcf3e5c42009-12-11 09:48:05 -0800438 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500439 BUG_ON(con->in_msg->con != con);
440 con->in_msg->con = NULL;
Sage Weilcf3e5c42009-12-11 09:48:05 -0800441 ceph_msg_put(con->in_msg);
442 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -0700443 con->ops->put(con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800444 }
445
Sage Weil31b80062009-10-06 11:31:13 -0700446 con->connect_seq = 0;
447 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800448 if (con->out_msg) {
449 ceph_msg_put(con->out_msg);
450 con->out_msg = NULL;
451 }
Sage Weil31b80062009-10-06 11:31:13 -0700452 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700453 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700454}
455
456/*
457 * mark a peer down. drop any open connections.
458 */
459void ceph_con_close(struct ceph_connection *con)
460{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700461 dout("con_close %p peer %s\n", con,
462 ceph_pr_addr(&con->peer_addr.in_addr));
Alex Eldera5988c42012-05-29 11:04:58 -0500463 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700464 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Alex Eldera5988c42012-05-29 11:04:58 -0500465 set_bit(CLOSED, &con->state);
466
Alex Elder928443c2012-05-22 11:41:43 -0500467 clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */
468 clear_bit(KEEPALIVE_PENDING, &con->flags);
469 clear_bit(WRITE_PENDING, &con->flags);
Alex Eldera5988c42012-05-29 11:04:58 -0500470
Sage Weilec302642009-12-22 10:43:42 -0800471 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700472 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700473 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800474 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800475 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700476 queue_con(con);
477}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700478EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700479
480/*
Sage Weil31b80062009-10-06 11:31:13 -0700481 * Reopen a closed connection, with a new peer address.
482 */
483void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
484{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700485 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700486 set_bit(OPENING, &con->state);
Alex Eldera5988c42012-05-29 11:04:58 -0500487 WARN_ON(!test_and_clear_bit(CLOSED, &con->state));
488
Sage Weil31b80062009-10-06 11:31:13 -0700489 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800490 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700491 queue_con(con);
492}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700493EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700494
495/*
Sage Weil87b315a2010-03-22 14:51:18 -0700496 * return true if this connection ever successfully opened
497 */
498bool ceph_con_opened(struct ceph_connection *con)
499{
500 return con->connect_seq > 0;
501}
502
503/*
Sage Weil31b80062009-10-06 11:31:13 -0700504 * initialize a new connection.
505 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500506void ceph_con_init(struct ceph_connection *con, void *private,
507 const struct ceph_connection_operations *ops,
508 struct ceph_messenger *msgr, __u8 entity_type, __u64 entity_num)
Sage Weil31b80062009-10-06 11:31:13 -0700509{
510 dout("con_init %p\n", con);
511 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500512 con->private = private;
513 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700514 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500515
516 con_sock_state_init(con);
517
Alex Elder1bfd89f2012-05-26 23:26:43 -0500518 con->peer_name.type = (__u8) entity_type;
519 con->peer_name.num = cpu_to_le64(entity_num);
520
Sage Weilec302642009-12-22 10:43:42 -0800521 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700522 INIT_LIST_HEAD(&con->out_queue);
523 INIT_LIST_HEAD(&con->out_sent);
524 INIT_DELAYED_WORK(&con->work, con_work);
Alex Eldera5988c42012-05-29 11:04:58 -0500525
526 set_bit(CLOSED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700527}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700528EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700529
530
531/*
532 * We maintain a global counter to order connection attempts. Get
533 * a unique seq greater than @gt.
534 */
535static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
536{
537 u32 ret;
538
539 spin_lock(&msgr->global_seq_lock);
540 if (msgr->global_seq < gt)
541 msgr->global_seq = gt;
542 ret = ++msgr->global_seq;
543 spin_unlock(&msgr->global_seq_lock);
544 return ret;
545}
546
Alex Eldere2200422012-05-23 14:35:23 -0500547static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600548{
549 con->out_kvec_left = 0;
550 con->out_kvec_bytes = 0;
551 con->out_kvec_cur = &con->out_kvec[0];
552}
553
Alex Eldere2200422012-05-23 14:35:23 -0500554static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600555 size_t size, void *data)
556{
557 int index;
558
559 index = con->out_kvec_left;
560 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
561
562 con->out_kvec[index].iov_len = size;
563 con->out_kvec[index].iov_base = data;
564 con->out_kvec_left++;
565 con->out_kvec_bytes += size;
566}
Sage Weil31b80062009-10-06 11:31:13 -0700567
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500568#ifdef CONFIG_BLOCK
569static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
570{
571 if (!bio) {
572 *iter = NULL;
573 *seg = 0;
574 return;
575 }
576 *iter = bio;
577 *seg = bio->bi_idx;
578}
579
580static void iter_bio_next(struct bio **bio_iter, int *seg)
581{
582 if (*bio_iter == NULL)
583 return;
584
585 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
586
587 (*seg)++;
588 if (*seg == (*bio_iter)->bi_vcnt)
589 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
590}
591#endif
592
Alex Elder739c9052012-06-11 14:57:13 -0500593static void prepare_write_message_data(struct ceph_connection *con)
594{
595 struct ceph_msg *msg = con->out_msg;
596
597 BUG_ON(!msg);
598 BUG_ON(!msg->hdr.data_len);
599
600 /* initialize page iterator */
601 con->out_msg_pos.page = 0;
602 if (msg->pages)
603 con->out_msg_pos.page_pos = msg->page_alignment;
604 else
605 con->out_msg_pos.page_pos = 0;
Alex Elder572c5882012-06-11 14:57:13 -0500606#ifdef CONFIG_BLOCK
Alex Elderabdaa6a2012-06-11 14:57:13 -0500607 if (msg->bio)
Alex Elder572c5882012-06-11 14:57:13 -0500608 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
609#endif
Alex Elder739c9052012-06-11 14:57:13 -0500610 con->out_msg_pos.data_pos = 0;
611 con->out_msg_pos.did_page_crc = false;
612 con->out_more = 1; /* data + footer will follow */
613}
614
Sage Weil31b80062009-10-06 11:31:13 -0700615/*
616 * Prepare footer for currently outgoing message, and finish things
617 * off. Assumes out_kvec* are already valid.. we just add on to the end.
618 */
Alex Elder859eb792012-02-14 14:05:33 -0600619static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700620{
621 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600622 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700623
Alex Elderfd154f32012-06-11 14:57:13 -0500624 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
625
Sage Weil31b80062009-10-06 11:31:13 -0700626 dout("prepare_write_message_footer %p\n", con);
627 con->out_kvec_is_msg = true;
628 con->out_kvec[v].iov_base = &m->footer;
629 con->out_kvec[v].iov_len = sizeof(m->footer);
630 con->out_kvec_bytes += sizeof(m->footer);
631 con->out_kvec_left++;
632 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800633 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700634}
635
636/*
637 * Prepare headers for the next outgoing message.
638 */
639static void prepare_write_message(struct ceph_connection *con)
640{
641 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600642 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700643
Alex Eldere2200422012-05-23 14:35:23 -0500644 con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700645 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800646 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700647
648 /* Sneak an ack in there first? If we can get it into the same
649 * TCP packet that's a good thing. */
650 if (con->in_seq > con->in_seq_acked) {
651 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -0500652 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700653 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500654 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600655 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700656 }
657
Alex Elder38941f82012-06-01 14:56:43 -0500658 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -0600659 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800660 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -0500661 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -0700662
663 /* put message on sent list */
664 ceph_msg_get(m);
665 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700666
Sage Weile84346b2010-05-11 21:20:38 -0700667 /*
668 * only assign outgoing seq # if we haven't sent this message
669 * yet. if it is requeued, resend with it's original seq.
670 */
671 if (m->needs_out_seq) {
672 m->hdr.seq = cpu_to_le64(++con->out_seq);
673 m->needs_out_seq = false;
674 }
Sage Weil31b80062009-10-06 11:31:13 -0700675
676 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
677 m, con->out_seq, le16_to_cpu(m->hdr.type),
678 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
679 le32_to_cpu(m->hdr.data_len),
680 m->nr_pages);
681 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
682
683 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -0500684 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
685 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
686 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600687
Sage Weil31b80062009-10-06 11:31:13 -0700688 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -0500689 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600690 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700691
692 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600693 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
694 con->out_msg->hdr.crc = cpu_to_le32(crc);
Alex Elderfd154f32012-06-11 14:57:13 -0500695 con->out_msg->footer.flags = 0;
Alex Eldera9a0c512012-02-15 07:43:54 -0600696
697 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
698 con->out_msg->footer.front_crc = cpu_to_le32(crc);
699 if (m->middle) {
700 crc = crc32c(0, m->middle->vec.iov_base,
701 m->middle->vec.iov_len);
702 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
703 } else
Sage Weil31b80062009-10-06 11:31:13 -0700704 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -0500705 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700706 le32_to_cpu(con->out_msg->footer.front_crc),
707 le32_to_cpu(con->out_msg->footer.middle_crc));
708
709 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -0500710 con->out_msg->footer.data_crc = 0;
711 if (m->hdr.data_len)
712 prepare_write_message_data(con);
713 else
Sage Weil31b80062009-10-06 11:31:13 -0700714 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600715 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700716
Alex Elder928443c2012-05-22 11:41:43 -0500717 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700718}
719
720/*
721 * Prepare an ack.
722 */
723static void prepare_write_ack(struct ceph_connection *con)
724{
725 dout("prepare_write_ack %p %llu -> %llu\n", con,
726 con->in_seq_acked, con->in_seq);
727 con->in_seq_acked = con->in_seq;
728
Alex Eldere2200422012-05-23 14:35:23 -0500729 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600730
Alex Eldere2200422012-05-23 14:35:23 -0500731 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600732
Sage Weil31b80062009-10-06 11:31:13 -0700733 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500734 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600735 &con->out_temp_ack);
736
Sage Weil31b80062009-10-06 11:31:13 -0700737 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elder928443c2012-05-22 11:41:43 -0500738 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700739}
740
741/*
742 * Prepare to write keepalive byte.
743 */
744static void prepare_write_keepalive(struct ceph_connection *con)
745{
746 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -0500747 con_out_kvec_reset(con);
748 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Alex Elder928443c2012-05-22 11:41:43 -0500749 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700750}
751
752/*
753 * Connection negotiation.
754 */
755
Alex Elderdac1e712012-05-16 15:16:39 -0500756static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
757 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800758{
Alex Eldera3530df2012-05-16 15:16:39 -0500759 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -0500760
761 if (!con->ops->get_authorizer) {
762 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
763 con->out_connect.authorizer_len = 0;
764
Alex Elder729796b2012-05-16 15:16:39 -0500765 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -0500766 }
767
768 /* Can't hold the mutex while getting authorizer */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800769
Sage Weilec302642009-12-22 10:43:42 -0800770 mutex_unlock(&con->mutex);
Alex Elderb1c6b982012-05-16 15:16:38 -0500771
Alex Elderdac1e712012-05-16 15:16:39 -0500772 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Elder8f43fb52012-05-16 15:16:39 -0500773
Sage Weilec302642009-12-22 10:43:42 -0800774 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800775
Alex Eldera3530df2012-05-16 15:16:39 -0500776 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -0500777 return auth;
Alex Elder928443c2012-05-22 11:41:43 -0500778 if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags))
Alex Elder729796b2012-05-16 15:16:39 -0500779 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700780
Alex Elder8f43fb52012-05-16 15:16:39 -0500781 con->auth_reply_buf = auth->authorizer_reply_buf;
782 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
783
Alex Elder859eb792012-02-14 14:05:33 -0600784
Alex Elder729796b2012-05-16 15:16:39 -0500785 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800786}
787
Sage Weil31b80062009-10-06 11:31:13 -0700788/*
789 * We connected to a peer and are saying hello.
790 */
Alex Eldere825a662012-05-16 15:16:38 -0500791static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700792{
Alex Eldere2200422012-05-23 14:35:23 -0500793 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
794 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -0500795 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800796
Sage Weileed0ef22009-11-10 14:34:36 -0800797 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500798 set_bit(WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800799}
800
Alex Eldere825a662012-05-16 15:16:38 -0500801static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800802{
Eric Dumazet95c96172012-04-15 05:58:06 +0000803 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -0700804 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -0500805 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -0500806 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700807
808 switch (con->peer_name.type) {
809 case CEPH_ENTITY_TYPE_MON:
810 proto = CEPH_MONC_PROTOCOL;
811 break;
812 case CEPH_ENTITY_TYPE_OSD:
813 proto = CEPH_OSDC_PROTOCOL;
814 break;
815 case CEPH_ENTITY_TYPE_MDS:
816 proto = CEPH_MDSC_PROTOCOL;
817 break;
818 default:
819 BUG();
820 }
821
822 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
823 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800824
Alex Eldere825a662012-05-16 15:16:38 -0500825 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700826 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
827 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
828 con->out_connect.global_seq = cpu_to_le32(global_seq);
829 con->out_connect.protocol_version = cpu_to_le32(proto);
830 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700831
Alex Elderdac1e712012-05-16 15:16:39 -0500832 auth_proto = CEPH_AUTH_UNKNOWN;
833 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -0500834 if (IS_ERR(auth))
835 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -0500836
Alex Elderdac1e712012-05-16 15:16:39 -0500837 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -0500838 con->out_connect.authorizer_len = auth ?
839 cpu_to_le32(auth->authorizer_buf_len) : 0;
840
Alex Eldere2200422012-05-23 14:35:23 -0500841 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -0500842 &con->out_connect);
843 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -0500844 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -0500845 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600846
Sage Weil31b80062009-10-06 11:31:13 -0700847 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500848 set_bit(WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800849
Alex Eldere10c7582012-05-16 15:16:38 -0500850 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700851}
852
Sage Weil31b80062009-10-06 11:31:13 -0700853/*
854 * write as much of pending kvecs to the socket as we can.
855 * 1 -> done
856 * 0 -> socket full, but more to do
857 * <0 -> error
858 */
859static int write_partial_kvec(struct ceph_connection *con)
860{
861 int ret;
862
863 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
864 while (con->out_kvec_bytes > 0) {
865 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
866 con->out_kvec_left, con->out_kvec_bytes,
867 con->out_more);
868 if (ret <= 0)
869 goto out;
870 con->out_kvec_bytes -= ret;
871 if (con->out_kvec_bytes == 0)
872 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600873
874 /* account for full iov entries consumed */
875 while (ret >= con->out_kvec_cur->iov_len) {
876 BUG_ON(!con->out_kvec_left);
877 ret -= con->out_kvec_cur->iov_len;
878 con->out_kvec_cur++;
879 con->out_kvec_left--;
880 }
881 /* and for a partially-consumed entry */
882 if (ret) {
883 con->out_kvec_cur->iov_len -= ret;
884 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700885 }
886 }
887 con->out_kvec_left = 0;
888 con->out_kvec_is_msg = false;
889 ret = 1;
890out:
891 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
892 con->out_kvec_bytes, con->out_kvec_left, ret);
893 return ret; /* done! */
894}
895
Alex Elder84ca8fc2012-06-11 14:57:13 -0500896static void out_msg_pos_next(struct ceph_connection *con, struct page *page,
897 size_t len, size_t sent, bool in_trail)
898{
899 struct ceph_msg *msg = con->out_msg;
900
901 BUG_ON(!msg);
902 BUG_ON(!sent);
903
904 con->out_msg_pos.data_pos += sent;
905 con->out_msg_pos.page_pos += sent;
906 if (sent == len) {
907 con->out_msg_pos.page_pos = 0;
908 con->out_msg_pos.page++;
909 con->out_msg_pos.did_page_crc = false;
910 if (in_trail)
911 list_move_tail(&page->lru,
912 &msg->trail->head);
913 else if (msg->pagelist)
914 list_move_tail(&page->lru,
915 &msg->pagelist->head);
916#ifdef CONFIG_BLOCK
917 else if (msg->bio)
918 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
919#endif
920 }
921}
922
Sage Weil31b80062009-10-06 11:31:13 -0700923/*
924 * Write as much message data payload as we can. If we finish, queue
925 * up the footer.
926 * 1 -> done, footer is now queued in out_kvec[].
927 * 0 -> socket full, but more to do
928 * <0 -> error
929 */
930static int write_partial_msg_pages(struct ceph_connection *con)
931{
932 struct ceph_msg *msg = con->out_msg;
Eric Dumazet95c96172012-04-15 05:58:06 +0000933 unsigned int data_len = le32_to_cpu(msg->hdr.data_len);
Sage Weil31b80062009-10-06 11:31:13 -0700934 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600935 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700936 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700937 int total_max_write;
Alex Elder84ca8fc2012-06-11 14:57:13 -0500938 bool in_trail = false;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700939 size_t trail_len = (msg->trail ? msg->trail->length : 0);
Sage Weil31b80062009-10-06 11:31:13 -0700940
941 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
Alex Elder84ca8fc2012-06-11 14:57:13 -0500942 con, msg, con->out_msg_pos.page, msg->nr_pages,
Sage Weil31b80062009-10-06 11:31:13 -0700943 con->out_msg_pos.page_pos);
944
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700945 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -0700946 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700947 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -0600948 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700949
950 total_max_write = data_len - trail_len -
951 con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -0700952
953 /*
954 * if we are calculating the data crc (the default), we need
955 * to map the page. if our pages[] has been revoked, use the
956 * zero page.
957 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700958
959 /* have we reached the trail part of the data? */
960 if (con->out_msg_pos.data_pos >= data_len - trail_len) {
Alex Elder84ca8fc2012-06-11 14:57:13 -0500961 in_trail = true;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700962
963 total_max_write = data_len - con->out_msg_pos.data_pos;
964
965 page = list_first_entry(&msg->trail->head,
966 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700967 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -0700968 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -0800969 } else if (msg->pagelist) {
970 page = list_first_entry(&msg->pagelist->head,
971 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700972#ifdef CONFIG_BLOCK
973 } else if (msg->bio) {
974 struct bio_vec *bv;
975
976 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
977 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -0600978 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700979 max_write = bv->bv_len;
980#endif
Sage Weil31b80062009-10-06 11:31:13 -0700981 } else {
Alex Elder57666512012-01-23 15:49:27 -0600982 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -0700983 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700984 len = min_t(int, max_write - con->out_msg_pos.page_pos,
985 total_max_write);
986
Alex Elder37675b02012-03-07 11:40:08 -0600987 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -0600988 void *base;
Alex Eldera9a0c512012-02-15 07:43:54 -0600989 u32 crc;
Alex Elder84ca8fc2012-06-11 14:57:13 -0500990 u32 tmpcrc = le32_to_cpu(msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -0600991 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -0700992
Alex Elder8d63e312012-03-07 11:40:08 -0600993 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -0700994 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -0600995 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Eldera9a0c512012-02-15 07:43:54 -0600996 crc = crc32c(tmpcrc, base, len);
Alex Elder84ca8fc2012-06-11 14:57:13 -0500997 msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -0600998 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -0700999 }
Alex Eldere36b13c2012-03-07 11:40:08 -06001000 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -06001001 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -06001002 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001003
Alex Elder0cdf9e62012-03-07 11:40:08 -06001004 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -07001005 kunmap(page);
1006
1007 if (ret <= 0)
1008 goto out;
1009
Alex Elder84ca8fc2012-06-11 14:57:13 -05001010 out_msg_pos_next(con, page, len, (size_t) ret, in_trail);
Sage Weil31b80062009-10-06 11:31:13 -07001011 }
1012
1013 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1014
1015 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001016 if (!do_datacrc)
Alex Elder84ca8fc2012-06-11 14:57:13 -05001017 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001018 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001019 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001020 ret = 1;
1021out:
1022 return ret;
1023}
1024
1025/*
1026 * write some zeros
1027 */
1028static int write_partial_skip(struct ceph_connection *con)
1029{
1030 int ret;
1031
1032 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001033 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001034
Alex Elder31739132012-03-07 11:40:08 -06001035 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001036 if (ret <= 0)
1037 goto out;
1038 con->out_skip -= ret;
1039 }
1040 ret = 1;
1041out:
1042 return ret;
1043}
1044
1045/*
1046 * Prepare to read connection handshake, or an ack.
1047 */
Sage Weileed0ef22009-11-10 14:34:36 -08001048static void prepare_read_banner(struct ceph_connection *con)
1049{
1050 dout("prepare_read_banner %p\n", con);
1051 con->in_base_pos = 0;
1052}
1053
Sage Weil31b80062009-10-06 11:31:13 -07001054static void prepare_read_connect(struct ceph_connection *con)
1055{
1056 dout("prepare_read_connect %p\n", con);
1057 con->in_base_pos = 0;
1058}
1059
1060static void prepare_read_ack(struct ceph_connection *con)
1061{
1062 dout("prepare_read_ack %p\n", con);
1063 con->in_base_pos = 0;
1064}
1065
1066static void prepare_read_tag(struct ceph_connection *con)
1067{
1068 dout("prepare_read_tag %p\n", con);
1069 con->in_base_pos = 0;
1070 con->in_tag = CEPH_MSGR_TAG_READY;
1071}
1072
1073/*
1074 * Prepare to read a message.
1075 */
1076static int prepare_read_message(struct ceph_connection *con)
1077{
1078 dout("prepare_read_message %p\n", con);
1079 BUG_ON(con->in_msg != NULL);
1080 con->in_base_pos = 0;
1081 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1082 return 0;
1083}
1084
1085
1086static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001087 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001088{
Alex Eldere6cee71f2012-05-10 10:29:50 -05001089 while (con->in_base_pos < end) {
1090 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001091 int have = size - left;
1092 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1093 if (ret <= 0)
1094 return ret;
1095 con->in_base_pos += ret;
1096 }
1097 return 1;
1098}
1099
1100
1101/*
1102 * Read all or part of the connect-side handshake on a new connection
1103 */
Sage Weileed0ef22009-11-10 14:34:36 -08001104static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001105{
Alex Elderfd516532012-05-10 10:29:50 -05001106 int size;
1107 int end;
1108 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001109
Sage Weileed0ef22009-11-10 14:34:36 -08001110 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001111
1112 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001113 size = strlen(CEPH_BANNER);
1114 end = size;
1115 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001116 if (ret <= 0)
1117 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001118
1119 size = sizeof (con->actual_peer_addr);
1120 end += size;
1121 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001122 if (ret <= 0)
1123 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001124
1125 size = sizeof (con->peer_addr_for_me);
1126 end += size;
1127 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001128 if (ret <= 0)
1129 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001130
Sage Weileed0ef22009-11-10 14:34:36 -08001131out:
1132 return ret;
1133}
1134
1135static int read_partial_connect(struct ceph_connection *con)
1136{
Alex Elderfd516532012-05-10 10:29:50 -05001137 int size;
1138 int end;
1139 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001140
1141 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1142
Alex Elderfd516532012-05-10 10:29:50 -05001143 size = sizeof (con->in_reply);
1144 end = size;
1145 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001146 if (ret <= 0)
1147 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001148
1149 size = le32_to_cpu(con->in_reply.authorizer_len);
1150 end += size;
1151 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001152 if (ret <= 0)
1153 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001154
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001155 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1156 con, (int)con->in_reply.tag,
1157 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001158 le32_to_cpu(con->in_reply.global_seq));
1159out:
1160 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001161
Sage Weil31b80062009-10-06 11:31:13 -07001162}
1163
1164/*
1165 * Verify the hello banner looks okay.
1166 */
1167static int verify_hello(struct ceph_connection *con)
1168{
1169 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001170 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001171 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001172 con->error_msg = "protocol error, bad banner";
1173 return -1;
1174 }
1175 return 0;
1176}
1177
1178static bool addr_is_blank(struct sockaddr_storage *ss)
1179{
1180 switch (ss->ss_family) {
1181 case AF_INET:
1182 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1183 case AF_INET6:
1184 return
1185 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1186 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1187 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1188 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1189 }
1190 return false;
1191}
1192
1193static int addr_port(struct sockaddr_storage *ss)
1194{
1195 switch (ss->ss_family) {
1196 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001197 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001198 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001199 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001200 }
1201 return 0;
1202}
1203
1204static void addr_set_port(struct sockaddr_storage *ss, int p)
1205{
1206 switch (ss->ss_family) {
1207 case AF_INET:
1208 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001209 break;
Sage Weil31b80062009-10-06 11:31:13 -07001210 case AF_INET6:
1211 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001212 break;
Sage Weil31b80062009-10-06 11:31:13 -07001213 }
1214}
1215
1216/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001217 * Unlike other *_pton function semantics, zero indicates success.
1218 */
1219static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1220 char delim, const char **ipend)
1221{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001222 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1223 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001224
1225 memset(ss, 0, sizeof(*ss));
1226
1227 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1228 ss->ss_family = AF_INET;
1229 return 0;
1230 }
1231
1232 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1233 ss->ss_family = AF_INET6;
1234 return 0;
1235 }
1236
1237 return -EINVAL;
1238}
1239
1240/*
1241 * Extract hostname string and resolve using kernel DNS facility.
1242 */
1243#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1244static int ceph_dns_resolve_name(const char *name, size_t namelen,
1245 struct sockaddr_storage *ss, char delim, const char **ipend)
1246{
1247 const char *end, *delim_p;
1248 char *colon_p, *ip_addr = NULL;
1249 int ip_len, ret;
1250
1251 /*
1252 * The end of the hostname occurs immediately preceding the delimiter or
1253 * the port marker (':') where the delimiter takes precedence.
1254 */
1255 delim_p = memchr(name, delim, namelen);
1256 colon_p = memchr(name, ':', namelen);
1257
1258 if (delim_p && colon_p)
1259 end = delim_p < colon_p ? delim_p : colon_p;
1260 else if (!delim_p && colon_p)
1261 end = colon_p;
1262 else {
1263 end = delim_p;
1264 if (!end) /* case: hostname:/ */
1265 end = name + namelen;
1266 }
1267
1268 if (end <= name)
1269 return -EINVAL;
1270
1271 /* do dns_resolve upcall */
1272 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1273 if (ip_len > 0)
1274 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1275 else
1276 ret = -ESRCH;
1277
1278 kfree(ip_addr);
1279
1280 *ipend = end;
1281
1282 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1283 ret, ret ? "failed" : ceph_pr_addr(ss));
1284
1285 return ret;
1286}
1287#else
1288static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1289 struct sockaddr_storage *ss, char delim, const char **ipend)
1290{
1291 return -EINVAL;
1292}
1293#endif
1294
1295/*
1296 * Parse a server name (IP or hostname). If a valid IP address is not found
1297 * then try to extract a hostname to resolve using userspace DNS upcall.
1298 */
1299static int ceph_parse_server_name(const char *name, size_t namelen,
1300 struct sockaddr_storage *ss, char delim, const char **ipend)
1301{
1302 int ret;
1303
1304 ret = ceph_pton(name, namelen, ss, delim, ipend);
1305 if (ret)
1306 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1307
1308 return ret;
1309}
1310
1311/*
Sage Weil31b80062009-10-06 11:31:13 -07001312 * Parse an ip[:port] list into an addr array. Use the default
1313 * monitor port if a port isn't specified.
1314 */
1315int ceph_parse_ips(const char *c, const char *end,
1316 struct ceph_entity_addr *addr,
1317 int max_count, int *count)
1318{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001319 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001320 const char *p = c;
1321
1322 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1323 for (i = 0; i < max_count; i++) {
1324 const char *ipend;
1325 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001326 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001327 char delim = ',';
1328
1329 if (*p == '[') {
1330 delim = ']';
1331 p++;
1332 }
Sage Weil31b80062009-10-06 11:31:13 -07001333
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001334 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1335 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001336 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001337 ret = -EINVAL;
1338
Sage Weil31b80062009-10-06 11:31:13 -07001339 p = ipend;
1340
Sage Weil39139f62010-07-08 09:54:52 -07001341 if (delim == ']') {
1342 if (*p != ']') {
1343 dout("missing matching ']'\n");
1344 goto bad;
1345 }
1346 p++;
1347 }
1348
Sage Weil31b80062009-10-06 11:31:13 -07001349 /* port? */
1350 if (p < end && *p == ':') {
1351 port = 0;
1352 p++;
1353 while (p < end && *p >= '0' && *p <= '9') {
1354 port = (port * 10) + (*p - '0');
1355 p++;
1356 }
1357 if (port > 65535 || port == 0)
1358 goto bad;
1359 } else {
1360 port = CEPH_MON_PORT;
1361 }
1362
1363 addr_set_port(ss, port);
1364
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001365 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001366
1367 if (p == end)
1368 break;
1369 if (*p != ',')
1370 goto bad;
1371 p++;
1372 }
1373
1374 if (p != end)
1375 goto bad;
1376
1377 if (count)
1378 *count = i + 1;
1379 return 0;
1380
1381bad:
Sage Weil39139f62010-07-08 09:54:52 -07001382 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001383 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001384}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001385EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001386
Sage Weileed0ef22009-11-10 14:34:36 -08001387static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001388{
Sage Weileed0ef22009-11-10 14:34:36 -08001389 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001390
1391 if (verify_hello(con) < 0)
1392 return -1;
1393
Sage Weil63f2d212009-11-03 15:17:56 -08001394 ceph_decode_addr(&con->actual_peer_addr);
1395 ceph_decode_addr(&con->peer_addr_for_me);
1396
Sage Weil31b80062009-10-06 11:31:13 -07001397 /*
1398 * Make sure the other end is who we wanted. note that the other
1399 * end may not yet know their ip address, so if it's 0.0.0.0, give
1400 * them the benefit of the doubt.
1401 */
Sage Weil103e2d32010-01-07 16:12:36 -08001402 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1403 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001404 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1405 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001406 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001407 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001408 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001409 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001410 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001411 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001412 return -1;
1413 }
1414
1415 /*
1416 * did we learn our address?
1417 */
1418 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1419 int port = addr_port(&con->msgr->inst.addr.in_addr);
1420
1421 memcpy(&con->msgr->inst.addr.in_addr,
1422 &con->peer_addr_for_me.in_addr,
1423 sizeof(con->peer_addr_for_me.in_addr));
1424 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001425 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001426 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001427 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001428 }
1429
Sage Weileed0ef22009-11-10 14:34:36 -08001430 set_bit(NEGOTIATING, &con->state);
1431 prepare_read_connect(con);
1432 return 0;
1433}
1434
Sage Weil04a419f2009-12-23 09:30:21 -08001435static void fail_protocol(struct ceph_connection *con)
1436{
1437 reset_connection(con);
1438 set_bit(CLOSED, &con->state); /* in case there's queued work */
Sage Weil04a419f2009-12-23 09:30:21 -08001439}
1440
Sage Weileed0ef22009-11-10 14:34:36 -08001441static int process_connect(struct ceph_connection *con)
1442{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001443 u64 sup_feat = con->msgr->supported_features;
1444 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001445 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001446 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001447
Sage Weileed0ef22009-11-10 14:34:36 -08001448 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1449
Sage Weil31b80062009-10-06 11:31:13 -07001450 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001451 case CEPH_MSGR_TAG_FEATURES:
1452 pr_err("%s%lld %s feature set mismatch,"
1453 " my %llx < server's %llx, missing %llx\n",
1454 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001455 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001456 sup_feat, server_feat, server_feat & ~sup_feat);
1457 con->error_msg = "missing required protocol features";
1458 fail_protocol(con);
1459 return -1;
1460
Sage Weil31b80062009-10-06 11:31:13 -07001461 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001462 pr_err("%s%lld %s protocol version mismatch,"
1463 " my %d != server's %d\n",
1464 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001465 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001466 le32_to_cpu(con->out_connect.protocol_version),
1467 le32_to_cpu(con->in_reply.protocol_version));
1468 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001469 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001470 return -1;
1471
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001472 case CEPH_MSGR_TAG_BADAUTHORIZER:
1473 con->auth_retry++;
1474 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1475 con->auth_retry);
1476 if (con->auth_retry == 2) {
1477 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001478 return -1;
1479 }
1480 con->auth_retry = 1;
Alex Eldere2200422012-05-23 14:35:23 -05001481 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001482 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001483 if (ret < 0)
1484 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001485 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001486 break;
Sage Weil31b80062009-10-06 11:31:13 -07001487
1488 case CEPH_MSGR_TAG_RESETSESSION:
1489 /*
1490 * If we connected with a large connect_seq but the peer
1491 * has no record of a session with us (no connection, or
1492 * connect_seq == 0), they will send RESETSESION to indicate
1493 * that they must have reset their session, and may have
1494 * dropped messages.
1495 */
1496 dout("process_connect got RESET peer seq %u\n",
1497 le32_to_cpu(con->in_connect.connect_seq));
1498 pr_err("%s%lld %s connection reset\n",
1499 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001500 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001501 reset_connection(con);
Alex Eldere2200422012-05-23 14:35:23 -05001502 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001503 ret = prepare_write_connect(con);
1504 if (ret < 0)
1505 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001506 prepare_read_connect(con);
1507
1508 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001509 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001510 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1511 if (con->ops->peer_reset)
1512 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001513 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07001514 if (test_bit(CLOSED, &con->state) ||
1515 test_bit(OPENING, &con->state))
1516 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001517 break;
1518
1519 case CEPH_MSGR_TAG_RETRY_SESSION:
1520 /*
1521 * If we sent a smaller connect_seq than the peer has, try
1522 * again with a larger value.
1523 */
1524 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1525 le32_to_cpu(con->out_connect.connect_seq),
1526 le32_to_cpu(con->in_connect.connect_seq));
1527 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Alex Eldere2200422012-05-23 14:35:23 -05001528 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001529 ret = prepare_write_connect(con);
1530 if (ret < 0)
1531 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001532 prepare_read_connect(con);
1533 break;
1534
1535 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1536 /*
1537 * If we sent a smaller global_seq than the peer has, try
1538 * again with a larger value.
1539 */
Sage Weileed0ef22009-11-10 14:34:36 -08001540 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001541 con->peer_global_seq,
1542 le32_to_cpu(con->in_connect.global_seq));
1543 get_global_seq(con->msgr,
1544 le32_to_cpu(con->in_connect.global_seq));
Alex Eldere2200422012-05-23 14:35:23 -05001545 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001546 ret = prepare_write_connect(con);
1547 if (ret < 0)
1548 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001549 prepare_read_connect(con);
1550 break;
1551
1552 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001553 if (req_feat & ~server_feat) {
1554 pr_err("%s%lld %s protocol feature mismatch,"
1555 " my required %llx > server's %llx, need %llx\n",
1556 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001557 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001558 req_feat, server_feat, req_feat & ~server_feat);
1559 con->error_msg = "missing required protocol features";
1560 fail_protocol(con);
1561 return -1;
1562 }
Sage Weil31b80062009-10-06 11:31:13 -07001563 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001564 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1565 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001566 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001567 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1568 con->peer_global_seq,
1569 le32_to_cpu(con->in_reply.connect_seq),
1570 con->connect_seq);
1571 WARN_ON(con->connect_seq !=
1572 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001573
1574 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elder928443c2012-05-22 11:41:43 -05001575 set_bit(LOSSYTX, &con->flags);
Sage Weil92ac41d2009-12-14 14:56:56 -08001576
Sage Weil31b80062009-10-06 11:31:13 -07001577 prepare_read_tag(con);
1578 break;
1579
1580 case CEPH_MSGR_TAG_WAIT:
1581 /*
1582 * If there is a connection race (we are opening
1583 * connections to each other), one of us may just have
1584 * to WAIT. This shouldn't happen if we are the
1585 * client.
1586 */
Sage Weil04177882011-05-12 15:33:17 -07001587 pr_err("process_connect got WAIT as client\n");
1588 con->error_msg = "protocol error, got WAIT as client";
1589 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001590
1591 default:
1592 pr_err("connect protocol error, will retry\n");
1593 con->error_msg = "protocol error, garbage tag during connect";
1594 return -1;
1595 }
1596 return 0;
1597}
1598
1599
1600/*
1601 * read (part of) an ack
1602 */
1603static int read_partial_ack(struct ceph_connection *con)
1604{
Alex Elderfd516532012-05-10 10:29:50 -05001605 int size = sizeof (con->in_temp_ack);
1606 int end = size;
1607
1608 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001609}
1610
1611
1612/*
1613 * We can finally discard anything that's been acked.
1614 */
1615static void process_ack(struct ceph_connection *con)
1616{
1617 struct ceph_msg *m;
1618 u64 ack = le64_to_cpu(con->in_temp_ack);
1619 u64 seq;
1620
Sage Weil31b80062009-10-06 11:31:13 -07001621 while (!list_empty(&con->out_sent)) {
1622 m = list_first_entry(&con->out_sent, struct ceph_msg,
1623 list_head);
1624 seq = le64_to_cpu(m->hdr.seq);
1625 if (seq > ack)
1626 break;
1627 dout("got ack for seq %llu type %d at %p\n", seq,
1628 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001629 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001630 ceph_msg_remove(m);
1631 }
Sage Weil31b80062009-10-06 11:31:13 -07001632 prepare_read_tag(con);
1633}
1634
1635
1636
1637
Yehuda Sadeh24504182010-01-08 13:58:34 -08001638static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001639 struct kvec *section,
1640 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001641{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001642 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001643
Yehuda Sadeh24504182010-01-08 13:58:34 -08001644 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001645
Yehuda Sadeh24504182010-01-08 13:58:34 -08001646 while (section->iov_len < sec_len) {
1647 BUG_ON(section->iov_base == NULL);
1648 left = sec_len - section->iov_len;
1649 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1650 section->iov_len, left);
1651 if (ret <= 0)
1652 return ret;
1653 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001654 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001655 if (section->iov_len == sec_len)
1656 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001657
1658 return 1;
1659}
1660
Alex Elder1c20f2d2012-06-04 14:43:32 -05001661static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
1662 struct ceph_msg_header *hdr);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001663
1664
1665static int read_partial_message_pages(struct ceph_connection *con,
1666 struct page **pages,
Eric Dumazet95c96172012-04-15 05:58:06 +00001667 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001668{
1669 void *p;
1670 int ret;
1671 int left;
1672
1673 left = min((int)(data_len - con->in_msg_pos.data_pos),
1674 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1675 /* (page) data */
1676 BUG_ON(pages == NULL);
1677 p = kmap(pages[con->in_msg_pos.page]);
1678 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1679 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001680 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001681 con->in_data_crc =
1682 crc32c(con->in_data_crc,
1683 p + con->in_msg_pos.page_pos, ret);
1684 kunmap(pages[con->in_msg_pos.page]);
1685 if (ret <= 0)
1686 return ret;
1687 con->in_msg_pos.data_pos += ret;
1688 con->in_msg_pos.page_pos += ret;
1689 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1690 con->in_msg_pos.page_pos = 0;
1691 con->in_msg_pos.page++;
1692 }
1693
1694 return ret;
1695}
1696
1697#ifdef CONFIG_BLOCK
1698static int read_partial_message_bio(struct ceph_connection *con,
1699 struct bio **bio_iter, int *bio_seg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001700 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001701{
1702 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1703 void *p;
1704 int ret, left;
1705
1706 if (IS_ERR(bv))
1707 return PTR_ERR(bv);
1708
1709 left = min((int)(data_len - con->in_msg_pos.data_pos),
1710 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1711
1712 p = kmap(bv->bv_page) + bv->bv_offset;
1713
1714 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1715 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001716 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001717 con->in_data_crc =
1718 crc32c(con->in_data_crc,
1719 p + con->in_msg_pos.page_pos, ret);
1720 kunmap(bv->bv_page);
1721 if (ret <= 0)
1722 return ret;
1723 con->in_msg_pos.data_pos += ret;
1724 con->in_msg_pos.page_pos += ret;
1725 if (con->in_msg_pos.page_pos == bv->bv_len) {
1726 con->in_msg_pos.page_pos = 0;
1727 iter_bio_next(bio_iter, bio_seg);
1728 }
1729
1730 return ret;
1731}
1732#endif
1733
Sage Weil31b80062009-10-06 11:31:13 -07001734/*
1735 * read (part of) a message.
1736 */
1737static int read_partial_message(struct ceph_connection *con)
1738{
1739 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05001740 int size;
1741 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001742 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00001743 unsigned int front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001744 bool do_datacrc = !con->msgr->nocrc;
Sage Weilae187562010-04-22 07:47:01 -07001745 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001746 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001747
1748 dout("read_partial_message con %p msg %p\n", con, m);
1749
1750 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05001751 size = sizeof (con->in_hdr);
1752 end = size;
1753 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05001754 if (ret <= 0)
1755 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001756
1757 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1758 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1759 pr_err("read_partial_message bad hdr "
1760 " crc %u != expected %u\n",
1761 crc, con->in_hdr.crc);
1762 return -EBADMSG;
1763 }
1764
Sage Weil31b80062009-10-06 11:31:13 -07001765 front_len = le32_to_cpu(con->in_hdr.front_len);
1766 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1767 return -EIO;
1768 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1769 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1770 return -EIO;
1771 data_len = le32_to_cpu(con->in_hdr.data_len);
1772 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1773 return -EIO;
1774
Sage Weilae187562010-04-22 07:47:01 -07001775 /* verify seq# */
1776 seq = le64_to_cpu(con->in_hdr.seq);
1777 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001778 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001779 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001780 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001781 seq, con->in_seq + 1);
1782 con->in_base_pos = -front_len - middle_len - data_len -
1783 sizeof(m->footer);
1784 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001785 return 0;
1786 } else if ((s64)seq - (s64)con->in_seq > 1) {
1787 pr_err("read_partial_message bad seq %lld expected %lld\n",
1788 seq, con->in_seq + 1);
1789 con->error_msg = "bad message sequence # for incoming message";
1790 return -EBADMSG;
1791 }
1792
Sage Weil31b80062009-10-06 11:31:13 -07001793 /* allocate message? */
1794 if (!con->in_msg) {
1795 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1796 con->in_hdr.front_len, con->in_hdr.data_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05001797 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
Sage Weil31b80062009-10-06 11:31:13 -07001798 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001799 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001800 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001801 con->in_base_pos = -front_len - middle_len - data_len -
1802 sizeof(m->footer);
1803 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001804 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001805 return 0;
1806 }
Sage Weila79832f2010-04-01 16:06:19 -07001807 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001808 con->error_msg =
1809 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001810 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001811 }
Alex Elder38941f82012-06-01 14:56:43 -05001812
1813 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07001814 m = con->in_msg;
1815 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001816 if (m->middle)
1817 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001818
1819 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001820 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001821 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001822 else
1823 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001824 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001825 }
1826
1827 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001828 ret = read_partial_message_section(con, &m->front, front_len,
1829 &con->in_front_crc);
1830 if (ret <= 0)
1831 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001832
1833 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001834 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001835 ret = read_partial_message_section(con, &m->middle->vec,
1836 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001837 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001838 if (ret <= 0)
1839 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001840 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001841#ifdef CONFIG_BLOCK
1842 if (m->bio && !m->bio_iter)
1843 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1844#endif
Sage Weil31b80062009-10-06 11:31:13 -07001845
1846 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001847 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001848 if (m->pages) {
1849 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001850 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001851 if (ret <= 0)
1852 return ret;
1853#ifdef CONFIG_BLOCK
1854 } else if (m->bio) {
1855
1856 ret = read_partial_message_bio(con,
1857 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001858 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001859 if (ret <= 0)
1860 return ret;
1861#endif
1862 } else {
1863 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001864 }
1865 }
1866
Sage Weil31b80062009-10-06 11:31:13 -07001867 /* footer */
Alex Elderfd516532012-05-10 10:29:50 -05001868 size = sizeof (m->footer);
1869 end += size;
1870 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05001871 if (ret <= 0)
1872 return ret;
1873
Sage Weil31b80062009-10-06 11:31:13 -07001874 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1875 m, front_len, m->footer.front_crc, middle_len,
1876 m->footer.middle_crc, data_len, m->footer.data_crc);
1877
1878 /* crc ok? */
1879 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1880 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1881 m, con->in_front_crc, m->footer.front_crc);
1882 return -EBADMSG;
1883 }
1884 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1885 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1886 m, con->in_middle_crc, m->footer.middle_crc);
1887 return -EBADMSG;
1888 }
Alex Elderbca064d2012-02-15 07:43:54 -06001889 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001890 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1891 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1892 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1893 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1894 return -EBADMSG;
1895 }
1896
1897 return 1; /* done! */
1898}
1899
1900/*
1901 * Process message. This happens in the worker thread. The callback should
1902 * be careful not to do anything that waits on other incoming messages or it
1903 * may deadlock.
1904 */
1905static void process_message(struct ceph_connection *con)
1906{
Sage Weil5e095e82009-12-14 14:30:34 -08001907 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001908
Alex Elder38941f82012-06-01 14:56:43 -05001909 BUG_ON(con->in_msg->con != con);
1910 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08001911 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001912 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07001913 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07001914
1915 /* if first message, set peer_name */
1916 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001917 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001918
Sage Weil31b80062009-10-06 11:31:13 -07001919 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001920 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001921
1922 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1923 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001924 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001925 le16_to_cpu(msg->hdr.type),
1926 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1927 le32_to_cpu(msg->hdr.front_len),
1928 le32_to_cpu(msg->hdr.data_len),
1929 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1930 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001931
1932 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001933 prepare_read_tag(con);
1934}
1935
1936
1937/*
1938 * Write something to the socket. Called in a worker thread when the
1939 * socket appears to be writeable and we have something ready to send.
1940 */
1941static int try_write(struct ceph_connection *con)
1942{
Sage Weil31b80062009-10-06 11:31:13 -07001943 int ret = 1;
1944
Sage Weild59315c2012-06-21 12:49:23 -07001945 dout("try_write start %p state %lu\n", con, con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001946
Sage Weil31b80062009-10-06 11:31:13 -07001947more:
1948 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1949
1950 /* open the socket first? */
1951 if (con->sock == NULL) {
Alex Eldera5988c42012-05-29 11:04:58 -05001952 clear_bit(NEGOTIATING, &con->state);
1953 set_bit(CONNECTING, &con->state);
1954
Alex Eldere2200422012-05-23 14:35:23 -05001955 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001956 prepare_write_banner(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001957 ret = prepare_write_connect(con);
1958 if (ret < 0)
1959 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08001960 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001961
Sage Weilcf3e5c42009-12-11 09:48:05 -08001962 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001963 con->in_tag = CEPH_MSGR_TAG_READY;
1964 dout("try_write initiating connect on %p new state %lu\n",
1965 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06001966 ret = ceph_tcp_connect(con);
1967 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07001968 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07001969 goto out;
1970 }
1971 }
1972
1973more_kvec:
1974 /* kvec data queued? */
1975 if (con->out_skip) {
1976 ret = write_partial_skip(con);
1977 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001978 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001979 }
1980 if (con->out_kvec_left) {
1981 ret = write_partial_kvec(con);
1982 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001983 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001984 }
1985
1986 /* msg pages? */
1987 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001988 if (con->out_msg_done) {
1989 ceph_msg_put(con->out_msg);
1990 con->out_msg = NULL; /* we're done with this one */
1991 goto do_next;
1992 }
1993
Sage Weil31b80062009-10-06 11:31:13 -07001994 ret = write_partial_msg_pages(con);
1995 if (ret == 1)
1996 goto more_kvec; /* we need to send the footer, too! */
1997 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08001998 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001999 if (ret < 0) {
2000 dout("try_write write_partial_msg_pages err %d\n",
2001 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002002 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002003 }
2004 }
2005
Sage Weilc86a2932009-12-14 14:04:30 -08002006do_next:
Sage Weil31b80062009-10-06 11:31:13 -07002007 if (!test_bit(CONNECTING, &con->state)) {
2008 /* is anything else pending? */
2009 if (!list_empty(&con->out_queue)) {
2010 prepare_write_message(con);
2011 goto more;
2012 }
2013 if (con->in_seq > con->in_seq_acked) {
2014 prepare_write_ack(con);
2015 goto more;
2016 }
Alex Elder928443c2012-05-22 11:41:43 -05002017 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002018 prepare_write_keepalive(con);
2019 goto more;
2020 }
2021 }
2022
2023 /* Nothing to do! */
Alex Elder928443c2012-05-22 11:41:43 -05002024 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002025 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002026 ret = 0;
2027out:
Sage Weil42961d22011-01-25 08:19:34 -08002028 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002029 return ret;
2030}
2031
2032
2033
2034/*
2035 * Read what we can from the socket.
2036 */
2037static int try_read(struct ceph_connection *con)
2038{
Sage Weil31b80062009-10-06 11:31:13 -07002039 int ret = -1;
2040
2041 if (!con->sock)
2042 return 0;
2043
2044 if (test_bit(STANDBY, &con->state))
2045 return 0;
2046
2047 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08002048
Sage Weil31b80062009-10-06 11:31:13 -07002049more:
2050 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2051 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002052
2053 /*
2054 * process_connect and process_message drop and re-take
2055 * con->mutex. make sure we handle a racing close or reopen.
2056 */
2057 if (test_bit(CLOSED, &con->state) ||
2058 test_bit(OPENING, &con->state)) {
2059 ret = -EAGAIN;
2060 goto out;
2061 }
2062
Sage Weil31b80062009-10-06 11:31:13 -07002063 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08002064 if (!test_bit(NEGOTIATING, &con->state)) {
2065 dout("try_read connecting\n");
2066 ret = read_partial_banner(con);
2067 if (ret <= 0)
Sage Weileed0ef22009-11-10 14:34:36 -08002068 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002069 ret = process_banner(con);
2070 if (ret < 0)
2071 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08002072 }
Sage Weil31b80062009-10-06 11:31:13 -07002073 ret = read_partial_connect(con);
2074 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002075 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002076 ret = process_connect(con);
2077 if (ret < 0)
2078 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002079 goto more;
2080 }
2081
2082 if (con->in_base_pos < 0) {
2083 /*
2084 * skipping + discarding content.
2085 *
2086 * FIXME: there must be a better way to do this!
2087 */
Alex Elder84495f42012-02-15 07:43:55 -06002088 static char buf[SKIP_BUF_SIZE];
2089 int skip = min((int) sizeof (buf), -con->in_base_pos);
2090
Sage Weil31b80062009-10-06 11:31:13 -07002091 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2092 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2093 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002094 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002095 con->in_base_pos += ret;
2096 if (con->in_base_pos)
2097 goto more;
2098 }
2099 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2100 /*
2101 * what's next?
2102 */
2103 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2104 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002105 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002106 dout("try_read got tag %d\n", (int)con->in_tag);
2107 switch (con->in_tag) {
2108 case CEPH_MSGR_TAG_MSG:
2109 prepare_read_message(con);
2110 break;
2111 case CEPH_MSGR_TAG_ACK:
2112 prepare_read_ack(con);
2113 break;
2114 case CEPH_MSGR_TAG_CLOSE:
2115 set_bit(CLOSED, &con->state); /* fixme */
Sage Weil98bdb0a2011-01-25 08:17:48 -08002116 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002117 default:
2118 goto bad_tag;
2119 }
2120 }
2121 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2122 ret = read_partial_message(con);
2123 if (ret <= 0) {
2124 switch (ret) {
2125 case -EBADMSG:
2126 con->error_msg = "bad crc";
2127 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002128 break;
Sage Weil31b80062009-10-06 11:31:13 -07002129 case -EIO:
2130 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002131 break;
Sage Weil31b80062009-10-06 11:31:13 -07002132 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002133 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002134 }
2135 if (con->in_tag == CEPH_MSGR_TAG_READY)
2136 goto more;
2137 process_message(con);
2138 goto more;
2139 }
2140 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2141 ret = read_partial_ack(con);
2142 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002143 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002144 process_ack(con);
2145 goto more;
2146 }
2147
Sage Weil31b80062009-10-06 11:31:13 -07002148out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002149 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002150 return ret;
2151
2152bad_tag:
2153 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2154 con->error_msg = "protocol error, garbage tag";
2155 ret = -1;
2156 goto out;
2157}
2158
2159
2160/*
2161 * Atomically queue work on a connection. Bump @con reference to
2162 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002163 */
2164static void queue_con(struct ceph_connection *con)
2165{
Sage Weil31b80062009-10-06 11:31:13 -07002166 if (!con->ops->get(con)) {
2167 dout("queue_con %p ref count 0\n", con);
2168 return;
2169 }
2170
Tejun Heof363e45fd12011-01-03 14:49:46 +01002171 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002172 dout("queue_con %p - already queued\n", con);
2173 con->ops->put(con);
2174 } else {
2175 dout("queue_con %p\n", con);
2176 }
2177}
2178
2179/*
2180 * Do some work on a connection. Drop a connection ref when we're done.
2181 */
2182static void con_work(struct work_struct *work)
2183{
2184 struct ceph_connection *con = container_of(work, struct ceph_connection,
2185 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002186 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002187
Sage Weil9dd46582010-04-28 13:51:50 -07002188 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002189restart:
Alex Elder928443c2012-05-22 11:41:43 -05002190 if (test_and_clear_bit(BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002191 dout("con_work %p backing off\n", con);
2192 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2193 round_jiffies_relative(con->delay))) {
2194 dout("con_work %p backoff %lu\n", con, con->delay);
2195 mutex_unlock(&con->mutex);
2196 return;
2197 } else {
2198 con->ops->put(con);
2199 dout("con_work %p FAILED to back off %lu\n", con,
2200 con->delay);
2201 }
2202 }
Sage Weil9dd46582010-04-28 13:51:50 -07002203
Sage Weile00de342011-03-04 12:25:05 -08002204 if (test_bit(STANDBY, &con->state)) {
2205 dout("con_work %p STANDBY\n", con);
2206 goto done;
2207 }
Sage Weil31b80062009-10-06 11:31:13 -07002208 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2209 dout("con_work CLOSED\n");
2210 con_close_socket(con);
2211 goto done;
2212 }
2213 if (test_and_clear_bit(OPENING, &con->state)) {
2214 /* reopen w/ new peer */
2215 dout("con_work OPENING\n");
2216 con_close_socket(con);
2217 }
2218
Alex Elder928443c2012-05-22 11:41:43 -05002219 if (test_and_clear_bit(SOCK_CLOSED, &con->flags))
Sage Weil0da5d702011-05-19 11:21:05 -07002220 goto fault;
2221
2222 ret = try_read(con);
2223 if (ret == -EAGAIN)
2224 goto restart;
2225 if (ret < 0)
2226 goto fault;
2227
2228 ret = try_write(con);
2229 if (ret == -EAGAIN)
2230 goto restart;
2231 if (ret < 0)
2232 goto fault;
Sage Weil31b80062009-10-06 11:31:13 -07002233
2234done:
Sage Weil9dd46582010-04-28 13:51:50 -07002235 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002236done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002237 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002238 return;
2239
2240fault:
2241 mutex_unlock(&con->mutex);
2242 ceph_fault(con); /* error/fault path */
2243 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002244}
2245
2246
2247/*
2248 * Generic error/fault handler. A retry mechanism is used with
2249 * exponential backoff
2250 */
2251static void ceph_fault(struct ceph_connection *con)
2252{
2253 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002254 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002255 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002256 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002257
Alex Elder928443c2012-05-22 11:41:43 -05002258 if (test_bit(LOSSYTX, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002259 dout("fault on LOSSYTX channel\n");
2260 goto out;
2261 }
2262
Sage Weilec302642009-12-22 10:43:42 -08002263 mutex_lock(&con->mutex);
Sage Weil91e45ce32010-02-15 12:05:09 -08002264 if (test_bit(CLOSED, &con->state))
2265 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002266
Sage Weil31b80062009-10-06 11:31:13 -07002267 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002268
2269 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002270 BUG_ON(con->in_msg->con != con);
2271 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08002272 ceph_msg_put(con->in_msg);
2273 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07002274 con->ops->put(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002275 }
Sage Weil31b80062009-10-06 11:31:13 -07002276
Sage Weile80a52d2010-02-25 12:40:45 -08002277 /* Requeue anything that hasn't been acked */
2278 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002279
Sage Weile76661d2011-03-03 10:10:15 -08002280 /* If there are no messages queued or keepalive pending, place
2281 * the connection in a STANDBY state */
2282 if (list_empty(&con->out_queue) &&
Alex Elder928443c2012-05-22 11:41:43 -05002283 !test_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weile00de342011-03-04 12:25:05 -08002284 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elder928443c2012-05-22 11:41:43 -05002285 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002286 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08002287 } else {
2288 /* retry after a delay. */
2289 if (con->delay == 0)
2290 con->delay = BASE_DELAY_INTERVAL;
2291 else if (con->delay < MAX_DELAY_INTERVAL)
2292 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002293 con->ops->get(con);
2294 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002295 round_jiffies_relative(con->delay))) {
2296 dout("fault queued %p delay %lu\n", con, con->delay);
2297 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002298 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002299 dout("fault failed to queue %p delay %lu, backoff\n",
2300 con, con->delay);
2301 /*
2302 * In many cases we see a socket state change
2303 * while con_work is running and end up
2304 * queuing (non-delayed) work, such that we
2305 * can't backoff with a delay. Set a flag so
2306 * that when con_work restarts we schedule the
2307 * delay then.
2308 */
Alex Elder928443c2012-05-22 11:41:43 -05002309 set_bit(BACKOFF, &con->flags);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002310 }
Sage Weil31b80062009-10-06 11:31:13 -07002311 }
2312
Sage Weil91e45ce32010-02-15 12:05:09 -08002313out_unlock:
2314 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002315out:
Sage Weil161fd652010-02-25 12:38:57 -08002316 /*
2317 * in case we faulted due to authentication, invalidate our
2318 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002319 */
Sage Weil161fd652010-02-25 12:38:57 -08002320 if (con->auth_retry && con->ops->invalidate_authorizer) {
2321 dout("calling invalidate_authorizer()\n");
2322 con->ops->invalidate_authorizer(con);
2323 }
2324
Sage Weil31b80062009-10-06 11:31:13 -07002325 if (con->ops->fault)
2326 con->ops->fault(con);
2327}
2328
2329
2330
2331/*
Alex Elder15d98822012-05-26 23:26:43 -05002332 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002333 */
Alex Elder15d98822012-05-26 23:26:43 -05002334void ceph_messenger_init(struct ceph_messenger *msgr,
2335 struct ceph_entity_addr *myaddr,
2336 u32 supported_features,
2337 u32 required_features,
2338 bool nocrc)
Sage Weil31b80062009-10-06 11:31:13 -07002339{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002340 msgr->supported_features = supported_features;
2341 msgr->required_features = required_features;
2342
Sage Weil31b80062009-10-06 11:31:13 -07002343 spin_lock_init(&msgr->global_seq_lock);
2344
Sage Weil31b80062009-10-06 11:31:13 -07002345 if (myaddr)
2346 msgr->inst.addr = *myaddr;
2347
2348 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002349 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002350 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002351 encode_my_addr(msgr);
Alex Elder15d98822012-05-26 23:26:43 -05002352 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002353
Alex Elder15d98822012-05-26 23:26:43 -05002354 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002355}
Alex Elder15d98822012-05-26 23:26:43 -05002356EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002357
Sage Weile00de342011-03-04 12:25:05 -08002358static void clear_standby(struct ceph_connection *con)
2359{
2360 /* come back from STANDBY? */
2361 if (test_and_clear_bit(STANDBY, &con->state)) {
2362 mutex_lock(&con->mutex);
2363 dout("clear_standby %p and ++connect_seq\n", con);
2364 con->connect_seq++;
Alex Elder928443c2012-05-22 11:41:43 -05002365 WARN_ON(test_bit(WRITE_PENDING, &con->flags));
2366 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags));
Sage Weile00de342011-03-04 12:25:05 -08002367 mutex_unlock(&con->mutex);
2368 }
2369}
2370
Sage Weil31b80062009-10-06 11:31:13 -07002371/*
2372 * Queue up an outgoing message on the given connection.
2373 */
2374void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2375{
2376 if (test_bit(CLOSED, &con->state)) {
2377 dout("con_send %p closed, dropping %p\n", con, msg);
2378 ceph_msg_put(msg);
2379 return;
2380 }
2381
2382 /* set src+dst */
Sage Weildbad1852010-03-25 15:45:38 -07002383 msg->hdr.src = con->msgr->inst.name;
Sage Weil31b80062009-10-06 11:31:13 -07002384
Sage Weil3ca02ef2010-03-01 15:25:00 -08002385 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2386
Sage Weile84346b2010-05-11 21:20:38 -07002387 msg->needs_out_seq = true;
2388
Sage Weil31b80062009-10-06 11:31:13 -07002389 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08002390 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002391
Alex Elder38941f82012-06-01 14:56:43 -05002392 BUG_ON(msg->con != NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002393 msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002394 BUG_ON(msg->con == NULL);
2395
Sage Weil31b80062009-10-06 11:31:13 -07002396 BUG_ON(!list_empty(&msg->list_head));
2397 list_add_tail(&msg->list_head, &con->out_queue);
2398 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2399 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2400 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2401 le32_to_cpu(msg->hdr.front_len),
2402 le32_to_cpu(msg->hdr.middle_len),
2403 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002404 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002405
2406 /* if there wasn't anything waiting to send before, queue
2407 * new work */
Sage Weile00de342011-03-04 12:25:05 -08002408 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002409 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002410 queue_con(con);
2411}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002412EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002413
2414/*
2415 * Revoke a message that was previously queued for send
2416 */
Alex Elder6740a842012-06-01 14:56:43 -05002417void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002418{
Alex Elder6740a842012-06-01 14:56:43 -05002419 struct ceph_connection *con = msg->con;
2420
2421 if (!con)
2422 return; /* Message not in our possession */
2423
Sage Weilec302642009-12-22 10:43:42 -08002424 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002425 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05002426 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002427 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -05002428 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002429 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -05002430 msg->con = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05002431 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05002432
Sage Weil31b80062009-10-06 11:31:13 -07002433 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002434 }
2435 if (con->out_msg == msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002436 dout("%s %p msg %p - was sending\n", __func__, con, msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002437 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002438 if (con->out_kvec_is_msg) {
2439 con->out_skip = con->out_kvec_bytes;
2440 con->out_kvec_is_msg = false;
2441 }
Sage Weiled98ada2010-07-05 12:15:14 -07002442 msg->hdr.seq = 0;
Alex Elder92ce0342012-06-04 14:43:33 -05002443
2444 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07002445 }
Sage Weilec302642009-12-22 10:43:42 -08002446 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002447}
2448
2449/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002450 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002451 */
Alex Elder8921d112012-06-01 14:56:43 -05002452void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002453{
Alex Elder8921d112012-06-01 14:56:43 -05002454 struct ceph_connection *con;
2455
2456 BUG_ON(msg == NULL);
2457 if (!msg->con) {
2458 dout("%s msg %p null con\n", __func__, msg);
2459
2460 return; /* Message not in our possession */
2461 }
2462
2463 con = msg->con;
Sage Weil350b1c32009-12-22 10:45:45 -08002464 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05002465 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002466 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
2467 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
2468 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002469
2470 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05002471 dout("%s %p msg %p revoked\n", __func__, con, msg);
2472 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08002473 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002474 front_len -
2475 middle_len -
2476 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002477 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002478 ceph_msg_put(con->in_msg);
2479 con->in_msg = NULL;
2480 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002481 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002482 } else {
Alex Elder8921d112012-06-01 14:56:43 -05002483 dout("%s %p in_msg %p msg %p no-op\n",
2484 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002485 }
2486 mutex_unlock(&con->mutex);
2487}
2488
2489/*
Sage Weil31b80062009-10-06 11:31:13 -07002490 * Queue a keepalive byte to ensure the tcp connection is alive.
2491 */
2492void ceph_con_keepalive(struct ceph_connection *con)
2493{
Sage Weile00de342011-03-04 12:25:05 -08002494 dout("con_keepalive %p\n", con);
2495 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002496 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2497 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002498 queue_con(con);
2499}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002500EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002501
2502
2503/*
2504 * construct a new message with given type, size
2505 * the new msg has a ref count of 1.
2506 */
Sage Weilb61c2762011-08-09 15:03:46 -07002507struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2508 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002509{
2510 struct ceph_msg *m;
2511
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002512 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002513 if (m == NULL)
2514 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002515 kref_init(&m->kref);
Alex Elder38941f82012-06-01 14:56:43 -05002516
2517 m->con = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002518 INIT_LIST_HEAD(&m->list_head);
2519
Sage Weil45c6ceb2010-05-11 15:01:51 -07002520 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002521 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002522 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2523 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002524 m->hdr.front_len = cpu_to_le32(front_len);
2525 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002526 m->hdr.data_len = 0;
2527 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002528 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002529 m->footer.front_crc = 0;
2530 m->footer.middle_crc = 0;
2531 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002532 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002533 m->front_max = front_len;
2534 m->front_is_vmalloc = false;
2535 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002536 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002537 m->pool = NULL;
2538
Henry C Changca208922011-05-03 02:29:56 +00002539 /* middle */
2540 m->middle = NULL;
2541
2542 /* data */
2543 m->nr_pages = 0;
2544 m->page_alignment = 0;
2545 m->pages = NULL;
2546 m->pagelist = NULL;
2547 m->bio = NULL;
2548 m->bio_iter = NULL;
2549 m->bio_seg = 0;
2550 m->trail = NULL;
2551
Sage Weil31b80062009-10-06 11:31:13 -07002552 /* front */
2553 if (front_len) {
2554 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002555 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002556 PAGE_KERNEL);
2557 m->front_is_vmalloc = true;
2558 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002559 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002560 }
2561 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002562 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002563 front_len);
2564 goto out2;
2565 }
2566 } else {
2567 m->front.iov_base = NULL;
2568 }
2569 m->front.iov_len = front_len;
2570
Sage Weilbb257662010-04-01 16:07:23 -07002571 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002572 return m;
2573
2574out2:
2575 ceph_msg_put(m);
2576out:
Sage Weilb61c2762011-08-09 15:03:46 -07002577 if (!can_fail) {
2578 pr_err("msg_new can't create type %d front %d\n", type,
2579 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002580 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002581 } else {
2582 dout("msg_new can't create type %d front %d\n", type,
2583 front_len);
2584 }
Sage Weila79832f2010-04-01 16:06:19 -07002585 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002586}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002587EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002588
2589/*
Sage Weil31b80062009-10-06 11:31:13 -07002590 * Allocate "middle" portion of a message, if it is needed and wasn't
2591 * allocated by alloc_msg. This allows us to read a small fixed-size
2592 * per-type header in the front and then gracefully fail (i.e.,
2593 * propagate the error to the caller based on info in the front) when
2594 * the middle is too large.
2595 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002596static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002597{
2598 int type = le16_to_cpu(msg->hdr.type);
2599 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2600
2601 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2602 ceph_msg_type_name(type), middle_len);
2603 BUG_ON(!middle_len);
2604 BUG_ON(msg->middle);
2605
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002606 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002607 if (!msg->middle)
2608 return -ENOMEM;
2609 return 0;
2610}
2611
Yehuda Sadeh24504182010-01-08 13:58:34 -08002612/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05002613 * Allocate a message for receiving an incoming message on a
2614 * connection, and save the result in con->in_msg. Uses the
2615 * connection's private alloc_msg op if available.
2616 *
2617 * Returns true if the message should be skipped, false otherwise.
2618 * If true is returned (skip message), con->in_msg will be NULL.
2619 * If false is returned, con->in_msg will contain a pointer to the
2620 * newly-allocated message, or NULL in case of memory exhaustion.
Yehuda Sadeh24504182010-01-08 13:58:34 -08002621 */
Alex Elder1c20f2d2012-06-04 14:43:32 -05002622static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2623 struct ceph_msg_header *hdr)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002624{
2625 int type = le16_to_cpu(hdr->type);
2626 int front_len = le32_to_cpu(hdr->front_len);
2627 int middle_len = le32_to_cpu(hdr->middle_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002628 int ret;
2629
Alex Elder1c20f2d2012-06-04 14:43:32 -05002630 BUG_ON(con->in_msg != NULL);
2631
Yehuda Sadeh24504182010-01-08 13:58:34 -08002632 if (con->ops->alloc_msg) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002633 int skip = 0;
2634
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002635 mutex_unlock(&con->mutex);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002636 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002637 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002638 if (con->in_msg) {
Sage Weil36eb71a2012-06-21 12:47:08 -07002639 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002640 BUG_ON(con->in_msg->con == NULL);
2641 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002642 if (skip)
2643 con->in_msg = NULL;
2644
2645 if (!con->in_msg)
2646 return skip != 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002647 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002648 if (!con->in_msg) {
2649 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2650 if (!con->in_msg) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002651 pr_err("unable to allocate msg type %d len %d\n",
2652 type, front_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002653 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002654 }
Sage Weil36eb71a2012-06-21 12:47:08 -07002655 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002656 BUG_ON(con->in_msg->con == NULL);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002657 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002658 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002659 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002660
Alex Elder1c20f2d2012-06-04 14:43:32 -05002661 if (middle_len && !con->in_msg->middle) {
2662 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002663 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002664 ceph_msg_put(con->in_msg);
2665 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002666 }
2667 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002668
Alex Elder1c20f2d2012-06-04 14:43:32 -05002669 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002670}
2671
Sage Weil31b80062009-10-06 11:31:13 -07002672
2673/*
2674 * Free a generically kmalloc'd message.
2675 */
2676void ceph_msg_kfree(struct ceph_msg *m)
2677{
2678 dout("msg_kfree %p\n", m);
2679 if (m->front_is_vmalloc)
2680 vfree(m->front.iov_base);
2681 else
2682 kfree(m->front.iov_base);
2683 kfree(m);
2684}
2685
2686/*
2687 * Drop a msg ref. Destroy as needed.
2688 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002689void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002690{
Sage Weilc2e552e2009-12-07 15:55:05 -08002691 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002692
Sage Weilc2e552e2009-12-07 15:55:05 -08002693 dout("ceph_msg_put last one on %p\n", m);
2694 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002695
Sage Weilc2e552e2009-12-07 15:55:05 -08002696 /* drop middle, data, if any */
2697 if (m->middle) {
2698 ceph_buffer_put(m->middle);
2699 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002700 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002701 m->nr_pages = 0;
2702 m->pages = NULL;
2703
Sage Weil58bb3b32009-12-23 12:12:31 -08002704 if (m->pagelist) {
2705 ceph_pagelist_release(m->pagelist);
2706 kfree(m->pagelist);
2707 m->pagelist = NULL;
2708 }
2709
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002710 m->trail = NULL;
2711
Sage Weilc2e552e2009-12-07 15:55:05 -08002712 if (m->pool)
2713 ceph_msgpool_put(m->pool, m);
2714 else
2715 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002716}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002717EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002718
2719void ceph_msg_dump(struct ceph_msg *msg)
2720{
2721 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2722 msg->front_max, msg->nr_pages);
2723 print_hex_dump(KERN_DEBUG, "header: ",
2724 DUMP_PREFIX_OFFSET, 16, 1,
2725 &msg->hdr, sizeof(msg->hdr), true);
2726 print_hex_dump(KERN_DEBUG, " front: ",
2727 DUMP_PREFIX_OFFSET, 16, 1,
2728 msg->front.iov_base, msg->front.iov_len, true);
2729 if (msg->middle)
2730 print_hex_dump(KERN_DEBUG, "middle: ",
2731 DUMP_PREFIX_OFFSET, 16, 1,
2732 msg->middle->vec.iov_base,
2733 msg->middle->vec.iov_len, true);
2734 print_hex_dump(KERN_DEBUG, "footer: ",
2735 DUMP_PREFIX_OFFSET, 16, 1,
2736 &msg->footer, sizeof(msg->footer), true);
2737}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002738EXPORT_SYMBOL(ceph_msg_dump);