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