]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
3d14c5d2 | 2 | #include <linux/ceph/ceph_debug.h> |
31b8006e SW |
3 | |
4 | #include <linux/crc32c.h> | |
5 | #include <linux/ctype.h> | |
6 | #include <linux/highmem.h> | |
7 | #include <linux/inet.h> | |
8 | #include <linux/kthread.h> | |
9 | #include <linux/net.h> | |
757856d2 | 10 | #include <linux/nsproxy.h> |
633ee407 | 11 | #include <linux/sched/mm.h> |
5a0e3ad6 | 12 | #include <linux/slab.h> |
31b8006e SW |
13 | #include <linux/socket.h> |
14 | #include <linux/string.h> | |
3ebc21f7 | 15 | #ifdef CONFIG_BLOCK |
68b4476b | 16 | #include <linux/bio.h> |
3ebc21f7 | 17 | #endif /* CONFIG_BLOCK */ |
ee3b56f2 | 18 | #include <linux/dns_resolver.h> |
31b8006e SW |
19 | #include <net/tcp.h> |
20 | ||
2b3e0c90 | 21 | #include <linux/ceph/ceph_features.h> |
3d14c5d2 YS |
22 | #include <linux/ceph/libceph.h> |
23 | #include <linux/ceph/messenger.h> | |
24 | #include <linux/ceph/decode.h> | |
25 | #include <linux/ceph/pagelist.h> | |
bc3b2d7f | 26 | #include <linux/export.h> |
31b8006e SW |
27 | |
28 | /* | |
29 | * Ceph uses the messenger to exchange ceph_msg messages with other | |
30 | * hosts in the system. The messenger provides ordered and reliable | |
31 | * delivery. We tolerate TCP disconnects by reconnecting (with | |
32 | * exponential backoff) in the case of a fault (disconnection, bad | |
33 | * crc, protocol error). Acks allow sent messages to be discarded by | |
34 | * the sender. | |
35 | */ | |
36 | ||
bc18f4b1 AE |
37 | /* |
38 | * We track the state of the socket on a given connection using | |
39 | * values defined below. The transition to a new socket state is | |
40 | * handled by a function which verifies we aren't coming from an | |
41 | * unexpected state. | |
42 | * | |
43 | * -------- | |
44 | * | NEW* | transient initial state | |
45 | * -------- | |
46 | * | con_sock_state_init() | |
47 | * v | |
48 | * ---------- | |
49 | * | CLOSED | initialized, but no socket (and no | |
50 | * ---------- TCP connection) | |
51 | * ^ \ | |
52 | * | \ con_sock_state_connecting() | |
53 | * | ---------------------- | |
54 | * | \ | |
55 | * + con_sock_state_closed() \ | |
fbb85a47 SW |
56 | * |+--------------------------- \ |
57 | * | \ \ \ | |
58 | * | ----------- \ \ | |
59 | * | | CLOSING | socket event; \ \ | |
60 | * | ----------- await close \ \ | |
61 | * | ^ \ | | |
62 | * | | \ | | |
63 | * | + con_sock_state_closing() \ | | |
64 | * | / \ | | | |
65 | * | / --------------- | | | |
66 | * | / \ v v | |
bc18f4b1 AE |
67 | * | / -------------- |
68 | * | / -----------------| CONNECTING | socket created, TCP | |
69 | * | | / -------------- connect initiated | |
70 | * | | | con_sock_state_connected() | |
71 | * | | v | |
72 | * ------------- | |
73 | * | CONNECTED | TCP connection established | |
74 | * ------------- | |
75 | * | |
76 | * State values for ceph_connection->sock_state; NEW is assumed to be 0. | |
77 | */ | |
ce2c8903 AE |
78 | |
79 | #define CON_SOCK_STATE_NEW 0 /* -> CLOSED */ | |
80 | #define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */ | |
81 | #define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */ | |
82 | #define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */ | |
83 | #define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */ | |
84 | ||
8dacc7da SW |
85 | /* |
86 | * connection states | |
87 | */ | |
88 | #define CON_STATE_CLOSED 1 /* -> PREOPEN */ | |
89 | #define CON_STATE_PREOPEN 2 /* -> CONNECTING, CLOSED */ | |
90 | #define CON_STATE_CONNECTING 3 /* -> NEGOTIATING, CLOSED */ | |
91 | #define CON_STATE_NEGOTIATING 4 /* -> OPEN, CLOSED */ | |
92 | #define CON_STATE_OPEN 5 /* -> STANDBY, CLOSED */ | |
93 | #define CON_STATE_STANDBY 6 /* -> PREOPEN, CLOSED */ | |
94 | ||
4a861692 SW |
95 | /* |
96 | * ceph_connection flag bits | |
97 | */ | |
98 | #define CON_FLAG_LOSSYTX 0 /* we can close channel or drop | |
99 | * messages on errors */ | |
100 | #define CON_FLAG_KEEPALIVE_PENDING 1 /* we need to send a keepalive */ | |
101 | #define CON_FLAG_WRITE_PENDING 2 /* we have data ready to send */ | |
102 | #define CON_FLAG_SOCK_CLOSED 3 /* socket state changed to closed */ | |
103 | #define CON_FLAG_BACKOFF 4 /* need to retry queuing delayed work */ | |
8dacc7da | 104 | |
c9ffc77a AE |
105 | static bool con_flag_valid(unsigned long con_flag) |
106 | { | |
107 | switch (con_flag) { | |
108 | case CON_FLAG_LOSSYTX: | |
109 | case CON_FLAG_KEEPALIVE_PENDING: | |
110 | case CON_FLAG_WRITE_PENDING: | |
111 | case CON_FLAG_SOCK_CLOSED: | |
112 | case CON_FLAG_BACKOFF: | |
113 | return true; | |
114 | default: | |
115 | return false; | |
116 | } | |
117 | } | |
118 | ||
119 | static void con_flag_clear(struct ceph_connection *con, unsigned long con_flag) | |
120 | { | |
121 | BUG_ON(!con_flag_valid(con_flag)); | |
122 | ||
123 | clear_bit(con_flag, &con->flags); | |
124 | } | |
125 | ||
126 | static void con_flag_set(struct ceph_connection *con, unsigned long con_flag) | |
127 | { | |
128 | BUG_ON(!con_flag_valid(con_flag)); | |
129 | ||
130 | set_bit(con_flag, &con->flags); | |
131 | } | |
132 | ||
133 | static bool con_flag_test(struct ceph_connection *con, unsigned long con_flag) | |
134 | { | |
135 | BUG_ON(!con_flag_valid(con_flag)); | |
136 | ||
137 | return test_bit(con_flag, &con->flags); | |
138 | } | |
139 | ||
140 | static bool con_flag_test_and_clear(struct ceph_connection *con, | |
141 | unsigned long con_flag) | |
142 | { | |
143 | BUG_ON(!con_flag_valid(con_flag)); | |
144 | ||
145 | return test_and_clear_bit(con_flag, &con->flags); | |
146 | } | |
147 | ||
148 | static bool con_flag_test_and_set(struct ceph_connection *con, | |
149 | unsigned long con_flag) | |
150 | { | |
151 | BUG_ON(!con_flag_valid(con_flag)); | |
152 | ||
153 | return test_and_set_bit(con_flag, &con->flags); | |
154 | } | |
155 | ||
e3d5d638 AE |
156 | /* Slab caches for frequently-allocated structures */ |
157 | ||
158 | static struct kmem_cache *ceph_msg_cache; | |
159 | ||
31b8006e SW |
160 | /* static tag bytes (protocol control messages) */ |
161 | static char tag_msg = CEPH_MSGR_TAG_MSG; | |
162 | static char tag_ack = CEPH_MSGR_TAG_ACK; | |
163 | static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE; | |
8b9558aa | 164 | static char tag_keepalive2 = CEPH_MSGR_TAG_KEEPALIVE2; |
31b8006e | 165 | |
a6a5349d SW |
166 | #ifdef CONFIG_LOCKDEP |
167 | static struct lock_class_key socket_class; | |
168 | #endif | |
169 | ||
31b8006e | 170 | static void queue_con(struct ceph_connection *con); |
37ab77ac | 171 | static void cancel_con(struct ceph_connection *con); |
68931622 | 172 | static void ceph_con_workfn(struct work_struct *); |
93209264 | 173 | static void con_fault(struct ceph_connection *con); |
31b8006e | 174 | |
31b8006e | 175 | /* |
f64a9317 AE |
176 | * Nicely render a sockaddr as a string. An array of formatted |
177 | * strings is used, to approximate reentrancy. | |
31b8006e | 178 | */ |
f64a9317 AE |
179 | #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */ |
180 | #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG) | |
181 | #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1) | |
182 | #define MAX_ADDR_STR_LEN 64 /* 54 is enough */ | |
183 | ||
184 | static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN]; | |
185 | static atomic_t addr_str_seq = ATOMIC_INIT(0); | |
31b8006e | 186 | |
57666519 | 187 | static struct page *zero_page; /* used in certain error cases */ |
57666519 | 188 | |
3d14c5d2 | 189 | const char *ceph_pr_addr(const struct sockaddr_storage *ss) |
31b8006e SW |
190 | { |
191 | int i; | |
192 | char *s; | |
99f0f3b2 AE |
193 | struct sockaddr_in *in4 = (struct sockaddr_in *) ss; |
194 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss; | |
31b8006e | 195 | |
f64a9317 | 196 | i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK; |
31b8006e SW |
197 | s = addr_str[i]; |
198 | ||
199 | switch (ss->ss_family) { | |
200 | case AF_INET: | |
bd406145 AE |
201 | snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr, |
202 | ntohs(in4->sin_port)); | |
31b8006e SW |
203 | break; |
204 | ||
205 | case AF_INET6: | |
bd406145 AE |
206 | snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr, |
207 | ntohs(in6->sin6_port)); | |
31b8006e SW |
208 | break; |
209 | ||
210 | default: | |
d3002b97 AE |
211 | snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)", |
212 | ss->ss_family); | |
31b8006e SW |
213 | } |
214 | ||
215 | return s; | |
216 | } | |
3d14c5d2 | 217 | EXPORT_SYMBOL(ceph_pr_addr); |
31b8006e | 218 | |
63f2d211 SW |
219 | static void encode_my_addr(struct ceph_messenger *msgr) |
220 | { | |
221 | memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr)); | |
222 | ceph_encode_addr(&msgr->my_enc_addr); | |
223 | } | |
224 | ||
31b8006e SW |
225 | /* |
226 | * work queue for all reading and writing to/from the socket. | |
227 | */ | |
e0f43c94 | 228 | static struct workqueue_struct *ceph_msgr_wq; |
31b8006e | 229 | |
e3d5d638 AE |
230 | static int ceph_msgr_slab_init(void) |
231 | { | |
232 | BUG_ON(ceph_msg_cache); | |
5ee61e95 | 233 | ceph_msg_cache = KMEM_CACHE(ceph_msg, 0); |
81b36be4 AE |
234 | if (!ceph_msg_cache) |
235 | return -ENOMEM; | |
236 | ||
0d9c1ab3 | 237 | return 0; |
e3d5d638 AE |
238 | } |
239 | ||
240 | static void ceph_msgr_slab_exit(void) | |
241 | { | |
242 | BUG_ON(!ceph_msg_cache); | |
243 | kmem_cache_destroy(ceph_msg_cache); | |
244 | ceph_msg_cache = NULL; | |
245 | } | |
246 | ||
15417167 | 247 | static void _ceph_msgr_exit(void) |
6173d1f0 | 248 | { |
d3002b97 | 249 | if (ceph_msgr_wq) { |
6173d1f0 | 250 | destroy_workqueue(ceph_msgr_wq); |
d3002b97 AE |
251 | ceph_msgr_wq = NULL; |
252 | } | |
6173d1f0 | 253 | |
6173d1f0 | 254 | BUG_ON(zero_page == NULL); |
09cbfeaf | 255 | put_page(zero_page); |
6173d1f0 | 256 | zero_page = NULL; |
d920ff6f BC |
257 | |
258 | ceph_msgr_slab_exit(); | |
6173d1f0 AE |
259 | } |
260 | ||
57a35dfb | 261 | int __init ceph_msgr_init(void) |
31b8006e | 262 | { |
d920ff6f BC |
263 | if (ceph_msgr_slab_init()) |
264 | return -ENOMEM; | |
265 | ||
57666519 AE |
266 | BUG_ON(zero_page != NULL); |
267 | zero_page = ZERO_PAGE(0); | |
09cbfeaf | 268 | get_page(zero_page); |
57666519 | 269 | |
f9865f06 ID |
270 | /* |
271 | * The number of active work items is limited by the number of | |
272 | * connections, so leave @max_active at default. | |
273 | */ | |
274 | ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_MEM_RECLAIM, 0); | |
6173d1f0 AE |
275 | if (ceph_msgr_wq) |
276 | return 0; | |
57666519 | 277 | |
6173d1f0 AE |
278 | pr_err("msgr_init failed to create workqueue\n"); |
279 | _ceph_msgr_exit(); | |
57666519 | 280 | |
6173d1f0 | 281 | return -ENOMEM; |
31b8006e SW |
282 | } |
283 | ||
284 | void ceph_msgr_exit(void) | |
285 | { | |
57666519 | 286 | BUG_ON(ceph_msgr_wq == NULL); |
57666519 | 287 | |
6173d1f0 | 288 | _ceph_msgr_exit(); |
31b8006e SW |
289 | } |
290 | ||
cd84db6e | 291 | void ceph_msgr_flush(void) |
a922d38f SW |
292 | { |
293 | flush_workqueue(ceph_msgr_wq); | |
294 | } | |
3d14c5d2 | 295 | EXPORT_SYMBOL(ceph_msgr_flush); |
a922d38f | 296 | |
ce2c8903 AE |
297 | /* Connection socket state transition functions */ |
298 | ||
299 | static void con_sock_state_init(struct ceph_connection *con) | |
300 | { | |
301 | int old_state; | |
302 | ||
303 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED); | |
304 | if (WARN_ON(old_state != CON_SOCK_STATE_NEW)) | |
305 | printk("%s: unexpected old state %d\n", __func__, old_state); | |
8007b8d6 SW |
306 | dout("%s con %p sock %d -> %d\n", __func__, con, old_state, |
307 | CON_SOCK_STATE_CLOSED); | |
ce2c8903 AE |
308 | } |
309 | ||
310 | static void con_sock_state_connecting(struct ceph_connection *con) | |
311 | { | |
312 | int old_state; | |
313 | ||
314 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING); | |
315 | if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED)) | |
316 | printk("%s: unexpected old state %d\n", __func__, old_state); | |
8007b8d6 SW |
317 | dout("%s con %p sock %d -> %d\n", __func__, con, old_state, |
318 | CON_SOCK_STATE_CONNECTING); | |
ce2c8903 AE |
319 | } |
320 | ||
321 | static void con_sock_state_connected(struct ceph_connection *con) | |
322 | { | |
323 | int old_state; | |
324 | ||
325 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED); | |
326 | if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING)) | |
327 | printk("%s: unexpected old state %d\n", __func__, old_state); | |
8007b8d6 SW |
328 | dout("%s con %p sock %d -> %d\n", __func__, con, old_state, |
329 | CON_SOCK_STATE_CONNECTED); | |
ce2c8903 AE |
330 | } |
331 | ||
332 | static void con_sock_state_closing(struct ceph_connection *con) | |
333 | { | |
334 | int old_state; | |
335 | ||
336 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING); | |
337 | if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING && | |
338 | old_state != CON_SOCK_STATE_CONNECTED && | |
339 | old_state != CON_SOCK_STATE_CLOSING)) | |
340 | printk("%s: unexpected old state %d\n", __func__, old_state); | |
8007b8d6 SW |
341 | dout("%s con %p sock %d -> %d\n", __func__, con, old_state, |
342 | CON_SOCK_STATE_CLOSING); | |
ce2c8903 AE |
343 | } |
344 | ||
345 | static void con_sock_state_closed(struct ceph_connection *con) | |
346 | { | |
347 | int old_state; | |
348 | ||
349 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED); | |
350 | if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED && | |
fbb85a47 | 351 | old_state != CON_SOCK_STATE_CLOSING && |
8007b8d6 SW |
352 | old_state != CON_SOCK_STATE_CONNECTING && |
353 | old_state != CON_SOCK_STATE_CLOSED)) | |
ce2c8903 | 354 | printk("%s: unexpected old state %d\n", __func__, old_state); |
8007b8d6 SW |
355 | dout("%s con %p sock %d -> %d\n", __func__, con, old_state, |
356 | CON_SOCK_STATE_CLOSED); | |
ce2c8903 | 357 | } |
a922d38f | 358 | |
31b8006e SW |
359 | /* |
360 | * socket callback functions | |
361 | */ | |
362 | ||
363 | /* data available on socket, or listen socket received a connect */ | |
676d2369 | 364 | static void ceph_sock_data_ready(struct sock *sk) |
31b8006e | 365 | { |
bd406145 | 366 | struct ceph_connection *con = sk->sk_user_data; |
a2a32584 GH |
367 | if (atomic_read(&con->msgr->stopping)) { |
368 | return; | |
369 | } | |
bd406145 | 370 | |
31b8006e | 371 | if (sk->sk_state != TCP_CLOSE_WAIT) { |
327800bd | 372 | dout("%s on %p state = %lu, queueing work\n", __func__, |
31b8006e SW |
373 | con, con->state); |
374 | queue_con(con); | |
375 | } | |
376 | } | |
377 | ||
378 | /* socket has buffer space for writing */ | |
327800bd | 379 | static void ceph_sock_write_space(struct sock *sk) |
31b8006e | 380 | { |
d3002b97 | 381 | struct ceph_connection *con = sk->sk_user_data; |
31b8006e | 382 | |
182fac26 JS |
383 | /* only queue to workqueue if there is data we want to write, |
384 | * and there is sufficient space in the socket buffer to accept | |
327800bd | 385 | * more data. clear SOCK_NOSPACE so that ceph_sock_write_space() |
182fac26 JS |
386 | * doesn't get called again until try_write() fills the socket |
387 | * buffer. See net/ipv4/tcp_input.c:tcp_check_space() | |
388 | * and net/core/stream.c:sk_stream_write_space(). | |
389 | */ | |
c9ffc77a | 390 | if (con_flag_test(con, CON_FLAG_WRITE_PENDING)) { |
64dc6130 | 391 | if (sk_stream_is_writeable(sk)) { |
327800bd | 392 | dout("%s %p queueing write work\n", __func__, con); |
182fac26 JS |
393 | clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
394 | queue_con(con); | |
395 | } | |
31b8006e | 396 | } else { |
327800bd | 397 | dout("%s %p nothing to write\n", __func__, con); |
31b8006e | 398 | } |
31b8006e SW |
399 | } |
400 | ||
401 | /* socket's state has changed */ | |
327800bd | 402 | static void ceph_sock_state_change(struct sock *sk) |
31b8006e | 403 | { |
bd406145 | 404 | struct ceph_connection *con = sk->sk_user_data; |
31b8006e | 405 | |
327800bd | 406 | dout("%s %p state = %lu sk_state = %u\n", __func__, |
31b8006e SW |
407 | con, con->state, sk->sk_state); |
408 | ||
31b8006e SW |
409 | switch (sk->sk_state) { |
410 | case TCP_CLOSE: | |
327800bd | 411 | dout("%s TCP_CLOSE\n", __func__); |
18370b36 | 412 | /* fall through */ |
31b8006e | 413 | case TCP_CLOSE_WAIT: |
327800bd | 414 | dout("%s TCP_CLOSE_WAIT\n", __func__); |
ce2c8903 | 415 | con_sock_state_closing(con); |
c9ffc77a | 416 | con_flag_set(con, CON_FLAG_SOCK_CLOSED); |
d65c9e0b | 417 | queue_con(con); |
31b8006e SW |
418 | break; |
419 | case TCP_ESTABLISHED: | |
327800bd | 420 | dout("%s TCP_ESTABLISHED\n", __func__); |
ce2c8903 | 421 | con_sock_state_connected(con); |
31b8006e SW |
422 | queue_con(con); |
423 | break; | |
d3002b97 AE |
424 | default: /* Everything else is uninteresting */ |
425 | break; | |
31b8006e SW |
426 | } |
427 | } | |
428 | ||
429 | /* | |
430 | * set up socket callbacks | |
431 | */ | |
432 | static void set_sock_callbacks(struct socket *sock, | |
433 | struct ceph_connection *con) | |
434 | { | |
435 | struct sock *sk = sock->sk; | |
bd406145 | 436 | sk->sk_user_data = con; |
327800bd AE |
437 | sk->sk_data_ready = ceph_sock_data_ready; |
438 | sk->sk_write_space = ceph_sock_write_space; | |
439 | sk->sk_state_change = ceph_sock_state_change; | |
31b8006e SW |
440 | } |
441 | ||
442 | ||
443 | /* | |
444 | * socket helpers | |
445 | */ | |
446 | ||
447 | /* | |
448 | * initiate connection to a remote socket. | |
449 | */ | |
41617d0c | 450 | static int ceph_tcp_connect(struct ceph_connection *con) |
31b8006e | 451 | { |
f91d3471 | 452 | struct sockaddr_storage *paddr = &con->peer_addr.in_addr; |
31b8006e | 453 | struct socket *sock; |
633ee407 | 454 | unsigned int noio_flag; |
31b8006e SW |
455 | int ret; |
456 | ||
457 | BUG_ON(con->sock); | |
633ee407 ID |
458 | |
459 | /* sock_create_kern() allocates with GFP_KERNEL */ | |
460 | noio_flag = memalloc_noio_save(); | |
757856d2 | 461 | ret = sock_create_kern(read_pnet(&con->msgr->net), paddr->ss_family, |
eeb1bd5c | 462 | SOCK_STREAM, IPPROTO_TCP, &sock); |
633ee407 | 463 | memalloc_noio_restore(noio_flag); |
31b8006e | 464 | if (ret) |
41617d0c | 465 | return ret; |
6d7fdb0a | 466 | sock->sk->sk_allocation = GFP_NOFS; |
31b8006e | 467 | |
a6a5349d SW |
468 | #ifdef CONFIG_LOCKDEP |
469 | lockdep_set_class(&sock->sk->sk_lock, &socket_class); | |
470 | #endif | |
471 | ||
31b8006e SW |
472 | set_sock_callbacks(sock, con); |
473 | ||
3d14c5d2 | 474 | dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr)); |
31b8006e | 475 | |
89a86be0 | 476 | con_sock_state_connecting(con); |
f91d3471 SW |
477 | ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr), |
478 | O_NONBLOCK); | |
31b8006e SW |
479 | if (ret == -EINPROGRESS) { |
480 | dout("connect %s EINPROGRESS sk_state = %u\n", | |
3d14c5d2 | 481 | ceph_pr_addr(&con->peer_addr.in_addr), |
31b8006e | 482 | sock->sk->sk_state); |
a5bc3129 | 483 | } else if (ret < 0) { |
31b8006e | 484 | pr_err("connect %s error %d\n", |
3d14c5d2 | 485 | ceph_pr_addr(&con->peer_addr.in_addr), ret); |
31b8006e | 486 | sock_release(sock); |
41617d0c | 487 | return ret; |
a5bc3129 | 488 | } |
89baaa57 | 489 | |
859bff51 | 490 | if (ceph_test_opt(from_msgr(con->msgr), TCP_NODELAY)) { |
ba988f87 CH |
491 | int optval = 1; |
492 | ||
493 | ret = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, | |
494 | (char *)&optval, sizeof(optval)); | |
495 | if (ret) | |
496 | pr_err("kernel_setsockopt(TCP_NODELAY) failed: %d", | |
497 | ret); | |
498 | } | |
499 | ||
a5bc3129 | 500 | con->sock = sock; |
41617d0c | 501 | return 0; |
31b8006e SW |
502 | } |
503 | ||
e5c93883 ID |
504 | /* |
505 | * If @buf is NULL, discard up to @len bytes. | |
506 | */ | |
31b8006e SW |
507 | static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len) |
508 | { | |
509 | struct kvec iov = {buf, len}; | |
510 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; | |
98bdb0aa | 511 | int r; |
31b8006e | 512 | |
e5c93883 ID |
513 | if (!buf) |
514 | msg.msg_flags |= MSG_TRUNC; | |
515 | ||
aa563d7b | 516 | iov_iter_kvec(&msg.msg_iter, READ, &iov, 1, len); |
100803a8 | 517 | r = sock_recvmsg(sock, &msg, msg.msg_flags); |
98bdb0aa SW |
518 | if (r == -EAGAIN) |
519 | r = 0; | |
520 | return r; | |
31b8006e SW |
521 | } |
522 | ||
afb3d90e AE |
523 | static int ceph_tcp_recvpage(struct socket *sock, struct page *page, |
524 | int page_offset, size_t length) | |
525 | { | |
100803a8 AV |
526 | struct bio_vec bvec = { |
527 | .bv_page = page, | |
528 | .bv_offset = page_offset, | |
529 | .bv_len = length | |
530 | }; | |
531 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; | |
532 | int r; | |
afb3d90e AE |
533 | |
534 | BUG_ON(page_offset + length > PAGE_SIZE); | |
aa563d7b | 535 | iov_iter_bvec(&msg.msg_iter, READ, &bvec, 1, length); |
100803a8 AV |
536 | r = sock_recvmsg(sock, &msg, msg.msg_flags); |
537 | if (r == -EAGAIN) | |
538 | r = 0; | |
539 | return r; | |
afb3d90e AE |
540 | } |
541 | ||
31b8006e SW |
542 | /* |
543 | * write something. @more is true if caller will be sending more data | |
544 | * shortly. | |
545 | */ | |
546 | static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov, | |
547 | size_t kvlen, size_t len, int more) | |
548 | { | |
549 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; | |
42961d23 | 550 | int r; |
31b8006e SW |
551 | |
552 | if (more) | |
553 | msg.msg_flags |= MSG_MORE; | |
554 | else | |
555 | msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */ | |
556 | ||
42961d23 SW |
557 | r = kernel_sendmsg(sock, &msg, iov, kvlen, len); |
558 | if (r == -EAGAIN) | |
559 | r = 0; | |
560 | return r; | |
31b8006e SW |
561 | } |
562 | ||
178eda29 | 563 | static int __ceph_tcp_sendpage(struct socket *sock, struct page *page, |
e1dcb128 | 564 | int offset, size_t size, bool more) |
31739139 AE |
565 | { |
566 | int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR); | |
567 | int ret; | |
568 | ||
569 | ret = kernel_sendpage(sock, page, offset, size, flags); | |
570 | if (ret == -EAGAIN) | |
571 | ret = 0; | |
572 | ||
573 | return ret; | |
574 | } | |
575 | ||
178eda29 CC |
576 | static int ceph_tcp_sendpage(struct socket *sock, struct page *page, |
577 | int offset, size_t size, bool more) | |
578 | { | |
61ff6e9b AV |
579 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; |
580 | struct bio_vec bvec; | |
178eda29 | 581 | int ret; |
178eda29 | 582 | |
7e241f64 ID |
583 | /* |
584 | * sendpage cannot properly handle pages with page_count == 0, | |
585 | * we need to fall back to sendmsg if that's the case. | |
586 | * | |
587 | * Same goes for slab pages: skb_can_coalesce() allows | |
588 | * coalescing neighboring slab objects into a single frag which | |
589 | * triggers one of hardened usercopy checks. | |
590 | */ | |
591 | if (page_count(page) >= 1 && !PageSlab(page)) | |
178eda29 CC |
592 | return __ceph_tcp_sendpage(sock, page, offset, size, more); |
593 | ||
61ff6e9b AV |
594 | bvec.bv_page = page; |
595 | bvec.bv_offset = offset; | |
596 | bvec.bv_len = size; | |
597 | ||
598 | if (more) | |
599 | msg.msg_flags |= MSG_MORE; | |
600 | else | |
601 | msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */ | |
602 | ||
aa563d7b | 603 | iov_iter_bvec(&msg.msg_iter, WRITE, &bvec, 1, size); |
61ff6e9b AV |
604 | ret = sock_sendmsg(sock, &msg); |
605 | if (ret == -EAGAIN) | |
606 | ret = 0; | |
178eda29 CC |
607 | |
608 | return ret; | |
609 | } | |
31b8006e SW |
610 | |
611 | /* | |
612 | * Shutdown/close the socket for the given connection. | |
613 | */ | |
614 | static int con_close_socket(struct ceph_connection *con) | |
615 | { | |
8007b8d6 | 616 | int rc = 0; |
31b8006e SW |
617 | |
618 | dout("con_close_socket on %p sock %p\n", con, con->sock); | |
8007b8d6 SW |
619 | if (con->sock) { |
620 | rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR); | |
621 | sock_release(con->sock); | |
622 | con->sock = NULL; | |
623 | } | |
456ea468 AE |
624 | |
625 | /* | |
4a861692 | 626 | * Forcibly clear the SOCK_CLOSED flag. It gets set |
456ea468 AE |
627 | * independent of the connection mutex, and we could have |
628 | * received a socket close event before we had the chance to | |
629 | * shut the socket down. | |
630 | */ | |
c9ffc77a | 631 | con_flag_clear(con, CON_FLAG_SOCK_CLOSED); |
8007b8d6 | 632 | |
ce2c8903 | 633 | con_sock_state_closed(con); |
31b8006e SW |
634 | return rc; |
635 | } | |
636 | ||
637 | /* | |
638 | * Reset a connection. Discard all incoming and outgoing messages | |
639 | * and clear *_seq state. | |
640 | */ | |
641 | static void ceph_msg_remove(struct ceph_msg *msg) | |
642 | { | |
643 | list_del_init(&msg->list_head); | |
38941f80 | 644 | |
31b8006e SW |
645 | ceph_msg_put(msg); |
646 | } | |
647 | static void ceph_msg_remove_list(struct list_head *head) | |
648 | { | |
649 | while (!list_empty(head)) { | |
650 | struct ceph_msg *msg = list_first_entry(head, struct ceph_msg, | |
651 | list_head); | |
652 | ceph_msg_remove(msg); | |
653 | } | |
654 | } | |
655 | ||
656 | static void reset_connection(struct ceph_connection *con) | |
657 | { | |
658 | /* reset connection, out_queue, msg_ and connect_seq */ | |
659 | /* discard existing out_queue and msg_seq */ | |
0fa6ebc6 | 660 | dout("reset_connection %p\n", con); |
31b8006e SW |
661 | ceph_msg_remove_list(&con->out_queue); |
662 | ceph_msg_remove_list(&con->out_sent); | |
663 | ||
cf3e5c40 | 664 | if (con->in_msg) { |
38941f80 | 665 | BUG_ON(con->in_msg->con != con); |
cf3e5c40 SW |
666 | ceph_msg_put(con->in_msg); |
667 | con->in_msg = NULL; | |
668 | } | |
669 | ||
31b8006e SW |
670 | con->connect_seq = 0; |
671 | con->out_seq = 0; | |
c86a2930 | 672 | if (con->out_msg) { |
583d0fef | 673 | BUG_ON(con->out_msg->con != con); |
c86a2930 SW |
674 | ceph_msg_put(con->out_msg); |
675 | con->out_msg = NULL; | |
676 | } | |
31b8006e | 677 | con->in_seq = 0; |
0e0d5e0c | 678 | con->in_seq_acked = 0; |
67645d76 ID |
679 | |
680 | con->out_skip = 0; | |
31b8006e SW |
681 | } |
682 | ||
683 | /* | |
684 | * mark a peer down. drop any open connections. | |
685 | */ | |
686 | void ceph_con_close(struct ceph_connection *con) | |
687 | { | |
8c50c817 | 688 | mutex_lock(&con->mutex); |
3d14c5d2 YS |
689 | dout("con_close %p peer %s\n", con, |
690 | ceph_pr_addr(&con->peer_addr.in_addr)); | |
8dacc7da | 691 | con->state = CON_STATE_CLOSED; |
a5988c49 | 692 | |
c9ffc77a AE |
693 | con_flag_clear(con, CON_FLAG_LOSSYTX); /* so we retry next connect */ |
694 | con_flag_clear(con, CON_FLAG_KEEPALIVE_PENDING); | |
695 | con_flag_clear(con, CON_FLAG_WRITE_PENDING); | |
696 | con_flag_clear(con, CON_FLAG_BACKOFF); | |
a5988c49 | 697 | |
31b8006e | 698 | reset_connection(con); |
6f2bc3ff | 699 | con->peer_global_seq = 0; |
37ab77ac | 700 | cancel_con(con); |
ee76e073 | 701 | con_close_socket(con); |
ec302645 | 702 | mutex_unlock(&con->mutex); |
31b8006e | 703 | } |
3d14c5d2 | 704 | EXPORT_SYMBOL(ceph_con_close); |
31b8006e | 705 | |
31b8006e SW |
706 | /* |
707 | * Reopen a closed connection, with a new peer address. | |
708 | */ | |
b7a9e5dd SW |
709 | void ceph_con_open(struct ceph_connection *con, |
710 | __u8 entity_type, __u64 entity_num, | |
711 | struct ceph_entity_addr *addr) | |
31b8006e | 712 | { |
5469155f | 713 | mutex_lock(&con->mutex); |
3d14c5d2 | 714 | dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr)); |
8dacc7da | 715 | |
122070a2 | 716 | WARN_ON(con->state != CON_STATE_CLOSED); |
8dacc7da | 717 | con->state = CON_STATE_PREOPEN; |
a5988c49 | 718 | |
b7a9e5dd SW |
719 | con->peer_name.type = (__u8) entity_type; |
720 | con->peer_name.num = cpu_to_le64(entity_num); | |
721 | ||
31b8006e | 722 | memcpy(&con->peer_addr, addr, sizeof(*addr)); |
03c677e1 | 723 | con->delay = 0; /* reset backoff memory */ |
5469155f | 724 | mutex_unlock(&con->mutex); |
31b8006e SW |
725 | queue_con(con); |
726 | } | |
3d14c5d2 | 727 | EXPORT_SYMBOL(ceph_con_open); |
31b8006e | 728 | |
87b315a5 SW |
729 | /* |
730 | * return true if this connection ever successfully opened | |
731 | */ | |
732 | bool ceph_con_opened(struct ceph_connection *con) | |
733 | { | |
734 | return con->connect_seq > 0; | |
735 | } | |
736 | ||
31b8006e SW |
737 | /* |
738 | * initialize a new connection. | |
739 | */ | |
1bfd89f4 AE |
740 | void ceph_con_init(struct ceph_connection *con, void *private, |
741 | const struct ceph_connection_operations *ops, | |
b7a9e5dd | 742 | struct ceph_messenger *msgr) |
31b8006e SW |
743 | { |
744 | dout("con_init %p\n", con); | |
745 | memset(con, 0, sizeof(*con)); | |
1bfd89f4 AE |
746 | con->private = private; |
747 | con->ops = ops; | |
31b8006e | 748 | con->msgr = msgr; |
ce2c8903 AE |
749 | |
750 | con_sock_state_init(con); | |
751 | ||
ec302645 | 752 | mutex_init(&con->mutex); |
31b8006e SW |
753 | INIT_LIST_HEAD(&con->out_queue); |
754 | INIT_LIST_HEAD(&con->out_sent); | |
68931622 | 755 | INIT_DELAYED_WORK(&con->work, ceph_con_workfn); |
a5988c49 | 756 | |
8dacc7da | 757 | con->state = CON_STATE_CLOSED; |
31b8006e | 758 | } |
3d14c5d2 | 759 | EXPORT_SYMBOL(ceph_con_init); |
31b8006e SW |
760 | |
761 | ||
762 | /* | |
763 | * We maintain a global counter to order connection attempts. Get | |
764 | * a unique seq greater than @gt. | |
765 | */ | |
766 | static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt) | |
767 | { | |
768 | u32 ret; | |
769 | ||
770 | spin_lock(&msgr->global_seq_lock); | |
771 | if (msgr->global_seq < gt) | |
772 | msgr->global_seq = gt; | |
773 | ret = ++msgr->global_seq; | |
774 | spin_unlock(&msgr->global_seq_lock); | |
775 | return ret; | |
776 | } | |
777 | ||
e2200423 | 778 | static void con_out_kvec_reset(struct ceph_connection *con) |
859eb799 | 779 | { |
67645d76 ID |
780 | BUG_ON(con->out_skip); |
781 | ||
859eb799 AE |
782 | con->out_kvec_left = 0; |
783 | con->out_kvec_bytes = 0; | |
784 | con->out_kvec_cur = &con->out_kvec[0]; | |
785 | } | |
786 | ||
e2200423 | 787 | static void con_out_kvec_add(struct ceph_connection *con, |
859eb799 AE |
788 | size_t size, void *data) |
789 | { | |
67645d76 | 790 | int index = con->out_kvec_left; |
859eb799 | 791 | |
67645d76 | 792 | BUG_ON(con->out_skip); |
859eb799 AE |
793 | BUG_ON(index >= ARRAY_SIZE(con->out_kvec)); |
794 | ||
795 | con->out_kvec[index].iov_len = size; | |
796 | con->out_kvec[index].iov_base = data; | |
797 | con->out_kvec_left++; | |
798 | con->out_kvec_bytes += size; | |
799 | } | |
31b8006e | 800 | |
67645d76 ID |
801 | /* |
802 | * Chop off a kvec from the end. Return residual number of bytes for | |
803 | * that kvec, i.e. how many bytes would have been written if the kvec | |
804 | * hadn't been nuked. | |
805 | */ | |
806 | static int con_out_kvec_skip(struct ceph_connection *con) | |
807 | { | |
808 | int off = con->out_kvec_cur - con->out_kvec; | |
809 | int skip = 0; | |
810 | ||
811 | if (con->out_kvec_bytes > 0) { | |
812 | skip = con->out_kvec[off + con->out_kvec_left - 1].iov_len; | |
813 | BUG_ON(con->out_kvec_bytes < skip); | |
814 | BUG_ON(!con->out_kvec_left); | |
815 | con->out_kvec_bytes -= skip; | |
816 | con->out_kvec_left--; | |
817 | } | |
818 | ||
819 | return skip; | |
820 | } | |
821 | ||
df6ad1f9 | 822 | #ifdef CONFIG_BLOCK |
6aaa4511 AE |
823 | |
824 | /* | |
825 | * For a bio data item, a piece is whatever remains of the next | |
826 | * entry in the current bio iovec, or the first entry in the next | |
827 | * bio in the list. | |
828 | */ | |
8ae4f4f5 | 829 | static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor, |
25aff7c5 | 830 | size_t length) |
6aaa4511 | 831 | { |
8ae4f4f5 | 832 | struct ceph_msg_data *data = cursor->data; |
5359a17d | 833 | struct ceph_bio_iter *it = &cursor->bio_iter; |
6aaa4511 | 834 | |
5359a17d ID |
835 | cursor->resid = min_t(size_t, length, data->bio_length); |
836 | *it = data->bio_pos; | |
837 | if (cursor->resid < it->iter.bi_size) | |
838 | it->iter.bi_size = cursor->resid; | |
6aaa4511 | 839 | |
5359a17d ID |
840 | BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter)); |
841 | cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter); | |
6aaa4511 AE |
842 | } |
843 | ||
8ae4f4f5 | 844 | static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor, |
6aaa4511 AE |
845 | size_t *page_offset, |
846 | size_t *length) | |
847 | { | |
5359a17d ID |
848 | struct bio_vec bv = bio_iter_iovec(cursor->bio_iter.bio, |
849 | cursor->bio_iter.iter); | |
6aaa4511 | 850 | |
5359a17d ID |
851 | *page_offset = bv.bv_offset; |
852 | *length = bv.bv_len; | |
853 | return bv.bv_page; | |
6aaa4511 AE |
854 | } |
855 | ||
8ae4f4f5 AE |
856 | static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor, |
857 | size_t bytes) | |
6aaa4511 | 858 | { |
5359a17d | 859 | struct ceph_bio_iter *it = &cursor->bio_iter; |
6aaa4511 | 860 | |
5359a17d ID |
861 | BUG_ON(bytes > cursor->resid); |
862 | BUG_ON(bytes > bio_iter_len(it->bio, it->iter)); | |
25aff7c5 | 863 | cursor->resid -= bytes; |
5359a17d | 864 | bio_advance_iter(it->bio, &it->iter, bytes); |
f38a5181 | 865 | |
5359a17d ID |
866 | if (!cursor->resid) { |
867 | BUG_ON(!cursor->last_piece); | |
868 | return false; /* no more data */ | |
869 | } | |
f38a5181 | 870 | |
5359a17d | 871 | if (!bytes || (it->iter.bi_size && it->iter.bi_bvec_done)) |
6aaa4511 AE |
872 | return false; /* more bytes to process in this segment */ |
873 | ||
5359a17d ID |
874 | if (!it->iter.bi_size) { |
875 | it->bio = it->bio->bi_next; | |
876 | it->iter = it->bio->bi_iter; | |
877 | if (cursor->resid < it->iter.bi_size) | |
878 | it->iter.bi_size = cursor->resid; | |
25aff7c5 | 879 | } |
6aaa4511 | 880 | |
5359a17d ID |
881 | BUG_ON(cursor->last_piece); |
882 | BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter)); | |
883 | cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter); | |
6aaa4511 AE |
884 | return true; |
885 | } | |
ea96571f | 886 | #endif /* CONFIG_BLOCK */ |
df6ad1f9 | 887 | |
b9e281c2 ID |
888 | static void ceph_msg_data_bvecs_cursor_init(struct ceph_msg_data_cursor *cursor, |
889 | size_t length) | |
890 | { | |
891 | struct ceph_msg_data *data = cursor->data; | |
892 | struct bio_vec *bvecs = data->bvec_pos.bvecs; | |
893 | ||
894 | cursor->resid = min_t(size_t, length, data->bvec_pos.iter.bi_size); | |
895 | cursor->bvec_iter = data->bvec_pos.iter; | |
896 | cursor->bvec_iter.bi_size = cursor->resid; | |
897 | ||
898 | BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter)); | |
899 | cursor->last_piece = | |
900 | cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter); | |
901 | } | |
902 | ||
903 | static struct page *ceph_msg_data_bvecs_next(struct ceph_msg_data_cursor *cursor, | |
904 | size_t *page_offset, | |
905 | size_t *length) | |
906 | { | |
907 | struct bio_vec bv = bvec_iter_bvec(cursor->data->bvec_pos.bvecs, | |
908 | cursor->bvec_iter); | |
909 | ||
910 | *page_offset = bv.bv_offset; | |
911 | *length = bv.bv_len; | |
912 | return bv.bv_page; | |
913 | } | |
914 | ||
915 | static bool ceph_msg_data_bvecs_advance(struct ceph_msg_data_cursor *cursor, | |
916 | size_t bytes) | |
917 | { | |
918 | struct bio_vec *bvecs = cursor->data->bvec_pos.bvecs; | |
919 | ||
920 | BUG_ON(bytes > cursor->resid); | |
921 | BUG_ON(bytes > bvec_iter_len(bvecs, cursor->bvec_iter)); | |
922 | cursor->resid -= bytes; | |
923 | bvec_iter_advance(bvecs, &cursor->bvec_iter, bytes); | |
924 | ||
925 | if (!cursor->resid) { | |
926 | BUG_ON(!cursor->last_piece); | |
927 | return false; /* no more data */ | |
928 | } | |
929 | ||
930 | if (!bytes || cursor->bvec_iter.bi_bvec_done) | |
931 | return false; /* more bytes to process in this segment */ | |
932 | ||
933 | BUG_ON(cursor->last_piece); | |
934 | BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter)); | |
935 | cursor->last_piece = | |
936 | cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter); | |
937 | return true; | |
938 | } | |
939 | ||
e766d7b5 AE |
940 | /* |
941 | * For a page array, a piece comes from the first page in the array | |
942 | * that has not already been fully consumed. | |
943 | */ | |
8ae4f4f5 | 944 | static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor, |
25aff7c5 | 945 | size_t length) |
e766d7b5 | 946 | { |
8ae4f4f5 | 947 | struct ceph_msg_data *data = cursor->data; |
e766d7b5 AE |
948 | int page_count; |
949 | ||
950 | BUG_ON(data->type != CEPH_MSG_DATA_PAGES); | |
951 | ||
952 | BUG_ON(!data->pages); | |
953 | BUG_ON(!data->length); | |
954 | ||
ca8b3a69 | 955 | cursor->resid = min(length, data->length); |
e766d7b5 | 956 | page_count = calc_pages_for(data->alignment, (u64)data->length); |
e766d7b5 AE |
957 | cursor->page_offset = data->alignment & ~PAGE_MASK; |
958 | cursor->page_index = 0; | |
56fc5659 AE |
959 | BUG_ON(page_count > (int)USHRT_MAX); |
960 | cursor->page_count = (unsigned short)page_count; | |
961 | BUG_ON(length > SIZE_MAX - cursor->page_offset); | |
5f740d7e | 962 | cursor->last_piece = cursor->page_offset + cursor->resid <= PAGE_SIZE; |
e766d7b5 AE |
963 | } |
964 | ||
8ae4f4f5 AE |
965 | static struct page * |
966 | ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor, | |
967 | size_t *page_offset, size_t *length) | |
e766d7b5 | 968 | { |
8ae4f4f5 | 969 | struct ceph_msg_data *data = cursor->data; |
e766d7b5 AE |
970 | |
971 | BUG_ON(data->type != CEPH_MSG_DATA_PAGES); | |
972 | ||
973 | BUG_ON(cursor->page_index >= cursor->page_count); | |
974 | BUG_ON(cursor->page_offset >= PAGE_SIZE); | |
e766d7b5 AE |
975 | |
976 | *page_offset = cursor->page_offset; | |
25aff7c5 | 977 | if (cursor->last_piece) |
e766d7b5 | 978 | *length = cursor->resid; |
25aff7c5 | 979 | else |
e766d7b5 | 980 | *length = PAGE_SIZE - *page_offset; |
e766d7b5 AE |
981 | |
982 | return data->pages[cursor->page_index]; | |
983 | } | |
984 | ||
8ae4f4f5 | 985 | static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor, |
e766d7b5 AE |
986 | size_t bytes) |
987 | { | |
8ae4f4f5 | 988 | BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES); |
e766d7b5 AE |
989 | |
990 | BUG_ON(cursor->page_offset + bytes > PAGE_SIZE); | |
e766d7b5 AE |
991 | |
992 | /* Advance the cursor page offset */ | |
993 | ||
994 | cursor->resid -= bytes; | |
5df521b1 AE |
995 | cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK; |
996 | if (!bytes || cursor->page_offset) | |
e766d7b5 AE |
997 | return false; /* more bytes to process in the current page */ |
998 | ||
d90deda6 YZ |
999 | if (!cursor->resid) |
1000 | return false; /* no more data */ | |
1001 | ||
5df521b1 | 1002 | /* Move on to the next page; offset is already at 0 */ |
e766d7b5 AE |
1003 | |
1004 | BUG_ON(cursor->page_index >= cursor->page_count); | |
e766d7b5 | 1005 | cursor->page_index++; |
25aff7c5 | 1006 | cursor->last_piece = cursor->resid <= PAGE_SIZE; |
e766d7b5 AE |
1007 | |
1008 | return true; | |
1009 | } | |
1010 | ||
fe38a2b6 | 1011 | /* |
dd236fcb AE |
1012 | * For a pagelist, a piece is whatever remains to be consumed in the |
1013 | * first page in the list, or the front of the next page. | |
fe38a2b6 | 1014 | */ |
8ae4f4f5 AE |
1015 | static void |
1016 | ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor, | |
25aff7c5 | 1017 | size_t length) |
fe38a2b6 | 1018 | { |
8ae4f4f5 | 1019 | struct ceph_msg_data *data = cursor->data; |
fe38a2b6 AE |
1020 | struct ceph_pagelist *pagelist; |
1021 | struct page *page; | |
1022 | ||
dd236fcb | 1023 | BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST); |
fe38a2b6 AE |
1024 | |
1025 | pagelist = data->pagelist; | |
1026 | BUG_ON(!pagelist); | |
25aff7c5 AE |
1027 | |
1028 | if (!length) | |
fe38a2b6 AE |
1029 | return; /* pagelist can be assigned but empty */ |
1030 | ||
1031 | BUG_ON(list_empty(&pagelist->head)); | |
1032 | page = list_first_entry(&pagelist->head, struct page, lru); | |
1033 | ||
ca8b3a69 | 1034 | cursor->resid = min(length, pagelist->length); |
fe38a2b6 AE |
1035 | cursor->page = page; |
1036 | cursor->offset = 0; | |
a51b272e | 1037 | cursor->last_piece = cursor->resid <= PAGE_SIZE; |
fe38a2b6 AE |
1038 | } |
1039 | ||
8ae4f4f5 AE |
1040 | static struct page * |
1041 | ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor, | |
1042 | size_t *page_offset, size_t *length) | |
fe38a2b6 | 1043 | { |
8ae4f4f5 | 1044 | struct ceph_msg_data *data = cursor->data; |
fe38a2b6 | 1045 | struct ceph_pagelist *pagelist; |
fe38a2b6 AE |
1046 | |
1047 | BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST); | |
1048 | ||
1049 | pagelist = data->pagelist; | |
1050 | BUG_ON(!pagelist); | |
1051 | ||
1052 | BUG_ON(!cursor->page); | |
25aff7c5 | 1053 | BUG_ON(cursor->offset + cursor->resid != pagelist->length); |
fe38a2b6 | 1054 | |
5df521b1 | 1055 | /* offset of first page in pagelist is always 0 */ |
fe38a2b6 | 1056 | *page_offset = cursor->offset & ~PAGE_MASK; |
5df521b1 | 1057 | if (cursor->last_piece) |
25aff7c5 AE |
1058 | *length = cursor->resid; |
1059 | else | |
1060 | *length = PAGE_SIZE - *page_offset; | |
fe38a2b6 | 1061 | |
8ae4f4f5 | 1062 | return cursor->page; |
fe38a2b6 AE |
1063 | } |
1064 | ||
8ae4f4f5 | 1065 | static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor, |
dd236fcb | 1066 | size_t bytes) |
fe38a2b6 | 1067 | { |
8ae4f4f5 | 1068 | struct ceph_msg_data *data = cursor->data; |
fe38a2b6 AE |
1069 | struct ceph_pagelist *pagelist; |
1070 | ||
1071 | BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST); | |
1072 | ||
1073 | pagelist = data->pagelist; | |
1074 | BUG_ON(!pagelist); | |
25aff7c5 AE |
1075 | |
1076 | BUG_ON(cursor->offset + cursor->resid != pagelist->length); | |
fe38a2b6 AE |
1077 | BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE); |
1078 | ||
1079 | /* Advance the cursor offset */ | |
1080 | ||
25aff7c5 | 1081 | cursor->resid -= bytes; |
fe38a2b6 | 1082 | cursor->offset += bytes; |
5df521b1 | 1083 | /* offset of first page in pagelist is always 0 */ |
fe38a2b6 AE |
1084 | if (!bytes || cursor->offset & ~PAGE_MASK) |
1085 | return false; /* more bytes to process in the current page */ | |
1086 | ||
d90deda6 YZ |
1087 | if (!cursor->resid) |
1088 | return false; /* no more data */ | |
1089 | ||
fe38a2b6 AE |
1090 | /* Move on to the next page */ |
1091 | ||
1092 | BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head)); | |
17ddc49b | 1093 | cursor->page = list_next_entry(cursor->page, lru); |
25aff7c5 | 1094 | cursor->last_piece = cursor->resid <= PAGE_SIZE; |
fe38a2b6 AE |
1095 | |
1096 | return true; | |
1097 | } | |
1098 | ||
dd236fcb AE |
1099 | /* |
1100 | * Message data is handled (sent or received) in pieces, where each | |
1101 | * piece resides on a single page. The network layer might not | |
1102 | * consume an entire piece at once. A data item's cursor keeps | |
1103 | * track of which piece is next to process and how much remains to | |
1104 | * be processed in that piece. It also tracks whether the current | |
1105 | * piece is the last one in the data item. | |
1106 | */ | |
ca8b3a69 | 1107 | static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor) |
dd236fcb | 1108 | { |
ca8b3a69 | 1109 | size_t length = cursor->total_resid; |
8ae4f4f5 | 1110 | |
8ae4f4f5 | 1111 | switch (cursor->data->type) { |
dd236fcb | 1112 | case CEPH_MSG_DATA_PAGELIST: |
8ae4f4f5 | 1113 | ceph_msg_data_pagelist_cursor_init(cursor, length); |
dd236fcb | 1114 | break; |
e766d7b5 | 1115 | case CEPH_MSG_DATA_PAGES: |
8ae4f4f5 | 1116 | ceph_msg_data_pages_cursor_init(cursor, length); |
e766d7b5 | 1117 | break; |
dd236fcb AE |
1118 | #ifdef CONFIG_BLOCK |
1119 | case CEPH_MSG_DATA_BIO: | |
8ae4f4f5 | 1120 | ceph_msg_data_bio_cursor_init(cursor, length); |
6aaa4511 | 1121 | break; |
dd236fcb | 1122 | #endif /* CONFIG_BLOCK */ |
b9e281c2 ID |
1123 | case CEPH_MSG_DATA_BVECS: |
1124 | ceph_msg_data_bvecs_cursor_init(cursor, length); | |
1125 | break; | |
6aaa4511 | 1126 | case CEPH_MSG_DATA_NONE: |
dd236fcb AE |
1127 | default: |
1128 | /* BUG(); */ | |
1129 | break; | |
1130 | } | |
8ae4f4f5 | 1131 | cursor->need_crc = true; |
dd236fcb AE |
1132 | } |
1133 | ||
ca8b3a69 AE |
1134 | static void ceph_msg_data_cursor_init(struct ceph_msg *msg, size_t length) |
1135 | { | |
1136 | struct ceph_msg_data_cursor *cursor = &msg->cursor; | |
ca8b3a69 AE |
1137 | |
1138 | BUG_ON(!length); | |
1139 | BUG_ON(length > msg->data_length); | |
0d9c1ab3 | 1140 | BUG_ON(!msg->num_data_items); |
ca8b3a69 | 1141 | |
ca8b3a69 | 1142 | cursor->total_resid = length; |
0d9c1ab3 | 1143 | cursor->data = msg->data; |
ca8b3a69 AE |
1144 | |
1145 | __ceph_msg_data_cursor_init(cursor); | |
1146 | } | |
1147 | ||
dd236fcb AE |
1148 | /* |
1149 | * Return the page containing the next piece to process for a given | |
1150 | * data item, and supply the page offset and length of that piece. | |
1151 | * Indicate whether this is the last piece in this data item. | |
1152 | */ | |
8ae4f4f5 AE |
1153 | static struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor, |
1154 | size_t *page_offset, size_t *length, | |
dd236fcb AE |
1155 | bool *last_piece) |
1156 | { | |
1157 | struct page *page; | |
1158 | ||
8ae4f4f5 | 1159 | switch (cursor->data->type) { |
dd236fcb | 1160 | case CEPH_MSG_DATA_PAGELIST: |
8ae4f4f5 | 1161 | page = ceph_msg_data_pagelist_next(cursor, page_offset, length); |
dd236fcb | 1162 | break; |
e766d7b5 | 1163 | case CEPH_MSG_DATA_PAGES: |
8ae4f4f5 | 1164 | page = ceph_msg_data_pages_next(cursor, page_offset, length); |
e766d7b5 | 1165 | break; |
dd236fcb AE |
1166 | #ifdef CONFIG_BLOCK |
1167 | case CEPH_MSG_DATA_BIO: | |
8ae4f4f5 | 1168 | page = ceph_msg_data_bio_next(cursor, page_offset, length); |
6aaa4511 | 1169 | break; |
dd236fcb | 1170 | #endif /* CONFIG_BLOCK */ |
b9e281c2 ID |
1171 | case CEPH_MSG_DATA_BVECS: |
1172 | page = ceph_msg_data_bvecs_next(cursor, page_offset, length); | |
1173 | break; | |
6aaa4511 | 1174 | case CEPH_MSG_DATA_NONE: |
dd236fcb AE |
1175 | default: |
1176 | page = NULL; | |
1177 | break; | |
1178 | } | |
5359a17d | 1179 | |
dd236fcb AE |
1180 | BUG_ON(!page); |
1181 | BUG_ON(*page_offset + *length > PAGE_SIZE); | |
1182 | BUG_ON(!*length); | |
5359a17d | 1183 | BUG_ON(*length > cursor->resid); |
dd236fcb | 1184 | if (last_piece) |
8ae4f4f5 | 1185 | *last_piece = cursor->last_piece; |
dd236fcb AE |
1186 | |
1187 | return page; | |
1188 | } | |
1189 | ||
1190 | /* | |
1191 | * Returns true if the result moves the cursor on to the next piece | |
1192 | * of the data item. | |
1193 | */ | |
1759f7b0 ID |
1194 | static void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor, |
1195 | size_t bytes) | |
dd236fcb AE |
1196 | { |
1197 | bool new_piece; | |
1198 | ||
25aff7c5 | 1199 | BUG_ON(bytes > cursor->resid); |
8ae4f4f5 | 1200 | switch (cursor->data->type) { |
dd236fcb | 1201 | case CEPH_MSG_DATA_PAGELIST: |
8ae4f4f5 | 1202 | new_piece = ceph_msg_data_pagelist_advance(cursor, bytes); |
dd236fcb | 1203 | break; |
e766d7b5 | 1204 | case CEPH_MSG_DATA_PAGES: |
8ae4f4f5 | 1205 | new_piece = ceph_msg_data_pages_advance(cursor, bytes); |
e766d7b5 | 1206 | break; |
dd236fcb AE |
1207 | #ifdef CONFIG_BLOCK |
1208 | case CEPH_MSG_DATA_BIO: | |
8ae4f4f5 | 1209 | new_piece = ceph_msg_data_bio_advance(cursor, bytes); |
6aaa4511 | 1210 | break; |
dd236fcb | 1211 | #endif /* CONFIG_BLOCK */ |
b9e281c2 ID |
1212 | case CEPH_MSG_DATA_BVECS: |
1213 | new_piece = ceph_msg_data_bvecs_advance(cursor, bytes); | |
1214 | break; | |
6aaa4511 | 1215 | case CEPH_MSG_DATA_NONE: |
dd236fcb AE |
1216 | default: |
1217 | BUG(); | |
1218 | break; | |
1219 | } | |
ca8b3a69 | 1220 | cursor->total_resid -= bytes; |
dd236fcb | 1221 | |
ca8b3a69 AE |
1222 | if (!cursor->resid && cursor->total_resid) { |
1223 | WARN_ON(!cursor->last_piece); | |
0d9c1ab3 | 1224 | cursor->data++; |
ca8b3a69 | 1225 | __ceph_msg_data_cursor_init(cursor); |
a51b272e | 1226 | new_piece = true; |
ca8b3a69 | 1227 | } |
a51b272e | 1228 | cursor->need_crc = new_piece; |
dd236fcb AE |
1229 | } |
1230 | ||
dbc0d3ca ID |
1231 | static size_t sizeof_footer(struct ceph_connection *con) |
1232 | { | |
1233 | return (con->peer_features & CEPH_FEATURE_MSG_AUTH) ? | |
1234 | sizeof(struct ceph_msg_footer) : | |
1235 | sizeof(struct ceph_msg_footer_old); | |
1236 | } | |
1237 | ||
98fa5dd8 | 1238 | static void prepare_message_data(struct ceph_msg *msg, u32 data_len) |
739c905b | 1239 | { |
4c59b4a2 | 1240 | /* Initialize data cursor */ |
fe38a2b6 | 1241 | |
8ae4f4f5 | 1242 | ceph_msg_data_cursor_init(msg, (size_t)data_len); |
739c905b AE |
1243 | } |
1244 | ||
31b8006e SW |
1245 | /* |
1246 | * Prepare footer for currently outgoing message, and finish things | |
1247 | * off. Assumes out_kvec* are already valid.. we just add on to the end. | |
1248 | */ | |
859eb799 | 1249 | static void prepare_write_message_footer(struct ceph_connection *con) |
31b8006e SW |
1250 | { |
1251 | struct ceph_msg *m = con->out_msg; | |
1252 | ||
fd154f3c AE |
1253 | m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE; |
1254 | ||
31b8006e | 1255 | dout("prepare_write_message_footer %p\n", con); |
89f08173 | 1256 | con_out_kvec_add(con, sizeof_footer(con), &m->footer); |
33d07337 YZ |
1257 | if (con->peer_features & CEPH_FEATURE_MSG_AUTH) { |
1258 | if (con->ops->sign_message) | |
79dbd1ba | 1259 | con->ops->sign_message(m); |
33d07337 YZ |
1260 | else |
1261 | m->footer.sig = 0; | |
33d07337 YZ |
1262 | } else { |
1263 | m->old_footer.flags = m->footer.flags; | |
33d07337 | 1264 | } |
31b8006e | 1265 | con->out_more = m->more_to_follow; |
c86a2930 | 1266 | con->out_msg_done = true; |
31b8006e SW |
1267 | } |
1268 | ||
1269 | /* | |
1270 | * Prepare headers for the next outgoing message. | |
1271 | */ | |
1272 | static void prepare_write_message(struct ceph_connection *con) | |
1273 | { | |
1274 | struct ceph_msg *m; | |
a9a0c51a | 1275 | u32 crc; |
31b8006e | 1276 | |
e2200423 | 1277 | con_out_kvec_reset(con); |
c86a2930 | 1278 | con->out_msg_done = false; |
31b8006e SW |
1279 | |
1280 | /* Sneak an ack in there first? If we can get it into the same | |
1281 | * TCP packet that's a good thing. */ | |
1282 | if (con->in_seq > con->in_seq_acked) { | |
1283 | con->in_seq_acked = con->in_seq; | |
e2200423 | 1284 | con_out_kvec_add(con, sizeof (tag_ack), &tag_ack); |
31b8006e | 1285 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); |
e2200423 | 1286 | con_out_kvec_add(con, sizeof (con->out_temp_ack), |
859eb799 | 1287 | &con->out_temp_ack); |
31b8006e SW |
1288 | } |
1289 | ||
38941f80 | 1290 | BUG_ON(list_empty(&con->out_queue)); |
859eb799 | 1291 | m = list_first_entry(&con->out_queue, struct ceph_msg, list_head); |
c86a2930 | 1292 | con->out_msg = m; |
38941f80 | 1293 | BUG_ON(m->con != con); |
4cf9d544 SW |
1294 | |
1295 | /* put message on sent list */ | |
1296 | ceph_msg_get(m); | |
1297 | list_move_tail(&m->list_head, &con->out_sent); | |
31b8006e | 1298 | |
e84346b7 SW |
1299 | /* |
1300 | * only assign outgoing seq # if we haven't sent this message | |
1301 | * yet. if it is requeued, resend with it's original seq. | |
1302 | */ | |
1303 | if (m->needs_out_seq) { | |
1304 | m->hdr.seq = cpu_to_le64(++con->out_seq); | |
1305 | m->needs_out_seq = false; | |
98ad5ebd | 1306 | |
4690faf0 ID |
1307 | if (con->ops->reencode_message) |
1308 | con->ops->reencode_message(m); | |
1309 | } | |
31b8006e | 1310 | |
98fa5dd8 | 1311 | dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n", |
31b8006e SW |
1312 | m, con->out_seq, le16_to_cpu(m->hdr.type), |
1313 | le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len), | |
98fa5dd8 | 1314 | m->data_length); |
98ad5ebd ID |
1315 | WARN_ON(m->front.iov_len != le32_to_cpu(m->hdr.front_len)); |
1316 | WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len)); | |
31b8006e SW |
1317 | |
1318 | /* tag + hdr + front + middle */ | |
e2200423 | 1319 | con_out_kvec_add(con, sizeof (tag_msg), &tag_msg); |
67645d76 | 1320 | con_out_kvec_add(con, sizeof(con->out_hdr), &con->out_hdr); |
e2200423 | 1321 | con_out_kvec_add(con, m->front.iov_len, m->front.iov_base); |
859eb799 | 1322 | |
31b8006e | 1323 | if (m->middle) |
e2200423 | 1324 | con_out_kvec_add(con, m->middle->vec.iov_len, |
859eb799 | 1325 | m->middle->vec.iov_base); |
31b8006e | 1326 | |
67645d76 | 1327 | /* fill in hdr crc and finalize hdr */ |
a9a0c51a AE |
1328 | crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc)); |
1329 | con->out_msg->hdr.crc = cpu_to_le32(crc); | |
67645d76 | 1330 | memcpy(&con->out_hdr, &con->out_msg->hdr, sizeof(con->out_hdr)); |
a9a0c51a | 1331 | |
67645d76 | 1332 | /* fill in front and middle crc, footer */ |
a9a0c51a AE |
1333 | crc = crc32c(0, m->front.iov_base, m->front.iov_len); |
1334 | con->out_msg->footer.front_crc = cpu_to_le32(crc); | |
1335 | if (m->middle) { | |
1336 | crc = crc32c(0, m->middle->vec.iov_base, | |
1337 | m->middle->vec.iov_len); | |
1338 | con->out_msg->footer.middle_crc = cpu_to_le32(crc); | |
1339 | } else | |
31b8006e | 1340 | con->out_msg->footer.middle_crc = 0; |
739c905b | 1341 | dout("%s front_crc %u middle_crc %u\n", __func__, |
31b8006e SW |
1342 | le32_to_cpu(con->out_msg->footer.front_crc), |
1343 | le32_to_cpu(con->out_msg->footer.middle_crc)); | |
67645d76 | 1344 | con->out_msg->footer.flags = 0; |
31b8006e SW |
1345 | |
1346 | /* is there a data payload? */ | |
739c905b | 1347 | con->out_msg->footer.data_crc = 0; |
98fa5dd8 AE |
1348 | if (m->data_length) { |
1349 | prepare_message_data(con->out_msg, m->data_length); | |
78625051 AE |
1350 | con->out_more = 1; /* data + footer will follow */ |
1351 | } else { | |
31b8006e | 1352 | /* no, queue up footer too and be done */ |
859eb799 | 1353 | prepare_write_message_footer(con); |
78625051 | 1354 | } |
31b8006e | 1355 | |
c9ffc77a | 1356 | con_flag_set(con, CON_FLAG_WRITE_PENDING); |
31b8006e SW |
1357 | } |
1358 | ||
1359 | /* | |
1360 | * Prepare an ack. | |
1361 | */ | |
1362 | static void prepare_write_ack(struct ceph_connection *con) | |
1363 | { | |
1364 | dout("prepare_write_ack %p %llu -> %llu\n", con, | |
1365 | con->in_seq_acked, con->in_seq); | |
1366 | con->in_seq_acked = con->in_seq; | |
1367 | ||
e2200423 | 1368 | con_out_kvec_reset(con); |
859eb799 | 1369 | |
e2200423 | 1370 | con_out_kvec_add(con, sizeof (tag_ack), &tag_ack); |
859eb799 | 1371 | |
31b8006e | 1372 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); |
e2200423 | 1373 | con_out_kvec_add(con, sizeof (con->out_temp_ack), |
859eb799 AE |
1374 | &con->out_temp_ack); |
1375 | ||
31b8006e | 1376 | con->out_more = 1; /* more will follow.. eventually.. */ |
c9ffc77a | 1377 | con_flag_set(con, CON_FLAG_WRITE_PENDING); |
31b8006e SW |
1378 | } |
1379 | ||
3a23083b SW |
1380 | /* |
1381 | * Prepare to share the seq during handshake | |
1382 | */ | |
1383 | static void prepare_write_seq(struct ceph_connection *con) | |
1384 | { | |
1385 | dout("prepare_write_seq %p %llu -> %llu\n", con, | |
1386 | con->in_seq_acked, con->in_seq); | |
1387 | con->in_seq_acked = con->in_seq; | |
1388 | ||
1389 | con_out_kvec_reset(con); | |
1390 | ||
1391 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); | |
1392 | con_out_kvec_add(con, sizeof (con->out_temp_ack), | |
1393 | &con->out_temp_ack); | |
1394 | ||
1395 | con_flag_set(con, CON_FLAG_WRITE_PENDING); | |
1396 | } | |
1397 | ||
31b8006e SW |
1398 | /* |
1399 | * Prepare to write keepalive byte. | |
1400 | */ | |
1401 | static void prepare_write_keepalive(struct ceph_connection *con) | |
1402 | { | |
1403 | dout("prepare_write_keepalive %p\n", con); | |
e2200423 | 1404 | con_out_kvec_reset(con); |
8b9558aa | 1405 | if (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2) { |
473bd2d7 | 1406 | struct timespec64 now; |
7f61f545 | 1407 | |
473bd2d7 | 1408 | ktime_get_real_ts64(&now); |
8b9558aa | 1409 | con_out_kvec_add(con, sizeof(tag_keepalive2), &tag_keepalive2); |
473bd2d7 | 1410 | ceph_encode_timespec64(&con->out_temp_keepalive2, &now); |
7f61f545 ID |
1411 | con_out_kvec_add(con, sizeof(con->out_temp_keepalive2), |
1412 | &con->out_temp_keepalive2); | |
8b9558aa YZ |
1413 | } else { |
1414 | con_out_kvec_add(con, sizeof(tag_keepalive), &tag_keepalive); | |
1415 | } | |
c9ffc77a | 1416 | con_flag_set(con, CON_FLAG_WRITE_PENDING); |
31b8006e SW |
1417 | } |
1418 | ||
1419 | /* | |
1420 | * Connection negotiation. | |
1421 | */ | |
1422 | ||
262614c4 | 1423 | static int get_connect_authorizer(struct ceph_connection *con) |
4e7a5dcd | 1424 | { |
a3530df3 | 1425 | struct ceph_auth_handshake *auth; |
262614c4 | 1426 | int auth_proto; |
b1c6b980 AE |
1427 | |
1428 | if (!con->ops->get_authorizer) { | |
262614c4 | 1429 | con->auth = NULL; |
b1c6b980 AE |
1430 | con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN; |
1431 | con->out_connect.authorizer_len = 0; | |
262614c4 | 1432 | return 0; |
b1c6b980 AE |
1433 | } |
1434 | ||
262614c4 | 1435 | auth = con->ops->get_authorizer(con, &auth_proto, con->auth_retry); |
a3530df3 | 1436 | if (IS_ERR(auth)) |
262614c4 | 1437 | return PTR_ERR(auth); |
0da5d703 | 1438 | |
262614c4 ID |
1439 | con->auth = auth; |
1440 | con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto); | |
1441 | con->out_connect.authorizer_len = cpu_to_le32(auth->authorizer_buf_len); | |
1442 | return 0; | |
4e7a5dcd SW |
1443 | } |
1444 | ||
31b8006e SW |
1445 | /* |
1446 | * We connected to a peer and are saying hello. | |
1447 | */ | |
e825a66d | 1448 | static void prepare_write_banner(struct ceph_connection *con) |
31b8006e | 1449 | { |
e2200423 AE |
1450 | con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER); |
1451 | con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr), | |
e825a66d | 1452 | &con->msgr->my_enc_addr); |
eed0ef2c | 1453 | |
eed0ef2c | 1454 | con->out_more = 0; |
c9ffc77a | 1455 | con_flag_set(con, CON_FLAG_WRITE_PENDING); |
eed0ef2c SW |
1456 | } |
1457 | ||
c0f56b48 ID |
1458 | static void __prepare_write_connect(struct ceph_connection *con) |
1459 | { | |
1460 | con_out_kvec_add(con, sizeof(con->out_connect), &con->out_connect); | |
1461 | if (con->auth) | |
1462 | con_out_kvec_add(con, con->auth->authorizer_buf_len, | |
1463 | con->auth->authorizer_buf); | |
1464 | ||
1465 | con->out_more = 0; | |
1466 | con_flag_set(con, CON_FLAG_WRITE_PENDING); | |
1467 | } | |
1468 | ||
e825a66d | 1469 | static int prepare_write_connect(struct ceph_connection *con) |
eed0ef2c | 1470 | { |
95c96174 | 1471 | unsigned int global_seq = get_global_seq(con->msgr, 0); |
31b8006e | 1472 | int proto; |
262614c4 | 1473 | int ret; |
31b8006e SW |
1474 | |
1475 | switch (con->peer_name.type) { | |
1476 | case CEPH_ENTITY_TYPE_MON: | |
1477 | proto = CEPH_MONC_PROTOCOL; | |
1478 | break; | |
1479 | case CEPH_ENTITY_TYPE_OSD: | |
1480 | proto = CEPH_OSDC_PROTOCOL; | |
1481 | break; | |
1482 | case CEPH_ENTITY_TYPE_MDS: | |
1483 | proto = CEPH_MDSC_PROTOCOL; | |
1484 | break; | |
1485 | default: | |
1486 | BUG(); | |
1487 | } | |
1488 | ||
1489 | dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con, | |
1490 | con->connect_seq, global_seq, proto); | |
4e7a5dcd | 1491 | |
859bff51 ID |
1492 | con->out_connect.features = |
1493 | cpu_to_le64(from_msgr(con->msgr)->supported_features); | |
31b8006e SW |
1494 | con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT); |
1495 | con->out_connect.connect_seq = cpu_to_le32(con->connect_seq); | |
1496 | con->out_connect.global_seq = cpu_to_le32(global_seq); | |
1497 | con->out_connect.protocol_version = cpu_to_le32(proto); | |
1498 | con->out_connect.flags = 0; | |
31b8006e | 1499 | |
262614c4 ID |
1500 | ret = get_connect_authorizer(con); |
1501 | if (ret) | |
1502 | return ret; | |
3da54776 | 1503 | |
c0f56b48 | 1504 | __prepare_write_connect(con); |
e10c758e | 1505 | return 0; |
31b8006e SW |
1506 | } |
1507 | ||
31b8006e SW |
1508 | /* |
1509 | * write as much of pending kvecs to the socket as we can. | |
1510 | * 1 -> done | |
1511 | * 0 -> socket full, but more to do | |
1512 | * <0 -> error | |
1513 | */ | |
1514 | static int write_partial_kvec(struct ceph_connection *con) | |
1515 | { | |
1516 | int ret; | |
1517 | ||
1518 | dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes); | |
1519 | while (con->out_kvec_bytes > 0) { | |
1520 | ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur, | |
1521 | con->out_kvec_left, con->out_kvec_bytes, | |
1522 | con->out_more); | |
1523 | if (ret <= 0) | |
1524 | goto out; | |
1525 | con->out_kvec_bytes -= ret; | |
1526 | if (con->out_kvec_bytes == 0) | |
1527 | break; /* done */ | |
f42299e6 AE |
1528 | |
1529 | /* account for full iov entries consumed */ | |
1530 | while (ret >= con->out_kvec_cur->iov_len) { | |
1531 | BUG_ON(!con->out_kvec_left); | |
1532 | ret -= con->out_kvec_cur->iov_len; | |
1533 | con->out_kvec_cur++; | |
1534 | con->out_kvec_left--; | |
1535 | } | |
1536 | /* and for a partially-consumed entry */ | |
1537 | if (ret) { | |
1538 | con->out_kvec_cur->iov_len -= ret; | |
1539 | con->out_kvec_cur->iov_base += ret; | |
31b8006e SW |
1540 | } |
1541 | } | |
1542 | con->out_kvec_left = 0; | |
31b8006e SW |
1543 | ret = 1; |
1544 | out: | |
1545 | dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con, | |
1546 | con->out_kvec_bytes, con->out_kvec_left, ret); | |
1547 | return ret; /* done! */ | |
1548 | } | |
1549 | ||
35b62808 AE |
1550 | static u32 ceph_crc32c_page(u32 crc, struct page *page, |
1551 | unsigned int page_offset, | |
1552 | unsigned int length) | |
1553 | { | |
1554 | char *kaddr; | |
1555 | ||
1556 | kaddr = kmap(page); | |
1557 | BUG_ON(kaddr == NULL); | |
1558 | crc = crc32c(crc, kaddr + page_offset, length); | |
1559 | kunmap(page); | |
1560 | ||
1561 | return crc; | |
1562 | } | |
31b8006e SW |
1563 | /* |
1564 | * Write as much message data payload as we can. If we finish, queue | |
1565 | * up the footer. | |
1566 | * 1 -> done, footer is now queued in out_kvec[]. | |
1567 | * 0 -> socket full, but more to do | |
1568 | * <0 -> error | |
1569 | */ | |
34d2d200 | 1570 | static int write_partial_message_data(struct ceph_connection *con) |
31b8006e SW |
1571 | { |
1572 | struct ceph_msg *msg = con->out_msg; | |
8ae4f4f5 | 1573 | struct ceph_msg_data_cursor *cursor = &msg->cursor; |
859bff51 | 1574 | bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC); |
f5db90bc | 1575 | u32 crc; |
31b8006e | 1576 | |
859a35d5 | 1577 | dout("%s %p msg %p\n", __func__, con, msg); |
31b8006e | 1578 | |
0d9c1ab3 | 1579 | if (!msg->num_data_items) |
4c59b4a2 AE |
1580 | return -EINVAL; |
1581 | ||
5821bd8c AE |
1582 | /* |
1583 | * Iterate through each page that contains data to be | |
1584 | * written, and send as much as possible for each. | |
1585 | * | |
1586 | * If we are calculating the data crc (the default), we will | |
1587 | * need to map the page. If we have no pages, they have | |
1588 | * been revoked, so use the zero page. | |
1589 | */ | |
f5db90bc | 1590 | crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0; |
45a267db | 1591 | while (cursor->total_resid) { |
8a166d05 | 1592 | struct page *page; |
e387d525 AE |
1593 | size_t page_offset; |
1594 | size_t length; | |
8a166d05 | 1595 | bool last_piece; |
f5db90bc | 1596 | int ret; |
68b4476b | 1597 | |
45a267db ID |
1598 | if (!cursor->resid) { |
1599 | ceph_msg_data_advance(cursor, 0); | |
1600 | continue; | |
1601 | } | |
1602 | ||
343128ce SB |
1603 | page = ceph_msg_data_next(cursor, &page_offset, &length, |
1604 | &last_piece); | |
e387d525 | 1605 | ret = ceph_tcp_sendpage(con->sock, page, page_offset, |
c2cfa194 | 1606 | length, !last_piece); |
f5db90bc AE |
1607 | if (ret <= 0) { |
1608 | if (do_datacrc) | |
1609 | msg->footer.data_crc = cpu_to_le32(crc); | |
31b8006e | 1610 | |
f5db90bc AE |
1611 | return ret; |
1612 | } | |
143334ff AE |
1613 | if (do_datacrc && cursor->need_crc) |
1614 | crc = ceph_crc32c_page(crc, page, page_offset, length); | |
1759f7b0 | 1615 | ceph_msg_data_advance(cursor, (size_t)ret); |
31b8006e SW |
1616 | } |
1617 | ||
34d2d200 | 1618 | dout("%s %p msg %p done\n", __func__, con, msg); |
31b8006e SW |
1619 | |
1620 | /* prepare and queue up footer, too */ | |
f5db90bc AE |
1621 | if (do_datacrc) |
1622 | msg->footer.data_crc = cpu_to_le32(crc); | |
1623 | else | |
84ca8fc8 | 1624 | msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC; |
e2200423 | 1625 | con_out_kvec_reset(con); |
859eb799 | 1626 | prepare_write_message_footer(con); |
f5db90bc AE |
1627 | |
1628 | return 1; /* must return > 0 to indicate success */ | |
31b8006e SW |
1629 | } |
1630 | ||
1631 | /* | |
1632 | * write some zeros | |
1633 | */ | |
1634 | static int write_partial_skip(struct ceph_connection *con) | |
1635 | { | |
1636 | int ret; | |
1637 | ||
67645d76 | 1638 | dout("%s %p %d left\n", __func__, con, con->out_skip); |
31b8006e | 1639 | while (con->out_skip > 0) { |
09cbfeaf | 1640 | size_t size = min(con->out_skip, (int) PAGE_SIZE); |
31b8006e | 1641 | |
e1dcb128 | 1642 | ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true); |
31b8006e SW |
1643 | if (ret <= 0) |
1644 | goto out; | |
1645 | con->out_skip -= ret; | |
1646 | } | |
1647 | ret = 1; | |
1648 | out: | |
1649 | return ret; | |
1650 | } | |
1651 | ||
1652 | /* | |
1653 | * Prepare to read connection handshake, or an ack. | |
1654 | */ | |
eed0ef2c SW |
1655 | static void prepare_read_banner(struct ceph_connection *con) |
1656 | { | |
1657 | dout("prepare_read_banner %p\n", con); | |
1658 | con->in_base_pos = 0; | |
1659 | } | |
1660 | ||
31b8006e SW |
1661 | static void prepare_read_connect(struct ceph_connection *con) |
1662 | { | |
1663 | dout("prepare_read_connect %p\n", con); | |
1664 | con->in_base_pos = 0; | |
1665 | } | |
1666 | ||
1667 | static void prepare_read_ack(struct ceph_connection *con) | |
1668 | { | |
1669 | dout("prepare_read_ack %p\n", con); | |
1670 | con->in_base_pos = 0; | |
1671 | } | |
1672 | ||
3a23083b SW |
1673 | static void prepare_read_seq(struct ceph_connection *con) |
1674 | { | |
1675 | dout("prepare_read_seq %p\n", con); | |
1676 | con->in_base_pos = 0; | |
1677 | con->in_tag = CEPH_MSGR_TAG_SEQ; | |
1678 | } | |
1679 | ||
31b8006e SW |
1680 | static void prepare_read_tag(struct ceph_connection *con) |
1681 | { | |
1682 | dout("prepare_read_tag %p\n", con); | |
1683 | con->in_base_pos = 0; | |
1684 | con->in_tag = CEPH_MSGR_TAG_READY; | |
1685 | } | |
1686 | ||
8b9558aa YZ |
1687 | static void prepare_read_keepalive_ack(struct ceph_connection *con) |
1688 | { | |
1689 | dout("prepare_read_keepalive_ack %p\n", con); | |
1690 | con->in_base_pos = 0; | |
1691 | } | |
1692 | ||
31b8006e SW |
1693 | /* |
1694 | * Prepare to read a message. | |
1695 | */ | |
1696 | static int prepare_read_message(struct ceph_connection *con) | |
1697 | { | |
1698 | dout("prepare_read_message %p\n", con); | |
1699 | BUG_ON(con->in_msg != NULL); | |
1700 | con->in_base_pos = 0; | |
1701 | con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0; | |
1702 | return 0; | |
1703 | } | |
1704 | ||
1705 | ||
1706 | static int read_partial(struct ceph_connection *con, | |
fd51653f | 1707 | int end, int size, void *object) |
31b8006e | 1708 | { |
e6cee71f AE |
1709 | while (con->in_base_pos < end) { |
1710 | int left = end - con->in_base_pos; | |
31b8006e SW |
1711 | int have = size - left; |
1712 | int ret = ceph_tcp_recvmsg(con->sock, object + have, left); | |
1713 | if (ret <= 0) | |
1714 | return ret; | |
1715 | con->in_base_pos += ret; | |
1716 | } | |
1717 | return 1; | |
1718 | } | |
1719 | ||
1720 | ||
1721 | /* | |
1722 | * Read all or part of the connect-side handshake on a new connection | |
1723 | */ | |
eed0ef2c | 1724 | static int read_partial_banner(struct ceph_connection *con) |
31b8006e | 1725 | { |
fd51653f AE |
1726 | int size; |
1727 | int end; | |
1728 | int ret; | |
31b8006e | 1729 | |
eed0ef2c | 1730 | dout("read_partial_banner %p at %d\n", con, con->in_base_pos); |
31b8006e SW |
1731 | |
1732 | /* peer's banner */ | |
fd51653f AE |
1733 | size = strlen(CEPH_BANNER); |
1734 | end = size; | |
1735 | ret = read_partial(con, end, size, con->in_banner); | |
31b8006e SW |
1736 | if (ret <= 0) |
1737 | goto out; | |
fd51653f AE |
1738 | |
1739 | size = sizeof (con->actual_peer_addr); | |
1740 | end += size; | |
1741 | ret = read_partial(con, end, size, &con->actual_peer_addr); | |
31b8006e SW |
1742 | if (ret <= 0) |
1743 | goto out; | |
fd51653f AE |
1744 | |
1745 | size = sizeof (con->peer_addr_for_me); | |
1746 | end += size; | |
1747 | ret = read_partial(con, end, size, &con->peer_addr_for_me); | |
31b8006e SW |
1748 | if (ret <= 0) |
1749 | goto out; | |
fd51653f | 1750 | |
eed0ef2c SW |
1751 | out: |
1752 | return ret; | |
1753 | } | |
1754 | ||
1755 | static int read_partial_connect(struct ceph_connection *con) | |
1756 | { | |
fd51653f AE |
1757 | int size; |
1758 | int end; | |
1759 | int ret; | |
eed0ef2c SW |
1760 | |
1761 | dout("read_partial_connect %p at %d\n", con, con->in_base_pos); | |
1762 | ||
fd51653f AE |
1763 | size = sizeof (con->in_reply); |
1764 | end = size; | |
1765 | ret = read_partial(con, end, size, &con->in_reply); | |
31b8006e SW |
1766 | if (ret <= 0) |
1767 | goto out; | |
fd51653f | 1768 | |
262614c4 ID |
1769 | if (con->auth) { |
1770 | size = le32_to_cpu(con->in_reply.authorizer_len); | |
130f52f2 ID |
1771 | if (size > con->auth->authorizer_reply_buf_len) { |
1772 | pr_err("authorizer reply too big: %d > %zu\n", size, | |
1773 | con->auth->authorizer_reply_buf_len); | |
1774 | ret = -EINVAL; | |
1775 | goto out; | |
1776 | } | |
1777 | ||
262614c4 ID |
1778 | end += size; |
1779 | ret = read_partial(con, end, size, | |
1780 | con->auth->authorizer_reply_buf); | |
1781 | if (ret <= 0) | |
1782 | goto out; | |
1783 | } | |
31b8006e | 1784 | |
4e7a5dcd SW |
1785 | dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n", |
1786 | con, (int)con->in_reply.tag, | |
1787 | le32_to_cpu(con->in_reply.connect_seq), | |
31b8006e SW |
1788 | le32_to_cpu(con->in_reply.global_seq)); |
1789 | out: | |
1790 | return ret; | |
1791 | } | |
1792 | ||
1793 | /* | |
1794 | * Verify the hello banner looks okay. | |
1795 | */ | |
1796 | static int verify_hello(struct ceph_connection *con) | |
1797 | { | |
1798 | if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) { | |
13e38c8a | 1799 | pr_err("connect to %s got bad banner\n", |
3d14c5d2 | 1800 | ceph_pr_addr(&con->peer_addr.in_addr)); |
31b8006e SW |
1801 | con->error_msg = "protocol error, bad banner"; |
1802 | return -1; | |
1803 | } | |
1804 | return 0; | |
1805 | } | |
1806 | ||
1807 | static bool addr_is_blank(struct sockaddr_storage *ss) | |
1808 | { | |
c44bd69c ID |
1809 | struct in_addr *addr = &((struct sockaddr_in *)ss)->sin_addr; |
1810 | struct in6_addr *addr6 = &((struct sockaddr_in6 *)ss)->sin6_addr; | |
1811 | ||
31b8006e SW |
1812 | switch (ss->ss_family) { |
1813 | case AF_INET: | |
c44bd69c | 1814 | return addr->s_addr == htonl(INADDR_ANY); |
31b8006e | 1815 | case AF_INET6: |
c44bd69c ID |
1816 | return ipv6_addr_any(addr6); |
1817 | default: | |
1818 | return true; | |
31b8006e | 1819 | } |
31b8006e SW |
1820 | } |
1821 | ||
1822 | static int addr_port(struct sockaddr_storage *ss) | |
1823 | { | |
1824 | switch (ss->ss_family) { | |
1825 | case AF_INET: | |
f28bcfbe | 1826 | return ntohs(((struct sockaddr_in *)ss)->sin_port); |
31b8006e | 1827 | case AF_INET6: |
f28bcfbe | 1828 | return ntohs(((struct sockaddr_in6 *)ss)->sin6_port); |
31b8006e SW |
1829 | } |
1830 | return 0; | |
1831 | } | |
1832 | ||
1833 | static void addr_set_port(struct sockaddr_storage *ss, int p) | |
1834 | { | |
1835 | switch (ss->ss_family) { | |
1836 | case AF_INET: | |
1837 | ((struct sockaddr_in *)ss)->sin_port = htons(p); | |
a2a79609 | 1838 | break; |
31b8006e SW |
1839 | case AF_INET6: |
1840 | ((struct sockaddr_in6 *)ss)->sin6_port = htons(p); | |
a2a79609 | 1841 | break; |
31b8006e SW |
1842 | } |
1843 | } | |
1844 | ||
ee3b56f2 NW |
1845 | /* |
1846 | * Unlike other *_pton function semantics, zero indicates success. | |
1847 | */ | |
1848 | static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss, | |
1849 | char delim, const char **ipend) | |
1850 | { | |
99f0f3b2 AE |
1851 | struct sockaddr_in *in4 = (struct sockaddr_in *) ss; |
1852 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss; | |
ee3b56f2 NW |
1853 | |
1854 | memset(ss, 0, sizeof(*ss)); | |
1855 | ||
1856 | if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) { | |
1857 | ss->ss_family = AF_INET; | |
1858 | return 0; | |
1859 | } | |
1860 | ||
1861 | if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) { | |
1862 | ss->ss_family = AF_INET6; | |
1863 | return 0; | |
1864 | } | |
1865 | ||
1866 | return -EINVAL; | |
1867 | } | |
1868 | ||
1869 | /* | |
1870 | * Extract hostname string and resolve using kernel DNS facility. | |
1871 | */ | |
1872 | #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER | |
1873 | static int ceph_dns_resolve_name(const char *name, size_t namelen, | |
1874 | struct sockaddr_storage *ss, char delim, const char **ipend) | |
1875 | { | |
1876 | const char *end, *delim_p; | |
1877 | char *colon_p, *ip_addr = NULL; | |
1878 | int ip_len, ret; | |
1879 | ||
1880 | /* | |
1881 | * The end of the hostname occurs immediately preceding the delimiter or | |
1882 | * the port marker (':') where the delimiter takes precedence. | |
1883 | */ | |
1884 | delim_p = memchr(name, delim, namelen); | |
1885 | colon_p = memchr(name, ':', namelen); | |
1886 | ||
1887 | if (delim_p && colon_p) | |
1888 | end = delim_p < colon_p ? delim_p : colon_p; | |
1889 | else if (!delim_p && colon_p) | |
1890 | end = colon_p; | |
1891 | else { | |
1892 | end = delim_p; | |
1893 | if (!end) /* case: hostname:/ */ | |
1894 | end = name + namelen; | |
1895 | } | |
1896 | ||
1897 | if (end <= name) | |
1898 | return -EINVAL; | |
1899 | ||
1900 | /* do dns_resolve upcall */ | |
1901 | ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL); | |
1902 | if (ip_len > 0) | |
1903 | ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL); | |
1904 | else | |
1905 | ret = -ESRCH; | |
1906 | ||
1907 | kfree(ip_addr); | |
1908 | ||
1909 | *ipend = end; | |
1910 | ||
1911 | pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name, | |
1912 | ret, ret ? "failed" : ceph_pr_addr(ss)); | |
1913 | ||
1914 | return ret; | |
1915 | } | |
1916 | #else | |
1917 | static inline int ceph_dns_resolve_name(const char *name, size_t namelen, | |
1918 | struct sockaddr_storage *ss, char delim, const char **ipend) | |
1919 | { | |
1920 | return -EINVAL; | |
1921 | } | |
1922 | #endif | |
1923 | ||
1924 | /* | |
1925 | * Parse a server name (IP or hostname). If a valid IP address is not found | |
1926 | * then try to extract a hostname to resolve using userspace DNS upcall. | |
1927 | */ | |
1928 | static int ceph_parse_server_name(const char *name, size_t namelen, | |
1929 | struct sockaddr_storage *ss, char delim, const char **ipend) | |
1930 | { | |
1931 | int ret; | |
1932 | ||
1933 | ret = ceph_pton(name, namelen, ss, delim, ipend); | |
1934 | if (ret) | |
1935 | ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend); | |
1936 | ||
1937 | return ret; | |
1938 | } | |
1939 | ||
31b8006e SW |
1940 | /* |
1941 | * Parse an ip[:port] list into an addr array. Use the default | |
1942 | * monitor port if a port isn't specified. | |
1943 | */ | |
1944 | int ceph_parse_ips(const char *c, const char *end, | |
1945 | struct ceph_entity_addr *addr, | |
1946 | int max_count, int *count) | |
1947 | { | |
ee3b56f2 | 1948 | int i, ret = -EINVAL; |
31b8006e SW |
1949 | const char *p = c; |
1950 | ||
1951 | dout("parse_ips on '%.*s'\n", (int)(end-c), c); | |
1952 | for (i = 0; i < max_count; i++) { | |
1953 | const char *ipend; | |
1954 | struct sockaddr_storage *ss = &addr[i].in_addr; | |
31b8006e | 1955 | int port; |
39139f64 SW |
1956 | char delim = ','; |
1957 | ||
1958 | if (*p == '[') { | |
1959 | delim = ']'; | |
1960 | p++; | |
1961 | } | |
31b8006e | 1962 | |
ee3b56f2 NW |
1963 | ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend); |
1964 | if (ret) | |
31b8006e | 1965 | goto bad; |
ee3b56f2 NW |
1966 | ret = -EINVAL; |
1967 | ||
31b8006e SW |
1968 | p = ipend; |
1969 | ||
39139f64 SW |
1970 | if (delim == ']') { |
1971 | if (*p != ']') { | |
1972 | dout("missing matching ']'\n"); | |
1973 | goto bad; | |
1974 | } | |
1975 | p++; | |
1976 | } | |
1977 | ||
31b8006e SW |
1978 | /* port? */ |
1979 | if (p < end && *p == ':') { | |
1980 | port = 0; | |
1981 | p++; | |
1982 | while (p < end && *p >= '0' && *p <= '9') { | |
1983 | port = (port * 10) + (*p - '0'); | |
1984 | p++; | |
1985 | } | |
f48db1e9 ID |
1986 | if (port == 0) |
1987 | port = CEPH_MON_PORT; | |
1988 | else if (port > 65535) | |
31b8006e SW |
1989 | goto bad; |
1990 | } else { | |
1991 | port = CEPH_MON_PORT; | |
1992 | } | |
1993 | ||
1994 | addr_set_port(ss, port); | |
1995 | ||
3d14c5d2 | 1996 | dout("parse_ips got %s\n", ceph_pr_addr(ss)); |
31b8006e SW |
1997 | |
1998 | if (p == end) | |
1999 | break; | |
2000 | if (*p != ',') | |
2001 | goto bad; | |
2002 | p++; | |
2003 | } | |
2004 | ||
2005 | if (p != end) | |
2006 | goto bad; | |
2007 | ||
2008 | if (count) | |
2009 | *count = i + 1; | |
2010 | return 0; | |
2011 | ||
2012 | bad: | |
39139f64 | 2013 | pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c); |
ee3b56f2 | 2014 | return ret; |
31b8006e | 2015 | } |
3d14c5d2 | 2016 | EXPORT_SYMBOL(ceph_parse_ips); |
31b8006e | 2017 | |
eed0ef2c | 2018 | static int process_banner(struct ceph_connection *con) |
31b8006e | 2019 | { |
eed0ef2c | 2020 | dout("process_banner on %p\n", con); |
31b8006e SW |
2021 | |
2022 | if (verify_hello(con) < 0) | |
2023 | return -1; | |
2024 | ||
63f2d211 SW |
2025 | ceph_decode_addr(&con->actual_peer_addr); |
2026 | ceph_decode_addr(&con->peer_addr_for_me); | |
2027 | ||
31b8006e SW |
2028 | /* |
2029 | * Make sure the other end is who we wanted. note that the other | |
2030 | * end may not yet know their ip address, so if it's 0.0.0.0, give | |
2031 | * them the benefit of the doubt. | |
2032 | */ | |
103e2d3a SW |
2033 | if (memcmp(&con->peer_addr, &con->actual_peer_addr, |
2034 | sizeof(con->peer_addr)) != 0 && | |
31b8006e SW |
2035 | !(addr_is_blank(&con->actual_peer_addr.in_addr) && |
2036 | con->actual_peer_addr.nonce == con->peer_addr.nonce)) { | |
b9a67899 JP |
2037 | pr_warn("wrong peer, want %s/%d, got %s/%d\n", |
2038 | ceph_pr_addr(&con->peer_addr.in_addr), | |
2039 | (int)le32_to_cpu(con->peer_addr.nonce), | |
2040 | ceph_pr_addr(&con->actual_peer_addr.in_addr), | |
2041 | (int)le32_to_cpu(con->actual_peer_addr.nonce)); | |
58bb3b37 | 2042 | con->error_msg = "wrong peer at address"; |
31b8006e SW |
2043 | return -1; |
2044 | } | |
2045 | ||
2046 | /* | |
2047 | * did we learn our address? | |
2048 | */ | |
2049 | if (addr_is_blank(&con->msgr->inst.addr.in_addr)) { | |
2050 | int port = addr_port(&con->msgr->inst.addr.in_addr); | |
2051 | ||
2052 | memcpy(&con->msgr->inst.addr.in_addr, | |
2053 | &con->peer_addr_for_me.in_addr, | |
2054 | sizeof(con->peer_addr_for_me.in_addr)); | |
2055 | addr_set_port(&con->msgr->inst.addr.in_addr, port); | |
63f2d211 | 2056 | encode_my_addr(con->msgr); |
eed0ef2c | 2057 | dout("process_banner learned my addr is %s\n", |
3d14c5d2 | 2058 | ceph_pr_addr(&con->msgr->inst.addr.in_addr)); |
31b8006e SW |
2059 | } |
2060 | ||
eed0ef2c SW |
2061 | return 0; |
2062 | } | |
2063 | ||
2064 | static int process_connect(struct ceph_connection *con) | |
2065 | { | |
859bff51 ID |
2066 | u64 sup_feat = from_msgr(con->msgr)->supported_features; |
2067 | u64 req_feat = from_msgr(con->msgr)->required_features; | |
dcbbd97c | 2068 | u64 server_feat = le64_to_cpu(con->in_reply.features); |
0da5d703 | 2069 | int ret; |
04a419f9 | 2070 | |
eed0ef2c SW |
2071 | dout("process_connect on %p tag %d\n", con, (int)con->in_tag); |
2072 | ||
262614c4 | 2073 | if (con->auth) { |
5c056fdc ID |
2074 | /* |
2075 | * Any connection that defines ->get_authorizer() | |
6daca13d ID |
2076 | * should also define ->add_authorizer_challenge() and |
2077 | * ->verify_authorizer_reply(). | |
2078 | * | |
5c056fdc ID |
2079 | * See get_connect_authorizer(). |
2080 | */ | |
6daca13d ID |
2081 | if (con->in_reply.tag == CEPH_MSGR_TAG_CHALLENGE_AUTHORIZER) { |
2082 | ret = con->ops->add_authorizer_challenge( | |
2083 | con, con->auth->authorizer_reply_buf, | |
2084 | le32_to_cpu(con->in_reply.authorizer_len)); | |
2085 | if (ret < 0) | |
2086 | return ret; | |
2087 | ||
2088 | con_out_kvec_reset(con); | |
2089 | __prepare_write_connect(con); | |
2090 | prepare_read_connect(con); | |
2091 | return 0; | |
2092 | } | |
2093 | ||
0dde5848 | 2094 | ret = con->ops->verify_authorizer_reply(con); |
5c056fdc ID |
2095 | if (ret < 0) { |
2096 | con->error_msg = "bad authorize reply"; | |
2097 | return ret; | |
2098 | } | |
2099 | } | |
2100 | ||
31b8006e | 2101 | switch (con->in_reply.tag) { |
04a419f9 SW |
2102 | case CEPH_MSGR_TAG_FEATURES: |
2103 | pr_err("%s%lld %s feature set mismatch," | |
2104 | " my %llx < server's %llx, missing %llx\n", | |
2105 | ENTITY_NAME(con->peer_name), | |
3d14c5d2 | 2106 | ceph_pr_addr(&con->peer_addr.in_addr), |
04a419f9 SW |
2107 | sup_feat, server_feat, server_feat & ~sup_feat); |
2108 | con->error_msg = "missing required protocol features"; | |
0fa6ebc6 | 2109 | reset_connection(con); |
04a419f9 SW |
2110 | return -1; |
2111 | ||
31b8006e | 2112 | case CEPH_MSGR_TAG_BADPROTOVER: |
31b8006e SW |
2113 | pr_err("%s%lld %s protocol version mismatch," |
2114 | " my %d != server's %d\n", | |
2115 | ENTITY_NAME(con->peer_name), | |
3d14c5d2 | 2116 | ceph_pr_addr(&con->peer_addr.in_addr), |
31b8006e SW |
2117 | le32_to_cpu(con->out_connect.protocol_version), |
2118 | le32_to_cpu(con->in_reply.protocol_version)); | |
2119 | con->error_msg = "protocol version mismatch"; | |
0fa6ebc6 | 2120 | reset_connection(con); |
31b8006e SW |
2121 | return -1; |
2122 | ||
4e7a5dcd SW |
2123 | case CEPH_MSGR_TAG_BADAUTHORIZER: |
2124 | con->auth_retry++; | |
2125 | dout("process_connect %p got BADAUTHORIZER attempt %d\n", con, | |
2126 | con->auth_retry); | |
2127 | if (con->auth_retry == 2) { | |
2128 | con->error_msg = "connect authorization failure"; | |
4e7a5dcd SW |
2129 | return -1; |
2130 | } | |
6d4221b5 | 2131 | con_out_kvec_reset(con); |
e825a66d | 2132 | ret = prepare_write_connect(con); |
0da5d703 SW |
2133 | if (ret < 0) |
2134 | return ret; | |
63733a0f | 2135 | prepare_read_connect(con); |
4e7a5dcd | 2136 | break; |
31b8006e SW |
2137 | |
2138 | case CEPH_MSGR_TAG_RESETSESSION: | |
2139 | /* | |
2140 | * If we connected with a large connect_seq but the peer | |
2141 | * has no record of a session with us (no connection, or | |
2142 | * connect_seq == 0), they will send RESETSESION to indicate | |
2143 | * that they must have reset their session, and may have | |
2144 | * dropped messages. | |
2145 | */ | |
2146 | dout("process_connect got RESET peer seq %u\n", | |
5bdca4e0 | 2147 | le32_to_cpu(con->in_reply.connect_seq)); |
31b8006e SW |
2148 | pr_err("%s%lld %s connection reset\n", |
2149 | ENTITY_NAME(con->peer_name), | |
3d14c5d2 | 2150 | ceph_pr_addr(&con->peer_addr.in_addr)); |
31b8006e | 2151 | reset_connection(con); |
6d4221b5 | 2152 | con_out_kvec_reset(con); |
5a0f8fdd AE |
2153 | ret = prepare_write_connect(con); |
2154 | if (ret < 0) | |
2155 | return ret; | |
31b8006e SW |
2156 | prepare_read_connect(con); |
2157 | ||
2158 | /* Tell ceph about it. */ | |
ec302645 | 2159 | mutex_unlock(&con->mutex); |
31b8006e SW |
2160 | pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name)); |
2161 | if (con->ops->peer_reset) | |
2162 | con->ops->peer_reset(con); | |
ec302645 | 2163 | mutex_lock(&con->mutex); |
8dacc7da | 2164 | if (con->state != CON_STATE_NEGOTIATING) |
0da5d703 | 2165 | return -EAGAIN; |
31b8006e SW |
2166 | break; |
2167 | ||
2168 | case CEPH_MSGR_TAG_RETRY_SESSION: | |
2169 | /* | |
2170 | * If we sent a smaller connect_seq than the peer has, try | |
2171 | * again with a larger value. | |
2172 | */ | |
5bdca4e0 | 2173 | dout("process_connect got RETRY_SESSION my seq %u, peer %u\n", |
31b8006e | 2174 | le32_to_cpu(con->out_connect.connect_seq), |
5bdca4e0 SW |
2175 | le32_to_cpu(con->in_reply.connect_seq)); |
2176 | con->connect_seq = le32_to_cpu(con->in_reply.connect_seq); | |
6d4221b5 | 2177 | con_out_kvec_reset(con); |
5a0f8fdd AE |
2178 | ret = prepare_write_connect(con); |
2179 | if (ret < 0) | |
2180 | return ret; | |
31b8006e SW |
2181 | prepare_read_connect(con); |
2182 | break; | |
2183 | ||
2184 | case CEPH_MSGR_TAG_RETRY_GLOBAL: | |
2185 | /* | |
2186 | * If we sent a smaller global_seq than the peer has, try | |
2187 | * again with a larger value. | |
2188 | */ | |
eed0ef2c | 2189 | dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n", |
31b8006e | 2190 | con->peer_global_seq, |
5bdca4e0 | 2191 | le32_to_cpu(con->in_reply.global_seq)); |
31b8006e | 2192 | get_global_seq(con->msgr, |
5bdca4e0 | 2193 | le32_to_cpu(con->in_reply.global_seq)); |
6d4221b5 | 2194 | con_out_kvec_reset(con); |
5a0f8fdd AE |
2195 | ret = prepare_write_connect(con); |
2196 | if (ret < 0) | |
2197 | return ret; | |
31b8006e SW |
2198 | prepare_read_connect(con); |
2199 | break; | |
2200 | ||
3a23083b | 2201 | case CEPH_MSGR_TAG_SEQ: |
31b8006e | 2202 | case CEPH_MSGR_TAG_READY: |
04a419f9 SW |
2203 | if (req_feat & ~server_feat) { |
2204 | pr_err("%s%lld %s protocol feature mismatch," | |
2205 | " my required %llx > server's %llx, need %llx\n", | |
2206 | ENTITY_NAME(con->peer_name), | |
3d14c5d2 | 2207 | ceph_pr_addr(&con->peer_addr.in_addr), |
04a419f9 SW |
2208 | req_feat, server_feat, req_feat & ~server_feat); |
2209 | con->error_msg = "missing required protocol features"; | |
0fa6ebc6 | 2210 | reset_connection(con); |
04a419f9 SW |
2211 | return -1; |
2212 | } | |
8dacc7da | 2213 | |
122070a2 | 2214 | WARN_ON(con->state != CON_STATE_NEGOTIATING); |
8dacc7da | 2215 | con->state = CON_STATE_OPEN; |
20e55c4c | 2216 | con->auth_retry = 0; /* we authenticated; clear flag */ |
31b8006e SW |
2217 | con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq); |
2218 | con->connect_seq++; | |
aba558e2 | 2219 | con->peer_features = server_feat; |
31b8006e SW |
2220 | dout("process_connect got READY gseq %d cseq %d (%d)\n", |
2221 | con->peer_global_seq, | |
2222 | le32_to_cpu(con->in_reply.connect_seq), | |
2223 | con->connect_seq); | |
2224 | WARN_ON(con->connect_seq != | |
2225 | le32_to_cpu(con->in_reply.connect_seq)); | |
92ac41d0 SW |
2226 | |
2227 | if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY) | |
c9ffc77a | 2228 | con_flag_set(con, CON_FLAG_LOSSYTX); |
92ac41d0 | 2229 | |
85effe18 | 2230 | con->delay = 0; /* reset backoff memory */ |
92ac41d0 | 2231 | |
3a23083b SW |
2232 | if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) { |
2233 | prepare_write_seq(con); | |
2234 | prepare_read_seq(con); | |
2235 | } else { | |
2236 | prepare_read_tag(con); | |
2237 | } | |
31b8006e SW |
2238 | break; |
2239 | ||
2240 | case CEPH_MSGR_TAG_WAIT: | |
2241 | /* | |
2242 | * If there is a connection race (we are opening | |
2243 | * connections to each other), one of us may just have | |
2244 | * to WAIT. This shouldn't happen if we are the | |
2245 | * client. | |
2246 | */ | |
04177882 SW |
2247 | con->error_msg = "protocol error, got WAIT as client"; |
2248 | return -1; | |
31b8006e SW |
2249 | |
2250 | default: | |
31b8006e SW |
2251 | con->error_msg = "protocol error, garbage tag during connect"; |
2252 | return -1; | |
2253 | } | |
2254 | return 0; | |
2255 | } | |
2256 | ||
2257 | ||
2258 | /* | |
2259 | * read (part of) an ack | |
2260 | */ | |
2261 | static int read_partial_ack(struct ceph_connection *con) | |
2262 | { | |
fd51653f AE |
2263 | int size = sizeof (con->in_temp_ack); |
2264 | int end = size; | |
31b8006e | 2265 | |
fd51653f | 2266 | return read_partial(con, end, size, &con->in_temp_ack); |
31b8006e SW |
2267 | } |
2268 | ||
31b8006e SW |
2269 | /* |
2270 | * We can finally discard anything that's been acked. | |
2271 | */ | |
2272 | static void process_ack(struct ceph_connection *con) | |
2273 | { | |
2274 | struct ceph_msg *m; | |
2275 | u64 ack = le64_to_cpu(con->in_temp_ack); | |
2276 | u64 seq; | |
0a2ad541 YZ |
2277 | bool reconnect = (con->in_tag == CEPH_MSGR_TAG_SEQ); |
2278 | struct list_head *list = reconnect ? &con->out_queue : &con->out_sent; | |
31b8006e | 2279 | |
0a2ad541 YZ |
2280 | /* |
2281 | * In the reconnect case, con_fault() has requeued messages | |
2282 | * in out_sent. We should cleanup old messages according to | |
2283 | * the reconnect seq. | |
2284 | */ | |
2285 | while (!list_empty(list)) { | |
2286 | m = list_first_entry(list, struct ceph_msg, list_head); | |
2287 | if (reconnect && m->needs_out_seq) | |
2288 | break; | |
31b8006e SW |
2289 | seq = le64_to_cpu(m->hdr.seq); |
2290 | if (seq > ack) | |
2291 | break; | |
2292 | dout("got ack for seq %llu type %d at %p\n", seq, | |
2293 | le16_to_cpu(m->hdr.type), m); | |
4cf9d544 | 2294 | m->ack_stamp = jiffies; |
31b8006e SW |
2295 | ceph_msg_remove(m); |
2296 | } | |
0a2ad541 | 2297 | |
31b8006e SW |
2298 | prepare_read_tag(con); |
2299 | } | |
2300 | ||
2301 | ||
2450418c | 2302 | static int read_partial_message_section(struct ceph_connection *con, |
213c99ee SW |
2303 | struct kvec *section, |
2304 | unsigned int sec_len, u32 *crc) | |
2450418c | 2305 | { |
68b4476b | 2306 | int ret, left; |
2450418c YS |
2307 | |
2308 | BUG_ON(!section); | |
2309 | ||
2310 | while (section->iov_len < sec_len) { | |
2311 | BUG_ON(section->iov_base == NULL); | |
2312 | left = sec_len - section->iov_len; | |
2313 | ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base + | |
2314 | section->iov_len, left); | |
2315 | if (ret <= 0) | |
2316 | return ret; | |
2317 | section->iov_len += ret; | |
2450418c | 2318 | } |
fe3ad593 AE |
2319 | if (section->iov_len == sec_len) |
2320 | *crc = crc32c(0, section->iov_base, section->iov_len); | |
31b8006e | 2321 | |
2450418c YS |
2322 | return 1; |
2323 | } | |
31b8006e | 2324 | |
34d2d200 AE |
2325 | static int read_partial_msg_data(struct ceph_connection *con) |
2326 | { | |
2327 | struct ceph_msg *msg = con->in_msg; | |
8ae4f4f5 | 2328 | struct ceph_msg_data_cursor *cursor = &msg->cursor; |
859bff51 | 2329 | bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC); |
686be208 AE |
2330 | struct page *page; |
2331 | size_t page_offset; | |
2332 | size_t length; | |
f5db90bc | 2333 | u32 crc = 0; |
34d2d200 AE |
2334 | int ret; |
2335 | ||
0d9c1ab3 | 2336 | if (!msg->num_data_items) |
4c59b4a2 | 2337 | return -EIO; |
34d2d200 | 2338 | |
f5db90bc AE |
2339 | if (do_datacrc) |
2340 | crc = con->in_data_crc; | |
45a267db ID |
2341 | while (cursor->total_resid) { |
2342 | if (!cursor->resid) { | |
2343 | ceph_msg_data_advance(cursor, 0); | |
2344 | continue; | |
2345 | } | |
2346 | ||
343128ce | 2347 | page = ceph_msg_data_next(cursor, &page_offset, &length, NULL); |
686be208 | 2348 | ret = ceph_tcp_recvpage(con->sock, page, page_offset, length); |
f5db90bc AE |
2349 | if (ret <= 0) { |
2350 | if (do_datacrc) | |
2351 | con->in_data_crc = crc; | |
2352 | ||
686be208 | 2353 | return ret; |
f5db90bc | 2354 | } |
686be208 AE |
2355 | |
2356 | if (do_datacrc) | |
f5db90bc | 2357 | crc = ceph_crc32c_page(crc, page, page_offset, ret); |
1759f7b0 | 2358 | ceph_msg_data_advance(cursor, (size_t)ret); |
34d2d200 | 2359 | } |
f5db90bc AE |
2360 | if (do_datacrc) |
2361 | con->in_data_crc = crc; | |
34d2d200 AE |
2362 | |
2363 | return 1; /* must return > 0 to indicate success */ | |
2364 | } | |
2365 | ||
31b8006e SW |
2366 | /* |
2367 | * read (part of) a message. | |
2368 | */ | |
686be208 AE |
2369 | static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip); |
2370 | ||
31b8006e SW |
2371 | static int read_partial_message(struct ceph_connection *con) |
2372 | { | |
2373 | struct ceph_msg *m = con->in_msg; | |
fd51653f AE |
2374 | int size; |
2375 | int end; | |
31b8006e | 2376 | int ret; |
95c96174 | 2377 | unsigned int front_len, middle_len, data_len; |
859bff51 | 2378 | bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC); |
33d07337 | 2379 | bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH); |
ae18756b | 2380 | u64 seq; |
fe3ad593 | 2381 | u32 crc; |
31b8006e SW |
2382 | |
2383 | dout("read_partial_message con %p msg %p\n", con, m); | |
2384 | ||
2385 | /* header */ | |
fd51653f AE |
2386 | size = sizeof (con->in_hdr); |
2387 | end = size; | |
2388 | ret = read_partial(con, end, size, &con->in_hdr); | |
57dac9d1 AE |
2389 | if (ret <= 0) |
2390 | return ret; | |
fe3ad593 AE |
2391 | |
2392 | crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc)); | |
2393 | if (cpu_to_le32(crc) != con->in_hdr.crc) { | |
67c64eb7 | 2394 | pr_err("read_partial_message bad hdr crc %u != expected %u\n", |
fe3ad593 AE |
2395 | crc, con->in_hdr.crc); |
2396 | return -EBADMSG; | |
2397 | } | |
2398 | ||
31b8006e SW |
2399 | front_len = le32_to_cpu(con->in_hdr.front_len); |
2400 | if (front_len > CEPH_MSG_MAX_FRONT_LEN) | |
2401 | return -EIO; | |
2402 | middle_len = le32_to_cpu(con->in_hdr.middle_len); | |
7b11ba37 | 2403 | if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN) |
31b8006e SW |
2404 | return -EIO; |
2405 | data_len = le32_to_cpu(con->in_hdr.data_len); | |
2406 | if (data_len > CEPH_MSG_MAX_DATA_LEN) | |
2407 | return -EIO; | |
2408 | ||
ae18756b SW |
2409 | /* verify seq# */ |
2410 | seq = le64_to_cpu(con->in_hdr.seq); | |
2411 | if ((s64)seq - (s64)con->in_seq < 1) { | |
df9f86fa | 2412 | pr_info("skipping %s%lld %s seq %lld expected %lld\n", |
ae18756b | 2413 | ENTITY_NAME(con->peer_name), |
3d14c5d2 | 2414 | ceph_pr_addr(&con->peer_addr.in_addr), |
ae18756b SW |
2415 | seq, con->in_seq + 1); |
2416 | con->in_base_pos = -front_len - middle_len - data_len - | |
dbc0d3ca | 2417 | sizeof_footer(con); |
ae18756b | 2418 | con->in_tag = CEPH_MSGR_TAG_READY; |
e7a88e82 | 2419 | return 1; |
ae18756b SW |
2420 | } else if ((s64)seq - (s64)con->in_seq > 1) { |
2421 | pr_err("read_partial_message bad seq %lld expected %lld\n", | |
2422 | seq, con->in_seq + 1); | |
2423 | con->error_msg = "bad message sequence # for incoming message"; | |
67c64eb7 | 2424 | return -EBADE; |
ae18756b SW |
2425 | } |
2426 | ||
31b8006e SW |
2427 | /* allocate message? */ |
2428 | if (!con->in_msg) { | |
4740a623 SW |
2429 | int skip = 0; |
2430 | ||
31b8006e | 2431 | dout("got hdr type %d front %d data %d\n", con->in_hdr.type, |
6ebc8b32 | 2432 | front_len, data_len); |
4740a623 SW |
2433 | ret = ceph_con_in_msg_alloc(con, &skip); |
2434 | if (ret < 0) | |
2435 | return ret; | |
f759ebb9 AE |
2436 | |
2437 | BUG_ON(!con->in_msg ^ skip); | |
2450418c | 2438 | if (skip) { |
31b8006e | 2439 | /* skip this message */ |
a79832f2 | 2440 | dout("alloc_msg said skip message\n"); |
31b8006e | 2441 | con->in_base_pos = -front_len - middle_len - data_len - |
dbc0d3ca | 2442 | sizeof_footer(con); |
31b8006e | 2443 | con->in_tag = CEPH_MSGR_TAG_READY; |
684be25c | 2444 | con->in_seq++; |
e7a88e82 | 2445 | return 1; |
31b8006e | 2446 | } |
38941f80 | 2447 | |
4740a623 | 2448 | BUG_ON(!con->in_msg); |
38941f80 | 2449 | BUG_ON(con->in_msg->con != con); |
31b8006e SW |
2450 | m = con->in_msg; |
2451 | m->front.iov_len = 0; /* haven't read it yet */ | |
2450418c YS |
2452 | if (m->middle) |
2453 | m->middle->vec.iov_len = 0; | |
9d7f0f13 | 2454 | |
78625051 | 2455 | /* prepare for data payload, if any */ |
a4107026 | 2456 | |
78625051 | 2457 | if (data_len) |
98fa5dd8 | 2458 | prepare_message_data(con->in_msg, data_len); |
31b8006e SW |
2459 | } |
2460 | ||
2461 | /* front */ | |
2450418c YS |
2462 | ret = read_partial_message_section(con, &m->front, front_len, |
2463 | &con->in_front_crc); | |
2464 | if (ret <= 0) | |
2465 | return ret; | |
31b8006e SW |
2466 | |
2467 | /* middle */ | |
2450418c | 2468 | if (m->middle) { |
213c99ee SW |
2469 | ret = read_partial_message_section(con, &m->middle->vec, |
2470 | middle_len, | |
2450418c | 2471 | &con->in_middle_crc); |
31b8006e SW |
2472 | if (ret <= 0) |
2473 | return ret; | |
31b8006e SW |
2474 | } |
2475 | ||
2476 | /* (page) data */ | |
34d2d200 AE |
2477 | if (data_len) { |
2478 | ret = read_partial_msg_data(con); | |
2479 | if (ret <= 0) | |
2480 | return ret; | |
31b8006e SW |
2481 | } |
2482 | ||
31b8006e | 2483 | /* footer */ |
89f08173 | 2484 | size = sizeof_footer(con); |
fd51653f AE |
2485 | end += size; |
2486 | ret = read_partial(con, end, size, &m->footer); | |
57dac9d1 AE |
2487 | if (ret <= 0) |
2488 | return ret; | |
2489 | ||
33d07337 YZ |
2490 | if (!need_sign) { |
2491 | m->footer.flags = m->old_footer.flags; | |
2492 | m->footer.sig = 0; | |
2493 | } | |
2494 | ||
31b8006e SW |
2495 | dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n", |
2496 | m, front_len, m->footer.front_crc, middle_len, | |
2497 | m->footer.middle_crc, data_len, m->footer.data_crc); | |
2498 | ||
2499 | /* crc ok? */ | |
2500 | if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) { | |
2501 | pr_err("read_partial_message %p front crc %u != exp. %u\n", | |
2502 | m, con->in_front_crc, m->footer.front_crc); | |
2503 | return -EBADMSG; | |
2504 | } | |
2505 | if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) { | |
2506 | pr_err("read_partial_message %p middle crc %u != exp %u\n", | |
2507 | m, con->in_middle_crc, m->footer.middle_crc); | |
2508 | return -EBADMSG; | |
2509 | } | |
bca064d2 | 2510 | if (do_datacrc && |
31b8006e SW |
2511 | (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 && |
2512 | con->in_data_crc != le32_to_cpu(m->footer.data_crc)) { | |
2513 | pr_err("read_partial_message %p data crc %u != exp. %u\n", m, | |
2514 | con->in_data_crc, le32_to_cpu(m->footer.data_crc)); | |
2515 | return -EBADMSG; | |
2516 | } | |
2517 | ||
33d07337 | 2518 | if (need_sign && con->ops->check_message_signature && |
79dbd1ba | 2519 | con->ops->check_message_signature(m)) { |
33d07337 YZ |
2520 | pr_err("read_partial_message %p signature check failed\n", m); |
2521 | return -EBADMSG; | |
2522 | } | |
2523 | ||
31b8006e SW |
2524 | return 1; /* done! */ |
2525 | } | |
2526 | ||
2527 | /* | |
2528 | * Process message. This happens in the worker thread. The callback should | |
2529 | * be careful not to do anything that waits on other incoming messages or it | |
2530 | * may deadlock. | |
2531 | */ | |
2532 | static void process_message(struct ceph_connection *con) | |
2533 | { | |
583d0fef | 2534 | struct ceph_msg *msg = con->in_msg; |
31b8006e | 2535 | |
38941f80 | 2536 | BUG_ON(con->in_msg->con != con); |
31b8006e SW |
2537 | con->in_msg = NULL; |
2538 | ||
2539 | /* if first message, set peer_name */ | |
2540 | if (con->peer_name.type == 0) | |
dbad185d | 2541 | con->peer_name = msg->hdr.src; |
31b8006e | 2542 | |
31b8006e | 2543 | con->in_seq++; |
ec302645 | 2544 | mutex_unlock(&con->mutex); |
31b8006e SW |
2545 | |
2546 | dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n", | |
2547 | msg, le64_to_cpu(msg->hdr.seq), | |
dbad185d | 2548 | ENTITY_NAME(msg->hdr.src), |
31b8006e SW |
2549 | le16_to_cpu(msg->hdr.type), |
2550 | ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), | |
2551 | le32_to_cpu(msg->hdr.front_len), | |
2552 | le32_to_cpu(msg->hdr.data_len), | |
2553 | con->in_front_crc, con->in_middle_crc, con->in_data_crc); | |
2554 | con->ops->dispatch(con, msg); | |
ec302645 SW |
2555 | |
2556 | mutex_lock(&con->mutex); | |
31b8006e SW |
2557 | } |
2558 | ||
8b9558aa YZ |
2559 | static int read_keepalive_ack(struct ceph_connection *con) |
2560 | { | |
2561 | struct ceph_timespec ceph_ts; | |
2562 | size_t size = sizeof(ceph_ts); | |
2563 | int ret = read_partial(con, size, size, &ceph_ts); | |
2564 | if (ret <= 0) | |
2565 | return ret; | |
473bd2d7 | 2566 | ceph_decode_timespec64(&con->last_keepalive_ack, &ceph_ts); |
8b9558aa YZ |
2567 | prepare_read_tag(con); |
2568 | return 1; | |
2569 | } | |
31b8006e SW |
2570 | |
2571 | /* | |
2572 | * Write something to the socket. Called in a worker thread when the | |
2573 | * socket appears to be writeable and we have something ready to send. | |
2574 | */ | |
2575 | static int try_write(struct ceph_connection *con) | |
2576 | { | |
31b8006e SW |
2577 | int ret = 1; |
2578 | ||
d59315ca | 2579 | dout("try_write start %p state %lu\n", con, con->state); |
9c55ad1c ID |
2580 | if (con->state != CON_STATE_PREOPEN && |
2581 | con->state != CON_STATE_CONNECTING && | |
2582 | con->state != CON_STATE_NEGOTIATING && | |
2583 | con->state != CON_STATE_OPEN) | |
2584 | return 0; | |
31b8006e | 2585 | |
31b8006e | 2586 | /* open the socket first? */ |
8dacc7da SW |
2587 | if (con->state == CON_STATE_PREOPEN) { |
2588 | BUG_ON(con->sock); | |
2589 | con->state = CON_STATE_CONNECTING; | |
a5988c49 | 2590 | |
e2200423 | 2591 | con_out_kvec_reset(con); |
e825a66d | 2592 | prepare_write_banner(con); |
eed0ef2c | 2593 | prepare_read_banner(con); |
31b8006e | 2594 | |
cf3e5c40 | 2595 | BUG_ON(con->in_msg); |
31b8006e SW |
2596 | con->in_tag = CEPH_MSGR_TAG_READY; |
2597 | dout("try_write initiating connect on %p new state %lu\n", | |
2598 | con, con->state); | |
41617d0c AE |
2599 | ret = ceph_tcp_connect(con); |
2600 | if (ret < 0) { | |
31b8006e | 2601 | con->error_msg = "connect error"; |
31b8006e SW |
2602 | goto out; |
2603 | } | |
2604 | } | |
2605 | ||
d2935d6f ID |
2606 | more: |
2607 | dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes); | |
9c55ad1c ID |
2608 | BUG_ON(!con->sock); |
2609 | ||
31b8006e | 2610 | /* kvec data queued? */ |
67645d76 ID |
2611 | if (con->out_kvec_left) { |
2612 | ret = write_partial_kvec(con); | |
31b8006e | 2613 | if (ret <= 0) |
42961d23 | 2614 | goto out; |
31b8006e | 2615 | } |
67645d76 ID |
2616 | if (con->out_skip) { |
2617 | ret = write_partial_skip(con); | |
31b8006e | 2618 | if (ret <= 0) |
42961d23 | 2619 | goto out; |
31b8006e SW |
2620 | } |
2621 | ||
2622 | /* msg pages? */ | |
2623 | if (con->out_msg) { | |
c86a2930 SW |
2624 | if (con->out_msg_done) { |
2625 | ceph_msg_put(con->out_msg); | |
2626 | con->out_msg = NULL; /* we're done with this one */ | |
2627 | goto do_next; | |
2628 | } | |
2629 | ||
34d2d200 | 2630 | ret = write_partial_message_data(con); |
31b8006e | 2631 | if (ret == 1) |
d2935d6f | 2632 | goto more; /* we need to send the footer, too! */ |
31b8006e | 2633 | if (ret == 0) |
42961d23 | 2634 | goto out; |
31b8006e | 2635 | if (ret < 0) { |
34d2d200 | 2636 | dout("try_write write_partial_message_data err %d\n", |
31b8006e | 2637 | ret); |
42961d23 | 2638 | goto out; |
31b8006e SW |
2639 | } |
2640 | } | |
2641 | ||
c86a2930 | 2642 | do_next: |
8dacc7da | 2643 | if (con->state == CON_STATE_OPEN) { |
8b9558aa YZ |
2644 | if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) { |
2645 | prepare_write_keepalive(con); | |
2646 | goto more; | |
2647 | } | |
31b8006e SW |
2648 | /* is anything else pending? */ |
2649 | if (!list_empty(&con->out_queue)) { | |
2650 | prepare_write_message(con); | |
2651 | goto more; | |
2652 | } | |
2653 | if (con->in_seq > con->in_seq_acked) { | |
2654 | prepare_write_ack(con); | |
2655 | goto more; | |
2656 | } | |
31b8006e SW |
2657 | } |
2658 | ||
2659 | /* Nothing to do! */ | |
c9ffc77a | 2660 | con_flag_clear(con, CON_FLAG_WRITE_PENDING); |
31b8006e | 2661 | dout("try_write nothing else to write.\n"); |
31b8006e SW |
2662 | ret = 0; |
2663 | out: | |
42961d23 | 2664 | dout("try_write done on %p ret %d\n", con, ret); |
31b8006e SW |
2665 | return ret; |
2666 | } | |
2667 | ||
31b8006e SW |
2668 | /* |
2669 | * Read what we can from the socket. | |
2670 | */ | |
2671 | static int try_read(struct ceph_connection *con) | |
2672 | { | |
31b8006e SW |
2673 | int ret = -1; |
2674 | ||
8dacc7da SW |
2675 | more: |
2676 | dout("try_read start on %p state %lu\n", con, con->state); | |
2677 | if (con->state != CON_STATE_CONNECTING && | |
2678 | con->state != CON_STATE_NEGOTIATING && | |
2679 | con->state != CON_STATE_OPEN) | |
31b8006e SW |
2680 | return 0; |
2681 | ||
8dacc7da | 2682 | BUG_ON(!con->sock); |
ec302645 | 2683 | |
31b8006e SW |
2684 | dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag, |
2685 | con->in_base_pos); | |
0da5d703 | 2686 | |
8dacc7da | 2687 | if (con->state == CON_STATE_CONNECTING) { |
7593af92 AE |
2688 | dout("try_read connecting\n"); |
2689 | ret = read_partial_banner(con); | |
2690 | if (ret <= 0) | |
ab166d5a | 2691 | goto out; |
7593af92 AE |
2692 | ret = process_banner(con); |
2693 | if (ret < 0) | |
2694 | goto out; | |
2695 | ||
8dacc7da | 2696 | con->state = CON_STATE_NEGOTIATING; |
7593af92 | 2697 | |
6d4221b5 JS |
2698 | /* |
2699 | * Received banner is good, exchange connection info. | |
2700 | * Do not reset out_kvec, as sending our banner raced | |
2701 | * with receiving peer banner after connect completed. | |
2702 | */ | |
7593af92 AE |
2703 | ret = prepare_write_connect(con); |
2704 | if (ret < 0) | |
2705 | goto out; | |
2706 | prepare_read_connect(con); | |
2707 | ||
2708 | /* Send connection info before awaiting response */ | |
0da5d703 SW |
2709 | goto out; |
2710 | } | |
2711 | ||
8dacc7da | 2712 | if (con->state == CON_STATE_NEGOTIATING) { |
7593af92 | 2713 | dout("try_read negotiating\n"); |
31b8006e SW |
2714 | ret = read_partial_connect(con); |
2715 | if (ret <= 0) | |
31b8006e | 2716 | goto out; |
98bdb0aa SW |
2717 | ret = process_connect(con); |
2718 | if (ret < 0) | |
2719 | goto out; | |
31b8006e SW |
2720 | goto more; |
2721 | } | |
2722 | ||
122070a2 | 2723 | WARN_ON(con->state != CON_STATE_OPEN); |
8dacc7da | 2724 | |
31b8006e SW |
2725 | if (con->in_base_pos < 0) { |
2726 | /* | |
2727 | * skipping + discarding content. | |
31b8006e | 2728 | */ |
e5c93883 | 2729 | ret = ceph_tcp_recvmsg(con->sock, NULL, -con->in_base_pos); |
31b8006e | 2730 | if (ret <= 0) |
98bdb0aa | 2731 | goto out; |
e5c93883 | 2732 | dout("skipped %d / %d bytes\n", ret, -con->in_base_pos); |
31b8006e SW |
2733 | con->in_base_pos += ret; |
2734 | if (con->in_base_pos) | |
2735 | goto more; | |
2736 | } | |
2737 | if (con->in_tag == CEPH_MSGR_TAG_READY) { | |
2738 | /* | |
2739 | * what's next? | |
2740 | */ | |
2741 | ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1); | |
2742 | if (ret <= 0) | |
98bdb0aa | 2743 | goto out; |
31b8006e SW |
2744 | dout("try_read got tag %d\n", (int)con->in_tag); |
2745 | switch (con->in_tag) { | |
2746 | case CEPH_MSGR_TAG_MSG: | |
2747 | prepare_read_message(con); | |
2748 | break; | |
2749 | case CEPH_MSGR_TAG_ACK: | |
2750 | prepare_read_ack(con); | |
2751 | break; | |
8b9558aa YZ |
2752 | case CEPH_MSGR_TAG_KEEPALIVE2_ACK: |
2753 | prepare_read_keepalive_ack(con); | |
2754 | break; | |
31b8006e | 2755 | case CEPH_MSGR_TAG_CLOSE: |
8dacc7da SW |
2756 | con_close_socket(con); |
2757 | con->state = CON_STATE_CLOSED; | |
98bdb0aa | 2758 | goto out; |
31b8006e SW |
2759 | default: |
2760 | goto bad_tag; | |
2761 | } | |
2762 | } | |
2763 | if (con->in_tag == CEPH_MSGR_TAG_MSG) { | |
2764 | ret = read_partial_message(con); | |
2765 | if (ret <= 0) { | |
2766 | switch (ret) { | |
2767 | case -EBADMSG: | |
a51983e4 | 2768 | con->error_msg = "bad crc/signature"; |
67c64eb7 ID |
2769 | /* fall through */ |
2770 | case -EBADE: | |
31b8006e | 2771 | ret = -EIO; |
98bdb0aa | 2772 | break; |
31b8006e SW |
2773 | case -EIO: |
2774 | con->error_msg = "io error"; | |
98bdb0aa | 2775 | break; |
31b8006e | 2776 | } |
98bdb0aa | 2777 | goto out; |
31b8006e SW |
2778 | } |
2779 | if (con->in_tag == CEPH_MSGR_TAG_READY) | |
2780 | goto more; | |
2781 | process_message(con); | |
7b862e07 SW |
2782 | if (con->state == CON_STATE_OPEN) |
2783 | prepare_read_tag(con); | |
31b8006e SW |
2784 | goto more; |
2785 | } | |
3a23083b SW |
2786 | if (con->in_tag == CEPH_MSGR_TAG_ACK || |
2787 | con->in_tag == CEPH_MSGR_TAG_SEQ) { | |
2788 | /* | |
2789 | * the final handshake seq exchange is semantically | |
2790 | * equivalent to an ACK | |
2791 | */ | |
31b8006e SW |
2792 | ret = read_partial_ack(con); |
2793 | if (ret <= 0) | |
98bdb0aa | 2794 | goto out; |
31b8006e SW |
2795 | process_ack(con); |
2796 | goto more; | |
2797 | } | |
8b9558aa YZ |
2798 | if (con->in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) { |
2799 | ret = read_keepalive_ack(con); | |
2800 | if (ret <= 0) | |
2801 | goto out; | |
2802 | goto more; | |
2803 | } | |
31b8006e | 2804 | |
31b8006e | 2805 | out: |
98bdb0aa | 2806 | dout("try_read done on %p ret %d\n", con, ret); |
31b8006e SW |
2807 | return ret; |
2808 | ||
2809 | bad_tag: | |
2810 | pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag); | |
2811 | con->error_msg = "protocol error, garbage tag"; | |
2812 | ret = -1; | |
2813 | goto out; | |
2814 | } | |
2815 | ||
2816 | ||
2817 | /* | |
802c6d96 AE |
2818 | * Atomically queue work on a connection after the specified delay. |
2819 | * Bump @con reference to avoid races with connection teardown. | |
2820 | * Returns 0 if work was queued, or an error code otherwise. | |
31b8006e | 2821 | */ |
802c6d96 | 2822 | static int queue_con_delay(struct ceph_connection *con, unsigned long delay) |
31b8006e | 2823 | { |
31b8006e | 2824 | if (!con->ops->get(con)) { |
802c6d96 | 2825 | dout("%s %p ref count 0\n", __func__, con); |
802c6d96 | 2826 | return -ENOENT; |
31b8006e SW |
2827 | } |
2828 | ||
802c6d96 AE |
2829 | if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) { |
2830 | dout("%s %p - already queued\n", __func__, con); | |
31b8006e | 2831 | con->ops->put(con); |
802c6d96 | 2832 | return -EBUSY; |
31b8006e | 2833 | } |
802c6d96 AE |
2834 | |
2835 | dout("%s %p %lu\n", __func__, con, delay); | |
802c6d96 AE |
2836 | return 0; |
2837 | } | |
2838 | ||
2839 | static void queue_con(struct ceph_connection *con) | |
2840 | { | |
2841 | (void) queue_con_delay(con, 0); | |
31b8006e SW |
2842 | } |
2843 | ||
37ab77ac ID |
2844 | static void cancel_con(struct ceph_connection *con) |
2845 | { | |
2846 | if (cancel_delayed_work(&con->work)) { | |
2847 | dout("%s %p\n", __func__, con); | |
2848 | con->ops->put(con); | |
2849 | } | |
2850 | } | |
2851 | ||
7bb21d68 AE |
2852 | static bool con_sock_closed(struct ceph_connection *con) |
2853 | { | |
c9ffc77a | 2854 | if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED)) |
7bb21d68 AE |
2855 | return false; |
2856 | ||
2857 | #define CASE(x) \ | |
2858 | case CON_STATE_ ## x: \ | |
2859 | con->error_msg = "socket closed (con state " #x ")"; \ | |
2860 | break; | |
2861 | ||
2862 | switch (con->state) { | |
2863 | CASE(CLOSED); | |
2864 | CASE(PREOPEN); | |
2865 | CASE(CONNECTING); | |
2866 | CASE(NEGOTIATING); | |
2867 | CASE(OPEN); | |
2868 | CASE(STANDBY); | |
2869 | default: | |
b9a67899 | 2870 | pr_warn("%s con %p unrecognized state %lu\n", |
7bb21d68 AE |
2871 | __func__, con, con->state); |
2872 | con->error_msg = "unrecognized con state"; | |
2873 | BUG(); | |
2874 | break; | |
2875 | } | |
2876 | #undef CASE | |
2877 | ||
2878 | return true; | |
2879 | } | |
2880 | ||
f20a39fd AE |
2881 | static bool con_backoff(struct ceph_connection *con) |
2882 | { | |
2883 | int ret; | |
2884 | ||
2885 | if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF)) | |
2886 | return false; | |
2887 | ||
2888 | ret = queue_con_delay(con, round_jiffies_relative(con->delay)); | |
2889 | if (ret) { | |
2890 | dout("%s: con %p FAILED to back off %lu\n", __func__, | |
2891 | con, con->delay); | |
2892 | BUG_ON(ret == -ENOENT); | |
2893 | con_flag_set(con, CON_FLAG_BACKOFF); | |
2894 | } | |
2895 | ||
2896 | return true; | |
2897 | } | |
2898 | ||
93209264 AE |
2899 | /* Finish fault handling; con->mutex must *not* be held here */ |
2900 | ||
2901 | static void con_fault_finish(struct ceph_connection *con) | |
2902 | { | |
f6330cc1 ID |
2903 | dout("%s %p\n", __func__, con); |
2904 | ||
93209264 AE |
2905 | /* |
2906 | * in case we faulted due to authentication, invalidate our | |
2907 | * current tickets so that we can get new ones. | |
2908 | */ | |
f6330cc1 ID |
2909 | if (con->auth_retry) { |
2910 | dout("auth_retry %d, invalidating\n", con->auth_retry); | |
2911 | if (con->ops->invalidate_authorizer) | |
2912 | con->ops->invalidate_authorizer(con); | |
2913 | con->auth_retry = 0; | |
93209264 AE |
2914 | } |
2915 | ||
2916 | if (con->ops->fault) | |
2917 | con->ops->fault(con); | |
2918 | } | |
2919 | ||
31b8006e SW |
2920 | /* |
2921 | * Do some work on a connection. Drop a connection ref when we're done. | |
2922 | */ | |
68931622 | 2923 | static void ceph_con_workfn(struct work_struct *work) |
31b8006e SW |
2924 | { |
2925 | struct ceph_connection *con = container_of(work, struct ceph_connection, | |
2926 | work.work); | |
49659416 | 2927 | bool fault; |
31b8006e | 2928 | |
9dd4658d | 2929 | mutex_lock(&con->mutex); |
49659416 AE |
2930 | while (true) { |
2931 | int ret; | |
31b8006e | 2932 | |
49659416 AE |
2933 | if ((fault = con_sock_closed(con))) { |
2934 | dout("%s: con %p SOCK_CLOSED\n", __func__, con); | |
2935 | break; | |
2936 | } | |
2937 | if (con_backoff(con)) { | |
2938 | dout("%s: con %p BACKOFF\n", __func__, con); | |
2939 | break; | |
2940 | } | |
2941 | if (con->state == CON_STATE_STANDBY) { | |
2942 | dout("%s: con %p STANDBY\n", __func__, con); | |
2943 | break; | |
2944 | } | |
2945 | if (con->state == CON_STATE_CLOSED) { | |
2946 | dout("%s: con %p CLOSED\n", __func__, con); | |
2947 | BUG_ON(con->sock); | |
2948 | break; | |
2949 | } | |
2950 | if (con->state == CON_STATE_PREOPEN) { | |
2951 | dout("%s: con %p PREOPEN\n", __func__, con); | |
2952 | BUG_ON(con->sock); | |
2953 | } | |
0da5d703 | 2954 | |
49659416 AE |
2955 | ret = try_read(con); |
2956 | if (ret < 0) { | |
2957 | if (ret == -EAGAIN) | |
2958 | continue; | |
67c64eb7 ID |
2959 | if (!con->error_msg) |
2960 | con->error_msg = "socket error on read"; | |
49659416 AE |
2961 | fault = true; |
2962 | break; | |
2963 | } | |
2964 | ||
2965 | ret = try_write(con); | |
2966 | if (ret < 0) { | |
2967 | if (ret == -EAGAIN) | |
2968 | continue; | |
67c64eb7 ID |
2969 | if (!con->error_msg) |
2970 | con->error_msg = "socket error on write"; | |
49659416 AE |
2971 | fault = true; |
2972 | } | |
2973 | ||
2974 | break; /* If we make it to here, we're done */ | |
3a140a0d | 2975 | } |
b6e7b6a1 AE |
2976 | if (fault) |
2977 | con_fault(con); | |
9dd4658d | 2978 | mutex_unlock(&con->mutex); |
0da5d703 | 2979 | |
b6e7b6a1 AE |
2980 | if (fault) |
2981 | con_fault_finish(con); | |
2982 | ||
2983 | con->ops->put(con); | |
31b8006e SW |
2984 | } |
2985 | ||
31b8006e SW |
2986 | /* |
2987 | * Generic error/fault handler. A retry mechanism is used with | |
2988 | * exponential backoff | |
2989 | */ | |
93209264 | 2990 | static void con_fault(struct ceph_connection *con) |
31b8006e | 2991 | { |
31b8006e | 2992 | dout("fault %p state %lu to peer %s\n", |
3d14c5d2 | 2993 | con, con->state, ceph_pr_addr(&con->peer_addr.in_addr)); |
31b8006e | 2994 | |
67c64eb7 ID |
2995 | pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name), |
2996 | ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg); | |
2997 | con->error_msg = NULL; | |
2998 | ||
122070a2 | 2999 | WARN_ON(con->state != CON_STATE_CONNECTING && |
8dacc7da SW |
3000 | con->state != CON_STATE_NEGOTIATING && |
3001 | con->state != CON_STATE_OPEN); | |
ec302645 | 3002 | |
31b8006e | 3003 | con_close_socket(con); |
5e095e8b | 3004 | |
c9ffc77a | 3005 | if (con_flag_test(con, CON_FLAG_LOSSYTX)) { |
8dacc7da SW |
3006 | dout("fault on LOSSYTX channel, marking CLOSED\n"); |
3007 | con->state = CON_STATE_CLOSED; | |
93209264 | 3008 | return; |
3b5ede07 SW |
3009 | } |
3010 | ||
5e095e8b | 3011 | if (con->in_msg) { |
38941f80 | 3012 | BUG_ON(con->in_msg->con != con); |
5e095e8b SW |
3013 | ceph_msg_put(con->in_msg); |
3014 | con->in_msg = NULL; | |
3015 | } | |
31b8006e | 3016 | |
e80a52d1 SW |
3017 | /* Requeue anything that hasn't been acked */ |
3018 | list_splice_init(&con->out_sent, &con->out_queue); | |
9bd2e6f8 | 3019 | |
e76661d0 SW |
3020 | /* If there are no messages queued or keepalive pending, place |
3021 | * the connection in a STANDBY state */ | |
3022 | if (list_empty(&con->out_queue) && | |
c9ffc77a | 3023 | !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) { |
e00de341 | 3024 | dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con); |
c9ffc77a | 3025 | con_flag_clear(con, CON_FLAG_WRITE_PENDING); |
8dacc7da | 3026 | con->state = CON_STATE_STANDBY; |
e80a52d1 SW |
3027 | } else { |
3028 | /* retry after a delay. */ | |
8dacc7da | 3029 | con->state = CON_STATE_PREOPEN; |
e80a52d1 SW |
3030 | if (con->delay == 0) |
3031 | con->delay = BASE_DELAY_INTERVAL; | |
3032 | else if (con->delay < MAX_DELAY_INTERVAL) | |
3033 | con->delay *= 2; | |
c9ffc77a | 3034 | con_flag_set(con, CON_FLAG_BACKOFF); |
8618e30b | 3035 | queue_con(con); |
31b8006e | 3036 | } |
31b8006e SW |
3037 | } |
3038 | ||
3039 | ||
3040 | ||
3041 | /* | |
15d9882c | 3042 | * initialize a new messenger instance |
31b8006e | 3043 | */ |
15d9882c | 3044 | void ceph_messenger_init(struct ceph_messenger *msgr, |
859bff51 | 3045 | struct ceph_entity_addr *myaddr) |
31b8006e | 3046 | { |
31b8006e SW |
3047 | spin_lock_init(&msgr->global_seq_lock); |
3048 | ||
31b8006e SW |
3049 | if (myaddr) |
3050 | msgr->inst.addr = *myaddr; | |
3051 | ||
3052 | /* select a random nonce */ | |
ac8839d7 | 3053 | msgr->inst.addr.type = 0; |
103e2d3a | 3054 | get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce)); |
63f2d211 | 3055 | encode_my_addr(msgr); |
31b8006e | 3056 | |
a2a32584 | 3057 | atomic_set(&msgr->stopping, 0); |
757856d2 | 3058 | write_pnet(&msgr->net, get_net(current->nsproxy->net_ns)); |
31b8006e | 3059 | |
15d9882c | 3060 | dout("%s %p\n", __func__, msgr); |
31b8006e | 3061 | } |
15d9882c | 3062 | EXPORT_SYMBOL(ceph_messenger_init); |
31b8006e | 3063 | |
757856d2 ID |
3064 | void ceph_messenger_fini(struct ceph_messenger *msgr) |
3065 | { | |
3066 | put_net(read_pnet(&msgr->net)); | |
3067 | } | |
3068 | EXPORT_SYMBOL(ceph_messenger_fini); | |
3069 | ||
583d0fef ID |
3070 | static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con) |
3071 | { | |
3072 | if (msg->con) | |
3073 | msg->con->ops->put(msg->con); | |
3074 | ||
3075 | msg->con = con ? con->ops->get(con) : NULL; | |
3076 | BUG_ON(msg->con != con); | |
3077 | } | |
3078 | ||
e00de341 SW |
3079 | static void clear_standby(struct ceph_connection *con) |
3080 | { | |
3081 | /* come back from STANDBY? */ | |
8dacc7da | 3082 | if (con->state == CON_STATE_STANDBY) { |
e00de341 | 3083 | dout("clear_standby %p and ++connect_seq\n", con); |
8dacc7da | 3084 | con->state = CON_STATE_PREOPEN; |
e00de341 | 3085 | con->connect_seq++; |
c9ffc77a AE |
3086 | WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING)); |
3087 | WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)); | |
e00de341 SW |
3088 | } |
3089 | } | |
3090 | ||
31b8006e SW |
3091 | /* |
3092 | * Queue up an outgoing message on the given connection. | |
3093 | */ | |
3094 | void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg) | |
3095 | { | |
31b8006e | 3096 | /* set src+dst */ |
dbad185d | 3097 | msg->hdr.src = con->msgr->inst.name; |
3ca02ef9 | 3098 | BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len)); |
e84346b7 SW |
3099 | msg->needs_out_seq = true; |
3100 | ||
ec302645 | 3101 | mutex_lock(&con->mutex); |
92ce034b | 3102 | |
8dacc7da | 3103 | if (con->state == CON_STATE_CLOSED) { |
a59b55a6 SW |
3104 | dout("con_send %p closed, dropping %p\n", con, msg); |
3105 | ceph_msg_put(msg); | |
3106 | mutex_unlock(&con->mutex); | |
3107 | return; | |
3108 | } | |
3109 | ||
583d0fef | 3110 | msg_con_set(msg, con); |
92ce034b | 3111 | |
31b8006e SW |
3112 | BUG_ON(!list_empty(&msg->list_head)); |
3113 | list_add_tail(&msg->list_head, &con->out_queue); | |
3114 | dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg, | |
3115 | ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type), | |
3116 | ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), | |
3117 | le32_to_cpu(msg->hdr.front_len), | |
3118 | le32_to_cpu(msg->hdr.middle_len), | |
3119 | le32_to_cpu(msg->hdr.data_len)); | |
00650931 SW |
3120 | |
3121 | clear_standby(con); | |
ec302645 | 3122 | mutex_unlock(&con->mutex); |
31b8006e SW |
3123 | |
3124 | /* if there wasn't anything waiting to send before, queue | |
3125 | * new work */ | |
c9ffc77a | 3126 | if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0) |
31b8006e SW |
3127 | queue_con(con); |
3128 | } | |
3d14c5d2 | 3129 | EXPORT_SYMBOL(ceph_con_send); |
31b8006e SW |
3130 | |
3131 | /* | |
3132 | * Revoke a message that was previously queued for send | |
3133 | */ | |
6740a845 | 3134 | void ceph_msg_revoke(struct ceph_msg *msg) |
31b8006e | 3135 | { |
6740a845 AE |
3136 | struct ceph_connection *con = msg->con; |
3137 | ||
583d0fef ID |
3138 | if (!con) { |
3139 | dout("%s msg %p null con\n", __func__, msg); | |
6740a845 | 3140 | return; /* Message not in our possession */ |
583d0fef | 3141 | } |
6740a845 | 3142 | |
ec302645 | 3143 | mutex_lock(&con->mutex); |
31b8006e | 3144 | if (!list_empty(&msg->list_head)) { |
38941f80 | 3145 | dout("%s %p msg %p - was on queue\n", __func__, con, msg); |
31b8006e | 3146 | list_del_init(&msg->list_head); |
31b8006e | 3147 | msg->hdr.seq = 0; |
38941f80 | 3148 | |
31b8006e | 3149 | ceph_msg_put(msg); |
ed98adad SW |
3150 | } |
3151 | if (con->out_msg == msg) { | |
67645d76 ID |
3152 | BUG_ON(con->out_skip); |
3153 | /* footer */ | |
3154 | if (con->out_msg_done) { | |
3155 | con->out_skip += con_out_kvec_skip(con); | |
3156 | } else { | |
3157 | BUG_ON(!msg->data_length); | |
89f08173 | 3158 | con->out_skip += sizeof_footer(con); |
31b8006e | 3159 | } |
67645d76 ID |
3160 | /* data, middle, front */ |
3161 | if (msg->data_length) | |
3162 | con->out_skip += msg->cursor.total_resid; | |
3163 | if (msg->middle) | |
3164 | con->out_skip += con_out_kvec_skip(con); | |
3165 | con->out_skip += con_out_kvec_skip(con); | |
3166 | ||
3167 | dout("%s %p msg %p - was sending, will write %d skip %d\n", | |
3168 | __func__, con, msg, con->out_kvec_bytes, con->out_skip); | |
ed98adad | 3169 | msg->hdr.seq = 0; |
67645d76 | 3170 | con->out_msg = NULL; |
92ce034b | 3171 | ceph_msg_put(msg); |
31b8006e | 3172 | } |
67645d76 | 3173 | |
ec302645 | 3174 | mutex_unlock(&con->mutex); |
31b8006e SW |
3175 | } |
3176 | ||
350b1c32 | 3177 | /* |
0d59ab81 | 3178 | * Revoke a message that we may be reading data into |
350b1c32 | 3179 | */ |
8921d114 | 3180 | void ceph_msg_revoke_incoming(struct ceph_msg *msg) |
350b1c32 | 3181 | { |
583d0fef | 3182 | struct ceph_connection *con = msg->con; |
8921d114 | 3183 | |
583d0fef | 3184 | if (!con) { |
8921d114 | 3185 | dout("%s msg %p null con\n", __func__, msg); |
8921d114 AE |
3186 | return; /* Message not in our possession */ |
3187 | } | |
3188 | ||
350b1c32 | 3189 | mutex_lock(&con->mutex); |
8921d114 | 3190 | if (con->in_msg == msg) { |
95c96174 ED |
3191 | unsigned int front_len = le32_to_cpu(con->in_hdr.front_len); |
3192 | unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len); | |
3193 | unsigned int data_len = le32_to_cpu(con->in_hdr.data_len); | |
350b1c32 SW |
3194 | |
3195 | /* skip rest of message */ | |
8921d114 AE |
3196 | dout("%s %p msg %p revoked\n", __func__, con, msg); |
3197 | con->in_base_pos = con->in_base_pos - | |
350b1c32 | 3198 | sizeof(struct ceph_msg_header) - |
0d59ab81 YS |
3199 | front_len - |
3200 | middle_len - | |
3201 | data_len - | |
350b1c32 | 3202 | sizeof(struct ceph_msg_footer); |
350b1c32 SW |
3203 | ceph_msg_put(con->in_msg); |
3204 | con->in_msg = NULL; | |
3205 | con->in_tag = CEPH_MSGR_TAG_READY; | |
684be25c | 3206 | con->in_seq++; |
350b1c32 | 3207 | } else { |
8921d114 AE |
3208 | dout("%s %p in_msg %p msg %p no-op\n", |
3209 | __func__, con, con->in_msg, msg); | |
350b1c32 SW |
3210 | } |
3211 | mutex_unlock(&con->mutex); | |
3212 | } | |
3213 | ||
31b8006e SW |
3214 | /* |
3215 | * Queue a keepalive byte to ensure the tcp connection is alive. | |
3216 | */ | |
3217 | void ceph_con_keepalive(struct ceph_connection *con) | |
3218 | { | |
e00de341 | 3219 | dout("con_keepalive %p\n", con); |
00650931 | 3220 | mutex_lock(&con->mutex); |
e00de341 | 3221 | clear_standby(con); |
00650931 | 3222 | mutex_unlock(&con->mutex); |
c9ffc77a AE |
3223 | if (con_flag_test_and_set(con, CON_FLAG_KEEPALIVE_PENDING) == 0 && |
3224 | con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0) | |
31b8006e SW |
3225 | queue_con(con); |
3226 | } | |
3d14c5d2 | 3227 | EXPORT_SYMBOL(ceph_con_keepalive); |
31b8006e | 3228 | |
8b9558aa YZ |
3229 | bool ceph_con_keepalive_expired(struct ceph_connection *con, |
3230 | unsigned long interval) | |
3231 | { | |
3232 | if (interval > 0 && | |
3233 | (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) { | |
473bd2d7 AB |
3234 | struct timespec64 now; |
3235 | struct timespec64 ts; | |
3236 | ktime_get_real_ts64(&now); | |
3237 | jiffies_to_timespec64(interval, &ts); | |
3238 | ts = timespec64_add(con->last_keepalive_ack, ts); | |
3239 | return timespec64_compare(&now, &ts) >= 0; | |
8b9558aa YZ |
3240 | } |
3241 | return false; | |
3242 | } | |
3243 | ||
0d9c1ab3 | 3244 | static struct ceph_msg_data *ceph_msg_data_add(struct ceph_msg *msg) |
43794509 | 3245 | { |
0d9c1ab3 ID |
3246 | BUG_ON(msg->num_data_items >= msg->max_data_items); |
3247 | return &msg->data[msg->num_data_items++]; | |
6644ed7b AE |
3248 | } |
3249 | ||
3250 | static void ceph_msg_data_destroy(struct ceph_msg_data *data) | |
3251 | { | |
e4339d28 | 3252 | if (data->type == CEPH_MSG_DATA_PAGELIST) |
6644ed7b | 3253 | ceph_pagelist_release(data->pagelist); |
43794509 AE |
3254 | } |
3255 | ||
90af3602 | 3256 | void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages, |
f1baeb2b | 3257 | size_t length, size_t alignment) |
02afca6c | 3258 | { |
6644ed7b AE |
3259 | struct ceph_msg_data *data; |
3260 | ||
07aa1558 AE |
3261 | BUG_ON(!pages); |
3262 | BUG_ON(!length); | |
6644ed7b | 3263 | |
0d9c1ab3 ID |
3264 | data = ceph_msg_data_add(msg); |
3265 | data->type = CEPH_MSG_DATA_PAGES; | |
6644ed7b AE |
3266 | data->pages = pages; |
3267 | data->length = length; | |
3268 | data->alignment = alignment & ~PAGE_MASK; | |
02afca6c | 3269 | |
5240d9f9 | 3270 | msg->data_length += length; |
02afca6c | 3271 | } |
90af3602 | 3272 | EXPORT_SYMBOL(ceph_msg_data_add_pages); |
31b8006e | 3273 | |
90af3602 | 3274 | void ceph_msg_data_add_pagelist(struct ceph_msg *msg, |
27fa8385 AE |
3275 | struct ceph_pagelist *pagelist) |
3276 | { | |
6644ed7b AE |
3277 | struct ceph_msg_data *data; |
3278 | ||
07aa1558 AE |
3279 | BUG_ON(!pagelist); |
3280 | BUG_ON(!pagelist->length); | |
27fa8385 | 3281 | |
0d9c1ab3 ID |
3282 | data = ceph_msg_data_add(msg); |
3283 | data->type = CEPH_MSG_DATA_PAGELIST; | |
89486833 | 3284 | refcount_inc(&pagelist->refcnt); |
6644ed7b AE |
3285 | data->pagelist = pagelist; |
3286 | ||
5240d9f9 | 3287 | msg->data_length += pagelist->length; |
27fa8385 | 3288 | } |
90af3602 | 3289 | EXPORT_SYMBOL(ceph_msg_data_add_pagelist); |
27fa8385 | 3290 | |
ea96571f | 3291 | #ifdef CONFIG_BLOCK |
5359a17d ID |
3292 | void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos, |
3293 | u32 length) | |
27fa8385 | 3294 | { |
6644ed7b AE |
3295 | struct ceph_msg_data *data; |
3296 | ||
0d9c1ab3 ID |
3297 | data = ceph_msg_data_add(msg); |
3298 | data->type = CEPH_MSG_DATA_BIO; | |
5359a17d | 3299 | data->bio_pos = *bio_pos; |
c851c495 | 3300 | data->bio_length = length; |
6644ed7b | 3301 | |
5240d9f9 | 3302 | msg->data_length += length; |
27fa8385 | 3303 | } |
90af3602 | 3304 | EXPORT_SYMBOL(ceph_msg_data_add_bio); |
ea96571f | 3305 | #endif /* CONFIG_BLOCK */ |
27fa8385 | 3306 | |
b9e281c2 ID |
3307 | void ceph_msg_data_add_bvecs(struct ceph_msg *msg, |
3308 | struct ceph_bvec_iter *bvec_pos) | |
3309 | { | |
3310 | struct ceph_msg_data *data; | |
3311 | ||
0d9c1ab3 ID |
3312 | data = ceph_msg_data_add(msg); |
3313 | data->type = CEPH_MSG_DATA_BVECS; | |
b9e281c2 ID |
3314 | data->bvec_pos = *bvec_pos; |
3315 | ||
b9e281c2 ID |
3316 | msg->data_length += bvec_pos->iter.bi_size; |
3317 | } | |
3318 | EXPORT_SYMBOL(ceph_msg_data_add_bvecs); | |
3319 | ||
31b8006e SW |
3320 | /* |
3321 | * construct a new message with given type, size | |
3322 | * the new msg has a ref count of 1. | |
3323 | */ | |
0d9c1ab3 ID |
3324 | struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items, |
3325 | gfp_t flags, bool can_fail) | |
31b8006e SW |
3326 | { |
3327 | struct ceph_msg *m; | |
3328 | ||
e3d5d638 | 3329 | m = kmem_cache_zalloc(ceph_msg_cache, flags); |
31b8006e SW |
3330 | if (m == NULL) |
3331 | goto out; | |
31b8006e SW |
3332 | |
3333 | m->hdr.type = cpu_to_le16(type); | |
45c6ceb5 | 3334 | m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT); |
31b8006e | 3335 | m->hdr.front_len = cpu_to_le32(front_len); |
ca20892d | 3336 | |
9516e45b AE |
3337 | INIT_LIST_HEAD(&m->list_head); |
3338 | kref_init(&m->kref); | |
ca20892d | 3339 | |
31b8006e SW |
3340 | /* front */ |
3341 | if (front_len) { | |
eeb0bed5 | 3342 | m->front.iov_base = ceph_kvmalloc(front_len, flags); |
31b8006e | 3343 | if (m->front.iov_base == NULL) { |
b61c2763 | 3344 | dout("ceph_msg_new can't allocate %d bytes\n", |
31b8006e SW |
3345 | front_len); |
3346 | goto out2; | |
3347 | } | |
3348 | } else { | |
3349 | m->front.iov_base = NULL; | |
3350 | } | |
f2be82b0 | 3351 | m->front_alloc_len = m->front.iov_len = front_len; |
31b8006e | 3352 | |
0d9c1ab3 ID |
3353 | if (max_data_items) { |
3354 | m->data = kmalloc_array(max_data_items, sizeof(*m->data), | |
3355 | flags); | |
3356 | if (!m->data) | |
3357 | goto out2; | |
3358 | ||
3359 | m->max_data_items = max_data_items; | |
3360 | } | |
3361 | ||
bb257664 | 3362 | dout("ceph_msg_new %p front %d\n", m, front_len); |
31b8006e SW |
3363 | return m; |
3364 | ||
3365 | out2: | |
3366 | ceph_msg_put(m); | |
3367 | out: | |
b61c2763 SW |
3368 | if (!can_fail) { |
3369 | pr_err("msg_new can't create type %d front %d\n", type, | |
3370 | front_len); | |
f0ed1b7c | 3371 | WARN_ON(1); |
b61c2763 SW |
3372 | } else { |
3373 | dout("msg_new can't create type %d front %d\n", type, | |
3374 | front_len); | |
3375 | } | |
a79832f2 | 3376 | return NULL; |
31b8006e | 3377 | } |
0d9c1ab3 ID |
3378 | EXPORT_SYMBOL(ceph_msg_new2); |
3379 | ||
3380 | struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags, | |
3381 | bool can_fail) | |
3382 | { | |
3383 | return ceph_msg_new2(type, front_len, 0, flags, can_fail); | |
3384 | } | |
3d14c5d2 | 3385 | EXPORT_SYMBOL(ceph_msg_new); |
31b8006e | 3386 | |
31b8006e SW |
3387 | /* |
3388 | * Allocate "middle" portion of a message, if it is needed and wasn't | |
3389 | * allocated by alloc_msg. This allows us to read a small fixed-size | |
3390 | * per-type header in the front and then gracefully fail (i.e., | |
3391 | * propagate the error to the caller based on info in the front) when | |
3392 | * the middle is too large. | |
3393 | */ | |
2450418c | 3394 | static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg) |
31b8006e SW |
3395 | { |
3396 | int type = le16_to_cpu(msg->hdr.type); | |
3397 | int middle_len = le32_to_cpu(msg->hdr.middle_len); | |
3398 | ||
3399 | dout("alloc_middle %p type %d %s middle_len %d\n", msg, type, | |
3400 | ceph_msg_type_name(type), middle_len); | |
3401 | BUG_ON(!middle_len); | |
3402 | BUG_ON(msg->middle); | |
3403 | ||
b6c1d5b8 | 3404 | msg->middle = ceph_buffer_new(middle_len, GFP_NOFS); |
31b8006e SW |
3405 | if (!msg->middle) |
3406 | return -ENOMEM; | |
3407 | return 0; | |
3408 | } | |
3409 | ||
2450418c | 3410 | /* |
1c20f2d2 AE |
3411 | * Allocate a message for receiving an incoming message on a |
3412 | * connection, and save the result in con->in_msg. Uses the | |
3413 | * connection's private alloc_msg op if available. | |
3414 | * | |
4740a623 SW |
3415 | * Returns 0 on success, or a negative error code. |
3416 | * | |
3417 | * On success, if we set *skip = 1: | |
3418 | * - the next message should be skipped and ignored. | |
3419 | * - con->in_msg == NULL | |
3420 | * or if we set *skip = 0: | |
3421 | * - con->in_msg is non-null. | |
3422 | * On error (ENOMEM, EAGAIN, ...), | |
3423 | * - con->in_msg == NULL | |
2450418c | 3424 | */ |
4740a623 | 3425 | static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip) |
2450418c | 3426 | { |
4740a623 | 3427 | struct ceph_msg_header *hdr = &con->in_hdr; |
2450418c | 3428 | int middle_len = le32_to_cpu(hdr->middle_len); |
1d866d1c | 3429 | struct ceph_msg *msg; |
4740a623 | 3430 | int ret = 0; |
2450418c | 3431 | |
1c20f2d2 | 3432 | BUG_ON(con->in_msg != NULL); |
53ded495 | 3433 | BUG_ON(!con->ops->alloc_msg); |
2450418c | 3434 | |
53ded495 AE |
3435 | mutex_unlock(&con->mutex); |
3436 | msg = con->ops->alloc_msg(con, hdr, skip); | |
3437 | mutex_lock(&con->mutex); | |
3438 | if (con->state != CON_STATE_OPEN) { | |
3439 | if (msg) | |
1d866d1c | 3440 | ceph_msg_put(msg); |
53ded495 AE |
3441 | return -EAGAIN; |
3442 | } | |
4137577a AE |
3443 | if (msg) { |
3444 | BUG_ON(*skip); | |
583d0fef | 3445 | msg_con_set(msg, con); |
4137577a | 3446 | con->in_msg = msg; |
4137577a AE |
3447 | } else { |
3448 | /* | |
3449 | * Null message pointer means either we should skip | |
3450 | * this message or we couldn't allocate memory. The | |
3451 | * former is not an error. | |
3452 | */ | |
3453 | if (*skip) | |
3454 | return 0; | |
4137577a | 3455 | |
67c64eb7 | 3456 | con->error_msg = "error allocating memory for incoming message"; |
53ded495 | 3457 | return -ENOMEM; |
2450418c | 3458 | } |
1c20f2d2 | 3459 | memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr)); |
2450418c | 3460 | |
1c20f2d2 AE |
3461 | if (middle_len && !con->in_msg->middle) { |
3462 | ret = ceph_alloc_middle(con, con->in_msg); | |
2450418c | 3463 | if (ret < 0) { |
1c20f2d2 AE |
3464 | ceph_msg_put(con->in_msg); |
3465 | con->in_msg = NULL; | |
2450418c YS |
3466 | } |
3467 | } | |
9d7f0f13 | 3468 | |
4740a623 | 3469 | return ret; |
2450418c YS |
3470 | } |
3471 | ||
31b8006e SW |
3472 | |
3473 | /* | |
3474 | * Free a generically kmalloc'd message. | |
3475 | */ | |
0215e44b | 3476 | static void ceph_msg_free(struct ceph_msg *m) |
31b8006e | 3477 | { |
0215e44b | 3478 | dout("%s %p\n", __func__, m); |
4965fc38 | 3479 | kvfree(m->front.iov_base); |
0d9c1ab3 | 3480 | kfree(m->data); |
e3d5d638 | 3481 | kmem_cache_free(ceph_msg_cache, m); |
31b8006e SW |
3482 | } |
3483 | ||
0215e44b | 3484 | static void ceph_msg_release(struct kref *kref) |
c2e552e7 SW |
3485 | { |
3486 | struct ceph_msg *m = container_of(kref, struct ceph_msg, kref); | |
0d9c1ab3 | 3487 | int i; |
31b8006e | 3488 | |
0215e44b | 3489 | dout("%s %p\n", __func__, m); |
c2e552e7 SW |
3490 | WARN_ON(!list_empty(&m->list_head)); |
3491 | ||
583d0fef ID |
3492 | msg_con_set(m, NULL); |
3493 | ||
c2e552e7 SW |
3494 | /* drop middle, data, if any */ |
3495 | if (m->middle) { | |
3496 | ceph_buffer_put(m->middle); | |
3497 | m->middle = NULL; | |
31b8006e | 3498 | } |
5240d9f9 | 3499 | |
0d9c1ab3 ID |
3500 | for (i = 0; i < m->num_data_items; i++) |
3501 | ceph_msg_data_destroy(&m->data[i]); | |
58bb3b37 | 3502 | |
c2e552e7 SW |
3503 | if (m->pool) |
3504 | ceph_msgpool_put(m->pool, m); | |
3505 | else | |
0215e44b ID |
3506 | ceph_msg_free(m); |
3507 | } | |
3508 | ||
3509 | struct ceph_msg *ceph_msg_get(struct ceph_msg *msg) | |
3510 | { | |
3511 | dout("%s %p (was %d)\n", __func__, msg, | |
2c935bc5 | 3512 | kref_read(&msg->kref)); |
0215e44b ID |
3513 | kref_get(&msg->kref); |
3514 | return msg; | |
3515 | } | |
3516 | EXPORT_SYMBOL(ceph_msg_get); | |
3517 | ||
3518 | void ceph_msg_put(struct ceph_msg *msg) | |
3519 | { | |
3520 | dout("%s %p (was %d)\n", __func__, msg, | |
2c935bc5 | 3521 | kref_read(&msg->kref)); |
0215e44b | 3522 | kref_put(&msg->kref, ceph_msg_release); |
31b8006e | 3523 | } |
0215e44b | 3524 | EXPORT_SYMBOL(ceph_msg_put); |
9ec7cab1 SW |
3525 | |
3526 | void ceph_msg_dump(struct ceph_msg *msg) | |
3527 | { | |
3cea4c30 ID |
3528 | pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg, |
3529 | msg->front_alloc_len, msg->data_length); | |
9ec7cab1 SW |
3530 | print_hex_dump(KERN_DEBUG, "header: ", |
3531 | DUMP_PREFIX_OFFSET, 16, 1, | |
3532 | &msg->hdr, sizeof(msg->hdr), true); | |
3533 | print_hex_dump(KERN_DEBUG, " front: ", | |
3534 | DUMP_PREFIX_OFFSET, 16, 1, | |
3535 | msg->front.iov_base, msg->front.iov_len, true); | |
3536 | if (msg->middle) | |
3537 | print_hex_dump(KERN_DEBUG, "middle: ", | |
3538 | DUMP_PREFIX_OFFSET, 16, 1, | |
3539 | msg->middle->vec.iov_base, | |
3540 | msg->middle->vec.iov_len, true); | |
3541 | print_hex_dump(KERN_DEBUG, "footer: ", | |
3542 | DUMP_PREFIX_OFFSET, 16, 1, | |
3543 | &msg->footer, sizeof(msg->footer), true); | |
3544 | } | |
3d14c5d2 | 3545 | EXPORT_SYMBOL(ceph_msg_dump); |