crush: add SET_CHOOSE_TRIES rule step
[platform/kernel/linux-rpi.git] / net / ceph / crush / mapper.c
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
21 #include <linux/crush/crush.h>
22 #include <linux/crush/hash.h>
23 #include <linux/crush/mapper.h>
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  */
36 int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
37 {
38         __u32 i;
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 {
72         unsigned int pr = r % bucket->size;
73         unsigned int i, s;
74
75         /* start a new permutation if @x has changed */
76         if (bucket->perm_x != (__u32)x || bucket->perm_n == 0) {
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) {
82                         s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
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) {
104                 unsigned int p = bucket->perm_n;
105                 /* no point in swapping the final entry */
106                 if (p < bucket->size - 1) {
107                         i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
108                                 (bucket->size - p);
109                         if (i) {
110                                 unsigned int t = bucket->perm[p + i];
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--) {
142                 __u64 w = crush_hash32_4(bucket->h.hash,x, bucket->h.items[i],
143                                          r, bucket->h.id);
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
156         dprintk("bad list sums for bucket %d\n", bucket->h.id);
157         return bucket->h.items[0];
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 {
192         int n;
193         __u32 w;
194         __u64 t;
195
196         /* start at root */
197         n = bucket->num_nodes >> 1;
198
199         while (!terminal(n)) {
200                 int l;
201                 /* pick point in [0, w) */
202                 w = bucket->node_weights[n];
203                 t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
204                                           bucket->h.id) * (__u64)w;
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 {
224         __u32 i;
225         int high = 0;
226         __u64 high_draw = 0;
227         __u64 draw;
228
229         for (i = 0; i < bucket->h.size; i++) {
230                 draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
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 {
243         dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
244         BUG_ON(in->size == 0);
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:
259                 dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
260                 return in->items[0];
261         }
262 }
263
264 /*
265  * true if device is marked "out" (failed, fully offloaded)
266  * of the cluster
267  */
268 static int is_out(const struct crush_map *map,
269                   const __u32 *weight, int weight_max,
270                   int item, int x)
271 {
272         if (item >= weight_max)
273                 return 1;
274         if (weight[item] >= 0x10000)
275                 return 0;
276         if (weight[item] == 0)
277                 return 1;
278         if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
279             < weight[item])
280                 return 0;
281         return 1;
282 }
283
284 /**
285  * crush_choose_firstn - choose numrep distinct items of given type
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
293  * @recurse_to_leaf: true if we want one device under each item of given type
294  * @descend_once: true if we should only try one descent before giving up
295  * @out2: second output vector for leaf items (if @recurse_to_leaf)
296  */
297 static int crush_choose_firstn(const struct crush_map *map,
298                                struct crush_bucket *bucket,
299                                const __u32 *weight, int weight_max,
300                                int x, int numrep, int type,
301                                int *out, int outpos,
302                                unsigned int attempts,
303                                unsigned int recurse_attempts,
304                                int recurse_to_leaf,
305                                int descend_once, int *out2)
306 {
307         int rep;
308         unsigned int ftotal, flocal;
309         int retry_descent, retry_bucket, skip_rep;
310         struct crush_bucket *in = bucket;
311         int r;
312         int i;
313         int item = 0;
314         int itemtype;
315         int collide, reject;
316
317         dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
318                 bucket->id, x, outpos, numrep);
319
320         for (rep = outpos; rep < numrep; rep++) {
321                 /* keep trying until we get a non-out, non-colliding item */
322                 ftotal = 0;
323                 skip_rep = 0;
324                 do {
325                         retry_descent = 0;
326                         in = bucket;               /* initial bucket */
327
328                         /* choose through intervening buckets */
329                         flocal = 0;
330                         do {
331                                 collide = 0;
332                                 retry_bucket = 0;
333                                 r = rep;
334                                 /* r' = r + f_total */
335                                 r += ftotal;
336
337                                 /* bucket choose */
338                                 if (in->size == 0) {
339                                         reject = 1;
340                                         goto reject;
341                                 }
342                                 if (map->choose_local_fallback_tries > 0 &&
343                                     flocal >= (in->size>>1) &&
344                                     flocal > map->choose_local_fallback_tries)
345                                         item = bucket_perm_choose(in, x, r);
346                                 else
347                                         item = crush_bucket_choose(in, x, r);
348                                 if (item >= map->max_devices) {
349                                         dprintk("   bad item %d\n", item);
350                                         skip_rep = 1;
351                                         break;
352                                 }
353
354                                 /* desired type? */
355                                 if (item < 0)
356                                         itemtype = map->buckets[-1-item]->type;
357                                 else
358                                         itemtype = 0;
359                                 dprintk("  item %d type %d\n", item, itemtype);
360
361                                 /* keep going? */
362                                 if (itemtype != type) {
363                                         if (item >= 0 ||
364                                             (-1-item) >= map->max_buckets) {
365                                                 dprintk("   bad item type %d\n", type);
366                                                 skip_rep = 1;
367                                                 break;
368                                         }
369                                         in = map->buckets[-1-item];
370                                         retry_bucket = 1;
371                                         continue;
372                                 }
373
374                                 /* collision? */
375                                 for (i = 0; i < outpos; i++) {
376                                         if (out[i] == item) {
377                                                 collide = 1;
378                                                 break;
379                                         }
380                                 }
381
382                                 reject = 0;
383                                 if (!collide && recurse_to_leaf) {
384                                         if (item < 0) {
385                                                 if (crush_choose_firstn(map,
386                                                          map->buckets[-1-item],
387                                                          weight, weight_max,
388                                                          x, outpos+1, 0,
389                                                          out2, outpos,
390                                                          recurse_attempts, 0,
391                                                          0,
392                                                          map->chooseleaf_descend_once,
393                                                          NULL) <= outpos)
394                                                         /* didn't get leaf */
395                                                         reject = 1;
396                                         } else {
397                                                 /* we already have a leaf! */
398                                                 out2[outpos] = item;
399                                         }
400                                 }
401
402                                 if (!reject) {
403                                         /* out? */
404                                         if (itemtype == 0)
405                                                 reject = is_out(map, weight,
406                                                                 weight_max,
407                                                                 item, x);
408                                         else
409                                                 reject = 0;
410                                 }
411
412 reject:
413                                 if (reject || collide) {
414                                         ftotal++;
415                                         flocal++;
416
417                                         if (reject && descend_once)
418                                                 /* let outer call try again */
419                                                 skip_rep = 1;
420                                         else if (collide && flocal <= map->choose_local_tries)
421                                                 /* retry locally a few times */
422                                                 retry_bucket = 1;
423                                         else if (map->choose_local_fallback_tries > 0 &&
424                                                  flocal <= in->size + map->choose_local_fallback_tries)
425                                                 /* exhaustive bucket search */
426                                                 retry_bucket = 1;
427                                         else if (ftotal <= attempts)
428                                                 /* then retry descent */
429                                                 retry_descent = 1;
430                                         else
431                                                 /* else give up */
432                                                 skip_rep = 1;
433                                         dprintk("  reject %d  collide %d  "
434                                                 "ftotal %u  flocal %u\n",
435                                                 reject, collide, ftotal,
436                                                 flocal);
437                                 }
438                         } while (retry_bucket);
439                 } while (retry_descent);
440
441                 if (skip_rep) {
442                         dprintk("skip rep\n");
443                         continue;
444                 }
445
446                 dprintk("CHOOSE got %d\n", item);
447                 out[outpos] = item;
448                 outpos++;
449         }
450
451         dprintk("CHOOSE returns %d\n", outpos);
452         return outpos;
453 }
454
455
456 /**
457  * crush_choose_indep: alternative breadth-first positionally stable mapping
458  *
459  */
460 static void crush_choose_indep(const struct crush_map *map,
461                                struct crush_bucket *bucket,
462                                const __u32 *weight, int weight_max,
463                                int x, int left, int numrep, int type,
464                                int *out, int outpos,
465                                unsigned int attempts,
466                                unsigned int recurse_attempts,
467                                int recurse_to_leaf,
468                                int *out2,
469                                int parent_r)
470 {
471         struct crush_bucket *in = bucket;
472         int endpos = outpos + left;
473         int rep;
474         unsigned int ftotal;
475         int r;
476         int i;
477         int item = 0;
478         int itemtype;
479         int collide;
480
481         dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
482                 bucket->id, x, outpos, numrep);
483
484         /* initially my result is undefined */
485         for (rep = outpos; rep < endpos; rep++) {
486                 out[rep] = CRUSH_ITEM_UNDEF;
487                 if (out2)
488                         out2[rep] = CRUSH_ITEM_UNDEF;
489         }
490
491         for (ftotal = 0; left > 0 && ftotal < attempts; ftotal++) {
492                 for (rep = outpos; rep < endpos; rep++) {
493                         if (out[rep] != CRUSH_ITEM_UNDEF)
494                                 continue;
495
496                         in = bucket;  /* initial bucket */
497
498                         /* choose through intervening buckets */
499                         for (;;) {
500                                 /* note: we base the choice on the position
501                                  * even in the nested call.  that means that
502                                  * if the first layer chooses the same bucket
503                                  * in a different position, we will tend to
504                                  * choose a different item in that bucket.
505                                  * this will involve more devices in data
506                                  * movement and tend to distribute the load.
507                                  */
508                                 r = rep + parent_r;
509
510                                 /* be careful */
511                                 if (in->alg == CRUSH_BUCKET_UNIFORM &&
512                                     in->size % numrep == 0)
513                                         /* r'=r+(n+1)*f_total */
514                                         r += (numrep+1) * ftotal;
515                                 else
516                                         /* r' = r + n*f_total */
517                                         r += numrep * ftotal;
518
519                                 /* bucket choose */
520                                 if (in->size == 0) {
521                                         dprintk("   empty bucket\n");
522                                         break;
523                                 }
524
525                                 item = crush_bucket_choose(in, x, r);
526                                 if (item >= map->max_devices) {
527                                         dprintk("   bad item %d\n", item);
528                                         out[rep] = CRUSH_ITEM_NONE;
529                                         if (out2)
530                                                 out2[rep] = CRUSH_ITEM_NONE;
531                                         left--;
532                                         break;
533                                 }
534
535                                 /* desired type? */
536                                 if (item < 0)
537                                         itemtype = map->buckets[-1-item]->type;
538                                 else
539                                         itemtype = 0;
540                                 dprintk("  item %d type %d\n", item, itemtype);
541
542                                 /* keep going? */
543                                 if (itemtype != type) {
544                                         if (item >= 0 ||
545                                             (-1-item) >= map->max_buckets) {
546                                                 dprintk("   bad item type %d\n", type);
547                                                 out[rep] = CRUSH_ITEM_NONE;
548                                                 if (out2)
549                                                         out2[rep] =
550                                                                 CRUSH_ITEM_NONE;
551                                                 left--;
552                                                 break;
553                                         }
554                                         in = map->buckets[-1-item];
555                                         continue;
556                                 }
557
558                                 /* collision? */
559                                 collide = 0;
560                                 for (i = outpos; i < endpos; i++) {
561                                         if (out[i] == item) {
562                                                 collide = 1;
563                                                 break;
564                                         }
565                                 }
566                                 if (collide)
567                                         break;
568
569                                 if (recurse_to_leaf) {
570                                         if (item < 0) {
571                                                 crush_choose_indep(map,
572                                                    map->buckets[-1-item],
573                                                    weight, weight_max,
574                                                    x, 1, numrep, 0,
575                                                    out2, rep,
576                                                    recurse_attempts, 0,
577                                                    0, NULL, r);
578                                                 if (out2[rep] == CRUSH_ITEM_NONE) {
579                                                         /* placed nothing; no leaf */
580                                                         break;
581                                                 }
582                                         } else {
583                                                 /* we already have a leaf! */
584                                                 out2[rep] = item;
585                                         }
586                                 }
587
588                                 /* out? */
589                                 if (itemtype == 0 &&
590                                     is_out(map, weight, weight_max, item, x))
591                                         break;
592
593                                 /* yay! */
594                                 out[rep] = item;
595                                 left--;
596                                 break;
597                         }
598                 }
599         }
600         for (rep = outpos; rep < endpos; rep++) {
601                 if (out[rep] == CRUSH_ITEM_UNDEF) {
602                         out[rep] = CRUSH_ITEM_NONE;
603                 }
604                 if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) {
605                         out2[rep] = CRUSH_ITEM_NONE;
606                 }
607         }
608 }
609
610 /**
611  * crush_do_rule - calculate a mapping with the given input and rule
612  * @map: the crush_map
613  * @ruleno: the rule id
614  * @x: hash input
615  * @result: pointer to result vector
616  * @result_max: maximum result size
617  * @weight: weight vector (for map leaves)
618  * @weight_max: size of weight vector
619  * @scratch: scratch vector for private use; must be >= 3 * result_max
620  */
621 int crush_do_rule(const struct crush_map *map,
622                   int ruleno, int x, int *result, int result_max,
623                   const __u32 *weight, int weight_max,
624                   int *scratch)
625 {
626         int result_len;
627         int *a = scratch;
628         int *b = scratch + result_max;
629         int *c = scratch + result_max*2;
630         int recurse_to_leaf;
631         int *w;
632         int wsize = 0;
633         int *o;
634         int osize;
635         int *tmp;
636         struct crush_rule *rule;
637         __u32 step;
638         int i, j;
639         int numrep;
640         int choose_tries = map->choose_total_tries;
641         int choose_leaf_tries = 0;
642         const int descend_once = 0;
643
644         if ((__u32)ruleno >= map->max_rules) {
645                 dprintk(" bad ruleno %d\n", ruleno);
646                 return 0;
647         }
648
649         rule = map->rules[ruleno];
650         result_len = 0;
651         w = a;
652         o = b;
653
654         for (step = 0; step < rule->len; step++) {
655                 int firstn = 0;
656                 struct crush_rule_step *curstep = &rule->steps[step];
657
658                 switch (curstep->op) {
659                 case CRUSH_RULE_TAKE:
660                         w[0] = curstep->arg1;
661                         wsize = 1;
662                         break;
663
664                 case CRUSH_RULE_SET_CHOOSE_TRIES:
665                         if (curstep->arg1 > 0)
666                                 choose_tries = curstep->arg1;
667                         break;
668
669                 case CRUSH_RULE_SET_CHOOSE_LEAF_TRIES:
670                         if (curstep->arg1 > 0)
671                                 choose_leaf_tries = curstep->arg1;
672                         break;
673
674                 case CRUSH_RULE_CHOOSE_LEAF_FIRSTN:
675                 case CRUSH_RULE_CHOOSE_FIRSTN:
676                         firstn = 1;
677                         /* fall through */
678                 case CRUSH_RULE_CHOOSE_LEAF_INDEP:
679                 case CRUSH_RULE_CHOOSE_INDEP:
680                         if (wsize == 0)
681                                 break;
682
683                         recurse_to_leaf =
684                                 curstep->op ==
685                                  CRUSH_RULE_CHOOSE_LEAF_FIRSTN ||
686                                 curstep->op ==
687                                 CRUSH_RULE_CHOOSE_LEAF_INDEP;
688
689                         /* reset output */
690                         osize = 0;
691
692                         for (i = 0; i < wsize; i++) {
693                                 /*
694                                  * see CRUSH_N, CRUSH_N_MINUS macros.
695                                  * basically, numrep <= 0 means relative to
696                                  * the provided result_max
697                                  */
698                                 numrep = curstep->arg1;
699                                 if (numrep <= 0) {
700                                         numrep += result_max;
701                                         if (numrep <= 0)
702                                                 continue;
703                                 }
704                                 j = 0;
705                                 if (firstn) {
706                                         osize += crush_choose_firstn(
707                                                 map,
708                                                 map->buckets[-1-w[i]],
709                                                 weight, weight_max,
710                                                 x, numrep,
711                                                 curstep->arg2,
712                                                 o+osize, j,
713                                                 choose_tries,
714                                                 choose_leaf_tries ? choose_leaf_tries : choose_tries,
715                                                 recurse_to_leaf,
716                                                 descend_once, c+osize);
717                                 } else {
718                                         crush_choose_indep(
719                                                 map,
720                                                 map->buckets[-1-w[i]],
721                                                 weight, weight_max,
722                                                 x, numrep, numrep,
723                                                 curstep->arg2,
724                                                 o+osize, j,
725                                                 choose_tries,
726                                                 choose_leaf_tries ? choose_leaf_tries : 1,
727                                                 recurse_to_leaf,
728                                                 c+osize,
729                                                 0);
730                                         osize += numrep;
731                                 }
732                         }
733
734                         if (recurse_to_leaf)
735                                 /* copy final _leaf_ values to output set */
736                                 memcpy(o, c, osize*sizeof(*o));
737
738                         /* swap o and w arrays */
739                         tmp = o;
740                         o = w;
741                         w = tmp;
742                         wsize = osize;
743                         break;
744
745
746                 case CRUSH_RULE_EMIT:
747                         for (i = 0; i < wsize && result_len < result_max; i++) {
748                                 result[result_len] = w[i];
749                                 result_len++;
750                         }
751                         wsize = 0;
752                         break;
753
754                 default:
755                         dprintk(" unknown op %d at step %d\n",
756                                 curstep->op, step);
757                         break;
758                 }
759         }
760         return result_len;
761 }
762
763