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