mapper.c 18.1 KB
Newer Older
S
Sage Weil 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

#ifdef __KERNEL__
# include <linux/string.h>
# include <linux/slab.h>
# include <linux/bug.h>
# include <linux/kernel.h>
# ifndef dprintk
#  define dprintk(args...)
# endif
#else
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
# define BUG_ON(x) assert(!(x))
# define dprintk(args...) /* printf(args) */
# define kmalloc(x, f) malloc(x)
# define kfree(x) free(x)
#endif

21 22
#include <linux/crush/crush.h>
#include <linux/crush/hash.h>
23
#include <linux/crush/mapper.h>
S
Sage Weil 已提交
24 25 26 27 28 29 30 31 32 33 34 35

/*
 * Implement the core CRUSH mapping algorithm.
 */

/**
 * crush_find_rule - find a crush_rule id for a given ruleset, type, and size.
 * @map: the crush_map
 * @ruleset: the storage ruleset id (user defined)
 * @type: storage ruleset type (user defined)
 * @size: output set size
 */
S
Sage Weil 已提交
36
int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
S
Sage Weil 已提交
37
{
S
Sage Weil 已提交
38
	__u32 i;
S
Sage Weil 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

	for (i = 0; i < map->max_rules; i++) {
		if (map->rules[i] &&
		    map->rules[i]->mask.ruleset == ruleset &&
		    map->rules[i]->mask.type == type &&
		    map->rules[i]->mask.min_size <= size &&
		    map->rules[i]->mask.max_size >= size)
			return i;
	}
	return -1;
}


/*
 * bucket choose methods
 *
 * For each bucket algorithm, we have a "choose" method that, given a
 * crush input @x and replica position (usually, position in output set) @r,
 * will produce an item in the bucket.
 */

/*
 * Choose based on a random permutation of the bucket.
 *
 * We used to use some prime number arithmetic to do this, but it
 * wasn't very random, and had some other bad behaviors.  Instead, we
 * calculate an actual random permutation of the bucket members.
 * Since this is expensive, we optimize for the r=0 case, which
 * captures the vast majority of calls.
 */
static int bucket_perm_choose(struct crush_bucket *bucket,
			      int x, int r)
{
72 73
	unsigned int pr = r % bucket->size;
	unsigned int i, s;
S
Sage Weil 已提交
74 75

	/* start a new permutation if @x has changed */
S
Sage Weil 已提交
76
	if (bucket->perm_x != (__u32)x || bucket->perm_n == 0) {
S
Sage Weil 已提交
77 78 79 80 81
		dprintk("bucket %d new x=%d\n", bucket->id, x);
		bucket->perm_x = x;

		/* optimize common r=0 case */
		if (pr == 0) {
82
			s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
S
Sage Weil 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
				bucket->size;
			bucket->perm[0] = s;
			bucket->perm_n = 0xffff;   /* magic value, see below */
			goto out;
		}

		for (i = 0; i < bucket->size; i++)
			bucket->perm[i] = i;
		bucket->perm_n = 0;
	} else if (bucket->perm_n == 0xffff) {
		/* clean up after the r=0 case above */
		for (i = 1; i < bucket->size; i++)
			bucket->perm[i] = i;
		bucket->perm[bucket->perm[0]] = 0;
		bucket->perm_n = 1;
	}

	/* calculate permutation up to pr */
	for (i = 0; i < bucket->perm_n; i++)
		dprintk(" perm_choose have %d: %d\n", i, bucket->perm[i]);
	while (bucket->perm_n <= pr) {
104
		unsigned int p = bucket->perm_n;
S
Sage Weil 已提交
105 106
		/* no point in swapping the final entry */
		if (p < bucket->size - 1) {
107
			i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
S
Sage Weil 已提交
108 109
				(bucket->size - p);
			if (i) {
110
				unsigned int t = bucket->perm[p + i];
S
Sage Weil 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
				bucket->perm[p + i] = bucket->perm[p];
				bucket->perm[p] = t;
			}
			dprintk(" perm_choose swap %d with %d\n", p, p+i);
		}
		bucket->perm_n++;
	}
	for (i = 0; i < bucket->size; i++)
		dprintk(" perm_choose  %d: %d\n", i, bucket->perm[i]);

	s = bucket->perm[pr];
out:
	dprintk(" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->id,
		bucket->size, x, r, pr, s);
	return bucket->items[s];
}

/* uniform */
static int bucket_uniform_choose(struct crush_bucket_uniform *bucket,
				 int x, int r)
{
	return bucket_perm_choose(&bucket->h, x, r);
}

