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