ANDROID: squashfs: Fix signed division issue
[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/nsproxy.h>
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/socket.h>
13 #include <linux/string.h>
14 #ifdef  CONFIG_BLOCK
15 #include <linux/bio.h>
16 #endif  /* CONFIG_BLOCK */
17 #include <linux/dns_resolver.h>
18 #include <net/tcp.h>
19
20 #include <linux/ceph/ceph_features.h>
21 #include <linux/ceph/libceph.h>
22 #include <linux/ceph/messenger.h>
23 #include <linux/ceph/decode.h>
24 #include <linux/ceph/pagelist.h>
25 #include <linux/export.h>
26
27 #define list_entry_next(pos, member)                                    \
28         list_entry(pos->member.next, typeof(*pos), member)
29
30 /*
31  * Ceph uses the messenger to exchange ceph_msg messages with other
32  * hosts in the system.  The messenger provides ordered and reliable
33  * delivery.  We tolerate TCP disconnects by reconnecting (with
34  * exponential backoff) in the case of a fault (disconnection, bad
35  * crc, protocol error).  Acks allow sent messages to be discarded by
36  * the sender.
37  */
38
39 /*
40  * We track the state of the socket on a given connection using
41  * values defined below.  The transition to a new socket state is
42  * handled by a function which verifies we aren't coming from an
43  * unexpected state.
44  *
45  *      --------
46  *      | NEW* |  transient initial state
47  *      --------
48  *          | con_sock_state_init()
49  *          v
50  *      ----------
51  *      | CLOSED |  initialized, but no socket (and no
52  *      ----------  TCP connection)
53  *       ^      \
54  *       |       \ con_sock_state_connecting()
55  *       |        ----------------------
56  *       |                              \
57  *       + con_sock_state_closed()       \
58  *       |+---------------------------    \
59  *       | \                          \    \
60  *       |  -----------                \    \
61  *       |  | CLOSING |  socket event;  \    \
62  *       |  -----------  await close     \    \
63  *       |       ^                        \   |
64  *       |       |                         \  |
65  *       |       + con_sock_state_closing() \ |
66  *       |      / \                         | |
67  *       |     /   ---------------          | |
68  *       |    /                   \         v v
69  *       |   /                    --------------
70  *       |  /    -----------------| CONNECTING |  socket created, TCP
71  *       |  |   /                 --------------  connect initiated
72  *       |  |   | con_sock_state_connected()
73  *       |  |   v
74  *      -------------
75  *      | CONNECTED |  TCP connection established
76  *      -------------
77  *
78  * State values for ceph_connection->sock_state; NEW is assumed to be 0.
79  */
80
81 #define CON_SOCK_STATE_NEW              0       /* -> CLOSED */
82 #define CON_SOCK_STATE_CLOSED           1       /* -> CONNECTING */
83 #define CON_SOCK_STATE_CONNECTING       2       /* -> CONNECTED or -> CLOSING */
84 #define CON_SOCK_STATE_CONNECTED        3       /* -> CLOSING or -> CLOSED */
85 #define CON_SOCK_STATE_CLOSING          4       /* -> CLOSED */
86
87 /*
88  * connection states
89  */
90 #define CON_STATE_CLOSED        1  /* -> PREOPEN */
91 #define CON_STATE_PREOPEN       2  /* -> CONNECTING, CLOSED */
92 #define CON_STATE_CONNECTING    3  /* -> NEGOTIATING, CLOSED */
93 #define CON_STATE_NEGOTIATING   4  /* -> OPEN, CLOSED */
94 #define CON_STATE_OPEN          5  /* -> STANDBY, CLOSED */
95 #define CON_STATE_STANDBY       6  /* -> PREOPEN, CLOSED */
96
97 /*
98  * ceph_connection flag bits
99  */
100 #define CON_FLAG_LOSSYTX           0  /* we can close channel or drop
101                                        * messages on errors */
102 #define CON_FLAG_KEEPALIVE_PENDING 1  /* we need to send a keepalive */
103 #define CON_FLAG_WRITE_PENDING     2  /* we have data ready to send */
104 #define CON_FLAG_SOCK_CLOSED       3  /* socket state changed to closed */
105 #define CON_FLAG_BACKOFF           4  /* need to retry queuing delayed work */
106
107 static bool con_flag_valid(unsigned long con_flag)
108 {
109         switch (con_flag) {
110         case CON_FLAG_LOSSYTX:
111         case CON_FLAG_KEEPALIVE_PENDING:
112         case CON_FLAG_WRITE_PENDING:
113         case CON_FLAG_SOCK_CLOSED:
114         case CON_FLAG_BACKOFF:
115                 return true;
116         default:
117                 return false;
118         }
119 }
120
121 static void con_flag_clear(struct ceph_connection *con, unsigned long con_flag)
122 {
123         BUG_ON(!con_flag_valid(con_flag));
124
125         clear_bit(con_flag, &con->flags);
126 }
127
128 static void con_flag_set(struct ceph_connection *con, unsigned long con_flag)
129 {
130         BUG_ON(!con_flag_valid(con_flag));
131
132         set_bit(con_flag, &con->flags);
133 }
134
135 static bool con_flag_test(struct ceph_connection *con, unsigned long con_flag)
136 {
137         BUG_ON(!con_flag_valid(con_flag));
138
139         return test_bit(con_flag, &con->flags);
140 }
141
142 static bool con_flag_test_and_clear(struct ceph_connection *con,
143                                         unsigned long con_flag)
144 {
145         BUG_ON(!con_flag_valid(con_flag));
146
147         return test_and_clear_bit(con_flag, &con->flags);
148 }
149
150 static bool con_flag_test_and_set(struct ceph_connection *con,
151                                         unsigned long con_flag)
152 {
153         BUG_ON(!con_flag_valid(con_flag));
154
155         return test_and_set_bit(con_flag, &con->flags);
156 }
157
158 /* Slab caches for frequently-allocated structures */
159
160 static struct kmem_cache        *ceph_msg_cache;
161 static struct kmem_cache        *ceph_msg_data_cache;
162
163 /* static tag bytes (protocol control messages) */
164 static char tag_msg = CEPH_MSGR_TAG_MSG;
165 static char tag_ack = CEPH_MSGR_TAG_ACK;
166 static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
167 static char tag_keepalive2 = CEPH_MSGR_TAG_KEEPALIVE2;
168
169 #ifdef CONFIG_LOCKDEP
170 static struct lock_class_key socket_class;
171 #endif
172
173 /*
174  * When skipping (ignoring) a block of input we read it into a "skip
175  * buffer," which is this many bytes in size.
176  */
177 #define SKIP_BUF_SIZE   1024
178
179 static void queue_con(struct ceph_connection *con);
180 static void cancel_con(struct ceph_connection *con);
181 static void ceph_con_workfn(struct work_struct *);
182 static void con_fault(struct ceph_connection *con);
183
184 /*
185  * Nicely render a sockaddr as a string.  An array of formatted
186  * strings is used, to approximate reentrancy.
187  */
188 #define ADDR_STR_COUNT_LOG      5       /* log2(# address strings in array) */
189 #define ADDR_STR_COUNT          (1 << ADDR_STR_COUNT_LOG)
190 #define ADDR_STR_COUNT_MASK     (ADDR_STR_COUNT - 1)
191 #define MAX_ADDR_STR_LEN        64      /* 54 is enough */
192
193 static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
194 static atomic_t addr_str_seq = ATOMIC_INIT(0);
195
196 static struct page *zero_page;          /* used in certain error cases */
197
198 const char *ceph_pr_addr(const struct sockaddr_storage *ss)
199 {
200         int i;
201         char *s;
202         struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
203         struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
204
205         i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
206         s = addr_str[i];
207
208         switch (ss->ss_family) {
209         case AF_INET:
210                 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
211                          ntohs(in4->sin_port));
212                 break;
213
214         case AF_INET6:
215                 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
216                          ntohs(in6->sin6_port));
217                 break;
218
219         default:
220                 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
221                          ss->ss_family);
222         }
223
224         return s;
225 }
226 EXPORT_SYMBOL(ceph_pr_addr);
227
228 static void encode_my_addr(struct ceph_messenger *msgr)
229 {
230         memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
231         ceph_encode_addr(&msgr->my_enc_addr);
232 }
233
234 /*
235  * work queue for all reading and writing to/from the socket.
236  */
237 static struct workqueue_struct *ceph_msgr_wq;
238
239 static int ceph_msgr_slab_init(void)
240 {
241         BUG_ON(ceph_msg_cache);
242         ceph_msg_cache = kmem_cache_create("ceph_msg",
243                                         sizeof (struct ceph_msg),
244                                         __alignof__(struct ceph_msg), 0, NULL);
245
246         if (!ceph_msg_cache)
247                 return -ENOMEM;
248
249         BUG_ON(ceph_msg_data_cache);
250         ceph_msg_data_cache = kmem_cache_create("ceph_msg_data",
251                                         sizeof (struct ceph_msg_data),
252                                         __alignof__(struct ceph_msg_data),
253                                         0, NULL);
254         if (ceph_msg_data_cache)
255                 return 0;
256
257         kmem_cache_destroy(ceph_msg_cache);
258         ceph_msg_cache = NULL;
259
260         return -ENOMEM;
261 }
262
263 static void ceph_msgr_slab_exit(void)
264 {
265         BUG_ON(!ceph_msg_data_cache);
266         kmem_cache_destroy(ceph_msg_data_cache);
267         ceph_msg_data_cache = NULL;
268
269         BUG_ON(!ceph_msg_cache);
270         kmem_cache_destroy(ceph_msg_cache);
271         ceph_msg_cache = NULL;
272 }
273
274 static void _ceph_msgr_exit(void)
275 {
276         if (ceph_msgr_wq) {
277                 destroy_workqueue(ceph_msgr_wq);
278                 ceph_msgr_wq = NULL;
279         }
280
281         BUG_ON(zero_page == NULL);
282         page_cache_release(zero_page);
283         zero_page = NULL;
284
285         ceph_msgr_slab_exit();
286 }
287
288 int ceph_msgr_init(void)
289 {
290         if (ceph_msgr_slab_init())
291                 return -ENOMEM;
292
293         BUG_ON(zero_page != NULL);
294         zero_page = ZERO_PAGE(0);
295         page_cache_get(zero_page);
296
297         /*
298          * The number of active work items is limited by the number of
299          * connections, so leave @max_active at default.
300          */
301         ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_MEM_RECLAIM, 0);
302         if (ceph_msgr_wq)
303                 return 0;
304
305         pr_err("msgr_init failed to create workqueue\n");
306         _ceph_msgr_exit();
307
308         return -ENOMEM;
309 }
310 EXPORT_SYMBOL(ceph_msgr_init);
311
312 void ceph_msgr_exit(void)
313 {
314         BUG_ON(ceph_msgr_wq == NULL);
315
316         _ceph_msgr_exit();
317 }
318 EXPORT_SYMBOL(ceph_msgr_exit);
319
320 void ceph_msgr_flush(void)
321 {
322         flush_workqueue(ceph_msgr_wq);
323 }
324 EXPORT_SYMBOL(ceph_msgr_flush);
325
326 /* Connection socket state transition functions */
327
328 static void con_sock_state_init(struct ceph_connection *con)
329 {
330         int old_state;
331
332         old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
333         if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
334                 printk("%s: unexpected old state %d\n", __func__, old_state);
335         dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
336              CON_SOCK_STATE_CLOSED);
337 }
338
339 static void con_sock_state_connecting(struct ceph_connection *con)
340 {
341         int old_state;
342
343         old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
344         if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
345                 printk("%s: unexpected old state %d\n", __func__, old_state);
346         dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
347              CON_SOCK_STATE_CONNECTING);
348 }
349
350 static void con_sock_state_connected(struct ceph_connection *con)
351 {
352         int old_state;
353
354         old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
355         if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
356                 printk("%s: unexpected old state %d\n", __func__, old_state);
357         dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
358              CON_SOCK_STATE_CONNECTED);
359 }
360
361 static void con_sock_state_closing(struct ceph_connection *con)
362 {
363         int old_state;
364
365         old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
366         if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
367                         old_state != CON_SOCK_STATE_CONNECTED &&
368                         old_state != CON_SOCK_STATE_CLOSING))
369                 printk("%s: unexpected old state %d\n", __func__, old_state);
370         dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
371              CON_SOCK_STATE_CLOSING);
372 }
373
374 static void con_sock_state_closed(struct ceph_connection *con)
375 {
376         int old_state;
377
378         old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
379         if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
380                     old_state != CON_SOCK_STATE_CLOSING &&
381                     old_state != CON_SOCK_STATE_CONNECTING &&
382                     old_state != CON_SOCK_STATE_CLOSED))
383                 printk("%s: unexpected old state %d\n", __func__, old_state);
384         dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
385              CON_SOCK_STATE_CLOSED);
386 }
387
388 /*
389  * socket callback functions
390  */
391
392 /* data available on socket, or listen socket received a connect */
393 static void ceph_sock_data_ready(struct sock *sk)
394 {
395         struct ceph_connection *con = sk->sk_user_data;
396         if (atomic_read(&con->msgr->stopping)) {
397                 return;
398         }
399
400         if (sk->sk_state != TCP_CLOSE_WAIT) {
401                 dout("%s on %p state = %lu, queueing work\n", __func__,
402                      con, con->state);
403                 queue_con(con);
404         }
405 }
406
407 /* socket has buffer space for writing */
408 static void ceph_sock_write_space(struct sock *sk)
409 {
410         struct ceph_connection *con = sk->sk_user_data;
411
412         /* only queue to workqueue if there is data we want to write,
413          * and there is sufficient space in the socket buffer to accept
414          * more data.  clear SOCK_NOSPACE so that ceph_sock_write_space()
415          * doesn't get called again until try_write() fills the socket
416          * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
417          * and net/core/stream.c:sk_stream_write_space().
418          */
419         if (con_flag_test(con, CON_FLAG_WRITE_PENDING)) {
420                 if (sk_stream_is_writeable(sk)) {
421                         dout("%s %p queueing write work\n", __func__, con);
422                         clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
423                         queue_con(con);
424                 }
425         } else {
426                 dout("%s %p nothing to write\n", __func__, con);
427         }
428 }
429
430 /* socket's state has changed */
431 static void ceph_sock_state_change(struct sock *sk)
432 {
433         struct ceph_connection *con = sk->sk_user_data;
434
435         dout("%s %p state = %lu sk_state = %u\n", __func__,
436              con, con->state, sk->sk_state);
437
438         switch (sk->sk_state) {
439         case TCP_CLOSE:
440                 dout("%s TCP_CLOSE\n", __func__);
441         case TCP_CLOSE_WAIT:
442                 dout("%s TCP_CLOSE_WAIT\n", __func__);
443                 con_sock_state_closing(con);
444                 con_flag_set(con, CON_FLAG_SOCK_CLOSED);
445                 queue_con(con);
446                 break;
447         case TCP_ESTABLISHED:
448                 dout("%s TCP_ESTABLISHED\n", __func__);
449                 con_sock_state_connected(con);
450                 queue_con(con);
451                 break;
452         default:        /* Everything else is uninteresting */
453                 break;
454         }
455 }
456
457 /*
458  * set up socket callbacks
459  */
460 static void set_sock_callbacks(struct socket *sock,
461                                struct ceph_connection *con)
462 {
463         struct sock *sk = sock->sk;
464         sk->sk_user_data = con;
465         sk->sk_data_ready = ceph_sock_data_ready;
466         sk->sk_write_space = ceph_sock_write_space;
467         sk->sk_state_change = ceph_sock_state_change;
468 }
469
470
471 /*
472  * socket helpers
473  */
474
475 /*
476  * initiate connection to a remote socket.
477  */
478 static int ceph_tcp_connect(struct ceph_connection *con)
479 {
480         struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
481         struct socket *sock;
482         unsigned int noio_flag;
483         int ret;
484
485         BUG_ON(con->sock);
486
487         /* sock_create_kern() allocates with GFP_KERNEL */
488         noio_flag = memalloc_noio_save();
489         ret = sock_create_kern(read_pnet(&con->msgr->net), paddr->ss_family,
490                                SOCK_STREAM, IPPROTO_TCP, &sock);
491         memalloc_noio_restore(noio_flag);
492         if (ret)
493                 return ret;
494         sock->sk->sk_allocation = GFP_NOFS;
495
496 #ifdef CONFIG_LOCKDEP
497         lockdep_set_class(&sock->sk->sk_lock, &socket_class);
498 #endif
499
500         set_sock_callbacks(sock, con);
501
502         dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
503
504         con_sock_state_connecting(con);
505         ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
506                                  O_NONBLOCK);
507         if (ret == -EINPROGRESS) {
508                 dout("connect %s EINPROGRESS sk_state = %u\n",
509                      ceph_pr_addr(&con->peer_addr.in_addr),
510                      sock->sk->sk_state);
511         } else if (ret < 0) {
512                 pr_err("connect %s error %d\n",
513                        ceph_pr_addr(&con->peer_addr.in_addr), ret);
514                 sock_release(sock);
515                 return ret;
516         }
517
518         if (ceph_test_opt(from_msgr(con->msgr), TCP_NODELAY)) {
519                 int optval = 1;
520
521                 ret = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
522                                         (char *)&optval, sizeof(optval));
523                 if (ret)
524                         pr_err("kernel_setsockopt(TCP_NODELAY) failed: %d",
525                                ret);
526         }
527
528         con->sock = sock;
529         return 0;
530 }
531
532 static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
533 {
534         struct kvec iov = {buf, len};
535         struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
536         int r;
537
538         r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
539         if (r == -EAGAIN)
540                 r = 0;
541         return r;
542 }
543
544 static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
545                      int page_offset, size_t length)
546 {
547         void *kaddr;
548         int ret;
549
550         BUG_ON(page_offset + length > PAGE_SIZE);
551
552         kaddr = kmap(page);
553         BUG_ON(!kaddr);
554         ret = ceph_tcp_recvmsg(sock, kaddr + page_offset, length);
555         kunmap(page);
556
557         return ret;
558 }
559
560 /*
561  * write something.  @more is true if caller will be sending more data
562  * shortly.
563  */
564 static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
565                      size_t kvlen, size_t len, int more)
566 {
567         struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
568         int r;
569
570         if (more)
571                 msg.msg_flags |= MSG_MORE;
572         else
573                 msg.msg_flags |= MSG_EOR;  /* superfluous, but what the hell */
574
575         r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
576         if (r == -EAGAIN)
577                 r = 0;
578         return r;
579 }
580
581 static int __ceph_tcp_sendpage(struct socket *sock, struct page *page,
582                      int offset, size_t size, bool more)
583 {
584         int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
585         int ret;
586
587         ret = kernel_sendpage(sock, page, offset, size, flags);
588         if (ret == -EAGAIN)
589                 ret = 0;
590
591         return ret;
592 }
593
594 static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
595                      int offset, size_t size, bool more)
596 {
597         int ret;
598         struct kvec iov;
599
600         /* sendpage cannot properly handle pages with page_count == 0,
601          * we need to fallback to sendmsg if that's the case */
602         if (page_count(page) >= 1)
603                 return __ceph_tcp_sendpage(sock, page, offset, size, more);
604
605         iov.iov_base = kmap(page) + offset;
606         iov.iov_len = size;
607         ret = ceph_tcp_sendmsg(sock, &iov, 1, size, more);
608         kunmap(page);
609
610         return ret;
611 }
612
613 /*
614  * Shutdown/close the socket for the given connection.
615  */
616 static int con_close_socket(struct ceph_connection *con)
617 {
618         int rc = 0;
619
620         dout("con_close_socket on %p sock %p\n", con, con->sock);
621         if (con->sock) {
622                 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
623                 sock_release(con->sock);
624                 con->sock = NULL;
625         }
626
627         /*
628          * Forcibly clear the SOCK_CLOSED flag.  It gets set
629          * independent of the connection mutex, and we could have
630          * received a socket close event before we had the chance to
631          * shut the socket down.
632          */
633         con_flag_clear(con, CON_FLAG_SOCK_CLOSED);
634
635         con_sock_state_closed(con);
636         return rc;
637 }
638
639 /*
640  * Reset a connection.  Discard all incoming and outgoing messages
641  * and clear *_seq state.
642  */
643 static void ceph_msg_remove(struct ceph_msg *msg)
644 {
645         list_del_init(&msg->list_head);
646
647         ceph_msg_put(msg);
648 }
649 static void ceph_msg_remove_list(struct list_head *head)
650 {
651         while (!list_empty(head)) {
652                 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
653                                                         list_head);
654                 ceph_msg_remove(msg);
655         }
656 }
657
658 static void reset_connection(struct ceph_connection *con)
659 {
660         /* reset connection, out_queue, msg_ and connect_seq */
661         /* discard existing out_queue and msg_seq */
662         dout("reset_connection %p\n", con);
663         ceph_msg_remove_list(&con->out_queue);
664         ceph_msg_remove_list(&con->out_sent);
665
666         if (con->in_msg) {
667                 BUG_ON(con->in_msg->con != con);
668                 ceph_msg_put(con->in_msg);
669                 con->in_msg = NULL;
670         }
671
672         con->connect_seq = 0;
673         con->out_seq = 0;
674         if (con->out_msg) {
675                 BUG_ON(con->out_msg->con != con);
676                 ceph_msg_put(con->out_msg);
677                 con->out_msg = NULL;
678         }
679         con->in_seq = 0;
680         con->in_seq_acked = 0;
681
682         con->out_skip = 0;
683 }
684
685 /*
686  * mark a peer down.  drop any open connections.
687  */
688 void ceph_con_close(struct ceph_connection *con)
689 {
690         mutex_lock(&con->mutex);
691         dout("con_close %p peer %s\n", con,
692              ceph_pr_addr(&con->peer_addr.in_addr));
693         con->state = CON_STATE_CLOSED;
694
695         con_flag_clear(con, CON_FLAG_LOSSYTX);  /* so we retry next connect */
696         con_flag_clear(con, CON_FLAG_KEEPALIVE_PENDING);
697         con_flag_clear(con, CON_FLAG_WRITE_PENDING);
698         con_flag_clear(con, CON_FLAG_BACKOFF);
699
700         reset_connection(con);
701         con->peer_global_seq = 0;
702         cancel_con(con);
703         con_close_socket(con);
704         mutex_unlock(&con->mutex);
705 }
706 EXPORT_SYMBOL(ceph_con_close);
707
708 /*
709  * Reopen a closed connection, with a new peer address.
710  */
711 void ceph_con_open(struct ceph_connection *con,
712                    __u8 entity_type, __u64 entity_num,
713                    struct ceph_entity_addr *addr)
714 {
715         mutex_lock(&con->mutex);
716         dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
717
718         WARN_ON(con->state != CON_STATE_CLOSED);
719         con->state = CON_STATE_PREOPEN;
720
721         con->peer_name.type = (__u8) entity_type;
722         con->peer_name.num = cpu_to_le64(entity_num);
723
724         memcpy(&con->peer_addr, addr, sizeof(*addr));
725         con->delay = 0;      /* reset backoff memory */
726         mutex_unlock(&con->mutex);
727         queue_con(con);
728 }
729 EXPORT_SYMBOL(ceph_con_open);
730
731 /*
732  * return true if this connection ever successfully opened
733  */
734 bool ceph_con_opened(struct ceph_connection *con)
735 {
736         return con->connect_seq > 0;
737 }
738
739 /*
740  * initialize a new connection.
741  */
742 void ceph_con_init(struct ceph_connection *con, void *private,
743         const struct ceph_connection_operations *ops,
744         struct ceph_messenger *msgr)
745 {
746         dout("con_init %p\n", con);
747         memset(con, 0, sizeof(*con));
748         con->private = private;
749         con->ops = ops;
750         con->msgr = msgr;
751
752         con_sock_state_init(con);
753
754         mutex_init(&con->mutex);
755         INIT_LIST_HEAD(&con->out_queue);
756         INIT_LIST_HEAD(&con->out_sent);
757         INIT_DELAYED_WORK(&con->work, ceph_con_workfn);
758
759         con->state = CON_STATE_CLOSED;
760 }
761 EXPORT_SYMBOL(ceph_con_init);
762
763
764 /*
765  * We maintain a global counter to order connection attempts.  Get
766  * a unique seq greater than @gt.
767  */
768 static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
769 {
770         u32 ret;
771
772         spin_lock(&msgr->global_seq_lock);
773         if (msgr->global_seq < gt)
774                 msgr->global_seq = gt;
775         ret = ++msgr->global_seq;
776         spin_unlock(&msgr->global_seq_lock);
777         return ret;
778 }
779
780 static void con_out_kvec_reset(struct ceph_connection *con)
781 {
782         BUG_ON(con->out_skip);
783
784         con->out_kvec_left = 0;
785         con->out_kvec_bytes = 0;
786         con->out_kvec_cur = &con->out_kvec[0];
787 }
788
789 static void con_out_kvec_add(struct ceph_connection *con,
790                                 size_t size, void *data)
791 {
792         int index = con->out_kvec_left;
793
794         BUG_ON(con->out_skip);
795         BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
796
797         con->out_kvec[index].iov_len = size;
798         con->out_kvec[index].iov_base = data;
799         con->out_kvec_left++;
800         con->out_kvec_bytes += size;
801 }
802
803 /*
804  * Chop off a kvec from the end.  Return residual number of bytes for
805  * that kvec, i.e. how many bytes would have been written if the kvec
806  * hadn't been nuked.
807  */
808 static int con_out_kvec_skip(struct ceph_connection *con)
809 {
810         int off = con->out_kvec_cur - con->out_kvec;
811         int skip = 0;
812
813         if (con->out_kvec_bytes > 0) {
814                 skip = con->out_kvec[off + con->out_kvec_left - 1].iov_len;
815                 BUG_ON(con->out_kvec_bytes < skip);
816                 BUG_ON(!con->out_kvec_left);
817                 con->out_kvec_bytes -= skip;
818                 con->out_kvec_left--;
819         }
820
821         return skip;
822 }
823
824 #ifdef CONFIG_BLOCK
825
826 /*
827  * For a bio data item, a piece is whatever remains of the next
828  * entry in the current bio iovec, or the first entry in the next
829  * bio in the list.
830  */
831 static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor,
832                                         size_t length)
833 {
834         struct ceph_msg_data *data = cursor->data;
835         struct bio *bio;
836
837         BUG_ON(data->type != CEPH_MSG_DATA_BIO);
838
839         bio = data->bio;
840         BUG_ON(!bio);
841
842         cursor->resid = min(length, data->bio_length);
843         cursor->bio = bio;
844         cursor->bvec_iter = bio->bi_iter;
845         cursor->last_piece =
846                 cursor->resid <= bio_iter_len(bio, cursor->bvec_iter);
847 }
848
849 static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor,
850                                                 size_t *page_offset,
851                                                 size_t *length)
852 {
853         struct ceph_msg_data *data = cursor->data;
854         struct bio *bio;
855         struct bio_vec bio_vec;
856
857         BUG_ON(data->type != CEPH_MSG_DATA_BIO);
858
859         bio = cursor->bio;
860         BUG_ON(!bio);
861
862         bio_vec = bio_iter_iovec(bio, cursor->bvec_iter);
863
864         *page_offset = (size_t) bio_vec.bv_offset;
865         BUG_ON(*page_offset >= PAGE_SIZE);
866         if (cursor->last_piece) /* pagelist offset is always 0 */
867                 *length = cursor->resid;
868         else
869                 *length = (size_t) bio_vec.bv_len;
870         BUG_ON(*length > cursor->resid);
871         BUG_ON(*page_offset + *length > PAGE_SIZE);
872
873         return bio_vec.bv_page;
874 }
875
876 static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
877                                         size_t bytes)
878 {
879         struct bio *bio;
880         struct bio_vec bio_vec;
881
882         BUG_ON(cursor->data->type != CEPH_MSG_DATA_BIO);
883
884         bio = cursor->bio;
885         BUG_ON(!bio);
886
887         bio_vec = bio_iter_iovec(bio, cursor->bvec_iter);
888
889         /* Advance the cursor offset */
890
891         BUG_ON(cursor->resid < bytes);
892         cursor->resid -= bytes;
893
894         bio_advance_iter(bio, &cursor->bvec_iter, bytes);
895
896         if (bytes < bio_vec.bv_len)
897                 return false;   /* more bytes to process in this segment */
898
899         /* Move on to the next segment, and possibly the next bio */
900
901         if (!cursor->bvec_iter.bi_size) {
902                 bio = bio->bi_next;
903                 cursor->bio = bio;
904                 if (bio)
905                         cursor->bvec_iter = bio->bi_iter;
906                 else
907                         memset(&cursor->bvec_iter, 0,
908                                sizeof(cursor->bvec_iter));
909         }
910
911         if (!cursor->last_piece) {
912                 BUG_ON(!cursor->resid);
913                 BUG_ON(!bio);
914                 /* A short read is OK, so use <= rather than == */
915                 if (cursor->resid <= bio_iter_len(bio, cursor->bvec_iter))
916                         cursor->last_piece = true;
917         }
918
919         return true;
920 }
921 #endif /* CONFIG_BLOCK */
922
923 /*
924  * For a page array, a piece comes from the first page in the array
925  * that has not already been fully consumed.
926  */
927 static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor,
928                                         size_t length)
929 {
930         struct ceph_msg_data *data = cursor->data;
931         int page_count;
932
933         BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
934
935         BUG_ON(!data->pages);
936         BUG_ON(!data->length);
937
938         cursor->resid = min(length, data->length);
939         page_count = calc_pages_for(data->alignment, (u64)data->length);
940         cursor->page_offset = data->alignment & ~PAGE_MASK;
941         cursor->page_index = 0;
942         BUG_ON(page_count > (int)USHRT_MAX);
943         cursor->page_count = (unsigned short)page_count;
944         BUG_ON(length > SIZE_MAX - cursor->page_offset);
945         cursor->last_piece = cursor->page_offset + cursor->resid <= PAGE_SIZE;
946 }
947
948 static struct page *
949 ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
950                                         size_t *page_offset, size_t *length)
951 {
952         struct ceph_msg_data *data = cursor->data;
953
954         BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
955
956         BUG_ON(cursor->page_index >= cursor->page_count);
957         BUG_ON(cursor->page_offset >= PAGE_SIZE);
958
959         *page_offset = cursor->page_offset;
960         if (cursor->last_piece)
961                 *length = cursor->resid;
962         else
963                 *length = PAGE_SIZE - *page_offset;
964
965         return data->pages[cursor->page_index];
966 }
967
968 static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
969                                                 size_t bytes)
970 {
971         BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES);
972
973         BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
974
975         /* Advance the cursor page offset */
976
977         cursor->resid -= bytes;
978         cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK;
979         if (!bytes || cursor->page_offset)
980                 return false;   /* more bytes to process in the current page */
981
982         if (!cursor->resid)
983                 return false;   /* no more data */
984
985         /* Move on to the next page; offset is already at 0 */
986
987         BUG_ON(cursor->page_index >= cursor->page_count);
988         cursor->page_index++;
989         cursor->last_piece = cursor->resid <= PAGE_SIZE;
990
991         return true;
992 }
993
994 /*
995  * For a pagelist, a piece is whatever remains to be consumed in the
996  * first page in the list, or the front of the next page.
997  */
998 static void
999 ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
1000                                         size_t length)
1001 {
1002         struct ceph_msg_data *data = cursor->data;
1003         struct ceph_pagelist *pagelist;
1004         struct page *page;
1005
1006         BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1007
1008         pagelist = data->pagelist;
1009         BUG_ON(!pagelist);
1010
1011         if (!length)
1012                 return;         /* pagelist can be assigned but empty */
1013
1014         BUG_ON(list_empty(&pagelist->head));
1015         page = list_first_entry(&pagelist->head, struct page, lru);
1016
1017         cursor->resid = min(length, pagelist->length);
1018         cursor->page = page;
1019         cursor->offset = 0;
1020         cursor->last_piece = cursor->resid <= PAGE_SIZE;
1021 }
1022
1023 static struct page *
1024 ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
1025                                 size_t *page_offset, size_t *length)
1026 {
1027         struct ceph_msg_data *data = cursor->data;
1028         struct ceph_pagelist *pagelist;
1029
1030         BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1031
1032         pagelist = data->pagelist;
1033         BUG_ON(!pagelist);
1034
1035         BUG_ON(!cursor->page);
1036         BUG_ON(cursor->offset + cursor->resid != pagelist->length);
1037
1038         /* offset of first page in pagelist is always 0 */
1039         *page_offset = cursor->offset & ~PAGE_MASK;
1040         if (cursor->last_piece)
1041                 *length = cursor->resid;
1042         else
1043                 *length = PAGE_SIZE - *page_offset;
1044
1045         return cursor->page;
1046 }
1047
1048 static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
1049                                                 size_t bytes)
1050 {
1051         struct ceph_msg_data *data = cursor->data;
1052         struct ceph_pagelist *pagelist;
1053
1054         BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1055
1056         pagelist = data->pagelist;
1057         BUG_ON(!pagelist);
1058
1059         BUG_ON(cursor->offset + cursor->resid != pagelist->length);
1060         BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
1061
1062         /* Advance the cursor offset */
1063
1064         cursor->resid -= bytes;
1065         cursor->offset += bytes;
1066         /* offset of first page in pagelist is always 0 */
1067         if (!bytes || cursor->offset & ~PAGE_MASK)
1068                 return false;   /* more bytes to process in the current page */
1069
1070         if (!cursor->resid)
1071                 return false;   /* no more data */
1072
1073         /* Move on to the next page */
1074
1075         BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
1076         cursor->page = list_entry_next(cursor->page, lru);
1077         cursor->last_piece = cursor->resid <= PAGE_SIZE;
1078
1079         return true;
1080 }
1081
1082 /*
1083  * Message data is handled (sent or received) in pieces, where each
1084  * piece resides on a single page.  The network layer might not
1085  * consume an entire piece at once.  A data item's cursor keeps
1086  * track of which piece is next to process and how much remains to
1087  * be processed in that piece.  It also tracks whether the current
1088  * piece is the last one in the data item.
1089  */
1090 static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor)
1091 {
1092         size_t length = cursor->total_resid;
1093
1094         switch (cursor->data->type) {
1095         case CEPH_MSG_DATA_PAGELIST:
1096                 ceph_msg_data_pagelist_cursor_init(cursor, length);
1097                 break;
1098         case CEPH_MSG_DATA_PAGES:
1099                 ceph_msg_data_pages_cursor_init(cursor, length);
1100                 break;
1101 #ifdef CONFIG_BLOCK
1102         case CEPH_MSG_DATA_BIO:
1103                 ceph_msg_data_bio_cursor_init(cursor, length);
1104                 break;
1105 #endif /* CONFIG_BLOCK */
1106         case CEPH_MSG_DATA_NONE:
1107         default:
1108                 /* BUG(); */
1109                 break;
1110         }
1111         cursor->need_crc = true;
1112 }
1113
1114 static void ceph_msg_data_cursor_init(struct ceph_msg *msg, size_t length)
1115 {
1116         struct ceph_msg_data_cursor *cursor = &msg->cursor;
1117         struct ceph_msg_data *data;
1118
1119         BUG_ON(!length);
1120         BUG_ON(length > msg->data_length);
1121         BUG_ON(list_empty(&msg->data));
1122
1123         cursor->data_head = &msg->data;
1124         cursor->total_resid = length;
1125         data = list_first_entry(&msg->data, struct ceph_msg_data, links);
1126         cursor->data = data;
1127
1128         __ceph_msg_data_cursor_init(cursor);
1129 }
1130
1131 /*
1132  * Return the page containing the next piece to process for a given
1133  * data item, and supply the page offset and length of that piece.
1134  * Indicate whether this is the last piece in this data item.
1135  */
1136 static struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
1137                                         size_t *page_offset, size_t *length,
1138                                         bool *last_piece)
1139 {
1140         struct page *page;
1141
1142         switch (cursor->data->type) {
1143         case CEPH_MSG_DATA_PAGELIST:
1144                 page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
1145                 break;
1146         case CEPH_MSG_DATA_PAGES:
1147                 page = ceph_msg_data_pages_next(cursor, page_offset, length);
1148                 break;
1149 #ifdef CONFIG_BLOCK
1150         case CEPH_MSG_DATA_BIO:
1151                 page = ceph_msg_data_bio_next(cursor, page_offset, length);
1152                 break;
1153 #endif /* CONFIG_BLOCK */
1154         case CEPH_MSG_DATA_NONE:
1155         default:
1156                 page = NULL;
1157                 break;
1158         }
1159         BUG_ON(!page);
1160         BUG_ON(*page_offset + *length > PAGE_SIZE);
1161         BUG_ON(!*length);
1162         if (last_piece)
1163                 *last_piece = cursor->last_piece;
1164
1165         return page;
1166 }
1167
1168 /*
1169  * Returns true if the result moves the cursor on to the next piece
1170  * of the data item.
1171  */
1172 static bool ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor,
1173                                 size_t bytes)
1174 {
1175         bool new_piece;
1176
1177         BUG_ON(bytes > cursor->resid);
1178         switch (cursor->data->type) {
1179         case CEPH_MSG_DATA_PAGELIST:
1180                 new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
1181                 break;
1182         case CEPH_MSG_DATA_PAGES:
1183                 new_piece = ceph_msg_data_pages_advance(cursor, bytes);
1184                 break;
1185 #ifdef CONFIG_BLOCK
1186         case CEPH_MSG_DATA_BIO:
1187                 new_piece = ceph_msg_data_bio_advance(cursor, bytes);
1188                 break;
1189 #endif /* CONFIG_BLOCK */
1190         case CEPH_MSG_DATA_NONE:
1191         default:
1192                 BUG();
1193                 break;
1194         }
1195         cursor->total_resid -= bytes;
1196
1197         if (!cursor->resid && cursor->total_resid) {
1198                 WARN_ON(!cursor->last_piece);
1199                 BUG_ON(list_is_last(&cursor->data->links, cursor->data_head));
1200                 cursor->data = list_entry_next(cursor->data, links);
1201                 __ceph_msg_data_cursor_init(cursor);
1202                 new_piece = true;
1203         }
1204         cursor->need_crc = new_piece;
1205
1206         return new_piece;
1207 }
1208
1209 static size_t sizeof_footer(struct ceph_connection *con)
1210 {
1211         return (con->peer_features & CEPH_FEATURE_MSG_AUTH) ?
1212             sizeof(struct ceph_msg_footer) :
1213             sizeof(struct ceph_msg_footer_old);
1214 }
1215
1216 static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
1217 {
1218         BUG_ON(!msg);
1219         BUG_ON(!data_len);
1220
1221         /* Initialize data cursor */
1222
1223         ceph_msg_data_cursor_init(msg, (size_t)data_len);
1224 }
1225
1226 /*
1227  * Prepare footer for currently outgoing message, and finish things
1228  * off.  Assumes out_kvec* are already valid.. we just add on to the end.
1229  */
1230 static void prepare_write_message_footer(struct ceph_connection *con)
1231 {
1232         struct ceph_msg *m = con->out_msg;
1233         int v = con->out_kvec_left;
1234
1235         m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
1236
1237         dout("prepare_write_message_footer %p\n", con);
1238         con->out_kvec[v].iov_base = &m->footer;
1239         if (con->peer_features & CEPH_FEATURE_MSG_AUTH) {
1240                 if (con->ops->sign_message)
1241                         con->ops->sign_message(m);
1242                 else
1243                         m->footer.sig = 0;
1244                 con->out_kvec[v].iov_len = sizeof(m->footer);
1245                 con->out_kvec_bytes += sizeof(m->footer);
1246         } else {
1247                 m->old_footer.flags = m->footer.flags;
1248                 con->out_kvec[v].iov_len = sizeof(m->old_footer);
1249                 con->out_kvec_bytes += sizeof(m->old_footer);
1250         }
1251         con->out_kvec_left++;
1252         con->out_more = m->more_to_follow;
1253         con->out_msg_done = true;
1254 }
1255
1256 /*
1257  * Prepare headers for the next outgoing message.
1258  */
1259 static void prepare_write_message(struct ceph_connection *con)
1260 {
1261         struct ceph_msg *m;
1262         u32 crc;
1263
1264         con_out_kvec_reset(con);
1265         con->out_msg_done = false;
1266
1267         /* Sneak an ack in there first?  If we can get it into the same
1268          * TCP packet that's a good thing. */
1269         if (con->in_seq > con->in_seq_acked) {
1270                 con->in_seq_acked = con->in_seq;
1271                 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
1272                 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1273                 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1274                         &con->out_temp_ack);
1275         }
1276
1277         BUG_ON(list_empty(&con->out_queue));
1278         m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
1279         con->out_msg = m;
1280         BUG_ON(m->con != con);
1281
1282         /* put message on sent list */
1283         ceph_msg_get(m);
1284         list_move_tail(&m->list_head, &con->out_sent);
1285
1286         /*
1287          * only assign outgoing seq # if we haven't sent this message
1288          * yet.  if it is requeued, resend with it's original seq.
1289          */
1290         if (m->needs_out_seq) {
1291                 m->hdr.seq = cpu_to_le64(++con->out_seq);
1292                 m->needs_out_seq = false;
1293         }
1294         WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
1295
1296         dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
1297              m, con->out_seq, le16_to_cpu(m->hdr.type),
1298              le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
1299              m->data_length);
1300         BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
1301
1302         /* tag + hdr + front + middle */
1303         con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
1304         con_out_kvec_add(con, sizeof(con->out_hdr), &con->out_hdr);
1305         con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
1306
1307         if (m->middle)
1308                 con_out_kvec_add(con, m->middle->vec.iov_len,
1309                         m->middle->vec.iov_base);
1310
1311         /* fill in hdr crc and finalize hdr */
1312         crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
1313         con->out_msg->hdr.crc = cpu_to_le32(crc);
1314         memcpy(&con->out_hdr, &con->out_msg->hdr, sizeof(con->out_hdr));
1315
1316         /* fill in front and middle crc, footer */
1317         crc = crc32c(0, m->front.iov_base, m->front.iov_len);
1318         con->out_msg->footer.front_crc = cpu_to_le32(crc);
1319         if (m->middle) {
1320                 crc = crc32c(0, m->middle->vec.iov_base,
1321                                 m->middle->vec.iov_len);
1322                 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
1323         } else
1324                 con->out_msg->footer.middle_crc = 0;
1325         dout("%s front_crc %u middle_crc %u\n", __func__,
1326              le32_to_cpu(con->out_msg->footer.front_crc),
1327              le32_to_cpu(con->out_msg->footer.middle_crc));
1328         con->out_msg->footer.flags = 0;
1329
1330         /* is there a data payload? */
1331         con->out_msg->footer.data_crc = 0;
1332         if (m->data_length) {
1333                 prepare_message_data(con->out_msg, m->data_length);
1334                 con->out_more = 1;  /* data + footer will follow */
1335         } else {
1336                 /* no, queue up footer too and be done */
1337                 prepare_write_message_footer(con);
1338         }
1339
1340         con_flag_set(con, CON_FLAG_WRITE_PENDING);
1341 }
1342
1343 /*
1344  * Prepare an ack.
1345  */
1346 static void prepare_write_ack(struct ceph_connection *con)
1347 {
1348         dout("prepare_write_ack %p %llu -> %llu\n", con,
1349              con->in_seq_acked, con->in_seq);
1350         con->in_seq_acked = con->in_seq;
1351
1352         con_out_kvec_reset(con);
1353
1354         con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
1355
1356         con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1357         con_out_kvec_add(con, sizeof (con->out_temp_ack),
1358                                 &con->out_temp_ack);
1359
1360         con->out_more = 1;  /* more will follow.. eventually.. */
1361         con_flag_set(con, CON_FLAG_WRITE_PENDING);
1362 }
1363
1364 /*
1365  * Prepare to share the seq during handshake
1366  */
1367 static void prepare_write_seq(struct ceph_connection *con)
1368 {
1369         dout("prepare_write_seq %p %llu -> %llu\n", con,
1370              con->in_seq_acked, con->in_seq);
1371         con->in_seq_acked = con->in_seq;
1372
1373         con_out_kvec_reset(con);
1374
1375         con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1376         con_out_kvec_add(con, sizeof (con->out_temp_ack),
1377                          &con->out_temp_ack);
1378
1379         con_flag_set(con, CON_FLAG_WRITE_PENDING);
1380 }
1381
1382 /*
1383  * Prepare to write keepalive byte.
1384  */
1385 static void prepare_write_keepalive(struct ceph_connection *con)
1386 {
1387         dout("prepare_write_keepalive %p\n", con);
1388         con_out_kvec_reset(con);
1389         if (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2) {
1390                 struct timespec now = CURRENT_TIME;
1391
1392                 con_out_kvec_add(con, sizeof(tag_keepalive2), &tag_keepalive2);
1393                 ceph_encode_timespec(&con->out_temp_keepalive2, &now);
1394                 con_out_kvec_add(con, sizeof(con->out_temp_keepalive2),
1395                                  &con->out_temp_keepalive2);
1396         } else {
1397                 con_out_kvec_add(con, sizeof(tag_keepalive), &tag_keepalive);
1398         }
1399         con_flag_set(con, CON_FLAG_WRITE_PENDING);
1400 }
1401
1402 /*
1403  * Connection negotiation.
1404  */
1405
1406 static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
1407                                                 int *auth_proto)
1408 {
1409         struct ceph_auth_handshake *auth;
1410
1411         if (!con->ops->get_authorizer) {
1412                 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
1413                 con->out_connect.authorizer_len = 0;
1414                 return NULL;
1415         }
1416
1417         /* Can't hold the mutex while getting authorizer */
1418         mutex_unlock(&con->mutex);
1419         auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
1420         mutex_lock(&con->mutex);
1421
1422         if (IS_ERR(auth))
1423                 return auth;
1424         if (con->state != CON_STATE_NEGOTIATING)
1425                 return ERR_PTR(-EAGAIN);
1426
1427         con->auth_reply_buf = auth->authorizer_reply_buf;
1428         con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
1429         return auth;
1430 }
1431
1432 /*
1433  * We connected to a peer and are saying hello.
1434  */
1435 static void prepare_write_banner(struct ceph_connection *con)
1436 {
1437         con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
1438         con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
1439                                         &con->msgr->my_enc_addr);
1440
1441         con->out_more = 0;
1442         con_flag_set(con, CON_FLAG_WRITE_PENDING);
1443 }
1444
1445 static int prepare_write_connect(struct ceph_connection *con)
1446 {
1447         unsigned int global_seq = get_global_seq(con->msgr, 0);
1448         int proto;
1449         int auth_proto;
1450         struct ceph_auth_handshake *auth;
1451
1452         switch (con->peer_name.type) {
1453         case CEPH_ENTITY_TYPE_MON:
1454                 proto = CEPH_MONC_PROTOCOL;
1455                 break;
1456         case CEPH_ENTITY_TYPE_OSD:
1457                 proto = CEPH_OSDC_PROTOCOL;
1458                 break;
1459         case CEPH_ENTITY_TYPE_MDS:
1460                 proto = CEPH_MDSC_PROTOCOL;
1461                 break;
1462         default:
1463                 BUG();
1464         }
1465
1466         dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
1467              con->connect_seq, global_seq, proto);
1468
1469         con->out_connect.features =
1470             cpu_to_le64(from_msgr(con->msgr)->supported_features);
1471         con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
1472         con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
1473         con->out_connect.global_seq = cpu_to_le32(global_seq);
1474         con->out_connect.protocol_version = cpu_to_le32(proto);
1475         con->out_connect.flags = 0;
1476
1477         auth_proto = CEPH_AUTH_UNKNOWN;
1478         auth = get_connect_authorizer(con, &auth_proto);
1479         if (IS_ERR(auth))
1480                 return PTR_ERR(auth);
1481
1482         con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
1483         con->out_connect.authorizer_len = auth ?
1484                 cpu_to_le32(auth->authorizer_buf_len) : 0;
1485
1486         con_out_kvec_add(con, sizeof (con->out_connect),
1487                                         &con->out_connect);
1488         if (auth && auth->authorizer_buf_len)
1489                 con_out_kvec_add(con, auth->authorizer_buf_len,
1490                                         auth->authorizer_buf);
1491
1492         con->out_more = 0;
1493         con_flag_set(con, CON_FLAG_WRITE_PENDING);
1494
1495         return 0;
1496 }
1497
1498 /*
1499  * write as much of pending kvecs to the socket as we can.
1500  *  1 -> done
1501  *  0 -> socket full, but more to do
1502  * <0 -> error
1503  */
1504 static int write_partial_kvec(struct ceph_connection *con)
1505 {
1506         int ret;
1507
1508         dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
1509         while (con->out_kvec_bytes > 0) {
1510                 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
1511                                        con->out_kvec_left, con->out_kvec_bytes,
1512                                        con->out_more);
1513                 if (ret <= 0)
1514                         goto out;
1515                 con->out_kvec_bytes -= ret;
1516                 if (con->out_kvec_bytes == 0)
1517                         break;            /* done */
1518
1519                 /* account for full iov entries consumed */
1520                 while (ret >= con->out_kvec_cur->iov_len) {
1521                         BUG_ON(!con->out_kvec_left);
1522                         ret -= con->out_kvec_cur->iov_len;
1523                         con->out_kvec_cur++;
1524                         con->out_kvec_left--;
1525                 }
1526                 /* and for a partially-consumed entry */
1527                 if (ret) {
1528                         con->out_kvec_cur->iov_len -= ret;
1529                         con->out_kvec_cur->iov_base += ret;
1530                 }
1531         }
1532         con->out_kvec_left = 0;
1533         ret = 1;
1534 out:
1535         dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
1536              con->out_kvec_bytes, con->out_kvec_left, ret);
1537         return ret;  /* done! */
1538 }
1539
1540 static u32 ceph_crc32c_page(u32 crc, struct page *page,
1541                                 unsigned int page_offset,
1542                                 unsigned int length)
1543 {
1544         char *kaddr;
1545
1546         kaddr = kmap(page);
1547         BUG_ON(kaddr == NULL);
1548         crc = crc32c(crc, kaddr + page_offset, length);
1549         kunmap(page);
1550
1551         return crc;
1552 }
1553 /*
1554  * Write as much message data payload as we can.  If we finish, queue
1555  * up the footer.
1556  *  1 -> done, footer is now queued in out_kvec[].
1557  *  0 -> socket full, but more to do
1558  * <0 -> error
1559  */
1560 static int write_partial_message_data(struct ceph_connection *con)
1561 {
1562         struct ceph_msg *msg = con->out_msg;
1563         struct ceph_msg_data_cursor *cursor = &msg->cursor;
1564         bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
1565         u32 crc;
1566
1567         dout("%s %p msg %p\n", __func__, con, msg);
1568
1569         if (list_empty(&msg->data))
1570                 return -EINVAL;
1571
1572         /*
1573          * Iterate through each page that contains data to be
1574          * written, and send as much as possible for each.
1575          *
1576          * If we are calculating the data crc (the default), we will
1577          * need to map the page.  If we have no pages, they have
1578          * been revoked, so use the zero page.
1579          */
1580         crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
1581         while (cursor->resid) {
1582                 struct page *page;
1583                 size_t page_offset;
1584                 size_t length;
1585                 bool last_piece;
1586                 bool need_crc;
1587                 int ret;
1588
1589                 page = ceph_msg_data_next(cursor, &page_offset, &length,
1590                                           &last_piece);
1591                 ret = ceph_tcp_sendpage(con->sock, page, page_offset,
1592                                         length, !last_piece);
1593                 if (ret <= 0) {
1594                         if (do_datacrc)
1595                                 msg->footer.data_crc = cpu_to_le32(crc);
1596
1597                         return ret;
1598                 }
1599                 if (do_datacrc && cursor->need_crc)
1600                         crc = ceph_crc32c_page(crc, page, page_offset, length);
1601                 need_crc = ceph_msg_data_advance(cursor, (size_t)ret);
1602         }
1603
1604         dout("%s %p msg %p done\n", __func__, con, msg);
1605
1606         /* prepare and queue up footer, too */
1607         if (do_datacrc)
1608                 msg->footer.data_crc = cpu_to_le32(crc);
1609         else
1610                 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
1611         con_out_kvec_reset(con);
1612         prepare_write_message_footer(con);
1613
1614         return 1;       /* must return > 0 to indicate success */
1615 }
1616
1617 /*
1618  * write some zeros
1619  */
1620 static int write_partial_skip(struct ceph_connection *con)
1621 {
1622         int ret;
1623
1624         dout("%s %p %d left\n", __func__, con, con->out_skip);
1625         while (con->out_skip > 0) {
1626                 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
1627
1628                 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
1629                 if (ret <= 0)
1630                         goto out;
1631                 con->out_skip -= ret;
1632         }
1633         ret = 1;
1634 out:
1635         return ret;
1636 }
1637
1638 /*
1639  * Prepare to read connection handshake, or an ack.
1640  */
1641 static void prepare_read_banner(struct ceph_connection *con)
1642 {
1643         dout("prepare_read_banner %p\n", con);
1644         con->in_base_pos = 0;
1645 }
1646
1647 static void prepare_read_connect(struct ceph_connection *con)
1648 {
1649         dout("prepare_read_connect %p\n", con);
1650         con->in_base_pos = 0;
1651 }
1652
1653 static void prepare_read_ack(struct ceph_connection *con)
1654 {
1655         dout("prepare_read_ack %p\n", con);
1656         con->in_base_pos = 0;
1657 }
1658
1659 static void prepare_read_seq(struct ceph_connection *con)
1660 {
1661         dout("prepare_read_seq %p\n", con);
1662         con->in_base_pos = 0;
1663         con->in_tag = CEPH_MSGR_TAG_SEQ;
1664 }
1665
1666 static void prepare_read_tag(struct ceph_connection *con)
1667 {
1668         dout("prepare_read_tag %p\n", con);
1669         con->in_base_pos = 0;
1670         con->in_tag = CEPH_MSGR_TAG_READY;
1671 }
1672
1673 static void prepare_read_keepalive_ack(struct ceph_connection *con)
1674 {
1675         dout("prepare_read_keepalive_ack %p\n", con);
1676         con->in_base_pos = 0;
1677 }
1678
1679 /*
1680  * Prepare to read a message.
1681  */
1682 static int prepare_read_message(struct ceph_connection *con)
1683 {
1684         dout("prepare_read_message %p\n", con);
1685         BUG_ON(con->in_msg != NULL);
1686         con->in_base_pos = 0;
1687         con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1688         return 0;
1689 }
1690
1691
1692 static int read_partial(struct ceph_connection *con,
1693                         int end, int size, void *object)
1694 {
1695         while (con->in_base_pos < end) {
1696                 int left = end - con->in_base_pos;
1697                 int have = size - left;
1698                 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1699                 if (ret <= 0)
1700                         return ret;
1701                 con->in_base_pos += ret;
1702         }
1703         return 1;
1704 }
1705
1706
1707 /*
1708  * Read all or part of the connect-side handshake on a new connection
1709  */
1710 static int read_partial_banner(struct ceph_connection *con)
1711 {
1712         int size;
1713         int end;
1714         int ret;
1715
1716         dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
1717
1718         /* peer's banner */
1719         size = strlen(CEPH_BANNER);
1720         end = size;
1721         ret = read_partial(con, end, size, con->in_banner);
1722         if (ret <= 0)
1723                 goto out;
1724
1725         size = sizeof (con->actual_peer_addr);
1726         end += size;
1727         ret = read_partial(con, end, size, &con->actual_peer_addr);
1728         if (ret <= 0)
1729                 goto out;
1730
1731         size = sizeof (con->peer_addr_for_me);
1732         end += size;
1733         ret = read_partial(con, end, size, &con->peer_addr_for_me);
1734         if (ret <= 0)
1735                 goto out;
1736
1737 out:
1738         return ret;
1739 }
1740
1741 static int read_partial_connect(struct ceph_connection *con)
1742 {
1743         int size;
1744         int end;
1745         int ret;
1746
1747         dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1748
1749         size = sizeof (con->in_reply);
1750         end = size;
1751         ret = read_partial(con, end, size, &con->in_reply);
1752         if (ret <= 0)
1753                 goto out;
1754
1755         size = le32_to_cpu(con->in_reply.authorizer_len);
1756         end += size;
1757         ret = read_partial(con, end, size, con->auth_reply_buf);
1758         if (ret <= 0)
1759                 goto out;
1760
1761         dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1762              con, (int)con->in_reply.tag,
1763              le32_to_cpu(con->in_reply.connect_seq),
1764              le32_to_cpu(con->in_reply.global_seq));
1765 out:
1766         return ret;
1767
1768 }
1769
1770 /*
1771  * Verify the hello banner looks okay.
1772  */
1773 static int verify_hello(struct ceph_connection *con)
1774 {
1775         if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
1776                 pr_err("connect to %s got bad banner\n",
1777                        ceph_pr_addr(&con->peer_addr.in_addr));
1778                 con->error_msg = "protocol error, bad banner";
1779                 return -1;
1780         }
1781         return 0;
1782 }
1783
1784 static bool addr_is_blank(struct sockaddr_storage *ss)
1785 {
1786         struct in_addr *addr = &((struct sockaddr_in *)ss)->sin_addr;
1787         struct in6_addr *addr6 = &((struct sockaddr_in6 *)ss)->sin6_addr;
1788
1789         switch (ss->ss_family) {
1790         case AF_INET:
1791                 return addr->s_addr == htonl(INADDR_ANY);
1792         case AF_INET6:
1793                 return ipv6_addr_any(addr6);
1794         default:
1795                 return true;
1796         }
1797 }
1798
1799 static int addr_port(struct sockaddr_storage *ss)
1800 {
1801         switch (ss->ss_family) {
1802         case AF_INET:
1803                 return ntohs(((struct sockaddr_in *)ss)->sin_port);
1804         case AF_INET6:
1805                 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
1806         }
1807         return 0;
1808 }
1809
1810 static void addr_set_port(struct sockaddr_storage *ss, int p)
1811 {
1812         switch (ss->ss_family) {
1813         case AF_INET:
1814                 ((struct sockaddr_in *)ss)->sin_port = htons(p);
1815                 break;
1816         case AF_INET6:
1817                 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
1818                 break;
1819         }
1820 }
1821
1822 /*
1823  * Unlike other *_pton function semantics, zero indicates success.
1824  */
1825 static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1826                 char delim, const char **ipend)
1827 {
1828         struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1829         struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
1830
1831         memset(ss, 0, sizeof(*ss));
1832
1833         if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1834                 ss->ss_family = AF_INET;
1835                 return 0;
1836         }
1837
1838         if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1839                 ss->ss_family = AF_INET6;
1840                 return 0;
1841         }
1842
1843         return -EINVAL;
1844 }
1845
1846 /*
1847  * Extract hostname string and resolve using kernel DNS facility.
1848  */
1849 #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1850 static int ceph_dns_resolve_name(const char *name, size_t namelen,
1851                 struct sockaddr_storage *ss, char delim, const char **ipend)
1852 {
1853         const char *end, *delim_p;
1854         char *colon_p, *ip_addr = NULL;
1855         int ip_len, ret;
1856
1857         /*
1858          * The end of the hostname occurs immediately preceding the delimiter or
1859          * the port marker (':') where the delimiter takes precedence.
1860          */
1861         delim_p = memchr(name, delim, namelen);
1862         colon_p = memchr(name, ':', namelen);
1863
1864         if (delim_p && colon_p)
1865                 end = delim_p < colon_p ? delim_p : colon_p;
1866         else if (!delim_p && colon_p)
1867                 end = colon_p;
1868         else {
1869                 end = delim_p;
1870                 if (!end) /* case: hostname:/ */
1871                         end = name + namelen;
1872         }
1873
1874         if (end <= name)
1875                 return -EINVAL;
1876
1877         /* do dns_resolve upcall */
1878         ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1879         if (ip_len > 0)
1880                 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1881         else
1882                 ret = -ESRCH;
1883
1884         kfree(ip_addr);
1885
1886         *ipend = end;
1887
1888         pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1889                         ret, ret ? "failed" : ceph_pr_addr(ss));
1890
1891         return ret;
1892 }
1893 #else
1894 static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1895                 struct sockaddr_storage *ss, char delim, const char **ipend)
1896 {
1897         return -EINVAL;
1898 }
1899 #endif
1900
1901 /*
1902  * Parse a server name (IP or hostname). If a valid IP address is not found
1903  * then try to extract a hostname to resolve using userspace DNS upcall.
1904  */
1905 static int ceph_parse_server_name(const char *name, size_t namelen,
1906                         struct sockaddr_storage *ss, char delim, const char **ipend)
1907 {
1908         int ret;
1909
1910         ret = ceph_pton(name, namelen, ss, delim, ipend);
1911         if (ret)
1912                 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1913
1914         return ret;
1915 }
1916
1917 /*
1918  * Parse an ip[:port] list into an addr array.  Use the default
1919  * monitor port if a port isn't specified.
1920  */
1921 int ceph_parse_ips(const char *c, const char *end,
1922                    struct ceph_entity_addr *addr,
1923                    int max_count, int *count)
1924 {
1925         int i, ret = -EINVAL;
1926         const char *p = c;
1927
1928         dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1929         for (i = 0; i < max_count; i++) {
1930                 const char *ipend;
1931                 struct sockaddr_storage *ss = &addr[i].in_addr;
1932                 int port;
1933                 char delim = ',';
1934
1935                 if (*p == '[') {
1936                         delim = ']';
1937                         p++;
1938                 }
1939
1940                 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1941                 if (ret)
1942                         goto bad;
1943                 ret = -EINVAL;
1944
1945                 p = ipend;
1946
1947                 if (delim == ']') {
1948                         if (*p != ']') {
1949                                 dout("missing matching ']'\n");
1950                                 goto bad;
1951                         }
1952                         p++;
1953                 }
1954
1955                 /* port? */
1956                 if (p < end && *p == ':') {
1957                         port = 0;
1958                         p++;
1959                         while (p < end && *p >= '0' && *p <= '9') {
1960                                 port = (port * 10) + (*p - '0');
1961                                 p++;
1962                         }
1963                         if (port == 0)
1964                                 port = CEPH_MON_PORT;
1965                         else if (port > 65535)
1966                                 goto bad;
1967                 } else {
1968                         port = CEPH_MON_PORT;
1969                 }
1970
1971                 addr_set_port(ss, port);
1972
1973                 dout("parse_ips got %s\n", ceph_pr_addr(ss));
1974
1975                 if (p == end)
1976                         break;
1977                 if (*p != ',')
1978                         goto bad;
1979                 p++;
1980         }
1981
1982         if (p != end)
1983                 goto bad;
1984
1985         if (count)
1986                 *count = i + 1;
1987         return 0;
1988
1989 bad:
1990         pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
1991         return ret;
1992 }
1993 EXPORT_SYMBOL(ceph_parse_ips);
1994
1995 static int process_banner(struct ceph_connection *con)
1996 {
1997         dout("process_banner on %p\n", con);
1998
1999         if (verify_hello(con) < 0)
2000                 return -1;
2001
2002         ceph_decode_addr(&con->actual_peer_addr);
2003         ceph_decode_addr(&con->peer_addr_for_me);
2004
2005         /*
2006          * Make sure the other end is who we wanted.  note that the other
2007          * end may not yet know their ip address, so if it's 0.0.0.0, give
2008          * them the benefit of the doubt.
2009          */
2010         if (memcmp(&con->peer_addr, &con->actual_peer_addr,
2011                    sizeof(con->peer_addr)) != 0 &&
2012             !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
2013               con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
2014                 pr_warn("wrong peer, want %s/%d, got %s/%d\n",
2015                         ceph_pr_addr(&con->peer_addr.in_addr),
2016                         (int)le32_to_cpu(con->peer_addr.nonce),
2017                         ceph_pr_addr(&con->actual_peer_addr.in_addr),
2018                         (int)le32_to_cpu(con->actual_peer_addr.nonce));
2019                 con->error_msg = "wrong peer at address";
2020                 return -1;
2021         }
2022
2023         /*
2024          * did we learn our address?
2025          */
2026         if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
2027                 int port = addr_port(&con->msgr->inst.addr.in_addr);
2028
2029                 memcpy(&con->msgr->inst.addr.in_addr,
2030                        &con->peer_addr_for_me.in_addr,
2031                        sizeof(con->peer_addr_for_me.in_addr));
2032                 addr_set_port(&con->msgr->inst.addr.in_addr, port);
2033                 encode_my_addr(con->msgr);
2034                 dout("process_banner learned my addr is %s\n",
2035                      ceph_pr_addr(&con->msgr->inst.addr.in_addr));
2036         }
2037
2038         return 0;
2039 }
2040
2041 static int process_connect(struct ceph_connection *con)
2042 {
2043         u64 sup_feat = from_msgr(con->msgr)->supported_features;
2044         u64 req_feat = from_msgr(con->msgr)->required_features;
2045         u64 server_feat = ceph_sanitize_features(
2046                                 le64_to_cpu(con->in_reply.features));
2047         int ret;
2048
2049         dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
2050
2051         if (con->auth_reply_buf) {
2052                 /*
2053                  * Any connection that defines ->get_authorizer()
2054                  * should also define ->verify_authorizer_reply().
2055                  * See get_connect_authorizer().
2056                  */
2057                 ret = con->ops->verify_authorizer_reply(con, 0);
2058                 if (ret < 0) {
2059                         con->error_msg = "bad authorize reply";
2060                         return ret;
2061                 }
2062         }
2063
2064         switch (con->in_reply.tag) {
2065         case CEPH_MSGR_TAG_FEATURES:
2066                 pr_err("%s%lld %s feature set mismatch,"
2067                        " my %llx < server's %llx, missing %llx\n",
2068                        ENTITY_NAME(con->peer_name),
2069                        ceph_pr_addr(&con->peer_addr.in_addr),
2070                        sup_feat, server_feat, server_feat & ~sup_feat);
2071                 con->error_msg = "missing required protocol features";
2072                 reset_connection(con);
2073                 return -1;
2074
2075         case CEPH_MSGR_TAG_BADPROTOVER:
2076                 pr_err("%s%lld %s protocol version mismatch,"
2077                        " my %d != server's %d\n",
2078                        ENTITY_NAME(con->peer_name),
2079                        ceph_pr_addr(&con->peer_addr.in_addr),
2080                        le32_to_cpu(con->out_connect.protocol_version),
2081                        le32_to_cpu(con->in_reply.protocol_version));
2082                 con->error_msg = "protocol version mismatch";
2083                 reset_connection(con);
2084                 return -1;
2085
2086         case CEPH_MSGR_TAG_BADAUTHORIZER:
2087                 con->auth_retry++;
2088                 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
2089                      con->auth_retry);
2090                 if (con->auth_retry == 2) {
2091                         con->error_msg = "connect authorization failure";
2092                         return -1;
2093                 }
2094                 con_out_kvec_reset(con);
2095                 ret = prepare_write_connect(con);
2096                 if (ret < 0)
2097                         return ret;
2098                 prepare_read_connect(con);
2099                 break;
2100
2101         case CEPH_MSGR_TAG_RESETSESSION:
2102                 /*
2103                  * If we connected with a large connect_seq but the peer
2104                  * has no record of a session with us (no connection, or
2105                  * connect_seq == 0), they will send RESETSESION to indicate
2106                  * that they must have reset their session, and may have
2107                  * dropped messages.
2108                  */
2109                 dout("process_connect got RESET peer seq %u\n",
2110                      le32_to_cpu(con->in_reply.connect_seq));
2111                 pr_err("%s%lld %s connection reset\n",
2112                        ENTITY_NAME(con->peer_name),
2113                        ceph_pr_addr(&con->peer_addr.in_addr));
2114                 reset_connection(con);
2115                 con_out_kvec_reset(con);
2116                 ret = prepare_write_connect(con);
2117                 if (ret < 0)
2118                         return ret;
2119                 prepare_read_connect(con);
2120
2121                 /* Tell ceph about it. */
2122                 mutex_unlock(&con->mutex);
2123                 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
2124                 if (con->ops->peer_reset)
2125                         con->ops->peer_reset(con);
2126                 mutex_lock(&con->mutex);
2127                 if (con->state != CON_STATE_NEGOTIATING)
2128                         return -EAGAIN;
2129                 break;
2130
2131         case CEPH_MSGR_TAG_RETRY_SESSION:
2132                 /*
2133                  * If we sent a smaller connect_seq than the peer has, try
2134                  * again with a larger value.
2135                  */
2136                 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
2137                      le32_to_cpu(con->out_connect.connect_seq),
2138                      le32_to_cpu(con->in_reply.connect_seq));
2139                 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
2140                 con_out_kvec_reset(con);
2141                 ret = prepare_write_connect(con);
2142                 if (ret < 0)
2143                         return ret;
2144                 prepare_read_connect(con);
2145                 break;
2146
2147         case CEPH_MSGR_TAG_RETRY_GLOBAL:
2148                 /*
2149                  * If we sent a smaller global_seq than the peer has, try
2150                  * again with a larger value.
2151                  */
2152                 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
2153                      con->peer_global_seq,
2154                      le32_to_cpu(con->in_reply.global_seq));
2155                 get_global_seq(con->msgr,
2156                                le32_to_cpu(con->in_reply.global_seq));
2157                 con_out_kvec_reset(con);
2158                 ret = prepare_write_connect(con);
2159                 if (ret < 0)
2160                         return ret;
2161                 prepare_read_connect(con);
2162                 break;
2163
2164         case CEPH_MSGR_TAG_SEQ:
2165         case CEPH_MSGR_TAG_READY:
2166                 if (req_feat & ~server_feat) {
2167                         pr_err("%s%lld %s protocol feature mismatch,"
2168                                " my required %llx > server's %llx, need %llx\n",
2169                                ENTITY_NAME(con->peer_name),
2170                                ceph_pr_addr(&con->peer_addr.in_addr),
2171                                req_feat, server_feat, req_feat & ~server_feat);
2172                         con->error_msg = "missing required protocol features";
2173                         reset_connection(con);
2174                         return -1;
2175                 }
2176
2177                 WARN_ON(con->state != CON_STATE_NEGOTIATING);
2178                 con->state = CON_STATE_OPEN;
2179                 con->auth_retry = 0;    /* we authenticated; clear flag */
2180                 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
2181                 con->connect_seq++;
2182                 con->peer_features = server_feat;
2183                 dout("process_connect got READY gseq %d cseq %d (%d)\n",
2184                      con->peer_global_seq,
2185                      le32_to_cpu(con->in_reply.connect_seq),
2186                      con->connect_seq);
2187                 WARN_ON(con->connect_seq !=
2188                         le32_to_cpu(con->in_reply.connect_seq));
2189
2190                 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
2191                         con_flag_set(con, CON_FLAG_LOSSYTX);
2192
2193                 con->delay = 0;      /* reset backoff memory */
2194
2195                 if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
2196                         prepare_write_seq(con);
2197                         prepare_read_seq(con);
2198                 } else {
2199                         prepare_read_tag(con);
2200                 }
2201                 break;
2202
2203         case CEPH_MSGR_TAG_WAIT:
2204                 /*
2205                  * If there is a connection race (we are opening
2206                  * connections to each other), one of us may just have
2207                  * to WAIT.  This shouldn't happen if we are the
2208                  * client.
2209                  */
2210                 con->error_msg = "protocol error, got WAIT as client";
2211                 return -1;
2212
2213         default:
2214                 con->error_msg = "protocol error, garbage tag during connect";
2215                 return -1;
2216         }
2217         return 0;
2218 }
2219
2220
2221 /*
2222  * read (part of) an ack
2223  */
2224 static int read_partial_ack(struct ceph_connection *con)
2225 {
2226         int size = sizeof (con->in_temp_ack);
2227         int end = size;
2228
2229         return read_partial(con, end, size, &con->in_temp_ack);
2230 }
2231
2232 /*
2233  * We can finally discard anything that's been acked.
2234  */
2235 static void process_ack(struct ceph_connection *con)
2236 {
2237         struct ceph_msg *m;
2238         u64 ack = le64_to_cpu(con->in_temp_ack);
2239         u64 seq;
2240
2241         while (!list_empty(&con->out_sent)) {
2242                 m = list_first_entry(&con->out_sent, struct ceph_msg,
2243                                      list_head);
2244                 seq = le64_to_cpu(m->hdr.seq);
2245                 if (seq > ack)
2246                         break;
2247                 dout("got ack for seq %llu type %d at %p\n", seq,
2248                      le16_to_cpu(m->hdr.type), m);
2249                 m->ack_stamp = jiffies;
2250                 ceph_msg_remove(m);
2251         }
2252         prepare_read_tag(con);
2253 }
2254
2255
2256 static int read_partial_message_section(struct ceph_connection *con,
2257                                         struct kvec *section,
2258                                         unsigned int sec_len, u32 *crc)
2259 {
2260         int ret, left;
2261
2262         BUG_ON(!section);
2263
2264         while (section->iov_len < sec_len) {
2265                 BUG_ON(section->iov_base == NULL);
2266                 left = sec_len - section->iov_len;
2267                 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
2268                                        section->iov_len, left);
2269                 if (ret <= 0)
2270                         return ret;
2271                 section->iov_len += ret;
2272         }
2273         if (section->iov_len == sec_len)
2274                 *crc = crc32c(0, section->iov_base, section->iov_len);
2275
2276         return 1;
2277 }
2278
2279 static int read_partial_msg_data(struct ceph_connection *con)
2280 {
2281         struct ceph_msg *msg = con->in_msg;
2282         struct ceph_msg_data_cursor *cursor = &msg->cursor;
2283         bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
2284         struct page *page;
2285         size_t page_offset;
2286         size_t length;
2287         u32 crc = 0;
2288         int ret;
2289
2290         BUG_ON(!msg);
2291         if (list_empty(&msg->data))
2292                 return -EIO;
2293
2294         if (do_datacrc)
2295                 crc = con->in_data_crc;
2296         while (cursor->resid) {
2297                 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
2298                 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
2299                 if (ret <= 0) {
2300                         if (do_datacrc)
2301                                 con->in_data_crc = crc;
2302
2303                         return ret;
2304                 }
2305
2306                 if (do_datacrc)
2307                         crc = ceph_crc32c_page(crc, page, page_offset, ret);
2308                 (void) ceph_msg_data_advance(cursor, (size_t)ret);
2309         }
2310         if (do_datacrc)
2311                 con->in_data_crc = crc;
2312
2313         return 1;       /* must return > 0 to indicate success */
2314 }
2315
2316 /*
2317  * read (part of) a message.
2318  */
2319 static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
2320
2321 static int read_partial_message(struct ceph_connection *con)
2322 {
2323         struct ceph_msg *m = con->in_msg;
2324         int size;
2325         int end;
2326         int ret;
2327         unsigned int front_len, middle_len, data_len;
2328         bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
2329         bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
2330         u64 seq;
2331         u32 crc;
2332
2333         dout("read_partial_message con %p msg %p\n", con, m);
2334
2335         /* header */
2336         size = sizeof (con->in_hdr);
2337         end = size;
2338         ret = read_partial(con, end, size, &con->in_hdr);
2339         if (ret <= 0)
2340                 return ret;
2341
2342         crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
2343         if (cpu_to_le32(crc) != con->in_hdr.crc) {
2344                 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
2345                        crc, con->in_hdr.crc);
2346                 return -EBADMSG;
2347         }
2348
2349         front_len = le32_to_cpu(con->in_hdr.front_len);
2350         if (front_len > CEPH_MSG_MAX_FRONT_LEN)
2351                 return -EIO;
2352         middle_len = le32_to_cpu(con->in_hdr.middle_len);
2353         if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
2354                 return -EIO;
2355         data_len = le32_to_cpu(con->in_hdr.data_len);
2356         if (data_len > CEPH_MSG_MAX_DATA_LEN)
2357                 return -EIO;
2358
2359         /* verify seq# */
2360         seq = le64_to_cpu(con->in_hdr.seq);
2361         if ((s64)seq - (s64)con->in_seq < 1) {
2362                 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
2363                         ENTITY_NAME(con->peer_name),
2364                         ceph_pr_addr(&con->peer_addr.in_addr),
2365                         seq, con->in_seq + 1);
2366                 con->in_base_pos = -front_len - middle_len - data_len -
2367                         sizeof_footer(con);
2368                 con->in_tag = CEPH_MSGR_TAG_READY;
2369                 return 1;
2370         } else if ((s64)seq - (s64)con->in_seq > 1) {
2371                 pr_err("read_partial_message bad seq %lld expected %lld\n",
2372                        seq, con->in_seq + 1);
2373                 con->error_msg = "bad message sequence # for incoming message";
2374                 return -EBADE;
2375         }
2376
2377         /* allocate message? */
2378         if (!con->in_msg) {
2379                 int skip = 0;
2380
2381                 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
2382                      front_len, data_len);
2383                 ret = ceph_con_in_msg_alloc(con, &skip);
2384                 if (ret < 0)
2385                         return ret;
2386
2387                 BUG_ON(!con->in_msg ^ skip);
2388                 if (skip) {
2389                         /* skip this message */
2390                         dout("alloc_msg said skip message\n");
2391                         con->in_base_pos = -front_len - middle_len - data_len -
2392                                 sizeof_footer(con);
2393                         con->in_tag = CEPH_MSGR_TAG_READY;
2394                         con->in_seq++;
2395                         return 1;
2396                 }
2397
2398                 BUG_ON(!con->in_msg);
2399                 BUG_ON(con->in_msg->con != con);
2400                 m = con->in_msg;
2401                 m->front.iov_len = 0;    /* haven't read it yet */
2402                 if (m->middle)
2403                         m->middle->vec.iov_len = 0;
2404
2405                 /* prepare for data payload, if any */
2406
2407                 if (data_len)
2408                         prepare_message_data(con->in_msg, data_len);
2409         }
2410
2411         /* front */
2412         ret = read_partial_message_section(con, &m->front, front_len,
2413                                            &con->in_front_crc);
2414         if (ret <= 0)
2415                 return ret;
2416
2417         /* middle */
2418         if (m->middle) {
2419                 ret = read_partial_message_section(con, &m->middle->vec,
2420                                                    middle_len,
2421                                                    &con->in_middle_crc);
2422                 if (ret <= 0)
2423                         return ret;
2424         }
2425
2426         /* (page) data */
2427         if (data_len) {
2428                 ret = read_partial_msg_data(con);
2429                 if (ret <= 0)
2430                         return ret;
2431         }
2432
2433         /* footer */
2434         if (need_sign)
2435                 size = sizeof(m->footer);
2436         else
2437                 size = sizeof(m->old_footer);
2438
2439         end += size;
2440         ret = read_partial(con, end, size, &m->footer);
2441         if (ret <= 0)
2442                 return ret;
2443
2444         if (!need_sign) {
2445                 m->footer.flags = m->old_footer.flags;
2446                 m->footer.sig = 0;
2447         }
2448
2449         dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
2450              m, front_len, m->footer.front_crc, middle_len,
2451              m->footer.middle_crc, data_len, m->footer.data_crc);
2452
2453         /* crc ok? */
2454         if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
2455                 pr_err("read_partial_message %p front crc %u != exp. %u\n",
2456                        m, con->in_front_crc, m->footer.front_crc);
2457                 return -EBADMSG;
2458         }
2459         if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
2460                 pr_err("read_partial_message %p middle crc %u != exp %u\n",
2461                        m, con->in_middle_crc, m->footer.middle_crc);
2462                 return -EBADMSG;
2463         }
2464         if (do_datacrc &&
2465             (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
2466             con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
2467                 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
2468                        con->in_data_crc, le32_to_cpu(m->footer.data_crc));
2469                 return -EBADMSG;
2470         }
2471
2472         if (need_sign && con->ops->check_message_signature &&
2473             con->ops->check_message_signature(m)) {
2474                 pr_err("read_partial_message %p signature check failed\n", m);
2475                 return -EBADMSG;
2476         }
2477
2478         return 1; /* done! */
2479 }
2480
2481 /*
2482  * Process message.  This happens in the worker thread.  The callback should
2483  * be careful not to do anything that waits on other incoming messages or it
2484  * may deadlock.
2485  */
2486 static void process_message(struct ceph_connection *con)
2487 {
2488         struct ceph_msg *msg = con->in_msg;
2489
2490         BUG_ON(con->in_msg->con != con);
2491         con->in_msg = NULL;
2492
2493         /* if first message, set peer_name */
2494         if (con->peer_name.type == 0)
2495                 con->peer_name = msg->hdr.src;
2496
2497         con->in_seq++;
2498         mutex_unlock(&con->mutex);
2499
2500         dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
2501              msg, le64_to_cpu(msg->hdr.seq),
2502              ENTITY_NAME(msg->hdr.src),
2503              le16_to_cpu(msg->hdr.type),
2504              ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2505              le32_to_cpu(msg->hdr.front_len),
2506              le32_to_cpu(msg->hdr.data_len),
2507              con->in_front_crc, con->in_middle_crc, con->in_data_crc);
2508         con->ops->dispatch(con, msg);
2509
2510         mutex_lock(&con->mutex);
2511 }
2512
2513 static int read_keepalive_ack(struct ceph_connection *con)
2514 {
2515         struct ceph_timespec ceph_ts;
2516         size_t size = sizeof(ceph_ts);
2517         int ret = read_partial(con, size, size, &ceph_ts);
2518         if (ret <= 0)
2519                 return ret;
2520         ceph_decode_timespec(&con->last_keepalive_ack, &ceph_ts);
2521         prepare_read_tag(con);
2522         return 1;
2523 }
2524
2525 /*
2526  * Write something to the socket.  Called in a worker thread when the
2527  * socket appears to be writeable and we have something ready to send.
2528  */
2529 static int try_write(struct ceph_connection *con)
2530 {
2531         int ret = 1;
2532
2533         dout("try_write start %p state %lu\n", con, con->state);
2534
2535 more:
2536         dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
2537
2538         /* open the socket first? */
2539         if (con->state == CON_STATE_PREOPEN) {
2540                 BUG_ON(con->sock);
2541                 con->state = CON_STATE_CONNECTING;
2542
2543                 con_out_kvec_reset(con);
2544                 prepare_write_banner(con);
2545                 prepare_read_banner(con);
2546
2547                 BUG_ON(con->in_msg);
2548                 con->in_tag = CEPH_MSGR_TAG_READY;
2549                 dout("try_write initiating connect on %p new state %lu\n",
2550                      con, con->state);
2551                 ret = ceph_tcp_connect(con);
2552                 if (ret < 0) {
2553                         con->error_msg = "connect error";
2554                         goto out;
2555                 }
2556         }
2557
2558 more_kvec:
2559         /* kvec data queued? */
2560         if (con->out_kvec_left) {
2561                 ret = write_partial_kvec(con);
2562                 if (ret <= 0)
2563                         goto out;
2564         }
2565         if (con->out_skip) {
2566                 ret = write_partial_skip(con);
2567                 if (ret <= 0)
2568                         goto out;
2569         }
2570
2571         /* msg pages? */
2572         if (con->out_msg) {
2573                 if (con->out_msg_done) {
2574                         ceph_msg_put(con->out_msg);
2575                         con->out_msg = NULL;   /* we're done with this one */
2576                         goto do_next;
2577                 }
2578
2579                 ret = write_partial_message_data(con);
2580                 if (ret == 1)
2581                         goto more_kvec;  /* we need to send the footer, too! */
2582                 if (ret == 0)
2583                         goto out;
2584                 if (ret < 0) {
2585                         dout("try_write write_partial_message_data err %d\n",
2586                              ret);
2587                         goto out;
2588                 }
2589         }
2590
2591 do_next:
2592         if (con->state == CON_STATE_OPEN) {
2593                 if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
2594                         prepare_write_keepalive(con);
2595                         goto more;
2596                 }
2597                 /* is anything else pending? */
2598                 if (!list_empty(&con->out_queue)) {
2599                         prepare_write_message(con);
2600                         goto more;
2601                 }
2602                 if (con->in_seq > con->in_seq_acked) {
2603                         prepare_write_ack(con);
2604                         goto more;
2605                 }
2606         }
2607
2608         /* Nothing to do! */
2609         con_flag_clear(con, CON_FLAG_WRITE_PENDING);
2610         dout("try_write nothing else to write.\n");
2611         ret = 0;
2612 out:
2613         dout("try_write done on %p ret %d\n", con, ret);
2614         return ret;
2615 }
2616
2617
2618
2619 /*
2620  * Read what we can from the socket.
2621  */
2622 static int try_read(struct ceph_connection *con)
2623 {
2624         int ret = -1;
2625
2626 more:
2627         dout("try_read start on %p state %lu\n", con, con->state);
2628         if (con->state != CON_STATE_CONNECTING &&
2629             con->state != CON_STATE_NEGOTIATING &&
2630             con->state != CON_STATE_OPEN)
2631                 return 0;
2632
2633         BUG_ON(!con->sock);
2634
2635         dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2636              con->in_base_pos);
2637
2638         if (con->state == CON_STATE_CONNECTING) {
2639                 dout("try_read connecting\n");
2640                 ret = read_partial_banner(con);
2641                 if (ret <= 0)
2642                         goto out;
2643                 ret = process_banner(con);
2644                 if (ret < 0)
2645                         goto out;
2646
2647                 con->state = CON_STATE_NEGOTIATING;
2648
2649                 /*
2650                  * Received banner is good, exchange connection info.
2651                  * Do not reset out_kvec, as sending our banner raced
2652                  * with receiving peer banner after connect completed.
2653                  */
2654                 ret = prepare_write_connect(con);
2655                 if (ret < 0)
2656                         goto out;
2657                 prepare_read_connect(con);
2658
2659                 /* Send connection info before awaiting response */
2660                 goto out;
2661         }
2662
2663         if (con->state == CON_STATE_NEGOTIATING) {
2664                 dout("try_read negotiating\n");
2665                 ret = read_partial_connect(con);
2666                 if (ret <= 0)
2667                         goto out;
2668                 ret = process_connect(con);
2669                 if (ret < 0)
2670                         goto out;
2671                 goto more;
2672         }
2673
2674         WARN_ON(con->state != CON_STATE_OPEN);
2675
2676         if (con->in_base_pos < 0) {
2677                 /*
2678                  * skipping + discarding content.
2679                  *
2680                  * FIXME: there must be a better way to do this!
2681                  */
2682                 static char buf[SKIP_BUF_SIZE];
2683                 int skip = min((int) sizeof (buf), -con->in_base_pos);
2684
2685                 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2686                 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2687                 if (ret <= 0)
2688                         goto out;
2689                 con->in_base_pos += ret;
2690                 if (con->in_base_pos)
2691                         goto more;
2692         }
2693         if (con->in_tag == CEPH_MSGR_TAG_READY) {
2694                 /*
2695                  * what's next?
2696                  */
2697                 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2698                 if (ret <= 0)
2699                         goto out;
2700                 dout("try_read got tag %d\n", (int)con->in_tag);
2701                 switch (con->in_tag) {
2702                 case CEPH_MSGR_TAG_MSG:
2703                         prepare_read_message(con);
2704                         break;
2705                 case CEPH_MSGR_TAG_ACK:
2706                         prepare_read_ack(con);
2707                         break;
2708                 case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
2709                         prepare_read_keepalive_ack(con);
2710                         break;
2711                 case CEPH_MSGR_TAG_CLOSE:
2712                         con_close_socket(con);
2713                         con->state = CON_STATE_CLOSED;
2714                         goto out;
2715                 default:
2716                         goto bad_tag;
2717                 }
2718         }
2719         if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2720                 ret = read_partial_message(con);
2721                 if (ret <= 0) {
2722                         switch (ret) {
2723                         case -EBADMSG:
2724                                 con->error_msg = "bad crc/signature";
2725                                 /* fall through */
2726                         case -EBADE:
2727                                 ret = -EIO;
2728                                 break;
2729                         case -EIO:
2730                                 con->error_msg = "io error";
2731                                 break;
2732                         }
2733                         goto out;
2734                 }
2735                 if (con->in_tag == CEPH_MSGR_TAG_READY)
2736                         goto more;
2737                 process_message(con);
2738                 if (con->state == CON_STATE_OPEN)
2739                         prepare_read_tag(con);
2740                 goto more;
2741         }
2742         if (con->in_tag == CEPH_MSGR_TAG_ACK ||
2743             con->in_tag == CEPH_MSGR_TAG_SEQ) {
2744                 /*
2745                  * the final handshake seq exchange is semantically
2746                  * equivalent to an ACK
2747                  */
2748                 ret = read_partial_ack(con);
2749                 if (ret <= 0)
2750                         goto out;
2751                 process_ack(con);
2752                 goto more;
2753         }
2754         if (con->in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
2755                 ret = read_keepalive_ack(con);
2756                 if (ret <= 0)
2757                         goto out;
2758                 goto more;
2759         }
2760
2761 out:
2762         dout("try_read done on %p ret %d\n", con, ret);
2763         return ret;
2764
2765 bad_tag:
2766         pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2767         con->error_msg = "protocol error, garbage tag";
2768         ret = -1;
2769         goto out;
2770 }
2771
2772
2773 /*
2774  * Atomically queue work on a connection after the specified delay.
2775  * Bump @con reference to avoid races with connection teardown.
2776  * Returns 0 if work was queued, or an error code otherwise.
2777  */
2778 static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
2779 {
2780         if (!con->ops->get(con)) {
2781                 dout("%s %p ref count 0\n", __func__, con);
2782                 return -ENOENT;
2783         }
2784
2785         if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
2786                 dout("%s %p - already queued\n", __func__, con);
2787                 con->ops->put(con);
2788                 return -EBUSY;
2789         }
2790
2791         dout("%s %p %lu\n", __func__, con, delay);
2792         return 0;
2793 }
2794
2795 static void queue_con(struct ceph_connection *con)
2796 {
2797         (void) queue_con_delay(con, 0);
2798 }
2799
2800 static void cancel_con(struct ceph_connection *con)
2801 {
2802         if (cancel_delayed_work(&con->work)) {
2803                 dout("%s %p\n", __func__, con);
2804                 con->ops->put(con);
2805         }
2806 }
2807
2808 static bool con_sock_closed(struct ceph_connection *con)
2809 {
2810         if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
2811                 return false;
2812
2813 #define CASE(x)                                                         \
2814         case CON_STATE_ ## x:                                           \
2815                 con->error_msg = "socket closed (con state " #x ")";    \
2816                 break;
2817
2818         switch (con->state) {
2819         CASE(CLOSED);
2820         CASE(PREOPEN);
2821         CASE(CONNECTING);
2822         CASE(NEGOTIATING);
2823         CASE(OPEN);
2824         CASE(STANDBY);
2825         default:
2826                 pr_warn("%s con %p unrecognized state %lu\n",
2827                         __func__, con, con->state);
2828                 con->error_msg = "unrecognized con state";
2829                 BUG();
2830                 break;
2831         }
2832 #undef CASE
2833
2834         return true;
2835 }
2836
2837 static bool con_backoff(struct ceph_connection *con)
2838 {
2839         int ret;
2840
2841         if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
2842                 return false;
2843
2844         ret = queue_con_delay(con, round_jiffies_relative(con->delay));
2845         if (ret) {
2846                 dout("%s: con %p FAILED to back off %lu\n", __func__,
2847                         con, con->delay);
2848                 BUG_ON(ret == -ENOENT);
2849                 con_flag_set(con, CON_FLAG_BACKOFF);
2850         }
2851
2852         return true;
2853 }
2854
2855 /* Finish fault handling; con->mutex must *not* be held here */
2856
2857 static void con_fault_finish(struct ceph_connection *con)
2858 {
2859         /*
2860          * in case we faulted due to authentication, invalidate our
2861          * current tickets so that we can get new ones.
2862          */
2863         if (con->auth_retry && con->ops->invalidate_authorizer) {
2864                 dout("calling invalidate_authorizer()\n");
2865                 con->ops->invalidate_authorizer(con);
2866         }
2867
2868         if (con->ops->fault)
2869                 con->ops->fault(con);
2870 }
2871
2872 /*
2873  * Do some work on a connection.  Drop a connection ref when we're done.
2874  */
2875 static void ceph_con_workfn(struct work_struct *work)
2876 {
2877         struct ceph_connection *con = container_of(work, struct ceph_connection,
2878                                                    work.work);
2879         bool fault;
2880
2881         mutex_lock(&con->mutex);
2882         while (true) {
2883                 int ret;
2884
2885                 if ((fault = con_sock_closed(con))) {
2886                         dout("%s: con %p SOCK_CLOSED\n", __func__, con);
2887                         break;
2888                 }
2889                 if (con_backoff(con)) {
2890                         dout("%s: con %p BACKOFF\n", __func__, con);
2891                         break;
2892                 }
2893                 if (con->state == CON_STATE_STANDBY) {
2894                         dout("%s: con %p STANDBY\n", __func__, con);
2895                         break;
2896                 }
2897                 if (con->state == CON_STATE_CLOSED) {
2898                         dout("%s: con %p CLOSED\n", __func__, con);
2899                         BUG_ON(con->sock);
2900                         break;
2901                 }
2902                 if (con->state == CON_STATE_PREOPEN) {
2903                         dout("%s: con %p PREOPEN\n", __func__, con);
2904                         BUG_ON(con->sock);
2905                 }
2906
2907                 ret = try_read(con);
2908                 if (ret < 0) {
2909                         if (ret == -EAGAIN)
2910                                 continue;
2911                         if (!con->error_msg)
2912                                 con->error_msg = "socket error on read";
2913                         fault = true;
2914                         break;
2915                 }
2916
2917                 ret = try_write(con);
2918                 if (ret < 0) {
2919                         if (ret == -EAGAIN)
2920                                 continue;
2921                         if (!con->error_msg)
2922                                 con->error_msg = "socket error on write";
2923                         fault = true;
2924                 }
2925
2926                 break;  /* If we make it to here, we're done */
2927         }
2928         if (fault)
2929                 con_fault(con);
2930         mutex_unlock(&con->mutex);
2931
2932         if (fault)
2933                 con_fault_finish(con);
2934
2935         con->ops->put(con);
2936 }
2937
2938 /*
2939  * Generic error/fault handler.  A retry mechanism is used with
2940  * exponential backoff
2941  */
2942 static void con_fault(struct ceph_connection *con)
2943 {
2944         dout("fault %p state %lu to peer %s\n",
2945              con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
2946
2947         pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
2948                 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
2949         con->error_msg = NULL;
2950
2951         WARN_ON(con->state != CON_STATE_CONNECTING &&
2952                con->state != CON_STATE_NEGOTIATING &&
2953                con->state != CON_STATE_OPEN);
2954
2955         con_close_socket(con);
2956
2957         if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
2958                 dout("fault on LOSSYTX channel, marking CLOSED\n");
2959                 con->state = CON_STATE_CLOSED;
2960                 return;
2961         }
2962
2963         if (con->in_msg) {
2964                 BUG_ON(con->in_msg->con != con);
2965                 ceph_msg_put(con->in_msg);
2966                 con->in_msg = NULL;
2967         }
2968
2969         /* Requeue anything that hasn't been acked */
2970         list_splice_init(&con->out_sent, &con->out_queue);
2971
2972         /* If there are no messages queued or keepalive pending, place
2973          * the connection in a STANDBY state */
2974         if (list_empty(&con->out_queue) &&
2975             !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
2976                 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
2977                 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
2978                 con->state = CON_STATE_STANDBY;
2979         } else {
2980                 /* retry after a delay. */
2981                 con->state = CON_STATE_PREOPEN;
2982                 if (con->delay == 0)
2983                         con->delay = BASE_DELAY_INTERVAL;
2984                 else if (con->delay < MAX_DELAY_INTERVAL)
2985                         con->delay *= 2;
2986                 con_flag_set(con, CON_FLAG_BACKOFF);
2987                 queue_con(con);
2988         }
2989 }
2990
2991
2992
2993 /*
2994  * initialize a new messenger instance
2995  */
2996 void ceph_messenger_init(struct ceph_messenger *msgr,
2997                          struct ceph_entity_addr *myaddr)
2998 {
2999         spin_lock_init(&msgr->global_seq_lock);
3000
3001         if (myaddr)
3002                 msgr->inst.addr = *myaddr;
3003
3004         /* select a random nonce */
3005         msgr->inst.addr.type = 0;
3006         get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
3007         encode_my_addr(msgr);
3008
3009         atomic_set(&msgr->stopping, 0);
3010         write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
3011
3012         dout("%s %p\n", __func__, msgr);
3013 }
3014 EXPORT_SYMBOL(ceph_messenger_init);
3015
3016 void ceph_messenger_fini(struct ceph_messenger *msgr)
3017 {
3018         put_net(read_pnet(&msgr->net));
3019 }
3020 EXPORT_SYMBOL(ceph_messenger_fini);
3021
3022 static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con)
3023 {
3024         if (msg->con)
3025                 msg->con->ops->put(msg->con);
3026
3027         msg->con = con ? con->ops->get(con) : NULL;
3028         BUG_ON(msg->con != con);
3029 }
3030
3031 static void clear_standby(struct ceph_connection *con)
3032 {
3033         /* come back from STANDBY? */
3034         if (con->state == CON_STATE_STANDBY) {
3035                 dout("clear_standby %p and ++connect_seq\n", con);
3036                 con->state = CON_STATE_PREOPEN;
3037                 con->connect_seq++;
3038                 WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
3039                 WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
3040         }
3041 }
3042
3043 /*
3044  * Queue up an outgoing message on the given connection.
3045  */
3046 void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
3047 {
3048         /* set src+dst */
3049         msg->hdr.src = con->msgr->inst.name;
3050         BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
3051         msg->needs_out_seq = true;
3052
3053         mutex_lock(&con->mutex);
3054
3055         if (con->state == CON_STATE_CLOSED) {
3056                 dout("con_send %p closed, dropping %p\n", con, msg);
3057                 ceph_msg_put(msg);
3058                 mutex_unlock(&con->mutex);
3059                 return;
3060         }
3061
3062         msg_con_set(msg, con);
3063
3064         BUG_ON(!list_empty(&msg->list_head));
3065         list_add_tail(&msg->list_head, &con->out_queue);
3066         dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
3067              ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
3068              ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
3069              le32_to_cpu(msg->hdr.front_len),
3070              le32_to_cpu(msg->hdr.middle_len),
3071              le32_to_cpu(msg->hdr.data_len));
3072
3073         clear_standby(con);
3074         mutex_unlock(&con->mutex);
3075
3076         /* if there wasn't anything waiting to send before, queue
3077          * new work */
3078         if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
3079                 queue_con(con);
3080 }
3081 EXPORT_SYMBOL(ceph_con_send);
3082
3083 /*
3084  * Revoke a message that was previously queued for send
3085  */
3086 void ceph_msg_revoke(struct ceph_msg *msg)
3087 {
3088         struct ceph_connection *con = msg->con;
3089
3090         if (!con) {
3091                 dout("%s msg %p null con\n", __func__, msg);
3092                 return;         /* Message not in our possession */
3093         }
3094
3095         mutex_lock(&con->mutex);
3096         if (!list_empty(&msg->list_head)) {
3097                 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
3098                 list_del_init(&msg->list_head);
3099                 msg->hdr.seq = 0;
3100
3101                 ceph_msg_put(msg);
3102         }
3103         if (con->out_msg == msg) {
3104                 BUG_ON(con->out_skip);
3105                 /* footer */
3106                 if (con->out_msg_done) {
3107                         con->out_skip += con_out_kvec_skip(con);
3108                 } else {
3109                         BUG_ON(!msg->data_length);
3110                         if (con->peer_features & CEPH_FEATURE_MSG_AUTH)
3111                                 con->out_skip += sizeof(msg->footer);
3112                         else
3113                                 con->out_skip += sizeof(msg->old_footer);
3114                 }
3115                 /* data, middle, front */
3116                 if (msg->data_length)
3117                         con->out_skip += msg->cursor.total_resid;
3118                 if (msg->middle)
3119                         con->out_skip += con_out_kvec_skip(con);
3120                 con->out_skip += con_out_kvec_skip(con);
3121
3122                 dout("%s %p msg %p - was sending, will write %d skip %d\n",
3123                      __func__, con, msg, con->out_kvec_bytes, con->out_skip);
3124                 msg->hdr.seq = 0;
3125                 con->out_msg = NULL;
3126                 ceph_msg_put(msg);
3127         }
3128
3129         mutex_unlock(&con->mutex);
3130 }
3131
3132 /*
3133  * Revoke a message that we may be reading data into
3134  */
3135 void ceph_msg_revoke_incoming(struct ceph_msg *msg)
3136 {
3137         struct ceph_connection *con = msg->con;
3138
3139         if (!con) {
3140                 dout("%s msg %p null con\n", __func__, msg);
3141                 return;         /* Message not in our possession */
3142         }
3143
3144         mutex_lock(&con->mutex);
3145         if (con->in_msg == msg) {
3146                 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
3147                 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
3148                 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
3149
3150                 /* skip rest of message */
3151                 dout("%s %p msg %p revoked\n", __func__, con, msg);
3152                 con->in_base_pos = con->in_base_pos -
3153                                 sizeof(struct ceph_msg_header) -
3154                                 front_len -
3155                                 middle_len -
3156                                 data_len -
3157                                 sizeof(struct ceph_msg_footer);
3158                 ceph_msg_put(con->in_msg);
3159                 con->in_msg = NULL;
3160                 con->in_tag = CEPH_MSGR_TAG_READY;
3161                 con->in_seq++;
3162         } else {
3163                 dout("%s %p in_msg %p msg %p no-op\n",
3164                      __func__, con, con->in_msg, msg);
3165         }
3166         mutex_unlock(&con->mutex);
3167 }
3168
3169 /*
3170  * Queue a keepalive byte to ensure the tcp connection is alive.
3171  */
3172 void ceph_con_keepalive(struct ceph_connection *con)
3173 {
3174         dout("con_keepalive %p\n", con);
3175         mutex_lock(&con->mutex);
3176         clear_standby(con);
3177         mutex_unlock(&con->mutex);
3178         if (con_flag_test_and_set(con, CON_FLAG_KEEPALIVE_PENDING) == 0 &&
3179             con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
3180                 queue_con(con);
3181 }
3182 EXPORT_SYMBOL(ceph_con_keepalive);
3183
3184 bool ceph_con_keepalive_expired(struct ceph_connection *con,
3185                                unsigned long interval)
3186 {
3187         if (interval > 0 &&
3188             (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) {
3189                 struct timespec now = CURRENT_TIME;
3190                 struct timespec ts;
3191                 jiffies_to_timespec(interval, &ts);
3192                 ts = timespec_add(con->last_keepalive_ack, ts);
3193                 return timespec_compare(&now, &ts) >= 0;
3194         }
3195         return false;
3196 }
3197
3198 static struct ceph_msg_data *ceph_msg_data_create(enum ceph_msg_data_type type)
3199 {
3200         struct ceph_msg_data *data;
3201
3202         if (WARN_ON(!ceph_msg_data_type_valid(type)))
3203                 return NULL;
3204
3205         data = kmem_cache_zalloc(ceph_msg_data_cache, GFP_NOFS);
3206         if (data)
3207                 data->type = type;
3208         INIT_LIST_HEAD(&data->links);
3209
3210         return data;
3211 }
3212
3213 static void ceph_msg_data_destroy(struct ceph_msg_data *data)
3214 {
3215         if (!data)
3216                 return;
3217
3218         WARN_ON(!list_empty(&data->links));
3219         if (data->type == CEPH_MSG_DATA_PAGELIST)
3220                 ceph_pagelist_release(data->pagelist);
3221         kmem_cache_free(ceph_msg_data_cache, data);
3222 }
3223
3224 void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
3225                 size_t length, size_t alignment)
3226 {
3227         struct ceph_msg_data *data;
3228
3229         BUG_ON(!pages);
3230         BUG_ON(!length);
3231
3232         data = ceph_msg_data_create(CEPH_MSG_DATA_PAGES);
3233         BUG_ON(!data);
3234         data->pages = pages;
3235         data->length = length;
3236         data->alignment = alignment & ~PAGE_MASK;
3237
3238         list_add_tail(&data->links, &msg->data);
3239         msg->data_length += length;
3240 }
3241 EXPORT_SYMBOL(ceph_msg_data_add_pages);
3242
3243 void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
3244                                 struct ceph_pagelist *pagelist)
3245 {
3246         struct ceph_msg_data *data;
3247
3248         BUG_ON(!pagelist);
3249         BUG_ON(!pagelist->length);
3250
3251         data = ceph_msg_data_create(CEPH_MSG_DATA_PAGELIST);
3252         BUG_ON(!data);
3253         data->pagelist = pagelist;
3254
3255         list_add_tail(&data->links, &msg->data);
3256         msg->data_length += pagelist->length;
3257 }
3258 EXPORT_SYMBOL(ceph_msg_data_add_pagelist);
3259
3260 #ifdef  CONFIG_BLOCK
3261 void ceph_msg_data_add_bio(struct ceph_msg *msg, struct bio *bio,
3262                 size_t length)
3263 {
3264         struct ceph_msg_data *data;
3265
3266         BUG_ON(!bio);
3267
3268         data = ceph_msg_data_create(CEPH_MSG_DATA_BIO);
3269         BUG_ON(!data);
3270         data->bio = bio;
3271         data->bio_length = length;
3272
3273         list_add_tail(&data->links, &msg->data);
3274         msg->data_length += length;
3275 }
3276 EXPORT_SYMBOL(ceph_msg_data_add_bio);
3277 #endif  /* CONFIG_BLOCK */
3278
3279 /*
3280  * construct a new message with given type, size
3281  * the new msg has a ref count of 1.
3282  */
3283 struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
3284                               bool can_fail)
3285 {
3286         struct ceph_msg *m;
3287
3288         m = kmem_cache_zalloc(ceph_msg_cache, flags);
3289         if (m == NULL)
3290                 goto out;
3291
3292         m->hdr.type = cpu_to_le16(type);
3293         m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
3294         m->hdr.front_len = cpu_to_le32(front_len);
3295
3296         INIT_LIST_HEAD(&m->list_head);
3297         kref_init(&m->kref);
3298         INIT_LIST_HEAD(&m->data);
3299
3300         /* front */
3301         if (front_len) {
3302                 m->front.iov_base = ceph_kvmalloc(front_len, flags);
3303                 if (m->front.iov_base == NULL) {
3304                         dout("ceph_msg_new can't allocate %d bytes\n",
3305                              front_len);
3306                         goto out2;
3307                 }
3308         } else {
3309                 m->front.iov_base = NULL;
3310         }
3311         m->front_alloc_len = m->front.iov_len = front_len;
3312
3313         dout("ceph_msg_new %p front %d\n", m, front_len);
3314         return m;
3315
3316 out2:
3317         ceph_msg_put(m);
3318 out:
3319         if (!can_fail) {
3320                 pr_err("msg_new can't create type %d front %d\n", type,
3321                        front_len);
3322                 WARN_ON(1);
3323         } else {
3324                 dout("msg_new can't create type %d front %d\n", type,
3325                      front_len);
3326         }
3327         return NULL;
3328 }
3329 EXPORT_SYMBOL(ceph_msg_new);
3330
3331 /*
3332  * Allocate "middle" portion of a message, if it is needed and wasn't
3333  * allocated by alloc_msg.  This allows us to read a small fixed-size
3334  * per-type header in the front and then gracefully fail (i.e.,
3335  * propagate the error to the caller based on info in the front) when
3336  * the middle is too large.
3337  */
3338 static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
3339 {
3340         int type = le16_to_cpu(msg->hdr.type);
3341         int middle_len = le32_to_cpu(msg->hdr.middle_len);
3342
3343         dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
3344              ceph_msg_type_name(type), middle_len);
3345         BUG_ON(!middle_len);
3346         BUG_ON(msg->middle);
3347
3348         msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
3349         if (!msg->middle)
3350                 return -ENOMEM;
3351         return 0;
3352 }
3353
3354 /*
3355  * Allocate a message for receiving an incoming message on a
3356  * connection, and save the result in con->in_msg.  Uses the
3357  * connection's private alloc_msg op if available.
3358  *
3359  * Returns 0 on success, or a negative error code.
3360  *
3361  * On success, if we set *skip = 1:
3362  *  - the next message should be skipped and ignored.
3363  *  - con->in_msg == NULL
3364  * or if we set *skip = 0:
3365  *  - con->in_msg is non-null.
3366  * On error (ENOMEM, EAGAIN, ...),
3367  *  - con->in_msg == NULL
3368  */
3369 static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
3370 {
3371         struct ceph_msg_header *hdr = &con->in_hdr;
3372         int middle_len = le32_to_cpu(hdr->middle_len);
3373         struct ceph_msg *msg;
3374         int ret = 0;
3375
3376         BUG_ON(con->in_msg != NULL);
3377         BUG_ON(!con->ops->alloc_msg);
3378
3379         mutex_unlock(&con->mutex);
3380         msg = con->ops->alloc_msg(con, hdr, skip);
3381         mutex_lock(&con->mutex);
3382         if (con->state != CON_STATE_OPEN) {
3383                 if (msg)
3384                         ceph_msg_put(msg);
3385                 return -EAGAIN;
3386         }
3387         if (msg) {
3388                 BUG_ON(*skip);
3389                 msg_con_set(msg, con);
3390                 con->in_msg = msg;
3391         } else {
3392                 /*
3393                  * Null message pointer means either we should skip
3394                  * this message or we couldn't allocate memory.  The
3395                  * former is not an error.
3396                  */
3397                 if (*skip)
3398                         return 0;
3399
3400                 con->error_msg = "error allocating memory for incoming message";
3401                 return -ENOMEM;
3402         }
3403         memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
3404
3405         if (middle_len && !con->in_msg->middle) {
3406                 ret = ceph_alloc_middle(con, con->in_msg);
3407                 if (ret < 0) {
3408                         ceph_msg_put(con->in_msg);
3409                         con->in_msg = NULL;
3410                 }
3411         }
3412
3413         return ret;
3414 }
3415
3416
3417 /*
3418  * Free a generically kmalloc'd message.
3419  */
3420 static void ceph_msg_free(struct ceph_msg *m)
3421 {
3422         dout("%s %p\n", __func__, m);
3423         kvfree(m->front.iov_base);
3424         kmem_cache_free(ceph_msg_cache, m);
3425 }
3426
3427 static void ceph_msg_release(struct kref *kref)
3428 {
3429         struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
3430         LIST_HEAD(data);
3431         struct list_head *links;
3432         struct list_head *next;
3433
3434         dout("%s %p\n", __func__, m);
3435         WARN_ON(!list_empty(&m->list_head));
3436
3437         msg_con_set(m, NULL);
3438
3439         /* drop middle, data, if any */
3440         if (m->middle) {
3441                 ceph_buffer_put(m->middle);
3442                 m->middle = NULL;
3443         }
3444
3445         list_splice_init(&m->data, &data);
3446         list_for_each_safe(links, next, &data) {
3447                 struct ceph_msg_data *data;
3448
3449                 data = list_entry(links, struct ceph_msg_data, links);
3450                 list_del_init(links);
3451                 ceph_msg_data_destroy(data);
3452         }
3453         m->data_length = 0;
3454
3455         if (m->pool)
3456                 ceph_msgpool_put(m->pool, m);
3457         else
3458                 ceph_msg_free(m);
3459 }
3460
3461 struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
3462 {
3463         dout("%s %p (was %d)\n", __func__, msg,
3464              atomic_read(&msg->kref.refcount));
3465         kref_get(&msg->kref);
3466         return msg;
3467 }
3468 EXPORT_SYMBOL(ceph_msg_get);
3469
3470 void ceph_msg_put(struct ceph_msg *msg)
3471 {
3472         dout("%s %p (was %d)\n", __func__, msg,
3473              atomic_read(&msg->kref.refcount));
3474         kref_put(&msg->kref, ceph_msg_release);
3475 }
3476 EXPORT_SYMBOL(ceph_msg_put);
3477
3478 void ceph_msg_dump(struct ceph_msg *msg)
3479 {
3480         pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg,
3481                  msg->front_alloc_len, msg->data_length);
3482         print_hex_dump(KERN_DEBUG, "header: ",
3483                        DUMP_PREFIX_OFFSET, 16, 1,
3484                        &msg->hdr, sizeof(msg->hdr), true);
3485         print_hex_dump(KERN_DEBUG, " front: ",
3486                        DUMP_PREFIX_OFFSET, 16, 1,
3487                        msg->front.iov_base, msg->front.iov_len, true);
3488         if (msg->middle)
3489                 print_hex_dump(KERN_DEBUG, "middle: ",
3490                                DUMP_PREFIX_OFFSET, 16, 1,
3491                                msg->middle->vec.iov_base,
3492                                msg->middle->vec.iov_len, true);
3493         print_hex_dump(KERN_DEBUG, "footer: ",
3494                        DUMP_PREFIX_OFFSET, 16, 1,
3495                        &msg->footer, sizeof(msg->footer), true);
3496 }
3497 EXPORT_SYMBOL(ceph_msg_dump);