blob: 56448660883d5dad2df9969ec41599492a75d1d8 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil31b80062009-10-06 11:31:13 -07002
3#include <linux/crc32c.h>
4#include <linux/ctype.h>
5#include <linux/highmem.h>
6#include <linux/inet.h>
7#include <linux/kthread.h>
8#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Sage Weil31b80062009-10-06 11:31:13 -070010#include <linux/socket.h>
11#include <linux/string.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070012#include <linux/bio.h>
13#include <linux/blkdev.h>
Noah Watkinsee3b56f2011-09-23 11:48:42 -070014#include <linux/dns_resolver.h>
Sage Weil31b80062009-10-06 11:31:13 -070015#include <net/tcp.h>
16
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070017#include <linux/ceph/libceph.h>
18#include <linux/ceph/messenger.h>
19#include <linux/ceph/decode.h>
20#include <linux/ceph/pagelist.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040021#include <linux/export.h>
Sage Weil31b80062009-10-06 11:31:13 -070022
23/*
24 * Ceph uses the messenger to exchange ceph_msg messages with other
25 * hosts in the system. The messenger provides ordered and reliable
26 * delivery. We tolerate TCP disconnects by reconnecting (with
27 * exponential backoff) in the case of a fault (disconnection, bad
28 * crc, protocol error). Acks allow sent messages to be discarded by
29 * the sender.
30 */
31
Alex Elderce2c8902012-05-22 22:15:49 -050032/* State values for ceph_connection->sock_state; NEW is assumed to be 0 */
33
34#define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
35#define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
36#define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
37#define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
38#define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
39
Sage Weil31b80062009-10-06 11:31:13 -070040/* static tag bytes (protocol control messages) */
41static char tag_msg = CEPH_MSGR_TAG_MSG;
42static char tag_ack = CEPH_MSGR_TAG_ACK;
43static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
44
Sage Weila6a53492010-04-13 14:07:07 -070045#ifdef CONFIG_LOCKDEP
46static struct lock_class_key socket_class;
47#endif
48
Alex Elder84495f42012-02-15 07:43:55 -060049/*
50 * When skipping (ignoring) a block of input we read it into a "skip
51 * buffer," which is this many bytes in size.
52 */
53#define SKIP_BUF_SIZE 1024
Sage Weil31b80062009-10-06 11:31:13 -070054
55static void queue_con(struct ceph_connection *con);
56static void con_work(struct work_struct *);
57static void ceph_fault(struct ceph_connection *con);
58
Sage Weil31b80062009-10-06 11:31:13 -070059/*
Alex Elderf64a9312012-01-23 15:49:27 -060060 * Nicely render a sockaddr as a string. An array of formatted
61 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -070062 */
Alex Elderf64a9312012-01-23 15:49:27 -060063#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
64#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
65#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
66#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
67
68static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
69static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -070070
Alex Elder57666512012-01-23 15:49:27 -060071static struct page *zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -060072
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070073const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -070074{
75 int i;
76 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -060077 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
78 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -070079
Alex Elderf64a9312012-01-23 15:49:27 -060080 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -070081 s = addr_str[i];
82
83 switch (ss->ss_family) {
84 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -060085 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
86 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -070087 break;
88
89 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -060090 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
91 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -070092 break;
93
94 default:
Alex Elderd3002b92012-02-14 14:05:33 -060095 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
96 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -070097 }
98
99 return s;
100}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700101EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700102
Sage Weil63f2d212009-11-03 15:17:56 -0800103static void encode_my_addr(struct ceph_messenger *msgr)
104{
105 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
106 ceph_encode_addr(&msgr->my_enc_addr);
107}
108
Sage Weil31b80062009-10-06 11:31:13 -0700109/*
110 * work queue for all reading and writing to/from the socket.
111 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600112static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700113
Alex Elder6173d1f2012-02-14 14:05:33 -0600114void _ceph_msgr_exit(void)
115{
Alex Elderd3002b92012-02-14 14:05:33 -0600116 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600117 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600118 ceph_msgr_wq = NULL;
119 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600120
Alex Elder6173d1f2012-02-14 14:05:33 -0600121 BUG_ON(zero_page == NULL);
122 kunmap(zero_page);
123 page_cache_release(zero_page);
124 zero_page = NULL;
125}
126
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700127int ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700128{
Alex Elder57666512012-01-23 15:49:27 -0600129 BUG_ON(zero_page != NULL);
130 zero_page = ZERO_PAGE(0);
131 page_cache_get(zero_page);
132
Tejun Heof363e452011-01-03 14:49:46 +0100133 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600134 if (ceph_msgr_wq)
135 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600136
Alex Elder6173d1f2012-02-14 14:05:33 -0600137 pr_err("msgr_init failed to create workqueue\n");
138 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600139
Alex Elder6173d1f2012-02-14 14:05:33 -0600140 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700141}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700142EXPORT_SYMBOL(ceph_msgr_init);
Sage Weil31b80062009-10-06 11:31:13 -0700143
144void ceph_msgr_exit(void)
145{
Alex Elder57666512012-01-23 15:49:27 -0600146 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600147
Alex Elder6173d1f2012-02-14 14:05:33 -0600148 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700149}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700150EXPORT_SYMBOL(ceph_msgr_exit);
Sage Weil31b80062009-10-06 11:31:13 -0700151
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700152void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700153{
154 flush_workqueue(ceph_msgr_wq);
155}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700156EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700157
Alex Elderce2c8902012-05-22 22:15:49 -0500158/* Connection socket state transition functions */
159
160static void con_sock_state_init(struct ceph_connection *con)
161{
162 int old_state;
163
164 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
165 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
166 printk("%s: unexpected old state %d\n", __func__, old_state);
167}
168
169static void con_sock_state_connecting(struct ceph_connection *con)
170{
171 int old_state;
172
173 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
174 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
175 printk("%s: unexpected old state %d\n", __func__, old_state);
176}
177
178static void con_sock_state_connected(struct ceph_connection *con)
179{
180 int old_state;
181
182 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
183 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
184 printk("%s: unexpected old state %d\n", __func__, old_state);
185}
186
187static void con_sock_state_closing(struct ceph_connection *con)
188{
189 int old_state;
190
191 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
192 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
193 old_state != CON_SOCK_STATE_CONNECTED &&
194 old_state != CON_SOCK_STATE_CLOSING))
195 printk("%s: unexpected old state %d\n", __func__, old_state);
196}
197
198static void con_sock_state_closed(struct ceph_connection *con)
199{
200 int old_state;
201
202 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
203 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
204 old_state != CON_SOCK_STATE_CLOSING))
205 printk("%s: unexpected old state %d\n", __func__, old_state);
206}
Sage Weila922d382010-05-29 09:41:23 -0700207
Sage Weil31b80062009-10-06 11:31:13 -0700208/*
209 * socket callback functions
210 */
211
212/* data available on socket, or listen socket received a connect */
Alex Elder327800b2012-05-22 11:41:43 -0500213static void ceph_sock_data_ready(struct sock *sk, int count_unused)
Sage Weil31b80062009-10-06 11:31:13 -0700214{
Alex Elderbd406142012-01-23 15:49:27 -0600215 struct ceph_connection *con = sk->sk_user_data;
216
Sage Weil31b80062009-10-06 11:31:13 -0700217 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500218 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700219 con, con->state);
220 queue_con(con);
221 }
222}
223
224/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500225static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700226{
Alex Elderd3002b92012-02-14 14:05:33 -0600227 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700228
Jim Schutt182fac22012-02-29 08:30:58 -0700229 /* only queue to workqueue if there is data we want to write,
230 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500231 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700232 * doesn't get called again until try_write() fills the socket
233 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
234 * and net/core/stream.c:sk_stream_write_space().
235 */
Alex Elder928443c2012-05-22 11:41:43 -0500236 if (test_bit(WRITE_PENDING, &con->flags)) {
Jim Schutt182fac22012-02-29 08:30:58 -0700237 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500238 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700239 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
240 queue_con(con);
241 }
Sage Weil31b80062009-10-06 11:31:13 -0700242 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500243 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700244 }
Sage Weil31b80062009-10-06 11:31:13 -0700245}
246
247/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500248static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700249{
Alex Elderbd406142012-01-23 15:49:27 -0600250 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700251
Alex Elder327800b2012-05-22 11:41:43 -0500252 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700253 con, con->state, sk->sk_state);
254
255 if (test_bit(CLOSED, &con->state))
256 return;
257
258 switch (sk->sk_state) {
259 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500260 dout("%s TCP_CLOSE\n", __func__);
Sage Weil31b80062009-10-06 11:31:13 -0700261 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500262 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500263 con_sock_state_closing(con);
Alex Elder928443c2012-05-22 11:41:43 -0500264 if (test_and_set_bit(SOCK_CLOSED, &con->flags) == 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700265 if (test_bit(CONNECTING, &con->state))
266 con->error_msg = "connection failed";
267 else
268 con->error_msg = "socket closed";
269 queue_con(con);
270 }
271 break;
272 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500273 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500274 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700275 queue_con(con);
276 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600277 default: /* Everything else is uninteresting */
278 break;
Sage Weil31b80062009-10-06 11:31:13 -0700279 }
280}
281
282/*
283 * set up socket callbacks
284 */
285static void set_sock_callbacks(struct socket *sock,
286 struct ceph_connection *con)
287{
288 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600289 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500290 sk->sk_data_ready = ceph_sock_data_ready;
291 sk->sk_write_space = ceph_sock_write_space;
292 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700293}
294
295
296/*
297 * socket helpers
298 */
299
300/*
301 * initiate connection to a remote socket.
302 */
Alex Elder41617d02012-02-14 14:05:33 -0600303static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700304{
Sage Weilf91d3472010-07-01 15:18:31 -0700305 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700306 struct socket *sock;
307 int ret;
308
309 BUG_ON(con->sock);
Sage Weilf91d3472010-07-01 15:18:31 -0700310 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
311 IPPROTO_TCP, &sock);
Sage Weil31b80062009-10-06 11:31:13 -0700312 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600313 return ret;
Sage Weil31b80062009-10-06 11:31:13 -0700314 sock->sk->sk_allocation = GFP_NOFS;
315
Sage Weila6a53492010-04-13 14:07:07 -0700316#ifdef CONFIG_LOCKDEP
317 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
318#endif
319
Sage Weil31b80062009-10-06 11:31:13 -0700320 set_sock_callbacks(sock, con);
321
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700322 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700323
Sage Weil89a86be2012-06-09 14:19:21 -0700324 con_sock_state_connecting(con);
Sage Weilf91d3472010-07-01 15:18:31 -0700325 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
326 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700327 if (ret == -EINPROGRESS) {
328 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700329 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700330 sock->sk->sk_state);
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600331 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700332 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700333 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700334 sock_release(sock);
Sage Weil31b80062009-10-06 11:31:13 -0700335 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -0700336
Alex Elder41617d02012-02-14 14:05:33 -0600337 return ret;
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600338 }
339 con->sock = sock;
Alex Elder41617d02012-02-14 14:05:33 -0600340 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700341}
342
343static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
344{
345 struct kvec iov = {buf, len};
346 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800347 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700348
Sage Weil98bdb0a2011-01-25 08:17:48 -0800349 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
350 if (r == -EAGAIN)
351 r = 0;
352 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700353}
354
355/*
356 * write something. @more is true if caller will be sending more data
357 * shortly.
358 */
359static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
360 size_t kvlen, size_t len, int more)
361{
362 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800363 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700364
365 if (more)
366 msg.msg_flags |= MSG_MORE;
367 else
368 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
369
Sage Weil42961d22011-01-25 08:19:34 -0800370 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
371 if (r == -EAGAIN)
372 r = 0;
373 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700374}
375
Alex Elder31739132012-03-07 11:40:08 -0600376static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
377 int offset, size_t size, int more)
378{
379 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
380 int ret;
381
382 ret = kernel_sendpage(sock, page, offset, size, flags);
383 if (ret == -EAGAIN)
384 ret = 0;
385
386 return ret;
387}
388
Sage Weil31b80062009-10-06 11:31:13 -0700389
390/*
391 * Shutdown/close the socket for the given connection.
392 */
393static int con_close_socket(struct ceph_connection *con)
394{
395 int rc;
396
397 dout("con_close_socket on %p sock %p\n", con, con->sock);
398 if (!con->sock)
399 return 0;
400 set_bit(SOCK_CLOSED, &con->state);
401 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
402 sock_release(con->sock);
403 con->sock = NULL;
404 clear_bit(SOCK_CLOSED, &con->state);
Alex Elderce2c8902012-05-22 22:15:49 -0500405 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700406 return rc;
407}
408
409/*
410 * Reset a connection. Discard all incoming and outgoing messages
411 * and clear *_seq state.
412 */
413static void ceph_msg_remove(struct ceph_msg *msg)
414{
415 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500416 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -0700417 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -0500418 msg->con = NULL;
419
Sage Weil31b80062009-10-06 11:31:13 -0700420 ceph_msg_put(msg);
421}
422static void ceph_msg_remove_list(struct list_head *head)
423{
424 while (!list_empty(head)) {
425 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
426 list_head);
427 ceph_msg_remove(msg);
428 }
429}
430
431static void reset_connection(struct ceph_connection *con)
432{
433 /* reset connection, out_queue, msg_ and connect_seq */
434 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700435 ceph_msg_remove_list(&con->out_queue);
436 ceph_msg_remove_list(&con->out_sent);
437
Sage Weilcf3e5c42009-12-11 09:48:05 -0800438 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500439 BUG_ON(con->in_msg->con != con);
440 con->in_msg->con = NULL;
Sage Weilcf3e5c42009-12-11 09:48:05 -0800441 ceph_msg_put(con->in_msg);
442 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -0700443 con->ops->put(con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800444 }
445
Sage Weil31b80062009-10-06 11:31:13 -0700446 con->connect_seq = 0;
447 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800448 if (con->out_msg) {
449 ceph_msg_put(con->out_msg);
450 con->out_msg = NULL;
451 }
Sage Weil31b80062009-10-06 11:31:13 -0700452 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700453 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700454}
455
456/*
457 * mark a peer down. drop any open connections.
458 */
459void ceph_con_close(struct ceph_connection *con)
460{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700461 dout("con_close %p peer %s\n", con,
462 ceph_pr_addr(&con->peer_addr.in_addr));
Alex Eldera5988c42012-05-29 11:04:58 -0500463 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700464 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Alex Eldera5988c42012-05-29 11:04:58 -0500465 set_bit(CLOSED, &con->state);
466
Alex Elder928443c2012-05-22 11:41:43 -0500467 clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */
468 clear_bit(KEEPALIVE_PENDING, &con->flags);
469 clear_bit(WRITE_PENDING, &con->flags);
Alex Eldera5988c42012-05-29 11:04:58 -0500470
Sage Weilec302642009-12-22 10:43:42 -0800471 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700472 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700473 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800474 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800475 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700476 queue_con(con);
477}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700478EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700479
480/*
Sage Weil31b80062009-10-06 11:31:13 -0700481 * Reopen a closed connection, with a new peer address.
482 */
483void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
484{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700485 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700486 set_bit(OPENING, &con->state);
Alex Eldera5988c42012-05-29 11:04:58 -0500487 WARN_ON(!test_and_clear_bit(CLOSED, &con->state));
488
Sage Weil31b80062009-10-06 11:31:13 -0700489 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800490 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700491 queue_con(con);
492}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700493EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700494
495/*
Sage Weil87b315a2010-03-22 14:51:18 -0700496 * return true if this connection ever successfully opened
497 */
498bool ceph_con_opened(struct ceph_connection *con)
499{
500 return con->connect_seq > 0;
501}
502
503/*
Sage Weil31b80062009-10-06 11:31:13 -0700504 * initialize a new connection.
505 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500506void ceph_con_init(struct ceph_connection *con, void *private,
507 const struct ceph_connection_operations *ops,
508 struct ceph_messenger *msgr, __u8 entity_type, __u64 entity_num)
Sage Weil31b80062009-10-06 11:31:13 -0700509{
510 dout("con_init %p\n", con);
511 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500512 con->private = private;
513 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700514 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500515
516 con_sock_state_init(con);
517
Alex Elder1bfd89f2012-05-26 23:26:43 -0500518 con->peer_name.type = (__u8) entity_type;
519 con->peer_name.num = cpu_to_le64(entity_num);
520
Sage Weilec302642009-12-22 10:43:42 -0800521 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700522 INIT_LIST_HEAD(&con->out_queue);
523 INIT_LIST_HEAD(&con->out_sent);
524 INIT_DELAYED_WORK(&con->work, con_work);
Alex Eldera5988c42012-05-29 11:04:58 -0500525
526 set_bit(CLOSED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700527}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700528EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700529
530
531/*
532 * We maintain a global counter to order connection attempts. Get
533 * a unique seq greater than @gt.
534 */
535static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
536{
537 u32 ret;
538
539 spin_lock(&msgr->global_seq_lock);
540 if (msgr->global_seq < gt)
541 msgr->global_seq = gt;
542 ret = ++msgr->global_seq;
543 spin_unlock(&msgr->global_seq_lock);
544 return ret;
545}
546
Alex Eldere2200422012-05-23 14:35:23 -0500547static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600548{
549 con->out_kvec_left = 0;
550 con->out_kvec_bytes = 0;
551 con->out_kvec_cur = &con->out_kvec[0];
552}
553
Alex Eldere2200422012-05-23 14:35:23 -0500554static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600555 size_t size, void *data)
556{
557 int index;
558
559 index = con->out_kvec_left;
560 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
561
562 con->out_kvec[index].iov_len = size;
563 con->out_kvec[index].iov_base = data;
564 con->out_kvec_left++;
565 con->out_kvec_bytes += size;
566}
Sage Weil31b80062009-10-06 11:31:13 -0700567
Alex Elder739c9052012-06-11 14:57:13 -0500568static void prepare_write_message_data(struct ceph_connection *con)
569{
570 struct ceph_msg *msg = con->out_msg;
571
572 BUG_ON(!msg);
573 BUG_ON(!msg->hdr.data_len);
574
575 /* initialize page iterator */
576 con->out_msg_pos.page = 0;
577 if (msg->pages)
578 con->out_msg_pos.page_pos = msg->page_alignment;
579 else
580 con->out_msg_pos.page_pos = 0;
581 con->out_msg_pos.data_pos = 0;
582 con->out_msg_pos.did_page_crc = false;
583 con->out_more = 1; /* data + footer will follow */
584}
585
Sage Weil31b80062009-10-06 11:31:13 -0700586/*
587 * Prepare footer for currently outgoing message, and finish things
588 * off. Assumes out_kvec* are already valid.. we just add on to the end.
589 */
Alex Elder859eb792012-02-14 14:05:33 -0600590static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700591{
592 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600593 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700594
595 dout("prepare_write_message_footer %p\n", con);
596 con->out_kvec_is_msg = true;
597 con->out_kvec[v].iov_base = &m->footer;
598 con->out_kvec[v].iov_len = sizeof(m->footer);
599 con->out_kvec_bytes += sizeof(m->footer);
600 con->out_kvec_left++;
601 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800602 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700603}
604
605/*
606 * Prepare headers for the next outgoing message.
607 */
608static void prepare_write_message(struct ceph_connection *con)
609{
610 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600611 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700612
Alex Eldere2200422012-05-23 14:35:23 -0500613 con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700614 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800615 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700616
617 /* Sneak an ack in there first? If we can get it into the same
618 * TCP packet that's a good thing. */
619 if (con->in_seq > con->in_seq_acked) {
620 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -0500621 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700622 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500623 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600624 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700625 }
626
Alex Elder38941f82012-06-01 14:56:43 -0500627 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -0600628 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800629 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -0500630 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -0700631
632 /* put message on sent list */
633 ceph_msg_get(m);
634 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700635
Sage Weile84346b2010-05-11 21:20:38 -0700636 /*
637 * only assign outgoing seq # if we haven't sent this message
638 * yet. if it is requeued, resend with it's original seq.
639 */
640 if (m->needs_out_seq) {
641 m->hdr.seq = cpu_to_le64(++con->out_seq);
642 m->needs_out_seq = false;
643 }
Yan, Zheng43643522012-06-06 19:35:55 -0500644#ifdef CONFIG_BLOCK
645 else
646 m->bio_iter = NULL;
647#endif
Sage Weil31b80062009-10-06 11:31:13 -0700648
649 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
650 m, con->out_seq, le16_to_cpu(m->hdr.type),
651 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
652 le32_to_cpu(m->hdr.data_len),
653 m->nr_pages);
654 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
655
656 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -0500657 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
658 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
659 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600660
Sage Weil31b80062009-10-06 11:31:13 -0700661 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -0500662 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600663 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700664
665 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600666 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
667 con->out_msg->hdr.crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -0700668 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
Alex Eldera9a0c512012-02-15 07:43:54 -0600669
670 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
671 con->out_msg->footer.front_crc = cpu_to_le32(crc);
672 if (m->middle) {
673 crc = crc32c(0, m->middle->vec.iov_base,
674 m->middle->vec.iov_len);
675 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
676 } else
Sage Weil31b80062009-10-06 11:31:13 -0700677 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -0500678 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700679 le32_to_cpu(con->out_msg->footer.front_crc),
680 le32_to_cpu(con->out_msg->footer.middle_crc));
681
682 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -0500683 con->out_msg->footer.data_crc = 0;
684 if (m->hdr.data_len)
685 prepare_write_message_data(con);
686 else
Sage Weil31b80062009-10-06 11:31:13 -0700687 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600688 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700689
Alex Elder928443c2012-05-22 11:41:43 -0500690 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700691}
692
693/*
694 * Prepare an ack.
695 */
696static void prepare_write_ack(struct ceph_connection *con)
697{
698 dout("prepare_write_ack %p %llu -> %llu\n", con,
699 con->in_seq_acked, con->in_seq);
700 con->in_seq_acked = con->in_seq;
701
Alex Eldere2200422012-05-23 14:35:23 -0500702 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600703
Alex Eldere2200422012-05-23 14:35:23 -0500704 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600705
Sage Weil31b80062009-10-06 11:31:13 -0700706 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500707 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600708 &con->out_temp_ack);
709
Sage Weil31b80062009-10-06 11:31:13 -0700710 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elder928443c2012-05-22 11:41:43 -0500711 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700712}
713
714/*
715 * Prepare to write keepalive byte.
716 */
717static void prepare_write_keepalive(struct ceph_connection *con)
718{
719 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -0500720 con_out_kvec_reset(con);
721 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Alex Elder928443c2012-05-22 11:41:43 -0500722 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700723}
724
725/*
726 * Connection negotiation.
727 */
728
Alex Elderdac1e712012-05-16 15:16:39 -0500729static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
730 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800731{
Alex Eldera3530df2012-05-16 15:16:39 -0500732 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -0500733
734 if (!con->ops->get_authorizer) {
735 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
736 con->out_connect.authorizer_len = 0;
737
Alex Elder729796b2012-05-16 15:16:39 -0500738 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -0500739 }
740
741 /* Can't hold the mutex while getting authorizer */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800742
Sage Weilec302642009-12-22 10:43:42 -0800743 mutex_unlock(&con->mutex);
Alex Elderb1c6b982012-05-16 15:16:38 -0500744
Alex Elderdac1e712012-05-16 15:16:39 -0500745 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Elder8f43fb52012-05-16 15:16:39 -0500746
Sage Weilec302642009-12-22 10:43:42 -0800747 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800748
Alex Eldera3530df2012-05-16 15:16:39 -0500749 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -0500750 return auth;
Alex Elder928443c2012-05-22 11:41:43 -0500751 if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags))
Alex Elder729796b2012-05-16 15:16:39 -0500752 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700753
Alex Elder8f43fb52012-05-16 15:16:39 -0500754 con->auth_reply_buf = auth->authorizer_reply_buf;
755 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
756
Alex Elder859eb792012-02-14 14:05:33 -0600757
Alex Elder729796b2012-05-16 15:16:39 -0500758 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800759}
760
Sage Weil31b80062009-10-06 11:31:13 -0700761/*
762 * We connected to a peer and are saying hello.
763 */
Alex Eldere825a662012-05-16 15:16:38 -0500764static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700765{
Alex Eldere2200422012-05-23 14:35:23 -0500766 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
767 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -0500768 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800769
Sage Weileed0ef22009-11-10 14:34:36 -0800770 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500771 set_bit(WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800772}
773
Alex Eldere825a662012-05-16 15:16:38 -0500774static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800775{
Eric Dumazet95c96172012-04-15 05:58:06 +0000776 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -0700777 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -0500778 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -0500779 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700780
781 switch (con->peer_name.type) {
782 case CEPH_ENTITY_TYPE_MON:
783 proto = CEPH_MONC_PROTOCOL;
784 break;
785 case CEPH_ENTITY_TYPE_OSD:
786 proto = CEPH_OSDC_PROTOCOL;
787 break;
788 case CEPH_ENTITY_TYPE_MDS:
789 proto = CEPH_MDSC_PROTOCOL;
790 break;
791 default:
792 BUG();
793 }
794
795 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
796 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800797
Alex Eldere825a662012-05-16 15:16:38 -0500798 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700799 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
800 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
801 con->out_connect.global_seq = cpu_to_le32(global_seq);
802 con->out_connect.protocol_version = cpu_to_le32(proto);
803 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700804
Alex Elderdac1e712012-05-16 15:16:39 -0500805 auth_proto = CEPH_AUTH_UNKNOWN;
806 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -0500807 if (IS_ERR(auth))
808 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -0500809
Alex Elderdac1e712012-05-16 15:16:39 -0500810 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -0500811 con->out_connect.authorizer_len = auth ?
812 cpu_to_le32(auth->authorizer_buf_len) : 0;
813
Alex Eldere2200422012-05-23 14:35:23 -0500814 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -0500815 &con->out_connect);
816 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -0500817 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -0500818 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600819
Sage Weil31b80062009-10-06 11:31:13 -0700820 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500821 set_bit(WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800822
Alex Eldere10c7582012-05-16 15:16:38 -0500823 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700824}
825
Sage Weil31b80062009-10-06 11:31:13 -0700826/*
827 * write as much of pending kvecs to the socket as we can.
828 * 1 -> done
829 * 0 -> socket full, but more to do
830 * <0 -> error
831 */
832static int write_partial_kvec(struct ceph_connection *con)
833{
834 int ret;
835
836 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
837 while (con->out_kvec_bytes > 0) {
838 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
839 con->out_kvec_left, con->out_kvec_bytes,
840 con->out_more);
841 if (ret <= 0)
842 goto out;
843 con->out_kvec_bytes -= ret;
844 if (con->out_kvec_bytes == 0)
845 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600846
847 /* account for full iov entries consumed */
848 while (ret >= con->out_kvec_cur->iov_len) {
849 BUG_ON(!con->out_kvec_left);
850 ret -= con->out_kvec_cur->iov_len;
851 con->out_kvec_cur++;
852 con->out_kvec_left--;
853 }
854 /* and for a partially-consumed entry */
855 if (ret) {
856 con->out_kvec_cur->iov_len -= ret;
857 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700858 }
859 }
860 con->out_kvec_left = 0;
861 con->out_kvec_is_msg = false;
862 ret = 1;
863out:
864 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
865 con->out_kvec_bytes, con->out_kvec_left, ret);
866 return ret; /* done! */
867}
868
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700869#ifdef CONFIG_BLOCK
870static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
871{
872 if (!bio) {
873 *iter = NULL;
874 *seg = 0;
875 return;
876 }
877 *iter = bio;
878 *seg = bio->bi_idx;
879}
880
881static void iter_bio_next(struct bio **bio_iter, int *seg)
882{
883 if (*bio_iter == NULL)
884 return;
885
886 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
887
888 (*seg)++;
889 if (*seg == (*bio_iter)->bi_vcnt)
890 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
891}
892#endif
893
Sage Weil31b80062009-10-06 11:31:13 -0700894/*
895 * Write as much message data payload as we can. If we finish, queue
896 * up the footer.
897 * 1 -> done, footer is now queued in out_kvec[].
898 * 0 -> socket full, but more to do
899 * <0 -> error
900 */
901static int write_partial_msg_pages(struct ceph_connection *con)
902{
903 struct ceph_msg *msg = con->out_msg;
Eric Dumazet95c96172012-04-15 05:58:06 +0000904 unsigned int data_len = le32_to_cpu(msg->hdr.data_len);
Sage Weil31b80062009-10-06 11:31:13 -0700905 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600906 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700907 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700908 int total_max_write;
909 int in_trail = 0;
910 size_t trail_len = (msg->trail ? msg->trail->length : 0);
Sage Weil31b80062009-10-06 11:31:13 -0700911
912 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
913 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
914 con->out_msg_pos.page_pos);
915
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700916#ifdef CONFIG_BLOCK
917 if (msg->bio && !msg->bio_iter)
918 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
919#endif
920
921 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -0700922 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700923 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -0600924 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700925
926 total_max_write = data_len - trail_len -
927 con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -0700928
929 /*
930 * if we are calculating the data crc (the default), we need
931 * to map the page. if our pages[] has been revoked, use the
932 * zero page.
933 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700934
935 /* have we reached the trail part of the data? */
936 if (con->out_msg_pos.data_pos >= data_len - trail_len) {
937 in_trail = 1;
938
939 total_max_write = data_len - con->out_msg_pos.data_pos;
940
941 page = list_first_entry(&msg->trail->head,
942 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700943 max_write = PAGE_SIZE;
944 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -0700945 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -0800946 } else if (msg->pagelist) {
947 page = list_first_entry(&msg->pagelist->head,
948 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700949#ifdef CONFIG_BLOCK
950 } else if (msg->bio) {
951 struct bio_vec *bv;
952
953 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
954 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -0600955 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700956 max_write = bv->bv_len;
957#endif
Sage Weil31b80062009-10-06 11:31:13 -0700958 } else {
Alex Elder57666512012-01-23 15:49:27 -0600959 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -0700960 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700961 len = min_t(int, max_write - con->out_msg_pos.page_pos,
962 total_max_write);
963
Alex Elder37675b02012-03-07 11:40:08 -0600964 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -0600965 void *base;
Alex Eldera9a0c512012-02-15 07:43:54 -0600966 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700967 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -0600968 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -0700969
Alex Elder8d63e312012-03-07 11:40:08 -0600970 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -0700971 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -0600972 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Eldera9a0c512012-02-15 07:43:54 -0600973 crc = crc32c(tmpcrc, base, len);
974 con->out_msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -0600975 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -0700976 }
Alex Eldere36b13c2012-03-07 11:40:08 -0600977 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -0600978 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -0600979 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -0700980
Alex Elder0cdf9e62012-03-07 11:40:08 -0600981 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -0700982 kunmap(page);
983
984 if (ret <= 0)
985 goto out;
986
987 con->out_msg_pos.data_pos += ret;
988 con->out_msg_pos.page_pos += ret;
989 if (ret == len) {
990 con->out_msg_pos.page_pos = 0;
991 con->out_msg_pos.page++;
Alex Elderbca064d2012-02-15 07:43:54 -0600992 con->out_msg_pos.did_page_crc = false;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700993 if (in_trail)
994 list_move_tail(&page->lru,
995 &msg->trail->head);
996 else if (msg->pagelist)
Sage Weil58bb3b32009-12-23 12:12:31 -0800997 list_move_tail(&page->lru,
998 &msg->pagelist->head);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700999#ifdef CONFIG_BLOCK
1000 else if (msg->bio)
1001 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
1002#endif
Sage Weil31b80062009-10-06 11:31:13 -07001003 }
1004 }
1005
1006 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1007
1008 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001009 if (!do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -07001010 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001011 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001012 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001013 ret = 1;
1014out:
1015 return ret;
1016}
1017
1018/*
1019 * write some zeros
1020 */
1021static int write_partial_skip(struct ceph_connection *con)
1022{
1023 int ret;
1024
1025 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001026 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001027
Alex Elder31739132012-03-07 11:40:08 -06001028 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001029 if (ret <= 0)
1030 goto out;
1031 con->out_skip -= ret;
1032 }
1033 ret = 1;
1034out:
1035 return ret;
1036}
1037
1038/*
1039 * Prepare to read connection handshake, or an ack.
1040 */
Sage Weileed0ef22009-11-10 14:34:36 -08001041static void prepare_read_banner(struct ceph_connection *con)
1042{
1043 dout("prepare_read_banner %p\n", con);
1044 con->in_base_pos = 0;
1045}
1046
Sage Weil31b80062009-10-06 11:31:13 -07001047static void prepare_read_connect(struct ceph_connection *con)
1048{
1049 dout("prepare_read_connect %p\n", con);
1050 con->in_base_pos = 0;
1051}
1052
1053static void prepare_read_ack(struct ceph_connection *con)
1054{
1055 dout("prepare_read_ack %p\n", con);
1056 con->in_base_pos = 0;
1057}
1058
1059static void prepare_read_tag(struct ceph_connection *con)
1060{
1061 dout("prepare_read_tag %p\n", con);
1062 con->in_base_pos = 0;
1063 con->in_tag = CEPH_MSGR_TAG_READY;
1064}
1065
1066/*
1067 * Prepare to read a message.
1068 */
1069static int prepare_read_message(struct ceph_connection *con)
1070{
1071 dout("prepare_read_message %p\n", con);
1072 BUG_ON(con->in_msg != NULL);
1073 con->in_base_pos = 0;
1074 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1075 return 0;
1076}
1077
1078
1079static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001080 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001081{
Alex Eldere6cee712012-05-10 10:29:50 -05001082 while (con->in_base_pos < end) {
1083 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001084 int have = size - left;
1085 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1086 if (ret <= 0)
1087 return ret;
1088 con->in_base_pos += ret;
1089 }
1090 return 1;
1091}
1092
1093
1094/*
1095 * Read all or part of the connect-side handshake on a new connection
1096 */
Sage Weileed0ef22009-11-10 14:34:36 -08001097static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001098{
Alex Elderfd516532012-05-10 10:29:50 -05001099 int size;
1100 int end;
1101 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001102
Sage Weileed0ef22009-11-10 14:34:36 -08001103 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001104
1105 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001106 size = strlen(CEPH_BANNER);
1107 end = size;
1108 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001109 if (ret <= 0)
1110 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001111
1112 size = sizeof (con->actual_peer_addr);
1113 end += size;
1114 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001115 if (ret <= 0)
1116 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001117
1118 size = sizeof (con->peer_addr_for_me);
1119 end += size;
1120 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001121 if (ret <= 0)
1122 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001123
Sage Weileed0ef22009-11-10 14:34:36 -08001124out:
1125 return ret;
1126}
1127
1128static int read_partial_connect(struct ceph_connection *con)
1129{
Alex Elderfd516532012-05-10 10:29:50 -05001130 int size;
1131 int end;
1132 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001133
1134 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1135
Alex Elderfd516532012-05-10 10:29:50 -05001136 size = sizeof (con->in_reply);
1137 end = size;
1138 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001139 if (ret <= 0)
1140 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001141
1142 size = le32_to_cpu(con->in_reply.authorizer_len);
1143 end += size;
1144 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001145 if (ret <= 0)
1146 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001147
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001148 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1149 con, (int)con->in_reply.tag,
1150 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001151 le32_to_cpu(con->in_reply.global_seq));
1152out:
1153 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001154
Sage Weil31b80062009-10-06 11:31:13 -07001155}
1156
1157/*
1158 * Verify the hello banner looks okay.
1159 */
1160static int verify_hello(struct ceph_connection *con)
1161{
1162 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001163 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001164 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001165 con->error_msg = "protocol error, bad banner";
1166 return -1;
1167 }
1168 return 0;
1169}
1170
1171static bool addr_is_blank(struct sockaddr_storage *ss)
1172{
1173 switch (ss->ss_family) {
1174 case AF_INET:
1175 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1176 case AF_INET6:
1177 return
1178 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1179 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1180 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1181 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1182 }
1183 return false;
1184}
1185
1186static int addr_port(struct sockaddr_storage *ss)
1187{
1188 switch (ss->ss_family) {
1189 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001190 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001191 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001192 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001193 }
1194 return 0;
1195}
1196
1197static void addr_set_port(struct sockaddr_storage *ss, int p)
1198{
1199 switch (ss->ss_family) {
1200 case AF_INET:
1201 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001202 break;
Sage Weil31b80062009-10-06 11:31:13 -07001203 case AF_INET6:
1204 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001205 break;
Sage Weil31b80062009-10-06 11:31:13 -07001206 }
1207}
1208
1209/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001210 * Unlike other *_pton function semantics, zero indicates success.
1211 */
1212static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1213 char delim, const char **ipend)
1214{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001215 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1216 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001217
1218 memset(ss, 0, sizeof(*ss));
1219
1220 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1221 ss->ss_family = AF_INET;
1222 return 0;
1223 }
1224
1225 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1226 ss->ss_family = AF_INET6;
1227 return 0;
1228 }
1229
1230 return -EINVAL;
1231}
1232
1233/*
1234 * Extract hostname string and resolve using kernel DNS facility.
1235 */
1236#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1237static int ceph_dns_resolve_name(const char *name, size_t namelen,
1238 struct sockaddr_storage *ss, char delim, const char **ipend)
1239{
1240 const char *end, *delim_p;
1241 char *colon_p, *ip_addr = NULL;
1242 int ip_len, ret;
1243
1244 /*
1245 * The end of the hostname occurs immediately preceding the delimiter or
1246 * the port marker (':') where the delimiter takes precedence.
1247 */
1248 delim_p = memchr(name, delim, namelen);
1249 colon_p = memchr(name, ':', namelen);
1250
1251 if (delim_p && colon_p)
1252 end = delim_p < colon_p ? delim_p : colon_p;
1253 else if (!delim_p && colon_p)
1254 end = colon_p;
1255 else {
1256 end = delim_p;
1257 if (!end) /* case: hostname:/ */
1258 end = name + namelen;
1259 }
1260
1261 if (end <= name)
1262 return -EINVAL;
1263
1264 /* do dns_resolve upcall */
1265 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1266 if (ip_len > 0)
1267 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1268 else
1269 ret = -ESRCH;
1270
1271 kfree(ip_addr);
1272
1273 *ipend = end;
1274
1275 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1276 ret, ret ? "failed" : ceph_pr_addr(ss));
1277
1278 return ret;
1279}
1280#else
1281static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1282 struct sockaddr_storage *ss, char delim, const char **ipend)
1283{
1284 return -EINVAL;
1285}
1286#endif
1287
1288/*
1289 * Parse a server name (IP or hostname). If a valid IP address is not found
1290 * then try to extract a hostname to resolve using userspace DNS upcall.
1291 */
1292static int ceph_parse_server_name(const char *name, size_t namelen,
1293 struct sockaddr_storage *ss, char delim, const char **ipend)
1294{
1295 int ret;
1296
1297 ret = ceph_pton(name, namelen, ss, delim, ipend);
1298 if (ret)
1299 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1300
1301 return ret;
1302}
1303
1304/*
Sage Weil31b80062009-10-06 11:31:13 -07001305 * Parse an ip[:port] list into an addr array. Use the default
1306 * monitor port if a port isn't specified.
1307 */
1308int ceph_parse_ips(const char *c, const char *end,
1309 struct ceph_entity_addr *addr,
1310 int max_count, int *count)
1311{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001312 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001313 const char *p = c;
1314
1315 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1316 for (i = 0; i < max_count; i++) {
1317 const char *ipend;
1318 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001319 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001320 char delim = ',';
1321
1322 if (*p == '[') {
1323 delim = ']';
1324 p++;
1325 }
Sage Weil31b80062009-10-06 11:31:13 -07001326
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001327 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1328 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001329 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001330 ret = -EINVAL;
1331
Sage Weil31b80062009-10-06 11:31:13 -07001332 p = ipend;
1333
Sage Weil39139f62010-07-08 09:54:52 -07001334 if (delim == ']') {
1335 if (*p != ']') {
1336 dout("missing matching ']'\n");
1337 goto bad;
1338 }
1339 p++;
1340 }
1341
Sage Weil31b80062009-10-06 11:31:13 -07001342 /* port? */
1343 if (p < end && *p == ':') {
1344 port = 0;
1345 p++;
1346 while (p < end && *p >= '0' && *p <= '9') {
1347 port = (port * 10) + (*p - '0');
1348 p++;
1349 }
1350 if (port > 65535 || port == 0)
1351 goto bad;
1352 } else {
1353 port = CEPH_MON_PORT;
1354 }
1355
1356 addr_set_port(ss, port);
1357
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001358 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001359
1360 if (p == end)
1361 break;
1362 if (*p != ',')
1363 goto bad;
1364 p++;
1365 }
1366
1367 if (p != end)
1368 goto bad;
1369
1370 if (count)
1371 *count = i + 1;
1372 return 0;
1373
1374bad:
Sage Weil39139f62010-07-08 09:54:52 -07001375 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001376 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001377}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001378EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001379
Sage Weileed0ef22009-11-10 14:34:36 -08001380static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001381{
Sage Weileed0ef22009-11-10 14:34:36 -08001382 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001383
1384 if (verify_hello(con) < 0)
1385 return -1;
1386
Sage Weil63f2d212009-11-03 15:17:56 -08001387 ceph_decode_addr(&con->actual_peer_addr);
1388 ceph_decode_addr(&con->peer_addr_for_me);
1389
Sage Weil31b80062009-10-06 11:31:13 -07001390 /*
1391 * Make sure the other end is who we wanted. note that the other
1392 * end may not yet know their ip address, so if it's 0.0.0.0, give
1393 * them the benefit of the doubt.
1394 */
Sage Weil103e2d32010-01-07 16:12:36 -08001395 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1396 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001397 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1398 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001399 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001400 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001401 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001402 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001403 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001404 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001405 return -1;
1406 }
1407
1408 /*
1409 * did we learn our address?
1410 */
1411 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1412 int port = addr_port(&con->msgr->inst.addr.in_addr);
1413
1414 memcpy(&con->msgr->inst.addr.in_addr,
1415 &con->peer_addr_for_me.in_addr,
1416 sizeof(con->peer_addr_for_me.in_addr));
1417 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001418 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001419 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001420 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001421 }
1422
Sage Weileed0ef22009-11-10 14:34:36 -08001423 set_bit(NEGOTIATING, &con->state);
1424 prepare_read_connect(con);
1425 return 0;
1426}
1427
Sage Weil04a419f2009-12-23 09:30:21 -08001428static void fail_protocol(struct ceph_connection *con)
1429{
1430 reset_connection(con);
1431 set_bit(CLOSED, &con->state); /* in case there's queued work */
Sage Weil04a419f2009-12-23 09:30:21 -08001432}
1433
Sage Weileed0ef22009-11-10 14:34:36 -08001434static int process_connect(struct ceph_connection *con)
1435{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001436 u64 sup_feat = con->msgr->supported_features;
1437 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001438 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001439 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001440
Sage Weileed0ef22009-11-10 14:34:36 -08001441 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1442
Sage Weil31b80062009-10-06 11:31:13 -07001443 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001444 case CEPH_MSGR_TAG_FEATURES:
1445 pr_err("%s%lld %s feature set mismatch,"
1446 " my %llx < server's %llx, missing %llx\n",
1447 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001448 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001449 sup_feat, server_feat, server_feat & ~sup_feat);
1450 con->error_msg = "missing required protocol features";
1451 fail_protocol(con);
1452 return -1;
1453
Sage Weil31b80062009-10-06 11:31:13 -07001454 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001455 pr_err("%s%lld %s protocol version mismatch,"
1456 " my %d != server's %d\n",
1457 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001458 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001459 le32_to_cpu(con->out_connect.protocol_version),
1460 le32_to_cpu(con->in_reply.protocol_version));
1461 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001462 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001463 return -1;
1464
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001465 case CEPH_MSGR_TAG_BADAUTHORIZER:
1466 con->auth_retry++;
1467 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1468 con->auth_retry);
1469 if (con->auth_retry == 2) {
1470 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001471 return -1;
1472 }
1473 con->auth_retry = 1;
Alex Eldere2200422012-05-23 14:35:23 -05001474 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001475 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001476 if (ret < 0)
1477 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001478 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001479 break;
Sage Weil31b80062009-10-06 11:31:13 -07001480
1481 case CEPH_MSGR_TAG_RESETSESSION:
1482 /*
1483 * If we connected with a large connect_seq but the peer
1484 * has no record of a session with us (no connection, or
1485 * connect_seq == 0), they will send RESETSESION to indicate
1486 * that they must have reset their session, and may have
1487 * dropped messages.
1488 */
1489 dout("process_connect got RESET peer seq %u\n",
1490 le32_to_cpu(con->in_connect.connect_seq));
1491 pr_err("%s%lld %s connection reset\n",
1492 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001493 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001494 reset_connection(con);
Alex Eldere2200422012-05-23 14:35:23 -05001495 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001496 ret = prepare_write_connect(con);
1497 if (ret < 0)
1498 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001499 prepare_read_connect(con);
1500
1501 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001502 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001503 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1504 if (con->ops->peer_reset)
1505 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001506 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07001507 if (test_bit(CLOSED, &con->state) ||
1508 test_bit(OPENING, &con->state))
1509 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001510 break;
1511
1512 case CEPH_MSGR_TAG_RETRY_SESSION:
1513 /*
1514 * If we sent a smaller connect_seq than the peer has, try
1515 * again with a larger value.
1516 */
1517 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1518 le32_to_cpu(con->out_connect.connect_seq),
1519 le32_to_cpu(con->in_connect.connect_seq));
1520 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Alex Eldere2200422012-05-23 14:35:23 -05001521 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001522 ret = prepare_write_connect(con);
1523 if (ret < 0)
1524 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001525 prepare_read_connect(con);
1526 break;
1527
1528 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1529 /*
1530 * If we sent a smaller global_seq than the peer has, try
1531 * again with a larger value.
1532 */
Sage Weileed0ef22009-11-10 14:34:36 -08001533 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001534 con->peer_global_seq,
1535 le32_to_cpu(con->in_connect.global_seq));
1536 get_global_seq(con->msgr,
1537 le32_to_cpu(con->in_connect.global_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_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001546 if (req_feat & ~server_feat) {
1547 pr_err("%s%lld %s protocol feature mismatch,"
1548 " my required %llx > server's %llx, need %llx\n",
1549 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001550 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001551 req_feat, server_feat, req_feat & ~server_feat);
1552 con->error_msg = "missing required protocol features";
1553 fail_protocol(con);
1554 return -1;
1555 }
Sage Weil31b80062009-10-06 11:31:13 -07001556 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001557 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1558 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001559 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001560 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1561 con->peer_global_seq,
1562 le32_to_cpu(con->in_reply.connect_seq),
1563 con->connect_seq);
1564 WARN_ON(con->connect_seq !=
1565 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001566
1567 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elder928443c2012-05-22 11:41:43 -05001568 set_bit(LOSSYTX, &con->flags);
Sage Weil92ac41d2009-12-14 14:56:56 -08001569
Sage Weil31b80062009-10-06 11:31:13 -07001570 prepare_read_tag(con);
1571 break;
1572
1573 case CEPH_MSGR_TAG_WAIT:
1574 /*
1575 * If there is a connection race (we are opening
1576 * connections to each other), one of us may just have
1577 * to WAIT. This shouldn't happen if we are the
1578 * client.
1579 */
Sage Weil04177882011-05-12 15:33:17 -07001580 pr_err("process_connect got WAIT as client\n");
1581 con->error_msg = "protocol error, got WAIT as client";
1582 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001583
1584 default:
1585 pr_err("connect protocol error, will retry\n");
1586 con->error_msg = "protocol error, garbage tag during connect";
1587 return -1;
1588 }
1589 return 0;
1590}
1591
1592
1593/*
1594 * read (part of) an ack
1595 */
1596static int read_partial_ack(struct ceph_connection *con)
1597{
Alex Elderfd516532012-05-10 10:29:50 -05001598 int size = sizeof (con->in_temp_ack);
1599 int end = size;
1600
1601 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001602}
1603
1604
1605/*
1606 * We can finally discard anything that's been acked.
1607 */
1608static void process_ack(struct ceph_connection *con)
1609{
1610 struct ceph_msg *m;
1611 u64 ack = le64_to_cpu(con->in_temp_ack);
1612 u64 seq;
1613
Sage Weil31b80062009-10-06 11:31:13 -07001614 while (!list_empty(&con->out_sent)) {
1615 m = list_first_entry(&con->out_sent, struct ceph_msg,
1616 list_head);
1617 seq = le64_to_cpu(m->hdr.seq);
1618 if (seq > ack)
1619 break;
1620 dout("got ack for seq %llu type %d at %p\n", seq,
1621 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001622 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001623 ceph_msg_remove(m);
1624 }
Sage Weil31b80062009-10-06 11:31:13 -07001625 prepare_read_tag(con);
1626}
1627
1628
1629
1630
Yehuda Sadeh24504182010-01-08 13:58:34 -08001631static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001632 struct kvec *section,
1633 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001634{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001635 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001636
Yehuda Sadeh24504182010-01-08 13:58:34 -08001637 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001638
Yehuda Sadeh24504182010-01-08 13:58:34 -08001639 while (section->iov_len < sec_len) {
1640 BUG_ON(section->iov_base == NULL);
1641 left = sec_len - section->iov_len;
1642 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1643 section->iov_len, left);
1644 if (ret <= 0)
1645 return ret;
1646 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001647 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001648 if (section->iov_len == sec_len)
1649 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001650
1651 return 1;
1652}
1653
Alex Elder1c20f2d2012-06-04 14:43:32 -05001654static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
1655 struct ceph_msg_header *hdr);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001656
1657
1658static int read_partial_message_pages(struct ceph_connection *con,
1659 struct page **pages,
Eric Dumazet95c96172012-04-15 05:58:06 +00001660 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001661{
1662 void *p;
1663 int ret;
1664 int left;
1665
1666 left = min((int)(data_len - con->in_msg_pos.data_pos),
1667 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1668 /* (page) data */
1669 BUG_ON(pages == NULL);
1670 p = kmap(pages[con->in_msg_pos.page]);
1671 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1672 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001673 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001674 con->in_data_crc =
1675 crc32c(con->in_data_crc,
1676 p + con->in_msg_pos.page_pos, ret);
1677 kunmap(pages[con->in_msg_pos.page]);
1678 if (ret <= 0)
1679 return ret;
1680 con->in_msg_pos.data_pos += ret;
1681 con->in_msg_pos.page_pos += ret;
1682 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1683 con->in_msg_pos.page_pos = 0;
1684 con->in_msg_pos.page++;
1685 }
1686
1687 return ret;
1688}
1689
1690#ifdef CONFIG_BLOCK
1691static int read_partial_message_bio(struct ceph_connection *con,
1692 struct bio **bio_iter, int *bio_seg,
Eric Dumazet95c96172012-04-15 05:58:06 +00001693 unsigned int data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001694{
1695 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1696 void *p;
1697 int ret, left;
1698
1699 if (IS_ERR(bv))
1700 return PTR_ERR(bv);
1701
1702 left = min((int)(data_len - con->in_msg_pos.data_pos),
1703 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1704
1705 p = kmap(bv->bv_page) + bv->bv_offset;
1706
1707 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1708 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001709 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001710 con->in_data_crc =
1711 crc32c(con->in_data_crc,
1712 p + con->in_msg_pos.page_pos, ret);
1713 kunmap(bv->bv_page);
1714 if (ret <= 0)
1715 return ret;
1716 con->in_msg_pos.data_pos += ret;
1717 con->in_msg_pos.page_pos += ret;
1718 if (con->in_msg_pos.page_pos == bv->bv_len) {
1719 con->in_msg_pos.page_pos = 0;
1720 iter_bio_next(bio_iter, bio_seg);
1721 }
1722
1723 return ret;
1724}
1725#endif
1726
Sage Weil31b80062009-10-06 11:31:13 -07001727/*
1728 * read (part of) a message.
1729 */
1730static int read_partial_message(struct ceph_connection *con)
1731{
1732 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05001733 int size;
1734 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001735 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00001736 unsigned int front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001737 bool do_datacrc = !con->msgr->nocrc;
Sage Weilae187562010-04-22 07:47:01 -07001738 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001739 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001740
1741 dout("read_partial_message con %p msg %p\n", con, m);
1742
1743 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05001744 size = sizeof (con->in_hdr);
1745 end = size;
1746 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05001747 if (ret <= 0)
1748 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001749
1750 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1751 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1752 pr_err("read_partial_message bad hdr "
1753 " crc %u != expected %u\n",
1754 crc, con->in_hdr.crc);
1755 return -EBADMSG;
1756 }
1757
Sage Weil31b80062009-10-06 11:31:13 -07001758 front_len = le32_to_cpu(con->in_hdr.front_len);
1759 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1760 return -EIO;
1761 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1762 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1763 return -EIO;
1764 data_len = le32_to_cpu(con->in_hdr.data_len);
1765 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1766 return -EIO;
1767
Sage Weilae187562010-04-22 07:47:01 -07001768 /* verify seq# */
1769 seq = le64_to_cpu(con->in_hdr.seq);
1770 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001771 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001772 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001773 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001774 seq, con->in_seq + 1);
1775 con->in_base_pos = -front_len - middle_len - data_len -
1776 sizeof(m->footer);
1777 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001778 return 0;
1779 } else if ((s64)seq - (s64)con->in_seq > 1) {
1780 pr_err("read_partial_message bad seq %lld expected %lld\n",
1781 seq, con->in_seq + 1);
1782 con->error_msg = "bad message sequence # for incoming message";
1783 return -EBADMSG;
1784 }
1785
Sage Weil31b80062009-10-06 11:31:13 -07001786 /* allocate message? */
1787 if (!con->in_msg) {
1788 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1789 con->in_hdr.front_len, con->in_hdr.data_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05001790 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
Sage Weil31b80062009-10-06 11:31:13 -07001791 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001792 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001793 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001794 con->in_base_pos = -front_len - middle_len - data_len -
1795 sizeof(m->footer);
1796 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001797 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001798 return 0;
1799 }
Sage Weila79832f2010-04-01 16:06:19 -07001800 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001801 con->error_msg =
1802 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001803 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001804 }
Alex Elder38941f82012-06-01 14:56:43 -05001805
1806 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07001807 m = con->in_msg;
1808 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001809 if (m->middle)
1810 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001811
1812 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001813 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001814 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001815 else
1816 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001817 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001818 }
1819
1820 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001821 ret = read_partial_message_section(con, &m->front, front_len,
1822 &con->in_front_crc);
1823 if (ret <= 0)
1824 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001825
1826 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001827 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001828 ret = read_partial_message_section(con, &m->middle->vec,
1829 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001830 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001831 if (ret <= 0)
1832 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001833 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001834#ifdef CONFIG_BLOCK
1835 if (m->bio && !m->bio_iter)
1836 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1837#endif
Sage Weil31b80062009-10-06 11:31:13 -07001838
1839 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001840 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001841 if (m->pages) {
1842 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001843 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001844 if (ret <= 0)
1845 return ret;
1846#ifdef CONFIG_BLOCK
1847 } else if (m->bio) {
1848
1849 ret = read_partial_message_bio(con,
1850 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001851 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001852 if (ret <= 0)
1853 return ret;
1854#endif
1855 } else {
1856 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001857 }
1858 }
1859
Sage Weil31b80062009-10-06 11:31:13 -07001860 /* footer */
Alex Elderfd516532012-05-10 10:29:50 -05001861 size = sizeof (m->footer);
1862 end += size;
1863 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05001864 if (ret <= 0)
1865 return ret;
1866
Sage Weil31b80062009-10-06 11:31:13 -07001867 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1868 m, front_len, m->footer.front_crc, middle_len,
1869 m->footer.middle_crc, data_len, m->footer.data_crc);
1870
1871 /* crc ok? */
1872 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1873 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1874 m, con->in_front_crc, m->footer.front_crc);
1875 return -EBADMSG;
1876 }
1877 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1878 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1879 m, con->in_middle_crc, m->footer.middle_crc);
1880 return -EBADMSG;
1881 }
Alex Elderbca064d2012-02-15 07:43:54 -06001882 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001883 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1884 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1885 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1886 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1887 return -EBADMSG;
1888 }
1889
1890 return 1; /* done! */
1891}
1892
1893/*
1894 * Process message. This happens in the worker thread. The callback should
1895 * be careful not to do anything that waits on other incoming messages or it
1896 * may deadlock.
1897 */
1898static void process_message(struct ceph_connection *con)
1899{
Sage Weil5e095e82009-12-14 14:30:34 -08001900 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001901
Alex Elder38941f82012-06-01 14:56:43 -05001902 BUG_ON(con->in_msg->con != con);
1903 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08001904 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001905 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07001906 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07001907
1908 /* if first message, set peer_name */
1909 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001910 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001911
Sage Weil31b80062009-10-06 11:31:13 -07001912 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001913 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001914
1915 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1916 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001917 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001918 le16_to_cpu(msg->hdr.type),
1919 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1920 le32_to_cpu(msg->hdr.front_len),
1921 le32_to_cpu(msg->hdr.data_len),
1922 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1923 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001924
1925 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001926 prepare_read_tag(con);
1927}
1928
1929
1930/*
1931 * Write something to the socket. Called in a worker thread when the
1932 * socket appears to be writeable and we have something ready to send.
1933 */
1934static int try_write(struct ceph_connection *con)
1935{
Sage Weil31b80062009-10-06 11:31:13 -07001936 int ret = 1;
1937
Sage Weild59315c2012-06-21 12:49:23 -07001938 dout("try_write start %p state %lu\n", con, con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001939
Sage Weil31b80062009-10-06 11:31:13 -07001940more:
1941 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1942
1943 /* open the socket first? */
1944 if (con->sock == NULL) {
Alex Eldera5988c42012-05-29 11:04:58 -05001945 clear_bit(NEGOTIATING, &con->state);
1946 set_bit(CONNECTING, &con->state);
1947
Alex Eldere2200422012-05-23 14:35:23 -05001948 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001949 prepare_write_banner(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001950 ret = prepare_write_connect(con);
1951 if (ret < 0)
1952 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08001953 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001954
Sage Weilcf3e5c42009-12-11 09:48:05 -08001955 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001956 con->in_tag = CEPH_MSGR_TAG_READY;
1957 dout("try_write initiating connect on %p new state %lu\n",
1958 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06001959 ret = ceph_tcp_connect(con);
1960 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07001961 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07001962 goto out;
1963 }
1964 }
1965
1966more_kvec:
1967 /* kvec data queued? */
1968 if (con->out_skip) {
1969 ret = write_partial_skip(con);
1970 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001971 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001972 }
1973 if (con->out_kvec_left) {
1974 ret = write_partial_kvec(con);
1975 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001976 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001977 }
1978
1979 /* msg pages? */
1980 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001981 if (con->out_msg_done) {
1982 ceph_msg_put(con->out_msg);
1983 con->out_msg = NULL; /* we're done with this one */
1984 goto do_next;
1985 }
1986
Sage Weil31b80062009-10-06 11:31:13 -07001987 ret = write_partial_msg_pages(con);
1988 if (ret == 1)
1989 goto more_kvec; /* we need to send the footer, too! */
1990 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08001991 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001992 if (ret < 0) {
1993 dout("try_write write_partial_msg_pages err %d\n",
1994 ret);
Sage Weil42961d22011-01-25 08:19:34 -08001995 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001996 }
1997 }
1998
Sage Weilc86a2932009-12-14 14:04:30 -08001999do_next:
Sage Weil31b80062009-10-06 11:31:13 -07002000 if (!test_bit(CONNECTING, &con->state)) {
2001 /* is anything else pending? */
2002 if (!list_empty(&con->out_queue)) {
2003 prepare_write_message(con);
2004 goto more;
2005 }
2006 if (con->in_seq > con->in_seq_acked) {
2007 prepare_write_ack(con);
2008 goto more;
2009 }
Alex Elder928443c2012-05-22 11:41:43 -05002010 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002011 prepare_write_keepalive(con);
2012 goto more;
2013 }
2014 }
2015
2016 /* Nothing to do! */
Alex Elder928443c2012-05-22 11:41:43 -05002017 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002018 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002019 ret = 0;
2020out:
Sage Weil42961d22011-01-25 08:19:34 -08002021 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002022 return ret;
2023}
2024
2025
2026
2027/*
2028 * Read what we can from the socket.
2029 */
2030static int try_read(struct ceph_connection *con)
2031{
Sage Weil31b80062009-10-06 11:31:13 -07002032 int ret = -1;
2033
2034 if (!con->sock)
2035 return 0;
2036
2037 if (test_bit(STANDBY, &con->state))
2038 return 0;
2039
2040 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08002041
Sage Weil31b80062009-10-06 11:31:13 -07002042more:
2043 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2044 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002045
2046 /*
2047 * process_connect and process_message drop and re-take
2048 * con->mutex. make sure we handle a racing close or reopen.
2049 */
2050 if (test_bit(CLOSED, &con->state) ||
2051 test_bit(OPENING, &con->state)) {
2052 ret = -EAGAIN;
2053 goto out;
2054 }
2055
Sage Weil31b80062009-10-06 11:31:13 -07002056 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08002057 if (!test_bit(NEGOTIATING, &con->state)) {
2058 dout("try_read connecting\n");
2059 ret = read_partial_banner(con);
2060 if (ret <= 0)
Sage Weileed0ef22009-11-10 14:34:36 -08002061 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002062 ret = process_banner(con);
2063 if (ret < 0)
2064 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08002065 }
Sage Weil31b80062009-10-06 11:31:13 -07002066 ret = read_partial_connect(con);
2067 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002068 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002069 ret = process_connect(con);
2070 if (ret < 0)
2071 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002072 goto more;
2073 }
2074
2075 if (con->in_base_pos < 0) {
2076 /*
2077 * skipping + discarding content.
2078 *
2079 * FIXME: there must be a better way to do this!
2080 */
Alex Elder84495f42012-02-15 07:43:55 -06002081 static char buf[SKIP_BUF_SIZE];
2082 int skip = min((int) sizeof (buf), -con->in_base_pos);
2083
Sage Weil31b80062009-10-06 11:31:13 -07002084 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2085 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2086 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002087 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002088 con->in_base_pos += ret;
2089 if (con->in_base_pos)
2090 goto more;
2091 }
2092 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2093 /*
2094 * what's next?
2095 */
2096 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2097 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002098 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002099 dout("try_read got tag %d\n", (int)con->in_tag);
2100 switch (con->in_tag) {
2101 case CEPH_MSGR_TAG_MSG:
2102 prepare_read_message(con);
2103 break;
2104 case CEPH_MSGR_TAG_ACK:
2105 prepare_read_ack(con);
2106 break;
2107 case CEPH_MSGR_TAG_CLOSE:
2108 set_bit(CLOSED, &con->state); /* fixme */
Sage Weil98bdb0a2011-01-25 08:17:48 -08002109 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002110 default:
2111 goto bad_tag;
2112 }
2113 }
2114 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2115 ret = read_partial_message(con);
2116 if (ret <= 0) {
2117 switch (ret) {
2118 case -EBADMSG:
2119 con->error_msg = "bad crc";
2120 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002121 break;
Sage Weil31b80062009-10-06 11:31:13 -07002122 case -EIO:
2123 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002124 break;
Sage Weil31b80062009-10-06 11:31:13 -07002125 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002126 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002127 }
2128 if (con->in_tag == CEPH_MSGR_TAG_READY)
2129 goto more;
2130 process_message(con);
2131 goto more;
2132 }
2133 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2134 ret = read_partial_ack(con);
2135 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002136 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002137 process_ack(con);
2138 goto more;
2139 }
2140
Sage Weil31b80062009-10-06 11:31:13 -07002141out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002142 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002143 return ret;
2144
2145bad_tag:
2146 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2147 con->error_msg = "protocol error, garbage tag";
2148 ret = -1;
2149 goto out;
2150}
2151
2152
2153/*
2154 * Atomically queue work on a connection. Bump @con reference to
2155 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002156 */
2157static void queue_con(struct ceph_connection *con)
2158{
Sage Weil31b80062009-10-06 11:31:13 -07002159 if (!con->ops->get(con)) {
2160 dout("queue_con %p ref count 0\n", con);
2161 return;
2162 }
2163
Tejun Heof363e452011-01-03 14:49:46 +01002164 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002165 dout("queue_con %p - already queued\n", con);
2166 con->ops->put(con);
2167 } else {
2168 dout("queue_con %p\n", con);
2169 }
2170}
2171
2172/*
2173 * Do some work on a connection. Drop a connection ref when we're done.
2174 */
2175static void con_work(struct work_struct *work)
2176{
2177 struct ceph_connection *con = container_of(work, struct ceph_connection,
2178 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002179 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002180
Sage Weil9dd46582010-04-28 13:51:50 -07002181 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002182restart:
Alex Elder928443c2012-05-22 11:41:43 -05002183 if (test_and_clear_bit(BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002184 dout("con_work %p backing off\n", con);
2185 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2186 round_jiffies_relative(con->delay))) {
2187 dout("con_work %p backoff %lu\n", con, con->delay);
2188 mutex_unlock(&con->mutex);
2189 return;
2190 } else {
2191 con->ops->put(con);
2192 dout("con_work %p FAILED to back off %lu\n", con,
2193 con->delay);
2194 }
2195 }
Sage Weil9dd46582010-04-28 13:51:50 -07002196
Sage Weile00de342011-03-04 12:25:05 -08002197 if (test_bit(STANDBY, &con->state)) {
2198 dout("con_work %p STANDBY\n", con);
2199 goto done;
2200 }
Sage Weil31b80062009-10-06 11:31:13 -07002201 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2202 dout("con_work CLOSED\n");
2203 con_close_socket(con);
2204 goto done;
2205 }
2206 if (test_and_clear_bit(OPENING, &con->state)) {
2207 /* reopen w/ new peer */
2208 dout("con_work OPENING\n");
2209 con_close_socket(con);
2210 }
2211
Alex Elder928443c2012-05-22 11:41:43 -05002212 if (test_and_clear_bit(SOCK_CLOSED, &con->flags))
Sage Weil0da5d702011-05-19 11:21:05 -07002213 goto fault;
2214
2215 ret = try_read(con);
2216 if (ret == -EAGAIN)
2217 goto restart;
2218 if (ret < 0)
2219 goto fault;
2220
2221 ret = try_write(con);
2222 if (ret == -EAGAIN)
2223 goto restart;
2224 if (ret < 0)
2225 goto fault;
Sage Weil31b80062009-10-06 11:31:13 -07002226
2227done:
Sage Weil9dd46582010-04-28 13:51:50 -07002228 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002229done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002230 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002231 return;
2232
2233fault:
2234 mutex_unlock(&con->mutex);
2235 ceph_fault(con); /* error/fault path */
2236 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002237}
2238
2239
2240/*
2241 * Generic error/fault handler. A retry mechanism is used with
2242 * exponential backoff
2243 */
2244static void ceph_fault(struct ceph_connection *con)
2245{
2246 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002247 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002248 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002249 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002250
Alex Elder928443c2012-05-22 11:41:43 -05002251 if (test_bit(LOSSYTX, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002252 dout("fault on LOSSYTX channel\n");
2253 goto out;
2254 }
2255
Sage Weilec302642009-12-22 10:43:42 -08002256 mutex_lock(&con->mutex);
Sage Weil91e45ce32010-02-15 12:05:09 -08002257 if (test_bit(CLOSED, &con->state))
2258 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002259
Sage Weil31b80062009-10-06 11:31:13 -07002260 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002261
2262 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002263 BUG_ON(con->in_msg->con != con);
2264 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08002265 ceph_msg_put(con->in_msg);
2266 con->in_msg = NULL;
Sage Weil36eb71a2012-06-21 12:47:08 -07002267 con->ops->put(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002268 }
Sage Weil31b80062009-10-06 11:31:13 -07002269
Sage Weile80a52d2010-02-25 12:40:45 -08002270 /* Requeue anything that hasn't been acked */
2271 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002272
Sage Weile76661d2011-03-03 10:10:15 -08002273 /* If there are no messages queued or keepalive pending, place
2274 * the connection in a STANDBY state */
2275 if (list_empty(&con->out_queue) &&
Alex Elder928443c2012-05-22 11:41:43 -05002276 !test_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weile00de342011-03-04 12:25:05 -08002277 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elder928443c2012-05-22 11:41:43 -05002278 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002279 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08002280 } else {
2281 /* retry after a delay. */
2282 if (con->delay == 0)
2283 con->delay = BASE_DELAY_INTERVAL;
2284 else if (con->delay < MAX_DELAY_INTERVAL)
2285 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002286 con->ops->get(con);
2287 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002288 round_jiffies_relative(con->delay))) {
2289 dout("fault queued %p delay %lu\n", con, con->delay);
2290 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002291 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002292 dout("fault failed to queue %p delay %lu, backoff\n",
2293 con, con->delay);
2294 /*
2295 * In many cases we see a socket state change
2296 * while con_work is running and end up
2297 * queuing (non-delayed) work, such that we
2298 * can't backoff with a delay. Set a flag so
2299 * that when con_work restarts we schedule the
2300 * delay then.
2301 */
Alex Elder928443c2012-05-22 11:41:43 -05002302 set_bit(BACKOFF, &con->flags);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002303 }
Sage Weil31b80062009-10-06 11:31:13 -07002304 }
2305
Sage Weil91e45ce32010-02-15 12:05:09 -08002306out_unlock:
2307 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002308out:
Sage Weil161fd652010-02-25 12:38:57 -08002309 /*
2310 * in case we faulted due to authentication, invalidate our
2311 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002312 */
Sage Weil161fd652010-02-25 12:38:57 -08002313 if (con->auth_retry && con->ops->invalidate_authorizer) {
2314 dout("calling invalidate_authorizer()\n");
2315 con->ops->invalidate_authorizer(con);
2316 }
2317
Sage Weil31b80062009-10-06 11:31:13 -07002318 if (con->ops->fault)
2319 con->ops->fault(con);
2320}
2321
2322
2323
2324/*
Alex Elder15d98822012-05-26 23:26:43 -05002325 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002326 */
Alex Elder15d98822012-05-26 23:26:43 -05002327void ceph_messenger_init(struct ceph_messenger *msgr,
2328 struct ceph_entity_addr *myaddr,
2329 u32 supported_features,
2330 u32 required_features,
2331 bool nocrc)
Sage Weil31b80062009-10-06 11:31:13 -07002332{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002333 msgr->supported_features = supported_features;
2334 msgr->required_features = required_features;
2335
Sage Weil31b80062009-10-06 11:31:13 -07002336 spin_lock_init(&msgr->global_seq_lock);
2337
Sage Weil31b80062009-10-06 11:31:13 -07002338 if (myaddr)
2339 msgr->inst.addr = *myaddr;
2340
2341 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002342 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002343 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002344 encode_my_addr(msgr);
Alex Elder15d98822012-05-26 23:26:43 -05002345 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002346
Alex Elder15d98822012-05-26 23:26:43 -05002347 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002348}
Alex Elder15d98822012-05-26 23:26:43 -05002349EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002350
Sage Weile00de342011-03-04 12:25:05 -08002351static void clear_standby(struct ceph_connection *con)
2352{
2353 /* come back from STANDBY? */
2354 if (test_and_clear_bit(STANDBY, &con->state)) {
2355 mutex_lock(&con->mutex);
2356 dout("clear_standby %p and ++connect_seq\n", con);
2357 con->connect_seq++;
Alex Elder928443c2012-05-22 11:41:43 -05002358 WARN_ON(test_bit(WRITE_PENDING, &con->flags));
2359 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags));
Sage Weile00de342011-03-04 12:25:05 -08002360 mutex_unlock(&con->mutex);
2361 }
2362}
2363
Sage Weil31b80062009-10-06 11:31:13 -07002364/*
2365 * Queue up an outgoing message on the given connection.
2366 */
2367void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2368{
2369 if (test_bit(CLOSED, &con->state)) {
2370 dout("con_send %p closed, dropping %p\n", con, msg);
2371 ceph_msg_put(msg);
2372 return;
2373 }
2374
2375 /* set src+dst */
Sage Weildbad1852010-03-25 15:45:38 -07002376 msg->hdr.src = con->msgr->inst.name;
Sage Weil31b80062009-10-06 11:31:13 -07002377
Sage Weil3ca02ef2010-03-01 15:25:00 -08002378 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2379
Sage Weile84346b2010-05-11 21:20:38 -07002380 msg->needs_out_seq = true;
2381
Sage Weil31b80062009-10-06 11:31:13 -07002382 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08002383 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002384
Alex Elder38941f82012-06-01 14:56:43 -05002385 BUG_ON(msg->con != NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002386 msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002387 BUG_ON(msg->con == NULL);
2388
Sage Weil31b80062009-10-06 11:31:13 -07002389 BUG_ON(!list_empty(&msg->list_head));
2390 list_add_tail(&msg->list_head, &con->out_queue);
2391 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2392 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2393 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2394 le32_to_cpu(msg->hdr.front_len),
2395 le32_to_cpu(msg->hdr.middle_len),
2396 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002397 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002398
2399 /* if there wasn't anything waiting to send before, queue
2400 * new work */
Sage Weile00de342011-03-04 12:25:05 -08002401 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002402 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002403 queue_con(con);
2404}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002405EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002406
2407/*
2408 * Revoke a message that was previously queued for send
2409 */
Alex Elder6740a842012-06-01 14:56:43 -05002410void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002411{
Alex Elder6740a842012-06-01 14:56:43 -05002412 struct ceph_connection *con = msg->con;
2413
2414 if (!con)
2415 return; /* Message not in our possession */
2416
Sage Weilec302642009-12-22 10:43:42 -08002417 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002418 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05002419 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002420 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -05002421 BUG_ON(msg->con == NULL);
Sage Weil36eb71a2012-06-21 12:47:08 -07002422 msg->con->ops->put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -05002423 msg->con = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05002424 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05002425
Sage Weil31b80062009-10-06 11:31:13 -07002426 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002427 }
2428 if (con->out_msg == msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002429 dout("%s %p msg %p - was sending\n", __func__, con, msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002430 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002431 if (con->out_kvec_is_msg) {
2432 con->out_skip = con->out_kvec_bytes;
2433 con->out_kvec_is_msg = false;
2434 }
Sage Weiled98ada2010-07-05 12:15:14 -07002435 msg->hdr.seq = 0;
Alex Elder92ce0342012-06-04 14:43:33 -05002436
2437 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07002438 }
Sage Weilec302642009-12-22 10:43:42 -08002439 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002440}
2441
2442/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002443 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002444 */
Alex Elder8921d112012-06-01 14:56:43 -05002445void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002446{
Alex Elder8921d112012-06-01 14:56:43 -05002447 struct ceph_connection *con;
2448
2449 BUG_ON(msg == NULL);
2450 if (!msg->con) {
2451 dout("%s msg %p null con\n", __func__, msg);
2452
2453 return; /* Message not in our possession */
2454 }
2455
2456 con = msg->con;
Sage Weil350b1c32009-12-22 10:45:45 -08002457 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05002458 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00002459 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
2460 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
2461 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002462
2463 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05002464 dout("%s %p msg %p revoked\n", __func__, con, msg);
2465 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08002466 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002467 front_len -
2468 middle_len -
2469 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002470 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002471 ceph_msg_put(con->in_msg);
2472 con->in_msg = NULL;
2473 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002474 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002475 } else {
Alex Elder8921d112012-06-01 14:56:43 -05002476 dout("%s %p in_msg %p msg %p no-op\n",
2477 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002478 }
2479 mutex_unlock(&con->mutex);
2480}
2481
2482/*
Sage Weil31b80062009-10-06 11:31:13 -07002483 * Queue a keepalive byte to ensure the tcp connection is alive.
2484 */
2485void ceph_con_keepalive(struct ceph_connection *con)
2486{
Sage Weile00de342011-03-04 12:25:05 -08002487 dout("con_keepalive %p\n", con);
2488 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002489 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2490 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002491 queue_con(con);
2492}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002493EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002494
2495
2496/*
2497 * construct a new message with given type, size
2498 * the new msg has a ref count of 1.
2499 */
Sage Weilb61c2762011-08-09 15:03:46 -07002500struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2501 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002502{
2503 struct ceph_msg *m;
2504
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002505 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002506 if (m == NULL)
2507 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002508 kref_init(&m->kref);
Alex Elder38941f82012-06-01 14:56:43 -05002509
2510 m->con = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002511 INIT_LIST_HEAD(&m->list_head);
2512
Sage Weil45c6ceb2010-05-11 15:01:51 -07002513 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002514 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002515 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2516 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002517 m->hdr.front_len = cpu_to_le32(front_len);
2518 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002519 m->hdr.data_len = 0;
2520 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002521 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002522 m->footer.front_crc = 0;
2523 m->footer.middle_crc = 0;
2524 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002525 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002526 m->front_max = front_len;
2527 m->front_is_vmalloc = false;
2528 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002529 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002530 m->pool = NULL;
2531
Henry C Changca208922011-05-03 02:29:56 +00002532 /* middle */
2533 m->middle = NULL;
2534
2535 /* data */
2536 m->nr_pages = 0;
2537 m->page_alignment = 0;
2538 m->pages = NULL;
2539 m->pagelist = NULL;
2540 m->bio = NULL;
2541 m->bio_iter = NULL;
2542 m->bio_seg = 0;
2543 m->trail = NULL;
2544
Sage Weil31b80062009-10-06 11:31:13 -07002545 /* front */
2546 if (front_len) {
2547 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002548 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002549 PAGE_KERNEL);
2550 m->front_is_vmalloc = true;
2551 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002552 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002553 }
2554 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002555 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002556 front_len);
2557 goto out2;
2558 }
2559 } else {
2560 m->front.iov_base = NULL;
2561 }
2562 m->front.iov_len = front_len;
2563
Sage Weilbb257662010-04-01 16:07:23 -07002564 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002565 return m;
2566
2567out2:
2568 ceph_msg_put(m);
2569out:
Sage Weilb61c2762011-08-09 15:03:46 -07002570 if (!can_fail) {
2571 pr_err("msg_new can't create type %d front %d\n", type,
2572 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002573 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002574 } else {
2575 dout("msg_new can't create type %d front %d\n", type,
2576 front_len);
2577 }
Sage Weila79832f2010-04-01 16:06:19 -07002578 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002579}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002580EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002581
2582/*
Sage Weil31b80062009-10-06 11:31:13 -07002583 * Allocate "middle" portion of a message, if it is needed and wasn't
2584 * allocated by alloc_msg. This allows us to read a small fixed-size
2585 * per-type header in the front and then gracefully fail (i.e.,
2586 * propagate the error to the caller based on info in the front) when
2587 * the middle is too large.
2588 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002589static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002590{
2591 int type = le16_to_cpu(msg->hdr.type);
2592 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2593
2594 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2595 ceph_msg_type_name(type), middle_len);
2596 BUG_ON(!middle_len);
2597 BUG_ON(msg->middle);
2598
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002599 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002600 if (!msg->middle)
2601 return -ENOMEM;
2602 return 0;
2603}
2604
Yehuda Sadeh24504182010-01-08 13:58:34 -08002605/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05002606 * Allocate a message for receiving an incoming message on a
2607 * connection, and save the result in con->in_msg. Uses the
2608 * connection's private alloc_msg op if available.
2609 *
2610 * Returns true if the message should be skipped, false otherwise.
2611 * If true is returned (skip message), con->in_msg will be NULL.
2612 * If false is returned, con->in_msg will contain a pointer to the
2613 * newly-allocated message, or NULL in case of memory exhaustion.
Yehuda Sadeh24504182010-01-08 13:58:34 -08002614 */
Alex Elder1c20f2d2012-06-04 14:43:32 -05002615static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2616 struct ceph_msg_header *hdr)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002617{
2618 int type = le16_to_cpu(hdr->type);
2619 int front_len = le32_to_cpu(hdr->front_len);
2620 int middle_len = le32_to_cpu(hdr->middle_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002621 int ret;
2622
Alex Elder1c20f2d2012-06-04 14:43:32 -05002623 BUG_ON(con->in_msg != NULL);
2624
Yehuda Sadeh24504182010-01-08 13:58:34 -08002625 if (con->ops->alloc_msg) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002626 int skip = 0;
2627
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002628 mutex_unlock(&con->mutex);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002629 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002630 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002631 if (con->in_msg) {
Sage Weil36eb71a2012-06-21 12:47:08 -07002632 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002633 BUG_ON(con->in_msg->con == NULL);
2634 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002635 if (skip)
2636 con->in_msg = NULL;
2637
2638 if (!con->in_msg)
2639 return skip != 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002640 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002641 if (!con->in_msg) {
2642 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2643 if (!con->in_msg) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002644 pr_err("unable to allocate msg type %d len %d\n",
2645 type, front_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002646 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002647 }
Sage Weil36eb71a2012-06-21 12:47:08 -07002648 con->in_msg->con = con->ops->get(con);
Alex Elder92ce0342012-06-04 14:43:33 -05002649 BUG_ON(con->in_msg->con == NULL);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002650 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002651 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002652 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002653
Alex Elder1c20f2d2012-06-04 14:43:32 -05002654 if (middle_len && !con->in_msg->middle) {
2655 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002656 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002657 ceph_msg_put(con->in_msg);
2658 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002659 }
2660 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002661
Alex Elder1c20f2d2012-06-04 14:43:32 -05002662 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002663}
2664
Sage Weil31b80062009-10-06 11:31:13 -07002665
2666/*
2667 * Free a generically kmalloc'd message.
2668 */
2669void ceph_msg_kfree(struct ceph_msg *m)
2670{
2671 dout("msg_kfree %p\n", m);
2672 if (m->front_is_vmalloc)
2673 vfree(m->front.iov_base);
2674 else
2675 kfree(m->front.iov_base);
2676 kfree(m);
2677}
2678
2679/*
2680 * Drop a msg ref. Destroy as needed.
2681 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002682void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002683{
Sage Weilc2e552e2009-12-07 15:55:05 -08002684 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002685
Sage Weilc2e552e2009-12-07 15:55:05 -08002686 dout("ceph_msg_put last one on %p\n", m);
2687 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002688
Sage Weilc2e552e2009-12-07 15:55:05 -08002689 /* drop middle, data, if any */
2690 if (m->middle) {
2691 ceph_buffer_put(m->middle);
2692 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002693 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002694 m->nr_pages = 0;
2695 m->pages = NULL;
2696
Sage Weil58bb3b32009-12-23 12:12:31 -08002697 if (m->pagelist) {
2698 ceph_pagelist_release(m->pagelist);
2699 kfree(m->pagelist);
2700 m->pagelist = NULL;
2701 }
2702
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002703 m->trail = NULL;
2704
Sage Weilc2e552e2009-12-07 15:55:05 -08002705 if (m->pool)
2706 ceph_msgpool_put(m->pool, m);
2707 else
2708 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002709}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002710EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002711
2712void ceph_msg_dump(struct ceph_msg *msg)
2713{
2714 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2715 msg->front_max, msg->nr_pages);
2716 print_hex_dump(KERN_DEBUG, "header: ",
2717 DUMP_PREFIX_OFFSET, 16, 1,
2718 &msg->hdr, sizeof(msg->hdr), true);
2719 print_hex_dump(KERN_DEBUG, " front: ",
2720 DUMP_PREFIX_OFFSET, 16, 1,
2721 msg->front.iov_base, msg->front.iov_len, true);
2722 if (msg->middle)
2723 print_hex_dump(KERN_DEBUG, "middle: ",
2724 DUMP_PREFIX_OFFSET, 16, 1,
2725 msg->middle->vec.iov_base,
2726 msg->middle->vec.iov_len, true);
2727 print_hex_dump(KERN_DEBUG, "footer: ",
2728 DUMP_PREFIX_OFFSET, 16, 1,
2729 &msg->footer, sizeof(msg->footer), true);
2730}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002731EXPORT_SYMBOL(ceph_msg_dump);