blob: c268393adfcb9307b06e98ee32305ac8fa404347 [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
21#include "crush.h"
22#include "hash.h"
23
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 */
35int crush_find_rule(struct crush_map *map, int ruleset, int type, int size)
36{
37 int i;
38
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 */
75 if (bucket->perm_x != x || bucket->perm_n == 0) {
76 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) {
81 s = crush_hash32_3(x, bucket->id, 0) %
82 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) {
106 i = crush_hash32_3(x, bucket->id, p) %
107 (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--) {
141 __u64 w = crush_hash32_4(x, bucket->h.items[i], r,
142 bucket->h.id);
143 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
155 BUG_ON(1);
156 return 0;
157}
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];
201 t = (__u64)crush_hash32_4(x, n, r, bucket->h.id) * (__u64)w;
202 t = t >> 32;
203
204 /* descend to the left or right? */
205 l = left(n);
206 if (t < bucket->node_weights[l])
207 n = l;
208 else
209 n = right(n);
210 }
211
212 return bucket->h.items[n >> 1];
213}
214
215
216/* straw */
217
218static int bucket_straw_choose(struct crush_bucket_straw *bucket,
219 int x, int r)
220{
221 int i;
222 int high = 0;
223 __u64 high_draw = 0;
224 __u64 draw;
225
226 for (i = 0; i < bucket->h.size; i++) {
227 draw = crush_hash32_3(x, bucket->h.items[i], r);
228 draw &= 0xffff;
229 draw *= bucket->straws[i];
230 if (i == 0 || draw > high_draw) {
231 high = i;
232 high_draw = draw;
233 }
234 }
235 return bucket->h.items[high];
236}
237
238static int crush_bucket_choose(struct crush_bucket *in, int x, int r)
239{
240 dprintk("choose %d x=%d r=%d\n", in->id, x, r);
241 switch (in->alg) {
242 case CRUSH_BUCKET_UNIFORM:
243 return bucket_uniform_choose((struct crush_bucket_uniform *)in,
244 x, r);
245 case CRUSH_BUCKET_LIST:
246 return bucket_list_choose((struct crush_bucket_list *)in,
247 x, r);
248 case CRUSH_BUCKET_TREE:
249 return bucket_tree_choose((struct crush_bucket_tree *)in,
250 x, r);
251 case CRUSH_BUCKET_STRAW:
252 return bucket_straw_choose((struct crush_bucket_straw *)in,
253 x, r);
254 default:
255 BUG_ON(1);
256/* return in->items[0] */;
257 }
258}
259
260/*
261 * true if device is marked "out" (failed, fully offloaded)
262 * of the cluster
263 */
264static int is_out(struct crush_map *map, __u32 *weight, int item, int x)
265{
266 if (weight[item] >= 0x1000)
267 return 0;
268 if (weight[item] == 0)
269 return 1;
270 if ((crush_hash32_2(x, item) & 0xffff) < weight[item])
271 return 0;
272 return 1;
273}
274
275/**
276 * crush_choose - choose numrep distinct items of given type
277 * @map: the crush_map
278 * @bucket: the bucket we are choose an item from
279 * @x: crush input value
280 * @numrep: the number of items to choose
281 * @type: the type of item to choose
282 * @out: pointer to output vector
283 * @outpos: our position in that vector
284 * @firstn: true if choosing "first n" items, false if choosing "indep"
285 * @recurse_to_leaf: true if we want one device under each item of given type
286 * @out2: second output vector for leaf items (if @recurse_to_leaf)
287 */
288static int crush_choose(struct crush_map *map,
289 struct crush_bucket *bucket,
290 __u32 *weight,
291 int x, int numrep, int type,
292 int *out, int outpos,
293 int firstn, int recurse_to_leaf,
294 int *out2)
295{
296 int rep;
297 int ftotal, flocal;
298 int retry_descent, retry_bucket, skip_rep;
299 struct crush_bucket *in = bucket;
300 int r;
301 int i;
Sage Weilb28813a2009-10-07 10:59:34 -0700302 int item = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700303 int itemtype;
304 int collide, reject;
305 const int orig_tries = 5; /* attempts before we fall back to search */
306 dprintk("choose bucket %d x %d outpos %d\n", bucket->id, x, outpos);
307
308 for (rep = outpos; rep < numrep; rep++) {
309 /* keep trying until we get a non-out, non-colliding item */
310 ftotal = 0;
311 skip_rep = 0;
312 do {
313 retry_descent = 0;
314 in = bucket; /* initial bucket */
315
316 /* choose through intervening buckets */
317 flocal = 0;
318 do {
Sage Weilb28813a2009-10-07 10:59:34 -0700319 collide = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700320 retry_bucket = 0;
321 r = rep;
322 if (in->alg == CRUSH_BUCKET_UNIFORM) {
323 /* be careful */
324 if (firstn || numrep >= in->size)
325 /* r' = r + f_total */
326 r += ftotal;
327 else if (in->size % numrep == 0)
328 /* r'=r+(n+1)*f_local */
329 r += (numrep+1) *
330 (flocal+ftotal);
331 else
332 /* r' = r + n*f_local */
333 r += numrep * (flocal+ftotal);
334 } else {
335 if (firstn)
336 /* r' = r + f_total */
337 r += ftotal;
338 else
339 /* r' = r + n*f_local */
340 r += numrep * (flocal+ftotal);
341 }
342
343 /* bucket choose */
Sage Weilb28813a2009-10-07 10:59:34 -0700344 if (in->size == 0) {
345 reject = 1;
346 goto reject;
347 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700348 if (flocal >= (in->size>>1) &&
349 flocal > orig_tries)
350 item = bucket_perm_choose(in, x, r);
351 else
352 item = crush_bucket_choose(in, x, r);
353 BUG_ON(item >= map->max_devices);
354
355 /* desired type? */
356 if (item < 0)
357 itemtype = map->buckets[-1-item]->type;
358 else
359 itemtype = 0;
360 dprintk(" item %d type %d\n", item, itemtype);
361
362 /* keep going? */
363 if (itemtype != type) {
364 BUG_ON(item >= 0 ||
365 (-1-item) >= map->max_buckets);
366 in = map->buckets[-1-item];
367 continue;
368 }
369
370 /* collision? */
Sage Weil5ecc0a02009-10-06 11:31:11 -0700371 for (i = 0; i < outpos; i++) {
372 if (out[i] == item) {
373 collide = 1;
374 break;
375 }
376 }
377
378 if (recurse_to_leaf &&
379 item < 0 &&
380 crush_choose(map, map->buckets[-1-item],
381 weight,
382 x, outpos+1, 0,
383 out2, outpos,
384 firstn, 0, NULL) <= outpos) {
385 reject = 1;
386 } else {
387 /* out? */
388 if (itemtype == 0)
389 reject = is_out(map, weight,
390 item, x);
391 else
392 reject = 0;
393 }
394
Sage Weilb28813a2009-10-07 10:59:34 -0700395reject:
Sage Weil5ecc0a02009-10-06 11:31:11 -0700396 if (reject || collide) {
397 ftotal++;
398 flocal++;
399
400 if (collide && flocal < 3)
401 /* retry locally a few times */
402 retry_bucket = 1;
403 else if (flocal < in->size + orig_tries)
404 /* exhaustive bucket search */
405 retry_bucket = 1;
406 else if (ftotal < 20)
407 /* then retry descent */
408 retry_descent = 1;
409 else
410 /* else give up */
411 skip_rep = 1;
412 dprintk(" reject %d collide %d "
413 "ftotal %d flocal %d\n",
414 reject, collide, ftotal,
415 flocal);
416 }
417 } while (retry_bucket);
418 } while (retry_descent);
419
420 if (skip_rep) {
421 dprintk("skip rep\n");
422 continue;
423 }
424
425 dprintk("choose got %d\n", item);
426 out[outpos] = item;
427 outpos++;
428 }
429
430 dprintk("choose returns %d\n", outpos);
431 return outpos;
432}
433
434
435/**
436 * crush_do_rule - calculate a mapping with the given input and rule
437 * @map: the crush_map
438 * @ruleno: the rule id
439 * @x: hash input
440 * @result: pointer to result vector
441 * @result_max: maximum result size
442 * @force: force initial replica choice; -1 for none
443 */
444int crush_do_rule(struct crush_map *map,
445 int ruleno, int x, int *result, int result_max,
446 int force, __u32 *weight)
447{
448 int result_len;
449 int force_context[CRUSH_MAX_DEPTH];
450 int force_pos = -1;
451 int a[CRUSH_MAX_SET];
452 int b[CRUSH_MAX_SET];
453 int c[CRUSH_MAX_SET];
454 int recurse_to_leaf;
455 int *w;
456 int wsize = 0;
457 int *o;
458 int osize;
459 int *tmp;
460 struct crush_rule *rule;
461 int step;
462 int i, j;
463 int numrep;
464 int firstn;
465 int rc = -1;
466
467 BUG_ON(ruleno >= map->max_rules);
468
469 rule = map->rules[ruleno];
470 result_len = 0;
471 w = a;
472 o = b;
473
474 /*
475 * determine hierarchical context of force, if any. note
476 * that this may or may not correspond to the specific types
477 * referenced by the crush rule.
478 */
479 if (force >= 0) {
480 if (force >= map->max_devices ||
481 map->device_parents[force] == 0) {
482 /*dprintk("CRUSH: forcefed device dne\n");*/
483 rc = -1; /* force fed device dne */
484 goto out;
485 }
486 if (!is_out(map, weight, force, x)) {
487 while (1) {
488 force_context[++force_pos] = force;
489 if (force >= 0)
490 force = map->device_parents[force];
491 else
492 force = map->bucket_parents[-1-force];
493 if (force == 0)
494 break;
495 }
496 }
497 }
498
499 for (step = 0; step < rule->len; step++) {
500 firstn = 0;
501 switch (rule->steps[step].op) {
502 case CRUSH_RULE_TAKE:
503 w[0] = rule->steps[step].arg1;
504 if (force_pos >= 0) {
505 BUG_ON(force_context[force_pos] != w[0]);
506 force_pos--;
507 }
508 wsize = 1;
509 break;
510
511 case CRUSH_RULE_CHOOSE_LEAF_FIRSTN:
512 case CRUSH_RULE_CHOOSE_FIRSTN:
513 firstn = 1;
514 case CRUSH_RULE_CHOOSE_LEAF_INDEP:
515 case CRUSH_RULE_CHOOSE_INDEP:
516 BUG_ON(wsize == 0);
517
518 recurse_to_leaf =
519 rule->steps[step].op ==
520 CRUSH_RULE_CHOOSE_LEAF_FIRSTN ||
521 rule->steps[step].op ==
522 CRUSH_RULE_CHOOSE_LEAF_INDEP;
523
524 /* reset output */
525 osize = 0;
526
527 for (i = 0; i < wsize; i++) {
528 /*
529 * see CRUSH_N, CRUSH_N_MINUS macros.
530 * basically, numrep <= 0 means relative to
531 * the provided result_max
532 */
533 numrep = rule->steps[step].arg1;
534 if (numrep <= 0) {
535 numrep += result_max;
536 if (numrep <= 0)
537 continue;
538 }
539 j = 0;
540 if (osize == 0 && force_pos >= 0) {
541 /* skip any intermediate types */
542 while (force_pos &&
543 force_context[force_pos] < 0 &&
544 rule->steps[step].arg2 !=
545 map->buckets[-1 -
546 force_context[force_pos]]->type)
547 force_pos--;
548 o[osize] = force_context[force_pos];
549 if (recurse_to_leaf)
550 c[osize] = force_context[0];
551 j++;
552 force_pos--;
553 }
554 osize += crush_choose(map,
555 map->buckets[-1-w[i]],
556 weight,
557 x, numrep,
558 rule->steps[step].arg2,
559 o+osize, j,
560 firstn,
561 recurse_to_leaf, c+osize);
562 }
563
564 if (recurse_to_leaf)
565 /* copy final _leaf_ values to output set */
566 memcpy(o, c, osize*sizeof(*o));
567
568 /* swap t and w arrays */
569 tmp = o;
570 o = w;
571 w = tmp;
572 wsize = osize;
573 break;
574
575
576 case CRUSH_RULE_EMIT:
577 for (i = 0; i < wsize && result_len < result_max; i++) {
578 result[result_len] = w[i];
579 result_len++;
580 }
581 wsize = 0;
582 break;
583
584 default:
585 BUG_ON(1);
586 }
587 }
588 rc = result_len;
589
590out:
591 return rc;
592}
593
594