mapper.c 22.5 KB
Newer Older
I
Ilya Dryomov 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2015 Intel Corporation All Rights Reserved
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */
S
Sage Weil 已提交
12 13 14 15 16 17

#ifdef __KERNEL__
# include <linux/string.h>
# include <linux/slab.h>
# include <linux/bug.h>
# include <linux/kernel.h>
I
Ilya Dryomov 已提交
18 19
# include <linux/crush/crush.h>
# include <linux/crush/hash.h>
S
Sage Weil 已提交
20
#else
I
Ilya Dryomov 已提交
21 22 23
# include "crush_compat.h"
# include "crush.h"
# include "hash.h"
S
Sage Weil 已提交
24
#endif
25
#include "crush_ln_table.h"
S
Sage Weil 已提交
26

I
Ilya Dryomov 已提交
27 28
#define dprintk(args...) /* printf(args) */

S
Sage Weil 已提交
29 30 31 32 33 34 35 36 37 38 39
/*
 * 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 已提交
40
int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
S
Sage Weil 已提交
41
{
S
Sage Weil 已提交
42
	__u32 i;
S
Sage Weil 已提交
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 72 73 74 75

	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)
{
76 77
	unsigned int pr = r % bucket->size;
	unsigned int i, s;
S
Sage Weil 已提交
78 79

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

		/* optimize common r=0 case */
		if (pr == 0) {
86
			s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
S
Sage Weil 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
				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) {
108
		unsigned int p = bucket->perm_n;
S
Sage Weil 已提交
109 110
		/* no point in swapping the final entry */
		if (p < bucket->size - 1) {
111
			i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
S
Sage Weil 已提交
112 113
				(bucket->size - p);
			if (i) {
114
				unsigned int t = bucket->perm[p + i];
S
Sage Weil 已提交
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 142 143 144 145
				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--) {
I
Ilya Dryomov 已提交
146
		__u64 w = crush_hash32_4(bucket->h.hash, x, bucket->h.items[i],
147
					 r, bucket->h.id);
S
Sage Weil 已提交
148 149 150 151 152 153 154 155 156 157 158 159
		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];
	}

160 161
	dprintk("bad list sums for bucket %d\n", bucket->h.id);
	return bucket->h.items[0];
S
Sage Weil 已提交
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 192 193 194 195
}


/* (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)
{
196
	int n;
S
Sage Weil 已提交
197 198 199 200 201 202 203
	__u32 w;
	__u64 t;

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

	while (!terminal(n)) {
204
		int l;
S
Sage Weil 已提交
205 206
		/* pick point in [0, w) */
		w = bucket->node_weights[n];
207 208
		t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
					  bucket->h.id) * (__u64)w;
S
Sage Weil 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
		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 已提交
228
	__u32 i;
S
Sage Weil 已提交
229 230 231 232 233
	int high = 0;
	__u64 high_draw = 0;
	__u64 draw;

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

I
Ilya Dryomov 已提交
245 246
/* compute 2^44*log2(input+1) */
static __u64 crush_ln(unsigned int xin)
247
{
I
Ilya Dryomov 已提交
248 249 250
	unsigned int x = xin, x1;
	int iexpon, index1, index2;
	__u64 RH, LH, LL, xl64, result;
251

I
Ilya Dryomov 已提交
252
	x++;
253

I
Ilya Dryomov 已提交
254 255 256 257 258 259
	/* normalize input */
	iexpon = 15;
	while (!(x & 0x18000)) {
		x <<= 1;
		iexpon--;
	}
260

I
Ilya Dryomov 已提交
261 262 263 264 265
	index1 = (x >> 8) << 1;
	/* RH ~ 2^56/index1 */
	RH = __RH_LH_tbl[index1 - 256];
	/* LH ~ 2^48 * log2(index1/256) */
	LH = __RH_LH_tbl[index1 + 1 - 256];
266

I
Ilya Dryomov 已提交
267 268 269 270
	/* RH*x ~ 2^48 * (2^15 + xf), xf<2^8 */
	xl64 = (__s64)x * RH;
	xl64 >>= 48;
	x1 = xl64;
271

I
Ilya Dryomov 已提交
272 273
	result = iexpon;
	result <<= (12 + 32);
274

I
Ilya Dryomov 已提交
275 276 277
	index2 = x1 & 0xff;
	/* LL ~ 2^48*log2(1.0+index2/2^15) */
	LL = __LL_tbl[index2];
278

I
Ilya Dryomov 已提交
279
	LH = LH + LL;
280

I
Ilya Dryomov 已提交
281 282
	LH >>= (48 - 12 - 32);
	result += LH;
283

I
Ilya Dryomov 已提交
284
	return result;
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
}