/* list */
static int bucket_list_choose(struct crush_bucket_list *bucket,
			      int x, int r)
{
	int i;

	for (i = bucket->h.size-1; i >= 0; i--) {
142 143
		__u64 w = crush_hash32_4(bucket->h.hash,x, bucket->h.items[i],
					 r, bucket->h.id);
S
Sage Weil 已提交
144 145 146 147 148 149 150 151 152 153 154 155
		w &= 0xffff;
		dprintk("list_choose i=%d x=%d r=%d item %d weight %x "
			"sw %x rand %llx",
			i, x, r, bucket->h.items[i], bucket->item_weights[i],
			bucket->sum_weights[i], w);
		w *= bucket->sum_weights[i];
		w = w >> 16;
		/*dprintk(" scaled %llx\n", w);*/
		if (w < bucket->item_weights[i])
			return bucket->h.items[i];
	}

156 157
	dprintk("bad list sums for bucket %d\n", bucket->h.id);
	return bucket->h.items[0];
S
Sage Weil 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
}


/* (binary) tree */
static int height(int n)
{
	int h = 0;
	while ((n & 1) == 0) {
		h++;
		n = n >> 1;
	}
	return h;
}

static int left(int x)
{
	int h = height(x);
	return x - (1 << (h-1));
}

static int right(int x)
{
	int h = height(x);
	return x + (1 << (h-1));
}

static int terminal(int x)
{
	return x & 1;
}

static int bucket_tree_choose(struct crush_bucket_tree *bucket,
			      int x, int r)
{
192
	int n;
S
Sage Weil 已提交
193 194 195 196 197 198 199
	__u32 w;
	__u64 t;

	/* start at root */
	n = bucket->num_nodes >> 1;

	while (!terminal(n)) {
200
		int l;
S
Sage Weil 已提交
201 202
		/* pick point in [0, w) */
		w = bucket->node_weights[n];
203 204
		t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
					  bucket->h.id) * (__u64)w;
S
Sage Weil 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
		t = t >> 32;

		/* descend to the left or right? */
		l = left(n);
		if (t < bucket->node_weights[l])
			n = l;
		else
			n = right(n);
	}

	return bucket->h.items[n >> 1];
}


/* straw */

static int bucket_straw_choose(struct crush_bucket_straw *bucket,
			       int x, int r)
{
S
Sage Weil 已提交
224
	__u32 i;
S
Sage Weil 已提交
225 226 227 228 229
	int high = 0;
	__u64 high_draw = 0;
	__u64 draw;

	for (i = 0; i < bucket->h.size; i++) {
230
		draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
S
Sage Weil 已提交
231 232 233 234 235 236 237 238 239 240 241 242
		draw &= 0xffff;
		draw *= bucket->straws[i];
		if (i == 0 || draw > high_draw) {
			high = i;
			high_draw = draw;
		}
	}
	return bucket->h.items[high];
}

static int crush_bucket_choose(struct crush_bucket *in, int x, int r)
{
243
	dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
244
	BUG_ON(in->size == 0);
S
Sage Weil 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258
	switch (in->alg) {
	case CRUSH_BUCKET_UNIFORM:
		return bucket_uniform_choose((struct crush_bucket_uniform *)in,
					  x, r);
	case CRUSH_BUCKET_LIST:
		return bucket_list_choose((struct crush_bucket_list *)in,
					  x, r);
	case CRUSH_BUCKET_TREE:
		return bucket_tree_choose((struct crush_bucket_tree *)in,
					  x, r);
	case CRUSH_BUCKET_STRAW:
		return bucket_straw_choose((struct crush_bucket_straw *)in,
					   x, r);
	default:
259
		dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
S
Sage Weil 已提交
260
		return in->items[0];
S
Sage Weil 已提交
261 262 263 264 265 266 267
	}
}

/*
 * true if device is marked "out" (failed, fully offloaded)
 * of the cluster
 */
268 269 270
static int is_out(const struct crush_map *map,
		  const __u32 *weight, int weight_max,
		  int item, int x)
S
Sage Weil 已提交
271
{
272 273
	if (item >= weight_max)
		return 1;
274
	if (weight[item] >= 0x10000)
S
Sage Weil 已提交
275 276 277
		return 0;
	if (weight[item] == 0)
		return 1;
278 279
	if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
	    < weight[item])
S
Sage Weil 已提交
280 281 282 283 284
		return 0;
	return 1;
}

