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