mapper.c 14.0 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>
S
Sage Weil 已提交
23 24 25 26 27 28 29 30 31 32 33 34

/*
 * 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 已提交
35
int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
S
Sage Weil 已提交
36
{
S
Sage Weil 已提交
37
	__u32 i;
S
Sage Weil 已提交
38 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 72 73 74

	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)
{
	unsigned pr = r % bucket->size;
	unsigned i, s;

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

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

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


/* (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)
{
	int n, l;
	__u32 w;
	__u64 t;

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

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

	for (i = 0; i < bucket->h.size; i++) {
228
		draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
S
Sage Weil 已提交
229 230 231 232 233 234 235 236 237 238 239 240
		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)
{
241
	dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
242
	BUG_ON(in->size == 0);
S
Sage Weil 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256
	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:
257
		dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
S
Sage Weil 已提交
258
		return in->items[0];
S
Sage Weil 已提交
259 260 261 262 263 264 265
	}
}

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

/**
 * crush_choose - choose numrep distinct items of given type
 * @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
 * @firstn: true if choosing "first n" items, false if choosing "indep"
 * @recurse_to_leaf: true if we want one device under each item of given type
 * @out2: second output vector for leaf items (if @recurse_to_leaf)
 */
S
Sage Weil 已提交
291
static int crush_choose(const struct crush_map *map,
S
Sage Weil 已提交
292
			struct crush_bucket *bucket,
S
Sage Weil 已提交
293
			const __u32 *weight,
S
Sage Weil 已提交
294 295 296 297 298 299
			int x, int numrep, int type,
			int *out, int outpos,
			int firstn, int recurse_to_leaf,
			int *out2)
{
	int rep;
S
Sage Weil 已提交
300
	unsigned int ftotal, flocal;
S
Sage Weil 已提交
301 302 303 304
	int retry_descent, retry_bucket, skip_rep;
	struct crush_bucket *in = bucket;
	int r;
	int i;
305
	int item = 0;
S
Sage Weil 已提交
306 307
	int itemtype;
	int collide, reject;
S
Sage Weil 已提交
308
	const unsigned int orig_tries = 5; /* attempts before we fall back to search */
309 310 311

	dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
		bucket->id, x, outpos, numrep);
S
Sage Weil 已提交
312 313 314 315 316 317 318 319 320 321 322 323

	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 {
324
				collide = 0;
S
Sage Weil 已提交
325 326 327 328
				retry_bucket = 0;
				r = rep;
				if (in->alg == CRUSH_BUCKET_UNIFORM) {
					/* be careful */
S
Sage Weil 已提交
329
					if (firstn || (__u32)numrep >= in->size)
S
Sage Weil 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
						/* r' = r + f_total */
						r += ftotal;
					else if (in->size % numrep == 0)
						/* r'=r+(n+1)*f_local */
						r += (numrep+1) *
							(flocal+ftotal);
					else
						/* r' = r + n*f_local */
						r += numrep * (flocal+ftotal);
				} else {
					if (firstn)
						/* r' = r + f_total */
						r += ftotal;
					else
						/* r' = r + n*f_local */
						r += numrep * (flocal+ftotal);
				}

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

				/* 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) {
373 374 375 376 377 378
					if (item >= 0 ||
					    (-1-item) >= map->max_buckets) {
						dprintk("   bad item type %d\n", type);
						skip_rep = 1;
						break;
					}
S
Sage Weil 已提交
379
					in = map->buckets[-1-item];
S
Sage Weil 已提交
380
					retry_bucket = 1;
S
Sage Weil 已提交
381 382 383 384 385 386 387 388 389 390 391
					continue;
				}

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

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
				reject = 0;
				if (recurse_to_leaf) {
					if (item < 0) {
						if (crush_choose(map,
							 map->buckets[-1-item],
							 weight,
							 x, outpos+1, 0,
							 out2, outpos,
							 firstn, 0,
							 NULL) <= outpos)
							/* didn't get leaf */
							reject = 1;
					} else {
						/* we already have a leaf! */
						out2[outpos] = item;
					}
				}

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

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

					if (collide && flocal < 3)
						/* retry locally a few times */
						retry_bucket = 1;
S
Sage Weil 已提交
427
					else if (flocal <= in->size + orig_tries)
S
Sage Weil 已提交
428 429 430 431 432 433 434 435 436
						/* exhaustive bucket search */
						retry_bucket = 1;
					else if (ftotal < 20)
						/* 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 445 446 447 448
						reject, collide, ftotal,
						flocal);
				}
			} while (retry_bucket);
		} while (retry_descent);

		if (skip_rep) {
			dprintk("skip rep\n");
			continue;
		}

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 459 460 461 462 463 464 465 466 467
	return outpos;
}


/**
 * 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
 * @force: force initial replica choice; -1 for none
 */