/**
285
 * crush_choose_firstn - choose numrep distinct items of given type
S
Sage Weil 已提交
286 287 288 289 290 291 292
 * @map: the crush_map
 * @bucket: the bucket we are choose an item from
 * @x: crush input value
 * @numrep: the number of items to choose
 * @type: the type of item to choose
 * @out: pointer to output vector
 * @outpos: our position in that vector
293 294
 * @tries: number of attempts to make
 * @recurse_tries: number of attempts to have recursive chooseleaf make
295 296
 * @local_retries: localized retries
 * @local_fallback_retries: localized fallback retries
297
 * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose)
S
Sage Weil 已提交
298 299
 * @out2: second output vector for leaf items (if @recurse_to_leaf)
 */
300 301 302 303 304
static int crush_choose_firstn(const struct crush_map *map,
			       struct crush_bucket *bucket,
			       const __u32 *weight, int weight_max,
			       int x, int numrep, int type,
			       int *out, int outpos,
I
Ilya Dryomov 已提交
305 306
			       unsigned int tries,
			       unsigned int recurse_tries,
307 308
			       unsigned int local_retries,
			       unsigned int local_fallback_retries,
309
			       int recurse_to_leaf,
I
Ilya Dryomov 已提交
310
			       int *out2)
S
Sage Weil 已提交
311 312
{
	int rep;
S
Sage Weil 已提交
313
	unsigned int ftotal, flocal;
S
Sage Weil 已提交
314 315 316 317
	int retry_descent, retry_bucket, skip_rep;
	struct crush_bucket *in = bucket;
	int r;
	int i;
318
	int item = 0;
S
Sage Weil 已提交
319 320
	int itemtype;
	int collide, reject;
321 322 323

	dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
		bucket->id, x, outpos, numrep);
S
Sage Weil 已提交
324 325 326 327 328 329 330 331 332 333 334 335

	for (rep = outpos; rep < numrep; rep++) {
		/* keep trying until we get a non-out, non-colliding item */
		ftotal = 0;
		skip_rep = 0;
		do {
			retry_descent = 0;
			in = bucket;               /* initial bucket */

			/* choose through intervening buckets */
			flocal = 0;
			do {
336
				collide = 0;
S
Sage Weil 已提交
337 338
				retry_bucket = 0;
				r = rep;
339 340
				/* r' = r + f_total */
				r += ftotal;
S
Sage Weil 已提交
341 342

				/* bucket choose */
343 344 345 346
				if (in->size == 0) {
					reject = 1;
					goto reject;
				}
347
				if (local_fallback_retries > 0 &&
S
Sage Weil 已提交
348
				    flocal >= (in->size>>1) &&
349
				    flocal > local_fallback_retries)
S
Sage Weil 已提交
350 351 352
					item = bucket_perm_choose(in, x, r);
				else
					item = crush_bucket_choose(in, x, r);
353 354 355 356 357
				if (item >= map->max_devices) {
					dprintk("   bad item %d\n", item);
					skip_rep = 1;
					break;
				}
S
Sage Weil 已提交
358 359 360 361 362 363 364 365 366 367

				/* desired type? */
				if (item < 0)
					itemtype = map->buckets[-1-item]->type;
				else
					itemtype = 0;
				dprintk("  item %d type %d\n", item, itemtype);

				/* keep going? */
				if (itemtype != type) {
368 369 370 371 372 373
					if (item >= 0 ||
					    (-1-item) >= map->max_buckets) {
						dprintk("   bad item type %d\n", type);
						skip_rep = 1;
						break;
					}
S
Sage Weil 已提交
374
					in = map->buckets[-1-item];
S
Sage Weil 已提交
375
					retry_bucket = 1;
S
Sage Weil 已提交
376 377 378 379 380 381 382 383 384 385 386
					continue;
				}

				/* collision? */
				for (i = 0; i < outpos; i++) {
					if (out[i] == item) {
						collide = 1;
						break;
					}
				}

387
				reject = 0;
388
				if (!collide && recurse_to_leaf) {
389
					if (item < 0) {
390
						if (crush_choose_firstn(map,
391
							 map->buckets[-1-item],
392
							 weight, weight_max,
393 394
							 x, outpos+1, 0,
							 out2, outpos,
I
Ilya Dryomov 已提交
395
							 recurse_tries, 0,
396 397
							 local_retries,
							 local_fallback_retries,
398
							 0,
399 400 401 402 403 404 405 406 407 408
							 NULL) <= outpos)
							/* didn't get leaf */
							reject = 1;
					} else {
						/* we already have a leaf! */
						out2[outpos] = item;
					}
				}

				if (!reject) {
S
Sage Weil 已提交
409 410 411
					/* out? */
					if (itemtype == 0)
						reject = is_out(map, weight,
412
								weight_max,
S
Sage Weil 已提交
413 414 415 416 417
								item, x);
					else
						reject = 0;
				}

418
reject:
S
Sage Weil 已提交
419 420 421 422
				if (reject || collide) {
					ftotal++;
					flocal++;

423
					if (collide && flocal <= local_retries)
S
Sage Weil 已提交
424 425
						/* retry locally a few times */
						retry_bucket = 1;
426 427
					else if (local_fallback_retries > 0 &&
						 flocal <= in->size + local_fallback_retries)
S
Sage Weil 已提交
428 429
						/* exhaustive bucket search */
						retry_bucket = 1;
430
					else if (ftotal < tries)
S
Sage Weil 已提交
431 432 433 434 435 436
						/* then retry descent */
						retry_descent = 1;
					else
						/* else give up */
						skip_rep = 1;
					dprintk("  reject %d  collide %d  "
S
Sage Weil 已提交
437
						"ftotal %u  flocal %u\n",
S
Sage Weil 已提交
438 439 440 441 442 443 444
						reject, collide, ftotal,
						flocal);
				}
			} while (retry_bucket);
		} while (retry_descent);

		if (skip_rep) {
445 446
			dprintk("skip rep\n");
			continue;
S
Sage Weil 已提交
447 448
		}

449
		dprintk("CHOOSE got %d\n", item);
S
Sage Weil 已提交
450 451 452 453
		out[outpos] = item;
		outpos++;
	}

