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