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