3 # include <linux/string.h>
4 # include <linux/slab.h>
5 # include <linux/bug.h>
6 # include <linux/kernel.h>
8 # define dprintk(args...)
15 # define BUG_ON(x) assert(!(x))
16 # define dprintk(args...) /* printf(args) */
17 # define kmalloc(x, f) malloc(x)
18 # define kfree(x) free(x)
21 #include <linux/crush/crush.h>
22 #include <linux/crush/hash.h>
25 * Implement the core CRUSH mapping algorithm.
29 * crush_find_rule - find a crush_rule id for a given ruleset, type, and size.
31 * @ruleset: the storage ruleset id (user defined)
32 * @type: storage ruleset type (user defined)
33 * @size: output set size
35 int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
39 for (i = 0; i < map->max_rules; i++) {
41 map->rules[i]->mask.ruleset == ruleset &&
42 map->rules[i]->mask.type == type &&
43 map->rules[i]->mask.min_size <= size &&
44 map->rules[i]->mask.max_size >= size)
52 * bucket choose methods
54 * For each bucket algorithm, we have a "choose" method that, given a
55 * crush input @x and replica position (usually, position in output set) @r,
56 * will produce an item in the bucket.
60 * Choose based on a random permutation of the bucket.
62 * We used to use some prime number arithmetic to do this, but it
63 * wasn't very random, and had some other bad behaviors. Instead, we
64 * calculate an actual random permutation of the bucket members.
65 * Since this is expensive, we optimize for the r=0 case, which
66 * captures the vast majority of calls.
68 static int bucket_perm_choose(struct crush_bucket *bucket,
71 unsigned int pr = r % bucket->size;
74 /* start a new permutation if @x has changed */
75 if (bucket->perm_x != (__u32)x || bucket->perm_n == 0) {
76 dprintk("bucket %d new x=%d\n", bucket->id, x);
79 /* optimize common r=0 case */
81 s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
84 bucket->perm_n = 0xffff; /* magic value, see below */
88 for (i = 0; i < bucket->size; i++)
91 } else if (bucket->perm_n == 0xffff) {
92 /* clean up after the r=0 case above */
93 for (i = 1; i < bucket->size; i++)
95 bucket->perm[bucket->perm[0]] = 0;
99 /* calculate permutation up to pr */
100 for (i = 0; i < bucket->perm_n; i++)
101 dprintk(" perm_choose have %d: %d\n", i, bucket->perm[i]);
102 while (bucket->perm_n <= pr) {
103 unsigned int p = bucket->perm_n;
104 /* no point in swapping the final entry */
105 if (p < bucket->size - 1) {
106 i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
109 unsigned int t = bucket->perm[p + i];
110 bucket->perm[p + i] = bucket->perm[p];
113 dprintk(" perm_choose swap %d with %d\n", p, p+i);
117 for (i = 0; i < bucket->size; i++)
118 dprintk(" perm_choose %d: %d\n", i, bucket->perm[i]);
120 s = bucket->perm[pr];
122 dprintk(" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->id,
123 bucket->size, x, r, pr, s);
124 return bucket->items[s];
128 static int bucket_uniform_choose(struct crush_bucket_uniform *bucket,
131 return bucket_perm_choose(&bucket->h, x, r);
135 static int bucket_list_choose(struct crush_bucket_list *bucket,
140 for (i = bucket->h.size-1; i >= 0; i--) {
141 __u64 w = crush_hash32_4(bucket->h.hash,x, bucket->h.items[i],
144 dprintk("list_choose i=%d x=%d r=%d item %d weight %x "
146 i, x, r, bucket->h.items[i], bucket->item_weights[i],
147 bucket->sum_weights[i], w);
148 w *= bucket->sum_weights[i];
150 /*dprintk(" scaled %llx\n", w);*/
151 if (w < bucket->item_weights[i])
152 return bucket->h.items[i];
155 dprintk("bad list sums for bucket %d\n", bucket->h.id);
156 return bucket->h.items[0];
161 static int height(int n)
164 while ((n & 1) == 0) {
171 static int left(int x)
174 return x - (1 << (h-1));
177 static int right(int x)
180 return x + (1 << (h-1));
183 static int terminal(int x)
188 static int bucket_tree_choose(struct crush_bucket_tree *bucket,
196 n = bucket->num_nodes >> 1;
198 while (!terminal(n)) {
200 /* pick point in [0, w) */
201 w = bucket->node_weights[n];
202 t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
203 bucket->h.id) * (__u64)w;
206 /* descend to the left or right? */
208 if (t < bucket->node_weights[l])
214 return bucket->h.items[n >> 1];
220 static int bucket_straw_choose(struct crush_bucket_straw *bucket,
228 for (i = 0; i < bucket->h.size; i++) {
229 draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
231 draw *= bucket->straws[i];
232 if (i == 0 || draw > high_draw) {
237 return bucket->h.items[high];
240 static int crush_bucket_choose(struct crush_bucket *in, int x, int r)
242 dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
243 BUG_ON(in->size == 0);
245 case CRUSH_BUCKET_UNIFORM:
246 return bucket_uniform_choose((struct crush_bucket_uniform *)in,
248 case CRUSH_BUCKET_LIST:
249 return bucket_list_choose((struct crush_bucket_list *)in,
251 case CRUSH_BUCKET_TREE:
252 return bucket_tree_choose((struct crush_bucket_tree *)in,
254 case CRUSH_BUCKET_STRAW:
255 return bucket_straw_choose((struct crush_bucket_straw *)in,
258 dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
264 * true if device is marked "out" (failed, fully offloaded)
267 static int is_out(const struct crush_map *map,
268 const __u32 *weight, int weight_max,
271 if (item >= weight_max)
273 if (weight[item] >= 0x10000)
275 if (weight[item] == 0)
277 if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
284 * crush_choose_firstn - choose numrep distinct items of given type
285 * @map: the crush_map
286 * @bucket: the bucket we are choose an item from
287 * @x: crush input value
288 * @numrep: the number of items to choose
289 * @type: the type of item to choose
290 * @out: pointer to output vector
291 * @outpos: our position in that vector
292 * @tries: number of attempts to make
293 * @recurse_tries: number of attempts to have recursive chooseleaf make
294 * @local_retries: localized retries
295 * @local_fallback_retries: localized fallback retries
296 * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose)
297 * @vary_r: pass r to recursive calls
298 * @out2: second output vector for leaf items (if @recurse_to_leaf)
299 * @parent_r: r value passed from the parent
301 static int crush_choose_firstn(const struct crush_map *map,
302 struct crush_bucket *bucket,
303 const __u32 *weight, int weight_max,
304 int x, int numrep, int type,
305 int *out, int outpos,
307 unsigned int recurse_tries,
308 unsigned int local_retries,
309 unsigned int local_fallback_retries,
316 unsigned int ftotal, flocal;
317 int retry_descent, retry_bucket, skip_rep;
318 struct crush_bucket *in = bucket;
325 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\n",
326 recurse_to_leaf ? "_LEAF" : "",
327 bucket->id, x, outpos, numrep,
328 tries, recurse_tries, local_retries, local_fallback_retries,
331 for (rep = outpos; rep < numrep; rep++) {
332 /* keep trying until we get a non-out, non-colliding item */
337 in = bucket; /* initial bucket */
339 /* choose through intervening buckets */
345 /* r' = r + f_total */
353 if (local_fallback_retries > 0 &&
354 flocal >= (in->size>>1) &&
355 flocal > local_fallback_retries)
356 item = bucket_perm_choose(in, x, r);
358 item = crush_bucket_choose(in, x, r);
359 if (item >= map->max_devices) {
360 dprintk(" bad item %d\n", item);
367 itemtype = map->buckets[-1-item]->type;
370 dprintk(" item %d type %d\n", item, itemtype);
373 if (itemtype != type) {
375 (-1-item) >= map->max_buckets) {
376 dprintk(" bad item type %d\n", type);
380 in = map->buckets[-1-item];
386 for (i = 0; i < outpos; i++) {
387 if (out[i] == item) {
394 if (!collide && recurse_to_leaf) {
398 sub_r = r >> (vary_r-1);
401 if (crush_choose_firstn(map,
402 map->buckets[-1-item],
408 local_fallback_retries,
413 /* didn't get leaf */
416 /* we already have a leaf! */
424 reject = is_out(map, weight,
432 if (reject || collide) {
436 if (collide && flocal <= local_retries)
437 /* retry locally a few times */
439 else if (local_fallback_retries > 0 &&
440 flocal <= in->size + local_fallback_retries)
441 /* exhaustive bucket search */
443 else if (ftotal < tries)
444 /* then retry descent */
449 dprintk(" reject %d collide %d "
450 "ftotal %u flocal %u\n",
451 reject, collide, ftotal,
454 } while (retry_bucket);
455 } while (retry_descent);
458 dprintk("skip rep\n");
462 dprintk("CHOOSE got %d\n", item);
467 dprintk("CHOOSE returns %d\n", outpos);
473 * crush_choose_indep: alternative breadth-first positionally stable mapping
476 static void crush_choose_indep(const struct crush_map *map,
477 struct crush_bucket *bucket,
478 const __u32 *weight, int weight_max,
479 int x, int left, int numrep, int type,
480 int *out, int outpos,
482 unsigned int recurse_tries,
487 struct crush_bucket *in = bucket;
488 int endpos = outpos + left;
497 dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
498 bucket->id, x, outpos, numrep);
500 /* initially my result is undefined */
501 for (rep = outpos; rep < endpos; rep++) {
502 out[rep] = CRUSH_ITEM_UNDEF;
504 out2[rep] = CRUSH_ITEM_UNDEF;
507 for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) {
508 for (rep = outpos; rep < endpos; rep++) {
509 if (out[rep] != CRUSH_ITEM_UNDEF)
512 in = bucket; /* initial bucket */
514 /* choose through intervening buckets */
516 /* note: we base the choice on the position
517 * even in the nested call. that means that
518 * if the first layer chooses the same bucket
519 * in a different position, we will tend to
520 * choose a different item in that bucket.
521 * this will involve more devices in data
522 * movement and tend to distribute the load.
527 if (in->alg == CRUSH_BUCKET_UNIFORM &&
528 in->size % numrep == 0)
529 /* r'=r+(n+1)*f_total */
530 r += (numrep+1) * ftotal;
532 /* r' = r + n*f_total */
533 r += numrep * ftotal;
537 dprintk(" empty bucket\n");
541 item = crush_bucket_choose(in, x, r);
542 if (item >= map->max_devices) {
543 dprintk(" bad item %d\n", item);
544 out[rep] = CRUSH_ITEM_NONE;
546 out2[rep] = CRUSH_ITEM_NONE;
553 itemtype = map->buckets[-1-item]->type;
556 dprintk(" item %d type %d\n", item, itemtype);
559 if (itemtype != type) {
561 (-1-item) >= map->max_buckets) {
562 dprintk(" bad item type %d\n", type);
563 out[rep] = CRUSH_ITEM_NONE;
570 in = map->buckets[-1-item];
576 for (i = outpos; i < endpos; i++) {
577 if (out[i] == item) {
585 if (recurse_to_leaf) {
587 crush_choose_indep(map,
588 map->buckets[-1-item],
594 if (out2[rep] == CRUSH_ITEM_NONE) {
595 /* placed nothing; no leaf */
599 /* we already have a leaf! */
606 is_out(map, weight, weight_max, item, x))
616 for (rep = outpos; rep < endpos; rep++) {
617 if (out[rep] == CRUSH_ITEM_UNDEF) {
618 out[rep] = CRUSH_ITEM_NONE;
620 if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) {
621 out2[rep] = CRUSH_ITEM_NONE;
627 * crush_do_rule - calculate a mapping with the given input and rule
628 * @map: the crush_map
629 * @ruleno: the rule id
631 * @result: pointer to result vector
632 * @result_max: maximum result size
633 * @weight: weight vector (for map leaves)
634 * @weight_max: size of weight vector
635 * @scratch: scratch vector for private use; must be >= 3 * result_max
637 int crush_do_rule(const struct crush_map *map,
638 int ruleno, int x, int *result, int result_max,
639 const __u32 *weight, int weight_max,
644 int *b = scratch + result_max;
645 int *c = scratch + result_max*2;
652 struct crush_rule *rule;
657 * the original choose_total_tries value was off by one (it
658 * counted "retries" and not "tries"). add one.
660 int choose_tries = map->choose_total_tries + 1;
661 int choose_leaf_tries = 0;
663 * the local tries values were counted as "retries", though,
664 * and need no adjustment
666 int choose_local_retries = map->choose_local_tries;
667 int choose_local_fallback_retries = map->choose_local_fallback_tries;
669 int vary_r = map->chooseleaf_vary_r;
671 if ((__u32)ruleno >= map->max_rules) {
672 dprintk(" bad ruleno %d\n", ruleno);
676 rule = map->rules[ruleno];
681 for (step = 0; step < rule->len; step++) {
683 struct crush_rule_step *curstep = &rule->steps[step];
685 switch (curstep->op) {
686 case CRUSH_RULE_TAKE:
687 w[0] = curstep->arg1;
691 case CRUSH_RULE_SET_CHOOSE_TRIES:
692 if (curstep->arg1 > 0)
693 choose_tries = curstep->arg1;
696 case CRUSH_RULE_SET_CHOOSELEAF_TRIES:
697 if (curstep->arg1 > 0)
698 choose_leaf_tries = curstep->arg1;
701 case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES:
702 if (curstep->arg1 >= 0)
703 choose_local_retries = curstep->arg1;
706 case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES:
707 if (curstep->arg1 >= 0)
708 choose_local_fallback_retries = curstep->arg1;
711 case CRUSH_RULE_SET_CHOOSELEAF_VARY_R:
712 if (curstep->arg1 >= 0)
713 vary_r = curstep->arg1;
716 case CRUSH_RULE_CHOOSELEAF_FIRSTN:
717 case CRUSH_RULE_CHOOSE_FIRSTN:
720 case CRUSH_RULE_CHOOSELEAF_INDEP:
721 case CRUSH_RULE_CHOOSE_INDEP:
727 CRUSH_RULE_CHOOSELEAF_FIRSTN ||
729 CRUSH_RULE_CHOOSELEAF_INDEP;
734 for (i = 0; i < wsize; i++) {
736 * see CRUSH_N, CRUSH_N_MINUS macros.
737 * basically, numrep <= 0 means relative to
738 * the provided result_max
740 numrep = curstep->arg1;
742 numrep += result_max;
749 if (choose_leaf_tries)
752 else if (map->chooseleaf_descend_once)
755 recurse_tries = choose_tries;
756 osize += crush_choose_firstn(
758 map->buckets[-1-w[i]],
765 choose_local_retries,
766 choose_local_fallback_retries,
774 map->buckets[-1-w[i]],
781 choose_leaf_tries : 1,
790 /* copy final _leaf_ values to output set */
791 memcpy(o, c, osize*sizeof(*o));
793 /* swap o and w arrays */
801 case CRUSH_RULE_EMIT:
802 for (i = 0; i < wsize && result_len < result_max; i++) {
803 result[result_len] = w[i];
810 dprintk(" unknown op %d at step %d\n",