Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 2 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3 | #include <linux/ceph/ceph_debug.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 4 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 5 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 6 | #include <linux/slab.h> |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 7 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 8 | #include <linux/ceph/libceph.h> |
| 9 | #include <linux/ceph/osdmap.h> |
| 10 | #include <linux/ceph/decode.h> |
| 11 | #include <linux/crush/hash.h> |
| 12 | #include <linux/crush/mapper.h> |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 13 | |
Ilya Dryomov | 0bb05da | 2017-06-22 19:44:06 +0200 | [diff] [blame] | 14 | char *ceph_osdmap_state_str(char *str, int len, u32 state) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 15 | { |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 16 | if (!len) |
Cong Ding | 1ec3911 | 2013-01-25 17:48:59 -0600 | [diff] [blame] | 17 | return str; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 18 | |
Cong Ding | 1ec3911 | 2013-01-25 17:48:59 -0600 | [diff] [blame] | 19 | if ((state & CEPH_OSD_EXISTS) && (state & CEPH_OSD_UP)) |
| 20 | snprintf(str, len, "exists, up"); |
| 21 | else if (state & CEPH_OSD_EXISTS) |
| 22 | snprintf(str, len, "exists"); |
| 23 | else if (state & CEPH_OSD_UP) |
| 24 | snprintf(str, len, "up"); |
| 25 | else |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 26 | snprintf(str, len, "doesn't exist"); |
Cong Ding | 1ec3911 | 2013-01-25 17:48:59 -0600 | [diff] [blame] | 27 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 28 | return str; |
| 29 | } |
| 30 | |
| 31 | /* maps */ |
| 32 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 33 | static int calc_bits_of(unsigned int t) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 34 | { |
| 35 | int b = 0; |
| 36 | while (t) { |
| 37 | t = t >> 1; |
| 38 | b++; |
| 39 | } |
| 40 | return b; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * the foo_mask is the smallest value 2^n-1 that is >= foo. |
| 45 | */ |
| 46 | static void calc_pg_masks(struct ceph_pg_pool_info *pi) |
| 47 | { |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 48 | pi->pg_num_mask = (1 << calc_bits_of(pi->pg_num-1)) - 1; |
| 49 | pi->pgp_num_mask = (1 << calc_bits_of(pi->pgp_num-1)) - 1; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /* |
| 53 | * decode crush map |
| 54 | */ |
| 55 | static int crush_decode_uniform_bucket(void **p, void *end, |
| 56 | struct crush_bucket_uniform *b) |
| 57 | { |
| 58 | dout("crush_decode_uniform_bucket %p to %p\n", *p, end); |
| 59 | ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 60 | b->item_weight = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 61 | return 0; |
| 62 | bad: |
| 63 | return -EINVAL; |
| 64 | } |
| 65 | |
| 66 | static int crush_decode_list_bucket(void **p, void *end, |
| 67 | struct crush_bucket_list *b) |
| 68 | { |
| 69 | int j; |
| 70 | dout("crush_decode_list_bucket %p to %p\n", *p, end); |
| 71 | b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); |
| 72 | if (b->item_weights == NULL) |
| 73 | return -ENOMEM; |
| 74 | b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); |
| 75 | if (b->sum_weights == NULL) |
| 76 | return -ENOMEM; |
| 77 | ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad); |
| 78 | for (j = 0; j < b->h.size; j++) { |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 79 | b->item_weights[j] = ceph_decode_32(p); |
| 80 | b->sum_weights[j] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 81 | } |
| 82 | return 0; |
| 83 | bad: |
| 84 | return -EINVAL; |
| 85 | } |
| 86 | |
| 87 | static int crush_decode_tree_bucket(void **p, void *end, |
| 88 | struct crush_bucket_tree *b) |
| 89 | { |
| 90 | int j; |
| 91 | dout("crush_decode_tree_bucket %p to %p\n", *p, end); |
Ilya Dryomov | 82cd003 | 2015-06-29 19:30:23 +0300 | [diff] [blame] | 92 | ceph_decode_8_safe(p, end, b->num_nodes, bad); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 93 | b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS); |
| 94 | if (b->node_weights == NULL) |
| 95 | return -ENOMEM; |
| 96 | ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad); |
| 97 | for (j = 0; j < b->num_nodes; j++) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 98 | b->node_weights[j] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 99 | return 0; |
| 100 | bad: |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
| 104 | static int crush_decode_straw_bucket(void **p, void *end, |
| 105 | struct crush_bucket_straw *b) |
| 106 | { |
| 107 | int j; |
| 108 | dout("crush_decode_straw_bucket %p to %p\n", *p, end); |
| 109 | b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); |
| 110 | if (b->item_weights == NULL) |
| 111 | return -ENOMEM; |
| 112 | b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); |
| 113 | if (b->straws == NULL) |
| 114 | return -ENOMEM; |
| 115 | ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad); |
| 116 | for (j = 0; j < b->h.size; j++) { |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 117 | b->item_weights[j] = ceph_decode_32(p); |
| 118 | b->straws[j] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 119 | } |
| 120 | return 0; |
| 121 | bad: |
| 122 | return -EINVAL; |
| 123 | } |
| 124 | |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 125 | static int crush_decode_straw2_bucket(void **p, void *end, |
| 126 | struct crush_bucket_straw2 *b) |
| 127 | { |
| 128 | int j; |
| 129 | dout("crush_decode_straw2_bucket %p to %p\n", *p, end); |
| 130 | b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); |
| 131 | if (b->item_weights == NULL) |
| 132 | return -ENOMEM; |
| 133 | ceph_decode_need(p, end, b->h.size * sizeof(u32), bad); |
| 134 | for (j = 0; j < b->h.size; j++) |
| 135 | b->item_weights[j] = ceph_decode_32(p); |
| 136 | return 0; |
| 137 | bad: |
| 138 | return -EINVAL; |
| 139 | } |
| 140 | |
Ilya Dryomov | 5cf9c4a | 2017-06-22 19:44:05 +0200 | [diff] [blame] | 141 | static struct crush_choose_arg_map *alloc_choose_arg_map(void) |
| 142 | { |
| 143 | struct crush_choose_arg_map *arg_map; |
| 144 | |
| 145 | arg_map = kzalloc(sizeof(*arg_map), GFP_NOIO); |
| 146 | if (!arg_map) |
| 147 | return NULL; |
| 148 | |
| 149 | RB_CLEAR_NODE(&arg_map->node); |
| 150 | return arg_map; |
| 151 | } |
| 152 | |
| 153 | static void free_choose_arg_map(struct crush_choose_arg_map *arg_map) |
| 154 | { |
| 155 | if (arg_map) { |
| 156 | int i, j; |
| 157 | |
| 158 | WARN_ON(!RB_EMPTY_NODE(&arg_map->node)); |
| 159 | |
| 160 | for (i = 0; i < arg_map->size; i++) { |
| 161 | struct crush_choose_arg *arg = &arg_map->args[i]; |
| 162 | |
| 163 | for (j = 0; j < arg->weight_set_size; j++) |
| 164 | kfree(arg->weight_set[j].weights); |
| 165 | kfree(arg->weight_set); |
| 166 | kfree(arg->ids); |
| 167 | } |
| 168 | kfree(arg_map->args); |
| 169 | kfree(arg_map); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | DEFINE_RB_FUNCS(choose_arg_map, struct crush_choose_arg_map, choose_args_index, |
| 174 | node); |
| 175 | |
| 176 | void clear_choose_args(struct crush_map *c) |
| 177 | { |
| 178 | while (!RB_EMPTY_ROOT(&c->choose_args)) { |
| 179 | struct crush_choose_arg_map *arg_map = |
| 180 | rb_entry(rb_first(&c->choose_args), |
| 181 | struct crush_choose_arg_map, node); |
| 182 | |
| 183 | erase_choose_arg_map(&c->choose_args, arg_map); |
| 184 | free_choose_arg_map(arg_map); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | static u32 *decode_array_32_alloc(void **p, void *end, u32 *plen) |
| 189 | { |
| 190 | u32 *a = NULL; |
| 191 | u32 len; |
| 192 | int ret; |
| 193 | |
| 194 | ceph_decode_32_safe(p, end, len, e_inval); |
| 195 | if (len) { |
| 196 | u32 i; |
| 197 | |
| 198 | a = kmalloc_array(len, sizeof(u32), GFP_NOIO); |
| 199 | if (!a) { |
| 200 | ret = -ENOMEM; |
| 201 | goto fail; |
| 202 | } |
| 203 | |
| 204 | ceph_decode_need(p, end, len * sizeof(u32), e_inval); |
| 205 | for (i = 0; i < len; i++) |
| 206 | a[i] = ceph_decode_32(p); |
| 207 | } |
| 208 | |
| 209 | *plen = len; |
| 210 | return a; |
| 211 | |
| 212 | e_inval: |
| 213 | ret = -EINVAL; |
| 214 | fail: |
| 215 | kfree(a); |
| 216 | return ERR_PTR(ret); |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Assumes @arg is zero-initialized. |
| 221 | */ |
| 222 | static int decode_choose_arg(void **p, void *end, struct crush_choose_arg *arg) |
| 223 | { |
| 224 | int ret; |
| 225 | |
| 226 | ceph_decode_32_safe(p, end, arg->weight_set_size, e_inval); |
| 227 | if (arg->weight_set_size) { |
| 228 | u32 i; |
| 229 | |
| 230 | arg->weight_set = kmalloc_array(arg->weight_set_size, |
| 231 | sizeof(*arg->weight_set), |
| 232 | GFP_NOIO); |
| 233 | if (!arg->weight_set) |
| 234 | return -ENOMEM; |
| 235 | |
| 236 | for (i = 0; i < arg->weight_set_size; i++) { |
| 237 | struct crush_weight_set *w = &arg->weight_set[i]; |
| 238 | |
| 239 | w->weights = decode_array_32_alloc(p, end, &w->size); |
| 240 | if (IS_ERR(w->weights)) { |
| 241 | ret = PTR_ERR(w->weights); |
| 242 | w->weights = NULL; |
| 243 | return ret; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | arg->ids = decode_array_32_alloc(p, end, &arg->ids_size); |
| 249 | if (IS_ERR(arg->ids)) { |
| 250 | ret = PTR_ERR(arg->ids); |
| 251 | arg->ids = NULL; |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | |
| 257 | e_inval: |
| 258 | return -EINVAL; |
| 259 | } |
| 260 | |
| 261 | static int decode_choose_args(void **p, void *end, struct crush_map *c) |
| 262 | { |
| 263 | struct crush_choose_arg_map *arg_map = NULL; |
| 264 | u32 num_choose_arg_maps, num_buckets; |
| 265 | int ret; |
| 266 | |
| 267 | ceph_decode_32_safe(p, end, num_choose_arg_maps, e_inval); |
| 268 | while (num_choose_arg_maps--) { |
| 269 | arg_map = alloc_choose_arg_map(); |
| 270 | if (!arg_map) { |
| 271 | ret = -ENOMEM; |
| 272 | goto fail; |
| 273 | } |
| 274 | |
| 275 | ceph_decode_64_safe(p, end, arg_map->choose_args_index, |
| 276 | e_inval); |
| 277 | arg_map->size = c->max_buckets; |
| 278 | arg_map->args = kcalloc(arg_map->size, sizeof(*arg_map->args), |
| 279 | GFP_NOIO); |
| 280 | if (!arg_map->args) { |
| 281 | ret = -ENOMEM; |
| 282 | goto fail; |
| 283 | } |
| 284 | |
| 285 | ceph_decode_32_safe(p, end, num_buckets, e_inval); |
| 286 | while (num_buckets--) { |
| 287 | struct crush_choose_arg *arg; |
| 288 | u32 bucket_index; |
| 289 | |
| 290 | ceph_decode_32_safe(p, end, bucket_index, e_inval); |
| 291 | if (bucket_index >= arg_map->size) |
| 292 | goto e_inval; |
| 293 | |
| 294 | arg = &arg_map->args[bucket_index]; |
| 295 | ret = decode_choose_arg(p, end, arg); |
| 296 | if (ret) |
| 297 | goto fail; |
Ilya Dryomov | c7ed1a4 | 2017-07-24 15:49:52 +0200 | [diff] [blame] | 298 | |
| 299 | if (arg->ids_size && |
| 300 | arg->ids_size != c->buckets[bucket_index]->size) |
| 301 | goto e_inval; |
Ilya Dryomov | 5cf9c4a | 2017-06-22 19:44:05 +0200 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | insert_choose_arg_map(&c->choose_args, arg_map); |
| 305 | } |
| 306 | |
| 307 | return 0; |
| 308 | |
| 309 | e_inval: |
| 310 | ret = -EINVAL; |
| 311 | fail: |
| 312 | free_choose_arg_map(arg_map); |
| 313 | return ret; |
| 314 | } |
| 315 | |
Ilya Dryomov | 66a0e2d | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 316 | static void crush_finalize(struct crush_map *c) |
| 317 | { |
| 318 | __s32 b; |
| 319 | |
| 320 | /* Space for the array of pointers to per-bucket workspace */ |
| 321 | c->working_size = sizeof(struct crush_work) + |
| 322 | c->max_buckets * sizeof(struct crush_work_bucket *); |
| 323 | |
| 324 | for (b = 0; b < c->max_buckets; b++) { |
| 325 | if (!c->buckets[b]) |
| 326 | continue; |
| 327 | |
| 328 | switch (c->buckets[b]->alg) { |
| 329 | default: |
| 330 | /* |
| 331 | * The base case, permutation variables and |
| 332 | * the pointer to the permutation array. |
| 333 | */ |
| 334 | c->working_size += sizeof(struct crush_work_bucket); |
| 335 | break; |
| 336 | } |
| 337 | /* Every bucket has a permutation array. */ |
| 338 | c->working_size += c->buckets[b]->size * sizeof(__u32); |
| 339 | } |
| 340 | } |
| 341 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 342 | static struct crush_map *crush_decode(void *pbyval, void *end) |
| 343 | { |
| 344 | struct crush_map *c; |
Ilya Dryomov | c2acfd9 | 2017-07-13 15:57:26 +0200 | [diff] [blame] | 345 | int err; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 346 | int i, j; |
| 347 | void **p = &pbyval; |
| 348 | void *start = pbyval; |
| 349 | u32 magic; |
| 350 | |
| 351 | dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p)); |
| 352 | |
| 353 | c = kzalloc(sizeof(*c), GFP_NOFS); |
| 354 | if (c == NULL) |
| 355 | return ERR_PTR(-ENOMEM); |
| 356 | |
Ilya Dryomov | 5cf9c4a | 2017-06-22 19:44:05 +0200 | [diff] [blame] | 357 | c->choose_args = RB_ROOT; |
| 358 | |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 359 | /* set tunables to default values */ |
| 360 | c->choose_local_tries = 2; |
| 361 | c->choose_local_fallback_tries = 5; |
| 362 | c->choose_total_tries = 19; |
Jim Schutt | 1604f48 | 2012-11-30 09:15:25 -0700 | [diff] [blame] | 363 | c->chooseleaf_descend_once = 0; |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 364 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 365 | ceph_decode_need(p, end, 4*sizeof(u32), bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 366 | magic = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 367 | if (magic != CRUSH_MAGIC) { |
| 368 | pr_err("crush_decode magic %x != current %x\n", |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 369 | (unsigned int)magic, (unsigned int)CRUSH_MAGIC); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 370 | goto bad; |
| 371 | } |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 372 | c->max_buckets = ceph_decode_32(p); |
| 373 | c->max_rules = ceph_decode_32(p); |
| 374 | c->max_devices = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 375 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 376 | c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS); |
| 377 | if (c->buckets == NULL) |
| 378 | goto badmem; |
| 379 | c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS); |
| 380 | if (c->rules == NULL) |
| 381 | goto badmem; |
| 382 | |
| 383 | /* buckets */ |
| 384 | for (i = 0; i < c->max_buckets; i++) { |
| 385 | int size = 0; |
| 386 | u32 alg; |
| 387 | struct crush_bucket *b; |
| 388 | |
| 389 | ceph_decode_32_safe(p, end, alg, bad); |
| 390 | if (alg == 0) { |
| 391 | c->buckets[i] = NULL; |
| 392 | continue; |
| 393 | } |
| 394 | dout("crush_decode bucket %d off %x %p to %p\n", |
| 395 | i, (int)(*p-start), *p, end); |
| 396 | |
| 397 | switch (alg) { |
| 398 | case CRUSH_BUCKET_UNIFORM: |
| 399 | size = sizeof(struct crush_bucket_uniform); |
| 400 | break; |
| 401 | case CRUSH_BUCKET_LIST: |
| 402 | size = sizeof(struct crush_bucket_list); |
| 403 | break; |
| 404 | case CRUSH_BUCKET_TREE: |
| 405 | size = sizeof(struct crush_bucket_tree); |
| 406 | break; |
| 407 | case CRUSH_BUCKET_STRAW: |
| 408 | size = sizeof(struct crush_bucket_straw); |
| 409 | break; |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 410 | case CRUSH_BUCKET_STRAW2: |
| 411 | size = sizeof(struct crush_bucket_straw2); |
| 412 | break; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 413 | default: |
| 414 | goto bad; |
| 415 | } |
| 416 | BUG_ON(size == 0); |
| 417 | b = c->buckets[i] = kzalloc(size, GFP_NOFS); |
| 418 | if (b == NULL) |
| 419 | goto badmem; |
| 420 | |
| 421 | ceph_decode_need(p, end, 4*sizeof(u32), bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 422 | b->id = ceph_decode_32(p); |
| 423 | b->type = ceph_decode_16(p); |
Sage Weil | fb69039 | 2009-11-07 20:18:22 -0800 | [diff] [blame] | 424 | b->alg = ceph_decode_8(p); |
| 425 | b->hash = ceph_decode_8(p); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 426 | b->weight = ceph_decode_32(p); |
| 427 | b->size = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 428 | |
| 429 | dout("crush_decode bucket size %d off %x %p to %p\n", |
| 430 | b->size, (int)(*p-start), *p, end); |
| 431 | |
| 432 | b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS); |
| 433 | if (b->items == NULL) |
| 434 | goto badmem; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 435 | |
| 436 | ceph_decode_need(p, end, b->size*sizeof(u32), bad); |
| 437 | for (j = 0; j < b->size; j++) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 438 | b->items[j] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 439 | |
| 440 | switch (b->alg) { |
| 441 | case CRUSH_BUCKET_UNIFORM: |
| 442 | err = crush_decode_uniform_bucket(p, end, |
| 443 | (struct crush_bucket_uniform *)b); |
| 444 | if (err < 0) |
Ilya Dryomov | c2acfd9 | 2017-07-13 15:57:26 +0200 | [diff] [blame] | 445 | goto fail; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 446 | break; |
| 447 | case CRUSH_BUCKET_LIST: |
| 448 | err = crush_decode_list_bucket(p, end, |
| 449 | (struct crush_bucket_list *)b); |
| 450 | if (err < 0) |
Ilya Dryomov | c2acfd9 | 2017-07-13 15:57:26 +0200 | [diff] [blame] | 451 | goto fail; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 452 | break; |
| 453 | case CRUSH_BUCKET_TREE: |
| 454 | err = crush_decode_tree_bucket(p, end, |
| 455 | (struct crush_bucket_tree *)b); |
| 456 | if (err < 0) |
Ilya Dryomov | c2acfd9 | 2017-07-13 15:57:26 +0200 | [diff] [blame] | 457 | goto fail; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 458 | break; |
| 459 | case CRUSH_BUCKET_STRAW: |
| 460 | err = crush_decode_straw_bucket(p, end, |
| 461 | (struct crush_bucket_straw *)b); |
| 462 | if (err < 0) |
Ilya Dryomov | c2acfd9 | 2017-07-13 15:57:26 +0200 | [diff] [blame] | 463 | goto fail; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 464 | break; |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 465 | case CRUSH_BUCKET_STRAW2: |
| 466 | err = crush_decode_straw2_bucket(p, end, |
| 467 | (struct crush_bucket_straw2 *)b); |
| 468 | if (err < 0) |
Ilya Dryomov | c2acfd9 | 2017-07-13 15:57:26 +0200 | [diff] [blame] | 469 | goto fail; |
Ilya Dryomov | 958a276 | 2015-04-14 16:54:52 +0300 | [diff] [blame] | 470 | break; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
| 474 | /* rules */ |
| 475 | dout("rule vec is %p\n", c->rules); |
| 476 | for (i = 0; i < c->max_rules; i++) { |
| 477 | u32 yes; |
| 478 | struct crush_rule *r; |
| 479 | |
| 480 | ceph_decode_32_safe(p, end, yes, bad); |
| 481 | if (!yes) { |
| 482 | dout("crush_decode NO rule %d off %x %p to %p\n", |
| 483 | i, (int)(*p-start), *p, end); |
| 484 | c->rules[i] = NULL; |
| 485 | continue; |
| 486 | } |
| 487 | |
| 488 | dout("crush_decode rule %d off %x %p to %p\n", |
| 489 | i, (int)(*p-start), *p, end); |
| 490 | |
| 491 | /* len */ |
| 492 | ceph_decode_32_safe(p, end, yes, bad); |
| 493 | #if BITS_PER_LONG == 32 |
Xi Wang | 6448669 | 2012-02-16 11:55:48 -0500 | [diff] [blame] | 494 | if (yes > (ULONG_MAX - sizeof(*r)) |
| 495 | / sizeof(struct crush_rule_step)) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 496 | goto bad; |
| 497 | #endif |
Gustavo A. R. Silva | 6b41d4d | 2019-01-15 13:41:53 -0600 | [diff] [blame] | 498 | r = kmalloc(struct_size(r, steps, yes), GFP_NOFS); |
| 499 | c->rules[i] = r; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 500 | if (r == NULL) |
| 501 | goto badmem; |
| 502 | dout(" rule %d is at %p\n", i, r); |
| 503 | r->len = yes; |
| 504 | ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */ |
| 505 | ceph_decode_need(p, end, r->len*3*sizeof(u32), bad); |
| 506 | for (j = 0; j < r->len; j++) { |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 507 | r->steps[j].op = ceph_decode_32(p); |
| 508 | r->steps[j].arg1 = ceph_decode_32(p); |
| 509 | r->steps[j].arg2 = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
Ilya Dryomov | 278b1d70 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 513 | ceph_decode_skip_map(p, end, 32, string, bad); /* type_map */ |
| 514 | ceph_decode_skip_map(p, end, 32, string, bad); /* name_map */ |
| 515 | ceph_decode_skip_map(p, end, 32, string, bad); /* rule_name_map */ |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 516 | |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 517 | /* tunables */ |
| 518 | ceph_decode_need(p, end, 3*sizeof(u32), done); |
| 519 | c->choose_local_tries = ceph_decode_32(p); |
| 520 | c->choose_local_fallback_tries = ceph_decode_32(p); |
| 521 | c->choose_total_tries = ceph_decode_32(p); |
Ilya Dryomov | b9b519b | 2016-02-01 16:57:16 +0100 | [diff] [blame] | 522 | dout("crush decode tunable choose_local_tries = %d\n", |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 523 | c->choose_local_tries); |
Ilya Dryomov | b9b519b | 2016-02-01 16:57:16 +0100 | [diff] [blame] | 524 | dout("crush decode tunable choose_local_fallback_tries = %d\n", |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 525 | c->choose_local_fallback_tries); |
Ilya Dryomov | b9b519b | 2016-02-01 16:57:16 +0100 | [diff] [blame] | 526 | dout("crush decode tunable choose_total_tries = %d\n", |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 527 | c->choose_total_tries); |
| 528 | |
Jim Schutt | 1604f48 | 2012-11-30 09:15:25 -0700 | [diff] [blame] | 529 | ceph_decode_need(p, end, sizeof(u32), done); |
| 530 | c->chooseleaf_descend_once = ceph_decode_32(p); |
Ilya Dryomov | b9b519b | 2016-02-01 16:57:16 +0100 | [diff] [blame] | 531 | dout("crush decode tunable chooseleaf_descend_once = %d\n", |
Jim Schutt | 1604f48 | 2012-11-30 09:15:25 -0700 | [diff] [blame] | 532 | c->chooseleaf_descend_once); |
| 533 | |
Ilya Dryomov | f140662 | 2014-05-09 18:27:34 +0400 | [diff] [blame] | 534 | ceph_decode_need(p, end, sizeof(u8), done); |
| 535 | c->chooseleaf_vary_r = ceph_decode_8(p); |
Ilya Dryomov | b9b519b | 2016-02-01 16:57:16 +0100 | [diff] [blame] | 536 | dout("crush decode tunable chooseleaf_vary_r = %d\n", |
Ilya Dryomov | f140662 | 2014-05-09 18:27:34 +0400 | [diff] [blame] | 537 | c->chooseleaf_vary_r); |
| 538 | |
Ilya Dryomov | b9b519b | 2016-02-01 16:57:16 +0100 | [diff] [blame] | 539 | /* skip straw_calc_version, allowed_bucket_algs */ |
| 540 | ceph_decode_need(p, end, sizeof(u8) + sizeof(u32), done); |
| 541 | *p += sizeof(u8) + sizeof(u32); |
| 542 | |
| 543 | ceph_decode_need(p, end, sizeof(u8), done); |
| 544 | c->chooseleaf_stable = ceph_decode_8(p); |
| 545 | dout("crush decode tunable chooseleaf_stable = %d\n", |
| 546 | c->chooseleaf_stable); |
| 547 | |
Ilya Dryomov | 5cf9c4a | 2017-06-22 19:44:05 +0200 | [diff] [blame] | 548 | if (*p != end) { |
| 549 | /* class_map */ |
| 550 | ceph_decode_skip_map(p, end, 32, 32, bad); |
| 551 | /* class_name */ |
| 552 | ceph_decode_skip_map(p, end, 32, string, bad); |
| 553 | /* class_bucket */ |
| 554 | ceph_decode_skip_map_of_map(p, end, 32, 32, 32, bad); |
| 555 | } |
| 556 | |
| 557 | if (*p != end) { |
| 558 | err = decode_choose_args(p, end, c); |
| 559 | if (err) |
Ilya Dryomov | c2acfd9 | 2017-07-13 15:57:26 +0200 | [diff] [blame] | 560 | goto fail; |
Ilya Dryomov | 5cf9c4a | 2017-06-22 19:44:05 +0200 | [diff] [blame] | 561 | } |
| 562 | |
Sage Weil | 546f04e | 2012-07-30 18:15:23 -0700 | [diff] [blame] | 563 | done: |
Ilya Dryomov | 9afd30d | 2017-02-28 18:53:53 +0100 | [diff] [blame] | 564 | crush_finalize(c); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 565 | dout("crush_decode success\n"); |
| 566 | return c; |
| 567 | |
| 568 | badmem: |
| 569 | err = -ENOMEM; |
Ilya Dryomov | c2acfd9 | 2017-07-13 15:57:26 +0200 | [diff] [blame] | 570 | fail: |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 571 | dout("crush_decode fail %d\n", err); |
| 572 | crush_destroy(c); |
| 573 | return ERR_PTR(err); |
Ilya Dryomov | c2acfd9 | 2017-07-13 15:57:26 +0200 | [diff] [blame] | 574 | |
| 575 | bad: |
| 576 | err = -EINVAL; |
| 577 | goto fail; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Ilya Dryomov | f984cb7 | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 580 | int ceph_pg_compare(const struct ceph_pg *lhs, const struct ceph_pg *rhs) |
| 581 | { |
| 582 | if (lhs->pool < rhs->pool) |
| 583 | return -1; |
| 584 | if (lhs->pool > rhs->pool) |
| 585 | return 1; |
| 586 | if (lhs->seed < rhs->seed) |
| 587 | return -1; |
| 588 | if (lhs->seed > rhs->seed) |
| 589 | return 1; |
| 590 | |
| 591 | return 0; |
| 592 | } |
| 593 | |
Ilya Dryomov | a02a946 | 2017-06-19 12:18:05 +0200 | [diff] [blame] | 594 | int ceph_spg_compare(const struct ceph_spg *lhs, const struct ceph_spg *rhs) |
| 595 | { |
| 596 | int ret; |
| 597 | |
| 598 | ret = ceph_pg_compare(&lhs->pgid, &rhs->pgid); |
| 599 | if (ret) |
| 600 | return ret; |
| 601 | |
| 602 | if (lhs->shard < rhs->shard) |
| 603 | return -1; |
| 604 | if (lhs->shard > rhs->shard) |
| 605 | return 1; |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 610 | static struct ceph_pg_mapping *alloc_pg_mapping(size_t payload_len) |
| 611 | { |
| 612 | struct ceph_pg_mapping *pg; |
| 613 | |
| 614 | pg = kmalloc(sizeof(*pg) + payload_len, GFP_NOIO); |
| 615 | if (!pg) |
| 616 | return NULL; |
| 617 | |
| 618 | RB_CLEAR_NODE(&pg->node); |
| 619 | return pg; |
| 620 | } |
| 621 | |
| 622 | static void free_pg_mapping(struct ceph_pg_mapping *pg) |
| 623 | { |
| 624 | WARN_ON(!RB_EMPTY_NODE(&pg->node)); |
| 625 | |
| 626 | kfree(pg); |
| 627 | } |
| 628 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 629 | /* |
Sage Weil | 9794b14 | 2010-02-16 15:53:32 -0800 | [diff] [blame] | 630 | * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid |
Ilya Dryomov | 9686f94 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 631 | * to a set of osds) and primary_temp (explicit primary setting) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 632 | */ |
Ilya Dryomov | ab75144 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 633 | DEFINE_RB_FUNCS2(pg_mapping, struct ceph_pg_mapping, pgid, ceph_pg_compare, |
| 634 | RB_BYPTR, const struct ceph_pg *, node) |
Sage Weil | 8adc8b3 | 2011-09-28 10:11:04 -0700 | [diff] [blame] | 635 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 636 | /* |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 637 | * rbtree of pg pool info |
| 638 | */ |
Ilya Dryomov | 8a4b863 | 2020-05-19 16:46:47 +0200 | [diff] [blame^] | 639 | DEFINE_RB_FUNCS(pg_pool, struct ceph_pg_pool_info, id, node) |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 640 | |
Ilya Dryomov | ce7f6a2 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 641 | struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id) |
| 642 | { |
Ilya Dryomov | 8a4b863 | 2020-05-19 16:46:47 +0200 | [diff] [blame^] | 643 | return lookup_pg_pool(&map->pg_pools, id); |
Ilya Dryomov | ce7f6a2 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 644 | } |
| 645 | |
Alex Elder | 72afc71 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 646 | const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id) |
| 647 | { |
| 648 | struct ceph_pg_pool_info *pi; |
| 649 | |
| 650 | if (id == CEPH_NOPOOL) |
| 651 | return NULL; |
| 652 | |
| 653 | if (WARN_ON_ONCE(id > (u64) INT_MAX)) |
| 654 | return NULL; |
| 655 | |
Ilya Dryomov | 8a4b863 | 2020-05-19 16:46:47 +0200 | [diff] [blame^] | 656 | pi = lookup_pg_pool(&map->pg_pools, id); |
Alex Elder | 72afc71 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 657 | return pi ? pi->name : NULL; |
| 658 | } |
| 659 | EXPORT_SYMBOL(ceph_pg_pool_name_by_id); |
| 660 | |
Yehuda Sadeh | 7669a2c | 2010-05-17 12:31:35 -0700 | [diff] [blame] | 661 | int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name) |
| 662 | { |
| 663 | struct rb_node *rbp; |
| 664 | |
| 665 | for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) { |
| 666 | struct ceph_pg_pool_info *pi = |
| 667 | rb_entry(rbp, struct ceph_pg_pool_info, node); |
| 668 | if (pi->name && strcmp(pi->name, name) == 0) |
| 669 | return pi->id; |
| 670 | } |
| 671 | return -ENOENT; |
| 672 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 673 | EXPORT_SYMBOL(ceph_pg_poolid_by_name); |
Yehuda Sadeh | 7669a2c | 2010-05-17 12:31:35 -0700 | [diff] [blame] | 674 | |
Ilya Dryomov | 7614209 | 2020-03-09 12:03:14 +0100 | [diff] [blame] | 675 | u64 ceph_pg_pool_flags(struct ceph_osdmap *map, u64 id) |
| 676 | { |
| 677 | struct ceph_pg_pool_info *pi; |
| 678 | |
Ilya Dryomov | 8a4b863 | 2020-05-19 16:46:47 +0200 | [diff] [blame^] | 679 | pi = lookup_pg_pool(&map->pg_pools, id); |
Ilya Dryomov | 7614209 | 2020-03-09 12:03:14 +0100 | [diff] [blame] | 680 | return pi ? pi->flags : 0; |
| 681 | } |
| 682 | EXPORT_SYMBOL(ceph_pg_pool_flags); |
| 683 | |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 684 | static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi) |
| 685 | { |
Ilya Dryomov | 8a4b863 | 2020-05-19 16:46:47 +0200 | [diff] [blame^] | 686 | erase_pg_pool(root, pi); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 687 | kfree(pi->name); |
| 688 | kfree(pi); |
| 689 | } |
| 690 | |
Ilya Dryomov | 0f70c7e | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 691 | static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi) |
Sage Weil | efd7576 | 2010-03-17 10:05:28 -0700 | [diff] [blame] | 692 | { |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 693 | u8 ev, cv; |
| 694 | unsigned len, num; |
| 695 | void *pool_end; |
Sage Weil | 73a7e69 | 2010-08-02 11:00:55 -0700 | [diff] [blame] | 696 | |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 697 | ceph_decode_need(p, end, 2 + 4, bad); |
| 698 | ev = ceph_decode_8(p); /* encoding version */ |
| 699 | cv = ceph_decode_8(p); /* compat version */ |
| 700 | if (ev < 5) { |
Joe Perches | b9a6789 | 2014-09-09 21:17:29 -0700 | [diff] [blame] | 701 | pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv); |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 702 | return -EINVAL; |
| 703 | } |
Ilya Dryomov | 17a13e40 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 704 | if (cv > 9) { |
Joe Perches | b9a6789 | 2014-09-09 21:17:29 -0700 | [diff] [blame] | 705 | pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv); |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 706 | return -EINVAL; |
| 707 | } |
| 708 | len = ceph_decode_32(p); |
| 709 | ceph_decode_need(p, end, len, bad); |
| 710 | pool_end = *p + len; |
Sage Weil | 73a7e69 | 2010-08-02 11:00:55 -0700 | [diff] [blame] | 711 | |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 712 | pi->type = ceph_decode_8(p); |
| 713 | pi->size = ceph_decode_8(p); |
| 714 | pi->crush_ruleset = ceph_decode_8(p); |
| 715 | pi->object_hash = ceph_decode_8(p); |
| 716 | |
| 717 | pi->pg_num = ceph_decode_32(p); |
| 718 | pi->pgp_num = ceph_decode_32(p); |
| 719 | |
| 720 | *p += 4 + 4; /* skip lpg* */ |
| 721 | *p += 4; /* skip last_change */ |
| 722 | *p += 8 + 4; /* skip snap_seq, snap_epoch */ |
| 723 | |
| 724 | /* skip snaps */ |
| 725 | num = ceph_decode_32(p); |
| 726 | while (num--) { |
| 727 | *p += 8; /* snapid key */ |
| 728 | *p += 1 + 1; /* versions */ |
| 729 | len = ceph_decode_32(p); |
| 730 | *p += len; |
Sage Weil | 73a7e69 | 2010-08-02 11:00:55 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Ilya Dryomov | 17a13e40 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 733 | /* skip removed_snaps */ |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 734 | num = ceph_decode_32(p); |
| 735 | *p += num * (8 + 8); |
| 736 | |
| 737 | *p += 8; /* skip auid */ |
| 738 | pi->flags = ceph_decode_64(p); |
Ilya Dryomov | 17a13e40 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 739 | *p += 4; /* skip crash_replay_interval */ |
| 740 | |
| 741 | if (ev >= 7) |
Ilya Dryomov | 04812ac | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 742 | pi->min_size = ceph_decode_8(p); |
| 743 | else |
| 744 | pi->min_size = pi->size - pi->size / 2; |
Ilya Dryomov | 17a13e40 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 745 | |
| 746 | if (ev >= 8) |
| 747 | *p += 8 + 8; /* skip quota_max_* */ |
| 748 | |
| 749 | if (ev >= 9) { |
| 750 | /* skip tiers */ |
| 751 | num = ceph_decode_32(p); |
| 752 | *p += num * 8; |
| 753 | |
| 754 | *p += 8; /* skip tier_of */ |
| 755 | *p += 1; /* skip cache_mode */ |
| 756 | |
| 757 | pi->read_tier = ceph_decode_64(p); |
| 758 | pi->write_tier = ceph_decode_64(p); |
| 759 | } else { |
| 760 | pi->read_tier = -1; |
| 761 | pi->write_tier = -1; |
| 762 | } |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 763 | |
Ilya Dryomov | 04812ac | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 764 | if (ev >= 10) { |
| 765 | /* skip properties */ |
| 766 | num = ceph_decode_32(p); |
| 767 | while (num--) { |
| 768 | len = ceph_decode_32(p); |
| 769 | *p += len; /* key */ |
| 770 | len = ceph_decode_32(p); |
| 771 | *p += len; /* val */ |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | if (ev >= 11) { |
| 776 | /* skip hit_set_params */ |
| 777 | *p += 1 + 1; /* versions */ |
| 778 | len = ceph_decode_32(p); |
| 779 | *p += len; |
| 780 | |
| 781 | *p += 4; /* skip hit_set_period */ |
| 782 | *p += 4; /* skip hit_set_count */ |
| 783 | } |
| 784 | |
| 785 | if (ev >= 12) |
| 786 | *p += 4; /* skip stripe_width */ |
| 787 | |
| 788 | if (ev >= 13) { |
| 789 | *p += 8; /* skip target_max_bytes */ |
| 790 | *p += 8; /* skip target_max_objects */ |
| 791 | *p += 4; /* skip cache_target_dirty_ratio_micro */ |
| 792 | *p += 4; /* skip cache_target_full_ratio_micro */ |
| 793 | *p += 4; /* skip cache_min_flush_age */ |
| 794 | *p += 4; /* skip cache_min_evict_age */ |
| 795 | } |
| 796 | |
| 797 | if (ev >= 14) { |
| 798 | /* skip erasure_code_profile */ |
| 799 | len = ceph_decode_32(p); |
| 800 | *p += len; |
| 801 | } |
| 802 | |
Ilya Dryomov | 8e48cf0 | 2017-06-05 14:45:00 +0200 | [diff] [blame] | 803 | /* |
| 804 | * last_force_op_resend_preluminous, will be overridden if the |
| 805 | * map was encoded with RESEND_ON_SPLIT |
| 806 | */ |
Ilya Dryomov | 04812ac | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 807 | if (ev >= 15) |
| 808 | pi->last_force_request_resend = ceph_decode_32(p); |
| 809 | else |
| 810 | pi->last_force_request_resend = 0; |
| 811 | |
Ilya Dryomov | 8e48cf0 | 2017-06-05 14:45:00 +0200 | [diff] [blame] | 812 | if (ev >= 16) |
| 813 | *p += 4; /* skip min_read_recency_for_promote */ |
| 814 | |
| 815 | if (ev >= 17) |
| 816 | *p += 8; /* skip expected_num_objects */ |
| 817 | |
| 818 | if (ev >= 19) |
| 819 | *p += 4; /* skip cache_target_dirty_high_ratio_micro */ |
| 820 | |
| 821 | if (ev >= 20) |
| 822 | *p += 4; /* skip min_write_recency_for_promote */ |
| 823 | |
| 824 | if (ev >= 21) |
| 825 | *p += 1; /* skip use_gmt_hitset */ |
| 826 | |
| 827 | if (ev >= 22) |
| 828 | *p += 1; /* skip fast_read */ |
| 829 | |
| 830 | if (ev >= 23) { |
| 831 | *p += 4; /* skip hit_set_grade_decay_rate */ |
| 832 | *p += 4; /* skip hit_set_search_last_n */ |
| 833 | } |
| 834 | |
| 835 | if (ev >= 24) { |
| 836 | /* skip opts */ |
| 837 | *p += 1 + 1; /* versions */ |
| 838 | len = ceph_decode_32(p); |
| 839 | *p += len; |
| 840 | } |
| 841 | |
| 842 | if (ev >= 25) |
| 843 | pi->last_force_request_resend = ceph_decode_32(p); |
| 844 | |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 845 | /* ignore the rest */ |
| 846 | |
| 847 | *p = pool_end; |
| 848 | calc_pg_masks(pi); |
Sage Weil | 73a7e69 | 2010-08-02 11:00:55 -0700 | [diff] [blame] | 849 | return 0; |
| 850 | |
| 851 | bad: |
| 852 | return -EINVAL; |
Sage Weil | efd7576 | 2010-03-17 10:05:28 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Ilya Dryomov | 0f70c7e | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 855 | static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map) |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 856 | { |
| 857 | struct ceph_pg_pool_info *pi; |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 858 | u32 num, len; |
| 859 | u64 pool; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 860 | |
| 861 | ceph_decode_32_safe(p, end, num, bad); |
| 862 | dout(" %d pool names\n", num); |
| 863 | while (num--) { |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 864 | ceph_decode_64_safe(p, end, pool, bad); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 865 | ceph_decode_32_safe(p, end, len, bad); |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 866 | dout(" pool %llu len %d\n", pool, len); |
Xi Wang | ad3b904 | 2012-06-06 19:35:55 -0500 | [diff] [blame] | 867 | ceph_decode_need(p, end, len, bad); |
Ilya Dryomov | 8a4b863 | 2020-05-19 16:46:47 +0200 | [diff] [blame^] | 868 | pi = lookup_pg_pool(&map->pg_pools, pool); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 869 | if (pi) { |
Xi Wang | ad3b904 | 2012-06-06 19:35:55 -0500 | [diff] [blame] | 870 | char *name = kstrndup(*p, len, GFP_NOFS); |
| 871 | |
| 872 | if (!name) |
| 873 | return -ENOMEM; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 874 | kfree(pi->name); |
Xi Wang | ad3b904 | 2012-06-06 19:35:55 -0500 | [diff] [blame] | 875 | pi->name = name; |
| 876 | dout(" name is %s\n", pi->name); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 877 | } |
| 878 | *p += len; |
| 879 | } |
| 880 | return 0; |
| 881 | |
| 882 | bad: |
| 883 | return -EINVAL; |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * osd map |
| 888 | */ |
Ilya Dryomov | e5253a7 | 2016-04-28 16:07:25 +0200 | [diff] [blame] | 889 | struct ceph_osdmap *ceph_osdmap_alloc(void) |
| 890 | { |
| 891 | struct ceph_osdmap *map; |
| 892 | |
| 893 | map = kzalloc(sizeof(*map), GFP_NOIO); |
| 894 | if (!map) |
| 895 | return NULL; |
| 896 | |
| 897 | map->pg_pools = RB_ROOT; |
| 898 | map->pool_max = -1; |
| 899 | map->pg_temp = RB_ROOT; |
| 900 | map->primary_temp = RB_ROOT; |
Ilya Dryomov | 6f428df | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 901 | map->pg_upmap = RB_ROOT; |
| 902 | map->pg_upmap_items = RB_ROOT; |
Ilya Dryomov | 743efcf | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 903 | mutex_init(&map->crush_workspace_mutex); |
Ilya Dryomov | e5253a7 | 2016-04-28 16:07:25 +0200 | [diff] [blame] | 904 | |
| 905 | return map; |
| 906 | } |
| 907 | |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 908 | void ceph_osdmap_destroy(struct ceph_osdmap *map) |
| 909 | { |
| 910 | dout("osdmap_destroy %p\n", map); |
| 911 | if (map->crush) |
| 912 | crush_destroy(map->crush); |
| 913 | while (!RB_EMPTY_ROOT(&map->pg_temp)) { |
| 914 | struct ceph_pg_mapping *pg = |
| 915 | rb_entry(rb_first(&map->pg_temp), |
| 916 | struct ceph_pg_mapping, node); |
Ilya Dryomov | ab75144 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 917 | erase_pg_mapping(&map->pg_temp, pg); |
| 918 | free_pg_mapping(pg); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 919 | } |
Ilya Dryomov | 9686f94 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 920 | while (!RB_EMPTY_ROOT(&map->primary_temp)) { |
| 921 | struct ceph_pg_mapping *pg = |
| 922 | rb_entry(rb_first(&map->primary_temp), |
| 923 | struct ceph_pg_mapping, node); |
Ilya Dryomov | ab75144 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 924 | erase_pg_mapping(&map->primary_temp, pg); |
| 925 | free_pg_mapping(pg); |
Ilya Dryomov | 9686f94 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 926 | } |
Ilya Dryomov | 6f428df | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 927 | while (!RB_EMPTY_ROOT(&map->pg_upmap)) { |
| 928 | struct ceph_pg_mapping *pg = |
| 929 | rb_entry(rb_first(&map->pg_upmap), |
| 930 | struct ceph_pg_mapping, node); |
| 931 | rb_erase(&pg->node, &map->pg_upmap); |
| 932 | kfree(pg); |
| 933 | } |
| 934 | while (!RB_EMPTY_ROOT(&map->pg_upmap_items)) { |
| 935 | struct ceph_pg_mapping *pg = |
| 936 | rb_entry(rb_first(&map->pg_upmap_items), |
| 937 | struct ceph_pg_mapping, node); |
| 938 | rb_erase(&pg->node, &map->pg_upmap_items); |
| 939 | kfree(pg); |
| 940 | } |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 941 | while (!RB_EMPTY_ROOT(&map->pg_pools)) { |
| 942 | struct ceph_pg_pool_info *pi = |
| 943 | rb_entry(rb_first(&map->pg_pools), |
| 944 | struct ceph_pg_pool_info, node); |
| 945 | __remove_pg_pool(&map->pg_pools, pi); |
| 946 | } |
Ilya Dryomov | cf73d88 | 2019-09-04 15:40:03 +0200 | [diff] [blame] | 947 | kvfree(map->osd_state); |
| 948 | kvfree(map->osd_weight); |
| 949 | kvfree(map->osd_addr); |
| 950 | kvfree(map->osd_primary_affinity); |
| 951 | kvfree(map->crush_workspace); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 952 | kfree(map); |
| 953 | } |
| 954 | |
| 955 | /* |
Ilya Dryomov | 4d60351 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 956 | * Adjust max_osd value, (re)allocate arrays. |
| 957 | * |
| 958 | * The new elements are properly initialized. |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 959 | */ |
Ilya Dryomov | cf73d88 | 2019-09-04 15:40:03 +0200 | [diff] [blame] | 960 | static int osdmap_set_max_osd(struct ceph_osdmap *map, u32 max) |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 961 | { |
Ilya Dryomov | 0bb05da | 2017-06-22 19:44:06 +0200 | [diff] [blame] | 962 | u32 *state; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 963 | u32 *weight; |
Ilya Dryomov | 4d60351 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 964 | struct ceph_entity_addr *addr; |
Ilya Dryomov | cf73d88 | 2019-09-04 15:40:03 +0200 | [diff] [blame] | 965 | u32 to_copy; |
Ilya Dryomov | 4d60351 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 966 | int i; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 967 | |
Ilya Dryomov | cf73d88 | 2019-09-04 15:40:03 +0200 | [diff] [blame] | 968 | dout("%s old %u new %u\n", __func__, map->max_osd, max); |
| 969 | if (max == map->max_osd) |
| 970 | return 0; |
| 971 | |
| 972 | state = ceph_kvmalloc(array_size(max, sizeof(*state)), GFP_NOFS); |
| 973 | weight = ceph_kvmalloc(array_size(max, sizeof(*weight)), GFP_NOFS); |
| 974 | addr = ceph_kvmalloc(array_size(max, sizeof(*addr)), GFP_NOFS); |
| 975 | if (!state || !weight || !addr) { |
| 976 | kvfree(state); |
| 977 | kvfree(weight); |
| 978 | kvfree(addr); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 979 | return -ENOMEM; |
Ilya Dryomov | cf73d88 | 2019-09-04 15:40:03 +0200 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | to_copy = min(map->max_osd, max); |
| 983 | if (map->osd_state) { |
| 984 | memcpy(state, map->osd_state, to_copy * sizeof(*state)); |
| 985 | memcpy(weight, map->osd_weight, to_copy * sizeof(*weight)); |
| 986 | memcpy(addr, map->osd_addr, to_copy * sizeof(*addr)); |
| 987 | kvfree(map->osd_state); |
| 988 | kvfree(map->osd_weight); |
| 989 | kvfree(map->osd_addr); |
| 990 | } |
| 991 | |
Li RongQing | 589506f | 2014-09-07 18:10:51 +0800 | [diff] [blame] | 992 | map->osd_state = state; |
Li RongQing | 589506f | 2014-09-07 18:10:51 +0800 | [diff] [blame] | 993 | map->osd_weight = weight; |
Li RongQing | 589506f | 2014-09-07 18:10:51 +0800 | [diff] [blame] | 994 | map->osd_addr = addr; |
Ilya Dryomov | 4d60351 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 995 | for (i = map->max_osd; i < max; i++) { |
Li RongQing | 589506f | 2014-09-07 18:10:51 +0800 | [diff] [blame] | 996 | map->osd_state[i] = 0; |
| 997 | map->osd_weight[i] = CEPH_OSD_OUT; |
| 998 | memset(map->osd_addr + i, 0, sizeof(*map->osd_addr)); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Ilya Dryomov | 2cfa34f | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1001 | if (map->osd_primary_affinity) { |
| 1002 | u32 *affinity; |
| 1003 | |
Ilya Dryomov | cf73d88 | 2019-09-04 15:40:03 +0200 | [diff] [blame] | 1004 | affinity = ceph_kvmalloc(array_size(max, sizeof(*affinity)), |
| 1005 | GFP_NOFS); |
Ilya Dryomov | 2cfa34f | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1006 | if (!affinity) |
| 1007 | return -ENOMEM; |
| 1008 | |
Ilya Dryomov | cf73d88 | 2019-09-04 15:40:03 +0200 | [diff] [blame] | 1009 | memcpy(affinity, map->osd_primary_affinity, |
| 1010 | to_copy * sizeof(*affinity)); |
| 1011 | kvfree(map->osd_primary_affinity); |
| 1012 | |
| 1013 | map->osd_primary_affinity = affinity; |
Ilya Dryomov | 2cfa34f | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1014 | for (i = map->max_osd; i < max; i++) |
Li RongQing | 589506f | 2014-09-07 18:10:51 +0800 | [diff] [blame] | 1015 | map->osd_primary_affinity[i] = |
| 1016 | CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; |
Ilya Dryomov | 2cfa34f | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1017 | } |
| 1018 | |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 1019 | map->max_osd = max; |
Ilya Dryomov | 4d60351 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1020 | |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 1021 | return 0; |
| 1022 | } |
| 1023 | |
Ilya Dryomov | 1b6a78b | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 1024 | static int osdmap_set_crush(struct ceph_osdmap *map, struct crush_map *crush) |
| 1025 | { |
Ilya Dryomov | 66a0e2d | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 1026 | void *workspace; |
Ilya Dryomov | 743efcf | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 1027 | size_t work_size; |
Ilya Dryomov | 66a0e2d | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 1028 | |
Ilya Dryomov | 1b6a78b | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 1029 | if (IS_ERR(crush)) |
| 1030 | return PTR_ERR(crush); |
| 1031 | |
Ilya Dryomov | 743efcf | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 1032 | work_size = crush_work_size(crush, CEPH_PG_MAX_SIZE); |
| 1033 | dout("%s work_size %zu bytes\n", __func__, work_size); |
Ilya Dryomov | cf73d88 | 2019-09-04 15:40:03 +0200 | [diff] [blame] | 1034 | workspace = ceph_kvmalloc(work_size, GFP_NOIO); |
Ilya Dryomov | 66a0e2d | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 1035 | if (!workspace) { |
| 1036 | crush_destroy(crush); |
| 1037 | return -ENOMEM; |
| 1038 | } |
| 1039 | crush_init_workspace(crush, workspace); |
| 1040 | |
Ilya Dryomov | 1b6a78b | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 1041 | if (map->crush) |
| 1042 | crush_destroy(map->crush); |
Ilya Dryomov | cf73d88 | 2019-09-04 15:40:03 +0200 | [diff] [blame] | 1043 | kvfree(map->crush_workspace); |
Ilya Dryomov | 1b6a78b | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 1044 | map->crush = crush; |
Ilya Dryomov | 66a0e2d | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 1045 | map->crush_workspace = workspace; |
Ilya Dryomov | 1b6a78b | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 1046 | return 0; |
| 1047 | } |
| 1048 | |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1049 | #define OSDMAP_WRAPPER_COMPAT_VER 7 |
| 1050 | #define OSDMAP_CLIENT_DATA_COMPAT_VER 1 |
| 1051 | |
| 1052 | /* |
| 1053 | * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps, |
| 1054 | * to struct_v of the client_data section for new (v7 and above) |
| 1055 | * osdmaps. |
| 1056 | */ |
| 1057 | static int get_osdmap_client_data_v(void **p, void *end, |
| 1058 | const char *prefix, u8 *v) |
| 1059 | { |
| 1060 | u8 struct_v; |
| 1061 | |
| 1062 | ceph_decode_8_safe(p, end, struct_v, e_inval); |
| 1063 | if (struct_v >= 7) { |
| 1064 | u8 struct_compat; |
| 1065 | |
| 1066 | ceph_decode_8_safe(p, end, struct_compat, e_inval); |
| 1067 | if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) { |
Joe Perches | b9a6789 | 2014-09-09 21:17:29 -0700 | [diff] [blame] | 1068 | pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n", |
| 1069 | struct_v, struct_compat, |
| 1070 | OSDMAP_WRAPPER_COMPAT_VER, prefix); |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1071 | return -EINVAL; |
| 1072 | } |
| 1073 | *p += 4; /* ignore wrapper struct_len */ |
| 1074 | |
| 1075 | ceph_decode_8_safe(p, end, struct_v, e_inval); |
| 1076 | ceph_decode_8_safe(p, end, struct_compat, e_inval); |
| 1077 | if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) { |
Joe Perches | b9a6789 | 2014-09-09 21:17:29 -0700 | [diff] [blame] | 1078 | pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n", |
| 1079 | struct_v, struct_compat, |
| 1080 | OSDMAP_CLIENT_DATA_COMPAT_VER, prefix); |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1081 | return -EINVAL; |
| 1082 | } |
| 1083 | *p += 4; /* ignore client data struct_len */ |
| 1084 | } else { |
| 1085 | u16 version; |
| 1086 | |
| 1087 | *p -= 1; |
| 1088 | ceph_decode_16_safe(p, end, version, e_inval); |
| 1089 | if (version < 6) { |
Joe Perches | b9a6789 | 2014-09-09 21:17:29 -0700 | [diff] [blame] | 1090 | pr_warn("got v %d < 6 of %s ceph_osdmap\n", |
| 1091 | version, prefix); |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1092 | return -EINVAL; |
| 1093 | } |
| 1094 | |
| 1095 | /* old osdmap enconding */ |
| 1096 | struct_v = 0; |
| 1097 | } |
| 1098 | |
| 1099 | *v = struct_v; |
| 1100 | return 0; |
| 1101 | |
| 1102 | e_inval: |
| 1103 | return -EINVAL; |
| 1104 | } |
| 1105 | |
Ilya Dryomov | 433fbdd | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1106 | static int __decode_pools(void **p, void *end, struct ceph_osdmap *map, |
| 1107 | bool incremental) |
| 1108 | { |
| 1109 | u32 n; |
| 1110 | |
| 1111 | ceph_decode_32_safe(p, end, n, e_inval); |
| 1112 | while (n--) { |
| 1113 | struct ceph_pg_pool_info *pi; |
| 1114 | u64 pool; |
| 1115 | int ret; |
| 1116 | |
| 1117 | ceph_decode_64_safe(p, end, pool, e_inval); |
| 1118 | |
Ilya Dryomov | 8a4b863 | 2020-05-19 16:46:47 +0200 | [diff] [blame^] | 1119 | pi = lookup_pg_pool(&map->pg_pools, pool); |
Ilya Dryomov | 433fbdd | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1120 | if (!incremental || !pi) { |
| 1121 | pi = kzalloc(sizeof(*pi), GFP_NOFS); |
| 1122 | if (!pi) |
| 1123 | return -ENOMEM; |
| 1124 | |
Ilya Dryomov | 8a4b863 | 2020-05-19 16:46:47 +0200 | [diff] [blame^] | 1125 | RB_CLEAR_NODE(&pi->node); |
Ilya Dryomov | 433fbdd | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1126 | pi->id = pool; |
| 1127 | |
Ilya Dryomov | 8a4b863 | 2020-05-19 16:46:47 +0200 | [diff] [blame^] | 1128 | if (!__insert_pg_pool(&map->pg_pools, pi)) { |
Ilya Dryomov | 433fbdd | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1129 | kfree(pi); |
Ilya Dryomov | 8a4b863 | 2020-05-19 16:46:47 +0200 | [diff] [blame^] | 1130 | return -EEXIST; |
Ilya Dryomov | 433fbdd | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | ret = decode_pool(p, end, pi); |
| 1135 | if (ret) |
| 1136 | return ret; |
| 1137 | } |
| 1138 | |
| 1139 | return 0; |
| 1140 | |
| 1141 | e_inval: |
| 1142 | return -EINVAL; |
| 1143 | } |
| 1144 | |
| 1145 | static int decode_pools(void **p, void *end, struct ceph_osdmap *map) |
| 1146 | { |
| 1147 | return __decode_pools(p, end, map, false); |
| 1148 | } |
| 1149 | |
| 1150 | static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map) |
| 1151 | { |
| 1152 | return __decode_pools(p, end, map, true); |
| 1153 | } |
| 1154 | |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1155 | typedef struct ceph_pg_mapping *(*decode_mapping_fn_t)(void **, void *, bool); |
| 1156 | |
| 1157 | static int decode_pg_mapping(void **p, void *end, struct rb_root *mapping_root, |
| 1158 | decode_mapping_fn_t fn, bool incremental) |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1159 | { |
| 1160 | u32 n; |
| 1161 | |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1162 | WARN_ON(!incremental && !fn); |
| 1163 | |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1164 | ceph_decode_32_safe(p, end, n, e_inval); |
| 1165 | while (n--) { |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1166 | struct ceph_pg_mapping *pg; |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1167 | struct ceph_pg pgid; |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1168 | int ret; |
| 1169 | |
| 1170 | ret = ceph_decode_pgid(p, end, &pgid); |
| 1171 | if (ret) |
| 1172 | return ret; |
| 1173 | |
Ilya Dryomov | ab75144 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1174 | pg = lookup_pg_mapping(mapping_root, &pgid); |
| 1175 | if (pg) { |
| 1176 | WARN_ON(!incremental); |
| 1177 | erase_pg_mapping(mapping_root, pg); |
| 1178 | free_pg_mapping(pg); |
| 1179 | } |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1180 | |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1181 | if (fn) { |
| 1182 | pg = fn(p, end, incremental); |
| 1183 | if (IS_ERR(pg)) |
| 1184 | return PTR_ERR(pg); |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1185 | |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1186 | if (pg) { |
| 1187 | pg->pgid = pgid; /* struct */ |
Ilya Dryomov | ab75144 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1188 | insert_pg_mapping(mapping_root, pg); |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | return 0; |
| 1194 | |
| 1195 | e_inval: |
| 1196 | return -EINVAL; |
| 1197 | } |
| 1198 | |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1199 | static struct ceph_pg_mapping *__decode_pg_temp(void **p, void *end, |
| 1200 | bool incremental) |
| 1201 | { |
| 1202 | struct ceph_pg_mapping *pg; |
| 1203 | u32 len, i; |
| 1204 | |
| 1205 | ceph_decode_32_safe(p, end, len, e_inval); |
| 1206 | if (len == 0 && incremental) |
| 1207 | return NULL; /* new_pg_temp: [] to remove */ |
| 1208 | if (len > (SIZE_MAX - sizeof(*pg)) / sizeof(u32)) |
| 1209 | return ERR_PTR(-EINVAL); |
| 1210 | |
| 1211 | ceph_decode_need(p, end, len * sizeof(u32), e_inval); |
| 1212 | pg = alloc_pg_mapping(len * sizeof(u32)); |
| 1213 | if (!pg) |
| 1214 | return ERR_PTR(-ENOMEM); |
| 1215 | |
| 1216 | pg->pg_temp.len = len; |
| 1217 | for (i = 0; i < len; i++) |
| 1218 | pg->pg_temp.osds[i] = ceph_decode_32(p); |
| 1219 | |
| 1220 | return pg; |
| 1221 | |
| 1222 | e_inval: |
| 1223 | return ERR_PTR(-EINVAL); |
| 1224 | } |
| 1225 | |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1226 | static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map) |
| 1227 | { |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1228 | return decode_pg_mapping(p, end, &map->pg_temp, __decode_pg_temp, |
| 1229 | false); |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map) |
| 1233 | { |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1234 | return decode_pg_mapping(p, end, &map->pg_temp, __decode_pg_temp, |
| 1235 | true); |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1236 | } |
| 1237 | |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1238 | static struct ceph_pg_mapping *__decode_primary_temp(void **p, void *end, |
| 1239 | bool incremental) |
Ilya Dryomov | d286de7 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1240 | { |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1241 | struct ceph_pg_mapping *pg; |
| 1242 | u32 osd; |
Ilya Dryomov | d286de7 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1243 | |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1244 | ceph_decode_32_safe(p, end, osd, e_inval); |
| 1245 | if (osd == (u32)-1 && incremental) |
| 1246 | return NULL; /* new_primary_temp: -1 to remove */ |
Ilya Dryomov | d286de7 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1247 | |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1248 | pg = alloc_pg_mapping(0); |
| 1249 | if (!pg) |
| 1250 | return ERR_PTR(-ENOMEM); |
Ilya Dryomov | d286de7 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1251 | |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1252 | pg->primary_temp.osd = osd; |
| 1253 | return pg; |
Ilya Dryomov | d286de7 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1254 | |
| 1255 | e_inval: |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1256 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | d286de7 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map) |
| 1260 | { |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1261 | return decode_pg_mapping(p, end, &map->primary_temp, |
| 1262 | __decode_primary_temp, false); |
Ilya Dryomov | d286de7 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | static int decode_new_primary_temp(void **p, void *end, |
| 1266 | struct ceph_osdmap *map) |
| 1267 | { |
Ilya Dryomov | a303bb0 | 2017-06-21 17:27:17 +0200 | [diff] [blame] | 1268 | return decode_pg_mapping(p, end, &map->primary_temp, |
| 1269 | __decode_primary_temp, true); |
Ilya Dryomov | d286de7 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1270 | } |
| 1271 | |
Ilya Dryomov | 2cfa34f | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1272 | u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd) |
| 1273 | { |
| 1274 | BUG_ON(osd >= map->max_osd); |
| 1275 | |
| 1276 | if (!map->osd_primary_affinity) |
| 1277 | return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; |
| 1278 | |
| 1279 | return map->osd_primary_affinity[osd]; |
| 1280 | } |
| 1281 | |
| 1282 | static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff) |
| 1283 | { |
| 1284 | BUG_ON(osd >= map->max_osd); |
| 1285 | |
| 1286 | if (!map->osd_primary_affinity) { |
| 1287 | int i; |
| 1288 | |
Ilya Dryomov | cf73d88 | 2019-09-04 15:40:03 +0200 | [diff] [blame] | 1289 | map->osd_primary_affinity = ceph_kvmalloc( |
| 1290 | array_size(map->max_osd, sizeof(*map->osd_primary_affinity)), |
| 1291 | GFP_NOFS); |
Ilya Dryomov | 2cfa34f | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1292 | if (!map->osd_primary_affinity) |
| 1293 | return -ENOMEM; |
| 1294 | |
| 1295 | for (i = 0; i < map->max_osd; i++) |
| 1296 | map->osd_primary_affinity[i] = |
| 1297 | CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; |
| 1298 | } |
| 1299 | |
| 1300 | map->osd_primary_affinity[osd] = aff; |
| 1301 | |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
Ilya Dryomov | 63a6993 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1305 | static int decode_primary_affinity(void **p, void *end, |
| 1306 | struct ceph_osdmap *map) |
| 1307 | { |
| 1308 | u32 len, i; |
| 1309 | |
| 1310 | ceph_decode_32_safe(p, end, len, e_inval); |
| 1311 | if (len == 0) { |
Ilya Dryomov | cf73d88 | 2019-09-04 15:40:03 +0200 | [diff] [blame] | 1312 | kvfree(map->osd_primary_affinity); |
Ilya Dryomov | 63a6993 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1313 | map->osd_primary_affinity = NULL; |
| 1314 | return 0; |
| 1315 | } |
| 1316 | if (len != map->max_osd) |
| 1317 | goto e_inval; |
| 1318 | |
| 1319 | ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval); |
| 1320 | |
| 1321 | for (i = 0; i < map->max_osd; i++) { |
| 1322 | int ret; |
| 1323 | |
| 1324 | ret = set_primary_affinity(map, i, ceph_decode_32(p)); |
| 1325 | if (ret) |
| 1326 | return ret; |
| 1327 | } |
| 1328 | |
| 1329 | return 0; |
| 1330 | |
| 1331 | e_inval: |
| 1332 | return -EINVAL; |
| 1333 | } |
| 1334 | |
| 1335 | static int decode_new_primary_affinity(void **p, void *end, |
| 1336 | struct ceph_osdmap *map) |
| 1337 | { |
| 1338 | u32 n; |
| 1339 | |
| 1340 | ceph_decode_32_safe(p, end, n, e_inval); |
| 1341 | while (n--) { |
| 1342 | u32 osd, aff; |
| 1343 | int ret; |
| 1344 | |
| 1345 | ceph_decode_32_safe(p, end, osd, e_inval); |
| 1346 | ceph_decode_32_safe(p, end, aff, e_inval); |
| 1347 | |
| 1348 | ret = set_primary_affinity(map, osd, aff); |
| 1349 | if (ret) |
| 1350 | return ret; |
Ilya Dryomov | f31da0f | 2014-04-02 20:34:04 +0400 | [diff] [blame] | 1351 | |
| 1352 | pr_info("osd%d primary-affinity 0x%x\n", osd, aff); |
Ilya Dryomov | 63a6993 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | return 0; |
| 1356 | |
| 1357 | e_inval: |
| 1358 | return -EINVAL; |
| 1359 | } |
| 1360 | |
Ilya Dryomov | 6f428df | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 1361 | static struct ceph_pg_mapping *__decode_pg_upmap(void **p, void *end, |
| 1362 | bool __unused) |
| 1363 | { |
| 1364 | return __decode_pg_temp(p, end, false); |
| 1365 | } |
| 1366 | |
| 1367 | static int decode_pg_upmap(void **p, void *end, struct ceph_osdmap *map) |
| 1368 | { |
| 1369 | return decode_pg_mapping(p, end, &map->pg_upmap, __decode_pg_upmap, |
| 1370 | false); |
| 1371 | } |
| 1372 | |
| 1373 | static int decode_new_pg_upmap(void **p, void *end, struct ceph_osdmap *map) |
| 1374 | { |
| 1375 | return decode_pg_mapping(p, end, &map->pg_upmap, __decode_pg_upmap, |
| 1376 | true); |
| 1377 | } |
| 1378 | |
| 1379 | static int decode_old_pg_upmap(void **p, void *end, struct ceph_osdmap *map) |
| 1380 | { |
| 1381 | return decode_pg_mapping(p, end, &map->pg_upmap, NULL, true); |
| 1382 | } |
| 1383 | |
| 1384 | static struct ceph_pg_mapping *__decode_pg_upmap_items(void **p, void *end, |
| 1385 | bool __unused) |
| 1386 | { |
| 1387 | struct ceph_pg_mapping *pg; |
| 1388 | u32 len, i; |
| 1389 | |
| 1390 | ceph_decode_32_safe(p, end, len, e_inval); |
| 1391 | if (len > (SIZE_MAX - sizeof(*pg)) / (2 * sizeof(u32))) |
| 1392 | return ERR_PTR(-EINVAL); |
| 1393 | |
| 1394 | ceph_decode_need(p, end, 2 * len * sizeof(u32), e_inval); |
Ilya Dryomov | f5cc689 | 2017-07-07 16:14:45 +0200 | [diff] [blame] | 1395 | pg = alloc_pg_mapping(2 * len * sizeof(u32)); |
Ilya Dryomov | 6f428df | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 1396 | if (!pg) |
| 1397 | return ERR_PTR(-ENOMEM); |
| 1398 | |
| 1399 | pg->pg_upmap_items.len = len; |
| 1400 | for (i = 0; i < len; i++) { |
| 1401 | pg->pg_upmap_items.from_to[i][0] = ceph_decode_32(p); |
| 1402 | pg->pg_upmap_items.from_to[i][1] = ceph_decode_32(p); |
| 1403 | } |
| 1404 | |
| 1405 | return pg; |
| 1406 | |
| 1407 | e_inval: |
| 1408 | return ERR_PTR(-EINVAL); |
| 1409 | } |
| 1410 | |
| 1411 | static int decode_pg_upmap_items(void **p, void *end, struct ceph_osdmap *map) |
| 1412 | { |
| 1413 | return decode_pg_mapping(p, end, &map->pg_upmap_items, |
| 1414 | __decode_pg_upmap_items, false); |
| 1415 | } |
| 1416 | |
| 1417 | static int decode_new_pg_upmap_items(void **p, void *end, |
| 1418 | struct ceph_osdmap *map) |
| 1419 | { |
| 1420 | return decode_pg_mapping(p, end, &map->pg_upmap_items, |
| 1421 | __decode_pg_upmap_items, true); |
| 1422 | } |
| 1423 | |
| 1424 | static int decode_old_pg_upmap_items(void **p, void *end, |
| 1425 | struct ceph_osdmap *map) |
| 1426 | { |
| 1427 | return decode_pg_mapping(p, end, &map->pg_upmap_items, NULL, true); |
| 1428 | } |
| 1429 | |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1430 | /* |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1431 | * decode a full map. |
| 1432 | */ |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1433 | static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1434 | { |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1435 | u8 struct_v; |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1436 | u32 epoch = 0; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1437 | void *start = *p; |
Ilya Dryomov | 3977058 | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1438 | u32 max; |
| 1439 | u32 len, i; |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1440 | int err; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1441 | |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1442 | dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p)); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1443 | |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1444 | err = get_osdmap_client_data_v(p, end, "full", &struct_v); |
| 1445 | if (err) |
| 1446 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1447 | |
Ilya Dryomov | 53bbaba | 2014-03-13 16:36:16 +0200 | [diff] [blame] | 1448 | /* fsid, epoch, created, modified */ |
| 1449 | ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) + |
| 1450 | sizeof(map->created) + sizeof(map->modified), e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1451 | ceph_decode_copy(p, &map->fsid, sizeof(map->fsid)); |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1452 | epoch = map->epoch = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1453 | ceph_decode_copy(p, &map->created, sizeof(map->created)); |
| 1454 | ceph_decode_copy(p, &map->modified, sizeof(map->modified)); |
| 1455 | |
Ilya Dryomov | 433fbdd | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1456 | /* pools */ |
| 1457 | err = decode_pools(p, end, map); |
| 1458 | if (err) |
| 1459 | goto bad; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 1460 | |
Ilya Dryomov | 0f70c7e | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1461 | /* pool_name */ |
| 1462 | err = decode_pool_names(p, end, map); |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1463 | if (err) |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 1464 | goto bad; |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 1465 | |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1466 | ceph_decode_32_safe(p, end, map->pool_max, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1467 | |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1468 | ceph_decode_32_safe(p, end, map->flags, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1469 | |
Ilya Dryomov | 3977058 | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1470 | /* max_osd */ |
| 1471 | ceph_decode_32_safe(p, end, max, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1472 | |
| 1473 | /* (re)alloc osd arrays */ |
| 1474 | err = osdmap_set_max_osd(map, max); |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1475 | if (err) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1476 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1477 | |
Ilya Dryomov | 2d88b2e | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1478 | /* osd_state, osd_weight, osd_addrs->client_addr */ |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1479 | ceph_decode_need(p, end, 3*sizeof(u32) + |
Jeff Layton | dcbc919 | 2019-06-03 15:08:13 -0400 | [diff] [blame] | 1480 | map->max_osd*(struct_v >= 5 ? sizeof(u32) : |
| 1481 | sizeof(u8)) + |
| 1482 | sizeof(*map->osd_weight), e_inval); |
Ilya Dryomov | 2d88b2e | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1483 | if (ceph_decode_32(p) != map->max_osd) |
| 1484 | goto e_inval; |
| 1485 | |
Ilya Dryomov | 0bb05da | 2017-06-22 19:44:06 +0200 | [diff] [blame] | 1486 | if (struct_v >= 5) { |
| 1487 | for (i = 0; i < map->max_osd; i++) |
| 1488 | map->osd_state[i] = ceph_decode_32(p); |
| 1489 | } else { |
| 1490 | for (i = 0; i < map->max_osd; i++) |
| 1491 | map->osd_state[i] = ceph_decode_8(p); |
| 1492 | } |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1493 | |
Ilya Dryomov | 2d88b2e | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1494 | if (ceph_decode_32(p) != map->max_osd) |
| 1495 | goto e_inval; |
| 1496 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1497 | for (i = 0; i < map->max_osd; i++) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 1498 | map->osd_weight[i] = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1499 | |
Ilya Dryomov | 2d88b2e | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1500 | if (ceph_decode_32(p) != map->max_osd) |
| 1501 | goto e_inval; |
| 1502 | |
Jeff Layton | dcbc919 | 2019-06-03 15:08:13 -0400 | [diff] [blame] | 1503 | for (i = 0; i < map->max_osd; i++) { |
| 1504 | err = ceph_decode_entity_addr(p, end, &map->osd_addr[i]); |
| 1505 | if (err) |
| 1506 | goto bad; |
| 1507 | } |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1508 | |
| 1509 | /* pg_temp */ |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1510 | err = decode_pg_temp(p, end, map); |
| 1511 | if (err) |
| 1512 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1513 | |
Ilya Dryomov | d286de7 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1514 | /* primary_temp */ |
| 1515 | if (struct_v >= 1) { |
| 1516 | err = decode_primary_temp(p, end, map); |
| 1517 | if (err) |
| 1518 | goto bad; |
| 1519 | } |
| 1520 | |
Ilya Dryomov | 63a6993 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1521 | /* primary_affinity */ |
| 1522 | if (struct_v >= 2) { |
| 1523 | err = decode_primary_affinity(p, end, map); |
| 1524 | if (err) |
| 1525 | goto bad; |
| 1526 | } else { |
Ilya Dryomov | 6f428df | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 1527 | WARN_ON(map->osd_primary_affinity); |
Ilya Dryomov | 63a6993 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1528 | } |
| 1529 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1530 | /* crush */ |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1531 | ceph_decode_32_safe(p, end, len, e_inval); |
Ilya Dryomov | 1b6a78b | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 1532 | err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end))); |
| 1533 | if (err) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1534 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1535 | |
Ilya Dryomov | 6f428df | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 1536 | *p += len; |
| 1537 | if (struct_v >= 3) { |
| 1538 | /* erasure_code_profiles */ |
| 1539 | ceph_decode_skip_map_of_map(p, end, string, string, string, |
Dan Carpenter | 00c8ebb3 | 2017-07-13 10:45:17 +0300 | [diff] [blame] | 1540 | e_inval); |
Ilya Dryomov | 6f428df | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | if (struct_v >= 4) { |
| 1544 | err = decode_pg_upmap(p, end, map); |
| 1545 | if (err) |
| 1546 | goto bad; |
| 1547 | |
| 1548 | err = decode_pg_upmap_items(p, end, map); |
| 1549 | if (err) |
| 1550 | goto bad; |
| 1551 | } else { |
| 1552 | WARN_ON(!RB_EMPTY_ROOT(&map->pg_upmap)); |
| 1553 | WARN_ON(!RB_EMPTY_ROOT(&map->pg_upmap_items)); |
| 1554 | } |
| 1555 | |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1556 | /* ignore the rest */ |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1557 | *p = end; |
| 1558 | |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1559 | dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd); |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1560 | return 0; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1561 | |
Ilya Dryomov | 597b52f | 2014-03-13 16:36:14 +0200 | [diff] [blame] | 1562 | e_inval: |
| 1563 | err = -EINVAL; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1564 | bad: |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1565 | pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n", |
| 1566 | err, epoch, (int)(*p - start), *p, start, end); |
| 1567 | print_hex_dump(KERN_DEBUG, "osdmap: ", |
| 1568 | DUMP_PREFIX_OFFSET, 16, 1, |
| 1569 | start, end - start, true); |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1570 | return err; |
| 1571 | } |
| 1572 | |
| 1573 | /* |
| 1574 | * Allocate and decode a full map. |
| 1575 | */ |
| 1576 | struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end) |
| 1577 | { |
| 1578 | struct ceph_osdmap *map; |
| 1579 | int ret; |
| 1580 | |
Ilya Dryomov | e5253a7 | 2016-04-28 16:07:25 +0200 | [diff] [blame] | 1581 | map = ceph_osdmap_alloc(); |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1582 | if (!map) |
| 1583 | return ERR_PTR(-ENOMEM); |
| 1584 | |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1585 | ret = osdmap_decode(p, end, map); |
| 1586 | if (ret) { |
| 1587 | ceph_osdmap_destroy(map); |
| 1588 | return ERR_PTR(ret); |
| 1589 | } |
| 1590 | |
| 1591 | return map; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | /* |
Ilya Dryomov | 930c532 | 2016-07-19 03:50:28 +0200 | [diff] [blame] | 1595 | * Encoding order is (new_up_client, new_state, new_weight). Need to |
| 1596 | * apply in the (new_weight, new_state, new_up_client) order, because |
| 1597 | * an incremental map may look like e.g. |
| 1598 | * |
| 1599 | * new_up_client: { osd=6, addr=... } # set osd_state and addr |
| 1600 | * new_state: { osd=6, xorstate=EXISTS } # clear osd_state |
| 1601 | */ |
Ilya Dryomov | 0bb05da | 2017-06-22 19:44:06 +0200 | [diff] [blame] | 1602 | static int decode_new_up_state_weight(void **p, void *end, u8 struct_v, |
Ilya Dryomov | 930c532 | 2016-07-19 03:50:28 +0200 | [diff] [blame] | 1603 | struct ceph_osdmap *map) |
| 1604 | { |
| 1605 | void *new_up_client; |
| 1606 | void *new_state; |
| 1607 | void *new_weight_end; |
| 1608 | u32 len; |
Jeff Layton | 8cb5f2b | 2019-06-04 15:10:44 -0400 | [diff] [blame] | 1609 | int i; |
Ilya Dryomov | 930c532 | 2016-07-19 03:50:28 +0200 | [diff] [blame] | 1610 | |
| 1611 | new_up_client = *p; |
| 1612 | ceph_decode_32_safe(p, end, len, e_inval); |
Jeff Layton | 8cb5f2b | 2019-06-04 15:10:44 -0400 | [diff] [blame] | 1613 | for (i = 0; i < len; ++i) { |
| 1614 | struct ceph_entity_addr addr; |
| 1615 | |
| 1616 | ceph_decode_skip_32(p, end, e_inval); |
| 1617 | if (ceph_decode_entity_addr(p, end, &addr)) |
| 1618 | goto e_inval; |
| 1619 | } |
Ilya Dryomov | 930c532 | 2016-07-19 03:50:28 +0200 | [diff] [blame] | 1620 | |
| 1621 | new_state = *p; |
| 1622 | ceph_decode_32_safe(p, end, len, e_inval); |
Ilya Dryomov | 0bb05da | 2017-06-22 19:44:06 +0200 | [diff] [blame] | 1623 | len *= sizeof(u32) + (struct_v >= 5 ? sizeof(u32) : sizeof(u8)); |
Ilya Dryomov | 930c532 | 2016-07-19 03:50:28 +0200 | [diff] [blame] | 1624 | ceph_decode_need(p, end, len, e_inval); |
| 1625 | *p += len; |
| 1626 | |
| 1627 | /* new_weight */ |
| 1628 | ceph_decode_32_safe(p, end, len, e_inval); |
| 1629 | while (len--) { |
| 1630 | s32 osd; |
| 1631 | u32 w; |
| 1632 | |
| 1633 | ceph_decode_need(p, end, 2*sizeof(u32), e_inval); |
| 1634 | osd = ceph_decode_32(p); |
| 1635 | w = ceph_decode_32(p); |
| 1636 | BUG_ON(osd >= map->max_osd); |
| 1637 | pr_info("osd%d weight 0x%x %s\n", osd, w, |
| 1638 | w == CEPH_OSD_IN ? "(in)" : |
| 1639 | (w == CEPH_OSD_OUT ? "(out)" : "")); |
| 1640 | map->osd_weight[osd] = w; |
| 1641 | |
| 1642 | /* |
| 1643 | * If we are marking in, set the EXISTS, and clear the |
| 1644 | * AUTOOUT and NEW bits. |
| 1645 | */ |
| 1646 | if (w) { |
| 1647 | map->osd_state[osd] |= CEPH_OSD_EXISTS; |
| 1648 | map->osd_state[osd] &= ~(CEPH_OSD_AUTOOUT | |
| 1649 | CEPH_OSD_NEW); |
| 1650 | } |
| 1651 | } |
| 1652 | new_weight_end = *p; |
| 1653 | |
| 1654 | /* new_state (up/down) */ |
| 1655 | *p = new_state; |
| 1656 | len = ceph_decode_32(p); |
| 1657 | while (len--) { |
| 1658 | s32 osd; |
Ilya Dryomov | 0bb05da | 2017-06-22 19:44:06 +0200 | [diff] [blame] | 1659 | u32 xorstate; |
Ilya Dryomov | 930c532 | 2016-07-19 03:50:28 +0200 | [diff] [blame] | 1660 | int ret; |
| 1661 | |
| 1662 | osd = ceph_decode_32(p); |
Ilya Dryomov | 0bb05da | 2017-06-22 19:44:06 +0200 | [diff] [blame] | 1663 | if (struct_v >= 5) |
| 1664 | xorstate = ceph_decode_32(p); |
| 1665 | else |
| 1666 | xorstate = ceph_decode_8(p); |
Ilya Dryomov | 930c532 | 2016-07-19 03:50:28 +0200 | [diff] [blame] | 1667 | if (xorstate == 0) |
| 1668 | xorstate = CEPH_OSD_UP; |
| 1669 | BUG_ON(osd >= map->max_osd); |
| 1670 | if ((map->osd_state[osd] & CEPH_OSD_UP) && |
| 1671 | (xorstate & CEPH_OSD_UP)) |
| 1672 | pr_info("osd%d down\n", osd); |
| 1673 | if ((map->osd_state[osd] & CEPH_OSD_EXISTS) && |
| 1674 | (xorstate & CEPH_OSD_EXISTS)) { |
| 1675 | pr_info("osd%d does not exist\n", osd); |
Ilya Dryomov | 930c532 | 2016-07-19 03:50:28 +0200 | [diff] [blame] | 1676 | ret = set_primary_affinity(map, osd, |
| 1677 | CEPH_OSD_DEFAULT_PRIMARY_AFFINITY); |
| 1678 | if (ret) |
| 1679 | return ret; |
| 1680 | memset(map->osd_addr + osd, 0, sizeof(*map->osd_addr)); |
| 1681 | map->osd_state[osd] = 0; |
| 1682 | } else { |
| 1683 | map->osd_state[osd] ^= xorstate; |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | /* new_up_client */ |
| 1688 | *p = new_up_client; |
| 1689 | len = ceph_decode_32(p); |
| 1690 | while (len--) { |
| 1691 | s32 osd; |
| 1692 | struct ceph_entity_addr addr; |
| 1693 | |
| 1694 | osd = ceph_decode_32(p); |
Ilya Dryomov | 930c532 | 2016-07-19 03:50:28 +0200 | [diff] [blame] | 1695 | BUG_ON(osd >= map->max_osd); |
Jeff Layton | 8cb5f2b | 2019-06-04 15:10:44 -0400 | [diff] [blame] | 1696 | if (ceph_decode_entity_addr(p, end, &addr)) |
| 1697 | goto e_inval; |
Ilya Dryomov | 930c532 | 2016-07-19 03:50:28 +0200 | [diff] [blame] | 1698 | pr_info("osd%d up\n", osd); |
| 1699 | map->osd_state[osd] |= CEPH_OSD_EXISTS | CEPH_OSD_UP; |
| 1700 | map->osd_addr[osd] = addr; |
| 1701 | } |
| 1702 | |
| 1703 | *p = new_weight_end; |
| 1704 | return 0; |
| 1705 | |
| 1706 | e_inval: |
| 1707 | return -EINVAL; |
| 1708 | } |
| 1709 | |
| 1710 | /* |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1711 | * decode and apply an incremental map update. |
| 1712 | */ |
| 1713 | struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end, |
Ilya Dryomov | 0c0a8de | 2016-04-28 16:07:21 +0200 | [diff] [blame] | 1714 | struct ceph_osdmap *map) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1715 | { |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1716 | struct ceph_fsid fsid; |
| 1717 | u32 epoch = 0; |
| 1718 | struct ceph_timespec modified; |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 1719 | s32 len; |
| 1720 | u64 pool; |
| 1721 | __s64 new_pool_max; |
| 1722 | __s32 new_flags, max; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1723 | void *start = *p; |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1724 | int err; |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1725 | u8 struct_v; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1726 | |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1727 | dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p)); |
| 1728 | |
Ilya Dryomov | ec7af97 | 2014-03-21 19:05:29 +0200 | [diff] [blame] | 1729 | err = get_osdmap_client_data_v(p, end, "inc", &struct_v); |
| 1730 | if (err) |
| 1731 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1732 | |
Ilya Dryomov | 53bbaba | 2014-03-13 16:36:16 +0200 | [diff] [blame] | 1733 | /* fsid, epoch, modified, new_pool_max, new_flags */ |
| 1734 | ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) + |
| 1735 | sizeof(u64) + sizeof(u32), e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1736 | ceph_decode_copy(p, &fsid, sizeof(fsid)); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 1737 | epoch = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1738 | BUG_ON(epoch != map->epoch+1); |
| 1739 | ceph_decode_copy(p, &modified, sizeof(modified)); |
Sage Weil | 4f6a7e5 | 2013-02-23 10:41:09 -0800 | [diff] [blame] | 1740 | new_pool_max = ceph_decode_64(p); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 1741 | new_flags = ceph_decode_32(p); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1742 | |
| 1743 | /* full map? */ |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1744 | ceph_decode_32_safe(p, end, len, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1745 | if (len > 0) { |
| 1746 | dout("apply_incremental full map len %d, %p to %p\n", |
| 1747 | len, *p, end); |
Ilya Dryomov | a2505d6 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1748 | return ceph_osdmap_decode(p, min(*p+len, end)); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | /* new crush? */ |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1752 | ceph_decode_32_safe(p, end, len, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1753 | if (len > 0) { |
Ilya Dryomov | 1b6a78b | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 1754 | err = osdmap_set_crush(map, |
| 1755 | crush_decode(*p, min(*p + len, end))); |
| 1756 | if (err) |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1757 | goto bad; |
Sage Weil | cebc5be | 2010-06-17 10:22:48 -0700 | [diff] [blame] | 1758 | *p += len; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | /* new flags? */ |
| 1762 | if (new_flags >= 0) |
| 1763 | map->flags = new_flags; |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1764 | if (new_pool_max >= 0) |
| 1765 | map->pool_max = new_pool_max; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1766 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1767 | /* new max? */ |
Ilya Dryomov | 53bbaba | 2014-03-13 16:36:16 +0200 | [diff] [blame] | 1768 | ceph_decode_32_safe(p, end, max, e_inval); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1769 | if (max >= 0) { |
| 1770 | err = osdmap_set_max_osd(map, max); |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1771 | if (err) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1772 | goto bad; |
| 1773 | } |
| 1774 | |
| 1775 | map->epoch++; |
Sage Weil | 3145666 | 2011-05-12 15:18:43 -0700 | [diff] [blame] | 1776 | map->modified = modified; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1777 | |
Ilya Dryomov | 433fbdd | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1778 | /* new_pools */ |
| 1779 | err = decode_new_pools(p, end, map); |
| 1780 | if (err) |
| 1781 | goto bad; |
Ilya Dryomov | 9464d00 | 2014-03-13 16:36:16 +0200 | [diff] [blame] | 1782 | |
Ilya Dryomov | 0f70c7e | 2014-03-21 19:05:27 +0200 | [diff] [blame] | 1783 | /* new_pool_names */ |
| 1784 | err = decode_pool_names(p, end, map); |
Ilya Dryomov | 9464d00 | 2014-03-13 16:36:16 +0200 | [diff] [blame] | 1785 | if (err) |
| 1786 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1787 | |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1788 | /* old_pool */ |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1789 | ceph_decode_32_safe(p, end, len, e_inval); |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1790 | while (len--) { |
| 1791 | struct ceph_pg_pool_info *pi; |
| 1792 | |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1793 | ceph_decode_64_safe(p, end, pool, e_inval); |
Ilya Dryomov | 8a4b863 | 2020-05-19 16:46:47 +0200 | [diff] [blame^] | 1794 | pi = lookup_pg_pool(&map->pg_pools, pool); |
Sage Weil | 2844a76 | 2010-04-09 15:46:42 -0700 | [diff] [blame] | 1795 | if (pi) |
| 1796 | __remove_pg_pool(&map->pg_pools, pi); |
Sage Weil | 4fc51be | 2010-02-16 15:55:03 -0800 | [diff] [blame] | 1797 | } |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1798 | |
Ilya Dryomov | 930c532 | 2016-07-19 03:50:28 +0200 | [diff] [blame] | 1799 | /* new_up_client, new_state, new_weight */ |
Ilya Dryomov | 0bb05da | 2017-06-22 19:44:06 +0200 | [diff] [blame] | 1800 | err = decode_new_up_state_weight(p, end, struct_v, map); |
Ilya Dryomov | 930c532 | 2016-07-19 03:50:28 +0200 | [diff] [blame] | 1801 | if (err) |
| 1802 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1803 | |
| 1804 | /* new_pg_temp */ |
Ilya Dryomov | 10db634 | 2014-03-21 19:05:28 +0200 | [diff] [blame] | 1805 | err = decode_new_pg_temp(p, end, map); |
| 1806 | if (err) |
| 1807 | goto bad; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1808 | |
Ilya Dryomov | d286de7 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1809 | /* new_primary_temp */ |
| 1810 | if (struct_v >= 1) { |
| 1811 | err = decode_new_primary_temp(p, end, map); |
| 1812 | if (err) |
| 1813 | goto bad; |
| 1814 | } |
| 1815 | |
Ilya Dryomov | 63a6993 | 2014-03-21 19:05:30 +0200 | [diff] [blame] | 1816 | /* new_primary_affinity */ |
| 1817 | if (struct_v >= 2) { |
| 1818 | err = decode_new_primary_affinity(p, end, map); |
| 1819 | if (err) |
| 1820 | goto bad; |
| 1821 | } |
| 1822 | |
Ilya Dryomov | 6f428df | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 1823 | if (struct_v >= 3) { |
| 1824 | /* new_erasure_code_profiles */ |
| 1825 | ceph_decode_skip_map_of_map(p, end, string, string, string, |
Dan Carpenter | 00c8ebb3 | 2017-07-13 10:45:17 +0300 | [diff] [blame] | 1826 | e_inval); |
Ilya Dryomov | 6f428df | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 1827 | /* old_erasure_code_profiles */ |
Dan Carpenter | 00c8ebb3 | 2017-07-13 10:45:17 +0300 | [diff] [blame] | 1828 | ceph_decode_skip_set(p, end, string, e_inval); |
Ilya Dryomov | 6f428df | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | if (struct_v >= 4) { |
| 1832 | err = decode_new_pg_upmap(p, end, map); |
| 1833 | if (err) |
| 1834 | goto bad; |
| 1835 | |
| 1836 | err = decode_old_pg_upmap(p, end, map); |
| 1837 | if (err) |
| 1838 | goto bad; |
| 1839 | |
| 1840 | err = decode_new_pg_upmap_items(p, end, map); |
| 1841 | if (err) |
| 1842 | goto bad; |
| 1843 | |
| 1844 | err = decode_old_pg_upmap_items(p, end, map); |
| 1845 | if (err) |
| 1846 | goto bad; |
| 1847 | } |
| 1848 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1849 | /* ignore the rest */ |
| 1850 | *p = end; |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1851 | |
| 1852 | dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1853 | return map; |
| 1854 | |
Ilya Dryomov | 86f1742 | 2014-03-13 16:36:15 +0200 | [diff] [blame] | 1855 | e_inval: |
| 1856 | err = -EINVAL; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1857 | bad: |
Ilya Dryomov | 38a8d56 | 2014-03-13 16:36:13 +0200 | [diff] [blame] | 1858 | pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n", |
| 1859 | err, epoch, (int)(*p - start), *p, start, end); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 1860 | print_hex_dump(KERN_DEBUG, "osdmap: ", |
| 1861 | DUMP_PREFIX_OFFSET, 16, 1, |
| 1862 | start, end - start, true); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1863 | return ERR_PTR(err); |
| 1864 | } |
| 1865 | |
Yan, Zheng | 30c156d | 2016-02-14 11:24:31 +0800 | [diff] [blame] | 1866 | void ceph_oloc_copy(struct ceph_object_locator *dest, |
| 1867 | const struct ceph_object_locator *src) |
| 1868 | { |
Ilya Dryomov | ca35ffe | 2017-06-05 14:44:59 +0200 | [diff] [blame] | 1869 | ceph_oloc_destroy(dest); |
Yan, Zheng | 30c156d | 2016-02-14 11:24:31 +0800 | [diff] [blame] | 1870 | |
| 1871 | dest->pool = src->pool; |
| 1872 | if (src->pool_ns) |
| 1873 | dest->pool_ns = ceph_get_string(src->pool_ns); |
Ilya Dryomov | ca35ffe | 2017-06-05 14:44:59 +0200 | [diff] [blame] | 1874 | else |
| 1875 | dest->pool_ns = NULL; |
Yan, Zheng | 30c156d | 2016-02-14 11:24:31 +0800 | [diff] [blame] | 1876 | } |
| 1877 | EXPORT_SYMBOL(ceph_oloc_copy); |
| 1878 | |
| 1879 | void ceph_oloc_destroy(struct ceph_object_locator *oloc) |
| 1880 | { |
| 1881 | ceph_put_string(oloc->pool_ns); |
| 1882 | } |
| 1883 | EXPORT_SYMBOL(ceph_oloc_destroy); |
| 1884 | |
Ilya Dryomov | d30291b | 2016-04-29 19:54:20 +0200 | [diff] [blame] | 1885 | void ceph_oid_copy(struct ceph_object_id *dest, |
| 1886 | const struct ceph_object_id *src) |
| 1887 | { |
Ilya Dryomov | ca35ffe | 2017-06-05 14:44:59 +0200 | [diff] [blame] | 1888 | ceph_oid_destroy(dest); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1889 | |
Ilya Dryomov | d30291b | 2016-04-29 19:54:20 +0200 | [diff] [blame] | 1890 | if (src->name != src->inline_name) { |
| 1891 | /* very rare, see ceph_object_id definition */ |
| 1892 | dest->name = kmalloc(src->name_len + 1, |
| 1893 | GFP_NOIO | __GFP_NOFAIL); |
Ilya Dryomov | ca35ffe | 2017-06-05 14:44:59 +0200 | [diff] [blame] | 1894 | } else { |
| 1895 | dest->name = dest->inline_name; |
Ilya Dryomov | d30291b | 2016-04-29 19:54:20 +0200 | [diff] [blame] | 1896 | } |
Ilya Dryomov | d30291b | 2016-04-29 19:54:20 +0200 | [diff] [blame] | 1897 | memcpy(dest->name, src->name, src->name_len + 1); |
| 1898 | dest->name_len = src->name_len; |
| 1899 | } |
| 1900 | EXPORT_SYMBOL(ceph_oid_copy); |
| 1901 | |
| 1902 | static __printf(2, 0) |
| 1903 | int oid_printf_vargs(struct ceph_object_id *oid, const char *fmt, va_list ap) |
| 1904 | { |
| 1905 | int len; |
| 1906 | |
| 1907 | WARN_ON(!ceph_oid_empty(oid)); |
| 1908 | |
| 1909 | len = vsnprintf(oid->inline_name, sizeof(oid->inline_name), fmt, ap); |
| 1910 | if (len >= sizeof(oid->inline_name)) |
| 1911 | return len; |
| 1912 | |
| 1913 | oid->name_len = len; |
| 1914 | return 0; |
| 1915 | } |
| 1916 | |
| 1917 | /* |
| 1918 | * If oid doesn't fit into inline buffer, BUG. |
| 1919 | */ |
| 1920 | void ceph_oid_printf(struct ceph_object_id *oid, const char *fmt, ...) |
| 1921 | { |
| 1922 | va_list ap; |
| 1923 | |
| 1924 | va_start(ap, fmt); |
| 1925 | BUG_ON(oid_printf_vargs(oid, fmt, ap)); |
| 1926 | va_end(ap); |
| 1927 | } |
| 1928 | EXPORT_SYMBOL(ceph_oid_printf); |
| 1929 | |
| 1930 | static __printf(3, 0) |
| 1931 | int oid_aprintf_vargs(struct ceph_object_id *oid, gfp_t gfp, |
| 1932 | const char *fmt, va_list ap) |
| 1933 | { |
| 1934 | va_list aq; |
| 1935 | int len; |
| 1936 | |
| 1937 | va_copy(aq, ap); |
| 1938 | len = oid_printf_vargs(oid, fmt, aq); |
| 1939 | va_end(aq); |
| 1940 | |
| 1941 | if (len) { |
| 1942 | char *external_name; |
| 1943 | |
| 1944 | external_name = kmalloc(len + 1, gfp); |
| 1945 | if (!external_name) |
| 1946 | return -ENOMEM; |
| 1947 | |
| 1948 | oid->name = external_name; |
| 1949 | WARN_ON(vsnprintf(oid->name, len + 1, fmt, ap) != len); |
| 1950 | oid->name_len = len; |
| 1951 | } |
| 1952 | |
| 1953 | return 0; |
| 1954 | } |
| 1955 | |
| 1956 | /* |
| 1957 | * If oid doesn't fit into inline buffer, allocate. |
| 1958 | */ |
| 1959 | int ceph_oid_aprintf(struct ceph_object_id *oid, gfp_t gfp, |
| 1960 | const char *fmt, ...) |
| 1961 | { |
| 1962 | va_list ap; |
| 1963 | int ret; |
| 1964 | |
| 1965 | va_start(ap, fmt); |
| 1966 | ret = oid_aprintf_vargs(oid, gfp, fmt, ap); |
| 1967 | va_end(ap); |
| 1968 | |
| 1969 | return ret; |
| 1970 | } |
| 1971 | EXPORT_SYMBOL(ceph_oid_aprintf); |
| 1972 | |
| 1973 | void ceph_oid_destroy(struct ceph_object_id *oid) |
| 1974 | { |
| 1975 | if (oid->name != oid->inline_name) |
| 1976 | kfree(oid->name); |
| 1977 | } |
| 1978 | EXPORT_SYMBOL(ceph_oid_destroy); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 1979 | |
Ilya Dryomov | 63244fa | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 1980 | /* |
| 1981 | * osds only |
| 1982 | */ |
| 1983 | static bool __osds_equal(const struct ceph_osds *lhs, |
| 1984 | const struct ceph_osds *rhs) |
| 1985 | { |
| 1986 | if (lhs->size == rhs->size && |
| 1987 | !memcmp(lhs->osds, rhs->osds, rhs->size * sizeof(rhs->osds[0]))) |
| 1988 | return true; |
| 1989 | |
| 1990 | return false; |
| 1991 | } |
| 1992 | |
| 1993 | /* |
| 1994 | * osds + primary |
| 1995 | */ |
| 1996 | static bool osds_equal(const struct ceph_osds *lhs, |
| 1997 | const struct ceph_osds *rhs) |
| 1998 | { |
| 1999 | if (__osds_equal(lhs, rhs) && |
| 2000 | lhs->primary == rhs->primary) |
| 2001 | return true; |
| 2002 | |
| 2003 | return false; |
| 2004 | } |
| 2005 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2006 | static bool osds_valid(const struct ceph_osds *set) |
| 2007 | { |
| 2008 | /* non-empty set */ |
| 2009 | if (set->size > 0 && set->primary >= 0) |
| 2010 | return true; |
| 2011 | |
| 2012 | /* empty can_shift_osds set */ |
| 2013 | if (!set->size && set->primary == -1) |
| 2014 | return true; |
| 2015 | |
| 2016 | /* empty !can_shift_osds set - all NONE */ |
| 2017 | if (set->size > 0 && set->primary == -1) { |
| 2018 | int i; |
| 2019 | |
| 2020 | for (i = 0; i < set->size; i++) { |
| 2021 | if (set->osds[i] != CRUSH_ITEM_NONE) |
| 2022 | break; |
| 2023 | } |
| 2024 | if (i == set->size) |
| 2025 | return true; |
| 2026 | } |
| 2027 | |
| 2028 | return false; |
| 2029 | } |
| 2030 | |
| 2031 | void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src) |
| 2032 | { |
| 2033 | memcpy(dest->osds, src->osds, src->size * sizeof(src->osds[0])); |
| 2034 | dest->size = src->size; |
| 2035 | dest->primary = src->primary; |
| 2036 | } |
| 2037 | |
Ilya Dryomov | 7de030d | 2017-06-15 16:30:54 +0200 | [diff] [blame] | 2038 | bool ceph_pg_is_split(const struct ceph_pg *pgid, u32 old_pg_num, |
| 2039 | u32 new_pg_num) |
Ilya Dryomov | 63244fa | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 2040 | { |
| 2041 | int old_bits = calc_bits_of(old_pg_num); |
| 2042 | int old_mask = (1 << old_bits) - 1; |
| 2043 | int n; |
| 2044 | |
| 2045 | WARN_ON(pgid->seed >= old_pg_num); |
| 2046 | if (new_pg_num <= old_pg_num) |
| 2047 | return false; |
| 2048 | |
| 2049 | for (n = 1; ; n++) { |
| 2050 | int next_bit = n << (old_bits - 1); |
| 2051 | u32 s = next_bit | pgid->seed; |
| 2052 | |
| 2053 | if (s < old_pg_num || s == pgid->seed) |
| 2054 | continue; |
| 2055 | if (s >= new_pg_num) |
| 2056 | break; |
| 2057 | |
| 2058 | s = ceph_stable_mod(s, old_pg_num, old_mask); |
| 2059 | if (s == pgid->seed) |
| 2060 | return true; |
| 2061 | } |
| 2062 | |
| 2063 | return false; |
| 2064 | } |
| 2065 | |
| 2066 | bool ceph_is_new_interval(const struct ceph_osds *old_acting, |
| 2067 | const struct ceph_osds *new_acting, |
| 2068 | const struct ceph_osds *old_up, |
| 2069 | const struct ceph_osds *new_up, |
| 2070 | int old_size, |
| 2071 | int new_size, |
| 2072 | int old_min_size, |
| 2073 | int new_min_size, |
| 2074 | u32 old_pg_num, |
| 2075 | u32 new_pg_num, |
| 2076 | bool old_sort_bitwise, |
| 2077 | bool new_sort_bitwise, |
Ilya Dryomov | ae78dd8 | 2017-07-27 17:59:14 +0200 | [diff] [blame] | 2078 | bool old_recovery_deletes, |
| 2079 | bool new_recovery_deletes, |
Ilya Dryomov | 63244fa | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 2080 | const struct ceph_pg *pgid) |
| 2081 | { |
| 2082 | return !osds_equal(old_acting, new_acting) || |
| 2083 | !osds_equal(old_up, new_up) || |
| 2084 | old_size != new_size || |
| 2085 | old_min_size != new_min_size || |
Ilya Dryomov | 7de030d | 2017-06-15 16:30:54 +0200 | [diff] [blame] | 2086 | ceph_pg_is_split(pgid, old_pg_num, new_pg_num) || |
Ilya Dryomov | ae78dd8 | 2017-07-27 17:59:14 +0200 | [diff] [blame] | 2087 | old_sort_bitwise != new_sort_bitwise || |
| 2088 | old_recovery_deletes != new_recovery_deletes; |
Ilya Dryomov | 63244fa | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | static int calc_pg_rank(int osd, const struct ceph_osds *acting) |
| 2092 | { |
| 2093 | int i; |
| 2094 | |
| 2095 | for (i = 0; i < acting->size; i++) { |
| 2096 | if (acting->osds[i] == osd) |
| 2097 | return i; |
| 2098 | } |
| 2099 | |
| 2100 | return -1; |
| 2101 | } |
| 2102 | |
| 2103 | static bool primary_changed(const struct ceph_osds *old_acting, |
| 2104 | const struct ceph_osds *new_acting) |
| 2105 | { |
| 2106 | if (!old_acting->size && !new_acting->size) |
| 2107 | return false; /* both still empty */ |
| 2108 | |
| 2109 | if (!old_acting->size ^ !new_acting->size) |
| 2110 | return true; /* was empty, now not, or vice versa */ |
| 2111 | |
| 2112 | if (old_acting->primary != new_acting->primary) |
| 2113 | return true; /* primary changed */ |
| 2114 | |
| 2115 | if (calc_pg_rank(old_acting->primary, old_acting) != |
| 2116 | calc_pg_rank(new_acting->primary, new_acting)) |
| 2117 | return true; |
| 2118 | |
| 2119 | return false; /* same primary (tho replicas may have changed) */ |
| 2120 | } |
| 2121 | |
| 2122 | bool ceph_osds_changed(const struct ceph_osds *old_acting, |
| 2123 | const struct ceph_osds *new_acting, |
| 2124 | bool any_change) |
| 2125 | { |
| 2126 | if (primary_changed(old_acting, new_acting)) |
| 2127 | return true; |
| 2128 | |
| 2129 | if (any_change && !__osds_equal(old_acting, new_acting)) |
| 2130 | return true; |
| 2131 | |
| 2132 | return false; |
| 2133 | } |
| 2134 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 2135 | /* |
Ilya Dryomov | d9591f5 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2136 | * Map an object into a PG. |
| 2137 | * |
| 2138 | * Should only be called with target_oid and target_oloc (as opposed to |
| 2139 | * base_oid and base_oloc), since tiering isn't taken into account. |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 2140 | */ |
Ilya Dryomov | a86f009 | 2018-05-23 14:46:53 +0200 | [diff] [blame] | 2141 | void __ceph_object_locator_to_pg(struct ceph_pg_pool_info *pi, |
| 2142 | const struct ceph_object_id *oid, |
| 2143 | const struct ceph_object_locator *oloc, |
| 2144 | struct ceph_pg *raw_pgid) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 2145 | { |
Ilya Dryomov | df28152 | 2017-06-15 16:30:56 +0200 | [diff] [blame] | 2146 | WARN_ON(pi->id != oloc->pool); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 2147 | |
Yan, Zheng | 30c156d | 2016-02-14 11:24:31 +0800 | [diff] [blame] | 2148 | if (!oloc->pool_ns) { |
| 2149 | raw_pgid->pool = oloc->pool; |
| 2150 | raw_pgid->seed = ceph_str_hash(pi->object_hash, oid->name, |
| 2151 | oid->name_len); |
| 2152 | dout("%s %s -> raw_pgid %llu.%x\n", __func__, oid->name, |
| 2153 | raw_pgid->pool, raw_pgid->seed); |
| 2154 | } else { |
| 2155 | char stack_buf[256]; |
| 2156 | char *buf = stack_buf; |
| 2157 | int nsl = oloc->pool_ns->len; |
| 2158 | size_t total = nsl + 1 + oid->name_len; |
Ilya Dryomov | 7c13cb6 | 2014-01-27 17:40:19 +0200 | [diff] [blame] | 2159 | |
Ilya Dryomov | a86f009 | 2018-05-23 14:46:53 +0200 | [diff] [blame] | 2160 | if (total > sizeof(stack_buf)) |
| 2161 | buf = kmalloc(total, GFP_NOIO | __GFP_NOFAIL); |
Yan, Zheng | 30c156d | 2016-02-14 11:24:31 +0800 | [diff] [blame] | 2162 | memcpy(buf, oloc->pool_ns->str, nsl); |
| 2163 | buf[nsl] = '\037'; |
| 2164 | memcpy(buf + nsl + 1, oid->name, oid->name_len); |
| 2165 | raw_pgid->pool = oloc->pool; |
| 2166 | raw_pgid->seed = ceph_str_hash(pi->object_hash, buf, total); |
| 2167 | if (buf != stack_buf) |
| 2168 | kfree(buf); |
| 2169 | dout("%s %s ns %.*s -> raw_pgid %llu.%x\n", __func__, |
| 2170 | oid->name, nsl, oloc->pool_ns->str, |
| 2171 | raw_pgid->pool, raw_pgid->seed); |
| 2172 | } |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 2173 | } |
Ilya Dryomov | df28152 | 2017-06-15 16:30:56 +0200 | [diff] [blame] | 2174 | |
| 2175 | int ceph_object_locator_to_pg(struct ceph_osdmap *osdmap, |
| 2176 | const struct ceph_object_id *oid, |
| 2177 | const struct ceph_object_locator *oloc, |
| 2178 | struct ceph_pg *raw_pgid) |
| 2179 | { |
| 2180 | struct ceph_pg_pool_info *pi; |
| 2181 | |
| 2182 | pi = ceph_pg_pool_by_id(osdmap, oloc->pool); |
| 2183 | if (!pi) |
| 2184 | return -ENOENT; |
| 2185 | |
Ilya Dryomov | a86f009 | 2018-05-23 14:46:53 +0200 | [diff] [blame] | 2186 | __ceph_object_locator_to_pg(pi, oid, oloc, raw_pgid); |
| 2187 | return 0; |
Ilya Dryomov | df28152 | 2017-06-15 16:30:56 +0200 | [diff] [blame] | 2188 | } |
Ilya Dryomov | d9591f5 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2189 | EXPORT_SYMBOL(ceph_object_locator_to_pg); |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 2190 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2191 | /* |
| 2192 | * Map a raw PG (full precision ps) into an actual PG. |
| 2193 | */ |
| 2194 | static void raw_pg_to_pg(struct ceph_pg_pool_info *pi, |
| 2195 | const struct ceph_pg *raw_pgid, |
| 2196 | struct ceph_pg *pgid) |
| 2197 | { |
| 2198 | pgid->pool = raw_pgid->pool; |
| 2199 | pgid->seed = ceph_stable_mod(raw_pgid->seed, pi->pg_num, |
| 2200 | pi->pg_num_mask); |
| 2201 | } |
| 2202 | |
| 2203 | /* |
| 2204 | * Map a raw PG (full precision ps) into a placement ps (placement |
| 2205 | * seed). Include pool id in that value so that different pools don't |
| 2206 | * use the same seeds. |
| 2207 | */ |
| 2208 | static u32 raw_pg_to_pps(struct ceph_pg_pool_info *pi, |
| 2209 | const struct ceph_pg *raw_pgid) |
| 2210 | { |
| 2211 | if (pi->flags & CEPH_POOL_FLAG_HASHPSPOOL) { |
| 2212 | /* hash pool id and seed so that pool PGs do not overlap */ |
| 2213 | return crush_hash32_2(CRUSH_HASH_RJENKINS1, |
| 2214 | ceph_stable_mod(raw_pgid->seed, |
| 2215 | pi->pgp_num, |
| 2216 | pi->pgp_num_mask), |
| 2217 | raw_pgid->pool); |
| 2218 | } else { |
| 2219 | /* |
| 2220 | * legacy behavior: add ps and pool together. this is |
| 2221 | * not a great approach because the PGs from each pool |
| 2222 | * will overlap on top of each other: 0.5 == 1.4 == |
| 2223 | * 2.3 == ... |
| 2224 | */ |
| 2225 | return ceph_stable_mod(raw_pgid->seed, pi->pgp_num, |
| 2226 | pi->pgp_num_mask) + |
| 2227 | (unsigned)raw_pgid->pool; |
| 2228 | } |
| 2229 | } |
| 2230 | |
Ilya Dryomov | e17e896 | 2017-07-24 16:43:49 +0200 | [diff] [blame] | 2231 | /* |
| 2232 | * Magic value used for a "default" fallback choose_args, used if the |
| 2233 | * crush_choose_arg_map passed to do_crush() does not exist. If this |
| 2234 | * also doesn't exist, fall back to canonical weights. |
| 2235 | */ |
| 2236 | #define CEPH_DEFAULT_CHOOSE_ARGS -1 |
| 2237 | |
Ilya Dryomov | 9d52147 | 2014-01-31 17:54:26 +0200 | [diff] [blame] | 2238 | static int do_crush(struct ceph_osdmap *map, int ruleno, int x, |
| 2239 | int *result, int result_max, |
Ilya Dryomov | 5cf9c4a | 2017-06-22 19:44:05 +0200 | [diff] [blame] | 2240 | const __u32 *weight, int weight_max, |
Ilya Dryomov | e17e896 | 2017-07-24 16:43:49 +0200 | [diff] [blame] | 2241 | s64 choose_args_index) |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 2242 | { |
Ilya Dryomov | 5cf9c4a | 2017-06-22 19:44:05 +0200 | [diff] [blame] | 2243 | struct crush_choose_arg_map *arg_map; |
Ilya Dryomov | 9d52147 | 2014-01-31 17:54:26 +0200 | [diff] [blame] | 2244 | int r; |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 2245 | |
Ilya Dryomov | 9d52147 | 2014-01-31 17:54:26 +0200 | [diff] [blame] | 2246 | BUG_ON(result_max > CEPH_PG_MAX_SIZE); |
| 2247 | |
Ilya Dryomov | 5cf9c4a | 2017-06-22 19:44:05 +0200 | [diff] [blame] | 2248 | arg_map = lookup_choose_arg_map(&map->crush->choose_args, |
| 2249 | choose_args_index); |
Ilya Dryomov | e17e896 | 2017-07-24 16:43:49 +0200 | [diff] [blame] | 2250 | if (!arg_map) |
| 2251 | arg_map = lookup_choose_arg_map(&map->crush->choose_args, |
| 2252 | CEPH_DEFAULT_CHOOSE_ARGS); |
Ilya Dryomov | 5cf9c4a | 2017-06-22 19:44:05 +0200 | [diff] [blame] | 2253 | |
Ilya Dryomov | 743efcf | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 2254 | mutex_lock(&map->crush_workspace_mutex); |
Ilya Dryomov | 9d52147 | 2014-01-31 17:54:26 +0200 | [diff] [blame] | 2255 | r = crush_do_rule(map->crush, ruleno, x, result, result_max, |
Ilya Dryomov | 5cf9c4a | 2017-06-22 19:44:05 +0200 | [diff] [blame] | 2256 | weight, weight_max, map->crush_workspace, |
| 2257 | arg_map ? arg_map->args : NULL); |
Ilya Dryomov | 743efcf | 2017-01-31 15:55:06 +0100 | [diff] [blame] | 2258 | mutex_unlock(&map->crush_workspace_mutex); |
Ilya Dryomov | 9d52147 | 2014-01-31 17:54:26 +0200 | [diff] [blame] | 2259 | |
| 2260 | return r; |
Ilya Dryomov | e8ef19c | 2013-12-24 21:19:24 +0200 | [diff] [blame] | 2261 | } |
| 2262 | |
Ilya Dryomov | 1c2e7b4 | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 2263 | static void remove_nonexistent_osds(struct ceph_osdmap *osdmap, |
| 2264 | struct ceph_pg_pool_info *pi, |
| 2265 | struct ceph_osds *set) |
| 2266 | { |
| 2267 | int i; |
| 2268 | |
| 2269 | if (ceph_can_shift_osds(pi)) { |
| 2270 | int removed = 0; |
| 2271 | |
| 2272 | /* shift left */ |
| 2273 | for (i = 0; i < set->size; i++) { |
| 2274 | if (!ceph_osd_exists(osdmap, set->osds[i])) { |
| 2275 | removed++; |
| 2276 | continue; |
| 2277 | } |
| 2278 | if (removed) |
| 2279 | set->osds[i - removed] = set->osds[i]; |
| 2280 | } |
| 2281 | set->size -= removed; |
| 2282 | } else { |
| 2283 | /* set dne devices to NONE */ |
| 2284 | for (i = 0; i < set->size; i++) { |
| 2285 | if (!ceph_osd_exists(osdmap, set->osds[i])) |
| 2286 | set->osds[i] = CRUSH_ITEM_NONE; |
| 2287 | } |
| 2288 | } |
| 2289 | } |
| 2290 | |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 2291 | /* |
Ilya Dryomov | 1c2e7b4 | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 2292 | * Calculate raw set (CRUSH output) for given PG and filter out |
| 2293 | * nonexistent OSDs. ->primary is undefined for a raw set. |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2294 | * |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2295 | * Placement seed (CRUSH input) is returned through @ppps. |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2296 | */ |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2297 | static void pg_to_raw_osds(struct ceph_osdmap *osdmap, |
| 2298 | struct ceph_pg_pool_info *pi, |
| 2299 | const struct ceph_pg *raw_pgid, |
| 2300 | struct ceph_osds *raw, |
| 2301 | u32 *ppps) |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2302 | { |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2303 | u32 pps = raw_pg_to_pps(pi, raw_pgid); |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2304 | int ruleno; |
| 2305 | int len; |
| 2306 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2307 | ceph_osds_init(raw); |
| 2308 | if (ppps) |
| 2309 | *ppps = pps; |
| 2310 | |
| 2311 | ruleno = crush_find_rule(osdmap->crush, pi->crush_ruleset, pi->type, |
| 2312 | pi->size); |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2313 | if (ruleno < 0) { |
| 2314 | pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n", |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2315 | pi->id, pi->crush_ruleset, pi->type, pi->size); |
| 2316 | return; |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2317 | } |
| 2318 | |
Ilya Dryomov | ef9324bb | 2017-02-08 18:57:48 +0100 | [diff] [blame] | 2319 | if (pi->size > ARRAY_SIZE(raw->osds)) { |
| 2320 | pr_err_ratelimited("pool %lld ruleset %d type %d too wide: size %d > %zu\n", |
| 2321 | pi->id, pi->crush_ruleset, pi->type, pi->size, |
| 2322 | ARRAY_SIZE(raw->osds)); |
| 2323 | return; |
| 2324 | } |
| 2325 | |
| 2326 | len = do_crush(osdmap, ruleno, pps, raw->osds, pi->size, |
Ilya Dryomov | 5cf9c4a | 2017-06-22 19:44:05 +0200 | [diff] [blame] | 2327 | osdmap->osd_weight, osdmap->max_osd, pi->id); |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2328 | if (len < 0) { |
| 2329 | pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n", |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2330 | len, ruleno, pi->id, pi->crush_ruleset, pi->type, |
| 2331 | pi->size); |
| 2332 | return; |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2333 | } |
| 2334 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2335 | raw->size = len; |
Ilya Dryomov | 1c2e7b4 | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 2336 | remove_nonexistent_osds(osdmap, pi, raw); |
| 2337 | } |
| 2338 | |
| 2339 | /* apply pg_upmap[_items] mappings */ |
| 2340 | static void apply_upmap(struct ceph_osdmap *osdmap, |
| 2341 | const struct ceph_pg *pgid, |
| 2342 | struct ceph_osds *raw) |
| 2343 | { |
| 2344 | struct ceph_pg_mapping *pg; |
| 2345 | int i, j; |
| 2346 | |
| 2347 | pg = lookup_pg_mapping(&osdmap->pg_upmap, pgid); |
| 2348 | if (pg) { |
| 2349 | /* make sure targets aren't marked out */ |
| 2350 | for (i = 0; i < pg->pg_upmap.len; i++) { |
| 2351 | int osd = pg->pg_upmap.osds[i]; |
| 2352 | |
| 2353 | if (osd != CRUSH_ITEM_NONE && |
| 2354 | osd < osdmap->max_osd && |
| 2355 | osdmap->osd_weight[osd] == 0) { |
| 2356 | /* reject/ignore explicit mapping */ |
| 2357 | return; |
| 2358 | } |
| 2359 | } |
| 2360 | for (i = 0; i < pg->pg_upmap.len; i++) |
| 2361 | raw->osds[i] = pg->pg_upmap.osds[i]; |
| 2362 | raw->size = pg->pg_upmap.len; |
Ilya Dryomov | f53b766 | 2017-07-27 15:16:39 +0200 | [diff] [blame] | 2363 | /* check and apply pg_upmap_items, if any */ |
Ilya Dryomov | 1c2e7b4 | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | pg = lookup_pg_mapping(&osdmap->pg_upmap_items, pgid); |
| 2367 | if (pg) { |
Ilya Dryomov | 29a0cfb | 2017-09-18 12:21:37 +0200 | [diff] [blame] | 2368 | /* |
| 2369 | * Note: this approach does not allow a bidirectional swap, |
| 2370 | * e.g., [[1,2],[2,1]] applied to [0,1,2] -> [0,2,1]. |
| 2371 | */ |
| 2372 | for (i = 0; i < pg->pg_upmap_items.len; i++) { |
| 2373 | int from = pg->pg_upmap_items.from_to[i][0]; |
| 2374 | int to = pg->pg_upmap_items.from_to[i][1]; |
| 2375 | int pos = -1; |
| 2376 | bool exists = false; |
Ilya Dryomov | 1c2e7b4 | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 2377 | |
Ilya Dryomov | 29a0cfb | 2017-09-18 12:21:37 +0200 | [diff] [blame] | 2378 | /* make sure replacement doesn't already appear */ |
| 2379 | for (j = 0; j < raw->size; j++) { |
| 2380 | int osd = raw->osds[j]; |
| 2381 | |
| 2382 | if (osd == to) { |
| 2383 | exists = true; |
Ilya Dryomov | 1c2e7b4 | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 2384 | break; |
| 2385 | } |
Ilya Dryomov | 29a0cfb | 2017-09-18 12:21:37 +0200 | [diff] [blame] | 2386 | /* ignore mapping if target is marked out */ |
| 2387 | if (osd == from && pos < 0 && |
| 2388 | !(to != CRUSH_ITEM_NONE && |
| 2389 | to < osdmap->max_osd && |
| 2390 | osdmap->osd_weight[to] == 0)) { |
| 2391 | pos = j; |
| 2392 | } |
Ilya Dryomov | 1c2e7b4 | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 2393 | } |
Ilya Dryomov | 29a0cfb | 2017-09-18 12:21:37 +0200 | [diff] [blame] | 2394 | if (!exists && pos >= 0) |
| 2395 | raw->osds[pos] = to; |
Ilya Dryomov | 1c2e7b4 | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 2396 | } |
| 2397 | } |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | /* |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2401 | * Given raw set, calculate up set and up primary. By definition of an |
| 2402 | * up set, the result won't contain nonexistent or down OSDs. |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2403 | * |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2404 | * This is done in-place - on return @set is the up set. If it's |
| 2405 | * empty, ->primary will remain undefined. |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2406 | */ |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2407 | static void raw_to_up_osds(struct ceph_osdmap *osdmap, |
| 2408 | struct ceph_pg_pool_info *pi, |
| 2409 | struct ceph_osds *set) |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2410 | { |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2411 | int i; |
| 2412 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2413 | /* ->primary is undefined for a raw set */ |
| 2414 | BUG_ON(set->primary != -1); |
| 2415 | |
| 2416 | if (ceph_can_shift_osds(pi)) { |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2417 | int removed = 0; |
| 2418 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2419 | /* shift left */ |
| 2420 | for (i = 0; i < set->size; i++) { |
| 2421 | if (ceph_osd_is_down(osdmap, set->osds[i])) { |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2422 | removed++; |
| 2423 | continue; |
| 2424 | } |
| 2425 | if (removed) |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2426 | set->osds[i - removed] = set->osds[i]; |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2427 | } |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2428 | set->size -= removed; |
| 2429 | if (set->size > 0) |
| 2430 | set->primary = set->osds[0]; |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2431 | } else { |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2432 | /* set down/dne devices to NONE */ |
| 2433 | for (i = set->size - 1; i >= 0; i--) { |
| 2434 | if (ceph_osd_is_down(osdmap, set->osds[i])) |
| 2435 | set->osds[i] = CRUSH_ITEM_NONE; |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2436 | else |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2437 | set->primary = set->osds[i]; |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2438 | } |
| 2439 | } |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2440 | } |
| 2441 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2442 | static void apply_primary_affinity(struct ceph_osdmap *osdmap, |
| 2443 | struct ceph_pg_pool_info *pi, |
| 2444 | u32 pps, |
| 2445 | struct ceph_osds *up) |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 2446 | { |
| 2447 | int i; |
| 2448 | int pos = -1; |
| 2449 | |
| 2450 | /* |
| 2451 | * Do we have any non-default primary_affinity values for these |
| 2452 | * osds? |
| 2453 | */ |
| 2454 | if (!osdmap->osd_primary_affinity) |
| 2455 | return; |
| 2456 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2457 | for (i = 0; i < up->size; i++) { |
| 2458 | int osd = up->osds[i]; |
Ilya Dryomov | 92b2e75 | 2014-04-10 18:09:41 +0400 | [diff] [blame] | 2459 | |
| 2460 | if (osd != CRUSH_ITEM_NONE && |
| 2461 | osdmap->osd_primary_affinity[osd] != |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 2462 | CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) { |
| 2463 | break; |
| 2464 | } |
| 2465 | } |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2466 | if (i == up->size) |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 2467 | return; |
| 2468 | |
| 2469 | /* |
| 2470 | * Pick the primary. Feed both the seed (for the pg) and the |
| 2471 | * osd into the hash/rng so that a proportional fraction of an |
| 2472 | * osd's pgs get rejected as primary. |
| 2473 | */ |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2474 | for (i = 0; i < up->size; i++) { |
| 2475 | int osd = up->osds[i]; |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 2476 | u32 aff; |
| 2477 | |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 2478 | if (osd == CRUSH_ITEM_NONE) |
| 2479 | continue; |
| 2480 | |
| 2481 | aff = osdmap->osd_primary_affinity[osd]; |
| 2482 | if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY && |
| 2483 | (crush_hash32_2(CRUSH_HASH_RJENKINS1, |
| 2484 | pps, osd) >> 16) >= aff) { |
| 2485 | /* |
| 2486 | * We chose not to use this primary. Note it |
| 2487 | * anyway as a fallback in case we don't pick |
| 2488 | * anyone else, but keep looking. |
| 2489 | */ |
| 2490 | if (pos < 0) |
| 2491 | pos = i; |
| 2492 | } else { |
| 2493 | pos = i; |
| 2494 | break; |
| 2495 | } |
| 2496 | } |
| 2497 | if (pos < 0) |
| 2498 | return; |
| 2499 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2500 | up->primary = up->osds[pos]; |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 2501 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2502 | if (ceph_can_shift_osds(pi) && pos > 0) { |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 2503 | /* move the new primary to the front */ |
| 2504 | for (i = pos; i > 0; i--) |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2505 | up->osds[i] = up->osds[i - 1]; |
| 2506 | up->osds[0] = up->primary; |
Ilya Dryomov | 47ec1f3 | 2014-03-24 17:12:49 +0200 | [diff] [blame] | 2507 | } |
| 2508 | } |
| 2509 | |
Ilya Dryomov | 2bd93d4 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2510 | /* |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2511 | * Get pg_temp and primary_temp mappings for given PG. |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2512 | * |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2513 | * Note that a PG may have none, only pg_temp, only primary_temp or |
| 2514 | * both pg_temp and primary_temp mappings. This means @temp isn't |
| 2515 | * always a valid OSD set on return: in the "only primary_temp" case, |
| 2516 | * @temp will have its ->primary >= 0 but ->size == 0. |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2517 | */ |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2518 | static void get_temp_osds(struct ceph_osdmap *osdmap, |
| 2519 | struct ceph_pg_pool_info *pi, |
Ilya Dryomov | 463bb8d | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 2520 | const struct ceph_pg *pgid, |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2521 | struct ceph_osds *temp) |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2522 | { |
| 2523 | struct ceph_pg_mapping *pg; |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2524 | int i; |
| 2525 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2526 | ceph_osds_init(temp); |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2527 | |
| 2528 | /* pg_temp? */ |
Ilya Dryomov | 463bb8d | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 2529 | pg = lookup_pg_mapping(&osdmap->pg_temp, pgid); |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2530 | if (pg) { |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2531 | for (i = 0; i < pg->pg_temp.len; i++) { |
| 2532 | if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) { |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2533 | if (ceph_can_shift_osds(pi)) |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2534 | continue; |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2535 | |
| 2536 | temp->osds[temp->size++] = CRUSH_ITEM_NONE; |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2537 | } else { |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2538 | temp->osds[temp->size++] = pg->pg_temp.osds[i]; |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2539 | } |
| 2540 | } |
| 2541 | |
| 2542 | /* apply pg_temp's primary */ |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2543 | for (i = 0; i < temp->size; i++) { |
| 2544 | if (temp->osds[i] != CRUSH_ITEM_NONE) { |
| 2545 | temp->primary = temp->osds[i]; |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2546 | break; |
| 2547 | } |
| 2548 | } |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2549 | } |
| 2550 | |
Ilya Dryomov | 5e8d4d3 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 2551 | /* primary_temp? */ |
Ilya Dryomov | 463bb8d | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 2552 | pg = lookup_pg_mapping(&osdmap->primary_temp, pgid); |
Ilya Dryomov | 5e8d4d3 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 2553 | if (pg) |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2554 | temp->primary = pg->primary_temp.osd; |
Ilya Dryomov | 45966c3 | 2014-03-24 17:12:47 +0200 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | /* |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2558 | * Map a PG to its acting set as well as its up set. |
Ilya Dryomov | ac97223 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 2559 | * |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2560 | * Acting set is used for data mapping purposes, while up set can be |
| 2561 | * recorded for detecting interval changes and deciding whether to |
| 2562 | * resend a request. |
Sage Weil | d85b705 | 2010-05-10 10:24:48 -0700 | [diff] [blame] | 2563 | */ |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2564 | void ceph_pg_to_up_acting_osds(struct ceph_osdmap *osdmap, |
Ilya Dryomov | df28152 | 2017-06-15 16:30:56 +0200 | [diff] [blame] | 2565 | struct ceph_pg_pool_info *pi, |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2566 | const struct ceph_pg *raw_pgid, |
| 2567 | struct ceph_osds *up, |
| 2568 | struct ceph_osds *acting) |
Sage Weil | d85b705 | 2010-05-10 10:24:48 -0700 | [diff] [blame] | 2569 | { |
Ilya Dryomov | 463bb8d | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 2570 | struct ceph_pg pgid; |
Ilya Dryomov | ac97223 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 2571 | u32 pps; |
Sage Weil | d85b705 | 2010-05-10 10:24:48 -0700 | [diff] [blame] | 2572 | |
Ilya Dryomov | df28152 | 2017-06-15 16:30:56 +0200 | [diff] [blame] | 2573 | WARN_ON(pi->id != raw_pgid->pool); |
Ilya Dryomov | 463bb8d | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 2574 | raw_pg_to_pg(pi, raw_pgid, &pgid); |
Sage Weil | d85b705 | 2010-05-10 10:24:48 -0700 | [diff] [blame] | 2575 | |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2576 | pg_to_raw_osds(osdmap, pi, raw_pgid, up, &pps); |
Ilya Dryomov | 1c2e7b4 | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 2577 | apply_upmap(osdmap, &pgid, up); |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2578 | raw_to_up_osds(osdmap, pi, up); |
| 2579 | apply_primary_affinity(osdmap, pi, pps, up); |
Ilya Dryomov | 463bb8d | 2017-06-21 17:27:18 +0200 | [diff] [blame] | 2580 | get_temp_osds(osdmap, pi, &pgid, acting); |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2581 | if (!acting->size) { |
| 2582 | memcpy(acting->osds, up->osds, up->size * sizeof(up->osds[0])); |
| 2583 | acting->size = up->size; |
| 2584 | if (acting->primary == -1) |
| 2585 | acting->primary = up->primary; |
Ilya Dryomov | ac97223 | 2014-03-24 17:12:48 +0200 | [diff] [blame] | 2586 | } |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2587 | WARN_ON(!osds_valid(up) || !osds_valid(acting)); |
Sage Weil | d85b705 | 2010-05-10 10:24:48 -0700 | [diff] [blame] | 2588 | } |
| 2589 | |
Ilya Dryomov | dc98ff7 | 2017-06-15 16:30:53 +0200 | [diff] [blame] | 2590 | bool ceph_pg_to_primary_shard(struct ceph_osdmap *osdmap, |
Ilya Dryomov | df28152 | 2017-06-15 16:30:56 +0200 | [diff] [blame] | 2591 | struct ceph_pg_pool_info *pi, |
Ilya Dryomov | dc98ff7 | 2017-06-15 16:30:53 +0200 | [diff] [blame] | 2592 | const struct ceph_pg *raw_pgid, |
| 2593 | struct ceph_spg *spgid) |
| 2594 | { |
Ilya Dryomov | dc98ff7 | 2017-06-15 16:30:53 +0200 | [diff] [blame] | 2595 | struct ceph_pg pgid; |
| 2596 | struct ceph_osds up, acting; |
| 2597 | int i; |
| 2598 | |
Ilya Dryomov | df28152 | 2017-06-15 16:30:56 +0200 | [diff] [blame] | 2599 | WARN_ON(pi->id != raw_pgid->pool); |
Ilya Dryomov | dc98ff7 | 2017-06-15 16:30:53 +0200 | [diff] [blame] | 2600 | raw_pg_to_pg(pi, raw_pgid, &pgid); |
| 2601 | |
| 2602 | if (ceph_can_shift_osds(pi)) { |
| 2603 | spgid->pgid = pgid; /* struct */ |
| 2604 | spgid->shard = CEPH_SPG_NOSHARD; |
| 2605 | return true; |
| 2606 | } |
| 2607 | |
Ilya Dryomov | df28152 | 2017-06-15 16:30:56 +0200 | [diff] [blame] | 2608 | ceph_pg_to_up_acting_osds(osdmap, pi, &pgid, &up, &acting); |
Ilya Dryomov | dc98ff7 | 2017-06-15 16:30:53 +0200 | [diff] [blame] | 2609 | for (i = 0; i < acting.size; i++) { |
| 2610 | if (acting.osds[i] == acting.primary) { |
| 2611 | spgid->pgid = pgid; /* struct */ |
| 2612 | spgid->shard = i; |
| 2613 | return true; |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | return false; |
| 2618 | } |
| 2619 | |
Sage Weil | d85b705 | 2010-05-10 10:24:48 -0700 | [diff] [blame] | 2620 | /* |
Ilya Dryomov | f81f163 | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 2621 | * Return acting primary for given PG, or -1 if none. |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 2622 | */ |
Ilya Dryomov | f81f163 | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 2623 | int ceph_pg_to_acting_primary(struct ceph_osdmap *osdmap, |
| 2624 | const struct ceph_pg *raw_pgid) |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 2625 | { |
Ilya Dryomov | df28152 | 2017-06-15 16:30:56 +0200 | [diff] [blame] | 2626 | struct ceph_pg_pool_info *pi; |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2627 | struct ceph_osds up, acting; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 2628 | |
Ilya Dryomov | df28152 | 2017-06-15 16:30:56 +0200 | [diff] [blame] | 2629 | pi = ceph_pg_pool_by_id(osdmap, raw_pgid->pool); |
| 2630 | if (!pi) |
| 2631 | return -1; |
| 2632 | |
| 2633 | ceph_pg_to_up_acting_osds(osdmap, pi, raw_pgid, &up, &acting); |
Ilya Dryomov | 6f3bfd4 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2634 | return acting.primary; |
Sage Weil | f24e998 | 2009-10-06 11:31:10 -0700 | [diff] [blame] | 2635 | } |
Ilya Dryomov | f81f163 | 2016-04-28 16:07:23 +0200 | [diff] [blame] | 2636 | EXPORT_SYMBOL(ceph_pg_to_acting_primary); |