blob: 91c41fe8311317fc2bf65a970be816c77f8b5a1a [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>
Sage Weil5ecc0a02009-10-06 11:31:11 -070023
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 */
Sage Weil8b12d472012-05-07 15:38:35 -070035int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
Sage Weil5ecc0a02009-10-06 11:31:11 -070036{
Sage Weil8b12d472012-05-07 15:38:35 -070037 __u32 i;
Sage Weil5ecc0a02009-10-06 11:31:11 -070038
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 */
68static int bucket_perm_choose(struct crush_bucket *bucket,
69 int x, int r)
70{
Eric Dumazet95c96172012-04-15 05:58:06 +000071 unsigned int pr = r % bucket->size;
72 unsigned int i, s;
Sage Weil5ecc0a02009-10-06 11:31:11 -070073
74 /* start a new permutation if @x has changed */
Sage Weil8b12d472012-05-07 15:38:35 -070075 if (bucket->perm_x != (__u32)x || bucket->perm_n == 0) {
Sage Weil5ecc0a02009-10-06 11:31:11 -070076 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) {
Sage Weilfb690392009-11-07 20:18:22 -080081 s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
Sage Weil5ecc0a02009-10-06 11:31:11 -070082 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) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000103 unsigned int p = bucket->perm_n;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700104 /* no point in swapping the final entry */
105 if (p < bucket->size - 1) {
Sage Weilfb690392009-11-07 20:18:22 -0800106 i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
Sage Weil5ecc0a02009-10-06 11:31:11 -0700107 (bucket->size - p);
108 if (i) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000109 unsigned int t = bucket->perm[p + i];
Sage Weil5ecc0a02009-10-06 11:31:11 -0700110 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];
121out:
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 */
128static 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 */
135static 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--) {
Sage Weilfb690392009-11-07 20:18:22 -0800141 __u64 w = crush_hash32_4(bucket->h.hash,x, bucket->h.items[i],
142 r, bucket->h.id);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700143 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
Sage Weila1f48952012-05-07 15:35:24 -0700155 dprintk("bad list sums for bucket %d\n", bucket->h.id);
156 return bucket->h.items[0];
Sage Weil5ecc0a02009-10-06 11:31:11 -0700157}
158
159
160/* (binary) tree */
161static 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
171static int left(int x)
172{
173 int h = height(x);
174 return x - (1 << (h-1));
175}
176
177static int right(int x)
178{
179 int h = height(x);
180 return x + (1 << (h-1));
181}
182
183static int terminal(int x)
184{
185 return x & 1;
186}
187
188static int bucket_tree_choose(struct crush_bucket_tree *bucket,
189 int x, int r)
190{
Ilya Dryomov8f99c852013-12-24 21:19:24 +0200191 int n;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700192 __u32 w;
193 __u64 t;
194
195 /* start at root */
196 n = bucket->num_nodes >> 1;
197
198 while (!terminal(n)) {
Ilya Dryomov8f99c852013-12-24 21:19:24 +0200199 int l;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700200 /* 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/**
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200284 * crush_choose_firstn - choose numrep distinct items of given type
Sage Weil5ecc0a02009-10-06 11:31:11 -0700285 * @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
Ilya Dryomov45002262015-04-14 16:04:23 +0300292 * @out_size: size of the out vector
Ilya Dryomov0e32d712013-12-24 21:19:27 +0200293 * @tries: number of attempts to make
294 * @recurse_tries: number of attempts to have recursive chooseleaf make
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200295 * @local_retries: localized retries
296 * @local_fallback_retries: localized fallback retries
Ilya Dryomov0e32d712013-12-24 21:19:27 +0200297 * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose)
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200298 * @vary_r: pass r to recursive calls
Sage Weil5ecc0a02009-10-06 11:31:11 -0700299 * @out2: second output vector for leaf items (if @recurse_to_leaf)
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200300 * @parent_r: r value passed from the parent
Sage Weil5ecc0a02009-10-06 11:31:11 -0700301 */
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200302static int crush_choose_firstn(const struct crush_map *map,
303 struct crush_bucket *bucket,
304 const __u32 *weight, int weight_max,
305 int x, int numrep, int type,
306 int *out, int outpos,
Ilya Dryomov45002262015-04-14 16:04:23 +0300307 int out_size,
Ilya Dryomov2d8be0b2013-12-24 21:19:27 +0200308 unsigned int tries,
309 unsigned int recurse_tries,
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200310 unsigned int local_retries,
311 unsigned int local_fallback_retries,
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200312 int recurse_to_leaf,
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200313 unsigned int vary_r,
314 int *out2,
315 int parent_r)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700316{
317 int rep;
Sage Weil8b12d472012-05-07 15:38:35 -0700318 unsigned int ftotal, flocal;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700319 int retry_descent, retry_bucket, skip_rep;
320 struct crush_bucket *in = bucket;
321 int r;
322 int i;
Sage Weilb28813a2009-10-07 10:59:34 -0700323 int item = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700324 int itemtype;
325 int collide, reject;
Ilya Dryomov45002262015-04-14 16:04:23 +0300326 int count = out_size;
Sage Weila1a31e72010-06-24 12:58:14 -0700327
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200328 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",
329 recurse_to_leaf ? "_LEAF" : "",
330 bucket->id, x, outpos, numrep,
331 tries, recurse_tries, local_retries, local_fallback_retries,
332 parent_r);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700333
Ilya Dryomov45002262015-04-14 16:04:23 +0300334 for (rep = outpos; rep < numrep && count > 0 ; rep++) {
Sage Weil5ecc0a02009-10-06 11:31:11 -0700335 /* keep trying until we get a non-out, non-colliding item */
336 ftotal = 0;
337 skip_rep = 0;
338 do {
339 retry_descent = 0;
340 in = bucket; /* initial bucket */
341
342 /* choose through intervening buckets */
343 flocal = 0;
344 do {
Sage Weilb28813a2009-10-07 10:59:34 -0700345 collide = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700346 retry_bucket = 0;
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200347 r = rep + parent_r;
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200348 /* r' = r + f_total */
349 r += ftotal;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700350
351 /* bucket choose */
Sage Weilb28813a2009-10-07 10:59:34 -0700352 if (in->size == 0) {
353 reject = 1;
354 goto reject;
355 }
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200356 if (local_fallback_retries > 0 &&
Sage Weil546f04e2012-07-30 18:15:23 -0700357 flocal >= (in->size>>1) &&
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200358 flocal > local_fallback_retries)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700359 item = bucket_perm_choose(in, x, r);
360 else
361 item = crush_bucket_choose(in, x, r);
Sage Weila1f48952012-05-07 15:35:24 -0700362 if (item >= map->max_devices) {
363 dprintk(" bad item %d\n", item);
364 skip_rep = 1;
365 break;
366 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700367
368 /* desired type? */
369 if (item < 0)
370 itemtype = map->buckets[-1-item]->type;
371 else
372 itemtype = 0;
373 dprintk(" item %d type %d\n", item, itemtype);
374
375 /* keep going? */
376 if (itemtype != type) {
Sage Weila1f48952012-05-07 15:35:24 -0700377 if (item >= 0 ||
378 (-1-item) >= map->max_buckets) {
379 dprintk(" bad item type %d\n", type);
380 skip_rep = 1;
381 break;
382 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700383 in = map->buckets[-1-item];
Sage Weil55bda7a2010-06-24 12:55:48 -0700384 retry_bucket = 1;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700385 continue;
386 }
387
388 /* collision? */
Sage Weil5ecc0a02009-10-06 11:31:11 -0700389 for (i = 0; i < outpos; i++) {
390 if (out[i] == item) {
391 collide = 1;
392 break;
393 }
394 }
395
Sage Weila1a31e72010-06-24 12:58:14 -0700396 reject = 0;
Sage Weil7d7c1f62013-01-15 18:49:09 -0800397 if (!collide && recurse_to_leaf) {
Sage Weila1a31e72010-06-24 12:58:14 -0700398 if (item < 0) {
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200399 int sub_r;
400 if (vary_r)
401 sub_r = r >> (vary_r-1);
402 else
403 sub_r = 0;
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200404 if (crush_choose_firstn(map,
Sage Weila1a31e72010-06-24 12:58:14 -0700405 map->buckets[-1-item],
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200406 weight, weight_max,
Sage Weila1a31e72010-06-24 12:58:14 -0700407 x, outpos+1, 0,
Ilya Dryomov45002262015-04-14 16:04:23 +0300408 out2, outpos, count,
Ilya Dryomov2d8be0b2013-12-24 21:19:27 +0200409 recurse_tries, 0,
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200410 local_retries,
411 local_fallback_retries,
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200412 0,
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200413 vary_r,
414 NULL,
415 sub_r) <= outpos)
Sage Weila1a31e72010-06-24 12:58:14 -0700416 /* didn't get leaf */
417 reject = 1;
418 } else {
419 /* we already have a leaf! */
420 out2[outpos] = item;
421 }
422 }
423
424 if (!reject) {
Sage Weil5ecc0a02009-10-06 11:31:11 -0700425 /* out? */
426 if (itemtype == 0)
427 reject = is_out(map, weight,
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200428 weight_max,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700429 item, x);
430 else
431 reject = 0;
432 }
433
Sage Weilb28813a2009-10-07 10:59:34 -0700434reject:
Sage Weil5ecc0a02009-10-06 11:31:11 -0700435 if (reject || collide) {
436 ftotal++;
437 flocal++;
438
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200439 if (collide && flocal <= local_retries)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700440 /* retry locally a few times */
441 retry_bucket = 1;
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200442 else if (local_fallback_retries > 0 &&
443 flocal <= in->size + local_fallback_retries)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700444 /* exhaustive bucket search */
445 retry_bucket = 1;
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200446 else if (ftotal < tries)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700447 /* then retry descent */
448 retry_descent = 1;
449 else
450 /* else give up */
451 skip_rep = 1;
452 dprintk(" reject %d collide %d "
Sage Weil8b12d472012-05-07 15:38:35 -0700453 "ftotal %u flocal %u\n",
Sage Weil5ecc0a02009-10-06 11:31:11 -0700454 reject, collide, ftotal,
455 flocal);
456 }
457 } while (retry_bucket);
458 } while (retry_descent);
459
460 if (skip_rep) {
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200461 dprintk("skip rep\n");
462 continue;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700463 }
464
Sage Weila1a31e72010-06-24 12:58:14 -0700465 dprintk("CHOOSE got %d\n", item);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700466 out[outpos] = item;
467 outpos++;
Ilya Dryomov45002262015-04-14 16:04:23 +0300468 count--;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700469 }
470
Sage Weila1a31e72010-06-24 12:58:14 -0700471 dprintk("CHOOSE returns %d\n", outpos);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700472 return outpos;
473}
474
475
476/**
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200477 * crush_choose_indep: alternative breadth-first positionally stable mapping
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200478 *
479 */
480static void crush_choose_indep(const struct crush_map *map,
Ilya Dryomovbe3226a2013-12-24 21:19:26 +0200481 struct crush_bucket *bucket,
482 const __u32 *weight, int weight_max,
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200483 int x, int left, int numrep, int type,
Ilya Dryomovbe3226a2013-12-24 21:19:26 +0200484 int *out, int outpos,
Ilya Dryomov2d8be0b2013-12-24 21:19:27 +0200485 unsigned int tries,
486 unsigned int recurse_tries,
Ilya Dryomovbe3226a2013-12-24 21:19:26 +0200487 int recurse_to_leaf,
Ilya Dryomov41586082013-12-24 21:19:25 +0200488 int *out2,
489 int parent_r)
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200490{
491 struct crush_bucket *in = bucket;
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200492 int endpos = outpos + left;
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200493 int rep;
494 unsigned int ftotal;
495 int r;
496 int i;
497 int item = 0;
498 int itemtype;
499 int collide;
500
501 dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
502 bucket->id, x, outpos, numrep);
503
504 /* initially my result is undefined */
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200505 for (rep = outpos; rep < endpos; rep++) {
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200506 out[rep] = CRUSH_ITEM_UNDEF;
507 if (out2)
508 out2[rep] = CRUSH_ITEM_UNDEF;
509 }
510
Ilya Dryomov2d8be0b2013-12-24 21:19:27 +0200511 for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) {
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200512 for (rep = outpos; rep < endpos; rep++) {
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200513 if (out[rep] != CRUSH_ITEM_UNDEF)
514 continue;
515
516 in = bucket; /* initial bucket */
517
518 /* choose through intervening buckets */
519 for (;;) {
Ilya Dryomov3102b0a2013-12-24 21:19:25 +0200520 /* note: we base the choice on the position
521 * even in the nested call. that means that
522 * if the first layer chooses the same bucket
523 * in a different position, we will tend to
524 * choose a different item in that bucket.
525 * this will involve more devices in data
526 * movement and tend to distribute the load.
527 */
Ilya Dryomov41586082013-12-24 21:19:25 +0200528 r = rep + parent_r;
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200529
530 /* be careful */
531 if (in->alg == CRUSH_BUCKET_UNIFORM &&
532 in->size % numrep == 0)
533 /* r'=r+(n+1)*f_total */
534 r += (numrep+1) * ftotal;
535 else
536 /* r' = r + n*f_total */
537 r += numrep * ftotal;
538
539 /* bucket choose */
540 if (in->size == 0) {
541 dprintk(" empty bucket\n");
542 break;
543 }
544
545 item = crush_bucket_choose(in, x, r);
546 if (item >= map->max_devices) {
547 dprintk(" bad item %d\n", item);
548 out[rep] = CRUSH_ITEM_NONE;
549 if (out2)
550 out2[rep] = CRUSH_ITEM_NONE;
551 left--;
552 break;
553 }
554
555 /* desired type? */
556 if (item < 0)
557 itemtype = map->buckets[-1-item]->type;
558 else
559 itemtype = 0;
560 dprintk(" item %d type %d\n", item, itemtype);
561
562 /* keep going? */
563 if (itemtype != type) {
564 if (item >= 0 ||
565 (-1-item) >= map->max_buckets) {
566 dprintk(" bad item type %d\n", type);
567 out[rep] = CRUSH_ITEM_NONE;
568 if (out2)
569 out2[rep] =
570 CRUSH_ITEM_NONE;
571 left--;
572 break;
573 }
574 in = map->buckets[-1-item];
575 continue;
576 }
577
578 /* collision? */
579 collide = 0;
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200580 for (i = outpos; i < endpos; i++) {
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200581 if (out[i] == item) {
582 collide = 1;
583 break;
584 }
585 }
586 if (collide)
587 break;
588
589 if (recurse_to_leaf) {
590 if (item < 0) {
591 crush_choose_indep(map,
Ilya Dryomovbe3226a2013-12-24 21:19:26 +0200592 map->buckets[-1-item],
593 weight, weight_max,
594 x, 1, numrep, 0,
595 out2, rep,
Ilya Dryomov2d8be0b2013-12-24 21:19:27 +0200596 recurse_tries, 0,
Ilya Dryomovbe3226a2013-12-24 21:19:26 +0200597 0, NULL, r);
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200598 if (out2[rep] == CRUSH_ITEM_NONE) {
599 /* placed nothing; no leaf */
600 break;
601 }
602 } else {
603 /* we already have a leaf! */
604 out2[rep] = item;
605 }
606 }
607
608 /* out? */
609 if (itemtype == 0 &&
610 is_out(map, weight, weight_max, item, x))
611 break;
612
613 /* yay! */
614 out[rep] = item;
615 left--;
616 break;
617 }
618 }
619 }
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200620 for (rep = outpos; rep < endpos; rep++) {
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200621 if (out[rep] == CRUSH_ITEM_UNDEF) {
622 out[rep] = CRUSH_ITEM_NONE;
623 }
624 if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) {
625 out2[rep] = CRUSH_ITEM_NONE;
626 }
627 }
628}
629
630/**
Sage Weil5ecc0a02009-10-06 11:31:11 -0700631 * crush_do_rule - calculate a mapping with the given input and rule
632 * @map: the crush_map
633 * @ruleno: the rule id
634 * @x: hash input
635 * @result: pointer to result vector
636 * @result_max: maximum result size
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200637 * @weight: weight vector (for map leaves)
638 * @weight_max: size of weight vector
Ilya Dryomove8ef19c2013-12-24 21:19:24 +0200639 * @scratch: scratch vector for private use; must be >= 3 * result_max
Sage Weil5ecc0a02009-10-06 11:31:11 -0700640 */
Sage Weil8b12d472012-05-07 15:38:35 -0700641int crush_do_rule(const struct crush_map *map,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700642 int ruleno, int x, int *result, int result_max,
Ilya Dryomove8ef19c2013-12-24 21:19:24 +0200643 const __u32 *weight, int weight_max,
644 int *scratch)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700645{
646 int result_len;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +0200647 int *a = scratch;
648 int *b = scratch + result_max;
649 int *c = scratch + result_max*2;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700650 int recurse_to_leaf;
651 int *w;
652 int wsize = 0;
653 int *o;
654 int osize;
655 int *tmp;
656 struct crush_rule *rule;
Sage Weil8b12d472012-05-07 15:38:35 -0700657 __u32 step;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700658 int i, j;
659 int numrep;
Ilya Dryomov45002262015-04-14 16:04:23 +0300660 int out_size;
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200661 /*
662 * the original choose_total_tries value was off by one (it
663 * counted "retries" and not "tries"). add one.
664 */
665 int choose_tries = map->choose_total_tries + 1;
Ilya Dryomovf18650a2013-12-24 21:19:26 +0200666 int choose_leaf_tries = 0;
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200667 /*
668 * the local tries values were counted as "retries", though,
669 * and need no adjustment
670 */
671 int choose_local_retries = map->choose_local_tries;
672 int choose_local_fallback_retries = map->choose_local_fallback_tries;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700673
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200674 int vary_r = map->chooseleaf_vary_r;
675
Sage Weila1f48952012-05-07 15:35:24 -0700676 if ((__u32)ruleno >= map->max_rules) {
677 dprintk(" bad ruleno %d\n", ruleno);
678 return 0;
679 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700680
681 rule = map->rules[ruleno];
682 result_len = 0;
683 w = a;
684 o = b;
685
Sage Weil5ecc0a02009-10-06 11:31:11 -0700686 for (step = 0; step < rule->len; step++) {
Ilya Dryomov8f99c852013-12-24 21:19:24 +0200687 int firstn = 0;
Sage Weil06682162012-05-07 15:35:48 -0700688 struct crush_rule_step *curstep = &rule->steps[step];
689
Sage Weil06682162012-05-07 15:35:48 -0700690 switch (curstep->op) {
Sage Weil5ecc0a02009-10-06 11:31:11 -0700691 case CRUSH_RULE_TAKE:
Sage Weil06682162012-05-07 15:35:48 -0700692 w[0] = curstep->arg1;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700693 wsize = 1;
694 break;
695
Ilya Dryomovcc10df42013-12-24 21:19:26 +0200696 case CRUSH_RULE_SET_CHOOSE_TRIES:
697 if (curstep->arg1 > 0)
698 choose_tries = curstep->arg1;
699 break;
700
Ilya Dryomov917edad2013-12-24 21:19:26 +0200701 case CRUSH_RULE_SET_CHOOSELEAF_TRIES:
Ilya Dryomovbe3226a2013-12-24 21:19:26 +0200702 if (curstep->arg1 > 0)
703 choose_leaf_tries = curstep->arg1;
704 break;
705
Ilya Dryomovf046bf92013-12-24 21:19:27 +0200706 case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES:
Ilya Dryomov6ed10022014-03-19 16:58:37 +0200707 if (curstep->arg1 >= 0)
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200708 choose_local_retries = curstep->arg1;
Ilya Dryomovf046bf92013-12-24 21:19:27 +0200709 break;
710
711 case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES:
Ilya Dryomov6ed10022014-03-19 16:58:37 +0200712 if (curstep->arg1 >= 0)
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200713 choose_local_fallback_retries = curstep->arg1;
Ilya Dryomovf046bf92013-12-24 21:19:27 +0200714 break;
715
Ilya Dryomovd83ed852014-03-19 16:58:37 +0200716 case CRUSH_RULE_SET_CHOOSELEAF_VARY_R:
717 if (curstep->arg1 >= 0)
718 vary_r = curstep->arg1;
719 break;
720
Ilya Dryomov917edad2013-12-24 21:19:26 +0200721 case CRUSH_RULE_CHOOSELEAF_FIRSTN:
Sage Weil5ecc0a02009-10-06 11:31:11 -0700722 case CRUSH_RULE_CHOOSE_FIRSTN:
723 firstn = 1;
Sage Weil06682162012-05-07 15:35:48 -0700724 /* fall through */
Ilya Dryomov917edad2013-12-24 21:19:26 +0200725 case CRUSH_RULE_CHOOSELEAF_INDEP:
Sage Weil5ecc0a02009-10-06 11:31:11 -0700726 case CRUSH_RULE_CHOOSE_INDEP:
Sage Weila1f48952012-05-07 15:35:24 -0700727 if (wsize == 0)
728 break;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700729
730 recurse_to_leaf =
Sage Weil06682162012-05-07 15:35:48 -0700731 curstep->op ==
Ilya Dryomov917edad2013-12-24 21:19:26 +0200732 CRUSH_RULE_CHOOSELEAF_FIRSTN ||
Sage Weil06682162012-05-07 15:35:48 -0700733 curstep->op ==
Ilya Dryomov917edad2013-12-24 21:19:26 +0200734 CRUSH_RULE_CHOOSELEAF_INDEP;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700735
736 /* reset output */
737 osize = 0;
738
739 for (i = 0; i < wsize; i++) {
740 /*
741 * see CRUSH_N, CRUSH_N_MINUS macros.
742 * basically, numrep <= 0 means relative to
743 * the provided result_max
744 */
Sage Weil06682162012-05-07 15:35:48 -0700745 numrep = curstep->arg1;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700746 if (numrep <= 0) {
747 numrep += result_max;
748 if (numrep <= 0)
749 continue;
750 }
751 j = 0;
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200752 if (firstn) {
Ilya Dryomovd390bb22013-12-24 21:19:26 +0200753 int recurse_tries;
754 if (choose_leaf_tries)
755 recurse_tries =
756 choose_leaf_tries;
757 else if (map->chooseleaf_descend_once)
758 recurse_tries = 1;
759 else
760 recurse_tries = choose_tries;
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200761 osize += crush_choose_firstn(
762 map,
763 map->buckets[-1-w[i]],
764 weight, weight_max,
765 x, numrep,
766 curstep->arg2,
767 o+osize, j,
Ilya Dryomov45002262015-04-14 16:04:23 +0300768 result_max-osize,
Ilya Dryomovf18650a2013-12-24 21:19:26 +0200769 choose_tries,
Ilya Dryomovd390bb22013-12-24 21:19:26 +0200770 recurse_tries,
Ilya Dryomov48a163d2014-03-19 16:58:36 +0200771 choose_local_retries,
772 choose_local_fallback_retries,
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200773 recurse_to_leaf,
Ilya Dryomove2b149c2014-03-19 16:58:37 +0200774 vary_r,
775 c+osize,
776 0);
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200777 } else {
Ilya Dryomov45002262015-04-14 16:04:23 +0300778 out_size = ((numrep < (result_max-osize)) ?
779 numrep : (result_max-osize));
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200780 crush_choose_indep(
781 map,
782 map->buckets[-1-w[i]],
783 weight, weight_max,
Ilya Dryomov45002262015-04-14 16:04:23 +0300784 x, out_size, numrep,
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200785 curstep->arg2,
786 o+osize, j,
Ilya Dryomovf18650a2013-12-24 21:19:26 +0200787 choose_tries,
Ilya Dryomovd390bb22013-12-24 21:19:26 +0200788 choose_leaf_tries ?
789 choose_leaf_tries : 1,
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200790 recurse_to_leaf,
Ilya Dryomov41586082013-12-24 21:19:25 +0200791 c+osize,
792 0);
Ilya Dryomov45002262015-04-14 16:04:23 +0300793 osize += out_size;
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200794 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700795 }
796
797 if (recurse_to_leaf)
798 /* copy final _leaf_ values to output set */
799 memcpy(o, c, osize*sizeof(*o));
800
Ilya Dryomov2a4ba742013-12-24 21:19:24 +0200801 /* swap o and w arrays */
Sage Weil5ecc0a02009-10-06 11:31:11 -0700802 tmp = o;
803 o = w;
804 w = tmp;
805 wsize = osize;
806 break;
807
808
809 case CRUSH_RULE_EMIT:
810 for (i = 0; i < wsize && result_len < result_max; i++) {
811 result[result_len] = w[i];
812 result_len++;
813 }
814 wsize = 0;
815 break;
816
817 default:
Sage Weila1f48952012-05-07 15:35:24 -0700818 dprintk(" unknown op %d at step %d\n",
819 curstep->op, step);
820 break;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700821 }
822 }
Sage Weilf1932fc2011-12-07 09:10:26 -0800823 return result_len;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700824}
825
826