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