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