454
	dprintk("CHOOSE returns %d\n", outpos);
S
Sage Weil 已提交
455 456 457 458
	return outpos;
}


459
/**
460
 * crush_choose_indep: alternative breadth-first positionally stable mapping
461 462 463
 *
 */
static void crush_choose_indep(const struct crush_map *map,
464 465
			       struct crush_bucket *bucket,
			       const __u32 *weight, int weight_max,
I
Ilya Dryomov 已提交
466
			       int x, int left, int numrep, int type,
467
			       int *out, int outpos,
I
Ilya Dryomov 已提交
468 469
			       unsigned int tries,
			       unsigned int recurse_tries,
470
			       int recurse_to_leaf,
471 472
			       int *out2,
			       int parent_r)
473 474
{
	struct crush_bucket *in = bucket;
I
Ilya Dryomov 已提交
475
	int endpos = outpos + left;
476 477 478 479 480 481 482 483 484 485 486 487
	int rep;
	unsigned int ftotal;
	int r;
	int i;
	int item = 0;
	int itemtype;
	int collide;

	dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
		bucket->id, x, outpos, numrep);

	/* initially my result is undefined */
I
Ilya Dryomov 已提交
488
	for (rep = outpos; rep < endpos; rep++) {
489 490 491 492 493
		out[rep] = CRUSH_ITEM_UNDEF;
		if (out2)
			out2[rep] = CRUSH_ITEM_UNDEF;
	}