S
Sage Weil 已提交
468
int crush_do_rule(const struct crush_map *map,
S
Sage Weil 已提交
469
		  int ruleno, int x, int *result, int result_max,
S
Sage Weil 已提交
470
		  int force, const __u32 *weight)
S
Sage Weil 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484
{
	int result_len;
	int force_context[CRUSH_MAX_DEPTH];
	int force_pos = -1;
	int a[CRUSH_MAX_SET];
	int b[CRUSH_MAX_SET];
	int c[CRUSH_MAX_SET];
	int recurse_to_leaf;
	int *w;
	int wsize = 0;
	int *o;
	int osize;
	int *tmp;
	struct crush_rule *rule;
S
Sage Weil 已提交
485
	__u32 step;
S
Sage Weil 已提交
486 487 488 489
	int i, j;
	int numrep;
	int firstn;

490 491 492 493
	if ((__u32)ruleno >= map->max_rules) {
		dprintk(" bad ruleno %d\n", ruleno);
		return 0;
	}
S
Sage Weil 已提交
494 495 496 497 498 499 500 501 502

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

	/*
	 * determine hierarchical context of force, if any.  note
	 * that this may or may not correspond to the specific types
S
Sage Weil 已提交
503 504
	 * referenced by the crush rule.  it will also only affect
	 * the first descent (TAKE).
S
Sage Weil 已提交
505
	 */
506 507 508 509 510 511 512 513 514 515 516 517
	if (force >= 0 &&
	    force < map->max_devices &&
	    map->device_parents[force] != 0 &&
	    !is_out(map, weight, force, x)) {
		while (1) {
			force_context[++force_pos] = force;
			if (force >= 0)
				force = map->device_parents[force];
			else
				force = map->bucket_parents[-1-force];
			if (force == 0)
				break;
S
Sage Weil 已提交
518 519 520 521
		}
	}

	for (step = 0; step < rule->len; step++) {
522 523
		struct crush_rule_step *curstep = &rule->steps[step];

S
Sage Weil 已提交
524
		firstn = 0;
525
		switch (curstep->op) {
S
Sage Weil 已提交
526
		case CRUSH_RULE_TAKE:
527
			w[0] = curstep->arg1;
S
Sage Weil 已提交
528 529

			/* find position in force_context/hierarchy */
530
			while (force_pos >= 0 && force_context[force_pos] != w[0])
S
Sage Weil 已提交
531
				force_pos--;
S
Sage Weil 已提交
532 533 534 535
			/* and move past it */
			if (force_pos >= 0)
				force_pos--;

S
Sage Weil 已提交
536 537 538 539 540 541
			wsize = 1;
			break;

		case CRUSH_RULE_CHOOSE_LEAF_FIRSTN:
		case CRUSH_RULE_CHOOSE_FIRSTN:
			firstn = 1;
542
			/* fall through */
S
Sage Weil 已提交
543 544
		case CRUSH_RULE_CHOOSE_LEAF_INDEP:
		case CRUSH_RULE_CHOOSE_INDEP:
545 546
			if (wsize == 0)
				break;
S
Sage Weil 已提交
547 548

			recurse_to_leaf =
549
				curstep->op ==
S
Sage Weil 已提交
550
				 CRUSH_RULE_CHOOSE_LEAF_FIRSTN ||
551
				curstep->op ==
S
Sage Weil 已提交
552 553 554 555 556 557 558 559 560 561 562
				CRUSH_RULE_CHOOSE_LEAF_INDEP;

			/* 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
				 */
563
				numrep = curstep->arg1;
S
Sage Weil 已提交
564 565 566 567 568 569 570 571 572 573
				if (numrep <= 0) {
					numrep += result_max;
					if (numrep <= 0)
						continue;
				}
				j = 0;
				if (osize == 0 && force_pos >= 0) {
					/* skip any intermediate types */
					while (force_pos &&
					       force_context[force_pos] < 0 &&
574
					       curstep->arg2 !=
S
Sage Weil 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587
					       map->buckets[-1 -
					       force_context[force_pos]]->type)
						force_pos--;
					o[osize] = force_context[force_pos];
					if (recurse_to_leaf)
						c[osize] = force_context[0];
					j++;
					force_pos--;
				}
				osize += crush_choose(map,
						      map->buckets[-1-w[i]],
						      weight,
						      x, numrep,
588
						      curstep->arg2,
S
Sage Weil 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
						      o+osize, j,
						      firstn,
						      recurse_to_leaf, c+osize);
			}

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

			/* swap t and w arrays */
			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:
615 616 617
			dprintk(" unknown op %d at step %d\n",
				curstep->op, step);
			break;
S
Sage Weil 已提交
618 619
		}
	}
620
	return result_len;
S
Sage Weil 已提交
621 622 623
}