1 #include <linux/ceph/ceph_debug.h>
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>
9 #include <linux/slab.h>
10 #include <linux/socket.h>
11 #include <linux/string.h>
12 #include <linux/bio.h>
13 #include <linux/blkdev.h>
14 #include <linux/dns_resolver.h>
17 #include <linux/ceph/libceph.h>
18 #include <linux/ceph/messenger.h>
19 #include <linux/ceph/decode.h>
20 #include <linux/ceph/pagelist.h>
21 #include <linux/export.h>
24 * Ceph uses the messenger to exchange ceph_msg messages with other
25 * hosts in the system. The messenger provides ordered and reliable
26 * delivery. We tolerate TCP disconnects by reconnecting (with
27 * exponential backoff) in the case of a fault (disconnection, bad
28 * crc, protocol error). Acks allow sent messages to be discarded by
32 /* static tag bytes (protocol control messages) */
33 static char tag_msg
= CEPH_MSGR_TAG_MSG
;
34 static char tag_ack
= CEPH_MSGR_TAG_ACK
;
35 static char tag_keepalive
= CEPH_MSGR_TAG_KEEPALIVE
;
38 static struct lock_class_key socket_class
;
42 static void queue_con(struct ceph_connection
*con
);
43 static void con_work(struct work_struct
*);
44 static void ceph_fault(struct ceph_connection
*con
);
47 * nicely render a sockaddr as a string.
49 #define MAX_ADDR_STR 20
50 #define MAX_ADDR_STR_LEN 60
51 static char addr_str
[MAX_ADDR_STR
][MAX_ADDR_STR_LEN
];
52 static DEFINE_SPINLOCK(addr_str_lock
);
53 static int last_addr_str
;
55 const char *ceph_pr_addr(const struct sockaddr_storage
*ss
)
59 struct sockaddr_in
*in4
= (void *)ss
;
60 struct sockaddr_in6
*in6
= (void *)ss
;
62 spin_lock(&addr_str_lock
);
64 if (last_addr_str
== MAX_ADDR_STR
)
66 spin_unlock(&addr_str_lock
);
69 switch (ss
->ss_family
) {
71 snprintf(s
, MAX_ADDR_STR_LEN
, "%pI4:%u", &in4
->sin_addr
,
72 (unsigned int)ntohs(in4
->sin_port
));
76 snprintf(s
, MAX_ADDR_STR_LEN
, "[%pI6c]:%u", &in6
->sin6_addr
,
77 (unsigned int)ntohs(in6
->sin6_port
));
81 snprintf(s
, MAX_ADDR_STR_LEN
, "(unknown sockaddr family %d)",
87 EXPORT_SYMBOL(ceph_pr_addr
);
89 static void encode_my_addr(struct ceph_messenger
*msgr
)
91 memcpy(&msgr
->my_enc_addr
, &msgr
->inst
.addr
, sizeof(msgr
->my_enc_addr
));
92 ceph_encode_addr(&msgr
->my_enc_addr
);
96 * work queue for all reading and writing to/from the socket.
98 struct workqueue_struct
*ceph_msgr_wq
;
100 int ceph_msgr_init(void)
102 ceph_msgr_wq
= alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT
, 0);
104 pr_err("msgr_init failed to create workqueue\n");
109 EXPORT_SYMBOL(ceph_msgr_init
);
111 void ceph_msgr_exit(void)
113 destroy_workqueue(ceph_msgr_wq
);
115 EXPORT_SYMBOL(ceph_msgr_exit
);
117 void ceph_msgr_flush(void)
119 flush_workqueue(ceph_msgr_wq
);
121 EXPORT_SYMBOL(ceph_msgr_flush
);
125 * socket callback functions
128 /* data available on socket, or listen socket received a connect */
129 static void ceph_data_ready(struct sock
*sk
, int count_unused
)
131 struct ceph_connection
*con
=
132 (struct ceph_connection
*)sk
->sk_user_data
;
133 if (sk
->sk_state
!= TCP_CLOSE_WAIT
) {
134 dout("ceph_data_ready on %p state = %lu, queueing work\n",
140 /* socket has buffer space for writing */
141 static void ceph_write_space(struct sock
*sk
)
143 struct ceph_connection
*con
=
144 (struct ceph_connection
*)sk
->sk_user_data
;
146 /* only queue to workqueue if there is data we want to write. */
147 if (test_bit(WRITE_PENDING
, &con
->state
)) {
148 dout("ceph_write_space %p queueing write work\n", con
);
151 dout("ceph_write_space %p nothing to write\n", con
);
154 /* since we have our own write_space, clear the SOCK_NOSPACE flag */
155 clear_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
158 /* socket's state has changed */
159 static void ceph_state_change(struct sock
*sk
)
161 struct ceph_connection
*con
=
162 (struct ceph_connection
*)sk
->sk_user_data
;
164 dout("ceph_state_change %p state = %lu sk_state = %u\n",
165 con
, con
->state
, sk
->sk_state
);
167 if (test_bit(CLOSED
, &con
->state
))
170 switch (sk
->sk_state
) {
172 dout("ceph_state_change TCP_CLOSE\n");
174 dout("ceph_state_change TCP_CLOSE_WAIT\n");
175 if (test_and_set_bit(SOCK_CLOSED
, &con
->state
) == 0) {
176 if (test_bit(CONNECTING
, &con
->state
))
177 con
->error_msg
= "connection failed";
179 con
->error_msg
= "socket closed";
183 case TCP_ESTABLISHED
:
184 dout("ceph_state_change TCP_ESTABLISHED\n");
191 * set up socket callbacks
193 static void set_sock_callbacks(struct socket
*sock
,
194 struct ceph_connection
*con
)
196 struct sock
*sk
= sock
->sk
;
197 sk
->sk_user_data
= (void *)con
;
198 sk
->sk_data_ready
= ceph_data_ready
;
199 sk
->sk_write_space
= ceph_write_space
;
200 sk
->sk_state_change
= ceph_state_change
;
209 * initiate connection to a remote socket.
211 static struct socket
*ceph_tcp_connect(struct ceph_connection
*con
)
213 struct sockaddr_storage
*paddr
= &con
->peer_addr
.in_addr
;
218 ret
= sock_create_kern(con
->peer_addr
.in_addr
.ss_family
, SOCK_STREAM
,
223 sock
->sk
->sk_allocation
= GFP_NOFS
;
225 #ifdef CONFIG_LOCKDEP
226 lockdep_set_class(&sock
->sk
->sk_lock
, &socket_class
);
229 set_sock_callbacks(sock
, con
);
231 dout("connect %s\n", ceph_pr_addr(&con
->peer_addr
.in_addr
));
233 ret
= sock
->ops
->connect(sock
, (struct sockaddr
*)paddr
, sizeof(*paddr
),
235 if (ret
== -EINPROGRESS
) {
236 dout("connect %s EINPROGRESS sk_state = %u\n",
237 ceph_pr_addr(&con
->peer_addr
.in_addr
),
242 pr_err("connect %s error %d\n",
243 ceph_pr_addr(&con
->peer_addr
.in_addr
), ret
);
246 con
->error_msg
= "connect error";
254 static int ceph_tcp_recvmsg(struct socket
*sock
, void *buf
, size_t len
)
256 struct kvec iov
= {buf
, len
};
257 struct msghdr msg
= { .msg_flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
};
260 r
= kernel_recvmsg(sock
, &msg
, &iov
, 1, len
, msg
.msg_flags
);
267 * write something. @more is true if caller will be sending more data
270 static int ceph_tcp_sendmsg(struct socket
*sock
, struct kvec
*iov
,
271 size_t kvlen
, size_t len
, int more
)
273 struct msghdr msg
= { .msg_flags
= MSG_DONTWAIT
| MSG_NOSIGNAL
};
277 msg
.msg_flags
|= MSG_MORE
;
279 msg
.msg_flags
|= MSG_EOR
; /* superfluous, but what the hell */
281 r
= kernel_sendmsg(sock
, &msg
, iov
, kvlen
, len
);
289 * Shutdown/close the socket for the given connection.
291 static int con_close_socket(struct ceph_connection
*con
)
295 dout("con_close_socket on %p sock %p\n", con
, con
->sock
);
298 set_bit(SOCK_CLOSED
, &con
->state
);
299 rc
= con
->sock
->ops
->shutdown(con
->sock
, SHUT_RDWR
);
300 sock_release(con
->sock
);
302 clear_bit(SOCK_CLOSED
, &con
->state
);
307 * Reset a connection. Discard all incoming and outgoing messages
308 * and clear *_seq state.
310 static void ceph_msg_remove(struct ceph_msg
*msg
)
312 list_del_init(&msg
->list_head
);
315 static void ceph_msg_remove_list(struct list_head
*head
)
317 while (!list_empty(head
)) {
318 struct ceph_msg
*msg
= list_first_entry(head
, struct ceph_msg
,
320 ceph_msg_remove(msg
);
324 static void reset_connection(struct ceph_connection
*con
)
326 /* reset connection, out_queue, msg_ and connect_seq */
327 /* discard existing out_queue and msg_seq */
328 ceph_msg_remove_list(&con
->out_queue
);
329 ceph_msg_remove_list(&con
->out_sent
);
332 ceph_msg_put(con
->in_msg
);
336 con
->connect_seq
= 0;
339 ceph_msg_put(con
->out_msg
);
343 con
->in_seq_acked
= 0;
347 * mark a peer down. drop any open connections.
349 void ceph_con_close(struct ceph_connection
*con
)
351 dout("con_close %p peer %s\n", con
,
352 ceph_pr_addr(&con
->peer_addr
.in_addr
));
353 set_bit(CLOSED
, &con
->state
); /* in case there's queued work */
354 clear_bit(STANDBY
, &con
->state
); /* avoid connect_seq bump */
355 clear_bit(LOSSYTX
, &con
->state
); /* so we retry next connect */
356 clear_bit(KEEPALIVE_PENDING
, &con
->state
);
357 clear_bit(WRITE_PENDING
, &con
->state
);
358 mutex_lock(&con
->mutex
);
359 reset_connection(con
);
360 con
->peer_global_seq
= 0;
361 cancel_delayed_work(&con
->work
);
362 mutex_unlock(&con
->mutex
);
365 EXPORT_SYMBOL(ceph_con_close
);
368 * Reopen a closed connection, with a new peer address.
370 void ceph_con_open(struct ceph_connection
*con
, struct ceph_entity_addr
*addr
)
372 dout("con_open %p %s\n", con
, ceph_pr_addr(&addr
->in_addr
));
373 set_bit(OPENING
, &con
->state
);
374 clear_bit(CLOSED
, &con
->state
);
375 memcpy(&con
->peer_addr
, addr
, sizeof(*addr
));
376 con
->delay
= 0; /* reset backoff memory */
379 EXPORT_SYMBOL(ceph_con_open
);
382 * return true if this connection ever successfully opened
384 bool ceph_con_opened(struct ceph_connection
*con
)
386 return con
->connect_seq
> 0;
392 struct ceph_connection
*ceph_con_get(struct ceph_connection
*con
)
394 dout("con_get %p nref = %d -> %d\n", con
,
395 atomic_read(&con
->nref
), atomic_read(&con
->nref
) + 1);
396 if (atomic_inc_not_zero(&con
->nref
))
401 void ceph_con_put(struct ceph_connection
*con
)
403 dout("con_put %p nref = %d -> %d\n", con
,
404 atomic_read(&con
->nref
), atomic_read(&con
->nref
) - 1);
405 BUG_ON(atomic_read(&con
->nref
) == 0);
406 if (atomic_dec_and_test(&con
->nref
)) {
413 * initialize a new connection.
415 void ceph_con_init(struct ceph_messenger
*msgr
, struct ceph_connection
*con
)
417 dout("con_init %p\n", con
);
418 memset(con
, 0, sizeof(*con
));
419 atomic_set(&con
->nref
, 1);
421 mutex_init(&con
->mutex
);
422 INIT_LIST_HEAD(&con
->out_queue
);
423 INIT_LIST_HEAD(&con
->out_sent
);
424 INIT_DELAYED_WORK(&con
->work
, con_work
);
426 EXPORT_SYMBOL(ceph_con_init
);
430 * We maintain a global counter to order connection attempts. Get
431 * a unique seq greater than @gt.
433 static u32
get_global_seq(struct ceph_messenger
*msgr
, u32 gt
)
437 spin_lock(&msgr
->global_seq_lock
);
438 if (msgr
->global_seq
< gt
)
439 msgr
->global_seq
= gt
;
440 ret
= ++msgr
->global_seq
;
441 spin_unlock(&msgr
->global_seq_lock
);
447 * Prepare footer for currently outgoing message, and finish things
448 * off. Assumes out_kvec* are already valid.. we just add on to the end.
450 static void prepare_write_message_footer(struct ceph_connection
*con
, int v
)
452 struct ceph_msg
*m
= con
->out_msg
;
454 dout("prepare_write_message_footer %p\n", con
);
455 con
->out_kvec_is_msg
= true;
456 con
->out_kvec
[v
].iov_base
= &m
->footer
;
457 con
->out_kvec
[v
].iov_len
= sizeof(m
->footer
);
458 con
->out_kvec_bytes
+= sizeof(m
->footer
);
459 con
->out_kvec_left
++;
460 con
->out_more
= m
->more_to_follow
;
461 con
->out_msg_done
= true;
465 * Prepare headers for the next outgoing message.
467 static void prepare_write_message(struct ceph_connection
*con
)
472 con
->out_kvec_bytes
= 0;
473 con
->out_kvec_is_msg
= true;
474 con
->out_msg_done
= false;
476 /* Sneak an ack in there first? If we can get it into the same
477 * TCP packet that's a good thing. */
478 if (con
->in_seq
> con
->in_seq_acked
) {
479 con
->in_seq_acked
= con
->in_seq
;
480 con
->out_kvec
[v
].iov_base
= &tag_ack
;
481 con
->out_kvec
[v
++].iov_len
= 1;
482 con
->out_temp_ack
= cpu_to_le64(con
->in_seq_acked
);
483 con
->out_kvec
[v
].iov_base
= &con
->out_temp_ack
;
484 con
->out_kvec
[v
++].iov_len
= sizeof(con
->out_temp_ack
);
485 con
->out_kvec_bytes
= 1 + sizeof(con
->out_temp_ack
);
488 m
= list_first_entry(&con
->out_queue
,
489 struct ceph_msg
, list_head
);
492 /* put message on sent list */
494 list_move_tail(&m
->list_head
, &con
->out_sent
);
497 * only assign outgoing seq # if we haven't sent this message
498 * yet. if it is requeued, resend with it's original seq.
500 if (m
->needs_out_seq
) {
501 m
->hdr
.seq
= cpu_to_le64(++con
->out_seq
);
502 m
->needs_out_seq
= false;
505 dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n",
506 m
, con
->out_seq
, le16_to_cpu(m
->hdr
.type
),
507 le32_to_cpu(m
->hdr
.front_len
), le32_to_cpu(m
->hdr
.middle_len
),
508 le32_to_cpu(m
->hdr
.data_len
),
510 BUG_ON(le32_to_cpu(m
->hdr
.front_len
) != m
->front
.iov_len
);
512 /* tag + hdr + front + middle */
513 con
->out_kvec
[v
].iov_base
= &tag_msg
;
514 con
->out_kvec
[v
++].iov_len
= 1;
515 con
->out_kvec
[v
].iov_base
= &m
->hdr
;
516 con
->out_kvec
[v
++].iov_len
= sizeof(m
->hdr
);
517 con
->out_kvec
[v
++] = m
->front
;
519 con
->out_kvec
[v
++] = m
->middle
->vec
;
520 con
->out_kvec_left
= v
;
521 con
->out_kvec_bytes
+= 1 + sizeof(m
->hdr
) + m
->front
.iov_len
+
522 (m
->middle
? m
->middle
->vec
.iov_len
: 0);
523 con
->out_kvec_cur
= con
->out_kvec
;
525 /* fill in crc (except data pages), footer */
526 con
->out_msg
->hdr
.crc
=
527 cpu_to_le32(crc32c(0, (void *)&m
->hdr
,
528 sizeof(m
->hdr
) - sizeof(m
->hdr
.crc
)));
529 con
->out_msg
->footer
.flags
= CEPH_MSG_FOOTER_COMPLETE
;
530 con
->out_msg
->footer
.front_crc
=
531 cpu_to_le32(crc32c(0, m
->front
.iov_base
, m
->front
.iov_len
));
533 con
->out_msg
->footer
.middle_crc
=
534 cpu_to_le32(crc32c(0, m
->middle
->vec
.iov_base
,
535 m
->middle
->vec
.iov_len
));
537 con
->out_msg
->footer
.middle_crc
= 0;
538 con
->out_msg
->footer
.data_crc
= 0;
539 dout("prepare_write_message front_crc %u data_crc %u\n",
540 le32_to_cpu(con
->out_msg
->footer
.front_crc
),
541 le32_to_cpu(con
->out_msg
->footer
.middle_crc
));
543 /* is there a data payload? */
544 if (le32_to_cpu(m
->hdr
.data_len
) > 0) {
545 /* initialize page iterator */
546 con
->out_msg_pos
.page
= 0;
548 con
->out_msg_pos
.page_pos
= m
->page_alignment
;
550 con
->out_msg_pos
.page_pos
= 0;
551 con
->out_msg_pos
.data_pos
= 0;
552 con
->out_msg_pos
.did_page_crc
= 0;
553 con
->out_more
= 1; /* data + footer will follow */
555 /* no, queue up footer too and be done */
556 prepare_write_message_footer(con
, v
);
559 set_bit(WRITE_PENDING
, &con
->state
);
565 static void prepare_write_ack(struct ceph_connection
*con
)
567 dout("prepare_write_ack %p %llu -> %llu\n", con
,
568 con
->in_seq_acked
, con
->in_seq
);
569 con
->in_seq_acked
= con
->in_seq
;
571 con
->out_kvec
[0].iov_base
= &tag_ack
;
572 con
->out_kvec
[0].iov_len
= 1;
573 con
->out_temp_ack
= cpu_to_le64(con
->in_seq_acked
);
574 con
->out_kvec
[1].iov_base
= &con
->out_temp_ack
;
575 con
->out_kvec
[1].iov_len
= sizeof(con
->out_temp_ack
);
576 con
->out_kvec_left
= 2;
577 con
->out_kvec_bytes
= 1 + sizeof(con
->out_temp_ack
);
578 con
->out_kvec_cur
= con
->out_kvec
;
579 con
->out_more
= 1; /* more will follow.. eventually.. */
580 set_bit(WRITE_PENDING
, &con
->state
);
584 * Prepare to write keepalive byte.
586 static void prepare_write_keepalive(struct ceph_connection
*con
)
588 dout("prepare_write_keepalive %p\n", con
);
589 con
->out_kvec
[0].iov_base
= &tag_keepalive
;
590 con
->out_kvec
[0].iov_len
= 1;
591 con
->out_kvec_left
= 1;
592 con
->out_kvec_bytes
= 1;
593 con
->out_kvec_cur
= con
->out_kvec
;
594 set_bit(WRITE_PENDING
, &con
->state
);
598 * Connection negotiation.
601 static int prepare_connect_authorizer(struct ceph_connection
*con
)
605 int auth_protocol
= 0;
607 mutex_unlock(&con
->mutex
);
608 if (con
->ops
->get_authorizer
)
609 con
->ops
->get_authorizer(con
, &auth_buf
, &auth_len
,
610 &auth_protocol
, &con
->auth_reply_buf
,
611 &con
->auth_reply_buf_len
,
613 mutex_lock(&con
->mutex
);
615 if (test_bit(CLOSED
, &con
->state
) ||
616 test_bit(OPENING
, &con
->state
))
619 con
->out_connect
.authorizer_protocol
= cpu_to_le32(auth_protocol
);
620 con
->out_connect
.authorizer_len
= cpu_to_le32(auth_len
);
623 con
->out_kvec
[con
->out_kvec_left
].iov_base
= auth_buf
;
624 con
->out_kvec
[con
->out_kvec_left
].iov_len
= auth_len
;
625 con
->out_kvec_left
++;
626 con
->out_kvec_bytes
+= auth_len
;
632 * We connected to a peer and are saying hello.
634 static void prepare_write_banner(struct ceph_messenger
*msgr
,
635 struct ceph_connection
*con
)
637 int len
= strlen(CEPH_BANNER
);
639 con
->out_kvec
[0].iov_base
= CEPH_BANNER
;
640 con
->out_kvec
[0].iov_len
= len
;
641 con
->out_kvec
[1].iov_base
= &msgr
->my_enc_addr
;
642 con
->out_kvec
[1].iov_len
= sizeof(msgr
->my_enc_addr
);
643 con
->out_kvec_left
= 2;
644 con
->out_kvec_bytes
= len
+ sizeof(msgr
->my_enc_addr
);
645 con
->out_kvec_cur
= con
->out_kvec
;
647 set_bit(WRITE_PENDING
, &con
->state
);
650 static int prepare_write_connect(struct ceph_messenger
*msgr
,
651 struct ceph_connection
*con
,
654 unsigned global_seq
= get_global_seq(con
->msgr
, 0);
657 switch (con
->peer_name
.type
) {
658 case CEPH_ENTITY_TYPE_MON
:
659 proto
= CEPH_MONC_PROTOCOL
;
661 case CEPH_ENTITY_TYPE_OSD
:
662 proto
= CEPH_OSDC_PROTOCOL
;
664 case CEPH_ENTITY_TYPE_MDS
:
665 proto
= CEPH_MDSC_PROTOCOL
;
671 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con
,
672 con
->connect_seq
, global_seq
, proto
);
674 con
->out_connect
.features
= cpu_to_le64(msgr
->supported_features
);
675 con
->out_connect
.host_type
= cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT
);
676 con
->out_connect
.connect_seq
= cpu_to_le32(con
->connect_seq
);
677 con
->out_connect
.global_seq
= cpu_to_le32(global_seq
);
678 con
->out_connect
.protocol_version
= cpu_to_le32(proto
);
679 con
->out_connect
.flags
= 0;
682 con
->out_kvec_left
= 0;
683 con
->out_kvec_bytes
= 0;
685 con
->out_kvec
[con
->out_kvec_left
].iov_base
= &con
->out_connect
;
686 con
->out_kvec
[con
->out_kvec_left
].iov_len
= sizeof(con
->out_connect
);
687 con
->out_kvec_left
++;
688 con
->out_kvec_bytes
+= sizeof(con
->out_connect
);
689 con
->out_kvec_cur
= con
->out_kvec
;
691 set_bit(WRITE_PENDING
, &con
->state
);
693 return prepare_connect_authorizer(con
);
698 * write as much of pending kvecs to the socket as we can.
700 * 0 -> socket full, but more to do
703 static int write_partial_kvec(struct ceph_connection
*con
)
707 dout("write_partial_kvec %p %d left\n", con
, con
->out_kvec_bytes
);
708 while (con
->out_kvec_bytes
> 0) {
709 ret
= ceph_tcp_sendmsg(con
->sock
, con
->out_kvec_cur
,
710 con
->out_kvec_left
, con
->out_kvec_bytes
,
714 con
->out_kvec_bytes
-= ret
;
715 if (con
->out_kvec_bytes
== 0)
718 if (ret
>= con
->out_kvec_cur
->iov_len
) {
719 ret
-= con
->out_kvec_cur
->iov_len
;
721 con
->out_kvec_left
--;
723 con
->out_kvec_cur
->iov_len
-= ret
;
724 con
->out_kvec_cur
->iov_base
+= ret
;
730 con
->out_kvec_left
= 0;
731 con
->out_kvec_is_msg
= false;
734 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con
,
735 con
->out_kvec_bytes
, con
->out_kvec_left
, ret
);
736 return ret
; /* done! */
740 static void init_bio_iter(struct bio
*bio
, struct bio
**iter
, int *seg
)
751 static void iter_bio_next(struct bio
**bio_iter
, int *seg
)
753 if (*bio_iter
== NULL
)
756 BUG_ON(*seg
>= (*bio_iter
)->bi_vcnt
);
759 if (*seg
== (*bio_iter
)->bi_vcnt
)
760 init_bio_iter((*bio_iter
)->bi_next
, bio_iter
, seg
);
765 * Write as much message data payload as we can. If we finish, queue
767 * 1 -> done, footer is now queued in out_kvec[].
768 * 0 -> socket full, but more to do
771 static int write_partial_msg_pages(struct ceph_connection
*con
)
773 struct ceph_msg
*msg
= con
->out_msg
;
774 unsigned data_len
= le32_to_cpu(msg
->hdr
.data_len
);
776 int crc
= con
->msgr
->nocrc
;
780 size_t trail_len
= (msg
->trail
? msg
->trail
->length
: 0);
782 dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n",
783 con
, con
->out_msg
, con
->out_msg_pos
.page
, con
->out_msg
->nr_pages
,
784 con
->out_msg_pos
.page_pos
);
787 if (msg
->bio
&& !msg
->bio_iter
)
788 init_bio_iter(msg
->bio
, &msg
->bio_iter
, &msg
->bio_seg
);
791 while (data_len
> con
->out_msg_pos
.data_pos
) {
792 struct page
*page
= NULL
;
794 int max_write
= PAGE_SIZE
;
797 total_max_write
= data_len
- trail_len
-
798 con
->out_msg_pos
.data_pos
;
801 * if we are calculating the data crc (the default), we need
802 * to map the page. if our pages[] has been revoked, use the
806 /* have we reached the trail part of the data? */
807 if (con
->out_msg_pos
.data_pos
>= data_len
- trail_len
) {
810 total_max_write
= data_len
- con
->out_msg_pos
.data_pos
;
812 page
= list_first_entry(&msg
->trail
->head
,
816 max_write
= PAGE_SIZE
;
817 } else if (msg
->pages
) {
818 page
= msg
->pages
[con
->out_msg_pos
.page
];
821 } else if (msg
->pagelist
) {
822 page
= list_first_entry(&msg
->pagelist
->head
,
827 } else if (msg
->bio
) {
830 bv
= bio_iovec_idx(msg
->bio_iter
, msg
->bio_seg
);
832 page_shift
= bv
->bv_offset
;
834 kaddr
= kmap(page
) + page_shift
;
835 max_write
= bv
->bv_len
;
838 page
= con
->msgr
->zero_page
;
840 kaddr
= page_address(con
->msgr
->zero_page
);
842 len
= min_t(int, max_write
- con
->out_msg_pos
.page_pos
,
845 if (crc
&& !con
->out_msg_pos
.did_page_crc
) {
846 void *base
= kaddr
+ con
->out_msg_pos
.page_pos
;
847 u32 tmpcrc
= le32_to_cpu(con
->out_msg
->footer
.data_crc
);
849 BUG_ON(kaddr
== NULL
);
850 con
->out_msg
->footer
.data_crc
=
851 cpu_to_le32(crc32c(tmpcrc
, base
, len
));
852 con
->out_msg_pos
.did_page_crc
= 1;
854 ret
= kernel_sendpage(con
->sock
, page
,
855 con
->out_msg_pos
.page_pos
+ page_shift
,
857 MSG_DONTWAIT
| MSG_NOSIGNAL
|
861 (msg
->pages
|| msg
->pagelist
|| msg
->bio
|| in_trail
))
869 con
->out_msg_pos
.data_pos
+= ret
;
870 con
->out_msg_pos
.page_pos
+= ret
;
872 con
->out_msg_pos
.page_pos
= 0;
873 con
->out_msg_pos
.page
++;
874 con
->out_msg_pos
.did_page_crc
= 0;
876 list_move_tail(&page
->lru
,
878 else if (msg
->pagelist
)
879 list_move_tail(&page
->lru
,
880 &msg
->pagelist
->head
);
883 iter_bio_next(&msg
->bio_iter
, &msg
->bio_seg
);
888 dout("write_partial_msg_pages %p msg %p done\n", con
, msg
);
890 /* prepare and queue up footer, too */
892 con
->out_msg
->footer
.flags
|= CEPH_MSG_FOOTER_NOCRC
;
893 con
->out_kvec_bytes
= 0;
894 con
->out_kvec_left
= 0;
895 con
->out_kvec_cur
= con
->out_kvec
;
896 prepare_write_message_footer(con
, 0);
905 static int write_partial_skip(struct ceph_connection
*con
)
909 while (con
->out_skip
> 0) {
911 .iov_base
= page_address(con
->msgr
->zero_page
),
912 .iov_len
= min(con
->out_skip
, (int)PAGE_CACHE_SIZE
)
915 ret
= ceph_tcp_sendmsg(con
->sock
, &iov
, 1, iov
.iov_len
, 1);
918 con
->out_skip
-= ret
;
926 * Prepare to read connection handshake, or an ack.
928 static void prepare_read_banner(struct ceph_connection
*con
)
930 dout("prepare_read_banner %p\n", con
);
931 con
->in_base_pos
= 0;
934 static void prepare_read_connect(struct ceph_connection
*con
)
936 dout("prepare_read_connect %p\n", con
);
937 con
->in_base_pos
= 0;
940 static void prepare_read_ack(struct ceph_connection
*con
)
942 dout("prepare_read_ack %p\n", con
);
943 con
->in_base_pos
= 0;
946 static void prepare_read_tag(struct ceph_connection
*con
)
948 dout("prepare_read_tag %p\n", con
);
949 con
->in_base_pos
= 0;
950 con
->in_tag
= CEPH_MSGR_TAG_READY
;
954 * Prepare to read a message.
956 static int prepare_read_message(struct ceph_connection
*con
)
958 dout("prepare_read_message %p\n", con
);
959 BUG_ON(con
->in_msg
!= NULL
);
960 con
->in_base_pos
= 0;
961 con
->in_front_crc
= con
->in_middle_crc
= con
->in_data_crc
= 0;
966 static int read_partial(struct ceph_connection
*con
,
967 int *to
, int size
, void *object
)
970 while (con
->in_base_pos
< *to
) {
971 int left
= *to
- con
->in_base_pos
;
972 int have
= size
- left
;
973 int ret
= ceph_tcp_recvmsg(con
->sock
, object
+ have
, left
);
976 con
->in_base_pos
+= ret
;
983 * Read all or part of the connect-side handshake on a new connection
985 static int read_partial_banner(struct ceph_connection
*con
)
989 dout("read_partial_banner %p at %d\n", con
, con
->in_base_pos
);
992 ret
= read_partial(con
, &to
, strlen(CEPH_BANNER
), con
->in_banner
);
995 ret
= read_partial(con
, &to
, sizeof(con
->actual_peer_addr
),
996 &con
->actual_peer_addr
);
999 ret
= read_partial(con
, &to
, sizeof(con
->peer_addr_for_me
),
1000 &con
->peer_addr_for_me
);
1007 static int read_partial_connect(struct ceph_connection
*con
)
1011 dout("read_partial_connect %p at %d\n", con
, con
->in_base_pos
);
1013 ret
= read_partial(con
, &to
, sizeof(con
->in_reply
), &con
->in_reply
);
1016 ret
= read_partial(con
, &to
, le32_to_cpu(con
->in_reply
.authorizer_len
),
1017 con
->auth_reply_buf
);
1021 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1022 con
, (int)con
->in_reply
.tag
,
1023 le32_to_cpu(con
->in_reply
.connect_seq
),
1024 le32_to_cpu(con
->in_reply
.global_seq
));
1031 * Verify the hello banner looks okay.
1033 static int verify_hello(struct ceph_connection
*con
)
1035 if (memcmp(con
->in_banner
, CEPH_BANNER
, strlen(CEPH_BANNER
))) {
1036 pr_err("connect to %s got bad banner\n",
1037 ceph_pr_addr(&con
->peer_addr
.in_addr
));
1038 con
->error_msg
= "protocol error, bad banner";
1044 static bool addr_is_blank(struct sockaddr_storage
*ss
)
1046 switch (ss
->ss_family
) {
1048 return ((struct sockaddr_in
*)ss
)->sin_addr
.s_addr
== 0;
1051 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[0] == 0 &&
1052 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[1] == 0 &&
1053 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[2] == 0 &&
1054 ((struct sockaddr_in6
*)ss
)->sin6_addr
.s6_addr32
[3] == 0;
1059 static int addr_port(struct sockaddr_storage
*ss
)
1061 switch (ss
->ss_family
) {
1063 return ntohs(((struct sockaddr_in
*)ss
)->sin_port
);
1065 return ntohs(((struct sockaddr_in6
*)ss
)->sin6_port
);
1070 static void addr_set_port(struct sockaddr_storage
*ss
, int p
)
1072 switch (ss
->ss_family
) {
1074 ((struct sockaddr_in
*)ss
)->sin_port
= htons(p
);
1077 ((struct sockaddr_in6
*)ss
)->sin6_port
= htons(p
);
1083 * Unlike other *_pton function semantics, zero indicates success.
1085 static int ceph_pton(const char *str
, size_t len
, struct sockaddr_storage
*ss
,
1086 char delim
, const char **ipend
)
1088 struct sockaddr_in
*in4
= (void *)ss
;
1089 struct sockaddr_in6
*in6
= (void *)ss
;
1091 memset(ss
, 0, sizeof(*ss
));
1093 if (in4_pton(str
, len
, (u8
*)&in4
->sin_addr
.s_addr
, delim
, ipend
)) {
1094 ss
->ss_family
= AF_INET
;
1098 if (in6_pton(str
, len
, (u8
*)&in6
->sin6_addr
.s6_addr
, delim
, ipend
)) {
1099 ss
->ss_family
= AF_INET6
;
1107 * Extract hostname string and resolve using kernel DNS facility.
1109 #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1110 static int ceph_dns_resolve_name(const char *name
, size_t namelen
,
1111 struct sockaddr_storage
*ss
, char delim
, const char **ipend
)
1113 const char *end
, *delim_p
;
1114 char *colon_p
, *ip_addr
= NULL
;
1118 * The end of the hostname occurs immediately preceding the delimiter or
1119 * the port marker (':') where the delimiter takes precedence.
1121 delim_p
= memchr(name
, delim
, namelen
);
1122 colon_p
= memchr(name
, ':', namelen
);
1124 if (delim_p
&& colon_p
)
1125 end
= delim_p
< colon_p
? delim_p
: colon_p
;
1126 else if (!delim_p
&& colon_p
)
1130 if (!end
) /* case: hostname:/ */
1131 end
= name
+ namelen
;
1137 /* do dns_resolve upcall */
1138 ip_len
= dns_query(NULL
, name
, end
- name
, NULL
, &ip_addr
, NULL
);
1140 ret
= ceph_pton(ip_addr
, ip_len
, ss
, -1, NULL
);
1148 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end
- name
), name
,
1149 ret
, ret
? "failed" : ceph_pr_addr(ss
));
1154 static inline int ceph_dns_resolve_name(const char *name
, size_t namelen
,
1155 struct sockaddr_storage
*ss
, char delim
, const char **ipend
)
1162 * Parse a server name (IP or hostname). If a valid IP address is not found
1163 * then try to extract a hostname to resolve using userspace DNS upcall.
1165 static int ceph_parse_server_name(const char *name
, size_t namelen
,
1166 struct sockaddr_storage
*ss
, char delim
, const char **ipend
)
1170 ret
= ceph_pton(name
, namelen
, ss
, delim
, ipend
);
1172 ret
= ceph_dns_resolve_name(name
, namelen
, ss
, delim
, ipend
);
1178 * Parse an ip[:port] list into an addr array. Use the default
1179 * monitor port if a port isn't specified.
1181 int ceph_parse_ips(const char *c
, const char *end
,
1182 struct ceph_entity_addr
*addr
,
1183 int max_count
, int *count
)
1185 int i
, ret
= -EINVAL
;
1188 dout("parse_ips on '%.*s'\n", (int)(end
-c
), c
);
1189 for (i
= 0; i
< max_count
; i
++) {
1191 struct sockaddr_storage
*ss
= &addr
[i
].in_addr
;
1200 ret
= ceph_parse_server_name(p
, end
- p
, ss
, delim
, &ipend
);
1209 dout("missing matching ']'\n");
1216 if (p
< end
&& *p
== ':') {
1219 while (p
< end
&& *p
>= '0' && *p
<= '9') {
1220 port
= (port
* 10) + (*p
- '0');
1223 if (port
> 65535 || port
== 0)
1226 port
= CEPH_MON_PORT
;
1229 addr_set_port(ss
, port
);
1231 dout("parse_ips got %s\n", ceph_pr_addr(ss
));
1248 pr_err("parse_ips bad ip '%.*s'\n", (int)(end
- c
), c
);
1251 EXPORT_SYMBOL(ceph_parse_ips
);
1253 static int process_banner(struct ceph_connection
*con
)
1255 dout("process_banner on %p\n", con
);
1257 if (verify_hello(con
) < 0)
1260 ceph_decode_addr(&con
->actual_peer_addr
);
1261 ceph_decode_addr(&con
->peer_addr_for_me
);
1264 * Make sure the other end is who we wanted. note that the other
1265 * end may not yet know their ip address, so if it's 0.0.0.0, give
1266 * them the benefit of the doubt.
1268 if (memcmp(&con
->peer_addr
, &con
->actual_peer_addr
,
1269 sizeof(con
->peer_addr
)) != 0 &&
1270 !(addr_is_blank(&con
->actual_peer_addr
.in_addr
) &&
1271 con
->actual_peer_addr
.nonce
== con
->peer_addr
.nonce
)) {
1272 pr_warning("wrong peer, want %s/%d, got %s/%d\n",
1273 ceph_pr_addr(&con
->peer_addr
.in_addr
),
1274 (int)le32_to_cpu(con
->peer_addr
.nonce
),
1275 ceph_pr_addr(&con
->actual_peer_addr
.in_addr
),
1276 (int)le32_to_cpu(con
->actual_peer_addr
.nonce
));
1277 con
->error_msg
= "wrong peer at address";
1282 * did we learn our address?
1284 if (addr_is_blank(&con
->msgr
->inst
.addr
.in_addr
)) {
1285 int port
= addr_port(&con
->msgr
->inst
.addr
.in_addr
);
1287 memcpy(&con
->msgr
->inst
.addr
.in_addr
,
1288 &con
->peer_addr_for_me
.in_addr
,
1289 sizeof(con
->peer_addr_for_me
.in_addr
));
1290 addr_set_port(&con
->msgr
->inst
.addr
.in_addr
, port
);
1291 encode_my_addr(con
->msgr
);
1292 dout("process_banner learned my addr is %s\n",
1293 ceph_pr_addr(&con
->msgr
->inst
.addr
.in_addr
));
1296 set_bit(NEGOTIATING
, &con
->state
);
1297 prepare_read_connect(con
);
1301 static void fail_protocol(struct ceph_connection
*con
)
1303 reset_connection(con
);
1304 set_bit(CLOSED
, &con
->state
); /* in case there's queued work */
1306 mutex_unlock(&con
->mutex
);
1307 if (con
->ops
->bad_proto
)
1308 con
->ops
->bad_proto(con
);
1309 mutex_lock(&con
->mutex
);
1312 static int process_connect(struct ceph_connection
*con
)
1314 u64 sup_feat
= con
->msgr
->supported_features
;
1315 u64 req_feat
= con
->msgr
->required_features
;
1316 u64 server_feat
= le64_to_cpu(con
->in_reply
.features
);
1319 dout("process_connect on %p tag %d\n", con
, (int)con
->in_tag
);
1321 switch (con
->in_reply
.tag
) {
1322 case CEPH_MSGR_TAG_FEATURES
:
1323 pr_err("%s%lld %s feature set mismatch,"
1324 " my %llx < server's %llx, missing %llx\n",
1325 ENTITY_NAME(con
->peer_name
),
1326 ceph_pr_addr(&con
->peer_addr
.in_addr
),
1327 sup_feat
, server_feat
, server_feat
& ~sup_feat
);
1328 con
->error_msg
= "missing required protocol features";
1332 case CEPH_MSGR_TAG_BADPROTOVER
:
1333 pr_err("%s%lld %s protocol version mismatch,"
1334 " my %d != server's %d\n",
1335 ENTITY_NAME(con
->peer_name
),
1336 ceph_pr_addr(&con
->peer_addr
.in_addr
),
1337 le32_to_cpu(con
->out_connect
.protocol_version
),
1338 le32_to_cpu(con
->in_reply
.protocol_version
));
1339 con
->error_msg
= "protocol version mismatch";
1343 case CEPH_MSGR_TAG_BADAUTHORIZER
:
1345 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con
,
1347 if (con
->auth_retry
== 2) {
1348 con
->error_msg
= "connect authorization failure";
1351 con
->auth_retry
= 1;
1352 ret
= prepare_write_connect(con
->msgr
, con
, 0);
1355 prepare_read_connect(con
);
1358 case CEPH_MSGR_TAG_RESETSESSION
:
1360 * If we connected with a large connect_seq but the peer
1361 * has no record of a session with us (no connection, or
1362 * connect_seq == 0), they will send RESETSESION to indicate
1363 * that they must have reset their session, and may have
1366 dout("process_connect got RESET peer seq %u\n",
1367 le32_to_cpu(con
->in_connect
.connect_seq
));
1368 pr_err("%s%lld %s connection reset\n",
1369 ENTITY_NAME(con
->peer_name
),
1370 ceph_pr_addr(&con
->peer_addr
.in_addr
));
1371 reset_connection(con
);
1372 prepare_write_connect(con
->msgr
, con
, 0);
1373 prepare_read_connect(con
);
1375 /* Tell ceph about it. */
1376 mutex_unlock(&con
->mutex
);
1377 pr_info("reset on %s%lld\n", ENTITY_NAME(con
->peer_name
));
1378 if (con
->ops
->peer_reset
)
1379 con
->ops
->peer_reset(con
);
1380 mutex_lock(&con
->mutex
);
1381 if (test_bit(CLOSED
, &con
->state
) ||
1382 test_bit(OPENING
, &con
->state
))
1386 case CEPH_MSGR_TAG_RETRY_SESSION
:
1388 * If we sent a smaller connect_seq than the peer has, try
1389 * again with a larger value.
1391 dout("process_connect got RETRY my seq = %u, peer_seq = %u\n",
1392 le32_to_cpu(con
->out_connect
.connect_seq
),
1393 le32_to_cpu(con
->in_connect
.connect_seq
));
1394 con
->connect_seq
= le32_to_cpu(con
->in_connect
.connect_seq
);
1395 prepare_write_connect(con
->msgr
, con
, 0);
1396 prepare_read_connect(con
);
1399 case CEPH_MSGR_TAG_RETRY_GLOBAL
:
1401 * If we sent a smaller global_seq than the peer has, try
1402 * again with a larger value.
1404 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
1405 con
->peer_global_seq
,
1406 le32_to_cpu(con
->in_connect
.global_seq
));
1407 get_global_seq(con
->msgr
,
1408 le32_to_cpu(con
->in_connect
.global_seq
));
1409 prepare_write_connect(con
->msgr
, con
, 0);
1410 prepare_read_connect(con
);
1413 case CEPH_MSGR_TAG_READY
:
1414 if (req_feat
& ~server_feat
) {
1415 pr_err("%s%lld %s protocol feature mismatch,"
1416 " my required %llx > server's %llx, need %llx\n",
1417 ENTITY_NAME(con
->peer_name
),
1418 ceph_pr_addr(&con
->peer_addr
.in_addr
),
1419 req_feat
, server_feat
, req_feat
& ~server_feat
);
1420 con
->error_msg
= "missing required protocol features";
1424 clear_bit(CONNECTING
, &con
->state
);
1425 con
->peer_global_seq
= le32_to_cpu(con
->in_reply
.global_seq
);
1427 con
->peer_features
= server_feat
;
1428 dout("process_connect got READY gseq %d cseq %d (%d)\n",
1429 con
->peer_global_seq
,
1430 le32_to_cpu(con
->in_reply
.connect_seq
),
1432 WARN_ON(con
->connect_seq
!=
1433 le32_to_cpu(con
->in_reply
.connect_seq
));
1435 if (con
->in_reply
.flags
& CEPH_MSG_CONNECT_LOSSY
)
1436 set_bit(LOSSYTX
, &con
->state
);
1438 prepare_read_tag(con
);
1441 case CEPH_MSGR_TAG_WAIT
:
1443 * If there is a connection race (we are opening
1444 * connections to each other), one of us may just have
1445 * to WAIT. This shouldn't happen if we are the
1448 pr_err("process_connect got WAIT as client\n");
1449 con
->error_msg
= "protocol error, got WAIT as client";
1453 pr_err("connect protocol error, will retry\n");
1454 con
->error_msg
= "protocol error, garbage tag during connect";
1462 * read (part of) an ack
1464 static int read_partial_ack(struct ceph_connection
*con
)
1468 return read_partial(con
, &to
, sizeof(con
->in_temp_ack
),
1474 * We can finally discard anything that's been acked.
1476 static void process_ack(struct ceph_connection
*con
)
1479 u64 ack
= le64_to_cpu(con
->in_temp_ack
);
1482 while (!list_empty(&con
->out_sent
)) {
1483 m
= list_first_entry(&con
->out_sent
, struct ceph_msg
,
1485 seq
= le64_to_cpu(m
->hdr
.seq
);
1488 dout("got ack for seq %llu type %d at %p\n", seq
,
1489 le16_to_cpu(m
->hdr
.type
), m
);
1490 m
->ack_stamp
= jiffies
;
1493 prepare_read_tag(con
);
1499 static int read_partial_message_section(struct ceph_connection
*con
,
1500 struct kvec
*section
,
1501 unsigned int sec_len
, u32
*crc
)
1507 while (section
->iov_len
< sec_len
) {
1508 BUG_ON(section
->iov_base
== NULL
);
1509 left
= sec_len
- section
->iov_len
;
1510 ret
= ceph_tcp_recvmsg(con
->sock
, (char *)section
->iov_base
+
1511 section
->iov_len
, left
);
1514 section
->iov_len
+= ret
;
1515 if (section
->iov_len
== sec_len
)
1516 *crc
= crc32c(0, section
->iov_base
,
1523 static struct ceph_msg
*ceph_alloc_msg(struct ceph_connection
*con
,
1524 struct ceph_msg_header
*hdr
,
1528 static int read_partial_message_pages(struct ceph_connection
*con
,
1529 struct page
**pages
,
1530 unsigned data_len
, int datacrc
)
1536 left
= min((int)(data_len
- con
->in_msg_pos
.data_pos
),
1537 (int)(PAGE_SIZE
- con
->in_msg_pos
.page_pos
));
1539 BUG_ON(pages
== NULL
);
1540 p
= kmap(pages
[con
->in_msg_pos
.page
]);
1541 ret
= ceph_tcp_recvmsg(con
->sock
, p
+ con
->in_msg_pos
.page_pos
,
1543 if (ret
> 0 && datacrc
)
1545 crc32c(con
->in_data_crc
,
1546 p
+ con
->in_msg_pos
.page_pos
, ret
);
1547 kunmap(pages
[con
->in_msg_pos
.page
]);
1550 con
->in_msg_pos
.data_pos
+= ret
;
1551 con
->in_msg_pos
.page_pos
+= ret
;
1552 if (con
->in_msg_pos
.page_pos
== PAGE_SIZE
) {
1553 con
->in_msg_pos
.page_pos
= 0;
1554 con
->in_msg_pos
.page
++;
1561 static int read_partial_message_bio(struct ceph_connection
*con
,
1562 struct bio
**bio_iter
, int *bio_seg
,
1563 unsigned data_len
, int datacrc
)
1565 struct bio_vec
*bv
= bio_iovec_idx(*bio_iter
, *bio_seg
);
1572 left
= min((int)(data_len
- con
->in_msg_pos
.data_pos
),
1573 (int)(bv
->bv_len
- con
->in_msg_pos
.page_pos
));
1575 p
= kmap(bv
->bv_page
) + bv
->bv_offset
;
1577 ret
= ceph_tcp_recvmsg(con
->sock
, p
+ con
->in_msg_pos
.page_pos
,
1579 if (ret
> 0 && datacrc
)
1581 crc32c(con
->in_data_crc
,
1582 p
+ con
->in_msg_pos
.page_pos
, ret
);
1583 kunmap(bv
->bv_page
);
1586 con
->in_msg_pos
.data_pos
+= ret
;
1587 con
->in_msg_pos
.page_pos
+= ret
;
1588 if (con
->in_msg_pos
.page_pos
== bv
->bv_len
) {
1589 con
->in_msg_pos
.page_pos
= 0;
1590 iter_bio_next(bio_iter
, bio_seg
);
1598 * read (part of) a message.
1600 static int read_partial_message(struct ceph_connection
*con
)
1602 struct ceph_msg
*m
= con
->in_msg
;
1605 unsigned front_len
, middle_len
, data_len
;
1606 int datacrc
= con
->msgr
->nocrc
;
1610 dout("read_partial_message con %p msg %p\n", con
, m
);
1613 while (con
->in_base_pos
< sizeof(con
->in_hdr
)) {
1614 left
= sizeof(con
->in_hdr
) - con
->in_base_pos
;
1615 ret
= ceph_tcp_recvmsg(con
->sock
,
1616 (char *)&con
->in_hdr
+ con
->in_base_pos
,
1620 con
->in_base_pos
+= ret
;
1621 if (con
->in_base_pos
== sizeof(con
->in_hdr
)) {
1622 u32 crc
= crc32c(0, (void *)&con
->in_hdr
,
1623 sizeof(con
->in_hdr
) - sizeof(con
->in_hdr
.crc
));
1624 if (crc
!= le32_to_cpu(con
->in_hdr
.crc
)) {
1625 pr_err("read_partial_message bad hdr "
1626 " crc %u != expected %u\n",
1627 crc
, con
->in_hdr
.crc
);
1632 front_len
= le32_to_cpu(con
->in_hdr
.front_len
);
1633 if (front_len
> CEPH_MSG_MAX_FRONT_LEN
)
1635 middle_len
= le32_to_cpu(con
->in_hdr
.middle_len
);
1636 if (middle_len
> CEPH_MSG_MAX_DATA_LEN
)
1638 data_len
= le32_to_cpu(con
->in_hdr
.data_len
);
1639 if (data_len
> CEPH_MSG_MAX_DATA_LEN
)
1643 seq
= le64_to_cpu(con
->in_hdr
.seq
);
1644 if ((s64
)seq
- (s64
)con
->in_seq
< 1) {
1645 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
1646 ENTITY_NAME(con
->peer_name
),
1647 ceph_pr_addr(&con
->peer_addr
.in_addr
),
1648 seq
, con
->in_seq
+ 1);
1649 con
->in_base_pos
= -front_len
- middle_len
- data_len
-
1651 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1653 } else if ((s64
)seq
- (s64
)con
->in_seq
> 1) {
1654 pr_err("read_partial_message bad seq %lld expected %lld\n",
1655 seq
, con
->in_seq
+ 1);
1656 con
->error_msg
= "bad message sequence # for incoming message";
1660 /* allocate message? */
1662 dout("got hdr type %d front %d data %d\n", con
->in_hdr
.type
,
1663 con
->in_hdr
.front_len
, con
->in_hdr
.data_len
);
1665 con
->in_msg
= ceph_alloc_msg(con
, &con
->in_hdr
, &skip
);
1667 /* skip this message */
1668 dout("alloc_msg said skip message\n");
1669 BUG_ON(con
->in_msg
);
1670 con
->in_base_pos
= -front_len
- middle_len
- data_len
-
1672 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1678 "error allocating memory for incoming message";
1682 m
->front
.iov_len
= 0; /* haven't read it yet */
1684 m
->middle
->vec
.iov_len
= 0;
1686 con
->in_msg_pos
.page
= 0;
1688 con
->in_msg_pos
.page_pos
= m
->page_alignment
;
1690 con
->in_msg_pos
.page_pos
= 0;
1691 con
->in_msg_pos
.data_pos
= 0;
1695 ret
= read_partial_message_section(con
, &m
->front
, front_len
,
1696 &con
->in_front_crc
);
1702 ret
= read_partial_message_section(con
, &m
->middle
->vec
,
1704 &con
->in_middle_crc
);
1709 if (m
->bio
&& !m
->bio_iter
)
1710 init_bio_iter(m
->bio
, &m
->bio_iter
, &m
->bio_seg
);
1714 while (con
->in_msg_pos
.data_pos
< data_len
) {
1716 ret
= read_partial_message_pages(con
, m
->pages
,
1721 } else if (m
->bio
) {
1723 ret
= read_partial_message_bio(con
,
1724 &m
->bio_iter
, &m
->bio_seg
,
1735 to
= sizeof(m
->hdr
) + sizeof(m
->footer
);
1736 while (con
->in_base_pos
< to
) {
1737 left
= to
- con
->in_base_pos
;
1738 ret
= ceph_tcp_recvmsg(con
->sock
, (char *)&m
->footer
+
1739 (con
->in_base_pos
- sizeof(m
->hdr
)),
1743 con
->in_base_pos
+= ret
;
1745 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
1746 m
, front_len
, m
->footer
.front_crc
, middle_len
,
1747 m
->footer
.middle_crc
, data_len
, m
->footer
.data_crc
);
1750 if (con
->in_front_crc
!= le32_to_cpu(m
->footer
.front_crc
)) {
1751 pr_err("read_partial_message %p front crc %u != exp. %u\n",
1752 m
, con
->in_front_crc
, m
->footer
.front_crc
);
1755 if (con
->in_middle_crc
!= le32_to_cpu(m
->footer
.middle_crc
)) {
1756 pr_err("read_partial_message %p middle crc %u != exp %u\n",
1757 m
, con
->in_middle_crc
, m
->footer
.middle_crc
);
1761 (m
->footer
.flags
& CEPH_MSG_FOOTER_NOCRC
) == 0 &&
1762 con
->in_data_crc
!= le32_to_cpu(m
->footer
.data_crc
)) {
1763 pr_err("read_partial_message %p data crc %u != exp. %u\n", m
,
1764 con
->in_data_crc
, le32_to_cpu(m
->footer
.data_crc
));
1768 return 1; /* done! */
1772 * Process message. This happens in the worker thread. The callback should
1773 * be careful not to do anything that waits on other incoming messages or it
1776 static void process_message(struct ceph_connection
*con
)
1778 struct ceph_msg
*msg
;
1783 /* if first message, set peer_name */
1784 if (con
->peer_name
.type
== 0)
1785 con
->peer_name
= msg
->hdr
.src
;
1788 mutex_unlock(&con
->mutex
);
1790 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
1791 msg
, le64_to_cpu(msg
->hdr
.seq
),
1792 ENTITY_NAME(msg
->hdr
.src
),
1793 le16_to_cpu(msg
->hdr
.type
),
1794 ceph_msg_type_name(le16_to_cpu(msg
->hdr
.type
)),
1795 le32_to_cpu(msg
->hdr
.front_len
),
1796 le32_to_cpu(msg
->hdr
.data_len
),
1797 con
->in_front_crc
, con
->in_middle_crc
, con
->in_data_crc
);
1798 con
->ops
->dispatch(con
, msg
);
1800 mutex_lock(&con
->mutex
);
1801 prepare_read_tag(con
);
1806 * Write something to the socket. Called in a worker thread when the
1807 * socket appears to be writeable and we have something ready to send.
1809 static int try_write(struct ceph_connection
*con
)
1811 struct ceph_messenger
*msgr
= con
->msgr
;
1814 dout("try_write start %p state %lu nref %d\n", con
, con
->state
,
1815 atomic_read(&con
->nref
));
1818 dout("try_write out_kvec_bytes %d\n", con
->out_kvec_bytes
);
1820 /* open the socket first? */
1821 if (con
->sock
== NULL
) {
1822 prepare_write_banner(msgr
, con
);
1823 prepare_write_connect(msgr
, con
, 1);
1824 prepare_read_banner(con
);
1825 set_bit(CONNECTING
, &con
->state
);
1826 clear_bit(NEGOTIATING
, &con
->state
);
1828 BUG_ON(con
->in_msg
);
1829 con
->in_tag
= CEPH_MSGR_TAG_READY
;
1830 dout("try_write initiating connect on %p new state %lu\n",
1832 con
->sock
= ceph_tcp_connect(con
);
1833 if (IS_ERR(con
->sock
)) {
1835 con
->error_msg
= "connect error";
1842 /* kvec data queued? */
1843 if (con
->out_skip
) {
1844 ret
= write_partial_skip(con
);
1848 if (con
->out_kvec_left
) {
1849 ret
= write_partial_kvec(con
);
1856 if (con
->out_msg_done
) {
1857 ceph_msg_put(con
->out_msg
);
1858 con
->out_msg
= NULL
; /* we're done with this one */
1862 ret
= write_partial_msg_pages(con
);
1864 goto more_kvec
; /* we need to send the footer, too! */
1868 dout("try_write write_partial_msg_pages err %d\n",
1875 if (!test_bit(CONNECTING
, &con
->state
)) {
1876 /* is anything else pending? */
1877 if (!list_empty(&con
->out_queue
)) {
1878 prepare_write_message(con
);
1881 if (con
->in_seq
> con
->in_seq_acked
) {
1882 prepare_write_ack(con
);
1885 if (test_and_clear_bit(KEEPALIVE_PENDING
, &con
->state
)) {
1886 prepare_write_keepalive(con
);
1891 /* Nothing to do! */
1892 clear_bit(WRITE_PENDING
, &con
->state
);
1893 dout("try_write nothing else to write.\n");
1896 dout("try_write done on %p ret %d\n", con
, ret
);
1903 * Read what we can from the socket.
1905 static int try_read(struct ceph_connection
*con
)
1912 if (test_bit(STANDBY
, &con
->state
))
1915 dout("try_read start on %p\n", con
);
1918 dout("try_read tag %d in_base_pos %d\n", (int)con
->in_tag
,
1922 * process_connect and process_message drop and re-take
1923 * con->mutex. make sure we handle a racing close or reopen.
1925 if (test_bit(CLOSED
, &con
->state
) ||
1926 test_bit(OPENING
, &con
->state
)) {
1931 if (test_bit(CONNECTING
, &con
->state
)) {
1932 if (!test_bit(NEGOTIATING
, &con
->state
)) {
1933 dout("try_read connecting\n");
1934 ret
= read_partial_banner(con
);
1937 ret
= process_banner(con
);
1941 ret
= read_partial_connect(con
);
1944 ret
= process_connect(con
);
1950 if (con
->in_base_pos
< 0) {
1952 * skipping + discarding content.
1954 * FIXME: there must be a better way to do this!
1956 static char buf
[1024];
1957 int skip
= min(1024, -con
->in_base_pos
);
1958 dout("skipping %d / %d bytes\n", skip
, -con
->in_base_pos
);
1959 ret
= ceph_tcp_recvmsg(con
->sock
, buf
, skip
);
1962 con
->in_base_pos
+= ret
;
1963 if (con
->in_base_pos
)
1966 if (con
->in_tag
== CEPH_MSGR_TAG_READY
) {
1970 ret
= ceph_tcp_recvmsg(con
->sock
, &con
->in_tag
, 1);
1973 dout("try_read got tag %d\n", (int)con
->in_tag
);
1974 switch (con
->in_tag
) {
1975 case CEPH_MSGR_TAG_MSG
:
1976 prepare_read_message(con
);
1978 case CEPH_MSGR_TAG_ACK
:
1979 prepare_read_ack(con
);
1981 case CEPH_MSGR_TAG_CLOSE
:
1982 set_bit(CLOSED
, &con
->state
); /* fixme */
1988 if (con
->in_tag
== CEPH_MSGR_TAG_MSG
) {
1989 ret
= read_partial_message(con
);
1993 con
->error_msg
= "bad crc";
1997 con
->error_msg
= "io error";
2002 if (con
->in_tag
== CEPH_MSGR_TAG_READY
)
2004 process_message(con
);
2007 if (con
->in_tag
== CEPH_MSGR_TAG_ACK
) {
2008 ret
= read_partial_ack(con
);
2016 dout("try_read done on %p ret %d\n", con
, ret
);
2020 pr_err("try_read bad con->in_tag = %d\n", (int)con
->in_tag
);
2021 con
->error_msg
= "protocol error, garbage tag";
2028 * Atomically queue work on a connection. Bump @con reference to
2029 * avoid races with connection teardown.
2031 static void queue_con(struct ceph_connection
*con
)
2033 if (test_bit(DEAD
, &con
->state
)) {
2034 dout("queue_con %p ignoring: DEAD\n",
2039 if (!con
->ops
->get(con
)) {
2040 dout("queue_con %p ref count 0\n", con
);
2044 if (!queue_delayed_work(ceph_msgr_wq
, &con
->work
, 0)) {
2045 dout("queue_con %p - already queued\n", con
);
2048 dout("queue_con %p\n", con
);
2053 * Do some work on a connection. Drop a connection ref when we're done.
2055 static void con_work(struct work_struct
*work
)
2057 struct ceph_connection
*con
= container_of(work
, struct ceph_connection
,
2061 mutex_lock(&con
->mutex
);
2063 if (test_and_clear_bit(BACKOFF
, &con
->state
)) {
2064 dout("con_work %p backing off\n", con
);
2065 if (queue_delayed_work(ceph_msgr_wq
, &con
->work
,
2066 round_jiffies_relative(con
->delay
))) {
2067 dout("con_work %p backoff %lu\n", con
, con
->delay
);
2068 mutex_unlock(&con
->mutex
);
2072 dout("con_work %p FAILED to back off %lu\n", con
,
2077 if (test_bit(STANDBY
, &con
->state
)) {
2078 dout("con_work %p STANDBY\n", con
);
2081 if (test_bit(CLOSED
, &con
->state
)) { /* e.g. if we are replaced */
2082 dout("con_work CLOSED\n");
2083 con_close_socket(con
);
2086 if (test_and_clear_bit(OPENING
, &con
->state
)) {
2087 /* reopen w/ new peer */
2088 dout("con_work OPENING\n");
2089 con_close_socket(con
);
2092 if (test_and_clear_bit(SOCK_CLOSED
, &con
->state
))
2095 ret
= try_read(con
);
2101 ret
= try_write(con
);
2108 mutex_unlock(&con
->mutex
);
2114 mutex_unlock(&con
->mutex
);
2115 ceph_fault(con
); /* error/fault path */
2121 * Generic error/fault handler. A retry mechanism is used with
2122 * exponential backoff
2124 static void ceph_fault(struct ceph_connection
*con
)
2126 pr_err("%s%lld %s %s\n", ENTITY_NAME(con
->peer_name
),
2127 ceph_pr_addr(&con
->peer_addr
.in_addr
), con
->error_msg
);
2128 dout("fault %p state %lu to peer %s\n",
2129 con
, con
->state
, ceph_pr_addr(&con
->peer_addr
.in_addr
));
2131 if (test_bit(LOSSYTX
, &con
->state
)) {
2132 dout("fault on LOSSYTX channel\n");
2136 mutex_lock(&con
->mutex
);
2137 if (test_bit(CLOSED
, &con
->state
))
2140 con_close_socket(con
);
2143 ceph_msg_put(con
->in_msg
);
2147 /* Requeue anything that hasn't been acked */
2148 list_splice_init(&con
->out_sent
, &con
->out_queue
);
2150 /* If there are no messages queued or keepalive pending, place
2151 * the connection in a STANDBY state */
2152 if (list_empty(&con
->out_queue
) &&
2153 !test_bit(KEEPALIVE_PENDING
, &con
->state
)) {
2154 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con
);
2155 clear_bit(WRITE_PENDING
, &con
->state
);
2156 set_bit(STANDBY
, &con
->state
);
2158 /* retry after a delay. */
2159 if (con
->delay
== 0)
2160 con
->delay
= BASE_DELAY_INTERVAL
;
2161 else if (con
->delay
< MAX_DELAY_INTERVAL
)
2164 if (queue_delayed_work(ceph_msgr_wq
, &con
->work
,
2165 round_jiffies_relative(con
->delay
))) {
2166 dout("fault queued %p delay %lu\n", con
, con
->delay
);
2169 dout("fault failed to queue %p delay %lu, backoff\n",
2172 * In many cases we see a socket state change
2173 * while con_work is running and end up
2174 * queuing (non-delayed) work, such that we
2175 * can't backoff with a delay. Set a flag so
2176 * that when con_work restarts we schedule the
2179 set_bit(BACKOFF
, &con
->state
);
2184 mutex_unlock(&con
->mutex
);
2187 * in case we faulted due to authentication, invalidate our
2188 * current tickets so that we can get new ones.
2190 if (con
->auth_retry
&& con
->ops
->invalidate_authorizer
) {
2191 dout("calling invalidate_authorizer()\n");
2192 con
->ops
->invalidate_authorizer(con
);
2195 if (con
->ops
->fault
)
2196 con
->ops
->fault(con
);
2202 * create a new messenger instance
2204 struct ceph_messenger
*ceph_messenger_create(struct ceph_entity_addr
*myaddr
,
2205 u32 supported_features
,
2206 u32 required_features
)
2208 struct ceph_messenger
*msgr
;
2210 msgr
= kzalloc(sizeof(*msgr
), GFP_KERNEL
);
2212 return ERR_PTR(-ENOMEM
);
2214 msgr
->supported_features
= supported_features
;
2215 msgr
->required_features
= required_features
;
2217 spin_lock_init(&msgr
->global_seq_lock
);
2219 /* the zero page is needed if a request is "canceled" while the message
2220 * is being written over the socket */
2221 msgr
->zero_page
= __page_cache_alloc(GFP_KERNEL
| __GFP_ZERO
);
2222 if (!msgr
->zero_page
) {
2224 return ERR_PTR(-ENOMEM
);
2226 kmap(msgr
->zero_page
);
2229 msgr
->inst
.addr
= *myaddr
;
2231 /* select a random nonce */
2232 msgr
->inst
.addr
.type
= 0;
2233 get_random_bytes(&msgr
->inst
.addr
.nonce
, sizeof(msgr
->inst
.addr
.nonce
));
2234 encode_my_addr(msgr
);
2236 dout("messenger_create %p\n", msgr
);
2239 EXPORT_SYMBOL(ceph_messenger_create
);
2241 void ceph_messenger_destroy(struct ceph_messenger
*msgr
)
2243 dout("destroy %p\n", msgr
);
2244 kunmap(msgr
->zero_page
);
2245 __free_page(msgr
->zero_page
);
2247 dout("destroyed messenger %p\n", msgr
);
2249 EXPORT_SYMBOL(ceph_messenger_destroy
);
2251 static void clear_standby(struct ceph_connection
*con
)
2253 /* come back from STANDBY? */
2254 if (test_and_clear_bit(STANDBY
, &con
->state
)) {
2255 mutex_lock(&con
->mutex
);
2256 dout("clear_standby %p and ++connect_seq\n", con
);
2258 WARN_ON(test_bit(WRITE_PENDING
, &con
->state
));
2259 WARN_ON(test_bit(KEEPALIVE_PENDING
, &con
->state
));
2260 mutex_unlock(&con
->mutex
);
2265 * Queue up an outgoing message on the given connection.
2267 void ceph_con_send(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2269 if (test_bit(CLOSED
, &con
->state
)) {
2270 dout("con_send %p closed, dropping %p\n", con
, msg
);
2276 msg
->hdr
.src
= con
->msgr
->inst
.name
;
2278 BUG_ON(msg
->front
.iov_len
!= le32_to_cpu(msg
->hdr
.front_len
));
2280 msg
->needs_out_seq
= true;
2283 mutex_lock(&con
->mutex
);
2284 BUG_ON(!list_empty(&msg
->list_head
));
2285 list_add_tail(&msg
->list_head
, &con
->out_queue
);
2286 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg
,
2287 ENTITY_NAME(con
->peer_name
), le16_to_cpu(msg
->hdr
.type
),
2288 ceph_msg_type_name(le16_to_cpu(msg
->hdr
.type
)),
2289 le32_to_cpu(msg
->hdr
.front_len
),
2290 le32_to_cpu(msg
->hdr
.middle_len
),
2291 le32_to_cpu(msg
->hdr
.data_len
));
2292 mutex_unlock(&con
->mutex
);
2294 /* if there wasn't anything waiting to send before, queue
2297 if (test_and_set_bit(WRITE_PENDING
, &con
->state
) == 0)
2300 EXPORT_SYMBOL(ceph_con_send
);
2303 * Revoke a message that was previously queued for send
2305 void ceph_con_revoke(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2307 mutex_lock(&con
->mutex
);
2308 if (!list_empty(&msg
->list_head
)) {
2309 dout("con_revoke %p msg %p - was on queue\n", con
, msg
);
2310 list_del_init(&msg
->list_head
);
2314 if (con
->out_msg
== msg
) {
2315 dout("con_revoke %p msg %p - was sending\n", con
, msg
);
2316 con
->out_msg
= NULL
;
2317 if (con
->out_kvec_is_msg
) {
2318 con
->out_skip
= con
->out_kvec_bytes
;
2319 con
->out_kvec_is_msg
= false;
2324 mutex_unlock(&con
->mutex
);
2328 * Revoke a message that we may be reading data into
2330 void ceph_con_revoke_message(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2332 mutex_lock(&con
->mutex
);
2333 if (con
->in_msg
&& con
->in_msg
== msg
) {
2334 unsigned front_len
= le32_to_cpu(con
->in_hdr
.front_len
);
2335 unsigned middle_len
= le32_to_cpu(con
->in_hdr
.middle_len
);
2336 unsigned data_len
= le32_to_cpu(con
->in_hdr
.data_len
);
2338 /* skip rest of message */
2339 dout("con_revoke_pages %p msg %p revoked\n", con
, msg
);
2340 con
->in_base_pos
= con
->in_base_pos
-
2341 sizeof(struct ceph_msg_header
) -
2345 sizeof(struct ceph_msg_footer
);
2346 ceph_msg_put(con
->in_msg
);
2348 con
->in_tag
= CEPH_MSGR_TAG_READY
;
2351 dout("con_revoke_pages %p msg %p pages %p no-op\n",
2352 con
, con
->in_msg
, msg
);
2354 mutex_unlock(&con
->mutex
);
2358 * Queue a keepalive byte to ensure the tcp connection is alive.
2360 void ceph_con_keepalive(struct ceph_connection
*con
)
2362 dout("con_keepalive %p\n", con
);
2364 if (test_and_set_bit(KEEPALIVE_PENDING
, &con
->state
) == 0 &&
2365 test_and_set_bit(WRITE_PENDING
, &con
->state
) == 0)
2368 EXPORT_SYMBOL(ceph_con_keepalive
);
2372 * construct a new message with given type, size
2373 * the new msg has a ref count of 1.
2375 struct ceph_msg
*ceph_msg_new(int type
, int front_len
, gfp_t flags
,
2380 m
= kmalloc(sizeof(*m
), flags
);
2383 kref_init(&m
->kref
);
2384 INIT_LIST_HEAD(&m
->list_head
);
2387 m
->hdr
.type
= cpu_to_le16(type
);
2388 m
->hdr
.priority
= cpu_to_le16(CEPH_MSG_PRIO_DEFAULT
);
2390 m
->hdr
.front_len
= cpu_to_le32(front_len
);
2391 m
->hdr
.middle_len
= 0;
2392 m
->hdr
.data_len
= 0;
2393 m
->hdr
.data_off
= 0;
2394 m
->hdr
.reserved
= 0;
2395 m
->footer
.front_crc
= 0;
2396 m
->footer
.middle_crc
= 0;
2397 m
->footer
.data_crc
= 0;
2398 m
->footer
.flags
= 0;
2399 m
->front_max
= front_len
;
2400 m
->front_is_vmalloc
= false;
2401 m
->more_to_follow
= false;
2410 m
->page_alignment
= 0;
2420 if (front_len
> PAGE_CACHE_SIZE
) {
2421 m
->front
.iov_base
= __vmalloc(front_len
, flags
,
2423 m
->front_is_vmalloc
= true;
2425 m
->front
.iov_base
= kmalloc(front_len
, flags
);
2427 if (m
->front
.iov_base
== NULL
) {
2428 dout("ceph_msg_new can't allocate %d bytes\n",
2433 m
->front
.iov_base
= NULL
;
2435 m
->front
.iov_len
= front_len
;
2437 dout("ceph_msg_new %p front %d\n", m
, front_len
);
2444 pr_err("msg_new can't create type %d front %d\n", type
,
2448 dout("msg_new can't create type %d front %d\n", type
,
2453 EXPORT_SYMBOL(ceph_msg_new
);
2456 * Allocate "middle" portion of a message, if it is needed and wasn't
2457 * allocated by alloc_msg. This allows us to read a small fixed-size
2458 * per-type header in the front and then gracefully fail (i.e.,
2459 * propagate the error to the caller based on info in the front) when
2460 * the middle is too large.
2462 static int ceph_alloc_middle(struct ceph_connection
*con
, struct ceph_msg
*msg
)
2464 int type
= le16_to_cpu(msg
->hdr
.type
);
2465 int middle_len
= le32_to_cpu(msg
->hdr
.middle_len
);
2467 dout("alloc_middle %p type %d %s middle_len %d\n", msg
, type
,
2468 ceph_msg_type_name(type
), middle_len
);
2469 BUG_ON(!middle_len
);
2470 BUG_ON(msg
->middle
);
2472 msg
->middle
= ceph_buffer_new(middle_len
, GFP_NOFS
);
2479 * Generic message allocator, for incoming messages.
2481 static struct ceph_msg
*ceph_alloc_msg(struct ceph_connection
*con
,
2482 struct ceph_msg_header
*hdr
,
2485 int type
= le16_to_cpu(hdr
->type
);
2486 int front_len
= le32_to_cpu(hdr
->front_len
);
2487 int middle_len
= le32_to_cpu(hdr
->middle_len
);
2488 struct ceph_msg
*msg
= NULL
;
2491 if (con
->ops
->alloc_msg
) {
2492 mutex_unlock(&con
->mutex
);
2493 msg
= con
->ops
->alloc_msg(con
, hdr
, skip
);
2494 mutex_lock(&con
->mutex
);
2500 msg
= ceph_msg_new(type
, front_len
, GFP_NOFS
, false);
2502 pr_err("unable to allocate msg type %d len %d\n",
2506 msg
->page_alignment
= le16_to_cpu(hdr
->data_off
);
2508 memcpy(&msg
->hdr
, &con
->in_hdr
, sizeof(con
->in_hdr
));
2510 if (middle_len
&& !msg
->middle
) {
2511 ret
= ceph_alloc_middle(con
, msg
);
2523 * Free a generically kmalloc'd message.
2525 void ceph_msg_kfree(struct ceph_msg
*m
)
2527 dout("msg_kfree %p\n", m
);
2528 if (m
->front_is_vmalloc
)
2529 vfree(m
->front
.iov_base
);
2531 kfree(m
->front
.iov_base
);
2536 * Drop a msg ref. Destroy as needed.
2538 void ceph_msg_last_put(struct kref
*kref
)
2540 struct ceph_msg
*m
= container_of(kref
, struct ceph_msg
, kref
);
2542 dout("ceph_msg_put last one on %p\n", m
);
2543 WARN_ON(!list_empty(&m
->list_head
));
2545 /* drop middle, data, if any */
2547 ceph_buffer_put(m
->middle
);
2554 ceph_pagelist_release(m
->pagelist
);
2562 ceph_msgpool_put(m
->pool
, m
);
2566 EXPORT_SYMBOL(ceph_msg_last_put
);
2568 void ceph_msg_dump(struct ceph_msg
*msg
)
2570 pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg
,
2571 msg
->front_max
, msg
->nr_pages
);
2572 print_hex_dump(KERN_DEBUG
, "header: ",
2573 DUMP_PREFIX_OFFSET
, 16, 1,
2574 &msg
->hdr
, sizeof(msg
->hdr
), true);
2575 print_hex_dump(KERN_DEBUG
, " front: ",
2576 DUMP_PREFIX_OFFSET
, 16, 1,
2577 msg
->front
.iov_base
, msg
->front
.iov_len
, true);
2579 print_hex_dump(KERN_DEBUG
, "middle: ",
2580 DUMP_PREFIX_OFFSET
, 16, 1,
2581 msg
->middle
->vec
.iov_base
,
2582 msg
->middle
->vec
.iov_len
, true);
2583 print_hex_dump(KERN_DEBUG
, "footer: ",
2584 DUMP_PREFIX_OFFSET
, 16, 1,
2585 &msg
->footer
, sizeof(msg
->footer
), true);
2587 EXPORT_SYMBOL(ceph_msg_dump
);