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