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