3 # include <linux/string.h>
4 # include <linux/slab.h>
6 # include <linux/kernel.h>
8 # define dprintk(args...)
15 # define BUG_ON(x) assert(!(x))
16 # define dprintk(args...)
17 # define kmalloc(x, f) malloc(x)
18 # define kfree(x) free(x)
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)
69 static int bucket_perm_choose(
struct crush_bucket *bucket,
72 unsigned int pr = r % bucket->
size;
77 dprintk(
"bucket %d new x=%d\n", bucket->
id, x);
89 for (i = 0; i < bucket->
size; i++)
92 }
else if (bucket->
perm_n == 0xffff) {
94 for (i = 1; i < bucket->
size; i++)
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;
106 if (p < bucket->
size - 1) {
110 unsigned int t = bucket->
perm[p +
i];
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]);
123 dprintk(
" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->
id,
124 bucket->
size, x, r, pr, s);
132 return bucket_perm_choose(&bucket->
h, x, r);
141 for (i = bucket->
h.size-1; i >= 0; i--) {
145 dprintk(
"list_choose i=%d x=%d r=%d item %d weight %x "
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];
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)
199 while (!terminal(n)) {
208 if (t < bucket->node_weights[l])
214 return bucket->
h.items[n >> 1];
228 for (i = 0; i < bucket->
h.size; 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);
269 if (weight[item] >= 0x10000)
271 if (weight[item] == 0)
292 static int crush_choose(
const struct crush_map *map,
295 int x,
int numrep,
int type,
296 int *
out,
int outpos,
297 int firstn,
int recurse_to_leaf,
301 unsigned int ftotal, flocal;
302 int retry_descent, retry_bucket, skip_rep;
310 dprintk(
"CHOOSE%s bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ?
"_LEAF" :
"",
311 bucket->
id, x, outpos, numrep);
313 for (rep = outpos; rep < numrep; rep++) {
332 else if (in->
size % numrep == 0)
338 r += numrep * (flocal+ftotal);
345 r += numrep * (flocal+ftotal);
354 flocal >= (in->
size>>1) &&
356 item = bucket_perm_choose(in, x, r);
358 item = crush_bucket_choose(in, x, r);
360 dprintk(
" bad item %d\n", item);
370 dprintk(
" item %d type %d\n", item, itemtype);
373 if (itemtype != type) {
376 dprintk(
" bad item type %d\n", type);
386 for (i = 0; i < outpos; i++) {
387 if (out[i] == item) {
394 if (recurse_to_leaf) {
396 if (crush_choose(map,
414 reject = is_out(map, weight,
421 if (reject || collide) {
425 if (collide && flocal <= map->choose_local_tries)
432 else if (ftotal <= map->choose_total_tries)
438 dprintk(
" reject %d collide %d "
439 "ftotal %u flocal %u\n",
440 reject, collide, ftotal,
443 }
while (retry_bucket);
444 }
while (retry_descent);
451 dprintk(
"CHOOSE got %d\n", item);
456 dprintk(
"CHOOSE returns %d\n", outpos);
470 int ruleno,
int x,
int *
result,
int result_max,
490 dprintk(
" bad ruleno %d\n", ruleno);
494 rule = map->
rules[ruleno];
499 for (step = 0; step < rule->
len; step++) {
503 switch (curstep->
op) {
505 w[0] = curstep->
arg1;
527 for (i = 0; i < wsize; i++) {
533 numrep = curstep->
arg1;
535 numrep += result_max;
540 osize += crush_choose(map,
547 recurse_to_leaf, c+osize);
552 memcpy(o, c, osize*
sizeof(*o));
563 for (i = 0; i < wsize && result_len < result_max; i++) {
564 result[result_len] = w[
i];
571 dprintk(
" unknown op %d at step %d\n",