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(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 pr
= r
% bucket
->size
;
74 /* start a new permutation if @x has changed */
75 if (bucket
->perm_x
!= 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 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 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
];
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
)) {
199 /* pick point in [0, w) */
200 w
= bucket
->node_weights
[n
];
201 t
= (__u64
)crush_hash32_4(bucket
->h
.hash
, x
, n
, r
,
202 bucket
->h
.id
) * (__u64
)w
;
205 /* descend to the left or right? */
207 if (t
< bucket
->node_weights
[l
])
213 return bucket
->h
.items
[n
>> 1];
219 static int bucket_straw_choose(struct crush_bucket_straw
*bucket
,
227 for (i
= 0; i
< bucket
->h
.size
; i
++) {
228 draw
= crush_hash32_3(bucket
->h
.hash
, x
, bucket
->h
.items
[i
], r
);
230 draw
*= bucket
->straws
[i
];
231 if (i
== 0 || draw
> high_draw
) {
236 return bucket
->h
.items
[high
];
239 static int crush_bucket_choose(struct crush_bucket
*in
, int x
, int r
)
241 dprintk(" crush_bucket_choose %d x=%d r=%d\n", in
->id
, x
, r
);
243 case CRUSH_BUCKET_UNIFORM
:
244 return bucket_uniform_choose((struct crush_bucket_uniform
*)in
,
246 case CRUSH_BUCKET_LIST
:
247 return bucket_list_choose((struct crush_bucket_list
*)in
,
249 case CRUSH_BUCKET_TREE
:
250 return bucket_tree_choose((struct crush_bucket_tree
*)in
,
252 case CRUSH_BUCKET_STRAW
:
253 return bucket_straw_choose((struct crush_bucket_straw
*)in
,
262 * true if device is marked "out" (failed, fully offloaded)
265 static int is_out(struct crush_map
*map
, __u32
*weight
, int item
, int x
)
267 if (weight
[item
] >= 0x10000)
269 if (weight
[item
] == 0)
271 if ((crush_hash32_2(CRUSH_HASH_RJENKINS1
, x
, item
) & 0xffff)
278 * crush_choose - choose numrep distinct items of given type
279 * @map: the crush_map
280 * @bucket: the bucket we are choose an item from
281 * @x: crush input value
282 * @numrep: the number of items to choose
283 * @type: the type of item to choose
284 * @out: pointer to output vector
285 * @outpos: our position in that vector
286 * @firstn: true if choosing "first n" items, false if choosing "indep"
287 * @recurse_to_leaf: true if we want one device under each item of given type
288 * @out2: second output vector for leaf items (if @recurse_to_leaf)
290 static int crush_choose(struct crush_map
*map
,
291 struct crush_bucket
*bucket
,
293 int x
, int numrep
, int type
,
294 int *out
, int outpos
,
295 int firstn
, int recurse_to_leaf
,
300 int retry_descent
, retry_bucket
, skip_rep
;
301 struct crush_bucket
*in
= bucket
;
307 const int orig_tries
= 5; /* attempts before we fall back to search */
309 dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf
? "_LEAF" : "",
310 bucket
->id
, x
, outpos
, numrep
);
312 for (rep
= outpos
; rep
< numrep
; rep
++) {
313 /* keep trying until we get a non-out, non-colliding item */
318 in
= bucket
; /* initial bucket */
320 /* choose through intervening buckets */
326 if (in
->alg
== CRUSH_BUCKET_UNIFORM
) {
328 if (firstn
|| numrep
>= in
->size
)
329 /* r' = r + f_total */
331 else if (in
->size
% numrep
== 0)
332 /* r'=r+(n+1)*f_local */
336 /* r' = r + n*f_local */
337 r
+= numrep
* (flocal
+ftotal
);
340 /* r' = r + f_total */
343 /* r' = r + n*f_local */
344 r
+= numrep
* (flocal
+ftotal
);
352 if (flocal
>= (in
->size
>>1) &&
354 item
= bucket_perm_choose(in
, x
, r
);
356 item
= crush_bucket_choose(in
, x
, r
);
357 BUG_ON(item
>= map
->max_devices
);
361 itemtype
= map
->buckets
[-1-item
]->type
;
364 dprintk(" item %d type %d\n", item
, itemtype
);
367 if (itemtype
!= type
) {
369 (-1-item
) >= map
->max_buckets
);
370 in
= map
->buckets
[-1-item
];
376 for (i
= 0; i
< outpos
; i
++) {
377 if (out
[i
] == item
) {
384 if (recurse_to_leaf
) {
386 if (crush_choose(map
,
387 map
->buckets
[-1-item
],
393 /* didn't get leaf */
396 /* we already have a leaf! */
404 reject
= is_out(map
, weight
,
411 if (reject
|| collide
) {
415 if (collide
&& flocal
< 3)
416 /* retry locally a few times */
418 else if (flocal
< in
->size
+ orig_tries
)
419 /* exhaustive bucket search */
421 else if (ftotal
< 20)
422 /* then retry descent */
427 dprintk(" reject %d collide %d "
428 "ftotal %d flocal %d\n",
429 reject
, collide
, ftotal
,
432 } while (retry_bucket
);
433 } while (retry_descent
);
436 dprintk("skip rep\n");
440 dprintk("CHOOSE got %d\n", item
);
445 dprintk("CHOOSE returns %d\n", outpos
);
451 * crush_do_rule - calculate a mapping with the given input and rule
452 * @map: the crush_map
453 * @ruleno: the rule id
455 * @result: pointer to result vector
456 * @result_max: maximum result size
457 * @force: force initial replica choice; -1 for none
459 int crush_do_rule(struct crush_map
*map
,
460 int ruleno
, int x
, int *result
, int result_max
,
461 int force
, __u32
*weight
)
464 int force_context
[CRUSH_MAX_DEPTH
];
466 int a
[CRUSH_MAX_SET
];
467 int b
[CRUSH_MAX_SET
];
468 int c
[CRUSH_MAX_SET
];
475 struct crush_rule
*rule
;
482 BUG_ON(ruleno
>= map
->max_rules
);
484 rule
= map
->rules
[ruleno
];
490 * determine hierarchical context of force, if any. note
491 * that this may or may not correspond to the specific types
492 * referenced by the crush rule.
495 if (force
>= map
->max_devices
||
496 map
->device_parents
[force
] == 0) {
497 /*dprintk("CRUSH: forcefed device dne\n");*/
498 rc
= -1; /* force fed device dne */
501 if (!is_out(map
, weight
, force
, x
)) {
503 force_context
[++force_pos
] = force
;
505 force
= map
->device_parents
[force
];
507 force
= map
->bucket_parents
[-1-force
];
514 for (step
= 0; step
< rule
->len
; step
++) {
516 switch (rule
->steps
[step
].op
) {
517 case CRUSH_RULE_TAKE
:
518 w
[0] = rule
->steps
[step
].arg1
;
519 if (force_pos
>= 0) {
520 BUG_ON(force_context
[force_pos
] != w
[0]);
526 case CRUSH_RULE_CHOOSE_LEAF_FIRSTN
:
527 case CRUSH_RULE_CHOOSE_FIRSTN
:
529 case CRUSH_RULE_CHOOSE_LEAF_INDEP
:
530 case CRUSH_RULE_CHOOSE_INDEP
:
534 rule
->steps
[step
].op
==
535 CRUSH_RULE_CHOOSE_LEAF_FIRSTN
||
536 rule
->steps
[step
].op
==
537 CRUSH_RULE_CHOOSE_LEAF_INDEP
;
542 for (i
= 0; i
< wsize
; i
++) {
544 * see CRUSH_N, CRUSH_N_MINUS macros.
545 * basically, numrep <= 0 means relative to
546 * the provided result_max
548 numrep
= rule
->steps
[step
].arg1
;
550 numrep
+= result_max
;
555 if (osize
== 0 && force_pos
>= 0) {
556 /* skip any intermediate types */
558 force_context
[force_pos
] < 0 &&
559 rule
->steps
[step
].arg2
!=
561 force_context
[force_pos
]]->type
)
563 o
[osize
] = force_context
[force_pos
];
565 c
[osize
] = force_context
[0];
569 osize
+= crush_choose(map
,
570 map
->buckets
[-1-w
[i
]],
573 rule
->steps
[step
].arg2
,
576 recurse_to_leaf
, c
+osize
);
580 /* copy final _leaf_ values to output set */
581 memcpy(o
, c
, osize
*sizeof(*o
));
583 /* swap t and w arrays */
591 case CRUSH_RULE_EMIT
:
592 for (i
= 0; i
< wsize
&& result_len
< result_max
; i
++) {
593 result
[result_len
] = w
[i
];