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