I
Ilya Dryomov 已提交
494
	for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) {
I
Ilya Dryomov 已提交
495
		for (rep = outpos; rep < endpos; rep++) {
496 497 498 499 500 501 502
			if (out[rep] != CRUSH_ITEM_UNDEF)
				continue;

			in = bucket;  /* initial bucket */

			/* choose through intervening buckets */
			for (;;) {
503 504 505 506 507 508 509 510
				/* note: we base the choice on the position
				 * even in the nested call.  that means that
				 * if the first layer chooses the same bucket
				 * in a different position, we will tend to
				 * choose a different item in that bucket.
				 * this will involve more devices in data
				 * movement and tend to distribute the load.
				 */
511
				r = rep + parent_r;
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562

				/* be careful */
				if (in->alg == CRUSH_BUCKET_UNIFORM &&
				    in->size % numrep == 0)
					/* r'=r+(n+1)*f_total */
					r += (numrep+1) * ftotal;
				else
					/* r' = r + n*f_total */
					r += numrep * ftotal;

				/* bucket choose */
				if (in->size == 0) {
					dprintk("   empty bucket\n");
					break;
				}

				item = crush_bucket_choose(in, x, r);
				if (item >= map->max_devices) {
					dprintk("   bad item %d\n", item);
					out[rep] = CRUSH_ITEM_NONE;
					if (out2)
						out2[rep] = CRUSH_ITEM_NONE;
					left--;
					break;
				}

				/* desired type? */
				if (item < 0)
					itemtype = map->buckets[-1-item]->type;
				else
					itemtype = 0;
				dprintk("  item %d type %d\n", item, itemtype);

				/* keep going? */
				if (itemtype != type) {
					if (item >= 0 ||
					    (-1-item) >= map->max_buckets) {
						dprintk("   bad item type %d\n", type);
						out[rep] = CRUSH_ITEM_NONE;
						if (out2)
							out2[rep] =
								CRUSH_ITEM_NONE;
						left--;
						break;
					}
					in = map->buckets[-1-item];
					continue;
				}

				/* collision? */
				collide = 0;
I
Ilya Dryomov 已提交
563
				for (i = outpos; i < endpos; i++) {
564 565 566 567 568 569 570 571 572 573 574
					if (out[i] == item) {
						collide = 1;
						break;
					}
				}
				if (collide)
					break;

				if (recurse_to_leaf) {
					if (item < 0) {
						crush_choose_indep(map,
575 576 577 578
						   map->buckets[-1-item],
						   weight, weight_max,
						   x, 1, numrep, 0,
						   out2, rep,
I
Ilya Dryomov 已提交
579
						   recurse_tries, 0,
580
						   0, NULL, r);
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
						if (out2[rep] == CRUSH_ITEM_NONE) {
							/* placed nothing; no leaf */
							break;
						}
					} else {
						/* we already have a leaf! */
						out2[rep] = item;
					}
				}

				/* out? */
				if (itemtype == 0 &&
				    is_out(map, weight, weight_max, item, x))
					break;

				/* yay! */
				out[rep] = item;
				left--;
				break;
			}
		}
	}
I
Ilya Dryomov 已提交
603
	for (rep = outpos; rep < endpos; rep++) {
604 605 606 607 608 609 610 611 612
		if (out[rep] == CRUSH_ITEM_UNDEF) {
			out[rep] = CRUSH_ITEM_NONE;
		}
		if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) {
			out2[rep] = CRUSH_ITEM_NONE;
		}
	}
}

S
Sage Weil 已提交
613 614 615 616 617 618 619
/**
 * crush_do_rule - calculate a mapping with the given input and rule
 * @map: the crush_map
 * @ruleno: the rule id
 * @x: hash input
 * @result: pointer to result vector
 * @result_max: maximum result size
620 621
 * @weight: weight vector (for map leaves)
 * @weight_max: size of weight vector
622
 * @scratch: scratch vector for private use; must be >= 3 * result_max
S
Sage Weil 已提交
623
 */
S
Sage Weil 已提交
624
int crush_do_rule(const struct crush_map *map,
S
Sage Weil 已提交
625
		  int ruleno, int x, int *result, int result_max,
626 627
		  const __u32 *weight, int weight_max,
		  int *scratch)
