1 /* 2 * Ceph - scalable distributed file system 3 * 4 * Copyright (C) 2015 Intel Corporation All Rights Reserved 5 * 6 * This is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License version 2.1, as published by the Free Software 9 * Foundation. See file COPYING. 10 * 11 */ 12 13 #ifdef __KERNEL__ 14 # include <linux/string.h> 15 # include <linux/slab.h> 16 # include <linux/bug.h> 17 # include <linux/kernel.h> 18 # include <linux/crush/crush.h> 19 # include <linux/crush/hash.h> 20 # include <linux/crush/mapper.h> 21 #else 22 # include "crush_compat.h" 23 # include "crush.h" 24 # include "hash.h" 25 # include "mapper.h" 26 #endif 27 #include "crush_ln_table.h" 28 29 #define dprintk(args...) /* printf(args) */ 30 31 /* 32 * Implement the core CRUSH mapping algorithm. 33 */ 34 35 /** 36 * crush_find_rule - find a crush_rule id for a given ruleset, type, and size. 37 * @map: the crush_map 38 * @ruleset: the storage ruleset id (user defined) 39 * @type: storage ruleset type (user defined) 40 * @size: output set size 41 */ 42 int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size) 43 { 44 __u32 i; 45 46 for (i = 0; i < map->max_rules; i++) { 47 if (map->rules[i] && 48 map->rules[i]->mask.ruleset == ruleset && 49 map->rules[i]->mask.type == type && 50 map->rules[i]->mask.min_size <= size && 51 map->rules[i]->mask.max_size >= size) 52 return i; 53 } 54 return -1; 55 } 56 57 58 /* 59 * bucket choose methods 60 * 61 * For each bucket algorithm, we have a "choose" method that, given a 62 * crush input @x and replica position (usually, position in output set) @r, 63 * will produce an item in the bucket. 64 */ 65 66 /* 67 * Choose based on a random permutation of the bucket. 68 * 69 * We used to use some prime number arithmetic to do this, but it 70 * wasn't very random, and had some other bad behaviors. Instead, we 71 * calculate an actual random permutation of the bucket members. 72 * Since this is expensive, we optimize for the r=0 case, which 73 * captures the vast majority of calls. 74 */ 75 static int bucket_perm_choose(struct crush_bucket *bucket, 76 int x, int r) 77 { 78 unsigned int pr = r % bucket->size; 79 unsigned int i, s; 80 81 /* start a new permutation if @x has changed */ 82 if (bucket->perm_x != (__u32)x || bucket->perm_n == 0) { 83 dprintk("bucket %d new x=%d\n", bucket->id, x); 84 bucket->perm_x = x; 85 86 /* optimize common r=0 case */ 87 if (pr == 0) { 88 s = crush_hash32_3(bucket->hash, x, bucket->id, 0) % 89 bucket->size; 90 bucket->perm[0] = s; 91 bucket->perm_n = 0xffff; /* magic value, see below */ 92 goto out; 93 } 94 95 for (i = 0; i < bucket->size; i++) 96 bucket->perm[i] = i; 97 bucket->perm_n = 0; 98 } else if (bucket->perm_n == 0xffff) { 99 /* clean up after the r=0 case above */ 100 for (i = 1; i < bucket->size; i++) 101 bucket->perm[i] = i; 102 bucket->perm[bucket->perm[0]] = 0; 103 bucket->perm_n = 1; 104 } 105 106 /* calculate permutation up to pr */ 107 for (i = 0; i < bucket->perm_n; i++) 108 dprintk(" perm_choose have %d: %d\n", i, bucket->perm[i]); 109 while (bucket->perm_n <= pr) { 110 unsigned int p = bucket->perm_n; 111 /* no point in swapping the final entry */ 112 if (p < bucket->size - 1) { 113 i = crush_hash32_3(bucket->hash, x, bucket->id, p) % 114 (bucket->size - p); 115 if (i) { 116 unsigned int t = bucket->perm[p + i]; 117 bucket->perm[p + i] = bucket->perm[p]; 118 bucket->perm[p] = t; 119 } 120 dprintk(" perm_choose swap %d with %d\n", p, p+i); 121 } 122 bucket->perm_n++; 123 } 124 for (i = 0; i < bucket->size; i++) 125 dprintk(" perm_choose %d: %d\n", i, bucket->perm[i]); 126 127 s = bucket->perm[pr]; 128 out: 129 dprintk(" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->id, 130 bucket->size, x, r, pr, s); 131 return bucket->items[s]; 132 } 133 134 /* uniform */ 135 static int bucket_uniform_choose(struct crush_bucket_uniform *bucket, 136 int x, int r) 137 { 138 return bucket_perm_choose(&bucket->h, x, r); 139 } 140 141 /* list */ 142 static int bucket_list_choose(struct crush_bucket_list *bucket, 143 int x, int r) 144 { 145 int i; 146 147 for (i = bucket->h.size-1; i >= 0; i--) { 148 __u64 w = crush_hash32_4(bucket->h.hash, x, bucket->h.items[i], 149 r, bucket->h.id); 150 w &= 0xffff; 151 dprintk("list_choose i=%d x=%d r=%d item %d weight %x " 152 "sw %x rand %llx", 153 i, x, r, bucket->h.items[i], bucket->item_weights[i], 154 bucket->sum_weights[i], w); 155 w *= bucket->sum_weights[i]; 156 w = w >> 16; 157 /*dprintk(" scaled %llx\n", w);*/ 158 if (w < bucket->item_weights[i]) 159 return bucket->h.items[i]; 160 } 161 162 dprintk("bad list sums for bucket %d\n", bucket->h.id); 163 return bucket->h.items[0]; 164 } 165 166 167 /* (binary) tree */ 168 static int height(int n) 169 { 170 int h = 0; 171 while ((n & 1) == 0) { 172 h++; 173 n = n >> 1; 174 } 175 return h; 176 } 177 178 static int left(int x) 179 { 180 int h = height(x); 181 return x - (1 << (h-1)); 182 } 183 184 static int right(int x) 185 { 186 int h = height(x); 187 return x + (1 << (h-1)); 188 } 189 190 static int terminal(int x) 191 { 192 return x & 1; 193 } 194 195 static int bucket_tree_choose(struct crush_bucket_tree *bucket, 196 int x, int r) 197 { 198 int n; 199 __u32 w; 200 __u64 t; 201 202 /* start at root */ 203 n = bucket->num_nodes >> 1; 204 205 while (!terminal(n)) { 206 int l; 207 /* pick point in [0, w) */ 208 w = bucket->node_weights[n]; 209 t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r, 210 bucket->h.id) * (__u64)w; 211 t = t >> 32; 212 213 /* descend to the left or right? */ 214 l = left(n); 215 if (t < bucket->node_weights[l]) 216 n = l; 217 else 218 n = right(n); 219 } 220 221 return bucket->h.items[n >> 1]; 222 } 223 224 225 /* straw */ 226 227 static int bucket_straw_choose(struct crush_bucket_straw *bucket, 228 int x, int r) 229 { 230 __u32 i; 231 int high = 0; 232 __u64 high_draw = 0; 233 __u64 draw; 234 235 for (i = 0; i < bucket->h.size; i++) { 236 draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r); 237 draw &= 0xffff; 238 draw *= bucket->straws[i]; 239 if (i == 0 || draw > high_draw) { 240 high = i; 241 high_draw = draw; 242 } 243 } 244 return bucket->h.items[high]; 245 } 246 247 /* compute 2^44*log2(input+1) */ 248 static __u64 crush_ln(unsigned int xin) 249 { 250 unsigned int x = xin; 251 int iexpon, index1, index2; 252 __u64 RH, LH, LL, xl64, result; 253 254 x++; 255 256 /* normalize input */ 257 iexpon = 15; 258 259 /* 260 * figure out number of bits we need to shift and 261 * do it in one step instead of iteratively 262 */ 263 if (!(x & 0x18000)) { 264 int bits = __builtin_clz(x & 0x1FFFF) - 16; 265 x <<= bits; 266 iexpon = 15 - bits; 267 } 268 269 index1 = (x >> 8) << 1; 270 /* RH ~ 2^56/index1 */ 271 RH = __RH_LH_tbl[index1 - 256]; 272 /* LH ~ 2^48 * log2(index1/256) */ 273 LH = __RH_LH_tbl[index1 + 1 - 256]; 274 275 /* RH*x ~ 2^48 * (2^15 + xf), xf<2^8 */ 276 xl64 = (__s64)x * RH; 277 xl64 >>= 48; 278 279 result = iexpon; 280 result <<= (12 + 32); 281 282 index2 = xl64 & 0xff; 283 /* LL ~ 2^48*log2(1.0+index2/2^15) */ 284 LL = __LL_tbl[index2]; 285 286 LH = LH + LL; 287 288 LH >>= (48 - 12 - 32); 289 result += LH; 290 291 return result; 292 } 293 294 295 /* 296 * straw2 297 * 298 * for reference, see: 299 * 300 * http://en.wikipedia.org/wiki/Exponential_distribution#Distribution_of_the_minimum_of_exponential_random_variables 301 * 302 */ 303 304 static int bucket_straw2_choose(struct crush_bucket_straw2 *bucket, 305 int x, int r) 306 { 307 unsigned int i, high = 0; 308 unsigned int u; 309 unsigned int w; 310 __s64 ln, draw, high_draw = 0; 311 312 for (i = 0; i < bucket->h.size; i++) { 313 w = bucket->item_weights[i]; 314 if (w) { 315 u = crush_hash32_3(bucket->h.hash, x, 316 bucket->h.items[i], r); 317 u &= 0xffff; 318 319 /* 320 * for some reason slightly less than 0x10000 produces 321 * a slightly more accurate distribution... probably a 322 * rounding effect. 323 * 324 * the natural log lookup table maps [0,0xffff] 325 * (corresponding to real numbers [1/0x10000, 1] to 326 * [0, 0xffffffffffff] (corresponding to real numbers 327 * [-11.090355,0]). 328 */ 329 ln = crush_ln(u) - 0x1000000000000ll; 330 331 /* 332 * divide by 16.16 fixed-point weight. note 333 * that the ln value is negative, so a larger 334 * weight means a larger (less negative) value 335 * for draw. 336 */ 337 draw = div64_s64(ln, w); 338 } else { 339 draw = S64_MIN; 340 } 341 342 if (i == 0 || draw > high_draw) { 343 high = i; 344 high_draw = draw; 345 } 346 } 347 return bucket->h.items[high]; 348 } 349 350 351 static int crush_bucket_choose(struct crush_bucket *in, int x, int r) 352 { 353 dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r); 354 BUG_ON(in->size == 0); 355 switch (in->alg) { 356 case CRUSH_BUCKET_UNIFORM: 357 return bucket_uniform_choose((struct crush_bucket_uniform *)in, 358 x, r); 359 case CRUSH_BUCKET_LIST: 360 return bucket_list_choose((struct crush_bucket_list *)in, 361 x, r); 362 case CRUSH_BUCKET_TREE: 363 return bucket_tree_choose((struct crush_bucket_tree *)in, 364 x, r); 365 case CRUSH_BUCKET_STRAW: 366 return bucket_straw_choose((struct crush_bucket_straw *)in, 367 x, r); 368 case CRUSH_BUCKET_STRAW2: 369 return bucket_straw2_choose((struct crush_bucket_straw2 *)in, 370 x, r); 371 default: 372 dprintk("unknown bucket %d alg %d\n", in->id, in->alg); 373 return in->items[0]; 374 } 375 } 376 377 378 /* 379 * true if device is marked "out" (failed, fully offloaded) 380 * of the cluster 381 */ 382 static int is_out(const struct crush_map *map, 383 const __u32 *weight, int weight_max, 384 int item, int x) 385 { 386 if (item >= weight_max) 387 return 1; 388 if (weight[item] >= 0x10000) 389 return 0; 390 if (weight[item] == 0) 391 return 1; 392 if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff) 393 < weight[item]) 394 return 0; 395 return 1; 396 } 397 398 /** 399 * crush_choose_firstn - choose numrep distinct items of given type 400 * @map: the crush_map 401 * @bucket: the bucket we are choose an item from 402 * @x: crush input value 403 * @numrep: the number of items to choose 404 * @type: the type of item to choose 405 * @out: pointer to output vector 406 * @outpos: our position in that vector 407 * @out_size: size of the out vector 408 * @tries: number of attempts to make 409 * @recurse_tries: number of attempts to have recursive chooseleaf make 410 * @local_retries: localized retries 411 * @local_fallback_retries: localized fallback retries 412 * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose) 413 * @stable: stable mode starts rep=0 in the recursive call for all replicas 414 * @vary_r: pass r to recursive calls 415 * @out2: second output vector for leaf items (if @recurse_to_leaf) 416 * @parent_r: r value passed from the parent 417 */ 418 static int crush_choose_firstn(const struct crush_map *map, 419 struct crush_bucket *bucket, 420 const __u32 *weight, int weight_max, 421 int x, int numrep, int type, 422 int *out, int outpos, 423 int out_size, 424 unsigned int tries, 425 unsigned int recurse_tries, 426 unsigned int local_retries, 427 unsigned int local_fallback_retries, 428 int recurse_to_leaf, 429 unsigned int vary_r, 430 unsigned int stable, 431 int *out2, 432 int parent_r) 433 { 434 int rep; 435 unsigned int ftotal, flocal; 436 int retry_descent, retry_bucket, skip_rep; 437 struct crush_bucket *in = bucket; 438 int r; 439 int i; 440 int item = 0; 441 int itemtype; 442 int collide, reject; 443 int count = out_size; 444 445 dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d tries %d recurse_tries %d local_retries %d local_fallback_retries %d parent_r %d stable %d\n", 446 recurse_to_leaf ? "_LEAF" : "", 447 bucket->id, x, outpos, numrep, 448 tries, recurse_tries, local_retries, local_fallback_retries, 449 parent_r, stable); 450 451 for (rep = stable ? 0 : outpos; rep < numrep && count > 0 ; rep++) { 452 /* keep trying until we get a non-out, non-colliding item */ 453 ftotal = 0; 454 skip_rep = 0; 455 do { 456 retry_descent = 0; 457 in = bucket; /* initial bucket */ 458 459 /* choose through intervening buckets */ 460 flocal = 0; 461 do { 462 collide = 0; 463 retry_bucket = 0; 464 r = rep + parent_r; 465 /* r' = r + f_total */ 466 r += ftotal; 467 468 /* bucket choose */ 469 if (in->size == 0) { 470 reject = 1; 471 goto reject; 472 } 473 if (local_fallback_retries > 0 && 474 flocal >= (in->size>>1) && 475 flocal > local_fallback_retries) 476 item = bucket_perm_choose(in, x, r); 477 else 478 item = crush_bucket_choose(in, x, r); 479 if (item >= map->max_devices) { 480 dprintk(" bad item %d\n", item); 481 skip_rep = 1; 482 break; 483 } 484 485 /* desired type? */ 486 if (item < 0) 487 itemtype = map->buckets[-1-item]->type; 488 else 489 itemtype = 0; 490 dprintk(" item %d type %d\n", item, itemtype); 491 492 /* keep going? */ 493 if (itemtype != type) { 494 if (item >= 0 || 495 (-1-item) >= map->max_buckets) { 496 dprintk(" bad item type %d\n", type); 497 skip_rep = 1; 498 break; 499 } 500 in = map->buckets[-1-item]; 501 retry_bucket = 1; 502 continue; 503 } 504 505 /* collision? */ 506 for (i = 0; i < outpos; i++) { 507 if (out[i] == item) { 508 collide = 1; 509 break; 510 } 511 } 512 513 reject = 0; 514 if (!collide && recurse_to_leaf) { 515 if (item < 0) { 516 int sub_r; 517 if (vary_r) 518 sub_r = r >> (vary_r-1); 519 else 520 sub_r = 0; 521 if (crush_choose_firstn(map, 522 map->buckets[-1-item], 523 weight, weight_max, 524 x, stable ? 1 : outpos+1, 0, 525 out2, outpos, count, 526 recurse_tries, 0, 527 local_retries, 528 local_fallback_retries, 529 0, 530 vary_r, 531 stable, 532 NULL, 533 sub_r) <= outpos) 534 /* didn't get leaf */ 535 reject = 1; 536 } else { 537 /* we already have a leaf! */ 538 out2[outpos] = item; 539 } 540 } 541 542 if (!reject) { 543 /* out? */ 544 if (itemtype == 0) 545 reject = is_out(map, weight, 546 weight_max, 547 item, x); 548 else 549 reject = 0; 550 } 551 552 reject: 553 if (reject || collide) { 554 ftotal++; 555 flocal++; 556 557 if (collide && flocal <= local_retries) 558 /* retry locally a few times */ 559 retry_bucket = 1; 560 else if (local_fallback_retries > 0 && 561 flocal <= in->size + local_fallback_retries) 562 /* exhaustive bucket search */ 563 retry_bucket = 1; 564 else if (ftotal < tries) 565 /* then retry descent */ 566 retry_descent = 1; 567 else 568 /* else give up */ 569 skip_rep = 1; 570 dprintk(" reject %d collide %d " 571 "ftotal %u flocal %u\n", 572 reject, collide, ftotal, 573 flocal); 574 } 575 } while (retry_bucket); 576 } while (retry_descent); 577 578 if (skip_rep) { 579 dprintk("skip rep\n"); 580 continue; 581 } 582 583 dprintk("CHOOSE got %d\n", item); 584 out[outpos] = item; 585 outpos++; 586 count--; 587 #ifndef __KERNEL__ 588 if (map->choose_tries && ftotal <= map->choose_total_tries) 589 map->choose_tries[ftotal]++; 590 #endif 591 } 592 593 dprintk("CHOOSE returns %d\n", outpos); 594 return outpos; 595 } 596 597 598 /** 599 * crush_choose_indep: alternative breadth-first positionally stable mapping 600 * 601 */ 602 static void crush_choose_indep(const struct crush_map *map, 603 struct crush_bucket *bucket, 604 const __u32 *weight, int weight_max, 605 int x, int left, int numrep, int type, 606 int *out, int outpos, 607 unsigned int tries, 608 unsigned int recurse_tries, 609 int recurse_to_leaf, 610 int *out2, 611 int parent_r) 612 { 613 struct crush_bucket *in = bucket; 614 int endpos = outpos + left; 615 int rep; 616 unsigned int ftotal; 617 int r; 618 int i; 619 int item = 0; 620 int itemtype; 621 int collide; 622 623 dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "", 624 bucket->id, x, outpos, numrep); 625 626 /* initially my result is undefined */ 627 for (rep = outpos; rep < endpos; rep++) { 628 out[rep] = CRUSH_ITEM_UNDEF; 629 if (out2) 630 out2[rep] = CRUSH_ITEM_UNDEF; 631 } 632 633 for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) { 634 #ifdef DEBUG_INDEP 635 if (out2 && ftotal) { 636 dprintk("%u %d a: ", ftotal, left); 637 for (rep = outpos; rep < endpos; rep++) { 638 dprintk(" %d", out[rep]); 639 } 640 dprintk("\n"); 641 dprintk("%u %d b: ", ftotal, left); 642 for (rep = outpos; rep < endpos; rep++) { 643 dprintk(" %d", out2[rep]); 644 } 645 dprintk("\n"); 646 } 647 #endif 648 for (rep = outpos; rep < endpos; rep++) { 649 if (out[rep] != CRUSH_ITEM_UNDEF) 650 continue; 651 652 in = bucket; /* initial bucket */ 653 654 /* choose through intervening buckets */ 655 for (;;) { 656 /* note: we base the choice on the position 657 * even in the nested call. that means that 658 * if the first layer chooses the same bucket 659 * in a different position, we will tend to 660 * choose a different item in that bucket. 661 * this will involve more devices in data 662 * movement and tend to distribute the load. 663 */ 664 r = rep + parent_r; 665 666 /* be careful */ 667 if (in->alg == CRUSH_BUCKET_UNIFORM && 668 in->size % numrep == 0) 669 /* r'=r+(n+1)*f_total */ 670 r += (numrep+1) * ftotal; 671 else 672 /* r' = r + n*f_total */ 673 r += numrep * ftotal; 674 675 /* bucket choose */ 676 if (in->size == 0) { 677 dprintk(" empty bucket\n"); 678 break; 679 } 680 681 item = crush_bucket_choose(in, x, r); 682 if (item >= map->max_devices) { 683 dprintk(" bad item %d\n", item); 684 out[rep] = CRUSH_ITEM_NONE; 685 if (out2) 686 out2[rep] = CRUSH_ITEM_NONE; 687 left--; 688 break; 689 } 690 691 /* desired type? */ 692 if (item < 0) 693 itemtype = map->buckets[-1-item]->type; 694 else 695 itemtype = 0; 696 dprintk(" item %d type %d\n", item, itemtype); 697 698 /* keep going? */ 699 if (itemtype != type) { 700 if (item >= 0 || 701 (-1-item) >= map->max_buckets) { 702 dprintk(" bad item type %d\n", type); 703 out[rep] = CRUSH_ITEM_NONE; 704 if (out2) 705 out2[rep] = 706 CRUSH_ITEM_NONE; 707 left--; 708 break; 709 } 710 in = map->buckets[-1-item]; 711 continue; 712 } 713 714 /* collision? */ 715 collide = 0; 716 for (i = outpos; i < endpos; i++) { 717 if (out[i] == item) { 718 collide = 1; 719 break; 720 } 721 } 722 if (collide) 723 break; 724 725 if (recurse_to_leaf) { 726 if (item < 0) { 727 crush_choose_indep(map, 728 map->buckets[-1-item], 729 weight, weight_max, 730 x, 1, numrep, 0, 731 out2, rep, 732 recurse_tries, 0, 733 0, NULL, r); 734 if (out2[rep] == CRUSH_ITEM_NONE) { 735 /* placed nothing; no leaf */ 736 break; 737 } 738 } else { 739 /* we already have a leaf! */ 740 out2[rep] = item; 741 } 742 } 743 744 /* out? */ 745 if (itemtype == 0 && 746 is_out(map, weight, weight_max, item, x)) 747 break; 748 749 /* yay! */ 750 out[rep] = item; 751 left--; 752 break; 753 } 754 } 755 } 756 for (rep = outpos; rep < endpos; rep++) { 757 if (out[rep] == CRUSH_ITEM_UNDEF) { 758 out[rep] = CRUSH_ITEM_NONE; 759 } 760 if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) { 761 out2[rep] = CRUSH_ITEM_NONE; 762 } 763 } 764 #ifndef __KERNEL__ 765 if (map->choose_tries && ftotal <= map->choose_total_tries) 766 map->choose_tries[ftotal]++; 767 #endif 768 #ifdef DEBUG_INDEP 769 if (out2) { 770 dprintk("%u %d a: ", ftotal, left); 771 for (rep = outpos; rep < endpos; rep++) { 772 dprintk(" %d", out[rep]); 773 } 774 dprintk("\n"); 775 dprintk("%u %d b: ", ftotal, left); 776 for (rep = outpos; rep < endpos; rep++) { 777 dprintk(" %d", out2[rep]); 778 } 779 dprintk("\n"); 780 } 781 #endif 782 } 783 784 /** 785 * crush_do_rule - calculate a mapping with the given input and rule 786 * @map: the crush_map 787 * @ruleno: the rule id 788 * @x: hash input 789 * @result: pointer to result vector 790 * @result_max: maximum result size 791 * @weight: weight vector (for map leaves) 792 * @weight_max: size of weight vector 793 * @scratch: scratch vector for private use; must be >= 3 * result_max 794 */ 795 int crush_do_rule(const struct crush_map *map, 796 int ruleno, int x, int *result, int result_max, 797 const __u32 *weight, int weight_max, 798 int *scratch) 799 { 800 int result_len; 801 int *a = scratch; 802 int *b = scratch + result_max; 803 int *c = scratch + result_max*2; 804 int recurse_to_leaf; 805 int *w; 806 int wsize = 0; 807 int *o; 808 int osize; 809 int *tmp; 810 struct crush_rule *rule; 811 __u32 step; 812 int i, j; 813 int numrep; 814 int out_size; 815 /* 816 * the original choose_total_tries value was off by one (it 817 * counted "retries" and not "tries"). add one. 818 */ 819 int choose_tries = map->choose_total_tries + 1; 820 int choose_leaf_tries = 0; 821 /* 822 * the local tries values were counted as "retries", though, 823 * and need no adjustment 824 */ 825 int choose_local_retries = map->choose_local_tries; 826 int choose_local_fallback_retries = map->choose_local_fallback_tries; 827 828 int vary_r = map->chooseleaf_vary_r; 829 int stable = map->chooseleaf_stable; 830 831 if ((__u32)ruleno >= map->max_rules) { 832 dprintk(" bad ruleno %d\n", ruleno); 833 return 0; 834 } 835 836 rule = map->rules[ruleno]; 837 result_len = 0; 838 w = a; 839 o = b; 840 841 for (step = 0; step < rule->len; step++) { 842 int firstn = 0; 843 struct crush_rule_step *curstep = &rule->steps[step]; 844 845 switch (curstep->op) { 846 case CRUSH_RULE_TAKE: 847 if ((curstep->arg1 >= 0 && 848 curstep->arg1 < map->max_devices) || 849 (-1-curstep->arg1 >= 0 && 850 -1-curstep->arg1 < map->max_buckets && 851 map->buckets[-1-curstep->arg1])) { 852 w[0] = curstep->arg1; 853 wsize = 1; 854 } else { 855 dprintk(" bad take value %d\n", curstep->arg1); 856 } 857 break; 858 859 case CRUSH_RULE_SET_CHOOSE_TRIES: 860 if (curstep->arg1 > 0) 861 choose_tries = curstep->arg1; 862 break; 863 864 case CRUSH_RULE_SET_CHOOSELEAF_TRIES: 865 if (curstep->arg1 > 0) 866 choose_leaf_tries = curstep->arg1; 867 break; 868 869 case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES: 870 if (curstep->arg1 >= 0) 871 choose_local_retries = curstep->arg1; 872 break; 873 874 case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES: 875 if (curstep->arg1 >= 0) 876 choose_local_fallback_retries = curstep->arg1; 877 break; 878 879 case CRUSH_RULE_SET_CHOOSELEAF_VARY_R: 880 if (curstep->arg1 >= 0) 881 vary_r = curstep->arg1; 882 break; 883 884 case CRUSH_RULE_SET_CHOOSELEAF_STABLE: 885 if (curstep->arg1 >= 0) 886 stable = curstep->arg1; 887 break; 888 889 case CRUSH_RULE_CHOOSELEAF_FIRSTN: 890 case CRUSH_RULE_CHOOSE_FIRSTN: 891 firstn = 1; 892 /* fall through */ 893 case CRUSH_RULE_CHOOSELEAF_INDEP: 894 case CRUSH_RULE_CHOOSE_INDEP: 895 if (wsize == 0) 896 break; 897 898 recurse_to_leaf = 899 curstep->op == 900 CRUSH_RULE_CHOOSELEAF_FIRSTN || 901 curstep->op == 902 CRUSH_RULE_CHOOSELEAF_INDEP; 903 904 /* reset output */ 905 osize = 0; 906 907 for (i = 0; i < wsize; i++) { 908 int bno; 909 /* 910 * see CRUSH_N, CRUSH_N_MINUS macros. 911 * basically, numrep <= 0 means relative to 912 * the provided result_max 913 */ 914 numrep = curstep->arg1; 915 if (numrep <= 0) { 916 numrep += result_max; 917 if (numrep <= 0) 918 continue; 919 } 920 j = 0; 921 /* make sure bucket id is valid */ 922 bno = -1 - w[i]; 923 if (bno < 0 || bno >= map->max_buckets) { 924 /* w[i] is probably CRUSH_ITEM_NONE */ 925 dprintk(" bad w[i] %d\n", w[i]); 926 continue; 927 } 928 if (firstn) { 929 int recurse_tries; 930 if (choose_leaf_tries) 931 recurse_tries = 932 choose_leaf_tries; 933 else if (map->chooseleaf_descend_once) 934 recurse_tries = 1; 935 else 936 recurse_tries = choose_tries; 937 osize += crush_choose_firstn( 938 map, 939 map->buckets[bno], 940 weight, weight_max, 941 x, numrep, 942 curstep->arg2, 943 o+osize, j, 944 result_max-osize, 945 choose_tries, 946 recurse_tries, 947 choose_local_retries, 948 choose_local_fallback_retries, 949 recurse_to_leaf, 950 vary_r, 951 stable, 952 c+osize, 953 0); 954 } else { 955 out_size = ((numrep < (result_max-osize)) ? 956 numrep : (result_max-osize)); 957 crush_choose_indep( 958 map, 959 map->buckets[bno], 960 weight, weight_max, 961 x, out_size, numrep, 962 curstep->arg2, 963 o+osize, j, 964 choose_tries, 965 choose_leaf_tries ? 966 choose_leaf_tries : 1, 967 recurse_to_leaf, 968 c+osize, 969 0); 970 osize += out_size; 971 } 972 } 973 974 if (recurse_to_leaf) 975 /* copy final _leaf_ values to output set */ 976 memcpy(o, c, osize*sizeof(*o)); 977 978 /* swap o and w arrays */ 979 tmp = o; 980 o = w; 981 w = tmp; 982 wsize = osize; 983 break; 984 985 986 case CRUSH_RULE_EMIT: 987 for (i = 0; i < wsize && result_len < result_max; i++) { 988 result[result_len] = w[i]; 989 result_len++; 990 } 991 wsize = 0; 992 break; 993 994 default: 995 dprintk(" unknown op %d at step %d\n", 996 curstep->op, step); 997 break; 998 } 999 } 1000 return result_len; 1001 } 1002