blob: 18d2cf66f1020694b54bcf3581c4c7a6c6db7d24 [file] [log] [blame]
Sage Weil5ecc0a02009-10-06 11:31:11 -07001
2#ifdef __KERNEL__
3# include <linux/string.h>
4# include <linux/slab.h>
5# include <linux/bug.h>
6# include <linux/kernel.h>
7# ifndef dprintk
8# define dprintk(args...)
9# endif
10#else
11# include <string.h>
12# include <stdio.h>
13# include <stdlib.h>
14# include <assert.h>
15# define BUG_ON(x) assert(!(x))
16# define dprintk(args...) /* printf(args) */
17# define kmalloc(x, f) malloc(x)
18# define kfree(x) free(x)
19#endif
20
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070021#include <linux/crush/crush.h>
22#include <linux/crush/hash.h>
hartleysfeb50ac2012-04-24 14:38:37 +000023#include <linux/crush/mapper.h>
Sage Weil5ecc0a02009-10-06 11:31:11 -070024
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 */
Sage Weil8b12d472012-05-07 15:38:35 -070036int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
Sage Weil5ecc0a02009-10-06 11:31:11 -070037{
Sage Weil8b12d472012-05-07 15:38:35 -070038 __u32 i;
Sage Weil5ecc0a02009-10-06 11:31:11 -070039
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 */
69static int bucket_perm_choose(struct crush_bucket *bucket,
70 int x, int r)
71{
Eric Dumazet95c96172012-04-15 05:58:06 +000072 unsigned int pr = r % bucket->size;
73 unsigned int i, s;
Sage Weil5ecc0a02009-10-06 11:31:11 -070074
75 /* start a new permutation if @x has changed */
Sage Weil8b12d472012-05-07 15:38:35 -070076 if (bucket->perm_x != (__u32)x || bucket->perm_n == 0) {
Sage Weil5ecc0a02009-10-06 11:31:11 -070077 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) {
Sage Weilfb690392009-11-07 20:18:22 -080082 s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
Sage Weil5ecc0a02009-10-06 11:31:11 -070083 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) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000104 unsigned int p = bucket->perm_n;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700105 /* no point in swapping the final entry */
106 if (p < bucket->size - 1) {
Sage Weilfb690392009-11-07 20:18:22 -0800107 i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
Sage Weil5ecc0a02009-10-06 11:31:11 -0700108 (bucket->size - p);
109 if (i) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000110 unsigned int t = bucket->perm[p + i];
Sage Weil5ecc0a02009-10-06 11:31:11 -0700111 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];
122out:
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 */
129static 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 */
136static 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--) {
Sage Weilfb690392009-11-07 20:18:22 -0800142 __u64 w = crush_hash32_4(bucket->h.hash,x, bucket->h.items[i],
143 r, bucket->h.id);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700144 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
Sage Weila1f48952012-05-07 15:35:24 -0700156 dprintk("bad list sums for bucket %d\n", bucket->h.id);
157 return bucket->h.items[0];
Sage Weil5ecc0a02009-10-06 11:31:11 -0700158}
159
160
161/* (binary) tree */
162static 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
172static int left(int x)
173{
174 int h = height(x);
175 return x - (1 << (h-1));
176}
177
178static int right(int x)
179{
180 int h = height(x);
181 return x + (1 << (h-1));
182}
183
184static int terminal(int x)
185{
186 return x & 1;
187}
188
189static int bucket_tree_choose(struct crush_bucket_tree *bucket,
190 int x, int r)
191{
192 int n, l;
193 __u32 w;
194 __u64 t;
195
196 /* start at root */
197 n = bucket->num_nodes >> 1;
198
199 while (!terminal(n)) {
200 /* pick point in [0, w) */
201 w = bucket->node_weights[n];
Sage Weilfb690392009-11-07 20:18:22 -0800202 t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
203 bucket->h.id) * (__u64)w;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700204 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
220static int bucket_straw_choose(struct crush_bucket_straw *bucket,
221 int x, int r)
222{
Sage Weil8b12d472012-05-07 15:38:35 -0700223 __u32 i;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700224 int high = 0;
225 __u64 high_draw = 0;
226 __u64 draw;
227
228 for (i = 0; i < bucket->h.size; i++) {
Sage Weilfb690392009-11-07 20:18:22 -0800229 draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700230 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
240static int crush_bucket_choose(struct crush_bucket *in, int x, int r)
241{
Sage Weila1a31e72010-06-24 12:58:14 -0700242 dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
Sage Weila1f48952012-05-07 15:35:24 -0700243 BUG_ON(in->size == 0);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700244 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:
Sage Weila1f48952012-05-07 15:35:24 -0700258 dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
Sage Weil50b885b2009-12-01 14:12:07 -0800259 return in->items[0];
Sage Weil5ecc0a02009-10-06 11:31:11 -0700260 }
261}
262
263/*
264 * true if device is marked "out" (failed, fully offloaded)
265 * of the cluster
266 */
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200267static int is_out(const struct crush_map *map,
268 const __u32 *weight, int weight_max,
269 int item, int x)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700270{
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200271 if (item >= weight_max)
272 return 1;
Sage Weil153a1092010-07-05 09:44:17 -0700273 if (weight[item] >= 0x10000)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700274 return 0;
275 if (weight[item] == 0)
276 return 1;
Sage Weilfb690392009-11-07 20:18:22 -0800277 if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
278 < weight[item])
Sage Weil5ecc0a02009-10-06 11:31:11 -0700279 return 0;
280 return 1;
281}
282
283/**
284 * crush_choose - 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 * @firstn: true if choosing "first n" items, false if choosing "indep"
293 * @recurse_to_leaf: true if we want one device under each item of given type
Jim Schutt1604f482012-11-30 09:15:25 -0700294 * @descend_once: true if we should only try one descent before giving up
Sage Weil5ecc0a02009-10-06 11:31:11 -0700295 * @out2: second output vector for leaf items (if @recurse_to_leaf)
296 */
Sage Weil8b12d472012-05-07 15:38:35 -0700297static int crush_choose(const struct crush_map *map,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700298 struct crush_bucket *bucket,
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200299 const __u32 *weight, int weight_max,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700300 int x, int numrep, int type,
301 int *out, int outpos,
302 int firstn, int recurse_to_leaf,
Jim Schutt1604f482012-11-30 09:15:25 -0700303 int descend_once, int *out2)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700304{
305 int rep;
Sage Weil8b12d472012-05-07 15:38:35 -0700306 unsigned int ftotal, flocal;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700307 int retry_descent, retry_bucket, skip_rep;
308 struct crush_bucket *in = bucket;
309 int r;
310 int i;
Sage Weilb28813a2009-10-07 10:59:34 -0700311 int item = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700312 int itemtype;
313 int collide, reject;
Sage Weila1a31e72010-06-24 12:58:14 -0700314
315 dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
316 bucket->id, x, outpos, numrep);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700317
318 for (rep = outpos; rep < numrep; rep++) {
319 /* keep trying until we get a non-out, non-colliding item */
320 ftotal = 0;
321 skip_rep = 0;
322 do {
323 retry_descent = 0;
324 in = bucket; /* initial bucket */
325
326 /* choose through intervening buckets */
327 flocal = 0;
328 do {
Sage Weilb28813a2009-10-07 10:59:34 -0700329 collide = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700330 retry_bucket = 0;
331 r = rep;
332 if (in->alg == CRUSH_BUCKET_UNIFORM) {
333 /* be careful */
Sage Weil8b12d472012-05-07 15:38:35 -0700334 if (firstn || (__u32)numrep >= in->size)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700335 /* r' = r + f_total */
336 r += ftotal;
337 else if (in->size % numrep == 0)
338 /* r'=r+(n+1)*f_local */
339 r += (numrep+1) *
340 (flocal+ftotal);
341 else
342 /* r' = r + n*f_local */
343 r += numrep * (flocal+ftotal);
344 } else {
345 if (firstn)
346 /* r' = r + f_total */
347 r += ftotal;
348 else
349 /* r' = r + n*f_local */
350 r += numrep * (flocal+ftotal);
351 }
352
353 /* bucket choose */
Sage Weilb28813a2009-10-07 10:59:34 -0700354 if (in->size == 0) {
355 reject = 1;
356 goto reject;
357 }
Sage Weil546f04e2012-07-30 18:15:23 -0700358 if (map->choose_local_fallback_tries > 0 &&
359 flocal >= (in->size>>1) &&
360 flocal > map->choose_local_fallback_tries)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700361 item = bucket_perm_choose(in, x, r);
362 else
363 item = crush_bucket_choose(in, x, r);
Sage Weila1f48952012-05-07 15:35:24 -0700364 if (item >= map->max_devices) {
365 dprintk(" bad item %d\n", item);
366 skip_rep = 1;
367 break;
368 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700369
370 /* desired type? */
371 if (item < 0)
372 itemtype = map->buckets[-1-item]->type;
373 else
374 itemtype = 0;
375 dprintk(" item %d type %d\n", item, itemtype);
376
377 /* keep going? */
378 if (itemtype != type) {
Sage Weila1f48952012-05-07 15:35:24 -0700379 if (item >= 0 ||
380 (-1-item) >= map->max_buckets) {
381 dprintk(" bad item type %d\n", type);
382 skip_rep = 1;
383 break;
384 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700385 in = map->buckets[-1-item];
Sage Weil55bda7a2010-06-24 12:55:48 -0700386 retry_bucket = 1;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700387 continue;
388 }
389
390 /* collision? */
Sage Weil5ecc0a02009-10-06 11:31:11 -0700391 for (i = 0; i < outpos; i++) {
392 if (out[i] == item) {
393 collide = 1;
394 break;
395 }
396 }
397
Sage Weila1a31e72010-06-24 12:58:14 -0700398 reject = 0;
Sage Weil7d7c1f62013-01-15 18:49:09 -0800399 if (!collide && recurse_to_leaf) {
Sage Weila1a31e72010-06-24 12:58:14 -0700400 if (item < 0) {
401 if (crush_choose(map,
402 map->buckets[-1-item],
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200403 weight, weight_max,
Sage Weila1a31e72010-06-24 12:58:14 -0700404 x, outpos+1, 0,
405 out2, outpos,
406 firstn, 0,
Jim Schutt1604f482012-11-30 09:15:25 -0700407 map->chooseleaf_descend_once,
Sage Weila1a31e72010-06-24 12:58:14 -0700408 NULL) <= outpos)
409 /* didn't get leaf */
410 reject = 1;
411 } else {
412 /* we already have a leaf! */
413 out2[outpos] = item;
414 }
415 }
416
417 if (!reject) {
Sage Weil5ecc0a02009-10-06 11:31:11 -0700418 /* out? */
419 if (itemtype == 0)
420 reject = is_out(map, weight,
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200421 weight_max,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700422 item, x);
423 else
424 reject = 0;
425 }
426
Sage Weilb28813a2009-10-07 10:59:34 -0700427reject:
Sage Weil5ecc0a02009-10-06 11:31:11 -0700428 if (reject || collide) {
429 ftotal++;
430 flocal++;
431
Jim Schutt1604f482012-11-30 09:15:25 -0700432 if (reject && descend_once)
433 /* let outer call try again */
434 skip_rep = 1;
435 else if (collide && flocal <= map->choose_local_tries)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700436 /* retry locally a few times */
437 retry_bucket = 1;
Sage Weil546f04e2012-07-30 18:15:23 -0700438 else if (map->choose_local_fallback_tries > 0 &&
439 flocal <= in->size + map->choose_local_fallback_tries)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700440 /* exhaustive bucket search */
441 retry_bucket = 1;
Sage Weil546f04e2012-07-30 18:15:23 -0700442 else if (ftotal <= map->choose_total_tries)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700443 /* then retry descent */
444 retry_descent = 1;
445 else
446 /* else give up */
447 skip_rep = 1;
448 dprintk(" reject %d collide %d "
Sage Weil8b12d472012-05-07 15:38:35 -0700449 "ftotal %u flocal %u\n",
Sage Weil5ecc0a02009-10-06 11:31:11 -0700450 reject, collide, ftotal,
451 flocal);
452 }
453 } while (retry_bucket);
454 } while (retry_descent);
455
456 if (skip_rep) {
457 dprintk("skip rep\n");
458 continue;
459 }
460
Sage Weila1a31e72010-06-24 12:58:14 -0700461 dprintk("CHOOSE got %d\n", item);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700462 out[outpos] = item;
463 outpos++;
464 }
465
Sage Weila1a31e72010-06-24 12:58:14 -0700466 dprintk("CHOOSE returns %d\n", outpos);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700467 return outpos;
468}
469
470
471/**
472 * crush_do_rule - calculate a mapping with the given input and rule
473 * @map: the crush_map
474 * @ruleno: the rule id
475 * @x: hash input
476 * @result: pointer to result vector
477 * @result_max: maximum result size
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200478 * @weight: weight vector (for map leaves)
479 * @weight_max: size of weight vector
Sage Weil5ecc0a02009-10-06 11:31:11 -0700480 */
Sage Weil8b12d472012-05-07 15:38:35 -0700481int crush_do_rule(const struct crush_map *map,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700482 int ruleno, int x, int *result, int result_max,
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200483 const __u32 *weight, int weight_max)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700484{
485 int result_len;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700486 int a[CRUSH_MAX_SET];
487 int b[CRUSH_MAX_SET];
488 int c[CRUSH_MAX_SET];
489 int recurse_to_leaf;
490 int *w;
491 int wsize = 0;
492 int *o;
493 int osize;
494 int *tmp;
495 struct crush_rule *rule;
Sage Weil8b12d472012-05-07 15:38:35 -0700496 __u32 step;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700497 int i, j;
498 int numrep;
499 int firstn;
Jim Schutt1604f482012-11-30 09:15:25 -0700500 const int descend_once = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700501
Sage Weila1f48952012-05-07 15:35:24 -0700502 if ((__u32)ruleno >= map->max_rules) {
503 dprintk(" bad ruleno %d\n", ruleno);
504 return 0;
505 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700506
507 rule = map->rules[ruleno];
508 result_len = 0;
509 w = a;
510 o = b;
511
Sage Weil5ecc0a02009-10-06 11:31:11 -0700512 for (step = 0; step < rule->len; step++) {
Sage Weil06682162012-05-07 15:35:48 -0700513 struct crush_rule_step *curstep = &rule->steps[step];
514
Sage Weil5ecc0a02009-10-06 11:31:11 -0700515 firstn = 0;
Sage Weil06682162012-05-07 15:35:48 -0700516 switch (curstep->op) {
Sage Weil5ecc0a02009-10-06 11:31:11 -0700517 case CRUSH_RULE_TAKE:
Sage Weil06682162012-05-07 15:35:48 -0700518 w[0] = curstep->arg1;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700519 wsize = 1;
520 break;
521
522 case CRUSH_RULE_CHOOSE_LEAF_FIRSTN:
523 case CRUSH_RULE_CHOOSE_FIRSTN:
524 firstn = 1;
Sage Weil06682162012-05-07 15:35:48 -0700525 /* fall through */
Sage Weil5ecc0a02009-10-06 11:31:11 -0700526 case CRUSH_RULE_CHOOSE_LEAF_INDEP:
527 case CRUSH_RULE_CHOOSE_INDEP:
Sage Weila1f48952012-05-07 15:35:24 -0700528 if (wsize == 0)
529 break;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700530
531 recurse_to_leaf =
Sage Weil06682162012-05-07 15:35:48 -0700532 curstep->op ==
Sage Weil5ecc0a02009-10-06 11:31:11 -0700533 CRUSH_RULE_CHOOSE_LEAF_FIRSTN ||
Sage Weil06682162012-05-07 15:35:48 -0700534 curstep->op ==
Sage Weil5ecc0a02009-10-06 11:31:11 -0700535 CRUSH_RULE_CHOOSE_LEAF_INDEP;
536
537 /* reset output */
538 osize = 0;
539
540 for (i = 0; i < wsize; i++) {
541 /*
542 * see CRUSH_N, CRUSH_N_MINUS macros.
543 * basically, numrep <= 0 means relative to
544 * the provided result_max
545 */
Sage Weil06682162012-05-07 15:35:48 -0700546 numrep = curstep->arg1;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700547 if (numrep <= 0) {
548 numrep += result_max;
549 if (numrep <= 0)
550 continue;
551 }
552 j = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700553 osize += crush_choose(map,
554 map->buckets[-1-w[i]],
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200555 weight, weight_max,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700556 x, numrep,
Sage Weil06682162012-05-07 15:35:48 -0700557 curstep->arg2,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700558 o+osize, j,
559 firstn,
Jim Schutt1604f482012-11-30 09:15:25 -0700560 recurse_to_leaf,
561 descend_once, c+osize);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700562 }
563
564 if (recurse_to_leaf)
565 /* copy final _leaf_ values to output set */
566 memcpy(o, c, osize*sizeof(*o));
567
568 /* swap t and w arrays */
569 tmp = o;
570 o = w;
571 w = tmp;
572 wsize = osize;
573 break;
574
575
576 case CRUSH_RULE_EMIT:
577 for (i = 0; i < wsize && result_len < result_max; i++) {
578 result[result_len] = w[i];
579 result_len++;
580 }
581 wsize = 0;
582 break;
583
584 default:
Sage Weila1f48952012-05-07 15:35:24 -0700585 dprintk(" unknown op %d at step %d\n",
586 curstep->op, step);
587 break;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700588 }
589 }
Sage Weilf1932fc2011-12-07 09:10:26 -0800590 return result_len;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700591}
592
593