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