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