]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | * Ceph - scalable distributed file system | |
3 | * | |
4 | * Copyright (C) 2015 Intel Corporation All Rights Reserved | |
5 | * | |
6 | * This is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License version 2.1, as published by the Free Software | |
9 | * Foundation. See file COPYING. | |
10 | * | |
11 | */ | |
12 | ||
13 | #ifdef __KERNEL__ | |
14 | # include <linux/string.h> | |
15 | # include <linux/slab.h> | |
16 | # include <linux/bug.h> | |
17 | # include <linux/kernel.h> | |
18 | # include <linux/crush/crush.h> | |
19 | # include <linux/crush/hash.h> | |
20 | #else | |
21 | # include "crush_compat.h" | |
22 | # include "crush.h" | |
23 | # include "hash.h" | |
24 | #endif | |
25 | #include "crush_ln_table.h" | |
26 | #include "mapper.h" | |
27 | ||
28 | #define dprintk(args...) /* printf(args) */ | |
29 | ||
30 | /* | |
31 | * Implement the core CRUSH mapping algorithm. | |
32 | */ | |
33 | ||
34 | /** | |
35 | * crush_find_rule - find a crush_rule id for a given ruleset, type, and size. | |
36 | * @map: the crush_map | |
37 | * @ruleset: the storage ruleset id (user defined) | |
38 | * @type: storage ruleset type (user defined) | |
39 | * @size: output set size | |
40 | */ | |
41 | int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size) | |
42 | { | |
43 | __u32 i; | |
44 | ||
45 | for (i = 0; i < map->max_rules; i++) { | |
46 | if (map->rules[i] && | |
47 | map->rules[i]->mask.ruleset == ruleset && | |
48 | map->rules[i]->mask.type == type && | |
49 | map->rules[i]->mask.min_size <= size && | |
50 | map->rules[i]->mask.max_size >= size) | |
51 | return i; | |
52 | } | |
53 | return -1; | |
54 | } | |
55 | ||
56 | /* | |
57 | * bucket choose methods | |
58 | * | |
59 | * For each bucket algorithm, we have a "choose" method that, given a | |
60 | * crush input @x and replica position (usually, position in output set) @r, | |
61 | * will produce an item in the bucket. | |
62 | */ | |
63 | ||
64 | /* | |
65 | * Choose based on a random permutation of the bucket. | |
66 | * | |
67 | * We used to use some prime number arithmetic to do this, but it | |
68 | * wasn't very random, and had some other bad behaviors. Instead, we | |
69 | * calculate an actual random permutation of the bucket members. | |
70 | * Since this is expensive, we optimize for the r=0 case, which | |
71 | * captures the vast majority of calls. | |
72 | */ | |
73 | static int bucket_perm_choose(const struct crush_bucket *bucket, | |
74 | struct crush_work_bucket *work, | |
75 | int x, int r) | |
76 | { | |
77 | unsigned int pr = r % bucket->size; | |
78 | unsigned int i, s; | |
79 | ||
80 | /* start a new permutation if @x has changed */ | |
81 | if (work->perm_x != (__u32)x || work->perm_n == 0) { | |
82 | dprintk("bucket %d new x=%d\n", bucket->id, x); | |
83 | work->perm_x = x; | |
84 | ||
85 | /* optimize common r=0 case */ | |
86 | if (pr == 0) { | |
87 | s = crush_hash32_3(bucket->hash, x, bucket->id, 0) % | |
88 | bucket->size; | |
89 | work->perm[0] = s; | |
90 | work->perm_n = 0xffff; /* magic value, see below */ | |
91 | goto out; | |
92 | } | |
93 | ||
94 | for (i = 0; i < bucket->size; i++) | |
95 | work->perm[i] = i; | |
96 | work->perm_n = 0; | |
97 | } else if (work->perm_n == 0xffff) { | |
98 | /* clean up after the r=0 case above */ | |
99 | for (i = 1; i < bucket->size; i++) | |
100 | work->perm[i] = i; | |
101 | work->perm[work->perm[0]] = 0; | |
102 | work->perm_n = 1; | |
103 | } | |
104 | ||
105 | /* calculate permutation up to pr */ | |
106 | for (i = 0; i < work->perm_n; i++) | |
107 | dprintk(" perm_choose have %d: %d\n", i, work->perm[i]); | |
108 | while (work->perm_n <= pr) { | |
109 | unsigned int p = work->perm_n; | |
110 | /* no point in swapping the final entry */ | |
111 | if (p < bucket->size - 1) { | |
112 | i = crush_hash32_3(bucket->hash, x, bucket->id, p) % | |
113 | (bucket->size - p); | |
114 | if (i) { | |
115 | unsigned int t = work->perm[p + i]; | |
116 | work->perm[p + i] = work->perm[p]; | |
117 | work->perm[p] = t; | |
118 | } | |
119 | dprintk(" perm_choose swap %d with %d\n", p, p+i); | |
120 | } | |
121 | work->perm_n++; | |
122 | } | |
123 | for (i = 0; i < bucket->size; i++) | |
124 | dprintk(" perm_choose %d: %d\n", i, work->perm[i]); | |
125 | ||
126 | s = work->perm[pr]; | |
127 | out: | |
128 | dprintk(" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->id, | |
129 | bucket->size, x, r, pr, s); | |
130 | return bucket->items[s]; | |
131 | } | |
132 | ||
133 | /* uniform */ | |
134 | static int bucket_uniform_choose(const struct crush_bucket_uniform *bucket, | |
135 | struct crush_work_bucket *work, int x, int r) | |
136 | { | |
137 | return bucket_perm_choose(&bucket->h, work, x, r); | |
138 | } | |
139 | ||
140 | /* list */ | |
141 | static int bucket_list_choose(const struct crush_bucket_list *bucket, | |
142 | int x, int r) | |
143 | { | |
144 | int i; | |
145 | ||
146 | for (i = bucket->h.size-1; i >= 0; i--) { | |
147 | __u64 w = crush_hash32_4(bucket->h.hash, x, bucket->h.items[i], | |
148 | r, bucket->h.id); | |
149 | w &= 0xffff; | |
150 | dprintk("list_choose i=%d x=%d r=%d item %d weight %x " | |
151 | "sw %x rand %llx", | |
152 | i, x, r, bucket->h.items[i], bucket->item_weights[i], | |
153 | bucket->sum_weights[i], w); | |
154 | w *= bucket->sum_weights[i]; | |
155 | w = w >> 16; | |
156 | /*dprintk(" scaled %llx\n", w);*/ | |
157 | if (w < bucket->item_weights[i]) { | |
158 | return bucket->h.items[i]; | |
159 | } | |
160 | } | |
161 | ||
162 | dprintk("bad list sums for bucket %d\n", bucket->h.id); | |
163 | return bucket->h.items[0]; | |
164 | } | |
165 | ||
166 | ||
167 | /* (binary) tree */ | |
168 | static int height(int n) | |
169 | { | |
170 | int h = 0; | |
171 | while ((n & 1) == 0) { | |
172 | h++; | |
173 | n = n >> 1; | |
174 | } | |
175 | return h; | |
176 | } | |
177 | ||
178 | static int left(int x) | |
179 | { | |
180 | int h = height(x); | |
181 | return x - (1 << (h-1)); | |
182 | } | |
183 | ||
184 | static int right(int x) | |
185 | { | |
186 | int h = height(x); | |
187 | return x + (1 << (h-1)); | |
188 | } | |
189 | ||
190 | static int terminal(int x) | |
191 | { | |
192 | return x & 1; | |
193 | } | |
194 | ||
195 | static int bucket_tree_choose(const struct crush_bucket_tree *bucket, | |
196 | int x, int r) | |
197 | { | |
198 | int n; | |
199 | __u32 w; | |
200 | __u64 t; | |
201 | ||
202 | /* start at root */ | |
203 | n = bucket->num_nodes >> 1; | |
204 | ||
205 | while (!terminal(n)) { | |
206 | int l; | |
207 | /* pick point in [0, w) */ | |
208 | w = bucket->node_weights[n]; | |
209 | t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r, | |
210 | bucket->h.id) * (__u64)w; | |
211 | t = t >> 32; | |
212 | ||
213 | /* descend to the left or right? */ | |
214 | l = left(n); | |
215 | if (t < bucket->node_weights[l]) | |
216 | n = l; | |
217 | else | |
218 | n = right(n); | |
219 | } | |
220 | ||
221 | return bucket->h.items[n >> 1]; | |
222 | } | |
223 | ||
224 | ||
225 | /* straw */ | |
226 | ||
227 | static int bucket_straw_choose(const struct crush_bucket_straw *bucket, | |
228 | int x, int r) | |
229 | { | |
230 | __u32 i; | |
231 | int high = 0; | |
232 | __u64 high_draw = 0; | |
233 | __u64 draw; | |
234 | ||
235 | for (i = 0; i < bucket->h.size; i++) { | |
236 | draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r); | |
237 | draw &= 0xffff; | |
238 | draw *= bucket->straws[i]; | |
239 | if (i == 0 || draw > high_draw) { | |
240 | high = i; | |
241 | high_draw = draw; | |
242 | } | |
243 | } | |
244 | return bucket->h.items[high]; | |
245 | } | |
246 | ||
247 | /* compute 2^44*log2(input+1) */ | |
248 | static __u64 crush_ln(unsigned int xin) | |
249 | { | |
250 | unsigned int x = xin; | |
251 | int iexpon, index1, index2; | |
252 | __u64 RH, LH, LL, xl64, result; | |
253 | ||
254 | x++; | |
255 | ||
256 | /* normalize input */ | |
257 | iexpon = 15; | |
258 | ||
259 | // figure out number of bits we need to shift and | |
260 | // do it in one step instead of iteratively | |
261 | if (!(x & 0x18000)) { | |
262 | int bits = __builtin_clz(x & 0x1FFFF) - 16; | |
263 | x <<= bits; | |
264 | iexpon = 15 - bits; | |
265 | } | |
266 | ||
267 | index1 = (x >> 8) << 1; | |
268 | /* RH ~ 2^56/index1 */ | |
269 | RH = __RH_LH_tbl[index1 - 256]; | |
270 | /* LH ~ 2^48 * log2(index1/256) */ | |
271 | LH = __RH_LH_tbl[index1 + 1 - 256]; | |
272 | ||
273 | /* RH*x ~ 2^48 * (2^15 + xf), xf<2^8 */ | |
274 | xl64 = (__s64)x * RH; | |
275 | xl64 >>= 48; | |
276 | ||
277 | result = iexpon; | |
278 | result <<= (12 + 32); | |
279 | ||
280 | index2 = xl64 & 0xff; | |
281 | /* LL ~ 2^48*log2(1.0+index2/2^15) */ | |
282 | LL = __LL_tbl[index2]; | |
283 | ||
284 | LH = LH + LL; | |
285 | ||
286 | LH >>= (48 - 12 - 32); | |
287 | result += LH; | |
288 | ||
289 | return result; | |
290 | } | |
291 | ||
292 | ||
293 | /* | |
294 | * straw2 | |
295 | * | |
296 | * for reference, see: | |
297 | * | |
298 | * http://en.wikipedia.org/wiki/Exponential_distribution#Distribution_of_the_minimum_of_exponential_random_variables | |
299 | * | |
300 | */ | |
301 | ||
302 | static inline __u32 *get_choose_arg_weights(const struct crush_bucket_straw2 *bucket, | |
303 | const struct crush_choose_arg *arg, | |
304 | int position) | |
305 | { | |
306 | if ((arg == NULL) || | |
307 | (arg->weight_set == NULL) || | |
308 | (arg->weight_set_size == 0)) | |
309 | return bucket->item_weights; | |
310 | if (position >= arg->weight_set_size) | |
311 | position = arg->weight_set_size - 1; | |
312 | return arg->weight_set[position].weights; | |
313 | } | |
314 | ||
224ce89b WB |
315 | static inline __s32 *get_choose_arg_ids(const struct crush_bucket_straw2 *bucket, |
316 | const struct crush_choose_arg *arg) | |
7c673cae FG |
317 | { |
318 | if ((arg == NULL) || (arg->ids == NULL)) | |
319 | return bucket->h.items; | |
320 | return arg->ids; | |
321 | } | |
322 | ||
323 | static int bucket_straw2_choose(const struct crush_bucket_straw2 *bucket, | |
324 | int x, int r, const struct crush_choose_arg *arg, | |
325 | int position) | |
326 | { | |
327 | unsigned int i, high = 0; | |
328 | unsigned int u; | |
329 | __s64 ln, draw, high_draw = 0; | |
330 | __u32 *weights = get_choose_arg_weights(bucket, arg, position); | |
224ce89b | 331 | __s32 *ids = get_choose_arg_ids(bucket, arg); |
7c673cae FG |
332 | for (i = 0; i < bucket->h.size; i++) { |
333 | dprintk("weight 0x%x item %d\n", weights[i], ids[i]); | |
334 | if (weights[i]) { | |
335 | u = crush_hash32_3(bucket->h.hash, x, ids[i], r); | |
336 | u &= 0xffff; | |
337 | ||
338 | /* | |
339 | * for some reason slightly less than 0x10000 produces | |
340 | * a slightly more accurate distribution... probably a | |
341 | * rounding effect. | |
342 | * | |
343 | * the natural log lookup table maps [0,0xffff] | |
344 | * (corresponding to real numbers [1/0x10000, 1] to | |
345 | * [0, 0xffffffffffff] (corresponding to real numbers | |
346 | * [-11.090355,0]). | |
347 | */ | |
348 | ln = crush_ln(u) - 0x1000000000000ll; | |
349 | ||
350 | /* | |
351 | * divide by 16.16 fixed-point weight. note | |
352 | * that the ln value is negative, so a larger | |
353 | * weight means a larger (less negative) value | |
354 | * for draw. | |
355 | */ | |
356 | draw = div64_s64(ln, weights[i]); | |
357 | } else { | |
358 | draw = S64_MIN; | |
359 | } | |
360 | ||
361 | if (i == 0 || draw > high_draw) { | |
362 | high = i; | |
363 | high_draw = draw; | |
364 | } | |
365 | } | |
366 | ||
367 | return bucket->h.items[high]; | |
368 | } | |
369 | ||
370 | ||
371 | static int crush_bucket_choose(const struct crush_bucket *in, | |
372 | struct crush_work_bucket *work, | |
373 | int x, int r, | |
374 | const struct crush_choose_arg *arg, | |
375 | int position) | |
376 | { | |
377 | dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r); | |
378 | BUG_ON(in->size == 0); | |
379 | switch (in->alg) { | |
380 | case CRUSH_BUCKET_UNIFORM: | |
381 | return bucket_uniform_choose( | |
382 | (const struct crush_bucket_uniform *)in, | |
383 | work, x, r); | |
384 | case CRUSH_BUCKET_LIST: | |
385 | return bucket_list_choose((const struct crush_bucket_list *)in, | |
386 | x, r); | |
387 | case CRUSH_BUCKET_TREE: | |
388 | return bucket_tree_choose((const struct crush_bucket_tree *)in, | |
389 | x, r); | |
390 | case CRUSH_BUCKET_STRAW: | |
391 | return bucket_straw_choose( | |
392 | (const struct crush_bucket_straw *)in, | |
393 | x, r); | |
394 | case CRUSH_BUCKET_STRAW2: | |
395 | return bucket_straw2_choose( | |
396 | (const struct crush_bucket_straw2 *)in, | |
397 | x, r, arg, position); | |
398 | default: | |
399 | dprintk("unknown bucket %d alg %d\n", in->id, in->alg); | |
400 | return in->items[0]; | |
401 | } | |
402 | } | |
403 | ||
404 | /* | |
405 | * true if device is marked "out" (failed, fully offloaded) | |
406 | * of the cluster | |
407 | */ | |
408 | static int is_out(const struct crush_map *map, | |
409 | const __u32 *weight, int weight_max, | |
410 | int item, int x) | |
411 | { | |
412 | if (item >= weight_max) | |
413 | return 1; | |
414 | if (weight[item] >= 0x10000) | |
415 | return 0; | |
416 | if (weight[item] == 0) | |
417 | return 1; | |
418 | if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff) | |
419 | < weight[item]) | |
420 | return 0; | |
421 | return 1; | |
422 | } | |
423 | ||
424 | /** | |
425 | * crush_choose_firstn - choose numrep distinct items of given type | |
426 | * @map: the crush_map | |
427 | * @bucket: the bucket we are choose an item from | |
428 | * @x: crush input value | |
429 | * @numrep: the number of items to choose | |
430 | * @type: the type of item to choose | |
431 | * @out: pointer to output vector | |
432 | * @outpos: our position in that vector | |
433 | * @out_size: size of the out vector | |
434 | * @tries: number of attempts to make | |
435 | * @recurse_tries: number of attempts to have recursive chooseleaf make | |
436 | * @local_retries: localized retries | |
437 | * @local_fallback_retries: localized fallback retries | |
438 | * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose) | |
439 | * @stable: stable mode starts rep=0 in the recursive call for all replicas | |
440 | * @vary_r: pass r to recursive calls | |
441 | * @out2: second output vector for leaf items (if @recurse_to_leaf) | |
442 | * @parent_r: r value passed from the parent | |
443 | */ | |
444 | static int crush_choose_firstn(const struct crush_map *map, | |
445 | struct crush_work *work, | |
446 | const struct crush_bucket *bucket, | |
447 | const __u32 *weight, int weight_max, | |
448 | int x, int numrep, int type, | |
449 | int *out, int outpos, | |
450 | int out_size, | |
451 | unsigned int tries, | |
452 | unsigned int recurse_tries, | |
453 | unsigned int local_retries, | |
454 | unsigned int local_fallback_retries, | |
455 | int recurse_to_leaf, | |
456 | unsigned int vary_r, | |
457 | unsigned int stable, | |
458 | int *out2, | |
459 | int parent_r, | |
460 | const struct crush_choose_arg *choose_args) | |
461 | { | |
462 | int rep; | |
463 | unsigned int ftotal, flocal; | |
464 | int retry_descent, retry_bucket, skip_rep; | |
465 | const struct crush_bucket *in = bucket; | |
466 | int r; | |
467 | int i; | |
468 | int item = 0; | |
469 | int itemtype; | |
470 | int collide, reject; | |
471 | int count = out_size; | |
472 | ||
473 | dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d tries %d \ | |
474 | recurse_tries %d local_retries %d local_fallback_retries %d \ | |
475 | parent_r %d stable %d\n", | |
476 | recurse_to_leaf ? "_LEAF" : "", | |
477 | bucket->id, x, outpos, numrep, | |
478 | tries, recurse_tries, local_retries, local_fallback_retries, | |
479 | parent_r, stable); | |
480 | ||
481 | for (rep = stable ? 0 : outpos; rep < numrep && count > 0 ; rep++) { | |
482 | /* keep trying until we get a non-out, non-colliding item */ | |
483 | ftotal = 0; | |
484 | skip_rep = 0; | |
485 | do { | |
486 | retry_descent = 0; | |
487 | in = bucket; /* initial bucket */ | |
488 | ||
489 | /* choose through intervening buckets */ | |
490 | flocal = 0; | |
491 | do { | |
492 | collide = 0; | |
493 | retry_bucket = 0; | |
494 | r = rep + parent_r; | |
495 | /* r' = r + f_total */ | |
496 | r += ftotal; | |
497 | ||
498 | /* bucket choose */ | |
499 | if (in->size == 0) { | |
500 | reject = 1; | |
501 | goto reject; | |
502 | } | |
503 | if (local_fallback_retries > 0 && | |
504 | flocal >= (in->size>>1) && | |
505 | flocal > local_fallback_retries) | |
506 | item = bucket_perm_choose( | |
507 | in, work->work[-1-in->id], | |
508 | x, r); | |
509 | else | |
510 | item = crush_bucket_choose( | |
511 | in, work->work[-1-in->id], | |
512 | x, r, | |
513 | (choose_args ? &choose_args[-1-in->id] : 0), | |
514 | outpos); | |
515 | if (item >= map->max_devices) { | |
516 | dprintk(" bad item %d\n", item); | |
517 | skip_rep = 1; | |
518 | break; | |
519 | } | |
520 | ||
521 | /* desired type? */ | |
522 | if (item < 0) | |
523 | itemtype = map->buckets[-1-item]->type; | |
524 | else | |
525 | itemtype = 0; | |
526 | dprintk(" item %d type %d\n", item, itemtype); | |
527 | ||
528 | /* keep going? */ | |
529 | if (itemtype != type) { | |
530 | if (item >= 0 || | |
531 | (-1-item) >= map->max_buckets) { | |
532 | dprintk(" bad item type %d\n", type); | |
533 | skip_rep = 1; | |
534 | break; | |
535 | } | |
536 | in = map->buckets[-1-item]; | |
537 | retry_bucket = 1; | |
538 | continue; | |
539 | } | |
540 | ||
541 | /* collision? */ | |
542 | for (i = 0; i < outpos; i++) { | |
543 | if (out[i] == item) { | |
544 | collide = 1; | |
545 | break; | |
546 | } | |
547 | } | |
548 | ||
549 | reject = 0; | |
550 | if (!collide && recurse_to_leaf) { | |
551 | if (item < 0) { | |
552 | int sub_r; | |
553 | if (vary_r) | |
554 | sub_r = r >> (vary_r-1); | |
555 | else | |
556 | sub_r = 0; | |
557 | if (crush_choose_firstn( | |
558 | map, | |
559 | work, | |
560 | map->buckets[-1-item], | |
561 | weight, weight_max, | |
562 | x, stable ? 1 : outpos+1, 0, | |
563 | out2, outpos, count, | |
564 | recurse_tries, 0, | |
565 | local_retries, | |
566 | local_fallback_retries, | |
567 | 0, | |
568 | vary_r, | |
569 | stable, | |
570 | NULL, | |
571 | sub_r, | |
572 | choose_args) <= outpos) | |
573 | /* didn't get leaf */ | |
574 | reject = 1; | |
575 | } else { | |
576 | /* we already have a leaf! */ | |
577 | out2[outpos] = item; | |
578 | } | |
579 | } | |
580 | ||
581 | if (!reject && !collide) { | |
582 | /* out? */ | |
583 | if (itemtype == 0) | |
584 | reject = is_out(map, weight, | |
585 | weight_max, | |
586 | item, x); | |
587 | } | |
588 | ||
589 | reject: | |
590 | if (reject || collide) { | |
591 | ftotal++; | |
592 | flocal++; | |
593 | ||
594 | if (collide && flocal <= local_retries) | |
595 | /* retry locally a few times */ | |
596 | retry_bucket = 1; | |
597 | else if (local_fallback_retries > 0 && | |
598 | flocal <= in->size + local_fallback_retries) | |
599 | /* exhaustive bucket search */ | |
600 | retry_bucket = 1; | |
601 | else if (ftotal < tries) | |
602 | /* then retry descent */ | |
603 | retry_descent = 1; | |
604 | else | |
605 | /* else give up */ | |
606 | skip_rep = 1; | |
607 | dprintk(" reject %d collide %d " | |
608 | "ftotal %u flocal %u\n", | |
609 | reject, collide, ftotal, | |
610 | flocal); | |
611 | } | |
612 | } while (retry_bucket); | |
613 | } while (retry_descent); | |
614 | ||
615 | if (skip_rep) { | |
616 | dprintk("skip rep\n"); | |
617 | continue; | |
618 | } | |
619 | ||
620 | dprintk("CHOOSE got %d\n", item); | |
621 | out[outpos] = item; | |
622 | outpos++; | |
623 | count--; | |
624 | #ifndef __KERNEL__ | |
625 | if (map->choose_tries && ftotal <= map->choose_total_tries) | |
626 | map->choose_tries[ftotal]++; | |
627 | #endif | |
628 | } | |
629 | ||
630 | dprintk("CHOOSE returns %d\n", outpos); | |
631 | return outpos; | |
632 | } | |
633 | ||
634 | ||
635 | /** | |
636 | * crush_choose_indep: alternative breadth-first positionally stable mapping | |
637 | * | |
638 | */ | |
639 | static void crush_choose_indep(const struct crush_map *map, | |
640 | struct crush_work *work, | |
641 | const struct crush_bucket *bucket, | |
642 | const __u32 *weight, int weight_max, | |
643 | int x, int left, int numrep, int type, | |
644 | int *out, int outpos, | |
645 | unsigned int tries, | |
646 | unsigned int recurse_tries, | |
647 | int recurse_to_leaf, | |
648 | int *out2, | |
649 | int parent_r, | |
650 | const struct crush_choose_arg *choose_args) | |
651 | { | |
652 | const struct crush_bucket *in = bucket; | |
653 | int endpos = outpos + left; | |
654 | int rep; | |
655 | unsigned int ftotal; | |
656 | int r; | |
657 | int i; | |
658 | int item = 0; | |
659 | int itemtype; | |
660 | int collide; | |
661 | ||
662 | dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "", | |
663 | bucket->id, x, outpos, numrep); | |
664 | ||
665 | /* initially my result is undefined */ | |
666 | for (rep = outpos; rep < endpos; rep++) { | |
667 | out[rep] = CRUSH_ITEM_UNDEF; | |
668 | if (out2) | |
669 | out2[rep] = CRUSH_ITEM_UNDEF; | |
670 | } | |
671 | ||
672 | for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) { | |
673 | #ifdef DEBUG_INDEP | |
674 | if (out2 && ftotal) { | |
675 | dprintk("%u %d a: ", ftotal, left); | |
676 | for (rep = outpos; rep < endpos; rep++) { | |
677 | dprintk(" %d", out[rep]); | |
678 | } | |
679 | dprintk("\n"); | |
680 | dprintk("%u %d b: ", ftotal, left); | |
681 | for (rep = outpos; rep < endpos; rep++) { | |
682 | dprintk(" %d", out2[rep]); | |
683 | } | |
684 | dprintk("\n"); | |
685 | } | |
686 | #endif | |
687 | for (rep = outpos; rep < endpos; rep++) { | |
688 | if (out[rep] != CRUSH_ITEM_UNDEF) | |
689 | continue; | |
690 | ||
691 | in = bucket; /* initial bucket */ | |
692 | ||
693 | /* choose through intervening buckets */ | |
694 | for (;;) { | |
695 | /* note: we base the choice on the position | |
696 | * even in the nested call. that means that | |
697 | * if the first layer chooses the same bucket | |
698 | * in a different position, we will tend to | |
699 | * choose a different item in that bucket. | |
700 | * this will involve more devices in data | |
701 | * movement and tend to distribute the load. | |
702 | */ | |
703 | r = rep + parent_r; | |
704 | ||
705 | /* be careful */ | |
706 | if (in->alg == CRUSH_BUCKET_UNIFORM && | |
707 | in->size % numrep == 0) | |
708 | /* r'=r+(n+1)*f_total */ | |
709 | r += (numrep+1) * ftotal; | |
710 | else | |
711 | /* r' = r + n*f_total */ | |
712 | r += numrep * ftotal; | |
713 | ||
714 | /* bucket choose */ | |
715 | if (in->size == 0) { | |
716 | dprintk(" empty bucket\n"); | |
717 | break; | |
718 | } | |
719 | ||
720 | item = crush_bucket_choose( | |
721 | in, work->work[-1-in->id], | |
722 | x, r, | |
723 | (choose_args ? &choose_args[-1-in->id] : 0), | |
724 | outpos); | |
725 | if (item >= map->max_devices) { | |
726 | dprintk(" bad item %d\n", item); | |
727 | out[rep] = CRUSH_ITEM_NONE; | |
728 | if (out2) | |
729 | out2[rep] = CRUSH_ITEM_NONE; | |
730 | left--; | |
731 | break; | |
732 | } | |
733 | ||
734 | /* desired type? */ | |
735 | if (item < 0) | |
736 | itemtype = map->buckets[-1-item]->type; | |
737 | else | |
738 | itemtype = 0; | |
739 | dprintk(" item %d type %d\n", item, itemtype); | |
740 | ||
741 | /* keep going? */ | |
742 | if (itemtype != type) { | |
743 | if (item >= 0 || | |
744 | (-1-item) >= map->max_buckets) { | |
745 | dprintk(" bad item type %d\n", type); | |
746 | out[rep] = CRUSH_ITEM_NONE; | |
747 | if (out2) | |
748 | out2[rep] = | |
749 | CRUSH_ITEM_NONE; | |
750 | left--; | |
751 | break; | |
752 | } | |
753 | in = map->buckets[-1-item]; | |
754 | continue; | |
755 | } | |
756 | ||
757 | /* collision? */ | |
758 | collide = 0; | |
759 | for (i = outpos; i < endpos; i++) { | |
760 | if (out[i] == item) { | |
761 | collide = 1; | |
762 | break; | |
763 | } | |
764 | } | |
765 | if (collide) | |
766 | break; | |
767 | ||
768 | if (recurse_to_leaf) { | |
769 | if (item < 0) { | |
770 | crush_choose_indep( | |
771 | map, | |
772 | work, | |
773 | map->buckets[-1-item], | |
774 | weight, weight_max, | |
775 | x, 1, numrep, 0, | |
776 | out2, rep, | |
777 | recurse_tries, 0, | |
778 | 0, NULL, r, choose_args); | |
779 | if (out2[rep] == CRUSH_ITEM_NONE) { | |
780 | /* placed nothing; no leaf */ | |
781 | break; | |
782 | } | |
783 | } else { | |
784 | /* we already have a leaf! */ | |
785 | out2[rep] = item; | |
786 | } | |
787 | } | |
788 | ||
789 | /* out? */ | |
790 | if (itemtype == 0 && | |
791 | is_out(map, weight, weight_max, item, x)) | |
792 | break; | |
793 | ||
794 | /* yay! */ | |
795 | out[rep] = item; | |
796 | left--; | |
797 | break; | |
798 | } | |
799 | } | |
800 | } | |
801 | for (rep = outpos; rep < endpos; rep++) { | |
802 | if (out[rep] == CRUSH_ITEM_UNDEF) { | |
803 | out[rep] = CRUSH_ITEM_NONE; | |
804 | } | |
805 | if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) { | |
806 | out2[rep] = CRUSH_ITEM_NONE; | |
807 | } | |
808 | } | |
809 | #ifndef __KERNEL__ | |
810 | if (map->choose_tries && ftotal <= map->choose_total_tries) | |
811 | map->choose_tries[ftotal]++; | |
812 | #endif | |
813 | #ifdef DEBUG_INDEP | |
814 | if (out2) { | |
815 | dprintk("%u %d a: ", ftotal, left); | |
816 | for (rep = outpos; rep < endpos; rep++) { | |
817 | dprintk(" %d", out[rep]); | |
818 | } | |
819 | dprintk("\n"); | |
820 | dprintk("%u %d b: ", ftotal, left); | |
821 | for (rep = outpos; rep < endpos; rep++) { | |
822 | dprintk(" %d", out2[rep]); | |
823 | } | |
824 | dprintk("\n"); | |
825 | } | |
826 | #endif | |
827 | } | |
828 | ||
829 | ||
830 | /* This takes a chunk of memory and sets it up to be a shiny new | |
831 | working area for a CRUSH placement computation. It must be called | |
832 | on any newly allocated memory before passing it in to | |
833 | crush_do_rule. It may be used repeatedly after that, so long as the | |
834 | map has not changed. If the map /has/ changed, you must make sure | |
835 | the working size is no smaller than what was allocated and re-run | |
836 | crush_init_workspace. | |
837 | ||
838 | If you do retain the working space between calls to crush, make it | |
839 | thread-local. If you reinstitute the locking I've spent so much | |
840 | time getting rid of, I will be very unhappy with you. */ | |
841 | ||
842 | void crush_init_workspace(const struct crush_map *m, void *v) { | |
843 | /* We work by moving through the available space and setting | |
844 | values and pointers as we go. | |
845 | ||
846 | It's a bit like Forth's use of the 'allot' word since we | |
847 | set the pointer first and then reserve the space for it to | |
848 | point to by incrementing the point. */ | |
849 | struct crush_work *w = (struct crush_work *)v; | |
850 | char *point = (char *)v; | |
851 | __s32 b; | |
852 | point += sizeof(struct crush_work); | |
853 | w->work = (struct crush_work_bucket **)point; | |
854 | point += m->max_buckets * sizeof(struct crush_work_bucket *); | |
855 | for (b = 0; b < m->max_buckets; ++b) { | |
856 | if (m->buckets[b] == 0) | |
857 | continue; | |
858 | ||
859 | w->work[b] = (struct crush_work_bucket *) point; | |
860 | switch (m->buckets[b]->alg) { | |
861 | default: | |
862 | point += sizeof(struct crush_work_bucket); | |
863 | break; | |
864 | } | |
865 | w->work[b]->perm_x = 0; | |
866 | w->work[b]->perm_n = 0; | |
867 | w->work[b]->perm = (__u32 *)point; | |
868 | point += m->buckets[b]->size * sizeof(__u32); | |
869 | } | |
870 | BUG_ON((char *)point - (char *)w != m->working_size); | |
871 | } | |
872 | ||
873 | /** | |
874 | * crush_do_rule - calculate a mapping with the given input and rule | |
875 | * @map: the crush_map | |
876 | * @ruleno: the rule id | |
877 | * @x: hash input | |
878 | * @result: pointer to result vector | |
879 | * @result_max: maximum result size | |
880 | * @weight: weight vector (for map leaves) | |
881 | * @weight_max: size of weight vector | |
882 | * @cwin: Pointer to at least map->working_size bytes of memory or NULL. | |
883 | */ | |
884 | int crush_do_rule(const struct crush_map *map, | |
885 | int ruleno, int x, int *result, int result_max, | |
886 | const __u32 *weight, int weight_max, | |
887 | void *cwin, const struct crush_choose_arg *choose_args) | |
888 | { | |
889 | int result_len; | |
890 | struct crush_work *cw = cwin; | |
891 | int *a = (int *)((char *)cw + map->working_size); | |
892 | int *b = a + result_max; | |
893 | int *c = b + result_max; | |
894 | int *w = a; | |
895 | int *o = b; | |
896 | int recurse_to_leaf; | |
897 | int wsize = 0; | |
898 | int osize; | |
899 | int *tmp; | |
900 | const struct crush_rule *rule; | |
901 | __u32 step; | |
902 | int i, j; | |
903 | int numrep; | |
904 | int out_size; | |
905 | /* | |
906 | * the original choose_total_tries value was off by one (it | |
907 | * counted "retries" and not "tries"). add one. | |
908 | */ | |
909 | int choose_tries = map->choose_total_tries + 1; | |
910 | int choose_leaf_tries = 0; | |
911 | /* | |
912 | * the local tries values were counted as "retries", though, | |
913 | * and need no adjustment | |
914 | */ | |
915 | int choose_local_retries = map->choose_local_tries; | |
916 | int choose_local_fallback_retries = map->choose_local_fallback_tries; | |
917 | ||
918 | int vary_r = map->chooseleaf_vary_r; | |
919 | int stable = map->chooseleaf_stable; | |
920 | ||
921 | if ((__u32)ruleno >= map->max_rules) { | |
922 | dprintk(" bad ruleno %d\n", ruleno); | |
923 | return 0; | |
924 | } | |
925 | ||
926 | rule = map->rules[ruleno]; | |
927 | result_len = 0; | |
928 | ||
929 | for (step = 0; step < rule->len; step++) { | |
930 | int firstn = 0; | |
931 | const struct crush_rule_step *curstep = &rule->steps[step]; | |
932 | ||
933 | switch (curstep->op) { | |
934 | case CRUSH_RULE_TAKE: | |
935 | if ((curstep->arg1 >= 0 && | |
936 | curstep->arg1 < map->max_devices) || | |
937 | (-1-curstep->arg1 >= 0 && | |
938 | -1-curstep->arg1 < map->max_buckets && | |
939 | map->buckets[-1-curstep->arg1])) { | |
940 | w[0] = curstep->arg1; | |
941 | wsize = 1; | |
942 | } else { | |
943 | dprintk(" bad take value %d\n", curstep->arg1); | |
944 | } | |
945 | break; | |
946 | ||
947 | case CRUSH_RULE_SET_CHOOSE_TRIES: | |
948 | if (curstep->arg1 > 0) | |
949 | choose_tries = curstep->arg1; | |
950 | break; | |
951 | ||
952 | case CRUSH_RULE_SET_CHOOSELEAF_TRIES: | |
953 | if (curstep->arg1 > 0) | |
954 | choose_leaf_tries = curstep->arg1; | |
955 | break; | |
956 | ||
957 | case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES: | |
958 | if (curstep->arg1 >= 0) | |
959 | choose_local_retries = curstep->arg1; | |
960 | break; | |
961 | ||
962 | case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES: | |
963 | if (curstep->arg1 >= 0) | |
964 | choose_local_fallback_retries = curstep->arg1; | |
965 | break; | |
966 | ||
967 | case CRUSH_RULE_SET_CHOOSELEAF_VARY_R: | |
968 | if (curstep->arg1 >= 0) | |
969 | vary_r = curstep->arg1; | |
970 | break; | |
971 | ||
972 | case CRUSH_RULE_SET_CHOOSELEAF_STABLE: | |
973 | if (curstep->arg1 >= 0) | |
974 | stable = curstep->arg1; | |
975 | break; | |
976 | ||
977 | case CRUSH_RULE_CHOOSELEAF_FIRSTN: | |
978 | case CRUSH_RULE_CHOOSE_FIRSTN: | |
979 | firstn = 1; | |
980 | /* fall through */ | |
981 | case CRUSH_RULE_CHOOSELEAF_INDEP: | |
982 | case CRUSH_RULE_CHOOSE_INDEP: | |
983 | if (wsize == 0) | |
984 | break; | |
985 | ||
986 | recurse_to_leaf = | |
987 | curstep->op == | |
988 | CRUSH_RULE_CHOOSELEAF_FIRSTN || | |
989 | curstep->op == | |
990 | CRUSH_RULE_CHOOSELEAF_INDEP; | |
991 | ||
992 | /* reset output */ | |
993 | osize = 0; | |
994 | ||
995 | for (i = 0; i < wsize; i++) { | |
996 | int bno; | |
997 | numrep = curstep->arg1; | |
998 | if (numrep <= 0) { | |
999 | numrep += result_max; | |
1000 | if (numrep <= 0) | |
1001 | continue; | |
1002 | } | |
1003 | j = 0; | |
1004 | /* make sure bucket id is valid */ | |
1005 | bno = -1 - w[i]; | |
1006 | if (bno < 0 || bno >= map->max_buckets) { | |
1007 | // w[i] is probably CRUSH_ITEM_NONE | |
1008 | dprintk(" bad w[i] %d\n", w[i]); | |
1009 | continue; | |
1010 | } | |
1011 | if (firstn) { | |
1012 | int recurse_tries; | |
1013 | if (choose_leaf_tries) | |
1014 | recurse_tries = | |
1015 | choose_leaf_tries; | |
1016 | else if (map->chooseleaf_descend_once) | |
1017 | recurse_tries = 1; | |
1018 | else | |
1019 | recurse_tries = choose_tries; | |
1020 | osize += crush_choose_firstn( | |
1021 | map, | |
1022 | cw, | |
1023 | map->buckets[bno], | |
1024 | weight, weight_max, | |
1025 | x, numrep, | |
1026 | curstep->arg2, | |
1027 | o+osize, j, | |
1028 | result_max-osize, | |
1029 | choose_tries, | |
1030 | recurse_tries, | |
1031 | choose_local_retries, | |
1032 | choose_local_fallback_retries, | |
1033 | recurse_to_leaf, | |
1034 | vary_r, | |
1035 | stable, | |
1036 | c+osize, | |
1037 | 0, | |
1038 | choose_args); | |
1039 | } else { | |
1040 | out_size = ((numrep < (result_max-osize)) ? | |
1041 | numrep : (result_max-osize)); | |
1042 | crush_choose_indep( | |
1043 | map, | |
1044 | cw, | |
1045 | map->buckets[bno], | |
1046 | weight, weight_max, | |
1047 | x, out_size, numrep, | |
1048 | curstep->arg2, | |
1049 | o+osize, j, | |
1050 | choose_tries, | |
1051 | choose_leaf_tries ? | |
1052 | choose_leaf_tries : 1, | |
1053 | recurse_to_leaf, | |
1054 | c+osize, | |
1055 | 0, | |
1056 | choose_args); | |
1057 | osize += out_size; | |
1058 | } | |
1059 | } | |
1060 | ||
1061 | if (recurse_to_leaf) | |
1062 | /* copy final _leaf_ values to output set */ | |
1063 | memcpy(o, c, osize*sizeof(*o)); | |
1064 | ||
1065 | /* swap o and w arrays */ | |
1066 | tmp = o; | |
1067 | o = w; | |
1068 | w = tmp; | |
1069 | wsize = osize; | |
1070 | break; | |
1071 | ||
1072 | ||
1073 | case CRUSH_RULE_EMIT: | |
1074 | for (i = 0; i < wsize && result_len < result_max; i++) { | |
1075 | result[result_len] = w[i]; | |
1076 | result_len++; | |
1077 | } | |
1078 | wsize = 0; | |
1079 | break; | |
1080 | ||
1081 | default: | |
1082 | dprintk(" unknown op %d at step %d\n", | |
1083 | curstep->op, step); | |
1084 | break; | |
1085 | } | |
1086 | } | |
1087 | ||
1088 | return result_len; | |
1089 | } |