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