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