blob: bb16edb1aef603765a28924c20b16fea8a21fb99 [file] [log] [blame]
Sage Weil31b80062009-10-06 11:31:13 -07001#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Sage Weil31b80062009-10-06 11:31:13 -070010#include <linux/socket.h>
11#include <linux/string.h>
12#include <net/tcp.h>
13
14#include "super.h"
15#include "messenger.h"
Sage Weil63f2d212009-11-03 15:17:56 -080016#include "decode.h"
Sage Weil58bb3b32009-12-23 12:12:31 -080017#include "pagelist.h"
Sage Weil31b80062009-10-06 11:31:13 -070018
19/*
20 * Ceph uses the messenger to exchange ceph_msg messages with other
21 * hosts in the system. The messenger provides ordered and reliable
22 * delivery. We tolerate TCP disconnects by reconnecting (with
23 * exponential backoff) in the case of a fault (disconnection, bad
24 * crc, protocol error). Acks allow sent messages to be discarded by
25 * the sender.
26 */
27
28/* static tag bytes (protocol control messages) */
29static char tag_msg = CEPH_MSGR_TAG_MSG;
30static char tag_ack = CEPH_MSGR_TAG_ACK;
31static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
32
Sage Weila6a53492010-04-13 14:07:07 -070033#ifdef CONFIG_LOCKDEP
34static struct lock_class_key socket_class;
35#endif
36
Sage Weil31b80062009-10-06 11:31:13 -070037
38static void queue_con(struct ceph_connection *con);
39static void con_work(struct work_struct *);
40static void ceph_fault(struct ceph_connection *con);
41
42const char *ceph_name_type_str(int t)
43{
44 switch (t) {
45 case CEPH_ENTITY_TYPE_MON: return "mon";
46 case CEPH_ENTITY_TYPE_MDS: return "mds";
47 case CEPH_ENTITY_TYPE_OSD: return "osd";
48 case CEPH_ENTITY_TYPE_CLIENT: return "client";
49 case CEPH_ENTITY_TYPE_ADMIN: return "admin";
50 default: return "???";
51 }
52}
53
54/*
55 * nicely render a sockaddr as a string.
56 */
57#define MAX_ADDR_STR 20
58static char addr_str[MAX_ADDR_STR][40];
59static DEFINE_SPINLOCK(addr_str_lock);
60static int last_addr_str;
61
62const char *pr_addr(const struct sockaddr_storage *ss)
63{
64 int i;
65 char *s;
66 struct sockaddr_in *in4 = (void *)ss;
67 unsigned char *quad = (void *)&in4->sin_addr.s_addr;
68 struct sockaddr_in6 *in6 = (void *)ss;
69
70 spin_lock(&addr_str_lock);
71 i = last_addr_str++;
72 if (last_addr_str == MAX_ADDR_STR)
73 last_addr_str = 0;
74 spin_unlock(&addr_str_lock);
75 s = addr_str[i];
76
77 switch (ss->ss_family) {
78 case AF_INET:
79 sprintf(s, "%u.%u.%u.%u:%u",
80 (unsigned int)quad[0],
81 (unsigned int)quad[1],
82 (unsigned int)quad[2],
83 (unsigned int)quad[3],
84 (unsigned int)ntohs(in4->sin_port));
85 break;
86
87 case AF_INET6:
88 sprintf(s, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%u",
89 in6->sin6_addr.s6_addr16[0],
90 in6->sin6_addr.s6_addr16[1],
91 in6->sin6_addr.s6_addr16[2],
92 in6->sin6_addr.s6_addr16[3],
93 in6->sin6_addr.s6_addr16[4],
94 in6->sin6_addr.s6_addr16[5],
95 in6->sin6_addr.s6_addr16[6],
96 in6->sin6_addr.s6_addr16[7],
97 (unsigned int)ntohs(in6->sin6_port));
98 break;
99
100 default:
101 sprintf(s, "(unknown sockaddr family %d)", (int)ss->ss_family);
102 }
103
104 return s;
105}
106
Sage Weil63f2d212009-11-03 15:17:56 -0800107static void encode_my_addr(struct ceph_messenger *msgr)
108{
109 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
110 ceph_encode_addr(&msgr->my_enc_addr);
111}
112
Sage Weil31b80062009-10-06 11:31:13 -0700113/*
114 * work queue for all reading and writing to/from the socket.
115 */
116struct workqueue_struct *ceph_msgr_wq;
117
118int __init ceph_msgr_init(void)
119{
120 ceph_msgr_wq = create_workqueue("ceph-msgr");
121 if (IS_ERR(ceph_msgr_wq)) {
122 int ret = PTR_ERR(ceph_msgr_wq);
123 pr_err("msgr_init failed to create workqueue: %d\n", ret);
124 ceph_msgr_wq = NULL;
125 return ret;
126 }
127 return 0;
128}
129
130void ceph_msgr_exit(void)
131{
132 destroy_workqueue(ceph_msgr_wq);
133}
134
135/*
136 * socket callback functions
137 */
138
139/* data available on socket, or listen socket received a connect */
140static void ceph_data_ready(struct sock *sk, int count_unused)
141{
142 struct ceph_connection *con =
143 (struct ceph_connection *)sk->sk_user_data;
144 if (sk->sk_state != TCP_CLOSE_WAIT) {
145 dout("ceph_data_ready on %p state = %lu, queueing work\n",
146 con, con->state);
147 queue_con(con);
148 }
149}
150
151/* socket has buffer space for writing */
152static void ceph_write_space(struct sock *sk)
153{
154 struct ceph_connection *con =
155 (struct ceph_connection *)sk->sk_user_data;
156
157 /* only queue to workqueue if there is data we want to write. */
158 if (test_bit(WRITE_PENDING, &con->state)) {
159 dout("ceph_write_space %p queueing write work\n", con);
160 queue_con(con);
161 } else {
162 dout("ceph_write_space %p nothing to write\n", con);
163 }
164
165 /* since we have our own write_space, clear the SOCK_NOSPACE flag */
166 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
167}
168
169/* socket's state has changed */
170static void ceph_state_change(struct sock *sk)
171{
172 struct ceph_connection *con =
173 (struct ceph_connection *)sk->sk_user_data;
174
175 dout("ceph_state_change %p state = %lu sk_state = %u\n",
176 con, con->state, sk->sk_state);
177
178 if (test_bit(CLOSED, &con->state))
179 return;
180
181 switch (sk->sk_state) {
182 case TCP_CLOSE:
183 dout("ceph_state_change TCP_CLOSE\n");
184 case TCP_CLOSE_WAIT:
185 dout("ceph_state_change TCP_CLOSE_WAIT\n");
186 if (test_and_set_bit(SOCK_CLOSED, &con->state) == 0) {
187 if (test_bit(CONNECTING, &con->state))
188 con->error_msg = "connection failed";
189 else
190 con->error_msg = "socket closed";
191 queue_con(con);
192 }
193 break;
194 case TCP_ESTABLISHED:
195 dout("ceph_state_change TCP_ESTABLISHED\n");
196 queue_con(con);
197 break;
198 }
199}
200
201/*
202 * set up socket callbacks
203 */
204static void set_sock_callbacks(struct socket *sock,
205 struct ceph_connection *con)
206{
207 struct sock *sk = sock->sk;
208 sk->sk_user_data = (void *)con;
209 sk->sk_data_ready = ceph_data_ready;
210 sk->sk_write_space = ceph_write_space;
211 sk->sk_state_change = ceph_state_change;
212}
213
214
215/*
216 * socket helpers
217 */
218
219/*
220 * initiate connection to a remote socket.
221 */
222static struct socket *ceph_tcp_connect(struct ceph_connection *con)
223{
224 struct sockaddr *paddr = (struct sockaddr *)&con->peer_addr.in_addr;
225 struct socket *sock;
226 int ret;
227
228 BUG_ON(con->sock);
229 ret = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
230 if (ret)
231 return ERR_PTR(ret);
232 con->sock = sock;
233 sock->sk->sk_allocation = GFP_NOFS;
234
Sage Weila6a53492010-04-13 14:07:07 -0700235#ifdef CONFIG_LOCKDEP
236 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
237#endif
238
Sage Weil31b80062009-10-06 11:31:13 -0700239 set_sock_callbacks(sock, con);
240
241 dout("connect %s\n", pr_addr(&con->peer_addr.in_addr));
242
243 ret = sock->ops->connect(sock, paddr, sizeof(*paddr), O_NONBLOCK);
244 if (ret == -EINPROGRESS) {
245 dout("connect %s EINPROGRESS sk_state = %u\n",
246 pr_addr(&con->peer_addr.in_addr),
247 sock->sk->sk_state);
248 ret = 0;
249 }
250 if (ret < 0) {
251 pr_err("connect %s error %d\n",
252 pr_addr(&con->peer_addr.in_addr), ret);
253 sock_release(sock);
254 con->sock = NULL;
255 con->error_msg = "connect error";
256 }
257
258 if (ret < 0)
259 return ERR_PTR(ret);
260 return sock;
261}
262
263static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
264{
265 struct kvec iov = {buf, len};
266 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
267
268 return kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
269}
270
271/*
272 * write something. @more is true if caller will be sending more data
273 * shortly.
274 */
275static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
276 size_t kvlen, size_t len, int more)
277{
278 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
279
280 if (more)
281 msg.msg_flags |= MSG_MORE;
282 else
283 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
284
285 return kernel_sendmsg(sock, &msg, iov, kvlen, len);
286}
287
288
289/*
290 * Shutdown/close the socket for the given connection.
291 */
292static int con_close_socket(struct ceph_connection *con)
293{
294 int rc;
295
296 dout("con_close_socket on %p sock %p\n", con, con->sock);
297 if (!con->sock)
298 return 0;
299 set_bit(SOCK_CLOSED, &con->state);
300 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
301 sock_release(con->sock);
302 con->sock = NULL;
303 clear_bit(SOCK_CLOSED, &con->state);
304 return rc;
305}
306
307/*
308 * Reset a connection. Discard all incoming and outgoing messages
309 * and clear *_seq state.
310 */
311static void ceph_msg_remove(struct ceph_msg *msg)
312{
313 list_del_init(&msg->list_head);
314 ceph_msg_put(msg);
315}
316static void ceph_msg_remove_list(struct list_head *head)
317{
318 while (!list_empty(head)) {
319 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
320 list_head);
321 ceph_msg_remove(msg);
322 }
323}
324
325static void reset_connection(struct ceph_connection *con)
326{
327 /* reset connection, out_queue, msg_ and connect_seq */
328 /* discard existing out_queue and msg_seq */
Sage Weil31b80062009-10-06 11:31:13 -0700329 ceph_msg_remove_list(&con->out_queue);
330 ceph_msg_remove_list(&con->out_sent);
331
Sage Weilcf3e5c42009-12-11 09:48:05 -0800332 if (con->in_msg) {
333 ceph_msg_put(con->in_msg);
334 con->in_msg = NULL;
335 }
336
Sage Weil31b80062009-10-06 11:31:13 -0700337 con->connect_seq = 0;
338 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800339 if (con->out_msg) {
340 ceph_msg_put(con->out_msg);
341 con->out_msg = NULL;
342 }
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700343 con->out_keepalive_pending = false;
Sage Weil31b80062009-10-06 11:31:13 -0700344 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700345 con->in_seq_acked = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700346}
347
348/*
349 * mark a peer down. drop any open connections.
350 */
351void ceph_con_close(struct ceph_connection *con)
352{
353 dout("con_close %p peer %s\n", con, pr_addr(&con->peer_addr.in_addr));
354 set_bit(CLOSED, &con->state); /* in case there's queued work */
355 clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */
Sage Weil1679f872010-02-26 13:55:51 -0800356 clear_bit(LOSSYTX, &con->state); /* so we retry next connect */
357 clear_bit(KEEPALIVE_PENDING, &con->state);
358 clear_bit(WRITE_PENDING, &con->state);
Sage Weilec302642009-12-22 10:43:42 -0800359 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700360 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700361 con->peer_global_seq = 0;
Sage Weil91e45ce2010-02-15 12:05:09 -0800362 cancel_delayed_work(&con->work);
Sage Weilec302642009-12-22 10:43:42 -0800363 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700364 queue_con(con);
365}
366
367/*
Sage Weil31b80062009-10-06 11:31:13 -0700368 * Reopen a closed connection, with a new peer address.
369 */
370void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr)
371{
372 dout("con_open %p %s\n", con, pr_addr(&addr->in_addr));
373 set_bit(OPENING, &con->state);
374 clear_bit(CLOSED, &con->state);
375 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800376 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -0700377 queue_con(con);
378}
379
380/*
Sage Weil87b315a2010-03-22 14:51:18 -0700381 * return true if this connection ever successfully opened
382 */
383bool ceph_con_opened(struct ceph_connection *con)
384{
385 return con->connect_seq > 0;
386}
387
388/*
Sage Weil31b80062009-10-06 11:31:13 -0700389 * generic get/put
390 */
391struct ceph_connection *ceph_con_get(struct ceph_connection *con)
392{
393 dout("con_get %p nref = %d -> %d\n", con,
394 atomic_read(&con->nref), atomic_read(&con->nref) + 1);
395 if (atomic_inc_not_zero(&con->nref))
396 return con;
397 return NULL;
398}
399
400void ceph_con_put(struct ceph_connection *con)
401{
402 dout("con_put %p nref = %d -> %d\n", con,
403 atomic_read(&con->nref), atomic_read(&con->nref) - 1);
404 BUG_ON(atomic_read(&con->nref) == 0);
405 if (atomic_dec_and_test(&con->nref)) {
Sage Weil71ececd2009-11-18 11:27:06 -0800406 BUG_ON(con->sock);
Sage Weil31b80062009-10-06 11:31:13 -0700407 kfree(con);
408 }
409}
410
411/*
412 * initialize a new connection.
413 */
414void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con)
415{
416 dout("con_init %p\n", con);
417 memset(con, 0, sizeof(*con));
418 atomic_set(&con->nref, 1);
419 con->msgr = msgr;
Sage Weilec302642009-12-22 10:43:42 -0800420 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700421 INIT_LIST_HEAD(&con->out_queue);
422 INIT_LIST_HEAD(&con->out_sent);
423 INIT_DELAYED_WORK(&con->work, con_work);
424}
425
426
427/*
428 * We maintain a global counter to order connection attempts. Get
429 * a unique seq greater than @gt.
430 */
431static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
432{
433 u32 ret;
434
435 spin_lock(&msgr->global_seq_lock);
436 if (msgr->global_seq < gt)
437 msgr->global_seq = gt;
438 ret = ++msgr->global_seq;
439 spin_unlock(&msgr->global_seq_lock);
440 return ret;
441}
442
443
444/*
445 * Prepare footer for currently outgoing message, and finish things
446 * off. Assumes out_kvec* are already valid.. we just add on to the end.
447 */
448static void prepare_write_message_footer(struct ceph_connection *con, int v)
449{
450 struct ceph_msg *m = con->out_msg;
451
452 dout("prepare_write_message_footer %p\n", con);
453 con->out_kvec_is_msg = true;
454 con->out_kvec[v].iov_base = &m->footer;
455 con->out_kvec[v].iov_len = sizeof(m->footer);
456 con->out_kvec_bytes += sizeof(m->footer);
457 con->out_kvec_left++;
458 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -0800459 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -0700460}
461
462/*
463 * Prepare headers for the next outgoing message.
464 */
465static void prepare_write_message(struct ceph_connection *con)
466{
467 struct ceph_msg *m;
468 int v = 0;
469
470 con->out_kvec_bytes = 0;
471 con->out_kvec_is_msg = true;
Sage Weilc86a2932009-12-14 14:04:30 -0800472 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -0700473
474 /* Sneak an ack in there first? If we can get it into the same
475 * TCP packet that's a good thing. */
476 if (con->in_seq > con->in_seq_acked) {
477 con->in_seq_acked = con->in_seq;
478 con->out_kvec[v].iov_base = &tag_ack;
479 con->out_kvec[v++].iov_len = 1;
480 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
481 con->out_kvec[v].iov_base = &con->out_temp_ack;
482 con->out_kvec[v++].iov_len = sizeof(con->out_temp_ack);
483 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
484 }
485
Sage Weil31b80062009-10-06 11:31:13 -0700486 m = list_first_entry(&con->out_queue,
487 struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -0800488 con->out_msg = m;
Sage Weilb3d1dbb2009-12-14 14:58:11 -0800489 if (test_bit(LOSSYTX, &con->state)) {
Sage Weil6c5d1a42010-02-13 20:29:31 -0800490 list_del_init(&m->list_head);
491 } else {
Sage Weilb3d1dbb2009-12-14 14:58:11 -0800492 /* put message on sent list */
493 ceph_msg_get(m);
494 list_move_tail(&m->list_head, &con->out_sent);
Sage Weilb3d1dbb2009-12-14 14:58:11 -0800495 }
Sage Weil31b80062009-10-06 11:31:13 -0700496
Sage Weile84346b2010-05-11 21:20:38 -0700497 /*
498 * only assign outgoing seq # if we haven't sent this message
499 * yet. if it is requeued, resend with it's original seq.
500 */
501 if (m->needs_out_seq) {
502 m->hdr.seq = cpu_to_le64(++con->out_seq);
503 m->needs_out_seq = false;
504 }
Sage Weil31b80062009-10-06 11:31:13 -0700505
506 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
507 m, con->out_seq, le16_to_cpu(m->hdr.type),
508 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
509 le32_to_cpu(m->hdr.data_len),
510 m->nr_pages);
511 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
512
513 /* tag + hdr + front + middle */
514 con->out_kvec[v].iov_base = &tag_msg;
515 con->out_kvec[v++].iov_len = 1;
516 con->out_kvec[v].iov_base = &m->hdr;
517 con->out_kvec[v++].iov_len = sizeof(m->hdr);
518 con->out_kvec[v++] = m->front;
519 if (m->middle)
520 con->out_kvec[v++] = m->middle->vec;
521 con->out_kvec_left = v;
522 con->out_kvec_bytes += 1 + sizeof(m->hdr) + m->front.iov_len +
523 (m->middle ? m->middle->vec.iov_len : 0);
524 con->out_kvec_cur = con->out_kvec;
525
526 /* fill in crc (except data pages), footer */
527 con->out_msg->hdr.crc =
528 cpu_to_le32(crc32c(0, (void *)&m->hdr,
529 sizeof(m->hdr) - sizeof(m->hdr.crc)));
530 con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE;
531 con->out_msg->footer.front_crc =
532 cpu_to_le32(crc32c(0, m->front.iov_base, m->front.iov_len));
533 if (m->middle)
534 con->out_msg->footer.middle_crc =
535 cpu_to_le32(crc32c(0, m->middle->vec.iov_base,
536 m->middle->vec.iov_len));
537 else
538 con->out_msg->footer.middle_crc = 0;
539 con->out_msg->footer.data_crc = 0;
540 dout("prepare_write_message front_crc %u data_crc %u\n",
541 le32_to_cpu(con->out_msg->footer.front_crc),
542 le32_to_cpu(con->out_msg->footer.middle_crc));
543
544 /* is there a data payload? */
545 if (le32_to_cpu(m->hdr.data_len) > 0) {
546 /* initialize page iterator */
547 con->out_msg_pos.page = 0;
548 con->out_msg_pos.page_pos =
549 le16_to_cpu(m->hdr.data_off) & ~PAGE_MASK;
550 con->out_msg_pos.data_pos = 0;
551 con->out_msg_pos.did_page_crc = 0;
552 con->out_more = 1; /* data + footer will follow */
553 } else {
554 /* no, queue up footer too and be done */
555 prepare_write_message_footer(con, v);
556 }
557
558 set_bit(WRITE_PENDING, &con->state);
559}
560
561/*
562 * Prepare an ack.
563 */
564static void prepare_write_ack(struct ceph_connection *con)
565{
566 dout("prepare_write_ack %p %llu -> %llu\n", con,
567 con->in_seq_acked, con->in_seq);
568 con->in_seq_acked = con->in_seq;
569
570 con->out_kvec[0].iov_base = &tag_ack;
571 con->out_kvec[0].iov_len = 1;
572 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
573 con->out_kvec[1].iov_base = &con->out_temp_ack;
574 con->out_kvec[1].iov_len = sizeof(con->out_temp_ack);
575 con->out_kvec_left = 2;
576 con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack);
577 con->out_kvec_cur = con->out_kvec;
578 con->out_more = 1; /* more will follow.. eventually.. */
579 set_bit(WRITE_PENDING, &con->state);
580}
581
582/*
583 * Prepare to write keepalive byte.
584 */
585static void prepare_write_keepalive(struct ceph_connection *con)
586{
587 dout("prepare_write_keepalive %p\n", con);
588 con->out_kvec[0].iov_base = &tag_keepalive;
589 con->out_kvec[0].iov_len = 1;
590 con->out_kvec_left = 1;
591 con->out_kvec_bytes = 1;
592 con->out_kvec_cur = con->out_kvec;
593 set_bit(WRITE_PENDING, &con->state);
594}
595
596/*
597 * Connection negotiation.
598 */
599
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800600static void prepare_connect_authorizer(struct ceph_connection *con)
601{
602 void *auth_buf;
603 int auth_len = 0;
604 int auth_protocol = 0;
605
Sage Weilec302642009-12-22 10:43:42 -0800606 mutex_unlock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800607 if (con->ops->get_authorizer)
608 con->ops->get_authorizer(con, &auth_buf, &auth_len,
609 &auth_protocol, &con->auth_reply_buf,
610 &con->auth_reply_buf_len,
611 con->auth_retry);
Sage Weilec302642009-12-22 10:43:42 -0800612 mutex_lock(&con->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800613
614 con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol);
615 con->out_connect.authorizer_len = cpu_to_le32(auth_len);
616
617 con->out_kvec[con->out_kvec_left].iov_base = auth_buf;
618 con->out_kvec[con->out_kvec_left].iov_len = auth_len;
619 con->out_kvec_left++;
620 con->out_kvec_bytes += auth_len;
621}
622
Sage Weil31b80062009-10-06 11:31:13 -0700623/*
624 * We connected to a peer and are saying hello.
625 */
Sage Weileed0ef22009-11-10 14:34:36 -0800626static void prepare_write_banner(struct ceph_messenger *msgr,
627 struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700628{
629 int len = strlen(CEPH_BANNER);
Sage Weileed0ef22009-11-10 14:34:36 -0800630
631 con->out_kvec[0].iov_base = CEPH_BANNER;
632 con->out_kvec[0].iov_len = len;
633 con->out_kvec[1].iov_base = &msgr->my_enc_addr;
634 con->out_kvec[1].iov_len = sizeof(msgr->my_enc_addr);
635 con->out_kvec_left = 2;
636 con->out_kvec_bytes = len + sizeof(msgr->my_enc_addr);
637 con->out_kvec_cur = con->out_kvec;
638 con->out_more = 0;
639 set_bit(WRITE_PENDING, &con->state);
640}
641
642static void prepare_write_connect(struct ceph_messenger *msgr,
643 struct ceph_connection *con,
644 int after_banner)
645{
Sage Weil31b80062009-10-06 11:31:13 -0700646 unsigned global_seq = get_global_seq(con->msgr, 0);
647 int proto;
648
649 switch (con->peer_name.type) {
650 case CEPH_ENTITY_TYPE_MON:
651 proto = CEPH_MONC_PROTOCOL;
652 break;
653 case CEPH_ENTITY_TYPE_OSD:
654 proto = CEPH_OSDC_PROTOCOL;
655 break;
656 case CEPH_ENTITY_TYPE_MDS:
657 proto = CEPH_MDSC_PROTOCOL;
658 break;
659 default:
660 BUG();
661 }
662
663 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
664 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800665
Sage Weil04a419f2009-12-23 09:30:21 -0800666 con->out_connect.features = CEPH_FEATURE_SUPPORTED;
Sage Weil31b80062009-10-06 11:31:13 -0700667 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
668 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
669 con->out_connect.global_seq = cpu_to_le32(global_seq);
670 con->out_connect.protocol_version = cpu_to_le32(proto);
671 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700672
Sage Weileed0ef22009-11-10 14:34:36 -0800673 if (!after_banner) {
674 con->out_kvec_left = 0;
675 con->out_kvec_bytes = 0;
676 }
677 con->out_kvec[con->out_kvec_left].iov_base = &con->out_connect;
678 con->out_kvec[con->out_kvec_left].iov_len = sizeof(con->out_connect);
679 con->out_kvec_left++;
680 con->out_kvec_bytes += sizeof(con->out_connect);
Sage Weil31b80062009-10-06 11:31:13 -0700681 con->out_kvec_cur = con->out_kvec;
682 con->out_more = 0;
683 set_bit(WRITE_PENDING, &con->state);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800684
685 prepare_connect_authorizer(con);
Sage Weil31b80062009-10-06 11:31:13 -0700686}
687
688
689/*
690 * write as much of pending kvecs to the socket as we can.
691 * 1 -> done
692 * 0 -> socket full, but more to do
693 * <0 -> error
694 */
695static int write_partial_kvec(struct ceph_connection *con)
696{
697 int ret;
698
699 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
700 while (con->out_kvec_bytes > 0) {
701 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
702 con->out_kvec_left, con->out_kvec_bytes,
703 con->out_more);
704 if (ret <= 0)
705 goto out;
706 con->out_kvec_bytes -= ret;
707 if (con->out_kvec_bytes == 0)
708 break; /* done */
709 while (ret > 0) {
710 if (ret >= con->out_kvec_cur->iov_len) {
711 ret -= con->out_kvec_cur->iov_len;
712 con->out_kvec_cur++;
713 con->out_kvec_left--;
714 } else {
715 con->out_kvec_cur->iov_len -= ret;
716 con->out_kvec_cur->iov_base += ret;
717 ret = 0;
718 break;
719 }
720 }
721 }
722 con->out_kvec_left = 0;
723 con->out_kvec_is_msg = false;
724 ret = 1;
725out:
726 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
727 con->out_kvec_bytes, con->out_kvec_left, ret);
728 return ret; /* done! */
729}
730
731/*
732 * Write as much message data payload as we can. If we finish, queue
733 * up the footer.
734 * 1 -> done, footer is now queued in out_kvec[].
735 * 0 -> socket full, but more to do
736 * <0 -> error
737 */
738static int write_partial_msg_pages(struct ceph_connection *con)
739{
740 struct ceph_msg *msg = con->out_msg;
741 unsigned data_len = le32_to_cpu(msg->hdr.data_len);
742 size_t len;
743 int crc = con->msgr->nocrc;
744 int ret;
745
746 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
747 con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages,
748 con->out_msg_pos.page_pos);
749
750 while (con->out_msg_pos.page < con->out_msg->nr_pages) {
751 struct page *page = NULL;
752 void *kaddr = NULL;
753
754 /*
755 * if we are calculating the data crc (the default), we need
756 * to map the page. if our pages[] has been revoked, use the
757 * zero page.
758 */
759 if (msg->pages) {
760 page = msg->pages[con->out_msg_pos.page];
761 if (crc)
762 kaddr = kmap(page);
Sage Weil58bb3b32009-12-23 12:12:31 -0800763 } else if (msg->pagelist) {
764 page = list_first_entry(&msg->pagelist->head,
765 struct page, lru);
766 if (crc)
767 kaddr = kmap(page);
Sage Weil31b80062009-10-06 11:31:13 -0700768 } else {
769 page = con->msgr->zero_page;
770 if (crc)
771 kaddr = page_address(con->msgr->zero_page);
772 }
773 len = min((int)(PAGE_SIZE - con->out_msg_pos.page_pos),
774 (int)(data_len - con->out_msg_pos.data_pos));
775 if (crc && !con->out_msg_pos.did_page_crc) {
776 void *base = kaddr + con->out_msg_pos.page_pos;
777 u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc);
778
779 BUG_ON(kaddr == NULL);
780 con->out_msg->footer.data_crc =
781 cpu_to_le32(crc32c(tmpcrc, base, len));
782 con->out_msg_pos.did_page_crc = 1;
783 }
784
785 ret = kernel_sendpage(con->sock, page,
786 con->out_msg_pos.page_pos, len,
787 MSG_DONTWAIT | MSG_NOSIGNAL |
788 MSG_MORE);
789
Sage Weil58bb3b32009-12-23 12:12:31 -0800790 if (crc && (msg->pages || msg->pagelist))
Sage Weil31b80062009-10-06 11:31:13 -0700791 kunmap(page);
792
793 if (ret <= 0)
794 goto out;
795
796 con->out_msg_pos.data_pos += ret;
797 con->out_msg_pos.page_pos += ret;
798 if (ret == len) {
799 con->out_msg_pos.page_pos = 0;
800 con->out_msg_pos.page++;
801 con->out_msg_pos.did_page_crc = 0;
Sage Weil58bb3b32009-12-23 12:12:31 -0800802 if (msg->pagelist)
803 list_move_tail(&page->lru,
804 &msg->pagelist->head);
Sage Weil31b80062009-10-06 11:31:13 -0700805 }
806 }
807
808 dout("write_partial_msg_pages %p msg %p done\n", con, msg);
809
810 /* prepare and queue up footer, too */
811 if (!crc)
812 con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
813 con->out_kvec_bytes = 0;
814 con->out_kvec_left = 0;
815 con->out_kvec_cur = con->out_kvec;
816 prepare_write_message_footer(con, 0);
817 ret = 1;
818out:
819 return ret;
820}
821
822/*
823 * write some zeros
824 */
825static int write_partial_skip(struct ceph_connection *con)
826{
827 int ret;
828
829 while (con->out_skip > 0) {
830 struct kvec iov = {
831 .iov_base = page_address(con->msgr->zero_page),
832 .iov_len = min(con->out_skip, (int)PAGE_CACHE_SIZE)
833 };
834
835 ret = ceph_tcp_sendmsg(con->sock, &iov, 1, iov.iov_len, 1);
836 if (ret <= 0)
837 goto out;
838 con->out_skip -= ret;
839 }
840 ret = 1;
841out:
842 return ret;
843}
844
845/*
846 * Prepare to read connection handshake, or an ack.
847 */
Sage Weileed0ef22009-11-10 14:34:36 -0800848static void prepare_read_banner(struct ceph_connection *con)
849{
850 dout("prepare_read_banner %p\n", con);
851 con->in_base_pos = 0;
852}
853
Sage Weil31b80062009-10-06 11:31:13 -0700854static void prepare_read_connect(struct ceph_connection *con)
855{
856 dout("prepare_read_connect %p\n", con);
857 con->in_base_pos = 0;
858}
859
860static void prepare_read_ack(struct ceph_connection *con)
861{
862 dout("prepare_read_ack %p\n", con);
863 con->in_base_pos = 0;
864}
865
866static void prepare_read_tag(struct ceph_connection *con)
867{
868 dout("prepare_read_tag %p\n", con);
869 con->in_base_pos = 0;
870 con->in_tag = CEPH_MSGR_TAG_READY;
871}
872
873/*
874 * Prepare to read a message.
875 */
876static int prepare_read_message(struct ceph_connection *con)
877{
878 dout("prepare_read_message %p\n", con);
879 BUG_ON(con->in_msg != NULL);
880 con->in_base_pos = 0;
881 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
882 return 0;
883}
884
885
886static int read_partial(struct ceph_connection *con,
887 int *to, int size, void *object)
888{
889 *to += size;
890 while (con->in_base_pos < *to) {
891 int left = *to - con->in_base_pos;
892 int have = size - left;
893 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
894 if (ret <= 0)
895 return ret;
896 con->in_base_pos += ret;
897 }
898 return 1;
899}
900
901
902/*
903 * Read all or part of the connect-side handshake on a new connection
904 */
Sage Weileed0ef22009-11-10 14:34:36 -0800905static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700906{
907 int ret, to = 0;
908
Sage Weileed0ef22009-11-10 14:34:36 -0800909 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -0700910
911 /* peer's banner */
912 ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner);
913 if (ret <= 0)
914 goto out;
915 ret = read_partial(con, &to, sizeof(con->actual_peer_addr),
916 &con->actual_peer_addr);
917 if (ret <= 0)
918 goto out;
919 ret = read_partial(con, &to, sizeof(con->peer_addr_for_me),
920 &con->peer_addr_for_me);
921 if (ret <= 0)
922 goto out;
Sage Weileed0ef22009-11-10 14:34:36 -0800923out:
924 return ret;
925}
926
927static int read_partial_connect(struct ceph_connection *con)
928{
929 int ret, to = 0;
930
931 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
932
Sage Weil31b80062009-10-06 11:31:13 -0700933 ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply);
934 if (ret <= 0)
935 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800936 ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len),
937 con->auth_reply_buf);
938 if (ret <= 0)
939 goto out;
Sage Weil31b80062009-10-06 11:31:13 -0700940
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800941 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
942 con, (int)con->in_reply.tag,
943 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -0700944 le32_to_cpu(con->in_reply.global_seq));
945out:
946 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -0800947
Sage Weil31b80062009-10-06 11:31:13 -0700948}
949
950/*
951 * Verify the hello banner looks okay.
952 */
953static int verify_hello(struct ceph_connection *con)
954{
955 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -0700956 pr_err("connect to %s got bad banner\n",
Sage Weil31b80062009-10-06 11:31:13 -0700957 pr_addr(&con->peer_addr.in_addr));
958 con->error_msg = "protocol error, bad banner";
959 return -1;
960 }
961 return 0;
962}
963
964static bool addr_is_blank(struct sockaddr_storage *ss)
965{
966 switch (ss->ss_family) {
967 case AF_INET:
968 return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0;
969 case AF_INET6:
970 return
971 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 &&
972 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 &&
973 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 &&
974 ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0;
975 }
976 return false;
977}
978
979static int addr_port(struct sockaddr_storage *ss)
980{
981 switch (ss->ss_family) {
982 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -0800983 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -0700984 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -0800985 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -0700986 }
987 return 0;
988}
989
990static void addr_set_port(struct sockaddr_storage *ss, int p)
991{
992 switch (ss->ss_family) {
993 case AF_INET:
994 ((struct sockaddr_in *)ss)->sin_port = htons(p);
995 case AF_INET6:
996 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
997 }
998}
999
1000/*
1001 * Parse an ip[:port] list into an addr array. Use the default
1002 * monitor port if a port isn't specified.
1003 */
1004int ceph_parse_ips(const char *c, const char *end,
1005 struct ceph_entity_addr *addr,
1006 int max_count, int *count)
1007{
1008 int i;
1009 const char *p = c;
1010
1011 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1012 for (i = 0; i < max_count; i++) {
1013 const char *ipend;
1014 struct sockaddr_storage *ss = &addr[i].in_addr;
1015 struct sockaddr_in *in4 = (void *)ss;
1016 struct sockaddr_in6 *in6 = (void *)ss;
1017 int port;
1018
1019 memset(ss, 0, sizeof(*ss));
1020 if (in4_pton(p, end - p, (u8 *)&in4->sin_addr.s_addr,
1021 ',', &ipend)) {
1022 ss->ss_family = AF_INET;
1023 } else if (in6_pton(p, end - p, (u8 *)&in6->sin6_addr.s6_addr,
1024 ',', &ipend)) {
1025 ss->ss_family = AF_INET6;
1026 } else {
1027 goto bad;
1028 }
1029 p = ipend;
1030
1031 /* port? */
1032 if (p < end && *p == ':') {
1033 port = 0;
1034 p++;
1035 while (p < end && *p >= '0' && *p <= '9') {
1036 port = (port * 10) + (*p - '0');
1037 p++;
1038 }
1039 if (port > 65535 || port == 0)
1040 goto bad;
1041 } else {
1042 port = CEPH_MON_PORT;
1043 }
1044
1045 addr_set_port(ss, port);
1046
1047 dout("parse_ips got %s\n", pr_addr(ss));
1048
1049 if (p == end)
1050 break;
1051 if (*p != ',')
1052 goto bad;
1053 p++;
1054 }
1055
1056 if (p != end)
1057 goto bad;
1058
1059 if (count)
1060 *count = i + 1;
1061 return 0;
1062
1063bad:
1064 pr_err("parse_ips bad ip '%s'\n", c);
1065 return -EINVAL;
1066}
1067
Sage Weileed0ef22009-11-10 14:34:36 -08001068static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001069{
Sage Weileed0ef22009-11-10 14:34:36 -08001070 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001071
1072 if (verify_hello(con) < 0)
1073 return -1;
1074
Sage Weil63f2d212009-11-03 15:17:56 -08001075 ceph_decode_addr(&con->actual_peer_addr);
1076 ceph_decode_addr(&con->peer_addr_for_me);
1077
Sage Weil31b80062009-10-06 11:31:13 -07001078 /*
1079 * Make sure the other end is who we wanted. note that the other
1080 * end may not yet know their ip address, so if it's 0.0.0.0, give
1081 * them the benefit of the doubt.
1082 */
Sage Weil103e2d32010-01-07 16:12:36 -08001083 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
1084 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07001085 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
1086 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Sage Weil103e2d32010-01-07 16:12:36 -08001087 pr_warning("wrong peer, want %s/%lld, got %s/%lld\n",
1088 pr_addr(&con->peer_addr.in_addr),
1089 le64_to_cpu(con->peer_addr.nonce),
1090 pr_addr(&con->actual_peer_addr.in_addr),
1091 le64_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08001092 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07001093 return -1;
1094 }
1095
1096 /*
1097 * did we learn our address?
1098 */
1099 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
1100 int port = addr_port(&con->msgr->inst.addr.in_addr);
1101
1102 memcpy(&con->msgr->inst.addr.in_addr,
1103 &con->peer_addr_for_me.in_addr,
1104 sizeof(con->peer_addr_for_me.in_addr));
1105 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08001106 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08001107 dout("process_banner learned my addr is %s\n",
Sage Weil31b80062009-10-06 11:31:13 -07001108 pr_addr(&con->msgr->inst.addr.in_addr));
1109 }
1110
Sage Weileed0ef22009-11-10 14:34:36 -08001111 set_bit(NEGOTIATING, &con->state);
1112 prepare_read_connect(con);
1113 return 0;
1114}
1115
Sage Weil04a419f2009-12-23 09:30:21 -08001116static void fail_protocol(struct ceph_connection *con)
1117{
1118 reset_connection(con);
1119 set_bit(CLOSED, &con->state); /* in case there's queued work */
1120
1121 mutex_unlock(&con->mutex);
1122 if (con->ops->bad_proto)
1123 con->ops->bad_proto(con);
1124 mutex_lock(&con->mutex);
1125}
1126
Sage Weileed0ef22009-11-10 14:34:36 -08001127static int process_connect(struct ceph_connection *con)
1128{
Sage Weil04a419f2009-12-23 09:30:21 -08001129 u64 sup_feat = CEPH_FEATURE_SUPPORTED;
1130 u64 req_feat = CEPH_FEATURE_REQUIRED;
1131 u64 server_feat = le64_to_cpu(con->in_reply.features);
1132
Sage Weileed0ef22009-11-10 14:34:36 -08001133 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
1134
Sage Weil31b80062009-10-06 11:31:13 -07001135 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08001136 case CEPH_MSGR_TAG_FEATURES:
1137 pr_err("%s%lld %s feature set mismatch,"
1138 " my %llx < server's %llx, missing %llx\n",
1139 ENTITY_NAME(con->peer_name),
1140 pr_addr(&con->peer_addr.in_addr),
1141 sup_feat, server_feat, server_feat & ~sup_feat);
1142 con->error_msg = "missing required protocol features";
1143 fail_protocol(con);
1144 return -1;
1145
Sage Weil31b80062009-10-06 11:31:13 -07001146 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07001147 pr_err("%s%lld %s protocol version mismatch,"
1148 " my %d != server's %d\n",
1149 ENTITY_NAME(con->peer_name),
1150 pr_addr(&con->peer_addr.in_addr),
1151 le32_to_cpu(con->out_connect.protocol_version),
1152 le32_to_cpu(con->in_reply.protocol_version));
1153 con->error_msg = "protocol version mismatch";
Sage Weil04a419f2009-12-23 09:30:21 -08001154 fail_protocol(con);
Sage Weil31b80062009-10-06 11:31:13 -07001155 return -1;
1156
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001157 case CEPH_MSGR_TAG_BADAUTHORIZER:
1158 con->auth_retry++;
1159 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
1160 con->auth_retry);
1161 if (con->auth_retry == 2) {
1162 con->error_msg = "connect authorization failure";
1163 reset_connection(con);
1164 set_bit(CLOSED, &con->state);
1165 return -1;
1166 }
1167 con->auth_retry = 1;
1168 prepare_write_connect(con->msgr, con, 0);
Sage Weil63733a02010-03-15 15:47:22 -07001169 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001170 break;
Sage Weil31b80062009-10-06 11:31:13 -07001171
1172 case CEPH_MSGR_TAG_RESETSESSION:
1173 /*
1174 * If we connected with a large connect_seq but the peer
1175 * has no record of a session with us (no connection, or
1176 * connect_seq == 0), they will send RESETSESION to indicate
1177 * that they must have reset their session, and may have
1178 * dropped messages.
1179 */
1180 dout("process_connect got RESET peer seq %u\n",
1181 le32_to_cpu(con->in_connect.connect_seq));
1182 pr_err("%s%lld %s connection reset\n",
1183 ENTITY_NAME(con->peer_name),
1184 pr_addr(&con->peer_addr.in_addr));
1185 reset_connection(con);
Sage Weileed0ef22009-11-10 14:34:36 -08001186 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001187 prepare_read_connect(con);
1188
1189 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08001190 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001191 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
1192 if (con->ops->peer_reset)
1193 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08001194 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001195 break;
1196
1197 case CEPH_MSGR_TAG_RETRY_SESSION:
1198 /*
1199 * If we sent a smaller connect_seq than the peer has, try
1200 * again with a larger value.
1201 */
1202 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1203 le32_to_cpu(con->out_connect.connect_seq),
1204 le32_to_cpu(con->in_connect.connect_seq));
1205 con->connect_seq = le32_to_cpu(con->in_connect.connect_seq);
Sage Weileed0ef22009-11-10 14:34:36 -08001206 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001207 prepare_read_connect(con);
1208 break;
1209
1210 case CEPH_MSGR_TAG_RETRY_GLOBAL:
1211 /*
1212 * If we sent a smaller global_seq than the peer has, try
1213 * again with a larger value.
1214 */
Sage Weileed0ef22009-11-10 14:34:36 -08001215 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07001216 con->peer_global_seq,
1217 le32_to_cpu(con->in_connect.global_seq));
1218 get_global_seq(con->msgr,
1219 le32_to_cpu(con->in_connect.global_seq));
Sage Weileed0ef22009-11-10 14:34:36 -08001220 prepare_write_connect(con->msgr, con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001221 prepare_read_connect(con);
1222 break;
1223
1224 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08001225 if (req_feat & ~server_feat) {
1226 pr_err("%s%lld %s protocol feature mismatch,"
1227 " my required %llx > server's %llx, need %llx\n",
1228 ENTITY_NAME(con->peer_name),
1229 pr_addr(&con->peer_addr.in_addr),
1230 req_feat, server_feat, req_feat & ~server_feat);
1231 con->error_msg = "missing required protocol features";
1232 fail_protocol(con);
1233 return -1;
1234 }
Sage Weil31b80062009-10-06 11:31:13 -07001235 clear_bit(CONNECTING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001236 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
1237 con->connect_seq++;
1238 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1239 con->peer_global_seq,
1240 le32_to_cpu(con->in_reply.connect_seq),
1241 con->connect_seq);
1242 WARN_ON(con->connect_seq !=
1243 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08001244
1245 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
1246 set_bit(LOSSYTX, &con->state);
1247
Sage Weil31b80062009-10-06 11:31:13 -07001248 prepare_read_tag(con);
1249 break;
1250
1251 case CEPH_MSGR_TAG_WAIT:
1252 /*
1253 * If there is a connection race (we are opening
1254 * connections to each other), one of us may just have
1255 * to WAIT. This shouldn't happen if we are the
1256 * client.
1257 */
1258 pr_err("process_connect peer connecting WAIT\n");
1259
1260 default:
1261 pr_err("connect protocol error, will retry\n");
1262 con->error_msg = "protocol error, garbage tag during connect";
1263 return -1;
1264 }
1265 return 0;
1266}
1267
1268
1269/*
1270 * read (part of) an ack
1271 */
1272static int read_partial_ack(struct ceph_connection *con)
1273{
1274 int to = 0;
1275
1276 return read_partial(con, &to, sizeof(con->in_temp_ack),
1277 &con->in_temp_ack);
1278}
1279
1280
1281/*
1282 * We can finally discard anything that's been acked.
1283 */
1284static void process_ack(struct ceph_connection *con)
1285{
1286 struct ceph_msg *m;
1287 u64 ack = le64_to_cpu(con->in_temp_ack);
1288 u64 seq;
1289
Sage Weil31b80062009-10-06 11:31:13 -07001290 while (!list_empty(&con->out_sent)) {
1291 m = list_first_entry(&con->out_sent, struct ceph_msg,
1292 list_head);
1293 seq = le64_to_cpu(m->hdr.seq);
1294 if (seq > ack)
1295 break;
1296 dout("got ack for seq %llu type %d at %p\n", seq,
1297 le16_to_cpu(m->hdr.type), m);
1298 ceph_msg_remove(m);
1299 }
Sage Weil31b80062009-10-06 11:31:13 -07001300 prepare_read_tag(con);
1301}
1302
1303
1304
1305
Yehuda Sadeh24504182010-01-08 13:58:34 -08001306static int read_partial_message_section(struct ceph_connection *con,
1307 struct kvec *section, unsigned int sec_len,
1308 u32 *crc)
1309{
1310 int left;
1311 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001312
Yehuda Sadeh24504182010-01-08 13:58:34 -08001313 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07001314
Yehuda Sadeh24504182010-01-08 13:58:34 -08001315 while (section->iov_len < sec_len) {
1316 BUG_ON(section->iov_base == NULL);
1317 left = sec_len - section->iov_len;
1318 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
1319 section->iov_len, left);
1320 if (ret <= 0)
1321 return ret;
1322 section->iov_len += ret;
1323 if (section->iov_len == sec_len)
1324 *crc = crc32c(0, section->iov_base,
1325 section->iov_len);
1326 }
1327
1328 return 1;
1329}
1330
1331static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
1332 struct ceph_msg_header *hdr,
1333 int *skip);
Sage Weil31b80062009-10-06 11:31:13 -07001334/*
1335 * read (part of) a message.
1336 */
1337static int read_partial_message(struct ceph_connection *con)
1338{
1339 struct ceph_msg *m = con->in_msg;
1340 void *p;
1341 int ret;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001342 int to, left;
Sage Weil31b80062009-10-06 11:31:13 -07001343 unsigned front_len, middle_len, data_len, data_off;
1344 int datacrc = con->msgr->nocrc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001345 int skip;
Sage Weilae187562010-04-22 07:47:01 -07001346 u64 seq;
Sage Weil31b80062009-10-06 11:31:13 -07001347
1348 dout("read_partial_message con %p msg %p\n", con, m);
1349
1350 /* header */
1351 while (con->in_base_pos < sizeof(con->in_hdr)) {
1352 left = sizeof(con->in_hdr) - con->in_base_pos;
1353 ret = ceph_tcp_recvmsg(con->sock,
1354 (char *)&con->in_hdr + con->in_base_pos,
1355 left);
1356 if (ret <= 0)
1357 return ret;
1358 con->in_base_pos += ret;
1359 if (con->in_base_pos == sizeof(con->in_hdr)) {
1360 u32 crc = crc32c(0, (void *)&con->in_hdr,
1361 sizeof(con->in_hdr) - sizeof(con->in_hdr.crc));
1362 if (crc != le32_to_cpu(con->in_hdr.crc)) {
1363 pr_err("read_partial_message bad hdr "
1364 " crc %u != expected %u\n",
1365 crc, con->in_hdr.crc);
1366 return -EBADMSG;
1367 }
1368 }
1369 }
Sage Weil31b80062009-10-06 11:31:13 -07001370 front_len = le32_to_cpu(con->in_hdr.front_len);
1371 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
1372 return -EIO;
1373 middle_len = le32_to_cpu(con->in_hdr.middle_len);
1374 if (middle_len > CEPH_MSG_MAX_DATA_LEN)
1375 return -EIO;
1376 data_len = le32_to_cpu(con->in_hdr.data_len);
1377 if (data_len > CEPH_MSG_MAX_DATA_LEN)
1378 return -EIO;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001379 data_off = le16_to_cpu(con->in_hdr.data_off);
Sage Weil31b80062009-10-06 11:31:13 -07001380
Sage Weilae187562010-04-22 07:47:01 -07001381 /* verify seq# */
1382 seq = le64_to_cpu(con->in_hdr.seq);
1383 if ((s64)seq - (s64)con->in_seq < 1) {
1384 pr_info("skipping %s%lld %s seq %lld, expected %lld\n",
1385 ENTITY_NAME(con->peer_name),
1386 pr_addr(&con->peer_addr.in_addr),
1387 seq, con->in_seq + 1);
1388 con->in_base_pos = -front_len - middle_len - data_len -
1389 sizeof(m->footer);
1390 con->in_tag = CEPH_MSGR_TAG_READY;
1391 con->in_seq++;
1392 return 0;
1393 } else if ((s64)seq - (s64)con->in_seq > 1) {
1394 pr_err("read_partial_message bad seq %lld expected %lld\n",
1395 seq, con->in_seq + 1);
1396 con->error_msg = "bad message sequence # for incoming message";
1397 return -EBADMSG;
1398 }
1399
Sage Weil31b80062009-10-06 11:31:13 -07001400 /* allocate message? */
1401 if (!con->in_msg) {
1402 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
1403 con->in_hdr.front_len, con->in_hdr.data_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001404 con->in_msg = ceph_alloc_msg(con, &con->in_hdr, &skip);
1405 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07001406 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07001407 dout("alloc_msg said skip message\n");
Sage Weil31b80062009-10-06 11:31:13 -07001408 con->in_base_pos = -front_len - middle_len - data_len -
1409 sizeof(m->footer);
1410 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07001411 con->in_seq++;
Sage Weil31b80062009-10-06 11:31:13 -07001412 return 0;
1413 }
Sage Weila79832f2010-04-01 16:06:19 -07001414 if (!con->in_msg) {
Sage Weil5b3a4db2010-02-19 21:43:23 -08001415 con->error_msg =
1416 "error allocating memory for incoming message";
Sage Weila79832f2010-04-01 16:06:19 -07001417 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -07001418 }
1419 m = con->in_msg;
1420 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001421 if (m->middle)
1422 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08001423
1424 con->in_msg_pos.page = 0;
1425 con->in_msg_pos.page_pos = data_off & ~PAGE_MASK;
1426 con->in_msg_pos.data_pos = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001427 }
1428
1429 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001430 ret = read_partial_message_section(con, &m->front, front_len,
1431 &con->in_front_crc);
1432 if (ret <= 0)
1433 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001434
1435 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08001436 if (m->middle) {
1437 ret = read_partial_message_section(con, &m->middle->vec, middle_len,
1438 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07001439 if (ret <= 0)
1440 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001441 }
1442
1443 /* (page) data */
Sage Weil31b80062009-10-06 11:31:13 -07001444 while (con->in_msg_pos.data_pos < data_len) {
1445 left = min((int)(data_len - con->in_msg_pos.data_pos),
1446 (int)(PAGE_SIZE - con->in_msg_pos.page_pos));
1447 BUG_ON(m->pages == NULL);
1448 p = kmap(m->pages[con->in_msg_pos.page]);
1449 ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos,
1450 left);
1451 if (ret > 0 && datacrc)
1452 con->in_data_crc =
1453 crc32c(con->in_data_crc,
1454 p + con->in_msg_pos.page_pos, ret);
1455 kunmap(m->pages[con->in_msg_pos.page]);
1456 if (ret <= 0)
1457 return ret;
1458 con->in_msg_pos.data_pos += ret;
1459 con->in_msg_pos.page_pos += ret;
1460 if (con->in_msg_pos.page_pos == PAGE_SIZE) {
1461 con->in_msg_pos.page_pos = 0;
1462 con->in_msg_pos.page++;
1463 }
1464 }
1465
Sage Weil31b80062009-10-06 11:31:13 -07001466 /* footer */
1467 to = sizeof(m->hdr) + sizeof(m->footer);
1468 while (con->in_base_pos < to) {
1469 left = to - con->in_base_pos;
1470 ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer +
1471 (con->in_base_pos - sizeof(m->hdr)),
1472 left);
1473 if (ret <= 0)
1474 return ret;
1475 con->in_base_pos += ret;
1476 }
1477 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1478 m, front_len, m->footer.front_crc, middle_len,
1479 m->footer.middle_crc, data_len, m->footer.data_crc);
1480
1481 /* crc ok? */
1482 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
1483 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1484 m, con->in_front_crc, m->footer.front_crc);
1485 return -EBADMSG;
1486 }
1487 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
1488 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1489 m, con->in_middle_crc, m->footer.middle_crc);
1490 return -EBADMSG;
1491 }
1492 if (datacrc &&
1493 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
1494 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
1495 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
1496 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
1497 return -EBADMSG;
1498 }
1499
1500 return 1; /* done! */
1501}
1502
1503/*
1504 * Process message. This happens in the worker thread. The callback should
1505 * be careful not to do anything that waits on other incoming messages or it
1506 * may deadlock.
1507 */
1508static void process_message(struct ceph_connection *con)
1509{
Sage Weil5e095e82009-12-14 14:30:34 -08001510 struct ceph_msg *msg;
Sage Weil31b80062009-10-06 11:31:13 -07001511
Sage Weil5e095e82009-12-14 14:30:34 -08001512 msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07001513 con->in_msg = NULL;
1514
1515 /* if first message, set peer_name */
1516 if (con->peer_name.type == 0)
1517 con->peer_name = msg->hdr.src.name;
1518
Sage Weil31b80062009-10-06 11:31:13 -07001519 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08001520 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001521
1522 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1523 msg, le64_to_cpu(msg->hdr.seq),
1524 ENTITY_NAME(msg->hdr.src.name),
1525 le16_to_cpu(msg->hdr.type),
1526 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
1527 le32_to_cpu(msg->hdr.front_len),
1528 le32_to_cpu(msg->hdr.data_len),
1529 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
1530 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08001531
1532 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001533 prepare_read_tag(con);
1534}
1535
1536
1537/*
1538 * Write something to the socket. Called in a worker thread when the
1539 * socket appears to be writeable and we have something ready to send.
1540 */
1541static int try_write(struct ceph_connection *con)
1542{
1543 struct ceph_messenger *msgr = con->msgr;
1544 int ret = 1;
1545
1546 dout("try_write start %p state %lu nref %d\n", con, con->state,
1547 atomic_read(&con->nref));
1548
Sage Weil31b80062009-10-06 11:31:13 -07001549more:
1550 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
1551
1552 /* open the socket first? */
1553 if (con->sock == NULL) {
1554 /*
1555 * if we were STANDBY and are reconnecting _this_
1556 * connection, bump connect_seq now. Always bump
1557 * global_seq.
1558 */
1559 if (test_and_clear_bit(STANDBY, &con->state))
1560 con->connect_seq++;
1561
Sage Weileed0ef22009-11-10 14:34:36 -08001562 prepare_write_banner(msgr, con);
1563 prepare_write_connect(msgr, con, 1);
1564 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07001565 set_bit(CONNECTING, &con->state);
Sage Weileed0ef22009-11-10 14:34:36 -08001566 clear_bit(NEGOTIATING, &con->state);
Sage Weil31b80062009-10-06 11:31:13 -07001567
Sage Weilcf3e5c42009-12-11 09:48:05 -08001568 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07001569 con->in_tag = CEPH_MSGR_TAG_READY;
1570 dout("try_write initiating connect on %p new state %lu\n",
1571 con, con->state);
1572 con->sock = ceph_tcp_connect(con);
1573 if (IS_ERR(con->sock)) {
1574 con->sock = NULL;
1575 con->error_msg = "connect error";
1576 ret = -1;
1577 goto out;
1578 }
1579 }
1580
1581more_kvec:
1582 /* kvec data queued? */
1583 if (con->out_skip) {
1584 ret = write_partial_skip(con);
1585 if (ret <= 0)
1586 goto done;
1587 if (ret < 0) {
1588 dout("try_write write_partial_skip err %d\n", ret);
1589 goto done;
1590 }
1591 }
1592 if (con->out_kvec_left) {
1593 ret = write_partial_kvec(con);
1594 if (ret <= 0)
1595 goto done;
Sage Weil31b80062009-10-06 11:31:13 -07001596 }
1597
1598 /* msg pages? */
1599 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08001600 if (con->out_msg_done) {
1601 ceph_msg_put(con->out_msg);
1602 con->out_msg = NULL; /* we're done with this one */
1603 goto do_next;
1604 }
1605
Sage Weil31b80062009-10-06 11:31:13 -07001606 ret = write_partial_msg_pages(con);
1607 if (ret == 1)
1608 goto more_kvec; /* we need to send the footer, too! */
1609 if (ret == 0)
1610 goto done;
1611 if (ret < 0) {
1612 dout("try_write write_partial_msg_pages err %d\n",
1613 ret);
1614 goto done;
1615 }
1616 }
1617
Sage Weilc86a2932009-12-14 14:04:30 -08001618do_next:
Sage Weil31b80062009-10-06 11:31:13 -07001619 if (!test_bit(CONNECTING, &con->state)) {
1620 /* is anything else pending? */
1621 if (!list_empty(&con->out_queue)) {
1622 prepare_write_message(con);
1623 goto more;
1624 }
1625 if (con->in_seq > con->in_seq_acked) {
1626 prepare_write_ack(con);
1627 goto more;
1628 }
1629 if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) {
1630 prepare_write_keepalive(con);
1631 goto more;
1632 }
1633 }
1634
1635 /* Nothing to do! */
1636 clear_bit(WRITE_PENDING, &con->state);
1637 dout("try_write nothing else to write.\n");
1638done:
1639 ret = 0;
1640out:
Sage Weil31b80062009-10-06 11:31:13 -07001641 dout("try_write done on %p\n", con);
1642 return ret;
1643}
1644
1645
1646
1647/*
1648 * Read what we can from the socket.
1649 */
1650static int try_read(struct ceph_connection *con)
1651{
Sage Weil31b80062009-10-06 11:31:13 -07001652 int ret = -1;
1653
1654 if (!con->sock)
1655 return 0;
1656
1657 if (test_bit(STANDBY, &con->state))
1658 return 0;
1659
1660 dout("try_read start on %p\n", con);
Sage Weilec302642009-12-22 10:43:42 -08001661
Sage Weil31b80062009-10-06 11:31:13 -07001662more:
1663 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
1664 con->in_base_pos);
1665 if (test_bit(CONNECTING, &con->state)) {
Sage Weileed0ef22009-11-10 14:34:36 -08001666 if (!test_bit(NEGOTIATING, &con->state)) {
1667 dout("try_read connecting\n");
1668 ret = read_partial_banner(con);
1669 if (ret <= 0)
1670 goto done;
1671 if (process_banner(con) < 0) {
1672 ret = -1;
1673 goto out;
1674 }
1675 }
Sage Weil31b80062009-10-06 11:31:13 -07001676 ret = read_partial_connect(con);
1677 if (ret <= 0)
1678 goto done;
1679 if (process_connect(con) < 0) {
1680 ret = -1;
1681 goto out;
1682 }
1683 goto more;
1684 }
1685
1686 if (con->in_base_pos < 0) {
1687 /*
1688 * skipping + discarding content.
1689 *
1690 * FIXME: there must be a better way to do this!
1691 */
1692 static char buf[1024];
1693 int skip = min(1024, -con->in_base_pos);
1694 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
1695 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
1696 if (ret <= 0)
1697 goto done;
1698 con->in_base_pos += ret;
1699 if (con->in_base_pos)
1700 goto more;
1701 }
1702 if (con->in_tag == CEPH_MSGR_TAG_READY) {
1703 /*
1704 * what's next?
1705 */
1706 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
1707 if (ret <= 0)
1708 goto done;
1709 dout("try_read got tag %d\n", (int)con->in_tag);
1710 switch (con->in_tag) {
1711 case CEPH_MSGR_TAG_MSG:
1712 prepare_read_message(con);
1713 break;
1714 case CEPH_MSGR_TAG_ACK:
1715 prepare_read_ack(con);
1716 break;
1717 case CEPH_MSGR_TAG_CLOSE:
1718 set_bit(CLOSED, &con->state); /* fixme */
1719 goto done;
1720 default:
1721 goto bad_tag;
1722 }
1723 }
1724 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
1725 ret = read_partial_message(con);
1726 if (ret <= 0) {
1727 switch (ret) {
1728 case -EBADMSG:
1729 con->error_msg = "bad crc";
1730 ret = -EIO;
1731 goto out;
1732 case -EIO:
1733 con->error_msg = "io error";
1734 goto out;
1735 default:
1736 goto done;
1737 }
1738 }
1739 if (con->in_tag == CEPH_MSGR_TAG_READY)
1740 goto more;
1741 process_message(con);
1742 goto more;
1743 }
1744 if (con->in_tag == CEPH_MSGR_TAG_ACK) {
1745 ret = read_partial_ack(con);
1746 if (ret <= 0)
1747 goto done;
1748 process_ack(con);
1749 goto more;
1750 }
1751
1752done:
1753 ret = 0;
1754out:
1755 dout("try_read done on %p\n", con);
1756 return ret;
1757
1758bad_tag:
1759 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
1760 con->error_msg = "protocol error, garbage tag";
1761 ret = -1;
1762 goto out;
1763}
1764
1765
1766/*
1767 * Atomically queue work on a connection. Bump @con reference to
1768 * avoid races with connection teardown.
1769 *
1770 * There is some trickery going on with QUEUED and BUSY because we
1771 * only want a _single_ thread operating on each connection at any
1772 * point in time, but we want to use all available CPUs.
1773 *
1774 * The worker thread only proceeds if it can atomically set BUSY. It
1775 * clears QUEUED and does it's thing. When it thinks it's done, it
1776 * clears BUSY, then rechecks QUEUED.. if it's set again, it loops
1777 * (tries again to set BUSY).
1778 *
1779 * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we
1780 * try to queue work. If that fails (work is already queued, or BUSY)
1781 * we give up (work also already being done or is queued) but leave QUEUED
1782 * set so that the worker thread will loop if necessary.
1783 */
1784static void queue_con(struct ceph_connection *con)
1785{
1786 if (test_bit(DEAD, &con->state)) {
1787 dout("queue_con %p ignoring: DEAD\n",
1788 con);
1789 return;
1790 }
1791
1792 if (!con->ops->get(con)) {
1793 dout("queue_con %p ref count 0\n", con);
1794 return;
1795 }
1796
1797 set_bit(QUEUED, &con->state);
1798 if (test_bit(BUSY, &con->state)) {
1799 dout("queue_con %p - already BUSY\n", con);
1800 con->ops->put(con);
1801 } else if (!queue_work(ceph_msgr_wq, &con->work.work)) {
1802 dout("queue_con %p - already queued\n", con);
1803 con->ops->put(con);
1804 } else {
1805 dout("queue_con %p\n", con);
1806 }
1807}
1808
1809/*
1810 * Do some work on a connection. Drop a connection ref when we're done.
1811 */
1812static void con_work(struct work_struct *work)
1813{
1814 struct ceph_connection *con = container_of(work, struct ceph_connection,
1815 work.work);
1816 int backoff = 0;
1817
1818more:
1819 if (test_and_set_bit(BUSY, &con->state) != 0) {
1820 dout("con_work %p BUSY already set\n", con);
1821 goto out;
1822 }
1823 dout("con_work %p start, clearing QUEUED\n", con);
1824 clear_bit(QUEUED, &con->state);
1825
Sage Weil9dd46582010-04-28 13:51:50 -07001826 mutex_lock(&con->mutex);
1827
Sage Weil31b80062009-10-06 11:31:13 -07001828 if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */
1829 dout("con_work CLOSED\n");
1830 con_close_socket(con);
1831 goto done;
1832 }
1833 if (test_and_clear_bit(OPENING, &con->state)) {
1834 /* reopen w/ new peer */
1835 dout("con_work OPENING\n");
1836 con_close_socket(con);
1837 }
1838
1839 if (test_and_clear_bit(SOCK_CLOSED, &con->state) ||
1840 try_read(con) < 0 ||
1841 try_write(con) < 0) {
Sage Weil9dd46582010-04-28 13:51:50 -07001842 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001843 backoff = 1;
1844 ceph_fault(con); /* error/fault path */
Sage Weil9dd46582010-04-28 13:51:50 -07001845 goto done_unlocked;
Sage Weil31b80062009-10-06 11:31:13 -07001846 }
1847
1848done:
Sage Weil9dd46582010-04-28 13:51:50 -07001849 mutex_unlock(&con->mutex);
1850
1851done_unlocked:
Sage Weil31b80062009-10-06 11:31:13 -07001852 clear_bit(BUSY, &con->state);
1853 dout("con->state=%lu\n", con->state);
1854 if (test_bit(QUEUED, &con->state)) {
Sage Weile2663ab2010-02-16 22:01:03 -08001855 if (!backoff || test_bit(OPENING, &con->state)) {
Sage Weil31b80062009-10-06 11:31:13 -07001856 dout("con_work %p QUEUED reset, looping\n", con);
1857 goto more;
1858 }
1859 dout("con_work %p QUEUED reset, but just faulted\n", con);
1860 clear_bit(QUEUED, &con->state);
1861 }
1862 dout("con_work %p done\n", con);
1863
1864out:
1865 con->ops->put(con);
1866}
1867
1868
1869/*
1870 * Generic error/fault handler. A retry mechanism is used with
1871 * exponential backoff
1872 */
1873static void ceph_fault(struct ceph_connection *con)
1874{
1875 pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
1876 pr_addr(&con->peer_addr.in_addr), con->error_msg);
1877 dout("fault %p state %lu to peer %s\n",
1878 con, con->state, pr_addr(&con->peer_addr.in_addr));
1879
1880 if (test_bit(LOSSYTX, &con->state)) {
1881 dout("fault on LOSSYTX channel\n");
1882 goto out;
1883 }
1884
Sage Weilec302642009-12-22 10:43:42 -08001885 mutex_lock(&con->mutex);
Sage Weil91e45ce2010-02-15 12:05:09 -08001886 if (test_bit(CLOSED, &con->state))
1887 goto out_unlock;
Sage Weilec302642009-12-22 10:43:42 -08001888
Sage Weil31b80062009-10-06 11:31:13 -07001889 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08001890
1891 if (con->in_msg) {
1892 ceph_msg_put(con->in_msg);
1893 con->in_msg = NULL;
1894 }
Sage Weil31b80062009-10-06 11:31:13 -07001895
Sage Weile80a52d2010-02-25 12:40:45 -08001896 /* Requeue anything that hasn't been acked */
1897 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001898
Sage Weil31b80062009-10-06 11:31:13 -07001899 /* If there are no messages in the queue, place the connection
1900 * in a STANDBY state (i.e., don't try to reconnect just yet). */
Sage Weil31b80062009-10-06 11:31:13 -07001901 if (list_empty(&con->out_queue) && !con->out_keepalive_pending) {
1902 dout("fault setting STANDBY\n");
1903 set_bit(STANDBY, &con->state);
Sage Weile80a52d2010-02-25 12:40:45 -08001904 } else {
1905 /* retry after a delay. */
1906 if (con->delay == 0)
1907 con->delay = BASE_DELAY_INTERVAL;
1908 else if (con->delay < MAX_DELAY_INTERVAL)
1909 con->delay *= 2;
1910 dout("fault queueing %p delay %lu\n", con, con->delay);
1911 con->ops->get(con);
1912 if (queue_delayed_work(ceph_msgr_wq, &con->work,
1913 round_jiffies_relative(con->delay)) == 0)
1914 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07001915 }
1916
Sage Weil91e45ce2010-02-15 12:05:09 -08001917out_unlock:
1918 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07001919out:
Sage Weil161fd652010-02-25 12:38:57 -08001920 /*
1921 * in case we faulted due to authentication, invalidate our
1922 * current tickets so that we can get new ones.
1923 */
1924 if (con->auth_retry && con->ops->invalidate_authorizer) {
1925 dout("calling invalidate_authorizer()\n");
1926 con->ops->invalidate_authorizer(con);
1927 }
1928
Sage Weil31b80062009-10-06 11:31:13 -07001929 if (con->ops->fault)
1930 con->ops->fault(con);
1931}
1932
1933
1934
1935/*
1936 * create a new messenger instance
1937 */
1938struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr)
1939{
1940 struct ceph_messenger *msgr;
1941
1942 msgr = kzalloc(sizeof(*msgr), GFP_KERNEL);
1943 if (msgr == NULL)
1944 return ERR_PTR(-ENOMEM);
1945
1946 spin_lock_init(&msgr->global_seq_lock);
1947
1948 /* the zero page is needed if a request is "canceled" while the message
1949 * is being written over the socket */
Yehuda Sadeh31459fe2010-03-17 13:54:02 -07001950 msgr->zero_page = __page_cache_alloc(GFP_KERNEL | __GFP_ZERO);
Sage Weil31b80062009-10-06 11:31:13 -07001951 if (!msgr->zero_page) {
1952 kfree(msgr);
1953 return ERR_PTR(-ENOMEM);
1954 }
1955 kmap(msgr->zero_page);
1956
1957 if (myaddr)
1958 msgr->inst.addr = *myaddr;
1959
1960 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08001961 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08001962 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08001963 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07001964
1965 dout("messenger_create %p\n", msgr);
1966 return msgr;
1967}
1968
1969void ceph_messenger_destroy(struct ceph_messenger *msgr)
1970{
1971 dout("destroy %p\n", msgr);
1972 kunmap(msgr->zero_page);
1973 __free_page(msgr->zero_page);
1974 kfree(msgr);
1975 dout("destroyed messenger %p\n", msgr);
1976}
1977
1978/*
1979 * Queue up an outgoing message on the given connection.
1980 */
1981void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
1982{
1983 if (test_bit(CLOSED, &con->state)) {
1984 dout("con_send %p closed, dropping %p\n", con, msg);
1985 ceph_msg_put(msg);
1986 return;
1987 }
1988
1989 /* set src+dst */
Sage Weil63f2d212009-11-03 15:17:56 -08001990 msg->hdr.src.name = con->msgr->inst.name;
1991 msg->hdr.src.addr = con->msgr->my_enc_addr;
1992 msg->hdr.orig_src = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07001993
Sage Weil3ca02ef2010-03-01 15:25:00 -08001994 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
1995
Sage Weile84346b2010-05-11 21:20:38 -07001996 msg->needs_out_seq = true;
1997
Sage Weil31b80062009-10-06 11:31:13 -07001998 /* queue */
Sage Weilec302642009-12-22 10:43:42 -08001999 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002000 BUG_ON(!list_empty(&msg->list_head));
2001 list_add_tail(&msg->list_head, &con->out_queue);
2002 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
2003 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
2004 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2005 le32_to_cpu(msg->hdr.front_len),
2006 le32_to_cpu(msg->hdr.middle_len),
2007 le32_to_cpu(msg->hdr.data_len));
Sage Weilec302642009-12-22 10:43:42 -08002008 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002009
2010 /* if there wasn't anything waiting to send before, queue
2011 * new work */
2012 if (test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2013 queue_con(con);
2014}
2015
2016/*
2017 * Revoke a message that was previously queued for send
2018 */
2019void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg)
2020{
Sage Weilec302642009-12-22 10:43:42 -08002021 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002022 if (!list_empty(&msg->list_head)) {
2023 dout("con_revoke %p msg %p\n", con, msg);
2024 list_del_init(&msg->list_head);
2025 ceph_msg_put(msg);
2026 msg->hdr.seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -08002027 if (con->out_msg == msg) {
2028 ceph_msg_put(con->out_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002029 con->out_msg = NULL;
Sage Weilc86a2932009-12-14 14:04:30 -08002030 }
Sage Weil31b80062009-10-06 11:31:13 -07002031 if (con->out_kvec_is_msg) {
2032 con->out_skip = con->out_kvec_bytes;
2033 con->out_kvec_is_msg = false;
2034 }
2035 } else {
2036 dout("con_revoke %p msg %p - not queued (sent?)\n", con, msg);
2037 }
Sage Weilec302642009-12-22 10:43:42 -08002038 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002039}
2040
2041/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002042 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08002043 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002044void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08002045{
2046 mutex_lock(&con->mutex);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002047 if (con->in_msg && con->in_msg == msg) {
2048 unsigned front_len = le32_to_cpu(con->in_hdr.front_len);
2049 unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len);
Sage Weil350b1c32009-12-22 10:45:45 -08002050 unsigned data_len = le32_to_cpu(con->in_hdr.data_len);
2051
2052 /* skip rest of message */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002053 dout("con_revoke_pages %p msg %p revoked\n", con, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002054 con->in_base_pos = con->in_base_pos -
2055 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002056 front_len -
2057 middle_len -
2058 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08002059 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08002060 ceph_msg_put(con->in_msg);
2061 con->in_msg = NULL;
2062 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002063 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08002064 } else {
2065 dout("con_revoke_pages %p msg %p pages %p no-op\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08002066 con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08002067 }
2068 mutex_unlock(&con->mutex);
2069}
2070
2071/*
Sage Weil31b80062009-10-06 11:31:13 -07002072 * Queue a keepalive byte to ensure the tcp connection is alive.
2073 */
2074void ceph_con_keepalive(struct ceph_connection *con)
2075{
2076 if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 &&
2077 test_and_set_bit(WRITE_PENDING, &con->state) == 0)
2078 queue_con(con);
2079}
2080
2081
2082/*
2083 * construct a new message with given type, size
2084 * the new msg has a ref count of 1.
2085 */
Sage Weilbb257662010-04-01 16:07:23 -07002086struct ceph_msg *ceph_msg_new(int type, int front_len)
Sage Weil31b80062009-10-06 11:31:13 -07002087{
2088 struct ceph_msg *m;
2089
2090 m = kmalloc(sizeof(*m), GFP_NOFS);
2091 if (m == NULL)
2092 goto out;
Sage Weilc2e552e2009-12-07 15:55:05 -08002093 kref_init(&m->kref);
Sage Weil31b80062009-10-06 11:31:13 -07002094 INIT_LIST_HEAD(&m->list_head);
2095
Sage Weil45c6ceb2010-05-11 15:01:51 -07002096 m->hdr.tid = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002097 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07002098 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
2099 m->hdr.version = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002100 m->hdr.front_len = cpu_to_le32(front_len);
2101 m->hdr.middle_len = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002102 m->hdr.data_len = 0;
2103 m->hdr.data_off = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002104 m->hdr.reserved = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002105 m->footer.front_crc = 0;
2106 m->footer.middle_crc = 0;
2107 m->footer.data_crc = 0;
Sage Weil45c6ceb2010-05-11 15:01:51 -07002108 m->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07002109 m->front_max = front_len;
2110 m->front_is_vmalloc = false;
2111 m->more_to_follow = false;
2112 m->pool = NULL;
2113
2114 /* front */
2115 if (front_len) {
2116 if (front_len > PAGE_CACHE_SIZE) {
2117 m->front.iov_base = __vmalloc(front_len, GFP_NOFS,
2118 PAGE_KERNEL);
2119 m->front_is_vmalloc = true;
2120 } else {
2121 m->front.iov_base = kmalloc(front_len, GFP_NOFS);
2122 }
2123 if (m->front.iov_base == NULL) {
2124 pr_err("msg_new can't allocate %d bytes\n",
2125 front_len);
2126 goto out2;
2127 }
2128 } else {
2129 m->front.iov_base = NULL;
2130 }
2131 m->front.iov_len = front_len;
2132
2133 /* middle */
2134 m->middle = NULL;
2135
2136 /* data */
Sage Weilbb257662010-04-01 16:07:23 -07002137 m->nr_pages = 0;
2138 m->pages = NULL;
Sage Weil58bb3b32009-12-23 12:12:31 -08002139 m->pagelist = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002140
Sage Weilbb257662010-04-01 16:07:23 -07002141 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07002142 return m;
2143
2144out2:
2145 ceph_msg_put(m);
2146out:
Sage Weilbb257662010-04-01 16:07:23 -07002147 pr_err("msg_new can't create type %d front %d\n", type, front_len);
Sage Weila79832f2010-04-01 16:06:19 -07002148 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002149}
2150
2151/*
Sage Weil31b80062009-10-06 11:31:13 -07002152 * Allocate "middle" portion of a message, if it is needed and wasn't
2153 * allocated by alloc_msg. This allows us to read a small fixed-size
2154 * per-type header in the front and then gracefully fail (i.e.,
2155 * propagate the error to the caller based on info in the front) when
2156 * the middle is too large.
2157 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002158static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07002159{
2160 int type = le16_to_cpu(msg->hdr.type);
2161 int middle_len = le32_to_cpu(msg->hdr.middle_len);
2162
2163 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
2164 ceph_msg_type_name(type), middle_len);
2165 BUG_ON(!middle_len);
2166 BUG_ON(msg->middle);
2167
Sage Weilb6c1d5b2009-12-07 12:17:17 -08002168 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07002169 if (!msg->middle)
2170 return -ENOMEM;
2171 return 0;
2172}
2173
Yehuda Sadeh24504182010-01-08 13:58:34 -08002174/*
2175 * Generic message allocator, for incoming messages.
2176 */
2177static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
2178 struct ceph_msg_header *hdr,
2179 int *skip)
2180{
2181 int type = le16_to_cpu(hdr->type);
2182 int front_len = le32_to_cpu(hdr->front_len);
2183 int middle_len = le32_to_cpu(hdr->middle_len);
2184 struct ceph_msg *msg = NULL;
2185 int ret;
2186
2187 if (con->ops->alloc_msg) {
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002188 mutex_unlock(&con->mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002189 msg = con->ops->alloc_msg(con, hdr, skip);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002190 mutex_lock(&con->mutex);
Sage Weila79832f2010-04-01 16:06:19 -07002191 if (!msg || *skip)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002192 return NULL;
2193 }
2194 if (!msg) {
2195 *skip = 0;
Sage Weilbb257662010-04-01 16:07:23 -07002196 msg = ceph_msg_new(type, front_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002197 if (!msg) {
2198 pr_err("unable to allocate msg type %d len %d\n",
2199 type, front_len);
Sage Weila79832f2010-04-01 16:06:19 -07002200 return NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002201 }
2202 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002203 memcpy(&msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08002204
Sage Weilbb257662010-04-01 16:07:23 -07002205 if (middle_len && !msg->middle) {
Yehuda Sadeh24504182010-01-08 13:58:34 -08002206 ret = ceph_alloc_middle(con, msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002207 if (ret < 0) {
2208 ceph_msg_put(msg);
Sage Weila79832f2010-04-01 16:06:19 -07002209 return NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002210 }
2211 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002212
Yehuda Sadeh24504182010-01-08 13:58:34 -08002213 return msg;
2214}
2215
Sage Weil31b80062009-10-06 11:31:13 -07002216
2217/*
2218 * Free a generically kmalloc'd message.
2219 */
2220void ceph_msg_kfree(struct ceph_msg *m)
2221{
2222 dout("msg_kfree %p\n", m);
2223 if (m->front_is_vmalloc)
2224 vfree(m->front.iov_base);
2225 else
2226 kfree(m->front.iov_base);
2227 kfree(m);
2228}
2229
2230/*
2231 * Drop a msg ref. Destroy as needed.
2232 */
Sage Weilc2e552e2009-12-07 15:55:05 -08002233void ceph_msg_last_put(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07002234{
Sage Weilc2e552e2009-12-07 15:55:05 -08002235 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Sage Weil31b80062009-10-06 11:31:13 -07002236
Sage Weilc2e552e2009-12-07 15:55:05 -08002237 dout("ceph_msg_put last one on %p\n", m);
2238 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07002239
Sage Weilc2e552e2009-12-07 15:55:05 -08002240 /* drop middle, data, if any */
2241 if (m->middle) {
2242 ceph_buffer_put(m->middle);
2243 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07002244 }
Sage Weilc2e552e2009-12-07 15:55:05 -08002245 m->nr_pages = 0;
2246 m->pages = NULL;
2247
Sage Weil58bb3b32009-12-23 12:12:31 -08002248 if (m->pagelist) {
2249 ceph_pagelist_release(m->pagelist);
2250 kfree(m->pagelist);
2251 m->pagelist = NULL;
2252 }
2253
Sage Weilc2e552e2009-12-07 15:55:05 -08002254 if (m->pool)
2255 ceph_msgpool_put(m->pool, m);
2256 else
2257 ceph_msg_kfree(m);
Sage Weil31b80062009-10-06 11:31:13 -07002258}
Sage Weil9ec7cab2009-12-14 15:13:47 -08002259
2260void ceph_msg_dump(struct ceph_msg *msg)
2261{
2262 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg,
2263 msg->front_max, msg->nr_pages);
2264 print_hex_dump(KERN_DEBUG, "header: ",
2265 DUMP_PREFIX_OFFSET, 16, 1,
2266 &msg->hdr, sizeof(msg->hdr), true);
2267 print_hex_dump(KERN_DEBUG, " front: ",
2268 DUMP_PREFIX_OFFSET, 16, 1,
2269 msg->front.iov_base, msg->front.iov_len, true);
2270 if (msg->middle)
2271 print_hex_dump(KERN_DEBUG, "middle: ",
2272 DUMP_PREFIX_OFFSET, 16, 1,
2273 msg->middle->vec.iov_base,
2274 msg->middle->vec.iov_len, true);
2275 print_hex_dump(KERN_DEBUG, "footer: ",
2276 DUMP_PREFIX_OFFSET, 16, 1,
2277 &msg->footer, sizeof(msg->footer), true);
2278}