]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/ceph/messenger.c
ceph: fix message memory leak, uninitialized variable
[net-next-2.6.git] / fs / ceph / messenger.c
CommitLineData
31b8006e
SW
1#include "ceph_debug.h"
2
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>
5a0e3ad6 9#include <linux/slab.h>
31b8006e
SW
10#include <linux/socket.h>
11#include <linux/string.h>
12#include <net/tcp.h>
13
14#include "super.h"
15#include "messenger.h"
63f2d211 16#include "decode.h"
58bb3b37 17#include "pagelist.h"
31b8006e
SW
18
19/*
20 * Ceph uses the messenger to exchange ceph_msg messages with other
21 * hosts in the system. The messenger provides ordered and reliable
22 * delivery. We tolerate TCP disconnects by reconnecting (with
23 * exponential backoff) in the case of a fault (disconnection, bad
24 * crc, protocol error). Acks allow sent messages to be discarded by
25 * the sender.
26 */
27
28/* static tag bytes (protocol control messages) */
29static char tag_msg = CEPH_MSGR_TAG_MSG;
30static char tag_ack = CEPH_MSGR_TAG_ACK;
31static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
32
a6a5349d
SW
33#ifdef CONFIG_LOCKDEP
34static struct lock_class_key socket_class;
35#endif
36
31b8006e
SW
37
38static void queue_con(struct ceph_connection *con);
39static void con_work(struct work_struct *);
40static void ceph_fault(struct ceph_connection *con);
41
31b8006e
SW
42/*
43 * nicely render a sockaddr as a string.
44 */
45#define MAX_ADDR_STR 20
46static char addr_str[MAX_ADDR_STR][40];
47static DEFINE_SPINLOCK(addr_str_lock);
48static int last_addr_str;
49
50const char *pr_addr(const struct sockaddr_storage *ss)
51{
52 int i;
53 char *s;
54 struct sockaddr_in *in4 = (void *)ss;
55 unsigned char *quad = (void *)&in4->sin_addr.s_addr;
56 struct sockaddr_in6 *in6 = (void *)ss;
57
58 spin_lock(&addr_str_lock);
59 i = last_addr_str++;
60 if (last_addr_str == MAX_ADDR_STR)
61 last_addr_str = 0;
62 spin_unlock(&addr_str_lock);
63 s = addr_str[i];
64
65 switch (ss->ss_family) {
66 case AF_INET:
67 sprintf(s, "%u.%u.%u.%u:%u",
68 (unsigned int)quad[0],
69 (unsigned int)quad[1],
70 (unsigned int)quad[2],
71 (unsigned int)quad[3],
72 (unsigned int)ntohs(in4->sin_port));
73 break;
74
75 case AF_INET6:
76 sprintf(s, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%u",
77 in6->sin6_addr.s6_addr16[0],
78 in6->sin6_addr.s6_addr16[1],
79 in6->sin6_addr.s6_addr16[2],
80 in6->sin6_addr.s6_addr16[3],
81 in6->sin6_addr.s6_addr16[4],
82 in6->sin6_addr.s6_addr16[5],
83 in6->sin6_addr.s6_addr16[6],
84 in6->sin6_addr.s6_addr16[7],
85 (unsigned int)ntohs(in6->sin6_port));
86 break;
87
88 default:
89 sprintf(s, "(unknown sockaddr family %d)", (int)ss->ss_family);
90 }
91
92 return s;
93}
94
63f2d211
SW
95static void encode_my_addr(struct ceph_messenger *msgr)
96{
97 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
98 ceph_encode_addr(&msgr->my_enc_addr);
99}
100
31b8006e
SW
101/*
102 * work queue for all reading and writing to/from the socket.
103 */
104struct workqueue_struct *ceph_msgr_wq;
105
106int __init ceph_msgr_init(void)
107{
108 ceph_msgr_wq = create_workqueue("ceph-msgr");
109 if (IS_ERR(ceph_msgr_wq)) {
110 int ret = PTR_ERR(ceph_msgr_wq);
111 pr_err("msgr_init failed to create workqueue: %d\n", ret);
112 ceph_msgr_wq = NULL;
113 return ret;
114 }
115 return 0;
116}
117
118void ceph_msgr_exit(void)
119{
120 destroy_workqueue(ceph_msgr_wq);
121}
122
a922d38f
SW
123void ceph_msgr_flush()
124{
125 flush_workqueue(ceph_msgr_wq);
126}
127
128
31b8006e
SW
129/*
130 * socket callback functions
131 */
132
133/* data available on socket, or listen socket received a connect */
134static void ceph_data_ready(struct sock *sk, int count_unused)
135{
136 struct ceph_connection *con =
137 (struct ceph_connection *)sk->sk_user_data;
138 if (sk->sk_state != TCP_CLOSE_WAIT) {
139 dout("ceph_data_ready on %p state = %lu, queueing work\n",
140 con, con->state);
141 queue_con(con);
142 }
143}
144
145/* socket has buffer space for writing */
146static void ceph_write_space(struct sock *sk)
147{
148 struct ceph_connection *con =
149 (struct ceph_connection *)sk->sk_user_data;
150
151 /* only queue to workqueue if there is data we want to write. */
152 if (test_bit(WRITE_PENDING, &con->state)) {
153 dout("ceph_write_space %p queueing write work\n", con);
154 queue_con(con);
155 } else {
156 dout("ceph_write_space %p nothing to write\n", con);
157 }
158
159 /* since we have our own write_space, clear the SOCK_NOSPACE flag */
160 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
161}
162
163/* socket's state has changed */
164static void ceph_state_change(struct sock *sk)
165{
166 struct ceph_connection *con =
167 (struct ceph_connection *)sk->sk_user_data;
168
169 dout("ceph_state_change %p state = %lu sk_state = %u\n",
170 con, con->state, sk->sk_state);
171
172 if (test_bit(CLOSED, &con->state))
173 return;
174
175 switch (sk->sk_state) {
176 case TCP_CLOSE:
177 dout("ceph_state_change TCP_CLOSE\n");
178 case TCP_CLOSE_WAIT:
179 dout("ceph_state_change TCP_CLOSE_WAIT\n");
180 if (test_and_set_bit(SOCK_CLOSED, &con->state) == 0) {
181 if (test_bit(CONNECTING, &con->state))
182 con->error_msg = "connection failed";
183 else
184 con->error_msg = "socket closed";
185 queue_con(con);
186 }
187 break;
188 case TCP_ESTABLISHED:
189 dout("ceph_state_change TCP_ESTABLISHED\n");
190 queue_con(con);
191 break;
192 }
193}
194
195/*
196 * set up socket callbacks
197 */
198static void set_sock_callbacks(struct socket *sock,
199 struct ceph_connection *con)
200{
201 struct sock *sk = sock->sk;
202 sk->sk_user_data = (void *)con;
203 sk->sk_data_ready = ceph_data_ready;
204 sk->sk_write_space = ceph_write_space;
205 sk->sk_state_change = ceph_state_change;
206}
207
208
209/*
210 * socket helpers
211 */
212
213/*
214 * initiate connection to a remote socket.
215 */
216static struct socket *ceph_tcp_connect(struct ceph_connection *con)
217{
218 struct sockaddr *paddr = (struct sockaddr *)&con->peer_addr.in_addr;
219 struct socket *sock;
220 int ret;
221
222 BUG_ON(con->sock);
223 ret = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
224 if (ret)
225 return ERR_PTR(ret);
226 con->sock = sock;
227 sock->sk->sk_allocation = GFP_NOFS;
228
a6a5349d
SW
229#ifdef CONFIG_LOCKDEP
230 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
231#endif
232
31b8006e
SW
233 set_sock_callbacks(sock, con);
234
235 dout("connect %s\n", pr_addr(&con->peer_addr.in_addr));
236
237 ret = sock->ops->connect(sock, paddr, sizeof(*paddr), O_NONBLOCK);
238 if (ret == -EINPROGRESS) {
239 dout("connect %s EINPROGRESS sk_state = %u\n",
240 pr_addr(&con->peer_addr.in_addr),
241 sock->sk->sk_state);
242 ret = 0;
243 }
244 if (ret < 0) {
245 pr_err("connect %s error %d\n",
246 pr_addr(&con->peer_addr.in_addr), ret);
247 sock_release(sock);
248 con->sock = NULL;
249 con->error_msg = "connect error";
250 }
251
252 if (ret < 0)
253 return ERR_PTR(ret);
254 return sock;
255}
256
257static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
258{
259 struct kvec iov = {buf, len};
260 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
261
262 return kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
263}
264
265/*
266 * write something. @more is true if caller will be sending more data
267 * shortly.
268 */
269static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
270 size_t kvlen, size_t len, int more)
271{
272 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
273
274 if (more)
275 msg.msg_flags |= MSG_MORE;
276 else
277 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
278
279 return kernel_sendmsg(sock, &msg, iov, kvlen, len);
280}
281
282
283/*
284 * Shutdown/close the socket for the given connection.
285 */
286static int con_close_socket(struct ceph_connection *con)
287{
288 int rc;
289
290 dout("con_close_socket on %p sock %p\n", con, con->sock);
291 if (!con->sock)
292 return 0;
293 set_bit(SOCK_CLOSED, &con->state);
294 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
295 sock_release(con->sock);
296 con->sock = NULL;
297 clear_bit(SOCK_CLOSED, &con->state);
298 return rc;
299}
300
301/*
302 * Reset a connection. Discard all incoming and outgoing messages
303 * and clear *_seq state.
304 */
305static void ceph_msg_remove(struct ceph_msg *msg)
306{
307 list_del_init(&msg->list_head);
308 ceph_msg_put(msg);
309}
310static void ceph_msg_remove_list(struct list_head *head)
311{
312 while (!list_empty(head)) {
313 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
314 list_head);
315 ceph_msg_remove(msg);
316 }
317}
318
319static void reset_connection(struct ceph_connection *con)
320{
321 /* reset connection, out_queue, msg_ and connect_seq */
322 /* discard existing out_queue and msg_seq */
31b8006e
SW
323 ceph_msg_remove_list(&con->out_queue);
324 ceph_msg_remove_list(&con->out_sent);
325
cf3e5c40
SW
326 if (con->in_msg) {
327 ceph_msg_put(con->in_msg);
328 con->in_msg = NULL;
329 }
330
31b8006e
SW
331 con->connect_seq = 0;
332 con->out_seq = 0;
c86a2930
SW
333 if (con->out_msg) {
334 ceph_msg_put(con->out_msg);
335 con->out_msg = NULL;
336 }
6f2bc3ff 337 con->out_keepalive_pending = false;
31b8006e 338 con->in_seq = 0;
0e0d5e0c 339 con->in_seq_acked = 0;
31b8006e
SW
340}
341
342/*
343 * mark a peer down. drop any open connections.
344 */
345void ceph_con_close(struct ceph_connection *con)
346{
347 dout("con_close %p peer %s\n", con, pr_addr(&con->peer_addr.in_addr));
348 set_bit(CLOSED, &con->state); /* in case there's queued work */
349 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
1679f876
SW
350 clear_bit(LOSSYTX, &con->state); /* so we retry next connect */
351 clear_bit(KEEPALIVE_PENDING, &con->state);
352 clear_bit(WRITE_PENDING, &con->state);
ec302645 353 mutex_lock(&con->mutex);
31b8006e 354 reset_connection(con);
6f2bc3ff 355 con->peer_global_seq = 0;
91e45ce3 356 cancel_delayed_work(&con->work);
ec302645 357 mutex_unlock(&con->mutex);
31b8006e
SW
358 queue_con(con);
359}
360
31b8006e
SW
361/*
362 * Reopen a closed connection, with a new peer address.
363 */
364void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
365{
366 dout("con_open %p %s\n", con, pr_addr(&addr->in_addr));
367 set_bit(OPENING, &con->state);
368 clear_bit(CLOSED, &con->state);
369 memcpy(&con->peer_addr, addr, sizeof(*addr));
03c677e1 370 con->delay = 0; /* reset backoff memory */
31b8006e
SW
371 queue_con(con);
372}
373
87b315a5
SW
374/*
375 * return true if this connection ever successfully opened
376 */
377bool ceph_con_opened(struct ceph_connection *con)
378{
379 return con->connect_seq > 0;
380}
381
31b8006e
SW
382/*
383 * generic get/put
384 */
385struct ceph_connection *ceph_con_get(struct ceph_connection *con)
386{
387 dout("con_get %p nref = %d -> %d\n", con,
388 atomic_read(&con->nref), atomic_read(&con->nref) + 1);
389 if (atomic_inc_not_zero(&con->nref))
390 return con;
391 return NULL;
392}
393
394void ceph_con_put(struct ceph_connection *con)
395{
396 dout("con_put %p nref = %d -> %d\n", con,
397 atomic_read(&con->nref), atomic_read(&con->nref) - 1);
398 BUG_ON(atomic_read(&con->nref) == 0);
399 if (atomic_dec_and_test(&con->nref)) {
71ececda 400 BUG_ON(con->sock);
31b8006e
SW
401 kfree(con);
402 }
403}
404
405/*
406 * initialize a new connection.
407 */
408void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con)
409{
410 dout("con_init %p\n", con);
411 memset(con, 0, sizeof(*con));
412 atomic_set(&con->nref, 1);
413 con->msgr = msgr;
ec302645 414 mutex_init(&con->mutex);
31b8006e
SW
415 INIT_LIST_HEAD(&con->out_queue);
416 INIT_LIST_HEAD(&con->out_sent);
417 INIT_DELAYED_WORK(&con->work, con_work);
418}
419
420
421/*
422 * We maintain a global counter to order connection attempts. Get
423 * a unique seq greater than @gt.
424 */
425static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
426{
427 u32 ret;
428
429 spin_lock(&msgr->global_seq_lock);
430 if (msgr->global_seq < gt)
431 msgr->global_seq = gt;
432 ret = ++msgr->global_seq;
433 spin_unlock(&msgr->global_seq_lock);
434 return ret;
435}
436
437
438/*
439 * Prepare footer for currently outgoing message, and finish things
440 * off. Assumes out_kvec* are already valid.. we just add on to the end.
441 */
442static void prepare_write_message_footer(struct ceph_connection *con, int v)
443{
444 struct ceph_msg *m = con->out_msg;
445
446 dout("prepare_write_message_footer %p\n", con);
447 con->out_kvec_is_msg = true;
448 con->out_kvec[v].iov_base = &m->footer;
449 con->out_kvec[v].iov_len = sizeof(m->footer);
450 con->out_kvec_bytes += sizeof(m->footer);
451 con->out_kvec_left++;
452 con->out_more = m->more_to_follow;
c86a2930 453 con->out_msg_done = true;
31b8006e
SW
454}
455
456/*
457 * Prepare headers for the next outgoing message.
458 */
459static void prepare_write_message(struct ceph_connection *con)
460{
461 struct ceph_msg *m;
462 int v = 0;
463
464 con->out_kvec_bytes = 0;
465 con->out_kvec_is_msg = true;
c86a2930 466 con->out_msg_done = false;
31b8006e
SW
467
468 /* Sneak an ack in there first? If we can get it into the same
469 * TCP packet that's a good thing. */
470 if (con->in_seq > con->in_seq_acked) {
471 con->in_seq_acked = con->in_seq;
472 con->out_kvec[v].iov_base = &tag_ack;
473 con->out_kvec[v++].iov_len = 1;
474 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
475 con->out_kvec[v].iov_base = &con->out_temp_ack;
476 con->out_kvec[v++].iov_len = sizeof(con->out_temp_ack);
477 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
478 }
479
31b8006e
SW
480 m = list_first_entry(&con->out_queue,
481 struct ceph_msg, list_head);
c86a2930 482 con->out_msg = m;
b3d1dbbd 483 if (test_bit(LOSSYTX, &con->state)) {
6c5d1a49
SW
484 list_del_init(&m->list_head);
485 } else {
b3d1dbbd
SW
486 /* put message on sent list */
487 ceph_msg_get(m);
488 list_move_tail(&m->list_head, &con->out_sent);
b3d1dbbd 489 }
31b8006e 490
e84346b7
SW
491 /*
492 * only assign outgoing seq # if we haven't sent this message
493 * yet. if it is requeued, resend with it's original seq.
494 */
495 if (m->needs_out_seq) {
496 m->hdr.seq = cpu_to_le64(++con->out_seq);
497 m->needs_out_seq = false;
498 }
31b8006e
SW
499
500 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
501 m, con->out_seq, le16_to_cpu(m->hdr.type),
502 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
503 le32_to_cpu(m->hdr.data_len),
504 m->nr_pages);
505 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
506
507 /* tag + hdr + front + middle */
508 con->out_kvec[v].iov_base = &tag_msg;
509 con->out_kvec[v++].iov_len = 1;
510 con->out_kvec[v].iov_base = &m->hdr;
511 con->out_kvec[v++].iov_len = sizeof(m->hdr);
512 con->out_kvec[v++] = m->front;
513 if (m->middle)
514 con->out_kvec[v++] = m->middle->vec;
515 con->out_kvec_left = v;
516 con->out_kvec_bytes += 1 + sizeof(m->hdr) + m->front.iov_len +
517 (m->middle ? m->middle->vec.iov_len : 0);
518 con->out_kvec_cur = con->out_kvec;
519
520 /* fill in crc (except data pages), footer */
521 con->out_msg->hdr.crc =
522 cpu_to_le32(crc32c(0, (void *)&m->hdr,
523 sizeof(m->hdr) - sizeof(m->hdr.crc)));
524 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
525 con->out_msg->footer.front_crc =
526 cpu_to_le32(crc32c(0, m->front.iov_base, m->front.iov_len));
527 if (m->middle)
528 con->out_msg->footer.middle_crc =
529 cpu_to_le32(crc32c(0, m->middle->vec.iov_base,
530 m->middle->vec.iov_len));
531 else
532 con->out_msg->footer.middle_crc = 0;
533 con->out_msg->footer.data_crc = 0;
534 dout("prepare_write_message front_crc %u data_crc %u\n",
535 le32_to_cpu(con->out_msg->footer.front_crc),
536 le32_to_cpu(con->out_msg->footer.middle_crc));
537
538 /* is there a data payload? */
539 if (le32_to_cpu(m->hdr.data_len) > 0) {
540 /* initialize page iterator */
541 con->out_msg_pos.page = 0;
542 con->out_msg_pos.page_pos =
543 le16_to_cpu(m->hdr.data_off) & ~PAGE_MASK;
544 con->out_msg_pos.data_pos = 0;
545 con->out_msg_pos.did_page_crc = 0;
546 con->out_more = 1; /* data + footer will follow */
547 } else {
548 /* no, queue up footer too and be done */
549 prepare_write_message_footer(con, v);
550 }
551
552 set_bit(WRITE_PENDING, &con->state);
553}
554
555/*
556 * Prepare an ack.
557 */
558static void prepare_write_ack(struct ceph_connection *con)
559{
560 dout("prepare_write_ack %p %llu -> %llu\n", con,
561 con->in_seq_acked, con->in_seq);
562 con->in_seq_acked = con->in_seq;
563
564 con->out_kvec[0].iov_base = &tag_ack;
565 con->out_kvec[0].iov_len = 1;
566 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
567 con->out_kvec[1].iov_base = &con->out_temp_ack;
568 con->out_kvec[1].iov_len = sizeof(con->out_temp_ack);
569 con->out_kvec_left = 2;
570 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
571 con->out_kvec_cur = con->out_kvec;
572 con->out_more = 1; /* more will follow.. eventually.. */
573 set_bit(WRITE_PENDING, &con->state);
574}
575
576/*
577 * Prepare to write keepalive byte.
578 */
579static void prepare_write_keepalive(struct ceph_connection *con)
580{
581 dout("prepare_write_keepalive %p\n", con);
582 con->out_kvec[0].iov_base = &tag_keepalive;
583 con->out_kvec[0].iov_len = 1;
584 con->out_kvec_left = 1;
585 con->out_kvec_bytes = 1;
586 con->out_kvec_cur = con->out_kvec;
587 set_bit(WRITE_PENDING, &con->state);
588}
589
590/*
591 * Connection negotiation.
592 */
593
4e7a5dcd
SW
594static void prepare_connect_authorizer(struct ceph_connection *con)
595{
596 void *auth_buf;
597 int auth_len = 0;
598 int auth_protocol = 0;
599
ec302645 600 mutex_unlock(&con->mutex);
4e7a5dcd
SW
601 if (con->ops->get_authorizer)
602 con->ops->get_authorizer(con, &auth_buf, &auth_len,
603 &auth_protocol, &con->auth_reply_buf,
604 &con->auth_reply_buf_len,
605 con->auth_retry);
ec302645 606 mutex_lock(&con->mutex);
4e7a5dcd
SW
607
608 con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol);
609 con->out_connect.authorizer_len = cpu_to_le32(auth_len);
610
611 con->out_kvec[con->out_kvec_left].iov_base = auth_buf;
612 con->out_kvec[con->out_kvec_left].iov_len = auth_len;
613 con->out_kvec_left++;
614 con->out_kvec_bytes += auth_len;
615}
616
31b8006e
SW
617/*
618 * We connected to a peer and are saying hello.
619 */
eed0ef2c
SW
620static void prepare_write_banner(struct ceph_messenger *msgr,
621 struct ceph_connection *con)
31b8006e
SW
622{
623 int len = strlen(CEPH_BANNER);
eed0ef2c
SW
624
625 con->out_kvec[0].iov_base = CEPH_BANNER;
626 con->out_kvec[0].iov_len = len;
627 con->out_kvec[1].iov_base = &msgr->my_enc_addr;
628 con->out_kvec[1].iov_len = sizeof(msgr->my_enc_addr);
629 con->out_kvec_left = 2;
630 con->out_kvec_bytes = len + sizeof(msgr->my_enc_addr);
631 con->out_kvec_cur = con->out_kvec;
632 con->out_more = 0;
633 set_bit(WRITE_PENDING, &con->state);
634}
635
636static void prepare_write_connect(struct ceph_messenger *msgr,
637 struct ceph_connection *con,
638 int after_banner)
639{
31b8006e
SW
640 unsigned global_seq = get_global_seq(con->msgr, 0);
641 int proto;
642
643 switch (con->peer_name.type) {
644 case CEPH_ENTITY_TYPE_MON:
645 proto = CEPH_MONC_PROTOCOL;
646 break;
647 case CEPH_ENTITY_TYPE_OSD:
648 proto = CEPH_OSDC_PROTOCOL;
649 break;
650 case CEPH_ENTITY_TYPE_MDS:
651 proto = CEPH_MDSC_PROTOCOL;
652 break;
653 default:
654 BUG();
655 }
656
657 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
658 con->connect_seq, global_seq, proto);
4e7a5dcd 659
0cf5537b 660 con->out_connect.features = cpu_to_le64(CEPH_FEATURE_SUPPORTED_CLIENT);
31b8006e
SW
661 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
662 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
663 con->out_connect.global_seq = cpu_to_le32(global_seq);
664 con->out_connect.protocol_version = cpu_to_le32(proto);
665 con->out_connect.flags = 0;
31b8006e 666
eed0ef2c
SW
667 if (!after_banner) {
668 con->out_kvec_left = 0;
669 con->out_kvec_bytes = 0;
670 }
671 con->out_kvec[con->out_kvec_left].iov_base = &con->out_connect;
672 con->out_kvec[con->out_kvec_left].iov_len = sizeof(con->out_connect);
673 con->out_kvec_left++;
674 con->out_kvec_bytes += sizeof(con->out_connect);
31b8006e
SW
675 con->out_kvec_cur = con->out_kvec;
676 con->out_more = 0;
677 set_bit(WRITE_PENDING, &con->state);
4e7a5dcd
SW
678
679 prepare_connect_authorizer(con);
31b8006e
SW
680}
681
682
683/*
684 * write as much of pending kvecs to the socket as we can.
685 * 1 -> done
686 * 0 -> socket full, but more to do
687 * <0 -> error
688 */
689static int write_partial_kvec(struct ceph_connection *con)
690{
691 int ret;
692
693 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
694 while (con->out_kvec_bytes > 0) {
695 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
696 con->out_kvec_left, con->out_kvec_bytes,
697 con->out_more);
698 if (ret <= 0)
699 goto out;
700 con->out_kvec_bytes -= ret;
701 if (con->out_kvec_bytes == 0)
702 break; /* done */
703 while (ret > 0) {
704 if (ret >= con->out_kvec_cur->iov_len) {
705 ret -= con->out_kvec_cur->iov_len;
706 con->out_kvec_cur++;
707 con->out_kvec_left--;
708 } else {
709 con->out_kvec_cur->iov_len -= ret;
710 con->out_kvec_cur->iov_base += ret;
711 ret = 0;
712 break;
713 }
714 }
715 }
716 con->out_kvec_left = 0;
717 con->out_kvec_is_msg = false;
718 ret = 1;
719out:
720 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
721 con->out_kvec_bytes, con->out_kvec_left, ret);
722 return ret; /* done! */
723}
724
725/*
726 * Write as much message data payload as we can. If we finish, queue
727 * up the footer.
728 * 1 -> done, footer is now queued in out_kvec[].
729 * 0 -> socket full, but more to do
730 * <0 -> error
731 */
732static int write_partial_msg_pages(struct ceph_connection *con)
733{
734 struct ceph_msg *msg = con->out_msg;
735 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
736 size_t len;
737 int crc = con->msgr->nocrc;
738 int ret;
739
740 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
741 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
742 con->out_msg_pos.page_pos);
743
744 while (con->out_msg_pos.page < con->out_msg->nr_pages) {
745 struct page *page = NULL;
746 void *kaddr = NULL;
747
748 /*
749 * if we are calculating the data crc (the default), we need
750 * to map the page. if our pages[] has been revoked, use the
751 * zero page.
752 */
753 if (msg->pages) {
754 page = msg->pages[con->out_msg_pos.page];
755 if (crc)
756 kaddr = kmap(page);
58bb3b37
SW
757 } else if (msg->pagelist) {
758 page = list_first_entry(&msg->pagelist->head,
759 struct page, lru);
760 if (crc)
761 kaddr = kmap(page);
31b8006e
SW
762 } else {
763 page = con->msgr->zero_page;
764 if (crc)
765 kaddr = page_address(con->msgr->zero_page);
766 }
767 len = min((int)(PAGE_SIZE - con->out_msg_pos.page_pos),
768 (int)(data_len - con->out_msg_pos.data_pos));
769 if (crc && !con->out_msg_pos.did_page_crc) {
770 void *base = kaddr + con->out_msg_pos.page_pos;
771 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
772
773 BUG_ON(kaddr == NULL);
774 con->out_msg->footer.data_crc =
775 cpu_to_le32(crc32c(tmpcrc, base, len));
776 con->out_msg_pos.did_page_crc = 1;
777 }
778
779 ret = kernel_sendpage(con->sock, page,
780 con->out_msg_pos.page_pos, len,
781 MSG_DONTWAIT | MSG_NOSIGNAL |
782 MSG_MORE);
783
58bb3b37 784 if (crc && (msg->pages || msg->pagelist))
31b8006e
SW
785 kunmap(page);
786
787 if (ret <= 0)
788 goto out;
789
790 con->out_msg_pos.data_pos += ret;
791 con->out_msg_pos.page_pos += ret;
792 if (ret == len) {
793 con->out_msg_pos.page_pos = 0;
794 con->out_msg_pos.page++;
795 con->out_msg_pos.did_page_crc = 0;
58bb3b37
SW
796 if (msg->pagelist)
797 list_move_tail(&page->lru,
798 &msg->pagelist->head);
31b8006e
SW
799 }
800 }
801
802 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
803
804 /* prepare and queue up footer, too */
805 if (!crc)
806 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
807 con->out_kvec_bytes = 0;
808 con->out_kvec_left = 0;
809 con->out_kvec_cur = con->out_kvec;
810 prepare_write_message_footer(con, 0);
811 ret = 1;
812out:
813 return ret;
814}
815
816/*
817 * write some zeros
818 */
819static int write_partial_skip(struct ceph_connection *con)
820{
821 int ret;
822
823 while (con->out_skip > 0) {
824 struct kvec iov = {
825 .iov_base = page_address(con->msgr->zero_page),
826 .iov_len = min(con->out_skip, (int)PAGE_CACHE_SIZE)
827 };
828
829 ret = ceph_tcp_sendmsg(con->sock, &iov, 1, iov.iov_len, 1);
830 if (ret <= 0)
831 goto out;
832 con->out_skip -= ret;
833 }
834 ret = 1;
835out:
836 return ret;
837}
838
839/*
840 * Prepare to read connection handshake, or an ack.
841 */
eed0ef2c
SW
842static void prepare_read_banner(struct ceph_connection *con)
843{
844 dout("prepare_read_banner %p\n", con);
845 con->in_base_pos = 0;
846}
847
31b8006e
SW
848static void prepare_read_connect(struct ceph_connection *con)
849{
850 dout("prepare_read_connect %p\n", con);
851 con->in_base_pos = 0;
852}
853
854static void prepare_read_ack(struct ceph_connection *con)
855{
856 dout("prepare_read_ack %p\n", con);
857 con->in_base_pos = 0;
858}
859
860static void prepare_read_tag(struct ceph_connection *con)
861{
862 dout("prepare_read_tag %p\n", con);
863 con->in_base_pos = 0;
864 con->in_tag = CEPH_MSGR_TAG_READY;
865}
866
867/*
868 * Prepare to read a message.
869 */
870static int prepare_read_message(struct ceph_connection *con)
871{
872 dout("prepare_read_message %p\n", con);
873 BUG_ON(con->in_msg != NULL);
874 con->in_base_pos = 0;
875 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
876 return 0;
877}
878
879
880static int read_partial(struct ceph_connection *con,
881 int *to, int size, void *object)
882{
883 *to += size;
884 while (con->in_base_pos < *to) {
885 int left = *to - con->in_base_pos;
886 int have = size - left;
887 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
888 if (ret <= 0)
889 return ret;
890 con->in_base_pos += ret;
891 }
892 return 1;
893}
894
895
896/*
897 * Read all or part of the connect-side handshake on a new connection
898 */
eed0ef2c 899static int read_partial_banner(struct ceph_connection *con)
31b8006e
SW
900{
901 int ret, to = 0;
902
eed0ef2c 903 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
31b8006e
SW
904
905 /* peer's banner */
906 ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner);
907 if (ret <= 0)
908 goto out;
909 ret = read_partial(con, &to, sizeof(con->actual_peer_addr),
910 &con->actual_peer_addr);
911 if (ret <= 0)
912 goto out;
913 ret = read_partial(con, &to, sizeof(con->peer_addr_for_me),
914 &con->peer_addr_for_me);
915 if (ret <= 0)
916 goto out;
eed0ef2c
SW
917out:
918 return ret;
919}
920
921static int read_partial_connect(struct ceph_connection *con)
922{
923 int ret, to = 0;
924
925 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
926
31b8006e
SW
927 ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply);
928 if (ret <= 0)
929 goto out;
4e7a5dcd
SW
930 ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len),
931 con->auth_reply_buf);
932 if (ret <= 0)
933 goto out;
31b8006e 934
4e7a5dcd
SW
935 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
936 con, (int)con->in_reply.tag,
937 le32_to_cpu(con->in_reply.connect_seq),
31b8006e
SW
938 le32_to_cpu(con->in_reply.global_seq));
939out:
940 return ret;
eed0ef2c 941
31b8006e
SW
942}
943
944/*
945 * Verify the hello banner looks okay.
946 */
947static int verify_hello(struct ceph_connection *con)
948{
949 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
13e38c8a 950 pr_err("connect to %s got bad banner\n",
31b8006e
SW
951 pr_addr(&con->peer_addr.in_addr));
952 con->error_msg = "protocol error, bad banner";
953 return -1;
954 }
955 return 0;
956}
957
958static bool addr_is_blank(struct sockaddr_storage *ss)
959{
960 switch (ss->ss_family) {
961 case AF_INET:
962 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
963 case AF_INET6:
964 return
965 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
966 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
967 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
968 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
969 }
970 return false;
971}
972
973static int addr_port(struct sockaddr_storage *ss)
974{
975 switch (ss->ss_family) {
976 case AF_INET:
f28bcfbe 977 return ntohs(((struct sockaddr_in *)ss)->sin_port);
31b8006e 978 case AF_INET6:
f28bcfbe 979 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
31b8006e
SW
980 }
981 return 0;
982}
983
984static void addr_set_port(struct sockaddr_storage *ss, int p)
985{
986 switch (ss->ss_family) {
987 case AF_INET:
988 ((struct sockaddr_in *)ss)->sin_port = htons(p);
989 case AF_INET6:
990 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
991 }
992}
993
994/*
995 * Parse an ip[:port] list into an addr array. Use the default
996 * monitor port if a port isn't specified.
997 */
998int ceph_parse_ips(const char *c, const char *end,
999 struct ceph_entity_addr *addr,
1000 int max_count, int *count)
1001{
1002 int i;
1003 const char *p = c;
1004
1005 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1006 for (i = 0; i < max_count; i++) {
1007 const char *ipend;
1008 struct sockaddr_storage *ss = &addr[i].in_addr;
1009 struct sockaddr_in *in4 = (void *)ss;
1010 struct sockaddr_in6 *in6 = (void *)ss;
1011 int port;
1012
1013 memset(ss, 0, sizeof(*ss));
1014 if (in4_pton(p, end - p, (u8 *)&in4->sin_addr.s_addr,
1015 ',', &ipend)) {
1016 ss->ss_family = AF_INET;
1017 } else if (in6_pton(p, end - p, (u8 *)&in6->sin6_addr.s6_addr,
1018 ',', &ipend)) {
1019 ss->ss_family = AF_INET6;
1020 } else {
1021 goto bad;
1022 }
1023 p = ipend;
1024
1025 /* port? */
1026 if (p < end && *p == ':') {
1027 port = 0;
1028 p++;
1029 while (p < end && *p >= '0' && *p <= '9') {
1030 port = (port * 10) + (*p - '0');
1031 p++;
1032 }
1033 if (port > 65535 || port == 0)
1034 goto bad;
1035 } else {
1036 port = CEPH_MON_PORT;
1037 }
1038
1039 addr_set_port(ss, port);
1040
1041 dout("parse_ips got %s\n", pr_addr(ss));
1042
1043 if (p == end)
1044 break;
1045 if (*p != ',')
1046 goto bad;
1047 p++;
1048 }
1049
1050 if (p != end)
1051 goto bad;
1052
1053 if (count)
1054 *count = i + 1;
1055 return 0;
1056
1057bad:
1058 pr_err("parse_ips bad ip '%s'\n", c);
1059 return -EINVAL;
1060}
1061
eed0ef2c 1062static int process_banner(struct ceph_connection *con)
31b8006e 1063{
eed0ef2c 1064 dout("process_banner on %p\n", con);
31b8006e
SW
1065
1066 if (verify_hello(con) < 0)
1067 return -1;
1068
63f2d211
SW
1069 ceph_decode_addr(&con->actual_peer_addr);
1070 ceph_decode_addr(&con->peer_addr_for_me);
1071
31b8006e
SW
1072 /*
1073 * Make sure the other end is who we wanted. note that the other
1074 * end may not yet know their ip address, so if it's 0.0.0.0, give
1075 * them the benefit of the doubt.
1076 */
103e2d3a
SW
1077 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1078 sizeof(con->peer_addr)) != 0 &&
31b8006e
SW
1079 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1080 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
103e2d3a
SW
1081 pr_warning("wrong peer, want %s/%lld, got %s/%lld\n",
1082 pr_addr(&con->peer_addr.in_addr),
1083 le64_to_cpu(con->peer_addr.nonce),
1084 pr_addr(&con->actual_peer_addr.in_addr),
1085 le64_to_cpu(con->actual_peer_addr.nonce));
58bb3b37 1086 con->error_msg = "wrong peer at address";
31b8006e
SW
1087 return -1;
1088 }
1089
1090 /*
1091 * did we learn our address?
1092 */
1093 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1094 int port = addr_port(&con->msgr->inst.addr.in_addr);
1095
1096 memcpy(&con->msgr->inst.addr.in_addr,
1097 &con->peer_addr_for_me.in_addr,
1098 sizeof(con->peer_addr_for_me.in_addr));
1099 addr_set_port(&con->msgr->inst.addr.in_addr, port);
63f2d211 1100 encode_my_addr(con->msgr);
eed0ef2c 1101 dout("process_banner learned my addr is %s\n",
31b8006e
SW
1102 pr_addr(&con->msgr->inst.addr.in_addr));
1103 }
1104
eed0ef2c
SW
1105 set_bit(NEGOTIATING, &con->state);
1106 prepare_read_connect(con);
1107 return 0;
1108}
1109
04a419f9
SW
1110static void fail_protocol(struct ceph_connection *con)
1111{
1112 reset_connection(con);
1113 set_bit(CLOSED, &con->state); /* in case there's queued work */
1114
1115 mutex_unlock(&con->mutex);
1116 if (con->ops->bad_proto)
1117 con->ops->bad_proto(con);
1118 mutex_lock(&con->mutex);
1119}
1120
eed0ef2c
SW
1121static int process_connect(struct ceph_connection *con)
1122{
dbad185d
SW
1123 u64 sup_feat = CEPH_FEATURE_SUPPORTED_CLIENT;
1124 u64 req_feat = CEPH_FEATURE_REQUIRED_CLIENT;
04a419f9
SW
1125 u64 server_feat = le64_to_cpu(con->in_reply.features);
1126
eed0ef2c
SW
1127 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1128
31b8006e 1129 switch (con->in_reply.tag) {
04a419f9
SW
1130 case CEPH_MSGR_TAG_FEATURES:
1131 pr_err("%s%lld %s feature set mismatch,"
1132 " my %llx < server's %llx, missing %llx\n",
1133 ENTITY_NAME(con->peer_name),
1134 pr_addr(&con->peer_addr.in_addr),
1135 sup_feat, server_feat, server_feat & ~sup_feat);
1136 con->error_msg = "missing required protocol features";
1137 fail_protocol(con);
1138 return -1;
1139
31b8006e 1140 case CEPH_MSGR_TAG_BADPROTOVER:
31b8006e
SW
1141 pr_err("%s%lld %s protocol version mismatch,"
1142 " my %d != server's %d\n",
1143 ENTITY_NAME(con->peer_name),
1144 pr_addr(&con->peer_addr.in_addr),
1145 le32_to_cpu(con->out_connect.protocol_version),
1146 le32_to_cpu(con->in_reply.protocol_version));
1147 con->error_msg = "protocol version mismatch";
04a419f9 1148 fail_protocol(con);
31b8006e
SW
1149 return -1;
1150
4e7a5dcd
SW
1151 case CEPH_MSGR_TAG_BADAUTHORIZER:
1152 con->auth_retry++;
1153 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1154 con->auth_retry);
1155 if (con->auth_retry == 2) {
1156 con->error_msg = "connect authorization failure";
1157 reset_connection(con);
1158 set_bit(CLOSED, &con->state);
1159 return -1;
1160 }
1161 con->auth_retry = 1;
1162 prepare_write_connect(con->msgr, con, 0);
63733a0f 1163 prepare_read_connect(con);
4e7a5dcd 1164 break;
31b8006e
SW
1165
1166 case CEPH_MSGR_TAG_RESETSESSION:
1167 /*
1168 * If we connected with a large connect_seq but the peer
1169 * has no record of a session with us (no connection, or
1170 * connect_seq == 0), they will send RESETSESION to indicate
1171 * that they must have reset their session, and may have
1172 * dropped messages.
1173 */
1174 dout("process_connect got RESET peer seq %u\n",
1175 le32_to_cpu(con->in_connect.connect_seq));
1176 pr_err("%s%lld %s connection reset\n",
1177 ENTITY_NAME(con->peer_name),
1178 pr_addr(&con->peer_addr.in_addr));
1179 reset_connection(con);
eed0ef2c 1180 prepare_write_connect(con->msgr, con, 0);
31b8006e
SW
1181 prepare_read_connect(con);
1182
1183 /* Tell ceph about it. */
ec302645 1184 mutex_unlock(&con->mutex);
31b8006e
SW
1185 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1186 if (con->ops->peer_reset)
1187 con->ops->peer_reset(con);
ec302645 1188 mutex_lock(&con->mutex);
31b8006e
SW
1189 break;
1190
1191 case CEPH_MSGR_TAG_RETRY_SESSION:
1192 /*
1193 * If we sent a smaller connect_seq than the peer has, try
1194 * again with a larger value.
1195 */
1196 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1197 le32_to_cpu(con->out_connect.connect_seq),
1198 le32_to_cpu(con->in_connect.connect_seq));
1199 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
eed0ef2c 1200 prepare_write_connect(con->msgr, con, 0);
31b8006e
SW
1201 prepare_read_connect(con);
1202 break;
1203
1204 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1205 /*
1206 * If we sent a smaller global_seq than the peer has, try
1207 * again with a larger value.
1208 */
eed0ef2c 1209 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
31b8006e
SW
1210 con->peer_global_seq,
1211 le32_to_cpu(con->in_connect.global_seq));
1212 get_global_seq(con->msgr,
1213 le32_to_cpu(con->in_connect.global_seq));
eed0ef2c 1214 prepare_write_connect(con->msgr, con, 0);
31b8006e
SW
1215 prepare_read_connect(con);
1216 break;
1217
1218 case CEPH_MSGR_TAG_READY:
04a419f9
SW
1219 if (req_feat & ~server_feat) {
1220 pr_err("%s%lld %s protocol feature mismatch,"
1221 " my required %llx > server's %llx, need %llx\n",
1222 ENTITY_NAME(con->peer_name),
1223 pr_addr(&con->peer_addr.in_addr),
1224 req_feat, server_feat, req_feat & ~server_feat);
1225 con->error_msg = "missing required protocol features";
1226 fail_protocol(con);
1227 return -1;
1228 }
31b8006e 1229 clear_bit(CONNECTING, &con->state);
31b8006e
SW
1230 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1231 con->connect_seq++;
aba558e2 1232 con->peer_features = server_feat;
31b8006e
SW
1233 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1234 con->peer_global_seq,
1235 le32_to_cpu(con->in_reply.connect_seq),
1236 con->connect_seq);
1237 WARN_ON(con->connect_seq !=
1238 le32_to_cpu(con->in_reply.connect_seq));
92ac41d0
SW
1239
1240 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
1241 set_bit(LOSSYTX, &con->state);
1242
31b8006e
SW
1243 prepare_read_tag(con);
1244 break;
1245
1246 case CEPH_MSGR_TAG_WAIT:
1247 /*
1248 * If there is a connection race (we are opening
1249 * connections to each other), one of us may just have
1250 * to WAIT. This shouldn't happen if we are the
1251 * client.
1252 */
1253 pr_err("process_connect peer connecting WAIT\n");
1254
1255 default:
1256 pr_err("connect protocol error, will retry\n");
1257 con->error_msg = "protocol error, garbage tag during connect";
1258 return -1;
1259 }
1260 return 0;
1261}
1262
1263
1264/*
1265 * read (part of) an ack
1266 */
1267static int read_partial_ack(struct ceph_connection *con)
1268{
1269 int to = 0;
1270
1271 return read_partial(con, &to, sizeof(con->in_temp_ack),
1272 &con->in_temp_ack);
1273}
1274
1275
1276/*
1277 * We can finally discard anything that's been acked.
1278 */
1279static void process_ack(struct ceph_connection *con)
1280{
1281 struct ceph_msg *m;
1282 u64 ack = le64_to_cpu(con->in_temp_ack);
1283 u64 seq;
1284
31b8006e
SW
1285 while (!list_empty(&con->out_sent)) {
1286 m = list_first_entry(&con->out_sent, struct ceph_msg,
1287 list_head);
1288 seq = le64_to_cpu(m->hdr.seq);
1289 if (seq > ack)
1290 break;
1291 dout("got ack for seq %llu type %d at %p\n", seq,
1292 le16_to_cpu(m->hdr.type), m);
1293 ceph_msg_remove(m);
1294 }
31b8006e
SW
1295 prepare_read_tag(con);
1296}
1297
1298
1299
1300
2450418c
YS
1301static int read_partial_message_section(struct ceph_connection *con,
1302 struct kvec *section, unsigned int sec_len,
1303 u32 *crc)
1304{
1305 int left;
1306 int ret;
1307
1308 BUG_ON(!section);
1309
1310 while (section->iov_len < sec_len) {
1311 BUG_ON(section->iov_base == NULL);
1312 left = sec_len - section->iov_len;
1313 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1314 section->iov_len, left);
1315 if (ret <= 0)
1316 return ret;
1317 section->iov_len += ret;
1318 if (section->iov_len == sec_len)
1319 *crc = crc32c(0, section->iov_base,
1320 section->iov_len);
1321 }
31b8006e 1322
2450418c
YS
1323 return 1;
1324}
31b8006e 1325
2450418c
YS
1326static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
1327 struct ceph_msg_header *hdr,
1328 int *skip);
31b8006e
SW
1329/*
1330 * read (part of) a message.
1331 */
1332static int read_partial_message(struct ceph_connection *con)
1333{
1334 struct ceph_msg *m = con->in_msg;
1335 void *p;
1336 int ret;
9d7f0f13 1337 int to, left;
31b8006e
SW
1338 unsigned front_len, middle_len, data_len, data_off;
1339 int datacrc = con->msgr->nocrc;
2450418c 1340 int skip;
ae18756b 1341 u64 seq;
31b8006e
SW
1342
1343 dout("read_partial_message con %p msg %p\n", con, m);
1344
1345 /* header */
1346 while (con->in_base_pos < sizeof(con->in_hdr)) {
1347 left = sizeof(con->in_hdr) - con->in_base_pos;
1348 ret = ceph_tcp_recvmsg(con->sock,
1349 (char *)&con->in_hdr + con->in_base_pos,
1350 left);
1351 if (ret <= 0)
1352 return ret;
1353 con->in_base_pos += ret;
1354 if (con->in_base_pos == sizeof(con->in_hdr)) {
1355 u32 crc = crc32c(0, (void *)&con->in_hdr,
1356 sizeof(con->in_hdr) - sizeof(con->in_hdr.crc));
1357 if (crc != le32_to_cpu(con->in_hdr.crc)) {
1358 pr_err("read_partial_message bad hdr "
1359 " crc %u != expected %u\n",
1360 crc, con->in_hdr.crc);
1361 return -EBADMSG;
1362 }
1363 }
1364 }
31b8006e
SW
1365 front_len = le32_to_cpu(con->in_hdr.front_len);
1366 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1367 return -EIO;
1368 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1369 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1370 return -EIO;
1371 data_len = le32_to_cpu(con->in_hdr.data_len);
1372 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1373 return -EIO;
9d7f0f13 1374 data_off = le16_to_cpu(con->in_hdr.data_off);
31b8006e 1375
ae18756b
SW
1376 /* verify seq# */
1377 seq = le64_to_cpu(con->in_hdr.seq);
1378 if ((s64)seq - (s64)con->in_seq < 1) {
1379 pr_info("skipping %s%lld %s seq %lld, expected %lld\n",
1380 ENTITY_NAME(con->peer_name),
1381 pr_addr(&con->peer_addr.in_addr),
1382 seq, con->in_seq + 1);
1383 con->in_base_pos = -front_len - middle_len - data_len -
1384 sizeof(m->footer);
1385 con->in_tag = CEPH_MSGR_TAG_READY;
1386 con->in_seq++;
1387 return 0;
1388 } else if ((s64)seq - (s64)con->in_seq > 1) {
1389 pr_err("read_partial_message bad seq %lld expected %lld\n",
1390 seq, con->in_seq + 1);
1391 con->error_msg = "bad message sequence # for incoming message";
1392 return -EBADMSG;
1393 }
1394
31b8006e
SW
1395 /* allocate message? */
1396 if (!con->in_msg) {
1397 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1398 con->in_hdr.front_len, con->in_hdr.data_len);
ae32be31 1399 skip = 0;
2450418c
YS
1400 con->in_msg = ceph_alloc_msg(con, &con->in_hdr, &skip);
1401 if (skip) {
31b8006e 1402 /* skip this message */
a79832f2 1403 dout("alloc_msg said skip message\n");
ae32be31 1404 BUG_ON(con->in_msg);
31b8006e
SW
1405 con->in_base_pos = -front_len - middle_len - data_len -
1406 sizeof(m->footer);
1407 con->in_tag = CEPH_MSGR_TAG_READY;
684be25c 1408 con->in_seq++;
31b8006e
SW
1409 return 0;
1410 }
a79832f2 1411 if (!con->in_msg) {
5b3a4db3
SW
1412 con->error_msg =
1413 "error allocating memory for incoming message";
a79832f2 1414 return -ENOMEM;
31b8006e
SW
1415 }
1416 m = con->in_msg;
1417 m->front.iov_len = 0; /* haven't read it yet */
2450418c
YS
1418 if (m->middle)
1419 m->middle->vec.iov_len = 0;
9d7f0f13
YS
1420
1421 con->in_msg_pos.page = 0;
1422 con->in_msg_pos.page_pos = data_off & ~PAGE_MASK;
1423 con->in_msg_pos.data_pos = 0;
31b8006e
SW
1424 }
1425
1426 /* front */
2450418c
YS
1427 ret = read_partial_message_section(con, &m->front, front_len,
1428 &con->in_front_crc);
1429 if (ret <= 0)
1430 return ret;
31b8006e
SW
1431
1432 /* middle */
2450418c
YS
1433 if (m->middle) {
1434 ret = read_partial_message_section(con, &m->middle->vec, middle_len,
1435 &con->in_middle_crc);
31b8006e
SW
1436 if (ret <= 0)
1437 return ret;
31b8006e
SW
1438 }
1439
1440 /* (page) data */
31b8006e
SW
1441 while (con->in_msg_pos.data_pos < data_len) {
1442 left = min((int)(data_len - con->in_msg_pos.data_pos),
1443 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1444 BUG_ON(m->pages == NULL);
1445 p = kmap(m->pages[con->in_msg_pos.page]);
1446 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1447 left);
1448 if (ret > 0 && datacrc)
1449 con->in_data_crc =
1450 crc32c(con->in_data_crc,
1451 p + con->in_msg_pos.page_pos, ret);
1452 kunmap(m->pages[con->in_msg_pos.page]);
1453 if (ret <= 0)
1454 return ret;
1455 con->in_msg_pos.data_pos += ret;
1456 con->in_msg_pos.page_pos += ret;
1457 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1458 con->in_msg_pos.page_pos = 0;
1459 con->in_msg_pos.page++;
1460 }
1461 }
1462
31b8006e
SW
1463 /* footer */
1464 to = sizeof(m->hdr) + sizeof(m->footer);
1465 while (con->in_base_pos < to) {
1466 left = to - con->in_base_pos;
1467 ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer +
1468 (con->in_base_pos - sizeof(m->hdr)),
1469 left);
1470 if (ret <= 0)
1471 return ret;
1472 con->in_base_pos += ret;
1473 }
1474 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1475 m, front_len, m->footer.front_crc, middle_len,
1476 m->footer.middle_crc, data_len, m->footer.data_crc);
1477
1478 /* crc ok? */
1479 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1480 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1481 m, con->in_front_crc, m->footer.front_crc);
1482 return -EBADMSG;
1483 }
1484 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1485 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1486 m, con->in_middle_crc, m->footer.middle_crc);
1487 return -EBADMSG;
1488 }
1489 if (datacrc &&
1490 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1491 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1492 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1493 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1494 return -EBADMSG;
1495 }
1496
1497 return 1; /* done! */
1498}
1499
1500/*
1501 * Process message. This happens in the worker thread. The callback should
1502 * be careful not to do anything that waits on other incoming messages or it
1503 * may deadlock.
1504 */
1505static void process_message(struct ceph_connection *con)
1506{
5e095e8b 1507 struct ceph_msg *msg;
31b8006e 1508
5e095e8b 1509 msg = con->in_msg;
31b8006e
SW
1510 con->in_msg = NULL;
1511
1512 /* if first message, set peer_name */
1513 if (con->peer_name.type == 0)
dbad185d 1514 con->peer_name = msg->hdr.src;
31b8006e 1515
31b8006e 1516 con->in_seq++;
ec302645 1517 mutex_unlock(&con->mutex);
31b8006e
SW
1518
1519 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1520 msg, le64_to_cpu(msg->hdr.seq),
dbad185d 1521 ENTITY_NAME(msg->hdr.src),
31b8006e
SW
1522 le16_to_cpu(msg->hdr.type),
1523 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1524 le32_to_cpu(msg->hdr.front_len),
1525 le32_to_cpu(msg->hdr.data_len),
1526 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1527 con->ops->dispatch(con, msg);
ec302645
SW
1528
1529 mutex_lock(&con->mutex);
31b8006e
SW
1530 prepare_read_tag(con);
1531}
1532
1533
1534/*
1535 * Write something to the socket. Called in a worker thread when the
1536 * socket appears to be writeable and we have something ready to send.
1537 */
1538static int try_write(struct ceph_connection *con)
1539{
1540 struct ceph_messenger *msgr = con->msgr;
1541 int ret = 1;
1542
1543 dout("try_write start %p state %lu nref %d\n", con, con->state,
1544 atomic_read(&con->nref));
1545
31b8006e
SW
1546more:
1547 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1548
1549 /* open the socket first? */
1550 if (con->sock == NULL) {
1551 /*
1552 * if we were STANDBY and are reconnecting _this_
1553 * connection, bump connect_seq now. Always bump
1554 * global_seq.
1555 */
1556 if (test_and_clear_bit(STANDBY, &con->state))
1557 con->connect_seq++;
1558
eed0ef2c
SW
1559 prepare_write_banner(msgr, con);
1560 prepare_write_connect(msgr, con, 1);
1561 prepare_read_banner(con);
31b8006e 1562 set_bit(CONNECTING, &con->state);
eed0ef2c 1563 clear_bit(NEGOTIATING, &con->state);
31b8006e 1564
cf3e5c40 1565 BUG_ON(con->in_msg);
31b8006e
SW
1566 con->in_tag = CEPH_MSGR_TAG_READY;
1567 dout("try_write initiating connect on %p new state %lu\n",
1568 con, con->state);
1569 con->sock = ceph_tcp_connect(con);
1570 if (IS_ERR(con->sock)) {
1571 con->sock = NULL;
1572 con->error_msg = "connect error";
1573 ret = -1;
1574 goto out;
1575 }
1576 }
1577
1578more_kvec:
1579 /* kvec data queued? */
1580 if (con->out_skip) {
1581 ret = write_partial_skip(con);
1582 if (ret <= 0)
1583 goto done;
1584 if (ret < 0) {
1585 dout("try_write write_partial_skip err %d\n", ret);
1586 goto done;
1587 }
1588 }
1589 if (con->out_kvec_left) {
1590 ret = write_partial_kvec(con);
1591 if (ret <= 0)
1592 goto done;
31b8006e
SW
1593 }
1594
1595 /* msg pages? */
1596 if (con->out_msg) {
c86a2930
SW
1597 if (con->out_msg_done) {
1598 ceph_msg_put(con->out_msg);
1599 con->out_msg = NULL; /* we're done with this one */
1600 goto do_next;
1601 }
1602
31b8006e
SW
1603 ret = write_partial_msg_pages(con);
1604 if (ret == 1)
1605 goto more_kvec; /* we need to send the footer, too! */
1606 if (ret == 0)
1607 goto done;
1608 if (ret < 0) {
1609 dout("try_write write_partial_msg_pages err %d\n",
1610 ret);
1611 goto done;
1612 }
1613 }
1614
c86a2930 1615do_next:
31b8006e
SW
1616 if (!test_bit(CONNECTING, &con->state)) {
1617 /* is anything else pending? */
1618 if (!list_empty(&con->out_queue)) {
1619 prepare_write_message(con);
1620 goto more;
1621 }
1622 if (con->in_seq > con->in_seq_acked) {
1623 prepare_write_ack(con);
1624 goto more;
1625 }
1626 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
1627 prepare_write_keepalive(con);
1628 goto more;
1629 }
1630 }
1631
1632 /* Nothing to do! */
1633 clear_bit(WRITE_PENDING, &con->state);
1634 dout("try_write nothing else to write.\n");
1635done:
1636 ret = 0;
1637out:
31b8006e
SW
1638 dout("try_write done on %p\n", con);
1639 return ret;
1640}
1641
1642
1643
1644/*
1645 * Read what we can from the socket.
1646 */
1647static int try_read(struct ceph_connection *con)
1648{
31b8006e
SW
1649 int ret = -1;
1650
1651 if (!con->sock)
1652 return 0;
1653
1654 if (test_bit(STANDBY, &con->state))
1655 return 0;
1656
1657 dout("try_read start on %p\n", con);
ec302645 1658
31b8006e
SW
1659more:
1660 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
1661 con->in_base_pos);
1662 if (test_bit(CONNECTING, &con->state)) {
eed0ef2c
SW
1663 if (!test_bit(NEGOTIATING, &con->state)) {
1664 dout("try_read connecting\n");
1665 ret = read_partial_banner(con);
1666 if (ret <= 0)
1667 goto done;
1668 if (process_banner(con) < 0) {
1669 ret = -1;
1670 goto out;
1671 }
1672 }
31b8006e
SW
1673 ret = read_partial_connect(con);
1674 if (ret <= 0)
1675 goto done;
1676 if (process_connect(con) < 0) {
1677 ret = -1;
1678 goto out;
1679 }
1680 goto more;
1681 }
1682
1683 if (con->in_base_pos < 0) {
1684 /*
1685 * skipping + discarding content.
1686 *
1687 * FIXME: there must be a better way to do this!
1688 */
1689 static char buf[1024];
1690 int skip = min(1024, -con->in_base_pos);
1691 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
1692 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
1693 if (ret <= 0)
1694 goto done;
1695 con->in_base_pos += ret;
1696 if (con->in_base_pos)
1697 goto more;
1698 }
1699 if (con->in_tag == CEPH_MSGR_TAG_READY) {
1700 /*
1701 * what's next?
1702 */
1703 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
1704 if (ret <= 0)
1705 goto done;
1706 dout("try_read got tag %d\n", (int)con->in_tag);
1707 switch (con->in_tag) {
1708 case CEPH_MSGR_TAG_MSG:
1709 prepare_read_message(con);
1710 break;
1711 case CEPH_MSGR_TAG_ACK:
1712 prepare_read_ack(con);
1713 break;
1714 case CEPH_MSGR_TAG_CLOSE:
1715 set_bit(CLOSED, &con->state); /* fixme */
1716 goto done;
1717 default:
1718 goto bad_tag;
1719 }
1720 }
1721 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
1722 ret = read_partial_message(con);
1723 if (ret <= 0) {
1724 switch (ret) {
1725 case -EBADMSG:
1726 con->error_msg = "bad crc";
1727 ret = -EIO;
1728 goto out;
1729 case -EIO:
1730 con->error_msg = "io error";
1731 goto out;
1732 default:
1733 goto done;
1734 }
1735 }
1736 if (con->in_tag == CEPH_MSGR_TAG_READY)
1737 goto more;
1738 process_message(con);
1739 goto more;
1740 }
1741 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
1742 ret = read_partial_ack(con);
1743 if (ret <= 0)
1744 goto done;
1745 process_ack(con);
1746 goto more;
1747 }
1748
1749done:
1750 ret = 0;
1751out:
1752 dout("try_read done on %p\n", con);
1753 return ret;
1754
1755bad_tag:
1756 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
1757 con->error_msg = "protocol error, garbage tag";
1758 ret = -1;
1759 goto out;
1760}
1761
1762
1763/*
1764 * Atomically queue work on a connection. Bump @con reference to
1765 * avoid races with connection teardown.
1766 *
1767 * There is some trickery going on with QUEUED and BUSY because we
1768 * only want a _single_ thread operating on each connection at any
1769 * point in time, but we want to use all available CPUs.
1770 *
1771 * The worker thread only proceeds if it can atomically set BUSY. It
1772 * clears QUEUED and does it's thing. When it thinks it's done, it
1773 * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
1774 * (tries again to set BUSY).
1775 *
1776 * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
1777 * try to queue work. If that fails (work is already queued, or BUSY)
1778 * we give up (work also already being done or is queued) but leave QUEUED
1779 * set so that the worker thread will loop if necessary.
1780 */
1781static void queue_con(struct ceph_connection *con)
1782{
1783 if (test_bit(DEAD, &con->state)) {
1784 dout("queue_con %p ignoring: DEAD\n",
1785 con);
1786 return;
1787 }
1788
1789 if (!con->ops->get(con)) {
1790 dout("queue_con %p ref count 0\n", con);
1791 return;
1792 }
1793
1794 set_bit(QUEUED, &con->state);
1795 if (test_bit(BUSY, &con->state)) {
1796 dout("queue_con %p - already BUSY\n", con);
1797 con->ops->put(con);
1798 } else if (!queue_work(ceph_msgr_wq, &con->work.work)) {
1799 dout("queue_con %p - already queued\n", con);
1800 con->ops->put(con);
1801 } else {
1802 dout("queue_con %p\n", con);
1803 }
1804}
1805
1806/*
1807 * Do some work on a connection. Drop a connection ref when we're done.
1808 */
1809static void con_work(struct work_struct *work)
1810{
1811 struct ceph_connection *con = container_of(work, struct ceph_connection,
1812 work.work);
1813 int backoff = 0;
1814
1815more:
1816 if (test_and_set_bit(BUSY, &con->state) != 0) {
1817 dout("con_work %p BUSY already set\n", con);
1818 goto out;
1819 }
1820 dout("con_work %p start, clearing QUEUED\n", con);
1821 clear_bit(QUEUED, &con->state);
1822
9dd4658d
SW
1823 mutex_lock(&con->mutex);
1824
31b8006e
SW
1825 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
1826 dout("con_work CLOSED\n");
1827 con_close_socket(con);
1828 goto done;
1829 }
1830 if (test_and_clear_bit(OPENING, &con->state)) {
1831 /* reopen w/ new peer */
1832 dout("con_work OPENING\n");
1833 con_close_socket(con);
1834 }
1835
1836 if (test_and_clear_bit(SOCK_CLOSED, &con->state) ||
1837 try_read(con) < 0 ||
1838 try_write(con) < 0) {
9dd4658d 1839 mutex_unlock(&con->mutex);
31b8006e
SW
1840 backoff = 1;
1841 ceph_fault(con); /* error/fault path */
9dd4658d 1842 goto done_unlocked;
31b8006e
SW
1843 }
1844
1845done:
9dd4658d
SW
1846 mutex_unlock(&con->mutex);
1847
1848done_unlocked:
31b8006e
SW
1849 clear_bit(BUSY, &con->state);
1850 dout("con->state=%lu\n", con->state);
1851 if (test_bit(QUEUED, &con->state)) {
e2663ab6 1852 if (!backoff || test_bit(OPENING, &con->state)) {
31b8006e
SW
1853 dout("con_work %p QUEUED reset, looping\n", con);
1854 goto more;
1855 }
1856 dout("con_work %p QUEUED reset, but just faulted\n", con);
1857 clear_bit(QUEUED, &con->state);
1858 }
1859 dout("con_work %p done\n", con);
1860
1861out:
1862 con->ops->put(con);
1863}
1864
1865
1866/*
1867 * Generic error/fault handler. A retry mechanism is used with
1868 * exponential backoff
1869 */
1870static void ceph_fault(struct ceph_connection *con)
1871{
1872 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
1873 pr_addr(&con->peer_addr.in_addr), con->error_msg);
1874 dout("fault %p state %lu to peer %s\n",
1875 con, con->state, pr_addr(&con->peer_addr.in_addr));
1876
1877 if (test_bit(LOSSYTX, &con->state)) {
1878 dout("fault on LOSSYTX channel\n");
1879 goto out;
1880 }
1881
ec302645 1882 mutex_lock(&con->mutex);
91e45ce3
SW
1883 if (test_bit(CLOSED, &con->state))
1884 goto out_unlock;
ec302645 1885
31b8006e 1886 con_close_socket(con);
5e095e8b
SW
1887
1888 if (con->in_msg) {
1889 ceph_msg_put(con->in_msg);
1890 con->in_msg = NULL;
1891 }
31b8006e 1892
e80a52d1
SW
1893 /* Requeue anything that hasn't been acked */
1894 list_splice_init(&con->out_sent, &con->out_queue);
9bd2e6f8 1895
31b8006e
SW
1896 /* If there are no messages in the queue, place the connection
1897 * in a STANDBY state (i.e., don't try to reconnect just yet). */
31b8006e
SW
1898 if (list_empty(&con->out_queue) && !con->out_keepalive_pending) {
1899 dout("fault setting STANDBY\n");
1900 set_bit(STANDBY, &con->state);
e80a52d1
SW
1901 } else {
1902 /* retry after a delay. */
1903 if (con->delay == 0)
1904 con->delay = BASE_DELAY_INTERVAL;
1905 else if (con->delay < MAX_DELAY_INTERVAL)
1906 con->delay *= 2;
1907 dout("fault queueing %p delay %lu\n", con, con->delay);
1908 con->ops->get(con);
1909 if (queue_delayed_work(ceph_msgr_wq, &con->work,
1910 round_jiffies_relative(con->delay)) == 0)
1911 con->ops->put(con);
31b8006e
SW
1912 }
1913
91e45ce3
SW
1914out_unlock:
1915 mutex_unlock(&con->mutex);
31b8006e 1916out:
161fd65a
SW
1917 /*
1918 * in case we faulted due to authentication, invalidate our
1919 * current tickets so that we can get new ones.
1920 */
1921 if (con->auth_retry && con->ops->invalidate_authorizer) {
1922 dout("calling invalidate_authorizer()\n");
1923 con->ops->invalidate_authorizer(con);
1924 }
1925
31b8006e
SW
1926 if (con->ops->fault)
1927 con->ops->fault(con);
1928}
1929
1930
1931
1932/*
1933 * create a new messenger instance
1934 */
1935struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr)
1936{
1937 struct ceph_messenger *msgr;
1938
1939 msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
1940 if (msgr == NULL)
1941 return ERR_PTR(-ENOMEM);
1942
1943 spin_lock_init(&msgr->global_seq_lock);
1944
1945 /* the zero page is needed if a request is "canceled" while the message
1946 * is being written over the socket */
31459fe4 1947 msgr->zero_page = __page_cache_alloc(GFP_KERNEL | __GFP_ZERO);
31b8006e
SW
1948 if (!msgr->zero_page) {
1949 kfree(msgr);
1950 return ERR_PTR(-ENOMEM);
1951 }
1952 kmap(msgr->zero_page);
1953
1954 if (myaddr)
1955 msgr->inst.addr = *myaddr;
1956
1957 /* select a random nonce */
ac8839d7 1958 msgr->inst.addr.type = 0;
103e2d3a 1959 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
63f2d211 1960 encode_my_addr(msgr);
31b8006e
SW
1961
1962 dout("messenger_create %p\n", msgr);
1963 return msgr;
1964}
1965
1966void ceph_messenger_destroy(struct ceph_messenger *msgr)
1967{
1968 dout("destroy %p\n", msgr);
1969 kunmap(msgr->zero_page);
1970 __free_page(msgr->zero_page);
1971 kfree(msgr);
1972 dout("destroyed messenger %p\n", msgr);
1973}
1974
1975/*
1976 * Queue up an outgoing message on the given connection.
1977 */
1978void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
1979{
1980 if (test_bit(CLOSED, &con->state)) {
1981 dout("con_send %p closed, dropping %p\n", con, msg);
1982 ceph_msg_put(msg);
1983 return;
1984 }
1985
1986 /* set src+dst */
dbad185d 1987 msg->hdr.src = con->msgr->inst.name;
31b8006e 1988
3ca02ef9
SW
1989 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
1990
e84346b7
SW
1991 msg->needs_out_seq = true;
1992
31b8006e 1993 /* queue */
ec302645 1994 mutex_lock(&con->mutex);
31b8006e
SW
1995 BUG_ON(!list_empty(&msg->list_head));
1996 list_add_tail(&msg->list_head, &con->out_queue);
1997 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
1998 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
1999 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2000 le32_to_cpu(msg->hdr.front_len),
2001 le32_to_cpu(msg->hdr.middle_len),
2002 le32_to_cpu(msg->hdr.data_len));
ec302645 2003 mutex_unlock(&con->mutex);
31b8006e
SW
2004
2005 /* if there wasn't anything waiting to send before, queue
2006 * new work */
2007 if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2008 queue_con(con);
2009}
2010
2011/*
2012 * Revoke a message that was previously queued for send
2013 */
2014void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
2015{
ec302645 2016 mutex_lock(&con->mutex);
31b8006e
SW
2017 if (!list_empty(&msg->list_head)) {
2018 dout("con_revoke %p msg %p\n", con, msg);
2019 list_del_init(&msg->list_head);
2020 ceph_msg_put(msg);
2021 msg->hdr.seq = 0;
c86a2930
SW
2022 if (con->out_msg == msg) {
2023 ceph_msg_put(con->out_msg);
31b8006e 2024 con->out_msg = NULL;
c86a2930 2025 }
31b8006e
SW
2026 if (con->out_kvec_is_msg) {
2027 con->out_skip = con->out_kvec_bytes;
2028 con->out_kvec_is_msg = false;
2029 }
2030 } else {
2031 dout("con_revoke %p msg %p - not queued (sent?)\n", con, msg);
2032 }
ec302645 2033 mutex_unlock(&con->mutex);
31b8006e
SW
2034}
2035
350b1c32 2036/*
0d59ab81 2037 * Revoke a message that we may be reading data into
350b1c32 2038 */
0d59ab81 2039void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
350b1c32
SW
2040{
2041 mutex_lock(&con->mutex);
0d59ab81
YS
2042 if (con->in_msg && con->in_msg == msg) {
2043 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2044 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
350b1c32
SW
2045 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2046
2047 /* skip rest of message */
0d59ab81 2048 dout("con_revoke_pages %p msg %p revoked\n", con, msg);
350b1c32
SW
2049 con->in_base_pos = con->in_base_pos -
2050 sizeof(struct ceph_msg_header) -
0d59ab81
YS
2051 front_len -
2052 middle_len -
2053 data_len -
350b1c32 2054 sizeof(struct ceph_msg_footer);
350b1c32
SW
2055 ceph_msg_put(con->in_msg);
2056 con->in_msg = NULL;
2057 con->in_tag = CEPH_MSGR_TAG_READY;
684be25c 2058 con->in_seq++;
350b1c32
SW
2059 } else {
2060 dout("con_revoke_pages %p msg %p pages %p no-op\n",
0d59ab81 2061 con, con->in_msg, msg);
350b1c32
SW
2062 }
2063 mutex_unlock(&con->mutex);
2064}
2065
31b8006e
SW
2066/*
2067 * Queue a keepalive byte to ensure the tcp connection is alive.
2068 */
2069void ceph_con_keepalive(struct ceph_connection *con)
2070{
2071 if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
2072 test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2073 queue_con(con);
2074}
2075
2076
2077/*
2078 * construct a new message with given type, size
2079 * the new msg has a ref count of 1.
2080 */
34d23762 2081struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags)
31b8006e
SW
2082{
2083 struct ceph_msg *m;
2084
34d23762 2085 m = kmalloc(sizeof(*m), flags);
31b8006e
SW
2086 if (m == NULL)
2087 goto out;
c2e552e7 2088 kref_init(&m->kref);
31b8006e
SW
2089 INIT_LIST_HEAD(&m->list_head);
2090
45c6ceb5 2091 m->hdr.tid = 0;
31b8006e 2092 m->hdr.type = cpu_to_le16(type);
45c6ceb5
SW
2093 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2094 m->hdr.version = 0;
31b8006e
SW
2095 m->hdr.front_len = cpu_to_le32(front_len);
2096 m->hdr.middle_len = 0;
bb257664
SW
2097 m->hdr.data_len = 0;
2098 m->hdr.data_off = 0;
45c6ceb5 2099 m->hdr.reserved = 0;
31b8006e
SW
2100 m->footer.front_crc = 0;
2101 m->footer.middle_crc = 0;
2102 m->footer.data_crc = 0;
45c6ceb5 2103 m->footer.flags = 0;
31b8006e
SW
2104 m->front_max = front_len;
2105 m->front_is_vmalloc = false;
2106 m->more_to_follow = false;
2107 m->pool = NULL;
2108
2109 /* front */
2110 if (front_len) {
2111 if (front_len > PAGE_CACHE_SIZE) {
34d23762 2112 m->front.iov_base = __vmalloc(front_len, flags,
31b8006e
SW
2113 PAGE_KERNEL);
2114 m->front_is_vmalloc = true;
2115 } else {
34d23762 2116 m->front.iov_base = kmalloc(front_len, flags);
31b8006e
SW
2117 }
2118 if (m->front.iov_base == NULL) {
2119 pr_err("msg_new can't allocate %d bytes\n",
2120 front_len);
2121 goto out2;
2122 }
2123 } else {
2124 m->front.iov_base = NULL;
2125 }
2126 m->front.iov_len = front_len;
2127
2128 /* middle */
2129 m->middle = NULL;
2130
2131 /* data */
bb257664
SW
2132 m->nr_pages = 0;
2133 m->pages = NULL;
58bb3b37 2134 m->pagelist = NULL;
31b8006e 2135
bb257664 2136 dout("ceph_msg_new %p front %d\n", m, front_len);
31b8006e
SW
2137 return m;
2138
2139out2:
2140 ceph_msg_put(m);
2141out:
bb257664 2142 pr_err("msg_new can't create type %d front %d\n", type, front_len);
a79832f2 2143 return NULL;
31b8006e
SW
2144}
2145
31b8006e
SW
2146/*
2147 * Allocate "middle" portion of a message, if it is needed and wasn't
2148 * allocated by alloc_msg. This allows us to read a small fixed-size
2149 * per-type header in the front and then gracefully fail (i.e.,
2150 * propagate the error to the caller based on info in the front) when
2151 * the middle is too large.
2152 */
2450418c 2153static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
31b8006e
SW
2154{
2155 int type = le16_to_cpu(msg->hdr.type);
2156 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2157
2158 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2159 ceph_msg_type_name(type), middle_len);
2160 BUG_ON(!middle_len);
2161 BUG_ON(msg->middle);
2162
b6c1d5b8 2163 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
31b8006e
SW
2164 if (!msg->middle)
2165 return -ENOMEM;
2166 return 0;
2167}
2168
2450418c
YS
2169/*
2170 * Generic message allocator, for incoming messages.
2171 */
2172static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
2173 struct ceph_msg_header *hdr,
2174 int *skip)
2175{
2176 int type = le16_to_cpu(hdr->type);
2177 int front_len = le32_to_cpu(hdr->front_len);
2178 int middle_len = le32_to_cpu(hdr->middle_len);
2179 struct ceph_msg *msg = NULL;
2180 int ret;
2181
2182 if (con->ops->alloc_msg) {
0547a9b3 2183 mutex_unlock(&con->mutex);
2450418c 2184 msg = con->ops->alloc_msg(con, hdr, skip);
0547a9b3 2185 mutex_lock(&con->mutex);
a79832f2 2186 if (!msg || *skip)
2450418c
YS
2187 return NULL;
2188 }
2189 if (!msg) {
2190 *skip = 0;
34d23762 2191 msg = ceph_msg_new(type, front_len, GFP_NOFS);
2450418c
YS
2192 if (!msg) {
2193 pr_err("unable to allocate msg type %d len %d\n",
2194 type, front_len);
a79832f2 2195 return NULL;
2450418c
YS
2196 }
2197 }
9d7f0f13 2198 memcpy(&msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
2450418c 2199
bb257664 2200 if (middle_len && !msg->middle) {
2450418c 2201 ret = ceph_alloc_middle(con, msg);
2450418c
YS
2202 if (ret < 0) {
2203 ceph_msg_put(msg);
a79832f2 2204 return NULL;
2450418c
YS
2205 }
2206 }
9d7f0f13 2207
2450418c
YS
2208 return msg;
2209}
2210
31b8006e
SW
2211
2212/*
2213 * Free a generically kmalloc'd message.
2214 */
2215void ceph_msg_kfree(struct ceph_msg *m)
2216{
2217 dout("msg_kfree %p\n", m);
2218 if (m->front_is_vmalloc)
2219 vfree(m->front.iov_base);
2220 else
2221 kfree(m->front.iov_base);
2222 kfree(m);
2223}
2224
2225/*
2226 * Drop a msg ref. Destroy as needed.
2227 */
c2e552e7
SW
2228void ceph_msg_last_put(struct kref *kref)
2229{
2230 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
31b8006e 2231
c2e552e7
SW
2232 dout("ceph_msg_put last one on %p\n", m);
2233 WARN_ON(!list_empty(&m->list_head));
2234
2235 /* drop middle, data, if any */
2236 if (m->middle) {
2237 ceph_buffer_put(m->middle);
2238 m->middle = NULL;
31b8006e 2239 }
c2e552e7
SW
2240 m->nr_pages = 0;
2241 m->pages = NULL;
2242
58bb3b37
SW
2243 if (m->pagelist) {
2244 ceph_pagelist_release(m->pagelist);
2245 kfree(m->pagelist);
2246 m->pagelist = NULL;
2247 }
2248
c2e552e7
SW
2249 if (m->pool)
2250 ceph_msgpool_put(m->pool, m);
2251 else
2252 ceph_msg_kfree(m);
31b8006e 2253}
9ec7cab1
SW
2254
2255void ceph_msg_dump(struct ceph_msg *msg)
2256{
2257 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2258 msg->front_max, msg->nr_pages);
2259 print_hex_dump(KERN_DEBUG, "header: ",
2260 DUMP_PREFIX_OFFSET, 16, 1,
2261 &msg->hdr, sizeof(msg->hdr), true);
2262 print_hex_dump(KERN_DEBUG, " front: ",
2263 DUMP_PREFIX_OFFSET, 16, 1,
2264 msg->front.iov_base, msg->front.iov_len, true);
2265 if (msg->middle)
2266 print_hex_dump(KERN_DEBUG, "middle: ",
2267 DUMP_PREFIX_OFFSET, 16, 1,
2268 msg->middle->vec.iov_base,
2269 msg->middle->vec.iov_len, true);
2270 print_hex_dump(KERN_DEBUG, "footer: ",
2271 DUMP_PREFIX_OFFSET, 16, 1,
2272 &msg->footer, sizeof(msg->footer), true);
2273}