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 "crush_ln_table.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 // compute 2^44*log2(input+1)
242 uint64_t crush_ln(unsigned xin
)
245 int iexpon
, index1
, index2
;
246 uint64_t RH
, LH
, LL
, xl64
, result
;
252 while(!(x
&0x18000)) { x
<<=1; iexpon
--; }
256 RH
= __RH_LH_tbl
[index1
- 256];
257 // LH ~ 2^48 * log2(index1/256)
258 LH
= __RH_LH_tbl
[index1
+ 1 - 256];
260 // RH*x ~ 2^48 * (2^15 + xf), xf<2^8
261 xl64
= (int64_t)x
* RH
;
266 result
<<= (12 + 32);
269 // LL ~ 2^48*log2(1.0+index2/2^15)
270 LL
= __LL_tbl
[index2
];
284 * for reference, see:
286 * http://en.wikipedia.org/wiki/Exponential_distribution#Distribution_of_the_minimum_of_exponential_random_variables
290 static int bucket_straw2_choose(struct crush_bucket_straw2
*bucket
,
293 unsigned i
, high
= 0;
296 __s64 ln
, draw
, high_draw
= 0;
298 for (i
= 0; i
< bucket
->h
.size
; i
++) {
299 w
= bucket
->item_weights
[i
];
301 u
= crush_hash32_3(bucket
->h
.hash
, x
,
302 bucket
->h
.items
[i
], r
);
306 * for some reason slightly less than 0x10000 produces
307 * a slightly more accurate distribution... probably a
310 * the natural log lookup table maps [0,0xffff]
311 * (corresponding to real numbers [1/0x10000, 1] to
312 * [0, 0xffffffffffff] (corresponding to real numbers
315 ln
= crush_ln(u
) - 0x1000000000000ll
;
318 * divide by 16.16 fixed-point weight. note
319 * that the ln value is negative, so a larger
320 * weight means a larger (less negative) value
323 draw
= div64_s64(ln
, w
);
328 if (i
== 0 || draw
> high_draw
) {
333 return bucket
->h
.items
[high
];
337 static int crush_bucket_choose(struct crush_bucket
*in
, int x
, int r
)
339 dprintk(" crush_bucket_choose %d x=%d r=%d\n", in
->id
, x
, r
);
340 BUG_ON(in
->size
== 0);
342 case CRUSH_BUCKET_UNIFORM
:
343 return bucket_uniform_choose((struct crush_bucket_uniform
*)in
,
345 case CRUSH_BUCKET_LIST
:
346 return bucket_list_choose((struct crush_bucket_list
*)in
,
348 case CRUSH_BUCKET_TREE
:
349 return bucket_tree_choose((struct crush_bucket_tree
*)in
,
351 case CRUSH_BUCKET_STRAW
:
352 return bucket_straw_choose((struct crush_bucket_straw
*)in
,
354 case CRUSH_BUCKET_STRAW2
:
355 return bucket_straw2_choose((struct crush_bucket_straw2
*)in
,
358 dprintk("unknown bucket %d alg %d\n", in
->id
, in
->alg
);
365 * true if device is marked "out" (failed, fully offloaded)
368 static int is_out(const struct crush_map
*map
,
369 const __u32
*weight
, int weight_max
,
372 if (item
>= weight_max
)
374 if (weight
[item
] >= 0x10000)
376 if (weight
[item
] == 0)
378 if ((crush_hash32_2(CRUSH_HASH_RJENKINS1
, x
, item
) & 0xffff)
385 * crush_choose_firstn - choose numrep distinct items of given type
386 * @map: the crush_map
387 * @bucket: the bucket we are choose an item from
388 * @x: crush input value
389 * @numrep: the number of items to choose
390 * @type: the type of item to choose
391 * @out: pointer to output vector
392 * @outpos: our position in that vector
393 * @out_size: size of the out vector
394 * @tries: number of attempts to make
395 * @recurse_tries: number of attempts to have recursive chooseleaf make
396 * @local_retries: localized retries
397 * @local_fallback_retries: localized fallback retries
398 * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose)
399 * @vary_r: pass r to recursive calls
400 * @out2: second output vector for leaf items (if @recurse_to_leaf)
401 * @parent_r: r value passed from the parent
403 static int crush_choose_firstn(const struct crush_map
*map
,
404 struct crush_bucket
*bucket
,
405 const __u32
*weight
, int weight_max
,
406 int x
, int numrep
, int type
,
407 int *out
, int outpos
,
410 unsigned int recurse_tries
,
411 unsigned int local_retries
,
412 unsigned int local_fallback_retries
,
419 unsigned int ftotal
, flocal
;
420 int retry_descent
, retry_bucket
, skip_rep
;
421 struct crush_bucket
*in
= bucket
;
427 int count
= out_size
;
429 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",
430 recurse_to_leaf
? "_LEAF" : "",
431 bucket
->id
, x
, outpos
, numrep
,
432 tries
, recurse_tries
, local_retries
, local_fallback_retries
,
435 for (rep
= outpos
; rep
< numrep
&& count
> 0 ; rep
++) {
436 /* keep trying until we get a non-out, non-colliding item */
441 in
= bucket
; /* initial bucket */
443 /* choose through intervening buckets */
449 /* r' = r + f_total */
457 if (local_fallback_retries
> 0 &&
458 flocal
>= (in
->size
>>1) &&
459 flocal
> local_fallback_retries
)
460 item
= bucket_perm_choose(in
, x
, r
);
462 item
= crush_bucket_choose(in
, x
, r
);
463 if (item
>= map
->max_devices
) {
464 dprintk(" bad item %d\n", item
);
471 itemtype
= map
->buckets
[-1-item
]->type
;
474 dprintk(" item %d type %d\n", item
, itemtype
);
477 if (itemtype
!= type
) {
479 (-1-item
) >= map
->max_buckets
) {
480 dprintk(" bad item type %d\n", type
);
484 in
= map
->buckets
[-1-item
];
490 for (i
= 0; i
< outpos
; i
++) {
491 if (out
[i
] == item
) {
498 if (!collide
&& recurse_to_leaf
) {
502 sub_r
= r
>> (vary_r
-1);
505 if (crush_choose_firstn(map
,
506 map
->buckets
[-1-item
],
512 local_fallback_retries
,
517 /* didn't get leaf */
520 /* we already have a leaf! */
528 reject
= is_out(map
, weight
,
536 if (reject
|| collide
) {
540 if (collide
&& flocal
<= local_retries
)
541 /* retry locally a few times */
543 else if (local_fallback_retries
> 0 &&
544 flocal
<= in
->size
+ local_fallback_retries
)
545 /* exhaustive bucket search */
547 else if (ftotal
< tries
)
548 /* then retry descent */
553 dprintk(" reject %d collide %d "
554 "ftotal %u flocal %u\n",
555 reject
, collide
, ftotal
,
558 } while (retry_bucket
);
559 } while (retry_descent
);
562 dprintk("skip rep\n");
566 dprintk("CHOOSE got %d\n", item
);
572 dprintk("CHOOSE returns %d\n", outpos
);
578 * crush_choose_indep: alternative breadth-first positionally stable mapping
581 static void crush_choose_indep(const struct crush_map
*map
,
582 struct crush_bucket
*bucket
,
583 const __u32
*weight
, int weight_max
,
584 int x
, int left
, int numrep
, int type
,
585 int *out
, int outpos
,
587 unsigned int recurse_tries
,
592 struct crush_bucket
*in
= bucket
;
593 int endpos
= outpos
+ left
;
602 dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf
? "_LEAF" : "",
603 bucket
->id
, x
, outpos
, numrep
);
605 /* initially my result is undefined */
606 for (rep
= outpos
; rep
< endpos
; rep
++) {
607 out
[rep
] = CRUSH_ITEM_UNDEF
;
609 out2
[rep
] = CRUSH_ITEM_UNDEF
;
612 for (ftotal
= 0; left
> 0 && ftotal
< tries
; ftotal
++) {
613 for (rep
= outpos
; rep
< endpos
; rep
++) {
614 if (out
[rep
] != CRUSH_ITEM_UNDEF
)
617 in
= bucket
; /* initial bucket */
619 /* choose through intervening buckets */
621 /* note: we base the choice on the position
622 * even in the nested call. that means that
623 * if the first layer chooses the same bucket
624 * in a different position, we will tend to
625 * choose a different item in that bucket.
626 * this will involve more devices in data
627 * movement and tend to distribute the load.
632 if (in
->alg
== CRUSH_BUCKET_UNIFORM
&&
633 in
->size
% numrep
== 0)
634 /* r'=r+(n+1)*f_total */
635 r
+= (numrep
+1) * ftotal
;
637 /* r' = r + n*f_total */
638 r
+= numrep
* ftotal
;
642 dprintk(" empty bucket\n");
646 item
= crush_bucket_choose(in
, x
, r
);
647 if (item
>= map
->max_devices
) {
648 dprintk(" bad item %d\n", item
);
649 out
[rep
] = CRUSH_ITEM_NONE
;
651 out2
[rep
] = CRUSH_ITEM_NONE
;
658 itemtype
= map
->buckets
[-1-item
]->type
;
661 dprintk(" item %d type %d\n", item
, itemtype
);
664 if (itemtype
!= type
) {
666 (-1-item
) >= map
->max_buckets
) {
667 dprintk(" bad item type %d\n", type
);
668 out
[rep
] = CRUSH_ITEM_NONE
;
675 in
= map
->buckets
[-1-item
];
681 for (i
= outpos
; i
< endpos
; i
++) {
682 if (out
[i
] == item
) {
690 if (recurse_to_leaf
) {
692 crush_choose_indep(map
,
693 map
->buckets
[-1-item
],
699 if (out2
[rep
] == CRUSH_ITEM_NONE
) {
700 /* placed nothing; no leaf */
704 /* we already have a leaf! */
711 is_out(map
, weight
, weight_max
, item
, x
))
721 for (rep
= outpos
; rep
< endpos
; rep
++) {
722 if (out
[rep
] == CRUSH_ITEM_UNDEF
) {
723 out
[rep
] = CRUSH_ITEM_NONE
;
725 if (out2
&& out2
[rep
] == CRUSH_ITEM_UNDEF
) {
726 out2
[rep
] = CRUSH_ITEM_NONE
;
732 * crush_do_rule - calculate a mapping with the given input and rule
733 * @map: the crush_map
734 * @ruleno: the rule id
736 * @result: pointer to result vector
737 * @result_max: maximum result size
738 * @weight: weight vector (for map leaves)
739 * @weight_max: size of weight vector
740 * @scratch: scratch vector for private use; must be >= 3 * result_max
742 int crush_do_rule(const struct crush_map
*map
,
743 int ruleno
, int x
, int *result
, int result_max
,
744 const __u32
*weight
, int weight_max
,
749 int *b
= scratch
+ result_max
;
750 int *c
= scratch
+ result_max
*2;
757 struct crush_rule
*rule
;
763 * the original choose_total_tries value was off by one (it
764 * counted "retries" and not "tries"). add one.
766 int choose_tries
= map
->choose_total_tries
+ 1;
767 int choose_leaf_tries
= 0;
769 * the local tries values were counted as "retries", though,
770 * and need no adjustment
772 int choose_local_retries
= map
->choose_local_tries
;
773 int choose_local_fallback_retries
= map
->choose_local_fallback_tries
;
775 int vary_r
= map
->chooseleaf_vary_r
;
777 if ((__u32
)ruleno
>= map
->max_rules
) {
778 dprintk(" bad ruleno %d\n", ruleno
);
782 rule
= map
->rules
[ruleno
];
787 for (step
= 0; step
< rule
->len
; step
++) {
789 struct crush_rule_step
*curstep
= &rule
->steps
[step
];
791 switch (curstep
->op
) {
792 case CRUSH_RULE_TAKE
:
793 w
[0] = curstep
->arg1
;
797 case CRUSH_RULE_SET_CHOOSE_TRIES
:
798 if (curstep
->arg1
> 0)
799 choose_tries
= curstep
->arg1
;
802 case CRUSH_RULE_SET_CHOOSELEAF_TRIES
:
803 if (curstep
->arg1
> 0)
804 choose_leaf_tries
= curstep
->arg1
;
807 case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES
:
808 if (curstep
->arg1
>= 0)
809 choose_local_retries
= curstep
->arg1
;
812 case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES
:
813 if (curstep
->arg1
>= 0)
814 choose_local_fallback_retries
= curstep
->arg1
;
817 case CRUSH_RULE_SET_CHOOSELEAF_VARY_R
:
818 if (curstep
->arg1
>= 0)
819 vary_r
= curstep
->arg1
;
822 case CRUSH_RULE_CHOOSELEAF_FIRSTN
:
823 case CRUSH_RULE_CHOOSE_FIRSTN
:
826 case CRUSH_RULE_CHOOSELEAF_INDEP
:
827 case CRUSH_RULE_CHOOSE_INDEP
:
833 CRUSH_RULE_CHOOSELEAF_FIRSTN
||
835 CRUSH_RULE_CHOOSELEAF_INDEP
;
840 for (i
= 0; i
< wsize
; i
++) {
842 * see CRUSH_N, CRUSH_N_MINUS macros.
843 * basically, numrep <= 0 means relative to
844 * the provided result_max
846 numrep
= curstep
->arg1
;
848 numrep
+= result_max
;
855 if (choose_leaf_tries
)
858 else if (map
->chooseleaf_descend_once
)
861 recurse_tries
= choose_tries
;
862 osize
+= crush_choose_firstn(
864 map
->buckets
[-1-w
[i
]],
872 choose_local_retries
,
873 choose_local_fallback_retries
,
879 out_size
= ((numrep
< (result_max
-osize
)) ?
880 numrep
: (result_max
-osize
));
883 map
->buckets
[-1-w
[i
]],
890 choose_leaf_tries
: 1,
899 /* copy final _leaf_ values to output set */
900 memcpy(o
, c
, osize
*sizeof(*o
));
902 /* swap o and w arrays */
910 case CRUSH_RULE_EMIT
:
911 for (i
= 0; i
< wsize
&& result_len
< result_max
; i
++) {
912 result
[result_len
] = w
[i
];
919 dprintk(" unknown op %d at step %d\n",