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