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