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