rbd: Clear ceph_msg->bio_iter for retransmitted message
[deliverable/linux.git] / net / ceph / messenger.c
1 #include <linux/ceph/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/slab.h>
10 #include <linux/socket.h>
11 #include <linux/string.h>
12 #include <linux/bio.h>
13 #include <linux/blkdev.h>
14 #include <linux/dns_resolver.h>
15 #include <net/tcp.h>
16
17 #include <linux/ceph/libceph.h>
18 #include <linux/ceph/messenger.h>
19 #include <linux/ceph/decode.h>
20 #include <linux/ceph/pagelist.h>
21 #include <linux/export.h>
22
23 /*
24 * Ceph uses the messenger to exchange ceph_msg messages with other
25 * hosts in the system. The messenger provides ordered and reliable
26 * delivery. We tolerate TCP disconnects by reconnecting (with
27 * exponential backoff) in the case of a fault (disconnection, bad
28 * crc, protocol error). Acks allow sent messages to be discarded by
29 * the sender.
30 */
31
32 /* State values for ceph_connection->sock_state; NEW is assumed to be 0 */
33
34 #define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
35 #define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
36 #define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
37 #define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
38 #define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
39
40 /* static tag bytes (protocol control messages) */
41 static char tag_msg = CEPH_MSGR_TAG_MSG;
42 static char tag_ack = CEPH_MSGR_TAG_ACK;
43 static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
44
45 #ifdef CONFIG_LOCKDEP
46 static struct lock_class_key socket_class;
47 #endif
48
49 /*
50 * When skipping (ignoring) a block of input we read it into a "skip
51 * buffer," which is this many bytes in size.
52 */
53 #define SKIP_BUF_SIZE 1024
54
55 static void queue_con(struct ceph_connection *con);
56 static void con_work(struct work_struct *);
57 static void ceph_fault(struct ceph_connection *con);
58
59 /*
60 * Nicely render a sockaddr as a string. An array of formatted
61 * strings is used, to approximate reentrancy.
62 */
63 #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
64 #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
65 #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
66 #define MAX_ADDR_STR_LEN 64 /* 54 is enough */
67
68 static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
69 static atomic_t addr_str_seq = ATOMIC_INIT(0);
70
71 static struct page *zero_page; /* used in certain error cases */
72
73 const char *ceph_pr_addr(const struct sockaddr_storage *ss)
74 {
75 int i;
76 char *s;
77 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
78 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
79
80 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
81 s = addr_str[i];
82
83 switch (ss->ss_family) {
84 case AF_INET:
85 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
86 ntohs(in4->sin_port));
87 break;
88
89 case AF_INET6:
90 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
91 ntohs(in6->sin6_port));
92 break;
93
94 default:
95 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
96 ss->ss_family);
97 }
98
99 return s;
100 }
101 EXPORT_SYMBOL(ceph_pr_addr);
102
103 static void encode_my_addr(struct ceph_messenger *msgr)
104 {
105 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
106 ceph_encode_addr(&msgr->my_enc_addr);
107 }
108
109 /*
110 * work queue for all reading and writing to/from the socket.
111 */
112 static struct workqueue_struct *ceph_msgr_wq;
113
114 void _ceph_msgr_exit(void)
115 {
116 if (ceph_msgr_wq) {
117 destroy_workqueue(ceph_msgr_wq);
118 ceph_msgr_wq = NULL;
119 }
120
121 BUG_ON(zero_page == NULL);
122 kunmap(zero_page);
123 page_cache_release(zero_page);
124 zero_page = NULL;
125 }
126
127 int ceph_msgr_init(void)
128 {
129 BUG_ON(zero_page != NULL);
130 zero_page = ZERO_PAGE(0);
131 page_cache_get(zero_page);
132
133 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0);
134 if (ceph_msgr_wq)
135 return 0;
136
137 pr_err("msgr_init failed to create workqueue\n");
138 _ceph_msgr_exit();
139
140 return -ENOMEM;
141 }
142 EXPORT_SYMBOL(ceph_msgr_init);
143
144 void ceph_msgr_exit(void)
145 {
146 BUG_ON(ceph_msgr_wq == NULL);
147
148 _ceph_msgr_exit();
149 }
150 EXPORT_SYMBOL(ceph_msgr_exit);
151
152 void ceph_msgr_flush(void)
153 {
154 flush_workqueue(ceph_msgr_wq);
155 }
156 EXPORT_SYMBOL(ceph_msgr_flush);
157
158 /* Connection socket state transition functions */
159
160 static void con_sock_state_init(struct ceph_connection *con)
161 {
162 int old_state;
163
164 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
165 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
166 printk("%s: unexpected old state %d\n", __func__, old_state);
167 }
168
169 static void con_sock_state_connecting(struct ceph_connection *con)
170 {
171 int old_state;
172
173 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
174 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
175 printk("%s: unexpected old state %d\n", __func__, old_state);
176 }
177
178 static void con_sock_state_connected(struct ceph_connection *con)
179 {
180 int old_state;
181
182 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
183 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
184 printk("%s: unexpected old state %d\n", __func__, old_state);
185 }
186
187 static void con_sock_state_closing(struct ceph_connection *con)
188 {
189 int old_state;
190
191 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
192 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
193 old_state != CON_SOCK_STATE_CONNECTED &&
194 old_state != CON_SOCK_STATE_CLOSING))
195 printk("%s: unexpected old state %d\n", __func__, old_state);
196 }
197
198 static void con_sock_state_closed(struct ceph_connection *con)
199 {
200 int old_state;
201
202 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
203 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
204 old_state != CON_SOCK_STATE_CLOSING))
205 printk("%s: unexpected old state %d\n", __func__, old_state);
206 }
207
208 /*
209 * socket callback functions
210 */
211
212 /* data available on socket, or listen socket received a connect */
213 static void ceph_sock_data_ready(struct sock *sk, int count_unused)
214 {
215 struct ceph_connection *con = sk->sk_user_data;
216
217 if (sk->sk_state != TCP_CLOSE_WAIT) {
218 dout("%s on %p state = %lu, queueing work\n", __func__,
219 con, con->state);
220 queue_con(con);
221 }
222 }
223
224 /* socket has buffer space for writing */
225 static void ceph_sock_write_space(struct sock *sk)
226 {
227 struct ceph_connection *con = sk->sk_user_data;
228
229 /* only queue to workqueue if there is data we want to write,
230 * and there is sufficient space in the socket buffer to accept
231 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
232 * doesn't get called again until try_write() fills the socket
233 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
234 * and net/core/stream.c:sk_stream_write_space().
235 */
236 if (test_bit(WRITE_PENDING, &con->flags)) {
237 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
238 dout("%s %p queueing write work\n", __func__, con);
239 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
240 queue_con(con);
241 }
242 } else {
243 dout("%s %p nothing to write\n", __func__, con);
244 }
245 }
246
247 /* socket's state has changed */
248 static void ceph_sock_state_change(struct sock *sk)
249 {
250 struct ceph_connection *con = sk->sk_user_data;
251
252 dout("%s %p state = %lu sk_state = %u\n", __func__,
253 con, con->state, sk->sk_state);
254
255 if (test_bit(CLOSED, &con->state))
256 return;
257
258 switch (sk->sk_state) {
259 case TCP_CLOSE:
260 dout("%s TCP_CLOSE\n", __func__);
261 case TCP_CLOSE_WAIT:
262 dout("%s TCP_CLOSE_WAIT\n", __func__);
263 con_sock_state_closing(con);
264 if (test_and_set_bit(SOCK_CLOSED, &con->flags) == 0) {
265 if (test_bit(CONNECTING, &con->state))
266 con->error_msg = "connection failed";
267 else
268 con->error_msg = "socket closed";
269 queue_con(con);
270 }
271 break;
272 case TCP_ESTABLISHED:
273 dout("%s TCP_ESTABLISHED\n", __func__);
274 con_sock_state_connected(con);
275 queue_con(con);
276 break;
277 default: /* Everything else is uninteresting */
278 break;
279 }
280 }
281
282 /*
283 * set up socket callbacks
284 */
285 static void set_sock_callbacks(struct socket *sock,
286 struct ceph_connection *con)
287 {
288 struct sock *sk = sock->sk;
289 sk->sk_user_data = con;
290 sk->sk_data_ready = ceph_sock_data_ready;
291 sk->sk_write_space = ceph_sock_write_space;
292 sk->sk_state_change = ceph_sock_state_change;
293 }
294
295
296 /*
297 * socket helpers
298 */
299
300 /*
301 * initiate connection to a remote socket.
302 */
303 static int ceph_tcp_connect(struct ceph_connection *con)
304 {
305 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
306 struct socket *sock;
307 int ret;
308
309 BUG_ON(con->sock);
310 ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM,
311 IPPROTO_TCP, &sock);
312 if (ret)
313 return ret;
314 sock->sk->sk_allocation = GFP_NOFS;
315
316 #ifdef CONFIG_LOCKDEP
317 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
318 #endif
319
320 set_sock_callbacks(sock, con);
321
322 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
323
324 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
325 O_NONBLOCK);
326 if (ret == -EINPROGRESS) {
327 dout("connect %s EINPROGRESS sk_state = %u\n",
328 ceph_pr_addr(&con->peer_addr.in_addr),
329 sock->sk->sk_state);
330 } else if (ret < 0) {
331 pr_err("connect %s error %d\n",
332 ceph_pr_addr(&con->peer_addr.in_addr), ret);
333 sock_release(sock);
334 con->error_msg = "connect error";
335
336 return ret;
337 }
338 con->sock = sock;
339 con_sock_state_connecting(con);
340
341 return 0;
342 }
343
344 static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
345 {
346 struct kvec iov = {buf, len};
347 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
348 int r;
349
350 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
351 if (r == -EAGAIN)
352 r = 0;
353 return r;
354 }
355
356 /*
357 * write something. @more is true if caller will be sending more data
358 * shortly.
359 */
360 static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
361 size_t kvlen, size_t len, int more)
362 {
363 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
364 int r;
365
366 if (more)
367 msg.msg_flags |= MSG_MORE;
368 else
369 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
370
371 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
372 if (r == -EAGAIN)
373 r = 0;
374 return r;
375 }
376
377 static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
378 int offset, size_t size, int more)
379 {
380 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
381 int ret;
382
383 ret = kernel_sendpage(sock, page, offset, size, flags);
384 if (ret == -EAGAIN)
385 ret = 0;
386
387 return ret;
388 }
389
390
391 /*
392 * Shutdown/close the socket for the given connection.
393 */
394 static int con_close_socket(struct ceph_connection *con)
395 {
396 int rc;
397
398 dout("con_close_socket on %p sock %p\n", con, con->sock);
399 if (!con->sock)
400 return 0;
401 set_bit(SOCK_CLOSED, &con->state);
402 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
403 sock_release(con->sock);
404 con->sock = NULL;
405 clear_bit(SOCK_CLOSED, &con->state);
406 con_sock_state_closed(con);
407 return rc;
408 }
409
410 /*
411 * Reset a connection. Discard all incoming and outgoing messages
412 * and clear *_seq state.
413 */
414 static void ceph_msg_remove(struct ceph_msg *msg)
415 {
416 list_del_init(&msg->list_head);
417 BUG_ON(msg->con == NULL);
418 ceph_con_put(msg->con);
419 msg->con = NULL;
420
421 ceph_msg_put(msg);
422 }
423 static void ceph_msg_remove_list(struct list_head *head)
424 {
425 while (!list_empty(head)) {
426 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
427 list_head);
428 ceph_msg_remove(msg);
429 }
430 }
431
432 static void reset_connection(struct ceph_connection *con)
433 {
434 /* reset connection, out_queue, msg_ and connect_seq */
435 /* discard existing out_queue and msg_seq */
436 ceph_msg_remove_list(&con->out_queue);
437 ceph_msg_remove_list(&con->out_sent);
438
439 if (con->in_msg) {
440 BUG_ON(con->in_msg->con != con);
441 con->in_msg->con = NULL;
442 ceph_msg_put(con->in_msg);
443 con->in_msg = NULL;
444 ceph_con_put(con->in_msg->con);
445 }
446
447 con->connect_seq = 0;
448 con->out_seq = 0;
449 if (con->out_msg) {
450 ceph_msg_put(con->out_msg);
451 con->out_msg = NULL;
452 }
453 con->in_seq = 0;
454 con->in_seq_acked = 0;
455 }
456
457 /*
458 * mark a peer down. drop any open connections.
459 */
460 void ceph_con_close(struct ceph_connection *con)
461 {
462 dout("con_close %p peer %s\n", con,
463 ceph_pr_addr(&con->peer_addr.in_addr));
464 clear_bit(NEGOTIATING, &con->state);
465 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
466 set_bit(CLOSED, &con->state);
467
468 clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */
469 clear_bit(KEEPALIVE_PENDING, &con->flags);
470 clear_bit(WRITE_PENDING, &con->flags);
471
472 mutex_lock(&con->mutex);
473 reset_connection(con);
474 con->peer_global_seq = 0;
475 cancel_delayed_work(&con->work);
476 mutex_unlock(&con->mutex);
477 queue_con(con);
478 }
479 EXPORT_SYMBOL(ceph_con_close);
480
481 /*
482 * Reopen a closed connection, with a new peer address.
483 */
484 void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
485 {
486 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
487 set_bit(OPENING, &con->state);
488 WARN_ON(!test_and_clear_bit(CLOSED, &con->state));
489
490 memcpy(&con->peer_addr, addr, sizeof(*addr));
491 con->delay = 0; /* reset backoff memory */
492 queue_con(con);
493 }
494 EXPORT_SYMBOL(ceph_con_open);
495
496 /*
497 * return true if this connection ever successfully opened
498 */
499 bool ceph_con_opened(struct ceph_connection *con)
500 {
501 return con->connect_seq > 0;
502 }
503
504 /*
505 * generic get/put
506 */
507 struct ceph_connection *ceph_con_get(struct ceph_connection *con)
508 {
509 int nref = __atomic_add_unless(&con->nref, 1, 0);
510
511 dout("con_get %p nref = %d -> %d\n", con, nref, nref + 1);
512
513 return nref ? con : NULL;
514 }
515
516 void ceph_con_put(struct ceph_connection *con)
517 {
518 int nref = atomic_dec_return(&con->nref);
519
520 BUG_ON(nref < 0);
521 if (nref == 0) {
522 BUG_ON(con->sock);
523 kfree(con);
524 }
525 dout("con_put %p nref = %d -> %d\n", con, nref + 1, nref);
526 }
527
528 /*
529 * initialize a new connection.
530 */
531 void ceph_con_init(struct ceph_connection *con, void *private,
532 const struct ceph_connection_operations *ops,
533 struct ceph_messenger *msgr, __u8 entity_type, __u64 entity_num)
534 {
535 dout("con_init %p\n", con);
536 memset(con, 0, sizeof(*con));
537 con->private = private;
538 con->ops = ops;
539 atomic_set(&con->nref, 1);
540 con->msgr = msgr;
541
542 con_sock_state_init(con);
543
544 con->peer_name.type = (__u8) entity_type;
545 con->peer_name.num = cpu_to_le64(entity_num);
546
547 mutex_init(&con->mutex);
548 INIT_LIST_HEAD(&con->out_queue);
549 INIT_LIST_HEAD(&con->out_sent);
550 INIT_DELAYED_WORK(&con->work, con_work);
551
552 set_bit(CLOSED, &con->state);
553 }
554 EXPORT_SYMBOL(ceph_con_init);
555
556
557 /*
558 * We maintain a global counter to order connection attempts. Get
559 * a unique seq greater than @gt.
560 */
561 static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
562 {
563 u32 ret;
564
565 spin_lock(&msgr->global_seq_lock);
566 if (msgr->global_seq < gt)
567 msgr->global_seq = gt;
568 ret = ++msgr->global_seq;
569 spin_unlock(&msgr->global_seq_lock);
570 return ret;
571 }
572
573 static void con_out_kvec_reset(struct ceph_connection *con)
574 {
575 con->out_kvec_left = 0;
576 con->out_kvec_bytes = 0;
577 con->out_kvec_cur = &con->out_kvec[0];
578 }
579
580 static void con_out_kvec_add(struct ceph_connection *con,
581 size_t size, void *data)
582 {
583 int index;
584
585 index = con->out_kvec_left;
586 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
587
588 con->out_kvec[index].iov_len = size;
589 con->out_kvec[index].iov_base = data;
590 con->out_kvec_left++;
591 con->out_kvec_bytes += size;
592 }
593
594 /*
595 * Prepare footer for currently outgoing message, and finish things
596 * off. Assumes out_kvec* are already valid.. we just add on to the end.
597 */
598 static void prepare_write_message_footer(struct ceph_connection *con)
599 {
600 struct ceph_msg *m = con->out_msg;
601 int v = con->out_kvec_left;
602
603 dout("prepare_write_message_footer %p\n", con);
604 con->out_kvec_is_msg = true;
605 con->out_kvec[v].iov_base = &m->footer;
606 con->out_kvec[v].iov_len = sizeof(m->footer);
607 con->out_kvec_bytes += sizeof(m->footer);
608 con->out_kvec_left++;
609 con->out_more = m->more_to_follow;
610 con->out_msg_done = true;
611 }
612
613 /*
614 * Prepare headers for the next outgoing message.
615 */
616 static void prepare_write_message(struct ceph_connection *con)
617 {
618 struct ceph_msg *m;
619 u32 crc;
620
621 con_out_kvec_reset(con);
622 con->out_kvec_is_msg = true;
623 con->out_msg_done = false;
624
625 /* Sneak an ack in there first? If we can get it into the same
626 * TCP packet that's a good thing. */
627 if (con->in_seq > con->in_seq_acked) {
628 con->in_seq_acked = con->in_seq;
629 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
630 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
631 con_out_kvec_add(con, sizeof (con->out_temp_ack),
632 &con->out_temp_ack);
633 }
634
635 BUG_ON(list_empty(&con->out_queue));
636 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
637 con->out_msg = m;
638 BUG_ON(m->con != con);
639
640 /* put message on sent list */
641 ceph_msg_get(m);
642 list_move_tail(&m->list_head, &con->out_sent);
643
644 /*
645 * only assign outgoing seq # if we haven't sent this message
646 * yet. if it is requeued, resend with it's original seq.
647 */
648 if (m->needs_out_seq) {
649 m->hdr.seq = cpu_to_le64(++con->out_seq);
650 m->needs_out_seq = false;
651 }
652 #ifdef CONFIG_BLOCK
653 else
654 m->bio_iter = NULL;
655 #endif
656
657 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
658 m, con->out_seq, le16_to_cpu(m->hdr.type),
659 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
660 le32_to_cpu(m->hdr.data_len),
661 m->nr_pages);
662 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
663
664 /* tag + hdr + front + middle */
665 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
666 con_out_kvec_add(con, sizeof (m->hdr), &m->hdr);
667 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
668
669 if (m->middle)
670 con_out_kvec_add(con, m->middle->vec.iov_len,
671 m->middle->vec.iov_base);
672
673 /* fill in crc (except data pages), footer */
674 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
675 con->out_msg->hdr.crc = cpu_to_le32(crc);
676 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
677
678 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
679 con->out_msg->footer.front_crc = cpu_to_le32(crc);
680 if (m->middle) {
681 crc = crc32c(0, m->middle->vec.iov_base,
682 m->middle->vec.iov_len);
683 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
684 } else
685 con->out_msg->footer.middle_crc = 0;
686 con->out_msg->footer.data_crc = 0;
687 dout("prepare_write_message front_crc %u data_crc %u\n",
688 le32_to_cpu(con->out_msg->footer.front_crc),
689 le32_to_cpu(con->out_msg->footer.middle_crc));
690
691 /* is there a data payload? */
692 if (le32_to_cpu(m->hdr.data_len) > 0) {
693 /* initialize page iterator */
694 con->out_msg_pos.page = 0;
695 if (m->pages)
696 con->out_msg_pos.page_pos = m->page_alignment;
697 else
698 con->out_msg_pos.page_pos = 0;
699 con->out_msg_pos.data_pos = 0;
700 con->out_msg_pos.did_page_crc = false;
701 con->out_more = 1; /* data + footer will follow */
702 } else {
703 /* no, queue up footer too and be done */
704 prepare_write_message_footer(con);
705 }
706
707 set_bit(WRITE_PENDING, &con->flags);
708 }
709
710 /*
711 * Prepare an ack.
712 */
713 static void prepare_write_ack(struct ceph_connection *con)
714 {
715 dout("prepare_write_ack %p %llu -> %llu\n", con,
716 con->in_seq_acked, con->in_seq);
717 con->in_seq_acked = con->in_seq;
718
719 con_out_kvec_reset(con);
720
721 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
722
723 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
724 con_out_kvec_add(con, sizeof (con->out_temp_ack),
725 &con->out_temp_ack);
726
727 con->out_more = 1; /* more will follow.. eventually.. */
728 set_bit(WRITE_PENDING, &con->flags);
729 }
730
731 /*
732 * Prepare to write keepalive byte.
733 */
734 static void prepare_write_keepalive(struct ceph_connection *con)
735 {
736 dout("prepare_write_keepalive %p\n", con);
737 con_out_kvec_reset(con);
738 con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive);
739 set_bit(WRITE_PENDING, &con->flags);
740 }
741
742 /*
743 * Connection negotiation.
744 */
745
746 static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
747 int *auth_proto)
748 {
749 struct ceph_auth_handshake *auth;
750
751 if (!con->ops->get_authorizer) {
752 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
753 con->out_connect.authorizer_len = 0;
754
755 return NULL;
756 }
757
758 /* Can't hold the mutex while getting authorizer */
759
760 mutex_unlock(&con->mutex);
761
762 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
763
764 mutex_lock(&con->mutex);
765
766 if (IS_ERR(auth))
767 return auth;
768 if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags))
769 return ERR_PTR(-EAGAIN);
770
771 con->auth_reply_buf = auth->authorizer_reply_buf;
772 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
773
774
775 return auth;
776 }
777
778 /*
779 * We connected to a peer and are saying hello.
780 */
781 static void prepare_write_banner(struct ceph_connection *con)
782 {
783 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
784 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
785 &con->msgr->my_enc_addr);
786
787 con->out_more = 0;
788 set_bit(WRITE_PENDING, &con->flags);
789 }
790
791 static int prepare_write_connect(struct ceph_connection *con)
792 {
793 unsigned global_seq = get_global_seq(con->msgr, 0);
794 int proto;
795 int auth_proto;
796 struct ceph_auth_handshake *auth;
797
798 switch (con->peer_name.type) {
799 case CEPH_ENTITY_TYPE_MON:
800 proto = CEPH_MONC_PROTOCOL;
801 break;
802 case CEPH_ENTITY_TYPE_OSD:
803 proto = CEPH_OSDC_PROTOCOL;
804 break;
805 case CEPH_ENTITY_TYPE_MDS:
806 proto = CEPH_MDSC_PROTOCOL;
807 break;
808 default:
809 BUG();
810 }
811
812 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
813 con->connect_seq, global_seq, proto);
814
815 con->out_connect.features = cpu_to_le64(con->msgr->supported_features);
816 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
817 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
818 con->out_connect.global_seq = cpu_to_le32(global_seq);
819 con->out_connect.protocol_version = cpu_to_le32(proto);
820 con->out_connect.flags = 0;
821
822 auth_proto = CEPH_AUTH_UNKNOWN;
823 auth = get_connect_authorizer(con, &auth_proto);
824 if (IS_ERR(auth))
825 return PTR_ERR(auth);
826
827 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
828 con->out_connect.authorizer_len = auth ?
829 cpu_to_le32(auth->authorizer_buf_len) : 0;
830
831 con_out_kvec_add(con, sizeof (con->out_connect),
832 &con->out_connect);
833 if (auth && auth->authorizer_buf_len)
834 con_out_kvec_add(con, auth->authorizer_buf_len,
835 auth->authorizer_buf);
836
837 con->out_more = 0;
838 set_bit(WRITE_PENDING, &con->flags);
839
840 return 0;
841 }
842
843 /*
844 * write as much of pending kvecs to the socket as we can.
845 * 1 -> done
846 * 0 -> socket full, but more to do
847 * <0 -> error
848 */
849 static int write_partial_kvec(struct ceph_connection *con)
850 {
851 int ret;
852
853 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
854 while (con->out_kvec_bytes > 0) {
855 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
856 con->out_kvec_left, con->out_kvec_bytes,
857 con->out_more);
858 if (ret <= 0)
859 goto out;
860 con->out_kvec_bytes -= ret;
861 if (con->out_kvec_bytes == 0)
862 break; /* done */
863
864 /* account for full iov entries consumed */
865 while (ret >= con->out_kvec_cur->iov_len) {
866 BUG_ON(!con->out_kvec_left);
867 ret -= con->out_kvec_cur->iov_len;
868 con->out_kvec_cur++;
869 con->out_kvec_left--;
870 }
871 /* and for a partially-consumed entry */
872 if (ret) {
873 con->out_kvec_cur->iov_len -= ret;
874 con->out_kvec_cur->iov_base += ret;
875 }
876 }
877 con->out_kvec_left = 0;
878 con->out_kvec_is_msg = false;
879 ret = 1;
880 out:
881 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
882 con->out_kvec_bytes, con->out_kvec_left, ret);
883 return ret; /* done! */
884 }
885
886 #ifdef CONFIG_BLOCK
887 static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg)
888 {
889 if (!bio) {
890 *iter = NULL;
891 *seg = 0;
892 return;
893 }
894 *iter = bio;
895 *seg = bio->bi_idx;
896 }
897
898 static void iter_bio_next(struct bio **bio_iter, int *seg)
899 {
900 if (*bio_iter == NULL)
901 return;
902
903 BUG_ON(*seg >= (*bio_iter)->bi_vcnt);
904
905 (*seg)++;
906 if (*seg == (*bio_iter)->bi_vcnt)
907 init_bio_iter((*bio_iter)->bi_next, bio_iter, seg);
908 }
909 #endif
910
911 /*
912 * Write as much message data payload as we can. If we finish, queue
913 * up the footer.
914 * 1 -> done, footer is now queued in out_kvec[].
915 * 0 -> socket full, but more to do
916 * <0 -> error
917 */
918 static int write_partial_msg_pages(struct ceph_connection *con)
919 {
920 struct ceph_msg *msg = con->out_msg;
921 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
922 size_t len;
923 bool do_datacrc = !con->msgr->nocrc;
924 int ret;
925 int total_max_write;
926 int in_trail = 0;
927 size_t trail_len = (msg->trail ? msg->trail->length : 0);
928
929 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
930 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
931 con->out_msg_pos.page_pos);
932
933 #ifdef CONFIG_BLOCK
934 if (msg->bio && !msg->bio_iter)
935 init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg);
936 #endif
937
938 while (data_len > con->out_msg_pos.data_pos) {
939 struct page *page = NULL;
940 int max_write = PAGE_SIZE;
941 int bio_offset = 0;
942
943 total_max_write = data_len - trail_len -
944 con->out_msg_pos.data_pos;
945
946 /*
947 * if we are calculating the data crc (the default), we need
948 * to map the page. if our pages[] has been revoked, use the
949 * zero page.
950 */
951
952 /* have we reached the trail part of the data? */
953 if (con->out_msg_pos.data_pos >= data_len - trail_len) {
954 in_trail = 1;
955
956 total_max_write = data_len - con->out_msg_pos.data_pos;
957
958 page = list_first_entry(&msg->trail->head,
959 struct page, lru);
960 max_write = PAGE_SIZE;
961 } else if (msg->pages) {
962 page = msg->pages[con->out_msg_pos.page];
963 } else if (msg->pagelist) {
964 page = list_first_entry(&msg->pagelist->head,
965 struct page, lru);
966 #ifdef CONFIG_BLOCK
967 } else if (msg->bio) {
968 struct bio_vec *bv;
969
970 bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg);
971 page = bv->bv_page;
972 bio_offset = bv->bv_offset;
973 max_write = bv->bv_len;
974 #endif
975 } else {
976 page = zero_page;
977 }
978 len = min_t(int, max_write - con->out_msg_pos.page_pos,
979 total_max_write);
980
981 if (do_datacrc && !con->out_msg_pos.did_page_crc) {
982 void *base;
983 u32 crc;
984 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
985 char *kaddr;
986
987 kaddr = kmap(page);
988 BUG_ON(kaddr == NULL);
989 base = kaddr + con->out_msg_pos.page_pos + bio_offset;
990 crc = crc32c(tmpcrc, base, len);
991 con->out_msg->footer.data_crc = cpu_to_le32(crc);
992 con->out_msg_pos.did_page_crc = true;
993 }
994 ret = ceph_tcp_sendpage(con->sock, page,
995 con->out_msg_pos.page_pos + bio_offset,
996 len, 1);
997
998 if (do_datacrc)
999 kunmap(page);
1000
1001 if (ret <= 0)
1002 goto out;
1003
1004 con->out_msg_pos.data_pos += ret;
1005 con->out_msg_pos.page_pos += ret;
1006 if (ret == len) {
1007 con->out_msg_pos.page_pos = 0;
1008 con->out_msg_pos.page++;
1009 con->out_msg_pos.did_page_crc = false;
1010 if (in_trail)
1011 list_move_tail(&page->lru,
1012 &msg->trail->head);
1013 else if (msg->pagelist)
1014 list_move_tail(&page->lru,
1015 &msg->pagelist->head);
1016 #ifdef CONFIG_BLOCK
1017 else if (msg->bio)
1018 iter_bio_next(&msg->bio_iter, &msg->bio_seg);
1019 #endif
1020 }
1021 }
1022
1023 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
1024
1025 /* prepare and queue up footer, too */
1026 if (!do_datacrc)
1027 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
1028 con_out_kvec_reset(con);
1029 prepare_write_message_footer(con);
1030 ret = 1;
1031 out:
1032 return ret;
1033 }
1034
1035 /*
1036 * write some zeros
1037 */
1038 static int write_partial_skip(struct ceph_connection *con)
1039 {
1040 int ret;
1041
1042 while (con->out_skip > 0) {
1043 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
1044
1045 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1);
1046 if (ret <= 0)
1047 goto out;
1048 con->out_skip -= ret;
1049 }
1050 ret = 1;
1051 out:
1052 return ret;
1053 }
1054
1055 /*
1056 * Prepare to read connection handshake, or an ack.
1057 */
1058 static void prepare_read_banner(struct ceph_connection *con)
1059 {
1060 dout("prepare_read_banner %p\n", con);
1061 con->in_base_pos = 0;
1062 }
1063
1064 static void prepare_read_connect(struct ceph_connection *con)
1065 {
1066 dout("prepare_read_connect %p\n", con);
1067 con->in_base_pos = 0;
1068 }
1069
1070 static void prepare_read_ack(struct ceph_connection *con)
1071 {
1072 dout("prepare_read_ack %p\n", con);
1073 con->in_base_pos = 0;
1074 }
1075
1076 static void prepare_read_tag(struct ceph_connection *con)
1077 {
1078 dout("prepare_read_tag %p\n", con);
1079 con->in_base_pos = 0;
1080 con->in_tag = CEPH_MSGR_TAG_READY;
1081 }
1082
1083 /*
1084 * Prepare to read a message.
1085 */
1086 static int prepare_read_message(struct ceph_connection *con)
1087 {
1088 dout("prepare_read_message %p\n", con);
1089 BUG_ON(con->in_msg != NULL);
1090 con->in_base_pos = 0;
1091 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1092 return 0;
1093 }
1094
1095
1096 static int read_partial(struct ceph_connection *con,
1097 int end, int size, void *object)
1098 {
1099 while (con->in_base_pos < end) {
1100 int left = end - con->in_base_pos;
1101 int have = size - left;
1102 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1103 if (ret <= 0)
1104 return ret;
1105 con->in_base_pos += ret;
1106 }
1107 return 1;
1108 }
1109
1110
1111 /*
1112 * Read all or part of the connect-side handshake on a new connection
1113 */
1114 static int read_partial_banner(struct ceph_connection *con)
1115 {
1116 int size;
1117 int end;
1118 int ret;
1119
1120 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
1121
1122 /* peer's banner */
1123 size = strlen(CEPH_BANNER);
1124 end = size;
1125 ret = read_partial(con, end, size, con->in_banner);
1126 if (ret <= 0)
1127 goto out;
1128
1129 size = sizeof (con->actual_peer_addr);
1130 end += size;
1131 ret = read_partial(con, end, size, &con->actual_peer_addr);
1132 if (ret <= 0)
1133 goto out;
1134
1135 size = sizeof (con->peer_addr_for_me);
1136 end += size;
1137 ret = read_partial(con, end, size, &con->peer_addr_for_me);
1138 if (ret <= 0)
1139 goto out;
1140
1141 out:
1142 return ret;
1143 }
1144
1145 static int read_partial_connect(struct ceph_connection *con)
1146 {
1147 int size;
1148 int end;
1149 int ret;
1150
1151 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1152
1153 size = sizeof (con->in_reply);
1154 end = size;
1155 ret = read_partial(con, end, size, &con->in_reply);
1156 if (ret <= 0)
1157 goto out;
1158
1159 size = le32_to_cpu(con->in_reply.authorizer_len);
1160 end += size;
1161 ret = read_partial(con, end, size, con->auth_reply_buf);
1162 if (ret <= 0)
1163 goto out;
1164
1165 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1166 con, (int)con->in_reply.tag,
1167 le32_to_cpu(con->in_reply.connect_seq),
1168 le32_to_cpu(con->in_reply.global_seq));
1169 out:
1170 return ret;
1171
1172 }
1173
1174 /*
1175 * Verify the hello banner looks okay.
1176 */
1177 static int verify_hello(struct ceph_connection *con)
1178 {
1179 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
1180 pr_err("connect to %s got bad banner\n",
1181 ceph_pr_addr(&con->peer_addr.in_addr));
1182 con->error_msg = "protocol error, bad banner";
1183 return -1;
1184 }
1185 return 0;
1186 }
1187
1188 static bool addr_is_blank(struct sockaddr_storage *ss)
1189 {
1190 switch (ss->ss_family) {
1191 case AF_INET:
1192 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
1193 case AF_INET6:
1194 return
1195 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
1196 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
1197 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
1198 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
1199 }
1200 return false;
1201 }
1202
1203 static int addr_port(struct sockaddr_storage *ss)
1204 {
1205 switch (ss->ss_family) {
1206 case AF_INET:
1207 return ntohs(((struct sockaddr_in *)ss)->sin_port);
1208 case AF_INET6:
1209 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
1210 }
1211 return 0;
1212 }
1213
1214 static void addr_set_port(struct sockaddr_storage *ss, int p)
1215 {
1216 switch (ss->ss_family) {
1217 case AF_INET:
1218 ((struct sockaddr_in *)ss)->sin_port = htons(p);
1219 break;
1220 case AF_INET6:
1221 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
1222 break;
1223 }
1224 }
1225
1226 /*
1227 * Unlike other *_pton function semantics, zero indicates success.
1228 */
1229 static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1230 char delim, const char **ipend)
1231 {
1232 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1233 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
1234
1235 memset(ss, 0, sizeof(*ss));
1236
1237 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1238 ss->ss_family = AF_INET;
1239 return 0;
1240 }
1241
1242 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1243 ss->ss_family = AF_INET6;
1244 return 0;
1245 }
1246
1247 return -EINVAL;
1248 }
1249
1250 /*
1251 * Extract hostname string and resolve using kernel DNS facility.
1252 */
1253 #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1254 static int ceph_dns_resolve_name(const char *name, size_t namelen,
1255 struct sockaddr_storage *ss, char delim, const char **ipend)
1256 {
1257 const char *end, *delim_p;
1258 char *colon_p, *ip_addr = NULL;
1259 int ip_len, ret;
1260
1261 /*
1262 * The end of the hostname occurs immediately preceding the delimiter or
1263 * the port marker (':') where the delimiter takes precedence.
1264 */
1265 delim_p = memchr(name, delim, namelen);
1266 colon_p = memchr(name, ':', namelen);
1267
1268 if (delim_p && colon_p)
1269 end = delim_p < colon_p ? delim_p : colon_p;
1270 else if (!delim_p && colon_p)
1271 end = colon_p;
1272 else {
1273 end = delim_p;
1274 if (!end) /* case: hostname:/ */
1275 end = name + namelen;
1276 }
1277
1278 if (end <= name)
1279 return -EINVAL;
1280
1281 /* do dns_resolve upcall */
1282 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1283 if (ip_len > 0)
1284 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1285 else
1286 ret = -ESRCH;
1287
1288 kfree(ip_addr);
1289
1290 *ipend = end;
1291
1292 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1293 ret, ret ? "failed" : ceph_pr_addr(ss));
1294
1295 return ret;
1296 }
1297 #else
1298 static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1299 struct sockaddr_storage *ss, char delim, const char **ipend)
1300 {
1301 return -EINVAL;
1302 }
1303 #endif
1304
1305 /*
1306 * Parse a server name (IP or hostname). If a valid IP address is not found
1307 * then try to extract a hostname to resolve using userspace DNS upcall.
1308 */
1309 static int ceph_parse_server_name(const char *name, size_t namelen,
1310 struct sockaddr_storage *ss, char delim, const char **ipend)
1311 {
1312 int ret;
1313
1314 ret = ceph_pton(name, namelen, ss, delim, ipend);
1315 if (ret)
1316 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1317
1318 return ret;
1319 }
1320
1321 /*
1322 * Parse an ip[:port] list into an addr array. Use the default
1323 * monitor port if a port isn't specified.
1324 */
1325 int ceph_parse_ips(const char *c, const char *end,
1326 struct ceph_entity_addr *addr,
1327 int max_count, int *count)
1328 {
1329 int i, ret = -EINVAL;
1330 const char *p = c;
1331
1332 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1333 for (i = 0; i < max_count; i++) {
1334 const char *ipend;
1335 struct sockaddr_storage *ss = &addr[i].in_addr;
1336 int port;
1337 char delim = ',';
1338
1339 if (*p == '[') {
1340 delim = ']';
1341 p++;
1342 }
1343
1344 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1345 if (ret)
1346 goto bad;
1347 ret = -EINVAL;
1348
1349 p = ipend;
1350
1351 if (delim == ']') {
1352 if (*p != ']') {
1353 dout("missing matching ']'\n");
1354 goto bad;
1355 }
1356 p++;
1357 }
1358
1359 /* port? */
1360 if (p < end && *p == ':') {
1361 port = 0;
1362 p++;
1363 while (p < end && *p >= '0' && *p <= '9') {
1364 port = (port * 10) + (*p - '0');
1365 p++;
1366 }
1367 if (port > 65535 || port == 0)
1368 goto bad;
1369 } else {
1370 port = CEPH_MON_PORT;
1371 }
1372
1373 addr_set_port(ss, port);
1374
1375 dout("parse_ips got %s\n", ceph_pr_addr(ss));
1376
1377 if (p == end)
1378 break;
1379 if (*p != ',')
1380 goto bad;
1381 p++;
1382 }
1383
1384 if (p != end)
1385 goto bad;
1386
1387 if (count)
1388 *count = i + 1;
1389 return 0;
1390
1391 bad:
1392 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
1393 return ret;
1394 }
1395 EXPORT_SYMBOL(ceph_parse_ips);
1396
1397 static int process_banner(struct ceph_connection *con)
1398 {
1399 dout("process_banner on %p\n", con);
1400
1401 if (verify_hello(con) < 0)
1402 return -1;
1403
1404 ceph_decode_addr(&con->actual_peer_addr);
1405 ceph_decode_addr(&con->peer_addr_for_me);
1406
1407 /*
1408 * Make sure the other end is who we wanted. note that the other
1409 * end may not yet know their ip address, so if it's 0.0.0.0, give
1410 * them the benefit of the doubt.
1411 */
1412 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1413 sizeof(con->peer_addr)) != 0 &&
1414 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1415 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
1416 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
1417 ceph_pr_addr(&con->peer_addr.in_addr),
1418 (int)le32_to_cpu(con->peer_addr.nonce),
1419 ceph_pr_addr(&con->actual_peer_addr.in_addr),
1420 (int)le32_to_cpu(con->actual_peer_addr.nonce));
1421 con->error_msg = "wrong peer at address";
1422 return -1;
1423 }
1424
1425 /*
1426 * did we learn our address?
1427 */
1428 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1429 int port = addr_port(&con->msgr->inst.addr.in_addr);
1430
1431 memcpy(&con->msgr->inst.addr.in_addr,
1432 &con->peer_addr_for_me.in_addr,
1433 sizeof(con->peer_addr_for_me.in_addr));
1434 addr_set_port(&con->msgr->inst.addr.in_addr, port);
1435 encode_my_addr(con->msgr);
1436 dout("process_banner learned my addr is %s\n",
1437 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
1438 }
1439
1440 set_bit(NEGOTIATING, &con->state);
1441 prepare_read_connect(con);
1442 return 0;
1443 }
1444
1445 static void fail_protocol(struct ceph_connection *con)
1446 {
1447 reset_connection(con);
1448 set_bit(CLOSED, &con->state); /* in case there's queued work */
1449 }
1450
1451 static int process_connect(struct ceph_connection *con)
1452 {
1453 u64 sup_feat = con->msgr->supported_features;
1454 u64 req_feat = con->msgr->required_features;
1455 u64 server_feat = le64_to_cpu(con->in_reply.features);
1456 int ret;
1457
1458 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1459
1460 switch (con->in_reply.tag) {
1461 case CEPH_MSGR_TAG_FEATURES:
1462 pr_err("%s%lld %s feature set mismatch,"
1463 " my %llx < server's %llx, missing %llx\n",
1464 ENTITY_NAME(con->peer_name),
1465 ceph_pr_addr(&con->peer_addr.in_addr),
1466 sup_feat, server_feat, server_feat & ~sup_feat);
1467 con->error_msg = "missing required protocol features";
1468 fail_protocol(con);
1469 return -1;
1470
1471 case CEPH_MSGR_TAG_BADPROTOVER:
1472 pr_err("%s%lld %s protocol version mismatch,"
1473 " my %d != server's %d\n",
1474 ENTITY_NAME(con->peer_name),
1475 ceph_pr_addr(&con->peer_addr.in_addr),
1476 le32_to_cpu(con->out_connect.protocol_version),
1477 le32_to_cpu(con->in_reply.protocol_version));
1478 con->error_msg = "protocol version mismatch";
1479 fail_protocol(con);
1480 return -1;
1481
1482 case CEPH_MSGR_TAG_BADAUTHORIZER:
1483 con->auth_retry++;
1484 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1485 con->auth_retry);
1486 if (con->auth_retry == 2) {
1487 con->error_msg = "connect authorization failure";
1488 return -1;
1489 }
1490 con->auth_retry = 1;
1491 con_out_kvec_reset(con);
1492 ret = prepare_write_connect(con);
1493 if (ret < 0)
1494 return ret;
1495 prepare_read_connect(con);
1496 break;
1497
1498 case CEPH_MSGR_TAG_RESETSESSION:
1499 /*
1500 * If we connected with a large connect_seq but the peer
1501 * has no record of a session with us (no connection, or
1502 * connect_seq == 0), they will send RESETSESION to indicate
1503 * that they must have reset their session, and may have
1504 * dropped messages.
1505 */
1506 dout("process_connect got RESET peer seq %u\n",
1507 le32_to_cpu(con->in_connect.connect_seq));
1508 pr_err("%s%lld %s connection reset\n",
1509 ENTITY_NAME(con->peer_name),
1510 ceph_pr_addr(&con->peer_addr.in_addr));
1511 reset_connection(con);
1512 con_out_kvec_reset(con);
1513 ret = prepare_write_connect(con);
1514 if (ret < 0)
1515 return ret;
1516 prepare_read_connect(con);
1517
1518 /* Tell ceph about it. */
1519 mutex_unlock(&con->mutex);
1520 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1521 if (con->ops->peer_reset)
1522 con->ops->peer_reset(con);
1523 mutex_lock(&con->mutex);
1524 if (test_bit(CLOSED, &con->state) ||
1525 test_bit(OPENING, &con->state))
1526 return -EAGAIN;
1527 break;
1528
1529 case CEPH_MSGR_TAG_RETRY_SESSION:
1530 /*
1531 * If we sent a smaller connect_seq than the peer has, try
1532 * again with a larger value.
1533 */
1534 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1535 le32_to_cpu(con->out_connect.connect_seq),
1536 le32_to_cpu(con->in_connect.connect_seq));
1537 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
1538 con_out_kvec_reset(con);
1539 ret = prepare_write_connect(con);
1540 if (ret < 0)
1541 return ret;
1542 prepare_read_connect(con);
1543 break;
1544
1545 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1546 /*
1547 * If we sent a smaller global_seq than the peer has, try
1548 * again with a larger value.
1549 */
1550 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
1551 con->peer_global_seq,
1552 le32_to_cpu(con->in_connect.global_seq));
1553 get_global_seq(con->msgr,
1554 le32_to_cpu(con->in_connect.global_seq));
1555 con_out_kvec_reset(con);
1556 ret = prepare_write_connect(con);
1557 if (ret < 0)
1558 return ret;
1559 prepare_read_connect(con);
1560 break;
1561
1562 case CEPH_MSGR_TAG_READY:
1563 if (req_feat & ~server_feat) {
1564 pr_err("%s%lld %s protocol feature mismatch,"
1565 " my required %llx > server's %llx, need %llx\n",
1566 ENTITY_NAME(con->peer_name),
1567 ceph_pr_addr(&con->peer_addr.in_addr),
1568 req_feat, server_feat, req_feat & ~server_feat);
1569 con->error_msg = "missing required protocol features";
1570 fail_protocol(con);
1571 return -1;
1572 }
1573 clear_bit(CONNECTING, &con->state);
1574 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1575 con->connect_seq++;
1576 con->peer_features = server_feat;
1577 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1578 con->peer_global_seq,
1579 le32_to_cpu(con->in_reply.connect_seq),
1580 con->connect_seq);
1581 WARN_ON(con->connect_seq !=
1582 le32_to_cpu(con->in_reply.connect_seq));
1583
1584 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
1585 set_bit(LOSSYTX, &con->flags);
1586
1587 prepare_read_tag(con);
1588 break;
1589
1590 case CEPH_MSGR_TAG_WAIT:
1591 /*
1592 * If there is a connection race (we are opening
1593 * connections to each other), one of us may just have
1594 * to WAIT. This shouldn't happen if we are the
1595 * client.
1596 */
1597 pr_err("process_connect got WAIT as client\n");
1598 con->error_msg = "protocol error, got WAIT as client";
1599 return -1;
1600
1601 default:
1602 pr_err("connect protocol error, will retry\n");
1603 con->error_msg = "protocol error, garbage tag during connect";
1604 return -1;
1605 }
1606 return 0;
1607 }
1608
1609
1610 /*
1611 * read (part of) an ack
1612 */
1613 static int read_partial_ack(struct ceph_connection *con)
1614 {
1615 int size = sizeof (con->in_temp_ack);
1616 int end = size;
1617
1618 return read_partial(con, end, size, &con->in_temp_ack);
1619 }
1620
1621
1622 /*
1623 * We can finally discard anything that's been acked.
1624 */
1625 static void process_ack(struct ceph_connection *con)
1626 {
1627 struct ceph_msg *m;
1628 u64 ack = le64_to_cpu(con->in_temp_ack);
1629 u64 seq;
1630
1631 while (!list_empty(&con->out_sent)) {
1632 m = list_first_entry(&con->out_sent, struct ceph_msg,
1633 list_head);
1634 seq = le64_to_cpu(m->hdr.seq);
1635 if (seq > ack)
1636 break;
1637 dout("got ack for seq %llu type %d at %p\n", seq,
1638 le16_to_cpu(m->hdr.type), m);
1639 m->ack_stamp = jiffies;
1640 ceph_msg_remove(m);
1641 }
1642 prepare_read_tag(con);
1643 }
1644
1645
1646
1647
1648 static int read_partial_message_section(struct ceph_connection *con,
1649 struct kvec *section,
1650 unsigned int sec_len, u32 *crc)
1651 {
1652 int ret, left;
1653
1654 BUG_ON(!section);
1655
1656 while (section->iov_len < sec_len) {
1657 BUG_ON(section->iov_base == NULL);
1658 left = sec_len - section->iov_len;
1659 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1660 section->iov_len, left);
1661 if (ret <= 0)
1662 return ret;
1663 section->iov_len += ret;
1664 }
1665 if (section->iov_len == sec_len)
1666 *crc = crc32c(0, section->iov_base, section->iov_len);
1667
1668 return 1;
1669 }
1670
1671 static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
1672 struct ceph_msg_header *hdr);
1673
1674
1675 static int read_partial_message_pages(struct ceph_connection *con,
1676 struct page **pages,
1677 unsigned data_len, bool do_datacrc)
1678 {
1679 void *p;
1680 int ret;
1681 int left;
1682
1683 left = min((int)(data_len - con->in_msg_pos.data_pos),
1684 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1685 /* (page) data */
1686 BUG_ON(pages == NULL);
1687 p = kmap(pages[con->in_msg_pos.page]);
1688 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1689 left);
1690 if (ret > 0 && do_datacrc)
1691 con->in_data_crc =
1692 crc32c(con->in_data_crc,
1693 p + con->in_msg_pos.page_pos, ret);
1694 kunmap(pages[con->in_msg_pos.page]);
1695 if (ret <= 0)
1696 return ret;
1697 con->in_msg_pos.data_pos += ret;
1698 con->in_msg_pos.page_pos += ret;
1699 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1700 con->in_msg_pos.page_pos = 0;
1701 con->in_msg_pos.page++;
1702 }
1703
1704 return ret;
1705 }
1706
1707 #ifdef CONFIG_BLOCK
1708 static int read_partial_message_bio(struct ceph_connection *con,
1709 struct bio **bio_iter, int *bio_seg,
1710 unsigned data_len, bool do_datacrc)
1711 {
1712 struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg);
1713 void *p;
1714 int ret, left;
1715
1716 if (IS_ERR(bv))
1717 return PTR_ERR(bv);
1718
1719 left = min((int)(data_len - con->in_msg_pos.data_pos),
1720 (int)(bv->bv_len - con->in_msg_pos.page_pos));
1721
1722 p = kmap(bv->bv_page) + bv->bv_offset;
1723
1724 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1725 left);
1726 if (ret > 0 && do_datacrc)
1727 con->in_data_crc =
1728 crc32c(con->in_data_crc,
1729 p + con->in_msg_pos.page_pos, ret);
1730 kunmap(bv->bv_page);
1731 if (ret <= 0)
1732 return ret;
1733 con->in_msg_pos.data_pos += ret;
1734 con->in_msg_pos.page_pos += ret;
1735 if (con->in_msg_pos.page_pos == bv->bv_len) {
1736 con->in_msg_pos.page_pos = 0;
1737 iter_bio_next(bio_iter, bio_seg);
1738 }
1739
1740 return ret;
1741 }
1742 #endif
1743
1744 /*
1745 * read (part of) a message.
1746 */
1747 static int read_partial_message(struct ceph_connection *con)
1748 {
1749 struct ceph_msg *m = con->in_msg;
1750 int size;
1751 int end;
1752 int ret;
1753 unsigned front_len, middle_len, data_len;
1754 bool do_datacrc = !con->msgr->nocrc;
1755 u64 seq;
1756 u32 crc;
1757
1758 dout("read_partial_message con %p msg %p\n", con, m);
1759
1760 /* header */
1761 size = sizeof (con->in_hdr);
1762 end = size;
1763 ret = read_partial(con, end, size, &con->in_hdr);
1764 if (ret <= 0)
1765 return ret;
1766
1767 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
1768 if (cpu_to_le32(crc) != con->in_hdr.crc) {
1769 pr_err("read_partial_message bad hdr "
1770 " crc %u != expected %u\n",
1771 crc, con->in_hdr.crc);
1772 return -EBADMSG;
1773 }
1774
1775 front_len = le32_to_cpu(con->in_hdr.front_len);
1776 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1777 return -EIO;
1778 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1779 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1780 return -EIO;
1781 data_len = le32_to_cpu(con->in_hdr.data_len);
1782 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1783 return -EIO;
1784
1785 /* verify seq# */
1786 seq = le64_to_cpu(con->in_hdr.seq);
1787 if ((s64)seq - (s64)con->in_seq < 1) {
1788 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
1789 ENTITY_NAME(con->peer_name),
1790 ceph_pr_addr(&con->peer_addr.in_addr),
1791 seq, con->in_seq + 1);
1792 con->in_base_pos = -front_len - middle_len - data_len -
1793 sizeof(m->footer);
1794 con->in_tag = CEPH_MSGR_TAG_READY;
1795 return 0;
1796 } else if ((s64)seq - (s64)con->in_seq > 1) {
1797 pr_err("read_partial_message bad seq %lld expected %lld\n",
1798 seq, con->in_seq + 1);
1799 con->error_msg = "bad message sequence # for incoming message";
1800 return -EBADMSG;
1801 }
1802
1803 /* allocate message? */
1804 if (!con->in_msg) {
1805 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1806 con->in_hdr.front_len, con->in_hdr.data_len);
1807 if (ceph_con_in_msg_alloc(con, &con->in_hdr)) {
1808 /* skip this message */
1809 dout("alloc_msg said skip message\n");
1810 BUG_ON(con->in_msg);
1811 con->in_base_pos = -front_len - middle_len - data_len -
1812 sizeof(m->footer);
1813 con->in_tag = CEPH_MSGR_TAG_READY;
1814 con->in_seq++;
1815 return 0;
1816 }
1817 if (!con->in_msg) {
1818 con->error_msg =
1819 "error allocating memory for incoming message";
1820 return -ENOMEM;
1821 }
1822
1823 BUG_ON(con->in_msg->con != con);
1824 m = con->in_msg;
1825 m->front.iov_len = 0; /* haven't read it yet */
1826 if (m->middle)
1827 m->middle->vec.iov_len = 0;
1828
1829 con->in_msg_pos.page = 0;
1830 if (m->pages)
1831 con->in_msg_pos.page_pos = m->page_alignment;
1832 else
1833 con->in_msg_pos.page_pos = 0;
1834 con->in_msg_pos.data_pos = 0;
1835 }
1836
1837 /* front */
1838 ret = read_partial_message_section(con, &m->front, front_len,
1839 &con->in_front_crc);
1840 if (ret <= 0)
1841 return ret;
1842
1843 /* middle */
1844 if (m->middle) {
1845 ret = read_partial_message_section(con, &m->middle->vec,
1846 middle_len,
1847 &con->in_middle_crc);
1848 if (ret <= 0)
1849 return ret;
1850 }
1851 #ifdef CONFIG_BLOCK
1852 if (m->bio && !m->bio_iter)
1853 init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg);
1854 #endif
1855
1856 /* (page) data */
1857 while (con->in_msg_pos.data_pos < data_len) {
1858 if (m->pages) {
1859 ret = read_partial_message_pages(con, m->pages,
1860 data_len, do_datacrc);
1861 if (ret <= 0)
1862 return ret;
1863 #ifdef CONFIG_BLOCK
1864 } else if (m->bio) {
1865
1866 ret = read_partial_message_bio(con,
1867 &m->bio_iter, &m->bio_seg,
1868 data_len, do_datacrc);
1869 if (ret <= 0)
1870 return ret;
1871 #endif
1872 } else {
1873 BUG_ON(1);
1874 }
1875 }
1876
1877 /* footer */
1878 size = sizeof (m->footer);
1879 end += size;
1880 ret = read_partial(con, end, size, &m->footer);
1881 if (ret <= 0)
1882 return ret;
1883
1884 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1885 m, front_len, m->footer.front_crc, middle_len,
1886 m->footer.middle_crc, data_len, m->footer.data_crc);
1887
1888 /* crc ok? */
1889 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1890 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1891 m, con->in_front_crc, m->footer.front_crc);
1892 return -EBADMSG;
1893 }
1894 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1895 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1896 m, con->in_middle_crc, m->footer.middle_crc);
1897 return -EBADMSG;
1898 }
1899 if (do_datacrc &&
1900 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1901 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1902 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1903 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1904 return -EBADMSG;
1905 }
1906
1907 return 1; /* done! */
1908 }
1909
1910 /*
1911 * Process message. This happens in the worker thread. The callback should
1912 * be careful not to do anything that waits on other incoming messages or it
1913 * may deadlock.
1914 */
1915 static void process_message(struct ceph_connection *con)
1916 {
1917 struct ceph_msg *msg;
1918
1919 BUG_ON(con->in_msg->con != con);
1920 con->in_msg->con = NULL;
1921 msg = con->in_msg;
1922 con->in_msg = NULL;
1923 ceph_con_put(con);
1924
1925 /* if first message, set peer_name */
1926 if (con->peer_name.type == 0)
1927 con->peer_name = msg->hdr.src;
1928
1929 con->in_seq++;
1930 mutex_unlock(&con->mutex);
1931
1932 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1933 msg, le64_to_cpu(msg->hdr.seq),
1934 ENTITY_NAME(msg->hdr.src),
1935 le16_to_cpu(msg->hdr.type),
1936 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1937 le32_to_cpu(msg->hdr.front_len),
1938 le32_to_cpu(msg->hdr.data_len),
1939 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1940 con->ops->dispatch(con, msg);
1941
1942 mutex_lock(&con->mutex);
1943 prepare_read_tag(con);
1944 }
1945
1946
1947 /*
1948 * Write something to the socket. Called in a worker thread when the
1949 * socket appears to be writeable and we have something ready to send.
1950 */
1951 static int try_write(struct ceph_connection *con)
1952 {
1953 int ret = 1;
1954
1955 dout("try_write start %p state %lu nref %d\n", con, con->state,
1956 atomic_read(&con->nref));
1957
1958 more:
1959 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1960
1961 /* open the socket first? */
1962 if (con->sock == NULL) {
1963 clear_bit(NEGOTIATING, &con->state);
1964 set_bit(CONNECTING, &con->state);
1965
1966 con_out_kvec_reset(con);
1967 prepare_write_banner(con);
1968 ret = prepare_write_connect(con);
1969 if (ret < 0)
1970 goto out;
1971 prepare_read_banner(con);
1972
1973 BUG_ON(con->in_msg);
1974 con->in_tag = CEPH_MSGR_TAG_READY;
1975 dout("try_write initiating connect on %p new state %lu\n",
1976 con, con->state);
1977 ret = ceph_tcp_connect(con);
1978 if (ret < 0) {
1979 con->error_msg = "connect error";
1980 goto out;
1981 }
1982 }
1983
1984 more_kvec:
1985 /* kvec data queued? */
1986 if (con->out_skip) {
1987 ret = write_partial_skip(con);
1988 if (ret <= 0)
1989 goto out;
1990 }
1991 if (con->out_kvec_left) {
1992 ret = write_partial_kvec(con);
1993 if (ret <= 0)
1994 goto out;
1995 }
1996
1997 /* msg pages? */
1998 if (con->out_msg) {
1999 if (con->out_msg_done) {
2000 ceph_msg_put(con->out_msg);
2001 con->out_msg = NULL; /* we're done with this one */
2002 goto do_next;
2003 }
2004
2005 ret = write_partial_msg_pages(con);
2006 if (ret == 1)
2007 goto more_kvec; /* we need to send the footer, too! */
2008 if (ret == 0)
2009 goto out;
2010 if (ret < 0) {
2011 dout("try_write write_partial_msg_pages err %d\n",
2012 ret);
2013 goto out;
2014 }
2015 }
2016
2017 do_next:
2018 if (!test_bit(CONNECTING, &con->state)) {
2019 /* is anything else pending? */
2020 if (!list_empty(&con->out_queue)) {
2021 prepare_write_message(con);
2022 goto more;
2023 }
2024 if (con->in_seq > con->in_seq_acked) {
2025 prepare_write_ack(con);
2026 goto more;
2027 }
2028 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) {
2029 prepare_write_keepalive(con);
2030 goto more;
2031 }
2032 }
2033
2034 /* Nothing to do! */
2035 clear_bit(WRITE_PENDING, &con->flags);
2036 dout("try_write nothing else to write.\n");
2037 ret = 0;
2038 out:
2039 dout("try_write done on %p ret %d\n", con, ret);
2040 return ret;
2041 }
2042
2043
2044
2045 /*
2046 * Read what we can from the socket.
2047 */
2048 static int try_read(struct ceph_connection *con)
2049 {
2050 int ret = -1;
2051
2052 if (!con->sock)
2053 return 0;
2054
2055 if (test_bit(STANDBY, &con->state))
2056 return 0;
2057
2058 dout("try_read start on %p\n", con);
2059
2060 more:
2061 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2062 con->in_base_pos);
2063
2064 /*
2065 * process_connect and process_message drop and re-take
2066 * con->mutex. make sure we handle a racing close or reopen.
2067 */
2068 if (test_bit(CLOSED, &con->state) ||
2069 test_bit(OPENING, &con->state)) {
2070 ret = -EAGAIN;
2071 goto out;
2072 }
2073
2074 if (test_bit(CONNECTING, &con->state)) {
2075 if (!test_bit(NEGOTIATING, &con->state)) {
2076 dout("try_read connecting\n");
2077 ret = read_partial_banner(con);
2078 if (ret <= 0)
2079 goto out;
2080 ret = process_banner(con);
2081 if (ret < 0)
2082 goto out;
2083 }
2084 ret = read_partial_connect(con);
2085 if (ret <= 0)
2086 goto out;
2087 ret = process_connect(con);
2088 if (ret < 0)
2089 goto out;
2090 goto more;
2091 }
2092
2093 if (con->in_base_pos < 0) {
2094 /*
2095 * skipping + discarding content.
2096 *
2097 * FIXME: there must be a better way to do this!
2098 */
2099 static char buf[SKIP_BUF_SIZE];
2100 int skip = min((int) sizeof (buf), -con->in_base_pos);
2101
2102 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2103 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2104 if (ret <= 0)
2105 goto out;
2106 con->in_base_pos += ret;
2107 if (con->in_base_pos)
2108 goto more;
2109 }
2110 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2111 /*
2112 * what's next?
2113 */
2114 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2115 if (ret <= 0)
2116 goto out;
2117 dout("try_read got tag %d\n", (int)con->in_tag);
2118 switch (con->in_tag) {
2119 case CEPH_MSGR_TAG_MSG:
2120 prepare_read_message(con);
2121 break;
2122 case CEPH_MSGR_TAG_ACK:
2123 prepare_read_ack(con);
2124 break;
2125 case CEPH_MSGR_TAG_CLOSE:
2126 set_bit(CLOSED, &con->state); /* fixme */
2127 goto out;
2128 default:
2129 goto bad_tag;
2130 }
2131 }
2132 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2133 ret = read_partial_message(con);
2134 if (ret <= 0) {
2135 switch (ret) {
2136 case -EBADMSG:
2137 con->error_msg = "bad crc";
2138 ret = -EIO;
2139 break;
2140 case -EIO:
2141 con->error_msg = "io error";
2142 break;
2143 }
2144 goto out;
2145 }
2146 if (con->in_tag == CEPH_MSGR_TAG_READY)
2147 goto more;
2148 process_message(con);
2149 goto more;
2150 }
2151 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
2152 ret = read_partial_ack(con);
2153 if (ret <= 0)
2154 goto out;
2155 process_ack(con);
2156 goto more;
2157 }
2158
2159 out:
2160 dout("try_read done on %p ret %d\n", con, ret);
2161 return ret;
2162
2163 bad_tag:
2164 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2165 con->error_msg = "protocol error, garbage tag";
2166 ret = -1;
2167 goto out;
2168 }
2169
2170
2171 /*
2172 * Atomically queue work on a connection. Bump @con reference to
2173 * avoid races with connection teardown.
2174 */
2175 static void queue_con(struct ceph_connection *con)
2176 {
2177 if (!con->ops->get(con)) {
2178 dout("queue_con %p ref count 0\n", con);
2179 return;
2180 }
2181
2182 if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) {
2183 dout("queue_con %p - already queued\n", con);
2184 con->ops->put(con);
2185 } else {
2186 dout("queue_con %p\n", con);
2187 }
2188 }
2189
2190 /*
2191 * Do some work on a connection. Drop a connection ref when we're done.
2192 */
2193 static void con_work(struct work_struct *work)
2194 {
2195 struct ceph_connection *con = container_of(work, struct ceph_connection,
2196 work.work);
2197 int ret;
2198
2199 mutex_lock(&con->mutex);
2200 restart:
2201 if (test_and_clear_bit(BACKOFF, &con->flags)) {
2202 dout("con_work %p backing off\n", con);
2203 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2204 round_jiffies_relative(con->delay))) {
2205 dout("con_work %p backoff %lu\n", con, con->delay);
2206 mutex_unlock(&con->mutex);
2207 return;
2208 } else {
2209 con->ops->put(con);
2210 dout("con_work %p FAILED to back off %lu\n", con,
2211 con->delay);
2212 }
2213 }
2214
2215 if (test_bit(STANDBY, &con->state)) {
2216 dout("con_work %p STANDBY\n", con);
2217 goto done;
2218 }
2219 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
2220 dout("con_work CLOSED\n");
2221 con_close_socket(con);
2222 goto done;
2223 }
2224 if (test_and_clear_bit(OPENING, &con->state)) {
2225 /* reopen w/ new peer */
2226 dout("con_work OPENING\n");
2227 con_close_socket(con);
2228 }
2229
2230 if (test_and_clear_bit(SOCK_CLOSED, &con->flags))
2231 goto fault;
2232
2233 ret = try_read(con);
2234 if (ret == -EAGAIN)
2235 goto restart;
2236 if (ret < 0)
2237 goto fault;
2238
2239 ret = try_write(con);
2240 if (ret == -EAGAIN)
2241 goto restart;
2242 if (ret < 0)
2243 goto fault;
2244
2245 done:
2246 mutex_unlock(&con->mutex);
2247 done_unlocked:
2248 con->ops->put(con);
2249 return;
2250
2251 fault:
2252 mutex_unlock(&con->mutex);
2253 ceph_fault(con); /* error/fault path */
2254 goto done_unlocked;
2255 }
2256
2257
2258 /*
2259 * Generic error/fault handler. A retry mechanism is used with
2260 * exponential backoff
2261 */
2262 static void ceph_fault(struct ceph_connection *con)
2263 {
2264 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
2265 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
2266 dout("fault %p state %lu to peer %s\n",
2267 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
2268
2269 if (test_bit(LOSSYTX, &con->flags)) {
2270 dout("fault on LOSSYTX channel\n");
2271 goto out;
2272 }
2273
2274 mutex_lock(&con->mutex);
2275 if (test_bit(CLOSED, &con->state))
2276 goto out_unlock;
2277
2278 con_close_socket(con);
2279
2280 if (con->in_msg) {
2281 BUG_ON(con->in_msg->con != con);
2282 con->in_msg->con = NULL;
2283 ceph_msg_put(con->in_msg);
2284 con->in_msg = NULL;
2285 ceph_con_put(con);
2286 }
2287
2288 /* Requeue anything that hasn't been acked */
2289 list_splice_init(&con->out_sent, &con->out_queue);
2290
2291 /* If there are no messages queued or keepalive pending, place
2292 * the connection in a STANDBY state */
2293 if (list_empty(&con->out_queue) &&
2294 !test_bit(KEEPALIVE_PENDING, &con->flags)) {
2295 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
2296 clear_bit(WRITE_PENDING, &con->flags);
2297 set_bit(STANDBY, &con->state);
2298 } else {
2299 /* retry after a delay. */
2300 if (con->delay == 0)
2301 con->delay = BASE_DELAY_INTERVAL;
2302 else if (con->delay < MAX_DELAY_INTERVAL)
2303 con->delay *= 2;
2304 con->ops->get(con);
2305 if (queue_delayed_work(ceph_msgr_wq, &con->work,
2306 round_jiffies_relative(con->delay))) {
2307 dout("fault queued %p delay %lu\n", con, con->delay);
2308 } else {
2309 con->ops->put(con);
2310 dout("fault failed to queue %p delay %lu, backoff\n",
2311 con, con->delay);
2312 /*
2313 * In many cases we see a socket state change
2314 * while con_work is running and end up
2315 * queuing (non-delayed) work, such that we
2316 * can't backoff with a delay. Set a flag so
2317 * that when con_work restarts we schedule the
2318 * delay then.
2319 */
2320 set_bit(BACKOFF, &con->flags);
2321 }
2322 }
2323
2324 out_unlock:
2325 mutex_unlock(&con->mutex);
2326 out:
2327 /*
2328 * in case we faulted due to authentication, invalidate our
2329 * current tickets so that we can get new ones.
2330 */
2331 if (con->auth_retry && con->ops->invalidate_authorizer) {
2332 dout("calling invalidate_authorizer()\n");
2333 con->ops->invalidate_authorizer(con);
2334 }
2335
2336 if (con->ops->fault)
2337 con->ops->fault(con);
2338 }
2339
2340
2341
2342 /*
2343 * initialize a new messenger instance
2344 */
2345 void ceph_messenger_init(struct ceph_messenger *msgr,
2346 struct ceph_entity_addr *myaddr,
2347 u32 supported_features,
2348 u32 required_features,
2349 bool nocrc)
2350 {
2351 msgr->supported_features = supported_features;
2352 msgr->required_features = required_features;
2353
2354 spin_lock_init(&msgr->global_seq_lock);
2355
2356 if (myaddr)
2357 msgr->inst.addr = *myaddr;
2358
2359 /* select a random nonce */
2360 msgr->inst.addr.type = 0;
2361 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
2362 encode_my_addr(msgr);
2363 msgr->nocrc = nocrc;
2364
2365 dout("%s %p\n", __func__, msgr);
2366 }
2367 EXPORT_SYMBOL(ceph_messenger_init);
2368
2369 static void clear_standby(struct ceph_connection *con)
2370 {
2371 /* come back from STANDBY? */
2372 if (test_and_clear_bit(STANDBY, &con->state)) {
2373 mutex_lock(&con->mutex);
2374 dout("clear_standby %p and ++connect_seq\n", con);
2375 con->connect_seq++;
2376 WARN_ON(test_bit(WRITE_PENDING, &con->flags));
2377 WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags));
2378 mutex_unlock(&con->mutex);
2379 }
2380 }
2381
2382 /*
2383 * Queue up an outgoing message on the given connection.
2384 */
2385 void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
2386 {
2387 if (test_bit(CLOSED, &con->state)) {
2388 dout("con_send %p closed, dropping %p\n", con, msg);
2389 ceph_msg_put(msg);
2390 return;
2391 }
2392
2393 /* set src+dst */
2394 msg->hdr.src = con->msgr->inst.name;
2395
2396 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
2397
2398 msg->needs_out_seq = true;
2399
2400 /* queue */
2401 mutex_lock(&con->mutex);
2402
2403 BUG_ON(msg->con != NULL);
2404 msg->con = ceph_con_get(con);
2405 BUG_ON(msg->con == NULL);
2406
2407 BUG_ON(!list_empty(&msg->list_head));
2408 list_add_tail(&msg->list_head, &con->out_queue);
2409 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2410 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2411 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2412 le32_to_cpu(msg->hdr.front_len),
2413 le32_to_cpu(msg->hdr.middle_len),
2414 le32_to_cpu(msg->hdr.data_len));
2415 mutex_unlock(&con->mutex);
2416
2417 /* if there wasn't anything waiting to send before, queue
2418 * new work */
2419 clear_standby(con);
2420 if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
2421 queue_con(con);
2422 }
2423 EXPORT_SYMBOL(ceph_con_send);
2424
2425 /*
2426 * Revoke a message that was previously queued for send
2427 */
2428 void ceph_msg_revoke(struct ceph_msg *msg)
2429 {
2430 struct ceph_connection *con = msg->con;
2431
2432 if (!con)
2433 return; /* Message not in our possession */
2434
2435 mutex_lock(&con->mutex);
2436 if (!list_empty(&msg->list_head)) {
2437 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
2438 list_del_init(&msg->list_head);
2439 BUG_ON(msg->con == NULL);
2440 ceph_con_put(msg->con);
2441 msg->con = NULL;
2442 msg->hdr.seq = 0;
2443
2444 ceph_msg_put(msg);
2445 }
2446 if (con->out_msg == msg) {
2447 dout("%s %p msg %p - was sending\n", __func__, con, msg);
2448 con->out_msg = NULL;
2449 if (con->out_kvec_is_msg) {
2450 con->out_skip = con->out_kvec_bytes;
2451 con->out_kvec_is_msg = false;
2452 }
2453 msg->hdr.seq = 0;
2454
2455 ceph_msg_put(msg);
2456 }
2457 mutex_unlock(&con->mutex);
2458 }
2459
2460 /*
2461 * Revoke a message that we may be reading data into
2462 */
2463 void ceph_msg_revoke_incoming(struct ceph_msg *msg)
2464 {
2465 struct ceph_connection *con;
2466
2467 BUG_ON(msg == NULL);
2468 if (!msg->con) {
2469 dout("%s msg %p null con\n", __func__, msg);
2470
2471 return; /* Message not in our possession */
2472 }
2473
2474 con = msg->con;
2475 mutex_lock(&con->mutex);
2476 if (con->in_msg == msg) {
2477 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2478 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
2479 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2480
2481 /* skip rest of message */
2482 dout("%s %p msg %p revoked\n", __func__, con, msg);
2483 con->in_base_pos = con->in_base_pos -
2484 sizeof(struct ceph_msg_header) -
2485 front_len -
2486 middle_len -
2487 data_len -
2488 sizeof(struct ceph_msg_footer);
2489 ceph_msg_put(con->in_msg);
2490 con->in_msg = NULL;
2491 con->in_tag = CEPH_MSGR_TAG_READY;
2492 con->in_seq++;
2493 } else {
2494 dout("%s %p in_msg %p msg %p no-op\n",
2495 __func__, con, con->in_msg, msg);
2496 }
2497 mutex_unlock(&con->mutex);
2498 }
2499
2500 /*
2501 * Queue a keepalive byte to ensure the tcp connection is alive.
2502 */
2503 void ceph_con_keepalive(struct ceph_connection *con)
2504 {
2505 dout("con_keepalive %p\n", con);
2506 clear_standby(con);
2507 if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 &&
2508 test_and_set_bit(WRITE_PENDING, &con->flags) == 0)
2509 queue_con(con);
2510 }
2511 EXPORT_SYMBOL(ceph_con_keepalive);
2512
2513
2514 /*
2515 * construct a new message with given type, size
2516 * the new msg has a ref count of 1.
2517 */
2518 struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
2519 bool can_fail)
2520 {
2521 struct ceph_msg *m;
2522
2523 m = kmalloc(sizeof(*m), flags);
2524 if (m == NULL)
2525 goto out;
2526 kref_init(&m->kref);
2527
2528 m->con = NULL;
2529 INIT_LIST_HEAD(&m->list_head);
2530
2531 m->hdr.tid = 0;
2532 m->hdr.type = cpu_to_le16(type);
2533 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2534 m->hdr.version = 0;
2535 m->hdr.front_len = cpu_to_le32(front_len);
2536 m->hdr.middle_len = 0;
2537 m->hdr.data_len = 0;
2538 m->hdr.data_off = 0;
2539 m->hdr.reserved = 0;
2540 m->footer.front_crc = 0;
2541 m->footer.middle_crc = 0;
2542 m->footer.data_crc = 0;
2543 m->footer.flags = 0;
2544 m->front_max = front_len;
2545 m->front_is_vmalloc = false;
2546 m->more_to_follow = false;
2547 m->ack_stamp = 0;
2548 m->pool = NULL;
2549
2550 /* middle */
2551 m->middle = NULL;
2552
2553 /* data */
2554 m->nr_pages = 0;
2555 m->page_alignment = 0;
2556 m->pages = NULL;
2557 m->pagelist = NULL;
2558 m->bio = NULL;
2559 m->bio_iter = NULL;
2560 m->bio_seg = 0;
2561 m->trail = NULL;
2562
2563 /* front */
2564 if (front_len) {
2565 if (front_len > PAGE_CACHE_SIZE) {
2566 m->front.iov_base = __vmalloc(front_len, flags,
2567 PAGE_KERNEL);
2568 m->front_is_vmalloc = true;
2569 } else {
2570 m->front.iov_base = kmalloc(front_len, flags);
2571 }
2572 if (m->front.iov_base == NULL) {
2573 dout("ceph_msg_new can't allocate %d bytes\n",
2574 front_len);
2575 goto out2;
2576 }
2577 } else {
2578 m->front.iov_base = NULL;
2579 }
2580 m->front.iov_len = front_len;
2581
2582 dout("ceph_msg_new %p front %d\n", m, front_len);
2583 return m;
2584
2585 out2:
2586 ceph_msg_put(m);
2587 out:
2588 if (!can_fail) {
2589 pr_err("msg_new can't create type %d front %d\n", type,
2590 front_len);
2591 WARN_ON(1);
2592 } else {
2593 dout("msg_new can't create type %d front %d\n", type,
2594 front_len);
2595 }
2596 return NULL;
2597 }
2598 EXPORT_SYMBOL(ceph_msg_new);
2599
2600 /*
2601 * Allocate "middle" portion of a message, if it is needed and wasn't
2602 * allocated by alloc_msg. This allows us to read a small fixed-size
2603 * per-type header in the front and then gracefully fail (i.e.,
2604 * propagate the error to the caller based on info in the front) when
2605 * the middle is too large.
2606 */
2607 static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
2608 {
2609 int type = le16_to_cpu(msg->hdr.type);
2610 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2611
2612 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2613 ceph_msg_type_name(type), middle_len);
2614 BUG_ON(!middle_len);
2615 BUG_ON(msg->middle);
2616
2617 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
2618 if (!msg->middle)
2619 return -ENOMEM;
2620 return 0;
2621 }
2622
2623 /*
2624 * Allocate a message for receiving an incoming message on a
2625 * connection, and save the result in con->in_msg. Uses the
2626 * connection's private alloc_msg op if available.
2627 *
2628 * Returns true if the message should be skipped, false otherwise.
2629 * If true is returned (skip message), con->in_msg will be NULL.
2630 * If false is returned, con->in_msg will contain a pointer to the
2631 * newly-allocated message, or NULL in case of memory exhaustion.
2632 */
2633 static bool ceph_con_in_msg_alloc(struct ceph_connection *con,
2634 struct ceph_msg_header *hdr)
2635 {
2636 int type = le16_to_cpu(hdr->type);
2637 int front_len = le32_to_cpu(hdr->front_len);
2638 int middle_len = le32_to_cpu(hdr->middle_len);
2639 int ret;
2640
2641 BUG_ON(con->in_msg != NULL);
2642
2643 if (con->ops->alloc_msg) {
2644 int skip = 0;
2645
2646 mutex_unlock(&con->mutex);
2647 con->in_msg = con->ops->alloc_msg(con, hdr, &skip);
2648 mutex_lock(&con->mutex);
2649 if (con->in_msg) {
2650 con->in_msg->con = ceph_con_get(con);
2651 BUG_ON(con->in_msg->con == NULL);
2652 }
2653 if (skip)
2654 con->in_msg = NULL;
2655
2656 if (!con->in_msg)
2657 return skip != 0;
2658 }
2659 if (!con->in_msg) {
2660 con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
2661 if (!con->in_msg) {
2662 pr_err("unable to allocate msg type %d len %d\n",
2663 type, front_len);
2664 return false;
2665 }
2666 con->in_msg->con = ceph_con_get(con);
2667 BUG_ON(con->in_msg->con == NULL);
2668 con->in_msg->page_alignment = le16_to_cpu(hdr->data_off);
2669 }
2670 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
2671
2672 if (middle_len && !con->in_msg->middle) {
2673 ret = ceph_alloc_middle(con, con->in_msg);
2674 if (ret < 0) {
2675 ceph_msg_put(con->in_msg);
2676 con->in_msg = NULL;
2677 }
2678 }
2679
2680 return false;
2681 }
2682
2683
2684 /*
2685 * Free a generically kmalloc'd message.
2686 */
2687 void ceph_msg_kfree(struct ceph_msg *m)
2688 {
2689 dout("msg_kfree %p\n", m);
2690 if (m->front_is_vmalloc)
2691 vfree(m->front.iov_base);
2692 else
2693 kfree(m->front.iov_base);
2694 kfree(m);
2695 }
2696
2697 /*
2698 * Drop a msg ref. Destroy as needed.
2699 */
2700 void ceph_msg_last_put(struct kref *kref)
2701 {
2702 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
2703
2704 dout("ceph_msg_put last one on %p\n", m);
2705 WARN_ON(!list_empty(&m->list_head));
2706
2707 /* drop middle, data, if any */
2708 if (m->middle) {
2709 ceph_buffer_put(m->middle);
2710 m->middle = NULL;
2711 }
2712 m->nr_pages = 0;
2713 m->pages = NULL;
2714
2715 if (m->pagelist) {
2716 ceph_pagelist_release(m->pagelist);
2717 kfree(m->pagelist);
2718 m->pagelist = NULL;
2719 }
2720
2721 m->trail = NULL;
2722
2723 if (m->pool)
2724 ceph_msgpool_put(m->pool, m);
2725 else
2726 ceph_msg_kfree(m);
2727 }
2728 EXPORT_SYMBOL(ceph_msg_last_put);
2729
2730 void ceph_msg_dump(struct ceph_msg *msg)
2731 {
2732 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2733 msg->front_max, msg->nr_pages);
2734 print_hex_dump(KERN_DEBUG, "header: ",
2735 DUMP_PREFIX_OFFSET, 16, 1,
2736 &msg->hdr, sizeof(msg->hdr), true);
2737 print_hex_dump(KERN_DEBUG, " front: ",
2738 DUMP_PREFIX_OFFSET, 16, 1,
2739 msg->front.iov_base, msg->front.iov_len, true);
2740 if (msg->middle)
2741 print_hex_dump(KERN_DEBUG, "middle: ",
2742 DUMP_PREFIX_OFFSET, 16, 1,
2743 msg->middle->vec.iov_base,
2744 msg->middle->vec.iov_len, true);
2745 print_hex_dump(KERN_DEBUG, "footer: ",
2746 DUMP_PREFIX_OFFSET, 16, 1,
2747 &msg->footer, sizeof(msg->footer), true);
2748 }
2749 EXPORT_SYMBOL(ceph_msg_dump);
This page took 0.107099 seconds and 5 git commands to generate.