Commit | Line | Data |
---|---|---|
5ecc0a0f SW |
1 | |
2 | #ifdef __KERNEL__ | |
3 | # include <linux/string.h> | |
4 | # include <linux/slab.h> | |
5 | # include <linux/bug.h> | |
6 | # include <linux/kernel.h> | |
7 | # ifndef dprintk | |
8 | # define dprintk(args...) | |
9 | # endif | |
10 | #else | |
11 | # include <string.h> | |
12 | # include <stdio.h> | |
13 | # include <stdlib.h> | |
14 | # include <assert.h> | |
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) | |
19 | #endif | |
20 | ||
3d14c5d2 YS |
21 | #include <linux/crush/crush.h> |
22 | #include <linux/crush/hash.h> | |
feb50ac1 | 23 | #include <linux/crush/mapper.h> |
5ecc0a0f SW |
24 | |
25 | /* | |
26 | * Implement the core CRUSH mapping algorithm. | |
27 | */ | |
28 | ||
29 | /** | |
30 | * crush_find_rule - find a crush_rule id for a given ruleset, type, and size. | |
31 | * @map: the crush_map | |
32 | * @ruleset: the storage ruleset id (user defined) | |
33 | * @type: storage ruleset type (user defined) | |
34 | * @size: output set size | |
35 | */ | |
8b12d47b | 36 | int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size) |
5ecc0a0f | 37 | { |
8b12d47b | 38 | __u32 i; |
5ecc0a0f SW |
39 | |
40 | for (i = 0; i < map->max_rules; i++) { | |
41 | if (map->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) | |
46 | return i; | |
47 | } | |
48 | return -1; | |
49 | } | |
50 | ||
51 | ||
52 | /* | |
53 | * bucket choose methods | |
54 | * | |
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. | |
58 | */ | |
59 | ||
60 | /* | |
61 | * Choose based on a random permutation of the bucket. | |
62 | * | |
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. | |
68 | */ | |
69 | static int bucket_perm_choose(struct crush_bucket *bucket, | |
70 | int x, int r) | |
71 | { | |
95c96174 ED |
72 | unsigned int pr = r % bucket->size; |
73 | unsigned int i, s; | |
5ecc0a0f SW |
74 | |
75 | /* start a new permutation if @x has changed */ | |
8b12d47b | 76 | if (bucket->perm_x != (__u32)x || bucket->perm_n == 0) { |
5ecc0a0f SW |
77 | dprintk("bucket %d new x=%d\n", bucket->id, x); |
78 | bucket->perm_x = x; | |
79 | ||
80 | /* optimize common r=0 case */ | |
81 | if (pr == 0) { | |
fb690390 | 82 | s = crush_hash32_3(bucket->hash, x, bucket->id, 0) % |
5ecc0a0f SW |
83 | bucket->size; |
84 | bucket->perm[0] = s; | |
85 | bucket->perm_n = 0xffff; /* magic value, see below */ | |
86 | goto out; | |
87 | } | |
88 | ||
89 | for (i = 0; i < bucket->size; i++) | |
90 | bucket->perm[i] = i; | |
91 | bucket->perm_n = 0; | |
92 | } else if (bucket->perm_n == 0xffff) { | |
93 | /* clean up after the r=0 case above */ | |
94 | for (i = 1; i < bucket->size; i++) | |
95 | bucket->perm[i] = i; | |
96 | bucket->perm[bucket->perm[0]] = 0; | |
97 | bucket->perm_n = 1; | |
98 | } | |
99 | ||
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) { | |
95c96174 | 104 | unsigned int p = bucket->perm_n; |
5ecc0a0f SW |
105 | /* no point in swapping the final entry */ |
106 | if (p < bucket->size - 1) { | |
fb690390 | 107 | i = crush_hash32_3(bucket->hash, x, bucket->id, p) % |
5ecc0a0f SW |
108 | (bucket->size - p); |
109 | if (i) { | |
95c96174 | 110 | unsigned int t = bucket->perm[p + i]; |
5ecc0a0f SW |
111 | bucket->perm[p + i] = bucket->perm[p]; |
112 | bucket->perm[p] = t; | |
113 | } | |
114 | dprintk(" perm_choose swap %d with %d\n", p, p+i); | |
115 | } | |
116 | bucket->perm_n++; | |
117 | } | |
118 | for (i = 0; i < bucket->size; i++) | |
119 | dprintk(" perm_choose %d: %d\n", i, bucket->perm[i]); | |
120 | ||
121 | s = bucket->perm[pr]; | |
122 | out: | |
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]; | |
126 | } | |
127 | ||
128 | /* uniform */ | |
129 | static int bucket_uniform_choose(struct crush_bucket_uniform *bucket, | |
130 | int x, int r) | |
131 | { | |
132 | return bucket_perm_choose(&bucket->h, x, r); | |
133 | } | |
134 | ||
135 | /* list */ | |
136 | static int bucket_list_choose(struct crush_bucket_list *bucket, | |
137 | int x, int r) | |
138 | { | |
139 | int i; | |
140 | ||
141 | for (i = bucket->h.size-1; i >= 0; i--) { | |
fb690390 SW |
142 | __u64 w = crush_hash32_4(bucket->h.hash,x, bucket->h.items[i], |
143 | r, bucket->h.id); | |
5ecc0a0f SW |
144 | w &= 0xffff; |
145 | dprintk("list_choose i=%d x=%d r=%d item %d weight %x " | |
146 | "sw %x rand %llx", | |
147 | i, x, r, bucket->h.items[i], bucket->item_weights[i], | |
148 | bucket->sum_weights[i], w); | |
149 | w *= bucket->sum_weights[i]; | |
150 | w = w >> 16; | |
151 | /*dprintk(" scaled %llx\n", w);*/ | |
152 | if (w < bucket->item_weights[i]) | |
153 | return bucket->h.items[i]; | |
154 | } | |
155 | ||
a1f4895b SW |
156 | dprintk("bad list sums for bucket %d\n", bucket->h.id); |
157 | return bucket->h.items[0]; | |
5ecc0a0f SW |
158 | } |
159 | ||
160 | ||
161 | /* (binary) tree */ | |
162 | static int height(int n) | |
163 | { | |
164 | int h = 0; | |
165 | while ((n & 1) == 0) { | |
166 | h++; | |
167 | n = n >> 1; | |
168 | } | |
169 | return h; | |
170 | } | |
171 | ||
172 | static int left(int x) | |
173 | { | |
174 | int h = height(x); | |
175 | return x - (1 << (h-1)); | |
176 | } | |
177 | ||
178 | static int right(int x) | |
179 | { | |
180 | int h = height(x); | |
181 | return x + (1 << (h-1)); | |
182 | } | |
183 | ||
184 | static int terminal(int x) | |
185 | { | |
186 | return x & 1; | |
187 | } | |
188 | ||
189 | static int bucket_tree_choose(struct crush_bucket_tree *bucket, | |
190 | int x, int r) | |
191 | { | |
8f99c85b | 192 | int n; |
5ecc0a0f SW |
193 | __u32 w; |
194 | __u64 t; | |
195 | ||
196 | /* start at root */ | |
197 | n = bucket->num_nodes >> 1; | |
198 | ||
199 | while (!terminal(n)) { | |
8f99c85b | 200 | int l; |
5ecc0a0f SW |
201 | /* pick point in [0, w) */ |
202 | w = bucket->node_weights[n]; | |
fb690390 SW |
203 | t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r, |
204 | bucket->h.id) * (__u64)w; | |
5ecc0a0f SW |
205 | t = t >> 32; |
206 | ||
207 | /* descend to the left or right? */ | |
208 | l = left(n); | |
209 | if (t < bucket->node_weights[l]) | |
210 | n = l; | |
211 | else | |
212 | n = right(n); | |
213 | } | |
214 | ||
215 | return bucket->h.items[n >> 1]; | |
216 | } | |
217 | ||
218 | ||
219 | /* straw */ | |
220 | ||
221 | static int bucket_straw_choose(struct crush_bucket_straw *bucket, | |
222 | int x, int r) | |
223 | { | |
8b12d47b | 224 | __u32 i; |
5ecc0a0f SW |
225 | int high = 0; |
226 | __u64 high_draw = 0; | |
227 | __u64 draw; | |
228 | ||
229 | for (i = 0; i < bucket->h.size; i++) { | |
fb690390 | 230 | draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r); |
5ecc0a0f SW |
231 | draw &= 0xffff; |
232 | draw *= bucket->straws[i]; | |
233 | if (i == 0 || draw > high_draw) { | |
234 | high = i; | |
235 | high_draw = draw; | |
236 | } | |
237 | } | |
238 | return bucket->h.items[high]; | |
239 | } | |
240 | ||
241 | static int crush_bucket_choose(struct crush_bucket *in, int x, int r) | |
242 | { | |
a1a31e73 | 243 | dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r); |
a1f4895b | 244 | BUG_ON(in->size == 0); |
5ecc0a0f SW |
245 | switch (in->alg) { |
246 | case CRUSH_BUCKET_UNIFORM: | |
247 | return bucket_uniform_choose((struct crush_bucket_uniform *)in, | |
248 | x, r); | |
249 | case CRUSH_BUCKET_LIST: | |
250 | return bucket_list_choose((struct crush_bucket_list *)in, | |
251 | x, r); | |
252 | case CRUSH_BUCKET_TREE: | |
253 | return bucket_tree_choose((struct crush_bucket_tree *)in, | |
254 | x, r); | |
255 | case CRUSH_BUCKET_STRAW: | |
256 | return bucket_straw_choose((struct crush_bucket_straw *)in, | |
257 | x, r); | |
258 | default: | |
a1f4895b | 259 | dprintk("unknown bucket %d alg %d\n", in->id, in->alg); |
50b885b9 | 260 | return in->items[0]; |
5ecc0a0f SW |
261 | } |
262 | } | |
263 | ||
264 | /* | |
265 | * true if device is marked "out" (failed, fully offloaded) | |
266 | * of the cluster | |
267 | */ | |
b3b33b0e ID |
268 | static int is_out(const struct crush_map *map, |
269 | const __u32 *weight, int weight_max, | |
270 | int item, int x) | |
5ecc0a0f | 271 | { |
b3b33b0e ID |
272 | if (item >= weight_max) |
273 | return 1; | |
153a1093 | 274 | if (weight[item] >= 0x10000) |
5ecc0a0f SW |
275 | return 0; |
276 | if (weight[item] == 0) | |
277 | return 1; | |
fb690390 SW |
278 | if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff) |
279 | < weight[item]) | |
5ecc0a0f SW |
280 | return 0; |
281 | return 1; | |
282 | } | |
283 | ||
284 | /** | |
9fe07182 | 285 | * crush_choose_firstn - choose numrep distinct items of given type |
5ecc0a0f SW |
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 | |
0e32d712 ID |
293 | * @tries: number of attempts to make |
294 | * @recurse_tries: number of attempts to have recursive chooseleaf make | |
48a163db ID |
295 | * @local_retries: localized retries |
296 | * @local_fallback_retries: localized fallback retries | |
0e32d712 | 297 | * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose) |
e2b149cc | 298 | * @vary_r: pass r to recursive calls |
5ecc0a0f | 299 | * @out2: second output vector for leaf items (if @recurse_to_leaf) |
e2b149cc | 300 | * @parent_r: r value passed from the parent |
5ecc0a0f | 301 | */ |
9fe07182 ID |
302 | static int crush_choose_firstn(const struct crush_map *map, |
303 | struct crush_bucket *bucket, | |
304 | const __u32 *weight, int weight_max, | |
305 | int x, int numrep, int type, | |
306 | int *out, int outpos, | |
2d8be0bc ID |
307 | unsigned int tries, |
308 | unsigned int recurse_tries, | |
48a163db ID |
309 | unsigned int local_retries, |
310 | unsigned int local_fallback_retries, | |
9fe07182 | 311 | int recurse_to_leaf, |
e2b149cc ID |
312 | unsigned int vary_r, |
313 | int *out2, | |
314 | int parent_r) | |
5ecc0a0f SW |
315 | { |
316 | int rep; | |
8b12d47b | 317 | unsigned int ftotal, flocal; |
5ecc0a0f SW |
318 | int retry_descent, retry_bucket, skip_rep; |
319 | struct crush_bucket *in = bucket; | |
320 | int r; | |
321 | int i; | |
b28813a6 | 322 | int item = 0; |
5ecc0a0f SW |
323 | int itemtype; |
324 | int collide, reject; | |
a1a31e73 | 325 | |
e2b149cc ID |
326 | 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", |
327 | recurse_to_leaf ? "_LEAF" : "", | |
328 | bucket->id, x, outpos, numrep, | |
329 | tries, recurse_tries, local_retries, local_fallback_retries, | |
330 | parent_r); | |
5ecc0a0f SW |
331 | |
332 | for (rep = outpos; rep < numrep; rep++) { | |
333 | /* keep trying until we get a non-out, non-colliding item */ | |
334 | ftotal = 0; | |
335 | skip_rep = 0; | |
336 | do { | |
337 | retry_descent = 0; | |
338 | in = bucket; /* initial bucket */ | |
339 | ||
340 | /* choose through intervening buckets */ | |
341 | flocal = 0; | |
342 | do { | |
b28813a6 | 343 | collide = 0; |
5ecc0a0f | 344 | retry_bucket = 0; |
e2b149cc | 345 | r = rep + parent_r; |
9fe07182 ID |
346 | /* r' = r + f_total */ |
347 | r += ftotal; | |
5ecc0a0f SW |
348 | |
349 | /* bucket choose */ | |
b28813a6 SW |
350 | if (in->size == 0) { |
351 | reject = 1; | |
352 | goto reject; | |
353 | } | |
48a163db | 354 | if (local_fallback_retries > 0 && |
546f04ef | 355 | flocal >= (in->size>>1) && |
48a163db | 356 | flocal > local_fallback_retries) |
5ecc0a0f SW |
357 | item = bucket_perm_choose(in, x, r); |
358 | else | |
359 | item = crush_bucket_choose(in, x, r); | |
a1f4895b SW |
360 | if (item >= map->max_devices) { |
361 | dprintk(" bad item %d\n", item); | |
362 | skip_rep = 1; | |
363 | break; | |
364 | } | |
5ecc0a0f SW |
365 | |
366 | /* desired type? */ | |
367 | if (item < 0) | |
368 | itemtype = map->buckets[-1-item]->type; | |
369 | else | |
370 | itemtype = 0; | |
371 | dprintk(" item %d type %d\n", item, itemtype); | |
372 | ||
373 | /* keep going? */ | |
374 | if (itemtype != type) { | |
a1f4895b SW |
375 | if (item >= 0 || |
376 | (-1-item) >= map->max_buckets) { | |
377 | dprintk(" bad item type %d\n", type); | |
378 | skip_rep = 1; | |
379 | break; | |
380 | } | |
5ecc0a0f | 381 | in = map->buckets[-1-item]; |
55bda7aa | 382 | retry_bucket = 1; |
5ecc0a0f SW |
383 | continue; |
384 | } | |
385 | ||
386 | /* collision? */ | |
5ecc0a0f SW |
387 | for (i = 0; i < outpos; i++) { |
388 | if (out[i] == item) { | |
389 | collide = 1; | |
390 | break; | |
391 | } | |
392 | } | |
393 | ||
a1a31e73 | 394 | reject = 0; |
7d7c1f61 | 395 | if (!collide && recurse_to_leaf) { |
a1a31e73 | 396 | if (item < 0) { |
e2b149cc ID |
397 | int sub_r; |
398 | if (vary_r) | |
399 | sub_r = r >> (vary_r-1); | |
400 | else | |
401 | sub_r = 0; | |
9fe07182 | 402 | if (crush_choose_firstn(map, |
a1a31e73 | 403 | map->buckets[-1-item], |
b3b33b0e | 404 | weight, weight_max, |
a1a31e73 SW |
405 | x, outpos+1, 0, |
406 | out2, outpos, | |
2d8be0bc | 407 | recurse_tries, 0, |
48a163db ID |
408 | local_retries, |
409 | local_fallback_retries, | |
9fe07182 | 410 | 0, |
e2b149cc ID |
411 | vary_r, |
412 | NULL, | |
413 | sub_r) <= outpos) | |
a1a31e73 SW |
414 | /* didn't get leaf */ |
415 | reject = 1; | |
416 | } else { | |
417 | /* we already have a leaf! */ | |
418 | out2[outpos] = item; | |
419 | } | |
420 | } | |
421 | ||
422 | if (!reject) { | |
5ecc0a0f SW |
423 | /* out? */ |
424 | if (itemtype == 0) | |
425 | reject = is_out(map, weight, | |
b3b33b0e | 426 | weight_max, |
5ecc0a0f SW |
427 | item, x); |
428 | else | |
429 | reject = 0; | |
430 | } | |
431 | ||
b28813a6 | 432 | reject: |
5ecc0a0f SW |
433 | if (reject || collide) { |
434 | ftotal++; | |
435 | flocal++; | |
436 | ||
48a163db | 437 | if (collide && flocal <= local_retries) |
5ecc0a0f SW |
438 | /* retry locally a few times */ |
439 | retry_bucket = 1; | |
48a163db ID |
440 | else if (local_fallback_retries > 0 && |
441 | flocal <= in->size + local_fallback_retries) | |
5ecc0a0f SW |
442 | /* exhaustive bucket search */ |
443 | retry_bucket = 1; | |
48a163db | 444 | else if (ftotal < tries) |
5ecc0a0f SW |
445 | /* then retry descent */ |
446 | retry_descent = 1; | |
447 | else | |
448 | /* else give up */ | |
449 | skip_rep = 1; | |
450 | dprintk(" reject %d collide %d " | |
8b12d47b | 451 | "ftotal %u flocal %u\n", |
5ecc0a0f SW |
452 | reject, collide, ftotal, |
453 | flocal); | |
454 | } | |
455 | } while (retry_bucket); | |
456 | } while (retry_descent); | |
457 | ||
458 | if (skip_rep) { | |
9fe07182 ID |
459 | dprintk("skip rep\n"); |
460 | continue; | |
5ecc0a0f SW |
461 | } |
462 | ||
a1a31e73 | 463 | dprintk("CHOOSE got %d\n", item); |
5ecc0a0f SW |
464 | out[outpos] = item; |
465 | outpos++; | |
466 | } | |
467 | ||
a1a31e73 | 468 | dprintk("CHOOSE returns %d\n", outpos); |
5ecc0a0f SW |
469 | return outpos; |
470 | } | |
471 | ||
472 | ||
9a3b490a | 473 | /** |
9fe07182 | 474 | * crush_choose_indep: alternative breadth-first positionally stable mapping |
9a3b490a ID |
475 | * |
476 | */ | |
477 | static void crush_choose_indep(const struct crush_map *map, | |
be3226ac ID |
478 | struct crush_bucket *bucket, |
479 | const __u32 *weight, int weight_max, | |
ab4ce2b5 | 480 | int x, int left, int numrep, int type, |
be3226ac | 481 | int *out, int outpos, |
2d8be0bc ID |
482 | unsigned int tries, |
483 | unsigned int recurse_tries, | |
be3226ac | 484 | int recurse_to_leaf, |
41586081 ID |
485 | int *out2, |
486 | int parent_r) | |
9a3b490a ID |
487 | { |
488 | struct crush_bucket *in = bucket; | |
ab4ce2b5 | 489 | int endpos = outpos + left; |
9a3b490a ID |
490 | int rep; |
491 | unsigned int ftotal; | |
492 | int r; | |
493 | int i; | |
494 | int item = 0; | |
495 | int itemtype; | |
496 | int collide; | |
497 | ||
498 | dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "", | |
499 | bucket->id, x, outpos, numrep); | |
500 | ||
501 | /* initially my result is undefined */ | |
ab4ce2b5 | 502 | for (rep = outpos; rep < endpos; rep++) { |
9a3b490a ID |
503 | out[rep] = CRUSH_ITEM_UNDEF; |
504 | if (out2) | |
505 | out2[rep] = CRUSH_ITEM_UNDEF; | |
506 | } | |
507 | ||
2d8be0bc | 508 | for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) { |
ab4ce2b5 | 509 | for (rep = outpos; rep < endpos; rep++) { |
9a3b490a ID |
510 | if (out[rep] != CRUSH_ITEM_UNDEF) |
511 | continue; | |
512 | ||
513 | in = bucket; /* initial bucket */ | |
514 | ||
515 | /* choose through intervening buckets */ | |
516 | for (;;) { | |
3102b0a5 ID |
517 | /* note: we base the choice on the position |
518 | * even in the nested call. that means that | |
519 | * if the first layer chooses the same bucket | |
520 | * in a different position, we will tend to | |
521 | * choose a different item in that bucket. | |
522 | * this will involve more devices in data | |
523 | * movement and tend to distribute the load. | |
524 | */ | |
41586081 | 525 | r = rep + parent_r; |
9a3b490a ID |
526 | |
527 | /* be careful */ | |
528 | if (in->alg == CRUSH_BUCKET_UNIFORM && | |
529 | in->size % numrep == 0) | |
530 | /* r'=r+(n+1)*f_total */ | |
531 | r += (numrep+1) * ftotal; | |
532 | else | |
533 | /* r' = r + n*f_total */ | |
534 | r += numrep * ftotal; | |
535 | ||
536 | /* bucket choose */ | |
537 | if (in->size == 0) { | |
538 | dprintk(" empty bucket\n"); | |
539 | break; | |
540 | } | |
541 | ||
542 | item = crush_bucket_choose(in, x, r); | |
543 | if (item >= map->max_devices) { | |
544 | dprintk(" bad item %d\n", item); | |
545 | out[rep] = CRUSH_ITEM_NONE; | |
546 | if (out2) | |
547 | out2[rep] = CRUSH_ITEM_NONE; | |
548 | left--; | |
549 | break; | |
550 | } | |
551 | ||
552 | /* desired type? */ | |
553 | if (item < 0) | |
554 | itemtype = map->buckets[-1-item]->type; | |
555 | else | |
556 | itemtype = 0; | |
557 | dprintk(" item %d type %d\n", item, itemtype); | |
558 | ||
559 | /* keep going? */ | |
560 | if (itemtype != type) { | |
561 | if (item >= 0 || | |
562 | (-1-item) >= map->max_buckets) { | |
563 | dprintk(" bad item type %d\n", type); | |
564 | out[rep] = CRUSH_ITEM_NONE; | |
565 | if (out2) | |
566 | out2[rep] = | |
567 | CRUSH_ITEM_NONE; | |
568 | left--; | |
569 | break; | |
570 | } | |
571 | in = map->buckets[-1-item]; | |
572 | continue; | |
573 | } | |
574 | ||
575 | /* collision? */ | |
576 | collide = 0; | |
ab4ce2b5 | 577 | for (i = outpos; i < endpos; i++) { |
9a3b490a ID |
578 | if (out[i] == item) { |
579 | collide = 1; | |
580 | break; | |
581 | } | |
582 | } | |
583 | if (collide) | |
584 | break; | |
585 | ||
586 | if (recurse_to_leaf) { | |
587 | if (item < 0) { | |
588 | crush_choose_indep(map, | |
be3226ac ID |
589 | map->buckets[-1-item], |
590 | weight, weight_max, | |
591 | x, 1, numrep, 0, | |
592 | out2, rep, | |
2d8be0bc | 593 | recurse_tries, 0, |
be3226ac | 594 | 0, NULL, r); |
9a3b490a ID |
595 | if (out2[rep] == CRUSH_ITEM_NONE) { |
596 | /* placed nothing; no leaf */ | |
597 | break; | |
598 | } | |
599 | } else { | |
600 | /* we already have a leaf! */ | |
601 | out2[rep] = item; | |
602 | } | |
603 | } | |
604 | ||
605 | /* out? */ | |
606 | if (itemtype == 0 && | |
607 | is_out(map, weight, weight_max, item, x)) | |
608 | break; | |
609 | ||
610 | /* yay! */ | |
611 | out[rep] = item; | |
612 | left--; | |
613 | break; | |
614 | } | |
615 | } | |
616 | } | |
ab4ce2b5 | 617 | for (rep = outpos; rep < endpos; rep++) { |
9a3b490a ID |
618 | if (out[rep] == CRUSH_ITEM_UNDEF) { |
619 | out[rep] = CRUSH_ITEM_NONE; | |
620 | } | |
621 | if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) { | |
622 | out2[rep] = CRUSH_ITEM_NONE; | |
623 | } | |
624 | } | |
625 | } | |
626 | ||
5ecc0a0f SW |
627 | /** |
628 | * crush_do_rule - calculate a mapping with the given input and rule | |
629 | * @map: the crush_map | |
630 | * @ruleno: the rule id | |
631 | * @x: hash input | |
632 | * @result: pointer to result vector | |
633 | * @result_max: maximum result size | |
b3b33b0e ID |
634 | * @weight: weight vector (for map leaves) |
635 | * @weight_max: size of weight vector | |
e8ef19c4 | 636 | * @scratch: scratch vector for private use; must be >= 3 * result_max |
5ecc0a0f | 637 | */ |
8b12d47b | 638 | int crush_do_rule(const struct crush_map *map, |
5ecc0a0f | 639 | int ruleno, int x, int *result, int result_max, |
e8ef19c4 ID |
640 | const __u32 *weight, int weight_max, |
641 | int *scratch) | |
5ecc0a0f SW |
642 | { |
643 | int result_len; | |
e8ef19c4 ID |
644 | int *a = scratch; |
645 | int *b = scratch + result_max; | |
646 | int *c = scratch + result_max*2; | |
5ecc0a0f SW |
647 | int recurse_to_leaf; |
648 | int *w; | |
649 | int wsize = 0; | |
650 | int *o; | |
651 | int osize; | |
652 | int *tmp; | |
653 | struct crush_rule *rule; | |
8b12d47b | 654 | __u32 step; |
5ecc0a0f SW |
655 | int i, j; |
656 | int numrep; | |
48a163db ID |
657 | /* |
658 | * the original choose_total_tries value was off by one (it | |
659 | * counted "retries" and not "tries"). add one. | |
660 | */ | |
661 | int choose_tries = map->choose_total_tries + 1; | |
f18650ac | 662 | int choose_leaf_tries = 0; |
48a163db ID |
663 | /* |
664 | * the local tries values were counted as "retries", though, | |
665 | * and need no adjustment | |
666 | */ | |
667 | int choose_local_retries = map->choose_local_tries; | |
668 | int choose_local_fallback_retries = map->choose_local_fallback_tries; | |
5ecc0a0f | 669 | |
e2b149cc ID |
670 | int vary_r = map->chooseleaf_vary_r; |
671 | ||
a1f4895b SW |
672 | if ((__u32)ruleno >= map->max_rules) { |
673 | dprintk(" bad ruleno %d\n", ruleno); | |
674 | return 0; | |
675 | } | |
5ecc0a0f SW |
676 | |
677 | rule = map->rules[ruleno]; | |
678 | result_len = 0; | |
679 | w = a; | |
680 | o = b; | |
681 | ||
5ecc0a0f | 682 | for (step = 0; step < rule->len; step++) { |
8f99c85b | 683 | int firstn = 0; |
0668216e SW |
684 | struct crush_rule_step *curstep = &rule->steps[step]; |
685 | ||
0668216e | 686 | switch (curstep->op) { |
5ecc0a0f | 687 | case CRUSH_RULE_TAKE: |
0668216e | 688 | w[0] = curstep->arg1; |
5ecc0a0f SW |
689 | wsize = 1; |
690 | break; | |
691 | ||
cc10df4a ID |
692 | case CRUSH_RULE_SET_CHOOSE_TRIES: |
693 | if (curstep->arg1 > 0) | |
694 | choose_tries = curstep->arg1; | |
695 | break; | |
696 | ||
917edad5 | 697 | case CRUSH_RULE_SET_CHOOSELEAF_TRIES: |
be3226ac ID |
698 | if (curstep->arg1 > 0) |
699 | choose_leaf_tries = curstep->arg1; | |
700 | break; | |
701 | ||
f046bf92 | 702 | case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES: |
6ed1002f | 703 | if (curstep->arg1 >= 0) |
48a163db | 704 | choose_local_retries = curstep->arg1; |
f046bf92 ID |
705 | break; |
706 | ||
707 | case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES: | |
6ed1002f | 708 | if (curstep->arg1 >= 0) |
48a163db | 709 | choose_local_fallback_retries = curstep->arg1; |
f046bf92 | 710 | break; |
d83ed858 ID |
711 | |
712 | case CRUSH_RULE_SET_CHOOSELEAF_VARY_R: | |
713 | if (curstep->arg1 >= 0) | |
714 | vary_r = curstep->arg1; | |
715 | break; | |
f046bf92 | 716 | |
917edad5 | 717 | case CRUSH_RULE_CHOOSELEAF_FIRSTN: |
5ecc0a0f SW |
718 | case CRUSH_RULE_CHOOSE_FIRSTN: |
719 | firstn = 1; | |
0668216e | 720 | /* fall through */ |
917edad5 | 721 | case CRUSH_RULE_CHOOSELEAF_INDEP: |
5ecc0a0f | 722 | case CRUSH_RULE_CHOOSE_INDEP: |
a1f4895b SW |
723 | if (wsize == 0) |
724 | break; | |
5ecc0a0f SW |
725 | |
726 | recurse_to_leaf = | |
0668216e | 727 | curstep->op == |
917edad5 | 728 | CRUSH_RULE_CHOOSELEAF_FIRSTN || |
0668216e | 729 | curstep->op == |
917edad5 | 730 | CRUSH_RULE_CHOOSELEAF_INDEP; |
5ecc0a0f SW |
731 | |
732 | /* reset output */ | |
733 | osize = 0; | |
734 | ||
735 | for (i = 0; i < wsize; i++) { | |
736 | /* | |
737 | * see CRUSH_N, CRUSH_N_MINUS macros. | |
738 | * basically, numrep <= 0 means relative to | |
739 | * the provided result_max | |
740 | */ | |
0668216e | 741 | numrep = curstep->arg1; |
5ecc0a0f SW |
742 | if (numrep <= 0) { |
743 | numrep += result_max; | |
744 | if (numrep <= 0) | |
745 | continue; | |
746 | } | |
747 | j = 0; | |
9a3b490a | 748 | if (firstn) { |
d390bb2a ID |
749 | int recurse_tries; |
750 | if (choose_leaf_tries) | |
751 | recurse_tries = | |
752 | choose_leaf_tries; | |
753 | else if (map->chooseleaf_descend_once) | |
754 | recurse_tries = 1; | |
755 | else | |
756 | recurse_tries = choose_tries; | |
9fe07182 ID |
757 | osize += crush_choose_firstn( |
758 | map, | |
759 | map->buckets[-1-w[i]], | |
760 | weight, weight_max, | |
761 | x, numrep, | |
762 | curstep->arg2, | |
763 | o+osize, j, | |
f18650ac | 764 | choose_tries, |
d390bb2a | 765 | recurse_tries, |
48a163db ID |
766 | choose_local_retries, |
767 | choose_local_fallback_retries, | |
9fe07182 | 768 | recurse_to_leaf, |
e2b149cc ID |
769 | vary_r, |
770 | c+osize, | |
771 | 0); | |
9a3b490a | 772 | } else { |
9fe07182 ID |
773 | crush_choose_indep( |
774 | map, | |
775 | map->buckets[-1-w[i]], | |
776 | weight, weight_max, | |
ab4ce2b5 | 777 | x, numrep, numrep, |
9fe07182 ID |
778 | curstep->arg2, |
779 | o+osize, j, | |
f18650ac | 780 | choose_tries, |
d390bb2a ID |
781 | choose_leaf_tries ? |
782 | choose_leaf_tries : 1, | |
9fe07182 | 783 | recurse_to_leaf, |
41586081 ID |
784 | c+osize, |
785 | 0); | |
9a3b490a ID |
786 | osize += numrep; |
787 | } | |
5ecc0a0f SW |
788 | } |
789 | ||
790 | if (recurse_to_leaf) | |
791 | /* copy final _leaf_ values to output set */ | |
792 | memcpy(o, c, osize*sizeof(*o)); | |
793 | ||
2a4ba74e | 794 | /* swap o and w arrays */ |
5ecc0a0f SW |
795 | tmp = o; |
796 | o = w; | |
797 | w = tmp; | |
798 | wsize = osize; | |
799 | break; | |
800 | ||
801 | ||
802 | case CRUSH_RULE_EMIT: | |
803 | for (i = 0; i < wsize && result_len < result_max; i++) { | |
804 | result[result_len] = w[i]; | |
805 | result_len++; | |
806 | } | |
807 | wsize = 0; | |
808 | break; | |
809 | ||
810 | default: | |
a1f4895b SW |
811 | dprintk(" unknown op %d at step %d\n", |
812 | curstep->op, step); | |
813 | break; | |
5ecc0a0f SW |
814 | } |
815 | } | |
f1932fc1 | 816 | return result_len; |
5ecc0a0f SW |
817 | } |
818 | ||
819 |