blob: 51cf731802604b2d3cce9dd82d6bec7831764376 [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 Elder0bcd1572012-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 Elder0bcd1572012-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 Elderf5e79a42012-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 Elderf5e79a42012-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 Elderf5e79a42012-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 Elderf5e79a42012-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 Elderbc327472012-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 Elderf5e79a42012-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 Elderf5e79a42012-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 Elderf5e79a42012-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 Elderf5e79a42012-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 Elderf5e79a42012-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 Elderf5e79a42012-05-22 11:41:43 -0500262 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elder0bcd1572012-05-22 22:15:49 -0500263 con_sock_state_closing(con);
Alex Elderbc327472012-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 Elderf5e79a42012-05-22 11:41:43 -0500273 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elder0bcd1572012-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 Elderf5e79a42012-05-22 11:41:43 -0500290 sk->sk_data_ready = ceph_sock_data_ready;
291 sk->sk_write_space = ceph_sock_write_space;
292 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700293}
294
295
296/*
297 * socket helpers
298 */
299
300/*
301 * initiate connection to a remote socket.
302 */
Alex Elder41617d02012-02-14 14:05:33 -0600303static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700304{
Sage Weilf91d3472010-07-01 15:18:31 -0700305 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700306 struct socket *sock;
307 int ret;
308
309 BUG_ON(con->sock);
Sage Weilf91d3472010-07-01 15:18:31 -0700310 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
311 IPPROTO_TCP, &sock);
Sage Weil31b80062009-10-06 11:31:13 -0700312 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600313 return ret;
Sage Weil31b80062009-10-06 11:31:13 -0700314 sock->sk->sk_allocation = GFP_NOFS;
315
Sage Weila6a53492010-04-13 14:07:07 -0700316#ifdef CONFIG_LOCKDEP
317 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
318#endif
319
Sage Weil31b80062009-10-06 11:31:13 -0700320 set_sock_callbacks(sock, con);
321
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700322 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700323
Sage Weilf91d3472010-07-01 15:18:31 -0700324 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
325 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700326 if (ret == -EINPROGRESS) {
327 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700328 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700329 sock->sk->sk_state);
Alex Eldera5bc3122012-01-23 15:49:27 -0600330 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700331 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700332 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700333 sock_release(sock);
Sage Weil31b80062009-10-06 11:31:13 -0700334 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -0700335
Alex Elder41617d02012-02-14 14:05:33 -0600336 return ret;
Alex Eldera5bc3122012-01-23 15:49:27 -0600337 }
338 con->sock = sock;
Alex Elder0bcd1572012-05-22 22:15:49 -0500339 con_sock_state_connecting(con);
Alex Eldera5bc3122012-01-23 15:49:27 -0600340
Alex Elder41617d02012-02-14 14:05:33 -0600341 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700342}
343
344static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
345{
346 struct kvec iov = {buf, len};
347 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800348 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700349
Sage Weil98bdb0a2011-01-25 08:17:48 -0800350 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
351 if (r == -EAGAIN)
352 r = 0;
353 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700354}
355
356/*
357 * write something. @more is true if caller will be sending more data
358 * shortly.
359 */
360static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
361 size_t kvlen, size_t len, int more)
362{
363 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800364 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700365
366 if (more)
367 msg.msg_flags |= MSG_MORE;
368 else
369 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
370
Sage Weil42961d22011-01-25 08:19:34 -0800371 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
372 if (r == -EAGAIN)
373 r = 0;
374 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700375}
376
Alex Elder31739132012-03-07 11:40:08 -0600377static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
378 int offset, size_t size, int more)
379{
380 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
381 int ret;
382
383 ret = kernel_sendpage(sock, page, offset, size, flags);
384 if (ret == -EAGAIN)
385 ret = 0;
386
387 return ret;
388}
389
Sage Weil31b80062009-10-06 11:31:13 -0700390
391/*
392 * Shutdown/close the socket for the given connection.
393 */
394static int con_close_socket(struct ceph_connection *con)
395{
396 int rc;
397
398 dout("con_close_socket on %p sock %p\n", con, con->sock);
399 if (!con->sock)
400 return 0;
401 set_bit(SOCK_CLOSED, &con->state);
402 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
403 sock_release(con->sock);
404 con->sock = NULL;
405 clear_bit(SOCK_CLOSED, &con->state);
Alex Elder0bcd1572012-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 Eldere84e0662012-06-01 14:56:43 -0500417 BUG_ON(msg->con == NULL);
418 msg->con = NULL;
419
Sage Weil31b80062009-10-06 11:31:13 -0700420 ceph_msg_put(msg);
421}
422static void ceph_msg_remove_list(struct list_head *head)
423{
424 while (!list_empty(head)) {
425 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
426 list_head);
427 ceph_msg_remove(msg);
428 }
429}
430
431static void reset_connection(struct ceph_connection *con)
432{
433 /* reset connection, out_queue, msg_ and connect_seq */
434 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700435 ceph_msg_remove_list(&con->out_queue);
436 ceph_msg_remove_list(&con->out_sent);
437
Sage Weilcf3e5c42009-12-11 09:48:05 -0800438 if (con->in_msg) {
Alex Eldere84e0662012-06-01 14:56:43 -0500439 BUG_ON(con->in_msg->con != con);
440 con->in_msg->con = NULL;
Sage Weilcf3e5c42009-12-11 09:48:05 -0800441 ceph_msg_put(con->in_msg);
442 con->in_msg = NULL;
443 }
444
Sage Weil31b80062009-10-06 11:31:13 -0700445 con->connect_seq = 0;
446 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800447 if (con->out_msg) {
448 ceph_msg_put(con->out_msg);
449 con->out_msg = NULL;
450 }
Sage Weil31b80062009-10-06 11:31:13 -0700451 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700452 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700453}
454
455/*
456 * mark a peer down. drop any open connections.
457 */
458void ceph_con_close(struct ceph_connection *con)
459{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700460 dout("con_close %p peer %s\n", con,
461 ceph_pr_addr(&con->peer_addr.in_addr));
Alex Elder51588ed2012-05-29 11:04:58 -0500462 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700463 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Alex Elder51588ed2012-05-29 11:04:58 -0500464 set_bit(CLOSED, &con->state);
465
Alex Elderbc327472012-05-22 11:41:43 -0500466 clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */
467 clear_bit(KEEPALIVE_PENDING, &con->flags);
468 clear_bit(WRITE_PENDING, &con->flags);
Alex Elder51588ed2012-05-29 11:04:58 -0500469
Sage Weilec302642009-12-22 10:43:42 -0800470 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700471 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700472 con->peer_global_seq = 0;
Sage Weil91e45ce2010-02-15 12:05:09 -0800473 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800474 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700475 queue_con(con);
476}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700477EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700478
479/*
Sage Weil31b80062009-10-06 11:31:13 -0700480 * Reopen a closed connection, with a new peer address.
481 */
482void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
483{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700484 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700485 set_bit(OPENING, &con->state);
Alex Elder51588ed2012-05-29 11:04:58 -0500486 WARN_ON(!test_and_clear_bit(CLOSED, &con->state));
487
Sage Weil31b80062009-10-06 11:31:13 -0700488 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800489 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700490 queue_con(con);
491}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700492EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700493
494/*
Sage Weil87b315a2010-03-22 14:51:18 -0700495 * return true if this connection ever successfully opened
496 */
497bool ceph_con_opened(struct ceph_connection *con)
498{
499 return con->connect_seq > 0;
500}
501
502/*
Sage Weil31b80062009-10-06 11:31:13 -0700503 * generic get/put
504 */
505struct ceph_connection *ceph_con_get(struct ceph_connection *con)
506{
Alex Elderd3002b92012-02-14 14:05:33 -0600507 int nref = __atomic_add_unless(&con->nref, 1, 0);
508
509 dout("con_get %p nref = %d -> %d\n", con, nref, nref + 1);
510
511 return nref ? con : NULL;
Sage Weil31b80062009-10-06 11:31:13 -0700512}
513
514void ceph_con_put(struct ceph_connection *con)
515{
Alex Elderd3002b92012-02-14 14:05:33 -0600516 int nref = atomic_dec_return(&con->nref);
517
518 BUG_ON(nref < 0);
519 if (nref == 0) {
Sage Weil71ececd2009-11-18 11:27:06 -0800520 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -0700521 kfree(con);
522 }
Alex Elderd3002b92012-02-14 14:05:33 -0600523 dout("con_put %p nref = %d -> %d\n", con, nref + 1, nref);
Sage Weil31b80062009-10-06 11:31:13 -0700524}
525
526/*
527 * initialize a new connection.
528 */
Alex Elder68801382012-05-26 23:26:43 -0500529void ceph_con_init(struct ceph_connection *con, void *private,
530 const struct ceph_connection_operations *ops,
531 struct ceph_messenger *msgr, __u8 entity_type, __u64 entity_num)
Sage Weil31b80062009-10-06 11:31:13 -0700532{
533 dout("con_init %p\n", con);
534 memset(con, 0, sizeof(*con));
Alex Elder68801382012-05-26 23:26:43 -0500535 con->private = private;
536 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700537 atomic_set(&con->nref, 1);
538 con->msgr = msgr;
Alex Elder0bcd1572012-05-22 22:15:49 -0500539
540 con_sock_state_init(con);
541
Alex Elder68801382012-05-26 23:26:43 -0500542 con->peer_name.type = (__u8) entity_type;
543 con->peer_name.num = cpu_to_le64(entity_num);
544
Sage Weilec302642009-12-22 10:43:42 -0800545 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700546 INIT_LIST_HEAD(&con->out_queue);
547 INIT_LIST_HEAD(&con->out_sent);
548 INIT_DELAYED_WORK(&con->work, con_work);
Alex Elder51588ed2012-05-29 11:04:58 -0500549
550 set_bit(CLOSED, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -0700551}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700552EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700553
554
555/*
556 * We maintain a global counter to order connection attempts. Get
557 * a unique seq greater than @gt.
558 */
559static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
560{
561 u32 ret;
562
563 spin_lock(&msgr->global_seq_lock);
564 if (msgr->global_seq < gt)
565 msgr->global_seq = gt;
566 ret = ++msgr->global_seq;
567 spin_unlock(&msgr->global_seq_lock);
568 return ret;
569}
570
Alex Elder4874ba92012-05-23 14:35:23 -0500571static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600572{
573 con->out_kvec_left = 0;
574 con->out_kvec_bytes = 0;
575 con->out_kvec_cur = &con->out_kvec[0];
576}
577
Alex Elder4874ba92012-05-23 14:35:23 -0500578static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600579 size_t size, void *data)
580{
581 int index;
582
583 index = con->out_kvec_left;
584 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
585
586 con->out_kvec[index].iov_len = size;
587 con->out_kvec[index].iov_base = data;
588 con->out_kvec_left++;
589 con->out_kvec_bytes += size;
590}
Sage Weil31b80062009-10-06 11:31:13 -0700591
592/*
593 * Prepare footer for currently outgoing message, and finish things
594 * off. Assumes out_kvec* are already valid.. we just add on to the end.
595 */
Alex Elder859eb792012-02-14 14:05:33 -0600596static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700597{
598 struct ceph_msg *m = con->out_msg;
Alex Elder859eb792012-02-14 14:05:33 -0600599 int v = con->out_kvec_left;
Sage Weil31b80062009-10-06 11:31:13 -0700600
601 dout("prepare_write_message_footer %p\n", con);
602 con->out_kvec_is_msg = true;
603 con->out_kvec[v].iov_base = &m->footer;
604 con->out_kvec[v].iov_len = sizeof(m->footer);
605 con->out_kvec_bytes += sizeof(m->footer);
606 con->out_kvec_left++;
607 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800608 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700609}
610
611/*
612 * Prepare headers for the next outgoing message.
613 */
614static void prepare_write_message(struct ceph_connection *con)
615{
616 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -0600617 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700618
Alex Elder4874ba92012-05-23 14:35:23 -0500619 con_out_kvec_reset(con);
Sage Weil31b80062009-10-06 11:31:13 -0700620 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800621 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700622
623 /* Sneak an ack in there first? If we can get it into the same
624 * TCP packet that's a good thing. */
625 if (con->in_seq > con->in_seq_acked) {
626 con->in_seq_acked = con->in_seq;
Alex Elder4874ba92012-05-23 14:35:23 -0500627 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700628 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Elder4874ba92012-05-23 14:35:23 -0500629 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600630 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -0700631 }
632
Alex Eldere84e0662012-06-01 14:56:43 -0500633 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -0600634 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800635 con->out_msg = m;
Alex Eldere84e0662012-06-01 14:56:43 -0500636 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -0700637
638 /* put message on sent list */
639 ceph_msg_get(m);
640 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700641
Sage Weile84346b2010-05-11 21:20:38 -0700642 /*
643 * only assign outgoing seq # if we haven't sent this message
644 * yet. if it is requeued, resend with it's original seq.
645 */
646 if (m->needs_out_seq) {
647 m->hdr.seq = cpu_to_le64(++con->out_seq);
648 m->needs_out_seq = false;
649 }
Yan, Zheng576e4282012-06-06 19:35:55 -0500650#ifdef CONFIG_BLOCK
651 else
652 m->bio_iter = NULL;
653#endif
Sage Weil31b80062009-10-06 11:31:13 -0700654
655 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
656 m, con->out_seq, le16_to_cpu(m->hdr.type),
657 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
658 le32_to_cpu(m->hdr.data_len),
659 m->nr_pages);
660 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
661
662 /* tag + hdr + front + middle */
Alex Elder4874ba92012-05-23 14:35:23 -0500663 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
664 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
665 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600666
Sage Weil31b80062009-10-06 11:31:13 -0700667 if (m->middle)
Alex Elder4874ba92012-05-23 14:35:23 -0500668 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600669 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700670
671 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600672 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
673 con->out_msg->hdr.crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -0700674 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
Alex Eldera9a0c512012-02-15 07:43:54 -0600675
676 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
677 con->out_msg->footer.front_crc = cpu_to_le32(crc);
678 if (m->middle) {
679 crc = crc32c(0, m->middle->vec.iov_base,
680 m->middle->vec.iov_len);
681 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
682 } else
Sage Weil31b80062009-10-06 11:31:13 -0700683 con->out_msg->footer.middle_crc = 0;
684 con->out_msg->footer.data_crc = 0;
685 dout("prepare_write_message front_crc %u data_crc %u\n",
686 le32_to_cpu(con->out_msg->footer.front_crc),
687 le32_to_cpu(con->out_msg->footer.middle_crc));
688
689 /* is there a data payload? */
690 if (le32_to_cpu(m->hdr.data_len) > 0) {
691 /* initialize page iterator */
692 con->out_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700693 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -0800694 con->out_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700695 else
696 con->out_msg_pos.page_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700697 con->out_msg_pos.data_pos = 0;
Alex Elderbca064d2012-02-15 07:43:54 -0600698 con->out_msg_pos.did_page_crc = false;
Sage Weil31b80062009-10-06 11:31:13 -0700699 con->out_more = 1; /* data + footer will follow */
700 } else {
701 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600702 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700703 }
704
Alex Elderbc327472012-05-22 11:41:43 -0500705 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700706}
707
708/*
709 * Prepare an ack.
710 */
711static void prepare_write_ack(struct ceph_connection *con)
712{
713 dout("prepare_write_ack %p %llu -> %llu\n", con,
714 con->in_seq_acked, con->in_seq);
715 con->in_seq_acked = con->in_seq;
716
Alex Elder4874ba92012-05-23 14:35:23 -0500717 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600718
Alex Elder4874ba92012-05-23 14:35:23 -0500719 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600720
Sage Weil31b80062009-10-06 11:31:13 -0700721 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Elder4874ba92012-05-23 14:35:23 -0500722 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600723 &con->out_temp_ack);
724
Sage Weil31b80062009-10-06 11:31:13 -0700725 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elderbc327472012-05-22 11:41:43 -0500726 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700727}
728
729/*
730 * Prepare to write keepalive byte.
731 */
732static void prepare_write_keepalive(struct ceph_connection *con)
733{
734 dout("prepare_write_keepalive %p\n", con);
Alex Elder4874ba92012-05-23 14:35:23 -0500735 con_out_kvec_reset(con);
736 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Alex Elderbc327472012-05-22 11:41:43 -0500737 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700738}
739
740/*
741 * Connection negotiation.
742 */
743
Alex Elder3f134472012-05-16 15:16:39 -0500744static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
745 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800746{
Alex Elder4f33c7e2012-05-16 15:16:39 -0500747 struct ceph_auth_handshake *auth;
Alex Elder29e1a952012-05-16 15:16:38 -0500748
749 if (!con->ops->get_authorizer) {
750 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
751 con->out_connect.authorizer_len = 0;
752
Alex Elder8d190552012-05-16 15:16:39 -0500753 return NULL;
Alex Elder29e1a952012-05-16 15:16:38 -0500754 }
755
756 /* Can't hold the mutex while getting authorizer */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800757
Sage Weilec302642009-12-22 10:43:42 -0800758 mutex_unlock(&con->mutex);
Alex Elder29e1a952012-05-16 15:16:38 -0500759
Alex Elder3f134472012-05-16 15:16:39 -0500760 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Eldered35fbc2012-05-16 15:16:39 -0500761
Sage Weilec302642009-12-22 10:43:42 -0800762 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800763
Alex Elder4f33c7e2012-05-16 15:16:39 -0500764 if (IS_ERR(auth))
Alex Elder8d190552012-05-16 15:16:39 -0500765 return auth;
Alex Elderbc327472012-05-22 11:41:43 -0500766 if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags))
Alex Elder8d190552012-05-16 15:16:39 -0500767 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700768
Alex Eldered35fbc2012-05-16 15:16:39 -0500769 con->auth_reply_buf = auth->authorizer_reply_buf;
770 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
771
Alex Elder859eb792012-02-14 14:05:33 -0600772
Alex Elder8d190552012-05-16 15:16:39 -0500773 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800774}
775
Sage Weil31b80062009-10-06 11:31:13 -0700776/*
777 * We connected to a peer and are saying hello.
778 */
Alex Elder7dd07ab2012-05-16 15:16:38 -0500779static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700780{
Alex Elder4874ba92012-05-23 14:35:23 -0500781 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
782 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Elder7dd07ab2012-05-16 15:16:38 -0500783 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800784
Sage Weileed0ef22009-11-10 14:34:36 -0800785 con->out_more = 0;
Alex Elderbc327472012-05-22 11:41:43 -0500786 set_bit(WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800787}
788
Alex Elder7dd07ab2012-05-16 15:16:38 -0500789static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800790{
Sage Weil31b80062009-10-06 11:31:13 -0700791 unsigned global_seq = get_global_seq(con->msgr, 0);
792 int proto;
Alex Elder3f134472012-05-16 15:16:39 -0500793 int auth_proto;
Alex Elder8d190552012-05-16 15:16:39 -0500794 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700795
796 switch (con->peer_name.type) {
797 case CEPH_ENTITY_TYPE_MON:
798 proto = CEPH_MONC_PROTOCOL;
799 break;
800 case CEPH_ENTITY_TYPE_OSD:
801 proto = CEPH_OSDC_PROTOCOL;
802 break;
803 case CEPH_ENTITY_TYPE_MDS:
804 proto = CEPH_MDSC_PROTOCOL;
805 break;
806 default:
807 BUG();
808 }
809
810 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
811 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800812
Alex Elder7dd07ab2012-05-16 15:16:38 -0500813 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700814 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
815 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
816 con->out_connect.global_seq = cpu_to_le32(global_seq);
817 con->out_connect.protocol_version = cpu_to_le32(proto);
818 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700819
Alex Elder3f134472012-05-16 15:16:39 -0500820 auth_proto = CEPH_AUTH_UNKNOWN;
821 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder8d190552012-05-16 15:16:39 -0500822 if (IS_ERR(auth))
823 return PTR_ERR(auth);
Alex Elder1d3df0e2012-05-16 15:16:39 -0500824
Alex Elder3f134472012-05-16 15:16:39 -0500825 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder1d3df0e2012-05-16 15:16:39 -0500826 con->out_connect.authorizer_len = auth ?
827 cpu_to_le32(auth->authorizer_buf_len) : 0;
828
Alex Elder4874ba92012-05-23 14:35:23 -0500829 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder1d3df0e2012-05-16 15:16:39 -0500830 &con->out_connect);
831 if (auth && auth->authorizer_buf_len)
Alex Elder4874ba92012-05-23 14:35:23 -0500832 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder1d3df0e2012-05-16 15:16:39 -0500833 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600834
Sage Weil31b80062009-10-06 11:31:13 -0700835 con->out_more = 0;
Alex Elderbc327472012-05-22 11:41:43 -0500836 set_bit(WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800837
Alex Elder59a521e2012-05-16 15:16:38 -0500838 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700839}
840
Sage Weil31b80062009-10-06 11:31:13 -0700841/*
842 * write as much of pending kvecs to the socket as we can.
843 * 1 -> done
844 * 0 -> socket full, but more to do
845 * <0 -> error
846 */
847static int write_partial_kvec(struct ceph_connection *con)
848{
849 int ret;
850
851 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
852 while (con->out_kvec_bytes > 0) {
853 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
854 con->out_kvec_left, con->out_kvec_bytes,
855 con->out_more);
856 if (ret <= 0)
857 goto out;
858 con->out_kvec_bytes -= ret;
859 if (con->out_kvec_bytes == 0)
860 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600861
862 /* account for full iov entries consumed */
863 while (ret >= con->out_kvec_cur->iov_len) {
864 BUG_ON(!con->out_kvec_left);
865 ret -= con->out_kvec_cur->iov_len;
866 con->out_kvec_cur++;
867 con->out_kvec_left--;
868 }
869 /* and for a partially-consumed entry */
870 if (ret) {
871 con->out_kvec_cur->iov_len -= ret;
872 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700873 }
874 }
875 con->out_kvec_left = 0;
876 con->out_kvec_is_msg = false;
877 ret = 1;
878out:
879 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
880 con->out_kvec_bytes, con->out_kvec_left, ret);
881 return ret; /* done! */
882}
883
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700884#ifdef CONFIG_BLOCK
885static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
886{
887 if (!bio) {
888 *iter = NULL;
889 *seg = 0;
890 return;
891 }
892 *iter = bio;
893 *seg = bio->bi_idx;
894}
895
896static void iter_bio_next(struct bio **bio_iter, int *seg)
897{
898 if (*bio_iter == NULL)
899 return;
900
901 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
902
903 (*seg)++;
904 if (*seg == (*bio_iter)->bi_vcnt)
905 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
906}
907#endif
908
Sage Weil31b80062009-10-06 11:31:13 -0700909/*
910 * Write as much message data payload as we can. If we finish, queue
911 * up the footer.
912 * 1 -> done, footer is now queued in out_kvec[].
913 * 0 -> socket full, but more to do
914 * <0 -> error
915 */
916static int write_partial_msg_pages(struct ceph_connection *con)
917{
918 struct ceph_msg *msg = con->out_msg;
919 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
920 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600921 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700922 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700923 int total_max_write;
924 int in_trail = 0;
925 size_t trail_len = (msg->trail ? msg->trail->length : 0);
Sage Weil31b80062009-10-06 11:31:13 -0700926
927 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
928 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
929 con->out_msg_pos.page_pos);
930
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700931#ifdef CONFIG_BLOCK
932 if (msg->bio && !msg->bio_iter)
933 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
934#endif
935
936 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -0700937 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700938 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -0600939 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700940
941 total_max_write = data_len - trail_len -
942 con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -0700943
944 /*
945 * if we are calculating the data crc (the default), we need
946 * to map the page. if our pages[] has been revoked, use the
947 * zero page.
948 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700949
950 /* have we reached the trail part of the data? */
951 if (con->out_msg_pos.data_pos >= data_len - trail_len) {
952 in_trail = 1;
953
954 total_max_write = data_len - con->out_msg_pos.data_pos;
955
956 page = list_first_entry(&msg->trail->head,
957 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700958 max_write = PAGE_SIZE;
959 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -0700960 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -0800961 } else if (msg->pagelist) {
962 page = list_first_entry(&msg->pagelist->head,
963 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700964#ifdef CONFIG_BLOCK
965 } else if (msg->bio) {
966 struct bio_vec *bv;
967
968 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
969 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -0600970 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700971 max_write = bv->bv_len;
972#endif
Sage Weil31b80062009-10-06 11:31:13 -0700973 } else {
Alex Elder57666512012-01-23 15:49:27 -0600974 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -0700975 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700976 len = min_t(int, max_write - con->out_msg_pos.page_pos,
977 total_max_write);
978
Alex Elder37675b02012-03-07 11:40:08 -0600979 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -0600980 void *base;
Alex Eldera9a0c512012-02-15 07:43:54 -0600981 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700982 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -0600983 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -0700984
Alex Elder8d63e312012-03-07 11:40:08 -0600985 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -0700986 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -0600987 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Eldera9a0c512012-02-15 07:43:54 -0600988 crc = crc32c(tmpcrc, base, len);
989 con->out_msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -0600990 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -0700991 }
Alex Eldere36b13c2012-03-07 11:40:08 -0600992 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -0600993 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -0600994 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -0700995
Alex Elder0cdf9e62012-03-07 11:40:08 -0600996 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -0700997 kunmap(page);
998
999 if (ret <= 0)
1000 goto out;
1001
1002 con->out_msg_pos.data_pos += ret;
1003 con->out_msg_pos.page_pos += ret;
1004 if (ret == len) {
1005 con->out_msg_pos.page_pos = 0;
1006 con->out_msg_pos.page++;
Alex Elderbca064d2012-02-15 07:43:54 -06001007 con->out_msg_pos.did_page_crc = false;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001008 if (in_trail)
1009 list_move_tail(&page->lru,
1010 &msg->trail->head);
1011 else if (msg->pagelist)
Sage Weil58bb3b32009-12-23 12:12:31 -08001012 list_move_tail(&page->lru,
1013 &msg->pagelist->head);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001014#ifdef CONFIG_BLOCK
1015 else if (msg->bio)
1016 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
1017#endif
Sage Weil31b80062009-10-06 11:31:13 -07001018 }
1019 }
1020
1021 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1022
1023 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001024 if (!do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -07001025 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Elder4874ba92012-05-23 14:35:23 -05001026 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001027 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001028 ret = 1;
1029out:
1030 return ret;
1031}
1032
1033/*
1034 * write some zeros
1035 */
1036static int write_partial_skip(struct ceph_connection *con)
1037{
1038 int ret;
1039
1040 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001041 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001042
Alex Elder31739132012-03-07 11:40:08 -06001043 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001044 if (ret <= 0)
1045 goto out;
1046 con->out_skip -= ret;
1047 }
1048 ret = 1;
1049out:
1050 return ret;
1051}
1052
1053/*
1054 * Prepare to read connection handshake, or an ack.
1055 */
Sage Weileed0ef22009-11-10 14:34:36 -08001056static void prepare_read_banner(struct ceph_connection *con)
1057{
1058 dout("prepare_read_banner %p\n", con);
1059 con->in_base_pos = 0;
1060}
1061
Sage Weil31b80062009-10-06 11:31:13 -07001062static void prepare_read_connect(struct ceph_connection *con)
1063{
1064 dout("prepare_read_connect %p\n", con);
1065 con->in_base_pos = 0;
1066}
1067
1068static void prepare_read_ack(struct ceph_connection *con)
1069{
1070 dout("prepare_read_ack %p\n", con);
1071 con->in_base_pos = 0;
1072}
1073
1074static void prepare_read_tag(struct ceph_connection *con)
1075{
1076 dout("prepare_read_tag %p\n", con);
1077 con->in_base_pos = 0;
1078 con->in_tag = CEPH_MSGR_TAG_READY;
1079}
1080
1081/*
1082 * Prepare to read a message.
1083 */
1084static int prepare_read_message(struct ceph_connection *con)
1085{
1086 dout("prepare_read_message %p\n", con);
1087 BUG_ON(con->in_msg != NULL);
1088 con->in_base_pos = 0;
1089 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1090 return 0;
1091}
1092
1093
1094static int read_partial(struct ceph_connection *con,
Alex Elder1f7631f2012-05-10 10:29:50 -05001095 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001096{
Alex Elderdd22ce52012-05-10 10:29:50 -05001097 while (con->in_base_pos < end) {
1098 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001099 int have = size - left;
1100 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1101 if (ret <= 0)
1102 return ret;
1103 con->in_base_pos += ret;
1104 }
1105 return 1;
1106}
1107
1108
1109/*
1110 * Read all or part of the connect-side handshake on a new connection
1111 */
Sage Weileed0ef22009-11-10 14:34:36 -08001112static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001113{
Alex Elder1f7631f2012-05-10 10:29:50 -05001114 int size;
1115 int end;
1116 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001117
Sage Weileed0ef22009-11-10 14:34:36 -08001118 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001119
1120 /* peer's banner */
Alex Elder1f7631f2012-05-10 10:29:50 -05001121 size = strlen(CEPH_BANNER);
1122 end = size;
1123 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001124 if (ret <= 0)
1125 goto out;
Alex Elder1f7631f2012-05-10 10:29:50 -05001126
1127 size = sizeof (con->actual_peer_addr);
1128 end += size;
1129 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001130 if (ret <= 0)
1131 goto out;
Alex Elder1f7631f2012-05-10 10:29:50 -05001132
1133 size = sizeof (con->peer_addr_for_me);
1134 end += size;
1135 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001136 if (ret <= 0)
1137 goto out;
Alex Elder1f7631f2012-05-10 10:29:50 -05001138
Sage Weileed0ef22009-11-10 14:34:36 -08001139out:
1140 return ret;
1141}
1142
1143static int read_partial_connect(struct ceph_connection *con)
1144{
Alex Elder1f7631f2012-05-10 10:29:50 -05001145 int size;
1146 int end;
1147 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001148
1149 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1150
Alex Elder1f7631f2012-05-10 10:29:50 -05001151 size = sizeof (con->in_reply);
1152 end = size;
1153 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001154 if (ret <= 0)
1155 goto out;
Alex Elder1f7631f2012-05-10 10:29:50 -05001156
1157 size = le32_to_cpu(con->in_reply.authorizer_len);
1158 end += size;
1159 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001160 if (ret <= 0)
1161 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001162
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001163 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1164 con, (int)con->in_reply.tag,
1165 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001166 le32_to_cpu(con->in_reply.global_seq));
1167out:
1168 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001169
Sage Weil31b80062009-10-06 11:31:13 -07001170}
1171
1172/*
1173 * Verify the hello banner looks okay.
1174 */
1175static int verify_hello(struct ceph_connection *con)
1176{
1177 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001178 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001179 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001180 con->error_msg = "protocol error, bad banner";
1181 return -1;
1182 }
1183 return 0;
1184}
1185
1186static bool addr_is_blank(struct sockaddr_storage *ss)
1187{
1188 switch (ss->ss_family) {
1189 case AF_INET:
1190 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1191 case AF_INET6:
1192 return
1193 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1194 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1195 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1196 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1197 }
1198 return false;
1199}
1200
1201static int addr_port(struct sockaddr_storage *ss)
1202{
1203 switch (ss->ss_family) {
1204 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001205 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001206 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001207 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001208 }
1209 return 0;
1210}
1211
1212static void addr_set_port(struct sockaddr_storage *ss, int p)
1213{
1214 switch (ss->ss_family) {
1215 case AF_INET:
1216 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001217 break;
Sage Weil31b80062009-10-06 11:31:13 -07001218 case AF_INET6:
1219 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001220 break;
Sage Weil31b80062009-10-06 11:31:13 -07001221 }
1222}
1223
1224/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001225 * Unlike other *_pton function semantics, zero indicates success.
1226 */
1227static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1228 char delim, const char **ipend)
1229{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001230 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1231 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001232
1233 memset(ss, 0, sizeof(*ss));
1234
1235 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1236 ss->ss_family = AF_INET;
1237 return 0;
1238 }
1239
1240 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1241 ss->ss_family = AF_INET6;
1242 return 0;
1243 }
1244
1245 return -EINVAL;
1246}
1247
1248/*
1249 * Extract hostname string and resolve using kernel DNS facility.
1250 */
1251#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1252static int ceph_dns_resolve_name(const char *name, size_t namelen,
1253 struct sockaddr_storage *ss, char delim, const char **ipend)
1254{
1255 const char *end, *delim_p;
1256 char *colon_p, *ip_addr = NULL;
1257 int ip_len, ret;
1258
1259 /*
1260 * The end of the hostname occurs immediately preceding the delimiter or
1261 * the port marker (':') where the delimiter takes precedence.
1262 */
1263 delim_p = memchr(name, delim, namelen);
1264 colon_p = memchr(name, ':', namelen);
1265
1266 if (delim_p && colon_p)
1267 end = delim_p < colon_p ? delim_p : colon_p;
1268 else if (!delim_p && colon_p)
1269 end = colon_p;
1270 else {
1271 end = delim_p;
1272 if (!end) /* case: hostname:/ */
1273 end = name + namelen;
1274 }
1275
1276 if (end <= name)
1277 return -EINVAL;
1278
1279 /* do dns_resolve upcall */
1280 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1281 if (ip_len > 0)
1282 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1283 else
1284 ret = -ESRCH;
1285
1286 kfree(ip_addr);
1287
1288 *ipend = end;
1289
1290 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1291 ret, ret ? "failed" : ceph_pr_addr(ss));
1292
1293 return ret;
1294}
1295#else
1296static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1297 struct sockaddr_storage *ss, char delim, const char **ipend)
1298{
1299 return -EINVAL;
1300}
1301#endif
1302
1303/*
1304 * Parse a server name (IP or hostname). If a valid IP address is not found
1305 * then try to extract a hostname to resolve using userspace DNS upcall.
1306 */
1307static int ceph_parse_server_name(const char *name, size_t namelen,
1308 struct sockaddr_storage *ss, char delim, const char **ipend)
1309{
1310 int ret;
1311
1312 ret = ceph_pton(name, namelen, ss, delim, ipend);
1313 if (ret)
1314 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1315
1316 return ret;
1317}
1318
1319/*
Sage Weil31b80062009-10-06 11:31:13 -07001320 * Parse an ip[:port] list into an addr array. Use the default
1321 * monitor port if a port isn't specified.
1322 */
1323int ceph_parse_ips(const char *c, const char *end,
1324 struct ceph_entity_addr *addr,
1325 int max_count, int *count)
1326{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001327 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001328 const char *p = c;
1329
1330 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1331 for (i = 0; i < max_count; i++) {
1332 const char *ipend;
1333 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001334 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001335 char delim = ',';
1336
1337 if (*p == '[') {
1338 delim = ']';
1339 p++;
1340 }
Sage Weil31b80062009-10-06 11:31:13 -07001341
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001342 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1343 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001344 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001345 ret = -EINVAL;
1346
Sage Weil31b80062009-10-06 11:31:13 -07001347 p = ipend;
1348
Sage Weil39139f62010-07-08 09:54:52 -07001349 if (delim == ']') {
1350 if (*p != ']') {
1351 dout("missing matching ']'\n");
1352 goto bad;
1353 }
1354 p++;
1355 }
1356
Sage Weil31b80062009-10-06 11:31:13 -07001357 /* port? */
1358 if (p < end && *p == ':') {
1359 port = 0;
1360 p++;
1361 while (p < end && *p >= '0' && *p <= '9') {
1362 port = (port * 10) + (*p - '0');
1363 p++;
1364 }
1365 if (port > 65535 || port == 0)
1366 goto bad;
1367 } else {
1368 port = CEPH_MON_PORT;
1369 }
1370
1371 addr_set_port(ss, port);
1372
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001373 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001374
1375 if (p == end)
1376 break;
1377 if (*p != ',')
1378 goto bad;
1379 p++;
1380 }
1381
1382 if (p != end)
1383 goto bad;
1384
1385 if (count)
1386 *count = i + 1;
1387 return 0;
1388
1389bad:
Sage Weil39139f62010-07-08 09:54:52 -07001390 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001391 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001392}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001393EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001394
Sage Weileed0ef22009-11-10 14:34:36 -08001395static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001396{
Sage Weileed0ef22009-11-10 14:34:36 -08001397 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001398
1399 if (verify_hello(con) < 0)
1400 return -1;
1401
Sage Weil63f2d212009-11-03 15:17:56 -08001402 ceph_decode_addr(&con->actual_peer_addr);
1403 ceph_decode_addr(&con->peer_addr_for_me);
1404
Sage Weil31b80062009-10-06 11:31:13 -07001405 /*
1406 * Make sure the other end is who we wanted. note that the other
1407 * end may not yet know their ip address, so if it's 0.0.0.0, give
1408 * them the benefit of the doubt.
1409 */
Sage Weil103e2d32010-01-07 16:12:36 -08001410 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1411 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001412 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1413 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001414 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001415 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001416 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001417 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001418 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001419 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001420 return -1;
1421 }
1422
1423 /*
1424 * did we learn our address?
1425 */
1426 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1427 int port = addr_port(&con->msgr->inst.addr.in_addr);
1428
1429 memcpy(&con->msgr->inst.addr.in_addr,
1430 &con->peer_addr_for_me.in_addr,
1431 sizeof(con->peer_addr_for_me.in_addr));
1432 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001433 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001434 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001435 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001436 }
1437
Sage Weileed0ef22009-11-10 14:34:36 -08001438 set_bit(NEGOTIATING, &con->state);
1439 prepare_read_connect(con);
1440 return 0;
1441}
1442
Sage Weil04a419f2009-12-23 09:30:21 -08001443static void fail_protocol(struct ceph_connection *con)
1444{
1445 reset_connection(con);
1446 set_bit(CLOSED, &con->state); /* in case there's queued work */
Sage Weil04a419f2009-12-23 09:30:21 -08001447}
1448
Sage Weileed0ef22009-11-10 14:34:36 -08001449static int process_connect(struct ceph_connection *con)
1450{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001451 u64 sup_feat = con->msgr->supported_features;
1452 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001453 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001454 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001455
Sage Weileed0ef22009-11-10 14:34:36 -08001456 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1457
Sage Weil31b80062009-10-06 11:31:13 -07001458 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001459 case CEPH_MSGR_TAG_FEATURES:
1460 pr_err("%s%lld %s feature set mismatch,"
1461 " my %llx < server's %llx, missing %llx\n",
1462 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001463 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001464 sup_feat, server_feat, server_feat & ~sup_feat);
1465 con->error_msg = "missing required protocol features";
1466 fail_protocol(con);
1467 return -1;
1468
Sage Weil31b80062009-10-06 11:31:13 -07001469 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001470 pr_err("%s%lld %s protocol version mismatch,"
1471 " my %d != server's %d\n",
1472 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001473 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001474 le32_to_cpu(con->out_connect.protocol_version),
1475 le32_to_cpu(con->in_reply.protocol_version));
1476 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001477 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001478 return -1;
1479
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001480 case CEPH_MSGR_TAG_BADAUTHORIZER:
1481 con->auth_retry++;
1482 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1483 con->auth_retry);
1484 if (con->auth_retry == 2) {
1485 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001486 return -1;
1487 }
1488 con->auth_retry = 1;
Alex Elder4874ba92012-05-23 14:35:23 -05001489 con_out_kvec_reset(con);
Alex Elder7dd07ab2012-05-16 15:16:38 -05001490 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001491 if (ret < 0)
1492 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001493 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001494 break;
Sage Weil31b80062009-10-06 11:31:13 -07001495
1496 case CEPH_MSGR_TAG_RESETSESSION:
1497 /*
1498 * If we connected with a large connect_seq but the peer
1499 * has no record of a session with us (no connection, or
1500 * connect_seq == 0), they will send RESETSESION to indicate
1501 * that they must have reset their session, and may have
1502 * dropped messages.
1503 */
1504 dout("process_connect got RESET peer seq %u\n",
Sage Weil49da2932012-07-10 11:53:34 -07001505 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil31b80062009-10-06 11:31:13 -07001506 pr_err("%s%lld %s connection reset\n",
1507 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001508 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001509 reset_connection(con);
Alex Elder4874ba92012-05-23 14:35:23 -05001510 con_out_kvec_reset(con);
Alex Elder59336e02012-05-16 21:51:59 -05001511 ret = prepare_write_connect(con);
1512 if (ret < 0)
1513 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001514 prepare_read_connect(con);
1515
1516 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001517 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001518 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1519 if (con->ops->peer_reset)
1520 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001521 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07001522 if (test_bit(CLOSED, &con->state) ||
1523 test_bit(OPENING, &con->state))
1524 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001525 break;
1526
1527 case CEPH_MSGR_TAG_RETRY_SESSION:
1528 /*
1529 * If we sent a smaller connect_seq than the peer has, try
1530 * again with a larger value.
1531 */
Sage Weil49da2932012-07-10 11:53:34 -07001532 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001533 le32_to_cpu(con->out_connect.connect_seq),
Sage Weil49da2932012-07-10 11:53:34 -07001534 le32_to_cpu(con->in_reply.connect_seq));
1535 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
Alex Elder4874ba92012-05-23 14:35:23 -05001536 con_out_kvec_reset(con);
Alex Elder59336e02012-05-16 21:51:59 -05001537 ret = prepare_write_connect(con);
1538 if (ret < 0)
1539 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001540 prepare_read_connect(con);
1541 break;
1542
1543 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1544 /*
1545 * If we sent a smaller global_seq than the peer has, try
1546 * again with a larger value.
1547 */
Sage Weileed0ef22009-11-10 14:34:36 -08001548 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001549 con->peer_global_seq,
Sage Weil49da2932012-07-10 11:53:34 -07001550 le32_to_cpu(con->in_reply.global_seq));
Sage Weil31b80062009-10-06 11:31:13 -07001551 get_global_seq(con->msgr,
Sage Weil49da2932012-07-10 11:53:34 -07001552 le32_to_cpu(con->in_reply.global_seq));
Alex Elder4874ba92012-05-23 14:35:23 -05001553 con_out_kvec_reset(con);
Alex Elder59336e02012-05-16 21:51:59 -05001554 ret = prepare_write_connect(con);
1555 if (ret < 0)
1556 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001557 prepare_read_connect(con);
1558 break;
1559
1560 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001561 if (req_feat & ~server_feat) {
1562 pr_err("%s%lld %s protocol feature mismatch,"
1563 " my required %llx > server's %llx, need %llx\n",
1564 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001565 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001566 req_feat, server_feat, req_feat & ~server_feat);
1567 con->error_msg = "missing required protocol features";
1568 fail_protocol(con);
1569 return -1;
1570 }
Sage Weil31b80062009-10-06 11:31:13 -07001571 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001572 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1573 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001574 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001575 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1576 con->peer_global_seq,
1577 le32_to_cpu(con->in_reply.connect_seq),
1578 con->connect_seq);
1579 WARN_ON(con->connect_seq !=
1580 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001581
1582 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elderbc327472012-05-22 11:41:43 -05001583 set_bit(LOSSYTX, &con->flags);
Sage Weil92ac41d2009-12-14 14:56:56 -08001584
Sage Weil31b80062009-10-06 11:31:13 -07001585 prepare_read_tag(con);
1586 break;
1587
1588 case CEPH_MSGR_TAG_WAIT:
1589 /*
1590 * If there is a connection race (we are opening
1591 * connections to each other), one of us may just have
1592 * to WAIT. This shouldn't happen if we are the
1593 * client.
1594 */
Sage Weil04177882011-05-12 15:33:17 -07001595 pr_err("process_connect got WAIT as client\n");
1596 con->error_msg = "protocol error, got WAIT as client";
1597 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001598
1599 default:
1600 pr_err("connect protocol error, will retry\n");
1601 con->error_msg = "protocol error, garbage tag during connect";
1602 return -1;
1603 }
1604 return 0;
1605}
1606
1607
1608/*
1609 * read (part of) an ack
1610 */
1611static int read_partial_ack(struct ceph_connection *con)
1612{
Alex Elder1f7631f2012-05-10 10:29:50 -05001613 int size = sizeof (con->in_temp_ack);
1614 int end = size;
1615
1616 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001617}
1618
1619
1620/*
1621 * We can finally discard anything that's been acked.
1622 */
1623static void process_ack(struct ceph_connection *con)
1624{
1625 struct ceph_msg *m;
1626 u64 ack = le64_to_cpu(con->in_temp_ack);
1627 u64 seq;
1628
Sage Weil31b80062009-10-06 11:31:13 -07001629 while (!list_empty(&con->out_sent)) {
1630 m = list_first_entry(&con->out_sent, struct ceph_msg,
1631 list_head);
1632 seq = le64_to_cpu(m->hdr.seq);
1633 if (seq > ack)
1634 break;
1635 dout("got ack for seq %llu type %d at %p\n", seq,
1636 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001637 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001638 ceph_msg_remove(m);
1639 }
Sage Weil31b80062009-10-06 11:31:13 -07001640 prepare_read_tag(con);
1641}
1642
1643
1644
1645
Yehuda Sadeh24504182010-01-08 13:58:34 -08001646static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001647 struct kvec *section,
1648 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001649{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001650 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001651
Yehuda Sadeh24504182010-01-08 13:58:34 -08001652 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001653
Yehuda Sadeh24504182010-01-08 13:58:34 -08001654 while (section->iov_len < sec_len) {
1655 BUG_ON(section->iov_base == NULL);
1656 left = sec_len - section->iov_len;
1657 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1658 section->iov_len, left);
1659 if (ret <= 0)
1660 return ret;
1661 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001662 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001663 if (section->iov_len == sec_len)
1664 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001665
1666 return 1;
1667}
1668
Alex Elder35067a22012-06-04 14:43:32 -05001669static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
1670 struct ceph_msg_header *hdr);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001671
1672
1673static int read_partial_message_pages(struct ceph_connection *con,
1674 struct page **pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001675 unsigned data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001676{
1677 void *p;
1678 int ret;
1679 int left;
1680
1681 left = min((int)(data_len - con->in_msg_pos.data_pos),
1682 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1683 /* (page) data */
1684 BUG_ON(pages == NULL);
1685 p = kmap(pages[con->in_msg_pos.page]);
1686 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1687 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001688 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001689 con->in_data_crc =
1690 crc32c(con->in_data_crc,
1691 p + con->in_msg_pos.page_pos, ret);
1692 kunmap(pages[con->in_msg_pos.page]);
1693 if (ret <= 0)
1694 return ret;
1695 con->in_msg_pos.data_pos += ret;
1696 con->in_msg_pos.page_pos += ret;
1697 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1698 con->in_msg_pos.page_pos = 0;
1699 con->in_msg_pos.page++;
1700 }
1701
1702 return ret;
1703}
1704
1705#ifdef CONFIG_BLOCK
1706static int read_partial_message_bio(struct ceph_connection *con,
1707 struct bio **bio_iter, int *bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001708 unsigned data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001709{
1710 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1711 void *p;
1712 int ret, left;
1713
1714 if (IS_ERR(bv))
1715 return PTR_ERR(bv);
1716
1717 left = min((int)(data_len - con->in_msg_pos.data_pos),
1718 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1719
1720 p = kmap(bv->bv_page) + bv->bv_offset;
1721
1722 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1723 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001724 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001725 con->in_data_crc =
1726 crc32c(con->in_data_crc,
1727 p + con->in_msg_pos.page_pos, ret);
1728 kunmap(bv->bv_page);
1729 if (ret <= 0)
1730 return ret;
1731 con->in_msg_pos.data_pos += ret;
1732 con->in_msg_pos.page_pos += ret;
1733 if (con->in_msg_pos.page_pos == bv->bv_len) {
1734 con->in_msg_pos.page_pos = 0;
1735 iter_bio_next(bio_iter, bio_seg);
1736 }
1737
1738 return ret;
1739}
1740#endif
1741
Sage Weil31b80062009-10-06 11:31:13 -07001742/*
1743 * read (part of) a message.
1744 */
1745static int read_partial_message(struct ceph_connection *con)
1746{
1747 struct ceph_msg *m = con->in_msg;
Alex Elder1f7631f2012-05-10 10:29:50 -05001748 int size;
1749 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001750 int ret;
Sage Weilc5c6b192010-11-09 12:40:00 -08001751 unsigned front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001752 bool do_datacrc = !con->msgr->nocrc;
Sage Weilae187562010-04-22 07:47:01 -07001753 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001754 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001755
1756 dout("read_partial_message con %p msg %p\n", con, m);
1757
1758 /* header */
Alex Elder1f7631f2012-05-10 10:29:50 -05001759 size = sizeof (con->in_hdr);
1760 end = size;
1761 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elderd0d7af62012-05-10 10:29:50 -05001762 if (ret <= 0)
1763 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001764
1765 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1766 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1767 pr_err("read_partial_message bad hdr "
1768 " crc %u != expected %u\n",
1769 crc, con->in_hdr.crc);
1770 return -EBADMSG;
1771 }
1772
Sage Weil31b80062009-10-06 11:31:13 -07001773 front_len = le32_to_cpu(con->in_hdr.front_len);
1774 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1775 return -EIO;
1776 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1777 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1778 return -EIO;
1779 data_len = le32_to_cpu(con->in_hdr.data_len);
1780 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1781 return -EIO;
1782
Sage Weilae187562010-04-22 07:47:01 -07001783 /* verify seq# */
1784 seq = le64_to_cpu(con->in_hdr.seq);
1785 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001786 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001787 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001788 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001789 seq, con->in_seq + 1);
1790 con->in_base_pos = -front_len - middle_len - data_len -
1791 sizeof(m->footer);
1792 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001793 return 0;
1794 } else if ((s64)seq - (s64)con->in_seq > 1) {
1795 pr_err("read_partial_message bad seq %lld expected %lld\n",
1796 seq, con->in_seq + 1);
1797 con->error_msg = "bad message sequence # for incoming message";
1798 return -EBADMSG;
1799 }
1800
Sage Weil31b80062009-10-06 11:31:13 -07001801 /* allocate message? */
1802 if (!con->in_msg) {
1803 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1804 con->in_hdr.front_len, con->in_hdr.data_len);
Alex Elder35067a22012-06-04 14:43:32 -05001805 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
Sage Weil31b80062009-10-06 11:31:13 -07001806 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001807 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001808 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001809 con->in_base_pos = -front_len - middle_len - data_len -
1810 sizeof(m->footer);
1811 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001812 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001813 return 0;
1814 }
Sage Weila79832f2010-04-01 16:06:19 -07001815 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001816 con->error_msg =
1817 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001818 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001819 }
Alex Eldere84e0662012-06-01 14:56:43 -05001820
1821 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07001822 m = con->in_msg;
1823 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001824 if (m->middle)
1825 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001826
1827 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001828 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001829 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001830 else
1831 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001832 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001833 }
1834
1835 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001836 ret = read_partial_message_section(con, &m->front, front_len,
1837 &con->in_front_crc);
1838 if (ret <= 0)
1839 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001840
1841 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001842 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001843 ret = read_partial_message_section(con, &m->middle->vec,
1844 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001845 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001846 if (ret <= 0)
1847 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001848 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001849#ifdef CONFIG_BLOCK
1850 if (m->bio && !m->bio_iter)
1851 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1852#endif
Sage Weil31b80062009-10-06 11:31:13 -07001853
1854 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001855 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001856 if (m->pages) {
1857 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001858 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001859 if (ret <= 0)
1860 return ret;
1861#ifdef CONFIG_BLOCK
1862 } else if (m->bio) {
1863
1864 ret = read_partial_message_bio(con,
1865 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001866 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001867 if (ret <= 0)
1868 return ret;
1869#endif
1870 } else {
1871 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001872 }
1873 }
1874
Sage Weil31b80062009-10-06 11:31:13 -07001875 /* footer */
Alex Elder1f7631f2012-05-10 10:29:50 -05001876 size = sizeof (m->footer);
1877 end += size;
1878 ret = read_partial(con, end, size, &m->footer);
Alex Elderd0d7af62012-05-10 10:29:50 -05001879 if (ret <= 0)
1880 return ret;
1881
Sage Weil31b80062009-10-06 11:31:13 -07001882 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1883 m, front_len, m->footer.front_crc, middle_len,
1884 m->footer.middle_crc, data_len, m->footer.data_crc);
1885
1886 /* crc ok? */
1887 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1888 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1889 m, con->in_front_crc, m->footer.front_crc);
1890 return -EBADMSG;
1891 }
1892 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1893 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1894 m, con->in_middle_crc, m->footer.middle_crc);
1895 return -EBADMSG;
1896 }
Alex Elderbca064d2012-02-15 07:43:54 -06001897 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001898 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1899 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1900 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1901 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1902 return -EBADMSG;
1903 }
1904
1905 return 1; /* done! */
1906}
1907
1908/*
1909 * Process message. This happens in the worker thread. The callback should
1910 * be careful not to do anything that waits on other incoming messages or it
1911 * may deadlock.
1912 */
1913static void process_message(struct ceph_connection *con)
1914{
Sage Weil5e095e82009-12-14 14:30:34 -08001915 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001916
Alex Eldere84e0662012-06-01 14:56:43 -05001917 BUG_ON(con->in_msg->con != con);
1918 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08001919 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001920 con->in_msg = NULL;
1921
1922 /* if first message, set peer_name */
1923 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001924 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001925
Sage Weil31b80062009-10-06 11:31:13 -07001926 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001927 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001928
1929 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1930 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001931 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001932 le16_to_cpu(msg->hdr.type),
1933 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1934 le32_to_cpu(msg->hdr.front_len),
1935 le32_to_cpu(msg->hdr.data_len),
1936 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1937 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001938
1939 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001940 prepare_read_tag(con);
1941}
1942
1943
1944/*
1945 * Write something to the socket. Called in a worker thread when the
1946 * socket appears to be writeable and we have something ready to send.
1947 */
1948static int try_write(struct ceph_connection *con)
1949{
Sage Weil31b80062009-10-06 11:31:13 -07001950 int ret = 1;
1951
1952 dout("try_write start %p state %lu nref %d\n", con, con->state,
1953 atomic_read(&con->nref));
1954
Sage Weil31b80062009-10-06 11:31:13 -07001955more:
1956 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1957
1958 /* open the socket first? */
1959 if (con->sock == NULL) {
Alex Elder51588ed2012-05-29 11:04:58 -05001960 clear_bit(NEGOTIATING, &con->state);
1961 set_bit(CONNECTING, &con->state);
1962
Alex Elder4874ba92012-05-23 14:35:23 -05001963 con_out_kvec_reset(con);
Alex Elder7dd07ab2012-05-16 15:16:38 -05001964 prepare_write_banner(con);
Alex Elder59336e02012-05-16 21:51:59 -05001965 ret = prepare_write_connect(con);
1966 if (ret < 0)
1967 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08001968 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001969
Sage Weilcf3e5c42009-12-11 09:48:05 -08001970 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001971 con->in_tag = CEPH_MSGR_TAG_READY;
1972 dout("try_write initiating connect on %p new state %lu\n",
1973 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06001974 ret = ceph_tcp_connect(con);
1975 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07001976 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07001977 goto out;
1978 }
1979 }
1980
1981more_kvec:
1982 /* kvec data queued? */
1983 if (con->out_skip) {
1984 ret = write_partial_skip(con);
1985 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001986 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001987 }
1988 if (con->out_kvec_left) {
1989 ret = write_partial_kvec(con);
1990 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001991 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001992 }
1993
1994 /* msg pages? */
1995 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001996 if (con->out_msg_done) {
1997 ceph_msg_put(con->out_msg);
1998 con->out_msg = NULL; /* we're done with this one */
1999 goto do_next;
2000 }
2001
Sage Weil31b80062009-10-06 11:31:13 -07002002 ret = write_partial_msg_pages(con);
2003 if (ret == 1)
2004 goto more_kvec; /* we need to send the footer, too! */
2005 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002006 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002007 if (ret < 0) {
2008 dout("try_write write_partial_msg_pages err %d\n",
2009 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002010 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002011 }
2012 }
2013
Sage Weilc86a2932009-12-14 14:04:30 -08002014do_next:
Sage Weil31b80062009-10-06 11:31:13 -07002015 if (!test_bit(CONNECTING, &con->state)) {
2016 /* is anything else pending? */
2017 if (!list_empty(&con->out_queue)) {
2018 prepare_write_message(con);
2019 goto more;
2020 }
2021 if (con->in_seq > con->in_seq_acked) {
2022 prepare_write_ack(con);
2023 goto more;
2024 }
Alex Elderbc327472012-05-22 11:41:43 -05002025 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002026 prepare_write_keepalive(con);
2027 goto more;
2028 }
2029 }
2030
2031 /* Nothing to do! */
Alex Elderbc327472012-05-22 11:41:43 -05002032 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002033 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002034 ret = 0;
2035out:
Sage Weil42961d22011-01-25 08:19:34 -08002036 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002037 return ret;
2038}
2039
2040
2041
2042/*
2043 * Read what we can from the socket.
2044 */
2045static int try_read(struct ceph_connection *con)
2046{
Sage Weil31b80062009-10-06 11:31:13 -07002047 int ret = -1;
2048
2049 if (!con->sock)
2050 return 0;
2051
2052 if (test_bit(STANDBY, &con->state))
2053 return 0;
2054
2055 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08002056
Sage Weil31b80062009-10-06 11:31:13 -07002057more:
2058 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2059 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002060
2061 /*
2062 * process_connect and process_message drop and re-take
2063 * con->mutex. make sure we handle a racing close or reopen.
2064 */
2065 if (test_bit(CLOSED, &con->state) ||
2066 test_bit(OPENING, &con->state)) {
2067 ret = -EAGAIN;
2068 goto out;
2069 }
2070
Sage Weil31b80062009-10-06 11:31:13 -07002071 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08002072 if (!test_bit(NEGOTIATING, &con->state)) {
2073 dout("try_read connecting\n");
2074 ret = read_partial_banner(con);
2075 if (ret <= 0)
Sage Weileed0ef22009-11-10 14:34:36 -08002076 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002077 ret = process_banner(con);
2078 if (ret < 0)
2079 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08002080 }
Sage Weil31b80062009-10-06 11:31:13 -07002081 ret = read_partial_connect(con);
2082 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002083 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002084 ret = process_connect(con);
2085 if (ret < 0)
2086 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002087 goto more;
2088 }
2089
2090 if (con->in_base_pos < 0) {
2091 /*
2092 * skipping + discarding content.
2093 *
2094 * FIXME: there must be a better way to do this!
2095 */
Alex Elder84495f42012-02-15 07:43:55 -06002096 static char buf[SKIP_BUF_SIZE];
2097 int skip = min((int) sizeof (buf), -con->in_base_pos);
2098
Sage Weil31b80062009-10-06 11:31:13 -07002099 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2100 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2101 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002102 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002103 con->in_base_pos += ret;
2104 if (con->in_base_pos)
2105 goto more;
2106 }
2107 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2108 /*
2109 * what's next?
2110 */
2111 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2112 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002113 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002114 dout("try_read got tag %d\n", (int)con->in_tag);
2115 switch (con->in_tag) {
2116 case CEPH_MSGR_TAG_MSG:
2117 prepare_read_message(con);
2118 break;
2119 case CEPH_MSGR_TAG_ACK:
2120 prepare_read_ack(con);
2121 break;
2122 case CEPH_MSGR_TAG_CLOSE:
2123 set_bit(CLOSED, &con->state); /* fixme */
Sage Weil98bdb0a2011-01-25 08:17:48 -08002124 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002125 default:
2126 goto bad_tag;
2127 }
2128 }
2129 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2130 ret = read_partial_message(con);
2131 if (ret <= 0) {
2132 switch (ret) {
2133 case -EBADMSG:
2134 con->error_msg = "bad crc";
2135 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002136 break;
Sage Weil31b80062009-10-06 11:31:13 -07002137 case -EIO:
2138 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002139 break;
Sage Weil31b80062009-10-06 11:31:13 -07002140 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002141 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002142 }
2143 if (con->in_tag == CEPH_MSGR_TAG_READY)
2144 goto more;
2145 process_message(con);
2146 goto more;
2147 }
2148 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2149 ret = read_partial_ack(con);
2150 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002151 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002152 process_ack(con);
2153 goto more;
2154 }
2155
Sage Weil31b80062009-10-06 11:31:13 -07002156out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002157 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002158 return ret;
2159
2160bad_tag:
2161 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2162 con->error_msg = "protocol error, garbage tag";
2163 ret = -1;
2164 goto out;
2165}
2166
2167
2168/*
2169 * Atomically queue work on a connection. Bump @con reference to
2170 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002171 */
2172static void queue_con(struct ceph_connection *con)
2173{
Sage Weil31b80062009-10-06 11:31:13 -07002174 if (!con->ops->get(con)) {
2175 dout("queue_con %p ref count 0\n", con);
2176 return;
2177 }
2178
Tejun Heof363e452011-01-03 14:49:46 +01002179 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002180 dout("queue_con %p - already queued\n", con);
2181 con->ops->put(con);
2182 } else {
2183 dout("queue_con %p\n", con);
2184 }
2185}
2186
2187/*
2188 * Do some work on a connection. Drop a connection ref when we're done.
2189 */
2190static void con_work(struct work_struct *work)
2191{
2192 struct ceph_connection *con = container_of(work, struct ceph_connection,
2193 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002194 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002195
Sage Weil9dd46582010-04-28 13:51:50 -07002196 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002197restart:
Alex Elderbc327472012-05-22 11:41:43 -05002198 if (test_and_clear_bit(BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002199 dout("con_work %p backing off\n", con);
2200 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2201 round_jiffies_relative(con->delay))) {
2202 dout("con_work %p backoff %lu\n", con, con->delay);
2203 mutex_unlock(&con->mutex);
2204 return;
2205 } else {
2206 con->ops->put(con);
2207 dout("con_work %p FAILED to back off %lu\n", con,
2208 con->delay);
2209 }
2210 }
Sage Weil9dd46582010-04-28 13:51:50 -07002211
Sage Weile00de342011-03-04 12:25:05 -08002212 if (test_bit(STANDBY, &con->state)) {
2213 dout("con_work %p STANDBY\n", con);
2214 goto done;
2215 }
Sage Weil31b80062009-10-06 11:31:13 -07002216 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2217 dout("con_work CLOSED\n");
2218 con_close_socket(con);
2219 goto done;
2220 }
2221 if (test_and_clear_bit(OPENING, &con->state)) {
2222 /* reopen w/ new peer */
2223 dout("con_work OPENING\n");
2224 con_close_socket(con);
2225 }
2226
Alex Elderbc327472012-05-22 11:41:43 -05002227 if (test_and_clear_bit(SOCK_CLOSED, &con->flags))
Sage Weil0da5d702011-05-19 11:21:05 -07002228 goto fault;
2229
2230 ret = try_read(con);
2231 if (ret == -EAGAIN)
2232 goto restart;
2233 if (ret < 0)
2234 goto fault;
2235
2236 ret = try_write(con);
2237 if (ret == -EAGAIN)
2238 goto restart;
2239 if (ret < 0)
2240 goto fault;
Sage Weil31b80062009-10-06 11:31:13 -07002241
2242done:
Sage Weil9dd46582010-04-28 13:51:50 -07002243 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002244done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002245 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002246 return;
2247
2248fault:
2249 mutex_unlock(&con->mutex);
2250 ceph_fault(con); /* error/fault path */
2251 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002252}
2253
2254
2255/*
2256 * Generic error/fault handler. A retry mechanism is used with
2257 * exponential backoff
2258 */
2259static void ceph_fault(struct ceph_connection *con)
2260{
2261 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002262 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002263 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002264 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002265
Alex Elderbc327472012-05-22 11:41:43 -05002266 if (test_bit(LOSSYTX, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002267 dout("fault on LOSSYTX channel\n");
2268 goto out;
2269 }
2270
Sage Weilec302642009-12-22 10:43:42 -08002271 mutex_lock(&con->mutex);
Sage Weil91e45ce2010-02-15 12:05:09 -08002272 if (test_bit(CLOSED, &con->state))
2273 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002274
Sage Weil31b80062009-10-06 11:31:13 -07002275 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002276
2277 if (con->in_msg) {
Alex Eldere84e0662012-06-01 14:56:43 -05002278 BUG_ON(con->in_msg->con != con);
2279 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08002280 ceph_msg_put(con->in_msg);
2281 con->in_msg = NULL;
2282 }
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 Elderbc327472012-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 Elderbc327472012-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 Elderbc327472012-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 Weil91e45ce2010-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 Elderd910c112012-05-26 23:26:43 -05002339 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002340 */
Alex Elderd910c112012-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 Elderd910c112012-05-26 23:26:43 -05002359 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002360
Alex Elderd910c112012-05-26 23:26:43 -05002361 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002362}
Alex Elderd910c112012-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 Elderbc327472012-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 Eldere84e0662012-06-01 14:56:43 -05002398 BUG_ON(msg->con != NULL);
2399 msg->con = con;
Sage Weil31b80062009-10-06 11:31:13 -07002400 BUG_ON(!list_empty(&msg->list_head));
2401 list_add_tail(&msg->list_head, &con->out_queue);
2402 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2403 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2404 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2405 le32_to_cpu(msg->hdr.front_len),
2406 le32_to_cpu(msg->hdr.middle_len),
2407 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002408 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002409
2410 /* if there wasn't anything waiting to send before, queue
2411 * new work */
Sage Weile00de342011-03-04 12:25:05 -08002412 clear_standby(con);
Alex Elderbc327472012-05-22 11:41:43 -05002413 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002414 queue_con(con);
2415}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002416EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002417
2418/*
2419 * Revoke a message that was previously queued for send
2420 */
2421void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
2422{
Sage Weilec302642009-12-22 10:43:42 -08002423 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002424 if (!list_empty(&msg->list_head)) {
Alex Eldere84e0662012-06-01 14:56:43 -05002425 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002426 list_del_init(&msg->list_head);
Alex Eldere84e0662012-06-01 14:56:43 -05002427 BUG_ON(msg->con == NULL);
2428 msg->con = NULL;
2429
Sage Weil31b80062009-10-06 11:31:13 -07002430 ceph_msg_put(msg);
2431 msg->hdr.seq = 0;
Sage Weiled98ada2010-07-05 12:15:14 -07002432 }
2433 if (con->out_msg == msg) {
Alex Eldere84e0662012-06-01 14:56:43 -05002434 dout("%s %p msg %p - was sending\n", __func__, con, msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002435 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002436 if (con->out_kvec_is_msg) {
2437 con->out_skip = con->out_kvec_bytes;
2438 con->out_kvec_is_msg = false;
2439 }
Sage Weiled98ada2010-07-05 12:15:14 -07002440 ceph_msg_put(msg);
2441 msg->hdr.seq = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002442 }
Sage Weilec302642009-12-22 10:43:42 -08002443 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002444}
2445
2446/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002447 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002448 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002449void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002450{
2451 mutex_lock(&con->mutex);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002452 if (con->in_msg && con->in_msg == msg) {
2453 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2454 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002455 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2456
2457 /* skip rest of message */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002458 dout("con_revoke_pages %p msg %p revoked\n", con, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002459 con->in_base_pos = con->in_base_pos -
2460 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002461 front_len -
2462 middle_len -
2463 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002464 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002465 ceph_msg_put(con->in_msg);
2466 con->in_msg = NULL;
2467 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002468 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002469 } else {
2470 dout("con_revoke_pages %p msg %p pages %p no-op\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002471 con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002472 }
2473 mutex_unlock(&con->mutex);
2474}
2475
2476/*
Sage Weil31b80062009-10-06 11:31:13 -07002477 * Queue a keepalive byte to ensure the tcp connection is alive.
2478 */
2479void ceph_con_keepalive(struct ceph_connection *con)
2480{
Sage Weile00de342011-03-04 12:25:05 -08002481 dout("con_keepalive %p\n", con);
2482 clear_standby(con);
Alex Elderbc327472012-05-22 11:41:43 -05002483 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2484 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002485 queue_con(con);
2486}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002487EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002488
2489
2490/*
2491 * construct a new message with given type, size
2492 * the new msg has a ref count of 1.
2493 */
Sage Weilb61c2762011-08-09 15:03:46 -07002494struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2495 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002496{
2497 struct ceph_msg *m;
2498
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002499 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002500 if (m == NULL)
2501 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002502 kref_init(&m->kref);
Alex Eldere84e0662012-06-01 14:56:43 -05002503
2504 m->con = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002505 INIT_LIST_HEAD(&m->list_head);
2506
Sage Weil45c6ceb2010-05-11 15:01:51 -07002507 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002508 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002509 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2510 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002511 m->hdr.front_len = cpu_to_le32(front_len);
2512 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002513 m->hdr.data_len = 0;
2514 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002515 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002516 m->footer.front_crc = 0;
2517 m->footer.middle_crc = 0;
2518 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002519 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002520 m->front_max = front_len;
2521 m->front_is_vmalloc = false;
2522 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002523 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002524 m->pool = NULL;
2525
Henry C Changca208922011-05-03 02:29:56 +00002526 /* middle */
2527 m->middle = NULL;
2528
2529 /* data */
2530 m->nr_pages = 0;
2531 m->page_alignment = 0;
2532 m->pages = NULL;
2533 m->pagelist = NULL;
2534 m->bio = NULL;
2535 m->bio_iter = NULL;
2536 m->bio_seg = 0;
2537 m->trail = NULL;
2538
Sage Weil31b80062009-10-06 11:31:13 -07002539 /* front */
2540 if (front_len) {
2541 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002542 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002543 PAGE_KERNEL);
2544 m->front_is_vmalloc = true;
2545 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002546 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002547 }
2548 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002549 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002550 front_len);
2551 goto out2;
2552 }
2553 } else {
2554 m->front.iov_base = NULL;
2555 }
2556 m->front.iov_len = front_len;
2557
Sage Weilbb257662010-04-01 16:07:23 -07002558 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002559 return m;
2560
2561out2:
2562 ceph_msg_put(m);
2563out:
Sage Weilb61c2762011-08-09 15:03:46 -07002564 if (!can_fail) {
2565 pr_err("msg_new can't create type %d front %d\n", type,
2566 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002567 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002568 } else {
2569 dout("msg_new can't create type %d front %d\n", type,
2570 front_len);
2571 }
Sage Weila79832f2010-04-01 16:06:19 -07002572 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002573}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002574EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002575
2576/*
Sage Weil31b80062009-10-06 11:31:13 -07002577 * Allocate "middle" portion of a message, if it is needed and wasn't
2578 * allocated by alloc_msg. This allows us to read a small fixed-size
2579 * per-type header in the front and then gracefully fail (i.e.,
2580 * propagate the error to the caller based on info in the front) when
2581 * the middle is too large.
2582 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002583static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002584{
2585 int type = le16_to_cpu(msg->hdr.type);
2586 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2587
2588 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2589 ceph_msg_type_name(type), middle_len);
2590 BUG_ON(!middle_len);
2591 BUG_ON(msg->middle);
2592
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002593 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002594 if (!msg->middle)
2595 return -ENOMEM;
2596 return 0;
2597}
2598
Yehuda Sadeh24504182010-01-08 13:58:34 -08002599/*
Alex Elder35067a22012-06-04 14:43:32 -05002600 * Allocate a message for receiving an incoming message on a
2601 * connection, and save the result in con->in_msg. Uses the
2602 * connection's private alloc_msg op if available.
2603 *
2604 * Returns true if the message should be skipped, false otherwise.
2605 * If true is returned (skip message), con->in_msg will be NULL.
2606 * If false is returned, con->in_msg will contain a pointer to the
2607 * newly-allocated message, or NULL in case of memory exhaustion.
Yehuda Sadeh24504182010-01-08 13:58:34 -08002608 */
Alex Elder35067a22012-06-04 14:43:32 -05002609static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2610 struct ceph_msg_header *hdr)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002611{
2612 int type = le16_to_cpu(hdr->type);
2613 int front_len = le32_to_cpu(hdr->front_len);
2614 int middle_len = le32_to_cpu(hdr->middle_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002615 int ret;
2616
Alex Elder35067a22012-06-04 14:43:32 -05002617 BUG_ON(con->in_msg != NULL);
2618
Yehuda Sadeh24504182010-01-08 13:58:34 -08002619 if (con->ops->alloc_msg) {
Alex Elder35067a22012-06-04 14:43:32 -05002620 int skip = 0;
2621
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002622 mutex_unlock(&con->mutex);
Alex Elder35067a22012-06-04 14:43:32 -05002623 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002624 mutex_lock(&con->mutex);
Alex Eldere84e0662012-06-01 14:56:43 -05002625 if (con->in_msg)
2626 con->in_msg->con = con;
Alex Elder35067a22012-06-04 14:43:32 -05002627 if (skip)
2628 con->in_msg = NULL;
2629
2630 if (!con->in_msg)
2631 return skip != 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002632 }
Alex Elder35067a22012-06-04 14:43:32 -05002633 if (!con->in_msg) {
2634 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2635 if (!con->in_msg) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002636 pr_err("unable to allocate msg type %d len %d\n",
2637 type, front_len);
Alex Elder35067a22012-06-04 14:43:32 -05002638 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002639 }
Alex Eldere84e0662012-06-01 14:56:43 -05002640 con->in_msg->con = con;
Alex Elder35067a22012-06-04 14:43:32 -05002641 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002642 }
Alex Elder35067a22012-06-04 14:43:32 -05002643 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002644
Alex Elder35067a22012-06-04 14:43:32 -05002645 if (middle_len && !con->in_msg->middle) {
2646 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002647 if (ret < 0) {
Alex Elder35067a22012-06-04 14:43:32 -05002648 ceph_msg_put(con->in_msg);
2649 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002650 }
2651 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002652
Alex Elder35067a22012-06-04 14:43:32 -05002653 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002654}
2655
Sage Weil31b80062009-10-06 11:31:13 -07002656
2657/*
2658 * Free a generically kmalloc'd message.
2659 */
2660void ceph_msg_kfree(struct ceph_msg *m)
2661{
2662 dout("msg_kfree %p\n", m);
2663 if (m->front_is_vmalloc)
2664 vfree(m->front.iov_base);
2665 else
2666 kfree(m->front.iov_base);
2667 kfree(m);
2668}
2669
2670/*
2671 * Drop a msg ref. Destroy as needed.
2672 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002673void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002674{
Sage Weilc2e552e2009-12-07 15:55:05 -08002675 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002676
Sage Weilc2e552e2009-12-07 15:55:05 -08002677 dout("ceph_msg_put last one on %p\n", m);
2678 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002679
Sage Weilc2e552e2009-12-07 15:55:05 -08002680 /* drop middle, data, if any */
2681 if (m->middle) {
2682 ceph_buffer_put(m->middle);
2683 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002684 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002685 m->nr_pages = 0;
2686 m->pages = NULL;
2687
Sage Weil58bb3b32009-12-23 12:12:31 -08002688 if (m->pagelist) {
2689 ceph_pagelist_release(m->pagelist);
2690 kfree(m->pagelist);
2691 m->pagelist = NULL;
2692 }
2693
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002694 m->trail = NULL;
2695
Sage Weilc2e552e2009-12-07 15:55:05 -08002696 if (m->pool)
2697 ceph_msgpool_put(m->pool, m);
2698 else
2699 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002700}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002701EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002702
2703void ceph_msg_dump(struct ceph_msg *msg)
2704{
2705 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2706 msg->front_max, msg->nr_pages);
2707 print_hex_dump(KERN_DEBUG, "header: ",
2708 DUMP_PREFIX_OFFSET, 16, 1,
2709 &msg->hdr, sizeof(msg->hdr), true);
2710 print_hex_dump(KERN_DEBUG, " front: ",
2711 DUMP_PREFIX_OFFSET, 16, 1,
2712 msg->front.iov_base, msg->front.iov_len, true);
2713 if (msg->middle)
2714 print_hex_dump(KERN_DEBUG, "middle: ",
2715 DUMP_PREFIX_OFFSET, 16, 1,
2716 msg->middle->vec.iov_base,
2717 msg->middle->vec.iov_len, true);
2718 print_hex_dump(KERN_DEBUG, "footer: ",
2719 DUMP_PREFIX_OFFSET, 16, 1,
2720 &msg->footer, sizeof(msg->footer), true);
2721}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002722EXPORT_SYMBOL(ceph_msg_dump);