/*
 * straw2
 *
 * for reference, see:
 *
 * http://en.wikipedia.org/wiki/Exponential_distribution#Distribution_of_the_minimum_of_exponential_random_variables
 *
 */

static int bucket_straw2_choose(struct crush_bucket_straw2 *bucket,
				int x, int r)
{
I
Ilya Dryomov 已提交
300 301 302
	unsigned int i, high = 0;
	unsigned int u;
	unsigned int w;
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
	__s64 ln, draw, high_draw = 0;

	for (i = 0; i < bucket->h.size; i++) {
		w = bucket->item_weights[i];
		if (w) {
			u = crush_hash32_3(bucket->h.hash, x,
					   bucket->h.items[i], r);
			u &= 0xffff;

			/*
			 * for some reason slightly less than 0x10000 produces
			 * a slightly more accurate distribution... probably a
			 * rounding effect.
			 *
			 * the natural log lookup table maps [0,0xffff]
			 * (corresponding to real numbers [1/0x10000, 1] to
			 * [0, 0xffffffffffff] (corresponding to real numbers
			 * [-11.090355,0]).
			 */
			ln = crush_ln(u) - 0x1000000000000ll;

			/*
			 * divide by 16.16 fixed-point weight.  note
			 * that the ln value is negative, so a larger
			 * weight means a larger (less negative) value
			 * for draw.
			 */
			draw = div64_s64(ln, w);
		} else {
			draw = S64_MIN;
		}

		if (i == 0 || draw > high_draw) {
			high = i;
			high_draw = draw;
		}
	}
	return bucket->h.items[high];
}


S
Sage Weil 已提交
344 345
static int crush_bucket_choose(struct crush_bucket *in, int x, int r)
{
346
	dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
347
	BUG_ON(in->size == 0);
S
Sage Weil 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360
	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);
361 362 363
	case CRUSH_BUCKET_STRAW2:
		return bucket_straw2_choose((struct crush_bucket_straw2 *)in,
					    x, r);
S
Sage Weil 已提交
364
	default:
365
		dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
S
Sage Weil 已提交
366
		return in->items[0];
S
Sage Weil 已提交
367 368 369
	}
}

370

S
Sage Weil 已提交
371 372 373 374
/*
 * true if device is marked "out" (failed, fully offloaded)
 * of the cluster
 */
375 376 377
static int is_out(const struct crush_map *map,
		  const __u32 *weight, int weight_max,
		  int item, int x)
S
Sage Weil 已提交
378
{
379 380
	if (item >= weight_max)
		return 1;
381
	if (weight[item] >= 0x10000)
S
Sage Weil 已提交
382 383 384
		return 0;
	if (weight[item] == 0)
		return 1;
385 386
	if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
	    < weight[item])
S
Sage Weil 已提交
387 388 389 390 391
		return 0;
	return 1;
}

/**
392
 * crush_choose_firstn - choose numrep distinct items of given type
S
Sage Weil 已提交
393 394 395 396 397 398 399
 * @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
400
 * @out_size: size of the out vector
401 402
 * @tries: number of attempts to make
 * @recurse_tries: number of attempts to have recursive chooseleaf make
403 404
 * @local_retries: localized retries
 * @local_fallback_retries: localized fallback retries
405
 * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose)
406
 * @vary_r: pass r to recursive calls
S
Sage Weil 已提交
407
 * @out2: second output vector for leaf items (if @recurse_to_leaf)
408
 * @parent_r: r value passed from the parent
S
Sage Weil 已提交
409
 */
