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