]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/ceph/messenger.c
ceph: fix printing of ipv6 addrs
[karo-tx-linux.git] / fs / ceph / messenger.c
1 #include "ceph_debug.h"
2
3 #include <linux/crc32c.h>
4 #include <linux/ctype.h>
5 #include <linux/highmem.h>
6 #include <linux/inet.h>
7 #include <linux/kthread.h>
8 #include <linux/net.h>
9 #include <linux/slab.h>
10 #include <linux/socket.h>
11 #include <linux/string.h>
12 #include <net/tcp.h>
13
14 #include "super.h"
15 #include "messenger.h"
16 #include "decode.h"
17 #include "pagelist.h"
18
19 /*
20  * Ceph uses the messenger to exchange ceph_msg messages with other
21  * hosts in the system.  The messenger provides ordered and reliable
22  * delivery.  We tolerate TCP disconnects by reconnecting (with
23  * exponential backoff) in the case of a fault (disconnection, bad
24  * crc, protocol error).  Acks allow sent messages to be discarded by
25  * the sender.
26  */
27
28 /* static tag bytes (protocol control messages) */
29 static char tag_msg = CEPH_MSGR_TAG_MSG;
30 static char tag_ack = CEPH_MSGR_TAG_ACK;
31 static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
32
33 #ifdef CONFIG_LOCKDEP
34 static struct lock_class_key socket_class;
35 #endif
36
37
38 static void queue_con(struct ceph_connection *con);
39 static void con_work(struct work_struct *);
40 static void ceph_fault(struct ceph_connection *con);
41
42 /*
43  * nicely render a sockaddr as a string.
44  */
45 #define MAX_ADDR_STR 20
46 #define MAX_ADDR_STR_LEN 60
47 static char addr_str[MAX_ADDR_STR][MAX_ADDR_STR_LEN];
48 static DEFINE_SPINLOCK(addr_str_lock);
49 static int last_addr_str;
50
51 const char *pr_addr(const struct sockaddr_storage *ss)
52 {
53         int i;
54         char *s;
55         struct sockaddr_in *in4 = (void *)ss;
56         struct sockaddr_in6 *in6 = (void *)ss;
57
58         spin_lock(&addr_str_lock);
59         i = last_addr_str++;
60         if (last_addr_str == MAX_ADDR_STR)
61                 last_addr_str = 0;
62         spin_unlock(&addr_str_lock);
63         s = addr_str[i];
64
65         switch (ss->ss_family) {
66         case AF_INET:
67                 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%u", &in4->sin_addr,
68                          (unsigned int)ntohs(in4->sin_port));
69                 break;
70
71         case AF_INET6:
72                 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%u", &in6->sin6_addr,
73                          (unsigned int)ntohs(in6->sin6_port));
74                 break;
75
76         default:
77                 sprintf(s, "(unknown sockaddr family %d)", (int)ss->ss_family);
78         }
79
80         return s;
81 }
82
83 static void encode_my_addr(struct ceph_messenger *msgr)
84 {
85         memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
86         ceph_encode_addr(&msgr->my_enc_addr);
87 }
88
89 /*
90  * work queue for all reading and writing to/from the socket.
91  */
92 struct workqueue_struct *ceph_msgr_wq;
93
94 int __init ceph_msgr_init(void)
95 {
96         ceph_msgr_wq = create_workqueue("ceph-msgr");
97         if (IS_ERR(ceph_msgr_wq)) {
98                 int ret = PTR_ERR(ceph_msgr_wq);
99                 pr_err("msgr_init failed to create workqueue: %d\n", ret);
100                 ceph_msgr_wq = NULL;
101                 return ret;
102         }
103         return 0;
104 }
105
106 void ceph_msgr_exit(void)
107 {
108         destroy_workqueue(ceph_msgr_wq);
109 }
110
111 void ceph_msgr_flush()
112 {
113         flush_workqueue(ceph_msgr_wq);
114 }
115
116
117 /*
118  * socket callback functions
119  */
120
121 /* data available on socket, or listen socket received a connect */
122 static void ceph_data_ready(struct sock *sk, int count_unused)
123 {
124         struct ceph_connection *con =
125                 (struct ceph_connection *)sk->sk_user_data;
126         if (sk->sk_state != TCP_CLOSE_WAIT) {
127                 dout("ceph_data_ready on %p state = %lu, queueing work\n",
128                      con, con->state);
129                 queue_con(con);
130         }
131 }
132
133 /* socket has buffer space for writing */
134 static void ceph_write_space(struct sock *sk)
135 {
136         struct ceph_connection *con =
137                 (struct ceph_connection *)sk->sk_user_data;
138
139         /* only queue to workqueue if there is data we want to write. */
140         if (test_bit(WRITE_PENDING, &con->state)) {
141                 dout("ceph_write_space %p queueing write work\n", con);
142                 queue_con(con);
143         } else {
144                 dout("ceph_write_space %p nothing to write\n", con);
145         }
146
147         /* since we have our own write_space, clear the SOCK_NOSPACE flag */
148         clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
149 }
150
151 /* socket's state has changed */
152 static void ceph_state_change(struct sock *sk)
153 {
154         struct ceph_connection *con =
155                 (struct ceph_connection *)sk->sk_user_data;
156
157         dout("ceph_state_change %p state = %lu sk_state = %u\n",
158              con, con->state, sk->sk_state);
159
160         if (test_bit(CLOSED, &con->state))
161                 return;
162
163         switch (sk->sk_state) {
164         case TCP_CLOSE:
165                 dout("ceph_state_change TCP_CLOSE\n");
166         case TCP_CLOSE_WAIT:
167                 dout("ceph_state_change TCP_CLOSE_WAIT\n");
168                 if (test_and_set_bit(SOCK_CLOSED, &con->state) == 0) {
169                         if (test_bit(CONNECTING, &con->state))
170                                 con->error_msg = "connection failed";
171                         else
172                                 con->error_msg = "socket closed";
173                         queue_con(con);
174                 }
175                 break;
176         case TCP_ESTABLISHED:
177                 dout("ceph_state_change TCP_ESTABLISHED\n");
178                 queue_con(con);
179                 break;
180         }
181 }
182
183 /*
184  * set up socket callbacks
185  */
186 static void set_sock_callbacks(struct socket *sock,
187                                struct ceph_connection *con)
188 {
189         struct sock *sk = sock->sk;
190         sk->sk_user_data = (void *)con;
191         sk->sk_data_ready = ceph_data_ready;
192         sk->sk_write_space = ceph_write_space;
193         sk->sk_state_change = ceph_state_change;
194 }
195
196
197 /*
198  * socket helpers
199  */
200
201 /*
202  * initiate connection to a remote socket.
203  */
204 static struct socket *ceph_tcp_connect(struct ceph_connection *con)
205 {
206         struct sockaddr *paddr = (struct sockaddr *)&con->peer_addr.in_addr;
207         struct socket *sock;
208         int ret;
209
210         BUG_ON(con->sock);
211         ret = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
212         if (ret)
213                 return ERR_PTR(ret);
214         con->sock = sock;
215         sock->sk->sk_allocation = GFP_NOFS;
216
217 #ifdef CONFIG_LOCKDEP
218         lockdep_set_class(&sock->sk->sk_lock, &socket_class);
219 #endif
220
221         set_sock_callbacks(sock, con);
222
223         dout("connect %s\n", pr_addr(&con->peer_addr.in_addr));
224
225         ret = sock->ops->connect(sock, paddr, sizeof(*paddr), O_NONBLOCK);
226         if (ret == -EINPROGRESS) {
227                 dout("connect %s EINPROGRESS sk_state = %u\n",
228                      pr_addr(&con->peer_addr.in_addr),
229                      sock->sk->sk_state);
230                 ret = 0;
231         }
232         if (ret < 0) {
233                 pr_err("connect %s error %d\n",
234                        pr_addr(&con->peer_addr.in_addr), ret);
235                 sock_release(sock);
236                 con->sock = NULL;
237                 con->error_msg = "connect error";
238         }
239
240         if (ret < 0)
241                 return ERR_PTR(ret);
242         return sock;
243 }
244
245 static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
246 {
247         struct kvec iov = {buf, len};
248         struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
249
250         return kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
251 }
252
253 /*
254  * write something.  @more is true if caller will be sending more data
255  * shortly.
256  */
257 static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
258                      size_t kvlen, size_t len, int more)
259 {
260         struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
261
262         if (more)
263                 msg.msg_flags |= MSG_MORE;
264         else
265                 msg.msg_flags |= MSG_EOR;  /* superfluous, but what the hell */
266
267         return kernel_sendmsg(sock, &msg, iov, kvlen, len);
268 }
269
270
271 /*
272  * Shutdown/close the socket for the given connection.
273  */
274 static int con_close_socket(struct ceph_connection *con)
275 {
276         int rc;
277
278         dout("con_close_socket on %p sock %p\n", con, con->sock);
279         if (!con->sock)
280                 return 0;
281         set_bit(SOCK_CLOSED, &con->state);
282         rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
283         sock_release(con->sock);
284         con->sock = NULL;
285         clear_bit(SOCK_CLOSED, &con->state);
286         return rc;
287 }
288
289 /*
290  * Reset a connection.  Discard all incoming and outgoing messages
291  * and clear *_seq state.
292  */
293 static void ceph_msg_remove(struct ceph_msg *msg)
294 {
295         list_del_init(&msg->list_head);
296         ceph_msg_put(msg);
297 }
298 static void ceph_msg_remove_list(struct list_head *head)
299 {
300         while (!list_empty(head)) {
301                 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
302                                                         list_head);
303                 ceph_msg_remove(msg);
304         }
305 }
306
307 static void reset_connection(struct ceph_connection *con)
308 {
309         /* reset connection, out_queue, msg_ and connect_seq */
310         /* discard existing out_queue and msg_seq */
311         ceph_msg_remove_list(&con->out_queue);
312         ceph_msg_remove_list(&con->out_sent);
313
314         if (con->in_msg) {
315                 ceph_msg_put(con->in_msg);
316                 con->in_msg = NULL;
317         }
318
319         con->connect_seq = 0;
320         con->out_seq = 0;
321         if (con->out_msg) {
322                 ceph_msg_put(con->out_msg);
323                 con->out_msg = NULL;
324         }
325         con->out_keepalive_pending = false;
326         con->in_seq = 0;
327         con->in_seq_acked = 0;
328 }
329
330 /*
331  * mark a peer down.  drop any open connections.
332  */
333 void ceph_con_close(struct ceph_connection *con)
334 {
335         dout("con_close %p peer %s\n", con, pr_addr(&con->peer_addr.in_addr));
336         set_bit(CLOSED, &con->state);  /* in case there's queued work */
337         clear_bit(STANDBY, &con->state);  /* avoid connect_seq bump */
338         clear_bit(LOSSYTX, &con->state);  /* so we retry next connect */
339         clear_bit(KEEPALIVE_PENDING, &con->state);
340         clear_bit(WRITE_PENDING, &con->state);
341         mutex_lock(&con->mutex);
342         reset_connection(con);
343         con->peer_global_seq = 0;
344         cancel_delayed_work(&con->work);
345         mutex_unlock(&con->mutex);
346         queue_con(con);
347 }
348
349 /*
350  * Reopen a closed connection, with a new peer address.
351  */
352 void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
353 {
354         dout("con_open %p %s\n", con, pr_addr(&addr->in_addr));
355         set_bit(OPENING, &con->state);
356         clear_bit(CLOSED, &con->state);
357         memcpy(&con->peer_addr, addr, sizeof(*addr));
358         con->delay = 0;      /* reset backoff memory */
359         queue_con(con);
360 }
361
362 /*
363  * return true if this connection ever successfully opened
364  */
365 bool ceph_con_opened(struct ceph_connection *con)
366 {
367         return con->connect_seq > 0;
368 }
369
370 /*
371  * generic get/put
372  */
373 struct ceph_connection *ceph_con_get(struct ceph_connection *con)
374 {
375         dout("con_get %p nref = %d -> %d\n", con,
376              atomic_read(&con->nref), atomic_read(&con->nref) + 1);
377         if (atomic_inc_not_zero(&con->nref))
378                 return con;
379         return NULL;
380 }
381
382 void ceph_con_put(struct ceph_connection *con)
383 {
384         dout("con_put %p nref = %d -> %d\n", con,
385              atomic_read(&con->nref), atomic_read(&con->nref) - 1);
386         BUG_ON(atomic_read(&con->nref) == 0);
387         if (atomic_dec_and_test(&con->nref)) {
388                 BUG_ON(con->sock);
389                 kfree(con);
390         }
391 }
392
393 /*
394  * initialize a new connection.
395  */
396 void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con)
397 {
398         dout("con_init %p\n", con);
399         memset(con, 0, sizeof(*con));
400         atomic_set(&con->nref, 1);
401         con->msgr = msgr;
402         mutex_init(&con->mutex);
403         INIT_LIST_HEAD(&con->out_queue);
404         INIT_LIST_HEAD(&con->out_sent);
405         INIT_DELAYED_WORK(&con->work, con_work);
406 }
407
408
409 /*
410  * We maintain a global counter to order connection attempts.  Get
411  * a unique seq greater than @gt.
412  */
413 static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
414 {
415         u32 ret;
416
417         spin_lock(&msgr->global_seq_lock);
418         if (msgr->global_seq < gt)
419                 msgr->global_seq = gt;
420         ret = ++msgr->global_seq;
421         spin_unlock(&msgr->global_seq_lock);
422         return ret;
423 }
424
425
426 /*
427  * Prepare footer for currently outgoing message, and finish things
428  * off.  Assumes out_kvec* are already valid.. we just add on to the end.
429  */
430 static void prepare_write_message_footer(struct ceph_connection *con, int v)
431 {
432         struct ceph_msg *m = con->out_msg;
433
434         dout("prepare_write_message_footer %p\n", con);
435         con->out_kvec_is_msg = true;
436         con->out_kvec[v].iov_base = &m->footer;
437         con->out_kvec[v].iov_len = sizeof(m->footer);
438         con->out_kvec_bytes += sizeof(m->footer);
439         con->out_kvec_left++;
440         con->out_more = m->more_to_follow;
441         con->out_msg_done = true;
442 }
443
444 /*
445  * Prepare headers for the next outgoing message.
446  */
447 static void prepare_write_message(struct ceph_connection *con)
448 {
449         struct ceph_msg *m;
450         int v = 0;
451
452         con->out_kvec_bytes = 0;
453         con->out_kvec_is_msg = true;
454         con->out_msg_done = false;
455
456         /* Sneak an ack in there first?  If we can get it into the same
457          * TCP packet that's a good thing. */
458         if (con->in_seq > con->in_seq_acked) {
459                 con->in_seq_acked = con->in_seq;
460                 con->out_kvec[v].iov_base = &tag_ack;
461                 con->out_kvec[v++].iov_len = 1;
462                 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
463                 con->out_kvec[v].iov_base = &con->out_temp_ack;
464                 con->out_kvec[v++].iov_len = sizeof(con->out_temp_ack);
465                 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
466         }
467
468         m = list_first_entry(&con->out_queue,
469                        struct ceph_msg, list_head);
470         con->out_msg = m;
471         if (test_bit(LOSSYTX, &con->state)) {
472                 list_del_init(&m->list_head);
473         } else {
474                 /* put message on sent list */
475                 ceph_msg_get(m);
476                 list_move_tail(&m->list_head, &con->out_sent);
477         }
478
479         /*
480          * only assign outgoing seq # if we haven't sent this message
481          * yet.  if it is requeued, resend with it's original seq.
482          */
483         if (m->needs_out_seq) {
484                 m->hdr.seq = cpu_to_le64(++con->out_seq);
485                 m->needs_out_seq = false;
486         }
487
488         dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
489              m, con->out_seq, le16_to_cpu(m->hdr.type),
490              le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
491              le32_to_cpu(m->hdr.data_len),
492              m->nr_pages);
493         BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
494
495         /* tag + hdr + front + middle */
496         con->out_kvec[v].iov_base = &tag_msg;
497         con->out_kvec[v++].iov_len = 1;
498         con->out_kvec[v].iov_base = &m->hdr;
499         con->out_kvec[v++].iov_len = sizeof(m->hdr);
500         con->out_kvec[v++] = m->front;
501         if (m->middle)
502                 con->out_kvec[v++] = m->middle->vec;
503         con->out_kvec_left = v;
504         con->out_kvec_bytes += 1 + sizeof(m->hdr) + m->front.iov_len +
505                 (m->middle ? m->middle->vec.iov_len : 0);
506         con->out_kvec_cur = con->out_kvec;
507
508         /* fill in crc (except data pages), footer */
509         con->out_msg->hdr.crc =
510                 cpu_to_le32(crc32c(0, (void *)&m->hdr,
511                                       sizeof(m->hdr) - sizeof(m->hdr.crc)));
512         con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
513         con->out_msg->footer.front_crc =
514                 cpu_to_le32(crc32c(0, m->front.iov_base, m->front.iov_len));
515         if (m->middle)
516                 con->out_msg->footer.middle_crc =
517                         cpu_to_le32(crc32c(0, m->middle->vec.iov_base,
518                                            m->middle->vec.iov_len));
519         else
520                 con->out_msg->footer.middle_crc = 0;
521         con->out_msg->footer.data_crc = 0;
522         dout("prepare_write_message front_crc %u data_crc %u\n",
523              le32_to_cpu(con->out_msg->footer.front_crc),
524              le32_to_cpu(con->out_msg->footer.middle_crc));
525
526         /* is there a data payload? */
527         if (le32_to_cpu(m->hdr.data_len) > 0) {
528                 /* initialize page iterator */
529                 con->out_msg_pos.page = 0;
530                 con->out_msg_pos.page_pos =
531                         le16_to_cpu(m->hdr.data_off) & ~PAGE_MASK;
532                 con->out_msg_pos.data_pos = 0;
533                 con->out_msg_pos.did_page_crc = 0;
534                 con->out_more = 1;  /* data + footer will follow */
535         } else {
536                 /* no, queue up footer too and be done */
537                 prepare_write_message_footer(con, v);
538         }
539
540         set_bit(WRITE_PENDING, &con->state);
541 }
542
543 /*
544  * Prepare an ack.
545  */
546 static void prepare_write_ack(struct ceph_connection *con)
547 {
548         dout("prepare_write_ack %p %llu -> %llu\n", con,
549              con->in_seq_acked, con->in_seq);
550         con->in_seq_acked = con->in_seq;
551
552         con->out_kvec[0].iov_base = &tag_ack;
553         con->out_kvec[0].iov_len = 1;
554         con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
555         con->out_kvec[1].iov_base = &con->out_temp_ack;
556         con->out_kvec[1].iov_len = sizeof(con->out_temp_ack);
557         con->out_kvec_left = 2;
558         con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
559         con->out_kvec_cur = con->out_kvec;
560         con->out_more = 1;  /* more will follow.. eventually.. */
561         set_bit(WRITE_PENDING, &con->state);
562 }
563
564 /*
565  * Prepare to write keepalive byte.
566  */
567 static void prepare_write_keepalive(struct ceph_connection *con)
568 {
569         dout("prepare_write_keepalive %p\n", con);
570         con->out_kvec[0].iov_base = &tag_keepalive;
571         con->out_kvec[0].iov_len = 1;
572         con->out_kvec_left = 1;
573         con->out_kvec_bytes = 1;
574         con->out_kvec_cur = con->out_kvec;
575         set_bit(WRITE_PENDING, &con->state);
576 }
577
578 /*
579  * Connection negotiation.
580  */
581
582 static void prepare_connect_authorizer(struct ceph_connection *con)
583 {
584         void *auth_buf;
585         int auth_len = 0;
586         int auth_protocol = 0;
587
588         mutex_unlock(&con->mutex);
589         if (con->ops->get_authorizer)
590                 con->ops->get_authorizer(con, &auth_buf, &auth_len,
591                                          &auth_protocol, &con->auth_reply_buf,
592                                          &con->auth_reply_buf_len,
593                                          con->auth_retry);
594         mutex_lock(&con->mutex);
595
596         con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol);
597         con->out_connect.authorizer_len = cpu_to_le32(auth_len);
598
599         con->out_kvec[con->out_kvec_left].iov_base = auth_buf;
600         con->out_kvec[con->out_kvec_left].iov_len = auth_len;
601         con->out_kvec_left++;
602         con->out_kvec_bytes += auth_len;
603 }
604
605 /*
606  * We connected to a peer and are saying hello.
607  */
608 static void prepare_write_banner(struct ceph_messenger *msgr,
609                                  struct ceph_connection *con)
610 {
611         int len = strlen(CEPH_BANNER);
612
613         con->out_kvec[0].iov_base = CEPH_BANNER;
614         con->out_kvec[0].iov_len = len;
615         con->out_kvec[1].iov_base = &msgr->my_enc_addr;
616         con->out_kvec[1].iov_len = sizeof(msgr->my_enc_addr);
617         con->out_kvec_left = 2;
618         con->out_kvec_bytes = len + sizeof(msgr->my_enc_addr);
619         con->out_kvec_cur = con->out_kvec;
620         con->out_more = 0;
621         set_bit(WRITE_PENDING, &con->state);
622 }
623
624 static void prepare_write_connect(struct ceph_messenger *msgr,
625                                   struct ceph_connection *con,
626                                   int after_banner)
627 {
628         unsigned global_seq = get_global_seq(con->msgr, 0);
629         int proto;
630
631         switch (con->peer_name.type) {
632         case CEPH_ENTITY_TYPE_MON:
633                 proto = CEPH_MONC_PROTOCOL;
634                 break;
635         case CEPH_ENTITY_TYPE_OSD:
636                 proto = CEPH_OSDC_PROTOCOL;
637                 break;
638         case CEPH_ENTITY_TYPE_MDS:
639                 proto = CEPH_MDSC_PROTOCOL;
640                 break;
641         default:
642                 BUG();
643         }
644
645         dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
646              con->connect_seq, global_seq, proto);
647
648         con->out_connect.features = cpu_to_le64(CEPH_FEATURE_SUPPORTED_CLIENT);
649         con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
650         con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
651         con->out_connect.global_seq = cpu_to_le32(global_seq);
652         con->out_connect.protocol_version = cpu_to_le32(proto);
653         con->out_connect.flags = 0;
654
655         if (!after_banner) {
656                 con->out_kvec_left = 0;
657                 con->out_kvec_bytes = 0;
658         }
659         con->out_kvec[con->out_kvec_left].iov_base = &con->out_connect;
660         con->out_kvec[con->out_kvec_left].iov_len = sizeof(con->out_connect);
661         con->out_kvec_left++;
662         con->out_kvec_bytes += sizeof(con->out_connect);
663         con->out_kvec_cur = con->out_kvec;
664         con->out_more = 0;
665         set_bit(WRITE_PENDING, &con->state);
666
667         prepare_connect_authorizer(con);
668 }
669
670
671 /*
672  * write as much of pending kvecs to the socket as we can.
673  *  1 -> done
674  *  0 -> socket full, but more to do
675  * <0 -> error
676  */
677 static int write_partial_kvec(struct ceph_connection *con)
678 {
679         int ret;
680
681         dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
682         while (con->out_kvec_bytes > 0) {
683                 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
684                                        con->out_kvec_left, con->out_kvec_bytes,
685                                        con->out_more);
686                 if (ret <= 0)
687                         goto out;
688                 con->out_kvec_bytes -= ret;
689                 if (con->out_kvec_bytes == 0)
690                         break;            /* done */
691                 while (ret > 0) {
692                         if (ret >= con->out_kvec_cur->iov_len) {
693                                 ret -= con->out_kvec_cur->iov_len;
694                                 con->out_kvec_cur++;
695                                 con->out_kvec_left--;
696                         } else {
697                                 con->out_kvec_cur->iov_len -= ret;
698                                 con->out_kvec_cur->iov_base += ret;
699                                 ret = 0;
700                                 break;
701                         }
702                 }
703         }
704         con->out_kvec_left = 0;
705         con->out_kvec_is_msg = false;
706         ret = 1;
707 out:
708         dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
709              con->out_kvec_bytes, con->out_kvec_left, ret);
710         return ret;  /* done! */
711 }
712
713 /*
714  * Write as much message data payload as we can.  If we finish, queue
715  * up the footer.
716  *  1 -> done, footer is now queued in out_kvec[].
717  *  0 -> socket full, but more to do
718  * <0 -> error
719  */
720 static int write_partial_msg_pages(struct ceph_connection *con)
721 {
722         struct ceph_msg *msg = con->out_msg;
723         unsigned data_len = le32_to_cpu(msg->hdr.data_len);
724         size_t len;
725         int crc = con->msgr->nocrc;
726         int ret;
727
728         dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
729              con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
730              con->out_msg_pos.page_pos);
731
732         while (con->out_msg_pos.page < con->out_msg->nr_pages) {
733                 struct page *page = NULL;
734                 void *kaddr = NULL;
735
736                 /*
737                  * if we are calculating the data crc (the default), we need
738                  * to map the page.  if our pages[] has been revoked, use the
739                  * zero page.
740                  */
741                 if (msg->pages) {
742                         page = msg->pages[con->out_msg_pos.page];
743                         if (crc)
744                                 kaddr = kmap(page);
745                 } else if (msg->pagelist) {
746                         page = list_first_entry(&msg->pagelist->head,
747                                                 struct page, lru);
748                         if (crc)
749                                 kaddr = kmap(page);
750                 } else {
751                         page = con->msgr->zero_page;
752                         if (crc)
753                                 kaddr = page_address(con->msgr->zero_page);
754                 }
755                 len = min((int)(PAGE_SIZE - con->out_msg_pos.page_pos),
756                           (int)(data_len - con->out_msg_pos.data_pos));
757                 if (crc && !con->out_msg_pos.did_page_crc) {
758                         void *base = kaddr + con->out_msg_pos.page_pos;
759                         u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
760
761                         BUG_ON(kaddr == NULL);
762                         con->out_msg->footer.data_crc =
763                                 cpu_to_le32(crc32c(tmpcrc, base, len));
764                         con->out_msg_pos.did_page_crc = 1;
765                 }
766
767                 ret = kernel_sendpage(con->sock, page,
768                                       con->out_msg_pos.page_pos, len,
769                                       MSG_DONTWAIT | MSG_NOSIGNAL |
770                                       MSG_MORE);
771
772                 if (crc && (msg->pages || msg->pagelist))
773                         kunmap(page);
774
775                 if (ret <= 0)
776                         goto out;
777
778                 con->out_msg_pos.data_pos += ret;
779                 con->out_msg_pos.page_pos += ret;
780                 if (ret == len) {
781                         con->out_msg_pos.page_pos = 0;
782                         con->out_msg_pos.page++;
783                         con->out_msg_pos.did_page_crc = 0;
784                         if (msg->pagelist)
785                                 list_move_tail(&page->lru,
786                                                &msg->pagelist->head);
787                 }
788         }
789
790         dout("write_partial_msg_pages %p msg %p done\n", con, msg);
791
792         /* prepare and queue up footer, too */
793         if (!crc)
794                 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
795         con->out_kvec_bytes = 0;
796         con->out_kvec_left = 0;
797         con->out_kvec_cur = con->out_kvec;
798         prepare_write_message_footer(con, 0);
799         ret = 1;
800 out:
801         return ret;
802 }
803
804 /*
805  * write some zeros
806  */
807 static int write_partial_skip(struct ceph_connection *con)
808 {
809         int ret;
810
811         while (con->out_skip > 0) {
812                 struct kvec iov = {
813                         .iov_base = page_address(con->msgr->zero_page),
814                         .iov_len = min(con->out_skip, (int)PAGE_CACHE_SIZE)
815                 };
816
817                 ret = ceph_tcp_sendmsg(con->sock, &iov, 1, iov.iov_len, 1);
818                 if (ret <= 0)
819                         goto out;
820                 con->out_skip -= ret;
821         }
822         ret = 1;
823 out:
824         return ret;
825 }
826
827 /*
828  * Prepare to read connection handshake, or an ack.
829  */
830 static void prepare_read_banner(struct ceph_connection *con)
831 {
832         dout("prepare_read_banner %p\n", con);
833         con->in_base_pos = 0;
834 }
835
836 static void prepare_read_connect(struct ceph_connection *con)
837 {
838         dout("prepare_read_connect %p\n", con);
839         con->in_base_pos = 0;
840 }
841
842 static void prepare_read_ack(struct ceph_connection *con)
843 {
844         dout("prepare_read_ack %p\n", con);
845         con->in_base_pos = 0;
846 }
847
848 static void prepare_read_tag(struct ceph_connection *con)
849 {
850         dout("prepare_read_tag %p\n", con);
851         con->in_base_pos = 0;
852         con->in_tag = CEPH_MSGR_TAG_READY;
853 }
854
855 /*
856  * Prepare to read a message.
857  */
858 static int prepare_read_message(struct ceph_connection *con)
859 {
860         dout("prepare_read_message %p\n", con);
861         BUG_ON(con->in_msg != NULL);
862         con->in_base_pos = 0;
863         con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
864         return 0;
865 }
866
867
868 static int read_partial(struct ceph_connection *con,
869                         int *to, int size, void *object)
870 {
871         *to += size;
872         while (con->in_base_pos < *to) {
873                 int left = *to - con->in_base_pos;
874                 int have = size - left;
875                 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
876                 if (ret <= 0)
877                         return ret;
878                 con->in_base_pos += ret;
879         }
880         return 1;
881 }
882
883
884 /*
885  * Read all or part of the connect-side handshake on a new connection
886  */
887 static int read_partial_banner(struct ceph_connection *con)
888 {
889         int ret, to = 0;
890
891         dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
892
893         /* peer's banner */
894         ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner);
895         if (ret <= 0)
896                 goto out;
897         ret = read_partial(con, &to, sizeof(con->actual_peer_addr),
898                            &con->actual_peer_addr);
899         if (ret <= 0)
900                 goto out;
901         ret = read_partial(con, &to, sizeof(con->peer_addr_for_me),
902                            &con->peer_addr_for_me);
903         if (ret <= 0)
904                 goto out;
905 out:
906         return ret;
907 }
908
909 static int read_partial_connect(struct ceph_connection *con)
910 {
911         int ret, to = 0;
912
913         dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
914
915         ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply);
916         if (ret <= 0)
917                 goto out;
918         ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len),
919                            con->auth_reply_buf);
920         if (ret <= 0)
921                 goto out;
922
923         dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
924              con, (int)con->in_reply.tag,
925              le32_to_cpu(con->in_reply.connect_seq),
926              le32_to_cpu(con->in_reply.global_seq));
927 out:
928         return ret;
929
930 }
931
932 /*
933  * Verify the hello banner looks okay.
934  */
935 static int verify_hello(struct ceph_connection *con)
936 {
937         if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
938                 pr_err("connect to %s got bad banner\n",
939                        pr_addr(&con->peer_addr.in_addr));
940                 con->error_msg = "protocol error, bad banner";
941                 return -1;
942         }
943         return 0;
944 }
945
946 static bool addr_is_blank(struct sockaddr_storage *ss)
947 {
948         switch (ss->ss_family) {
949         case AF_INET:
950                 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
951         case AF_INET6:
952                 return
953                      ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
954                      ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
955                      ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
956                      ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
957         }
958         return false;
959 }
960
961 static int addr_port(struct sockaddr_storage *ss)
962 {
963         switch (ss->ss_family) {
964         case AF_INET:
965                 return ntohs(((struct sockaddr_in *)ss)->sin_port);
966         case AF_INET6:
967                 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
968         }
969         return 0;
970 }
971
972 static void addr_set_port(struct sockaddr_storage *ss, int p)
973 {
974         switch (ss->ss_family) {
975         case AF_INET:
976                 ((struct sockaddr_in *)ss)->sin_port = htons(p);
977         case AF_INET6:
978                 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
979         }
980 }
981
982 /*
983  * Parse an ip[:port] list into an addr array.  Use the default
984  * monitor port if a port isn't specified.
985  */
986 int ceph_parse_ips(const char *c, const char *end,
987                    struct ceph_entity_addr *addr,
988                    int max_count, int *count)
989 {
990         int i;
991         const char *p = c;
992
993         dout("parse_ips on '%.*s'\n", (int)(end-c), c);
994         for (i = 0; i < max_count; i++) {
995                 const char *ipend;
996                 struct sockaddr_storage *ss = &addr[i].in_addr;
997                 struct sockaddr_in *in4 = (void *)ss;
998                 struct sockaddr_in6 *in6 = (void *)ss;
999                 int port;
1000
1001                 memset(ss, 0, sizeof(*ss));
1002                 if (in4_pton(p, end - p, (u8 *)&in4->sin_addr.s_addr,
1003                              ',', &ipend)) {
1004                         ss->ss_family = AF_INET;
1005                 } else if (in6_pton(p, end - p, (u8 *)&in6->sin6_addr.s6_addr,
1006                                     ',', &ipend)) {
1007                         ss->ss_family = AF_INET6;
1008                 } else {
1009                         goto bad;
1010                 }
1011                 p = ipend;
1012
1013                 /* port? */
1014                 if (p < end && *p == ':') {
1015                         port = 0;
1016                         p++;
1017                         while (p < end && *p >= '0' && *p <= '9') {
1018                                 port = (port * 10) + (*p - '0');
1019                                 p++;
1020                         }
1021                         if (port > 65535 || port == 0)
1022                                 goto bad;
1023                 } else {
1024                         port = CEPH_MON_PORT;
1025                 }
1026
1027                 addr_set_port(ss, port);
1028
1029                 dout("parse_ips got %s\n", pr_addr(ss));
1030
1031                 if (p == end)
1032                         break;
1033                 if (*p != ',')
1034                         goto bad;
1035                 p++;
1036         }
1037
1038         if (p != end)
1039                 goto bad;
1040
1041         if (count)
1042                 *count = i + 1;
1043         return 0;
1044
1045 bad:
1046         pr_err("parse_ips bad ip '%s'\n", c);
1047         return -EINVAL;
1048 }
1049
1050 static int process_banner(struct ceph_connection *con)
1051 {
1052         dout("process_banner on %p\n", con);
1053
1054         if (verify_hello(con) < 0)
1055                 return -1;
1056
1057         ceph_decode_addr(&con->actual_peer_addr);
1058         ceph_decode_addr(&con->peer_addr_for_me);
1059
1060         /*
1061          * Make sure the other end is who we wanted.  note that the other
1062          * end may not yet know their ip address, so if it's 0.0.0.0, give
1063          * them the benefit of the doubt.
1064          */
1065         if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1066                    sizeof(con->peer_addr)) != 0 &&
1067             !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1068               con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
1069                 pr_warning("wrong peer, want %s/%lld, got %s/%lld\n",
1070                            pr_addr(&con->peer_addr.in_addr),
1071                            le64_to_cpu(con->peer_addr.nonce),
1072                            pr_addr(&con->actual_peer_addr.in_addr),
1073                            le64_to_cpu(con->actual_peer_addr.nonce));
1074                 con->error_msg = "wrong peer at address";
1075                 return -1;
1076         }
1077
1078         /*
1079          * did we learn our address?
1080          */
1081         if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1082                 int port = addr_port(&con->msgr->inst.addr.in_addr);
1083
1084                 memcpy(&con->msgr->inst.addr.in_addr,
1085                        &con->peer_addr_for_me.in_addr,
1086                        sizeof(con->peer_addr_for_me.in_addr));
1087                 addr_set_port(&con->msgr->inst.addr.in_addr, port);
1088                 encode_my_addr(con->msgr);
1089                 dout("process_banner learned my addr is %s\n",
1090                      pr_addr(&con->msgr->inst.addr.in_addr));
1091         }
1092
1093         set_bit(NEGOTIATING, &con->state);
1094         prepare_read_connect(con);
1095         return 0;
1096 }
1097
1098 static void fail_protocol(struct ceph_connection *con)
1099 {
1100         reset_connection(con);
1101         set_bit(CLOSED, &con->state);  /* in case there's queued work */
1102
1103         mutex_unlock(&con->mutex);
1104         if (con->ops->bad_proto)
1105                 con->ops->bad_proto(con);
1106         mutex_lock(&con->mutex);
1107 }
1108
1109 static int process_connect(struct ceph_connection *con)
1110 {
1111         u64 sup_feat = CEPH_FEATURE_SUPPORTED_CLIENT;
1112         u64 req_feat = CEPH_FEATURE_REQUIRED_CLIENT;
1113         u64 server_feat = le64_to_cpu(con->in_reply.features);
1114
1115         dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1116
1117         switch (con->in_reply.tag) {
1118         case CEPH_MSGR_TAG_FEATURES:
1119                 pr_err("%s%lld %s feature set mismatch,"
1120                        " my %llx < server's %llx, missing %llx\n",
1121                        ENTITY_NAME(con->peer_name),
1122                        pr_addr(&con->peer_addr.in_addr),
1123                        sup_feat, server_feat, server_feat & ~sup_feat);
1124                 con->error_msg = "missing required protocol features";
1125                 fail_protocol(con);
1126                 return -1;
1127
1128         case CEPH_MSGR_TAG_BADPROTOVER:
1129                 pr_err("%s%lld %s protocol version mismatch,"
1130                        " my %d != server's %d\n",
1131                        ENTITY_NAME(con->peer_name),
1132                        pr_addr(&con->peer_addr.in_addr),
1133                        le32_to_cpu(con->out_connect.protocol_version),
1134                        le32_to_cpu(con->in_reply.protocol_version));
1135                 con->error_msg = "protocol version mismatch";
1136                 fail_protocol(con);
1137                 return -1;
1138
1139         case CEPH_MSGR_TAG_BADAUTHORIZER:
1140                 con->auth_retry++;
1141                 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1142                      con->auth_retry);
1143                 if (con->auth_retry == 2) {
1144                         con->error_msg = "connect authorization failure";
1145                         reset_connection(con);
1146                         set_bit(CLOSED, &con->state);
1147                         return -1;
1148                 }
1149                 con->auth_retry = 1;
1150                 prepare_write_connect(con->msgr, con, 0);
1151                 prepare_read_connect(con);
1152                 break;
1153
1154         case CEPH_MSGR_TAG_RESETSESSION:
1155                 /*
1156                  * If we connected with a large connect_seq but the peer
1157                  * has no record of a session with us (no connection, or
1158                  * connect_seq == 0), they will send RESETSESION to indicate
1159                  * that they must have reset their session, and may have
1160                  * dropped messages.
1161                  */
1162                 dout("process_connect got RESET peer seq %u\n",
1163                      le32_to_cpu(con->in_connect.connect_seq));
1164                 pr_err("%s%lld %s connection reset\n",
1165                        ENTITY_NAME(con->peer_name),
1166                        pr_addr(&con->peer_addr.in_addr));
1167                 reset_connection(con);
1168                 prepare_write_connect(con->msgr, con, 0);
1169                 prepare_read_connect(con);
1170
1171                 /* Tell ceph about it. */
1172                 mutex_unlock(&con->mutex);
1173                 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1174                 if (con->ops->peer_reset)
1175                         con->ops->peer_reset(con);
1176                 mutex_lock(&con->mutex);
1177                 break;
1178
1179         case CEPH_MSGR_TAG_RETRY_SESSION:
1180                 /*
1181                  * If we sent a smaller connect_seq than the peer has, try
1182                  * again with a larger value.
1183                  */
1184                 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1185                      le32_to_cpu(con->out_connect.connect_seq),
1186                      le32_to_cpu(con->in_connect.connect_seq));
1187                 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
1188                 prepare_write_connect(con->msgr, con, 0);
1189                 prepare_read_connect(con);
1190                 break;
1191
1192         case CEPH_MSGR_TAG_RETRY_GLOBAL:
1193                 /*
1194                  * If we sent a smaller global_seq than the peer has, try
1195                  * again with a larger value.
1196                  */
1197                 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
1198                      con->peer_global_seq,
1199                      le32_to_cpu(con->in_connect.global_seq));
1200                 get_global_seq(con->msgr,
1201                                le32_to_cpu(con->in_connect.global_seq));
1202                 prepare_write_connect(con->msgr, con, 0);
1203                 prepare_read_connect(con);
1204                 break;
1205
1206         case CEPH_MSGR_TAG_READY:
1207                 if (req_feat & ~server_feat) {
1208                         pr_err("%s%lld %s protocol feature mismatch,"
1209                                " my required %llx > server's %llx, need %llx\n",
1210                                ENTITY_NAME(con->peer_name),
1211                                pr_addr(&con->peer_addr.in_addr),
1212                                req_feat, server_feat, req_feat & ~server_feat);
1213                         con->error_msg = "missing required protocol features";
1214                         fail_protocol(con);
1215                         return -1;
1216                 }
1217                 clear_bit(CONNECTING, &con->state);
1218                 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1219                 con->connect_seq++;
1220                 con->peer_features = server_feat;
1221                 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1222                      con->peer_global_seq,
1223                      le32_to_cpu(con->in_reply.connect_seq),
1224                      con->connect_seq);
1225                 WARN_ON(con->connect_seq !=
1226                         le32_to_cpu(con->in_reply.connect_seq));
1227
1228                 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
1229                         set_bit(LOSSYTX, &con->state);
1230
1231                 prepare_read_tag(con);
1232                 break;
1233
1234         case CEPH_MSGR_TAG_WAIT:
1235                 /*
1236                  * If there is a connection race (we are opening
1237                  * connections to each other), one of us may just have
1238                  * to WAIT.  This shouldn't happen if we are the
1239                  * client.
1240                  */
1241                 pr_err("process_connect peer connecting WAIT\n");
1242
1243         default:
1244                 pr_err("connect protocol error, will retry\n");
1245                 con->error_msg = "protocol error, garbage tag during connect";
1246                 return -1;
1247         }
1248         return 0;
1249 }
1250
1251
1252 /*
1253  * read (part of) an ack
1254  */
1255 static int read_partial_ack(struct ceph_connection *con)
1256 {
1257         int to = 0;
1258
1259         return read_partial(con, &to, sizeof(con->in_temp_ack),
1260                             &con->in_temp_ack);
1261 }
1262
1263
1264 /*
1265  * We can finally discard anything that's been acked.
1266  */
1267 static void process_ack(struct ceph_connection *con)
1268 {
1269         struct ceph_msg *m;
1270         u64 ack = le64_to_cpu(con->in_temp_ack);
1271         u64 seq;
1272
1273         while (!list_empty(&con->out_sent)) {
1274                 m = list_first_entry(&con->out_sent, struct ceph_msg,
1275                                      list_head);
1276                 seq = le64_to_cpu(m->hdr.seq);
1277                 if (seq > ack)
1278                         break;
1279                 dout("got ack for seq %llu type %d at %p\n", seq,
1280                      le16_to_cpu(m->hdr.type), m);
1281                 ceph_msg_remove(m);
1282         }
1283         prepare_read_tag(con);
1284 }
1285
1286
1287
1288
1289 static int read_partial_message_section(struct ceph_connection *con,
1290                                         struct kvec *section, unsigned int sec_len,
1291                                         u32 *crc)
1292 {
1293         int left;
1294         int ret;
1295
1296         BUG_ON(!section);
1297
1298         while (section->iov_len < sec_len) {
1299                 BUG_ON(section->iov_base == NULL);
1300                 left = sec_len - section->iov_len;
1301                 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1302                                        section->iov_len, left);
1303                 if (ret <= 0)
1304                         return ret;
1305                 section->iov_len += ret;
1306                 if (section->iov_len == sec_len)
1307                         *crc = crc32c(0, section->iov_base,
1308                                       section->iov_len);
1309         }
1310
1311         return 1;
1312 }
1313
1314 static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
1315                                 struct ceph_msg_header *hdr,
1316                                 int *skip);
1317 /*
1318  * read (part of) a message.
1319  */
1320 static int read_partial_message(struct ceph_connection *con)
1321 {
1322         struct ceph_msg *m = con->in_msg;
1323         void *p;
1324         int ret;
1325         int to, left;
1326         unsigned front_len, middle_len, data_len, data_off;
1327         int datacrc = con->msgr->nocrc;
1328         int skip;
1329         u64 seq;
1330
1331         dout("read_partial_message con %p msg %p\n", con, m);
1332
1333         /* header */
1334         while (con->in_base_pos < sizeof(con->in_hdr)) {
1335                 left = sizeof(con->in_hdr) - con->in_base_pos;
1336                 ret = ceph_tcp_recvmsg(con->sock,
1337                                        (char *)&con->in_hdr + con->in_base_pos,
1338                                        left);
1339                 if (ret <= 0)
1340                         return ret;
1341                 con->in_base_pos += ret;
1342                 if (con->in_base_pos == sizeof(con->in_hdr)) {
1343                         u32 crc = crc32c(0, (void *)&con->in_hdr,
1344                                  sizeof(con->in_hdr) - sizeof(con->in_hdr.crc));
1345                         if (crc != le32_to_cpu(con->in_hdr.crc)) {
1346                                 pr_err("read_partial_message bad hdr "
1347                                        " crc %u != expected %u\n",
1348                                        crc, con->in_hdr.crc);
1349                                 return -EBADMSG;
1350                         }
1351                 }
1352         }
1353         front_len = le32_to_cpu(con->in_hdr.front_len);
1354         if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1355                 return -EIO;
1356         middle_len = le32_to_cpu(con->in_hdr.middle_len);
1357         if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1358                 return -EIO;
1359         data_len = le32_to_cpu(con->in_hdr.data_len);
1360         if (data_len > CEPH_MSG_MAX_DATA_LEN)
1361                 return -EIO;
1362         data_off = le16_to_cpu(con->in_hdr.data_off);
1363
1364         /* verify seq# */
1365         seq = le64_to_cpu(con->in_hdr.seq);
1366         if ((s64)seq - (s64)con->in_seq < 1) {
1367                 pr_info("skipping %s%lld %s seq %lld, expected %lld\n",
1368                         ENTITY_NAME(con->peer_name),
1369                         pr_addr(&con->peer_addr.in_addr),
1370                         seq, con->in_seq + 1);
1371                 con->in_base_pos = -front_len - middle_len - data_len -
1372                         sizeof(m->footer);
1373                 con->in_tag = CEPH_MSGR_TAG_READY;
1374                 con->in_seq++;
1375                 return 0;
1376         } else if ((s64)seq - (s64)con->in_seq > 1) {
1377                 pr_err("read_partial_message bad seq %lld expected %lld\n",
1378                        seq, con->in_seq + 1);
1379                 con->error_msg = "bad message sequence # for incoming message";
1380                 return -EBADMSG;
1381         }
1382
1383         /* allocate message? */
1384         if (!con->in_msg) {
1385                 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1386                      con->in_hdr.front_len, con->in_hdr.data_len);
1387                 skip = 0;
1388                 con->in_msg = ceph_alloc_msg(con, &con->in_hdr, &skip);
1389                 if (skip) {
1390                         /* skip this message */
1391                         dout("alloc_msg said skip message\n");
1392                         BUG_ON(con->in_msg);
1393                         con->in_base_pos = -front_len - middle_len - data_len -
1394                                 sizeof(m->footer);
1395                         con->in_tag = CEPH_MSGR_TAG_READY;
1396                         con->in_seq++;
1397                         return 0;
1398                 }
1399                 if (!con->in_msg) {
1400                         con->error_msg =
1401                                 "error allocating memory for incoming message";
1402                         return -ENOMEM;
1403                 }
1404                 m = con->in_msg;
1405                 m->front.iov_len = 0;    /* haven't read it yet */
1406                 if (m->middle)
1407                         m->middle->vec.iov_len = 0;
1408
1409                 con->in_msg_pos.page = 0;
1410                 con->in_msg_pos.page_pos = data_off & ~PAGE_MASK;
1411                 con->in_msg_pos.data_pos = 0;
1412         }
1413
1414         /* front */
1415         ret = read_partial_message_section(con, &m->front, front_len,
1416                                            &con->in_front_crc);
1417         if (ret <= 0)
1418                 return ret;
1419
1420         /* middle */
1421         if (m->middle) {
1422                 ret = read_partial_message_section(con, &m->middle->vec, middle_len,
1423                                                    &con->in_middle_crc);
1424                 if (ret <= 0)
1425                         return ret;
1426         }
1427
1428         /* (page) data */
1429         while (con->in_msg_pos.data_pos < data_len) {
1430                 left = min((int)(data_len - con->in_msg_pos.data_pos),
1431                            (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1432                 BUG_ON(m->pages == NULL);
1433                 p = kmap(m->pages[con->in_msg_pos.page]);
1434                 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1435                                        left);
1436                 if (ret > 0 && datacrc)
1437                         con->in_data_crc =
1438                                 crc32c(con->in_data_crc,
1439                                           p + con->in_msg_pos.page_pos, ret);
1440                 kunmap(m->pages[con->in_msg_pos.page]);
1441                 if (ret <= 0)
1442                         return ret;
1443                 con->in_msg_pos.data_pos += ret;
1444                 con->in_msg_pos.page_pos += ret;
1445                 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1446                         con->in_msg_pos.page_pos = 0;
1447                         con->in_msg_pos.page++;
1448                 }
1449         }
1450
1451         /* footer */
1452         to = sizeof(m->hdr) + sizeof(m->footer);
1453         while (con->in_base_pos < to) {
1454                 left = to - con->in_base_pos;
1455                 ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer +
1456                                        (con->in_base_pos - sizeof(m->hdr)),
1457                                        left);
1458                 if (ret <= 0)
1459                         return ret;
1460                 con->in_base_pos += ret;
1461         }
1462         dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1463              m, front_len, m->footer.front_crc, middle_len,
1464              m->footer.middle_crc, data_len, m->footer.data_crc);
1465
1466         /* crc ok? */
1467         if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1468                 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1469                        m, con->in_front_crc, m->footer.front_crc);
1470                 return -EBADMSG;
1471         }
1472         if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1473                 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1474                        m, con->in_middle_crc, m->footer.middle_crc);
1475                 return -EBADMSG;
1476         }
1477         if (datacrc &&
1478             (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1479             con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1480                 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1481                        con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1482                 return -EBADMSG;
1483         }
1484
1485         return 1; /* done! */
1486 }
1487
1488 /*
1489  * Process message.  This happens in the worker thread.  The callback should
1490  * be careful not to do anything that waits on other incoming messages or it
1491  * may deadlock.
1492  */
1493 static void process_message(struct ceph_connection *con)
1494 {
1495         struct ceph_msg *msg;
1496
1497         msg = con->in_msg;
1498         con->in_msg = NULL;
1499
1500         /* if first message, set peer_name */
1501         if (con->peer_name.type == 0)
1502                 con->peer_name = msg->hdr.src;
1503
1504         con->in_seq++;
1505         mutex_unlock(&con->mutex);
1506
1507         dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1508              msg, le64_to_cpu(msg->hdr.seq),
1509              ENTITY_NAME(msg->hdr.src),
1510              le16_to_cpu(msg->hdr.type),
1511              ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1512              le32_to_cpu(msg->hdr.front_len),
1513              le32_to_cpu(msg->hdr.data_len),
1514              con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1515         con->ops->dispatch(con, msg);
1516
1517         mutex_lock(&con->mutex);
1518         prepare_read_tag(con);
1519 }
1520
1521
1522 /*
1523  * Write something to the socket.  Called in a worker thread when the
1524  * socket appears to be writeable and we have something ready to send.
1525  */
1526 static int try_write(struct ceph_connection *con)
1527 {
1528         struct ceph_messenger *msgr = con->msgr;
1529         int ret = 1;
1530
1531         dout("try_write start %p state %lu nref %d\n", con, con->state,
1532              atomic_read(&con->nref));
1533
1534 more:
1535         dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1536
1537         /* open the socket first? */
1538         if (con->sock == NULL) {
1539                 /*
1540                  * if we were STANDBY and are reconnecting _this_
1541                  * connection, bump connect_seq now.  Always bump
1542                  * global_seq.
1543                  */
1544                 if (test_and_clear_bit(STANDBY, &con->state))
1545                         con->connect_seq++;
1546
1547                 prepare_write_banner(msgr, con);
1548                 prepare_write_connect(msgr, con, 1);
1549                 prepare_read_banner(con);
1550                 set_bit(CONNECTING, &con->state);
1551                 clear_bit(NEGOTIATING, &con->state);
1552
1553                 BUG_ON(con->in_msg);
1554                 con->in_tag = CEPH_MSGR_TAG_READY;
1555                 dout("try_write initiating connect on %p new state %lu\n",
1556                      con, con->state);
1557                 con->sock = ceph_tcp_connect(con);
1558                 if (IS_ERR(con->sock)) {
1559                         con->sock = NULL;
1560                         con->error_msg = "connect error";
1561                         ret = -1;
1562                         goto out;
1563                 }
1564         }
1565
1566 more_kvec:
1567         /* kvec data queued? */
1568         if (con->out_skip) {
1569                 ret = write_partial_skip(con);
1570                 if (ret <= 0)
1571                         goto done;
1572                 if (ret < 0) {
1573                         dout("try_write write_partial_skip err %d\n", ret);
1574                         goto done;
1575                 }
1576         }
1577         if (con->out_kvec_left) {
1578                 ret = write_partial_kvec(con);
1579                 if (ret <= 0)
1580                         goto done;
1581         }
1582
1583         /* msg pages? */
1584         if (con->out_msg) {
1585                 if (con->out_msg_done) {
1586                         ceph_msg_put(con->out_msg);
1587                         con->out_msg = NULL;   /* we're done with this one */
1588                         goto do_next;
1589                 }
1590
1591                 ret = write_partial_msg_pages(con);
1592                 if (ret == 1)
1593                         goto more_kvec;  /* we need to send the footer, too! */
1594                 if (ret == 0)
1595                         goto done;
1596                 if (ret < 0) {
1597                         dout("try_write write_partial_msg_pages err %d\n",
1598                              ret);
1599                         goto done;
1600                 }
1601         }
1602
1603 do_next:
1604         if (!test_bit(CONNECTING, &con->state)) {
1605                 /* is anything else pending? */
1606                 if (!list_empty(&con->out_queue)) {
1607                         prepare_write_message(con);
1608                         goto more;
1609                 }
1610                 if (con->in_seq > con->in_seq_acked) {
1611                         prepare_write_ack(con);
1612                         goto more;
1613                 }
1614                 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
1615                         prepare_write_keepalive(con);
1616                         goto more;
1617                 }
1618         }
1619
1620         /* Nothing to do! */
1621         clear_bit(WRITE_PENDING, &con->state);
1622         dout("try_write nothing else to write.\n");
1623 done:
1624         ret = 0;
1625 out:
1626         dout("try_write done on %p\n", con);
1627         return ret;
1628 }
1629
1630
1631
1632 /*
1633  * Read what we can from the socket.
1634  */
1635 static int try_read(struct ceph_connection *con)
1636 {
1637         int ret = -1;
1638
1639         if (!con->sock)
1640                 return 0;
1641
1642         if (test_bit(STANDBY, &con->state))
1643                 return 0;
1644
1645         dout("try_read start on %p\n", con);
1646
1647 more:
1648         dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
1649              con->in_base_pos);
1650         if (test_bit(CONNECTING, &con->state)) {
1651                 if (!test_bit(NEGOTIATING, &con->state)) {
1652                         dout("try_read connecting\n");
1653                         ret = read_partial_banner(con);
1654                         if (ret <= 0)
1655                                 goto done;
1656                         if (process_banner(con) < 0) {
1657                                 ret = -1;
1658                                 goto out;
1659                         }
1660                 }
1661                 ret = read_partial_connect(con);
1662                 if (ret <= 0)
1663                         goto done;
1664                 if (process_connect(con) < 0) {
1665                         ret = -1;
1666                         goto out;
1667                 }
1668                 goto more;
1669         }
1670
1671         if (con->in_base_pos < 0) {
1672                 /*
1673                  * skipping + discarding content.
1674                  *
1675                  * FIXME: there must be a better way to do this!
1676                  */
1677                 static char buf[1024];
1678                 int skip = min(1024, -con->in_base_pos);
1679                 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
1680                 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
1681                 if (ret <= 0)
1682                         goto done;
1683                 con->in_base_pos += ret;
1684                 if (con->in_base_pos)
1685                         goto more;
1686         }
1687         if (con->in_tag == CEPH_MSGR_TAG_READY) {
1688                 /*
1689                  * what's next?
1690                  */
1691                 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
1692                 if (ret <= 0)
1693                         goto done;
1694                 dout("try_read got tag %d\n", (int)con->in_tag);
1695                 switch (con->in_tag) {
1696                 case CEPH_MSGR_TAG_MSG:
1697                         prepare_read_message(con);
1698                         break;
1699                 case CEPH_MSGR_TAG_ACK:
1700                         prepare_read_ack(con);
1701                         break;
1702                 case CEPH_MSGR_TAG_CLOSE:
1703                         set_bit(CLOSED, &con->state);   /* fixme */
1704                         goto done;
1705                 default:
1706                         goto bad_tag;
1707                 }
1708         }
1709         if (con->in_tag == CEPH_MSGR_TAG_MSG) {
1710                 ret = read_partial_message(con);
1711                 if (ret <= 0) {
1712                         switch (ret) {
1713                         case -EBADMSG:
1714                                 con->error_msg = "bad crc";
1715                                 ret = -EIO;
1716                                 goto out;
1717                         case -EIO:
1718                                 con->error_msg = "io error";
1719                                 goto out;
1720                         default:
1721                                 goto done;
1722                         }
1723                 }
1724                 if (con->in_tag == CEPH_MSGR_TAG_READY)
1725                         goto more;
1726                 process_message(con);
1727                 goto more;
1728         }
1729         if (con->in_tag == CEPH_MSGR_TAG_ACK) {
1730                 ret = read_partial_ack(con);
1731                 if (ret <= 0)
1732                         goto done;
1733                 process_ack(con);
1734                 goto more;
1735         }
1736
1737 done:
1738         ret = 0;
1739 out:
1740         dout("try_read done on %p\n", con);
1741         return ret;
1742
1743 bad_tag:
1744         pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
1745         con->error_msg = "protocol error, garbage tag";
1746         ret = -1;
1747         goto out;
1748 }
1749
1750
1751 /*
1752  * Atomically queue work on a connection.  Bump @con reference to
1753  * avoid races with connection teardown.
1754  *
1755  * There is some trickery going on with QUEUED and BUSY because we
1756  * only want a _single_ thread operating on each connection at any
1757  * point in time, but we want to use all available CPUs.
1758  *
1759  * The worker thread only proceeds if it can atomically set BUSY.  It
1760  * clears QUEUED and does it's thing.  When it thinks it's done, it
1761  * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
1762  * (tries again to set BUSY).
1763  *
1764  * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
1765  * try to queue work.  If that fails (work is already queued, or BUSY)
1766  * we give up (work also already being done or is queued) but leave QUEUED
1767  * set so that the worker thread will loop if necessary.
1768  */
1769 static void queue_con(struct ceph_connection *con)
1770 {
1771         if (test_bit(DEAD, &con->state)) {
1772                 dout("queue_con %p ignoring: DEAD\n",
1773                      con);
1774                 return;
1775         }
1776
1777         if (!con->ops->get(con)) {
1778                 dout("queue_con %p ref count 0\n", con);
1779                 return;
1780         }
1781
1782         set_bit(QUEUED, &con->state);
1783         if (test_bit(BUSY, &con->state)) {
1784                 dout("queue_con %p - already BUSY\n", con);
1785                 con->ops->put(con);
1786         } else if (!queue_work(ceph_msgr_wq, &con->work.work)) {
1787                 dout("queue_con %p - already queued\n", con);
1788                 con->ops->put(con);
1789         } else {
1790                 dout("queue_con %p\n", con);
1791         }
1792 }
1793
1794 /*
1795  * Do some work on a connection.  Drop a connection ref when we're done.
1796  */
1797 static void con_work(struct work_struct *work)
1798 {
1799         struct ceph_connection *con = container_of(work, struct ceph_connection,
1800                                                    work.work);
1801         int backoff = 0;
1802
1803 more:
1804         if (test_and_set_bit(BUSY, &con->state) != 0) {
1805                 dout("con_work %p BUSY already set\n", con);
1806                 goto out;
1807         }
1808         dout("con_work %p start, clearing QUEUED\n", con);
1809         clear_bit(QUEUED, &con->state);
1810
1811         mutex_lock(&con->mutex);
1812
1813         if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
1814                 dout("con_work CLOSED\n");
1815                 con_close_socket(con);
1816                 goto done;
1817         }
1818         if (test_and_clear_bit(OPENING, &con->state)) {
1819                 /* reopen w/ new peer */
1820                 dout("con_work OPENING\n");
1821                 con_close_socket(con);
1822         }
1823
1824         if (test_and_clear_bit(SOCK_CLOSED, &con->state) ||
1825             try_read(con) < 0 ||
1826             try_write(con) < 0) {
1827                 mutex_unlock(&con->mutex);
1828                 backoff = 1;
1829                 ceph_fault(con);     /* error/fault path */
1830                 goto done_unlocked;
1831         }
1832
1833 done:
1834         mutex_unlock(&con->mutex);
1835
1836 done_unlocked:
1837         clear_bit(BUSY, &con->state);
1838         dout("con->state=%lu\n", con->state);
1839         if (test_bit(QUEUED, &con->state)) {
1840                 if (!backoff || test_bit(OPENING, &con->state)) {
1841                         dout("con_work %p QUEUED reset, looping\n", con);
1842                         goto more;
1843                 }
1844                 dout("con_work %p QUEUED reset, but just faulted\n", con);
1845                 clear_bit(QUEUED, &con->state);
1846         }
1847         dout("con_work %p done\n", con);
1848
1849 out:
1850         con->ops->put(con);
1851 }
1852
1853
1854 /*
1855  * Generic error/fault handler.  A retry mechanism is used with
1856  * exponential backoff
1857  */
1858 static void ceph_fault(struct ceph_connection *con)
1859 {
1860         pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
1861                pr_addr(&con->peer_addr.in_addr), con->error_msg);
1862         dout("fault %p state %lu to peer %s\n",
1863              con, con->state, pr_addr(&con->peer_addr.in_addr));
1864
1865         if (test_bit(LOSSYTX, &con->state)) {
1866                 dout("fault on LOSSYTX channel\n");
1867                 goto out;
1868         }
1869
1870         mutex_lock(&con->mutex);
1871         if (test_bit(CLOSED, &con->state))
1872                 goto out_unlock;
1873
1874         con_close_socket(con);
1875
1876         if (con->in_msg) {
1877                 ceph_msg_put(con->in_msg);
1878                 con->in_msg = NULL;
1879         }
1880
1881         /* Requeue anything that hasn't been acked */
1882         list_splice_init(&con->out_sent, &con->out_queue);
1883
1884         /* If there are no messages in the queue, place the connection
1885          * in a STANDBY state (i.e., don't try to reconnect just yet). */
1886         if (list_empty(&con->out_queue) && !con->out_keepalive_pending) {
1887                 dout("fault setting STANDBY\n");
1888                 set_bit(STANDBY, &con->state);
1889         } else {
1890                 /* retry after a delay. */
1891                 if (con->delay == 0)
1892                         con->delay = BASE_DELAY_INTERVAL;
1893                 else if (con->delay < MAX_DELAY_INTERVAL)
1894                         con->delay *= 2;
1895                 dout("fault queueing %p delay %lu\n", con, con->delay);
1896                 con->ops->get(con);
1897                 if (queue_delayed_work(ceph_msgr_wq, &con->work,
1898                                        round_jiffies_relative(con->delay)) == 0)
1899                         con->ops->put(con);
1900         }
1901
1902 out_unlock:
1903         mutex_unlock(&con->mutex);
1904 out:
1905         /*
1906          * in case we faulted due to authentication, invalidate our
1907          * current tickets so that we can get new ones.
1908          */
1909         if (con->auth_retry && con->ops->invalidate_authorizer) {
1910                 dout("calling invalidate_authorizer()\n");
1911                 con->ops->invalidate_authorizer(con);
1912         }
1913
1914         if (con->ops->fault)
1915                 con->ops->fault(con);
1916 }
1917
1918
1919
1920 /*
1921  * create a new messenger instance
1922  */
1923 struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr)
1924 {
1925         struct ceph_messenger *msgr;
1926
1927         msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
1928         if (msgr == NULL)
1929                 return ERR_PTR(-ENOMEM);
1930
1931         spin_lock_init(&msgr->global_seq_lock);
1932
1933         /* the zero page is needed if a request is "canceled" while the message
1934          * is being written over the socket */
1935         msgr->zero_page = __page_cache_alloc(GFP_KERNEL | __GFP_ZERO);
1936         if (!msgr->zero_page) {
1937                 kfree(msgr);
1938                 return ERR_PTR(-ENOMEM);
1939         }
1940         kmap(msgr->zero_page);
1941
1942         if (myaddr)
1943                 msgr->inst.addr = *myaddr;
1944
1945         /* select a random nonce */
1946         msgr->inst.addr.type = 0;
1947         get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
1948         encode_my_addr(msgr);
1949
1950         dout("messenger_create %p\n", msgr);
1951         return msgr;
1952 }
1953
1954 void ceph_messenger_destroy(struct ceph_messenger *msgr)
1955 {
1956         dout("destroy %p\n", msgr);
1957         kunmap(msgr->zero_page);
1958         __free_page(msgr->zero_page);
1959         kfree(msgr);
1960         dout("destroyed messenger %p\n", msgr);
1961 }
1962
1963 /*
1964  * Queue up an outgoing message on the given connection.
1965  */
1966 void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
1967 {
1968         if (test_bit(CLOSED, &con->state)) {
1969                 dout("con_send %p closed, dropping %p\n", con, msg);
1970                 ceph_msg_put(msg);
1971                 return;
1972         }
1973
1974         /* set src+dst */
1975         msg->hdr.src = con->msgr->inst.name;
1976
1977         BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
1978
1979         msg->needs_out_seq = true;
1980
1981         /* queue */
1982         mutex_lock(&con->mutex);
1983         BUG_ON(!list_empty(&msg->list_head));
1984         list_add_tail(&msg->list_head, &con->out_queue);
1985         dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
1986              ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
1987              ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1988              le32_to_cpu(msg->hdr.front_len),
1989              le32_to_cpu(msg->hdr.middle_len),
1990              le32_to_cpu(msg->hdr.data_len));
1991         mutex_unlock(&con->mutex);
1992
1993         /* if there wasn't anything waiting to send before, queue
1994          * new work */
1995         if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
1996                 queue_con(con);
1997 }
1998
1999 /*
2000  * Revoke a message that was previously queued for send
2001  */
2002 void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
2003 {
2004         mutex_lock(&con->mutex);
2005         if (!list_empty(&msg->list_head)) {
2006                 dout("con_revoke %p msg %p - was on queue\n", con, msg);
2007                 list_del_init(&msg->list_head);
2008                 ceph_msg_put(msg);
2009                 msg->hdr.seq = 0;
2010         }
2011         if (con->out_msg == msg) {
2012                 dout("con_revoke %p msg %p - was sending\n", con, msg);
2013                 con->out_msg = NULL;
2014                 if (con->out_kvec_is_msg) {
2015                         con->out_skip = con->out_kvec_bytes;
2016                         con->out_kvec_is_msg = false;
2017                 }
2018                 ceph_msg_put(msg);
2019                 msg->hdr.seq = 0;
2020         }
2021         mutex_unlock(&con->mutex);
2022 }
2023
2024 /*
2025  * Revoke a message that we may be reading data into
2026  */
2027 void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
2028 {
2029         mutex_lock(&con->mutex);
2030         if (con->in_msg && con->in_msg == msg) {
2031                 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2032                 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
2033                 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2034
2035                 /* skip rest of message */
2036                 dout("con_revoke_pages %p msg %p revoked\n", con, msg);
2037                         con->in_base_pos = con->in_base_pos -
2038                                 sizeof(struct ceph_msg_header) -
2039                                 front_len -
2040                                 middle_len -
2041                                 data_len -
2042                                 sizeof(struct ceph_msg_footer);
2043                 ceph_msg_put(con->in_msg);
2044                 con->in_msg = NULL;
2045                 con->in_tag = CEPH_MSGR_TAG_READY;
2046                 con->in_seq++;
2047         } else {
2048                 dout("con_revoke_pages %p msg %p pages %p no-op\n",
2049                      con, con->in_msg, msg);
2050         }
2051         mutex_unlock(&con->mutex);
2052 }
2053
2054 /*
2055  * Queue a keepalive byte to ensure the tcp connection is alive.
2056  */
2057 void ceph_con_keepalive(struct ceph_connection *con)
2058 {
2059         if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
2060             test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2061                 queue_con(con);
2062 }
2063
2064
2065 /*
2066  * construct a new message with given type, size
2067  * the new msg has a ref count of 1.
2068  */
2069 struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags)
2070 {
2071         struct ceph_msg *m;
2072
2073         m = kmalloc(sizeof(*m), flags);
2074         if (m == NULL)
2075                 goto out;
2076         kref_init(&m->kref);
2077         INIT_LIST_HEAD(&m->list_head);
2078
2079         m->hdr.tid = 0;
2080         m->hdr.type = cpu_to_le16(type);
2081         m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2082         m->hdr.version = 0;
2083         m->hdr.front_len = cpu_to_le32(front_len);
2084         m->hdr.middle_len = 0;
2085         m->hdr.data_len = 0;
2086         m->hdr.data_off = 0;
2087         m->hdr.reserved = 0;
2088         m->footer.front_crc = 0;
2089         m->footer.middle_crc = 0;
2090         m->footer.data_crc = 0;
2091         m->footer.flags = 0;
2092         m->front_max = front_len;
2093         m->front_is_vmalloc = false;
2094         m->more_to_follow = false;
2095         m->pool = NULL;
2096
2097         /* front */
2098         if (front_len) {
2099                 if (front_len > PAGE_CACHE_SIZE) {
2100                         m->front.iov_base = __vmalloc(front_len, flags,
2101                                                       PAGE_KERNEL);
2102                         m->front_is_vmalloc = true;
2103                 } else {
2104                         m->front.iov_base = kmalloc(front_len, flags);
2105                 }
2106                 if (m->front.iov_base == NULL) {
2107                         pr_err("msg_new can't allocate %d bytes\n",
2108                              front_len);
2109                         goto out2;
2110                 }
2111         } else {
2112                 m->front.iov_base = NULL;
2113         }
2114         m->front.iov_len = front_len;
2115
2116         /* middle */
2117         m->middle = NULL;
2118
2119         /* data */
2120         m->nr_pages = 0;
2121         m->pages = NULL;
2122         m->pagelist = NULL;
2123
2124         dout("ceph_msg_new %p front %d\n", m, front_len);
2125         return m;
2126
2127 out2:
2128         ceph_msg_put(m);
2129 out:
2130         pr_err("msg_new can't create type %d front %d\n", type, front_len);
2131         return NULL;
2132 }
2133
2134 /*
2135  * Allocate "middle" portion of a message, if it is needed and wasn't
2136  * allocated by alloc_msg.  This allows us to read a small fixed-size
2137  * per-type header in the front and then gracefully fail (i.e.,
2138  * propagate the error to the caller based on info in the front) when
2139  * the middle is too large.
2140  */
2141 static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
2142 {
2143         int type = le16_to_cpu(msg->hdr.type);
2144         int middle_len = le32_to_cpu(msg->hdr.middle_len);
2145
2146         dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2147              ceph_msg_type_name(type), middle_len);
2148         BUG_ON(!middle_len);
2149         BUG_ON(msg->middle);
2150
2151         msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
2152         if (!msg->middle)
2153                 return -ENOMEM;
2154         return 0;
2155 }
2156
2157 /*
2158  * Generic message allocator, for incoming messages.
2159  */
2160 static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
2161                                 struct ceph_msg_header *hdr,
2162                                 int *skip)
2163 {
2164         int type = le16_to_cpu(hdr->type);
2165         int front_len = le32_to_cpu(hdr->front_len);
2166         int middle_len = le32_to_cpu(hdr->middle_len);
2167         struct ceph_msg *msg = NULL;
2168         int ret;
2169
2170         if (con->ops->alloc_msg) {
2171                 mutex_unlock(&con->mutex);
2172                 msg = con->ops->alloc_msg(con, hdr, skip);
2173                 mutex_lock(&con->mutex);
2174                 if (!msg || *skip)
2175                         return NULL;
2176         }
2177         if (!msg) {
2178                 *skip = 0;
2179                 msg = ceph_msg_new(type, front_len, GFP_NOFS);
2180                 if (!msg) {
2181                         pr_err("unable to allocate msg type %d len %d\n",
2182                                type, front_len);
2183                         return NULL;
2184                 }
2185         }
2186         memcpy(&msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
2187
2188         if (middle_len && !msg->middle) {
2189                 ret = ceph_alloc_middle(con, msg);
2190                 if (ret < 0) {
2191                         ceph_msg_put(msg);
2192                         return NULL;
2193                 }
2194         }
2195
2196         return msg;
2197 }
2198
2199
2200 /*
2201  * Free a generically kmalloc'd message.
2202  */
2203 void ceph_msg_kfree(struct ceph_msg *m)
2204 {
2205         dout("msg_kfree %p\n", m);
2206         if (m->front_is_vmalloc)
2207                 vfree(m->front.iov_base);
2208         else
2209                 kfree(m->front.iov_base);
2210         kfree(m);
2211 }
2212
2213 /*
2214  * Drop a msg ref.  Destroy as needed.
2215  */
2216 void ceph_msg_last_put(struct kref *kref)
2217 {
2218         struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
2219
2220         dout("ceph_msg_put last one on %p\n", m);
2221         WARN_ON(!list_empty(&m->list_head));
2222
2223         /* drop middle, data, if any */
2224         if (m->middle) {
2225                 ceph_buffer_put(m->middle);
2226                 m->middle = NULL;
2227         }
2228         m->nr_pages = 0;
2229         m->pages = NULL;
2230
2231         if (m->pagelist) {
2232                 ceph_pagelist_release(m->pagelist);
2233                 kfree(m->pagelist);
2234                 m->pagelist = NULL;
2235         }
2236
2237         if (m->pool)
2238                 ceph_msgpool_put(m->pool, m);
2239         else
2240                 ceph_msg_kfree(m);
2241 }
2242
2243 void ceph_msg_dump(struct ceph_msg *msg)
2244 {
2245         pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2246                  msg->front_max, msg->nr_pages);
2247         print_hex_dump(KERN_DEBUG, "header: ",
2248                        DUMP_PREFIX_OFFSET, 16, 1,
2249                        &msg->hdr, sizeof(msg->hdr), true);
2250         print_hex_dump(KERN_DEBUG, " front: ",
2251                        DUMP_PREFIX_OFFSET, 16, 1,
2252                        msg->front.iov_base, msg->front.iov_len, true);
2253         if (msg->middle)
2254                 print_hex_dump(KERN_DEBUG, "middle: ",
2255                                DUMP_PREFIX_OFFSET, 16, 1,
2256                                msg->middle->vec.iov_base,
2257                                msg->middle->vec.iov_len, true);
2258         print_hex_dump(KERN_DEBUG, "footer: ",
2259                        DUMP_PREFIX_OFFSET, 16, 1,
2260                        &msg->footer, sizeof(msg->footer), true);
2261 }