410 411 412 413 414
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,
415
			       int out_size,
I
Ilya Dryomov 已提交
416 417
			       unsigned int tries,
			       unsigned int recurse_tries,
418 419
			       unsigned int local_retries,
			       unsigned int local_fallback_retries,
420
			       int recurse_to_leaf,
421 422 423
			       unsigned int vary_r,
			       int *out2,
			       int parent_r)
S
Sage Weil 已提交
424 425
{
	int rep;
S
Sage Weil 已提交
426
	unsigned int ftotal, flocal;
S
Sage Weil 已提交
427 428 429 430
	int retry_descent, retry_bucket, skip_rep;
	struct crush_bucket *in = bucket;
	int r;
	int i;
431
	int item = 0;
S
Sage Weil 已提交
432 433
	int itemtype;
	int collide, reject;
434
	int count = out_size;
435

436 437 438 439 440
	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",
		recurse_to_leaf ? "_LEAF" : "",
		bucket->id, x, outpos, numrep,
		tries, recurse_tries, local_retries, local_fallback_retries,
		parent_r);
S
Sage Weil 已提交
441

442
	for (rep = outpos; rep < numrep && count > 0 ; rep++) {
S
Sage Weil 已提交
443 444 445 446 447 448 449 450 451 452
		/* 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 {
453
				collide = 0;
S
Sage Weil 已提交
454
				retry_bucket = 0;
455
				r = rep + parent_r;
456 457
				/* r' = r + f_total */
				r += ftotal;
S
Sage Weil 已提交
458 459

				/* bucket choose */
460 461 462 463
				if (in->size == 0) {
					reject = 1;
					goto reject;
				}
464
				if (local_fallback_retries > 0 &&
S
Sage Weil 已提交
465
				    flocal >= (in->size>>1) &&
466
				    flocal > local_fallback_retries)
S
Sage Weil 已提交
467 468 469
					item = bucket_perm_choose(in, x, r);
				else
					item = crush_bucket_choose(in, x, r);
470 471 472 473 474
				if (item >= map->max_devices) {
					dprintk("   bad item %d\n", item);
					skip_rep = 1;
					break;
				}
S
Sage Weil 已提交
475 476 477 478 479 480 481 482 483 484

				/* 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) {
485 486 487 488 489 490
					if (item >= 0 ||
					    (-1-item) >= map->max_buckets) {
						dprintk("   bad item type %d\n", type);
						skip_rep = 1;
						break;
					}
S
Sage Weil 已提交
491
					in = map->buckets[-1-item];
S
Sage Weil 已提交
492
					retry_bucket = 1;
S
Sage Weil 已提交
493 494 495 496 497 498 499 500 501 502 503
					continue;
				}

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

504
				reject = 0;
505
				if (!collide && recurse_to_leaf) {
506
					if (item < 0) {
507 508 509 510 511
						int sub_r;
						if (vary_r)
							sub_r = r >> (vary_r-1);
						else
							sub_r = 0;
512
						if (crush_choose_firstn(map,
513
							 map->buckets[-1-item],
514
							 weight, weight_max,
515
							 x, outpos+1, 0,
516
							 out2, outpos, count,
I
Ilya Dryomov 已提交
517
							 recurse_tries, 0,
518 519
							 local_retries,
							 local_fallback_retries,
520
							 0,
521 522 523
							 vary_r,
							 NULL,
							 sub_r) <= outpos)
524 525 526 527 528 529 530 531 532
							/* didn't get leaf */
							reject = 1;
					} else {
						/* we already have a leaf! */
						out2[outpos] = item;
					}
				}

				if (!reject) {
S
Sage Weil 已提交
533 534 535
					/* out? */
					if (itemtype == 0)
						reject = is_out(map, weight,
536
								weight_max,
S
Sage Weil 已提交
537 538 539 540 541
								item, x);
					else
						reject = 0;
				}

