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>
23 #include <linux/crush/mapper.h>
26 * Implement the core CRUSH mapping algorithm.
30 * crush_find_rule - find a crush_rule id for a given ruleset, type, and size.
32 * @ruleset: the storage ruleset id (user defined)
33 * @type: storage ruleset type (user defined)
34 * @size: output set size
36 int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
40 for (i = 0; i < map->max_rules; i++) {
42 map->rules[i]->mask.ruleset == ruleset &&
43 map->rules[i]->mask.type == type &&
44 map->rules[i]->mask.min_size <= size &&
45 map->rules[i]->mask.max_size >= size)
53 * bucket choose methods
55 * For each bucket algorithm, we have a "choose" method that, given a
56 * crush input @x and replica position (usually, position in output set) @r,
57 * will produce an item in the bucket.
61 * Choose based on a random permutation of the bucket.
63 * We used to use some prime number arithmetic to do this, but it
64 * wasn't very random, and had some other bad behaviors. Instead, we
65 * calculate an actual random permutation of the bucket members.
66 * Since this is expensive, we optimize for the r=0 case, which
67 * captures the vast majority of calls.
69 static int bucket_perm_choose(struct crush_bucket *bucket,
72 unsigned int pr = r % bucket->size;
75 /* start a new permutation if @x has changed */
76 if (bucket->perm_x != (__u32)x || bucket->perm_n == 0) {
77 dprintk("bucket %d new x=%d\n", bucket->id, x);
80 /* optimize common r=0 case */
82 s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
85 bucket->perm_n = 0xffff; /* magic value, see below */
89 for (i = 0; i < bucket->size; i++)
92 } else if (bucket->perm_n == 0xffff) {
93 /* clean up after the r=0 case above */
94 for (i = 1; i < bucket->size; i++)
96 bucket->perm[bucket->perm[0]] = 0;
100 /* calculate permutation up to pr */
101 for (i = 0; i < bucket->perm_n; i++)
102 dprintk(" perm_choose have %d: %d\n", i, bucket->perm[i]);
103 while (bucket->perm_n <= pr) {
104 unsigned int p = bucket->perm_n;
105 /* no point in swapping the final entry */
106 if (p < bucket->size - 1) {
107 i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
110 unsigned int t = bucket->perm[p + i];
111 bucket->perm[p + i] = bucket->perm[p];
114 dprintk(" perm_choose swap %d with %d\n", p, p+i);
118 for (i = 0; i < bucket->size; i++)
119 dprintk(" perm_choose %d: %d\n", i, bucket->perm[i]);
121 s = bucket->perm[pr];
123 dprintk(" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->id,
124 bucket->size, x, r, pr, s);
125 return bucket->items[s];
129 static int bucket_uniform_choose(struct crush_bucket_uniform *bucket,
132 return bucket_perm_choose(&bucket->h, x, r);
136 static int bucket_list_choose(struct crush_bucket_list *bucket,
141 for (i = bucket->h.size-1; i >= 0; i--) {
142 __u64 w = crush_hash32_4(bucket->h.hash,x, bucket->h.items[i],
145 dprintk("list_choose i=%d x=%d r=%d item %d weight %x "
147 i, x, r, bucket->h.items[i], bucket->item_weights[i],
148 bucket->sum_weights[i], w);
149 w *= bucket->sum_weights[i];
151 /*dprintk(" scaled %llx\n", w);*/
152 if (w < bucket->item_weights[i])
153 return bucket->h.items[i];
156 dprintk("bad list sums for bucket %d\n", bucket->h.id);
157 return bucket->h.items[0];
162 static int height(int n)
165 while ((n & 1) == 0) {
172 static int left(int x)
175 return x - (1 << (h-1));
178 static int right(int x)
181 return x + (1 << (h-1));
184 static int terminal(int x)
189 static int bucket_tree_choose(struct crush_bucket_tree *bucket,
197 n = bucket->num_nodes >> 1;
199 while (!terminal(n)) {
201 /* pick point in [0, w) */
202 w = bucket->node_weights[n];
203 t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
204 bucket->h.id) * (__u64)w;
207 /* descend to the left or right? */
209 if (t < bucket->node_weights[l])
215 return bucket->h.items[n >> 1];
221 static int bucket_straw_choose(struct crush_bucket_straw *bucket,
229 for (i = 0; i < bucket->h.size; i++) {
230 draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
232 draw *= bucket->straws[i];
233 if (i == 0 || draw > high_draw) {
238 return bucket->h.items[high];
241 static int crush_bucket_choose(struct crush_bucket *in, int x, int r)
243 dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
244 BUG_ON(in->size == 0);
246 case CRUSH_BUCKET_UNIFORM:
247 return bucket_uniform_choose((struct crush_bucket_uniform *)in,
249 case CRUSH_BUCKET_LIST:
250 return bucket_list_choose((struct crush_bucket_list *)in,
252 case CRUSH_BUCKET_TREE:
253 return bucket_tree_choose((struct crush_bucket_tree *)in,
255 case CRUSH_BUCKET_STRAW:
256 return bucket_straw_choose((struct crush_bucket_straw *)in,
259 dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
265 * true if device is marked "out" (failed, fully offloaded)
268 static int is_out(const struct crush_map *map,
269 const __u32 *weight, int weight_max,
272 if (item >= weight_max)
274 if (weight[item] >= 0x10000)
276 if (weight[item] == 0)
278 if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
285 * crush_choose_firstn - choose numrep distinct items of given type
286 * @map: the crush_map
287 * @bucket: the bucket we are choose an item from
288 * @x: crush input value
289 * @numrep: the number of items to choose
290 * @type: the type of item to choose
291 * @out: pointer to output vector
292 * @outpos: our position in that vector
293 * @recurse_to_leaf: true if we want one device under each item of given type
294 * @descend_once: true if we should only try one descent before giving up
295 * @out2: second output vector for leaf items (if @recurse_to_leaf)
297 static int crush_choose_firstn(const struct crush_map *map,
298 struct crush_bucket *bucket,
299 const __u32 *weight, int weight_max,
300 int x, int numrep, int type,
301 int *out, int outpos,
303 int descend_once, int *out2)
306 unsigned int ftotal, flocal;
307 int retry_descent, retry_bucket, skip_rep;
308 struct crush_bucket *in = bucket;
315 dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
316 bucket->id, x, outpos, numrep);
318 for (rep = outpos; rep < numrep; rep++) {
319 /* keep trying until we get a non-out, non-colliding item */
324 in = bucket; /* initial bucket */
326 /* choose through intervening buckets */
332 /* r' = r + f_total */
340 if (map->choose_local_fallback_tries > 0 &&
341 flocal >= (in->size>>1) &&
342 flocal > map->choose_local_fallback_tries)
343 item = bucket_perm_choose(in, x, r);
345 item = crush_bucket_choose(in, x, r);
346 if (item >= map->max_devices) {
347 dprintk(" bad item %d\n", item);
354 itemtype = map->buckets[-1-item]->type;
357 dprintk(" item %d type %d\n", item, itemtype);
360 if (itemtype != type) {
362 (-1-item) >= map->max_buckets) {
363 dprintk(" bad item type %d\n", type);
367 in = map->buckets[-1-item];
373 for (i = 0; i < outpos; i++) {
374 if (out[i] == item) {
381 if (!collide && recurse_to_leaf) {
383 if (crush_choose_firstn(map,
384 map->buckets[-1-item],
389 map->chooseleaf_descend_once,
391 /* didn't get leaf */
394 /* we already have a leaf! */
402 reject = is_out(map, weight,
410 if (reject || collide) {
414 if (reject && descend_once)
415 /* let outer call try again */
417 else if (collide && flocal <= map->choose_local_tries)
418 /* retry locally a few times */
420 else if (map->choose_local_fallback_tries > 0 &&
421 flocal <= in->size + map->choose_local_fallback_tries)
422 /* exhaustive bucket search */
424 else if (ftotal <= map->choose_total_tries)
425 /* then retry descent */
430 dprintk(" reject %d collide %d "
431 "ftotal %u flocal %u\n",
432 reject, collide, ftotal,
435 } while (retry_bucket);
436 } while (retry_descent);
439 dprintk("skip rep\n");
443 dprintk("CHOOSE got %d\n", item);
448 dprintk("CHOOSE returns %d\n", outpos);
454 * crush_choose_indep: alternative breadth-first positionally stable mapping
457 static void crush_choose_indep(const struct crush_map *map,
458 struct crush_bucket *bucket,
459 const __u32 *weight, int weight_max,
460 int x, int left, int numrep, int type,
461 int *out, int outpos,
466 struct crush_bucket *in = bucket;
467 int endpos = outpos + left;
476 dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
477 bucket->id, x, outpos, numrep);
479 /* initially my result is undefined */
480 for (rep = outpos; rep < endpos; rep++) {
481 out[rep] = CRUSH_ITEM_UNDEF;
483 out2[rep] = CRUSH_ITEM_UNDEF;
486 for (ftotal = 0; left > 0 && ftotal < map->choose_total_tries; ftotal++) {
487 for (rep = outpos; rep < endpos; rep++) {
488 if (out[rep] != CRUSH_ITEM_UNDEF)
491 in = bucket; /* initial bucket */
493 /* choose through intervening buckets */
495 /* note: we base the choice on the position
496 * even in the nested call. that means that
497 * if the first layer chooses the same bucket
498 * in a different position, we will tend to
499 * choose a different item in that bucket.
500 * this will involve more devices in data
501 * movement and tend to distribute the load.
506 if (in->alg == CRUSH_BUCKET_UNIFORM &&
507 in->size % numrep == 0)
508 /* r'=r+(n+1)*f_total */
509 r += (numrep+1) * ftotal;
511 /* r' = r + n*f_total */
512 r += numrep * ftotal;
516 dprintk(" empty bucket\n");
520 item = crush_bucket_choose(in, x, r);
521 if (item >= map->max_devices) {
522 dprintk(" bad item %d\n", item);
523 out[rep] = CRUSH_ITEM_NONE;
525 out2[rep] = CRUSH_ITEM_NONE;
532 itemtype = map->buckets[-1-item]->type;
535 dprintk(" item %d type %d\n", item, itemtype);
538 if (itemtype != type) {
540 (-1-item) >= map->max_buckets) {
541 dprintk(" bad item type %d\n", type);
542 out[rep] = CRUSH_ITEM_NONE;
549 in = map->buckets[-1-item];
555 for (i = outpos; i < endpos; i++) {
556 if (out[i] == item) {
564 if (recurse_to_leaf) {
566 crush_choose_indep(map,
567 map->buckets[-1-item],
572 if (out2[rep] == CRUSH_ITEM_NONE) {
573 /* placed nothing; no leaf */
577 /* we already have a leaf! */
584 is_out(map, weight, weight_max, item, x))
594 for (rep = outpos; rep < endpos; rep++) {
595 if (out[rep] == CRUSH_ITEM_UNDEF) {
596 out[rep] = CRUSH_ITEM_NONE;
598 if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) {
599 out2[rep] = CRUSH_ITEM_NONE;
605 * crush_do_rule - calculate a mapping with the given input and rule
606 * @map: the crush_map
607 * @ruleno: the rule id
609 * @result: pointer to result vector
610 * @result_max: maximum result size
611 * @weight: weight vector (for map leaves)
612 * @weight_max: size of weight vector
613 * @scratch: scratch vector for private use; must be >= 3 * result_max
615 int crush_do_rule(const struct crush_map *map,
616 int ruleno, int x, int *result, int result_max,
617 const __u32 *weight, int weight_max,
622 int *b = scratch + result_max;
623 int *c = scratch + result_max*2;
630 struct crush_rule *rule;
634 const int descend_once = 0;
636 if ((__u32)ruleno >= map->max_rules) {
637 dprintk(" bad ruleno %d\n", ruleno);
641 rule = map->rules[ruleno];
646 for (step = 0; step < rule->len; step++) {
648 struct crush_rule_step *curstep = &rule->steps[step];
650 switch (curstep->op) {
651 case CRUSH_RULE_TAKE:
652 w[0] = curstep->arg1;
656 case CRUSH_RULE_CHOOSE_LEAF_FIRSTN:
657 case CRUSH_RULE_CHOOSE_FIRSTN:
660 case CRUSH_RULE_CHOOSE_LEAF_INDEP:
661 case CRUSH_RULE_CHOOSE_INDEP:
667 CRUSH_RULE_CHOOSE_LEAF_FIRSTN ||
669 CRUSH_RULE_CHOOSE_LEAF_INDEP;
674 for (i = 0; i < wsize; i++) {
676 * see CRUSH_N, CRUSH_N_MINUS macros.
677 * basically, numrep <= 0 means relative to
678 * the provided result_max
680 numrep = curstep->arg1;
682 numrep += result_max;
688 osize += crush_choose_firstn(
690 map->buckets[-1-w[i]],
696 descend_once, c+osize);
700 map->buckets[-1-w[i]],
713 /* copy final _leaf_ values to output set */
714 memcpy(o, c, osize*sizeof(*o));
716 /* swap o and w arrays */
724 case CRUSH_RULE_EMIT:
725 for (i = 0; i < wsize && result_len < result_max; i++) {
726 result[result_len] = w[i];
733 dprintk(" unknown op %d at step %d\n",