blob: 00baad5d3bdee37c2cb6f91975b4fe2046575960 [file] [log] [blame]
Sage Weil5ecc0a02009-10-06 11:31:11 -07001
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
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070021#include <linux/crush/crush.h>
22#include <linux/crush/hash.h>
Sage Weil5ecc0a02009-10-06 11:31:11 -070023
24/*
25 * Implement the core CRUSH mapping algorithm.
26 */
27
28/**
29 * crush_find_rule - find a crush_rule id for a given ruleset, type, and size.
30 * @map: the crush_map
31 * @ruleset: the storage ruleset id (user defined)
32 * @type: storage ruleset type (user defined)
33 * @size: output set size
34 */
Sage Weil8b12d472012-05-07 15:38:35 -070035int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
Sage Weil5ecc0a02009-10-06 11:31:11 -070036{
Sage Weil8b12d472012-05-07 15:38:35 -070037 __u32 i;
Sage Weil5ecc0a02009-10-06 11:31:11 -070038
39 for (i = 0; i < map->max_rules; i++) {
40 if (map->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)
45 return i;
46 }
47 return -1;
48}
49
50
51/*
52 * bucket choose methods
53 *
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.
57 */
58
59/*
60 * Choose based on a random permutation of the bucket.
61 *
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.
67 */
68static int bucket_perm_choose(struct crush_bucket *bucket,
69 int x, int r)
70{
71 unsigned pr = r % bucket->size;
72 unsigned i, s;
73
74 /* start a new permutation if @x has changed */
Sage Weil8b12d472012-05-07 15:38:35 -070075 if (bucket->perm_x != (__u32)x || bucket->perm_n == 0) {
Sage Weil5ecc0a02009-10-06 11:31:11 -070076 dprintk("bucket %d new x=%d\n", bucket->id, x);
77 bucket->perm_x = x;
78
79 /* optimize common r=0 case */
80 if (pr == 0) {
Sage Weilfb690392009-11-07 20:18:22 -080081 s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
Sage Weil5ecc0a02009-10-06 11:31:11 -070082 bucket->size;
83 bucket->perm[0] = s;
84 bucket->perm_n = 0xffff; /* magic value, see below */
85 goto out;
86 }
87
88 for (i = 0; i < bucket->size; i++)
89 bucket->perm[i] = i;
90 bucket->perm_n = 0;
91 } else if (bucket->perm_n == 0xffff) {
92 /* clean up after the r=0 case above */
93 for (i = 1; i < bucket->size; i++)
94 bucket->perm[i] = i;
95 bucket->perm[bucket->perm[0]] = 0;
96 bucket->perm_n = 1;
97 }
98
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) {
Sage Weilfb690392009-11-07 20:18:22 -0800106 i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
Sage Weil5ecc0a02009-10-06 11:31:11 -0700107 (bucket->size - p);
108 if (i) {
109 unsigned t = bucket->perm[p + i];
110 bucket->perm[p + i] = bucket->perm[p];
111 bucket->perm[p] = t;
112 }
113 dprintk(" perm_choose swap %d with %d\n", p, p+i);
114 }
115 bucket->perm_n++;
116 }
117 for (i = 0; i < bucket->size; i++)
118 dprintk(" perm_choose %d: %d\n", i, bucket->perm[i]);
119
120 s = bucket->perm[pr];
121out:
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];
125}
126
127/* uniform */
128static int bucket_uniform_choose(struct crush_bucket_uniform *bucket,
129 int x, int r)
130{
131 return bucket_perm_choose(&bucket->h, x, r);
132}
133
134/* list */
135static int bucket_list_choose(struct crush_bucket_list *bucket,
136 int x, int r)
137{
138 int i;
139
140 for (i = bucket->h.size-1; i >= 0; i--) {
Sage Weilfb690392009-11-07 20:18:22 -0800141 __u64 w = crush_hash32_4(bucket->h.hash,x, bucket->h.items[i],
142 r, bucket->h.id);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700143 w &= 0xffff;
144 dprintk("list_choose i=%d x=%d r=%d item %d weight %x "
145 "sw %x rand %llx",
146 i, x, r, bucket->h.items[i], bucket->item_weights[i],
147 bucket->sum_weights[i], w);
148 w *= bucket->sum_weights[i];
149 w = w >> 16;
150 /*dprintk(" scaled %llx\n", w);*/
151 if (w < bucket->item_weights[i])
152 return bucket->h.items[i];
153 }
154
Sage Weila1f48952012-05-07 15:35:24 -0700155 dprintk("bad list sums for bucket %d\n", bucket->h.id);
156 return bucket->h.items[0];
Sage Weil5ecc0a02009-10-06 11:31:11 -0700157}
158
159
160/* (binary) tree */
161static int height(int n)
162{
163 int h = 0;
164 while ((n & 1) == 0) {
165 h++;
166 n = n >> 1;
167 }
168 return h;
169}
170
171static int left(int x)
172{
173 int h = height(x);
174 return x - (1 << (h-1));
175}
176
177static int right(int x)
178{
179 int h = height(x);
180 return x + (1 << (h-1));
181}
182
183static int terminal(int x)
184{
185 return x & 1;
186}
187
188static int bucket_tree_choose(struct crush_bucket_tree *bucket,
189 int x, int r)
190{
191 int n, l;
192 __u32 w;
193 __u64 t;
194
195 /* start at root */
196 n = bucket->num_nodes >> 1;
197
198 while (!terminal(n)) {
199 /* pick point in [0, w) */
200 w = bucket->node_weights[n];
Sage Weilfb690392009-11-07 20:18:22 -0800201 t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
202 bucket->h.id) * (__u64)w;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700203 t = t >> 32;
204
205 /* descend to the left or right? */
206 l = left(n);
207 if (t < bucket->node_weights[l])
208 n = l;
209 else
210 n = right(n);
211 }
212
213 return bucket->h.items[n >> 1];
214}
215
216
217/* straw */
218
219static int bucket_straw_choose(struct crush_bucket_straw *bucket,
220 int x, int r)
221{
Sage Weil8b12d472012-05-07 15:38:35 -0700222 __u32 i;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700223 int high = 0;
224 __u64 high_draw = 0;
225 __u64 draw;
226
227 for (i = 0; i < bucket->h.size; i++) {
Sage Weilfb690392009-11-07 20:18:22 -0800228 draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700229 draw &= 0xffff;
230 draw *= bucket->straws[i];
231 if (i == 0 || draw > high_draw) {
232 high = i;
233 high_draw = draw;
234 }
235 }
236 return bucket->h.items[high];
237}
238
239static int crush_bucket_choose(struct crush_bucket *in, int x, int r)
240{
Sage Weila1a31e72010-06-24 12:58:14 -0700241 dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
Sage Weila1f48952012-05-07 15:35:24 -0700242 BUG_ON(in->size == 0);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700243 switch (in->alg) {
244 case CRUSH_BUCKET_UNIFORM:
245 return bucket_uniform_choose((struct crush_bucket_uniform *)in,
246 x, r);
247 case CRUSH_BUCKET_LIST:
248 return bucket_list_choose((struct crush_bucket_list *)in,
249 x, r);
250 case CRUSH_BUCKET_TREE:
251 return bucket_tree_choose((struct crush_bucket_tree *)in,
252 x, r);
253 case CRUSH_BUCKET_STRAW:
254 return bucket_straw_choose((struct crush_bucket_straw *)in,
255 x, r);
256 default:
Sage Weila1f48952012-05-07 15:35:24 -0700257 dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
Sage Weil50b885b2009-12-01 14:12:07 -0800258 return in->items[0];
Sage Weil5ecc0a02009-10-06 11:31:11 -0700259 }
260}
261
262/*
263 * true if device is marked "out" (failed, fully offloaded)
264 * of the cluster
265 */
Sage Weil8b12d472012-05-07 15:38:35 -0700266static int is_out(const struct crush_map *map, const __u32 *weight, int item, int x)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700267{
Sage Weil153a1092010-07-05 09:44:17 -0700268 if (weight[item] >= 0x10000)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700269 return 0;
270 if (weight[item] == 0)
271 return 1;
Sage Weilfb690392009-11-07 20:18:22 -0800272 if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
273 < weight[item])
Sage Weil5ecc0a02009-10-06 11:31:11 -0700274 return 0;
275 return 1;
276}
277
278/**
279 * crush_choose - choose numrep distinct items of given type
280 * @map: the crush_map
281 * @bucket: the bucket we are choose an item from
282 * @x: crush input value
283 * @numrep: the number of items to choose
284 * @type: the type of item to choose
285 * @out: pointer to output vector
286 * @outpos: our position in that vector
287 * @firstn: true if choosing "first n" items, false if choosing "indep"
288 * @recurse_to_leaf: true if we want one device under each item of given type
289 * @out2: second output vector for leaf items (if @recurse_to_leaf)
290 */
Sage Weil8b12d472012-05-07 15:38:35 -0700291static int crush_choose(const struct crush_map *map,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700292 struct crush_bucket *bucket,
Sage Weil8b12d472012-05-07 15:38:35 -0700293 const __u32 *weight,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700294 int x, int numrep, int type,
295 int *out, int outpos,
296 int firstn, int recurse_to_leaf,
297 int *out2)
298{
299 int rep;
Sage Weil8b12d472012-05-07 15:38:35 -0700300 unsigned int ftotal, flocal;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700301 int retry_descent, retry_bucket, skip_rep;
302 struct crush_bucket *in = bucket;
303 int r;
304 int i;
Sage Weilb28813a2009-10-07 10:59:34 -0700305 int item = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700306 int itemtype;
307 int collide, reject;
Sage Weil8b12d472012-05-07 15:38:35 -0700308 const unsigned int orig_tries = 5; /* attempts before we fall back to search */
Sage Weila1a31e72010-06-24 12:58:14 -0700309
310 dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
311 bucket->id, x, outpos, numrep);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700312
313 for (rep = outpos; rep < numrep; rep++) {
314 /* keep trying until we get a non-out, non-colliding item */
315 ftotal = 0;
316 skip_rep = 0;
317 do {
318 retry_descent = 0;
319 in = bucket; /* initial bucket */
320
321 /* choose through intervening buckets */
322 flocal = 0;
323 do {
Sage Weilb28813a2009-10-07 10:59:34 -0700324 collide = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700325 retry_bucket = 0;
326 r = rep;
327 if (in->alg == CRUSH_BUCKET_UNIFORM) {
328 /* be careful */
Sage Weil8b12d472012-05-07 15:38:35 -0700329 if (firstn || (__u32)numrep >= in->size)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700330 /* r' = r + f_total */
331 r += ftotal;
332 else if (in->size % numrep == 0)
333 /* r'=r+(n+1)*f_local */
334 r += (numrep+1) *
335 (flocal+ftotal);
336 else
337 /* r' = r + n*f_local */
338 r += numrep * (flocal+ftotal);
339 } else {
340 if (firstn)
341 /* r' = r + f_total */
342 r += ftotal;
343 else
344 /* r' = r + n*f_local */
345 r += numrep * (flocal+ftotal);
346 }
347
348 /* bucket choose */
Sage Weilb28813a2009-10-07 10:59:34 -0700349 if (in->size == 0) {
350 reject = 1;
351 goto reject;
352 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700353 if (flocal >= (in->size>>1) &&
354 flocal > orig_tries)
355 item = bucket_perm_choose(in, x, r);
356 else
357 item = crush_bucket_choose(in, x, r);
Sage Weila1f48952012-05-07 15:35:24 -0700358 if (item >= map->max_devices) {
359 dprintk(" bad item %d\n", item);
360 skip_rep = 1;
361 break;
362 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700363
364 /* desired type? */
365 if (item < 0)
366 itemtype = map->buckets[-1-item]->type;
367 else
368 itemtype = 0;
369 dprintk(" item %d type %d\n", item, itemtype);
370
371 /* keep going? */
372 if (itemtype != type) {
Sage Weila1f48952012-05-07 15:35:24 -0700373 if (item >= 0 ||
374 (-1-item) >= map->max_buckets) {
375 dprintk(" bad item type %d\n", type);
376 skip_rep = 1;
377 break;
378 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700379 in = map->buckets[-1-item];
Sage Weil55bda7a2010-06-24 12:55:48 -0700380 retry_bucket = 1;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700381 continue;
382 }
383
384 /* collision? */
Sage Weil5ecc0a02009-10-06 11:31:11 -0700385 for (i = 0; i < outpos; i++) {
386 if (out[i] == item) {
387 collide = 1;
388 break;
389 }
390 }
391
Sage Weila1a31e72010-06-24 12:58:14 -0700392 reject = 0;
393 if (recurse_to_leaf) {
394 if (item < 0) {
395 if (crush_choose(map,
396 map->buckets[-1-item],
397 weight,
398 x, outpos+1, 0,
399 out2, outpos,
400 firstn, 0,
401 NULL) <= outpos)
402 /* didn't get leaf */
403 reject = 1;
404 } else {
405 /* we already have a leaf! */
406 out2[outpos] = item;
407 }
408 }
409
410 if (!reject) {
Sage Weil5ecc0a02009-10-06 11:31:11 -0700411 /* out? */
412 if (itemtype == 0)
413 reject = is_out(map, weight,
414 item, x);
415 else
416 reject = 0;
417 }
418
Sage Weilb28813a2009-10-07 10:59:34 -0700419reject:
Sage Weil5ecc0a02009-10-06 11:31:11 -0700420 if (reject || collide) {
421 ftotal++;
422 flocal++;
423
424 if (collide && flocal < 3)
425 /* retry locally a few times */
426 retry_bucket = 1;
Sage Weilc90f95e2012-05-07 15:35:09 -0700427 else if (flocal <= in->size + orig_tries)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700428 /* exhaustive bucket search */
429 retry_bucket = 1;
430 else if (ftotal < 20)
431 /* then retry descent */
432 retry_descent = 1;
433 else
434 /* else give up */
435 skip_rep = 1;
436 dprintk(" reject %d collide %d "
Sage Weil8b12d472012-05-07 15:38:35 -0700437 "ftotal %u flocal %u\n",
Sage Weil5ecc0a02009-10-06 11:31:11 -0700438 reject, collide, ftotal,
439 flocal);
440 }
441 } while (retry_bucket);
442 } while (retry_descent);
443
444 if (skip_rep) {
445 dprintk("skip rep\n");
446 continue;
447 }
448
Sage Weila1a31e72010-06-24 12:58:14 -0700449 dprintk("CHOOSE got %d\n", item);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700450 out[outpos] = item;
451 outpos++;
452 }
453
Sage Weila1a31e72010-06-24 12:58:14 -0700454 dprintk("CHOOSE returns %d\n", outpos);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700455 return outpos;
456}
457
458
459/**
460 * crush_do_rule - calculate a mapping with the given input and rule
461 * @map: the crush_map
462 * @ruleno: the rule id
463 * @x: hash input
464 * @result: pointer to result vector
465 * @result_max: maximum result size
466 * @force: force initial replica choice; -1 for none
467 */
Sage Weil8b12d472012-05-07 15:38:35 -0700468int crush_do_rule(const struct crush_map *map,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700469 int ruleno, int x, int *result, int result_max,
Sage Weil8b12d472012-05-07 15:38:35 -0700470 int force, const __u32 *weight)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700471{
472 int result_len;
473 int force_context[CRUSH_MAX_DEPTH];
474 int force_pos = -1;
475 int a[CRUSH_MAX_SET];
476 int b[CRUSH_MAX_SET];
477 int c[CRUSH_MAX_SET];
478 int recurse_to_leaf;
479 int *w;
480 int wsize = 0;
481 int *o;
482 int osize;
483 int *tmp;
484 struct crush_rule *rule;
Sage Weil8b12d472012-05-07 15:38:35 -0700485 __u32 step;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700486 int i, j;
487 int numrep;
488 int firstn;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700489
Sage Weila1f48952012-05-07 15:35:24 -0700490 if ((__u32)ruleno >= map->max_rules) {
491 dprintk(" bad ruleno %d\n", ruleno);
492 return 0;
493 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700494
495 rule = map->rules[ruleno];
496 result_len = 0;
497 w = a;
498 o = b;
499
500 /*
501 * determine hierarchical context of force, if any. note
502 * that this may or may not correspond to the specific types
Sage Weil8b12d472012-05-07 15:38:35 -0700503 * referenced by the crush rule. it will also only affect
504 * the first descent (TAKE).
Sage Weil5ecc0a02009-10-06 11:31:11 -0700505 */
Sage Weilf1932fc2011-12-07 09:10:26 -0800506 if (force >= 0 &&
507 force < map->max_devices &&
508 map->device_parents[force] != 0 &&
509 !is_out(map, weight, force, x)) {
510 while (1) {
511 force_context[++force_pos] = force;
512 if (force >= 0)
513 force = map->device_parents[force];
514 else
515 force = map->bucket_parents[-1-force];
516 if (force == 0)
517 break;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700518 }
519 }
520
521 for (step = 0; step < rule->len; step++) {
522 firstn = 0;
523 switch (rule->steps[step].op) {
524 case CRUSH_RULE_TAKE:
525 w[0] = rule->steps[step].arg1;
Sage Weile11b05d2011-12-12 09:35:22 -0800526
527 /* find position in force_context/hierarchy */
528 while (force_pos >= 0 &&
529 force_context[force_pos] != w[0])
Sage Weil5ecc0a02009-10-06 11:31:11 -0700530 force_pos--;
Sage Weile11b05d2011-12-12 09:35:22 -0800531 /* and move past it */
532 if (force_pos >= 0)
533 force_pos--;
534
Sage Weil5ecc0a02009-10-06 11:31:11 -0700535 wsize = 1;
536 break;
537
538 case CRUSH_RULE_CHOOSE_LEAF_FIRSTN:
539 case CRUSH_RULE_CHOOSE_FIRSTN:
540 firstn = 1;
541 case CRUSH_RULE_CHOOSE_LEAF_INDEP:
542 case CRUSH_RULE_CHOOSE_INDEP:
Sage Weila1f48952012-05-07 15:35:24 -0700543 if (wsize == 0)
544 break;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700545
546 recurse_to_leaf =
547 rule->steps[step].op ==
548 CRUSH_RULE_CHOOSE_LEAF_FIRSTN ||
549 rule->steps[step].op ==
550 CRUSH_RULE_CHOOSE_LEAF_INDEP;
551
552 /* reset output */
553 osize = 0;
554
555 for (i = 0; i < wsize; i++) {
556 /*
557 * see CRUSH_N, CRUSH_N_MINUS macros.
558 * basically, numrep <= 0 means relative to
559 * the provided result_max
560 */
561 numrep = rule->steps[step].arg1;
562 if (numrep <= 0) {
563 numrep += result_max;
564 if (numrep <= 0)
565 continue;
566 }
567 j = 0;
568 if (osize == 0 && force_pos >= 0) {
569 /* skip any intermediate types */
570 while (force_pos &&
571 force_context[force_pos] < 0 &&
572 rule->steps[step].arg2 !=
573 map->buckets[-1 -
574 force_context[force_pos]]->type)
575 force_pos--;
576 o[osize] = force_context[force_pos];
577 if (recurse_to_leaf)
578 c[osize] = force_context[0];
579 j++;
580 force_pos--;
581 }
582 osize += crush_choose(map,
583 map->buckets[-1-w[i]],
584 weight,
585 x, numrep,
586 rule->steps[step].arg2,
587 o+osize, j,
588 firstn,
589 recurse_to_leaf, c+osize);
590 }
591
592 if (recurse_to_leaf)
593 /* copy final _leaf_ values to output set */
594 memcpy(o, c, osize*sizeof(*o));
595
596 /* swap t and w arrays */
597 tmp = o;
598 o = w;
599 w = tmp;
600 wsize = osize;
601 break;
602
603
604 case CRUSH_RULE_EMIT:
605 for (i = 0; i < wsize && result_len < result_max; i++) {
606 result[result_len] = w[i];
607 result_len++;
608 }
609 wsize = 0;
610 break;
611
612 default:
Sage Weila1f48952012-05-07 15:35:24 -0700613 dprintk(" unknown op %d at step %d\n",
614 curstep->op, step);
615 break;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700616 }
617 }
Sage Weilf1932fc2011-12-07 09:10:26 -0800618 return result_len;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700619}
620
621