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