blob: ad5b70801f37788edbb71b6d95cbe7d25ab94abd [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
32/* static tag bytes (protocol control messages) */
33static char tag_msg = CEPH_MSGR_TAG_MSG;
34static char tag_ack = CEPH_MSGR_TAG_ACK;
35static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
36
Sage Weila6a53492010-04-13 14:07:07 -070037#ifdef CONFIG_LOCKDEP
38static struct lock_class_key socket_class;
39#endif
40
Sage Weil31b80062009-10-06 11:31:13 -070041
42static void queue_con(struct ceph_connection *con);
43static void con_work(struct work_struct *);
44static void ceph_fault(struct ceph_connection *con);
45
Sage Weil31b80062009-10-06 11:31:13 -070046/*
47 * nicely render a sockaddr as a string.
48 */
49#define MAX_ADDR_STR 20
Sage Weild06dbaf2010-07-08 10:47:16 -070050#define MAX_ADDR_STR_LEN 60
51static char addr_str[MAX_ADDR_STR][MAX_ADDR_STR_LEN];
Sage Weil31b80062009-10-06 11:31:13 -070052static DEFINE_SPINLOCK(addr_str_lock);
53static int last_addr_str;
54
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070055const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -070056{
57 int i;
58 char *s;
59 struct sockaddr_in *in4 = (void *)ss;
Sage Weil31b80062009-10-06 11:31:13 -070060 struct sockaddr_in6 *in6 = (void *)ss;
61
62 spin_lock(&addr_str_lock);
63 i = last_addr_str++;
64 if (last_addr_str == MAX_ADDR_STR)
65 last_addr_str = 0;
66 spin_unlock(&addr_str_lock);
67 s = addr_str[i];
68
69 switch (ss->ss_family) {
70 case AF_INET:
Sage Weild06dbaf2010-07-08 10:47:16 -070071 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%u", &in4->sin_addr,
72 (unsigned int)ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -070073 break;
74
75 case AF_INET6:
Sage Weild06dbaf2010-07-08 10:47:16 -070076 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%u", &in6->sin6_addr,
77 (unsigned int)ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -070078 break;
79
80 default:
Sage Weil12a2f642011-05-12 14:34:04 -070081 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %d)",
82 (int)ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -070083 }
84
85 return s;
86}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070087EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -070088
Sage Weil63f2d212009-11-03 15:17:56 -080089static void encode_my_addr(struct ceph_messenger *msgr)
90{
91 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
92 ceph_encode_addr(&msgr->my_enc_addr);
93}
94
Sage Weil31b80062009-10-06 11:31:13 -070095/*
96 * work queue for all reading and writing to/from the socket.
97 */
98struct workqueue_struct *ceph_msgr_wq;
99
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700100int ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700101{
Tejun Heof363e45fd12011-01-03 14:49:46 +0100102 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
Sage Weild96c9042010-12-13 20:30:28 -0800103 if (!ceph_msgr_wq) {
104 pr_err("msgr_init failed to create workqueue\n");
105 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700106 }
107 return 0;
108}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700109EXPORT_SYMBOL(ceph_msgr_init);
Sage Weil31b80062009-10-06 11:31:13 -0700110
111void ceph_msgr_exit(void)
112{
113 destroy_workqueue(ceph_msgr_wq);
114}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700115EXPORT_SYMBOL(ceph_msgr_exit);
Sage Weil31b80062009-10-06 11:31:13 -0700116
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700117void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700118{
119 flush_workqueue(ceph_msgr_wq);
120}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700121EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700122
123
Sage Weil31b80062009-10-06 11:31:13 -0700124/*
125 * socket callback functions
126 */
127
128/* data available on socket, or listen socket received a connect */
129static void ceph_data_ready(struct sock *sk, int count_unused)
130{
131 struct ceph_connection *con =
132 (struct ceph_connection *)sk->sk_user_data;
133 if (sk->sk_state != TCP_CLOSE_WAIT) {
134 dout("ceph_data_ready on %p state = %lu, queueing work\n",
135 con, con->state);
136 queue_con(con);
137 }
138}
139
140/* socket has buffer space for writing */
141static void ceph_write_space(struct sock *sk)
142{
143 struct ceph_connection *con =
144 (struct ceph_connection *)sk->sk_user_data;
145
146 /* only queue to workqueue if there is data we want to write. */
147 if (test_bit(WRITE_PENDING, &con->state)) {
148 dout("ceph_write_space %p queueing write work\n", con);
149 queue_con(con);
150 } else {
151 dout("ceph_write_space %p nothing to write\n", con);
152 }
153
154 /* since we have our own write_space, clear the SOCK_NOSPACE flag */
155 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
156}
157
158/* socket's state has changed */
159static void ceph_state_change(struct sock *sk)
160{
161 struct ceph_connection *con =
162 (struct ceph_connection *)sk->sk_user_data;
163
164 dout("ceph_state_change %p state = %lu sk_state = %u\n",
165 con, con->state, sk->sk_state);
166
167 if (test_bit(CLOSED, &con->state))
168 return;
169
170 switch (sk->sk_state) {
171 case TCP_CLOSE:
172 dout("ceph_state_change TCP_CLOSE\n");
173 case TCP_CLOSE_WAIT:
174 dout("ceph_state_change TCP_CLOSE_WAIT\n");
175 if (test_and_set_bit(SOCK_CLOSED, &con->state) == 0) {
176 if (test_bit(CONNECTING, &con->state))
177 con->error_msg = "connection failed";
178 else
179 con->error_msg = "socket closed";
180 queue_con(con);
181 }
182 break;
183 case TCP_ESTABLISHED:
184 dout("ceph_state_change TCP_ESTABLISHED\n");
185 queue_con(con);
186 break;
187 }
188}
189
190/*
191 * set up socket callbacks
192 */
193static void set_sock_callbacks(struct socket *sock,
194 struct ceph_connection *con)
195{
196 struct sock *sk = sock->sk;
197 sk->sk_user_data = (void *)con;
198 sk->sk_data_ready = ceph_data_ready;
199 sk->sk_write_space = ceph_write_space;
200 sk->sk_state_change = ceph_state_change;
201}
202
203
204/*
205 * socket helpers
206 */
207
208/*
209 * initiate connection to a remote socket.
210 */
211static struct socket *ceph_tcp_connect(struct ceph_connection *con)
212{
Sage Weilf91d3472010-07-01 15:18:31 -0700213 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700214 struct socket *sock;
215 int ret;
216
217 BUG_ON(con->sock);
Sage Weilf91d3472010-07-01 15:18:31 -0700218 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
219 IPPROTO_TCP, &sock);
Sage Weil31b80062009-10-06 11:31:13 -0700220 if (ret)
221 return ERR_PTR(ret);
222 con->sock = sock;
223 sock->sk->sk_allocation = GFP_NOFS;
224
Sage Weila6a53492010-04-13 14:07:07 -0700225#ifdef CONFIG_LOCKDEP
226 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
227#endif
228
Sage Weil31b80062009-10-06 11:31:13 -0700229 set_sock_callbacks(sock, con);
230
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700231 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700232
Sage Weilf91d3472010-07-01 15:18:31 -0700233 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
234 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700235 if (ret == -EINPROGRESS) {
236 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700237 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700238 sock->sk->sk_state);
239 ret = 0;
240 }
241 if (ret < 0) {
242 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700243 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700244 sock_release(sock);
245 con->sock = NULL;
246 con->error_msg = "connect error";
247 }
248
249 if (ret < 0)
250 return ERR_PTR(ret);
251 return sock;
252}
253
254static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
255{
256 struct kvec iov = {buf, len};
257 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800258 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700259
Sage Weil98bdb0a2011-01-25 08:17:48 -0800260 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
261 if (r == -EAGAIN)
262 r = 0;
263 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700264}
265
266/*
267 * write something. @more is true if caller will be sending more data
268 * shortly.
269 */
270static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
271 size_t kvlen, size_t len, int more)
272{
273 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800274 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700275
276 if (more)
277 msg.msg_flags |= MSG_MORE;
278 else
279 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
280
Sage Weil42961d22011-01-25 08:19:34 -0800281 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
282 if (r == -EAGAIN)
283 r = 0;
284 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700285}
286
287
288/*
289 * Shutdown/close the socket for the given connection.
290 */
291static int con_close_socket(struct ceph_connection *con)
292{
293 int rc;
294
295 dout("con_close_socket on %p sock %p\n", con, con->sock);
296 if (!con->sock)
297 return 0;
298 set_bit(SOCK_CLOSED, &con->state);
299 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
300 sock_release(con->sock);
301 con->sock = NULL;
302 clear_bit(SOCK_CLOSED, &con->state);
303 return rc;
304}
305
306/*
307 * Reset a connection. Discard all incoming and outgoing messages
308 * and clear *_seq state.
309 */
310static void ceph_msg_remove(struct ceph_msg *msg)
311{
312 list_del_init(&msg->list_head);
313 ceph_msg_put(msg);
314}
315static void ceph_msg_remove_list(struct list_head *head)
316{
317 while (!list_empty(head)) {
318 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
319 list_head);
320 ceph_msg_remove(msg);
321 }
322}
323
324static void reset_connection(struct ceph_connection *con)
325{
326 /* reset connection, out_queue, msg_ and connect_seq */
327 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700328 ceph_msg_remove_list(&con->out_queue);
329 ceph_msg_remove_list(&con->out_sent);
330
Sage Weilcf3e5c42009-12-11 09:48:05 -0800331 if (con->in_msg) {
332 ceph_msg_put(con->in_msg);
333 con->in_msg = NULL;
334 }
335
Sage Weil31b80062009-10-06 11:31:13 -0700336 con->connect_seq = 0;
337 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800338 if (con->out_msg) {
339 ceph_msg_put(con->out_msg);
340 con->out_msg = NULL;
341 }
Sage Weil31b80062009-10-06 11:31:13 -0700342 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700343 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700344}
345
346/*
347 * mark a peer down. drop any open connections.
348 */
349void ceph_con_close(struct ceph_connection *con)
350{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700351 dout("con_close %p peer %s\n", con,
352 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700353 set_bit(CLOSED, &con->state); /* in case there's queued work */
354 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Sage Weil1679f872010-02-26 13:55:51 -0800355 clear_bit(LOSSYTX, &con->state); /* so we retry next connect */
356 clear_bit(KEEPALIVE_PENDING, &con->state);
357 clear_bit(WRITE_PENDING, &con->state);
Sage Weilec302642009-12-22 10:43:42 -0800358 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700359 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700360 con->peer_global_seq = 0;
Sage Weil91e45ce32010-02-15 12:05:09 -0800361 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800362 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700363 queue_con(con);
364}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700365EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700366
367/*
Sage Weil31b80062009-10-06 11:31:13 -0700368 * Reopen a closed connection, with a new peer address.
369 */
370void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
371{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700372 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700373 set_bit(OPENING, &con->state);
374 clear_bit(CLOSED, &con->state);
375 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800376 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700377 queue_con(con);
378}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700379EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700380
381/*
Sage Weil87b315a2010-03-22 14:51:18 -0700382 * return true if this connection ever successfully opened
383 */
384bool ceph_con_opened(struct ceph_connection *con)
385{
386 return con->connect_seq > 0;
387}
388
389/*
Sage Weil31b80062009-10-06 11:31:13 -0700390 * generic get/put
391 */
392struct ceph_connection *ceph_con_get(struct ceph_connection *con)
393{
394 dout("con_get %p nref = %d -> %d\n", con,
395 atomic_read(&con->nref), atomic_read(&con->nref) + 1);
396 if (atomic_inc_not_zero(&con->nref))
397 return con;
398 return NULL;
399}
400
401void ceph_con_put(struct ceph_connection *con)
402{
403 dout("con_put %p nref = %d -> %d\n", con,
404 atomic_read(&con->nref), atomic_read(&con->nref) - 1);
405 BUG_ON(atomic_read(&con->nref) == 0);
406 if (atomic_dec_and_test(&con->nref)) {
Sage Weil71ececd2009-11-18 11:27:06 -0800407 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -0700408 kfree(con);
409 }
410}
411
412/*
413 * initialize a new connection.
414 */
415void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con)
416{
417 dout("con_init %p\n", con);
418 memset(con, 0, sizeof(*con));
419 atomic_set(&con->nref, 1);
420 con->msgr = msgr;
Sage Weilec302642009-12-22 10:43:42 -0800421 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700422 INIT_LIST_HEAD(&con->out_queue);
423 INIT_LIST_HEAD(&con->out_sent);
424 INIT_DELAYED_WORK(&con->work, con_work);
425}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700426EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700427
428
429/*
430 * We maintain a global counter to order connection attempts. Get
431 * a unique seq greater than @gt.
432 */
433static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
434{
435 u32 ret;
436
437 spin_lock(&msgr->global_seq_lock);
438 if (msgr->global_seq < gt)
439 msgr->global_seq = gt;
440 ret = ++msgr->global_seq;
441 spin_unlock(&msgr->global_seq_lock);
442 return ret;
443}
444
445
446/*
447 * Prepare footer for currently outgoing message, and finish things
448 * off. Assumes out_kvec* are already valid.. we just add on to the end.
449 */
450static void prepare_write_message_footer(struct ceph_connection *con, int v)
451{
452 struct ceph_msg *m = con->out_msg;
453
454 dout("prepare_write_message_footer %p\n", con);
455 con->out_kvec_is_msg = true;
456 con->out_kvec[v].iov_base = &m->footer;
457 con->out_kvec[v].iov_len = sizeof(m->footer);
458 con->out_kvec_bytes += sizeof(m->footer);
459 con->out_kvec_left++;
460 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800461 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700462}
463
464/*
465 * Prepare headers for the next outgoing message.
466 */
467static void prepare_write_message(struct ceph_connection *con)
468{
469 struct ceph_msg *m;
470 int v = 0;
471
472 con->out_kvec_bytes = 0;
473 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800474 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700475
476 /* Sneak an ack in there first? If we can get it into the same
477 * TCP packet that's a good thing. */
478 if (con->in_seq > con->in_seq_acked) {
479 con->in_seq_acked = con->in_seq;
480 con->out_kvec[v].iov_base = &tag_ack;
481 con->out_kvec[v++].iov_len = 1;
482 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
483 con->out_kvec[v].iov_base = &con->out_temp_ack;
484 con->out_kvec[v++].iov_len = sizeof(con->out_temp_ack);
485 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
486 }
487
Sage Weil31b80062009-10-06 11:31:13 -0700488 m = list_first_entry(&con->out_queue,
489 struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800490 con->out_msg = m;
Sage Weil4cf9d542011-07-26 11:27:24 -0700491
492 /* put message on sent list */
493 ceph_msg_get(m);
494 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -0700495
Sage Weile84346b2010-05-11 21:20:38 -0700496 /*
497 * only assign outgoing seq # if we haven't sent this message
498 * yet. if it is requeued, resend with it's original seq.
499 */
500 if (m->needs_out_seq) {
501 m->hdr.seq = cpu_to_le64(++con->out_seq);
502 m->needs_out_seq = false;
503 }
Sage Weil31b80062009-10-06 11:31:13 -0700504
505 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
506 m, con->out_seq, le16_to_cpu(m->hdr.type),
507 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
508 le32_to_cpu(m->hdr.data_len),
509 m->nr_pages);
510 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
511
512 /* tag + hdr + front + middle */
513 con->out_kvec[v].iov_base = &tag_msg;
514 con->out_kvec[v++].iov_len = 1;
515 con->out_kvec[v].iov_base = &m->hdr;
516 con->out_kvec[v++].iov_len = sizeof(m->hdr);
517 con->out_kvec[v++] = m->front;
518 if (m->middle)
519 con->out_kvec[v++] = m->middle->vec;
520 con->out_kvec_left = v;
521 con->out_kvec_bytes += 1 + sizeof(m->hdr) + m->front.iov_len +
522 (m->middle ? m->middle->vec.iov_len : 0);
523 con->out_kvec_cur = con->out_kvec;
524
525 /* fill in crc (except data pages), footer */
526 con->out_msg->hdr.crc =
527 cpu_to_le32(crc32c(0, (void *)&m->hdr,
528 sizeof(m->hdr) - sizeof(m->hdr.crc)));
529 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
530 con->out_msg->footer.front_crc =
531 cpu_to_le32(crc32c(0, m->front.iov_base, m->front.iov_len));
532 if (m->middle)
533 con->out_msg->footer.middle_crc =
534 cpu_to_le32(crc32c(0, m->middle->vec.iov_base,
535 m->middle->vec.iov_len));
536 else
537 con->out_msg->footer.middle_crc = 0;
538 con->out_msg->footer.data_crc = 0;
539 dout("prepare_write_message front_crc %u data_crc %u\n",
540 le32_to_cpu(con->out_msg->footer.front_crc),
541 le32_to_cpu(con->out_msg->footer.middle_crc));
542
543 /* is there a data payload? */
544 if (le32_to_cpu(m->hdr.data_len) > 0) {
545 /* initialize page iterator */
546 con->out_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700547 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -0800548 con->out_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700549 else
550 con->out_msg_pos.page_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700551 con->out_msg_pos.data_pos = 0;
552 con->out_msg_pos.did_page_crc = 0;
553 con->out_more = 1; /* data + footer will follow */
554 } else {
555 /* no, queue up footer too and be done */
556 prepare_write_message_footer(con, v);
557 }
558
559 set_bit(WRITE_PENDING, &con->state);
560}
561
562/*
563 * Prepare an ack.
564 */
565static void prepare_write_ack(struct ceph_connection *con)
566{
567 dout("prepare_write_ack %p %llu -> %llu\n", con,
568 con->in_seq_acked, con->in_seq);
569 con->in_seq_acked = con->in_seq;
570
571 con->out_kvec[0].iov_base = &tag_ack;
572 con->out_kvec[0].iov_len = 1;
573 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
574 con->out_kvec[1].iov_base = &con->out_temp_ack;
575 con->out_kvec[1].iov_len = sizeof(con->out_temp_ack);
576 con->out_kvec_left = 2;
577 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
578 con->out_kvec_cur = con->out_kvec;
579 con->out_more = 1; /* more will follow.. eventually.. */
580 set_bit(WRITE_PENDING, &con->state);
581}
582
583/*
584 * Prepare to write keepalive byte.
585 */
586static void prepare_write_keepalive(struct ceph_connection *con)
587{
588 dout("prepare_write_keepalive %p\n", con);
589 con->out_kvec[0].iov_base = &tag_keepalive;
590 con->out_kvec[0].iov_len = 1;
591 con->out_kvec_left = 1;
592 con->out_kvec_bytes = 1;
593 con->out_kvec_cur = con->out_kvec;
594 set_bit(WRITE_PENDING, &con->state);
595}
596
597/*
598 * Connection negotiation.
599 */
600
Sage Weil0da5d702011-05-19 11:21:05 -0700601static int prepare_connect_authorizer(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800602{
603 void *auth_buf;
604 int auth_len = 0;
605 int auth_protocol = 0;
606
Sage Weilec302642009-12-22 10:43:42 -0800607 mutex_unlock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800608 if (con->ops->get_authorizer)
609 con->ops->get_authorizer(con, &auth_buf, &auth_len,
610 &auth_protocol, &con->auth_reply_buf,
611 &con->auth_reply_buf_len,
612 con->auth_retry);
Sage Weilec302642009-12-22 10:43:42 -0800613 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800614
Sage Weil0da5d702011-05-19 11:21:05 -0700615 if (test_bit(CLOSED, &con->state) ||
616 test_bit(OPENING, &con->state))
617 return -EAGAIN;
618
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800619 con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol);
620 con->out_connect.authorizer_len = cpu_to_le32(auth_len);
621
Sage Weile8f54ce2011-05-12 14:18:42 -0700622 if (auth_len) {
623 con->out_kvec[con->out_kvec_left].iov_base = auth_buf;
624 con->out_kvec[con->out_kvec_left].iov_len = auth_len;
625 con->out_kvec_left++;
626 con->out_kvec_bytes += auth_len;
627 }
Sage Weil0da5d702011-05-19 11:21:05 -0700628 return 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800629}
630
Sage Weil31b80062009-10-06 11:31:13 -0700631/*
632 * We connected to a peer and are saying hello.
633 */
Sage Weileed0ef22009-11-10 14:34:36 -0800634static void prepare_write_banner(struct ceph_messenger *msgr,
635 struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700636{
637 int len = strlen(CEPH_BANNER);
Sage Weileed0ef22009-11-10 14:34:36 -0800638
639 con->out_kvec[0].iov_base = CEPH_BANNER;
640 con->out_kvec[0].iov_len = len;
641 con->out_kvec[1].iov_base = &msgr->my_enc_addr;
642 con->out_kvec[1].iov_len = sizeof(msgr->my_enc_addr);
643 con->out_kvec_left = 2;
644 con->out_kvec_bytes = len + sizeof(msgr->my_enc_addr);
645 con->out_kvec_cur = con->out_kvec;
646 con->out_more = 0;
647 set_bit(WRITE_PENDING, &con->state);
648}
649
Sage Weil0da5d702011-05-19 11:21:05 -0700650static int prepare_write_connect(struct ceph_messenger *msgr,
651 struct ceph_connection *con,
652 int after_banner)
Sage Weileed0ef22009-11-10 14:34:36 -0800653{
Sage Weil31b80062009-10-06 11:31:13 -0700654 unsigned global_seq = get_global_seq(con->msgr, 0);
655 int proto;
656
657 switch (con->peer_name.type) {
658 case CEPH_ENTITY_TYPE_MON:
659 proto = CEPH_MONC_PROTOCOL;
660 break;
661 case CEPH_ENTITY_TYPE_OSD:
662 proto = CEPH_OSDC_PROTOCOL;
663 break;
664 case CEPH_ENTITY_TYPE_MDS:
665 proto = CEPH_MDSC_PROTOCOL;
666 break;
667 default:
668 BUG();
669 }
670
671 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
672 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800673
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700674 con->out_connect.features = cpu_to_le64(msgr->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -0700675 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
676 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
677 con->out_connect.global_seq = cpu_to_le32(global_seq);
678 con->out_connect.protocol_version = cpu_to_le32(proto);
679 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700680
Sage Weileed0ef22009-11-10 14:34:36 -0800681 if (!after_banner) {
682 con->out_kvec_left = 0;
683 con->out_kvec_bytes = 0;
684 }
685 con->out_kvec[con->out_kvec_left].iov_base = &con->out_connect;
686 con->out_kvec[con->out_kvec_left].iov_len = sizeof(con->out_connect);
687 con->out_kvec_left++;
688 con->out_kvec_bytes += sizeof(con->out_connect);
Sage Weil31b80062009-10-06 11:31:13 -0700689 con->out_kvec_cur = con->out_kvec;
690 con->out_more = 0;
691 set_bit(WRITE_PENDING, &con->state);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800692
Sage Weil0da5d702011-05-19 11:21:05 -0700693 return prepare_connect_authorizer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700694}
695
696
697/*
698 * write as much of pending kvecs to the socket as we can.
699 * 1 -> done
700 * 0 -> socket full, but more to do
701 * <0 -> error
702 */
703static int write_partial_kvec(struct ceph_connection *con)
704{
705 int ret;
706
707 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
708 while (con->out_kvec_bytes > 0) {
709 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
710 con->out_kvec_left, con->out_kvec_bytes,
711 con->out_more);
712 if (ret <= 0)
713 goto out;
714 con->out_kvec_bytes -= ret;
715 if (con->out_kvec_bytes == 0)
716 break; /* done */
717 while (ret > 0) {
718 if (ret >= con->out_kvec_cur->iov_len) {
719 ret -= con->out_kvec_cur->iov_len;
720 con->out_kvec_cur++;
721 con->out_kvec_left--;
722 } else {
723 con->out_kvec_cur->iov_len -= ret;
724 con->out_kvec_cur->iov_base += ret;
725 ret = 0;
726 break;
727 }
728 }
729 }
730 con->out_kvec_left = 0;
731 con->out_kvec_is_msg = false;
732 ret = 1;
733out:
734 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
735 con->out_kvec_bytes, con->out_kvec_left, ret);
736 return ret; /* done! */
737}
738
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700739#ifdef CONFIG_BLOCK
740static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
741{
742 if (!bio) {
743 *iter = NULL;
744 *seg = 0;
745 return;
746 }
747 *iter = bio;
748 *seg = bio->bi_idx;
749}
750
751static void iter_bio_next(struct bio **bio_iter, int *seg)
752{
753 if (*bio_iter == NULL)
754 return;
755
756 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
757
758 (*seg)++;
759 if (*seg == (*bio_iter)->bi_vcnt)
760 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
761}
762#endif
763
Sage Weil31b80062009-10-06 11:31:13 -0700764/*
765 * Write as much message data payload as we can. If we finish, queue
766 * up the footer.
767 * 1 -> done, footer is now queued in out_kvec[].
768 * 0 -> socket full, but more to do
769 * <0 -> error
770 */
771static int write_partial_msg_pages(struct ceph_connection *con)
772{
773 struct ceph_msg *msg = con->out_msg;
774 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
775 size_t len;
776 int crc = con->msgr->nocrc;
777 int ret;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700778 int total_max_write;
779 int in_trail = 0;
780 size_t trail_len = (msg->trail ? msg->trail->length : 0);
Sage Weil31b80062009-10-06 11:31:13 -0700781
782 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
783 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
784 con->out_msg_pos.page_pos);
785
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700786#ifdef CONFIG_BLOCK
787 if (msg->bio && !msg->bio_iter)
788 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
789#endif
790
791 while (data_len > con->out_msg_pos.data_pos) {
Sage Weil31b80062009-10-06 11:31:13 -0700792 struct page *page = NULL;
793 void *kaddr = NULL;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700794 int max_write = PAGE_SIZE;
795 int page_shift = 0;
796
797 total_max_write = data_len - trail_len -
798 con->out_msg_pos.data_pos;
Sage Weil31b80062009-10-06 11:31:13 -0700799
800 /*
801 * if we are calculating the data crc (the default), we need
802 * to map the page. if our pages[] has been revoked, use the
803 * zero page.
804 */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700805
806 /* have we reached the trail part of the data? */
807 if (con->out_msg_pos.data_pos >= data_len - trail_len) {
808 in_trail = 1;
809
810 total_max_write = data_len - con->out_msg_pos.data_pos;
811
812 page = list_first_entry(&msg->trail->head,
813 struct page, lru);
814 if (crc)
815 kaddr = kmap(page);
816 max_write = PAGE_SIZE;
817 } else if (msg->pages) {
Sage Weil31b80062009-10-06 11:31:13 -0700818 page = msg->pages[con->out_msg_pos.page];
819 if (crc)
820 kaddr = kmap(page);
Sage Weil58bb3b32009-12-23 12:12:31 -0800821 } else if (msg->pagelist) {
822 page = list_first_entry(&msg->pagelist->head,
823 struct page, lru);
824 if (crc)
825 kaddr = kmap(page);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700826#ifdef CONFIG_BLOCK
827 } else if (msg->bio) {
828 struct bio_vec *bv;
829
830 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
831 page = bv->bv_page;
832 page_shift = bv->bv_offset;
833 if (crc)
834 kaddr = kmap(page) + page_shift;
835 max_write = bv->bv_len;
836#endif
Sage Weil31b80062009-10-06 11:31:13 -0700837 } else {
838 page = con->msgr->zero_page;
839 if (crc)
840 kaddr = page_address(con->msgr->zero_page);
841 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700842 len = min_t(int, max_write - con->out_msg_pos.page_pos,
843 total_max_write);
844
Sage Weil31b80062009-10-06 11:31:13 -0700845 if (crc && !con->out_msg_pos.did_page_crc) {
846 void *base = kaddr + con->out_msg_pos.page_pos;
847 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
848
849 BUG_ON(kaddr == NULL);
850 con->out_msg->footer.data_crc =
851 cpu_to_le32(crc32c(tmpcrc, base, len));
852 con->out_msg_pos.did_page_crc = 1;
853 }
Sage Weil31b80062009-10-06 11:31:13 -0700854 ret = kernel_sendpage(con->sock, page,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700855 con->out_msg_pos.page_pos + page_shift,
856 len,
Sage Weil31b80062009-10-06 11:31:13 -0700857 MSG_DONTWAIT | MSG_NOSIGNAL |
858 MSG_MORE);
859
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700860 if (crc &&
861 (msg->pages || msg->pagelist || msg->bio || in_trail))
Sage Weil31b80062009-10-06 11:31:13 -0700862 kunmap(page);
863
Sage Weil42961d22011-01-25 08:19:34 -0800864 if (ret == -EAGAIN)
865 ret = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700866 if (ret <= 0)
867 goto out;
868
869 con->out_msg_pos.data_pos += ret;
870 con->out_msg_pos.page_pos += ret;
871 if (ret == len) {
872 con->out_msg_pos.page_pos = 0;
873 con->out_msg_pos.page++;
874 con->out_msg_pos.did_page_crc = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700875 if (in_trail)
876 list_move_tail(&page->lru,
877 &msg->trail->head);
878 else if (msg->pagelist)
Sage Weil58bb3b32009-12-23 12:12:31 -0800879 list_move_tail(&page->lru,
880 &msg->pagelist->head);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700881#ifdef CONFIG_BLOCK
882 else if (msg->bio)
883 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
884#endif
Sage Weil31b80062009-10-06 11:31:13 -0700885 }
886 }
887
888 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
889
890 /* prepare and queue up footer, too */
891 if (!crc)
892 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
893 con->out_kvec_bytes = 0;
894 con->out_kvec_left = 0;
895 con->out_kvec_cur = con->out_kvec;
896 prepare_write_message_footer(con, 0);
897 ret = 1;
898out:
899 return ret;
900}
901
902/*
903 * write some zeros
904 */
905static int write_partial_skip(struct ceph_connection *con)
906{
907 int ret;
908
909 while (con->out_skip > 0) {
910 struct kvec iov = {
911 .iov_base = page_address(con->msgr->zero_page),
912 .iov_len = min(con->out_skip, (int)PAGE_CACHE_SIZE)
913 };
914
915 ret = ceph_tcp_sendmsg(con->sock, &iov, 1, iov.iov_len, 1);
916 if (ret <= 0)
917 goto out;
918 con->out_skip -= ret;
919 }
920 ret = 1;
921out:
922 return ret;
923}
924
925/*
926 * Prepare to read connection handshake, or an ack.
927 */
Sage Weileed0ef22009-11-10 14:34:36 -0800928static void prepare_read_banner(struct ceph_connection *con)
929{
930 dout("prepare_read_banner %p\n", con);
931 con->in_base_pos = 0;
932}
933
Sage Weil31b80062009-10-06 11:31:13 -0700934static void prepare_read_connect(struct ceph_connection *con)
935{
936 dout("prepare_read_connect %p\n", con);
937 con->in_base_pos = 0;
938}
939
940static void prepare_read_ack(struct ceph_connection *con)
941{
942 dout("prepare_read_ack %p\n", con);
943 con->in_base_pos = 0;
944}
945
946static void prepare_read_tag(struct ceph_connection *con)
947{
948 dout("prepare_read_tag %p\n", con);
949 con->in_base_pos = 0;
950 con->in_tag = CEPH_MSGR_TAG_READY;
951}
952
953/*
954 * Prepare to read a message.
955 */
956static int prepare_read_message(struct ceph_connection *con)
957{
958 dout("prepare_read_message %p\n", con);
959 BUG_ON(con->in_msg != NULL);
960 con->in_base_pos = 0;
961 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
962 return 0;
963}
964
965
966static int read_partial(struct ceph_connection *con,
967 int *to, int size, void *object)
968{
969 *to += size;
970 while (con->in_base_pos < *to) {
971 int left = *to - con->in_base_pos;
972 int have = size - left;
973 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
974 if (ret <= 0)
975 return ret;
976 con->in_base_pos += ret;
977 }
978 return 1;
979}
980
981
982/*
983 * Read all or part of the connect-side handshake on a new connection
984 */
Sage Weileed0ef22009-11-10 14:34:36 -0800985static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700986{
987 int ret, to = 0;
988
Sage Weileed0ef22009-11-10 14:34:36 -0800989 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -0700990
991 /* peer's banner */
992 ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner);
993 if (ret <= 0)
994 goto out;
995 ret = read_partial(con, &to, sizeof(con->actual_peer_addr),
996 &con->actual_peer_addr);
997 if (ret <= 0)
998 goto out;
999 ret = read_partial(con, &to, sizeof(con->peer_addr_for_me),
1000 &con->peer_addr_for_me);
1001 if (ret <= 0)
1002 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08001003out:
1004 return ret;
1005}
1006
1007static int read_partial_connect(struct ceph_connection *con)
1008{
1009 int ret, to = 0;
1010
1011 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1012
Sage Weil31b80062009-10-06 11:31:13 -07001013 ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply);
1014 if (ret <= 0)
1015 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001016 ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len),
1017 con->auth_reply_buf);
1018 if (ret <= 0)
1019 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001020
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001021 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1022 con, (int)con->in_reply.tag,
1023 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001024 le32_to_cpu(con->in_reply.global_seq));
1025out:
1026 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001027
Sage Weil31b80062009-10-06 11:31:13 -07001028}
1029
1030/*
1031 * Verify the hello banner looks okay.
1032 */
1033static int verify_hello(struct ceph_connection *con)
1034{
1035 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001036 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001037 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001038 con->error_msg = "protocol error, bad banner";
1039 return -1;
1040 }
1041 return 0;
1042}
1043
1044static bool addr_is_blank(struct sockaddr_storage *ss)
1045{
1046 switch (ss->ss_family) {
1047 case AF_INET:
1048 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1049 case AF_INET6:
1050 return
1051 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1052 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1053 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1054 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1055 }
1056 return false;
1057}
1058
1059static int addr_port(struct sockaddr_storage *ss)
1060{
1061 switch (ss->ss_family) {
1062 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001063 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001064 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001065 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001066 }
1067 return 0;
1068}
1069
1070static void addr_set_port(struct sockaddr_storage *ss, int p)
1071{
1072 switch (ss->ss_family) {
1073 case AF_INET:
1074 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001075 break;
Sage Weil31b80062009-10-06 11:31:13 -07001076 case AF_INET6:
1077 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001078 break;
Sage Weil31b80062009-10-06 11:31:13 -07001079 }
1080}
1081
1082/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001083 * Unlike other *_pton function semantics, zero indicates success.
1084 */
1085static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1086 char delim, const char **ipend)
1087{
1088 struct sockaddr_in *in4 = (void *)ss;
1089 struct sockaddr_in6 *in6 = (void *)ss;
1090
1091 memset(ss, 0, sizeof(*ss));
1092
1093 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1094 ss->ss_family = AF_INET;
1095 return 0;
1096 }
1097
1098 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1099 ss->ss_family = AF_INET6;
1100 return 0;
1101 }
1102
1103 return -EINVAL;
1104}
1105
1106/*
1107 * Extract hostname string and resolve using kernel DNS facility.
1108 */
1109#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1110static int ceph_dns_resolve_name(const char *name, size_t namelen,
1111 struct sockaddr_storage *ss, char delim, const char **ipend)
1112{
1113 const char *end, *delim_p;
1114 char *colon_p, *ip_addr = NULL;
1115 int ip_len, ret;
1116
1117 /*
1118 * The end of the hostname occurs immediately preceding the delimiter or
1119 * the port marker (':') where the delimiter takes precedence.
1120 */
1121 delim_p = memchr(name, delim, namelen);
1122 colon_p = memchr(name, ':', namelen);
1123
1124 if (delim_p && colon_p)
1125 end = delim_p < colon_p ? delim_p : colon_p;
1126 else if (!delim_p && colon_p)
1127 end = colon_p;
1128 else {
1129 end = delim_p;
1130 if (!end) /* case: hostname:/ */
1131 end = name + namelen;
1132 }
1133
1134 if (end <= name)
1135 return -EINVAL;
1136
1137 /* do dns_resolve upcall */
1138 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1139 if (ip_len > 0)
1140 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1141 else
1142 ret = -ESRCH;
1143
1144 kfree(ip_addr);
1145
1146 *ipend = end;
1147
1148 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1149 ret, ret ? "failed" : ceph_pr_addr(ss));
1150
1151 return ret;
1152}
1153#else
1154static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1155 struct sockaddr_storage *ss, char delim, const char **ipend)
1156{
1157 return -EINVAL;
1158}
1159#endif
1160
1161/*
1162 * Parse a server name (IP or hostname). If a valid IP address is not found
1163 * then try to extract a hostname to resolve using userspace DNS upcall.
1164 */
1165static int ceph_parse_server_name(const char *name, size_t namelen,
1166 struct sockaddr_storage *ss, char delim, const char **ipend)
1167{
1168 int ret;
1169
1170 ret = ceph_pton(name, namelen, ss, delim, ipend);
1171 if (ret)
1172 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1173
1174 return ret;
1175}
1176
1177/*
Sage Weil31b80062009-10-06 11:31:13 -07001178 * Parse an ip[:port] list into an addr array. Use the default
1179 * monitor port if a port isn't specified.
1180 */
1181int ceph_parse_ips(const char *c, const char *end,
1182 struct ceph_entity_addr *addr,
1183 int max_count, int *count)
1184{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001185 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001186 const char *p = c;
1187
1188 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1189 for (i = 0; i < max_count; i++) {
1190 const char *ipend;
1191 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001192 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001193 char delim = ',';
1194
1195 if (*p == '[') {
1196 delim = ']';
1197 p++;
1198 }
Sage Weil31b80062009-10-06 11:31:13 -07001199
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001200 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1201 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001202 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001203 ret = -EINVAL;
1204
Sage Weil31b80062009-10-06 11:31:13 -07001205 p = ipend;
1206
Sage Weil39139f62010-07-08 09:54:52 -07001207 if (delim == ']') {
1208 if (*p != ']') {
1209 dout("missing matching ']'\n");
1210 goto bad;
1211 }
1212 p++;
1213 }
1214
Sage Weil31b80062009-10-06 11:31:13 -07001215 /* port? */
1216 if (p < end && *p == ':') {
1217 port = 0;
1218 p++;
1219 while (p < end && *p >= '0' && *p <= '9') {
1220 port = (port * 10) + (*p - '0');
1221 p++;
1222 }
1223 if (port > 65535 || port == 0)
1224 goto bad;
1225 } else {
1226 port = CEPH_MON_PORT;
1227 }
1228
1229 addr_set_port(ss, port);
1230
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001231 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001232
1233 if (p == end)
1234 break;
1235 if (*p != ',')
1236 goto bad;
1237 p++;
1238 }
1239
1240 if (p != end)
1241 goto bad;
1242
1243 if (count)
1244 *count = i + 1;
1245 return 0;
1246
1247bad:
Sage Weil39139f62010-07-08 09:54:52 -07001248 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001249 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001250}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001251EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001252
Sage Weileed0ef22009-11-10 14:34:36 -08001253static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001254{
Sage Weileed0ef22009-11-10 14:34:36 -08001255 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001256
1257 if (verify_hello(con) < 0)
1258 return -1;
1259
Sage Weil63f2d212009-11-03 15:17:56 -08001260 ceph_decode_addr(&con->actual_peer_addr);
1261 ceph_decode_addr(&con->peer_addr_for_me);
1262
Sage Weil31b80062009-10-06 11:31:13 -07001263 /*
1264 * Make sure the other end is who we wanted. note that the other
1265 * end may not yet know their ip address, so if it's 0.0.0.0, give
1266 * them the benefit of the doubt.
1267 */
Sage Weil103e2d32010-01-07 16:12:36 -08001268 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1269 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001270 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1271 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001272 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001273 ceph_pr_addr(&con->peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001274 (int)le32_to_cpu(con->peer_addr.nonce),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001275 ceph_pr_addr(&con->actual_peer_addr.in_addr),
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001276 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001277 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001278 return -1;
1279 }
1280
1281 /*
1282 * did we learn our address?
1283 */
1284 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1285 int port = addr_port(&con->msgr->inst.addr.in_addr);
1286
1287 memcpy(&con->msgr->inst.addr.in_addr,
1288 &con->peer_addr_for_me.in_addr,
1289 sizeof(con->peer_addr_for_me.in_addr));
1290 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001291 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001292 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001293 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001294 }
1295
Sage Weileed0ef22009-11-10 14:34:36 -08001296 set_bit(NEGOTIATING, &con->state);
1297 prepare_read_connect(con);
1298 return 0;
1299}
1300
Sage Weil04a419f2009-12-23 09:30:21 -08001301static void fail_protocol(struct ceph_connection *con)
1302{
1303 reset_connection(con);
1304 set_bit(CLOSED, &con->state); /* in case there's queued work */
1305
1306 mutex_unlock(&con->mutex);
1307 if (con->ops->bad_proto)
1308 con->ops->bad_proto(con);
1309 mutex_lock(&con->mutex);
1310}
1311
Sage Weileed0ef22009-11-10 14:34:36 -08001312static int process_connect(struct ceph_connection *con)
1313{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001314 u64 sup_feat = con->msgr->supported_features;
1315 u64 req_feat = con->msgr->required_features;
Sage Weil04a419f2009-12-23 09:30:21 -08001316 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07001317 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08001318
Sage Weileed0ef22009-11-10 14:34:36 -08001319 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1320
Sage Weil31b80062009-10-06 11:31:13 -07001321 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001322 case CEPH_MSGR_TAG_FEATURES:
1323 pr_err("%s%lld %s feature set mismatch,"
1324 " my %llx < server's %llx, missing %llx\n",
1325 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001326 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001327 sup_feat, server_feat, server_feat & ~sup_feat);
1328 con->error_msg = "missing required protocol features";
1329 fail_protocol(con);
1330 return -1;
1331
Sage Weil31b80062009-10-06 11:31:13 -07001332 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001333 pr_err("%s%lld %s protocol version mismatch,"
1334 " my %d != server's %d\n",
1335 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001336 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07001337 le32_to_cpu(con->out_connect.protocol_version),
1338 le32_to_cpu(con->in_reply.protocol_version));
1339 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001340 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001341 return -1;
1342
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001343 case CEPH_MSGR_TAG_BADAUTHORIZER:
1344 con->auth_retry++;
1345 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1346 con->auth_retry);
1347 if (con->auth_retry == 2) {
1348 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001349 return -1;
1350 }
1351 con->auth_retry = 1;
Sage Weil0da5d702011-05-19 11:21:05 -07001352 ret = prepare_write_connect(con->msgr, con, 0);
1353 if (ret < 0)
1354 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07001355 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001356 break;
Sage Weil31b80062009-10-06 11:31:13 -07001357
1358 case CEPH_MSGR_TAG_RESETSESSION:
1359 /*
1360 * If we connected with a large connect_seq but the peer
1361 * has no record of a session with us (no connection, or
1362 * connect_seq == 0), they will send RESETSESION to indicate
1363 * that they must have reset their session, and may have
1364 * dropped messages.
1365 */
1366 dout("process_connect got RESET peer seq %u\n",
1367 le32_to_cpu(con->in_connect.connect_seq));
1368 pr_err("%s%lld %s connection reset\n",
1369 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001370 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001371 reset_connection(con);
Sage Weileed0ef22009-11-10 14:34:36 -08001372 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001373 prepare_read_connect(con);
1374
1375 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001376 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001377 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1378 if (con->ops->peer_reset)
1379 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001380 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07001381 if (test_bit(CLOSED, &con->state) ||
1382 test_bit(OPENING, &con->state))
1383 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07001384 break;
1385
1386 case CEPH_MSGR_TAG_RETRY_SESSION:
1387 /*
1388 * If we sent a smaller connect_seq than the peer has, try
1389 * again with a larger value.
1390 */
1391 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1392 le32_to_cpu(con->out_connect.connect_seq),
1393 le32_to_cpu(con->in_connect.connect_seq));
1394 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Sage Weileed0ef22009-11-10 14:34:36 -08001395 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001396 prepare_read_connect(con);
1397 break;
1398
1399 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1400 /*
1401 * If we sent a smaller global_seq than the peer has, try
1402 * again with a larger value.
1403 */
Sage Weileed0ef22009-11-10 14:34:36 -08001404 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001405 con->peer_global_seq,
1406 le32_to_cpu(con->in_connect.global_seq));
1407 get_global_seq(con->msgr,
1408 le32_to_cpu(con->in_connect.global_seq));
Sage Weileed0ef22009-11-10 14:34:36 -08001409 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001410 prepare_read_connect(con);
1411 break;
1412
1413 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001414 if (req_feat & ~server_feat) {
1415 pr_err("%s%lld %s protocol feature mismatch,"
1416 " my required %llx > server's %llx, need %llx\n",
1417 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001418 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08001419 req_feat, server_feat, req_feat & ~server_feat);
1420 con->error_msg = "missing required protocol features";
1421 fail_protocol(con);
1422 return -1;
1423 }
Sage Weil31b80062009-10-06 11:31:13 -07001424 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001425 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1426 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07001427 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07001428 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1429 con->peer_global_seq,
1430 le32_to_cpu(con->in_reply.connect_seq),
1431 con->connect_seq);
1432 WARN_ON(con->connect_seq !=
1433 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001434
1435 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
1436 set_bit(LOSSYTX, &con->state);
1437
Sage Weil31b80062009-10-06 11:31:13 -07001438 prepare_read_tag(con);
1439 break;
1440
1441 case CEPH_MSGR_TAG_WAIT:
1442 /*
1443 * If there is a connection race (we are opening
1444 * connections to each other), one of us may just have
1445 * to WAIT. This shouldn't happen if we are the
1446 * client.
1447 */
Sage Weil04177882011-05-12 15:33:17 -07001448 pr_err("process_connect got WAIT as client\n");
1449 con->error_msg = "protocol error, got WAIT as client";
1450 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07001451
1452 default:
1453 pr_err("connect protocol error, will retry\n");
1454 con->error_msg = "protocol error, garbage tag during connect";
1455 return -1;
1456 }
1457 return 0;
1458}
1459
1460
1461/*
1462 * read (part of) an ack
1463 */
1464static int read_partial_ack(struct ceph_connection *con)
1465{
1466 int to = 0;
1467
1468 return read_partial(con, &to, sizeof(con->in_temp_ack),
1469 &con->in_temp_ack);
1470}
1471
1472
1473/*
1474 * We can finally discard anything that's been acked.
1475 */
1476static void process_ack(struct ceph_connection *con)
1477{
1478 struct ceph_msg *m;
1479 u64 ack = le64_to_cpu(con->in_temp_ack);
1480 u64 seq;
1481
Sage Weil31b80062009-10-06 11:31:13 -07001482 while (!list_empty(&con->out_sent)) {
1483 m = list_first_entry(&con->out_sent, struct ceph_msg,
1484 list_head);
1485 seq = le64_to_cpu(m->hdr.seq);
1486 if (seq > ack)
1487 break;
1488 dout("got ack for seq %llu type %d at %p\n", seq,
1489 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07001490 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07001491 ceph_msg_remove(m);
1492 }
Sage Weil31b80062009-10-06 11:31:13 -07001493 prepare_read_tag(con);
1494}
1495
1496
1497
1498
Yehuda Sadeh24504182010-01-08 13:58:34 -08001499static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001500 struct kvec *section,
1501 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001502{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001503 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07001504
Yehuda Sadeh24504182010-01-08 13:58:34 -08001505 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001506
Yehuda Sadeh24504182010-01-08 13:58:34 -08001507 while (section->iov_len < sec_len) {
1508 BUG_ON(section->iov_base == NULL);
1509 left = sec_len - section->iov_len;
1510 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1511 section->iov_len, left);
1512 if (ret <= 0)
1513 return ret;
1514 section->iov_len += ret;
1515 if (section->iov_len == sec_len)
1516 *crc = crc32c(0, section->iov_base,
1517 section->iov_len);
1518 }
1519
1520 return 1;
1521}
1522
1523static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
1524 struct ceph_msg_header *hdr,
1525 int *skip);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001526
1527
1528static int read_partial_message_pages(struct ceph_connection *con,
1529 struct page **pages,
1530 unsigned data_len, int datacrc)
1531{
1532 void *p;
1533 int ret;
1534 int left;
1535
1536 left = min((int)(data_len - con->in_msg_pos.data_pos),
1537 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1538 /* (page) data */
1539 BUG_ON(pages == NULL);
1540 p = kmap(pages[con->in_msg_pos.page]);
1541 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1542 left);
1543 if (ret > 0 && datacrc)
1544 con->in_data_crc =
1545 crc32c(con->in_data_crc,
1546 p + con->in_msg_pos.page_pos, ret);
1547 kunmap(pages[con->in_msg_pos.page]);
1548 if (ret <= 0)
1549 return ret;
1550 con->in_msg_pos.data_pos += ret;
1551 con->in_msg_pos.page_pos += ret;
1552 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1553 con->in_msg_pos.page_pos = 0;
1554 con->in_msg_pos.page++;
1555 }
1556
1557 return ret;
1558}
1559
1560#ifdef CONFIG_BLOCK
1561static int read_partial_message_bio(struct ceph_connection *con,
1562 struct bio **bio_iter, int *bio_seg,
1563 unsigned data_len, int datacrc)
1564{
1565 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1566 void *p;
1567 int ret, left;
1568
1569 if (IS_ERR(bv))
1570 return PTR_ERR(bv);
1571
1572 left = min((int)(data_len - con->in_msg_pos.data_pos),
1573 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1574
1575 p = kmap(bv->bv_page) + bv->bv_offset;
1576
1577 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1578 left);
1579 if (ret > 0 && datacrc)
1580 con->in_data_crc =
1581 crc32c(con->in_data_crc,
1582 p + con->in_msg_pos.page_pos, ret);
1583 kunmap(bv->bv_page);
1584 if (ret <= 0)
1585 return ret;
1586 con->in_msg_pos.data_pos += ret;
1587 con->in_msg_pos.page_pos += ret;
1588 if (con->in_msg_pos.page_pos == bv->bv_len) {
1589 con->in_msg_pos.page_pos = 0;
1590 iter_bio_next(bio_iter, bio_seg);
1591 }
1592
1593 return ret;
1594}
1595#endif
1596
Sage Weil31b80062009-10-06 11:31:13 -07001597/*
1598 * read (part of) a message.
1599 */
1600static int read_partial_message(struct ceph_connection *con)
1601{
1602 struct ceph_msg *m = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001603 int ret;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001604 int to, left;
Sage Weilc5c6b192010-11-09 12:40:00 -08001605 unsigned front_len, middle_len, data_len;
Sage Weil31b80062009-10-06 11:31:13 -07001606 int datacrc = con->msgr->nocrc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001607 int skip;
Sage Weilae187562010-04-22 07:47:01 -07001608 u64 seq;
Sage Weil31b80062009-10-06 11:31:13 -07001609
1610 dout("read_partial_message con %p msg %p\n", con, m);
1611
1612 /* header */
1613 while (con->in_base_pos < sizeof(con->in_hdr)) {
1614 left = sizeof(con->in_hdr) - con->in_base_pos;
1615 ret = ceph_tcp_recvmsg(con->sock,
1616 (char *)&con->in_hdr + con->in_base_pos,
1617 left);
1618 if (ret <= 0)
1619 return ret;
1620 con->in_base_pos += ret;
1621 if (con->in_base_pos == sizeof(con->in_hdr)) {
1622 u32 crc = crc32c(0, (void *)&con->in_hdr,
1623 sizeof(con->in_hdr) - sizeof(con->in_hdr.crc));
1624 if (crc != le32_to_cpu(con->in_hdr.crc)) {
1625 pr_err("read_partial_message bad hdr "
1626 " crc %u != expected %u\n",
1627 crc, con->in_hdr.crc);
1628 return -EBADMSG;
1629 }
1630 }
1631 }
Sage Weil31b80062009-10-06 11:31:13 -07001632 front_len = le32_to_cpu(con->in_hdr.front_len);
1633 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1634 return -EIO;
1635 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1636 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1637 return -EIO;
1638 data_len = le32_to_cpu(con->in_hdr.data_len);
1639 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1640 return -EIO;
1641
Sage Weilae187562010-04-22 07:47:01 -07001642 /* verify seq# */
1643 seq = le64_to_cpu(con->in_hdr.seq);
1644 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07001645 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07001646 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001647 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07001648 seq, con->in_seq + 1);
1649 con->in_base_pos = -front_len - middle_len - data_len -
1650 sizeof(m->footer);
1651 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weilae187562010-04-22 07:47:01 -07001652 return 0;
1653 } else if ((s64)seq - (s64)con->in_seq > 1) {
1654 pr_err("read_partial_message bad seq %lld expected %lld\n",
1655 seq, con->in_seq + 1);
1656 con->error_msg = "bad message sequence # for incoming message";
1657 return -EBADMSG;
1658 }
1659
Sage Weil31b80062009-10-06 11:31:13 -07001660 /* allocate message? */
1661 if (!con->in_msg) {
1662 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1663 con->in_hdr.front_len, con->in_hdr.data_len);
Sage Weilae32be32010-06-13 10:30:19 -07001664 skip = 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001665 con->in_msg = ceph_alloc_msg(con, &con->in_hdr, &skip);
1666 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07001667 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001668 dout("alloc_msg said skip message\n");
Sage Weilae32be32010-06-13 10:30:19 -07001669 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001670 con->in_base_pos = -front_len - middle_len - data_len -
1671 sizeof(m->footer);
1672 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001673 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001674 return 0;
1675 }
Sage Weila79832f2010-04-01 16:06:19 -07001676 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001677 con->error_msg =
1678 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001679 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001680 }
1681 m = con->in_msg;
1682 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001683 if (m->middle)
1684 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001685
1686 con->in_msg_pos.page = 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001687 if (m->pages)
Sage Weilc5c6b192010-11-09 12:40:00 -08001688 con->in_msg_pos.page_pos = m->page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001689 else
1690 con->in_msg_pos.page_pos = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001691 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001692 }
1693
1694 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001695 ret = read_partial_message_section(con, &m->front, front_len,
1696 &con->in_front_crc);
1697 if (ret <= 0)
1698 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001699
1700 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001701 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07001702 ret = read_partial_message_section(con, &m->middle->vec,
1703 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001704 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001705 if (ret <= 0)
1706 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001707 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001708#ifdef CONFIG_BLOCK
1709 if (m->bio && !m->bio_iter)
1710 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1711#endif
Sage Weil31b80062009-10-06 11:31:13 -07001712
1713 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001714 while (con->in_msg_pos.data_pos < data_len) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001715 if (m->pages) {
1716 ret = read_partial_message_pages(con, m->pages,
1717 data_len, datacrc);
1718 if (ret <= 0)
1719 return ret;
1720#ifdef CONFIG_BLOCK
1721 } else if (m->bio) {
1722
1723 ret = read_partial_message_bio(con,
1724 &m->bio_iter, &m->bio_seg,
1725 data_len, datacrc);
1726 if (ret <= 0)
1727 return ret;
1728#endif
1729 } else {
1730 BUG_ON(1);
Sage Weil31b80062009-10-06 11:31:13 -07001731 }
1732 }
1733
Sage Weil31b80062009-10-06 11:31:13 -07001734 /* footer */
1735 to = sizeof(m->hdr) + sizeof(m->footer);
1736 while (con->in_base_pos < to) {
1737 left = to - con->in_base_pos;
1738 ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer +
1739 (con->in_base_pos - sizeof(m->hdr)),
1740 left);
1741 if (ret <= 0)
1742 return ret;
1743 con->in_base_pos += ret;
1744 }
1745 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1746 m, front_len, m->footer.front_crc, middle_len,
1747 m->footer.middle_crc, data_len, m->footer.data_crc);
1748
1749 /* crc ok? */
1750 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1751 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1752 m, con->in_front_crc, m->footer.front_crc);
1753 return -EBADMSG;
1754 }
1755 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1756 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1757 m, con->in_middle_crc, m->footer.middle_crc);
1758 return -EBADMSG;
1759 }
1760 if (datacrc &&
1761 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1762 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1763 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1764 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1765 return -EBADMSG;
1766 }
1767
1768 return 1; /* done! */
1769}
1770
1771/*
1772 * Process message. This happens in the worker thread. The callback should
1773 * be careful not to do anything that waits on other incoming messages or it
1774 * may deadlock.
1775 */
1776static void process_message(struct ceph_connection *con)
1777{
Sage Weil5e095e82009-12-14 14:30:34 -08001778 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001779
Sage Weil5e095e82009-12-14 14:30:34 -08001780 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001781 con->in_msg = NULL;
1782
1783 /* if first message, set peer_name */
1784 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07001785 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001786
Sage Weil31b80062009-10-06 11:31:13 -07001787 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001788 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001789
1790 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1791 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07001792 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07001793 le16_to_cpu(msg->hdr.type),
1794 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1795 le32_to_cpu(msg->hdr.front_len),
1796 le32_to_cpu(msg->hdr.data_len),
1797 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1798 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001799
1800 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001801 prepare_read_tag(con);
1802}
1803
1804
1805/*
1806 * Write something to the socket. Called in a worker thread when the
1807 * socket appears to be writeable and we have something ready to send.
1808 */
1809static int try_write(struct ceph_connection *con)
1810{
1811 struct ceph_messenger *msgr = con->msgr;
1812 int ret = 1;
1813
1814 dout("try_write start %p state %lu nref %d\n", con, con->state,
1815 atomic_read(&con->nref));
1816
Sage Weil31b80062009-10-06 11:31:13 -07001817more:
1818 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1819
1820 /* open the socket first? */
1821 if (con->sock == NULL) {
Sage Weileed0ef22009-11-10 14:34:36 -08001822 prepare_write_banner(msgr, con);
1823 prepare_write_connect(msgr, con, 1);
1824 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001825 set_bit(CONNECTING, &con->state);
Sage Weileed0ef22009-11-10 14:34:36 -08001826 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001827
Sage Weilcf3e5c42009-12-11 09:48:05 -08001828 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001829 con->in_tag = CEPH_MSGR_TAG_READY;
1830 dout("try_write initiating connect on %p new state %lu\n",
1831 con, con->state);
1832 con->sock = ceph_tcp_connect(con);
1833 if (IS_ERR(con->sock)) {
1834 con->sock = NULL;
1835 con->error_msg = "connect error";
1836 ret = -1;
1837 goto out;
1838 }
1839 }
1840
1841more_kvec:
1842 /* kvec data queued? */
1843 if (con->out_skip) {
1844 ret = write_partial_skip(con);
1845 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001846 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001847 }
1848 if (con->out_kvec_left) {
1849 ret = write_partial_kvec(con);
1850 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08001851 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001852 }
1853
1854 /* msg pages? */
1855 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001856 if (con->out_msg_done) {
1857 ceph_msg_put(con->out_msg);
1858 con->out_msg = NULL; /* we're done with this one */
1859 goto do_next;
1860 }
1861
Sage Weil31b80062009-10-06 11:31:13 -07001862 ret = write_partial_msg_pages(con);
1863 if (ret == 1)
1864 goto more_kvec; /* we need to send the footer, too! */
1865 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08001866 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001867 if (ret < 0) {
1868 dout("try_write write_partial_msg_pages err %d\n",
1869 ret);
Sage Weil42961d22011-01-25 08:19:34 -08001870 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001871 }
1872 }
1873
Sage Weilc86a2932009-12-14 14:04:30 -08001874do_next:
Sage Weil31b80062009-10-06 11:31:13 -07001875 if (!test_bit(CONNECTING, &con->state)) {
1876 /* is anything else pending? */
1877 if (!list_empty(&con->out_queue)) {
1878 prepare_write_message(con);
1879 goto more;
1880 }
1881 if (con->in_seq > con->in_seq_acked) {
1882 prepare_write_ack(con);
1883 goto more;
1884 }
1885 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
1886 prepare_write_keepalive(con);
1887 goto more;
1888 }
1889 }
1890
1891 /* Nothing to do! */
1892 clear_bit(WRITE_PENDING, &con->state);
1893 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07001894 ret = 0;
1895out:
Sage Weil42961d22011-01-25 08:19:34 -08001896 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07001897 return ret;
1898}
1899
1900
1901
1902/*
1903 * Read what we can from the socket.
1904 */
1905static int try_read(struct ceph_connection *con)
1906{
Sage Weil31b80062009-10-06 11:31:13 -07001907 int ret = -1;
1908
1909 if (!con->sock)
1910 return 0;
1911
1912 if (test_bit(STANDBY, &con->state))
1913 return 0;
1914
1915 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08001916
Sage Weil31b80062009-10-06 11:31:13 -07001917more:
1918 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
1919 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07001920
1921 /*
1922 * process_connect and process_message drop and re-take
1923 * con->mutex. make sure we handle a racing close or reopen.
1924 */
1925 if (test_bit(CLOSED, &con->state) ||
1926 test_bit(OPENING, &con->state)) {
1927 ret = -EAGAIN;
1928 goto out;
1929 }
1930
Sage Weil31b80062009-10-06 11:31:13 -07001931 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08001932 if (!test_bit(NEGOTIATING, &con->state)) {
1933 dout("try_read connecting\n");
1934 ret = read_partial_banner(con);
1935 if (ret <= 0)
Sage Weileed0ef22009-11-10 14:34:36 -08001936 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08001937 ret = process_banner(con);
1938 if (ret < 0)
1939 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -08001940 }
Sage Weil31b80062009-10-06 11:31:13 -07001941 ret = read_partial_connect(con);
1942 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07001943 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08001944 ret = process_connect(con);
1945 if (ret < 0)
1946 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001947 goto more;
1948 }
1949
1950 if (con->in_base_pos < 0) {
1951 /*
1952 * skipping + discarding content.
1953 *
1954 * FIXME: there must be a better way to do this!
1955 */
1956 static char buf[1024];
1957 int skip = min(1024, -con->in_base_pos);
1958 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
1959 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
1960 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08001961 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001962 con->in_base_pos += ret;
1963 if (con->in_base_pos)
1964 goto more;
1965 }
1966 if (con->in_tag == CEPH_MSGR_TAG_READY) {
1967 /*
1968 * what's next?
1969 */
1970 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
1971 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08001972 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001973 dout("try_read got tag %d\n", (int)con->in_tag);
1974 switch (con->in_tag) {
1975 case CEPH_MSGR_TAG_MSG:
1976 prepare_read_message(con);
1977 break;
1978 case CEPH_MSGR_TAG_ACK:
1979 prepare_read_ack(con);
1980 break;
1981 case CEPH_MSGR_TAG_CLOSE:
1982 set_bit(CLOSED, &con->state); /* fixme */
Sage Weil98bdb0a2011-01-25 08:17:48 -08001983 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001984 default:
1985 goto bad_tag;
1986 }
1987 }
1988 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
1989 ret = read_partial_message(con);
1990 if (ret <= 0) {
1991 switch (ret) {
1992 case -EBADMSG:
1993 con->error_msg = "bad crc";
1994 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08001995 break;
Sage Weil31b80062009-10-06 11:31:13 -07001996 case -EIO:
1997 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08001998 break;
Sage Weil31b80062009-10-06 11:31:13 -07001999 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002000 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002001 }
2002 if (con->in_tag == CEPH_MSGR_TAG_READY)
2003 goto more;
2004 process_message(con);
2005 goto more;
2006 }
2007 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2008 ret = read_partial_ack(con);
2009 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002010 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002011 process_ack(con);
2012 goto more;
2013 }
2014
Sage Weil31b80062009-10-06 11:31:13 -07002015out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002016 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002017 return ret;
2018
2019bad_tag:
2020 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2021 con->error_msg = "protocol error, garbage tag";
2022 ret = -1;
2023 goto out;
2024}
2025
2026
2027/*
2028 * Atomically queue work on a connection. Bump @con reference to
2029 * avoid races with connection teardown.
Sage Weil31b80062009-10-06 11:31:13 -07002030 */
2031static void queue_con(struct ceph_connection *con)
2032{
2033 if (test_bit(DEAD, &con->state)) {
2034 dout("queue_con %p ignoring: DEAD\n",
2035 con);
2036 return;
2037 }
2038
2039 if (!con->ops->get(con)) {
2040 dout("queue_con %p ref count 0\n", con);
2041 return;
2042 }
2043
Tejun Heof363e45fd12011-01-03 14:49:46 +01002044 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
Sage Weil31b80062009-10-06 11:31:13 -07002045 dout("queue_con %p - already queued\n", con);
2046 con->ops->put(con);
2047 } else {
2048 dout("queue_con %p\n", con);
2049 }
2050}
2051
2052/*
2053 * Do some work on a connection. Drop a connection ref when we're done.
2054 */
2055static void con_work(struct work_struct *work)
2056{
2057 struct ceph_connection *con = container_of(work, struct ceph_connection,
2058 work.work);
Sage Weil0da5d702011-05-19 11:21:05 -07002059 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002060
Sage Weil9dd46582010-04-28 13:51:50 -07002061 mutex_lock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002062restart:
Sage Weil60bf8bf2011-03-04 12:24:28 -08002063 if (test_and_clear_bit(BACKOFF, &con->state)) {
2064 dout("con_work %p backing off\n", con);
2065 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2066 round_jiffies_relative(con->delay))) {
2067 dout("con_work %p backoff %lu\n", con, con->delay);
2068 mutex_unlock(&con->mutex);
2069 return;
2070 } else {
2071 con->ops->put(con);
2072 dout("con_work %p FAILED to back off %lu\n", con,
2073 con->delay);
2074 }
2075 }
Sage Weil9dd46582010-04-28 13:51:50 -07002076
Sage Weile00de342011-03-04 12:25:05 -08002077 if (test_bit(STANDBY, &con->state)) {
2078 dout("con_work %p STANDBY\n", con);
2079 goto done;
2080 }
Sage Weil31b80062009-10-06 11:31:13 -07002081 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2082 dout("con_work CLOSED\n");
2083 con_close_socket(con);
2084 goto done;
2085 }
2086 if (test_and_clear_bit(OPENING, &con->state)) {
2087 /* reopen w/ new peer */
2088 dout("con_work OPENING\n");
2089 con_close_socket(con);
2090 }
2091
Sage Weil0da5d702011-05-19 11:21:05 -07002092 if (test_and_clear_bit(SOCK_CLOSED, &con->state))
2093 goto fault;
2094
2095 ret = try_read(con);
2096 if (ret == -EAGAIN)
2097 goto restart;
2098 if (ret < 0)
2099 goto fault;
2100
2101 ret = try_write(con);
2102 if (ret == -EAGAIN)
2103 goto restart;
2104 if (ret < 0)
2105 goto fault;
Sage Weil31b80062009-10-06 11:31:13 -07002106
2107done:
Sage Weil9dd46582010-04-28 13:51:50 -07002108 mutex_unlock(&con->mutex);
Sage Weil9dd46582010-04-28 13:51:50 -07002109done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07002110 con->ops->put(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002111 return;
2112
2113fault:
2114 mutex_unlock(&con->mutex);
2115 ceph_fault(con); /* error/fault path */
2116 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07002117}
2118
2119
2120/*
2121 * Generic error/fault handler. A retry mechanism is used with
2122 * exponential backoff
2123 */
2124static void ceph_fault(struct ceph_connection *con)
2125{
2126 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002127 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002128 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002129 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002130
2131 if (test_bit(LOSSYTX, &con->state)) {
2132 dout("fault on LOSSYTX channel\n");
2133 goto out;
2134 }
2135
Sage Weilec302642009-12-22 10:43:42 -08002136 mutex_lock(&con->mutex);
Sage Weil91e45ce32010-02-15 12:05:09 -08002137 if (test_bit(CLOSED, &con->state))
2138 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08002139
Sage Weil31b80062009-10-06 11:31:13 -07002140 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002141
2142 if (con->in_msg) {
2143 ceph_msg_put(con->in_msg);
2144 con->in_msg = NULL;
2145 }
Sage Weil31b80062009-10-06 11:31:13 -07002146
Sage Weile80a52d2010-02-25 12:40:45 -08002147 /* Requeue anything that hasn't been acked */
2148 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002149
Sage Weile76661d2011-03-03 10:10:15 -08002150 /* If there are no messages queued or keepalive pending, place
2151 * the connection in a STANDBY state */
2152 if (list_empty(&con->out_queue) &&
2153 !test_bit(KEEPALIVE_PENDING, &con->state)) {
Sage Weile00de342011-03-04 12:25:05 -08002154 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
2155 clear_bit(WRITE_PENDING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07002156 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08002157 } else {
2158 /* retry after a delay. */
2159 if (con->delay == 0)
2160 con->delay = BASE_DELAY_INTERVAL;
2161 else if (con->delay < MAX_DELAY_INTERVAL)
2162 con->delay *= 2;
Sage Weile80a52d2010-02-25 12:40:45 -08002163 con->ops->get(con);
2164 if (queue_delayed_work(ceph_msgr_wq, &con->work,
Sage Weil60bf8bf2011-03-04 12:24:28 -08002165 round_jiffies_relative(con->delay))) {
2166 dout("fault queued %p delay %lu\n", con, con->delay);
2167 } else {
Sage Weile80a52d2010-02-25 12:40:45 -08002168 con->ops->put(con);
Sage Weil60bf8bf2011-03-04 12:24:28 -08002169 dout("fault failed to queue %p delay %lu, backoff\n",
2170 con, con->delay);
2171 /*
2172 * In many cases we see a socket state change
2173 * while con_work is running and end up
2174 * queuing (non-delayed) work, such that we
2175 * can't backoff with a delay. Set a flag so
2176 * that when con_work restarts we schedule the
2177 * delay then.
2178 */
2179 set_bit(BACKOFF, &con->state);
2180 }
Sage Weil31b80062009-10-06 11:31:13 -07002181 }
2182
Sage Weil91e45ce32010-02-15 12:05:09 -08002183out_unlock:
2184 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002185out:
Sage Weil161fd652010-02-25 12:38:57 -08002186 /*
2187 * in case we faulted due to authentication, invalidate our
2188 * current tickets so that we can get new ones.
Sage Weil213c99e2010-08-03 10:25:11 -07002189 */
Sage Weil161fd652010-02-25 12:38:57 -08002190 if (con->auth_retry && con->ops->invalidate_authorizer) {
2191 dout("calling invalidate_authorizer()\n");
2192 con->ops->invalidate_authorizer(con);
2193 }
2194
Sage Weil31b80062009-10-06 11:31:13 -07002195 if (con->ops->fault)
2196 con->ops->fault(con);
2197}
2198
2199
2200
2201/*
2202 * create a new messenger instance
2203 */
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002204struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr,
2205 u32 supported_features,
2206 u32 required_features)
Sage Weil31b80062009-10-06 11:31:13 -07002207{
2208 struct ceph_messenger *msgr;
2209
2210 msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
2211 if (msgr == NULL)
2212 return ERR_PTR(-ENOMEM);
2213
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002214 msgr->supported_features = supported_features;
2215 msgr->required_features = required_features;
2216
Sage Weil31b80062009-10-06 11:31:13 -07002217 spin_lock_init(&msgr->global_seq_lock);
2218
2219 /* the zero page is needed if a request is "canceled" while the message
2220 * is being written over the socket */
Yehuda Sadeh31459fe2010-03-17 13:54:02 -07002221 msgr->zero_page = __page_cache_alloc(GFP_KERNEL | __GFP_ZERO);
Sage Weil31b80062009-10-06 11:31:13 -07002222 if (!msgr->zero_page) {
2223 kfree(msgr);
2224 return ERR_PTR(-ENOMEM);
2225 }
2226 kmap(msgr->zero_page);
2227
2228 if (myaddr)
2229 msgr->inst.addr = *myaddr;
2230
2231 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08002232 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08002233 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08002234 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07002235
2236 dout("messenger_create %p\n", msgr);
2237 return msgr;
2238}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002239EXPORT_SYMBOL(ceph_messenger_create);
Sage Weil31b80062009-10-06 11:31:13 -07002240
2241void ceph_messenger_destroy(struct ceph_messenger *msgr)
2242{
2243 dout("destroy %p\n", msgr);
2244 kunmap(msgr->zero_page);
2245 __free_page(msgr->zero_page);
2246 kfree(msgr);
2247 dout("destroyed messenger %p\n", msgr);
2248}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002249EXPORT_SYMBOL(ceph_messenger_destroy);
Sage Weil31b80062009-10-06 11:31:13 -07002250
Sage Weile00de342011-03-04 12:25:05 -08002251static void clear_standby(struct ceph_connection *con)
2252{
2253 /* come back from STANDBY? */
2254 if (test_and_clear_bit(STANDBY, &con->state)) {
2255 mutex_lock(&con->mutex);
2256 dout("clear_standby %p and ++connect_seq\n", con);
2257 con->connect_seq++;
2258 WARN_ON(test_bit(WRITE_PENDING, &con->state));
2259 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->state));
2260 mutex_unlock(&con->mutex);
2261 }
2262}
2263
Sage Weil31b80062009-10-06 11:31:13 -07002264/*
2265 * Queue up an outgoing message on the given connection.
2266 */
2267void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2268{
2269 if (test_bit(CLOSED, &con->state)) {
2270 dout("con_send %p closed, dropping %p\n", con, msg);
2271 ceph_msg_put(msg);
2272 return;
2273 }
2274
2275 /* set src+dst */
Sage Weildbad1852010-03-25 15:45:38 -07002276 msg->hdr.src = con->msgr->inst.name;
Sage Weil31b80062009-10-06 11:31:13 -07002277
Sage Weil3ca02ef2010-03-01 15:25:00 -08002278 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2279
Sage Weile84346b2010-05-11 21:20:38 -07002280 msg->needs_out_seq = true;
2281
Sage Weil31b80062009-10-06 11:31:13 -07002282 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08002283 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002284 BUG_ON(!list_empty(&msg->list_head));
2285 list_add_tail(&msg->list_head, &con->out_queue);
2286 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2287 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2288 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2289 le32_to_cpu(msg->hdr.front_len),
2290 le32_to_cpu(msg->hdr.middle_len),
2291 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002292 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002293
2294 /* if there wasn't anything waiting to send before, queue
2295 * new work */
Sage Weile00de342011-03-04 12:25:05 -08002296 clear_standby(con);
Sage Weil31b80062009-10-06 11:31:13 -07002297 if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2298 queue_con(con);
2299}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002300EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07002301
2302/*
2303 * Revoke a message that was previously queued for send
2304 */
2305void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
2306{
Sage Weilec302642009-12-22 10:43:42 -08002307 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002308 if (!list_empty(&msg->list_head)) {
Sage Weiled98ada2010-07-05 12:15:14 -07002309 dout("con_revoke %p msg %p - was on queue\n", con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07002310 list_del_init(&msg->list_head);
2311 ceph_msg_put(msg);
2312 msg->hdr.seq = 0;
Sage Weiled98ada2010-07-05 12:15:14 -07002313 }
2314 if (con->out_msg == msg) {
2315 dout("con_revoke %p msg %p - was sending\n", con, msg);
2316 con->out_msg = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002317 if (con->out_kvec_is_msg) {
2318 con->out_skip = con->out_kvec_bytes;
2319 con->out_kvec_is_msg = false;
2320 }
Sage Weiled98ada2010-07-05 12:15:14 -07002321 ceph_msg_put(msg);
2322 msg->hdr.seq = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002323 }
Sage Weilec302642009-12-22 10:43:42 -08002324 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002325}
2326
2327/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002328 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002329 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002330void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002331{
2332 mutex_lock(&con->mutex);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002333 if (con->in_msg && con->in_msg == msg) {
2334 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2335 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002336 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2337
2338 /* skip rest of message */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002339 dout("con_revoke_pages %p msg %p revoked\n", con, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002340 con->in_base_pos = con->in_base_pos -
2341 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002342 front_len -
2343 middle_len -
2344 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002345 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002346 ceph_msg_put(con->in_msg);
2347 con->in_msg = NULL;
2348 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002349 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002350 } else {
2351 dout("con_revoke_pages %p msg %p pages %p no-op\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002352 con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002353 }
2354 mutex_unlock(&con->mutex);
2355}
2356
2357/*
Sage Weil31b80062009-10-06 11:31:13 -07002358 * Queue a keepalive byte to ensure the tcp connection is alive.
2359 */
2360void ceph_con_keepalive(struct ceph_connection *con)
2361{
Sage Weile00de342011-03-04 12:25:05 -08002362 dout("con_keepalive %p\n", con);
2363 clear_standby(con);
Sage Weil31b80062009-10-06 11:31:13 -07002364 if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
2365 test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2366 queue_con(con);
2367}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002368EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07002369
2370
2371/*
2372 * construct a new message with given type, size
2373 * the new msg has a ref count of 1.
2374 */
Sage Weilb61c2762011-08-09 15:03:46 -07002375struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2376 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07002377{
2378 struct ceph_msg *m;
2379
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002380 m = kmalloc(sizeof(*m), flags);
Sage Weil31b80062009-10-06 11:31:13 -07002381 if (m == NULL)
2382 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002383 kref_init(&m->kref);
Sage Weil31b80062009-10-06 11:31:13 -07002384 INIT_LIST_HEAD(&m->list_head);
2385
Sage Weil45c6ceb2010-05-11 15:01:51 -07002386 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002387 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002388 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2389 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002390 m->hdr.front_len = cpu_to_le32(front_len);
2391 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002392 m->hdr.data_len = 0;
2393 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002394 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002395 m->footer.front_crc = 0;
2396 m->footer.middle_crc = 0;
2397 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002398 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002399 m->front_max = front_len;
2400 m->front_is_vmalloc = false;
2401 m->more_to_follow = false;
Jim Schuttc0d5f9d2011-09-16 08:27:31 -06002402 m->ack_stamp = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002403 m->pool = NULL;
2404
Henry C Changca208922011-05-03 02:29:56 +00002405 /* middle */
2406 m->middle = NULL;
2407
2408 /* data */
2409 m->nr_pages = 0;
2410 m->page_alignment = 0;
2411 m->pages = NULL;
2412 m->pagelist = NULL;
2413 m->bio = NULL;
2414 m->bio_iter = NULL;
2415 m->bio_seg = 0;
2416 m->trail = NULL;
2417
Sage Weil31b80062009-10-06 11:31:13 -07002418 /* front */
2419 if (front_len) {
2420 if (front_len > PAGE_CACHE_SIZE) {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002421 m->front.iov_base = __vmalloc(front_len, flags,
Sage Weil31b80062009-10-06 11:31:13 -07002422 PAGE_KERNEL);
2423 m->front_is_vmalloc = true;
2424 } else {
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002425 m->front.iov_base = kmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07002426 }
2427 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07002428 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07002429 front_len);
2430 goto out2;
2431 }
2432 } else {
2433 m->front.iov_base = NULL;
2434 }
2435 m->front.iov_len = front_len;
2436
Sage Weilbb257662010-04-01 16:07:23 -07002437 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002438 return m;
2439
2440out2:
2441 ceph_msg_put(m);
2442out:
Sage Weilb61c2762011-08-09 15:03:46 -07002443 if (!can_fail) {
2444 pr_err("msg_new can't create type %d front %d\n", type,
2445 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07002446 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07002447 } else {
2448 dout("msg_new can't create type %d front %d\n", type,
2449 front_len);
2450 }
Sage Weila79832f2010-04-01 16:06:19 -07002451 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002452}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002453EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07002454
2455/*
Sage Weil31b80062009-10-06 11:31:13 -07002456 * Allocate "middle" portion of a message, if it is needed and wasn't
2457 * allocated by alloc_msg. This allows us to read a small fixed-size
2458 * per-type header in the front and then gracefully fail (i.e.,
2459 * propagate the error to the caller based on info in the front) when
2460 * the middle is too large.
2461 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002462static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002463{
2464 int type = le16_to_cpu(msg->hdr.type);
2465 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2466
2467 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2468 ceph_msg_type_name(type), middle_len);
2469 BUG_ON(!middle_len);
2470 BUG_ON(msg->middle);
2471
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002472 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002473 if (!msg->middle)
2474 return -ENOMEM;
2475 return 0;
2476}
2477
Yehuda Sadeh24504182010-01-08 13:58:34 -08002478/*
2479 * Generic message allocator, for incoming messages.
2480 */
2481static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
2482 struct ceph_msg_header *hdr,
2483 int *skip)
2484{
2485 int type = le16_to_cpu(hdr->type);
2486 int front_len = le32_to_cpu(hdr->front_len);
2487 int middle_len = le32_to_cpu(hdr->middle_len);
2488 struct ceph_msg *msg = NULL;
2489 int ret;
2490
2491 if (con->ops->alloc_msg) {
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002492 mutex_unlock(&con->mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002493 msg = con->ops->alloc_msg(con, hdr, skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002494 mutex_lock(&con->mutex);
Sage Weila79832f2010-04-01 16:06:19 -07002495 if (!msg || *skip)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002496 return NULL;
2497 }
2498 if (!msg) {
2499 *skip = 0;
Sage Weilb61c2762011-08-09 15:03:46 -07002500 msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002501 if (!msg) {
2502 pr_err("unable to allocate msg type %d len %d\n",
2503 type, front_len);
Sage Weila79832f2010-04-01 16:06:19 -07002504 return NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002505 }
Sage Weilc5c6b192010-11-09 12:40:00 -08002506 msg->page_alignment = le16_to_cpu(hdr->data_off);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002507 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002508 memcpy(&msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002509
Sage Weilbb257662010-04-01 16:07:23 -07002510 if (middle_len && !msg->middle) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002511 ret = ceph_alloc_middle(con, msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002512 if (ret < 0) {
2513 ceph_msg_put(msg);
Sage Weila79832f2010-04-01 16:06:19 -07002514 return NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002515 }
2516 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002517
Yehuda Sadeh24504182010-01-08 13:58:34 -08002518 return msg;
2519}
2520
Sage Weil31b80062009-10-06 11:31:13 -07002521
2522/*
2523 * Free a generically kmalloc'd message.
2524 */
2525void ceph_msg_kfree(struct ceph_msg *m)
2526{
2527 dout("msg_kfree %p\n", m);
2528 if (m->front_is_vmalloc)
2529 vfree(m->front.iov_base);
2530 else
2531 kfree(m->front.iov_base);
2532 kfree(m);
2533}
2534
2535/*
2536 * Drop a msg ref. Destroy as needed.
2537 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002538void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002539{
Sage Weilc2e552e2009-12-07 15:55:05 -08002540 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002541
Sage Weilc2e552e2009-12-07 15:55:05 -08002542 dout("ceph_msg_put last one on %p\n", m);
2543 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002544
Sage Weilc2e552e2009-12-07 15:55:05 -08002545 /* drop middle, data, if any */
2546 if (m->middle) {
2547 ceph_buffer_put(m->middle);
2548 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002549 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002550 m->nr_pages = 0;
2551 m->pages = NULL;
2552
Sage Weil58bb3b32009-12-23 12:12:31 -08002553 if (m->pagelist) {
2554 ceph_pagelist_release(m->pagelist);
2555 kfree(m->pagelist);
2556 m->pagelist = NULL;
2557 }
2558
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002559 m->trail = NULL;
2560
Sage Weilc2e552e2009-12-07 15:55:05 -08002561 if (m->pool)
2562 ceph_msgpool_put(m->pool, m);
2563 else
2564 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002565}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002566EXPORT_SYMBOL(ceph_msg_last_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08002567
2568void ceph_msg_dump(struct ceph_msg *msg)
2569{
2570 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2571 msg->front_max, msg->nr_pages);
2572 print_hex_dump(KERN_DEBUG, "header: ",
2573 DUMP_PREFIX_OFFSET, 16, 1,
2574 &msg->hdr, sizeof(msg->hdr), true);
2575 print_hex_dump(KERN_DEBUG, " front: ",
2576 DUMP_PREFIX_OFFSET, 16, 1,
2577 msg->front.iov_base, msg->front.iov_len, true);
2578 if (msg->middle)
2579 print_hex_dump(KERN_DEBUG, "middle: ",
2580 DUMP_PREFIX_OFFSET, 16, 1,
2581 msg->middle->vec.iov_base,
2582 msg->middle->vec.iov_len, true);
2583 print_hex_dump(KERN_DEBUG, "footer: ",
2584 DUMP_PREFIX_OFFSET, 16, 1,
2585 &msg->footer, sizeof(msg->footer), true);
2586}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002587EXPORT_SYMBOL(ceph_msg_dump);