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