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