blob: 88ac083bb995f512186b85a669e7c93da801d8ef [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 Weilf91d3472010-07-01 15:18:31 -0700324 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
325 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700326 if (ret == -EINPROGRESS) {
327 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700328 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700329 sock->sk->sk_state);
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600330 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700331 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700332 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700333 sock_release(sock);
Sage Weil31b80062009-10-06 11:31:13 -0700334 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -0700335
Alex Elder41617d02012-02-14 14:05:33 -0600336 return ret;
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600337 }
338 con->sock = sock;
Alex Elderce2c8902012-05-22 22:15:49 -0500339 con_sock_state_connecting(con);
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600340
Alex Elder41617d02012-02-14 14:05:33 -0600341 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700342}
343
344static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
345{
346 struct kvec iov = {buf, len};
347 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800348 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700349
Sage Weil98bdb0a2011-01-25 08:17:48 -0800350 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
351 if (r == -EAGAIN)
352 r = 0;
353 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700354}
355
356/*
357 * write something. @more is true if caller will be sending more data
358 * shortly.
359 */
360static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
361 size_t kvlen, size_t len, int more)
362{
363 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800364 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700365
366 if (more)
367 msg.msg_flags |= MSG_MORE;
368 else
369 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
370
Sage Weil42961d22011-01-25 08:19:34 -0800371 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
372 if (r == -EAGAIN)
373 r = 0;
374 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700375}
376
Alex Elder31739132012-03-07 11:40:08 -0600377static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
378 int offset, size_t size, int more)
379{
380 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
381 int ret;
382
383 ret = kernel_sendpage(sock, page, offset, size, flags);
384 if (ret == -EAGAIN)
385 ret = 0;
386
387 return ret;
388}
389
Sage Weil31b80062009-10-06 11:31:13 -0700390
391/*
392 * Shutdown/close the socket for the given connection.
393 */
394static int con_close_socket(struct ceph_connection *con)
395{
396 int rc;
397
398 dout("con_close_socket on %p sock %p\n", con, con->sock);
399 if (!con->sock)
400 return 0;
401 set_bit(SOCK_CLOSED, &con->state);
402 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
403 sock_release(con->sock);
404 con->sock = NULL;
405 clear_bit(SOCK_CLOSED, &con->state);
Alex Elderce2c8902012-05-22 22:15:49 -0500406 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700407 return rc;
408}
409
410/*
411 * Reset a connection. Discard all incoming and outgoing messages
412 * and clear *_seq state.
413 */
414static void ceph_msg_remove(struct ceph_msg *msg)
415{
416 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500417 BUG_ON(msg->con == NULL);
Alex Elder92ce0342012-06-04 14:43:33 -0500418 ceph_con_put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -0500419 msg->con = NULL;
420
Sage Weil31b80062009-10-06 11:31:13 -0700421 ceph_msg_put(msg);
422}
423static void ceph_msg_remove_list(struct list_head *head)
424{
425 while (!list_empty(head)) {
426 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
427 list_head);
428 ceph_msg_remove(msg);
429 }
430}
431
432static void reset_connection(struct ceph_connection *con)
433{
434 /* reset connection, out_queue, msg_ and connect_seq */
435 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700436 ceph_msg_remove_list(&con->out_queue);
437 ceph_msg_remove_list(&con->out_sent);
438
Sage Weilcf3e5c42009-12-11 09:48:05 -0800439 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500440 BUG_ON(con->in_msg->con != con);
441 con->in_msg->con = NULL;
Sage Weilcf3e5c42009-12-11 09:48:05 -0800442 ceph_msg_put(con->in_msg);
443 con->in_msg = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -0500444 ceph_con_put(con->in_msg->con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800445 }
446
Sage Weil31b80062009-10-06 11:31:13 -0700447 con->connect_seq = 0;
448 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800449 if (con->out_msg) {
450 ceph_msg_put(con->out_msg);
451 con->out_msg = NULL;
452 }
Sage Weil31b80062009-10-06 11:31:13 -0700453 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700454 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700455}
456
457/*
458 * mark a peer down. drop any open connections.
459 */
460void ceph_con_close(struct ceph_connection *con)
461{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700462 dout("con_close %p peer %s\n", con,
463 ceph_pr_addr(&con->peer_addr.in_addr));
Alex Eldera5988c42012-05-29 11:04:58 -0500464 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700465 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Alex Eldera5988c42012-05-29 11:04:58 -0500466 set_bit(CLOSED, &con->state);
467
Alex Elder928443c2012-05-22 11:41:43 -0500468 clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */
469 clear_bit(KEEPALIVE_PENDING, &con->flags);
470 clear_bit(WRITE_PENDING, &con->flags);
Alex Eldera5988c42012-05-29 11:04:58 -0500471
Sage Weilec302642009-12-22 10:43:42 -0800472 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700473 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700474 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800475 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800476 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700477 queue_con(con);
478}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700479EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700480
481/*
Sage Weil31b80062009-10-06 11:31:13 -0700482 * Reopen a closed connection, with a new peer address.
483 */
484void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
485{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700486 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700487 set_bit(OPENING, &con->state);
Alex Eldera5988c42012-05-29 11:04:58 -0500488 WARN_ON(!test_and_clear_bit(CLOSED, &con->state));
489
Sage Weil31b80062009-10-06 11:31:13 -0700490 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800491 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700492 queue_con(con);
493}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700494EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700495
496/*
Sage Weil87b315a2010-03-22 14:51:18 -0700497 * return true if this connection ever successfully opened
498 */
499bool ceph_con_opened(struct ceph_connection *con)
500{
501 return con->connect_seq > 0;
502}
503
504/*
Sage Weil31b80062009-10-06 11:31:13 -0700505 * generic get/put
506 */
507struct ceph_connection *ceph_con_get(struct ceph_connection *con)
508{
Alex Elderd3002b92012-02-14 14:05:33 -0600509 int nref = __atomic_add_unless(&con->nref, 1, 0);
510
511 dout("con_get %p nref = %d -> %d\n", con, nref, nref + 1);
512
513 return nref ? con : NULL;
Sage Weil31b80062009-10-06 11:31:13 -0700514}
515
516void ceph_con_put(struct ceph_connection *con)
517{
Alex Elderd3002b92012-02-14 14:05:33 -0600518 int nref = atomic_dec_return(&con->nref);
519
520 BUG_ON(nref < 0);
521 if (nref == 0) {
Sage Weil71ececd2009-11-18 11:27:06 -0800522 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -0700523 kfree(con);
524 }
Alex Elderd3002b92012-02-14 14:05:33 -0600525 dout("con_put %p nref = %d -> %d\n", con, nref + 1, nref);
Sage Weil31b80062009-10-06 11:31:13 -0700526}
527
528/*
529 * initialize a new connection.
530 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500531void ceph_con_init(struct ceph_connection *con, void *private,
532 const struct ceph_connection_operations *ops,
533 struct ceph_messenger *msgr, __u8 entity_type, __u64 entity_num)
Sage Weil31b80062009-10-06 11:31:13 -0700534{
535 dout("con_init %p\n", con);
536 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500537 con->private = private;
538 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700539 atomic_set(&con->nref, 1);
540 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500541
542 con_sock_state_init(con);
543
Alex Elder1bfd89f2012-05-26 23:26:43 -0500544 con->peer_name.type = (__u8) entity_type;
545 con->peer_name.num = cpu_to_le64(entity_num);
546
Sage Weilec302642009-12-22 10:43:42 -0800547 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700548 INIT_LIST_HEAD(&con->out_queue);
549 INIT_LIST_HEAD(&con->out_sent);
550 INIT_DELAYED_WORK(&con->work, con_work);
Alex Eldera5988c42012-05-29 11:04:58 -0500551
552 set_bit(CLOSED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700553}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700554EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700555
556
557/*
558 * We maintain a global counter to order connection attempts. Get
559 * a unique seq greater than @gt.
560 */
561static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
562{
563 u32 ret;
564
565 spin_lock(&msgr->global_seq_lock);
566 if (msgr->global_seq < gt)
567 msgr->global_seq = gt;
568 ret = ++msgr->global_seq;
569 spin_unlock(&msgr->global_seq_lock);
570 return ret;
571}
572
Alex Eldere2200422012-05-23 14:35:23 -0500573static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600574{
575 con->out_kvec_left = 0;
576 con->out_kvec_bytes = 0;
577 con->out_kvec_cur = &con->out_kvec[0];
578}
579
Alex Eldere2200422012-05-23 14:35:23 -0500580static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600581 size_t size, void *data)
582{
583 int index;
584
585 index = con->out_kvec_left;
586 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
587
588 con->out_kvec[index].iov_len = size;
589 con->out_kvec[index].iov_base = data;
590 con->out_kvec_left++;
591 con->out_kvec_bytes += size;
592}
Sage Weil31b80062009-10-06 11:31:13 -0700593
594/*
595 * Prepare footer for currently outgoing message, and finish things
596 * off. Assumes out_kvec* are already valid.. we just add on to the end.
597 */
Alex Elder859eb792012-02-14 14:05:33 -0600598static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700599{
600 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600601 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700602
603 dout("prepare_write_message_footer %p\n", con);
604 con->out_kvec_is_msg = true;
605 con->out_kvec[v].iov_base = &m->footer;
606 con->out_kvec[v].iov_len = sizeof(m->footer);
607 con->out_kvec_bytes += sizeof(m->footer);
608 con->out_kvec_left++;
609 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800610 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700611}
612
613/*
614 * Prepare headers for the next outgoing message.
615 */
616static void prepare_write_message(struct ceph_connection *con)
617{
618 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600619 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700620
Alex Eldere2200422012-05-23 14:35:23 -0500621 con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700622 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800623 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700624
625 /* Sneak an ack in there first? If we can get it into the same
626 * TCP packet that's a good thing. */
627 if (con->in_seq > con->in_seq_acked) {
628 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -0500629 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700630 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500631 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600632 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700633 }
634
Alex Elder38941f82012-06-01 14:56:43 -0500635 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -0600636 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800637 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -0500638 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -0700639
640 /* put message on sent list */
641 ceph_msg_get(m);
642 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700643
Sage Weile84346b2010-05-11 21:20:38 -0700644 /*
645 * only assign outgoing seq # if we haven't sent this message
646 * yet. if it is requeued, resend with it's original seq.
647 */
648 if (m->needs_out_seq) {
649 m->hdr.seq = cpu_to_le64(++con->out_seq);
650 m->needs_out_seq = false;
651 }
Sage Weil31b80062009-10-06 11:31:13 -0700652
653 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
654 m, con->out_seq, le16_to_cpu(m->hdr.type),
655 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
656 le32_to_cpu(m->hdr.data_len),
657 m->nr_pages);
658 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
659
660 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -0500661 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
662 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
663 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600664
Sage Weil31b80062009-10-06 11:31:13 -0700665 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -0500666 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600667 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700668
669 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600670 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
671 con->out_msg->hdr.crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -0700672 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
Alex Eldera9a0c512012-02-15 07:43:54 -0600673
674 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
675 con->out_msg->footer.front_crc = cpu_to_le32(crc);
676 if (m->middle) {
677 crc = crc32c(0, m->middle->vec.iov_base,
678 m->middle->vec.iov_len);
679 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
680 } else
Sage Weil31b80062009-10-06 11:31:13 -0700681 con->out_msg->footer.middle_crc = 0;
682 con->out_msg->footer.data_crc = 0;
683 dout("prepare_write_message front_crc %u data_crc %u\n",
684 le32_to_cpu(con->out_msg->footer.front_crc),
685 le32_to_cpu(con->out_msg->footer.middle_crc));
686
687 /* is there a data payload? */
688 if (le32_to_cpu(m->hdr.data_len) > 0) {
689 /* initialize page iterator */
690 con->out_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700691 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -0800692 con->out_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700693 else
694 con->out_msg_pos.page_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700695 con->out_msg_pos.data_pos = 0;
Alex Elderbca064d2012-02-15 07:43:54 -0600696 con->out_msg_pos.did_page_crc = false;
Sage Weil31b80062009-10-06 11:31:13 -0700697 con->out_more = 1; /* data + footer will follow */
698 } else {
699 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600700 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700701 }
702
Alex Elder928443c2012-05-22 11:41:43 -0500703 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700704}
705
706/*
707 * Prepare an ack.
708 */
709static void prepare_write_ack(struct ceph_connection *con)
710{
711 dout("prepare_write_ack %p %llu -> %llu\n", con,
712 con->in_seq_acked, con->in_seq);
713 con->in_seq_acked = con->in_seq;
714
Alex Eldere2200422012-05-23 14:35:23 -0500715 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600716
Alex Eldere2200422012-05-23 14:35:23 -0500717 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600718
Sage Weil31b80062009-10-06 11:31:13 -0700719 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500720 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600721 &con->out_temp_ack);
722
Sage Weil31b80062009-10-06 11:31:13 -0700723 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elder928443c2012-05-22 11:41:43 -0500724 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700725}
726
727/*
728 * Prepare to write keepalive byte.
729 */
730static void prepare_write_keepalive(struct ceph_connection *con)
731{
732 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -0500733 con_out_kvec_reset(con);
734 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Alex Elder928443c2012-05-22 11:41:43 -0500735 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700736}
737
738/*
739 * Connection negotiation.
740 */
741
Alex Elderdac1e712012-05-16 15:16:39 -0500742static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
743 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800744{
Alex Eldera3530df2012-05-16 15:16:39 -0500745 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -0500746
747 if (!con->ops->get_authorizer) {
748 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
749 con->out_connect.authorizer_len = 0;
750
Alex Elder729796b2012-05-16 15:16:39 -0500751 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -0500752 }
753
754 /* Can't hold the mutex while getting authorizer */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800755
Sage Weilec302642009-12-22 10:43:42 -0800756 mutex_unlock(&con->mutex);
Alex Elderb1c6b982012-05-16 15:16:38 -0500757
Alex Elderdac1e712012-05-16 15:16:39 -0500758 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Elder8f43fb52012-05-16 15:16:39 -0500759
Sage Weilec302642009-12-22 10:43:42 -0800760 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800761
Alex Eldera3530df2012-05-16 15:16:39 -0500762 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -0500763 return auth;
Alex Elder928443c2012-05-22 11:41:43 -0500764 if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags))
Alex Elder729796b2012-05-16 15:16:39 -0500765 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700766
Alex Elder8f43fb52012-05-16 15:16:39 -0500767 con->auth_reply_buf = auth->authorizer_reply_buf;
768 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
769
Alex Elder859eb792012-02-14 14:05:33 -0600770
Alex Elder729796b2012-05-16 15:16:39 -0500771 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800772}
773
Sage Weil31b80062009-10-06 11:31:13 -0700774/*
775 * We connected to a peer and are saying hello.
776 */
Alex Eldere825a662012-05-16 15:16:38 -0500777static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700778{
Alex Eldere2200422012-05-23 14:35:23 -0500779 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
780 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -0500781 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800782
Sage Weileed0ef22009-11-10 14:34:36 -0800783 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500784 set_bit(WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800785}
786
Alex Eldere825a662012-05-16 15:16:38 -0500787static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800788{
Sage Weil31b80062009-10-06 11:31:13 -0700789 unsigned global_seq = get_global_seq(con->msgr, 0);
790 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -0500791 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -0500792 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700793
794 switch (con->peer_name.type) {
795 case CEPH_ENTITY_TYPE_MON:
796 proto = CEPH_MONC_PROTOCOL;
797 break;
798 case CEPH_ENTITY_TYPE_OSD:
799 proto = CEPH_OSDC_PROTOCOL;
800 break;
801 case CEPH_ENTITY_TYPE_MDS:
802 proto = CEPH_MDSC_PROTOCOL;
803 break;
804 default:
805 BUG();
806 }
807
808 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
809 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800810
Alex Eldere825a662012-05-16 15:16:38 -0500811 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700812 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
813 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
814 con->out_connect.global_seq = cpu_to_le32(global_seq);
815 con->out_connect.protocol_version = cpu_to_le32(proto);
816 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700817
Alex Elderdac1e712012-05-16 15:16:39 -0500818 auth_proto = CEPH_AUTH_UNKNOWN;
819 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -0500820 if (IS_ERR(auth))
821 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -0500822
Alex Elderdac1e712012-05-16 15:16:39 -0500823 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -0500824 con->out_connect.authorizer_len = auth ?
825 cpu_to_le32(auth->authorizer_buf_len) : 0;
826
Alex Eldere2200422012-05-23 14:35:23 -0500827 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -0500828 &con->out_connect);
829 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -0500830 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -0500831 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600832
Sage Weil31b80062009-10-06 11:31:13 -0700833 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500834 set_bit(WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800835
Alex Eldere10c7582012-05-16 15:16:38 -0500836 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700837}
838
Sage Weil31b80062009-10-06 11:31:13 -0700839/*
840 * write as much of pending kvecs to the socket as we can.
841 * 1 -> done
842 * 0 -> socket full, but more to do
843 * <0 -> error
844 */
845static int write_partial_kvec(struct ceph_connection *con)
846{
847 int ret;
848
849 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
850 while (con->out_kvec_bytes > 0) {
851 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
852 con->out_kvec_left, con->out_kvec_bytes,
853 con->out_more);
854 if (ret <= 0)
855 goto out;
856 con->out_kvec_bytes -= ret;
857 if (con->out_kvec_bytes == 0)
858 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600859
860 /* account for full iov entries consumed */
861 while (ret >= con->out_kvec_cur->iov_len) {
862 BUG_ON(!con->out_kvec_left);
863 ret -= con->out_kvec_cur->iov_len;
864 con->out_kvec_cur++;
865 con->out_kvec_left--;
866 }
867 /* and for a partially-consumed entry */
868 if (ret) {
869 con->out_kvec_cur->iov_len -= ret;
870 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700871 }
872 }
873 con->out_kvec_left = 0;
874 con->out_kvec_is_msg = false;
875 ret = 1;
876out:
877 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
878 con->out_kvec_bytes, con->out_kvec_left, ret);
879 return ret; /* done! */
880}
881
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700882#ifdef CONFIG_BLOCK
883static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
884{
885 if (!bio) {
886 *iter = NULL;
887 *seg = 0;
888 return;
889 }
890 *iter = bio;
891 *seg = bio->bi_idx;
892}
893
894static void iter_bio_next(struct bio **bio_iter, int *seg)
895{
896 if (*bio_iter == NULL)
897 return;
898
899 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
900
901 (*seg)++;
902 if (*seg == (*bio_iter)->bi_vcnt)
903 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
904}
905#endif
906
Sage Weil31b80062009-10-06 11:31:13 -0700907/*
908 * Write as much message data payload as we can. If we finish, queue
909 * up the footer.
910 * 1 -> done, footer is now queued in out_kvec[].
911 * 0 -> socket full, but more to do
912 * <0 -> error
913 */
914static int write_partial_msg_pages(struct ceph_connection *con)
915{
916 struct ceph_msg *msg = con->out_msg;
917 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
918 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600919 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700920 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700921 int total_max_write;
922 int in_trail = 0;
923 size_t trail_len = (msg->trail ? msg->trail->length : 0);
Sage Weil31b80062009-10-06 11:31:13 -0700924
925 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
926 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
927 con->out_msg_pos.page_pos);
928
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700929#ifdef CONFIG_BLOCK
930 if (msg->bio && !msg->bio_iter)
931 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
932#endif
933
934 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -0700935 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700936 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -0600937 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700938
939 total_max_write = data_len - trail_len -
940 con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -0700941
942 /*
943 * if we are calculating the data crc (the default), we need
944 * to map the page. if our pages[] has been revoked, use the
945 * zero page.
946 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700947
948 /* have we reached the trail part of the data? */
949 if (con->out_msg_pos.data_pos >= data_len - trail_len) {
950 in_trail = 1;
951
952 total_max_write = data_len - con->out_msg_pos.data_pos;
953
954 page = list_first_entry(&msg->trail->head,
955 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700956 max_write = PAGE_SIZE;
957 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -0700958 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -0800959 } else if (msg->pagelist) {
960 page = list_first_entry(&msg->pagelist->head,
961 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700962#ifdef CONFIG_BLOCK
963 } else if (msg->bio) {
964 struct bio_vec *bv;
965
966 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
967 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -0600968 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700969 max_write = bv->bv_len;
970#endif
Sage Weil31b80062009-10-06 11:31:13 -0700971 } else {
Alex Elder57666512012-01-23 15:49:27 -0600972 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -0700973 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700974 len = min_t(int, max_write - con->out_msg_pos.page_pos,
975 total_max_write);
976
Alex Elder37675b02012-03-07 11:40:08 -0600977 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -0600978 void *base;
Alex Eldera9a0c512012-02-15 07:43:54 -0600979 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700980 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -0600981 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -0700982
Alex Elder8d63e312012-03-07 11:40:08 -0600983 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -0700984 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -0600985 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Eldera9a0c512012-02-15 07:43:54 -0600986 crc = crc32c(tmpcrc, base, len);
987 con->out_msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -0600988 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -0700989 }
Alex Eldere36b13c2012-03-07 11:40:08 -0600990 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -0600991 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -0600992 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -0700993
Alex Elder0cdf9e62012-03-07 11:40:08 -0600994 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -0700995 kunmap(page);
996
997 if (ret <= 0)
998 goto out;
999
1000 con->out_msg_pos.data_pos += ret;
1001 con->out_msg_pos.page_pos += ret;
1002 if (ret == len) {
1003 con->out_msg_pos.page_pos = 0;
1004 con->out_msg_pos.page++;
Alex Elderbca064d2012-02-15 07:43:54 -06001005 con->out_msg_pos.did_page_crc = false;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001006 if (in_trail)
1007 list_move_tail(&page->lru,
1008 &msg->trail->head);
1009 else if (msg->pagelist)
Sage Weil58bb3b32009-12-23 12:12:31 -08001010 list_move_tail(&page->lru,
1011 &msg->pagelist->head);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001012#ifdef CONFIG_BLOCK
1013 else if (msg->bio)
1014 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
1015#endif
Sage Weil31b80062009-10-06 11:31:13 -07001016 }
1017 }
1018
1019 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1020
1021 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001022 if (!do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -07001023 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001024 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001025 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001026 ret = 1;
1027out:
1028 return ret;
1029}
1030
1031/*
1032 * write some zeros
1033 */
1034static int write_partial_skip(struct ceph_connection *con)
1035{
1036 int ret;
1037
1038 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001039 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001040
Alex Elder31739132012-03-07 11:40:08 -06001041 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001042 if (ret <= 0)
1043 goto out;
1044 con->out_skip -= ret;
1045 }
1046 ret = 1;
1047out:
1048 return ret;
1049}
1050
1051/*
1052 * Prepare to read connection handshake, or an ack.
1053 */
Sage Weileed0ef22009-11-10 14:34:36 -08001054static void prepare_read_banner(struct ceph_connection *con)
1055{
1056 dout("prepare_read_banner %p\n", con);
1057 con->in_base_pos = 0;
1058}
1059
Sage Weil31b80062009-10-06 11:31:13 -07001060static void prepare_read_connect(struct ceph_connection *con)
1061{
1062 dout("prepare_read_connect %p\n", con);
1063 con->in_base_pos = 0;
1064}
1065
1066static void prepare_read_ack(struct ceph_connection *con)
1067{
1068 dout("prepare_read_ack %p\n", con);
1069 con->in_base_pos = 0;
1070}
1071
1072static void prepare_read_tag(struct ceph_connection *con)
1073{
1074 dout("prepare_read_tag %p\n", con);
1075 con->in_base_pos = 0;
1076 con->in_tag = CEPH_MSGR_TAG_READY;
1077}
1078
1079/*
1080 * Prepare to read a message.
1081 */
1082static int prepare_read_message(struct ceph_connection *con)
1083{
1084 dout("prepare_read_message %p\n", con);
1085 BUG_ON(con->in_msg != NULL);
1086 con->in_base_pos = 0;
1087 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1088 return 0;
1089}
1090
1091
1092static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001093 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001094{
Alex Eldere6cee712012-05-10 10:29:50 -05001095 while (con->in_base_pos < end) {
1096 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001097 int have = size - left;
1098 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1099 if (ret <= 0)
1100 return ret;
1101 con->in_base_pos += ret;
1102 }
1103 return 1;
1104}
1105
1106
1107/*
1108 * Read all or part of the connect-side handshake on a new connection
1109 */
Sage Weileed0ef22009-11-10 14:34:36 -08001110static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001111{
Alex Elderfd516532012-05-10 10:29:50 -05001112 int size;
1113 int end;
1114 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001115
Sage Weileed0ef22009-11-10 14:34:36 -08001116 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001117
1118 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001119 size = strlen(CEPH_BANNER);
1120 end = size;
1121 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001122 if (ret <= 0)
1123 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001124
1125 size = sizeof (con->actual_peer_addr);
1126 end += size;
1127 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001128 if (ret <= 0)
1129 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001130
1131 size = sizeof (con->peer_addr_for_me);
1132 end += size;
1133 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001134 if (ret <= 0)
1135 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001136
Sage Weileed0ef22009-11-10 14:34:36 -08001137out:
1138 return ret;
1139}
1140
1141static int read_partial_connect(struct ceph_connection *con)
1142{
Alex Elderfd516532012-05-10 10:29:50 -05001143 int size;
1144 int end;
1145 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001146
1147 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1148
Alex Elderfd516532012-05-10 10:29:50 -05001149 size = sizeof (con->in_reply);
1150 end = size;
1151 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001152 if (ret <= 0)
1153 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001154
1155 size = le32_to_cpu(con->in_reply.authorizer_len);
1156 end += size;
1157 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001158 if (ret <= 0)
1159 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001160
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001161 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1162 con, (int)con->in_reply.tag,
1163 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001164 le32_to_cpu(con->in_reply.global_seq));
1165out:
1166 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001167
Sage Weil31b80062009-10-06 11:31:13 -07001168}
1169
1170/*
1171 * Verify the hello banner looks okay.
1172 */
1173static int verify_hello(struct ceph_connection *con)
1174{
1175 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001176 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001177 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001178 con->error_msg = "protocol error, bad banner";
1179 return -1;
1180 }
1181 return 0;
1182}
1183
1184static bool addr_is_blank(struct sockaddr_storage *ss)
1185{
1186 switch (ss->ss_family) {
1187 case AF_INET:
1188 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1189 case AF_INET6:
1190 return
1191 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1192 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1193 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1194 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1195 }
1196 return false;
1197}
1198
1199static int addr_port(struct sockaddr_storage *ss)
1200{
1201 switch (ss->ss_family) {
1202 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001203 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001204 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001205 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001206 }
1207 return 0;
1208}
1209
1210static void addr_set_port(struct sockaddr_storage *ss, int p)
1211{
1212 switch (ss->ss_family) {
1213 case AF_INET:
1214 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001215 break;
Sage Weil31b80062009-10-06 11:31:13 -07001216 case AF_INET6:
1217 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001218 break;
Sage Weil31b80062009-10-06 11:31:13 -07001219 }
1220}
1221
1222/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001223 * Unlike other *_pton function semantics, zero indicates success.
1224 */
1225static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1226 char delim, const char **ipend)
1227{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001228 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1229 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001230
1231 memset(ss, 0, sizeof(*ss));
1232
1233 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1234 ss->ss_family = AF_INET;
1235 return 0;
1236 }
1237
1238 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1239 ss->ss_family = AF_INET6;
1240 return 0;
1241 }
1242
1243 return -EINVAL;
1244}
1245
1246/*
1247 * Extract hostname string and resolve using kernel DNS facility.
1248 */
1249#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1250static int ceph_dns_resolve_name(const char *name, size_t namelen,
1251 struct sockaddr_storage *ss, char delim, const char **ipend)
1252{
1253 const char *end, *delim_p;
1254 char *colon_p, *ip_addr = NULL;
1255 int ip_len, ret;
1256
1257 /*
1258 * The end of the hostname occurs immediately preceding the delimiter or
1259 * the port marker (':') where the delimiter takes precedence.
1260 */
1261 delim_p = memchr(name, delim, namelen);
1262 colon_p = memchr(name, ':', namelen);
1263
1264 if (delim_p && colon_p)
1265 end = delim_p < colon_p ? delim_p : colon_p;
1266 else if (!delim_p && colon_p)
1267 end = colon_p;
1268 else {
1269 end = delim_p;
1270 if (!end) /* case: hostname:/ */
1271 end = name + namelen;
1272 }
1273
1274 if (end <= name)
1275 return -EINVAL;
1276
1277 /* do dns_resolve upcall */
1278 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1279 if (ip_len > 0)
1280 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1281 else
1282 ret = -ESRCH;
1283
1284 kfree(ip_addr);
1285
1286 *ipend = end;
1287
1288 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1289 ret, ret ? "failed" : ceph_pr_addr(ss));
1290
1291 return ret;
1292}
1293#else
1294static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1295 struct sockaddr_storage *ss, char delim, const char **ipend)
1296{
1297 return -EINVAL;
1298}
1299#endif
1300
1301/*
1302 * Parse a server name (IP or hostname). If a valid IP address is not found
1303 * then try to extract a hostname to resolve using userspace DNS upcall.
1304 */
1305static int ceph_parse_server_name(const char *name, size_t namelen,
1306 struct sockaddr_storage *ss, char delim, const char **ipend)
1307{
1308 int ret;
1309
1310 ret = ceph_pton(name, namelen, ss, delim, ipend);
1311 if (ret)
1312 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1313
1314 return ret;
1315}
1316
1317/*
Sage Weil31b80062009-10-06 11:31:13 -07001318 * Parse an ip[:port] list into an addr array. Use the default
1319 * monitor port if a port isn't specified.
1320 */
1321int ceph_parse_ips(const char *c, const char *end,
1322 struct ceph_entity_addr *addr,
1323 int max_count, int *count)
1324{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001325 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001326 const char *p = c;
1327
1328 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1329 for (i = 0; i < max_count; i++) {
1330 const char *ipend;
1331 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001332 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001333 char delim = ',';
1334
1335 if (*p == '[') {
1336 delim = ']';
1337 p++;
1338 }
Sage Weil31b80062009-10-06 11:31:13 -07001339
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001340 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1341 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001342 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001343 ret = -EINVAL;
1344
Sage Weil31b80062009-10-06 11:31:13 -07001345 p = ipend;
1346
Sage Weil39139f62010-07-08 09:54:52 -07001347 if (delim == ']') {
1348 if (*p != ']') {
1349 dout("missing matching ']'\n");
1350 goto bad;
1351 }
1352 p++;
1353 }
1354
Sage Weil31b80062009-10-06 11:31:13 -07001355 /* port? */
1356 if (p < end && *p == ':') {
1357 port = 0;
1358 p++;
1359 while (p < end && *p >= '0' && *p <= '9') {
1360 port = (port * 10) + (*p - '0');
1361 p++;
1362 }
1363 if (port > 65535 || port == 0)
1364 goto bad;
1365 } else {
1366 port = CEPH_MON_PORT;
1367 }
1368
1369 addr_set_port(ss, port);
1370
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001371 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001372
1373 if (p == end)
1374 break;
1375 if (*p != ',')
1376 goto bad;
1377 p++;
1378 }
1379
1380 if (p != end)
1381 goto bad;
1382
1383 if (count)
1384 *count = i + 1;
1385 return 0;
1386
1387bad:
Sage Weil39139f62010-07-08 09:54:52 -07001388 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001389 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001390}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001391EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001392
Sage Weileed0ef22009-11-10 14:34:36 -08001393static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001394{
Sage Weileed0ef22009-11-10 14:34:36 -08001395 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001396
1397 if (verify_hello(con) < 0)
1398 return -1;
1399
Sage Weil63f2d212009-11-03 15:17:56 -08001400 ceph_decode_addr(&con->actual_peer_addr);
1401 ceph_decode_addr(&con->peer_addr_for_me);
1402
Sage Weil31b80062009-10-06 11:31:13 -07001403 /*
1404 * Make sure the other end is who we wanted. note that the other
1405 * end may not yet know their ip address, so if it's 0.0.0.0, give
1406 * them the benefit of the doubt.
1407 */
Sage Weil103e2d32010-01-07 16:12:36 -08001408 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1409 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001410 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1411 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001412 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001413 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001414 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001415 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001416 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001417 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001418 return -1;
1419 }
1420
1421 /*
1422 * did we learn our address?
1423 */
1424 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1425 int port = addr_port(&con->msgr->inst.addr.in_addr);
1426
1427 memcpy(&con->msgr->inst.addr.in_addr,
1428 &con->peer_addr_for_me.in_addr,
1429 sizeof(con->peer_addr_for_me.in_addr));
1430 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001431 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001432 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001433 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001434 }
1435
Sage Weileed0ef22009-11-10 14:34:36 -08001436 set_bit(NEGOTIATING, &con->state);
1437 prepare_read_connect(con);
1438 return 0;
1439}
1440
Sage Weil04a419f2009-12-23 09:30:21 -08001441static void fail_protocol(struct ceph_connection *con)
1442{
1443 reset_connection(con);
1444 set_bit(CLOSED, &con->state); /* in case there's queued work */
Sage Weil04a419f2009-12-23 09:30:21 -08001445}
1446
Sage Weileed0ef22009-11-10 14:34:36 -08001447static int process_connect(struct ceph_connection *con)
1448{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001449 u64 sup_feat = con->msgr->supported_features;
1450 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001451 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001452 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001453
Sage Weileed0ef22009-11-10 14:34:36 -08001454 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1455
Sage Weil31b80062009-10-06 11:31:13 -07001456 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001457 case CEPH_MSGR_TAG_FEATURES:
1458 pr_err("%s%lld %s feature set mismatch,"
1459 " my %llx < server's %llx, missing %llx\n",
1460 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001461 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001462 sup_feat, server_feat, server_feat & ~sup_feat);
1463 con->error_msg = "missing required protocol features";
1464 fail_protocol(con);
1465 return -1;
1466
Sage Weil31b80062009-10-06 11:31:13 -07001467 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001468 pr_err("%s%lld %s protocol version mismatch,"
1469 " my %d != server's %d\n",
1470 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001471 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001472 le32_to_cpu(con->out_connect.protocol_version),
1473 le32_to_cpu(con->in_reply.protocol_version));
1474 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001475 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001476 return -1;
1477
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001478 case CEPH_MSGR_TAG_BADAUTHORIZER:
1479 con->auth_retry++;
1480 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1481 con->auth_retry);
1482 if (con->auth_retry == 2) {
1483 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001484 return -1;
1485 }
1486 con->auth_retry = 1;
Alex Eldere2200422012-05-23 14:35:23 -05001487 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001488 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001489 if (ret < 0)
1490 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001491 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001492 break;
Sage Weil31b80062009-10-06 11:31:13 -07001493
1494 case CEPH_MSGR_TAG_RESETSESSION:
1495 /*
1496 * If we connected with a large connect_seq but the peer
1497 * has no record of a session with us (no connection, or
1498 * connect_seq == 0), they will send RESETSESION to indicate
1499 * that they must have reset their session, and may have
1500 * dropped messages.
1501 */
1502 dout("process_connect got RESET peer seq %u\n",
1503 le32_to_cpu(con->in_connect.connect_seq));
1504 pr_err("%s%lld %s connection reset\n",
1505 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001506 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001507 reset_connection(con);
Alex Eldere2200422012-05-23 14:35:23 -05001508 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001509 ret = prepare_write_connect(con);
1510 if (ret < 0)
1511 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001512 prepare_read_connect(con);
1513
1514 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001515 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001516 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1517 if (con->ops->peer_reset)
1518 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001519 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07001520 if (test_bit(CLOSED, &con->state) ||
1521 test_bit(OPENING, &con->state))
1522 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001523 break;
1524
1525 case CEPH_MSGR_TAG_RETRY_SESSION:
1526 /*
1527 * If we sent a smaller connect_seq than the peer has, try
1528 * again with a larger value.
1529 */
1530 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1531 le32_to_cpu(con->out_connect.connect_seq),
1532 le32_to_cpu(con->in_connect.connect_seq));
1533 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Alex Eldere2200422012-05-23 14:35:23 -05001534 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001535 ret = prepare_write_connect(con);
1536 if (ret < 0)
1537 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001538 prepare_read_connect(con);
1539 break;
1540
1541 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1542 /*
1543 * If we sent a smaller global_seq than the peer has, try
1544 * again with a larger value.
1545 */
Sage Weileed0ef22009-11-10 14:34:36 -08001546 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001547 con->peer_global_seq,
1548 le32_to_cpu(con->in_connect.global_seq));
1549 get_global_seq(con->msgr,
1550 le32_to_cpu(con->in_connect.global_seq));
Alex Eldere2200422012-05-23 14:35:23 -05001551 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001552 ret = prepare_write_connect(con);
1553 if (ret < 0)
1554 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001555 prepare_read_connect(con);
1556 break;
1557
1558 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001559 if (req_feat & ~server_feat) {
1560 pr_err("%s%lld %s protocol feature mismatch,"
1561 " my required %llx > server's %llx, need %llx\n",
1562 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001563 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001564 req_feat, server_feat, req_feat & ~server_feat);
1565 con->error_msg = "missing required protocol features";
1566 fail_protocol(con);
1567 return -1;
1568 }
Sage Weil31b80062009-10-06 11:31:13 -07001569 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001570 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1571 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001572 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001573 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1574 con->peer_global_seq,
1575 le32_to_cpu(con->in_reply.connect_seq),
1576 con->connect_seq);
1577 WARN_ON(con->connect_seq !=
1578 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001579
1580 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elder928443c2012-05-22 11:41:43 -05001581 set_bit(LOSSYTX, &con->flags);
Sage Weil92ac41d2009-12-14 14:56:56 -08001582
Sage Weil31b80062009-10-06 11:31:13 -07001583 prepare_read_tag(con);
1584 break;
1585
1586 case CEPH_MSGR_TAG_WAIT:
1587 /*
1588 * If there is a connection race (we are opening
1589 * connections to each other), one of us may just have
1590 * to WAIT. This shouldn't happen if we are the
1591 * client.
1592 */
Sage Weil04177882011-05-12 15:33:17 -07001593 pr_err("process_connect got WAIT as client\n");
1594 con->error_msg = "protocol error, got WAIT as client";
1595 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001596
1597 default:
1598 pr_err("connect protocol error, will retry\n");
1599 con->error_msg = "protocol error, garbage tag during connect";
1600 return -1;
1601 }
1602 return 0;
1603}
1604
1605
1606/*
1607 * read (part of) an ack
1608 */
1609static int read_partial_ack(struct ceph_connection *con)
1610{
Alex Elderfd516532012-05-10 10:29:50 -05001611 int size = sizeof (con->in_temp_ack);
1612 int end = size;
1613
1614 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001615}
1616
1617
1618/*
1619 * We can finally discard anything that's been acked.
1620 */
1621static void process_ack(struct ceph_connection *con)
1622{
1623 struct ceph_msg *m;
1624 u64 ack = le64_to_cpu(con->in_temp_ack);
1625 u64 seq;
1626
Sage Weil31b80062009-10-06 11:31:13 -07001627 while (!list_empty(&con->out_sent)) {
1628 m = list_first_entry(&con->out_sent, struct ceph_msg,
1629 list_head);
1630 seq = le64_to_cpu(m->hdr.seq);
1631 if (seq > ack)
1632 break;
1633 dout("got ack for seq %llu type %d at %p\n", seq,
1634 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001635 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001636 ceph_msg_remove(m);
1637 }
Sage Weil31b80062009-10-06 11:31:13 -07001638 prepare_read_tag(con);
1639}
1640
1641
1642
1643
Yehuda Sadeh24504182010-01-08 13:58:34 -08001644static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001645 struct kvec *section,
1646 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001647{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001648 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001649
Yehuda Sadeh24504182010-01-08 13:58:34 -08001650 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001651
Yehuda Sadeh24504182010-01-08 13:58:34 -08001652 while (section->iov_len < sec_len) {
1653 BUG_ON(section->iov_base == NULL);
1654 left = sec_len - section->iov_len;
1655 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1656 section->iov_len, left);
1657 if (ret <= 0)
1658 return ret;
1659 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001660 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001661 if (section->iov_len == sec_len)
1662 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001663
1664 return 1;
1665}
1666
Alex Elder1c20f2d2012-06-04 14:43:32 -05001667static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
1668 struct ceph_msg_header *hdr);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001669
1670
1671static int read_partial_message_pages(struct ceph_connection *con,
1672 struct page **pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001673 unsigned data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001674{
1675 void *p;
1676 int ret;
1677 int left;
1678
1679 left = min((int)(data_len - con->in_msg_pos.data_pos),
1680 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1681 /* (page) data */
1682 BUG_ON(pages == NULL);
1683 p = kmap(pages[con->in_msg_pos.page]);
1684 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1685 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001686 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001687 con->in_data_crc =
1688 crc32c(con->in_data_crc,
1689 p + con->in_msg_pos.page_pos, ret);
1690 kunmap(pages[con->in_msg_pos.page]);
1691 if (ret <= 0)
1692 return ret;
1693 con->in_msg_pos.data_pos += ret;
1694 con->in_msg_pos.page_pos += ret;
1695 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1696 con->in_msg_pos.page_pos = 0;
1697 con->in_msg_pos.page++;
1698 }
1699
1700 return ret;
1701}
1702
1703#ifdef CONFIG_BLOCK
1704static int read_partial_message_bio(struct ceph_connection *con,
1705 struct bio **bio_iter, int *bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001706 unsigned data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001707{
1708 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1709 void *p;
1710 int ret, left;
1711
1712 if (IS_ERR(bv))
1713 return PTR_ERR(bv);
1714
1715 left = min((int)(data_len - con->in_msg_pos.data_pos),
1716 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1717
1718 p = kmap(bv->bv_page) + bv->bv_offset;
1719
1720 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1721 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001722 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001723 con->in_data_crc =
1724 crc32c(con->in_data_crc,
1725 p + con->in_msg_pos.page_pos, ret);
1726 kunmap(bv->bv_page);
1727 if (ret <= 0)
1728 return ret;
1729 con->in_msg_pos.data_pos += ret;
1730 con->in_msg_pos.page_pos += ret;
1731 if (con->in_msg_pos.page_pos == bv->bv_len) {
1732 con->in_msg_pos.page_pos = 0;
1733 iter_bio_next(bio_iter, bio_seg);
1734 }
1735
1736 return ret;
1737}
1738#endif
1739
Sage Weil31b80062009-10-06 11:31:13 -07001740/*
1741 * read (part of) a message.
1742 */
1743static int read_partial_message(struct ceph_connection *con)
1744{
1745 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05001746 int size;
1747 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001748 int ret;
Sage Weilc5c6b192010-11-09 12:40:00 -08001749 unsigned front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001750 bool do_datacrc = !con->msgr->nocrc;
Sage Weilae187562010-04-22 07:47:01 -07001751 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001752 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001753
1754 dout("read_partial_message con %p msg %p\n", con, m);
1755
1756 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05001757 size = sizeof (con->in_hdr);
1758 end = size;
1759 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05001760 if (ret <= 0)
1761 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001762
1763 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1764 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1765 pr_err("read_partial_message bad hdr "
1766 " crc %u != expected %u\n",
1767 crc, con->in_hdr.crc);
1768 return -EBADMSG;
1769 }
1770
Sage Weil31b80062009-10-06 11:31:13 -07001771 front_len = le32_to_cpu(con->in_hdr.front_len);
1772 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1773 return -EIO;
1774 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1775 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1776 return -EIO;
1777 data_len = le32_to_cpu(con->in_hdr.data_len);
1778 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1779 return -EIO;
1780
Sage Weilae187562010-04-22 07:47:01 -07001781 /* verify seq# */
1782 seq = le64_to_cpu(con->in_hdr.seq);
1783 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001784 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001785 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001786 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001787 seq, con->in_seq + 1);
1788 con->in_base_pos = -front_len - middle_len - data_len -
1789 sizeof(m->footer);
1790 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001791 return 0;
1792 } else if ((s64)seq - (s64)con->in_seq > 1) {
1793 pr_err("read_partial_message bad seq %lld expected %lld\n",
1794 seq, con->in_seq + 1);
1795 con->error_msg = "bad message sequence # for incoming message";
1796 return -EBADMSG;
1797 }
1798
Sage Weil31b80062009-10-06 11:31:13 -07001799 /* allocate message? */
1800 if (!con->in_msg) {
1801 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1802 con->in_hdr.front_len, con->in_hdr.data_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05001803 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
Sage Weil31b80062009-10-06 11:31:13 -07001804 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001805 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001806 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001807 con->in_base_pos = -front_len - middle_len - data_len -
1808 sizeof(m->footer);
1809 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001810 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001811 return 0;
1812 }
Sage Weila79832f2010-04-01 16:06:19 -07001813 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001814 con->error_msg =
1815 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001816 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001817 }
Alex Elder38941f82012-06-01 14:56:43 -05001818
1819 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07001820 m = con->in_msg;
1821 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001822 if (m->middle)
1823 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001824
1825 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001826 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001827 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001828 else
1829 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001830 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001831 }
1832
1833 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001834 ret = read_partial_message_section(con, &m->front, front_len,
1835 &con->in_front_crc);
1836 if (ret <= 0)
1837 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001838
1839 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001840 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001841 ret = read_partial_message_section(con, &m->middle->vec,
1842 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001843 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001844 if (ret <= 0)
1845 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001846 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001847#ifdef CONFIG_BLOCK
1848 if (m->bio && !m->bio_iter)
1849 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1850#endif
Sage Weil31b80062009-10-06 11:31:13 -07001851
1852 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001853 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001854 if (m->pages) {
1855 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001856 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001857 if (ret <= 0)
1858 return ret;
1859#ifdef CONFIG_BLOCK
1860 } else if (m->bio) {
1861
1862 ret = read_partial_message_bio(con,
1863 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001864 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001865 if (ret <= 0)
1866 return ret;
1867#endif
1868 } else {
1869 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001870 }
1871 }
1872
Sage Weil31b80062009-10-06 11:31:13 -07001873 /* footer */
Alex Elderfd516532012-05-10 10:29:50 -05001874 size = sizeof (m->footer);
1875 end += size;
1876 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05001877 if (ret <= 0)
1878 return ret;
1879
Sage Weil31b80062009-10-06 11:31:13 -07001880 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1881 m, front_len, m->footer.front_crc, middle_len,
1882 m->footer.middle_crc, data_len, m->footer.data_crc);
1883
1884 /* crc ok? */
1885 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1886 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1887 m, con->in_front_crc, m->footer.front_crc);
1888 return -EBADMSG;
1889 }
1890 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1891 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1892 m, con->in_middle_crc, m->footer.middle_crc);
1893 return -EBADMSG;
1894 }
Alex Elderbca064d2012-02-15 07:43:54 -06001895 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001896 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1897 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1898 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1899 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1900 return -EBADMSG;
1901 }
1902
1903 return 1; /* done! */
1904}
1905
1906/*
1907 * Process message. This happens in the worker thread. The callback should
1908 * be careful not to do anything that waits on other incoming messages or it
1909 * may deadlock.
1910 */
1911static void process_message(struct ceph_connection *con)
1912{
Sage Weil5e095e82009-12-14 14:30:34 -08001913 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001914
Alex Elder38941f82012-06-01 14:56:43 -05001915 BUG_ON(con->in_msg->con != con);
1916 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08001917 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001918 con->in_msg = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05001919 ceph_con_put(con);
Sage Weil31b80062009-10-06 11:31:13 -07001920
1921 /* if first message, set peer_name */
1922 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001923 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001924
Sage Weil31b80062009-10-06 11:31:13 -07001925 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001926 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001927
1928 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1929 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001930 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001931 le16_to_cpu(msg->hdr.type),
1932 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1933 le32_to_cpu(msg->hdr.front_len),
1934 le32_to_cpu(msg->hdr.data_len),
1935 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1936 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001937
1938 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001939 prepare_read_tag(con);
1940}
1941
1942
1943/*
1944 * Write something to the socket. Called in a worker thread when the
1945 * socket appears to be writeable and we have something ready to send.
1946 */
1947static int try_write(struct ceph_connection *con)
1948{
Sage Weil31b80062009-10-06 11:31:13 -07001949 int ret = 1;
1950
1951 dout("try_write start %p state %lu nref %d\n", con, con->state,
1952 atomic_read(&con->nref));
1953
Sage Weil31b80062009-10-06 11:31:13 -07001954more:
1955 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1956
1957 /* open the socket first? */
1958 if (con->sock == NULL) {
Alex Eldera5988c42012-05-29 11:04:58 -05001959 clear_bit(NEGOTIATING, &con->state);
1960 set_bit(CONNECTING, &con->state);
1961
Alex Eldere2200422012-05-23 14:35:23 -05001962 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001963 prepare_write_banner(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001964 ret = prepare_write_connect(con);
1965 if (ret < 0)
1966 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08001967 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001968
Sage Weilcf3e5c42009-12-11 09:48:05 -08001969 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001970 con->in_tag = CEPH_MSGR_TAG_READY;
1971 dout("try_write initiating connect on %p new state %lu\n",
1972 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06001973 ret = ceph_tcp_connect(con);
1974 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07001975 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07001976 goto out;
1977 }
1978 }
1979
1980more_kvec:
1981 /* kvec data queued? */
1982 if (con->out_skip) {
1983 ret = write_partial_skip(con);
1984 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001985 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001986 }
1987 if (con->out_kvec_left) {
1988 ret = write_partial_kvec(con);
1989 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001990 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001991 }
1992
1993 /* msg pages? */
1994 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001995 if (con->out_msg_done) {
1996 ceph_msg_put(con->out_msg);
1997 con->out_msg = NULL; /* we're done with this one */
1998 goto do_next;
1999 }
2000
Sage Weil31b80062009-10-06 11:31:13 -07002001 ret = write_partial_msg_pages(con);
2002 if (ret == 1)
2003 goto more_kvec; /* we need to send the footer, too! */
2004 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002005 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002006 if (ret < 0) {
2007 dout("try_write write_partial_msg_pages err %d\n",
2008 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002009 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002010 }
2011 }
2012
Sage Weilc86a2932009-12-14 14:04:30 -08002013do_next:
Sage Weil31b80062009-10-06 11:31:13 -07002014 if (!test_bit(CONNECTING, &con->state)) {
2015 /* is anything else pending? */
2016 if (!list_empty(&con->out_queue)) {
2017 prepare_write_message(con);
2018 goto more;
2019 }
2020 if (con->in_seq > con->in_seq_acked) {
2021 prepare_write_ack(con);
2022 goto more;
2023 }
Alex Elder928443c2012-05-22 11:41:43 -05002024 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002025 prepare_write_keepalive(con);
2026 goto more;
2027 }
2028 }
2029
2030 /* Nothing to do! */
Alex Elder928443c2012-05-22 11:41:43 -05002031 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002032 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002033 ret = 0;
2034out:
Sage Weil42961d22011-01-25 08:19:34 -08002035 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002036 return ret;
2037}
2038
2039
2040
2041/*
2042 * Read what we can from the socket.
2043 */
2044static int try_read(struct ceph_connection *con)
2045{
Sage Weil31b80062009-10-06 11:31:13 -07002046 int ret = -1;
2047
2048 if (!con->sock)
2049 return 0;
2050
2051 if (test_bit(STANDBY, &con->state))
2052 return 0;
2053
2054 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08002055
Sage Weil31b80062009-10-06 11:31:13 -07002056more:
2057 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2058 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002059
2060 /*
2061 * process_connect and process_message drop and re-take
2062 * con->mutex. make sure we handle a racing close or reopen.
2063 */
2064 if (test_bit(CLOSED, &con->state) ||
2065 test_bit(OPENING, &con->state)) {
2066 ret = -EAGAIN;
2067 goto out;
2068 }
2069
Sage Weil31b80062009-10-06 11:31:13 -07002070 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08002071 if (!test_bit(NEGOTIATING, &con->state)) {
2072 dout("try_read connecting\n");
2073 ret = read_partial_banner(con);
2074 if (ret <= 0)
Sage Weileed0ef22009-11-10 14:34:36 -08002075 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002076 ret = process_banner(con);
2077 if (ret < 0)
2078 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08002079 }
Sage Weil31b80062009-10-06 11:31:13 -07002080 ret = read_partial_connect(con);
2081 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002082 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002083 ret = process_connect(con);
2084 if (ret < 0)
2085 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002086 goto more;
2087 }
2088
2089 if (con->in_base_pos < 0) {
2090 /*
2091 * skipping + discarding content.
2092 *
2093 * FIXME: there must be a better way to do this!
2094 */
Alex Elder84495f42012-02-15 07:43:55 -06002095 static char buf[SKIP_BUF_SIZE];
2096 int skip = min((int) sizeof (buf), -con->in_base_pos);
2097
Sage Weil31b80062009-10-06 11:31:13 -07002098 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2099 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2100 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002101 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002102 con->in_base_pos += ret;
2103 if (con->in_base_pos)
2104 goto more;
2105 }
2106 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2107 /*
2108 * what's next?
2109 */
2110 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2111 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002112 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002113 dout("try_read got tag %d\n", (int)con->in_tag);
2114 switch (con->in_tag) {
2115 case CEPH_MSGR_TAG_MSG:
2116 prepare_read_message(con);
2117 break;
2118 case CEPH_MSGR_TAG_ACK:
2119 prepare_read_ack(con);
2120 break;
2121 case CEPH_MSGR_TAG_CLOSE:
2122 set_bit(CLOSED, &con->state); /* fixme */
Sage Weil98bdb0a2011-01-25 08:17:48 -08002123 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002124 default:
2125 goto bad_tag;
2126 }
2127 }
2128 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2129 ret = read_partial_message(con);
2130 if (ret <= 0) {
2131 switch (ret) {
2132 case -EBADMSG:
2133 con->error_msg = "bad crc";
2134 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002135 break;
Sage Weil31b80062009-10-06 11:31:13 -07002136 case -EIO:
2137 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002138 break;
Sage Weil31b80062009-10-06 11:31:13 -07002139 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002140 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002141 }
2142 if (con->in_tag == CEPH_MSGR_TAG_READY)
2143 goto more;
2144 process_message(con);
2145 goto more;
2146 }
2147 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2148 ret = read_partial_ack(con);
2149 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002150 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002151 process_ack(con);
2152 goto more;
2153 }
2154
Sage Weil31b80062009-10-06 11:31:13 -07002155out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002156 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002157 return ret;
2158
2159bad_tag:
2160 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2161 con->error_msg = "protocol error, garbage tag";
2162 ret = -1;
2163 goto out;
2164}
2165
2166
2167/*
2168 * Atomically queue work on a connection. Bump @con reference to
2169 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002170 */
2171static void queue_con(struct ceph_connection *con)
2172{
Sage Weil31b80062009-10-06 11:31:13 -07002173 if (!con->ops->get(con)) {
2174 dout("queue_con %p ref count 0\n", con);
2175 return;
2176 }
2177
Tejun Heof363e452011-01-03 14:49:46 +01002178 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002179 dout("queue_con %p - already queued\n", con);
2180 con->ops->put(con);
2181 } else {
2182 dout("queue_con %p\n", con);
2183 }
2184}
2185
2186/*
2187 * Do some work on a connection. Drop a connection ref when we're done.
2188 */
2189static void con_work(struct work_struct *work)
2190{
2191 struct ceph_connection *con = container_of(work, struct ceph_connection,
2192 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002193 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002194
Sage Weil9dd46582010-04-28 13:51:50 -07002195 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002196restart:
Alex Elder928443c2012-05-22 11:41:43 -05002197 if (test_and_clear_bit(BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002198 dout("con_work %p backing off\n", con);
2199 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2200 round_jiffies_relative(con->delay))) {
2201 dout("con_work %p backoff %lu\n", con, con->delay);
2202 mutex_unlock(&con->mutex);
2203 return;
2204 } else {
2205 con->ops->put(con);
2206 dout("con_work %p FAILED to back off %lu\n", con,
2207 con->delay);
2208 }
2209 }
Sage Weil9dd46582010-04-28 13:51:50 -07002210
Sage Weile00de342011-03-04 12:25:05 -08002211 if (test_bit(STANDBY, &con->state)) {
2212 dout("con_work %p STANDBY\n", con);
2213 goto done;
2214 }
Sage Weil31b80062009-10-06 11:31:13 -07002215 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2216 dout("con_work CLOSED\n");
2217 con_close_socket(con);
2218 goto done;
2219 }
2220 if (test_and_clear_bit(OPENING, &con->state)) {
2221 /* reopen w/ new peer */
2222 dout("con_work OPENING\n");
2223 con_close_socket(con);
2224 }
2225
Alex Elder928443c2012-05-22 11:41:43 -05002226 if (test_and_clear_bit(SOCK_CLOSED, &con->flags))
Sage Weil0da5d702011-05-19 11:21:05 -07002227 goto fault;
2228
2229 ret = try_read(con);
2230 if (ret == -EAGAIN)
2231 goto restart;
2232 if (ret < 0)
2233 goto fault;
2234
2235 ret = try_write(con);
2236 if (ret == -EAGAIN)
2237 goto restart;
2238 if (ret < 0)
2239 goto fault;
Sage Weil31b80062009-10-06 11:31:13 -07002240
2241done:
Sage Weil9dd46582010-04-28 13:51:50 -07002242 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002243done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002244 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002245 return;
2246
2247fault:
2248 mutex_unlock(&con->mutex);
2249 ceph_fault(con); /* error/fault path */
2250 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002251}
2252
2253
2254/*
2255 * Generic error/fault handler. A retry mechanism is used with
2256 * exponential backoff
2257 */
2258static void ceph_fault(struct ceph_connection *con)
2259{
2260 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002261 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002262 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002263 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002264
Alex Elder928443c2012-05-22 11:41:43 -05002265 if (test_bit(LOSSYTX, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002266 dout("fault on LOSSYTX channel\n");
2267 goto out;
2268 }
2269
Sage Weilec302642009-12-22 10:43:42 -08002270 mutex_lock(&con->mutex);
Sage Weil91e45ce32010-02-15 12:05:09 -08002271 if (test_bit(CLOSED, &con->state))
2272 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002273
Sage Weil31b80062009-10-06 11:31:13 -07002274 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002275
2276 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002277 BUG_ON(con->in_msg->con != con);
2278 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08002279 ceph_msg_put(con->in_msg);
2280 con->in_msg = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05002281 ceph_con_put(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002282 }
Sage Weil31b80062009-10-06 11:31:13 -07002283
Sage Weile80a52d2010-02-25 12:40:45 -08002284 /* Requeue anything that hasn't been acked */
2285 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002286
Sage Weile76661d2011-03-03 10:10:15 -08002287 /* If there are no messages queued or keepalive pending, place
2288 * the connection in a STANDBY state */
2289 if (list_empty(&con->out_queue) &&
Alex Elder928443c2012-05-22 11:41:43 -05002290 !test_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weile00de342011-03-04 12:25:05 -08002291 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elder928443c2012-05-22 11:41:43 -05002292 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002293 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08002294 } else {
2295 /* retry after a delay. */
2296 if (con->delay == 0)
2297 con->delay = BASE_DELAY_INTERVAL;
2298 else if (con->delay < MAX_DELAY_INTERVAL)
2299 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002300 con->ops->get(con);
2301 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002302 round_jiffies_relative(con->delay))) {
2303 dout("fault queued %p delay %lu\n", con, con->delay);
2304 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002305 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002306 dout("fault failed to queue %p delay %lu, backoff\n",
2307 con, con->delay);
2308 /*
2309 * In many cases we see a socket state change
2310 * while con_work is running and end up
2311 * queuing (non-delayed) work, such that we
2312 * can't backoff with a delay. Set a flag so
2313 * that when con_work restarts we schedule the
2314 * delay then.
2315 */
Alex Elder928443c2012-05-22 11:41:43 -05002316 set_bit(BACKOFF, &con->flags);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002317 }
Sage Weil31b80062009-10-06 11:31:13 -07002318 }
2319
Sage Weil91e45ce32010-02-15 12:05:09 -08002320out_unlock:
2321 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002322out:
Sage Weil161fd652010-02-25 12:38:57 -08002323 /*
2324 * in case we faulted due to authentication, invalidate our
2325 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002326 */
Sage Weil161fd652010-02-25 12:38:57 -08002327 if (con->auth_retry && con->ops->invalidate_authorizer) {
2328 dout("calling invalidate_authorizer()\n");
2329 con->ops->invalidate_authorizer(con);
2330 }
2331
Sage Weil31b80062009-10-06 11:31:13 -07002332 if (con->ops->fault)
2333 con->ops->fault(con);
2334}
2335
2336
2337
2338/*
Alex Elder15d98822012-05-26 23:26:43 -05002339 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002340 */
Alex Elder15d98822012-05-26 23:26:43 -05002341void ceph_messenger_init(struct ceph_messenger *msgr,
2342 struct ceph_entity_addr *myaddr,
2343 u32 supported_features,
2344 u32 required_features,
2345 bool nocrc)
Sage Weil31b80062009-10-06 11:31:13 -07002346{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002347 msgr->supported_features = supported_features;
2348 msgr->required_features = required_features;
2349
Sage Weil31b80062009-10-06 11:31:13 -07002350 spin_lock_init(&msgr->global_seq_lock);
2351
Sage Weil31b80062009-10-06 11:31:13 -07002352 if (myaddr)
2353 msgr->inst.addr = *myaddr;
2354
2355 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002356 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002357 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002358 encode_my_addr(msgr);
Alex Elder15d98822012-05-26 23:26:43 -05002359 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002360
Alex Elder15d98822012-05-26 23:26:43 -05002361 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002362}
Alex Elder15d98822012-05-26 23:26:43 -05002363EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002364
Sage Weile00de342011-03-04 12:25:05 -08002365static void clear_standby(struct ceph_connection *con)
2366{
2367 /* come back from STANDBY? */
2368 if (test_and_clear_bit(STANDBY, &con->state)) {
2369 mutex_lock(&con->mutex);
2370 dout("clear_standby %p and ++connect_seq\n", con);
2371 con->connect_seq++;
Alex Elder928443c2012-05-22 11:41:43 -05002372 WARN_ON(test_bit(WRITE_PENDING, &con->flags));
2373 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags));
Sage Weile00de342011-03-04 12:25:05 -08002374 mutex_unlock(&con->mutex);
2375 }
2376}
2377
Sage Weil31b80062009-10-06 11:31:13 -07002378/*
2379 * Queue up an outgoing message on the given connection.
2380 */
2381void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2382{
2383 if (test_bit(CLOSED, &con->state)) {
2384 dout("con_send %p closed, dropping %p\n", con, msg);
2385 ceph_msg_put(msg);
2386 return;
2387 }
2388
2389 /* set src+dst */
Sage Weildbad1852010-03-25 15:45:38 -07002390 msg->hdr.src = con->msgr->inst.name;
Sage Weil31b80062009-10-06 11:31:13 -07002391
Sage Weil3ca02ef2010-03-01 15:25:00 -08002392 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2393
Sage Weile84346b2010-05-11 21:20:38 -07002394 msg->needs_out_seq = true;
2395
Sage Weil31b80062009-10-06 11:31:13 -07002396 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08002397 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002398
Alex Elder38941f82012-06-01 14:56:43 -05002399 BUG_ON(msg->con != NULL);
Alex Elder92ce0342012-06-04 14:43:33 -05002400 msg->con = ceph_con_get(con);
2401 BUG_ON(msg->con == NULL);
2402
Sage Weil31b80062009-10-06 11:31:13 -07002403 BUG_ON(!list_empty(&msg->list_head));
2404 list_add_tail(&msg->list_head, &con->out_queue);
2405 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2406 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2407 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2408 le32_to_cpu(msg->hdr.front_len),
2409 le32_to_cpu(msg->hdr.middle_len),
2410 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002411 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002412
2413 /* if there wasn't anything waiting to send before, queue
2414 * new work */
Sage Weile00de342011-03-04 12:25:05 -08002415 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002416 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002417 queue_con(con);
2418}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002419EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002420
2421/*
2422 * Revoke a message that was previously queued for send
2423 */
2424void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
2425{
Sage Weilec302642009-12-22 10:43:42 -08002426 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002427 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05002428 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002429 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -05002430 BUG_ON(msg->con == NULL);
Alex Elder92ce0342012-06-04 14:43:33 -05002431 ceph_con_put(msg->con);
Alex Elder38941f82012-06-01 14:56:43 -05002432 msg->con = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05002433 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05002434
Sage Weil31b80062009-10-06 11:31:13 -07002435 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002436 }
2437 if (con->out_msg == msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002438 dout("%s %p msg %p - was sending\n", __func__, con, msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002439 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002440 if (con->out_kvec_is_msg) {
2441 con->out_skip = con->out_kvec_bytes;
2442 con->out_kvec_is_msg = false;
2443 }
Sage Weiled98ada2010-07-05 12:15:14 -07002444 msg->hdr.seq = 0;
Alex Elder92ce0342012-06-04 14:43:33 -05002445
2446 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07002447 }
Sage Weilec302642009-12-22 10:43:42 -08002448 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002449}
2450
2451/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002452 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002453 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002454void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002455{
2456 mutex_lock(&con->mutex);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002457 if (con->in_msg && con->in_msg == msg) {
2458 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2459 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002460 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2461
2462 /* skip rest of message */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002463 dout("con_revoke_pages %p msg %p revoked\n", con, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002464 con->in_base_pos = con->in_base_pos -
2465 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002466 front_len -
2467 middle_len -
2468 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002469 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002470 ceph_msg_put(con->in_msg);
2471 con->in_msg = NULL;
2472 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002473 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002474 } else {
2475 dout("con_revoke_pages %p msg %p pages %p no-op\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002476 con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002477 }
2478 mutex_unlock(&con->mutex);
2479}
2480
2481/*
Sage Weil31b80062009-10-06 11:31:13 -07002482 * Queue a keepalive byte to ensure the tcp connection is alive.
2483 */
2484void ceph_con_keepalive(struct ceph_connection *con)
2485{
Sage Weile00de342011-03-04 12:25:05 -08002486 dout("con_keepalive %p\n", con);
2487 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002488 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2489 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002490 queue_con(con);
2491}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002492EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002493
2494
2495/*
2496 * construct a new message with given type, size
2497 * the new msg has a ref count of 1.
2498 */
Sage Weilb61c2762011-08-09 15:03:46 -07002499struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2500 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002501{
2502 struct ceph_msg *m;
2503
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002504 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002505 if (m == NULL)
2506 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002507 kref_init(&m->kref);
Alex Elder38941f82012-06-01 14:56:43 -05002508
2509 m->con = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002510 INIT_LIST_HEAD(&m->list_head);
2511
Sage Weil45c6ceb2010-05-11 15:01:51 -07002512 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002513 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002514 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2515 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002516 m->hdr.front_len = cpu_to_le32(front_len);
2517 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002518 m->hdr.data_len = 0;
2519 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002520 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002521 m->footer.front_crc = 0;
2522 m->footer.middle_crc = 0;
2523 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002524 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002525 m->front_max = front_len;
2526 m->front_is_vmalloc = false;
2527 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002528 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002529 m->pool = NULL;
2530
Henry C Changca208922011-05-03 02:29:56 +00002531 /* middle */
2532 m->middle = NULL;
2533
2534 /* data */
2535 m->nr_pages = 0;
2536 m->page_alignment = 0;
2537 m->pages = NULL;
2538 m->pagelist = NULL;
2539 m->bio = NULL;
2540 m->bio_iter = NULL;
2541 m->bio_seg = 0;
2542 m->trail = NULL;
2543
Sage Weil31b80062009-10-06 11:31:13 -07002544 /* front */
2545 if (front_len) {
2546 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002547 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002548 PAGE_KERNEL);
2549 m->front_is_vmalloc = true;
2550 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002551 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002552 }
2553 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002554 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002555 front_len);
2556 goto out2;
2557 }
2558 } else {
2559 m->front.iov_base = NULL;
2560 }
2561 m->front.iov_len = front_len;
2562
Sage Weilbb257662010-04-01 16:07:23 -07002563 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002564 return m;
2565
2566out2:
2567 ceph_msg_put(m);
2568out:
Sage Weilb61c2762011-08-09 15:03:46 -07002569 if (!can_fail) {
2570 pr_err("msg_new can't create type %d front %d\n", type,
2571 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002572 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002573 } else {
2574 dout("msg_new can't create type %d front %d\n", type,
2575 front_len);
2576 }
Sage Weila79832f2010-04-01 16:06:19 -07002577 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002578}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002579EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002580
2581/*
Sage Weil31b80062009-10-06 11:31:13 -07002582 * Allocate "middle" portion of a message, if it is needed and wasn't
2583 * allocated by alloc_msg. This allows us to read a small fixed-size
2584 * per-type header in the front and then gracefully fail (i.e.,
2585 * propagate the error to the caller based on info in the front) when
2586 * the middle is too large.
2587 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002588static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002589{
2590 int type = le16_to_cpu(msg->hdr.type);
2591 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2592
2593 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2594 ceph_msg_type_name(type), middle_len);
2595 BUG_ON(!middle_len);
2596 BUG_ON(msg->middle);
2597
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002598 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002599 if (!msg->middle)
2600 return -ENOMEM;
2601 return 0;
2602}
2603
Yehuda Sadeh24504182010-01-08 13:58:34 -08002604/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05002605 * Allocate a message for receiving an incoming message on a
2606 * connection, and save the result in con->in_msg. Uses the
2607 * connection's private alloc_msg op if available.
2608 *
2609 * Returns true if the message should be skipped, false otherwise.
2610 * If true is returned (skip message), con->in_msg will be NULL.
2611 * If false is returned, con->in_msg will contain a pointer to the
2612 * newly-allocated message, or NULL in case of memory exhaustion.
Yehuda Sadeh24504182010-01-08 13:58:34 -08002613 */
Alex Elder1c20f2d2012-06-04 14:43:32 -05002614static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2615 struct ceph_msg_header *hdr)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002616{
2617 int type = le16_to_cpu(hdr->type);
2618 int front_len = le32_to_cpu(hdr->front_len);
2619 int middle_len = le32_to_cpu(hdr->middle_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002620 int ret;
2621
Alex Elder1c20f2d2012-06-04 14:43:32 -05002622 BUG_ON(con->in_msg != NULL);
2623
Yehuda Sadeh24504182010-01-08 13:58:34 -08002624 if (con->ops->alloc_msg) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002625 int skip = 0;
2626
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002627 mutex_unlock(&con->mutex);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002628 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002629 mutex_lock(&con->mutex);
Alex Elder92ce0342012-06-04 14:43:33 -05002630 if (con->in_msg) {
2631 con->in_msg->con = ceph_con_get(con);
2632 BUG_ON(con->in_msg->con == NULL);
2633 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002634 if (skip)
2635 con->in_msg = NULL;
2636
2637 if (!con->in_msg)
2638 return skip != 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002639 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002640 if (!con->in_msg) {
2641 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2642 if (!con->in_msg) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002643 pr_err("unable to allocate msg type %d len %d\n",
2644 type, front_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002645 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002646 }
Alex Elder92ce0342012-06-04 14:43:33 -05002647 con->in_msg->con = ceph_con_get(con);
2648 BUG_ON(con->in_msg->con == NULL);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002649 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002650 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002651 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002652
Alex Elder1c20f2d2012-06-04 14:43:32 -05002653 if (middle_len && !con->in_msg->middle) {
2654 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002655 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002656 ceph_msg_put(con->in_msg);
2657 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002658 }
2659 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002660
Alex Elder1c20f2d2012-06-04 14:43:32 -05002661 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002662}
2663
Sage Weil31b80062009-10-06 11:31:13 -07002664
2665/*
2666 * Free a generically kmalloc'd message.
2667 */
2668void ceph_msg_kfree(struct ceph_msg *m)
2669{
2670 dout("msg_kfree %p\n", m);
2671 if (m->front_is_vmalloc)
2672 vfree(m->front.iov_base);
2673 else
2674 kfree(m->front.iov_base);
2675 kfree(m);
2676}
2677
2678/*
2679 * Drop a msg ref. Destroy as needed.
2680 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002681void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002682{
Sage Weilc2e552e2009-12-07 15:55:05 -08002683 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002684
Sage Weilc2e552e2009-12-07 15:55:05 -08002685 dout("ceph_msg_put last one on %p\n", m);
2686 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002687
Sage Weilc2e552e2009-12-07 15:55:05 -08002688 /* drop middle, data, if any */
2689 if (m->middle) {
2690 ceph_buffer_put(m->middle);
2691 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002692 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002693 m->nr_pages = 0;
2694 m->pages = NULL;
2695
Sage Weil58bb3b32009-12-23 12:12:31 -08002696 if (m->pagelist) {
2697 ceph_pagelist_release(m->pagelist);
2698 kfree(m->pagelist);
2699 m->pagelist = NULL;
2700 }
2701
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002702 m->trail = NULL;
2703
Sage Weilc2e552e2009-12-07 15:55:05 -08002704 if (m->pool)
2705 ceph_msgpool_put(m->pool, m);
2706 else
2707 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002708}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002709EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002710
2711void ceph_msg_dump(struct ceph_msg *msg)
2712{
2713 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2714 msg->front_max, msg->nr_pages);
2715 print_hex_dump(KERN_DEBUG, "header: ",
2716 DUMP_PREFIX_OFFSET, 16, 1,
2717 &msg->hdr, sizeof(msg->hdr), true);
2718 print_hex_dump(KERN_DEBUG, " front: ",
2719 DUMP_PREFIX_OFFSET, 16, 1,
2720 msg->front.iov_base, msg->front.iov_len, true);
2721 if (msg->middle)
2722 print_hex_dump(KERN_DEBUG, "middle: ",
2723 DUMP_PREFIX_OFFSET, 16, 1,
2724 msg->middle->vec.iov_base,
2725 msg->middle->vec.iov_len, true);
2726 print_hex_dump(KERN_DEBUG, "footer: ",
2727 DUMP_PREFIX_OFFSET, 16, 1,
2728 &msg->footer, sizeof(msg->footer), true);
2729}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002730EXPORT_SYMBOL(ceph_msg_dump);