blob: 68b49b5b8e861c56c2f42b685b991eaaaf40bb80 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil31b80062009-10-06 11:31:13 -07002
3#include <linux/crc32c.h>
4#include <linux/ctype.h>
5#include <linux/highmem.h>
6#include <linux/inet.h>
7#include <linux/kthread.h>
8#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Sage Weil31b80062009-10-06 11:31:13 -070010#include <linux/socket.h>
11#include <linux/string.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070012#include <linux/bio.h>
13#include <linux/blkdev.h>
Noah Watkinsee3b56f2011-09-23 11:48:42 -070014#include <linux/dns_resolver.h>
Sage Weil31b80062009-10-06 11:31:13 -070015#include <net/tcp.h>
16
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070017#include <linux/ceph/libceph.h>
18#include <linux/ceph/messenger.h>
19#include <linux/ceph/decode.h>
20#include <linux/ceph/pagelist.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040021#include <linux/export.h>
Sage Weil31b80062009-10-06 11:31:13 -070022
23/*
24 * Ceph uses the messenger to exchange ceph_msg messages with other
25 * hosts in the system. The messenger provides ordered and reliable
26 * delivery. We tolerate TCP disconnects by reconnecting (with
27 * exponential backoff) in the case of a fault (disconnection, bad
28 * crc, protocol error). Acks allow sent messages to be discarded by
29 * the sender.
30 */
31
Alex Elderce2c8902012-05-22 22:15:49 -050032/* State values for ceph_connection->sock_state; NEW is assumed to be 0 */
33
34#define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
35#define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
36#define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
37#define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
38#define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
39
Sage Weil31b80062009-10-06 11:31:13 -070040/* static tag bytes (protocol control messages) */
41static char tag_msg = CEPH_MSGR_TAG_MSG;
42static char tag_ack = CEPH_MSGR_TAG_ACK;
43static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
44
Sage Weila6a53492010-04-13 14:07:07 -070045#ifdef CONFIG_LOCKDEP
46static struct lock_class_key socket_class;
47#endif
48
Alex Elder84495f42012-02-15 07:43:55 -060049/*
50 * When skipping (ignoring) a block of input we read it into a "skip
51 * buffer," which is this many bytes in size.
52 */
53#define SKIP_BUF_SIZE 1024
Sage Weil31b80062009-10-06 11:31:13 -070054
55static void queue_con(struct ceph_connection *con);
56static void con_work(struct work_struct *);
57static void ceph_fault(struct ceph_connection *con);
58
Sage Weil31b80062009-10-06 11:31:13 -070059/*
Alex Elderf64a9312012-01-23 15:49:27 -060060 * Nicely render a sockaddr as a string. An array of formatted
61 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -070062 */
Alex Elderf64a9312012-01-23 15:49:27 -060063#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
64#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
65#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
66#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
67
68static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
69static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -070070
Alex Elder57666512012-01-23 15:49:27 -060071static struct page *zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -060072
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070073const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -070074{
75 int i;
76 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -060077 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
78 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -070079
Alex Elderf64a9312012-01-23 15:49:27 -060080 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -070081 s = addr_str[i];
82
83 switch (ss->ss_family) {
84 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -060085 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
86 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -070087 break;
88
89 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -060090 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
91 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -070092 break;
93
94 default:
Alex Elderd3002b92012-02-14 14:05:33 -060095 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
96 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -070097 }
98
99 return s;
100}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700101EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700102
Sage Weil63f2d212009-11-03 15:17:56 -0800103static void encode_my_addr(struct ceph_messenger *msgr)
104{
105 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
106 ceph_encode_addr(&msgr->my_enc_addr);
107}
108
Sage Weil31b80062009-10-06 11:31:13 -0700109/*
110 * work queue for all reading and writing to/from the socket.
111 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600112static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700113
Alex Elder6173d1f2012-02-14 14:05:33 -0600114void _ceph_msgr_exit(void)
115{
Alex Elderd3002b92012-02-14 14:05:33 -0600116 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600117 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600118 ceph_msgr_wq = NULL;
119 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600120
Alex Elder6173d1f2012-02-14 14:05:33 -0600121 BUG_ON(zero_page == NULL);
122 kunmap(zero_page);
123 page_cache_release(zero_page);
124 zero_page = NULL;
125}
126
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700127int ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700128{
Alex Elder57666512012-01-23 15:49:27 -0600129 BUG_ON(zero_page != NULL);
130 zero_page = ZERO_PAGE(0);
131 page_cache_get(zero_page);
132
Tejun Heof363e452011-01-03 14:49:46 +0100133 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600134 if (ceph_msgr_wq)
135 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600136
Alex Elder6173d1f2012-02-14 14:05:33 -0600137 pr_err("msgr_init failed to create workqueue\n");
138 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600139
Alex Elder6173d1f2012-02-14 14:05:33 -0600140 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700141}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700142EXPORT_SYMBOL(ceph_msgr_init);
Sage Weil31b80062009-10-06 11:31:13 -0700143
144void ceph_msgr_exit(void)
145{
Alex Elder57666512012-01-23 15:49:27 -0600146 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600147
Alex Elder6173d1f2012-02-14 14:05:33 -0600148 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700149}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700150EXPORT_SYMBOL(ceph_msgr_exit);
Sage Weil31b80062009-10-06 11:31:13 -0700151
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700152void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700153{
154 flush_workqueue(ceph_msgr_wq);
155}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700156EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700157
Alex Elderce2c8902012-05-22 22:15:49 -0500158/* Connection socket state transition functions */
159
160static void con_sock_state_init(struct ceph_connection *con)
161{
162 int old_state;
163
164 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
165 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
166 printk("%s: unexpected old state %d\n", __func__, old_state);
167}
168
169static void con_sock_state_connecting(struct ceph_connection *con)
170{
171 int old_state;
172
173 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
174 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
175 printk("%s: unexpected old state %d\n", __func__, old_state);
176}
177
178static void con_sock_state_connected(struct ceph_connection *con)
179{
180 int old_state;
181
182 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
183 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
184 printk("%s: unexpected old state %d\n", __func__, old_state);
185}
186
187static void con_sock_state_closing(struct ceph_connection *con)
188{
189 int old_state;
190
191 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
192 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
193 old_state != CON_SOCK_STATE_CONNECTED &&
194 old_state != CON_SOCK_STATE_CLOSING))
195 printk("%s: unexpected old state %d\n", __func__, old_state);
196}
197
198static void con_sock_state_closed(struct ceph_connection *con)
199{
200 int old_state;
201
202 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
203 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
204 old_state != CON_SOCK_STATE_CLOSING))
205 printk("%s: unexpected old state %d\n", __func__, old_state);
206}
Sage Weila922d382010-05-29 09:41:23 -0700207
Sage Weil31b80062009-10-06 11:31:13 -0700208/*
209 * socket callback functions
210 */
211
212/* data available on socket, or listen socket received a connect */
Alex Elder327800b2012-05-22 11:41:43 -0500213static void ceph_sock_data_ready(struct sock *sk, int count_unused)
Sage Weil31b80062009-10-06 11:31:13 -0700214{
Alex Elderbd406142012-01-23 15:49:27 -0600215 struct ceph_connection *con = sk->sk_user_data;
216
Sage Weil31b80062009-10-06 11:31:13 -0700217 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500218 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700219 con, con->state);
220 queue_con(con);
221 }
222}
223
224/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500225static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700226{
Alex Elderd3002b92012-02-14 14:05:33 -0600227 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700228
Jim Schutt182fac22012-02-29 08:30:58 -0700229 /* only queue to workqueue if there is data we want to write,
230 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500231 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700232 * doesn't get called again until try_write() fills the socket
233 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
234 * and net/core/stream.c:sk_stream_write_space().
235 */
Alex Elder928443c2012-05-22 11:41:43 -0500236 if (test_bit(WRITE_PENDING, &con->flags)) {
Jim Schutt182fac22012-02-29 08:30:58 -0700237 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500238 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700239 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
240 queue_con(con);
241 }
Sage Weil31b80062009-10-06 11:31:13 -0700242 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500243 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700244 }
Sage Weil31b80062009-10-06 11:31:13 -0700245}
246
247/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500248static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700249{
Alex Elderbd406142012-01-23 15:49:27 -0600250 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700251
Alex Elder327800b2012-05-22 11:41:43 -0500252 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700253 con, con->state, sk->sk_state);
254
255 if (test_bit(CLOSED, &con->state))
256 return;
257
258 switch (sk->sk_state) {
259 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500260 dout("%s TCP_CLOSE\n", __func__);
Sage Weil31b80062009-10-06 11:31:13 -0700261 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500262 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500263 con_sock_state_closing(con);
Alex Elder928443c2012-05-22 11:41:43 -0500264 if (test_and_set_bit(SOCK_CLOSED, &con->flags) == 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700265 if (test_bit(CONNECTING, &con->state))
266 con->error_msg = "connection failed";
267 else
268 con->error_msg = "socket closed";
269 queue_con(con);
270 }
271 break;
272 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500273 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500274 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700275 queue_con(con);
276 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600277 default: /* Everything else is uninteresting */
278 break;
Sage Weil31b80062009-10-06 11:31:13 -0700279 }
280}
281
282/*
283 * set up socket callbacks
284 */
285static void set_sock_callbacks(struct socket *sock,
286 struct ceph_connection *con)
287{
288 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600289 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500290 sk->sk_data_ready = ceph_sock_data_ready;
291 sk->sk_write_space = ceph_sock_write_space;
292 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700293}
294
295
296/*
297 * socket helpers
298 */
299
300/*
301 * initiate connection to a remote socket.
302 */
Alex Elder41617d02012-02-14 14:05:33 -0600303static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700304{
Sage Weilf91d3472010-07-01 15:18:31 -0700305 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700306 struct socket *sock;
307 int ret;
308
309 BUG_ON(con->sock);
Sage Weilf91d3472010-07-01 15:18:31 -0700310 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
311 IPPROTO_TCP, &sock);
Sage Weil31b80062009-10-06 11:31:13 -0700312 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600313 return ret;
Sage Weil31b80062009-10-06 11:31:13 -0700314 sock->sk->sk_allocation = GFP_NOFS;
315
Sage Weila6a53492010-04-13 14:07:07 -0700316#ifdef CONFIG_LOCKDEP
317 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
318#endif
319
Sage Weil31b80062009-10-06 11:31:13 -0700320 set_sock_callbacks(sock, con);
321
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700322 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700323
Sage Weilf91d3472010-07-01 15:18:31 -0700324 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
325 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700326 if (ret == -EINPROGRESS) {
327 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700328 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700329 sock->sk->sk_state);
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600330 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700331 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700332 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700333 sock_release(sock);
Sage Weil31b80062009-10-06 11:31:13 -0700334 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -0700335
Alex Elder41617d02012-02-14 14:05:33 -0600336 return ret;
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600337 }
338 con->sock = sock;
Alex Elderce2c8902012-05-22 22:15:49 -0500339 con_sock_state_connecting(con);
Alex Eldera5bc3129a2012-01-23 15:49:27 -0600340
Alex Elder41617d02012-02-14 14:05:33 -0600341 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700342}
343
344static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
345{
346 struct kvec iov = {buf, len};
347 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800348 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700349
Sage Weil98bdb0a2011-01-25 08:17:48 -0800350 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
351 if (r == -EAGAIN)
352 r = 0;
353 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700354}
355
356/*
357 * write something. @more is true if caller will be sending more data
358 * shortly.
359 */
360static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
361 size_t kvlen, size_t len, int more)
362{
363 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800364 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700365
366 if (more)
367 msg.msg_flags |= MSG_MORE;
368 else
369 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
370
Sage Weil42961d22011-01-25 08:19:34 -0800371 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
372 if (r == -EAGAIN)
373 r = 0;
374 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700375}
376
Alex Elder31739132012-03-07 11:40:08 -0600377static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
378 int offset, size_t size, int more)
379{
380 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
381 int ret;
382
383 ret = kernel_sendpage(sock, page, offset, size, flags);
384 if (ret == -EAGAIN)
385 ret = 0;
386
387 return ret;
388}
389
Sage Weil31b80062009-10-06 11:31:13 -0700390
391/*
392 * Shutdown/close the socket for the given connection.
393 */
394static int con_close_socket(struct ceph_connection *con)
395{
396 int rc;
397
398 dout("con_close_socket on %p sock %p\n", con, con->sock);
399 if (!con->sock)
400 return 0;
401 set_bit(SOCK_CLOSED, &con->state);
402 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
403 sock_release(con->sock);
404 con->sock = NULL;
405 clear_bit(SOCK_CLOSED, &con->state);
Alex Elderce2c8902012-05-22 22:15:49 -0500406 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700407 return rc;
408}
409
410/*
411 * Reset a connection. Discard all incoming and outgoing messages
412 * and clear *_seq state.
413 */
414static void ceph_msg_remove(struct ceph_msg *msg)
415{
416 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500417 BUG_ON(msg->con == NULL);
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 Elder38941f82012-06-01 14:56:43 -0500439 BUG_ON(con->in_msg->con != con);
440 con->in_msg->con = NULL;
Sage Weilcf3e5c42009-12-11 09:48:05 -0800441 ceph_msg_put(con->in_msg);
442 con->in_msg = NULL;
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 Eldera5988c42012-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 Eldera5988c42012-05-29 11:04:58 -0500464 set_bit(CLOSED, &con->state);
465
Alex Elder928443c2012-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 Eldera5988c42012-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 Weil91e45ce32010-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 Eldera5988c42012-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 Elder1bfd89f2012-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 Elder1bfd89f2012-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 Elderce2c8902012-05-22 22:15:49 -0500539
540 con_sock_state_init(con);
541
Alex Elder1bfd89f2012-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 Eldera5988c42012-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 Eldere2200422012-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 Eldere2200422012-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 Eldere2200422012-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 Eldere2200422012-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 Eldere2200422012-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 Elder38941f82012-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 Elder38941f82012-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 }
Sage Weil31b80062009-10-06 11:31:13 -0700650
651 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
652 m, con->out_seq, le16_to_cpu(m->hdr.type),
653 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
654 le32_to_cpu(m->hdr.data_len),
655 m->nr_pages);
656 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
657
658 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -0500659 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
660 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
661 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -0600662
Sage Weil31b80062009-10-06 11:31:13 -0700663 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -0500664 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -0600665 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -0700666
667 /* fill in crc (except data pages), footer */
Alex Eldera9a0c512012-02-15 07:43:54 -0600668 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
669 con->out_msg->hdr.crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -0700670 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
Alex Eldera9a0c512012-02-15 07:43:54 -0600671
672 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
673 con->out_msg->footer.front_crc = cpu_to_le32(crc);
674 if (m->middle) {
675 crc = crc32c(0, m->middle->vec.iov_base,
676 m->middle->vec.iov_len);
677 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
678 } else
Sage Weil31b80062009-10-06 11:31:13 -0700679 con->out_msg->footer.middle_crc = 0;
680 con->out_msg->footer.data_crc = 0;
681 dout("prepare_write_message front_crc %u data_crc %u\n",
682 le32_to_cpu(con->out_msg->footer.front_crc),
683 le32_to_cpu(con->out_msg->footer.middle_crc));
684
685 /* is there a data payload? */
686 if (le32_to_cpu(m->hdr.data_len) > 0) {
687 /* initialize page iterator */
688 con->out_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700689 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -0800690 con->out_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700691 else
692 con->out_msg_pos.page_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700693 con->out_msg_pos.data_pos = 0;
Alex Elderbca064d2012-02-15 07:43:54 -0600694 con->out_msg_pos.did_page_crc = false;
Sage Weil31b80062009-10-06 11:31:13 -0700695 con->out_more = 1; /* data + footer will follow */
696 } else {
697 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -0600698 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700699 }
700
Alex Elder928443c2012-05-22 11:41:43 -0500701 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700702}
703
704/*
705 * Prepare an ack.
706 */
707static void prepare_write_ack(struct ceph_connection *con)
708{
709 dout("prepare_write_ack %p %llu -> %llu\n", con,
710 con->in_seq_acked, con->in_seq);
711 con->in_seq_acked = con->in_seq;
712
Alex Eldere2200422012-05-23 14:35:23 -0500713 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -0600714
Alex Eldere2200422012-05-23 14:35:23 -0500715 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -0600716
Sage Weil31b80062009-10-06 11:31:13 -0700717 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -0500718 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -0600719 &con->out_temp_ack);
720
Sage Weil31b80062009-10-06 11:31:13 -0700721 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elder928443c2012-05-22 11:41:43 -0500722 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700723}
724
725/*
726 * Prepare to write keepalive byte.
727 */
728static void prepare_write_keepalive(struct ceph_connection *con)
729{
730 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -0500731 con_out_kvec_reset(con);
732 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
Alex Elder928443c2012-05-22 11:41:43 -0500733 set_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -0700734}
735
736/*
737 * Connection negotiation.
738 */
739
Alex Elderdac1e712012-05-16 15:16:39 -0500740static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
741 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800742{
Alex Eldera3530df2012-05-16 15:16:39 -0500743 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -0500744
745 if (!con->ops->get_authorizer) {
746 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
747 con->out_connect.authorizer_len = 0;
748
Alex Elder729796b2012-05-16 15:16:39 -0500749 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -0500750 }
751
752 /* Can't hold the mutex while getting authorizer */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800753
Sage Weilec302642009-12-22 10:43:42 -0800754 mutex_unlock(&con->mutex);
Alex Elderb1c6b982012-05-16 15:16:38 -0500755
Alex Elderdac1e712012-05-16 15:16:39 -0500756 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Elder8f43fb52012-05-16 15:16:39 -0500757
Sage Weilec302642009-12-22 10:43:42 -0800758 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800759
Alex Eldera3530df2012-05-16 15:16:39 -0500760 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -0500761 return auth;
Alex Elder928443c2012-05-22 11:41:43 -0500762 if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags))
Alex Elder729796b2012-05-16 15:16:39 -0500763 return ERR_PTR(-EAGAIN);
Sage Weil0da5d702011-05-19 11:21:05 -0700764
Alex Elder8f43fb52012-05-16 15:16:39 -0500765 con->auth_reply_buf = auth->authorizer_reply_buf;
766 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
767
Alex Elder859eb792012-02-14 14:05:33 -0600768
Alex Elder729796b2012-05-16 15:16:39 -0500769 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800770}
771
Sage Weil31b80062009-10-06 11:31:13 -0700772/*
773 * We connected to a peer and are saying hello.
774 */
Alex Eldere825a662012-05-16 15:16:38 -0500775static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700776{
Alex Eldere2200422012-05-23 14:35:23 -0500777 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
778 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -0500779 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -0800780
Sage Weileed0ef22009-11-10 14:34:36 -0800781 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500782 set_bit(WRITE_PENDING, &con->flags);
Sage Weileed0ef22009-11-10 14:34:36 -0800783}
784
Alex Eldere825a662012-05-16 15:16:38 -0500785static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -0800786{
Sage Weil31b80062009-10-06 11:31:13 -0700787 unsigned global_seq = get_global_seq(con->msgr, 0);
788 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -0500789 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -0500790 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -0700791
792 switch (con->peer_name.type) {
793 case CEPH_ENTITY_TYPE_MON:
794 proto = CEPH_MONC_PROTOCOL;
795 break;
796 case CEPH_ENTITY_TYPE_OSD:
797 proto = CEPH_OSDC_PROTOCOL;
798 break;
799 case CEPH_ENTITY_TYPE_MDS:
800 proto = CEPH_MDSC_PROTOCOL;
801 break;
802 default:
803 BUG();
804 }
805
806 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
807 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800808
Alex Eldere825a662012-05-16 15:16:38 -0500809 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700810 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
811 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
812 con->out_connect.global_seq = cpu_to_le32(global_seq);
813 con->out_connect.protocol_version = cpu_to_le32(proto);
814 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700815
Alex Elderdac1e712012-05-16 15:16:39 -0500816 auth_proto = CEPH_AUTH_UNKNOWN;
817 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -0500818 if (IS_ERR(auth))
819 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -0500820
Alex Elderdac1e712012-05-16 15:16:39 -0500821 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -0500822 con->out_connect.authorizer_len = auth ?
823 cpu_to_le32(auth->authorizer_buf_len) : 0;
824
Alex Eldere2200422012-05-23 14:35:23 -0500825 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -0500826 &con->out_connect);
827 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -0500828 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -0500829 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -0600830
Sage Weil31b80062009-10-06 11:31:13 -0700831 con->out_more = 0;
Alex Elder928443c2012-05-22 11:41:43 -0500832 set_bit(WRITE_PENDING, &con->flags);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800833
Alex Eldere10c7582012-05-16 15:16:38 -0500834 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700835}
836
Sage Weil31b80062009-10-06 11:31:13 -0700837/*
838 * write as much of pending kvecs to the socket as we can.
839 * 1 -> done
840 * 0 -> socket full, but more to do
841 * <0 -> error
842 */
843static int write_partial_kvec(struct ceph_connection *con)
844{
845 int ret;
846
847 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
848 while (con->out_kvec_bytes > 0) {
849 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
850 con->out_kvec_left, con->out_kvec_bytes,
851 con->out_more);
852 if (ret <= 0)
853 goto out;
854 con->out_kvec_bytes -= ret;
855 if (con->out_kvec_bytes == 0)
856 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -0600857
858 /* account for full iov entries consumed */
859 while (ret >= con->out_kvec_cur->iov_len) {
860 BUG_ON(!con->out_kvec_left);
861 ret -= con->out_kvec_cur->iov_len;
862 con->out_kvec_cur++;
863 con->out_kvec_left--;
864 }
865 /* and for a partially-consumed entry */
866 if (ret) {
867 con->out_kvec_cur->iov_len -= ret;
868 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -0700869 }
870 }
871 con->out_kvec_left = 0;
872 con->out_kvec_is_msg = false;
873 ret = 1;
874out:
875 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
876 con->out_kvec_bytes, con->out_kvec_left, ret);
877 return ret; /* done! */
878}
879
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700880#ifdef CONFIG_BLOCK
881static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
882{
883 if (!bio) {
884 *iter = NULL;
885 *seg = 0;
886 return;
887 }
888 *iter = bio;
889 *seg = bio->bi_idx;
890}
891
892static void iter_bio_next(struct bio **bio_iter, int *seg)
893{
894 if (*bio_iter == NULL)
895 return;
896
897 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
898
899 (*seg)++;
900 if (*seg == (*bio_iter)->bi_vcnt)
901 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
902}
903#endif
904
Sage Weil31b80062009-10-06 11:31:13 -0700905/*
906 * Write as much message data payload as we can. If we finish, queue
907 * up the footer.
908 * 1 -> done, footer is now queued in out_kvec[].
909 * 0 -> socket full, but more to do
910 * <0 -> error
911 */
912static int write_partial_msg_pages(struct ceph_connection *con)
913{
914 struct ceph_msg *msg = con->out_msg;
915 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
916 size_t len;
Alex Elder37675b02012-03-07 11:40:08 -0600917 bool do_datacrc = !con->msgr->nocrc;
Sage Weil31b80062009-10-06 11:31:13 -0700918 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700919 int total_max_write;
920 int in_trail = 0;
921 size_t trail_len = (msg->trail ? msg->trail->length : 0);
Sage Weil31b80062009-10-06 11:31:13 -0700922
923 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
924 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
925 con->out_msg_pos.page_pos);
926
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700927#ifdef CONFIG_BLOCK
928 if (msg->bio && !msg->bio_iter)
929 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
930#endif
931
932 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -0700933 struct page *page = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700934 int max_write = PAGE_SIZE;
Alex Elder9bd19662012-03-07 11:40:08 -0600935 int bio_offset = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700936
937 total_max_write = data_len - trail_len -
938 con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -0700939
940 /*
941 * if we are calculating the data crc (the default), we need
942 * to map the page. if our pages[] has been revoked, use the
943 * zero page.
944 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700945
946 /* have we reached the trail part of the data? */
947 if (con->out_msg_pos.data_pos >= data_len - trail_len) {
948 in_trail = 1;
949
950 total_max_write = data_len - con->out_msg_pos.data_pos;
951
952 page = list_first_entry(&msg->trail->head,
953 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700954 max_write = PAGE_SIZE;
955 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -0700956 page = msg->pages[con->out_msg_pos.page];
Sage Weil58bb3b32009-12-23 12:12:31 -0800957 } else if (msg->pagelist) {
958 page = list_first_entry(&msg->pagelist->head,
959 struct page, lru);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700960#ifdef CONFIG_BLOCK
961 } else if (msg->bio) {
962 struct bio_vec *bv;
963
964 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
965 page = bv->bv_page;
Alex Elder9bd19662012-03-07 11:40:08 -0600966 bio_offset = bv->bv_offset;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700967 max_write = bv->bv_len;
968#endif
Sage Weil31b80062009-10-06 11:31:13 -0700969 } else {
Alex Elder57666512012-01-23 15:49:27 -0600970 page = zero_page;
Sage Weil31b80062009-10-06 11:31:13 -0700971 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700972 len = min_t(int, max_write - con->out_msg_pos.page_pos,
973 total_max_write);
974
Alex Elder37675b02012-03-07 11:40:08 -0600975 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
Alex Elder9bd19662012-03-07 11:40:08 -0600976 void *base;
Alex Eldera9a0c512012-02-15 07:43:54 -0600977 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -0700978 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
Alex Elder8d63e312012-03-07 11:40:08 -0600979 char *kaddr;
Sage Weil31b80062009-10-06 11:31:13 -0700980
Alex Elder8d63e312012-03-07 11:40:08 -0600981 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -0700982 BUG_ON(kaddr == NULL);
Alex Elder9bd19662012-03-07 11:40:08 -0600983 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
Alex Eldera9a0c512012-02-15 07:43:54 -0600984 crc = crc32c(tmpcrc, base, len);
985 con->out_msg->footer.data_crc = cpu_to_le32(crc);
Alex Elderbca064d2012-02-15 07:43:54 -0600986 con->out_msg_pos.did_page_crc = true;
Sage Weil31b80062009-10-06 11:31:13 -0700987 }
Alex Eldere36b13c2012-03-07 11:40:08 -0600988 ret = ceph_tcp_sendpage(con->sock, page,
Alex Elder9bd19662012-03-07 11:40:08 -0600989 con->out_msg_pos.page_pos + bio_offset,
Alex Eldere36b13c2012-03-07 11:40:08 -0600990 len, 1);
Sage Weil31b80062009-10-06 11:31:13 -0700991
Alex Elder0cdf9e62012-03-07 11:40:08 -0600992 if (do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -0700993 kunmap(page);
994
995 if (ret <= 0)
996 goto out;
997
998 con->out_msg_pos.data_pos += ret;
999 con->out_msg_pos.page_pos += ret;
1000 if (ret == len) {
1001 con->out_msg_pos.page_pos = 0;
1002 con->out_msg_pos.page++;
Alex Elderbca064d2012-02-15 07:43:54 -06001003 con->out_msg_pos.did_page_crc = false;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001004 if (in_trail)
1005 list_move_tail(&page->lru,
1006 &msg->trail->head);
1007 else if (msg->pagelist)
Sage Weil58bb3b32009-12-23 12:12:31 -08001008 list_move_tail(&page->lru,
1009 &msg->pagelist->head);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001010#ifdef CONFIG_BLOCK
1011 else if (msg->bio)
1012 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
1013#endif
Sage Weil31b80062009-10-06 11:31:13 -07001014 }
1015 }
1016
1017 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1018
1019 /* prepare and queue up footer, too */
Alex Elder37675b02012-03-07 11:40:08 -06001020 if (!do_datacrc)
Sage Weil31b80062009-10-06 11:31:13 -07001021 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001022 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001023 prepare_write_message_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07001024 ret = 1;
1025out:
1026 return ret;
1027}
1028
1029/*
1030 * write some zeros
1031 */
1032static int write_partial_skip(struct ceph_connection *con)
1033{
1034 int ret;
1035
1036 while (con->out_skip > 0) {
Alex Elder31739132012-03-07 11:40:08 -06001037 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001038
Alex Elder31739132012-03-07 11:40:08 -06001039 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
Sage Weil31b80062009-10-06 11:31:13 -07001040 if (ret <= 0)
1041 goto out;
1042 con->out_skip -= ret;
1043 }
1044 ret = 1;
1045out:
1046 return ret;
1047}
1048
1049/*
1050 * Prepare to read connection handshake, or an ack.
1051 */
Sage Weileed0ef22009-11-10 14:34:36 -08001052static void prepare_read_banner(struct ceph_connection *con)
1053{
1054 dout("prepare_read_banner %p\n", con);
1055 con->in_base_pos = 0;
1056}
1057
Sage Weil31b80062009-10-06 11:31:13 -07001058static void prepare_read_connect(struct ceph_connection *con)
1059{
1060 dout("prepare_read_connect %p\n", con);
1061 con->in_base_pos = 0;
1062}
1063
1064static void prepare_read_ack(struct ceph_connection *con)
1065{
1066 dout("prepare_read_ack %p\n", con);
1067 con->in_base_pos = 0;
1068}
1069
1070static void prepare_read_tag(struct ceph_connection *con)
1071{
1072 dout("prepare_read_tag %p\n", con);
1073 con->in_base_pos = 0;
1074 con->in_tag = CEPH_MSGR_TAG_READY;
1075}
1076
1077/*
1078 * Prepare to read a message.
1079 */
1080static int prepare_read_message(struct ceph_connection *con)
1081{
1082 dout("prepare_read_message %p\n", con);
1083 BUG_ON(con->in_msg != NULL);
1084 con->in_base_pos = 0;
1085 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1086 return 0;
1087}
1088
1089
1090static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001091 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001092{
Alex Eldere6cee712012-05-10 10:29:50 -05001093 while (con->in_base_pos < end) {
1094 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001095 int have = size - left;
1096 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1097 if (ret <= 0)
1098 return ret;
1099 con->in_base_pos += ret;
1100 }
1101 return 1;
1102}
1103
1104
1105/*
1106 * Read all or part of the connect-side handshake on a new connection
1107 */
Sage Weileed0ef22009-11-10 14:34:36 -08001108static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001109{
Alex Elderfd516532012-05-10 10:29:50 -05001110 int size;
1111 int end;
1112 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001113
Sage Weileed0ef22009-11-10 14:34:36 -08001114 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001115
1116 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001117 size = strlen(CEPH_BANNER);
1118 end = size;
1119 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001120 if (ret <= 0)
1121 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001122
1123 size = sizeof (con->actual_peer_addr);
1124 end += size;
1125 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001126 if (ret <= 0)
1127 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001128
1129 size = sizeof (con->peer_addr_for_me);
1130 end += size;
1131 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001132 if (ret <= 0)
1133 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001134
Sage Weileed0ef22009-11-10 14:34:36 -08001135out:
1136 return ret;
1137}
1138
1139static int read_partial_connect(struct ceph_connection *con)
1140{
Alex Elderfd516532012-05-10 10:29:50 -05001141 int size;
1142 int end;
1143 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001144
1145 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1146
Alex Elderfd516532012-05-10 10:29:50 -05001147 size = sizeof (con->in_reply);
1148 end = size;
1149 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001150 if (ret <= 0)
1151 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001152
1153 size = le32_to_cpu(con->in_reply.authorizer_len);
1154 end += size;
1155 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001156 if (ret <= 0)
1157 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001158
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001159 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1160 con, (int)con->in_reply.tag,
1161 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001162 le32_to_cpu(con->in_reply.global_seq));
1163out:
1164 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001165
Sage Weil31b80062009-10-06 11:31:13 -07001166}
1167
1168/*
1169 * Verify the hello banner looks okay.
1170 */
1171static int verify_hello(struct ceph_connection *con)
1172{
1173 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001174 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001175 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001176 con->error_msg = "protocol error, bad banner";
1177 return -1;
1178 }
1179 return 0;
1180}
1181
1182static bool addr_is_blank(struct sockaddr_storage *ss)
1183{
1184 switch (ss->ss_family) {
1185 case AF_INET:
1186 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1187 case AF_INET6:
1188 return
1189 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1190 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1191 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1192 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1193 }
1194 return false;
1195}
1196
1197static int addr_port(struct sockaddr_storage *ss)
1198{
1199 switch (ss->ss_family) {
1200 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001201 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001202 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001203 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001204 }
1205 return 0;
1206}
1207
1208static void addr_set_port(struct sockaddr_storage *ss, int p)
1209{
1210 switch (ss->ss_family) {
1211 case AF_INET:
1212 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001213 break;
Sage Weil31b80062009-10-06 11:31:13 -07001214 case AF_INET6:
1215 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001216 break;
Sage Weil31b80062009-10-06 11:31:13 -07001217 }
1218}
1219
1220/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001221 * Unlike other *_pton function semantics, zero indicates success.
1222 */
1223static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1224 char delim, const char **ipend)
1225{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001226 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1227 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001228
1229 memset(ss, 0, sizeof(*ss));
1230
1231 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1232 ss->ss_family = AF_INET;
1233 return 0;
1234 }
1235
1236 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1237 ss->ss_family = AF_INET6;
1238 return 0;
1239 }
1240
1241 return -EINVAL;
1242}
1243
1244/*
1245 * Extract hostname string and resolve using kernel DNS facility.
1246 */
1247#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1248static int ceph_dns_resolve_name(const char *name, size_t namelen,
1249 struct sockaddr_storage *ss, char delim, const char **ipend)
1250{
1251 const char *end, *delim_p;
1252 char *colon_p, *ip_addr = NULL;
1253 int ip_len, ret;
1254
1255 /*
1256 * The end of the hostname occurs immediately preceding the delimiter or
1257 * the port marker (':') where the delimiter takes precedence.
1258 */
1259 delim_p = memchr(name, delim, namelen);
1260 colon_p = memchr(name, ':', namelen);
1261
1262 if (delim_p && colon_p)
1263 end = delim_p < colon_p ? delim_p : colon_p;
1264 else if (!delim_p && colon_p)
1265 end = colon_p;
1266 else {
1267 end = delim_p;
1268 if (!end) /* case: hostname:/ */
1269 end = name + namelen;
1270 }
1271
1272 if (end <= name)
1273 return -EINVAL;
1274
1275 /* do dns_resolve upcall */
1276 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1277 if (ip_len > 0)
1278 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1279 else
1280 ret = -ESRCH;
1281
1282 kfree(ip_addr);
1283
1284 *ipend = end;
1285
1286 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1287 ret, ret ? "failed" : ceph_pr_addr(ss));
1288
1289 return ret;
1290}
1291#else
1292static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1293 struct sockaddr_storage *ss, char delim, const char **ipend)
1294{
1295 return -EINVAL;
1296}
1297#endif
1298
1299/*
1300 * Parse a server name (IP or hostname). If a valid IP address is not found
1301 * then try to extract a hostname to resolve using userspace DNS upcall.
1302 */
1303static int ceph_parse_server_name(const char *name, size_t namelen,
1304 struct sockaddr_storage *ss, char delim, const char **ipend)
1305{
1306 int ret;
1307
1308 ret = ceph_pton(name, namelen, ss, delim, ipend);
1309 if (ret)
1310 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1311
1312 return ret;
1313}
1314
1315/*
Sage Weil31b80062009-10-06 11:31:13 -07001316 * Parse an ip[:port] list into an addr array. Use the default
1317 * monitor port if a port isn't specified.
1318 */
1319int ceph_parse_ips(const char *c, const char *end,
1320 struct ceph_entity_addr *addr,
1321 int max_count, int *count)
1322{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001323 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001324 const char *p = c;
1325
1326 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1327 for (i = 0; i < max_count; i++) {
1328 const char *ipend;
1329 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001330 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001331 char delim = ',';
1332
1333 if (*p == '[') {
1334 delim = ']';
1335 p++;
1336 }
Sage Weil31b80062009-10-06 11:31:13 -07001337
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001338 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1339 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001340 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001341 ret = -EINVAL;
1342
Sage Weil31b80062009-10-06 11:31:13 -07001343 p = ipend;
1344
Sage Weil39139f62010-07-08 09:54:52 -07001345 if (delim == ']') {
1346 if (*p != ']') {
1347 dout("missing matching ']'\n");
1348 goto bad;
1349 }
1350 p++;
1351 }
1352
Sage Weil31b80062009-10-06 11:31:13 -07001353 /* port? */
1354 if (p < end && *p == ':') {
1355 port = 0;
1356 p++;
1357 while (p < end && *p >= '0' && *p <= '9') {
1358 port = (port * 10) + (*p - '0');
1359 p++;
1360 }
1361 if (port > 65535 || port == 0)
1362 goto bad;
1363 } else {
1364 port = CEPH_MON_PORT;
1365 }
1366
1367 addr_set_port(ss, port);
1368
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001369 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001370
1371 if (p == end)
1372 break;
1373 if (*p != ',')
1374 goto bad;
1375 p++;
1376 }
1377
1378 if (p != end)
1379 goto bad;
1380
1381 if (count)
1382 *count = i + 1;
1383 return 0;
1384
1385bad:
Sage Weil39139f62010-07-08 09:54:52 -07001386 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001387 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001388}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001389EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001390
Sage Weileed0ef22009-11-10 14:34:36 -08001391static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001392{
Sage Weileed0ef22009-11-10 14:34:36 -08001393 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001394
1395 if (verify_hello(con) < 0)
1396 return -1;
1397
Sage Weil63f2d212009-11-03 15:17:56 -08001398 ceph_decode_addr(&con->actual_peer_addr);
1399 ceph_decode_addr(&con->peer_addr_for_me);
1400
Sage Weil31b80062009-10-06 11:31:13 -07001401 /*
1402 * Make sure the other end is who we wanted. note that the other
1403 * end may not yet know their ip address, so if it's 0.0.0.0, give
1404 * them the benefit of the doubt.
1405 */
Sage Weil103e2d32010-01-07 16:12:36 -08001406 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1407 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001408 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1409 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001410 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001411 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001412 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001413 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001414 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001415 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001416 return -1;
1417 }
1418
1419 /*
1420 * did we learn our address?
1421 */
1422 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1423 int port = addr_port(&con->msgr->inst.addr.in_addr);
1424
1425 memcpy(&con->msgr->inst.addr.in_addr,
1426 &con->peer_addr_for_me.in_addr,
1427 sizeof(con->peer_addr_for_me.in_addr));
1428 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001429 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001430 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001431 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001432 }
1433
Sage Weileed0ef22009-11-10 14:34:36 -08001434 set_bit(NEGOTIATING, &con->state);
1435 prepare_read_connect(con);
1436 return 0;
1437}
1438
Sage Weil04a419f2009-12-23 09:30:21 -08001439static void fail_protocol(struct ceph_connection *con)
1440{
1441 reset_connection(con);
1442 set_bit(CLOSED, &con->state); /* in case there's queued work */
Sage Weil04a419f2009-12-23 09:30:21 -08001443}
1444
Sage Weileed0ef22009-11-10 14:34:36 -08001445static int process_connect(struct ceph_connection *con)
1446{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001447 u64 sup_feat = con->msgr->supported_features;
1448 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001449 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001450 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001451
Sage Weileed0ef22009-11-10 14:34:36 -08001452 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1453
Sage Weil31b80062009-10-06 11:31:13 -07001454 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001455 case CEPH_MSGR_TAG_FEATURES:
1456 pr_err("%s%lld %s feature set mismatch,"
1457 " my %llx < server's %llx, missing %llx\n",
1458 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001459 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001460 sup_feat, server_feat, server_feat & ~sup_feat);
1461 con->error_msg = "missing required protocol features";
1462 fail_protocol(con);
1463 return -1;
1464
Sage Weil31b80062009-10-06 11:31:13 -07001465 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001466 pr_err("%s%lld %s protocol version mismatch,"
1467 " my %d != server's %d\n",
1468 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001469 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001470 le32_to_cpu(con->out_connect.protocol_version),
1471 le32_to_cpu(con->in_reply.protocol_version));
1472 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001473 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001474 return -1;
1475
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001476 case CEPH_MSGR_TAG_BADAUTHORIZER:
1477 con->auth_retry++;
1478 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1479 con->auth_retry);
1480 if (con->auth_retry == 2) {
1481 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001482 return -1;
1483 }
1484 con->auth_retry = 1;
Alex Eldere2200422012-05-23 14:35:23 -05001485 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001486 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07001487 if (ret < 0)
1488 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001489 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001490 break;
Sage Weil31b80062009-10-06 11:31:13 -07001491
1492 case CEPH_MSGR_TAG_RESETSESSION:
1493 /*
1494 * If we connected with a large connect_seq but the peer
1495 * has no record of a session with us (no connection, or
1496 * connect_seq == 0), they will send RESETSESION to indicate
1497 * that they must have reset their session, and may have
1498 * dropped messages.
1499 */
1500 dout("process_connect got RESET peer seq %u\n",
1501 le32_to_cpu(con->in_connect.connect_seq));
1502 pr_err("%s%lld %s connection reset\n",
1503 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001504 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001505 reset_connection(con);
Alex Eldere2200422012-05-23 14:35:23 -05001506 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001507 ret = prepare_write_connect(con);
1508 if (ret < 0)
1509 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001510 prepare_read_connect(con);
1511
1512 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001513 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001514 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1515 if (con->ops->peer_reset)
1516 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001517 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07001518 if (test_bit(CLOSED, &con->state) ||
1519 test_bit(OPENING, &con->state))
1520 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001521 break;
1522
1523 case CEPH_MSGR_TAG_RETRY_SESSION:
1524 /*
1525 * If we sent a smaller connect_seq than the peer has, try
1526 * again with a larger value.
1527 */
1528 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1529 le32_to_cpu(con->out_connect.connect_seq),
1530 le32_to_cpu(con->in_connect.connect_seq));
1531 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Alex Eldere2200422012-05-23 14:35:23 -05001532 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001533 ret = prepare_write_connect(con);
1534 if (ret < 0)
1535 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001536 prepare_read_connect(con);
1537 break;
1538
1539 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1540 /*
1541 * If we sent a smaller global_seq than the peer has, try
1542 * again with a larger value.
1543 */
Sage Weileed0ef22009-11-10 14:34:36 -08001544 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001545 con->peer_global_seq,
1546 le32_to_cpu(con->in_connect.global_seq));
1547 get_global_seq(con->msgr,
1548 le32_to_cpu(con->in_connect.global_seq));
Alex Eldere2200422012-05-23 14:35:23 -05001549 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001550 ret = prepare_write_connect(con);
1551 if (ret < 0)
1552 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001553 prepare_read_connect(con);
1554 break;
1555
1556 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001557 if (req_feat & ~server_feat) {
1558 pr_err("%s%lld %s protocol feature mismatch,"
1559 " my required %llx > server's %llx, need %llx\n",
1560 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001561 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001562 req_feat, server_feat, req_feat & ~server_feat);
1563 con->error_msg = "missing required protocol features";
1564 fail_protocol(con);
1565 return -1;
1566 }
Sage Weil31b80062009-10-06 11:31:13 -07001567 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001568 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1569 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001570 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001571 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1572 con->peer_global_seq,
1573 le32_to_cpu(con->in_reply.connect_seq),
1574 con->connect_seq);
1575 WARN_ON(con->connect_seq !=
1576 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001577
1578 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elder928443c2012-05-22 11:41:43 -05001579 set_bit(LOSSYTX, &con->flags);
Sage Weil92ac41d2009-12-14 14:56:56 -08001580
Sage Weil31b80062009-10-06 11:31:13 -07001581 prepare_read_tag(con);
1582 break;
1583
1584 case CEPH_MSGR_TAG_WAIT:
1585 /*
1586 * If there is a connection race (we are opening
1587 * connections to each other), one of us may just have
1588 * to WAIT. This shouldn't happen if we are the
1589 * client.
1590 */
Sage Weil04177882011-05-12 15:33:17 -07001591 pr_err("process_connect got WAIT as client\n");
1592 con->error_msg = "protocol error, got WAIT as client";
1593 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001594
1595 default:
1596 pr_err("connect protocol error, will retry\n");
1597 con->error_msg = "protocol error, garbage tag during connect";
1598 return -1;
1599 }
1600 return 0;
1601}
1602
1603
1604/*
1605 * read (part of) an ack
1606 */
1607static int read_partial_ack(struct ceph_connection *con)
1608{
Alex Elderfd516532012-05-10 10:29:50 -05001609 int size = sizeof (con->in_temp_ack);
1610 int end = size;
1611
1612 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001613}
1614
1615
1616/*
1617 * We can finally discard anything that's been acked.
1618 */
1619static void process_ack(struct ceph_connection *con)
1620{
1621 struct ceph_msg *m;
1622 u64 ack = le64_to_cpu(con->in_temp_ack);
1623 u64 seq;
1624
Sage Weil31b80062009-10-06 11:31:13 -07001625 while (!list_empty(&con->out_sent)) {
1626 m = list_first_entry(&con->out_sent, struct ceph_msg,
1627 list_head);
1628 seq = le64_to_cpu(m->hdr.seq);
1629 if (seq > ack)
1630 break;
1631 dout("got ack for seq %llu type %d at %p\n", seq,
1632 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001633 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001634 ceph_msg_remove(m);
1635 }
Sage Weil31b80062009-10-06 11:31:13 -07001636 prepare_read_tag(con);
1637}
1638
1639
1640
1641
Yehuda Sadeh24504182010-01-08 13:58:34 -08001642static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001643 struct kvec *section,
1644 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001645{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001646 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001647
Yehuda Sadeh24504182010-01-08 13:58:34 -08001648 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001649
Yehuda Sadeh24504182010-01-08 13:58:34 -08001650 while (section->iov_len < sec_len) {
1651 BUG_ON(section->iov_base == NULL);
1652 left = sec_len - section->iov_len;
1653 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1654 section->iov_len, left);
1655 if (ret <= 0)
1656 return ret;
1657 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001658 }
Alex Elderfe3ad592012-02-15 07:43:54 -06001659 if (section->iov_len == sec_len)
1660 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001661
1662 return 1;
1663}
1664
Alex Elder1c20f2d2012-06-04 14:43:32 -05001665static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
1666 struct ceph_msg_header *hdr);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001667
1668
1669static int read_partial_message_pages(struct ceph_connection *con,
1670 struct page **pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001671 unsigned data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001672{
1673 void *p;
1674 int ret;
1675 int left;
1676
1677 left = min((int)(data_len - con->in_msg_pos.data_pos),
1678 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1679 /* (page) data */
1680 BUG_ON(pages == NULL);
1681 p = kmap(pages[con->in_msg_pos.page]);
1682 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1683 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001684 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001685 con->in_data_crc =
1686 crc32c(con->in_data_crc,
1687 p + con->in_msg_pos.page_pos, ret);
1688 kunmap(pages[con->in_msg_pos.page]);
1689 if (ret <= 0)
1690 return ret;
1691 con->in_msg_pos.data_pos += ret;
1692 con->in_msg_pos.page_pos += ret;
1693 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1694 con->in_msg_pos.page_pos = 0;
1695 con->in_msg_pos.page++;
1696 }
1697
1698 return ret;
1699}
1700
1701#ifdef CONFIG_BLOCK
1702static int read_partial_message_bio(struct ceph_connection *con,
1703 struct bio **bio_iter, int *bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001704 unsigned data_len, bool do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001705{
1706 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1707 void *p;
1708 int ret, left;
1709
1710 if (IS_ERR(bv))
1711 return PTR_ERR(bv);
1712
1713 left = min((int)(data_len - con->in_msg_pos.data_pos),
1714 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1715
1716 p = kmap(bv->bv_page) + bv->bv_offset;
1717
1718 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1719 left);
Alex Elderbca064d2012-02-15 07:43:54 -06001720 if (ret > 0 && do_datacrc)
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001721 con->in_data_crc =
1722 crc32c(con->in_data_crc,
1723 p + con->in_msg_pos.page_pos, ret);
1724 kunmap(bv->bv_page);
1725 if (ret <= 0)
1726 return ret;
1727 con->in_msg_pos.data_pos += ret;
1728 con->in_msg_pos.page_pos += ret;
1729 if (con->in_msg_pos.page_pos == bv->bv_len) {
1730 con->in_msg_pos.page_pos = 0;
1731 iter_bio_next(bio_iter, bio_seg);
1732 }
1733
1734 return ret;
1735}
1736#endif
1737
Sage Weil31b80062009-10-06 11:31:13 -07001738/*
1739 * read (part of) a message.
1740 */
1741static int read_partial_message(struct ceph_connection *con)
1742{
1743 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05001744 int size;
1745 int end;
Sage Weil31b80062009-10-06 11:31:13 -07001746 int ret;
Sage Weilc5c6b192010-11-09 12:40:00 -08001747 unsigned front_len, middle_len, data_len;
Alex Elder37675b02012-03-07 11:40:08 -06001748 bool do_datacrc = !con->msgr->nocrc;
Sage Weilae187562010-04-22 07:47:01 -07001749 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06001750 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001751
1752 dout("read_partial_message con %p msg %p\n", con, m);
1753
1754 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05001755 size = sizeof (con->in_hdr);
1756 end = size;
1757 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05001758 if (ret <= 0)
1759 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06001760
1761 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1762 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1763 pr_err("read_partial_message bad hdr "
1764 " crc %u != expected %u\n",
1765 crc, con->in_hdr.crc);
1766 return -EBADMSG;
1767 }
1768
Sage Weil31b80062009-10-06 11:31:13 -07001769 front_len = le32_to_cpu(con->in_hdr.front_len);
1770 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1771 return -EIO;
1772 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1773 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1774 return -EIO;
1775 data_len = le32_to_cpu(con->in_hdr.data_len);
1776 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1777 return -EIO;
1778
Sage Weilae187562010-04-22 07:47:01 -07001779 /* verify seq# */
1780 seq = le64_to_cpu(con->in_hdr.seq);
1781 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001782 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001783 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001784 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001785 seq, con->in_seq + 1);
1786 con->in_base_pos = -front_len - middle_len - data_len -
1787 sizeof(m->footer);
1788 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001789 return 0;
1790 } else if ((s64)seq - (s64)con->in_seq > 1) {
1791 pr_err("read_partial_message bad seq %lld expected %lld\n",
1792 seq, con->in_seq + 1);
1793 con->error_msg = "bad message sequence # for incoming message";
1794 return -EBADMSG;
1795 }
1796
Sage Weil31b80062009-10-06 11:31:13 -07001797 /* allocate message? */
1798 if (!con->in_msg) {
1799 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1800 con->in_hdr.front_len, con->in_hdr.data_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05001801 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
Sage Weil31b80062009-10-06 11:31:13 -07001802 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001803 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001804 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001805 con->in_base_pos = -front_len - middle_len - data_len -
1806 sizeof(m->footer);
1807 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001808 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001809 return 0;
1810 }
Sage Weila79832f2010-04-01 16:06:19 -07001811 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001812 con->error_msg =
1813 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001814 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001815 }
Alex Elder38941f82012-06-01 14:56:43 -05001816
1817 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07001818 m = con->in_msg;
1819 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001820 if (m->middle)
1821 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001822
1823 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001824 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001825 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001826 else
1827 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001828 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001829 }
1830
1831 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001832 ret = read_partial_message_section(con, &m->front, front_len,
1833 &con->in_front_crc);
1834 if (ret <= 0)
1835 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001836
1837 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001838 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001839 ret = read_partial_message_section(con, &m->middle->vec,
1840 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001841 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001842 if (ret <= 0)
1843 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001844 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001845#ifdef CONFIG_BLOCK
1846 if (m->bio && !m->bio_iter)
1847 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1848#endif
Sage Weil31b80062009-10-06 11:31:13 -07001849
1850 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001851 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001852 if (m->pages) {
1853 ret = read_partial_message_pages(con, m->pages,
Alex Elderbca064d2012-02-15 07:43:54 -06001854 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001855 if (ret <= 0)
1856 return ret;
1857#ifdef CONFIG_BLOCK
1858 } else if (m->bio) {
1859
1860 ret = read_partial_message_bio(con,
1861 &m->bio_iter, &m->bio_seg,
Alex Elderbca064d2012-02-15 07:43:54 -06001862 data_len, do_datacrc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001863 if (ret <= 0)
1864 return ret;
1865#endif
1866 } else {
1867 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001868 }
1869 }
1870
Sage Weil31b80062009-10-06 11:31:13 -07001871 /* footer */
Alex Elderfd516532012-05-10 10:29:50 -05001872 size = sizeof (m->footer);
1873 end += size;
1874 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05001875 if (ret <= 0)
1876 return ret;
1877
Sage Weil31b80062009-10-06 11:31:13 -07001878 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1879 m, front_len, m->footer.front_crc, middle_len,
1880 m->footer.middle_crc, data_len, m->footer.data_crc);
1881
1882 /* crc ok? */
1883 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1884 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1885 m, con->in_front_crc, m->footer.front_crc);
1886 return -EBADMSG;
1887 }
1888 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1889 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1890 m, con->in_middle_crc, m->footer.middle_crc);
1891 return -EBADMSG;
1892 }
Alex Elderbca064d2012-02-15 07:43:54 -06001893 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07001894 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1895 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1896 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1897 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1898 return -EBADMSG;
1899 }
1900
1901 return 1; /* done! */
1902}
1903
1904/*
1905 * Process message. This happens in the worker thread. The callback should
1906 * be careful not to do anything that waits on other incoming messages or it
1907 * may deadlock.
1908 */
1909static void process_message(struct ceph_connection *con)
1910{
Sage Weil5e095e82009-12-14 14:30:34 -08001911 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001912
Alex Elder38941f82012-06-01 14:56:43 -05001913 BUG_ON(con->in_msg->con != con);
1914 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08001915 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001916 con->in_msg = NULL;
1917
1918 /* if first message, set peer_name */
1919 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001920 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001921
Sage Weil31b80062009-10-06 11:31:13 -07001922 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001923 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001924
1925 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1926 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001927 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001928 le16_to_cpu(msg->hdr.type),
1929 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1930 le32_to_cpu(msg->hdr.front_len),
1931 le32_to_cpu(msg->hdr.data_len),
1932 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1933 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001934
1935 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001936 prepare_read_tag(con);
1937}
1938
1939
1940/*
1941 * Write something to the socket. Called in a worker thread when the
1942 * socket appears to be writeable and we have something ready to send.
1943 */
1944static int try_write(struct ceph_connection *con)
1945{
Sage Weil31b80062009-10-06 11:31:13 -07001946 int ret = 1;
1947
1948 dout("try_write start %p state %lu nref %d\n", con, con->state,
1949 atomic_read(&con->nref));
1950
Sage Weil31b80062009-10-06 11:31:13 -07001951more:
1952 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1953
1954 /* open the socket first? */
1955 if (con->sock == NULL) {
Alex Eldera5988c42012-05-29 11:04:58 -05001956 clear_bit(NEGOTIATING, &con->state);
1957 set_bit(CONNECTING, &con->state);
1958
Alex Eldere2200422012-05-23 14:35:23 -05001959 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05001960 prepare_write_banner(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05001961 ret = prepare_write_connect(con);
1962 if (ret < 0)
1963 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08001964 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001965
Sage Weilcf3e5c42009-12-11 09:48:05 -08001966 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001967 con->in_tag = CEPH_MSGR_TAG_READY;
1968 dout("try_write initiating connect on %p new state %lu\n",
1969 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06001970 ret = ceph_tcp_connect(con);
1971 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07001972 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07001973 goto out;
1974 }
1975 }
1976
1977more_kvec:
1978 /* kvec data queued? */
1979 if (con->out_skip) {
1980 ret = write_partial_skip(con);
1981 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001982 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001983 }
1984 if (con->out_kvec_left) {
1985 ret = write_partial_kvec(con);
1986 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001987 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001988 }
1989
1990 /* msg pages? */
1991 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001992 if (con->out_msg_done) {
1993 ceph_msg_put(con->out_msg);
1994 con->out_msg = NULL; /* we're done with this one */
1995 goto do_next;
1996 }
1997
Sage Weil31b80062009-10-06 11:31:13 -07001998 ret = write_partial_msg_pages(con);
1999 if (ret == 1)
2000 goto more_kvec; /* we need to send the footer, too! */
2001 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002002 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002003 if (ret < 0) {
2004 dout("try_write write_partial_msg_pages err %d\n",
2005 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002006 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002007 }
2008 }
2009
Sage Weilc86a2932009-12-14 14:04:30 -08002010do_next:
Sage Weil31b80062009-10-06 11:31:13 -07002011 if (!test_bit(CONNECTING, &con->state)) {
2012 /* is anything else pending? */
2013 if (!list_empty(&con->out_queue)) {
2014 prepare_write_message(con);
2015 goto more;
2016 }
2017 if (con->in_seq > con->in_seq_acked) {
2018 prepare_write_ack(con);
2019 goto more;
2020 }
Alex Elder928443c2012-05-22 11:41:43 -05002021 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002022 prepare_write_keepalive(con);
2023 goto more;
2024 }
2025 }
2026
2027 /* Nothing to do! */
Alex Elder928443c2012-05-22 11:41:43 -05002028 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002029 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002030 ret = 0;
2031out:
Sage Weil42961d22011-01-25 08:19:34 -08002032 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002033 return ret;
2034}
2035
2036
2037
2038/*
2039 * Read what we can from the socket.
2040 */
2041static int try_read(struct ceph_connection *con)
2042{
Sage Weil31b80062009-10-06 11:31:13 -07002043 int ret = -1;
2044
2045 if (!con->sock)
2046 return 0;
2047
2048 if (test_bit(STANDBY, &con->state))
2049 return 0;
2050
2051 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08002052
Sage Weil31b80062009-10-06 11:31:13 -07002053more:
2054 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2055 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002056
2057 /*
2058 * process_connect and process_message drop and re-take
2059 * con->mutex. make sure we handle a racing close or reopen.
2060 */
2061 if (test_bit(CLOSED, &con->state) ||
2062 test_bit(OPENING, &con->state)) {
2063 ret = -EAGAIN;
2064 goto out;
2065 }
2066
Sage Weil31b80062009-10-06 11:31:13 -07002067 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08002068 if (!test_bit(NEGOTIATING, &con->state)) {
2069 dout("try_read connecting\n");
2070 ret = read_partial_banner(con);
2071 if (ret <= 0)
Sage Weileed0ef22009-11-10 14:34:36 -08002072 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002073 ret = process_banner(con);
2074 if (ret < 0)
2075 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08002076 }
Sage Weil31b80062009-10-06 11:31:13 -07002077 ret = read_partial_connect(con);
2078 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002079 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002080 ret = process_connect(con);
2081 if (ret < 0)
2082 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002083 goto more;
2084 }
2085
2086 if (con->in_base_pos < 0) {
2087 /*
2088 * skipping + discarding content.
2089 *
2090 * FIXME: there must be a better way to do this!
2091 */
Alex Elder84495f42012-02-15 07:43:55 -06002092 static char buf[SKIP_BUF_SIZE];
2093 int skip = min((int) sizeof (buf), -con->in_base_pos);
2094
Sage Weil31b80062009-10-06 11:31:13 -07002095 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2096 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2097 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002098 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002099 con->in_base_pos += ret;
2100 if (con->in_base_pos)
2101 goto more;
2102 }
2103 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2104 /*
2105 * what's next?
2106 */
2107 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2108 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002109 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002110 dout("try_read got tag %d\n", (int)con->in_tag);
2111 switch (con->in_tag) {
2112 case CEPH_MSGR_TAG_MSG:
2113 prepare_read_message(con);
2114 break;
2115 case CEPH_MSGR_TAG_ACK:
2116 prepare_read_ack(con);
2117 break;
2118 case CEPH_MSGR_TAG_CLOSE:
2119 set_bit(CLOSED, &con->state); /* fixme */
Sage Weil98bdb0a2011-01-25 08:17:48 -08002120 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002121 default:
2122 goto bad_tag;
2123 }
2124 }
2125 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2126 ret = read_partial_message(con);
2127 if (ret <= 0) {
2128 switch (ret) {
2129 case -EBADMSG:
2130 con->error_msg = "bad crc";
2131 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002132 break;
Sage Weil31b80062009-10-06 11:31:13 -07002133 case -EIO:
2134 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002135 break;
Sage Weil31b80062009-10-06 11:31:13 -07002136 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002137 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002138 }
2139 if (con->in_tag == CEPH_MSGR_TAG_READY)
2140 goto more;
2141 process_message(con);
2142 goto more;
2143 }
2144 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2145 ret = read_partial_ack(con);
2146 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002147 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002148 process_ack(con);
2149 goto more;
2150 }
2151
Sage Weil31b80062009-10-06 11:31:13 -07002152out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002153 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002154 return ret;
2155
2156bad_tag:
2157 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2158 con->error_msg = "protocol error, garbage tag";
2159 ret = -1;
2160 goto out;
2161}
2162
2163
2164/*
2165 * Atomically queue work on a connection. Bump @con reference to
2166 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002167 */
2168static void queue_con(struct ceph_connection *con)
2169{
Sage Weil31b80062009-10-06 11:31:13 -07002170 if (!con->ops->get(con)) {
2171 dout("queue_con %p ref count 0\n", con);
2172 return;
2173 }
2174
Tejun Heof363e452011-01-03 14:49:46 +01002175 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002176 dout("queue_con %p - already queued\n", con);
2177 con->ops->put(con);
2178 } else {
2179 dout("queue_con %p\n", con);
2180 }
2181}
2182
2183/*
2184 * Do some work on a connection. Drop a connection ref when we're done.
2185 */
2186static void con_work(struct work_struct *work)
2187{
2188 struct ceph_connection *con = container_of(work, struct ceph_connection,
2189 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002190 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002191
Sage Weil9dd46582010-04-28 13:51:50 -07002192 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002193restart:
Alex Elder928443c2012-05-22 11:41:43 -05002194 if (test_and_clear_bit(BACKOFF, &con->flags)) {
Sage Weil60bf8bf2011-03-04 12:24:28 -08002195 dout("con_work %p backing off\n", con);
2196 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2197 round_jiffies_relative(con->delay))) {
2198 dout("con_work %p backoff %lu\n", con, con->delay);
2199 mutex_unlock(&con->mutex);
2200 return;
2201 } else {
2202 con->ops->put(con);
2203 dout("con_work %p FAILED to back off %lu\n", con,
2204 con->delay);
2205 }
2206 }
Sage Weil9dd46582010-04-28 13:51:50 -07002207
Sage Weile00de342011-03-04 12:25:05 -08002208 if (test_bit(STANDBY, &con->state)) {
2209 dout("con_work %p STANDBY\n", con);
2210 goto done;
2211 }
Sage Weil31b80062009-10-06 11:31:13 -07002212 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2213 dout("con_work CLOSED\n");
2214 con_close_socket(con);
2215 goto done;
2216 }
2217 if (test_and_clear_bit(OPENING, &con->state)) {
2218 /* reopen w/ new peer */
2219 dout("con_work OPENING\n");
2220 con_close_socket(con);
2221 }
2222
Alex Elder928443c2012-05-22 11:41:43 -05002223 if (test_and_clear_bit(SOCK_CLOSED, &con->flags))
Sage Weil0da5d702011-05-19 11:21:05 -07002224 goto fault;
2225
2226 ret = try_read(con);
2227 if (ret == -EAGAIN)
2228 goto restart;
2229 if (ret < 0)
2230 goto fault;
2231
2232 ret = try_write(con);
2233 if (ret == -EAGAIN)
2234 goto restart;
2235 if (ret < 0)
2236 goto fault;
Sage Weil31b80062009-10-06 11:31:13 -07002237
2238done:
Sage Weil9dd46582010-04-28 13:51:50 -07002239 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002240done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002241 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002242 return;
2243
2244fault:
2245 mutex_unlock(&con->mutex);
2246 ceph_fault(con); /* error/fault path */
2247 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002248}
2249
2250
2251/*
2252 * Generic error/fault handler. A retry mechanism is used with
2253 * exponential backoff
2254 */
2255static void ceph_fault(struct ceph_connection *con)
2256{
2257 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002258 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002259 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002260 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002261
Alex Elder928443c2012-05-22 11:41:43 -05002262 if (test_bit(LOSSYTX, &con->flags)) {
Sage Weil31b80062009-10-06 11:31:13 -07002263 dout("fault on LOSSYTX channel\n");
2264 goto out;
2265 }
2266
Sage Weilec302642009-12-22 10:43:42 -08002267 mutex_lock(&con->mutex);
Sage Weil91e45ce32010-02-15 12:05:09 -08002268 if (test_bit(CLOSED, &con->state))
2269 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002270
Sage Weil31b80062009-10-06 11:31:13 -07002271 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002272
2273 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002274 BUG_ON(con->in_msg->con != con);
2275 con->in_msg->con = NULL;
Sage Weil5e095e82009-12-14 14:30:34 -08002276 ceph_msg_put(con->in_msg);
2277 con->in_msg = NULL;
2278 }
Sage Weil31b80062009-10-06 11:31:13 -07002279
Sage Weile80a52d2010-02-25 12:40:45 -08002280 /* Requeue anything that hasn't been acked */
2281 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002282
Sage Weile76661d2011-03-03 10:10:15 -08002283 /* If there are no messages queued or keepalive pending, place
2284 * the connection in a STANDBY state */
2285 if (list_empty(&con->out_queue) &&
Alex Elder928443c2012-05-22 11:41:43 -05002286 !test_bit(KEEPALIVE_PENDING, &con->flags)) {
Sage Weile00de342011-03-04 12:25:05 -08002287 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elder928443c2012-05-22 11:41:43 -05002288 clear_bit(WRITE_PENDING, &con->flags);
Sage Weil31b80062009-10-06 11:31:13 -07002289 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08002290 } else {
2291 /* retry after a delay. */
2292 if (con->delay == 0)
2293 con->delay = BASE_DELAY_INTERVAL;
2294 else if (con->delay < MAX_DELAY_INTERVAL)
2295 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002296 con->ops->get(con);
2297 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002298 round_jiffies_relative(con->delay))) {
2299 dout("fault queued %p delay %lu\n", con, con->delay);
2300 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002301 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002302 dout("fault failed to queue %p delay %lu, backoff\n",
2303 con, con->delay);
2304 /*
2305 * In many cases we see a socket state change
2306 * while con_work is running and end up
2307 * queuing (non-delayed) work, such that we
2308 * can't backoff with a delay. Set a flag so
2309 * that when con_work restarts we schedule the
2310 * delay then.
2311 */
Alex Elder928443c2012-05-22 11:41:43 -05002312 set_bit(BACKOFF, &con->flags);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002313 }
Sage Weil31b80062009-10-06 11:31:13 -07002314 }
2315
Sage Weil91e45ce32010-02-15 12:05:09 -08002316out_unlock:
2317 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002318out:
Sage Weil161fd652010-02-25 12:38:57 -08002319 /*
2320 * in case we faulted due to authentication, invalidate our
2321 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002322 */
Sage Weil161fd652010-02-25 12:38:57 -08002323 if (con->auth_retry && con->ops->invalidate_authorizer) {
2324 dout("calling invalidate_authorizer()\n");
2325 con->ops->invalidate_authorizer(con);
2326 }
2327
Sage Weil31b80062009-10-06 11:31:13 -07002328 if (con->ops->fault)
2329 con->ops->fault(con);
2330}
2331
2332
2333
2334/*
Alex Elder15d98822012-05-26 23:26:43 -05002335 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002336 */
Alex Elder15d98822012-05-26 23:26:43 -05002337void ceph_messenger_init(struct ceph_messenger *msgr,
2338 struct ceph_entity_addr *myaddr,
2339 u32 supported_features,
2340 u32 required_features,
2341 bool nocrc)
Sage Weil31b80062009-10-06 11:31:13 -07002342{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002343 msgr->supported_features = supported_features;
2344 msgr->required_features = required_features;
2345
Sage Weil31b80062009-10-06 11:31:13 -07002346 spin_lock_init(&msgr->global_seq_lock);
2347
Sage Weil31b80062009-10-06 11:31:13 -07002348 if (myaddr)
2349 msgr->inst.addr = *myaddr;
2350
2351 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002352 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002353 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002354 encode_my_addr(msgr);
Alex Elder15d98822012-05-26 23:26:43 -05002355 msgr->nocrc = nocrc;
Sage Weil31b80062009-10-06 11:31:13 -07002356
Alex Elder15d98822012-05-26 23:26:43 -05002357 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002358}
Alex Elder15d98822012-05-26 23:26:43 -05002359EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07002360
Sage Weile00de342011-03-04 12:25:05 -08002361static void clear_standby(struct ceph_connection *con)
2362{
2363 /* come back from STANDBY? */
2364 if (test_and_clear_bit(STANDBY, &con->state)) {
2365 mutex_lock(&con->mutex);
2366 dout("clear_standby %p and ++connect_seq\n", con);
2367 con->connect_seq++;
Alex Elder928443c2012-05-22 11:41:43 -05002368 WARN_ON(test_bit(WRITE_PENDING, &con->flags));
2369 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags));
Sage Weile00de342011-03-04 12:25:05 -08002370 mutex_unlock(&con->mutex);
2371 }
2372}
2373
Sage Weil31b80062009-10-06 11:31:13 -07002374/*
2375 * Queue up an outgoing message on the given connection.
2376 */
2377void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2378{
2379 if (test_bit(CLOSED, &con->state)) {
2380 dout("con_send %p closed, dropping %p\n", con, msg);
2381 ceph_msg_put(msg);
2382 return;
2383 }
2384
2385 /* set src+dst */
Sage Weildbad1852010-03-25 15:45:38 -07002386 msg->hdr.src = con->msgr->inst.name;
Sage Weil31b80062009-10-06 11:31:13 -07002387
Sage Weil3ca02ef2010-03-01 15:25:00 -08002388 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2389
Sage Weile84346b2010-05-11 21:20:38 -07002390 msg->needs_out_seq = true;
2391
Sage Weil31b80062009-10-06 11:31:13 -07002392 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08002393 mutex_lock(&con->mutex);
Alex Elder38941f82012-06-01 14:56:43 -05002394 BUG_ON(msg->con != NULL);
2395 msg->con = con;
Sage Weil31b80062009-10-06 11:31:13 -07002396 BUG_ON(!list_empty(&msg->list_head));
2397 list_add_tail(&msg->list_head, &con->out_queue);
2398 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2399 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2400 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2401 le32_to_cpu(msg->hdr.front_len),
2402 le32_to_cpu(msg->hdr.middle_len),
2403 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002404 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002405
2406 /* if there wasn't anything waiting to send before, queue
2407 * new work */
Sage Weile00de342011-03-04 12:25:05 -08002408 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002409 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002410 queue_con(con);
2411}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002412EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002413
2414/*
2415 * Revoke a message that was previously queued for send
2416 */
2417void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
2418{
Sage Weilec302642009-12-22 10:43:42 -08002419 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002420 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05002421 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002422 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -05002423 BUG_ON(msg->con == NULL);
2424 msg->con = NULL;
2425
Sage Weil31b80062009-10-06 11:31:13 -07002426 ceph_msg_put(msg);
2427 msg->hdr.seq = 0;
Sage Weiled98ada2010-07-05 12:15:14 -07002428 }
2429 if (con->out_msg == msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002430 dout("%s %p msg %p - was sending\n", __func__, con, msg);
Sage Weiled98ada2010-07-05 12:15:14 -07002431 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002432 if (con->out_kvec_is_msg) {
2433 con->out_skip = con->out_kvec_bytes;
2434 con->out_kvec_is_msg = false;
2435 }
Sage Weiled98ada2010-07-05 12:15:14 -07002436 ceph_msg_put(msg);
2437 msg->hdr.seq = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002438 }
Sage Weilec302642009-12-22 10:43:42 -08002439 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002440}
2441
2442/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002443 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002444 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002445void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002446{
2447 mutex_lock(&con->mutex);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002448 if (con->in_msg && con->in_msg == msg) {
2449 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2450 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002451 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2452
2453 /* skip rest of message */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002454 dout("con_revoke_pages %p msg %p revoked\n", con, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002455 con->in_base_pos = con->in_base_pos -
2456 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002457 front_len -
2458 middle_len -
2459 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002460 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002461 ceph_msg_put(con->in_msg);
2462 con->in_msg = NULL;
2463 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002464 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002465 } else {
2466 dout("con_revoke_pages %p msg %p pages %p no-op\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002467 con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002468 }
2469 mutex_unlock(&con->mutex);
2470}
2471
2472/*
Sage Weil31b80062009-10-06 11:31:13 -07002473 * Queue a keepalive byte to ensure the tcp connection is alive.
2474 */
2475void ceph_con_keepalive(struct ceph_connection *con)
2476{
Sage Weile00de342011-03-04 12:25:05 -08002477 dout("con_keepalive %p\n", con);
2478 clear_standby(con);
Alex Elder928443c2012-05-22 11:41:43 -05002479 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2480 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07002481 queue_con(con);
2482}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002483EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002484
2485
2486/*
2487 * construct a new message with given type, size
2488 * the new msg has a ref count of 1.
2489 */
Sage Weilb61c2762011-08-09 15:03:46 -07002490struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2491 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002492{
2493 struct ceph_msg *m;
2494
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002495 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002496 if (m == NULL)
2497 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002498 kref_init(&m->kref);
Alex Elder38941f82012-06-01 14:56:43 -05002499
2500 m->con = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002501 INIT_LIST_HEAD(&m->list_head);
2502
Sage Weil45c6ceb2010-05-11 15:01:51 -07002503 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002504 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002505 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2506 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002507 m->hdr.front_len = cpu_to_le32(front_len);
2508 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002509 m->hdr.data_len = 0;
2510 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002511 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002512 m->footer.front_crc = 0;
2513 m->footer.middle_crc = 0;
2514 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002515 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002516 m->front_max = front_len;
2517 m->front_is_vmalloc = false;
2518 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002519 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002520 m->pool = NULL;
2521
Henry C Changca208922011-05-03 02:29:56 +00002522 /* middle */
2523 m->middle = NULL;
2524
2525 /* data */
2526 m->nr_pages = 0;
2527 m->page_alignment = 0;
2528 m->pages = NULL;
2529 m->pagelist = NULL;
2530 m->bio = NULL;
2531 m->bio_iter = NULL;
2532 m->bio_seg = 0;
2533 m->trail = NULL;
2534
Sage Weil31b80062009-10-06 11:31:13 -07002535 /* front */
2536 if (front_len) {
2537 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002538 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002539 PAGE_KERNEL);
2540 m->front_is_vmalloc = true;
2541 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002542 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002543 }
2544 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002545 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002546 front_len);
2547 goto out2;
2548 }
2549 } else {
2550 m->front.iov_base = NULL;
2551 }
2552 m->front.iov_len = front_len;
2553
Sage Weilbb257662010-04-01 16:07:23 -07002554 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002555 return m;
2556
2557out2:
2558 ceph_msg_put(m);
2559out:
Sage Weilb61c2762011-08-09 15:03:46 -07002560 if (!can_fail) {
2561 pr_err("msg_new can't create type %d front %d\n", type,
2562 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002563 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002564 } else {
2565 dout("msg_new can't create type %d front %d\n", type,
2566 front_len);
2567 }
Sage Weila79832f2010-04-01 16:06:19 -07002568 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002569}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002570EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002571
2572/*
Sage Weil31b80062009-10-06 11:31:13 -07002573 * Allocate "middle" portion of a message, if it is needed and wasn't
2574 * allocated by alloc_msg. This allows us to read a small fixed-size
2575 * per-type header in the front and then gracefully fail (i.e.,
2576 * propagate the error to the caller based on info in the front) when
2577 * the middle is too large.
2578 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002579static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002580{
2581 int type = le16_to_cpu(msg->hdr.type);
2582 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2583
2584 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2585 ceph_msg_type_name(type), middle_len);
2586 BUG_ON(!middle_len);
2587 BUG_ON(msg->middle);
2588
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002589 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002590 if (!msg->middle)
2591 return -ENOMEM;
2592 return 0;
2593}
2594
Yehuda Sadeh24504182010-01-08 13:58:34 -08002595/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05002596 * Allocate a message for receiving an incoming message on a
2597 * connection, and save the result in con->in_msg. Uses the
2598 * connection's private alloc_msg op if available.
2599 *
2600 * Returns true if the message should be skipped, false otherwise.
2601 * If true is returned (skip message), con->in_msg will be NULL.
2602 * If false is returned, con->in_msg will contain a pointer to the
2603 * newly-allocated message, or NULL in case of memory exhaustion.
Yehuda Sadeh24504182010-01-08 13:58:34 -08002604 */
Alex Elder1c20f2d2012-06-04 14:43:32 -05002605static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2606 struct ceph_msg_header *hdr)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002607{
2608 int type = le16_to_cpu(hdr->type);
2609 int front_len = le32_to_cpu(hdr->front_len);
2610 int middle_len = le32_to_cpu(hdr->middle_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002611 int ret;
2612
Alex Elder1c20f2d2012-06-04 14:43:32 -05002613 BUG_ON(con->in_msg != NULL);
2614
Yehuda Sadeh24504182010-01-08 13:58:34 -08002615 if (con->ops->alloc_msg) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002616 int skip = 0;
2617
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002618 mutex_unlock(&con->mutex);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002619 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002620 mutex_lock(&con->mutex);
Alex Elder38941f82012-06-01 14:56:43 -05002621 if (con->in_msg)
2622 con->in_msg->con = con;
Alex Elder1c20f2d2012-06-04 14:43:32 -05002623 if (skip)
2624 con->in_msg = NULL;
2625
2626 if (!con->in_msg)
2627 return skip != 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002628 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002629 if (!con->in_msg) {
2630 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2631 if (!con->in_msg) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002632 pr_err("unable to allocate msg type %d len %d\n",
2633 type, front_len);
Alex Elder1c20f2d2012-06-04 14:43:32 -05002634 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002635 }
Alex Elder38941f82012-06-01 14:56:43 -05002636 con->in_msg->con = con;
Alex Elder1c20f2d2012-06-04 14:43:32 -05002637 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002638 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05002639 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002640
Alex Elder1c20f2d2012-06-04 14:43:32 -05002641 if (middle_len && !con->in_msg->middle) {
2642 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002643 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05002644 ceph_msg_put(con->in_msg);
2645 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002646 }
2647 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002648
Alex Elder1c20f2d2012-06-04 14:43:32 -05002649 return false;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002650}
2651
Sage Weil31b80062009-10-06 11:31:13 -07002652
2653/*
2654 * Free a generically kmalloc'd message.
2655 */
2656void ceph_msg_kfree(struct ceph_msg *m)
2657{
2658 dout("msg_kfree %p\n", m);
2659 if (m->front_is_vmalloc)
2660 vfree(m->front.iov_base);
2661 else
2662 kfree(m->front.iov_base);
2663 kfree(m);
2664}
2665
2666/*
2667 * Drop a msg ref. Destroy as needed.
2668 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002669void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002670{
Sage Weilc2e552e2009-12-07 15:55:05 -08002671 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002672
Sage Weilc2e552e2009-12-07 15:55:05 -08002673 dout("ceph_msg_put last one on %p\n", m);
2674 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002675
Sage Weilc2e552e2009-12-07 15:55:05 -08002676 /* drop middle, data, if any */
2677 if (m->middle) {
2678 ceph_buffer_put(m->middle);
2679 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002680 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002681 m->nr_pages = 0;
2682 m->pages = NULL;
2683
Sage Weil58bb3b32009-12-23 12:12:31 -08002684 if (m->pagelist) {
2685 ceph_pagelist_release(m->pagelist);
2686 kfree(m->pagelist);
2687 m->pagelist = NULL;
2688 }
2689
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002690 m->trail = NULL;
2691
Sage Weilc2e552e2009-12-07 15:55:05 -08002692 if (m->pool)
2693 ceph_msgpool_put(m->pool, m);
2694 else
2695 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002696}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002697EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002698
2699void ceph_msg_dump(struct ceph_msg *msg)
2700{
2701 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2702 msg->front_max, msg->nr_pages);
2703 print_hex_dump(KERN_DEBUG, "header: ",
2704 DUMP_PREFIX_OFFSET, 16, 1,
2705 &msg->hdr, sizeof(msg->hdr), true);
2706 print_hex_dump(KERN_DEBUG, " front: ",
2707 DUMP_PREFIX_OFFSET, 16, 1,
2708 msg->front.iov_base, msg->front.iov_len, true);
2709 if (msg->middle)
2710 print_hex_dump(KERN_DEBUG, "middle: ",
2711 DUMP_PREFIX_OFFSET, 16, 1,
2712 msg->middle->vec.iov_base,
2713 msg->middle->vec.iov_len, true);
2714 print_hex_dump(KERN_DEBUG, "footer: ",
2715 DUMP_PREFIX_OFFSET, 16, 1,
2716 &msg->footer, sizeof(msg->footer), true);
2717}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002718EXPORT_SYMBOL(ceph_msg_dump);