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)
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("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
] >= 0x1000)
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 */
308 dprintk("choose bucket %d x %d outpos %d\n", bucket
->id
, x
, outpos
);
310 for (rep
= outpos
; rep
< numrep
; rep
++) {
311 /* keep trying until we get a non-out, non-colliding item */
316 in
= bucket
; /* initial bucket */
318 /* choose through intervening buckets */
324 if (in
->alg
== CRUSH_BUCKET_UNIFORM
) {
326 if (firstn
|| numrep
>= in
->size
)
327 /* r' = r + f_total */
329 else if (in
->size
% numrep
== 0)
330 /* r'=r+(n+1)*f_local */
334 /* r' = r + n*f_local */
335 r
+= numrep
* (flocal
+ftotal
);
338 /* r' = r + f_total */
341 /* r' = r + n*f_local */
342 r
+= numrep
* (flocal
+ftotal
);
350 if (flocal
>= (in
->size
>>1) &&
352 item
= bucket_perm_choose(in
, x
, r
);
354 item
= crush_bucket_choose(in
, x
, r
);
355 BUG_ON(item
>= map
->max_devices
);
359 itemtype
= map
->buckets
[-1-item
]->type
;
362 dprintk(" item %d type %d\n", item
, itemtype
);
365 if (itemtype
!= type
) {
367 (-1-item
) >= map
->max_buckets
);
368 in
= map
->buckets
[-1-item
];
373 for (i
= 0; i
< outpos
; i
++) {
374 if (out
[i
] == item
) {
380 if (recurse_to_leaf
&&
382 crush_choose(map
, map
->buckets
[-1-item
],
386 firstn
, 0, NULL
) <= outpos
) {
391 reject
= is_out(map
, weight
,
398 if (reject
|| collide
) {
402 if (collide
&& flocal
< 3)
403 /* retry locally a few times */
405 else if (flocal
< in
->size
+ orig_tries
)
406 /* exhaustive bucket search */
408 else if (ftotal
< 20)
409 /* then retry descent */
414 dprintk(" reject %d collide %d "
415 "ftotal %d flocal %d\n",
416 reject
, collide
, ftotal
,
419 } while (retry_bucket
);
420 } while (retry_descent
);
423 dprintk("skip rep\n");
427 dprintk("choose got %d\n", item
);
432 dprintk("choose returns %d\n", outpos
);
438 * crush_do_rule - calculate a mapping with the given input and rule
439 * @map: the crush_map
440 * @ruleno: the rule id
442 * @result: pointer to result vector
443 * @result_max: maximum result size
444 * @force: force initial replica choice; -1 for none
446 int crush_do_rule(struct crush_map
*map
,
447 int ruleno
, int x
, int *result
, int result_max
,
448 int force
, __u32
*weight
)
451 int force_context
[CRUSH_MAX_DEPTH
];
453 int a
[CRUSH_MAX_SET
];
454 int b
[CRUSH_MAX_SET
];
455 int c
[CRUSH_MAX_SET
];
462 struct crush_rule
*rule
;
469 BUG_ON(ruleno
>= map
->max_rules
);
471 rule
= map
->rules
[ruleno
];
477 * determine hierarchical context of force, if any. note
478 * that this may or may not correspond to the specific types
479 * referenced by the crush rule.
482 if (force
>= map
->max_devices
||
483 map
->device_parents
[force
] == 0) {
484 /*dprintk("CRUSH: forcefed device dne\n");*/
485 rc
= -1; /* force fed device dne */
488 if (!is_out(map
, weight
, force
, x
)) {
490 force_context
[++force_pos
] = force
;
492 force
= map
->device_parents
[force
];
494 force
= map
->bucket_parents
[-1-force
];
501 for (step
= 0; step
< rule
->len
; step
++) {
503 switch (rule
->steps
[step
].op
) {
504 case CRUSH_RULE_TAKE
:
505 w
[0] = rule
->steps
[step
].arg1
;
506 if (force_pos
>= 0) {
507 BUG_ON(force_context
[force_pos
] != w
[0]);
513 case CRUSH_RULE_CHOOSE_LEAF_FIRSTN
:
514 case CRUSH_RULE_CHOOSE_FIRSTN
:
516 case CRUSH_RULE_CHOOSE_LEAF_INDEP
:
517 case CRUSH_RULE_CHOOSE_INDEP
:
521 rule
->steps
[step
].op
==
522 CRUSH_RULE_CHOOSE_LEAF_FIRSTN
||
523 rule
->steps
[step
].op
==
524 CRUSH_RULE_CHOOSE_LEAF_INDEP
;
529 for (i
= 0; i
< wsize
; i
++) {
531 * see CRUSH_N, CRUSH_N_MINUS macros.
532 * basically, numrep <= 0 means relative to
533 * the provided result_max
535 numrep
= rule
->steps
[step
].arg1
;
537 numrep
+= result_max
;
542 if (osize
== 0 && force_pos
>= 0) {
543 /* skip any intermediate types */
545 force_context
[force_pos
] < 0 &&
546 rule
->steps
[step
].arg2
!=
548 force_context
[force_pos
]]->type
)
550 o
[osize
] = force_context
[force_pos
];
552 c
[osize
] = force_context
[0];
556 osize
+= crush_choose(map
,
557 map
->buckets
[-1-w
[i
]],
560 rule
->steps
[step
].arg2
,
563 recurse_to_leaf
, c
+osize
);
567 /* copy final _leaf_ values to output set */
568 memcpy(o
, c
, osize
*sizeof(*o
));
570 /* swap t and w arrays */
578 case CRUSH_RULE_EMIT
:
579 for (i
= 0; i
< wsize
&& result_len
< result_max
; i
++) {
580 result
[result_len
] = w
[i
];