1 #include <linux/ceph/ceph_debug.h>
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>
9 #include <linux/nsproxy.h>
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/socket.h>
13 #include <linux/string.h>
14 #ifdef CONFIG_BLOCK
15 #include <linux/bio.h>
16 #endif /* CONFIG_BLOCK */
17 #include <linux/dns_resolver.h>
18 #include <net/tcp.h>
19
20 #include <linux/ceph/ceph_features.h>
21 #include <linux/ceph/libceph.h>
22 #include <linux/ceph/messenger.h>
23 #include <linux/ceph/decode.h>
24 #include <linux/ceph/pagelist.h>
25 #include <linux/export.h>
26
27 #define list_entry_next(pos, member) \
28 list_entry(pos->member.next, typeof(*pos), member)
29
30 /*
31 * Ceph uses the messenger to exchange ceph_msg messages with other
32 * hosts in the system. The messenger provides ordered and reliable
33 * delivery. We tolerate TCP disconnects by reconnecting (with
34 * exponential backoff) in the case of a fault (disconnection, bad
35 * crc, protocol error). Acks allow sent messages to be discarded by
36 * the sender.
37 */
38
39 /*
40 * We track the state of the socket on a given connection using
41 * values defined below. The transition to a new socket state is
42 * handled by a function which verifies we aren't coming from an
43 * unexpected state.
44 *
45 * --------
46 * | NEW* | transient initial state
47 * --------
48 * | con_sock_state_init()
49 * v
50 * ----------
51 * | CLOSED | initialized, but no socket (and no
52 * ---------- TCP connection)
53 * ^ \
54 * | \ con_sock_state_connecting()
55 * | ----------------------
56 * | \
57 * + con_sock_state_closed() \
58 * |+--------------------------- \
59 * | \ \ \
60 * | ----------- \ \
61 * | | CLOSING | socket event; \ \
62 * | ----------- await close \ \
63 * | ^ \ |
64 * | | \ |
65 * | + con_sock_state_closing() \ |
66 * | / \ | |
67 * | / --------------- | |
68 * | / \ v v
69 * | / --------------
70 * | / -----------------| CONNECTING | socket created, TCP
71 * | | / -------------- connect initiated
72 * | | | con_sock_state_connected()
73 * | | v
74 * -------------
75 * | CONNECTED | TCP connection established
76 * -------------
77 *
78 * State values for ceph_connection->sock_state; NEW is assumed to be 0.
79 */
80
81 #define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
82 #define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
83 #define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
84 #define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
85 #define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
86
87 /*
88 * connection states
89 */
90 #define CON_STATE_CLOSED 1 /* -> PREOPEN */
91 #define CON_STATE_PREOPEN 2 /* -> CONNECTING, CLOSED */
92 #define CON_STATE_CONNECTING 3 /* -> NEGOTIATING, CLOSED */
93 #define CON_STATE_NEGOTIATING 4 /* -> OPEN, CLOSED */
94 #define CON_STATE_OPEN 5 /* -> STANDBY, CLOSED */
95 #define CON_STATE_STANDBY 6 /* -> PREOPEN, CLOSED */
96
97 /*
98 * ceph_connection flag bits
99 */
100 #define CON_FLAG_LOSSYTX 0 /* we can close channel or drop
101 * messages on errors */
102 #define CON_FLAG_KEEPALIVE_PENDING 1 /* we need to send a keepalive */
103 #define CON_FLAG_WRITE_PENDING 2 /* we have data ready to send */
104 #define CON_FLAG_SOCK_CLOSED 3 /* socket state changed to closed */
105 #define CON_FLAG_BACKOFF 4 /* need to retry queuing delayed work */
106
con_flag_valid(unsigned long con_flag)107 static bool con_flag_valid(unsigned long con_flag)
108 {
109 switch (con_flag) {
110 case CON_FLAG_LOSSYTX:
111 case CON_FLAG_KEEPALIVE_PENDING:
112 case CON_FLAG_WRITE_PENDING:
113 case CON_FLAG_SOCK_CLOSED:
114 case CON_FLAG_BACKOFF:
115 return true;
116 default:
117 return false;
118 }
119 }
120
con_flag_clear(struct ceph_connection * con,unsigned long con_flag)121 static void con_flag_clear(struct ceph_connection *con, unsigned long con_flag)
122 {
123 BUG_ON(!con_flag_valid(con_flag));
124
125 clear_bit(con_flag, &con->flags);
126 }
127
con_flag_set(struct ceph_connection * con,unsigned long con_flag)128 static void con_flag_set(struct ceph_connection *con, unsigned long con_flag)
129 {
130 BUG_ON(!con_flag_valid(con_flag));
131
132 set_bit(con_flag, &con->flags);
133 }
134
con_flag_test(struct ceph_connection * con,unsigned long con_flag)135 static bool con_flag_test(struct ceph_connection *con, unsigned long con_flag)
136 {
137 BUG_ON(!con_flag_valid(con_flag));
138
139 return test_bit(con_flag, &con->flags);
140 }
141
con_flag_test_and_clear(struct ceph_connection * con,unsigned long con_flag)142 static bool con_flag_test_and_clear(struct ceph_connection *con,
143 unsigned long con_flag)
144 {
145 BUG_ON(!con_flag_valid(con_flag));
146
147 return test_and_clear_bit(con_flag, &con->flags);
148 }
149
con_flag_test_and_set(struct ceph_connection * con,unsigned long con_flag)150 static bool con_flag_test_and_set(struct ceph_connection *con,
151 unsigned long con_flag)
152 {
153 BUG_ON(!con_flag_valid(con_flag));
154
155 return test_and_set_bit(con_flag, &con->flags);
156 }
157
158 /* Slab caches for frequently-allocated structures */
159
160 static struct kmem_cache *ceph_msg_cache;
161 static struct kmem_cache *ceph_msg_data_cache;
162
163 /* static tag bytes (protocol control messages) */
164 static char tag_msg = CEPH_MSGR_TAG_MSG;
165 static char tag_ack = CEPH_MSGR_TAG_ACK;
166 static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
167 static char tag_keepalive2 = CEPH_MSGR_TAG_KEEPALIVE2;
168
169 #ifdef CONFIG_LOCKDEP
170 static struct lock_class_key socket_class;
171 #endif
172
173 /*
174 * When skipping (ignoring) a block of input we read it into a "skip
175 * buffer," which is this many bytes in size.
176 */
177 #define SKIP_BUF_SIZE 1024
178
179 static void queue_con(struct ceph_connection *con);
180 static void cancel_con(struct ceph_connection *con);
181 static void ceph_con_workfn(struct work_struct *);
182 static void con_fault(struct ceph_connection *con);
183
184 /*
185 * Nicely render a sockaddr as a string. An array of formatted
186 * strings is used, to approximate reentrancy.
187 */
188 #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
189 #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
190 #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
191 #define MAX_ADDR_STR_LEN 64 /* 54 is enough */
192
193 static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
194 static atomic_t addr_str_seq = ATOMIC_INIT(0);
195
196 static struct page *zero_page; /* used in certain error cases */
197
ceph_pr_addr(const struct sockaddr_storage * ss)198 const char *ceph_pr_addr(const struct sockaddr_storage *ss)
199 {
200 int i;
201 char *s;
202 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
203 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
204
205 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
206 s = addr_str[i];
207
208 switch (ss->ss_family) {
209 case AF_INET:
210 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
211 ntohs(in4->sin_port));
212 break;
213
214 case AF_INET6:
215 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
216 ntohs(in6->sin6_port));
217 break;
218
219 default:
220 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
221 ss->ss_family);
222 }
223
224 return s;
225 }
226 EXPORT_SYMBOL(ceph_pr_addr);
227
encode_my_addr(struct ceph_messenger * msgr)228 static void encode_my_addr(struct ceph_messenger *msgr)
229 {
230 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
231 ceph_encode_addr(&msgr->my_enc_addr);
232 }
233
234 /*
235 * work queue for all reading and writing to/from the socket.
236 */
237 static struct workqueue_struct *ceph_msgr_wq;
238
ceph_msgr_slab_init(void)239 static int ceph_msgr_slab_init(void)
240 {
241 BUG_ON(ceph_msg_cache);
242 ceph_msg_cache = kmem_cache_create("ceph_msg",
243 sizeof (struct ceph_msg),
244 __alignof__(struct ceph_msg), 0, NULL);
245
246 if (!ceph_msg_cache)
247 return -ENOMEM;
248
249 BUG_ON(ceph_msg_data_cache);
250 ceph_msg_data_cache = kmem_cache_create("ceph_msg_data",
251 sizeof (struct ceph_msg_data),
252 __alignof__(struct ceph_msg_data),
253 0, NULL);
254 if (ceph_msg_data_cache)
255 return 0;
256
257 kmem_cache_destroy(ceph_msg_cache);
258 ceph_msg_cache = NULL;
259
260 return -ENOMEM;
261 }
262
ceph_msgr_slab_exit(void)263 static void ceph_msgr_slab_exit(void)
264 {
265 BUG_ON(!ceph_msg_data_cache);
266 kmem_cache_destroy(ceph_msg_data_cache);
267 ceph_msg_data_cache = NULL;
268
269 BUG_ON(!ceph_msg_cache);
270 kmem_cache_destroy(ceph_msg_cache);
271 ceph_msg_cache = NULL;
272 }
273
_ceph_msgr_exit(void)274 static void _ceph_msgr_exit(void)
275 {
276 if (ceph_msgr_wq) {
277 destroy_workqueue(ceph_msgr_wq);
278 ceph_msgr_wq = NULL;
279 }
280
281 BUG_ON(zero_page == NULL);
282 page_cache_release(zero_page);
283 zero_page = NULL;
284
285 ceph_msgr_slab_exit();
286 }
287
ceph_msgr_init(void)288 int ceph_msgr_init(void)
289 {
290 if (ceph_msgr_slab_init())
291 return -ENOMEM;
292
293 BUG_ON(zero_page != NULL);
294 zero_page = ZERO_PAGE(0);
295 page_cache_get(zero_page);
296
297 /*
298 * The number of active work items is limited by the number of
299 * connections, so leave @max_active at default.
300 */
301 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_MEM_RECLAIM, 0);
302 if (ceph_msgr_wq)
303 return 0;
304
305 pr_err("msgr_init failed to create workqueue\n");
306 _ceph_msgr_exit();
307
308 return -ENOMEM;
309 }
310 EXPORT_SYMBOL(ceph_msgr_init);
311
ceph_msgr_exit(void)312 void ceph_msgr_exit(void)
313 {
314 BUG_ON(ceph_msgr_wq == NULL);
315
316 _ceph_msgr_exit();
317 }
318 EXPORT_SYMBOL(ceph_msgr_exit);
319
ceph_msgr_flush(void)320 void ceph_msgr_flush(void)
321 {
322 flush_workqueue(ceph_msgr_wq);
323 }
324 EXPORT_SYMBOL(ceph_msgr_flush);
325
326 /* Connection socket state transition functions */
327
con_sock_state_init(struct ceph_connection * con)328 static void con_sock_state_init(struct ceph_connection *con)
329 {
330 int old_state;
331
332 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
333 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
334 printk("%s: unexpected old state %d\n", __func__, old_state);
335 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
336 CON_SOCK_STATE_CLOSED);
337 }
338
con_sock_state_connecting(struct ceph_connection * con)339 static void con_sock_state_connecting(struct ceph_connection *con)
340 {
341 int old_state;
342
343 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
344 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
345 printk("%s: unexpected old state %d\n", __func__, old_state);
346 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
347 CON_SOCK_STATE_CONNECTING);
348 }
349
con_sock_state_connected(struct ceph_connection * con)350 static void con_sock_state_connected(struct ceph_connection *con)
351 {
352 int old_state;
353
354 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
355 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
356 printk("%s: unexpected old state %d\n", __func__, old_state);
357 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
358 CON_SOCK_STATE_CONNECTED);
359 }
360
con_sock_state_closing(struct ceph_connection * con)361 static void con_sock_state_closing(struct ceph_connection *con)
362 {
363 int old_state;
364
365 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
366 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
367 old_state != CON_SOCK_STATE_CONNECTED &&
368 old_state != CON_SOCK_STATE_CLOSING))
369 printk("%s: unexpected old state %d\n", __func__, old_state);
370 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
371 CON_SOCK_STATE_CLOSING);
372 }
373
con_sock_state_closed(struct ceph_connection * con)374 static void con_sock_state_closed(struct ceph_connection *con)
375 {
376 int old_state;
377
378 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
379 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
380 old_state != CON_SOCK_STATE_CLOSING &&
381 old_state != CON_SOCK_STATE_CONNECTING &&
382 old_state != CON_SOCK_STATE_CLOSED))
383 printk("%s: unexpected old state %d\n", __func__, old_state);
384 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
385 CON_SOCK_STATE_CLOSED);
386 }
387
388 /*
389 * socket callback functions
390 */
391
392 /* data available on socket, or listen socket received a connect */
ceph_sock_data_ready(struct sock * sk)393 static void ceph_sock_data_ready(struct sock *sk)
394 {
395 struct ceph_connection *con = sk->sk_user_data;
396 if (atomic_read(&con->msgr->stopping)) {
397 return;
398 }
399
400 if (sk->sk_state != TCP_CLOSE_WAIT) {
401 dout("%s on %p state = %lu, queueing work\n", __func__,
402 con, con->state);
403 queue_con(con);
404 }
405 }
406
407 /* socket has buffer space for writing */
ceph_sock_write_space(struct sock * sk)408 static void ceph_sock_write_space(struct sock *sk)
409 {
410 struct ceph_connection *con = sk->sk_user_data;
411
412 /* only queue to workqueue if there is data we want to write,
413 * and there is sufficient space in the socket buffer to accept
414 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
415 * doesn't get called again until try_write() fills the socket
416 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
417 * and net/core/stream.c:sk_stream_write_space().
418 */
419 if (con_flag_test(con, CON_FLAG_WRITE_PENDING)) {
420 if (sk_stream_is_writeable(sk)) {
421 dout("%s %p queueing write work\n", __func__, con);
422 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
423 queue_con(con);
424 }
425 } else {
426 dout("%s %p nothing to write\n", __func__, con);
427 }
428 }
429
430 /* socket's state has changed */
ceph_sock_state_change(struct sock * sk)431 static void ceph_sock_state_change(struct sock *sk)
432 {
433 struct ceph_connection *con = sk->sk_user_data;
434
435 dout("%s %p state = %lu sk_state = %u\n", __func__,
436 con, con->state, sk->sk_state);
437
438 switch (sk->sk_state) {
439 case TCP_CLOSE:
440 dout("%s TCP_CLOSE\n", __func__);
441 case TCP_CLOSE_WAIT:
442 dout("%s TCP_CLOSE_WAIT\n", __func__);
443 con_sock_state_closing(con);
444 con_flag_set(con, CON_FLAG_SOCK_CLOSED);
445 queue_con(con);
446 break;
447 case TCP_ESTABLISHED:
448 dout("%s TCP_ESTABLISHED\n", __func__);
449 con_sock_state_connected(con);
450 queue_con(con);
451 break;
452 default: /* Everything else is uninteresting */
453 break;
454 }
455 }
456
457 /*
458 * set up socket callbacks
459 */
set_sock_callbacks(struct socket * sock,struct ceph_connection * con)460 static void set_sock_callbacks(struct socket *sock,
461 struct ceph_connection *con)
462 {
463 struct sock *sk = sock->sk;
464 sk->sk_user_data = con;
465 sk->sk_data_ready = ceph_sock_data_ready;
466 sk->sk_write_space = ceph_sock_write_space;
467 sk->sk_state_change = ceph_sock_state_change;
468 }
469
470
471 /*
472 * socket helpers
473 */
474
475 /*
476 * initiate connection to a remote socket.
477 */
ceph_tcp_connect(struct ceph_connection * con)478 static int ceph_tcp_connect(struct ceph_connection *con)
479 {
480 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
481 struct socket *sock;
482 unsigned int noio_flag;
483 int ret;
484
485 BUG_ON(con->sock);
486
487 /* sock_create_kern() allocates with GFP_KERNEL */
488 noio_flag = memalloc_noio_save();
489 ret = sock_create_kern(read_pnet(&con->msgr->net), paddr->ss_family,
490 SOCK_STREAM, IPPROTO_TCP, &sock);
491 memalloc_noio_restore(noio_flag);
492 if (ret)
493 return ret;
494 sock->sk->sk_allocation = GFP_NOFS;
495
496 #ifdef CONFIG_LOCKDEP
497 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
498 #endif
499
500 set_sock_callbacks(sock, con);
501
502 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
503
504 con_sock_state_connecting(con);
505 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
506 O_NONBLOCK);
507 if (ret == -EINPROGRESS) {
508 dout("connect %s EINPROGRESS sk_state = %u\n",
509 ceph_pr_addr(&con->peer_addr.in_addr),
510 sock->sk->sk_state);
511 } else if (ret < 0) {
512 pr_err("connect %s error %d\n",
513 ceph_pr_addr(&con->peer_addr.in_addr), ret);
514 sock_release(sock);
515 return ret;
516 }
517
518 if (ceph_test_opt(from_msgr(con->msgr), TCP_NODELAY)) {
519 int optval = 1;
520
521 ret = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
522 (char *)&optval, sizeof(optval));
523 if (ret)
524 pr_err("kernel_setsockopt(TCP_NODELAY) failed: %d",
525 ret);
526 }
527
528 con->sock = sock;
529 return 0;
530 }
531
ceph_tcp_recvmsg(struct socket * sock,void * buf,size_t len)532 static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
533 {
534 struct kvec iov = {buf, len};
535 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
536 int r;
537
538 r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags);
539 if (r == -EAGAIN)
540 r = 0;
541 return r;
542 }
543
ceph_tcp_recvpage(struct socket * sock,struct page * page,int page_offset,size_t length)544 static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
545 int page_offset, size_t length)
546 {
547 void *kaddr;
548 int ret;
549
550 BUG_ON(page_offset + length > PAGE_SIZE);
551
552 kaddr = kmap(page);
553 BUG_ON(!kaddr);
554 ret = ceph_tcp_recvmsg(sock, kaddr + page_offset, length);
555 kunmap(page);
556
557 return ret;
558 }
559
560 /*
561 * write something. @more is true if caller will be sending more data
562 * shortly.
563 */
ceph_tcp_sendmsg(struct socket * sock,struct kvec * iov,size_t kvlen,size_t len,int more)564 static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
565 size_t kvlen, size_t len, int more)
566 {
567 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
568 int r;
569
570 if (more)
571 msg.msg_flags |= MSG_MORE;
572 else
573 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
574
575 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
576 if (r == -EAGAIN)
577 r = 0;
578 return r;
579 }
580
__ceph_tcp_sendpage(struct socket * sock,struct page * page,int offset,size_t size,bool more)581 static int __ceph_tcp_sendpage(struct socket *sock, struct page *page,
582 int offset, size_t size, bool more)
583 {
584 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
585 int ret;
586
587 ret = kernel_sendpage(sock, page, offset, size, flags);
588 if (ret == -EAGAIN)
589 ret = 0;
590
591 return ret;
592 }
593
ceph_tcp_sendpage(struct socket * sock,struct page * page,int offset,size_t size,bool more)594 static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
595 int offset, size_t size, bool more)
596 {
597 int ret;
598 struct kvec iov;
599
600 /* sendpage cannot properly handle pages with page_count == 0,
601 * we need to fallback to sendmsg if that's the case */
602 if (page_count(page) >= 1)
603 return __ceph_tcp_sendpage(sock, page, offset, size, more);
604
605 iov.iov_base = kmap(page) + offset;
606 iov.iov_len = size;
607 ret = ceph_tcp_sendmsg(sock, &iov, 1, size, more);
608 kunmap(page);
609
610 return ret;
611 }
612
613 /*
614 * Shutdown/close the socket for the given connection.
615 */
con_close_socket(struct ceph_connection * con)616 static int con_close_socket(struct ceph_connection *con)
617 {
618 int rc = 0;
619
620 dout("con_close_socket on %p sock %p\n", con, con->sock);
621 if (con->sock) {
622 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
623 sock_release(con->sock);
624 con->sock = NULL;
625 }
626
627 /*
628 * Forcibly clear the SOCK_CLOSED flag. It gets set
629 * independent of the connection mutex, and we could have
630 * received a socket close event before we had the chance to
631 * shut the socket down.
632 */
633 con_flag_clear(con, CON_FLAG_SOCK_CLOSED);
634
635 con_sock_state_closed(con);
636 return rc;
637 }
638
639 /*
640 * Reset a connection. Discard all incoming and outgoing messages
641 * and clear *_seq state.
642 */
ceph_msg_remove(struct ceph_msg * msg)643 static void ceph_msg_remove(struct ceph_msg *msg)
644 {
645 list_del_init(&msg->list_head);
646
647 ceph_msg_put(msg);
648 }
ceph_msg_remove_list(struct list_head * head)649 static void ceph_msg_remove_list(struct list_head *head)
650 {
651 while (!list_empty(head)) {
652 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
653 list_head);
654 ceph_msg_remove(msg);
655 }
656 }
657
reset_connection(struct ceph_connection * con)658 static void reset_connection(struct ceph_connection *con)
659 {
660 /* reset connection, out_queue, msg_ and connect_seq */
661 /* discard existing out_queue and msg_seq */
662 dout("reset_connection %p\n", con);
663 ceph_msg_remove_list(&con->out_queue);
664 ceph_msg_remove_list(&con->out_sent);
665
666 if (con->in_msg) {
667 BUG_ON(con->in_msg->con != con);
668 ceph_msg_put(con->in_msg);
669 con->in_msg = NULL;
670 }
671
672 con->connect_seq = 0;
673 con->out_seq = 0;
674 if (con->out_msg) {
675 BUG_ON(con->out_msg->con != con);
676 ceph_msg_put(con->out_msg);
677 con->out_msg = NULL;
678 }
679 con->in_seq = 0;
680 con->in_seq_acked = 0;
681
682 con->out_skip = 0;
683 }
684
685 /*
686 * mark a peer down. drop any open connections.
687 */
ceph_con_close(struct ceph_connection * con)688 void ceph_con_close(struct ceph_connection *con)
689 {
690 mutex_lock(&con->mutex);
691 dout("con_close %p peer %s\n", con,
692 ceph_pr_addr(&con->peer_addr.in_addr));
693 con->state = CON_STATE_CLOSED;
694
695 con_flag_clear(con, CON_FLAG_LOSSYTX); /* so we retry next connect */
696 con_flag_clear(con, CON_FLAG_KEEPALIVE_PENDING);
697 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
698 con_flag_clear(con, CON_FLAG_BACKOFF);
699
700 reset_connection(con);
701 con->peer_global_seq = 0;
702 cancel_con(con);
703 con_close_socket(con);
704 mutex_unlock(&con->mutex);
705 }
706 EXPORT_SYMBOL(ceph_con_close);
707
708 /*
709 * Reopen a closed connection, with a new peer address.
710 */
ceph_con_open(struct ceph_connection * con,__u8 entity_type,__u64 entity_num,struct ceph_entity_addr * addr)711 void ceph_con_open(struct ceph_connection *con,
712 __u8 entity_type, __u64 entity_num,
713 struct ceph_entity_addr *addr)
714 {
715 mutex_lock(&con->mutex);
716 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
717
718 WARN_ON(con->state != CON_STATE_CLOSED);
719 con->state = CON_STATE_PREOPEN;
720
721 con->peer_name.type = (__u8) entity_type;
722 con->peer_name.num = cpu_to_le64(entity_num);
723
724 memcpy(&con->peer_addr, addr, sizeof(*addr));
725 con->delay = 0; /* reset backoff memory */
726 mutex_unlock(&con->mutex);
727 queue_con(con);
728 }
729 EXPORT_SYMBOL(ceph_con_open);
730
731 /*
732 * return true if this connection ever successfully opened
733 */
ceph_con_opened(struct ceph_connection * con)734 bool ceph_con_opened(struct ceph_connection *con)
735 {
736 return con->connect_seq > 0;
737 }
738
739 /*
740 * initialize a new connection.
741 */
ceph_con_init(struct ceph_connection * con,void * private,const struct ceph_connection_operations * ops,struct ceph_messenger * msgr)742 void ceph_con_init(struct ceph_connection *con, void *private,
743 const struct ceph_connection_operations *ops,
744 struct ceph_messenger *msgr)
745 {
746 dout("con_init %p\n", con);
747 memset(con, 0, sizeof(*con));
748 con->private = private;
749 con->ops = ops;
750 con->msgr = msgr;
751
752 con_sock_state_init(con);
753
754 mutex_init(&con->mutex);
755 INIT_LIST_HEAD(&con->out_queue);
756 INIT_LIST_HEAD(&con->out_sent);
757 INIT_DELAYED_WORK(&con->work, ceph_con_workfn);
758
759 con->state = CON_STATE_CLOSED;
760 }
761 EXPORT_SYMBOL(ceph_con_init);
762
763
764 /*
765 * We maintain a global counter to order connection attempts. Get
766 * a unique seq greater than @gt.
767 */
get_global_seq(struct ceph_messenger * msgr,u32 gt)768 static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
769 {
770 u32 ret;
771
772 spin_lock(&msgr->global_seq_lock);
773 if (msgr->global_seq < gt)
774 msgr->global_seq = gt;
775 ret = ++msgr->global_seq;
776 spin_unlock(&msgr->global_seq_lock);
777 return ret;
778 }
779
con_out_kvec_reset(struct ceph_connection * con)780 static void con_out_kvec_reset(struct ceph_connection *con)
781 {
782 BUG_ON(con->out_skip);
783
784 con->out_kvec_left = 0;
785 con->out_kvec_bytes = 0;
786 con->out_kvec_cur = &con->out_kvec[0];
787 }
788
con_out_kvec_add(struct ceph_connection * con,size_t size,void * data)789 static void con_out_kvec_add(struct ceph_connection *con,
790 size_t size, void *data)
791 {
792 int index = con->out_kvec_left;
793
794 BUG_ON(con->out_skip);
795 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
796
797 con->out_kvec[index].iov_len = size;
798 con->out_kvec[index].iov_base = data;
799 con->out_kvec_left++;
800 con->out_kvec_bytes += size;
801 }
802
803 /*
804 * Chop off a kvec from the end. Return residual number of bytes for
805 * that kvec, i.e. how many bytes would have been written if the kvec
806 * hadn't been nuked.
807 */
con_out_kvec_skip(struct ceph_connection * con)808 static int con_out_kvec_skip(struct ceph_connection *con)
809 {
810 int off = con->out_kvec_cur - con->out_kvec;
811 int skip = 0;
812
813 if (con->out_kvec_bytes > 0) {
814 skip = con->out_kvec[off + con->out_kvec_left - 1].iov_len;
815 BUG_ON(con->out_kvec_bytes < skip);
816 BUG_ON(!con->out_kvec_left);
817 con->out_kvec_bytes -= skip;
818 con->out_kvec_left--;
819 }
820
821 return skip;
822 }
823
824 #ifdef CONFIG_BLOCK
825
826 /*
827 * For a bio data item, a piece is whatever remains of the next
828 * entry in the current bio iovec, or the first entry in the next
829 * bio in the list.
830 */
ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor * cursor,size_t length)831 static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor,
832 size_t length)
833 {
834 struct ceph_msg_data *data = cursor->data;
835 struct bio *bio;
836
837 BUG_ON(data->type != CEPH_MSG_DATA_BIO);
838
839 bio = data->bio;
840 BUG_ON(!bio);
841
842 cursor->resid = min(length, data->bio_length);
843 cursor->bio = bio;
844 cursor->bvec_iter = bio->bi_iter;
845 cursor->last_piece =
846 cursor->resid <= bio_iter_len(bio, cursor->bvec_iter);
847 }
848
ceph_msg_data_bio_next(struct ceph_msg_data_cursor * cursor,size_t * page_offset,size_t * length)849 static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor,
850 size_t *page_offset,
851 size_t *length)
852 {
853 struct ceph_msg_data *data = cursor->data;
854 struct bio *bio;
855 struct bio_vec bio_vec;
856
857 BUG_ON(data->type != CEPH_MSG_DATA_BIO);
858
859 bio = cursor->bio;
860 BUG_ON(!bio);
861
862 bio_vec = bio_iter_iovec(bio, cursor->bvec_iter);
863
864 *page_offset = (size_t) bio_vec.bv_offset;
865 BUG_ON(*page_offset >= PAGE_SIZE);
866 if (cursor->last_piece) /* pagelist offset is always 0 */
867 *length = cursor->resid;
868 else
869 *length = (size_t) bio_vec.bv_len;
870 BUG_ON(*length > cursor->resid);
871 BUG_ON(*page_offset + *length > PAGE_SIZE);
872
873 return bio_vec.bv_page;
874 }
875
ceph_msg_data_bio_advance(struct ceph_msg_data_cursor * cursor,size_t bytes)876 static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
877 size_t bytes)
878 {
879 struct bio *bio;
880 struct bio_vec bio_vec;
881
882 BUG_ON(cursor->data->type != CEPH_MSG_DATA_BIO);
883
884 bio = cursor->bio;
885 BUG_ON(!bio);
886
887 bio_vec = bio_iter_iovec(bio, cursor->bvec_iter);
888
889 /* Advance the cursor offset */
890
891 BUG_ON(cursor->resid < bytes);
892 cursor->resid -= bytes;
893
894 bio_advance_iter(bio, &cursor->bvec_iter, bytes);
895
896 if (bytes < bio_vec.bv_len)
897 return false; /* more bytes to process in this segment */
898
899 /* Move on to the next segment, and possibly the next bio */
900
901 if (!cursor->bvec_iter.bi_size) {
902 bio = bio->bi_next;
903 cursor->bio = bio;
904 if (bio)
905 cursor->bvec_iter = bio->bi_iter;
906 else
907 memset(&cursor->bvec_iter, 0,
908 sizeof(cursor->bvec_iter));
909 }
910
911 if (!cursor->last_piece) {
912 BUG_ON(!cursor->resid);
913 BUG_ON(!bio);
914 /* A short read is OK, so use <= rather than == */
915 if (cursor->resid <= bio_iter_len(bio, cursor->bvec_iter))
916 cursor->last_piece = true;
917 }
918
919 return true;
920 }
921 #endif /* CONFIG_BLOCK */
922
923 /*
924 * For a page array, a piece comes from the first page in the array
925 * that has not already been fully consumed.
926 */
ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor * cursor,size_t length)927 static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor,
928 size_t length)
929 {
930 struct ceph_msg_data *data = cursor->data;
931 int page_count;
932
933 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
934
935 BUG_ON(!data->pages);
936 BUG_ON(!data->length);
937
938 cursor->resid = min(length, data->length);
939 page_count = calc_pages_for(data->alignment, (u64)data->length);
940 cursor->page_offset = data->alignment & ~PAGE_MASK;
941 cursor->page_index = 0;
942 BUG_ON(page_count > (int)USHRT_MAX);
943 cursor->page_count = (unsigned short)page_count;
944 BUG_ON(length > SIZE_MAX - cursor->page_offset);
945 cursor->last_piece = cursor->page_offset + cursor->resid <= PAGE_SIZE;
946 }
947
948 static struct page *
ceph_msg_data_pages_next(struct ceph_msg_data_cursor * cursor,size_t * page_offset,size_t * length)949 ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
950 size_t *page_offset, size_t *length)
951 {
952 struct ceph_msg_data *data = cursor->data;
953
954 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
955
956 BUG_ON(cursor->page_index >= cursor->page_count);
957 BUG_ON(cursor->page_offset >= PAGE_SIZE);
958
959 *page_offset = cursor->page_offset;
960 if (cursor->last_piece)
961 *length = cursor->resid;
962 else
963 *length = PAGE_SIZE - *page_offset;
964
965 return data->pages[cursor->page_index];
966 }
967
ceph_msg_data_pages_advance(struct ceph_msg_data_cursor * cursor,size_t bytes)968 static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
969 size_t bytes)
970 {
971 BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES);
972
973 BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
974
975 /* Advance the cursor page offset */
976
977 cursor->resid -= bytes;
978 cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK;
979 if (!bytes || cursor->page_offset)
980 return false; /* more bytes to process in the current page */
981
982 if (!cursor->resid)
983 return false; /* no more data */
984
985 /* Move on to the next page; offset is already at 0 */
986
987 BUG_ON(cursor->page_index >= cursor->page_count);
988 cursor->page_index++;
989 cursor->last_piece = cursor->resid <= PAGE_SIZE;
990
991 return true;
992 }
993
994 /*
995 * For a pagelist, a piece is whatever remains to be consumed in the
996 * first page in the list, or the front of the next page.
997 */
998 static void
ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor * cursor,size_t length)999 ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
1000 size_t length)
1001 {
1002 struct ceph_msg_data *data = cursor->data;
1003 struct ceph_pagelist *pagelist;
1004 struct page *page;
1005
1006 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1007
1008 pagelist = data->pagelist;
1009 BUG_ON(!pagelist);
1010
1011 if (!length)
1012 return; /* pagelist can be assigned but empty */
1013
1014 BUG_ON(list_empty(&pagelist->head));
1015 page = list_first_entry(&pagelist->head, struct page, lru);
1016
1017 cursor->resid = min(length, pagelist->length);
1018 cursor->page = page;
1019 cursor->offset = 0;
1020 cursor->last_piece = cursor->resid <= PAGE_SIZE;
1021 }
1022
1023 static struct page *
ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor * cursor,size_t * page_offset,size_t * length)1024 ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
1025 size_t *page_offset, size_t *length)
1026 {
1027 struct ceph_msg_data *data = cursor->data;
1028 struct ceph_pagelist *pagelist;
1029
1030 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1031
1032 pagelist = data->pagelist;
1033 BUG_ON(!pagelist);
1034
1035 BUG_ON(!cursor->page);
1036 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
1037
1038 /* offset of first page in pagelist is always 0 */
1039 *page_offset = cursor->offset & ~PAGE_MASK;
1040 if (cursor->last_piece)
1041 *length = cursor->resid;
1042 else
1043 *length = PAGE_SIZE - *page_offset;
1044
1045 return cursor->page;
1046 }
1047
ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor * cursor,size_t bytes)1048 static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
1049 size_t bytes)
1050 {
1051 struct ceph_msg_data *data = cursor->data;
1052 struct ceph_pagelist *pagelist;
1053
1054 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1055
1056 pagelist = data->pagelist;
1057 BUG_ON(!pagelist);
1058
1059 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
1060 BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
1061
1062 /* Advance the cursor offset */
1063
1064 cursor->resid -= bytes;
1065 cursor->offset += bytes;
1066 /* offset of first page in pagelist is always 0 */
1067 if (!bytes || cursor->offset & ~PAGE_MASK)
1068 return false; /* more bytes to process in the current page */
1069
1070 if (!cursor->resid)
1071 return false; /* no more data */
1072
1073 /* Move on to the next page */
1074
1075 BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
1076 cursor->page = list_entry_next(cursor->page, lru);
1077 cursor->last_piece = cursor->resid <= PAGE_SIZE;
1078
1079 return true;
1080 }
1081
1082 /*
1083 * Message data is handled (sent or received) in pieces, where each
1084 * piece resides on a single page. The network layer might not
1085 * consume an entire piece at once. A data item's cursor keeps
1086 * track of which piece is next to process and how much remains to
1087 * be processed in that piece. It also tracks whether the current
1088 * piece is the last one in the data item.
1089 */
__ceph_msg_data_cursor_init(struct ceph_msg_data_cursor * cursor)1090 static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor)
1091 {
1092 size_t length = cursor->total_resid;
1093
1094 switch (cursor->data->type) {
1095 case CEPH_MSG_DATA_PAGELIST:
1096 ceph_msg_data_pagelist_cursor_init(cursor, length);
1097 break;
1098 case CEPH_MSG_DATA_PAGES:
1099 ceph_msg_data_pages_cursor_init(cursor, length);
1100 break;
1101 #ifdef CONFIG_BLOCK
1102 case CEPH_MSG_DATA_BIO:
1103 ceph_msg_data_bio_cursor_init(cursor, length);
1104 break;
1105 #endif /* CONFIG_BLOCK */
1106 case CEPH_MSG_DATA_NONE:
1107 default:
1108 /* BUG(); */
1109 break;
1110 }
1111 cursor->need_crc = true;
1112 }
1113
ceph_msg_data_cursor_init(struct ceph_msg * msg,size_t length)1114 static void ceph_msg_data_cursor_init(struct ceph_msg *msg, size_t length)
1115 {
1116 struct ceph_msg_data_cursor *cursor = &msg->cursor;
1117 struct ceph_msg_data *data;
1118
1119 BUG_ON(!length);
1120 BUG_ON(length > msg->data_length);
1121 BUG_ON(list_empty(&msg->data));
1122
1123 cursor->data_head = &msg->data;
1124 cursor->total_resid = length;
1125 data = list_first_entry(&msg->data, struct ceph_msg_data, links);
1126 cursor->data = data;
1127
1128 __ceph_msg_data_cursor_init(cursor);
1129 }
1130
1131 /*
1132 * Return the page containing the next piece to process for a given
1133 * data item, and supply the page offset and length of that piece.
1134 * Indicate whether this is the last piece in this data item.
1135 */
ceph_msg_data_next(struct ceph_msg_data_cursor * cursor,size_t * page_offset,size_t * length,bool * last_piece)1136 static struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
1137 size_t *page_offset, size_t *length,
1138 bool *last_piece)
1139 {
1140 struct page *page;
1141
1142 switch (cursor->data->type) {
1143 case CEPH_MSG_DATA_PAGELIST:
1144 page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
1145 break;
1146 case CEPH_MSG_DATA_PAGES:
1147 page = ceph_msg_data_pages_next(cursor, page_offset, length);
1148 break;
1149 #ifdef CONFIG_BLOCK
1150 case CEPH_MSG_DATA_BIO:
1151 page = ceph_msg_data_bio_next(cursor, page_offset, length);
1152 break;
1153 #endif /* CONFIG_BLOCK */
1154 case CEPH_MSG_DATA_NONE:
1155 default:
1156 page = NULL;
1157 break;
1158 }
1159 BUG_ON(!page);
1160 BUG_ON(*page_offset + *length > PAGE_SIZE);
1161 BUG_ON(!*length);
1162 if (last_piece)
1163 *last_piece = cursor->last_piece;
1164
1165 return page;
1166 }
1167
1168 /*
1169 * Returns true if the result moves the cursor on to the next piece
1170 * of the data item.
1171 */
ceph_msg_data_advance(struct ceph_msg_data_cursor * cursor,size_t bytes)1172 static bool ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor,
1173 size_t bytes)
1174 {
1175 bool new_piece;
1176
1177 BUG_ON(bytes > cursor->resid);
1178 switch (cursor->data->type) {
1179 case CEPH_MSG_DATA_PAGELIST:
1180 new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
1181 break;
1182 case CEPH_MSG_DATA_PAGES:
1183 new_piece = ceph_msg_data_pages_advance(cursor, bytes);
1184 break;
1185 #ifdef CONFIG_BLOCK
1186 case CEPH_MSG_DATA_BIO:
1187 new_piece = ceph_msg_data_bio_advance(cursor, bytes);
1188 break;
1189 #endif /* CONFIG_BLOCK */
1190 case CEPH_MSG_DATA_NONE:
1191 default:
1192 BUG();
1193 break;
1194 }
1195 cursor->total_resid -= bytes;
1196
1197 if (!cursor->resid && cursor->total_resid) {
1198 WARN_ON(!cursor->last_piece);
1199 BUG_ON(list_is_last(&cursor->data->links, cursor->data_head));
1200 cursor->data = list_entry_next(cursor->data, links);
1201 __ceph_msg_data_cursor_init(cursor);
1202 new_piece = true;
1203 }
1204 cursor->need_crc = new_piece;
1205
1206 return new_piece;
1207 }
1208
sizeof_footer(struct ceph_connection * con)1209 static size_t sizeof_footer(struct ceph_connection *con)
1210 {
1211 return (con->peer_features & CEPH_FEATURE_MSG_AUTH) ?
1212 sizeof(struct ceph_msg_footer) :
1213 sizeof(struct ceph_msg_footer_old);
1214 }
1215
prepare_message_data(struct ceph_msg * msg,u32 data_len)1216 static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
1217 {
1218 BUG_ON(!msg);
1219 BUG_ON(!data_len);
1220
1221 /* Initialize data cursor */
1222
1223 ceph_msg_data_cursor_init(msg, (size_t)data_len);
1224 }
1225
1226 /*
1227 * Prepare footer for currently outgoing message, and finish things
1228 * off. Assumes out_kvec* are already valid.. we just add on to the end.
1229 */
prepare_write_message_footer(struct ceph_connection * con)1230 static void prepare_write_message_footer(struct ceph_connection *con)
1231 {
1232 struct ceph_msg *m = con->out_msg;
1233 int v = con->out_kvec_left;
1234
1235 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
1236
1237 dout("prepare_write_message_footer %p\n", con);
1238 con->out_kvec[v].iov_base = &m->footer;
1239 if (con->peer_features & CEPH_FEATURE_MSG_AUTH) {
1240 if (con->ops->sign_message)
1241 con->ops->sign_message(m);
1242 else
1243 m->footer.sig = 0;
1244 con->out_kvec[v].iov_len = sizeof(m->footer);
1245 con->out_kvec_bytes += sizeof(m->footer);
1246 } else {
1247 m->old_footer.flags = m->footer.flags;
1248 con->out_kvec[v].iov_len = sizeof(m->old_footer);
1249 con->out_kvec_bytes += sizeof(m->old_footer);
1250 }
1251 con->out_kvec_left++;
1252 con->out_more = m->more_to_follow;
1253 con->out_msg_done = true;
1254 }
1255
1256 /*
1257 * Prepare headers for the next outgoing message.
1258 */
prepare_write_message(struct ceph_connection * con)1259 static void prepare_write_message(struct ceph_connection *con)
1260 {
1261 struct ceph_msg *m;
1262 u32 crc;
1263
1264 con_out_kvec_reset(con);
1265 con->out_msg_done = false;
1266
1267 /* Sneak an ack in there first? If we can get it into the same
1268 * TCP packet that's a good thing. */
1269 if (con->in_seq > con->in_seq_acked) {
1270 con->in_seq_acked = con->in_seq;
1271 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
1272 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1273 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1274 &con->out_temp_ack);
1275 }
1276
1277 BUG_ON(list_empty(&con->out_queue));
1278 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
1279 con->out_msg = m;
1280 BUG_ON(m->con != con);
1281
1282 /* put message on sent list */
1283 ceph_msg_get(m);
1284 list_move_tail(&m->list_head, &con->out_sent);
1285
1286 /*
1287 * only assign outgoing seq # if we haven't sent this message
1288 * yet. if it is requeued, resend with it's original seq.
1289 */
1290 if (m->needs_out_seq) {
1291 m->hdr.seq = cpu_to_le64(++con->out_seq);
1292 m->needs_out_seq = false;
1293 }
1294 WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
1295
1296 dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
1297 m, con->out_seq, le16_to_cpu(m->hdr.type),
1298 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
1299 m->data_length);
1300 BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len);
1301
1302 /* tag + hdr + front + middle */
1303 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
1304 con_out_kvec_add(con, sizeof(con->out_hdr), &con->out_hdr);
1305 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
1306
1307 if (m->middle)
1308 con_out_kvec_add(con, m->middle->vec.iov_len,
1309 m->middle->vec.iov_base);
1310
1311 /* fill in hdr crc and finalize hdr */
1312 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
1313 con->out_msg->hdr.crc = cpu_to_le32(crc);
1314 memcpy(&con->out_hdr, &con->out_msg->hdr, sizeof(con->out_hdr));
1315
1316 /* fill in front and middle crc, footer */
1317 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
1318 con->out_msg->footer.front_crc = cpu_to_le32(crc);
1319 if (m->middle) {
1320 crc = crc32c(0, m->middle->vec.iov_base,
1321 m->middle->vec.iov_len);
1322 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
1323 } else
1324 con->out_msg->footer.middle_crc = 0;
1325 dout("%s front_crc %u middle_crc %u\n", __func__,
1326 le32_to_cpu(con->out_msg->footer.front_crc),
1327 le32_to_cpu(con->out_msg->footer.middle_crc));
1328 con->out_msg->footer.flags = 0;
1329
1330 /* is there a data payload? */
1331 con->out_msg->footer.data_crc = 0;
1332 if (m->data_length) {
1333 prepare_message_data(con->out_msg, m->data_length);
1334 con->out_more = 1; /* data + footer will follow */
1335 } else {
1336 /* no, queue up footer too and be done */
1337 prepare_write_message_footer(con);
1338 }
1339
1340 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1341 }
1342
1343 /*
1344 * Prepare an ack.
1345 */
prepare_write_ack(struct ceph_connection * con)1346 static void prepare_write_ack(struct ceph_connection *con)
1347 {
1348 dout("prepare_write_ack %p %llu -> %llu\n", con,
1349 con->in_seq_acked, con->in_seq);
1350 con->in_seq_acked = con->in_seq;
1351
1352 con_out_kvec_reset(con);
1353
1354 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
1355
1356 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1357 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1358 &con->out_temp_ack);
1359
1360 con->out_more = 1; /* more will follow.. eventually.. */
1361 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1362 }
1363
1364 /*
1365 * Prepare to share the seq during handshake
1366 */
prepare_write_seq(struct ceph_connection * con)1367 static void prepare_write_seq(struct ceph_connection *con)
1368 {
1369 dout("prepare_write_seq %p %llu -> %llu\n", con,
1370 con->in_seq_acked, con->in_seq);
1371 con->in_seq_acked = con->in_seq;
1372
1373 con_out_kvec_reset(con);
1374
1375 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1376 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1377 &con->out_temp_ack);
1378
1379 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1380 }
1381
1382 /*
1383 * Prepare to write keepalive byte.
1384 */
prepare_write_keepalive(struct ceph_connection * con)1385 static void prepare_write_keepalive(struct ceph_connection *con)
1386 {
1387 dout("prepare_write_keepalive %p\n", con);
1388 con_out_kvec_reset(con);
1389 if (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2) {
1390 struct timespec now = CURRENT_TIME;
1391
1392 con_out_kvec_add(con, sizeof(tag_keepalive2), &tag_keepalive2);
1393 ceph_encode_timespec(&con->out_temp_keepalive2, &now);
1394 con_out_kvec_add(con, sizeof(con->out_temp_keepalive2),
1395 &con->out_temp_keepalive2);
1396 } else {
1397 con_out_kvec_add(con, sizeof(tag_keepalive), &tag_keepalive);
1398 }
1399 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1400 }
1401
1402 /*
1403 * Connection negotiation.
1404 */
1405
get_connect_authorizer(struct ceph_connection * con,int * auth_proto)1406 static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
1407 int *auth_proto)
1408 {
1409 struct ceph_auth_handshake *auth;
1410
1411 if (!con->ops->get_authorizer) {
1412 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
1413 con->out_connect.authorizer_len = 0;
1414 return NULL;
1415 }
1416
1417 /* Can't hold the mutex while getting authorizer */
1418 mutex_unlock(&con->mutex);
1419 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
1420 mutex_lock(&con->mutex);
1421
1422 if (IS_ERR(auth))
1423 return auth;
1424 if (con->state != CON_STATE_NEGOTIATING)
1425 return ERR_PTR(-EAGAIN);
1426
1427 con->auth_reply_buf = auth->authorizer_reply_buf;
1428 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
1429 return auth;
1430 }
1431
1432 /*
1433 * We connected to a peer and are saying hello.
1434 */
prepare_write_banner(struct ceph_connection * con)1435 static void prepare_write_banner(struct ceph_connection *con)
1436 {
1437 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
1438 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
1439 &con->msgr->my_enc_addr);
1440
1441 con->out_more = 0;
1442 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1443 }
1444
prepare_write_connect(struct ceph_connection * con)1445 static int prepare_write_connect(struct ceph_connection *con)
1446 {
1447 unsigned int global_seq = get_global_seq(con->msgr, 0);
1448 int proto;
1449 int auth_proto;
1450 struct ceph_auth_handshake *auth;
1451
1452 switch (con->peer_name.type) {
1453 case CEPH_ENTITY_TYPE_MON:
1454 proto = CEPH_MONC_PROTOCOL;
1455 break;
1456 case CEPH_ENTITY_TYPE_OSD:
1457 proto = CEPH_OSDC_PROTOCOL;
1458 break;
1459 case CEPH_ENTITY_TYPE_MDS:
1460 proto = CEPH_MDSC_PROTOCOL;
1461 break;
1462 default:
1463 BUG();
1464 }
1465
1466 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
1467 con->connect_seq, global_seq, proto);
1468
1469 con->out_connect.features =
1470 cpu_to_le64(from_msgr(con->msgr)->supported_features);
1471 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
1472 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
1473 con->out_connect.global_seq = cpu_to_le32(global_seq);
1474 con->out_connect.protocol_version = cpu_to_le32(proto);
1475 con->out_connect.flags = 0;
1476
1477 auth_proto = CEPH_AUTH_UNKNOWN;
1478 auth = get_connect_authorizer(con, &auth_proto);
1479 if (IS_ERR(auth))
1480 return PTR_ERR(auth);
1481
1482 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
1483 con->out_connect.authorizer_len = auth ?
1484 cpu_to_le32(auth->authorizer_buf_len) : 0;
1485
1486 con_out_kvec_add(con, sizeof (con->out_connect),
1487 &con->out_connect);
1488 if (auth && auth->authorizer_buf_len)
1489 con_out_kvec_add(con, auth->authorizer_buf_len,
1490 auth->authorizer_buf);
1491
1492 con->out_more = 0;
1493 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1494
1495 return 0;
1496 }
1497
1498 /*
1499 * write as much of pending kvecs to the socket as we can.
1500 * 1 -> done
1501 * 0 -> socket full, but more to do
1502 * <0 -> error
1503 */
write_partial_kvec(struct ceph_connection * con)1504 static int write_partial_kvec(struct ceph_connection *con)
1505 {
1506 int ret;
1507
1508 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
1509 while (con->out_kvec_bytes > 0) {
1510 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
1511 con->out_kvec_left, con->out_kvec_bytes,
1512 con->out_more);
1513 if (ret <= 0)
1514 goto out;
1515 con->out_kvec_bytes -= ret;
1516 if (con->out_kvec_bytes == 0)
1517 break; /* done */
1518
1519 /* account for full iov entries consumed */
1520 while (ret >= con->out_kvec_cur->iov_len) {
1521 BUG_ON(!con->out_kvec_left);
1522 ret -= con->out_kvec_cur->iov_len;
1523 con->out_kvec_cur++;
1524 con->out_kvec_left--;
1525 }
1526 /* and for a partially-consumed entry */
1527 if (ret) {
1528 con->out_kvec_cur->iov_len -= ret;
1529 con->out_kvec_cur->iov_base += ret;
1530 }
1531 }
1532 con->out_kvec_left = 0;
1533 ret = 1;
1534 out:
1535 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
1536 con->out_kvec_bytes, con->out_kvec_left, ret);
1537 return ret; /* done! */
1538 }
1539
ceph_crc32c_page(u32 crc,struct page * page,unsigned int page_offset,unsigned int length)1540 static u32 ceph_crc32c_page(u32 crc, struct page *page,
1541 unsigned int page_offset,
1542 unsigned int length)
1543 {
1544 char *kaddr;
1545
1546 kaddr = kmap(page);
1547 BUG_ON(kaddr == NULL);
1548 crc = crc32c(crc, kaddr + page_offset, length);
1549 kunmap(page);
1550
1551 return crc;
1552 }
1553 /*
1554 * Write as much message data payload as we can. If we finish, queue
1555 * up the footer.
1556 * 1 -> done, footer is now queued in out_kvec[].
1557 * 0 -> socket full, but more to do
1558 * <0 -> error
1559 */
write_partial_message_data(struct ceph_connection * con)1560 static int write_partial_message_data(struct ceph_connection *con)
1561 {
1562 struct ceph_msg *msg = con->out_msg;
1563 struct ceph_msg_data_cursor *cursor = &msg->cursor;
1564 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
1565 u32 crc;
1566
1567 dout("%s %p msg %p\n", __func__, con, msg);
1568
1569 if (list_empty(&msg->data))
1570 return -EINVAL;
1571
1572 /*
1573 * Iterate through each page that contains data to be
1574 * written, and send as much as possible for each.
1575 *
1576 * If we are calculating the data crc (the default), we will
1577 * need to map the page. If we have no pages, they have
1578 * been revoked, so use the zero page.
1579 */
1580 crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
1581 while (cursor->resid) {
1582 struct page *page;
1583 size_t page_offset;
1584 size_t length;
1585 bool last_piece;
1586 bool need_crc;
1587 int ret;
1588
1589 page = ceph_msg_data_next(cursor, &page_offset, &length,
1590 &last_piece);
1591 ret = ceph_tcp_sendpage(con->sock, page, page_offset,
1592 length, !last_piece);
1593 if (ret <= 0) {
1594 if (do_datacrc)
1595 msg->footer.data_crc = cpu_to_le32(crc);
1596
1597 return ret;
1598 }
1599 if (do_datacrc && cursor->need_crc)
1600 crc = ceph_crc32c_page(crc, page, page_offset, length);
1601 need_crc = ceph_msg_data_advance(cursor, (size_t)ret);
1602 }
1603
1604 dout("%s %p msg %p done\n", __func__, con, msg);
1605
1606 /* prepare and queue up footer, too */
1607 if (do_datacrc)
1608 msg->footer.data_crc = cpu_to_le32(crc);
1609 else
1610 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
1611 con_out_kvec_reset(con);
1612 prepare_write_message_footer(con);
1613
1614 return 1; /* must return > 0 to indicate success */
1615 }
1616
1617 /*
1618 * write some zeros
1619 */
write_partial_skip(struct ceph_connection * con)1620 static int write_partial_skip(struct ceph_connection *con)
1621 {
1622 int ret;
1623
1624 dout("%s %p %d left\n", __func__, con, con->out_skip);
1625 while (con->out_skip > 0) {
1626 size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE);
1627
1628 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
1629 if (ret <= 0)
1630 goto out;
1631 con->out_skip -= ret;
1632 }
1633 ret = 1;
1634 out:
1635 return ret;
1636 }
1637
1638 /*
1639 * Prepare to read connection handshake, or an ack.
1640 */
prepare_read_banner(struct ceph_connection * con)1641 static void prepare_read_banner(struct ceph_connection *con)
1642 {
1643 dout("prepare_read_banner %p\n", con);
1644 con->in_base_pos = 0;
1645 }
1646
prepare_read_connect(struct ceph_connection * con)1647 static void prepare_read_connect(struct ceph_connection *con)
1648 {
1649 dout("prepare_read_connect %p\n", con);
1650 con->in_base_pos = 0;
1651 }
1652
prepare_read_ack(struct ceph_connection * con)1653 static void prepare_read_ack(struct ceph_connection *con)
1654 {
1655 dout("prepare_read_ack %p\n", con);
1656 con->in_base_pos = 0;
1657 }
1658
prepare_read_seq(struct ceph_connection * con)1659 static void prepare_read_seq(struct ceph_connection *con)
1660 {
1661 dout("prepare_read_seq %p\n", con);
1662 con->in_base_pos = 0;
1663 con->in_tag = CEPH_MSGR_TAG_SEQ;
1664 }
1665
prepare_read_tag(struct ceph_connection * con)1666 static void prepare_read_tag(struct ceph_connection *con)
1667 {
1668 dout("prepare_read_tag %p\n", con);
1669 con->in_base_pos = 0;
1670 con->in_tag = CEPH_MSGR_TAG_READY;
1671 }
1672
prepare_read_keepalive_ack(struct ceph_connection * con)1673 static void prepare_read_keepalive_ack(struct ceph_connection *con)
1674 {
1675 dout("prepare_read_keepalive_ack %p\n", con);
1676 con->in_base_pos = 0;
1677 }
1678
1679 /*
1680 * Prepare to read a message.
1681 */
prepare_read_message(struct ceph_connection * con)1682 static int prepare_read_message(struct ceph_connection *con)
1683 {
1684 dout("prepare_read_message %p\n", con);
1685 BUG_ON(con->in_msg != NULL);
1686 con->in_base_pos = 0;
1687 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1688 return 0;
1689 }
1690
1691
read_partial(struct ceph_connection * con,int end,int size,void * object)1692 static int read_partial(struct ceph_connection *con,
1693 int end, int size, void *object)
1694 {
1695 while (con->in_base_pos < end) {
1696 int left = end - con->in_base_pos;
1697 int have = size - left;
1698 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1699 if (ret <= 0)
1700 return ret;
1701 con->in_base_pos += ret;
1702 }
1703 return 1;
1704 }
1705
1706
1707 /*
1708 * Read all or part of the connect-side handshake on a new connection
1709 */
read_partial_banner(struct ceph_connection * con)1710 static int read_partial_banner(struct ceph_connection *con)
1711 {
1712 int size;
1713 int end;
1714 int ret;
1715
1716 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
1717
1718 /* peer's banner */
1719 size = strlen(CEPH_BANNER);
1720 end = size;
1721 ret = read_partial(con, end, size, con->in_banner);
1722 if (ret <= 0)
1723 goto out;
1724
1725 size = sizeof (con->actual_peer_addr);
1726 end += size;
1727 ret = read_partial(con, end, size, &con->actual_peer_addr);
1728 if (ret <= 0)
1729 goto out;
1730
1731 size = sizeof (con->peer_addr_for_me);
1732 end += size;
1733 ret = read_partial(con, end, size, &con->peer_addr_for_me);
1734 if (ret <= 0)
1735 goto out;
1736
1737 out:
1738 return ret;
1739 }
1740
read_partial_connect(struct ceph_connection * con)1741 static int read_partial_connect(struct ceph_connection *con)
1742 {
1743 int size;
1744 int end;
1745 int ret;
1746
1747 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1748
1749 size = sizeof (con->in_reply);
1750 end = size;
1751 ret = read_partial(con, end, size, &con->in_reply);
1752 if (ret <= 0)
1753 goto out;
1754
1755 size = le32_to_cpu(con->in_reply.authorizer_len);
1756 end += size;
1757 ret = read_partial(con, end, size, con->auth_reply_buf);
1758 if (ret <= 0)
1759 goto out;
1760
1761 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1762 con, (int)con->in_reply.tag,
1763 le32_to_cpu(con->in_reply.connect_seq),
1764 le32_to_cpu(con->in_reply.global_seq));
1765 out:
1766 return ret;
1767
1768 }
1769
1770 /*
1771 * Verify the hello banner looks okay.
1772 */
verify_hello(struct ceph_connection * con)1773 static int verify_hello(struct ceph_connection *con)
1774 {
1775 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
1776 pr_err("connect to %s got bad banner\n",
1777 ceph_pr_addr(&con->peer_addr.in_addr));
1778 con->error_msg = "protocol error, bad banner";
1779 return -1;
1780 }
1781 return 0;
1782 }
1783
addr_is_blank(struct sockaddr_storage * ss)1784 static bool addr_is_blank(struct sockaddr_storage *ss)
1785 {
1786 struct in_addr *addr = &((struct sockaddr_in *)ss)->sin_addr;
1787 struct in6_addr *addr6 = &((struct sockaddr_in6 *)ss)->sin6_addr;
1788
1789 switch (ss->ss_family) {
1790 case AF_INET:
1791 return addr->s_addr == htonl(INADDR_ANY);
1792 case AF_INET6:
1793 return ipv6_addr_any(addr6);
1794 default:
1795 return true;
1796 }
1797 }
1798
addr_port(struct sockaddr_storage * ss)1799 static int addr_port(struct sockaddr_storage *ss)
1800 {
1801 switch (ss->ss_family) {
1802 case AF_INET:
1803 return ntohs(((struct sockaddr_in *)ss)->sin_port);
1804 case AF_INET6:
1805 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
1806 }
1807 return 0;
1808 }
1809
addr_set_port(struct sockaddr_storage * ss,int p)1810 static void addr_set_port(struct sockaddr_storage *ss, int p)
1811 {
1812 switch (ss->ss_family) {
1813 case AF_INET:
1814 ((struct sockaddr_in *)ss)->sin_port = htons(p);
1815 break;
1816 case AF_INET6:
1817 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
1818 break;
1819 }
1820 }
1821
1822 /*
1823 * Unlike other *_pton function semantics, zero indicates success.
1824 */
ceph_pton(const char * str,size_t len,struct sockaddr_storage * ss,char delim,const char ** ipend)1825 static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1826 char delim, const char **ipend)
1827 {
1828 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1829 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
1830
1831 memset(ss, 0, sizeof(*ss));
1832
1833 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1834 ss->ss_family = AF_INET;
1835 return 0;
1836 }
1837
1838 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1839 ss->ss_family = AF_INET6;
1840 return 0;
1841 }
1842
1843 return -EINVAL;
1844 }
1845
1846 /*
1847 * Extract hostname string and resolve using kernel DNS facility.
1848 */
1849 #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
ceph_dns_resolve_name(const char * name,size_t namelen,struct sockaddr_storage * ss,char delim,const char ** ipend)1850 static int ceph_dns_resolve_name(const char *name, size_t namelen,
1851 struct sockaddr_storage *ss, char delim, const char **ipend)
1852 {
1853 const char *end, *delim_p;
1854 char *colon_p, *ip_addr = NULL;
1855 int ip_len, ret;
1856
1857 /*
1858 * The end of the hostname occurs immediately preceding the delimiter or
1859 * the port marker (':') where the delimiter takes precedence.
1860 */
1861 delim_p = memchr(name, delim, namelen);
1862 colon_p = memchr(name, ':', namelen);
1863
1864 if (delim_p && colon_p)
1865 end = delim_p < colon_p ? delim_p : colon_p;
1866 else if (!delim_p && colon_p)
1867 end = colon_p;
1868 else {
1869 end = delim_p;
1870 if (!end) /* case: hostname:/ */
1871 end = name + namelen;
1872 }
1873
1874 if (end <= name)
1875 return -EINVAL;
1876
1877 /* do dns_resolve upcall */
1878 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1879 if (ip_len > 0)
1880 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1881 else
1882 ret = -ESRCH;
1883
1884 kfree(ip_addr);
1885
1886 *ipend = end;
1887
1888 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1889 ret, ret ? "failed" : ceph_pr_addr(ss));
1890
1891 return ret;
1892 }
1893 #else
ceph_dns_resolve_name(const char * name,size_t namelen,struct sockaddr_storage * ss,char delim,const char ** ipend)1894 static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1895 struct sockaddr_storage *ss, char delim, const char **ipend)
1896 {
1897 return -EINVAL;
1898 }
1899 #endif
1900
1901 /*
1902 * Parse a server name (IP or hostname). If a valid IP address is not found
1903 * then try to extract a hostname to resolve using userspace DNS upcall.
1904 */
ceph_parse_server_name(const char * name,size_t namelen,struct sockaddr_storage * ss,char delim,const char ** ipend)1905 static int ceph_parse_server_name(const char *name, size_t namelen,
1906 struct sockaddr_storage *ss, char delim, const char **ipend)
1907 {
1908 int ret;
1909
1910 ret = ceph_pton(name, namelen, ss, delim, ipend);
1911 if (ret)
1912 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1913
1914 return ret;
1915 }
1916
1917 /*
1918 * Parse an ip[:port] list into an addr array. Use the default
1919 * monitor port if a port isn't specified.
1920 */
ceph_parse_ips(const char * c,const char * end,struct ceph_entity_addr * addr,int max_count,int * count)1921 int ceph_parse_ips(const char *c, const char *end,
1922 struct ceph_entity_addr *addr,
1923 int max_count, int *count)
1924 {
1925 int i, ret = -EINVAL;
1926 const char *p = c;
1927
1928 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1929 for (i = 0; i < max_count; i++) {
1930 const char *ipend;
1931 struct sockaddr_storage *ss = &addr[i].in_addr;
1932 int port;
1933 char delim = ',';
1934
1935 if (*p == '[') {
1936 delim = ']';
1937 p++;
1938 }
1939
1940 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1941 if (ret)
1942 goto bad;
1943 ret = -EINVAL;
1944
1945 p = ipend;
1946
1947 if (delim == ']') {
1948 if (*p != ']') {
1949 dout("missing matching ']'\n");
1950 goto bad;
1951 }
1952 p++;
1953 }
1954
1955 /* port? */
1956 if (p < end && *p == ':') {
1957 port = 0;
1958 p++;
1959 while (p < end && *p >= '0' && *p <= '9') {
1960 port = (port * 10) + (*p - '0');
1961 p++;
1962 }
1963 if (port == 0)
1964 port = CEPH_MON_PORT;
1965 else if (port > 65535)
1966 goto bad;
1967 } else {
1968 port = CEPH_MON_PORT;
1969 }
1970
1971 addr_set_port(ss, port);
1972
1973 dout("parse_ips got %s\n", ceph_pr_addr(ss));
1974
1975 if (p == end)
1976 break;
1977 if (*p != ',')
1978 goto bad;
1979 p++;
1980 }
1981
1982 if (p != end)
1983 goto bad;
1984
1985 if (count)
1986 *count = i + 1;
1987 return 0;
1988
1989 bad:
1990 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
1991 return ret;
1992 }
1993 EXPORT_SYMBOL(ceph_parse_ips);
1994
process_banner(struct ceph_connection * con)1995 static int process_banner(struct ceph_connection *con)
1996 {
1997 dout("process_banner on %p\n", con);
1998
1999 if (verify_hello(con) < 0)
2000 return -1;
2001
2002 ceph_decode_addr(&con->actual_peer_addr);
2003 ceph_decode_addr(&con->peer_addr_for_me);
2004
2005 /*
2006 * Make sure the other end is who we wanted. note that the other
2007 * end may not yet know their ip address, so if it's 0.0.0.0, give
2008 * them the benefit of the doubt.
2009 */
2010 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
2011 sizeof(con->peer_addr)) != 0 &&
2012 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
2013 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
2014 pr_warn("wrong peer, want %s/%d, got %s/%d\n",
2015 ceph_pr_addr(&con->peer_addr.in_addr),
2016 (int)le32_to_cpu(con->peer_addr.nonce),
2017 ceph_pr_addr(&con->actual_peer_addr.in_addr),
2018 (int)le32_to_cpu(con->actual_peer_addr.nonce));
2019 con->error_msg = "wrong peer at address";
2020 return -1;
2021 }
2022
2023 /*
2024 * did we learn our address?
2025 */
2026 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
2027 int port = addr_port(&con->msgr->inst.addr.in_addr);
2028
2029 memcpy(&con->msgr->inst.addr.in_addr,
2030 &con->peer_addr_for_me.in_addr,
2031 sizeof(con->peer_addr_for_me.in_addr));
2032 addr_set_port(&con->msgr->inst.addr.in_addr, port);
2033 encode_my_addr(con->msgr);
2034 dout("process_banner learned my addr is %s\n",
2035 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
2036 }
2037
2038 return 0;
2039 }
2040
process_connect(struct ceph_connection * con)2041 static int process_connect(struct ceph_connection *con)
2042 {
2043 u64 sup_feat = from_msgr(con->msgr)->supported_features;
2044 u64 req_feat = from_msgr(con->msgr)->required_features;
2045 u64 server_feat = ceph_sanitize_features(
2046 le64_to_cpu(con->in_reply.features));
2047 int ret;
2048
2049 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
2050
2051 if (con->auth_reply_buf) {
2052 int len = le32_to_cpu(con->in_reply.authorizer_len);
2053
2054 /*
2055 * Any connection that defines ->get_authorizer()
2056 * should also define ->verify_authorizer_reply().
2057 * See get_connect_authorizer().
2058 */
2059 if (len) {
2060 ret = con->ops->verify_authorizer_reply(con, 0);
2061 if (ret < 0) {
2062 con->error_msg = "bad authorize reply";
2063 return ret;
2064 }
2065 }
2066 }
2067
2068 switch (con->in_reply.tag) {
2069 case CEPH_MSGR_TAG_FEATURES:
2070 pr_err("%s%lld %s feature set mismatch,"
2071 " my %llx < server's %llx, missing %llx\n",
2072 ENTITY_NAME(con->peer_name),
2073 ceph_pr_addr(&con->peer_addr.in_addr),
2074 sup_feat, server_feat, server_feat & ~sup_feat);
2075 con->error_msg = "missing required protocol features";
2076 reset_connection(con);
2077 return -1;
2078
2079 case CEPH_MSGR_TAG_BADPROTOVER:
2080 pr_err("%s%lld %s protocol version mismatch,"
2081 " my %d != server's %d\n",
2082 ENTITY_NAME(con->peer_name),
2083 ceph_pr_addr(&con->peer_addr.in_addr),
2084 le32_to_cpu(con->out_connect.protocol_version),
2085 le32_to_cpu(con->in_reply.protocol_version));
2086 con->error_msg = "protocol version mismatch";
2087 reset_connection(con);
2088 return -1;
2089
2090 case CEPH_MSGR_TAG_BADAUTHORIZER:
2091 con->auth_retry++;
2092 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
2093 con->auth_retry);
2094 if (con->auth_retry == 2) {
2095 con->error_msg = "connect authorization failure";
2096 return -1;
2097 }
2098 con_out_kvec_reset(con);
2099 ret = prepare_write_connect(con);
2100 if (ret < 0)
2101 return ret;
2102 prepare_read_connect(con);
2103 break;
2104
2105 case CEPH_MSGR_TAG_RESETSESSION:
2106 /*
2107 * If we connected with a large connect_seq but the peer
2108 * has no record of a session with us (no connection, or
2109 * connect_seq == 0), they will send RESETSESION to indicate
2110 * that they must have reset their session, and may have
2111 * dropped messages.
2112 */
2113 dout("process_connect got RESET peer seq %u\n",
2114 le32_to_cpu(con->in_reply.connect_seq));
2115 pr_err("%s%lld %s connection reset\n",
2116 ENTITY_NAME(con->peer_name),
2117 ceph_pr_addr(&con->peer_addr.in_addr));
2118 reset_connection(con);
2119 con_out_kvec_reset(con);
2120 ret = prepare_write_connect(con);
2121 if (ret < 0)
2122 return ret;
2123 prepare_read_connect(con);
2124
2125 /* Tell ceph about it. */
2126 mutex_unlock(&con->mutex);
2127 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
2128 if (con->ops->peer_reset)
2129 con->ops->peer_reset(con);
2130 mutex_lock(&con->mutex);
2131 if (con->state != CON_STATE_NEGOTIATING)
2132 return -EAGAIN;
2133 break;
2134
2135 case CEPH_MSGR_TAG_RETRY_SESSION:
2136 /*
2137 * If we sent a smaller connect_seq than the peer has, try
2138 * again with a larger value.
2139 */
2140 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
2141 le32_to_cpu(con->out_connect.connect_seq),
2142 le32_to_cpu(con->in_reply.connect_seq));
2143 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
2144 con_out_kvec_reset(con);
2145 ret = prepare_write_connect(con);
2146 if (ret < 0)
2147 return ret;
2148 prepare_read_connect(con);
2149 break;
2150
2151 case CEPH_MSGR_TAG_RETRY_GLOBAL:
2152 /*
2153 * If we sent a smaller global_seq than the peer has, try
2154 * again with a larger value.
2155 */
2156 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
2157 con->peer_global_seq,
2158 le32_to_cpu(con->in_reply.global_seq));
2159 get_global_seq(con->msgr,
2160 le32_to_cpu(con->in_reply.global_seq));
2161 con_out_kvec_reset(con);
2162 ret = prepare_write_connect(con);
2163 if (ret < 0)
2164 return ret;
2165 prepare_read_connect(con);
2166 break;
2167
2168 case CEPH_MSGR_TAG_SEQ:
2169 case CEPH_MSGR_TAG_READY:
2170 if (req_feat & ~server_feat) {
2171 pr_err("%s%lld %s protocol feature mismatch,"
2172 " my required %llx > server's %llx, need %llx\n",
2173 ENTITY_NAME(con->peer_name),
2174 ceph_pr_addr(&con->peer_addr.in_addr),
2175 req_feat, server_feat, req_feat & ~server_feat);
2176 con->error_msg = "missing required protocol features";
2177 reset_connection(con);
2178 return -1;
2179 }
2180
2181 WARN_ON(con->state != CON_STATE_NEGOTIATING);
2182 con->state = CON_STATE_OPEN;
2183 con->auth_retry = 0; /* we authenticated; clear flag */
2184 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
2185 con->connect_seq++;
2186 con->peer_features = server_feat;
2187 dout("process_connect got READY gseq %d cseq %d (%d)\n",
2188 con->peer_global_seq,
2189 le32_to_cpu(con->in_reply.connect_seq),
2190 con->connect_seq);
2191 WARN_ON(con->connect_seq !=
2192 le32_to_cpu(con->in_reply.connect_seq));
2193
2194 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
2195 con_flag_set(con, CON_FLAG_LOSSYTX);
2196
2197 con->delay = 0; /* reset backoff memory */
2198
2199 if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
2200 prepare_write_seq(con);
2201 prepare_read_seq(con);
2202 } else {
2203 prepare_read_tag(con);
2204 }
2205 break;
2206
2207 case CEPH_MSGR_TAG_WAIT:
2208 /*
2209 * If there is a connection race (we are opening
2210 * connections to each other), one of us may just have
2211 * to WAIT. This shouldn't happen if we are the
2212 * client.
2213 */
2214 con->error_msg = "protocol error, got WAIT as client";
2215 return -1;
2216
2217 default:
2218 con->error_msg = "protocol error, garbage tag during connect";
2219 return -1;
2220 }
2221 return 0;
2222 }
2223
2224
2225 /*
2226 * read (part of) an ack
2227 */
read_partial_ack(struct ceph_connection * con)2228 static int read_partial_ack(struct ceph_connection *con)
2229 {
2230 int size = sizeof (con->in_temp_ack);
2231 int end = size;
2232
2233 return read_partial(con, end, size, &con->in_temp_ack);
2234 }
2235
2236 /*
2237 * We can finally discard anything that's been acked.
2238 */
process_ack(struct ceph_connection * con)2239 static void process_ack(struct ceph_connection *con)
2240 {
2241 struct ceph_msg *m;
2242 u64 ack = le64_to_cpu(con->in_temp_ack);
2243 u64 seq;
2244
2245 while (!list_empty(&con->out_sent)) {
2246 m = list_first_entry(&con->out_sent, struct ceph_msg,
2247 list_head);
2248 seq = le64_to_cpu(m->hdr.seq);
2249 if (seq > ack)
2250 break;
2251 dout("got ack for seq %llu type %d at %p\n", seq,
2252 le16_to_cpu(m->hdr.type), m);
2253 m->ack_stamp = jiffies;
2254 ceph_msg_remove(m);
2255 }
2256 prepare_read_tag(con);
2257 }
2258
2259
read_partial_message_section(struct ceph_connection * con,struct kvec * section,unsigned int sec_len,u32 * crc)2260 static int read_partial_message_section(struct ceph_connection *con,
2261 struct kvec *section,
2262 unsigned int sec_len, u32 *crc)
2263 {
2264 int ret, left;
2265
2266 BUG_ON(!section);
2267
2268 while (section->iov_len < sec_len) {
2269 BUG_ON(section->iov_base == NULL);
2270 left = sec_len - section->iov_len;
2271 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
2272 section->iov_len, left);
2273 if (ret <= 0)
2274 return ret;
2275 section->iov_len += ret;
2276 }
2277 if (section->iov_len == sec_len)
2278 *crc = crc32c(0, section->iov_base, section->iov_len);
2279
2280 return 1;
2281 }
2282
read_partial_msg_data(struct ceph_connection * con)2283 static int read_partial_msg_data(struct ceph_connection *con)
2284 {
2285 struct ceph_msg *msg = con->in_msg;
2286 struct ceph_msg_data_cursor *cursor = &msg->cursor;
2287 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
2288 struct page *page;
2289 size_t page_offset;
2290 size_t length;
2291 u32 crc = 0;
2292 int ret;
2293
2294 BUG_ON(!msg);
2295 if (list_empty(&msg->data))
2296 return -EIO;
2297
2298 if (do_datacrc)
2299 crc = con->in_data_crc;
2300 while (cursor->resid) {
2301 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
2302 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
2303 if (ret <= 0) {
2304 if (do_datacrc)
2305 con->in_data_crc = crc;
2306
2307 return ret;
2308 }
2309
2310 if (do_datacrc)
2311 crc = ceph_crc32c_page(crc, page, page_offset, ret);
2312 (void) ceph_msg_data_advance(cursor, (size_t)ret);
2313 }
2314 if (do_datacrc)
2315 con->in_data_crc = crc;
2316
2317 return 1; /* must return > 0 to indicate success */
2318 }
2319
2320 /*
2321 * read (part of) a message.
2322 */
2323 static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
2324
read_partial_message(struct ceph_connection * con)2325 static int read_partial_message(struct ceph_connection *con)
2326 {
2327 struct ceph_msg *m = con->in_msg;
2328 int size;
2329 int end;
2330 int ret;
2331 unsigned int front_len, middle_len, data_len;
2332 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
2333 bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
2334 u64 seq;
2335 u32 crc;
2336
2337 dout("read_partial_message con %p msg %p\n", con, m);
2338
2339 /* header */
2340 size = sizeof (con->in_hdr);
2341 end = size;
2342 ret = read_partial(con, end, size, &con->in_hdr);
2343 if (ret <= 0)
2344 return ret;
2345
2346 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
2347 if (cpu_to_le32(crc) != con->in_hdr.crc) {
2348 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
2349 crc, con->in_hdr.crc);
2350 return -EBADMSG;
2351 }
2352
2353 front_len = le32_to_cpu(con->in_hdr.front_len);
2354 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
2355 return -EIO;
2356 middle_len = le32_to_cpu(con->in_hdr.middle_len);
2357 if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
2358 return -EIO;
2359 data_len = le32_to_cpu(con->in_hdr.data_len);
2360 if (data_len > CEPH_MSG_MAX_DATA_LEN)
2361 return -EIO;
2362
2363 /* verify seq# */
2364 seq = le64_to_cpu(con->in_hdr.seq);
2365 if ((s64)seq - (s64)con->in_seq < 1) {
2366 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
2367 ENTITY_NAME(con->peer_name),
2368 ceph_pr_addr(&con->peer_addr.in_addr),
2369 seq, con->in_seq + 1);
2370 con->in_base_pos = -front_len - middle_len - data_len -
2371 sizeof_footer(con);
2372 con->in_tag = CEPH_MSGR_TAG_READY;
2373 return 1;
2374 } else if ((s64)seq - (s64)con->in_seq > 1) {
2375 pr_err("read_partial_message bad seq %lld expected %lld\n",
2376 seq, con->in_seq + 1);
2377 con->error_msg = "bad message sequence # for incoming message";
2378 return -EBADE;
2379 }
2380
2381 /* allocate message? */
2382 if (!con->in_msg) {
2383 int skip = 0;
2384
2385 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
2386 front_len, data_len);
2387 ret = ceph_con_in_msg_alloc(con, &skip);
2388 if (ret < 0)
2389 return ret;
2390
2391 BUG_ON(!con->in_msg ^ skip);
2392 if (skip) {
2393 /* skip this message */
2394 dout("alloc_msg said skip message\n");
2395 con->in_base_pos = -front_len - middle_len - data_len -
2396 sizeof_footer(con);
2397 con->in_tag = CEPH_MSGR_TAG_READY;
2398 con->in_seq++;
2399 return 1;
2400 }
2401
2402 BUG_ON(!con->in_msg);
2403 BUG_ON(con->in_msg->con != con);
2404 m = con->in_msg;
2405 m->front.iov_len = 0; /* haven't read it yet */
2406 if (m->middle)
2407 m->middle->vec.iov_len = 0;
2408
2409 /* prepare for data payload, if any */
2410
2411 if (data_len)
2412 prepare_message_data(con->in_msg, data_len);
2413 }
2414
2415 /* front */
2416 ret = read_partial_message_section(con, &m->front, front_len,
2417 &con->in_front_crc);
2418 if (ret <= 0)
2419 return ret;
2420
2421 /* middle */
2422 if (m->middle) {
2423 ret = read_partial_message_section(con, &m->middle->vec,
2424 middle_len,
2425 &con->in_middle_crc);
2426 if (ret <= 0)
2427 return ret;
2428 }
2429
2430 /* (page) data */
2431 if (data_len) {
2432 ret = read_partial_msg_data(con);
2433 if (ret <= 0)
2434 return ret;
2435 }
2436
2437 /* footer */
2438 if (need_sign)
2439 size = sizeof(m->footer);
2440 else
2441 size = sizeof(m->old_footer);
2442
2443 end += size;
2444 ret = read_partial(con, end, size, &m->footer);
2445 if (ret <= 0)
2446 return ret;
2447
2448 if (!need_sign) {
2449 m->footer.flags = m->old_footer.flags;
2450 m->footer.sig = 0;
2451 }
2452
2453 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
2454 m, front_len, m->footer.front_crc, middle_len,
2455 m->footer.middle_crc, data_len, m->footer.data_crc);
2456
2457 /* crc ok? */
2458 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
2459 pr_err("read_partial_message %p front crc %u != exp. %u\n",
2460 m, con->in_front_crc, m->footer.front_crc);
2461 return -EBADMSG;
2462 }
2463 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
2464 pr_err("read_partial_message %p middle crc %u != exp %u\n",
2465 m, con->in_middle_crc, m->footer.middle_crc);
2466 return -EBADMSG;
2467 }
2468 if (do_datacrc &&
2469 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
2470 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
2471 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
2472 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
2473 return -EBADMSG;
2474 }
2475
2476 if (need_sign && con->ops->check_message_signature &&
2477 con->ops->check_message_signature(m)) {
2478 pr_err("read_partial_message %p signature check failed\n", m);
2479 return -EBADMSG;
2480 }
2481
2482 return 1; /* done! */
2483 }
2484
2485 /*
2486 * Process message. This happens in the worker thread. The callback should
2487 * be careful not to do anything that waits on other incoming messages or it
2488 * may deadlock.
2489 */
process_message(struct ceph_connection * con)2490 static void process_message(struct ceph_connection *con)
2491 {
2492 struct ceph_msg *msg = con->in_msg;
2493
2494 BUG_ON(con->in_msg->con != con);
2495 con->in_msg = NULL;
2496
2497 /* if first message, set peer_name */
2498 if (con->peer_name.type == 0)
2499 con->peer_name = msg->hdr.src;
2500
2501 con->in_seq++;
2502 mutex_unlock(&con->mutex);
2503
2504 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
2505 msg, le64_to_cpu(msg->hdr.seq),
2506 ENTITY_NAME(msg->hdr.src),
2507 le16_to_cpu(msg->hdr.type),
2508 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2509 le32_to_cpu(msg->hdr.front_len),
2510 le32_to_cpu(msg->hdr.data_len),
2511 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
2512 con->ops->dispatch(con, msg);
2513
2514 mutex_lock(&con->mutex);
2515 }
2516
read_keepalive_ack(struct ceph_connection * con)2517 static int read_keepalive_ack(struct ceph_connection *con)
2518 {
2519 struct ceph_timespec ceph_ts;
2520 size_t size = sizeof(ceph_ts);
2521 int ret = read_partial(con, size, size, &ceph_ts);
2522 if (ret <= 0)
2523 return ret;
2524 ceph_decode_timespec(&con->last_keepalive_ack, &ceph_ts);
2525 prepare_read_tag(con);
2526 return 1;
2527 }
2528
2529 /*
2530 * Write something to the socket. Called in a worker thread when the
2531 * socket appears to be writeable and we have something ready to send.
2532 */
try_write(struct ceph_connection * con)2533 static int try_write(struct ceph_connection *con)
2534 {
2535 int ret = 1;
2536
2537 dout("try_write start %p state %lu\n", con, con->state);
2538 if (con->state != CON_STATE_PREOPEN &&
2539 con->state != CON_STATE_CONNECTING &&
2540 con->state != CON_STATE_NEGOTIATING &&
2541 con->state != CON_STATE_OPEN)
2542 return 0;
2543
2544 more:
2545 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
2546
2547 /* open the socket first? */
2548 if (con->state == CON_STATE_PREOPEN) {
2549 BUG_ON(con->sock);
2550 con->state = CON_STATE_CONNECTING;
2551
2552 con_out_kvec_reset(con);
2553 prepare_write_banner(con);
2554 prepare_read_banner(con);
2555
2556 BUG_ON(con->in_msg);
2557 con->in_tag = CEPH_MSGR_TAG_READY;
2558 dout("try_write initiating connect on %p new state %lu\n",
2559 con, con->state);
2560 ret = ceph_tcp_connect(con);
2561 if (ret < 0) {
2562 con->error_msg = "connect error";
2563 goto out;
2564 }
2565 }
2566
2567 more_kvec:
2568 BUG_ON(!con->sock);
2569
2570 /* kvec data queued? */
2571 if (con->out_kvec_left) {
2572 ret = write_partial_kvec(con);
2573 if (ret <= 0)
2574 goto out;
2575 }
2576 if (con->out_skip) {
2577 ret = write_partial_skip(con);
2578 if (ret <= 0)
2579 goto out;
2580 }
2581
2582 /* msg pages? */
2583 if (con->out_msg) {
2584 if (con->out_msg_done) {
2585 ceph_msg_put(con->out_msg);
2586 con->out_msg = NULL; /* we're done with this one */
2587 goto do_next;
2588 }
2589
2590 ret = write_partial_message_data(con);
2591 if (ret == 1)
2592 goto more_kvec; /* we need to send the footer, too! */
2593 if (ret == 0)
2594 goto out;
2595 if (ret < 0) {
2596 dout("try_write write_partial_message_data err %d\n",
2597 ret);
2598 goto out;
2599 }
2600 }
2601
2602 do_next:
2603 if (con->state == CON_STATE_OPEN) {
2604 if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
2605 prepare_write_keepalive(con);
2606 goto more;
2607 }
2608 /* is anything else pending? */
2609 if (!list_empty(&con->out_queue)) {
2610 prepare_write_message(con);
2611 goto more;
2612 }
2613 if (con->in_seq > con->in_seq_acked) {
2614 prepare_write_ack(con);
2615 goto more;
2616 }
2617 }
2618
2619 /* Nothing to do! */
2620 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
2621 dout("try_write nothing else to write.\n");
2622 ret = 0;
2623 out:
2624 dout("try_write done on %p ret %d\n", con, ret);
2625 return ret;
2626 }
2627
2628
2629
2630 /*
2631 * Read what we can from the socket.
2632 */
try_read(struct ceph_connection * con)2633 static int try_read(struct ceph_connection *con)
2634 {
2635 int ret = -1;
2636
2637 more:
2638 dout("try_read start on %p state %lu\n", con, con->state);
2639 if (con->state != CON_STATE_CONNECTING &&
2640 con->state != CON_STATE_NEGOTIATING &&
2641 con->state != CON_STATE_OPEN)
2642 return 0;
2643
2644 BUG_ON(!con->sock);
2645
2646 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2647 con->in_base_pos);
2648
2649 if (con->state == CON_STATE_CONNECTING) {
2650 dout("try_read connecting\n");
2651 ret = read_partial_banner(con);
2652 if (ret <= 0)
2653 goto out;
2654 ret = process_banner(con);
2655 if (ret < 0)
2656 goto out;
2657
2658 con->state = CON_STATE_NEGOTIATING;
2659
2660 /*
2661 * Received banner is good, exchange connection info.
2662 * Do not reset out_kvec, as sending our banner raced
2663 * with receiving peer banner after connect completed.
2664 */
2665 ret = prepare_write_connect(con);
2666 if (ret < 0)
2667 goto out;
2668 prepare_read_connect(con);
2669
2670 /* Send connection info before awaiting response */
2671 goto out;
2672 }
2673
2674 if (con->state == CON_STATE_NEGOTIATING) {
2675 dout("try_read negotiating\n");
2676 ret = read_partial_connect(con);
2677 if (ret <= 0)
2678 goto out;
2679 ret = process_connect(con);
2680 if (ret < 0)
2681 goto out;
2682 goto more;
2683 }
2684
2685 WARN_ON(con->state != CON_STATE_OPEN);
2686
2687 if (con->in_base_pos < 0) {
2688 /*
2689 * skipping + discarding content.
2690 *
2691 * FIXME: there must be a better way to do this!
2692 */
2693 static char buf[SKIP_BUF_SIZE];
2694 int skip = min((int) sizeof (buf), -con->in_base_pos);
2695
2696 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2697 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2698 if (ret <= 0)
2699 goto out;
2700 con->in_base_pos += ret;
2701 if (con->in_base_pos)
2702 goto more;
2703 }
2704 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2705 /*
2706 * what's next?
2707 */
2708 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2709 if (ret <= 0)
2710 goto out;
2711 dout("try_read got tag %d\n", (int)con->in_tag);
2712 switch (con->in_tag) {
2713 case CEPH_MSGR_TAG_MSG:
2714 prepare_read_message(con);
2715 break;
2716 case CEPH_MSGR_TAG_ACK:
2717 prepare_read_ack(con);
2718 break;
2719 case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
2720 prepare_read_keepalive_ack(con);
2721 break;
2722 case CEPH_MSGR_TAG_CLOSE:
2723 con_close_socket(con);
2724 con->state = CON_STATE_CLOSED;
2725 goto out;
2726 default:
2727 goto bad_tag;
2728 }
2729 }
2730 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2731 ret = read_partial_message(con);
2732 if (ret <= 0) {
2733 switch (ret) {
2734 case -EBADMSG:
2735 con->error_msg = "bad crc/signature";
2736 /* fall through */
2737 case -EBADE:
2738 ret = -EIO;
2739 break;
2740 case -EIO:
2741 con->error_msg = "io error";
2742 break;
2743 }
2744 goto out;
2745 }
2746 if (con->in_tag == CEPH_MSGR_TAG_READY)
2747 goto more;
2748 process_message(con);
2749 if (con->state == CON_STATE_OPEN)
2750 prepare_read_tag(con);
2751 goto more;
2752 }
2753 if (con->in_tag == CEPH_MSGR_TAG_ACK ||
2754 con->in_tag == CEPH_MSGR_TAG_SEQ) {
2755 /*
2756 * the final handshake seq exchange is semantically
2757 * equivalent to an ACK
2758 */
2759 ret = read_partial_ack(con);
2760 if (ret <= 0)
2761 goto out;
2762 process_ack(con);
2763 goto more;
2764 }
2765 if (con->in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
2766 ret = read_keepalive_ack(con);
2767 if (ret <= 0)
2768 goto out;
2769 goto more;
2770 }
2771
2772 out:
2773 dout("try_read done on %p ret %d\n", con, ret);
2774 return ret;
2775
2776 bad_tag:
2777 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2778 con->error_msg = "protocol error, garbage tag";
2779 ret = -1;
2780 goto out;
2781 }
2782
2783
2784 /*
2785 * Atomically queue work on a connection after the specified delay.
2786 * Bump @con reference to avoid races with connection teardown.
2787 * Returns 0 if work was queued, or an error code otherwise.
2788 */
queue_con_delay(struct ceph_connection * con,unsigned long delay)2789 static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
2790 {
2791 if (!con->ops->get(con)) {
2792 dout("%s %p ref count 0\n", __func__, con);
2793 return -ENOENT;
2794 }
2795
2796 if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
2797 dout("%s %p - already queued\n", __func__, con);
2798 con->ops->put(con);
2799 return -EBUSY;
2800 }
2801
2802 dout("%s %p %lu\n", __func__, con, delay);
2803 return 0;
2804 }
2805
queue_con(struct ceph_connection * con)2806 static void queue_con(struct ceph_connection *con)
2807 {
2808 (void) queue_con_delay(con, 0);
2809 }
2810
cancel_con(struct ceph_connection * con)2811 static void cancel_con(struct ceph_connection *con)
2812 {
2813 if (cancel_delayed_work(&con->work)) {
2814 dout("%s %p\n", __func__, con);
2815 con->ops->put(con);
2816 }
2817 }
2818
con_sock_closed(struct ceph_connection * con)2819 static bool con_sock_closed(struct ceph_connection *con)
2820 {
2821 if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
2822 return false;
2823
2824 #define CASE(x) \
2825 case CON_STATE_ ## x: \
2826 con->error_msg = "socket closed (con state " #x ")"; \
2827 break;
2828
2829 switch (con->state) {
2830 CASE(CLOSED);
2831 CASE(PREOPEN);
2832 CASE(CONNECTING);
2833 CASE(NEGOTIATING);
2834 CASE(OPEN);
2835 CASE(STANDBY);
2836 default:
2837 pr_warn("%s con %p unrecognized state %lu\n",
2838 __func__, con, con->state);
2839 con->error_msg = "unrecognized con state";
2840 BUG();
2841 break;
2842 }
2843 #undef CASE
2844
2845 return true;
2846 }
2847
con_backoff(struct ceph_connection * con)2848 static bool con_backoff(struct ceph_connection *con)
2849 {
2850 int ret;
2851
2852 if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
2853 return false;
2854
2855 ret = queue_con_delay(con, round_jiffies_relative(con->delay));
2856 if (ret) {
2857 dout("%s: con %p FAILED to back off %lu\n", __func__,
2858 con, con->delay);
2859 BUG_ON(ret == -ENOENT);
2860 con_flag_set(con, CON_FLAG_BACKOFF);
2861 }
2862
2863 return true;
2864 }
2865
2866 /* Finish fault handling; con->mutex must *not* be held here */
2867
con_fault_finish(struct ceph_connection * con)2868 static void con_fault_finish(struct ceph_connection *con)
2869 {
2870 /*
2871 * in case we faulted due to authentication, invalidate our
2872 * current tickets so that we can get new ones.
2873 */
2874 if (con->auth_retry && con->ops->invalidate_authorizer) {
2875 dout("calling invalidate_authorizer()\n");
2876 con->ops->invalidate_authorizer(con);
2877 }
2878
2879 if (con->ops->fault)
2880 con->ops->fault(con);
2881 }
2882
2883 /*
2884 * Do some work on a connection. Drop a connection ref when we're done.
2885 */
ceph_con_workfn(struct work_struct * work)2886 static void ceph_con_workfn(struct work_struct *work)
2887 {
2888 struct ceph_connection *con = container_of(work, struct ceph_connection,
2889 work.work);
2890 bool fault;
2891
2892 mutex_lock(&con->mutex);
2893 while (true) {
2894 int ret;
2895
2896 if ((fault = con_sock_closed(con))) {
2897 dout("%s: con %p SOCK_CLOSED\n", __func__, con);
2898 break;
2899 }
2900 if (con_backoff(con)) {
2901 dout("%s: con %p BACKOFF\n", __func__, con);
2902 break;
2903 }
2904 if (con->state == CON_STATE_STANDBY) {
2905 dout("%s: con %p STANDBY\n", __func__, con);
2906 break;
2907 }
2908 if (con->state == CON_STATE_CLOSED) {
2909 dout("%s: con %p CLOSED\n", __func__, con);
2910 BUG_ON(con->sock);
2911 break;
2912 }
2913 if (con->state == CON_STATE_PREOPEN) {
2914 dout("%s: con %p PREOPEN\n", __func__, con);
2915 BUG_ON(con->sock);
2916 }
2917
2918 ret = try_read(con);
2919 if (ret < 0) {
2920 if (ret == -EAGAIN)
2921 continue;
2922 if (!con->error_msg)
2923 con->error_msg = "socket error on read";
2924 fault = true;
2925 break;
2926 }
2927
2928 ret = try_write(con);
2929 if (ret < 0) {
2930 if (ret == -EAGAIN)
2931 continue;
2932 if (!con->error_msg)
2933 con->error_msg = "socket error on write";
2934 fault = true;
2935 }
2936
2937 break; /* If we make it to here, we're done */
2938 }
2939 if (fault)
2940 con_fault(con);
2941 mutex_unlock(&con->mutex);
2942
2943 if (fault)
2944 con_fault_finish(con);
2945
2946 con->ops->put(con);
2947 }
2948
2949 /*
2950 * Generic error/fault handler. A retry mechanism is used with
2951 * exponential backoff
2952 */
con_fault(struct ceph_connection * con)2953 static void con_fault(struct ceph_connection *con)
2954 {
2955 dout("fault %p state %lu to peer %s\n",
2956 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
2957
2958 pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
2959 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
2960 con->error_msg = NULL;
2961
2962 WARN_ON(con->state != CON_STATE_CONNECTING &&
2963 con->state != CON_STATE_NEGOTIATING &&
2964 con->state != CON_STATE_OPEN);
2965
2966 con_close_socket(con);
2967
2968 if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
2969 dout("fault on LOSSYTX channel, marking CLOSED\n");
2970 con->state = CON_STATE_CLOSED;
2971 return;
2972 }
2973
2974 if (con->in_msg) {
2975 BUG_ON(con->in_msg->con != con);
2976 ceph_msg_put(con->in_msg);
2977 con->in_msg = NULL;
2978 }
2979 if (con->out_msg) {
2980 BUG_ON(con->out_msg->con != con);
2981 ceph_msg_put(con->out_msg);
2982 con->out_msg = NULL;
2983 }
2984
2985 /* Requeue anything that hasn't been acked */
2986 list_splice_init(&con->out_sent, &con->out_queue);
2987
2988 /* If there are no messages queued or keepalive pending, place
2989 * the connection in a STANDBY state */
2990 if (list_empty(&con->out_queue) &&
2991 !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
2992 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
2993 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
2994 con->state = CON_STATE_STANDBY;
2995 } else {
2996 /* retry after a delay. */
2997 con->state = CON_STATE_PREOPEN;
2998 if (con->delay == 0)
2999 con->delay = BASE_DELAY_INTERVAL;
3000 else if (con->delay < MAX_DELAY_INTERVAL)
3001 con->delay *= 2;
3002 con_flag_set(con, CON_FLAG_BACKOFF);
3003 queue_con(con);
3004 }
3005 }
3006
3007
3008
3009 /*
3010 * initialize a new messenger instance
3011 */
ceph_messenger_init(struct ceph_messenger * msgr,struct ceph_entity_addr * myaddr)3012 void ceph_messenger_init(struct ceph_messenger *msgr,
3013 struct ceph_entity_addr *myaddr)
3014 {
3015 spin_lock_init(&msgr->global_seq_lock);
3016
3017 if (myaddr)
3018 msgr->inst.addr = *myaddr;
3019
3020 /* select a random nonce */
3021 msgr->inst.addr.type = 0;
3022 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
3023 encode_my_addr(msgr);
3024
3025 atomic_set(&msgr->stopping, 0);
3026 write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
3027
3028 dout("%s %p\n", __func__, msgr);
3029 }
3030 EXPORT_SYMBOL(ceph_messenger_init);
3031
ceph_messenger_fini(struct ceph_messenger * msgr)3032 void ceph_messenger_fini(struct ceph_messenger *msgr)
3033 {
3034 put_net(read_pnet(&msgr->net));
3035 }
3036 EXPORT_SYMBOL(ceph_messenger_fini);
3037
msg_con_set(struct ceph_msg * msg,struct ceph_connection * con)3038 static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con)
3039 {
3040 if (msg->con)
3041 msg->con->ops->put(msg->con);
3042
3043 msg->con = con ? con->ops->get(con) : NULL;
3044 BUG_ON(msg->con != con);
3045 }
3046
clear_standby(struct ceph_connection * con)3047 static void clear_standby(struct ceph_connection *con)
3048 {
3049 /* come back from STANDBY? */
3050 if (con->state == CON_STATE_STANDBY) {
3051 dout("clear_standby %p and ++connect_seq\n", con);
3052 con->state = CON_STATE_PREOPEN;
3053 con->connect_seq++;
3054 WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
3055 WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
3056 }
3057 }
3058
3059 /*
3060 * Queue up an outgoing message on the given connection.
3061 */
ceph_con_send(struct ceph_connection * con,struct ceph_msg * msg)3062 void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
3063 {
3064 /* set src+dst */
3065 msg->hdr.src = con->msgr->inst.name;
3066 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
3067 msg->needs_out_seq = true;
3068
3069 mutex_lock(&con->mutex);
3070
3071 if (con->state == CON_STATE_CLOSED) {
3072 dout("con_send %p closed, dropping %p\n", con, msg);
3073 ceph_msg_put(msg);
3074 mutex_unlock(&con->mutex);
3075 return;
3076 }
3077
3078 msg_con_set(msg, con);
3079
3080 BUG_ON(!list_empty(&msg->list_head));
3081 list_add_tail(&msg->list_head, &con->out_queue);
3082 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
3083 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
3084 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
3085 le32_to_cpu(msg->hdr.front_len),
3086 le32_to_cpu(msg->hdr.middle_len),
3087 le32_to_cpu(msg->hdr.data_len));
3088
3089 clear_standby(con);
3090 mutex_unlock(&con->mutex);
3091
3092 /* if there wasn't anything waiting to send before, queue
3093 * new work */
3094 if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
3095 queue_con(con);
3096 }
3097 EXPORT_SYMBOL(ceph_con_send);
3098
3099 /*
3100 * Revoke a message that was previously queued for send
3101 */
ceph_msg_revoke(struct ceph_msg * msg)3102 void ceph_msg_revoke(struct ceph_msg *msg)
3103 {
3104 struct ceph_connection *con = msg->con;
3105
3106 if (!con) {
3107 dout("%s msg %p null con\n", __func__, msg);
3108 return; /* Message not in our possession */
3109 }
3110
3111 mutex_lock(&con->mutex);
3112 if (!list_empty(&msg->list_head)) {
3113 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
3114 list_del_init(&msg->list_head);
3115 msg->hdr.seq = 0;
3116
3117 ceph_msg_put(msg);
3118 }
3119 if (con->out_msg == msg) {
3120 BUG_ON(con->out_skip);
3121 /* footer */
3122 if (con->out_msg_done) {
3123 con->out_skip += con_out_kvec_skip(con);
3124 } else {
3125 BUG_ON(!msg->data_length);
3126 if (con->peer_features & CEPH_FEATURE_MSG_AUTH)
3127 con->out_skip += sizeof(msg->footer);
3128 else
3129 con->out_skip += sizeof(msg->old_footer);
3130 }
3131 /* data, middle, front */
3132 if (msg->data_length)
3133 con->out_skip += msg->cursor.total_resid;
3134 if (msg->middle)
3135 con->out_skip += con_out_kvec_skip(con);
3136 con->out_skip += con_out_kvec_skip(con);
3137
3138 dout("%s %p msg %p - was sending, will write %d skip %d\n",
3139 __func__, con, msg, con->out_kvec_bytes, con->out_skip);
3140 msg->hdr.seq = 0;
3141 con->out_msg = NULL;
3142 ceph_msg_put(msg);
3143 }
3144
3145 mutex_unlock(&con->mutex);
3146 }
3147
3148 /*
3149 * Revoke a message that we may be reading data into
3150 */
ceph_msg_revoke_incoming(struct ceph_msg * msg)3151 void ceph_msg_revoke_incoming(struct ceph_msg *msg)
3152 {
3153 struct ceph_connection *con = msg->con;
3154
3155 if (!con) {
3156 dout("%s msg %p null con\n", __func__, msg);
3157 return; /* Message not in our possession */
3158 }
3159
3160 mutex_lock(&con->mutex);
3161 if (con->in_msg == msg) {
3162 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
3163 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
3164 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
3165
3166 /* skip rest of message */
3167 dout("%s %p msg %p revoked\n", __func__, con, msg);
3168 con->in_base_pos = con->in_base_pos -
3169 sizeof(struct ceph_msg_header) -
3170 front_len -
3171 middle_len -
3172 data_len -
3173 sizeof(struct ceph_msg_footer);
3174 ceph_msg_put(con->in_msg);
3175 con->in_msg = NULL;
3176 con->in_tag = CEPH_MSGR_TAG_READY;
3177 con->in_seq++;
3178 } else {
3179 dout("%s %p in_msg %p msg %p no-op\n",
3180 __func__, con, con->in_msg, msg);
3181 }
3182 mutex_unlock(&con->mutex);
3183 }
3184
3185 /*
3186 * Queue a keepalive byte to ensure the tcp connection is alive.
3187 */
ceph_con_keepalive(struct ceph_connection * con)3188 void ceph_con_keepalive(struct ceph_connection *con)
3189 {
3190 dout("con_keepalive %p\n", con);
3191 mutex_lock(&con->mutex);
3192 clear_standby(con);
3193 con_flag_set(con, CON_FLAG_KEEPALIVE_PENDING);
3194 mutex_unlock(&con->mutex);
3195
3196 if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
3197 queue_con(con);
3198 }
3199 EXPORT_SYMBOL(ceph_con_keepalive);
3200
ceph_con_keepalive_expired(struct ceph_connection * con,unsigned long interval)3201 bool ceph_con_keepalive_expired(struct ceph_connection *con,
3202 unsigned long interval)
3203 {
3204 if (interval > 0 &&
3205 (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) {
3206 struct timespec now = CURRENT_TIME;
3207 struct timespec ts;
3208 jiffies_to_timespec(interval, &ts);
3209 ts = timespec_add(con->last_keepalive_ack, ts);
3210 return timespec_compare(&now, &ts) >= 0;
3211 }
3212 return false;
3213 }
3214
ceph_msg_data_create(enum ceph_msg_data_type type)3215 static struct ceph_msg_data *ceph_msg_data_create(enum ceph_msg_data_type type)
3216 {
3217 struct ceph_msg_data *data;
3218
3219 if (WARN_ON(!ceph_msg_data_type_valid(type)))
3220 return NULL;
3221
3222 data = kmem_cache_zalloc(ceph_msg_data_cache, GFP_NOFS);
3223 if (data)
3224 data->type = type;
3225 INIT_LIST_HEAD(&data->links);
3226
3227 return data;
3228 }
3229
ceph_msg_data_destroy(struct ceph_msg_data * data)3230 static void ceph_msg_data_destroy(struct ceph_msg_data *data)
3231 {
3232 if (!data)
3233 return;
3234
3235 WARN_ON(!list_empty(&data->links));
3236 if (data->type == CEPH_MSG_DATA_PAGELIST)
3237 ceph_pagelist_release(data->pagelist);
3238 kmem_cache_free(ceph_msg_data_cache, data);
3239 }
3240
ceph_msg_data_add_pages(struct ceph_msg * msg,struct page ** pages,size_t length,size_t alignment)3241 void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
3242 size_t length, size_t alignment)
3243 {
3244 struct ceph_msg_data *data;
3245
3246 BUG_ON(!pages);
3247 BUG_ON(!length);
3248
3249 data = ceph_msg_data_create(CEPH_MSG_DATA_PAGES);
3250 BUG_ON(!data);
3251 data->pages = pages;
3252 data->length = length;
3253 data->alignment = alignment & ~PAGE_MASK;
3254
3255 list_add_tail(&data->links, &msg->data);
3256 msg->data_length += length;
3257 }
3258 EXPORT_SYMBOL(ceph_msg_data_add_pages);
3259
ceph_msg_data_add_pagelist(struct ceph_msg * msg,struct ceph_pagelist * pagelist)3260 void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
3261 struct ceph_pagelist *pagelist)
3262 {
3263 struct ceph_msg_data *data;
3264
3265 BUG_ON(!pagelist);
3266 BUG_ON(!pagelist->length);
3267
3268 data = ceph_msg_data_create(CEPH_MSG_DATA_PAGELIST);
3269 BUG_ON(!data);
3270 data->pagelist = pagelist;
3271
3272 list_add_tail(&data->links, &msg->data);
3273 msg->data_length += pagelist->length;
3274 }
3275 EXPORT_SYMBOL(ceph_msg_data_add_pagelist);
3276
3277 #ifdef CONFIG_BLOCK
ceph_msg_data_add_bio(struct ceph_msg * msg,struct bio * bio,size_t length)3278 void ceph_msg_data_add_bio(struct ceph_msg *msg, struct bio *bio,
3279 size_t length)
3280 {
3281 struct ceph_msg_data *data;
3282
3283 BUG_ON(!bio);
3284
3285 data = ceph_msg_data_create(CEPH_MSG_DATA_BIO);
3286 BUG_ON(!data);
3287 data->bio = bio;
3288 data->bio_length = length;
3289
3290 list_add_tail(&data->links, &msg->data);
3291 msg->data_length += length;
3292 }
3293 EXPORT_SYMBOL(ceph_msg_data_add_bio);
3294 #endif /* CONFIG_BLOCK */
3295
3296 /*
3297 * construct a new message with given type, size
3298 * the new msg has a ref count of 1.
3299 */
ceph_msg_new(int type,int front_len,gfp_t flags,bool can_fail)3300 struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
3301 bool can_fail)
3302 {
3303 struct ceph_msg *m;
3304
3305 m = kmem_cache_zalloc(ceph_msg_cache, flags);
3306 if (m == NULL)
3307 goto out;
3308
3309 m->hdr.type = cpu_to_le16(type);
3310 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
3311 m->hdr.front_len = cpu_to_le32(front_len);
3312
3313 INIT_LIST_HEAD(&m->list_head);
3314 kref_init(&m->kref);
3315 INIT_LIST_HEAD(&m->data);
3316
3317 /* front */
3318 if (front_len) {
3319 m->front.iov_base = ceph_kvmalloc(front_len, flags);
3320 if (m->front.iov_base == NULL) {
3321 dout("ceph_msg_new can't allocate %d bytes\n",
3322 front_len);
3323 goto out2;
3324 }
3325 } else {
3326 m->front.iov_base = NULL;
3327 }
3328 m->front_alloc_len = m->front.iov_len = front_len;
3329
3330 dout("ceph_msg_new %p front %d\n", m, front_len);
3331 return m;
3332
3333 out2:
3334 ceph_msg_put(m);
3335 out:
3336 if (!can_fail) {
3337 pr_err("msg_new can't create type %d front %d\n", type,
3338 front_len);
3339 WARN_ON(1);
3340 } else {
3341 dout("msg_new can't create type %d front %d\n", type,
3342 front_len);
3343 }
3344 return NULL;
3345 }
3346 EXPORT_SYMBOL(ceph_msg_new);
3347
3348 /*
3349 * Allocate "middle" portion of a message, if it is needed and wasn't
3350 * allocated by alloc_msg. This allows us to read a small fixed-size
3351 * per-type header in the front and then gracefully fail (i.e.,
3352 * propagate the error to the caller based on info in the front) when
3353 * the middle is too large.
3354 */
ceph_alloc_middle(struct ceph_connection * con,struct ceph_msg * msg)3355 static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
3356 {
3357 int type = le16_to_cpu(msg->hdr.type);
3358 int middle_len = le32_to_cpu(msg->hdr.middle_len);
3359
3360 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
3361 ceph_msg_type_name(type), middle_len);
3362 BUG_ON(!middle_len);
3363 BUG_ON(msg->middle);
3364
3365 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
3366 if (!msg->middle)
3367 return -ENOMEM;
3368 return 0;
3369 }
3370
3371 /*
3372 * Allocate a message for receiving an incoming message on a
3373 * connection, and save the result in con->in_msg. Uses the
3374 * connection's private alloc_msg op if available.
3375 *
3376 * Returns 0 on success, or a negative error code.
3377 *
3378 * On success, if we set *skip = 1:
3379 * - the next message should be skipped and ignored.
3380 * - con->in_msg == NULL
3381 * or if we set *skip = 0:
3382 * - con->in_msg is non-null.
3383 * On error (ENOMEM, EAGAIN, ...),
3384 * - con->in_msg == NULL
3385 */
ceph_con_in_msg_alloc(struct ceph_connection * con,int * skip)3386 static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
3387 {
3388 struct ceph_msg_header *hdr = &con->in_hdr;
3389 int middle_len = le32_to_cpu(hdr->middle_len);
3390 struct ceph_msg *msg;
3391 int ret = 0;
3392
3393 BUG_ON(con->in_msg != NULL);
3394 BUG_ON(!con->ops->alloc_msg);
3395
3396 mutex_unlock(&con->mutex);
3397 msg = con->ops->alloc_msg(con, hdr, skip);
3398 mutex_lock(&con->mutex);
3399 if (con->state != CON_STATE_OPEN) {
3400 if (msg)
3401 ceph_msg_put(msg);
3402 return -EAGAIN;
3403 }
3404 if (msg) {
3405 BUG_ON(*skip);
3406 msg_con_set(msg, con);
3407 con->in_msg = msg;
3408 } else {
3409 /*
3410 * Null message pointer means either we should skip
3411 * this message or we couldn't allocate memory. The
3412 * former is not an error.
3413 */
3414 if (*skip)
3415 return 0;
3416
3417 con->error_msg = "error allocating memory for incoming message";
3418 return -ENOMEM;
3419 }
3420 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
3421
3422 if (middle_len && !con->in_msg->middle) {
3423 ret = ceph_alloc_middle(con, con->in_msg);
3424 if (ret < 0) {
3425 ceph_msg_put(con->in_msg);
3426 con->in_msg = NULL;
3427 }
3428 }
3429
3430 return ret;
3431 }
3432
3433
3434 /*
3435 * Free a generically kmalloc'd message.
3436 */
ceph_msg_free(struct ceph_msg * m)3437 static void ceph_msg_free(struct ceph_msg *m)
3438 {
3439 dout("%s %p\n", __func__, m);
3440 kvfree(m->front.iov_base);
3441 kmem_cache_free(ceph_msg_cache, m);
3442 }
3443
ceph_msg_release(struct kref * kref)3444 static void ceph_msg_release(struct kref *kref)
3445 {
3446 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
3447 LIST_HEAD(data);
3448 struct list_head *links;
3449 struct list_head *next;
3450
3451 dout("%s %p\n", __func__, m);
3452 WARN_ON(!list_empty(&m->list_head));
3453
3454 msg_con_set(m, NULL);
3455
3456 /* drop middle, data, if any */
3457 if (m->middle) {
3458 ceph_buffer_put(m->middle);
3459 m->middle = NULL;
3460 }
3461
3462 list_splice_init(&m->data, &data);
3463 list_for_each_safe(links, next, &data) {
3464 struct ceph_msg_data *data;
3465
3466 data = list_entry(links, struct ceph_msg_data, links);
3467 list_del_init(links);
3468 ceph_msg_data_destroy(data);
3469 }
3470 m->data_length = 0;
3471
3472 if (m->pool)
3473 ceph_msgpool_put(m->pool, m);
3474 else
3475 ceph_msg_free(m);
3476 }
3477
ceph_msg_get(struct ceph_msg * msg)3478 struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
3479 {
3480 dout("%s %p (was %d)\n", __func__, msg,
3481 atomic_read(&msg->kref.refcount));
3482 kref_get(&msg->kref);
3483 return msg;
3484 }
3485 EXPORT_SYMBOL(ceph_msg_get);
3486
ceph_msg_put(struct ceph_msg * msg)3487 void ceph_msg_put(struct ceph_msg *msg)
3488 {
3489 dout("%s %p (was %d)\n", __func__, msg,
3490 atomic_read(&msg->kref.refcount));
3491 kref_put(&msg->kref, ceph_msg_release);
3492 }
3493 EXPORT_SYMBOL(ceph_msg_put);
3494
ceph_msg_dump(struct ceph_msg * msg)3495 void ceph_msg_dump(struct ceph_msg *msg)
3496 {
3497 pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg,
3498 msg->front_alloc_len, msg->data_length);
3499 print_hex_dump(KERN_DEBUG, "header: ",
3500 DUMP_PREFIX_OFFSET, 16, 1,
3501 &msg->hdr, sizeof(msg->hdr), true);
3502 print_hex_dump(KERN_DEBUG, " front: ",
3503 DUMP_PREFIX_OFFSET, 16, 1,
3504 msg->front.iov_base, msg->front.iov_len, true);
3505 if (msg->middle)
3506 print_hex_dump(KERN_DEBUG, "middle: ",
3507 DUMP_PREFIX_OFFSET, 16, 1,
3508 msg->middle->vec.iov_base,
3509 msg->middle->vec.iov_len, true);
3510 print_hex_dump(KERN_DEBUG, "footer: ",
3511 DUMP_PREFIX_OFFSET, 16, 1,
3512 &msg->footer, sizeof(msg->footer), true);
3513 }
3514 EXPORT_SYMBOL(ceph_msg_dump);
3515