542
reject:
S
Sage Weil 已提交
543 544 545 546
				if (reject || collide) {
					ftotal++;
					flocal++;

547
					if (collide && flocal <= local_retries)
S
Sage Weil 已提交
548 549
						/* retry locally a few times */
						retry_bucket = 1;
550 551
					else if (local_fallback_retries > 0 &&
						 flocal <= in->size + local_fallback_retries)
S
Sage Weil 已提交
552 553
						/* exhaustive bucket search */
						retry_bucket = 1;
554
					else if (ftotal < tries)
S
Sage Weil 已提交
555 556 557 558 559 560
						/* then retry descent */
						retry_descent = 1;
					else
						/* else give up */
						skip_rep = 1;
					dprintk("  reject %d  collide %d  "
S
Sage Weil 已提交
561
						"ftotal %u  flocal %u\n",
S
Sage Weil 已提交
562 563 564 565 566 567 568
						reject, collide, ftotal,
						flocal);
				}
			} while (retry_bucket);
		} while (retry_descent);

		if (skip_rep) {
569 570
			dprintk("skip rep\n");
			continue;
S
Sage Weil 已提交
571 572
		}

573
		dprintk("CHOOSE got %d\n", item);
S
Sage Weil 已提交
574 575
		out[outpos] = item;
		outpos++;
576
		count--;
I
Ilya Dryomov 已提交
577 578 579 580
#ifndef __KERNEL__
		if (map->choose_tries && ftotal <= map->choose_total_tries)
			map->choose_tries[ftotal]++;
#endif
S
Sage Weil 已提交
581 582
	}

583
	dprintk("CHOOSE returns %d\n", outpos);
S
Sage Weil 已提交
584 585 586 587
	return outpos;
}


588
/**
589
 * crush_choose_indep: alternative breadth-first positionally stable mapping
590 591 592
 *
 */
static void crush_choose_indep(const struct crush_map *map,
593 594
			       struct crush_bucket *bucket,
			       const __u32 *weight, int weight_max,
I
Ilya Dryomov 已提交
595
			       int x, int left, int numrep, int type,
596
			       int *out, int outpos,
I
Ilya Dryomov 已提交
597 598
			       unsigned int tries,
			       unsigned int recurse_tries,
599
			       int recurse_to_leaf,
600 601
			       int *out2,
			       int parent_r)
