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