crush: drop unnecessary include from mapper.c
[linux-2.6-microblaze.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
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  */
35 int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
36 {
37         __u32 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  */
68 static int bucket_perm_choose(struct crush_bucket *bucket,
69                               int x, int r)
70 {
71         unsigned int pr = r % bucket->size;
72         unsigned int i, s;
73
74         /* start a new permutation if @x has changed */
75         if (bucket->perm_x != (__u32)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(bucket->hash, 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 int p = bucket->perm_n;
104                 /* no point in swapping the final entry */
105                 if (p < bucket->size - 1) {
106                         i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
107                                 (bucket->size - p);
108                         if (i) {
109                                 unsigned int 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];
121 out:
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 */
128 static 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 */
135 static 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(bucket->h.hash,x, bucket->h.items[i],
142                                          r, 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         dprintk("bad list sums for bucket %d\n", bucket->h.id);
156         return bucket->h.items[0];
157 }
158
159
160 /* (binary) tree */
161 static 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
171 static int left(int x)
172 {
173         int h = height(x);
174         return x - (1 << (h-1));
175 }
176
177 static int right(int x)
178 {
179         int h = height(x);
180         return x + (1 << (h-1));
181 }
182
183 static int terminal(int x)
184 {
185         return x & 1;
186 }
187
188 static int bucket_tree_choose(struct crush_bucket_tree *bucket,
189                               int x, int r)
190 {
191         int n;
192         __u32 w;
193         __u64 t;
194
195         /* start at root */
196         n = bucket->num_nodes >> 1;
197
198         while (!terminal(n)) {
199                 int l;
200                 /* pick point in [0, w) */
201                 w = bucket->node_weights[n];
202                 t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
203                                           bucket->h.id) * (__u64)w;
204                 t = t >> 32;
205
206                 /* descend to the left or right? */
207                 l = left(n);
208                 if (t < bucket->node_weights[l])
209                         n = l;
210                 else
211                         n = right(n);
212         }
213
214         return bucket->h.items[n >> 1];
215 }
216
217
218 /* straw */
219
220 static int bucket_straw_choose(struct crush_bucket_straw *bucket,
221                                int x, int r)
222 {
223         __u32 i;
224         int high = 0;
225         __u64 high_draw = 0;
226         __u64 draw;
227
228         for (i = 0; i < bucket->h.size; i++) {
229                 draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
230                 draw &= 0xffff;
231                 draw *= bucket->straws[i];
232                 if (i == 0 || draw > high_draw) {
233                         high = i;
234                         high_draw = draw;
235                 }
236         }
237         return bucket->h.items[high];
238 }
239
240 static int crush_bucket_choose(struct crush_bucket *in, int x, int r)
241 {
242         dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
243         BUG_ON(in->size == 0);
244         switch (in->alg) {
245         case CRUSH_BUCKET_UNIFORM:
246                 return bucket_uniform_choose((struct crush_bucket_uniform *)in,
247                                           x, r);
248         case CRUSH_BUCKET_LIST:
249                 return bucket_list_choose((struct crush_bucket_list *)in,
250                                           x, r);
251         case CRUSH_BUCKET_TREE:
252                 return bucket_tree_choose((struct crush_bucket_tree *)in,
253                                           x, r);
254         case CRUSH_BUCKET_STRAW:
255                 return bucket_straw_choose((struct crush_bucket_straw *)in,
256                                            x, r);
257         default:
258                 dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
259                 return in->items[0];
260         }
261 }
262
263 /*
264  * true if device is marked "out" (failed, fully offloaded)
265  * of the cluster
266  */
267 static int is_out(const struct crush_map *map,
268                   const __u32 *weight, int weight_max,
269                   int item, int x)
270 {
271         if (item >= weight_max)
272                 return 1;
273         if (weight[item] >= 0x10000)
274                 return 0;
275         if (weight[item] == 0)
276                 return 1;
277         if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
278             < weight[item])
279                 return 0;
280         return 1;
281 }
282
283 /**
284  * crush_choose_firstn - choose numrep distinct items of given type
285  * @map: the crush_map
286  * @bucket: the bucket we are choose an item from
287  * @x: crush input value
288  * @numrep: the number of items to choose
289  * @type: the type of item to choose
290  * @out: pointer to output vector
291  * @outpos: our position in that vector
292  * @tries: number of attempts to make
293  * @recurse_tries: number of attempts to have recursive chooseleaf make
294  * @local_retries: localized retries
295  * @local_fallback_retries: localized fallback retries
296  * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose)
297  * @vary_r: pass r to recursive calls
298  * @out2: second output vector for leaf items (if @recurse_to_leaf)
299  * @parent_r: r value passed from the parent
300  */
301 static int crush_choose_firstn(const struct crush_map *map,
302                                struct crush_bucket *bucket,
303                                const __u32 *weight, int weight_max,
304                                int x, int numrep, int type,
305                                int *out, int outpos,
306                                unsigned int tries,
307                                unsigned int recurse_tries,
308                                unsigned int local_retries,
309                                unsigned int local_fallback_retries,
310                                int recurse_to_leaf,
311                                unsigned int vary_r,
312                                int *out2,
313                                int parent_r)
314 {
315         int rep;
316         unsigned int ftotal, flocal;
317         int retry_descent, retry_bucket, skip_rep;
318         struct crush_bucket *in = bucket;
319         int r;
320         int i;
321         int item = 0;
322         int itemtype;
323         int collide, reject;
324
325         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\n",
326                 recurse_to_leaf ? "_LEAF" : "",
327                 bucket->id, x, outpos, numrep,
328                 tries, recurse_tries, local_retries, local_fallback_retries,
329                 parent_r);
330
331         for (rep = outpos; rep < numrep; rep++) {
332                 /* keep trying until we get a non-out, non-colliding item */
333                 ftotal = 0;
334                 skip_rep = 0;
335                 do {
336                         retry_descent = 0;
337                         in = bucket;               /* initial bucket */
338
339                         /* choose through intervening buckets */
340                         flocal = 0;
341                         do {
342                                 collide = 0;
343                                 retry_bucket = 0;
344                                 r = rep + parent_r;
345                                 /* r' = r + f_total */
346                                 r += ftotal;
347
348                                 /* bucket choose */
349                                 if (in->size == 0) {
350                                         reject = 1;
351                                         goto reject;
352                                 }
353                                 if (local_fallback_retries > 0 &&
354                                     flocal >= (in->size>>1) &&
355                                     flocal > local_fallback_retries)
356                                         item = bucket_perm_choose(in, x, r);
357                                 else
358                                         item = crush_bucket_choose(in, x, r);
359                                 if (item >= map->max_devices) {
360                                         dprintk("   bad item %d\n", item);
361                                         skip_rep = 1;
362                                         break;
363                                 }
364
365                                 /* desired type? */
366                                 if (item < 0)
367                                         itemtype = map->buckets[-1-item]->type;
368                                 else
369                                         itemtype = 0;
370                                 dprintk("  item %d type %d\n", item, itemtype);
371
372                                 /* keep going? */
373                                 if (itemtype != type) {
374                                         if (item >= 0 ||
375                                             (-1-item) >= map->max_buckets) {
376                                                 dprintk("   bad item type %d\n", type);
377                                                 skip_rep = 1;
378                                                 break;
379                                         }
380                                         in = map->buckets[-1-item];
381                                         retry_bucket = 1;
382                                         continue;
383                                 }
384
385                                 /* collision? */
386                                 for (i = 0; i < outpos; i++) {
387                                         if (out[i] == item) {
388                                                 collide = 1;
389                                                 break;
390                                         }
391                                 }
392
393                                 reject = 0;
394                                 if (!collide && recurse_to_leaf) {
395                                         if (item < 0) {
396                                                 int sub_r;
397                                                 if (vary_r)
398                                                         sub_r = r >> (vary_r-1);
399                                                 else
400                                                         sub_r = 0;
401                                                 if (crush_choose_firstn(map,
402                                                          map->buckets[-1-item],
403                                                          weight, weight_max,
404                                                          x, outpos+1, 0,
405                                                          out2, outpos,
406                                                          recurse_tries, 0,
407                                                          local_retries,
408                                                          local_fallback_retries,
409                                                          0,
410                                                          vary_r,
411                                                          NULL,
412                                                          sub_r) <= outpos)
413                                                         /* didn't get leaf */
414                                                         reject = 1;
415                                         } else {
416                                                 /* we already have a leaf! */
417                                                 out2[outpos] = item;
418                                         }
419                                 }
420
421                                 if (!reject) {
422                                         /* out? */
423                                         if (itemtype == 0)
424                                                 reject = is_out(map, weight,
425                                                                 weight_max,
426                                                                 item, x);
427                                         else
428                                                 reject = 0;
429                                 }
430
431 reject:
432                                 if (reject || collide) {
433                                         ftotal++;
434                                         flocal++;
435
436                                         if (collide && flocal <= local_retries)
437                                                 /* retry locally a few times */
438                                                 retry_bucket = 1;
439                                         else if (local_fallback_retries > 0 &&
440                                                  flocal <= in->size + local_fallback_retries)
441                                                 /* exhaustive bucket search */
442                                                 retry_bucket = 1;
443                                         else if (ftotal < tries)
444                                                 /* then retry descent */
445                                                 retry_descent = 1;
446                                         else
447                                                 /* else give up */
448                                                 skip_rep = 1;
449                                         dprintk("  reject %d  collide %d  "
450                                                 "ftotal %u  flocal %u\n",
451                                                 reject, collide, ftotal,
452                                                 flocal);
453                                 }
454                         } while (retry_bucket);
455                 } while (retry_descent);
456
457                 if (skip_rep) {
458                         dprintk("skip rep\n");
459                         continue;
460                 }
461
462                 dprintk("CHOOSE got %d\n", item);
463                 out[outpos] = item;
464                 outpos++;
465         }
466
467         dprintk("CHOOSE returns %d\n", outpos);
468         return outpos;
469 }
470
471
472 /**
473  * crush_choose_indep: alternative breadth-first positionally stable mapping
474  *
475  */
476 static void crush_choose_indep(const struct crush_map *map,
477                                struct crush_bucket *bucket,
478                                const __u32 *weight, int weight_max,
479                                int x, int left, int numrep, int type,
480                                int *out, int outpos,
481                                unsigned int tries,
482                                unsigned int recurse_tries,
483                                int recurse_to_leaf,
484                                int *out2,
485                                int parent_r)
486 {
487         struct crush_bucket *in = bucket;
488         int endpos = outpos + left;
489         int rep;
490         unsigned int ftotal;
491         int r;
492         int i;
493         int item = 0;
494         int itemtype;
495         int collide;
496
497         dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
498                 bucket->id, x, outpos, numrep);
499
500         /* initially my result is undefined */
501         for (rep = outpos; rep < endpos; rep++) {
502                 out[rep] = CRUSH_ITEM_UNDEF;
503                 if (out2)
504                         out2[rep] = CRUSH_ITEM_UNDEF;
505         }
506
507         for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) {
508                 for (rep = outpos; rep < endpos; rep++) {
509                         if (out[rep] != CRUSH_ITEM_UNDEF)
510                                 continue;
511
512                         in = bucket;  /* initial bucket */
513
514                         /* choose through intervening buckets */
515                         for (;;) {
516                                 /* note: we base the choice on the position
517                                  * even in the nested call.  that means that
518                                  * if the first layer chooses the same bucket
519                                  * in a different position, we will tend to
520                                  * choose a different item in that bucket.
521                                  * this will involve more devices in data
522                                  * movement and tend to distribute the load.
523                                  */
524                                 r = rep + parent_r;
525
526                                 /* be careful */
527                                 if (in->alg == CRUSH_BUCKET_UNIFORM &&
528                                     in->size % numrep == 0)
529                                         /* r'=r+(n+1)*f_total */
530                                         r += (numrep+1) * ftotal;
531                                 else
532                                         /* r' = r + n*f_total */
533                                         r += numrep * ftotal;
534
535                                 /* bucket choose */
536                                 if (in->size == 0) {
537                                         dprintk("   empty bucket\n");
538                                         break;
539                                 }
540
541                                 item = crush_bucket_choose(in, x, r);
542                                 if (item >= map->max_devices) {
543                                         dprintk("   bad item %d\n", item);
544                                         out[rep] = CRUSH_ITEM_NONE;
545                                         if (out2)
546                                                 out2[rep] = CRUSH_ITEM_NONE;
547                                         left--;
548                                         break;
549                                 }
550
551                                 /* desired type? */
552                                 if (item < 0)
553                                         itemtype = map->buckets[-1-item]->type;
554                                 else
555                                         itemtype = 0;
556                                 dprintk("  item %d type %d\n", item, itemtype);
557
558                                 /* keep going? */
559                                 if (itemtype != type) {
560                                         if (item >= 0 ||
561                                             (-1-item) >= map->max_buckets) {
562                                                 dprintk("   bad item type %d\n", type);
563                                                 out[rep] = CRUSH_ITEM_NONE;
564                                                 if (out2)
565                                                         out2[rep] =
566                                                                 CRUSH_ITEM_NONE;
567                                                 left--;
568                                                 break;
569                                         }
570                                         in = map->buckets[-1-item];
571                                         continue;
572                                 }
573
574                                 /* collision? */
575                                 collide = 0;
576                                 for (i = outpos; i < endpos; i++) {
577                                         if (out[i] == item) {
578                                                 collide = 1;
579                                                 break;
580                                         }
581                                 }
582                                 if (collide)
583                                         break;
584
585                                 if (recurse_to_leaf) {
586                                         if (item < 0) {
587                                                 crush_choose_indep(map,
588                                                    map->buckets[-1-item],
589                                                    weight, weight_max,
590                                                    x, 1, numrep, 0,
591                                                    out2, rep,
592                                                    recurse_tries, 0,
593                                                    0, NULL, r);
594                                                 if (out2[rep] == CRUSH_ITEM_NONE) {
595                                                         /* placed nothing; no leaf */
596                                                         break;
597                                                 }
598                                         } else {
599                                                 /* we already have a leaf! */
600                                                 out2[rep] = item;
601                                         }
602                                 }
603
604                                 /* out? */
605                                 if (itemtype == 0 &&
606                                     is_out(map, weight, weight_max, item, x))
607                                         break;
608
609                                 /* yay! */
610                                 out[rep] = item;
611                                 left--;
612                                 break;
613                         }
614                 }
615         }
616         for (rep = outpos; rep < endpos; rep++) {
617                 if (out[rep] == CRUSH_ITEM_UNDEF) {
618                         out[rep] = CRUSH_ITEM_NONE;
619                 }
620                 if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) {
621                         out2[rep] = CRUSH_ITEM_NONE;
622                 }
623         }
624 }
625
626 /**
627  * crush_do_rule - calculate a mapping with the given input and rule
628  * @map: the crush_map
629  * @ruleno: the rule id
630  * @x: hash input
631  * @result: pointer to result vector
632  * @result_max: maximum result size
633  * @weight: weight vector (for map leaves)
634  * @weight_max: size of weight vector
635  * @scratch: scratch vector for private use; must be >= 3 * result_max
636  */
637 int crush_do_rule(const struct crush_map *map,
638                   int ruleno, int x, int *result, int result_max,
639                   const __u32 *weight, int weight_max,
640                   int *scratch)
641 {
642         int result_len;
643         int *a = scratch;
644         int *b = scratch + result_max;
645         int *c = scratch + result_max*2;
646         int recurse_to_leaf;
647         int *w;
648         int wsize = 0;
649         int *o;
650         int osize;
651         int *tmp;
652         struct crush_rule *rule;
653         __u32 step;
654         int i, j;
655         int numrep;
656         /*
657          * the original choose_total_tries value was off by one (it
658          * counted "retries" and not "tries").  add one.
659          */
660         int choose_tries = map->choose_total_tries + 1;
661         int choose_leaf_tries = 0;
662         /*
663          * the local tries values were counted as "retries", though,
664          * and need no adjustment
665          */
666         int choose_local_retries = map->choose_local_tries;
667         int choose_local_fallback_retries = map->choose_local_fallback_tries;
668
669         int vary_r = map->chooseleaf_vary_r;
670
671         if ((__u32)ruleno >= map->max_rules) {
672                 dprintk(" bad ruleno %d\n", ruleno);
673                 return 0;
674         }
675
676         rule = map->rules[ruleno];
677         result_len = 0;
678         w = a;
679         o = b;
680
681         for (step = 0; step < rule->len; step++) {
682                 int firstn = 0;
683                 struct crush_rule_step *curstep = &rule->steps[step];
684
685                 switch (curstep->op) {
686                 case CRUSH_RULE_TAKE:
687                         w[0] = curstep->arg1;
688                         wsize = 1;
689                         break;
690
691                 case CRUSH_RULE_SET_CHOOSE_TRIES:
692                         if (curstep->arg1 > 0)
693                                 choose_tries = curstep->arg1;
694                         break;
695
696                 case CRUSH_RULE_SET_CHOOSELEAF_TRIES:
697                         if (curstep->arg1 > 0)
698                                 choose_leaf_tries = curstep->arg1;
699                         break;
700
701                 case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES:
702                         if (curstep->arg1 >= 0)
703                                 choose_local_retries = curstep->arg1;
704                         break;
705
706                 case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES:
707                         if (curstep->arg1 >= 0)
708                                 choose_local_fallback_retries = curstep->arg1;
709                         break;
710
711                 case CRUSH_RULE_SET_CHOOSELEAF_VARY_R:
712                         if (curstep->arg1 >= 0)
713                                 vary_r = curstep->arg1;
714                         break;
715
716                 case CRUSH_RULE_CHOOSELEAF_FIRSTN:
717                 case CRUSH_RULE_CHOOSE_FIRSTN:
718                         firstn = 1;
719                         /* fall through */
720                 case CRUSH_RULE_CHOOSELEAF_INDEP:
721                 case CRUSH_RULE_CHOOSE_INDEP:
722                         if (wsize == 0)
723                                 break;
724
725                         recurse_to_leaf =
726                                 curstep->op ==
727                                  CRUSH_RULE_CHOOSELEAF_FIRSTN ||
728                                 curstep->op ==
729                                 CRUSH_RULE_CHOOSELEAF_INDEP;
730
731                         /* reset output */
732                         osize = 0;
733
734                         for (i = 0; i < wsize; i++) {
735                                 /*
736                                  * see CRUSH_N, CRUSH_N_MINUS macros.
737                                  * basically, numrep <= 0 means relative to
738                                  * the provided result_max
739                                  */
740                                 numrep = curstep->arg1;
741                                 if (numrep <= 0) {
742                                         numrep += result_max;
743                                         if (numrep <= 0)
744                                                 continue;
745                                 }
746                                 j = 0;
747                                 if (firstn) {
748                                         int recurse_tries;
749                                         if (choose_leaf_tries)
750                                                 recurse_tries =
751                                                         choose_leaf_tries;
752                                         else if (map->chooseleaf_descend_once)
753                                                 recurse_tries = 1;
754                                         else
755                                                 recurse_tries = choose_tries;
756                                         osize += crush_choose_firstn(
757                                                 map,
758                                                 map->buckets[-1-w[i]],
759                                                 weight, weight_max,
760                                                 x, numrep,
761                                                 curstep->arg2,
762                                                 o+osize, j,
763                                                 choose_tries,
764                                                 recurse_tries,
765                                                 choose_local_retries,
766                                                 choose_local_fallback_retries,
767                                                 recurse_to_leaf,
768                                                 vary_r,
769                                                 c+osize,
770                                                 0);
771                                 } else {
772                                         crush_choose_indep(
773                                                 map,
774                                                 map->buckets[-1-w[i]],
775                                                 weight, weight_max,
776                                                 x, numrep, numrep,
777                                                 curstep->arg2,
778                                                 o+osize, j,
779                                                 choose_tries,
780                                                 choose_leaf_tries ?
781                                                    choose_leaf_tries : 1,
782                                                 recurse_to_leaf,
783                                                 c+osize,
784                                                 0);
785                                         osize += numrep;
786                                 }
787                         }
788
789                         if (recurse_to_leaf)
790                                 /* copy final _leaf_ values to output set */
791                                 memcpy(o, c, osize*sizeof(*o));
792
793                         /* swap o and w arrays */
794                         tmp = o;
795                         o = w;
796                         w = tmp;
797                         wsize = osize;
798                         break;
799
800
801                 case CRUSH_RULE_EMIT:
802                         for (i = 0; i < wsize && result_len < result_max; i++) {
803                                 result[result_len] = w[i];
804                                 result_len++;
805                         }
806                         wsize = 0;
807                         break;
808
809                 default:
810                         dprintk(" unknown op %d at step %d\n",
811                                 curstep->op, step);
812                         break;
813                 }
814         }
815         return result_len;
816 }
817
818