602 603
{
	struct crush_bucket *in = bucket;
I
Ilya Dryomov 已提交
604
	int endpos = outpos + left;
605 606 607 608 609 610 611 612 613 614 615 616
	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 已提交
617
	for (rep = outpos; rep < endpos; rep++) {
618 619 620 621 622
		out[rep] = CRUSH_ITEM_UNDEF;
		if (out2)
			out2[rep] = CRUSH_ITEM_UNDEF;
	}

I
Ilya Dryomov 已提交
623
	for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) {
I
Ilya Dryomov 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636 637
#ifdef DEBUG_INDEP
		if (out2 && ftotal) {
			dprintk("%u %d a: ", ftotal, left);
			for (rep = outpos; rep < endpos; rep++) {
				dprintk(" %d", out[rep]);
			}
			dprintk("\n");
			dprintk("%u %d b: ", ftotal, left);
			for (rep = outpos; rep < endpos; rep++) {
				dprintk(" %d", out2[rep]);
			}
			dprintk("\n");
		}
#endif
I
Ilya Dryomov 已提交
638
		for (rep = outpos; rep < endpos; rep++) {
639 640 641 642 643 644 645
			if (out[rep] != CRUSH_ITEM_UNDEF)
				continue;

			in = bucket;  /* initial bucket */

			/* choose through intervening buckets */
			for (;;) {
646 647 648 649 650 651 652 653
				/* 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.
				 */
654
				r = rep + parent_r;
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705

				/* 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 已提交
706
				for (i = outpos; i < endpos; i++) {
707 708 709 710 711 712 713 714 715 716 717
					if (out[i] == item) {
						collide = 1;
						break;
					}
				}
				if (collide)
					break;

				if (recurse_to_leaf) {
					if (item < 0) {
						crush_choose_indep(map,
718 719 720 721
						   map->buckets[-1-item],
						   weight, weight_max,
						   x, 1, numrep, 0,
						   out2, rep,
I
Ilya Dryomov 已提交
722
						   recurse_tries, 0,
723
						   0, NULL, r);
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
						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 已提交
746
	for (rep = outpos; rep < endpos; rep++) {
747 748 749 750 751 752 753
		if (out[rep] == CRUSH_ITEM_UNDEF) {
			out[rep] = CRUSH_ITEM_NONE;
		}
		if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) {
			out2[rep] = CRUSH_ITEM_NONE;
		}
	}
I
Ilya Dryomov 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
#ifndef __KERNEL__
	if (map->choose_tries && ftotal <= map->choose_total_tries)
		map->choose_tries[ftotal]++;
#endif
#ifdef DEBUG_INDEP
	if (out2) {
		dprintk("%u %d a: ", ftotal, left);
		for (rep = outpos; rep < endpos; rep++) {
			dprintk(" %d", out[rep]);
		}
		dprintk("\n");
		dprintk("%u %d b: ", ftotal, left);
		for (rep = outpos; rep < endpos; rep++) {
			dprintk(" %d", out2[rep]);
		}
		dprintk("\n");
	}
#endif
772 773
}

S
Sage Weil 已提交
774 775 776 777 778 779 780
/**
 * 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
781 782
 * @weight: weight vector (for map leaves)
 * @weight_max: size of weight vector
783
 * @scratch: scratch vector for private use; must be >= 3 * result_max
S
Sage Weil 已提交
784
 */
S
Sage Weil 已提交
785
int crush_do_rule(const struct crush_map *map,
S
Sage Weil 已提交
786
		  int ruleno, int x, int *result, int result_max,
787 788
		  const __u32 *weight, int weight_max,
		  int *scratch)
S
Sage Weil 已提交
789 790
{
	int result_len;
791 792 793
	int *a = scratch;
	int *b = scratch + result_max;
	int *c = scratch + result_max*2;
S
Sage Weil 已提交
794 795 796 797 798 799 800
	int recurse_to_leaf;
	int *w;
	int wsize = 0;
	int *o;
	int osize;
	int *tmp;
	struct crush_rule *rule;
S
Sage Weil 已提交
801
	__u32 step;
S
Sage Weil 已提交
802 803
	int i, j;
	int numrep;
804
	int out_size;
805 806 807 808 809
	/*
	 * 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;
810
	int choose_leaf_tries = 0;
811 812 813 814 815 816
	/*
	 * 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 已提交
817

818 819
	int vary_r = map->chooseleaf_vary_r;

820 821 822 823
	if ((__u32)ruleno >= map->max_rules) {
		dprintk(" bad ruleno %d\n", ruleno);
		return 0;
	}
S
Sage Weil 已提交
824 825 826 827 828 829 830

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

	for (step = 0; step < rule->len; step++) {
831
		int firstn = 0;
832 833 834
		struct crush_rule_step *curstep = &rule->steps[step];

		switch (curstep->op) {
S
Sage Weil 已提交
835
		case CRUSH_RULE_TAKE:
836 837
			if ((curstep->arg1 >= 0 &&
			     curstep->arg1 < map->max_devices) ||
838 839
			    (-1-curstep->arg1 >= 0 &&
			     -1-curstep->arg1 < map->max_buckets &&
840 841 842 843 844 845
			     map->buckets[-1-curstep->arg1])) {
				w[0] = curstep->arg1;
				wsize = 1;
			} else {
				dprintk(" bad take value %d\n", curstep->arg1);
			}
S
Sage Weil 已提交
846 847
			break;

848 849 850 851 852
		case CRUSH_RULE_SET_CHOOSE_TRIES:
			if (curstep->arg1 > 0)
				choose_tries = curstep->arg1;
			break;

853
		case CRUSH_RULE_SET_CHOOSELEAF_TRIES:
854 855 856 857
			if (curstep->arg1 > 0)
				choose_leaf_tries = curstep->arg1;
			break;

858
		case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES:
859
			if (curstep->arg1 >= 0)
860
				choose_local_retries = curstep->arg1;
861 862 863
			break;

		case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES:
864
			if (curstep->arg1 >= 0)
865
				choose_local_fallback_retries = curstep->arg1;
866
			break;
867 868 869 870 871

		case CRUSH_RULE_SET_CHOOSELEAF_VARY_R:
			if (curstep->arg1 >= 0)
				vary_r = curstep->arg1;
			break;
872

873
		case CRUSH_RULE_CHOOSELEAF_FIRSTN:
S
Sage Weil 已提交
874 875
		case CRUSH_RULE_CHOOSE_FIRSTN:
			firstn = 1;
876
			/* fall through */
877
		case CRUSH_RULE_CHOOSELEAF_INDEP:
S
Sage Weil 已提交
878
		case CRUSH_RULE_CHOOSE_INDEP:
879 880
			if (wsize == 0)
				break;
S
Sage Weil 已提交
881 882

			recurse_to_leaf =
883
				curstep->op ==
884
				 CRUSH_RULE_CHOOSELEAF_FIRSTN ||
885
				curstep->op ==
886
				CRUSH_RULE_CHOOSELEAF_INDEP;
S
Sage Weil 已提交
887 888 889 890 891

			/* reset output */
			osize = 0;

			for (i = 0; i < wsize; i++) {
892
				int bno;
S
Sage Weil 已提交
893 894 895 896 897
				/*
				 * see CRUSH_N, CRUSH_N_MINUS macros.
				 * basically, numrep <= 0 means relative to
				 * the provided result_max
				 */
898
				numrep = curstep->arg1;
S
Sage Weil 已提交
899 900 901 902 903 904
				if (numrep <= 0) {
					numrep += result_max;
					if (numrep <= 0)
						continue;
				}
				j = 0;
905 906 907 908 909 910 911
				/* make sure bucket id is valid */
				bno = -1 - w[i];
				if (bno < 0 || bno >= map->max_buckets) {
					/* w[i] is probably CRUSH_ITEM_NONE */
					dprintk("  bad w[i] %d\n", w[i]);
					continue;
				}
912
				if (firstn) {
I
Ilya Dryomov 已提交
913 914 915 916 917 918 919 920
					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;
921 922
					osize += crush_choose_firstn(
						map,
923
						map->buckets[bno],
924 925 926 927
						weight, weight_max,
						x, numrep,
						curstep->arg2,
						o+osize, j,
928
						result_max-osize,
929
						choose_tries,
I
Ilya Dryomov 已提交
930
						recurse_tries,
931 932
						choose_local_retries,
						choose_local_fallback_retries,
933
						recurse_to_leaf,
934 935 936
						vary_r,
						c+osize,
						0);
937
				} else {
938
					out_size = ((numrep < (result_max-osize)) ?
I
Ilya Dryomov 已提交
939
						    numrep : (result_max-osize));
940 941
					crush_choose_indep(
						map,
942
						map->buckets[bno],
943
						weight, weight_max,
944
						x, out_size, numrep,
945 946
						curstep->arg2,
						o+osize, j,
947
						choose_tries,
I
Ilya Dryomov 已提交
948 949
						choose_leaf_tries ?
						   choose_leaf_tries : 1,
950
						recurse_to_leaf,
951 952
						c+osize,
						0);
953
					osize += out_size;
954
				}
S
Sage Weil 已提交
955 956 957 958 959 960
			}

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

I
Ilya Dryomov 已提交
961
			/* swap o and w arrays */
S
Sage Weil 已提交
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
			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:
978 979 980
			dprintk(" unknown op %d at step %d\n",
				curstep->op, step);
			break;
S
Sage Weil 已提交
981 982
		}
	}
983
	return result_len;
S
Sage Weil 已提交
984
}