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