1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/ceph/ceph_debug.h> 4 5 #include <linux/module.h> 6 #include <linux/slab.h> 7 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> 13 14 static __printf(2, 3) 15 void osdmap_info(const struct ceph_osdmap *map, const char *fmt, ...) 16 { 17 struct va_format vaf; 18 va_list args; 19 20 va_start(args, fmt); 21 vaf.fmt = fmt; 22 vaf.va = &args; 23 24 printk(KERN_INFO "%s (%pU e%u): %pV", KBUILD_MODNAME, &map->fsid, 25 map->epoch, &vaf); 26 27 va_end(args); 28 } 29 30 char *ceph_osdmap_state_str(char *str, int len, u32 state) 31 { 32 if (!len) 33 return str; 34 35 if ((state & CEPH_OSD_EXISTS) && (state & CEPH_OSD_UP)) 36 snprintf(str, len, "exists, up"); 37 else if (state & CEPH_OSD_EXISTS) 38 snprintf(str, len, "exists"); 39 else if (state & CEPH_OSD_UP) 40 snprintf(str, len, "up"); 41 else 42 snprintf(str, len, "doesn't exist"); 43 44 return str; 45 } 46 47 /* maps */ 48 49 static int calc_bits_of(unsigned int t) 50 { 51 int b = 0; 52 while (t) { 53 t = t >> 1; 54 b++; 55 } 56 return b; 57 } 58 59 /* 60 * the foo_mask is the smallest value 2^n-1 that is >= foo. 61 */ 62 static void calc_pg_masks(struct ceph_pg_pool_info *pi) 63 { 64 pi->pg_num_mask = (1 << calc_bits_of(pi->pg_num-1)) - 1; 65 pi->pgp_num_mask = (1 << calc_bits_of(pi->pgp_num-1)) - 1; 66 } 67 68 /* 69 * decode crush map 70 */ 71 static int crush_decode_uniform_bucket(void **p, void *end, 72 struct crush_bucket_uniform *b) 73 { 74 dout("crush_decode_uniform_bucket %p to %p\n", *p, end); 75 ceph_decode_32_safe(p, end, b->item_weight, bad); 76 return 0; 77 bad: 78 return -EINVAL; 79 } 80 81 static int crush_decode_list_bucket(void **p, void *end, 82 struct crush_bucket_list *b) 83 { 84 int j; 85 dout("crush_decode_list_bucket %p to %p\n", *p, end); 86 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); 87 if (b->item_weights == NULL) 88 return -ENOMEM; 89 b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); 90 if (b->sum_weights == NULL) 91 return -ENOMEM; 92 ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad); 93 for (j = 0; j < b->h.size; j++) { 94 b->item_weights[j] = ceph_decode_32(p); 95 b->sum_weights[j] = ceph_decode_32(p); 96 } 97 return 0; 98 bad: 99 return -EINVAL; 100 } 101 102 static int crush_decode_tree_bucket(void **p, void *end, 103 struct crush_bucket_tree *b) 104 { 105 int j; 106 dout("crush_decode_tree_bucket %p to %p\n", *p, end); 107 ceph_decode_8_safe(p, end, b->num_nodes, bad); 108 b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS); 109 if (b->node_weights == NULL) 110 return -ENOMEM; 111 ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad); 112 for (j = 0; j < b->num_nodes; j++) 113 b->node_weights[j] = ceph_decode_32(p); 114 return 0; 115 bad: 116 return -EINVAL; 117 } 118 119 static int crush_decode_straw_bucket(void **p, void *end, 120 struct crush_bucket_straw *b) 121 { 122 int j; 123 dout("crush_decode_straw_bucket %p to %p\n", *p, end); 124 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); 125 if (b->item_weights == NULL) 126 return -ENOMEM; 127 b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); 128 if (b->straws == NULL) 129 return -ENOMEM; 130 ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad); 131 for (j = 0; j < b->h.size; j++) { 132 b->item_weights[j] = ceph_decode_32(p); 133 b->straws[j] = ceph_decode_32(p); 134 } 135 return 0; 136 bad: 137 return -EINVAL; 138 } 139 140 static int crush_decode_straw2_bucket(void **p, void *end, 141 struct crush_bucket_straw2 *b) 142 { 143 int j; 144 dout("crush_decode_straw2_bucket %p to %p\n", *p, end); 145 b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); 146 if (b->item_weights == NULL) 147 return -ENOMEM; 148 ceph_decode_need(p, end, b->h.size * sizeof(u32), bad); 149 for (j = 0; j < b->h.size; j++) 150 b->item_weights[j] = ceph_decode_32(p); 151 return 0; 152 bad: 153 return -EINVAL; 154 } 155 156 struct crush_name_node { 157 struct rb_node cn_node; 158 int cn_id; 159 char cn_name[]; 160 }; 161 162 static struct crush_name_node *alloc_crush_name(size_t name_len) 163 { 164 struct crush_name_node *cn; 165 166 cn = kmalloc(sizeof(*cn) + name_len + 1, GFP_NOIO); 167 if (!cn) 168 return NULL; 169 170 RB_CLEAR_NODE(&cn->cn_node); 171 return cn; 172 } 173 174 static void free_crush_name(struct crush_name_node *cn) 175 { 176 WARN_ON(!RB_EMPTY_NODE(&cn->cn_node)); 177 178 kfree(cn); 179 } 180 181 DEFINE_RB_FUNCS(crush_name, struct crush_name_node, cn_id, cn_node) 182 183 static int decode_crush_names(void **p, void *end, struct rb_root *root) 184 { 185 u32 n; 186 187 ceph_decode_32_safe(p, end, n, e_inval); 188 while (n--) { 189 struct crush_name_node *cn; 190 int id; 191 u32 name_len; 192 193 ceph_decode_32_safe(p, end, id, e_inval); 194 ceph_decode_32_safe(p, end, name_len, e_inval); 195 ceph_decode_need(p, end, name_len, e_inval); 196 197 cn = alloc_crush_name(name_len); 198 if (!cn) 199 return -ENOMEM; 200 201 cn->cn_id = id; 202 memcpy(cn->cn_name, *p, name_len); 203 cn->cn_name[name_len] = '\0'; 204 *p += name_len; 205 206 if (!__insert_crush_name(root, cn)) { 207 free_crush_name(cn); 208 return -EEXIST; 209 } 210 } 211 212 return 0; 213 214 e_inval: 215 return -EINVAL; 216 } 217 218 void clear_crush_names(struct rb_root *root) 219 { 220 while (!RB_EMPTY_ROOT(root)) { 221 struct crush_name_node *cn = 222 rb_entry(rb_first(root), struct crush_name_node, cn_node); 223 224 erase_crush_name(root, cn); 225 free_crush_name(cn); 226 } 227 } 228 229 static struct crush_choose_arg_map *alloc_choose_arg_map(void) 230 { 231 struct crush_choose_arg_map *arg_map; 232 233 arg_map = kzalloc_obj(*arg_map, GFP_NOIO); 234 if (!arg_map) 235 return NULL; 236 237 RB_CLEAR_NODE(&arg_map->node); 238 return arg_map; 239 } 240 241 static void free_choose_arg_map(struct crush_choose_arg_map *arg_map) 242 { 243 int i, j; 244 245 if (!arg_map) 246 return; 247 248 WARN_ON(!RB_EMPTY_NODE(&arg_map->node)); 249 250 if (arg_map->args) { 251 for (i = 0; i < arg_map->size; i++) { 252 struct crush_choose_arg *arg = &arg_map->args[i]; 253 if (arg->weight_set) { 254 for (j = 0; j < arg->weight_set_size; j++) 255 kfree(arg->weight_set[j].weights); 256 kfree(arg->weight_set); 257 } 258 kfree(arg->ids); 259 } 260 kfree(arg_map->args); 261 } 262 kfree(arg_map); 263 } 264 265 DEFINE_RB_FUNCS(choose_arg_map, struct crush_choose_arg_map, choose_args_index, 266 node); 267 268 void clear_choose_args(struct crush_map *c) 269 { 270 while (!RB_EMPTY_ROOT(&c->choose_args)) { 271 struct crush_choose_arg_map *arg_map = 272 rb_entry(rb_first(&c->choose_args), 273 struct crush_choose_arg_map, node); 274 275 erase_choose_arg_map(&c->choose_args, arg_map); 276 free_choose_arg_map(arg_map); 277 } 278 } 279 280 static u32 *decode_array_32_alloc(void **p, void *end, u32 *plen) 281 { 282 u32 *a = NULL; 283 u32 len; 284 int ret; 285 286 ceph_decode_32_safe(p, end, len, e_inval); 287 if (len) { 288 u32 i; 289 290 a = kmalloc_objs(u32, len, GFP_NOIO); 291 if (!a) { 292 ret = -ENOMEM; 293 goto fail; 294 } 295 296 ceph_decode_need(p, end, len * sizeof(u32), e_inval); 297 for (i = 0; i < len; i++) 298 a[i] = ceph_decode_32(p); 299 } 300 301 *plen = len; 302 return a; 303 304 e_inval: 305 ret = -EINVAL; 306 fail: 307 kfree(a); 308 return ERR_PTR(ret); 309 } 310 311 /* 312 * Assumes @arg is zero-initialized. 313 */ 314 static int decode_choose_arg(void **p, void *end, struct crush_choose_arg *arg) 315 { 316 int ret; 317 318 ceph_decode_32_safe(p, end, arg->weight_set_size, e_inval); 319 if (arg->weight_set_size) { 320 u32 i; 321 322 arg->weight_set = kzalloc_objs(*arg->weight_set, 323 arg->weight_set_size, GFP_NOIO); 324 if (!arg->weight_set) 325 return -ENOMEM; 326 327 for (i = 0; i < arg->weight_set_size; i++) { 328 struct crush_weight_set *w = &arg->weight_set[i]; 329 330 w->weights = decode_array_32_alloc(p, end, &w->size); 331 if (IS_ERR(w->weights)) { 332 ret = PTR_ERR(w->weights); 333 w->weights = NULL; 334 return ret; 335 } 336 } 337 } 338 339 arg->ids = decode_array_32_alloc(p, end, &arg->ids_size); 340 if (IS_ERR(arg->ids)) { 341 ret = PTR_ERR(arg->ids); 342 arg->ids = NULL; 343 return ret; 344 } 345 346 return 0; 347 348 e_inval: 349 return -EINVAL; 350 } 351 352 static int decode_choose_args(void **p, void *end, struct crush_map *c) 353 { 354 struct crush_choose_arg_map *arg_map = NULL; 355 u32 num_choose_arg_maps, num_buckets; 356 int ret; 357 358 ceph_decode_32_safe(p, end, num_choose_arg_maps, e_inval); 359 while (num_choose_arg_maps--) { 360 arg_map = alloc_choose_arg_map(); 361 if (!arg_map) { 362 ret = -ENOMEM; 363 goto fail; 364 } 365 366 ceph_decode_64_safe(p, end, arg_map->choose_args_index, 367 e_inval); 368 arg_map->size = c->max_buckets; 369 arg_map->args = kzalloc_objs(*arg_map->args, arg_map->size, 370 GFP_NOIO); 371 if (!arg_map->args) { 372 ret = -ENOMEM; 373 goto fail; 374 } 375 376 ceph_decode_32_safe(p, end, num_buckets, e_inval); 377 while (num_buckets--) { 378 struct crush_choose_arg *arg; 379 struct crush_bucket *bucket; 380 u32 bucket_index; 381 u32 i; 382 383 ceph_decode_32_safe(p, end, bucket_index, e_inval); 384 if (bucket_index >= arg_map->size) 385 goto e_inval; 386 387 arg = &arg_map->args[bucket_index]; 388 if (arg->ids || arg->weight_set) 389 goto e_inval; 390 ret = decode_choose_arg(p, end, arg); 391 if (ret) 392 goto fail; 393 394 bucket = c->buckets[bucket_index]; 395 if (arg->ids_size && 396 (!bucket || arg->ids_size != bucket->size)) 397 goto e_inval; 398 399 if (arg->weight_set_size && !bucket) 400 goto e_inval; 401 402 for (i = 0; i < arg->weight_set_size; i++) { 403 if (arg->weight_set[i].size != bucket->size) 404 goto e_inval; 405 } 406 } 407 408 if (!__insert_choose_arg_map(&c->choose_args, arg_map)) { 409 ret = -EEXIST; 410 goto fail; 411 } 412 } 413 414 return 0; 415 416 e_inval: 417 ret = -EINVAL; 418 fail: 419 free_choose_arg_map(arg_map); 420 return ret; 421 } 422 423 static void crush_finalize(struct crush_map *c) 424 { 425 __s32 b; 426 427 /* Space for the array of pointers to per-bucket workspace */ 428 c->working_size = sizeof(struct crush_work) + 429 c->max_buckets * sizeof(struct crush_work_bucket *); 430 431 for (b = 0; b < c->max_buckets; b++) { 432 if (!c->buckets[b]) 433 continue; 434 435 switch (c->buckets[b]->alg) { 436 default: 437 /* 438 * The base case, permutation variables and 439 * the pointer to the permutation array. 440 */ 441 c->working_size += sizeof(struct crush_work_bucket); 442 break; 443 } 444 /* Every bucket has a permutation array. */ 445 c->working_size += c->buckets[b]->size * sizeof(__u32); 446 } 447 } 448 449 static struct crush_map *crush_decode(void *pbyval, void *end) 450 { 451 struct crush_map *c; 452 int err; 453 int i, j; 454 void **p = &pbyval; 455 void *start = pbyval; 456 u32 magic; 457 458 dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p)); 459 460 c = kzalloc_obj(*c, GFP_NOFS); 461 if (c == NULL) 462 return ERR_PTR(-ENOMEM); 463 464 c->type_names = RB_ROOT; 465 c->names = RB_ROOT; 466 c->choose_args = RB_ROOT; 467 468 /* set tunables to default values */ 469 c->choose_local_tries = 2; 470 c->choose_local_fallback_tries = 5; 471 c->choose_total_tries = 19; 472 c->chooseleaf_descend_once = 0; 473 474 ceph_decode_need(p, end, 4*sizeof(u32), bad); 475 magic = ceph_decode_32(p); 476 if (magic != CRUSH_MAGIC) { 477 pr_err("crush_decode magic %x != current %x\n", 478 (unsigned int)magic, (unsigned int)CRUSH_MAGIC); 479 goto bad; 480 } 481 c->max_buckets = ceph_decode_32(p); 482 c->max_rules = ceph_decode_32(p); 483 c->max_devices = ceph_decode_32(p); 484 485 c->buckets = kzalloc_objs(*c->buckets, c->max_buckets, GFP_NOFS); 486 if (c->buckets == NULL) 487 goto badmem; 488 c->rules = kzalloc_objs(*c->rules, c->max_rules, GFP_NOFS); 489 if (c->rules == NULL) 490 goto badmem; 491 492 /* buckets */ 493 for (i = 0; i < c->max_buckets; i++) { 494 int size = 0; 495 u32 alg; 496 struct crush_bucket *b; 497 498 ceph_decode_32_safe(p, end, alg, bad); 499 if (alg == 0) { 500 c->buckets[i] = NULL; 501 continue; 502 } 503 dout("crush_decode bucket %d off %x %p to %p\n", 504 i, (int)(*p-start), *p, end); 505 506 switch (alg) { 507 case CRUSH_BUCKET_UNIFORM: 508 size = sizeof(struct crush_bucket_uniform); 509 break; 510 case CRUSH_BUCKET_LIST: 511 size = sizeof(struct crush_bucket_list); 512 break; 513 case CRUSH_BUCKET_TREE: 514 size = sizeof(struct crush_bucket_tree); 515 break; 516 case CRUSH_BUCKET_STRAW: 517 size = sizeof(struct crush_bucket_straw); 518 break; 519 case CRUSH_BUCKET_STRAW2: 520 size = sizeof(struct crush_bucket_straw2); 521 break; 522 default: 523 goto bad; 524 } 525 BUG_ON(size == 0); 526 b = c->buckets[i] = kzalloc(size, GFP_NOFS); 527 if (b == NULL) 528 goto badmem; 529 530 ceph_decode_need(p, end, 4*sizeof(u32), bad); 531 b->id = ceph_decode_32(p); 532 if (b->id != -1 - i) 533 goto bad; 534 b->type = ceph_decode_16(p); 535 if (b->type == 0) 536 goto bad; 537 b->alg = ceph_decode_8(p); 538 if (b->alg != alg) { 539 b->alg = 0; 540 goto bad; 541 } 542 b->hash = ceph_decode_8(p); 543 b->weight = ceph_decode_32(p); 544 b->size = ceph_decode_32(p); 545 546 dout("crush_decode bucket size %d off %x %p to %p\n", 547 b->size, (int)(*p-start), *p, end); 548 549 b->items = kzalloc_objs(__s32, b->size, GFP_NOFS); 550 if (b->items == NULL) 551 goto badmem; 552 553 ceph_decode_need(p, end, b->size*sizeof(u32), bad); 554 for (j = 0; j < b->size; j++) 555 b->items[j] = ceph_decode_32(p); 556 557 switch (b->alg) { 558 case CRUSH_BUCKET_UNIFORM: 559 err = crush_decode_uniform_bucket(p, end, 560 (struct crush_bucket_uniform *)b); 561 if (err < 0) 562 goto fail; 563 break; 564 case CRUSH_BUCKET_LIST: 565 err = crush_decode_list_bucket(p, end, 566 (struct crush_bucket_list *)b); 567 if (err < 0) 568 goto fail; 569 break; 570 case CRUSH_BUCKET_TREE: 571 err = crush_decode_tree_bucket(p, end, 572 (struct crush_bucket_tree *)b); 573 if (err < 0) 574 goto fail; 575 break; 576 case CRUSH_BUCKET_STRAW: 577 err = crush_decode_straw_bucket(p, end, 578 (struct crush_bucket_straw *)b); 579 if (err < 0) 580 goto fail; 581 break; 582 case CRUSH_BUCKET_STRAW2: 583 err = crush_decode_straw2_bucket(p, end, 584 (struct crush_bucket_straw2 *)b); 585 if (err < 0) 586 goto fail; 587 break; 588 } 589 } 590 591 /* rules */ 592 dout("rule vec is %p\n", c->rules); 593 for (i = 0; i < c->max_rules; i++) { 594 u32 yes; 595 struct crush_rule *r; 596 597 ceph_decode_32_safe(p, end, yes, bad); 598 if (!yes) { 599 dout("crush_decode NO rule %d off %x %p to %p\n", 600 i, (int)(*p-start), *p, end); 601 c->rules[i] = NULL; 602 continue; 603 } 604 605 dout("crush_decode rule %d off %x %p to %p\n", 606 i, (int)(*p-start), *p, end); 607 608 /* len */ 609 ceph_decode_32_safe(p, end, yes, bad); 610 #if BITS_PER_LONG == 32 611 if (yes > (ULONG_MAX - sizeof(*r)) 612 / sizeof(struct crush_rule_step)) 613 goto bad; 614 #endif 615 r = kmalloc_flex(*r, steps, yes, GFP_NOFS); 616 if (r == NULL) 617 goto badmem; 618 dout(" rule %d is at %p\n", i, r); 619 c->rules[i] = r; 620 r->len = yes; 621 ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */ 622 ceph_decode_need(p, end, r->len*3*sizeof(u32), bad); 623 for (j = 0; j < r->len; j++) { 624 r->steps[j].op = ceph_decode_32(p); 625 r->steps[j].arg1 = ceph_decode_32(p); 626 r->steps[j].arg2 = ceph_decode_32(p); 627 } 628 } 629 630 err = decode_crush_names(p, end, &c->type_names); 631 if (err) 632 goto fail; 633 634 err = decode_crush_names(p, end, &c->names); 635 if (err) 636 goto fail; 637 638 ceph_decode_skip_map(p, end, 32, string, bad); /* rule_name_map */ 639 640 /* tunables */ 641 ceph_decode_need(p, end, 3*sizeof(u32), done); 642 c->choose_local_tries = ceph_decode_32(p); 643 c->choose_local_fallback_tries = ceph_decode_32(p); 644 c->choose_total_tries = ceph_decode_32(p); 645 dout("crush decode tunable choose_local_tries = %d\n", 646 c->choose_local_tries); 647 dout("crush decode tunable choose_local_fallback_tries = %d\n", 648 c->choose_local_fallback_tries); 649 dout("crush decode tunable choose_total_tries = %d\n", 650 c->choose_total_tries); 651 652 ceph_decode_need(p, end, sizeof(u32), done); 653 c->chooseleaf_descend_once = ceph_decode_32(p); 654 dout("crush decode tunable chooseleaf_descend_once = %d\n", 655 c->chooseleaf_descend_once); 656 657 ceph_decode_need(p, end, sizeof(u8), done); 658 c->chooseleaf_vary_r = ceph_decode_8(p); 659 dout("crush decode tunable chooseleaf_vary_r = %d\n", 660 c->chooseleaf_vary_r); 661 662 /* skip straw_calc_version, allowed_bucket_algs */ 663 ceph_decode_need(p, end, sizeof(u8) + sizeof(u32), done); 664 *p += sizeof(u8) + sizeof(u32); 665 666 ceph_decode_need(p, end, sizeof(u8), done); 667 c->chooseleaf_stable = ceph_decode_8(p); 668 dout("crush decode tunable chooseleaf_stable = %d\n", 669 c->chooseleaf_stable); 670 671 if (*p != end) { 672 /* class_map */ 673 ceph_decode_skip_map(p, end, 32, 32, bad); 674 /* class_name */ 675 ceph_decode_skip_map(p, end, 32, string, bad); 676 /* class_bucket */ 677 ceph_decode_skip_map_of_map(p, end, 32, 32, 32, bad); 678 } 679 680 if (*p != end) { 681 err = decode_choose_args(p, end, c); 682 if (err) 683 goto fail; 684 } 685 686 done: 687 crush_finalize(c); 688 dout("crush_decode success\n"); 689 return c; 690 691 badmem: 692 err = -ENOMEM; 693 fail: 694 dout("crush_decode fail %d\n", err); 695 crush_destroy(c); 696 return ERR_PTR(err); 697 698 bad: 699 err = -EINVAL; 700 goto fail; 701 } 702 703 int ceph_pg_compare(const struct ceph_pg *lhs, const struct ceph_pg *rhs) 704 { 705 if (lhs->pool < rhs->pool) 706 return -1; 707 if (lhs->pool > rhs->pool) 708 return 1; 709 if (lhs->seed < rhs->seed) 710 return -1; 711 if (lhs->seed > rhs->seed) 712 return 1; 713 714 return 0; 715 } 716 717 int ceph_spg_compare(const struct ceph_spg *lhs, const struct ceph_spg *rhs) 718 { 719 int ret; 720 721 ret = ceph_pg_compare(&lhs->pgid, &rhs->pgid); 722 if (ret) 723 return ret; 724 725 if (lhs->shard < rhs->shard) 726 return -1; 727 if (lhs->shard > rhs->shard) 728 return 1; 729 730 return 0; 731 } 732 733 static struct ceph_pg_mapping *alloc_pg_mapping(size_t payload_len) 734 { 735 struct ceph_pg_mapping *pg; 736 737 pg = kmalloc(sizeof(*pg) + payload_len, GFP_NOIO); 738 if (!pg) 739 return NULL; 740 741 RB_CLEAR_NODE(&pg->node); 742 return pg; 743 } 744 745 static void free_pg_mapping(struct ceph_pg_mapping *pg) 746 { 747 WARN_ON(!RB_EMPTY_NODE(&pg->node)); 748 749 kfree(pg); 750 } 751 752 /* 753 * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid 754 * to a set of osds) and primary_temp (explicit primary setting) 755 */ 756 DEFINE_RB_FUNCS2(pg_mapping, struct ceph_pg_mapping, pgid, ceph_pg_compare, 757 RB_BYPTR, const struct ceph_pg *, node) 758 759 /* 760 * rbtree of pg pool info 761 */ 762 DEFINE_RB_FUNCS(pg_pool, struct ceph_pg_pool_info, id, node) 763 764 struct ceph_pg_pool_info *ceph_pg_pool_by_id(struct ceph_osdmap *map, u64 id) 765 { 766 return lookup_pg_pool(&map->pg_pools, id); 767 } 768 769 const char *ceph_pg_pool_name_by_id(struct ceph_osdmap *map, u64 id) 770 { 771 struct ceph_pg_pool_info *pi; 772 773 if (id == CEPH_NOPOOL) 774 return NULL; 775 776 if (WARN_ON_ONCE(id > (u64) INT_MAX)) 777 return NULL; 778 779 pi = lookup_pg_pool(&map->pg_pools, id); 780 return pi ? pi->name : NULL; 781 } 782 EXPORT_SYMBOL(ceph_pg_pool_name_by_id); 783 784 int ceph_pg_poolid_by_name(struct ceph_osdmap *map, const char *name) 785 { 786 struct rb_node *rbp; 787 788 for (rbp = rb_first(&map->pg_pools); rbp; rbp = rb_next(rbp)) { 789 struct ceph_pg_pool_info *pi = 790 rb_entry(rbp, struct ceph_pg_pool_info, node); 791 if (pi->name && strcmp(pi->name, name) == 0) 792 return pi->id; 793 } 794 return -ENOENT; 795 } 796 EXPORT_SYMBOL(ceph_pg_poolid_by_name); 797 798 u64 ceph_pg_pool_flags(struct ceph_osdmap *map, u64 id) 799 { 800 struct ceph_pg_pool_info *pi; 801 802 pi = lookup_pg_pool(&map->pg_pools, id); 803 return pi ? pi->flags : 0; 804 } 805 EXPORT_SYMBOL(ceph_pg_pool_flags); 806 807 static void __remove_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *pi) 808 { 809 erase_pg_pool(root, pi); 810 kfree(pi->name); 811 kfree(pi); 812 } 813 814 static int decode_pool(void **p, void *end, struct ceph_pg_pool_info *pi) 815 { 816 u8 ev, cv; 817 unsigned len, num; 818 void *pool_end; 819 820 ceph_decode_need(p, end, 2 + 4, bad); 821 ev = ceph_decode_8(p); /* encoding version */ 822 cv = ceph_decode_8(p); /* compat version */ 823 if (ev < 5) { 824 pr_warn("got v %d < 5 cv %d of ceph_pg_pool\n", ev, cv); 825 return -EINVAL; 826 } 827 if (cv > 9) { 828 pr_warn("got v %d cv %d > 9 of ceph_pg_pool\n", ev, cv); 829 return -EINVAL; 830 } 831 len = ceph_decode_32(p); 832 ceph_decode_need(p, end, len, bad); 833 pool_end = *p + len; 834 835 ceph_decode_need(p, end, 4 + 4 + 4, bad); 836 pi->type = ceph_decode_8(p); 837 pi->size = ceph_decode_8(p); 838 pi->crush_ruleset = ceph_decode_8(p); 839 pi->object_hash = ceph_decode_8(p); 840 pi->pg_num = ceph_decode_32(p); 841 pi->pgp_num = ceph_decode_32(p); 842 843 /* lpg*, last_change, snap_seq, snap_epoch */ 844 ceph_decode_skip_n(p, end, 8 + 4 + 8 + 4, bad); 845 846 /* skip snaps */ 847 ceph_decode_32_safe(p, end, num, bad); 848 while (num--) { 849 /* snapid key, pool snap (with versions) */ 850 ceph_decode_skip_n(p, end, 8 + 2, bad); 851 ceph_decode_skip_string(p, end, bad); 852 } 853 854 /* removed_snaps */ 855 ceph_decode_skip_map(p, end, 64, 64, bad); 856 857 ceph_decode_need(p, end, 8 + 8 + 4, bad); 858 *p += 8; /* skip auid */ 859 pi->flags = ceph_decode_64(p); 860 *p += 4; /* skip crash_replay_interval */ 861 862 if (ev >= 7) 863 ceph_decode_8_safe(p, end, pi->min_size, bad); 864 else 865 pi->min_size = pi->size - pi->size / 2; 866 867 if (ev >= 8) 868 /* quota_max_* */ 869 ceph_decode_skip_n(p, end, 8 + 8, bad); 870 871 if (ev >= 9) { 872 /* tiers */ 873 ceph_decode_skip_set(p, end, 64, bad); 874 875 ceph_decode_need(p, end, 8 + 1 + 8 + 8, bad); 876 *p += 8; /* skip tier_of */ 877 *p += 1; /* skip cache_mode */ 878 pi->read_tier = ceph_decode_64(p); 879 pi->write_tier = ceph_decode_64(p); 880 } else { 881 pi->read_tier = -1; 882 pi->write_tier = -1; 883 } 884 885 if (ev >= 10) 886 /* properties */ 887 ceph_decode_skip_map(p, end, string, string, bad); 888 889 if (ev >= 11) { 890 /* hit_set_params (with versions) */ 891 ceph_decode_skip_n(p, end, 2, bad); 892 ceph_decode_skip_string(p, end, bad); 893 894 /* hit_set_period, hit_set_count */ 895 ceph_decode_skip_n(p, end, 4 + 4, bad); 896 } 897 898 if (ev >= 12) 899 /* stripe_width */ 900 ceph_decode_skip_32(p, end, bad); 901 902 if (ev >= 13) 903 /* target_max_*, cache_target_*, cache_min_* */ 904 ceph_decode_skip_n(p, end, 16 + 8 + 8, bad); 905 906 if (ev >= 14) 907 /* erasure_code_profile */ 908 ceph_decode_skip_string(p, end, bad); 909 910 /* 911 * last_force_op_resend_preluminous, will be overridden if the 912 * map was encoded with RESEND_ON_SPLIT 913 */ 914 if (ev >= 15) 915 ceph_decode_32_safe(p, end, pi->last_force_request_resend, bad); 916 else 917 pi->last_force_request_resend = 0; 918 919 if (ev >= 16) 920 /* min_read_recency_for_promote */ 921 ceph_decode_skip_32(p, end, bad); 922 923 if (ev >= 17) 924 /* expected_num_objects */ 925 ceph_decode_skip_64(p, end, bad); 926 927 if (ev >= 19) 928 /* cache_target_dirty_high_ratio_micro */ 929 ceph_decode_skip_32(p, end, bad); 930 931 if (ev >= 20) 932 /* min_write_recency_for_promote */ 933 ceph_decode_skip_32(p, end, bad); 934 935 if (ev >= 21) 936 /* use_gmt_hitset */ 937 ceph_decode_skip_8(p, end, bad); 938 939 if (ev >= 22) 940 /* fast_read */ 941 ceph_decode_skip_8(p, end, bad); 942 943 if (ev >= 23) 944 /* hit_set_grade_decay_rate, hit_set_search_last_n */ 945 ceph_decode_skip_n(p, end, 4 + 4, bad); 946 947 if (ev >= 24) { 948 /* opts (with versions) */ 949 ceph_decode_skip_n(p, end, 2, bad); 950 ceph_decode_skip_string(p, end, bad); 951 } 952 953 if (ev >= 25) 954 ceph_decode_32_safe(p, end, pi->last_force_request_resend, bad); 955 956 /* ignore the rest */ 957 958 *p = pool_end; 959 calc_pg_masks(pi); 960 return 0; 961 962 bad: 963 return -EINVAL; 964 } 965 966 static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map) 967 { 968 struct ceph_pg_pool_info *pi; 969 u32 num, len; 970 u64 pool; 971 972 ceph_decode_32_safe(p, end, num, bad); 973 dout(" %d pool names\n", num); 974 while (num--) { 975 ceph_decode_64_safe(p, end, pool, bad); 976 ceph_decode_32_safe(p, end, len, bad); 977 dout(" pool %llu len %d\n", pool, len); 978 ceph_decode_need(p, end, len, bad); 979 pi = lookup_pg_pool(&map->pg_pools, pool); 980 if (pi) { 981 char *name = kstrndup(*p, len, GFP_NOFS); 982 983 if (!name) 984 return -ENOMEM; 985 kfree(pi->name); 986 pi->name = name; 987 dout(" name is %s\n", pi->name); 988 } 989 *p += len; 990 } 991 return 0; 992 993 bad: 994 return -EINVAL; 995 } 996 997 /* 998 * CRUSH workspaces 999 * 1000 * workspace_manager framework borrowed from fs/btrfs/compression.c. 1001 * Two simplifications: there is only one type of workspace and there 1002 * is always at least one workspace. 1003 */ 1004 static struct crush_work *alloc_workspace(const struct crush_map *c) 1005 { 1006 struct crush_work *work; 1007 size_t work_size; 1008 1009 WARN_ON(!c->working_size); 1010 work_size = crush_work_size(c, CEPH_PG_MAX_SIZE); 1011 dout("%s work_size %zu bytes\n", __func__, work_size); 1012 1013 work = kvmalloc(work_size, GFP_NOIO); 1014 if (!work) 1015 return NULL; 1016 1017 INIT_LIST_HEAD(&work->item); 1018 crush_init_workspace(c, work); 1019 return work; 1020 } 1021 1022 static void free_workspace(struct crush_work *work) 1023 { 1024 WARN_ON(!list_empty(&work->item)); 1025 kvfree(work); 1026 } 1027 1028 static void init_workspace_manager(struct workspace_manager *wsm) 1029 { 1030 INIT_LIST_HEAD(&wsm->idle_ws); 1031 spin_lock_init(&wsm->ws_lock); 1032 atomic_set(&wsm->total_ws, 0); 1033 wsm->free_ws = 0; 1034 init_waitqueue_head(&wsm->ws_wait); 1035 } 1036 1037 static void add_initial_workspace(struct workspace_manager *wsm, 1038 struct crush_work *work) 1039 { 1040 WARN_ON(!list_empty(&wsm->idle_ws)); 1041 1042 list_add(&work->item, &wsm->idle_ws); 1043 atomic_set(&wsm->total_ws, 1); 1044 wsm->free_ws = 1; 1045 } 1046 1047 static void cleanup_workspace_manager(struct workspace_manager *wsm) 1048 { 1049 struct crush_work *work; 1050 1051 while (!list_empty(&wsm->idle_ws)) { 1052 work = list_first_entry(&wsm->idle_ws, struct crush_work, 1053 item); 1054 list_del_init(&work->item); 1055 free_workspace(work); 1056 } 1057 atomic_set(&wsm->total_ws, 0); 1058 wsm->free_ws = 0; 1059 } 1060 1061 /* 1062 * Finds an available workspace or allocates a new one. If it's not 1063 * possible to allocate a new one, waits until there is one. 1064 */ 1065 static struct crush_work *get_workspace(struct workspace_manager *wsm, 1066 const struct crush_map *c) 1067 { 1068 struct crush_work *work; 1069 int cpus = num_online_cpus(); 1070 1071 again: 1072 spin_lock(&wsm->ws_lock); 1073 if (!list_empty(&wsm->idle_ws)) { 1074 work = list_first_entry(&wsm->idle_ws, struct crush_work, 1075 item); 1076 list_del_init(&work->item); 1077 wsm->free_ws--; 1078 spin_unlock(&wsm->ws_lock); 1079 return work; 1080 1081 } 1082 if (atomic_read(&wsm->total_ws) > cpus) { 1083 DEFINE_WAIT(wait); 1084 1085 spin_unlock(&wsm->ws_lock); 1086 prepare_to_wait(&wsm->ws_wait, &wait, TASK_UNINTERRUPTIBLE); 1087 if (atomic_read(&wsm->total_ws) > cpus && !wsm->free_ws) 1088 schedule(); 1089 finish_wait(&wsm->ws_wait, &wait); 1090 goto again; 1091 } 1092 atomic_inc(&wsm->total_ws); 1093 spin_unlock(&wsm->ws_lock); 1094 1095 work = alloc_workspace(c); 1096 if (!work) { 1097 atomic_dec(&wsm->total_ws); 1098 wake_up(&wsm->ws_wait); 1099 1100 /* 1101 * Do not return the error but go back to waiting. We 1102 * have the initial workspace and the CRUSH computation 1103 * time is bounded so we will get it eventually. 1104 */ 1105 WARN_ON(atomic_read(&wsm->total_ws) < 1); 1106 goto again; 1107 } 1108 return work; 1109 } 1110 1111 /* 1112 * Puts a workspace back on the list or frees it if we have enough 1113 * idle ones sitting around. 1114 */ 1115 static void put_workspace(struct workspace_manager *wsm, 1116 struct crush_work *work) 1117 { 1118 spin_lock(&wsm->ws_lock); 1119 if (wsm->free_ws <= num_online_cpus()) { 1120 list_add(&work->item, &wsm->idle_ws); 1121 wsm->free_ws++; 1122 spin_unlock(&wsm->ws_lock); 1123 goto wake; 1124 } 1125 spin_unlock(&wsm->ws_lock); 1126 1127 free_workspace(work); 1128 atomic_dec(&wsm->total_ws); 1129 wake: 1130 if (wq_has_sleeper(&wsm->ws_wait)) 1131 wake_up(&wsm->ws_wait); 1132 } 1133 1134 /* 1135 * osd map 1136 */ 1137 struct ceph_osdmap *ceph_osdmap_alloc(void) 1138 { 1139 struct ceph_osdmap *map; 1140 1141 map = kzalloc_obj(*map, GFP_NOIO); 1142 if (!map) 1143 return NULL; 1144 1145 map->pg_pools = RB_ROOT; 1146 map->pool_max = -1; 1147 map->pg_temp = RB_ROOT; 1148 map->primary_temp = RB_ROOT; 1149 map->pg_upmap = RB_ROOT; 1150 map->pg_upmap_items = RB_ROOT; 1151 1152 init_workspace_manager(&map->crush_wsm); 1153 1154 return map; 1155 } 1156 1157 void ceph_osdmap_destroy(struct ceph_osdmap *map) 1158 { 1159 dout("osdmap_destroy %p\n", map); 1160 1161 if (map->crush) 1162 crush_destroy(map->crush); 1163 cleanup_workspace_manager(&map->crush_wsm); 1164 1165 while (!RB_EMPTY_ROOT(&map->pg_temp)) { 1166 struct ceph_pg_mapping *pg = 1167 rb_entry(rb_first(&map->pg_temp), 1168 struct ceph_pg_mapping, node); 1169 erase_pg_mapping(&map->pg_temp, pg); 1170 free_pg_mapping(pg); 1171 } 1172 while (!RB_EMPTY_ROOT(&map->primary_temp)) { 1173 struct ceph_pg_mapping *pg = 1174 rb_entry(rb_first(&map->primary_temp), 1175 struct ceph_pg_mapping, node); 1176 erase_pg_mapping(&map->primary_temp, pg); 1177 free_pg_mapping(pg); 1178 } 1179 while (!RB_EMPTY_ROOT(&map->pg_upmap)) { 1180 struct ceph_pg_mapping *pg = 1181 rb_entry(rb_first(&map->pg_upmap), 1182 struct ceph_pg_mapping, node); 1183 rb_erase(&pg->node, &map->pg_upmap); 1184 kfree(pg); 1185 } 1186 while (!RB_EMPTY_ROOT(&map->pg_upmap_items)) { 1187 struct ceph_pg_mapping *pg = 1188 rb_entry(rb_first(&map->pg_upmap_items), 1189 struct ceph_pg_mapping, node); 1190 rb_erase(&pg->node, &map->pg_upmap_items); 1191 kfree(pg); 1192 } 1193 while (!RB_EMPTY_ROOT(&map->pg_pools)) { 1194 struct ceph_pg_pool_info *pi = 1195 rb_entry(rb_first(&map->pg_pools), 1196 struct ceph_pg_pool_info, node); 1197 __remove_pg_pool(&map->pg_pools, pi); 1198 } 1199 kvfree(map->osd_state); 1200 kvfree(map->osd_weight); 1201 kvfree(map->osd_addr); 1202 kvfree(map->osd_primary_affinity); 1203 kfree(map); 1204 } 1205 1206 /* 1207 * Adjust max_osd value, (re)allocate arrays. 1208 * 1209 * The new elements are properly initialized. 1210 */ 1211 static int osdmap_set_max_osd(struct ceph_osdmap *map, u32 max) 1212 { 1213 u32 *state; 1214 u32 *weight; 1215 struct ceph_entity_addr *addr; 1216 u32 to_copy; 1217 int i; 1218 1219 dout("%s old %u new %u\n", __func__, map->max_osd, max); 1220 if (max == map->max_osd) 1221 return 0; 1222 1223 state = kvmalloc(array_size(max, sizeof(*state)), GFP_NOFS); 1224 weight = kvmalloc(array_size(max, sizeof(*weight)), GFP_NOFS); 1225 addr = kvmalloc(array_size(max, sizeof(*addr)), GFP_NOFS); 1226 if (!state || !weight || !addr) { 1227 kvfree(state); 1228 kvfree(weight); 1229 kvfree(addr); 1230 return -ENOMEM; 1231 } 1232 1233 to_copy = min(map->max_osd, max); 1234 if (map->osd_state) { 1235 memcpy(state, map->osd_state, to_copy * sizeof(*state)); 1236 memcpy(weight, map->osd_weight, to_copy * sizeof(*weight)); 1237 memcpy(addr, map->osd_addr, to_copy * sizeof(*addr)); 1238 kvfree(map->osd_state); 1239 kvfree(map->osd_weight); 1240 kvfree(map->osd_addr); 1241 } 1242 1243 map->osd_state = state; 1244 map->osd_weight = weight; 1245 map->osd_addr = addr; 1246 for (i = map->max_osd; i < max; i++) { 1247 map->osd_state[i] = 0; 1248 map->osd_weight[i] = CEPH_OSD_OUT; 1249 memset(map->osd_addr + i, 0, sizeof(*map->osd_addr)); 1250 } 1251 1252 if (map->osd_primary_affinity) { 1253 u32 *affinity; 1254 1255 affinity = kvmalloc(array_size(max, sizeof(*affinity)), 1256 GFP_NOFS); 1257 if (!affinity) 1258 return -ENOMEM; 1259 1260 memcpy(affinity, map->osd_primary_affinity, 1261 to_copy * sizeof(*affinity)); 1262 kvfree(map->osd_primary_affinity); 1263 1264 map->osd_primary_affinity = affinity; 1265 for (i = map->max_osd; i < max; i++) 1266 map->osd_primary_affinity[i] = 1267 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; 1268 } 1269 1270 map->max_osd = max; 1271 1272 return 0; 1273 } 1274 1275 static int osdmap_set_crush(struct ceph_osdmap *map, struct crush_map *crush) 1276 { 1277 struct crush_work *work; 1278 1279 if (IS_ERR(crush)) 1280 return PTR_ERR(crush); 1281 1282 work = alloc_workspace(crush); 1283 if (!work) { 1284 crush_destroy(crush); 1285 return -ENOMEM; 1286 } 1287 1288 if (map->crush) 1289 crush_destroy(map->crush); 1290 cleanup_workspace_manager(&map->crush_wsm); 1291 map->crush = crush; 1292 add_initial_workspace(&map->crush_wsm, work); 1293 return 0; 1294 } 1295 1296 #define OSDMAP_WRAPPER_COMPAT_VER 7 1297 #define OSDMAP_CLIENT_DATA_COMPAT_VER 1 1298 1299 /* 1300 * Return 0 or error. On success, *v is set to 0 for old (v6) osdmaps, 1301 * to struct_v of the client_data section for new (v7 and above) 1302 * osdmaps. 1303 */ 1304 static int get_osdmap_client_data_v(void **p, void *end, 1305 const char *prefix, u8 *v) 1306 { 1307 u8 struct_v; 1308 1309 ceph_decode_8_safe(p, end, struct_v, e_inval); 1310 if (struct_v >= 7) { 1311 u8 struct_compat; 1312 1313 ceph_decode_8_safe(p, end, struct_compat, e_inval); 1314 if (struct_compat > OSDMAP_WRAPPER_COMPAT_VER) { 1315 pr_warn("got v %d cv %d > %d of %s ceph_osdmap\n", 1316 struct_v, struct_compat, 1317 OSDMAP_WRAPPER_COMPAT_VER, prefix); 1318 return -EINVAL; 1319 } 1320 *p += 4; /* ignore wrapper struct_len */ 1321 1322 ceph_decode_8_safe(p, end, struct_v, e_inval); 1323 ceph_decode_8_safe(p, end, struct_compat, e_inval); 1324 if (struct_compat > OSDMAP_CLIENT_DATA_COMPAT_VER) { 1325 pr_warn("got v %d cv %d > %d of %s ceph_osdmap client data\n", 1326 struct_v, struct_compat, 1327 OSDMAP_CLIENT_DATA_COMPAT_VER, prefix); 1328 return -EINVAL; 1329 } 1330 *p += 4; /* ignore client data struct_len */ 1331 } else { 1332 u16 version; 1333 1334 *p -= 1; 1335 ceph_decode_16_safe(p, end, version, e_inval); 1336 if (version < 6) { 1337 pr_warn("got v %d < 6 of %s ceph_osdmap\n", 1338 version, prefix); 1339 return -EINVAL; 1340 } 1341 1342 /* old osdmap encoding */ 1343 struct_v = 0; 1344 } 1345 1346 *v = struct_v; 1347 return 0; 1348 1349 e_inval: 1350 return -EINVAL; 1351 } 1352 1353 static int __decode_pools(void **p, void *end, struct ceph_osdmap *map, 1354 bool incremental) 1355 { 1356 u32 n; 1357 1358 ceph_decode_32_safe(p, end, n, e_inval); 1359 while (n--) { 1360 struct ceph_pg_pool_info *pi; 1361 u64 pool; 1362 int ret; 1363 1364 ceph_decode_64_safe(p, end, pool, e_inval); 1365 1366 pi = lookup_pg_pool(&map->pg_pools, pool); 1367 if (!incremental || !pi) { 1368 pi = kzalloc_obj(*pi, GFP_NOFS); 1369 if (!pi) 1370 return -ENOMEM; 1371 1372 RB_CLEAR_NODE(&pi->node); 1373 pi->id = pool; 1374 1375 if (!__insert_pg_pool(&map->pg_pools, pi)) { 1376 kfree(pi); 1377 return -EEXIST; 1378 } 1379 } 1380 1381 ret = decode_pool(p, end, pi); 1382 if (ret) 1383 return ret; 1384 } 1385 1386 return 0; 1387 1388 e_inval: 1389 return -EINVAL; 1390 } 1391 1392 static int decode_pools(void **p, void *end, struct ceph_osdmap *map) 1393 { 1394 return __decode_pools(p, end, map, false); 1395 } 1396 1397 static int decode_new_pools(void **p, void *end, struct ceph_osdmap *map) 1398 { 1399 return __decode_pools(p, end, map, true); 1400 } 1401 1402 typedef struct ceph_pg_mapping *(*decode_mapping_fn_t)(void **, void *, bool); 1403 1404 static int decode_pg_mapping(void **p, void *end, struct rb_root *mapping_root, 1405 decode_mapping_fn_t fn, bool incremental) 1406 { 1407 u32 n; 1408 1409 WARN_ON(!incremental && !fn); 1410 1411 ceph_decode_32_safe(p, end, n, e_inval); 1412 while (n--) { 1413 struct ceph_pg_mapping *pg; 1414 struct ceph_pg pgid; 1415 int ret; 1416 1417 ret = ceph_decode_pgid(p, end, &pgid); 1418 if (ret) 1419 return ret; 1420 1421 pg = lookup_pg_mapping(mapping_root, &pgid); 1422 if (pg) { 1423 WARN_ON(!incremental); 1424 erase_pg_mapping(mapping_root, pg); 1425 free_pg_mapping(pg); 1426 } 1427 1428 if (fn) { 1429 pg = fn(p, end, incremental); 1430 if (IS_ERR(pg)) 1431 return PTR_ERR(pg); 1432 1433 if (pg) { 1434 pg->pgid = pgid; /* struct */ 1435 insert_pg_mapping(mapping_root, pg); 1436 } 1437 } 1438 } 1439 1440 return 0; 1441 1442 e_inval: 1443 return -EINVAL; 1444 } 1445 1446 static struct ceph_pg_mapping *__decode_pg_temp(void **p, void *end, 1447 bool incremental) 1448 { 1449 struct ceph_pg_mapping *pg; 1450 u32 len, i; 1451 1452 ceph_decode_32_safe(p, end, len, e_inval); 1453 if (len == 0 && incremental) 1454 return NULL; /* new_pg_temp: [] to remove */ 1455 if (len > CEPH_PG_MAX_SIZE) 1456 return ERR_PTR(-EINVAL); 1457 1458 ceph_decode_need(p, end, len * sizeof(u32), e_inval); 1459 pg = alloc_pg_mapping(len * sizeof(u32)); 1460 if (!pg) 1461 return ERR_PTR(-ENOMEM); 1462 1463 pg->pg_temp.len = len; 1464 for (i = 0; i < len; i++) 1465 pg->pg_temp.osds[i] = ceph_decode_32(p); 1466 1467 return pg; 1468 1469 e_inval: 1470 return ERR_PTR(-EINVAL); 1471 } 1472 1473 static int decode_pg_temp(void **p, void *end, struct ceph_osdmap *map) 1474 { 1475 return decode_pg_mapping(p, end, &map->pg_temp, __decode_pg_temp, 1476 false); 1477 } 1478 1479 static int decode_new_pg_temp(void **p, void *end, struct ceph_osdmap *map) 1480 { 1481 return decode_pg_mapping(p, end, &map->pg_temp, __decode_pg_temp, 1482 true); 1483 } 1484 1485 static struct ceph_pg_mapping *__decode_primary_temp(void **p, void *end, 1486 bool incremental) 1487 { 1488 struct ceph_pg_mapping *pg; 1489 u32 osd; 1490 1491 ceph_decode_32_safe(p, end, osd, e_inval); 1492 if (osd == (u32)-1 && incremental) 1493 return NULL; /* new_primary_temp: -1 to remove */ 1494 1495 pg = alloc_pg_mapping(0); 1496 if (!pg) 1497 return ERR_PTR(-ENOMEM); 1498 1499 pg->primary_temp.osd = osd; 1500 return pg; 1501 1502 e_inval: 1503 return ERR_PTR(-EINVAL); 1504 } 1505 1506 static int decode_primary_temp(void **p, void *end, struct ceph_osdmap *map) 1507 { 1508 return decode_pg_mapping(p, end, &map->primary_temp, 1509 __decode_primary_temp, false); 1510 } 1511 1512 static int decode_new_primary_temp(void **p, void *end, 1513 struct ceph_osdmap *map) 1514 { 1515 return decode_pg_mapping(p, end, &map->primary_temp, 1516 __decode_primary_temp, true); 1517 } 1518 1519 u32 ceph_get_primary_affinity(struct ceph_osdmap *map, int osd) 1520 { 1521 if (!map->osd_primary_affinity) 1522 return CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; 1523 1524 return map->osd_primary_affinity[osd]; 1525 } 1526 1527 static int set_primary_affinity(struct ceph_osdmap *map, int osd, u32 aff) 1528 { 1529 if (!map->osd_primary_affinity) { 1530 int i; 1531 1532 map->osd_primary_affinity = kvmalloc( 1533 array_size(map->max_osd, sizeof(*map->osd_primary_affinity)), 1534 GFP_NOFS); 1535 if (!map->osd_primary_affinity) 1536 return -ENOMEM; 1537 1538 for (i = 0; i < map->max_osd; i++) 1539 map->osd_primary_affinity[i] = 1540 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY; 1541 } 1542 1543 map->osd_primary_affinity[osd] = aff; 1544 1545 return 0; 1546 } 1547 1548 static int decode_primary_affinity(void **p, void *end, 1549 struct ceph_osdmap *map) 1550 { 1551 u32 len, i; 1552 1553 ceph_decode_32_safe(p, end, len, e_inval); 1554 if (len == 0) { 1555 kvfree(map->osd_primary_affinity); 1556 map->osd_primary_affinity = NULL; 1557 return 0; 1558 } 1559 if (len != map->max_osd) 1560 goto e_inval; 1561 1562 ceph_decode_need(p, end, map->max_osd*sizeof(u32), e_inval); 1563 1564 for (i = 0; i < map->max_osd; i++) { 1565 int ret; 1566 1567 ret = set_primary_affinity(map, i, ceph_decode_32(p)); 1568 if (ret) 1569 return ret; 1570 } 1571 1572 return 0; 1573 1574 e_inval: 1575 return -EINVAL; 1576 } 1577 1578 static int decode_new_primary_affinity(void **p, void *end, 1579 struct ceph_osdmap *map) 1580 { 1581 u32 n; 1582 1583 ceph_decode_32_safe(p, end, n, e_inval); 1584 while (n--) { 1585 u32 osd, aff; 1586 int ret; 1587 1588 ceph_decode_32_safe(p, end, osd, e_inval); 1589 ceph_decode_32_safe(p, end, aff, e_inval); 1590 if (osd >= map->max_osd) 1591 goto e_inval; 1592 1593 ret = set_primary_affinity(map, osd, aff); 1594 if (ret) 1595 return ret; 1596 1597 osdmap_info(map, "osd%d primary-affinity 0x%x\n", osd, aff); 1598 } 1599 1600 return 0; 1601 1602 e_inval: 1603 return -EINVAL; 1604 } 1605 1606 static struct ceph_pg_mapping *__decode_pg_upmap(void **p, void *end, 1607 bool __unused) 1608 { 1609 return __decode_pg_temp(p, end, false); 1610 } 1611 1612 static int decode_pg_upmap(void **p, void *end, struct ceph_osdmap *map) 1613 { 1614 return decode_pg_mapping(p, end, &map->pg_upmap, __decode_pg_upmap, 1615 false); 1616 } 1617 1618 static int decode_new_pg_upmap(void **p, void *end, struct ceph_osdmap *map) 1619 { 1620 return decode_pg_mapping(p, end, &map->pg_upmap, __decode_pg_upmap, 1621 true); 1622 } 1623 1624 static int decode_old_pg_upmap(void **p, void *end, struct ceph_osdmap *map) 1625 { 1626 return decode_pg_mapping(p, end, &map->pg_upmap, NULL, true); 1627 } 1628 1629 static struct ceph_pg_mapping *__decode_pg_upmap_items(void **p, void *end, 1630 bool __unused) 1631 { 1632 struct ceph_pg_mapping *pg; 1633 u32 len, i; 1634 1635 ceph_decode_32_safe(p, end, len, e_inval); 1636 if (len > CEPH_PG_MAX_SIZE) 1637 return ERR_PTR(-EINVAL); 1638 1639 ceph_decode_need(p, end, 2 * len * sizeof(u32), e_inval); 1640 pg = alloc_pg_mapping(2 * len * sizeof(u32)); 1641 if (!pg) 1642 return ERR_PTR(-ENOMEM); 1643 1644 pg->pg_upmap_items.len = len; 1645 for (i = 0; i < len; i++) { 1646 pg->pg_upmap_items.from_to[i][0] = ceph_decode_32(p); 1647 pg->pg_upmap_items.from_to[i][1] = ceph_decode_32(p); 1648 } 1649 1650 return pg; 1651 1652 e_inval: 1653 return ERR_PTR(-EINVAL); 1654 } 1655 1656 static int decode_pg_upmap_items(void **p, void *end, struct ceph_osdmap *map) 1657 { 1658 return decode_pg_mapping(p, end, &map->pg_upmap_items, 1659 __decode_pg_upmap_items, false); 1660 } 1661 1662 static int decode_new_pg_upmap_items(void **p, void *end, 1663 struct ceph_osdmap *map) 1664 { 1665 return decode_pg_mapping(p, end, &map->pg_upmap_items, 1666 __decode_pg_upmap_items, true); 1667 } 1668 1669 static int decode_old_pg_upmap_items(void **p, void *end, 1670 struct ceph_osdmap *map) 1671 { 1672 return decode_pg_mapping(p, end, &map->pg_upmap_items, NULL, true); 1673 } 1674 1675 /* 1676 * decode a full map. 1677 */ 1678 static int osdmap_decode(void **p, void *end, bool msgr2, 1679 struct ceph_osdmap *map) 1680 { 1681 u8 struct_v; 1682 u32 epoch = 0; 1683 void *start = *p; 1684 u32 max; 1685 u32 len, i; 1686 int err; 1687 1688 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p)); 1689 1690 err = get_osdmap_client_data_v(p, end, "full", &struct_v); 1691 if (err) 1692 goto bad; 1693 1694 /* fsid, epoch, created, modified */ 1695 ceph_decode_need(p, end, sizeof(map->fsid) + sizeof(u32) + 1696 sizeof(map->created) + sizeof(map->modified), e_inval); 1697 ceph_decode_copy(p, &map->fsid, sizeof(map->fsid)); 1698 epoch = map->epoch = ceph_decode_32(p); 1699 ceph_decode_copy(p, &map->created, sizeof(map->created)); 1700 ceph_decode_copy(p, &map->modified, sizeof(map->modified)); 1701 1702 /* pools */ 1703 err = decode_pools(p, end, map); 1704 if (err) 1705 goto bad; 1706 1707 /* pool_name */ 1708 err = decode_pool_names(p, end, map); 1709 if (err) 1710 goto bad; 1711 1712 ceph_decode_32_safe(p, end, map->pool_max, e_inval); 1713 1714 ceph_decode_32_safe(p, end, map->flags, e_inval); 1715 1716 /* max_osd */ 1717 ceph_decode_32_safe(p, end, max, e_inval); 1718 1719 /* (re)alloc osd arrays */ 1720 err = osdmap_set_max_osd(map, max); 1721 if (err) 1722 goto bad; 1723 1724 /* osd_state, osd_weight, osd_addrs->client_addr */ 1725 ceph_decode_need(p, end, 3*sizeof(u32) + 1726 map->max_osd*(struct_v >= 5 ? sizeof(u32) : 1727 sizeof(u8)) + 1728 map->max_osd*sizeof(*map->osd_weight), e_inval); 1729 if (ceph_decode_32(p) != map->max_osd) 1730 goto e_inval; 1731 1732 if (struct_v >= 5) { 1733 for (i = 0; i < map->max_osd; i++) 1734 map->osd_state[i] = ceph_decode_32(p); 1735 } else { 1736 for (i = 0; i < map->max_osd; i++) 1737 map->osd_state[i] = ceph_decode_8(p); 1738 } 1739 1740 if (ceph_decode_32(p) != map->max_osd) 1741 goto e_inval; 1742 1743 for (i = 0; i < map->max_osd; i++) 1744 map->osd_weight[i] = ceph_decode_32(p); 1745 1746 if (ceph_decode_32(p) != map->max_osd) 1747 goto e_inval; 1748 1749 for (i = 0; i < map->max_osd; i++) { 1750 struct ceph_entity_addr *addr = &map->osd_addr[i]; 1751 1752 if (struct_v >= 8) 1753 err = ceph_decode_entity_addrvec(p, end, msgr2, addr); 1754 else 1755 err = ceph_decode_entity_addr(p, end, addr); 1756 if (err) 1757 goto bad; 1758 1759 dout("%s osd%d addr %s\n", __func__, i, ceph_pr_addr(addr)); 1760 } 1761 1762 /* pg_temp */ 1763 err = decode_pg_temp(p, end, map); 1764 if (err) 1765 goto bad; 1766 1767 /* primary_temp */ 1768 if (struct_v >= 1) { 1769 err = decode_primary_temp(p, end, map); 1770 if (err) 1771 goto bad; 1772 } 1773 1774 /* primary_affinity */ 1775 if (struct_v >= 2) { 1776 err = decode_primary_affinity(p, end, map); 1777 if (err) 1778 goto bad; 1779 } else { 1780 WARN_ON(map->osd_primary_affinity); 1781 } 1782 1783 /* crush */ 1784 ceph_decode_32_safe(p, end, len, e_inval); 1785 err = osdmap_set_crush(map, crush_decode(*p, min(*p + len, end))); 1786 if (err) 1787 goto bad; 1788 1789 *p += len; 1790 if (struct_v >= 3) { 1791 /* erasure_code_profiles */ 1792 ceph_decode_skip_map_of_map(p, end, string, string, string, 1793 e_inval); 1794 } 1795 1796 if (struct_v >= 4) { 1797 err = decode_pg_upmap(p, end, map); 1798 if (err) 1799 goto bad; 1800 1801 err = decode_pg_upmap_items(p, end, map); 1802 if (err) 1803 goto bad; 1804 } else { 1805 WARN_ON(!RB_EMPTY_ROOT(&map->pg_upmap)); 1806 WARN_ON(!RB_EMPTY_ROOT(&map->pg_upmap_items)); 1807 } 1808 1809 /* ignore the rest */ 1810 *p = end; 1811 1812 dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd); 1813 return 0; 1814 1815 e_inval: 1816 err = -EINVAL; 1817 bad: 1818 pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n", 1819 err, epoch, (int)(*p - start), *p, start, end); 1820 print_hex_dump(KERN_DEBUG, "osdmap: ", 1821 DUMP_PREFIX_OFFSET, 16, 1, 1822 start, end - start, true); 1823 return err; 1824 } 1825 1826 /* 1827 * Allocate and decode a full map. 1828 */ 1829 struct ceph_osdmap *ceph_osdmap_decode(void **p, void *end, bool msgr2) 1830 { 1831 struct ceph_osdmap *map; 1832 int ret; 1833 1834 map = ceph_osdmap_alloc(); 1835 if (!map) 1836 return ERR_PTR(-ENOMEM); 1837 1838 ret = osdmap_decode(p, end, msgr2, map); 1839 if (ret) { 1840 ceph_osdmap_destroy(map); 1841 return ERR_PTR(ret); 1842 } 1843 1844 return map; 1845 } 1846 1847 /* 1848 * Encoding order is (new_up_client, new_state, new_weight). Need to 1849 * apply in the (new_weight, new_state, new_up_client) order, because 1850 * an incremental map may look like e.g. 1851 * 1852 * new_up_client: { osd=6, addr=... } # set osd_state and addr 1853 * new_state: { osd=6, xorstate=EXISTS } # clear osd_state 1854 */ 1855 static int decode_new_up_state_weight(void **p, void *end, u8 struct_v, 1856 bool msgr2, struct ceph_osdmap *map) 1857 { 1858 void *new_up_client; 1859 void *new_state; 1860 void *new_weight_end; 1861 const u32 new_state_item_size = 1862 sizeof(u32) + (struct_v >= 5 ? sizeof(u32) : sizeof(u8)); 1863 u32 len; 1864 int ret; 1865 int i; 1866 1867 new_up_client = *p; 1868 ceph_decode_32_safe(p, end, len, e_inval); 1869 for (i = 0; i < len; ++i) { 1870 struct ceph_entity_addr addr; 1871 1872 ceph_decode_skip_32(p, end, e_inval); 1873 if (struct_v >= 7) 1874 ret = ceph_decode_entity_addrvec(p, end, msgr2, &addr); 1875 else 1876 ret = ceph_decode_entity_addr(p, end, &addr); 1877 if (ret) 1878 return ret; 1879 } 1880 1881 new_state = *p; 1882 ceph_decode_32_safe(p, end, len, e_inval); 1883 if (check_mul_overflow(len, new_state_item_size, &len)) 1884 goto e_inval; 1885 ceph_decode_need(p, end, len, e_inval); 1886 *p += len; 1887 1888 /* new_weight */ 1889 ceph_decode_32_safe(p, end, len, e_inval); 1890 while (len--) { 1891 s32 osd; 1892 u32 w; 1893 1894 ceph_decode_need(p, end, 2*sizeof(u32), e_inval); 1895 osd = ceph_decode_32(p); 1896 w = ceph_decode_32(p); 1897 if (osd >= map->max_osd) 1898 goto e_inval; 1899 1900 osdmap_info(map, "osd%d weight 0x%x %s\n", osd, w, 1901 w == CEPH_OSD_IN ? "(in)" : 1902 (w == CEPH_OSD_OUT ? "(out)" : "")); 1903 map->osd_weight[osd] = w; 1904 1905 /* 1906 * If we are marking in, set the EXISTS, and clear the 1907 * AUTOOUT and NEW bits. 1908 */ 1909 if (w) { 1910 map->osd_state[osd] |= CEPH_OSD_EXISTS; 1911 map->osd_state[osd] &= ~(CEPH_OSD_AUTOOUT | 1912 CEPH_OSD_NEW); 1913 } 1914 } 1915 new_weight_end = *p; 1916 1917 /* new_state (up/down) */ 1918 *p = new_state; 1919 len = ceph_decode_32(p); 1920 while (len--) { 1921 s32 osd; 1922 u32 xorstate; 1923 1924 osd = ceph_decode_32(p); 1925 if (osd >= map->max_osd) 1926 goto e_inval; 1927 1928 if (struct_v >= 5) 1929 xorstate = ceph_decode_32(p); 1930 else 1931 xorstate = ceph_decode_8(p); 1932 if (xorstate == 0) 1933 xorstate = CEPH_OSD_UP; 1934 if ((map->osd_state[osd] & CEPH_OSD_UP) && 1935 (xorstate & CEPH_OSD_UP)) 1936 osdmap_info(map, "osd%d down\n", osd); 1937 if ((map->osd_state[osd] & CEPH_OSD_EXISTS) && 1938 (xorstate & CEPH_OSD_EXISTS)) { 1939 osdmap_info(map, "osd%d does not exist\n", osd); 1940 ret = set_primary_affinity(map, osd, 1941 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY); 1942 if (ret) 1943 return ret; 1944 memset(map->osd_addr + osd, 0, sizeof(*map->osd_addr)); 1945 map->osd_state[osd] = 0; 1946 } else { 1947 map->osd_state[osd] ^= xorstate; 1948 } 1949 } 1950 1951 /* new_up_client */ 1952 *p = new_up_client; 1953 len = ceph_decode_32(p); 1954 while (len--) { 1955 s32 osd; 1956 struct ceph_entity_addr addr; 1957 1958 osd = ceph_decode_32(p); 1959 if (osd >= map->max_osd) 1960 goto e_inval; 1961 1962 if (struct_v >= 7) 1963 ret = ceph_decode_entity_addrvec(p, end, msgr2, &addr); 1964 else 1965 ret = ceph_decode_entity_addr(p, end, &addr); 1966 if (ret) 1967 return ret; 1968 1969 dout("%s osd%d addr %s\n", __func__, osd, ceph_pr_addr(&addr)); 1970 1971 osdmap_info(map, "osd%d up\n", osd); 1972 map->osd_state[osd] |= CEPH_OSD_EXISTS | CEPH_OSD_UP; 1973 map->osd_addr[osd] = addr; 1974 } 1975 1976 *p = new_weight_end; 1977 return 0; 1978 1979 e_inval: 1980 return -EINVAL; 1981 } 1982 1983 /* 1984 * decode and apply an incremental map update. 1985 */ 1986 struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end, bool msgr2, 1987 struct ceph_osdmap *map) 1988 { 1989 struct ceph_fsid fsid; 1990 u32 epoch = 0; 1991 struct ceph_timespec modified; 1992 s32 len; 1993 u64 pool; 1994 __s64 new_pool_max; 1995 __s32 new_flags, max; 1996 void *start = *p; 1997 int err; 1998 u8 struct_v; 1999 2000 dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p)); 2001 2002 err = get_osdmap_client_data_v(p, end, "inc", &struct_v); 2003 if (err) 2004 goto bad; 2005 2006 /* fsid, epoch, modified, new_pool_max, new_flags */ 2007 ceph_decode_need(p, end, sizeof(fsid) + sizeof(u32) + sizeof(modified) + 2008 sizeof(u64) + sizeof(u32), e_inval); 2009 ceph_decode_copy(p, &fsid, sizeof(fsid)); 2010 epoch = ceph_decode_32(p); 2011 ceph_decode_copy(p, &modified, sizeof(modified)); 2012 new_pool_max = ceph_decode_64(p); 2013 new_flags = ceph_decode_32(p); 2014 2015 if (epoch != map->epoch + 1) 2016 goto e_inval; 2017 2018 /* full map? */ 2019 ceph_decode_32_safe(p, end, len, e_inval); 2020 if (len > 0) { 2021 dout("apply_incremental full map len %d, %p to %p\n", 2022 len, *p, end); 2023 return ceph_osdmap_decode(p, min(*p+len, end), msgr2); 2024 } 2025 2026 /* new crush? */ 2027 ceph_decode_32_safe(p, end, len, e_inval); 2028 if (len > 0) { 2029 err = osdmap_set_crush(map, 2030 crush_decode(*p, min(*p + len, end))); 2031 if (err) 2032 goto bad; 2033 *p += len; 2034 } 2035 2036 /* new flags? */ 2037 if (new_flags >= 0) 2038 map->flags = new_flags; 2039 if (new_pool_max >= 0) 2040 map->pool_max = new_pool_max; 2041 2042 /* new max? */ 2043 ceph_decode_32_safe(p, end, max, e_inval); 2044 if (max >= 0) { 2045 err = osdmap_set_max_osd(map, max); 2046 if (err) 2047 goto bad; 2048 } 2049 2050 map->epoch++; 2051 map->modified = modified; 2052 2053 /* new_pools */ 2054 err = decode_new_pools(p, end, map); 2055 if (err) 2056 goto bad; 2057 2058 /* new_pool_names */ 2059 err = decode_pool_names(p, end, map); 2060 if (err) 2061 goto bad; 2062 2063 /* old_pool */ 2064 ceph_decode_32_safe(p, end, len, e_inval); 2065 while (len--) { 2066 struct ceph_pg_pool_info *pi; 2067 2068 ceph_decode_64_safe(p, end, pool, e_inval); 2069 pi = lookup_pg_pool(&map->pg_pools, pool); 2070 if (pi) 2071 __remove_pg_pool(&map->pg_pools, pi); 2072 } 2073 2074 /* new_up_client, new_state, new_weight */ 2075 err = decode_new_up_state_weight(p, end, struct_v, msgr2, map); 2076 if (err) 2077 goto bad; 2078 2079 /* new_pg_temp */ 2080 err = decode_new_pg_temp(p, end, map); 2081 if (err) 2082 goto bad; 2083 2084 /* new_primary_temp */ 2085 if (struct_v >= 1) { 2086 err = decode_new_primary_temp(p, end, map); 2087 if (err) 2088 goto bad; 2089 } 2090 2091 /* new_primary_affinity */ 2092 if (struct_v >= 2) { 2093 err = decode_new_primary_affinity(p, end, map); 2094 if (err) 2095 goto bad; 2096 } 2097 2098 if (struct_v >= 3) { 2099 /* new_erasure_code_profiles */ 2100 ceph_decode_skip_map_of_map(p, end, string, string, string, 2101 e_inval); 2102 /* old_erasure_code_profiles */ 2103 ceph_decode_skip_set(p, end, string, e_inval); 2104 } 2105 2106 if (struct_v >= 4) { 2107 err = decode_new_pg_upmap(p, end, map); 2108 if (err) 2109 goto bad; 2110 2111 err = decode_old_pg_upmap(p, end, map); 2112 if (err) 2113 goto bad; 2114 2115 err = decode_new_pg_upmap_items(p, end, map); 2116 if (err) 2117 goto bad; 2118 2119 err = decode_old_pg_upmap_items(p, end, map); 2120 if (err) 2121 goto bad; 2122 } 2123 2124 /* ignore the rest */ 2125 *p = end; 2126 2127 dout("inc osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd); 2128 return map; 2129 2130 e_inval: 2131 err = -EINVAL; 2132 bad: 2133 pr_err("corrupt inc osdmap (%d) epoch %d off %d (%p of %p-%p)\n", 2134 err, epoch, (int)(*p - start), *p, start, end); 2135 print_hex_dump(KERN_DEBUG, "osdmap: ", 2136 DUMP_PREFIX_OFFSET, 16, 1, 2137 start, end - start, true); 2138 return ERR_PTR(err); 2139 } 2140 2141 void ceph_oloc_copy(struct ceph_object_locator *dest, 2142 const struct ceph_object_locator *src) 2143 { 2144 ceph_oloc_destroy(dest); 2145 2146 dest->pool = src->pool; 2147 if (src->pool_ns) 2148 dest->pool_ns = ceph_get_string(src->pool_ns); 2149 else 2150 dest->pool_ns = NULL; 2151 } 2152 EXPORT_SYMBOL(ceph_oloc_copy); 2153 2154 void ceph_oloc_destroy(struct ceph_object_locator *oloc) 2155 { 2156 ceph_put_string(oloc->pool_ns); 2157 } 2158 EXPORT_SYMBOL(ceph_oloc_destroy); 2159 2160 void ceph_oid_copy(struct ceph_object_id *dest, 2161 const struct ceph_object_id *src) 2162 { 2163 ceph_oid_destroy(dest); 2164 2165 if (src->name != src->inline_name) { 2166 /* very rare, see ceph_object_id definition */ 2167 dest->name = kmalloc(src->name_len + 1, 2168 GFP_NOIO | __GFP_NOFAIL); 2169 } else { 2170 dest->name = dest->inline_name; 2171 } 2172 memcpy(dest->name, src->name, src->name_len + 1); 2173 dest->name_len = src->name_len; 2174 } 2175 EXPORT_SYMBOL(ceph_oid_copy); 2176 2177 static __printf(2, 0) 2178 int oid_printf_vargs(struct ceph_object_id *oid, const char *fmt, va_list ap) 2179 { 2180 int len; 2181 2182 WARN_ON(!ceph_oid_empty(oid)); 2183 2184 len = vsnprintf(oid->inline_name, sizeof(oid->inline_name), fmt, ap); 2185 if (len >= sizeof(oid->inline_name)) 2186 return len; 2187 2188 oid->name_len = len; 2189 return 0; 2190 } 2191 2192 /* 2193 * If oid doesn't fit into inline buffer, BUG. 2194 */ 2195 void ceph_oid_printf(struct ceph_object_id *oid, const char *fmt, ...) 2196 { 2197 va_list ap; 2198 2199 va_start(ap, fmt); 2200 BUG_ON(oid_printf_vargs(oid, fmt, ap)); 2201 va_end(ap); 2202 } 2203 EXPORT_SYMBOL(ceph_oid_printf); 2204 2205 static __printf(3, 0) 2206 int oid_aprintf_vargs(struct ceph_object_id *oid, gfp_t gfp, 2207 const char *fmt, va_list ap) 2208 { 2209 va_list aq; 2210 int len; 2211 2212 va_copy(aq, ap); 2213 len = oid_printf_vargs(oid, fmt, aq); 2214 va_end(aq); 2215 2216 if (len) { 2217 char *external_name; 2218 2219 external_name = kmalloc(len + 1, gfp); 2220 if (!external_name) 2221 return -ENOMEM; 2222 2223 oid->name = external_name; 2224 WARN_ON(vsnprintf(oid->name, len + 1, fmt, ap) != len); 2225 oid->name_len = len; 2226 } 2227 2228 return 0; 2229 } 2230 2231 /* 2232 * If oid doesn't fit into inline buffer, allocate. 2233 */ 2234 int ceph_oid_aprintf(struct ceph_object_id *oid, gfp_t gfp, 2235 const char *fmt, ...) 2236 { 2237 va_list ap; 2238 int ret; 2239 2240 va_start(ap, fmt); 2241 ret = oid_aprintf_vargs(oid, gfp, fmt, ap); 2242 va_end(ap); 2243 2244 return ret; 2245 } 2246 EXPORT_SYMBOL(ceph_oid_aprintf); 2247 2248 void ceph_oid_destroy(struct ceph_object_id *oid) 2249 { 2250 if (oid->name != oid->inline_name) 2251 kfree(oid->name); 2252 } 2253 EXPORT_SYMBOL(ceph_oid_destroy); 2254 2255 /* 2256 * osds only 2257 */ 2258 static bool __osds_equal(const struct ceph_osds *lhs, 2259 const struct ceph_osds *rhs) 2260 { 2261 if (lhs->size == rhs->size && 2262 !memcmp(lhs->osds, rhs->osds, rhs->size * sizeof(rhs->osds[0]))) 2263 return true; 2264 2265 return false; 2266 } 2267 2268 /* 2269 * osds + primary 2270 */ 2271 static bool osds_equal(const struct ceph_osds *lhs, 2272 const struct ceph_osds *rhs) 2273 { 2274 if (__osds_equal(lhs, rhs) && 2275 lhs->primary == rhs->primary) 2276 return true; 2277 2278 return false; 2279 } 2280 2281 static bool osds_valid(const struct ceph_osds *set) 2282 { 2283 /* non-empty set */ 2284 if (set->size > 0 && set->primary >= 0) 2285 return true; 2286 2287 /* empty can_shift_osds set */ 2288 if (!set->size && set->primary == -1) 2289 return true; 2290 2291 /* empty !can_shift_osds set - all NONE */ 2292 if (set->size > 0 && set->primary == -1) { 2293 int i; 2294 2295 for (i = 0; i < set->size; i++) { 2296 if (set->osds[i] != CRUSH_ITEM_NONE) 2297 break; 2298 } 2299 if (i == set->size) 2300 return true; 2301 } 2302 2303 return false; 2304 } 2305 2306 void ceph_osds_copy(struct ceph_osds *dest, const struct ceph_osds *src) 2307 { 2308 memcpy(dest->osds, src->osds, src->size * sizeof(src->osds[0])); 2309 dest->size = src->size; 2310 dest->primary = src->primary; 2311 } 2312 2313 bool ceph_pg_is_split(const struct ceph_pg *pgid, u32 old_pg_num, 2314 u32 new_pg_num) 2315 { 2316 int old_bits = calc_bits_of(old_pg_num); 2317 int old_mask = (1 << old_bits) - 1; 2318 int n; 2319 2320 WARN_ON(pgid->seed >= old_pg_num); 2321 if (new_pg_num <= old_pg_num) 2322 return false; 2323 2324 for (n = 1; ; n++) { 2325 int next_bit = n << (old_bits - 1); 2326 u32 s = next_bit | pgid->seed; 2327 2328 if (s < old_pg_num || s == pgid->seed) 2329 continue; 2330 if (s >= new_pg_num) 2331 break; 2332 2333 s = ceph_stable_mod(s, old_pg_num, old_mask); 2334 if (s == pgid->seed) 2335 return true; 2336 } 2337 2338 return false; 2339 } 2340 2341 bool ceph_is_new_interval(const struct ceph_osds *old_acting, 2342 const struct ceph_osds *new_acting, 2343 const struct ceph_osds *old_up, 2344 const struct ceph_osds *new_up, 2345 int old_size, 2346 int new_size, 2347 int old_min_size, 2348 int new_min_size, 2349 u32 old_pg_num, 2350 u32 new_pg_num, 2351 bool old_sort_bitwise, 2352 bool new_sort_bitwise, 2353 bool old_recovery_deletes, 2354 bool new_recovery_deletes, 2355 const struct ceph_pg *pgid) 2356 { 2357 return !osds_equal(old_acting, new_acting) || 2358 !osds_equal(old_up, new_up) || 2359 old_size != new_size || 2360 old_min_size != new_min_size || 2361 ceph_pg_is_split(pgid, old_pg_num, new_pg_num) || 2362 old_sort_bitwise != new_sort_bitwise || 2363 old_recovery_deletes != new_recovery_deletes; 2364 } 2365 2366 static int calc_pg_rank(int osd, const struct ceph_osds *acting) 2367 { 2368 int i; 2369 2370 for (i = 0; i < acting->size; i++) { 2371 if (acting->osds[i] == osd) 2372 return i; 2373 } 2374 2375 return -1; 2376 } 2377 2378 static bool primary_changed(const struct ceph_osds *old_acting, 2379 const struct ceph_osds *new_acting) 2380 { 2381 if (!old_acting->size && !new_acting->size) 2382 return false; /* both still empty */ 2383 2384 if (!old_acting->size ^ !new_acting->size) 2385 return true; /* was empty, now not, or vice versa */ 2386 2387 if (old_acting->primary != new_acting->primary) 2388 return true; /* primary changed */ 2389 2390 if (calc_pg_rank(old_acting->primary, old_acting) != 2391 calc_pg_rank(new_acting->primary, new_acting)) 2392 return true; 2393 2394 return false; /* same primary (tho replicas may have changed) */ 2395 } 2396 2397 bool ceph_osds_changed(const struct ceph_osds *old_acting, 2398 const struct ceph_osds *new_acting, 2399 bool any_change) 2400 { 2401 if (primary_changed(old_acting, new_acting)) 2402 return true; 2403 2404 if (any_change && !__osds_equal(old_acting, new_acting)) 2405 return true; 2406 2407 return false; 2408 } 2409 2410 /* 2411 * Map an object into a PG. 2412 * 2413 * Should only be called with target_oid and target_oloc (as opposed to 2414 * base_oid and base_oloc), since tiering isn't taken into account. 2415 */ 2416 void __ceph_object_locator_to_pg(struct ceph_pg_pool_info *pi, 2417 const struct ceph_object_id *oid, 2418 const struct ceph_object_locator *oloc, 2419 struct ceph_pg *raw_pgid) 2420 { 2421 WARN_ON(pi->id != oloc->pool); 2422 2423 if (!oloc->pool_ns) { 2424 raw_pgid->pool = oloc->pool; 2425 raw_pgid->seed = ceph_str_hash(pi->object_hash, oid->name, 2426 oid->name_len); 2427 dout("%s %s -> raw_pgid %llu.%x\n", __func__, oid->name, 2428 raw_pgid->pool, raw_pgid->seed); 2429 } else { 2430 char stack_buf[256]; 2431 char *buf = stack_buf; 2432 int nsl = oloc->pool_ns->len; 2433 size_t total = nsl + 1 + oid->name_len; 2434 2435 if (total > sizeof(stack_buf)) 2436 buf = kmalloc(total, GFP_NOIO | __GFP_NOFAIL); 2437 memcpy(buf, oloc->pool_ns->str, nsl); 2438 buf[nsl] = '\037'; 2439 memcpy(buf + nsl + 1, oid->name, oid->name_len); 2440 raw_pgid->pool = oloc->pool; 2441 raw_pgid->seed = ceph_str_hash(pi->object_hash, buf, total); 2442 if (buf != stack_buf) 2443 kfree(buf); 2444 dout("%s %s ns %.*s -> raw_pgid %llu.%x\n", __func__, 2445 oid->name, nsl, oloc->pool_ns->str, 2446 raw_pgid->pool, raw_pgid->seed); 2447 } 2448 } 2449 2450 int ceph_object_locator_to_pg(struct ceph_osdmap *osdmap, 2451 const struct ceph_object_id *oid, 2452 const struct ceph_object_locator *oloc, 2453 struct ceph_pg *raw_pgid) 2454 { 2455 struct ceph_pg_pool_info *pi; 2456 2457 pi = ceph_pg_pool_by_id(osdmap, oloc->pool); 2458 if (!pi) 2459 return -ENOENT; 2460 2461 __ceph_object_locator_to_pg(pi, oid, oloc, raw_pgid); 2462 return 0; 2463 } 2464 EXPORT_SYMBOL(ceph_object_locator_to_pg); 2465 2466 /* 2467 * Map a raw PG (full precision ps) into an actual PG. 2468 */ 2469 static void raw_pg_to_pg(struct ceph_pg_pool_info *pi, 2470 const struct ceph_pg *raw_pgid, 2471 struct ceph_pg *pgid) 2472 { 2473 pgid->pool = raw_pgid->pool; 2474 pgid->seed = ceph_stable_mod(raw_pgid->seed, pi->pg_num, 2475 pi->pg_num_mask); 2476 } 2477 2478 /* 2479 * Map a raw PG (full precision ps) into a placement ps (placement 2480 * seed). Include pool id in that value so that different pools don't 2481 * use the same seeds. 2482 */ 2483 static u32 raw_pg_to_pps(struct ceph_pg_pool_info *pi, 2484 const struct ceph_pg *raw_pgid) 2485 { 2486 if (pi->flags & CEPH_POOL_FLAG_HASHPSPOOL) { 2487 /* hash pool id and seed so that pool PGs do not overlap */ 2488 return crush_hash32_2(CRUSH_HASH_RJENKINS1, 2489 ceph_stable_mod(raw_pgid->seed, 2490 pi->pgp_num, 2491 pi->pgp_num_mask), 2492 raw_pgid->pool); 2493 } else { 2494 /* 2495 * legacy behavior: add ps and pool together. this is 2496 * not a great approach because the PGs from each pool 2497 * will overlap on top of each other: 0.5 == 1.4 == 2498 * 2.3 == ... 2499 */ 2500 return ceph_stable_mod(raw_pgid->seed, pi->pgp_num, 2501 pi->pgp_num_mask) + 2502 (unsigned)raw_pgid->pool; 2503 } 2504 } 2505 2506 /* 2507 * Magic value used for a "default" fallback choose_args, used if the 2508 * crush_choose_arg_map passed to do_crush() does not exist. If this 2509 * also doesn't exist, fall back to canonical weights. 2510 */ 2511 #define CEPH_DEFAULT_CHOOSE_ARGS -1 2512 2513 static int do_crush(struct ceph_osdmap *map, int ruleno, int x, 2514 int *result, int result_max, 2515 const __u32 *weight, int weight_max, 2516 s64 choose_args_index) 2517 { 2518 struct crush_choose_arg_map *arg_map; 2519 struct crush_work *work; 2520 int r; 2521 2522 BUG_ON(result_max > CEPH_PG_MAX_SIZE); 2523 2524 arg_map = lookup_choose_arg_map(&map->crush->choose_args, 2525 choose_args_index); 2526 if (!arg_map) 2527 arg_map = lookup_choose_arg_map(&map->crush->choose_args, 2528 CEPH_DEFAULT_CHOOSE_ARGS); 2529 2530 work = get_workspace(&map->crush_wsm, map->crush); 2531 r = crush_do_rule(map->crush, ruleno, x, result, result_max, 2532 weight, weight_max, work, 2533 arg_map ? arg_map->args : NULL); 2534 put_workspace(&map->crush_wsm, work); 2535 return r; 2536 } 2537 2538 static void remove_nonexistent_osds(struct ceph_osdmap *osdmap, 2539 struct ceph_pg_pool_info *pi, 2540 struct ceph_osds *set) 2541 { 2542 int i; 2543 2544 if (ceph_can_shift_osds(pi)) { 2545 int removed = 0; 2546 2547 /* shift left */ 2548 for (i = 0; i < set->size; i++) { 2549 if (!ceph_osd_exists(osdmap, set->osds[i])) { 2550 removed++; 2551 continue; 2552 } 2553 if (removed) 2554 set->osds[i - removed] = set->osds[i]; 2555 } 2556 set->size -= removed; 2557 } else { 2558 /* set dne devices to NONE */ 2559 for (i = 0; i < set->size; i++) { 2560 if (!ceph_osd_exists(osdmap, set->osds[i])) 2561 set->osds[i] = CRUSH_ITEM_NONE; 2562 } 2563 } 2564 } 2565 2566 /* 2567 * Calculate raw set (CRUSH output) for given PG and filter out 2568 * nonexistent OSDs. ->primary is undefined for a raw set. 2569 * 2570 * Placement seed (CRUSH input) is returned through @ppps. 2571 */ 2572 static void pg_to_raw_osds(struct ceph_osdmap *osdmap, 2573 struct ceph_pg_pool_info *pi, 2574 const struct ceph_pg *raw_pgid, 2575 struct ceph_osds *raw, 2576 u32 *ppps) 2577 { 2578 u32 pps = raw_pg_to_pps(pi, raw_pgid); 2579 int ruleno; 2580 int len; 2581 2582 ceph_osds_init(raw); 2583 if (ppps) 2584 *ppps = pps; 2585 2586 ruleno = crush_find_rule(osdmap->crush, pi->crush_ruleset, pi->type, 2587 pi->size); 2588 if (ruleno < 0) { 2589 pr_err("no crush rule: pool %lld ruleset %d type %d size %d\n", 2590 pi->id, pi->crush_ruleset, pi->type, pi->size); 2591 return; 2592 } 2593 2594 if (pi->size > ARRAY_SIZE(raw->osds)) { 2595 pr_err_ratelimited("pool %lld ruleset %d type %d too wide: size %d > %zu\n", 2596 pi->id, pi->crush_ruleset, pi->type, pi->size, 2597 ARRAY_SIZE(raw->osds)); 2598 return; 2599 } 2600 2601 len = do_crush(osdmap, ruleno, pps, raw->osds, pi->size, 2602 osdmap->osd_weight, osdmap->max_osd, pi->id); 2603 if (len < 0) { 2604 pr_err("error %d from crush rule %d: pool %lld ruleset %d type %d size %d\n", 2605 len, ruleno, pi->id, pi->crush_ruleset, pi->type, 2606 pi->size); 2607 return; 2608 } 2609 2610 raw->size = len; 2611 remove_nonexistent_osds(osdmap, pi, raw); 2612 } 2613 2614 /* apply pg_upmap[_items] mappings */ 2615 static void apply_upmap(struct ceph_osdmap *osdmap, 2616 const struct ceph_pg *pgid, 2617 struct ceph_osds *raw) 2618 { 2619 struct ceph_pg_mapping *pg; 2620 int i, j; 2621 2622 pg = lookup_pg_mapping(&osdmap->pg_upmap, pgid); 2623 if (pg) { 2624 /* make sure targets aren't marked out */ 2625 for (i = 0; i < pg->pg_upmap.len; i++) { 2626 int osd = pg->pg_upmap.osds[i]; 2627 2628 if (osd != CRUSH_ITEM_NONE && 2629 osd < osdmap->max_osd && 2630 osdmap->osd_weight[osd] == 0) { 2631 /* reject/ignore explicit mapping */ 2632 return; 2633 } 2634 } 2635 for (i = 0; i < pg->pg_upmap.len; i++) 2636 raw->osds[i] = pg->pg_upmap.osds[i]; 2637 raw->size = pg->pg_upmap.len; 2638 /* check and apply pg_upmap_items, if any */ 2639 } 2640 2641 pg = lookup_pg_mapping(&osdmap->pg_upmap_items, pgid); 2642 if (pg) { 2643 /* 2644 * Note: this approach does not allow a bidirectional swap, 2645 * e.g., [[1,2],[2,1]] applied to [0,1,2] -> [0,2,1]. 2646 */ 2647 for (i = 0; i < pg->pg_upmap_items.len; i++) { 2648 int from = pg->pg_upmap_items.from_to[i][0]; 2649 int to = pg->pg_upmap_items.from_to[i][1]; 2650 int pos = -1; 2651 bool exists = false; 2652 2653 /* make sure replacement doesn't already appear */ 2654 for (j = 0; j < raw->size; j++) { 2655 int osd = raw->osds[j]; 2656 2657 if (osd == to) { 2658 exists = true; 2659 break; 2660 } 2661 /* ignore mapping if target is marked out */ 2662 if (osd == from && pos < 0 && 2663 !(to != CRUSH_ITEM_NONE && 2664 to < osdmap->max_osd && 2665 osdmap->osd_weight[to] == 0)) { 2666 pos = j; 2667 } 2668 } 2669 if (!exists && pos >= 0) 2670 raw->osds[pos] = to; 2671 } 2672 } 2673 } 2674 2675 /* 2676 * Given raw set, calculate up set and up primary. By definition of an 2677 * up set, the result won't contain nonexistent or down OSDs. 2678 * 2679 * This is done in-place - on return @set is the up set. If it's 2680 * empty, ->primary will remain undefined. 2681 */ 2682 static void raw_to_up_osds(struct ceph_osdmap *osdmap, 2683 struct ceph_pg_pool_info *pi, 2684 struct ceph_osds *set) 2685 { 2686 int i; 2687 2688 /* ->primary is undefined for a raw set */ 2689 BUG_ON(set->primary != -1); 2690 2691 if (ceph_can_shift_osds(pi)) { 2692 int removed = 0; 2693 2694 /* shift left */ 2695 for (i = 0; i < set->size; i++) { 2696 if (ceph_osd_is_down(osdmap, set->osds[i])) { 2697 removed++; 2698 continue; 2699 } 2700 if (removed) 2701 set->osds[i - removed] = set->osds[i]; 2702 } 2703 set->size -= removed; 2704 if (set->size > 0) 2705 set->primary = set->osds[0]; 2706 } else { 2707 /* set down/dne devices to NONE */ 2708 for (i = set->size - 1; i >= 0; i--) { 2709 if (ceph_osd_is_down(osdmap, set->osds[i])) 2710 set->osds[i] = CRUSH_ITEM_NONE; 2711 else 2712 set->primary = set->osds[i]; 2713 } 2714 } 2715 } 2716 2717 static void apply_primary_affinity(struct ceph_osdmap *osdmap, 2718 struct ceph_pg_pool_info *pi, 2719 u32 pps, 2720 struct ceph_osds *up) 2721 { 2722 int i; 2723 int pos = -1; 2724 2725 /* 2726 * Do we have any non-default primary_affinity values for these 2727 * osds? 2728 */ 2729 if (!osdmap->osd_primary_affinity) 2730 return; 2731 2732 for (i = 0; i < up->size; i++) { 2733 int osd = up->osds[i]; 2734 2735 if (osd != CRUSH_ITEM_NONE && 2736 osdmap->osd_primary_affinity[osd] != 2737 CEPH_OSD_DEFAULT_PRIMARY_AFFINITY) { 2738 break; 2739 } 2740 } 2741 if (i == up->size) 2742 return; 2743 2744 /* 2745 * Pick the primary. Feed both the seed (for the pg) and the 2746 * osd into the hash/rng so that a proportional fraction of an 2747 * osd's pgs get rejected as primary. 2748 */ 2749 for (i = 0; i < up->size; i++) { 2750 int osd = up->osds[i]; 2751 u32 aff; 2752 2753 if (osd == CRUSH_ITEM_NONE) 2754 continue; 2755 2756 aff = osdmap->osd_primary_affinity[osd]; 2757 if (aff < CEPH_OSD_MAX_PRIMARY_AFFINITY && 2758 (crush_hash32_2(CRUSH_HASH_RJENKINS1, 2759 pps, osd) >> 16) >= aff) { 2760 /* 2761 * We chose not to use this primary. Note it 2762 * anyway as a fallback in case we don't pick 2763 * anyone else, but keep looking. 2764 */ 2765 if (pos < 0) 2766 pos = i; 2767 } else { 2768 pos = i; 2769 break; 2770 } 2771 } 2772 if (pos < 0) 2773 return; 2774 2775 up->primary = up->osds[pos]; 2776 2777 if (ceph_can_shift_osds(pi) && pos > 0) { 2778 /* move the new primary to the front */ 2779 for (i = pos; i > 0; i--) 2780 up->osds[i] = up->osds[i - 1]; 2781 up->osds[0] = up->primary; 2782 } 2783 } 2784 2785 /* 2786 * Get pg_temp and primary_temp mappings for given PG. 2787 * 2788 * Note that a PG may have none, only pg_temp, only primary_temp or 2789 * both pg_temp and primary_temp mappings. This means @temp isn't 2790 * always a valid OSD set on return: in the "only primary_temp" case, 2791 * @temp will have its ->primary >= 0 but ->size == 0. 2792 */ 2793 static void get_temp_osds(struct ceph_osdmap *osdmap, 2794 struct ceph_pg_pool_info *pi, 2795 const struct ceph_pg *pgid, 2796 struct ceph_osds *temp) 2797 { 2798 struct ceph_pg_mapping *pg; 2799 int i; 2800 2801 ceph_osds_init(temp); 2802 2803 /* pg_temp? */ 2804 pg = lookup_pg_mapping(&osdmap->pg_temp, pgid); 2805 if (pg) { 2806 for (i = 0; i < pg->pg_temp.len; i++) { 2807 if (ceph_osd_is_down(osdmap, pg->pg_temp.osds[i])) { 2808 if (ceph_can_shift_osds(pi)) 2809 continue; 2810 2811 temp->osds[temp->size++] = CRUSH_ITEM_NONE; 2812 } else { 2813 temp->osds[temp->size++] = pg->pg_temp.osds[i]; 2814 } 2815 } 2816 2817 /* apply pg_temp's primary */ 2818 for (i = 0; i < temp->size; i++) { 2819 if (temp->osds[i] != CRUSH_ITEM_NONE) { 2820 temp->primary = temp->osds[i]; 2821 break; 2822 } 2823 } 2824 } 2825 2826 /* primary_temp? (shouldn't ever be a nonexistent or down OSD) */ 2827 pg = lookup_pg_mapping(&osdmap->primary_temp, pgid); 2828 if (pg && !WARN_ON_ONCE(ceph_osd_is_down(osdmap, 2829 pg->primary_temp.osd))) 2830 temp->primary = pg->primary_temp.osd; 2831 } 2832 2833 /* 2834 * Map a PG to its acting set as well as its up set. 2835 * 2836 * Acting set is used for data mapping purposes, while up set can be 2837 * recorded for detecting interval changes and deciding whether to 2838 * resend a request. 2839 */ 2840 void ceph_pg_to_up_acting_osds(struct ceph_osdmap *osdmap, 2841 struct ceph_pg_pool_info *pi, 2842 const struct ceph_pg *raw_pgid, 2843 struct ceph_osds *up, 2844 struct ceph_osds *acting) 2845 { 2846 struct ceph_pg pgid; 2847 u32 pps; 2848 2849 WARN_ON(pi->id != raw_pgid->pool); 2850 raw_pg_to_pg(pi, raw_pgid, &pgid); 2851 2852 pg_to_raw_osds(osdmap, pi, raw_pgid, up, &pps); 2853 apply_upmap(osdmap, &pgid, up); 2854 raw_to_up_osds(osdmap, pi, up); 2855 apply_primary_affinity(osdmap, pi, pps, up); 2856 get_temp_osds(osdmap, pi, &pgid, acting); 2857 if (!acting->size) { 2858 memcpy(acting->osds, up->osds, up->size * sizeof(up->osds[0])); 2859 acting->size = up->size; 2860 if (acting->primary == -1) 2861 acting->primary = up->primary; 2862 } 2863 WARN_ON(!osds_valid(up) || !osds_valid(acting)); 2864 } 2865 2866 bool ceph_pg_to_primary_shard(struct ceph_osdmap *osdmap, 2867 struct ceph_pg_pool_info *pi, 2868 const struct ceph_pg *raw_pgid, 2869 struct ceph_spg *spgid) 2870 { 2871 struct ceph_pg pgid; 2872 struct ceph_osds up, acting; 2873 int i; 2874 2875 WARN_ON(pi->id != raw_pgid->pool); 2876 raw_pg_to_pg(pi, raw_pgid, &pgid); 2877 2878 if (ceph_can_shift_osds(pi)) { 2879 spgid->pgid = pgid; /* struct */ 2880 spgid->shard = CEPH_SPG_NOSHARD; 2881 return true; 2882 } 2883 2884 ceph_pg_to_up_acting_osds(osdmap, pi, &pgid, &up, &acting); 2885 for (i = 0; i < acting.size; i++) { 2886 if (acting.osds[i] == acting.primary) { 2887 spgid->pgid = pgid; /* struct */ 2888 spgid->shard = i; 2889 return true; 2890 } 2891 } 2892 2893 return false; 2894 } 2895 2896 /* 2897 * Return acting primary for given PG, or -1 if none. 2898 */ 2899 int ceph_pg_to_acting_primary(struct ceph_osdmap *osdmap, 2900 const struct ceph_pg *raw_pgid) 2901 { 2902 struct ceph_pg_pool_info *pi; 2903 struct ceph_osds up, acting; 2904 2905 pi = ceph_pg_pool_by_id(osdmap, raw_pgid->pool); 2906 if (!pi) 2907 return -1; 2908 2909 ceph_pg_to_up_acting_osds(osdmap, pi, raw_pgid, &up, &acting); 2910 return acting.primary; 2911 } 2912 EXPORT_SYMBOL(ceph_pg_to_acting_primary); 2913 2914 static struct crush_loc_node *alloc_crush_loc(size_t type_name_len, 2915 size_t name_len) 2916 { 2917 struct crush_loc_node *loc; 2918 2919 loc = kmalloc(sizeof(*loc) + type_name_len + name_len + 2, GFP_NOIO); 2920 if (!loc) 2921 return NULL; 2922 2923 RB_CLEAR_NODE(&loc->cl_node); 2924 return loc; 2925 } 2926 2927 static void free_crush_loc(struct crush_loc_node *loc) 2928 { 2929 WARN_ON(!RB_EMPTY_NODE(&loc->cl_node)); 2930 2931 kfree(loc); 2932 } 2933 2934 static int crush_loc_compare(const struct crush_loc *loc1, 2935 const struct crush_loc *loc2) 2936 { 2937 return strcmp(loc1->cl_type_name, loc2->cl_type_name) ?: 2938 strcmp(loc1->cl_name, loc2->cl_name); 2939 } 2940 2941 DEFINE_RB_FUNCS2(crush_loc, struct crush_loc_node, cl_loc, crush_loc_compare, 2942 RB_BYPTR, const struct crush_loc *, cl_node) 2943 2944 /* 2945 * Parses a set of <bucket type name>':'<bucket name> pairs separated 2946 * by '|', e.g. "rack:foo1|rack:foo2|datacenter:bar". 2947 * 2948 * Note that @crush_location is modified by strsep(). 2949 */ 2950 int ceph_parse_crush_location(char *crush_location, struct rb_root *locs) 2951 { 2952 struct crush_loc_node *loc; 2953 const char *type_name, *name, *colon; 2954 size_t type_name_len, name_len; 2955 2956 dout("%s '%s'\n", __func__, crush_location); 2957 while ((type_name = strsep(&crush_location, "|"))) { 2958 colon = strchr(type_name, ':'); 2959 if (!colon) 2960 return -EINVAL; 2961 2962 type_name_len = colon - type_name; 2963 if (type_name_len == 0) 2964 return -EINVAL; 2965 2966 name = colon + 1; 2967 name_len = strlen(name); 2968 if (name_len == 0) 2969 return -EINVAL; 2970 2971 loc = alloc_crush_loc(type_name_len, name_len); 2972 if (!loc) 2973 return -ENOMEM; 2974 2975 loc->cl_loc.cl_type_name = loc->cl_data; 2976 memcpy(loc->cl_loc.cl_type_name, type_name, type_name_len); 2977 loc->cl_loc.cl_type_name[type_name_len] = '\0'; 2978 2979 loc->cl_loc.cl_name = loc->cl_data + type_name_len + 1; 2980 memcpy(loc->cl_loc.cl_name, name, name_len); 2981 loc->cl_loc.cl_name[name_len] = '\0'; 2982 2983 if (!__insert_crush_loc(locs, loc)) { 2984 free_crush_loc(loc); 2985 return -EEXIST; 2986 } 2987 2988 dout("%s type_name '%s' name '%s'\n", __func__, 2989 loc->cl_loc.cl_type_name, loc->cl_loc.cl_name); 2990 } 2991 2992 return 0; 2993 } 2994 2995 int ceph_compare_crush_locs(struct rb_root *locs1, struct rb_root *locs2) 2996 { 2997 struct rb_node *n1 = rb_first(locs1); 2998 struct rb_node *n2 = rb_first(locs2); 2999 int ret; 3000 3001 for ( ; n1 && n2; n1 = rb_next(n1), n2 = rb_next(n2)) { 3002 struct crush_loc_node *loc1 = 3003 rb_entry(n1, struct crush_loc_node, cl_node); 3004 struct crush_loc_node *loc2 = 3005 rb_entry(n2, struct crush_loc_node, cl_node); 3006 3007 ret = crush_loc_compare(&loc1->cl_loc, &loc2->cl_loc); 3008 if (ret) 3009 return ret; 3010 } 3011 3012 if (!n1 && n2) 3013 return -1; 3014 if (n1 && !n2) 3015 return 1; 3016 return 0; 3017 } 3018 3019 void ceph_clear_crush_locs(struct rb_root *locs) 3020 { 3021 while (!RB_EMPTY_ROOT(locs)) { 3022 struct crush_loc_node *loc = 3023 rb_entry(rb_first(locs), struct crush_loc_node, cl_node); 3024 3025 erase_crush_loc(locs, loc); 3026 free_crush_loc(loc); 3027 } 3028 } 3029 3030 /* 3031 * [a-zA-Z0-9-_.]+ 3032 */ 3033 static bool is_valid_crush_name(const char *name) 3034 { 3035 do { 3036 if (!('a' <= *name && *name <= 'z') && 3037 !('A' <= *name && *name <= 'Z') && 3038 !('0' <= *name && *name <= '9') && 3039 *name != '-' && *name != '_' && *name != '.') 3040 return false; 3041 } while (*++name != '\0'); 3042 3043 return true; 3044 } 3045 3046 /* 3047 * Gets the parent of an item. Returns its id (<0 because the 3048 * parent is always a bucket), type id (>0 for the same reason, 3049 * via @parent_type_id) and location (via @parent_loc). If no 3050 * parent, returns 0. 3051 * 3052 * Does a linear search, as there are no parent pointers of any 3053 * kind. Note that the result is ambiguous for items that occur 3054 * multiple times in the map. 3055 */ 3056 static int get_immediate_parent(struct crush_map *c, int id, 3057 u16 *parent_type_id, 3058 struct crush_loc *parent_loc) 3059 { 3060 struct crush_bucket *b; 3061 struct crush_name_node *type_cn, *cn; 3062 int i, j; 3063 3064 for (i = 0; i < c->max_buckets; i++) { 3065 b = c->buckets[i]; 3066 if (!b) 3067 continue; 3068 3069 /* ignore per-class shadow hierarchy */ 3070 cn = lookup_crush_name(&c->names, b->id); 3071 if (!cn || !is_valid_crush_name(cn->cn_name)) 3072 continue; 3073 3074 for (j = 0; j < b->size; j++) { 3075 if (b->items[j] != id) 3076 continue; 3077 3078 type_cn = lookup_crush_name(&c->type_names, b->type); 3079 if (WARN_ON_ONCE(!type_cn)) 3080 continue; 3081 3082 *parent_type_id = b->type; 3083 parent_loc->cl_type_name = type_cn->cn_name; 3084 parent_loc->cl_name = cn->cn_name; 3085 return b->id; 3086 } 3087 } 3088 3089 return 0; /* no parent */ 3090 } 3091 3092 /* 3093 * Calculates the locality/distance from an item to a client 3094 * location expressed in terms of CRUSH hierarchy as a set of 3095 * (bucket type name, bucket name) pairs. Specifically, looks 3096 * for the lowest-valued bucket type for which the location of 3097 * @id matches one of the locations in @locs, so for standard 3098 * bucket types (host = 1, rack = 3, datacenter = 8, zone = 9) 3099 * a matching host is closer than a matching rack and a matching 3100 * data center is closer than a matching zone. 3101 * 3102 * Specifying multiple locations (a "multipath" location) such 3103 * as "rack=foo1 rack=foo2 datacenter=bar" is allowed -- @locs 3104 * is a multimap. The locality will be: 3105 * 3106 * - 3 for OSDs in racks foo1 and foo2 3107 * - 8 for OSDs in data center bar 3108 * - -1 for all other OSDs 3109 * 3110 * The lowest possible bucket type is 1, so the best locality 3111 * for an OSD is 1 (i.e. a matching host). Locality 0 would be 3112 * the OSD itself. 3113 */ 3114 int ceph_get_crush_locality(struct ceph_osdmap *osdmap, int id, 3115 struct rb_root *locs) 3116 { 3117 struct crush_loc loc; 3118 u16 type_id; 3119 3120 /* 3121 * Instead of repeated get_immediate_parent() calls, 3122 * the location of @id could be obtained with a single 3123 * depth-first traversal. 3124 */ 3125 for (;;) { 3126 id = get_immediate_parent(osdmap->crush, id, &type_id, &loc); 3127 if (id >= 0) 3128 return -1; /* not local */ 3129 3130 if (lookup_crush_loc(locs, &loc)) 3131 return type_id; 3132 } 3133 } 3134