S
Sage Weil 已提交
628 629
{
	int result_len;
630 631 632
	int *a = scratch;
	int *b = scratch + result_max;
	int *c = scratch + result_max*2;
S
Sage Weil 已提交
633 634 635 636 637 638 639
	int recurse_to_leaf;
	int *w;
	int wsize = 0;
	int *o;
	int osize;
	int *tmp;
	struct crush_rule *rule;
S
Sage Weil 已提交
640
	__u32 step;
S
Sage Weil 已提交
641 642
	int i, j;
	int numrep;
643 644 645 646 647
	/*
	 * the original choose_total_tries value was off by one (it
	 * counted "retries" and not "tries").  add one.
	 */
	int choose_tries = map->choose_total_tries + 1;
648
	int choose_leaf_tries = 0;
649 650 651 652 653 654
	/*
	 * the local tries values were counted as "retries", though,
	 * and need no adjustment
	 */
	int choose_local_retries = map->choose_local_tries;
	int choose_local_fallback_retries = map->choose_local_fallback_tries;
S
Sage Weil 已提交
655

656 657 658 659
	if ((__u32)ruleno >= map->max_rules) {
		dprintk(" bad ruleno %d\n", ruleno);
		return 0;
	}
S
Sage Weil 已提交
660 661 662 663 664 665 666

	rule = map->rules[ruleno];
	result_len = 0;
	w = a;
	o = b;

	for (step = 0; step < rule->len; step++) {
667
		int firstn = 0;
668 669 670
		struct crush_rule_step *curstep = &rule->steps[step];

		switch (curstep->op) {
S
Sage Weil 已提交
671
		case CRUSH_RULE_TAKE:
672
			w[0] = curstep->arg1;
S
Sage Weil 已提交
673 674 675
			wsize = 1;
			break;

676 677 678 679 680
		case CRUSH_RULE_SET_CHOOSE_TRIES:
			if (curstep->arg1 > 0)
				choose_tries = curstep->arg1;
			break;

681
		case CRUSH_RULE_SET_CHOOSELEAF_TRIES:
682 683 684 685
			if (curstep->arg1 > 0)
				choose_leaf_tries = curstep->arg1;
			break;

686 687
		case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES:
			if (curstep->arg1 > 0)
688
				choose_local_retries = curstep->arg1;
689 690 691 692
			break;

		case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES:
			if (curstep->arg1 > 0)
693
				choose_local_fallback_retries = curstep->arg1;
694 695
			break;

696
		case CRUSH_RULE_CHOOSELEAF_FIRSTN:
S
Sage Weil 已提交
697 698
		case CRUSH_RULE_CHOOSE_FIRSTN:
			firstn = 1;
699
			/* fall through */
700
		case CRUSH_RULE_CHOOSELEAF_INDEP:
S
Sage Weil 已提交
701
		case CRUSH_RULE_CHOOSE_INDEP:
702 703
			if (wsize == 0)
				break;
S
Sage Weil 已提交
704 705

			recurse_to_leaf =
706
				curstep->op ==
707
				 CRUSH_RULE_CHOOSELEAF_FIRSTN ||
708
				curstep->op ==
709
				CRUSH_RULE_CHOOSELEAF_INDEP;
S
Sage Weil 已提交
710 711 712 713 714 715 716 717 718 719

			/* reset output */
			osize = 0;

			for (i = 0; i < wsize; i++) {
				/*
				 * see CRUSH_N, CRUSH_N_MINUS macros.
				 * basically, numrep <= 0 means relative to
				 * the provided result_max
				 */
720
				numrep = curstep->arg1;
S
Sage Weil 已提交
721 722 723 724 725 726
				if (numrep <= 0) {
					numrep += result_max;
					if (numrep <= 0)
						continue;
				}
				j = 0;
727
				if (firstn) {
I
Ilya Dryomov 已提交
728 729 730 731 732 733 734 735
					int recurse_tries;
					if (choose_leaf_tries)
						recurse_tries =
							choose_leaf_tries;
					else if (map->chooseleaf_descend_once)
						recurse_tries = 1;
					else
						recurse_tries = choose_tries;
736 737 738 739 740 741 742
					osize += crush_choose_firstn(
						map,
						map->buckets[-1-w[i]],
						weight, weight_max,
						x, numrep,
						curstep->arg2,
						o+osize, j,
743
						choose_tries,
I
Ilya Dryomov 已提交
744
						recurse_tries,
745 746
						choose_local_retries,
						choose_local_fallback_retries,
747
						recurse_to_leaf,
I
Ilya Dryomov 已提交
748
						c+osize);
749
				} else {
750 751 752 753
					crush_choose_indep(
						map,
						map->buckets[-1-w[i]],
						weight, weight_max,
I
Ilya Dryomov 已提交
754
						x, numrep, numrep,
755 756
						curstep->arg2,
						o+osize, j,
757
						choose_tries,
I
Ilya Dryomov 已提交
758 759
						choose_leaf_tries ?
						   choose_leaf_tries : 1,
760
						recurse_to_leaf,
761 762
						c+osize,
						0);
763 764
					osize += numrep;
				}
S
Sage Weil 已提交
765 766 767 768 769 770
			}

			if (recurse_to_leaf)
				/* copy final _leaf_ values to output set */
				memcpy(o, c, osize*sizeof(*o));

I
Ilya Dryomov 已提交
771
			/* swap o and w arrays */
S
Sage Weil 已提交
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
			tmp = o;
			o = w;
			w = tmp;
			wsize = osize;
			break;


		case CRUSH_RULE_EMIT:
			for (i = 0; i < wsize && result_len < result_max; i++) {
				result[result_len] = w[i];
				result_len++;
			}
			wsize = 0;
			break;

		default:
788 789 790
			dprintk(" unknown op %d at step %d\n",
				curstep->op, step);
			break;
S
Sage Weil 已提交
791 792
		}
	}
793
	return result_len;
S
Sage Weil 已提交
794 795 796
}