Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 10 | #include <linux/socket.h> |
| 11 | #include <linux/string.h> |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 12 | #include <linux/bio.h> |
| 13 | #include <linux/blkdev.h> |
Noah Watkins | ee3b56f | 2011-09-23 11:48:42 -0700 | [diff] [blame] | 14 | #include <linux/dns_resolver.h> |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 15 | #include <net/tcp.h> |
| 16 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 17 | #include <linux/ceph/libceph.h> |
| 18 | #include <linux/ceph/messenger.h> |
| 19 | #include <linux/ceph/decode.h> |
| 20 | #include <linux/ceph/pagelist.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 21 | #include <linux/export.h> |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | * Ceph uses the messenger to exchange ceph_msg messages with other |
| 25 | * hosts in the system. The messenger provides ordered and reliable |
| 26 | * delivery. We tolerate TCP disconnects by reconnecting (with |
| 27 | * exponential backoff) in the case of a fault (disconnection, bad |
| 28 | * crc, protocol error). Acks allow sent messages to be discarded by |
| 29 | * the sender. |
| 30 | */ |
| 31 | |
Alex Elder | bc18f4b | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 32 | /* |
| 33 | * We track the state of the socket on a given connection using |
| 34 | * values defined below. The transition to a new socket state is |
| 35 | * handled by a function which verifies we aren't coming from an |
| 36 | * unexpected state. |
| 37 | * |
| 38 | * -------- |
| 39 | * | NEW* | transient initial state |
| 40 | * -------- |
| 41 | * | con_sock_state_init() |
| 42 | * v |
| 43 | * ---------- |
| 44 | * | CLOSED | initialized, but no socket (and no |
| 45 | * ---------- TCP connection) |
| 46 | * ^ \ |
| 47 | * | \ con_sock_state_connecting() |
| 48 | * | ---------------------- |
| 49 | * | \ |
| 50 | * + con_sock_state_closed() \ |
Sage Weil | fbb85a4 | 2012-06-27 12:31:02 -0700 | [diff] [blame] | 51 | * |+--------------------------- \ |
| 52 | * | \ \ \ |
| 53 | * | ----------- \ \ |
| 54 | * | | CLOSING | socket event; \ \ |
| 55 | * | ----------- await close \ \ |
| 56 | * | ^ \ | |
| 57 | * | | \ | |
| 58 | * | + con_sock_state_closing() \ | |
| 59 | * | / \ | | |
| 60 | * | / --------------- | | |
| 61 | * | / \ v v |
Alex Elder | bc18f4b | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 62 | * | / -------------- |
| 63 | * | / -----------------| CONNECTING | socket created, TCP |
| 64 | * | | / -------------- connect initiated |
| 65 | * | | | con_sock_state_connected() |
| 66 | * | | v |
| 67 | * ------------- |
| 68 | * | CONNECTED | TCP connection established |
| 69 | * ------------- |
| 70 | * |
| 71 | * State values for ceph_connection->sock_state; NEW is assumed to be 0. |
| 72 | */ |
Alex Elder | ce2c890 | 2012-05-22 22:15:49 -0500 | [diff] [blame] | 73 | |
| 74 | #define CON_SOCK_STATE_NEW 0 /* -> CLOSED */ |
| 75 | #define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */ |
| 76 | #define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */ |
| 77 | #define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */ |
| 78 | #define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */ |
| 79 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 80 | /* static tag bytes (protocol control messages) */ |
| 81 | static char tag_msg = CEPH_MSGR_TAG_MSG; |
| 82 | static char tag_ack = CEPH_MSGR_TAG_ACK; |
| 83 | static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE; |
| 84 | |
Sage Weil | a6a5349 | 2010-04-13 14:07:07 -0700 | [diff] [blame] | 85 | #ifdef CONFIG_LOCKDEP |
| 86 | static struct lock_class_key socket_class; |
| 87 | #endif |
| 88 | |
Alex Elder | 84495f4 | 2012-02-15 07:43:55 -0600 | [diff] [blame] | 89 | /* |
| 90 | * When skipping (ignoring) a block of input we read it into a "skip |
| 91 | * buffer," which is this many bytes in size. |
| 92 | */ |
| 93 | #define SKIP_BUF_SIZE 1024 |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 94 | |
| 95 | static void queue_con(struct ceph_connection *con); |
| 96 | static void con_work(struct work_struct *); |
| 97 | static void ceph_fault(struct ceph_connection *con); |
| 98 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 99 | /* |
Alex Elder | f64a931 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 100 | * Nicely render a sockaddr as a string. An array of formatted |
| 101 | * strings is used, to approximate reentrancy. |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 102 | */ |
Alex Elder | f64a931 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 103 | #define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */ |
| 104 | #define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG) |
| 105 | #define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1) |
| 106 | #define MAX_ADDR_STR_LEN 64 /* 54 is enough */ |
| 107 | |
| 108 | static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN]; |
| 109 | static atomic_t addr_str_seq = ATOMIC_INIT(0); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 110 | |
Alex Elder | 5766651 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 111 | static struct page *zero_page; /* used in certain error cases */ |
Alex Elder | 5766651 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 112 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 113 | const char *ceph_pr_addr(const struct sockaddr_storage *ss) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 114 | { |
| 115 | int i; |
| 116 | char *s; |
Alex Elder | 99f0f3b | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 117 | struct sockaddr_in *in4 = (struct sockaddr_in *) ss; |
| 118 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 119 | |
Alex Elder | f64a931 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 120 | i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 121 | s = addr_str[i]; |
| 122 | |
| 123 | switch (ss->ss_family) { |
| 124 | case AF_INET: |
Alex Elder | bd40614 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 125 | snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr, |
| 126 | ntohs(in4->sin_port)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 127 | break; |
| 128 | |
| 129 | case AF_INET6: |
Alex Elder | bd40614 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 130 | snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr, |
| 131 | ntohs(in6->sin6_port)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 132 | break; |
| 133 | |
| 134 | default: |
Alex Elder | d3002b9 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 135 | snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)", |
| 136 | ss->ss_family); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | return s; |
| 140 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 141 | EXPORT_SYMBOL(ceph_pr_addr); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 142 | |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 143 | static void encode_my_addr(struct ceph_messenger *msgr) |
| 144 | { |
| 145 | memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr)); |
| 146 | ceph_encode_addr(&msgr->my_enc_addr); |
| 147 | } |
| 148 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 149 | /* |
| 150 | * work queue for all reading and writing to/from the socket. |
| 151 | */ |
Alex Elder | e0f43c9 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 152 | static struct workqueue_struct *ceph_msgr_wq; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 153 | |
Alex Elder | 6173d1f | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 154 | void _ceph_msgr_exit(void) |
| 155 | { |
Alex Elder | d3002b9 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 156 | if (ceph_msgr_wq) { |
Alex Elder | 6173d1f | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 157 | destroy_workqueue(ceph_msgr_wq); |
Alex Elder | d3002b9 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 158 | ceph_msgr_wq = NULL; |
| 159 | } |
Alex Elder | 6173d1f | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 160 | |
Alex Elder | 6173d1f | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 161 | BUG_ON(zero_page == NULL); |
| 162 | kunmap(zero_page); |
| 163 | page_cache_release(zero_page); |
| 164 | zero_page = NULL; |
| 165 | } |
| 166 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 167 | int ceph_msgr_init(void) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 168 | { |
Alex Elder | 5766651 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 169 | BUG_ON(zero_page != NULL); |
| 170 | zero_page = ZERO_PAGE(0); |
| 171 | page_cache_get(zero_page); |
| 172 | |
Tejun Heo | f363e45fd1 | 2011-01-03 14:49:46 +0100 | [diff] [blame] | 173 | ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_NON_REENTRANT, 0); |
Alex Elder | 6173d1f | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 174 | if (ceph_msgr_wq) |
| 175 | return 0; |
Alex Elder | 5766651 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 176 | |
Alex Elder | 6173d1f | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 177 | pr_err("msgr_init failed to create workqueue\n"); |
| 178 | _ceph_msgr_exit(); |
Alex Elder | 5766651 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 179 | |
Alex Elder | 6173d1f | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 180 | return -ENOMEM; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 181 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 182 | EXPORT_SYMBOL(ceph_msgr_init); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 183 | |
| 184 | void ceph_msgr_exit(void) |
| 185 | { |
Alex Elder | 5766651 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 186 | BUG_ON(ceph_msgr_wq == NULL); |
Alex Elder | 5766651 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 187 | |
Alex Elder | 6173d1f | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 188 | _ceph_msgr_exit(); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 189 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 190 | EXPORT_SYMBOL(ceph_msgr_exit); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 191 | |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 192 | void ceph_msgr_flush(void) |
Sage Weil | a922d38 | 2010-05-29 09:41:23 -0700 | [diff] [blame] | 193 | { |
| 194 | flush_workqueue(ceph_msgr_wq); |
| 195 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 196 | EXPORT_SYMBOL(ceph_msgr_flush); |
Sage Weil | a922d38 | 2010-05-29 09:41:23 -0700 | [diff] [blame] | 197 | |
Alex Elder | ce2c890 | 2012-05-22 22:15:49 -0500 | [diff] [blame] | 198 | /* Connection socket state transition functions */ |
| 199 | |
| 200 | static void con_sock_state_init(struct ceph_connection *con) |
| 201 | { |
| 202 | int old_state; |
| 203 | |
| 204 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED); |
| 205 | if (WARN_ON(old_state != CON_SOCK_STATE_NEW)) |
| 206 | printk("%s: unexpected old state %d\n", __func__, old_state); |
| 207 | } |
| 208 | |
| 209 | static void con_sock_state_connecting(struct ceph_connection *con) |
| 210 | { |
| 211 | int old_state; |
| 212 | |
| 213 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING); |
| 214 | if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED)) |
| 215 | printk("%s: unexpected old state %d\n", __func__, old_state); |
| 216 | } |
| 217 | |
| 218 | static void con_sock_state_connected(struct ceph_connection *con) |
| 219 | { |
| 220 | int old_state; |
| 221 | |
| 222 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED); |
| 223 | if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING)) |
| 224 | printk("%s: unexpected old state %d\n", __func__, old_state); |
| 225 | } |
| 226 | |
| 227 | static void con_sock_state_closing(struct ceph_connection *con) |
| 228 | { |
| 229 | int old_state; |
| 230 | |
| 231 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING); |
| 232 | if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING && |
| 233 | old_state != CON_SOCK_STATE_CONNECTED && |
| 234 | old_state != CON_SOCK_STATE_CLOSING)) |
| 235 | printk("%s: unexpected old state %d\n", __func__, old_state); |
| 236 | } |
| 237 | |
| 238 | static void con_sock_state_closed(struct ceph_connection *con) |
| 239 | { |
| 240 | int old_state; |
| 241 | |
| 242 | old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED); |
| 243 | if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED && |
Sage Weil | fbb85a4 | 2012-06-27 12:31:02 -0700 | [diff] [blame] | 244 | old_state != CON_SOCK_STATE_CLOSING && |
| 245 | old_state != CON_SOCK_STATE_CONNECTING)) |
Alex Elder | ce2c890 | 2012-05-22 22:15:49 -0500 | [diff] [blame] | 246 | printk("%s: unexpected old state %d\n", __func__, old_state); |
| 247 | } |
Sage Weil | a922d38 | 2010-05-29 09:41:23 -0700 | [diff] [blame] | 248 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 249 | /* |
| 250 | * socket callback functions |
| 251 | */ |
| 252 | |
| 253 | /* data available on socket, or listen socket received a connect */ |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 254 | static void ceph_sock_data_ready(struct sock *sk, int count_unused) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 255 | { |
Alex Elder | bd40614 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 256 | struct ceph_connection *con = sk->sk_user_data; |
Guanjun He | a2a3258 | 2012-07-08 19:50:33 -0700 | [diff] [blame] | 257 | if (atomic_read(&con->msgr->stopping)) { |
| 258 | return; |
| 259 | } |
Alex Elder | bd40614 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 260 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 261 | if (sk->sk_state != TCP_CLOSE_WAIT) { |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 262 | dout("%s on %p state = %lu, queueing work\n", __func__, |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 263 | con, con->state); |
| 264 | queue_con(con); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /* socket has buffer space for writing */ |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 269 | static void ceph_sock_write_space(struct sock *sk) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 270 | { |
Alex Elder | d3002b9 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 271 | struct ceph_connection *con = sk->sk_user_data; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 272 | |
Jim Schutt | 182fac2 | 2012-02-29 08:30:58 -0700 | [diff] [blame] | 273 | /* only queue to workqueue if there is data we want to write, |
| 274 | * and there is sufficient space in the socket buffer to accept |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 275 | * more data. clear SOCK_NOSPACE so that ceph_sock_write_space() |
Jim Schutt | 182fac2 | 2012-02-29 08:30:58 -0700 | [diff] [blame] | 276 | * doesn't get called again until try_write() fills the socket |
| 277 | * buffer. See net/ipv4/tcp_input.c:tcp_check_space() |
| 278 | * and net/core/stream.c:sk_stream_write_space(). |
| 279 | */ |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 280 | if (test_bit(WRITE_PENDING, &con->flags)) { |
Jim Schutt | 182fac2 | 2012-02-29 08:30:58 -0700 | [diff] [blame] | 281 | if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 282 | dout("%s %p queueing write work\n", __func__, con); |
Jim Schutt | 182fac2 | 2012-02-29 08:30:58 -0700 | [diff] [blame] | 283 | clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 284 | queue_con(con); |
| 285 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 286 | } else { |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 287 | dout("%s %p nothing to write\n", __func__, con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 288 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* socket's state has changed */ |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 292 | static void ceph_sock_state_change(struct sock *sk) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 293 | { |
Alex Elder | bd40614 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 294 | struct ceph_connection *con = sk->sk_user_data; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 295 | |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 296 | dout("%s %p state = %lu sk_state = %u\n", __func__, |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 297 | con, con->state, sk->sk_state); |
| 298 | |
| 299 | if (test_bit(CLOSED, &con->state)) |
| 300 | return; |
| 301 | |
| 302 | switch (sk->sk_state) { |
| 303 | case TCP_CLOSE: |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 304 | dout("%s TCP_CLOSE\n", __func__); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 305 | case TCP_CLOSE_WAIT: |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 306 | dout("%s TCP_CLOSE_WAIT\n", __func__); |
Alex Elder | ce2c890 | 2012-05-22 22:15:49 -0500 | [diff] [blame] | 307 | con_sock_state_closing(con); |
Alex Elder | d65c9e0 | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 308 | set_bit(SOCK_CLOSED, &con->flags); |
| 309 | queue_con(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 310 | break; |
| 311 | case TCP_ESTABLISHED: |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 312 | dout("%s TCP_ESTABLISHED\n", __func__); |
Alex Elder | ce2c890 | 2012-05-22 22:15:49 -0500 | [diff] [blame] | 313 | con_sock_state_connected(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 314 | queue_con(con); |
| 315 | break; |
Alex Elder | d3002b9 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 316 | default: /* Everything else is uninteresting */ |
| 317 | break; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * set up socket callbacks |
| 323 | */ |
| 324 | static void set_sock_callbacks(struct socket *sock, |
| 325 | struct ceph_connection *con) |
| 326 | { |
| 327 | struct sock *sk = sock->sk; |
Alex Elder | bd40614 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 328 | sk->sk_user_data = con; |
Alex Elder | 327800b | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 329 | sk->sk_data_ready = ceph_sock_data_ready; |
| 330 | sk->sk_write_space = ceph_sock_write_space; |
| 331 | sk->sk_state_change = ceph_sock_state_change; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | |
| 335 | /* |
| 336 | * socket helpers |
| 337 | */ |
| 338 | |
| 339 | /* |
| 340 | * initiate connection to a remote socket. |
| 341 | */ |
Alex Elder | 41617d0 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 342 | static int ceph_tcp_connect(struct ceph_connection *con) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 343 | { |
Sage Weil | f91d347 | 2010-07-01 15:18:31 -0700 | [diff] [blame] | 344 | struct sockaddr_storage *paddr = &con->peer_addr.in_addr; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 345 | struct socket *sock; |
| 346 | int ret; |
| 347 | |
| 348 | BUG_ON(con->sock); |
Sage Weil | f91d347 | 2010-07-01 15:18:31 -0700 | [diff] [blame] | 349 | ret = sock_create_kern(con->peer_addr.in_addr.ss_family, SOCK_STREAM, |
| 350 | IPPROTO_TCP, &sock); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 351 | if (ret) |
Alex Elder | 41617d0 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 352 | return ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 353 | sock->sk->sk_allocation = GFP_NOFS; |
| 354 | |
Sage Weil | a6a5349 | 2010-04-13 14:07:07 -0700 | [diff] [blame] | 355 | #ifdef CONFIG_LOCKDEP |
| 356 | lockdep_set_class(&sock->sk->sk_lock, &socket_class); |
| 357 | #endif |
| 358 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 359 | set_sock_callbacks(sock, con); |
| 360 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 361 | dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 362 | |
Sage Weil | 89a86be | 2012-06-09 14:19:21 -0700 | [diff] [blame] | 363 | con_sock_state_connecting(con); |
Sage Weil | f91d347 | 2010-07-01 15:18:31 -0700 | [diff] [blame] | 364 | ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr), |
| 365 | O_NONBLOCK); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 366 | if (ret == -EINPROGRESS) { |
| 367 | dout("connect %s EINPROGRESS sk_state = %u\n", |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 368 | ceph_pr_addr(&con->peer_addr.in_addr), |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 369 | sock->sk->sk_state); |
Alex Elder | a5bc312 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 370 | } else if (ret < 0) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 371 | pr_err("connect %s error %d\n", |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 372 | ceph_pr_addr(&con->peer_addr.in_addr), ret); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 373 | sock_release(sock); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 374 | con->error_msg = "connect error"; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 375 | |
Alex Elder | 41617d0 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 376 | return ret; |
Alex Elder | a5bc312 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 377 | } |
| 378 | con->sock = sock; |
Alex Elder | 41617d0 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 379 | return 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len) |
| 383 | { |
| 384 | struct kvec iov = {buf, len}; |
| 385 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 386 | int r; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 387 | |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 388 | r = kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags); |
| 389 | if (r == -EAGAIN) |
| 390 | r = 0; |
| 391 | return r; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /* |
| 395 | * write something. @more is true if caller will be sending more data |
| 396 | * shortly. |
| 397 | */ |
| 398 | static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov, |
| 399 | size_t kvlen, size_t len, int more) |
| 400 | { |
| 401 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; |
Sage Weil | 42961d2 | 2011-01-25 08:19:34 -0800 | [diff] [blame] | 402 | int r; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 403 | |
| 404 | if (more) |
| 405 | msg.msg_flags |= MSG_MORE; |
| 406 | else |
| 407 | msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */ |
| 408 | |
Sage Weil | 42961d2 | 2011-01-25 08:19:34 -0800 | [diff] [blame] | 409 | r = kernel_sendmsg(sock, &msg, iov, kvlen, len); |
| 410 | if (r == -EAGAIN) |
| 411 | r = 0; |
| 412 | return r; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Alex Elder | 3173913 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 415 | static int ceph_tcp_sendpage(struct socket *sock, struct page *page, |
| 416 | int offset, size_t size, int more) |
| 417 | { |
| 418 | int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR); |
| 419 | int ret; |
| 420 | |
| 421 | ret = kernel_sendpage(sock, page, offset, size, flags); |
| 422 | if (ret == -EAGAIN) |
| 423 | ret = 0; |
| 424 | |
| 425 | return ret; |
| 426 | } |
| 427 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * Shutdown/close the socket for the given connection. |
| 431 | */ |
| 432 | static int con_close_socket(struct ceph_connection *con) |
| 433 | { |
| 434 | int rc; |
| 435 | |
| 436 | dout("con_close_socket on %p sock %p\n", con, con->sock); |
| 437 | if (!con->sock) |
| 438 | return 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 439 | rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR); |
| 440 | sock_release(con->sock); |
| 441 | con->sock = NULL; |
Alex Elder | 456ea46 | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 442 | |
| 443 | /* |
| 444 | * Forcibly clear the SOCK_CLOSE flag. It gets set |
| 445 | * independent of the connection mutex, and we could have |
| 446 | * received a socket close event before we had the chance to |
| 447 | * shut the socket down. |
| 448 | */ |
Alex Elder | a8d00e3 | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 449 | clear_bit(SOCK_CLOSED, &con->flags); |
Alex Elder | ce2c890 | 2012-05-22 22:15:49 -0500 | [diff] [blame] | 450 | con_sock_state_closed(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 451 | return rc; |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Reset a connection. Discard all incoming and outgoing messages |
| 456 | * and clear *_seq state. |
| 457 | */ |
| 458 | static void ceph_msg_remove(struct ceph_msg *msg) |
| 459 | { |
| 460 | list_del_init(&msg->list_head); |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 461 | BUG_ON(msg->con == NULL); |
Sage Weil | 36eb71a | 2012-06-21 12:47:08 -0700 | [diff] [blame] | 462 | msg->con->ops->put(msg->con); |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 463 | msg->con = NULL; |
| 464 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 465 | ceph_msg_put(msg); |
| 466 | } |
| 467 | static void ceph_msg_remove_list(struct list_head *head) |
| 468 | { |
| 469 | while (!list_empty(head)) { |
| 470 | struct ceph_msg *msg = list_first_entry(head, struct ceph_msg, |
| 471 | list_head); |
| 472 | ceph_msg_remove(msg); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | static void reset_connection(struct ceph_connection *con) |
| 477 | { |
| 478 | /* reset connection, out_queue, msg_ and connect_seq */ |
| 479 | /* discard existing out_queue and msg_seq */ |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 480 | ceph_msg_remove_list(&con->out_queue); |
| 481 | ceph_msg_remove_list(&con->out_sent); |
| 482 | |
Sage Weil | cf3e5c4 | 2009-12-11 09:48:05 -0800 | [diff] [blame] | 483 | if (con->in_msg) { |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 484 | BUG_ON(con->in_msg->con != con); |
| 485 | con->in_msg->con = NULL; |
Sage Weil | cf3e5c4 | 2009-12-11 09:48:05 -0800 | [diff] [blame] | 486 | ceph_msg_put(con->in_msg); |
| 487 | con->in_msg = NULL; |
Sage Weil | 36eb71a | 2012-06-21 12:47:08 -0700 | [diff] [blame] | 488 | con->ops->put(con); |
Sage Weil | cf3e5c4 | 2009-12-11 09:48:05 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 491 | con->connect_seq = 0; |
| 492 | con->out_seq = 0; |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 493 | if (con->out_msg) { |
| 494 | ceph_msg_put(con->out_msg); |
| 495 | con->out_msg = NULL; |
| 496 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 497 | con->in_seq = 0; |
Sage Weil | 0e0d5e0 | 2010-04-02 16:07:19 -0700 | [diff] [blame] | 498 | con->in_seq_acked = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /* |
| 502 | * mark a peer down. drop any open connections. |
| 503 | */ |
| 504 | void ceph_con_close(struct ceph_connection *con) |
| 505 | { |
Sage Weil | 8c50c81 | 2012-07-30 16:24:37 -0700 | [diff] [blame] | 506 | mutex_lock(&con->mutex); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 507 | dout("con_close %p peer %s\n", con, |
| 508 | ceph_pr_addr(&con->peer_addr.in_addr)); |
Alex Elder | a5988c4 | 2012-05-29 11:04:58 -0500 | [diff] [blame] | 509 | clear_bit(NEGOTIATING, &con->state); |
Alex Elder | bb9e6bb | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 510 | clear_bit(CONNECTING, &con->state); |
Alex Elder | e27947c | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 511 | clear_bit(CONNECTED, &con->state); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 512 | clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */ |
Alex Elder | a5988c4 | 2012-05-29 11:04:58 -0500 | [diff] [blame] | 513 | set_bit(CLOSED, &con->state); |
| 514 | |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 515 | clear_bit(LOSSYTX, &con->flags); /* so we retry next connect */ |
| 516 | clear_bit(KEEPALIVE_PENDING, &con->flags); |
| 517 | clear_bit(WRITE_PENDING, &con->flags); |
Alex Elder | a5988c4 | 2012-05-29 11:04:58 -0500 | [diff] [blame] | 518 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 519 | reset_connection(con); |
Sage Weil | 6f2bc3f | 2010-04-02 16:16:34 -0700 | [diff] [blame] | 520 | con->peer_global_seq = 0; |
Sage Weil | 91e45ce3 | 2010-02-15 12:05:09 -0800 | [diff] [blame] | 521 | cancel_delayed_work(&con->work); |
Sage Weil | ee76e07 | 2012-07-20 16:45:49 -0700 | [diff] [blame^] | 522 | con_close_socket(con); |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 523 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 524 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 525 | EXPORT_SYMBOL(ceph_con_close); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 526 | |
| 527 | /* |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 528 | * Reopen a closed connection, with a new peer address. |
| 529 | */ |
Sage Weil | b7a9e5d | 2012-06-27 12:24:08 -0700 | [diff] [blame] | 530 | void ceph_con_open(struct ceph_connection *con, |
| 531 | __u8 entity_type, __u64 entity_num, |
| 532 | struct ceph_entity_addr *addr) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 533 | { |
Sage Weil | 5469155 | 2012-07-30 16:21:40 -0700 | [diff] [blame] | 534 | mutex_lock(&con->mutex); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 535 | dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 536 | set_bit(OPENING, &con->state); |
Alex Elder | a5988c4 | 2012-05-29 11:04:58 -0500 | [diff] [blame] | 537 | WARN_ON(!test_and_clear_bit(CLOSED, &con->state)); |
| 538 | |
Sage Weil | b7a9e5d | 2012-06-27 12:24:08 -0700 | [diff] [blame] | 539 | con->peer_name.type = (__u8) entity_type; |
| 540 | con->peer_name.num = cpu_to_le64(entity_num); |
| 541 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 542 | memcpy(&con->peer_addr, addr, sizeof(*addr)); |
Sage Weil | 03c677e | 2009-11-20 15:14:15 -0800 | [diff] [blame] | 543 | con->delay = 0; /* reset backoff memory */ |
Sage Weil | 5469155 | 2012-07-30 16:21:40 -0700 | [diff] [blame] | 544 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 545 | queue_con(con); |
| 546 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 547 | EXPORT_SYMBOL(ceph_con_open); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 548 | |
| 549 | /* |
Sage Weil | 87b315a | 2010-03-22 14:51:18 -0700 | [diff] [blame] | 550 | * return true if this connection ever successfully opened |
| 551 | */ |
| 552 | bool ceph_con_opened(struct ceph_connection *con) |
| 553 | { |
| 554 | return con->connect_seq > 0; |
| 555 | } |
| 556 | |
| 557 | /* |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 558 | * initialize a new connection. |
| 559 | */ |
Alex Elder | 1bfd89f | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 560 | void ceph_con_init(struct ceph_connection *con, void *private, |
| 561 | const struct ceph_connection_operations *ops, |
Sage Weil | b7a9e5d | 2012-06-27 12:24:08 -0700 | [diff] [blame] | 562 | struct ceph_messenger *msgr) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 563 | { |
| 564 | dout("con_init %p\n", con); |
| 565 | memset(con, 0, sizeof(*con)); |
Alex Elder | 1bfd89f | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 566 | con->private = private; |
| 567 | con->ops = ops; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 568 | con->msgr = msgr; |
Alex Elder | ce2c890 | 2012-05-22 22:15:49 -0500 | [diff] [blame] | 569 | |
| 570 | con_sock_state_init(con); |
| 571 | |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 572 | mutex_init(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 573 | INIT_LIST_HEAD(&con->out_queue); |
| 574 | INIT_LIST_HEAD(&con->out_sent); |
| 575 | INIT_DELAYED_WORK(&con->work, con_work); |
Alex Elder | a5988c4 | 2012-05-29 11:04:58 -0500 | [diff] [blame] | 576 | |
| 577 | set_bit(CLOSED, &con->state); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 578 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 579 | EXPORT_SYMBOL(ceph_con_init); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 580 | |
| 581 | |
| 582 | /* |
| 583 | * We maintain a global counter to order connection attempts. Get |
| 584 | * a unique seq greater than @gt. |
| 585 | */ |
| 586 | static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt) |
| 587 | { |
| 588 | u32 ret; |
| 589 | |
| 590 | spin_lock(&msgr->global_seq_lock); |
| 591 | if (msgr->global_seq < gt) |
| 592 | msgr->global_seq = gt; |
| 593 | ret = ++msgr->global_seq; |
| 594 | spin_unlock(&msgr->global_seq_lock); |
| 595 | return ret; |
| 596 | } |
| 597 | |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 598 | static void con_out_kvec_reset(struct ceph_connection *con) |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 599 | { |
| 600 | con->out_kvec_left = 0; |
| 601 | con->out_kvec_bytes = 0; |
| 602 | con->out_kvec_cur = &con->out_kvec[0]; |
| 603 | } |
| 604 | |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 605 | static void con_out_kvec_add(struct ceph_connection *con, |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 606 | size_t size, void *data) |
| 607 | { |
| 608 | int index; |
| 609 | |
| 610 | index = con->out_kvec_left; |
| 611 | BUG_ON(index >= ARRAY_SIZE(con->out_kvec)); |
| 612 | |
| 613 | con->out_kvec[index].iov_len = size; |
| 614 | con->out_kvec[index].iov_base = data; |
| 615 | con->out_kvec_left++; |
| 616 | con->out_kvec_bytes += size; |
| 617 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 618 | |
Alex Elder | df6ad1f | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 619 | #ifdef CONFIG_BLOCK |
| 620 | static void init_bio_iter(struct bio *bio, struct bio **iter, int *seg) |
| 621 | { |
| 622 | if (!bio) { |
| 623 | *iter = NULL; |
| 624 | *seg = 0; |
| 625 | return; |
| 626 | } |
| 627 | *iter = bio; |
| 628 | *seg = bio->bi_idx; |
| 629 | } |
| 630 | |
| 631 | static void iter_bio_next(struct bio **bio_iter, int *seg) |
| 632 | { |
| 633 | if (*bio_iter == NULL) |
| 634 | return; |
| 635 | |
| 636 | BUG_ON(*seg >= (*bio_iter)->bi_vcnt); |
| 637 | |
| 638 | (*seg)++; |
| 639 | if (*seg == (*bio_iter)->bi_vcnt) |
| 640 | init_bio_iter((*bio_iter)->bi_next, bio_iter, seg); |
| 641 | } |
| 642 | #endif |
| 643 | |
Alex Elder | 739c905 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 644 | static void prepare_write_message_data(struct ceph_connection *con) |
| 645 | { |
| 646 | struct ceph_msg *msg = con->out_msg; |
| 647 | |
| 648 | BUG_ON(!msg); |
| 649 | BUG_ON(!msg->hdr.data_len); |
| 650 | |
| 651 | /* initialize page iterator */ |
| 652 | con->out_msg_pos.page = 0; |
| 653 | if (msg->pages) |
| 654 | con->out_msg_pos.page_pos = msg->page_alignment; |
| 655 | else |
| 656 | con->out_msg_pos.page_pos = 0; |
Alex Elder | 572c588 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 657 | #ifdef CONFIG_BLOCK |
Alex Elder | abdaa6a | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 658 | if (msg->bio) |
Alex Elder | 572c588 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 659 | init_bio_iter(msg->bio, &msg->bio_iter, &msg->bio_seg); |
| 660 | #endif |
Alex Elder | 739c905 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 661 | con->out_msg_pos.data_pos = 0; |
| 662 | con->out_msg_pos.did_page_crc = false; |
| 663 | con->out_more = 1; /* data + footer will follow */ |
| 664 | } |
| 665 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 666 | /* |
| 667 | * Prepare footer for currently outgoing message, and finish things |
| 668 | * off. Assumes out_kvec* are already valid.. we just add on to the end. |
| 669 | */ |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 670 | static void prepare_write_message_footer(struct ceph_connection *con) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 671 | { |
| 672 | struct ceph_msg *m = con->out_msg; |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 673 | int v = con->out_kvec_left; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 674 | |
Alex Elder | fd154f3 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 675 | m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE; |
| 676 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 677 | dout("prepare_write_message_footer %p\n", con); |
| 678 | con->out_kvec_is_msg = true; |
| 679 | con->out_kvec[v].iov_base = &m->footer; |
| 680 | con->out_kvec[v].iov_len = sizeof(m->footer); |
| 681 | con->out_kvec_bytes += sizeof(m->footer); |
| 682 | con->out_kvec_left++; |
| 683 | con->out_more = m->more_to_follow; |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 684 | con->out_msg_done = true; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /* |
| 688 | * Prepare headers for the next outgoing message. |
| 689 | */ |
| 690 | static void prepare_write_message(struct ceph_connection *con) |
| 691 | { |
| 692 | struct ceph_msg *m; |
Alex Elder | a9a0c51 | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 693 | u32 crc; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 694 | |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 695 | con_out_kvec_reset(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 696 | con->out_kvec_is_msg = true; |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 697 | con->out_msg_done = false; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 698 | |
| 699 | /* Sneak an ack in there first? If we can get it into the same |
| 700 | * TCP packet that's a good thing. */ |
| 701 | if (con->in_seq > con->in_seq_acked) { |
| 702 | con->in_seq_acked = con->in_seq; |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 703 | con_out_kvec_add(con, sizeof (tag_ack), &tag_ack); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 704 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 705 | con_out_kvec_add(con, sizeof (con->out_temp_ack), |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 706 | &con->out_temp_ack); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 709 | BUG_ON(list_empty(&con->out_queue)); |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 710 | m = list_first_entry(&con->out_queue, struct ceph_msg, list_head); |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 711 | con->out_msg = m; |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 712 | BUG_ON(m->con != con); |
Sage Weil | 4cf9d54 | 2011-07-26 11:27:24 -0700 | [diff] [blame] | 713 | |
| 714 | /* put message on sent list */ |
| 715 | ceph_msg_get(m); |
| 716 | list_move_tail(&m->list_head, &con->out_sent); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 717 | |
Sage Weil | e84346b | 2010-05-11 21:20:38 -0700 | [diff] [blame] | 718 | /* |
| 719 | * only assign outgoing seq # if we haven't sent this message |
| 720 | * yet. if it is requeued, resend with it's original seq. |
| 721 | */ |
| 722 | if (m->needs_out_seq) { |
| 723 | m->hdr.seq = cpu_to_le64(++con->out_seq); |
| 724 | m->needs_out_seq = false; |
| 725 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 726 | |
| 727 | dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n", |
| 728 | m, con->out_seq, le16_to_cpu(m->hdr.type), |
| 729 | le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len), |
| 730 | le32_to_cpu(m->hdr.data_len), |
| 731 | m->nr_pages); |
| 732 | BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len); |
| 733 | |
| 734 | /* tag + hdr + front + middle */ |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 735 | con_out_kvec_add(con, sizeof (tag_msg), &tag_msg); |
| 736 | con_out_kvec_add(con, sizeof (m->hdr), &m->hdr); |
| 737 | con_out_kvec_add(con, m->front.iov_len, m->front.iov_base); |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 738 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 739 | if (m->middle) |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 740 | con_out_kvec_add(con, m->middle->vec.iov_len, |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 741 | m->middle->vec.iov_base); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 742 | |
| 743 | /* fill in crc (except data pages), footer */ |
Alex Elder | a9a0c51 | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 744 | crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc)); |
| 745 | con->out_msg->hdr.crc = cpu_to_le32(crc); |
Alex Elder | fd154f3 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 746 | con->out_msg->footer.flags = 0; |
Alex Elder | a9a0c51 | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 747 | |
| 748 | crc = crc32c(0, m->front.iov_base, m->front.iov_len); |
| 749 | con->out_msg->footer.front_crc = cpu_to_le32(crc); |
| 750 | if (m->middle) { |
| 751 | crc = crc32c(0, m->middle->vec.iov_base, |
| 752 | m->middle->vec.iov_len); |
| 753 | con->out_msg->footer.middle_crc = cpu_to_le32(crc); |
| 754 | } else |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 755 | con->out_msg->footer.middle_crc = 0; |
Alex Elder | 739c905 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 756 | dout("%s front_crc %u middle_crc %u\n", __func__, |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 757 | le32_to_cpu(con->out_msg->footer.front_crc), |
| 758 | le32_to_cpu(con->out_msg->footer.middle_crc)); |
| 759 | |
| 760 | /* is there a data payload? */ |
Alex Elder | 739c905 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 761 | con->out_msg->footer.data_crc = 0; |
| 762 | if (m->hdr.data_len) |
| 763 | prepare_write_message_data(con); |
| 764 | else |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 765 | /* no, queue up footer too and be done */ |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 766 | prepare_write_message_footer(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 767 | |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 768 | set_bit(WRITE_PENDING, &con->flags); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | /* |
| 772 | * Prepare an ack. |
| 773 | */ |
| 774 | static void prepare_write_ack(struct ceph_connection *con) |
| 775 | { |
| 776 | dout("prepare_write_ack %p %llu -> %llu\n", con, |
| 777 | con->in_seq_acked, con->in_seq); |
| 778 | con->in_seq_acked = con->in_seq; |
| 779 | |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 780 | con_out_kvec_reset(con); |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 781 | |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 782 | con_out_kvec_add(con, sizeof (tag_ack), &tag_ack); |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 783 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 784 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 785 | con_out_kvec_add(con, sizeof (con->out_temp_ack), |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 786 | &con->out_temp_ack); |
| 787 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 788 | con->out_more = 1; /* more will follow.. eventually.. */ |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 789 | set_bit(WRITE_PENDING, &con->flags); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /* |
| 793 | * Prepare to write keepalive byte. |
| 794 | */ |
| 795 | static void prepare_write_keepalive(struct ceph_connection *con) |
| 796 | { |
| 797 | dout("prepare_write_keepalive %p\n", con); |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 798 | con_out_kvec_reset(con); |
| 799 | con_out_kvec_add(con, sizeof (tag_keepalive), &tag_keepalive); |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 800 | set_bit(WRITE_PENDING, &con->flags); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /* |
| 804 | * Connection negotiation. |
| 805 | */ |
| 806 | |
Alex Elder | dac1e71 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 807 | static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con, |
| 808 | int *auth_proto) |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 809 | { |
Alex Elder | a3530df | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 810 | struct ceph_auth_handshake *auth; |
Alex Elder | b1c6b98 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 811 | |
| 812 | if (!con->ops->get_authorizer) { |
| 813 | con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN; |
| 814 | con->out_connect.authorizer_len = 0; |
| 815 | |
Alex Elder | 729796b | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 816 | return NULL; |
Alex Elder | b1c6b98 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | /* Can't hold the mutex while getting authorizer */ |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 820 | |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 821 | mutex_unlock(&con->mutex); |
Alex Elder | b1c6b98 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 822 | |
Alex Elder | dac1e71 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 823 | auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry); |
Alex Elder | 8f43fb5 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 824 | |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 825 | mutex_lock(&con->mutex); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 826 | |
Alex Elder | a3530df | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 827 | if (IS_ERR(auth)) |
Alex Elder | 729796b | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 828 | return auth; |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 829 | if (test_bit(CLOSED, &con->state) || test_bit(OPENING, &con->flags)) |
Alex Elder | 729796b | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 830 | return ERR_PTR(-EAGAIN); |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 831 | |
Alex Elder | 8f43fb5 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 832 | con->auth_reply_buf = auth->authorizer_reply_buf; |
| 833 | con->auth_reply_buf_len = auth->authorizer_reply_buf_len; |
| 834 | |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 835 | |
Alex Elder | 729796b | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 836 | return auth; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 837 | } |
| 838 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 839 | /* |
| 840 | * We connected to a peer and are saying hello. |
| 841 | */ |
Alex Elder | e825a66 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 842 | static void prepare_write_banner(struct ceph_connection *con) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 843 | { |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 844 | con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER); |
| 845 | con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr), |
Alex Elder | e825a66 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 846 | &con->msgr->my_enc_addr); |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 847 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 848 | con->out_more = 0; |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 849 | set_bit(WRITE_PENDING, &con->flags); |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 850 | } |
| 851 | |
Alex Elder | e825a66 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 852 | static int prepare_write_connect(struct ceph_connection *con) |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 853 | { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 854 | unsigned int global_seq = get_global_seq(con->msgr, 0); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 855 | int proto; |
Alex Elder | dac1e71 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 856 | int auth_proto; |
Alex Elder | 729796b | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 857 | struct ceph_auth_handshake *auth; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 858 | |
| 859 | switch (con->peer_name.type) { |
| 860 | case CEPH_ENTITY_TYPE_MON: |
| 861 | proto = CEPH_MONC_PROTOCOL; |
| 862 | break; |
| 863 | case CEPH_ENTITY_TYPE_OSD: |
| 864 | proto = CEPH_OSDC_PROTOCOL; |
| 865 | break; |
| 866 | case CEPH_ENTITY_TYPE_MDS: |
| 867 | proto = CEPH_MDSC_PROTOCOL; |
| 868 | break; |
| 869 | default: |
| 870 | BUG(); |
| 871 | } |
| 872 | |
| 873 | dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con, |
| 874 | con->connect_seq, global_seq, proto); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 875 | |
Alex Elder | e825a66 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 876 | con->out_connect.features = cpu_to_le64(con->msgr->supported_features); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 877 | con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT); |
| 878 | con->out_connect.connect_seq = cpu_to_le32(con->connect_seq); |
| 879 | con->out_connect.global_seq = cpu_to_le32(global_seq); |
| 880 | con->out_connect.protocol_version = cpu_to_le32(proto); |
| 881 | con->out_connect.flags = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 882 | |
Alex Elder | dac1e71 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 883 | auth_proto = CEPH_AUTH_UNKNOWN; |
| 884 | auth = get_connect_authorizer(con, &auth_proto); |
Alex Elder | 729796b | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 885 | if (IS_ERR(auth)) |
| 886 | return PTR_ERR(auth); |
Alex Elder | 3da5477 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 887 | |
Alex Elder | dac1e71 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 888 | con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto); |
Alex Elder | 3da5477 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 889 | con->out_connect.authorizer_len = auth ? |
| 890 | cpu_to_le32(auth->authorizer_buf_len) : 0; |
| 891 | |
Alex Elder | ab166d5 | 2012-05-31 11:37:29 -0500 | [diff] [blame] | 892 | con_out_kvec_reset(con); |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 893 | con_out_kvec_add(con, sizeof (con->out_connect), |
Alex Elder | 3da5477 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 894 | &con->out_connect); |
| 895 | if (auth && auth->authorizer_buf_len) |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 896 | con_out_kvec_add(con, auth->authorizer_buf_len, |
Alex Elder | 3da5477 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 897 | auth->authorizer_buf); |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 898 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 899 | con->out_more = 0; |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 900 | set_bit(WRITE_PENDING, &con->flags); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 901 | |
Alex Elder | e10c758 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 902 | return 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 905 | /* |
| 906 | * write as much of pending kvecs to the socket as we can. |
| 907 | * 1 -> done |
| 908 | * 0 -> socket full, but more to do |
| 909 | * <0 -> error |
| 910 | */ |
| 911 | static int write_partial_kvec(struct ceph_connection *con) |
| 912 | { |
| 913 | int ret; |
| 914 | |
| 915 | dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes); |
| 916 | while (con->out_kvec_bytes > 0) { |
| 917 | ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur, |
| 918 | con->out_kvec_left, con->out_kvec_bytes, |
| 919 | con->out_more); |
| 920 | if (ret <= 0) |
| 921 | goto out; |
| 922 | con->out_kvec_bytes -= ret; |
| 923 | if (con->out_kvec_bytes == 0) |
| 924 | break; /* done */ |
Alex Elder | f42299e | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 925 | |
| 926 | /* account for full iov entries consumed */ |
| 927 | while (ret >= con->out_kvec_cur->iov_len) { |
| 928 | BUG_ON(!con->out_kvec_left); |
| 929 | ret -= con->out_kvec_cur->iov_len; |
| 930 | con->out_kvec_cur++; |
| 931 | con->out_kvec_left--; |
| 932 | } |
| 933 | /* and for a partially-consumed entry */ |
| 934 | if (ret) { |
| 935 | con->out_kvec_cur->iov_len -= ret; |
| 936 | con->out_kvec_cur->iov_base += ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | con->out_kvec_left = 0; |
| 940 | con->out_kvec_is_msg = false; |
| 941 | ret = 1; |
| 942 | out: |
| 943 | dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con, |
| 944 | con->out_kvec_bytes, con->out_kvec_left, ret); |
| 945 | return ret; /* done! */ |
| 946 | } |
| 947 | |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 948 | static void out_msg_pos_next(struct ceph_connection *con, struct page *page, |
| 949 | size_t len, size_t sent, bool in_trail) |
| 950 | { |
| 951 | struct ceph_msg *msg = con->out_msg; |
| 952 | |
| 953 | BUG_ON(!msg); |
| 954 | BUG_ON(!sent); |
| 955 | |
| 956 | con->out_msg_pos.data_pos += sent; |
| 957 | con->out_msg_pos.page_pos += sent; |
Alex Elder | 5821bd8 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 958 | if (sent < len) |
| 959 | return; |
| 960 | |
| 961 | BUG_ON(sent != len); |
| 962 | con->out_msg_pos.page_pos = 0; |
| 963 | con->out_msg_pos.page++; |
| 964 | con->out_msg_pos.did_page_crc = false; |
| 965 | if (in_trail) |
| 966 | list_move_tail(&page->lru, |
| 967 | &msg->trail->head); |
| 968 | else if (msg->pagelist) |
| 969 | list_move_tail(&page->lru, |
| 970 | &msg->pagelist->head); |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 971 | #ifdef CONFIG_BLOCK |
Alex Elder | 5821bd8 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 972 | else if (msg->bio) |
| 973 | iter_bio_next(&msg->bio_iter, &msg->bio_seg); |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 974 | #endif |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 975 | } |
| 976 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 977 | /* |
| 978 | * Write as much message data payload as we can. If we finish, queue |
| 979 | * up the footer. |
| 980 | * 1 -> done, footer is now queued in out_kvec[]. |
| 981 | * 0 -> socket full, but more to do |
| 982 | * <0 -> error |
| 983 | */ |
| 984 | static int write_partial_msg_pages(struct ceph_connection *con) |
| 985 | { |
| 986 | struct ceph_msg *msg = con->out_msg; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 987 | unsigned int data_len = le32_to_cpu(msg->hdr.data_len); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 988 | size_t len; |
Alex Elder | 37675b0 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 989 | bool do_datacrc = !con->msgr->nocrc; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 990 | int ret; |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 991 | int total_max_write; |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 992 | bool in_trail = false; |
Alex Elder | 5821bd8 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 993 | const size_t trail_len = (msg->trail ? msg->trail->length : 0); |
| 994 | const size_t trail_off = data_len - trail_len; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 995 | |
| 996 | dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n", |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 997 | con, msg, con->out_msg_pos.page, msg->nr_pages, |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 998 | con->out_msg_pos.page_pos); |
| 999 | |
Alex Elder | 5821bd8 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 1000 | /* |
| 1001 | * Iterate through each page that contains data to be |
| 1002 | * written, and send as much as possible for each. |
| 1003 | * |
| 1004 | * If we are calculating the data crc (the default), we will |
| 1005 | * need to map the page. If we have no pages, they have |
| 1006 | * been revoked, so use the zero page. |
| 1007 | */ |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1008 | while (data_len > con->out_msg_pos.data_pos) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1009 | struct page *page = NULL; |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1010 | int max_write = PAGE_SIZE; |
Alex Elder | 9bd1966 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1011 | int bio_offset = 0; |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1012 | |
Alex Elder | 5821bd8 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 1013 | in_trail = in_trail || con->out_msg_pos.data_pos >= trail_off; |
| 1014 | if (!in_trail) |
| 1015 | total_max_write = trail_off - con->out_msg_pos.data_pos; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1016 | |
Alex Elder | 5821bd8 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 1017 | if (in_trail) { |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1018 | total_max_write = data_len - con->out_msg_pos.data_pos; |
| 1019 | |
| 1020 | page = list_first_entry(&msg->trail->head, |
| 1021 | struct page, lru); |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1022 | } else if (msg->pages) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1023 | page = msg->pages[con->out_msg_pos.page]; |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 1024 | } else if (msg->pagelist) { |
| 1025 | page = list_first_entry(&msg->pagelist->head, |
| 1026 | struct page, lru); |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1027 | #ifdef CONFIG_BLOCK |
| 1028 | } else if (msg->bio) { |
| 1029 | struct bio_vec *bv; |
| 1030 | |
| 1031 | bv = bio_iovec_idx(msg->bio_iter, msg->bio_seg); |
| 1032 | page = bv->bv_page; |
Alex Elder | 9bd1966 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1033 | bio_offset = bv->bv_offset; |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1034 | max_write = bv->bv_len; |
| 1035 | #endif |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1036 | } else { |
Alex Elder | 5766651 | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 1037 | page = zero_page; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1038 | } |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1039 | len = min_t(int, max_write - con->out_msg_pos.page_pos, |
| 1040 | total_max_write); |
| 1041 | |
Alex Elder | 37675b0 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1042 | if (do_datacrc && !con->out_msg_pos.did_page_crc) { |
Alex Elder | 9bd1966 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1043 | void *base; |
Alex Elder | 5821bd8 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 1044 | u32 crc = le32_to_cpu(msg->footer.data_crc); |
Alex Elder | 8d63e31 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1045 | char *kaddr; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1046 | |
Alex Elder | 8d63e31 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1047 | kaddr = kmap(page); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1048 | BUG_ON(kaddr == NULL); |
Alex Elder | 9bd1966 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1049 | base = kaddr + con->out_msg_pos.page_pos + bio_offset; |
Alex Elder | 5821bd8 | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 1050 | crc = crc32c(crc, base, len); |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 1051 | msg->footer.data_crc = cpu_to_le32(crc); |
Alex Elder | bca064d | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1052 | con->out_msg_pos.did_page_crc = true; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1053 | } |
Alex Elder | e36b13c | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1054 | ret = ceph_tcp_sendpage(con->sock, page, |
Alex Elder | 9bd1966 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1055 | con->out_msg_pos.page_pos + bio_offset, |
Alex Elder | e36b13c | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1056 | len, 1); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1057 | |
Alex Elder | 0cdf9e6 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1058 | if (do_datacrc) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1059 | kunmap(page); |
| 1060 | |
| 1061 | if (ret <= 0) |
| 1062 | goto out; |
| 1063 | |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 1064 | out_msg_pos_next(con, page, len, (size_t) ret, in_trail); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | dout("write_partial_msg_pages %p msg %p done\n", con, msg); |
| 1068 | |
| 1069 | /* prepare and queue up footer, too */ |
Alex Elder | 37675b0 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1070 | if (!do_datacrc) |
Alex Elder | 84ca8fc | 2012-06-11 14:57:13 -0500 | [diff] [blame] | 1071 | msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC; |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 1072 | con_out_kvec_reset(con); |
Alex Elder | 859eb79 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 1073 | prepare_write_message_footer(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1074 | ret = 1; |
| 1075 | out: |
| 1076 | return ret; |
| 1077 | } |
| 1078 | |
| 1079 | /* |
| 1080 | * write some zeros |
| 1081 | */ |
| 1082 | static int write_partial_skip(struct ceph_connection *con) |
| 1083 | { |
| 1084 | int ret; |
| 1085 | |
| 1086 | while (con->out_skip > 0) { |
Alex Elder | 3173913 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1087 | size_t size = min(con->out_skip, (int) PAGE_CACHE_SIZE); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1088 | |
Alex Elder | 3173913 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1089 | ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, 1); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1090 | if (ret <= 0) |
| 1091 | goto out; |
| 1092 | con->out_skip -= ret; |
| 1093 | } |
| 1094 | ret = 1; |
| 1095 | out: |
| 1096 | return ret; |
| 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | * Prepare to read connection handshake, or an ack. |
| 1101 | */ |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1102 | static void prepare_read_banner(struct ceph_connection *con) |
| 1103 | { |
| 1104 | dout("prepare_read_banner %p\n", con); |
| 1105 | con->in_base_pos = 0; |
| 1106 | } |
| 1107 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1108 | static void prepare_read_connect(struct ceph_connection *con) |
| 1109 | { |
| 1110 | dout("prepare_read_connect %p\n", con); |
| 1111 | con->in_base_pos = 0; |
| 1112 | } |
| 1113 | |
| 1114 | static void prepare_read_ack(struct ceph_connection *con) |
| 1115 | { |
| 1116 | dout("prepare_read_ack %p\n", con); |
| 1117 | con->in_base_pos = 0; |
| 1118 | } |
| 1119 | |
| 1120 | static void prepare_read_tag(struct ceph_connection *con) |
| 1121 | { |
| 1122 | dout("prepare_read_tag %p\n", con); |
| 1123 | con->in_base_pos = 0; |
| 1124 | con->in_tag = CEPH_MSGR_TAG_READY; |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * Prepare to read a message. |
| 1129 | */ |
| 1130 | static int prepare_read_message(struct ceph_connection *con) |
| 1131 | { |
| 1132 | dout("prepare_read_message %p\n", con); |
| 1133 | BUG_ON(con->in_msg != NULL); |
| 1134 | con->in_base_pos = 0; |
| 1135 | con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0; |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | |
| 1140 | static int read_partial(struct ceph_connection *con, |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1141 | int end, int size, void *object) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1142 | { |
Alex Elder | e6cee71 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1143 | while (con->in_base_pos < end) { |
| 1144 | int left = end - con->in_base_pos; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1145 | int have = size - left; |
| 1146 | int ret = ceph_tcp_recvmsg(con->sock, object + have, left); |
| 1147 | if (ret <= 0) |
| 1148 | return ret; |
| 1149 | con->in_base_pos += ret; |
| 1150 | } |
| 1151 | return 1; |
| 1152 | } |
| 1153 | |
| 1154 | |
| 1155 | /* |
| 1156 | * Read all or part of the connect-side handshake on a new connection |
| 1157 | */ |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1158 | static int read_partial_banner(struct ceph_connection *con) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1159 | { |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1160 | int size; |
| 1161 | int end; |
| 1162 | int ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1163 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1164 | dout("read_partial_banner %p at %d\n", con, con->in_base_pos); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1165 | |
| 1166 | /* peer's banner */ |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1167 | size = strlen(CEPH_BANNER); |
| 1168 | end = size; |
| 1169 | ret = read_partial(con, end, size, con->in_banner); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1170 | if (ret <= 0) |
| 1171 | goto out; |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1172 | |
| 1173 | size = sizeof (con->actual_peer_addr); |
| 1174 | end += size; |
| 1175 | ret = read_partial(con, end, size, &con->actual_peer_addr); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1176 | if (ret <= 0) |
| 1177 | goto out; |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1178 | |
| 1179 | size = sizeof (con->peer_addr_for_me); |
| 1180 | end += size; |
| 1181 | ret = read_partial(con, end, size, &con->peer_addr_for_me); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1182 | if (ret <= 0) |
| 1183 | goto out; |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1184 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1185 | out: |
| 1186 | return ret; |
| 1187 | } |
| 1188 | |
| 1189 | static int read_partial_connect(struct ceph_connection *con) |
| 1190 | { |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1191 | int size; |
| 1192 | int end; |
| 1193 | int ret; |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1194 | |
| 1195 | dout("read_partial_connect %p at %d\n", con, con->in_base_pos); |
| 1196 | |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1197 | size = sizeof (con->in_reply); |
| 1198 | end = size; |
| 1199 | ret = read_partial(con, end, size, &con->in_reply); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1200 | if (ret <= 0) |
| 1201 | goto out; |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1202 | |
| 1203 | size = le32_to_cpu(con->in_reply.authorizer_len); |
| 1204 | end += size; |
| 1205 | ret = read_partial(con, end, size, con->auth_reply_buf); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1206 | if (ret <= 0) |
| 1207 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1208 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1209 | dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n", |
| 1210 | con, (int)con->in_reply.tag, |
| 1211 | le32_to_cpu(con->in_reply.connect_seq), |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1212 | le32_to_cpu(con->in_reply.global_seq)); |
| 1213 | out: |
| 1214 | return ret; |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1215 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | /* |
| 1219 | * Verify the hello banner looks okay. |
| 1220 | */ |
| 1221 | static int verify_hello(struct ceph_connection *con) |
| 1222 | { |
| 1223 | if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) { |
Sage Weil | 13e38c8 | 2009-10-09 16:36:34 -0700 | [diff] [blame] | 1224 | pr_err("connect to %s got bad banner\n", |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1225 | ceph_pr_addr(&con->peer_addr.in_addr)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1226 | con->error_msg = "protocol error, bad banner"; |
| 1227 | return -1; |
| 1228 | } |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | static bool addr_is_blank(struct sockaddr_storage *ss) |
| 1233 | { |
| 1234 | switch (ss->ss_family) { |
| 1235 | case AF_INET: |
| 1236 | return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0; |
| 1237 | case AF_INET6: |
| 1238 | return |
| 1239 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 && |
| 1240 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 && |
| 1241 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 && |
| 1242 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0; |
| 1243 | } |
| 1244 | return false; |
| 1245 | } |
| 1246 | |
| 1247 | static int addr_port(struct sockaddr_storage *ss) |
| 1248 | { |
| 1249 | switch (ss->ss_family) { |
| 1250 | case AF_INET: |
Sage Weil | f28bcfb | 2009-11-04 11:46:35 -0800 | [diff] [blame] | 1251 | return ntohs(((struct sockaddr_in *)ss)->sin_port); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1252 | case AF_INET6: |
Sage Weil | f28bcfb | 2009-11-04 11:46:35 -0800 | [diff] [blame] | 1253 | return ntohs(((struct sockaddr_in6 *)ss)->sin6_port); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1254 | } |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
| 1258 | static void addr_set_port(struct sockaddr_storage *ss, int p) |
| 1259 | { |
| 1260 | switch (ss->ss_family) { |
| 1261 | case AF_INET: |
| 1262 | ((struct sockaddr_in *)ss)->sin_port = htons(p); |
Sage Weil | a2a7960 | 2011-05-12 15:34:24 -0700 | [diff] [blame] | 1263 | break; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1264 | case AF_INET6: |
| 1265 | ((struct sockaddr_in6 *)ss)->sin6_port = htons(p); |
Sage Weil | a2a7960 | 2011-05-12 15:34:24 -0700 | [diff] [blame] | 1266 | break; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | /* |
Noah Watkins | ee3b56f | 2011-09-23 11:48:42 -0700 | [diff] [blame] | 1271 | * Unlike other *_pton function semantics, zero indicates success. |
| 1272 | */ |
| 1273 | static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss, |
| 1274 | char delim, const char **ipend) |
| 1275 | { |
Alex Elder | 99f0f3b | 2012-01-23 15:49:27 -0600 | [diff] [blame] | 1276 | struct sockaddr_in *in4 = (struct sockaddr_in *) ss; |
| 1277 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss; |
Noah Watkins | ee3b56f | 2011-09-23 11:48:42 -0700 | [diff] [blame] | 1278 | |
| 1279 | memset(ss, 0, sizeof(*ss)); |
| 1280 | |
| 1281 | if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) { |
| 1282 | ss->ss_family = AF_INET; |
| 1283 | return 0; |
| 1284 | } |
| 1285 | |
| 1286 | if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) { |
| 1287 | ss->ss_family = AF_INET6; |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
| 1291 | return -EINVAL; |
| 1292 | } |
| 1293 | |
| 1294 | /* |
| 1295 | * Extract hostname string and resolve using kernel DNS facility. |
| 1296 | */ |
| 1297 | #ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER |
| 1298 | static int ceph_dns_resolve_name(const char *name, size_t namelen, |
| 1299 | struct sockaddr_storage *ss, char delim, const char **ipend) |
| 1300 | { |
| 1301 | const char *end, *delim_p; |
| 1302 | char *colon_p, *ip_addr = NULL; |
| 1303 | int ip_len, ret; |
| 1304 | |
| 1305 | /* |
| 1306 | * The end of the hostname occurs immediately preceding the delimiter or |
| 1307 | * the port marker (':') where the delimiter takes precedence. |
| 1308 | */ |
| 1309 | delim_p = memchr(name, delim, namelen); |
| 1310 | colon_p = memchr(name, ':', namelen); |
| 1311 | |
| 1312 | if (delim_p && colon_p) |
| 1313 | end = delim_p < colon_p ? delim_p : colon_p; |
| 1314 | else if (!delim_p && colon_p) |
| 1315 | end = colon_p; |
| 1316 | else { |
| 1317 | end = delim_p; |
| 1318 | if (!end) /* case: hostname:/ */ |
| 1319 | end = name + namelen; |
| 1320 | } |
| 1321 | |
| 1322 | if (end <= name) |
| 1323 | return -EINVAL; |
| 1324 | |
| 1325 | /* do dns_resolve upcall */ |
| 1326 | ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL); |
| 1327 | if (ip_len > 0) |
| 1328 | ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL); |
| 1329 | else |
| 1330 | ret = -ESRCH; |
| 1331 | |
| 1332 | kfree(ip_addr); |
| 1333 | |
| 1334 | *ipend = end; |
| 1335 | |
| 1336 | pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name, |
| 1337 | ret, ret ? "failed" : ceph_pr_addr(ss)); |
| 1338 | |
| 1339 | return ret; |
| 1340 | } |
| 1341 | #else |
| 1342 | static inline int ceph_dns_resolve_name(const char *name, size_t namelen, |
| 1343 | struct sockaddr_storage *ss, char delim, const char **ipend) |
| 1344 | { |
| 1345 | return -EINVAL; |
| 1346 | } |
| 1347 | #endif |
| 1348 | |
| 1349 | /* |
| 1350 | * Parse a server name (IP or hostname). If a valid IP address is not found |
| 1351 | * then try to extract a hostname to resolve using userspace DNS upcall. |
| 1352 | */ |
| 1353 | static int ceph_parse_server_name(const char *name, size_t namelen, |
| 1354 | struct sockaddr_storage *ss, char delim, const char **ipend) |
| 1355 | { |
| 1356 | int ret; |
| 1357 | |
| 1358 | ret = ceph_pton(name, namelen, ss, delim, ipend); |
| 1359 | if (ret) |
| 1360 | ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend); |
| 1361 | |
| 1362 | return ret; |
| 1363 | } |
| 1364 | |
| 1365 | /* |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1366 | * Parse an ip[:port] list into an addr array. Use the default |
| 1367 | * monitor port if a port isn't specified. |
| 1368 | */ |
| 1369 | int ceph_parse_ips(const char *c, const char *end, |
| 1370 | struct ceph_entity_addr *addr, |
| 1371 | int max_count, int *count) |
| 1372 | { |
Noah Watkins | ee3b56f | 2011-09-23 11:48:42 -0700 | [diff] [blame] | 1373 | int i, ret = -EINVAL; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1374 | const char *p = c; |
| 1375 | |
| 1376 | dout("parse_ips on '%.*s'\n", (int)(end-c), c); |
| 1377 | for (i = 0; i < max_count; i++) { |
| 1378 | const char *ipend; |
| 1379 | struct sockaddr_storage *ss = &addr[i].in_addr; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1380 | int port; |
Sage Weil | 39139f6 | 2010-07-08 09:54:52 -0700 | [diff] [blame] | 1381 | char delim = ','; |
| 1382 | |
| 1383 | if (*p == '[') { |
| 1384 | delim = ']'; |
| 1385 | p++; |
| 1386 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1387 | |
Noah Watkins | ee3b56f | 2011-09-23 11:48:42 -0700 | [diff] [blame] | 1388 | ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend); |
| 1389 | if (ret) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1390 | goto bad; |
Noah Watkins | ee3b56f | 2011-09-23 11:48:42 -0700 | [diff] [blame] | 1391 | ret = -EINVAL; |
| 1392 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1393 | p = ipend; |
| 1394 | |
Sage Weil | 39139f6 | 2010-07-08 09:54:52 -0700 | [diff] [blame] | 1395 | if (delim == ']') { |
| 1396 | if (*p != ']') { |
| 1397 | dout("missing matching ']'\n"); |
| 1398 | goto bad; |
| 1399 | } |
| 1400 | p++; |
| 1401 | } |
| 1402 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1403 | /* port? */ |
| 1404 | if (p < end && *p == ':') { |
| 1405 | port = 0; |
| 1406 | p++; |
| 1407 | while (p < end && *p >= '0' && *p <= '9') { |
| 1408 | port = (port * 10) + (*p - '0'); |
| 1409 | p++; |
| 1410 | } |
| 1411 | if (port > 65535 || port == 0) |
| 1412 | goto bad; |
| 1413 | } else { |
| 1414 | port = CEPH_MON_PORT; |
| 1415 | } |
| 1416 | |
| 1417 | addr_set_port(ss, port); |
| 1418 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1419 | dout("parse_ips got %s\n", ceph_pr_addr(ss)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1420 | |
| 1421 | if (p == end) |
| 1422 | break; |
| 1423 | if (*p != ',') |
| 1424 | goto bad; |
| 1425 | p++; |
| 1426 | } |
| 1427 | |
| 1428 | if (p != end) |
| 1429 | goto bad; |
| 1430 | |
| 1431 | if (count) |
| 1432 | *count = i + 1; |
| 1433 | return 0; |
| 1434 | |
| 1435 | bad: |
Sage Weil | 39139f6 | 2010-07-08 09:54:52 -0700 | [diff] [blame] | 1436 | pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c); |
Noah Watkins | ee3b56f | 2011-09-23 11:48:42 -0700 | [diff] [blame] | 1437 | return ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1438 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1439 | EXPORT_SYMBOL(ceph_parse_ips); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1440 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1441 | static int process_banner(struct ceph_connection *con) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1442 | { |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1443 | dout("process_banner on %p\n", con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1444 | |
| 1445 | if (verify_hello(con) < 0) |
| 1446 | return -1; |
| 1447 | |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 1448 | ceph_decode_addr(&con->actual_peer_addr); |
| 1449 | ceph_decode_addr(&con->peer_addr_for_me); |
| 1450 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1451 | /* |
| 1452 | * Make sure the other end is who we wanted. note that the other |
| 1453 | * end may not yet know their ip address, so if it's 0.0.0.0, give |
| 1454 | * them the benefit of the doubt. |
| 1455 | */ |
Sage Weil | 103e2d3 | 2010-01-07 16:12:36 -0800 | [diff] [blame] | 1456 | if (memcmp(&con->peer_addr, &con->actual_peer_addr, |
| 1457 | sizeof(con->peer_addr)) != 0 && |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1458 | !(addr_is_blank(&con->actual_peer_addr.in_addr) && |
| 1459 | con->actual_peer_addr.nonce == con->peer_addr.nonce)) { |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 1460 | pr_warning("wrong peer, want %s/%d, got %s/%d\n", |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1461 | ceph_pr_addr(&con->peer_addr.in_addr), |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 1462 | (int)le32_to_cpu(con->peer_addr.nonce), |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1463 | ceph_pr_addr(&con->actual_peer_addr.in_addr), |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 1464 | (int)le32_to_cpu(con->actual_peer_addr.nonce)); |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 1465 | con->error_msg = "wrong peer at address"; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1466 | return -1; |
| 1467 | } |
| 1468 | |
| 1469 | /* |
| 1470 | * did we learn our address? |
| 1471 | */ |
| 1472 | if (addr_is_blank(&con->msgr->inst.addr.in_addr)) { |
| 1473 | int port = addr_port(&con->msgr->inst.addr.in_addr); |
| 1474 | |
| 1475 | memcpy(&con->msgr->inst.addr.in_addr, |
| 1476 | &con->peer_addr_for_me.in_addr, |
| 1477 | sizeof(con->peer_addr_for_me.in_addr)); |
| 1478 | addr_set_port(&con->msgr->inst.addr.in_addr, port); |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 1479 | encode_my_addr(con->msgr); |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1480 | dout("process_banner learned my addr is %s\n", |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1481 | ceph_pr_addr(&con->msgr->inst.addr.in_addr)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1484 | return 0; |
| 1485 | } |
| 1486 | |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1487 | static void fail_protocol(struct ceph_connection *con) |
| 1488 | { |
| 1489 | reset_connection(con); |
| 1490 | set_bit(CLOSED, &con->state); /* in case there's queued work */ |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1491 | } |
| 1492 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1493 | static int process_connect(struct ceph_connection *con) |
| 1494 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1495 | u64 sup_feat = con->msgr->supported_features; |
| 1496 | u64 req_feat = con->msgr->required_features; |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1497 | u64 server_feat = le64_to_cpu(con->in_reply.features); |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 1498 | int ret; |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1499 | |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1500 | dout("process_connect on %p tag %d\n", con, (int)con->in_tag); |
| 1501 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1502 | switch (con->in_reply.tag) { |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1503 | case CEPH_MSGR_TAG_FEATURES: |
| 1504 | pr_err("%s%lld %s feature set mismatch," |
| 1505 | " my %llx < server's %llx, missing %llx\n", |
| 1506 | ENTITY_NAME(con->peer_name), |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1507 | ceph_pr_addr(&con->peer_addr.in_addr), |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1508 | sup_feat, server_feat, server_feat & ~sup_feat); |
| 1509 | con->error_msg = "missing required protocol features"; |
| 1510 | fail_protocol(con); |
| 1511 | return -1; |
| 1512 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1513 | case CEPH_MSGR_TAG_BADPROTOVER: |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1514 | pr_err("%s%lld %s protocol version mismatch," |
| 1515 | " my %d != server's %d\n", |
| 1516 | ENTITY_NAME(con->peer_name), |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1517 | ceph_pr_addr(&con->peer_addr.in_addr), |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1518 | le32_to_cpu(con->out_connect.protocol_version), |
| 1519 | le32_to_cpu(con->in_reply.protocol_version)); |
| 1520 | con->error_msg = "protocol version mismatch"; |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1521 | fail_protocol(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1522 | return -1; |
| 1523 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1524 | case CEPH_MSGR_TAG_BADAUTHORIZER: |
| 1525 | con->auth_retry++; |
| 1526 | dout("process_connect %p got BADAUTHORIZER attempt %d\n", con, |
| 1527 | con->auth_retry); |
| 1528 | if (con->auth_retry == 2) { |
| 1529 | con->error_msg = "connect authorization failure"; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1530 | return -1; |
| 1531 | } |
| 1532 | con->auth_retry = 1; |
Alex Elder | e825a66 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 1533 | ret = prepare_write_connect(con); |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 1534 | if (ret < 0) |
| 1535 | return ret; |
Sage Weil | 63733a0 | 2010-03-15 15:47:22 -0700 | [diff] [blame] | 1536 | prepare_read_connect(con); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 1537 | break; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1538 | |
| 1539 | case CEPH_MSGR_TAG_RESETSESSION: |
| 1540 | /* |
| 1541 | * If we connected with a large connect_seq but the peer |
| 1542 | * has no record of a session with us (no connection, or |
| 1543 | * connect_seq == 0), they will send RESETSESION to indicate |
| 1544 | * that they must have reset their session, and may have |
| 1545 | * dropped messages. |
| 1546 | */ |
| 1547 | dout("process_connect got RESET peer seq %u\n", |
Sage Weil | a16cb1f | 2012-07-10 11:53:34 -0700 | [diff] [blame] | 1548 | le32_to_cpu(con->in_reply.connect_seq)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1549 | pr_err("%s%lld %s connection reset\n", |
| 1550 | ENTITY_NAME(con->peer_name), |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1551 | ceph_pr_addr(&con->peer_addr.in_addr)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1552 | reset_connection(con); |
Alex Elder | 5a0f8fd | 2012-05-16 21:51:59 -0500 | [diff] [blame] | 1553 | ret = prepare_write_connect(con); |
| 1554 | if (ret < 0) |
| 1555 | return ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1556 | prepare_read_connect(con); |
| 1557 | |
| 1558 | /* Tell ceph about it. */ |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1559 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1560 | pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name)); |
| 1561 | if (con->ops->peer_reset) |
| 1562 | con->ops->peer_reset(con); |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1563 | mutex_lock(&con->mutex); |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 1564 | if (test_bit(CLOSED, &con->state) || |
| 1565 | test_bit(OPENING, &con->state)) |
| 1566 | return -EAGAIN; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1567 | break; |
| 1568 | |
| 1569 | case CEPH_MSGR_TAG_RETRY_SESSION: |
| 1570 | /* |
| 1571 | * If we sent a smaller connect_seq than the peer has, try |
| 1572 | * again with a larger value. |
| 1573 | */ |
Sage Weil | a16cb1f | 2012-07-10 11:53:34 -0700 | [diff] [blame] | 1574 | dout("process_connect got RETRY_SESSION my seq %u, peer %u\n", |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1575 | le32_to_cpu(con->out_connect.connect_seq), |
Sage Weil | a16cb1f | 2012-07-10 11:53:34 -0700 | [diff] [blame] | 1576 | le32_to_cpu(con->in_reply.connect_seq)); |
| 1577 | con->connect_seq = le32_to_cpu(con->in_reply.connect_seq); |
Alex Elder | 5a0f8fd | 2012-05-16 21:51:59 -0500 | [diff] [blame] | 1578 | ret = prepare_write_connect(con); |
| 1579 | if (ret < 0) |
| 1580 | return ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1581 | prepare_read_connect(con); |
| 1582 | break; |
| 1583 | |
| 1584 | case CEPH_MSGR_TAG_RETRY_GLOBAL: |
| 1585 | /* |
| 1586 | * If we sent a smaller global_seq than the peer has, try |
| 1587 | * again with a larger value. |
| 1588 | */ |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 1589 | dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n", |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1590 | con->peer_global_seq, |
Sage Weil | a16cb1f | 2012-07-10 11:53:34 -0700 | [diff] [blame] | 1591 | le32_to_cpu(con->in_reply.global_seq)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1592 | get_global_seq(con->msgr, |
Sage Weil | a16cb1f | 2012-07-10 11:53:34 -0700 | [diff] [blame] | 1593 | le32_to_cpu(con->in_reply.global_seq)); |
Alex Elder | 5a0f8fd | 2012-05-16 21:51:59 -0500 | [diff] [blame] | 1594 | ret = prepare_write_connect(con); |
| 1595 | if (ret < 0) |
| 1596 | return ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1597 | prepare_read_connect(con); |
| 1598 | break; |
| 1599 | |
| 1600 | case CEPH_MSGR_TAG_READY: |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1601 | if (req_feat & ~server_feat) { |
| 1602 | pr_err("%s%lld %s protocol feature mismatch," |
| 1603 | " my required %llx > server's %llx, need %llx\n", |
| 1604 | ENTITY_NAME(con->peer_name), |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1605 | ceph_pr_addr(&con->peer_addr.in_addr), |
Sage Weil | 04a419f | 2009-12-23 09:30:21 -0800 | [diff] [blame] | 1606 | req_feat, server_feat, req_feat & ~server_feat); |
| 1607 | con->error_msg = "missing required protocol features"; |
| 1608 | fail_protocol(con); |
| 1609 | return -1; |
| 1610 | } |
Alex Elder | 3ec50d1 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 1611 | clear_bit(NEGOTIATING, &con->state); |
Alex Elder | e27947c | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 1612 | set_bit(CONNECTED, &con->state); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1613 | con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq); |
| 1614 | con->connect_seq++; |
Sage Weil | aba558e | 2010-05-12 15:23:30 -0700 | [diff] [blame] | 1615 | con->peer_features = server_feat; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1616 | dout("process_connect got READY gseq %d cseq %d (%d)\n", |
| 1617 | con->peer_global_seq, |
| 1618 | le32_to_cpu(con->in_reply.connect_seq), |
| 1619 | con->connect_seq); |
| 1620 | WARN_ON(con->connect_seq != |
| 1621 | le32_to_cpu(con->in_reply.connect_seq)); |
Sage Weil | 92ac41d | 2009-12-14 14:56:56 -0800 | [diff] [blame] | 1622 | |
| 1623 | if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY) |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 1624 | set_bit(LOSSYTX, &con->flags); |
Sage Weil | 92ac41d | 2009-12-14 14:56:56 -0800 | [diff] [blame] | 1625 | |
Sage Weil | 85effe1 | 2012-07-30 16:22:05 -0700 | [diff] [blame] | 1626 | con->delay = 0; /* reset backoff memory */ |
| 1627 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1628 | prepare_read_tag(con); |
| 1629 | break; |
| 1630 | |
| 1631 | case CEPH_MSGR_TAG_WAIT: |
| 1632 | /* |
| 1633 | * If there is a connection race (we are opening |
| 1634 | * connections to each other), one of us may just have |
| 1635 | * to WAIT. This shouldn't happen if we are the |
| 1636 | * client. |
| 1637 | */ |
Sage Weil | 0417788 | 2011-05-12 15:33:17 -0700 | [diff] [blame] | 1638 | pr_err("process_connect got WAIT as client\n"); |
| 1639 | con->error_msg = "protocol error, got WAIT as client"; |
| 1640 | return -1; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1641 | |
| 1642 | default: |
| 1643 | pr_err("connect protocol error, will retry\n"); |
| 1644 | con->error_msg = "protocol error, garbage tag during connect"; |
| 1645 | return -1; |
| 1646 | } |
| 1647 | return 0; |
| 1648 | } |
| 1649 | |
| 1650 | |
| 1651 | /* |
| 1652 | * read (part of) an ack |
| 1653 | */ |
| 1654 | static int read_partial_ack(struct ceph_connection *con) |
| 1655 | { |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1656 | int size = sizeof (con->in_temp_ack); |
| 1657 | int end = size; |
| 1658 | |
| 1659 | return read_partial(con, end, size, &con->in_temp_ack); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | |
| 1663 | /* |
| 1664 | * We can finally discard anything that's been acked. |
| 1665 | */ |
| 1666 | static void process_ack(struct ceph_connection *con) |
| 1667 | { |
| 1668 | struct ceph_msg *m; |
| 1669 | u64 ack = le64_to_cpu(con->in_temp_ack); |
| 1670 | u64 seq; |
| 1671 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1672 | while (!list_empty(&con->out_sent)) { |
| 1673 | m = list_first_entry(&con->out_sent, struct ceph_msg, |
| 1674 | list_head); |
| 1675 | seq = le64_to_cpu(m->hdr.seq); |
| 1676 | if (seq > ack) |
| 1677 | break; |
| 1678 | dout("got ack for seq %llu type %d at %p\n", seq, |
| 1679 | le16_to_cpu(m->hdr.type), m); |
Sage Weil | 4cf9d54 | 2011-07-26 11:27:24 -0700 | [diff] [blame] | 1680 | m->ack_stamp = jiffies; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1681 | ceph_msg_remove(m); |
| 1682 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1683 | prepare_read_tag(con); |
| 1684 | } |
| 1685 | |
| 1686 | |
| 1687 | |
| 1688 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1689 | static int read_partial_message_section(struct ceph_connection *con, |
Sage Weil | 213c99e | 2010-08-03 10:25:11 -0700 | [diff] [blame] | 1690 | struct kvec *section, |
| 1691 | unsigned int sec_len, u32 *crc) |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1692 | { |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1693 | int ret, left; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1694 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1695 | BUG_ON(!section); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1696 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1697 | while (section->iov_len < sec_len) { |
| 1698 | BUG_ON(section->iov_base == NULL); |
| 1699 | left = sec_len - section->iov_len; |
| 1700 | ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base + |
| 1701 | section->iov_len, left); |
| 1702 | if (ret <= 0) |
| 1703 | return ret; |
| 1704 | section->iov_len += ret; |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1705 | } |
Alex Elder | fe3ad59 | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1706 | if (section->iov_len == sec_len) |
| 1707 | *crc = crc32c(0, section->iov_base, section->iov_len); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1708 | |
| 1709 | return 1; |
| 1710 | } |
| 1711 | |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 1712 | static bool ceph_con_in_msg_alloc(struct ceph_connection *con, |
| 1713 | struct ceph_msg_header *hdr); |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1714 | |
| 1715 | |
| 1716 | static int read_partial_message_pages(struct ceph_connection *con, |
| 1717 | struct page **pages, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1718 | unsigned int data_len, bool do_datacrc) |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1719 | { |
| 1720 | void *p; |
| 1721 | int ret; |
| 1722 | int left; |
| 1723 | |
| 1724 | left = min((int)(data_len - con->in_msg_pos.data_pos), |
| 1725 | (int)(PAGE_SIZE - con->in_msg_pos.page_pos)); |
| 1726 | /* (page) data */ |
| 1727 | BUG_ON(pages == NULL); |
| 1728 | p = kmap(pages[con->in_msg_pos.page]); |
| 1729 | ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos, |
| 1730 | left); |
Alex Elder | bca064d | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1731 | if (ret > 0 && do_datacrc) |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1732 | con->in_data_crc = |
| 1733 | crc32c(con->in_data_crc, |
| 1734 | p + con->in_msg_pos.page_pos, ret); |
| 1735 | kunmap(pages[con->in_msg_pos.page]); |
| 1736 | if (ret <= 0) |
| 1737 | return ret; |
| 1738 | con->in_msg_pos.data_pos += ret; |
| 1739 | con->in_msg_pos.page_pos += ret; |
| 1740 | if (con->in_msg_pos.page_pos == PAGE_SIZE) { |
| 1741 | con->in_msg_pos.page_pos = 0; |
| 1742 | con->in_msg_pos.page++; |
| 1743 | } |
| 1744 | |
| 1745 | return ret; |
| 1746 | } |
| 1747 | |
| 1748 | #ifdef CONFIG_BLOCK |
| 1749 | static int read_partial_message_bio(struct ceph_connection *con, |
| 1750 | struct bio **bio_iter, int *bio_seg, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1751 | unsigned int data_len, bool do_datacrc) |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1752 | { |
| 1753 | struct bio_vec *bv = bio_iovec_idx(*bio_iter, *bio_seg); |
| 1754 | void *p; |
| 1755 | int ret, left; |
| 1756 | |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1757 | left = min((int)(data_len - con->in_msg_pos.data_pos), |
| 1758 | (int)(bv->bv_len - con->in_msg_pos.page_pos)); |
| 1759 | |
| 1760 | p = kmap(bv->bv_page) + bv->bv_offset; |
| 1761 | |
| 1762 | ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos, |
| 1763 | left); |
Alex Elder | bca064d | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1764 | if (ret > 0 && do_datacrc) |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1765 | con->in_data_crc = |
| 1766 | crc32c(con->in_data_crc, |
| 1767 | p + con->in_msg_pos.page_pos, ret); |
| 1768 | kunmap(bv->bv_page); |
| 1769 | if (ret <= 0) |
| 1770 | return ret; |
| 1771 | con->in_msg_pos.data_pos += ret; |
| 1772 | con->in_msg_pos.page_pos += ret; |
| 1773 | if (con->in_msg_pos.page_pos == bv->bv_len) { |
| 1774 | con->in_msg_pos.page_pos = 0; |
| 1775 | iter_bio_next(bio_iter, bio_seg); |
| 1776 | } |
| 1777 | |
| 1778 | return ret; |
| 1779 | } |
| 1780 | #endif |
| 1781 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1782 | /* |
| 1783 | * read (part of) a message. |
| 1784 | */ |
| 1785 | static int read_partial_message(struct ceph_connection *con) |
| 1786 | { |
| 1787 | struct ceph_msg *m = con->in_msg; |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1788 | int size; |
| 1789 | int end; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1790 | int ret; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1791 | unsigned int front_len, middle_len, data_len; |
Alex Elder | 37675b0 | 2012-03-07 11:40:08 -0600 | [diff] [blame] | 1792 | bool do_datacrc = !con->msgr->nocrc; |
Sage Weil | ae18756 | 2010-04-22 07:47:01 -0700 | [diff] [blame] | 1793 | u64 seq; |
Alex Elder | fe3ad59 | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1794 | u32 crc; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1795 | |
| 1796 | dout("read_partial_message con %p msg %p\n", con, m); |
| 1797 | |
| 1798 | /* header */ |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1799 | size = sizeof (con->in_hdr); |
| 1800 | end = size; |
| 1801 | ret = read_partial(con, end, size, &con->in_hdr); |
Alex Elder | 57dac9d | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1802 | if (ret <= 0) |
| 1803 | return ret; |
Alex Elder | fe3ad59 | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1804 | |
| 1805 | crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc)); |
| 1806 | if (cpu_to_le32(crc) != con->in_hdr.crc) { |
| 1807 | pr_err("read_partial_message bad hdr " |
| 1808 | " crc %u != expected %u\n", |
| 1809 | crc, con->in_hdr.crc); |
| 1810 | return -EBADMSG; |
| 1811 | } |
| 1812 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1813 | front_len = le32_to_cpu(con->in_hdr.front_len); |
| 1814 | if (front_len > CEPH_MSG_MAX_FRONT_LEN) |
| 1815 | return -EIO; |
| 1816 | middle_len = le32_to_cpu(con->in_hdr.middle_len); |
| 1817 | if (middle_len > CEPH_MSG_MAX_DATA_LEN) |
| 1818 | return -EIO; |
| 1819 | data_len = le32_to_cpu(con->in_hdr.data_len); |
| 1820 | if (data_len > CEPH_MSG_MAX_DATA_LEN) |
| 1821 | return -EIO; |
| 1822 | |
Sage Weil | ae18756 | 2010-04-22 07:47:01 -0700 | [diff] [blame] | 1823 | /* verify seq# */ |
| 1824 | seq = le64_to_cpu(con->in_hdr.seq); |
| 1825 | if ((s64)seq - (s64)con->in_seq < 1) { |
Sage Weil | df9f86f | 2010-11-01 15:49:23 -0700 | [diff] [blame] | 1826 | pr_info("skipping %s%lld %s seq %lld expected %lld\n", |
Sage Weil | ae18756 | 2010-04-22 07:47:01 -0700 | [diff] [blame] | 1827 | ENTITY_NAME(con->peer_name), |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1828 | ceph_pr_addr(&con->peer_addr.in_addr), |
Sage Weil | ae18756 | 2010-04-22 07:47:01 -0700 | [diff] [blame] | 1829 | seq, con->in_seq + 1); |
| 1830 | con->in_base_pos = -front_len - middle_len - data_len - |
| 1831 | sizeof(m->footer); |
| 1832 | con->in_tag = CEPH_MSGR_TAG_READY; |
Sage Weil | ae18756 | 2010-04-22 07:47:01 -0700 | [diff] [blame] | 1833 | return 0; |
| 1834 | } else if ((s64)seq - (s64)con->in_seq > 1) { |
| 1835 | pr_err("read_partial_message bad seq %lld expected %lld\n", |
| 1836 | seq, con->in_seq + 1); |
| 1837 | con->error_msg = "bad message sequence # for incoming message"; |
| 1838 | return -EBADMSG; |
| 1839 | } |
| 1840 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1841 | /* allocate message? */ |
| 1842 | if (!con->in_msg) { |
| 1843 | dout("got hdr type %d front %d data %d\n", con->in_hdr.type, |
| 1844 | con->in_hdr.front_len, con->in_hdr.data_len); |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 1845 | if (ceph_con_in_msg_alloc(con, &con->in_hdr)) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1846 | /* skip this message */ |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1847 | dout("alloc_msg said skip message\n"); |
Sage Weil | ae32be3 | 2010-06-13 10:30:19 -0700 | [diff] [blame] | 1848 | BUG_ON(con->in_msg); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1849 | con->in_base_pos = -front_len - middle_len - data_len - |
| 1850 | sizeof(m->footer); |
| 1851 | con->in_tag = CEPH_MSGR_TAG_READY; |
Sage Weil | 684be25 | 2010-04-21 20:45:59 -0700 | [diff] [blame] | 1852 | con->in_seq++; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1853 | return 0; |
| 1854 | } |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1855 | if (!con->in_msg) { |
Sage Weil | 5b3a4db | 2010-02-19 21:43:23 -0800 | [diff] [blame] | 1856 | con->error_msg = |
| 1857 | "error allocating memory for incoming message"; |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1858 | return -ENOMEM; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1859 | } |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 1860 | |
| 1861 | BUG_ON(con->in_msg->con != con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1862 | m = con->in_msg; |
| 1863 | m->front.iov_len = 0; /* haven't read it yet */ |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1864 | if (m->middle) |
| 1865 | m->middle->vec.iov_len = 0; |
Yehuda Sadeh | 9d7f0f1 | 2010-01-11 10:32:02 -0800 | [diff] [blame] | 1866 | |
| 1867 | con->in_msg_pos.page = 0; |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1868 | if (m->pages) |
Sage Weil | c5c6b19 | 2010-11-09 12:40:00 -0800 | [diff] [blame] | 1869 | con->in_msg_pos.page_pos = m->page_alignment; |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1870 | else |
| 1871 | con->in_msg_pos.page_pos = 0; |
Yehuda Sadeh | 9d7f0f1 | 2010-01-11 10:32:02 -0800 | [diff] [blame] | 1872 | con->in_msg_pos.data_pos = 0; |
Sage Weil | a410702 | 2012-07-30 16:20:25 -0700 | [diff] [blame] | 1873 | |
| 1874 | #ifdef CONFIG_BLOCK |
| 1875 | if (m->bio) |
| 1876 | init_bio_iter(m->bio, &m->bio_iter, &m->bio_seg); |
| 1877 | #endif |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | /* front */ |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1881 | ret = read_partial_message_section(con, &m->front, front_len, |
| 1882 | &con->in_front_crc); |
| 1883 | if (ret <= 0) |
| 1884 | return ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1885 | |
| 1886 | /* middle */ |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1887 | if (m->middle) { |
Sage Weil | 213c99e | 2010-08-03 10:25:11 -0700 | [diff] [blame] | 1888 | ret = read_partial_message_section(con, &m->middle->vec, |
| 1889 | middle_len, |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 1890 | &con->in_middle_crc); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1891 | if (ret <= 0) |
| 1892 | return ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | /* (page) data */ |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1896 | while (con->in_msg_pos.data_pos < data_len) { |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1897 | if (m->pages) { |
| 1898 | ret = read_partial_message_pages(con, m->pages, |
Alex Elder | bca064d | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1899 | data_len, do_datacrc); |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1900 | if (ret <= 0) |
| 1901 | return ret; |
| 1902 | #ifdef CONFIG_BLOCK |
| 1903 | } else if (m->bio) { |
Sage Weil | a410702 | 2012-07-30 16:20:25 -0700 | [diff] [blame] | 1904 | BUG_ON(!m->bio_iter); |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1905 | ret = read_partial_message_bio(con, |
| 1906 | &m->bio_iter, &m->bio_seg, |
Alex Elder | bca064d | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1907 | data_len, do_datacrc); |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 1908 | if (ret <= 0) |
| 1909 | return ret; |
| 1910 | #endif |
| 1911 | } else { |
| 1912 | BUG_ON(1); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1916 | /* footer */ |
Alex Elder | fd51653 | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1917 | size = sizeof (m->footer); |
| 1918 | end += size; |
| 1919 | ret = read_partial(con, end, size, &m->footer); |
Alex Elder | 57dac9d | 2012-05-10 10:29:50 -0500 | [diff] [blame] | 1920 | if (ret <= 0) |
| 1921 | return ret; |
| 1922 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1923 | dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n", |
| 1924 | m, front_len, m->footer.front_crc, middle_len, |
| 1925 | m->footer.middle_crc, data_len, m->footer.data_crc); |
| 1926 | |
| 1927 | /* crc ok? */ |
| 1928 | if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) { |
| 1929 | pr_err("read_partial_message %p front crc %u != exp. %u\n", |
| 1930 | m, con->in_front_crc, m->footer.front_crc); |
| 1931 | return -EBADMSG; |
| 1932 | } |
| 1933 | if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) { |
| 1934 | pr_err("read_partial_message %p middle crc %u != exp %u\n", |
| 1935 | m, con->in_middle_crc, m->footer.middle_crc); |
| 1936 | return -EBADMSG; |
| 1937 | } |
Alex Elder | bca064d | 2012-02-15 07:43:54 -0600 | [diff] [blame] | 1938 | if (do_datacrc && |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1939 | (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 && |
| 1940 | con->in_data_crc != le32_to_cpu(m->footer.data_crc)) { |
| 1941 | pr_err("read_partial_message %p data crc %u != exp. %u\n", m, |
| 1942 | con->in_data_crc, le32_to_cpu(m->footer.data_crc)); |
| 1943 | return -EBADMSG; |
| 1944 | } |
| 1945 | |
| 1946 | return 1; /* done! */ |
| 1947 | } |
| 1948 | |
| 1949 | /* |
| 1950 | * Process message. This happens in the worker thread. The callback should |
| 1951 | * be careful not to do anything that waits on other incoming messages or it |
| 1952 | * may deadlock. |
| 1953 | */ |
| 1954 | static void process_message(struct ceph_connection *con) |
| 1955 | { |
Sage Weil | 5e095e8 | 2009-12-14 14:30:34 -0800 | [diff] [blame] | 1956 | struct ceph_msg *msg; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1957 | |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 1958 | BUG_ON(con->in_msg->con != con); |
| 1959 | con->in_msg->con = NULL; |
Sage Weil | 5e095e8 | 2009-12-14 14:30:34 -0800 | [diff] [blame] | 1960 | msg = con->in_msg; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1961 | con->in_msg = NULL; |
Sage Weil | 36eb71a | 2012-06-21 12:47:08 -0700 | [diff] [blame] | 1962 | con->ops->put(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1963 | |
| 1964 | /* if first message, set peer_name */ |
| 1965 | if (con->peer_name.type == 0) |
Sage Weil | dbad185 | 2010-03-25 15:45:38 -0700 | [diff] [blame] | 1966 | con->peer_name = msg->hdr.src; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1967 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1968 | con->in_seq++; |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1969 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1970 | |
| 1971 | dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n", |
| 1972 | msg, le64_to_cpu(msg->hdr.seq), |
Sage Weil | dbad185 | 2010-03-25 15:45:38 -0700 | [diff] [blame] | 1973 | ENTITY_NAME(msg->hdr.src), |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1974 | le16_to_cpu(msg->hdr.type), |
| 1975 | ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), |
| 1976 | le32_to_cpu(msg->hdr.front_len), |
| 1977 | le32_to_cpu(msg->hdr.data_len), |
| 1978 | con->in_front_crc, con->in_middle_crc, con->in_data_crc); |
| 1979 | con->ops->dispatch(con, msg); |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 1980 | |
| 1981 | mutex_lock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1982 | prepare_read_tag(con); |
| 1983 | } |
| 1984 | |
| 1985 | |
| 1986 | /* |
| 1987 | * Write something to the socket. Called in a worker thread when the |
| 1988 | * socket appears to be writeable and we have something ready to send. |
| 1989 | */ |
| 1990 | static int try_write(struct ceph_connection *con) |
| 1991 | { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1992 | int ret = 1; |
| 1993 | |
Sage Weil | d59315c | 2012-06-21 12:49:23 -0700 | [diff] [blame] | 1994 | dout("try_write start %p state %lu\n", con, con->state); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1995 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1996 | more: |
| 1997 | dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes); |
| 1998 | |
| 1999 | /* open the socket first? */ |
| 2000 | if (con->sock == NULL) { |
Alex Elder | a5988c4 | 2012-05-29 11:04:58 -0500 | [diff] [blame] | 2001 | set_bit(CONNECTING, &con->state); |
| 2002 | |
Alex Elder | e220042 | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 2003 | con_out_kvec_reset(con); |
Alex Elder | e825a66 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 2004 | prepare_write_banner(con); |
Sage Weil | eed0ef2 | 2009-11-10 14:34:36 -0800 | [diff] [blame] | 2005 | prepare_read_banner(con); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2006 | |
Sage Weil | cf3e5c4 | 2009-12-11 09:48:05 -0800 | [diff] [blame] | 2007 | BUG_ON(con->in_msg); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2008 | con->in_tag = CEPH_MSGR_TAG_READY; |
| 2009 | dout("try_write initiating connect on %p new state %lu\n", |
| 2010 | con, con->state); |
Alex Elder | 41617d0 | 2012-02-14 14:05:33 -0600 | [diff] [blame] | 2011 | ret = ceph_tcp_connect(con); |
| 2012 | if (ret < 0) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2013 | con->error_msg = "connect error"; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2014 | goto out; |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | more_kvec: |
| 2019 | /* kvec data queued? */ |
| 2020 | if (con->out_skip) { |
| 2021 | ret = write_partial_skip(con); |
| 2022 | if (ret <= 0) |
Sage Weil | 42961d2 | 2011-01-25 08:19:34 -0800 | [diff] [blame] | 2023 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2024 | } |
| 2025 | if (con->out_kvec_left) { |
| 2026 | ret = write_partial_kvec(con); |
| 2027 | if (ret <= 0) |
Sage Weil | 42961d2 | 2011-01-25 08:19:34 -0800 | [diff] [blame] | 2028 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | /* msg pages? */ |
| 2032 | if (con->out_msg) { |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 2033 | if (con->out_msg_done) { |
| 2034 | ceph_msg_put(con->out_msg); |
| 2035 | con->out_msg = NULL; /* we're done with this one */ |
| 2036 | goto do_next; |
| 2037 | } |
| 2038 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2039 | ret = write_partial_msg_pages(con); |
| 2040 | if (ret == 1) |
| 2041 | goto more_kvec; /* we need to send the footer, too! */ |
| 2042 | if (ret == 0) |
Sage Weil | 42961d2 | 2011-01-25 08:19:34 -0800 | [diff] [blame] | 2043 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2044 | if (ret < 0) { |
| 2045 | dout("try_write write_partial_msg_pages err %d\n", |
| 2046 | ret); |
Sage Weil | 42961d2 | 2011-01-25 08:19:34 -0800 | [diff] [blame] | 2047 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2048 | } |
| 2049 | } |
| 2050 | |
Sage Weil | c86a293 | 2009-12-14 14:04:30 -0800 | [diff] [blame] | 2051 | do_next: |
Alex Elder | 7593af9 | 2012-05-24 11:55:03 -0500 | [diff] [blame] | 2052 | if (!test_bit(CONNECTING, &con->state) && |
| 2053 | !test_bit(NEGOTIATING, &con->state)) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2054 | /* is anything else pending? */ |
| 2055 | if (!list_empty(&con->out_queue)) { |
| 2056 | prepare_write_message(con); |
| 2057 | goto more; |
| 2058 | } |
| 2059 | if (con->in_seq > con->in_seq_acked) { |
| 2060 | prepare_write_ack(con); |
| 2061 | goto more; |
| 2062 | } |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2063 | if (test_and_clear_bit(KEEPALIVE_PENDING, &con->flags)) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2064 | prepare_write_keepalive(con); |
| 2065 | goto more; |
| 2066 | } |
| 2067 | } |
| 2068 | |
| 2069 | /* Nothing to do! */ |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2070 | clear_bit(WRITE_PENDING, &con->flags); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2071 | dout("try_write nothing else to write.\n"); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2072 | ret = 0; |
| 2073 | out: |
Sage Weil | 42961d2 | 2011-01-25 08:19:34 -0800 | [diff] [blame] | 2074 | dout("try_write done on %p ret %d\n", con, ret); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2075 | return ret; |
| 2076 | } |
| 2077 | |
| 2078 | |
| 2079 | |
| 2080 | /* |
| 2081 | * Read what we can from the socket. |
| 2082 | */ |
| 2083 | static int try_read(struct ceph_connection *con) |
| 2084 | { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2085 | int ret = -1; |
| 2086 | |
| 2087 | if (!con->sock) |
| 2088 | return 0; |
| 2089 | |
| 2090 | if (test_bit(STANDBY, &con->state)) |
| 2091 | return 0; |
| 2092 | |
| 2093 | dout("try_read start on %p\n", con); |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 2094 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2095 | more: |
| 2096 | dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag, |
| 2097 | con->in_base_pos); |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 2098 | |
| 2099 | /* |
| 2100 | * process_connect and process_message drop and re-take |
| 2101 | * con->mutex. make sure we handle a racing close or reopen. |
| 2102 | */ |
| 2103 | if (test_bit(CLOSED, &con->state) || |
| 2104 | test_bit(OPENING, &con->state)) { |
| 2105 | ret = -EAGAIN; |
| 2106 | goto out; |
| 2107 | } |
| 2108 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2109 | if (test_bit(CONNECTING, &con->state)) { |
Alex Elder | 7593af9 | 2012-05-24 11:55:03 -0500 | [diff] [blame] | 2110 | dout("try_read connecting\n"); |
| 2111 | ret = read_partial_banner(con); |
| 2112 | if (ret <= 0) |
Alex Elder | ab166d5 | 2012-05-31 11:37:29 -0500 | [diff] [blame] | 2113 | goto out; |
Alex Elder | 7593af9 | 2012-05-24 11:55:03 -0500 | [diff] [blame] | 2114 | ret = process_banner(con); |
| 2115 | if (ret < 0) |
| 2116 | goto out; |
| 2117 | |
| 2118 | clear_bit(CONNECTING, &con->state); |
| 2119 | set_bit(NEGOTIATING, &con->state); |
| 2120 | |
| 2121 | /* Banner is good, exchange connection info */ |
| 2122 | ret = prepare_write_connect(con); |
| 2123 | if (ret < 0) |
| 2124 | goto out; |
| 2125 | prepare_read_connect(con); |
| 2126 | |
| 2127 | /* Send connection info before awaiting response */ |
| 2128 | goto out; |
| 2129 | } |
| 2130 | |
| 2131 | if (test_bit(NEGOTIATING, &con->state)) { |
| 2132 | dout("try_read negotiating\n"); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2133 | ret = read_partial_connect(con); |
| 2134 | if (ret <= 0) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2135 | goto out; |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2136 | ret = process_connect(con); |
| 2137 | if (ret < 0) |
| 2138 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2139 | goto more; |
| 2140 | } |
| 2141 | |
| 2142 | if (con->in_base_pos < 0) { |
| 2143 | /* |
| 2144 | * skipping + discarding content. |
| 2145 | * |
| 2146 | * FIXME: there must be a better way to do this! |
| 2147 | */ |
Alex Elder | 84495f4 | 2012-02-15 07:43:55 -0600 | [diff] [blame] | 2148 | static char buf[SKIP_BUF_SIZE]; |
| 2149 | int skip = min((int) sizeof (buf), -con->in_base_pos); |
| 2150 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2151 | dout("skipping %d / %d bytes\n", skip, -con->in_base_pos); |
| 2152 | ret = ceph_tcp_recvmsg(con->sock, buf, skip); |
| 2153 | if (ret <= 0) |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2154 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2155 | con->in_base_pos += ret; |
| 2156 | if (con->in_base_pos) |
| 2157 | goto more; |
| 2158 | } |
| 2159 | if (con->in_tag == CEPH_MSGR_TAG_READY) { |
| 2160 | /* |
| 2161 | * what's next? |
| 2162 | */ |
| 2163 | ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1); |
| 2164 | if (ret <= 0) |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2165 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2166 | dout("try_read got tag %d\n", (int)con->in_tag); |
| 2167 | switch (con->in_tag) { |
| 2168 | case CEPH_MSGR_TAG_MSG: |
| 2169 | prepare_read_message(con); |
| 2170 | break; |
| 2171 | case CEPH_MSGR_TAG_ACK: |
| 2172 | prepare_read_ack(con); |
| 2173 | break; |
| 2174 | case CEPH_MSGR_TAG_CLOSE: |
Alex Elder | e27947c | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 2175 | clear_bit(CONNECTED, &con->state); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2176 | set_bit(CLOSED, &con->state); /* fixme */ |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2177 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2178 | default: |
| 2179 | goto bad_tag; |
| 2180 | } |
| 2181 | } |
| 2182 | if (con->in_tag == CEPH_MSGR_TAG_MSG) { |
| 2183 | ret = read_partial_message(con); |
| 2184 | if (ret <= 0) { |
| 2185 | switch (ret) { |
| 2186 | case -EBADMSG: |
| 2187 | con->error_msg = "bad crc"; |
| 2188 | ret = -EIO; |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2189 | break; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2190 | case -EIO: |
| 2191 | con->error_msg = "io error"; |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2192 | break; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2193 | } |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2194 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2195 | } |
| 2196 | if (con->in_tag == CEPH_MSGR_TAG_READY) |
| 2197 | goto more; |
| 2198 | process_message(con); |
| 2199 | goto more; |
| 2200 | } |
| 2201 | if (con->in_tag == CEPH_MSGR_TAG_ACK) { |
| 2202 | ret = read_partial_ack(con); |
| 2203 | if (ret <= 0) |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2204 | goto out; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2205 | process_ack(con); |
| 2206 | goto more; |
| 2207 | } |
| 2208 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2209 | out: |
Sage Weil | 98bdb0a | 2011-01-25 08:17:48 -0800 | [diff] [blame] | 2210 | dout("try_read done on %p ret %d\n", con, ret); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2211 | return ret; |
| 2212 | |
| 2213 | bad_tag: |
| 2214 | pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag); |
| 2215 | con->error_msg = "protocol error, garbage tag"; |
| 2216 | ret = -1; |
| 2217 | goto out; |
| 2218 | } |
| 2219 | |
| 2220 | |
| 2221 | /* |
| 2222 | * Atomically queue work on a connection. Bump @con reference to |
| 2223 | * avoid races with connection teardown. |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2224 | */ |
| 2225 | static void queue_con(struct ceph_connection *con) |
| 2226 | { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2227 | if (!con->ops->get(con)) { |
| 2228 | dout("queue_con %p ref count 0\n", con); |
| 2229 | return; |
| 2230 | } |
| 2231 | |
Tejun Heo | f363e45fd1 | 2011-01-03 14:49:46 +0100 | [diff] [blame] | 2232 | if (!queue_delayed_work(ceph_msgr_wq, &con->work, 0)) { |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2233 | dout("queue_con %p - already queued\n", con); |
| 2234 | con->ops->put(con); |
| 2235 | } else { |
| 2236 | dout("queue_con %p\n", con); |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | /* |
| 2241 | * Do some work on a connection. Drop a connection ref when we're done. |
| 2242 | */ |
| 2243 | static void con_work(struct work_struct *work) |
| 2244 | { |
| 2245 | struct ceph_connection *con = container_of(work, struct ceph_connection, |
| 2246 | work.work); |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 2247 | int ret; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2248 | |
Sage Weil | 9dd4658 | 2010-04-28 13:51:50 -0700 | [diff] [blame] | 2249 | mutex_lock(&con->mutex); |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 2250 | restart: |
Alex Elder | 188048b | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 2251 | if (test_and_clear_bit(SOCK_CLOSED, &con->flags)) { |
Alex Elder | e27947c | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 2252 | if (test_and_clear_bit(CONNECTED, &con->state)) |
| 2253 | con->error_msg = "socket closed"; |
Alex Elder | 7593af9 | 2012-05-24 11:55:03 -0500 | [diff] [blame] | 2254 | else if (test_and_clear_bit(NEGOTIATING, &con->state)) |
| 2255 | con->error_msg = "negotiation failed"; |
| 2256 | else if (test_and_clear_bit(CONNECTING, &con->state)) |
Alex Elder | 188048b | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 2257 | con->error_msg = "connection failed"; |
Alex Elder | 7593af9 | 2012-05-24 11:55:03 -0500 | [diff] [blame] | 2258 | else |
Alex Elder | e27947c | 2012-05-23 14:35:23 -0500 | [diff] [blame] | 2259 | con->error_msg = "unrecognized con state"; |
Alex Elder | 188048b | 2012-06-20 21:53:53 -0500 | [diff] [blame] | 2260 | goto fault; |
| 2261 | } |
| 2262 | |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2263 | if (test_and_clear_bit(BACKOFF, &con->flags)) { |
Sage Weil | 60bf8bf | 2011-03-04 12:24:28 -0800 | [diff] [blame] | 2264 | dout("con_work %p backing off\n", con); |
| 2265 | if (queue_delayed_work(ceph_msgr_wq, &con->work, |
| 2266 | round_jiffies_relative(con->delay))) { |
| 2267 | dout("con_work %p backoff %lu\n", con, con->delay); |
| 2268 | mutex_unlock(&con->mutex); |
| 2269 | return; |
| 2270 | } else { |
| 2271 | con->ops->put(con); |
| 2272 | dout("con_work %p FAILED to back off %lu\n", con, |
| 2273 | con->delay); |
| 2274 | } |
| 2275 | } |
Sage Weil | 9dd4658 | 2010-04-28 13:51:50 -0700 | [diff] [blame] | 2276 | |
Sage Weil | e00de34 | 2011-03-04 12:25:05 -0800 | [diff] [blame] | 2277 | if (test_bit(STANDBY, &con->state)) { |
| 2278 | dout("con_work %p STANDBY\n", con); |
| 2279 | goto done; |
| 2280 | } |
Sage Weil | 2e8cb10 | 2012-07-20 15:40:04 -0700 | [diff] [blame] | 2281 | if (test_bit(CLOSED, &con->state)) { |
| 2282 | dout("con_work %p CLOSED\n", con); |
| 2283 | BUG_ON(con->sock); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2284 | goto done; |
| 2285 | } |
| 2286 | if (test_and_clear_bit(OPENING, &con->state)) { |
| 2287 | /* reopen w/ new peer */ |
| 2288 | dout("con_work OPENING\n"); |
Sage Weil | 2e8cb10 | 2012-07-20 15:40:04 -0700 | [diff] [blame] | 2289 | BUG_ON(con->sock); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2290 | } |
| 2291 | |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 2292 | ret = try_read(con); |
| 2293 | if (ret == -EAGAIN) |
| 2294 | goto restart; |
Sage Weil | 3a140a0 | 2012-07-30 16:24:21 -0700 | [diff] [blame] | 2295 | if (ret < 0) { |
| 2296 | con->error_msg = "socket error on read"; |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 2297 | goto fault; |
Sage Weil | 3a140a0 | 2012-07-30 16:24:21 -0700 | [diff] [blame] | 2298 | } |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 2299 | |
| 2300 | ret = try_write(con); |
| 2301 | if (ret == -EAGAIN) |
| 2302 | goto restart; |
Sage Weil | 3a140a0 | 2012-07-30 16:24:21 -0700 | [diff] [blame] | 2303 | if (ret < 0) { |
| 2304 | con->error_msg = "socket error on write"; |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 2305 | goto fault; |
Sage Weil | 3a140a0 | 2012-07-30 16:24:21 -0700 | [diff] [blame] | 2306 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2307 | |
| 2308 | done: |
Sage Weil | 9dd4658 | 2010-04-28 13:51:50 -0700 | [diff] [blame] | 2309 | mutex_unlock(&con->mutex); |
Sage Weil | 9dd4658 | 2010-04-28 13:51:50 -0700 | [diff] [blame] | 2310 | done_unlocked: |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2311 | con->ops->put(con); |
Sage Weil | 0da5d70 | 2011-05-19 11:21:05 -0700 | [diff] [blame] | 2312 | return; |
| 2313 | |
| 2314 | fault: |
| 2315 | mutex_unlock(&con->mutex); |
| 2316 | ceph_fault(con); /* error/fault path */ |
| 2317 | goto done_unlocked; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | |
| 2321 | /* |
| 2322 | * Generic error/fault handler. A retry mechanism is used with |
| 2323 | * exponential backoff |
| 2324 | */ |
| 2325 | static void ceph_fault(struct ceph_connection *con) |
| 2326 | { |
Sage Weil | 3b5ede0 | 2012-07-20 15:22:53 -0700 | [diff] [blame] | 2327 | mutex_lock(&con->mutex); |
| 2328 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2329 | pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name), |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2330 | ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2331 | dout("fault %p state %lu to peer %s\n", |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2332 | con, con->state, ceph_pr_addr(&con->peer_addr.in_addr)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2333 | |
Sage Weil | 91e45ce3 | 2010-02-15 12:05:09 -0800 | [diff] [blame] | 2334 | if (test_bit(CLOSED, &con->state)) |
| 2335 | goto out_unlock; |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 2336 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2337 | con_close_socket(con); |
Sage Weil | 5e095e8 | 2009-12-14 14:30:34 -0800 | [diff] [blame] | 2338 | |
Sage Weil | 3b5ede0 | 2012-07-20 15:22:53 -0700 | [diff] [blame] | 2339 | if (test_bit(LOSSYTX, &con->flags)) { |
| 2340 | dout("fault on LOSSYTX channel\n"); |
| 2341 | goto out_unlock; |
| 2342 | } |
| 2343 | |
Sage Weil | 5e095e8 | 2009-12-14 14:30:34 -0800 | [diff] [blame] | 2344 | if (con->in_msg) { |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2345 | BUG_ON(con->in_msg->con != con); |
| 2346 | con->in_msg->con = NULL; |
Sage Weil | 5e095e8 | 2009-12-14 14:30:34 -0800 | [diff] [blame] | 2347 | ceph_msg_put(con->in_msg); |
| 2348 | con->in_msg = NULL; |
Sage Weil | 36eb71a | 2012-06-21 12:47:08 -0700 | [diff] [blame] | 2349 | con->ops->put(con); |
Sage Weil | 5e095e8 | 2009-12-14 14:30:34 -0800 | [diff] [blame] | 2350 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2351 | |
Sage Weil | e80a52d | 2010-02-25 12:40:45 -0800 | [diff] [blame] | 2352 | /* Requeue anything that hasn't been acked */ |
| 2353 | list_splice_init(&con->out_sent, &con->out_queue); |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 2354 | |
Sage Weil | e76661d | 2011-03-03 10:10:15 -0800 | [diff] [blame] | 2355 | /* If there are no messages queued or keepalive pending, place |
| 2356 | * the connection in a STANDBY state */ |
| 2357 | if (list_empty(&con->out_queue) && |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2358 | !test_bit(KEEPALIVE_PENDING, &con->flags)) { |
Sage Weil | e00de34 | 2011-03-04 12:25:05 -0800 | [diff] [blame] | 2359 | dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con); |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2360 | clear_bit(WRITE_PENDING, &con->flags); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2361 | set_bit(STANDBY, &con->state); |
Sage Weil | e80a52d | 2010-02-25 12:40:45 -0800 | [diff] [blame] | 2362 | } else { |
| 2363 | /* retry after a delay. */ |
| 2364 | if (con->delay == 0) |
| 2365 | con->delay = BASE_DELAY_INTERVAL; |
| 2366 | else if (con->delay < MAX_DELAY_INTERVAL) |
| 2367 | con->delay *= 2; |
Sage Weil | e80a52d | 2010-02-25 12:40:45 -0800 | [diff] [blame] | 2368 | con->ops->get(con); |
| 2369 | if (queue_delayed_work(ceph_msgr_wq, &con->work, |
Sage Weil | 60bf8bf | 2011-03-04 12:24:28 -0800 | [diff] [blame] | 2370 | round_jiffies_relative(con->delay))) { |
| 2371 | dout("fault queued %p delay %lu\n", con, con->delay); |
| 2372 | } else { |
Sage Weil | e80a52d | 2010-02-25 12:40:45 -0800 | [diff] [blame] | 2373 | con->ops->put(con); |
Sage Weil | 60bf8bf | 2011-03-04 12:24:28 -0800 | [diff] [blame] | 2374 | dout("fault failed to queue %p delay %lu, backoff\n", |
| 2375 | con, con->delay); |
| 2376 | /* |
| 2377 | * In many cases we see a socket state change |
| 2378 | * while con_work is running and end up |
| 2379 | * queuing (non-delayed) work, such that we |
| 2380 | * can't backoff with a delay. Set a flag so |
| 2381 | * that when con_work restarts we schedule the |
| 2382 | * delay then. |
| 2383 | */ |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2384 | set_bit(BACKOFF, &con->flags); |
Sage Weil | 60bf8bf | 2011-03-04 12:24:28 -0800 | [diff] [blame] | 2385 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2386 | } |
| 2387 | |
Sage Weil | 91e45ce3 | 2010-02-15 12:05:09 -0800 | [diff] [blame] | 2388 | out_unlock: |
| 2389 | mutex_unlock(&con->mutex); |
Sage Weil | 161fd65 | 2010-02-25 12:38:57 -0800 | [diff] [blame] | 2390 | /* |
| 2391 | * in case we faulted due to authentication, invalidate our |
| 2392 | * current tickets so that we can get new ones. |
Sage Weil | 213c99e | 2010-08-03 10:25:11 -0700 | [diff] [blame] | 2393 | */ |
Sage Weil | 161fd65 | 2010-02-25 12:38:57 -0800 | [diff] [blame] | 2394 | if (con->auth_retry && con->ops->invalidate_authorizer) { |
| 2395 | dout("calling invalidate_authorizer()\n"); |
| 2396 | con->ops->invalidate_authorizer(con); |
| 2397 | } |
| 2398 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2399 | if (con->ops->fault) |
| 2400 | con->ops->fault(con); |
| 2401 | } |
| 2402 | |
| 2403 | |
| 2404 | |
| 2405 | /* |
Alex Elder | 15d9882 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 2406 | * initialize a new messenger instance |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2407 | */ |
Alex Elder | 15d9882 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 2408 | void ceph_messenger_init(struct ceph_messenger *msgr, |
| 2409 | struct ceph_entity_addr *myaddr, |
| 2410 | u32 supported_features, |
| 2411 | u32 required_features, |
| 2412 | bool nocrc) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2413 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2414 | msgr->supported_features = supported_features; |
| 2415 | msgr->required_features = required_features; |
| 2416 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2417 | spin_lock_init(&msgr->global_seq_lock); |
| 2418 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2419 | if (myaddr) |
| 2420 | msgr->inst.addr = *myaddr; |
| 2421 | |
| 2422 | /* select a random nonce */ |
Sage Weil | ac8839d | 2010-01-27 14:28:10 -0800 | [diff] [blame] | 2423 | msgr->inst.addr.type = 0; |
Sage Weil | 103e2d3 | 2010-01-07 16:12:36 -0800 | [diff] [blame] | 2424 | get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce)); |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 2425 | encode_my_addr(msgr); |
Alex Elder | 15d9882 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 2426 | msgr->nocrc = nocrc; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2427 | |
Guanjun He | a2a3258 | 2012-07-08 19:50:33 -0700 | [diff] [blame] | 2428 | atomic_set(&msgr->stopping, 0); |
| 2429 | |
Alex Elder | 15d9882 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 2430 | dout("%s %p\n", __func__, msgr); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2431 | } |
Alex Elder | 15d9882 | 2012-05-26 23:26:43 -0500 | [diff] [blame] | 2432 | EXPORT_SYMBOL(ceph_messenger_init); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2433 | |
Sage Weil | e00de34 | 2011-03-04 12:25:05 -0800 | [diff] [blame] | 2434 | static void clear_standby(struct ceph_connection *con) |
| 2435 | { |
| 2436 | /* come back from STANDBY? */ |
| 2437 | if (test_and_clear_bit(STANDBY, &con->state)) { |
Sage Weil | e00de34 | 2011-03-04 12:25:05 -0800 | [diff] [blame] | 2438 | dout("clear_standby %p and ++connect_seq\n", con); |
| 2439 | con->connect_seq++; |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2440 | WARN_ON(test_bit(WRITE_PENDING, &con->flags)); |
| 2441 | WARN_ON(test_bit(KEEPALIVE_PENDING, &con->flags)); |
Sage Weil | e00de34 | 2011-03-04 12:25:05 -0800 | [diff] [blame] | 2442 | } |
| 2443 | } |
| 2444 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2445 | /* |
| 2446 | * Queue up an outgoing message on the given connection. |
| 2447 | */ |
| 2448 | void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg) |
| 2449 | { |
Sage Weil | a59b55a | 2012-07-20 15:34:04 -0700 | [diff] [blame] | 2450 | /* set src+dst */ |
| 2451 | msg->hdr.src = con->msgr->inst.name; |
| 2452 | BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len)); |
| 2453 | msg->needs_out_seq = true; |
| 2454 | |
| 2455 | mutex_lock(&con->mutex); |
| 2456 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2457 | if (test_bit(CLOSED, &con->state)) { |
| 2458 | dout("con_send %p closed, dropping %p\n", con, msg); |
| 2459 | ceph_msg_put(msg); |
Sage Weil | a59b55a | 2012-07-20 15:34:04 -0700 | [diff] [blame] | 2460 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2461 | return; |
| 2462 | } |
| 2463 | |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2464 | BUG_ON(msg->con != NULL); |
Sage Weil | 36eb71a | 2012-06-21 12:47:08 -0700 | [diff] [blame] | 2465 | msg->con = con->ops->get(con); |
Alex Elder | 92ce034 | 2012-06-04 14:43:33 -0500 | [diff] [blame] | 2466 | BUG_ON(msg->con == NULL); |
| 2467 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2468 | BUG_ON(!list_empty(&msg->list_head)); |
| 2469 | list_add_tail(&msg->list_head, &con->out_queue); |
| 2470 | dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg, |
| 2471 | ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type), |
| 2472 | ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), |
| 2473 | le32_to_cpu(msg->hdr.front_len), |
| 2474 | le32_to_cpu(msg->hdr.middle_len), |
| 2475 | le32_to_cpu(msg->hdr.data_len)); |
Sage Weil | 0065093 | 2012-07-20 15:33:04 -0700 | [diff] [blame] | 2476 | |
| 2477 | clear_standby(con); |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 2478 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2479 | |
| 2480 | /* if there wasn't anything waiting to send before, queue |
| 2481 | * new work */ |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2482 | if (test_and_set_bit(WRITE_PENDING, &con->flags) == 0) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2483 | queue_con(con); |
| 2484 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2485 | EXPORT_SYMBOL(ceph_con_send); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2486 | |
| 2487 | /* |
| 2488 | * Revoke a message that was previously queued for send |
| 2489 | */ |
Alex Elder | 6740a84 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2490 | void ceph_msg_revoke(struct ceph_msg *msg) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2491 | { |
Alex Elder | 6740a84 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2492 | struct ceph_connection *con = msg->con; |
| 2493 | |
| 2494 | if (!con) |
| 2495 | return; /* Message not in our possession */ |
| 2496 | |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 2497 | mutex_lock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2498 | if (!list_empty(&msg->list_head)) { |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2499 | dout("%s %p msg %p - was on queue\n", __func__, con, msg); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2500 | list_del_init(&msg->list_head); |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2501 | BUG_ON(msg->con == NULL); |
Sage Weil | 36eb71a | 2012-06-21 12:47:08 -0700 | [diff] [blame] | 2502 | msg->con->ops->put(msg->con); |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2503 | msg->con = NULL; |
Alex Elder | 92ce034 | 2012-06-04 14:43:33 -0500 | [diff] [blame] | 2504 | msg->hdr.seq = 0; |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2505 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2506 | ceph_msg_put(msg); |
Sage Weil | ed98ada | 2010-07-05 12:15:14 -0700 | [diff] [blame] | 2507 | } |
| 2508 | if (con->out_msg == msg) { |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2509 | dout("%s %p msg %p - was sending\n", __func__, con, msg); |
Sage Weil | ed98ada | 2010-07-05 12:15:14 -0700 | [diff] [blame] | 2510 | con->out_msg = NULL; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2511 | if (con->out_kvec_is_msg) { |
| 2512 | con->out_skip = con->out_kvec_bytes; |
| 2513 | con->out_kvec_is_msg = false; |
| 2514 | } |
Sage Weil | ed98ada | 2010-07-05 12:15:14 -0700 | [diff] [blame] | 2515 | msg->hdr.seq = 0; |
Alex Elder | 92ce034 | 2012-06-04 14:43:33 -0500 | [diff] [blame] | 2516 | |
| 2517 | ceph_msg_put(msg); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2518 | } |
Sage Weil | ec30264 | 2009-12-22 10:43:42 -0800 | [diff] [blame] | 2519 | mutex_unlock(&con->mutex); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2520 | } |
| 2521 | |
| 2522 | /* |
Yehuda Sadeh | 0d59ab8 | 2010-01-13 17:03:23 -0800 | [diff] [blame] | 2523 | * Revoke a message that we may be reading data into |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2524 | */ |
Alex Elder | 8921d11 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2525 | void ceph_msg_revoke_incoming(struct ceph_msg *msg) |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2526 | { |
Alex Elder | 8921d11 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2527 | struct ceph_connection *con; |
| 2528 | |
| 2529 | BUG_ON(msg == NULL); |
| 2530 | if (!msg->con) { |
| 2531 | dout("%s msg %p null con\n", __func__, msg); |
| 2532 | |
| 2533 | return; /* Message not in our possession */ |
| 2534 | } |
| 2535 | |
| 2536 | con = msg->con; |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2537 | mutex_lock(&con->mutex); |
Alex Elder | 8921d11 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2538 | if (con->in_msg == msg) { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 2539 | unsigned int front_len = le32_to_cpu(con->in_hdr.front_len); |
| 2540 | unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len); |
| 2541 | unsigned int data_len = le32_to_cpu(con->in_hdr.data_len); |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2542 | |
| 2543 | /* skip rest of message */ |
Alex Elder | 8921d11 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2544 | dout("%s %p msg %p revoked\n", __func__, con, msg); |
| 2545 | con->in_base_pos = con->in_base_pos - |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2546 | sizeof(struct ceph_msg_header) - |
Yehuda Sadeh | 0d59ab8 | 2010-01-13 17:03:23 -0800 | [diff] [blame] | 2547 | front_len - |
| 2548 | middle_len - |
| 2549 | data_len - |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2550 | sizeof(struct ceph_msg_footer); |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2551 | ceph_msg_put(con->in_msg); |
| 2552 | con->in_msg = NULL; |
| 2553 | con->in_tag = CEPH_MSGR_TAG_READY; |
Sage Weil | 684be25 | 2010-04-21 20:45:59 -0700 | [diff] [blame] | 2554 | con->in_seq++; |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2555 | } else { |
Alex Elder | 8921d11 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2556 | dout("%s %p in_msg %p msg %p no-op\n", |
| 2557 | __func__, con, con->in_msg, msg); |
Sage Weil | 350b1c3 | 2009-12-22 10:45:45 -0800 | [diff] [blame] | 2558 | } |
| 2559 | mutex_unlock(&con->mutex); |
| 2560 | } |
| 2561 | |
| 2562 | /* |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2563 | * Queue a keepalive byte to ensure the tcp connection is alive. |
| 2564 | */ |
| 2565 | void ceph_con_keepalive(struct ceph_connection *con) |
| 2566 | { |
Sage Weil | e00de34 | 2011-03-04 12:25:05 -0800 | [diff] [blame] | 2567 | dout("con_keepalive %p\n", con); |
Sage Weil | 0065093 | 2012-07-20 15:33:04 -0700 | [diff] [blame] | 2568 | mutex_lock(&con->mutex); |
Sage Weil | e00de34 | 2011-03-04 12:25:05 -0800 | [diff] [blame] | 2569 | clear_standby(con); |
Sage Weil | 0065093 | 2012-07-20 15:33:04 -0700 | [diff] [blame] | 2570 | mutex_unlock(&con->mutex); |
Alex Elder | 928443c | 2012-05-22 11:41:43 -0500 | [diff] [blame] | 2571 | if (test_and_set_bit(KEEPALIVE_PENDING, &con->flags) == 0 && |
| 2572 | test_and_set_bit(WRITE_PENDING, &con->flags) == 0) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2573 | queue_con(con); |
| 2574 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2575 | EXPORT_SYMBOL(ceph_con_keepalive); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2576 | |
| 2577 | |
| 2578 | /* |
| 2579 | * construct a new message with given type, size |
| 2580 | * the new msg has a ref count of 1. |
| 2581 | */ |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 2582 | struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags, |
| 2583 | bool can_fail) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2584 | { |
| 2585 | struct ceph_msg *m; |
| 2586 | |
Yehuda Sadeh | 34d2376 | 2010-04-06 14:33:58 -0700 | [diff] [blame] | 2587 | m = kmalloc(sizeof(*m), flags); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2588 | if (m == NULL) |
| 2589 | goto out; |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2590 | kref_init(&m->kref); |
Alex Elder | 38941f8 | 2012-06-01 14:56:43 -0500 | [diff] [blame] | 2591 | |
| 2592 | m->con = NULL; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2593 | INIT_LIST_HEAD(&m->list_head); |
| 2594 | |
Sage Weil | 45c6ceb | 2010-05-11 15:01:51 -0700 | [diff] [blame] | 2595 | m->hdr.tid = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2596 | m->hdr.type = cpu_to_le16(type); |
Sage Weil | 45c6ceb | 2010-05-11 15:01:51 -0700 | [diff] [blame] | 2597 | m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT); |
| 2598 | m->hdr.version = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2599 | m->hdr.front_len = cpu_to_le32(front_len); |
| 2600 | m->hdr.middle_len = 0; |
Sage Weil | bb25766 | 2010-04-01 16:07:23 -0700 | [diff] [blame] | 2601 | m->hdr.data_len = 0; |
| 2602 | m->hdr.data_off = 0; |
Sage Weil | 45c6ceb | 2010-05-11 15:01:51 -0700 | [diff] [blame] | 2603 | m->hdr.reserved = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2604 | m->footer.front_crc = 0; |
| 2605 | m->footer.middle_crc = 0; |
| 2606 | m->footer.data_crc = 0; |
Sage Weil | 45c6ceb | 2010-05-11 15:01:51 -0700 | [diff] [blame] | 2607 | m->footer.flags = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2608 | m->front_max = front_len; |
| 2609 | m->front_is_vmalloc = false; |
| 2610 | m->more_to_follow = false; |
Jim Schutt | c0d5f9d | 2011-09-16 08:27:31 -0600 | [diff] [blame] | 2611 | m->ack_stamp = 0; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2612 | m->pool = NULL; |
| 2613 | |
Henry C Chang | ca20892 | 2011-05-03 02:29:56 +0000 | [diff] [blame] | 2614 | /* middle */ |
| 2615 | m->middle = NULL; |
| 2616 | |
| 2617 | /* data */ |
| 2618 | m->nr_pages = 0; |
| 2619 | m->page_alignment = 0; |
| 2620 | m->pages = NULL; |
| 2621 | m->pagelist = NULL; |
| 2622 | m->bio = NULL; |
| 2623 | m->bio_iter = NULL; |
| 2624 | m->bio_seg = 0; |
| 2625 | m->trail = NULL; |
| 2626 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2627 | /* front */ |
| 2628 | if (front_len) { |
| 2629 | if (front_len > PAGE_CACHE_SIZE) { |
Yehuda Sadeh | 34d2376 | 2010-04-06 14:33:58 -0700 | [diff] [blame] | 2630 | m->front.iov_base = __vmalloc(front_len, flags, |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2631 | PAGE_KERNEL); |
| 2632 | m->front_is_vmalloc = true; |
| 2633 | } else { |
Yehuda Sadeh | 34d2376 | 2010-04-06 14:33:58 -0700 | [diff] [blame] | 2634 | m->front.iov_base = kmalloc(front_len, flags); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2635 | } |
| 2636 | if (m->front.iov_base == NULL) { |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 2637 | dout("ceph_msg_new can't allocate %d bytes\n", |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2638 | front_len); |
| 2639 | goto out2; |
| 2640 | } |
| 2641 | } else { |
| 2642 | m->front.iov_base = NULL; |
| 2643 | } |
| 2644 | m->front.iov_len = front_len; |
| 2645 | |
Sage Weil | bb25766 | 2010-04-01 16:07:23 -0700 | [diff] [blame] | 2646 | dout("ceph_msg_new %p front %d\n", m, front_len); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2647 | return m; |
| 2648 | |
| 2649 | out2: |
| 2650 | ceph_msg_put(m); |
| 2651 | out: |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 2652 | if (!can_fail) { |
| 2653 | pr_err("msg_new can't create type %d front %d\n", type, |
| 2654 | front_len); |
Sage Weil | f0ed1b7 | 2011-08-09 15:05:07 -0700 | [diff] [blame] | 2655 | WARN_ON(1); |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 2656 | } else { |
| 2657 | dout("msg_new can't create type %d front %d\n", type, |
| 2658 | front_len); |
| 2659 | } |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 2660 | return NULL; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2661 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2662 | EXPORT_SYMBOL(ceph_msg_new); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2663 | |
| 2664 | /* |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2665 | * Allocate "middle" portion of a message, if it is needed and wasn't |
| 2666 | * allocated by alloc_msg. This allows us to read a small fixed-size |
| 2667 | * per-type header in the front and then gracefully fail (i.e., |
| 2668 | * propagate the error to the caller based on info in the front) when |
| 2669 | * the middle is too large. |
| 2670 | */ |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2671 | static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2672 | { |
| 2673 | int type = le16_to_cpu(msg->hdr.type); |
| 2674 | int middle_len = le32_to_cpu(msg->hdr.middle_len); |
| 2675 | |
| 2676 | dout("alloc_middle %p type %d %s middle_len %d\n", msg, type, |
| 2677 | ceph_msg_type_name(type), middle_len); |
| 2678 | BUG_ON(!middle_len); |
| 2679 | BUG_ON(msg->middle); |
| 2680 | |
Sage Weil | b6c1d5b | 2009-12-07 12:17:17 -0800 | [diff] [blame] | 2681 | msg->middle = ceph_buffer_new(middle_len, GFP_NOFS); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2682 | if (!msg->middle) |
| 2683 | return -ENOMEM; |
| 2684 | return 0; |
| 2685 | } |
| 2686 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2687 | /* |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2688 | * Allocate a message for receiving an incoming message on a |
| 2689 | * connection, and save the result in con->in_msg. Uses the |
| 2690 | * connection's private alloc_msg op if available. |
| 2691 | * |
| 2692 | * Returns true if the message should be skipped, false otherwise. |
| 2693 | * If true is returned (skip message), con->in_msg will be NULL. |
| 2694 | * If false is returned, con->in_msg will contain a pointer to the |
| 2695 | * newly-allocated message, or NULL in case of memory exhaustion. |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2696 | */ |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2697 | static bool ceph_con_in_msg_alloc(struct ceph_connection *con, |
| 2698 | struct ceph_msg_header *hdr) |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2699 | { |
| 2700 | int type = le16_to_cpu(hdr->type); |
| 2701 | int front_len = le32_to_cpu(hdr->front_len); |
| 2702 | int middle_len = le32_to_cpu(hdr->middle_len); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2703 | int ret; |
| 2704 | |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2705 | BUG_ON(con->in_msg != NULL); |
| 2706 | |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2707 | if (con->ops->alloc_msg) { |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2708 | int skip = 0; |
| 2709 | |
Yehuda Sadeh | 0547a9b | 2010-01-11 14:47:13 -0800 | [diff] [blame] | 2710 | mutex_unlock(&con->mutex); |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2711 | con->in_msg = con->ops->alloc_msg(con, hdr, &skip); |
Yehuda Sadeh | 0547a9b | 2010-01-11 14:47:13 -0800 | [diff] [blame] | 2712 | mutex_lock(&con->mutex); |
Alex Elder | 92ce034 | 2012-06-04 14:43:33 -0500 | [diff] [blame] | 2713 | if (con->in_msg) { |
Sage Weil | 36eb71a | 2012-06-21 12:47:08 -0700 | [diff] [blame] | 2714 | con->in_msg->con = con->ops->get(con); |
Alex Elder | 92ce034 | 2012-06-04 14:43:33 -0500 | [diff] [blame] | 2715 | BUG_ON(con->in_msg->con == NULL); |
| 2716 | } |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2717 | if (skip) |
| 2718 | con->in_msg = NULL; |
| 2719 | |
| 2720 | if (!con->in_msg) |
| 2721 | return skip != 0; |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2722 | } |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2723 | if (!con->in_msg) { |
| 2724 | con->in_msg = ceph_msg_new(type, front_len, GFP_NOFS, false); |
| 2725 | if (!con->in_msg) { |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2726 | pr_err("unable to allocate msg type %d len %d\n", |
| 2727 | type, front_len); |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2728 | return false; |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2729 | } |
Sage Weil | 36eb71a | 2012-06-21 12:47:08 -0700 | [diff] [blame] | 2730 | con->in_msg->con = con->ops->get(con); |
Alex Elder | 92ce034 | 2012-06-04 14:43:33 -0500 | [diff] [blame] | 2731 | BUG_ON(con->in_msg->con == NULL); |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2732 | con->in_msg->page_alignment = le16_to_cpu(hdr->data_off); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2733 | } |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2734 | memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr)); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2735 | |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2736 | if (middle_len && !con->in_msg->middle) { |
| 2737 | ret = ceph_alloc_middle(con, con->in_msg); |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2738 | if (ret < 0) { |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2739 | ceph_msg_put(con->in_msg); |
| 2740 | con->in_msg = NULL; |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2741 | } |
| 2742 | } |
Yehuda Sadeh | 9d7f0f1 | 2010-01-11 10:32:02 -0800 | [diff] [blame] | 2743 | |
Alex Elder | 1c20f2d | 2012-06-04 14:43:32 -0500 | [diff] [blame] | 2744 | return false; |
Yehuda Sadeh | 2450418 | 2010-01-08 13:58:34 -0800 | [diff] [blame] | 2745 | } |
| 2746 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2747 | |
| 2748 | /* |
| 2749 | * Free a generically kmalloc'd message. |
| 2750 | */ |
| 2751 | void ceph_msg_kfree(struct ceph_msg *m) |
| 2752 | { |
| 2753 | dout("msg_kfree %p\n", m); |
| 2754 | if (m->front_is_vmalloc) |
| 2755 | vfree(m->front.iov_base); |
| 2756 | else |
| 2757 | kfree(m->front.iov_base); |
| 2758 | kfree(m); |
| 2759 | } |
| 2760 | |
| 2761 | /* |
| 2762 | * Drop a msg ref. Destroy as needed. |
| 2763 | */ |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2764 | void ceph_msg_last_put(struct kref *kref) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2765 | { |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2766 | struct ceph_msg *m = container_of(kref, struct ceph_msg, kref); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2767 | |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2768 | dout("ceph_msg_put last one on %p\n", m); |
| 2769 | WARN_ON(!list_empty(&m->list_head)); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2770 | |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2771 | /* drop middle, data, if any */ |
| 2772 | if (m->middle) { |
| 2773 | ceph_buffer_put(m->middle); |
| 2774 | m->middle = NULL; |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2775 | } |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2776 | m->nr_pages = 0; |
| 2777 | m->pages = NULL; |
| 2778 | |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 2779 | if (m->pagelist) { |
| 2780 | ceph_pagelist_release(m->pagelist); |
| 2781 | kfree(m->pagelist); |
| 2782 | m->pagelist = NULL; |
| 2783 | } |
| 2784 | |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 2785 | m->trail = NULL; |
| 2786 | |
Sage Weil | c2e552e | 2009-12-07 15:55:05 -0800 | [diff] [blame] | 2787 | if (m->pool) |
| 2788 | ceph_msgpool_put(m->pool, m); |
| 2789 | else |
| 2790 | ceph_msg_kfree(m); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 2791 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2792 | EXPORT_SYMBOL(ceph_msg_last_put); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 2793 | |
| 2794 | void ceph_msg_dump(struct ceph_msg *msg) |
| 2795 | { |
| 2796 | pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg, |
| 2797 | msg->front_max, msg->nr_pages); |
| 2798 | print_hex_dump(KERN_DEBUG, "header: ", |
| 2799 | DUMP_PREFIX_OFFSET, 16, 1, |
| 2800 | &msg->hdr, sizeof(msg->hdr), true); |
| 2801 | print_hex_dump(KERN_DEBUG, " front: ", |
| 2802 | DUMP_PREFIX_OFFSET, 16, 1, |
| 2803 | msg->front.iov_base, msg->front.iov_len, true); |
| 2804 | if (msg->middle) |
| 2805 | print_hex_dump(KERN_DEBUG, "middle: ", |
| 2806 | DUMP_PREFIX_OFFSET, 16, 1, |
| 2807 | msg->middle->vec.iov_base, |
| 2808 | msg->middle->vec.iov_len, true); |
| 2809 | print_hex_dump(KERN_DEBUG, "footer: ", |
| 2810 | DUMP_PREFIX_OFFSET, 16, 1, |
| 2811 | &msg->footer, sizeof(msg->footer), true); |
| 2812 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2813 | EXPORT_SYMBOL(ceph_msg_dump); |