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