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