blob: 769a2c9fe1afb5136ec352e26b856c54c3a5801f [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 Weilf91d3472010-07-01 15:18:31 -0700324 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
325 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700326 if (ret == -EINPROGRESS) {
327 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700328 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700329 sock->sk->sk_state);
Alex Eldera5bc3122012-01-23 15:49:27 -0600330 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700331 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700332 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700333 sock_release(sock);
Sage Weil31b80062009-10-06 11:31:13 -0700334 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -0700335
Alex Elder41617d02012-02-14 14:05:33 -0600336 return ret;
Alex Eldera5bc3122012-01-23 15:49:27 -0600337 }
338 con->sock = sock;
Alex Elderce2c8902012-05-22 22:15:49 -0500339 con_sock_state_connecting(con);
Alex Eldera5bc3122012-01-23 15:49:27 -0600340
Alex Elder41617d02012-02-14 14:05:33 -0600341 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700342}
343
344static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
345{
346 struct kvec iov = {buf, len};
347 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800348 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700349
Sage Weil98bdb0a2011-01-25 08:17:48 -0800350 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
351 if (r == -EAGAIN)
352 r = 0;
353 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700354}
355
356/*
357 * write something. @more is true if caller will be sending more data
358 * shortly.
359 */
360static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
361 size_t kvlen, size_t len, int more)
362{
363 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800364 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700365
366 if (more)
367 msg.msg_flags |= MSG_MORE;
368 else
369 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
370
Sage Weil42961d22011-01-25 08:19:34 -0800371 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
372 if (r == -EAGAIN)
373 r = 0;
374 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700375}
376
Alex Elder31739132012-03-07 11:40:08 -0600377static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
378 int offset, size_t size, int more)
379{
380 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
381 int ret;
382
383 ret = kernel_sendpage(sock, page, offset, size, flags);
384 if (ret == -EAGAIN)
385 ret = 0;
386
387 return ret;
388}
389
Sage Weil31b80062009-10-06 11:31:13 -0700390
391/*
392 * Shutdown/close the socket for the given connection.
393 */
394static int con_close_socket(struct ceph_connection *con)
395{
396 int rc;
397
398 dout("con_close_socket on %p sock %p\n", con, con->sock);
399 if (!con->sock)
400 return 0;
401 set_bit(SOCK_CLOSED, &con->state);
402 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
403 sock_release(con->sock);
404 con->sock = NULL;
405 clear_bit(SOCK_CLOSED, &con->state);
Alex Elderce2c8902012-05-22 22:15:49 -0500406 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700407 return rc;
408}
409
410/*
411 * Reset a connection. Discard all incoming and outgoing messages
412 * and clear *_seq state.
413 */
414static void ceph_msg_remove(struct ceph_msg *msg)
415{
416 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500417 BUG_ON(msg->con == NULL);
Alex Elder92ce0342012-06-04 14:43:33 -0500418 ceph_con_put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -0500419 msg->con = NULL;
420
Sage Weil31b80062009-10-06 11:31:13 -0700421 ceph_msg_put(msg);
422}
423static void ceph_msg_remove_list(struct list_head *head)
424{
425 while (!list_empty(head)) {
426 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
427 list_head);
428 ceph_msg_remove(msg);
429 }
430}
431
432static void reset_connection(struct ceph_connection *con)
433{
434 /* reset connection, out_queue, msg_ and connect_seq */
435 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700436 ceph_msg_remove_list(&con->out_queue);
437 ceph_msg_remove_list(&con->out_sent);
438
Sage Weilcf3e5c42009-12-11 09:48:05 -0800439 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500440 BUG_ON(con->in_msg->con != con);
441 con->in_msg->con = NULL;
Sage Weilcf3e5c42009-12-11 09:48:05 -0800442 ceph_msg_put(con->in_msg);
443 con->in_msg = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -0500444 ceph_con_put(con->in_msg->con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800445 }
446
Sage Weil31b80062009-10-06 11:31:13 -0700447 con->connect_seq = 0;
448 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800449 if (con->out_msg) {
450 ceph_msg_put(con->out_msg);
451 con->out_msg = NULL;
452 }
Sage Weil31b80062009-10-06 11:31:13 -0700453 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700454 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700455}
456
457/*
458 * mark a peer down. drop any open connections.
459 */
460void ceph_con_close(struct ceph_connection *con)
461{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700462 dout("con_close %p peer %s\n", con,
463 ceph_pr_addr(&con->peer_addr.in_addr));
Alex Eldera5988c42012-05-29 11:04:58 -0500464 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700465 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Alex Eldera5988c42012-05-29 11:04:58 -0500466 set_bit(CLOSED, &con->state);
467
Alex Elder928443c2012-05-22 11:41:43 -0500468 clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */
469 clear_bit(KEEPALIVE_PENDING, &con->flags);
470 clear_bit(WRITE_PENDING, &con->flags);
Alex Eldera5988c42012-05-29 11:04:58 -0500471
Sage Weilec302642009-12-22 10:43:42 -0800472 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700473 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700474 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800475 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800476 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700477 queue_con(con);
478}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700479EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700480
481/*
Sage Weil31b80062009-10-06 11:31:13 -0700482 * Reopen a closed connection, with a new peer address.
483 */
484void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
485{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700486 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700487 set_bit(OPENING, &con->state);
Alex Eldera5988c42012-05-29 11:04:58 -0500488 WARN_ON(!test_and_clear_bit(CLOSED, &con->state));
489
Sage Weil31b80062009-10-06 11:31:13 -0700490 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800491 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700492 queue_con(con);
493}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700494EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700495
496/*
Sage Weil87b315a2010-03-22 14:51:18 -0700497 * return true if this connection ever successfully opened
498 */
499bool ceph_con_opened(struct ceph_connection *con)
500{
501 return con->connect_seq > 0;
502}
503
504/*
Sage Weil31b80062009-10-06 11:31:13 -0700505 * generic get/put
506 */
507struct ceph_connection *ceph_con_get(struct ceph_connection *con)
508{
Alex Elderd3002b92012-02-14 14:05:33 -0600509 int nref = __atomic_add_unless(&con->nref, 1, 0);
510
511 dout("con_get %p nref = %d -> %d\n", con, nref, nref + 1);
512
513 return nref ? con : NULL;
Sage Weil31b80062009-10-06 11:31:13 -0700514}
515
516void ceph_con_put(struct ceph_connection *con)
517{
Alex Elderd3002b92012-02-14 14:05:33 -0600518 int nref = atomic_dec_return(&con->nref);
519
520 BUG_ON(nref < 0);
521 if (nref == 0) {
Sage Weil71ececd2009-11-18 11:27:06 -0800522 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -0700523 kfree(con);
524 }
Alex Elderd3002b92012-02-14 14:05:33 -0600525 dout("con_put %p nref = %d -> %d\n", con, nref + 1, nref);
Sage Weil31b80062009-10-06 11:31:13 -0700526}
527
528/*
529 * initialize a new connection.
530 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500531void ceph_con_init(struct ceph_connection *con, void *private,
532 const struct ceph_connection_operations *ops,
533 struct ceph_messenger *msgr, __u8 entity_type, __u64 entity_num)
Sage Weil31b80062009-10-06 11:31:13 -0700534{
535 dout("con_init %p\n", con);
536 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500537 con->private = private;
538 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700539 atomic_set(&con->nref, 1);
540 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500541
542 con_sock_state_init(con);
543
Alex Elder1bfd89f2012-05-26 23:26:43 -0500544 con->peer_name.type = (__u8) entity_type;
545 con->peer_name.num = cpu_to_le64(entity_num);
546
Sage Weilec302642009-12-22 10:43:42 -0800547 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700548 INIT_LIST_HEAD(&con->out_queue);
549 INIT_LIST_HEAD(&con->out_sent);
550 INIT_DELAYED_WORK(&con->work, con_work);
Alex Eldera5988c42012-05-29 11:04:58 -0500551
552 set_bit(CLOSED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700553}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700554EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700555
556
557/*
558 * We maintain a global counter to order connection attempts. Get
559 * a unique seq greater than @gt.
560 */
561static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
562{
563 u32 ret;
564
565 spin_lock(&msgr->global_seq_lock);
566 if (msgr->global_seq < gt)
567 msgr->global_seq = gt;
568 ret = ++msgr->global_seq;
569 spin_unlock(&msgr->global_seq_lock);
570 return ret;
571}
572
Alex Eldere2200422012-05-23 14:35:23 -0500573static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600574{
575 con->out_kvec_left = 0;
576 con->out_kvec_bytes = 0;
577 con->out_kvec_cur = &con->out_kvec[0];
578}
579
Alex Eldere2200422012-05-23 14:35:23 -0500580static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600581 size_t size, void *data)
582{
583 int index;
584
585 index = con->out_kvec_left;
586 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
587
588 con->out_kvec[index].iov_len = size;
589 con->out_kvec[index].iov_base = data;
590 con->out_kvec_left++;
591 con->out_kvec_bytes += size;
592}
Sage Weil31b80062009-10-06 11:31:13 -0700593
594/*
595 * Prepare footer for currently outgoing message, and finish things
596 * off. Assumes out_kvec* are already valid.. we just add on to the end.
597 */
Alex Elder859eb792012-02-14 14:05:33 -0600598static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700599{
600 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600601 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700602
603 dout("prepare_write_message_footer %p\n", con);
604 con->out_kvec_is_msg = true;
605 con->out_kvec[v].iov_base = &m->footer;
606 con->out_kvec[v].iov_len = sizeof(m->footer);
607 con->out_kvec_bytes += sizeof(m->footer);
608 con->out_kvec_left++;
609 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800610 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700611}
612
613/*
614 * Prepare headers for the next outgoing message.
615 */
616static void prepare_write_message(struct ceph_connection *con)
617{
618 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600619 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700620
Alex Eldere2200422012-05-23 14:35:23 -0500621 con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700622 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800623 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700624
625 /* Sneak an ack in there first? If we can get it into the same
626 * TCP packet that's a good thing. */
627 if (con->in_seq > con->in_seq_acked) {
628 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -0500629 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700630 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500631 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600632 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700633 }
634
Alex Elder38941f82012-06-01 14:56:43 -0500635 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -0600636 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800637 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -0500638 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -0700639
640 /* put message on sent list */
641 ceph_msg_get(m);
642 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700643
Sage Weile84346b2010-05-11 21:20:38 -0700644 /*
645 * only assign outgoing seq # if we haven't sent this message
646 * yet. if it is requeued, resend with it's original seq.
647 */
648 if (m->needs_out_seq) {
649 m->hdr.seq = cpu_to_le64(++con->out_seq);
650 m->needs_out_seq = false;
651 }
Yan, Zheng43643522012-06-06 19:35:55 -0500652#ifdef CONFIG_BLOCK
653 else
654 m->bio_iter = NULL;
655#endif
Sage Weil31b80062009-10-06 11:31:13 -0700656
657 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
658 m, con->out_seq, le16_to_cpu(m->hdr.type),
659 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
660 le32_to_cpu(m->hdr.data_len),
661 m->nr_pages);
662 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
663
664 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -0500665 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
666 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
667 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600668
Sage Weil31b80062009-10-06 11:31:13 -0700669 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -0500670 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600671 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700672
673 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600674 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
675 con->out_msg->hdr.crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -0700676 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
Alex Eldera9a0c512012-02-15 07:43:54 -0600677
678 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
679 con->out_msg->footer.front_crc = cpu_to_le32(crc);
680 if (m->middle) {
681 crc = crc32c(0, m->middle->vec.iov_base,
682 m->middle->vec.iov_len);
683 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
684 } else
Sage Weil31b80062009-10-06 11:31:13 -0700685 con->out_msg->footer.middle_crc = 0;
686 con->out_msg->footer.data_crc = 0;
687 dout("prepare_write_message front_crc %u data_crc %u\n",
688 le32_to_cpu(con->out_msg->footer.front_crc),
689 le32_to_cpu(con->out_msg->footer.middle_crc));
690
691 /* is there a data payload? */
692 if (le32_to_cpu(m->hdr.data_len) > 0) {
693 /* initialize page iterator */
694 con->out_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700695 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -0800696 con->out_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700697 else
698 con->out_msg_pos.page_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700699 con->out_msg_pos.data_pos = 0;
Alex Elderbca064d2012-02-15 07:43:54 -0600700 con->out_msg_pos.did_page_crc = false;
Sage Weil31b80062009-10-06 11:31:13 -0700701 con->out_more = 1; /* data + footer will follow */
702 } else {
703 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600704 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700705 }
706
Alex Elder928443c2012-05-22 11:41:43 -0500707 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700708}
709
710/*
711 * Prepare an ack.
712 */
713static void prepare_write_ack(struct ceph_connection *con)
714{
715 dout("prepare_write_ack %p %llu -> %llu\n", con,
716 con->in_seq_acked, con->in_seq);
717 con->in_seq_acked = con->in_seq;
718
Alex Eldere2200422012-05-23 14:35:23 -0500719 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600720
Alex Eldere2200422012-05-23 14:35:23 -0500721 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600722
Sage Weil31b80062009-10-06 11:31:13 -0700723 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500724 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600725 &con->out_temp_ack);
726
Sage Weil31b80062009-10-06 11:31:13 -0700727 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elder928443c2012-05-22 11:41:43 -0500728 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700729}
730
731/*
732 * Prepare to write keepalive byte.
733 */
734static void prepare_write_keepalive(struct ceph_connection *con)
735{
736 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -0500737 con_out_kvec_reset(con);
738 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Alex Elder928443c2012-05-22 11:41:43 -0500739 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700740}
741
742/*
743 * Connection negotiation.
744 */
745
Alex Elderdac1e712012-05-16 15:16:39 -0500746static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
747 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800748{
Alex Eldera3530df2012-05-16 15:16:39 -0500749 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -0500750
751 if (!con->ops->get_authorizer) {
752 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
753 con->out_connect.authorizer_len = 0;
754
Alex Elder729796b2012-05-16 15:16:39 -0500755 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -0500756 }
757
758 /* Can't hold the mutex while getting authorizer */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800759
Sage Weilec302642009-12-22 10:43:42 -0800760 mutex_unlock(&con->mutex);
Alex Elderb1c6b982012-05-16 15:16:38 -0500761
Alex Elderdac1e712012-05-16 15:16:39 -0500762 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Elder8f43fb52012-05-16 15:16:39 -0500763
Sage Weilec302642009-12-22 10:43:42 -0800764 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800765
Alex Eldera3530df2012-05-16 15:16:39 -0500766 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -0500767 return auth;
Alex Elder928443c2012-05-22 11:41:43 -0500768 if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags))
Alex Elder729796b2012-05-16 15:16:39 -0500769 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700770
Alex Elder8f43fb52012-05-16 15:16:39 -0500771 con->auth_reply_buf = auth->authorizer_reply_buf;
772 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
773
Alex Elder859eb792012-02-14 14:05:33 -0600774
Alex Elder729796b2012-05-16 15:16:39 -0500775 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800776}
777
Sage Weil31b80062009-10-06 11:31:13 -0700778/*
779 * We connected to a peer and are saying hello.
780 */
Alex Eldere825a662012-05-16 15:16:38 -0500781static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700782{
Alex Eldere2200422012-05-23 14:35:23 -0500783 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
784 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -0500785 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800786
Sage Weileed0ef22009-11-10 14:34:36 -0800787 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500788 set_bit(WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800789}
790
Alex Eldere825a662012-05-16 15:16:38 -0500791static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800792{
Sage Weil31b80062009-10-06 11:31:13 -0700793 unsigned global_seq = get_global_seq(con->msgr, 0);
794 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -0500795 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -0500796 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700797
798 switch (con->peer_name.type) {
799 case CEPH_ENTITY_TYPE_MON:
800 proto = CEPH_MONC_PROTOCOL;
801 break;
802 case CEPH_ENTITY_TYPE_OSD:
803 proto = CEPH_OSDC_PROTOCOL;
804 break;
805 case CEPH_ENTITY_TYPE_MDS:
806 proto = CEPH_MDSC_PROTOCOL;
807 break;
808 default:
809 BUG();
810 }
811
812 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
813 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800814
Alex Eldere825a662012-05-16 15:16:38 -0500815 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700816 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
817 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
818 con->out_connect.global_seq = cpu_to_le32(global_seq);
819 con->out_connect.protocol_version = cpu_to_le32(proto);
820 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700821
Alex Elderdac1e712012-05-16 15:16:39 -0500822 auth_proto = CEPH_AUTH_UNKNOWN;
823 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -0500824 if (IS_ERR(auth))
825 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -0500826
Alex Elderdac1e712012-05-16 15:16:39 -0500827 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -0500828 con->out_connect.authorizer_len = auth ?
829 cpu_to_le32(auth->authorizer_buf_len) : 0;
830
Alex Eldere2200422012-05-23 14:35:23 -0500831 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -0500832 &con->out_connect);
833 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -0500834 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -0500835 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600836
Sage Weil31b80062009-10-06 11:31:13 -0700837 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500838 set_bit(WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800839
Alex Eldere10c7582012-05-16 15:16:38 -0500840 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700841}
842
Sage Weil31b80062009-10-06 11:31:13 -0700843/*
844 * write as much of pending kvecs to the socket as we can.
845 * 1 -> done
846 * 0 -> socket full, but more to do
847 * <0 -> error
848 */
849static int write_partial_kvec(struct ceph_connection *con)
850{
851 int ret;
852
853 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
854 while (con->out_kvec_bytes > 0) {
855 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
856 con->out_kvec_left, con->out_kvec_bytes,
857 con->out_more);
858 if (ret <= 0)
859 goto out;
860 con->out_kvec_bytes -= ret;
861 if (con->out_kvec_bytes == 0)
862 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600863
864 /* account for full iov entries consumed */
865 while (ret >= con->out_kvec_cur->iov_len) {
866 BUG_ON(!con->out_kvec_left);
867 ret -= con->out_kvec_cur->iov_len;
868 con->out_kvec_cur++;
869 con->out_kvec_left--;
870 }
871 /* and for a partially-consumed entry */
872 if (ret) {
873 con->out_kvec_cur->iov_len -= ret;
874 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700875 }
876 }
877 con->out_kvec_left = 0;
878 con->out_kvec_is_msg = false;
879 ret = 1;
880out:
881 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
882 con->out_kvec_bytes, con->out_kvec_left, ret);
883 return ret; /* done! */
884}
885
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700886#ifdef CONFIG_BLOCK
887static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
888{
889 if (!bio) {
890 *iter = NULL;
891 *seg = 0;
892 return;
893 }
894 *iter = bio;
895 *seg = bio->bi_idx;
896}
897
898static void iter_bio_next(struct bio **bio_iter, int *seg)
899{
900 if (*bio_iter == NULL)
901 return;
902
903 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
904
905 (*seg)++;
906 if (*seg == (*bio_iter)->bi_vcnt)
907 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
908}
909#endif
910
Sage Weil31b80062009-10-06 11:31:13 -0700911/*
912 * Write as much message data payload as we can. If we finish, queue
913 * up the footer.
914 * 1 -> done, footer is now queued in out_kvec[].
915 * 0 -> socket full, but more to do
916 * <0 -> error
917 */
918static int write_partial_msg_pages(struct ceph_connection *con)
919{
920 struct ceph_msg *msg = con->out_msg;
921 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
922 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600923 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700924 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700925 int total_max_write;
926 int in_trail = 0;
927 size_t trail_len = (msg->trail ? msg->trail->length : 0);
Sage Weil31b80062009-10-06 11:31:13 -0700928
929 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
930 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
931 con->out_msg_pos.page_pos);
932
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700933#ifdef CONFIG_BLOCK
934 if (msg->bio && !msg->bio_iter)
935 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
936#endif
937
938 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -0700939 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700940 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -0600941 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700942
943 total_max_write = data_len - trail_len -
944 con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -0700945
946 /*
947 * if we are calculating the data crc (the default), we need
948 * to map the page. if our pages[] has been revoked, use the
949 * zero page.
950 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700951
952 /* have we reached the trail part of the data? */
953 if (con->out_msg_pos.data_pos >= data_len - trail_len) {
954 in_trail = 1;
955
956 total_max_write = data_len - con->out_msg_pos.data_pos;
957
958 page = list_first_entry(&msg->trail->head,
959 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700960 max_write = PAGE_SIZE;
961 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -0700962 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -0800963 } else if (msg->pagelist) {
964 page = list_first_entry(&msg->pagelist->head,
965 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700966#ifdef CONFIG_BLOCK
967 } else if (msg->bio) {
968 struct bio_vec *bv;
969
970 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
971 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -0600972 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700973 max_write = bv->bv_len;
974#endif
Sage Weil31b80062009-10-06 11:31:13 -0700975 } else {
Alex Elder57666512012-01-23 15:49:27 -0600976 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -0700977 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700978 len = min_t(int, max_write - con->out_msg_pos.page_pos,
979 total_max_write);
980
Alex Elder37675b02012-03-07 11:40:08 -0600981 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -0600982 void *base;
Alex Eldera9a0c512012-02-15 07:43:54 -0600983 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700984 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -0600985 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -0700986
Alex Elder8d63e312012-03-07 11:40:08 -0600987 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -0700988 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -0600989 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Eldera9a0c512012-02-15 07:43:54 -0600990 crc = crc32c(tmpcrc, base, len);
991 con->out_msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -0600992 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -0700993 }
Alex Eldere36b13c2012-03-07 11:40:08 -0600994 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -0600995 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -0600996 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -0700997
Alex Elder0cdf9e62012-03-07 11:40:08 -0600998 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -0700999 kunmap(page);
1000
1001 if (ret <= 0)
1002 goto out;
1003
1004 con->out_msg_pos.data_pos += ret;
1005 con->out_msg_pos.page_pos += ret;
1006 if (ret == len) {
1007 con->out_msg_pos.page_pos = 0;
1008 con->out_msg_pos.page++;
Alex Elderbca064d2012-02-15 07:43:54 -06001009 con->out_msg_pos.did_page_crc = false;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001010 if (in_trail)
1011 list_move_tail(&page->lru,
1012 &msg->trail->head);
1013 else if (msg->pagelist)
Sage Weil58bb3b32009-12-23 12:12:31 -08001014 list_move_tail(&page->lru,
1015 &msg->pagelist->head);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001016#ifdef CONFIG_BLOCK
1017 else if (msg->bio)
1018 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
1019#endif
Sage Weil31b80062009-10-06 11:31:13 -07001020 }
1021 }
1022
1023 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1024
1025 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001026 if (!do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -07001027 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001028 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001029 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001030 ret = 1;
1031out:
1032 return ret;
1033}
1034
1035/*
1036 * write some zeros
1037 */
1038static int write_partial_skip(struct ceph_connection *con)
1039{
1040 int ret;
1041
1042 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001043 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001044
Alex Elder31739132012-03-07 11:40:08 -06001045 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001046 if (ret <= 0)
1047 goto out;
1048 con->out_skip -= ret;
1049 }
1050 ret = 1;
1051out:
1052 return ret;
1053}
1054
1055/*
1056 * Prepare to read connection handshake, or an ack.
1057 */
Sage Weileed0ef22009-11-10 14:34:36 -08001058static void prepare_read_banner(struct ceph_connection *con)
1059{
1060 dout("prepare_read_banner %p\n", con);
1061 con->in_base_pos = 0;
1062}
1063
Sage Weil31b80062009-10-06 11:31:13 -07001064static void prepare_read_connect(struct ceph_connection *con)
1065{
1066 dout("prepare_read_connect %p\n", con);
1067 con->in_base_pos = 0;
1068}
1069
1070static void prepare_read_ack(struct ceph_connection *con)
1071{
1072 dout("prepare_read_ack %p\n", con);
1073 con->in_base_pos = 0;
1074}
1075
1076static void prepare_read_tag(struct ceph_connection *con)
1077{
1078 dout("prepare_read_tag %p\n", con);
1079 con->in_base_pos = 0;
1080 con->in_tag = CEPH_MSGR_TAG_READY;
1081}
1082
1083/*
1084 * Prepare to read a message.
1085 */
1086static int prepare_read_message(struct ceph_connection *con)
1087{
1088 dout("prepare_read_message %p\n", con);
1089 BUG_ON(con->in_msg != NULL);
1090 con->in_base_pos = 0;
1091 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1092 return 0;
1093}
1094
1095
1096static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001097 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001098{
Alex Eldere6cee712012-05-10 10:29:50 -05001099 while (con->in_base_pos < end) {
1100 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001101 int have = size - left;
1102 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1103 if (ret <= 0)
1104 return ret;
1105 con->in_base_pos += ret;
1106 }
1107 return 1;
1108}
1109
1110
1111/*
1112 * Read all or part of the connect-side handshake on a new connection
1113 */
Sage Weileed0ef22009-11-10 14:34:36 -08001114static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001115{
Alex Elderfd516532012-05-10 10:29:50 -05001116 int size;
1117 int end;
1118 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001119
Sage Weileed0ef22009-11-10 14:34:36 -08001120 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001121
1122 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001123 size = strlen(CEPH_BANNER);
1124 end = size;
1125 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001126 if (ret <= 0)
1127 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001128
1129 size = sizeof (con->actual_peer_addr);
1130 end += size;
1131 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001132 if (ret <= 0)
1133 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001134
1135 size = sizeof (con->peer_addr_for_me);
1136 end += size;
1137 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001138 if (ret <= 0)
1139 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001140
Sage Weileed0ef22009-11-10 14:34:36 -08001141out:
1142 return ret;
1143}
1144
1145static int read_partial_connect(struct ceph_connection *con)
1146{
Alex Elderfd516532012-05-10 10:29:50 -05001147 int size;
1148 int end;
1149 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001150
1151 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1152
Alex Elderfd516532012-05-10 10:29:50 -05001153 size = sizeof (con->in_reply);
1154 end = size;
1155 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001156 if (ret <= 0)
1157 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001158
1159 size = le32_to_cpu(con->in_reply.authorizer_len);
1160 end += size;
1161 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001162 if (ret <= 0)
1163 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001164
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001165 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1166 con, (int)con->in_reply.tag,
1167 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001168 le32_to_cpu(con->in_reply.global_seq));
1169out:
1170 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001171
Sage Weil31b80062009-10-06 11:31:13 -07001172}
1173
1174/*
1175 * Verify the hello banner looks okay.
1176 */
1177static int verify_hello(struct ceph_connection *con)
1178{
1179 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001180 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001181 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001182 con->error_msg = "protocol error, bad banner";
1183 return -1;
1184 }
1185 return 0;
1186}
1187
1188static bool addr_is_blank(struct sockaddr_storage *ss)
1189{
1190 switch (ss->ss_family) {
1191 case AF_INET:
1192 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1193 case AF_INET6:
1194 return
1195 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1196 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1197 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1198 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1199 }
1200 return false;
1201}
1202
1203static int addr_port(struct sockaddr_storage *ss)
1204{
1205 switch (ss->ss_family) {
1206 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001207 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001208 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001209 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001210 }
1211 return 0;
1212}
1213
1214static void addr_set_port(struct sockaddr_storage *ss, int p)
1215{
1216 switch (ss->ss_family) {
1217 case AF_INET:
1218 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001219 break;
Sage Weil31b80062009-10-06 11:31:13 -07001220 case AF_INET6:
1221 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001222 break;
Sage Weil31b80062009-10-06 11:31:13 -07001223 }
1224}
1225
1226/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001227 * Unlike other *_pton function semantics, zero indicates success.
1228 */
1229static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1230 char delim, const char **ipend)
1231{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001232 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1233 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001234
1235 memset(ss, 0, sizeof(*ss));
1236
1237 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1238 ss->ss_family = AF_INET;
1239 return 0;
1240 }
1241
1242 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1243 ss->ss_family = AF_INET6;
1244 return 0;
1245 }
1246
1247 return -EINVAL;
1248}
1249
1250/*
1251 * Extract hostname string and resolve using kernel DNS facility.
1252 */
1253#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1254static int ceph_dns_resolve_name(const char *name, size_t namelen,
1255 struct sockaddr_storage *ss, char delim, const char **ipend)
1256{
1257 const char *end, *delim_p;
1258 char *colon_p, *ip_addr = NULL;
1259 int ip_len, ret;
1260
1261 /*
1262 * The end of the hostname occurs immediately preceding the delimiter or
1263 * the port marker (':') where the delimiter takes precedence.
1264 */
1265 delim_p = memchr(name, delim, namelen);
1266 colon_p = memchr(name, ':', namelen);
1267
1268 if (delim_p && colon_p)
1269 end = delim_p < colon_p ? delim_p : colon_p;
1270 else if (!delim_p && colon_p)
1271 end = colon_p;
1272 else {
1273 end = delim_p;
1274 if (!end) /* case: hostname:/ */
1275 end = name + namelen;
1276 }
1277
1278 if (end <= name)
1279 return -EINVAL;
1280
1281 /* do dns_resolve upcall */
1282 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1283 if (ip_len > 0)
1284 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1285 else
1286 ret = -ESRCH;
1287
1288 kfree(ip_addr);
1289
1290 *ipend = end;
1291
1292 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1293 ret, ret ? "failed" : ceph_pr_addr(ss));
1294
1295 return ret;
1296}
1297#else
1298static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1299 struct sockaddr_storage *ss, char delim, const char **ipend)
1300{
1301 return -EINVAL;
1302}
1303#endif
1304
1305/*
1306 * Parse a server name (IP or hostname). If a valid IP address is not found
1307 * then try to extract a hostname to resolve using userspace DNS upcall.
1308 */
1309static int ceph_parse_server_name(const char *name, size_t namelen,
1310 struct sockaddr_storage *ss, char delim, const char **ipend)
1311{
1312 int ret;
1313
1314 ret = ceph_pton(name, namelen, ss, delim, ipend);
1315 if (ret)
1316 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1317
1318 return ret;
1319}
1320
1321/*
Sage Weil31b80062009-10-06 11:31:13 -07001322 * Parse an ip[:port] list into an addr array. Use the default
1323 * monitor port if a port isn't specified.
1324 */
1325int ceph_parse_ips(const char *c, const char *end,
1326 struct ceph_entity_addr *addr,
1327 int max_count, int *count)
1328{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001329 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001330 const char *p = c;
1331
1332 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1333 for (i = 0; i < max_count; i++) {
1334 const char *ipend;
1335 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001336 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001337 char delim = ',';
1338
1339 if (*p == '[') {
1340 delim = ']';
1341 p++;
1342 }
Sage Weil31b80062009-10-06 11:31:13 -07001343
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001344 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1345 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001346 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001347 ret = -EINVAL;
1348
Sage Weil31b80062009-10-06 11:31:13 -07001349 p = ipend;
1350
Sage Weil39139f62010-07-08 09:54:52 -07001351 if (delim == ']') {
1352 if (*p != ']') {
1353 dout("missing matching ']'\n");
1354 goto bad;
1355 }
1356 p++;
1357 }
1358
Sage Weil31b80062009-10-06 11:31:13 -07001359 /* port? */
1360 if (p < end && *p == ':') {
1361 port = 0;
1362 p++;
1363 while (p < end && *p >= '0' && *p <= '9') {
1364 port = (port * 10) + (*p - '0');
1365 p++;
1366 }
1367 if (port > 65535 || port == 0)
1368 goto bad;
1369 } else {
1370 port = CEPH_MON_PORT;
1371 }
1372
1373 addr_set_port(ss, port);
1374
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001375 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001376
1377 if (p == end)
1378 break;
1379 if (*p != ',')
1380 goto bad;
1381 p++;
1382 }
1383
1384 if (p != end)
1385 goto bad;
1386
1387 if (count)
1388 *count = i + 1;
1389 return 0;
1390
1391bad:
Sage Weil39139f62010-07-08 09:54:52 -07001392 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001393 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001394}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001395EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001396
Sage Weileed0ef22009-11-10 14:34:36 -08001397static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001398{
Sage Weileed0ef22009-11-10 14:34:36 -08001399 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001400
1401 if (verify_hello(con) < 0)
1402 return -1;
1403
Sage Weil63f2d212009-11-03 15:17:56 -08001404 ceph_decode_addr(&con->actual_peer_addr);
1405 ceph_decode_addr(&con->peer_addr_for_me);
1406
Sage Weil31b80062009-10-06 11:31:13 -07001407 /*
1408 * Make sure the other end is who we wanted. note that the other
1409 * end may not yet know their ip address, so if it's 0.0.0.0, give
1410 * them the benefit of the doubt.
1411 */
Sage Weil103e2d32010-01-07 16:12:36 -08001412 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1413 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001414 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1415 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001416 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001417 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001418 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001419 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001420 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001421 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001422 return -1;
1423 }
1424
1425 /*
1426 * did we learn our address?
1427 */
1428 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1429 int port = addr_port(&con->msgr->inst.addr.in_addr);
1430
1431 memcpy(&con->msgr->inst.addr.in_addr,
1432 &con->peer_addr_for_me.in_addr,
1433 sizeof(con->peer_addr_for_me.in_addr));
1434 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001435 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001436 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001437 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001438 }
1439
Sage Weileed0ef22009-11-10 14:34:36 -08001440 set_bit(NEGOTIATING, &con->state);
1441 prepare_read_connect(con);
1442 return 0;
1443}
1444
Sage Weil04a419f2009-12-23 09:30:21 -08001445static void fail_protocol(struct ceph_connection *con)
1446{
1447 reset_connection(con);
1448 set_bit(CLOSED, &con->state); /* in case there's queued work */
Sage Weil04a419f2009-12-23 09:30:21 -08001449}
1450
Sage Weileed0ef22009-11-10 14:34:36 -08001451static int process_connect(struct ceph_connection *con)
1452{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001453 u64 sup_feat = con->msgr->supported_features;
1454 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001455 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001456 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001457
Sage Weileed0ef22009-11-10 14:34:36 -08001458 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1459
Sage Weil31b80062009-10-06 11:31:13 -07001460 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001461 case CEPH_MSGR_TAG_FEATURES:
1462 pr_err("%s%lld %s feature set mismatch,"
1463 " my %llx < server's %llx, missing %llx\n",
1464 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001465 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001466 sup_feat, server_feat, server_feat & ~sup_feat);
1467 con->error_msg = "missing required protocol features";
1468 fail_protocol(con);
1469 return -1;
1470
Sage Weil31b80062009-10-06 11:31:13 -07001471 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001472 pr_err("%s%lld %s protocol version mismatch,"
1473 " my %d != server's %d\n",
1474 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001475 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001476 le32_to_cpu(con->out_connect.protocol_version),
1477 le32_to_cpu(con->in_reply.protocol_version));
1478 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001479 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001480 return -1;
1481
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001482 case CEPH_MSGR_TAG_BADAUTHORIZER:
1483 con->auth_retry++;
1484 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1485 con->auth_retry);
1486 if (con->auth_retry == 2) {
1487 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001488 return -1;
1489 }
1490 con->auth_retry = 1;
Alex Eldere2200422012-05-23 14:35:23 -05001491 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001492 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001493 if (ret < 0)
1494 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001495 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001496 break;
Sage Weil31b80062009-10-06 11:31:13 -07001497
1498 case CEPH_MSGR_TAG_RESETSESSION:
1499 /*
1500 * If we connected with a large connect_seq but the peer
1501 * has no record of a session with us (no connection, or
1502 * connect_seq == 0), they will send RESETSESION to indicate
1503 * that they must have reset their session, and may have
1504 * dropped messages.
1505 */
1506 dout("process_connect got RESET peer seq %u\n",
1507 le32_to_cpu(con->in_connect.connect_seq));
1508 pr_err("%s%lld %s connection reset\n",
1509 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001510 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001511 reset_connection(con);
Alex Eldere2200422012-05-23 14:35:23 -05001512 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001513 ret = prepare_write_connect(con);
1514 if (ret < 0)
1515 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001516 prepare_read_connect(con);
1517
1518 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001519 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001520 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1521 if (con->ops->peer_reset)
1522 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001523 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07001524 if (test_bit(CLOSED, &con->state) ||
1525 test_bit(OPENING, &con->state))
1526 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001527 break;
1528
1529 case CEPH_MSGR_TAG_RETRY_SESSION:
1530 /*
1531 * If we sent a smaller connect_seq than the peer has, try
1532 * again with a larger value.
1533 */
1534 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1535 le32_to_cpu(con->out_connect.connect_seq),
1536 le32_to_cpu(con->in_connect.connect_seq));
1537 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Alex Eldere2200422012-05-23 14:35:23 -05001538 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001539 ret = prepare_write_connect(con);
1540 if (ret < 0)
1541 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001542 prepare_read_connect(con);
1543 break;
1544
1545 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1546 /*
1547 * If we sent a smaller global_seq than the peer has, try
1548 * again with a larger value.
1549 */
Sage Weileed0ef22009-11-10 14:34:36 -08001550 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001551 con->peer_global_seq,
1552 le32_to_cpu(con->in_connect.global_seq));
1553 get_global_seq(con->msgr,
1554 le32_to_cpu(con->in_connect.global_seq));
Alex Eldere2200422012-05-23 14:35:23 -05001555 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001556 ret = prepare_write_connect(con);
1557 if (ret < 0)
1558 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001559 prepare_read_connect(con);
1560 break;
1561
1562 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001563 if (req_feat & ~server_feat) {
1564 pr_err("%s%lld %s protocol feature mismatch,"
1565 " my required %llx > server's %llx, need %llx\n",
1566 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001567 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001568 req_feat, server_feat, req_feat & ~server_feat);
1569 con->error_msg = "missing required protocol features";
1570 fail_protocol(con);
1571 return -1;
1572 }
Sage Weil31b80062009-10-06 11:31:13 -07001573 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001574 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1575 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001576 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001577 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1578 con->peer_global_seq,
1579 le32_to_cpu(con->in_reply.connect_seq),
1580 con->connect_seq);
1581 WARN_ON(con->connect_seq !=
1582 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001583
1584 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elder928443c2012-05-22 11:41:43 -05001585 set_bit(LOSSYTX, &con->flags);
Sage Weil92ac41d2009-12-14 14:56:56 -08001586
Sage Weil31b80062009-10-06 11:31:13 -07001587 prepare_read_tag(con);
1588 break;
1589
1590 case CEPH_MSGR_TAG_WAIT:
1591 /*
1592 * If there is a connection race (we are opening
1593 * connections to each other), one of us may just have
1594 * to WAIT. This shouldn't happen if we are the
1595 * client.
1596 */
Sage Weil04177882011-05-12 15:33:17 -07001597 pr_err("process_connect got WAIT as client\n");
1598 con->error_msg = "protocol error, got WAIT as client";
1599 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001600
1601 default:
1602 pr_err("connect protocol error, will retry\n");
1603 con->error_msg = "protocol error, garbage tag during connect";
1604 return -1;
1605 }
1606 return 0;
1607}
1608
1609
1610/*
1611 * read (part of) an ack
1612 */
1613static int read_partial_ack(struct ceph_connection *con)
1614{
Alex Elderfd516532012-05-10 10:29:50 -05001615 int size = sizeof (con->in_temp_ack);
1616 int end = size;
1617
1618 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001619}
1620
1621
1622/*
1623 * We can finally discard anything that's been acked.
1624 */
1625static void process_ack(struct ceph_connection *con)
1626{
1627 struct ceph_msg *m;
1628 u64 ack = le64_to_cpu(con->in_temp_ack);
1629 u64 seq;
1630
Sage Weil31b80062009-10-06 11:31:13 -07001631 while (!list_empty(&con->out_sent)) {
1632 m = list_first_entry(&con->out_sent, struct ceph_msg,
1633 list_head);
1634 seq = le64_to_cpu(m->hdr.seq);
1635 if (seq > ack)
1636 break;
1637 dout("got ack for seq %llu type %d at %p\n", seq,
1638 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001639 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001640 ceph_msg_remove(m);
1641 }
Sage Weil31b80062009-10-06 11:31:13 -07001642 prepare_read_tag(con);
1643}
1644
1645
1646
1647
Yehuda Sadeh24504182010-01-08 13:58:34 -08001648static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001649 struct kvec *section,
1650 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001651{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001652 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001653
Yehuda Sadeh24504182010-01-08 13:58:34 -08001654 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001655
Yehuda Sadeh24504182010-01-08 13:58:34 -08001656 while (section->iov_len < sec_len) {
1657 BUG_ON(section->iov_base == NULL);
1658 left = sec_len - section->iov_len;
1659 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1660 section->iov_len, left);
1661 if (ret <= 0)
1662 return ret;
1663 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001664 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001665 if (section->iov_len == sec_len)
1666 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001667
1668 return 1;
1669}
1670
Alex Elder1c20f2d2012-06-04 14:43:32 -05001671static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
1672 struct ceph_msg_header *hdr);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001673
1674
1675static int read_partial_message_pages(struct ceph_connection *con,
1676 struct page **pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001677 unsigned data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001678{
1679 void *p;
1680 int ret;
1681 int left;
1682
1683 left = min((int)(data_len - con->in_msg_pos.data_pos),
1684 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1685 /* (page) data */
1686 BUG_ON(pages == NULL);
1687 p = kmap(pages[con->in_msg_pos.page]);
1688 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1689 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001690 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001691 con->in_data_crc =
1692 crc32c(con->in_data_crc,
1693 p + con->in_msg_pos.page_pos, ret);
1694 kunmap(pages[con->in_msg_pos.page]);
1695 if (ret <= 0)
1696 return ret;
1697 con->in_msg_pos.data_pos += ret;
1698 con->in_msg_pos.page_pos += ret;
1699 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1700 con->in_msg_pos.page_pos = 0;
1701 con->in_msg_pos.page++;
1702 }
1703
1704 return ret;
1705}
1706
1707#ifdef CONFIG_BLOCK
1708static int read_partial_message_bio(struct ceph_connection *con,
1709 struct bio **bio_iter, int *bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001710 unsigned data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001711{
1712 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1713 void *p;
1714 int ret, left;
1715
1716 if (IS_ERR(bv))
1717 return PTR_ERR(bv);
1718
1719 left = min((int)(data_len - con->in_msg_pos.data_pos),
1720 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1721
1722 p = kmap(bv->bv_page) + bv->bv_offset;
1723
1724 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1725 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001726 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001727 con->in_data_crc =
1728 crc32c(con->in_data_crc,
1729 p + con->in_msg_pos.page_pos, ret);
1730 kunmap(bv->bv_page);
1731 if (ret <= 0)
1732 return ret;
1733 con->in_msg_pos.data_pos += ret;
1734 con->in_msg_pos.page_pos += ret;
1735 if (con->in_msg_pos.page_pos == bv->bv_len) {
1736 con->in_msg_pos.page_pos = 0;
1737 iter_bio_next(bio_iter, bio_seg);
1738 }
1739
1740 return ret;
1741}
1742#endif
1743
Sage Weil31b80062009-10-06 11:31:13 -07001744/*
1745 * read (part of) a message.
1746 */
1747static int read_partial_message(struct ceph_connection *con)
1748{
1749 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05001750 int size;
1751 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001752 int ret;
Sage Weilc5c6b192010-11-09 12:40:00 -08001753 unsigned front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001754 bool do_datacrc = !con->msgr->nocrc;
Sage Weilae187562010-04-22 07:47:01 -07001755 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001756 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001757
1758 dout("read_partial_message con %p msg %p\n", con, m);
1759
1760 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05001761 size = sizeof (con->in_hdr);
1762 end = size;
1763 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05001764 if (ret <= 0)
1765 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001766
1767 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1768 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1769 pr_err("read_partial_message bad hdr "
1770 " crc %u != expected %u\n",
1771 crc, con->in_hdr.crc);
1772 return -EBADMSG;
1773 }
1774
Sage Weil31b80062009-10-06 11:31:13 -07001775 front_len = le32_to_cpu(con->in_hdr.front_len);
1776 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1777 return -EIO;
1778 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1779 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1780 return -EIO;
1781 data_len = le32_to_cpu(con->in_hdr.data_len);
1782 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1783 return -EIO;
1784
Sage Weilae187562010-04-22 07:47:01 -07001785 /* verify seq# */
1786 seq = le64_to_cpu(con->in_hdr.seq);
1787 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001788 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001789 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001790 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001791 seq, con->in_seq + 1);
1792 con->in_base_pos = -front_len - middle_len - data_len -
1793 sizeof(m->footer);
1794 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001795 return 0;
1796 } else if ((s64)seq - (s64)con->in_seq > 1) {
1797 pr_err("read_partial_message bad seq %lld expected %lld\n",
1798 seq, con->in_seq + 1);
1799 con->error_msg = "bad message sequence # for incoming message";
1800 return -EBADMSG;
1801 }
1802
Sage Weil31b80062009-10-06 11:31:13 -07001803 /* allocate message? */
1804 if (!con->in_msg) {
1805 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1806 con->in_hdr.front_len, con->in_hdr.data_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05001807 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
Sage Weil31b80062009-10-06 11:31:13 -07001808 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001809 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001810 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001811 con->in_base_pos = -front_len - middle_len - data_len -
1812 sizeof(m->footer);
1813 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001814 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001815 return 0;
1816 }
Sage Weila79832f2010-04-01 16:06:19 -07001817 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001818 con->error_msg =
1819 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001820 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001821 }
Alex Elder38941f82012-06-01 14:56:43 -05001822
1823 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07001824 m = con->in_msg;
1825 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001826 if (m->middle)
1827 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001828
1829 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001830 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001831 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001832 else
1833 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001834 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001835 }
1836
1837 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001838 ret = read_partial_message_section(con, &m->front, front_len,
1839 &con->in_front_crc);
1840 if (ret <= 0)
1841 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001842
1843 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001844 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001845 ret = read_partial_message_section(con, &m->middle->vec,
1846 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001847 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001848 if (ret <= 0)
1849 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001850 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001851#ifdef CONFIG_BLOCK
1852 if (m->bio && !m->bio_iter)
1853 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1854#endif
Sage Weil31b80062009-10-06 11:31:13 -07001855
1856 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001857 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001858 if (m->pages) {
1859 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001860 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001861 if (ret <= 0)
1862 return ret;
1863#ifdef CONFIG_BLOCK
1864 } else if (m->bio) {
1865
1866 ret = read_partial_message_bio(con,
1867 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001868 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001869 if (ret <= 0)
1870 return ret;
1871#endif
1872 } else {
1873 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001874 }
1875 }
1876
Sage Weil31b80062009-10-06 11:31:13 -07001877 /* footer */
Alex Elderfd516532012-05-10 10:29:50 -05001878 size = sizeof (m->footer);
1879 end += size;
1880 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05001881 if (ret <= 0)
1882 return ret;
1883
Sage Weil31b80062009-10-06 11:31:13 -07001884 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1885 m, front_len, m->footer.front_crc, middle_len,
1886 m->footer.middle_crc, data_len, m->footer.data_crc);
1887
1888 /* crc ok? */
1889 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1890 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1891 m, con->in_front_crc, m->footer.front_crc);
1892 return -EBADMSG;
1893 }
1894 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1895 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1896 m, con->in_middle_crc, m->footer.middle_crc);
1897 return -EBADMSG;
1898 }
Alex Elderbca064d2012-02-15 07:43:54 -06001899 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001900 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1901 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1902 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1903 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1904 return -EBADMSG;
1905 }
1906
1907 return 1; /* done! */
1908}
1909
1910/*
1911 * Process message. This happens in the worker thread. The callback should
1912 * be careful not to do anything that waits on other incoming messages or it
1913 * may deadlock.
1914 */
1915static void process_message(struct ceph_connection *con)
1916{
Sage Weil5e095e82009-12-14 14:30:34 -08001917 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001918
Alex Elder38941f82012-06-01 14:56:43 -05001919 BUG_ON(con->in_msg->con != con);
1920 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08001921 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001922 con->in_msg = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05001923 ceph_con_put(con);
Sage Weil31b80062009-10-06 11:31:13 -07001924
1925 /* if first message, set peer_name */
1926 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001927 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001928
Sage Weil31b80062009-10-06 11:31:13 -07001929 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001930 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001931
1932 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1933 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001934 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001935 le16_to_cpu(msg->hdr.type),
1936 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1937 le32_to_cpu(msg->hdr.front_len),
1938 le32_to_cpu(msg->hdr.data_len),
1939 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1940 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001941
1942 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001943 prepare_read_tag(con);
1944}
1945
1946
1947/*
1948 * Write something to the socket. Called in a worker thread when the
1949 * socket appears to be writeable and we have something ready to send.
1950 */
1951static int try_write(struct ceph_connection *con)
1952{
Sage Weil31b80062009-10-06 11:31:13 -07001953 int ret = 1;
1954
1955 dout("try_write start %p state %lu nref %d\n", con, con->state,
1956 atomic_read(&con->nref));
1957
Sage Weil31b80062009-10-06 11:31:13 -07001958more:
1959 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1960
1961 /* open the socket first? */
1962 if (con->sock == NULL) {
Alex Eldera5988c42012-05-29 11:04:58 -05001963 clear_bit(NEGOTIATING, &con->state);
1964 set_bit(CONNECTING, &con->state);
1965
Alex Eldere2200422012-05-23 14:35:23 -05001966 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001967 prepare_write_banner(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001968 ret = prepare_write_connect(con);
1969 if (ret < 0)
1970 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08001971 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001972
Sage Weilcf3e5c42009-12-11 09:48:05 -08001973 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001974 con->in_tag = CEPH_MSGR_TAG_READY;
1975 dout("try_write initiating connect on %p new state %lu\n",
1976 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06001977 ret = ceph_tcp_connect(con);
1978 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07001979 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07001980 goto out;
1981 }
1982 }
1983
1984more_kvec:
1985 /* kvec data queued? */
1986 if (con->out_skip) {
1987 ret = write_partial_skip(con);
1988 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001989 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001990 }
1991 if (con->out_kvec_left) {
1992 ret = write_partial_kvec(con);
1993 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001994 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001995 }
1996
1997 /* msg pages? */
1998 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001999 if (con->out_msg_done) {
2000 ceph_msg_put(con->out_msg);
2001 con->out_msg = NULL; /* we're done with this one */
2002 goto do_next;
2003 }
2004
Sage Weil31b80062009-10-06 11:31:13 -07002005 ret = write_partial_msg_pages(con);
2006 if (ret == 1)
2007 goto more_kvec; /* we need to send the footer, too! */
2008 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002009 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002010 if (ret < 0) {
2011 dout("try_write write_partial_msg_pages err %d\n",
2012 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002013 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002014 }
2015 }
2016
Sage Weilc86a2932009-12-14 14:04:30 -08002017do_next:
Sage Weil31b80062009-10-06 11:31:13 -07002018 if (!test_bit(CONNECTING, &con->state)) {
2019 /* is anything else pending? */
2020 if (!list_empty(&con->out_queue)) {
2021 prepare_write_message(con);
2022 goto more;
2023 }
2024 if (con->in_seq > con->in_seq_acked) {
2025 prepare_write_ack(con);
2026 goto more;
2027 }
Alex Elder928443c2012-05-22 11:41:43 -05002028 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002029 prepare_write_keepalive(con);
2030 goto more;
2031 }
2032 }
2033
2034 /* Nothing to do! */
Alex Elder928443c2012-05-22 11:41:43 -05002035 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002036 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002037 ret = 0;
2038out:
Sage Weil42961d22011-01-25 08:19:34 -08002039 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002040 return ret;
2041}
2042
2043
2044
2045/*
2046 * Read what we can from the socket.
2047 */
2048static int try_read(struct ceph_connection *con)
2049{
Sage Weil31b80062009-10-06 11:31:13 -07002050 int ret = -1;
2051
2052 if (!con->sock)
2053 return 0;
2054
2055 if (test_bit(STANDBY, &con->state))
2056 return 0;
2057
2058 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08002059
Sage Weil31b80062009-10-06 11:31:13 -07002060more:
2061 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2062 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002063
2064 /*
2065 * process_connect and process_message drop and re-take
2066 * con->mutex. make sure we handle a racing close or reopen.
2067 */
2068 if (test_bit(CLOSED, &con->state) ||
2069 test_bit(OPENING, &con->state)) {
2070 ret = -EAGAIN;
2071 goto out;
2072 }
2073
Sage Weil31b80062009-10-06 11:31:13 -07002074 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08002075 if (!test_bit(NEGOTIATING, &con->state)) {
2076 dout("try_read connecting\n");
2077 ret = read_partial_banner(con);
2078 if (ret <= 0)
Sage Weileed0ef22009-11-10 14:34:36 -08002079 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002080 ret = process_banner(con);
2081 if (ret < 0)
2082 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08002083 }
Sage Weil31b80062009-10-06 11:31:13 -07002084 ret = read_partial_connect(con);
2085 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002086 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002087 ret = process_connect(con);
2088 if (ret < 0)
2089 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002090 goto more;
2091 }
2092
2093 if (con->in_base_pos < 0) {
2094 /*
2095 * skipping + discarding content.
2096 *
2097 * FIXME: there must be a better way to do this!
2098 */
Alex Elder84495f42012-02-15 07:43:55 -06002099 static char buf[SKIP_BUF_SIZE];
2100 int skip = min((int) sizeof (buf), -con->in_base_pos);
2101
Sage Weil31b80062009-10-06 11:31:13 -07002102 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2103 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2104 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002105 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002106 con->in_base_pos += ret;
2107 if (con->in_base_pos)
2108 goto more;
2109 }
2110 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2111 /*
2112 * what's next?
2113 */
2114 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2115 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002116 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002117 dout("try_read got tag %d\n", (int)con->in_tag);
2118 switch (con->in_tag) {
2119 case CEPH_MSGR_TAG_MSG:
2120 prepare_read_message(con);
2121 break;
2122 case CEPH_MSGR_TAG_ACK:
2123 prepare_read_ack(con);
2124 break;
2125 case CEPH_MSGR_TAG_CLOSE:
2126 set_bit(CLOSED, &con->state); /* fixme */
Sage Weil98bdb0a2011-01-25 08:17:48 -08002127 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002128 default:
2129 goto bad_tag;
2130 }
2131 }
2132 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2133 ret = read_partial_message(con);
2134 if (ret <= 0) {
2135 switch (ret) {
2136 case -EBADMSG:
2137 con->error_msg = "bad crc";
2138 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002139 break;
Sage Weil31b80062009-10-06 11:31:13 -07002140 case -EIO:
2141 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002142 break;
Sage Weil31b80062009-10-06 11:31:13 -07002143 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002144 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002145 }
2146 if (con->in_tag == CEPH_MSGR_TAG_READY)
2147 goto more;
2148 process_message(con);
2149 goto more;
2150 }
2151 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2152 ret = read_partial_ack(con);
2153 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002154 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002155 process_ack(con);
2156 goto more;
2157 }
2158
Sage Weil31b80062009-10-06 11:31:13 -07002159out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002160 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002161 return ret;
2162
2163bad_tag:
2164 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2165 con->error_msg = "protocol error, garbage tag";
2166 ret = -1;
2167 goto out;
2168}
2169
2170
2171/*
2172 * Atomically queue work on a connection. Bump @con reference to
2173 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002174 */
2175static void queue_con(struct ceph_connection *con)
2176{
Sage Weil31b80062009-10-06 11:31:13 -07002177 if (!con->ops->get(con)) {
2178 dout("queue_con %p ref count 0\n", con);
2179 return;
2180 }
2181
Tejun Heof363e45fd12011-01-03 14:49:46 +01002182 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002183 dout("queue_con %p - already queued\n", con);
2184 con->ops->put(con);
2185 } else {
2186 dout("queue_con %p\n", con);
2187 }
2188}
2189
2190/*
2191 * Do some work on a connection. Drop a connection ref when we're done.
2192 */
2193static void con_work(struct work_struct *work)
2194{
2195 struct ceph_connection *con = container_of(work, struct ceph_connection,
2196 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002197 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002198
Sage Weil9dd46582010-04-28 13:51:50 -07002199 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002200restart:
Alex Elder928443c2012-05-22 11:41:43 -05002201 if (test_and_clear_bit(BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002202 dout("con_work %p backing off\n", con);
2203 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2204 round_jiffies_relative(con->delay))) {
2205 dout("con_work %p backoff %lu\n", con, con->delay);
2206 mutex_unlock(&con->mutex);
2207 return;
2208 } else {
2209 con->ops->put(con);
2210 dout("con_work %p FAILED to back off %lu\n", con,
2211 con->delay);
2212 }
2213 }
Sage Weil9dd46582010-04-28 13:51:50 -07002214
Sage Weile00de342011-03-04 12:25:05 -08002215 if (test_bit(STANDBY, &con->state)) {
2216 dout("con_work %p STANDBY\n", con);
2217 goto done;
2218 }
Sage Weil31b80062009-10-06 11:31:13 -07002219 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2220 dout("con_work CLOSED\n");
2221 con_close_socket(con);
2222 goto done;
2223 }
2224 if (test_and_clear_bit(OPENING, &con->state)) {
2225 /* reopen w/ new peer */
2226 dout("con_work OPENING\n");
2227 con_close_socket(con);
2228 }
2229
Alex Elder928443c2012-05-22 11:41:43 -05002230 if (test_and_clear_bit(SOCK_CLOSED, &con->flags))
Sage Weil0da5d702011-05-19 11:21:05 -07002231 goto fault;
2232
2233 ret = try_read(con);
2234 if (ret == -EAGAIN)
2235 goto restart;
2236 if (ret < 0)
2237 goto fault;
2238
2239 ret = try_write(con);
2240 if (ret == -EAGAIN)
2241 goto restart;
2242 if (ret < 0)
2243 goto fault;
Sage Weil31b80062009-10-06 11:31:13 -07002244
2245done:
Sage Weil9dd46582010-04-28 13:51:50 -07002246 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002247done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002248 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002249 return;
2250
2251fault:
2252 mutex_unlock(&con->mutex);
2253 ceph_fault(con); /* error/fault path */
2254 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002255}
2256
2257
2258/*
2259 * Generic error/fault handler. A retry mechanism is used with
2260 * exponential backoff
2261 */
2262static void ceph_fault(struct ceph_connection *con)
2263{
2264 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002265 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002266 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002267 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002268
Alex Elder928443c2012-05-22 11:41:43 -05002269 if (test_bit(LOSSYTX, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002270 dout("fault on LOSSYTX channel\n");
2271 goto out;
2272 }
2273
Sage Weilec302642009-12-22 10:43:42 -08002274 mutex_lock(&con->mutex);
Sage Weil91e45ce32010-02-15 12:05:09 -08002275 if (test_bit(CLOSED, &con->state))
2276 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002277
Sage Weil31b80062009-10-06 11:31:13 -07002278 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002279
2280 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002281 BUG_ON(con->in_msg->con != con);
2282 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08002283 ceph_msg_put(con->in_msg);
2284 con->in_msg = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05002285 ceph_con_put(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002286 }
Sage Weil31b80062009-10-06 11:31:13 -07002287
Sage Weile80a52d2010-02-25 12:40:45 -08002288 /* Requeue anything that hasn't been acked */
2289 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002290
Sage Weile76661d2011-03-03 10:10:15 -08002291 /* If there are no messages queued or keepalive pending, place
2292 * the connection in a STANDBY state */
2293 if (list_empty(&con->out_queue) &&
Alex Elder928443c2012-05-22 11:41:43 -05002294 !test_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weile00de342011-03-04 12:25:05 -08002295 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elder928443c2012-05-22 11:41:43 -05002296 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002297 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08002298 } else {
2299 /* retry after a delay. */
2300 if (con->delay == 0)
2301 con->delay = BASE_DELAY_INTERVAL;
2302 else if (con->delay < MAX_DELAY_INTERVAL)
2303 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002304 con->ops->get(con);
2305 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002306 round_jiffies_relative(con->delay))) {
2307 dout("fault queued %p delay %lu\n", con, con->delay);
2308 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002309 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002310 dout("fault failed to queue %p delay %lu, backoff\n",
2311 con, con->delay);
2312 /*
2313 * In many cases we see a socket state change
2314 * while con_work is running and end up
2315 * queuing (non-delayed) work, such that we
2316 * can't backoff with a delay. Set a flag so
2317 * that when con_work restarts we schedule the
2318 * delay then.
2319 */
Alex Elder928443c2012-05-22 11:41:43 -05002320 set_bit(BACKOFF, &con->flags);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002321 }
Sage Weil31b80062009-10-06 11:31:13 -07002322 }
2323
Sage Weil91e45ce32010-02-15 12:05:09 -08002324out_unlock:
2325 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002326out:
Sage Weil161fd652010-02-25 12:38:57 -08002327 /*
2328 * in case we faulted due to authentication, invalidate our
2329 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002330 */
Sage Weil161fd652010-02-25 12:38:57 -08002331 if (con->auth_retry && con->ops->invalidate_authorizer) {
2332 dout("calling invalidate_authorizer()\n");
2333 con->ops->invalidate_authorizer(con);
2334 }
2335
Sage Weil31b80062009-10-06 11:31:13 -07002336 if (con->ops->fault)
2337 con->ops->fault(con);
2338}
2339
2340
2341
2342/*
Alex Elder15d98822012-05-26 23:26:43 -05002343 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002344 */
Alex Elder15d98822012-05-26 23:26:43 -05002345void ceph_messenger_init(struct ceph_messenger *msgr,
2346 struct ceph_entity_addr *myaddr,
2347 u32 supported_features,
2348 u32 required_features,
2349 bool nocrc)
Sage Weil31b80062009-10-06 11:31:13 -07002350{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002351 msgr->supported_features = supported_features;
2352 msgr->required_features = required_features;
2353
Sage Weil31b80062009-10-06 11:31:13 -07002354 spin_lock_init(&msgr->global_seq_lock);
2355
Sage Weil31b80062009-10-06 11:31:13 -07002356 if (myaddr)
2357 msgr->inst.addr = *myaddr;
2358
2359 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002360 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002361 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002362 encode_my_addr(msgr);
Alex Elder15d98822012-05-26 23:26:43 -05002363 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002364
Alex Elder15d98822012-05-26 23:26:43 -05002365 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002366}
Alex Elder15d98822012-05-26 23:26:43 -05002367EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002368
Sage Weile00de342011-03-04 12:25:05 -08002369static void clear_standby(struct ceph_connection *con)
2370{
2371 /* come back from STANDBY? */
2372 if (test_and_clear_bit(STANDBY, &con->state)) {
2373 mutex_lock(&con->mutex);
2374 dout("clear_standby %p and ++connect_seq\n", con);
2375 con->connect_seq++;
Alex Elder928443c2012-05-22 11:41:43 -05002376 WARN_ON(test_bit(WRITE_PENDING, &con->flags));
2377 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags));
Sage Weile00de342011-03-04 12:25:05 -08002378 mutex_unlock(&con->mutex);
2379 }
2380}
2381
Sage Weil31b80062009-10-06 11:31:13 -07002382/*
2383 * Queue up an outgoing message on the given connection.
2384 */
2385void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2386{
2387 if (test_bit(CLOSED, &con->state)) {
2388 dout("con_send %p closed, dropping %p\n", con, msg);
2389 ceph_msg_put(msg);
2390 return;
2391 }
2392
2393 /* set src+dst */
Sage Weildbad1852010-03-25 15:45:38 -07002394 msg->hdr.src = con->msgr->inst.name;
Sage Weil31b80062009-10-06 11:31:13 -07002395
Sage Weil3ca02ef2010-03-01 15:25:00 -08002396 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2397
Sage Weile84346b2010-05-11 21:20:38 -07002398 msg->needs_out_seq = true;
2399
Sage Weil31b80062009-10-06 11:31:13 -07002400 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08002401 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002402
Alex Elder38941f82012-06-01 14:56:43 -05002403 BUG_ON(msg->con != NULL);
Alex Elder92ce0342012-06-04 14:43:33 -05002404 msg->con = ceph_con_get(con);
2405 BUG_ON(msg->con == NULL);
2406
Sage Weil31b80062009-10-06 11:31:13 -07002407 BUG_ON(!list_empty(&msg->list_head));
2408 list_add_tail(&msg->list_head, &con->out_queue);
2409 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2410 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2411 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2412 le32_to_cpu(msg->hdr.front_len),
2413 le32_to_cpu(msg->hdr.middle_len),
2414 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002415 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002416
2417 /* if there wasn't anything waiting to send before, queue
2418 * new work */
Sage Weile00de342011-03-04 12:25:05 -08002419 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002420 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002421 queue_con(con);
2422}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002423EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002424
2425/*
2426 * Revoke a message that was previously queued for send
2427 */
Alex Elder6740a842012-06-01 14:56:43 -05002428void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002429{
Alex Elder6740a842012-06-01 14:56:43 -05002430 struct ceph_connection *con = msg->con;
2431
2432 if (!con)
2433 return; /* Message not in our possession */
2434
Sage Weilec302642009-12-22 10:43:42 -08002435 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002436 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05002437 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002438 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -05002439 BUG_ON(msg->con == NULL);
Alex Elder92ce0342012-06-04 14:43:33 -05002440 ceph_con_put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -05002441 msg->con = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05002442 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05002443
Sage Weil31b80062009-10-06 11:31:13 -07002444 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002445 }
2446 if (con->out_msg == msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002447 dout("%s %p msg %p - was sending\n", __func__, con, msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002448 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002449 if (con->out_kvec_is_msg) {
2450 con->out_skip = con->out_kvec_bytes;
2451 con->out_kvec_is_msg = false;
2452 }
Sage Weiled98ada2010-07-05 12:15:14 -07002453 msg->hdr.seq = 0;
Alex Elder92ce0342012-06-04 14:43:33 -05002454
2455 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07002456 }
Sage Weilec302642009-12-22 10:43:42 -08002457 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002458}
2459
2460/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002461 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002462 */
Alex Elder8921d112012-06-01 14:56:43 -05002463void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002464{
Alex Elder8921d112012-06-01 14:56:43 -05002465 struct ceph_connection *con;
2466
2467 BUG_ON(msg == NULL);
2468 if (!msg->con) {
2469 dout("%s msg %p null con\n", __func__, msg);
2470
2471 return; /* Message not in our possession */
2472 }
2473
2474 con = msg->con;
Sage Weil350b1c32009-12-22 10:45:45 -08002475 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05002476 if (con->in_msg == msg) {
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002477 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2478 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002479 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2480
2481 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05002482 dout("%s %p msg %p revoked\n", __func__, con, msg);
2483 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08002484 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002485 front_len -
2486 middle_len -
2487 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002488 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002489 ceph_msg_put(con->in_msg);
2490 con->in_msg = NULL;
2491 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002492 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002493 } else {
Alex Elder8921d112012-06-01 14:56:43 -05002494 dout("%s %p in_msg %p msg %p no-op\n",
2495 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002496 }
2497 mutex_unlock(&con->mutex);
2498}
2499
2500/*
Sage Weil31b80062009-10-06 11:31:13 -07002501 * Queue a keepalive byte to ensure the tcp connection is alive.
2502 */
2503void ceph_con_keepalive(struct ceph_connection *con)
2504{
Sage Weile00de342011-03-04 12:25:05 -08002505 dout("con_keepalive %p\n", con);
2506 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002507 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2508 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002509 queue_con(con);
2510}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002511EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002512
2513
2514/*
2515 * construct a new message with given type, size
2516 * the new msg has a ref count of 1.
2517 */
Sage Weilb61c2762011-08-09 15:03:46 -07002518struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2519 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002520{
2521 struct ceph_msg *m;
2522
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002523 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002524 if (m == NULL)
2525 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002526 kref_init(&m->kref);
Alex Elder38941f82012-06-01 14:56:43 -05002527
2528 m->con = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002529 INIT_LIST_HEAD(&m->list_head);
2530
Sage Weil45c6ceb2010-05-11 15:01:51 -07002531 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002532 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002533 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2534 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002535 m->hdr.front_len = cpu_to_le32(front_len);
2536 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002537 m->hdr.data_len = 0;
2538 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002539 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002540 m->footer.front_crc = 0;
2541 m->footer.middle_crc = 0;
2542 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002543 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002544 m->front_max = front_len;
2545 m->front_is_vmalloc = false;
2546 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002547 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002548 m->pool = NULL;
2549
Henry C Changca208922011-05-03 02:29:56 +00002550 /* middle */
2551 m->middle = NULL;
2552
2553 /* data */
2554 m->nr_pages = 0;
2555 m->page_alignment = 0;
2556 m->pages = NULL;
2557 m->pagelist = NULL;
2558 m->bio = NULL;
2559 m->bio_iter = NULL;
2560 m->bio_seg = 0;
2561 m->trail = NULL;
2562
Sage Weil31b80062009-10-06 11:31:13 -07002563 /* front */
2564 if (front_len) {
2565 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002566 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002567 PAGE_KERNEL);
2568 m->front_is_vmalloc = true;
2569 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002570 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002571 }
2572 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002573 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002574 front_len);
2575 goto out2;
2576 }
2577 } else {
2578 m->front.iov_base = NULL;
2579 }
2580 m->front.iov_len = front_len;
2581
Sage Weilbb257662010-04-01 16:07:23 -07002582 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002583 return m;
2584
2585out2:
2586 ceph_msg_put(m);
2587out:
Sage Weilb61c2762011-08-09 15:03:46 -07002588 if (!can_fail) {
2589 pr_err("msg_new can't create type %d front %d\n", type,
2590 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002591 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002592 } else {
2593 dout("msg_new can't create type %d front %d\n", type,
2594 front_len);
2595 }
Sage Weila79832f2010-04-01 16:06:19 -07002596 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002597}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002598EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002599
2600/*
Sage Weil31b80062009-10-06 11:31:13 -07002601 * Allocate "middle" portion of a message, if it is needed and wasn't
2602 * allocated by alloc_msg. This allows us to read a small fixed-size
2603 * per-type header in the front and then gracefully fail (i.e.,
2604 * propagate the error to the caller based on info in the front) when
2605 * the middle is too large.
2606 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002607static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002608{
2609 int type = le16_to_cpu(msg->hdr.type);
2610 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2611
2612 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2613 ceph_msg_type_name(type), middle_len);
2614 BUG_ON(!middle_len);
2615 BUG_ON(msg->middle);
2616
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002617 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002618 if (!msg->middle)
2619 return -ENOMEM;
2620 return 0;
2621}
2622
Yehuda Sadeh24504182010-01-08 13:58:34 -08002623/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05002624 * Allocate a message for receiving an incoming message on a
2625 * connection, and save the result in con->in_msg. Uses the
2626 * connection's private alloc_msg op if available.
2627 *
2628 * Returns true if the message should be skipped, false otherwise.
2629 * If true is returned (skip message), con->in_msg will be NULL.
2630 * If false is returned, con->in_msg will contain a pointer to the
2631 * newly-allocated message, or NULL in case of memory exhaustion.
Yehuda Sadeh24504182010-01-08 13:58:34 -08002632 */
Alex Elder1c20f2d2012-06-04 14:43:32 -05002633static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2634 struct ceph_msg_header *hdr)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002635{
2636 int type = le16_to_cpu(hdr->type);
2637 int front_len = le32_to_cpu(hdr->front_len);
2638 int middle_len = le32_to_cpu(hdr->middle_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002639 int ret;
2640
Alex Elder1c20f2d2012-06-04 14:43:32 -05002641 BUG_ON(con->in_msg != NULL);
2642
Yehuda Sadeh24504182010-01-08 13:58:34 -08002643 if (con->ops->alloc_msg) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002644 int skip = 0;
2645
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002646 mutex_unlock(&con->mutex);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002647 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002648 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002649 if (con->in_msg) {
2650 con->in_msg->con = ceph_con_get(con);
2651 BUG_ON(con->in_msg->con == NULL);
2652 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002653 if (skip)
2654 con->in_msg = NULL;
2655
2656 if (!con->in_msg)
2657 return skip != 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002658 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002659 if (!con->in_msg) {
2660 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2661 if (!con->in_msg) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002662 pr_err("unable to allocate msg type %d len %d\n",
2663 type, front_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002664 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002665 }
Alex Elder92ce0342012-06-04 14:43:33 -05002666 con->in_msg->con = ceph_con_get(con);
2667 BUG_ON(con->in_msg->con == NULL);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002668 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002669 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002670 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002671
Alex Elder1c20f2d2012-06-04 14:43:32 -05002672 if (middle_len && !con->in_msg->middle) {
2673 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002674 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002675 ceph_msg_put(con->in_msg);
2676 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002677 }
2678 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002679
Alex Elder1c20f2d2012-06-04 14:43:32 -05002680 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002681}
2682
Sage Weil31b80062009-10-06 11:31:13 -07002683
2684/*
2685 * Free a generically kmalloc'd message.
2686 */
2687void ceph_msg_kfree(struct ceph_msg *m)
2688{
2689 dout("msg_kfree %p\n", m);
2690 if (m->front_is_vmalloc)
2691 vfree(m->front.iov_base);
2692 else
2693 kfree(m->front.iov_base);
2694 kfree(m);
2695}
2696
2697/*
2698 * Drop a msg ref. Destroy as needed.
2699 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002700void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002701{
Sage Weilc2e552e2009-12-07 15:55:05 -08002702 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002703
Sage Weilc2e552e2009-12-07 15:55:05 -08002704 dout("ceph_msg_put last one on %p\n", m);
2705 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002706
Sage Weilc2e552e2009-12-07 15:55:05 -08002707 /* drop middle, data, if any */
2708 if (m->middle) {
2709 ceph_buffer_put(m->middle);
2710 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002711 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002712 m->nr_pages = 0;
2713 m->pages = NULL;
2714
Sage Weil58bb3b32009-12-23 12:12:31 -08002715 if (m->pagelist) {
2716 ceph_pagelist_release(m->pagelist);
2717 kfree(m->pagelist);
2718 m->pagelist = NULL;
2719 }
2720
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002721 m->trail = NULL;
2722
Sage Weilc2e552e2009-12-07 15:55:05 -08002723 if (m->pool)
2724 ceph_msgpool_put(m->pool, m);
2725 else
2726 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002727}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002728EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002729
2730void ceph_msg_dump(struct ceph_msg *msg)
2731{
2732 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2733 msg->front_max, msg->nr_pages);
2734 print_hex_dump(KERN_DEBUG, "header: ",
2735 DUMP_PREFIX_OFFSET, 16, 1,
2736 &msg->hdr, sizeof(msg->hdr), true);
2737 print_hex_dump(KERN_DEBUG, " front: ",
2738 DUMP_PREFIX_OFFSET, 16, 1,
2739 msg->front.iov_base, msg->front.iov_len, true);
2740 if (msg->middle)
2741 print_hex_dump(KERN_DEBUG, "middle: ",
2742 DUMP_PREFIX_OFFSET, 16, 1,
2743 msg->middle->vec.iov_base,
2744 msg->middle->vec.iov_len, true);
2745 print_hex_dump(KERN_DEBUG, "footer: ",
2746 DUMP_PREFIX_OFFSET, 16, 1,
2747 &msg->footer, sizeof(msg->footer), true);
2748}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002749EXPORT_SYMBOL(ceph_msg_dump);