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