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