blob: 71ce4f12a7c9fd73531f661bc15620425b8adc53 [file] [log] [blame]
Sage Weil5ecc0a02009-10-06 11:31:11 -07001
2#ifdef __KERNEL__
3# include <linux/string.h>
4# include <linux/slab.h>
5# include <linux/bug.h>
6# include <linux/kernel.h>
7# ifndef dprintk
8# define dprintk(args...)
9# endif
10#else
11# include <string.h>
12# include <stdio.h>
13# include <stdlib.h>
14# include <assert.h>
15# define BUG_ON(x) assert(!(x))
16# define dprintk(args...) /* printf(args) */
17# define kmalloc(x, f) malloc(x)
18# define kfree(x) free(x)
19#endif
20
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070021#include <linux/crush/crush.h>
22#include <linux/crush/hash.h>
hartleysfeb50ac2012-04-24 14:38:37 +000023#include <linux/crush/mapper.h>
Sage Weil5ecc0a02009-10-06 11:31:11 -070024
25/*
26 * Implement the core CRUSH mapping algorithm.
27 */
28
29/**
30 * crush_find_rule - find a crush_rule id for a given ruleset, type, and size.
31 * @map: the crush_map
32 * @ruleset: the storage ruleset id (user defined)
33 * @type: storage ruleset type (user defined)
34 * @size: output set size
35 */
Sage Weil8b12d472012-05-07 15:38:35 -070036int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
Sage Weil5ecc0a02009-10-06 11:31:11 -070037{
Sage Weil8b12d472012-05-07 15:38:35 -070038 __u32 i;
Sage Weil5ecc0a02009-10-06 11:31:11 -070039
40 for (i = 0; i < map->max_rules; i++) {
41 if (map->rules[i] &&
42 map->rules[i]->mask.ruleset == ruleset &&
43 map->rules[i]->mask.type == type &&
44 map->rules[i]->mask.min_size <= size &&
45 map->rules[i]->mask.max_size >= size)
46 return i;
47 }
48 return -1;
49}
50
51
52/*
53 * bucket choose methods
54 *
55 * For each bucket algorithm, we have a "choose" method that, given a
56 * crush input @x and replica position (usually, position in output set) @r,
57 * will produce an item in the bucket.
58 */
59
60/*
61 * Choose based on a random permutation of the bucket.
62 *
63 * We used to use some prime number arithmetic to do this, but it
64 * wasn't very random, and had some other bad behaviors. Instead, we
65 * calculate an actual random permutation of the bucket members.
66 * Since this is expensive, we optimize for the r=0 case, which
67 * captures the vast majority of calls.
68 */
69static int bucket_perm_choose(struct crush_bucket *bucket,
70 int x, int r)
71{
Eric Dumazet95c96172012-04-15 05:58:06 +000072 unsigned int pr = r % bucket->size;
73 unsigned int i, s;
Sage Weil5ecc0a02009-10-06 11:31:11 -070074
75 /* start a new permutation if @x has changed */
Sage Weil8b12d472012-05-07 15:38:35 -070076 if (bucket->perm_x != (__u32)x || bucket->perm_n == 0) {
Sage Weil5ecc0a02009-10-06 11:31:11 -070077 dprintk("bucket %d new x=%d\n", bucket->id, x);
78 bucket->perm_x = x;
79
80 /* optimize common r=0 case */
81 if (pr == 0) {
Sage Weilfb690392009-11-07 20:18:22 -080082 s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
Sage Weil5ecc0a02009-10-06 11:31:11 -070083 bucket->size;
84 bucket->perm[0] = s;
85 bucket->perm_n = 0xffff; /* magic value, see below */
86 goto out;
87 }
88
89 for (i = 0; i < bucket->size; i++)
90 bucket->perm[i] = i;
91 bucket->perm_n = 0;
92 } else if (bucket->perm_n == 0xffff) {
93 /* clean up after the r=0 case above */
94 for (i = 1; i < bucket->size; i++)
95 bucket->perm[i] = i;
96 bucket->perm[bucket->perm[0]] = 0;
97 bucket->perm_n = 1;
98 }
99
100 /* calculate permutation up to pr */
101 for (i = 0; i < bucket->perm_n; i++)
102 dprintk(" perm_choose have %d: %d\n", i, bucket->perm[i]);
103 while (bucket->perm_n <= pr) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000104 unsigned int p = bucket->perm_n;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700105 /* no point in swapping the final entry */
106 if (p < bucket->size - 1) {
Sage Weilfb690392009-11-07 20:18:22 -0800107 i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
Sage Weil5ecc0a02009-10-06 11:31:11 -0700108 (bucket->size - p);
109 if (i) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000110 unsigned int t = bucket->perm[p + i];
Sage Weil5ecc0a02009-10-06 11:31:11 -0700111 bucket->perm[p + i] = bucket->perm[p];
112 bucket->perm[p] = t;
113 }
114 dprintk(" perm_choose swap %d with %d\n", p, p+i);
115 }
116 bucket->perm_n++;
117 }
118 for (i = 0; i < bucket->size; i++)
119 dprintk(" perm_choose %d: %d\n", i, bucket->perm[i]);
120
121 s = bucket->perm[pr];
122out:
123 dprintk(" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->id,
124 bucket->size, x, r, pr, s);
125 return bucket->items[s];
126}
127
128/* uniform */
129static int bucket_uniform_choose(struct crush_bucket_uniform *bucket,
130 int x, int r)
131{
132 return bucket_perm_choose(&bucket->h, x, r);
133}
134
135/* list */
136static int bucket_list_choose(struct crush_bucket_list *bucket,
137 int x, int r)
138{
139 int i;
140
141 for (i = bucket->h.size-1; i >= 0; i--) {
Sage Weilfb690392009-11-07 20:18:22 -0800142 __u64 w = crush_hash32_4(bucket->h.hash,x, bucket->h.items[i],
143 r, bucket->h.id);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700144 w &= 0xffff;
145 dprintk("list_choose i=%d x=%d r=%d item %d weight %x "
146 "sw %x rand %llx",
147 i, x, r, bucket->h.items[i], bucket->item_weights[i],
148 bucket->sum_weights[i], w);
149 w *= bucket->sum_weights[i];
150 w = w >> 16;
151 /*dprintk(" scaled %llx\n", w);*/
152 if (w < bucket->item_weights[i])
153 return bucket->h.items[i];
154 }
155
Sage Weila1f48952012-05-07 15:35:24 -0700156 dprintk("bad list sums for bucket %d\n", bucket->h.id);
157 return bucket->h.items[0];
Sage Weil5ecc0a02009-10-06 11:31:11 -0700158}
159
160
161/* (binary) tree */
162static int height(int n)
163{
164 int h = 0;
165 while ((n & 1) == 0) {
166 h++;
167 n = n >> 1;
168 }
169 return h;
170}
171
172static int left(int x)
173{
174 int h = height(x);
175 return x - (1 << (h-1));
176}
177
178static int right(int x)
179{
180 int h = height(x);
181 return x + (1 << (h-1));
182}
183
184static int terminal(int x)
185{
186 return x & 1;
187}
188
189static int bucket_tree_choose(struct crush_bucket_tree *bucket,
190 int x, int r)
191{
Ilya Dryomov8f99c852013-12-24 21:19:24 +0200192 int n;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700193 __u32 w;
194 __u64 t;
195
196 /* start at root */
197 n = bucket->num_nodes >> 1;
198
199 while (!terminal(n)) {
Ilya Dryomov8f99c852013-12-24 21:19:24 +0200200 int l;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700201 /* pick point in [0, w) */
202 w = bucket->node_weights[n];
Sage Weilfb690392009-11-07 20:18:22 -0800203 t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
204 bucket->h.id) * (__u64)w;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700205 t = t >> 32;
206
207 /* descend to the left or right? */
208 l = left(n);
209 if (t < bucket->node_weights[l])
210 n = l;
211 else
212 n = right(n);
213 }
214
215 return bucket->h.items[n >> 1];
216}
217
218
219/* straw */
220
221static int bucket_straw_choose(struct crush_bucket_straw *bucket,
222 int x, int r)
223{
Sage Weil8b12d472012-05-07 15:38:35 -0700224 __u32 i;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700225 int high = 0;
226 __u64 high_draw = 0;
227 __u64 draw;
228
229 for (i = 0; i < bucket->h.size; i++) {
Sage Weilfb690392009-11-07 20:18:22 -0800230 draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700231 draw &= 0xffff;
232 draw *= bucket->straws[i];
233 if (i == 0 || draw > high_draw) {
234 high = i;
235 high_draw = draw;
236 }
237 }
238 return bucket->h.items[high];
239}
240
241static int crush_bucket_choose(struct crush_bucket *in, int x, int r)
242{
Sage Weila1a31e72010-06-24 12:58:14 -0700243 dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
Sage Weila1f48952012-05-07 15:35:24 -0700244 BUG_ON(in->size == 0);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700245 switch (in->alg) {
246 case CRUSH_BUCKET_UNIFORM:
247 return bucket_uniform_choose((struct crush_bucket_uniform *)in,
248 x, r);
249 case CRUSH_BUCKET_LIST:
250 return bucket_list_choose((struct crush_bucket_list *)in,
251 x, r);
252 case CRUSH_BUCKET_TREE:
253 return bucket_tree_choose((struct crush_bucket_tree *)in,
254 x, r);
255 case CRUSH_BUCKET_STRAW:
256 return bucket_straw_choose((struct crush_bucket_straw *)in,
257 x, r);
258 default:
Sage Weila1f48952012-05-07 15:35:24 -0700259 dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
Sage Weil50b885b2009-12-01 14:12:07 -0800260 return in->items[0];
Sage Weil5ecc0a02009-10-06 11:31:11 -0700261 }
262}
263
264/*
265 * true if device is marked "out" (failed, fully offloaded)
266 * of the cluster
267 */
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200268static int is_out(const struct crush_map *map,
269 const __u32 *weight, int weight_max,
270 int item, int x)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700271{
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200272 if (item >= weight_max)
273 return 1;
Sage Weil153a1092010-07-05 09:44:17 -0700274 if (weight[item] >= 0x10000)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700275 return 0;
276 if (weight[item] == 0)
277 return 1;
Sage Weilfb690392009-11-07 20:18:22 -0800278 if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
279 < weight[item])
Sage Weil5ecc0a02009-10-06 11:31:11 -0700280 return 0;
281 return 1;
282}
283
284/**
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200285 * crush_choose_firstn - choose numrep distinct items of given type
Sage Weil5ecc0a02009-10-06 11:31:11 -0700286 * @map: the crush_map
287 * @bucket: the bucket we are choose an item from
288 * @x: crush input value
289 * @numrep: the number of items to choose
290 * @type: the type of item to choose
291 * @out: pointer to output vector
292 * @outpos: our position in that vector
Sage Weil5ecc0a02009-10-06 11:31:11 -0700293 * @recurse_to_leaf: true if we want one device under each item of given type
294 * @out2: second output vector for leaf items (if @recurse_to_leaf)
295 */
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200296static int crush_choose_firstn(const struct crush_map *map,
297 struct crush_bucket *bucket,
298 const __u32 *weight, int weight_max,
299 int x, int numrep, int type,
300 int *out, int outpos,
Ilya Dryomov2d8be0b2013-12-24 21:19:27 +0200301 unsigned int tries,
302 unsigned int recurse_tries,
Ilya Dryomovf046bf92013-12-24 21:19:27 +0200303 unsigned int local_tries,
304 unsigned int local_fallback_tries,
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200305 int recurse_to_leaf,
Ilya Dryomovd390bb22013-12-24 21:19:26 +0200306 int *out2)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700307{
308 int rep;
Sage Weil8b12d472012-05-07 15:38:35 -0700309 unsigned int ftotal, flocal;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700310 int retry_descent, retry_bucket, skip_rep;
311 struct crush_bucket *in = bucket;
312 int r;
313 int i;
Sage Weilb28813a2009-10-07 10:59:34 -0700314 int item = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700315 int itemtype;
316 int collide, reject;
Sage Weila1a31e72010-06-24 12:58:14 -0700317
318 dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
319 bucket->id, x, outpos, numrep);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700320
321 for (rep = outpos; rep < numrep; rep++) {
322 /* keep trying until we get a non-out, non-colliding item */
323 ftotal = 0;
324 skip_rep = 0;
325 do {
326 retry_descent = 0;
327 in = bucket; /* initial bucket */
328
329 /* choose through intervening buckets */
330 flocal = 0;
331 do {
Sage Weilb28813a2009-10-07 10:59:34 -0700332 collide = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700333 retry_bucket = 0;
334 r = rep;
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200335 /* r' = r + f_total */
336 r += ftotal;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700337
338 /* bucket choose */
Sage Weilb28813a2009-10-07 10:59:34 -0700339 if (in->size == 0) {
340 reject = 1;
341 goto reject;
342 }
Ilya Dryomovf046bf92013-12-24 21:19:27 +0200343 if (local_fallback_tries > 0 &&
Sage Weil546f04e2012-07-30 18:15:23 -0700344 flocal >= (in->size>>1) &&
Ilya Dryomovf046bf92013-12-24 21:19:27 +0200345 flocal > local_fallback_tries)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700346 item = bucket_perm_choose(in, x, r);
347 else
348 item = crush_bucket_choose(in, x, r);
Sage Weila1f48952012-05-07 15:35:24 -0700349 if (item >= map->max_devices) {
350 dprintk(" bad item %d\n", item);
351 skip_rep = 1;
352 break;
353 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700354
355 /* desired type? */
356 if (item < 0)
357 itemtype = map->buckets[-1-item]->type;
358 else
359 itemtype = 0;
360 dprintk(" item %d type %d\n", item, itemtype);
361
362 /* keep going? */
363 if (itemtype != type) {
Sage Weila1f48952012-05-07 15:35:24 -0700364 if (item >= 0 ||
365 (-1-item) >= map->max_buckets) {
366 dprintk(" bad item type %d\n", type);
367 skip_rep = 1;
368 break;
369 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700370 in = map->buckets[-1-item];
Sage Weil55bda7a2010-06-24 12:55:48 -0700371 retry_bucket = 1;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700372 continue;
373 }
374
375 /* collision? */
Sage Weil5ecc0a02009-10-06 11:31:11 -0700376 for (i = 0; i < outpos; i++) {
377 if (out[i] == item) {
378 collide = 1;
379 break;
380 }
381 }
382
Sage Weila1a31e72010-06-24 12:58:14 -0700383 reject = 0;
Sage Weil7d7c1f62013-01-15 18:49:09 -0800384 if (!collide && recurse_to_leaf) {
Sage Weila1a31e72010-06-24 12:58:14 -0700385 if (item < 0) {
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200386 if (crush_choose_firstn(map,
Sage Weila1a31e72010-06-24 12:58:14 -0700387 map->buckets[-1-item],
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200388 weight, weight_max,
Sage Weila1a31e72010-06-24 12:58:14 -0700389 x, outpos+1, 0,
390 out2, outpos,
Ilya Dryomov2d8be0b2013-12-24 21:19:27 +0200391 recurse_tries, 0,
Ilya Dryomovf046bf92013-12-24 21:19:27 +0200392 local_tries,
393 local_fallback_tries,
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200394 0,
Sage Weila1a31e72010-06-24 12:58:14 -0700395 NULL) <= outpos)
396 /* didn't get leaf */
397 reject = 1;
398 } else {
399 /* we already have a leaf! */
400 out2[outpos] = item;
401 }
402 }
403
404 if (!reject) {
Sage Weil5ecc0a02009-10-06 11:31:11 -0700405 /* out? */
406 if (itemtype == 0)
407 reject = is_out(map, weight,
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200408 weight_max,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700409 item, x);
410 else
411 reject = 0;
412 }
413
Sage Weilb28813a2009-10-07 10:59:34 -0700414reject:
Sage Weil5ecc0a02009-10-06 11:31:11 -0700415 if (reject || collide) {
416 ftotal++;
417 flocal++;
418
Ilya Dryomovf046bf92013-12-24 21:19:27 +0200419 if (collide && flocal <= local_tries)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700420 /* retry locally a few times */
421 retry_bucket = 1;
Ilya Dryomovf046bf92013-12-24 21:19:27 +0200422 else if (local_fallback_tries > 0 &&
423 flocal <= in->size + local_fallback_tries)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700424 /* exhaustive bucket search */
425 retry_bucket = 1;
Ilya Dryomov2d8be0b2013-12-24 21:19:27 +0200426 else if (ftotal <= tries)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700427 /* then retry descent */
428 retry_descent = 1;
429 else
430 /* else give up */
431 skip_rep = 1;
432 dprintk(" reject %d collide %d "
Sage Weil8b12d472012-05-07 15:38:35 -0700433 "ftotal %u flocal %u\n",
Sage Weil5ecc0a02009-10-06 11:31:11 -0700434 reject, collide, ftotal,
435 flocal);
436 }
437 } while (retry_bucket);
438 } while (retry_descent);
439
440 if (skip_rep) {
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200441 dprintk("skip rep\n");
442 continue;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700443 }
444
Sage Weila1a31e72010-06-24 12:58:14 -0700445 dprintk("CHOOSE got %d\n", item);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700446 out[outpos] = item;
447 outpos++;
448 }
449
Sage Weila1a31e72010-06-24 12:58:14 -0700450 dprintk("CHOOSE returns %d\n", outpos);
Sage Weil5ecc0a02009-10-06 11:31:11 -0700451 return outpos;
452}
453
454
455/**
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200456 * crush_choose_indep: alternative breadth-first positionally stable mapping
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200457 *
458 */
459static void crush_choose_indep(const struct crush_map *map,
Ilya Dryomovbe3226a2013-12-24 21:19:26 +0200460 struct crush_bucket *bucket,
461 const __u32 *weight, int weight_max,
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200462 int x, int left, int numrep, int type,
Ilya Dryomovbe3226a2013-12-24 21:19:26 +0200463 int *out, int outpos,
Ilya Dryomov2d8be0b2013-12-24 21:19:27 +0200464 unsigned int tries,
465 unsigned int recurse_tries,
Ilya Dryomovbe3226a2013-12-24 21:19:26 +0200466 int recurse_to_leaf,
Ilya Dryomov41586082013-12-24 21:19:25 +0200467 int *out2,
468 int parent_r)
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200469{
470 struct crush_bucket *in = bucket;
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200471 int endpos = outpos + left;
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200472 int rep;
473 unsigned int ftotal;
474 int r;
475 int i;
476 int item = 0;
477 int itemtype;
478 int collide;
479
480 dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
481 bucket->id, x, outpos, numrep);
482
483 /* initially my result is undefined */
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200484 for (rep = outpos; rep < endpos; rep++) {
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200485 out[rep] = CRUSH_ITEM_UNDEF;
486 if (out2)
487 out2[rep] = CRUSH_ITEM_UNDEF;
488 }
489
Ilya Dryomov2d8be0b2013-12-24 21:19:27 +0200490 for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) {
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200491 for (rep = outpos; rep < endpos; rep++) {
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200492 if (out[rep] != CRUSH_ITEM_UNDEF)
493 continue;
494
495 in = bucket; /* initial bucket */
496
497 /* choose through intervening buckets */
498 for (;;) {
Ilya Dryomov3102b0a2013-12-24 21:19:25 +0200499 /* note: we base the choice on the position
500 * even in the nested call. that means that
501 * if the first layer chooses the same bucket
502 * in a different position, we will tend to
503 * choose a different item in that bucket.
504 * this will involve more devices in data
505 * movement and tend to distribute the load.
506 */
Ilya Dryomov41586082013-12-24 21:19:25 +0200507 r = rep + parent_r;
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200508
509 /* be careful */
510 if (in->alg == CRUSH_BUCKET_UNIFORM &&
511 in->size % numrep == 0)
512 /* r'=r+(n+1)*f_total */
513 r += (numrep+1) * ftotal;
514 else
515 /* r' = r + n*f_total */
516 r += numrep * ftotal;
517
518 /* bucket choose */
519 if (in->size == 0) {
520 dprintk(" empty bucket\n");
521 break;
522 }
523
524 item = crush_bucket_choose(in, x, r);
525 if (item >= map->max_devices) {
526 dprintk(" bad item %d\n", item);
527 out[rep] = CRUSH_ITEM_NONE;
528 if (out2)
529 out2[rep] = CRUSH_ITEM_NONE;
530 left--;
531 break;
532 }
533
534 /* desired type? */
535 if (item < 0)
536 itemtype = map->buckets[-1-item]->type;
537 else
538 itemtype = 0;
539 dprintk(" item %d type %d\n", item, itemtype);
540
541 /* keep going? */
542 if (itemtype != type) {
543 if (item >= 0 ||
544 (-1-item) >= map->max_buckets) {
545 dprintk(" bad item type %d\n", type);
546 out[rep] = CRUSH_ITEM_NONE;
547 if (out2)
548 out2[rep] =
549 CRUSH_ITEM_NONE;
550 left--;
551 break;
552 }
553 in = map->buckets[-1-item];
554 continue;
555 }
556
557 /* collision? */
558 collide = 0;
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200559 for (i = outpos; i < endpos; i++) {
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200560 if (out[i] == item) {
561 collide = 1;
562 break;
563 }
564 }
565 if (collide)
566 break;
567
568 if (recurse_to_leaf) {
569 if (item < 0) {
570 crush_choose_indep(map,
Ilya Dryomovbe3226a2013-12-24 21:19:26 +0200571 map->buckets[-1-item],
572 weight, weight_max,
573 x, 1, numrep, 0,
574 out2, rep,
Ilya Dryomov2d8be0b2013-12-24 21:19:27 +0200575 recurse_tries, 0,
Ilya Dryomovbe3226a2013-12-24 21:19:26 +0200576 0, NULL, r);
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200577 if (out2[rep] == CRUSH_ITEM_NONE) {
578 /* placed nothing; no leaf */
579 break;
580 }
581 } else {
582 /* we already have a leaf! */
583 out2[rep] = item;
584 }
585 }
586
587 /* out? */
588 if (itemtype == 0 &&
589 is_out(map, weight, weight_max, item, x))
590 break;
591
592 /* yay! */
593 out[rep] = item;
594 left--;
595 break;
596 }
597 }
598 }
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200599 for (rep = outpos; rep < endpos; rep++) {
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200600 if (out[rep] == CRUSH_ITEM_UNDEF) {
601 out[rep] = CRUSH_ITEM_NONE;
602 }
603 if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) {
604 out2[rep] = CRUSH_ITEM_NONE;
605 }
606 }
607}
608
609/**
Sage Weil5ecc0a02009-10-06 11:31:11 -0700610 * crush_do_rule - calculate a mapping with the given input and rule
611 * @map: the crush_map
612 * @ruleno: the rule id
613 * @x: hash input
614 * @result: pointer to result vector
615 * @result_max: maximum result size
Ilya Dryomovb3b33b02013-12-24 21:19:24 +0200616 * @weight: weight vector (for map leaves)
617 * @weight_max: size of weight vector
Ilya Dryomove8ef19c2013-12-24 21:19:24 +0200618 * @scratch: scratch vector for private use; must be >= 3 * result_max
Sage Weil5ecc0a02009-10-06 11:31:11 -0700619 */
Sage Weil8b12d472012-05-07 15:38:35 -0700620int crush_do_rule(const struct crush_map *map,
Sage Weil5ecc0a02009-10-06 11:31:11 -0700621 int ruleno, int x, int *result, int result_max,
Ilya Dryomove8ef19c2013-12-24 21:19:24 +0200622 const __u32 *weight, int weight_max,
623 int *scratch)
Sage Weil5ecc0a02009-10-06 11:31:11 -0700624{
625 int result_len;
Ilya Dryomove8ef19c2013-12-24 21:19:24 +0200626 int *a = scratch;
627 int *b = scratch + result_max;
628 int *c = scratch + result_max*2;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700629 int recurse_to_leaf;
630 int *w;
631 int wsize = 0;
632 int *o;
633 int osize;
634 int *tmp;
635 struct crush_rule *rule;
Sage Weil8b12d472012-05-07 15:38:35 -0700636 __u32 step;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700637 int i, j;
638 int numrep;
Ilya Dryomovf18650a2013-12-24 21:19:26 +0200639 int choose_tries = map->choose_total_tries;
Ilya Dryomovf046bf92013-12-24 21:19:27 +0200640 int choose_local_tries = map->choose_local_tries;
641 int choose_local_fallback_tries = map->choose_local_fallback_tries;
Ilya Dryomovf18650a2013-12-24 21:19:26 +0200642 int choose_leaf_tries = 0;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700643
Sage Weila1f48952012-05-07 15:35:24 -0700644 if ((__u32)ruleno >= map->max_rules) {
645 dprintk(" bad ruleno %d\n", ruleno);
646 return 0;
647 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700648
649 rule = map->rules[ruleno];
650 result_len = 0;
651 w = a;
652 o = b;
653
Sage Weil5ecc0a02009-10-06 11:31:11 -0700654 for (step = 0; step < rule->len; step++) {
Ilya Dryomov8f99c852013-12-24 21:19:24 +0200655 int firstn = 0;
Sage Weil06682162012-05-07 15:35:48 -0700656 struct crush_rule_step *curstep = &rule->steps[step];
657
Sage Weil06682162012-05-07 15:35:48 -0700658 switch (curstep->op) {
Sage Weil5ecc0a02009-10-06 11:31:11 -0700659 case CRUSH_RULE_TAKE:
Sage Weil06682162012-05-07 15:35:48 -0700660 w[0] = curstep->arg1;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700661 wsize = 1;
662 break;
663
Ilya Dryomovcc10df42013-12-24 21:19:26 +0200664 case CRUSH_RULE_SET_CHOOSE_TRIES:
665 if (curstep->arg1 > 0)
666 choose_tries = curstep->arg1;
667 break;
668
Ilya Dryomov917edad2013-12-24 21:19:26 +0200669 case CRUSH_RULE_SET_CHOOSELEAF_TRIES:
Ilya Dryomovbe3226a2013-12-24 21:19:26 +0200670 if (curstep->arg1 > 0)
671 choose_leaf_tries = curstep->arg1;
672 break;
673
Ilya Dryomovf046bf92013-12-24 21:19:27 +0200674 case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES:
675 if (curstep->arg1 > 0)
676 choose_local_tries = curstep->arg1;
677 break;
678
679 case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES:
680 if (curstep->arg1 > 0)
681 choose_local_fallback_tries = curstep->arg1;
682 break;
683
Ilya Dryomov917edad2013-12-24 21:19:26 +0200684 case CRUSH_RULE_CHOOSELEAF_FIRSTN:
Sage Weil5ecc0a02009-10-06 11:31:11 -0700685 case CRUSH_RULE_CHOOSE_FIRSTN:
686 firstn = 1;
Sage Weil06682162012-05-07 15:35:48 -0700687 /* fall through */
Ilya Dryomov917edad2013-12-24 21:19:26 +0200688 case CRUSH_RULE_CHOOSELEAF_INDEP:
Sage Weil5ecc0a02009-10-06 11:31:11 -0700689 case CRUSH_RULE_CHOOSE_INDEP:
Sage Weila1f48952012-05-07 15:35:24 -0700690 if (wsize == 0)
691 break;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700692
693 recurse_to_leaf =
Sage Weil06682162012-05-07 15:35:48 -0700694 curstep->op ==
Ilya Dryomov917edad2013-12-24 21:19:26 +0200695 CRUSH_RULE_CHOOSELEAF_FIRSTN ||
Sage Weil06682162012-05-07 15:35:48 -0700696 curstep->op ==
Ilya Dryomov917edad2013-12-24 21:19:26 +0200697 CRUSH_RULE_CHOOSELEAF_INDEP;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700698
699 /* reset output */
700 osize = 0;
701
702 for (i = 0; i < wsize; i++) {
703 /*
704 * see CRUSH_N, CRUSH_N_MINUS macros.
705 * basically, numrep <= 0 means relative to
706 * the provided result_max
707 */
Sage Weil06682162012-05-07 15:35:48 -0700708 numrep = curstep->arg1;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700709 if (numrep <= 0) {
710 numrep += result_max;
711 if (numrep <= 0)
712 continue;
713 }
714 j = 0;
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200715 if (firstn) {
Ilya Dryomovd390bb22013-12-24 21:19:26 +0200716 int recurse_tries;
717 if (choose_leaf_tries)
718 recurse_tries =
719 choose_leaf_tries;
720 else if (map->chooseleaf_descend_once)
721 recurse_tries = 1;
722 else
723 recurse_tries = choose_tries;
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200724 osize += crush_choose_firstn(
725 map,
726 map->buckets[-1-w[i]],
727 weight, weight_max,
728 x, numrep,
729 curstep->arg2,
730 o+osize, j,
Ilya Dryomovf18650a2013-12-24 21:19:26 +0200731 choose_tries,
Ilya Dryomovd390bb22013-12-24 21:19:26 +0200732 recurse_tries,
Ilya Dryomovf046bf92013-12-24 21:19:27 +0200733 choose_local_tries,
734 choose_local_fallback_tries,
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200735 recurse_to_leaf,
Ilya Dryomovd390bb22013-12-24 21:19:26 +0200736 c+osize);
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200737 } else {
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200738 crush_choose_indep(
739 map,
740 map->buckets[-1-w[i]],
741 weight, weight_max,
Ilya Dryomovab4ce2b52013-12-24 21:19:25 +0200742 x, numrep, numrep,
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200743 curstep->arg2,
744 o+osize, j,
Ilya Dryomovf18650a2013-12-24 21:19:26 +0200745 choose_tries,
Ilya Dryomovd390bb22013-12-24 21:19:26 +0200746 choose_leaf_tries ?
747 choose_leaf_tries : 1,
Ilya Dryomov9fe07182013-12-24 21:19:25 +0200748 recurse_to_leaf,
Ilya Dryomov41586082013-12-24 21:19:25 +0200749 c+osize,
750 0);
Ilya Dryomov9a3b4902013-12-24 21:19:25 +0200751 osize += numrep;
752 }
Sage Weil5ecc0a02009-10-06 11:31:11 -0700753 }
754
755 if (recurse_to_leaf)
756 /* copy final _leaf_ values to output set */
757 memcpy(o, c, osize*sizeof(*o));
758
Ilya Dryomov2a4ba742013-12-24 21:19:24 +0200759 /* swap o and w arrays */
Sage Weil5ecc0a02009-10-06 11:31:11 -0700760 tmp = o;
761 o = w;
762 w = tmp;
763 wsize = osize;
764 break;
765
766
767 case CRUSH_RULE_EMIT:
768 for (i = 0; i < wsize && result_len < result_max; i++) {
769 result[result_len] = w[i];
770 result_len++;
771 }
772 wsize = 0;
773 break;
774
775 default:
Sage Weila1f48952012-05-07 15:35:24 -0700776 dprintk(" unknown op %d at step %d\n",
777 curstep->op, step);
778 break;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700779 }
780 }
Sage Weilf1932fc2011-12-07 09:10:26 -0800781 return result_len;
Sage Weil5ecc0a02009-10-06 11:31:11 -0700782}
783
784